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