blob: ee458def042c19a7b3d8403de28b5df8d0c37dd5 [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
Martin Roth3b32af92022-11-27 12:55:31 -070033USE_COREBOOT_MIRROR=0
34COREBOOT_MIRROR_URL="https://www.coreboot.org/releases/crossgcc-sources"
Stefan Reinauer074d9132009-09-26 16:43:17 +000035
Stefan Reinauer699ac272015-06-05 12:43:28 -070036# GCC toolchain version numbers
Patrick Georgi0afb90a2021-05-10 23:34:18 +020037GMP_VERSION=6.2.1
Elyes Haouas2c1511a2023-01-06 21:38:23 +010038MPFR_VERSION=4.2.0
Elyes Haouasd15a9f92022-12-10 06:00:08 +010039MPC_VERSION=1.3.1
Felix Singer36847972022-12-01 11:36:17 +000040GCC_VERSION=11.3.0
Felix Singer0686c692022-08-20 04:30:17 +020041BINUTILS_VERSION=2.40
Felix Singer90753392023-07-10 23:56:18 +000042IASL_VERSION="R06_28_23"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070043# CLANG version number
Felix Singer2b4d2ed2023-06-23 21:11:06 +000044CLANG_VERSION=16.0.6
Elyes Haouasdea0f212023-06-03 09:51:40 +020045CMAKE_VERSION=3.26.4
Elyes Haouasa84823d2023-03-16 07:07:02 +010046NASM_VERSION=2.16.01
Stefan Reinauer074d9132009-09-26 16:43:17 +000047
Martin Roth3b32af92022-11-27 12:55:31 -070048# Filename for each package
49GMP_ARCHIVE="gmp-${GMP_VERSION}.tar.xz"
50MPFR_ARCHIVE="mpfr-${MPFR_VERSION}.tar.xz"
51MPC_ARCHIVE="mpc-${MPC_VERSION}.tar.gz"
52GCC_ARCHIVE="gcc-${GCC_VERSION}.tar.xz"
53BINUTILS_ARCHIVE="binutils-${BINUTILS_VERSION}.tar.xz"
Felix Singer90753392023-07-10 23:56:18 +000054IASL_ARCHIVE="${IASL_VERSION}.tar.gz"
Martin Roth3b32af92022-11-27 12:55:31 -070055# CLANG toolchain FILE locations
56LLVM_ARCHIVE="llvm-${CLANG_VERSION}.src.tar.xz"
57CLANG_ARCHIVE="clang-${CLANG_VERSION}.src.tar.xz"
58CRT_ARCHIVE="compiler-rt-${CLANG_VERSION}.src.tar.xz"
59CTE_ARCHIVE="clang-tools-extra-${CLANG_VERSION}.src.tar.xz"
60LLVMCMAKE_ARCHIVE="cmake-${CLANG_VERSION}.src.tar.xz"
61CMAKE_ARCHIVE="cmake-${CMAKE_VERSION}.tar.gz"
62NASM_ARCHIVE="nasm-${NASM_VERSION}.tar.bz2"
63
64# These URLs are sanitized by the jenkins toolchain test builder, so if
Martin Roth1484c032016-03-08 09:37:14 -070065# a completely new URL is added here, it probably needs to be added
66# to the jenkins build as well, or the builder won't download it.
Stefan Reinauer074d9132009-09-26 16:43:17 +000067
Martin Roth3b32af92022-11-27 12:55:31 -070068# GCC toolchain archive locations
69GMP_BASE_URL="https://ftpmirror.gnu.org/gmp"
70MPFR_BASE_URL="https://ftpmirror.gnu.org/mpfr"
71MPC_BASE_URL="https://ftpmirror.gnu.org/mpc"
72GCC_BASE_URL="https://ftpmirror.gnu.org/gcc/gcc-${GCC_VERSION}"
73BINUTILS_BASE_URL="https://ftpmirror.gnu.org/binutils"
Felix Singer90753392023-07-10 23:56:18 +000074IASL_BASE_URL="https://github.com/acpica/acpica/archive/refs/tags"
Martin Roth3b32af92022-11-27 12:55:31 -070075# CLANG toolchain archive locations
76LLVM_BASE_URL="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}"
77CLANG_BASE_URL="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}"
78CRT_BASE_URL="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}"
79CTE_BASE_URL="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}"
80LLVMCMAKE_BASE_URL="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}"
81CMAKE_BASE_URL="https://cmake.org/files/v${CMAKE_VERSION%.*}"
82NASM_BASE_URL="https://www.nasm.us/pub/nasm/releasebuilds/${NASM_VERSION}"
83
84ALL_ARCHIVES="$GMP_BASE_URL/$GMP_ARCHIVE $MPFR_BASE_URL/$MPFR_ARCHIVE $MPC_BASE_URL/$MPC_ARCHIVE \
85 $GCC_BASE_URL/$GCC_ARCHIVE $BINUTILS_BASE_URL/$BINUTILS_ARCHIVE $IASL_BASE_URL/$IASL_ARCHIVE \
86 $LLVM_BASE_URL/$LLVM_ARCHIVE $CLANG_BASE_URL/$CLANG_ARCHIVE $LLVMCMAKE_BASE_URL/$LLVMCMAKE_ARCHIVE \
87 $CRT_BASE_URL/$CRT_ARCHIVE $CTE_BASE_URL/$CTE_ARCHIVE $CMAKE_BASE_URL/$CMAKE_ARCHIVE $NASM_BASE_URL/$NASM_ARCHIVE"
Martin Roth58c68d52016-03-01 15:22:42 -070088
Stefan Reinauer699ac272015-06-05 12:43:28 -070089# GCC toolchain directories
Stefan Reinauer074d9132009-09-26 16:43:17 +000090GMP_DIR="gmp-${GMP_VERSION}"
91MPFR_DIR="mpfr-${MPFR_VERSION}"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000092MPC_DIR="mpc-${MPC_VERSION}"
Martin Roth987d42d2018-07-22 11:45:29 -060093# shellcheck disable=SC2034
Stefan Reinauer074d9132009-09-26 16:43:17 +000094GCC_DIR="gcc-${GCC_VERSION}"
Martin Roth987d42d2018-07-22 11:45:29 -060095# shellcheck disable=SC2034
Stefan Reinauer074d9132009-09-26 16:43:17 +000096BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
Felix Singer90753392023-07-10 23:56:18 +000097IASL_DIR="acpica-${IASL_VERSION}"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070098# CLANG toolchain directories
99LLVM_DIR="llvm-${CLANG_VERSION}.src"
Elyes HAOUAS8f1853c2020-08-02 09:46:15 +0200100CLANG_DIR="clang-${CLANG_VERSION}.src"
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700101CRT_DIR="compiler-rt-${CLANG_VERSION}.src"
102CTE_DIR="clang-tools-extra-${CLANG_VERSION}.src"
Elyes Haouasc8870b12022-09-16 12:10:00 +0200103LLVMCMAKE_DIR="cmake-${CLANG_VERSION}.src"
Stefan Reinauer3b593492017-06-19 17:28:54 -0700104CMAKE_DIR="cmake-${CMAKE_VERSION}"
Martin Rothd70f5fa2019-05-26 17:24:19 -0600105NASM_DIR="nasm-${NASM_VERSION}"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000106
Patrick Georgi6321f522015-02-27 23:36:26 +0100107unset MAKELEVEL MAKEFLAGS
108
Patrick Georgi1861ff72011-10-31 12:15:55 +0100109red='\033[0;31m'
110RED='\033[1;31m'
111green='\033[0;32m'
112GREEN='\033[1;32m'
113blue='\033[0;34m'
Patrick Georgi1861ff72011-10-31 12:15:55 +0100114CYAN='\033[1;36m'
115NC='\033[0m' # No Color
Stefan Reinauer074d9132009-09-26 16:43:17 +0000116
Martin Roth987d42d2018-07-22 11:45:29 -0600117UNAME=$(if uname | grep -iq cygwin; then echo Cygwin; else uname; fi)
Martin Roth88463822016-01-07 11:03:36 -0700118HALT_FOR_TOOLS=0
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700119
Nico Huber08bb8372017-06-24 19:50:35 +0200120hostcc()
121{
122 # $1 "host" or "target"
Martin Roth987d42d2018-07-22 11:45:29 -0600123 if [ "$BOOTSTRAP" = 1 ] && [ "$1" = target ]; then
124 echo "$DESTDIR$TARGETDIR/bin/gcc"
Nico Huber08bb8372017-06-24 19:50:35 +0200125 else
Martin Roth987d42d2018-07-22 11:45:29 -0600126 echo "$CC"
Nico Huber08bb8372017-06-24 19:50:35 +0200127 fi
128}
129
130hostcxx()
131{
132 # $1 "host" or "target"
Martin Roth987d42d2018-07-22 11:45:29 -0600133 if [ "$BOOTSTRAP" = 1 ] && [ "$1" = target ]; then
134 echo "$DESTDIR$TARGETDIR/bin/g++"
Nico Huber08bb8372017-06-24 19:50:35 +0200135 else
Martin Roth987d42d2018-07-22 11:45:29 -0600136 echo "$CXX"
Nico Huber08bb8372017-06-24 19:50:35 +0200137 fi
138}
139
Patrick Georgi198d23c2012-12-08 08:02:44 +0100140normalize_dirs()
141{
Martin Roth987d42d2018-07-22 11:45:29 -0600142 mkdir -p "$DESTDIR$TARGETDIR/lib"
143 test -d "$DESTDIR$TARGETDIR/lib32" && mv "$DESTDIR$TARGETDIR"/lib32/* "$DESTDIR$TARGETDIR/lib"
144 test -d "$DESTDIR$TARGETDIR/lib64" && mv "$DESTDIR$TARGETDIR"/lib64/* "$DESTDIR$TARGETDIR/lib"
Martin Roth1039d272022-01-31 10:17:43 -0700145 rm -rf "$DESTDIR$TARGETDIR/lib32" "$DESTDIR$TARGETDIR/lib64"
Patrick Georgi198d23c2012-12-08 08:02:44 +0100146
Martin Roth987d42d2018-07-22 11:45:29 -0600147 perl -pi -e "s,/lib32,/lib," "$DESTDIR$TARGETDIR"/lib/*.la
148 perl -pi -e "s,/lib64,/lib," "$DESTDIR$TARGETDIR"/lib/*.la
Patrick Georgi198d23c2012-12-08 08:02:44 +0100149}
150
Nico Hubercd87e1b2017-06-24 20:35:59 +0200151countdown()
152{
153 tout=${1:-10}
154
Martin Roth987d42d2018-07-22 11:45:29 -0600155 printf "\nPress Ctrl-C to abort, Enter to continue... %2ds" "$tout"
156 while [ "$tout" -gt 0 ]; do
Nico Hubercd87e1b2017-06-24 20:35:59 +0200157 sleep 1
158 tout=$((tout - 1))
159 printf "\b\b\b%2ds" $tout
160 done
161 printf "\n"
162}
163
164timeout()
165{
166 tout=${1:-10}
167
168 # Ignore SIGUSR1, should interrupt `read` though.
169 trap false USR1
170 # Clean up in case the user aborts.
171 trap 'kill $counter > /dev/null 2>&1' EXIT
172
Martin Roth987d42d2018-07-22 11:45:29 -0600173 (countdown "$tout"; kill -USR1 $$)&
Nico Hubercd87e1b2017-06-24 20:35:59 +0200174 counter=$!
175
176 # Some shells with sh compatibility mode (e.g. zsh, mksh) only
177 # let us interrupt `read` if a non-standard -t parameter is given.
Martin Roth987d42d2018-07-22 11:45:29 -0600178 # shellcheck disable=SC2034,SC2039,SC2162
Nico Hubercd87e1b2017-06-24 20:35:59 +0200179 if echo | read -t 1 foo 2>/dev/null; then
180 read -t $((tout + 1)) foo
181 else
182 read foo
183 fi
184
185 kill $counter > /dev/null 2>&1
186 trap - USR1 EXIT
187}
188
Stefan Reinauer93a96302015-06-15 12:36:53 -0700189please_install()
190{
Martin Roth88463822016-01-07 11:03:36 -0700191 HALT_FOR_TOOLS=1
Martin Roth987d42d2018-07-22 11:45:29 -0600192 # shellcheck disable=SC1091
Patrick Georgi5da95dc2015-07-17 23:33:05 +0200193 test -r /etc/os-release && . /etc/os-release
Alex Thiessen72d10892018-01-12 01:45:53 +0000194 # vanilla debian doesn't define `ID_LIKE`, just `ID`
195 if [ -z "${ID_LIKE}" ] && [ -n "${ID}" ]; then
196 ID_LIKE=${ID}
197 fi
Stefan Reinauer93a96302015-06-15 12:36:53 -0700198 case "$ID_LIKE" in
199 debian) solution="sudo apt-get install $1" ;;
200 suse) solution="sudo zypper install $1" ;;
201 *) solution="using your OS packaging system" ;;
202 esac
203
Patrick Georgidf1ff232017-01-07 09:28:43 +0100204 printf "${RED}ERROR:${red} Missing tool: Please install '$1'. (eg $solution)${NC}\n" >&2
Martin Rothe3963172016-01-06 13:54:32 -0700205 if [ -n "$2" ]; then
Patrick Georgidf1ff232017-01-07 09:28:43 +0100206 printf "${RED}ERROR:${red} or install '$2'.${NC}\n" >&2
Martin Rothe3963172016-01-06 13:54:32 -0700207 fi
Stefan Reinauer93a96302015-06-15 12:36:53 -0700208}
209
Patrick Georgib7062882015-02-24 10:52:14 +0100210searchtool()
Stefan Reinauer074d9132009-09-26 16:43:17 +0000211{
212 # $1 short name
Patrick Georgib7062882015-02-24 10:52:14 +0100213 # $2 search string
214 # $3 soft fail if set
Martin Rothe3963172016-01-06 13:54:32 -0700215 # $4 alternative package to install on failure
Patrick Georgib7062882015-02-24 10:52:14 +0100216 # result: file name of that tool on stdout
217 # or no output if nothing suitable was found
218 search=GNU
219 if [ -n "$2" ]; then
220 search="$2"
221 fi
Stefan Reinauer074d9132009-09-26 16:43:17 +0000222 for i in "$1" "g$1" "gnu$1"; do
Martin Roth987d42d2018-07-22 11:45:29 -0600223 if [ -x "$(command -v "$i" 2>/dev/null)" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700224 if [ "$(cat /dev/null | $i --version 2>&1 | grep -c "$search")" \
225 -gt 0 ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600226 echo "$i"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000227 return
228 fi
229 fi
230 done
Andrew Wub67e9a12014-04-28 18:13:44 +0800231 # A workaround for OSX 10.9 and some BSDs, whose nongnu
232 # patch and tar also work.
Martin Roth987d42d2018-07-22 11:45:29 -0600233 if [ "$UNAME" = "Darwin" ] || [ "$UNAME" = "FreeBSD" ] || [ "$UNAME" = "NetBSD" ] || [ "$UNAME" = "OpenBSD" ]; then
234 if [ "$1" = "patch" ] || [ "$1" = "tar" ]; then
235 if [ -x "$(command -v "$1" 2>/dev/null)" ]; then
236 echo "$1"
Zheng Bao36156ff2012-09-28 16:18:58 +0800237 return
238 fi
239 fi
240 fi
Martin Roth987d42d2018-07-22 11:45:29 -0600241 if echo "$1" | grep -q "sum" ; then
242 algor=$(echo "$1" | sed -e 's,sum,,')
243 if [ -x "$(command -v "$1" 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400244 #xxxsum [file]
Martin Roth987d42d2018-07-22 11:45:29 -0600245 echo "$1"
zbao5cf758d2015-09-01 22:28:57 -0400246 return
Martin Roth987d42d2018-07-22 11:45:29 -0600247 elif [ -x "$(command -v "$algor" 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400248 #xxx [file]
Martin Roth987d42d2018-07-22 11:45:29 -0600249 echo "$algor"
zbao5cf758d2015-09-01 22:28:57 -0400250 return
Martin Rothd55f5eb2017-06-15 11:37:26 -0600251 elif [ -x "$(command -v openssl 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400252 #openssl xxx [file]
Martin Roth987d42d2018-07-22 11:45:29 -0600253 echo openssl "$algor"
zbao5cf758d2015-09-01 22:28:57 -0400254 return
Martin Rothd55f5eb2017-06-15 11:37:26 -0600255 elif [ -x "$(command -v cksum 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400256 #cksum -a xxx [file]
257 #cksum has special options in NetBSD. Actually, NetBSD will use the second case above.
Martin Roth987d42d2018-07-22 11:45:29 -0600258 echo "buildgcc" | cksum -a "$algor" > /dev/null 2>/dev/null && \
259 echo cksum -a "$algor"
zbao5cf758d2015-09-01 22:28:57 -0400260 return
zbao939dc842015-04-30 04:44:07 +0800261 fi
262 fi
Martin Rothe3963172016-01-06 13:54:32 -0700263
Martin Roth987d42d2018-07-22 11:45:29 -0600264 [ -z "$3" ] && please_install "$1" "$4"
Patrick Georgib7062882015-02-24 10:52:14 +0100265 false
Stefan Reinauer074d9132009-09-26 16:43:17 +0000266}
267
Martin Roth591790f2016-03-20 20:39:04 -0600268# Run a compile check of the specified library option to see if it's installed
269check_for_library() {
Martin Roth987d42d2018-07-22 11:45:29 -0600270 LIBRARY_FLAGS="$1"
271 LIBRARY_PACKAGES="$2"
272 LIBTEST_FILE=.libtest
Martin Roth591790f2016-03-20 20:39:04 -0600273
274 echo "int main(int argc, char **argv) { (void) argc; (void) argv; return 0; }" > "${LIBTEST_FILE}.c"
275
Martin Roth987d42d2018-07-22 11:45:29 -0600276 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200277 "$CC" $CFLAGS $LIBRARY_FLAGS "${LIBTEST_FILE}.c" -o "${LIBTEST_FILE}" >/dev/null 2>&1 || \
Martin Roth591790f2016-03-20 20:39:04 -0600278 please_install "$LIBRARY_PACKAGES"
279 rm -rf "${LIBTEST_FILE}.c" "${LIBTEST_FILE}"
280}
281
Nico Huberaee7f462016-09-20 12:55:19 +0200282buildcc_major() {
283 echo "${GCC_VERSION}" | cut -d. -f1
284}
285
Nico Huber26267a72016-12-04 02:45:58 +0100286buildcc_minor() {
287 echo "${GCC_VERSION}" | cut -d. -f2
288}
289
Nico Huberaee7f462016-09-20 12:55:19 +0200290buildcc_version() {
291 echo "${GCC_VERSION}" | cut -d. -f1-2
292}
293
294hostcc_major() {
295 (echo __GNUC__ | ${CC} -E - 2>/dev/null || echo 0) | tail -1
296}
297
298hostcc_minor() {
299 (echo __GNUC_MINOR__ | ${CC} -E - 2>/dev/null || echo 0) | tail -1
300}
301
302hostcc_version() {
303 printf "%d.%d" "$(hostcc_major)" "$(hostcc_minor)"
304}
305
Nico Huber75797162016-09-20 13:01:57 +0200306hostcc_has_gnat1() {
307 [ -x "$(${CC} -print-prog-name=gnat1)" ]
308}
309
Nico Hubercd87e1b2017-06-24 20:35:59 +0200310have_gnat() {
311 hostcc_has_gnat1 && \
312 searchtool gnatbind "Free Software Foundation" nofail > /dev/null
313}
314
Nico Huber75797162016-09-20 13:01:57 +0200315ada_requested() {
316 echo "${LANGUAGES}" | grep -q '\<ada\>'
317}
318
Stefan Reinauer88935482015-06-05 09:51:10 -0700319download() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700320 package=$1
Martin Roth987d42d2018-07-22 11:45:29 -0600321 # shellcheck disable=SC2086
Martin Roth3b32af92022-11-27 12:55:31 -0700322 if [ "${USE_COREBOOT_MIRROR}" -eq 0 ]; then
323 url="$(eval echo \$$package"_BASE_URL")"
324 else
325 url="${COREBOOT_MIRROR_URL}"
326 fi
Stefan Reinauer699ac272015-06-05 12:43:28 -0700327
Martin Roth3b32af92022-11-27 12:55:31 -0700328 file="$(eval echo \$$package"_ARCHIVE")"
329 printf " * ${file} "
Stefan Reinauer88935482015-06-05 09:51:10 -0700330
Martin Roth3b32af92022-11-27 12:55:31 -0700331 if test -f "tarballs/${file}"; then
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200332 printf "(cached)... "
zbaod22b9ea2015-08-30 23:35:16 -0400333 else
Martin Roth3b32af92022-11-27 12:55:31 -0700334 printf "(downloading from ${url}/${file})"
335 rm -f "tarballs/${file}"
Martin Roth987d42d2018-07-22 11:45:29 -0600336 cd tarballs || exit 1
Martin Roth3b32af92022-11-27 12:55:31 -0700337 download_showing_percentage "${url}/${file}"
Stefan Reinauer88935482015-06-05 09:51:10 -0700338 cd ..
zbaod22b9ea2015-08-30 23:35:16 -0400339 fi
340
Martin Roth3b32af92022-11-27 12:55:31 -0700341 if [ ! -f "tarballs/${file}" ]; then
342 printf "${RED}Failed to download ${file}.${NC}\n"
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700343 exit 1
344 fi
Stefan Reinauer88935482015-06-05 09:51:10 -0700345}
346
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200347# Compute the hash of the package given in $1, and print it raw (just the
348# hexadecimal hash).
349compute_hash() {
350 package=$1
Martin Roth987d42d2018-07-22 11:45:29 -0600351 # shellcheck disable=SC2086
Martin Roth3b32af92022-11-27 12:55:31 -0700352 file="$(eval echo \$$package"_ARCHIVE")"
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200353
354 if test -z "$CHECKSUM"; then
355 echo "${RED}\$CHECKSUM program missing. This is bad.${NC}" 1>&2
356 exit 1
357 fi
358
359 $CHECKSUM "tarballs/$file" 2>/dev/null | sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@'
360}
361
362error_hash_missing() {
363 package="$1"
Martin Roth987d42d2018-07-22 11:45:29 -0600364 # shellcheck disable=SC2086
Martin Roth3b32af92022-11-27 12:55:31 -0700365 file="$(eval echo \$$package"_ARCHIVE")"
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200366
367 fullhashfile="util/crossgcc/sum/$file.cksum"
368 printf "${RED}hash file missing:${NC}\n\n" 1>&2
369 printf "Please verify util/crossgcc/tarball/$file carefully\n" 1>&2
370 printf "(using PGP if possible), and then rename\n" 1>&2
371 printf " ${CYAN}${fullhashfile}.calc${NC}\n" 1>&2
372 printf " to ${CYAN}${fullhashfile}${NC}\n\n" 1>&2
373
374 exit 1
375}
376
377# Read the known hash file of the package given in $1, and print it raw.
378get_known_hash() {
379 package=$1
Martin Roth987d42d2018-07-22 11:45:29 -0600380 # shellcheck disable=SC2086
Martin Roth3b32af92022-11-27 12:55:31 -0700381 file="$(eval echo \$$package"_ARCHIVE")"
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200382 hashfile="sum/$file.cksum"
383
384 if [ ! -f "$hashfile" ]; then
385 calc_hash="$(compute_hash "$package")" || exit 1
386 echo "$calc_hash tarballs/$file" > "${hashfile}.calc"
387
388 error_hash_missing "$package"
389 exit 1
390 fi
391
Martin Roth987d42d2018-07-22 11:45:29 -0600392 sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@' < "$hashfile"
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200393}
394
395error_hash_mismatch() {
396 package=$1
397 known_hash="$2"
398 computed_hash="$3"
Martin Roth987d42d2018-07-22 11:45:29 -0600399 # shellcheck disable=SC2086
Martin Roth3b32af92022-11-27 12:55:31 -0700400 file="$(eval echo \$$package"_ARCHIVE")"
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200401
402 printf "${RED}hash mismatch:${NC}\n\n"
403 printf " expected (known) hash: $known_hash\n"
404 printf "calculated hash of downloaded file: $computed_hash\n\n"
405
406 printf "If you think this is due to a network error, please delete\n"
407 printf " ${CYAN}util/crossgcc/tarballs/$file${NC}\n"
408 printf "and try again. If the problem persists, it may be due to an\n"
409 printf "administration error on the file server, or you might be\n"
410 printf "subject to a Man-in-the-Middle attack\n\n"
411
412 exit 1
413}
414
415# verify_hash - Check that the hash of the file given in $1 matches the known
416# hash; Bail out on mismatch or missing hash file.
417verify_hash() {
418 package=$1
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200419
420 known_hash="$(get_known_hash "$package")" || exit "$?"
421 computed_hash="$(compute_hash "$package")" || exit "$?"
422
423 if [ "$known_hash" != "$computed_hash" ]; then
424 error_hash_mismatch "$package" "$known_hash" "$computed_hash"
425 exit 1
426 fi
427
Jonathan Neuschäfercb76069e2018-10-01 09:45:49 +0200428 printf "${GREEN}hash verified (${known_hash})${NC}\n"
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200429}
430
Stefan Reinauer88935482015-06-05 09:51:10 -0700431unpack_and_patch() {
Martin Roth987d42d2018-07-22 11:45:29 -0600432 package="$1"
433 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700434 archive="$(eval echo \$$package"_ARCHIVE")"
Martin Roth987d42d2018-07-22 11:45:29 -0600435 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700436 dir="$(eval echo \$$package"_DIR")"
Martin Roth987d42d2018-07-22 11:45:29 -0600437 test -d "${dir}" && test -f "${dir}/.unpack_success" || (
Martin Roth3b32af92022-11-27 12:55:31 -0700438 printf " * "$archive"\n"
Stefan Reinauer88935482015-06-05 09:51:10 -0700439 FLAGS=zxf
Martin Roth987d42d2018-07-22 11:45:29 -0600440 suffix=$(echo "$archive" | sed 's,.*\.,,')
Martin Roth95f7b222016-03-20 12:38:48 -0600441 if [ "$suffix" = "gz" ] && [ -n "$PIGZ" ]; then FLAGS="-I pigz -xf"
442 elif [ "$suffix" = "gz" ]; then FLAGS=zxf
443 elif [ "$suffix" = "bz2" ] && [ -n "$LBZIP2" ]; then FLAGS="-I lbzip2 -xf"
444 elif [ "$suffix" = "bz2" ]; then FLAGS=jxf
445 elif [ "$suffix" = "xz" ]; then FLAGS="--xz -xf"
446 elif [ "$suffix" = "lzma" ]; then FLAGS="--lzma -xf"
447 fi
Martin Roth987d42d2018-07-22 11:45:29 -0600448 # shellcheck disable=SC2086
Martin Roth3b32af92022-11-27 12:55:31 -0700449 $TAR $FLAGS "tarballs/$archive"
Stefan Reinauer88935482015-06-05 09:51:10 -0700450 for patch in patches/${dir}_*.patch; do
Martin Roth987d42d2018-07-22 11:45:29 -0600451 test -r "$patch" || continue
452 printf " o $(basename "$patch")\n"
453 (cd "${dir}" || exit 1; $PATCH -s -N -p1 <"../${patch}") || {
Stefan Reinauer88935482015-06-05 09:51:10 -0700454 printf "\n${RED}Failed $patch.${NC}\n"
Stefan Reinauer916b8452015-06-09 11:54:49 -0700455 exit 1
456 }
Stefan Reinauer88935482015-06-05 09:51:10 -0700457 done
Martin Roth987d42d2018-07-22 11:45:29 -0600458 touch "${dir}/.unpack_success"
Stefan Reinauer88935482015-06-05 09:51:10 -0700459 )
460}
461
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700462fn_exists()
463{
Martin Roth987d42d2018-07-22 11:45:29 -0600464 # shellcheck disable=SC2039
465 type "$1" >/dev/null 2>&1
Patrick Georgi74c06422015-03-25 18:40:13 +0100466}
467
Patrick Georgif2c15f52015-06-11 21:07:31 +0200468is_package_enabled()
469{
470 echo "$PACKAGES" |grep -q "\<$1\>"
471}
472
Martin Roth444ece22016-04-01 18:46:29 -0600473package_uses_targetarch()
474{
Patrick Georgif32eed12021-10-16 10:50:00 +0200475 if [ "$1" = "GCC" ] || [ "$1" = "BINUTILS" ]; then
Martin Roth444ece22016-04-01 18:46:29 -0600476 true
477 else
478 false
479 fi
480}
481
Nico Huber11ea2b32016-01-26 16:09:31 +0100482generic_build()
483{
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700484 package=$1
Nico Huber11ea2b32016-01-26 16:09:31 +0100485 host_target=$2
486 builddir=$3
487 success=$4
Martin Rothc295d5e2016-11-14 11:56:11 -0700488 version=$5
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700489
Martin Roth987d42d2018-07-22 11:45:29 -0600490 fn_exists "build_$package" || return
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700491
Nico Huber11ea2b32016-01-26 16:09:31 +0100492 mkdir -p "$builddir"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700493
Nico Huber11ea2b32016-01-26 16:09:31 +0100494 if [ -f "$success" ]; then
Martin Roth2c6a8062016-11-14 11:58:39 -0700495 printf "Skipping $package v$version for $host_target as it is already built\n"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700496 else
Martin Roth2c6a8062016-11-14 11:58:39 -0700497 printf "Building $package v$version for $host_target ... "
Nico Huber11ea2b32016-01-26 16:09:31 +0100498 DIR="$PWD"
Martin Roth987d42d2018-07-22 11:45:29 -0600499 cd "$builddir" || exit 1
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700500 rm -f .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600501 "build_${package}" "$host_target" > build.log 2>&1
502 cd "$DIR" || exit 1
Nico Huber11ea2b32016-01-26 16:09:31 +0100503 if [ ! -f "$builddir/.failed" ]; then
504 touch "$success";
505 else
Idwer Volleringe2ef3cf2017-08-15 00:53:13 +0200506 printf "${RED}failed${NC}. Check '$builddir/build.log'.\n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700507 exit 1
508 fi
509 printf "${green}ok${NC}\n"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700510 fi
511}
512
Nico Huber11ea2b32016-01-26 16:09:31 +0100513build_for_host()
514{
Martin Rothc295d5e2016-11-14 11:56:11 -0700515 package="$1"
Martin Roth987d42d2018-07-22 11:45:29 -0600516 # shellcheck disable=SC2086
Martin Rothc295d5e2016-11-14 11:56:11 -0700517 version="$(eval echo \$$package"_VERSION")"
Patrick Georgi1b593e52016-11-25 15:27:41 +0100518 generic_build "$package" host "build-$package" "${DESTDIR}${TARGETDIR}/.${package}.${version}.success" "$version"
Nico Huber11ea2b32016-01-26 16:09:31 +0100519}
520
521build_for_target()
522{
Martin Rothc295d5e2016-11-14 11:56:11 -0700523 package="$1"
Martin Roth987d42d2018-07-22 11:45:29 -0600524 # shellcheck disable=SC2086
Martin Rothc295d5e2016-11-14 11:56:11 -0700525 version="$(eval echo \$$package"_VERSION")"
Patrick Georgi1b593e52016-11-25 15:27:41 +0100526 generic_build "$package" target "build-${TARGETARCH}-$package" "${DESTDIR}${TARGETDIR}/.${TARGETARCH}-${package}.${version}.success" "$version"
Nico Huber11ea2b32016-01-26 16:09:31 +0100527}
528
529build()
530{
Martin Roth987d42d2018-07-22 11:45:29 -0600531 if package_uses_targetarch "$1"; then
532 if [ $BOOTSTRAP -eq 1 ] && [ ! -f "${DESTDIR}${TARGETDIR}/.GCC.${GCC_VERSION}.success" ]; then
Nico Huber234d2462016-01-26 16:10:17 +0100533 build_for_host GCC
534 fi
Martin Roth987d42d2018-07-22 11:45:29 -0600535 build_for_target "$1"
Nico Huber11ea2b32016-01-26 16:09:31 +0100536 else
Martin Roth987d42d2018-07-22 11:45:29 -0600537 build_for_host "$1"
Nico Huber11ea2b32016-01-26 16:09:31 +0100538 fi
539}
540
Nico Huber18c74d62017-06-24 20:35:59 +0200541exit_handler()
Zheng Baobb003c82016-08-05 13:41:51 +0800542{
543 printf "${NC}Stop\n"
544 exit 1
545}
546
Stefan Reinauer074d9132009-09-26 16:43:17 +0000547cleanup()
548{
Stefan Reinauer88e83e52016-04-06 15:39:48 -0700549 if [ $SAVETEMPS -ne 0 ]; then
550 printf "Leaving temporary files around... ${green}ok${NC}\n"
551 return
552 fi
553
Stefan Reinauer074d9132009-09-26 16:43:17 +0000554 printf "Cleaning up temporary files... "
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700555 for package in $PACKAGES; do
Martin Roth987d42d2018-07-22 11:45:29 -0600556 # shellcheck disable=SC2086
557 rm -rf "build-${TARGETARCH}-$package" "build-$package" "$(eval echo \$$package"_DIR")"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700558 done
zbaoff3f15c2015-08-26 22:00:31 -0400559 rm -f getopt
Stefan Reinauer074d9132009-09-26 16:43:17 +0000560 printf "${green}ok${NC}\n"
561}
562
563myhelp()
564{
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700565 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>] [-C] [-G] [-S]\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000566 printf " $0 [-V|--version]\n"
567 printf " $0 [-h|--help]\n\n"
568
569 printf "Options:\n"
Felix Singer60c56be2021-10-17 14:54:12 +0200570 printf " [-W|--print-version Print machine readable version\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000571 printf " [-V|--version] print version number and exit\n"
572 printf " [-h|--help] print this help and exit\n"
573 printf " [-c|--clean] remove temporary files before build\n"
574 printf " [-t|--savetemps] don't remove temporary files after build\n"
Patrick Georgic1a75b12011-11-04 21:37:14 +0100575 printf " [-y|--ccache] Use ccache when building cross compiler\n"
Nico Huber152e78e2016-09-20 15:38:40 +0200576 printf " [-n|--nocolor] don't print color codes in output\n"
577 printf " [-u|--urls] print the urls for all packages\n"
Patrick Georgi73166c72010-04-14 20:42:42 +0000578 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
Martin Roth588c79d2016-03-20 12:03:20 -0600579 printf " [-s]--supported <tool> print supported version of a tool\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000580 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
Martin Roth588c79d2016-03-20 12:03:20 -0600581 printf " (defaults to $TARGETDIR)\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000582 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100583 printf " (for RPM builds, default unset)\n"
Patrick Georgif32eed12021-10-16 10:50:00 +0200584 printf " [-P|--package <package>] Build a specific package: GCC, CLANG, IASL\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700585 printf " (defaults to $PACKAGE)\n"
586 printf "GCC specific options:\n"
Nico Huber234d2462016-01-26 16:10:17 +0100587 printf " [-b|--bootstrap] bootstrap the host compiler before building\n"
588 printf " the cross compiler\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700589 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
590 printf " (defaults to $TARGETARCH)\n"
Nico Huberbb313bf2015-09-08 12:30:27 +0200591 printf " [-l|--languages <languages>] comma separated list of target languages\n"
Nico Huber66c2c1a2016-09-20 13:09:29 +0200592 printf " (defaults to $DEFAULT_LANGUAGES)\n"
Patrick Georgif32eed12021-10-16 10:50:00 +0200593 printf "Platforms for GCC:\n"
Julius Wernerf96d9052019-08-16 15:35:39 -0700594 printf " x86_64 i386-elf i386-mingw32 riscv-elf arm aarch64\n"
Jonathan Neuschäferf14f6402016-03-11 20:22:23 +0100595 printf " powerpc64le-linux-gnu nds32le-elf\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000596}
597
Martin Rothedf965a2015-11-25 16:58:33 -0700598printversion() {
Martin Roth7014f822020-12-21 15:49:33 -0700599 printf "${blue}Welcome to the ${red}coreboot${blue} cross toolchain builder v$CROSSGCC_VERSION ${NC}\n\n"
Martin Rothedf965a2015-11-25 16:58:33 -0700600}
601
Stefan Reinauer074d9132009-09-26 16:43:17 +0000602myversion()
603{
Martin Rothedf965a2015-11-25 16:58:33 -0700604 printversion
Stefan Reinauer074d9132009-09-26 16:43:17 +0000605
606 cat << EOF
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000607Copyright (C) 2008-2010 by coresystems GmbH
Marc Jones2aac3f62011-08-08 16:07:50 -0600608Copyright (C) 2011 by Sage Electronic Engineering
Stefan Reinauer074d9132009-09-26 16:43:17 +0000609
610This program is free software; you can redistribute it and/or modify
611it under the terms of the GNU General Public License as published by
612the Free Software Foundation; version 2 of the License.
613
614This program is distributed in the hope that it will be useful,
615but WITHOUT ANY WARRANTY; without even the implied warranty of
616MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
617GNU General Public License for more details.
618
619EOF
620}
621
Nico Huber5f72f962016-01-31 23:05:19 +0100622have_hostcflags_from_gmp() {
Martin Roth987d42d2018-07-22 11:45:29 -0600623 grep -q __GMP_CFLAGS "$DESTDIR$TARGETDIR/include/gmp.h" >/dev/null 2>&1
Nico Huber5f72f962016-01-31 23:05:19 +0100624}
625
626set_hostcflags_from_gmp() {
627 # Now set CFLAGS to match GMP CFLAGS but strip out -pedantic
628 # as GCC 4.6.x fails if it's there.
Martin Roth987d42d2018-07-22 11:45:29 -0600629 HOSTCFLAGS="$(grep __GMP_CFLAGS "$DESTDIR$TARGETDIR/include/gmp.h" |cut -d\" -f2 |\
Idwer Volleringba349ab2016-07-24 02:10:19 +0200630 sed s,-pedantic,,)"
Martin Roth987d42d2018-07-22 11:45:29 -0600631 export HOSTCFLAGS
Nico Huber5f72f962016-01-31 23:05:19 +0100632}
633
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700634build_GMP() {
Nico Huber8bbd5962016-12-22 16:05:54 +0100635 # Check if GCC enables `-pie` by default (possible since GCC 6).
636 # We need PIC in all static libraries then.
Nico Huber2c1c02e2017-01-02 20:51:28 +0100637 if $CC -dumpspecs 2>/dev/null | grep -q '[{;][[:space:]]*:-pie\>'
Nico Huber8bbd5962016-12-22 16:05:54 +0100638 then
639 OPTIONS="$OPTIONS --with-pic"
640 fi
641
Martin Roth987d42d2018-07-22 11:45:29 -0600642 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200643 CC="$(hostcc host)" CXX="$(hostcxx host)" \
Felix Singerf8157af2022-11-23 10:12:26 +0100644 ../${GMP_DIR}/configure \
645 --disable-shared \
646 --enable-fat \
647 --prefix="$TARGETDIR" \
648 $OPTIONS || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600649 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700650 $MAKE $JOBS || touch .failed
651 $MAKE install DESTDIR=$DESTDIR || touch .failed
652
653 normalize_dirs
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700654
Nico Huber5f72f962016-01-31 23:05:19 +0100655 set_hostcflags_from_gmp
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700656}
657
658build_MPFR() {
Martin Roth987d42d2018-07-22 11:45:29 -0600659 test "$UNAME" = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
Nico Huber08bb8372017-06-24 19:50:35 +0200660 CC="$(hostcc host)" CXX="$(hostcxx host)" \
Felix Singerf8157af2022-11-23 10:12:26 +0100661 ../${MPFR_DIR}/configure \
662 --disable-shared \
663 --prefix="$TARGETDIR" \
Martin Roth987d42d2018-07-22 11:45:29 -0600664 --infodir="$TARGETDIR/info" \
Felix Singerf8157af2022-11-23 10:12:26 +0100665 --with-gmp="$DESTDIR$TARGETDIR" \
666 CFLAGS="$HOSTCFLAGS" || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600667 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700668 $MAKE $JOBS || touch .failed
669 $MAKE install DESTDIR=$DESTDIR || touch .failed
670
671 normalize_dirs
672
673 # work around build problem of libgmp.la
674 if [ "$DESTDIR" != "" ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600675 perl -pi -e "s,$DESTDIR,," "$DESTDIR$TARGETDIR/lib/libgmp.la"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700676 fi
677}
678
679build_MPC() {
Nico Huber08bb8372017-06-24 19:50:35 +0200680 CC="$(hostcc host)" CXX="$(hostcxx host)" \
Felix Singerf8157af2022-11-23 10:12:26 +0100681 ../${MPC_DIR}/configure \
682 --disable-shared \
683 --prefix="$TARGETDIR" \
684 --infodir="$TARGETDIR/info" \
685 --with-mpfr="$DESTDIR$TARGETDIR" \
686 --with-gmp="$DESTDIR$TARGETDIR" \
687 CFLAGS="$HOSTCFLAGS" || touch .failed
Patrick Georgi790aab62017-04-26 17:35:35 +0200688
689 # work around build problem of libmpfr.la
690 if [ "$DESTDIR" != "" ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600691 perl -pi -e "s,$TARGETDIR/lib/libgmp.la,$DESTDIR\$&," "$DESTDIR$TARGETDIR/lib/libmpfr.la"
Patrick Georgi790aab62017-04-26 17:35:35 +0200692 fi
693
Martin Roth987d42d2018-07-22 11:45:29 -0600694 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700695 $MAKE $JOBS || touch .failed
696 $MAKE install DESTDIR=$DESTDIR || touch .failed
697
Patrick Georgi790aab62017-04-26 17:35:35 +0200698 # work around build problem of libmpfr.la
699 if [ "$DESTDIR" != "" ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600700 perl -pi -e "s,$DESTDIR,," "$DESTDIR$TARGETDIR/lib/libmpfr.la"
Patrick Georgi790aab62017-04-26 17:35:35 +0200701 fi
702
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700703 normalize_dirs
704}
705
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700706build_BINUTILS() {
Nico Huberae6187f2016-12-22 22:16:39 +0100707 if [ $TARGETARCH = "x86_64-elf" ]; then
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700708 ADDITIONALTARGET=",i386-elf"
709 fi
Patrick Georgif0d5f672022-09-08 20:25:46 +0200710 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200711 CC="$(hostcc target)" CXX="$(hostcxx target)" \
Felix Singerf8157af2022-11-23 10:12:26 +0100712 ../binutils-${BINUTILS_VERSION}/configure \
713 --prefix="$TARGETDIR" \
714 --target=${TARGETARCH} \
715 --enable-targets=${TARGETARCH}${ADDITIONALTARGET} \
716 --disable-werror \
717 --disable-nls \
718 --enable-lto \
719 --enable-gold \
720 --enable-multilib \
Elias Souza17d9d892023-02-18 12:53:34 -0300721 --disable-docs \
722 --disable-texinfo \
Patrick Georgif0d5f672022-09-08 20:25:46 +0200723 ${BINUTILS_OPTIONS} \
Nico Huber010ecf802016-12-22 16:19:37 +0100724 CFLAGS="$HOSTCFLAGS" \
Felix Singerf8157af2022-11-23 10:12:26 +0100725 CXXFLAGS="$HOSTCFLAGS" || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600726 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700727 $MAKE $JOBS || touch .failed
728 $MAKE install DESTDIR=$DESTDIR || touch .failed
729}
730
Nico Huber234d2462016-01-26 16:10:17 +0100731bootstrap_GCC() {
Martin Roth987d42d2018-07-22 11:45:29 -0600732 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200733 CC="$(hostcc host)" CXX="$(hostcxx host)" \
Nico Huber234d2462016-01-26 16:10:17 +0100734 CFLAGS="$HOSTCFLAGS" \
735 CFLAGS_FOR_BUILD="$HOSTCFLAGS" \
736 CFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
737 CXXFLAGS="$HOSTCFLAGS" \
738 CXXFLAGS_FOR_BUILD="$HOSTCFLAGS" \
739 CXXFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
740 ../gcc-${GCC_VERSION}/configure \
Felix Singerf8157af2022-11-23 10:12:26 +0100741 --prefix="$TARGETDIR" \
742 --libexecdir="$TARGETDIR/lib" \
Nico Huber234d2462016-01-26 16:10:17 +0100743 --enable-bootstrap \
Felix Singerf8157af2022-11-23 10:12:26 +0100744 --disable-werror \
745 --disable-nls \
746 --disable-shared \
747 --disable-multilib \
748 --disable-libssp \
749 --disable-libquadmath \
750 --disable-libcc1 \
Patrick Georgi97a9df42017-04-26 22:18:18 +0200751 --disable-libsanitizer \
Felix Singerf8157af2022-11-23 10:12:26 +0100752 ${GCC_OPTIONS} \
753 --enable-languages="${LANGUAGES}" \
754 --with-gmp="$DESTDIR$TARGETDIR" \
755 --with-mpfr="$DESTDIR$TARGETDIR" \
Martin Roth987d42d2018-07-22 11:45:29 -0600756 --with-mpc="$DESTDIR$TARGETDIR" \
Martin Roth7014f822020-12-21 15:49:33 -0700757 --with-pkgversion="coreboot bootstrap v$CROSSGCC_VERSION" \
Nico Huber234d2462016-01-26 16:10:17 +0100758 && \
Martin Roth987d42d2018-07-22 11:45:29 -0600759 # shellcheck disable=SC2086
Nico Huber234d2462016-01-26 16:10:17 +0100760 $MAKE $JOBS BOOT_CFLAGS="$HOSTCFLAGS" BUILD_CONFIG="" bootstrap && \
761 $MAKE install-gcc \
762 install-target-libgcc \
763 maybe-install-target-libada \
764 maybe-install-target-libstdc++-v3 \
Nico Huber08bb8372017-06-24 19:50:35 +0200765 DESTDIR=$DESTDIR || touch .failed
Nico Huber234d2462016-01-26 16:10:17 +0100766}
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700767
Nico Huber234d2462016-01-26 16:10:17 +0100768build_cross_GCC() {
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700769 # Work around crazy code generator in GCC that confuses CLANG.
Aaron Durbinb2229dc2016-04-07 16:58:10 -0500770 $CC --version | grep clang >/dev/null 2>&1 && \
Patrick Georgia7a5a562017-06-26 07:21:46 +0200771 CLANGFLAGS="-fbracket-depth=1024"
Nico Huber3f12d932017-08-31 23:11:18 +0200772 [ -n "$CXX" ] && $CXX --version | grep clang >/dev/null 2>&1 && \
Patrick Georgia7a5a562017-06-26 07:21:46 +0200773 CLANGCXXFLAGS="-fbracket-depth=1024"
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700774
Stefan Reinauerd0f83722015-06-09 11:52:28 -0700775 # GCC does not honor HOSTCFLAGS at all. CFLAGS are used for
776 # both target and host object files.
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700777 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
778 # but it does not seem to work properly. At least the host library
779 # libiberty is not compiled with CFLAGS_FOR_BUILD.
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700780 # Also set the CXX version of the flags because GCC is now compiled
781 # using C++.
Martin Roth987d42d2018-07-22 11:45:29 -0600782 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200783 CC="$(hostcc target)" CXX="$(hostcxx target)" \
Nico Huber3f12d932017-08-31 23:11:18 +0200784 CFLAGS_FOR_TARGET="-O2 -Dinhibit_libc" \
Patrick Georgia7a5a562017-06-26 07:21:46 +0200785 CFLAGS="$HOSTCFLAGS $CLANGFLAGS" \
786 CFLAGS_FOR_BUILD="$HOSTCFLAGS $CLANGFLAGS" \
787 CXXFLAGS="$HOSTCFLAGS $CLANGCXXFLAGS" \
788 CXXFLAGS_FOR_BUILD="$HOSTCFLAGS $CLANGCXXFLAGS" \
789 ../gcc-${GCC_VERSION}/configure \
Felix Singerf8157af2022-11-23 10:12:26 +0100790 --prefix="$TARGETDIR" \
791 --libexecdir="$TARGETDIR/lib" \
792 --target=${TARGETARCH} \
793 --disable-werror \
794 --disable-shared \
795 --enable-lto \
796 --enable-plugins \
797 --enable-gold \
798 --enable-ld=default \
799 --disable-libssp \
800 --disable-bootstrap \
801 --disable-nls \
802 --disable-libquadmath \
803 --without-headers \
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100804 --disable-threads \
Felix Singerf8157af2022-11-23 10:12:26 +0100805 --enable-interwork \
806 --enable-multilib \
807 --enable-targets=all \
808 --disable-libatomic \
809 --disable-libcc1 \
810 --disable-decimal-float \
811 ${GCC_OPTIONS} \
812 --enable-languages="${LANGUAGES}" \
Ronald G. Minnich7ee16b72016-02-12 21:54:59 +0000813 --with-system-zlib \
Felix Singerf8157af2022-11-23 10:12:26 +0100814 --with-gmp="$DESTDIR$TARGETDIR" \
815 --with-mpfr="$DESTDIR$TARGETDIR" \
Martin Roth987d42d2018-07-22 11:45:29 -0600816 --with-mpc="$DESTDIR$TARGETDIR" \
Felix Singerf8157af2022-11-23 10:12:26 +0100817 --with-gnu-as \
818 --with-gnu-ld \
Martin Roth7014f822020-12-21 15:49:33 -0700819 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION" \
Nico Huber659f40b2018-05-23 17:06:53 +0200820 && \
Patrick Georgi095db332018-06-26 21:00:58 +0200821 mkdir -p gcc/$TARGETARCH && \
Nico Hubere87fcd42020-12-21 00:11:54 +0100822 rm -f "gcc/$TARGETARCH/$GCC_VERSION" && \
Martin Roth987d42d2018-07-22 11:45:29 -0600823 ln -s "$DESTDIR$TARGETDIR/$TARGETARCH/bin" "gcc/$TARGETARCH/$GCC_VERSION" && \
Nico Huber659f40b2018-05-23 17:06:53 +0200824 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-gcc && \
Martin Roth987d42d2018-07-22 11:45:29 -0600825 $MAKE install-gcc DESTDIR="$DESTDIR" || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700826
Martin Roth987d42d2018-07-22 11:45:29 -0600827 if [ ! -f .failed ] && [ "$(echo $TARGETARCH | grep -c -- -mingw32)" -eq 0 ]; then
828 # shellcheck disable=SC2086
Nico Huber659f40b2018-05-23 17:06:53 +0200829 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-target-libgcc && \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700830 $MAKE install-target-libgcc DESTDIR=$DESTDIR || touch .failed
831 fi
832}
833
Nico Huber234d2462016-01-26 16:10:17 +0100834build_GCC() {
835 if [ "$1" = host ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600836 bootstrap_GCC "$1"
Nico Huber234d2462016-01-26 16:10:17 +0100837 else
Martin Roth987d42d2018-07-22 11:45:29 -0600838 build_cross_GCC "$1"
Nico Huber234d2462016-01-26 16:10:17 +0100839 fi
840}
841
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700842build_IASL() {
843 RDIR=$PWD
Martin Roth987d42d2018-07-22 11:45:29 -0600844 cd ../$IASL_DIR/generate/unix || exit 1
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700845 CFLAGS="$HOSTCFLAGS"
846 HOST="_LINUX"
Martin Roth987d42d2018-07-22 11:45:29 -0600847 test "$UNAME" = "Darwin" && HOST="_APPLE"
848 test "$UNAME" = "FreeBSD" && HOST="_FreeBSD"
849 test "$UNAME" = "Cygwin" && HOST="_CYGWIN"
Nico Huber08bb8372017-06-24 19:50:35 +0200850 HOST="$HOST" CFLAGS="$CFLAGS" \
Martin Roth7014f822020-12-21 15:49:33 -0700851 OPT_CFLAGS="-O -D_FORTIFY_SOURCE=2 -D COREBOOT_TOOLCHAIN_VERSION='\"coreboot toolchain v$CROSSGCC_VERSION\"' " \
Elyes HAOUAS157851f2020-05-29 10:20:14 +0200852 $MAKE $JOBS CC="$(hostcc host)" iasl acpibin acpidump acpiexec acpihelp acpisrc acpixtract
Patrick Georgi54f86a52021-05-11 15:06:37 +0200853 mkdir -p "$DESTDIR$TARGETDIR/bin/"
854 (cd "$DESTDIR$TARGETDIR/bin" && rm -f iasl acpibin acpidump acpiexec acpihelp acpisrc acpixtract) || touch "$RDIR/.failed"
855 (cd bin && cp iasl acpibin acpidump acpiexec acpihelp acpisrc acpixtract "$DESTDIR$TARGETDIR/bin") || touch "$RDIR/.failed"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700856}
857
858build_LLVM() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700859
Patrick Georgi5a76a532021-06-03 08:48:26 +0200860 ln -nsf "$LLVM_DIR" ../llvm
861 ln -nsf "$CLANG_DIR" ../clang
862 ln -nsf "$CTE_DIR" ../clang-tools-extra
863 ln -nsf "$CRT_DIR" ../compiler-rt
Elyes Haouasc8870b12022-09-16 12:10:00 +0200864 ln -nsf "$LLVMCMAKE_DIR" ../cmake
Martin Roth987d42d2018-07-22 11:45:29 -0600865
Felix Singerf8157af2022-11-23 10:12:26 +0100866 $CMAKE -G "Unix Makefiles" \
867 -DCMAKE_INSTALL_PREFIX="$DESTDIR$TARGETDIR" \
Martin Roth7014f822020-12-21 15:49:33 -0700868 -DCLANG_VENDOR="coreboot toolchain v$CROSSGCC_VERSION - " \
Patrick Georgi5a76a532021-06-03 08:48:26 +0200869 -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra;compiler-rt" \
Felix Singerf8157af2022-11-23 10:12:26 +0100870 -DLLVM_INCLUDE_BENCHMARKS="OFF" \
Felix Singer52191f22022-11-23 10:20:11 +0100871 -DLLVM_INCLUDE_TESTS="OFF" \
872 -DLLVM_INCLUDE_EXAMPLES="OFF" \
Felix Singerf8157af2022-11-23 10:12:26 +0100873 -DCMAKE_BUILD_TYPE=Release \
Felix Singer3e7438b2022-11-23 10:02:10 +0100874 -DLLVM_TARGETS_TO_BUILD="AArch64;ARM;PowerPC;RISCV;X86" \
875 ../llvm || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600876 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700877 $MAKE $JOBS || touch .failed
878 $MAKE install || touch .failed
879
Elyes Haouas49119422022-11-21 11:19:14 +0100880 rm -f ../llvm ../clang ../clang-tools-extra ../compiler-rt ../cmake
Patrick Georgi5a76a532021-06-03 08:48:26 +0200881
Elyes HAOUAS8f1853c2020-08-02 09:46:15 +0200882 cp -a ../$CLANG_DIR/tools/scan-build/* "$DESTDIR$TARGETDIR/bin"
883 cp -a ../$CLANG_DIR/tools/scan-view/* "$DESTDIR$TARGETDIR/bin"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700884}
885
Stefan Reinauer3b593492017-06-19 17:28:54 -0700886build_CMAKE() {
Nico Huber08bb8372017-06-24 19:50:35 +0200887 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS" \
Felix Singerf8157af2022-11-23 10:12:26 +0100888 ../${CMAKE_DIR}/configure \
Felix Singer3a5507f2023-04-01 11:32:56 +0200889 --parallel=${THREADS} \
Felix Singerf8157af2022-11-23 10:12:26 +0100890 --prefix="$TARGETDIR" || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600891 # shellcheck disable=SC2086
Stefan Reinauer3b593492017-06-19 17:28:54 -0700892 $MAKE $JOBS || touch .failed
893 $MAKE install DESTDIR=$DESTDIR || touch .failed
Stefan Reinauer3b593492017-06-19 17:28:54 -0700894}
895
Martin Rothd70f5fa2019-05-26 17:24:19 -0600896build_NASM() {
Elyes Haouas8d50e422023-01-28 12:54:28 +0100897 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS" \
Felix Singerf8157af2022-11-23 10:12:26 +0100898 ../${NASM_DIR}/configure \
899 --prefix="$TARGETDIR" || touch .failed
Martin Rothd70f5fa2019-05-26 17:24:19 -0600900 # shellcheck disable=SC2086
901 $MAKE $JOBS || touch .failed
902 $MAKE install DESTDIR=$DESTDIR || touch .failed
903
904 normalize_dirs
905}
906
Stefan Reinauerc66fd3c2017-06-21 14:57:28 -0700907print_supported() {
908 case "$PRINTSUPPORTED" in
Martin Rothedf965a2015-11-25 16:58:33 -0700909 BINUTILS|binutils) printf "%s\n" "$BINUTILS_VERSION";;
910 CLANG|clang) printf "%s\n" "$CLANG_VERSION";;
Martin Rothedf965a2015-11-25 16:58:33 -0700911 GCC|gcc) printf "%s\n" "$GCC_VERSION";;
Martin Rothedf965a2015-11-25 16:58:33 -0700912 GMP|gmp) printf "%s\n" "$GMP_VERSION";;
913 IASL|iasl) printf "%s\n" "$IASL_VERSION";;
Martin Rothedf965a2015-11-25 16:58:33 -0700914 MPC|mpc) printf "%s\n" "$MPC_VERSION";;
915 MPFR|mpfr) printf "%s\n" "$MPFR_VERSION";;
Martin Rothd70f5fa2019-05-26 17:24:19 -0600916 NASM|nasm) printf "%s\n" "${NASM_VERSION}";;
Stefan Reinauerc66fd3c2017-06-21 14:57:28 -0700917 *) printf "Unknown tool %s\n" "$PRINTSUPPORTED";;
Martin Rothedf965a2015-11-25 16:58:33 -0700918 esac
919}
Stefan Reinauer074d9132009-09-26 16:43:17 +0000920
Nico Huber18c74d62017-06-24 20:35:59 +0200921trap exit_handler 1 2 3 15
Zheng Baobb003c82016-08-05 13:41:51 +0800922
Patrick Georgi6bba29f2010-01-29 17:40:52 +0000923# Look if we have getopt. If not, build it.
924export PATH=$PATH:.
925getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
926
Stefan Reinauer074d9132009-09-26 16:43:17 +0000927# parse parameters.. try to find out whether we're running GNU getopt
Nico Huber27e8412b2017-12-05 22:02:21 +0100928getoptbrand="$(getopt -V 2>/dev/null | sed -e '1!d' -e 's,^\(......\).*,\1,')"
Peter Stuge09377b72011-08-21 06:24:55 +0200929if [ "${getoptbrand}" = "getopt" ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000930 # Detected GNU getopt that supports long options.
Martin Roth3b32af92022-11-27 12:55:31 -0700931 args=$(getopt -l print-version,version,help,clean,directory:,bootstrap,bootstrap-only,platform:,languages:,package:,jobs:,destdir:,savetemps,scripting,ccache,supported:,urls,nocolor,mirror -o WVhcd:bBp:l:P:j:D:tSys:unm -- "$@")
Nico Huber78df0bf2016-09-20 14:11:53 +0200932 getopt_ret=$?
Nico Hubercc414dd2016-09-20 13:59:38 +0200933 eval set -- "$args"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000934else
935 # Detected non-GNU getopt
Martin Roth3b32af92022-11-27 12:55:31 -0700936 args=$(getopt WVhcd:bBp:l:P:j:D:tSys:unm $*)
Nico Huber78df0bf2016-09-20 14:11:53 +0200937 getopt_ret=$?
Martin Roth987d42d2018-07-22 11:45:29 -0600938 # shellcheck disable=SC2086
Stefan Reinauer074d9132009-09-26 16:43:17 +0000939 set -- $args
940fi
941
Nico Huber78df0bf2016-09-20 14:11:53 +0200942if [ $getopt_ret != 0 ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000943 myhelp
944 exit 1
945fi
946
947while true ; do
948 case "$1" in
Felix Singer60c56be2021-10-17 14:54:12 +0200949 -W|--print-version) shift; echo $CROSSGCC_VERSION; exit 0;;
Patrick Georgi9681cdc2017-09-22 18:57:10 +0200950 -V|--version) shift; myversion; exit 0;;
951 -h|--help) shift; myhelp; exit 0;;
952 -c|--clean) shift; clean=1;;
953 -t|--savetemps) shift; SAVETEMPS=1;;
954 -d|--directory) shift; TARGETDIR="$1"; shift;;
955 -b|--bootstrap) shift; BOOTSTRAP=1;;
956 -B|--bootstrap-only) shift; BOOTSTRAPONLY=1; BOOTSTRAP=1;;
957 -p|--platform) shift; TARGETARCH="$1"; shift;;
958 -l|--languages) shift; LANGUAGES="$1"; shift;;
Martin Roth3b32af92022-11-27 12:55:31 -0700959 -m|--mirror) shift; USE_COREBOOT_MIRROR=1;;
Patrick Georgi9681cdc2017-09-22 18:57:10 +0200960 -D|--destdir) shift; DESTDIR="$1"; shift;;
961 -j|--jobs) shift; THREADS="$1"; JOBS="-j $1"; shift;;
962 -P|--package) shift; PACKAGE="$1"; shift;;
Patrick Georgi9681cdc2017-09-22 18:57:10 +0200963 -y|--ccache) shift; USECCACHE=1;;
964 -s|--supported) shift; PRINTSUPPORTED="$1"; shift;;
965 -u|--urls) shift; printf "%s\n" "$ALL_ARCHIVES"; exit 0;;
966 -n|--nocolor) shift; \
Martin Roth4a53db02016-03-01 15:21:07 -0700967 unset red RED green GREEN blue BLUE cyan CYAN NC;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200968 --) shift; break;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200969 *) break;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000970 esac
971done
972
Nico Huberbd74d562016-09-20 14:16:43 +0200973if [ $# -gt 0 ]; then
974 printf "Excessive arguments: $*\n"
975 myhelp
976 exit 1
977fi
978
Stefan Reinauerc66fd3c2017-06-21 14:57:28 -0700979if [ -n "$PRINTSUPPORTED" ]; then
980 print_supported
Martin Rothedf965a2015-11-25 16:58:33 -0700981 exit 0
982fi
983
984#print toolchain builder version string as the header
985printversion
986
Martin Roth2c6a8062016-11-14 11:58:39 -0700987printf "Building toolchain using %d thread(s).\n\n" "$THREADS"
988
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700989case "$TARGETARCH" in
Timothy Pearsona9f62352015-12-30 18:59:00 -0600990 x86_64-elf) ;;
991 x86_64*) TARGETARCH=x86_64-elf;;
992 i386-elf) ;;
993 i386-mingw32) ;;
Patrick Georgif0d5f672022-09-08 20:25:46 +0200994 riscv-elf) TARGETARCH=riscv64-elf
995 GCC_OPTIONS="$GCC_OPTIONS --with-isa-spec=20191213"
996 BINUTILS_OPTIONS="$BINUTILS_OPTIONS --with-isa-spec=20191213";;
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100997 powerpc64*-linux*) ;;
Timothy Pearsona9f62352015-12-30 18:59:00 -0600998 i386*) TARGETARCH=i386-elf;;
Stefan Reinauer27522ad2016-01-29 17:31:34 -0800999 arm*) TARGETARCH=arm-eabi;;
Timothy Pearsona9f62352015-12-30 18:59:00 -06001000 aarch64*) TARGETARCH=aarch64-elf;;
Martin Roth3e0f74d2016-03-08 12:07:04 -07001001 nds32le-elf) ;;
Timothy Pearsona9f62352015-12-30 18:59:00 -06001002 *) printf "${red}WARNING: Unsupported architecture $TARGETARCH.${NC}\n\n"; ;;
Ronald G. Minnichb460a662013-05-26 05:33:35 -07001003esac
1004
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001005# Figure out which packages to build
Stefan Reinauer699ac272015-06-05 12:43:28 -07001006
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001007case "$PACKAGE" in
1008 GCC|gcc)
Stefan Reinauer81693d42017-06-21 15:25:36 -07001009 echo "Target architecture is $TARGETARCH"
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001010 NAME="${TARGETARCH} cross GCC"
Nico Huberafda56e2017-12-07 17:50:53 +01001011 PACKAGES="GMP MPFR MPC BINUTILS GCC"
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001012 ;;
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001013 CLANG|clang)
Stefan Reinauer81693d42017-06-21 15:25:36 -07001014 NAME="LLVM clang"
1015 LLVM_VERSION=${CLANG_VERSION}
Elyes Haouasc8870b12022-09-16 12:10:00 +02001016 PACKAGES="CMAKE LLVM CLANG CRT CTE LLVMCMAKE"
Stefan Reinauerca117e72017-06-19 17:29:08 -07001017 CMAKE=${DESTDIR}${TARGETDIR}/bin/cmake
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001018 ;;
1019 IASL|iasl)
1020 NAME="IASL ACPI compiler"
1021 PACKAGES=IASL
1022 ;;
Stefan Reinauer3b593492017-06-19 17:28:54 -07001023 CMAKE|cmake)
1024 NAME="CMake"
1025 PACKAGES=CMAKE
1026 ;;
Martin Rothd70f5fa2019-05-26 17:24:19 -06001027 NASM|nasm)
1028 NAME="NASM"
1029 PACKAGES=NASM
1030 ;;
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001031 *)
Patrick Georgif32eed12021-10-16 10:50:00 +02001032 printf "${red}ERROR: Unsupported package $PACKAGE. (Supported packages are GCC, CLANG, IASL, and NASM)${NC}\n\n";
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001033 exit 1
1034 ;;
1035esac
Peter Stugeceacd772011-06-09 04:54:16 +02001036
Stefan Reinauer66e93352015-06-16 22:38:23 +02001037# Find all the required tools:
1038
1039TAR=$(searchtool tar) || exit $?
1040PATCH=$(searchtool patch) || exit $?
1041MAKE=$(searchtool make) || exit $?
1042SHA1SUM=$(searchtool sha1sum)
zbao5cf758d2015-09-01 22:28:57 -04001043#SHA512SUM=$(searchtool sha512sum)
1044#MD5SUM=$(searchtool md5sum)
Stefan Reinauer66e93352015-06-16 22:38:23 +02001045CHECKSUM=$SHA1SUM
Martin Roth95f7b222016-03-20 12:38:48 -06001046LBZIP2=$(searchtool lbzip2 "" nofail)
1047PIGZ=$(searchtool pigz "" nofail)
Stefan Reinauer66e93352015-06-16 22:38:23 +02001048
1049searchtool m4 > /dev/null
1050searchtool bison > /dev/null
1051searchtool flex flex > /dev/null
Stefan Reinauer66e93352015-06-16 22:38:23 +02001052searchtool bzip2 "bzip2," > /dev/null
Nico Huber0ff3b392017-03-12 16:07:33 +01001053searchtool xz "XZ Utils" "" "xz-utils" > /dev/null
Stefan Reinauer66e93352015-06-16 22:38:23 +02001054
Patrick Georgidf1ff232017-01-07 09:28:43 +01001055if searchtool wget "GNU" nofail > /dev/null; then
1056 download_showing_percentage() {
1057 url=$1
Stefan Reinauer81693d42017-06-21 15:25:36 -07001058 printf "... ${red} 0%%"
Martin Roth82a30a12020-11-07 15:17:45 -07001059 wget --tries=3 "$url" 2>&1 | while read -r line; do
Martin Roth987d42d2018-07-22 11:45:29 -06001060 echo "$line" | grep -o "[0-9]\+%" | awk '{printf("\b\b\b\b%4s", $1)}'
Patrick Georgidf1ff232017-01-07 09:28:43 +01001061 done
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +02001062 printf "${NC}... "
Patrick Georgidf1ff232017-01-07 09:28:43 +01001063 }
1064elif searchtool curl "^curl " > /dev/null; then
1065 download_showing_percentage() {
1066 url=$1
1067 echo
Idwer Vollering5190f422020-11-24 15:13:16 +01001068 curl -O --progress-bar --location --retry 3 "$url"
Patrick Georgidf1ff232017-01-07 09:28:43 +01001069 }
1070fi
1071
Nico Huber08bb8372017-06-24 19:50:35 +02001072# Allow $CC override from the environment.
1073if [ -n "$CC" ]; then
1074 if [ ! -x "$(command -v "$CC" 2>/dev/null)" ]; then
1075 printf "${RED}ERROR:${red} CC is set to '%s' but wasn't found.${NC}\n" "$CC"
1076 HALT_FOR_TOOLS=1
1077 fi
1078else
Patrick Georgi8f2cdad2017-07-10 11:48:34 +02001079 if searchtool gnatgcc "Free Software Foundation" nofail > /dev/null; then
1080 CC=gnatgcc
1081 elif searchtool gcc "Free Software Foundation" nofail > /dev/null; then
Nico Huber08bb8372017-06-24 19:50:35 +02001082 CC=gcc
1083 else
1084 searchtool cc '^' nofail > /dev/null || please_install gcc
1085 CC=cc
1086 fi
1087fi
Martin Roth591790f2016-03-20 20:39:04 -06001088
Nico Huber08bb8372017-06-24 19:50:35 +02001089# We can leave $CXX empty if it's not set since *buildgcc* never
1090# calls it directly. This way configure scripts can search for
1091# themselves and we still override it when a bootstrapped g++ is
1092# to be used (cf. hostcxx()).
1093if [ -n "$CXX" ]; then
1094 if [ ! -x "$(command -v "$CXX" 2>/dev/null)" ]; then
1095 printf "${RED}ERROR:${red} CXX is set to '%s' but wasn't found.${NC}\n" "$CXX"
1096 HALT_FOR_TOOLS=1
1097 fi
1098else
1099 searchtool g++ "Free Software Foundation" nofail > /dev/null || \
1100 searchtool clang "clang version" nofail > /dev/null || \
1101 searchtool clang "LLVM" "" "g++" > /dev/null
1102fi
1103
1104check_for_library "-lz" "zlib (zlib1g-dev or zlib-devel)"
Nico Huber156d87c2016-09-20 12:59:53 +02001105
Martin Roth88463822016-01-07 11:03:36 -07001106if [ "$HALT_FOR_TOOLS" -ne 0 ]; then
1107 exit 1
1108fi
1109
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001110# This initial cleanup is useful when updating the toolchain script.
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001111
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001112if [ "$clean" = "1" ]; then
1113 cleanup
1114fi
Stefan Reinauer074d9132009-09-26 16:43:17 +00001115
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001116# Set up host compiler and flags needed for various OSes
Stefan Reinauer074d9132009-09-26 16:43:17 +00001117
Patrick Georgif2c15f52015-06-11 21:07:31 +02001118if is_package_enabled "GCC"; then
Patrick Georgie8253fe2017-07-18 11:35:35 +02001119# sane preset: let the configure script figure out things by itself
1120# more importantly, avoid any values that might already linger in the variable
1121OPTIONS="ABI="
Martin Roth987d42d2018-07-22 11:45:29 -06001122if [ "$UNAME" = "Darwin" ]; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001123 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
1124
1125 # generally the OS X compiler can create x64 binaries.
1126 # Per default it generated i386 binaries in 10.5 and x64
1127 # binaries in 10.6 (even if the kernel is 32bit)
1128 # For some weird reason, 10.5 autodetects an ABI=64 though
1129 # so we're setting the ABI explicitly here.
Martin Roth987d42d2018-07-22 11:45:29 -06001130 if [ "$(sysctl -n hw.optional.x86_64 2>/dev/null)" -eq 1 ] 2>/dev/null; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001131 OPTIONS="ABI=64"
1132 else
1133 OPTIONS="ABI=32"
1134 fi
Stefan Reinauer9491b4d2011-10-11 22:37:59 -07001135
Stefan Reinauer4266b922012-12-05 16:18:32 -08001136 # In Xcode 4.5.2 the default compiler is clang.
1137 # However, this compiler fails to compile gcc 4.7.x. As a
1138 # workaround it's possible to compile gcc with llvm-gcc.
Stefan Reinauer9491b4d2011-10-11 22:37:59 -07001139 if $CC -v 2>&1 | grep -q LLVM; then
Stefan Reinauer4266b922012-12-05 16:18:32 -08001140 CC=llvm-gcc
Stefan Reinauer9491b4d2011-10-11 22:37:59 -07001141 fi
Martin Roth987d42d2018-07-22 11:45:29 -06001142elif [ "$UNAME" = "Linux" ] || [ "$UNAME" = "Cygwin" ]; then
Patrick Georgiddb8f802015-07-04 17:45:54 +02001143 # gmp is overeager with detecting 64bit CPUs even if they run
1144 # a 32bit kernel and userland.
1145 if [ "$(uname -m 2>/dev/null)" = "i686" ]; then
1146 OPTIONS="ABI=32"
1147 fi
Martin Roth987d42d2018-07-22 11:45:29 -06001148elif [ "$UNAME" = "NetBSD" ]; then
Nico Huberde45c592016-01-20 23:22:33 +01001149 # same for NetBSD but this one reports an i386
1150 if [ "$(uname -m 2>/dev/null)" = "i386" ]; then
1151 OPTIONS="ABI=32"
1152 fi
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001153fi
Nico Huber66c2c1a2016-09-20 13:09:29 +02001154if [ -z "${LANGUAGES}" ]; then
Nico Hubercd87e1b2017-06-24 20:35:59 +02001155 if have_gnat; then
Nico Huber66c2c1a2016-09-20 13:09:29 +02001156 printf "\nFound compatible Ada compiler, enabling Ada support by default.\n\n"
1157 LANGUAGES="ada,${DEFAULT_LANGUAGES}"
1158 else
Nico Hubercd87e1b2017-06-24 20:35:59 +02001159 printf "\n${red}WARNING${NC}\n"
1160 printf "No compatible Ada compiler (GNAT) found. You can continue without\n"
1161 printf "Ada support, but this will limit the features of ${blue}coreboot${NC} (e.g.\n"
1162 printf "native graphics initialization won't be available on most Intel\n"
1163 printf "boards).\n\n"
1164
1165 printf "Usually, you can install GNAT with your package management system\n"
1166 printf "(the package is called \`gnat\` or \`gcc-ada\`). It has to match the\n"
1167 printf "\`gcc\` package in version. If there are multiple versions of GCC in-\n"
1168 printf "stalled, you can point this script to the matching version through\n"
1169 printf "the \`CC\` and \`CXX\` environment variables.\n\n"
1170
1171 printf "e.g. on Ubuntu 14.04, if \`gcc\` is \`gcc-4.8\`:\n"
1172 printf " apt-get install gnat-4.8 && make crossgcc\n\n"
1173
1174 printf "on Ubuntu 16.04, if \`gcc\` is \`gcc-5\`:\n"
1175 printf " apt-get install gnat-5 && make crossgcc\n"
1176 timeout 30
Nico Huber66c2c1a2016-09-20 13:09:29 +02001177 LANGUAGES="${DEFAULT_LANGUAGES}"
1178 fi
1179fi
Patrick Georgif2741aa2020-07-06 16:35:56 +02001180if [ "$BOOTSTRAP" != 1 ] && \
1181 { [ "$(hostcc_major)" -lt 4 ] || \
1182 { [ "$(hostcc_major)" -eq 4 ] && \
1183 [ "$(hostcc_minor)" -lt 9 ] ; } ; }
1184then
1185 printf "\n${red}WARNING${NC}\n"
1186 printf "Building coreboot requires a host compiler newer than 4.9.x while\n"
1187 printf "yours is $(hostcc_version).\n"
1188 printf "Enabling bootstrapping to provide a sufficiently new compiler:\n"
1189 printf "This will take significantly longer than a usual build.\n"
1190 printf "Alternatively you can abort now and update your host compiler.\n"
1191 timeout 15
1192 BOOTSTRAP=1
1193fi
Nico Hubercd87e1b2017-06-24 20:35:59 +02001194if ada_requested; then
Patrick Georgif2741aa2020-07-06 16:35:56 +02001195 if ! have_gnat; then
Nico Hubercd87e1b2017-06-24 20:35:59 +02001196 please_install gnat gcc-ada
1197 exit 1
1198 fi
Nico Hubercd87e1b2017-06-24 20:35:59 +02001199fi
Stefan Reinauer14ce2132015-06-05 13:01:13 -07001200fi # GCC
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001201
Nico Huber5f72f962016-01-31 23:05:19 +01001202export HOSTCFLAGS="-Os"
1203if have_hostcflags_from_gmp; then
1204 set_hostcflags_from_gmp
1205fi
1206
Patrick Georgic1a75b12011-11-04 21:37:14 +01001207if [ "$USECCACHE" = 1 ]; then
1208 CC="ccache $CC"
1209fi
1210
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001211# Prepare target directory for building GCC
1212# (dependencies must be in the PATH)
Martin Roth987d42d2018-07-22 11:45:29 -06001213mkdir -p "$DESTDIR$TARGETDIR/bin"
1214mkdir -p "$DESTDIR$TARGETDIR/share"
Stefan Reinauer16bd7892012-12-07 23:57:01 +01001215export PATH=$DESTDIR$TARGETDIR/bin:$PATH
1216
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001217# Download, unpack, patch and build all packages
1218
Idwer Vollering458e7df2020-09-25 12:26:51 +02001219printf "Downloading and verifying tarballs ...\n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001220mkdir -p tarballs
1221for P in $PACKAGES; do
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +02001222 download "$P" || exit "$?"
1223 verify_hash "$P" || exit "$?"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001224done
Martin Roth03625172016-02-17 14:44:14 -07001225printf "Downloaded tarballs ... ${green}ok${NC}\n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001226
Elyes HAOUASb0f19882018-06-09 11:59:00 +02001227printf "Unpacking and patching ...\n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001228for P in $PACKAGES; do
1229 unpack_and_patch $P || exit 1
1230done
1231printf "Unpacked and patched ... ${green}ok${NC}\n"
1232
Patrick Georgi9681cdc2017-09-22 18:57:10 +02001233if [ -n "$BOOTSTRAPONLY" ]; then
Elyes HAOUASb0f19882018-06-09 11:59:00 +02001234 printf "Building bootstrap compiler only ...\n"
Nico Huberafda56e2017-12-07 17:50:53 +01001235 for pkg in GMP MPFR MPC GCC; do
Patrick Georgi9681cdc2017-09-22 18:57:10 +02001236 build_for_host $pkg
1237 done
1238 exit 0
1239fi
1240
Elyes HAOUASb0f19882018-06-09 11:59:00 +02001241printf "Building packages ...\n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -07001242for package in $PACKAGES; do
1243 build $package
1244done
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001245printf "Packages built ... ${green}ok${NC}\n"
Stefan Reinauer14ce2132015-06-05 13:01:13 -07001246
Stefan Reinauer699ac272015-06-05 12:43:28 -07001247# Adding git information of current tree to target directory
1248# for reproducibility
Stefan Reinauerd7649122015-06-09 11:44:25 -07001249PROGNAME=$(basename "$0")
Stefan Reinauer88352d72016-04-06 15:57:03 -07001250rm -f "$DESTDIR$TARGETDIR/share/$PROGNAME-*"
Felix Singer62fcffb2021-10-19 13:25:18 +02001251cp "$PROGNAME" "$DESTDIR$TARGETDIR/share/$PROGNAME-$CROSSGCC_VERSION"
Zheng Bao30b895f2013-02-06 18:04:40 +08001252
Patrick Georgi0c2df6f2017-11-22 16:14:09 -05001253# Adding edk2 tools template
1254mkdir -p "$DESTDIR$TARGETDIR/share/edk2config"
1255sed -e "s,@@PREFIX@@,$TARGETDIR,g" edk2tools.txt > "$DESTDIR$TARGETDIR/share/edk2config/tools_def.txt"
1256printf "Copied EDK2 tools template ... ${green}ok${NC}\n"
1257
Stefan Reinauer88e83e52016-04-06 15:39:48 -07001258cleanup
Stefan Reinauer074d9132009-09-26 16:43:17 +00001259
Stefan Reinauer81693d42017-06-21 15:25:36 -07001260printf "\n${green}You can now run $NAME from $TARGETDIR.${NC}\n"