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