remove dead and untested code
[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 # Tool section
105 ##############################################################################
106 TAR=tar
107
108 ##############################################################################
109 # OS and Tooldetection section
110 # Detects which tools and flags to use
111 ##############################################################################
112
113 case "$(uname)" in
114         Linux)
115         echo "Found Linux OS."
116         ;;
117         Darwin)
118         echo "Found Darwin OS."
119
120         # darwin dependencies
121         DARWINDEPS="wget"
122
123   echo "Installing dependencies ..."
124   if ! which brew > /dev/null; then
125     echo "Homebrew not installed."
126   else
127       brew install ${DARWINDEPS} | (grep -v '^Warning: Formula already installed: ' || true)
128   fi
129
130         TAR=gnutar
131         ;;
132         CYGWIN*)
133         echo "Found CygWin that means Windows most likely."
134         ;;
135         *)
136         echo "Found unknown OS. Aborting!"
137         exit 1
138         ;;
139 esac
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         wget -c $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 install() {
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 cd ${SUMMON_DIR}
210
211 if [ ! -e build ]; then
212     mkdir build
213 fi
214
215 if [ ! -e ${STAMPS}/${BINUTILS}.build ]; then
216     unpack ${BINUTILS}
217     cd build
218     log "Configuring ${BINUTILS}"
219     ../${BINUTILS}/configure --target=${TARGET} \
220                            --prefix=${PREFIX} \
221                            --enable-interwork \
222                            --enable-multilib \
223                            --with-gnu-as \
224                            --with-gnu-ld \
225                            --disable-nls \
226                            --disable-werror \
227                            ${BINUTILFLAGS}
228     log "Building ${BINUTILS}"
229     make ${MAKEFLAGS}
230     install ${BINUTILS} install
231     cd ..
232     log "Cleaning up ${BINUTILS}"
233     touch ${STAMPS}/${BINUTILS}.build
234     rm -rf build/* ${BINUTILS}
235 fi
236
237 if [ ! -e ${STAMPS}/${GCC}-boot.build ]; then
238     unpack ${GCC} boot
239     unpack ${GMP}
240     mv ${GMP} ${GCCDIR}/gmp
241     unpack ${MPFR}
242     mv ${MPFR} ${GCCDIR}/mpfr
243     unpack ${MPC}
244     mv ${MPC} ${GCCDIR}/mpc
245     cd build
246     log "Configuring ${GCC}-boot"
247     ../${GCCDIR}/configure --target=${TARGET} \
248                       --prefix=${PREFIX} \
249                       --enable-interwork \
250                       --enable-multilib \
251                       --enable-languages="c" \
252                       --with-newlib \
253                       --without-headers \
254                       --disable-shared \
255                       --with-gnu-as \
256                       --with-gnu-ld \
257                       --disable-nls \
258                       --disable-werror \
259                       --with-system-zlib \
260                       ${GCCFLAGS}
261     log "Building ${GCC}-boot"
262     make ${MAKEFLAGS} all-gcc
263     install ${GCC}-boot install-gcc
264     cd ..
265     log "Cleaning up ${GCC}-boot"
266     touch ${STAMPS}/${GCC}-boot.build
267     rm -rf build/* ${GCCDIR}
268 fi
269
270 if [ ! -e ${STAMPS}/${NEWLIB}.build ]; then
271     unpack ${NEWLIB}
272     cd build
273     log "Configuring ${NEWLIB}"
274     # configuration options and flags partially
275     # from https://github.com/jsnyder/arm-eabi-toolchain
276     ../${NEWLIB}/configure --target=${TARGET} \
277                          --prefix=${PREFIX} \
278                          --enable-interwork \
279                          --enable-multilib \
280                          --with-gnu-as \
281                          --with-gnu-ld \
282                          --disable-nls \
283                          --disable-werror \
284                          --disable-newlib-supplied-syscalls \
285                          --disable-shared \
286                          --disable-nls \
287                          --disable-libgloss \
288                          --with-float=soft
289     log "Building ${NEWLIB}"
290     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"
291     make ${MAKEFLAGS} CFLAGS_FOR_TARGET="${NEWLIB_FLAGS}" CCASFLAGS="${NEWLIB_FLAGS}"
292     install ${NEWLIB} install
293     cd ..
294     log "Cleaning up ${NEWLIB}"
295     touch ${STAMPS}/${NEWLIB}.build
296     rm -rf build/* ${NEWLIB}
297 fi
298
299 # Yes, you need to build gcc again!
300 if [ ! -e ${STAMPS}/${GCC}.build ]; then
301     unpack ${GCC}
302     unpack ${GMP}
303     mv ${GMP} ${GCCDIR}/gmp
304     unpack ${MPFR}
305     mv ${MPFR} ${GCCDIR}/mpfr
306     unpack ${MPC}
307     mv ${MPC} ${GCCDIR}/mpc
308     cd build
309     log "Configuring ${GCC}"
310     [ ${USE_CPP} = 1 ] && GCCFLAGS="--enable-languages='c,c++' ${GCCFLAGS}"
311     ../${GCCDIR}/configure --target=${TARGET} \
312                       --prefix=${PREFIX} \
313                       --enable-interwork \
314                       --enable-multilib \
315                       --with-newlib \
316                       --disable-shared \
317                       --with-gnu-as \
318                       --with-gnu-ld \
319                       --disable-nls \
320                       --disable-werror \
321                       --with-system-zlib \
322                      ${GCCFLAGS}
323     log "Building ${GCC}"
324     make ${MAKEFLAGS}
325     install ${GCC} install
326     cd ..
327     log "Cleaning up ${GCC}"
328     touch ${STAMPS}/${GCC}.build
329     rm -rf build/* ${GCCDIR}
330 fi
331
332 if [ ! -e ${STAMPS}/${GDB}.build ]; then
333     unpack ${GDB}
334     cd build
335     log "Configuring ${GDB}"
336     ../${GDB}/configure --target=${TARGET} \
337                       --prefix=${PREFIX} \
338                       --enable-interwork \
339                       --enable-multilib \
340                       --disable-werror \
341                       ${GDBFLAGS}
342     log "Building ${GDB}"
343     make ${MAKEFLAGS}
344     install ${GDB} install
345     cd ..
346     log "Cleaning up ${GDB}"
347     touch ${STAMPS}/${GDB}.build
348     rm -rf build/* ${GDB}
349 fi
350
351 if [ ${SUMMON_DIR_CREATED} = 1 ]; then
352     log "Removing work directory"
353     rm -rf ${SUMMON_DIR}
354 fi