source: mainline/tools/toolchain.sh@ 4aba581

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

implementing a test function for the different platforms

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