Pulled all logging out into a function
[fw/cortex-toolchain] / 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
12 TARGET=arm-none-eabi            # Or: TARGET=arm-elf
13 PREFIX=${HOME}/arm-none-eabi    # Install location of your final toolchain
14 PARALLEL=""                     # Or: PARALLEL="-j 5" for 4 CPUs
15 DARWIN_OPT_PATH=/opt/local      # Path in which MacPorts or Fink is installed
16
17 BINUTILS=binutils-2.20
18 GCC=gcc-4.5.1
19 NEWLIB=newlib-1.18.0
20 GDB=gdb-7.2
21 LIBCMSIS=v1.10-2
22 LIBSTM32=v3.0.0-1
23 LIBSTM32USB=v3.0.1-1
24 LIBOPENSTM32=master
25 LIBSTM32_EN=0
26 LIBOPENSTM32_EN=0
27
28 SUMMON_DIR=$(pwd)
29 SOURCES=${SUMMON_DIR}/sources
30 STAMPS=${SUMMON_DIR}/stamps
31
32 export PATH="${PREFIX}/bin:${PATH}"
33
34 GCCFLAGS=
35 GDBFLAGS=
36 BINUTILFLAGS=
37
38 # Fetch a versioned file from a URL
39 function fetch {
40     if [ ! -e ${STAMPS}/$1.fetch ]; then
41         echo "Downloading $1 sources..."
42         wget -c --no-passive-ftp $2
43         touch ${STAMPS}/$1.fetch
44     fi
45 }
46
47 # Log a message out to the console
48 function log {
49     echo "******************************************************************"
50     echo "* $*"
51     echo "******************************************************************"
52 }
53
54 case "$(uname)" in
55         Linux)
56         echo "Found Linux OS."
57         ;;
58         Darwin)
59         echo "Found Darwin OS."
60         GCCFLAGS="--with-gmp=${DARWIN_OPT_PATH} \
61                   --with-mpfr=${DARWIN_OPT_PATH} \
62                   --with-mpc=${DARWIN_OPT_PATH} \
63                   -with-libiconv-prefix=${DARWIN_OPT_PATH}"
64         ;;
65         *)
66         echo "Found unknown OS. Aborting!"
67         exit 1
68         ;;
69 esac
70
71 mkdir -p ${STAMPS} ${SOURCES}
72
73 cd ${SOURCES}
74
75 fetch ${BINUTILS} http://ftp.gnu.org/gnu/binutils/${BINUTILS}.tar.bz2
76 fetch ${GCC} ${GCC_URL}
77 fetch ${NEWLIB} ftp://sources.redhat.com/pub/newlib/${NEWLIB}.tar.gz
78 fetch ${GDB} http://ftp.gnu.org/gnu/gdb/${GDB}.tar.bz2
79
80 if [ ${LIBSTM32_EN} != 0 ]; then
81 if [ ! -e libcmsis-${LIBCMSIS}.tar.bz2 ]; then
82         echo "Cloning libcmsis sources..."
83         git clone git://git.open-bldc.org/libcmsis.git
84         cd libcmsis
85         git archive --format=tar --prefix=libcmsis-${LIBCMSIS}/ ${LIBCMSIS} | \
86             bzip2 --stdout > ../libcmsis-${LIBCMSIS}.tar.bz2
87         cd ..
88         rm -rf libcmsis
89 fi
90
91 if [ ! -e libstm32-${LIBSTM32}.tar.bz2 ]; then
92         echo "Cloning libstm32 sources..."
93         git clone git://git.open-bldc.org/libstm32.git
94         cd libstm32
95         git archive --format=tar --prefix=libstm32-${LIBSTM32}/ ${LIBSTM32} | \
96             bzip2 --stdout > ../libstm32-${LIBSTM32}.tar.bz2
97         cd ..
98         rm -rf libstm32
99 fi
100
101 if [ ! -e libstm32usb-${LIBSTM32USB}.tar.bz2 ]; then
102         echo "Cloning libstm32usb sources..."
103         git clone git://git.open-bldc.org/libstm32usb.git
104         cd libstm32usb
105         git archive --format=tar --prefix=libstm32usb-${LIBSTM32USB}/ ${LIBSTM32USB} | \
106             bzip2 --stdout > ../libstm32usb-${LIBSTM32USB}.tar.bz2
107         cd ..
108         rm -rf libstm32usb
109 fi
110 fi
111
112 if [ ${LIBOPENSTM32_EN} != 0 ]; then
113 if [ ! -e libopenstm32-${LIBOPENSTM32}.tar.bz2 ]; then
114         echo "Cloning libopenstm32 sources..."
115         git clone git://libopenstm32.git.sourceforge.net/gitroot/libopenstm32/libopenstm32
116         cd libopenstm32
117         git archive --format=tar --prefix=libopenstm32-${LIBOPENSTM32}/ ${LIBOPENSTM32} | \
118             bzip2 --stdout > ../libopenstm32-${LIBOPENSTM32}.tar.bz2
119         cd ..
120         rm -rf libopenstm32
121 fi
122 fi
123
124 cd ${SUMMON_DIR}
125
126 if [ ! -e build ]; then
127     mkdir build
128 fi
129
130 if [ ! -e ${STAMPS}/${BINUTILS}.build ]; then
131     log "Unpacking ${BINUTILS}"
132     tar xfvj ${SOURCES}/${BINUTILS}.tar.bz2
133     cd build
134     log "Configuring ${BINUTILS}"
135     ../${BINUTILS}/configure --target=${TARGET} \
136                            --prefix=${PREFIX} \
137                            --enable-interwork \
138                            --enable-multilib \
139                            --with-gnu-as \
140                            --with-gnu-ld \
141                            --disable-nls \
142                            --disable-werror \
143                            ${BINUTILFLAGS} || exit
144     log "Building ${BINUTILS}"
145     make ${PARALLEL} || exit
146     log "Installing ${BINUTILS}"
147     make install || exit
148     cd ..
149     log "Cleaning up ${BINUTILS}"
150     touch ${STAMPS}/${BINUTILS}.build
151     rm -rf build/* ${BINUTILS}
152 fi
153
154 if [ ! -e ${STAMPS}/${GCC}-boot.build ]; then
155     log "Unpacking ${GCC}-boot"
156     tar xfvj ${SOURCES}/${GCC}.tar.bz2
157     cd build
158     log "Configuring ${GCC}-boot"
159     ../${GCC}/configure --target=${TARGET} \
160                       --prefix=${PREFIX} \
161                       --enable-interwork \
162                       --enable-multilib \
163                       --enable-languages="c" \
164                       --with-newlib \
165                       --without-headers \
166                       --disable-shared \
167                       --with-gnu-as \
168                       --with-gnu-ld \
169                       --disable-nls \
170                       --disable-werror \
171                       ${GCCFLAGS} || exit
172     log "Building ${GCC}-boot"
173     make ${PARALLEL} all-gcc || exit
174     log "Installing ${GCC}-boot"
175     make install-gcc || exit
176     cd ..
177     log "Cleaning up ${GCC}-boot"
178     touch ${STAMPS}/${GCC}-boot.build
179     rm -rf build/* ${GCC}
180 fi
181
182 if [ ! -e ${STAMPS}/${NEWLIB}.build ]; then
183     log "Unpacking ${NEWLIB}"
184     tar xfvz ${SOURCES}/${NEWLIB}.tar.gz
185     cd build
186     log "Configuring ${NEWLIB}"
187     ../${NEWLIB}/configure --target=${TARGET} \
188                          --prefix=${PREFIX} \
189                          --enable-interwork \
190                          --enable-multilib \
191                          --with-gnu-as \
192                          --with-gnu-ld \
193                          --disable-nls \
194                          --disable-werror \
195                          --disable-newlib-supplied-syscalls || exit
196     log "Building ${NEWLIB}"
197     make ${PARALLEL} || exit
198     log "Installing ${NEWLIB}"
199     make install || exit
200     cd ..
201     log "Cleaning up ${NEWLIB}"
202     touch ${STAMPS}/${NEWLIB}.build
203     rm -rf build/* ${NEWLIB}
204 fi
205
206 # Yes, you need to build gcc again!
207 if [ ! -e ${STAMPS}/${GCC}.build ]; then
208     log "Unpacking ${GCC}"
209     tar xfvj ${SOURCES}/${GCC}.tar.bz2
210     cd build
211     log "Configuring ${GCC}"
212     ../${GCC}/configure --target=${TARGET} \
213                       --prefix=${PREFIX} \
214                       --enable-interwork \
215                       --enable-multilib \
216                       --enable-languages="c,c++" \
217                       --with-newlib \
218                       --disable-shared \
219                       --with-gnu-as \
220                       --with-gnu-ld \
221                       --disable-nls \
222                       --disable-werror \
223                      ${GCCFLAGS} || exit
224     log "Building ${GCC}"
225     make ${PARALLEL} || exit
226     log "Installing ${GCC}"
227     make install || exit
228     cd ..
229     log "Cleaning up ${GCC}"
230     touch ${STAMPS}/${GCC}.build
231     rm -rf build/* ${GCC}
232 fi
233
234 if [ ! -e ${STAMPS}/${GDB}.build ]; then
235     log "Unpacking ${GDB}"
236     tar xfvj ${SOURCES}/${GDB}.tar.bz2
237     cd build
238     log "Configuring ${GDB}"
239     ../${GDB}/configure --target=${TARGET} \
240                       --prefix=${PREFIX} \
241                       --enable-interwork \
242                       --enable-multilib \
243                       --disable-werror \
244                       ${GDBFLAGS} || exit
245     log "Building ${GDB}"
246     make ${PARALLEL} || exit
247     log "Installing ${GDB}"
248     make install || exit
249     cd ..
250     log "Cleaning up ${GDB}"
251     touch ${STAMPS}/${GDB}.build
252     rm -rf build/* ${GDB}
253 fi
254
255 if [ ${LIBSTM32_EN} != 0 ]; then
256 if [ ! -e .libcmsis-${LIBCMSIS}.build ]; then
257     log "Unpacking libcmsis-${LIBCMSIS}"
258     tar xfvj ${SOURCES}/libcmsis-${LIBCMSIS}.tar.bz2
259     cd libcmsis-${LIBCMSIS}
260     log "Building libcmsis-${LIBCMSIS}"
261     make arch_prefix=${TARGET} prefix=${PREFIX} || exit
262     log "Installing libcmsis-${LIBCMSIS}"
263     make arch_prefix=${TARGET} prefix=${PREFIX} install || exit
264     cd ..
265     log "Cleaning up libcmsis-${LIBCMSIS}"
266     touch .libcmsis-${LIBCMSIS}.build
267     rm -rf libcmsis-${LIBCMSIS}
268 fi
269
270 if [ ! -e .libstm32-${LIBSTM32}.build ]; then
271     log "Unpacking libstm32-${LIBSTM32}"
272     tar xfvj ${SOURCES}/libstm32-${LIBSTM32}.tar.bz2
273     cd libstm32-${LIBSTM32}
274     log "Building libstm32-${LIBSTM32}"
275     make arch_prefix=${TARGET} prefix=${PREFIX} || exit
276     log "Installing libstm32-${LIBSTM32}"
277     make arch_prefix=${TARGET} prefix=${PREFIX} install || exit
278     cd ..
279     log "Cleaning up libstm32-${LIBSTM32}"
280     touch .libstm32-${LIBSTM32}.build
281     rm -rf libstm32-${LIBSTM32}
282 fi
283
284 if [ ! -e .libstm32usb-${LIBSTM32USB}.build ]; then
285     log "Unpacking libstm32usb-${LIBSTM32USB}"
286     tar xfvj ${SOURCES}/libstm32usb-${LIBSTM32USB}.tar.bz2
287     cd libstm32usb-${LIBSTM32USB}
288     log "Building libstm32usb-${LIBSTM32USB}"
289     make arch_prefix=${TARGET} prefix=${PREFIX} || exit
290     log "Installing libstm32usb-${LIBSTM32USB}"
291     make arch_prefix=${TARGET} prefix=${PREFIX} install || exit
292     cd ..
293     log "Cleaning up libstm32usb-${LIBSTM32USB}"
294     touch .libstm32usb-${LIBSTM32USB}.build
295     rm -rf libstm32usb-${LIBSTM32USB}
296 fi
297 fi
298
299 if [ $LIBOPENSTM32_EN != 0 ]; then
300     log "Unpacking libopenstm32-${LIBOPENSTM32}"
301     tar xfvj ${SOURCES}/libopenstm32-${LIBOPENSTM32}.tar.bz2
302     cd libopenstm32-${LIBOPENSTM32}
303     log "Building libopenstm32-${LIBOPENSTM32}"
304     make PREFIX=${TARGET} DESTDIR=${PREFIX} || exit
305     log "Installing libopenstm32-${LIBOPENSTM32}"
306     make PREFIX=${TARGET} DESTDIR=${PREFIX} install || exit
307     cd ..
308     log "Cleaning up libopenstm32-${LIBOPENSTM32}"
309     touch .libopenstm32-${LIBOPENSTM32}.build
310     rm -rf libopenstm32-${LIBOPENSTM32}
311 fi