aboutsummaryrefslogtreecommitdiff
path: root/build.sh
blob: 0018c29f2e57d5f6f8e71cdf856bd83e148c8fe6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env bash

set -e
set -x


if [ ! -x "$(which ld)" -o ! -x "$(which gcc)" ]; then
	echo "${0}: missing binutils/gcc"
	exit 1
fi

if [ ! -x /usr/bin/wget -o ! -x /usr/bin/whiptail ]; then
	echo "${0}: missing wget/whiptail"
	echo "${0}: On Debian: \`sudo apt install wget whiptail\`"
	echo "${0}: On ArchLinux: \`sudo pacman -S wget libnewt\`"
	exit 1
fi

BIN_DLSITE="https://ftp.gnu.org/gnu/binutils"
GCC_DLSITE="https://bigsearcher.com/mirrors/gcc/releases"
CPUCORES=$(cat /proc/cpuinfo | grep -E '^processor' | wc -l)

# download choosen binutils version
BIN_CONTENT=$(wget "${BIN_DLSITE}" -q -O - | grep -oE '>binutils-[[:digit:]]+.[[:digit:]]+(|.[[:digit:]]+)(|.[[:digit:]]+).tar.gz<')
BIN_MENU=$(echo "${BIN_CONTENT}" | sed -n 's/^>binutils-\(.*\).tar.gz<$/\1 binutils-\1/p')
BINUTILS_VERSION=$(whiptail --menu 'choose binutils version' 35 55 25 ${BIN_MENU} 3>&1 1>&2 2>&3)
echo "binutils: ${BINUTILS_VERSION}"

# download choosen gcc version
GCC_CONTENT=$(wget "${GCC_DLSITE}" -q -O - | grep -oE '"gcc-[[:digit:]]+.[[:digit:]]+(|.[[:digit:]]+)/"')
GCC_MENU=$(echo "${GCC_CONTENT}" | sed -n 's/^"gcc-\(.*\)\/"$/\1 gcc-\1/p')
GCC_VERSION=$(whiptail --menu 'choose gcc version' 35 55 25 ${GCC_MENU} 3>&1 1>&2 2>&3)
echo "gcc: ${GCC_VERSION}"

# enable multilib?
MULTILIB_ENABLE=$(whiptail --clear --menu 'enable multilib? (requires libc-dev:i386 as well as libc-dev:amd64)' 15 35 5 y yes n no 3>&1 1>&2 2>&3)
if [ x"${MULTILIB_ENABLE}" = x'y' ]; then
	MULTILIB_ARG="--enable-multilib"
else
	MULTILIB_ARG="--disable-multilib"
fi

# build gccgo?
GCCGO_ENABLE=$(whiptail --clear --menu 'build gccgo?' 15 35 5 y yes n no 3>&1 1>&2 2>&3)
if [ x"${GCCGO_ENABLE}" = x'y' ]; then
	GCCGO_BIN="--enable-gold=yes"
	GCCGO_GCC=",go"
else
	GCCGO_BIN="--enable-ld=yes"
	GCCGO_GCC=""
fi

# check build dirs existence
BIN_BUILD="binutils-${BINUTILS_VERSION}-build"
if [ -d ${BIN_BUILD} ]; then
	ANSW=$(whiptail --clear --menu 'binutils-build exists: delete?' 15 35 5 y yes n no 3>&1 1>&2 2>&3)
	if [ x"${ANSW}" = x'y' ]; then
		rm -rf ${BIN_BUILD}
	fi
fi
GCC_BUILD="gcc-${GCC_VERSION}-build"
if [ -d ${GCC_BUILD} ]; then
	ANSW=$(whiptail --clear --menu 'gcc-build exists: delete?' 15 35 5 y yes n no 3>&1 1>&2 2>&3)
	if [ x"${ANSW}" = x'y' ]; then
		rm -rf ${GCC_BUILD}
	fi
