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