port to homebrew instead of macports
[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
8 # Stop if any command fails
9 set -e
10
11 ##############################################################################
12 # Settings section
13 # You probably want to customize those
14 ##############################################################################
15 TARGET=arm-eabi         # Or: TARGET=arm-elf
16 PREFIX=${HOME}/arm      # Install location of your final toolchain
17 PARALLEL=                       # Or: PARALLEL="-j 5" for 4 CPUs
18 DARWIN_CELLAR=$(brew --cellar)  # Path in which MacPorts or Fink is installed
19 # Set to 'sudo' if you need superuser privileges while installing
20 SUDO=''
21 # Set to 1 to be quieter while running
22 QUIET=1
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         GCCURL=http://ftp.gnu.org/gnu/gcc/${GCC}/${GCC}.tar.gz
42 else
43         # For the Linaro GCC:
44         GCCRELEASE=4.6-2011.07
45         GCCVERSION=${GCCRELEASE}
46         GCC=gcc-linaro-${GCCVERSION}
47         GCCURL=http://launchpad.net/gcc-linaro/4.5/${GCCRELEASE}/+download/${GCC}.tar.bz2
48 fi
49
50 BINUTILS=binutils-2.21
51 NEWLIB=newlib-1.19.0
52 GDB=gdb-7.3
53 OOCD=master
54 LIBCMSIS=v1.10-2
55 LIBSTM32=v3.0.0-1
56 LIBSTM32USB=v3.0.1-1
57 LIBOPENSTM32=master
58
59 ##############################################################################
60 # Flags section
61 ##############################################################################
62
63 if which getconf > /dev/null; then
64         CPUS=$(getconf _NPROCESSORS_ONLN)
65 else
66         CPUS=1
67 fi
68 PARALLEL=-j$((CPUS + 1))
69 echo "${CPUS} cpu's detected running make with '${PARALLEL}' flag"
70
71 GDBFLAGS=
72 BINUTILFLAGS=
73
74 if [ ${DEFAULT_TO_CORTEX_M3} == 0 ] ; then
75         GCCFLAGS=
76 else
77         # To default to the Cortex-M3:
78         GCCFLAGS="--with-arch=armv7-m --with-mode=thumb --with-float=soft"
79 fi
80
81 # Pull in the local configuration, if any
82 if [ -f local.sh ]; then
83     . ./local.sh
84 fi
85
86 MAKEFLAGS=${PARALLEL}
87 TARFLAGS=v
88
89 if [ ${QUIET} != 0 ]; then
90     TARFLAGS=
91     MAKEFLAGS="${MAKEFLAGS} -s"
92 fi
93
94 export PATH="${PREFIX}/bin:${PATH}"
95
96 SUMMON_DIR=$(pwd)
97 SOURCES=${SUMMON_DIR}/sources
98 STAMPS=${SUMMON_DIR}/stamps
99
100
101 ##############################################################################
102 # Tool section
103 ##############################################################################
104 TAR=tar
105
106 ##############################################################################
107 # OS and Tooldetection section
108 # Detects which tools and flags to use
109 ##############################################################################
110
111 case "$(uname)" in
112         Linux)
113         echo "Found Linux OS."
114         ;;
115         Darwin)
116         echo "Found Darwin OS."
117
118         # darwin dependencies
119         DARWINDEPS="gnu-tar wget mpfr libmpc gmp"
120   DARWINDEPSCOUNT=4
121
122   echo "Installing dependencies"
123   if ! which brew > /dev/null; then
124     echo "homebrew not installed."
125   else
126     brew install ${DARWINDEPS} | grep -v '^Warning: Formula already installed: '
127   fi
128
129   GCCFLAGS="${GCCFLAGS} \
130       --with-gmp=$(brew --prefix gmp) \
131       --with-mpfr=$(brew --prefix mpfr) \
132                   --with-mpc=$(brew --prefix mpc)
133   -with-libiconv-prefix=$(brew --prefix libiconv)"
134
135   TAR=gnutar
136
137                   --with-libiconv-prefix=${DARWIN_OPT_PATH}"
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