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