blob: 2907c098b9b61763bad1c849c18af93576c6e8c9 [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 Huber32c4a062016-01-25 18:30:57 +010029LANGUAGES=c
Stefan Reinauer074d9132009-09-26 16:43:17 +000030DESTDIR=
Stefan Reinauer85b07d62015-06-09 14:45:14 -070031SAVETEMPS=0
32SKIPPYTHON=1
Nico Huber234d2462016-01-26 16:10:17 +010033BOOTSTRAP=0
Stefan Reinauer074d9132009-09-26 16:43:17 +000034
Stefan Reinauer699ac272015-06-05 12:43:28 -070035# GCC toolchain version numbers
Timothy Pearson96310162015-12-28 22:53:29 -060036GMP_VERSION=6.1.0
Iru Caiae8e3a02016-04-02 10:50:59 +080037MPFR_VERSION=3.1.4
Paul Menzele6619422015-02-23 10:33:59 +010038MPC_VERSION=1.0.3
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000039LIBELF_VERSION=0.8.13
Iru Caiae8e3a02016-04-02 10:50:59 +080040GCC_VERSION=5.3.0
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070041GCC_AUTOCONF_VERSION=2.69
Martin Rothbcfa7cc2016-07-14 13:01:37 -060042BINUTILS_VERSION=2.26.1
Stefan Reinauer498f3d02016-04-21 18:10:15 -070043GDB_VERSION=7.11
Martin Roth2ab981b2016-08-31 16:43:49 -060044IASL_VERSION=20160831
Stefan Reinauer3dc5d072016-04-21 19:29:03 -070045PYTHON_VERSION=3.5.1
Stefan Reinauer498f3d02016-04-21 18:10:15 -070046EXPAT_VERSION=2.1.1
Stefan Reinauer14ce2132015-06-05 13:01:13 -070047# CLANG version number
Iru Caiae8e3a02016-04-02 10:50:59 +080048CLANG_VERSION=3.8.0
Martin Roth589ef9d2016-08-11 18:35:06 -060049MAKE_VERSION=4.2.1
Stefan Reinauer074d9132009-09-26 16:43:17 +000050
Stefan Reinauer699ac272015-06-05 12:43:28 -070051# GCC toolchain archive locations
Martin Roth1484c032016-03-08 09:37:14 -070052# These are sanitized by the jenkins toolchain test builder, so if
53# a completely new URL is added here, it probably needs to be added
54# to the jenkins build as well, or the builder won't download it.
Stefan Reinauerd9d21022016-04-21 18:16:30 -070055GMP_ARCHIVE="http://ftpmirror.gnu.org/gmp/gmp-${GMP_VERSION}.tar.xz"
56MPFR_ARCHIVE="http://ftpmirror.gnu.org/mpfr/mpfr-${MPFR_VERSION}.tar.xz"
Idwer Vollering7962fc72012-10-25 02:14:09 +020057MPC_ARCHIVE="http://ftpmirror.gnu.org/mpc/mpc-${MPC_VERSION}.tar.gz"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000058LIBELF_ARCHIVE="http://www.mr511.de/software/libelf-${LIBELF_VERSION}.tar.gz"
Idwer Vollering1cfee0b2012-10-25 02:03:53 +020059GCC_ARCHIVE="http://ftpmirror.gnu.org/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.bz2"
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070060BINUTILS_ARCHIVE="http://ftpmirror.gnu.org/binutils/binutils-${BINUTILS_VERSION}.tar.bz2"
Patrick Georgi53c388f2015-03-07 09:55:18 +010061GDB_ARCHIVE="http://ftpmirror.gnu.org/gdb/gdb-${GDB_VERSION}.tar.xz"
Patrick Georgi85bb9462016-02-11 17:22:16 +010062IASL_ARCHIVE="https://acpica.org/sites/acpica/files/acpica-unix2-${IASL_VERSION}.tar.gz"
Patrick Georgi53c388f2015-03-07 09:55:18 +010063PYTHON_ARCHIVE="http://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz"
Stefan Reinauer498f3d02016-04-21 18:10:15 -070064EXPAT_ARCHIVE="http://downloads.sourceforge.net/sourceforge/expat/expat-${EXPAT_VERSION}.tar.bz2"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070065# CLANG toolchain archive locations
66LLVM_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/llvm-${CLANG_VERSION}.src.tar.xz"
67CFE_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/cfe-${CLANG_VERSION}.src.tar.xz"
68CRT_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/compiler-rt-${CLANG_VERSION}.src.tar.xz"
69CTE_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/clang-tools-extra-${CLANG_VERSION}.src.tar.xz"
Stefan Reinauera851bbb2016-01-29 18:03:26 -080070MAKE_ARCHIVE="http://ftpmirror.gnu.org/make/make-${MAKE_VERSION}.tar.bz2"
Stefan Reinauer074d9132009-09-26 16:43:17 +000071
Martin Roth58c68d52016-03-01 15:22:42 -070072ALL_ARCHIVES="$GMP_ARCHIVE $MPFR_ARCHIVE $MPC_ARCHIVE $LIBELF_ARCHIVE \
73 $GCC_ARCHIVE $BINUTILS_ARCHIVE $GDB_ARCHIVE $IASL_ARCHIVE \
74 $PYTHON_ARCHIVE $EXPAT_ARCHIVE $LLVM_ARCHIVE $CFE_ARCHIVE \
75 $CRT_ARCHIVE $CTE_ARCHIVE $MAKE_ARCHIVE"
76
Stefan Reinauer699ac272015-06-05 12:43:28 -070077# GCC toolchain directories
Stefan Reinauer074d9132009-09-26 16:43:17 +000078GMP_DIR="gmp-${GMP_VERSION}"
79MPFR_DIR="mpfr-${MPFR_VERSION}"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000080MPC_DIR="mpc-${MPC_VERSION}"
81LIBELF_DIR="libelf-${LIBELF_VERSION}"
Stefan Reinauer074d9132009-09-26 16:43:17 +000082GCC_DIR="gcc-${GCC_VERSION}"
83BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
84GDB_DIR="gdb-${GDB_VERSION}"
Patrick Georgi85bb9462016-02-11 17:22:16 +010085IASL_DIR="acpica-unix2-${IASL_VERSION}"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +010086PYTHON_DIR="Python-${PYTHON_VERSION}"
87EXPAT_DIR="expat-${EXPAT_VERSION}"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070088# CLANG toolchain directories
89LLVM_DIR="llvm-${CLANG_VERSION}.src"
90CFE_DIR="cfe-${CLANG_VERSION}.src"
91CRT_DIR="compiler-rt-${CLANG_VERSION}.src"
92CTE_DIR="clang-tools-extra-${CLANG_VERSION}.src"
Stefan Reinauera851bbb2016-01-29 18:03:26 -080093MAKE_DIR="make-${MAKE_VERSION}"
Stefan Reinauer074d9132009-09-26 16:43:17 +000094
Patrick Georgi6321f522015-02-27 23:36:26 +010095unset MAKELEVEL MAKEFLAGS
96
Patrick Georgi1861ff72011-10-31 12:15:55 +010097red='\033[0;31m'
98RED='\033[1;31m'
99green='\033[0;32m'
100GREEN='\033[1;32m'
101blue='\033[0;34m'
102BLUE='\033[1;34m'
103cyan='\033[0;36m'
104CYAN='\033[1;36m'
105NC='\033[0m' # No Color
Stefan Reinauer074d9132009-09-26 16:43:17 +0000106
zbaoe05c1ea2015-08-05 23:10:05 -0400107UNAME=$(uname | grep -iq cygwin && echo Cygwin || uname)
Martin Roth88463822016-01-07 11:03:36 -0700108HALT_FOR_TOOLS=0
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700109
Patrick Georgi198d23c2012-12-08 08:02:44 +0100110normalize_dirs()
111{
112 mkdir -p $DESTDIR$TARGETDIR/lib
113 test -d $DESTDIR$TARGETDIR/lib32 && mv $DESTDIR$TARGETDIR/lib32/* $DESTDIR$TARGETDIR/lib
114 test -d $DESTDIR$TARGETDIR/lib64 && mv $DESTDIR$TARGETDIR/lib64/* $DESTDIR$TARGETDIR/lib
115 rmdir -p $DESTDIR$TARGETDIR/lib32 $DESTDIR$TARGETDIR/lib64
116
117 perl -pi -e "s,/lib32,/lib," $DESTDIR$TARGETDIR/lib/*.la
118 perl -pi -e "s,/lib64,/lib," $DESTDIR$TARGETDIR/lib/*.la
119}
120
Stefan Reinauer93a96302015-06-15 12:36:53 -0700121please_install()
122{
Martin Roth88463822016-01-07 11:03:36 -0700123 HALT_FOR_TOOLS=1
Patrick Georgi5da95dc2015-07-17 23:33:05 +0200124 test -r /etc/os-release && . /etc/os-release
Stefan Reinauer93a96302015-06-15 12:36:53 -0700125 case "$ID_LIKE" in
126 debian) solution="sudo apt-get install $1" ;;
127 suse) solution="sudo zypper install $1" ;;
128 *) solution="using your OS packaging system" ;;
129 esac
130
Martin Roth591790f2016-03-20 20:39:04 -0600131 printf "${RED}ERROR:${red} Missing tool: Please install \'$1\'. (eg $solution)${NC}\n" >&2
Martin Rothe3963172016-01-06 13:54:32 -0700132 if [ -n "$2" ]; then
Martin Roth591790f2016-03-20 20:39:04 -0600133 printf "${RED}ERROR:${red} or install \'$2\'.${NC}\n" >&2
Martin Rothe3963172016-01-06 13:54:32 -0700134 fi
Stefan Reinauer93a96302015-06-15 12:36:53 -0700135}
136
Patrick Georgib7062882015-02-24 10:52:14 +0100137searchtool()
Stefan Reinauer074d9132009-09-26 16:43:17 +0000138{
139 # $1 short name
Patrick Georgib7062882015-02-24 10:52:14 +0100140 # $2 search string
141 # $3 soft fail if set
Martin Rothe3963172016-01-06 13:54:32 -0700142 # $4 alternative package to install on failure
Patrick Georgib7062882015-02-24 10:52:14 +0100143 # result: file name of that tool on stdout
144 # or no output if nothing suitable was found
145 search=GNU
146 if [ -n "$2" ]; then
147 search="$2"
148 fi
Stefan Reinauer074d9132009-09-26 16:43:17 +0000149 for i in "$1" "g$1" "gnu$1"; do
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700150 if [ -x "$(which $i 2>/dev/null)" ]; then
151 if [ "$(cat /dev/null | $i --version 2>&1 | grep -c "$search")" \
152 -gt 0 ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000153 echo $i
154 return
155 fi
156 fi
157 done
Andrew Wub67e9a12014-04-28 18:13:44 +0800158 # A workaround for OSX 10.9 and some BSDs, whose nongnu
159 # patch and tar also work.
Patrick Georgi7cb26b42015-04-28 21:29:22 +0200160 if [ $UNAME = "Darwin" -o $UNAME = "FreeBSD" -o $UNAME = "NetBSD" -o $UNAME = "OpenBSD" ]; then
Patrick Georgib7062882015-02-24 10:52:14 +0100161 if [ "$1" = "patch" -o "$1" = "tar" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700162 if [ -x "$(which $1 2>/dev/null)" ]; then
Zheng Bao36156ff2012-09-28 16:18:58 +0800163 echo $1
164 return
165 fi
166 fi
167 fi
zbao5cf758d2015-09-01 22:28:57 -0400168 if echo $1 | grep -q "sum" ; then
169 algor=$(echo $1 | sed -e 's,sum,,')
170 if [ -x "$(which $1 2>/dev/null)" ]; then
171 #xxxsum [file]
172 echo $1
173 return
174 elif [ -x "$(which $algor 2>/dev/null)" ]; then
175 #xxx [file]
176 echo $algor
177 return
178 elif [ -x "$(which openssl 2>/dev/null)" ]; then
179 #openssl xxx [file]
180 echo openssl $algor
181 return
182 elif [ -x "$(which cksum 2>/dev/null)" ]; then
183 #cksum -a xxx [file]
184 #cksum has special options in NetBSD. Actually, NetBSD will use the second case above.
185 echo "buildgcc" | cksum -a $algor > /dev/null 2>/dev/null && \
186 echo cksum -a $algor
187 return
zbao939dc842015-04-30 04:44:07 +0800188 fi
189 fi
Martin Rothe3963172016-01-06 13:54:32 -0700190
Martin Roth88463822016-01-07 11:03:36 -0700191 [ -z "$3" ] && please_install $1 $4
Patrick Georgib7062882015-02-24 10:52:14 +0100192 false
Stefan Reinauer074d9132009-09-26 16:43:17 +0000193}
194
Martin Roth591790f2016-03-20 20:39:04 -0600195# Run a compile check of the specified library option to see if it's installed
196check_for_library() {
197 local LIBRARY_FLAGS=$1
Aaron Durbin0ac4e5a2016-04-07 16:56:25 -0500198 local LIBRARY_PACKAGES="$2"
Martin Roth591790f2016-03-20 20:39:04 -0600199 local LIBTEST_FILE=.libtest
200
201 echo "int main(int argc, char **argv) { (void) argc; (void) argv; return 0; }" > "${LIBTEST_FILE}.c"
202
203 cc $CFLAGS $LIBRARY_FLAGS "${LIBTEST_FILE}.c" -o "${LIBTEST_FILE}" >/dev/null 2>&1 || \
204 please_install "$LIBRARY_PACKAGES"
205 rm -rf "${LIBTEST_FILE}.c" "${LIBTEST_FILE}"
206}
207
Nico Huberaee7f462016-09-20 12:55:19 +0200208buildcc_major() {
209 echo "${GCC_VERSION}" | cut -d. -f1
210}
211
212buildcc_version() {
213 echo "${GCC_VERSION}" | cut -d. -f1-2
214}
215
216hostcc_major() {
217 (echo __GNUC__ | ${CC} -E - 2>/dev/null || echo 0) | tail -1
218}
219
220hostcc_minor() {
221 (echo __GNUC_MINOR__ | ${CC} -E - 2>/dev/null || echo 0) | tail -1
222}
223
224hostcc_version() {
225 printf "%d.%d" "$(hostcc_major)" "$(hostcc_minor)"
226}
227
Nico Huber156d87c2016-09-20 12:59:53 +0200228check_cc() {
229 if is_package_enabled "GCC"; then
230 if [ "$(hostcc_major)" != "$(buildcc_major)" -a "${BOOTSTRAP}" != "1" ]; then
231 printf "\n${red}warning: Building GCC $(buildcc_version) with a different major "
232 printf "version ($(hostcc_version)).\n"
233 printf " Bootstrapping (-b) is recommended.${NC}\n\n"
234 fi
235 fi
236}
237
zbao06e85ac2015-08-24 22:08:36 -0400238check_sum() {
239 test -z "$CHECKSUM" || \
240 test "$(cat sum/$1.cksum 2>/dev/null | sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@')" = \
241 "$($CHECKSUM tarballs/$1 2>/dev/null | sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@')"
242}
243
244compute_sum() {
245 test ! -f sum/$1.cksum && test -f tarballs/$1 && \
246 (test -z "$CHECKSUM" || $CHECKSUM tarballs/$1 > sum/$1.cksum ) && \
247 printf "(checksum created. ${RED}Note. Please upload sum/$1.cksum if the corresponding archive is upgraded.)${NC}"
248}
249
zbao282dd952015-09-06 05:00:04 -0400250download_showing_percentage() {
251 url=$1
252 printf " ..${red} 0%%"
253 wget --no-check-certificate $url 2>&1 | while read line; do
zbao282dd952015-09-06 05:00:04 -0400254 echo $line | grep -o "[0-9]\+%" | awk '{printf("\b\b\b\b%4s", $1)}'
zbao282dd952015-09-06 05:00:04 -0400255 done
Zheng Baobb003c82016-08-05 13:41:51 +0800256 printf "${NC}"
zbao282dd952015-09-06 05:00:04 -0400257}
258
Stefan Reinauer88935482015-06-05 09:51:10 -0700259download() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700260 package=$1
261 archive="$(eval echo \$$package"_ARCHIVE")"
Stefan Reinauer699ac272015-06-05 12:43:28 -0700262
Stefan Reinauerd7649122015-06-09 11:44:25 -0700263 FILE=$(basename $archive)
Stefan Reinauer88935482015-06-05 09:51:10 -0700264 printf " * $FILE "
265
zbaod22b9ea2015-08-30 23:35:16 -0400266 if test -f tarballs/$FILE && check_sum $FILE ; then
267 printf "(cached)"
268 else
Paul Menzelb2ba3952015-06-12 11:58:14 +0200269 printf "(downloading from $archive)"
Stefan Reinauer88935482015-06-05 09:51:10 -0700270 rm -f tarballs/$FILE
271 cd tarballs
zbao282dd952015-09-06 05:00:04 -0400272 download_showing_percentage $archive
Stefan Reinauer88935482015-06-05 09:51:10 -0700273 cd ..
zbao06e85ac2015-08-24 22:08:36 -0400274 compute_sum $FILE
zbaod22b9ea2015-08-30 23:35:16 -0400275 fi
276
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700277 if [ ! -f tarballs/$FILE ]; then
zbao282dd952015-09-06 05:00:04 -0400278 printf "\n${RED}Failed to download $FILE.${NC}\n"
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700279 exit 1
280 fi
Stefan Reinauer88935482015-06-05 09:51:10 -0700281 printf "\n"
282}
283
284unpack_and_patch() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700285 package=$1
286 archive="$(eval echo \$$package"_ARCHIVE")"
287 dir="$(eval echo \$$package"_DIR")"
Stefan Reinauer88935482015-06-05 09:51:10 -0700288 test -d ${dir} && test -f ${dir}/.unpack_success || (
Stefan Reinauerd7649122015-06-09 11:44:25 -0700289 printf " * $(basename $archive)\n"
Stefan Reinauer88935482015-06-05 09:51:10 -0700290 FLAGS=zxf
Stefan Reinauerd7649122015-06-09 11:44:25 -0700291 suffix=$(echo $archive | sed 's,.*\.,,')
Martin Roth95f7b222016-03-20 12:38:48 -0600292 if [ "$suffix" = "gz" ] && [ -n "$PIGZ" ]; then FLAGS="-I pigz -xf"
293 elif [ "$suffix" = "gz" ]; then FLAGS=zxf
294 elif [ "$suffix" = "bz2" ] && [ -n "$LBZIP2" ]; then FLAGS="-I lbzip2 -xf"
295 elif [ "$suffix" = "bz2" ]; then FLAGS=jxf
296 elif [ "$suffix" = "xz" ]; then FLAGS="--xz -xf"
297 elif [ "$suffix" = "lzma" ]; then FLAGS="--lzma -xf"
298 fi
Stefan Reinauerd7649122015-06-09 11:44:25 -0700299 $TAR $FLAGS tarballs/$(basename $archive)
Stefan Reinauer88935482015-06-05 09:51:10 -0700300 for patch in patches/${dir}_*.patch; do
301 test -r $patch || continue
Stefan Reinauerd7649122015-06-09 11:44:25 -0700302 printf " o $(basename $patch)\n"
Nico Huber4b48ed82016-07-27 00:28:03 +0200303 (cd ${dir} && $PATCH -s -N -p1 <../${patch}) || {
Stefan Reinauer88935482015-06-05 09:51:10 -0700304 printf "\n${RED}Failed $patch.${NC}\n"
Stefan Reinauer916b8452015-06-09 11:54:49 -0700305 exit 1
306 }
Stefan Reinauer88935482015-06-05 09:51:10 -0700307 done
308 touch ${dir}/.unpack_success
309 )
310}
311
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700312fn_exists()
313{
Patrick Georgic8665912015-06-11 21:04:31 +0200314 type $1 >/dev/null 2>&1
Patrick Georgi74c06422015-03-25 18:40:13 +0100315}
316
Patrick Georgif2c15f52015-06-11 21:07:31 +0200317is_package_enabled()
318{
319 echo "$PACKAGES" |grep -q "\<$1\>"
320}
321
Martin Roth444ece22016-04-01 18:46:29 -0600322package_uses_targetarch()
323{
Stefan Reinauera344b682016-04-21 19:29:51 -0700324 if [ "$1" = "GCC" ] || [ "$1" = "GDB" ] || [ "$1" = "BINUTILS" ]; then
Martin Roth444ece22016-04-01 18:46:29 -0600325 true
326 else
327 false
328 fi
329}
330
Nico Huber11ea2b32016-01-26 16:09:31 +0100331generic_build()
332{
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700333 package=$1
Nico Huber11ea2b32016-01-26 16:09:31 +0100334 host_target=$2
335 builddir=$3
336 success=$4
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700337
338 fn_exists build_$package || return
339
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700340 version="$(eval echo \$$package"_VERSION")"
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700341
Nico Huber11ea2b32016-01-26 16:09:31 +0100342 mkdir -p "$builddir"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700343
Nico Huber11ea2b32016-01-26 16:09:31 +0100344 if [ -f "$success" ]; then
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700345 printf "Skipping $package as it is already built\n"
346 else
347 printf "Building $package $version ... "
Nico Huber11ea2b32016-01-26 16:09:31 +0100348 DIR="$PWD"
349 cd "$builddir"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700350 rm -f .failed
Nico Huber11ea2b32016-01-26 16:09:31 +0100351 build_${package} $host_target > build.log 2>&1
352 cd "$DIR"
353 if [ ! -f "$builddir/.failed" ]; then
354 touch "$success";
355 else
356 printf "${RED}failed${NC}. Check $builddir/build.log.\n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700357 exit 1
358 fi
359 printf "${green}ok${NC}\n"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700360 fi
361}
362
Nico Huber11ea2b32016-01-26 16:09:31 +0100363build_for_host()
364{
365 generic_build $1 host build-$1 "${TARGETDIR}/.$1.success"
366}
367
368build_for_target()
369{
370 generic_build $1 target build-${TARGETARCH}-$1 "${TARGETDIR}/.${TARGETARCH}-$1.success"
371}
372
373build()
374{
375 if package_uses_targetarch $1; then
Nico Huber234d2462016-01-26 16:10:17 +0100376 if [ $BOOTSTRAP -eq 1 -a ! -f "${TARGETDIR}/.GCC.success" ]; then
377 build_for_host GCC
378 fi
Nico Huber11ea2b32016-01-26 16:09:31 +0100379 build_for_target $1
380 else
381 build_for_host $1
382 fi
383}
384
Zheng Baobb003c82016-08-05 13:41:51 +0800385quit()
386{
387 printf "${NC}Stop\n"
388 exit 1
389}
390
Stefan Reinauer074d9132009-09-26 16:43:17 +0000391cleanup()
392{
Stefan Reinauer88e83e52016-04-06 15:39:48 -0700393 if [ $SAVETEMPS -ne 0 ]; then
394 printf "Leaving temporary files around... ${green}ok${NC}\n"
395 return
396 fi
397
Stefan Reinauer074d9132009-09-26 16:43:17 +0000398 printf "Cleaning up temporary files... "
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700399 for package in $PACKAGES; do
Martin Roth444ece22016-04-01 18:46:29 -0600400 rm -rf build-${TARGETARCH}-$package build-$package $(eval echo \$$package"_DIR")
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700401 done
zbaoff3f15c2015-08-26 22:00:31 -0400402 rm -f getopt
Stefan Reinauer074d9132009-09-26 16:43:17 +0000403 printf "${green}ok${NC}\n"
404}
405
406myhelp()
407{
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700408 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>] [-C] [-G] [-S]\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000409 printf " $0 [-V|--version]\n"
410 printf " $0 [-h|--help]\n\n"
411
412 printf "Options:\n"
413 printf " [-V|--version] print version number and exit\n"
414 printf " [-h|--help] print this help and exit\n"
415 printf " [-c|--clean] remove temporary files before build\n"
416 printf " [-t|--savetemps] don't remove temporary files after build\n"
Patrick Georgic1a75b12011-11-04 21:37:14 +0100417 printf " [-y|--ccache] Use ccache when building cross compiler\n"
Nico Huber152e78e2016-09-20 15:38:40 +0200418 printf " [-n|--nocolor] don't print color codes in output\n"
419 printf " [-u|--urls] print the urls for all packages\n"
Patrick Georgi73166c72010-04-14 20:42:42 +0000420 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
Martin Roth588c79d2016-03-20 12:03:20 -0600421 printf " [-s]--supported <tool> print supported version of a tool\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000422 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
Martin Roth588c79d2016-03-20 12:03:20 -0600423 printf " (defaults to $TARGETDIR)\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000424 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100425 printf " (for RPM builds, default unset)\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700426 printf " [-P|--package <package>] Build a specific package: GCC, CLANG, IASL, GDB\n"
427 printf " (defaults to $PACKAGE)\n"
428 printf "GCC specific options:\n"
Nico Huber234d2462016-01-26 16:10:17 +0100429 printf " [-b|--bootstrap] bootstrap the host compiler before building\n"
430 printf " the cross compiler\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700431 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
432 printf " (defaults to $TARGETARCH)\n"
Nico Huberbb313bf2015-09-08 12:30:27 +0200433 printf " [-l|--languages <languages>] comma separated list of target languages\n"
434 printf " (defaults to $LANGUAGES)\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700435 printf "GDB specific options:\n"
436 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
437 printf " (defaults to $TARGETARCH)\n"
438 printf " [-S|--scripting] build scripting support for GDB\n\n"
Martin Rothd1da74f2015-06-21 12:06:28 -0600439 printf "Platforms for GCC & GDB:\n"
Martin Roth3e0f74d2016-03-08 12:07:04 -0700440 printf " x86_64 i386-elf i386-mingw32 mipsel-elf riscv-elf arm aarch64\n"
Jonathan Neuschäferf14f6402016-03-11 20:22:23 +0100441 printf " powerpc64le-linux-gnu nds32le-elf\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000442}
443
Martin Rothedf965a2015-11-25 16:58:33 -0700444printversion() {
445 printf "${blue}Welcome to the ${red}coreboot${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
446}
447
Stefan Reinauer074d9132009-09-26 16:43:17 +0000448myversion()
449{
Martin Rothedf965a2015-11-25 16:58:33 -0700450 printversion
Stefan Reinauer074d9132009-09-26 16:43:17 +0000451
452 cat << EOF
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000453Copyright (C) 2008-2010 by coresystems GmbH
Marc Jones2aac3f62011-08-08 16:07:50 -0600454Copyright (C) 2011 by Sage Electronic Engineering
Stefan Reinauer074d9132009-09-26 16:43:17 +0000455
456This program is free software; you can redistribute it and/or modify
457it under the terms of the GNU General Public License as published by
458the Free Software Foundation; version 2 of the License.
459
460This program is distributed in the hope that it will be useful,
461but WITHOUT ANY WARRANTY; without even the implied warranty of
462MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
463GNU General Public License for more details.
464
465EOF
466}
467
Nico Huber5f72f962016-01-31 23:05:19 +0100468have_hostcflags_from_gmp() {
469 grep -q __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h >/dev/null 2>&1
470}
471
472set_hostcflags_from_gmp() {
473 # Now set CFLAGS to match GMP CFLAGS but strip out -pedantic
474 # as GCC 4.6.x fails if it's there.
Idwer Volleringba349ab2016-07-24 02:10:19 +0200475 export HOSTCFLAGS="$(grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2 |\
476 sed s,-pedantic,,)"
Nico Huber5f72f962016-01-31 23:05:19 +0100477}
478
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700479build_GMP() {
Paul Kocialkowski7899db22016-06-17 21:41:00 -0700480 CC="$CC" ../${GMP_DIR}/configure --disable-shared --enable-fat \
Nico Huber5f72f962016-01-31 23:05:19 +0100481 --prefix=$TARGETDIR $OPTIONS \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700482 || touch .failed
483 $MAKE $JOBS || touch .failed
484 $MAKE install DESTDIR=$DESTDIR || touch .failed
485
486 normalize_dirs
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700487
Nico Huber5f72f962016-01-31 23:05:19 +0100488 set_hostcflags_from_gmp
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700489}
490
491build_MPFR() {
492 test $UNAME = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
493 CC="$CC" ../${MPFR_DIR}/configure --disable-shared --prefix=$TARGETDIR \
494 --infodir=$TARGETDIR/info \
495 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || \
496 touch .failed
497 $MAKE $JOBS || touch .failed
498 $MAKE install DESTDIR=$DESTDIR || touch .failed
499
500 normalize_dirs
501
502 # work around build problem of libgmp.la
503 if [ "$DESTDIR" != "" ]; then
504 perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
505 fi
506}
507
508build_MPC() {
509 CC="$CC" ../${MPC_DIR}/configure --disable-shared --prefix=$TARGETDIR \
510 --infodir=$TARGETDIR/info --with-mpfr=$DESTDIR$TARGETDIR \
511 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || \
512 touch .failed
513 $MAKE $JOBS || touch .failed
514 $MAKE install DESTDIR=$DESTDIR || touch .failed
515
516 normalize_dirs
517}
518
519build_LIBELF() {
520 CC="$CC" CFLAGS="$HOSTCFLAGS" libelf_cv_elf_h_works=no \
521 ../${LIBELF_DIR}/configure --disable-shared --prefix=$TARGETDIR \
522 --infodir=$TARGETDIR/info CFLAGS="$HOSTCFLAGS" || touch .failed
523 $MAKE $JOBS || touch .failed
524 $MAKE install prefix=$DESTDIR$TARGETDIR || touch .failed
525
526 normalize_dirs
527}
528
529build_BINUTILS() {
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700530 if [ $TARGETARCH == "x86_64-elf" ]; then
531 ADDITIONALTARGET=",i386-elf"
532 fi
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700533 CC="$CC" ../binutils-${BINUTILS_VERSION}/configure --prefix=$TARGETDIR \
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700534 --target=${TARGETARCH} --enable-targets=${TARGETARCH}${ADDITIONALTARGET} \
535 --disable-werror --disable-nls --enable-lto --enable-gold \
Stefan Reinauer2fe2d3d2016-04-05 02:14:48 -0700536 --enable-interwork --enable-multilib \
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700537 --enable-plugins --enable-multilibs CFLAGS="$HOSTCFLAGS" || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700538 $MAKE $JOBS || touch .failed
539 $MAKE install DESTDIR=$DESTDIR || touch .failed
540}
541
Nico Huber234d2462016-01-26 16:10:17 +0100542bootstrap_GCC() {
543 CC="$CC" \
544 CFLAGS="$HOSTCFLAGS" \
545 CFLAGS_FOR_BUILD="$HOSTCFLAGS" \
546 CFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
547 CXXFLAGS="$HOSTCFLAGS" \
548 CXXFLAGS_FOR_BUILD="$HOSTCFLAGS" \
549 CXXFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
550 ../gcc-${GCC_VERSION}/configure \
551 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
552 --enable-bootstrap \
553 --disable-werror --disable-nls \
554 --disable-shared --disable-multilib \
555 --disable-libssp --disable-libquadmath --disable-libcc1 \
556 ${GCC_OPTIONS} --enable-languages="${LANGUAGES}" \
557 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
558 --with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
559 --with-pkgversion="coreboot bootstrap v$CROSSGCC_VERSION $CROSSGCC_DATE" \
560 && \
561 $MAKE $JOBS BOOT_CFLAGS="$HOSTCFLAGS" BUILD_CONFIG="" bootstrap && \
562 $MAKE install-gcc \
563 install-target-libgcc \
564 maybe-install-target-libada \
565 maybe-install-target-libstdc++-v3 \
566 DESTDIR=$DESTDIR && \
567 ln -s gcc $DESTDIR$TARGETDIR/bin/cc || touch .failed
568}
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700569
Nico Huber234d2462016-01-26 16:10:17 +0100570build_cross_GCC() {
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700571 # Work around crazy code generator in GCC that confuses CLANG.
Aaron Durbinb2229dc2016-04-07 16:58:10 -0500572 $CC --version | grep clang >/dev/null 2>&1 && \
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700573 HOSTCFLAGS="$HOSTCFLAGS -fbracket-depth=1024"
574
Stefan Reinauerd0f83722015-06-09 11:52:28 -0700575 # GCC does not honor HOSTCFLAGS at all. CFLAGS are used for
576 # both target and host object files.
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700577 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
578 # but it does not seem to work properly. At least the host library
579 # libiberty is not compiled with CFLAGS_FOR_BUILD.
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700580 # Also set the CXX version of the flags because GCC is now compiled
581 # using C++.
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100582 CC="$CC" CFLAGS_FOR_TARGET="-O2 -Dinhibit_libc" CFLAGS="$HOSTCFLAGS" \
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700583 CFLAGS_FOR_BUILD="$HOSTCFLAGS" CXXFLAGS="$HOSTCFLAGS" \
584 CXXFLAGS_FOR_BUILD="$HOSTCFLAGS" ../gcc-${GCC_VERSION}/configure \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700585 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
586 --target=${TARGETARCH} --disable-werror --disable-shared \
Stefan Reinauer2f97ebc2015-06-09 12:24:54 -0700587 --enable-lto --enable-plugins --enable-gold --enable-ld=default \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700588 --disable-libssp --disable-bootstrap --disable-nls \
589 --disable-libquadmath --without-headers \
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100590 --disable-threads \
Patrick Georgi1fc02d12016-03-23 21:44:43 +0100591 --enable-interwork --enable-multilib --enable-targets=all \
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100592 --disable-libatomic --disable-libcc1 --disable-decimal-float \
Nico Huberbb313bf2015-09-08 12:30:27 +0200593 ${GCC_OPTIONS} --enable-languages="${LANGUAGES}" \
Ronald G. Minnich7ee16b72016-02-12 21:54:59 +0000594 --with-system-zlib \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700595 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
596 --with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
597 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE" \
598 || touch .failed
599 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-gcc || touch .failed
600 $MAKE install-gcc DESTDIR=$DESTDIR || touch .failed
601
Stefan Reinauerd7649122015-06-09 11:44:25 -0700602 if [ "$(echo $TARGETARCH | grep -c -- -mingw32)" -eq 0 ]; then
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700603 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-target-libgcc || touch .failed
604 $MAKE install-target-libgcc DESTDIR=$DESTDIR || touch .failed
605 fi
606}
607
Nico Huber234d2462016-01-26 16:10:17 +0100608build_GCC() {
609 if [ "$1" = host ]; then
610 bootstrap_GCC
611 else
612 build_cross_GCC
613 fi
614}
615
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700616build_EXPAT() {
617 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${EXPAT_DIR}/configure --disable-shared \
Stefan Reinauera344b682016-04-21 19:29:51 -0700618 --prefix=$TARGETDIR || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700619 $MAKE || touch .failed
620 $MAKE install DESTDIR=$DESTDIR || touch .failed
621
622 normalize_dirs
623}
624
625build_PYTHON() {
626 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${PYTHON_DIR}/configure --prefix=$TARGETDIR \
Stefan Reinauera344b682016-04-21 19:29:51 -0700627 || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700628 $MAKE $JOBS || touch .failed
629 $MAKE install DESTDIR=$DESTDIR || touch .failed
630
631 normalize_dirs
632}
633
634build_GDB() {
635 export PYTHONHOME=$DESTDIR$TARGETDIR
636 if [ $(uname) != "FreeBSD" -a $(uname) != "NetBSD" ]; then
637 LIBDL="-ldl"
638 fi
639 LDFLAGS="-Wl,-rpath,\$\$ORIGIN/../lib/ -L$DESTDIR$TARGETDIR/lib \
640 -lpthread $LIBDL -lutil" \
641 CC="$CC" CFLAGS="$HOSTCFLAGS -I$DESTDIR$TARGETDIR/include" \
Stefan Reinauer7b40e422015-06-09 11:56:32 -0700642 ../${GDB_DIR}/configure --prefix=$TARGETDIR \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700643 --target=${TARGETARCH} --disable-werror --disable-nls
644 $MAKE $JOBS || touch .failed
645 $MAKE install DESTDIR=$DESTDIR || touch .failed
646}
647
648build_IASL() {
649 RDIR=$PWD
650 cd ../$IASL_DIR/generate/unix
651 CFLAGS="$HOSTCFLAGS"
652 HOST="_LINUX"
653 test $UNAME = "Darwin" && HOST="_APPLE"
654 test $UNAME = "FreeBSD" && HOST="_FreeBSD"
655 test $UNAME = "Cygwin" && HOST="_CYGWIN"
Martin Roth6f656132015-12-08 11:14:10 -0700656 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 -0700657 rm -f $DESTDIR$TARGETDIR/bin/iasl || touch $RDIR/.failed
658 cp bin/iasl $DESTDIR$TARGETDIR/bin || touch $RDIR/.failed
659}
660
661build_LLVM() {
662 cd ..
663 ln -sf $PWD/$CFE_DIR $LLVM_DIR/tools/clang
664 ln -sf $PWD/$CTE_DIR $LLVM_DIR/tools/clang/tools/extra
665 ln -sf $PWD/$CRT_DIR $LLVM_DIR/projects/compiler-rt
666 cd -
667
668 $CMAKE -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$DESTDIR$TARGETDIR \
Martin Rothfb5647a2015-12-18 09:26:45 -0700669 -DCLANG_VENDOR="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE - " \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700670 -DCMAKE_BUILD_TYPE=Release ../$LLVM_DIR || touch .failed
671 $MAKE $JOBS || touch .failed
672 $MAKE install || touch .failed
673
674 cp -a ../$CFE_DIR/tools/scan-build/* $DESTDIR$TARGETDIR/bin
675 cp -a ../$CFE_DIR/tools/scan-view/* $DESTDIR$TARGETDIR/bin
Stefan Reinauere9e6e3d2015-07-07 00:20:42 +0200676
677 # create symlinks to work around broken --print-librt-file-name
678 # when used with -target.
679 cd $DESTDIR$TARGETDIR/lib/clang/${CLANG_VERSION}/lib
680 for i in */libclang_rt.builtins*.a; do
681 ln -s $i .
682 done
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700683}
684
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800685build_MAKE() {
686 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${MAKE_DIR}/configure --prefix=$TARGETDIR \
687 --disable-nls || touch .failed
688 $MAKE $JOBS || touch .failed
689 $MAKE install DESTDIR=$DESTDIR || touch .failed
690
691 normalize_dirs
692}
693
Martin Rothedf965a2015-11-25 16:58:33 -0700694print_stable() {
695 case "$PRINTSTABLE" in
696 AUTOCONF|autoconf) printf "%s\n" "$GCC_AUTOCONF_VERSION";;
697 BINUTILS|binutils) printf "%s\n" "$BINUTILS_VERSION";;
698 CLANG|clang) printf "%s\n" "$CLANG_VERSION";;
699 EXPAT|expat) printf "%s\n" "$EXPAT_VERSION";;
700 GCC|gcc) printf "%s\n" "$GCC_VERSION";;
701 GDB|gdb) printf "%s\n" "$GDB_VERSION";;
702 GMP|gmp) printf "%s\n" "$GMP_VERSION";;
703 IASL|iasl) printf "%s\n" "$IASL_VERSION";;
704 LIBELF|libelf) printf "%s\n" "$LIBELF_VERSION";;
705 MPC|mpc) printf "%s\n" "$MPC_VERSION";;
706 MPFR|mpfr) printf "%s\n" "$MPFR_VERSION";;
707 PYTHON|python) printf "%s\n" "$PYTHON_VERSION";;
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800708 MAKE|make) printf "%s\n" "$MAKE_VERSION";;
Martin Rothedf965a2015-11-25 16:58:33 -0700709 *) printf "Unknown tool %s\n" "$PRINTSTABLE";;
710 esac
711}
Stefan Reinauer074d9132009-09-26 16:43:17 +0000712
Zheng Baobb003c82016-08-05 13:41:51 +0800713trap quit 1 2 3 15
714
Patrick Georgi6bba29f2010-01-29 17:40:52 +0000715# Look if we have getopt. If not, build it.
716export PATH=$PATH:.
717getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
718
Stefan Reinauer074d9132009-09-26 16:43:17 +0000719# parse parameters.. try to find out whether we're running GNU getopt
Stefan Reinauerd7649122015-06-09 11:44:25 -0700720getoptbrand="$(getopt -V | sed -e '1!d' -e 's,^\(......\).*,\1,')"
Peter Stuge09377b72011-08-21 06:24:55 +0200721if [ "${getoptbrand}" = "getopt" ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000722 # Detected GNU getopt that supports long options.
Nico Hubercc414dd2016-09-20 13:59:38 +0200723 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 +0200724 getopt_ret=$?
Nico Hubercc414dd2016-09-20 13:59:38 +0200725 eval set -- "$args"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000726else
727 # Detected non-GNU getopt
Nico Huber152e78e2016-09-20 15:38:40 +0200728 args=$(getopt Vhcd:bp:l:P:j:D:tSys:un $*)
Nico Huber78df0bf2016-09-20 14:11:53 +0200729 getopt_ret=$?
Stefan Reinauer074d9132009-09-26 16:43:17 +0000730 set -- $args
731fi
732
Nico Huber78df0bf2016-09-20 14:11:53 +0200733if [ $getopt_ret != 0 ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000734 myhelp
735 exit 1
736fi
737
738while true ; do
739 case "$1" in
740 -V|--version) shift; myversion; exit 0;;
741 -h|--help) shift; myhelp; exit 0;;
Patrick Georgi44af57a2013-12-19 22:06:22 +0100742 -c|--clean) shift; clean=1;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000743 -t|--savetemps) shift; SAVETEMPS=1;;
744 -d|--directory) shift; TARGETDIR="$1"; shift;;
Nico Huber234d2462016-01-26 16:10:17 +0100745 -b|--bootstrap) shift; BOOTSTRAP=1;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000746 -p|--platform) shift; TARGETARCH="$1"; shift;;
Nico Huberbb313bf2015-09-08 12:30:27 +0200747 -l|--languages) shift; LANGUAGES="$1"; shift;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000748 -D|--destdir) shift; DESTDIR="$1"; shift;;
Patrick Georgi73166c72010-04-14 20:42:42 +0000749 -j|--jobs) shift; JOBS="-j $1"; shift;;
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700750 -P|--package) shift; PACKAGE="$1"; shift;;
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100751 -S|--scripting) shift; SKIPPYTHON=0;;
Patrick Georgic1a75b12011-11-04 21:37:14 +0100752 -y|--ccache) shift; USECCACHE=1;;
Martin Rothedf965a2015-11-25 16:58:33 -0700753 -s|--supported) shift; PRINTSTABLE="$1"; shift;;
Nico Huber152e78e2016-09-20 15:38:40 +0200754 -u|--urls) shift; printf "%s\n" "$ALL_ARCHIVES"; exit 0;;
755 -n|--nocolor) shift; \
Martin Roth4a53db02016-03-01 15:21:07 -0700756 unset red RED green GREEN blue BLUE cyan CYAN NC;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200757 --) shift; break;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200758 *) break;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000759 esac
760done
761
Nico Huberbd74d562016-09-20 14:16:43 +0200762if [ $# -gt 0 ]; then
763 printf "Excessive arguments: $*\n"
764 myhelp
765 exit 1
766fi
767
Martin Rothedf965a2015-11-25 16:58:33 -0700768if [ -n "$PRINTSTABLE" ]; then
769 print_stable
770 exit 0
771fi
772
773#print toolchain builder version string as the header
774printversion
775
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700776case "$TARGETARCH" in
Timothy Pearsona9f62352015-12-30 18:59:00 -0600777 x86_64-elf) ;;
778 x86_64*) TARGETARCH=x86_64-elf;;
779 i386-elf) ;;
780 i386-mingw32) ;;
781 mipsel-elf) ;;
Iru Caiae8e3a02016-04-02 10:50:59 +0800782 riscv-elf) TARGETARCH=riscv64-elf;;
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100783 powerpc64*-linux*) ;;
Timothy Pearsona9f62352015-12-30 18:59:00 -0600784 i386*) TARGETARCH=i386-elf;;
Stefan Reinauer27522ad2016-01-29 17:31:34 -0800785 arm*) TARGETARCH=arm-eabi;;
Timothy Pearsona9f62352015-12-30 18:59:00 -0600786 aarch64*) TARGETARCH=aarch64-elf;;
Martin Roth3e0f74d2016-03-08 12:07:04 -0700787 nds32le-elf) ;;
Timothy Pearsona9f62352015-12-30 18:59:00 -0600788 *) printf "${red}WARNING: Unsupported architecture $TARGETARCH.${NC}\n\n"; ;;
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700789esac
790
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700791# Figure out which packages to build
Stefan Reinauer699ac272015-06-05 12:43:28 -0700792
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700793case "$PACKAGE" in
794 GCC|gcc)
795 echo "Target architecture is now $TARGETARCH"
796 NAME="${TARGETARCH} cross GCC"
Stefan Reinauerbd8b2e32015-06-17 11:55:02 -0700797 PACKAGES="GMP MPFR MPC LIBELF BINUTILS GCC"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700798 ;;
799 GDB|gdb)
800 NAME="${TARGETARCH} cross GDB"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700801 if [ $SKIPPYTHON -eq 0 ]; then
Ronald G. Minnichfb944f42016-02-27 04:06:42 +0000802 PACKAGES="EXPAT PYTHON GDB"
803 else
804 PACKAGES="EXPAT GDB"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700805 fi
806 ;;
807 CLANG|clang)
808 NAME=clang
809 PACKAGES="LLVM CFE CRT CTE"
David Hendrickscd5cdd32015-06-15 17:03:22 -0700810 CMAKE=$(searchtool cmake "cmake") || exit $?
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700811 ;;
812 IASL|iasl)
813 NAME="IASL ACPI compiler"
814 PACKAGES=IASL
815 ;;
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800816 MAKE|make)
817 NAME="GNU Make"
818 PACKAGES=MAKE
819 ;;
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700820 *)
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800821 printf "${red}ERROR: Unsupported package $PACKAGE. (Supported packages are GCC, GDB, CLANG, IASL, MAKE)${NC}\n\n";
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700822 exit 1
823 ;;
824esac
Peter Stugeceacd772011-06-09 04:54:16 +0200825
Stefan Reinauer66e93352015-06-16 22:38:23 +0200826# Find all the required tools:
827
828TAR=$(searchtool tar) || exit $?
829PATCH=$(searchtool patch) || exit $?
830MAKE=$(searchtool make) || exit $?
831SHA1SUM=$(searchtool sha1sum)
zbao5cf758d2015-09-01 22:28:57 -0400832#SHA512SUM=$(searchtool sha512sum)
833#MD5SUM=$(searchtool md5sum)
Stefan Reinauer66e93352015-06-16 22:38:23 +0200834CHECKSUM=$SHA1SUM
Martin Roth95f7b222016-03-20 12:38:48 -0600835LBZIP2=$(searchtool lbzip2 "" nofail)
836PIGZ=$(searchtool pigz "" nofail)
Stefan Reinauer66e93352015-06-16 22:38:23 +0200837
838searchtool m4 > /dev/null
839searchtool bison > /dev/null
840searchtool flex flex > /dev/null
841searchtool g++ "Free Software Foundation" nofail > /dev/null || \
Marc Jones5ee69512015-06-17 15:38:17 -0600842searchtool clang "clang version" nofail > /dev/null || \
Martin Rothe3963172016-01-06 13:54:32 -0700843searchtool clang "LLVM" "" "g++" > /dev/null
Stefan Reinauer66e93352015-06-16 22:38:23 +0200844searchtool wget > /dev/null
845searchtool bzip2 "bzip2," > /dev/null
846
Martin Roth591790f2016-03-20 20:39:04 -0600847check_for_library "-lz" "zlib (zlib1g-dev or zlib-devel)"
848
Nico Huber156d87c2016-09-20 12:59:53 +0200849CC=cc
850check_cc
851
Martin Roth88463822016-01-07 11:03:36 -0700852if [ "$HALT_FOR_TOOLS" -ne 0 ]; then
853 exit 1
854fi
855
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700856# This initial cleanup is useful when updating the toolchain script.
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000857
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700858if [ "$clean" = "1" ]; then
859 cleanup
860fi
Stefan Reinauer074d9132009-09-26 16:43:17 +0000861
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700862# Set up host compiler and flags needed for various OSes
Stefan Reinauer074d9132009-09-26 16:43:17 +0000863
Patrick Georgif2c15f52015-06-11 21:07:31 +0200864if is_package_enabled "GCC"; then
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700865if [ $UNAME = "Darwin" ]; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000866 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
867
868 # generally the OS X compiler can create x64 binaries.
869 # Per default it generated i386 binaries in 10.5 and x64
870 # binaries in 10.6 (even if the kernel is 32bit)
871 # For some weird reason, 10.5 autodetects an ABI=64 though
872 # so we're setting the ABI explicitly here.
Stefan Reinauerd7649122015-06-09 11:44:25 -0700873 if [ $(sysctl -n hw.optional.x86_64 2>/dev/null) -eq 1 ] 2>/dev/null; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000874 OPTIONS="ABI=64"
875 else
876 OPTIONS="ABI=32"
877 fi
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700878
Stefan Reinauer4266b922012-12-05 16:18:32 -0800879 # In Xcode 4.5.2 the default compiler is clang.
880 # However, this compiler fails to compile gcc 4.7.x. As a
881 # workaround it's possible to compile gcc with llvm-gcc.
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700882 if $CC -v 2>&1 | grep -q LLVM; then
Stefan Reinauer4266b922012-12-05 16:18:32 -0800883 CC=llvm-gcc
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700884 fi
zbao11f1d312015-08-05 23:15:54 -0400885elif [ $UNAME = "Linux" -o $UNAME = "Cygwin" ]; then
Patrick Georgiddb8f802015-07-04 17:45:54 +0200886 # gmp is overeager with detecting 64bit CPUs even if they run
887 # a 32bit kernel and userland.
888 if [ "$(uname -m 2>/dev/null)" = "i686" ]; then
889 OPTIONS="ABI=32"
890 fi
Nico Huberde45c592016-01-20 23:22:33 +0100891elif [ $UNAME = "NetBSD" ]; then
892 # same for NetBSD but this one reports an i386
893 if [ "$(uname -m 2>/dev/null)" = "i386" ]; then
894 OPTIONS="ABI=32"
895 fi
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000896fi
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700897fi # GCC
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000898
Nico Huber5f72f962016-01-31 23:05:19 +0100899export HOSTCFLAGS="-Os"
900if have_hostcflags_from_gmp; then
901 set_hostcflags_from_gmp
902fi
903
Patrick Georgic1a75b12011-11-04 21:37:14 +0100904if [ "$USECCACHE" = 1 ]; then
905 CC="ccache $CC"
906fi
907
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700908# Prepare target directory for building GCC
909# (dependencies must be in the PATH)
Stefan Reinauer16bd7892012-12-07 23:57:01 +0100910mkdir -p $DESTDIR$TARGETDIR/bin
Stefan Reinauer88352d72016-04-06 15:57:03 -0700911mkdir -p $DESTDIR$TARGETDIR/share
Stefan Reinauer16bd7892012-12-07 23:57:01 +0100912export PATH=$DESTDIR$TARGETDIR/bin:$PATH
913
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700914# Download, unpack, patch and build all packages
915
Martin Roth03625172016-02-17 14:44:14 -0700916printf "Downloading tarballs ... \n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700917mkdir -p tarballs
918for P in $PACKAGES; do
919 download $P
920done
Martin Roth03625172016-02-17 14:44:14 -0700921printf "Downloaded tarballs ... ${green}ok${NC}\n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700922
923printf "Unpacking and patching ... \n"
924for P in $PACKAGES; do
925 unpack_and_patch $P || exit 1
926done
927printf "Unpacked and patched ... ${green}ok${NC}\n"
928
929printf "Building packages ... \n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700930for package in $PACKAGES; do
931 build $package
932done
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700933printf "Packages built ... ${green}ok${NC}\n"
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700934
Stefan Reinauer699ac272015-06-05 12:43:28 -0700935# Adding git information of current tree to target directory
936# for reproducibility
Stefan Reinauerd7649122015-06-09 11:44:25 -0700937PROGNAME=$(basename "$0")
Stefan Reinauer88352d72016-04-06 15:57:03 -0700938rm -f "$DESTDIR$TARGETDIR/share/$PROGNAME-*"
939cp "$PROGNAME" "$DESTDIR$TARGETDIR/share/$PROGNAME-$CROSSGCC_VERSION-$CROSGCC_COMMIT"
Zheng Bao30b895f2013-02-06 18:04:40 +0800940
Stefan Reinauer88e83e52016-04-06 15:39:48 -0700941cleanup
Stefan Reinauer074d9132009-09-26 16:43:17 +0000942
Stefan Reinauer699ac272015-06-05 12:43:28 -0700943printf "\n${green}You can now run your $NAME toolchain from $TARGETDIR.${NC}\n"