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