source: mainline/tools/toolchain.sh@ 603c8740

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 603c8740 was 603c8740, checked in by Martin Decky <martin@…>, 14 years ago
  • bump to GCC 4.6 and binutils 2.21
  • add support for parallel toolchain builds
  • change default CROSS_PREFIX to /usr/local/cross
  • Property mode set to 100755
File size: 10.5 KB
Line 
1#! /bin/bash
2
3#
4# Copyright (c) 2009 Martin Decky
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10#
11# - Redistributions of source code must retain the above copyright
12# notice, this list of conditions and the following disclaimer.
13# - Redistributions in binary form must reproduce the above copyright
14# notice, this list of conditions and the following disclaimer in the
15# documentation and/or other materials provided with the distribution.
16# - The name of the author may not be used to endorse or promote products
17# derived from this software without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29#
30
31GMP_MAIN=<<EOF
32#define GCC_GMP_VERSION_NUM(a, b, c) \
33 (((a) << 16L) | ((b) << 8) | (c))
34
35#define GCC_GMP_VERSION \
36 GCC_GMP_VERSION_NUM(__GNU_MP_VERSION, __GNU_MP_VERSION_MINOR, __GNU_MP_VERSION_PATCHLEVEL)
37
38#if GCC_GMP_VERSION < GCC_GMP_VERSION_NUM(4,3,2)
39 choke me
40#endif
41EOF
42
43MPFR_MAIN=<<EOF
44#if MPFR_VERSION < MPFR_VERSION_NUM(2, 4, 2)
45choke me
46 #endif
47EOF
48
49MPC_MAIN=<<EOF
50#if MPC_VERSION < MPC_VERSION_NUM(0, 8, 1)
51 choke me
52#endif
53EOF
54
55BINUTILS_VERSION="2.21"
56GCC_VERSION="4.6.0"
57
58BASEDIR="`pwd`"
59BINUTILS="binutils-${BINUTILS_VERSION}.tar.bz2"
60GCC_CORE="gcc-core-${GCC_VERSION}.tar.bz2"
61GCC_OBJC="gcc-objc-${GCC_VERSION}.tar.bz2"
62GCC_CPP="gcc-g++-${GCC_VERSION}.tar.bz2"
63
64#
65# Check if the library described in the argument
66# exists and has acceptable version.
67#
68check_dependency() {
69 DEPENDENCY="$1"
70 HEADER="$2"
71 BODY="$3"
72
73 FNAME="/tmp/conftest-$$"
74
75 echo "#include ${HEADER}" > "${FNAME}.c"
76 echo >> "${FNAME}.c"
77 echo "int main()" >> "${FNAME}.c"
78 echo "{" >> "${FNAME}.c"
79 echo "${BODY}" >> "${FNAME}.c"
80 echo " return 0;" >> "${FNAME}.c"
81 echo "}" >> "${FNAME}.c"
82
83 cc -c -o "${FNAME}.o" "${FNAME}.c" 2> "${FNAME}.log"
84 RC="$?"
85
86 if [ "$RC" -ne "0" ] ; then
87 echo " ${DEPENDENCY} not found, too old or compiler error."
88 echo " Please recheck manually the source file \"${FNAME}.c\"."
89 echo " The compilation of the toolchain is probably going to fail,"
90 echo " you have been warned."
91 echo
92 echo " ===== Compiler output ====="
93 cat "${FNAME}.log"
94 echo " ==========================="
95 echo
96 else
97 echo " ${DEPENDENCY} found"
98 rm -f "${FNAME}.log" "${FNAME}.o" "${FNAME}.c"
99 fi
100}
101
102check_dependecies() {
103 echo ">>> Basic dependency check"
104 check_dependency "GMP" "<gmp.h>" "${GMP_MAIN}"
105 check_dependency "MPFR" "<mpfr.h>" "${MPFR_MAIN}"
106 check_dependency "MPC" "<mpc.h>" "${MPC_MAIN}"
107 echo
108}
109
110check_error() {
111 if [ "$1" -ne "0" ]; then
112 echo
113 echo "Script failed: $2"
114
115 exit 1
116 fi
117}
118
119check_md5() {
120 FILE="$1"
121 SUM="$2"
122
123 COMPUTED="`md5sum "${FILE}" | cut -d' ' -f1`"
124 if [ "${SUM}" != "${COMPUTED}" ] ; then
125 echo
126 echo "Checksum of ${FILE} does not match."
127
128 exit 2
129 fi
130}
131
132show_usage() {
133 echo "Cross-compiler toolchain build script"
134 echo
135 echo "Syntax:"
136 echo " $0 <platform>"
137 echo
138 echo "Possible target platforms are:"
139 echo " amd64 AMD64 (x86-64, x64)"
140 echo " arm32 ARM"
141 echo " ia32 IA-32 (x86, i386)"
142 echo " ia64 IA-64 (Itanium)"
143 echo " mips32 MIPS little-endian 32b"
144 echo " mips32eb MIPS big-endian 32b"
145 echo " mips64 MIPS little-endian 64b"
146 echo " ppc32 32-bit PowerPC"
147 echo " ppc64 64-bit PowerPC"
148 echo " sparc64 SPARC V9"
149 echo " all build all targets"
150 echo " parallel same as 'all', but in parallel"
151 echo
152 echo "The toolchain will be installed to the directory specified by"
153 echo "the CROSS_PREFIX environment variable. If the variable is not"
154 echo "defined, /usr/local/cross will be used by default."
155 echo
156
157 exit 3
158}
159
160change_title() {
161 echo -en "\e]0;$1\a"
162}
163
164show_countdown() {
165 TM="$1"
166
167 if [ "${TM}" -eq 0 ] ; then
168 echo
169 return 0
170 fi
171
172 echo -n "${TM} "
173 change_title "${TM}"
174 sleep 1
175
176 TM="`expr "${TM}" - 1`"
177 show_countdown "${TM}"
178}
179
180show_dependencies() {
181 echo "IMPORTANT NOTICE:"
182 echo
183 echo "For a successful compilation and use of the cross-compiler"
184 echo "toolchain you need at least the following dependencies."
185 echo
186 echo "Please make sure that the dependencies are present in your"
187 echo "system. Otherwise the compilation process might fail after"
188 echo "a few seconds or minutes."
189 echo
190 echo " - SED, AWK, Flex, Bison, gzip, bzip2, Bourne Shell"
191 echo " - gettext, zlib, Texinfo, libelf, libgomp"
192 echo " - GNU Multiple Precision Library (GMP)"
193 echo " - GNU Make"
194 echo " - GNU tar"
195 echo " - GNU Coreutils"
196 echo " - GNU Sharutils"
197 echo " - MPFR"
198 echo " - MPC"
199 echo " - Parma Polyhedra Library (PPL)"
200 echo " - ClooG-PPL"
201 echo " - native C compiler, assembler and linker"
202 echo " - native C library with headers"
203 echo
204}
205
206download_fetch() {
207 SOURCE="$1"
208 FILE="$2"
209 CHECKSUM="$3"
210
211 if [ ! -f "${FILE}" ]; then
212 change_title "Downloading ${FILE}"
213 wget -c "${SOURCE}${FILE}"
214 check_error $? "Error downloading ${FILE}."
215 fi
216
217 check_md5 "${FILE}" "${CHECKSUM}"
218}
219
220source_check() {
221 FILE="$1"
222
223 if [ ! -f "${FILE}" ]; then
224 echo
225 echo "File ${FILE} not found."
226
227 exit 4
228 fi
229}
230
231cleanup_dir() {
232 DIR="$1"
233
234 if [ -d "${DIR}" ]; then
235 change_title "Removing ${DIR}"
236 echo " >>> Removing ${DIR}"
237 rm -fr "${DIR}"
238 fi
239}
240
241create_dir() {
242 DIR="$1"
243 DESC="$2"
244
245 change_title "Creating ${DESC}"
246 echo ">>> Creating ${DESC}"
247
248 mkdir -p "${DIR}"
249 test -d "${DIR}"
250 check_error $? "Unable to create ${DIR}."
251}
252
253unpack_tarball() {
254 FILE="$1"
255 DESC="$2"
256
257 change_title "Unpacking ${DESC}"
258 echo " >>> Unpacking ${DESC}"
259
260 tar -xjf "${FILE}"
261 check_error $? "Error unpacking ${DESC}."
262}
263
264prepare() {
265 show_dependencies
266 check_dependecies
267 show_countdown 10
268
269 BINUTILS_SOURCE="ftp://ftp.gnu.org/gnu/binutils/"
270 GCC_SOURCE="ftp://ftp.gnu.org/gnu/gcc/gcc-${GCC_VERSION}/"
271
272 download_fetch "${BINUTILS_SOURCE}" "${BINUTILS}" "c84c5acc9d266f1a7044b51c85a823f5"
273 download_fetch "${GCC_SOURCE}" "${GCC_CORE}" "b1957f3209080b2f55bc3756d3a62b7c"
274 download_fetch "${GCC_SOURCE}" "${GCC_OBJC}" "120d4675366ee82ea52f9ed65b57da04"
275 download_fetch "${GCC_SOURCE}" "${GCC_CPP}" "a30090fa655d0db4c970740d353c81f1"
276}
277
278build_target() {
279 PLATFORM="$1"
280 TARGET="$2"
281
282 WORKDIR="${BASEDIR}/${PLATFORM}"
283 BINUTILSDIR="${WORKDIR}/binutils-${BINUTILS_VERSION}"
284 GCCDIR="${WORKDIR}/gcc-${GCC_VERSION}"
285 OBJDIR="${WORKDIR}/gcc-obj"
286
287 if [ -z "${CROSS_PREFIX}" ] ; then
288 CROSS_PREFIX="/usr/local/cross"
289 fi
290
291 PREFIX="${CROSS_PREFIX}/${PLATFORM}"
292
293 echo ">>> Downloading tarballs"
294 source_check "${BASEDIR}/${BINUTILS}"
295 source_check "${BASEDIR}/${GCC_CORE}"
296 source_check "${BASEDIR}/${GCC_OBJC}"
297 source_check "${BASEDIR}/${GCC_CPP}"
298
299 echo ">>> Removing previous content"
300 cleanup_dir "${PREFIX}"
301 cleanup_dir "${WORKDIR}"
302
303 create_dir "${PREFIX}" "destination directory"
304 create_dir "${OBJDIR}" "GCC object directory"
305
306 echo ">>> Unpacking tarballs"
307 cd "${WORKDIR}"
308 check_error $? "Change directory failed."
309
310 unpack_tarball "${BASEDIR}/${BINUTILS}" "binutils"
311 unpack_tarball "${BASEDIR}/${GCC_CORE}" "GCC Core"
312 unpack_tarball "${BASEDIR}/${GCC_OBJC}" "Objective C"
313 unpack_tarball "${BASEDIR}/${GCC_CPP}" "C++"
314
315 echo ">>> Processing binutils (${PLATFORM})"
316 cd "${BINUTILSDIR}"
317 check_error $? "Change directory failed."
318
319 change_title "binutils: configure (${PLATFORM})"
320 ./configure "--target=${TARGET}" "--prefix=${PREFIX}" "--program-prefix=${TARGET}-" --disable-nls
321 check_error $? "Error configuring binutils."
322
323 change_title "binutils: make (${PLATFORM})"
324 make all install
325 check_error $? "Error compiling/installing binutils."
326
327 echo ">>> Processing GCC (${PLATFORM})"
328 cd "${OBJDIR}"
329 check_error $? "Change directory failed."
330
331 change_title "GCC: configure (${PLATFORM})"
332 "${GCCDIR}/configure" "--target=${TARGET}" "--prefix=${PREFIX}" "--program-prefix=${TARGET}-" --with-gnu-as --with-gnu-ld --disable-nls --disable-threads --enable-languages=c,objc,c++,obj-c++ --disable-multilib --disable-libgcj --without-headers --disable-shared --enable-lto
333 check_error $? "Error configuring GCC."
334
335 change_title "GCC: make (${PLATFORM})"
336 PATH="${PATH}:${PREFIX}/bin" make all-gcc install-gcc
337 check_error $? "Error compiling/installing GCC."
338
339 cd "${BASEDIR}"
340 check_error $? "Change directory failed."
341
342 echo ">>> Cleaning up"
343 cleanup_dir "${WORKDIR}"
344
345 echo
346 echo ">>> Cross-compiler for ${TARGET} installed."
347}
348
349if [ "$#" -lt "1" ]; then
350 show_usage
351fi
352
353case "$1" in
354 "amd64")
355 prepare
356 build_target "amd64" "amd64-linux-gnu"
357 ;;
358 "arm32")
359 prepare
360 build_target "arm32" "arm-linux-gnu"
361 ;;
362 "ia32")
363 prepare
364 build_target "ia32" "i686-pc-linux-gnu"
365 ;;
366 "ia64")
367 prepare
368 build_target "ia64" "ia64-pc-linux-gnu"
369 ;;
370 "mips32")
371 prepare
372 build_target "mips32" "mipsel-linux-gnu"
373 ;;
374 "mips32eb")
375 prepare
376 build_target "mips32eb" "mips-linux-gnu"
377 ;;
378 "mips64")
379 prepare
380 build_target "mips64" "mips64el-linux-gnu"
381 ;;
382 "ppc32")
383 prepare
384 build_target "ppc32" "ppc-linux-gnu"
385 ;;
386 "ppc64")
387 prepare
388 build_target "ppc64" "ppc64-linux-gnu"
389 ;;
390 "sparc64")
391 prepare
392 build_target "sparc64" "sparc64-linux-gnu"
393 ;;
394 "all")
395 prepare
396 build_target "amd64" "amd64-linux-gnu"
397 build_target "arm32" "arm-linux-gnu"
398 build_target "ia32" "i686-pc-linux-gnu"
399 build_target "ia64" "ia64-pc-linux-gnu"
400 build_target "mips32" "mipsel-linux-gnu"
401 build_target "mips32eb" "mips-linux-gnu"
402 build_target "mips64" "mips64el-linux-gnu"
403 build_target "ppc32" "ppc-linux-gnu"
404 build_target "ppc64" "ppc64-linux-gnu"
405 build_target "sparc64" "sparc64-linux-gnu"
406 ;;
407 "parallel")
408 prepare
409 build_target "amd64" "amd64-linux-gnu" &
410 build_target "arm32" "arm-linux-gnu" &
411 build_target "ia32" "i686-pc-linux-gnu" &
412 build_target "ia64" "ia64-pc-linux-gnu" &
413 build_target "mips32" "mipsel-linux-gnu" &
414 build_target "mips32eb" "mips-linux-gnu" &
415 build_target "mips64" "mips64el-linux-gnu" &
416 build_target "ppc32" "ppc-linux-gnu" &
417 build_target "ppc64" "ppc64-linux-gnu" &
418 build_target "sparc64" "sparc64-linux-gnu" &
419 wait
420 ;;
421 *)
422 show_usage
423 ;;
424esac
Note: See TracBrowser for help on using the repository browser.