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