f6826da7ac599c6bff11b6c194b078017927c517
[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 if ! which wget > /dev/null; then
109     echo "Please install wget"
110     exit 1
111 fi
112
113 if [ USE_LINARO = 1 ]; then
114     if which gnutar > /dev/null; then
115         TAR=gnutar
116     elif which gtar > /dev/null; then
117         TAR=gtar
118     else    # hopefully it understands the GNU format
119         TAR=tar
120     fi
121 else
122     TAR=tar     # use the system tar
123 fi
124
125 ##############################################################################
126 # Building section
127 # You probably don't have to touch anything after this
128 ##############################################################################
129
130 # Fetch a versioned file from a URL
131 fetch() {
132     if [ ! -e ${STAMPS}/$1.fetch ]; then
133         log "Downloading $1 sources..."
134         wget -c $2
135         touch ${STAMPS}/$1.fetch
136     fi
137 }
138
139 # Log a message out to the console
140 log() {
141     echo "******************************************************************"
142     echo "* $*"
143     echo "******************************************************************"
144 }
145
146 # Unpack an archive
147 unpack() {
148     log Unpacking $*
149     # Use 'auto' mode decompression.  Replace with a switch if tar doesn't support -a
150     ARCHIVE=$(ls ${SOURCES}/$1.tar.*)
151     case ${ARCHIVE} in
152         *.bz2)
153             echo "archive type bz2"
154             TYPE=j
155             ;;
156         *.gz)
157             echo "archive type gz"
158             TYPE=z
159             ;;
160         *)
161             echo "Unknown archive type of $1"
162             echo "leaving the decision to tar"
163             TYPE=
164             ;;
165     esac
166     ${TAR} xf${TYPE}${TARFLAGS} ${SOURCES}/$1.tar.*
167 }
168
169 # Install a build
170 install() {
171     log $1
172     ${SUDO} make ${MAKEFLAGS} $2 $3 $4 $5 $6 $7 $8
173 }
174
175 if [ ! -d $SUMMON_DIR ]; then
176     mkdir -p ${SUMMON_DIR}
177     SUMMON_DIR_CREATED=1
178 else
179     SUMMON_DIR_CREATED=0
180 fi
181 mkdir -p ${STAMPS} ${SOURCES}
182
183 cd ${SOURCES}
184
185 fetch ${BINUTILS} http://ftp.gnu.org/gnu/binutils/${BINUTILS}.tar.bz2
186 fetch ${GCC} ${GCCURL}
187 fetch ${NEWLIB} ftp://sources.redhat.com/pub/newlib/${NEWLIB}.tar.gz
188 fetch ${GDB} http://ftp.gnu.org/gnu/gdb/${GDB}.tar.bz2
189 fetch ${GMP} ftp://ftp.gmplib.org/pub/${GMP}/${GMP}.tar.bz2
190 fetch ${MPFR} http://www.mpfr.org/${MPFR}/${MPFR}.tar.bz2
191 fetch ${MPC} http://www.multiprecision.org/mpc/download/${MPC}.tar.gz
192
193 cd ${SUMMON_DIR}
194
195 if [ ! -e build ]; then
196     mkdir build
197 fi
198
199 if [ ! -e ${STAMPS}/${BINUTILS}.build ]; then
200     unpack ${BINUTILS}
201     cd build
202     log "Configuring ${BINUTILS}"
203     ../${BINUTILS}/configure --target=${TARGET} \
204                            --prefix=${PREFIX} \
205                            --enable-interwork \
206                            --enable-multilib \
207                            --with-gnu-as \
208                            --with-gnu-ld \
209                            --disable-nls \
210                            --disable-werror \
211                            ${BINUTILFLAGS}
212     log "Building ${BINUTILS}"
213     make ${MAKEFLAGS}
214     install ${BINUTILS} install
215     cd ..
216     log "Cleaning up ${BINUTILS}"
217     touch ${STAMPS}/${BINUTILS}.build
218     rm -rf build/* ${BINUTILS}
219 fi
220
221 if [ ! -e ${STAMPS}/${GCC}-boot.build ]; then
222     unpack ${GCC} boot
223     unpack ${GMP}
224     mv ${GMP} ${GCCDIR}/gmp
225     unpack ${MPFR}
226     mv ${MPFR} ${GCCDIR}/mpfr
227     unpack ${MPC}
228     mv ${MPC} ${GCCDIR}/mpc
229     cd build
230     log "Configuring ${GCC}-boot"
231     ../${GCCDIR}/configure --target=${TARGET} \
232                       --prefix=${PREFIX} \
233                       --enable-interwork \
234                       --enable-multilib \
235                       --enable-languages="c" \
236                       --with-newlib \
237                       --without-headers \
238                       --disable-shared \
239                       --with-gnu-as \
240                       --with-gnu-ld \
241                       --disable-nls \
242                       --disable-werror \
243                       --with-system-zlib \
244                       ${GCCFLAGS}
245     log "Building ${GCC}-boot"
246     make ${MAKEFLAGS} all-gcc
247     install ${GCC}-boot install-gcc
248     cd ..
249     log "Cleaning up ${GCC}-boot"
250     touch ${STAMPS}/${GCC}-boot.build
251     rm -rf build/* ${GCCDIR}
252 fi
253
254 if [ ! -e ${STAMPS}/${NEWLIB}.build ]; then
255     unpack ${NEWLIB}
256     cd build
257     log "Configuring ${NEWLIB}"
258     # configuration options and flags partially
259     # from https://github.com/jsnyder/arm-eabi-toolchain
260     ../${NEWLIB}/configure --target=${TARGET} \
261                          --prefix=${PREFIX} \
262                          --enable-interwork \
263                          --enable-multilib \
264                          --with-gnu-as \
265                          --with-gnu-ld \
266                          --disable-nls \
267                          --disable-werror \
268                          --disable-newlib-supplied-syscalls \
269                          --disable-shared \
270                          --disable-nls \
271                          --disable-libgloss \
272                          --with-float=soft
273     log "Building ${NEWLIB}"
274     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"
275     make ${MAKEFLAGS} CFLAGS_FOR_TARGET="${NEWLIB_FLAGS}" CCASFLAGS="${NEWLIB_FLAGS}"
276     install ${NEWLIB} install
277     cd ..
278     log "Cleaning up ${NEWLIB}"
279     touch ${STAMPS}/${NEWLIB}.build
280     rm -rf build/* ${NEWLIB}
281 fi
282
283 # Yes, you need to build gcc again!
284 if [ ! -e ${STAMPS}/${GCC}.build ]; then
285     unpack ${GCC}
286     unpack ${GMP}
287     mv ${GMP} ${GCCDIR}/gmp
288     unpack ${MPFR}
289     mv ${MPFR} ${GCCDIR}/mpfr
290     unpack ${MPC}
291     mv ${MPC} ${GCCDIR}/mpc
292     cd build
293     log "Configuring ${GCC}"
294     [ ${USE_CPP} = 1 ] && GCCFLAGS="--enable-languages='c,c++' ${GCCFLAGS}"
295     ../${GCCDIR}/configure --target=${TARGET} \
296                       --prefix=${PREFIX} \
297                       --enable-interwork \
298                       --enable-multilib \
299                       --with-newlib \
300                       --disable-shared \
301                       --with-gnu-as \
302                       --with-gnu-ld \
303                       --disable-nls \
304                       --disable-werror \
305                       --with-system-zlib \
306                      ${GCCFLAGS}
307     log "Building ${GCC}"
308     make ${MAKEFLAGS}
309     install ${GCC} install
310     cd ..
311     log "Cleaning up ${GCC}"
312     touch ${STAMPS}/${GCC}.build
313     rm -rf build/* ${GCCDIR}
314 fi
315
316 if [ ! -e ${STAMPS}/${GDB}.build ]; then
317     unpack ${GDB}
318     cd build
319     log "Configuring ${GDB}"
320     ../${GDB}/configure --target=${TARGET} \
321                       --prefix=${PREFIX} \
322                       --enable-interwork \
323                       --enable-multilib \
324                       --disable-werror \
325                       ${GDBFLAGS}
326     log "Building ${GDB}"
327     make ${MAKEFLAGS}
328     install ${GDB} install
329     cd ..
330     log "Cleaning up ${GDB}"
331     touch ${STAMPS}/${GDB}.build
332     rm -rf build/* ${GDB}
333 fi
334
335 if [ ${SUMMON_DIR_CREATED} = 1 ]; then
336     log "Removing work directory"
337     rm -rf ${SUMMON_DIR}
338 fi