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
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
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"
48 if [ "$QUIET" = "1" ]; then
49 "$@" >> $OUTDIR/build.log
2>&1
51 "$@" 2>&1 |
tee -a $OUTDIR/build.log
55 function download-archive
()
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"
68 log
echo "$filename already downloaded, skipping"
74 [!/]*) echo "$PWD/$1" ;;
79 function extract-tool
()
81 #echo "extract-tool " $1 $2 $3 $4
83 TARFILE
=${1}-${2}.
tar$3
87 if [ -f ${TARGETDIR}/.extracted
]; then
88 log
echo "$TARFILE already extracted into $TARGETDIR, skipping"
91 if [ ! -f $ARCHIVES/$TARFILE ]; then
92 log
echo "error, missing $TARFILE"
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"
102 echo extracting
$TARFILE
105 tar xf
$ARCHIVES/$TARFILE ||
exit 1
107 if [ -n "$PATCH" ]; then
109 log
patch -d $TARGETDIR -p1 < "$PATCH" ||
exit 1
112 touch $TARGETDIR/.extracted ||
exit 1
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"
131 if [ "$HOSTARCH" = "amd64" ]; then
135 if [ $# == "0" ]; then
139 while getopts a
:cfghj
:o
:qs? arg
142 a
) ARCHES
=$OPTARG ;;
146 j
) PARALLEL
="-j$OPTARG" ;;
147 o
) OUTDIR
="$OPTARG" ;;
152 * ) echo "unrecognized option '$arg'" ; exit 1 ;;
157 if [ -z "$ARCHES" ]; then
158 echo need to specify architectures to build
163 if [ -z "$OUTDIR" ]; then
166 if ! mkdir
-p -- "$OUTDIR"; then
167 log
echo Unable to create output directory
"$OUTDIR"
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.
182 if [ "$CCACHE" = "1" ]; then
183 export CC
="ccache $CC"
184 export CXX
="ccache $CXX"
188 log
echo "ARCHES='$ARCHES' PARALLEL='$PARALLEL' FETCH='$FETCH' CCACHE='$CCACHE'"
189 # load GCCVER and BINVER
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
218 for ARCH
in $ARCHES; do
219 echo building
for arch
\"$ARCH\"
221 arm
) TARGET
=arm-eabi
;;
222 pdp11
) TARGET
=pdp11-aout
;;
223 *) TARGET
=$ARCH-elf;;
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
233 if [ ! -f $BINBUILDPATH/built.txt
]; then
234 echo building binutils
235 mkdir
-p $BINBUILDPATH
237 log ..
/binutils-
$BINVER/configure
--target=$TARGET --prefix=$INSTALLPATH --disable-werror
245 if [ ! -f $GCCBUILDPATH/built.txt
]; then
248 if [ $ARCH == "arm" ]; then
249 ARCH_OPTIONS
="--with-cpu=arm926ej-s --with-fpu=vfp"
252 mkdir
-p $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
264 if [ ! -f $GDBBUILDPATH/built.txt
]; then
266 aarch64-elf
) EXTRA_TARGETS
="--enable-targets=arm-eabi" ;;
267 *) EXTRA_TARGETS
="" ;;
271 mkdir
-p $GDBBUILDPATH
273 log ..
/gdb-
$GDBVER/configure
--target=$TARGET --prefix=$INSTALLPATH --disable-werror $EXTRA_TARGETS
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}"