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