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