build verbose
[summon-arm-toolchain.git] / summon-arm-toolchain
1 #!/bin/bash
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-eabi         # Or: TARGET=arm-elf
17 PREFIX=${HOME}/arm      # Install location of your final toolchain
18 PARALLEL=                       # Or: PARALLEL="-j 5" for 4 CPUs
19 # Set to 'sudo' if you need superuser privileges while installing
20 SUDO=''
21 # Set to 1 to be quieter while running
22 QUIET=0
23 # Set to 1 to use linaro gcc instead of the FSF gcc
24 USE_LINARO=1
25 # Set to 1 to enable building of OpenOCD
26 OOCD_EN=1
27 # Set to 1 to build libstm32 provided by ST
28 LIBSTM32_EN=0
29 # Set to 1 to build libopenstm32 an open source library for stm32
30 LIBOPENSTM32_EN=1
31 # Make the gcc default to Cortex-M3
32 DEFAULT_TO_CORTEX_M3=1
33
34 ##############################################################################
35 # Version and download url settings section
36 ##############################################################################
37 if [ ${USE_LINARO} == 0 ] ; then
38         # For FSF GCC:
39         GCCVERSION=4.5.1
40         GCC=gcc-${GCCVERSION}
41         GCCDIR=${GCC}
42         GCCURL=http://ftp.gnu.org/gnu/gcc/${GCC}/${GCC}.tar.gz
43 else
44         # For the Linaro GCC:
45         GCCRELEASE=4.6-2011.07
46         GCCVERSION=4.6-2011.07-0
47         GCC=gcc-linaro-${GCCRELEASE}
48         GCCDIR=gcc-linaro-${GCCVERSION}
49         GCCURL=http://launchpad.net/gcc-linaro/4.6/${GCCRELEASE}/+download/gcc-linaro-${GCCRELEASE}.tar.bz2
50 fi
51
52 BINUTILS=binutils-2.21
53 NEWLIB=newlib-1.19.0
54 GDB=gdb-7.3
55 OOCD=master
56 LIBCMSIS=v1.10-2
57 LIBSTM32=v3.0.0-1
58 LIBSTM32USB=v3.0.1-1
59 LIBOPENSTM32=master
60
61 ##############################################################################
62 # Flags section
63 ##############################################################################
64
65 if which getconf > /dev/null; then
66         CPUS=$(getconf _NPROCESSORS_ONLN)
67 else
68         CPUS=1
69 fi
70 PARALLEL=-j$((CPUS + 1))
71 echo "${CPUS} cpu's detected running make with '${PARALLEL}' flag"
72
73 GDBFLAGS=
74 BINUTILFLAGS=
75
76 if [ ${DEFAULT_TO_CORTEX_M3} == 0 ] ; then
77         GCCFLAGS=
78 else
79         # To default to the Cortex-M3:
80         GCCFLAGS="--with-arch=armv7-m --with-mode=thumb --with-float=soft"
81 fi
82
83 # Pull in the local configuration, if any
84 if [ -f local.sh ]; then
85     . ./local.sh
86 fi
87
88 MAKEFLAGS=${PARALLEL}
89 TARFLAGS=v
90
91 if [ ${QUIET} != 0 ]; then
92     TARFLAGS=
93     MAKEFLAGS="${MAKEFLAGS} -s"
94 fi
95
96 export PATH="${PREFIX}/bin:${PATH}"
97
98 SUMMON_DIR=$(pwd)
99 SOURCES=${SUMMON_DIR}/sources
100 STAMPS=${SUMMON_DIR}/stamps
101
102
103 ##############################################################################
104 # Tool section
105 ##############################################################################
106 TAR=tar
107
108 ##############################################################################
109 # OS and Tooldetection section
110 # Detects which tools and flags to use
111 ##############################################################################
112
113 case "$(uname)" in
114         Linux)
115         echo "Found Linux OS."
116         ;;
117         Darwin)
118         echo "Found Darwin OS."
119
120         # darwin dependencies
121         DARWINDEPS="wget mpfr libmpc gmp"
122   DARWINDEPSCOUNT=4
123
124   echo "Installing dependencies ..."
125   if ! which brew > /dev/null; then
126     echo "Homebrew not installed."
127   else
128       brew install ${DARWINDEPS} | (grep -v '^Warning: Formula already installed: ' || true)
129   fi
130
131   GCCFLAGS="${GCCFLAGS} \
132       --with-gmp=$(brew --prefix gmp) \
133       --with-mpfr=$(brew --prefix mpfr) \
134                   --with-mpc=$(brew --prefix mpc)
135   -with-libiconv-prefix=$(brew --prefix libiconv)"
136
137   TAR=gnutar
138
139         OOCD_CFLAGS="-m32 -I/opt/mine/include -I/opt/local/include"
140         OOCD_LDFLAGS="-L/opt/mine/lib -L/opt/local/lib"
141         ;;
142         CYGWIN*)
143         echo "Found CygWin that means Windows most likely."
144         ;;
145         *)
146         echo "Found unknown OS. Aborting!"
147         exit 1
148         ;;
149 esac
150
151 ##############################################################################
152 # Building section
153 # You probably don't have to touch anything after this
154 ##############################################################################
155
156 # Fetch a versioned file from a URL
157 function fetch {
158     if [ ! -e ${STAMPS}/$1.fetch ]; then
159         log "Downloading $1 sources..."
160         wget -c $2
161         touch ${STAMPS}/$1.fetch
162     fi
163 }
164
165 # Log a message out to the console
166 function log {
167     echo "******************************************************************"
168     echo "* $*"
169     echo "******************************************************************"
170 }
171
172 # Unpack an archive
173 function unpack {
174     log Unpacking $*
175     # Use 'auto' mode decompression.  Replace with a switch if tar doesn't support -a
176     ARCHIVE=$(ls ${SOURCES}/$1.tar.*)
177     case ${ARCHIVE} in
178         *.bz2)
179             echo "archive type bz2"
180             TYPE=j
181             ;;
182         *.gz)
183             echo "archive type gz"
184             TYPE=z
185             ;;
186         *)
187             echo "Unknown archive type of $1"
188             echo ${ARCHIVE}
189             exit 1
190             ;;
191     esac
192     ${TAR} xf${TYPE}${TARFLAGS} ${SOURCES}/$1.tar.*
193 }
194
195 # Install a build
196 function install {
197     log $1
198     ${SUDO} make ${MAKEFLAGS} $2 $3 $4 $5 $6 $7 $8
199 }
200
201
202 mkdir -p ${STAMPS} ${SOURCES}
203
204 cd ${SOURCES}
205
206 fetch ${BINUTILS} http://ftp.gnu.org/gnu/binutils/${BINUTILS}.tar.bz2
207 fetch ${GCC} ${GCCURL}
208 fetch ${NEWLIB} ftp://sources.redhat.com/pub/newlib/${NEWLIB}.tar.gz
209 fetch ${GDB} http://ftp.gnu.org/gnu/gdb/${GDB}.tar.bz2
210
211 if [ ${OOCD_EN} != 0 ]; then
212 if [ ! -e openocd-${OOCD}.tar.bz2 ]; then
213         log "Cloning OpenOCD sources..."
214         git clone git://openocd.git.sourceforge.net/gitroot/openocd/openocd openocd-${OOCD}
215         cd openocd-${OOCD}
216         ./bootstrap
217         cd ..
218         tar cfvj openocd-${OOCD}.tar.bz2 openocd-${OOCD}
219         #git archive --format=tar --prefix=openocd-${OOCD}/ ${OOCD} | \
220         #    bzip2 --stdout > ../openocd-${OOCD}.tar.bz2
221         rm -rf openocd-${OOCD}
222 fi
223 fi
224
225 if [ ${LIBSTM32_EN} != 0 ]; then
226 if [ ! -e libcmsis-${LIBCMSIS}.tar.bz2 ]; then
227         log "Cloning libcmsis sources..."
228         git clone git://git.open-bldc.org/libcmsis.git
229         cd libcmsis
230         git archive --format=tar --prefix=libcmsis-${LIBCMSIS}/ ${LIBCMSIS} | \
231             bzip2 --stdout > ../libcmsis-${LIBCMSIS}.tar.bz2
232         cd ..
233         rm -rf libcmsis
234 fi
235
236 if [ ! -e libstm32-${LIBSTM32}.tar.bz2 ]; then
237         log "Cloning libstm32 sources..."
238         git clone git://git.open-bldc.org/libstm32.git
239         cd libstm32
240         git archive --format=tar --prefix=libstm32-${LIBSTM32}/ ${LIBSTM32} | \
241             bzip2 --stdout > ../libstm32-${LIBSTM32}.tar.bz2
242         cd ..
243         rm -rf libstm32
244 fi
245
246 if [ ! -e libstm32usb-${LIBSTM32USB}.tar.bz2 ]; then
247         log "Cloning libstm32usb sources..."
248         git clone git://git.open-bldc.org/libstm32usb.git
249         cd libstm32usb
250         git archive --format=tar --prefix=libstm32usb-${LIBSTM32USB}/ ${LIBSTM32USB} | \
251             bzip2 --stdout > ../libstm32usb-${LIBSTM32USB}.tar.bz2
252         cd ..
253         rm -rf libstm32usb
254 fi
255 fi
256
257 if [ ${LIBOPENSTM32_EN} != 0 ]; then
258 if [ ! -e libopenstm32-${LIBOPENSTM32}.tar.bz2 ]; then
259         log "Cloning libopenstm32 sources..."
260         git clone git://libopenstm32.git.sourceforge.net/gitroot/libopenstm32/libopenstm32
261         cd libopenstm32
262         git archive --format=tar --prefix=libopenstm32-${LIBOPENSTM32}/ ${LIBOPENSTM32} | \
263             bzip2 --stdout > ../libopenstm32-${LIBOPENSTM32}.tar.bz2
264         cd ..
265         rm -rf libopenstm32
266 fi
267 fi
268
269 cd ${SUMMON_DIR}
270
271 if [ ! -e build ]; then
272     mkdir build
273 fi
274
275 if [ ! -e ${STAMPS}/${BINUTILS}.build ]; then
276     unpack ${BINUTILS}
277     cd build
278     log "Configuring ${BINUTILS}"
279     ../${BINUTILS}/configure --target=${TARGET} \
280                            --prefix=${PREFIX} \
281                            --enable-interwork \
282                            --enable-multilib \
283                            --with-gnu-as \
284                            --with-gnu-ld \
285                            --disable-nls \
286                            --disable-werror \
287                            ${BINUTILFLAGS}
288     log "Building ${BINUTILS}"
289     make ${MAKEFLAGS}
290     install ${BINUTILS} install
291     cd ..
292     log "Cleaning up ${BINUTILS}"
293     touch ${STAMPS}/${BINUTILS}.build
294     rm -rf build/* ${BINUTILS}
295 fi
296
297 if [ ! -e ${STAMPS}/${GCC}-boot.build ]; then
298     unpack ${GCC} boot
299     cd build
300     log "Configuring ${GCC}-boot"
301     ../${GCCDIR}/configure --target=${TARGET} \
302                       --prefix=${PREFIX} \
303                       --enable-interwork \
304                       --enable-multilib \
305                       --enable-languages="c" \
306                       --with-newlib \
307                       --without-headers \
308                       --disable-shared \
309                       --with-gnu-as \
310                       --with-gnu-ld \
311                       --disable-nls \
312                       --disable-werror \
313                       --with-system-zlib \
314                       ${GCCFLAGS}
315     log "Building ${GCC}-boot"
316     make ${MAKEFLAGS} all-gcc
317     install ${GCC}-boot install-gcc
318     cd ..
319     log "Cleaning up ${GCC}-boot"
320     touch ${STAMPS}/${GCC}-boot.build
321     rm -rf build/* ${GCCDIR}
322 fi
323
324 if [ ! -e ${STAMPS}/${NEWLIB}.build ]; then
325     unpack ${NEWLIB}
326     cd build
327     log "Configuring ${NEWLIB}"
328     ../${NEWLIB}/configure --target=${TARGET} \
329                          --prefix=${PREFIX} \
330                          --enable-interwork \
331                          --enable-multilib \
332                          --with-gnu-as \
333                          --with-gnu-ld \
334                          --disable-nls \
335                          --disable-werror \
336                          --disable-newlib-supplied-syscalls \
337                          --with-float=soft
338     log "Building ${NEWLIB}"
339     make ${MAKEFLAGS} CFLAGS_FOR_TARGET="-msoft-float" CCASFLAGS="-msoft-float"
340     install ${NEWLIB} install
341     cd ..
342     log "Cleaning up ${NEWLIB}"
343     touch ${STAMPS}/${NEWLIB}.build
344     rm -rf build/* ${NEWLIB}
345 fi
346
347 # Yes, you need to build gcc again!
348 if [ ! -e ${STAMPS}/${GCC}.build ]; then
349     unpack ${GCC}
350     cd build
351     log "Configuring ${GCC}"
352     ../${GCCDIR}/configure --target=${TARGET} \
353                       --prefix=${PREFIX} \
354                       --enable-interwork \
355                       --enable-multilib \
356                       --enable-languages="c,c++" \
357                       --with-newlib \
358                       --disable-shared \
359                       --with-gnu-as \
360                       --with-gnu-ld \
361                       --disable-nls \
362                       --disable-werror \
363                       --with-system-zlib \
364                      ${GCCFLAGS}
365     log "Building ${GCC}"
366     make ${MAKEFLAGS}
367     install ${GCC} install
368     cd ..
369     log "Cleaning up ${GCC}"
370     touch ${STAMPS}/${GCC}.build
371     rm -rf build/* ${GCCDIR}
372 fi
373
374 if [ ! -e ${STAMPS}/${GDB}.build ]; then
375     unpack ${GDB}
376     cd build
377     log "Configuring ${GDB}"
378     ../${GDB}/configure --target=${TARGET} \
379                       --prefix=${PREFIX} \
380                       --enable-interwork \
381                       --enable-multilib \
382                       --disable-werror \
383                       ${GDBFLAGS}
384     log "Building ${GDB}"
385     make ${MAKEFLAGS}
386     install ${GDB} install
387     cd ..
388     log "Cleaning up ${GDB}"
389     touch ${STAMPS}/${GDB}.build
390     rm -rf build/* ${GDB}
391 fi
392
393 if [ ${OOCD_EN} != 0 ]; then
394 if [ ! -e ${STAMPS}/openocd-${OOCD}.build ]; then
395     unpack openocd-${OOCD}
396     cd build
397     log "Configuring openocd-${OOCD}"
398     CFLAGS="${CFLAGS} ${OOCD_CFLAGS}" \
399     LDFLAGS="${LDFLAGS} ${OOCD_LDFLAGS}" \
400     ../openocd-${OOCD}/configure --enable-maintainer-mode \
401                       --prefix=${PREFIX} \
402                       --enable-dummy \
403                       --enable-parport \
404                       --enable-ft2232_libftdi \
405                       --enable-usb_blaster_libftdi \
406                       --enable-amtjtagaccel \
407                       --enable-zy1000 \
408                       --enable-ep93xx \
409                       --enable-at91rm9200 \
410                       --enable-gw16012 \
411                       --enable-presto_libftdi \
412                       --enable-usbprog \
413                       --enable-jlink \
414                       --enable-vsllink \
415                       --enable-rlink \
416                       --enable-arm-jtag-ew \
417                       --enable-buspirate
418     log "Building openocd-${OOCD}"
419     make ${MAKEFLAGS}
420     install openocd-${OOCD} install
421     cd ..
422     log "Cleaning up openocd-${OOCD}"
423     touch ${STAMPS}/openocd-${OOCD}.build
424     rm -rf build/* ${OOCD}
425 fi
426 fi
427
428 if [ ${LIBSTM32_EN} != 0 ]; then
429 if [ ! -e ${STAMPS}/libcmsis-${LIBCMSIS}.build ]; then
430     unpack libcmsis-${LIBCMSIS}
431     cd libcmsis-${LIBCMSIS}
432     log "Building libcmsis-${LIBCMSIS}"
433     make arch_prefix=${TARGET} prefix=${PREFIX}
434     install libcmsis-${LIBCMSIS} arch_prefix=${TARGET} prefix=${PREFIX} install
435     cd ..
436     log "Cleaning up libcmsis-${LIBCMSIS}"
437     touch ${STAMPS}/libcmsis-${LIBCMSIS}.build
438     rm -rf libcmsis-${LIBCMSIS}
439 fi
440
441 if [ ! -e ${STAMPS}/libstm32-${LIBSTM32}.build ]; then
442     unpack libstm32-${LIBSTM32}
443     cd libstm32-${LIBSTM32}
444     log "Building libstm32-${LIBSTM32}"
445     make arch_prefix=${TARGET} prefix=${PREFIX}
446     install libstm32-${LIBSTM32} arch_prefix=${TARGET} prefix=${PREFIX} install
447     cd ..
448     log "Cleaning up libstm32-${LIBSTM32}"
449     touch ${STAMPS}/libstm32-${LIBSTM32}.build
450     rm -rf libstm32-${LIBSTM32}
451 fi
452
453 if [ ! -e ${STAMPS}/libstm32usb-${LIBSTM32USB}.build ]; then
454     unpack libstm32usb-${LIBSTM32USB}
455     cd libstm32usb-${LIBSTM32USB}
456     log "Building libstm32usb-${LIBSTM32USB}"
457     make arch_prefix=${TARGET} prefix=${PREFIX}
458     install libstm32usb-${LIBSTM32USB} arch_prefix=${TARGET} prefix=${PREFIX} install
459     cd ..
460     log "Cleaning up libstm32usb-${LIBSTM32USB}"
461     touch ${STAMPS}/libstm32usb-${LIBSTM32USB}.build
462     rm -rf libstm32usb-${LIBSTM32USB}
463 fi
464 fi
465
466 if [ $LIBOPENSTM32_EN != 0 ]; then
467 if [ ! -e ${STAMPS}/libopenstm32-${LIBOPENSTM32}.build ]; then
468     unpack libopenstm32-${LIBOPENSTM32}
469     cd libopenstm32-${LIBOPENSTM32}
470     log "Building libopenstm32-${LIBOPENSTM32}"
471     make PREFIX=${TARGET} DESTDIR=${PREFIX}
472     install libopenstm32-${LIBOPENSTM32} PREFIX=${TARGET} DESTDIR=${PREFIX} install
473     cd ..
474     log "Cleaning up libopenstm32-${LIBOPENSTM32}"
475     touch ${STAMPS}/libopenstm32-${LIBOPENSTM32}.build
476     rm -rf libopenstm32-${LIBOPENSTM32}
477 fi
478 fi