allow specifying the make command
[summon-arm-toolchain.git] / summon-arm-toolchain
1 #!/bin/sh
2 # Written by Uwe Hermann <uwe@hermann-uwe.de>, released as public domain.
3 # Modified by Piotr Esden-Tempski <piotr@esden.net>, released as public domain.
4
5 # adapted for r0ket <http://r0ket.badge.events.ccc.de> development by
6 # Matthias Rampke <matthias@rampke.de>
7 # requires Homebrew <https://mxcl.github.com/homebrew>
8
9 # Stop if any command fails
10 set -e
11
12 ##############################################################################
13 # Settings section
14 # You probably want to customize those
15 ##############################################################################
16 TARGET=arm-none-eabi            # Or: TARGET=arm-elf
17 [ -z $PREFIX ] && PREFIX=${HOME}/arm    # Install location of your final toolchain
18 [ -z $WORKDIR ] && WORKDIR=$PREFIX/work     #temporary directory
19 PARALLEL=                       # Or: PARALLEL="-j 5" for 4 CPUs
20 # Set to 'sudo' if you need superuser privileges while installing
21 SUDO=''
22 # Set to 1 to be quieter while running
23 QUIET=0
24 # Set to 1 to use linaro gcc instead of the FSF gcc
25 USE_LINARO=0
26 # Set to 1 to enable C++
27 USE_CPP=0
28 # Make the gcc default to Cortex-M3
29 DEFAULT_TO_CORTEX_M3=1
30
31 ##############################################################################
32 # Version and download url settings section
33 ##############################################################################
34 if [ ${USE_LINARO} = 0 ] ; then
35         # For FSF GCC:
36         GCCVERSION=4.6.1
37         if [ ${USE_CPP} = 0 ]; then
38             GCC=gcc-core-${GCCVERSION}
39             GCCDIR=gcc-${GCCVERSION}
40         else
41             GCC=gcc-${GCCVERSION}
42             GCCDIR=${GCC}
43         fi
44         GCCURL=http://ftp.gnu.org/gnu/gcc/${GCCDIR}/${GCC}.tar.bz2
45 else
46         # For the Linaro GCC:
47         GCCRELEASE=4.6-2011.07
48         GCCVERSION=4.6-2011.07-0
49         GCC=gcc-linaro-${GCCRELEASE}
50         GCCDIR=gcc-linaro-${GCCVERSION}
51         GCCURL=http://launchpad.net/gcc-linaro/4.6/${GCCRELEASE}/+download/gcc-linaro-${GCCRELEASE}.tar.bz2
52 fi
53
54 BINUTILS=binutils-2.21
55 NEWLIB=newlib-1.19.0
56 GDB=gdb-7.3
57 GMP=gmp-5.0.2
58 MPFR=mpfr-3.0.1
59 MPC=mpc-0.9
60
61 SHA1SUMS="ef93235588eb443e4c4a77f229a8d131bccaecc6  binutils-2.21.tar.bz2
62 8bf66f7a71534ba564563a0ba0b2517aead8ac30  gcc-4.6.1.tar.bz2
63 9b766705f051ffb7321de58f247688b0ae661b98  gcc-core-4.6.1.tar.bz2
64 828d99a63e0578879150f5681d234763cff69313  gcc-linaro-4.6-2011.07.tar.bz2
65 fefd90275d6decdca00c5c29c533e2ef3d439664  gdb-7.3.tar.bz2
66 2968220e1988eabb61f921d11e5d2db5431e0a35  gmp-5.0.2.tar.bz2
67 229722d553030734d49731844abfef7617b64f1a  mpc-0.9.tar.gz
68 fbf402fc196724ae60ef01eb6ca8490b1ea4db69  mpfr-3.0.1.tar.bz2
69 b2269d30ce7b93b7c714b90ef2f40221c2df0fcd  newlib-1.19.0.tar.gz"
70
71 ##############################################################################
72 # Flags section
73 ##############################################################################
74
75 if which getconf > /dev/null 2> /dev/null && getconf _NPROCESSORS_ONLN > /dev/null 2> /dev/null; then
76         CPUS=$(getconf _NPROCESSORS_ONLN)
77 elif which sysctl > /dev/null 2> /dev/null && sysctl hw.ncpu 2> /dev/null > /dev/null; then
78   CPUS=$(sysctl hw.ncpu | sed -E -e 's/^hw\.ncpu(: | = )//')
79 else
80         CPUS=1
81 fi
82 PARALLEL=-j$((CPUS + 1))
83 echo "${CPUS} cpu's detected running make with '${PARALLEL}' flag"
84
85 GDBFLAGS=
86 BINUTILFLAGS=
87
88 if [ ${DEFAULT_TO_CORTEX_M3} = 0 ] ; then
89         GCCFLAGS=
90 else
91         # To default to the Cortex-M3:
92         GCCFLAGS="--with-arch=armv7-m --with-mode=thumb --with-float=soft"
93 fi
94
95 # Pull in the local configuration, if any
96 if [ -f local.sh ]; then
97     . ./local.sh
98 fi
99
100 MAKEFLAGS=${PARALLEL} ${MAKESILENT}
101 TARFLAGS=v
102 MAKESILENT=
103
104 if [ ${QUIET} != 0 ]; then
105     TARFLAGS=
106     MAKESILENT="-s"
107 fi
108
109 export PATH="${PREFIX}/bin:${PATH}"
110
111 SUMMON_DIR=${WORKDIR}
112 SOURCES=${SUMMON_DIR}/sources
113 STAMPS=${SUMMON_DIR}/stamps
114
115
116 ##############################################################################
117 # OS and Tooldetection section
118 # Detects which tools and flags to use
119 ##############################################################################
120
121 # normalized fetching with whatever we can find
122 if which fetch > /dev/null; then
123     FETCHCMD="fetch -p"
124 elif which wget > /dev/null; then
125     FETCHCMD="wget -c"
126 elif which curl > /dev/null; then
127     FETCHCMD="curl -C - -O -L"
128 else
129     FETCHCMD=ftp    # the only HTTP client on some BSD
130 fi
131
132 if [ USE_LINARO = 1 ]; then
133     if which gnutar > /dev/null; then
134         TAR=gnutar
135     elif which gtar > /dev/null; then
136         TAR=gtar
137     else    # hopefully it understands the GNU format
138         TAR=tar
139     fi
140 else
141     TAR=tar     # use the system tar
142 fi
143
144 if [ -z MAKE ]; then
145   MAKE=make
146   export make # make it known to the configure scripts
147 fi
148
149 ##############################################################################
150 # Building section
151 # You probably don't have to touch anything after this
152 ##############################################################################
153
154 # Fetch a versioned file from a URL
155 dofetch() {
156     if [ ! -e ${STAMPS}/$1.fetch ]; then
157         log "Downloading $1 sources..."
158         ${FETCHCMD} $2
159         touch ${STAMPS}/$1.fetch
160     fi
161 }
162
163 # Log a message out to the console
164 log() {
165     echo "******************************************************************"
166     echo "* $*"
167     echo "******************************************************************"
168 }
169
170 # Unpack an archive
171 unpack() {
172     log Unpacking $*
173     # Use 'auto' mode decompression.  Replace with a switch if tar doesn't support -a
174     ARCHIVE=$(ls ${SOURCES}/$1.tar.*)
175     case ${ARCHIVE} in
176         *.bz2)
177             echo "archive type bz2"
178             TYPE=j
179             ;;
180         *.gz)
181             echo "archive type gz"
182             TYPE=z
183             ;;
184         *)
185             echo "Unknown archive type of $1"
186             echo "leaving the decision to tar"
187             TYPE=
188             ;;
189     esac
190     ${TAR} xf${TYPE}${TARFLAGS} ${SOURCES}/$1.tar.*
191 }
192
193 # Install a build
194 doinstall() {
195     log $1
196     ${SUDO} ${MAKE} ${MAKESILENT} $2 $3 $4 $5 $6 $7 $8
197 }
198
199 if [ ! -d $SUMMON_DIR ]; then
200     mkdir -p ${SUMMON_DIR}
201     SUMMON_DIR_CREATED=1
202 else
203     SUMMON_DIR_CREATED=0
204 fi
205 mkdir -p ${STAMPS} ${SOURCES}
206
207 cd ${SOURCES}
208
209 dofetch ${BINUTILS} http://ftp.gnu.org/gnu/binutils/${BINUTILS}.tar.bz2
210 dofetch ${GCC} ${GCCURL}
211 dofetch ${NEWLIB} ftp://sources.redhat.com/pub/newlib/${NEWLIB}.tar.gz
212 dofetch ${GDB} http://ftp.gnu.org/gnu/gdb/${GDB}.tar.bz2
213 dofetch ${GMP} ftp://ftp.gmplib.org/pub/${GMP}/${GMP}.tar.bz2
214 dofetch ${MPFR} http://www.mpfr.org/${MPFR}/${MPFR}.tar.bz2
215 dofetch ${MPC} http://www.multiprecision.org/mpc/download/${MPC}.tar.gz
216
217 if which sha1sum > /dev/null; then
218     for f in ${BINUTILS} ${GCC} ${NEWLIB} ${GDB} ${GMP} ${MPFR} ${MPC}; do
219         ( echo "$SHA1SUMS" | grep -F $f | sha1sum -c - ) || ( log 'ERROR: Checksum fail!'; exit 1 )
220     done
221 else
222     log "WARING: skipping checksum checks due to missing sha1sum"
223 fi
224
225 cd ${SUMMON_DIR}
226
227 if [ ! -e build ]; then
228     mkdir build
229 fi
230
231 if [ ! -e ${STAMPS}/${BINUTILS}.build ]; then
232     unpack ${BINUTILS}
233     cd build
234     log "Configuring ${BINUTILS}"
235     ../${BINUTILS}/configure --target=${TARGET} \
236                            --prefix=${PREFIX} \
237                            --enable-interwork \
238                            --enable-multilib \
239                            --with-gnu-as \
240                            --with-gnu-ld \
241                            --disable-nls \
242                            --disable-werror \
243                            ${BINUTILFLAGS}
244     log "Building ${BINUTILS}"
245     ${MAKE} ${MAKEFLAGS}
246     doinstall ${BINUTILS} install
247     cd ..
248     log "Cleaning up ${BINUTILS}"
249     touch ${STAMPS}/${BINUTILS}.build
250     rm -rf build/* ${BINUTILS}
251 fi
252
253 if [ ! -e ${STAMPS}/${GCC}-boot.build ]; then
254     unpack ${GCC} boot
255     unpack ${GMP}
256     mv ${GMP} ${GCCDIR}/gmp
257     unpack ${MPFR}
258     mv ${MPFR} ${GCCDIR}/mpfr
259     unpack ${MPC}
260     mv ${MPC} ${GCCDIR}/mpc
261     cd build
262     log "Configuring ${GCC}-boot"
263     ../${GCCDIR}/configure --target=${TARGET} \
264                       --prefix=${PREFIX} \
265                       --enable-interwork \
266                       --enable-multilib \
267                       --enable-languages="c" \
268                       --with-newlib \
269                       --without-headers \
270                       --disable-shared \
271                       --with-gnu-as \
272                       --with-gnu-ld \
273                       --disable-nls \
274                       --disable-werror \
275                       --with-system-zlib \
276                       ${GCCFLAGS}
277     log "Building ${GCC}-boot"
278     ${MAKE} ${MAKEFLAGS} all-gcc
279     doinstall ${GCC}-boot install-gcc
280     cd ..
281     log "Cleaning up ${GCC}-boot"
282     touch ${STAMPS}/${GCC}-boot.build
283     rm -rf build/* ${GCCDIR}
284 fi
285
286 if [ ! -e ${STAMPS}/${NEWLIB}.build ]; then
287     unpack ${NEWLIB}
288     cd build
289     log "Configuring ${NEWLIB}"
290     # configuration options and flags partially
291     # from https://github.com/jsnyder/arm-eabi-toolchain
292     ../${NEWLIB}/configure --target=${TARGET} \
293                          --prefix=${PREFIX} \
294                          --enable-interwork \
295                          --enable-multilib \
296                          --with-gnu-as \
297                          --with-gnu-ld \
298                          --disable-nls \
299                          --disable-werror \
300                          --disable-newlib-supplied-syscalls \
301                          --disable-shared \
302                          --disable-nls \
303                          --disable-libgloss \
304                          --with-float=soft
305     log "Building ${NEWLIB}"
306     NEWLIB_FLAGS="-ffunction-sections -fdata-sections -DPREFER_SIZE_OVER_SPEED -D__OPTIMIZE_SIZE__ -Os -fomit-frame-pointer -fno-unroll-loops -D__BUFSIZ__=256 -mabi=aapcs"
307     ${MAKE} ${MAKEFLAGS} CFLAGS_FOR_TARGET="${NEWLIB_FLAGS}" CCASFLAGS="${NEWLIB_FLAGS}"
308     doinstall ${NEWLIB} install
309     cd ..
310     log "Cleaning up ${NEWLIB}"
311     touch ${STAMPS}/${NEWLIB}.build
312     rm -rf build/* ${NEWLIB}
313 fi
314
315 # Yes, you need to build gcc again!
316 if [ ! -e ${STAMPS}/${GCC}.build ]; then
317     unpack ${GCC}
318     unpack ${GMP}
319     mv ${GMP} ${GCCDIR}/gmp
320     unpack ${MPFR}
321     mv ${MPFR} ${GCCDIR}/mpfr
322     unpack ${MPC}
323     mv ${MPC} ${GCCDIR}/mpc
324     cd build
325     log "Configuring ${GCC}"
326     [ ${USE_CPP} = 1 ] && GCCFLAGS="--enable-languages='c,c++' ${GCCFLAGS}"
327     ../${GCCDIR}/configure --target=${TARGET} \
328                       --prefix=${PREFIX} \
329                       --enable-interwork \
330                       --enable-multilib \
331                       --with-newlib \
332                       --disable-shared \
333                       --with-gnu-as \
334                       --with-gnu-ld \
335                       --disable-nls \
336                       --disable-werror \
337                       --with-system-zlib \
338                      ${GCCFLAGS}
339     log "Building ${GCC}"
340     ${MAKE} ${MAKEFLAGS}
341     doinstall ${GCC} install
342     cd ..
343     log "Cleaning up ${GCC}"
344     touch ${STAMPS}/${GCC}.build
345     rm -rf build/* ${GCCDIR}
346 fi
347
348 if [ ! -e ${STAMPS}/${GDB}.build ]; then
349     unpack ${GDB}
350     cd build
351     log "Configuring ${GDB}"
352     ../${GDB}/configure --target=${TARGET} \
353                       --prefix=${PREFIX} \
354                       --enable-interwork \
355                       --enable-multilib \
356                       --disable-werror \
357                       ${GDBFLAGS}
358     log "Building ${GDB}"
359     ${MAKE} ${MAKEFLAGS}
360     doinstall ${GDB} install
361     cd ..
362     log "Cleaning up ${GDB}"
363     touch ${STAMPS}/${GDB}.build
364     rm -rf build/* ${GDB}
365 fi
366
367 if [ ${SUMMON_DIR_CREATED} = 1 ]; then
368     log "Removing work directory"
369     rm -rf ${SUMMON_DIR}
370 fi