source: mainline/tools/toolchain.sh@ 10fc93c

topic/simplify-dev-export
Last change on this file since 10fc93c was 3e05a69, checked in by Vojtech Horky <vojtech.horky@…>, 21 months ago

Fix toolchain.sh —test-version

We use only $TARGET now (probably forgotten change in the cleanup in
commit 0b5aa1f073776eb55ebd8bb41de4d74d4ccb147c).

  • Property mode set to 100755
File size: 14.8 KB
Line 
1#!/bin/sh
2
3#
4# Copyright (c) 2009 Martin Decky
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10#
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.
18#
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.
29#
30
31BINUTILS_GDB_GIT="https://github.com/HelenOS/binutils-gdb.git"
32
33BINUTILS_BRANCH="binutils-2_41-helenos"
34BINUTILS_VERSION="2.41"
35
36GDB_BRANCH="gdb-13.2-helenos"
37GDB_VERSION="13.2"
38
39GCC_GIT="https://github.com/HelenOS/gcc.git"
40GCC_BRANCH="13_2_0-helenos"
41GCC_VERSION="13.2"
42
43BASEDIR="$PWD"
44SRCDIR="$(readlink -f $(dirname "$0"))"
45
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.
49INSTALL_DIR="${BASEDIR}/PKG"
50
51SYSTEM_INSTALL=false
52
53BUILD_GDB=false
54BUILD_BINUTILS=true
55BUILD_GCC=true
56
57if [ -z "$JOBS" ] ; then
58 JOBS="`nproc`"
59fi
60
61check_error() {
62 if [ "$1" -ne "0" ] ; then
63 echo
64 echo "Script failed: $2"
65
66 exit 1
67 fi
68}
69
70show_usage() {
71 echo "HelenOS cross-compiler toolchain build script"
72 echo
73 echo "Syntax:"
74 echo " $0 [--system-wide] [--with-gdb|--only-gdb] <platform>"
75 echo " $0 [--system-wide] --test-version [<platform>]"
76 echo
77 echo "Possible target platforms are:"
78 echo " amd64 AMD64 (x86-64, x64)"
79 echo " arm32 ARM 32b"
80 echo " arm64 AArch64"
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"
91 echo
92 echo "The toolchain target installation directory is determined by matching"
93 echo "the first of the following conditions:"
94 echo
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"
103 echo " is assumed."
104 echo
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."
108 echo
109 echo "The --test-version mode tests the currently installed version of the"
110 echo "toolchain."
111
112 exit 3
113}
114
115set_cross_prefix() {
116 if [ -z "$CROSS_PREFIX" ] ; then
117 if $SYSTEM_INSTALL ; then
118 CROSS_PREFIX="/opt/HelenOS/cross"
119 else
120 if [ -z "$XDG_DATA_HOME" ] ; then
121 XDG_DATA_HOME="$HOME/.local/share"
122 fi
123 CROSS_PREFIX="$XDG_DATA_HOME/HelenOS/cross"
124 fi
125 fi
126}
127
128test_version() {
129 set_cross_prefix
130
131 echo "HelenOS cross-compiler toolchain build script"
132 echo
133 echo "Testing the version of the installed software in $CROSS_PREFIX"
134 echo
135
136 if [ -z "$1" ] || [ "$1" = "all" ] ; then
137 PLATFORMS='amd64 arm32 arm64 ia32 ia64 mips32 mips32eb ppc32 riscv64 sparc64'
138 else
139 PLATFORMS="$1"
140 fi
141
142 for PLATFORM in $PLATFORMS ; do
143 set_target_from_platform "$PLATFORM"
144 PREFIX="${CROSS_PREFIX}/bin/${TARGET}"
145
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"
150 done
151}
152
153test_app_version() {
154 PKGNAME="$1"
155 APPNAME="$2"
156 REGEX="$3"
157 INS_VERSION="$4"
158
159 APP="${PREFIX}-${APPNAME}"
160 if [ ! -e $APP ]; then
161 echo "- $PKGNAME is missing"
162 else
163 VERSION=`${APP} -v 2>&1 | sed -n "s:^${REGEX}.*:\1:p"`
164
165 if [ -z "$VERSION" ]; then
166 echo "- $PKGNAME Unexpected output"
167 return 1
168 fi
169
170 if [ "$INS_VERSION" = "$VERSION" ]; then
171 echo "+ $PKGNAME is up-to-date ($INS_VERSION)"
172 else
173 echo "- $PKGNAME ($VERSION) is outdated ($INS_VERSION)"
174 fi
175 fi
176}
177
178change_title() {
179 printf "\e]0;$1\a"
180}
181
182ring_bell() {
183 printf '\a'
184 sleep 0.1
185 printf '\a'
186 sleep 0.1
187 printf '\a'
188 sleep 0.1
189 printf '\a'
190 sleep 0.1
191 printf '\a'
192}
193
194show_countdown() {
195 TM="$1"
196
197 if [ "${TM}" -eq 0 ] ; then
198 echo
199 return 0
200 fi
201
202 printf "${TM} "
203 change_title "${TM}"
204 sleep 1
205
206 TM="`expr "${TM}" - 1`"
207 show_countdown "${TM}"
208}
209
210show_dependencies() {
211 set_cross_prefix
212
213 echo "HelenOS cross-compiler toolchain build script"
214 echo
215 echo "Installing software to $CROSS_PREFIX"
216 echo
217 echo
218 echo "IMPORTANT NOTICE:"
219 echo
220 echo "For a successful compilation and use of the cross-compiler"
221 echo "toolchain you need at least the following dependencies."
222 echo
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."
226 echo
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"
232 echo
233}
234
235cleanup_dir() {
236 DIR="$1"
237
238 if [ -d "${DIR}" ] ; then
239 change_title "Removing ${DIR}"
240 echo " >>> Removing ${DIR}"
241 rm -fr "${DIR}"
242 fi
243}
244
245create_dir() {
246 DIR="$1"
247 DESC="$2"
248
249 change_title "Creating ${DESC}"
250 echo ">>> Creating ${DESC}"
251
252 mkdir -p "${DIR}"
253 test -d "${DIR}"
254 check_error $? "Unable to create ${DIR}."
255}
256
257check_dirs() {
258 cd "${BASEDIR}"
259 check_error $? "Unable to change directory to ${BASEDIR}."
260 ABS_BASE="$PWD"
261
262 if $SYSTEM_INSTALL && [ ! -d "${CROSS_PREFIX}" ]; then
263 ring_bell
264 ( set -x ; sudo -k mkdir -p "${CROSS_PREFIX}" )
265 else
266 mkdir -p "${CROSS_PREFIX}"
267 fi
268
269 cd "${CROSS_PREFIX}"
270 check_error $? "Unable to change directory to ${CROSS_PREFIX}."
271
272 while [ "${#PWD}" -gt "${#ABS_BASE}" ]; do
273 cd ..
274 done
275
276 if [ "$PWD" = "$ABS_BASE" ]; then
277 echo
278 echo "CROSS_PREFIX cannot reside within the working directory."
279
280 exit 5
281 fi
282
283 cd "${BASEDIR}"
284}
285
286prepare() {
287 show_dependencies
288 show_countdown 10
289
290 mkdir -p "${BASEDIR}/downloads"
291 cd "${BASEDIR}/downloads"
292 check_error $? "Change directory failed."
293
294 change_title "Downloading sources"
295 echo ">>> Downloading sources"
296
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
301 fi
302
303 if $BUILD_GCC ; then
304 git clone --depth 1 -b "$GCC_BRANCH" "$GCC_GIT" "gcc-$GCC_VERSION"
305 git -C "gcc-$GCC_VERSION" pull
306
307 change_title "Downloading GCC prerequisites"
308 echo ">>> Downloading GCC prerequisites"
309 cd "gcc-${GCC_VERSION}"
310 ./contrib/download_prerequisites
311 cd ..
312 fi
313
314 if $BUILD_GDB ; then
315 git clone --depth 1 -b "$GDB_BRANCH" "$BINUTILS_GDB_GIT" "gdb-$GDB_VERSION"
316 git -C "gdb-$GDB_VERSION" pull
317 fi
318
319 # This sets the CROSS_PREFIX variable
320 set_cross_prefix
321
322 DESTDIR_SPEC="DESTDIR=${INSTALL_DIR}"
323
324 check_dirs
325}
326
327set_target_from_platform() {
328 case "$1" in
329 "arm32")
330 GNU_ARCH="arm"
331 ;;
332 "arm64")
333 GNU_ARCH="aarch64"
334 ;;
335 "ia32")
336 GNU_ARCH="i686"
337 ;;
338 "mips32")
339 GNU_ARCH="mipsel"
340 ;;
341 "mips32eb")
342 GNU_ARCH="mips"
343 ;;
344 "ppc32")
345 GNU_ARCH="ppc"
346 ;;
347 *)
348 GNU_ARCH="$1"
349 ;;
350 esac
351
352 TARGET="${GNU_ARCH}-helenos"
353}
354
355build_binutils() {
356 # This sets the TARGET variable
357 set_target_from_platform "$1"
358
359 WORKDIR="${BASEDIR}/${TARGET}"
360 BINUTILSDIR="${WORKDIR}/binutils-${BINUTILS_VERSION}"
361
362 echo ">>> Removing previous content"
363 cleanup_dir "${WORKDIR}"
364 mkdir -p "${WORKDIR}"
365
366 echo ">>> Processing binutils (${TARGET})"
367 mkdir -p "${BINUTILSDIR}"
368 cd "${BINUTILSDIR}"
369 check_error $? "Change directory failed."
370
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}-" \
376 --disable-nls \
377 --disable-werror \
378 --enable-gold \
379 --enable-deterministic-archives \
380 --disable-gdb \
381 --with-sysroot
382 check_error $? "Error configuring binutils."
383
384 change_title "binutils: make (${TARGET})"
385 make all -j$JOBS
386 check_error $? "Error compiling binutils."
387
388 change_title "binutils: install (${TARGET})"
389 make install $DESTDIR_SPEC
390 check_error $? "Error installing binutils."
391}
392
393build_gcc() {
394 # This sets the TARGET variable
395 set_target_from_platform "$1"
396
397 WORKDIR="${BASEDIR}/${TARGET}"
398 GCCDIR="${WORKDIR}/gcc-${GCC_VERSION}"
399
400 echo ">>> Removing previous content"
401 cleanup_dir "${WORKDIR}"
402 mkdir -p "${WORKDIR}"
403
404 echo ">>> Processing GCC (${TARGET})"
405 mkdir -p "${GCCDIR}"
406 cd "${GCCDIR}"
407 check_error $? "Change directory failed."
408
409 BUILDPATH="${CROSS_PREFIX}/bin:${PATH}"
410
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}-" \
416 --with-gnu-as \
417 --with-gnu-ld \
418 --disable-nls \
419 --enable-languages=c,c++,go \
420 --enable-lto \
421 --disable-shared \
422 --disable-werror \
423 --without-headers # TODO: Replace with proper sysroot so we can build more libs
424 check_error $? "Error configuring GCC."
425
426 change_title "GCC: make (${TARGET})"
427 PATH="${BUILDPATH}" make all-gcc -j$JOBS
428 check_error $? "Error compiling GCC."
429
430 change_title "GCC: install (${TARGET})"
431 PATH="${BUILDPATH}" make install-gcc $DESTDIR_SPEC
432 check_error $? "Error installing GCC."
433}
434
435build_libgcc() {
436 # This sets the TARGET variable
437 set_target_from_platform "$1"
438
439 WORKDIR="${BASEDIR}/${TARGET}"
440 GCCDIR="${WORKDIR}/gcc-${GCC_VERSION}"
441
442 # No removing previous content here, we need the previous GCC build
443
444 cd "${GCCDIR}"
445 check_error $? "Change directory failed."
446
447 BUILDPATH="${CROSS_PREFIX}/bin:${PATH}"
448
449 change_title "libgcc: make (${TARGET})"
450
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++."
458
459 change_title "libgcc: install (${TARGET})"
460
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."
465}
466
467build_gdb() {
468 # This sets the TARGET variable
469 set_target_from_platform "$1"
470
471 WORKDIR="${BASEDIR}/${TARGET}"
472 GDBDIR="${WORKDIR}/gdb-${GDB_VERSION}"
473
474 echo ">>> Removing previous content"
475 cleanup_dir "${WORKDIR}"
476 mkdir -p "${WORKDIR}"
477
478 echo ">>> Processing GDB (${TARGET})"
479 mkdir -p "${GDBDIR}"
480 cd "${GDBDIR}"
481 check_error $? "Change directory failed."
482
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}-" \
488 --enable-werror=no
489 check_error $? "Error configuring GDB."
490
491 change_title "GDB: make (${TARGET})"
492 make all-gdb -j$JOBS
493 check_error $? "Error compiling GDB."
494
495 change_title "GDB: install (${TARGET})"
496 make install-gdb $DESTDIR_SPEC
497 check_error $? "Error installing GDB."
498}
499
500install_pkg() {
501 echo ">>> Moving to the destination directory."
502 if $SYSTEM_INSTALL ; then
503 ring_bell
504 ( set -x ; sudo -k cp -r -t "${CROSS_PREFIX}" "${INSTALL_DIR}${CROSS_PREFIX}/"* )
505 else
506 ( set -x ; cp -r -t "${CROSS_PREFIX}" "${INSTALL_DIR}${CROSS_PREFIX}/"* )
507 fi
508}
509
510link_clang() {
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"`"
514
515 ln -s $CLANG "${INSTALL_DIR}${CROSS_PREFIX}/bin/${TARGET}-clang"
516 ln -s $LLD "${INSTALL_DIR}${CROSS_PREFIX}/bin/${TARGET}-ld.lld"
517}
518
519while [ "$#" -gt 1 ] ; do
520 case "$1" in
521 --system-wide)
522 SYSTEM_INSTALL=true
523 shift
524 ;;
525 --test-version)
526 test_version "$2"
527 exit
528 ;;
529 --with-gdb)
530 BUILD_GDB=true
531 shift
532 ;;
533 --only-gdb)
534 BUILD_GDB=true
535 BUILD_BINUTILS=false
536 BUILD_GCC=false
537 shift
538 ;;
539 *)
540 show_usage
541 ;;
542 esac
543done
544
545if [ "$#" -lt "1" ] ; then
546 show_usage
547fi
548
549PLATFORMS="amd64 arm32 arm64 ia32 ia64 mips32 mips32eb ppc32 riscv64 sparc64"
550
551run_one() {
552 $1 $PLATFORM
553}
554
555run_all() {
556 for x in $PLATFORMS ; do
557 $1 $x
558 done
559}
560
561run_parallel() {
562 for x in $PLATFORMS ; do
563 $1 $x &
564 done
565 wait
566}
567
568run_2way() {
569 $1 amd64 &
570 $1 arm32 &
571 wait
572
573 $1 arm64 &
574 $1 ia32 &
575 wait
576
577 $1 ia64 &
578 $1 mips32 &
579 wait
580
581 $1 mips32eb &
582 $1 ppc32 &
583 wait
584
585 $1 riscv64 &
586 $1 sparc64 &
587 wait
588}
589
590everything() {
591 RUNNER="$1"
592
593 prepare
594
595 if $BUILD_BINUTILS ; then
596 $RUNNER build_binutils
597
598 if $BUILD_GCC ; then
599 # gcc/libgcc may fail to build correctly if binutils is not installed first
600 echo ">>> Installing binutils"
601 install_pkg
602 fi
603 fi
604
605 if $BUILD_GCC ; then
606 $RUNNER build_gcc
607
608 # libgcc may fail to build correctly if gcc is not installed first
609 echo ">>> Installing GCC"
610 install_pkg
611
612 $RUNNER build_libgcc
613 fi
614
615 if $BUILD_GDB ; then
616 $RUNNER build_gdb
617 fi
618
619 echo ">>> Installing all files"
620 install_pkg
621
622 link_clang
623}
624
625case "$1" in
626 --test-version)
627 test_version
628 exit
629 ;;
630 amd64|arm32|arm64|ia32|ia64|mips32|mips32eb|ppc32|riscv64|sparc64)
631 PLATFORM="$1"
632 everything run_one
633 ;;
634 "all")
635 everything run_all
636 ;;
637 "parallel")
638 everything run_parallel
639 ;;
640 "2-way")
641 everything run_2way
642 ;;
643 *)
644 show_usage
645 ;;
646esac
Note: See TracBrowser for help on using the repository browser.