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