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