blob: 273f921a15ddadeb1d92b4d85af5d733739fc017 [file] [log] [blame]
Peter Stuge09377b72011-08-21 06:24:55 +02001#!/bin/sh
Stefan Reinauer074d9132009-09-26 16:43:17 +00002#
Stefan Reinauer5f1ad892010-05-17 11:02:25 +00003# Copyright (C) 2008-2010 by coresystems GmbH
Stefan Reinauer074d9132009-09-26 16:43:17 +00004# written by Patrick Georgi <patrick.georgi@coresystems.de> and
5# Stefan Reinauer <stefan.reinauer@coresystems.de>
Stefan Reinauer14e22772010-04-27 06:56:47 +00006#
Marc Jones2aac3f62011-08-08 16:07:50 -06007# Copyright (C) 2011 by Sage Electronic Engineering
8#
Stefan Reinauer074d9132009-09-26 16:43:17 +00009# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; version 2 of the License.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
Stefan Reinauer074d9132009-09-26 16:43:17 +000018
Stefan Reinauerd7649122015-06-09 11:44:25 -070019cd $(dirname $0)
Patrick Georgi8135dba2015-03-17 21:05:20 +010020
Martin Roth2ab981b2016-08-31 16:43:49 -060021CROSSGCC_DATE="August 31st, 2016"
22CROSSGCC_VERSION="1.43"
Stefan Reinauer88352d72016-04-06 15:57:03 -070023CROSSGCC_COMMIT=$( git describe )
Stefan Reinauer074d9132009-09-26 16:43:17 +000024
25# default settings
Stefan Reinauer85b07d62015-06-09 14:45:14 -070026PACKAGE=GCC
Stefan Reinauerd7649122015-06-09 11:44:25 -070027TARGETDIR=$(pwd)/xgcc
Stefan Reinauer074d9132009-09-26 16:43:17 +000028TARGETARCH=i386-elf
Nico Huber66c2c1a2016-09-20 13:09:29 +020029DEFAULT_LANGUAGES=c
30LANGUAGES=
Stefan Reinauer074d9132009-09-26 16:43:17 +000031DESTDIR=
Stefan Reinauer85b07d62015-06-09 14:45:14 -070032SAVETEMPS=0
33SKIPPYTHON=1
Nico Huber234d2462016-01-26 16:10:17 +010034BOOTSTRAP=0
Martin Roth2c6a8062016-11-14 11:58:39 -070035THREADS=1
Stefan Reinauer074d9132009-09-26 16:43:17 +000036
Stefan Reinauer699ac272015-06-05 12:43:28 -070037# GCC toolchain version numbers
Timothy Pearson96310162015-12-28 22:53:29 -060038GMP_VERSION=6.1.0
Iru Caiae8e3a02016-04-02 10:50:59 +080039MPFR_VERSION=3.1.4
Paul Menzele6619422015-02-23 10:33:59 +010040MPC_VERSION=1.0.3
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000041LIBELF_VERSION=0.8.13
Iru Caiae8e3a02016-04-02 10:50:59 +080042GCC_VERSION=5.3.0
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070043GCC_AUTOCONF_VERSION=2.69
Martin Rothbcfa7cc2016-07-14 13:01:37 -060044BINUTILS_VERSION=2.26.1
Stefan Reinauer498f3d02016-04-21 18:10:15 -070045GDB_VERSION=7.11
Martin Roth2ab981b2016-08-31 16:43:49 -060046IASL_VERSION=20160831
Stefan Reinauer3dc5d072016-04-21 19:29:03 -070047PYTHON_VERSION=3.5.1
Stefan Reinauer498f3d02016-04-21 18:10:15 -070048EXPAT_VERSION=2.1.1
Stefan Reinauer14ce2132015-06-05 13:01:13 -070049# CLANG version number
Iru Caiae8e3a02016-04-02 10:50:59 +080050CLANG_VERSION=3.8.0
Martin Roth589ef9d2016-08-11 18:35:06 -060051MAKE_VERSION=4.2.1
Stefan Reinauer074d9132009-09-26 16:43:17 +000052
Stefan Reinauer699ac272015-06-05 12:43:28 -070053# GCC toolchain archive locations
Martin Roth1484c032016-03-08 09:37:14 -070054# These are sanitized by the jenkins toolchain test builder, so if
55# a completely new URL is added here, it probably needs to be added
56# to the jenkins build as well, or the builder won't download it.
Stefan Reinauerd9d21022016-04-21 18:16:30 -070057GMP_ARCHIVE="http://ftpmirror.gnu.org/gmp/gmp-${GMP_VERSION}.tar.xz"
58MPFR_ARCHIVE="http://ftpmirror.gnu.org/mpfr/mpfr-${MPFR_VERSION}.tar.xz"
Idwer Vollering7962fc72012-10-25 02:14:09 +020059MPC_ARCHIVE="http://ftpmirror.gnu.org/mpc/mpc-${MPC_VERSION}.tar.gz"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000060LIBELF_ARCHIVE="http://www.mr511.de/software/libelf-${LIBELF_VERSION}.tar.gz"
Idwer Vollering1cfee0b2012-10-25 02:03:53 +020061GCC_ARCHIVE="http://ftpmirror.gnu.org/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.bz2"
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070062BINUTILS_ARCHIVE="http://ftpmirror.gnu.org/binutils/binutils-${BINUTILS_VERSION}.tar.bz2"
Patrick Georgi53c388f2015-03-07 09:55:18 +010063GDB_ARCHIVE="http://ftpmirror.gnu.org/gdb/gdb-${GDB_VERSION}.tar.xz"
Patrick Georgi85bb9462016-02-11 17:22:16 +010064IASL_ARCHIVE="https://acpica.org/sites/acpica/files/acpica-unix2-${IASL_VERSION}.tar.gz"
Patrick Georgi53c388f2015-03-07 09:55:18 +010065PYTHON_ARCHIVE="http://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz"
Stefan Reinauer498f3d02016-04-21 18:10:15 -070066EXPAT_ARCHIVE="http://downloads.sourceforge.net/sourceforge/expat/expat-${EXPAT_VERSION}.tar.bz2"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070067# CLANG toolchain archive locations
68LLVM_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/llvm-${CLANG_VERSION}.src.tar.xz"
69CFE_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/cfe-${CLANG_VERSION}.src.tar.xz"
70CRT_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/compiler-rt-${CLANG_VERSION}.src.tar.xz"
71CTE_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/clang-tools-extra-${CLANG_VERSION}.src.tar.xz"
Stefan Reinauera851bbb2016-01-29 18:03:26 -080072MAKE_ARCHIVE="http://ftpmirror.gnu.org/make/make-${MAKE_VERSION}.tar.bz2"
Stefan Reinauer074d9132009-09-26 16:43:17 +000073
Martin Roth58c68d52016-03-01 15:22:42 -070074ALL_ARCHIVES="$GMP_ARCHIVE $MPFR_ARCHIVE $MPC_ARCHIVE $LIBELF_ARCHIVE \
75 $GCC_ARCHIVE $BINUTILS_ARCHIVE $GDB_ARCHIVE $IASL_ARCHIVE \
76 $PYTHON_ARCHIVE $EXPAT_ARCHIVE $LLVM_ARCHIVE $CFE_ARCHIVE \
77 $CRT_ARCHIVE $CTE_ARCHIVE $MAKE_ARCHIVE"
78
Stefan Reinauer699ac272015-06-05 12:43:28 -070079# GCC toolchain directories
Stefan Reinauer074d9132009-09-26 16:43:17 +000080GMP_DIR="gmp-${GMP_VERSION}"
81MPFR_DIR="mpfr-${MPFR_VERSION}"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000082MPC_DIR="mpc-${MPC_VERSION}"
83LIBELF_DIR="libelf-${LIBELF_VERSION}"
Stefan Reinauer074d9132009-09-26 16:43:17 +000084GCC_DIR="gcc-${GCC_VERSION}"
85BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
86GDB_DIR="gdb-${GDB_VERSION}"
Patrick Georgi85bb9462016-02-11 17:22:16 +010087IASL_DIR="acpica-unix2-${IASL_VERSION}"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +010088PYTHON_DIR="Python-${PYTHON_VERSION}"
89EXPAT_DIR="expat-${EXPAT_VERSION}"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070090# CLANG toolchain directories
91LLVM_DIR="llvm-${CLANG_VERSION}.src"
92CFE_DIR="cfe-${CLANG_VERSION}.src"
93CRT_DIR="compiler-rt-${CLANG_VERSION}.src"
94CTE_DIR="clang-tools-extra-${CLANG_VERSION}.src"
Stefan Reinauera851bbb2016-01-29 18:03:26 -080095MAKE_DIR="make-${MAKE_VERSION}"
Stefan Reinauer074d9132009-09-26 16:43:17 +000096
Patrick Georgi6321f522015-02-27 23:36:26 +010097unset MAKELEVEL MAKEFLAGS
98
Patrick Georgi1861ff72011-10-31 12:15:55 +010099red='\033[0;31m'
100RED='\033[1;31m'
101green='\033[0;32m'
102GREEN='\033[1;32m'
103blue='\033[0;34m'
104BLUE='\033[1;34m'
105cyan='\033[0;36m'
106CYAN='\033[1;36m'
107NC='\033[0m' # No Color
Stefan Reinauer074d9132009-09-26 16:43:17 +0000108
zbaoe05c1ea2015-08-05 23:10:05 -0400109UNAME=$(uname | grep -iq cygwin && echo Cygwin || uname)
Martin Roth88463822016-01-07 11:03:36 -0700110HALT_FOR_TOOLS=0
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700111
Patrick Georgi198d23c2012-12-08 08:02:44 +0100112normalize_dirs()
113{
114 mkdir -p $DESTDIR$TARGETDIR/lib
115 test -d $DESTDIR$TARGETDIR/lib32 && mv $DESTDIR$TARGETDIR/lib32/* $DESTDIR$TARGETDIR/lib
116 test -d $DESTDIR$TARGETDIR/lib64 && mv $DESTDIR$TARGETDIR/lib64/* $DESTDIR$TARGETDIR/lib
117 rmdir -p $DESTDIR$TARGETDIR/lib32 $DESTDIR$TARGETDIR/lib64
118
119 perl -pi -e "s,/lib32,/lib," $DESTDIR$TARGETDIR/lib/*.la
120 perl -pi -e "s,/lib64,/lib," $DESTDIR$TARGETDIR/lib/*.la
121}
122
Stefan Reinauer93a96302015-06-15 12:36:53 -0700123please_install()
124{
Martin Roth88463822016-01-07 11:03:36 -0700125 HALT_FOR_TOOLS=1
Patrick Georgi5da95dc2015-07-17 23:33:05 +0200126 test -r /etc/os-release && . /etc/os-release
Stefan Reinauer93a96302015-06-15 12:36:53 -0700127 case "$ID_LIKE" in
128 debian) solution="sudo apt-get install $1" ;;
129 suse) solution="sudo zypper install $1" ;;
130 *) solution="using your OS packaging system" ;;
131 esac
132
Martin Roth591790f2016-03-20 20:39:04 -0600133 printf "${RED}ERROR:${red} Missing tool: Please install \'$1\'. (eg $solution)${NC}\n" >&2
Martin Rothe3963172016-01-06 13:54:32 -0700134 if [ -n "$2" ]; then
Martin Roth591790f2016-03-20 20:39:04 -0600135 printf "${RED}ERROR:${red} or install \'$2\'.${NC}\n" >&2
Martin Rothe3963172016-01-06 13:54:32 -0700136 fi
Stefan Reinauer93a96302015-06-15 12:36:53 -0700137}
138
Patrick Georgib7062882015-02-24 10:52:14 +0100139searchtool()
Stefan Reinauer074d9132009-09-26 16:43:17 +0000140{
141 # $1 short name
Patrick Georgib7062882015-02-24 10:52:14 +0100142 # $2 search string
143 # $3 soft fail if set
Martin Rothe3963172016-01-06 13:54:32 -0700144 # $4 alternative package to install on failure
Patrick Georgib7062882015-02-24 10:52:14 +0100145 # result: file name of that tool on stdout
146 # or no output if nothing suitable was found
147 search=GNU
148 if [ -n "$2" ]; then
149 search="$2"
150 fi
Stefan Reinauer074d9132009-09-26 16:43:17 +0000151 for i in "$1" "g$1" "gnu$1"; do
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700152 if [ -x "$(which $i 2>/dev/null)" ]; then
153 if [ "$(cat /dev/null | $i --version 2>&1 | grep -c "$search")" \
154 -gt 0 ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000155 echo $i
156 return
157 fi
158 fi
159 done
Andrew Wub67e9a12014-04-28 18:13:44 +0800160 # A workaround for OSX 10.9 and some BSDs, whose nongnu
161 # patch and tar also work.
Patrick Georgi7cb26b42015-04-28 21:29:22 +0200162 if [ $UNAME = "Darwin" -o $UNAME = "FreeBSD" -o $UNAME = "NetBSD" -o $UNAME = "OpenBSD" ]; then
Patrick Georgib7062882015-02-24 10:52:14 +0100163 if [ "$1" = "patch" -o "$1" = "tar" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700164 if [ -x "$(which $1 2>/dev/null)" ]; then
Zheng Bao36156ff2012-09-28 16:18:58 +0800165 echo $1
166 return
167 fi
168 fi
169 fi
zbao5cf758d2015-09-01 22:28:57 -0400170 if echo $1 | grep -q "sum" ; then
171 algor=$(echo $1 | sed -e 's,sum,,')
172 if [ -x "$(which $1 2>/dev/null)" ]; then
173 #xxxsum [file]
174 echo $1
175 return
176 elif [ -x "$(which $algor 2>/dev/null)" ]; then
177 #xxx [file]
178 echo $algor
179 return
180 elif [ -x "$(which openssl 2>/dev/null)" ]; then
181 #openssl xxx [file]
182 echo openssl $algor
183 return
184 elif [ -x "$(which cksum 2>/dev/null)" ]; then
185 #cksum -a xxx [file]
186 #cksum has special options in NetBSD. Actually, NetBSD will use the second case above.
187 echo "buildgcc" | cksum -a $algor > /dev/null 2>/dev/null && \
188 echo cksum -a $algor
189 return
zbao939dc842015-04-30 04:44:07 +0800190 fi
191 fi
Martin Rothe3963172016-01-06 13:54:32 -0700192
Martin Roth88463822016-01-07 11:03:36 -0700193 [ -z "$3" ] && please_install $1 $4
Patrick Georgib7062882015-02-24 10:52:14 +0100194 false
Stefan Reinauer074d9132009-09-26 16:43:17 +0000195}
196
Martin Roth591790f2016-03-20 20:39:04 -0600197# Run a compile check of the specified library option to see if it's installed
198check_for_library() {
199 local LIBRARY_FLAGS=$1
Aaron Durbin0ac4e5a2016-04-07 16:56:25 -0500200 local LIBRARY_PACKAGES="$2"
Martin Roth591790f2016-03-20 20:39:04 -0600201 local LIBTEST_FILE=.libtest
202
203 echo "int main(int argc, char **argv) { (void) argc; (void) argv; return 0; }" > "${LIBTEST_FILE}.c"
204
205 cc $CFLAGS $LIBRARY_FLAGS "${LIBTEST_FILE}.c" -o "${LIBTEST_FILE}" >/dev/null 2>&1 || \
206 please_install "$LIBRARY_PACKAGES"
207 rm -rf "${LIBTEST_FILE}.c" "${LIBTEST_FILE}"
208}
209
Nico Huberaee7f462016-09-20 12:55:19 +0200210buildcc_major() {
211 echo "${GCC_VERSION}" | cut -d. -f1
212}
213
Nico Huber26267a72016-12-04 02:45:58 +0100214buildcc_minor() {
215 echo "${GCC_VERSION}" | cut -d. -f2
216}
217
Nico Huberaee7f462016-09-20 12:55:19 +0200218buildcc_version() {
219 echo "${GCC_VERSION}" | cut -d. -f1-2
220}
221
222hostcc_major() {
223 (echo __GNUC__ | ${CC} -E - 2>/dev/null || echo 0) | tail -1
224}
225
226hostcc_minor() {
227 (echo __GNUC_MINOR__ | ${CC} -E - 2>/dev/null || echo 0) | tail -1
228}
229
230hostcc_version() {
231 printf "%d.%d" "$(hostcc_major)" "$(hostcc_minor)"
232}
233
Nico Huber75797162016-09-20 13:01:57 +0200234hostcc_has_gnat1() {
235 [ -x "$(${CC} -print-prog-name=gnat1)" ]
236}
237
238ada_requested() {
239 echo "${LANGUAGES}" | grep -q '\<ada\>'
240}
241
242check_gnat() {
243 if hostcc_has_gnat1; then
Nico Huber26267a72016-12-04 02:45:58 +0100244 if [ \( "$(hostcc_major)" -lt "$(buildcc_major)" -o \
245 \( "$(hostcc_major)" -eq "$(buildcc_major)" -a \
246 "$(hostcc_minor)" -lt "$(buildcc_minor)" \) \) \
247 -a \
248 "${BOOTSTRAP}" != "1" ]; \
249 then
Nico Huber75797162016-09-20 13:01:57 +0200250 printf "\n${RED}ERROR:${red} Building the Ada compiler (gnat $(buildcc_version)) "
Nico Huber26267a72016-12-04 02:45:58 +0100251 printf "with an older host compiler\n version ($(hostcc_version)) "
Nico Huber75797162016-09-20 13:01:57 +0200252 printf "requires bootstrapping (-b).${NC}\n\n"
253 HALT_FOR_TOOLS=1
254 fi
Nico Huber04a1f6a2016-09-20 13:07:49 +0200255 else
256 please_install gnat
Nico Huber75797162016-09-20 13:01:57 +0200257 fi
258}
259
Nico Huber156d87c2016-09-20 12:59:53 +0200260check_cc() {
261 if is_package_enabled "GCC"; then
262 if [ "$(hostcc_major)" != "$(buildcc_major)" -a "${BOOTSTRAP}" != "1" ]; then
263 printf "\n${red}warning: Building GCC $(buildcc_version) with a different major "
264 printf "version ($(hostcc_version)).\n"
265 printf " Bootstrapping (-b) is recommended.${NC}\n\n"
266 fi
Nico Huber75797162016-09-20 13:01:57 +0200267 if ada_requested; then
268 check_gnat
269 fi
Nico Huber156d87c2016-09-20 12:59:53 +0200270 fi
271}
272
zbao06e85ac2015-08-24 22:08:36 -0400273check_sum() {
274 test -z "$CHECKSUM" || \
275 test "$(cat sum/$1.cksum 2>/dev/null | sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@')" = \
276 "$($CHECKSUM tarballs/$1 2>/dev/null | sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@')"
277}
278
279compute_sum() {
280 test ! -f sum/$1.cksum && test -f tarballs/$1 && \
281 (test -z "$CHECKSUM" || $CHECKSUM tarballs/$1 > sum/$1.cksum ) && \
282 printf "(checksum created. ${RED}Note. Please upload sum/$1.cksum if the corresponding archive is upgraded.)${NC}"
283}
284
zbao282dd952015-09-06 05:00:04 -0400285download_showing_percentage() {
286 url=$1
287 printf " ..${red} 0%%"
288 wget --no-check-certificate $url 2>&1 | while read line; do
zbao282dd952015-09-06 05:00:04 -0400289 echo $line | grep -o "[0-9]\+%" | awk '{printf("\b\b\b\b%4s", $1)}'
zbao282dd952015-09-06 05:00:04 -0400290 done
Zheng Baobb003c82016-08-05 13:41:51 +0800291 printf "${NC}"
zbao282dd952015-09-06 05:00:04 -0400292}
293
Stefan Reinauer88935482015-06-05 09:51:10 -0700294download() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700295 package=$1
296 archive="$(eval echo \$$package"_ARCHIVE")"
Stefan Reinauer699ac272015-06-05 12:43:28 -0700297
Stefan Reinauerd7649122015-06-09 11:44:25 -0700298 FILE=$(basename $archive)
Stefan Reinauer88935482015-06-05 09:51:10 -0700299 printf " * $FILE "
300
zbaod22b9ea2015-08-30 23:35:16 -0400301 if test -f tarballs/$FILE && check_sum $FILE ; then
302 printf "(cached)"
303 else
Paul Menzelb2ba3952015-06-12 11:58:14 +0200304 printf "(downloading from $archive)"
Stefan Reinauer88935482015-06-05 09:51:10 -0700305 rm -f tarballs/$FILE
306 cd tarballs
zbao282dd952015-09-06 05:00:04 -0400307 download_showing_percentage $archive
Stefan Reinauer88935482015-06-05 09:51:10 -0700308 cd ..
zbao06e85ac2015-08-24 22:08:36 -0400309 compute_sum $FILE
zbaod22b9ea2015-08-30 23:35:16 -0400310 fi
311
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700312 if [ ! -f tarballs/$FILE ]; then
zbao282dd952015-09-06 05:00:04 -0400313 printf "\n${RED}Failed to download $FILE.${NC}\n"
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700314 exit 1
315 fi
Stefan Reinauer88935482015-06-05 09:51:10 -0700316 printf "\n"
317}
318
319unpack_and_patch() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700320 package=$1
321 archive="$(eval echo \$$package"_ARCHIVE")"
322 dir="$(eval echo \$$package"_DIR")"
Stefan Reinauer88935482015-06-05 09:51:10 -0700323 test -d ${dir} && test -f ${dir}/.unpack_success || (
Stefan Reinauerd7649122015-06-09 11:44:25 -0700324 printf " * $(basename $archive)\n"
Stefan Reinauer88935482015-06-05 09:51:10 -0700325 FLAGS=zxf
Stefan Reinauerd7649122015-06-09 11:44:25 -0700326 suffix=$(echo $archive | sed 's,.*\.,,')
Martin Roth95f7b222016-03-20 12:38:48 -0600327 if [ "$suffix" = "gz" ] && [ -n "$PIGZ" ]; then FLAGS="-I pigz -xf"
328 elif [ "$suffix" = "gz" ]; then FLAGS=zxf
329 elif [ "$suffix" = "bz2" ] && [ -n "$LBZIP2" ]; then FLAGS="-I lbzip2 -xf"
330 elif [ "$suffix" = "bz2" ]; then FLAGS=jxf
331 elif [ "$suffix" = "xz" ]; then FLAGS="--xz -xf"
332 elif [ "$suffix" = "lzma" ]; then FLAGS="--lzma -xf"
333 fi
Stefan Reinauerd7649122015-06-09 11:44:25 -0700334 $TAR $FLAGS tarballs/$(basename $archive)
Stefan Reinauer88935482015-06-05 09:51:10 -0700335 for patch in patches/${dir}_*.patch; do
336 test -r $patch || continue
Stefan Reinauerd7649122015-06-09 11:44:25 -0700337 printf " o $(basename $patch)\n"
Nico Huber4b48ed82016-07-27 00:28:03 +0200338 (cd ${dir} && $PATCH -s -N -p1 <../${patch}) || {
Stefan Reinauer88935482015-06-05 09:51:10 -0700339 printf "\n${RED}Failed $patch.${NC}\n"
Stefan Reinauer916b8452015-06-09 11:54:49 -0700340 exit 1
341 }
Stefan Reinauer88935482015-06-05 09:51:10 -0700342 done
343 touch ${dir}/.unpack_success
344 )
345}
346
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700347fn_exists()
348{
Patrick Georgic8665912015-06-11 21:04:31 +0200349 type $1 >/dev/null 2>&1
Patrick Georgi74c06422015-03-25 18:40:13 +0100350}
351
Patrick Georgif2c15f52015-06-11 21:07:31 +0200352is_package_enabled()
353{
354 echo "$PACKAGES" |grep -q "\<$1\>"
355}
356
Martin Roth444ece22016-04-01 18:46:29 -0600357package_uses_targetarch()
358{
Stefan Reinauera344b682016-04-21 19:29:51 -0700359 if [ "$1" = "GCC" ] || [ "$1" = "GDB" ] || [ "$1" = "BINUTILS" ]; then
Martin Roth444ece22016-04-01 18:46:29 -0600360 true
361 else
362 false
363 fi
364}
365
Nico Huber11ea2b32016-01-26 16:09:31 +0100366generic_build()
367{
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700368 package=$1
Nico Huber11ea2b32016-01-26 16:09:31 +0100369 host_target=$2
370 builddir=$3
371 success=$4
Martin Rothc295d5e2016-11-14 11:56:11 -0700372 version=$5
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700373
374 fn_exists build_$package || return
375
Nico Huber11ea2b32016-01-26 16:09:31 +0100376 mkdir -p "$builddir"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700377
Nico Huber11ea2b32016-01-26 16:09:31 +0100378 if [ -f "$success" ]; then
Martin Roth2c6a8062016-11-14 11:58:39 -0700379 printf "Skipping $package v$version for $host_target as it is already built\n"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700380 else
Martin Roth2c6a8062016-11-14 11:58:39 -0700381 printf "Building $package v$version for $host_target ... "
Nico Huber11ea2b32016-01-26 16:09:31 +0100382 DIR="$PWD"
383 cd "$builddir"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700384 rm -f .failed
Nico Huber11ea2b32016-01-26 16:09:31 +0100385 build_${package} $host_target > build.log 2>&1
386 cd "$DIR"
387 if [ ! -f "$builddir/.failed" ]; then
388 touch "$success";
389 else
390 printf "${RED}failed${NC}. Check $builddir/build.log.\n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700391 exit 1
392 fi
393 printf "${green}ok${NC}\n"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700394 fi
395}
396
Nico Huber11ea2b32016-01-26 16:09:31 +0100397build_for_host()
398{
Martin Rothc295d5e2016-11-14 11:56:11 -0700399 package="$1"
400 version="$(eval echo \$$package"_VERSION")"
Patrick Georgi1b593e52016-11-25 15:27:41 +0100401 generic_build "$package" host "build-$package" "${DESTDIR}${TARGETDIR}/.${package}.${version}.success" "$version"
Nico Huber11ea2b32016-01-26 16:09:31 +0100402}
403
404build_for_target()
405{
Martin Rothc295d5e2016-11-14 11:56:11 -0700406 package="$1"
407 version="$(eval echo \$$package"_VERSION")"
Patrick Georgi1b593e52016-11-25 15:27:41 +0100408 generic_build "$package" target "build-${TARGETARCH}-$package" "${DESTDIR}${TARGETDIR}/.${TARGETARCH}-${package}.${version}.success" "$version"
Nico Huber11ea2b32016-01-26 16:09:31 +0100409}
410
411build()
412{
413 if package_uses_targetarch $1; then
Patrick Georgi1b593e52016-11-25 15:27:41 +0100414 if [ $BOOTSTRAP -eq 1 -a ! -f "${DESTDIR}${TARGETDIR}/.GCC.success" ]; then
Nico Huber234d2462016-01-26 16:10:17 +0100415 build_for_host GCC
416 fi
Nico Huber11ea2b32016-01-26 16:09:31 +0100417 build_for_target $1
418 else
419 build_for_host $1
420 fi
421}
422
Zheng Baobb003c82016-08-05 13:41:51 +0800423quit()
424{
425 printf "${NC}Stop\n"
426 exit 1
427}
428
Stefan Reinauer074d9132009-09-26 16:43:17 +0000429cleanup()
430{
Stefan Reinauer88e83e52016-04-06 15:39:48 -0700431 if [ $SAVETEMPS -ne 0 ]; then
432 printf "Leaving temporary files around... ${green}ok${NC}\n"
433 return
434 fi
435
Stefan Reinauer074d9132009-09-26 16:43:17 +0000436 printf "Cleaning up temporary files... "
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700437 for package in $PACKAGES; do
Martin Roth444ece22016-04-01 18:46:29 -0600438 rm -rf build-${TARGETARCH}-$package build-$package $(eval echo \$$package"_DIR")
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700439 done
zbaoff3f15c2015-08-26 22:00:31 -0400440 rm -f getopt
Stefan Reinauer074d9132009-09-26 16:43:17 +0000441 printf "${green}ok${NC}\n"
442}
443
444myhelp()
445{
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700446 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>] [-C] [-G] [-S]\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000447 printf " $0 [-V|--version]\n"
448 printf " $0 [-h|--help]\n\n"
449
450 printf "Options:\n"
451 printf " [-V|--version] print version number and exit\n"
452 printf " [-h|--help] print this help and exit\n"
453 printf " [-c|--clean] remove temporary files before build\n"
454 printf " [-t|--savetemps] don't remove temporary files after build\n"
Patrick Georgic1a75b12011-11-04 21:37:14 +0100455 printf " [-y|--ccache] Use ccache when building cross compiler\n"
Nico Huber152e78e2016-09-20 15:38:40 +0200456 printf " [-n|--nocolor] don't print color codes in output\n"
457 printf " [-u|--urls] print the urls for all packages\n"
Patrick Georgi73166c72010-04-14 20:42:42 +0000458 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
Martin Roth588c79d2016-03-20 12:03:20 -0600459 printf " [-s]--supported <tool> print supported version of a tool\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000460 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
Martin Roth588c79d2016-03-20 12:03:20 -0600461 printf " (defaults to $TARGETDIR)\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000462 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100463 printf " (for RPM builds, default unset)\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700464 printf " [-P|--package <package>] Build a specific package: GCC, CLANG, IASL, GDB\n"
465 printf " (defaults to $PACKAGE)\n"
466 printf "GCC specific options:\n"
Nico Huber234d2462016-01-26 16:10:17 +0100467 printf " [-b|--bootstrap] bootstrap the host compiler before building\n"
468 printf " the cross compiler\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700469 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
470 printf " (defaults to $TARGETARCH)\n"
Nico Huberbb313bf2015-09-08 12:30:27 +0200471 printf " [-l|--languages <languages>] comma separated list of target languages\n"
Nico Huber66c2c1a2016-09-20 13:09:29 +0200472 printf " (defaults to $DEFAULT_LANGUAGES)\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700473 printf "GDB specific options:\n"
474 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
475 printf " (defaults to $TARGETARCH)\n"
476 printf " [-S|--scripting] build scripting support for GDB\n\n"
Martin Rothd1da74f2015-06-21 12:06:28 -0600477 printf "Platforms for GCC & GDB:\n"
Martin Roth3e0f74d2016-03-08 12:07:04 -0700478 printf " x86_64 i386-elf i386-mingw32 mipsel-elf riscv-elf arm aarch64\n"
Jonathan Neuschäferf14f6402016-03-11 20:22:23 +0100479 printf " powerpc64le-linux-gnu nds32le-elf\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000480}
481
Martin Rothedf965a2015-11-25 16:58:33 -0700482printversion() {
483 printf "${blue}Welcome to the ${red}coreboot${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
484}
485
Stefan Reinauer074d9132009-09-26 16:43:17 +0000486myversion()
487{
Martin Rothedf965a2015-11-25 16:58:33 -0700488 printversion
Stefan Reinauer074d9132009-09-26 16:43:17 +0000489
490 cat << EOF
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000491Copyright (C) 2008-2010 by coresystems GmbH
Marc Jones2aac3f62011-08-08 16:07:50 -0600492Copyright (C) 2011 by Sage Electronic Engineering
Stefan Reinauer074d9132009-09-26 16:43:17 +0000493
494This program is free software; you can redistribute it and/or modify
495it under the terms of the GNU General Public License as published by
496the Free Software Foundation; version 2 of the License.
497
498This program is distributed in the hope that it will be useful,
499but WITHOUT ANY WARRANTY; without even the implied warranty of
500MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
501GNU General Public License for more details.
502
503EOF
504}
505
Nico Huber5f72f962016-01-31 23:05:19 +0100506have_hostcflags_from_gmp() {
507 grep -q __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h >/dev/null 2>&1
508}
509
510set_hostcflags_from_gmp() {
511 # Now set CFLAGS to match GMP CFLAGS but strip out -pedantic
512 # as GCC 4.6.x fails if it's there.
Idwer Volleringba349ab2016-07-24 02:10:19 +0200513 export HOSTCFLAGS="$(grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2 |\
514 sed s,-pedantic,,)"
Nico Huber5f72f962016-01-31 23:05:19 +0100515}
516
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700517build_GMP() {
Nico Huber8bbd5962016-12-22 16:05:54 +0100518 # Check if GCC enables `-pie` by default (possible since GCC 6).
519 # We need PIC in all static libraries then.
520 if "${CC}" -dumpspecs 2>/dev/null | grep -q '[{;][[:space:]]*:-pie\>'
521 then
522 OPTIONS="$OPTIONS --with-pic"
523 fi
524
Paul Kocialkowski7899db22016-06-17 21:41:00 -0700525 CC="$CC" ../${GMP_DIR}/configure --disable-shared --enable-fat \
Nico Huber5f72f962016-01-31 23:05:19 +0100526 --prefix=$TARGETDIR $OPTIONS \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700527 || touch .failed
528 $MAKE $JOBS || touch .failed
529 $MAKE install DESTDIR=$DESTDIR || touch .failed
530
531 normalize_dirs
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700532
Nico Huber5f72f962016-01-31 23:05:19 +0100533 set_hostcflags_from_gmp
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700534}
535
536build_MPFR() {
537 test $UNAME = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
538 CC="$CC" ../${MPFR_DIR}/configure --disable-shared --prefix=$TARGETDIR \
539 --infodir=$TARGETDIR/info \
540 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || \
541 touch .failed
542 $MAKE $JOBS || touch .failed
543 $MAKE install DESTDIR=$DESTDIR || touch .failed
544
545 normalize_dirs
546
547 # work around build problem of libgmp.la
548 if [ "$DESTDIR" != "" ]; then
549 perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
550 fi
551}
552
553build_MPC() {
554 CC="$CC" ../${MPC_DIR}/configure --disable-shared --prefix=$TARGETDIR \
555 --infodir=$TARGETDIR/info --with-mpfr=$DESTDIR$TARGETDIR \
556 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || \
557 touch .failed
558 $MAKE $JOBS || touch .failed
559 $MAKE install DESTDIR=$DESTDIR || touch .failed
560
561 normalize_dirs
562}
563
564build_LIBELF() {
565 CC="$CC" CFLAGS="$HOSTCFLAGS" libelf_cv_elf_h_works=no \
566 ../${LIBELF_DIR}/configure --disable-shared --prefix=$TARGETDIR \
567 --infodir=$TARGETDIR/info CFLAGS="$HOSTCFLAGS" || touch .failed
568 $MAKE $JOBS || touch .failed
569 $MAKE install prefix=$DESTDIR$TARGETDIR || touch .failed
570
571 normalize_dirs
572}
573
574build_BINUTILS() {
Nico Huberae6187f2016-12-22 22:16:39 +0100575 if [ $TARGETARCH = "x86_64-elf" ]; then
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700576 ADDITIONALTARGET=",i386-elf"
577 fi
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700578 CC="$CC" ../binutils-${BINUTILS_VERSION}/configure --prefix=$TARGETDIR \
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700579 --target=${TARGETARCH} --enable-targets=${TARGETARCH}${ADDITIONALTARGET} \
580 --disable-werror --disable-nls --enable-lto --enable-gold \
Stefan Reinauer2fe2d3d2016-04-05 02:14:48 -0700581 --enable-interwork --enable-multilib \
Nico Huber010ecf802016-12-22 16:19:37 +0100582 --enable-plugins --enable-multilibs
583 CFLAGS="$HOSTCFLAGS" \
584 CXXFLAGS="$HOSTCFLAGS" \
585 || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700586 $MAKE $JOBS || touch .failed
587 $MAKE install DESTDIR=$DESTDIR || touch .failed
588}
589
Nico Huber234d2462016-01-26 16:10:17 +0100590bootstrap_GCC() {
591 CC="$CC" \
592 CFLAGS="$HOSTCFLAGS" \
593 CFLAGS_FOR_BUILD="$HOSTCFLAGS" \
594 CFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
595 CXXFLAGS="$HOSTCFLAGS" \
596 CXXFLAGS_FOR_BUILD="$HOSTCFLAGS" \
597 CXXFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
598 ../gcc-${GCC_VERSION}/configure \
599 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
600 --enable-bootstrap \
601 --disable-werror --disable-nls \
602 --disable-shared --disable-multilib \
603 --disable-libssp --disable-libquadmath --disable-libcc1 \
604 ${GCC_OPTIONS} --enable-languages="${LANGUAGES}" \
605 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
606 --with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
607 --with-pkgversion="coreboot bootstrap v$CROSSGCC_VERSION $CROSSGCC_DATE" \
608 && \
609 $MAKE $JOBS BOOT_CFLAGS="$HOSTCFLAGS" BUILD_CONFIG="" bootstrap && \
610 $MAKE install-gcc \
611 install-target-libgcc \
612 maybe-install-target-libada \
613 maybe-install-target-libstdc++-v3 \
614 DESTDIR=$DESTDIR && \
615 ln -s gcc $DESTDIR$TARGETDIR/bin/cc || touch .failed
616}
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700617
Nico Huber234d2462016-01-26 16:10:17 +0100618build_cross_GCC() {
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700619 # Work around crazy code generator in GCC that confuses CLANG.
Aaron Durbinb2229dc2016-04-07 16:58:10 -0500620 $CC --version | grep clang >/dev/null 2>&1 && \
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700621 HOSTCFLAGS="$HOSTCFLAGS -fbracket-depth=1024"
622
Stefan Reinauerd0f83722015-06-09 11:52:28 -0700623 # GCC does not honor HOSTCFLAGS at all. CFLAGS are used for
624 # both target and host object files.
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700625 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
626 # but it does not seem to work properly. At least the host library
627 # libiberty is not compiled with CFLAGS_FOR_BUILD.
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700628 # Also set the CXX version of the flags because GCC is now compiled
629 # using C++.
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100630 CC="$CC" CFLAGS_FOR_TARGET="-O2 -Dinhibit_libc" CFLAGS="$HOSTCFLAGS" \
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700631 CFLAGS_FOR_BUILD="$HOSTCFLAGS" CXXFLAGS="$HOSTCFLAGS" \
632 CXXFLAGS_FOR_BUILD="$HOSTCFLAGS" ../gcc-${GCC_VERSION}/configure \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700633 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
634 --target=${TARGETARCH} --disable-werror --disable-shared \
Stefan Reinauer2f97ebc2015-06-09 12:24:54 -0700635 --enable-lto --enable-plugins --enable-gold --enable-ld=default \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700636 --disable-libssp --disable-bootstrap --disable-nls \
637 --disable-libquadmath --without-headers \
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100638 --disable-threads \
Patrick Georgi1fc02d12016-03-23 21:44:43 +0100639 --enable-interwork --enable-multilib --enable-targets=all \
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100640 --disable-libatomic --disable-libcc1 --disable-decimal-float \
Nico Huberbb313bf2015-09-08 12:30:27 +0200641 ${GCC_OPTIONS} --enable-languages="${LANGUAGES}" \
Ronald G. Minnich7ee16b72016-02-12 21:54:59 +0000642 --with-system-zlib \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700643 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
644 --with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
645 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE" \
646 || touch .failed
647 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-gcc || touch .failed
648 $MAKE install-gcc DESTDIR=$DESTDIR || touch .failed
649
Stefan Reinauerd7649122015-06-09 11:44:25 -0700650 if [ "$(echo $TARGETARCH | grep -c -- -mingw32)" -eq 0 ]; then
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700651 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-target-libgcc || touch .failed
652 $MAKE install-target-libgcc DESTDIR=$DESTDIR || touch .failed
653 fi
654}
655
Nico Huber234d2462016-01-26 16:10:17 +0100656build_GCC() {
657 if [ "$1" = host ]; then
658 bootstrap_GCC
659 else
660 build_cross_GCC
661 fi
662}
663
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700664build_EXPAT() {
665 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${EXPAT_DIR}/configure --disable-shared \
Stefan Reinauera344b682016-04-21 19:29:51 -0700666 --prefix=$TARGETDIR || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700667 $MAKE || touch .failed
668 $MAKE install DESTDIR=$DESTDIR || touch .failed
669
670 normalize_dirs
671}
672
673build_PYTHON() {
674 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${PYTHON_DIR}/configure --prefix=$TARGETDIR \
Stefan Reinauera344b682016-04-21 19:29:51 -0700675 || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700676 $MAKE $JOBS || touch .failed
677 $MAKE install DESTDIR=$DESTDIR || touch .failed
678
679 normalize_dirs
680}
681
682build_GDB() {
683 export PYTHONHOME=$DESTDIR$TARGETDIR
684 if [ $(uname) != "FreeBSD" -a $(uname) != "NetBSD" ]; then
685 LIBDL="-ldl"
686 fi
687 LDFLAGS="-Wl,-rpath,\$\$ORIGIN/../lib/ -L$DESTDIR$TARGETDIR/lib \
688 -lpthread $LIBDL -lutil" \
689 CC="$CC" CFLAGS="$HOSTCFLAGS -I$DESTDIR$TARGETDIR/include" \
Stefan Reinauer7b40e422015-06-09 11:56:32 -0700690 ../${GDB_DIR}/configure --prefix=$TARGETDIR \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700691 --target=${TARGETARCH} --disable-werror --disable-nls
692 $MAKE $JOBS || touch .failed
693 $MAKE install DESTDIR=$DESTDIR || touch .failed
694}
695
696build_IASL() {
697 RDIR=$PWD
698 cd ../$IASL_DIR/generate/unix
699 CFLAGS="$HOSTCFLAGS"
700 HOST="_LINUX"
701 test $UNAME = "Darwin" && HOST="_APPLE"
702 test $UNAME = "FreeBSD" && HOST="_FreeBSD"
703 test $UNAME = "Cygwin" && HOST="_CYGWIN"
Martin Roth6f656132015-12-08 11:14:10 -0700704 HOST="$HOST" OPT_CFLAGS="-O -D_FORTIFY_SOURCE=2 -D COREBOOT_TOOLCHAIN_VERSION='\"coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE\"' " CFLAGS="$CFLAGS" $MAKE CC="$CC" iasl || touch $RDIR/.failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700705 rm -f $DESTDIR$TARGETDIR/bin/iasl || touch $RDIR/.failed
706 cp bin/iasl $DESTDIR$TARGETDIR/bin || touch $RDIR/.failed
707}
708
709build_LLVM() {
710 cd ..
711 ln -sf $PWD/$CFE_DIR $LLVM_DIR/tools/clang
712 ln -sf $PWD/$CTE_DIR $LLVM_DIR/tools/clang/tools/extra
713 ln -sf $PWD/$CRT_DIR $LLVM_DIR/projects/compiler-rt
714 cd -
715
716 $CMAKE -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$DESTDIR$TARGETDIR \
Martin Rothfb5647a2015-12-18 09:26:45 -0700717 -DCLANG_VENDOR="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE - " \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700718 -DCMAKE_BUILD_TYPE=Release ../$LLVM_DIR || touch .failed
719 $MAKE $JOBS || touch .failed
720 $MAKE install || touch .failed
721
722 cp -a ../$CFE_DIR/tools/scan-build/* $DESTDIR$TARGETDIR/bin
723 cp -a ../$CFE_DIR/tools/scan-view/* $DESTDIR$TARGETDIR/bin
Stefan Reinauere9e6e3d2015-07-07 00:20:42 +0200724
725 # create symlinks to work around broken --print-librt-file-name
726 # when used with -target.
727 cd $DESTDIR$TARGETDIR/lib/clang/${CLANG_VERSION}/lib
728 for i in */libclang_rt.builtins*.a; do
729 ln -s $i .
730 done
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700731}
732
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800733build_MAKE() {
734 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${MAKE_DIR}/configure --prefix=$TARGETDIR \
735 --disable-nls || touch .failed
736 $MAKE $JOBS || touch .failed
737 $MAKE install DESTDIR=$DESTDIR || touch .failed
738
739 normalize_dirs
740}
741
Martin Rothedf965a2015-11-25 16:58:33 -0700742print_stable() {
743 case "$PRINTSTABLE" in
744 AUTOCONF|autoconf) printf "%s\n" "$GCC_AUTOCONF_VERSION";;
745 BINUTILS|binutils) printf "%s\n" "$BINUTILS_VERSION";;
746 CLANG|clang) printf "%s\n" "$CLANG_VERSION";;
747 EXPAT|expat) printf "%s\n" "$EXPAT_VERSION";;
748 GCC|gcc) printf "%s\n" "$GCC_VERSION";;
749 GDB|gdb) printf "%s\n" "$GDB_VERSION";;
750 GMP|gmp) printf "%s\n" "$GMP_VERSION";;
751 IASL|iasl) printf "%s\n" "$IASL_VERSION";;
752 LIBELF|libelf) printf "%s\n" "$LIBELF_VERSION";;
753 MPC|mpc) printf "%s\n" "$MPC_VERSION";;
754 MPFR|mpfr) printf "%s\n" "$MPFR_VERSION";;
755 PYTHON|python) printf "%s\n" "$PYTHON_VERSION";;
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800756 MAKE|make) printf "%s\n" "$MAKE_VERSION";;
Martin Rothedf965a2015-11-25 16:58:33 -0700757 *) printf "Unknown tool %s\n" "$PRINTSTABLE";;
758 esac
759}
Stefan Reinauer074d9132009-09-26 16:43:17 +0000760
Zheng Baobb003c82016-08-05 13:41:51 +0800761trap quit 1 2 3 15
762
Patrick Georgi6bba29f2010-01-29 17:40:52 +0000763# Look if we have getopt. If not, build it.
764export PATH=$PATH:.
765getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
766
Stefan Reinauer074d9132009-09-26 16:43:17 +0000767# parse parameters.. try to find out whether we're running GNU getopt
Stefan Reinauerd7649122015-06-09 11:44:25 -0700768getoptbrand="$(getopt -V | sed -e '1!d' -e 's,^\(......\).*,\1,')"
Peter Stuge09377b72011-08-21 06:24:55 +0200769if [ "${getoptbrand}" = "getopt" ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000770 # Detected GNU getopt that supports long options.
Nico Hubercc414dd2016-09-20 13:59:38 +0200771 args=$(getopt -l version,help,clean,directory:,bootstrap,platform:,languages:,package:,jobs:,destdir:,savetemps,scripting,ccache,supported:,urls,nocolor -o Vhcd:bp:l:P:j:D:tSys:un -- "$@")
Nico Huber78df0bf2016-09-20 14:11:53 +0200772 getopt_ret=$?
Nico Hubercc414dd2016-09-20 13:59:38 +0200773 eval set -- "$args"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000774else
775 # Detected non-GNU getopt
Nico Huber152e78e2016-09-20 15:38:40 +0200776 args=$(getopt Vhcd:bp:l:P:j:D:tSys:un $*)
Nico Huber78df0bf2016-09-20 14:11:53 +0200777 getopt_ret=$?
Stefan Reinauer074d9132009-09-26 16:43:17 +0000778 set -- $args
779fi
780
Nico Huber78df0bf2016-09-20 14:11:53 +0200781if [ $getopt_ret != 0 ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000782 myhelp
783 exit 1
784fi
785
786while true ; do
787 case "$1" in
788 -V|--version) shift; myversion; exit 0;;
789 -h|--help) shift; myhelp; exit 0;;
Patrick Georgi44af57a2013-12-19 22:06:22 +0100790 -c|--clean) shift; clean=1;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000791 -t|--savetemps) shift; SAVETEMPS=1;;
792 -d|--directory) shift; TARGETDIR="$1"; shift;;
Nico Huber234d2462016-01-26 16:10:17 +0100793 -b|--bootstrap) shift; BOOTSTRAP=1;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000794 -p|--platform) shift; TARGETARCH="$1"; shift;;
Nico Huberbb313bf2015-09-08 12:30:27 +0200795 -l|--languages) shift; LANGUAGES="$1"; shift;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000796 -D|--destdir) shift; DESTDIR="$1"; shift;;
Martin Roth2c6a8062016-11-14 11:58:39 -0700797 -j|--jobs) shift; THREADS="$1"; JOBS="-j $1"; shift;;
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700798 -P|--package) shift; PACKAGE="$1"; shift;;
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100799 -S|--scripting) shift; SKIPPYTHON=0;;
Patrick Georgic1a75b12011-11-04 21:37:14 +0100800 -y|--ccache) shift; USECCACHE=1;;
Martin Rothedf965a2015-11-25 16:58:33 -0700801 -s|--supported) shift; PRINTSTABLE="$1"; shift;;
Nico Huber152e78e2016-09-20 15:38:40 +0200802 -u|--urls) shift; printf "%s\n" "$ALL_ARCHIVES"; exit 0;;
803 -n|--nocolor) shift; \
Martin Roth4a53db02016-03-01 15:21:07 -0700804 unset red RED green GREEN blue BLUE cyan CYAN NC;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200805 --) shift; break;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200806 *) break;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000807 esac
808done
809
Nico Huberbd74d562016-09-20 14:16:43 +0200810if [ $# -gt 0 ]; then
811 printf "Excessive arguments: $*\n"
812 myhelp
813 exit 1
814fi
815
Martin Rothedf965a2015-11-25 16:58:33 -0700816if [ -n "$PRINTSTABLE" ]; then
817 print_stable
818 exit 0
819fi
820
821#print toolchain builder version string as the header
822printversion
823
Martin Roth2c6a8062016-11-14 11:58:39 -0700824printf "Building toolchain using %d thread(s).\n\n" "$THREADS"
825
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700826case "$TARGETARCH" in
Timothy Pearsona9f62352015-12-30 18:59:00 -0600827 x86_64-elf) ;;
828 x86_64*) TARGETARCH=x86_64-elf;;
829 i386-elf) ;;
830 i386-mingw32) ;;
831 mipsel-elf) ;;
Iru Caiae8e3a02016-04-02 10:50:59 +0800832 riscv-elf) TARGETARCH=riscv64-elf;;
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100833 powerpc64*-linux*) ;;
Timothy Pearsona9f62352015-12-30 18:59:00 -0600834 i386*) TARGETARCH=i386-elf;;
Stefan Reinauer27522ad2016-01-29 17:31:34 -0800835 arm*) TARGETARCH=arm-eabi;;
Timothy Pearsona9f62352015-12-30 18:59:00 -0600836 aarch64*) TARGETARCH=aarch64-elf;;
Martin Roth3e0f74d2016-03-08 12:07:04 -0700837 nds32le-elf) ;;
Timothy Pearsona9f62352015-12-30 18:59:00 -0600838 *) printf "${red}WARNING: Unsupported architecture $TARGETARCH.${NC}\n\n"; ;;
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700839esac
840
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700841# Figure out which packages to build
Stefan Reinauer699ac272015-06-05 12:43:28 -0700842
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700843case "$PACKAGE" in
844 GCC|gcc)
845 echo "Target architecture is now $TARGETARCH"
846 NAME="${TARGETARCH} cross GCC"
Stefan Reinauerbd8b2e32015-06-17 11:55:02 -0700847 PACKAGES="GMP MPFR MPC LIBELF BINUTILS GCC"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700848 ;;
849 GDB|gdb)
850 NAME="${TARGETARCH} cross GDB"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700851 if [ $SKIPPYTHON -eq 0 ]; then
Ronald G. Minnichfb944f42016-02-27 04:06:42 +0000852 PACKAGES="EXPAT PYTHON GDB"
853 else
854 PACKAGES="EXPAT GDB"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700855 fi
856 ;;
857 CLANG|clang)
858 NAME=clang
859 PACKAGES="LLVM CFE CRT CTE"
David Hendrickscd5cdd32015-06-15 17:03:22 -0700860 CMAKE=$(searchtool cmake "cmake") || exit $?
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700861 ;;
862 IASL|iasl)
863 NAME="IASL ACPI compiler"
864 PACKAGES=IASL
865 ;;
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800866 MAKE|make)
867 NAME="GNU Make"
868 PACKAGES=MAKE
869 ;;
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700870 *)
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800871 printf "${red}ERROR: Unsupported package $PACKAGE. (Supported packages are GCC, GDB, CLANG, IASL, MAKE)${NC}\n\n";
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700872 exit 1
873 ;;
874esac
Peter Stugeceacd772011-06-09 04:54:16 +0200875
Stefan Reinauer66e93352015-06-16 22:38:23 +0200876# Find all the required tools:
877
878TAR=$(searchtool tar) || exit $?
879PATCH=$(searchtool patch) || exit $?
880MAKE=$(searchtool make) || exit $?
881SHA1SUM=$(searchtool sha1sum)
zbao5cf758d2015-09-01 22:28:57 -0400882#SHA512SUM=$(searchtool sha512sum)
883#MD5SUM=$(searchtool md5sum)
Stefan Reinauer66e93352015-06-16 22:38:23 +0200884CHECKSUM=$SHA1SUM
Martin Roth95f7b222016-03-20 12:38:48 -0600885LBZIP2=$(searchtool lbzip2 "" nofail)
886PIGZ=$(searchtool pigz "" nofail)
Stefan Reinauer66e93352015-06-16 22:38:23 +0200887
888searchtool m4 > /dev/null
889searchtool bison > /dev/null
890searchtool flex flex > /dev/null
891searchtool g++ "Free Software Foundation" nofail > /dev/null || \
Marc Jones5ee69512015-06-17 15:38:17 -0600892searchtool clang "clang version" nofail > /dev/null || \
Martin Rothe3963172016-01-06 13:54:32 -0700893searchtool clang "LLVM" "" "g++" > /dev/null
Stefan Reinauer66e93352015-06-16 22:38:23 +0200894searchtool wget > /dev/null
895searchtool bzip2 "bzip2," > /dev/null
896
Martin Roth591790f2016-03-20 20:39:04 -0600897check_for_library "-lz" "zlib (zlib1g-dev or zlib-devel)"
898
Nico Huber156d87c2016-09-20 12:59:53 +0200899CC=cc
900check_cc
901
Martin Roth88463822016-01-07 11:03:36 -0700902if [ "$HALT_FOR_TOOLS" -ne 0 ]; then
903 exit 1
904fi
905
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700906# This initial cleanup is useful when updating the toolchain script.
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000907
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700908if [ "$clean" = "1" ]; then
909 cleanup
910fi
Stefan Reinauer074d9132009-09-26 16:43:17 +0000911
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700912# Set up host compiler and flags needed for various OSes
Stefan Reinauer074d9132009-09-26 16:43:17 +0000913
Patrick Georgif2c15f52015-06-11 21:07:31 +0200914if is_package_enabled "GCC"; then
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700915if [ $UNAME = "Darwin" ]; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000916 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
917
918 # generally the OS X compiler can create x64 binaries.
919 # Per default it generated i386 binaries in 10.5 and x64
920 # binaries in 10.6 (even if the kernel is 32bit)
921 # For some weird reason, 10.5 autodetects an ABI=64 though
922 # so we're setting the ABI explicitly here.
Stefan Reinauerd7649122015-06-09 11:44:25 -0700923 if [ $(sysctl -n hw.optional.x86_64 2>/dev/null) -eq 1 ] 2>/dev/null; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000924 OPTIONS="ABI=64"
925 else
926 OPTIONS="ABI=32"
927 fi
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700928
Stefan Reinauer4266b922012-12-05 16:18:32 -0800929 # In Xcode 4.5.2 the default compiler is clang.
930 # However, this compiler fails to compile gcc 4.7.x. As a
931 # workaround it's possible to compile gcc with llvm-gcc.
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700932 if $CC -v 2>&1 | grep -q LLVM; then
Stefan Reinauer4266b922012-12-05 16:18:32 -0800933 CC=llvm-gcc
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700934 fi
zbao11f1d312015-08-05 23:15:54 -0400935elif [ $UNAME = "Linux" -o $UNAME = "Cygwin" ]; then
Patrick Georgiddb8f802015-07-04 17:45:54 +0200936 # gmp is overeager with detecting 64bit CPUs even if they run
937 # a 32bit kernel and userland.
938 if [ "$(uname -m 2>/dev/null)" = "i686" ]; then
939 OPTIONS="ABI=32"
940 fi
Nico Huberde45c592016-01-20 23:22:33 +0100941elif [ $UNAME = "NetBSD" ]; then
942 # same for NetBSD but this one reports an i386
943 if [ "$(uname -m 2>/dev/null)" = "i386" ]; then
944 OPTIONS="ABI=32"
945 fi
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000946fi
Nico Huber66c2c1a2016-09-20 13:09:29 +0200947if [ -z "${LANGUAGES}" ]; then
948 if hostcc_has_gnat1 && \
949 [ "$(hostcc_version)" = "$(buildcc_version)" -o "${BOOTSTRAP}" = "1" ];
950 then
951 printf "\nFound compatible Ada compiler, enabling Ada support by default.\n\n"
952 LANGUAGES="ada,${DEFAULT_LANGUAGES}"
953 else
954 LANGUAGES="${DEFAULT_LANGUAGES}"
955 fi
956fi
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700957fi # GCC
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000958
Nico Huber5f72f962016-01-31 23:05:19 +0100959export HOSTCFLAGS="-Os"
960if have_hostcflags_from_gmp; then
961 set_hostcflags_from_gmp
962fi
963
Patrick Georgic1a75b12011-11-04 21:37:14 +0100964if [ "$USECCACHE" = 1 ]; then
965 CC="ccache $CC"
966fi
967
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700968# Prepare target directory for building GCC
969# (dependencies must be in the PATH)
Stefan Reinauer16bd7892012-12-07 23:57:01 +0100970mkdir -p $DESTDIR$TARGETDIR/bin
Stefan Reinauer88352d72016-04-06 15:57:03 -0700971mkdir -p $DESTDIR$TARGETDIR/share
Stefan Reinauer16bd7892012-12-07 23:57:01 +0100972export PATH=$DESTDIR$TARGETDIR/bin:$PATH
973
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700974# Download, unpack, patch and build all packages
975
Martin Roth03625172016-02-17 14:44:14 -0700976printf "Downloading tarballs ... \n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700977mkdir -p tarballs
978for P in $PACKAGES; do
979 download $P
980done
Martin Roth03625172016-02-17 14:44:14 -0700981printf "Downloaded tarballs ... ${green}ok${NC}\n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700982
983printf "Unpacking and patching ... \n"
984for P in $PACKAGES; do
985 unpack_and_patch $P || exit 1
986done
987printf "Unpacked and patched ... ${green}ok${NC}\n"
988
989printf "Building packages ... \n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700990for package in $PACKAGES; do
991 build $package
992done
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700993printf "Packages built ... ${green}ok${NC}\n"
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700994
Stefan Reinauer699ac272015-06-05 12:43:28 -0700995# Adding git information of current tree to target directory
996# for reproducibility
Stefan Reinauerd7649122015-06-09 11:44:25 -0700997PROGNAME=$(basename "$0")
Stefan Reinauer88352d72016-04-06 15:57:03 -0700998rm -f "$DESTDIR$TARGETDIR/share/$PROGNAME-*"
999cp "$PROGNAME" "$DESTDIR$TARGETDIR/share/$PROGNAME-$CROSSGCC_VERSION-$CROSGCC_COMMIT"
Zheng Bao30b895f2013-02-06 18:04:40 +08001000
Stefan Reinauer88e83e52016-04-06 15:39:48 -07001001cleanup
Stefan Reinauer074d9132009-09-26 16:43:17 +00001002
Stefan Reinauer699ac272015-06-05 12:43:28 -07001003printf "\n${green}You can now run your $NAME toolchain from $TARGETDIR.${NC}\n"