4 # Copyright (c) 2009 Martin Decky
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
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.
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.
32 #define GCC_GMP_VERSION_NUM(a, b, c) \
33 (((a) << 16L) | ((b) << 8) | (c))
35 #define GCC_GMP_VERSION \
36 GCC_GMP_VERSION_NUM(__GNU_MP_VERSION, __GNU_MP_VERSION_MINOR, __GNU_MP_VERSION_PATCHLEVEL)
38 #if GCC_GMP_VERSION < GCC_GMP_VERSION_NUM(4, 3, 2)
44 #if MPFR_VERSION < MPFR_VERSION_NUM(2, 4, 2)
50 #if MPC_VERSION < MPC_VERSION_NUM(0, 8, 1)
55 BINUTILS_VERSION
="2.23.1"
57 BINUTILS_PATCHES
="toolchain-binutils-2.23.1.patch"
59 GCC_PATCHES
="toolchain-gcc-4.8.1-targets.patch toolchain-gcc-4.8.1-headers.patch"
61 GDB_PATCHES
="toolchain-gdb-7.6.1.patch"
64 SRCDIR
="$(readlink -f $(dirname "$0"))"
65 BINUTILS
="binutils-${BINUTILS_VERSION}${BINUTILS_RELEASE}.tar.bz2"
66 GCC
="gcc-${GCC_VERSION}.tar.bz2"
67 GDB
="gdb-${GDB_VERSION}.tar.bz2"
70 USE_HELENOS_TARGET
=false
71 INSTALL_DIR
="${BASEDIR}/PKG"
74 # Check if the library described in the argument
75 # exists and has acceptable version.
82 FNAME
="/tmp/conftest-$$"
84 echo "#include ${HEADER}" > "${FNAME}.c"
86 echo "int main()" >> "${FNAME}.c"
87 echo "{" >> "${FNAME}.c"
88 echo "${BODY}" >> "${FNAME}.c"
89 echo " return 0;" >> "${FNAME}.c"
90 echo "}" >> "${FNAME}.c"
92 cc
-c -o "${FNAME}.o" "${FNAME}.c" 2> "${FNAME}.log"
95 if [ "$RC" -ne "0" ] ; then
96 echo " ${DEPENDENCY} not found, too old or compiler error."
97 echo " Please recheck manually the source file \"${FNAME}.c\"."
98 echo " The compilation of the toolchain is probably going to fail,"
99 echo " you have been warned."
101 echo " ===== Compiler output ====="
103 echo " ==========================="
106 echo " ${DEPENDENCY} found"
107 rm -f "${FNAME}.log" "${FNAME}.o" "${FNAME}.c"
111 check_dependecies
() {
112 echo ">>> Basic dependency check"
113 check_dependency
"GMP" "<gmp.h>" "${GMP_MAIN}"
114 check_dependency
"MPFR" "<mpfr.h>" "${MPFR_MAIN}"
115 check_dependency
"MPC" "<mpc.h>" "${MPC_MAIN}"
120 if [ "$1" -ne "0" ]; then
122 echo "Script failed: $2"
132 COMPUTED
="`md5sum "${FILE}" | cut -d' ' -f1`"
133 if [ "${SUM}" != "${COMPUTED}" ] ; then
135 echo "Checksum of ${FILE} does not match."
142 echo "Cross-compiler toolchain build script"
145 echo " $0 [--no-install] [--helenos-target] <platform>"
147 echo "Possible target platforms are:"
148 echo " amd64 AMD64 (x86-64, x64)"
150 echo " ia32 IA-32 (x86, i386)"
151 echo " ia64 IA-64 (Itanium)"
152 echo " mips32 MIPS little-endian 32b"
153 echo " mips32eb MIPS big-endian 32b"
154 echo " mips64 MIPS little-endian 64b"
155 echo " ppc32 32-bit PowerPC"
156 echo " ppc64 64-bit PowerPC"
157 echo " sparc32 SPARC V8"
158 echo " sparc64 SPARC V9"
159 echo " all build all targets"
160 echo " parallel same as 'all', but all in parallel"
161 echo " 2-way same as 'all', but 2-way parallel"
163 echo "The toolchain is installed into directory specified by the"
164 echo "CROSS_PREFIX environment variable. If the variable is not"
165 echo "defined, /usr/local/cross/ is used as default."
167 echo "If --no-install is present, the toolchain still uses the"
168 echo "CROSS_PREFIX as the target directory but the installation"
169 echo "copies the files into PKG/ subdirectory without affecting"
170 echo "the actual root file system. That is only useful if you do"
171 echo "not want to run the script under the super user."
173 echo "The --helenos-target will build HelenOS-specific toolchain"
174 echo "(i.e. it will use *-helenos-* triplet instead of *-linux-*)."
175 echo "This toolchain is installed into /usr/local/cross-helenos by"
176 echo "default. The settings can be changed by setting environment"
177 echo "variable CROSS_HELENOS_PREFIX."
178 echo "Using the HelenOS-specific toolchain is still an experimental"
179 echo "feature that is not fully supported."
192 if [ "${TM}" -eq 0 ] ; then
201 TM
="`expr "${TM}" - 1`"
202 show_countdown
"${TM}"
205 show_dependencies
() {
206 echo "IMPORTANT NOTICE:"
208 echo "For a successful compilation and use of the cross-compiler"
209 echo "toolchain you need at least the following dependencies."
211 echo "Please make sure that the dependencies are present in your"
212 echo "system. Otherwise the compilation process might fail after"
213 echo "a few seconds or minutes."
215 echo " - SED, AWK, Flex, Bison, gzip, bzip2, Bourne Shell"
216 echo " - gettext, zlib, Texinfo, libelf, libgomp"
218 echo " - GNU Multiple Precision Library (GMP)"
221 echo " - GNU Coreutils"
222 echo " - GNU Sharutils"
225 echo " - Parma Polyhedra Library (PPL)"
227 echo " - native C compiler, assembler and linker"
228 echo " - native C library with headers"
237 if [ ! -f "${FILE}" ]; then
238 change_title
"Downloading ${FILE}"
239 wget
-c "${SOURCE}${FILE}"
240 check_error $?
"Error downloading ${FILE}."
243 check_md5
"${FILE}" "${CHECKSUM}"
249 if [ ! -f "${FILE}" ]; then
251 echo "File ${FILE} not found."
260 if [ -d "${DIR}" ]; then
261 change_title
"Removing ${DIR}"
262 echo " >>> Removing ${DIR}"
271 change_title
"Creating ${DESC}"
272 echo ">>> Creating ${DESC}"
276 check_error $?
"Unable to create ${DIR}."
285 check_error $?
"Unable to change directory to ${OUTSIDE}."
289 check_error $?
"Unable to change directory to ${BASE}."
293 check_error $?
"Unable to change directory to ${ORIGINAL}."
295 BASE_LEN
="${#ABS_BASE}"
296 OUTSIDE_TRIM
="${ABS_OUTSIDE:0:${BASE_LEN}}"
298 if [ "${OUTSIDE_TRIM}" == "${ABS_BASE}" ] ; then
300 echo "CROSS_PREFIX cannot reside within the working directory."
310 change_title
"Unpacking ${DESC}"
311 echo " >>> Unpacking ${DESC}"
314 check_error $?
"Error unpacking ${DESC}."
322 change_title
"Patching ${DESC}"
323 echo " >>> Patching ${DESC} with ${PATCH_FILE}"
325 patch -t "-p${PATCH_STRIP}" <"$PATCH_FILE"
326 check_error $?
"Error patching ${DESC}."
334 BINUTILS_SOURCE
="ftp://ftp.gnu.org/gnu/binutils/"
335 GCC_SOURCE
="ftp://ftp.gnu.org/gnu/gcc/gcc-${GCC_VERSION}/"
336 GDB_SOURCE
="ftp://ftp.gnu.org/gnu/gdb/"
338 download_fetch
"${BINUTILS_SOURCE}" "${BINUTILS}" "33adb18c3048d057ac58d07a3f1adb38"
339 download_fetch
"${GCC_SOURCE}" "${GCC}" "3b2386c114cd74185aa3754b58a79304"
340 download_fetch
"${GDB_SOURCE}" "${GDB}" "fbc4dab4181e6e9937075b43a4ce2732"
343 set_target_from_platform
() {
346 LINUX_TARGET
="amd64-linux-gnu"
347 HELENOS_TARGET
="amd64-helenos"
350 LINUX_TARGET
="arm-linux-gnueabi"
351 HELENOS_TARGET
="arm-helenos-gnueabi"
354 LINUX_TARGET
="i686-pc-linux-gnu"
355 HELENOS_TARGET
="i686-pc-helenos"
358 LINUX_TARGET
="ia64-pc-linux-gnu"
359 HELENOS_TARGET
="ia64-pc-helenos"
362 LINUX_TARGET
="mipsel-linux-gnu"
363 HELENOS_TARGET
="mipsel-helenos"
366 LINUX_TARGET
="mips-linux-gnu"
367 HELENOS_TARGET
="mips-helenos"
370 LINUX_TARGET
="mips64el-linux-gnu"
371 HELENOS_TARGET
="mips64el-helenos"
374 LINUX_TARGET
="ppc-linux-gnu"
375 HELENOS_TARGET
="ppc-helenos"
378 LINUX_TARGET
="ppc64-linux-gnu"
379 HELENOS_TARGET
="ppc64-helenos"
382 LINUX_TARGET
="sparc-leon3-linux-gnu"
383 HELENOS_TARGET
="sparc-leon3-helenos"
386 LINUX_TARGET
="sparc64-linux-gnu"
387 HELENOS_TARGET
="sparc64-helenos"
390 check_error
1 "No target known for $1."
397 # This sets the *_TARGET variables
398 set_target_from_platform
"$PLATFORM"
399 if $USE_HELENOS_TARGET; then
400 TARGET
="$HELENOS_TARGET"
402 TARGET
="$LINUX_TARGET"
405 WORKDIR
="${BASEDIR}/${PLATFORM}"
406 BINUTILSDIR
="${WORKDIR}/binutils-${BINUTILS_VERSION}"
407 GCCDIR
="${WORKDIR}/gcc-${GCC_VERSION}"
408 OBJDIR
="${WORKDIR}/gcc-obj"
409 GDBDIR
="${WORKDIR}/gdb-${GDB_VERSION}"
411 if [ -z "${CROSS_PREFIX}" ] ; then
412 CROSS_PREFIX
="/usr/local/cross"
414 if [ -z "${CROSS_HELENOS_PREFIX}" ] ; then
415 CROSS_HELENOS_PREFIX
="/usr/local/cross-helenos"
418 if $USE_HELENOS_TARGET; then
419 PREFIX
="${CROSS_HELENOS_PREFIX}/${PLATFORM}"
421 PREFIX
="${CROSS_PREFIX}/${PLATFORM}"
424 echo ">>> Downloading tarballs"
425 source_check
"${BASEDIR}/${BINUTILS}"
426 source_check
"${BASEDIR}/${GCC}"
427 source_check
"${BASEDIR}/${GDB}"
429 echo ">>> Removing previous content"
430 $REAL_INSTALL && cleanup_dir
"${PREFIX}"
431 cleanup_dir
"${WORKDIR}"
433 $REAL_INSTALL && create_dir
"${PREFIX}" "destination directory"
434 create_dir
"${OBJDIR}" "GCC object directory"
436 check_dirs
"${PREFIX}" "${WORKDIR}"
438 echo ">>> Unpacking tarballs"
440 check_error $?
"Change directory failed."
442 unpack_tarball
"${BASEDIR}/${BINUTILS}" "binutils"
443 unpack_tarball
"${BASEDIR}/${GCC}" "GCC"
444 unpack_tarball
"${BASEDIR}/${GDB}" "GDB"
446 echo ">>> Applying patches"
447 for p
in $BINUTILS_PATCHES; do
448 patch_sources
"${SRCDIR}/${p}" 0 "binutils"
450 for p
in $GCC_PATCHES; do
451 patch_sources
"${SRCDIR}/${p}" 0 "GCC"
453 for p
in $GDB_PATCHES; do
454 patch_sources
"${SRCDIR}/${p}" 0 "GDB"
457 echo ">>> Processing binutils (${PLATFORM})"
459 check_error $?
"Change directory failed."
461 change_title
"binutils: configure (${PLATFORM})"
462 CFLAGS
=-Wno-error .
/configure \
463 "--target=${TARGET}" \
464 "--prefix=${PREFIX}" "--program-prefix=${TARGET}-" \
465 --disable-nls --disable-werror
466 check_error $?
"Error configuring binutils."
468 change_title
"binutils: make (${PLATFORM})"
470 check_error $?
"Error compiling binutils."
472 change_title
"binutils: install (${PLATFORM})"
473 if $REAL_INSTALL; then
476 make install "DESTDIR=${INSTALL_DIR}"
478 check_error $?
"Error installing binutils."
481 echo ">>> Processing GCC (${PLATFORM})"
483 check_error $?
"Change directory failed."
485 change_title
"GCC: configure (${PLATFORM})"
486 PATH
="$PATH:${INSTALL_DIR}/${PREFIX}/bin" "${GCCDIR}/configure" \
487 "--target=${TARGET}" \
488 "--prefix=${PREFIX}" "--program-prefix=${TARGET}-" \
489 --with-gnu-as --with-gnu-ld --disable-nls --disable-threads \
490 --enable-languages=c
,objc
,c
++,obj-c
++ \
491 --disable-multilib --disable-libgcj --without-headers \
492 --disable-shared --enable-lto --disable-werror
493 check_error $?
"Error configuring GCC."
495 change_title
"GCC: make (${PLATFORM})"
496 PATH
="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-gcc
497 check_error $?
"Error compiling GCC."
499 change_title
"GCC: install (${PLATFORM})"
500 if $REAL_INSTALL; then
501 PATH
="${PATH}:${PREFIX}/bin" make install-gcc
503 PATH
="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-gcc "DESTDIR=${INSTALL_DIR}"
505 check_error $?
"Error installing GCC."
508 echo ">>> Processing GDB (${PLATFORM})"
510 check_error $?
"Change directory failed."
512 change_title
"GDB: configure (${PLATFORM})"
513 PATH
="$PATH:${INSTALL_DIR}/${PREFIX}/bin" .
/configure \
514 "--target=${TARGET}" \
515 "--prefix=${PREFIX}" "--program-prefix=${TARGET}-"
516 check_error $?
"Error configuring GDB."
518 change_title
"GDB: make (${PLATFORM})"
519 PATH
="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all
520 check_error $?
"Error compiling GDB."
522 change_title
"GDB: make (${PLATFORM})"
523 if $REAL_INSTALL; then
524 PATH
="${PATH}:${PREFIX}/bin" make install
526 PATH
="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install "DESTDIR=${INSTALL_DIR}"
528 check_error $?
"Error installing GDB."
532 check_error $?
"Change directory failed."
534 echo ">>> Cleaning up"
535 cleanup_dir
"${WORKDIR}"
538 echo ">>> Cross-compiler for ${TARGET} installed."
541 while [ "$#" -gt 1 ]; do
548 USE_HELENOS_TARGET
=true
557 if [ "$#" -lt "1" ]; then
562 amd64|arm32|ia32|ia64|mips32|mips32eb|mips64|ppc32|ppc64|sparc32|sparc64
)
572 build_target
"mips32"
573 build_target
"mips32eb"
574 build_target
"mips64"
577 build_target
"sparc32"
578 build_target
"sparc64"
582 build_target
"amd64" &
583 build_target
"arm32" &
584 build_target
"ia32" &
585 build_target
"ia64" &
586 build_target
"mips32" &
587 build_target
"mips32eb" &
588 build_target
"mips64" &
589 build_target
"ppc32" &
590 build_target
"ppc64" &
591 build_target
"sparc32" &
592 build_target
"sparc64" &
597 build_target
"amd64" &
598 build_target
"arm32" &
601 build_target
"ia32" &
602 build_target
"ia64" &
605 build_target
"mips32" &
606 build_target
"mips32eb" &
609 build_target
"mips64" &
610 build_target
"ppc32" &
613 build_target
"ppc64" &
614 build_target
"sparc32" &
617 build_target
"sparc64" &