fixed the MacPorts check
[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-elf          # Or: TARGET=arm-elf
22 PREFIX=/arm     # 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='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=1
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
107         # darwin dependencies
108         DARWINDEPS="gnutar mpfr libmpc gmp"
109   DARWINDEPSCOUNT=4
110
111   GCCFLAGS="${GCCFLAGS} \
112             --with-gmp=${DARWIN_OPT_PATH} \
113                   --with-mpfr=${DARWIN_OPT_PATH} \
114                   --with-mpc=${DARWIN_OPT_PATH} \
115                         -with-libiconv-prefix=${DARWIN_OPT_PATH}"
116
117   echo "Attempting to automatically check if common dependencies are installed on your system"
118   if ! which port > /dev/null; then
119     echo "MacPorts not installed."
120   else
121     pc=`port installed ${DARWINDEPS} | wc -l | tr -d " "`
122     if [ ${pc} -lt $[${DARWINDEPSCOUNT}+1] ];
123     then
124       echo "Something is missing, do you want to install everything needed?"
125       echo "(You need super user rights to do this.)"
126
127       if [ read = "y" || read = "yes" ];
128       then
129         sudo port install ${DARWINDEPS}
130       fi
131     else
132       echo "Found: "${DARWINDEPS}"!"
133     fi
134   fi
135
136   TAR=gnutar
137
138         ;;
139
140         *)
141         echo "Found unknown OS. Aborting!"
142         exit 1
143         ;;
144 esac
145
146 ##############################################################################
147 # Building section
148 # You probably don't have to touch anything after this
149 ##############################################################################
150
151 # Fetch a versioned file from a URL
152 function fetch {
153     if [ ! -e ${STAMPS}/$1.fetch ]; then
154         log "Downloading $1 sources..."
155         wget -c --no-passive-ftp $2
156         touch ${STAMPS}/$1.fetch
157     fi
158 }
159
160 # Log a message out to the console
161 function log {
162     echo "******************************************************************"
163     echo "* $*"
164     echo "******************************************************************"
165 }
166
167 # Unpack an archive
168 function unpack {
169     log Unpacking $*
170     if [ -f ${SOURCES}/$1.tar.gz ];
171     then
172       ${TAR} xfz${TARFLAGS} ${SOURCES}/$1.tar.gz
173     else
174       ${TAR} xfj${TARFLAGS} ${SOURCES}/$1.tar.bz2
175     fi
176 }
177
178 # Install a build
179 function install {
180     log $1
181     ${SUDO} make ${MAKEFLAGS} $2 $3 $4 $5 $6 $7 $8
182 }
183
184
185 mkdir -p ${STAMPS} ${SOURCES}
186
187 cd ${SOURCES}
188
189 fetch ${BINUTILS} http://ftp.gnu.org/gnu/binutils/${BINUTILS}.tar.bz2
190 fetch ${GCC} ${GCCURL}
191 fetch ${NEWLIB} ftp://sources.redhat.com/pub/newlib/${NEWLIB}.tar.gz
192 fetch ${GDB} http://ftp.gnu.org/gnu/gdb/${GDB}.tar.bz2
193
194 if [ ${LIBSTM32_EN} != 0 ]; then
195 if [ ! -e libcmsis-${LIBCMSIS}.tar.bz2 ]; then
196         log "Cloning libcmsis sources..."
197         git clone git://git.open-bldc.org/libcmsis.git
198         cd libcmsis
199         git archive --format=tar --prefix=libcmsis-${LIBCMSIS}/ ${LIBCMSIS} | \
200             bzip2 --stdout > ../libcmsis-${LIBCMSIS}.tar.bz2
201         cd ..
202         rm -rf libcmsis
203 fi
204
205 if [ ! -e libstm32-${LIBSTM32}.tar.bz2 ]; then
206         log "Cloning libstm32 sources..."
207         git clone git://git.open-bldc.org/libstm32.git
208         cd libstm32
209         git archive --format=tar --prefix=libstm32-${LIBSTM32}/ ${LIBSTM32} | \
210             bzip2 --stdout > ../libstm32-${LIBSTM32}.tar.bz2
211         cd ..
212         rm -rf libstm32
213 fi
214
215 if [ ! -e libstm32usb-${LIBSTM32USB}.tar.bz2 ]; then
216         log "Cloning libstm32usb sources..."
217         git clone git://git.open-bldc.org/libstm32usb.git
218         cd libstm32usb
219         git archive --format=tar --prefix=libstm32usb-${LIBSTM32USB}/ ${LIBSTM32USB} | \
220             bzip2 --stdout > ../libstm32usb-${LIBSTM32USB}.tar.bz2
221         cd ..
222         rm -rf libstm32usb
223 fi
224 fi
225
226 if [ ${LIBOPENSTM32_EN} != 0 ]; then
227 if [ ! -e libopenstm32-${LIBOPENSTM32}.tar.bz2 ]; then
228         log "Cloning libopenstm32 sources..."
229         git clone git://libopenstm32.git.sourceforge.net/gitroot/libopenstm32/libopenstm32
230         cd libopenstm32
231         git archive --format=tar --prefix=libopenstm32-${LIBOPENSTM32}/ ${LIBOPENSTM32} | \
232             bzip2 --stdout > ../libopenstm32-${LIBOPENSTM32}.tar.bz2
233         cd ..
234         rm -rf libopenstm32
235 fi
236 fi
237
238 cd ${SUMMON_DIR}
239
240 if [ ! -e build ]; then
241     mkdir build
242 fi
243
244 if [ ! -e ${STAMPS}/${BINUTILS}.build ]; then
245     unpack ${BINUTILS}
246     cd build
247     log "Configuring ${BINUTILS}"
248     ../${BINUTILS}/configure --target=${TARGET} \
249                            --prefix=${PREFIX} \
250                            --enable-interwork \
251                            --enable-multilib \
252                            --with-gnu-as \
253                            --with-gnu-ld \
254                            --disable-nls \
255                            --disable-werror \
256                            ${BINUTILFLAGS}
257     log "Building ${BINUTILS}"
258     make ${MAKEFLAGS}
259     install ${BINUTILS} install
260     cd ..
261     log "Cleaning up ${BINUTILS}"
262     touch ${STAMPS}/${BINUTILS}.build
263     rm -rf build/* ${BINUTILS}
264 fi
265
266 if [ ! -e ${STAMPS}/${GCC}-boot.build ]; then
267     unpack ${GCC} boot
268     cd build
269     log "Configuring ${GCC}-boot"
270     ../${GCC}/configure --target=${TARGET} \
271                       --prefix=${PREFIX} \
272                       --enable-interwork \
273                       --enable-multilib \
274                       --enable-languages="c" \
275                       --with-newlib \
276                       --without-headers \
277                       --disable-shared \
278                       --with-gnu-as \
279                       --with-gnu-ld \
280                       --disable-nls \
281                       --disable-werror \
282                       --with-system-zlib \
283                       ${GCCFLAGS}
284     log "Building ${GCC}-boot"
285     make ${MAKEFLAGS} all-gcc
286     install ${GCC}-boot install-gcc
287     cd ..
288     log "Cleaning up ${GCC}-boot"
289     touch ${STAMPS}/${GCC}-boot.build
290     rm -rf build/* ${GCC}
291 fi
292
293 if [ ! -e ${STAMPS}/${NEWLIB}.build ]; then
294     unpack ${NEWLIB}
295     cd build
296     log "Configuring ${NEWLIB}"
297     ../${NEWLIB}/configure --target=${TARGET} \
298                          --prefix=${PREFIX} \
299                          --enable-interwork \
300                          --enable-multilib \
301                          --with-gnu-as \
302                          --with-gnu-ld \
303                          --disable-nls \
304                          --disable-werror \
305                          --disable-newlib-supplied-syscalls
306     log "Building ${NEWLIB}"
307     make ${MAKEFLAGS}
308     install ${NEWLIB} install
309     cd ..
310     log "Cleaning up ${NEWLIB}"
311     touch ${STAMPS}/${NEWLIB}.build
312     rm -rf build/* ${NEWLIB}
313 fi
314
315 # Yes, you need to build gcc again!
316 if [ ! -e ${STAMPS}/${GCC}.build ]; then
317     unpack ${GCC}
318     cd build
319     log "Configuring ${GCC}"
320     ../${GCC}/configure --target=${TARGET} \
321                       --prefix=${PREFIX} \
322                       --enable-interwork \
323                       --enable-multilib \
324                       --enable-languages="c,c++" \
325                       --with-newlib \
326                       --disable-shared \
327                       --with-gnu-as \
328                       --with-gnu-ld \
329                       --disable-nls \
330                       --disable-werror \
331                       --with-system-zlib \
332                      ${GCCFLAGS}
333     log "Building ${GCC}"
334     make ${MAKEFLAGS}
335     install ${GCC} install
336     cd ..
337     log "Cleaning up ${GCC}"
338     touch ${STAMPS}/${GCC}.build
339     rm -rf build/* ${GCC}
340 fi
341
342 if [ ! -e ${STAMPS}/${GDB}.build ]; then
343     unpack ${GDB}
344     cd build
345     log "Configuring ${GDB}"
346     ../${GDB}/configure --target=${TARGET} \
347                       --prefix=${PREFIX} \
348                       --enable-interwork \
349                       --enable-multilib \
350                       --disable-werror \
351                       ${GDBFLAGS}
352     log "Building ${GDB}"
353     make ${MAKEFLAGS}
354     install ${GDB} install
355     cd ..
356     log "Cleaning up ${GDB}"
357     touch ${STAMPS}/${GDB}.build
358     rm -rf build/* ${GDB}
359 fi
360
361 if [ ${LIBSTM32_EN} != 0 ]; then
362 if [ ! -e .libcmsis-${LIBCMSIS}.build ]; then
363     unpack libcmsis-${LIBCMSIS}
364     cd libcmsis-${LIBCMSIS}
365     log "Building libcmsis-${LIBCMSIS}"
366     make arch_prefix=${TARGET} prefix=${PREFIX}
367     install libcmsis-${LIBCMSIS} arch_prefix=${TARGET} prefix=${PREFIX} install
368     cd ..
369     log "Cleaning up libcmsis-${LIBCMSIS}"
370     touch .libcmsis-${LIBCMSIS}.build
371     rm -rf libcmsis-${LIBCMSIS}
372 fi
373
374 if [ ! -e .libstm32-${LIBSTM32}.build ]; then
375     unpack libstm32-${LIBSTM32}
376     cd libstm32-${LIBSTM32}
377     log "Building libstm32-${LIBSTM32}"
378     make arch_prefix=${TARGET} prefix=${PREFIX}
379     install libstm32-${LIBSTM32} arch_prefix=${TARGET} prefix=${PREFIX} install
380     cd ..
381     log "Cleaning up libstm32-${LIBSTM32}"
382     touch .libstm32-${LIBSTM32}.build
383     rm -rf libstm32-${LIBSTM32}
384 fi
385
386 if [ ! -e .libstm32usb-${LIBSTM32USB}.build ]; then
387     unpack libstm32usb-${LIBSTM32USB}
388     cd libstm32usb-${LIBSTM32USB}
389     log "Building libstm32usb-${LIBSTM32USB}"
390     make arch_prefix=${TARGET} prefix=${PREFIX}
391     install libstm32usb-${LIBSTM32USB} arch_prefix=${TARGET} prefix=${PREFIX} install
392     cd ..
393     log "Cleaning up libstm32usb-${LIBSTM32USB}"
394     touch .libstm32usb-${LIBSTM32USB}.build
395     rm -rf libstm32usb-${LIBSTM32USB}
396 fi
397 fi
398
399 if [ $LIBOPENSTM32_EN != 0 ]; then
400     unpack libopenstm32-${LIBOPENSTM32}
401     cd libopenstm32-${LIBOPENSTM32}
402     log "Building libopenstm32-${LIBOPENSTM32}"
403     make PREFIX=${TARGET} DESTDIR=${PREFIX}
404     install libopenstm32-${LIBOPENSTM32} PREFIX=${TARGET} DESTDIR=${PREFIX} install
405     cd ..
406     log "Cleaning up libopenstm32-${LIBOPENSTM32}"
407     touch .libopenstm32-${LIBOPENSTM32}.build
408     rm -rf libopenstm32-${LIBOPENSTM32}
409 fi