blob: 5af3d5aadfd0bcccb3ffe47ba1287d4e58fc0813 [file] [log] [blame]
Peter Stuge09377b72011-08-21 06:24:55 +02001#!/bin/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
Patrick Georgi5ce73e92019-01-10 14:07:56 +010019CROSSGCC_DATE="$(git log -n 1 --pretty=%ad --date=short .)"
Wonkyu Kim31eeda12019-01-18 17:33:55 -080020CROSSGCC_VERSION="$(git log -n 1 --pretty=%h .)"
Stefan Reinauer88352d72016-04-06 15:57:03 -070021CROSSGCC_COMMIT=$( git describe )
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
31SKIPPYTHON=1
Nico Huber234d2462016-01-26 16:10:17 +010032BOOTSTRAP=0
Martin Roth2c6a8062016-11-14 11:58:39 -070033THREADS=1
Stefan Reinauer074d9132009-09-26 16:43:17 +000034
Stefan Reinauer699ac272015-06-05 12:43:28 -070035# GCC toolchain version numbers
Elyes HAOUASe5412682020-01-18 17:43:18 +010036GMP_VERSION=6.2.0
Elyes HAOUAS150c4382020-07-11 00:40:15 +020037MPFR_VERSION=4.1.0
Elyes HAOUAS96187062020-08-18 21:53:38 +020038MPC_VERSION=1.2.0
Patrick Rudolph6b88f902020-03-15 07:29:14 +010039GCC_VERSION=8.3.0
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070040GCC_AUTOCONF_VERSION=2.69
Elyes HAOUAS794a9b72020-09-19 13:57:36 +020041BINUTILS_VERSION=2.35.1
Elyes HAOUASa0eb64a2020-05-24 15:03:33 +020042GDB_VERSION=9.2
Elyes HAOUAS274c3fa2020-10-02 08:23:01 +020043IASL_VERSION=20200925
Elyes HAOUAS384e2c92020-07-22 20:22:55 +020044PYTHON_VERSION=3.8.5
Elyes HAOUASa2fbddf2019-10-03 10:38:39 +020045EXPAT_VERSION=2.2.9
Stefan Reinauer14ce2132015-06-05 13:01:13 -070046# CLANG version number
Elyes HAOUASf106b3b2020-10-14 10:52:08 +020047CLANG_VERSION=11.0.0
Elyes HAOUAS9c693692020-08-17 10:49:30 +020048CMAKE_VERSION=3.18.1
Elyes HAOUAS1ba663c2020-08-23 07:50:32 +020049NASM_VERSION=2.15.05
Stefan Reinauer074d9132009-09-26 16:43:17 +000050
Stefan Reinauer699ac272015-06-05 12:43:28 -070051# GCC toolchain archive locations
Martin Roth1484c032016-03-08 09:37:14 -070052# These are sanitized by the jenkins toolchain test builder, so if
53# a completely new URL is added here, it probably needs to be added
54# to the jenkins build as well, or the builder won't download it.
Doug Gale43ae4332017-09-27 22:10:16 -040055GMP_ARCHIVE="https://ftpmirror.gnu.org/gmp/gmp-${GMP_VERSION}.tar.xz"
56MPFR_ARCHIVE="https://ftpmirror.gnu.org/mpfr/mpfr-${MPFR_VERSION}.tar.xz"
57MPC_ARCHIVE="https://ftpmirror.gnu.org/mpc/mpc-${MPC_VERSION}.tar.gz"
Patrick Georgib1d26f02018-05-02 17:13:34 +020058GCC_ARCHIVE="https://ftpmirror.gnu.org/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.xz"
Iru Cai776b5ba2017-07-28 21:40:34 +080059BINUTILS_ARCHIVE="https://ftpmirror.gnu.org/binutils/binutils-${BINUTILS_VERSION}.tar.xz"
Doug Gale43ae4332017-09-27 22:10:16 -040060GDB_ARCHIVE="https://ftpmirror.gnu.org/gdb/gdb-${GDB_VERSION}.tar.xz"
Patrick Georgi85bb9462016-02-11 17:22:16 +010061IASL_ARCHIVE="https://acpica.org/sites/acpica/files/acpica-unix2-${IASL_VERSION}.tar.gz"
Doug Gale43ae4332017-09-27 22:10:16 -040062PYTHON_ARCHIVE="https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz"
63EXPAT_ARCHIVE="https://downloads.sourceforge.net/sourceforge/expat/expat-${EXPAT_VERSION}.tar.bz2"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070064# CLANG toolchain archive locations
Elyes HAOUAS8f1853c2020-08-02 09:46:15 +020065LLVM_ARCHIVE="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}/llvm-${CLANG_VERSION}.src.tar.xz"
66CLANG_ARCHIVE="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}/clang-${CLANG_VERSION}.src.tar.xz"
67CRT_ARCHIVE="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}/compiler-rt-${CLANG_VERSION}.src.tar.xz"
68CTE_ARCHIVE="https://github.com/llvm/llvm-project/releases/download/llvmorg-${CLANG_VERSION}/clang-tools-extra-${CLANG_VERSION}.src.tar.xz"
Elyes HAOUAS9c693692020-08-17 10:49:30 +020069CMAKE_ARCHIVE="https://cmake.org/files/v3.18/cmake-${CMAKE_VERSION}.tar.gz"
Martin Rothd70f5fa2019-05-26 17:24:19 -060070NASM_ARCHIVE="https://www.nasm.us/pub/nasm/releasebuilds/${NASM_VERSION}/nasm-${NASM_VERSION}.tar.bz2"
Stefan Reinauer074d9132009-09-26 16:43:17 +000071
Nico Huberafda56e2017-12-07 17:50:53 +010072ALL_ARCHIVES="$GMP_ARCHIVE $MPFR_ARCHIVE $MPC_ARCHIVE \
Martin Roth58c68d52016-03-01 15:22:42 -070073 $GCC_ARCHIVE $BINUTILS_ARCHIVE $GDB_ARCHIVE $IASL_ARCHIVE \
Elyes HAOUAS8f1853c2020-08-02 09:46:15 +020074 $PYTHON_ARCHIVE $EXPAT_ARCHIVE $LLVM_ARCHIVE $CLANG_ARCHIVE \
Elyes HAOUAS91fb1392020-01-20 15:33:11 +010075 $CRT_ARCHIVE $CTE_ARCHIVE $CMAKE_ARCHIVE $NASM_ARCHIVE"
Martin Roth58c68d52016-03-01 15:22:42 -070076
Stefan Reinauer699ac272015-06-05 12:43:28 -070077# GCC toolchain directories
Stefan Reinauer074d9132009-09-26 16:43:17 +000078GMP_DIR="gmp-${GMP_VERSION}"
79MPFR_DIR="mpfr-${MPFR_VERSION}"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000080MPC_DIR="mpc-${MPC_VERSION}"
Martin Roth987d42d2018-07-22 11:45:29 -060081# shellcheck disable=SC2034
Stefan Reinauer074d9132009-09-26 16:43:17 +000082GCC_DIR="gcc-${GCC_VERSION}"
Martin Roth987d42d2018-07-22 11:45:29 -060083# shellcheck disable=SC2034
Stefan Reinauer074d9132009-09-26 16:43:17 +000084BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
85GDB_DIR="gdb-${GDB_VERSION}"
Patrick Georgi85bb9462016-02-11 17:22:16 +010086IASL_DIR="acpica-unix2-${IASL_VERSION}"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +010087PYTHON_DIR="Python-${PYTHON_VERSION}"
88EXPAT_DIR="expat-${EXPAT_VERSION}"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070089# CLANG toolchain directories
90LLVM_DIR="llvm-${CLANG_VERSION}.src"
Elyes HAOUAS8f1853c2020-08-02 09:46:15 +020091CLANG_DIR="clang-${CLANG_VERSION}.src"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070092CRT_DIR="compiler-rt-${CLANG_VERSION}.src"
93CTE_DIR="clang-tools-extra-${CLANG_VERSION}.src"
Stefan Reinauer3b593492017-06-19 17:28:54 -070094CMAKE_DIR="cmake-${CMAKE_VERSION}"
Martin Rothd70f5fa2019-05-26 17:24:19 -060095NASM_DIR="nasm-${NASM_VERSION}"
Stefan Reinauer074d9132009-09-26 16:43:17 +000096
Patrick Georgi6321f522015-02-27 23:36:26 +010097unset MAKELEVEL MAKEFLAGS
98
Patrick Georgi1861ff72011-10-31 12:15:55 +010099red='\033[0;31m'
100RED='\033[1;31m'
101green='\033[0;32m'
102GREEN='\033[1;32m'
103blue='\033[0;34m'
Patrick Georgi1861ff72011-10-31 12:15:55 +0100104CYAN='\033[1;36m'
105NC='\033[0m' # No Color
Stefan Reinauer074d9132009-09-26 16:43:17 +0000106
Martin Roth987d42d2018-07-22 11:45:29 -0600107UNAME=$(if uname | grep -iq cygwin; then echo Cygwin; else uname; fi)
Martin Roth88463822016-01-07 11:03:36 -0700108HALT_FOR_TOOLS=0
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700109
Nico Huber08bb8372017-06-24 19:50:35 +0200110hostcc()
111{
112 # $1 "host" or "target"
Martin Roth987d42d2018-07-22 11:45:29 -0600113 if [ "$BOOTSTRAP" = 1 ] && [ "$1" = target ]; then
114 echo "$DESTDIR$TARGETDIR/bin/gcc"
Nico Huber08bb8372017-06-24 19:50:35 +0200115 else
Martin Roth987d42d2018-07-22 11:45:29 -0600116 echo "$CC"
Nico Huber08bb8372017-06-24 19:50:35 +0200117 fi
118}
119
120hostcxx()
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/g++"
Nico Huber08bb8372017-06-24 19:50:35 +0200125 else
Martin Roth987d42d2018-07-22 11:45:29 -0600126 echo "$CXX"
Nico Huber08bb8372017-06-24 19:50:35 +0200127 fi
128}
129
Patrick Georgi198d23c2012-12-08 08:02:44 +0100130normalize_dirs()
131{
Martin Roth987d42d2018-07-22 11:45:29 -0600132 mkdir -p "$DESTDIR$TARGETDIR/lib"
133 test -d "$DESTDIR$TARGETDIR/lib32" && mv "$DESTDIR$TARGETDIR"/lib32/* "$DESTDIR$TARGETDIR/lib"
134 test -d "$DESTDIR$TARGETDIR/lib64" && mv "$DESTDIR$TARGETDIR"/lib64/* "$DESTDIR$TARGETDIR/lib"
135 rmdir -p "$DESTDIR$TARGETDIR/lib32" "$DESTDIR$TARGETDIR/lib64"
Patrick Georgi198d23c2012-12-08 08:02:44 +0100136
Martin Roth987d42d2018-07-22 11:45:29 -0600137 perl -pi -e "s,/lib32,/lib," "$DESTDIR$TARGETDIR"/lib/*.la
138 perl -pi -e "s,/lib64,/lib," "$DESTDIR$TARGETDIR"/lib/*.la
Patrick Georgi198d23c2012-12-08 08:02:44 +0100139}
140
Nico Hubercd87e1b2017-06-24 20:35:59 +0200141countdown()
142{
143 tout=${1:-10}
144
Martin Roth987d42d2018-07-22 11:45:29 -0600145 printf "\nPress Ctrl-C to abort, Enter to continue... %2ds" "$tout"
146 while [ "$tout" -gt 0 ]; do
Nico Hubercd87e1b2017-06-24 20:35:59 +0200147 sleep 1
148 tout=$((tout - 1))
149 printf "\b\b\b%2ds" $tout
150 done
151 printf "\n"
152}
153
154timeout()
155{
156 tout=${1:-10}
157
158 # Ignore SIGUSR1, should interrupt `read` though.
159 trap false USR1
160 # Clean up in case the user aborts.
161 trap 'kill $counter > /dev/null 2>&1' EXIT
162
Martin Roth987d42d2018-07-22 11:45:29 -0600163 (countdown "$tout"; kill -USR1 $$)&
Nico Hubercd87e1b2017-06-24 20:35:59 +0200164 counter=$!
165
166 # Some shells with sh compatibility mode (e.g. zsh, mksh) only
167 # let us interrupt `read` if a non-standard -t parameter is given.
Martin Roth987d42d2018-07-22 11:45:29 -0600168 # shellcheck disable=SC2034,SC2039,SC2162
Nico Hubercd87e1b2017-06-24 20:35:59 +0200169 if echo | read -t 1 foo 2>/dev/null; then
170 read -t $((tout + 1)) foo
171 else
172 read foo
173 fi
174
175 kill $counter > /dev/null 2>&1
176 trap - USR1 EXIT
177}
178
Stefan Reinauer93a96302015-06-15 12:36:53 -0700179please_install()
180{
Martin Roth88463822016-01-07 11:03:36 -0700181 HALT_FOR_TOOLS=1
Martin Roth987d42d2018-07-22 11:45:29 -0600182 # shellcheck disable=SC1091
Patrick Georgi5da95dc2015-07-17 23:33:05 +0200183 test -r /etc/os-release && . /etc/os-release
Alex Thiessen72d10892018-01-12 01:45:53 +0000184 # vanilla debian doesn't define `ID_LIKE`, just `ID`
185 if [ -z "${ID_LIKE}" ] && [ -n "${ID}" ]; then
186 ID_LIKE=${ID}
187 fi
Stefan Reinauer93a96302015-06-15 12:36:53 -0700188 case "$ID_LIKE" in
189 debian) solution="sudo apt-get install $1" ;;
190 suse) solution="sudo zypper install $1" ;;
191 *) solution="using your OS packaging system" ;;
192 esac
193
Patrick Georgidf1ff232017-01-07 09:28:43 +0100194 printf "${RED}ERROR:${red} Missing tool: Please install '$1'. (eg $solution)${NC}\n" >&2
Martin Rothe3963172016-01-06 13:54:32 -0700195 if [ -n "$2" ]; then
Patrick Georgidf1ff232017-01-07 09:28:43 +0100196 printf "${RED}ERROR:${red} or install '$2'.${NC}\n" >&2
Martin Rothe3963172016-01-06 13:54:32 -0700197 fi
Stefan Reinauer93a96302015-06-15 12:36:53 -0700198}
199
Patrick Georgib7062882015-02-24 10:52:14 +0100200searchtool()
Stefan Reinauer074d9132009-09-26 16:43:17 +0000201{
202 # $1 short name
Patrick Georgib7062882015-02-24 10:52:14 +0100203 # $2 search string
204 # $3 soft fail if set
Martin Rothe3963172016-01-06 13:54:32 -0700205 # $4 alternative package to install on failure
Patrick Georgib7062882015-02-24 10:52:14 +0100206 # result: file name of that tool on stdout
207 # or no output if nothing suitable was found
208 search=GNU
209 if [ -n "$2" ]; then
210 search="$2"
211 fi
Stefan Reinauer074d9132009-09-26 16:43:17 +0000212 for i in "$1" "g$1" "gnu$1"; do
Martin Roth987d42d2018-07-22 11:45:29 -0600213 if [ -x "$(command -v "$i" 2>/dev/null)" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700214 if [ "$(cat /dev/null | $i --version 2>&1 | grep -c "$search")" \
215 -gt 0 ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600216 echo "$i"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000217 return
218 fi
219 fi
220 done
Andrew Wub67e9a12014-04-28 18:13:44 +0800221 # A workaround for OSX 10.9 and some BSDs, whose nongnu
222 # patch and tar also work.
Martin Roth987d42d2018-07-22 11:45:29 -0600223 if [ "$UNAME" = "Darwin" ] || [ "$UNAME" = "FreeBSD" ] || [ "$UNAME" = "NetBSD" ] || [ "$UNAME" = "OpenBSD" ]; then
224 if [ "$1" = "patch" ] || [ "$1" = "tar" ]; then
225 if [ -x "$(command -v "$1" 2>/dev/null)" ]; then
226 echo "$1"
Zheng Bao36156ff2012-09-28 16:18:58 +0800227 return
228 fi
229 fi
230 fi
Martin Roth987d42d2018-07-22 11:45:29 -0600231 if echo "$1" | grep -q "sum" ; then
232 algor=$(echo "$1" | sed -e 's,sum,,')
233 if [ -x "$(command -v "$1" 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400234 #xxxsum [file]
Martin Roth987d42d2018-07-22 11:45:29 -0600235 echo "$1"
zbao5cf758d2015-09-01 22:28:57 -0400236 return
Martin Roth987d42d2018-07-22 11:45:29 -0600237 elif [ -x "$(command -v "$algor" 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400238 #xxx [file]
Martin Roth987d42d2018-07-22 11:45:29 -0600239 echo "$algor"
zbao5cf758d2015-09-01 22:28:57 -0400240 return
Martin Rothd55f5eb2017-06-15 11:37:26 -0600241 elif [ -x "$(command -v openssl 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400242 #openssl xxx [file]
Martin Roth987d42d2018-07-22 11:45:29 -0600243 echo openssl "$algor"
zbao5cf758d2015-09-01 22:28:57 -0400244 return
Martin Rothd55f5eb2017-06-15 11:37:26 -0600245 elif [ -x "$(command -v cksum 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400246 #cksum -a xxx [file]
247 #cksum has special options in NetBSD. Actually, NetBSD will use the second case above.
Martin Roth987d42d2018-07-22 11:45:29 -0600248 echo "buildgcc" | cksum -a "$algor" > /dev/null 2>/dev/null && \
249 echo cksum -a "$algor"
zbao5cf758d2015-09-01 22:28:57 -0400250 return
zbao939dc842015-04-30 04:44:07 +0800251 fi
252 fi
Martin Rothe3963172016-01-06 13:54:32 -0700253
Martin Roth987d42d2018-07-22 11:45:29 -0600254 [ -z "$3" ] && please_install "$1" "$4"
Patrick Georgib7062882015-02-24 10:52:14 +0100255 false
Stefan Reinauer074d9132009-09-26 16:43:17 +0000256}
257
Martin Roth591790f2016-03-20 20:39:04 -0600258# Run a compile check of the specified library option to see if it's installed
259check_for_library() {
Martin Roth987d42d2018-07-22 11:45:29 -0600260 LIBRARY_FLAGS="$1"
261 LIBRARY_PACKAGES="$2"
262 LIBTEST_FILE=.libtest
Martin Roth591790f2016-03-20 20:39:04 -0600263
264 echo "int main(int argc, char **argv) { (void) argc; (void) argv; return 0; }" > "${LIBTEST_FILE}.c"
265
Martin Roth987d42d2018-07-22 11:45:29 -0600266 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200267 "$CC" $CFLAGS $LIBRARY_FLAGS "${LIBTEST_FILE}.c" -o "${LIBTEST_FILE}" >/dev/null 2>&1 || \
Martin Roth591790f2016-03-20 20:39:04 -0600268 please_install "$LIBRARY_PACKAGES"
269 rm -rf "${LIBTEST_FILE}.c" "${LIBTEST_FILE}"
270}
271
Nico Huberaee7f462016-09-20 12:55:19 +0200272buildcc_major() {
273 echo "${GCC_VERSION}" | cut -d. -f1
274}
275
Nico Huber26267a72016-12-04 02:45:58 +0100276buildcc_minor() {
277 echo "${GCC_VERSION}" | cut -d. -f2
278}
279
Nico Huberaee7f462016-09-20 12:55:19 +0200280buildcc_version() {
281 echo "${GCC_VERSION}" | cut -d. -f1-2
282}
283
284hostcc_major() {
285 (echo __GNUC__ | ${CC} -E - 2>/dev/null || echo 0) | tail -1
286}
287
288hostcc_minor() {
289 (echo __GNUC_MINOR__ | ${CC} -E - 2>/dev/null || echo 0) | tail -1
290}
291
292hostcc_version() {
293 printf "%d.%d" "$(hostcc_major)" "$(hostcc_minor)"
294}
295
Nico Huber75797162016-09-20 13:01:57 +0200296hostcc_has_gnat1() {
297 [ -x "$(${CC} -print-prog-name=gnat1)" ]
298}
299
Nico Hubercd87e1b2017-06-24 20:35:59 +0200300have_gnat() {
301 hostcc_has_gnat1 && \
302 searchtool gnatbind "Free Software Foundation" nofail > /dev/null
303}
304
Nico Huber75797162016-09-20 13:01:57 +0200305ada_requested() {
306 echo "${LANGUAGES}" | grep -q '\<ada\>'
307}
308
Stefan Reinauer88935482015-06-05 09:51:10 -0700309download() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700310 package=$1
Martin Roth987d42d2018-07-22 11:45:29 -0600311 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700312 archive="$(eval echo \$$package"_ARCHIVE")"
Stefan Reinauer699ac272015-06-05 12:43:28 -0700313
Martin Roth987d42d2018-07-22 11:45:29 -0600314 FILE=$(basename "$archive")
Stefan Reinauer88935482015-06-05 09:51:10 -0700315 printf " * $FILE "
316
Martin Roth987d42d2018-07-22 11:45:29 -0600317 if test -f "tarballs/$FILE"; then
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200318 printf "(cached)... "
zbaod22b9ea2015-08-30 23:35:16 -0400319 else
Paul Menzelb2ba3952015-06-12 11:58:14 +0200320 printf "(downloading from $archive)"
Martin Roth987d42d2018-07-22 11:45:29 -0600321 rm -f "tarballs/$FILE"
322 cd tarballs || exit 1
323 download_showing_percentage "$archive"
Stefan Reinauer88935482015-06-05 09:51:10 -0700324 cd ..
zbaod22b9ea2015-08-30 23:35:16 -0400325 fi
326
Martin Roth987d42d2018-07-22 11:45:29 -0600327 if [ ! -f "tarballs/$FILE" ]; then
Patrick Georgidf1ff232017-01-07 09:28:43 +0100328 printf "${RED}Failed to download $FILE.${NC}\n"
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700329 exit 1
330 fi
Stefan Reinauer88935482015-06-05 09:51:10 -0700331}
332
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200333# Compute the hash of the package given in $1, and print it raw (just the
334# hexadecimal hash).
335compute_hash() {
336 package=$1
Martin Roth987d42d2018-07-22 11:45:29 -0600337 # shellcheck disable=SC2086
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200338 archive="$(eval echo \$$package"_ARCHIVE")"
339 file="$(basename "$archive")"
340
341 if test -z "$CHECKSUM"; then
342 echo "${RED}\$CHECKSUM program missing. This is bad.${NC}" 1>&2
343 exit 1
344 fi
345
346 $CHECKSUM "tarballs/$file" 2>/dev/null | sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@'
347}
348
349error_hash_missing() {
350 package="$1"
Martin Roth987d42d2018-07-22 11:45:29 -0600351 # shellcheck disable=SC2086
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200352 archive="$(eval echo \$$package"_ARCHIVE")"
353 file="$(basename "$archive")"
354
355 fullhashfile="util/crossgcc/sum/$file.cksum"
356 printf "${RED}hash file missing:${NC}\n\n" 1>&2
357 printf "Please verify util/crossgcc/tarball/$file carefully\n" 1>&2
358 printf "(using PGP if possible), and then rename\n" 1>&2
359 printf " ${CYAN}${fullhashfile}.calc${NC}\n" 1>&2
360 printf " to ${CYAN}${fullhashfile}${NC}\n\n" 1>&2
361
362 exit 1
363}
364
365# Read the known hash file of the package given in $1, and print it raw.
366get_known_hash() {
367 package=$1
Martin Roth987d42d2018-07-22 11:45:29 -0600368 # shellcheck disable=SC2086
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200369 archive="$(eval echo \$$package"_ARCHIVE")"
370 file="$(basename "$archive")"
371 hashfile="sum/$file.cksum"
372
373 if [ ! -f "$hashfile" ]; then
374 calc_hash="$(compute_hash "$package")" || exit 1
375 echo "$calc_hash tarballs/$file" > "${hashfile}.calc"
376
377 error_hash_missing "$package"
378 exit 1
379 fi
380
Martin Roth987d42d2018-07-22 11:45:29 -0600381 sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@' < "$hashfile"
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200382}
383
384error_hash_mismatch() {
385 package=$1
386 known_hash="$2"
387 computed_hash="$3"
Martin Roth987d42d2018-07-22 11:45:29 -0600388 # shellcheck disable=SC2086
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200389 archive="$(eval echo \$$package"_ARCHIVE")"
390 file="$(basename "$archive")"
391
392 printf "${RED}hash mismatch:${NC}\n\n"
393 printf " expected (known) hash: $known_hash\n"
394 printf "calculated hash of downloaded file: $computed_hash\n\n"
395
396 printf "If you think this is due to a network error, please delete\n"
397 printf " ${CYAN}util/crossgcc/tarballs/$file${NC}\n"
398 printf "and try again. If the problem persists, it may be due to an\n"
399 printf "administration error on the file server, or you might be\n"
400 printf "subject to a Man-in-the-Middle attack\n\n"
401
402 exit 1
403}
404
405# verify_hash - Check that the hash of the file given in $1 matches the known
406# hash; Bail out on mismatch or missing hash file.
407verify_hash() {
408 package=$1
Martin Roth987d42d2018-07-22 11:45:29 -0600409 # shellcheck disable=SC2086
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200410 archive="$(eval echo \$$package"_ARCHIVE")"
411
412 known_hash="$(get_known_hash "$package")" || exit "$?"
413 computed_hash="$(compute_hash "$package")" || exit "$?"
414
415 if [ "$known_hash" != "$computed_hash" ]; then
416 error_hash_mismatch "$package" "$known_hash" "$computed_hash"
417 exit 1
418 fi
419
Jonathan Neuschäfercb76069e2018-10-01 09:45:49 +0200420 printf "${GREEN}hash verified (${known_hash})${NC}\n"
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200421}
422
Stefan Reinauer88935482015-06-05 09:51:10 -0700423unpack_and_patch() {
Martin Roth987d42d2018-07-22 11:45:29 -0600424 package="$1"
425 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700426 archive="$(eval echo \$$package"_ARCHIVE")"
Martin Roth987d42d2018-07-22 11:45:29 -0600427 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700428 dir="$(eval echo \$$package"_DIR")"
Martin Roth987d42d2018-07-22 11:45:29 -0600429 test -d "${dir}" && test -f "${dir}/.unpack_success" || (
430 printf " * $(basename "$archive")\n"
Stefan Reinauer88935482015-06-05 09:51:10 -0700431 FLAGS=zxf
Martin Roth987d42d2018-07-22 11:45:29 -0600432 suffix=$(echo "$archive" | sed 's,.*\.,,')
Martin Roth95f7b222016-03-20 12:38:48 -0600433 if [ "$suffix" = "gz" ] && [ -n "$PIGZ" ]; then FLAGS="-I pigz -xf"
434 elif [ "$suffix" = "gz" ]; then FLAGS=zxf
435 elif [ "$suffix" = "bz2" ] && [ -n "$LBZIP2" ]; then FLAGS="-I lbzip2 -xf"
436 elif [ "$suffix" = "bz2" ]; then FLAGS=jxf
437 elif [ "$suffix" = "xz" ]; then FLAGS="--xz -xf"
438 elif [ "$suffix" = "lzma" ]; then FLAGS="--lzma -xf"
439 fi
Martin Roth987d42d2018-07-22 11:45:29 -0600440 # shellcheck disable=SC2086
441 $TAR $FLAGS "tarballs/$(basename "$archive")"
Stefan Reinauer88935482015-06-05 09:51:10 -0700442 for patch in patches/${dir}_*.patch; do
Martin Roth987d42d2018-07-22 11:45:29 -0600443 test -r "$patch" || continue
444 printf " o $(basename "$patch")\n"
445 (cd "${dir}" || exit 1; $PATCH -s -N -p1 <"../${patch}") || {
Stefan Reinauer88935482015-06-05 09:51:10 -0700446 printf "\n${RED}Failed $patch.${NC}\n"
Stefan Reinauer916b8452015-06-09 11:54:49 -0700447 exit 1
448 }
Stefan Reinauer88935482015-06-05 09:51:10 -0700449 done
Martin Roth987d42d2018-07-22 11:45:29 -0600450 touch "${dir}/.unpack_success"
Stefan Reinauer88935482015-06-05 09:51:10 -0700451 )
452}
453
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700454fn_exists()
455{
Martin Roth987d42d2018-07-22 11:45:29 -0600456 # shellcheck disable=SC2039
457 type "$1" >/dev/null 2>&1
Patrick Georgi74c06422015-03-25 18:40:13 +0100458}
459
Patrick Georgif2c15f52015-06-11 21:07:31 +0200460is_package_enabled()
461{
462 echo "$PACKAGES" |grep -q "\<$1\>"
463}
464
Martin Roth444ece22016-04-01 18:46:29 -0600465package_uses_targetarch()
466{
Stefan Reinauera344b682016-04-21 19:29:51 -0700467 if [ "$1" = "GCC" ] || [ "$1" = "GDB" ] || [ "$1" = "BINUTILS" ]; then
Martin Roth444ece22016-04-01 18:46:29 -0600468 true
469 else
470 false
471 fi
472}
473
Nico Huber11ea2b32016-01-26 16:09:31 +0100474generic_build()
475{
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700476 package=$1
Nico Huber11ea2b32016-01-26 16:09:31 +0100477 host_target=$2
478 builddir=$3
479 success=$4
Martin Rothc295d5e2016-11-14 11:56:11 -0700480 version=$5
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700481
Martin Roth987d42d2018-07-22 11:45:29 -0600482 fn_exists "build_$package" || return
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700483
Nico Huber11ea2b32016-01-26 16:09:31 +0100484 mkdir -p "$builddir"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700485
Nico Huber11ea2b32016-01-26 16:09:31 +0100486 if [ -f "$success" ]; then
Martin Roth2c6a8062016-11-14 11:58:39 -0700487 printf "Skipping $package v$version for $host_target as it is already built\n"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700488 else
Martin Roth2c6a8062016-11-14 11:58:39 -0700489 printf "Building $package v$version for $host_target ... "
Nico Huber11ea2b32016-01-26 16:09:31 +0100490 DIR="$PWD"
Martin Roth987d42d2018-07-22 11:45:29 -0600491 cd "$builddir" || exit 1
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700492 rm -f .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600493 "build_${package}" "$host_target" > build.log 2>&1
494 cd "$DIR" || exit 1
Nico Huber11ea2b32016-01-26 16:09:31 +0100495 if [ ! -f "$builddir/.failed" ]; then
496 touch "$success";
497 else
Idwer Volleringe2ef3cf2017-08-15 00:53:13 +0200498 printf "${RED}failed${NC}. Check '$builddir/build.log'.\n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700499 exit 1
500 fi
501 printf "${green}ok${NC}\n"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700502 fi
503}
504
Nico Huber11ea2b32016-01-26 16:09:31 +0100505build_for_host()
506{
Martin Rothc295d5e2016-11-14 11:56:11 -0700507 package="$1"
Martin Roth987d42d2018-07-22 11:45:29 -0600508 # shellcheck disable=SC2086
Martin Rothc295d5e2016-11-14 11:56:11 -0700509 version="$(eval echo \$$package"_VERSION")"
Patrick Georgi1b593e52016-11-25 15:27:41 +0100510 generic_build "$package" host "build-$package" "${DESTDIR}${TARGETDIR}/.${package}.${version}.success" "$version"
Nico Huber11ea2b32016-01-26 16:09:31 +0100511}
512
513build_for_target()
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" target "build-${TARGETARCH}-$package" "${DESTDIR}${TARGETDIR}/.${TARGETARCH}-${package}.${version}.success" "$version"
Nico Huber11ea2b32016-01-26 16:09:31 +0100519}
520
521build()
522{
Martin Roth987d42d2018-07-22 11:45:29 -0600523 if package_uses_targetarch "$1"; then
524 if [ $BOOTSTRAP -eq 1 ] && [ ! -f "${DESTDIR}${TARGETDIR}/.GCC.${GCC_VERSION}.success" ]; then
Nico Huber234d2462016-01-26 16:10:17 +0100525 build_for_host GCC
526 fi
Martin Roth987d42d2018-07-22 11:45:29 -0600527 build_for_target "$1"
Nico Huber11ea2b32016-01-26 16:09:31 +0100528 else
Martin Roth987d42d2018-07-22 11:45:29 -0600529 build_for_host "$1"
Nico Huber11ea2b32016-01-26 16:09:31 +0100530 fi
531}
532
Nico Huber18c74d62017-06-24 20:35:59 +0200533exit_handler()
Zheng Baobb003c82016-08-05 13:41:51 +0800534{
535 printf "${NC}Stop\n"
536 exit 1
537}
538
Stefan Reinauer074d9132009-09-26 16:43:17 +0000539cleanup()
540{
Stefan Reinauer88e83e52016-04-06 15:39:48 -0700541 if [ $SAVETEMPS -ne 0 ]; then
542 printf "Leaving temporary files around... ${green}ok${NC}\n"
543 return
544 fi
545
Stefan Reinauer074d9132009-09-26 16:43:17 +0000546 printf "Cleaning up temporary files... "
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700547 for package in $PACKAGES; do
Martin Roth987d42d2018-07-22 11:45:29 -0600548 # shellcheck disable=SC2086
549 rm -rf "build-${TARGETARCH}-$package" "build-$package" "$(eval echo \$$package"_DIR")"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700550 done
zbaoff3f15c2015-08-26 22:00:31 -0400551 rm -f getopt
Stefan Reinauer074d9132009-09-26 16:43:17 +0000552 printf "${green}ok${NC}\n"
553}
554
555myhelp()
556{
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700557 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>] [-C] [-G] [-S]\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000558 printf " $0 [-V|--version]\n"
559 printf " $0 [-h|--help]\n\n"
560
561 printf "Options:\n"
562 printf " [-V|--version] print version number and exit\n"
563 printf " [-h|--help] print this help and exit\n"
564 printf " [-c|--clean] remove temporary files before build\n"
565 printf " [-t|--savetemps] don't remove temporary files after build\n"
Patrick Georgic1a75b12011-11-04 21:37:14 +0100566 printf " [-y|--ccache] Use ccache when building cross compiler\n"
Nico Huber152e78e2016-09-20 15:38:40 +0200567 printf " [-n|--nocolor] don't print color codes in output\n"
568 printf " [-u|--urls] print the urls for all packages\n"
Patrick Georgi73166c72010-04-14 20:42:42 +0000569 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
Martin Roth588c79d2016-03-20 12:03:20 -0600570 printf " [-s]--supported <tool> print supported version of a tool\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000571 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
Martin Roth588c79d2016-03-20 12:03:20 -0600572 printf " (defaults to $TARGETDIR)\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000573 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100574 printf " (for RPM builds, default unset)\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700575 printf " [-P|--package <package>] Build a specific package: GCC, CLANG, IASL, GDB\n"
576 printf " (defaults to $PACKAGE)\n"
577 printf "GCC specific options:\n"
Nico Huber234d2462016-01-26 16:10:17 +0100578 printf " [-b|--bootstrap] bootstrap the host compiler before building\n"
579 printf " the cross compiler\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700580 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
581 printf " (defaults to $TARGETARCH)\n"
Nico Huberbb313bf2015-09-08 12:30:27 +0200582 printf " [-l|--languages <languages>] comma separated list of target languages\n"
Nico Huber66c2c1a2016-09-20 13:09:29 +0200583 printf " (defaults to $DEFAULT_LANGUAGES)\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700584 printf "GDB specific options:\n"
585 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
586 printf " (defaults to $TARGETARCH)\n"
587 printf " [-S|--scripting] build scripting support for GDB\n\n"
Martin Rothd1da74f2015-06-21 12:06:28 -0600588 printf "Platforms for GCC & GDB:\n"
Julius Wernerf96d9052019-08-16 15:35:39 -0700589 printf " x86_64 i386-elf i386-mingw32 riscv-elf arm aarch64\n"
Jonathan Neuschäferf14f6402016-03-11 20:22:23 +0100590 printf " powerpc64le-linux-gnu nds32le-elf\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000591}
592
Martin Rothedf965a2015-11-25 16:58:33 -0700593printversion() {
594 printf "${blue}Welcome to the ${red}coreboot${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
595}
596
Stefan Reinauer074d9132009-09-26 16:43:17 +0000597myversion()
598{
Martin Rothedf965a2015-11-25 16:58:33 -0700599 printversion
Stefan Reinauer074d9132009-09-26 16:43:17 +0000600
601 cat << EOF
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000602Copyright (C) 2008-2010 by coresystems GmbH
Marc Jones2aac3f62011-08-08 16:07:50 -0600603Copyright (C) 2011 by Sage Electronic Engineering
Stefan Reinauer074d9132009-09-26 16:43:17 +0000604
605This program is free software; you can redistribute it and/or modify
606it under the terms of the GNU General Public License as published by
607the Free Software Foundation; version 2 of the License.
608
609This program is distributed in the hope that it will be useful,
610but WITHOUT ANY WARRANTY; without even the implied warranty of
611MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
612GNU General Public License for more details.
613
614EOF
615}
616
Nico Huber5f72f962016-01-31 23:05:19 +0100617have_hostcflags_from_gmp() {
Martin Roth987d42d2018-07-22 11:45:29 -0600618 grep -q __GMP_CFLAGS "$DESTDIR$TARGETDIR/include/gmp.h" >/dev/null 2>&1
Nico Huber5f72f962016-01-31 23:05:19 +0100619}
620
621set_hostcflags_from_gmp() {
622 # Now set CFLAGS to match GMP CFLAGS but strip out -pedantic
623 # as GCC 4.6.x fails if it's there.
Martin Roth987d42d2018-07-22 11:45:29 -0600624 HOSTCFLAGS="$(grep __GMP_CFLAGS "$DESTDIR$TARGETDIR/include/gmp.h" |cut -d\" -f2 |\
Idwer Volleringba349ab2016-07-24 02:10:19 +0200625 sed s,-pedantic,,)"
Martin Roth987d42d2018-07-22 11:45:29 -0600626 export HOSTCFLAGS
Nico Huber5f72f962016-01-31 23:05:19 +0100627}
628
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700629build_GMP() {
Nico Huber8bbd5962016-12-22 16:05:54 +0100630 # Check if GCC enables `-pie` by default (possible since GCC 6).
631 # We need PIC in all static libraries then.
Nico Huber2c1c02e2017-01-02 20:51:28 +0100632 if $CC -dumpspecs 2>/dev/null | grep -q '[{;][[:space:]]*:-pie\>'
Nico Huber8bbd5962016-12-22 16:05:54 +0100633 then
634 OPTIONS="$OPTIONS --with-pic"
635 fi
636
Martin Roth987d42d2018-07-22 11:45:29 -0600637 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200638 CC="$(hostcc host)" CXX="$(hostcxx host)" \
639 ../${GMP_DIR}/configure --disable-shared --enable-fat \
Martin Roth987d42d2018-07-22 11:45:29 -0600640 --prefix="$TARGETDIR" $OPTIONS \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700641 || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600642 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700643 $MAKE $JOBS || touch .failed
644 $MAKE install DESTDIR=$DESTDIR || touch .failed
645
646 normalize_dirs
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700647
Nico Huber5f72f962016-01-31 23:05:19 +0100648 set_hostcflags_from_gmp
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700649}
650
651build_MPFR() {
Martin Roth987d42d2018-07-22 11:45:29 -0600652 test "$UNAME" = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
Nico Huber08bb8372017-06-24 19:50:35 +0200653 CC="$(hostcc host)" CXX="$(hostcxx host)" \
Martin Roth987d42d2018-07-22 11:45:29 -0600654 ../${MPFR_DIR}/configure --disable-shared --prefix="$TARGETDIR" \
655 --infodir="$TARGETDIR/info" \
656 --with-gmp="$DESTDIR$TARGETDIR" CFLAGS="$HOSTCFLAGS" || \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700657 touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600658 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700659 $MAKE $JOBS || touch .failed
660 $MAKE install DESTDIR=$DESTDIR || touch .failed
661
662 normalize_dirs
663
664 # work around build problem of libgmp.la
665 if [ "$DESTDIR" != "" ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600666 perl -pi -e "s,$DESTDIR,," "$DESTDIR$TARGETDIR/lib/libgmp.la"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700667 fi
668}
669
670build_MPC() {
Nico Huber08bb8372017-06-24 19:50:35 +0200671 CC="$(hostcc host)" CXX="$(hostcxx host)" \
Martin Roth987d42d2018-07-22 11:45:29 -0600672 ../${MPC_DIR}/configure --disable-shared --prefix="$TARGETDIR" \
673 --infodir="$TARGETDIR/info" --with-mpfr="$DESTDIR$TARGETDIR" \
674 --with-gmp="$DESTDIR$TARGETDIR" CFLAGS="$HOSTCFLAGS" || \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700675 touch .failed
Patrick Georgi790aab62017-04-26 17:35:35 +0200676
677 # work around build problem of libmpfr.la
678 if [ "$DESTDIR" != "" ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600679 perl -pi -e "s,$TARGETDIR/lib/libgmp.la,$DESTDIR\$&," "$DESTDIR$TARGETDIR/lib/libmpfr.la"
Patrick Georgi790aab62017-04-26 17:35:35 +0200680 fi
681
Martin Roth987d42d2018-07-22 11:45:29 -0600682 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700683 $MAKE $JOBS || touch .failed
684 $MAKE install DESTDIR=$DESTDIR || touch .failed
685
Patrick Georgi790aab62017-04-26 17:35:35 +0200686 # work around build problem of libmpfr.la
687 if [ "$DESTDIR" != "" ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600688 perl -pi -e "s,$DESTDIR,," "$DESTDIR$TARGETDIR/lib/libmpfr.la"
Patrick Georgi790aab62017-04-26 17:35:35 +0200689 fi
690
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700691 normalize_dirs
692}
693
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700694build_BINUTILS() {
Nico Huberae6187f2016-12-22 22:16:39 +0100695 if [ $TARGETARCH = "x86_64-elf" ]; then
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700696 ADDITIONALTARGET=",i386-elf"
697 fi
Nico Huber08bb8372017-06-24 19:50:35 +0200698 CC="$(hostcc target)" CXX="$(hostcxx target)" \
Martin Roth987d42d2018-07-22 11:45:29 -0600699 ../binutils-${BINUTILS_VERSION}/configure --prefix="$TARGETDIR" \
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700700 --target=${TARGETARCH} --enable-targets=${TARGETARCH}${ADDITIONALTARGET} \
701 --disable-werror --disable-nls --enable-lto --enable-gold \
Stefan Reinauer2fe2d3d2016-04-05 02:14:48 -0700702 --enable-interwork --enable-multilib \
Vivia Nikolaidou81084362018-04-28 01:21:44 +0300703 --enable-plugins --enable-multilibs \
Nico Huber010ecf802016-12-22 16:19:37 +0100704 CFLAGS="$HOSTCFLAGS" \
705 CXXFLAGS="$HOSTCFLAGS" \
706 || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600707 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700708 $MAKE $JOBS || touch .failed
709 $MAKE install DESTDIR=$DESTDIR || touch .failed
710}
711
Nico Huber234d2462016-01-26 16:10:17 +0100712bootstrap_GCC() {
Martin Roth987d42d2018-07-22 11:45:29 -0600713 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200714 CC="$(hostcc host)" CXX="$(hostcxx host)" \
Nico Huber234d2462016-01-26 16:10:17 +0100715 CFLAGS="$HOSTCFLAGS" \
716 CFLAGS_FOR_BUILD="$HOSTCFLAGS" \
717 CFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
718 CXXFLAGS="$HOSTCFLAGS" \
719 CXXFLAGS_FOR_BUILD="$HOSTCFLAGS" \
720 CXXFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
721 ../gcc-${GCC_VERSION}/configure \
Martin Roth987d42d2018-07-22 11:45:29 -0600722 --prefix="$TARGETDIR" --libexecdir="$TARGETDIR/lib" \
Nico Huber234d2462016-01-26 16:10:17 +0100723 --enable-bootstrap \
724 --disable-werror --disable-nls \
725 --disable-shared --disable-multilib \
726 --disable-libssp --disable-libquadmath --disable-libcc1 \
Patrick Georgi97a9df42017-04-26 22:18:18 +0200727 --disable-libsanitizer \
Nico Huber234d2462016-01-26 16:10:17 +0100728 ${GCC_OPTIONS} --enable-languages="${LANGUAGES}" \
Martin Roth987d42d2018-07-22 11:45:29 -0600729 --with-gmp="$DESTDIR$TARGETDIR" --with-mpfr="$DESTDIR$TARGETDIR" \
730 --with-mpc="$DESTDIR$TARGETDIR" \
Nico Huber234d2462016-01-26 16:10:17 +0100731 --with-pkgversion="coreboot bootstrap v$CROSSGCC_VERSION $CROSSGCC_DATE" \
732 && \
Martin Roth987d42d2018-07-22 11:45:29 -0600733 # shellcheck disable=SC2086
Nico Huber234d2462016-01-26 16:10:17 +0100734 $MAKE $JOBS BOOT_CFLAGS="$HOSTCFLAGS" BUILD_CONFIG="" bootstrap && \
735 $MAKE install-gcc \
736 install-target-libgcc \
737 maybe-install-target-libada \
738 maybe-install-target-libstdc++-v3 \
Nico Huber08bb8372017-06-24 19:50:35 +0200739 DESTDIR=$DESTDIR || touch .failed
Nico Huber234d2462016-01-26 16:10:17 +0100740}
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700741
Nico Huber234d2462016-01-26 16:10:17 +0100742build_cross_GCC() {
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700743 # Work around crazy code generator in GCC that confuses CLANG.
Aaron Durbinb2229dc2016-04-07 16:58:10 -0500744 $CC --version | grep clang >/dev/null 2>&1 && \
Patrick Georgia7a5a562017-06-26 07:21:46 +0200745 CLANGFLAGS="-fbracket-depth=1024"
Nico Huber3f12d932017-08-31 23:11:18 +0200746 [ -n "$CXX" ] && $CXX --version | grep clang >/dev/null 2>&1 && \
Patrick Georgia7a5a562017-06-26 07:21:46 +0200747 CLANGCXXFLAGS="-fbracket-depth=1024"
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700748
Stefan Reinauerd0f83722015-06-09 11:52:28 -0700749 # GCC does not honor HOSTCFLAGS at all. CFLAGS are used for
750 # both target and host object files.
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700751 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
752 # but it does not seem to work properly. At least the host library
753 # libiberty is not compiled with CFLAGS_FOR_BUILD.
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700754 # Also set the CXX version of the flags because GCC is now compiled
755 # using C++.
Martin Roth987d42d2018-07-22 11:45:29 -0600756 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200757 CC="$(hostcc target)" CXX="$(hostcxx target)" \
Nico Huber3f12d932017-08-31 23:11:18 +0200758 CFLAGS_FOR_TARGET="-O2 -Dinhibit_libc" \
Patrick Georgia7a5a562017-06-26 07:21:46 +0200759 CFLAGS="$HOSTCFLAGS $CLANGFLAGS" \
760 CFLAGS_FOR_BUILD="$HOSTCFLAGS $CLANGFLAGS" \
761 CXXFLAGS="$HOSTCFLAGS $CLANGCXXFLAGS" \
762 CXXFLAGS_FOR_BUILD="$HOSTCFLAGS $CLANGCXXFLAGS" \
763 ../gcc-${GCC_VERSION}/configure \
Martin Roth987d42d2018-07-22 11:45:29 -0600764 --prefix="$TARGETDIR" --libexecdir="$TARGETDIR/lib" \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700765 --target=${TARGETARCH} --disable-werror --disable-shared \
Stefan Reinauer2f97ebc2015-06-09 12:24:54 -0700766 --enable-lto --enable-plugins --enable-gold --enable-ld=default \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700767 --disable-libssp --disable-bootstrap --disable-nls \
768 --disable-libquadmath --without-headers \
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100769 --disable-threads \
Patrick Georgi1fc02d12016-03-23 21:44:43 +0100770 --enable-interwork --enable-multilib --enable-targets=all \
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100771 --disable-libatomic --disable-libcc1 --disable-decimal-float \
Nico Huberbb313bf2015-09-08 12:30:27 +0200772 ${GCC_OPTIONS} --enable-languages="${LANGUAGES}" \
Ronald G. Minnich7ee16b72016-02-12 21:54:59 +0000773 --with-system-zlib \
Martin Roth987d42d2018-07-22 11:45:29 -0600774 --with-gmp="$DESTDIR$TARGETDIR" --with-mpfr="$DESTDIR$TARGETDIR" \
775 --with-mpc="$DESTDIR$TARGETDIR" \
Patrick Georgidce4d462018-09-07 01:00:54 +0200776 --with-gnu-as --with-gnu-ld \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700777 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE" \
Nico Huber659f40b2018-05-23 17:06:53 +0200778 && \
Patrick Georgi095db332018-06-26 21:00:58 +0200779 mkdir -p gcc/$TARGETARCH && \
Martin Roth987d42d2018-07-22 11:45:29 -0600780 ln -s "$DESTDIR$TARGETDIR/$TARGETARCH/bin" "gcc/$TARGETARCH/$GCC_VERSION" && \
Nico Huber659f40b2018-05-23 17:06:53 +0200781 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-gcc && \
Martin Roth987d42d2018-07-22 11:45:29 -0600782 $MAKE install-gcc DESTDIR="$DESTDIR" || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700783
Martin Roth987d42d2018-07-22 11:45:29 -0600784 if [ ! -f .failed ] && [ "$(echo $TARGETARCH | grep -c -- -mingw32)" -eq 0 ]; then
785 # shellcheck disable=SC2086
Nico Huber659f40b2018-05-23 17:06:53 +0200786 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-target-libgcc && \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700787 $MAKE install-target-libgcc DESTDIR=$DESTDIR || touch .failed
788 fi
789}
790
Nico Huber234d2462016-01-26 16:10:17 +0100791build_GCC() {
792 if [ "$1" = host ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600793 bootstrap_GCC "$1"
Nico Huber234d2462016-01-26 16:10:17 +0100794 else
Martin Roth987d42d2018-07-22 11:45:29 -0600795 build_cross_GCC "$1"
Nico Huber234d2462016-01-26 16:10:17 +0100796 fi
797}
798
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700799build_EXPAT() {
Nico Huber08bb8372017-06-24 19:50:35 +0200800 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS"
Martin Roth987d42d2018-07-22 11:45:29 -0600801 ../${EXPAT_DIR}/configure --disable-shared --prefix="$TARGETDIR" \
Nico Huber08bb8372017-06-24 19:50:35 +0200802 || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700803 $MAKE || touch .failed
804 $MAKE install DESTDIR=$DESTDIR || touch .failed
805
806 normalize_dirs
807}
808
809build_PYTHON() {
Nico Huber08bb8372017-06-24 19:50:35 +0200810 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS"
Martin Roth987d42d2018-07-22 11:45:29 -0600811 ../${PYTHON_DIR}/configure --prefix="$TARGETDIR" \
Stefan Reinauera344b682016-04-21 19:29:51 -0700812 || 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 DESTDIR=$DESTDIR || touch .failed
816
817 normalize_dirs
818}
819
820build_GDB() {
821 export PYTHONHOME=$DESTDIR$TARGETDIR
Martin Roth987d42d2018-07-22 11:45:29 -0600822 if [ "$UNAME" != "FreeBSD" ] && [ "$UNAME" != "NetBSD" ]; then
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700823 LIBDL="-ldl"
824 fi
825 LDFLAGS="-Wl,-rpath,\$\$ORIGIN/../lib/ -L$DESTDIR$TARGETDIR/lib \
826 -lpthread $LIBDL -lutil" \
Nico Huber08bb8372017-06-24 19:50:35 +0200827 CC="$(hostcc target)" CXX="$(hostcxx target)" \
828 CFLAGS="$HOSTCFLAGS -I$DESTDIR$TARGETDIR/include" \
Martin Roth987d42d2018-07-22 11:45:29 -0600829 ../${GDB_DIR}/configure --prefix="$TARGETDIR" \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700830 --target=${TARGETARCH} --disable-werror --disable-nls
Martin Roth987d42d2018-07-22 11:45:29 -0600831 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700832 $MAKE $JOBS || touch .failed
833 $MAKE install DESTDIR=$DESTDIR || touch .failed
834}
835
836build_IASL() {
837 RDIR=$PWD
Martin Roth987d42d2018-07-22 11:45:29 -0600838 cd ../$IASL_DIR/generate/unix || exit 1
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700839 CFLAGS="$HOSTCFLAGS"
840 HOST="_LINUX"
Martin Roth987d42d2018-07-22 11:45:29 -0600841 test "$UNAME" = "Darwin" && HOST="_APPLE"
842 test "$UNAME" = "FreeBSD" && HOST="_FreeBSD"
843 test "$UNAME" = "Cygwin" && HOST="_CYGWIN"
Nico Huber08bb8372017-06-24 19:50:35 +0200844 HOST="$HOST" CFLAGS="$CFLAGS" \
845 OPT_CFLAGS="-O -D_FORTIFY_SOURCE=2 -D COREBOOT_TOOLCHAIN_VERSION='\"coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE\"' " \
Elyes HAOUAS157851f2020-05-29 10:20:14 +0200846 $MAKE $JOBS CC="$(hostcc host)" iasl acpibin acpidump acpiexec acpihelp acpisrc acpixtract
847 rm -f "$DESTDIR$TARGETDIR/bin/{iasl,acpibin,acpidump,acpiexec,acpihelp,acpisrc,acpixtract}" || touch "$RDIR/.failed"
848 cp bin/{iasl,acpibin,acpidump,acpiexec,acpihelp,acpisrc,acpixtract} "$DESTDIR$TARGETDIR/bin" || touch "$RDIR/.failed"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700849}
850
851build_LLVM() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700852
Martin Roth987d42d2018-07-22 11:45:29 -0600853 cd .. || exit 1
Elyes HAOUAS8f1853c2020-08-02 09:46:15 +0200854 ln -sf "$PWD/$CLANG_DIR" "$LLVM_DIR/tools/clang"
Martin Roth987d42d2018-07-22 11:45:29 -0600855 ln -sf "$PWD/$CTE_DIR" "$LLVM_DIR/tools/clang/tools/extra"
856 ln -sf "$PWD/$CRT_DIR" "$LLVM_DIR/projects/compiler-rt"
857 cd - || exit 1
858
859 $CMAKE -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$DESTDIR$TARGETDIR" \
Martin Rothfb5647a2015-12-18 09:26:45 -0700860 -DCLANG_VENDOR="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE - " \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700861 -DCMAKE_BUILD_TYPE=Release ../$LLVM_DIR || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600862 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700863 $MAKE $JOBS || touch .failed
864 $MAKE install || touch .failed
865
Elyes HAOUAS8f1853c2020-08-02 09:46:15 +0200866 cp -a ../$CLANG_DIR/tools/scan-build/* "$DESTDIR$TARGETDIR/bin"
867 cp -a ../$CLANG_DIR/tools/scan-view/* "$DESTDIR$TARGETDIR/bin"
Stefan Reinauere9e6e3d2015-07-07 00:20:42 +0200868
869 # create symlinks to work around broken --print-librt-file-name
870 # when used with -target.
Martin Roth987d42d2018-07-22 11:45:29 -0600871 cd "$DESTDIR$TARGETDIR/lib/clang/${CLANG_VERSION}/lib" || exit 1
Stefan Reinauere9e6e3d2015-07-07 00:20:42 +0200872 for i in */libclang_rt.builtins*.a; do
Martin Roth987d42d2018-07-22 11:45:29 -0600873 ln -s "$i" .
Stefan Reinauere9e6e3d2015-07-07 00:20:42 +0200874 done
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700875}
876
Stefan Reinauer3b593492017-06-19 17:28:54 -0700877build_CMAKE() {
Nico Huber08bb8372017-06-24 19:50:35 +0200878 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS" \
Martin Roth987d42d2018-07-22 11:45:29 -0600879 ../${CMAKE_DIR}/configure --prefix="$TARGETDIR" \
Stefan Reinauer3b593492017-06-19 17:28:54 -0700880 || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600881 # shellcheck disable=SC2086
Stefan Reinauer3b593492017-06-19 17:28:54 -0700882 $MAKE $JOBS || touch .failed
883 $MAKE install DESTDIR=$DESTDIR || touch .failed
884
885 normalize_dirs
886}
887
Martin Rothd70f5fa2019-05-26 17:24:19 -0600888build_NASM() {
889 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS"
890 ../${NASM_DIR}/configure --prefix="$TARGETDIR" \
891 || touch .failed
892 # shellcheck disable=SC2086
893 $MAKE $JOBS || touch .failed
894 $MAKE install DESTDIR=$DESTDIR || touch .failed
895
896 normalize_dirs
897}
898
Stefan Reinauerc66fd3c2017-06-21 14:57:28 -0700899print_supported() {
900 case "$PRINTSUPPORTED" in
Martin Rothedf965a2015-11-25 16:58:33 -0700901 AUTOCONF|autoconf) printf "%s\n" "$GCC_AUTOCONF_VERSION";;
902 BINUTILS|binutils) printf "%s\n" "$BINUTILS_VERSION";;
903 CLANG|clang) printf "%s\n" "$CLANG_VERSION";;
904 EXPAT|expat) printf "%s\n" "$EXPAT_VERSION";;
905 GCC|gcc) printf "%s\n" "$GCC_VERSION";;
906 GDB|gdb) printf "%s\n" "$GDB_VERSION";;
907 GMP|gmp) printf "%s\n" "$GMP_VERSION";;
908 IASL|iasl) printf "%s\n" "$IASL_VERSION";;
Martin Rothedf965a2015-11-25 16:58:33 -0700909 MPC|mpc) printf "%s\n" "$MPC_VERSION";;
910 MPFR|mpfr) printf "%s\n" "$MPFR_VERSION";;
911 PYTHON|python) printf "%s\n" "$PYTHON_VERSION";;
Martin Rothd70f5fa2019-05-26 17:24:19 -0600912 NASM|nasm) printf "%s\n" "${NASM_VERSION}";;
Stefan Reinauerc66fd3c2017-06-21 14:57:28 -0700913 *) printf "Unknown tool %s\n" "$PRINTSUPPORTED";;
Martin Rothedf965a2015-11-25 16:58:33 -0700914 esac
915}
Stefan Reinauer074d9132009-09-26 16:43:17 +0000916
Nico Huber18c74d62017-06-24 20:35:59 +0200917trap exit_handler 1 2 3 15
Zheng Baobb003c82016-08-05 13:41:51 +0800918
Patrick Georgi6bba29f2010-01-29 17:40:52 +0000919# Look if we have getopt. If not, build it.
920export PATH=$PATH:.
921getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
922
Stefan Reinauer074d9132009-09-26 16:43:17 +0000923# parse parameters.. try to find out whether we're running GNU getopt
Nico Huber27e8412b2017-12-05 22:02:21 +0100924getoptbrand="$(getopt -V 2>/dev/null | sed -e '1!d' -e 's,^\(......\).*,\1,')"
Peter Stuge09377b72011-08-21 06:24:55 +0200925if [ "${getoptbrand}" = "getopt" ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000926 # Detected GNU getopt that supports long options.
Patrick Georgi9681cdc2017-09-22 18:57:10 +0200927 args=$(getopt -l version,help,clean,directory:,bootstrap,bootstrap-only,platform:,languages:,package:,jobs:,destdir:,savetemps,scripting,ccache,supported:,urls,nocolor -o Vhcd:bBp:l:P:j:D:tSys:un -- "$@")
Nico Huber78df0bf2016-09-20 14:11:53 +0200928 getopt_ret=$?
Nico Hubercc414dd2016-09-20 13:59:38 +0200929 eval set -- "$args"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000930else
931 # Detected non-GNU getopt
Patrick Georgi9681cdc2017-09-22 18:57:10 +0200932 args=$(getopt Vhcd:bBp:l:P:j:D:tSys:un $*)
Nico Huber78df0bf2016-09-20 14:11:53 +0200933 getopt_ret=$?
Martin Roth987d42d2018-07-22 11:45:29 -0600934 # shellcheck disable=SC2086
Stefan Reinauer074d9132009-09-26 16:43:17 +0000935 set -- $args
936fi
937
Nico Huber78df0bf2016-09-20 14:11:53 +0200938if [ $getopt_ret != 0 ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000939 myhelp
940 exit 1
941fi
942
943while true ; do
944 case "$1" in
Patrick Georgi9681cdc2017-09-22 18:57:10 +0200945 -V|--version) shift; myversion; exit 0;;
946 -h|--help) shift; myhelp; exit 0;;
947 -c|--clean) shift; clean=1;;
948 -t|--savetemps) shift; SAVETEMPS=1;;
949 -d|--directory) shift; TARGETDIR="$1"; shift;;
950 -b|--bootstrap) shift; BOOTSTRAP=1;;
951 -B|--bootstrap-only) shift; BOOTSTRAPONLY=1; BOOTSTRAP=1;;
952 -p|--platform) shift; TARGETARCH="$1"; shift;;
953 -l|--languages) shift; LANGUAGES="$1"; shift;;
954 -D|--destdir) shift; DESTDIR="$1"; shift;;
955 -j|--jobs) shift; THREADS="$1"; JOBS="-j $1"; shift;;
956 -P|--package) shift; PACKAGE="$1"; shift;;
957 -S|--scripting) shift; SKIPPYTHON=0;;
958 -y|--ccache) shift; USECCACHE=1;;
959 -s|--supported) shift; PRINTSUPPORTED="$1"; shift;;
960 -u|--urls) shift; printf "%s\n" "$ALL_ARCHIVES"; exit 0;;
961 -n|--nocolor) shift; \
Martin Roth4a53db02016-03-01 15:21:07 -0700962 unset red RED green GREEN blue BLUE cyan CYAN NC;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200963 --) shift; break;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200964 *) break;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000965 esac
966done
967
Nico Huberbd74d562016-09-20 14:16:43 +0200968if [ $# -gt 0 ]; then
969 printf "Excessive arguments: $*\n"
970 myhelp
971 exit 1
972fi
973
Stefan Reinauerc66fd3c2017-06-21 14:57:28 -0700974if [ -n "$PRINTSUPPORTED" ]; then
975 print_supported
Martin Rothedf965a2015-11-25 16:58:33 -0700976 exit 0
977fi
978
979#print toolchain builder version string as the header
980printversion
981
Martin Roth2c6a8062016-11-14 11:58:39 -0700982printf "Building toolchain using %d thread(s).\n\n" "$THREADS"
983
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700984case "$TARGETARCH" in
Timothy Pearsona9f62352015-12-30 18:59:00 -0600985 x86_64-elf) ;;
986 x86_64*) TARGETARCH=x86_64-elf;;
987 i386-elf) ;;
988 i386-mingw32) ;;
Iru Caiae8e3a02016-04-02 10:50:59 +0800989 riscv-elf) TARGETARCH=riscv64-elf;;
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100990 powerpc64*-linux*) ;;
Timothy Pearsona9f62352015-12-30 18:59:00 -0600991 i386*) TARGETARCH=i386-elf;;
Stefan Reinauer27522ad2016-01-29 17:31:34 -0800992 arm*) TARGETARCH=arm-eabi;;
Timothy Pearsona9f62352015-12-30 18:59:00 -0600993 aarch64*) TARGETARCH=aarch64-elf;;
Martin Roth3e0f74d2016-03-08 12:07:04 -0700994 nds32le-elf) ;;
Timothy Pearsona9f62352015-12-30 18:59:00 -0600995 *) printf "${red}WARNING: Unsupported architecture $TARGETARCH.${NC}\n\n"; ;;
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700996esac
997
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700998# Figure out which packages to build
Stefan Reinauer699ac272015-06-05 12:43:28 -0700999
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001000case "$PACKAGE" in
1001 GCC|gcc)
Stefan Reinauer81693d42017-06-21 15:25:36 -07001002 echo "Target architecture is $TARGETARCH"
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001003 NAME="${TARGETARCH} cross GCC"
Nico Huberafda56e2017-12-07 17:50:53 +01001004 PACKAGES="GMP MPFR MPC BINUTILS GCC"
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001005 ;;
1006 GDB|gdb)
1007 NAME="${TARGETARCH} cross GDB"
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001008 if [ $SKIPPYTHON -eq 0 ]; then
Ronald G. Minnichfb944f42016-02-27 04:06:42 +00001009 PACKAGES="EXPAT PYTHON GDB"
1010 else
1011 PACKAGES="EXPAT GDB"
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001012 fi
1013 ;;
1014 CLANG|clang)
Stefan Reinauer81693d42017-06-21 15:25:36 -07001015 NAME="LLVM clang"
1016 LLVM_VERSION=${CLANG_VERSION}
Elyes HAOUAS8f1853c2020-08-02 09:46:15 +02001017 PACKAGES="CMAKE LLVM CLANG CRT CTE"
Stefan Reinauerca117e72017-06-19 17:29:08 -07001018 CMAKE=${DESTDIR}${TARGETDIR}/bin/cmake
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001019 ;;
1020 IASL|iasl)
1021 NAME="IASL ACPI compiler"
1022 PACKAGES=IASL
1023 ;;
Stefan Reinauer3b593492017-06-19 17:28:54 -07001024 CMAKE|cmake)
1025 NAME="CMake"
1026 PACKAGES=CMAKE
1027 ;;
Martin Rothd70f5fa2019-05-26 17:24:19 -06001028 NASM|nasm)
1029 NAME="NASM"
1030 PACKAGES=NASM
1031 ;;
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001032 *)
Elyes HAOUAS91fb1392020-01-20 15:33:11 +01001033 printf "${red}ERROR: Unsupported package $PACKAGE. (Supported packages are GCC, GDB, CLANG, IASL, and NASM)${NC}\n\n";
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001034 exit 1
1035 ;;
1036esac
Peter Stugeceacd772011-06-09 04:54:16 +02001037
Stefan Reinauer66e93352015-06-16 22:38:23 +02001038# Find all the required tools:
1039
1040TAR=$(searchtool tar) || exit $?
1041PATCH=$(searchtool patch) || exit $?
1042MAKE=$(searchtool make) || exit $?
1043SHA1SUM=$(searchtool sha1sum)
zbao5cf758d2015-09-01 22:28:57 -04001044#SHA512SUM=$(searchtool sha512sum)
1045#MD5SUM=$(searchtool md5sum)
Stefan Reinauer66e93352015-06-16 22:38:23 +02001046CHECKSUM=$SHA1SUM
Martin Roth95f7b222016-03-20 12:38:48 -06001047LBZIP2=$(searchtool lbzip2 "" nofail)
1048PIGZ=$(searchtool pigz "" nofail)
Stefan Reinauer66e93352015-06-16 22:38:23 +02001049
1050searchtool m4 > /dev/null
1051searchtool bison > /dev/null
1052searchtool flex flex > /dev/null
Stefan Reinauer66e93352015-06-16 22:38:23 +02001053searchtool bzip2 "bzip2," > /dev/null
Nico Huber0ff3b392017-03-12 16:07:33 +01001054searchtool xz "XZ Utils" "" "xz-utils" > /dev/null
Stefan Reinauer66e93352015-06-16 22:38:23 +02001055
Patrick Georgidf1ff232017-01-07 09:28:43 +01001056if searchtool wget "GNU" nofail > /dev/null; then
1057 download_showing_percentage() {
1058 url=$1
Stefan Reinauer81693d42017-06-21 15:25:36 -07001059 printf "... ${red} 0%%"
Martin Roth82a30a12020-11-07 15:17:45 -07001060 wget --tries=3 "$url" 2>&1 | while read -r line; do
Martin Roth987d42d2018-07-22 11:45:29 -06001061 echo "$line" | grep -o "[0-9]\+%" | awk '{printf("\b\b\b\b%4s", $1)}'
Patrick Georgidf1ff232017-01-07 09:28:43 +01001062 done
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +02001063 printf "${NC}... "
Patrick Georgidf1ff232017-01-07 09:28:43 +01001064 }
1065elif searchtool curl "^curl " > /dev/null; then
1066 download_showing_percentage() {
1067 url=$1
1068 echo
Idwer Vollering5190f422020-11-24 15:13:16 +01001069 curl -O --progress-bar --location --retry 3 "$url"
Patrick Georgidf1ff232017-01-07 09:28:43 +01001070 }
1071fi
1072
Nico Huber08bb8372017-06-24 19:50:35 +02001073# Allow $CC override from the environment.
1074if [ -n "$CC" ]; then
1075 if [ ! -x "$(command -v "$CC" 2>/dev/null)" ]; then
1076 printf "${RED}ERROR:${red} CC is set to '%s' but wasn't found.${NC}\n" "$CC"
1077 HALT_FOR_TOOLS=1
1078 fi
1079else
Patrick Georgi8f2cdad2017-07-10 11:48:34 +02001080 if searchtool gnatgcc "Free Software Foundation" nofail > /dev/null; then
1081 CC=gnatgcc
1082 elif searchtool gcc "Free Software Foundation" nofail > /dev/null; then
Nico Huber08bb8372017-06-24 19:50:35 +02001083 CC=gcc
1084 else
1085 searchtool cc '^' nofail > /dev/null || please_install gcc
1086 CC=cc
1087 fi
1088fi
Martin Roth591790f2016-03-20 20:39:04 -06001089
Nico Huber08bb8372017-06-24 19:50:35 +02001090# We can leave $CXX empty if it's not set since *buildgcc* never
1091# calls it directly. This way configure scripts can search for
1092# themselves and we still override it when a bootstrapped g++ is
1093# to be used (cf. hostcxx()).
1094if [ -n "$CXX" ]; then
1095 if [ ! -x "$(command -v "$CXX" 2>/dev/null)" ]; then
1096 printf "${RED}ERROR:${red} CXX is set to '%s' but wasn't found.${NC}\n" "$CXX"
1097 HALT_FOR_TOOLS=1
1098 fi
1099else
1100 searchtool g++ "Free Software Foundation" nofail > /dev/null || \
1101 searchtool clang "clang version" nofail > /dev/null || \
1102 searchtool clang "LLVM" "" "g++" > /dev/null
1103fi
1104
1105check_for_library "-lz" "zlib (zlib1g-dev or zlib-devel)"
Nico Huber156d87c2016-09-20 12:59:53 +02001106
Martin Roth88463822016-01-07 11:03:36 -07001107if [ "$HALT_FOR_TOOLS" -ne 0 ]; then
1108 exit 1
1109fi
1110
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001111# This initial cleanup is useful when updating the toolchain script.
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001112
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001113if [ "$clean" = "1" ]; then
1114 cleanup
1115fi
Stefan Reinauer074d9132009-09-26 16:43:17 +00001116
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001117# Set up host compiler and flags needed for various OSes
Stefan Reinauer074d9132009-09-26 16:43:17 +00001118
Patrick Georgif2c15f52015-06-11 21:07:31 +02001119if is_package_enabled "GCC"; then
Patrick Georgie8253fe2017-07-18 11:35:35 +02001120# sane preset: let the configure script figure out things by itself
1121# more importantly, avoid any values that might already linger in the variable
1122OPTIONS="ABI="
Martin Roth987d42d2018-07-22 11:45:29 -06001123if [ "$UNAME" = "Darwin" ]; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001124 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
1125
1126 # generally the OS X compiler can create x64 binaries.
1127 # Per default it generated i386 binaries in 10.5 and x64
1128 # binaries in 10.6 (even if the kernel is 32bit)
1129 # For some weird reason, 10.5 autodetects an ABI=64 though
1130 # so we're setting the ABI explicitly here.
Martin Roth987d42d2018-07-22 11:45:29 -06001131 if [ "$(sysctl -n hw.optional.x86_64 2>/dev/null)" -eq 1 ] 2>/dev/null; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001132 OPTIONS="ABI=64"
1133 else
1134 OPTIONS="ABI=32"
1135 fi
Stefan Reinauer9491b4d2011-10-11 22:37:59 -07001136
Stefan Reinauer4266b922012-12-05 16:18:32 -08001137 # In Xcode 4.5.2 the default compiler is clang.
1138 # However, this compiler fails to compile gcc 4.7.x. As a
1139 # workaround it's possible to compile gcc with llvm-gcc.
Stefan Reinauer9491b4d2011-10-11 22:37:59 -07001140 if $CC -v 2>&1 | grep -q LLVM; then
Stefan Reinauer4266b922012-12-05 16:18:32 -08001141 CC=llvm-gcc
Stefan Reinauer9491b4d2011-10-11 22:37:59 -07001142 fi
Martin Roth987d42d2018-07-22 11:45:29 -06001143elif [ "$UNAME" = "Linux" ] || [ "$UNAME" = "Cygwin" ]; then
Patrick Georgiddb8f802015-07-04 17:45:54 +02001144 # gmp is overeager with detecting 64bit CPUs even if they run
1145 # a 32bit kernel and userland.
1146 if [ "$(uname -m 2>/dev/null)" = "i686" ]; then
1147 OPTIONS="ABI=32"
1148 fi
Martin Roth987d42d2018-07-22 11:45:29 -06001149elif [ "$UNAME" = "NetBSD" ]; then
Nico Huberde45c592016-01-20 23:22:33 +01001150 # same for NetBSD but this one reports an i386
1151 if [ "$(uname -m 2>/dev/null)" = "i386" ]; then
1152 OPTIONS="ABI=32"
1153 fi
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001154fi
Nico Huber66c2c1a2016-09-20 13:09:29 +02001155if [ -z "${LANGUAGES}" ]; then
Nico Hubercd87e1b2017-06-24 20:35:59 +02001156 if have_gnat; then
Nico Huber66c2c1a2016-09-20 13:09:29 +02001157 printf "\nFound compatible Ada compiler, enabling Ada support by default.\n\n"
1158 LANGUAGES="ada,${DEFAULT_LANGUAGES}"
1159 else
Nico Hubercd87e1b2017-06-24 20:35:59 +02001160 printf "\n${red}WARNING${NC}\n"
1161 printf "No compatible Ada compiler (GNAT) found. You can continue without\n"
1162 printf "Ada support, but this will limit the features of ${blue}coreboot${NC} (e.g.\n"
1163 printf "native graphics initialization won't be available on most Intel\n"
1164 printf "boards).\n\n"
1165
1166 printf "Usually, you can install GNAT with your package management system\n"
1167 printf "(the package is called \`gnat\` or \`gcc-ada\`). It has to match the\n"
1168 printf "\`gcc\` package in version. If there are multiple versions of GCC in-\n"
1169 printf "stalled, you can point this script to the matching version through\n"
1170 printf "the \`CC\` and \`CXX\` environment variables.\n\n"
1171
1172 printf "e.g. on Ubuntu 14.04, if \`gcc\` is \`gcc-4.8\`:\n"
1173 printf " apt-get install gnat-4.8 && make crossgcc\n\n"
1174
1175 printf "on Ubuntu 16.04, if \`gcc\` is \`gcc-5\`:\n"
1176 printf " apt-get install gnat-5 && make crossgcc\n"
1177 timeout 30
Nico Huber66c2c1a2016-09-20 13:09:29 +02001178 LANGUAGES="${DEFAULT_LANGUAGES}"
1179 fi
1180fi
Patrick Georgif2741aa2020-07-06 16:35:56 +02001181if [ "$BOOTSTRAP" != 1 ] && \
1182 { [ "$(hostcc_major)" -lt 4 ] || \
1183 { [ "$(hostcc_major)" -eq 4 ] && \
1184 [ "$(hostcc_minor)" -lt 9 ] ; } ; }
1185then
1186 printf "\n${red}WARNING${NC}\n"
1187 printf "Building coreboot requires a host compiler newer than 4.9.x while\n"
1188 printf "yours is $(hostcc_version).\n"
1189 printf "Enabling bootstrapping to provide a sufficiently new compiler:\n"
1190 printf "This will take significantly longer than a usual build.\n"
1191 printf "Alternatively you can abort now and update your host compiler.\n"
1192 timeout 15
1193 BOOTSTRAP=1
1194fi
Nico Hubercd87e1b2017-06-24 20:35:59 +02001195if ada_requested; then
Patrick Georgif2741aa2020-07-06 16:35:56 +02001196 if ! have_gnat; then
Nico Hubercd87e1b2017-06-24 20:35:59 +02001197 please_install gnat gcc-ada
1198 exit 1
1199 fi
Nico Hubercd87e1b2017-06-24 20:35:59 +02001200fi
Stefan Reinauer14ce2132015-06-05 13:01:13 -07001201fi # GCC
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001202
Nico Huber5f72f962016-01-31 23:05:19 +01001203export HOSTCFLAGS="-Os"
1204if have_hostcflags_from_gmp; then
1205 set_hostcflags_from_gmp
1206fi
1207
Patrick Georgic1a75b12011-11-04 21:37:14 +01001208if [ "$USECCACHE" = 1 ]; then
1209 CC="ccache $CC"
1210fi
1211
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001212# Prepare target directory for building GCC
1213# (dependencies must be in the PATH)
Martin Roth987d42d2018-07-22 11:45:29 -06001214mkdir -p "$DESTDIR$TARGETDIR/bin"
1215mkdir -p "$DESTDIR$TARGETDIR/share"
Stefan Reinauer16bd7892012-12-07 23:57:01 +01001216export PATH=$DESTDIR$TARGETDIR/bin:$PATH
1217
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001218# Download, unpack, patch and build all packages
1219
Idwer Vollering458e7df2020-09-25 12:26:51 +02001220printf "Downloading and verifying tarballs ...\n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001221mkdir -p tarballs
1222for P in $PACKAGES; do
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +02001223 download "$P" || exit "$?"
1224 verify_hash "$P" || exit "$?"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001225done
Martin Roth03625172016-02-17 14:44:14 -07001226printf "Downloaded tarballs ... ${green}ok${NC}\n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001227
Elyes HAOUASb0f19882018-06-09 11:59:00 +02001228printf "Unpacking and patching ...\n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001229for P in $PACKAGES; do
1230 unpack_and_patch $P || exit 1
1231done
1232printf "Unpacked and patched ... ${green}ok${NC}\n"
1233
Patrick Georgi9681cdc2017-09-22 18:57:10 +02001234if [ -n "$BOOTSTRAPONLY" ]; then
Elyes HAOUASb0f19882018-06-09 11:59:00 +02001235 printf "Building bootstrap compiler only ...\n"
Nico Huberafda56e2017-12-07 17:50:53 +01001236 for pkg in GMP MPFR MPC GCC; do
Patrick Georgi9681cdc2017-09-22 18:57:10 +02001237 build_for_host $pkg
1238 done
1239 exit 0
1240fi
1241
Elyes HAOUASb0f19882018-06-09 11:59:00 +02001242printf "Building packages ...\n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -07001243for package in $PACKAGES; do
1244 build $package
1245done
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001246printf "Packages built ... ${green}ok${NC}\n"
Stefan Reinauer14ce2132015-06-05 13:01:13 -07001247
Stefan Reinauer699ac272015-06-05 12:43:28 -07001248# Adding git information of current tree to target directory
1249# for reproducibility
Stefan Reinauerd7649122015-06-09 11:44:25 -07001250PROGNAME=$(basename "$0")
Stefan Reinauer88352d72016-04-06 15:57:03 -07001251rm -f "$DESTDIR$TARGETDIR/share/$PROGNAME-*"
Patrick Georgi4eb84cf2017-07-07 11:18:46 +02001252cp "$PROGNAME" "$DESTDIR$TARGETDIR/share/$PROGNAME-$CROSSGCC_VERSION-$CROSSGCC_COMMIT"
Zheng Bao30b895f2013-02-06 18:04:40 +08001253
Patrick Georgi0c2df6f2017-11-22 16:14:09 -05001254# Adding edk2 tools template
1255mkdir -p "$DESTDIR$TARGETDIR/share/edk2config"
1256sed -e "s,@@PREFIX@@,$TARGETDIR,g" edk2tools.txt > "$DESTDIR$TARGETDIR/share/edk2config/tools_def.txt"
1257printf "Copied EDK2 tools template ... ${green}ok${NC}\n"
1258
Stefan Reinauer88e83e52016-04-06 15:39:48 -07001259cleanup
Stefan Reinauer074d9132009-09-26 16:43:17 +00001260
Stefan Reinauer81693d42017-06-21 15:25:36 -07001261printf "\n${green}You can now run $NAME from $TARGETDIR.${NC}\n"