new software versions
[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="gnutar mpfr libmpc gmp"
120   DARWINDEPSCOUNT=4
121
122   GCCFLAGS="${GCCFLAGS} \
123             --with-gmp=${DARWIN_OPT_PATH} \
124                   --with-mpfr=${DARWIN_OPT_PATH} \
125                   --with-mpc=${DARWIN_OPT_PATH} \
126                         -with-libiconv-prefix=${DARWIN_OPT_PATH}"
127
128   echo "Attempting to automatically check if common dependencies are installed on your system"
129   if ! which port > /dev/null; then
130     echo "MacPorts not installed."
131   else
132     pc=`port installed ${DARWINDEPS} | wc -l | tr -d " "`
133     if [ ${pc} -lt $[${DARWINDEPSCOUNT}+1] ];
134     then
135       echo "Something is missing, do you want to install everything needed?"
136       echo "(You need super user rights to do this.)"
137
138       if [ read = "y" || read = "yes" ];
139       then
140         sudo port install ${DARWINDEPS}
141       fi
142     else
143       echo "Found: "${DARWINDEPS}"!"
144     fi
145   fi
146
147   TAR=gnutar
148
149                   --with-libiconv-prefix=${DARWIN_OPT_PATH}"
150         OOCD_CFLAGS="-m32 -I/opt/mine/include -I/opt/local/include"
151         OOCD_LDFLAGS="-L/opt/mine/lib -L/opt/local/lib"
152         ;;
153         CYGWIN*)
154         echo "Found CygWin that means Windows most likely."
155         ;;
156         *)
157         echo "Found unknown OS. Aborting!"
158         exit 1
159         ;;
160 esac
161
162 ##############################################################################
163 # Building section
164 # You probably don't have to touch anything after this
165 ##############################################################################
166
167 # Fetch a versioned file from a URL
168 function fetch {
169     if [ ! -e ${STAMPS}/$1.fetch ]; then
170         log "Downloading $1 sources..."
171         wget -c --no-passive-ftp $2
172         touch ${STAMPS}/$1.fetch
173     fi
174 }
175
176 # Log a message out to the console
177 function log {
178     echo "******************************************************************"
179     echo "* $*"
180     echo "******************************************************************"
181 }
182
183 # Unpack an archive
184 function unpack {
185     log Unpacking $*
186     # Use 'auto' mode decompression.  Replace with a switch if tar doesn't support -a
187     ARCHIVE=$(ls ${SOURCES}/$1.tar.*)
188     case ${ARCHIVE} in
189         *.bz2)
190             echo "archive type bz2"
191             TYPE=j
192             ;;
193         *.gz)
194             echo "archive type gz"
195             TYPE=z
196             ;;
197         *)
198             echo "Unknown archive type of $1"
199             echo ${ARCHIVE}
200             exit 1
201             ;;
202     esac
203     ${TAR} xf${TYPE}${TARFLAGS} ${SOURCES}/$1.tar.*
204 }
205
206 # Install a build
207 function install {
208     log $1
209     ${SUDO} make ${MAKEFLAGS} $2 $3 $4 $5 $6 $7 $8
210 }
211
212
213 mkdir -p ${STAMPS} ${SOURCES}
214
215 cd ${SOURCES}
216
217 fetch ${BINUTILS} http://ftp.gnu.org/gnu/binutils/${BINUTILS}.tar.bz2
218 fetch ${GCC} ${GCCURL}
219 fetch ${NEWLIB} ftp://sources.redhat.com/pub/newlib/${NEWLIB}.tar.gz
220 fetch ${GDB} http://ftp.gnu.org/gnu/gdb/${GDB}.tar.bz2
221
222 if [ ${OOCD_EN} != 0 ]; then
223 if [ ! -e openocd-${OOCD}.tar.bz2 ]; then
224         log "Cloning OpenOCD sources..."
225         git clone git://openocd.git.sourceforge.net/gitroot/openocd/openocd openocd-${OOCD}
226         cd openocd-${OOCD}
227         ./bootstrap
228         cd ..
229         tar cfvj openocd-${OOCD}.tar.bz2 openocd-${OOCD}
230         #git archive --format=tar --prefix=openocd-${OOCD}/ ${OOCD} | \
231         #    bzip2 --stdout > ../openocd-${OOCD}.tar.bz2
232         rm -rf openocd-${OOCD}
233 fi
234 fi
235
236 if [ ${LIBSTM32_EN} != 0 ]; then
237 if [ ! -e libcmsis-${LIBCMSIS}.tar.bz2 ]; then
238         log "Cloning libcmsis sources..."
239         git clone git://git.open-bldc.org/libcmsis.git
240         cd libcmsis
241         git archive --format=tar --prefix=libcmsis-${LIBCMSIS}/ ${LIBCMSIS} | \
242             bzip2 --stdout > ../libcmsis-${LIBCMSIS}.tar.bz2
243         cd ..
244         rm -rf libcmsis
245 fi
246
247 if [ ! -e libstm32-${LIBSTM32}.tar.bz2 ]; then
248         log "Cloning libstm32 sources..."
249         git clone git://git.open-bldc.org/libstm32.git
250         cd libstm32
251         git archive --format=tar --prefix=libstm32-${LIBSTM32}/ ${LIBSTM32} | \
252             bzip2 --stdout > ../libstm32-${LIBSTM32}.tar.bz2
253         cd ..
254         rm -rf libstm32
255 fi
256
257 if [ ! -e libstm32usb-${LIBSTM32USB}.tar.bz2 ]; then
258         log "Cloning libstm32usb sources..."
259         git clone git://git.open-bldc.org/libstm32usb.git
260         cd libstm32usb
261         git archive --format=tar --prefix=libstm32usb-${LIBSTM32USB}/ ${LIBSTM32USB} | \
262             bzip2 --stdout > ../libstm32usb-${LIBSTM32USB}.tar.bz2
263         cd ..
264         rm -rf libstm32usb
265 fi
266 fi
267
268 if [ ${LIBOPENSTM32_EN} != 0 ]; then
269 if [ ! -e libopenstm32-${LIBOPENSTM32}.tar.bz2 ]; then
270         log "Cloning libopenstm32 sources..."
271         git clone git://libopenstm32.git.sourceforge.net/gitroot/libopenstm32/libopenstm32
272         cd libopenstm32
273         git archive --format=tar --prefix=libopenstm32-${LIBOPENSTM32}/ ${LIBOPENSTM32} | \
274             bzip2 --stdout > ../libopenstm32-${LIBOPENSTM32}.tar.bz2
275         cd ..
276         rm -rf libopenstm32
277 fi
278 fi
279
280 cd ${SUMMON_DIR}
281
282 if [ ! -e build ]; then
283     mkdir build
284 fi
285
286 if [ ! -e ${STAMPS}/${BINUTILS}.build ]; then
287     unpack ${BINUTILS}
288     cd build
289     log "Configuring ${BINUTILS}"
290     ../${BINUTILS}/configure --target=${TARGET} \
291                            --prefix=${PREFIX} \
292                            --enable-interwork \
293                            --enable-multilib \
294                            --with-gnu-as \
295                            --with-gnu-ld \
296                            --disable-nls \
297                            --disable-werror \
298                            ${BINUTILFLAGS}
299     log "Building ${BINUTILS}"
300     make ${MAKEFLAGS}
301     install ${BINUTILS} install
302     cd ..
303     log "Cleaning up ${BINUTILS}"
304     touch ${STAMPS}/${BINUTILS}.build
305     rm -rf build/* ${BINUTILS}
306 fi
307
308 if [ ! -e ${STAMPS}/${GCC}-boot.build ]; then
309     unpack ${GCC} boot
310     cd build
311     log "Configuring ${GCC}-boot"
312     ../${GCC}/configure --target=${TARGET} \
313                       --prefix=${PREFIX} \
314                       --enable-interwork \
315                       --enable-multilib \
316                       --enable-languages="c" \
317                       --with-newlib \
318                       --without-headers \
319                       --disable-shared \
320                       --with-gnu-as \
321                       --with-gnu-ld \
322                       --disable-nls \
323                       --disable-werror \
324                       --with-system-zlib \
325                       ${GCCFLAGS}
326     log "Building ${GCC}-boot"
327     make ${MAKEFLAGS} all-gcc
328     install ${GCC}-boot install-gcc
329     cd ..
330     log "Cleaning up ${GCC}-boot"
331     touch ${STAMPS}/${GCC}-boot.build
332     rm -rf build/* ${GCC}
333 fi
334
335 if [ ! -e ${STAMPS}/${NEWLIB}.build ]; then
336     unpack ${NEWLIB}
337     cd build
338     log "Configuring ${NEWLIB}"
339     ../${NEWLIB}/configure --target=${TARGET} \
340                          --prefix=${PREFIX} \
341                          --enable-interwork \
342                          --enable-multilib \
343                          --with-gnu-as \
344                          --with-gnu-ld \
345                          --disable-nls \
346                          --disable-werror \
347                          --disable-newlib-supplied-syscalls \
348                          --with-float=soft
349     log "Building ${NEWLIB}"
350     make ${MAKEFLAGS} CFLAGS_FOR_TARGET="-msoft-float" CCASFLAGS="-msoft-float"
351     install ${NEWLIB} install
352     cd ..
353     log "Cleaning up ${NEWLIB}"
354     touch ${STAMPS}/${NEWLIB}.build
355     rm -rf build/* ${NEWLIB}
356 fi
357
358 # Yes, you need to build gcc again!
359 if [ ! -e ${STAMPS}/${GCC}.build ]; then
360     unpack ${GCC}
361     cd build
362     log "Configuring ${GCC}"
363     ../${GCC}/configure --target=${TARGET} \
364                       --prefix=${PREFIX} \
365                       --enable-interwork \
366                       --enable-multilib \
367                       --enable-languages="c,c++" \
368                       --with-newlib \
369                       --disable-shared \
370                       --with-gnu-as \
371                       --with-gnu-ld \
372                       --disable-nls \
373                       --disable-werror \
374                       --with-system-zlib \
375                      ${GCCFLAGS}
376     log "Building ${GCC}"
377     make ${MAKEFLAGS}
378     install ${GCC} install
379     cd ..
380     log "Cleaning up ${GCC}"
381     touch ${STAMPS}/${GCC}.build
382     rm -rf build/* ${GCC}
383 fi
384
385 if [ ! -e ${STAMPS}/${GDB}.build ]; then
386     unpack ${GDB}
387     cd build
388     log "Configuring ${GDB}"
389     ../${GDB}/configure --target=${TARGET} \
390                       --prefix=${PREFIX} \
391                       --enable-interwork \
392                       --enable-multilib \
393                       --disable-werror \
394                       ${GDBFLAGS}
395     log "Building ${GDB}"
396     make ${MAKEFLAGS}
397     install ${GDB} install
398     cd ..
399     log "Cleaning up ${GDB}"
400     touch ${STAMPS}/${GDB}.build
401     rm -rf build/* ${GDB}
402 fi
403
404 if [ ${OOCD_EN} != 0 ]; then
405 if [ ! -e ${STAMPS}/openocd-${OOCD}.build ]; then
406     unpack openocd-${OOCD}
407     cd build
408     log "Configuring openocd-${OOCD}"
409     CFLAGS="${CFLAGS} ${OOCD_CFLAGS}" \
410     LDFLAGS="${LDFLAGS} ${OOCD_LDFLAGS}" \
411     ../openocd-${OOCD}/configure --enable-maintainer-mode \
412                       --prefix=${PREFIX} \
413                       --enable-dummy \
414                       --enable-parport \
415                       --enable-ft2232_libftdi \
416                       --enable-usb_blaster_libftdi \
417                       --enable-amtjtagaccel \
418                       --enable-zy1000 \
419                       --enable-ep93xx \
420                       --enable-at91rm9200 \
421                       --enable-gw16012 \
422                       --enable-presto_libftdi \
423                       --enable-usbprog \
424                       --enable-jlink \
425                       --enable-vsllink \
426                       --enable-rlink \
427                       --enable-arm-jtag-ew \
428                       --enable-buspirate
429     log "Building openocd-${OOCD}"
430     make ${MAKEFLAGS}
431     install openocd-${OOCD} install
432     cd ..
433     log "Cleaning up openocd-${OOCD}"
434     touch ${STAMPS}/openocd-${OOCD}.build
435     rm -rf build/* ${OOCD}
436 fi
437 fi
438
439 if [ ${LIBSTM32_EN} != 0 ]; then
440 if [ ! -e ${STAMPS}/libcmsis-${LIBCMSIS}.build ]; then
441     unpack libcmsis-${LIBCMSIS}
442     cd libcmsis-${LIBCMSIS}
443     log "Building libcmsis-${LIBCMSIS}"
444     make arch_prefix=${TARGET} prefix=${PREFIX}
445     install libcmsis-${LIBCMSIS} arch_prefix=${TARGET} prefix=${PREFIX} install
446     cd ..
447     log "Cleaning up libcmsis-${LIBCMSIS}"
448     touch ${STAMPS}/libcmsis-${LIBCMSIS}.build
449     rm -rf libcmsis-${LIBCMSIS}
450 fi
451
452 if [ ! -e ${STAMPS}/libstm32-${LIBSTM32}.build ]; then
453     unpack libstm32-${LIBSTM32}
454     cd libstm32-${LIBSTM32}
455     log "Building libstm32-${LIBSTM32}"
456     make arch_prefix=${TARGET} prefix=${PREFIX}
457     install libstm32-${LIBSTM32} arch_prefix=${TARGET} prefix=${PREFIX} install
458     cd ..
459     log "Cleaning up libstm32-${LIBSTM32}"
460     touch ${STAMPS}/libstm32-${LIBSTM32}.build
461     rm -rf libstm32-${LIBSTM32}
462 fi
463
464 if [ ! -e ${STAMPS}/libstm32usb-${LIBSTM32USB}.build ]; then
465     unpack libstm32usb-${LIBSTM32USB}
466     cd libstm32usb-${LIBSTM32USB}
467     log "Building libstm32usb-${LIBSTM32USB}"
468     make arch_prefix=${TARGET} prefix=${PREFIX}
469     install libstm32usb-${LIBSTM32USB} arch_prefix=${TARGET} prefix=${PREFIX} install
470     cd ..
471     log "Cleaning up libstm32usb-${LIBSTM32USB}"
472     touch ${STAMPS}/libstm32usb-${LIBSTM32USB}.build
473     rm -rf libstm32usb-${LIBSTM32USB}
474 fi
475 fi
476
477 if [ $LIBOPENSTM32_EN != 0 ]; then
478 if [ ! -e ${STAMPS}/libopenstm32-${LIBOPENSTM32}.build ]; then
479     unpack libopenstm32-${LIBOPENSTM32}
480     cd libopenstm32-${LIBOPENSTM32}
481     log "Building libopenstm32-${LIBOPENSTM32}"
482     make PREFIX=${TARGET} DESTDIR=${PREFIX}
483     install libopenstm32-${LIBOPENSTM32} PREFIX=${TARGET} DESTDIR=${PREFIX} install
484     cd ..
485     log "Cleaning up libopenstm32-${LIBOPENSTM32}"
486     touch ${STAMPS}/libopenstm32-${LIBOPENSTM32}.build
487     rm -rf libopenstm32-${LIBOPENSTM32}
488 fi
489 fi