Debian Codegraph @

util/crossgcc/buildgcc

raw symbols SCIP
1 #!/usr/bin/env sh
2 # shellcheck disable=SC2030,SC2031,SC2059
3 # The above line must be directly after the shebang line.
4 # Disables these warnings:
5 # 2030 - Modification of var is local (to subshell caused by pipeline).
6 # shell check 0.4.6 gets confused by the read -t 1 command and interprets
7 # the '1' as $1 getting modified.
8 # 2031 - var was modified in a subshell. That change might be lost.
9 # caused by shell check bug with SC2030? This causes any $1 from that
10 # point on to be flagged.
11 # 2059 - Don't use variables in the printf format string. Use printf "..%s.." "$foo".
12 # This is used for all of our color printing.
13
14 #
15 # SPDX-License-Identifier: GPL-2.0-only
16
17 cd "$(dirname "$0")" || exit 1
18
19 if [ -z "$CROSSGCC_VERSION" ]; then
20 CROSSGCC_VERSION="$(git log -n 1 --pretty=%cd --date=short .)_$(git log -n 1 --pretty=%h .)"
21 fi
22
23 # default settings
24 PACKAGE=GCC
25 TARGETDIR=$(pwd)/xgcc
26 TARGETARCH=i386-elf
27 DEFAULT_LANGUAGES=c
28 LANGUAGES=
29 DESTDIR=
30 SAVETEMPS=0
31 BOOTSTRAP=0
32 THREADS=1
33 FETCH_ONLY=0
34 USE_COREBOOT_MIRROR=0
35 COREBOOT_MIRROR_URL="https://www.coreboot.org/releases/crossgcc-sources"
36
37 # GCC Toolchain (built first)
38 # Math libraries (GCC dependencies)
39 GMP_VERSION=6.3.0
40 MPFR_VERSION=4.2.2
41 MPC_VERSION=1.3.1
42 # Core GCC components
43 GCC_VERSION=14.2.0
44 BINUTILS_VERSION=2.44
45 LIBSTDCXX_VERSION="${GCC_VERSION}" # GCC-specific C++ library
46
47 # Low-level Tools (built after GCC)
48 NASM_VERSION=2.16.03 # x86 assembly
49 IASL_VERSION=20250404 # ACPI compiler
50
51 # Clang/LLVM Toolchain (alternative to GCC, built separately)
52 CLANG_VERSION=18.1.8
53 CMAKE_VERSION=4.0.3 # Required for building Clang
54
55 # Architecture-specific options
56 RISCV_ISA_SPEC=20191213
57
58 # Filename for each package
59 GMP_ARCHIVE="gmp-${GMP_VERSION}.tar.xz"
60 MPFR_ARCHIVE="mpfr-${MPFR_VERSION}.tar.xz"
61 MPC_ARCHIVE="mpc-${MPC_VERSION}.tar.gz"
62 GCC_ARCHIVE="gcc-${GCC_VERSION}.tar.xz"
63 LIBSTDCXX_ARCHIVE="${GCC_ARCHIVE}"
64 BINUTILS_ARCHIVE="binutils-${BINUTILS_VERSION}.tar.xz"
65 IASL_ARCHIVE="acpica-unix-${IASL_VERSION}.tar.gz"
66 # CLANG toolchain FILE locations
67 LLD_ARCHIVE="lld-${CLANG_VERSION}.src.tar.xz"
68 LLVM_ARCHIVE="llvm-${CLANG_VERSION}.src.tar.xz"
69 CLANG_ARCHIVE="clang-${CLANG_VERSION}.src.tar.xz"
70 CRT_ARCHIVE="compiler-rt-${CLANG_VERSION}.src.tar.xz"
71 CTE_ARCHIVE="clang-tools-extra-${CLANG_VERSION}.src.tar.xz"
72 LLVMCMAKE_ARCHIVE="cmake-${CLANG_VERSION}.src.tar.xz"
73 LIBUNWIND_ARCHIVE="libunwind-${CLANG_VERSION}.src.tar.xz"
74 RUNTIMES_ARCHIVE="runtimes-${CLANG_VERSION}.src.tar.xz"
75 CMAKE_ARCHIVE="cmake-${CMAKE_VERSION}.tar.gz"
76 NASM_ARCHIVE="nasm-${NASM_VERSION}.tar.bz2"
77
78 # These URLs are sanitized by the jenkins toolchain test builder, so if
79 # a completely new URL is added here, it probably needs to be added
80 # to the jenkins build as well, or the builder won't download it.
81
82 # GCC toolchain archive locations
83 GMP_BASE_URL="https://ftpmirror.gnu.org/gmp"
84 MPFR_BASE_URL="https://ftpmirror.gnu.org/mpfr"
85 MPC_BASE_URL="https://ftpmirror.gnu.org/mpc"
86 GCC_BASE_URL="https://ftpmirror.gnu.org/gcc/gcc-${GCC_VERSION}"
87 LIBSTDCXX_BASE_URL="${GCC_BASE_URL}"
88 BINUTILS_BASE_URL="https://ftpmirror.gnu.org/binutils"
89 IASL_BASE_URL="https://downloadmirror.intel.com/852044"
90 # CLANG toolchain archive locations
91 LLVM_BASE_URL="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}"
92 CLANG_BASE_URL="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}"
93 CRT_BASE_URL="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}"
94 CTE_BASE_URL="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}"
95 LLVMCMAKE_BASE_URL="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}"
96 LLD_BASE_URL="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}"
97 LIBUNWIND_BASE_URL="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}"
98 RUNTIMES_BASE_URL="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}"
99 CMAKE_BASE_URL="https://cmake.org/files/v${CMAKE_VERSION%.*}"
100 NASM_BASE_URL="https://www.nasm.us/pub/nasm/releasebuilds/${NASM_VERSION}"
101
102 ALL_ARCHIVES="$GMP_BASE_URL/$GMP_ARCHIVE $MPFR_BASE_URL/$MPFR_ARCHIVE $MPC_BASE_URL/$MPC_ARCHIVE \
103 $GCC_BASE_URL/$GCC_ARCHIVE $BINUTILS_BASE_URL/$BINUTILS_ARCHIVE $IASL_BASE_URL/$IASL_ARCHIVE \
104 $LLD_BASE_URL/$LLD_ARCHIVE $LLVM_BASE_URL/$LLVM_ARCHIVE $CLANG_BASE_URL/$CLANG_ARCHIVE \
105 $LLVMCMAKE_BASE_URL/$LLVMCMAKE_ARCHIVE $CRT_BASE_URL/$CRT_ARCHIVE $CTE_BASE_URL/$CTE_ARCHIVE \
106 $LIBUNWIND_BASE_URL/$LIBUNWIND_ARCHIVE $RUNTIMES_BASE_URL/$RUNTIMES_ARCHIVE \
107 $CMAKE_BASE_URL/$CMAKE_ARCHIVE $NASM_BASE_URL/$NASM_ARCHIVE $LIBSTDCXX_BASE_URL/$LIBSTDCXX_ARCHIVE"
108
109 # GCC toolchain directories
110 GMP_DIR="gmp-${GMP_VERSION}"
111 MPFR_DIR="mpfr-${MPFR_VERSION}"
112 MPC_DIR="mpc-${MPC_VERSION}"
113 # shellcheck disable=SC2034
114 GCC_DIR="gcc-${GCC_VERSION}"
115 LIBSTDCXX_DIR="${GCC_DIR}"
116 # shellcheck disable=SC2034
117 BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
118 IASL_DIR="acpica-unix-${IASL_VERSION}"
119 # CLANG toolchain directories
120 LLD_DIR="lld-${CLANG_VERSION}.src"
121 LLVM_DIR="llvm-${CLANG_VERSION}.src"
122 CLANG_DIR="clang-${CLANG_VERSION}.src"
123 CRT_DIR="compiler-rt-${CLANG_VERSION}.src"
124 CTE_DIR="clang-tools-extra-${CLANG_VERSION}.src"
125 LLVMCMAKE_DIR="cmake-${CLANG_VERSION}.src"
126 LIBUNWIND_DIR="libunwind-${CLANG_VERSION}.src"
127 RUNTIMES_DIR="runtimes-${CLANG_VERSION}.src"
128 CMAKE_DIR="cmake-${CMAKE_VERSION}"
129 NASM_DIR="nasm-${NASM_VERSION}"
130
131 unset MAKELEVEL MAKEFLAGS
132
133 red='\033[0;31m'
134 RED='\033[1;31m'
135 green='\033[0;32m'
136 GREEN='\033[1;32m'
137 blue='\033[0;34m'
138 CYAN='\033[1;36m'
139 NC='\033[0m' # No Color
140
141 UNAME=$(if uname | grep -iq cygwin; then echo Cygwin; else uname; fi)
142 HALT_FOR_TOOLS=0
143
144 hostcc()
145 {
146 # $1 "host" or "target"
147 if [ "$BOOTSTRAP" = 1 ] && [ "$1" = target ]; then
148 echo "$DESTDIR$TARGETDIR/bin/gcc"
149 else
150 echo "$CC"
151 fi
152 }
153
154 hostcxx()
155 {
156 # $1 "host" or "target"
157 if [ "$BOOTSTRAP" = 1 ] && [ "$1" = target ]; then
158 echo "$DESTDIR$TARGETDIR/bin/g++"
159 else
160 echo "$CXX"
161 fi
162 }
163
164 normalize_dirs()
165 {
166 mkdir -p "$DESTDIR$TARGETDIR/lib"
167 test -d "$DESTDIR$TARGETDIR/lib32" && mv "$DESTDIR$TARGETDIR"/lib32/* "$DESTDIR$TARGETDIR/lib"
168 test -d "$DESTDIR$TARGETDIR/lib64" && mv "$DESTDIR$TARGETDIR"/lib64/* "$DESTDIR$TARGETDIR/lib"
169 rm -rf "$DESTDIR$TARGETDIR/lib32" "$DESTDIR$TARGETDIR/lib64"
170
171 perl -pi -e "s,/lib32,/lib," "$DESTDIR$TARGETDIR"/lib/*.la
172 perl -pi -e "s,/lib64,/lib," "$DESTDIR$TARGETDIR"/lib/*.la
173 }
174
175 countdown()
176 {
177 tout=${1:-10}
178
179 printf "\nPress Ctrl-C to abort, Enter to continue... %2ds" "$tout"
180 while [ "$tout" -gt 0 ]; do
181 sleep 1
182 tout=$((tout - 1))
183 printf "\b\b\b%2ds" $tout
184 done
185 printf "\n"
186 }
187
188 timeout()
189 {
190 tout=${1:-10}
191
192 # Ignore SIGUSR1, should interrupt `read` though.
193 trap false USR1
194 # Clean up in case the user aborts.
195 trap 'kill $counter > /dev/null 2>&1' EXIT
196
197 (countdown "$tout"; kill -USR1 $$)&
198 counter=$!
199
200 # Some shells with sh compatibility mode (e.g. zsh, mksh) only
201 # let us interrupt `read` if a non-standard -t parameter is given.
202 # shellcheck disable=SC2034,SC2039,SC2162
203 if echo | read -t 1 foo 2>/dev/null; then
204 read -t $((tout + 1)) foo
205 else
206 read foo
207 fi
208
209 kill $counter > /dev/null 2>&1
210 trap - USR1 EXIT
211 }
212
213 please_install()
214 {
215 HALT_FOR_TOOLS=1
216 # shellcheck disable=SC1091
217 test -r /etc/os-release && . /etc/os-release
218 # vanilla debian doesn't define `ID_LIKE`, just `ID`
219 if [ -z "${ID_LIKE}" ] && [ -n "${ID}" ]; then
220 ID_LIKE=${ID}
221 fi
222 case "$ID_LIKE" in
223 debian) solution="sudo apt-get install $1" ;;
224 suse) solution="sudo zypper install $1" ;;
225 *) solution="using your OS packaging system" ;;
226 esac
227
228 printf "${RED}ERROR:${red} Missing tool: Please install '$1'. (eg $solution)${NC}\n" >&2
229 if [ -n "$2" ]; then
230 printf "${RED}ERROR:${red} or install '$2'.${NC}\n" >&2
231 fi
232 }
233
234 searchtool()
235 {
236 # $1 short name
237 # $2 search string
238 # $3 soft fail if set
239 # $4 alternative package to install on failure
240 # result: file name of that tool on stdout
241 # or no output if nothing suitable was found
242 search=GNU
243 if [ -n "$2" ]; then
244 search="$2"
245 fi
246 for i in "$1" "g$1" "gnu$1"; do
247 if [ -x "$(command -v "$i" 2>/dev/null)" ]; then
248 if [ "$(cat /dev/null | $i --version 2>&1 | grep -c "$search")" \
249 -gt 0 ]; then
250 echo "$i"
251 return
252 fi
253 fi
254 done
255 # A workaround for OSX 10.9 and some BSDs, whose nongnu
256 # patch and tar also work.
257 if [ "$UNAME" = "Darwin" ] || [ "$UNAME" = "FreeBSD" ] || [ "$UNAME" = "NetBSD" ] || [ "$UNAME" = "OpenBSD" ]; then
258 if [ "$1" = "patch" ] || [ "$1" = "tar" ]; then
259 if [ -x "$(command -v "$1" 2>/dev/null)" ]; then
260 echo "$1"
261 return
262 fi
263 fi
264 fi
265 if echo "$1" | grep -q "sum" ; then
266 algor=$(echo "$1" | sed -e 's,sum,,')
267 if [ -x "$(command -v "$1" 2>/dev/null)" ]; then
268 #xxxsum [file]
269 echo "$1"
270 return
271 elif [ -x "$(command -v "$algor" 2>/dev/null)" ]; then
272 #xxx [file]
273 echo "$algor"
274 return
275 elif [ -x "$(command -v openssl 2>/dev/null)" ]; then
276 #openssl xxx [file]
277 echo openssl "$algor"
278 return
279 elif [ -x "$(command -v cksum 2>/dev/null)" ]; then
280 #cksum -a xxx [file]
281 #cksum has special options in NetBSD. Actually, NetBSD will use the second case above.
282 echo "buildgcc" | cksum -a "$algor" > /dev/null 2>/dev/null && \
283 echo cksum -a "$algor"
284 return
285 fi
286 fi
287
288 [ -z "$3" ] && please_install "$1" "$4"
289 false
290 }
291
292 # Run a compile check of the specified library option to see if it's installed
293 check_for_library() {
294 LIBRARY_FLAGS="$1"
295 LIBRARY_PACKAGES="$2"
296 LIBTEST_FILE=.libtest
297
298 echo "int main(int argc, char **argv) { (void) argc; (void) argv; return 0; }" > "${LIBTEST_FILE}.c"
299
300 # shellcheck disable=SC2086
301 "$CC" $CFLAGS $LIBRARY_FLAGS "${LIBTEST_FILE}.c" -o "${LIBTEST_FILE}" >/dev/null 2>&1 || \
302 please_install "$LIBRARY_PACKAGES"
303 rm -rf "${LIBTEST_FILE}.c" "${LIBTEST_FILE}"
304 }
305
306 buildcc_major() {
307 echo "${GCC_VERSION}" | cut -d. -f1
308 }
309
310 buildcc_minor() {
311 echo "${GCC_VERSION}" | cut -d. -f2
312 }
313
314 buildcc_version() {
315 echo "${GCC_VERSION}" | cut -d. -f1-2
316 }
317
318 hostcc_major() {
319 (echo __GNUC__ | ${CC} -E - 2>/dev/null || echo 0) | tail -1
320 }
321
322 hostcc_minor() {
323 (echo __GNUC_MINOR__ | ${CC} -E - 2>/dev/null || echo 0) | tail -1
324 }
325
326 hostcc_version() {
327 printf "%d.%d" "$(hostcc_major)" "$(hostcc_minor)"
328 }
329
330 hostcc_has_gnat1() {
331 [ -x "$(${CC} -print-prog-name=gnat1)" ]
332 }
333
334 have_gnat() {
335 hostcc_has_gnat1 && \
336 searchtool gnatbind "Free Software Foundation" nofail > /dev/null
337 }
338
339 ada_requested() {
340 echo "${LANGUAGES}" | grep -q '\<ada\>'
341 }
342
343 download() {
344 package=$1
345 # shellcheck disable=SC2086
346 if [ "${USE_COREBOOT_MIRROR}" -eq 0 ]; then
347 url="$(eval echo \$$package"_BASE_URL")"
348 else
349 url="${COREBOOT_MIRROR_URL}"
350 fi
351
352 file="$(eval echo \$$package"_ARCHIVE")"
353 printf " * ${file} "
354
355 if test -f "tarballs/${file}"; then
356 printf "(cached)... "
357 else
358 printf "(downloading from ${url}/${file})"
359 rm -f "tarballs/${file}"
360 cd tarballs || exit 1
361 download_showing_percentage "${url}/${file}"
362 cd ..
363 fi
364
365 if [ ! -f "tarballs/${file}" ]; then
366 printf "${RED}Failed to download ${file}.${NC}\n"
367 exit 1
368 fi
369 }
370
371 # Compute the hash of the package given in $1, and print it raw (just the
372 # hexadecimal hash).
373 compute_hash() {
374 package=$1
375 # shellcheck disable=SC2086
376 file="$(eval echo \$$package"_ARCHIVE")"
377
378 if test -z "$CHECKSUM"; then
379 echo "${RED}\$CHECKSUM program missing. This is bad.${NC}" 1>&2
380 exit 1
381 fi
382
383 $CHECKSUM "tarballs/$file" 2>/dev/null | sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@'
384 }
385
386 error_hash_missing() {
387 package="$1"
388 # shellcheck disable=SC2086
389 file="$(eval echo \$$package"_ARCHIVE")"
390
391 fullhashfile="util/crossgcc/sum/$file.cksum"
392 printf "${RED}hash file missing:${NC}\n\n" 1>&2
393 printf "Please verify util/crossgcc/tarball/$file carefully\n" 1>&2
394 printf "(using PGP if possible), and then rename\n" 1>&2
395 printf " ${CYAN}${fullhashfile}.calc${NC}\n" 1>&2
396 printf " to ${CYAN}${fullhashfile}${NC}\n\n" 1>&2
397
398 exit 1
399 }
400
401 # Read the known hash file of the package given in $1, and print it raw.
402 get_known_hash() {
403 package=$1
404 # shellcheck disable=SC2086
405 file="$(eval echo \$$package"_ARCHIVE")"
406 hashfile="sum/$file.cksum"
407
408 if [ ! -f "$hashfile" ]; then
409 calc_hash="$(compute_hash "$package")" || exit 1
410 echo "$calc_hash tarballs/$file" > "${hashfile}.calc"
411
412 error_hash_missing "$package"
413 exit 1
414 fi
415
416 sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@' < "$hashfile"
417 }
418
419 error_hash_mismatch() {
420 package=$1
421 known_hash="$2"
422 computed_hash="$3"
423 # shellcheck disable=SC2086
424 file="$(eval echo \$$package"_ARCHIVE")"
425
426 printf "${RED}hash mismatch:${NC}\n\n"
427 printf " expected (known) hash: $known_hash\n"
428 printf "calculated hash of downloaded file: $computed_hash\n\n"
429
430 printf "If you think this is due to a network error, please delete\n"
431 printf " ${CYAN}util/crossgcc/tarballs/$file${NC}\n"
432 printf "and try again. If the problem persists, it may be due to an\n"
433 printf "administration error on the file server, or you might be\n"
434 printf "subject to a Man-in-the-Middle attack\n\n"
435
436 exit 1
437 }
438
439 # verify_hash - Check that the hash of the file given in $1 matches the known
440 # hash; Bail out on mismatch or missing hash file.
441 verify_hash() {
442 package=$1
443
444 known_hash="$(get_known_hash "$package")" || exit "$?"
445 computed_hash="$(compute_hash "$package")" || exit "$?"
446
447 if [ "$known_hash" != "$computed_hash" ]; then
448 error_hash_mismatch "$package" "$known_hash" "$computed_hash"
449 exit 1
450 fi
451
452 printf "${GREEN}hash verified (${known_hash})${NC}\n"
453 }
454
455 unpack_and_patch() {
456 package="$1"
457 # shellcheck disable=SC2086
458 archive="$(eval echo \$$package"_ARCHIVE")"
459 # shellcheck disable=SC2086
460 dir="$(eval echo \$$package"_DIR")"
461 test -d "${dir}" && test -f "${dir}/.unpack_success" || (
462 printf " * "$archive"\n"
463 FLAGS=zxf
464 suffix=$(echo "$archive" | sed 's,.*\.,,')
465 if [ "$suffix" = "gz" ] && [ -n "$PIGZ" ]; then FLAGS="-I pigz -xf"
466 elif [ "$suffix" = "gz" ]; then FLAGS=zxf
467 elif [ "$suffix" = "bz2" ] && [ -n "$LBZIP2" ]; then FLAGS="-I lbzip2 -xf"
468 elif [ "$suffix" = "bz2" ]; then FLAGS=jxf
469 elif [ "$suffix" = "xz" ]; then FLAGS="--xz -xf"
470 elif [ "$suffix" = "lzma" ]; then FLAGS="--lzma -xf"
471 fi
472 # shellcheck disable=SC2086
473 $TAR $FLAGS "tarballs/$archive"
474 for patch in patches/${dir}_*.patch; do
475 test -r "$patch" || continue
476 printf " o $(basename "$patch")\n"
477 (cd "${dir}" || exit 1; $PATCH -s -N -p1 <"../${patch}") || {
478 printf "\n${RED}Failed $patch.${NC}\n"
479 exit 1
480 }
481 done
482 touch "${dir}/.unpack_success"
483 )
484 }
485
486 fn_exists()
487 {
488 # shellcheck disable=SC2039
489 type "$1" >/dev/null 2>&1
490 }
491
492 is_package_enabled()
493 {
494 echo "$PACKAGES" |grep -q "\<$1\>"
495 }
496
497 package_uses_targetarch()
498 {
499 if [ "$1" = "GCC" ] || [ "$1" = "BINUTILS" ] || [ "$1" = "LIBSTDCXX" ]; then
500 true
501 else
502 false
503 fi
504 }
505
506 generic_build()
507 {
508 package=$1
509 host_target=$2
510 builddir=$3
511 success=$4
512 version=$5
513
514 fn_exists "build_$package" || return
515
516 mkdir -p "$builddir"
517
518 if [ "$package" = "GMP" ] && [ -f "$DESTDIR$TARGETDIR/include/gmp.h" ]; then
519 set_hostcflags_from_gmp
520 fi
521
522 if [ -f "$success" ]; then
523 printf "Skipping $package v$version for $host_target as it is already built\n"
524 else
525 printf "Building $package v$version for $host_target ... "
526 DIR="$PWD"
527 cd "$builddir" || exit 1
528 rm -f .failed
529 "build_${package}" "$host_target" > build.log 2>&1
530 cd "$DIR" || exit 1
531 if [ ! -f "$builddir/.failed" ]; then
532 touch "$success";
533 else
534 printf "${RED}failed${NC}. Check '$builddir/build.log'.\n"
535 exit 1
536 fi
537 printf "${green}ok${NC}\n"
538 fi
539 }
540
541 build_for_host()
542 {
543 package="$1"
544 # shellcheck disable=SC2086
545 version="$(eval echo \$$package"_VERSION")"
546 generic_build "$package" host "build-$package" "${DESTDIR}${TARGETDIR}/.${package}.${version}.success" "$version"
547 }
548
549 build_for_target()
550 {
551 package="$1"
552 # shellcheck disable=SC2086
553 version="$(eval echo \$$package"_VERSION")"
554 generic_build "$package" target "build-${TARGETARCH}-$package" "${DESTDIR}${TARGETDIR}/.${TARGETARCH}-${package}.${version}.success" "$version"
555 }
556
557 build()
558 {
559 if package_uses_targetarch "$1"; then
560 if [ $BOOTSTRAP -eq 1 ] && [ ! -f "${DESTDIR}${TARGETDIR}/.GCC.${GCC_VERSION}.success" ]; then
561 build_for_host GCC
562 fi
563 build_for_target "$1"
564 else
565 build_for_host "$1"
566 fi
567 }
568
569 exit_handler()
570 {
571 printf "${NC}Stop\n"
572 exit 1
573 }
574
575 cleanup()
576 {
577 if [ $SAVETEMPS -ne 0 ]; then
578 printf "Leaving temporary files around... ${green}ok${NC}\n"
579 return
580 fi
581
582 printf "Cleaning up temporary files... "
583 for package in $PACKAGES; do
584 # shellcheck disable=SC2086
585 rm -rf "build-${TARGETARCH}-$package" "build-$package" "$(eval echo \$$package"_DIR")"
586 done
587 rm -f getopt
588 printf "${green}ok${NC}\n"
589 }
590
591 myhelp()
592 {
593 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>] [-C] [-G] [-S]\n"
594 printf " $0 [-V|--version]\n"
595 printf " $0 [-h|--help]\n\n"
596
597 printf "Options:\n"
598 printf " [-W|--print-version Print machine readable version\n"
599 printf " [-V|--version] print version number and exit\n"
600 printf " [-h|--help] print this help and exit\n"
601 printf " [-c|--clean] remove temporary files before build\n"
602 printf " [-t|--savetemps] don't remove temporary files after build\n"
603 printf " [-y|--ccache] Use ccache when building cross compiler\n"
604 printf " [-n|--nocolor] don't print color codes in output\n"
605 printf " [-u|--urls] print the urls for all packages\n"
606 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
607 printf " [-s]--supported <tool> print supported version of a tool\n"
608 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
609 printf " (defaults to $TARGETDIR)\n\n"
610 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
611 printf " (for RPM builds, default unset)\n"
612 printf " [-f|--fetch] Download tarballs, but don't build anything\n"
613 printf " [-P|--package <package>] Build a specific package: GCC, CLANG, IASL\n"
614 printf " (defaults to $PACKAGE)\n"
615 printf "GCC specific options:\n"
616 printf " [-b|--bootstrap] bootstrap the host compiler before building\n"
617 printf " the cross compiler\n"
618 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
619 printf " (defaults to $TARGETARCH)\n"
620 printf " [-l|--languages <languages>] comma separated list of target languages\n"
621 printf " (defaults to $DEFAULT_LANGUAGES)\n"
622 printf "Platforms for GCC:\n"
623 printf " x86_64 i386-elf i386-mingw32 riscv-elf arm aarch64\n"
624 printf " powerpc64le-linux-gnu nds32le-elf\n\n"
625 }
626
627 printversion() {
628 printf "${blue}Welcome to the ${red}coreboot${blue} cross toolchain builder v$CROSSGCC_VERSION ${NC}\n\n"
629 }
630
631 myversion()
632 {
633 printversion
634
635 cat << EOF
636 Copyright (C) 2008-2010 by coresystems GmbH
637 Copyright (C) 2011 by Sage Electronic Engineering
638
639 This program is free software; you can redistribute it and/or modify
640 it under the terms of the GNU General Public License as published by
641 the Free Software Foundation; version 2 of the License.
642
643 This program is distributed in the hope that it will be useful,
644 but WITHOUT ANY WARRANTY; without even the implied warranty of
645 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
646 GNU General Public License for more details.
647
648 EOF
649 }
650
651 have_hostcflags_from_gmp() {
652 grep -q __GMP_CFLAGS "$DESTDIR$TARGETDIR/include/gmp.h" >/dev/null 2>&1
653 }
654
655 set_hostcflags_from_gmp() {
656 # Now set CFLAGS to match GMP CFLAGS but strip out -pedantic
657 # as GCC 4.6.x fails if it's there.
658 HOSTCFLAGS="$(grep __GMP_CFLAGS "$DESTDIR$TARGETDIR/include/gmp.h" |cut -d\" -f2 |\
659 sed s,-pedantic,,)"
660 export HOSTCFLAGS
661 }
662
663 build_GMP() {
664 # Check if GCC enables `-pie` by default (possible since GCC 6).
665 # We need PIC in all static libraries then.
666 if $CC -dumpspecs 2>/dev/null | grep -q '[{;][[:space:]]*:-pie\>'
667 then
668 OPTIONS="$OPTIONS --with-pic"
669 fi
670
671 # shellcheck disable=SC2086
672 CC="$(hostcc host)" CXX="$(hostcxx host)" \
673 ../${GMP_DIR}/configure \
674 --disable-shared \
675 --enable-fat \
676 --prefix="$TARGETDIR" \
677 $OPTIONS || touch .failed
678 # shellcheck disable=SC2086
679 $MAKE $JOBS || touch .failed
680 $MAKE install DESTDIR=$DESTDIR || touch .failed
681
682 normalize_dirs
683
684 set_hostcflags_from_gmp
685 }
686
687 build_MPFR() {
688 test "$UNAME" = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
689 CC="$(hostcc host)" CXX="$(hostcxx host)" \
690 ../${MPFR_DIR}/configure \
691 --disable-shared \
692 --prefix="$TARGETDIR" \
693 --infodir="$TARGETDIR/info" \
694 --with-gmp="$DESTDIR$TARGETDIR" \
695 CFLAGS="$HOSTCFLAGS" || touch .failed
696 # shellcheck disable=SC2086
697 $MAKE $JOBS || touch .failed
698 $MAKE install DESTDIR=$DESTDIR || touch .failed
699
700 normalize_dirs
701
702 # work around build problem of libgmp.la
703 if [ "$DESTDIR" != "" ]; then
704 perl -pi -e "s,$DESTDIR,," "$DESTDIR$TARGETDIR/lib/libgmp.la"
705 fi
706 }
707
708 build_MPC() {
709 CC="$(hostcc host)" CXX="$(hostcxx host)" \
710 ../${MPC_DIR}/configure \
711 --disable-shared \
712 --prefix="$TARGETDIR" \
713 --infodir="$TARGETDIR/info" \
714 --with-mpfr="$DESTDIR$TARGETDIR" \
715 --with-gmp="$DESTDIR$TARGETDIR" \
716 CFLAGS="$HOSTCFLAGS" || touch .failed
717
718 # work around build problem of libmpfr.la
719 if [ "$DESTDIR" != "" ]; then
720 perl -pi -e "s,$TARGETDIR/lib/libgmp.la,$DESTDIR\$&," "$DESTDIR$TARGETDIR/lib/libmpfr.la"
721 fi
722
723 # shellcheck disable=SC2086
724 $MAKE $JOBS || touch .failed
725 $MAKE install DESTDIR=$DESTDIR || touch .failed
726
727 # work around build problem of libmpfr.la
728 if [ "$DESTDIR" != "" ]; then
729 perl -pi -e "s,$DESTDIR,," "$DESTDIR$TARGETDIR/lib/libmpfr.la"
730 fi
731
732 normalize_dirs
733 }
734
735 build_BINUTILS() {
736 if [ $TARGETARCH = "x86_64-elf" ]; then
737 ADDITIONALTARGET=",i386-elf"
738 fi
739 # shellcheck disable=SC2086
740 CC="$(hostcc target)" CXX="$(hostcxx target)" \
741 ../binutils-${BINUTILS_VERSION}/configure \
742 --prefix="$TARGETDIR" \
743 --target=${TARGETARCH} \
744 --enable-targets=${TARGETARCH}${ADDITIONALTARGET} \
745 --disable-werror \
746 --disable-nls \
747 --enable-lto \
748 --enable-gold \
749 --enable-multilib \
750 --disable-docs \
751 --disable-texinfo \
752 ${TARGET_BINUTILS_OPTIONS} \
753 CFLAGS="$HOSTCFLAGS" \
754 CXXFLAGS="$HOSTCFLAGS" || touch .failed
755 # shellcheck disable=SC2086
756 $MAKE $JOBS || touch .failed
757 $MAKE install DESTDIR=$DESTDIR || touch .failed
758 }
759
760 bootstrap_GCC() {
761 # shellcheck disable=SC2086
762 CC="$(hostcc host)" CXX="$(hostcxx host)" \
763 CFLAGS="$HOSTCFLAGS" \
764 CFLAGS_FOR_BUILD="$HOSTCFLAGS" \
765 CFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
766 CXXFLAGS="$HOSTCFLAGS" \
767 CXXFLAGS_FOR_BUILD="$HOSTCFLAGS" \
768 CXXFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
769 ../gcc-${GCC_VERSION}/configure \
770 --prefix="$TARGETDIR" \
771 --libexecdir="$TARGETDIR/lib" \
772 --enable-bootstrap \
773 --disable-werror \
774 --disable-nls \
775 --disable-shared \
776 --disable-multilib \
777 --disable-libssp \
778 --disable-libquadmath \
779 --disable-libcc1 \
780 --disable-libsanitizer \
781 ${GCC_OPTIONS} \
782 --enable-languages="${LANGUAGES}" \
783 --with-gmp="$DESTDIR$TARGETDIR" \
784 --with-mpfr="$DESTDIR$TARGETDIR" \
785 --with-mpc="$DESTDIR$TARGETDIR" \
786 --with-pkgversion="coreboot bootstrap v$CROSSGCC_VERSION" \
787 && \
788 # shellcheck disable=SC2086
789 $MAKE $JOBS BOOT_CFLAGS="$HOSTCFLAGS" BUILD_CONFIG="" bootstrap && \
790 $MAKE install-gcc \
791 install-target-libgcc \
792 maybe-install-target-libada \
793 maybe-install-target-libstdc++-v3 \
794 DESTDIR=$DESTDIR || touch .failed
795 }
796
797 configure_GCC(){
798 # Work around crazy code generator in GCC that confuses CLANG.
799 $CC --version | grep clang >/dev/null 2>&1 && \
800 CLANGFLAGS="-fbracket-depth=1024"
801 [ -n "$CXX" ] && $CXX --version | grep clang >/dev/null 2>&1 && \
802 CLANGCXXFLAGS="-fbracket-depth=1024"
803
804 # standard code model is medlow but all mainboards are compiled with medany code model
805 if [ "${TARGETARCH}" = "riscv64-elf" ]; then
806 CFLAGS_FOR_TARGET_EXTRA="-mcmodel=medany"
807 fi
808
809 # GCC does not honor HOSTCFLAGS at all. CFLAGS are used for
810 # both target and host object files.
811 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
812 # but it does not seem to work properly. At least the host library
813 # libiberty is not compiled with CFLAGS_FOR_BUILD.
814 # Also set the CXX version of the flags because GCC is now compiled
815 # using C++.
816 # shellcheck disable=SC2086
817 CC="$(hostcc target)" CXX="$(hostcxx target)" \
818 CFLAGS_FOR_TARGET="${CFLAGS_FOR_TARGET_EXTRA} -O2 -Dinhibit_libc" \
819 CFLAGS="$HOSTCFLAGS $CLANGFLAGS" \
820 CFLAGS_FOR_BUILD="$HOSTCFLAGS $CLANGFLAGS" \
821 CXXFLAGS="$HOSTCFLAGS $CLANGCXXFLAGS" \
822 CXXFLAGS_FOR_BUILD="$HOSTCFLAGS $CLANGCXXFLAGS" \
823 ../gcc-${GCC_VERSION}/configure \
824 --prefix="$TARGETDIR" \
825 --libexecdir="$TARGETDIR/lib" \
826 --target=${TARGETARCH} \
827 --disable-werror \
828 --disable-shared \
829 --enable-lto \
830 --enable-plugins \
831 --enable-gold \
832 --enable-ld=default \
833 --disable-libssp \
834 --disable-bootstrap \
835 --disable-nls \
836 --disable-libquadmath \
837 --without-headers \
838 --disable-threads \
839 --enable-interwork \
840 --enable-multilib \
841 --enable-targets=all \
842 --disable-libatomic \
843 --disable-libcc1 \
844 --disable-decimal-float \
845 ${GCC_OPTIONS} \
846 ${TARGET_GCC_OPTIONS} \
847 --enable-languages="${LANGUAGES}" \
848 --with-system-zlib \
849 --with-gmp="$DESTDIR$TARGETDIR" \
850 --with-mpfr="$DESTDIR$TARGETDIR" \
851 --with-mpc="$DESTDIR$TARGETDIR" \
852 --with-gnu-as \
853 --with-gnu-ld \
854 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION" \
855 "$@"
856 }
857
858 build_cross_GCC() {
859 configure_GCC && \
860 mkdir -p gcc/$TARGETARCH && \
861 rm -f "gcc/$TARGETARCH/$GCC_VERSION" && \
862 ln -s "$DESTDIR$TARGETDIR/$TARGETARCH/bin" "gcc/$TARGETARCH/$GCC_VERSION" && \
863 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-gcc && \
864 $MAKE install-gcc DESTDIR="$DESTDIR" || touch .failed
865
866 if [ ! -f .failed ] && [ "$(echo $TARGETARCH | grep -c -- -mingw32)" -eq 0 ]; then
867 # shellcheck disable=SC2086
868 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-target-libgcc && \
869 $MAKE install-target-libgcc DESTDIR=$DESTDIR || touch .failed
870 fi
871 }
872
873 build_LIBSTDCXX() {
874 if [ ! -d "${LIBSTDCXX_INCLUDE_PATH}" ]; then
875 touch .failed
876 printf "libstdcxx include path \"%s\" does not exist" "${LIBSTDCXX_INCLUDE_PATH}"
877 return
878 fi
879 configure_GCC \
880 --with-gxx-include-dir="${LIBSTDCXX_INCLUDE_PATH}/coreboot-${TARGETARCH}/include/" \
881 --with-sysroot="${LIBSTDCXX_INCLUDE_PATH}/" && \
882 mkdir -p gcc/$TARGETARCH && \
883 # shellcheck disable=SC2086
884 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-target-libgcc && \
885 $MAKE install-target-libgcc DESTDIR="$DESTDIR" && \
886 # shellcheck disable=SC2086
887 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-target-libstdc++-v3 && \
888 $MAKE install-target-libstdc++-v3 DESTDIR="$DESTDIR" || touch .failed
889 }
890
891 build_GCC() {
892 if [ "$1" = host ]; then
893 bootstrap_GCC "$1"
894 else
895 build_cross_GCC "$1"
896 fi
897 }
898
899 build_IASL() {
900 RDIR=$PWD
901 cd ../$IASL_DIR/generate/unix || exit 1
902 CFLAGS="$HOSTCFLAGS"
903 OPT_LDFLAGS=""
904 HOST="_LINUX"
905 test "$UNAME" = "Darwin" && HOST="_APPLE" && OPT_LDFLAGS="-Wl,-no_fixup_chains"
906 test "$UNAME" = "FreeBSD" && HOST="_FreeBSD"
907 test "$UNAME" = "Cygwin" && HOST="_CYGWIN"
908 HOST="$HOST" CFLAGS="$CFLAGS" \
909 OPT_LDFLAGS="$OPT_LDFLAGS" \
910 OPT_CFLAGS="-O -D_FORTIFY_SOURCE=2 -D COREBOOT_TOOLCHAIN_VERSION='\"coreboot toolchain v$CROSSGCC_VERSION\"' " \
911 $MAKE $JOBS CC="$(hostcc host)" iasl acpibin acpidump acpiexec acpihelp acpisrc acpixtract
912 mkdir -p "$DESTDIR$TARGETDIR/bin/"
913 (cd "$DESTDIR$TARGETDIR/bin" && rm -f iasl acpibin acpidump acpiexec acpihelp acpisrc acpixtract) || touch "$RDIR/.failed"
914 (cd bin && cp iasl acpibin acpidump acpiexec acpihelp acpisrc acpixtract "$DESTDIR$TARGETDIR/bin") || touch "$RDIR/.failed"
915 }
916
917 build_LLVM() {
918
919 ln -nsf "$LLD_DIR" ../lld
920 ln -nsf "$LLVM_DIR" ../llvm
921 ln -nsf "$CLANG_DIR" ../clang
922 ln -nsf "$CTE_DIR" ../clang-tools-extra
923 ln -nsf "$CRT_DIR" ../compiler-rt
924 ln -nsf "$LLVMCMAKE_DIR" ../cmake
925 ln -nsf "$LIBUNWIND_DIR" ../libunwind
926 ln -nsf "$RUNTIMES_DIR" ../runtimes
927
928 $CMAKE -G "Unix Makefiles" \
929 -DCMAKE_INSTALL_PREFIX="$DESTDIR$TARGETDIR" \
930 -DCLANG_VENDOR="coreboot toolchain v$CROSSGCC_VERSION - " \
931 -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;lld" \
932 -DLLVM_INCLUDE_BENCHMARKS="OFF" \
933 -DLLVM_INCLUDE_TESTS="OFF" \
934 -DLLVM_INCLUDE_EXAMPLES="OFF" \
935 -DCMAKE_BUILD_TYPE=Release \
936 -DLLVM_TARGETS_TO_BUILD="AArch64;ARM;PowerPC;RISCV;X86" \
937 -DLLVM_ENABLE_RUNTIMES="compiler-rt" \
938 ../llvm || touch .failed
939 # shellcheck disable=SC2086
940 $MAKE $JOBS || touch .failed
941 $MAKE install || touch .failed
942
943 rm -f ../llvm ../clang ../clang-tools-extra ../compiler-rt ../cmake ../lld ../libunwind ../runtimes
944 }
945
946 build_CMAKE() {
947 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS" \
948 ../${CMAKE_DIR}/configure \
949 --prefix="$TARGETDIR" || touch .failed
950 # shellcheck disable=SC2086
951 $MAKE $JOBS || touch .failed
952 $MAKE install DESTDIR=$DESTDIR || touch .failed
953 }
954
955 build_NASM() {
956 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS" \
957 ../${NASM_DIR}/configure \
958 --prefix="$TARGETDIR" || touch .failed
959 # shellcheck disable=SC2086
960 $MAKE $JOBS || touch .failed
961 $MAKE install DESTDIR=$DESTDIR || touch .failed
962
963 normalize_dirs
964 }
965
966 print_supported() {
967 case "$PRINTSUPPORTED" in
968 BINUTILS|binutils) printf "%s\n" "$BINUTILS_VERSION";;
969 CLANG|clang) printf "%s\n" "$CLANG_VERSION";;
970 GCC|gcc) printf "%s\n" "$GCC_VERSION";;
971 GMP|gmp) printf "%s\n" "$GMP_VERSION";;
972 IASL|iasl) printf "%s\n" "$IASL_VERSION";;
973 MPC|mpc) printf "%s\n" "$MPC_VERSION";;
974 MPFR|mpfr) printf "%s\n" "$MPFR_VERSION";;
975 NASM|nasm) printf "%s\n" "${NASM_VERSION}";;
976 *) printf "Unknown tool %s\n" "$PRINTSUPPORTED";;
977 esac
978 }
979
980 trap exit_handler 1 2 3 15
981
982 # Look if we have getopt. If not, build it.
983 export PATH=$PATH:.
984 getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
985
986 # parse parameters.. try to find out whether we're running GNU getopt
987 getoptbrand="$(getopt -V 2>/dev/null | sed -e '1!d' -e 's,^\(......\).*,\1,')"
988 if [ "${getoptbrand}" = "getopt" ]; then
989 # Detected GNU getopt that supports long options.
990 args=$(getopt -l print-version,version,help,clean,directory:,bootstrap,bootstrap-only,platform:,languages:,package:,libstdcxx_include:,jobs:,destdir:,savetemps,scripting,ccache,supported:,urls,nocolor,mirror,fetch -o WVhcd:bBp:l:P:L:j:D:tSys:unmf -- "$@")
991 getopt_ret=$?
992 eval set -- "$args"
993 else
994 # Detected non-GNU getopt
995 args=$(getopt WVhcd:bBp:l:P:L:j:D:tSys:unm $*)
996 getopt_ret=$?
997 # shellcheck disable=SC2086
998 set -- $args
999 fi
1000
1001 if [ $getopt_ret != 0 ]; then
1002 myhelp
1003 exit 1
1004 fi
1005
1006 while true ; do
1007 case "$1" in
1008 -W|--print-version) shift; echo $CROSSGCC_VERSION; exit 0;;
1009 -V|--version) shift; myversion; exit 0;;
1010 -h|--help) shift; myhelp; exit 0;;
1011 -c|--clean) shift; clean=1;;
1012 -t|--savetemps) shift; SAVETEMPS=1;;
1013 -d|--directory) shift; TARGETDIR="$1"; shift;;
1014 -b|--bootstrap) shift; BOOTSTRAP=1;;
1015 -B|--bootstrap-only) shift; BOOTSTRAPONLY=1; BOOTSTRAP=1;;
1016 -p|--platform) shift; TARGETARCH="$1"; shift;;
1017 -l|--languages) shift; LANGUAGES="$1"; shift;;
1018 -m|--mirror) shift; USE_COREBOOT_MIRROR=1;;
1019 -D|--destdir) shift; DESTDIR="$1"; shift;;
1020 -f|--fetch) shift; FETCH_ONLY=1;;
1021 -j|--jobs) shift; THREADS="$1"; JOBS="-j $1"; shift;;
1022 -P|--package) shift; PACKAGE="$1"; shift;;
1023 -L|--libstdcxx_include) shift; LIBSTDCXX_INCLUDE_PATH="$1"; shift;;
1024 -y|--ccache) shift; USECCACHE=1;;
1025 -s|--supported) shift; PRINTSUPPORTED="$1"; shift;;
1026 -u|--urls) shift; printf "%s\n" "$ALL_ARCHIVES"; exit 0;;
1027 -n|--nocolor) shift; \
1028 unset red RED green GREEN blue BLUE cyan CYAN NC;;
1029 --) shift; break;;
1030 *) break;;
1031 esac
1032 done
1033
1034 if [ $# -gt 0 ]; then
1035 printf "Excessive arguments: $*\n"
1036 myhelp
1037 exit 1
1038 fi
1039
1040 if [ -n "$PRINTSUPPORTED" ]; then
1041 print_supported
1042 exit 0
1043 fi
1044
1045 #print toolchain builder version string as the header
1046 printversion
1047
1048 printf "Building toolchain using %d thread(s).\n\n" "$THREADS"
1049
1050 case "$TARGETARCH" in
1051 x86_64-elf) ;;
1052 x86_64*) TARGETARCH=x86_64-elf;;
1053 i386-elf) ;;
1054 i386-mingw32) ;;
1055 riscv-elf | riscv64-elf)
1056 TARGETARCH=riscv64-elf
1057 TARGET_GCC_OPTIONS="$TARGET_GCC_OPTIONS --with-isa-spec=${RISCV_ISA_SPEC}"
1058 TARGET_BINUTILS_OPTIONS="$TARGET_BINUTILS_OPTIONS --with-isa-spec=${RISCV_ISA_SPEC}";;
1059 powerpc64*-linux*) ;;
1060 i386*) TARGETARCH=i386-elf;;
1061 arm*) TARGETARCH=arm-eabi;;
1062 aarch64*) TARGETARCH=aarch64-elf;;
1063 nds32le-elf) ;;
1064 *) printf "${red}WARNING: Unsupported architecture $TARGETARCH.${NC}\n\n"; ;;
1065 esac
1066
1067 # Figure out which packages to build
1068
1069 case "$PACKAGE" in
1070 GCC|gcc)
1071 echo "Target architecture is $TARGETARCH"
1072 NAME="${TARGETARCH} cross GCC"
1073 PACKAGES="GMP MPFR MPC BINUTILS GCC"
1074 ;;
1075 LIBSTDCXX|libstdcxx)
1076 echo "Target architecture is $TARGETARCH"
1077 NAME="${TARGETARCH} cross libstdcxx"
1078 PACKAGES=LIBSTDCXX
1079 ;;
1080 CLANG|clang)
1081 NAME="LLVM clang"
1082 LLVM_VERSION=${CLANG_VERSION}
1083 PACKAGES="CMAKE LLVM CLANG CRT CTE LLVMCMAKE LLD LIBUNWIND RUNTIMES"
1084 CMAKE=${DESTDIR}${TARGETDIR}/bin/cmake
1085 ;;
1086 IASL|iasl)
1087 NAME="IASL ACPI compiler"
1088 PACKAGES=IASL
1089 ;;
1090 CMAKE|cmake)
1091 NAME="CMake"
1092 PACKAGES=CMAKE
1093 ;;
1094 NASM|nasm)
1095 NAME="NASM"
1096 PACKAGES=NASM
1097 ;;
1098 *)
1099 printf "${red}ERROR: Unsupported package $PACKAGE. (Supported packages are GCC, CLANG, IASL, and NASM)${NC}\n\n";
1100 exit 1
1101 ;;
1102 esac
1103
1104 # Find all the required tools:
1105
1106 TAR=$(searchtool tar) || exit $?
1107 PATCH=$(searchtool patch) || exit $?
1108 MAKE=$(searchtool make) || exit $?
1109 SHA1SUM=$(searchtool sha1sum)
1110 #SHA512SUM=$(searchtool sha512sum)
1111 #MD5SUM=$(searchtool md5sum)
1112 CHECKSUM=$SHA1SUM
1113 LBZIP2=$(searchtool lbzip2 "" nofail)
1114 PIGZ=$(searchtool pigz "" nofail)
1115
1116 searchtool m4 > /dev/null
1117 searchtool bison > /dev/null
1118 searchtool flex flex > /dev/null
1119 searchtool bzip2 "bzip2," > /dev/null
1120 searchtool xz "XZ Utils" "" "xz-utils" > /dev/null
1121
1122 if searchtool wget "GNU" nofail > /dev/null; then
1123 download_showing_percentage() {
1124 url=$1
1125 printf "... ${red} 0%%"
1126 wget --tries=3 "$url" 2>&1 | while read -r line; do
1127 echo "$line" | grep -o "\<[0-9]\{1,3\}%" | awk '{printf("\b\b\b\b%4s", $1)}'
1128 done
1129 printf "${NC}... "
1130 }
1131 elif searchtool curl "^curl " > /dev/null; then
1132 download_showing_percentage() {
1133 url=$1
1134 echo
1135 curl -O --progress-bar --location --retry 3 "$url"
1136 }
1137 fi
1138
1139 # Allow $CC override from the environment.
1140 if [ -n "$CC" ]; then
1141 if [ ! -x "$(command -v "$CC" 2>/dev/null)" ]; then
1142 printf "${RED}ERROR:${red} CC is set to '%s' but wasn't found.${NC}\n" "$CC"
1143 HALT_FOR_TOOLS=1
1144 fi
1145 else
1146 if searchtool gnatgcc "Free Software Foundation" nofail > /dev/null; then
1147 # gnatgcc is deprecated and in recent GCC releases its purpose is
1148 # fulfilled by the gcc binary. In case of a deprecated gnatgcc
1149 # version is installed, it doesn't provide the expected output and
1150 # hostcc_has_gnat1() fails. In this case, just set the value of CC
1151 # to gcc.
1152 # TODO: Remove this whole branch when time is appropriate as the
1153 # second branch fulfills our needs.
1154 CC=gnatgcc
1155 if ! hostcc_has_gnat1; then
1156 CC=gcc
1157 fi
1158 elif searchtool gcc "Free Software Foundation" nofail > /dev/null; then
1159 CC=gcc
1160 else
1161 searchtool cc '^' nofail > /dev/null || please_install gcc
1162 CC=cc
1163 fi
1164 fi
1165
1166 # We can leave $CXX empty if it's not set since *buildgcc* never
1167 # calls it directly. This way configure scripts can search for
1168 # themselves and we still override it when a bootstrapped g++ is
1169 # to be used (cf. hostcxx()).
1170 if [ -n "$CXX" ]; then
1171 if [ ! -x "$(command -v "$CXX" 2>/dev/null)" ]; then
1172 printf "${RED}ERROR:${red} CXX is set to '%s' but wasn't found.${NC}\n" "$CXX"
1173 HALT_FOR_TOOLS=1
1174 fi
1175 else
1176 searchtool g++ "Free Software Foundation" nofail > /dev/null || \
1177 searchtool clang "clang version" nofail > /dev/null || \
1178 searchtool clang "LLVM" "" "g++" > /dev/null
1179 fi
1180
1181 check_for_library "-lz" "zlib (zlib1g-dev or zlib-devel)"
1182
1183 if [ "$HALT_FOR_TOOLS" -ne 0 ]; then
1184 exit 1
1185 fi
1186
1187 # This initial cleanup is useful when updating the toolchain script.
1188
1189 if [ "$clean" = "1" ]; then
1190 cleanup
1191 fi
1192
1193 # Set up host compiler and flags needed for various OSes
1194
1195 if is_package_enabled "GCC"; then
1196 # sane preset: let the configure script figure out things by itself
1197 # more importantly, avoid any values that might already linger in the variable
1198 OPTIONS="ABI="
1199 if [ "$UNAME" = "Darwin" ]; then
1200 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
1201
1202 # generally the OS X compiler can create x64 binaries.
1203 # Per default it generated i386 binaries in 10.5 and x64
1204 # binaries in 10.6 (even if the kernel is 32bit)
1205 # For some weird reason, 10.5 autodetects an ABI=64 though
1206 # so we're setting the ABI explicitly here.
1207 if [ "$(sysctl -n hw.optional.x86_64 2>/dev/null)" -eq 1 ] 2>/dev/null; then
1208 OPTIONS="ABI=64"
1209 elif [ "$(sysctl -n hw.optional.arm64 2>/dev/null)" -eq 1 ] 2>/dev/null; then
1210 OPTIONS="ABI=64"
1211 else
1212 OPTIONS="ABI=32"
1213 fi
1214
1215 # In Xcode 4.5.2 the default compiler is clang.
1216 # However, this compiler fails to compile gcc 4.7.x. As a
1217 # workaround it's possible to compile gcc with llvm-gcc.
1218 if $CC -v 2>&1 | grep -q LLVM; then
1219 CC=llvm-gcc
1220 fi
1221 elif [ "$UNAME" = "Linux" ] || [ "$UNAME" = "Cygwin" ]; then
1222 # gmp is overeager with detecting 64bit CPUs even if they run
1223 # a 32bit kernel and userland.
1224 if [ "$(uname -m 2>/dev/null)" = "i686" ]; then
1225 OPTIONS="ABI=32"
1226 fi
1227 elif [ "$UNAME" = "NetBSD" ]; then
1228 # same for NetBSD but this one reports an i386
1229 if [ "$(uname -m 2>/dev/null)" = "i386" ]; then
1230 OPTIONS="ABI=32"
1231 fi
1232 fi
1233 if [ -z "${LANGUAGES}" ]; then
1234 if have_gnat; then
1235 printf "\nFound compatible Ada compiler, enabling Ada support by default.\n\n"
1236 LANGUAGES="ada,${DEFAULT_LANGUAGES}"
1237 else
1238 printf "\n${red}WARNING${NC}\n"
1239 printf "No compatible Ada compiler (GNAT) found. You can continue without\n"
1240 printf "Ada support, but this will limit the features of ${blue}coreboot${NC} (e.g.\n"
1241 printf "native graphics initialization won't be available on most Intel\n"
1242 printf "boards).\n\n"
1243
1244 printf "Usually, you can install GNAT with your package management system\n"
1245 printf "(the package is called \`gnat\` or \`gcc-ada\`). It has to match the\n"
1246 printf "\`gcc\` package in version. If there are multiple versions of GCC in-\n"
1247 printf "stalled, you can point this script to the matching version through\n"
1248 printf "the \`CC\` and \`CXX\` environment variables.\n\n"
1249
1250 printf "e.g. on Ubuntu 14.04, if \`gcc\` is \`gcc-4.8\`:\n"
1251 printf " apt-get install gnat-4.8 && make crossgcc\n\n"
1252
1253 printf "on Ubuntu 16.04, if \`gcc\` is \`gcc-5\`:\n"
1254 printf " apt-get install gnat-5 && make crossgcc\n"
1255 timeout 30
1256 LANGUAGES="${DEFAULT_LANGUAGES}"
1257 fi
1258 fi
1259 if [ "$BOOTSTRAP" != 1 ] && \
1260 { [ "$(hostcc_major)" -lt 4 ] || \
1261 { [ "$(hostcc_major)" -eq 4 ] && \
1262 [ "$(hostcc_minor)" -lt 9 ] ; } ; }
1263 then
1264 printf "\n${red}WARNING${NC}\n"
1265 printf "Building coreboot requires a host compiler newer than 4.9.x while\n"
1266 printf "yours is $(hostcc_version).\n"
1267 printf "Enabling bootstrapping to provide a sufficiently new compiler:\n"
1268 printf "This will take significantly longer than a usual build.\n"
1269 printf "Alternatively you can abort now and update your host compiler.\n"
1270 timeout 15
1271 BOOTSTRAP=1
1272 fi
1273 if ada_requested; then
1274 if ! have_gnat; then
1275 please_install gnat gcc-ada
1276 exit 1
1277 fi
1278 fi
1279 fi # GCC
1280
1281 export HOSTCFLAGS="-Os"
1282 if have_hostcflags_from_gmp; then
1283 set_hostcflags_from_gmp
1284 fi
1285
1286 if [ "$USECCACHE" = 1 ]; then
1287 CC="ccache $CC"
1288 fi
1289
1290 # Prepare target directory for building GCC
1291 # (dependencies must be in the PATH)
1292 mkdir -p "$DESTDIR$TARGETDIR/bin"
1293 mkdir -p "$DESTDIR$TARGETDIR/share"
1294 export PATH=$DESTDIR$TARGETDIR/bin:$PATH
1295
1296 # Download, unpack, patch and build all packages
1297
1298 printf "Downloading and verifying tarballs ...\n"
1299 mkdir -p tarballs
1300 for P in $PACKAGES; do
1301 download "$P" || exit "$?"
1302 verify_hash "$P" || exit "$?"
1303 done
1304 printf "Downloaded tarballs ... ${green}ok${NC}\n"
1305
1306 if [ ${FETCH_ONLY} -eq 1 ]; then
1307 exit 0
1308 fi
1309
1310 printf "Unpacking and patching ...\n"
1311 for P in $PACKAGES; do
1312 unpack_and_patch $P || exit 1
1313 done
1314 printf "Unpacked and patched ... ${green}ok${NC}\n"
1315
1316 if [ -n "$BOOTSTRAPONLY" ]; then
1317 printf "Building bootstrap compiler only ...\n"
1318 for pkg in GMP MPFR MPC GCC; do
1319 build_for_host $pkg
1320 done
1321 exit 0
1322 fi
1323
1324 printf "Building packages ...\n"
1325 for package in $PACKAGES; do
1326 build $package
1327 done
1328 printf "Packages built ... ${green}ok${NC}\n"
1329
1330 # Adding git information of current tree to target directory
1331 # for reproducibility
1332 PROGNAME=$(basename "$0")
1333 rm -f "$DESTDIR$TARGETDIR/share/$PROGNAME-*"
1334 cp "$PROGNAME" "$DESTDIR$TARGETDIR/share/$PROGNAME-$CROSSGCC_VERSION"
1335
1336 # Adding edk2 tools template
1337 mkdir -p "$DESTDIR$TARGETDIR/share/edk2config"
1338 sed -e "s,@@PREFIX@@,$TARGETDIR,g" edk2tools.txt > "$DESTDIR$TARGETDIR/share/edk2config/tools_def.txt"
1339 printf "Copied EDK2 tools template ... ${green}ok${NC}\n"
1340
1341 cleanup
1342
1343 printf "\n${green}You can now run $NAME from $TARGETDIR.${NC}\n"