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 BINUTILS
="binutils-${BINUTILS_VERSION}${BINUTILS_RELEASE}.tar.bz2"
65 GCC
="gcc-${GCC_VERSION}.tar.bz2"
66 GDB
="gdb-${GDB_VERSION}.tar.bz2"
69 USE_HELENOS_TARGET
=false
70 INSTALL_DIR
="${BASEDIR}/PKG"
73 # Check if the library described in the argument
74 # exists and has acceptable version.
81 FNAME
="/tmp/conftest-$$"
83 echo "#include ${HEADER}" > "${FNAME}.c"
85 echo "int main()" >> "${FNAME}.c"
86 echo "{" >> "${FNAME}.c"
87 echo "${BODY}" >> "${FNAME}.c"
88 echo " return 0;" >> "${FNAME}.c"
89 echo "}" >> "${FNAME}.c"
91 cc
-c -o "${FNAME}.o" "${FNAME}.c" 2> "${FNAME}.log"
94 if [ "$RC" -ne "0" ] ; then
95 echo " ${DEPENDENCY} not found, too old or compiler error."
96 echo " Please recheck manually the source file \"${FNAME}.c\"."
97 echo " The compilation of the toolchain is probably going to fail,"
98 echo " you have been warned."
100 echo " ===== Compiler output ====="
102 echo " ==========================="
105 echo " ${DEPENDENCY} found"
106 rm -f "${FNAME}.log" "${FNAME}.o" "${FNAME}.c"
110 check_dependecies
() {
111 echo ">>> Basic dependency check"
112 check_dependency
"GMP" "<gmp.h>" "${GMP_MAIN}"
113 check_dependency
"MPFR" "<mpfr.h>" "${MPFR_MAIN}"
114 check_dependency
"MPC" "<mpc.h>" "${MPC_MAIN}"
119 if [ "$1" -ne "0" ]; then
121 echo "Script failed: $2"
131 COMPUTED
="`md5sum "${FILE}" | cut -d' ' -f1`"
132 if [ "${SUM}" != "${COMPUTED}" ] ; then
134 echo "Checksum of ${FILE} does not match."
141 echo "Cross-compiler toolchain build script"
144 echo " $0 [--no-install] [--helenos-target] <platform>"
146 echo "Possible target platforms are:"
147 echo " amd64 AMD64 (x86-64, x64)"
149 echo " ia32 IA-32 (x86, i386)"
150 echo " ia64 IA-64 (Itanium)"
151 echo " mips32 MIPS little-endian 32b"
152 echo " mips32eb MIPS big-endian 32b"
153 echo " mips64 MIPS little-endian 64b"
154 echo " ppc32 32-bit PowerPC"
155 echo " ppc64 64-bit PowerPC"
156 echo " sparc64 SPARC V9"
157 echo " all build all targets"
158 echo " parallel same as 'all', but all in parallel"
159 echo " 2-way same as 'all', but 2-way parallel"
161 echo "The toolchain is installed into directory specified by the"
162 echo "CROSS_PREFIX environment variable. If the variable is not"
163 echo "defined, /usr/local/cross/ is used as default."
165 echo "If --no-install is present, the toolchain still uses the"
166 echo "CROSS_PREFIX as the target directory but the installation"
167 echo "copies the files into PKG/ subdirectory without affecting"
168 echo "the actual root file system. That is only useful if you do"
169 echo "not want to run the script under the super user."
171 echo "The --helenos-target will build HelenOS-specific toolchain"
172 echo "(i.e. it will use *-helenos-* triplet instead of *-linux-*)."
173 echo "This toolchain is installed into /usr/local/cross-helenos by"
174 echo "default. The settings can be changed by setting environment"
175 echo "variable CROSS_HELENOS_PREFIX."
176 echo "Using the HelenOS-specific toolchain is still an experimental"
177 echo "feature that is not fully supported."
190 if [ "${TM}" -eq 0 ] ; then
199 TM
="`expr "${TM}" - 1`"
200 show_countdown
"${TM}"
203 show_dependencies
() {
204 echo "IMPORTANT NOTICE:"
206 echo "For a successful compilation and use of the cross-compiler"
207 echo "toolchain you need at least the following dependencies."
209 echo "Please make sure that the dependencies are present in your"
210 echo "system. Otherwise the compilation process might fail after"
211 echo "a few seconds or minutes."
213 echo " - SED, AWK, Flex, Bison, gzip, bzip2, Bourne Shell"
214 echo " - gettext, zlib, Texinfo, libelf, libgomp"
216 echo " - GNU Multiple Precision Library (GMP)"
219 echo " - GNU Coreutils"
220 echo " - GNU Sharutils"
223 echo " - Parma Polyhedra Library (PPL)"
225 echo " - native C compiler, assembler and linker"
226 echo " - native C library with headers"
235 if [ ! -f "${FILE}" ]; then
236 change_title
"Downloading ${FILE}"
237 wget
-c "${SOURCE}${FILE}"
238 check_error $?
"Error downloading ${FILE}."
241 check_md5
"${FILE}" "${CHECKSUM}"
247 if [ ! -f "${FILE}" ]; then
249 echo "File ${FILE} not found."
258 if [ -d "${DIR}" ]; then
259 change_title
"Removing ${DIR}"
260 echo " >>> Removing ${DIR}"
269 change_title
"Creating ${DESC}"
270 echo ">>> Creating ${DESC}"
274 check_error $?
"Unable to create ${DIR}."
283 check_error $?
"Unable to change directory to ${OUTSIDE}."
287 check_error $?
"Unable to change directory to ${BASE}."
291 check_error $?
"Unable to change directory to ${ORIGINAL}."
293 BASE_LEN
="${#ABS_BASE}"
294 OUTSIDE_TRIM
="${ABS_OUTSIDE:0:${BASE_LEN}}"
296 if [ "${OUTSIDE_TRIM}" == "${ABS_BASE}" ] ; then
298 echo "CROSS_PREFIX cannot reside within the working directory."
308 change_title
"Unpacking ${DESC}"
309 echo " >>> Unpacking ${DESC}"
312 check_error $?
"Error unpacking ${DESC}."
320 change_title
"Patching ${DESC}"
321 echo " >>> Patching ${DESC} with ${PATCH_FILE}"
323 patch -t "-p${PATCH_STRIP}" <"$PATCH_FILE"
324 check_error $?
"Error patching ${DESC}."
332 BINUTILS_SOURCE
="ftp://ftp.gnu.org/gnu/binutils/"
333 GCC_SOURCE
="ftp://ftp.gnu.org/gnu/gcc/gcc-${GCC_VERSION}/"
334 GDB_SOURCE
="ftp://ftp.gnu.org/gnu/gdb/"
336 download_fetch
"${BINUTILS_SOURCE}" "${BINUTILS}" "33adb18c3048d057ac58d07a3f1adb38"
337 download_fetch
"${GCC_SOURCE}" "${GCC}" "3b2386c114cd74185aa3754b58a79304"
338 download_fetch
"${GDB_SOURCE}" "${GDB}" "fbc4dab4181e6e9937075b43a4ce2732"
341 set_target_from_platform
() {
344 LINUX_TARGET
="amd64-linux-gnu"
345 HELENOS_TARGET
="amd64-helenos"
348 LINUX_TARGET
="arm-linux-gnueabi"
349 HELENOS_TARGET
="arm-helenos-gnueabi"
352 LINUX_TARGET
="i686-pc-linux-gnu"
353 HELENOS_TARGET
="i686-pc-helenos"
356 LINUX_TARGET
="ia64-pc-linux-gnu"
357 HELENOS_TARGET
="ia64-pc-helenos"
360 LINUX_TARGET
="mipsel-linux-gnu"
361 HELENOS_TARGET
="mipsel-helenos"
364 LINUX_TARGET
="mips-linux-gnu"
365 HELENOS_TARGET
="mips-helenos"
368 LINUX_TARGET
="mips64el-linux-gnu"
369 HELENOS_TARGET
="mips64el-helenos"
372 LINUX_TARGET
="ppc-linux-gnu"
373 HELENOS_TARGET
="ppc-helenos"
376 LINUX_TARGET
="ppc64-linux-gnu"
377 HELENOS_TARGET
="ppc64-helenos"
380 LINUX_TARGET
="sparc64-linux-gnu"
381 HELENOS_TARGET
="sparc64-helenos"
384 check_error
1 "No target known for $1."
391 # This sets the *_TARGET variables
392 set_target_from_platform
"$PLATFORM"
393 if $USE_HELENOS_TARGET; then
394 TARGET
="$HELENOS_TARGET"
396 TARGET
="$LINUX_TARGET"
399 WORKDIR
="${BASEDIR}/${PLATFORM}"
400 BINUTILSDIR
="${WORKDIR}/binutils-${BINUTILS_VERSION}"
401 GCCDIR
="${WORKDIR}/gcc-${GCC_VERSION}"
402 OBJDIR
="${WORKDIR}/gcc-obj"
403 GDBDIR
="${WORKDIR}/gdb-${GDB_VERSION}"
405 if [ -z "${CROSS_PREFIX}" ] ; then
406 CROSS_PREFIX
="/usr/local/cross"
408 if [ -z "${CROSS_HELENOS_PREFIX}" ] ; then
409 CROSS_HELENOS_PREFIX
="/usr/local/cross-helenos"
412 if $USE_HELENOS_TARGET; then
413 PREFIX
="${CROSS_HELENOS_PREFIX}/${PLATFORM}"
415 PREFIX
="${CROSS_PREFIX}/${PLATFORM}"
418 echo ">>> Downloading tarballs"
419 source_check
"${BASEDIR}/${BINUTILS}"
420 source_check
"${BASEDIR}/${GCC}"
421 source_check
"${BASEDIR}/${GDB}"
423 echo ">>> Removing previous content"
424 $REAL_INSTALL && cleanup_dir
"${PREFIX}"
425 cleanup_dir
"${WORKDIR}"
427 $REAL_INSTALL && create_dir
"${PREFIX}" "destination directory"
428 create_dir
"${OBJDIR}" "GCC object directory"
430 check_dirs
"${PREFIX}" "${WORKDIR}"
432 echo ">>> Unpacking tarballs"
434 check_error $?
"Change directory failed."
436 unpack_tarball
"${BASEDIR}/${BINUTILS}" "binutils"
437 unpack_tarball
"${BASEDIR}/${GCC}" "GCC"
438 unpack_tarball
"${BASEDIR}/${GDB}" "GDB"
440 echo ">>> Applying patches"
441 for p
in $BINUTILS_PATCHES; do
442 patch_sources
"${BASEDIR}/${p}" 0 "binutils"
444 for p
in $GCC_PATCHES; do
445 patch_sources
"${BASEDIR}/${p}" 0 "GCC"
447 for p
in $GDB_PATCHES; do
448 patch_sources
"${BASEDIR}/${p}" 0 "GDB"
451 echo ">>> Processing binutils (${PLATFORM})"
453 check_error $?
"Change directory failed."
455 change_title
"binutils: configure (${PLATFORM})"
456 CFLAGS
=-Wno-error .
/configure \
457 "--target=${TARGET}" \
458 "--prefix=${PREFIX}" "--program-prefix=${TARGET}-" \
459 --disable-nls --disable-werror
460 check_error $?
"Error configuring binutils."
462 change_title
"binutils: make (${PLATFORM})"
464 check_error $?
"Error compiling binutils."
466 change_title
"binutils: install (${PLATFORM})"
467 if $REAL_INSTALL; then
470 make install "DESTDIR=${INSTALL_DIR}"
472 check_error $?
"Error installing binutils."
475 echo ">>> Processing GCC (${PLATFORM})"
477 check_error $?
"Change directory failed."
479 change_title
"GCC: configure (${PLATFORM})"
480 PATH
="$PATH:${INSTALL_DIR}/${PREFIX}/bin" "${GCCDIR}/configure" \
481 "--target=${TARGET}" \
482 "--prefix=${PREFIX}" "--program-prefix=${TARGET}-" \
483 --with-gnu-as --with-gnu-ld --disable-nls --disable-threads \
484 --enable-languages=c
,objc
,c
++,obj-c
++ \
485 --disable-multilib --disable-libgcj --without-headers \
486 --disable-shared --enable-lto --disable-werror
487 check_error $?
"Error configuring GCC."
489 change_title
"GCC: make (${PLATFORM})"
490 PATH
="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-gcc
491 check_error $?
"Error compiling GCC."
493 change_title
"GCC: install (${PLATFORM})"
494 if $REAL_INSTALL; then
495 PATH
="${PATH}:${PREFIX}/bin" make install-gcc
497 PATH
="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-gcc "DESTDIR=${INSTALL_DIR}"
499 check_error $?
"Error installing GCC."
502 echo ">>> Processing GDB (${PLATFORM})"
504 check_error $?
"Change directory failed."
506 change_title
"GDB: configure (${PLATFORM})"
507 PATH
="$PATH:${INSTALL_DIR}/${PREFIX}/bin" .
/configure \
508 "--target=${TARGET}" \
509 "--prefix=${PREFIX}" "--program-prefix=${TARGET}-"
510 check_error $?
"Error configuring GDB."
512 change_title
"GDB: make (${PLATFORM})"
513 PATH
="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all
514 check_error $?
"Error compiling GDB."
516 change_title
"GDB: make (${PLATFORM})"
517 if $REAL_INSTALL; then
518 PATH
="${PATH}:${PREFIX}/bin" make install
520 PATH
="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install "DESTDIR=${INSTALL_DIR}"
522 check_error $?
"Error installing GDB."
526 check_error $?
"Change directory failed."
528 echo ">>> Cleaning up"
529 cleanup_dir
"${WORKDIR}"
532 echo ">>> Cross-compiler for ${TARGET} installed."
535 while [ "$#" -gt 1 ]; do
542 USE_HELENOS_TARGET
=true
551 if [ "$#" -lt "1" ]; then
556 amd64|arm32|ia32|ia64|mips32|mips32eb|mips64|ppc32|ppc64|sparc64
)
566 build_target
"mips32"
567 build_target
"mips32eb"
568 build_target
"mips64"
571 build_target
"sparc64"
575 build_target
"amd64" &
576 build_target
"arm32" &
577 build_target
"ia32" &
578 build_target
"ia64" &
579 build_target
"mips32" &
580 build_target
"mips32eb" &
581 build_target
"mips64" &
582 build_target
"ppc32" &
583 build_target
"ppc64" &
584 build_target
"sparc64" &
589 build_target
"amd64" &
590 build_target
"arm32" &
593 build_target
"ia32" &
594 build_target
"ia64" &
597 build_target
"mips32" &
598 build_target
"mips32eb" &
601 build_target
"mips64" &
602 build_target
"ppc32" &
605 build_target
"ppc64" &
606 build_target
"sparc64" &