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