source: mainline/tools/toolchain.sh@ 77578e8

Last change on this file since 77578e8 was 8fd0675f, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 3 years ago

Update files with /bin/sh shebang and an extra space

  • Property mode set to 100755
File size: 14.1 KB
Line 
1#!/bin/sh
2#
3# SPDX-FileCopyrightText: 2009 Martin Decky
4#
5# SPDX-License-Identifier: BSD-3-Clause
6#
7
8BINUTILS_GDB_GIT="https://github.com/HelenOS/binutils-gdb.git"
9
10BINUTILS_BRANCH="binutils-2_31_1-helenos"
11BINUTILS_VERSION="2.31.1"
12
13GDB_BRANCH="gdb-8_2-helenos"
14GDB_VERSION="8.2"
15
16GCC_GIT="https://github.com/HelenOS/gcc.git"
17GCC_BRANCH="8_2_0-helenos"
18GCC_VERSION="8.2.0"
19
20BASEDIR="$PWD"
21SRCDIR="$(readlink -f $(dirname "$0"))"
22
23SYSTEM_INSTALL=false
24REAL_INSTALL=true
25USE_HELENOS_TARGET=true
26
27check_error() {
28 if [ "$1" -ne "0" ] ; then
29 echo
30 echo "Script failed: $2"
31
32 exit 1
33 fi
34}
35
36show_usage() {
37 echo "HelenOS cross-compiler toolchain build script"
38 echo
39 echo "Syntax:"
40 echo " $0 [--system-wide] [--no-install] [--non-helenos-target] <platform>"
41 echo " $0 [--system-wide] --test-version [<platform>]"
42 echo
43 echo "Possible target platforms are:"
44 echo " amd64 AMD64 (x86-64, x64)"
45 echo " arm32 ARM 32b"
46 echo " arm64 AArch64"
47 echo " ia32 IA-32 (x86, i386)"
48 echo " ia64 IA-64 (Itanium)"
49 echo " mips32 MIPS little-endian 32b"
50 echo " mips32eb MIPS big-endian 32b"
51 echo " ppc32 PowerPC 32b"
52 echo " riscv64 RISC-V 64b"
53 echo " sparc64 SPARC V9"
54 echo " all build all targets"
55 echo " essential build only targets currently needed for HelenOS development"
56 echo " parallel same as 'all', but all in parallel"
57 echo " 2-way same as 'all', but 2-way parallel"
58 echo
59 echo "The toolchain target installation directory is determined by matching"
60 echo "the first of the following conditions:"
61 echo
62 echo " (1) If the \$CROSS_PREFIX environment variable is set, then it is"
63 echo " used as the target installation directory."
64 echo " (2) If the --system-wide option is used, then /opt/HelenOS/cross"
65 echo " is used as the target installation directory. This usually"
66 echo " requires running this script with super user privileges."
67 echo " (3) In other cases, \$XDG_DATA_HOME/HelenOS/cross is used as the"
68 echo " target installation directory. If the \$XDG_DATA_HOME environment"
69 echo " variable is not set, then the default value of \$HOME/.local/share"
70 echo " is assumed."
71 echo
72 echo "If the --no-install option is used, the toolchain still uses the"
73 echo "target installation directory as determined above, but the files"
74 echo "are actually copied into the PKG/ subdirectory during the installation"
75 echo "without affecting the actual target file system. This might be useful"
76 echo "when preparing a system-wide installation, but avoiding running this"
77 echo "script under the super user."
78 echo
79 echo "The --non-helenos-target option will build non-HelenOS-specific"
80 echo "toolchain (i.e. it will use *-linux-* triplet instead of *-helenos)."
81 echo "Using this toolchain for building HelenOS is not supported."
82 echo
83 echo "The --test-version mode tests the currently installed version of the"
84 echo "toolchain."
85
86 exit 3
87}
88
89set_cross_prefix() {
90 if [ -z "$CROSS_PREFIX" ] ; then
91 if $SYSTEM_INSTALL ; then
92 CROSS_PREFIX="/opt/HelenOS/cross"
93 else
94 if [ -z "$XDG_DATA_HOME" ] ; then
95 XDG_DATA_HOME="$HOME/.local/share"
96 fi
97 CROSS_PREFIX="$XDG_DATA_HOME/HelenOS/cross"
98 fi
99 fi
100}
101
102test_version() {
103 set_cross_prefix
104
105 echo "HelenOS cross-compiler toolchain build script"
106 echo
107 echo "Testing the version of the installed software in $CROSS_PREFIX"
108 echo
109
110 if [ -z "$1" ] || [ "$1" = "all" ] ; then
111 PLATFORMS='amd64 arm32 arm64 ia32 ia64 mips32 mips32eb ppc32 riscv64 sparc64'
112 else
113 PLATFORMS="$1"
114 fi
115
116 for PLATFORM in $PLATFORMS ; do
117 set_target_from_platform "$PLATFORM"
118 PREFIX="${CROSS_PREFIX}/bin/${HELENOS_TARGET}"
119
120 echo "== $PLATFORM =="
121 test_app_version "Binutils" "ld" "GNU ld (.*) \([.0-9]*\)" "$BINUTILS_VERSION"
122 test_app_version "GCC" "gcc" "gcc version \([.0-9]*\)" "$GCC_VERSION"
123 test_app_version "GDB" "gdb" "GNU gdb (.*)[[:space:]]\+\([.0-9]*\)" "$GDB_VERSION"
124 done
125}
126
127test_app_version() {
128 PKGNAME="$1"
129 APPNAME="$2"
130 REGEX="$3"
131 INS_VERSION="$4"
132
133 APP="${PREFIX}-${APPNAME}"
134 if [ ! -e $APP ]; then
135 echo "- $PKGNAME is missing"
136 else
137 VERSION=`${APP} -v 2>&1 | sed -n "s:^${REGEX}.*:\1:p"`
138
139 if [ -z "$VERSION" ]; then
140 echo "- $PKGNAME Unexpected output"
141 return 1
142 fi
143
144 if [ "$INS_VERSION" = "$VERSION" ]; then
145 echo "+ $PKGNAME is up-to-date ($INS_VERSION)"
146 else
147 echo "- $PKGNAME ($VERSION) is outdated ($INS_VERSION)"
148 fi
149 fi
150}
151
152change_title() {
153 printf "\e]0;$1\a"
154}
155
156show_countdown() {
157 TM="$1"
158
159 if [ "${TM}" -eq 0 ] ; then
160 echo
161 return 0
162 fi
163
164 printf "${TM} "
165 change_title "${TM}"
166 sleep 1
167
168 TM="`expr "${TM}" - 1`"
169 show_countdown "${TM}"
170}
171
172show_dependencies() {
173 set_cross_prefix
174
175 echo "HelenOS cross-compiler toolchain build script"
176 echo
177 echo "Installing software to $CROSS_PREFIX"
178 echo
179 echo
180 echo "IMPORTANT NOTICE:"
181 echo
182 echo "For a successful compilation and use of the cross-compiler"
183 echo "toolchain you need at least the following dependencies."
184 echo
185 echo "Please make sure that the dependencies are present in your"
186 echo "system. Otherwise the compilation process might fail after"
187 echo "a few seconds or minutes."
188 echo
189 echo " - SED, AWK, Flex, Bison, gzip, bzip2, Bourne Shell"
190 echo " - gettext, zlib, Texinfo, libelf, libgomp"
191 echo " - GNU Make, Coreutils, Sharutils, tar"
192 echo " - native C and C++ compiler, assembler and linker"
193 echo " - native C and C++ standard library with headers"
194 echo
195}
196
197cleanup_dir() {
198 DIR="$1"
199
200 if [ -d "${DIR}" ] ; then
201 change_title "Removing ${DIR}"
202 echo " >>> Removing ${DIR}"
203 rm -fr "${DIR}"
204 fi
205}
206
207create_dir() {
208 DIR="$1"
209 DESC="$2"
210
211 change_title "Creating ${DESC}"
212 echo ">>> Creating ${DESC}"
213
214 mkdir -p "${DIR}"
215 test -d "${DIR}"
216 check_error $? "Unable to create ${DIR}."
217}
218
219check_dirs() {
220 OUTSIDE="$1"
221 BASE="$2"
222 ORIGINAL="$PWD"
223
224 cd "${BASE}"
225 check_error $? "Unable to change directory to ${BASE}."
226 ABS_BASE="$PWD"
227 cd "${ORIGINAL}"
228 check_error $? "Unable to change directory to ${ORIGINAL}."
229
230 mkdir -p "${OUTSIDE}"
231 cd "${OUTSIDE}"
232 check_error $? "Unable to change directory to ${OUTSIDE}."
233
234 while [ "${#PWD}" -gt "${#ABS_BASE}" ]; do
235 cd ..
236 done
237
238 if [ "$PWD" = "$ABS_BASE" ]; then
239 echo
240 echo "CROSS_PREFIX cannot reside within the working directory."
241
242 exit 5
243 fi
244
245 cd "${ORIGINAL}"
246 check_error $? "Unable to change directory to ${ORIGINAL}."
247}
248
249prepare() {
250 show_dependencies
251 show_countdown 10
252
253 mkdir -p "${BASEDIR}/downloads"
254 cd "${BASEDIR}/downloads"
255 check_error $? "Change directory failed."
256
257 change_title "Downloading sources"
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"
262
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
267
268 change_title "Downloading GCC prerequisites"
269 echo ">>> Downloading GCC prerequisites"
270 cd "gcc-${GCC_VERSION}"
271 ./contrib/download_prerequisites
272 cd ..
273}
274
275set_target_from_platform() {
276 case "$1" in
277 "arm32")
278 GNU_ARCH="arm"
279 ;;
280 "arm64")
281 GNU_ARCH="aarch64"
282 ;;
283 "ia32")
284 GNU_ARCH="i686"
285 ;;
286 "mips32")
287 GNU_ARCH="mipsel"
288 ;;
289 "mips32eb")
290 GNU_ARCH="mips"
291 ;;
292 "ppc32")
293 GNU_ARCH="ppc"
294 ;;
295 *)
296 GNU_ARCH="$1"
297 ;;
298 esac
299
300 HELENOS_TARGET="${GNU_ARCH}-helenos"
301
302 case "$1" in
303 "arm32")
304 LINUX_TARGET="${GNU_ARCH}-linux-gnueabi"
305 ;;
306 *)
307 LINUX_TARGET="${GNU_ARCH}-linux-gnu"
308 ;;
309 esac
310}
311
312build_target() {
313 PLATFORM="$1"
314
315 # This sets the *_TARGET variables
316 set_target_from_platform "$PLATFORM"
317 if $USE_HELENOS_TARGET ; then
318 TARGET="$HELENOS_TARGET"
319 else
320 TARGET="$LINUX_TARGET"
321 fi
322
323 WORKDIR="${BASEDIR}/${TARGET}"
324 INSTALL_DIR="${BASEDIR}/PKG"
325 BINUTILSDIR="${WORKDIR}/binutils-${BINUTILS_VERSION}"
326 GCCDIR="${WORKDIR}/gcc-${GCC_VERSION}"
327 GDBDIR="${WORKDIR}/gdb-${GDB_VERSION}"
328
329 # This sets the CROSS_PREFIX variable
330 set_cross_prefix
331
332 if [ -z "$JOBS" ] ; then
333 JOBS="`nproc`"
334 fi
335
336 PREFIX="${CROSS_PREFIX}"
337
338 echo ">>> Removing previous content"
339 cleanup_dir "${WORKDIR}"
340 mkdir -p "${WORKDIR}"
341 check_dirs "${PREFIX}" "${WORKDIR}"
342
343 if $USE_HELENOS_TARGET ; then
344 change_title "Creating build sysroot"
345 echo ">>> Creating build sysroot"
346 mkdir -p "${WORKDIR}/sysroot/include"
347 mkdir "${WORKDIR}/sysroot/lib"
348 ARCH="$PLATFORM"
349 if [ "$ARCH" = "mips32eb" ]; then
350 ARCH=mips32
351 fi
352
353 cp -r -L -t "${WORKDIR}/sysroot/include" \
354 ${SRCDIR}/../abi/include/* \
355 ${SRCDIR}/../uspace/lib/c/arch/${ARCH}/include/* \
356 ${SRCDIR}/../uspace/lib/c/include/*
357 check_error $? "Failed to create build sysroot."
358 fi
359
360
361 echo ">>> Processing binutils (${PLATFORM})"
362 mkdir -p "${BINUTILSDIR}"
363 cd "${BINUTILSDIR}"
364 check_error $? "Change directory failed."
365
366 change_title "binutils: configure (${PLATFORM})"
367 CFLAGS="-Wno-error -fcommon" "${BASEDIR}/downloads/binutils-${BINUTILS_VERSION}/configure" \
368 "--target=${TARGET}" \
369 "--prefix=${PREFIX}" \
370 "--program-prefix=${TARGET}-" \
371 --disable-nls \
372 --disable-werror \
373 --enable-gold \
374 --enable-deterministic-archives \
375 --disable-gdb \
376 --with-sysroot
377 check_error $? "Error configuring binutils."
378
379 change_title "binutils: make (${PLATFORM})"
380 make all -j$JOBS
381 check_error $? "Error compiling binutils."
382
383 change_title "binutils: install (${PLATFORM})"
384 make install "DESTDIR=${INSTALL_DIR}"
385 check_error $? "Error installing binutils."
386
387
388 echo ">>> Processing GCC (${PLATFORM})"
389 mkdir -p "${GCCDIR}"
390 cd "${GCCDIR}"
391 check_error $? "Change directory failed."
392
393 if $USE_HELENOS_TARGET ; then
394 SYSROOT=--with-sysroot --with-build-sysroot="${WORKDIR}/sysroot"
395 else
396 SYSROOT=--without-headers
397 fi
398
399 change_title "GCC: configure (${PLATFORM})"
400 PATH="$PATH:${INSTALL_DIR}/${PREFIX}/bin" "${BASEDIR}/downloads/gcc-${GCC_VERSION}/configure" \
401 "--target=${TARGET}" \
402 "--prefix=${PREFIX}" \
403 "--program-prefix=${TARGET}-" \
404 --with-gnu-as \
405 --with-gnu-ld \
406 --disable-nls \
407 --enable-languages=c,c++,go \
408 --enable-lto \
409 --disable-shared \
410 --disable-werror \
411 $SYSROOT
412 check_error $? "Error configuring GCC."
413
414 change_title "GCC: make (${PLATFORM})"
415 PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-gcc -j$JOBS
416 check_error $? "Error compiling GCC."
417
418 if $USE_HELENOS_TARGET ; then
419 PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libgcc -j$JOBS
420 check_error $? "Error compiling libgcc."
421 # TODO: libatomic and libstdc++ need some extra care
422 # PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libatomic -j$JOBS
423 # check_error $? "Error compiling libatomic."
424 # PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libstdc++-v3 -j$JOBS
425 # check_error $? "Error compiling libstdc++."
426 fi
427
428 change_title "GCC: install (${PLATFORM})"
429 PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-gcc "DESTDIR=${INSTALL_DIR}"
430 if $USE_HELENOS_TARGET ; then
431 PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libgcc "DESTDIR=${INSTALL_DIR}"
432 # PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libatomic "DESTDIR=${INSTALL_DIR}"
433 # PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libstdc++-v3 "DESTDIR=${INSTALL_DIR}"
434 fi
435 check_error $? "Error installing GCC."
436
437
438 echo ">>> Processing GDB (${PLATFORM})"
439 mkdir -p "${GDBDIR}"
440 cd "${GDBDIR}"
441 check_error $? "Change directory failed."
442
443 change_title "GDB: configure (${PLATFORM})"
444 CFLAGS="-fcommon" PATH="$PATH:${INSTALL_DIR}/${PREFIX}/bin" "${BASEDIR}/downloads/gdb-${GDB_VERSION}/configure" \
445 "--target=${TARGET}" \
446 "--prefix=${PREFIX}" \
447 "--program-prefix=${TARGET}-" \
448 --enable-werror=no
449 check_error $? "Error configuring GDB."
450
451 change_title "GDB: make (${PLATFORM})"
452 PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-gdb -j$JOBS
453 check_error $? "Error compiling GDB."
454
455 change_title "GDB: make (${PLATFORM})"
456 PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-gdb "DESTDIR=${INSTALL_DIR}"
457 check_error $? "Error installing GDB."
458
459 # Symlink clang and lld to the install path.
460 CLANG="`which clang 2> /dev/null || echo "/usr/bin/clang"`"
461 LLD="`which ld.lld 2> /dev/null || echo "/usr/bin/ld.lld"`"
462
463 ln -s $CLANG "${INSTALL_DIR}/${PREFIX}/bin/${TARGET}-clang"
464 ln -s $LLD "${INSTALL_DIR}/${PREFIX}/bin/${TARGET}-ld.lld"
465
466 if $REAL_INSTALL ; then
467 echo ">>> Moving to the destination directory."
468 cp -r -t "${PREFIX}" "${INSTALL_DIR}/${PREFIX}/"*
469 fi
470
471 cd "${BASEDIR}"
472 check_error $? "Change directory failed."
473
474 echo ">>> Cleaning up"
475 cleanup_dir "${WORKDIR}"
476
477 echo
478 echo ">>> Cross-compiler for ${TARGET} installed."
479}
480
481while [ "$#" -gt 1 ] ; do
482 case "$1" in
483 --system-wide)
484 SYSTEM_INSTALL=true
485 shift
486 ;;
487 --test-version)
488 test_version "$2"
489 exit
490 ;;
491 --no-install)
492 REAL_INSTALL=false
493 shift
494 ;;
495 --non-helenos-target)
496 USE_HELENOS_TARGET=false
497 shift
498 ;;
499 *)
500 show_usage
501 ;;
502 esac
503done
504
505if [ "$#" -lt "1" ] ; then
506 show_usage
507fi
508
509case "$1" in
510 --test-version)
511 test_version
512 exit
513 ;;
514 amd64|arm32|arm64|ia32|ia64|mips32|mips32eb|ppc32|riscv64|sparc64)
515 prepare
516 build_target "$1"
517 ;;
518 "all")
519 prepare
520 build_target "amd64"
521 build_target "arm32"
522 build_target "arm64"
523 build_target "ia32"
524 build_target "ia64"
525 build_target "mips32"
526 build_target "mips32eb"
527 build_target "ppc32"
528 build_target "riscv64"
529 build_target "sparc64"
530 ;;
531 "essential")
532 prepare
533 build_target "amd64"
534 build_target "arm32"
535 build_target "arm64"
536 build_target "ia32"
537 build_target "ia64"
538 build_target "mips32"
539 build_target "mips32eb"
540 build_target "ppc32"
541 build_target "sparc64"
542 ;;
543 "parallel")
544 prepare
545 build_target "amd64" &
546 build_target "arm32" &
547 build_target "arm64" &
548 build_target "ia32" &
549 build_target "ia64" &
550 build_target "mips32" &
551 build_target "mips32eb" &
552 build_target "ppc32" &
553 build_target "riscv64" &
554 build_target "sparc64" &
555 wait
556 ;;
557 "2-way")
558 prepare
559 build_target "amd64" &
560 build_target "arm32" &
561 wait
562
563 build_target "arm64" &
564 build_target "ia32" &
565 wait
566
567 build_target "ia64" &
568 build_target "mips32" &
569 wait
570
571 build_target "mips32eb" &
572 build_target "ppc32" &
573 wait
574
575 build_target "riscv64" &
576 build_target "sparc64" &
577 wait
578 ;;
579 *)
580 show_usage
581 ;;
582esac
Note: See TracBrowser for help on using the repository browser.