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