Added --use-system-zlib to fix compilation on debian unstable.
[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-none-eabi            # Or: TARGET=arm-elf
22 PREFIX=${HOME}/arm-none-eabi    # 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 # Building section
96 # You probably don't have to touch anything after this
97 ##############################################################################
98
99 # Fetch a versioned file from a URL
100 function fetch {
101     if [ ! -e ${STAMPS}/$1.fetch ]; then
102         log "Downloading $1 sources..."
103         wget -c --no-passive-ftp $2
104         touch ${STAMPS}/$1.fetch
105     fi
106 }
107
108 # Log a message out to the console
109 function log {
110     echo "******************************************************************"
111     echo "* $*"
112     echo "******************************************************************"
113 }
114
115 # Unpack an archive
116 function unpack {
117     log Unpacking $*
118     # Use 'auto' mode decompression.  Replace with a switch if tar doesn't support -a
119     tar xaf${TARFLAGS} ${SOURCES}/$1.tar.*
120 }
121
122 # Install a build
123 function install {
124     log $1
125     ${SUDO} make ${MAKEFLAGS} $2 $3 $4 $5 $6 $7 $8
126 }
127
128 case "$(uname)" in
129         Linux)
130         echo "Found Linux OS."
131         ;;
132         Darwin)
133         echo "Found Darwin OS."
134         GCCFLAGS="${GCCFLAGS} \
135                   --with-gmp=${DARWIN_OPT_PATH} \
136                   --with-mpfr=${DARWIN_OPT_PATH} \
137                   --with-mpc=${DARWIN_OPT_PATH} \
138                   -with-libiconv-prefix=${DARWIN_OPT_PATH}"
139         ;;
140         *)
141         echo "Found unknown OS. Aborting!"
142         exit 1
143         ;;
144 esac
145
146 mkdir -p ${STAMPS} ${SOURCES}
147
148 cd ${SOURCES}
149
150 fetch ${BINUTILS} http://ftp.gnu.org/gnu/binutils/${BINUTILS}.tar.bz2
151 fetch ${GCC} ${GCCURL}
152 fetch ${NEWLIB} ftp://sources.redhat.com/pub/newlib/${NEWLIB}.tar.gz
153 fetch ${GDB} http://ftp.gnu.org/gnu/gdb/${GDB}.tar.bz2
154
155 if [ ${LIBSTM32_EN} != 0 ]; then
156 if [ ! -e libcmsis-${LIBCMSIS}.tar.bz2 ]; then
157         log "Cloning libcmsis sources..."
158         git clone git://git.open-bldc.org/libcmsis.git
159         cd libcmsis
160         git archive --format=tar --prefix=libcmsis-${LIBCMSIS}/ ${LIBCMSIS} | \
161             bzip2 --stdout > ../libcmsis-${LIBCMSIS}.tar.bz2
162         cd ..
163         rm -rf libcmsis
164 fi
165
166 if [ ! -e libstm32-${LIBSTM32}.tar.bz2 ]; then
167         log "Cloning libstm32 sources..."
168         git clone git://git.open-bldc.org/libstm32.git
169         cd libstm32
170         git archive --format=tar --prefix=libstm32-${LIBSTM32}/ ${LIBSTM32} | \
171             bzip2 --stdout > ../libstm32-${LIBSTM32}.tar.bz2
172         cd ..
173         rm -rf libstm32
174 fi
175
176 if [ ! -e libstm32usb-${LIBSTM32USB}.tar.bz2 ]; then
177         log "Cloning libstm32usb sources..."
178         git clone git://git.open-bldc.org/libstm32usb.git
179         cd libstm32usb
180         git archive --format=tar --prefix=libstm32usb-${LIBSTM32USB}/ ${LIBSTM32USB} | \
181             bzip2 --stdout > ../libstm32usb-${LIBSTM32USB}.tar.bz2
182         cd ..
183         rm -rf libstm32usb
184 fi
185 fi
186
187 if [ ${LIBOPENSTM32_EN} != 0 ]; then
188 if [ ! -e libopenstm32-${LIBOPENSTM32}.tar.bz2 ]; then
189         log "Cloning libopenstm32 sources..."
190         git clone git://libopenstm32.git.sourceforge.net/gitroot/libopenstm32/libopenstm32
191         cd libopenstm32
192         git archive --format=tar --prefix=libopenstm32-${LIBOPENSTM32}/ ${LIBOPENSTM32} | \
193             bzip2 --stdout > ../libopenstm32-${LIBOPENSTM32}.tar.bz2
194         cd ..
195         rm -rf libopenstm32
196 fi
197 fi
198
199 cd ${SUMMON_DIR}
200
201 if [ ! -e build ]; then
202     mkdir build
203 fi
204
205 if [ ! -e ${STAMPS}/${BINUTILS}.build ]; then
206     unpack ${BINUTILS}
207     cd build
208     log "Configuring ${BINUTILS}"
209     ../${BINUTILS}/configure --target=${TARGET} \
210                            --prefix=${PREFIX} \
211                            --enable-interwork \
212                            --enable-multilib \
213                            --with-gnu-as \
214                            --with-gnu-ld \
215                            --disable-nls \
216                            --disable-werror \
217                            ${BINUTILFLAGS}
218     log "Building ${BINUTILS}"
219     make ${MAKEFLAGS}
220     install ${BINUTILS} install
221     cd ..
222     log "Cleaning up ${BINUTILS}"
223     touch ${STAMPS}/${BINUTILS}.build
224     rm -rf build/* ${BINUTILS}
225 fi
226
227 if [ ! -e ${STAMPS}/${GCC}-boot.build ]; then
228     unpack ${GCC} boot
229     cd build
230     log "Configuring ${GCC}-boot"
231     ../${GCC}/configure --target=${TARGET} \
232                       --prefix=${PREFIX} \
233                       --enable-interwork \
234                       --enable-multilib \
235                       --enable-languages="c" \
236                       --with-newlib \
237                       --without-headers \
238                       --disable-shared \
239                       --with-gnu-as \
240                       --with-gnu-ld \
241                       --disable-nls \
242                       --disable-werror \
243                       --with-system-zlib \
244                       ${GCCFLAGS}
245     log "Building ${GCC}-boot"
246     make ${MAKEFLAGS} all-gcc
247     install ${GCC}-boot install-gcc
248     cd ..
249     log "Cleaning up ${GCC}-boot"
250     touch ${STAMPS}/${GCC}-boot.build
251     rm -rf build/* ${GCC}
252 fi
253
254 if [ ! -e ${STAMPS}/${NEWLIB}.build ]; then
255     unpack ${NEWLIB}
256     cd build
257     log "Configuring ${NEWLIB}"
258     ../${NEWLIB}/configure --target=${TARGET} \
259                          --prefix=${PREFIX} \
260                          --enable-interwork \
261                          --enable-multilib \
262                          --with-gnu-as \
263                          --with-gnu-ld \
264                          --disable-nls \
265                          --disable-werror \
266                          --disable-newlib-supplied-syscalls
267     log "Building ${NEWLIB}"
268     make ${MAKEFLAGS}
269     install ${NEWLIB} install
270     cd ..
271     log "Cleaning up ${NEWLIB}"
272     touch ${STAMPS}/${NEWLIB}.build
273     rm -rf build/* ${NEWLIB}
274 fi
275
276 # Yes, you need to build gcc again!
277 if [ ! -e ${STAMPS}/${GCC}.build ]; then
278     unpack ${GCC}
279     cd build
280     log "Configuring ${GCC}"
281     ../${GCC}/configure --target=${TARGET} \
282                       --prefix=${PREFIX} \
283                       --enable-interwork \
284                       --enable-multilib \
285                       --enable-languages="c,c++" \
286                       --with-newlib \
287                       --disable-shared \
288                       --with-gnu-as \
289                       --with-gnu-ld \
290                       --disable-nls \
291                       --disable-werror \
292                       --with-system-zlib \
293                      ${GCCFLAGS}
294     log "Building ${GCC}"
295     make ${MAKEFLAGS}
296     install ${GCC} install
297     cd ..
298     log "Cleaning up ${GCC}"
299     touch ${STAMPS}/${GCC}.build
300     rm -rf build/* ${GCC}
301 fi
302
303 if [ ! -e ${STAMPS}/${GDB}.build ]; then
304     unpack ${GDB}
305     cd build
306     log "Configuring ${GDB}"
307     ../${GDB}/configure --target=${TARGET} \
308                       --prefix=${PREFIX} \
309                       --enable-interwork \
310                       --enable-multilib \
311                       --disable-werror \
312                       ${GDBFLAGS}
313     log "Building ${GDB}"
314     make ${MAKEFLAGS}
315     install ${GDB} install
316     cd ..
317     log "Cleaning up ${GDB}"
318     touch ${STAMPS}/${GDB}.build
319     rm -rf build/* ${GDB}
320 fi
321
322 if [ ${LIBSTM32_EN} != 0 ]; then
323 if [ ! -e .libcmsis-${LIBCMSIS}.build ]; then
324     unpack libcmsis-${LIBCMSIS}
325     cd libcmsis-${LIBCMSIS}
326     log "Building libcmsis-${LIBCMSIS}"
327     make arch_prefix=${TARGET} prefix=${PREFIX}
328     install libcmsis-${LIBCMSIS} arch_prefix=${TARGET} prefix=${PREFIX} install
329     cd ..
330     log "Cleaning up libcmsis-${LIBCMSIS}"
331     touch .libcmsis-${LIBCMSIS}.build
332     rm -rf libcmsis-${LIBCMSIS}
333 fi
334
335 if [ ! -e .libstm32-${LIBSTM32}.build ]; then
336     unpack libstm32-${LIBSTM32}
337     cd libstm32-${LIBSTM32}
338     log "Building libstm32-${LIBSTM32}"
339     make arch_prefix=${TARGET} prefix=${PREFIX}
340     install libstm32-${LIBSTM32} arch_prefix=${TARGET} prefix=${PREFIX} install
341     cd ..
342     log "Cleaning up libstm32-${LIBSTM32}"
343     touch .libstm32-${LIBSTM32}.build
344     rm -rf libstm32-${LIBSTM32}
345 fi
346
347 if [ ! -e .libstm32usb-${LIBSTM32USB}.build ]; then
348     unpack libstm32usb-${LIBSTM32USB}
349     cd libstm32usb-${LIBSTM32USB}
350     log "Building libstm32usb-${LIBSTM32USB}"
351     make arch_prefix=${TARGET} prefix=${PREFIX}
352     install libstm32usb-${LIBSTM32USB} arch_prefix=${TARGET} prefix=${PREFIX} install
353     cd ..
354     log "Cleaning up libstm32usb-${LIBSTM32USB}"
355     touch .libstm32usb-${LIBSTM32USB}.build
356     rm -rf libstm32usb-${LIBSTM32USB}
357 fi
358 fi
359
360 if [ $LIBOPENSTM32_EN != 0 ]; then
361     unpack libopenstm32-${LIBOPENSTM32}
362     cd libopenstm32-${LIBOPENSTM32}
363     log "Building libopenstm32-${LIBOPENSTM32}"
364     make PREFIX=${TARGET} DESTDIR=${PREFIX}
365     install libopenstm32-${LIBOPENSTM32} PREFIX=${TARGET} DESTDIR=${PREFIX} install
366     cd ..
367     log "Cleaning up libopenstm32-${LIBOPENSTM32}"
368     touch .libopenstm32-${LIBOPENSTM32}.build
369     rm -rf libopenstm32-${LIBOPENSTM32}
370 fi