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