Pulled unpacking out into a function
[summon-arm-toolchain.git] / 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 # Unpack an archive
55 function unpack {
56     # Use 'auto' mode decompression.  Replace with a switch if tar doesn't support -a
57     tar xvaf $1
58 }
59
60 case "$(uname)" in
61         Linux)
62         echo "Found Linux OS."
63         ;;
64         Darwin)
65         echo "Found Darwin OS."
66         GCCFLAGS="${GCCFLAGS} \
67                   --with-gmp=${DARWIN_OPT_PATH} \
68                   --with-mpfr=${DARWIN_OPT_PATH} \
69                   --with-mpc=${DARWIN_OPT_PATH} \
70                   -with-libiconv-prefix=${DARWIN_OPT_PATH}"
71         ;;
72         *)
73         echo "Found unknown OS. Aborting!"
74         exit 1
75         ;;
76 esac
77
78 mkdir -p ${STAMPS} ${SOURCES}
79
80 cd ${SOURCES}
81
82 fetch ${BINUTILS} http://ftp.gnu.org/gnu/binutils/${BINUTILS}.tar.bz2
83 fetch ${GCC} ${GCC_URL}
84 fetch ${NEWLIB} ftp://sources.redhat.com/pub/newlib/${NEWLIB}.tar.gz
85 fetch ${GDB} http://ftp.gnu.org/gnu/gdb/${GDB}.tar.bz2
86
87 if [ ${LIBSTM32_EN} != 0 ]; then
88 if [ ! -e libcmsis-${LIBCMSIS}.tar.bz2 ]; then
89         echo "Cloning libcmsis sources..."
90         git clone git://git.open-bldc.org/libcmsis.git
91         cd libcmsis
92         git archive --format=tar --prefix=libcmsis-${LIBCMSIS}/ ${LIBCMSIS} | \
93             bzip2 --stdout > ../libcmsis-${LIBCMSIS}.tar.bz2
94         cd ..
95         rm -rf libcmsis
96 fi
97
98 if [ ! -e libstm32-${LIBSTM32}.tar.bz2 ]; then
99         echo "Cloning libstm32 sources..."
100         git clone git://git.open-bldc.org/libstm32.git
101         cd libstm32
102         git archive --format=tar --prefix=libstm32-${LIBSTM32}/ ${LIBSTM32} | \
103             bzip2 --stdout > ../libstm32-${LIBSTM32}.tar.bz2
104         cd ..
105         rm -rf libstm32
106 fi
107
108 if [ ! -e libstm32usb-${LIBSTM32USB}.tar.bz2 ]; then
109         echo "Cloning libstm32usb sources..."
110         git clone git://git.open-bldc.org/libstm32usb.git
111         cd libstm32usb
112         git archive --format=tar --prefix=libstm32usb-${LIBSTM32USB}/ ${LIBSTM32USB} | \
113             bzip2 --stdout > ../libstm32usb-${LIBSTM32USB}.tar.bz2
114         cd ..
115         rm -rf libstm32usb
116 fi
117 fi
118
119 if [ ${LIBOPENSTM32_EN} != 0 ]; then
120 if [ ! -e libopenstm32-${LIBOPENSTM32}.tar.bz2 ]; then
121         echo "Cloning libopenstm32 sources..."
122         git clone git://libopenstm32.git.sourceforge.net/gitroot/libopenstm32/libopenstm32
123         cd libopenstm32
124         git archive --format=tar --prefix=libopenstm32-${LIBOPENSTM32}/ ${LIBOPENSTM32} | \
125             bzip2 --stdout > ../libopenstm32-${LIBOPENSTM32}.tar.bz2
126         cd ..
127         rm -rf libopenstm32
128 fi
129 fi
130
131 cd ${SUMMON_DIR}
132
133 if [ ! -e build ]; then
134     mkdir build
135 fi
136
137 if [ ! -e ${STAMPS}/${BINUTILS}.build ]; then
138     log "Unpacking ${BINUTILS}"
139     unpack ${SOURCES}/${BINUTILS}.tar.bz2
140     cd build
141     log "Configuring ${BINUTILS}"
142     ../${BINUTILS}/configure --target=${TARGET} \
143                            --prefix=${PREFIX} \
144                            --enable-interwork \
145                            --enable-multilib \
146                            --with-gnu-as \
147                            --with-gnu-ld \
148                            --disable-nls \
149                            --disable-werror \
150                            ${BINUTILFLAGS} || exit
151     log "Building ${BINUTILS}"
152     make ${PARALLEL} || exit
153     log "Installing ${BINUTILS}"
154     make install || exit
155     cd ..
156     log "Cleaning up ${BINUTILS}"
157     touch ${STAMPS}/${BINUTILS}.build
158     rm -rf build/* ${BINUTILS}
159 fi
160
161 if [ ! -e ${STAMPS}/${GCC}-boot.build ]; then
162     log "Unpacking ${GCC}-boot"
163     unpack ${SOURCES}/${GCC}.tar.bz2
164     cd build
165     log "Configuring ${GCC}-boot"
166     ../${GCC}/configure --target=${TARGET} \
167                       --prefix=${PREFIX} \
168                       --enable-interwork \
169                       --enable-multilib \
170                       --enable-languages="c" \
171                       --with-newlib \
172                       --without-headers \
173                       --disable-shared \
174                       --with-gnu-as \
175                       --with-gnu-ld \
176                       --disable-nls \
177                       --disable-werror \
178                       ${GCCFLAGS} || exit
179     log "Building ${GCC}-boot"
180     make ${PARALLEL} all-gcc || exit
181     log "Installing ${GCC}-boot"
182     make install-gcc || exit
183     cd ..
184     log "Cleaning up ${GCC}-boot"
185     touch ${STAMPS}/${GCC}-boot.build
186     rm -rf build/* ${GCC}
187 fi
188
189 if [ ! -e ${STAMPS}/${NEWLIB}.build ]; then
190     log "Unpacking ${NEWLIB}"
191     unpack ${SOURCES}/${NEWLIB}.tar.gz
192     cd build
193     log "Configuring ${NEWLIB}"
194     ../${NEWLIB}/configure --target=${TARGET} \
195                          --prefix=${PREFIX} \
196                          --enable-interwork \
197                          --enable-multilib \
198                          --with-gnu-as \
199                          --with-gnu-ld \
200                          --disable-nls \
201                          --disable-werror \
202                          --disable-newlib-supplied-syscalls || exit
203     log "Building ${NEWLIB}"
204     make ${PARALLEL} || exit
205     log "Installing ${NEWLIB}"
206     make install || exit
207     cd ..
208     log "Cleaning up ${NEWLIB}"
209     touch ${STAMPS}/${NEWLIB}.build
210     rm -rf build/* ${NEWLIB}
211 fi
212
213 # Yes, you need to build gcc again!
214 if [ ! -e ${STAMPS}/${GCC}.build ]; then
215     log "Unpacking ${GCC}"
216     unpack ${SOURCES}/${GCC}.tar.bz2
217     cd build
218     log "Configuring ${GCC}"
219     ../${GCC}/configure --target=${TARGET} \
220                       --prefix=${PREFIX} \
221                       --enable-interwork \
222                       --enable-multilib \
223                       --enable-languages="c,c++" \
224                       --with-newlib \
225                       --disable-shared \
226                       --with-gnu-as \
227                       --with-gnu-ld \
228                       --disable-nls \
229                       --disable-werror \
230                      ${GCCFLAGS} || exit
231     log "Building ${GCC}"
232     make ${PARALLEL} || exit
233     log "Installing ${GCC}"
234     make install || exit
235     cd ..
236     log "Cleaning up ${GCC}"
237     touch ${STAMPS}/${GCC}.build
238     rm -rf build/* ${GCC}
239 fi
240
241 if [ ! -e ${STAMPS}/${GDB}.build ]; then
242     log "Unpacking ${GDB}"
243     unpack ${SOURCES}/${GDB}.tar.bz2
244     cd build
245     log "Configuring ${GDB}"
246     ../${GDB}/configure --target=${TARGET} \
247                       --prefix=${PREFIX} \
248                       --enable-interwork \
249                       --enable-multilib \
250                       --disable-werror \
251                       ${GDBFLAGS} || exit
252     log "Building ${GDB}"
253     make ${PARALLEL} || exit
254     log "Installing ${GDB}"
255     make install || exit
256     cd ..
257     log "Cleaning up ${GDB}"
258     touch ${STAMPS}/${GDB}.build
259     rm -rf build/* ${GDB}
260 fi
261
262 if [ ${LIBSTM32_EN} != 0 ]; then
263 if [ ! -e .libcmsis-${LIBCMSIS}.build ]; then
264     log "Unpacking libcmsis-${LIBCMSIS}"
265     unpack ${SOURCES}/libcmsis-${LIBCMSIS}.tar.bz2
266     cd libcmsis-${LIBCMSIS}
267     log "Building libcmsis-${LIBCMSIS}"
268     make arch_prefix=${TARGET} prefix=${PREFIX} || exit
269     log "Installing libcmsis-${LIBCMSIS}"
270     make arch_prefix=${TARGET} prefix=${PREFIX} install || exit
271     cd ..
272     log "Cleaning up libcmsis-${LIBCMSIS}"
273     touch .libcmsis-${LIBCMSIS}.build
274     rm -rf libcmsis-${LIBCMSIS}
275 fi
276
277 if [ ! -e .libstm32-${LIBSTM32}.build ]; then
278     log "Unpacking libstm32-${LIBSTM32}"
279     unpack ${SOURCES}/libstm32-${LIBSTM32}.tar.bz2
280     cd libstm32-${LIBSTM32}
281     log "Building libstm32-${LIBSTM32}"
282     make arch_prefix=${TARGET} prefix=${PREFIX} || exit
283     log "Installing libstm32-${LIBSTM32}"
284     make arch_prefix=${TARGET} prefix=${PREFIX} install || exit
285     cd ..
286     log "Cleaning up libstm32-${LIBSTM32}"
287     touch .libstm32-${LIBSTM32}.build
288     rm -rf libstm32-${LIBSTM32}
289 fi
290
291 if [ ! -e .libstm32usb-${LIBSTM32USB}.build ]; then
292     log "Unpacking libstm32usb-${LIBSTM32USB}"
293     unpack ${SOURCES}/libstm32usb-${LIBSTM32USB}.tar.bz2
294     cd libstm32usb-${LIBSTM32USB}
295     log "Building libstm32usb-${LIBSTM32USB}"
296     make arch_prefix=${TARGET} prefix=${PREFIX} || exit
297     log "Installing libstm32usb-${LIBSTM32USB}"
298     make arch_prefix=${TARGET} prefix=${PREFIX} install || exit
299     cd ..
300     log "Cleaning up libstm32usb-${LIBSTM32USB}"
301     touch .libstm32usb-${LIBSTM32USB}.build
302     rm -rf libstm32usb-${LIBSTM32USB}
303 fi
304 fi
305
306 if [ $LIBOPENSTM32_EN != 0 ]; then
307     log "Unpacking libopenstm32-${LIBOPENSTM32}"
308     unpack ${SOURCES}/libopenstm32-${LIBOPENSTM32}.tar.bz2
309     cd libopenstm32-${LIBOPENSTM32}
310     log "Building libopenstm32-${LIBOPENSTM32}"
311     make PREFIX=${TARGET} DESTDIR=${PREFIX} || exit
312     log "Installing libopenstm32-${LIBOPENSTM32}"
313     make PREFIX=${TARGET} DESTDIR=${PREFIX} install || exit
314     cd ..
315     log "Cleaning up libopenstm32-${LIBOPENSTM32}"
316     touch .libopenstm32-${LIBOPENSTM32}.build
317     rm -rf libopenstm32-${LIBOPENSTM32}
318 fi