[doit] make sure -O2 is set and gmake is used on mac and freebsd
[toolchains.git] / doit
blob372a367ce33edd6d7ee89da6286cdb37726e0a5d
1 #!/usr/bin/env bash
3 OS=`uname`
4 HOSTARCH=`uname -m`
5 PARALLEL=
6 FETCH=0
7 QUIET=0
8 STRIP=0
9 GNU_MIRROR=https://mirrors.kernel.org/gnu
10 # Get absolute path, will spawn a subshell then exit so our pwd is retained
11 SCRIPTROOT=$(cd "$(dirname $0)" && pwd)
12 PATCHES=$SCRIPTROOT/patches/
14 # Cause errors in pipes to return failure when necessary
15 set -o pipefail
17 function err() {
18 echo "doit: error during build"
19 if [ "$QUIET" = "1" ]; then
20 echo "doit: dumping last 50 lines of build log..."
21 echo "doit: see $OUTDIR/build.log for the full details"
22 tail -50 $OUTDIR/build.log
24 exit 1
27 trap err ERR
29 function help()
31 echo "Options"
32 echo " -a <arch list> architectures to build"
33 echo " example: -a 'arm' or -a 'arm i386 x86_64' for multiple"
34 echo " -c use compilation cache (ccache must be installed)"
35 echo " -f fetch source releases from upstream"
36 echo " -h|-? display this help message"
37 echo " -j<#> use <#> parallel workers to build"
38 echo " -o <dir> output directory"
39 echo " -q make the build quieter"
40 echo " -s strip the binaries"
41 exit 1
44 function log()
46 if [ "$QUIET" = "1" ]; then
47 "$@" >> $OUTDIR/build.log 2>&1
48 else
49 "$@" 2>&1 | tee -a $OUTDIR/build.log
53 function extract-tool()
55 #echo "extract-tool " $1 $2 $3 $4
57 TARFILE=${1}-${2}.tar$3
58 TARGETDIR=${1}-${2}
59 HASH="$4"
60 PATCH="$5"
61 if [ -f ${TARGETDIR}/.extracted ]; then
62 log echo "$TARFILE already extracted into $TARGETDIR, skipping"
63 return 0
65 if [ ! -f $ARCHIVES/$TARFILE ]; then
66 log echo "error, missing $TARFILE"
67 exit 1
70 echo "checking $TARFILE integrity"
71 if [ "$(shasum -a 256 -b "$ARCHIVES/$TARFILE" | cut -f1 -d' ')" != "$HASH" ]; then
72 log echo "$TARFILE failed integrity check"
73 exit 1
76 echo extracting $TARFILE
77 pushd $OUTDIR
78 rm -rf $TARGETDIR
79 tar xf $ARCHIVES/$TARFILE || exit 1
81 if [ -n "$PATCH" ]; then
82 log echo patching $1
83 log patch -d $TARGETDIR -p1 < "$PATCH" || exit 1
86 touch $TARGETDIR/.extracted || exit 1
87 popd
90 MAKE=make
91 if [ "$OS" = "Linux" ]; then
92 COUNT=`grep processor /proc/cpuinfo | wc -l`
93 PARALLEL=-j`expr $COUNT + $COUNT`
95 if [ "$OS" = "Darwin" ]; then
96 PARALLEL=-j`sysctl -n hw.ncpu`
97 export CXXFLAGS="-fbracket-depth=1024 -O2"
99 if [ "$OS" = "FreeBSD" ]; then
100 PARALLEL=-j`sysctl -n hw.ncpu`
101 export CXXFLAGS="-fbracket-depth=1024 -O2"
102 MAKE=gmake
105 if [ "$HOSTARCH" = "amd64" ]; then
106 HOSTARCH=x86_64
109 if [ $# == "0" ]; then
110 help
113 while getopts a:cfhj:o:qs? arg
115 case $arg in
116 a ) ARCHES=$OPTARG ;;
117 c ) CCACHE=1 ;;
118 j ) PARALLEL="-j$OPTARG" ;;
119 f ) FETCH=1 ;;
120 o ) OUTDIR=$OPTARG ;;
121 q ) QUIET=1 ;;
122 s ) STRIP=1 ;;
123 h ) help ;;
124 ? ) help ;;
125 * ) echo "unrecognized option '$arg'" ; exit 1 ;;
126 esac
127 done
130 if [ -z "$ARCHES" ]; then
131 echo need to specify architectures to build
132 echo ie -a "arm sh"
133 exit 1
136 if [ -z "$OUTDIR" ]; then
137 OUTDIR=`pwd`
139 ARCHIVES=$OUTDIR/archives
141 if [ -z $(which makeinfo) ]; then
142 echo makeinfo not found. On debian/ubuntu this is provided by the texinfo package.
143 exit 1
146 export CC="cc"
147 export CXX="c++"
149 if [ "$CCACHE" = "1" ]; then
150 export CC="ccache $CC"
151 export CXX="ccache $CXX"
154 log date
155 log echo "ARCHES='$ARCHES' PARALLEL='$PARALLEL' FETCH='$FETCH' CCACHE='$CCACHE'"
156 # load GCCVER and BINVER
157 . toolvers
159 if [ "$FETCH" = "1" ]; then
160 if [ ! -f $ARCHIVES/binutils-$BINVER.tar.bz2 ]; then
161 echo fetching binutils-$BINVER
162 log wget -P $ARCHIVES -N $GNU_MIRROR/binutils/binutils-$BINVER.tar.bz2
164 if [ ! -f $ARCHIVES/gcc-$GCCVER.tar.bz2 ]; then
165 echo fetching gcc-$GCCVER
166 log wget -P $ARCHIVES -N $GNU_MIRROR/gcc/gcc-$GCCVER/gcc-$GCCVER.tar.xz
168 if [ ! -f $ARCHIVES/gdb-$GDBVER.tar.xz ]; then
169 echo fetching gdb-$GDBVER
170 log wget -P $ARCHIVES -N $GNU_MIRROR/gdb/gdb-$GDBVER.tar.xz
172 if [ ! -f $ARCHIVES/mpfr-$MPFRVER.tar.bz2 ]; then
173 echo fetching mpfr-$MPFRVER
174 log wget -P $ARCHIVES -N $GNU_MIRROR/mpfr/mpfr-$MPFRVER.tar.bz2
176 if [ ! -f $ARCHIVES/mpc-$MPCVER.tar.gz ]; then
177 echo fetching mpc-$MPCVER
178 log wget -P $ARCHIVES -N $GNU_MIRROR/mpc/mpc-$MPCVER.tar.gz
180 if [ ! -f $ARCHIVES/gmp-$GMPVER.tar.bz2 ]; then
181 echo fetching gmp-$GMPVER
182 log wget -P $ARCHIVES -N $GNU_MIRROR/gmp/gmp-$GMPVER.tar.bz2
186 if [ ! -f $OUTDIR/.extracted-stamp ]; then
187 extract-tool binutils $BINVER .bz2 $BINHASH
188 extract-tool gcc $GCCVER .xz $GCCHASH $PATCHES/gcc-patch.txt
189 extract-tool gdb $GDBVER .xz $GDBHASH $PATCHES/gdb-patch.txt
190 extract-tool gmp $GMPVER .bz2 $GMPHASH
191 extract-tool mpc $MPCVER .gz $MPCHASH
192 extract-tool mpfr $MPFRVER .bz2 $MPFRHASH
193 touch $OUTDIR/.extracted-stamp
196 # link the last three libs into gcc
197 pushd $OUTDIR/gcc-$GCCVER
198 ln -sf ../gmp-$GMPVER gmp
199 ln -sf ../mpc-$MPCVER mpc
200 ln -sf ../mpfr-$MPFRVER mpfr
201 popd
203 for ARCH in $ARCHES; do
204 echo building for arch \"$ARCH\"
205 if [ "$ARCH" == "arm" ]; then
206 TARGET=arm-eabi
207 else
208 TARGET=$ARCH-elf
211 INSTALLPATH=$OUTDIR/$TARGET-$GCCVER-$OS-$HOSTARCH
212 BINBUILDPATH=$OUTDIR/build-binutils-$BINVER-$ARCH-$OS-$HOSTARCH
213 GCCBUILDPATH=$OUTDIR/build-gcc-$GCCVER-$ARCH-$OS-$HOSTARCH
214 GDBBUILDPATH=$OUTDIR/build-gdb-$GDBVER-$ARCH-$OS-$HOSTARCH
215 export PATH=$INSTALLPATH/bin:$PATH
217 # Building Binutils
218 if [ ! -f $BINBUILDPATH/built.txt ]; then
219 echo building binutils
220 mkdir -p $BINBUILDPATH
221 pushd $BINBUILDPATH
222 log ../binutils-$BINVER/configure --target=$TARGET --prefix=$INSTALLPATH --disable-werror
223 log $MAKE $PARALLEL
224 log $MAKE install
225 touch built.txt
226 popd
229 # Building GCC
230 if [ ! -f $GCCBUILDPATH/built.txt ]; then
231 echo building gcc
232 ARCH_OPTIONS=
233 if [ $ARCH == "arm" ]; then
234 ARCH_OPTIONS="--with-cpu=arm926ej-s --with-fpu=vfp"
237 mkdir -p $GCCBUILDPATH
238 pushd $GCCBUILDPATH
239 log ../gcc-$GCCVER/configure --target=$TARGET --prefix=$INSTALLPATH --enable-languages=c,c++ $ARCH_OPTIONS --disable-werror
240 log $MAKE all-gcc $PARALLEL
241 log $MAKE all-target-libgcc $PARALLEL
242 log $MAKE install-gcc
243 log $MAKE install-target-libgcc
244 touch built.txt
245 popd
248 if [ ! -f $GDBBUILDPATH/built.txt ]; then
249 case "$TARGET" in
250 aarch64-elf) EXTRA_TARGETS="--enable-targets=arm-eabi" ;;
251 *) EXTRA_TARGETS="" ;;
252 esac
254 echo building gdb
255 mkdir -p $GDBBUILDPATH
256 pushd $GDBBUILDPATH
257 log ../gdb-$GDBVER/configure --target=$TARGET --prefix=$INSTALLPATH --disable-werror $EXTRA_TARGETS
258 log $MAKE $PARALLEL
259 log $MAKE install
260 touch built.txt
261 popd
264 # Optionally strip the binaries
265 if [ "${STRIP}" = "1" ]; then
266 find "${INSTALLPATH}/bin" -type f -exec strip {} \;
267 for filename in $(find "${INSTALLPATH}/libexec" -type f); do
268 (file "${filename}" | grep -q ELF) && strip "${filename}"
269 done
271 done
273 echo all done