source: mainline/tools/toolchain.sh@ 6c9f1a6

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 6c9f1a6 was 6c9f1a6, checked in by Vojtech Horky <vojtechhorky@…>, 12 years ago

Toolchain script refactoring

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