Enable/disable the virtq when it's set/torn down
[helenos.git] / tools / toolchain.sh
blobb170a098dfda0493ca0d7d2cbb09eb261ec36011
1 #! /bin/bash
4 # Copyright (c) 2009 Martin Decky
5 # All rights reserved.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
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.
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.
31 BINUTILS_GDB_GIT="https://github.com/HelenOS/binutils-gdb.git"
33 BINUTILS_BRANCH="binutils-2_30-helenos"
34 BINUTILS_VERSION="2.30"
36 GDB_BRANCH="gdb-8_1-helenos"
37 GDB_VERSION="8.1"
39 GCC_GIT="https://github.com/HelenOS/gcc.git"
40 GCC_BRANCH="7_3_0-helenos"
41 GCC_VERSION="7.3.0"
43 BASEDIR="`pwd`"
44 SRCDIR="$(readlink -f $(dirname "$0"))"
46 REAL_INSTALL=true
47 USE_HELENOS_TARGET=false
49 check_error() {
50 if [ "$1" -ne "0" ] ; then
51 echo
52 echo "Script failed: $2"
54 exit 1
58 show_usage() {
59 echo "Cross-compiler toolchain build script"
60 echo
61 echo "Syntax:"
62 echo " $0 [--no-install] [--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 " mips64 MIPS little-endian 64b"
72 echo " ppc32 PowerPC 32b"
73 echo " ppc64 PowerPC 64v"
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 --helenos-target will build HelenOS-specific toolchain"
92 echo "(i.e. it will use *-helenos triplet instead of *-linux-*)."
93 echo "Using the HelenOS-specific toolchain is still an experimental"
94 echo "feature that is not fully supported."
95 echo
97 exit 3
100 change_title() {
101 echo -en "\e]0;$1\a"
104 show_countdown() {
105 TM="$1"
107 if [ "${TM}" -eq 0 ] ; then
108 echo
109 return 0
112 echo -n "${TM} "
113 change_title "${TM}"
114 sleep 1
116 TM="`expr "${TM}" - 1`"
117 show_countdown "${TM}"
120 show_dependencies() {
121 echo "IMPORTANT NOTICE:"
122 echo
123 echo "For a successful compilation and use of the cross-compiler"
124 echo "toolchain you need at least the following dependencies."
125 echo
126 echo "Please make sure that the dependencies are present in your"
127 echo "system. Otherwise the compilation process might fail after"
128 echo "a few seconds or minutes."
129 echo
130 echo " - SED, AWK, Flex, Bison, gzip, bzip2, Bourne Shell"
131 echo " - gettext, zlib, Texinfo, libelf, libgomp"
132 echo " - GNU Make, Coreutils, Sharutils, tar"
133 echo " - native C and C++ compiler, assembler and linker"
134 echo " - native C and C++ standard library with headers"
135 echo
138 cleanup_dir() {
139 DIR="$1"
141 if [ -d "${DIR}" ] ; then
142 change_title "Removing ${DIR}"
143 echo " >>> Removing ${DIR}"
144 rm -fr "${DIR}"
148 create_dir() {
149 DIR="$1"
150 DESC="$2"
152 change_title "Creating ${DESC}"
153 echo ">>> Creating ${DESC}"
155 mkdir -p "${DIR}"
156 test -d "${DIR}"
157 check_error $? "Unable to create ${DIR}."
160 check_dirs() {
161 OUTSIDE="$1"
162 BASE="$2"
163 ORIGINAL="`pwd`"
165 mkdir -p "${OUTSIDE}"
167 cd "${OUTSIDE}"
168 check_error $? "Unable to change directory to ${OUTSIDE}."
169 ABS_OUTSIDE="`pwd`"
171 cd "${BASE}"
172 check_error $? "Unable to change directory to ${BASE}."
173 ABS_BASE="`pwd`"
175 cd "${ORIGINAL}"
176 check_error $? "Unable to change directory to ${ORIGINAL}."
178 BASE_LEN="${#ABS_BASE}"
179 OUTSIDE_TRIM="${ABS_OUTSIDE:0:${BASE_LEN}}"
181 if [ "${OUTSIDE_TRIM}" == "${ABS_BASE}" ] ; then
182 echo
183 echo "CROSS_PREFIX cannot reside within the working directory."
185 exit 5
189 prepare() {
190 show_dependencies
191 show_countdown 10
193 mkdir -p "${BASEDIR}/downloads"
194 cd "${BASEDIR}/downloads"
195 check_error $? "Change directory failed."
197 echo ">>> Downloading sources"
198 git clone --depth 1 -b "$BINUTILS_BRANCH" "$BINUTILS_GDB_GIT" "binutils-$BINUTILS_VERSION"
199 git clone --depth 1 -b "$GDB_BRANCH" "$BINUTILS_GDB_GIT" "gdb-$GDB_VERSION"
200 git clone --depth 1 -b "$GCC_BRANCH" "$GCC_GIT" "gcc-$GCC_VERSION"
202 # If the directory already existed, pull upstream changes.
203 git -C "binutils-$BINUTILS_VERSION" pull
204 git -C "gdb-$GDB_VERSION" pull
205 git -C "gcc-$GCC_VERSION" pull
207 echo ">>> Downloading GCC prerequisites"
208 cd "gcc-${GCC_VERSION}"
209 ./contrib/download_prerequisites
210 cd ..
213 set_target_from_platform() {
214 case "$1" in
215 "arm32")
216 GNU_ARCH="arm"
218 "ia32")
219 GNU_ARCH="i686"
221 "mips32")
222 GNU_ARCH="mipsel"
224 "mips32eb")
225 GNU_ARCH="mips"
227 "mips64")
228 GNU_ARCH="mips64el"
230 "ppc32")
231 GNU_ARCH="ppc"
234 GNU_ARCH="$1"
236 esac
238 HELENOS_TARGET="${GNU_ARCH}-helenos"
240 # TODO: Clean up this mess.
241 case "$1" in
242 "amd64")
243 LINUX_TARGET="${GNU_ARCH}-unknown-elf"
245 "ia32" | "ia64")
246 LINUX_TARGET="${GNU_ARCH}-pc-linux-gnu"
248 "arm32")
249 LINUX_TARGET="${GNU_ARCH}-linux-gnueabi"
251 "riscv64")
252 LINUX_TARGET="${GNU_ARCH}-unknown-linux-gnu"
255 LINUX_TARGET="${GNU_ARCH}-linux-gnu"
257 esac
260 build_target() {
261 PLATFORM="$1"
263 # This sets the *_TARGET variables
264 set_target_from_platform "$PLATFORM"
265 if $USE_HELENOS_TARGET ; then
266 TARGET="$HELENOS_TARGET"
267 else
268 TARGET="$LINUX_TARGET"
271 WORKDIR="${BASEDIR}/${TARGET}"
272 INSTALL_DIR="${WORKDIR}/PKG"
273 BINUTILSDIR="${WORKDIR}/binutils-${BINUTILS_VERSION}"
274 GCCDIR="${WORKDIR}/gcc-${GCC_VERSION}"
275 GDBDIR="${WORKDIR}/gdb-${GDB_VERSION}"
277 if [ -z "${CROSS_PREFIX}" ] ; then
278 CROSS_PREFIX="/usr/local/cross"
281 if [ -z "$JOBS" ] ; then
282 JOBS=`nproc`
285 PREFIX="${CROSS_PREFIX}/${TARGET}"
287 echo ">>> Removing previous content"
288 cleanup_dir "${WORKDIR}"
289 mkdir -p "${WORKDIR}"
290 check_dirs "${PREFIX}" "${WORKDIR}"
292 if $USE_HELENOS_TARGET ; then
293 echo ">>> Creating build sysroot"
294 mkdir -p "${WORKDIR}/sysroot/include"
295 mkdir "${WORKDIR}/sysroot/lib"
296 cp -r -L -t "${WORKDIR}/sysroot/include" \
297 ${SRCDIR}/../abi/include/* \
298 ${SRCDIR}/../uspace/lib/c/arch/${PLATFORM}/include/* \
299 ${SRCDIR}/../uspace/lib/c/include/*
300 check_error $? "Failed to create build sysroot."
303 echo ">>> Processing binutils (${PLATFORM})"
304 mkdir -p "${BINUTILSDIR}"
305 cd "${BINUTILSDIR}"
306 check_error $? "Change directory failed."
308 change_title "binutils: configure (${PLATFORM})"
309 CFLAGS=-Wno-error "${BASEDIR}/downloads/binutils-${BINUTILS_VERSION}/configure" \
310 "--target=${TARGET}" \
311 "--prefix=${PREFIX}" \
312 "--program-prefix=${TARGET}-" \
313 --disable-nls \
314 --disable-werror \
315 --enable-gold \
316 --enable-deterministic-archives \
317 --disable-gdb \
318 --with-sysroot
319 check_error $? "Error configuring binutils."
321 change_title "binutils: make (${PLATFORM})"
322 make all -j$JOBS
323 check_error $? "Error compiling binutils."
325 change_title "binutils: install (${PLATFORM})"
326 make install "DESTDIR=${INSTALL_DIR}"
327 check_error $? "Error installing binutils."
330 echo ">>> Processing GCC (${PLATFORM})"
331 mkdir -p "${GCCDIR}"
332 cd "${GCCDIR}"
333 check_error $? "Change directory failed."
335 if $USE_HELENOS_TARGET ; then
336 SYSROOT=--with-sysroot --with-build-sysroot="${WORKDIR}/sysroot"
337 else
338 SYSROOT=--without-headers
341 change_title "GCC: configure (${PLATFORM})"
342 PATH="$PATH:${INSTALL_DIR}/${PREFIX}/bin" "${BASEDIR}/downloads/gcc-${GCC_VERSION}/configure" \
343 "--target=${TARGET}" \
344 "--prefix=${PREFIX}" \
345 "--program-prefix=${TARGET}-" \
346 --with-gnu-as \
347 --with-gnu-ld \
348 --disable-nls \
349 --enable-languages=c,c++,go \
350 --enable-lto \
351 --disable-shared \
352 --disable-werror \
353 $SYSROOT
354 check_error $? "Error configuring GCC."
356 change_title "GCC: make (${PLATFORM})"
357 PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-gcc -j$JOBS
358 check_error $? "Error compiling GCC."
360 if $USE_HELENOS_TARGET ; then
361 PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libgcc -j$JOBS
362 check_error $? "Error compiling libgcc."
363 # TODO: needs some extra care
364 #PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libatomic -j$JOBS
365 #check_error $? "Error compiling libatomic."
366 #PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-target-libstdc++-v3 -j$JOBS
367 #check_error $? "Error compiling libstdc++."
370 change_title "GCC: install (${PLATFORM})"
371 PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-gcc "DESTDIR=${INSTALL_DIR}"
372 if $USE_HELENOS_TARGET ; then
373 PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libgcc "DESTDIR=${INSTALL_DIR}"
374 #PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libatomic "DESTDIR=${INSTALL_DIR}"
375 #PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-target-libstdc++-v3 "DESTDIR=${INSTALL_DIR}"
377 check_error $? "Error installing GCC."
380 echo ">>> Processing GDB (${PLATFORM})"
381 mkdir -p "${GDBDIR}"
382 cd "${GDBDIR}"
383 check_error $? "Change directory failed."
385 change_title "GDB: configure (${PLATFORM})"
386 PATH="$PATH:${INSTALL_DIR}/${PREFIX}/bin" "${BASEDIR}/downloads/gdb-${GDB_VERSION}/configure" \
387 "--target=${TARGET}" \
388 "--prefix=${PREFIX}" \
389 "--program-prefix=${TARGET}-" \
390 --enable-werror=no
391 check_error $? "Error configuring GDB."
393 change_title "GDB: make (${PLATFORM})"
394 PATH="${PATH}:${PREFIX}/bin:${INSTALL_DIR}/${PREFIX}/bin" make all-gdb -j$JOBS
395 check_error $? "Error compiling GDB."
397 change_title "GDB: make (${PLATFORM})"
398 PATH="${PATH}:${INSTALL_DIR}/${PREFIX}/bin" make install-gdb "DESTDIR=${INSTALL_DIR}"
399 check_error $? "Error installing GDB."
401 # Symlink clang and lld to the install path.
402 CLANG="`which clang 2> /dev/null || echo "/usr/bin/clang"`"
403 LLD="`which ld.lld 2> /dev/null || echo "/usr/bin/ld.lld"`"
405 ln -s $CLANG "${INSTALL_DIR}/${PREFIX}/bin/${TARGET}-clang"
406 ln -s $LLD "${INSTALL_DIR}/${PREFIX}/bin/${TARGET}-ld.lld"
408 if $REAL_INSTALL ; then
409 echo ">>> Moving to the destination directory."
410 cleanup_dir "${PREFIX}"
411 echo mv "${INSTALL_DIR}/${PREFIX}" "${PREFIX}"
412 mv "${INSTALL_DIR}/${PREFIX}" "${PREFIX}"
415 cd "${BASEDIR}"
416 check_error $? "Change directory failed."
418 echo ">>> Cleaning up"
419 cleanup_dir "${WORKDIR}"
421 echo
422 echo ">>> Cross-compiler for ${TARGET} installed."
425 while [ "$#" -gt 1 ] ; do
426 case "$1" in
427 --no-install)
428 REAL_INSTALL=false
429 shift
431 --helenos-target)
432 USE_HELENOS_TARGET=true
433 shift
436 show_usage
438 esac
439 done
441 if [ "$#" -lt "1" ] ; then
442 show_usage
445 case "$1" in
446 amd64|arm32|ia32|ia64|mips32|mips32eb|mips64|ppc32|ppc64|riscv64|sparc64)
447 prepare
448 build_target "$1"
450 "all")
451 prepare
452 build_target "amd64"
453 build_target "arm32"
454 build_target "ia32"
455 build_target "ia64"
456 build_target "mips32"
457 build_target "mips32eb"
458 build_target "mips64"
459 build_target "ppc32"
460 build_target "ppc64"
461 build_target "riscv64"
462 build_target "sparc64"
464 "essential")
465 prepare
466 build_target "amd64"
467 build_target "arm32"
468 build_target "ia32"
469 build_target "ia64"
470 build_target "mips32"
471 build_target "mips32eb"
472 build_target "ppc32"
473 build_target "sparc64"
475 "parallel")
476 prepare
477 build_target "amd64" &
478 build_target "arm32" &
479 build_target "ia32" &
480 build_target "ia64" &
481 build_target "mips32" &
482 build_target "mips32eb" &
483 build_target "mips64" &
484 build_target "ppc32" &
485 build_target "ppc64" &
486 build_target "riscv64" &
487 build_target "sparc64" &
488 wait
490 "2-way")
491 prepare
492 build_target "amd64" &
493 build_target "arm32" &
494 wait
496 build_target "ia32" &
497 build_target "ia64" &
498 wait
500 build_target "mips32" &
501 build_target "mips32eb" &
502 wait
504 build_target "mips64" &
505 build_target "ppc32" &
506 wait
508 build_target "riscv64" &
509 build_target "ppc64" &
510 wait
512 build_target "sparc64" &
513 wait
516 show_usage
518 esac