test
[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.1
55 NEWLIB=newlib-1.19.0
56 GDB=gdb-7.3.1
57 GMP=gmp-5.0.2
58 MPFR=mpfr-3.0.1
59 MPC=mpc-0.9
60
61 SHA1SUMS="525255ca6874b872540c9967a1d26acfbc7c8230  binutils-2.21.1.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 e57f2c7f93536ef54fab728eb733bf2c36550718  gdb-7.3.1.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     if which gnumake > /dev/null; then
146         export MAKE=gnumake
147     elif which gmake > /dev/null; then
148         export MAKE=gmake
149     else
150         export MAKE=make    # should be GNU make
151     fi
152 fi
153
154 ##############################################################################
155 # Building section
156 # You probably don't have to touch anything after this
157 ##############################################################################
158
159 # Fetch a versioned file from a URL
160 dofetch() {
161     if [ ! -e ${STAMPS}/$1.fetch ]; then
162         log "Downloading $1 sources..."
163         ${FETCHCMD} $2
164         touch ${STAMPS}/$1.fetch
165     fi
166 }
167
168 # Log a message out to the console
169 log() {
170     echo "******************************************************************"
171     echo "* $*"
172     echo "******************************************************************"
173 }
174
175 # Unpack an archive
176 unpack() {
177     log Unpacking $*
178     # Use 'auto' mode decompression.  Replace with a switch if tar doesn't support -a
179     ARCHIVE=$(ls ${SOURCES}/$1.tar.*)
180     case ${ARCHIVE} in
181         *.bz2)
182             echo "archive type bz2"
183             TYPE=j
184             ;;
185         *.gz)
186             echo "archive type gz"
187             TYPE=z
188             ;;
189         *)
190             echo "Unknown archive type of $1"
191             echo "leaving the decision to tar"
192             TYPE=
193             ;;
194     esac
195     ${TAR} xf${TYPE}${TARFLAGS} ${SOURCES}/$1.tar.*
196 }
197
198 # Install a build
199 doinstall() {
200     log $1
201     ${SUDO} ${MAKE} ${MAKESILENT} $2 $3 $4 $5 $6 $7 $8
202 }
203
204 if [ ! -d $SUMMON_DIR ]; then
205     mkdir -p ${SUMMON_DIR}
206     SUMMON_DIR_CREATED=1
207 else
208     SUMMON_DIR_CREATED=0
209 fi
210 mkdir -p ${STAMPS} ${SOURCES}
211
212 cd ${SOURCES}
213
214 dofetch ${BINUTILS} http://ftp.gnu.org/gnu/binutils/${BINUTILS}.tar.bz2
215 dofetch ${GCC} ${GCCURL}
216 dofetch ${NEWLIB} ftp://sources.redhat.com/pub/newlib/${NEWLIB}.tar.gz
217 dofetch ${GDB} http://ftp.gnu.org/gnu/gdb/${GDB}.tar.bz2
218 dofetch ${GMP} ftp://ftp.gmplib.org/pub/${GMP}/${GMP}.tar.bz2
219 dofetch ${MPFR} http://www.mpfr.org/${MPFR}/${MPFR}.tar.bz2
220 dofetch ${MPC} http://www.multiprecision.org/mpc/download/${MPC}.tar.gz
221
222 if which sha1sum > /dev/null; then
223     for f in ${BINUTILS} ${GCC} ${NEWLIB} ${GDB} ${GMP} ${MPFR} ${MPC}; do
224         ( echo "$SHA1SUMS" | grep -F $f | sha1sum -c - ) || ( log 'ERROR: Checksum fail!'; exit 1 )
225     done
226 else
227     log "WARING: skipping checksum checks due to missing sha1sum"
228 fi
229
230 cd ${SUMMON_DIR}
231
232 if [ ! -e build ]; then
233     mkdir build
234 fi
235
236 if [ ! -e ${STAMPS}/${BINUTILS}.build ]; then
237     unpack ${BINUTILS}
238     cd build
239     log "Configuring ${BINUTILS}"
240     ../${BINUTILS}/configure --target=${TARGET} \
241                            --prefix=${PREFIX} \
242                            --enable-interwork \
243                            --enable-multilib \
244                            --with-gnu-as \
245                            --with-gnu-ld \
246                            --disable-nls \
247                            --disable-werror \
248                            ${BINUTILFLAGS}
249     log "Building ${BINUTILS}"
250     ${MAKE} ${MAKEFLAGS}
251     doinstall ${BINUTILS} install
252     cd ..
253     log "Cleaning up ${BINUTILS}"
254     touch ${STAMPS}/${BINUTILS}.build
255     rm -rf build/* ${BINUTILS}
256 fi
257
258 if [ ! -e ${STAMPS}/${GCC}-boot.build ]; then
259     unpack ${GCC} boot
260     unpack ${GMP}
261     mv ${GMP} ${GCCDIR}/gmp
262     unpack ${MPFR}
263     mv ${MPFR} ${GCCDIR}/mpfr
264     unpack ${MPC}
265     mv ${MPC} ${GCCDIR}/mpc
266     cd build
267     log "Configuring ${GCC}-boot"
268     ../${GCCDIR}/configure --target=${TARGET} \
269                       --prefix=${PREFIX} \
270                       --enable-interwork \
271                       --enable-multilib \
272                       --enable-languages="c" \
273                       --with-newlib \
274                       --without-headers \
275                       --disable-shared \
276                       --with-gnu-as \
277                       --with-gnu-ld \
278                       --disable-nls \
279                       --disable-werror \
280                       --with-system-zlib \
281                       ${GCCFLAGS}
282     log "Building ${GCC}-boot"
283     ${MAKE} ${MAKEFLAGS} all-gcc
284     doinstall ${GCC}-boot install-gcc
285     cd ..
286     log "Cleaning up ${GCC}-boot"
287     touch ${STAMPS}/${GCC}-boot.build
288     rm -rf build/* ${GCCDIR}
289 fi
290
291 if [ ! -e ${STAMPS}/${NEWLIB}.build ]; then
292     unpack ${NEWLIB}
293     cd build
294     log "Configuring ${NEWLIB}"
295     # configuration options and flags partially
296     # from https://github.com/jsnyder/arm-eabi-toolchain
297     ../${NEWLIB}/configure --target=${TARGET} \
298                          --prefix=${PREFIX} \
299                          --enable-interwork \
300                          --enable-multilib \
301                          --with-gnu-as \
302                          --with-gnu-ld \
303                          --disable-nls \
304                          --disable-werror \
305                          --disable-newlib-supplied-syscalls \
306                          --disable-shared \
307                          --disable-nls \
308                          --disable-libgloss \
309                          --with-float=soft
310     log "Building ${NEWLIB}"
311     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"
312     ${MAKE} ${MAKEFLAGS} CFLAGS_FOR_TARGET="${NEWLIB_FLAGS}" CCASFLAGS="${NEWLIB_FLAGS}"
313     doinstall ${NEWLIB} install
314     cd ..
315     log "Cleaning up ${NEWLIB}"
316     touch ${STAMPS}/${NEWLIB}.build
317     rm -rf build/* ${NEWLIB}
318 fi
319
320 # Yes, you need to build gcc again!
321 if [ ! -e ${STAMPS}/${GCC}.build ]; then
322     unpack ${GCC}
323     unpack ${GMP}
324     mv ${GMP} ${GCCDIR}/gmp
325     unpack ${MPFR}
326     mv ${MPFR} ${GCCDIR}/mpfr
327     unpack ${MPC}
328     mv ${MPC} ${GCCDIR}/mpc
329     cd build
330     log "Configuring ${GCC}"
331     [ ${USE_CPP} = 1 ] && GCCFLAGS="--enable-languages='c,c++' ${GCCFLAGS}"
332     ../${GCCDIR}/configure --target=${TARGET} \
333                       --prefix=${PREFIX} \
334                       --enable-interwork \
335                       --enable-multilib \
336                       --with-newlib \
337                       --disable-shared \
338                       --with-gnu-as \
339                       --with-gnu-ld \
340                       --disable-nls \
341                       --disable-werror \
342                       --with-system-zlib \
343                      ${GCCFLAGS}
344     log "Building ${GCC}"
345     ${MAKE} ${MAKEFLAGS}
346     doinstall ${GCC} install
347     cd ..
348     log "Cleaning up ${GCC}"
349     touch ${STAMPS}/${GCC}.build
350     rm -rf build/* ${GCCDIR}
351 fi
352
353 if [ ! -e ${STAMPS}/${GDB}.build ]; then
354     unpack ${GDB}
355     cd build
356     log "Configuring ${GDB}"
357     ../${GDB}/configure --target=${TARGET} \
358                       --prefix=${PREFIX} \
359                       --enable-interwork \
360                       --enable-multilib \
361                       --disable-werror \
362                       ${GDBFLAGS}
363     log "Building ${GDB}"
364     ${MAKE} ${MAKEFLAGS}
365     doinstall ${GDB} install
366     cd ..
367     log "Cleaning up ${GDB}"
368     touch ${STAMPS}/${GDB}.build
369     rm -rf build/* ${GDB}
370 fi
371
372 if [ ${SUMMON_DIR_CREATED} = 1 ]; then
373     log "Removing work directory"
374     rm -rf ${SUMMON_DIR}
375 fi