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_41-helenos"
34 BINUTILS_VERSION
="2.41"
36 GDB_BRANCH
="gdb-13.2-helenos"
39 GCC_GIT
="https://github.com/HelenOS/gcc.git"
40 GCC_BRANCH
="13_2_0-helenos"
44 SRCDIR
="$(readlink -f $(dirname "$0"))"
46 # If we install into a temporary directory and copy from there,
47 # we don't have to trust the upstream makefiles respect the prefix for
48 # all files, and we also have a ready-made package for distribution.
49 INSTALL_DIR
="${BASEDIR}/PKG"
57 if [ -z "$JOBS" ] ; then
62 if [ "$1" -ne "0" ] ; then
64 echo "Script failed: $2"
71 echo "HelenOS cross-compiler toolchain build script"
74 echo " $0 [--system-wide] [--with-gdb|--only-gdb] <platform>"
75 echo " $0 [--system-wide] --test-version [<platform>]"
77 echo "Possible target platforms are:"
78 echo " amd64 AMD64 (x86-64, x64)"
81 echo " ia32 IA-32 (x86, i386)"
82 echo " ia64 IA-64 (Itanium)"
83 echo " mips32 MIPS little-endian 32b"
84 echo " mips32eb MIPS big-endian 32b"
85 echo " ppc32 PowerPC 32b"
86 echo " riscv64 RISC-V 64b"
87 echo " sparc64 SPARC V9"
88 echo " all build all targets"
89 echo " parallel same as 'all', but all in parallel"
90 echo " 2-way same as 'all', but 2-way parallel"
92 echo "The toolchain target installation directory is determined by matching"
93 echo "the first of the following conditions:"
95 echo " (1) If the \$CROSS_PREFIX environment variable is set, then it is"
96 echo " used as the target installation directory."
97 echo " (2) If the --system-wide option is used, then /opt/HelenOS/cross"
98 echo " is used as the target installation directory. This usually"
99 echo " requires running this script with super user privileges."
100 echo " (3) In other cases, \$XDG_DATA_HOME/HelenOS/cross is used as the"
101 echo " target installation directory. If the \$XDG_DATA_HOME environment"
102 echo " variable is not set, then the default value of \$HOME/.local/share"
105 echo "The --non-helenos-target option will build non-HelenOS-specific"
106 echo "toolchain (i.e. it will use *-linux-* triplet instead of *-helenos)."
107 echo "Using this toolchain for building HelenOS is not supported."
109 echo "The --test-version mode tests the currently installed version of the"
116 if [ -z "$CROSS_PREFIX" ] ; then
117 if $SYSTEM_INSTALL ; then
118 CROSS_PREFIX
="/opt/HelenOS/cross"
120 if [ -z "$XDG_DATA_HOME" ] ; then
121 XDG_DATA_HOME
="$HOME/.local/share"
123 CROSS_PREFIX
="$XDG_DATA_HOME/HelenOS/cross"
131 echo "HelenOS cross-compiler toolchain build script"
133 echo "Testing the version of the installed software in $CROSS_PREFIX"
136 if [ -z "$1" ] ||
[ "$1" = "all" ] ; then
137 PLATFORMS
='amd64 arm32 arm64 ia32 ia64 mips32 mips32eb ppc32 riscv64 sparc64'
142 for PLATFORM
in $PLATFORMS ; do
143 set_target_from_platform
"$PLATFORM"
144 PREFIX
="${CROSS_PREFIX}/bin/${TARGET}"
146 echo "== $PLATFORM =="
147 test_app_version
"Binutils" "ld" "GNU ld (.*) \([.0-9]*\)" "$BINUTILS_VERSION"
148 test_app_version
"GCC" "gcc" "gcc version \([.0-9]*\)" "$GCC_VERSION"
149 test_app_version
"GDB" "gdb" "GNU gdb (.*)[[:space:]]\+\([.0-9]*\)" "$GDB_VERSION"
159 APP
="${PREFIX}-${APPNAME}"
160 if [ ! -e $APP ]; then
161 echo "- $PKGNAME is missing"
163 VERSION
=`${APP} -v 2>&1 | sed -n "s:^${REGEX}.*:\1:p"`
165 if [ -z "$VERSION" ]; then
166 echo "- $PKGNAME Unexpected output"
170 if [ "$INS_VERSION" = "$VERSION" ]; then
171 echo "+ $PKGNAME is up-to-date ($INS_VERSION)"
173 echo "- $PKGNAME ($VERSION) is outdated ($INS_VERSION)"
197 if [ "${TM}" -eq 0 ] ; then
206 TM
="`expr "${TM}" - 1`"
207 show_countdown
"${TM}"
210 show_dependencies
() {
213 echo "HelenOS cross-compiler toolchain build script"
215 echo "Installing software to $CROSS_PREFIX"
218 echo "IMPORTANT NOTICE:"
220 echo "For a successful compilation and use of the cross-compiler"
221 echo "toolchain you need at least the following dependencies."
223 echo "Please make sure that the dependencies are present in your"
224 echo "system. Otherwise the compilation process might fail after"
225 echo "a few seconds or minutes."
227 echo " - SED, AWK, Flex, Bison, gzip, bzip2, Bourne Shell"
228 echo " - gettext, zlib, Texinfo, libelf, libgomp"
229 echo " - GNU Make, Coreutils, Sharutils, tar"
230 echo " - native C and C++ compiler, assembler and linker"
231 echo " - native C and C++ standard library with headers"
238 if [ -d "${DIR}" ] ; then
239 change_title
"Removing ${DIR}"
240 echo " >>> Removing ${DIR}"
249 change_title
"Creating ${DESC}"
250 echo ">>> Creating ${DESC}"
254 check_error $?
"Unable to create ${DIR}."
259 check_error $?
"Unable to change directory to ${BASEDIR}."
262 if $SYSTEM_INSTALL && [ ! -d "${CROSS_PREFIX}" ]; then
264 ( set -x ; sudo
-k mkdir
-p "${CROSS_PREFIX}" )
266 mkdir
-p "${CROSS_PREFIX}"
270 check_error $?
"Unable to change directory to ${CROSS_PREFIX}."
272 while [ "${#PWD}" -gt "${#ABS_BASE}" ]; do
276 if [ "$PWD" = "$ABS_BASE" ]; then
278 echo "CROSS_PREFIX cannot reside within the working directory."
290 mkdir
-p "${BASEDIR}/downloads"
291 cd "${BASEDIR}/downloads"
292 check_error $?
"Change directory failed."
294 change_title
"Downloading sources"
295 echo ">>> Downloading sources"
297 if $BUILD_BINUTILS ; then
298 git clone
--depth 1 -b "$BINUTILS_BRANCH" "$BINUTILS_GDB_GIT" "binutils-$BINUTILS_VERSION"
299 # If the directory already existed, pull upstream changes.
300 git
-C "binutils-$BINUTILS_VERSION" pull
304 git clone
--depth 1 -b "$GCC_BRANCH" "$GCC_GIT" "gcc-$GCC_VERSION"
305 git
-C "gcc-$GCC_VERSION" pull
307 change_title
"Downloading GCC prerequisites"
308 echo ">>> Downloading GCC prerequisites"
309 cd "gcc-${GCC_VERSION}"
310 .
/contrib
/download_prerequisites
315 git clone
--depth 1 -b "$GDB_BRANCH" "$BINUTILS_GDB_GIT" "gdb-$GDB_VERSION"
316 git
-C "gdb-$GDB_VERSION" pull
319 # This sets the CROSS_PREFIX variable
322 DESTDIR_SPEC
="DESTDIR=${INSTALL_DIR}"
327 set_target_from_platform
() {
352 TARGET
="${GNU_ARCH}-helenos"
356 # This sets the TARGET variable
357 set_target_from_platform
"$1"
359 WORKDIR
="${BASEDIR}/${TARGET}"
360 BINUTILSDIR
="${WORKDIR}/binutils-${BINUTILS_VERSION}"
362 echo ">>> Removing previous content"
363 cleanup_dir
"${WORKDIR}"
364 mkdir
-p "${WORKDIR}"
366 echo ">>> Processing binutils (${TARGET})"
367 mkdir
-p "${BINUTILSDIR}"
369 check_error $?
"Change directory failed."
371 change_title
"binutils: configure (${TARGET})"
372 CFLAGS
="-Wno-error -fcommon" "${BASEDIR}/downloads/binutils-${BINUTILS_VERSION}/configure" \
373 "--target=${TARGET}" \
374 "--prefix=${CROSS_PREFIX}" \
375 "--program-prefix=${TARGET}-" \
379 --enable-deterministic-archives \
382 check_error $?
"Error configuring binutils."
384 change_title
"binutils: make (${TARGET})"
386 check_error $?
"Error compiling binutils."
388 change_title
"binutils: install (${TARGET})"
389 make install $DESTDIR_SPEC
390 check_error $?
"Error installing binutils."
394 # This sets the TARGET variable
395 set_target_from_platform
"$1"
397 WORKDIR
="${BASEDIR}/${TARGET}"
398 GCCDIR
="${WORKDIR}/gcc-${GCC_VERSION}"
400 echo ">>> Removing previous content"
401 cleanup_dir
"${WORKDIR}"
402 mkdir
-p "${WORKDIR}"
404 echo ">>> Processing GCC (${TARGET})"
407 check_error $?
"Change directory failed."
409 BUILDPATH
="${CROSS_PREFIX}/bin:${PATH}"
411 change_title
"GCC: configure (${TARGET})"
412 PATH
="${BUILDPATH}" "${BASEDIR}/downloads/gcc-${GCC_VERSION}/configure" \
413 "--target=${TARGET}" \
414 "--prefix=${CROSS_PREFIX}" \
415 "--program-prefix=${TARGET}-" \
419 --enable-languages=c
,c
++,go \
423 --without-headers # TODO: Replace with proper sysroot so we can build more libs
424 check_error $?
"Error configuring GCC."
426 change_title
"GCC: make (${TARGET})"
427 PATH
="${BUILDPATH}" make all-gcc
-j$JOBS
428 check_error $?
"Error compiling GCC."
430 change_title
"GCC: install (${TARGET})"
431 PATH
="${BUILDPATH}" make install-gcc
$DESTDIR_SPEC
432 check_error $?
"Error installing GCC."
436 # This sets the TARGET variable
437 set_target_from_platform
"$1"
439 WORKDIR
="${BASEDIR}/${TARGET}"
440 GCCDIR
="${WORKDIR}/gcc-${GCC_VERSION}"
442 # No removing previous content here, we need the previous GCC build
445 check_error $?
"Change directory failed."
447 BUILDPATH
="${CROSS_PREFIX}/bin:${PATH}"
449 change_title
"libgcc: make (${TARGET})"
451 PATH
="${BUILDPATH}" make all-target-libgcc
-j$JOBS
452 check_error $?
"Error compiling libgcc."
453 # TODO: libatomic and libstdc++ need some extra care
454 # PATH="${BUILDPATH}" make all-target-libatomic -j$JOBS
455 # check_error $? "Error compiling libatomic."
456 # PATH="${BUILDPATH}" make all-target-libstdc++-v3 -j$JOBS
457 # check_error $? "Error compiling libstdc++."
459 change_title
"libgcc: install (${TARGET})"
461 PATH
="${BUILDPATH}" make install-target-libgcc
$DESTDIR_SPEC
462 # PATH="${BUILDPATH}" make install-target-libatomic $DESTDIR_SPEC
463 # PATH="${BUILDPATH}" make install-target-libstdc++-v3 $DESTDIR_SPEC
464 check_error $?
"Error installing libgcc."
468 # This sets the TARGET variable
469 set_target_from_platform
"$1"
471 WORKDIR
="${BASEDIR}/${TARGET}"
472 GDBDIR
="${WORKDIR}/gdb-${GDB_VERSION}"
474 echo ">>> Removing previous content"
475 cleanup_dir
"${WORKDIR}"
476 mkdir
-p "${WORKDIR}"
478 echo ">>> Processing GDB (${TARGET})"
481 check_error $?
"Change directory failed."
483 change_title
"GDB: configure (${TARGET})"
484 CFLAGS
="-fcommon" "${BASEDIR}/downloads/gdb-${GDB_VERSION}/configure" \
485 "--target=${TARGET}" \
486 "--prefix=${CROSS_PREFIX}" \
487 "--program-prefix=${TARGET}-" \
489 check_error $?
"Error configuring GDB."
491 change_title
"GDB: make (${TARGET})"
493 check_error $?
"Error compiling GDB."
495 change_title
"GDB: install (${TARGET})"
496 make install-gdb
$DESTDIR_SPEC
497 check_error $?
"Error installing GDB."
501 echo ">>> Moving to the destination directory."
502 if $SYSTEM_INSTALL ; then
504 ( set -x ; sudo
-k cp -r -t "${CROSS_PREFIX}" "${INSTALL_DIR}${CROSS_PREFIX}/"* )
506 ( set -x ; cp -r -t "${CROSS_PREFIX}" "${INSTALL_DIR}${CROSS_PREFIX}/"* )
511 # Symlink clang and lld to the install path.
512 CLANG
="`which clang 2> /dev/null || echo "/usr
/bin
/clang
"`"
513 LLD
="`which ld.lld 2> /dev/null || echo "/usr
/bin
/ld.lld
"`"
515 ln -s $CLANG "${INSTALL_DIR}${CROSS_PREFIX}/bin/${TARGET}-clang"
516 ln -s $LLD "${INSTALL_DIR}${CROSS_PREFIX}/bin/${TARGET}-ld.lld"
519 while [ "$#" -gt 1 ] ; do
545 if [ "$#" -lt "1" ] ; then
549 PLATFORMS
="amd64 arm32 arm64 ia32 ia64 mips32 mips32eb ppc32 riscv64 sparc64"
556 for x
in $PLATFORMS ; do
562 for x
in $PLATFORMS ; do
595 if $BUILD_BINUTILS ; then
596 $RUNNER build_binutils
599 # gcc/libgcc may fail to build correctly if binutils is not installed first
600 echo ">>> Installing binutils"
608 # libgcc may fail to build correctly if gcc is not installed first
609 echo ">>> Installing GCC"
619 echo ">>> Installing all files"
630 amd64|arm32|arm64|ia32|ia64|mips32|mips32eb|ppc32|riscv64|sparc64
)
638 everything run_parallel