fi

# "sysroot"
INSTALLDIR="$(pwd)/gcc-${GCC_VERSION}-root"
if [ -d "${INSTALLDIR}" ]; then
	ANSW=$(whiptail --menu 'sysroot gcc-'"${GCC_VERSION}"'-root exists: delete?' 15 35 5 y yes n no 3>&1 1>&2 2>&3)
	if [ x"${ANSW}" = x'y' ]; then
		rm -rf "${INSTALLDIR}"
	fi
fi

# get the source code
if [ ! -f "binutils-${BINUTILS_VERSION}.tar.gz" ]; then
	wget -O "binutils-${BINUTILS_VERSION}.tar.gz" "${BIN_DLSITE}/binutils-${BINUTILS_VERSION}.tar.gz"
fi
test -d ${BIN_BUILD} || tar -xvf binutils-${BINUTILS_VERSION}.tar.gz
if [ ! -f "gcc-${GCC_VERSION}.tar.bz2" -a ! -f "gcc-${GCC_VERSION}.tar.gz" ]; then
	wget -O "gcc-${GCC_VERSION}.tar.bz2" "${GCC_DLSITE}/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.bz2" || \
        { rm -f "gcc-${GCC_VERSION}.tar.bz2"; \
            wget -O "gcc-${GCC_VERSION}.tar.gz" "${GCC_DLSITE}/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.gz"; }
fi
test ! -d ${GCC_BUILD} -a -r gcc-${GCC_VERSION}.tar.bz2 && tar -xvf gcc-${GCC_VERSION}.tar.bz2
test ! -d ${GCC_BUILD} -a -r gcc-${GCC_VERSION}.tar.gz && tar -xvf gcc-${GCC_VERSION}.tar.gz

# download the prerequisites e.g. GMP,MPFR,MPC
cd gcc-${GCC_VERSION}
if [ -x ./contrib/download_prerequisites ]; then
	./contrib/download_prerequisites
else
	# c&p from gcc5 `download_prerequisites` script
	# Necessary to build GCC.
	MPFR=mpfr-2.4.2
	GMP=gmp-4.3.2
	MPC=mpc-0.8.1
	if [ ! -f "$MPFR.tar.bz2" -o ! -h mpfr ]; then
		rm -rf mpfr
		wget -O "$MPFR.tar.bz2" "ftp://gcc.gnu.org/pub/gcc/infrastructure/$MPFR.tar.bz2"
		tar xjf $MPFR.tar.bz2
		ln -sf $MPFR mpfr
	fi
	if [ ! -f "$GMP.tar.bz2" -o ! -h gmp ]; then
		rm -rf gmp
		wget -O "$GMP.tar.bz2" "ftp://gcc.gnu.org/pub/gcc/infrastructure/$GMP.tar.bz2"
		tar xjf $GMP.tar.bz2
		ln -sf $GMP gmp
	fi
	if [ ! -f "$MPC.tar.gz" -o ! -h mpc ]; then
		rm -rf mpc
		wget -O "$MPC.tar.gz" "ftp://gcc.gnu.org/pub/gcc/infrastructure/$MPC.tar.gz"
		tar xzf $MPC.tar.gz
		ln -sf $MPC mpc
	fi
fi

# set multiarch vars (if debian based)
if [ -x /usr/bin/dpkg-architecture ]; then
	BUILD_ARCH=$(/usr/bin/dpkg-architecture -q DEB_BUILD_MULTIARCH)
	HOST_ARCH=$(/usr/bin/dpkg-architecture -q DEB_HOST_MULTIARCH)
	TARGET_ARCH=$(/usr/bin/dpkg-architecture -q DEB_TARGET_MULTIARCH)
	MULTIARCH="--build=${BUILD_ARCH} --host=${HOST_ARCH} --target=${TARGET_ARCH}"
	LPATH="/usr/lib/${TARGET_ARCH}"
