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