[version] bump gcc and other projects to 7.4.0
[toolchains.git] / doit
blob27df135e6cf5a57e8a40b1109dd64a1783d0ea4a
1 #!/usr/bin/env bash
3 OS=`uname`
4 HOSTARCH=`uname -m`
5 PARALLEL=
6 FETCH=0
7 QUIET=0
8 STRIP=0
9 GDB=1
10 GNU_MIRROR=https://mirrors.kernel.org/gnu
11 # Get absolute path, will spawn a subshell then exit so our pwd is retained
12 SCRIPTROOT=$(cd "$(dirname $0)" && pwd)
13 PATCHES=$SCRIPTROOT/patches/
15 # Cause errors in pipes to return failure when necessary
16 set -o pipefail
18 function err() {
19 echo "doit: error during build"
20 if [ "$QUIET" = "1" ]; then
21 echo "doit: dumping last 50 lines of build log..."
22 echo "doit: see $OUTDIR/build.log for the full details"
23 tail -50 $OUTDIR/build.log
25 exit 1
28 trap err ERR
30 function help()
32 echo "Options"
33 echo " -a <arch list> architectures to build"
34 echo " example: -a 'arm' or -a 'arm i386 x86_64' for multiple"
35 echo " -c use compilation cache (ccache must be installed)"
36 echo " -f fetch source releases from upstream"
37 echo " -g do not build GDB (not supported on some architectures)"
38 echo " -h|-? display this help message"
39 echo " -j<#> use <#> parallel workers to build"
40 echo " -o <dir> output directory"
41 echo " -q make the build quieter"
42 echo " -s strip the binaries"
43 exit 1
46 function log()
48 if [ "$QUIET" = "1" ]; then
49 "$@" >> $OUTDIR/build.log 2>&1
50 else
51 "$@" 2>&1 | tee -a $OUTDIR/build.log
55 function download-archive()
57 local name="$1"
58 local ver="$2"
59 local ext="$3"
60 local subdir="$4"
62 local filename="$name-$ver.tar.$ext"
64 if [ ! -f "$ARCHIVES/$filename" ]; then
65 echo "fetching $name-$ver"
66 log wget -P "$ARCHIVES" -N "$GNU_MIRROR/$name/$subdir$filename"
67 else
68 log echo "$filename already downloaded, skipping"
72 function full-path {
73 case "$1" in
74 [!/]*) echo "$PWD/$1" ;;
75 *) echo "$1" ;;
76 esac
79 function extract-tool()
81 #echo "extract-tool " $1 $2 $3 $4
83 TARFILE=${1}-${2}.tar$3
84 TARGETDIR=${1}-${2}
85 HASH="$4"
86 PATCH="$5"
87 if [ -f ${TARGETDIR}/.extracted ]; then
88 log echo "$TARFILE already extracted into $TARGETDIR, skipping"
89 return 0
91 if [ ! -f $ARCHIVES/$TARFILE ]; then
92 log echo "error, missing $TARFILE"
93 exit 1
96 echo "checking $TARFILE integrity"
97 if [ "$(shasum -a 256 -b "$ARCHIVES/$TARFILE" | cut -f1 -d' ')" != "$HASH" ]; then
98 log echo "$TARFILE failed integrity check"
99 exit 1
102 echo extracting $TARFILE
103 pushd $OUTDIR
104 rm -rf $TARGETDIR
105 tar xf $ARCHIVES/$TARFILE || exit 1
107 if [ -n "$PATCH" ]; then
108 log echo patching $1
109 log patch -d $TARGETDIR -p1 < "$PATCH" || exit 1
112 touch $TARGETDIR/.extracted || exit 1
113 popd
116 MAKE=make
117 if [ "$OS" = "Linux" ]; then
118 COUNT=`grep processor /proc/cpuinfo | wc -l`
119 PARALLEL=-j`expr $COUNT + $COUNT`
121 if [ "$OS" = "Darwin" ]; then
122 PARALLEL=-j`sysctl -n hw.ncpu`
123 export CXXFLAGS="-fbracket-depth=1024 -O2"
125 if [ "$OS" = "FreeBSD" ]; then
126 PARALLEL=-j`sysctl -n hw.ncpu`
127 export CXXFLAGS="-fbracket-depth=1024 -O2"
128 MAKE=gmake
131 if [ "$HOSTARCH" = "amd64" ]; then
132 HOSTARCH=x86_64
135 if [ $# == "0" ]; then
136 help
139 while getopts a:cfghj:o:qs? arg
141 case $arg in
142 a ) ARCHES=$OPTARG ;;
143 c ) CCACHE=1 ;;
144 f ) FETCH=1 ;;
145 g ) GDB=0 ;;
146 j ) PARALLEL="-j$OPTARG" ;;
147 o ) OUTDIR="$OPTARG" ;;
148 q ) QUIET=1 ;;
149 s ) STRIP=1 ;;
150 h ) help ;;
151 ? ) help ;;
152 * ) echo "unrecognized option '$arg'" ; exit 1 ;;
153 esac
154 done
157 if [ -z "$ARCHES" ]; then
158 echo need to specify architectures to build
159 echo ie -a "arm sh"
160 exit 1
163 if [ -z "$OUTDIR" ]; then
164 OUTDIR=`pwd`
165 else
166 if ! mkdir -p -- "$OUTDIR"; then
167 log echo Unable to create output directory "$OUTDIR"
168 exit 1
170 OUTDIR="$(full-path "$OUTDIR")"
172 ARCHIVES=$OUTDIR/archives
174 if [ -z $(which makeinfo) ]; then
175 echo makeinfo not found. On debian/ubuntu this is provided by the texinfo package.
176 exit 1
179 export CC="cc"
180 export CXX="c++"
182 if [ "$CCACHE" = "1" ]; then
183 export CC="ccache $CC"
184 export CXX="ccache $CXX"
187 log date
188 log echo "ARCHES='$ARCHES' PARALLEL='$PARALLEL' FETCH='$FETCH' CCACHE='$CCACHE'"
189 # load GCCVER and BINVER
190 . toolvers
192 if [ "$FETCH" = "1" ]; then
193 download-archive binutils $BINVER xz
194 download-archive gcc $GCCVER xz "gcc-$GCCVER/"
195 download-archive gdb $GDBVER xz
196 download-archive gmp $GMPVER xz
197 download-archive mpc $MPCVER gz
198 download-archive mpfr $MPFRVER xz
201 if [ ! -f $OUTDIR/.extracted-stamp ]; then
202 extract-tool binutils $BINVER .xz $BINHASH
203 extract-tool gcc $GCCVER .xz $GCCHASH $PATCHES/gcc-patch.txt
204 extract-tool gdb $GDBVER .xz $GDBHASH $PATCHES/gdb-patch.txt
205 extract-tool gmp $GMPVER .xz $GMPHASH
206 extract-tool mpc $MPCVER .gz $MPCHASH
207 extract-tool mpfr $MPFRVER .xz $MPFRHASH
208 touch $OUTDIR/.extracted-stamp
211 # link the last three libs into gcc
212 pushd $OUTDIR/gcc-$GCCVER
213 ln -sf ../gmp-$GMPVER gmp
214 ln -sf ../mpc-$MPCVER mpc
215 ln -sf ../mpfr-$MPFRVER mpfr
216 popd
218 for ARCH in $ARCHES; do
219 echo building for arch \"$ARCH\"
220 case $ARCH in
221 arm) TARGET=arm-eabi;;
222 pdp11) TARGET=pdp11-aout;;
223 *) TARGET=$ARCH-elf;;
224 esac
226 INSTALLPATH=$OUTDIR/$TARGET-$GCCVER-$OS-$HOSTARCH
227 BINBUILDPATH=$OUTDIR/build-binutils-$BINVER-$ARCH-$OS-$HOSTARCH
228 GCCBUILDPATH=$OUTDIR/build-gcc-$GCCVER-$ARCH-$OS-$HOSTARCH
229 GDBBUILDPATH=$OUTDIR/build-gdb-$GDBVER-$ARCH-$OS-$HOSTARCH
230 export PATH=$INSTALLPATH/bin:$PATH
232 # Building Binutils
233 if [ ! -f $BINBUILDPATH/built.txt ]; then
234 echo building binutils
235 mkdir -p $BINBUILDPATH
236 pushd $BINBUILDPATH
237 log ../binutils-$BINVER/configure --target=$TARGET --prefix=$INSTALLPATH --disable-werror
238 log $MAKE $PARALLEL
239 log $MAKE install
240 touch built.txt
241 popd
244 # Building GCC
245 if [ ! -f $GCCBUILDPATH/built.txt ]; then
246 echo building gcc
247 ARCH_OPTIONS=
248 if [ $ARCH == "arm" ]; then
249 ARCH_OPTIONS="--with-cpu=arm926ej-s --with-fpu=vfp"
252 mkdir -p $GCCBUILDPATH
253 pushd $GCCBUILDPATH
254 log ../gcc-$GCCVER/configure --target=$TARGET --prefix=$INSTALLPATH --enable-languages=c,c++ $ARCH_OPTIONS --disable-werror
255 log $MAKE all-gcc $PARALLEL
256 log $MAKE all-target-libgcc $PARALLEL
257 log $MAKE install-gcc
258 log $MAKE install-target-libgcc
259 touch built.txt
260 popd
263 if (( $GDB )); then
264 if [ ! -f $GDBBUILDPATH/built.txt ]; then
265 case "$TARGET" in
266 aarch64-elf) EXTRA_TARGETS="--enable-targets=arm-eabi" ;;
267 *) EXTRA_TARGETS="" ;;
268 esac
270 echo building gdb
271 mkdir -p $GDBBUILDPATH
272 pushd $GDBBUILDPATH
273 log ../gdb-$GDBVER/configure --target=$TARGET --prefix=$INSTALLPATH --disable-werror $EXTRA_TARGETS
274 log $MAKE $PARALLEL
275 log $MAKE install
276 touch built.txt
277 popd
281 # Optionally strip the binaries
282 if [ "${STRIP}" = "1" ]; then
283 find "${INSTALLPATH}/bin" -type f -exec strip {} \;
284 for filename in $(find "${INSTALLPATH}/libexec" -type f); do
285 (file "${filename}" | grep -q ELF) && strip "${filename}"
286 done
288 done
290 echo all done