blob: b650c62299fe2e787610dc3cdf15f10cd4909bfe [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
Iru Caiae8e3a02016-04-02 10:50:59 +080021CROSSGCC_DATE="April 16th, 2016"
22CROSSGCC_VERSION="1.39"
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
Stefan Reinauer074d9132009-09-26 16:43:17 +000033
Stefan Reinauer699ac272015-06-05 12:43:28 -070034# GCC toolchain version numbers
Timothy Pearson96310162015-12-28 22:53:29 -060035GMP_VERSION=6.1.0
Iru Caiae8e3a02016-04-02 10:50:59 +080036MPFR_VERSION=3.1.4
Paul Menzele6619422015-02-23 10:33:59 +010037MPC_VERSION=1.0.3
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000038LIBELF_VERSION=0.8.13
Iru Caiae8e3a02016-04-02 10:50:59 +080039GCC_VERSION=5.3.0
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070040GCC_AUTOCONF_VERSION=2.69
Iru Caiae8e3a02016-04-02 10:50:59 +080041BINUTILS_VERSION=2.26
Stefan Reinauer498f3d02016-04-21 18:10:15 -070042GDB_VERSION=7.11
Martin Roth64b35f32015-12-30 15:10:13 -070043IASL_VERSION=20160318
Stefan Reinauer3dc5d072016-04-21 19:29:03 -070044PYTHON_VERSION=3.5.1
Stefan Reinauer498f3d02016-04-21 18:10:15 -070045EXPAT_VERSION=2.1.1
Stefan Reinauer14ce2132015-06-05 13:01:13 -070046# CLANG version number
Iru Caiae8e3a02016-04-02 10:50:59 +080047CLANG_VERSION=3.8.0
Stefan Reinauera851bbb2016-01-29 18:03:26 -080048MAKE_VERSION=4.1
Stefan Reinauer074d9132009-09-26 16:43:17 +000049
Stefan Reinauer699ac272015-06-05 12:43:28 -070050# GCC toolchain archive locations
Martin Roth1484c032016-03-08 09:37:14 -070051# These are sanitized by the jenkins toolchain test builder, so if
52# a completely new URL is added here, it probably needs to be added
53# to the jenkins build as well, or the builder won't download it.
Stefan Reinauerd9d21022016-04-21 18:16:30 -070054GMP_ARCHIVE="http://ftpmirror.gnu.org/gmp/gmp-${GMP_VERSION}.tar.xz"
55MPFR_ARCHIVE="http://ftpmirror.gnu.org/mpfr/mpfr-${MPFR_VERSION}.tar.xz"
Idwer Vollering7962fc72012-10-25 02:14:09 +020056MPC_ARCHIVE="http://ftpmirror.gnu.org/mpc/mpc-${MPC_VERSION}.tar.gz"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000057LIBELF_ARCHIVE="http://www.mr511.de/software/libelf-${LIBELF_VERSION}.tar.gz"
Idwer Vollering1cfee0b2012-10-25 02:03:53 +020058GCC_ARCHIVE="http://ftpmirror.gnu.org/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.bz2"
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070059BINUTILS_ARCHIVE="http://ftpmirror.gnu.org/binutils/binutils-${BINUTILS_VERSION}.tar.bz2"
Patrick Georgi53c388f2015-03-07 09:55:18 +010060GDB_ARCHIVE="http://ftpmirror.gnu.org/gdb/gdb-${GDB_VERSION}.tar.xz"
Patrick Georgi85bb9462016-02-11 17:22:16 +010061IASL_ARCHIVE="https://acpica.org/sites/acpica/files/acpica-unix2-${IASL_VERSION}.tar.gz"
Patrick Georgi53c388f2015-03-07 09:55:18 +010062PYTHON_ARCHIVE="http://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz"
Stefan Reinauer498f3d02016-04-21 18:10:15 -070063EXPAT_ARCHIVE="http://downloads.sourceforge.net/sourceforge/expat/expat-${EXPAT_VERSION}.tar.bz2"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070064# CLANG toolchain archive locations
65LLVM_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/llvm-${CLANG_VERSION}.src.tar.xz"
66CFE_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/cfe-${CLANG_VERSION}.src.tar.xz"
67CRT_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/compiler-rt-${CLANG_VERSION}.src.tar.xz"
68CTE_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/clang-tools-extra-${CLANG_VERSION}.src.tar.xz"
Stefan Reinauera851bbb2016-01-29 18:03:26 -080069MAKE_ARCHIVE="http://ftpmirror.gnu.org/make/make-${MAKE_VERSION}.tar.bz2"
Stefan Reinauer074d9132009-09-26 16:43:17 +000070
Martin Roth58c68d52016-03-01 15:22:42 -070071ALL_ARCHIVES="$GMP_ARCHIVE $MPFR_ARCHIVE $MPC_ARCHIVE $LIBELF_ARCHIVE \
72 $GCC_ARCHIVE $BINUTILS_ARCHIVE $GDB_ARCHIVE $IASL_ARCHIVE \
73 $PYTHON_ARCHIVE $EXPAT_ARCHIVE $LLVM_ARCHIVE $CFE_ARCHIVE \
74 $CRT_ARCHIVE $CTE_ARCHIVE $MAKE_ARCHIVE"
75
Stefan Reinauer699ac272015-06-05 12:43:28 -070076# GCC toolchain directories
Stefan Reinauer074d9132009-09-26 16:43:17 +000077GMP_DIR="gmp-${GMP_VERSION}"
78MPFR_DIR="mpfr-${MPFR_VERSION}"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000079MPC_DIR="mpc-${MPC_VERSION}"
80LIBELF_DIR="libelf-${LIBELF_VERSION}"
Stefan Reinauer074d9132009-09-26 16:43:17 +000081GCC_DIR="gcc-${GCC_VERSION}"
82BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
83GDB_DIR="gdb-${GDB_VERSION}"
Patrick Georgi85bb9462016-02-11 17:22:16 +010084IASL_DIR="acpica-unix2-${IASL_VERSION}"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +010085PYTHON_DIR="Python-${PYTHON_VERSION}"
86EXPAT_DIR="expat-${EXPAT_VERSION}"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070087# CLANG toolchain directories
88LLVM_DIR="llvm-${CLANG_VERSION}.src"
89CFE_DIR="cfe-${CLANG_VERSION}.src"
90CRT_DIR="compiler-rt-${CLANG_VERSION}.src"
91CTE_DIR="clang-tools-extra-${CLANG_VERSION}.src"
Stefan Reinauera851bbb2016-01-29 18:03:26 -080092MAKE_DIR="make-${MAKE_VERSION}"
Stefan Reinauer074d9132009-09-26 16:43:17 +000093
Patrick Georgi6321f522015-02-27 23:36:26 +010094unset MAKELEVEL MAKEFLAGS
95
Patrick Georgi1861ff72011-10-31 12:15:55 +010096red='\033[0;31m'
97RED='\033[1;31m'
98green='\033[0;32m'
99GREEN='\033[1;32m'
100blue='\033[0;34m'
101BLUE='\033[1;34m'
102cyan='\033[0;36m'
103CYAN='\033[1;36m'
104NC='\033[0m' # No Color
Stefan Reinauer074d9132009-09-26 16:43:17 +0000105
zbaoe05c1ea2015-08-05 23:10:05 -0400106UNAME=$(uname | grep -iq cygwin && echo Cygwin || uname)
Martin Roth88463822016-01-07 11:03:36 -0700107HALT_FOR_TOOLS=0
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700108
Patrick Georgi198d23c2012-12-08 08:02:44 +0100109normalize_dirs()
110{
111 mkdir -p $DESTDIR$TARGETDIR/lib
112 test -d $DESTDIR$TARGETDIR/lib32 && mv $DESTDIR$TARGETDIR/lib32/* $DESTDIR$TARGETDIR/lib
113 test -d $DESTDIR$TARGETDIR/lib64 && mv $DESTDIR$TARGETDIR/lib64/* $DESTDIR$TARGETDIR/lib
114 rmdir -p $DESTDIR$TARGETDIR/lib32 $DESTDIR$TARGETDIR/lib64
115
116 perl -pi -e "s,/lib32,/lib," $DESTDIR$TARGETDIR/lib/*.la
117 perl -pi -e "s,/lib64,/lib," $DESTDIR$TARGETDIR/lib/*.la
118}
119
Stefan Reinauer93a96302015-06-15 12:36:53 -0700120please_install()
121{
Martin Roth88463822016-01-07 11:03:36 -0700122 HALT_FOR_TOOLS=1
Patrick Georgi5da95dc2015-07-17 23:33:05 +0200123 test -r /etc/os-release && . /etc/os-release
Stefan Reinauer93a96302015-06-15 12:36:53 -0700124 case "$ID_LIKE" in
125 debian) solution="sudo apt-get install $1" ;;
126 suse) solution="sudo zypper install $1" ;;
127 *) solution="using your OS packaging system" ;;
128 esac
129
Martin Roth591790f2016-03-20 20:39:04 -0600130 printf "${RED}ERROR:${red} Missing tool: Please install \'$1\'. (eg $solution)${NC}\n" >&2
Martin Rothe3963172016-01-06 13:54:32 -0700131 if [ -n "$2" ]; then
Martin Roth591790f2016-03-20 20:39:04 -0600132 printf "${RED}ERROR:${red} or install \'$2\'.${NC}\n" >&2
Martin Rothe3963172016-01-06 13:54:32 -0700133 fi
Stefan Reinauer93a96302015-06-15 12:36:53 -0700134}
135
Patrick Georgib7062882015-02-24 10:52:14 +0100136searchtool()
Stefan Reinauer074d9132009-09-26 16:43:17 +0000137{
138 # $1 short name
Patrick Georgib7062882015-02-24 10:52:14 +0100139 # $2 search string
140 # $3 soft fail if set
Martin Rothe3963172016-01-06 13:54:32 -0700141 # $4 alternative package to install on failure
Patrick Georgib7062882015-02-24 10:52:14 +0100142 # result: file name of that tool on stdout
143 # or no output if nothing suitable was found
144 search=GNU
145 if [ -n "$2" ]; then
146 search="$2"
147 fi
Stefan Reinauer074d9132009-09-26 16:43:17 +0000148 for i in "$1" "g$1" "gnu$1"; do
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700149 if [ -x "$(which $i 2>/dev/null)" ]; then
150 if [ "$(cat /dev/null | $i --version 2>&1 | grep -c "$search")" \
151 -gt 0 ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000152 echo $i
153 return
154 fi
155 fi
156 done
Andrew Wub67e9a12014-04-28 18:13:44 +0800157 # A workaround for OSX 10.9 and some BSDs, whose nongnu
158 # patch and tar also work.
Patrick Georgi7cb26b42015-04-28 21:29:22 +0200159 if [ $UNAME = "Darwin" -o $UNAME = "FreeBSD" -o $UNAME = "NetBSD" -o $UNAME = "OpenBSD" ]; then
Patrick Georgib7062882015-02-24 10:52:14 +0100160 if [ "$1" = "patch" -o "$1" = "tar" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700161 if [ -x "$(which $1 2>/dev/null)" ]; then
Zheng Bao36156ff2012-09-28 16:18:58 +0800162 echo $1
163 return
164 fi
165 fi
166 fi
Stefan Reinauerd7649122015-06-09 11:44:25 -0700167 if [ "$(echo $1 | cut -b -3)" = "sha" ]; then
zbao939dc842015-04-30 04:44:07 +0800168 if [ $UNAME = "FreeBSD" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700169 if [ -x "$(which sha1 2>/dev/null)" ]; then
zbao939dc842015-04-30 04:44:07 +0800170 echo sha1
171 return
172 fi
173 fi
174 if [ $UNAME = "NetBSD" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700175 if [ -x "$(which cksum 2>/dev/null)" ]; then
Stefan Reinauerd7649122015-06-09 11:44:25 -0700176 echo cksum -a $(echo $1 | sed -e 's,sum,,')
zbao939dc842015-04-30 04:44:07 +0800177 return
178 fi
179 fi
180 if [ $UNAME = "Darwin" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700181 if [ -x "$(which openssl 2>/dev/null)" ]; then
Stefan Reinauerd7649122015-06-09 11:44:25 -0700182 echo openssl $(echo $1 | sed -e 's,sum,,')
zbao939dc842015-04-30 04:44:07 +0800183 return
184 fi
185 fi
186 fi
Martin Rothe3963172016-01-06 13:54:32 -0700187
Martin Roth88463822016-01-07 11:03:36 -0700188 [ -z "$3" ] && please_install $1 $4
Patrick Georgib7062882015-02-24 10:52:14 +0100189 false
Stefan Reinauer074d9132009-09-26 16:43:17 +0000190}
191
Martin Roth591790f2016-03-20 20:39:04 -0600192# Run a compile check of the specified library option to see if it's installed
193check_for_library() {
194 local LIBRARY_FLAGS=$1
Aaron Durbin0ac4e5a2016-04-07 16:56:25 -0500195 local LIBRARY_PACKAGES="$2"
Martin Roth591790f2016-03-20 20:39:04 -0600196 local LIBTEST_FILE=.libtest
197
198 echo "int main(int argc, char **argv) { (void) argc; (void) argv; return 0; }" > "${LIBTEST_FILE}.c"
199
200 cc $CFLAGS $LIBRARY_FLAGS "${LIBTEST_FILE}.c" -o "${LIBTEST_FILE}" >/dev/null 2>&1 || \
201 please_install "$LIBRARY_PACKAGES"
202 rm -rf "${LIBTEST_FILE}.c" "${LIBTEST_FILE}"
203}
204
zbao06e85ac2015-08-24 22:08:36 -0400205check_sum() {
206 test -z "$CHECKSUM" || \
207 test "$(cat sum/$1.cksum 2>/dev/null | sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@')" = \
208 "$($CHECKSUM tarballs/$1 2>/dev/null | sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@')"
209}
210
211compute_sum() {
212 test ! -f sum/$1.cksum && test -f tarballs/$1 && \
213 (test -z "$CHECKSUM" || $CHECKSUM tarballs/$1 > sum/$1.cksum ) && \
214 printf "(checksum created. ${RED}Note. Please upload sum/$1.cksum if the corresponding archive is upgraded.)${NC}"
215}
216
zbao282dd952015-09-06 05:00:04 -0400217download_showing_percentage() {
218 url=$1
219 printf " ..${red} 0%%"
220 wget --no-check-certificate $url 2>&1 | while read line; do
221 printf "${red}"
222 echo $line | grep -o "[0-9]\+%" | awk '{printf("\b\b\b\b%4s", $1)}'
223 printf "${NC}"
224 done
225}
226
Stefan Reinauer88935482015-06-05 09:51:10 -0700227download() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700228 package=$1
229 archive="$(eval echo \$$package"_ARCHIVE")"
Stefan Reinauer699ac272015-06-05 12:43:28 -0700230
Stefan Reinauerd7649122015-06-09 11:44:25 -0700231 FILE=$(basename $archive)
Stefan Reinauer88935482015-06-05 09:51:10 -0700232 printf " * $FILE "
233
zbaod22b9ea2015-08-30 23:35:16 -0400234 if test -f tarballs/$FILE && check_sum $FILE ; then
235 printf "(cached)"
236 else
Paul Menzelb2ba3952015-06-12 11:58:14 +0200237 printf "(downloading from $archive)"
Stefan Reinauer88935482015-06-05 09:51:10 -0700238 rm -f tarballs/$FILE
239 cd tarballs
zbao282dd952015-09-06 05:00:04 -0400240 download_showing_percentage $archive
Stefan Reinauer88935482015-06-05 09:51:10 -0700241 cd ..
zbao06e85ac2015-08-24 22:08:36 -0400242 compute_sum $FILE
zbaod22b9ea2015-08-30 23:35:16 -0400243 fi
244
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700245 if [ ! -f tarballs/$FILE ]; then
zbao282dd952015-09-06 05:00:04 -0400246 printf "\n${RED}Failed to download $FILE.${NC}\n"
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700247 exit 1
248 fi
Stefan Reinauer88935482015-06-05 09:51:10 -0700249 printf "\n"
250}
251
252unpack_and_patch() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700253 package=$1
254 archive="$(eval echo \$$package"_ARCHIVE")"
255 dir="$(eval echo \$$package"_DIR")"
Stefan Reinauer88935482015-06-05 09:51:10 -0700256 test -d ${dir} && test -f ${dir}/.unpack_success || (
Stefan Reinauerd7649122015-06-09 11:44:25 -0700257 printf " * $(basename $archive)\n"
Stefan Reinauer88935482015-06-05 09:51:10 -0700258 FLAGS=zxf
Stefan Reinauerd7649122015-06-09 11:44:25 -0700259 suffix=$(echo $archive | sed 's,.*\.,,')
Martin Roth95f7b222016-03-20 12:38:48 -0600260 if [ "$suffix" = "gz" ] && [ -n "$PIGZ" ]; then FLAGS="-I pigz -xf"
261 elif [ "$suffix" = "gz" ]; then FLAGS=zxf
262 elif [ "$suffix" = "bz2" ] && [ -n "$LBZIP2" ]; then FLAGS="-I lbzip2 -xf"
263 elif [ "$suffix" = "bz2" ]; then FLAGS=jxf
264 elif [ "$suffix" = "xz" ]; then FLAGS="--xz -xf"
265 elif [ "$suffix" = "lzma" ]; then FLAGS="--lzma -xf"
266 fi
Stefan Reinauerd7649122015-06-09 11:44:25 -0700267 $TAR $FLAGS tarballs/$(basename $archive)
Stefan Reinauer88935482015-06-05 09:51:10 -0700268 for patch in patches/${dir}_*.patch; do
269 test -r $patch || continue
Stefan Reinauerd7649122015-06-09 11:44:25 -0700270 printf " o $(basename $patch)\n"
Stefan Reinauer916b8452015-06-09 11:54:49 -0700271 $PATCH -s -N -p0 < $(echo $patch) || {
Stefan Reinauer88935482015-06-05 09:51:10 -0700272 printf "\n${RED}Failed $patch.${NC}\n"
Stefan Reinauer916b8452015-06-09 11:54:49 -0700273 exit 1
274 }
Stefan Reinauer88935482015-06-05 09:51:10 -0700275 done
276 touch ${dir}/.unpack_success
277 )
278}
279
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700280fn_exists()
281{
Patrick Georgic8665912015-06-11 21:04:31 +0200282 type $1 >/dev/null 2>&1
Patrick Georgi74c06422015-03-25 18:40:13 +0100283}
284
Patrick Georgif2c15f52015-06-11 21:07:31 +0200285is_package_enabled()
286{
287 echo "$PACKAGES" |grep -q "\<$1\>"
288}
289
Martin Roth444ece22016-04-01 18:46:29 -0600290package_uses_targetarch()
291{
Stefan Reinauera344b682016-04-21 19:29:51 -0700292 if [ "$1" = "GCC" ] || [ "$1" = "GDB" ] || [ "$1" = "BINUTILS" ]; then
Martin Roth444ece22016-04-01 18:46:29 -0600293 true
294 else
295 false
296 fi
297}
298
Nico Huber11ea2b32016-01-26 16:09:31 +0100299generic_build()
300{
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700301 package=$1
Nico Huber11ea2b32016-01-26 16:09:31 +0100302 host_target=$2
303 builddir=$3
304 success=$4
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700305
306 fn_exists build_$package || return
307
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700308 version="$(eval echo \$$package"_VERSION")"
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700309
Nico Huber11ea2b32016-01-26 16:09:31 +0100310 mkdir -p "$builddir"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700311
Nico Huber11ea2b32016-01-26 16:09:31 +0100312 if [ -f "$success" ]; then
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700313 printf "Skipping $package as it is already built\n"
314 else
315 printf "Building $package $version ... "
Nico Huber11ea2b32016-01-26 16:09:31 +0100316 DIR="$PWD"
317 cd "$builddir"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700318 rm -f .failed
Nico Huber11ea2b32016-01-26 16:09:31 +0100319 build_${package} $host_target > build.log 2>&1
320 cd "$DIR"
321 if [ ! -f "$builddir/.failed" ]; then
322 touch "$success";
323 else
324 printf "${RED}failed${NC}. Check $builddir/build.log.\n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700325 exit 1
326 fi
327 printf "${green}ok${NC}\n"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700328 fi
329}
330
Nico Huber11ea2b32016-01-26 16:09:31 +0100331build_for_host()
332{
333 generic_build $1 host build-$1 "${TARGETDIR}/.$1.success"
334}
335
336build_for_target()
337{
338 generic_build $1 target build-${TARGETARCH}-$1 "${TARGETDIR}/.${TARGETARCH}-$1.success"
339}
340
341build()
342{
343 if package_uses_targetarch $1; then
344 build_for_target $1
345 else
346 build_for_host $1
347 fi
348}
349
Stefan Reinauer074d9132009-09-26 16:43:17 +0000350cleanup()
351{
Stefan Reinauer88e83e52016-04-06 15:39:48 -0700352 if [ $SAVETEMPS -ne 0 ]; then
353 printf "Leaving temporary files around... ${green}ok${NC}\n"
354 return
355 fi
356
Stefan Reinauer074d9132009-09-26 16:43:17 +0000357 printf "Cleaning up temporary files... "
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700358 for package in $PACKAGES; do
Martin Roth444ece22016-04-01 18:46:29 -0600359 rm -rf build-${TARGETARCH}-$package build-$package $(eval echo \$$package"_DIR")
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700360 done
zbaoff3f15c2015-08-26 22:00:31 -0400361 rm -f getopt
Stefan Reinauer074d9132009-09-26 16:43:17 +0000362 printf "${green}ok${NC}\n"
363}
364
365myhelp()
366{
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700367 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>] [-C] [-G] [-S]\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000368 printf " $0 [-V|--version]\n"
369 printf " $0 [-h|--help]\n\n"
370
371 printf "Options:\n"
372 printf " [-V|--version] print version number and exit\n"
373 printf " [-h|--help] print this help and exit\n"
374 printf " [-c|--clean] remove temporary files before build\n"
375 printf " [-t|--savetemps] don't remove temporary files after build\n"
Patrick Georgic1a75b12011-11-04 21:37:14 +0100376 printf " [-y|--ccache] Use ccache when building cross compiler\n"
Martin Roth4a53db02016-03-01 15:21:07 -0700377 printf " [--nocolor] don't print color codes in output\n"
Martin Roth58c68d52016-03-01 15:22:42 -0700378 printf " [--urls] print the urls for all packages\n"
Patrick Georgi73166c72010-04-14 20:42:42 +0000379 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
Martin Roth588c79d2016-03-20 12:03:20 -0600380 printf " [-s]--supported <tool> print supported version of a tool\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000381 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
Martin Roth588c79d2016-03-20 12:03:20 -0600382 printf " (defaults to $TARGETDIR)\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000383 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100384 printf " (for RPM builds, default unset)\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700385 printf " [-P|--package <package>] Build a specific package: GCC, CLANG, IASL, GDB\n"
386 printf " (defaults to $PACKAGE)\n"
387 printf "GCC specific options:\n"
388 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
389 printf " (defaults to $TARGETARCH)\n"
Nico Huberbb313bf2015-09-08 12:30:27 +0200390 printf " [-l|--languages <languages>] comma separated list of target languages\n"
391 printf " (defaults to $LANGUAGES)\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700392 printf "GDB specific options:\n"
393 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
394 printf " (defaults to $TARGETARCH)\n"
395 printf " [-S|--scripting] build scripting support for GDB\n\n"
Martin Rothd1da74f2015-06-21 12:06:28 -0600396 printf "Platforms for GCC & GDB:\n"
Martin Roth3e0f74d2016-03-08 12:07:04 -0700397 printf " x86_64 i386-elf i386-mingw32 mipsel-elf riscv-elf arm aarch64\n"
Jonathan Neuschäferf14f6402016-03-11 20:22:23 +0100398 printf " powerpc64le-linux-gnu nds32le-elf\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000399}
400
Martin Rothedf965a2015-11-25 16:58:33 -0700401printversion() {
402 printf "${blue}Welcome to the ${red}coreboot${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
403}
404
Stefan Reinauer074d9132009-09-26 16:43:17 +0000405myversion()
406{
Martin Rothedf965a2015-11-25 16:58:33 -0700407 printversion
Stefan Reinauer074d9132009-09-26 16:43:17 +0000408
409 cat << EOF
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000410Copyright (C) 2008-2010 by coresystems GmbH
Marc Jones2aac3f62011-08-08 16:07:50 -0600411Copyright (C) 2011 by Sage Electronic Engineering
Stefan Reinauer074d9132009-09-26 16:43:17 +0000412
413This program is free software; you can redistribute it and/or modify
414it under the terms of the GNU General Public License as published by
415the Free Software Foundation; version 2 of the License.
416
417This program is distributed in the hope that it will be useful,
418but WITHOUT ANY WARRANTY; without even the implied warranty of
419MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
420GNU General Public License for more details.
421
422EOF
423}
424
Nico Huber5f72f962016-01-31 23:05:19 +0100425have_hostcflags_from_gmp() {
426 grep -q __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h >/dev/null 2>&1
427}
428
429set_hostcflags_from_gmp() {
430 # Now set CFLAGS to match GMP CFLAGS but strip out -pedantic
431 # as GCC 4.6.x fails if it's there.
432 export HOSTCFLAGS=$(grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2 |\
433 sed s,-pedantic,,)
434}
435
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700436build_GMP() {
Nico Huber5f72f962016-01-31 23:05:19 +0100437 CC="$CC" CFLAGS="$HOSTCFLAGS" \
438 ../${GMP_DIR}/configure --disable-shared --enable-fat \
439 --prefix=$TARGETDIR $OPTIONS \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700440 || touch .failed
441 $MAKE $JOBS || touch .failed
442 $MAKE install DESTDIR=$DESTDIR || touch .failed
443
444 normalize_dirs
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700445
Nico Huber5f72f962016-01-31 23:05:19 +0100446 set_hostcflags_from_gmp
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700447}
448
449build_MPFR() {
450 test $UNAME = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
451 CC="$CC" ../${MPFR_DIR}/configure --disable-shared --prefix=$TARGETDIR \
452 --infodir=$TARGETDIR/info \
453 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || \
454 touch .failed
455 $MAKE $JOBS || touch .failed
456 $MAKE install DESTDIR=$DESTDIR || touch .failed
457
458 normalize_dirs
459
460 # work around build problem of libgmp.la
461 if [ "$DESTDIR" != "" ]; then
462 perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
463 fi
464}
465
466build_MPC() {
467 CC="$CC" ../${MPC_DIR}/configure --disable-shared --prefix=$TARGETDIR \
468 --infodir=$TARGETDIR/info --with-mpfr=$DESTDIR$TARGETDIR \
469 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || \
470 touch .failed
471 $MAKE $JOBS || touch .failed
472 $MAKE install DESTDIR=$DESTDIR || touch .failed
473
474 normalize_dirs
475}
476
477build_LIBELF() {
478 CC="$CC" CFLAGS="$HOSTCFLAGS" libelf_cv_elf_h_works=no \
479 ../${LIBELF_DIR}/configure --disable-shared --prefix=$TARGETDIR \
480 --infodir=$TARGETDIR/info CFLAGS="$HOSTCFLAGS" || touch .failed
481 $MAKE $JOBS || touch .failed
482 $MAKE install prefix=$DESTDIR$TARGETDIR || touch .failed
483
484 normalize_dirs
485}
486
487build_BINUTILS() {
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700488 if [ $TARGETARCH == "x86_64-elf" ]; then
489 ADDITIONALTARGET=",i386-elf"
490 fi
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700491 CC="$CC" ../binutils-${BINUTILS_VERSION}/configure --prefix=$TARGETDIR \
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700492 --target=${TARGETARCH} --enable-targets=${TARGETARCH}${ADDITIONALTARGET} \
493 --disable-werror --disable-nls --enable-lto --enable-gold \
Stefan Reinauer2fe2d3d2016-04-05 02:14:48 -0700494 --enable-interwork --enable-multilib \
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700495 --enable-plugins --enable-multilibs CFLAGS="$HOSTCFLAGS" || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700496 $MAKE $JOBS || touch .failed
497 $MAKE install DESTDIR=$DESTDIR || touch .failed
498}
499
500
501build_GCC() {
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700502 # Work around crazy code generator in GCC that confuses CLANG.
Aaron Durbinb2229dc2016-04-07 16:58:10 -0500503 $CC --version | grep clang >/dev/null 2>&1 && \
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700504 HOSTCFLAGS="$HOSTCFLAGS -fbracket-depth=1024"
505
Stefan Reinauerd0f83722015-06-09 11:52:28 -0700506 # GCC does not honor HOSTCFLAGS at all. CFLAGS are used for
507 # both target and host object files.
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700508 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
509 # but it does not seem to work properly. At least the host library
510 # libiberty is not compiled with CFLAGS_FOR_BUILD.
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700511 # Also set the CXX version of the flags because GCC is now compiled
512 # using C++.
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100513 CC="$CC" CFLAGS_FOR_TARGET="-O2 -Dinhibit_libc" CFLAGS="$HOSTCFLAGS" \
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700514 CFLAGS_FOR_BUILD="$HOSTCFLAGS" CXXFLAGS="$HOSTCFLAGS" \
515 CXXFLAGS_FOR_BUILD="$HOSTCFLAGS" ../gcc-${GCC_VERSION}/configure \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700516 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
517 --target=${TARGETARCH} --disable-werror --disable-shared \
Stefan Reinauer2f97ebc2015-06-09 12:24:54 -0700518 --enable-lto --enable-plugins --enable-gold --enable-ld=default \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700519 --disable-libssp --disable-bootstrap --disable-nls \
520 --disable-libquadmath --without-headers \
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100521 --disable-threads \
Patrick Georgi1fc02d12016-03-23 21:44:43 +0100522 --enable-interwork --enable-multilib --enable-targets=all \
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100523 --disable-libatomic --disable-libcc1 --disable-decimal-float \
Nico Huberbb313bf2015-09-08 12:30:27 +0200524 ${GCC_OPTIONS} --enable-languages="${LANGUAGES}" \
Ronald G. Minnich7ee16b72016-02-12 21:54:59 +0000525 --with-system-zlib \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700526 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
527 --with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
528 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE" \
529 || touch .failed
530 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-gcc || touch .failed
531 $MAKE install-gcc DESTDIR=$DESTDIR || touch .failed
532
Stefan Reinauerd7649122015-06-09 11:44:25 -0700533 if [ "$(echo $TARGETARCH | grep -c -- -mingw32)" -eq 0 ]; then
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700534 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-target-libgcc || touch .failed
535 $MAKE install-target-libgcc DESTDIR=$DESTDIR || touch .failed
536 fi
537}
538
539build_EXPAT() {
540 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${EXPAT_DIR}/configure --disable-shared \
Stefan Reinauera344b682016-04-21 19:29:51 -0700541 --prefix=$TARGETDIR || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700542 $MAKE || touch .failed
543 $MAKE install DESTDIR=$DESTDIR || touch .failed
544
545 normalize_dirs
546}
547
548build_PYTHON() {
549 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${PYTHON_DIR}/configure --prefix=$TARGETDIR \
Stefan Reinauera344b682016-04-21 19:29:51 -0700550 || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700551 $MAKE $JOBS || touch .failed
552 $MAKE install DESTDIR=$DESTDIR || touch .failed
553
554 normalize_dirs
555}
556
557build_GDB() {
558 export PYTHONHOME=$DESTDIR$TARGETDIR
559 if [ $(uname) != "FreeBSD" -a $(uname) != "NetBSD" ]; then
560 LIBDL="-ldl"
561 fi
562 LDFLAGS="-Wl,-rpath,\$\$ORIGIN/../lib/ -L$DESTDIR$TARGETDIR/lib \
563 -lpthread $LIBDL -lutil" \
564 CC="$CC" CFLAGS="$HOSTCFLAGS -I$DESTDIR$TARGETDIR/include" \
Stefan Reinauer7b40e422015-06-09 11:56:32 -0700565 ../${GDB_DIR}/configure --prefix=$TARGETDIR \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700566 --target=${TARGETARCH} --disable-werror --disable-nls
567 $MAKE $JOBS || touch .failed
568 $MAKE install DESTDIR=$DESTDIR || touch .failed
569}
570
571build_IASL() {
572 RDIR=$PWD
573 cd ../$IASL_DIR/generate/unix
574 CFLAGS="$HOSTCFLAGS"
575 HOST="_LINUX"
576 test $UNAME = "Darwin" && HOST="_APPLE"
577 test $UNAME = "FreeBSD" && HOST="_FreeBSD"
578 test $UNAME = "Cygwin" && HOST="_CYGWIN"
Martin Roth6f656132015-12-08 11:14:10 -0700579 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 -0700580 rm -f $DESTDIR$TARGETDIR/bin/iasl || touch $RDIR/.failed
581 cp bin/iasl $DESTDIR$TARGETDIR/bin || touch $RDIR/.failed
582}
583
584build_LLVM() {
585 cd ..
586 ln -sf $PWD/$CFE_DIR $LLVM_DIR/tools/clang
587 ln -sf $PWD/$CTE_DIR $LLVM_DIR/tools/clang/tools/extra
588 ln -sf $PWD/$CRT_DIR $LLVM_DIR/projects/compiler-rt
589 cd -
590
591 $CMAKE -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$DESTDIR$TARGETDIR \
Martin Rothfb5647a2015-12-18 09:26:45 -0700592 -DCLANG_VENDOR="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE - " \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700593 -DCMAKE_BUILD_TYPE=Release ../$LLVM_DIR || touch .failed
594 $MAKE $JOBS || touch .failed
595 $MAKE install || touch .failed
596
597 cp -a ../$CFE_DIR/tools/scan-build/* $DESTDIR$TARGETDIR/bin
598 cp -a ../$CFE_DIR/tools/scan-view/* $DESTDIR$TARGETDIR/bin
Stefan Reinauere9e6e3d2015-07-07 00:20:42 +0200599
600 # create symlinks to work around broken --print-librt-file-name
601 # when used with -target.
602 cd $DESTDIR$TARGETDIR/lib/clang/${CLANG_VERSION}/lib
603 for i in */libclang_rt.builtins*.a; do
604 ln -s $i .
605 done
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700606}
607
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800608build_MAKE() {
609 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${MAKE_DIR}/configure --prefix=$TARGETDIR \
610 --disable-nls || touch .failed
611 $MAKE $JOBS || touch .failed
612 $MAKE install DESTDIR=$DESTDIR || touch .failed
613
614 normalize_dirs
615}
616
Martin Rothedf965a2015-11-25 16:58:33 -0700617print_stable() {
618 case "$PRINTSTABLE" in
619 AUTOCONF|autoconf) printf "%s\n" "$GCC_AUTOCONF_VERSION";;
620 BINUTILS|binutils) printf "%s\n" "$BINUTILS_VERSION";;
621 CLANG|clang) printf "%s\n" "$CLANG_VERSION";;
622 EXPAT|expat) printf "%s\n" "$EXPAT_VERSION";;
623 GCC|gcc) printf "%s\n" "$GCC_VERSION";;
624 GDB|gdb) printf "%s\n" "$GDB_VERSION";;
625 GMP|gmp) printf "%s\n" "$GMP_VERSION";;
626 IASL|iasl) printf "%s\n" "$IASL_VERSION";;
627 LIBELF|libelf) printf "%s\n" "$LIBELF_VERSION";;
628 MPC|mpc) printf "%s\n" "$MPC_VERSION";;
629 MPFR|mpfr) printf "%s\n" "$MPFR_VERSION";;
630 PYTHON|python) printf "%s\n" "$PYTHON_VERSION";;
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800631 MAKE|make) printf "%s\n" "$MAKE_VERSION";;
Martin Rothedf965a2015-11-25 16:58:33 -0700632 *) printf "Unknown tool %s\n" "$PRINTSTABLE";;
633 esac
634}
Stefan Reinauer074d9132009-09-26 16:43:17 +0000635
Patrick Georgi6bba29f2010-01-29 17:40:52 +0000636# Look if we have getopt. If not, build it.
637export PATH=$PATH:.
638getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
639
Stefan Reinauer074d9132009-09-26 16:43:17 +0000640# parse parameters.. try to find out whether we're running GNU getopt
Stefan Reinauerd7649122015-06-09 11:44:25 -0700641getoptbrand="$(getopt -V | sed -e '1!d' -e 's,^\(......\).*,\1,')"
Peter Stuge09377b72011-08-21 06:24:55 +0200642if [ "${getoptbrand}" = "getopt" ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000643 # Detected GNU getopt that supports long options.
Martin Rothedf965a2015-11-25 16:58:33 -0700644 args=$(getopt -l version,help,clean,directory:,platform:,languages:,package:,jobs:,destdir:,savetemps,scripting,ccache,supported: Vhcd:p:l:P:j:D:tSys: -- "$@")
Stefan Reinauer074d9132009-09-26 16:43:17 +0000645 eval set "$args"
646else
647 # Detected non-GNU getopt
Martin Rothedf965a2015-11-25 16:58:33 -0700648 args=$(getopt Vhcd:p:l:P:j:D:tSys: $*)
Stefan Reinauer074d9132009-09-26 16:43:17 +0000649 set -- $args
650fi
651
652if [ $? != 0 ]; then
653 myhelp
654 exit 1
655fi
656
657while true ; do
658 case "$1" in
659 -V|--version) shift; myversion; exit 0;;
660 -h|--help) shift; myhelp; exit 0;;
Patrick Georgi44af57a2013-12-19 22:06:22 +0100661 -c|--clean) shift; clean=1;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000662 -t|--savetemps) shift; SAVETEMPS=1;;
663 -d|--directory) shift; TARGETDIR="$1"; shift;;
664 -p|--platform) shift; TARGETARCH="$1"; shift;;
Nico Huberbb313bf2015-09-08 12:30:27 +0200665 -l|--languages) shift; LANGUAGES="$1"; shift;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000666 -D|--destdir) shift; DESTDIR="$1"; shift;;
Patrick Georgi73166c72010-04-14 20:42:42 +0000667 -j|--jobs) shift; JOBS="-j $1"; shift;;
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700668 -P|--package) shift; PACKAGE="$1"; shift;;
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100669 -S|--scripting) shift; SKIPPYTHON=0;;
Patrick Georgic1a75b12011-11-04 21:37:14 +0100670 -y|--ccache) shift; USECCACHE=1;;
Martin Rothedf965a2015-11-25 16:58:33 -0700671 -s|--supported) shift; PRINTSTABLE="$1"; shift;;
Martin Roth58c68d52016-03-01 15:22:42 -0700672 --urls) shift; printf "%s\n" "$ALL_ARCHIVES"; exit 0;;
Martin Roth4a53db02016-03-01 15:21:07 -0700673 --nocolor) shift; \
674 unset red RED green GREEN blue BLUE cyan CYAN NC;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200675 --) shift; break;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000676 -*) printf "Invalid option\n\n"; myhelp; exit 1;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200677 *) break;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000678 esac
679done
680
Martin Rothedf965a2015-11-25 16:58:33 -0700681if [ -n "$PRINTSTABLE" ]; then
682 print_stable
683 exit 0
684fi
685
686#print toolchain builder version string as the header
687printversion
688
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700689case "$TARGETARCH" in
Timothy Pearsona9f62352015-12-30 18:59:00 -0600690 x86_64-elf) ;;
691 x86_64*) TARGETARCH=x86_64-elf;;
692 i386-elf) ;;
693 i386-mingw32) ;;
694 mipsel-elf) ;;
Iru Caiae8e3a02016-04-02 10:50:59 +0800695 riscv-elf) TARGETARCH=riscv64-elf;;
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100696 powerpc64*-linux*) ;;
Timothy Pearsona9f62352015-12-30 18:59:00 -0600697 i386*) TARGETARCH=i386-elf;;
Stefan Reinauer27522ad2016-01-29 17:31:34 -0800698 arm*) TARGETARCH=arm-eabi;;
Timothy Pearsona9f62352015-12-30 18:59:00 -0600699 aarch64*) TARGETARCH=aarch64-elf;;
Martin Roth3e0f74d2016-03-08 12:07:04 -0700700 nds32le-elf) ;;
Timothy Pearsona9f62352015-12-30 18:59:00 -0600701 *) printf "${red}WARNING: Unsupported architecture $TARGETARCH.${NC}\n\n"; ;;
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700702esac
703
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700704# Figure out which packages to build
Stefan Reinauer699ac272015-06-05 12:43:28 -0700705
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700706case "$PACKAGE" in
707 GCC|gcc)
708 echo "Target architecture is now $TARGETARCH"
709 NAME="${TARGETARCH} cross GCC"
Stefan Reinauerbd8b2e32015-06-17 11:55:02 -0700710 PACKAGES="GMP MPFR MPC LIBELF BINUTILS GCC"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700711 ;;
712 GDB|gdb)
713 NAME="${TARGETARCH} cross GDB"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700714 if [ $SKIPPYTHON -eq 0 ]; then
Ronald G. Minnichfb944f42016-02-27 04:06:42 +0000715 PACKAGES="EXPAT PYTHON GDB"
716 else
717 PACKAGES="EXPAT GDB"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700718 fi
719 ;;
720 CLANG|clang)
721 NAME=clang
722 PACKAGES="LLVM CFE CRT CTE"
David Hendrickscd5cdd32015-06-15 17:03:22 -0700723 CMAKE=$(searchtool cmake "cmake") || exit $?
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700724 ;;
725 IASL|iasl)
726 NAME="IASL ACPI compiler"
727 PACKAGES=IASL
728 ;;
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800729 MAKE|make)
730 NAME="GNU Make"
731 PACKAGES=MAKE
732 ;;
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700733 *)
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800734 printf "${red}ERROR: Unsupported package $PACKAGE. (Supported packages are GCC, GDB, CLANG, IASL, MAKE)${NC}\n\n";
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700735 exit 1
736 ;;
737esac
Peter Stugeceacd772011-06-09 04:54:16 +0200738
Stefan Reinauer66e93352015-06-16 22:38:23 +0200739# Find all the required tools:
740
741TAR=$(searchtool tar) || exit $?
742PATCH=$(searchtool patch) || exit $?
743MAKE=$(searchtool make) || exit $?
744SHA1SUM=$(searchtool sha1sum)
745SHA512SUM=$(searchtool sha512sum)
746CHECKSUM=$SHA1SUM
Martin Roth95f7b222016-03-20 12:38:48 -0600747LBZIP2=$(searchtool lbzip2 "" nofail)
748PIGZ=$(searchtool pigz "" nofail)
Stefan Reinauer66e93352015-06-16 22:38:23 +0200749
750searchtool m4 > /dev/null
751searchtool bison > /dev/null
752searchtool flex flex > /dev/null
753searchtool g++ "Free Software Foundation" nofail > /dev/null || \
Marc Jones5ee69512015-06-17 15:38:17 -0600754searchtool clang "clang version" nofail > /dev/null || \
Martin Rothe3963172016-01-06 13:54:32 -0700755searchtool clang "LLVM" "" "g++" > /dev/null
Stefan Reinauer66e93352015-06-16 22:38:23 +0200756searchtool wget > /dev/null
757searchtool bzip2 "bzip2," > /dev/null
758
Martin Roth591790f2016-03-20 20:39:04 -0600759check_for_library "-lz" "zlib (zlib1g-dev or zlib-devel)"
760
Martin Roth88463822016-01-07 11:03:36 -0700761if [ "$HALT_FOR_TOOLS" -ne 0 ]; then
762 exit 1
763fi
764
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700765# This initial cleanup is useful when updating the toolchain script.
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000766
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700767if [ "$clean" = "1" ]; then
768 cleanup
769fi
Stefan Reinauer074d9132009-09-26 16:43:17 +0000770
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700771# Set up host compiler and flags needed for various OSes
Stefan Reinauer074d9132009-09-26 16:43:17 +0000772
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700773CC=cc
Patrick Georgif2c15f52015-06-11 21:07:31 +0200774if is_package_enabled "GCC"; then
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700775if [ $UNAME = "Darwin" ]; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000776 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
777
778 # generally the OS X compiler can create x64 binaries.
779 # Per default it generated i386 binaries in 10.5 and x64
780 # binaries in 10.6 (even if the kernel is 32bit)
781 # For some weird reason, 10.5 autodetects an ABI=64 though
782 # so we're setting the ABI explicitly here.
Stefan Reinauerd7649122015-06-09 11:44:25 -0700783 if [ $(sysctl -n hw.optional.x86_64 2>/dev/null) -eq 1 ] 2>/dev/null; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000784 OPTIONS="ABI=64"
785 else
786 OPTIONS="ABI=32"
787 fi
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700788
Stefan Reinauer4266b922012-12-05 16:18:32 -0800789 # In Xcode 4.5.2 the default compiler is clang.
790 # However, this compiler fails to compile gcc 4.7.x. As a
791 # workaround it's possible to compile gcc with llvm-gcc.
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700792 if $CC -v 2>&1 | grep -q LLVM; then
Stefan Reinauer4266b922012-12-05 16:18:32 -0800793 CC=llvm-gcc
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700794 fi
zbao11f1d312015-08-05 23:15:54 -0400795elif [ $UNAME = "Linux" -o $UNAME = "Cygwin" ]; then
Patrick Georgiddb8f802015-07-04 17:45:54 +0200796 # gmp is overeager with detecting 64bit CPUs even if they run
797 # a 32bit kernel and userland.
798 if [ "$(uname -m 2>/dev/null)" = "i686" ]; then
799 OPTIONS="ABI=32"
800 fi
Nico Huberde45c592016-01-20 23:22:33 +0100801elif [ $UNAME = "NetBSD" ]; then
802 # same for NetBSD but this one reports an i386
803 if [ "$(uname -m 2>/dev/null)" = "i386" ]; then
804 OPTIONS="ABI=32"
805 fi
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000806fi
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700807fi # GCC
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000808
Nico Huber5f72f962016-01-31 23:05:19 +0100809export HOSTCFLAGS="-Os"
810if have_hostcflags_from_gmp; then
811 set_hostcflags_from_gmp
812fi
813
Patrick Georgic1a75b12011-11-04 21:37:14 +0100814if [ "$USECCACHE" = 1 ]; then
815 CC="ccache $CC"
816fi
817
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700818# Prepare target directory for building GCC
819# (dependencies must be in the PATH)
Stefan Reinauer16bd7892012-12-07 23:57:01 +0100820mkdir -p $DESTDIR$TARGETDIR/bin
Stefan Reinauer88352d72016-04-06 15:57:03 -0700821mkdir -p $DESTDIR$TARGETDIR/share
Stefan Reinauer16bd7892012-12-07 23:57:01 +0100822export PATH=$DESTDIR$TARGETDIR/bin:$PATH
823
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700824# Download, unpack, patch and build all packages
825
Martin Roth03625172016-02-17 14:44:14 -0700826printf "Downloading tarballs ... \n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700827mkdir -p tarballs
828for P in $PACKAGES; do
829 download $P
830done
Martin Roth03625172016-02-17 14:44:14 -0700831printf "Downloaded tarballs ... ${green}ok${NC}\n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700832
833printf "Unpacking and patching ... \n"
834for P in $PACKAGES; do
835 unpack_and_patch $P || exit 1
836done
837printf "Unpacked and patched ... ${green}ok${NC}\n"
838
839printf "Building packages ... \n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700840for package in $PACKAGES; do
841 build $package
842done
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700843printf "Packages built ... ${green}ok${NC}\n"
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700844
Stefan Reinauer699ac272015-06-05 12:43:28 -0700845# Adding git information of current tree to target directory
846# for reproducibility
Stefan Reinauerd7649122015-06-09 11:44:25 -0700847PROGNAME=$(basename "$0")
Stefan Reinauer88352d72016-04-06 15:57:03 -0700848rm -f "$DESTDIR$TARGETDIR/share/$PROGNAME-*"
849cp "$PROGNAME" "$DESTDIR$TARGETDIR/share/$PROGNAME-$CROSSGCC_VERSION-$CROSGCC_COMMIT"
Zheng Bao30b895f2013-02-06 18:04:40 +0800850
Stefan Reinauer88e83e52016-04-06 15:39:48 -0700851cleanup
Stefan Reinauer074d9132009-09-26 16:43:17 +0000852
Stefan Reinauer699ac272015-06-05 12:43:28 -0700853printf "\n${green}You can now run your $NAME toolchain from $TARGETDIR.${NC}\n"