source: mainline/tools/toolchain.sh@ 159776f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 159776f was 40c0483, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 6 years ago

Make shell script tools more portable

  • Property mode set to 100755
File size: 14.1 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_31_1-helenos"
34BINUTILS_VERSION="2.31.1"
35
36GDB_BRANCH="gdb-8_2-helenos"
37GDB_VERSION="8.2"
38
39GCC_GIT="https://github.com/HelenOS/gcc.git"
40GCC_BRANCH="8_2_0-helenos"
41GCC_VERSION="8.2.0"
42
43BASEDIR="`pwd`"
44SRCDIR="$(readlink -f $(dirname "$0"))"
45
46REAL_INSTALL=true
47USE_HELENOS_TARGET=true
48
49check_error() {
50 if [ "$1" -ne "0" ] ; then
51 echo
52 echo "Script failed: $2"
53
54 exit 1
55 fi
56}
57
58show_usage() {
59 echo "Cross-compiler toolchain build script"
60 echo
61 echo "Syntax:"
62 echo " $0 [--no-install] [--non-helenos-target] <platform>"
63 echo " $0 --test-version [<platform>]"
64 echo
65 echo "Possible target platforms are:"
66 echo " amd64 AMD64 (x86-64, x64)"
67 echo " arm32 ARM 32b"
68 echo " arm64 AArch64"
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"
80 echo
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."
84 echo
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."
90 echo
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."
94 echo
95
96 exit 3
97}
98
99test_version() {
100 echo "Cross-compiler toolchain build script"
101 echo
102 echo "Start testing the version of the installed software"
103 echo
104
105 if [ -z "$1" ] || [ "$1" = "all" ] ; then
106 PLATFORMS='amd64 arm32 arm64 ia32 ia64 mips32 mips32eb ppc32 riscv64 sparc64'
107 else
108 PLATFORMS="$1"
109 fi
110
111
112 if [ -z "${CROSS_PREFIX}" ] ; then
113 CROSS_PREFIX="/usr/local/cross"
114 fi
115
116 for i in $PLATFORMS
117 do
118 PLATFORM="$i"
119 set_target_from_platform "$PLATFORM"
120 PREFIX="${CROSS_PREFIX}/bin/${HELENOS_TARGET}"
121
122 echo "== $PLATFORM =="
123 test_app_version "Binutils" "ld" "GNU ld (.*) \([.0-9]*\)" "$BINUTILS_VERSION"
124 test_app_version "GCC" "gcc" "gcc version \([.0-9]*\)" "$GCC_VERSION"
125 test_app_version "GDB" "gdb" "GNU gdb (.*)[[:space:]]\+\([.0-9]*\)" "$GDB_VERSION"
126 done
127
128 exit
129}
130
131test_app_version() {
132 PKGNAME="$1"
133 APPNAME="$2"
134 REGEX="$3"
135 INS_VERSION="$4"
136
137
138 APP="${PREFIX}-${APPNAME}"
139 if [ ! -e $APP ]; then
140 echo "- $PKGNAME is missing"
141 else
142 VERSION=`${APP} -v 2>&1 | sed -n "s:^${REGEX}.*:\1:p"`
143
144 if [ -z "$VERSION" ]; then
145 echo "- $PKGNAME Unexpected output"
146 return 1
147 fi
148
149 if [ "$INS_VERSION" = "$VERSION" ]; then
150 echo "+ $PKGNAME is uptodate ($INS_VERSION)"
151 else
152 echo "- $PKGNAME ($VERSION) is outdated ($INS_VERSION)"
153 fi
154 fi
155}
156
157
158
159change_title() {
160 printf "\e]0;$1\a"
161}
162
163show_countdown() {
164 TM="$1"
165
166 if [ "${TM}" -eq 0 ] ; then
167 echo
168 return 0
169 fi
170
171 printf "${TM} "
172 change_title "${TM}"
173 sleep 1
174
175 TM="`expr "${TM}" - 1`"
176 show_countdown "${TM}"
177}
178
179show_dependencies() {
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 echo ">>> Downloading sources"
258 git clone --depth 1 -b "$BINUTILS_BRANCH" "$BINUTILS_GDB_GIT" "binutils-$BINUTILS_VERSION"
259 git clone --depth 1 -b "$GDB_BRANCH" "$BINUTILS_GDB_GIT" "gdb-$GDB_VERSION"
260 git clone --depth 1 -b "$GCC_BRANCH" "$GCC_GIT" "gcc-$GCC_VERSION"
261
262 # If the directory already existed, pull upstream changes.
263 git -C "binutils-$BINUTILS_VERSION" pull
264 git -C "gdb-$GDB_VERSION" pull
265 git -C "gcc-$GCC_VERSION" pull
266
267 echo ">>> Downloading GCC prerequisites"
268 cd "gcc-${GCC_VERSION}"
269 ./contrib/download_prerequisites
270 cd ..
271}
272
273set_target_from_platform() {
274 case "$1" in
275 "arm32")
276 GNU_ARCH="arm"
277 ;;
278 "arm64")
279 GNU_ARCH="aarch64"
280 ;;
281 "ia32")
282 GNU_ARCH="i686"
283 ;;
284 "mips32")
285 GNU_ARCH="mipsel"
286 ;;
287 "mips32eb")
288 GNU_ARCH="mips"
289 ;;
290 "ppc32")
291 GNU_ARCH="ppc"
292 ;;
293 *)
294 GNU_ARCH="$1"
295 ;;
296 esac
297
298 HELENOS_TARGET="${GNU_ARCH}-helenos"
299
300 case "$1" in
301 "arm32")
302 LINUX_TARGET="${GNU_ARCH}-linux-gnueabi"
303 ;;
304 *)
305 LINUX_TARGET="${GNU_ARCH}-linux-gnu"
306 ;;
307 esac
308}
309
310build_target() {
311 PLATFORM="$1"
312
313 # This sets the *_TARGET variables
314 set_target_from_platform "$PLATFORM"
315 if $USE_HELENOS_TARGET ; then
316 TARGET="$HELENOS_TARGET"
317 else
318 TARGET="$LINUX_TARGET"
319 fi
320
321 WORKDIR="${BASEDIR}/${TARGET}"
322 INSTALL_DIR="${BASEDIR}/PKG"
323 BINUTILSDIR="${WORKDIR}/binutils-${BINUTILS_VERSION}"
324 GCCDIR="${WORKDIR}/gcc-${GCC_VERSION}"
325 GDBDIR="${WORKDIR}/gdb-${GDB_VERSION}"
326
327 if [ -z "${CROSS_PREFIX}" ] ; then
328 CROSS_PREFIX="/usr/local/cross"
329 fi
330
331 if [ -z "$JOBS" ] ; then
332 JOBS=`nproc`
333 fi
334
335 PREFIX="${CROSS_PREFIX}"
336
337 echo ">>> Removing previous content"
338 cleanup_dir "${WORKDIR}"
339 mkdir -p "${WORKDIR}"
340 check_dirs "${PREFIX}" "${WORKDIR}"
341
342 if $USE_HELENOS_TARGET ; then
343 echo ">>> Creating build sysroot"
344 mkdir -p "${WORKDIR}/sysroot/include"
345 mkdir "${WORKDIR}/sysroot/lib"
346 ARCH="$PLATFORM"
347 if [ "$ARCH" = "mips32eb" ]; then
348 ARCH=mips32
349 fi
350
351 cp -r -L -t "${WORKDIR}/sysroot/include" \
352 ${SRCDIR}/../abi/include/* \
353 ${SRCDIR}/../uspace/lib/c/arch/${ARCH}/include/* \
354 ${SRCDIR}/../uspace/lib/c/include/*
355 check_error $? "Failed to create build sysroot."
356 fi
357
358 echo ">>> Processing binutils (${PLATFORM})"
359 mkdir -p "${BINUTILSDIR}"
360 cd "${BINUTILSDIR}"
361 check_error $? "Change directory failed."
362
363 change_title "binutils: configure (${PLATFORM})"
364 CFLAGS=-Wno-error "${BASEDIR}/downloads/binutils-${BINUTILS_VERSION}/configure" \
365 "--target=${TARGET}" \
366 "--prefix=${PREFIX}" \
367 "--program-prefix=${TARGET}-" \
368 --disable-nls \
369 --disable-werror \
370 --enable-gold \
371 --enable-deterministic-archives \
372 --disable-gdb \
373 --with-sysroot
374 check_error $? "Error configuring binutils."
375
376 change_title "binutils: make (${PLATFORM})"
377 make all -j$JOBS
378 check_error $? "Error compiling binutils."
379
380 change_title "binutils: install (${PLATFORM})"
381 make install "DESTDIR=${INSTALL_DIR}"
382 check_error $? "Error installing binutils."
383
384
385 echo ">>> Processing GCC (${PLATFORM})"
386 mkdir -p "${GCCDIR}"
387 cd "${GCCDIR}"
388 check_error $? "Change directory failed."
389
390 if $USE_HELENOS_TARGET ; then
391 SYSROOT=--with-sysroot --with-build-sysroot="${WORKDIR}/sysroot"
392 else
393 SYSROOT=--without-headers
394 fi
395
396 change_title "GCC: configure (${PLATFORM})"
397 PATH="$PATH:${INSTALL_DIR}/${PREFIX}/bin" "${BASEDIR}/downloads/gcc-${GCC_VERSION}/configure" \
398 "--target=${TARGET}" \
399 "--prefix=${PREFIX}" \
400 "--program-prefix=${TARGET}-" \
401 --with-gnu-as \
402 --with-gnu-ld \
403 --disable-nls \
404 --enable-languages=c,c++,go \
405 --enable-lto \
406 --disable-shared \
407 --disable-werror \
408 $SYSROOT
409 check_error $? "Error configuring GCC."
410
411 change_title "GCC: make (${PLATFORM})"
412 PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-gcc -j$JOBS
413 check_error $? "Error compiling GCC."
414
415 if $USE_HELENOS_TARGET ; then
416 PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libgcc -j$JOBS
417 check_error $? "Error compiling libgcc."
418 # TODO: needs some extra care
419 #PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libatomic -j$JOBS
420 #check_error $? "Error compiling libatomic."
421 #PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libstdc++-v3 -j$JOBS
422 #check_error $? "Error compiling libstdc++."
423 fi
424
425 change_title "GCC: install (${PLATFORM})"
426 PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-gcc "DESTDIR=${INSTALL_DIR}"
427 if $USE_HELENOS_TARGET ; then
428 PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libgcc "DESTDIR=${INSTALL_DIR}"
429 #PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libatomic "DESTDIR=${INSTALL_DIR}"
430 #PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libstdc++-v3 "DESTDIR=${INSTALL_DIR}"
431 fi
432 check_error $? "Error installing GCC."
433
434
435 echo ">>> Processing GDB (${PLATFORM})"
436 mkdir -p "${GDBDIR}"
437 cd "${GDBDIR}"
438 check_error $? "Change directory failed."
439
440 change_title "GDB: configure (${PLATFORM})"
441 PATH="$PATH:${INSTALL_DIR}/${PREFIX}/bin" "${BASEDIR}/downloads/gdb-${GDB_VERSION}/configure" \
442 "--target=${TARGET}" \
443 "--prefix=${PREFIX}" \
444 "--program-prefix=${TARGET}-" \
445 --enable-werror=no
446 check_error $? "Error configuring GDB."
447
448 change_title "GDB: make (${PLATFORM})"
449 PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-gdb -j$JOBS
450 check_error $? "Error compiling GDB."
451
452 change_title "GDB: make (${PLATFORM})"
453 PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-gdb "DESTDIR=${INSTALL_DIR}"
454 check_error $? "Error installing GDB."
455
456 # Symlink clang and lld to the install path.
457 CLANG="`which clang 2> /dev/null || echo "/usr/bin/clang"`"
458 LLD="`which ld.lld 2> /dev/null || echo "/usr/bin/ld.lld"`"
459
460 ln -s $CLANG "${INSTALL_DIR}/${PREFIX}/bin/${TARGET}-clang"
461 ln -s $LLD "${INSTALL_DIR}/${PREFIX}/bin/${TARGET}-ld.lld"
462
463 if $REAL_INSTALL ; then
464 echo ">>> Moving to the destination directory."
465 echo cp -r -t "${PREFIX}" "${INSTALL_DIR}/${PREFIX}/"*
466 cp -r -t "${PREFIX}" "${INSTALL_DIR}/${PREFIX}/"*
467 fi
468
469 cd "${BASEDIR}"
470 check_error $? "Change directory failed."
471
472 echo ">>> Cleaning up"
473 cleanup_dir "${WORKDIR}"
474
475 echo
476 echo ">>> Cross-compiler for ${TARGET} installed."
477}
478
479while [ "$#" -gt 1 ] ; do
480 case "$1" in
481 --test-version)
482 test_version "$2"
483 exit
484 ;;
485 --no-install)
486 REAL_INSTALL=false
487 shift
488 ;;
489 --non-helenos-target)
490 USE_HELENOS_TARGET=false
491 shift
492 ;;
493 *)
494 show_usage
495 ;;
496 esac
497done
498
499if [ "$#" -lt "1" ] ; then
500 show_usage
501fi
502
503case "$1" in
504 --test-version)
505 test_version
506 ;;
507 amd64|arm32|arm64|ia32|ia64|mips32|mips32eb|ppc32|riscv64|sparc64)
508 prepare
509 build_target "$1"
510 ;;
511 "all")
512 prepare
513 build_target "amd64"
514 build_target "arm32"
515 build_target "arm64"
516 build_target "ia32"
517 build_target "ia64"
518 build_target "mips32"
519 build_target "mips32eb"
520 build_target "ppc32"
521 build_target "riscv64"
522 build_target "sparc64"
523 ;;
524 "essential")
525 prepare
526 build_target "amd64"
527 build_target "arm32"
528 build_target "arm64"
529 build_target "ia32"
530 build_target "ia64"
531 build_target "mips32"
532 build_target "mips32eb"
533 build_target "ppc32"
534 build_target "sparc64"
535 ;;
536 "parallel")
537 prepare
538 build_target "amd64" &
539 build_target "arm32" &
540 build_target "arm64" &
541 build_target "ia32" &
542 build_target "ia64" &
543 build_target "mips32" &
544 build_target "mips32eb" &
545 build_target "ppc32" &
546 build_target "riscv64" &
547 build_target "sparc64" &
548 wait
549 ;;
550 "2-way")
551 prepare
552 build_target "amd64" &
553 build_target "arm32" &
554 wait
555
556 build_target "arm64" &
557 build_target "ia32" &
558 wait
559
560 build_target "ia64" &
561 build_target "mips32" &
562 wait
563
564 build_target "mips32eb" &
565 build_target "ppc32" &
566 wait
567
568 build_target "riscv64" &
569 build_target "sparc64" &
570 wait
571 ;;
572 *)
573 show_usage
574 ;;
575esac
Note: See TracBrowser for help on using the repository browser.