blob: 6b7558f2bd191f0f6f6f38ffcd4b443dc0ffca52 [file] [log] [blame]
Martin Roth0ad5fbd2020-12-24 12:06:38 -07001#!/usr/bin/env sh
Martin Roth987d42d2018-07-22 11:45:29 -06002# 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
Stefan Reinauer074d9132009-09-26 16:43:17 +000014#
Patrick Georgi7333a112020-05-08 20:48:04 +020015# SPDX-License-Identifier: GPL-2.0-only
Stefan Reinauer074d9132009-09-26 16:43:17 +000016
Martin Roth987d42d2018-07-22 11:45:29 -060017cd "$(dirname "$0")" || exit 1
Patrick Georgi8135dba2015-03-17 21:05:20 +010018
Felix Singer02750d02021-10-17 15:43:21 +020019if [ -z "$CROSSGCC_VERSION" ]; then
20 CROSSGCC_VERSION="$(git log -n 1 --pretty=%cd --date=short .)_$(git log -n 1 --pretty=%h .)"
21fi
Stefan Reinauer074d9132009-09-26 16:43:17 +000022
23# default settings
Stefan Reinauer85b07d62015-06-09 14:45:14 -070024PACKAGE=GCC
Stefan Reinauerd7649122015-06-09 11:44:25 -070025TARGETDIR=$(pwd)/xgcc
Stefan Reinauer074d9132009-09-26 16:43:17 +000026TARGETARCH=i386-elf
Nico Huber66c2c1a2016-09-20 13:09:29 +020027DEFAULT_LANGUAGES=c
28LANGUAGES=
Stefan Reinauer074d9132009-09-26 16:43:17 +000029DESTDIR=
Stefan Reinauer85b07d62015-06-09 14:45:14 -070030SAVETEMPS=0
Nico Huber234d2462016-01-26 16:10:17 +010031BOOTSTRAP=0
Martin Roth2c6a8062016-11-14 11:58:39 -070032THREADS=1
Stefan Reinauer074d9132009-09-26 16:43:17 +000033
Stefan Reinauer699ac272015-06-05 12:43:28 -070034# GCC toolchain version numbers
Patrick Georgi0afb90a2021-05-10 23:34:18 +020035GMP_VERSION=6.2.1
Elyes HAOUAS150c4382020-07-11 00:40:15 +020036MPFR_VERSION=4.1.0
Patrick Georgif963a0f2021-05-10 23:35:51 +020037MPC_VERSION=1.2.1
Patrick Georgib0d87f72021-05-10 23:39:45 +020038GCC_VERSION=11.2.0
Patrick Georgif28e3682021-05-10 23:33:00 +020039BINUTILS_VERSION=2.37
Elyes HAOUASad3828e2021-11-17 19:27:37 +010040IASL_VERSION=20211217
Stefan Reinauer14ce2132015-06-05 13:01:13 -070041# CLANG version number
Elyes HAOUASa0b80322021-11-17 19:23:22 +010042CLANG_VERSION=13.0.1
Elyes HAOUASe854b0b2021-11-17 19:16:49 +010043CMAKE_VERSION=3.22.2
Elyes HAOUAS1ba663c2020-08-23 07:50:32 +020044NASM_VERSION=2.15.05
Stefan Reinauer074d9132009-09-26 16:43:17 +000045
Stefan Reinauer699ac272015-06-05 12:43:28 -070046# GCC toolchain archive locations
Martin Roth1484c032016-03-08 09:37:14 -070047# These are sanitized by the jenkins toolchain test builder, so if
48# a completely new URL is added here, it probably needs to be added
49# to the jenkins build as well, or the builder won't download it.
Doug Gale43ae4332017-09-27 22:10:16 -040050GMP_ARCHIVE="https://ftpmirror.gnu.org/gmp/gmp-${GMP_VERSION}.tar.xz"
51MPFR_ARCHIVE="https://ftpmirror.gnu.org/mpfr/mpfr-${MPFR_VERSION}.tar.xz"
52MPC_ARCHIVE="https://ftpmirror.gnu.org/mpc/mpc-${MPC_VERSION}.tar.gz"
Patrick Georgib1d26f02018-05-02 17:13:34 +020053GCC_ARCHIVE="https://ftpmirror.gnu.org/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.xz"
Iru Cai776b5ba2017-07-28 21:40:34 +080054BINUTILS_ARCHIVE="https://ftpmirror.gnu.org/binutils/binutils-${BINUTILS_VERSION}.tar.xz"
Patrick Georgi85bb9462016-02-11 17:22:16 +010055IASL_ARCHIVE="https://acpica.org/sites/acpica/files/acpica-unix2-${IASL_VERSION}.tar.gz"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070056# CLANG toolchain archive locations
Elyes HAOUAS8f1853c2020-08-02 09:46:15 +020057LLVM_ARCHIVE="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}/llvm-${CLANG_VERSION}.src.tar.xz"
58CLANG_ARCHIVE="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}/clang-${CLANG_VERSION}.src.tar.xz"
59CRT_ARCHIVE="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}/compiler-rt-${CLANG_VERSION}.src.tar.xz"
60CTE_ARCHIVE="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}/clang-tools-extra-${CLANG_VERSION}.src.tar.xz"
Patrick Georgi5a76a532021-06-03 08:48:26 +020061CMAKE_ARCHIVE="https://cmake.org/files/v${CMAKE_VERSION%.*}/cmake-${CMAKE_VERSION}.tar.gz"
Martin Rothd70f5fa2019-05-26 17:24:19 -060062NASM_ARCHIVE="https://www.nasm.us/pub/nasm/releasebuilds/${NASM_VERSION}/nasm-${NASM_VERSION}.tar.bz2"
Stefan Reinauer074d9132009-09-26 16:43:17 +000063
Nico Huberafda56e2017-12-07 17:50:53 +010064ALL_ARCHIVES="$GMP_ARCHIVE $MPFR_ARCHIVE $MPC_ARCHIVE \
Patrick Georgif32eed12021-10-16 10:50:00 +020065 $GCC_ARCHIVE $BINUTILS_ARCHIVE $IASL_ARCHIVE \
66 $LLVM_ARCHIVE $CLANG_ARCHIVE \
Elyes HAOUAS91fb1392020-01-20 15:33:11 +010067 $CRT_ARCHIVE $CTE_ARCHIVE $CMAKE_ARCHIVE $NASM_ARCHIVE"
Martin Roth58c68d52016-03-01 15:22:42 -070068
Stefan Reinauer699ac272015-06-05 12:43:28 -070069# GCC toolchain directories
Stefan Reinauer074d9132009-09-26 16:43:17 +000070GMP_DIR="gmp-${GMP_VERSION}"
71MPFR_DIR="mpfr-${MPFR_VERSION}"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000072MPC_DIR="mpc-${MPC_VERSION}"
Martin Roth987d42d2018-07-22 11:45:29 -060073# shellcheck disable=SC2034
Stefan Reinauer074d9132009-09-26 16:43:17 +000074GCC_DIR="gcc-${GCC_VERSION}"
Martin Roth987d42d2018-07-22 11:45:29 -060075# shellcheck disable=SC2034
Stefan Reinauer074d9132009-09-26 16:43:17 +000076BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
Patrick Georgi85bb9462016-02-11 17:22:16 +010077IASL_DIR="acpica-unix2-${IASL_VERSION}"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070078# CLANG toolchain directories
79LLVM_DIR="llvm-${CLANG_VERSION}.src"
Elyes HAOUAS8f1853c2020-08-02 09:46:15 +020080CLANG_DIR="clang-${CLANG_VERSION}.src"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070081CRT_DIR="compiler-rt-${CLANG_VERSION}.src"
82CTE_DIR="clang-tools-extra-${CLANG_VERSION}.src"
Stefan Reinauer3b593492017-06-19 17:28:54 -070083CMAKE_DIR="cmake-${CMAKE_VERSION}"
Martin Rothd70f5fa2019-05-26 17:24:19 -060084NASM_DIR="nasm-${NASM_VERSION}"
Stefan Reinauer074d9132009-09-26 16:43:17 +000085
Patrick Georgi6321f522015-02-27 23:36:26 +010086unset MAKELEVEL MAKEFLAGS
87
Patrick Georgi1861ff72011-10-31 12:15:55 +010088red='\033[0;31m'
89RED='\033[1;31m'
90green='\033[0;32m'
91GREEN='\033[1;32m'
92blue='\033[0;34m'
Patrick Georgi1861ff72011-10-31 12:15:55 +010093CYAN='\033[1;36m'
94NC='\033[0m' # No Color
Stefan Reinauer074d9132009-09-26 16:43:17 +000095
Martin Roth987d42d2018-07-22 11:45:29 -060096UNAME=$(if uname | grep -iq cygwin; then echo Cygwin; else uname; fi)
Martin Roth88463822016-01-07 11:03:36 -070097HALT_FOR_TOOLS=0
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070098
Nico Huber08bb8372017-06-24 19:50:35 +020099hostcc()
100{
101 # $1 "host" or "target"
Martin Roth987d42d2018-07-22 11:45:29 -0600102 if [ "$BOOTSTRAP" = 1 ] && [ "$1" = target ]; then
103 echo "$DESTDIR$TARGETDIR/bin/gcc"
Nico Huber08bb8372017-06-24 19:50:35 +0200104 else
Martin Roth987d42d2018-07-22 11:45:29 -0600105 echo "$CC"
Nico Huber08bb8372017-06-24 19:50:35 +0200106 fi
107}
108
109hostcxx()
110{
111 # $1 "host" or "target"
Martin Roth987d42d2018-07-22 11:45:29 -0600112 if [ "$BOOTSTRAP" = 1 ] && [ "$1" = target ]; then
113 echo "$DESTDIR$TARGETDIR/bin/g++"
Nico Huber08bb8372017-06-24 19:50:35 +0200114 else
Martin Roth987d42d2018-07-22 11:45:29 -0600115 echo "$CXX"
Nico Huber08bb8372017-06-24 19:50:35 +0200116 fi
117}
118
Patrick Georgi198d23c2012-12-08 08:02:44 +0100119normalize_dirs()
120{
Martin Roth987d42d2018-07-22 11:45:29 -0600121 mkdir -p "$DESTDIR$TARGETDIR/lib"
122 test -d "$DESTDIR$TARGETDIR/lib32" && mv "$DESTDIR$TARGETDIR"/lib32/* "$DESTDIR$TARGETDIR/lib"
123 test -d "$DESTDIR$TARGETDIR/lib64" && mv "$DESTDIR$TARGETDIR"/lib64/* "$DESTDIR$TARGETDIR/lib"
Martin Roth1039d272022-01-31 10:17:43 -0700124 rm -rf "$DESTDIR$TARGETDIR/lib32" "$DESTDIR$TARGETDIR/lib64"
Patrick Georgi198d23c2012-12-08 08:02:44 +0100125
Martin Roth987d42d2018-07-22 11:45:29 -0600126 perl -pi -e "s,/lib32,/lib," "$DESTDIR$TARGETDIR"/lib/*.la
127 perl -pi -e "s,/lib64,/lib," "$DESTDIR$TARGETDIR"/lib/*.la
Patrick Georgi198d23c2012-12-08 08:02:44 +0100128}
129
Nico Hubercd87e1b2017-06-24 20:35:59 +0200130countdown()
131{
132 tout=${1:-10}
133
Martin Roth987d42d2018-07-22 11:45:29 -0600134 printf "\nPress Ctrl-C to abort, Enter to continue... %2ds" "$tout"
135 while [ "$tout" -gt 0 ]; do
Nico Hubercd87e1b2017-06-24 20:35:59 +0200136 sleep 1
137 tout=$((tout - 1))
138 printf "\b\b\b%2ds" $tout
139 done
140 printf "\n"
141}
142
143timeout()
144{
145 tout=${1:-10}
146
147 # Ignore SIGUSR1, should interrupt `read` though.
148 trap false USR1
149 # Clean up in case the user aborts.
150 trap 'kill $counter > /dev/null 2>&1' EXIT
151
Martin Roth987d42d2018-07-22 11:45:29 -0600152 (countdown "$tout"; kill -USR1 $$)&
Nico Hubercd87e1b2017-06-24 20:35:59 +0200153 counter=$!
154
155 # Some shells with sh compatibility mode (e.g. zsh, mksh) only
156 # let us interrupt `read` if a non-standard -t parameter is given.
Martin Roth987d42d2018-07-22 11:45:29 -0600157 # shellcheck disable=SC2034,SC2039,SC2162
Nico Hubercd87e1b2017-06-24 20:35:59 +0200158 if echo | read -t 1 foo 2>/dev/null; then
159 read -t $((tout + 1)) foo
160 else
161 read foo
162 fi
163
164 kill $counter > /dev/null 2>&1
165 trap - USR1 EXIT
166}
167
Stefan Reinauer93a96302015-06-15 12:36:53 -0700168please_install()
169{
Martin Roth88463822016-01-07 11:03:36 -0700170 HALT_FOR_TOOLS=1
Martin Roth987d42d2018-07-22 11:45:29 -0600171 # shellcheck disable=SC1091
Patrick Georgi5da95dc2015-07-17 23:33:05 +0200172 test -r /etc/os-release && . /etc/os-release
Alex Thiessen72d10892018-01-12 01:45:53 +0000173 # vanilla debian doesn't define `ID_LIKE`, just `ID`
174 if [ -z "${ID_LIKE}" ] && [ -n "${ID}" ]; then
175 ID_LIKE=${ID}
176 fi
Stefan Reinauer93a96302015-06-15 12:36:53 -0700177 case "$ID_LIKE" in
178 debian) solution="sudo apt-get install $1" ;;
179 suse) solution="sudo zypper install $1" ;;
180 *) solution="using your OS packaging system" ;;
181 esac
182
Patrick Georgidf1ff232017-01-07 09:28:43 +0100183 printf "${RED}ERROR:${red} Missing tool: Please install '$1'. (eg $solution)${NC}\n" >&2
Martin Rothe3963172016-01-06 13:54:32 -0700184 if [ -n "$2" ]; then
Patrick Georgidf1ff232017-01-07 09:28:43 +0100185 printf "${RED}ERROR:${red} or install '$2'.${NC}\n" >&2
Martin Rothe3963172016-01-06 13:54:32 -0700186 fi
Stefan Reinauer93a96302015-06-15 12:36:53 -0700187}
188
Patrick Georgib7062882015-02-24 10:52:14 +0100189searchtool()
Stefan Reinauer074d9132009-09-26 16:43:17 +0000190{
191 # $1 short name
Patrick Georgib7062882015-02-24 10:52:14 +0100192 # $2 search string
193 # $3 soft fail if set
Martin Rothe3963172016-01-06 13:54:32 -0700194 # $4 alternative package to install on failure
Patrick Georgib7062882015-02-24 10:52:14 +0100195 # result: file name of that tool on stdout
196 # or no output if nothing suitable was found
197 search=GNU
198 if [ -n "$2" ]; then
199 search="$2"
200 fi
Stefan Reinauer074d9132009-09-26 16:43:17 +0000201 for i in "$1" "g$1" "gnu$1"; do
Martin Roth987d42d2018-07-22 11:45:29 -0600202 if [ -x "$(command -v "$i" 2>/dev/null)" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700203 if [ "$(cat /dev/null | $i --version 2>&1 | grep -c "$search")" \
204 -gt 0 ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600205 echo "$i"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000206 return
207 fi
208 fi
209 done
Andrew Wub67e9a12014-04-28 18:13:44 +0800210 # A workaround for OSX 10.9 and some BSDs, whose nongnu
211 # patch and tar also work.
Martin Roth987d42d2018-07-22 11:45:29 -0600212 if [ "$UNAME" = "Darwin" ] || [ "$UNAME" = "FreeBSD" ] || [ "$UNAME" = "NetBSD" ] || [ "$UNAME" = "OpenBSD" ]; then
213 if [ "$1" = "patch" ] || [ "$1" = "tar" ]; then
214 if [ -x "$(command -v "$1" 2>/dev/null)" ]; then
215 echo "$1"
Zheng Bao36156ff2012-09-28 16:18:58 +0800216 return
217 fi
218 fi
219 fi
Martin Roth987d42d2018-07-22 11:45:29 -0600220 if echo "$1" | grep -q "sum" ; then
221 algor=$(echo "$1" | sed -e 's,sum,,')
222 if [ -x "$(command -v "$1" 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400223 #xxxsum [file]
Martin Roth987d42d2018-07-22 11:45:29 -0600224 echo "$1"
zbao5cf758d2015-09-01 22:28:57 -0400225 return
Martin Roth987d42d2018-07-22 11:45:29 -0600226 elif [ -x "$(command -v "$algor" 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400227 #xxx [file]
Martin Roth987d42d2018-07-22 11:45:29 -0600228 echo "$algor"
zbao5cf758d2015-09-01 22:28:57 -0400229 return
Martin Rothd55f5eb2017-06-15 11:37:26 -0600230 elif [ -x "$(command -v openssl 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400231 #openssl xxx [file]
Martin Roth987d42d2018-07-22 11:45:29 -0600232 echo openssl "$algor"
zbao5cf758d2015-09-01 22:28:57 -0400233 return
Martin Rothd55f5eb2017-06-15 11:37:26 -0600234 elif [ -x "$(command -v cksum 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400235 #cksum -a xxx [file]
236 #cksum has special options in NetBSD. Actually, NetBSD will use the second case above.
Martin Roth987d42d2018-07-22 11:45:29 -0600237 echo "buildgcc" | cksum -a "$algor" > /dev/null 2>/dev/null && \
238 echo cksum -a "$algor"
zbao5cf758d2015-09-01 22:28:57 -0400239 return
zbao939dc842015-04-30 04:44:07 +0800240 fi
241 fi
Martin Rothe3963172016-01-06 13:54:32 -0700242
Martin Roth987d42d2018-07-22 11:45:29 -0600243 [ -z "$3" ] && please_install "$1" "$4"
Patrick Georgib7062882015-02-24 10:52:14 +0100244 false
Stefan Reinauer074d9132009-09-26 16:43:17 +0000245}
246
Martin Roth591790f2016-03-20 20:39:04 -0600247# Run a compile check of the specified library option to see if it's installed
248check_for_library() {
Martin Roth987d42d2018-07-22 11:45:29 -0600249 LIBRARY_FLAGS="$1"
250 LIBRARY_PACKAGES="$2"
251 LIBTEST_FILE=.libtest
Martin Roth591790f2016-03-20 20:39:04 -0600252
253 echo "int main(int argc, char **argv) { (void) argc; (void) argv; return 0; }" > "${LIBTEST_FILE}.c"
254
Martin Roth987d42d2018-07-22 11:45:29 -0600255 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200256 "$CC" $CFLAGS $LIBRARY_FLAGS "${LIBTEST_FILE}.c" -o "${LIBTEST_FILE}" >/dev/null 2>&1 || \
Martin Roth591790f2016-03-20 20:39:04 -0600257 please_install "$LIBRARY_PACKAGES"
258 rm -rf "${LIBTEST_FILE}.c" "${LIBTEST_FILE}"
259}
260
Nico Huberaee7f462016-09-20 12:55:19 +0200261buildcc_major() {
262 echo "${GCC_VERSION}" | cut -d. -f1
263}
264
Nico Huber26267a72016-12-04 02:45:58 +0100265buildcc_minor() {
266 echo "${GCC_VERSION}" | cut -d. -f2
267}
268
Nico Huberaee7f462016-09-20 12:55:19 +0200269buildcc_version() {
270 echo "${GCC_VERSION}" | cut -d. -f1-2
271}
272
273hostcc_major() {
274 (echo __GNUC__ | ${CC} -E - 2>/dev/null || echo 0) | tail -1
275}
276
277hostcc_minor() {
278 (echo __GNUC_MINOR__ | ${CC} -E - 2>/dev/null || echo 0) | tail -1
279}
280
281hostcc_version() {
282 printf "%d.%d" "$(hostcc_major)" "$(hostcc_minor)"
283}
284
Nico Huber75797162016-09-20 13:01:57 +0200285hostcc_has_gnat1() {
286 [ -x "$(${CC} -print-prog-name=gnat1)" ]
287}
288
Nico Hubercd87e1b2017-06-24 20:35:59 +0200289have_gnat() {
290 hostcc_has_gnat1 && \
291 searchtool gnatbind "Free Software Foundation" nofail > /dev/null
292}
293
Nico Huber75797162016-09-20 13:01:57 +0200294ada_requested() {
295 echo "${LANGUAGES}" | grep -q '\<ada\>'
296}
297
Stefan Reinauer88935482015-06-05 09:51:10 -0700298download() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700299 package=$1
Martin Roth987d42d2018-07-22 11:45:29 -0600300 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700301 archive="$(eval echo \$$package"_ARCHIVE")"
Stefan Reinauer699ac272015-06-05 12:43:28 -0700302
Martin Roth987d42d2018-07-22 11:45:29 -0600303 FILE=$(basename "$archive")
Stefan Reinauer88935482015-06-05 09:51:10 -0700304 printf " * $FILE "
305
Martin Roth987d42d2018-07-22 11:45:29 -0600306 if test -f "tarballs/$FILE"; then
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200307 printf "(cached)... "
zbaod22b9ea2015-08-30 23:35:16 -0400308 else
Paul Menzelb2ba3952015-06-12 11:58:14 +0200309 printf "(downloading from $archive)"
Martin Roth987d42d2018-07-22 11:45:29 -0600310 rm -f "tarballs/$FILE"
311 cd tarballs || exit 1
312 download_showing_percentage "$archive"
Stefan Reinauer88935482015-06-05 09:51:10 -0700313 cd ..
zbaod22b9ea2015-08-30 23:35:16 -0400314 fi
315
Martin Roth987d42d2018-07-22 11:45:29 -0600316 if [ ! -f "tarballs/$FILE" ]; then
Patrick Georgidf1ff232017-01-07 09:28:43 +0100317 printf "${RED}Failed to download $FILE.${NC}\n"
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700318 exit 1
319 fi
Stefan Reinauer88935482015-06-05 09:51:10 -0700320}
321
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200322# Compute the hash of the package given in $1, and print it raw (just the
323# hexadecimal hash).
324compute_hash() {
325 package=$1
Martin Roth987d42d2018-07-22 11:45:29 -0600326 # shellcheck disable=SC2086
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200327 archive="$(eval echo \$$package"_ARCHIVE")"
328 file="$(basename "$archive")"
329
330 if test -z "$CHECKSUM"; then
331 echo "${RED}\$CHECKSUM program missing. This is bad.${NC}" 1>&2
332 exit 1
333 fi
334
335 $CHECKSUM "tarballs/$file" 2>/dev/null | sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@'
336}
337
338error_hash_missing() {
339 package="$1"
Martin Roth987d42d2018-07-22 11:45:29 -0600340 # shellcheck disable=SC2086
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200341 archive="$(eval echo \$$package"_ARCHIVE")"
342 file="$(basename "$archive")"
343
344 fullhashfile="util/crossgcc/sum/$file.cksum"
345 printf "${RED}hash file missing:${NC}\n\n" 1>&2
346 printf "Please verify util/crossgcc/tarball/$file carefully\n" 1>&2
347 printf "(using PGP if possible), and then rename\n" 1>&2
348 printf " ${CYAN}${fullhashfile}.calc${NC}\n" 1>&2
349 printf " to ${CYAN}${fullhashfile}${NC}\n\n" 1>&2
350
351 exit 1
352}
353
354# Read the known hash file of the package given in $1, and print it raw.
355get_known_hash() {
356 package=$1
Martin Roth987d42d2018-07-22 11:45:29 -0600357 # shellcheck disable=SC2086
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200358 archive="$(eval echo \$$package"_ARCHIVE")"
359 file="$(basename "$archive")"
360 hashfile="sum/$file.cksum"
361
362 if [ ! -f "$hashfile" ]; then
363 calc_hash="$(compute_hash "$package")" || exit 1
364 echo "$calc_hash tarballs/$file" > "${hashfile}.calc"
365
366 error_hash_missing "$package"
367 exit 1
368 fi
369
Martin Roth987d42d2018-07-22 11:45:29 -0600370 sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@' < "$hashfile"
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200371}
372
373error_hash_mismatch() {
374 package=$1
375 known_hash="$2"
376 computed_hash="$3"
Martin Roth987d42d2018-07-22 11:45:29 -0600377 # shellcheck disable=SC2086
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200378 archive="$(eval echo \$$package"_ARCHIVE")"
379 file="$(basename "$archive")"
380
381 printf "${RED}hash mismatch:${NC}\n\n"
382 printf " expected (known) hash: $known_hash\n"
383 printf "calculated hash of downloaded file: $computed_hash\n\n"
384
385 printf "If you think this is due to a network error, please delete\n"
386 printf " ${CYAN}util/crossgcc/tarballs/$file${NC}\n"
387 printf "and try again. If the problem persists, it may be due to an\n"
388 printf "administration error on the file server, or you might be\n"
389 printf "subject to a Man-in-the-Middle attack\n\n"
390
391 exit 1
392}
393
394# verify_hash - Check that the hash of the file given in $1 matches the known
395# hash; Bail out on mismatch or missing hash file.
396verify_hash() {
397 package=$1
Martin Roth987d42d2018-07-22 11:45:29 -0600398 # shellcheck disable=SC2086
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200399 archive="$(eval echo \$$package"_ARCHIVE")"
400
401 known_hash="$(get_known_hash "$package")" || exit "$?"
402 computed_hash="$(compute_hash "$package")" || exit "$?"
403
404 if [ "$known_hash" != "$computed_hash" ]; then
405 error_hash_mismatch "$package" "$known_hash" "$computed_hash"
406 exit 1
407 fi
408
Jonathan Neuschäfercb76069e2018-10-01 09:45:49 +0200409 printf "${GREEN}hash verified (${known_hash})${NC}\n"
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200410}
411
Stefan Reinauer88935482015-06-05 09:51:10 -0700412unpack_and_patch() {
Martin Roth987d42d2018-07-22 11:45:29 -0600413 package="$1"
414 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700415 archive="$(eval echo \$$package"_ARCHIVE")"
Martin Roth987d42d2018-07-22 11:45:29 -0600416 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700417 dir="$(eval echo \$$package"_DIR")"
Martin Roth987d42d2018-07-22 11:45:29 -0600418 test -d "${dir}" && test -f "${dir}/.unpack_success" || (
419 printf " * $(basename "$archive")\n"
Stefan Reinauer88935482015-06-05 09:51:10 -0700420 FLAGS=zxf
Martin Roth987d42d2018-07-22 11:45:29 -0600421 suffix=$(echo "$archive" | sed 's,.*\.,,')
Martin Roth95f7b222016-03-20 12:38:48 -0600422 if [ "$suffix" = "gz" ] && [ -n "$PIGZ" ]; then FLAGS="-I pigz -xf"
423 elif [ "$suffix" = "gz" ]; then FLAGS=zxf
424 elif [ "$suffix" = "bz2" ] && [ -n "$LBZIP2" ]; then FLAGS="-I lbzip2 -xf"
425 elif [ "$suffix" = "bz2" ]; then FLAGS=jxf
426 elif [ "$suffix" = "xz" ]; then FLAGS="--xz -xf"
427 elif [ "$suffix" = "lzma" ]; then FLAGS="--lzma -xf"
428 fi
Martin Roth987d42d2018-07-22 11:45:29 -0600429 # shellcheck disable=SC2086
430 $TAR $FLAGS "tarballs/$(basename "$archive")"
Stefan Reinauer88935482015-06-05 09:51:10 -0700431 for patch in patches/${dir}_*.patch; do
Martin Roth987d42d2018-07-22 11:45:29 -0600432 test -r "$patch" || continue
433 printf " o $(basename "$patch")\n"
434 (cd "${dir}" || exit 1; $PATCH -s -N -p1 <"../${patch}") || {
Stefan Reinauer88935482015-06-05 09:51:10 -0700435 printf "\n${RED}Failed $patch.${NC}\n"
Stefan Reinauer916b8452015-06-09 11:54:49 -0700436 exit 1
437 }
Stefan Reinauer88935482015-06-05 09:51:10 -0700438 done
Martin Roth987d42d2018-07-22 11:45:29 -0600439 touch "${dir}/.unpack_success"
Stefan Reinauer88935482015-06-05 09:51:10 -0700440 )
441}
442
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700443fn_exists()
444{
Martin Roth987d42d2018-07-22 11:45:29 -0600445 # shellcheck disable=SC2039
446 type "$1" >/dev/null 2>&1
Patrick Georgi74c06422015-03-25 18:40:13 +0100447}
448
Patrick Georgif2c15f52015-06-11 21:07:31 +0200449is_package_enabled()
450{
451 echo "$PACKAGES" |grep -q "\<$1\>"
452}
453
Martin Roth444ece22016-04-01 18:46:29 -0600454package_uses_targetarch()
455{
Patrick Georgif32eed12021-10-16 10:50:00 +0200456 if [ "$1" = "GCC" ] || [ "$1" = "BINUTILS" ]; then
Martin Roth444ece22016-04-01 18:46:29 -0600457 true
458 else
459 false
460 fi
461}
462
Nico Huber11ea2b32016-01-26 16:09:31 +0100463generic_build()
464{
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700465 package=$1
Nico Huber11ea2b32016-01-26 16:09:31 +0100466 host_target=$2
467 builddir=$3
468 success=$4
Martin Rothc295d5e2016-11-14 11:56:11 -0700469 version=$5
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700470
Martin Roth987d42d2018-07-22 11:45:29 -0600471 fn_exists "build_$package" || return
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700472
Nico Huber11ea2b32016-01-26 16:09:31 +0100473 mkdir -p "$builddir"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700474
Nico Huber11ea2b32016-01-26 16:09:31 +0100475 if [ -f "$success" ]; then
Martin Roth2c6a8062016-11-14 11:58:39 -0700476 printf "Skipping $package v$version for $host_target as it is already built\n"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700477 else
Martin Roth2c6a8062016-11-14 11:58:39 -0700478 printf "Building $package v$version for $host_target ... "
Nico Huber11ea2b32016-01-26 16:09:31 +0100479 DIR="$PWD"
Martin Roth987d42d2018-07-22 11:45:29 -0600480 cd "$builddir" || exit 1
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700481 rm -f .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600482 "build_${package}" "$host_target" > build.log 2>&1
483 cd "$DIR" || exit 1
Nico Huber11ea2b32016-01-26 16:09:31 +0100484 if [ ! -f "$builddir/.failed" ]; then
485 touch "$success";
486 else
Idwer Volleringe2ef3cf2017-08-15 00:53:13 +0200487 printf "${RED}failed${NC}. Check '$builddir/build.log'.\n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700488 exit 1
489 fi
490 printf "${green}ok${NC}\n"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700491 fi
492}
493
Nico Huber11ea2b32016-01-26 16:09:31 +0100494build_for_host()
495{
Martin Rothc295d5e2016-11-14 11:56:11 -0700496 package="$1"
Martin Roth987d42d2018-07-22 11:45:29 -0600497 # shellcheck disable=SC2086
Martin Rothc295d5e2016-11-14 11:56:11 -0700498 version="$(eval echo \$$package"_VERSION")"
Patrick Georgi1b593e52016-11-25 15:27:41 +0100499 generic_build "$package" host "build-$package" "${DESTDIR}${TARGETDIR}/.${package}.${version}.success" "$version"
Nico Huber11ea2b32016-01-26 16:09:31 +0100500}
501
502build_for_target()
503{
Martin Rothc295d5e2016-11-14 11:56:11 -0700504 package="$1"
Martin Roth987d42d2018-07-22 11:45:29 -0600505 # shellcheck disable=SC2086
Martin Rothc295d5e2016-11-14 11:56:11 -0700506 version="$(eval echo \$$package"_VERSION")"
Patrick Georgi1b593e52016-11-25 15:27:41 +0100507 generic_build "$package" target "build-${TARGETARCH}-$package" "${DESTDIR}${TARGETDIR}/.${TARGETARCH}-${package}.${version}.success" "$version"
Nico Huber11ea2b32016-01-26 16:09:31 +0100508}
509
510build()
511{
Martin Roth987d42d2018-07-22 11:45:29 -0600512 if package_uses_targetarch "$1"; then
513 if [ $BOOTSTRAP -eq 1 ] && [ ! -f "${DESTDIR}${TARGETDIR}/.GCC.${GCC_VERSION}.success" ]; then
Nico Huber234d2462016-01-26 16:10:17 +0100514 build_for_host GCC
515 fi
Martin Roth987d42d2018-07-22 11:45:29 -0600516 build_for_target "$1"
Nico Huber11ea2b32016-01-26 16:09:31 +0100517 else
Martin Roth987d42d2018-07-22 11:45:29 -0600518 build_for_host "$1"
Nico Huber11ea2b32016-01-26 16:09:31 +0100519 fi
520}
521
Nico Huber18c74d62017-06-24 20:35:59 +0200522exit_handler()
Zheng Baobb003c82016-08-05 13:41:51 +0800523{
524 printf "${NC}Stop\n"
525 exit 1
526}
527
Stefan Reinauer074d9132009-09-26 16:43:17 +0000528cleanup()
529{
Stefan Reinauer88e83e52016-04-06 15:39:48 -0700530 if [ $SAVETEMPS -ne 0 ]; then
531 printf "Leaving temporary files around... ${green}ok${NC}\n"
532 return
533 fi
534
Stefan Reinauer074d9132009-09-26 16:43:17 +0000535 printf "Cleaning up temporary files... "
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700536 for package in $PACKAGES; do
Martin Roth987d42d2018-07-22 11:45:29 -0600537 # shellcheck disable=SC2086
538 rm -rf "build-${TARGETARCH}-$package" "build-$package" "$(eval echo \$$package"_DIR")"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700539 done
zbaoff3f15c2015-08-26 22:00:31 -0400540 rm -f getopt
Stefan Reinauer074d9132009-09-26 16:43:17 +0000541 printf "${green}ok${NC}\n"
542}
543
544myhelp()
545{
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700546 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>] [-C] [-G] [-S]\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000547 printf " $0 [-V|--version]\n"
548 printf " $0 [-h|--help]\n\n"
549
550 printf "Options:\n"
Felix Singer60c56be2021-10-17 14:54:12 +0200551 printf " [-W|--print-version Print machine readable version\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000552 printf " [-V|--version] print version number and exit\n"
553 printf " [-h|--help] print this help and exit\n"
554 printf " [-c|--clean] remove temporary files before build\n"
555 printf " [-t|--savetemps] don't remove temporary files after build\n"
Patrick Georgic1a75b12011-11-04 21:37:14 +0100556 printf " [-y|--ccache] Use ccache when building cross compiler\n"
Nico Huber152e78e2016-09-20 15:38:40 +0200557 printf " [-n|--nocolor] don't print color codes in output\n"
558 printf " [-u|--urls] print the urls for all packages\n"
Patrick Georgi73166c72010-04-14 20:42:42 +0000559 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
Martin Roth588c79d2016-03-20 12:03:20 -0600560 printf " [-s]--supported <tool> print supported version of a tool\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000561 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
Martin Roth588c79d2016-03-20 12:03:20 -0600562 printf " (defaults to $TARGETDIR)\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000563 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100564 printf " (for RPM builds, default unset)\n"
Patrick Georgif32eed12021-10-16 10:50:00 +0200565 printf " [-P|--package <package>] Build a specific package: GCC, CLANG, IASL\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700566 printf " (defaults to $PACKAGE)\n"
567 printf "GCC specific options:\n"
Nico Huber234d2462016-01-26 16:10:17 +0100568 printf " [-b|--bootstrap] bootstrap the host compiler before building\n"
569 printf " the cross compiler\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700570 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
571 printf " (defaults to $TARGETARCH)\n"
Nico Huberbb313bf2015-09-08 12:30:27 +0200572 printf " [-l|--languages <languages>] comma separated list of target languages\n"
Nico Huber66c2c1a2016-09-20 13:09:29 +0200573 printf " (defaults to $DEFAULT_LANGUAGES)\n"
Patrick Georgif32eed12021-10-16 10:50:00 +0200574 printf "Platforms for GCC:\n"
Julius Wernerf96d9052019-08-16 15:35:39 -0700575 printf " x86_64 i386-elf i386-mingw32 riscv-elf arm aarch64\n"
Jonathan Neuschäferf14f6402016-03-11 20:22:23 +0100576 printf " powerpc64le-linux-gnu nds32le-elf\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000577}
578
Martin Rothedf965a2015-11-25 16:58:33 -0700579printversion() {
Martin Roth7014f822020-12-21 15:49:33 -0700580 printf "${blue}Welcome to the ${red}coreboot${blue} cross toolchain builder v$CROSSGCC_VERSION ${NC}\n\n"
Martin Rothedf965a2015-11-25 16:58:33 -0700581}
582
Stefan Reinauer074d9132009-09-26 16:43:17 +0000583myversion()
584{
Martin Rothedf965a2015-11-25 16:58:33 -0700585 printversion
Stefan Reinauer074d9132009-09-26 16:43:17 +0000586
587 cat << EOF
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000588Copyright (C) 2008-2010 by coresystems GmbH
Marc Jones2aac3f62011-08-08 16:07:50 -0600589Copyright (C) 2011 by Sage Electronic Engineering
Stefan Reinauer074d9132009-09-26 16:43:17 +0000590
591This program is free software; you can redistribute it and/or modify
592it under the terms of the GNU General Public License as published by
593the Free Software Foundation; version 2 of the License.
594
595This program is distributed in the hope that it will be useful,
596but WITHOUT ANY WARRANTY; without even the implied warranty of
597MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
598GNU General Public License for more details.
599
600EOF
601}
602
Nico Huber5f72f962016-01-31 23:05:19 +0100603have_hostcflags_from_gmp() {
Martin Roth987d42d2018-07-22 11:45:29 -0600604 grep -q __GMP_CFLAGS "$DESTDIR$TARGETDIR/include/gmp.h" >/dev/null 2>&1
Nico Huber5f72f962016-01-31 23:05:19 +0100605}
606
607set_hostcflags_from_gmp() {
608 # Now set CFLAGS to match GMP CFLAGS but strip out -pedantic
609 # as GCC 4.6.x fails if it's there.
Martin Roth987d42d2018-07-22 11:45:29 -0600610 HOSTCFLAGS="$(grep __GMP_CFLAGS "$DESTDIR$TARGETDIR/include/gmp.h" |cut -d\" -f2 |\
Idwer Volleringba349ab2016-07-24 02:10:19 +0200611 sed s,-pedantic,,)"
Martin Roth987d42d2018-07-22 11:45:29 -0600612 export HOSTCFLAGS
Nico Huber5f72f962016-01-31 23:05:19 +0100613}
614
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700615build_GMP() {
Nico Huber8bbd5962016-12-22 16:05:54 +0100616 # Check if GCC enables `-pie` by default (possible since GCC 6).
617 # We need PIC in all static libraries then.
Nico Huber2c1c02e2017-01-02 20:51:28 +0100618 if $CC -dumpspecs 2>/dev/null | grep -q '[{;][[:space:]]*:-pie\>'
Nico Huber8bbd5962016-12-22 16:05:54 +0100619 then
620 OPTIONS="$OPTIONS --with-pic"
621 fi
622
Martin Roth987d42d2018-07-22 11:45:29 -0600623 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200624 CC="$(hostcc host)" CXX="$(hostcxx host)" \
625 ../${GMP_DIR}/configure --disable-shared --enable-fat \
Martin Roth987d42d2018-07-22 11:45:29 -0600626 --prefix="$TARGETDIR" $OPTIONS \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700627 || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600628 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700629 $MAKE $JOBS || touch .failed
630 $MAKE install DESTDIR=$DESTDIR || touch .failed
631
632 normalize_dirs
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700633
Nico Huber5f72f962016-01-31 23:05:19 +0100634 set_hostcflags_from_gmp
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700635}
636
637build_MPFR() {
Martin Roth987d42d2018-07-22 11:45:29 -0600638 test "$UNAME" = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
Nico Huber08bb8372017-06-24 19:50:35 +0200639 CC="$(hostcc host)" CXX="$(hostcxx host)" \
Martin Roth987d42d2018-07-22 11:45:29 -0600640 ../${MPFR_DIR}/configure --disable-shared --prefix="$TARGETDIR" \
641 --infodir="$TARGETDIR/info" \
642 --with-gmp="$DESTDIR$TARGETDIR" CFLAGS="$HOSTCFLAGS" || \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700643 touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600644 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700645 $MAKE $JOBS || touch .failed
646 $MAKE install DESTDIR=$DESTDIR || touch .failed
647
648 normalize_dirs
649
650 # work around build problem of libgmp.la
651 if [ "$DESTDIR" != "" ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600652 perl -pi -e "s,$DESTDIR,," "$DESTDIR$TARGETDIR/lib/libgmp.la"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700653 fi
654}
655
656build_MPC() {
Nico Huber08bb8372017-06-24 19:50:35 +0200657 CC="$(hostcc host)" CXX="$(hostcxx host)" \
Martin Roth987d42d2018-07-22 11:45:29 -0600658 ../${MPC_DIR}/configure --disable-shared --prefix="$TARGETDIR" \
659 --infodir="$TARGETDIR/info" --with-mpfr="$DESTDIR$TARGETDIR" \
660 --with-gmp="$DESTDIR$TARGETDIR" CFLAGS="$HOSTCFLAGS" || \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700661 touch .failed
Patrick Georgi790aab62017-04-26 17:35:35 +0200662
663 # work around build problem of libmpfr.la
664 if [ "$DESTDIR" != "" ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600665 perl -pi -e "s,$TARGETDIR/lib/libgmp.la,$DESTDIR\$&," "$DESTDIR$TARGETDIR/lib/libmpfr.la"
Patrick Georgi790aab62017-04-26 17:35:35 +0200666 fi
667
Martin Roth987d42d2018-07-22 11:45:29 -0600668 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700669 $MAKE $JOBS || touch .failed
670 $MAKE install DESTDIR=$DESTDIR || touch .failed
671
Patrick Georgi790aab62017-04-26 17:35:35 +0200672 # work around build problem of libmpfr.la
673 if [ "$DESTDIR" != "" ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600674 perl -pi -e "s,$DESTDIR,," "$DESTDIR$TARGETDIR/lib/libmpfr.la"
Patrick Georgi790aab62017-04-26 17:35:35 +0200675 fi
676
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700677 normalize_dirs
678}
679
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700680build_BINUTILS() {
Nico Huberae6187f2016-12-22 22:16:39 +0100681 if [ $TARGETARCH = "x86_64-elf" ]; then
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700682 ADDITIONALTARGET=",i386-elf"
683 fi
Nico Huber08bb8372017-06-24 19:50:35 +0200684 CC="$(hostcc target)" CXX="$(hostcxx target)" \
Martin Roth987d42d2018-07-22 11:45:29 -0600685 ../binutils-${BINUTILS_VERSION}/configure --prefix="$TARGETDIR" \
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700686 --target=${TARGETARCH} --enable-targets=${TARGETARCH}${ADDITIONALTARGET} \
687 --disable-werror --disable-nls --enable-lto --enable-gold \
Stefan Reinauer2fe2d3d2016-04-05 02:14:48 -0700688 --enable-interwork --enable-multilib \
Vivia Nikolaidou81084362018-04-28 01:21:44 +0300689 --enable-plugins --enable-multilibs \
Nico Huber010ecf802016-12-22 16:19:37 +0100690 CFLAGS="$HOSTCFLAGS" \
691 CXXFLAGS="$HOSTCFLAGS" \
692 || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600693 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700694 $MAKE $JOBS || touch .failed
695 $MAKE install DESTDIR=$DESTDIR || touch .failed
696}
697
Nico Huber234d2462016-01-26 16:10:17 +0100698bootstrap_GCC() {
Martin Roth987d42d2018-07-22 11:45:29 -0600699 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200700 CC="$(hostcc host)" CXX="$(hostcxx host)" \
Nico Huber234d2462016-01-26 16:10:17 +0100701 CFLAGS="$HOSTCFLAGS" \
702 CFLAGS_FOR_BUILD="$HOSTCFLAGS" \
703 CFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
704 CXXFLAGS="$HOSTCFLAGS" \
705 CXXFLAGS_FOR_BUILD="$HOSTCFLAGS" \
706 CXXFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
707 ../gcc-${GCC_VERSION}/configure \
Martin Roth987d42d2018-07-22 11:45:29 -0600708 --prefix="$TARGETDIR" --libexecdir="$TARGETDIR/lib" \
Nico Huber234d2462016-01-26 16:10:17 +0100709 --enable-bootstrap \
710 --disable-werror --disable-nls \
711 --disable-shared --disable-multilib \
712 --disable-libssp --disable-libquadmath --disable-libcc1 \
Patrick Georgi97a9df42017-04-26 22:18:18 +0200713 --disable-libsanitizer \
Nico Huber234d2462016-01-26 16:10:17 +0100714 ${GCC_OPTIONS} --enable-languages="${LANGUAGES}" \
Martin Roth987d42d2018-07-22 11:45:29 -0600715 --with-gmp="$DESTDIR$TARGETDIR" --with-mpfr="$DESTDIR$TARGETDIR" \
716 --with-mpc="$DESTDIR$TARGETDIR" \
Martin Roth7014f822020-12-21 15:49:33 -0700717 --with-pkgversion="coreboot bootstrap v$CROSSGCC_VERSION" \
Nico Huber234d2462016-01-26 16:10:17 +0100718 && \
Martin Roth987d42d2018-07-22 11:45:29 -0600719 # shellcheck disable=SC2086
Nico Huber234d2462016-01-26 16:10:17 +0100720 $MAKE $JOBS BOOT_CFLAGS="$HOSTCFLAGS" BUILD_CONFIG="" bootstrap && \
721 $MAKE install-gcc \
722 install-target-libgcc \
723 maybe-install-target-libada \
724 maybe-install-target-libstdc++-v3 \
Nico Huber08bb8372017-06-24 19:50:35 +0200725 DESTDIR=$DESTDIR || touch .failed
Nico Huber234d2462016-01-26 16:10:17 +0100726}
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700727
Nico Huber234d2462016-01-26 16:10:17 +0100728build_cross_GCC() {
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700729 # Work around crazy code generator in GCC that confuses CLANG.
Aaron Durbinb2229dc2016-04-07 16:58:10 -0500730 $CC --version | grep clang >/dev/null 2>&1 && \
Patrick Georgia7a5a562017-06-26 07:21:46 +0200731 CLANGFLAGS="-fbracket-depth=1024"
Nico Huber3f12d932017-08-31 23:11:18 +0200732 [ -n "$CXX" ] && $CXX --version | grep clang >/dev/null 2>&1 && \
Patrick Georgia7a5a562017-06-26 07:21:46 +0200733 CLANGCXXFLAGS="-fbracket-depth=1024"
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700734
Stefan Reinauerd0f83722015-06-09 11:52:28 -0700735 # GCC does not honor HOSTCFLAGS at all. CFLAGS are used for
736 # both target and host object files.
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700737 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
738 # but it does not seem to work properly. At least the host library
739 # libiberty is not compiled with CFLAGS_FOR_BUILD.
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700740 # Also set the CXX version of the flags because GCC is now compiled
741 # using C++.
Martin Roth987d42d2018-07-22 11:45:29 -0600742 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200743 CC="$(hostcc target)" CXX="$(hostcxx target)" \
Nico Huber3f12d932017-08-31 23:11:18 +0200744 CFLAGS_FOR_TARGET="-O2 -Dinhibit_libc" \
Patrick Georgia7a5a562017-06-26 07:21:46 +0200745 CFLAGS="$HOSTCFLAGS $CLANGFLAGS" \
746 CFLAGS_FOR_BUILD="$HOSTCFLAGS $CLANGFLAGS" \
747 CXXFLAGS="$HOSTCFLAGS $CLANGCXXFLAGS" \
748 CXXFLAGS_FOR_BUILD="$HOSTCFLAGS $CLANGCXXFLAGS" \
749 ../gcc-${GCC_VERSION}/configure \
Martin Roth987d42d2018-07-22 11:45:29 -0600750 --prefix="$TARGETDIR" --libexecdir="$TARGETDIR/lib" \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700751 --target=${TARGETARCH} --disable-werror --disable-shared \
Stefan Reinauer2f97ebc2015-06-09 12:24:54 -0700752 --enable-lto --enable-plugins --enable-gold --enable-ld=default \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700753 --disable-libssp --disable-bootstrap --disable-nls \
754 --disable-libquadmath --without-headers \
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100755 --disable-threads \
Patrick Georgi1fc02d12016-03-23 21:44:43 +0100756 --enable-interwork --enable-multilib --enable-targets=all \
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100757 --disable-libatomic --disable-libcc1 --disable-decimal-float \
Nico Huberbb313bf2015-09-08 12:30:27 +0200758 ${GCC_OPTIONS} --enable-languages="${LANGUAGES}" \
Ronald G. Minnich7ee16b72016-02-12 21:54:59 +0000759 --with-system-zlib \
Martin Roth987d42d2018-07-22 11:45:29 -0600760 --with-gmp="$DESTDIR$TARGETDIR" --with-mpfr="$DESTDIR$TARGETDIR" \
761 --with-mpc="$DESTDIR$TARGETDIR" \
Patrick Georgidce4d462018-09-07 01:00:54 +0200762 --with-gnu-as --with-gnu-ld \
Martin Roth7014f822020-12-21 15:49:33 -0700763 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION" \
Nico Huber659f40b2018-05-23 17:06:53 +0200764 && \
Patrick Georgi095db332018-06-26 21:00:58 +0200765 mkdir -p gcc/$TARGETARCH && \
Nico Hubere87fcd42020-12-21 00:11:54 +0100766 rm -f "gcc/$TARGETARCH/$GCC_VERSION" && \
Martin Roth987d42d2018-07-22 11:45:29 -0600767 ln -s "$DESTDIR$TARGETDIR/$TARGETARCH/bin" "gcc/$TARGETARCH/$GCC_VERSION" && \
Nico Huber659f40b2018-05-23 17:06:53 +0200768 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-gcc && \
Martin Roth987d42d2018-07-22 11:45:29 -0600769 $MAKE install-gcc DESTDIR="$DESTDIR" || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700770
Martin Roth987d42d2018-07-22 11:45:29 -0600771 if [ ! -f .failed ] && [ "$(echo $TARGETARCH | grep -c -- -mingw32)" -eq 0 ]; then
772 # shellcheck disable=SC2086
Nico Huber659f40b2018-05-23 17:06:53 +0200773 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-target-libgcc && \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700774 $MAKE install-target-libgcc DESTDIR=$DESTDIR || touch .failed
775 fi
776}
777
Nico Huber234d2462016-01-26 16:10:17 +0100778build_GCC() {
779 if [ "$1" = host ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600780 bootstrap_GCC "$1"
Nico Huber234d2462016-01-26 16:10:17 +0100781 else
Martin Roth987d42d2018-07-22 11:45:29 -0600782 build_cross_GCC "$1"
Nico Huber234d2462016-01-26 16:10:17 +0100783 fi
784}
785
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700786build_IASL() {
787 RDIR=$PWD
Martin Roth987d42d2018-07-22 11:45:29 -0600788 cd ../$IASL_DIR/generate/unix || exit 1
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700789 CFLAGS="$HOSTCFLAGS"
790 HOST="_LINUX"
Martin Roth987d42d2018-07-22 11:45:29 -0600791 test "$UNAME" = "Darwin" && HOST="_APPLE"
792 test "$UNAME" = "FreeBSD" && HOST="_FreeBSD"
793 test "$UNAME" = "Cygwin" && HOST="_CYGWIN"
Nico Huber08bb8372017-06-24 19:50:35 +0200794 HOST="$HOST" CFLAGS="$CFLAGS" \
Martin Roth7014f822020-12-21 15:49:33 -0700795 OPT_CFLAGS="-O -D_FORTIFY_SOURCE=2 -D COREBOOT_TOOLCHAIN_VERSION='\"coreboot toolchain v$CROSSGCC_VERSION\"' " \
Elyes HAOUAS157851f2020-05-29 10:20:14 +0200796 $MAKE $JOBS CC="$(hostcc host)" iasl acpibin acpidump acpiexec acpihelp acpisrc acpixtract
Patrick Georgi54f86a52021-05-11 15:06:37 +0200797 mkdir -p "$DESTDIR$TARGETDIR/bin/"
798 (cd "$DESTDIR$TARGETDIR/bin" && rm -f iasl acpibin acpidump acpiexec acpihelp acpisrc acpixtract) || touch "$RDIR/.failed"
799 (cd bin && cp iasl acpibin acpidump acpiexec acpihelp acpisrc acpixtract "$DESTDIR$TARGETDIR/bin") || touch "$RDIR/.failed"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700800}
801
802build_LLVM() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700803
Patrick Georgi5a76a532021-06-03 08:48:26 +0200804 ln -nsf "$LLVM_DIR" ../llvm
805 ln -nsf "$CLANG_DIR" ../clang
806 ln -nsf "$CTE_DIR" ../clang-tools-extra
807 ln -nsf "$CRT_DIR" ../compiler-rt
Martin Roth987d42d2018-07-22 11:45:29 -0600808
809 $CMAKE -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$DESTDIR$TARGETDIR" \
Martin Roth7014f822020-12-21 15:49:33 -0700810 -DCLANG_VENDOR="coreboot toolchain v$CROSSGCC_VERSION - " \
Patrick Georgi5a76a532021-06-03 08:48:26 +0200811 -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;compiler-rt" \
812 -DCMAKE_BUILD_TYPE=Release ../llvm || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600813 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700814 $MAKE $JOBS || touch .failed
815 $MAKE install || touch .failed
816
Patrick Georgi5a76a532021-06-03 08:48:26 +0200817 rm -f ../llvm ../clang ../clang-tools-extra ../compiler-rt
818
Elyes HAOUAS8f1853c2020-08-02 09:46:15 +0200819 cp -a ../$CLANG_DIR/tools/scan-build/* "$DESTDIR$TARGETDIR/bin"
820 cp -a ../$CLANG_DIR/tools/scan-view/* "$DESTDIR$TARGETDIR/bin"
Stefan Reinauere9e6e3d2015-07-07 00:20:42 +0200821
822 # create symlinks to work around broken --print-librt-file-name
823 # when used with -target.
Martin Roth987d42d2018-07-22 11:45:29 -0600824 cd "$DESTDIR$TARGETDIR/lib/clang/${CLANG_VERSION}/lib" || exit 1
Stefan Reinauere9e6e3d2015-07-07 00:20:42 +0200825 for i in */libclang_rt.builtins*.a; do
Martin Roth987d42d2018-07-22 11:45:29 -0600826 ln -s "$i" .
Stefan Reinauere9e6e3d2015-07-07 00:20:42 +0200827 done
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700828}
829
Stefan Reinauer3b593492017-06-19 17:28:54 -0700830build_CMAKE() {
Nico Huber08bb8372017-06-24 19:50:35 +0200831 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS" \
Martin Roth987d42d2018-07-22 11:45:29 -0600832 ../${CMAKE_DIR}/configure --prefix="$TARGETDIR" \
Stefan Reinauer3b593492017-06-19 17:28:54 -0700833 || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600834 # shellcheck disable=SC2086
Stefan Reinauer3b593492017-06-19 17:28:54 -0700835 $MAKE $JOBS || touch .failed
836 $MAKE install DESTDIR=$DESTDIR || touch .failed
Stefan Reinauer3b593492017-06-19 17:28:54 -0700837}
838
Martin Rothd70f5fa2019-05-26 17:24:19 -0600839build_NASM() {
840 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS"
841 ../${NASM_DIR}/configure --prefix="$TARGETDIR" \
842 || touch .failed
843 # shellcheck disable=SC2086
844 $MAKE $JOBS || touch .failed
845 $MAKE install DESTDIR=$DESTDIR || touch .failed
846
847 normalize_dirs
848}
849
Stefan Reinauerc66fd3c2017-06-21 14:57:28 -0700850print_supported() {
851 case "$PRINTSUPPORTED" in
Martin Rothedf965a2015-11-25 16:58:33 -0700852 BINUTILS|binutils) printf "%s\n" "$BINUTILS_VERSION";;
853 CLANG|clang) printf "%s\n" "$CLANG_VERSION";;
Martin Rothedf965a2015-11-25 16:58:33 -0700854 GCC|gcc) printf "%s\n" "$GCC_VERSION";;
Martin Rothedf965a2015-11-25 16:58:33 -0700855 GMP|gmp) printf "%s\n" "$GMP_VERSION";;
856 IASL|iasl) printf "%s\n" "$IASL_VERSION";;
Martin Rothedf965a2015-11-25 16:58:33 -0700857 MPC|mpc) printf "%s\n" "$MPC_VERSION";;
858 MPFR|mpfr) printf "%s\n" "$MPFR_VERSION";;
Martin Rothd70f5fa2019-05-26 17:24:19 -0600859 NASM|nasm) printf "%s\n" "${NASM_VERSION}";;
Stefan Reinauerc66fd3c2017-06-21 14:57:28 -0700860 *) printf "Unknown tool %s\n" "$PRINTSUPPORTED";;
Martin Rothedf965a2015-11-25 16:58:33 -0700861 esac
862}
Stefan Reinauer074d9132009-09-26 16:43:17 +0000863
Nico Huber18c74d62017-06-24 20:35:59 +0200864trap exit_handler 1 2 3 15
Zheng Baobb003c82016-08-05 13:41:51 +0800865
Patrick Georgi6bba29f2010-01-29 17:40:52 +0000866# Look if we have getopt. If not, build it.
867export PATH=$PATH:.
868getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
869
Stefan Reinauer074d9132009-09-26 16:43:17 +0000870# parse parameters.. try to find out whether we're running GNU getopt
Nico Huber27e8412b2017-12-05 22:02:21 +0100871getoptbrand="$(getopt -V 2>/dev/null | sed -e '1!d' -e 's,^\(......\).*,\1,')"
Peter Stuge09377b72011-08-21 06:24:55 +0200872if [ "${getoptbrand}" = "getopt" ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000873 # Detected GNU getopt that supports long options.
Felix Singer60c56be2021-10-17 14:54:12 +0200874 args=$(getopt -l print-version,version,help,clean,directory:,bootstrap,bootstrap-only,platform:,languages:,package:,jobs:,destdir:,savetemps,scripting,ccache,supported:,urls,nocolor -o WVhcd:bBp:l:P:j:D:tSys:un -- "$@")
Nico Huber78df0bf2016-09-20 14:11:53 +0200875 getopt_ret=$?
Nico Hubercc414dd2016-09-20 13:59:38 +0200876 eval set -- "$args"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000877else
878 # Detected non-GNU getopt
Felix Singer60c56be2021-10-17 14:54:12 +0200879 args=$(getopt WVhcd:bBp:l:P:j:D:tSys:un $*)
Nico Huber78df0bf2016-09-20 14:11:53 +0200880 getopt_ret=$?
Martin Roth987d42d2018-07-22 11:45:29 -0600881 # shellcheck disable=SC2086
Stefan Reinauer074d9132009-09-26 16:43:17 +0000882 set -- $args
883fi
884
Nico Huber78df0bf2016-09-20 14:11:53 +0200885if [ $getopt_ret != 0 ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000886 myhelp
887 exit 1
888fi
889
890while true ; do
891 case "$1" in
Felix Singer60c56be2021-10-17 14:54:12 +0200892 -W|--print-version) shift; echo $CROSSGCC_VERSION; exit 0;;
Patrick Georgi9681cdc2017-09-22 18:57:10 +0200893 -V|--version) shift; myversion; exit 0;;
894 -h|--help) shift; myhelp; exit 0;;
895 -c|--clean) shift; clean=1;;
896 -t|--savetemps) shift; SAVETEMPS=1;;
897 -d|--directory) shift; TARGETDIR="$1"; shift;;
898 -b|--bootstrap) shift; BOOTSTRAP=1;;
899 -B|--bootstrap-only) shift; BOOTSTRAPONLY=1; BOOTSTRAP=1;;
900 -p|--platform) shift; TARGETARCH="$1"; shift;;
901 -l|--languages) shift; LANGUAGES="$1"; shift;;
902 -D|--destdir) shift; DESTDIR="$1"; shift;;
903 -j|--jobs) shift; THREADS="$1"; JOBS="-j $1"; shift;;
904 -P|--package) shift; PACKAGE="$1"; shift;;
Patrick Georgi9681cdc2017-09-22 18:57:10 +0200905 -y|--ccache) shift; USECCACHE=1;;
906 -s|--supported) shift; PRINTSUPPORTED="$1"; shift;;
907 -u|--urls) shift; printf "%s\n" "$ALL_ARCHIVES"; exit 0;;
908 -n|--nocolor) shift; \
Martin Roth4a53db02016-03-01 15:21:07 -0700909 unset red RED green GREEN blue BLUE cyan CYAN NC;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200910 --) shift; break;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200911 *) break;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000912 esac
913done
914
Nico Huberbd74d562016-09-20 14:16:43 +0200915if [ $# -gt 0 ]; then
916 printf "Excessive arguments: $*\n"
917 myhelp
918 exit 1
919fi
920
Stefan Reinauerc66fd3c2017-06-21 14:57:28 -0700921if [ -n "$PRINTSUPPORTED" ]; then
922 print_supported
Martin Rothedf965a2015-11-25 16:58:33 -0700923 exit 0
924fi
925
926#print toolchain builder version string as the header
927printversion
928
Martin Roth2c6a8062016-11-14 11:58:39 -0700929printf "Building toolchain using %d thread(s).\n\n" "$THREADS"
930
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700931case "$TARGETARCH" in
Timothy Pearsona9f62352015-12-30 18:59:00 -0600932 x86_64-elf) ;;
933 x86_64*) TARGETARCH=x86_64-elf;;
934 i386-elf) ;;
935 i386-mingw32) ;;
Iru Caiae8e3a02016-04-02 10:50:59 +0800936 riscv-elf) TARGETARCH=riscv64-elf;;
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100937 powerpc64*-linux*) ;;
Timothy Pearsona9f62352015-12-30 18:59:00 -0600938 i386*) TARGETARCH=i386-elf;;
Stefan Reinauer27522ad2016-01-29 17:31:34 -0800939 arm*) TARGETARCH=arm-eabi;;
Timothy Pearsona9f62352015-12-30 18:59:00 -0600940 aarch64*) TARGETARCH=aarch64-elf;;
Martin Roth3e0f74d2016-03-08 12:07:04 -0700941 nds32le-elf) ;;
Timothy Pearsona9f62352015-12-30 18:59:00 -0600942 *) printf "${red}WARNING: Unsupported architecture $TARGETARCH.${NC}\n\n"; ;;
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700943esac
944
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700945# Figure out which packages to build
Stefan Reinauer699ac272015-06-05 12:43:28 -0700946
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700947case "$PACKAGE" in
948 GCC|gcc)
Stefan Reinauer81693d42017-06-21 15:25:36 -0700949 echo "Target architecture is $TARGETARCH"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700950 NAME="${TARGETARCH} cross GCC"
Nico Huberafda56e2017-12-07 17:50:53 +0100951 PACKAGES="GMP MPFR MPC BINUTILS GCC"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700952 ;;
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700953 CLANG|clang)
Stefan Reinauer81693d42017-06-21 15:25:36 -0700954 NAME="LLVM clang"
955 LLVM_VERSION=${CLANG_VERSION}
Elyes HAOUAS8f1853c2020-08-02 09:46:15 +0200956 PACKAGES="CMAKE LLVM CLANG CRT CTE"
Stefan Reinauerca117e72017-06-19 17:29:08 -0700957 CMAKE=${DESTDIR}${TARGETDIR}/bin/cmake
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700958 ;;
959 IASL|iasl)
960 NAME="IASL ACPI compiler"
961 PACKAGES=IASL
962 ;;
Stefan Reinauer3b593492017-06-19 17:28:54 -0700963 CMAKE|cmake)
964 NAME="CMake"
965 PACKAGES=CMAKE
966 ;;
Martin Rothd70f5fa2019-05-26 17:24:19 -0600967 NASM|nasm)
968 NAME="NASM"
969 PACKAGES=NASM
970 ;;
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700971 *)
Patrick Georgif32eed12021-10-16 10:50:00 +0200972 printf "${red}ERROR: Unsupported package $PACKAGE. (Supported packages are GCC, CLANG, IASL, and NASM)${NC}\n\n";
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700973 exit 1
974 ;;
975esac
Peter Stugeceacd772011-06-09 04:54:16 +0200976
Stefan Reinauer66e93352015-06-16 22:38:23 +0200977# Find all the required tools:
978
979TAR=$(searchtool tar) || exit $?
980PATCH=$(searchtool patch) || exit $?
981MAKE=$(searchtool make) || exit $?
982SHA1SUM=$(searchtool sha1sum)
zbao5cf758d2015-09-01 22:28:57 -0400983#SHA512SUM=$(searchtool sha512sum)
984#MD5SUM=$(searchtool md5sum)
Stefan Reinauer66e93352015-06-16 22:38:23 +0200985CHECKSUM=$SHA1SUM
Martin Roth95f7b222016-03-20 12:38:48 -0600986LBZIP2=$(searchtool lbzip2 "" nofail)
987PIGZ=$(searchtool pigz "" nofail)
Stefan Reinauer66e93352015-06-16 22:38:23 +0200988
989searchtool m4 > /dev/null
990searchtool bison > /dev/null
991searchtool flex flex > /dev/null
Stefan Reinauer66e93352015-06-16 22:38:23 +0200992searchtool bzip2 "bzip2," > /dev/null
Nico Huber0ff3b392017-03-12 16:07:33 +0100993searchtool xz "XZ Utils" "" "xz-utils" > /dev/null
Stefan Reinauer66e93352015-06-16 22:38:23 +0200994
Patrick Georgidf1ff232017-01-07 09:28:43 +0100995if searchtool wget "GNU" nofail > /dev/null; then
996 download_showing_percentage() {
997 url=$1
Stefan Reinauer81693d42017-06-21 15:25:36 -0700998 printf "... ${red} 0%%"
Martin Roth82a30a12020-11-07 15:17:45 -0700999 wget --tries=3 "$url" 2>&1 | while read -r line; do
Martin Roth987d42d2018-07-22 11:45:29 -06001000 echo "$line" | grep -o "[0-9]\+%" | awk '{printf("\b\b\b\b%4s", $1)}'
Patrick Georgidf1ff232017-01-07 09:28:43 +01001001 done
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +02001002 printf "${NC}... "
Patrick Georgidf1ff232017-01-07 09:28:43 +01001003 }
1004elif searchtool curl "^curl " > /dev/null; then
1005 download_showing_percentage() {
1006 url=$1
1007 echo
Idwer Vollering5190f422020-11-24 15:13:16 +01001008 curl -O --progress-bar --location --retry 3 "$url"
Patrick Georgidf1ff232017-01-07 09:28:43 +01001009 }
1010fi
1011
Nico Huber08bb8372017-06-24 19:50:35 +02001012# Allow $CC override from the environment.
1013if [ -n "$CC" ]; then
1014 if [ ! -x "$(command -v "$CC" 2>/dev/null)" ]; then
1015 printf "${RED}ERROR:${red} CC is set to '%s' but wasn't found.${NC}\n" "$CC"
1016 HALT_FOR_TOOLS=1
1017 fi
1018else
Patrick Georgi8f2cdad2017-07-10 11:48:34 +02001019 if searchtool gnatgcc "Free Software Foundation" nofail > /dev/null; then
1020 CC=gnatgcc
1021 elif searchtool gcc "Free Software Foundation" nofail > /dev/null; then
Nico Huber08bb8372017-06-24 19:50:35 +02001022 CC=gcc
1023 else
1024 searchtool cc '^' nofail > /dev/null || please_install gcc
1025 CC=cc
1026 fi
1027fi
Martin Roth591790f2016-03-20 20:39:04 -06001028
Nico Huber08bb8372017-06-24 19:50:35 +02001029# We can leave $CXX empty if it's not set since *buildgcc* never
1030# calls it directly. This way configure scripts can search for
1031# themselves and we still override it when a bootstrapped g++ is
1032# to be used (cf. hostcxx()).
1033if [ -n "$CXX" ]; then
1034 if [ ! -x "$(command -v "$CXX" 2>/dev/null)" ]; then
1035 printf "${RED}ERROR:${red} CXX is set to '%s' but wasn't found.${NC}\n" "$CXX"
1036 HALT_FOR_TOOLS=1
1037 fi
1038else
1039 searchtool g++ "Free Software Foundation" nofail > /dev/null || \
1040 searchtool clang "clang version" nofail > /dev/null || \
1041 searchtool clang "LLVM" "" "g++" > /dev/null
1042fi
1043
1044check_for_library "-lz" "zlib (zlib1g-dev or zlib-devel)"
Nico Huber156d87c2016-09-20 12:59:53 +02001045
Martin Roth88463822016-01-07 11:03:36 -07001046if [ "$HALT_FOR_TOOLS" -ne 0 ]; then
1047 exit 1
1048fi
1049
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001050# This initial cleanup is useful when updating the toolchain script.
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001051
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001052if [ "$clean" = "1" ]; then
1053 cleanup
1054fi
Stefan Reinauer074d9132009-09-26 16:43:17 +00001055
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001056# Set up host compiler and flags needed for various OSes
Stefan Reinauer074d9132009-09-26 16:43:17 +00001057
Patrick Georgif2c15f52015-06-11 21:07:31 +02001058if is_package_enabled "GCC"; then
Patrick Georgie8253fe2017-07-18 11:35:35 +02001059# sane preset: let the configure script figure out things by itself
1060# more importantly, avoid any values that might already linger in the variable
1061OPTIONS="ABI="
Martin Roth987d42d2018-07-22 11:45:29 -06001062if [ "$UNAME" = "Darwin" ]; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001063 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
1064
1065 # generally the OS X compiler can create x64 binaries.
1066 # Per default it generated i386 binaries in 10.5 and x64
1067 # binaries in 10.6 (even if the kernel is 32bit)
1068 # For some weird reason, 10.5 autodetects an ABI=64 though
1069 # so we're setting the ABI explicitly here.
Martin Roth987d42d2018-07-22 11:45:29 -06001070 if [ "$(sysctl -n hw.optional.x86_64 2>/dev/null)" -eq 1 ] 2>/dev/null; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001071 OPTIONS="ABI=64"
1072 else
1073 OPTIONS="ABI=32"
1074 fi
Stefan Reinauer9491b4d2011-10-11 22:37:59 -07001075
Stefan Reinauer4266b922012-12-05 16:18:32 -08001076 # In Xcode 4.5.2 the default compiler is clang.
1077 # However, this compiler fails to compile gcc 4.7.x. As a
1078 # workaround it's possible to compile gcc with llvm-gcc.
Stefan Reinauer9491b4d2011-10-11 22:37:59 -07001079 if $CC -v 2>&1 | grep -q LLVM; then
Stefan Reinauer4266b922012-12-05 16:18:32 -08001080 CC=llvm-gcc
Stefan Reinauer9491b4d2011-10-11 22:37:59 -07001081 fi
Martin Roth987d42d2018-07-22 11:45:29 -06001082elif [ "$UNAME" = "Linux" ] || [ "$UNAME" = "Cygwin" ]; then
Patrick Georgiddb8f802015-07-04 17:45:54 +02001083 # gmp is overeager with detecting 64bit CPUs even if they run
1084 # a 32bit kernel and userland.
1085 if [ "$(uname -m 2>/dev/null)" = "i686" ]; then
1086 OPTIONS="ABI=32"
1087 fi
Martin Roth987d42d2018-07-22 11:45:29 -06001088elif [ "$UNAME" = "NetBSD" ]; then
Nico Huberde45c592016-01-20 23:22:33 +01001089 # same for NetBSD but this one reports an i386
1090 if [ "$(uname -m 2>/dev/null)" = "i386" ]; then
1091 OPTIONS="ABI=32"
1092 fi
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001093fi
Nico Huber66c2c1a2016-09-20 13:09:29 +02001094if [ -z "${LANGUAGES}" ]; then
Nico Hubercd87e1b2017-06-24 20:35:59 +02001095 if have_gnat; then
Nico Huber66c2c1a2016-09-20 13:09:29 +02001096 printf "\nFound compatible Ada compiler, enabling Ada support by default.\n\n"
1097 LANGUAGES="ada,${DEFAULT_LANGUAGES}"
1098 else
Nico Hubercd87e1b2017-06-24 20:35:59 +02001099 printf "\n${red}WARNING${NC}\n"
1100 printf "No compatible Ada compiler (GNAT) found. You can continue without\n"
1101 printf "Ada support, but this will limit the features of ${blue}coreboot${NC} (e.g.\n"
1102 printf "native graphics initialization won't be available on most Intel\n"
1103 printf "boards).\n\n"
1104
1105 printf "Usually, you can install GNAT with your package management system\n"
1106 printf "(the package is called \`gnat\` or \`gcc-ada\`). It has to match the\n"
1107 printf "\`gcc\` package in version. If there are multiple versions of GCC in-\n"
1108 printf "stalled, you can point this script to the matching version through\n"
1109 printf "the \`CC\` and \`CXX\` environment variables.\n\n"
1110
1111 printf "e.g. on Ubuntu 14.04, if \`gcc\` is \`gcc-4.8\`:\n"
1112 printf " apt-get install gnat-4.8 && make crossgcc\n\n"
1113
1114 printf "on Ubuntu 16.04, if \`gcc\` is \`gcc-5\`:\n"
1115 printf " apt-get install gnat-5 && make crossgcc\n"
1116 timeout 30
Nico Huber66c2c1a2016-09-20 13:09:29 +02001117 LANGUAGES="${DEFAULT_LANGUAGES}"
1118 fi
1119fi
Patrick Georgif2741aa2020-07-06 16:35:56 +02001120if [ "$BOOTSTRAP" != 1 ] && \
1121 { [ "$(hostcc_major)" -lt 4 ] || \
1122 { [ "$(hostcc_major)" -eq 4 ] && \
1123 [ "$(hostcc_minor)" -lt 9 ] ; } ; }
1124then
1125 printf "\n${red}WARNING${NC}\n"
1126 printf "Building coreboot requires a host compiler newer than 4.9.x while\n"
1127 printf "yours is $(hostcc_version).\n"
1128 printf "Enabling bootstrapping to provide a sufficiently new compiler:\n"
1129 printf "This will take significantly longer than a usual build.\n"
1130 printf "Alternatively you can abort now and update your host compiler.\n"
1131 timeout 15
1132 BOOTSTRAP=1
1133fi
Nico Hubercd87e1b2017-06-24 20:35:59 +02001134if ada_requested; then
Patrick Georgif2741aa2020-07-06 16:35:56 +02001135 if ! have_gnat; then
Nico Hubercd87e1b2017-06-24 20:35:59 +02001136 please_install gnat gcc-ada
1137 exit 1
1138 fi
Nico Hubercd87e1b2017-06-24 20:35:59 +02001139fi
Stefan Reinauer14ce2132015-06-05 13:01:13 -07001140fi # GCC
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001141
Nico Huber5f72f962016-01-31 23:05:19 +01001142export HOSTCFLAGS="-Os"
1143if have_hostcflags_from_gmp; then
1144 set_hostcflags_from_gmp
1145fi
1146
Patrick Georgic1a75b12011-11-04 21:37:14 +01001147if [ "$USECCACHE" = 1 ]; then
1148 CC="ccache $CC"
1149fi
1150
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001151# Prepare target directory for building GCC
1152# (dependencies must be in the PATH)
Martin Roth987d42d2018-07-22 11:45:29 -06001153mkdir -p "$DESTDIR$TARGETDIR/bin"
1154mkdir -p "$DESTDIR$TARGETDIR/share"
Stefan Reinauer16bd7892012-12-07 23:57:01 +01001155export PATH=$DESTDIR$TARGETDIR/bin:$PATH
1156
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001157# Download, unpack, patch and build all packages
1158
Idwer Vollering458e7df2020-09-25 12:26:51 +02001159printf "Downloading and verifying tarballs ...\n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001160mkdir -p tarballs
1161for P in $PACKAGES; do
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +02001162 download "$P" || exit "$?"
1163 verify_hash "$P" || exit "$?"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001164done
Martin Roth03625172016-02-17 14:44:14 -07001165printf "Downloaded tarballs ... ${green}ok${NC}\n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001166
Elyes HAOUASb0f19882018-06-09 11:59:00 +02001167printf "Unpacking and patching ...\n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001168for P in $PACKAGES; do
1169 unpack_and_patch $P || exit 1
1170done
1171printf "Unpacked and patched ... ${green}ok${NC}\n"
1172
Patrick Georgi9681cdc2017-09-22 18:57:10 +02001173if [ -n "$BOOTSTRAPONLY" ]; then
Elyes HAOUASb0f19882018-06-09 11:59:00 +02001174 printf "Building bootstrap compiler only ...\n"
Nico Huberafda56e2017-12-07 17:50:53 +01001175 for pkg in GMP MPFR MPC GCC; do
Patrick Georgi9681cdc2017-09-22 18:57:10 +02001176 build_for_host $pkg
1177 done
1178 exit 0
1179fi
1180
Elyes HAOUASb0f19882018-06-09 11:59:00 +02001181printf "Building packages ...\n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -07001182for package in $PACKAGES; do
1183 build $package
1184done
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001185printf "Packages built ... ${green}ok${NC}\n"
Stefan Reinauer14ce2132015-06-05 13:01:13 -07001186
Stefan Reinauer699ac272015-06-05 12:43:28 -07001187# Adding git information of current tree to target directory
1188# for reproducibility
Stefan Reinauerd7649122015-06-09 11:44:25 -07001189PROGNAME=$(basename "$0")
Stefan Reinauer88352d72016-04-06 15:57:03 -07001190rm -f "$DESTDIR$TARGETDIR/share/$PROGNAME-*"
Felix Singer62fcffb2021-10-19 13:25:18 +02001191cp "$PROGNAME" "$DESTDIR$TARGETDIR/share/$PROGNAME-$CROSSGCC_VERSION"
Zheng Bao30b895f2013-02-06 18:04:40 +08001192
Patrick Georgi0c2df6f2017-11-22 16:14:09 -05001193# Adding edk2 tools template
1194mkdir -p "$DESTDIR$TARGETDIR/share/edk2config"
1195sed -e "s,@@PREFIX@@,$TARGETDIR,g" edk2tools.txt > "$DESTDIR$TARGETDIR/share/edk2config/tools_def.txt"
1196printf "Copied EDK2 tools template ... ${green}ok${NC}\n"
1197
Stefan Reinauer88e83e52016-04-06 15:39:48 -07001198cleanup
Stefan Reinauer074d9132009-09-26 16:43:17 +00001199
Stefan Reinauer81693d42017-06-21 15:25:36 -07001200printf "\n${green}You can now run $NAME from $TARGETDIR.${NC}\n"