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