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