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