check filesums if sha1sum is available
[summon-arm-toolchain.git] / summon-arm-toolchain
1 #!/bin/sh
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 # adapted for r0ket <http://r0ket.badge.events.ccc.de> development by
6 # Matthias Rampke <matthias@rampke.de>
7 # requires Homebrew <https://mxcl.github.com/homebrew>
8
9 # Stop if any command fails
10 set -e
11
12 ##############################################################################
13 # Settings section
14 # You probably want to customize those
15 ##############################################################################
16 TARGET=arm-none-eabi            # Or: TARGET=arm-elf
17 [ -z $PREFIX ] && PREFIX=${HOME}/arm    # Install location of your final toolchain
18 [ -z $WORKDIR ] && WORKDIR=$PREFIX/work     #temporary directory
19 PARALLEL=                       # Or: PARALLEL="-j 5" for 4 CPUs
20 # Set to 'sudo' if you need superuser privileges while installing
21 SUDO=''
22 # Set to 1 to be quieter while running
23 QUIET=0
24 # Set to 1 to use linaro gcc instead of the FSF gcc
25 USE_LINARO=0
26 # Set to 1 to enable C++
27 USE_CPP=0
28 # Make the gcc default to Cortex-M3
29 DEFAULT_TO_CORTEX_M3=1
30
31 ##############################################################################
32 # Version and download url settings section
33 ##############################################################################
34 if [ ${USE_LINARO} = 0 ] ; then
35         # For FSF GCC:
36         GCCVERSION=4.6.1
37         if [ ${USE_CPP} = 0 ]; then
38             GCC=gcc-core-${GCCVERSION}
39             GCCDIR=gcc-${GCCVERSION}
40         else
41             GCC=gcc-${GCCVERSION}
42             GCCDIR=${GCC}
43         fi
44         GCCURL=http://ftp.gnu.org/gnu/gcc/${GCCDIR}/${GCC}.tar.bz2
45 else
46         # For the Linaro GCC:
47         GCCRELEASE=4.6-2011.07
48         GCCVERSION=4.6-2011.07-0
49         GCC=gcc-linaro-${GCCRELEASE}
50         GCCDIR=gcc-linaro-${GCCVERSION}
51         GCCURL=http://launchpad.net/gcc-linaro/4.6/${GCCRELEASE}/+download/gcc-linaro-${GCCRELEASE}.tar.bz2
52 fi
53
54 BINUTILS=binutils-2.21
55 NEWLIB=newlib-1.19.0
56 GDB=gdb-7.3
57 GMP=gmp-5.0.2
58 MPFR=mpfr-3.0.1
59 MPC=mpc-0.9
60
61 SHA1SUMS="ef93235588eb443e4c4a77f229a8d131bccaecc6  binutils-2.21.tar.bz2
62 8bf66f7a71534ba564563a0ba0b2517aead8ac30  gcc-4.6.1.tar.bz2
63 9b766705f051ffb7321de58f247688b0ae661b98  gcc-core-4.6.1.tar.bz2
64 828d99a63e0578879150f5681d234763cff69313  gcc-linaro-4.6-2011.07.tar.bz2
65 fefd90275d6decdca00c5c29c533e2ef3d439664  gdb-7.3.tar.bz2
66 2968220e1988eabb61f921d11e5d2db5431e0a35  gmp-5.0.2.tar.bz2
67 229722d553030734d49731844abfef7617b64f1a  mpc-0.9.tar.gz
68 fbf402fc196724ae60ef01eb6ca8490b1ea4db69  mpfr-3.0.1.tar.bz2
69 b2269d30ce7b93b7c714b90ef2f40221c2df0fcd  newlib-1.19.0.tar.gz"
70
71 ##############################################################################
72 # Flags section
73 ##############################################################################
74
75 if which getconf > /dev/null; then
76         CPUS=$(getconf _NPROCESSORS_ONLN)
77 else
78         CPUS=1
79 fi
80 PARALLEL=-j$((CPUS + 1))
81 echo "${CPUS} cpu's detected running make with '${PARALLEL}' flag"
82
83 GDBFLAGS=
84 BINUTILFLAGS=
85
86 if [ ${DEFAULT_TO_CORTEX_M3} = 0 ] ; then
87         GCCFLAGS=
88 else
89         # To default to the Cortex-M3:
90         GCCFLAGS="--with-arch=armv7-m --with-mode=thumb --with-float=soft"
91 fi
92
93 # Pull in the local configuration, if any
94 if [ -f local.sh ]; then
95     . ./local.sh
96 fi
97
98 MAKEFLAGS=${PARALLEL}
99 TARFLAGS=v
100
101 if [ ${QUIET} != 0 ]; then
102     TARFLAGS=
103     MAKEFLAGS="${MAKEFLAGS} -s"
104 fi
105
106 export PATH="${PREFIX}/bin:${PATH}"
107
108 SUMMON_DIR=${WORKDIR}
109 SOURCES=${SUMMON_DIR}/sources
110 STAMPS=${SUMMON_DIR}/stamps
111
112
113 ##############################################################################
114 # OS and Tooldetection section
115 # Detects which tools and flags to use
116 ##############################################################################
117
118 # normalized fetching with whatever we can find
119 if which fetch > /dev/null; then
120     FETCHCMD="fetch -p"
121 elif which wget > /dev/null; then
122     FETCHCMD=wget
123 elif which curl > /dev/null; then
124     FETCHCMD="curl -O -L"
125 else
126     FETCHCMD=ftp    # the only HTTP client on some BSD
127 fi
128
129 if [ USE_LINARO = 1 ]; then
130     if which gnutar > /dev/null; then
131         TAR=gnutar
132     elif which gtar > /dev/null; then
133         TAR=gtar
134     else    # hopefully it understands the GNU format
135         TAR=tar
136     fi
137 else
138     TAR=tar     # use the system tar
139 fi
140
141 ##############################################################################
142 # Building section
143 # You probably don't have to touch anything after this
144 ##############################################################################
145
146 # Fetch a versioned file from a URL
147 fetch() {
148     if [ ! -e ${STAMPS}/$1.fetch ]; then
149         log "Downloading $1 sources..."
150         ${FETCHCMD} $2
151         touch ${STAMPS}/$1.fetch
152     fi
153 }
154
155 # Log a message out to the console
156 log() {
157     echo "******************************************************************"
158     echo "* $*"
159     echo "******************************************************************"
160 }
161
162 # Unpack an archive
163 unpack() {
164     log Unpacking $*
165     # Use 'auto' mode decompression.  Replace with a switch if tar doesn't support -a
166     ARCHIVE=$(ls ${SOURCES}/$1.tar.*)
167     case ${ARCHIVE} in
168         *.bz2)
169             echo "archive type bz2"
170             TYPE=j
171             ;;
172         *.gz)
173             echo "archive type gz"
174             TYPE=z
175             ;;
176         *)
177             echo "Unknown archive type of $1"
178             echo "leaving the decision to tar"
179             TYPE=
180             ;;
181     esac
182     ${TAR} xf${TYPE}${TARFLAGS} ${SOURCES}/$1.tar.*
183 }
184
185 # Install a build
186 doinstall() {
187     log $1
188     ${SUDO} make ${MAKEFLAGS} $2 $3 $4 $5 $6 $7 $8
189 }
190
191 if [ ! -d $SUMMON_DIR ]; then
192     mkdir -p ${SUMMON_DIR}
193     SUMMON_DIR_CREATED=1
194 else
195     SUMMON_DIR_CREATED=0
196 fi
197 mkdir -p ${STAMPS} ${SOURCES}
198
199 cd ${SOURCES}
200
201 fetch ${BINUTILS} http://ftp.gnu.org/gnu/binutils/${BINUTILS}.tar.bz2
202 fetch ${GCC} ${GCCURL}
203 fetch ${NEWLIB} ftp://sources.redhat.com/pub/newlib/${NEWLIB}.tar.gz
204 fetch ${GDB} http://ftp.gnu.org/gnu/gdb/${GDB}.tar.bz2
205 fetch ${GMP} ftp://ftp.gmplib.org/pub/${GMP}/${GMP}.tar.bz2
206 fetch ${MPFR} http://www.mpfr.org/${MPFR}/${MPFR}.tar.bz2
207 fetch ${MPC} http://www.multiprecision.org/mpc/download/${MPC}.tar.gz
208
209 if which sha1sum > /dev/null; then
210     ( echo "$SHA1SUMS" | sha1sum -c - ) || ( log 'ERROR: Checksum fail!'; exit 1 )
211 else
212     log "WARING: skipping checksum checks due to missing sha1sum"
213 fi
214
215 cd ${SUMMON_DIR}
216
217 if [ ! -e build ]; then
218     mkdir build
219 fi
220
221 if [ ! -e ${STAMPS}/${BINUTILS}.build ]; then
222     unpack ${BINUTILS}
223     cd build
224     log "Configuring ${BINUTILS}"
225     ../${BINUTILS}/configure --target=${TARGET} \
226                            --prefix=${PREFIX} \
227                            --enable-interwork \
228                            --enable-multilib \
229                            --with-gnu-as \
230                            --with-gnu-ld \
231                            --disable-nls \
232                            --disable-werror \
233                            ${BINUTILFLAGS}
234     log "Building ${BINUTILS}"
235     make ${MAKEFLAGS}
236     doinstall ${BINUTILS} install
237     cd ..
238     log "Cleaning up ${BINUTILS}"
239     touch ${STAMPS}/${BINUTILS}.build
240     rm -rf build/* ${BINUTILS}
241 fi
242
243 if [ ! -e ${STAMPS}/${GCC}-boot.build ]; then
244     unpack ${GCC} boot
245     unpack ${GMP}
246     mv ${GMP} ${GCCDIR}/gmp
247     unpack ${MPFR}
248     mv ${MPFR} ${GCCDIR}/mpfr
249     unpack ${MPC}
250     mv ${MPC} ${GCCDIR}/mpc
251     cd build
252     log "Configuring ${GCC}-boot"
253     ../${GCCDIR}/configure --target=${TARGET} \
254                       --prefix=${PREFIX} \
255                       --enable-interwork \
256                       --enable-multilib \
257                       --enable-languages="c" \
258                       --with-newlib \
259                       --without-headers \
260                       --disable-shared \
261                       --with-gnu-as \
262                       --with-gnu-ld \
263                       --disable-nls \
264                       --disable-werror \
265                       --with-system-zlib \
266                       ${GCCFLAGS}
267     log "Building ${GCC}-boot"
268     make ${MAKEFLAGS} all-gcc
269     doinstall ${GCC}-boot install-gcc
270     cd ..
271     log "Cleaning up ${GCC}-boot"
272     touch ${STAMPS}/${GCC}-boot.build
273     rm -rf build/* ${GCCDIR}
274 fi
275
276 if [ ! -e ${STAMPS}/${NEWLIB}.build ]; then
277     unpack ${NEWLIB}
278     cd build
279     log "Configuring ${NEWLIB}"
280     # configuration options and flags partially
281     # from https://github.com/jsnyder/arm-eabi-toolchain
282     ../${NEWLIB}/configure --target=${TARGET} \
283                          --prefix=${PREFIX} \
284                          --enable-interwork \
285                          --enable-multilib \
286                          --with-gnu-as \
287                          --with-gnu-ld \
288                          --disable-nls \
289                          --disable-werror \
290                          --disable-newlib-supplied-syscalls \
291                          --disable-shared \
292                          --disable-nls \
293                          --disable-libgloss \
294                          --with-float=soft
295     log "Building ${NEWLIB}"
296     NEWLIB_FLAGS="-ffunction-sections -fdata-sections -DPREFER_SIZE_OVER_SPEED -D__OPTIMIZE_SIZE__ -Os -fomit-frame-pointer -fno-unroll-loops -D__BUFSIZ__=256 -mabi=aapcs"
297     make ${MAKEFLAGS} CFLAGS_FOR_TARGET="${NEWLIB_FLAGS}" CCASFLAGS="${NEWLIB_FLAGS}"
298     doinstall ${NEWLIB} install
299     cd ..
300     log "Cleaning up ${NEWLIB}"
301     touch ${STAMPS}/${NEWLIB}.build
302     rm -rf build/* ${NEWLIB}
303 fi
304
305 # Yes, you need to build gcc again!
306 if [ ! -e ${STAMPS}/${GCC}.build ]; then
307     unpack ${GCC}
308     unpack ${GMP}
309     mv ${GMP} ${GCCDIR}/gmp
310     unpack ${MPFR}
311     mv ${MPFR} ${GCCDIR}/mpfr
312     unpack ${MPC}
313     mv ${MPC} ${GCCDIR}/mpc
314     cd build
315     log "Configuring ${GCC}"
316     [ ${USE_CPP} = 1 ] && GCCFLAGS="--enable-languages='c,c++' ${GCCFLAGS}"
317     ../${GCCDIR}/configure --target=${TARGET} \
318                       --prefix=${PREFIX} \
319                       --enable-interwork \
320                       --enable-multilib \
321                       --with-newlib \
322                       --disable-shared \
323                       --with-gnu-as \
324                       --with-gnu-ld \
325                       --disable-nls \
326                       --disable-werror \
327                       --with-system-zlib \
328                      ${GCCFLAGS}
329     log "Building ${GCC}"
330     make ${MAKEFLAGS}
331     doinstall ${GCC} install
332     cd ..
333     log "Cleaning up ${GCC}"
334     touch ${STAMPS}/${GCC}.build
335     rm -rf build/* ${GCCDIR}
336 fi
337
338 if [ ! -e ${STAMPS}/${GDB}.build ]; then
339     unpack ${GDB}
340     cd build
341     log "Configuring ${GDB}"
342     ../${GDB}/configure --target=${TARGET} \
343                       --prefix=${PREFIX} \
344                       --enable-interwork \
345                       --enable-multilib \
346                       --disable-werror \
347                       ${GDBFLAGS}
348     log "Building ${GDB}"
349     make ${MAKEFLAGS}
350     doinstall ${GDB} install
351     cd ..
352     log "Cleaning up ${GDB}"
353     touch ${STAMPS}/${GDB}.build
354     rm -rf build/* ${GDB}
355 fi
356
357 if [ ${SUMMON_DIR_CREATED} = 1 ]; then
358     log "Removing work directory"
359     rm -rf ${SUMMON_DIR}
360 fi