355454761b80a53a0c81cb04971b726e87d296ac
[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 ##############################################################################
98 # Version and download url settings section
99 ##############################################################################
100 if [ ${USE_LINARO} == 0 ] ; then
101         # For FSF GCC:
102         GCCVERSION=4.5.2
103         GCC=gcc-${GCCVERSION}
104         GCCURL=http://ftp.gnu.org/gnu/gcc/${GCC}/${GCC}.tar.gz
105 else
106         # For the Linaro GCC:
107         GCCRELEASE=4.5-2011.02-0
108         GCCVERSION=4.5-2011.02-0
109         GCC=gcc-linaro-${GCCVERSION}
110         GCCURL=http://launchpad.net/gcc-linaro/4.5/${GCCRELEASE}/+download/${GCC}.tar.bz2
111 fi
112
113 BINUTILS=binutils-2.21
114 NEWLIB=newlib-1.19.0
115 GDB=gdb-7.2
116 OOCD=master
117 LIBCMSIS=v1.10-3
118 LIBSTM32=v3.0.0-2
119 LIBSTM32USB=v3.0.1-1
120 LIBOPENSTM32=master
121
122 ##############################################################################
123 # Flags section
124 ##############################################################################
125
126 if which getconf > /dev/null; then
127         CPUS=$(getconf _NPROCESSORS_ONLN)
128 else
129         CPUS=1
130 fi
131 PARALLEL=-j$((CPUS + 1))
132 echo "${CPUS} cpu's detected running make with '${PARALLEL}' flag"
133
134 GDBFLAGS=
135 BINUTILFLAGS=
136
137 if [ ${DEFAULT_TO_CORTEX_M3} == 0 ] ; then
138         GCCFLAGS=
139 else
140         # To default to the Cortex-M3:
141         GCCFLAGS="--with-arch=armv7-m --with-mode=thumb --with-float=soft"
142 fi
143
144 # Pull in the local configuration, if any
145 if [ -f local.sh ]; then
146     . ./local.sh
147 fi
148
149 MAKEFLAGS=${PARALLEL}
150 TARFLAGS=v
151
152 if [ ${QUIET} != 0 ]; then
153     TARFLAGS=
154     MAKEFLAGS="${MAKEFLAGS} -s"
155 fi
156
157 export PATH="${PREFIX}/bin:${PATH}"
158
159 SUMMON_DIR=$(pwd)
160 SOURCES=${SUMMON_DIR}/sources
161 STAMPS=${SUMMON_DIR}/stamps
162
163
164 ##############################################################################
165 # Tool section
166 ##############################################################################
167 TAR=tar
168
169 ##############################################################################
170 # OS and Tooldetection section
171 # Detects which tools and flags to use
172 ##############################################################################
173
174 case "$(uname)" in
175         Linux)
176         echo "Found Linux OS."
177         ;;
178         Darwin)
179         echo "Found Darwin OS."
180         GCCFLAGS="${GCCFLAGS} \
181                   --with-gmp=${DARWIN_OPT_PATH} \
182                   --with-mpfr=${DARWIN_OPT_PATH} \
183                   --with-mpc=${DARWIN_OPT_PATH} \
184                   --with-libiconv-prefix=${DARWIN_OPT_PATH}"
185         OOCD_CFLAGS="-I/opt/mine/include -I/opt/local/include"
186         OOCD_LDFLAGS="-L/opt/mine/lib -L/opt/local/lib"
187         ;;
188         CYGWIN*)
189         echo "Found CygWin that means Windows most likely."
190         ;;
191         *)
192         echo "Found unknown OS. Aborting!"
193         exit 1
194         ;;
195 esac
196
197 ##############################################################################
198 # Building section
199 # You probably don't have to touch anything after this
200 ##############################################################################
201
202 # Fetch a versioned file from a URL
203 function fetch {
204     if [ ! -e ${STAMPS}/$1.fetch ]; then
205         log "Downloading $1 sources..."
206         wget -c --no-passive-ftp $2
207         touch ${STAMPS}/$1.fetch
208     fi
209 }
210
211 # Log a message out to the console
212 function log {
213     echo "******************************************************************"
214     echo "* $*"
215     echo "******************************************************************"
216 }
217
218 # Unpack an archive
219 function unpack {
220     log Unpacking $*
221     # Use 'auto' mode decompression.  Replace with a switch if tar doesn't support -a
222     ARCHIVE=$(ls ${SOURCES}/$1.tar.*)
223     case ${ARCHIVE} in
224         *.bz2)
225             echo "archive type bz2"
226             TYPE=j
227             ;;
228         *.gz)
229             echo "archive type gz"
230             TYPE=z
231             ;;
232         *)
233             echo "Unknown archive type of $1"
234             echo ${ARCHIVE}
235             exit 1
236             ;;
237     esac
238     ${TAR} xf${TYPE}${TARFLAGS} ${SOURCES}/$1.tar.*
239 }
240
241 # Install a build
242 function install {
243     log $1
244     ${SUDO} make ${MAKEFLAGS} $2 $3 $4 $5 $6 $7 $8
245 }
246
247
248 mkdir -p ${STAMPS} ${SOURCES}
249
250 cd ${SOURCES}
251
252 fetch ${BINUTILS} http://ftp.gnu.org/gnu/binutils/${BINUTILS}.tar.bz2
253 fetch ${GCC} ${GCCURL}
254 fetch ${NEWLIB} ftp://sources.redhat.com/pub/newlib/${NEWLIB}.tar.gz
255 fetch ${GDB} http://ftp.gnu.org/gnu/gdb/${GDB}.tar.bz2
256
257 if [ ${OOCD_EN} != 0 ]; then
258 if [ ! -e openocd-${OOCD}.tar.bz2 ]; then
259         log "Cloning OpenOCD sources..."
260         git clone git://openocd.git.sourceforge.net/gitroot/openocd/openocd openocd-${OOCD}
261         cd openocd-${OOCD}
262         ./bootstrap
263         cd ..
264         tar cfvj openocd-${OOCD}.tar.bz2 openocd-${OOCD}
265         #git archive --format=tar --prefix=openocd-${OOCD}/ ${OOCD} | \
266         #    bzip2 --stdout > ../openocd-${OOCD}.tar.bz2
267         rm -rf openocd-${OOCD}
268 fi
269 fi
270
271 if [ ${LIBSTM32_EN} != 0 ]; then
272 if [ ! -e libcmsis-${LIBCMSIS}.tar.bz2 ]; then
273         log "Cloning libcmsis sources..."
274         git clone git://git.open-bldc.org/libcmsis.git
275         cd libcmsis
276         git archive --format=tar --prefix=libcmsis-${LIBCMSIS}/ ${LIBCMSIS} | \
277             bzip2 --stdout > ../libcmsis-${LIBCMSIS}.tar.bz2
278         cd ..
279         rm -rf libcmsis
280 fi
281
282 if [ ! -e libstm32-${LIBSTM32}.tar.bz2 ]; then
283         log "Cloning libstm32 sources..."
284         git clone git://git.open-bldc.org/libstm32.git
285         cd libstm32
286         git archive --format=tar --prefix=libstm32-${LIBSTM32}/ ${LIBSTM32} | \
287             bzip2 --stdout > ../libstm32-${LIBSTM32}.tar.bz2
288         cd ..
289         rm -rf libstm32
290 fi
291
292 if [ ! -e libstm32usb-${LIBSTM32USB}.tar.bz2 ]; then
293         log "Cloning libstm32usb sources..."
294         git clone git://git.open-bldc.org/libstm32usb.git
295         cd libstm32usb
296         git archive --format=tar --prefix=libstm32usb-${LIBSTM32USB}/ ${LIBSTM32USB} | \
297             bzip2 --stdout > ../libstm32usb-${LIBSTM32USB}.tar.bz2
298         cd ..
299         rm -rf libstm32usb
300 fi
301 fi
302
303 if [ ${LIBOPENSTM32_EN} != 0 ]; then
304 if [ ! -e libopenstm32-${LIBOPENSTM32}.tar.bz2 ]; then
305         log "Cloning libopenstm32 sources..."
306         git clone git://libopenstm32.git.sourceforge.net/gitroot/libopenstm32/libopenstm32
307         cd libopenstm32
308         git archive --format=tar --prefix=libopenstm32-${LIBOPENSTM32}/ ${LIBOPENSTM32} | \
309             bzip2 --stdout > ../libopenstm32-${LIBOPENSTM32}.tar.bz2
310         cd ..
311         rm -rf libopenstm32
312 fi
313 fi
314
315 cd ${SUMMON_DIR}
316
317 if [ ! -e build ]; then
318     mkdir build
319 fi
320
321 if [ ! -e ${STAMPS}/${BINUTILS}.build ]; then
322     unpack ${BINUTILS}
323     cd build
324     log "Configuring ${BINUTILS}"
325     ../${BINUTILS}/configure --target=${TARGET} \
326                            --prefix=${PREFIX} \
327                            --enable-interwork \
328                            --enable-multilib \
329                            --with-gnu-as \
330                            --with-gnu-ld \
331                            --disable-nls \
332                            --disable-werror \
333                            ${BINUTILFLAGS}
334     log "Building ${BINUTILS}"
335     make ${MAKEFLAGS}
336     install ${BINUTILS} install
337     cd ..
338     log "Cleaning up ${BINUTILS}"
339     touch ${STAMPS}/${BINUTILS}.build
340     rm -rf build/* ${BINUTILS}
341 fi
342
343 if [ ! -e ${STAMPS}/${GCC}-${NEWLIB}.build ]; then
344     unpack ${GCC}
345     unpack ${NEWLIB}
346
347     log "Adding newlib symlink to gcc"
348     ln -f -s `pwd`/${NEWLIB}/newlib ${GCC}
349     log "Adding libgloss symlink to gcc"
350     ln -f -s `pwd`/${NEWLIB}/libgloss ${GCC}
351
352     if [ ${DEFAULT_TO_CORTEX_M3} == 0 ] ; then
353         log "Patching gcc to add multilib support"
354         cd ${GCC}
355         patch -p0 -i ../patches/patch-gcc-config-arm-t-arm-elf.diff
356         cd ..
357     fi
358
359     cd build
360     log "Configuring ${GCC} and ${NEWLIB}"
361     ../${GCC}/configure --target=${TARGET} \
362                       --prefix=${PREFIX} \
363                       --enable-interwork \
364                       --enable-multilib \
365                       --enable-languages="c,c++" \
366                       --with-newlib \
367                       --with-gnu-as \
368                       --with-gnu-ld \
369                       --disable-nls \
370                       --disable-shared \
371                       --disable-threads \
372                       --with-headers=newlib/libc/include \
373                       --disable-libssp \
374                       --disable-libstdcxx-pch \
375                       --disable-libmudflap \
376                       --disable-libgomp \
377                       --disable-werror \
378                       --with-system-zlib \
379                       --disable-newlib-supplied-syscalls \
380                       ${GCCFLAGS}
381     log "Building ${GCC} and ${NEWLIB}"
382     make ${MAKEFLAGS}
383     install ${GCC} install
384     cd ..
385     log "Cleaning up ${GCC} and ${NEWLIB}"
386     touch ${STAMPS}/${GCC}-${NEWLIB}.build
387     rm -rf build/* ${GCC} ${NEWLIB}
388 fi
389
390 if [ ! -e ${STAMPS}/${GDB}.build ]; then
391     unpack ${GDB}
392     cd build
393     log "Configuring ${GDB}"
394     ../${GDB}/configure --target=${TARGET} \
395                       --prefix=${PREFIX} \
396                       --enable-interwork \
397                       --enable-multilib \
398                       --disable-werror \
399                       ${GDBFLAGS}
400     log "Building ${GDB}"
401     make ${MAKEFLAGS}
402     install ${GDB} install
403     cd ..
404     log "Cleaning up ${GDB}"
405     touch ${STAMPS}/${GDB}.build
406     rm -rf build/* ${GDB}
407 fi
408
409 if [ ${OOCD_EN} != 0 ]; then
410 if [ ! -e ${STAMPS}/openocd-${OOCD}.build ]; then
411     unpack openocd-${OOCD}
412     cd build
413     log "Configuring openocd-${OOCD}"
414     CFLAGS="${CFLAGS} ${OOCD_CFLAGS}" \
415     LDFLAGS="${LDFLAGS} ${OOCD_LDFLAGS}" \
416     ../openocd-${OOCD}/configure --enable-maintainer-mode \
417                                  --prefix=${PREFIX} \
418                                  --enable-dummy \
419                                  --enable-ft2232_libftdi \
420                                  --enable-usb_blaster_libftdi \
421                                  --enable-ep93xx \
422                                  --enable-at91rm9200 \
423                                  --enable-presto_libftdi \
424                                  --enable-usbprog \
425                                  --enable-jlink \
426                                  --enable-vsllink \
427                                  --enable-rlink \
428                                  --enable-arm-jtag-ew
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