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