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