else
	MULTIARCH=""
	LPATH="/usr/lib"
fi

if [ x"${LIBRARY_PATH}" != x ]; then
	export LIBRARY_PATH="${LIBRARY_PATH}:${LPATH}"
else
	export LIBRARY_PATH="${LPATH}"
fi
unset LPATH

# create the build directories
cd ..
mkdir ${BIN_BUILD} ${GCC_BUILD} || true

cd ${BIN_BUILD}
# build binutils
../binutils-${BINUTILS_VERSION}/configure            \
    ${MULTIARCH}                                     \
    ${MULTILIB_ARG}                                  \
    --prefix=${INSTALLDIR}                           \
    --disable-nls                                    \
    ${GCCGO_BIN}                                     \
&& sed -i 's|^MAKEINFO\s\+=\s\+makeinfo$|MAKEINFO = true|' ./Makefile \
&& make -j${CPUCORES:-2}                             \
&& make install

cd ../${GCC_BUILD}
# build gcc
../gcc-${GCC_VERSION}/configure                      \
    --prefix=${INSTALLDIR}                           \
    ${MULTIARCH}                                     \
    --with-as=${INSTALLDIR}/bin/as                   \
    --with-ld=${INSTALLDIR}/bin/ld                   \
    --enable-static                                  \
    --enable-shared                                  \
    --enable-threads=posix                           \
    --enable-__cxa_atexit                            \
    --enable-clocale=gnu                             \
    --enable-languages=c,c++${GCCGO_GCC}             \
    ${MULTILIB_ARG}                                  \
    --with-system-zlib                               \
    ${GCCGO_BIN}                                     \
    --enable-lto                                     \
    --disable-nls                                    \
&& sed -i 's|^MAKEINFO\s\+=\s\+makeinfo$|MAKEINFO = true|' ./Makefile \
&& make -j${CPUCORES:-2}                             \
&& make install

# write activation script to gcc root
cat << EOF > "${INSTALLDIR}/activate.sh"
#!/bin/bash

DIR="\$(realpath "\$(dirname "\${BASH_SOURCE}")")"
echo "*** ROOT: \${DIR}"
export PATH="\${DIR}/bin:\${DIR}/usr/bin:\${PATH}"
export CMAKE_C_COMPILER="\${DIR}/bin/gcc"
export CMAKE_CXX_COMPILER="\${DIR}/bin/g++"
export CC="\${CMAKE_C_COMPILER}"
export CXX="\${CMAKE_CXX_COMPILER}"
export LD_LIBRARY_PATH="\${DIR}/lib:\${DIR}/lib64"
EOF
chmod +x "${INSTALLDIR}/activate.sh"

# Notes
#
#   --enable-shared --enable-threads=posix --enable-__cxa_atexit: 
#       These parameters are required to build the C++ libraries to published standards.
#   
#   --enable-clocale=gnu: 
#       This parameter is a failsafe for incomplete locale data.
#   
#   --disable-multilib: 
#       This parameter ensures that files are created for the specific
#       architecture of your computer.
#        This will disable building 32-bit support on 64-bit systems where the
#        32 bit version of libc is not installed and you do not want to go
#        through the trouble of building it. Diagnosis: "Compiler build fails
#        with fatal error: gnu/stubs-32.h: No such file or directory"
#   
#   --with-system-zlib: 
#       Uses the system zlib instead of the bundled one. zlib is used for
#       compressing and uncompressing GCC's intermediate language in LTO (Link
#       Time Optimization) object files.
#   
#   --enable-languages=all
#   --enable-languages=c,c++,fortran,go,objc,obj-c++: 
#       This command identifies which languages to build. You may modify this
#       command to remove undesired language
#
#   --enable-gold[=ARG]
#       build gold [ARG={default,yes,no}]
#   --enable-ld[=ARG]
#       build ld [ARG={default,yes,no}]
#   --enable-lto
#       enable link time optimization support