compile GMP, MPFR and MPC with GCC
[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 GMP=gmp-5.0.2
69 MPFR=mpfr-3.0.1
70 MPC=mpc-0.9
71
72 ##############################################################################
73 # Flags section
74 ##############################################################################
75
76 if which getconf > /dev/null; then
77         CPUS=$(getconf _NPROCESSORS_ONLN)
78 else
79         CPUS=1
80 fi
81 PARALLEL=-j$((CPUS + 1))
82 echo "${CPUS} cpu's detected running make with '${PARALLEL}' flag"
83
84 GDBFLAGS=
85 BINUTILFLAGS=
86
87 if [ ${DEFAULT_TO_CORTEX_M3} = 0 ] ; then
88         GCCFLAGS=
89 else
90         # To default to the Cortex-M3:
91         GCCFLAGS="--with-arch=armv7-m --with-mode=thumb --with-float=soft"
92 fi
93
94 # Pull in the local configuration, if any
95 if [ -f local.sh ]; then
96     . ./local.sh
97 fi
98
99 MAKEFLAGS=${PARALLEL}
100 TARFLAGS=v
101
102 if [ ${QUIET} != 0 ]; then
103     TARFLAGS=
104     MAKEFLAGS="${MAKEFLAGS} -s"
105 fi
106
107 export PATH="${PREFIX}/bin:${PATH}"
108
109 SUMMON_DIR=${WORKDIR}
110 SOURCES=${SUMMON_DIR}/sources
111 STAMPS=${SUMMON_DIR}/stamps
112
113
114 ##############################################################################
115 # Tool section
116 ##############################################################################
117 TAR=tar
118
119 ##############################################################################
120 # OS and Tooldetection section
121 # Detects which tools and flags to use
122 ##############################################################################
123
124 case "$(uname)" in
125         Linux)
126         echo "Found Linux OS."
127         ;;
128         Darwin)
129         echo "Found Darwin OS."
130
131         # darwin dependencies
132         DARWINDEPS="wget"
133
134   echo "Installing dependencies ..."
135   if ! which brew > /dev/null; then
136     echo "Homebrew not installed."
137   else
138       brew install ${DARWINDEPS} | (grep -v '^Warning: Formula already installed: ' || true)
139   fi
140
141         TAR=gnutar
142
143         OOCD_CFLAGS="-m32 -I/opt/mine/include -I/opt/local/include"
144         OOCD_LDFLAGS="-L/opt/mine/lib -L/opt/local/lib"
145         ;;
146         CYGWIN*)
147         echo "Found CygWin that means Windows most likely."
148         ;;
149         *)
150         echo "Found unknown OS. Aborting!"
151         exit 1
152         ;;
153 esac
154
155 ##############################################################################
156 # Building section
157 # You probably don't have to touch anything after this
158 ##############################################################################
159
160 # Fetch a versioned file from a URL
161 fetch() {
162     if [ ! -e ${STAMPS}/$1.fetch ]; then
163         log "Downloading $1 sources..."
164         wget -c $2
165         touch ${STAMPS}/$1.fetch
166     fi
167 }
168
169 # Log a message out to the console
170 log() {
171     echo "******************************************************************"
172     echo "* $*"
173     echo "******************************************************************"
174 }
175
176 # Unpack an archive
177 unpack() {
178     log Unpacking $*
179     # Use 'auto' mode decompression.  Replace with a switch if tar doesn't support -a
180     ARCHIVE=$(ls ${SOURCES}/$1.tar.*)
181     case ${ARCHIVE} in
182         *.bz2)
183             echo "archive type bz2"
184             TYPE=j
185             ;;
186         *.gz)
187             echo "archive type gz"
188             TYPE=z
189             ;;
190         *)
191             echo "Unknown archive type of $1"
192             echo "leaving the decision to tar"
193             TYPE=
194             ;;
195     esac
196     ${TAR} xf${TYPE}${TARFLAGS} ${SOURCES}/$1.tar.*
197 }
198
199 # Install a build
200 install() {
201     log $1
202     ${SUDO} make ${MAKEFLAGS} $2 $3 $4 $5 $6 $7 $8
203 }
204
205 if [ ! -d $SUMMON_DIR ]; then
206     mkdir -p ${SUMMON_DIR}
207     SUMMON_DIR_CREATED=1
208 else
209     SUMMON_DIR_CREATED=0
210 fi
211 mkdir -p ${STAMPS} ${SOURCES}
212
213 cd ${SOURCES}
214
215 fetch ${BINUTILS} http://ftp.gnu.org/gnu/binutils/${BINUTILS}.tar.bz2
216 fetch ${GCC} ${GCCURL}
217 fetch ${NEWLIB} ftp://sources.redhat.com/pub/newlib/${NEWLIB}.tar.gz
218 fetch ${GDB} http://ftp.gnu.org/gnu/gdb/${GDB}.tar.bz2
219 fetch ${GMP} ftp://ftp.gmplib.org/pub/${GMP}/${GMP}.tar.bz2
220 fetch ${MPFR} http://www.mpfr.org/${MPFR}/${MPFR}.tar.bz2
221 fetch ${MPC} http://www.multiprecision.org/mpc/download/${MPC}.tar.gz
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     unpack ${GMP}
312     mv ${GMP} ${GCCDIR}/gmp
313     unpack ${MPFR}
314     mv ${MPFR} ${GCCDIR}/mpfr
315     unpack ${MPC}
316     mv ${MPC} ${GCCDIR}/mpc
317     cd build
318     log "Configuring ${GCC}-boot"
319     ../${GCCDIR}/configure --target=${TARGET} \
320                       --prefix=${PREFIX} \
321                       --enable-interwork \
322                       --enable-multilib \
323                       --enable-languages="c" \
324                       --with-newlib \
325                       --without-headers \
326                       --disable-shared \
327                       --with-gnu-as \
328                       --with-gnu-ld \
329                       --disable-nls \
330                       --disable-werror \
331                       --with-system-zlib \
332                       ${GCCFLAGS}
333     log "Building ${GCC}-boot"
334     make ${MAKEFLAGS} all-gcc
335     install ${GCC}-boot install-gcc
336     cd ..
337     log "Cleaning up ${GCC}-boot"
338     touch ${STAMPS}/${GCC}-boot.build
339     rm -rf build/* ${GCCDIR}
340 fi
341
342 if [ ! -e ${STAMPS}/${NEWLIB}.build ]; then
343     unpack ${NEWLIB}
344     cd build
345     log "Configuring ${NEWLIB}"
346     # configuration options and flags partially
347     # from https://github.com/jsnyder/arm-eabi-toolchain
348     ../${NEWLIB}/configure --target=${TARGET} \
349                          --prefix=${PREFIX} \
350                          --enable-interwork \
351                          --enable-multilib \
352                          --with-gnu-as \
353                          --with-gnu-ld \
354                          --disable-nls \
355                          --disable-werror \
356                          --disable-newlib-supplied-syscalls \
357                          --disable-shared \
358                          --disable-nls \
359                          --disable-libgloss \
360                          --with-float=soft
361     log "Building ${NEWLIB}"
362     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"
363     make ${MAKEFLAGS} CFLAGS_FOR_TARGET="${NEWLIB_FLAGS}" CCASFLAGS="${NEWLIB_FLAGS}"
364     install ${NEWLIB} install
365     cd ..
366     log "Cleaning up ${NEWLIB}"
367     touch ${STAMPS}/${NEWLIB}.build
368     rm -rf build/* ${NEWLIB}
369 fi
370
371 # Yes, you need to build gcc again!
372 if [ ! -e ${STAMPS}/${GCC}.build ]; then
373     unpack ${GCC}
374     unpack ${GMP}
375     mv ${GMP} ${GCCDIR}/gmp
376     unpack ${MPFR}
377     mv ${MPFR} ${GCCDIR}/mpfr
378     unpack ${MPC}
379     mv ${MPC} ${GCCDIR}/mpc
380     cd build
381     log "Configuring ${GCC}"
382     [ ${USE_CPP} = 1 ] && GCCFLAGS="--enable-languages='c,c++' ${GCCFLAGS}"
383     ../${GCCDIR}/configure --target=${TARGET} \
384                       --prefix=${PREFIX} \
385                       --enable-interwork \
386                       --enable-multilib \
387                       --with-newlib \
388                       --disable-shared \
389                       --with-gnu-as \
390                       --with-gnu-ld \
391                       --disable-nls \
392                       --disable-werror \
393                       --with-system-zlib \
394                      ${GCCFLAGS}
395     log "Building ${GCC}"
396     make ${MAKEFLAGS}
397     install ${GCC} install
398     cd ..
399     log "Cleaning up ${GCC}"
400     touch ${STAMPS}/${GCC}.build
401     rm -rf build/* ${GCCDIR}
402 fi
403
404 if [ ! -e ${STAMPS}/${GDB}.build ]; then
405     unpack ${GDB}
406     cd build
407     log "Configuring ${GDB}"
408     ../${GDB}/configure --target=${TARGET} \
409                       --prefix=${PREFIX} \
410                       --enable-interwork \
411                       --enable-multilib \
412                       --disable-werror \
413                       ${GDBFLAGS}
414     log "Building ${GDB}"
415     make ${MAKEFLAGS}
416     install ${GDB} install
417     cd ..
418     log "Cleaning up ${GDB}"
419     touch ${STAMPS}/${GDB}.build
420     rm -rf build/* ${GDB}
421 fi
422
423 if [ ${OOCD_EN} != 0 ]; then
424 if [ ! -e ${STAMPS}/openocd-${OOCD}.build ]; then
425     unpack openocd-${OOCD}
426     cd build
427     log "Configuring openocd-${OOCD}"
428     CFLAGS="${CFLAGS} ${OOCD_CFLAGS}" \
429     LDFLAGS="${LDFLAGS} ${OOCD_LDFLAGS}" \
430     ../openocd-${OOCD}/configure --enable-maintainer-mode \
431                       --prefix=${PREFIX} \
432                       --enable-dummy \
433                       --enable-parport \
434                       --enable-ft2232_libftdi \
435                       --enable-usb_blaster_libftdi \
436                       --enable-amtjtagaccel \
437                       --enable-zy1000 \
438                       --enable-ep93xx \
439                       --enable-at91rm9200 \
440                       --enable-gw16012 \
441                       --enable-presto_libftdi \
442                       --enable-usbprog \
443                       --enable-jlink \
444                       --enable-vsllink \
445                       --enable-rlink \
446                       --enable-arm-jtag-ew \
447                       --enable-buspirate
448     log "Building openocd-${OOCD}"
449     make ${MAKEFLAGS}
450     install openocd-${OOCD} install
451     cd ..
452     log "Cleaning up openocd-${OOCD}"
453     touch ${STAMPS}/openocd-${OOCD}.build
454     rm -rf build/* ${OOCD}
455 fi
456 fi
457
458 if [ ${LIBSTM32_EN} != 0 ]; then
459 if [ ! -e ${STAMPS}/libcmsis-${LIBCMSIS}.build ]; then
460     unpack libcmsis-${LIBCMSIS}
461     cd libcmsis-${LIBCMSIS}
462     log "Building libcmsis-${LIBCMSIS}"
463     make arch_prefix=${TARGET} prefix=${PREFIX}
464     install libcmsis-${LIBCMSIS} arch_prefix=${TARGET} prefix=${PREFIX} install
465     cd ..
466     log "Cleaning up libcmsis-${LIBCMSIS}"
467     touch ${STAMPS}/libcmsis-${LIBCMSIS}.build
468     rm -rf libcmsis-${LIBCMSIS}
469 fi
470
471 if [ ! -e ${STAMPS}/libstm32-${LIBSTM32}.build ]; then
472     unpack libstm32-${LIBSTM32}
473     cd libstm32-${LIBSTM32}
474     log "Building libstm32-${LIBSTM32}"
475     make arch_prefix=${TARGET} prefix=${PREFIX}
476     install libstm32-${LIBSTM32} arch_prefix=${TARGET} prefix=${PREFIX} install
477     cd ..
478     log "Cleaning up libstm32-${LIBSTM32}"
479     touch ${STAMPS}/libstm32-${LIBSTM32}.build
480     rm -rf libstm32-${LIBSTM32}
481 fi
482
483 if [ ! -e ${STAMPS}/libstm32usb-${LIBSTM32USB}.build ]; then
484     unpack libstm32usb-${LIBSTM32USB}
485     cd libstm32usb-${LIBSTM32USB}
486     log "Building libstm32usb-${LIBSTM32USB}"
487     make arch_prefix=${TARGET} prefix=${PREFIX}
488     install libstm32usb-${LIBSTM32USB} arch_prefix=${TARGET} prefix=${PREFIX} install
489     cd ..
490     log "Cleaning up libstm32usb-${LIBSTM32USB}"
491     touch ${STAMPS}/libstm32usb-${LIBSTM32USB}.build
492     rm -rf libstm32usb-${LIBSTM32USB}
493 fi
494 fi
495
496 if [ $LIBOPENSTM32_EN != 0 ]; then
497 if [ ! -e ${STAMPS}/libopenstm32-${LIBOPENSTM32}.build ]; then
498     unpack libopenstm32-${LIBOPENSTM32}
499     cd libopenstm32-${LIBOPENSTM32}
500     log "Building libopenstm32-${LIBOPENSTM32}"
501     make PREFIX=${TARGET} DESTDIR=${PREFIX}
502     install libopenstm32-${LIBOPENSTM32} PREFIX=${TARGET} DESTDIR=${PREFIX} install
503     cd ..
504     log "Cleaning up libopenstm32-${LIBOPENSTM32}"
505     touch ${STAMPS}/libopenstm32-${LIBOPENSTM32}.build
506     rm -rf libopenstm32-${LIBOPENSTM32}
507 fi
508 fi
509
510 if [ ${SUMMON_DIR_CREATED} = 1 ]; then
511     log "Removing work directory"
512     rm -rf ${SUMMON_DIR}
513 fi