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.
31 BINUTILS_GDB_GIT
="https://github.com/HelenOS/binutils-gdb.git"
33 BINUTILS_BRANCH
="binutils-2_31_1-helenos"
34 BINUTILS_VERSION
="2.31.1"
36 GDB_BRANCH
="gdb-8_2-helenos"
39 GCC_GIT
="https://github.com/HelenOS/gcc.git"
40 GCC_BRANCH
="8_2_0-helenos"
44 SRCDIR
="$(readlink -f $(dirname "$0"))"
47 USE_HELENOS_TARGET
=true
50 if [ "$1" -ne "0" ] ; then
52 echo "Script failed: $2"
59 echo "Cross-compiler toolchain build script"
62 echo " $0 [--no-install] [--non-helenos-target] <platform>"
63 echo " $0 --test-version [<platform>]"
65 echo "Possible target platforms are:"
66 echo " amd64 AMD64 (x86-64, x64)"
69 echo " ia32 IA-32 (x86, i386)"
70 echo " ia64 IA-64 (Itanium)"
71 echo " mips32 MIPS little-endian 32b"
72 echo " mips32eb MIPS big-endian 32b"
73 echo " ppc32 PowerPC 32b"
74 echo " riscv64 RISC-V 64b"
75 echo " sparc64 SPARC V9"
76 echo " all build all targets"
77 echo " essential build only targets currently needed for HelenOS development"
78 echo " parallel same as 'all', but all in parallel"
79 echo " 2-way same as 'all', but 2-way parallel"
81 echo "The toolchain is installed into directory specified by the"
82 echo "CROSS_PREFIX environment variable. If the variable is not"
83 echo "defined, /usr/local/cross/ is used as default."
85 echo "If --no-install is present, the toolchain still uses the"
86 echo "CROSS_PREFIX as the target directory but the installation"
87 echo "copies the files into PKG/ subdirectory without affecting"
88 echo "the actual root file system. That is only useful if you do"
89 echo "not want to run the script under the super user."
91 echo "The --non-helenos-target will build non-HelenOS-specific toolchain"
92 echo "(i.e. it will use *-linux-* triplet instead of *-helenos)."
93 echo "Using this toolchain for building HelenOS is not supported."
100 echo "Cross-compiler toolchain build script"
102 echo "Start testing the version of the installed software"
105 if [ -z "$1" ] ||
[ "$1" == "all" ] ; then
106 PLATFORMS
=("amd64" "arm32" "arm64" "ia32" "ia64" "mips32" "mips32eb" "ppc32" "riscv64" "sparc64")
112 if [ -z "${CROSS_PREFIX}" ] ; then
113 CROSS_PREFIX
="/usr/local/cross"
116 for i
in "${PLATFORMS[@]}"
119 set_target_from_platform
"$PLATFORM"
120 PREFIX
="${CROSS_PREFIX}/bin/${HELENOS_TARGET}"
122 echo "== $PLATFORM =="
123 test_app_version
"Binutils" "ld" "GNU\ ld\ \(GNU\ Binutils\)\ ((\.|[0-9])+)" "$BINUTILS_VERSION"
124 test_app_version
"GCC" "gcc" "gcc\ version\ ((\.|[0-9])+)" "$GCC_VERSION"
125 test_app_version
"GDB" "gdb" "GNU\ gdb\ \(GDB\)\s+((\.|[0-9])+)" "$GDB_VERSION"
138 APP
="${PREFIX}-${APPNAME}"
139 if [ ! -e $APP ]; then
140 echo "- $PKGNAME is missing"
143 OUT
=$
(${APP} -v 2>&1)
146 if [[ "$OUT" =~
$REGEX ]]; then
147 VERSION
="${BASH_REMATCH[1]}"
148 if [ "$INS_VERSION" = "$VERSION" ]; then
149 echo "+ $PKGNAME is uptodate ($INS_VERSION)"
151 echo "- $PKGNAME ($VERSION) is outdated ($INS_VERSION)"
154 echo "- $PKGNAME Unexpected output"
168 if [ "${TM}" -eq 0 ] ; then
177 TM
="`expr "${TM}" - 1`"
178 show_countdown
"${TM}"
181 show_dependencies
() {
182 echo "IMPORTANT NOTICE:"
184 echo "For a successful compilation and use of the cross-compiler"
185 echo "toolchain you need at least the following dependencies."
187 echo "Please make sure that the dependencies are present in your"
188 echo "system. Otherwise the compilation process might fail after"
189 echo "a few seconds or minutes."
191 echo " - SED, AWK, Flex, Bison, gzip, bzip2, Bourne Shell"
192 echo " - gettext, zlib, Texinfo, libelf, libgomp"
193 echo " - GNU Make, Coreutils, Sharutils, tar"
194 echo " - native C and C++ compiler, assembler and linker"
195 echo " - native C and C++ standard library with headers"
202 if [ -d "${DIR}" ] ; then
203 change_title
"Removing ${DIR}"
204 echo " >>> Removing ${DIR}"
213 change_title
"Creating ${DESC}"
214 echo ">>> Creating ${DESC}"
218 check_error $?
"Unable to create ${DIR}."
226 mkdir
-p "${OUTSIDE}"
229 check_error $?
"Unable to change directory to ${OUTSIDE}."
233 check_error $?
"Unable to change directory to ${BASE}."
237 check_error $?
"Unable to change directory to ${ORIGINAL}."
239 BASE_LEN
="${#ABS_BASE}"
240 OUTSIDE_TRIM
="${ABS_OUTSIDE:0:${BASE_LEN}}"
242 if [ "${OUTSIDE_TRIM}" == "${ABS_BASE}" ] ; then
244 echo "CROSS_PREFIX cannot reside within the working directory."
254 mkdir
-p "${BASEDIR}/downloads"
255 cd "${BASEDIR}/downloads"
256 check_error $?
"Change directory failed."
258 echo ">>> Downloading sources"
259 git clone
--depth 1 -b "$BINUTILS_BRANCH" "$BINUTILS_GDB_GIT" "binutils-$BINUTILS_VERSION"
260 git clone
--depth 1 -b "$GDB_BRANCH" "$BINUTILS_GDB_GIT" "gdb-$GDB_VERSION"
261 git clone
--depth 1 -b "$GCC_BRANCH" "$GCC_GIT" "gcc-$GCC_VERSION"
263 # If the directory already existed, pull upstream changes.
264 git
-C "binutils-$BINUTILS_VERSION" pull
265 git
-C "gdb-$GDB_VERSION" pull
266 git
-C "gcc-$GCC_VERSION" pull
268 echo ">>> Downloading GCC prerequisites"
269 cd "gcc-${GCC_VERSION}"
270 .
/contrib
/download_prerequisites
274 set_target_from_platform
() {
299 HELENOS_TARGET
="${GNU_ARCH}-helenos"
303 LINUX_TARGET
="${GNU_ARCH}-linux-gnueabi"
306 LINUX_TARGET
="${GNU_ARCH}-linux-gnu"
314 # This sets the *_TARGET variables
315 set_target_from_platform
"$PLATFORM"
316 if $USE_HELENOS_TARGET ; then
317 TARGET
="$HELENOS_TARGET"
319 TARGET
="$LINUX_TARGET"
322 WORKDIR
="${BASEDIR}/${TARGET}"
323 INSTALL_DIR
="${BASEDIR}/PKG"
324 BINUTILSDIR
="${WORKDIR}/binutils-${BINUTILS_VERSION}"
325 GCCDIR
="${WORKDIR}/gcc-${GCC_VERSION}"
326 GDBDIR
="${WORKDIR}/gdb-${GDB_VERSION}"
328 if [ -z "${CROSS_PREFIX}" ] ; then
329 CROSS_PREFIX
="/usr/local/cross"
332 if [ -z "$JOBS" ] ; then
336 PREFIX
="${CROSS_PREFIX}"
338 echo ">>> Removing previous content"
339 cleanup_dir
"${WORKDIR}"
340 mkdir
-p "${WORKDIR}"
341 check_dirs
"${PREFIX}" "${WORKDIR}"
343 if $USE_HELENOS_TARGET ; then
344 echo ">>> Creating build sysroot"
345 mkdir
-p "${WORKDIR}/sysroot/include"
346 mkdir
"${WORKDIR}/sysroot/lib"
347 cp -r -L -t "${WORKDIR}/sysroot/include" \
348 ${SRCDIR}/..
/abi
/include
/* \
349 ${SRCDIR}/..
/uspace
/lib
/c
/arch
/${PLATFORM}/include
/* \
350 ${SRCDIR}/..
/uspace
/lib
/c
/include
/*
351 check_error $?
"Failed to create build sysroot."
354 echo ">>> Processing binutils (${PLATFORM})"
355 mkdir
-p "${BINUTILSDIR}"
357 check_error $?
"Change directory failed."
359 change_title
"binutils: configure (${PLATFORM})"
360 CFLAGS
=-Wno-error "${BASEDIR}/downloads/binutils-${BINUTILS_VERSION}/configure" \
361 "--target=${TARGET}" \
362 "--prefix=${PREFIX}" \
363 "--program-prefix=${TARGET}-" \
367 --enable-deterministic-archives \
370 check_error $?
"Error configuring binutils."
372 change_title
"binutils: make (${PLATFORM})"
374 check_error $?
"Error compiling binutils."
376 change_title
"binutils: install (${PLATFORM})"
377 make install "DESTDIR=${INSTALL_DIR}"
378 check_error $?
"Error installing binutils."
381 echo ">>> Processing GCC (${PLATFORM})"
384 check_error $?
"Change directory failed."
386 if $USE_HELENOS_TARGET ; then
387 SYSROOT
=--with-sysroot --with-build-sysroot="${WORKDIR}/sysroot"
389 SYSROOT
=--without-headers
392 change_title
"GCC: configure (${PLATFORM})"
393 PATH
="$PATH:${INSTALL_DIR}/${PREFIX}/bin" "${BASEDIR}/downloads/gcc-${GCC_VERSION}/configure" \
394 "--target=${TARGET}" \
395 "--prefix=${PREFIX}" \
396 "--program-prefix=${TARGET}-" \
400 --enable-languages=c
,c
++,go \
405 check_error $?
"Error configuring GCC."
407 change_title
"GCC: make (${PLATFORM})"
408 PATH
="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-gcc
-j$JOBS
409 check_error $?
"Error compiling GCC."
411 if $USE_HELENOS_TARGET ; then
412 PATH
="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libgcc
-j$JOBS
413 check_error $?
"Error compiling libgcc."
414 # TODO: needs some extra care
415 #PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libatomic -j$JOBS
416 #check_error $? "Error compiling libatomic."
417 #PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libstdc++-v3 -j$JOBS
418 #check_error $? "Error compiling libstdc++."
421 change_title
"GCC: install (${PLATFORM})"
422 PATH
="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-gcc "DESTDIR=${INSTALL_DIR}"
423 if $USE_HELENOS_TARGET ; then
424 PATH
="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libgcc "DESTDIR=${INSTALL_DIR}"
425 #PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libatomic "DESTDIR=${INSTALL_DIR}"
426 #PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libstdc++-v3 "DESTDIR=${INSTALL_DIR}"
428 check_error $?
"Error installing GCC."
431 echo ">>> Processing GDB (${PLATFORM})"
434 check_error $?
"Change directory failed."
436 change_title
"GDB: configure (${PLATFORM})"
437 PATH
="$PATH:${INSTALL_DIR}/${PREFIX}/bin" "${BASEDIR}/downloads/gdb-${GDB_VERSION}/configure" \
438 "--target=${TARGET}" \
439 "--prefix=${PREFIX}" \
440 "--program-prefix=${TARGET}-" \
442 check_error $?
"Error configuring GDB."
444 change_title
"GDB: make (${PLATFORM})"
445 PATH
="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-gdb
-j$JOBS
446 check_error $?
"Error compiling GDB."
448 change_title
"GDB: make (${PLATFORM})"
449 PATH
="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-gdb "DESTDIR=${INSTALL_DIR}"
450 check_error $?
"Error installing GDB."
452 # Symlink clang and lld to the install path.
453 CLANG
="`which clang 2> /dev/null || echo "/usr
/bin
/clang
"`"
454 LLD
="`which ld.lld 2> /dev/null || echo "/usr
/bin
/ld.lld
"`"
456 ln -s $CLANG "${INSTALL_DIR}/${PREFIX}/bin/${TARGET}-clang"
457 ln -s $LLD "${INSTALL_DIR}/${PREFIX}/bin/${TARGET}-ld.lld"
459 if $REAL_INSTALL ; then
460 echo ">>> Moving to the destination directory."
461 echo cp -r -t "${PREFIX}" "${INSTALL_DIR}/${PREFIX}/"*
462 cp -r -t "${PREFIX}" "${INSTALL_DIR}/${PREFIX}/"*
466 check_error $?
"Change directory failed."
468 echo ">>> Cleaning up"
469 cleanup_dir
"${WORKDIR}"
472 echo ">>> Cross-compiler for ${TARGET} installed."
475 while [ "$#" -gt 1 ] ; do
485 --non-helenos-target)
486 USE_HELENOS_TARGET
=false
495 if [ "$#" -lt "1" ] ; then
503 amd64|arm32|arm64|ia32|ia64|mips32|mips32eb|ppc32|riscv64|sparc64
)
514 build_target
"mips32"
515 build_target
"mips32eb"
517 build_target
"riscv64"
518 build_target
"sparc64"
527 build_target
"mips32"
528 build_target
"mips32eb"
530 build_target
"sparc64"
534 build_target
"amd64" &
535 build_target
"arm32" &
536 build_target
"arm64" &
537 build_target
"ia32" &
538 build_target
"ia64" &
539 build_target
"mips32" &
540 build_target
"mips32eb" &
541 build_target
"ppc32" &
542 build_target
"riscv64" &
543 build_target
"sparc64" &
548 build_target
"amd64" &
549 build_target
"arm32" &
552 build_target
"arm64" &
553 build_target
"ia32" &
556 build_target
"ia64" &
557 build_target
"mips32" &
560 build_target
"mips32eb" &
561 build_target
"ppc32" &
564 build_target
"riscv64" &
565 build_target
"sparc64" &