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