blob: 150e6166525e3c910480b74c2a4facdd277ca614 [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#
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000015# Copyright (C) 2008-2010 by coresystems GmbH
Stefan Reinauer074d9132009-09-26 16:43:17 +000016# written by Patrick Georgi <patrick.georgi@coresystems.de> and
17# Stefan Reinauer <stefan.reinauer@coresystems.de>
Stefan Reinauer14e22772010-04-27 06:56:47 +000018#
Marc Jones2aac3f62011-08-08 16:07:50 -060019# Copyright (C) 2011 by Sage Electronic Engineering
20#
Stefan Reinauer074d9132009-09-26 16:43:17 +000021# This program is free software; you can redistribute it and/or modify
22# it under the terms of the GNU General Public License as published by
23# the Free Software Foundation; version 2 of the License.
24#
25# This program is distributed in the hope that it will be useful,
26# but WITHOUT ANY WARRANTY; without even the implied warranty of
27# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28# GNU General Public License for more details.
29#
Stefan Reinauer074d9132009-09-26 16:43:17 +000030
Martin Roth987d42d2018-07-22 11:45:29 -060031cd "$(dirname "$0")" || exit 1
Patrick Georgi8135dba2015-03-17 21:05:20 +010032
Patrick Georgi5ce73e92019-01-10 14:07:56 +010033CROSSGCC_DATE="$(git log -n 1 --pretty=%ad --date=short .)"
Wonkyu Kim31eeda12019-01-18 17:33:55 -080034CROSSGCC_VERSION="$(git log -n 1 --pretty=%h .)"
Stefan Reinauer88352d72016-04-06 15:57:03 -070035CROSSGCC_COMMIT=$( git describe )
Stefan Reinauer074d9132009-09-26 16:43:17 +000036
37# default settings
Stefan Reinauer85b07d62015-06-09 14:45:14 -070038PACKAGE=GCC
Stefan Reinauerd7649122015-06-09 11:44:25 -070039TARGETDIR=$(pwd)/xgcc
Stefan Reinauer074d9132009-09-26 16:43:17 +000040TARGETARCH=i386-elf
Nico Huber66c2c1a2016-09-20 13:09:29 +020041DEFAULT_LANGUAGES=c
42LANGUAGES=
Stefan Reinauer074d9132009-09-26 16:43:17 +000043DESTDIR=
Stefan Reinauer85b07d62015-06-09 14:45:14 -070044SAVETEMPS=0
45SKIPPYTHON=1
Nico Huber234d2462016-01-26 16:10:17 +010046BOOTSTRAP=0
Martin Roth2c6a8062016-11-14 11:58:39 -070047THREADS=1
Stefan Reinauer074d9132009-09-26 16:43:17 +000048
Stefan Reinauer699ac272015-06-05 12:43:28 -070049# GCC toolchain version numbers
Iru Cai03353de2016-10-29 23:37:42 +080050GMP_VERSION=6.1.2
Elyes HAOUASc3c9afb2019-02-03 15:51:42 +010051MPFR_VERSION=4.0.2
Stefan Reinaueraedb1df2018-10-02 16:00:22 -070052MPC_VERSION=1.1.0
Patrick Rudolph6b88f902020-03-15 07:29:14 +010053GCC_VERSION=8.3.0
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070054GCC_AUTOCONF_VERSION=2.69
Elyes HAOUAS18ecdbf2019-10-14 10:16:29 +020055BINUTILS_VERSION=2.33.1
Elyes HAOUAS23686812019-09-21 12:23:16 +020056GDB_VERSION=8.3.1
Elyes HAOUAS65718762020-02-04 07:33:47 +010057IASL_VERSION=20200110
Elyes HAOUASa87a7412019-12-19 11:50:43 +010058PYTHON_VERSION=3.8.1
Elyes HAOUASa2fbddf2019-10-03 10:38:39 +020059EXPAT_VERSION=2.2.9
Stefan Reinauer14ce2132015-06-05 13:01:13 -070060# CLANG version number
Elyes HAOUAS3ba84c52019-09-20 09:01:53 +020061CLANG_VERSION=9.0.0
Martin Roth589ef9d2016-08-11 18:35:06 -060062MAKE_VERSION=4.2.1
Elyes HAOUAS40f539f2020-01-03 16:09:35 +010063CMAKE_VERSION=3.16.2
Martin Rothd70f5fa2019-05-26 17:24:19 -060064NASM_VERSION=2.14.02
Stefan Reinauer074d9132009-09-26 16:43:17 +000065
Stefan Reinauer699ac272015-06-05 12:43:28 -070066# GCC toolchain archive locations
Martin Roth1484c032016-03-08 09:37:14 -070067# These are sanitized by the jenkins toolchain test builder, so if
68# a completely new URL is added here, it probably needs to be added
69# to the jenkins build as well, or the builder won't download it.
Doug Gale43ae4332017-09-27 22:10:16 -040070GMP_ARCHIVE="https://ftpmirror.gnu.org/gmp/gmp-${GMP_VERSION}.tar.xz"
71MPFR_ARCHIVE="https://ftpmirror.gnu.org/mpfr/mpfr-${MPFR_VERSION}.tar.xz"
72MPC_ARCHIVE="https://ftpmirror.gnu.org/mpc/mpc-${MPC_VERSION}.tar.gz"
Patrick Georgib1d26f02018-05-02 17:13:34 +020073GCC_ARCHIVE="https://ftpmirror.gnu.org/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.xz"
Iru Cai776b5ba2017-07-28 21:40:34 +080074BINUTILS_ARCHIVE="https://ftpmirror.gnu.org/binutils/binutils-${BINUTILS_VERSION}.tar.xz"
Doug Gale43ae4332017-09-27 22:10:16 -040075GDB_ARCHIVE="https://ftpmirror.gnu.org/gdb/gdb-${GDB_VERSION}.tar.xz"
Patrick Georgi85bb9462016-02-11 17:22:16 +010076IASL_ARCHIVE="https://acpica.org/sites/acpica/files/acpica-unix2-${IASL_VERSION}.tar.gz"
Doug Gale43ae4332017-09-27 22:10:16 -040077PYTHON_ARCHIVE="https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz"
78EXPAT_ARCHIVE="https://downloads.sourceforge.net/sourceforge/expat/expat-${EXPAT_VERSION}.tar.bz2"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070079# CLANG toolchain archive locations
Doug Gale43ae4332017-09-27 22:10:16 -040080LLVM_ARCHIVE="https://releases.llvm.org/${CLANG_VERSION}/llvm-${CLANG_VERSION}.src.tar.xz"
81CFE_ARCHIVE="https://releases.llvm.org/${CLANG_VERSION}/cfe-${CLANG_VERSION}.src.tar.xz"
82CRT_ARCHIVE="https://releases.llvm.org/${CLANG_VERSION}/compiler-rt-${CLANG_VERSION}.src.tar.xz"
83CTE_ARCHIVE="https://releases.llvm.org/${CLANG_VERSION}/clang-tools-extra-${CLANG_VERSION}.src.tar.xz"
84MAKE_ARCHIVE="https://ftpmirror.gnu.org/make/make-${MAKE_VERSION}.tar.bz2"
Elyes HAOUAS18315db2019-11-26 19:12:21 +010085CMAKE_ARCHIVE="https://cmake.org/files/v3.16/cmake-${CMAKE_VERSION}.tar.gz"
Martin Rothd70f5fa2019-05-26 17:24:19 -060086NASM_ARCHIVE="https://www.nasm.us/pub/nasm/releasebuilds/${NASM_VERSION}/nasm-${NASM_VERSION}.tar.bz2"
Stefan Reinauer074d9132009-09-26 16:43:17 +000087
Nico Huberafda56e2017-12-07 17:50:53 +010088ALL_ARCHIVES="$GMP_ARCHIVE $MPFR_ARCHIVE $MPC_ARCHIVE \
Martin Roth58c68d52016-03-01 15:22:42 -070089 $GCC_ARCHIVE $BINUTILS_ARCHIVE $GDB_ARCHIVE $IASL_ARCHIVE \
90 $PYTHON_ARCHIVE $EXPAT_ARCHIVE $LLVM_ARCHIVE $CFE_ARCHIVE \
Martin Rothd70f5fa2019-05-26 17:24:19 -060091 $CRT_ARCHIVE $CTE_ARCHIVE $MAKE_ARCHIVE $CMAKE_ARCHIVE \
92 $NASM_ARCHIVE"
Martin Roth58c68d52016-03-01 15:22:42 -070093
Stefan Reinauer699ac272015-06-05 12:43:28 -070094# GCC toolchain directories
Stefan Reinauer074d9132009-09-26 16:43:17 +000095GMP_DIR="gmp-${GMP_VERSION}"
96MPFR_DIR="mpfr-${MPFR_VERSION}"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000097MPC_DIR="mpc-${MPC_VERSION}"
Martin Roth987d42d2018-07-22 11:45:29 -060098# shellcheck disable=SC2034
Stefan Reinauer074d9132009-09-26 16:43:17 +000099GCC_DIR="gcc-${GCC_VERSION}"
Martin Roth987d42d2018-07-22 11:45:29 -0600100# shellcheck disable=SC2034
Stefan Reinauer074d9132009-09-26 16:43:17 +0000101BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
102GDB_DIR="gdb-${GDB_VERSION}"
Patrick Georgi85bb9462016-02-11 17:22:16 +0100103IASL_DIR="acpica-unix2-${IASL_VERSION}"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100104PYTHON_DIR="Python-${PYTHON_VERSION}"
105EXPAT_DIR="expat-${EXPAT_VERSION}"
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700106# CLANG toolchain directories
107LLVM_DIR="llvm-${CLANG_VERSION}.src"
108CFE_DIR="cfe-${CLANG_VERSION}.src"
109CRT_DIR="compiler-rt-${CLANG_VERSION}.src"
110CTE_DIR="clang-tools-extra-${CLANG_VERSION}.src"
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800111MAKE_DIR="make-${MAKE_VERSION}"
Stefan Reinauer3b593492017-06-19 17:28:54 -0700112CMAKE_DIR="cmake-${CMAKE_VERSION}"
Martin Rothd70f5fa2019-05-26 17:24:19 -0600113NASM_DIR="nasm-${NASM_VERSION}"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000114
Patrick Georgi6321f522015-02-27 23:36:26 +0100115unset MAKELEVEL MAKEFLAGS
116
Patrick Georgi1861ff72011-10-31 12:15:55 +0100117red='\033[0;31m'
118RED='\033[1;31m'
119green='\033[0;32m'
120GREEN='\033[1;32m'
121blue='\033[0;34m'
Patrick Georgi1861ff72011-10-31 12:15:55 +0100122CYAN='\033[1;36m'
123NC='\033[0m' # No Color
Stefan Reinauer074d9132009-09-26 16:43:17 +0000124
Martin Roth987d42d2018-07-22 11:45:29 -0600125UNAME=$(if uname | grep -iq cygwin; then echo Cygwin; else uname; fi)
Martin Roth88463822016-01-07 11:03:36 -0700126HALT_FOR_TOOLS=0
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700127
Nico Huber08bb8372017-06-24 19:50:35 +0200128hostcc()
129{
130 # $1 "host" or "target"
Martin Roth987d42d2018-07-22 11:45:29 -0600131 if [ "$BOOTSTRAP" = 1 ] && [ "$1" = target ]; then
132 echo "$DESTDIR$TARGETDIR/bin/gcc"
Nico Huber08bb8372017-06-24 19:50:35 +0200133 else
Martin Roth987d42d2018-07-22 11:45:29 -0600134 echo "$CC"
Nico Huber08bb8372017-06-24 19:50:35 +0200135 fi
136}
137
138hostcxx()
139{
140 # $1 "host" or "target"
Martin Roth987d42d2018-07-22 11:45:29 -0600141 if [ "$BOOTSTRAP" = 1 ] && [ "$1" = target ]; then
142 echo "$DESTDIR$TARGETDIR/bin/g++"
Nico Huber08bb8372017-06-24 19:50:35 +0200143 else
Martin Roth987d42d2018-07-22 11:45:29 -0600144 echo "$CXX"
Nico Huber08bb8372017-06-24 19:50:35 +0200145 fi
146}
147
Patrick Georgi198d23c2012-12-08 08:02:44 +0100148normalize_dirs()
149{
Martin Roth987d42d2018-07-22 11:45:29 -0600150 mkdir -p "$DESTDIR$TARGETDIR/lib"
151 test -d "$DESTDIR$TARGETDIR/lib32" && mv "$DESTDIR$TARGETDIR"/lib32/* "$DESTDIR$TARGETDIR/lib"
152 test -d "$DESTDIR$TARGETDIR/lib64" && mv "$DESTDIR$TARGETDIR"/lib64/* "$DESTDIR$TARGETDIR/lib"
153 rmdir -p "$DESTDIR$TARGETDIR/lib32" "$DESTDIR$TARGETDIR/lib64"
Patrick Georgi198d23c2012-12-08 08:02:44 +0100154
Martin Roth987d42d2018-07-22 11:45:29 -0600155 perl -pi -e "s,/lib32,/lib," "$DESTDIR$TARGETDIR"/lib/*.la
156 perl -pi -e "s,/lib64,/lib," "$DESTDIR$TARGETDIR"/lib/*.la
Patrick Georgi198d23c2012-12-08 08:02:44 +0100157}
158
Nico Hubercd87e1b2017-06-24 20:35:59 +0200159countdown()
160{
161 tout=${1:-10}
162
Martin Roth987d42d2018-07-22 11:45:29 -0600163 printf "\nPress Ctrl-C to abort, Enter to continue... %2ds" "$tout"
164 while [ "$tout" -gt 0 ]; do
Nico Hubercd87e1b2017-06-24 20:35:59 +0200165 sleep 1
166 tout=$((tout - 1))
167 printf "\b\b\b%2ds" $tout
168 done
169 printf "\n"
170}
171
172timeout()
173{
174 tout=${1:-10}
175
176 # Ignore SIGUSR1, should interrupt `read` though.
177 trap false USR1
178 # Clean up in case the user aborts.
179 trap 'kill $counter > /dev/null 2>&1' EXIT
180
Martin Roth987d42d2018-07-22 11:45:29 -0600181 (countdown "$tout"; kill -USR1 $$)&
Nico Hubercd87e1b2017-06-24 20:35:59 +0200182 counter=$!
183
184 # Some shells with sh compatibility mode (e.g. zsh, mksh) only
185 # let us interrupt `read` if a non-standard -t parameter is given.
Martin Roth987d42d2018-07-22 11:45:29 -0600186 # shellcheck disable=SC2034,SC2039,SC2162
Nico Hubercd87e1b2017-06-24 20:35:59 +0200187 if echo | read -t 1 foo 2>/dev/null; then
188 read -t $((tout + 1)) foo
189 else
190 read foo
191 fi
192
193 kill $counter > /dev/null 2>&1
194 trap - USR1 EXIT
195}
196
Stefan Reinauer93a96302015-06-15 12:36:53 -0700197please_install()
198{
Martin Roth88463822016-01-07 11:03:36 -0700199 HALT_FOR_TOOLS=1
Martin Roth987d42d2018-07-22 11:45:29 -0600200 # shellcheck disable=SC1091
Patrick Georgi5da95dc2015-07-17 23:33:05 +0200201 test -r /etc/os-release && . /etc/os-release
Alex Thiessen72d10892018-01-12 01:45:53 +0000202 # vanilla debian doesn't define `ID_LIKE`, just `ID`
203 if [ -z "${ID_LIKE}" ] && [ -n "${ID}" ]; then
204 ID_LIKE=${ID}
205 fi
Stefan Reinauer93a96302015-06-15 12:36:53 -0700206 case "$ID_LIKE" in
207 debian) solution="sudo apt-get install $1" ;;
208 suse) solution="sudo zypper install $1" ;;
209 *) solution="using your OS packaging system" ;;
210 esac
211
Patrick Georgidf1ff232017-01-07 09:28:43 +0100212 printf "${RED}ERROR:${red} Missing tool: Please install '$1'. (eg $solution)${NC}\n" >&2
Martin Rothe3963172016-01-06 13:54:32 -0700213 if [ -n "$2" ]; then
Patrick Georgidf1ff232017-01-07 09:28:43 +0100214 printf "${RED}ERROR:${red} or install '$2'.${NC}\n" >&2
Martin Rothe3963172016-01-06 13:54:32 -0700215 fi
Stefan Reinauer93a96302015-06-15 12:36:53 -0700216}
217
Patrick Georgib7062882015-02-24 10:52:14 +0100218searchtool()
Stefan Reinauer074d9132009-09-26 16:43:17 +0000219{
220 # $1 short name
Patrick Georgib7062882015-02-24 10:52:14 +0100221 # $2 search string
222 # $3 soft fail if set
Martin Rothe3963172016-01-06 13:54:32 -0700223 # $4 alternative package to install on failure
Patrick Georgib7062882015-02-24 10:52:14 +0100224 # result: file name of that tool on stdout
225 # or no output if nothing suitable was found
226 search=GNU
227 if [ -n "$2" ]; then
228 search="$2"
229 fi
Stefan Reinauer074d9132009-09-26 16:43:17 +0000230 for i in "$1" "g$1" "gnu$1"; do
Martin Roth987d42d2018-07-22 11:45:29 -0600231 if [ -x "$(command -v "$i" 2>/dev/null)" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700232 if [ "$(cat /dev/null | $i --version 2>&1 | grep -c "$search")" \
233 -gt 0 ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600234 echo "$i"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000235 return
236 fi
237 fi
238 done
Andrew Wub67e9a12014-04-28 18:13:44 +0800239 # A workaround for OSX 10.9 and some BSDs, whose nongnu
240 # patch and tar also work.
Martin Roth987d42d2018-07-22 11:45:29 -0600241 if [ "$UNAME" = "Darwin" ] || [ "$UNAME" = "FreeBSD" ] || [ "$UNAME" = "NetBSD" ] || [ "$UNAME" = "OpenBSD" ]; then
242 if [ "$1" = "patch" ] || [ "$1" = "tar" ]; then
243 if [ -x "$(command -v "$1" 2>/dev/null)" ]; then
244 echo "$1"
Zheng Bao36156ff2012-09-28 16:18:58 +0800245 return
246 fi
247 fi
248 fi
Martin Roth987d42d2018-07-22 11:45:29 -0600249 if echo "$1" | grep -q "sum" ; then
250 algor=$(echo "$1" | sed -e 's,sum,,')
251 if [ -x "$(command -v "$1" 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400252 #xxxsum [file]
Martin Roth987d42d2018-07-22 11:45:29 -0600253 echo "$1"
zbao5cf758d2015-09-01 22:28:57 -0400254 return
Martin Roth987d42d2018-07-22 11:45:29 -0600255 elif [ -x "$(command -v "$algor" 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400256 #xxx [file]
Martin Roth987d42d2018-07-22 11:45:29 -0600257 echo "$algor"
zbao5cf758d2015-09-01 22:28:57 -0400258 return
Martin Rothd55f5eb2017-06-15 11:37:26 -0600259 elif [ -x "$(command -v openssl 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400260 #openssl xxx [file]
Martin Roth987d42d2018-07-22 11:45:29 -0600261 echo openssl "$algor"
zbao5cf758d2015-09-01 22:28:57 -0400262 return
Martin Rothd55f5eb2017-06-15 11:37:26 -0600263 elif [ -x "$(command -v cksum 2>/dev/null)" ]; then
zbao5cf758d2015-09-01 22:28:57 -0400264 #cksum -a xxx [file]
265 #cksum has special options in NetBSD. Actually, NetBSD will use the second case above.
Martin Roth987d42d2018-07-22 11:45:29 -0600266 echo "buildgcc" | cksum -a "$algor" > /dev/null 2>/dev/null && \
267 echo cksum -a "$algor"
zbao5cf758d2015-09-01 22:28:57 -0400268 return
zbao939dc842015-04-30 04:44:07 +0800269 fi
270 fi
Martin Rothe3963172016-01-06 13:54:32 -0700271
Martin Roth987d42d2018-07-22 11:45:29 -0600272 [ -z "$3" ] && please_install "$1" "$4"
Patrick Georgib7062882015-02-24 10:52:14 +0100273 false
Stefan Reinauer074d9132009-09-26 16:43:17 +0000274}
275
Martin Roth591790f2016-03-20 20:39:04 -0600276# Run a compile check of the specified library option to see if it's installed
277check_for_library() {
Martin Roth987d42d2018-07-22 11:45:29 -0600278 LIBRARY_FLAGS="$1"
279 LIBRARY_PACKAGES="$2"
280 LIBTEST_FILE=.libtest
Martin Roth591790f2016-03-20 20:39:04 -0600281
282 echo "int main(int argc, char **argv) { (void) argc; (void) argv; return 0; }" > "${LIBTEST_FILE}.c"
283
Martin Roth987d42d2018-07-22 11:45:29 -0600284 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200285 "$CC" $CFLAGS $LIBRARY_FLAGS "${LIBTEST_FILE}.c" -o "${LIBTEST_FILE}" >/dev/null 2>&1 || \
Martin Roth591790f2016-03-20 20:39:04 -0600286 please_install "$LIBRARY_PACKAGES"
287 rm -rf "${LIBTEST_FILE}.c" "${LIBTEST_FILE}"
288}
289
Nico Huberaee7f462016-09-20 12:55:19 +0200290buildcc_major() {
291 echo "${GCC_VERSION}" | cut -d. -f1
292}
293
Nico Huber26267a72016-12-04 02:45:58 +0100294buildcc_minor() {
295 echo "${GCC_VERSION}" | cut -d. -f2
296}
297
Nico Huberaee7f462016-09-20 12:55:19 +0200298buildcc_version() {
299 echo "${GCC_VERSION}" | cut -d. -f1-2
300}
301
302hostcc_major() {
303 (echo __GNUC__ | ${CC} -E - 2>/dev/null || echo 0) | tail -1
304}
305
306hostcc_minor() {
307 (echo __GNUC_MINOR__ | ${CC} -E - 2>/dev/null || echo 0) | tail -1
308}
309
310hostcc_version() {
311 printf "%d.%d" "$(hostcc_major)" "$(hostcc_minor)"
312}
313
Nico Huber75797162016-09-20 13:01:57 +0200314hostcc_has_gnat1() {
315 [ -x "$(${CC} -print-prog-name=gnat1)" ]
316}
317
Nico Hubercd87e1b2017-06-24 20:35:59 +0200318have_gnat() {
319 hostcc_has_gnat1 && \
320 searchtool gnatbind "Free Software Foundation" nofail > /dev/null
321}
322
Nico Huber75797162016-09-20 13:01:57 +0200323ada_requested() {
324 echo "${LANGUAGES}" | grep -q '\<ada\>'
325}
326
Stefan Reinauer88935482015-06-05 09:51:10 -0700327download() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700328 package=$1
Martin Roth987d42d2018-07-22 11:45:29 -0600329 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700330 archive="$(eval echo \$$package"_ARCHIVE")"
Stefan Reinauer699ac272015-06-05 12:43:28 -0700331
Martin Roth987d42d2018-07-22 11:45:29 -0600332 FILE=$(basename "$archive")
Stefan Reinauer88935482015-06-05 09:51:10 -0700333 printf " * $FILE "
334
Martin Roth987d42d2018-07-22 11:45:29 -0600335 if test -f "tarballs/$FILE"; then
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200336 printf "(cached)... "
zbaod22b9ea2015-08-30 23:35:16 -0400337 else
Paul Menzelb2ba3952015-06-12 11:58:14 +0200338 printf "(downloading from $archive)"
Martin Roth987d42d2018-07-22 11:45:29 -0600339 rm -f "tarballs/$FILE"
340 cd tarballs || exit 1
341 download_showing_percentage "$archive"
Stefan Reinauer88935482015-06-05 09:51:10 -0700342 cd ..
zbaod22b9ea2015-08-30 23:35:16 -0400343 fi
344
Martin Roth987d42d2018-07-22 11:45:29 -0600345 if [ ! -f "tarballs/$FILE" ]; then
Patrick Georgidf1ff232017-01-07 09:28:43 +0100346 printf "${RED}Failed to download $FILE.${NC}\n"
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700347 exit 1
348 fi
Stefan Reinauer88935482015-06-05 09:51:10 -0700349}
350
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200351# Compute the hash of the package given in $1, and print it raw (just the
352# hexadecimal hash).
353compute_hash() {
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 if test -z "$CHECKSUM"; then
360 echo "${RED}\$CHECKSUM program missing. This is bad.${NC}" 1>&2
361 exit 1
362 fi
363
364 $CHECKSUM "tarballs/$file" 2>/dev/null | sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@'
365}
366
367error_hash_missing() {
368 package="$1"
Martin Roth987d42d2018-07-22 11:45:29 -0600369 # shellcheck disable=SC2086
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200370 archive="$(eval echo \$$package"_ARCHIVE")"
371 file="$(basename "$archive")"
372
373 fullhashfile="util/crossgcc/sum/$file.cksum"
374 printf "${RED}hash file missing:${NC}\n\n" 1>&2
375 printf "Please verify util/crossgcc/tarball/$file carefully\n" 1>&2
376 printf "(using PGP if possible), and then rename\n" 1>&2
377 printf " ${CYAN}${fullhashfile}.calc${NC}\n" 1>&2
378 printf " to ${CYAN}${fullhashfile}${NC}\n\n" 1>&2
379
380 exit 1
381}
382
383# Read the known hash file of the package given in $1, and print it raw.
384get_known_hash() {
385 package=$1
Martin Roth987d42d2018-07-22 11:45:29 -0600386 # shellcheck disable=SC2086
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200387 archive="$(eval echo \$$package"_ARCHIVE")"
388 file="$(basename "$archive")"
389 hashfile="sum/$file.cksum"
390
391 if [ ! -f "$hashfile" ]; then
392 calc_hash="$(compute_hash "$package")" || exit 1
393 echo "$calc_hash tarballs/$file" > "${hashfile}.calc"
394
395 error_hash_missing "$package"
396 exit 1
397 fi
398
Martin Roth987d42d2018-07-22 11:45:29 -0600399 sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@' < "$hashfile"
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200400}
401
402error_hash_mismatch() {
403 package=$1
404 known_hash="$2"
405 computed_hash="$3"
Martin Roth987d42d2018-07-22 11:45:29 -0600406 # shellcheck disable=SC2086
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200407 archive="$(eval echo \$$package"_ARCHIVE")"
408 file="$(basename "$archive")"
409
410 printf "${RED}hash mismatch:${NC}\n\n"
411 printf " expected (known) hash: $known_hash\n"
412 printf "calculated hash of downloaded file: $computed_hash\n\n"
413
414 printf "If you think this is due to a network error, please delete\n"
415 printf " ${CYAN}util/crossgcc/tarballs/$file${NC}\n"
416 printf "and try again. If the problem persists, it may be due to an\n"
417 printf "administration error on the file server, or you might be\n"
418 printf "subject to a Man-in-the-Middle attack\n\n"
419
420 exit 1
421}
422
423# verify_hash - Check that the hash of the file given in $1 matches the known
424# hash; Bail out on mismatch or missing hash file.
425verify_hash() {
426 package=$1
Martin Roth987d42d2018-07-22 11:45:29 -0600427 # shellcheck disable=SC2086
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200428 archive="$(eval echo \$$package"_ARCHIVE")"
429
430 known_hash="$(get_known_hash "$package")" || exit "$?"
431 computed_hash="$(compute_hash "$package")" || exit "$?"
432
433 if [ "$known_hash" != "$computed_hash" ]; then
434 error_hash_mismatch "$package" "$known_hash" "$computed_hash"
435 exit 1
436 fi
437
Jonathan Neuschäfercb76069e2018-10-01 09:45:49 +0200438 printf "${GREEN}hash verified (${known_hash})${NC}\n"
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +0200439}
440
Stefan Reinauer88935482015-06-05 09:51:10 -0700441unpack_and_patch() {
Martin Roth987d42d2018-07-22 11:45:29 -0600442 package="$1"
443 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700444 archive="$(eval echo \$$package"_ARCHIVE")"
Martin Roth987d42d2018-07-22 11:45:29 -0600445 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700446 dir="$(eval echo \$$package"_DIR")"
Martin Roth987d42d2018-07-22 11:45:29 -0600447 test -d "${dir}" && test -f "${dir}/.unpack_success" || (
448 printf " * $(basename "$archive")\n"
Stefan Reinauer88935482015-06-05 09:51:10 -0700449 FLAGS=zxf
Martin Roth987d42d2018-07-22 11:45:29 -0600450 suffix=$(echo "$archive" | sed 's,.*\.,,')
Martin Roth95f7b222016-03-20 12:38:48 -0600451 if [ "$suffix" = "gz" ] && [ -n "$PIGZ" ]; then FLAGS="-I pigz -xf"
452 elif [ "$suffix" = "gz" ]; then FLAGS=zxf
453 elif [ "$suffix" = "bz2" ] && [ -n "$LBZIP2" ]; then FLAGS="-I lbzip2 -xf"
454 elif [ "$suffix" = "bz2" ]; then FLAGS=jxf
455 elif [ "$suffix" = "xz" ]; then FLAGS="--xz -xf"
456 elif [ "$suffix" = "lzma" ]; then FLAGS="--lzma -xf"
457 fi
Martin Roth987d42d2018-07-22 11:45:29 -0600458 # shellcheck disable=SC2086
459 $TAR $FLAGS "tarballs/$(basename "$archive")"
Stefan Reinauer88935482015-06-05 09:51:10 -0700460 for patch in patches/${dir}_*.patch; do
Martin Roth987d42d2018-07-22 11:45:29 -0600461 test -r "$patch" || continue
462 printf " o $(basename "$patch")\n"
463 (cd "${dir}" || exit 1; $PATCH -s -N -p1 <"../${patch}") || {
Stefan Reinauer88935482015-06-05 09:51:10 -0700464 printf "\n${RED}Failed $patch.${NC}\n"
Stefan Reinauer916b8452015-06-09 11:54:49 -0700465 exit 1
466 }
Stefan Reinauer88935482015-06-05 09:51:10 -0700467 done
Martin Roth987d42d2018-07-22 11:45:29 -0600468 touch "${dir}/.unpack_success"
Stefan Reinauer88935482015-06-05 09:51:10 -0700469 )
470}
471
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700472fn_exists()
473{
Martin Roth987d42d2018-07-22 11:45:29 -0600474 # shellcheck disable=SC2039
475 type "$1" >/dev/null 2>&1
Patrick Georgi74c06422015-03-25 18:40:13 +0100476}
477
Patrick Georgif2c15f52015-06-11 21:07:31 +0200478is_package_enabled()
479{
480 echo "$PACKAGES" |grep -q "\<$1\>"
481}
482
Martin Roth444ece22016-04-01 18:46:29 -0600483package_uses_targetarch()
484{
Stefan Reinauera344b682016-04-21 19:29:51 -0700485 if [ "$1" = "GCC" ] || [ "$1" = "GDB" ] || [ "$1" = "BINUTILS" ]; then
Martin Roth444ece22016-04-01 18:46:29 -0600486 true
487 else
488 false
489 fi
490}
491
Nico Huber11ea2b32016-01-26 16:09:31 +0100492generic_build()
493{
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700494 package=$1
Nico Huber11ea2b32016-01-26 16:09:31 +0100495 host_target=$2
496 builddir=$3
497 success=$4
Martin Rothc295d5e2016-11-14 11:56:11 -0700498 version=$5
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700499
Martin Roth987d42d2018-07-22 11:45:29 -0600500 fn_exists "build_$package" || return
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700501
Nico Huber11ea2b32016-01-26 16:09:31 +0100502 mkdir -p "$builddir"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700503
Nico Huber11ea2b32016-01-26 16:09:31 +0100504 if [ -f "$success" ]; then
Martin Roth2c6a8062016-11-14 11:58:39 -0700505 printf "Skipping $package v$version for $host_target as it is already built\n"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700506 else
Martin Roth2c6a8062016-11-14 11:58:39 -0700507 printf "Building $package v$version for $host_target ... "
Nico Huber11ea2b32016-01-26 16:09:31 +0100508 DIR="$PWD"
Martin Roth987d42d2018-07-22 11:45:29 -0600509 cd "$builddir" || exit 1
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700510 rm -f .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600511 "build_${package}" "$host_target" > build.log 2>&1
512 cd "$DIR" || exit 1
Nico Huber11ea2b32016-01-26 16:09:31 +0100513 if [ ! -f "$builddir/.failed" ]; then
514 touch "$success";
515 else
Idwer Volleringe2ef3cf2017-08-15 00:53:13 +0200516 printf "${RED}failed${NC}. Check '$builddir/build.log'.\n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700517 exit 1
518 fi
519 printf "${green}ok${NC}\n"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700520 fi
521}
522
Nico Huber11ea2b32016-01-26 16:09:31 +0100523build_for_host()
524{
Martin Rothc295d5e2016-11-14 11:56:11 -0700525 package="$1"
Martin Roth987d42d2018-07-22 11:45:29 -0600526 # shellcheck disable=SC2086
Martin Rothc295d5e2016-11-14 11:56:11 -0700527 version="$(eval echo \$$package"_VERSION")"
Patrick Georgi1b593e52016-11-25 15:27:41 +0100528 generic_build "$package" host "build-$package" "${DESTDIR}${TARGETDIR}/.${package}.${version}.success" "$version"
Nico Huber11ea2b32016-01-26 16:09:31 +0100529}
530
531build_for_target()
532{
Martin Rothc295d5e2016-11-14 11:56:11 -0700533 package="$1"
Martin Roth987d42d2018-07-22 11:45:29 -0600534 # shellcheck disable=SC2086
Martin Rothc295d5e2016-11-14 11:56:11 -0700535 version="$(eval echo \$$package"_VERSION")"
Patrick Georgi1b593e52016-11-25 15:27:41 +0100536 generic_build "$package" target "build-${TARGETARCH}-$package" "${DESTDIR}${TARGETDIR}/.${TARGETARCH}-${package}.${version}.success" "$version"
Nico Huber11ea2b32016-01-26 16:09:31 +0100537}
538
539build()
540{
Martin Roth987d42d2018-07-22 11:45:29 -0600541 if package_uses_targetarch "$1"; then
542 if [ $BOOTSTRAP -eq 1 ] && [ ! -f "${DESTDIR}${TARGETDIR}/.GCC.${GCC_VERSION}.success" ]; then
Nico Huber234d2462016-01-26 16:10:17 +0100543 build_for_host GCC
544 fi
Martin Roth987d42d2018-07-22 11:45:29 -0600545 build_for_target "$1"
Nico Huber11ea2b32016-01-26 16:09:31 +0100546 else
Martin Roth987d42d2018-07-22 11:45:29 -0600547 build_for_host "$1"
Nico Huber11ea2b32016-01-26 16:09:31 +0100548 fi
549}
550
Nico Huber18c74d62017-06-24 20:35:59 +0200551exit_handler()
Zheng Baobb003c82016-08-05 13:41:51 +0800552{
553 printf "${NC}Stop\n"
554 exit 1
555}
556
Stefan Reinauer074d9132009-09-26 16:43:17 +0000557cleanup()
558{
Stefan Reinauer88e83e52016-04-06 15:39:48 -0700559 if [ $SAVETEMPS -ne 0 ]; then
560 printf "Leaving temporary files around... ${green}ok${NC}\n"
561 return
562 fi
563
Stefan Reinauer074d9132009-09-26 16:43:17 +0000564 printf "Cleaning up temporary files... "
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700565 for package in $PACKAGES; do
Martin Roth987d42d2018-07-22 11:45:29 -0600566 # shellcheck disable=SC2086
567 rm -rf "build-${TARGETARCH}-$package" "build-$package" "$(eval echo \$$package"_DIR")"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700568 done
zbaoff3f15c2015-08-26 22:00:31 -0400569 rm -f getopt
Stefan Reinauer074d9132009-09-26 16:43:17 +0000570 printf "${green}ok${NC}\n"
571}
572
573myhelp()
574{
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700575 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>] [-C] [-G] [-S]\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000576 printf " $0 [-V|--version]\n"
577 printf " $0 [-h|--help]\n\n"
578
579 printf "Options:\n"
580 printf " [-V|--version] print version number and exit\n"
581 printf " [-h|--help] print this help and exit\n"
582 printf " [-c|--clean] remove temporary files before build\n"
583 printf " [-t|--savetemps] don't remove temporary files after build\n"
Patrick Georgic1a75b12011-11-04 21:37:14 +0100584 printf " [-y|--ccache] Use ccache when building cross compiler\n"
Nico Huber152e78e2016-09-20 15:38:40 +0200585 printf " [-n|--nocolor] don't print color codes in output\n"
586 printf " [-u|--urls] print the urls for all packages\n"
Patrick Georgi73166c72010-04-14 20:42:42 +0000587 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
Martin Roth588c79d2016-03-20 12:03:20 -0600588 printf " [-s]--supported <tool> print supported version of a tool\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000589 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
Martin Roth588c79d2016-03-20 12:03:20 -0600590 printf " (defaults to $TARGETDIR)\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000591 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100592 printf " (for RPM builds, default unset)\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700593 printf " [-P|--package <package>] Build a specific package: GCC, CLANG, IASL, GDB\n"
594 printf " (defaults to $PACKAGE)\n"
595 printf "GCC specific options:\n"
Nico Huber234d2462016-01-26 16:10:17 +0100596 printf " [-b|--bootstrap] bootstrap the host compiler before building\n"
597 printf " the cross compiler\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700598 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
599 printf " (defaults to $TARGETARCH)\n"
Nico Huberbb313bf2015-09-08 12:30:27 +0200600 printf " [-l|--languages <languages>] comma separated list of target languages\n"
Nico Huber66c2c1a2016-09-20 13:09:29 +0200601 printf " (defaults to $DEFAULT_LANGUAGES)\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700602 printf "GDB specific options:\n"
603 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
604 printf " (defaults to $TARGETARCH)\n"
605 printf " [-S|--scripting] build scripting support for GDB\n\n"
Martin Rothd1da74f2015-06-21 12:06:28 -0600606 printf "Platforms for GCC & GDB:\n"
Julius Wernerf96d9052019-08-16 15:35:39 -0700607 printf " x86_64 i386-elf i386-mingw32 riscv-elf arm aarch64\n"
Jonathan Neuschäferf14f6402016-03-11 20:22:23 +0100608 printf " powerpc64le-linux-gnu nds32le-elf\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000609}
610
Martin Rothedf965a2015-11-25 16:58:33 -0700611printversion() {
612 printf "${blue}Welcome to the ${red}coreboot${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
613}
614
Stefan Reinauer074d9132009-09-26 16:43:17 +0000615myversion()
616{
Martin Rothedf965a2015-11-25 16:58:33 -0700617 printversion
Stefan Reinauer074d9132009-09-26 16:43:17 +0000618
619 cat << EOF
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000620Copyright (C) 2008-2010 by coresystems GmbH
Marc Jones2aac3f62011-08-08 16:07:50 -0600621Copyright (C) 2011 by Sage Electronic Engineering
Stefan Reinauer074d9132009-09-26 16:43:17 +0000622
623This program is free software; you can redistribute it and/or modify
624it under the terms of the GNU General Public License as published by
625the Free Software Foundation; version 2 of the License.
626
627This program is distributed in the hope that it will be useful,
628but WITHOUT ANY WARRANTY; without even the implied warranty of
629MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
630GNU General Public License for more details.
631
632EOF
633}
634
Nico Huber5f72f962016-01-31 23:05:19 +0100635have_hostcflags_from_gmp() {
Martin Roth987d42d2018-07-22 11:45:29 -0600636 grep -q __GMP_CFLAGS "$DESTDIR$TARGETDIR/include/gmp.h" >/dev/null 2>&1
Nico Huber5f72f962016-01-31 23:05:19 +0100637}
638
639set_hostcflags_from_gmp() {
640 # Now set CFLAGS to match GMP CFLAGS but strip out -pedantic
641 # as GCC 4.6.x fails if it's there.
Martin Roth987d42d2018-07-22 11:45:29 -0600642 HOSTCFLAGS="$(grep __GMP_CFLAGS "$DESTDIR$TARGETDIR/include/gmp.h" |cut -d\" -f2 |\
Idwer Volleringba349ab2016-07-24 02:10:19 +0200643 sed s,-pedantic,,)"
Martin Roth987d42d2018-07-22 11:45:29 -0600644 export HOSTCFLAGS
Nico Huber5f72f962016-01-31 23:05:19 +0100645}
646
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700647build_GMP() {
Nico Huber8bbd5962016-12-22 16:05:54 +0100648 # Check if GCC enables `-pie` by default (possible since GCC 6).
649 # We need PIC in all static libraries then.
Nico Huber2c1c02e2017-01-02 20:51:28 +0100650 if $CC -dumpspecs 2>/dev/null | grep -q '[{;][[:space:]]*:-pie\>'
Nico Huber8bbd5962016-12-22 16:05:54 +0100651 then
652 OPTIONS="$OPTIONS --with-pic"
653 fi
654
Martin Roth987d42d2018-07-22 11:45:29 -0600655 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200656 CC="$(hostcc host)" CXX="$(hostcxx host)" \
657 ../${GMP_DIR}/configure --disable-shared --enable-fat \
Martin Roth987d42d2018-07-22 11:45:29 -0600658 --prefix="$TARGETDIR" $OPTIONS \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700659 || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600660 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700661 $MAKE $JOBS || touch .failed
662 $MAKE install DESTDIR=$DESTDIR || touch .failed
663
664 normalize_dirs
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700665
Nico Huber5f72f962016-01-31 23:05:19 +0100666 set_hostcflags_from_gmp
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700667}
668
669build_MPFR() {
Martin Roth987d42d2018-07-22 11:45:29 -0600670 test "$UNAME" = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
Nico Huber08bb8372017-06-24 19:50:35 +0200671 CC="$(hostcc host)" CXX="$(hostcxx host)" \
Martin Roth987d42d2018-07-22 11:45:29 -0600672 ../${MPFR_DIR}/configure --disable-shared --prefix="$TARGETDIR" \
673 --infodir="$TARGETDIR/info" \
674 --with-gmp="$DESTDIR$TARGETDIR" CFLAGS="$HOSTCFLAGS" || \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700675 touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600676 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700677 $MAKE $JOBS || touch .failed
678 $MAKE install DESTDIR=$DESTDIR || touch .failed
679
680 normalize_dirs
681
682 # work around build problem of libgmp.la
683 if [ "$DESTDIR" != "" ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600684 perl -pi -e "s,$DESTDIR,," "$DESTDIR$TARGETDIR/lib/libgmp.la"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700685 fi
686}
687
688build_MPC() {
Nico Huber08bb8372017-06-24 19:50:35 +0200689 CC="$(hostcc host)" CXX="$(hostcxx host)" \
Martin Roth987d42d2018-07-22 11:45:29 -0600690 ../${MPC_DIR}/configure --disable-shared --prefix="$TARGETDIR" \
691 --infodir="$TARGETDIR/info" --with-mpfr="$DESTDIR$TARGETDIR" \
692 --with-gmp="$DESTDIR$TARGETDIR" CFLAGS="$HOSTCFLAGS" || \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700693 touch .failed
Patrick Georgi790aab62017-04-26 17:35:35 +0200694
695 # work around build problem of libmpfr.la
696 if [ "$DESTDIR" != "" ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600697 perl -pi -e "s,$TARGETDIR/lib/libgmp.la,$DESTDIR\$&," "$DESTDIR$TARGETDIR/lib/libmpfr.la"
Patrick Georgi790aab62017-04-26 17:35:35 +0200698 fi
699
Martin Roth987d42d2018-07-22 11:45:29 -0600700 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700701 $MAKE $JOBS || touch .failed
702 $MAKE install DESTDIR=$DESTDIR || touch .failed
703
Patrick Georgi790aab62017-04-26 17:35:35 +0200704 # work around build problem of libmpfr.la
705 if [ "$DESTDIR" != "" ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600706 perl -pi -e "s,$DESTDIR,," "$DESTDIR$TARGETDIR/lib/libmpfr.la"
Patrick Georgi790aab62017-04-26 17:35:35 +0200707 fi
708
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700709 normalize_dirs
710}
711
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700712build_BINUTILS() {
Nico Huberae6187f2016-12-22 22:16:39 +0100713 if [ $TARGETARCH = "x86_64-elf" ]; then
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700714 ADDITIONALTARGET=",i386-elf"
715 fi
Nico Huber08bb8372017-06-24 19:50:35 +0200716 CC="$(hostcc target)" CXX="$(hostcxx target)" \
Martin Roth987d42d2018-07-22 11:45:29 -0600717 ../binutils-${BINUTILS_VERSION}/configure --prefix="$TARGETDIR" \
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700718 --target=${TARGETARCH} --enable-targets=${TARGETARCH}${ADDITIONALTARGET} \
719 --disable-werror --disable-nls --enable-lto --enable-gold \
Stefan Reinauer2fe2d3d2016-04-05 02:14:48 -0700720 --enable-interwork --enable-multilib \
Vivia Nikolaidou81084362018-04-28 01:21:44 +0300721 --enable-plugins --enable-multilibs \
Nico Huber010ecf802016-12-22 16:19:37 +0100722 CFLAGS="$HOSTCFLAGS" \
723 CXXFLAGS="$HOSTCFLAGS" \
724 || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600725 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700726 $MAKE $JOBS || touch .failed
727 $MAKE install DESTDIR=$DESTDIR || touch .failed
728}
729
Nico Huber234d2462016-01-26 16:10:17 +0100730bootstrap_GCC() {
Martin Roth987d42d2018-07-22 11:45:29 -0600731 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200732 CC="$(hostcc host)" CXX="$(hostcxx host)" \
Nico Huber234d2462016-01-26 16:10:17 +0100733 CFLAGS="$HOSTCFLAGS" \
734 CFLAGS_FOR_BUILD="$HOSTCFLAGS" \
735 CFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
736 CXXFLAGS="$HOSTCFLAGS" \
737 CXXFLAGS_FOR_BUILD="$HOSTCFLAGS" \
738 CXXFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
739 ../gcc-${GCC_VERSION}/configure \
Martin Roth987d42d2018-07-22 11:45:29 -0600740 --prefix="$TARGETDIR" --libexecdir="$TARGETDIR/lib" \
Nico Huber234d2462016-01-26 16:10:17 +0100741 --enable-bootstrap \
742 --disable-werror --disable-nls \
743 --disable-shared --disable-multilib \
744 --disable-libssp --disable-libquadmath --disable-libcc1 \
Patrick Georgi97a9df42017-04-26 22:18:18 +0200745 --disable-libsanitizer \
Nico Huber234d2462016-01-26 16:10:17 +0100746 ${GCC_OPTIONS} --enable-languages="${LANGUAGES}" \
Martin Roth987d42d2018-07-22 11:45:29 -0600747 --with-gmp="$DESTDIR$TARGETDIR" --with-mpfr="$DESTDIR$TARGETDIR" \
748 --with-mpc="$DESTDIR$TARGETDIR" \
Nico Huber234d2462016-01-26 16:10:17 +0100749 --with-pkgversion="coreboot bootstrap v$CROSSGCC_VERSION $CROSSGCC_DATE" \
750 && \
Martin Roth987d42d2018-07-22 11:45:29 -0600751 # shellcheck disable=SC2086
Nico Huber234d2462016-01-26 16:10:17 +0100752 $MAKE $JOBS BOOT_CFLAGS="$HOSTCFLAGS" BUILD_CONFIG="" bootstrap && \
753 $MAKE install-gcc \
754 install-target-libgcc \
755 maybe-install-target-libada \
756 maybe-install-target-libstdc++-v3 \
Nico Huber08bb8372017-06-24 19:50:35 +0200757 DESTDIR=$DESTDIR || touch .failed
Nico Huber234d2462016-01-26 16:10:17 +0100758}
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700759
Nico Huber234d2462016-01-26 16:10:17 +0100760build_cross_GCC() {
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700761 # Work around crazy code generator in GCC that confuses CLANG.
Aaron Durbinb2229dc2016-04-07 16:58:10 -0500762 $CC --version | grep clang >/dev/null 2>&1 && \
Patrick Georgia7a5a562017-06-26 07:21:46 +0200763 CLANGFLAGS="-fbracket-depth=1024"
Nico Huber3f12d932017-08-31 23:11:18 +0200764 [ -n "$CXX" ] && $CXX --version | grep clang >/dev/null 2>&1 && \
Patrick Georgia7a5a562017-06-26 07:21:46 +0200765 CLANGCXXFLAGS="-fbracket-depth=1024"
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700766
Stefan Reinauerd0f83722015-06-09 11:52:28 -0700767 # GCC does not honor HOSTCFLAGS at all. CFLAGS are used for
768 # both target and host object files.
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700769 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
770 # but it does not seem to work properly. At least the host library
771 # libiberty is not compiled with CFLAGS_FOR_BUILD.
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700772 # Also set the CXX version of the flags because GCC is now compiled
773 # using C++.
Martin Roth987d42d2018-07-22 11:45:29 -0600774 # shellcheck disable=SC2086
Nico Huber08bb8372017-06-24 19:50:35 +0200775 CC="$(hostcc target)" CXX="$(hostcxx target)" \
Nico Huber3f12d932017-08-31 23:11:18 +0200776 CFLAGS_FOR_TARGET="-O2 -Dinhibit_libc" \
Patrick Georgia7a5a562017-06-26 07:21:46 +0200777 CFLAGS="$HOSTCFLAGS $CLANGFLAGS" \
778 CFLAGS_FOR_BUILD="$HOSTCFLAGS $CLANGFLAGS" \
779 CXXFLAGS="$HOSTCFLAGS $CLANGCXXFLAGS" \
780 CXXFLAGS_FOR_BUILD="$HOSTCFLAGS $CLANGCXXFLAGS" \
781 ../gcc-${GCC_VERSION}/configure \
Martin Roth987d42d2018-07-22 11:45:29 -0600782 --prefix="$TARGETDIR" --libexecdir="$TARGETDIR/lib" \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700783 --target=${TARGETARCH} --disable-werror --disable-shared \
Stefan Reinauer2f97ebc2015-06-09 12:24:54 -0700784 --enable-lto --enable-plugins --enable-gold --enable-ld=default \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700785 --disable-libssp --disable-bootstrap --disable-nls \
786 --disable-libquadmath --without-headers \
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100787 --disable-threads \
Patrick Georgi1fc02d12016-03-23 21:44:43 +0100788 --enable-interwork --enable-multilib --enable-targets=all \
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100789 --disable-libatomic --disable-libcc1 --disable-decimal-float \
Nico Huberbb313bf2015-09-08 12:30:27 +0200790 ${GCC_OPTIONS} --enable-languages="${LANGUAGES}" \
Ronald G. Minnich7ee16b72016-02-12 21:54:59 +0000791 --with-system-zlib \
Martin Roth987d42d2018-07-22 11:45:29 -0600792 --with-gmp="$DESTDIR$TARGETDIR" --with-mpfr="$DESTDIR$TARGETDIR" \
793 --with-mpc="$DESTDIR$TARGETDIR" \
Patrick Georgidce4d462018-09-07 01:00:54 +0200794 --with-gnu-as --with-gnu-ld \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700795 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE" \
Nico Huber659f40b2018-05-23 17:06:53 +0200796 && \
Patrick Georgi095db332018-06-26 21:00:58 +0200797 mkdir -p gcc/$TARGETARCH && \
Martin Roth987d42d2018-07-22 11:45:29 -0600798 ln -s "$DESTDIR$TARGETDIR/$TARGETARCH/bin" "gcc/$TARGETARCH/$GCC_VERSION" && \
Nico Huber659f40b2018-05-23 17:06:53 +0200799 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-gcc && \
Martin Roth987d42d2018-07-22 11:45:29 -0600800 $MAKE install-gcc DESTDIR="$DESTDIR" || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700801
Martin Roth987d42d2018-07-22 11:45:29 -0600802 if [ ! -f .failed ] && [ "$(echo $TARGETARCH | grep -c -- -mingw32)" -eq 0 ]; then
803 # shellcheck disable=SC2086
Nico Huber659f40b2018-05-23 17:06:53 +0200804 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-target-libgcc && \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700805 $MAKE install-target-libgcc DESTDIR=$DESTDIR || touch .failed
806 fi
807}
808
Nico Huber234d2462016-01-26 16:10:17 +0100809build_GCC() {
810 if [ "$1" = host ]; then
Martin Roth987d42d2018-07-22 11:45:29 -0600811 bootstrap_GCC "$1"
Nico Huber234d2462016-01-26 16:10:17 +0100812 else
Martin Roth987d42d2018-07-22 11:45:29 -0600813 build_cross_GCC "$1"
Nico Huber234d2462016-01-26 16:10:17 +0100814 fi
815}
816
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700817build_EXPAT() {
Nico Huber08bb8372017-06-24 19:50:35 +0200818 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS"
Martin Roth987d42d2018-07-22 11:45:29 -0600819 ../${EXPAT_DIR}/configure --disable-shared --prefix="$TARGETDIR" \
Nico Huber08bb8372017-06-24 19:50:35 +0200820 || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700821 $MAKE || touch .failed
822 $MAKE install DESTDIR=$DESTDIR || touch .failed
823
824 normalize_dirs
825}
826
827build_PYTHON() {
Nico Huber08bb8372017-06-24 19:50:35 +0200828 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS"
Martin Roth987d42d2018-07-22 11:45:29 -0600829 ../${PYTHON_DIR}/configure --prefix="$TARGETDIR" \
Stefan Reinauera344b682016-04-21 19:29:51 -0700830 || touch .failed
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 normalize_dirs
836}
837
838build_GDB() {
839 export PYTHONHOME=$DESTDIR$TARGETDIR
Martin Roth987d42d2018-07-22 11:45:29 -0600840 if [ "$UNAME" != "FreeBSD" ] && [ "$UNAME" != "NetBSD" ]; then
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700841 LIBDL="-ldl"
842 fi
843 LDFLAGS="-Wl,-rpath,\$\$ORIGIN/../lib/ -L$DESTDIR$TARGETDIR/lib \
844 -lpthread $LIBDL -lutil" \
Nico Huber08bb8372017-06-24 19:50:35 +0200845 CC="$(hostcc target)" CXX="$(hostcxx target)" \
846 CFLAGS="$HOSTCFLAGS -I$DESTDIR$TARGETDIR/include" \
Martin Roth987d42d2018-07-22 11:45:29 -0600847 ../${GDB_DIR}/configure --prefix="$TARGETDIR" \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700848 --target=${TARGETARCH} --disable-werror --disable-nls
Martin Roth987d42d2018-07-22 11:45:29 -0600849 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700850 $MAKE $JOBS || touch .failed
851 $MAKE install DESTDIR=$DESTDIR || touch .failed
852}
853
854build_IASL() {
855 RDIR=$PWD
Martin Roth987d42d2018-07-22 11:45:29 -0600856 cd ../$IASL_DIR/generate/unix || exit 1
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700857 CFLAGS="$HOSTCFLAGS"
858 HOST="_LINUX"
Martin Roth987d42d2018-07-22 11:45:29 -0600859 test "$UNAME" = "Darwin" && HOST="_APPLE"
860 test "$UNAME" = "FreeBSD" && HOST="_FreeBSD"
861 test "$UNAME" = "Cygwin" && HOST="_CYGWIN"
Nico Huber08bb8372017-06-24 19:50:35 +0200862 HOST="$HOST" CFLAGS="$CFLAGS" \
863 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 -0700864 $MAKE $JOBS CC="$(hostcc host)" iasl acpibin acpidump acpiexec acpihelp acpinames acpisrc acpixtract
865 rm -f "$DESTDIR$TARGETDIR/bin/{iasl,acpibin,acpidump,acpiexec,acpihelp,acpinames,acpisrc,acpixtract}" || touch "$RDIR/.failed"
866 cp bin/{iasl,acpibin,acpidump,acpiexec,acpihelp,acpinames,acpisrc,acpixtract} "$DESTDIR$TARGETDIR/bin" || touch "$RDIR/.failed"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700867}
868
869build_LLVM() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700870
Martin Roth987d42d2018-07-22 11:45:29 -0600871 cd .. || exit 1
872 ln -sf "$PWD/$CFE_DIR" "$LLVM_DIR/tools/clang"
873 ln -sf "$PWD/$CTE_DIR" "$LLVM_DIR/tools/clang/tools/extra"
874 ln -sf "$PWD/$CRT_DIR" "$LLVM_DIR/projects/compiler-rt"
875 cd - || exit 1
876
877 $CMAKE -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX="$DESTDIR$TARGETDIR" \
Martin Rothfb5647a2015-12-18 09:26:45 -0700878 -DCLANG_VENDOR="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE - " \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700879 -DCMAKE_BUILD_TYPE=Release ../$LLVM_DIR || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600880 # shellcheck disable=SC2086
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700881 $MAKE $JOBS || touch .failed
882 $MAKE install || touch .failed
883
Martin Roth987d42d2018-07-22 11:45:29 -0600884 cp -a ../$CFE_DIR/tools/scan-build/* "$DESTDIR$TARGETDIR/bin"
885 cp -a ../$CFE_DIR/tools/scan-view/* "$DESTDIR$TARGETDIR/bin"
Stefan Reinauere9e6e3d2015-07-07 00:20:42 +0200886
887 # create symlinks to work around broken --print-librt-file-name
888 # when used with -target.
Martin Roth987d42d2018-07-22 11:45:29 -0600889 cd "$DESTDIR$TARGETDIR/lib/clang/${CLANG_VERSION}/lib" || exit 1
Stefan Reinauere9e6e3d2015-07-07 00:20:42 +0200890 for i in */libclang_rt.builtins*.a; do
Martin Roth987d42d2018-07-22 11:45:29 -0600891 ln -s "$i" .
Stefan Reinauere9e6e3d2015-07-07 00:20:42 +0200892 done
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700893}
894
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800895build_MAKE() {
Stefan Reinauerf5fa96f2019-09-21 17:39:30 -0700896 ( cd ../${MAKE_DIR}/ && aclocal && autoconf )
Nico Huber08bb8372017-06-24 19:50:35 +0200897 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS" \
Martin Roth987d42d2018-07-22 11:45:29 -0600898 ../${MAKE_DIR}/configure --prefix="$TARGETDIR" --disable-nls \
Nico Huber08bb8372017-06-24 19:50:35 +0200899 || touch .failed
Stefan Reinauerf5fa96f2019-09-21 17:39:30 -0700900 $MAKE -k clean
Martin Roth987d42d2018-07-22 11:45:29 -0600901 # shellcheck disable=SC2086
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800902 $MAKE $JOBS || touch .failed
903 $MAKE install DESTDIR=$DESTDIR || touch .failed
904
905 normalize_dirs
906}
907
Stefan Reinauer3b593492017-06-19 17:28:54 -0700908build_CMAKE() {
Nico Huber08bb8372017-06-24 19:50:35 +0200909 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS" \
Martin Roth987d42d2018-07-22 11:45:29 -0600910 ../${CMAKE_DIR}/configure --prefix="$TARGETDIR" \
Stefan Reinauer3b593492017-06-19 17:28:54 -0700911 || touch .failed
Martin Roth987d42d2018-07-22 11:45:29 -0600912 # shellcheck disable=SC2086
Stefan Reinauer3b593492017-06-19 17:28:54 -0700913 $MAKE $JOBS || touch .failed
914 $MAKE install DESTDIR=$DESTDIR || touch .failed
915
916 normalize_dirs
917}
918
Martin Rothd70f5fa2019-05-26 17:24:19 -0600919build_NASM() {
920 CC="$(hostcc host)" CXX="$(hostcxx host)" CFLAGS="$HOSTCFLAGS"
921 ../${NASM_DIR}/configure --prefix="$TARGETDIR" \
922 || touch .failed
923 # shellcheck disable=SC2086
924 $MAKE $JOBS || touch .failed
925 $MAKE install DESTDIR=$DESTDIR || touch .failed
926
927 normalize_dirs
928}
929
Stefan Reinauerc66fd3c2017-06-21 14:57:28 -0700930print_supported() {
931 case "$PRINTSUPPORTED" in
Martin Rothedf965a2015-11-25 16:58:33 -0700932 AUTOCONF|autoconf) printf "%s\n" "$GCC_AUTOCONF_VERSION";;
933 BINUTILS|binutils) printf "%s\n" "$BINUTILS_VERSION";;
934 CLANG|clang) printf "%s\n" "$CLANG_VERSION";;
935 EXPAT|expat) printf "%s\n" "$EXPAT_VERSION";;
936 GCC|gcc) printf "%s\n" "$GCC_VERSION";;
937 GDB|gdb) printf "%s\n" "$GDB_VERSION";;
938 GMP|gmp) printf "%s\n" "$GMP_VERSION";;
939 IASL|iasl) printf "%s\n" "$IASL_VERSION";;
Martin Rothedf965a2015-11-25 16:58:33 -0700940 MPC|mpc) printf "%s\n" "$MPC_VERSION";;
941 MPFR|mpfr) printf "%s\n" "$MPFR_VERSION";;
942 PYTHON|python) printf "%s\n" "$PYTHON_VERSION";;
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800943 MAKE|make) printf "%s\n" "$MAKE_VERSION";;
Martin Rothd70f5fa2019-05-26 17:24:19 -0600944 NASM|nasm) printf "%s\n" "${NASM_VERSION}";;
Stefan Reinauerc66fd3c2017-06-21 14:57:28 -0700945 *) printf "Unknown tool %s\n" "$PRINTSUPPORTED";;
Martin Rothedf965a2015-11-25 16:58:33 -0700946 esac
947}
Stefan Reinauer074d9132009-09-26 16:43:17 +0000948
Nico Huber18c74d62017-06-24 20:35:59 +0200949trap exit_handler 1 2 3 15
Zheng Baobb003c82016-08-05 13:41:51 +0800950
Patrick Georgi6bba29f2010-01-29 17:40:52 +0000951# Look if we have getopt. If not, build it.
952export PATH=$PATH:.
953getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
954
Stefan Reinauer074d9132009-09-26 16:43:17 +0000955# parse parameters.. try to find out whether we're running GNU getopt
Nico Huber27e8412b2017-12-05 22:02:21 +0100956getoptbrand="$(getopt -V 2>/dev/null | sed -e '1!d' -e 's,^\(......\).*,\1,')"
Peter Stuge09377b72011-08-21 06:24:55 +0200957if [ "${getoptbrand}" = "getopt" ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000958 # Detected GNU getopt that supports long options.
Patrick Georgi9681cdc2017-09-22 18:57:10 +0200959 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 +0200960 getopt_ret=$?
Nico Hubercc414dd2016-09-20 13:59:38 +0200961 eval set -- "$args"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000962else
963 # Detected non-GNU getopt
Patrick Georgi9681cdc2017-09-22 18:57:10 +0200964 args=$(getopt Vhcd:bBp:l:P:j:D:tSys:un $*)
Nico Huber78df0bf2016-09-20 14:11:53 +0200965 getopt_ret=$?
Martin Roth987d42d2018-07-22 11:45:29 -0600966 # shellcheck disable=SC2086
Stefan Reinauer074d9132009-09-26 16:43:17 +0000967 set -- $args
968fi
969
Nico Huber78df0bf2016-09-20 14:11:53 +0200970if [ $getopt_ret != 0 ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000971 myhelp
972 exit 1
973fi
974
975while true ; do
976 case "$1" in
Patrick Georgi9681cdc2017-09-22 18:57:10 +0200977 -V|--version) shift; myversion; exit 0;;
978 -h|--help) shift; myhelp; exit 0;;
979 -c|--clean) shift; clean=1;;
980 -t|--savetemps) shift; SAVETEMPS=1;;
981 -d|--directory) shift; TARGETDIR="$1"; shift;;
982 -b|--bootstrap) shift; BOOTSTRAP=1;;
983 -B|--bootstrap-only) shift; BOOTSTRAPONLY=1; BOOTSTRAP=1;;
984 -p|--platform) shift; TARGETARCH="$1"; shift;;
985 -l|--languages) shift; LANGUAGES="$1"; shift;;
986 -D|--destdir) shift; DESTDIR="$1"; shift;;
987 -j|--jobs) shift; THREADS="$1"; JOBS="-j $1"; shift;;
988 -P|--package) shift; PACKAGE="$1"; shift;;
989 -S|--scripting) shift; SKIPPYTHON=0;;
990 -y|--ccache) shift; USECCACHE=1;;
991 -s|--supported) shift; PRINTSUPPORTED="$1"; shift;;
992 -u|--urls) shift; printf "%s\n" "$ALL_ARCHIVES"; exit 0;;
993 -n|--nocolor) shift; \
Martin Roth4a53db02016-03-01 15:21:07 -0700994 unset red RED green GREEN blue BLUE cyan CYAN NC;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200995 --) shift; break;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200996 *) break;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000997 esac
998done
999
Nico Huberbd74d562016-09-20 14:16:43 +02001000if [ $# -gt 0 ]; then
1001 printf "Excessive arguments: $*\n"
1002 myhelp
1003 exit 1
1004fi
1005
Stefan Reinauerc66fd3c2017-06-21 14:57:28 -07001006if [ -n "$PRINTSUPPORTED" ]; then
1007 print_supported
Martin Rothedf965a2015-11-25 16:58:33 -07001008 exit 0
1009fi
1010
1011#print toolchain builder version string as the header
1012printversion
1013
Martin Roth2c6a8062016-11-14 11:58:39 -07001014printf "Building toolchain using %d thread(s).\n\n" "$THREADS"
1015
Ronald G. Minnichb460a662013-05-26 05:33:35 -07001016case "$TARGETARCH" in
Timothy Pearsona9f62352015-12-30 18:59:00 -06001017 x86_64-elf) ;;
1018 x86_64*) TARGETARCH=x86_64-elf;;
1019 i386-elf) ;;
1020 i386-mingw32) ;;
Iru Caiae8e3a02016-04-02 10:50:59 +08001021 riscv-elf) TARGETARCH=riscv64-elf;;
Patrick Georgi0a97d7e2016-01-25 09:51:22 +01001022 powerpc64*-linux*) ;;
Timothy Pearsona9f62352015-12-30 18:59:00 -06001023 i386*) TARGETARCH=i386-elf;;
Stefan Reinauer27522ad2016-01-29 17:31:34 -08001024 arm*) TARGETARCH=arm-eabi;;
Timothy Pearsona9f62352015-12-30 18:59:00 -06001025 aarch64*) TARGETARCH=aarch64-elf;;
Martin Roth3e0f74d2016-03-08 12:07:04 -07001026 nds32le-elf) ;;
Timothy Pearsona9f62352015-12-30 18:59:00 -06001027 *) printf "${red}WARNING: Unsupported architecture $TARGETARCH.${NC}\n\n"; ;;
Ronald G. Minnichb460a662013-05-26 05:33:35 -07001028esac
1029
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001030# Figure out which packages to build
Stefan Reinauer699ac272015-06-05 12:43:28 -07001031
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001032case "$PACKAGE" in
1033 GCC|gcc)
Stefan Reinauer81693d42017-06-21 15:25:36 -07001034 echo "Target architecture is $TARGETARCH"
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001035 NAME="${TARGETARCH} cross GCC"
Nico Huberafda56e2017-12-07 17:50:53 +01001036 PACKAGES="GMP MPFR MPC BINUTILS GCC"
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001037 ;;
1038 GDB|gdb)
1039 NAME="${TARGETARCH} cross GDB"
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001040 if [ $SKIPPYTHON -eq 0 ]; then
Ronald G. Minnichfb944f42016-02-27 04:06:42 +00001041 PACKAGES="EXPAT PYTHON GDB"
1042 else
1043 PACKAGES="EXPAT GDB"
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001044 fi
1045 ;;
1046 CLANG|clang)
Stefan Reinauer81693d42017-06-21 15:25:36 -07001047 NAME="LLVM clang"
1048 LLVM_VERSION=${CLANG_VERSION}
Stefan Reinauerca117e72017-06-19 17:29:08 -07001049 PACKAGES="CMAKE LLVM CFE CRT CTE"
1050 CMAKE=${DESTDIR}${TARGETDIR}/bin/cmake
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001051 ;;
1052 IASL|iasl)
1053 NAME="IASL ACPI compiler"
1054 PACKAGES=IASL
1055 ;;
Stefan Reinauera851bbb2016-01-29 18:03:26 -08001056 MAKE|make)
1057 NAME="GNU Make"
1058 PACKAGES=MAKE
1059 ;;
Stefan Reinauer3b593492017-06-19 17:28:54 -07001060 CMAKE|cmake)
1061 NAME="CMake"
1062 PACKAGES=CMAKE
1063 ;;
Martin Rothd70f5fa2019-05-26 17:24:19 -06001064 NASM|nasm)
1065 NAME="NASM"
1066 PACKAGES=NASM
1067 ;;
Stefan Reinauer85b07d62015-06-09 14:45:14 -07001068 *)
Martin Rothd70f5fa2019-05-26 17:24:19 -06001069 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 -07001070 exit 1
1071 ;;
1072esac
Peter Stugeceacd772011-06-09 04:54:16 +02001073
Stefan Reinauer66e93352015-06-16 22:38:23 +02001074# Find all the required tools:
1075
1076TAR=$(searchtool tar) || exit $?
1077PATCH=$(searchtool patch) || exit $?
1078MAKE=$(searchtool make) || exit $?
1079SHA1SUM=$(searchtool sha1sum)
zbao5cf758d2015-09-01 22:28:57 -04001080#SHA512SUM=$(searchtool sha512sum)
1081#MD5SUM=$(searchtool md5sum)
Stefan Reinauer66e93352015-06-16 22:38:23 +02001082CHECKSUM=$SHA1SUM
Martin Roth95f7b222016-03-20 12:38:48 -06001083LBZIP2=$(searchtool lbzip2 "" nofail)
1084PIGZ=$(searchtool pigz "" nofail)
Stefan Reinauer66e93352015-06-16 22:38:23 +02001085
1086searchtool m4 > /dev/null
1087searchtool bison > /dev/null
1088searchtool flex flex > /dev/null
Stefan Reinauer66e93352015-06-16 22:38:23 +02001089searchtool bzip2 "bzip2," > /dev/null
Nico Huber0ff3b392017-03-12 16:07:33 +01001090searchtool xz "XZ Utils" "" "xz-utils" > /dev/null
Stefan Reinauer66e93352015-06-16 22:38:23 +02001091
Patrick Georgidf1ff232017-01-07 09:28:43 +01001092if searchtool wget "GNU" nofail > /dev/null; then
1093 download_showing_percentage() {
1094 url=$1
Stefan Reinauer81693d42017-06-21 15:25:36 -07001095 printf "... ${red} 0%%"
Martin Roth987d42d2018-07-22 11:45:29 -06001096 wget "$url" 2>&1 | while read -r line; do
1097 echo "$line" | grep -o "[0-9]\+%" | awk '{printf("\b\b\b\b%4s", $1)}'
Patrick Georgidf1ff232017-01-07 09:28:43 +01001098 done
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +02001099 printf "${NC}... "
Patrick Georgidf1ff232017-01-07 09:28:43 +01001100 }
1101elif searchtool curl "^curl " > /dev/null; then
1102 download_showing_percentage() {
1103 url=$1
1104 echo
Martin Roth987d42d2018-07-22 11:45:29 -06001105 curl -#OL "$url"
Patrick Georgidf1ff232017-01-07 09:28:43 +01001106 }
1107fi
1108
Nico Huber08bb8372017-06-24 19:50:35 +02001109# Allow $CC override from the environment.
1110if [ -n "$CC" ]; then
1111 if [ ! -x "$(command -v "$CC" 2>/dev/null)" ]; then
1112 printf "${RED}ERROR:${red} CC is set to '%s' but wasn't found.${NC}\n" "$CC"
1113 HALT_FOR_TOOLS=1
1114 fi
1115else
Patrick Georgi8f2cdad2017-07-10 11:48:34 +02001116 if searchtool gnatgcc "Free Software Foundation" nofail > /dev/null; then
1117 CC=gnatgcc
1118 elif searchtool gcc "Free Software Foundation" nofail > /dev/null; then
Nico Huber08bb8372017-06-24 19:50:35 +02001119 CC=gcc
1120 else
1121 searchtool cc '^' nofail > /dev/null || please_install gcc
1122 CC=cc
1123 fi
1124fi
Martin Roth591790f2016-03-20 20:39:04 -06001125
Nico Huber08bb8372017-06-24 19:50:35 +02001126# We can leave $CXX empty if it's not set since *buildgcc* never
1127# calls it directly. This way configure scripts can search for
1128# themselves and we still override it when a bootstrapped g++ is
1129# to be used (cf. hostcxx()).
1130if [ -n "$CXX" ]; then
1131 if [ ! -x "$(command -v "$CXX" 2>/dev/null)" ]; then
1132 printf "${RED}ERROR:${red} CXX is set to '%s' but wasn't found.${NC}\n" "$CXX"
1133 HALT_FOR_TOOLS=1
1134 fi
1135else
1136 searchtool g++ "Free Software Foundation" nofail > /dev/null || \
1137 searchtool clang "clang version" nofail > /dev/null || \
1138 searchtool clang "LLVM" "" "g++" > /dev/null
1139fi
1140
1141check_for_library "-lz" "zlib (zlib1g-dev or zlib-devel)"
Nico Huber156d87c2016-09-20 12:59:53 +02001142
Martin Roth88463822016-01-07 11:03:36 -07001143if [ "$HALT_FOR_TOOLS" -ne 0 ]; then
1144 exit 1
1145fi
1146
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001147# This initial cleanup is useful when updating the toolchain script.
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001148
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001149if [ "$clean" = "1" ]; then
1150 cleanup
1151fi
Stefan Reinauer074d9132009-09-26 16:43:17 +00001152
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001153# Set up host compiler and flags needed for various OSes
Stefan Reinauer074d9132009-09-26 16:43:17 +00001154
Patrick Georgif2c15f52015-06-11 21:07:31 +02001155if is_package_enabled "GCC"; then
Patrick Georgie8253fe2017-07-18 11:35:35 +02001156# sane preset: let the configure script figure out things by itself
1157# more importantly, avoid any values that might already linger in the variable
1158OPTIONS="ABI="
Martin Roth987d42d2018-07-22 11:45:29 -06001159if [ "$UNAME" = "Darwin" ]; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001160 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
1161
1162 # generally the OS X compiler can create x64 binaries.
1163 # Per default it generated i386 binaries in 10.5 and x64
1164 # binaries in 10.6 (even if the kernel is 32bit)
1165 # For some weird reason, 10.5 autodetects an ABI=64 though
1166 # so we're setting the ABI explicitly here.
Martin Roth987d42d2018-07-22 11:45:29 -06001167 if [ "$(sysctl -n hw.optional.x86_64 2>/dev/null)" -eq 1 ] 2>/dev/null; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001168 OPTIONS="ABI=64"
1169 else
1170 OPTIONS="ABI=32"
1171 fi
Stefan Reinauer9491b4d2011-10-11 22:37:59 -07001172
Stefan Reinauer4266b922012-12-05 16:18:32 -08001173 # In Xcode 4.5.2 the default compiler is clang.
1174 # However, this compiler fails to compile gcc 4.7.x. As a
1175 # workaround it's possible to compile gcc with llvm-gcc.
Stefan Reinauer9491b4d2011-10-11 22:37:59 -07001176 if $CC -v 2>&1 | grep -q LLVM; then
Stefan Reinauer4266b922012-12-05 16:18:32 -08001177 CC=llvm-gcc
Stefan Reinauer9491b4d2011-10-11 22:37:59 -07001178 fi
Martin Roth987d42d2018-07-22 11:45:29 -06001179elif [ "$UNAME" = "Linux" ] || [ "$UNAME" = "Cygwin" ]; then
Patrick Georgiddb8f802015-07-04 17:45:54 +02001180 # gmp is overeager with detecting 64bit CPUs even if they run
1181 # a 32bit kernel and userland.
1182 if [ "$(uname -m 2>/dev/null)" = "i686" ]; then
1183 OPTIONS="ABI=32"
1184 fi
Martin Roth987d42d2018-07-22 11:45:29 -06001185elif [ "$UNAME" = "NetBSD" ]; then
Nico Huberde45c592016-01-20 23:22:33 +01001186 # same for NetBSD but this one reports an i386
1187 if [ "$(uname -m 2>/dev/null)" = "i386" ]; then
1188 OPTIONS="ABI=32"
1189 fi
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001190fi
Nico Huber66c2c1a2016-09-20 13:09:29 +02001191if [ -z "${LANGUAGES}" ]; then
Nico Hubercd87e1b2017-06-24 20:35:59 +02001192 if have_gnat; then
Nico Huber66c2c1a2016-09-20 13:09:29 +02001193 printf "\nFound compatible Ada compiler, enabling Ada support by default.\n\n"
1194 LANGUAGES="ada,${DEFAULT_LANGUAGES}"
1195 else
Nico Hubercd87e1b2017-06-24 20:35:59 +02001196 printf "\n${red}WARNING${NC}\n"
1197 printf "No compatible Ada compiler (GNAT) found. You can continue without\n"
1198 printf "Ada support, but this will limit the features of ${blue}coreboot${NC} (e.g.\n"
1199 printf "native graphics initialization won't be available on most Intel\n"
1200 printf "boards).\n\n"
1201
1202 printf "Usually, you can install GNAT with your package management system\n"
1203 printf "(the package is called \`gnat\` or \`gcc-ada\`). It has to match the\n"
1204 printf "\`gcc\` package in version. If there are multiple versions of GCC in-\n"
1205 printf "stalled, you can point this script to the matching version through\n"
1206 printf "the \`CC\` and \`CXX\` environment variables.\n\n"
1207
1208 printf "e.g. on Ubuntu 14.04, if \`gcc\` is \`gcc-4.8\`:\n"
1209 printf " apt-get install gnat-4.8 && make crossgcc\n\n"
1210
1211 printf "on Ubuntu 16.04, if \`gcc\` is \`gcc-5\`:\n"
1212 printf " apt-get install gnat-5 && make crossgcc\n"
1213 timeout 30
Nico Huber66c2c1a2016-09-20 13:09:29 +02001214 LANGUAGES="${DEFAULT_LANGUAGES}"
1215 fi
1216fi
Nico Hubercd87e1b2017-06-24 20:35:59 +02001217if ada_requested; then
1218 if have_gnat; then
Martin Roth987d42d2018-07-22 11:45:29 -06001219 if [ "$BOOTSTRAP" != 1 ] && \
Stefan Taunerb75a08b2018-08-01 06:57:36 +02001220 { [ "$(hostcc_major)" -lt 4 ] || \
1221 { [ "$(hostcc_major)" -eq 4 ] && \
1222 [ "$(hostcc_minor)" -lt 9 ] ; } ; }
Nico Hubercd87e1b2017-06-24 20:35:59 +02001223 then
1224 printf "\n${red}WARNING${NC}\n"
1225 printf "Building the Ada compiler (GNAT $(buildcc_version)) with a host compiler older\n"
1226 printf "than 4.9.x (yours $(hostcc_version)) requires bootstrapping. This will take\n"
1227 printf "significantly longer than a usual build. You can abort and update\n"
1228 printf "your host GNAT or disable Ada support with BUILD_LANGUAGES=c (or\n"
1229 printf "\`-l c\` in case you invoke \`buildgcc\` directly).\n"
1230 timeout 15
1231 BOOTSTRAP=1
1232 fi
1233 else
1234 please_install gnat gcc-ada
1235 exit 1
1236 fi
1237else
Martin Roth987d42d2018-07-22 11:45:29 -06001238 if [ "$(hostcc_major)" -lt 4 ] && [ "$BOOTSTRAP" != 1 ]; then
Nico Hubercd87e1b2017-06-24 20:35:59 +02001239 printf "\n${red}WARNING${NC}\n"
1240 printf "Building GCC $(buildcc_version) with a very old host compiler ($(hostcc_version)).\n"
1241 printf "Bootstrapping (-b) is recommended.\n"
1242 timeout 10
1243 fi
1244fi
Stefan Reinauer14ce2132015-06-05 13:01:13 -07001245fi # GCC
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00001246
Nico Huber5f72f962016-01-31 23:05:19 +01001247export HOSTCFLAGS="-Os"
1248if have_hostcflags_from_gmp; then
1249 set_hostcflags_from_gmp
1250fi
1251
Patrick Georgic1a75b12011-11-04 21:37:14 +01001252if [ "$USECCACHE" = 1 ]; then
1253 CC="ccache $CC"
1254fi
1255
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001256# Prepare target directory for building GCC
1257# (dependencies must be in the PATH)
Martin Roth987d42d2018-07-22 11:45:29 -06001258mkdir -p "$DESTDIR$TARGETDIR/bin"
1259mkdir -p "$DESTDIR$TARGETDIR/share"
Stefan Reinauer16bd7892012-12-07 23:57:01 +01001260export PATH=$DESTDIR$TARGETDIR/bin:$PATH
1261
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001262# Download, unpack, patch and build all packages
1263
Elyes HAOUASb0f19882018-06-09 11:59:00 +02001264printf "Downloading and verifing tarballs ...\n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001265mkdir -p tarballs
1266for P in $PACKAGES; do
Jonathan Neuschäfer92d483a2017-09-26 22:33:53 +02001267 download "$P" || exit "$?"
1268 verify_hash "$P" || exit "$?"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001269done
Martin Roth03625172016-02-17 14:44:14 -07001270printf "Downloaded tarballs ... ${green}ok${NC}\n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001271
Elyes HAOUASb0f19882018-06-09 11:59:00 +02001272printf "Unpacking and patching ...\n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001273for P in $PACKAGES; do
1274 unpack_and_patch $P || exit 1
1275done
1276printf "Unpacked and patched ... ${green}ok${NC}\n"
1277
Patrick Georgi9681cdc2017-09-22 18:57:10 +02001278if [ -n "$BOOTSTRAPONLY" ]; then
Elyes HAOUASb0f19882018-06-09 11:59:00 +02001279 printf "Building bootstrap compiler only ...\n"
Nico Huberafda56e2017-12-07 17:50:53 +01001280 for pkg in GMP MPFR MPC GCC; do
Patrick Georgi9681cdc2017-09-22 18:57:10 +02001281 build_for_host $pkg
1282 done
1283 exit 0
1284fi
1285
Elyes HAOUASb0f19882018-06-09 11:59:00 +02001286printf "Building packages ...\n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -07001287for package in $PACKAGES; do
1288 build $package
1289done
Stefan Reinauer5dd26352015-06-09 14:52:22 -07001290printf "Packages built ... ${green}ok${NC}\n"
Stefan Reinauer14ce2132015-06-05 13:01:13 -07001291
Stefan Reinauer699ac272015-06-05 12:43:28 -07001292# Adding git information of current tree to target directory
1293# for reproducibility
Stefan Reinauerd7649122015-06-09 11:44:25 -07001294PROGNAME=$(basename "$0")
Stefan Reinauer88352d72016-04-06 15:57:03 -07001295rm -f "$DESTDIR$TARGETDIR/share/$PROGNAME-*"
Patrick Georgi4eb84cf2017-07-07 11:18:46 +02001296cp "$PROGNAME" "$DESTDIR$TARGETDIR/share/$PROGNAME-$CROSSGCC_VERSION-$CROSSGCC_COMMIT"
Zheng Bao30b895f2013-02-06 18:04:40 +08001297
Patrick Georgi0c2df6f2017-11-22 16:14:09 -05001298# Adding edk2 tools template
1299mkdir -p "$DESTDIR$TARGETDIR/share/edk2config"
1300sed -e "s,@@PREFIX@@,$TARGETDIR,g" edk2tools.txt > "$DESTDIR$TARGETDIR/share/edk2config/tools_def.txt"
1301printf "Copied EDK2 tools template ... ${green}ok${NC}\n"
1302
Stefan Reinauer88e83e52016-04-06 15:39:48 -07001303cleanup
Stefan Reinauer074d9132009-09-26 16:43:17 +00001304
Stefan Reinauer81693d42017-06-21 15:25:36 -07001305printf "\n${green}You can now run $NAME from $TARGETDIR.${NC}\n"