rename install() to not mask the standard command
[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 ##############################################################################
62 # Flags section
63 ##############################################################################
64
65 if which getconf > /dev/null; then
66         CPUS=$(getconf _NPROCESSORS_ONLN)
67 else
68         CPUS=1
69 fi
70 PARALLEL=-j$((CPUS + 1))
71 echo "${CPUS} cpu's detected running make with '${PARALLEL}' flag"
72
73 GDBFLAGS=
74 BINUTILFLAGS=
75
76 if [ ${DEFAULT_TO_CORTEX_M3} = 0 ] ; then
77         GCCFLAGS=
78 else
79         # To default to the Cortex-M3:
80         GCCFLAGS="--with-arch=armv7-m --with-mode=thumb --with-float=soft"
81 fi
82
83 # Pull in the local configuration, if any
84 if [ -f local.sh ]; then
85     . ./local.sh
86 fi
87
88 MAKEFLAGS=${PARALLEL}
89 TARFLAGS=v
90
91 if [ ${QUIET} != 0 ]; then
92     TARFLAGS=
93     MAKEFLAGS="${MAKEFLAGS} -s"
94 fi
95
96 export PATH="${PREFIX}/bin:${PATH}"
97
98 SUMMON_DIR=${WORKDIR}
99 SOURCES=${SUMMON_DIR}/sources
100 STAMPS=${SUMMON_DIR}/stamps
101
102
103 ##############################################################################
104 # OS and Tooldetection section
105 # Detects which tools and flags to use
106 ##############################################################################
107
108 # normalized fetching with whatever we can find
109 if which fetch > /dev/null; then
110     FETCHCMD="fetch -p"
111 elif which wget > /dev/null; then
112     FETCHCMD=wget
113 elif which curl > /dev/null; then
114     FETCHCMD="curl -O"
115 else
116     FETCHCMD=ftp    # the only HTTP client on some BSD
117 fi
118
119 if [ USE_LINARO = 1 ]; then
120     if which gnutar > /dev/null; then
121         TAR=gnutar
122     elif which gtar > /dev/null; then
123         TAR=gtar
124     else    # hopefully it understands the GNU format
125         TAR=tar
126     fi
127 else
128     TAR=tar     # use the system tar
129 fi
130
131 ##############################################################################
132 # Building section
133 # You probably don't have to touch anything after this
134 ##############################################################################
135
136 # Fetch a versioned file from a URL
137 fetch() {
138     if [ ! -e ${STAMPS}/$1.fetch ]; then
139         log "Downloading $1 sources..."
140         ${FETCHCMD} $2
141         touch ${STAMPS}/$1.fetch
142     fi
143 }
144
145 # Log a message out to the console
146 log() {
147     echo "******************************************************************"
148     echo "* $*"
149     echo "******************************************************************"
150 }
151
152 # Unpack an archive
153 unpack() {
154     log Unpacking $*
155     # Use 'auto' mode decompression.  Replace with a switch if tar doesn't support -a
156     ARCHIVE=$(ls ${SOURCES}/$1.tar.*)
157     case ${ARCHIVE} in
158         *.bz2)
159             echo "archive type bz2"
160             TYPE=j
161             ;;
162         *.gz)
163             echo "archive type gz"
164             TYPE=z
165             ;;
166         *)
167             echo "Unknown archive type of $1"
168             echo "leaving the decision to tar"
169             TYPE=
170             ;;
171     esac
172     ${TAR} xf${TYPE}${TARFLAGS} ${SOURCES}/$1.tar.*
173 }
174
175 # Install a build
176 doinstall() {
177     log $1
178     ${SUDO} make ${MAKEFLAGS} $2 $3 $4 $5 $6 $7 $8
179 }
180
181 if [ ! -d $SUMMON_DIR ]; then
182     mkdir -p ${SUMMON_DIR}
183     SUMMON_DIR_CREATED=1
184 else
185     SUMMON_DIR_CREATED=0
186 fi
187 mkdir -p ${STAMPS} ${SOURCES}
188
189 cd ${SOURCES}
190
191 fetch ${BINUTILS} http://ftp.gnu.org/gnu/binutils/${BINUTILS}.tar.bz2
192 fetch ${GCC} ${GCCURL}
193 fetch ${NEWLIB} ftp://sources.redhat.com/pub/newlib/${NEWLIB}.tar.gz
194 fetch ${GDB} http://ftp.gnu.org/gnu/gdb/${GDB}.tar.bz2
195 fetch ${GMP} ftp://ftp.gmplib.org/pub/${GMP}/${GMP}.tar.bz2
196 fetch ${MPFR} http://www.mpfr.org/${MPFR}/${MPFR}.tar.bz2
197 fetch ${MPC} http://www.multiprecision.org/mpc/download/${MPC}.tar.gz
198
199 cd ${SUMMON_DIR}
200
201 if [ ! -e build ]; then
202     mkdir build
203 fi
204
205 if [ ! -e ${STAMPS}/${BINUTILS}.build ]; then
206     unpack ${BINUTILS}
207     cd build
208     log "Configuring ${BINUTILS}"
209     ../${BINUTILS}/configure --target=${TARGET} \
210                            --prefix=${PREFIX} \
211                            --enable-interwork \
212                            --enable-multilib \
213                            --with-gnu-as \
214                            --with-gnu-ld \
215                            --disable-nls \
216                            --disable-werror \
217                            ${BINUTILFLAGS}
218     log "Building ${BINUTILS}"
219     make ${MAKEFLAGS}
220     doinstall ${BINUTILS} install
221     cd ..
222     log "Cleaning up ${BINUTILS}"
223     touch ${STAMPS}/${BINUTILS}.build
224     rm -rf build/* ${BINUTILS}
225 fi
226
227 if [ ! -e ${STAMPS}/${GCC}-boot.build ]; then
228     unpack ${GCC} boot
229     unpack ${GMP}
230     mv ${GMP} ${GCCDIR}/gmp
231     unpack ${MPFR}
232     mv ${MPFR} ${GCCDIR}/mpfr
233     unpack ${MPC}
234     mv ${MPC} ${GCCDIR}/mpc
235     cd build
236     log "Configuring ${GCC}-boot"
237     ../${GCCDIR}/configure --target=${TARGET} \
238                       --prefix=${PREFIX} \
239                       --enable-interwork \
240                       --enable-multilib \
241                       --enable-languages="c" \
242                       --with-newlib \
243                       --without-headers \
244                       --disable-shared \
245                       --with-gnu-as \
246                       --with-gnu-ld \
247                       --disable-nls \
248                       --disable-werror \
249                       --with-system-zlib \
250                       ${GCCFLAGS}
251     log "Building ${GCC}-boot"
252     make ${MAKEFLAGS} all-gcc
253     doinstall ${GCC}-boot install-gcc
254     cd ..
255     log "Cleaning up ${GCC}-boot"
256     touch ${STAMPS}/${GCC}-boot.build
257     rm -rf build/* ${GCCDIR}
258 fi
259
260 if [ ! -e ${STAMPS}/${NEWLIB}.build ]; then
261     unpack ${NEWLIB}
262     cd build
263     log "Configuring ${NEWLIB}"
264     # configuration options and flags partially
265     # from https://github.com/jsnyder/arm-eabi-toolchain
266     ../${NEWLIB}/configure --target=${TARGET} \
267                          --prefix=${PREFIX} \
268                          --enable-interwork \
269                          --enable-multilib \
270                          --with-gnu-as \
271                          --with-gnu-ld \
272                          --disable-nls \
273                          --disable-werror \
274                          --disable-newlib-supplied-syscalls \
275                          --disable-shared \
276                          --disable-nls \
277                          --disable-libgloss \
278                          --with-float=soft
279     log "Building ${NEWLIB}"
280     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"
281     make ${MAKEFLAGS} CFLAGS_FOR_TARGET="${NEWLIB_FLAGS}" CCASFLAGS="${NEWLIB_FLAGS}"
282     doinstall ${NEWLIB} install
283     cd ..
284     log "Cleaning up ${NEWLIB}"
285     touch ${STAMPS}/${NEWLIB}.build
286     rm -rf build/* ${NEWLIB}
287 fi
288
289 # Yes, you need to build gcc again!
290 if [ ! -e ${STAMPS}/${GCC}.build ]; then
291     unpack ${GCC}
292     unpack ${GMP}
293     mv ${GMP} ${GCCDIR}/gmp
294     unpack ${MPFR}
295     mv ${MPFR} ${GCCDIR}/mpfr
296     unpack ${MPC}
297     mv ${MPC} ${GCCDIR}/mpc
298     cd build
299     log "Configuring ${GCC}"
300     [ ${USE_CPP} = 1 ] && GCCFLAGS="--enable-languages='c,c++' ${GCCFLAGS}"
301     ../${GCCDIR}/configure --target=${TARGET} \
302                       --prefix=${PREFIX} \
303                       --enable-interwork \
304                       --enable-multilib \
305                       --with-newlib \
306                       --disable-shared \
307                       --with-gnu-as \
308                       --with-gnu-ld \
309                       --disable-nls \
310                       --disable-werror \
311                       --with-system-zlib \
312                      ${GCCFLAGS}
313     log "Building ${GCC}"
314     make ${MAKEFLAGS}
315     doinstall ${GCC} install
316     cd ..
317     log "Cleaning up ${GCC}"
318     touch ${STAMPS}/${GCC}.build
319     rm -rf build/* ${GCCDIR}
320 fi
321
322 if [ ! -e ${STAMPS}/${GDB}.build ]; then
323     unpack ${GDB}
324     cd build
325     log "Configuring ${GDB}"
326     ../${GDB}/configure --target=${TARGET} \
327                       --prefix=${PREFIX} \
328                       --enable-interwork \
329                       --enable-multilib \
330                       --disable-werror \
331                       ${GDBFLAGS}
332     log "Building ${GDB}"
333     make ${MAKEFLAGS}
334     doinstall ${GDB} install
335     cd ..
336     log "Cleaning up ${GDB}"
337     touch ${STAMPS}/${GDB}.build
338     rm -rf build/* ${GDB}
339 fi
340
341 if [ ${SUMMON_DIR_CREATED} = 1 ]; then
342     log "Removing work directory"
343     rm -rf ${SUMMON_DIR}
344 fi