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