Not using -a option for tar anymore that was cousing too many problems.
[fw/cortex-toolchain] / 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-none-eabi            # Or: TARGET=arm-elf
22 PREFIX=${HOME}/sat      # 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=
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=0
31 # Set to 1 to build libstm32 provided by ST
32 LIBSTM32_EN=0
33 # Set to 1 to build libopenstm32 an open source library for stm32
34 LIBOPENSTM32_EN=0
35 # Make the gcc default to Cortex-M3
36 DEFAULT_TO_CORTEX_M3=0
37
38 ##############################################################################
39 # Version and download url settings section
40 ##############################################################################
41 if [ ${USE_LINARO} == 0 ] ; then
42         # For FSF GCC:
43         GCCVERSION=4.5.1
44         GCC=gcc-${GCCVERSION}
45         GCCURL=http://ftp.gnu.org/gnu/gcc/${GCC}/${GCC}.tar.gz
46 else
47         # For the Linaro GCC:
48         GCCVERSION=4.5-2010.08-1
49         GCC=gcc-linaro-${GCCVERSION}
50         GCCURL=http://launchpad.net/gcc-linaro/4.5/${GCCVERSION}/+download/${GCC}.tar.gz
51 fi
52
53 BINUTILS=binutils-2.20
54 NEWLIB=newlib-1.18.0
55 GDB=gdb-7.2
56 LIBCMSIS=v1.10-2
57 LIBSTM32=v3.0.0-1
58 LIBSTM32USB=v3.0.1-1
59 LIBOPENSTM32=master
60
61 ##############################################################################
62 # Flags section
63 ##############################################################################
64
65 GDBFLAGS=
66 BINUTILFLAGS=
67
68 if [ ${DEFAULT_TO_CORTEX_M3} == 0 ] ; then
69         GCCFLAGS=
70 else
71         # To default to the Cortex-M3:
72         GCCFLAGS="--with-arch=armv7-m --with-mode=thumb"
73 fi
74
75 # Pull in the local configuration, if any
76 if [ -f local.sh ]; then
77     . ./local.sh
78 fi
79
80 MAKEFLAGS=${PARALLEL}
81 TARFLAGS=v
82
83 if [ ${QUIET} != 0 ]; then
84     TARFLAGS=
85     MAKEFLAGS="${MAKEFLAGS} -s"
86 fi
87
88 export PATH="${PREFIX}/bin:${PATH}"
89
90 SUMMON_DIR=$(pwd)
91 SOURCES=${SUMMON_DIR}/sources
92 STAMPS=${SUMMON_DIR}/stamps
93
94
95 ##############################################################################
96 # Tool section
97 ##############################################################################
98 TAR=tar
99
100 ##############################################################################
101 # OS and Tooldetection section
102 # Detects which tools and flags to use
103 ##############################################################################
104
105 case "$(uname)" in
106         Linux)
107         echo "Found Linux OS."
108         ;;
109         Darwin)
110         echo "Found Darwin OS."
111         GCCFLAGS="${GCCFLAGS} \
112                   --with-gmp=${DARWIN_OPT_PATH} \
113                   --with-mpfr=${DARWIN_OPT_PATH} \
114                   --with-mpc=${DARWIN_OPT_PATH} \
115                   -with-libiconv-prefix=${DARWIN_OPT_PATH}"
116         ;;
117         *)
118         echo "Found unknown OS. Aborting!"
119         exit 1
120         ;;
121 esac
122
123 ##############################################################################
124 # Building section
125 # You probably don't have to touch anything after this
126 ##############################################################################
127
128 # Fetch a versioned file from a URL
129 function fetch {
130     if [ ! -e ${STAMPS}/$1.fetch ]; then
131         log "Downloading $1 sources..."
132         wget -c --no-passive-ftp $2
133         touch ${STAMPS}/$1.fetch
134     fi
135 }
136
137 # Log a message out to the console
138 function log {
139     echo "******************************************************************"
140     echo "* $*"
141     echo "******************************************************************"
142 }
143
144 # Unpack an archive
145 function unpack {
146     log Unpacking $*
147     # Use 'auto' mode decompression.  Replace with a switch if tar doesn't support -a
148     ARCHIVE=$(ls ${SOURCES}/$1.tar.*)
149     case ${ARCHIVE} in
150         *.bz2)
151             echo "archive type bz2"
152             TYPE=j
153             ;;
154         *.gz)
155             echo "archive type gz"
156             TYPE=z
157             ;;
158         *)
159             echo "Unknown archive type of $1"
160             echo ${ARCHIVE}
161             exit 1
162             ;;
163     esac
164     ${TAR} xf${TYPE}${TARFLAGS} ${SOURCES}/$1.tar.*
165 }
166
167 # Install a build
168 function install {
169     log $1
170     ${SUDO} make ${MAKEFLAGS} $2 $3 $4 $5 $6 $7 $8
171 }
172
173
174 mkdir -p ${STAMPS} ${SOURCES}
175
176 cd ${SOURCES}
177
178 fetch ${BINUTILS} http://ftp.gnu.org/gnu/binutils/${BINUTILS}.tar.bz2
179 fetch ${GCC} ${GCCURL}
180 fetch ${NEWLIB} ftp://sources.redhat.com/pub/newlib/${NEWLIB}.tar.gz
181 fetch ${GDB} http://ftp.gnu.org/gnu/gdb/${GDB}.tar.bz2
182
183 if [ ${LIBSTM32_EN} != 0 ]; then
184 if [ ! -e libcmsis-${LIBCMSIS}.tar.bz2 ]; then
185         log "Cloning libcmsis sources..."
186         git clone git://git.open-bldc.org/libcmsis.git
187         cd libcmsis
188         git archive --format=tar --prefix=libcmsis-${LIBCMSIS}/ ${LIBCMSIS} | \
189             bzip2 --stdout > ../libcmsis-${LIBCMSIS}.tar.bz2
190         cd ..
191         rm -rf libcmsis
192 fi
193
194 if [ ! -e libstm32-${LIBSTM32}.tar.bz2 ]; then
195         log "Cloning libstm32 sources..."
196         git clone git://git.open-bldc.org/libstm32.git
197         cd libstm32
198         git archive --format=tar --prefix=libstm32-${LIBSTM32}/ ${LIBSTM32} | \
199             bzip2 --stdout > ../libstm32-${LIBSTM32}.tar.bz2
200         cd ..
201         rm -rf libstm32
202 fi
203
204 if [ ! -e libstm32usb-${LIBSTM32USB}.tar.bz2 ]; then
205         log "Cloning libstm32usb sources..."
206         git clone git://git.open-bldc.org/libstm32usb.git
207         cd libstm32usb
208         git archive --format=tar --prefix=libstm32usb-${LIBSTM32USB}/ ${LIBSTM32USB} | \
209             bzip2 --stdout > ../libstm32usb-${LIBSTM32USB}.tar.bz2
210         cd ..
211         rm -rf libstm32usb
212 fi
213 fi
214
215 if [ ${LIBOPENSTM32_EN} != 0 ]; then
216 if [ ! -e libopenstm32-${LIBOPENSTM32}.tar.bz2 ]; then
217         log "Cloning libopenstm32 sources..."
218         git clone git://libopenstm32.git.sourceforge.net/gitroot/libopenstm32/libopenstm32
219         cd libopenstm32
220         git archive --format=tar --prefix=libopenstm32-${LIBOPENSTM32}/ ${LIBOPENSTM32} | \
221             bzip2 --stdout > ../libopenstm32-${LIBOPENSTM32}.tar.bz2
222         cd ..
223         rm -rf libopenstm32
224 fi
225 fi
226
227 cd ${SUMMON_DIR}
228
229 if [ ! -e build ]; then
230     mkdir build
231 fi
232
233 if [ ! -e ${STAMPS}/${BINUTILS}.build ]; then
234     unpack ${BINUTILS}
235     cd build
236     log "Configuring ${BINUTILS}"
237     ../${BINUTILS}/configure --target=${TARGET} \
238                            --prefix=${PREFIX} \
239                            --enable-interwork \
240                            --enable-multilib \
241                            --with-gnu-as \
242                            --with-gnu-ld \
243                            --disable-nls \
244                            --disable-werror \
245                            ${BINUTILFLAGS}
246     log "Building ${BINUTILS}"
247     make ${MAKEFLAGS}
248     install ${BINUTILS} install
249     cd ..
250     log "Cleaning up ${BINUTILS}"
251     touch ${STAMPS}/${BINUTILS}.build
252     rm -rf build/* ${BINUTILS}
253 fi
254
255 if [ ! -e ${STAMPS}/${GCC}-boot.build ]; then
256     unpack ${GCC} boot
257     cd build
258     log "Configuring ${GCC}-boot"
259     ../${GCC}/configure --target=${TARGET} \
260                       --prefix=${PREFIX} \
261                       --enable-interwork \
262                       --enable-multilib \
263                       --enable-languages="c" \
264                       --with-newlib \
265                       --without-headers \
266                       --disable-shared \
267                       --with-gnu-as \
268                       --with-gnu-ld \
269                       --disable-nls \
270                       --disable-werror \
271                       --with-system-zlib \
272                       ${GCCFLAGS}
273     log "Building ${GCC}-boot"
274     make ${MAKEFLAGS} all-gcc
275     install ${GCC}-boot install-gcc
276     cd ..
277     log "Cleaning up ${GCC}-boot"
278     touch ${STAMPS}/${GCC}-boot.build
279     rm -rf build/* ${GCC}
280 fi
281
282 if [ ! -e ${STAMPS}/${NEWLIB}.build ]; then
283     unpack ${NEWLIB}
284     cd build
285     log "Configuring ${NEWLIB}"
286     ../${NEWLIB}/configure --target=${TARGET} \
287                          --prefix=${PREFIX} \
288                          --enable-interwork \
289                          --enable-multilib \
290                          --with-gnu-as \
291                          --with-gnu-ld \
292                          --disable-nls \
293                          --disable-werror \
294                          --disable-newlib-supplied-syscalls
295     log "Building ${NEWLIB}"
296     make ${MAKEFLAGS}
297     install ${NEWLIB} install
298     cd ..
299     log "Cleaning up ${NEWLIB}"
300     touch ${STAMPS}/${NEWLIB}.build
301     rm -rf build/* ${NEWLIB}
302 fi
303
304 # Yes, you need to build gcc again!
305 if [ ! -e ${STAMPS}/${GCC}.build ]; then
306     unpack ${GCC}
307     cd build
308     log "Configuring ${GCC}"
309     ../${GCC}/configure --target=${TARGET} \
310                       --prefix=${PREFIX} \
311                       --enable-interwork \
312                       --enable-multilib \
313                       --enable-languages="c,c++" \
314                       --with-newlib \
315                       --disable-shared \
316                       --with-gnu-as \
317                       --with-gnu-ld \
318                       --disable-nls \
319                       --disable-werror \
320                       --with-system-zlib \
321                      ${GCCFLAGS}
322     log "Building ${GCC}"
323     make ${MAKEFLAGS}
324     install ${GCC} install
325     cd ..
326     log "Cleaning up ${GCC}"
327     touch ${STAMPS}/${GCC}.build
328     rm -rf build/* ${GCC}
329 fi
330
331 if [ ! -e ${STAMPS}/${GDB}.build ]; then
332     unpack ${GDB}
333     cd build
334     log "Configuring ${GDB}"
335     ../${GDB}/configure --target=${TARGET} \
336                       --prefix=${PREFIX} \
337                       --enable-interwork \
338                       --enable-multilib \
339                       --disable-werror \
340                       ${GDBFLAGS}
341     log "Building ${GDB}"
342     make ${MAKEFLAGS}
343     install ${GDB} install
344     cd ..
345     log "Cleaning up ${GDB}"
346     touch ${STAMPS}/${GDB}.build
347     rm -rf build/* ${GDB}
348 fi
349
350 if [ ${LIBSTM32_EN} != 0 ]; then
351 if [ ! -e .libcmsis-${LIBCMSIS}.build ]; then
352     unpack libcmsis-${LIBCMSIS}
353     cd libcmsis-${LIBCMSIS}
354     log "Building libcmsis-${LIBCMSIS}"
355     make arch_prefix=${TARGET} prefix=${PREFIX}
356     install libcmsis-${LIBCMSIS} arch_prefix=${TARGET} prefix=${PREFIX} install
357     cd ..
358     log "Cleaning up libcmsis-${LIBCMSIS}"
359     touch .libcmsis-${LIBCMSIS}.build
360     rm -rf libcmsis-${LIBCMSIS}
361 fi
362
363 if [ ! -e .libstm32-${LIBSTM32}.build ]; then
364     unpack libstm32-${LIBSTM32}
365     cd libstm32-${LIBSTM32}
366     log "Building libstm32-${LIBSTM32}"
367     make arch_prefix=${TARGET} prefix=${PREFIX}
368     install libstm32-${LIBSTM32} arch_prefix=${TARGET} prefix=${PREFIX} install
369     cd ..
370     log "Cleaning up libstm32-${LIBSTM32}"
371     touch .libstm32-${LIBSTM32}.build
372     rm -rf libstm32-${LIBSTM32}
373 fi
374
375 if [ ! -e .libstm32usb-${LIBSTM32USB}.build ]; then
376     unpack libstm32usb-${LIBSTM32USB}
377     cd libstm32usb-${LIBSTM32USB}
378     log "Building libstm32usb-${LIBSTM32USB}"
379     make arch_prefix=${TARGET} prefix=${PREFIX}
380     install libstm32usb-${LIBSTM32USB} arch_prefix=${TARGET} prefix=${PREFIX} install
381     cd ..
382     log "Cleaning up libstm32usb-${LIBSTM32USB}"
383     touch .libstm32usb-${LIBSTM32USB}.build
384     rm -rf libstm32usb-${LIBSTM32USB}
385 fi
386 fi
387
388 if [ $LIBOPENSTM32_EN != 0 ]; then
389     unpack libopenstm32-${LIBOPENSTM32}
390     cd libopenstm32-${LIBOPENSTM32}
391     log "Building libopenstm32-${LIBOPENSTM32}"
392     make PREFIX=${TARGET} DESTDIR=${PREFIX}
393     install libopenstm32-${LIBOPENSTM32} PREFIX=${TARGET} DESTDIR=${PREFIX} install
394     cd ..
395     log "Cleaning up libopenstm32-${LIBOPENSTM32}"
396     touch .libopenstm32-${LIBOPENSTM32}.build
397     rm -rf libopenstm32-${LIBOPENSTM32}
398 fi