blob: 8770e02813cf71489b09da42aa9cef28856b7770 [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
Iru Cai03353de2016-10-29 23:37:42 +080036GMP_VERSION=6.1.2
Elyes HAOUASc3c9afb2019-02-03 15:51:42 +010037MPFR_VERSION=4.0.2
Stefan Reinaueraedb1df2018-10-02 16:00:22 -070038MPC_VERSION=1.1.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 HAOUAS18ecdbf2019-10-14 10:16:29 +020041BINUTILS_VERSION=2.33.1
Elyes HAOUAS23686812019-09-21 12:23:16 +020042GDB_VERSION=8.3.1
Elyes HAOUAS65718762020-02-04 07:33:47 +010043IASL_VERSION=20200110
Elyes HAOUASa87a7412019-12-19 11:50:43 +010044PYTHON_VERSION=3.8.1
Elyes HAOUASa2fbddf2019-10-03 10:38:39 +020045EXPAT_VERSION=2.2.9
Stefan Reinauer14ce2132015-06-05 13:01:13 -070046# CLANG version number
Elyes HAOUAS3ba84c52019-09-20 09:01:53 +020047CLANG_VERSION=9.0.0
Martin Roth589ef9d2016-08-11 18:35:06 -060048MAKE_VERSION=4.2.1
Elyes HAOUAS40f539f2020-01-03 16:09:35 +010049CMAKE_VERSION=3.16.2
Martin Rothd70f5fa2019-05-26 17:24:19 -060050NASM_VERSION=2.14.02
Stefan Reinauer074d9132009-09-26 16:43:17 +000051
Stefan Reinauer699ac272015-06-05 12:43:28 -070052# GCC toolchain archive locations
Martin Roth1484c032016-03-08 09:37:14 -070053# These are sanitized by the jenkins toolchain test builder, so if
54# a completely new URL is added here, it probably needs to be added
55# to the jenkins build as well, or the builder won't download it.
Doug Gale43ae4332017-09-27 22:10:16 -040056GMP_ARCHIVE="https://ftpmirror.gnu.org/gmp/gmp-${GMP_VERSION}.tar.xz"
57MPFR_ARCHIVE="https://ftpmirror.gnu.org/mpfr/mpfr-${MPFR_VERSION}.tar.xz"
58MPC_ARCHIVE="https://ftpmirror.gnu.org/mpc/mpc-${MPC_VERSION}.tar.gz"
Patrick Georgib1d26f02018-05-02 17:13:34 +020059GCC_ARCHIVE="https://ftpmirror.gnu.org/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.xz"
Iru Cai776b5ba2017-07-28 21:40:34 +080060BINUTILS_ARCHIVE="https://ftpmirror.gnu.org/binutils/binutils-${BINUTILS_VERSION}.tar.xz"
Doug Gale43ae4332017-09-27 22:10:16 -040061GDB_ARCHIVE="https://ftpmirror.gnu.org/gdb/gdb-${GDB_VERSION}.tar.xz"
Patrick Georgi85bb9462016-02-11 17:22:16 +010062IASL_ARCHIVE="https://acpica.org/sites/acpica/files/acpica-unix2-${IASL_VERSION}.tar.gz"
Doug Gale43ae4332017-09-27 22:10:16 -040063PYTHON_ARCHIVE="https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz"
64EXPAT_ARCHIVE="https://downloads.sourceforge.net/sourceforge/expat/expat-${EXPAT_VERSION}.tar.bz2"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070065# CLANG toolchain archive locations
Doug Gale43ae4332017-09-27 22:10:16 -040066LLVM_ARCHIVE="https://releases.llvm.org/${CLANG_VERSION}/llvm-${CLANG_VERSION}.src.tar.xz"
67CFE_ARCHIVE="https://releases.llvm.org/${CLANG_VERSION}/cfe-${CLANG_VERSION}.src.tar.xz"
68CRT_ARCHIVE="https://releases.llvm.org/${CLANG_VERSION}/compiler-rt-${CLANG_VERSION}.src.tar.xz"
69CTE_ARCHIVE="https://releases.llvm.org/${CLANG_VERSION}/clang-tools-extra-${CLANG_VERSION}.src.tar.xz"
70MAKE_ARCHIVE="https://ftpmirror.gnu.org/make/make-${MAKE_VERSION}.tar.bz2"
Elyes HAOUAS18315db2019-11-26 19:12:21 +010071CMAKE_ARCHIVE="https://cmake.org/files/v3.16/cmake-${CMAKE_VERSION}.tar.gz"
Martin Rothd70f5fa2019-05-26 17:24:19 -060072NASM_ARCHIVE="https://www.nasm.us/pub/nasm/releasebuilds/${NASM_VERSION}/nasm-${NASM_VERSION}.tar.bz2"
Stefan Reinauer074d9132009-09-26 16:43:17 +000073
Nico Huberafda56e2017-12-07 17:50:53 +010074ALL_ARCHIVES="$GMP_ARCHIVE $MPFR_ARCHIVE $MPC_ARCHIVE \
Martin Roth58c68d52016-03-01 15:22:42 -070075 $GCC_ARCHIVE $BINUTILS_ARCHIVE $GDB_ARCHIVE $IASL_ARCHIVE \
76 $PYTHON_ARCHIVE $EXPAT_ARCHIVE $LLVM_ARCHIVE $CFE_ARCHIVE \
Martin Rothd70f5fa2019-05-26 17:24:19 -060077 $CRT_ARCHIVE $CTE_ARCHIVE $MAKE_ARCHIVE $CMAKE_ARCHIVE \
78 $NASM_ARCHIVE"
Martin Roth58c68d52016-03-01 15:22:42 -070079
Stefan Reinauer699ac272015-06-05 12:43:28 -070080# GCC toolchain directories
Stefan Reinauer074d9132009-09-26 16:43:17 +000081GMP_DIR="gmp-${GMP_VERSION}"
82MPFR_DIR="mpfr-${MPFR_VERSION}"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000083MPC_DIR="mpc-${MPC_VERSION}"
Martin Roth987d42d2018-07-22 11:45:29 -060084# shellcheck disable=SC2034
Stefan Reinauer074d9132009-09-26 16:43:17 +000085GCC_DIR="gcc-${GCC_VERSION}"
Martin Roth987d42d2018-07-22 11:45:29 -060086# shellcheck disable=SC2034
Stefan Reinauer074d9132009-09-26 16:43:17 +000087BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
88GDB_DIR="gdb-${GDB_VERSION}"
Patrick Georgi85bb9462016-02-11 17:22:16 +010089IASL_DIR="acpica-unix2-${IASL_VERSION}"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +010090PYTHON_DIR="Python-${PYTHON_VERSION}"
91EXPAT_DIR="expat-${EXPAT_VERSION}"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070092# CLANG toolchain directories
93LLVM_DIR="llvm-${CLANG_VERSION}.src"
94CFE_DIR="cfe-${CLANG_VERSION}.src"
95CRT_DIR="compiler-rt-${CLANG_VERSION}.src"
96CTE_DIR="clang-tools-extra-${CLANG_VERSION}.src"
Stefan Reinauera851bbb2016-01-29 18:03:26 -080097MAKE_DIR="make-${MAKE_VERSION}"
Stefan Reinauer3b593492017-06-19 17:28:54 -070098CMAKE_DIR="cmake-${CMAKE_VERSION}"
Martin Rothd70f5fa2019-05-26 17:24:19 -060099NASM_DIR="nasm-${NASM_VERSION}"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000100
Patrick Georgi6321f522015-02-27 23:36:26 +0100101unset MAKELEVEL MAKEFLAGS
102
Patrick Georgi1861ff72011-10-31 12:15:55 +0100103red='\033[0;31m'
104RED='\033[1;31m'
105green='\033[0;32m'
106GREEN='\033[1;32m'
107blue='\033[0;34m'
Patrick Georgi1861ff72011-10-31 12:15:55 +0100108CYAN='\033[1;36m'
109NC='\033[0m' # No Color
Stefan Reinauer074d9132009-09-26 16:43:17 +0000110
Martin Roth987d42d2018-07-22 11:45:29 -0600111UNAME=$(if uname | grep -iq cygwin; then echo Cygwin; else uname; fi)
Martin Roth88463822016-01-07 11:03:36 -0700112HALT_FOR_TOOLS=0
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700113
Nico Huber08bb8372017-06-24 19:50:35 +0200114hostcc()
115{
116 # $1 "host" or "target"
Martin Roth987d42d2018-07-22 11:45:29 -0600117 if [ "$BOOTSTRAP" = 1 ] && [ "$1" = target ]; then
118 echo "$DESTDIR$TARGETDIR/bin/gcc"
Nico Huber08bb8372017-06-24 19:50:35 +0200119 else
Martin Roth987d42d2018-07-22 11:45:29 -0600120 echo "$CC"
Nico Huber08bb8372017-06-24 19:50:35 +0200121 fi
122}
123
124hostcxx()
125{
126 # $1 "host" or "target"
Martin Roth987d42d2018-07-22 11:45:29 -0600127 if [ "$BOOTSTRAP" = 1 ] && [ "$1" = target ]; then
128 echo "$DESTDIR$TARGETDIR/bin/g++"
Nico Huber08bb8372017-06-24 19:50:35 +0200129 else
Martin Roth987d42d2018-07-22 11:45:29 -0600130 echo "$CXX"
Nico Huber08bb8372017-06-24 19:50:35 +0200131 fi
132}
133
Patrick Georgi198d23c2012-12-08 08:02:44 +0100134normalize_dirs()
135{
Martin Roth987d42d2018-07-22 11:45:29 -0600136 mkdir -p "$DESTDIR$TARGETDIR/lib"
137 test -d "$DESTDIR$TARGETDIR/lib32" && mv "$DESTDIR$TARGETDIR"/lib32/* "$DESTDIR$TARGETDIR/lib"
138 test -d "$DESTDIR$TARGETDIR/lib64" && mv "$DESTDIR$TARGETDIR"/lib64/* "$DESTDIR$TARGETDIR/lib"
139 rmdir -p "$DESTDIR$TARGETDIR/lib32" "$DESTDIR$TARGETDIR/lib64"
Patrick Georgi198d23c2012-12-08 08:02:44 +0100140
Martin Roth987d42d2018-07-22 11:45:29 -0600141 perl -pi -e "s,/lib32,/lib," "$DESTDIR$TARGETDIR"/lib/*.la
142 perl -pi -e "s,/lib64,/lib," "$DESTDIR$TARGETDIR"/lib/*.la
Patrick Georgi198d23c2012-12-08 08:02:44 +0100143}
144
Nico Hubercd87e1b2017-06-24 20:35:59 +0200145countdown()
146{
147 tout=${1:-10}
148
Martin Roth987d42d2018-07-22 11:45:29 -0600149 printf "\nPress Ctrl-C to abort, Enter to continue... %2ds" "$tout"
150 while [ "$tout" -gt 0 ]; do
Nico Hubercd87e1b2017-06-24 20:35:59 +0200151 sleep 1
152 tout=$((tout - 1))
153 printf "\b\b\b%2ds" $tout
154 done
155 printf "\n"
156}
157
158timeout()
159{
160 tout=${1:-10}
161
162 # Ignore SIGUSR1, should interrupt `read` though.
163 trap false USR1
164 # Clean up in case the user aborts.
165 trap 'kill $counter > /dev/null 2>&1' EXIT
166
Martin Roth987d42d2018-07-22 11:45:29 -0600167 (countdown "$tout"; kill -USR1 $$)&
Nico Hubercd87e1b2017-06-24 20:35:59 +0200168 counter=$!
169
170 # Some shells with sh compatibility mode (e.g. zsh, mksh) only
171 # let us interrupt `read` if a non-standard -t parameter is given.
Martin Roth987d42d2018-07-22 11:45:29 -0600172 # shellcheck disable=SC2034,SC2039,SC2162
Nico Hubercd87e1b2017-06-24 20:35:59 +0200173 if echo | read -t 1 foo 2>/dev/null; then
174 read -t $((tout + 1)) foo
175 else
176 read foo
177 fi
178
179 kill $counter > /dev/null 2>&1
180 trap - USR1 EXIT
181}
182
Stefan Reinauer93a96302015-06-15 12:36:53 -0700183please_install()
184{
Martin Roth88463822016-01-07 11:03:36 -0700185 HALT_FOR_TOOLS=1
Martin Roth987d42d2018-07-22 11:45:29 -0600186 # shellcheck disable=SC1091
Patrick Georgi5da95dc2015-07-17 23:33:05 +0200187 test -r /etc/os-release && . /etc/os-release
Alex Thiessen72d10892018-01-12 01:45:53 +0000188 # vanilla debian doesn't define `ID_LIKE`, just `ID`
189 if [ -z "${ID_LIKE}" ] && [ -n "${ID}" ]; then
190 ID_LIKE=${ID}
191 fi
Stefan Reinauer93a96302015-06-15 12:36:53 -0700192 case "$ID_LIKE" in
193 debian) solution="sudo apt-get install $1" ;;
194 suse) solution="sudo zypper install $1" ;;
195 *) solution="using your OS packaging system" ;;
196 esac
197
Patrick Georgidf1ff232017-01-07 09:28:43 +0100198 printf "${RED}ERROR:${red} Missing tool: Please install '$1'. (eg $solution)${NC}\n" >&2
Martin Rothe3963172016-01-06 13:54:32 -0700199 if [ -n "$2" ]; then
Patrick Georgidf1ff232017-01-07 09:28:43 +0100200 printf "${RED}ERROR:${red} or install '$2'.${NC}\n" >&2
Martin Rothe3963172016-01-06 13:54:32 -0700201 fi
Stefan Reinauer93a96302015-06-15 12:36:53 -0700202}
203
Patrick Georgib7062882015-02-24 10:52:14 +0100204searchtool()
Stefan Reinauer074d9132009-09-26 16:43:17 +0000205{
206 # $1 short name
Patrick Georgib7062882015-02-24 10:52:14 +0100207 # $2 search string
208 # $3 soft fail if set
Martin Rothe3963172016-01-06 13:54:32 -0700209 # $4 alternative package to install on failure
Patrick Georgib7062882015-02-24 10:52:14 +0100210 # result: file name of that tool on stdout
211 # or no output if nothing suitable was found
212 search=GNU
213 if [ -n "$2" ]; then
214 search="$2"
215 fi
Stefan Reinauer074d9132009-09-26 16:43:17 +0000216 for i in "$1" "g$1" "gnu$1"; do
Martin Roth987d42d2018-07-22 11:45:29 -0600217 if [ -x "$(command -v "$i" 2>/dev/null)" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700218 if [ "$(cat /dev/null | $i --version 2>&1 | grep -c "$search")" \
219 -gt 0 ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600220 echo "$i"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000221 return
222 fi
223 fi
224 done
Andrew Wub67e9a12014-04-28 18:13:44 +0800225 # A workaround for OSX 10.9 and some BSDs, whose nongnu
226 # patch and tar also work.
Martin Roth987d42d2018-07-22 11:45:29 -0600227 if [ "$UNAME" = "Darwin" ] || [ "$UNAME" = "FreeBSD" ] || [ "$UNAME" = "NetBSD" ] || [ "$UNAME" = "OpenBSD" ]; then
228 if [ "$1" = "patch" ] || [ "$1" = "tar" ]; then
229 if [ -x "$(command -v "$1" 2>/dev/null)" ]; then
230 echo "$1"
Zheng Bao36156ff2012-09-28 16:18:58 +0800231 return
232 fi
233 fi
234 fi
Martin Roth987d42d2018-07-22 11:45:29 -0600235 if echo "$1" | grep -q "sum" ; then
236 algor=$(echo "$1" | sed -e 's,sum,,')
237 if [ -x "$(command -v "$1" 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400238 #xxxsum [file]
Martin Roth987d42d2018-07-22 11:45:29 -0600239 echo "$1"
zbao5cf758d2015-09-01 22:28:57 -0400240 return
Martin Roth987d42d2018-07-22 11:45:29 -0600241 elif [ -x "$(command -v "$algor" 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400242 #xxx [file]
Martin Roth987d42d2018-07-22 11:45:29 -0600243 echo "$algor"
zbao5cf758d2015-09-01 22:28:57 -0400244 return
Martin Rothd55f5eb2017-06-15 11:37:26 -0600245 elif [ -x "$(command -v openssl 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400246 #openssl xxx [file]
Martin Roth987d42d2018-07-22 11:45:29 -0600247 echo openssl "$algor"
zbao5cf758d2015-09-01 22:28:57 -0400248 return
Martin Rothd55f5eb2017-06-15 11:37:26 -0600249 elif [ -x "$(command -v cksum 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400250 #cksum -a xxx [file]
251 #cksum has special options in NetBSD. Actually, NetBSD will use the second case above.
Martin Roth987d42d2018-07-22 11:45:29 -0600252 echo "buildgcc" | cksum -a "$algor" > /dev/null 2>/dev/null && \
253 echo cksum -a "$algor"
zbao5cf758d2015-09-01 22:28:57 -0400254 return
zbao939dc842015-04-30 04:44:07 +0800255 fi
256 fi
Martin Rothe3963172016-01-06 13:54:32 -0700257
Martin Roth987d42d2018-07-22 11:45:29 -0600258 [ -z "$3" ] && please_install "$1" "$4"
Patrick Georgib7062882015-02-24 10:52:14 +0100259 false
Stefan Reinauer074d9132009-09-26 16:43:17 +0000260}
261
Martin Roth591790f2016-03-20 20:39:04 -0600262# Run a compile check of the specified library option to see if it's installed
263check_for_library() {
Martin Roth987d42d2018-07-22 11:45:29 -0600264 LIBRARY_FLAGS="$1"
265 LIBRARY_PACKAGES="$2"
266 LIBTEST_FILE=.libtest
Martin Roth591790f2016-03-20 20:39:04 -0600267
268 echo "int main(int argc, char **argv) { (void) argc; (void) argv; return 0; }" > "${LIBTEST_FILE}.c"
269
Martin Roth987d42d2018-07-22 11:45:29 -0600270 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200271 "$CC" $CFLAGS $LIBRARY_FLAGS "${LIBTEST_FILE}.c" -o "${LIBTEST_FILE}" >/dev/null 2>&1 || \
Martin Roth591790f2016-03-20 20:39:04 -0600272 please_install "$LIBRARY_PACKAGES"
273 rm -rf "${LIBTEST_FILE}.c" "${LIBTEST_FILE}"
274}
275
Nico Huberaee7f462016-09-20 12:55:19 +0200276buildcc_major() {
277 echo "${GCC_VERSION}" | cut -d. -f1
278}
279
Nico Huber26267a72016-12-04 02:45:58 +0100280buildcc_minor() {
281 echo "${GCC_VERSION}" | cut -d. -f2
282}
283
Nico Huberaee7f462016-09-20 12:55:19 +0200284buildcc_version() {
285 echo "${GCC_VERSION}" | cut -d. -f1-2
286}
287
288hostcc_major() {
289 (echo __GNUC__ | ${CC} -E - 2>/dev/null || echo 0) | tail -1
290}
291
292hostcc_minor() {
293 (echo __GNUC_MINOR__ | ${CC} -E - 2>/dev/null || echo 0) | tail -1
294}
295
296hostcc_version() {
297 printf "%d.%d" "$(hostcc_major)" "$(hostcc_minor)"
298}
299
Nico Huber75797162016-09-20 13:01:57 +0200300hostcc_has_gnat1() {
301 [ -x "$(${CC} -print-prog-name=gnat1)" ]
302}
303
Nico Hubercd87e1b2017-06-24 20:35:59 +0200304have_gnat() {
305 hostcc_has_gnat1 && \
306 searchtool gnatbind "Free Software Foundation" nofail > /dev/null
307}
308
Nico Huber75797162016-09-20 13:01:57 +0200309ada_requested() {
310 echo "${LANGUAGES}" | grep -q '\<ada\>'
311}
312
Stefan Reinauer88935482015-06-05 09:51:10 -0700313download() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700314 package=$1
Martin Roth987d42d2018-07-22 11:45:29 -0600315 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700316 archive="$(eval echo \$$package"_ARCHIVE")"
Stefan Reinauer699ac272015-06-05 12:43:28 -0700317
Martin Roth987d42d2018-07-22 11:45:29 -0600318 FILE=$(basename "$archive")
Stefan Reinauer88935482015-06-05 09:51:10 -0700319 printf " * $FILE "
320
Martin Roth987d42d2018-07-22 11:45:29 -0600321 if test -f "tarballs/$FILE"; then
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200322 printf "(cached)... "
zbaod22b9ea2015-08-30 23:35:16 -0400323 else
Paul Menzelb2ba3952015-06-12 11:58:14 +0200324 printf "(downloading from $archive)"
Martin Roth987d42d2018-07-22 11:45:29 -0600325 rm -f "tarballs/$FILE"
326 cd tarballs || exit 1
327 download_showing_percentage "$archive"
Stefan Reinauer88935482015-06-05 09:51:10 -0700328 cd ..
zbaod22b9ea2015-08-30 23:35:16 -0400329 fi
330
Martin Roth987d42d2018-07-22 11:45:29 -0600331 if [ ! -f "tarballs/$FILE" ]; then
Patrick Georgidf1ff232017-01-07 09:28:43 +0100332 printf "${RED}Failed to download $FILE.${NC}\n"
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700333 exit 1
334 fi
Stefan Reinauer88935482015-06-05 09:51:10 -0700335}
336
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200337# Compute the hash of the package given in $1, and print it raw (just the
338# hexadecimal hash).
339compute_hash() {
340 package=$1
Martin Roth987d42d2018-07-22 11:45:29 -0600341 # shellcheck disable=SC2086
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200342 archive="$(eval echo \$$package"_ARCHIVE")"
343 file="$(basename "$archive")"
344
345 if test -z "$CHECKSUM"; then
346 echo "${RED}\$CHECKSUM program missing. This is bad.${NC}" 1>&2
347 exit 1
348 fi
349
350 $CHECKSUM "tarballs/$file" 2>/dev/null | sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@'
351}
352
353error_hash_missing() {
354 package="$1"
Martin Roth987d42d2018-07-22 11:45:29 -0600355 # shellcheck disable=SC2086
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200356 archive="$(eval echo \$$package"_ARCHIVE")"
357 file="$(basename "$archive")"
358
359 fullhashfile="util/crossgcc/sum/$file.cksum"
360 printf "${RED}hash file missing:${NC}\n\n" 1>&2
361 printf "Please verify util/crossgcc/tarball/$file carefully\n" 1>&2
362 printf "(using PGP if possible), and then rename\n" 1>&2
363 printf " ${CYAN}${fullhashfile}.calc${NC}\n" 1>&2
364 printf " to ${CYAN}${fullhashfile}${NC}\n\n" 1>&2
365
366 exit 1
367}
368
369# Read the known hash file of the package given in $1, and print it raw.
370get_known_hash() {
371 package=$1
Martin Roth987d42d2018-07-22 11:45:29 -0600372 # shellcheck disable=SC2086
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200373 archive="$(eval echo \$$package"_ARCHIVE")"
374 file="$(basename "$archive")"
375 hashfile="sum/$file.cksum"
376
377 if [ ! -f "$hashfile" ]; then
378 calc_hash="$(compute_hash "$package")" || exit 1
379 echo "$calc_hash tarballs/$file" > "${hashfile}.calc"
380
381 error_hash_missing "$package"
382 exit 1
383 fi
384
Martin Roth987d42d2018-07-22 11:45:29 -0600385 sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@' < "$hashfile"
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200386}
387
388error_hash_mismatch() {
389 package=$1
390 known_hash="$2"
391 computed_hash="$3"
Martin Roth987d42d2018-07-22 11:45:29 -0600392 # shellcheck disable=SC2086
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200393 archive="$(eval echo \$$package"_ARCHIVE")"
394 file="$(basename "$archive")"
395
396 printf "${RED}hash mismatch:${NC}\n\n"
397 printf " expected (known) hash: $known_hash\n"
398 printf "calculated hash of downloaded file: $computed_hash\n\n"
399
400 printf "If you think this is due to a network error, please delete\n"
401 printf " ${CYAN}util/crossgcc/tarballs/$file${NC}\n"
402 printf "and try again. If the problem persists, it may be due to an\n"
403 printf "administration error on the file server, or you might be\n"
404 printf "subject to a Man-in-the-Middle attack\n\n"
405
406 exit 1
407}
408
409# verify_hash - Check that the hash of the file given in $1 matches the known
410# hash; Bail out on mismatch or missing hash file.
411verify_hash() {
412 package=$1
Martin Roth987d42d2018-07-22 11:45:29 -0600413 # shellcheck disable=SC2086
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200414 archive="$(eval echo \$$package"_ARCHIVE")"
415
416 known_hash="$(get_known_hash "$package")" || exit "$?"
417 computed_hash="$(compute_hash "$package")" || exit "$?"
418
419 if [ "$known_hash" != "$computed_hash" ]; then
420 error_hash_mismatch "$package" "$known_hash" "$computed_hash"
421 exit 1
422 fi
423
Jonathan Neuschäfercb76069e2018-10-01 09:45:49 +0200424 printf "${GREEN}hash verified (${known_hash})${NC}\n"
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200425}
426
Stefan Reinauer88935482015-06-05 09:51:10 -0700427unpack_and_patch() {
Martin Roth987d42d2018-07-22 11:45:29 -0600428 package="$1"
429 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700430 archive="$(eval echo \$$package"_ARCHIVE")"
Martin Roth987d42d2018-07-22 11:45:29 -0600431 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700432 dir="$(eval echo \$$package"_DIR")"
Martin Roth987d42d2018-07-22 11:45:29 -0600433 test -d "${dir}" && test -f "${dir}/.unpack_success" || (
434 printf " * $(basename "$archive")\n"
Stefan Reinauer88935482015-06-05 09:51:10 -0700435 FLAGS=zxf
Martin Roth987d42d2018-07-22 11:45:29 -0600436 suffix=$(echo "$archive" | sed 's,.*\.,,')
Martin Roth95f7b222016-03-20 12:38:48 -0600437 if [ "$suffix" = "gz" ] && [ -n "$PIGZ" ]; then FLAGS="-I pigz -xf"
438 elif [ "$suffix" = "gz" ]; then FLAGS=zxf
439 elif [ "$suffix" = "bz2" ] && [ -n "$LBZIP2" ]; then FLAGS="-I lbzip2 -xf"
440 elif [ "$suffix" = "bz2" ]; then FLAGS=jxf
441 elif [ "$suffix" = "xz" ]; then FLAGS="--xz -xf"
442 elif [ "$suffix" = "lzma" ]; then FLAGS="--lzma -xf"
443 fi
Martin Roth987d42d2018-07-22 11:45:29 -0600444 # shellcheck disable=SC2086
445 $TAR $FLAGS "tarballs/$(basename "$archive")"
Stefan Reinauer88935482015-06-05 09:51:10 -0700446 for patch in patches/${dir}_*.patch; do
Martin Roth987d42d2018-07-22 11:45:29 -0600447 test -r "$patch" || continue
448 printf " o $(basename "$patch")\n"
449 (cd "${dir}" || exit 1; $PATCH -s -N -p1 <"../${patch}") || {
Stefan Reinauer88935482015-06-05 09:51:10 -0700450 printf "\n${RED}Failed $patch.${NC}\n"
Stefan Reinauer916b8452015-06-09 11:54:49 -0700451 exit 1
452 }
Stefan Reinauer88935482015-06-05 09:51:10 -0700453 done
Martin Roth987d42d2018-07-22 11:45:29 -0600454 touch "${dir}/.unpack_success"
Stefan Reinauer88935482015-06-05 09:51:10 -0700455 )
456}
457
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700458fn_exists()
459{
Martin Roth987d42d2018-07-22 11:45:29 -0600460 # shellcheck disable=SC2039
461 type "$1" >/dev/null 2>&1
Patrick Georgi74c06422015-03-25 18:40:13 +0100462}
463
Patrick Georgif2c15f52015-06-11 21:07:31 +0200464is_package_enabled()
465{
466 echo "$PACKAGES" |grep -q "\<$1\>"
467}
468
Martin Roth444ece22016-04-01 18:46:29 -0600469package_uses_targetarch()
470{
Stefan Reinauera344b682016-04-21 19:29:51 -0700471 if [ "$1" = "GCC" ] || [ "$1" = "GDB" ] || [ "$1" = "BINUTILS" ]; then
Martin Roth444ece22016-04-01 18:46:29 -0600472 true
473 else
474 false
475 fi
476}
477
Nico Huber11ea2b32016-01-26 16:09:31 +0100478generic_build()
479{
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700480 package=$1
Nico Huber11ea2b32016-01-26 16:09:31 +0100481 host_target=$2
482 builddir=$3
483 success=$4
Martin Rothc295d5e2016-11-14 11:56:11 -0700484 version=$5
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700485
Martin Roth987d42d2018-07-22 11:45:29 -0600486 fn_exists "build_$package" || return
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700487
Nico Huber11ea2b32016-01-26 16:09:31 +0100488 mkdir -p "$builddir"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700489
Nico Huber11ea2b32016-01-26 16:09:31 +0100490 if [ -f "$success" ]; then
Martin Roth2c6a8062016-11-14 11:58:39 -0700491 printf "Skipping $package v$version for $host_target as it is already built\n"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700492 else
Martin Roth2c6a8062016-11-14 11:58:39 -0700493 printf "Building $package v$version for $host_target ... "
Nico Huber11ea2b32016-01-26 16:09:31 +0100494 DIR="$PWD"
Martin Roth987d42d2018-07-22 11:45:29 -0600495 cd "$builddir" || exit 1
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700496 rm -f .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600497 "build_${package}" "$host_target" > build.log 2>&1
498 cd "$DIR" || exit 1
Nico Huber11ea2b32016-01-26 16:09:31 +0100499 if [ ! -f "$builddir/.failed" ]; then
500 touch "$success";
501 else
Idwer Volleringe2ef3cf2017-08-15 00:53:13 +0200502 printf "${RED}failed${NC}. Check '$builddir/build.log'.\n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700503 exit 1
504 fi
505 printf "${green}ok${NC}\n"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700506 fi
507}
508
Nico Huber11ea2b32016-01-26 16:09:31 +0100509build_for_host()
510{
Martin Rothc295d5e2016-11-14 11:56:11 -0700511 package="$1"
Martin Roth987d42d2018-07-22 11:45:29 -0600512 # shellcheck disable=SC2086
Martin Rothc295d5e2016-11-14 11:56:11 -0700513 version="$(eval echo \$$package"_VERSION")"
Patrick Georgi1b593e52016-11-25 15:27:41 +0100514 generic_build "$package" host "build-$package" "${DESTDIR}${TARGETDIR}/.${package}.${version}.success" "$version"
Nico Huber11ea2b32016-01-26 16:09:31 +0100515}
516
517build_for_target()
518{
Martin Rothc295d5e2016-11-14 11:56:11 -0700519 package="$1"
Martin Roth987d42d2018-07-22 11:45:29 -0600520 # shellcheck disable=SC2086
Martin Rothc295d5e2016-11-14 11:56:11 -0700521 version="$(eval echo \$$package"_VERSION")"
Patrick Georgi1b593e52016-11-25 15:27:41 +0100522 generic_build "$package" target "build-${TARGETARCH}-$package" "${DESTDIR}${TARGETDIR}/.${TARGETARCH}-${package}.${version}.success" "$version"
Nico Huber11ea2b32016-01-26 16:09:31 +0100523}
524
525build()
526{
Martin Roth987d42d2018-07-22 11:45:29 -0600527 if package_uses_targetarch "$1"; then
528 if [ $BOOTSTRAP -eq 1 ] && [ ! -f "${DESTDIR}${TARGETDIR}/.GCC.${GCC_VERSION}.success" ]; then
Nico Huber234d2462016-01-26 16:10:17 +0100529 build_for_host GCC
530 fi
Martin Roth987d42d2018-07-22 11:45:29 -0600531 build_for_target "$1"
Nico Huber11ea2b32016-01-26 16:09:31 +0100532 else
Martin Roth987d42d2018-07-22 11:45:29 -0600533 build_for_host "$1"
Nico Huber11ea2b32016-01-26 16:09:31 +0100534 fi
535}
536
Nico Huber18c74d62017-06-24 20:35:59 +0200537exit_handler()
Zheng Baobb003c82016-08-05 13:41:51 +0800538{
539 printf "${NC}Stop\n"
540 exit 1
541}
542
Stefan Reinauer074d9132009-09-26 16:43:17 +0000543cleanup()
544{
Stefan Reinauer88e83e52016-04-06 15:39:48 -0700545 if [ $SAVETEMPS -ne 0 ]; then
546 printf "Leaving temporary files around... ${green}ok${NC}\n"
547 return
548 fi
549
Stefan Reinauer074d9132009-09-26 16:43:17 +0000550 printf "Cleaning up temporary files... "
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700551 for package in $PACKAGES; do
Martin Roth987d42d2018-07-22 11:45:29 -0600552 # shellcheck disable=SC2086
553 rm -rf "build-${TARGETARCH}-$package" "build-$package" "$(eval echo \$$package"_DIR")"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700554 done
zbaoff3f15c2015-08-26 22:00:31 -0400555 rm -f getopt
Stefan Reinauer074d9132009-09-26 16:43:17 +0000556 printf "${green}ok${NC}\n"
557}
558
559myhelp()
560{
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700561 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>] [-C] [-G] [-S]\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000562 printf " $0 [-V|--version]\n"
563 printf " $0 [-h|--help]\n\n"
564
565 printf "Options:\n"
566 printf " [-V|--version] print version number and exit\n"
567 printf " [-h|--help] print this help and exit\n"
568 printf " [-c|--clean] remove temporary files before build\n"
569 printf " [-t|--savetemps] don't remove temporary files after build\n"
Patrick Georgic1a75b12011-11-04 21:37:14 +0100570 printf " [-y|--ccache] Use ccache when building cross compiler\n"
Nico Huber152e78e2016-09-20 15:38:40 +0200571 printf " [-n|--nocolor] don't print color codes in output\n"
572 printf " [-u|--urls] print the urls for all packages\n"
Patrick Georgi73166c72010-04-14 20:42:42 +0000573 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
Martin Roth588c79d2016-03-20 12:03:20 -0600574 printf " [-s]--supported <tool> print supported version of a tool\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000575 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
Martin Roth588c79d2016-03-20 12:03:20 -0600576 printf " (defaults to $TARGETDIR)\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000577 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100578 printf " (for RPM builds, default unset)\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700579 printf " [-P|--package <package>] Build a specific package: GCC, CLANG, IASL, GDB\n"
580 printf " (defaults to $PACKAGE)\n"
581 printf "GCC specific options:\n"
Nico Huber234d2462016-01-26 16:10:17 +0100582 printf " [-b|--bootstrap] bootstrap the host compiler before building\n"
583 printf " the cross compiler\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700584 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
585 printf " (defaults to $TARGETARCH)\n"
Nico Huberbb313bf2015-09-08 12:30:27 +0200586 printf " [-l|--languages <languages>] comma separated list of target languages\n"
Nico Huber66c2c1a2016-09-20 13:09:29 +0200587 printf " (defaults to $DEFAULT_LANGUAGES)\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700588 printf "GDB specific options:\n"
589 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
590 printf " (defaults to $TARGETARCH)\n"
591 printf " [-S|--scripting] build scripting support for GDB\n\n"
Martin Rothd1da74f2015-06-21 12:06:28 -0600592 printf "Platforms for GCC & GDB:\n"
Julius Wernerf96d9052019-08-16 15:35:39 -0700593 printf " x86_64 i386-elf i386-mingw32 riscv-elf arm aarch64\n"
Jonathan Neuschäferf14f6402016-03-11 20:22:23 +0100594 printf " powerpc64le-linux-gnu nds32le-elf\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000595}
596
Martin Rothedf965a2015-11-25 16:58:33 -0700597printversion() {
598 printf "${blue}Welcome to the ${red}coreboot${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
599}
600
Stefan Reinauer074d9132009-09-26 16:43:17 +0000601myversion()
602{
Martin Rothedf965a2015-11-25 16:58:33 -0700603 printversion
Stefan Reinauer074d9132009-09-26 16:43:17 +0000604
605 cat << EOF
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000606Copyright (C) 2008-2010 by coresystems GmbH
Marc Jones2aac3f62011-08-08 16:07:50 -0600607Copyright (C) 2011 by Sage Electronic Engineering
Stefan Reinauer074d9132009-09-26 16:43:17 +0000608
609This program is free software; you can redistribute it and/or modify
610it under the terms of the GNU General Public License as published by
611the Free Software Foundation; version 2 of the License.
612
613This program is distributed in the hope that it will be useful,
614but WITHOUT ANY WARRANTY; without even the implied warranty of
615MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
616GNU General Public License for more details.
617
618EOF
619}
620
Nico Huber5f72f962016-01-31 23:05:19 +0100621have_hostcflags_from_gmp() {
Martin Roth987d42d2018-07-22 11:45:29 -0600622 grep -q __GMP_CFLAGS "$DESTDIR$TARGETDIR/include/gmp.h" >/dev/null 2>&1
Nico Huber5f72f962016-01-31 23:05:19 +0100623}
624
625set_hostcflags_from_gmp() {
626 # Now set CFLAGS to match GMP CFLAGS but strip out -pedantic
627 # as GCC 4.6.x fails if it's there.
Martin Roth987d42d2018-07-22 11:45:29 -0600628 HOSTCFLAGS="$(grep __GMP_CFLAGS "$DESTDIR$TARGETDIR/include/gmp.h" |cut -d\" -f2 |\
Idwer Volleringba349ab2016-07-24 02:10:19 +0200629 sed s,-pedantic,,)"
Martin Roth987d42d2018-07-22 11:45:29 -0600630 export HOSTCFLAGS
Nico Huber5f72f962016-01-31 23:05:19 +0100631}
632
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700633build_GMP() {
Nico Huber8bbd5962016-12-22 16:05:54 +0100634 # Check if GCC enables `-pie` by default (possible since GCC 6).
635 # We need PIC in all static libraries then.
Nico Huber2c1c02e2017-01-02 20:51:28 +0100636 if $CC -dumpspecs 2>/dev/null | grep -q '[{;][[:space:]]*:-pie\>'
Nico Huber8bbd5962016-12-22 16:05:54 +0100637 then
638 OPTIONS="$OPTIONS --with-pic"
639 fi
640
Martin Roth987d42d2018-07-22 11:45:29 -0600641 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200642 CC="$(hostcc host)" CXX="$(hostcxx host)" \
643 ../${GMP_DIR}/configure --disable-shared --enable-fat \
Martin Roth987d42d2018-07-22 11:45:29 -0600644 --prefix="$TARGETDIR" $OPTIONS \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700645 || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600646 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700647 $MAKE $JOBS || touch .failed
648 $MAKE install DESTDIR=$DESTDIR || touch .failed
649
650 normalize_dirs
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700651
Nico Huber5f72f962016-01-31 23:05:19 +0100652 set_hostcflags_from_gmp
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700653}
654
655build_MPFR() {
Martin Roth987d42d2018-07-22 11:45:29 -0600656 test "$UNAME" = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
Nico Huber08bb8372017-06-24 19:50:35 +0200657 CC="$(hostcc host)" CXX="$(hostcxx host)" \
Martin Roth987d42d2018-07-22 11:45:29 -0600658 ../${MPFR_DIR}/configure --disable-shared --prefix="$TARGETDIR" \
659 --infodir="$TARGETDIR/info" \
660 --with-gmp="$DESTDIR$TARGETDIR" CFLAGS="$HOSTCFLAGS" || \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700661 touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600662 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700663 $MAKE $JOBS || touch .failed
664 $MAKE install DESTDIR=$DESTDIR || touch .failed
665
666 normalize_dirs
667
668 # work around build problem of libgmp.la
669 if [ "$DESTDIR" != "" ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600670 perl -pi -e "s,$DESTDIR,," "$DESTDIR$TARGETDIR/lib/libgmp.la"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700671 fi
672}
673
674build_MPC() {
Nico Huber08bb8372017-06-24 19:50:35 +0200675 CC="$(hostcc host)" CXX="$(hostcxx host)" \
Martin Roth987d42d2018-07-22 11:45:29 -0600676 ../${MPC_DIR}/configure --disable-shared --prefix="$TARGETDIR" \
677 --infodir="$TARGETDIR/info" --with-mpfr="$DESTDIR$TARGETDIR" \
678 --with-gmp="$DESTDIR$TARGETDIR" CFLAGS="$HOSTCFLAGS" || \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700679 touch .failed
Patrick Georgi790aab62017-04-26 17:35:35 +0200680
681 # work around build problem of libmpfr.la
682 if [ "$DESTDIR" != "" ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600683 perl -pi -e "s,$TARGETDIR/lib/libgmp.la,$DESTDIR\$&," "$DESTDIR$TARGETDIR/lib/libmpfr.la"
Patrick Georgi790aab62017-04-26 17:35:35 +0200684 fi
685
Martin Roth987d42d2018-07-22 11:45:29 -0600686 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700687 $MAKE $JOBS || touch .failed
688 $MAKE install DESTDIR=$DESTDIR || touch .failed
689
Patrick Georgi790aab62017-04-26 17:35:35 +0200690 # work around build problem of libmpfr.la
691 if [ "$DESTDIR" != "" ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600692 perl -pi -e "s,$DESTDIR,," "$DESTDIR$TARGETDIR/lib/libmpfr.la"
Patrick Georgi790aab62017-04-26 17:35:35 +0200693 fi
694
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700695 normalize_dirs
696}
697
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700698build_BINUTILS() {
Nico Huberae6187f2016-12-22 22:16:39 +0100699 if [ $TARGETARCH = "x86_64-elf" ]; then
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700700 ADDITIONALTARGET=",i386-elf"
701 fi
Nico Huber08bb8372017-06-24 19:50:35 +0200702 CC="$(hostcc target)" CXX="$(hostcxx target)" \
Martin Roth987d42d2018-07-22 11:45:29 -0600703 ../binutils-${BINUTILS_VERSION}/configure --prefix="$TARGETDIR" \
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700704 --target=${TARGETARCH} --enable-targets=${TARGETARCH}${ADDITIONALTARGET} \
705 --disable-werror --disable-nls --enable-lto --enable-gold \
Stefan Reinauer2fe2d3d2016-04-05 02:14:48 -0700706 --enable-interwork --enable-multilib \
Vivia Nikolaidou81084362018-04-28 01:21:44 +0300707 --enable-plugins --enable-multilibs \
Nico Huber010ecf802016-12-22 16:19:37 +0100708 CFLAGS="$HOSTCFLAGS" \
709 CXXFLAGS="$HOSTCFLAGS" \
710 || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600711 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700712 $MAKE $JOBS || touch .failed
713 $MAKE install DESTDIR=$DESTDIR || touch .failed
714}
715
Nico Huber234d2462016-01-26 16:10:17 +0100716bootstrap_GCC() {
Martin Roth987d42d2018-07-22 11:45:29 -0600717 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200718 CC="$(hostcc host)" CXX="$(hostcxx host)" \
Nico Huber234d2462016-01-26 16:10:17 +0100719 CFLAGS="$HOSTCFLAGS" \
720 CFLAGS_FOR_BUILD="$HOSTCFLAGS" \
721 CFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
722 CXXFLAGS="$HOSTCFLAGS" \
723 CXXFLAGS_FOR_BUILD="$HOSTCFLAGS" \
724 CXXFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
725 ../gcc-${GCC_VERSION}/configure \
Martin Roth987d42d2018-07-22 11:45:29 -0600726 --prefix="$TARGETDIR" --libexecdir="$TARGETDIR/lib" \
Nico Huber234d2462016-01-26 16:10:17 +0100727 --enable-bootstrap \
728 --disable-werror --disable-nls \
729 --disable-shared --disable-multilib \
730 --disable-libssp --disable-libquadmath --disable-libcc1 \
Patrick Georgi97a9df42017-04-26 22:18:18 +0200731 --disable-libsanitizer \
Nico Huber234d2462016-01-26 16:10:17 +0100732 ${GCC_OPTIONS} --enable-languages="${LANGUAGES}" \
Martin Roth987d42d2018-07-22 11:45:29 -0600733 --with-gmp="$DESTDIR$TARGETDIR" --with-mpfr="$DESTDIR$TARGETDIR" \
734 --with-mpc="$DESTDIR$TARGETDIR" \
Nico Huber234d2462016-01-26 16:10:17 +0100735 --with-pkgversion="coreboot bootstrap v$CROSSGCC_VERSION $CROSSGCC_DATE" \
736 && \
Martin Roth987d42d2018-07-22 11:45:29 -0600737 # shellcheck disable=SC2086
Nico Huber234d2462016-01-26 16:10:17 +0100738 $MAKE $JOBS BOOT_CFLAGS="$HOSTCFLAGS" BUILD_CONFIG="" bootstrap && \
739 $MAKE install-gcc \
740 install-target-libgcc \
741 maybe-install-target-libada \
742 maybe-install-target-libstdc++-v3 \
Nico Huber08bb8372017-06-24 19:50:35 +0200743 DESTDIR=$DESTDIR || touch .failed
Nico Huber234d2462016-01-26 16:10:17 +0100744}
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700745
Nico Huber234d2462016-01-26 16:10:17 +0100746build_cross_GCC() {
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700747 # Work around crazy code generator in GCC that confuses CLANG.
Aaron Durbinb2229dc2016-04-07 16:58:10 -0500748 $CC --version | grep clang >/dev/null 2>&1 && \
Patrick Georgia7a5a562017-06-26 07:21:46 +0200749 CLANGFLAGS="-fbracket-depth=1024"
Nico Huber3f12d932017-08-31 23:11:18 +0200750 [ -n "$CXX" ] && $CXX --version | grep clang >/dev/null 2>&1 && \
Patrick Georgia7a5a562017-06-26 07:21:46 +0200751 CLANGCXXFLAGS="-fbracket-depth=1024"
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700752
Stefan Reinauerd0f83722015-06-09 11:52:28 -0700753 # GCC does not honor HOSTCFLAGS at all. CFLAGS are used for
754 # both target and host object files.
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700755 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
756 # but it does not seem to work properly. At least the host library
757 # libiberty is not compiled with CFLAGS_FOR_BUILD.
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700758 # Also set the CXX version of the flags because GCC is now compiled
759 # using C++.
Martin Roth987d42d2018-07-22 11:45:29 -0600760 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200761 CC="$(hostcc target)" CXX="$(hostcxx target)" \
Nico Huber3f12d932017-08-31 23:11:18 +0200762 CFLAGS_FOR_TARGET="-O2 -Dinhibit_libc" \
Patrick Georgia7a5a562017-06-26 07:21:46 +0200763 CFLAGS="$HOSTCFLAGS $CLANGFLAGS" \
764 CFLAGS_FOR_BUILD="$HOSTCFLAGS $CLANGFLAGS" \
765 CXXFLAGS="$HOSTCFLAGS $CLANGCXXFLAGS" \
766 CXXFLAGS_FOR_BUILD="$HOSTCFLAGS $CLANGCXXFLAGS" \
767 ../gcc-${GCC_VERSION}/configure \
Martin Roth987d42d2018-07-22 11:45:29 -0600768 --prefix="$TARGETDIR" --libexecdir="$TARGETDIR/lib" \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700769 --target=${TARGETARCH} --disable-werror --disable-shared \
Stefan Reinauer2f97ebc2015-06-09 12:24:54 -0700770 --enable-lto --enable-plugins --enable-gold --enable-ld=default \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700771 --disable-libssp --disable-bootstrap --disable-nls \
772 --disable-libquadmath --without-headers \
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100773 --disable-threads \
Patrick Georgi1fc02d12016-03-23 21:44:43 +0100774 --enable-interwork --enable-multilib --enable-targets=all \
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100775 --disable-libatomic --disable-libcc1 --disable-decimal-float \
Nico Huberbb313bf2015-09-08 12:30:27 +0200776 ${GCC_OPTIONS} --enable-languages="${LANGUAGES}" \
Ronald G. Minnich7ee16b72016-02-12 21:54:59 +0000777 --with-system-zlib \
Martin Roth987d42d2018-07-22 11:45:29 -0600778 --with-gmp="$DESTDIR$TARGETDIR" --with-mpfr="$DESTDIR$TARGETDIR" \
779 --with-mpc="$DESTDIR$TARGETDIR" \
Patrick Georgidce4d462018-09-07 01:00:54 +0200780 --with-gnu-as --with-gnu-ld \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700781 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE" \
Nico Huber659f40b2018-05-23 17:06:53 +0200782 && \
Patrick Georgi095db332018-06-26 21:00:58 +0200783 mkdir -p gcc/$TARGETARCH && \
Martin Roth987d42d2018-07-22 11:45:29 -0600784 ln -s "$DESTDIR$TARGETDIR/$TARGETARCH/bin" "gcc/$TARGETARCH/$GCC_VERSION" && \
Nico Huber659f40b2018-05-23 17:06:53 +0200785 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-gcc && \
Martin Roth987d42d2018-07-22 11:45:29 -0600786 $MAKE install-gcc DESTDIR="$DESTDIR" || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700787
Martin Roth987d42d2018-07-22 11:45:29 -0600788 if [ ! -f .failed ] && [ "$(echo $TARGETARCH | grep -c -- -mingw32)" -eq 0 ]; then
789 # shellcheck disable=SC2086
Nico Huber659f40b2018-05-23 17:06:53 +0200790 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-target-libgcc && \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700791 $MAKE install-target-libgcc DESTDIR=$DESTDIR || touch .failed
792 fi
793}
794
Nico Huber234d2462016-01-26 16:10:17 +0100795build_GCC() {
796 if [ "$1" = host ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600797 bootstrap_GCC "$1"
Nico Huber234d2462016-01-26 16:10:17 +0100798 else
Martin Roth987d42d2018-07-22 11:45:29 -0600799 build_cross_GCC "$1"
Nico Huber234d2462016-01-26 16:10:17 +0100800 fi
801}
802
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700803build_EXPAT() {
Nico Huber08bb8372017-06-24 19:50:35 +0200804 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS"
Martin Roth987d42d2018-07-22 11:45:29 -0600805 ../${EXPAT_DIR}/configure --disable-shared --prefix="$TARGETDIR" \
Nico Huber08bb8372017-06-24 19:50:35 +0200806 || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700807 $MAKE || touch .failed
808 $MAKE install DESTDIR=$DESTDIR || touch .failed
809
810 normalize_dirs
811}
812
813build_PYTHON() {
Nico Huber08bb8372017-06-24 19:50:35 +0200814 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS"
Martin Roth987d42d2018-07-22 11:45:29 -0600815 ../${PYTHON_DIR}/configure --prefix="$TARGETDIR" \
Stefan Reinauera344b682016-04-21 19:29:51 -0700816 || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600817 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700818 $MAKE $JOBS || touch .failed
819 $MAKE install DESTDIR=$DESTDIR || touch .failed
820
821 normalize_dirs
822}
823
824build_GDB() {
825 export PYTHONHOME=$DESTDIR$TARGETDIR
Martin Roth987d42d2018-07-22 11:45:29 -0600826 if [ "$UNAME" != "FreeBSD" ] && [ "$UNAME" != "NetBSD" ]; then
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700827 LIBDL="-ldl"
828 fi
829 LDFLAGS="-Wl,-rpath,\$\$ORIGIN/../lib/ -L$DESTDIR$TARGETDIR/lib \
830 -lpthread $LIBDL -lutil" \
Nico Huber08bb8372017-06-24 19:50:35 +0200831 CC="$(hostcc target)" CXX="$(hostcxx target)" \
832 CFLAGS="$HOSTCFLAGS -I$DESTDIR$TARGETDIR/include" \
Martin Roth987d42d2018-07-22 11:45:29 -0600833 ../${GDB_DIR}/configure --prefix="$TARGETDIR" \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700834 --target=${TARGETARCH} --disable-werror --disable-nls
Martin Roth987d42d2018-07-22 11:45:29 -0600835 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700836 $MAKE $JOBS || touch .failed
837 $MAKE install DESTDIR=$DESTDIR || touch .failed
838}
839
840build_IASL() {
841 RDIR=$PWD
Martin Roth987d42d2018-07-22 11:45:29 -0600842 cd ../$IASL_DIR/generate/unix || exit 1
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700843 CFLAGS="$HOSTCFLAGS"
844 HOST="_LINUX"
Martin Roth987d42d2018-07-22 11:45:29 -0600845 test "$UNAME" = "Darwin" && HOST="_APPLE"
846 test "$UNAME" = "FreeBSD" && HOST="_FreeBSD"
847 test "$UNAME" = "Cygwin" && HOST="_CYGWIN"
Nico Huber08bb8372017-06-24 19:50:35 +0200848 HOST="$HOST" CFLAGS="$CFLAGS" \
849 OPT_CFLAGS="-O -D_FORTIFY_SOURCE=2 -D COREBOOT_TOOLCHAIN_VERSION='\"coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE\"' " \
Stefan Reinauer7385b6562019-10-27 00:39:47 -0700850 $MAKE $JOBS CC="$(hostcc host)" iasl acpibin acpidump acpiexec acpihelp acpinames acpisrc acpixtract
851 rm -f "$DESTDIR$TARGETDIR/bin/{iasl,acpibin,acpidump,acpiexec,acpihelp,acpinames,acpisrc,acpixtract}" || touch "$RDIR/.failed"
852 cp bin/{iasl,acpibin,acpidump,acpiexec,acpihelp,acpinames,acpisrc,acpixtract} "$DESTDIR$TARGETDIR/bin" || touch "$RDIR/.failed"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700853}
854
855build_LLVM() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700856
Martin Roth987d42d2018-07-22 11:45:29 -0600857 cd .. || exit 1
858 ln -sf "$PWD/$CFE_DIR" "$LLVM_DIR/tools/clang"
859 ln -sf "$PWD/$CTE_DIR" "$LLVM_DIR/tools/clang/tools/extra"
860 ln -sf "$PWD/$CRT_DIR" "$LLVM_DIR/projects/compiler-rt"
861 cd - || exit 1
862
863 $CMAKE -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$DESTDIR$TARGETDIR" \
Martin Rothfb5647a2015-12-18 09:26:45 -0700864 -DCLANG_VENDOR="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE - " \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700865 -DCMAKE_BUILD_TYPE=Release ../$LLVM_DIR || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600866 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700867 $MAKE $JOBS || touch .failed
868 $MAKE install || touch .failed
869
Martin Roth987d42d2018-07-22 11:45:29 -0600870 cp -a ../$CFE_DIR/tools/scan-build/* "$DESTDIR$TARGETDIR/bin"
871 cp -a ../$CFE_DIR/tools/scan-view/* "$DESTDIR$TARGETDIR/bin"
Stefan Reinauere9e6e3d2015-07-07 00:20:42 +0200872
873 # create symlinks to work around broken --print-librt-file-name
874 # when used with -target.
Martin Roth987d42d2018-07-22 11:45:29 -0600875 cd "$DESTDIR$TARGETDIR/lib/clang/${CLANG_VERSION}/lib" || exit 1
Stefan Reinauere9e6e3d2015-07-07 00:20:42 +0200876 for i in */libclang_rt.builtins*.a; do
Martin Roth987d42d2018-07-22 11:45:29 -0600877 ln -s "$i" .
Stefan Reinauere9e6e3d2015-07-07 00:20:42 +0200878 done
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700879}
880
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800881build_MAKE() {
Stefan Reinauerf5fa96f2019-09-21 17:39:30 -0700882 ( cd ../${MAKE_DIR}/ && aclocal && autoconf )
Nico Huber08bb8372017-06-24 19:50:35 +0200883 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS" \
Martin Roth987d42d2018-07-22 11:45:29 -0600884 ../${MAKE_DIR}/configure --prefix="$TARGETDIR" --disable-nls \
Nico Huber08bb8372017-06-24 19:50:35 +0200885 || touch .failed
Stefan Reinauerf5fa96f2019-09-21 17:39:30 -0700886 $MAKE -k clean
Martin Roth987d42d2018-07-22 11:45:29 -0600887 # shellcheck disable=SC2086
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800888 $MAKE $JOBS || touch .failed
889 $MAKE install DESTDIR=$DESTDIR || touch .failed
890
891 normalize_dirs
892}
893
Stefan Reinauer3b593492017-06-19 17:28:54 -0700894build_CMAKE() {
Nico Huber08bb8372017-06-24 19:50:35 +0200895 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS" \
Martin Roth987d42d2018-07-22 11:45:29 -0600896 ../${CMAKE_DIR}/configure --prefix="$TARGETDIR" \
Stefan Reinauer3b593492017-06-19 17:28:54 -0700897 || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600898 # shellcheck disable=SC2086
Stefan Reinauer3b593492017-06-19 17:28:54 -0700899 $MAKE $JOBS || touch .failed
900 $MAKE install DESTDIR=$DESTDIR || touch .failed
901
902 normalize_dirs
903}
904
Martin Rothd70f5fa2019-05-26 17:24:19 -0600905build_NASM() {
906 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS"
907 ../${NASM_DIR}/configure --prefix="$TARGETDIR" \
908 || touch .failed
909 # shellcheck disable=SC2086
910 $MAKE $JOBS || touch .failed
911 $MAKE install DESTDIR=$DESTDIR || touch .failed
912
913 normalize_dirs
914}
915
Stefan Reinauerc66fd3c2017-06-21 14:57:28 -0700916print_supported() {
917 case "$PRINTSUPPORTED" in
Martin Rothedf965a2015-11-25 16:58:33 -0700918 AUTOCONF|autoconf) printf "%s\n" "$GCC_AUTOCONF_VERSION";;
919 BINUTILS|binutils) printf "%s\n" "$BINUTILS_VERSION";;
920 CLANG|clang) printf "%s\n" "$CLANG_VERSION";;
921 EXPAT|expat) printf "%s\n" "$EXPAT_VERSION";;
922 GCC|gcc) printf "%s\n" "$GCC_VERSION";;
923 GDB|gdb) printf "%s\n" "$GDB_VERSION";;
924 GMP|gmp) printf "%s\n" "$GMP_VERSION";;
925 IASL|iasl) printf "%s\n" "$IASL_VERSION";;
Martin Rothedf965a2015-11-25 16:58:33 -0700926 MPC|mpc) printf "%s\n" "$MPC_VERSION";;
927 MPFR|mpfr) printf "%s\n" "$MPFR_VERSION";;
928 PYTHON|python) printf "%s\n" "$PYTHON_VERSION";;
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800929 MAKE|make) printf "%s\n" "$MAKE_VERSION";;
Martin Rothd70f5fa2019-05-26 17:24:19 -0600930 NASM|nasm) printf "%s\n" "${NASM_VERSION}";;
Stefan Reinauerc66fd3c2017-06-21 14:57:28 -0700931 *) printf "Unknown tool %s\n" "$PRINTSUPPORTED";;
Martin Rothedf965a2015-11-25 16:58:33 -0700932 esac
933}
Stefan Reinauer074d9132009-09-26 16:43:17 +0000934
Nico Huber18c74d62017-06-24 20:35:59 +0200935trap exit_handler 1 2 3 15
Zheng Baobb003c82016-08-05 13:41:51 +0800936
Patrick Georgi6bba29f2010-01-29 17:40:52 +0000937# Look if we have getopt. If not, build it.
938export PATH=$PATH:.
939getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
940
Stefan Reinauer074d9132009-09-26 16:43:17 +0000941# parse parameters.. try to find out whether we're running GNU getopt
Nico Huber27e8412b2017-12-05 22:02:21 +0100942getoptbrand="$(getopt -V 2>/dev/null | sed -e '1!d' -e 's,^\(......\).*,\1,')"
Peter Stuge09377b72011-08-21 06:24:55 +0200943if [ "${getoptbrand}" = "getopt" ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000944 # Detected GNU getopt that supports long options.
Patrick Georgi9681cdc2017-09-22 18:57:10 +0200945 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 +0200946 getopt_ret=$?
Nico Hubercc414dd2016-09-20 13:59:38 +0200947 eval set -- "$args"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000948else
949 # Detected non-GNU getopt
Patrick Georgi9681cdc2017-09-22 18:57:10 +0200950 args=$(getopt Vhcd:bBp:l:P:j:D:tSys:un $*)
Nico Huber78df0bf2016-09-20 14:11:53 +0200951 getopt_ret=$?
Martin Roth987d42d2018-07-22 11:45:29 -0600952 # shellcheck disable=SC2086
Stefan Reinauer074d9132009-09-26 16:43:17 +0000953 set -- $args
954fi
955
Nico Huber78df0bf2016-09-20 14:11:53 +0200956if [ $getopt_ret != 0 ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000957 myhelp
958 exit 1
959fi
960
961while true ; do
962 case "$1" in
Patrick Georgi9681cdc2017-09-22 18:57:10 +0200963 -V|--version) shift; myversion; exit 0;;
964 -h|--help) shift; myhelp; exit 0;;
965 -c|--clean) shift; clean=1;;
966 -t|--savetemps) shift; SAVETEMPS=1;;
967 -d|--directory) shift; TARGETDIR="$1"; shift;;
968 -b|--bootstrap) shift; BOOTSTRAP=1;;
969 -B|--bootstrap-only) shift; BOOTSTRAPONLY=1; BOOTSTRAP=1;;
970 -p|--platform) shift; TARGETARCH="$1"; shift;;
971 -l|--languages) shift; LANGUAGES="$1"; shift;;
972 -D|--destdir) shift; DESTDIR="$1"; shift;;
973 -j|--jobs) shift; THREADS="$1"; JOBS="-j $1"; shift;;
974 -P|--package) shift; PACKAGE="$1"; shift;;
975 -S|--scripting) shift; SKIPPYTHON=0;;
976 -y|--ccache) shift; USECCACHE=1;;
977 -s|--supported) shift; PRINTSUPPORTED="$1"; shift;;
978 -u|--urls) shift; printf "%s\n" "$ALL_ARCHIVES"; exit 0;;
979 -n|--nocolor) shift; \
Martin Roth4a53db02016-03-01 15:21:07 -0700980 unset red RED green GREEN blue BLUE cyan CYAN NC;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200981 --) shift; break;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200982 *) break;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000983 esac
984done
985
Nico Huberbd74d562016-09-20 14:16:43 +0200986if [ $# -gt 0 ]; then
987 printf "Excessive arguments: $*\n"
988 myhelp
989 exit 1
990fi
991
Stefan Reinauerc66fd3c2017-06-21 14:57:28 -0700992if [ -n "$PRINTSUPPORTED" ]; then
993 print_supported
Martin Rothedf965a2015-11-25 16:58:33 -0700994 exit 0
995fi
996
997#print toolchain builder version string as the header
998printversion
999
Martin Roth2c6a8062016-11-14 11:58:39 -07001000printf "Building toolchain using %d thread(s).\n\n" "$THREADS"
1001
Ronald G. Minnichb460a662013-05-26 05:33:35 -07001002case "$TARGETARCH" in
Timothy Pearsona9f62352015-12-30 18:59:00 -06001003 x86_64-elf) ;;
1004 x86_64*) TARGETARCH=x86_64-elf;;
1005 i386-elf) ;;
1006 i386-mingw32) ;;
Iru Caiae8e3a02016-04-02 10:50:59 +08001007 riscv-elf) TARGETARCH=riscv64-elf;;
Patrick Georgi0a97d7e2016-01-25 09:51:22 +01001008 powerpc64*-linux*) ;;
Timothy Pearsona9f62352015-12-30 18:59:00 -06001009 i386*) TARGETARCH=i386-elf;;
Stefan Reinauer27522ad2016-01-29 17:31:34 -08001010 arm*) TARGETARCH=arm-eabi;;
Timothy Pearsona9f62352015-12-30 18:59:00 -06001011 aarch64*) TARGETARCH=aarch64-elf;;
Martin Roth3e0f74d2016-03-08 12:07:04 -07001012 nds32le-elf) ;;
Timothy Pearsona9f62352015-12-30 18:59:00 -06001013 *) printf "${red}WARNING: Unsupported architecture $TARGETARCH.${NC}\n\n"; ;;
Ronald G. Minnichb460a662013-05-26 05:33:35 -07001014esac
1015
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001016# Figure out which packages to build
Stefan Reinauer699ac272015-06-05 12:43:28 -07001017
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001018case "$PACKAGE" in
1019 GCC|gcc)
Stefan Reinauer81693d42017-06-21 15:25:36 -07001020 echo "Target architecture is $TARGETARCH"
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001021 NAME="${TARGETARCH} cross GCC"
Nico Huberafda56e2017-12-07 17:50:53 +01001022 PACKAGES="GMP MPFR MPC BINUTILS GCC"
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001023 ;;
1024 GDB|gdb)
1025 NAME="${TARGETARCH} cross GDB"
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001026 if [ $SKIPPYTHON -eq 0 ]; then
Ronald G. Minnichfb944f42016-02-27 04:06:42 +00001027 PACKAGES="EXPAT PYTHON GDB"
1028 else
1029 PACKAGES="EXPAT GDB"
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001030 fi
1031 ;;
1032 CLANG|clang)
Stefan Reinauer81693d42017-06-21 15:25:36 -07001033 NAME="LLVM clang"
1034 LLVM_VERSION=${CLANG_VERSION}
Stefan Reinauerca117e72017-06-19 17:29:08 -07001035 PACKAGES="CMAKE LLVM CFE CRT CTE"
1036 CMAKE=${DESTDIR}${TARGETDIR}/bin/cmake
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001037 ;;
1038 IASL|iasl)
1039 NAME="IASL ACPI compiler"
1040 PACKAGES=IASL
1041 ;;
Stefan Reinauera851bbb2016-01-29 18:03:26 -08001042 MAKE|make)
1043 NAME="GNU Make"
1044 PACKAGES=MAKE
1045 ;;
Stefan Reinauer3b593492017-06-19 17:28:54 -07001046 CMAKE|cmake)
1047 NAME="CMake"
1048 PACKAGES=CMAKE
1049 ;;
Martin Rothd70f5fa2019-05-26 17:24:19 -06001050 NASM|nasm)
1051 NAME="NASM"
1052 PACKAGES=NASM
1053 ;;
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001054 *)
Martin Rothd70f5fa2019-05-26 17:24:19 -06001055 printf "${red}ERROR: Unsupported package $PACKAGE. (Supported packages are GCC, GDB, CLANG, IASL, MAKE, and NASM)${NC}\n\n";
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001056 exit 1
1057 ;;
1058esac
Peter Stugeceacd772011-06-09 04:54:16 +02001059
Stefan Reinauer66e93352015-06-16 22:38:23 +02001060# Find all the required tools:
1061
1062TAR=$(searchtool tar) || exit $?
1063PATCH=$(searchtool patch) || exit $?
1064MAKE=$(searchtool make) || exit $?
1065SHA1SUM=$(searchtool sha1sum)
zbao5cf758d2015-09-01 22:28:57 -04001066#SHA512SUM=$(searchtool sha512sum)
1067#MD5SUM=$(searchtool md5sum)
Stefan Reinauer66e93352015-06-16 22:38:23 +02001068CHECKSUM=$SHA1SUM
Martin Roth95f7b222016-03-20 12:38:48 -06001069LBZIP2=$(searchtool lbzip2 "" nofail)
1070PIGZ=$(searchtool pigz "" nofail)
Stefan Reinauer66e93352015-06-16 22:38:23 +02001071
1072searchtool m4 > /dev/null
1073searchtool bison > /dev/null
1074searchtool flex flex > /dev/null
Stefan Reinauer66e93352015-06-16 22:38:23 +02001075searchtool bzip2 "bzip2," > /dev/null
Nico Huber0ff3b392017-03-12 16:07:33 +01001076searchtool xz "XZ Utils" "" "xz-utils" > /dev/null
Stefan Reinauer66e93352015-06-16 22:38:23 +02001077
Patrick Georgidf1ff232017-01-07 09:28:43 +01001078if searchtool wget "GNU" nofail > /dev/null; then
1079 download_showing_percentage() {
1080 url=$1
Stefan Reinauer81693d42017-06-21 15:25:36 -07001081 printf "... ${red} 0%%"
Martin Roth987d42d2018-07-22 11:45:29 -06001082 wget "$url" 2>&1 | while read -r line; do
1083 echo "$line" | grep -o "[0-9]\+%" | awk '{printf("\b\b\b\b%4s", $1)}'
Patrick Georgidf1ff232017-01-07 09:28:43 +01001084 done
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +02001085 printf "${NC}... "
Patrick Georgidf1ff232017-01-07 09:28:43 +01001086 }
1087elif searchtool curl "^curl " > /dev/null; then
1088 download_showing_percentage() {
1089 url=$1
1090 echo
Martin Roth987d42d2018-07-22 11:45:29 -06001091 curl -#OL "$url"
Patrick Georgidf1ff232017-01-07 09:28:43 +01001092 }
1093fi
1094
Nico Huber08bb8372017-06-24 19:50:35 +02001095# Allow $CC override from the environment.
1096if [ -n "$CC" ]; then
1097 if [ ! -x "$(command -v "$CC" 2>/dev/null)" ]; then
1098 printf "${RED}ERROR:${red} CC is set to '%s' but wasn't found.${NC}\n" "$CC"
1099 HALT_FOR_TOOLS=1
1100 fi
1101else
Patrick Georgi8f2cdad2017-07-10 11:48:34 +02001102 if searchtool gnatgcc "Free Software Foundation" nofail > /dev/null; then
1103 CC=gnatgcc
1104 elif searchtool gcc "Free Software Foundation" nofail > /dev/null; then
Nico Huber08bb8372017-06-24 19:50:35 +02001105 CC=gcc
1106 else
1107 searchtool cc '^' nofail > /dev/null || please_install gcc
1108 CC=cc
1109 fi
1110fi
Martin Roth591790f2016-03-20 20:39:04 -06001111
Nico Huber08bb8372017-06-24 19:50:35 +02001112# We can leave $CXX empty if it's not set since *buildgcc* never
1113# calls it directly. This way configure scripts can search for
1114# themselves and we still override it when a bootstrapped g++ is
1115# to be used (cf. hostcxx()).
1116if [ -n "$CXX" ]; then
1117 if [ ! -x "$(command -v "$CXX" 2>/dev/null)" ]; then
1118 printf "${RED}ERROR:${red} CXX is set to '%s' but wasn't found.${NC}\n" "$CXX"
1119 HALT_FOR_TOOLS=1
1120 fi
1121else
1122 searchtool g++ "Free Software Foundation" nofail > /dev/null || \
1123 searchtool clang "clang version" nofail > /dev/null || \
1124 searchtool clang "LLVM" "" "g++" > /dev/null
1125fi
1126
1127check_for_library "-lz" "zlib (zlib1g-dev or zlib-devel)"
Nico Huber156d87c2016-09-20 12:59:53 +02001128
Martin Roth88463822016-01-07 11:03:36 -07001129if [ "$HALT_FOR_TOOLS" -ne 0 ]; then
1130 exit 1
1131fi
1132
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001133# This initial cleanup is useful when updating the toolchain script.
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001134
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001135if [ "$clean" = "1" ]; then
1136 cleanup
1137fi
Stefan Reinauer074d9132009-09-26 16:43:17 +00001138
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001139# Set up host compiler and flags needed for various OSes
Stefan Reinauer074d9132009-09-26 16:43:17 +00001140
Patrick Georgif2c15f52015-06-11 21:07:31 +02001141if is_package_enabled "GCC"; then
Patrick Georgie8253fe2017-07-18 11:35:35 +02001142# sane preset: let the configure script figure out things by itself
1143# more importantly, avoid any values that might already linger in the variable
1144OPTIONS="ABI="
Martin Roth987d42d2018-07-22 11:45:29 -06001145if [ "$UNAME" = "Darwin" ]; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001146 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
1147
1148 # generally the OS X compiler can create x64 binaries.
1149 # Per default it generated i386 binaries in 10.5 and x64
1150 # binaries in 10.6 (even if the kernel is 32bit)
1151 # For some weird reason, 10.5 autodetects an ABI=64 though
1152 # so we're setting the ABI explicitly here.
Martin Roth987d42d2018-07-22 11:45:29 -06001153 if [ "$(sysctl -n hw.optional.x86_64 2>/dev/null)" -eq 1 ] 2>/dev/null; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001154 OPTIONS="ABI=64"
1155 else
1156 OPTIONS="ABI=32"
1157 fi
Stefan Reinauer9491b4d2011-10-11 22:37:59 -07001158
Stefan Reinauer4266b922012-12-05 16:18:32 -08001159 # In Xcode 4.5.2 the default compiler is clang.
1160 # However, this compiler fails to compile gcc 4.7.x. As a
1161 # workaround it's possible to compile gcc with llvm-gcc.
Stefan Reinauer9491b4d2011-10-11 22:37:59 -07001162 if $CC -v 2>&1 | grep -q LLVM; then
Stefan Reinauer4266b922012-12-05 16:18:32 -08001163 CC=llvm-gcc
Stefan Reinauer9491b4d2011-10-11 22:37:59 -07001164 fi
Martin Roth987d42d2018-07-22 11:45:29 -06001165elif [ "$UNAME" = "Linux" ] || [ "$UNAME" = "Cygwin" ]; then
Patrick Georgiddb8f802015-07-04 17:45:54 +02001166 # gmp is overeager with detecting 64bit CPUs even if they run
1167 # a 32bit kernel and userland.
1168 if [ "$(uname -m 2>/dev/null)" = "i686" ]; then
1169 OPTIONS="ABI=32"
1170 fi
Martin Roth987d42d2018-07-22 11:45:29 -06001171elif [ "$UNAME" = "NetBSD" ]; then
Nico Huberde45c592016-01-20 23:22:33 +01001172 # same for NetBSD but this one reports an i386
1173 if [ "$(uname -m 2>/dev/null)" = "i386" ]; then
1174 OPTIONS="ABI=32"
1175 fi
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001176fi
Nico Huber66c2c1a2016-09-20 13:09:29 +02001177if [ -z "${LANGUAGES}" ]; then
Nico Hubercd87e1b2017-06-24 20:35:59 +02001178 if have_gnat; then
Nico Huber66c2c1a2016-09-20 13:09:29 +02001179 printf "\nFound compatible Ada compiler, enabling Ada support by default.\n\n"
1180 LANGUAGES="ada,${DEFAULT_LANGUAGES}"
1181 else
Nico Hubercd87e1b2017-06-24 20:35:59 +02001182 printf "\n${red}WARNING${NC}\n"
1183 printf "No compatible Ada compiler (GNAT) found. You can continue without\n"
1184 printf "Ada support, but this will limit the features of ${blue}coreboot${NC} (e.g.\n"
1185 printf "native graphics initialization won't be available on most Intel\n"
1186 printf "boards).\n\n"
1187
1188 printf "Usually, you can install GNAT with your package management system\n"
1189 printf "(the package is called \`gnat\` or \`gcc-ada\`). It has to match the\n"
1190 printf "\`gcc\` package in version. If there are multiple versions of GCC in-\n"
1191 printf "stalled, you can point this script to the matching version through\n"
1192 printf "the \`CC\` and \`CXX\` environment variables.\n\n"
1193
1194 printf "e.g. on Ubuntu 14.04, if \`gcc\` is \`gcc-4.8\`:\n"
1195 printf " apt-get install gnat-4.8 && make crossgcc\n\n"
1196
1197 printf "on Ubuntu 16.04, if \`gcc\` is \`gcc-5\`:\n"
1198 printf " apt-get install gnat-5 && make crossgcc\n"
1199 timeout 30
Nico Huber66c2c1a2016-09-20 13:09:29 +02001200 LANGUAGES="${DEFAULT_LANGUAGES}"
1201 fi
1202fi
Nico Hubercd87e1b2017-06-24 20:35:59 +02001203if ada_requested; then
1204 if have_gnat; then
Martin Roth987d42d2018-07-22 11:45:29 -06001205 if [ "$BOOTSTRAP" != 1 ] && \
Stefan Taunerb75a08b2018-08-01 06:57:36 +02001206 { [ "$(hostcc_major)" -lt 4 ] || \
1207 { [ "$(hostcc_major)" -eq 4 ] && \
1208 [ "$(hostcc_minor)" -lt 9 ] ; } ; }
Nico Hubercd87e1b2017-06-24 20:35:59 +02001209 then
1210 printf "\n${red}WARNING${NC}\n"
1211 printf "Building the Ada compiler (GNAT $(buildcc_version)) with a host compiler older\n"
1212 printf "than 4.9.x (yours $(hostcc_version)) requires bootstrapping. This will take\n"
1213 printf "significantly longer than a usual build. You can abort and update\n"
1214 printf "your host GNAT or disable Ada support with BUILD_LANGUAGES=c (or\n"
1215 printf "\`-l c\` in case you invoke \`buildgcc\` directly).\n"
1216 timeout 15
1217 BOOTSTRAP=1
1218 fi
1219 else
1220 please_install gnat gcc-ada
1221 exit 1
1222 fi
1223else
Martin Roth987d42d2018-07-22 11:45:29 -06001224 if [ "$(hostcc_major)" -lt 4 ] && [ "$BOOTSTRAP" != 1 ]; then
Nico Hubercd87e1b2017-06-24 20:35:59 +02001225 printf "\n${red}WARNING${NC}\n"
1226 printf "Building GCC $(buildcc_version) with a very old host compiler ($(hostcc_version)).\n"
1227 printf "Bootstrapping (-b) is recommended.\n"
1228 timeout 10
1229 fi
1230fi
Stefan Reinauer14ce2132015-06-05 13:01:13 -07001231fi # GCC
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001232
Nico Huber5f72f962016-01-31 23:05:19 +01001233export HOSTCFLAGS="-Os"
1234if have_hostcflags_from_gmp; then
1235 set_hostcflags_from_gmp
1236fi
1237
Patrick Georgic1a75b12011-11-04 21:37:14 +01001238if [ "$USECCACHE" = 1 ]; then
1239 CC="ccache $CC"
1240fi
1241
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001242# Prepare target directory for building GCC
1243# (dependencies must be in the PATH)
Martin Roth987d42d2018-07-22 11:45:29 -06001244mkdir -p "$DESTDIR$TARGETDIR/bin"
1245mkdir -p "$DESTDIR$TARGETDIR/share"
Stefan Reinauer16bd7892012-12-07 23:57:01 +01001246export PATH=$DESTDIR$TARGETDIR/bin:$PATH
1247
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001248# Download, unpack, patch and build all packages
1249
Elyes HAOUASb0f19882018-06-09 11:59:00 +02001250printf "Downloading and verifing tarballs ...\n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001251mkdir -p tarballs
1252for P in $PACKAGES; do
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +02001253 download "$P" || exit "$?"
1254 verify_hash "$P" || exit "$?"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001255done
Martin Roth03625172016-02-17 14:44:14 -07001256printf "Downloaded tarballs ... ${green}ok${NC}\n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001257
Elyes HAOUASb0f19882018-06-09 11:59:00 +02001258printf "Unpacking and patching ...\n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001259for P in $PACKAGES; do
1260 unpack_and_patch $P || exit 1
1261done
1262printf "Unpacked and patched ... ${green}ok${NC}\n"
1263
Patrick Georgi9681cdc2017-09-22 18:57:10 +02001264if [ -n "$BOOTSTRAPONLY" ]; then
Elyes HAOUASb0f19882018-06-09 11:59:00 +02001265 printf "Building bootstrap compiler only ...\n"
Nico Huberafda56e2017-12-07 17:50:53 +01001266 for pkg in GMP MPFR MPC GCC; do
Patrick Georgi9681cdc2017-09-22 18:57:10 +02001267 build_for_host $pkg
1268 done
1269 exit 0
1270fi
1271
Elyes HAOUASb0f19882018-06-09 11:59:00 +02001272printf "Building packages ...\n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -07001273for package in $PACKAGES; do
1274 build $package
1275done
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001276printf "Packages built ... ${green}ok${NC}\n"
Stefan Reinauer14ce2132015-06-05 13:01:13 -07001277
Stefan Reinauer699ac272015-06-05 12:43:28 -07001278# Adding git information of current tree to target directory
1279# for reproducibility
Stefan Reinauerd7649122015-06-09 11:44:25 -07001280PROGNAME=$(basename "$0")
Stefan Reinauer88352d72016-04-06 15:57:03 -07001281rm -f "$DESTDIR$TARGETDIR/share/$PROGNAME-*"
Patrick Georgi4eb84cf2017-07-07 11:18:46 +02001282cp "$PROGNAME" "$DESTDIR$TARGETDIR/share/$PROGNAME-$CROSSGCC_VERSION-$CROSSGCC_COMMIT"
Zheng Bao30b895f2013-02-06 18:04:40 +08001283
Patrick Georgi0c2df6f2017-11-22 16:14:09 -05001284# Adding edk2 tools template
1285mkdir -p "$DESTDIR$TARGETDIR/share/edk2config"
1286sed -e "s,@@PREFIX@@,$TARGETDIR,g" edk2tools.txt > "$DESTDIR$TARGETDIR/share/edk2config/tools_def.txt"
1287printf "Copied EDK2 tools template ... ${green}ok${NC}\n"
1288
Stefan Reinauer88e83e52016-04-06 15:39:48 -07001289cleanup
Stefan Reinauer074d9132009-09-26 16:43:17 +00001290
Stefan Reinauer81693d42017-06-21 15:25:36 -07001291printf "\n${green}You can now run $NAME from $TARGETDIR.${NC}\n"