source: mainline/tools/toolchain.sh@ 13eecc4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 13eecc4 was cb15b49c, checked in by Matthieu Riolo <matthieu.riolo@…>, 7 years ago

implement platform 'all' and adding help message to show_usage

  • Property mode set to 100755
File size: 13.9 KB
Line 
1#! /bin/bash
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 " ia32 IA-32 (x86, i386)"
69 echo " ia64 IA-64 (Itanium)"
70 echo " mips32 MIPS little-endian 32b"
71 echo " mips32eb MIPS big-endian 32b"
72 echo " ppc32 PowerPC 32b"
73 echo " riscv64 RISC-V 64b"
74 echo " sparc64 SPARC V9"
75 echo " all build all targets"
76 echo " essential build only targets currently needed for HelenOS development"
77 echo " parallel same as 'all', but all in parallel"
78 echo " 2-way same as 'all', but 2-way parallel"
79 echo
80 echo "The toolchain is installed into directory specified by the"
81 echo "CROSS_PREFIX environment variable. If the variable is not"
82 echo "defined, /usr/local/cross/ is used as default."
83 echo
84 echo "If --no-install is present, the toolchain still uses the"
85 echo "CROSS_PREFIX as the target directory but the installation"
86 echo "copies the files into PKG/ subdirectory without affecting"
87 echo "the actual root file system. That is only useful if you do"
88 echo "not want to run the script under the super user."
89 echo
90 echo "The --non-helenos-target will build non-HelenOS-specific toolchain"
91 echo "(i.e. it will use *-linux-* triplet instead of *-helenos)."
92 echo "Using this toolchain for building HelenOS is not supported."
93 echo
94
95 exit 3
96}
97
98test_version() {
99 echo "Cross-compiler toolchain build script"
100 echo
101 echo "Start testing the version of the installed software"
102 echo
103
104 if [ -z "$1" ] || [ "$1" == "all" ] ; then
105 PLATFORMS=("amd64" "arm32" "ia32" "ia64" "mips32" "mips32eb" "ppc32" "riscv64" "sparc64")
106 else
107 PLATFORMS=("$1")
108 fi
109
110
111 if [ -z "${CROSS_PREFIX}" ] ; then
112 CROSS_PREFIX="/usr/local/cross"
113 fi
114
115 for i in "${PLATFORMS[@]}"
116 do
117 PLATFORM="$i"
118 set_target_from_platform "$PLATFORM"
119 PREFIX="${CROSS_PREFIX}/bin/${HELENOS_TARGET}"
120
121 echo "== $PLATFORM =="
122 test_app_version "Binutils" "ld" "GNU\ ld\ \(GNU\ Binutils\)\ ((\.|[0-9])+)" "$BINUTILS_VERSION"
123 test_app_version "GCC" "gcc" "gcc\ version\ ((\.|[0-9])+)" "$GCC_VERSION"
124 test_app_version "GDB" "gdb" "GNU\ gdb\ \(GDB\)\s+((\.|[0-9])+)" "$GDB_VERSION"
125 done
126
127 exit
128}
129
130test_app_version() {
131 PKGNAME="$1"
132 APPNAME="$2"
133 REGEX="$3"
134 INS_VERSION="$4"
135
136
137 APP="${PREFIX}-${APPNAME}"
138 if [ ! -e $APP ]; then
139 echo "- $PKGNAME is missing"
140 else
141 {
142 OUT=$(${APP} -v 2>&1)
143 } &> /dev/null
144
145 if [[ "$OUT" =~ $REGEX ]]; then
146 VERSION="${BASH_REMATCH[1]}"
147 if [ "$INS_VERSION" = "$VERSION" ]; then
148 echo "+ $PKGNAME is uptodate ($INS_VERSION)"
149 else
150 echo "- $PKGNAME ($VERSION) is outdated ($INS_VERSION)"
151 fi
152 else
153 echo "- $PKGNAME Unexpected output"
154 fi
155 fi
156}
157
158
159
160change_title() {
161 echo -en "\e]0;$1\a"
162}
163
164show_countdown() {
165 TM="$1"
166
167 if [ "${TM}" -eq 0 ] ; then
168 echo
169 return 0
170 fi
171
172 echo -n "${TM} "
173 change_title "${TM}"
174 sleep 1
175
176 TM="`expr "${TM}" - 1`"
177 show_countdown "${TM}"
178}
179
180show_dependencies() {
181 echo "IMPORTANT NOTICE:"
182 echo
183 echo "For a successful compilation and use of the cross-compiler"
184 echo "toolchain you need at least the following dependencies."
185 echo
186 echo "Please make sure that the dependencies are present in your"
187 echo "system. Otherwise the compilation process might fail after"
188 echo "a few seconds or minutes."
189 echo
190 echo " - SED, AWK, Flex, Bison, gzip, bzip2, Bourne Shell"
191 echo " - gettext, zlib, Texinfo, libelf, libgomp"
192 echo " - GNU Make, Coreutils, Sharutils, tar"
193 echo " - native C and C++ compiler, assembler and linker"
194 echo " - native C and C++ standard library with headers"
195 echo
196}
197
198cleanup_dir() {
199 DIR="$1"
200
201 if [ -d "${DIR}" ] ; then
202 change_title "Removing ${DIR}"
203 echo " >>> Removing ${DIR}"
204 rm -fr "${DIR}"
205 fi
206}
207
208create_dir() {
209 DIR="$1"
210 DESC="$2"
211
212 change_title "Creating ${DESC}"
213 echo ">>> Creating ${DESC}"
214
215 mkdir -p "${DIR}"
216 test -d "${DIR}"
217 check_error $? "Unable to create ${DIR}."
218}
219
220check_dirs() {
221 OUTSIDE="$1"
222 BASE="$2"
223 ORIGINAL="`pwd`"
224
225 mkdir -p "${OUTSIDE}"
226
227 cd "${OUTSIDE}"
228 check_error $? "Unable to change directory to ${OUTSIDE}."
229 ABS_OUTSIDE="`pwd`"
230
231 cd "${BASE}"
232 check_error $? "Unable to change directory to ${BASE}."
233 ABS_BASE="`pwd`"
234
235 cd "${ORIGINAL}"
236 check_error $? "Unable to change directory to ${ORIGINAL}."
237
238 BASE_LEN="${#ABS_BASE}"
239 OUTSIDE_TRIM="${ABS_OUTSIDE:0:${BASE_LEN}}"
240
241 if [ "${OUTSIDE_TRIM}" == "${ABS_BASE}" ] ; then
242 echo
243 echo "CROSS_PREFIX cannot reside within the working directory."
244
245 exit 5
246 fi
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 "ia32")
279 GNU_ARCH="i686"
280 ;;
281 "mips32")
282 GNU_ARCH="mipsel"
283 ;;
284 "mips32eb")
285 GNU_ARCH="mips"
286 ;;
287 "ppc32")
288 GNU_ARCH="ppc"
289 ;;
290 *)
291 GNU_ARCH="$1"
292 ;;
293 esac
294
295 HELENOS_TARGET="${GNU_ARCH}-helenos"
296
297 case "$1" in
298 "arm32")
299 LINUX_TARGET="${GNU_ARCH}-linux-gnueabi"
300 ;;
301 *)
302 LINUX_TARGET="${GNU_ARCH}-linux-gnu"
303 ;;
304 esac
305}
306
307build_target() {
308 PLATFORM="$1"
309
310 # This sets the *_TARGET variables
311 set_target_from_platform "$PLATFORM"
312 if $USE_HELENOS_TARGET ; then
313 TARGET="$HELENOS_TARGET"
314 else
315 TARGET="$LINUX_TARGET"
316 fi
317
318 WORKDIR="${BASEDIR}/${TARGET}"
319 INSTALL_DIR="${BASEDIR}/PKG"
320 BINUTILSDIR="${WORKDIR}/binutils-${BINUTILS_VERSION}"
321 GCCDIR="${WORKDIR}/gcc-${GCC_VERSION}"
322 GDBDIR="${WORKDIR}/gdb-${GDB_VERSION}"
323
324 if [ -z "${CROSS_PREFIX}" ] ; then
325 CROSS_PREFIX="/usr/local/cross"
326 fi
327
328 if [ -z "$JOBS" ] ; then
329 JOBS=`nproc`
330 fi
331
332 PREFIX="${CROSS_PREFIX}"
333
334 echo ">>> Removing previous content"
335 cleanup_dir "${WORKDIR}"
336 mkdir -p "${WORKDIR}"
337 check_dirs "${PREFIX}" "${WORKDIR}"
338
339 if $USE_HELENOS_TARGET ; then
340 echo ">>> Creating build sysroot"
341 mkdir -p "${WORKDIR}/sysroot/include"
342 mkdir "${WORKDIR}/sysroot/lib"
343 cp -r -L -t "${WORKDIR}/sysroot/include" \
344 ${SRCDIR}/../abi/include/* \
345 ${SRCDIR}/../uspace/lib/c/arch/${PLATFORM}/include/* \
346 ${SRCDIR}/../uspace/lib/c/include/*
347 check_error $? "Failed to create build sysroot."
348 fi
349
350 echo ">>> Processing binutils (${PLATFORM})"
351 mkdir -p "${BINUTILSDIR}"
352 cd "${BINUTILSDIR}"
353 check_error $? "Change directory failed."
354
355 change_title "binutils: configure (${PLATFORM})"
356 CFLAGS=-Wno-error "${BASEDIR}/downloads/binutils-${BINUTILS_VERSION}/configure" \
357 "--target=${TARGET}" \
358 "--prefix=${PREFIX}" \
359 "--program-prefix=${TARGET}-" \
360 --disable-nls \
361 --disable-werror \
362 --enable-gold \
363 --enable-deterministic-archives \
364 --disable-gdb \
365 --with-sysroot
366 check_error $? "Error configuring binutils."
367
368 change_title "binutils: make (${PLATFORM})"
369 make all -j$JOBS
370 check_error $? "Error compiling binutils."
371
372 change_title "binutils: install (${PLATFORM})"
373 make install "DESTDIR=${INSTALL_DIR}"
374 check_error $? "Error installing binutils."
375
376
377 echo ">>> Processing GCC (${PLATFORM})"
378 mkdir -p "${GCCDIR}"
379 cd "${GCCDIR}"
380 check_error $? "Change directory failed."
381
382 if $USE_HELENOS_TARGET ; then
383 SYSROOT=--with-sysroot --with-build-sysroot="${WORKDIR}/sysroot"
384 else
385 SYSROOT=--without-headers
386 fi
387
388 change_title "GCC: configure (${PLATFORM})"
389 PATH="$PATH:${INSTALL_DIR}/${PREFIX}/bin" "${BASEDIR}/downloads/gcc-${GCC_VERSION}/configure" \
390 "--target=${TARGET}" \
391 "--prefix=${PREFIX}" \
392 "--program-prefix=${TARGET}-" \
393 --with-gnu-as \
394 --with-gnu-ld \
395 --disable-nls \
396 --enable-languages=c,c++,go \
397 --enable-lto \
398 --disable-shared \
399 --disable-werror \
400 $SYSROOT
401 check_error $? "Error configuring GCC."
402
403 change_title "GCC: make (${PLATFORM})"
404 PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-gcc -j$JOBS
405 check_error $? "Error compiling GCC."
406
407 if $USE_HELENOS_TARGET ; then
408 PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libgcc -j$JOBS
409 check_error $? "Error compiling libgcc."
410 # TODO: needs some extra care
411 #PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libatomic -j$JOBS
412 #check_error $? "Error compiling libatomic."
413 #PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libstdc++-v3 -j$JOBS
414 #check_error $? "Error compiling libstdc++."
415 fi
416
417 change_title "GCC: install (${PLATFORM})"
418 PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-gcc "DESTDIR=${INSTALL_DIR}"
419 if $USE_HELENOS_TARGET ; then
420 PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libgcc "DESTDIR=${INSTALL_DIR}"
421 #PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libatomic "DESTDIR=${INSTALL_DIR}"
422 #PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libstdc++-v3 "DESTDIR=${INSTALL_DIR}"
423 fi
424 check_error $? "Error installing GCC."
425
426
427 echo ">>> Processing GDB (${PLATFORM})"
428 mkdir -p "${GDBDIR}"
429 cd "${GDBDIR}"
430 check_error $? "Change directory failed."
431
432 change_title "GDB: configure (${PLATFORM})"
433 PATH="$PATH:${INSTALL_DIR}/${PREFIX}/bin" "${BASEDIR}/downloads/gdb-${GDB_VERSION}/configure" \
434 "--target=${TARGET}" \
435 "--prefix=${PREFIX}" \
436 "--program-prefix=${TARGET}-" \
437 --enable-werror=no
438 check_error $? "Error configuring GDB."
439
440 change_title "GDB: make (${PLATFORM})"
441 PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-gdb -j$JOBS
442 check_error $? "Error compiling GDB."
443
444 change_title "GDB: make (${PLATFORM})"
445 PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-gdb "DESTDIR=${INSTALL_DIR}"
446 check_error $? "Error installing GDB."
447
448 # Symlink clang and lld to the install path.
449 CLANG="`which clang 2> /dev/null || echo "/usr/bin/clang"`"
450 LLD="`which ld.lld 2> /dev/null || echo "/usr/bin/ld.lld"`"
451
452 ln -s $CLANG "${INSTALL_DIR}/${PREFIX}/bin/${TARGET}-clang"
453 ln -s $LLD "${INSTALL_DIR}/${PREFIX}/bin/${TARGET}-ld.lld"
454
455 if $REAL_INSTALL ; then
456 echo ">>> Moving to the destination directory."
457 echo cp -r -t "${PREFIX}" "${INSTALL_DIR}/${PREFIX}/"*
458 cp -r -t "${PREFIX}" "${INSTALL_DIR}/${PREFIX}/"*
459 fi
460
461 cd "${BASEDIR}"
462 check_error $? "Change directory failed."
463
464 echo ">>> Cleaning up"
465 cleanup_dir "${WORKDIR}"
466
467 echo
468 echo ">>> Cross-compiler for ${TARGET} installed."
469}
470
471while [ "$#" -gt 1 ] ; do
472 case "$1" in
473 --test-version)
474 test_version "$2"
475 exit
476 ;;
477 --no-install)
478 REAL_INSTALL=false
479 shift
480 ;;
481 --non-helenos-target)
482 USE_HELENOS_TARGET=false
483 shift
484 ;;
485 *)
486 show_usage
487 ;;
488 esac
489done
490
491if [ "$#" -lt "1" ] ; then
492 show_usage
493fi
494
495case "$1" in
496 --test-version)
497 test_version
498 ;;
499 amd64|arm32|ia32|ia64|mips32|mips32eb|ppc32|riscv64|sparc64)
500 prepare
501 build_target "$1"
502 ;;
503 "all")
504 prepare
505 build_target "amd64"
506 build_target "arm32"
507 build_target "ia32"
508 build_target "ia64"
509 build_target "mips32"
510 build_target "mips32eb"
511 build_target "ppc32"
512 build_target "riscv64"
513 build_target "sparc64"
514 ;;
515 "essential")
516 prepare
517 build_target "amd64"
518 build_target "arm32"
519 build_target "ia32"
520 build_target "ia64"
521 build_target "mips32"
522 build_target "mips32eb"
523 build_target "ppc32"
524 build_target "sparc64"
525 ;;
526 "parallel")
527 prepare
528 build_target "amd64" &
529 build_target "arm32" &
530 build_target "ia32" &
531 build_target "ia64" &
532 build_target "mips32" &
533 build_target "mips32eb" &
534 build_target "ppc32" &
535 build_target "riscv64" &
536 build_target "sparc64" &
537 wait
538 ;;
539 "2-way")
540 prepare
541 build_target "amd64" &
542 build_target "arm32" &
543 wait
544
545 build_target "ia32" &
546 build_target "ia64" &
547 wait
548
549 build_target "mips32" &
550 build_target "mips32eb" &
551 wait
552
553 build_target "ppc32" &
554 build_target "riscv64" &
555 wait
556
557 build_target "sparc64" &
558 wait
559 ;;
560 *)
561 show_usage
562 ;;
563esac
Note: See TracBrowser for help on using the repository browser.