Shifted some variables about to allow a better override from local.sh
[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 TARGET=arm-none-eabi            # Or: TARGET=arm-elf
18 PREFIX=${HOME}/arm-none-eabi    # Install location of your final toolchain
19 PARALLEL=                       # Or: PARALLEL="-j 5" for 4 CPUs
20 DARWIN_OPT_PATH=/opt/local      # Path in which MacPorts or Fink is installed
21 # Set to 'sudo' if you need superuser privileges while installing
22 SUDO=
23 # Set to 1 to be quieter while running
24 QUIET=0
25
26 # For FSF GCC:
27 GCCVERSION=4.5.1
28 GCC=gcc-${GCCVERSION}
29 GCCURL=http://ftp.gnu.org/gnu/gcc/${GCC}/${GCC}.tar.gz
30
31 # For the Linaro GCC:
32 # GCCVERSION=4.5-2010.08-1
33 # GCC=gcc-linaro-${GCCVERSION}
34 # GCCURL=http://launchpad.net/gcc-linaro/4.5/${GCCVERSION}/+download/${GCC}.tar.gz
35
36 BINUTILS=binutils-2.20
37 NEWLIB=newlib-1.18.0
38 GDB=gdb-7.2
39 LIBCMSIS=v1.10-2
40 LIBSTM32=v3.0.0-1
41 LIBSTM32USB=v3.0.1-1
42 LIBOPENSTM32=master
43 LIBSTM32_EN=0
44 LIBOPENSTM32_EN=0
45
46 GCCFLAGS=
47 GDBFLAGS=
48 BINUTILFLAGS=
49
50 # To default to the Cortex-M3:
51 # GCCFLAGS="--with-arch=armv7-m --with-mode=thumb"
52
53 # Pull in the local configuration, if any
54 if [ -f local.sh ]; then
55     . ./local.sh
56 fi
57
58 MAKEFLAGS=${PARALLEL}
59 TARFLAGS=v
60
61 if [ ${QUIET} != 0 ]; then
62     TARFLAGS=
63     MAKEFLAGS="${MAKEFLAGS} -s"
64 fi
65
66 export PATH="${PREFIX}/bin:${PATH}"
67
68 SUMMON_DIR=$(pwd)
69 SOURCES=${SUMMON_DIR}/sources
70 STAMPS=${SUMMON_DIR}/stamps
71
72 # Fetch a versioned file from a URL
73 function fetch {
74     if [ ! -e ${STAMPS}/$1.fetch ]; then
75         log "Downloading $1 sources..."
76         wget -c --no-passive-ftp $2
77         touch ${STAMPS}/$1.fetch
78     fi
79 }
80
81 # Log a message out to the console
82 function log {
83     echo "******************************************************************"
84     echo "* $*"
85     echo "******************************************************************"
86 }
87
88 # Unpack an archive
89 function unpack {
90     log Unpacking $*
91     # Use 'auto' mode decompression.  Replace with a switch if tar doesn't support -a
92     tar xaf${TARFLAGS} ${SOURCES}/$1.tar.*
93 }
94
95 # Install a build
96 function install {
97     log $1
98     ${SUDO} make ${MAKEFLAGS} $2 $3 $4 $5 $6 $7 $8
99 }
100
101 case "$(uname)" in
102         Linux)
103         echo "Found Linux OS."
104         ;;
105         Darwin)
106         echo "Found Darwin OS."
107         GCCFLAGS="${GCCFLAGS} \
108                   --with-gmp=${DARWIN_OPT_PATH} \
109                   --with-mpfr=${DARWIN_OPT_PATH} \
110                   --with-mpc=${DARWIN_OPT_PATH} \
111                   -with-libiconv-prefix=${DARWIN_OPT_PATH}"
112         ;;
113         *)
114         echo "Found unknown OS. Aborting!"
115         exit 1
116         ;;
117 esac
118
119 mkdir -p ${STAMPS} ${SOURCES}
120
121 cd ${SOURCES}
122
123 fetch ${BINUTILS} http://ftp.gnu.org/gnu/binutils/${BINUTILS}.tar.bz2
124 fetch ${GCC} ${GCCURL}
125 fetch ${NEWLIB} ftp://sources.redhat.com/pub/newlib/${NEWLIB}.tar.gz
126 fetch ${GDB} http://ftp.gnu.org/gnu/gdb/${GDB}.tar.bz2
127
128 if [ ${LIBSTM32_EN} != 0 ]; then
129 if [ ! -e libcmsis-${LIBCMSIS}.tar.bz2 ]; then
130         log "Cloning libcmsis sources..."
131         git clone git://git.open-bldc.org/libcmsis.git
132         cd libcmsis
133         git archive --format=tar --prefix=libcmsis-${LIBCMSIS}/ ${LIBCMSIS} | \
134             bzip2 --stdout > ../libcmsis-${LIBCMSIS}.tar.bz2
135         cd ..
136         rm -rf libcmsis
137 fi
138
139 if [ ! -e libstm32-${LIBSTM32}.tar.bz2 ]; then
140         log "Cloning libstm32 sources..."
141         git clone git://git.open-bldc.org/libstm32.git
142         cd libstm32
143         git archive --format=tar --prefix=libstm32-${LIBSTM32}/ ${LIBSTM32} | \
144             bzip2 --stdout > ../libstm32-${LIBSTM32}.tar.bz2
145         cd ..
146         rm -rf libstm32
147 fi
148
149 if [ ! -e libstm32usb-${LIBSTM32USB}.tar.bz2 ]; then
150         log "Cloning libstm32usb sources..."
151         git clone git://git.open-bldc.org/libstm32usb.git
152         cd libstm32usb
153         git archive --format=tar --prefix=libstm32usb-${LIBSTM32USB}/ ${LIBSTM32USB} | \
154             bzip2 --stdout > ../libstm32usb-${LIBSTM32USB}.tar.bz2
155         cd ..
156         rm -rf libstm32usb
157 fi
158 fi
159
160 if [ ${LIBOPENSTM32_EN} != 0 ]; then
161 if [ ! -e libopenstm32-${LIBOPENSTM32}.tar.bz2 ]; then
162         log "Cloning libopenstm32 sources..."
163         git clone git://libopenstm32.git.sourceforge.net/gitroot/libopenstm32/libopenstm32
164         cd libopenstm32
165         git archive --format=tar --prefix=libopenstm32-${LIBOPENSTM32}/ ${LIBOPENSTM32} | \
166             bzip2 --stdout > ../libopenstm32-${LIBOPENSTM32}.tar.bz2
167         cd ..
168         rm -rf libopenstm32
169 fi
170 fi
171
172 cd ${SUMMON_DIR}
173
174 if [ ! -e build ]; then
175     mkdir build
176 fi
177
178 if [ ! -e ${STAMPS}/${BINUTILS}.build ]; then
179     unpack ${BINUTILS}
180     cd build
181     log "Configuring ${BINUTILS}"
182     ../${BINUTILS}/configure --target=${TARGET} \
183                            --prefix=${PREFIX} \
184                            --enable-interwork \
185                            --enable-multilib \
186                            --with-gnu-as \
187                            --with-gnu-ld \
188                            --disable-nls \
189                            --disable-werror \
190                            ${BINUTILFLAGS}
191     log "Building ${BINUTILS}"
192     make ${MAKEFLAGS}
193     install ${BINUTILS} install
194     cd ..
195     log "Cleaning up ${BINUTILS}"
196     touch ${STAMPS}/${BINUTILS}.build
197     rm -rf build/* ${BINUTILS}
198 fi
199
200 if [ ! -e ${STAMPS}/${GCC}-boot.build ]; then
201     unpack ${GCC} boot
202     cd build
203     log "Configuring ${GCC}-boot"
204     ../${GCC}/configure --target=${TARGET} \
205                       --prefix=${PREFIX} \
206                       --enable-interwork \
207                       --enable-multilib \
208                       --enable-languages="c" \
209                       --with-newlib \
210                       --without-headers \
211                       --disable-shared \
212                       --with-gnu-as \
213                       --with-gnu-ld \
214                       --disable-nls \
215                       --disable-werror \
216                       ${GCCFLAGS}
217     log "Building ${GCC}-boot"
218     make ${MAKEFLAGS} all-gcc
219     install ${GCC}-boot install-gcc
220     cd ..
221     log "Cleaning up ${GCC}-boot"
222     touch ${STAMPS}/${GCC}-boot.build
223     rm -rf build/* ${GCC}
224 fi
225
226 if [ ! -e ${STAMPS}/${NEWLIB}.build ]; then
227     unpack ${NEWLIB}
228     cd build
229     log "Configuring ${NEWLIB}"
230     ../${NEWLIB}/configure --target=${TARGET} \
231                          --prefix=${PREFIX} \
232                          --enable-interwork \
233                          --enable-multilib \
234                          --with-gnu-as \
235                          --with-gnu-ld \
236                          --disable-nls \
237                          --disable-werror \
238                          --disable-newlib-supplied-syscalls
239     log "Building ${NEWLIB}"
240     make ${MAKEFLAGS}
241     install ${NEWLIB} install
242     cd ..
243     log "Cleaning up ${NEWLIB}"
244     touch ${STAMPS}/${NEWLIB}.build
245     rm -rf build/* ${NEWLIB}
246 fi
247
248 # Yes, you need to build gcc again!
249 if [ ! -e ${STAMPS}/${GCC}.build ]; then
250     unpack ${GCC}
251     cd build
252     log "Configuring ${GCC}"
253     ../${GCC}/configure --target=${TARGET} \
254                       --prefix=${PREFIX} \
255                       --enable-interwork \
256                       --enable-multilib \
257                       --enable-languages="c,c++" \
258                       --with-newlib \
259                       --disable-shared \
260                       --with-gnu-as \
261                       --with-gnu-ld \
262                       --disable-nls \
263                       --disable-werror \
264                      ${GCCFLAGS}
265     log "Building ${GCC}"
266     make ${MAKEFLAGS}
267     install ${GCC} install
268     cd ..
269     log "Cleaning up ${GCC}"
270     touch ${STAMPS}/${GCC}.build
271     rm -rf build/* ${GCC}
272 fi
273
274 if [ ! -e ${STAMPS}/${GDB}.build ]; then
275     unpack ${GDB}
276     cd build
277     log "Configuring ${GDB}"
278     ../${GDB}/configure --target=${TARGET} \
279                       --prefix=${PREFIX} \
280                       --enable-interwork \
281                       --enable-multilib \
282                       --disable-werror \
283                       ${GDBFLAGS}
284     log "Building ${GDB}"
285     make ${MAKEFLAGS}
286     install ${GDB} install
287     cd ..
288     log "Cleaning up ${GDB}"
289     touch ${STAMPS}/${GDB}.build
290     rm -rf build/* ${GDB}
291 fi
292
293 if [ ${LIBSTM32_EN} != 0 ]; then
294 if [ ! -e .libcmsis-${LIBCMSIS}.build ]; then
295     unpack libcmsis-${LIBCMSIS}
296     cd libcmsis-${LIBCMSIS}
297     log "Building libcmsis-${LIBCMSIS}"
298     make arch_prefix=${TARGET} prefix=${PREFIX}
299     install libcmsis-${LIBCMSIS} arch_prefix=${TARGET} prefix=${PREFIX} install
300     cd ..
301     log "Cleaning up libcmsis-${LIBCMSIS}"
302     touch .libcmsis-${LIBCMSIS}.build
303     rm -rf libcmsis-${LIBCMSIS}
304 fi
305
306 if [ ! -e .libstm32-${LIBSTM32}.build ]; then
307     unpack libstm32-${LIBSTM32}
308     cd libstm32-${LIBSTM32}
309     log "Building libstm32-${LIBSTM32}"
310     make arch_prefix=${TARGET} prefix=${PREFIX}
311     install libstm32-${LIBSTM32} arch_prefix=${TARGET} prefix=${PREFIX} install
312     cd ..
313     log "Cleaning up libstm32-${LIBSTM32}"
314     touch .libstm32-${LIBSTM32}.build
315     rm -rf libstm32-${LIBSTM32}
316 fi
317
318 if [ ! -e .libstm32usb-${LIBSTM32USB}.build ]; then
319     unpack libstm32usb-${LIBSTM32USB}
320     cd libstm32usb-${LIBSTM32USB}
321     log "Building libstm32usb-${LIBSTM32USB}"
322     make arch_prefix=${TARGET} prefix=${PREFIX}
323     install libstm32usb-${LIBSTM32USB} arch_prefix=${TARGET} prefix=${PREFIX} install
324     cd ..
325     log "Cleaning up libstm32usb-${LIBSTM32USB}"
326     touch .libstm32usb-${LIBSTM32USB}.build
327     rm -rf libstm32usb-${LIBSTM32USB}
328 fi
329 fi
330
331 if [ $LIBOPENSTM32_EN != 0 ]; then
332     unpack libopenstm32-${LIBOPENSTM32}
333     cd libopenstm32-${LIBOPENSTM32}
334     log "Building libopenstm32-${LIBOPENSTM32}"
335     make PREFIX=${TARGET} DESTDIR=${PREFIX}
336     install libopenstm32-${LIBOPENSTM32} PREFIX=${TARGET} DESTDIR=${PREFIX} install
337     cd ..
338     log "Cleaning up libopenstm32-${LIBOPENSTM32}"
339     touch .libopenstm32-${LIBOPENSTM32}.build
340     rm -rf libopenstm32-${LIBOPENSTM32}
341 fi