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_30-helenos"
34 BINUTILS_VERSION
="2.30"
36 GDB_BRANCH
="gdb-8_1-helenos"
39 GCC_GIT
="https://github.com/HelenOS/gcc.git"
40 GCC_BRANCH
="7_3_0-helenos"
44 SRCDIR
="$(readlink -f $(dirname "$0"))"
47 USE_HELENOS_TARGET
=false
50 if [ "$1" -ne "0" ] ; then
52 echo "Script failed: $2"
59 echo "Cross-compiler toolchain build script"
62 echo " $0 [--no-install] [--helenos-target] <platform>"
64 echo "Possible target platforms are:"
65 echo " amd64 AMD64 (x86-64, x64)"
67 echo " ia32 IA-32 (x86, i386)"
68 echo " ia64 IA-64 (Itanium)"
69 echo " mips32 MIPS little-endian 32b"
70 echo " mips32eb MIPS big-endian 32b"
71 echo " mips64 MIPS little-endian 64b"
72 echo " ppc32 PowerPC 32b"
73 echo " ppc64 PowerPC 64v"
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 --helenos-target will build HelenOS-specific toolchain"
92 echo "(i.e. it will use *-helenos triplet instead of *-linux-*)."
93 echo "Using the HelenOS-specific toolchain is still an experimental"
94 echo "feature that is not fully supported."
107 if [ "${TM}" -eq 0 ] ; then
116 TM
="`expr "${TM}" - 1`"
117 show_countdown
"${TM}"
120 show_dependencies
() {
121 echo "IMPORTANT NOTICE:"
123 echo "For a successful compilation and use of the cross-compiler"
124 echo "toolchain you need at least the following dependencies."
126 echo "Please make sure that the dependencies are present in your"
127 echo "system. Otherwise the compilation process might fail after"
128 echo "a few seconds or minutes."
130 echo " - SED, AWK, Flex, Bison, gzip, bzip2, Bourne Shell"
131 echo " - gettext, zlib, Texinfo, libelf, libgomp"
132 echo " - GNU Make, Coreutils, Sharutils, tar"
133 echo " - native C and C++ compiler, assembler and linker"
134 echo " - native C and C++ standard library with headers"
141 if [ -d "${DIR}" ] ; then
142 change_title
"Removing ${DIR}"
143 echo " >>> Removing ${DIR}"
152 change_title
"Creating ${DESC}"
153 echo ">>> Creating ${DESC}"
157 check_error $?
"Unable to create ${DIR}."
165 mkdir
-p "${OUTSIDE}"
168 check_error $?
"Unable to change directory to ${OUTSIDE}."
172 check_error $?
"Unable to change directory to ${BASE}."
176 check_error $?
"Unable to change directory to ${ORIGINAL}."
178 BASE_LEN
="${#ABS_BASE}"
179 OUTSIDE_TRIM
="${ABS_OUTSIDE:0:${BASE_LEN}}"
181 if [ "${OUTSIDE_TRIM}" == "${ABS_BASE}" ] ; then
183 echo "CROSS_PREFIX cannot reside within the working directory."
193 mkdir
-p "${BASEDIR}/downloads"
194 cd "${BASEDIR}/downloads"
195 check_error $?
"Change directory failed."
197 echo ">>> Downloading sources"
198 git clone
--depth 1 -b "$BINUTILS_BRANCH" "$BINUTILS_GDB_GIT" "binutils-$BINUTILS_VERSION"
199 git clone
--depth 1 -b "$GDB_BRANCH" "$BINUTILS_GDB_GIT" "gdb-$GDB_VERSION"
200 git clone
--depth 1 -b "$GCC_BRANCH" "$GCC_GIT" "gcc-$GCC_VERSION"
202 # If the directory already existed, pull upstream changes.
203 git
-C "binutils-$BINUTILS_VERSION" pull
204 git
-C "gdb-$GDB_VERSION" pull
205 git
-C "gcc-$GCC_VERSION" pull
207 echo ">>> Downloading GCC prerequisites"
208 cd "gcc-${GCC_VERSION}"
209 .
/contrib
/download_prerequisites
213 set_target_from_platform
() {
238 HELENOS_TARGET
="${GNU_ARCH}-helenos"
240 # TODO: Clean up this mess.
243 LINUX_TARGET
="${GNU_ARCH}-unknown-elf"
246 LINUX_TARGET
="${GNU_ARCH}-pc-linux-gnu"
249 LINUX_TARGET
="${GNU_ARCH}-linux-gnueabi"
252 LINUX_TARGET
="${GNU_ARCH}-unknown-linux-gnu"
255 LINUX_TARGET
="${GNU_ARCH}-linux-gnu"
263 # This sets the *_TARGET variables
264 set_target_from_platform
"$PLATFORM"
265 if $USE_HELENOS_TARGET ; then
266 TARGET
="$HELENOS_TARGET"
268 TARGET
="$LINUX_TARGET"
271 WORKDIR
="${BASEDIR}/${TARGET}"
272 INSTALL_DIR
="${WORKDIR}/PKG"
273 BINUTILSDIR
="${WORKDIR}/binutils-${BINUTILS_VERSION}"
274 GCCDIR
="${WORKDIR}/gcc-${GCC_VERSION}"
275 GDBDIR
="${WORKDIR}/gdb-${GDB_VERSION}"
277 if [ -z "${CROSS_PREFIX}" ] ; then
278 CROSS_PREFIX
="/usr/local/cross"
281 if [ -z "$JOBS" ] ; then
285 PREFIX
="${CROSS_PREFIX}/${TARGET}"
287 echo ">>> Removing previous content"
288 cleanup_dir
"${WORKDIR}"
289 mkdir
-p "${WORKDIR}"
290 check_dirs
"${PREFIX}" "${WORKDIR}"
292 if $USE_HELENOS_TARGET ; then
293 echo ">>> Creating build sysroot"
294 mkdir
-p "${WORKDIR}/sysroot/include"
295 mkdir
"${WORKDIR}/sysroot/lib"
296 cp -r -L -t "${WORKDIR}/sysroot/include" \
297 ${SRCDIR}/..
/abi
/include
/* \
298 ${SRCDIR}/..
/uspace
/lib
/c
/arch
/${PLATFORM}/include
/* \
299 ${SRCDIR}/..
/uspace
/lib
/c
/include
/*
300 check_error $?
"Failed to create build sysroot."
303 echo ">>> Processing binutils (${PLATFORM})"
304 mkdir
-p "${BINUTILSDIR}"
306 check_error $?
"Change directory failed."
308 change_title
"binutils: configure (${PLATFORM})"
309 CFLAGS
=-Wno-error "${BASEDIR}/downloads/binutils-${BINUTILS_VERSION}/configure" \
310 "--target=${TARGET}" \
311 "--prefix=${PREFIX}" \
312 "--program-prefix=${TARGET}-" \
316 --enable-deterministic-archives \
319 check_error $?
"Error configuring binutils."
321 change_title
"binutils: make (${PLATFORM})"
323 check_error $?
"Error compiling binutils."
325 change_title
"binutils: install (${PLATFORM})"
326 make install "DESTDIR=${INSTALL_DIR}"
327 check_error $?
"Error installing binutils."
330 echo ">>> Processing GCC (${PLATFORM})"
333 check_error $?
"Change directory failed."
335 if $USE_HELENOS_TARGET ; then
336 SYSROOT
=--with-sysroot --with-build-sysroot="${WORKDIR}/sysroot"
338 SYSROOT
=--without-headers
341 change_title
"GCC: configure (${PLATFORM})"
342 PATH
="$PATH:${INSTALL_DIR}/${PREFIX}/bin" "${BASEDIR}/downloads/gcc-${GCC_VERSION}/configure" \
343 "--target=${TARGET}" \
344 "--prefix=${PREFIX}" \
345 "--program-prefix=${TARGET}-" \
349 --enable-languages=c
,c
++,go \
354 check_error $?
"Error configuring GCC."
356 change_title
"GCC: make (${PLATFORM})"
357 PATH
="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-gcc
-j$JOBS
358 check_error $?
"Error compiling GCC."
360 if $USE_HELENOS_TARGET ; then
361 PATH
="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libgcc
-j$JOBS
362 check_error $?
"Error compiling libgcc."
363 # TODO: needs some extra care
364 #PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libatomic -j$JOBS
365 #check_error $? "Error compiling libatomic."
366 #PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libstdc++-v3 -j$JOBS
367 #check_error $? "Error compiling libstdc++."
370 change_title
"GCC: install (${PLATFORM})"
371 PATH
="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-gcc "DESTDIR=${INSTALL_DIR}"
372 if $USE_HELENOS_TARGET ; then
373 PATH
="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libgcc "DESTDIR=${INSTALL_DIR}"
374 #PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libatomic "DESTDIR=${INSTALL_DIR}"
375 #PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libstdc++-v3 "DESTDIR=${INSTALL_DIR}"
377 check_error $?
"Error installing GCC."
380 echo ">>> Processing GDB (${PLATFORM})"
383 check_error $?
"Change directory failed."
385 change_title
"GDB: configure (${PLATFORM})"
386 PATH
="$PATH:${INSTALL_DIR}/${PREFIX}/bin" "${BASEDIR}/downloads/gdb-${GDB_VERSION}/configure" \
387 "--target=${TARGET}" \
388 "--prefix=${PREFIX}" \
389 "--program-prefix=${TARGET}-" \
391 check_error $?
"Error configuring GDB."
393 change_title
"GDB: make (${PLATFORM})"
394 PATH
="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-gdb
-j$JOBS
395 check_error $?
"Error compiling GDB."
397 change_title
"GDB: make (${PLATFORM})"
398 PATH
="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-gdb "DESTDIR=${INSTALL_DIR}"
399 check_error $?
"Error installing GDB."
401 # Symlink clang and lld to the install path.
402 CLANG
="`which clang 2> /dev/null || echo "/usr
/bin
/clang
"`"
403 LLD
="`which ld.lld 2> /dev/null || echo "/usr
/bin
/ld.lld
"`"
405 ln -s $CLANG "${INSTALL_DIR}/${PREFIX}/bin/${TARGET}-clang"
406 ln -s $LLD "${INSTALL_DIR}/${PREFIX}/bin/${TARGET}-ld.lld"
408 if $REAL_INSTALL ; then
409 echo ">>> Moving to the destination directory."
410 cleanup_dir
"${PREFIX}"
411 echo mv "${INSTALL_DIR}/${PREFIX}" "${PREFIX}"
412 mv "${INSTALL_DIR}/${PREFIX}" "${PREFIX}"
416 check_error $?
"Change directory failed."
418 echo ">>> Cleaning up"
419 cleanup_dir
"${WORKDIR}"
422 echo ">>> Cross-compiler for ${TARGET} installed."
425 while [ "$#" -gt 1 ] ; do
432 USE_HELENOS_TARGET
=true
441 if [ "$#" -lt "1" ] ; then
446 amd64|arm32|ia32|ia64|mips32|mips32eb|mips64|ppc32|ppc64|riscv64|sparc64
)
456 build_target
"mips32"
457 build_target
"mips32eb"
458 build_target
"mips64"
461 build_target
"riscv64"
462 build_target
"sparc64"
470 build_target
"mips32"
471 build_target
"mips32eb"
473 build_target
"sparc64"
477 build_target
"amd64" &
478 build_target
"arm32" &
479 build_target
"ia32" &
480 build_target
"ia64" &
481 build_target
"mips32" &
482 build_target
"mips32eb" &
483 build_target
"mips64" &
484 build_target
"ppc32" &
485 build_target
"ppc64" &
486 build_target
"riscv64" &
487 build_target
"sparc64" &
492 build_target
"amd64" &
493 build_target
"arm32" &
496 build_target
"ia32" &
497 build_target
"ia64" &
500 build_target
"mips32" &
501 build_target
"mips32eb" &
504 build_target
"mips64" &
505 build_target
"ppc32" &
508 build_target
"riscv64" &
509 build_target
"ppc64" &
512 build_target
"sparc64" &