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