source: mainline/tools/toolchain.sh@ 3c9646b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3c9646b was 285589b, checked in by Martin Decky <martin@…>, 12 years ago

make sure that CROSS_PREFIX is set to some sane value

  • Property mode set to 100755
File size: 11.9 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)
45 choke 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.23.1"
56BINUTILS_RELEASE=""
57GCC_VERSION="4.8.1"
58GDB_VERSION="7.6"
59
60BASEDIR="`pwd`"
61BINUTILS="binutils-${BINUTILS_VERSION}${BINUTILS_RELEASE}.tar.bz2"
62GCC="gcc-${GCC_VERSION}.tar.bz2"
63GDB="gdb-${GDB_VERSION}.tar.bz2"
64
65#
66# Check if the library described in the argument
67# exists and has acceptable version.
68#
69check_dependency() {
70 DEPENDENCY="$1"
71 HEADER="$2"
72 BODY="$3"
73
74 FNAME="/tmp/conftest-$$"
75
76 echo "#include ${HEADER}" > "${FNAME}.c"
77 echo >> "${FNAME}.c"
78 echo "int main()" >> "${FNAME}.c"
79 echo "{" >> "${FNAME}.c"
80 echo "${BODY}" >> "${FNAME}.c"
81 echo " return 0;" >> "${FNAME}.c"
82 echo "}" >> "${FNAME}.c"
83
84 cc -c -o "${FNAME}.o" "${FNAME}.c" 2> "${FNAME}.log"
85 RC="$?"
86
87 if [ "$RC" -ne "0" ] ; then
88 echo " ${DEPENDENCY} not found, too old or compiler error."
89 echo " Please recheck manually the source file \"${FNAME}.c\"."
90 echo " The compilation of the toolchain is probably going to fail,"
91 echo " you have been warned."
92 echo
93 echo " ===== Compiler output ====="
94 cat "${FNAME}.log"
95 echo " ==========================="
96 echo
97 else
98 echo " ${DEPENDENCY} found"
99 rm -f "${FNAME}.log" "${FNAME}.o" "${FNAME}.c"
100 fi
101}
102
103check_dependecies() {
104 echo ">>> Basic dependency check"
105 check_dependency "GMP" "<gmp.h>" "${GMP_MAIN}"
106 check_dependency "MPFR" "<mpfr.h>" "${MPFR_MAIN}"
107 check_dependency "MPC" "<mpc.h>" "${MPC_MAIN}"
108 echo
109}
110
111check_error() {
112 if [ "$1" -ne "0" ]; then
113 echo
114 echo "Script failed: $2"
115
116 exit 1
117 fi
118}
119
120check_md5() {
121 FILE="$1"
122 SUM="$2"
123
124 COMPUTED="`md5sum "${FILE}" | cut -d' ' -f1`"
125 if [ "${SUM}" != "${COMPUTED}" ] ; then
126 echo
127 echo "Checksum of ${FILE} does not match."
128
129 exit 2
130 fi
131}
132
133show_usage() {
134 echo "Cross-compiler toolchain build script"
135 echo
136 echo "Syntax:"
137 echo " $0 <platform>"
138 echo
139 echo "Possible target platforms are:"
140 echo " amd64 AMD64 (x86-64, x64)"
141 echo " arm32 ARM"
142 echo " ia32 IA-32 (x86, i386)"
143 echo " ia64 IA-64 (Itanium)"
144 echo " mips32 MIPS little-endian 32b"
145 echo " mips32eb MIPS big-endian 32b"
146 echo " mips64 MIPS little-endian 64b"
147 echo " ppc32 32-bit PowerPC"
148 echo " ppc64 64-bit PowerPC"
149 echo " sparc64 SPARC V9"
150 echo " all build all targets"
151 echo " parallel same as 'all', but all in parallel"
152 echo " 2-way same as 'all', but 2-way parallel"
153 echo
154 echo "The toolchain will be installed to the directory specified by"
155 echo "the CROSS_PREFIX environment variable. If the variable is not"
156 echo "defined, /usr/local/cross will be used by default."
157 echo
158
159 exit 3
160}
161
162change_title() {
163 echo -en "\e]0;$1\a"
164}
165
166show_countdown() {
167 TM="$1"
168
169 if [ "${TM}" -eq 0 ] ; then
170 echo
171 return 0
172 fi
173
174 echo -n "${TM} "
175 change_title "${TM}"
176 sleep 1
177
178 TM="`expr "${TM}" - 1`"
179 show_countdown "${TM}"
180}
181
182show_dependencies() {
183 echo "IMPORTANT NOTICE:"
184 echo
185 echo "For a successful compilation and use of the cross-compiler"
186 echo "toolchain you need at least the following dependencies."
187 echo
188 echo "Please make sure that the dependencies are present in your"
189 echo "system. Otherwise the compilation process might fail after"
190 echo "a few seconds or minutes."
191 echo
192 echo " - SED, AWK, Flex, Bison, gzip, bzip2, Bourne Shell"
193 echo " - gettext, zlib, Texinfo, libelf, libgomp"
194 echo " - terminfo"
195 echo " - GNU Multiple Precision Library (GMP)"
196 echo " - GNU Make"
197 echo " - GNU tar"
198 echo " - GNU Coreutils"
199 echo " - GNU Sharutils"
200 echo " - MPFR"
201 echo " - MPC"
202 echo " - Parma Polyhedra Library (PPL)"
203 echo " - ClooG-PPL"
204 echo " - native C compiler, assembler and linker"
205 echo " - native C library with headers"
206 echo
207}
208
209download_fetch() {
210 SOURCE="$1"
211 FILE="$2"
212 CHECKSUM="$3"
213
214 if [ ! -f "${FILE}" ]; then
215 change_title "Downloading ${FILE}"
216 wget -c "${SOURCE}${FILE}"
217 check_error $? "Error downloading ${FILE}."
218 fi
219
220 check_md5 "${FILE}" "${CHECKSUM}"
221}
222
223source_check() {
224 FILE="$1"
225
226 if [ ! -f "${FILE}" ]; then
227 echo
228 echo "File ${FILE} not found."
229
230 exit 4
231 fi
232}
233
234cleanup_dir() {
235 DIR="$1"
236
237 if [ -d "${DIR}" ]; then
238 change_title "Removing ${DIR}"
239 echo " >>> Removing ${DIR}"
240 rm -fr "${DIR}"
241 fi
242}
243
244create_dir() {
245 DIR="$1"
246 DESC="$2"
247
248 change_title "Creating ${DESC}"
249 echo ">>> Creating ${DESC}"
250
251 mkdir -p "${DIR}"
252 test -d "${DIR}"
253 check_error $? "Unable to create ${DIR}."
254}
255
256check_dirs() {
257 OUTSIDE="$1"
258 BASE="$2"
259 ORIGINAL="`pwd`"
260
261 cd "${OUTSIDE}"
262 check_error $? "Unable to change directory to ${OUTSIDE}."
263 ABS_OUTSIDE="`pwd`"
264
265 cd "${BASE}"
266 check_error $? "Unable to change directory to ${BASE}."
267 ABS_BASE="`pwd`"
268
269 cd "${ORIGINAL}"
270 check_error $? "Unable to change directory to ${ORIGINAL}."
271
272 BASE_LEN="${#ABS_BASE}"
273 OUTSIDE_TRIM="${ABS_OUTSIDE:0:${BASE_LEN}}"
274
275 if [ "${OUTSIDE_TRIM}" == "${ABS_BASE}" ] ; then
276 echo
277 echo "CROSS_PREFIX cannot reside within the working directory."
278
279 exit 5
280 fi
281}
282
283unpack_tarball() {
284 FILE="$1"
285 DESC="$2"
286
287 change_title "Unpacking ${DESC}"
288 echo " >>> Unpacking ${DESC}"
289
290 tar -xjf "${FILE}"
291 check_error $? "Error unpacking ${DESC}."
292}
293
294prepare() {
295 show_dependencies
296 check_dependecies
297 show_countdown 10
298
299 BINUTILS_SOURCE="ftp://ftp.gnu.org/gnu/binutils/"
300 GCC_SOURCE="ftp://ftp.gnu.org/gnu/gcc/gcc-${GCC_VERSION}/"
301 GDB_SOURCE="ftp://ftp.gnu.org/gnu/gdb/"
302
303 download_fetch "${BINUTILS_SOURCE}" "${BINUTILS}" "33adb18c3048d057ac58d07a3f1adb38"
304 download_fetch "${GCC_SOURCE}" "${GCC}" "3b2386c114cd74185aa3754b58a79304"
305 download_fetch "${GDB_SOURCE}" "${GDB}" "fda57170e4d11cdde74259ca575412a8"
306}
307
308build_target() {
309 PLATFORM="$1"
310 TARGET="$2"
311
312 WORKDIR="${BASEDIR}/${PLATFORM}"
313 BINUTILSDIR="${WORKDIR}/binutils-${BINUTILS_VERSION}"
314 GCCDIR="${WORKDIR}/gcc-${GCC_VERSION}"
315 OBJDIR="${WORKDIR}/gcc-obj"
316 GDBDIR="${WORKDIR}/gdb-${GDB_VERSION}"
317
318 if [ -z "${CROSS_PREFIX}" ] ; then
319 CROSS_PREFIX="/usr/local/cross"
320 fi
321
322 PREFIX="${CROSS_PREFIX}/${PLATFORM}"
323
324 echo ">>> Downloading tarballs"
325 source_check "${BASEDIR}/${BINUTILS}"
326 source_check "${BASEDIR}/${GCC}"
327 source_check "${BASEDIR}/${GDB}"
328
329 echo ">>> Removing previous content"
330 cleanup_dir "${PREFIX}"
331 cleanup_dir "${WORKDIR}"
332
333 create_dir "${PREFIX}" "destination directory"
334 create_dir "${OBJDIR}" "GCC object directory"
335
336 check_dirs "${PREFIX}" "${WORKDIR}"
337
338 echo ">>> Unpacking tarballs"
339 cd "${WORKDIR}"
340 check_error $? "Change directory failed."
341
342 unpack_tarball "${BASEDIR}/${BINUTILS}" "binutils"
343 unpack_tarball "${BASEDIR}/${GCC}" "GCC"
344 unpack_tarball "${BASEDIR}/${GDB}" "GDB"
345
346 echo ">>> Processing binutils (${PLATFORM})"
347 cd "${BINUTILSDIR}"
348 check_error $? "Change directory failed."
349
350 change_title "binutils: configure (${PLATFORM})"
351 CFLAGS=-Wno-error ./configure "--target=${TARGET}" "--prefix=${PREFIX}" "--program-prefix=${TARGET}-" --disable-nls --disable-werror
352 check_error $? "Error configuring binutils."
353
354 change_title "binutils: make (${PLATFORM})"
355 make all install
356 check_error $? "Error compiling/installing binutils."
357
358 echo ">>> Processing GCC (${PLATFORM})"
359 cd "${OBJDIR}"
360 check_error $? "Change directory failed."
361
362 change_title "GCC: configure (${PLATFORM})"
363 "${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 --disable-werror
364 check_error $? "Error configuring GCC."
365
366 change_title "GCC: make (${PLATFORM})"
367 PATH="${PATH}:${PREFIX}/bin" make all-gcc install-gcc
368 check_error $? "Error compiling/installing GCC."
369
370 echo ">>> Processing GDB (${PLATFORM})"
371 cd "${GDBDIR}"
372 check_error $? "Change directory failed."
373
374 change_title "GDB: configure (${PLATFORM})"
375 ./configure "--target=${TARGET}" "--prefix=${PREFIX}" "--program-prefix=${TARGET}-"
376 check_error $? "Error configuring GDB."
377
378 change_title "GDB: make (${PLATFORM})"
379 make all install
380 check_error $? "Error compiling/installing GDB."
381
382 cd "${BASEDIR}"
383 check_error $? "Change directory failed."
384
385 echo ">>> Cleaning up"
386 cleanup_dir "${WORKDIR}"
387
388 echo
389 echo ">>> Cross-compiler for ${TARGET} installed."
390}
391
392if [ "$#" -lt "1" ]; then
393 show_usage
394fi
395
396case "$1" in
397 "amd64")
398 prepare
399 build_target "amd64" "amd64-linux-gnu"
400 ;;
401 "arm32")
402 prepare
403 build_target "arm32" "arm-linux-gnueabi"
404 ;;
405 "ia32")
406 prepare
407 build_target "ia32" "i686-pc-linux-gnu"
408 ;;
409 "ia64")
410 prepare
411 build_target "ia64" "ia64-pc-linux-gnu"
412 ;;
413 "mips32")
414 prepare
415 build_target "mips32" "mipsel-linux-gnu"
416 ;;
417 "mips32eb")
418 prepare
419 build_target "mips32eb" "mips-linux-gnu"
420 ;;
421 "mips64")
422 prepare
423 build_target "mips64" "mips64el-linux-gnu"
424 ;;
425 "ppc32")
426 prepare
427 build_target "ppc32" "ppc-linux-gnu"
428 ;;
429 "ppc64")
430 prepare
431 build_target "ppc64" "ppc64-linux-gnu"
432 ;;
433 "sparc64")
434 prepare
435 build_target "sparc64" "sparc64-linux-gnu"
436 ;;
437 "all")
438 prepare
439 build_target "amd64" "amd64-linux-gnu"
440 build_target "arm32" "arm-linux-gnueabi"
441 build_target "ia32" "i686-pc-linux-gnu"
442 build_target "ia64" "ia64-pc-linux-gnu"
443 build_target "mips32" "mipsel-linux-gnu"
444 build_target "mips32eb" "mips-linux-gnu"
445 build_target "mips64" "mips64el-linux-gnu"
446 build_target "ppc32" "ppc-linux-gnu"
447 build_target "ppc64" "ppc64-linux-gnu"
448 build_target "sparc64" "sparc64-linux-gnu"
449 ;;
450 "parallel")
451 prepare
452 build_target "amd64" "amd64-linux-gnu" &
453 build_target "arm32" "arm-linux-gnueabi" &
454 build_target "ia32" "i686-pc-linux-gnu" &
455 build_target "ia64" "ia64-pc-linux-gnu" &
456 build_target "mips32" "mipsel-linux-gnu" &
457 build_target "mips32eb" "mips-linux-gnu" &
458 build_target "mips64" "mips64el-linux-gnu" &
459 build_target "ppc32" "ppc-linux-gnu" &
460 build_target "ppc64" "ppc64-linux-gnu" &
461 build_target "sparc64" "sparc64-linux-gnu" &
462 wait
463 ;;
464 "2-way")
465 prepare
466 build_target "amd64" "amd64-linux-gnu" &
467 build_target "arm32" "arm-linux-gnueabi" &
468 wait
469
470 build_target "ia32" "i686-pc-linux-gnu" &
471 build_target "ia64" "ia64-pc-linux-gnu" &
472 wait
473
474 build_target "mips32" "mipsel-linux-gnu" &
475 build_target "mips32eb" "mips-linux-gnu" &
476 wait
477
478 build_target "mips64" "mips64el-linux-gnu" &
479 build_target "ppc32" "ppc-linux-gnu" &
480 wait
481
482 build_target "ppc64" "ppc64-linux-gnu" &
483 build_target "sparc64" "sparc64-linux-gnu" &
484 wait
485 ;;
486 *)
487 show_usage
488 ;;
489esac
Note: See TracBrowser for help on using the repository browser.