blob: 72e8366df92aa69584b5d3ee0f1213f7faad612f [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
Stefan Reinauer074d9132009-09-26 16:43:17 +000035
Stefan Reinauer699ac272015-06-05 12:43:28 -070036# GCC toolchain version numbers
Timothy Pearson96310162015-12-28 22:53:29 -060037GMP_VERSION=6.1.0
Iru Caiae8e3a02016-04-02 10:50:59 +080038MPFR_VERSION=3.1.4
Paul Menzele6619422015-02-23 10:33:59 +010039MPC_VERSION=1.0.3
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000040LIBELF_VERSION=0.8.13
Iru Caiae8e3a02016-04-02 10:50:59 +080041GCC_VERSION=5.3.0
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070042GCC_AUTOCONF_VERSION=2.69
Martin Rothbcfa7cc2016-07-14 13:01:37 -060043BINUTILS_VERSION=2.26.1
Stefan Reinauer498f3d02016-04-21 18:10:15 -070044GDB_VERSION=7.11
Martin Roth2ab981b2016-08-31 16:43:49 -060045IASL_VERSION=20160831
Stefan Reinauer3dc5d072016-04-21 19:29:03 -070046PYTHON_VERSION=3.5.1
Stefan Reinauer498f3d02016-04-21 18:10:15 -070047EXPAT_VERSION=2.1.1
Stefan Reinauer14ce2132015-06-05 13:01:13 -070048# CLANG version number
Iru Caiae8e3a02016-04-02 10:50:59 +080049CLANG_VERSION=3.8.0
Martin Roth589ef9d2016-08-11 18:35:06 -060050MAKE_VERSION=4.2.1
Stefan Reinauer074d9132009-09-26 16:43:17 +000051
Stefan Reinauer699ac272015-06-05 12:43:28 -070052# GCC toolchain archive locations
Martin Roth1484c032016-03-08 09:37:14 -070053# These are sanitized by the jenkins toolchain test builder, so if
54# a completely new URL is added here, it probably needs to be added
55# to the jenkins build as well, or the builder won't download it.
Stefan Reinauerd9d21022016-04-21 18:16:30 -070056GMP_ARCHIVE="http://ftpmirror.gnu.org/gmp/gmp-${GMP_VERSION}.tar.xz"
57MPFR_ARCHIVE="http://ftpmirror.gnu.org/mpfr/mpfr-${MPFR_VERSION}.tar.xz"
Idwer Vollering7962fc72012-10-25 02:14:09 +020058MPC_ARCHIVE="http://ftpmirror.gnu.org/mpc/mpc-${MPC_VERSION}.tar.gz"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000059LIBELF_ARCHIVE="http://www.mr511.de/software/libelf-${LIBELF_VERSION}.tar.gz"
Idwer Vollering1cfee0b2012-10-25 02:03:53 +020060GCC_ARCHIVE="http://ftpmirror.gnu.org/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.bz2"
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070061BINUTILS_ARCHIVE="http://ftpmirror.gnu.org/binutils/binutils-${BINUTILS_VERSION}.tar.bz2"
Patrick Georgi53c388f2015-03-07 09:55:18 +010062GDB_ARCHIVE="http://ftpmirror.gnu.org/gdb/gdb-${GDB_VERSION}.tar.xz"
Patrick Georgi85bb9462016-02-11 17:22:16 +010063IASL_ARCHIVE="https://acpica.org/sites/acpica/files/acpica-unix2-${IASL_VERSION}.tar.gz"
Patrick Georgi53c388f2015-03-07 09:55:18 +010064PYTHON_ARCHIVE="http://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz"
Stefan Reinauer498f3d02016-04-21 18:10:15 -070065EXPAT_ARCHIVE="http://downloads.sourceforge.net/sourceforge/expat/expat-${EXPAT_VERSION}.tar.bz2"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070066# CLANG toolchain archive locations
67LLVM_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/llvm-${CLANG_VERSION}.src.tar.xz"
68CFE_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/cfe-${CLANG_VERSION}.src.tar.xz"
69CRT_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/compiler-rt-${CLANG_VERSION}.src.tar.xz"
70CTE_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/clang-tools-extra-${CLANG_VERSION}.src.tar.xz"
Stefan Reinauera851bbb2016-01-29 18:03:26 -080071MAKE_ARCHIVE="http://ftpmirror.gnu.org/make/make-${MAKE_VERSION}.tar.bz2"
Stefan Reinauer074d9132009-09-26 16:43:17 +000072
Martin Roth58c68d52016-03-01 15:22:42 -070073ALL_ARCHIVES="$GMP_ARCHIVE $MPFR_ARCHIVE $MPC_ARCHIVE $LIBELF_ARCHIVE \
74 $GCC_ARCHIVE $BINUTILS_ARCHIVE $GDB_ARCHIVE $IASL_ARCHIVE \
75 $PYTHON_ARCHIVE $EXPAT_ARCHIVE $LLVM_ARCHIVE $CFE_ARCHIVE \
76 $CRT_ARCHIVE $CTE_ARCHIVE $MAKE_ARCHIVE"
77
Stefan Reinauer699ac272015-06-05 12:43:28 -070078# GCC toolchain directories
Stefan Reinauer074d9132009-09-26 16:43:17 +000079GMP_DIR="gmp-${GMP_VERSION}"
80MPFR_DIR="mpfr-${MPFR_VERSION}"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000081MPC_DIR="mpc-${MPC_VERSION}"
82LIBELF_DIR="libelf-${LIBELF_VERSION}"
Stefan Reinauer074d9132009-09-26 16:43:17 +000083GCC_DIR="gcc-${GCC_VERSION}"
84BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
85GDB_DIR="gdb-${GDB_VERSION}"
Patrick Georgi85bb9462016-02-11 17:22:16 +010086IASL_DIR="acpica-unix2-${IASL_VERSION}"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +010087PYTHON_DIR="Python-${PYTHON_VERSION}"
88EXPAT_DIR="expat-${EXPAT_VERSION}"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070089# CLANG toolchain directories
90LLVM_DIR="llvm-${CLANG_VERSION}.src"
91CFE_DIR="cfe-${CLANG_VERSION}.src"
92CRT_DIR="compiler-rt-${CLANG_VERSION}.src"
93CTE_DIR="clang-tools-extra-${CLANG_VERSION}.src"
Stefan Reinauera851bbb2016-01-29 18:03:26 -080094MAKE_DIR="make-${MAKE_VERSION}"
Stefan Reinauer074d9132009-09-26 16:43:17 +000095
Patrick Georgi6321f522015-02-27 23:36:26 +010096unset MAKELEVEL MAKEFLAGS
97
Patrick Georgi1861ff72011-10-31 12:15:55 +010098red='\033[0;31m'
99RED='\033[1;31m'
100green='\033[0;32m'
101GREEN='\033[1;32m'
102blue='\033[0;34m'
103BLUE='\033[1;34m'
104cyan='\033[0;36m'
105CYAN='\033[1;36m'
106NC='\033[0m' # No Color
Stefan Reinauer074d9132009-09-26 16:43:17 +0000107
zbaoe05c1ea2015-08-05 23:10:05 -0400108UNAME=$(uname | grep -iq cygwin && echo Cygwin || uname)
Martin Roth88463822016-01-07 11:03:36 -0700109HALT_FOR_TOOLS=0
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700110
Patrick Georgi198d23c2012-12-08 08:02:44 +0100111normalize_dirs()
112{
113 mkdir -p $DESTDIR$TARGETDIR/lib
114 test -d $DESTDIR$TARGETDIR/lib32 && mv $DESTDIR$TARGETDIR/lib32/* $DESTDIR$TARGETDIR/lib
115 test -d $DESTDIR$TARGETDIR/lib64 && mv $DESTDIR$TARGETDIR/lib64/* $DESTDIR$TARGETDIR/lib
116 rmdir -p $DESTDIR$TARGETDIR/lib32 $DESTDIR$TARGETDIR/lib64
117
118 perl -pi -e "s,/lib32,/lib," $DESTDIR$TARGETDIR/lib/*.la
119 perl -pi -e "s,/lib64,/lib," $DESTDIR$TARGETDIR/lib/*.la
120}
121
Stefan Reinauer93a96302015-06-15 12:36:53 -0700122please_install()
123{
Martin Roth88463822016-01-07 11:03:36 -0700124 HALT_FOR_TOOLS=1
Patrick Georgi5da95dc2015-07-17 23:33:05 +0200125 test -r /etc/os-release && . /etc/os-release
Stefan Reinauer93a96302015-06-15 12:36:53 -0700126 case "$ID_LIKE" in
127 debian) solution="sudo apt-get install $1" ;;
128 suse) solution="sudo zypper install $1" ;;
129 *) solution="using your OS packaging system" ;;
130 esac
131
Martin Roth591790f2016-03-20 20:39:04 -0600132 printf "${RED}ERROR:${red} Missing tool: Please install \'$1\'. (eg $solution)${NC}\n" >&2
Martin Rothe3963172016-01-06 13:54:32 -0700133 if [ -n "$2" ]; then
Martin Roth591790f2016-03-20 20:39:04 -0600134 printf "${RED}ERROR:${red} or install \'$2\'.${NC}\n" >&2
Martin Rothe3963172016-01-06 13:54:32 -0700135 fi
Stefan Reinauer93a96302015-06-15 12:36:53 -0700136}
137
Patrick Georgib7062882015-02-24 10:52:14 +0100138searchtool()
Stefan Reinauer074d9132009-09-26 16:43:17 +0000139{
140 # $1 short name
Patrick Georgib7062882015-02-24 10:52:14 +0100141 # $2 search string
142 # $3 soft fail if set
Martin Rothe3963172016-01-06 13:54:32 -0700143 # $4 alternative package to install on failure
Patrick Georgib7062882015-02-24 10:52:14 +0100144 # result: file name of that tool on stdout
145 # or no output if nothing suitable was found
146 search=GNU
147 if [ -n "$2" ]; then
148 search="$2"
149 fi
Stefan Reinauer074d9132009-09-26 16:43:17 +0000150 for i in "$1" "g$1" "gnu$1"; do
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700151 if [ -x "$(which $i 2>/dev/null)" ]; then
152 if [ "$(cat /dev/null | $i --version 2>&1 | grep -c "$search")" \
153 -gt 0 ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000154 echo $i
155 return
156 fi
157 fi
158 done
Andrew Wub67e9a12014-04-28 18:13:44 +0800159 # A workaround for OSX 10.9 and some BSDs, whose nongnu
160 # patch and tar also work.
Patrick Georgi7cb26b42015-04-28 21:29:22 +0200161 if [ $UNAME = "Darwin" -o $UNAME = "FreeBSD" -o $UNAME = "NetBSD" -o $UNAME = "OpenBSD" ]; then
Patrick Georgib7062882015-02-24 10:52:14 +0100162 if [ "$1" = "patch" -o "$1" = "tar" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700163 if [ -x "$(which $1 2>/dev/null)" ]; then
Zheng Bao36156ff2012-09-28 16:18:58 +0800164 echo $1
165 return
166 fi
167 fi
168 fi
zbao5cf758d2015-09-01 22:28:57 -0400169 if echo $1 | grep -q "sum" ; then
170 algor=$(echo $1 | sed -e 's,sum,,')
171 if [ -x "$(which $1 2>/dev/null)" ]; then
172 #xxxsum [file]
173 echo $1
174 return
175 elif [ -x "$(which $algor 2>/dev/null)" ]; then
176 #xxx [file]
177 echo $algor
178 return
179 elif [ -x "$(which openssl 2>/dev/null)" ]; then
180 #openssl xxx [file]
181 echo openssl $algor
182 return
183 elif [ -x "$(which cksum 2>/dev/null)" ]; then
184 #cksum -a xxx [file]
185 #cksum has special options in NetBSD. Actually, NetBSD will use the second case above.
186 echo "buildgcc" | cksum -a $algor > /dev/null 2>/dev/null && \
187 echo cksum -a $algor
188 return
zbao939dc842015-04-30 04:44:07 +0800189 fi
190 fi
Martin Rothe3963172016-01-06 13:54:32 -0700191
Martin Roth88463822016-01-07 11:03:36 -0700192 [ -z "$3" ] && please_install $1 $4
Patrick Georgib7062882015-02-24 10:52:14 +0100193 false
Stefan Reinauer074d9132009-09-26 16:43:17 +0000194}
195
Martin Roth591790f2016-03-20 20:39:04 -0600196# Run a compile check of the specified library option to see if it's installed
197check_for_library() {
198 local LIBRARY_FLAGS=$1
Aaron Durbin0ac4e5a2016-04-07 16:56:25 -0500199 local LIBRARY_PACKAGES="$2"
Martin Roth591790f2016-03-20 20:39:04 -0600200 local LIBTEST_FILE=.libtest
201
202 echo "int main(int argc, char **argv) { (void) argc; (void) argv; return 0; }" > "${LIBTEST_FILE}.c"
203
204 cc $CFLAGS $LIBRARY_FLAGS "${LIBTEST_FILE}.c" -o "${LIBTEST_FILE}" >/dev/null 2>&1 || \
205 please_install "$LIBRARY_PACKAGES"
206 rm -rf "${LIBTEST_FILE}.c" "${LIBTEST_FILE}"
207}
208
Nico Huberaee7f462016-09-20 12:55:19 +0200209buildcc_major() {
210 echo "${GCC_VERSION}" | cut -d. -f1
211}
212
213buildcc_version() {
214 echo "${GCC_VERSION}" | cut -d. -f1-2
215}
216
217hostcc_major() {
218 (echo __GNUC__ | ${CC} -E - 2>/dev/null || echo 0) | tail -1
219}
220
221hostcc_minor() {
222 (echo __GNUC_MINOR__ | ${CC} -E - 2>/dev/null || echo 0) | tail -1
223}
224
225hostcc_version() {
226 printf "%d.%d" "$(hostcc_major)" "$(hostcc_minor)"
227}
228
Nico Huber75797162016-09-20 13:01:57 +0200229hostcc_has_gnat1() {
230 [ -x "$(${CC} -print-prog-name=gnat1)" ]
231}
232
233ada_requested() {
234 echo "${LANGUAGES}" | grep -q '\<ada\>'
235}
236
237check_gnat() {
238 if hostcc_has_gnat1; then
239 if [ "$(hostcc_version)" != "$(buildcc_version)" -a "${BOOTSTRAP}" != "1" ]; then
240 printf "\n${RED}ERROR:${red} Building the Ada compiler (gnat $(buildcc_version)) "
241 printf "with a different host compiler\n version ($(hostcc_version)) "
242 printf "requires bootstrapping (-b).${NC}\n\n"
243 HALT_FOR_TOOLS=1
244 fi
Nico Huber04a1f6a2016-09-20 13:07:49 +0200245 else
246 please_install gnat
Nico Huber75797162016-09-20 13:01:57 +0200247 fi
248}
249
Nico Huber156d87c2016-09-20 12:59:53 +0200250check_cc() {
251 if is_package_enabled "GCC"; then
252 if [ "$(hostcc_major)" != "$(buildcc_major)" -a "${BOOTSTRAP}" != "1" ]; then
253 printf "\n${red}warning: Building GCC $(buildcc_version) with a different major "
254 printf "version ($(hostcc_version)).\n"
255 printf " Bootstrapping (-b) is recommended.${NC}\n\n"
256 fi
Nico Huber75797162016-09-20 13:01:57 +0200257 if ada_requested; then
258 check_gnat
259 fi
Nico Huber156d87c2016-09-20 12:59:53 +0200260 fi
261}
262
zbao06e85ac2015-08-24 22:08:36 -0400263check_sum() {
264 test -z "$CHECKSUM" || \
265 test "$(cat sum/$1.cksum 2>/dev/null | sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@')" = \
266 "$($CHECKSUM tarballs/$1 2>/dev/null | sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@')"
267}
268
269compute_sum() {
270 test ! -f sum/$1.cksum && test -f tarballs/$1 && \
271 (test -z "$CHECKSUM" || $CHECKSUM tarballs/$1 > sum/$1.cksum ) && \
272 printf "(checksum created. ${RED}Note. Please upload sum/$1.cksum if the corresponding archive is upgraded.)${NC}"
273}
274
zbao282dd952015-09-06 05:00:04 -0400275download_showing_percentage() {
276 url=$1
277 printf " ..${red} 0%%"
278 wget --no-check-certificate $url 2>&1 | while read line; do
zbao282dd952015-09-06 05:00:04 -0400279 echo $line | grep -o "[0-9]\+%" | awk '{printf("\b\b\b\b%4s", $1)}'
zbao282dd952015-09-06 05:00:04 -0400280 done
Zheng Baobb003c82016-08-05 13:41:51 +0800281 printf "${NC}"
zbao282dd952015-09-06 05:00:04 -0400282}
283
Stefan Reinauer88935482015-06-05 09:51:10 -0700284download() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700285 package=$1
286 archive="$(eval echo \$$package"_ARCHIVE")"
Stefan Reinauer699ac272015-06-05 12:43:28 -0700287
Stefan Reinauerd7649122015-06-09 11:44:25 -0700288 FILE=$(basename $archive)
Stefan Reinauer88935482015-06-05 09:51:10 -0700289 printf " * $FILE "
290
zbaod22b9ea2015-08-30 23:35:16 -0400291 if test -f tarballs/$FILE && check_sum $FILE ; then
292 printf "(cached)"
293 else
Paul Menzelb2ba3952015-06-12 11:58:14 +0200294 printf "(downloading from $archive)"
Stefan Reinauer88935482015-06-05 09:51:10 -0700295 rm -f tarballs/$FILE
296 cd tarballs
zbao282dd952015-09-06 05:00:04 -0400297 download_showing_percentage $archive
Stefan Reinauer88935482015-06-05 09:51:10 -0700298 cd ..
zbao06e85ac2015-08-24 22:08:36 -0400299 compute_sum $FILE
zbaod22b9ea2015-08-30 23:35:16 -0400300 fi
301
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700302 if [ ! -f tarballs/$FILE ]; then
zbao282dd952015-09-06 05:00:04 -0400303 printf "\n${RED}Failed to download $FILE.${NC}\n"
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700304 exit 1
305 fi
Stefan Reinauer88935482015-06-05 09:51:10 -0700306 printf "\n"
307}
308
309unpack_and_patch() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700310 package=$1
311 archive="$(eval echo \$$package"_ARCHIVE")"
312 dir="$(eval echo \$$package"_DIR")"
Stefan Reinauer88935482015-06-05 09:51:10 -0700313 test -d ${dir} && test -f ${dir}/.unpack_success || (
Stefan Reinauerd7649122015-06-09 11:44:25 -0700314 printf " * $(basename $archive)\n"
Stefan Reinauer88935482015-06-05 09:51:10 -0700315 FLAGS=zxf
Stefan Reinauerd7649122015-06-09 11:44:25 -0700316 suffix=$(echo $archive | sed 's,.*\.,,')
Martin Roth95f7b222016-03-20 12:38:48 -0600317 if [ "$suffix" = "gz" ] && [ -n "$PIGZ" ]; then FLAGS="-I pigz -xf"
318 elif [ "$suffix" = "gz" ]; then FLAGS=zxf
319 elif [ "$suffix" = "bz2" ] && [ -n "$LBZIP2" ]; then FLAGS="-I lbzip2 -xf"
320 elif [ "$suffix" = "bz2" ]; then FLAGS=jxf
321 elif [ "$suffix" = "xz" ]; then FLAGS="--xz -xf"
322 elif [ "$suffix" = "lzma" ]; then FLAGS="--lzma -xf"
323 fi
Stefan Reinauerd7649122015-06-09 11:44:25 -0700324 $TAR $FLAGS tarballs/$(basename $archive)
Stefan Reinauer88935482015-06-05 09:51:10 -0700325 for patch in patches/${dir}_*.patch; do
326 test -r $patch || continue
Stefan Reinauerd7649122015-06-09 11:44:25 -0700327 printf " o $(basename $patch)\n"
Nico Huber4b48ed82016-07-27 00:28:03 +0200328 (cd ${dir} && $PATCH -s -N -p1 <../${patch}) || {
Stefan Reinauer88935482015-06-05 09:51:10 -0700329 printf "\n${RED}Failed $patch.${NC}\n"
Stefan Reinauer916b8452015-06-09 11:54:49 -0700330 exit 1
331 }
Stefan Reinauer88935482015-06-05 09:51:10 -0700332 done
333 touch ${dir}/.unpack_success
334 )
335}
336
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700337fn_exists()
338{
Patrick Georgic8665912015-06-11 21:04:31 +0200339 type $1 >/dev/null 2>&1
Patrick Georgi74c06422015-03-25 18:40:13 +0100340}
341
Patrick Georgif2c15f52015-06-11 21:07:31 +0200342is_package_enabled()
343{
344 echo "$PACKAGES" |grep -q "\<$1\>"
345}
346
Martin Roth444ece22016-04-01 18:46:29 -0600347package_uses_targetarch()
348{
Stefan Reinauera344b682016-04-21 19:29:51 -0700349 if [ "$1" = "GCC" ] || [ "$1" = "GDB" ] || [ "$1" = "BINUTILS" ]; then
Martin Roth444ece22016-04-01 18:46:29 -0600350 true
351 else
352 false
353 fi
354}
355
Nico Huber11ea2b32016-01-26 16:09:31 +0100356generic_build()
357{
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700358 package=$1
Nico Huber11ea2b32016-01-26 16:09:31 +0100359 host_target=$2
360 builddir=$3
361 success=$4
Martin Rothc295d5e2016-11-14 11:56:11 -0700362 version=$5
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700363
364 fn_exists build_$package || return
365
Nico Huber11ea2b32016-01-26 16:09:31 +0100366 mkdir -p "$builddir"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700367
Nico Huber11ea2b32016-01-26 16:09:31 +0100368 if [ -f "$success" ]; then
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700369 printf "Skipping $package as it is already built\n"
370 else
371 printf "Building $package $version ... "
Nico Huber11ea2b32016-01-26 16:09:31 +0100372 DIR="$PWD"
373 cd "$builddir"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700374 rm -f .failed
Nico Huber11ea2b32016-01-26 16:09:31 +0100375 build_${package} $host_target > build.log 2>&1
376 cd "$DIR"
377 if [ ! -f "$builddir/.failed" ]; then
378 touch "$success";
379 else
380 printf "${RED}failed${NC}. Check $builddir/build.log.\n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700381 exit 1
382 fi
383 printf "${green}ok${NC}\n"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700384 fi
385}
386
Nico Huber11ea2b32016-01-26 16:09:31 +0100387build_for_host()
388{
Martin Rothc295d5e2016-11-14 11:56:11 -0700389 package="$1"
390 version="$(eval echo \$$package"_VERSION")"
Patrick Georgi1b593e52016-11-25 15:27:41 +0100391 generic_build "$package" host "build-$package" "${DESTDIR}${TARGETDIR}/.${package}.${version}.success" "$version"
Nico Huber11ea2b32016-01-26 16:09:31 +0100392}
393
394build_for_target()
395{
Martin Rothc295d5e2016-11-14 11:56:11 -0700396 package="$1"
397 version="$(eval echo \$$package"_VERSION")"
Patrick Georgi1b593e52016-11-25 15:27:41 +0100398 generic_build "$package" target "build-${TARGETARCH}-$package" "${DESTDIR}${TARGETDIR}/.${TARGETARCH}-${package}.${version}.success" "$version"
Nico Huber11ea2b32016-01-26 16:09:31 +0100399}
400
401build()
402{
403 if package_uses_targetarch $1; then
Patrick Georgi1b593e52016-11-25 15:27:41 +0100404 if [ $BOOTSTRAP -eq 1 -a ! -f "${DESTDIR}${TARGETDIR}/.GCC.success" ]; then
Nico Huber234d2462016-01-26 16:10:17 +0100405 build_for_host GCC
406 fi
Nico Huber11ea2b32016-01-26 16:09:31 +0100407 build_for_target $1
408 else
409 build_for_host $1
410 fi
411}
412
Zheng Baobb003c82016-08-05 13:41:51 +0800413quit()
414{
415 printf "${NC}Stop\n"
416 exit 1
417}
418
Stefan Reinauer074d9132009-09-26 16:43:17 +0000419cleanup()
420{
Stefan Reinauer88e83e52016-04-06 15:39:48 -0700421 if [ $SAVETEMPS -ne 0 ]; then
422 printf "Leaving temporary files around... ${green}ok${NC}\n"
423 return
424 fi
425
Stefan Reinauer074d9132009-09-26 16:43:17 +0000426 printf "Cleaning up temporary files... "
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700427 for package in $PACKAGES; do
Martin Roth444ece22016-04-01 18:46:29 -0600428 rm -rf build-${TARGETARCH}-$package build-$package $(eval echo \$$package"_DIR")
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700429 done
zbaoff3f15c2015-08-26 22:00:31 -0400430 rm -f getopt
Stefan Reinauer074d9132009-09-26 16:43:17 +0000431 printf "${green}ok${NC}\n"
432}
433
434myhelp()
435{
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700436 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>] [-C] [-G] [-S]\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000437 printf " $0 [-V|--version]\n"
438 printf " $0 [-h|--help]\n\n"
439
440 printf "Options:\n"
441 printf " [-V|--version] print version number and exit\n"
442 printf " [-h|--help] print this help and exit\n"
443 printf " [-c|--clean] remove temporary files before build\n"
444 printf " [-t|--savetemps] don't remove temporary files after build\n"
Patrick Georgic1a75b12011-11-04 21:37:14 +0100445 printf " [-y|--ccache] Use ccache when building cross compiler\n"
Nico Huber152e78e2016-09-20 15:38:40 +0200446 printf " [-n|--nocolor] don't print color codes in output\n"
447 printf " [-u|--urls] print the urls for all packages\n"
Patrick Georgi73166c72010-04-14 20:42:42 +0000448 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
Martin Roth588c79d2016-03-20 12:03:20 -0600449 printf " [-s]--supported <tool> print supported version of a tool\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000450 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
Martin Roth588c79d2016-03-20 12:03:20 -0600451 printf " (defaults to $TARGETDIR)\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000452 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100453 printf " (for RPM builds, default unset)\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700454 printf " [-P|--package <package>] Build a specific package: GCC, CLANG, IASL, GDB\n"
455 printf " (defaults to $PACKAGE)\n"
456 printf "GCC specific options:\n"
Nico Huber234d2462016-01-26 16:10:17 +0100457 printf " [-b|--bootstrap] bootstrap the host compiler before building\n"
458 printf " the cross compiler\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700459 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
460 printf " (defaults to $TARGETARCH)\n"
Nico Huberbb313bf2015-09-08 12:30:27 +0200461 printf " [-l|--languages <languages>] comma separated list of target languages\n"
Nico Huber66c2c1a2016-09-20 13:09:29 +0200462 printf " (defaults to $DEFAULT_LANGUAGES)\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700463 printf "GDB specific options:\n"
464 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
465 printf " (defaults to $TARGETARCH)\n"
466 printf " [-S|--scripting] build scripting support for GDB\n\n"
Martin Rothd1da74f2015-06-21 12:06:28 -0600467 printf "Platforms for GCC & GDB:\n"
Martin Roth3e0f74d2016-03-08 12:07:04 -0700468 printf " x86_64 i386-elf i386-mingw32 mipsel-elf riscv-elf arm aarch64\n"
Jonathan Neuschäferf14f6402016-03-11 20:22:23 +0100469 printf " powerpc64le-linux-gnu nds32le-elf\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000470}
471
Martin Rothedf965a2015-11-25 16:58:33 -0700472printversion() {
473 printf "${blue}Welcome to the ${red}coreboot${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
474}
475
Stefan Reinauer074d9132009-09-26 16:43:17 +0000476myversion()
477{
Martin Rothedf965a2015-11-25 16:58:33 -0700478 printversion
Stefan Reinauer074d9132009-09-26 16:43:17 +0000479
480 cat << EOF
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000481Copyright (C) 2008-2010 by coresystems GmbH
Marc Jones2aac3f62011-08-08 16:07:50 -0600482Copyright (C) 2011 by Sage Electronic Engineering
Stefan Reinauer074d9132009-09-26 16:43:17 +0000483
484This program is free software; you can redistribute it and/or modify
485it under the terms of the GNU General Public License as published by
486the Free Software Foundation; version 2 of the License.
487
488This program is distributed in the hope that it will be useful,
489but WITHOUT ANY WARRANTY; without even the implied warranty of
490MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
491GNU General Public License for more details.
492
493EOF
494}
495
Nico Huber5f72f962016-01-31 23:05:19 +0100496have_hostcflags_from_gmp() {
497 grep -q __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h >/dev/null 2>&1
498}
499
500set_hostcflags_from_gmp() {
501 # Now set CFLAGS to match GMP CFLAGS but strip out -pedantic
502 # as GCC 4.6.x fails if it's there.
Idwer Volleringba349ab2016-07-24 02:10:19 +0200503 export HOSTCFLAGS="$(grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2 |\
504 sed s,-pedantic,,)"
Nico Huber5f72f962016-01-31 23:05:19 +0100505}
506
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700507build_GMP() {
Paul Kocialkowski7899db22016-06-17 21:41:00 -0700508 CC="$CC" ../${GMP_DIR}/configure --disable-shared --enable-fat \
Nico Huber5f72f962016-01-31 23:05:19 +0100509 --prefix=$TARGETDIR $OPTIONS \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700510 || touch .failed
511 $MAKE $JOBS || touch .failed
512 $MAKE install DESTDIR=$DESTDIR || touch .failed
513
514 normalize_dirs
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700515
Nico Huber5f72f962016-01-31 23:05:19 +0100516 set_hostcflags_from_gmp
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700517}
518
519build_MPFR() {
520 test $UNAME = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
521 CC="$CC" ../${MPFR_DIR}/configure --disable-shared --prefix=$TARGETDIR \
522 --infodir=$TARGETDIR/info \
523 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || \
524 touch .failed
525 $MAKE $JOBS || touch .failed
526 $MAKE install DESTDIR=$DESTDIR || touch .failed
527
528 normalize_dirs
529
530 # work around build problem of libgmp.la
531 if [ "$DESTDIR" != "" ]; then
532 perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
533 fi
534}
535
536build_MPC() {
537 CC="$CC" ../${MPC_DIR}/configure --disable-shared --prefix=$TARGETDIR \
538 --infodir=$TARGETDIR/info --with-mpfr=$DESTDIR$TARGETDIR \
539 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || \
540 touch .failed
541 $MAKE $JOBS || touch .failed
542 $MAKE install DESTDIR=$DESTDIR || touch .failed
543
544 normalize_dirs
545}
546
547build_LIBELF() {
548 CC="$CC" CFLAGS="$HOSTCFLAGS" libelf_cv_elf_h_works=no \
549 ../${LIBELF_DIR}/configure --disable-shared --prefix=$TARGETDIR \
550 --infodir=$TARGETDIR/info CFLAGS="$HOSTCFLAGS" || touch .failed
551 $MAKE $JOBS || touch .failed
552 $MAKE install prefix=$DESTDIR$TARGETDIR || touch .failed
553
554 normalize_dirs
555}
556
557build_BINUTILS() {
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700558 if [ $TARGETARCH == "x86_64-elf" ]; then
559 ADDITIONALTARGET=",i386-elf"
560 fi
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700561 CC="$CC" ../binutils-${BINUTILS_VERSION}/configure --prefix=$TARGETDIR \
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700562 --target=${TARGETARCH} --enable-targets=${TARGETARCH}${ADDITIONALTARGET} \
563 --disable-werror --disable-nls --enable-lto --enable-gold \
Stefan Reinauer2fe2d3d2016-04-05 02:14:48 -0700564 --enable-interwork --enable-multilib \
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700565 --enable-plugins --enable-multilibs CFLAGS="$HOSTCFLAGS" || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700566 $MAKE $JOBS || touch .failed
567 $MAKE install DESTDIR=$DESTDIR || touch .failed
568}
569
Nico Huber234d2462016-01-26 16:10:17 +0100570bootstrap_GCC() {
571 CC="$CC" \
572 CFLAGS="$HOSTCFLAGS" \
573 CFLAGS_FOR_BUILD="$HOSTCFLAGS" \
574 CFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
575 CXXFLAGS="$HOSTCFLAGS" \
576 CXXFLAGS_FOR_BUILD="$HOSTCFLAGS" \
577 CXXFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
578 ../gcc-${GCC_VERSION}/configure \
579 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
580 --enable-bootstrap \
581 --disable-werror --disable-nls \
582 --disable-shared --disable-multilib \
583 --disable-libssp --disable-libquadmath --disable-libcc1 \
584 ${GCC_OPTIONS} --enable-languages="${LANGUAGES}" \
585 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
586 --with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
587 --with-pkgversion="coreboot bootstrap v$CROSSGCC_VERSION $CROSSGCC_DATE" \
588 && \
589 $MAKE $JOBS BOOT_CFLAGS="$HOSTCFLAGS" BUILD_CONFIG="" bootstrap && \
590 $MAKE install-gcc \
591 install-target-libgcc \
592 maybe-install-target-libada \
593 maybe-install-target-libstdc++-v3 \
594 DESTDIR=$DESTDIR && \
595 ln -s gcc $DESTDIR$TARGETDIR/bin/cc || touch .failed
596}
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700597
Nico Huber234d2462016-01-26 16:10:17 +0100598build_cross_GCC() {
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700599 # Work around crazy code generator in GCC that confuses CLANG.
Aaron Durbinb2229dc2016-04-07 16:58:10 -0500600 $CC --version | grep clang >/dev/null 2>&1 && \
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700601 HOSTCFLAGS="$HOSTCFLAGS -fbracket-depth=1024"
602
Stefan Reinauerd0f83722015-06-09 11:52:28 -0700603 # GCC does not honor HOSTCFLAGS at all. CFLAGS are used for
604 # both target and host object files.
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700605 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
606 # but it does not seem to work properly. At least the host library
607 # libiberty is not compiled with CFLAGS_FOR_BUILD.
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700608 # Also set the CXX version of the flags because GCC is now compiled
609 # using C++.
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100610 CC="$CC" CFLAGS_FOR_TARGET="-O2 -Dinhibit_libc" CFLAGS="$HOSTCFLAGS" \
Stefan Reinauer45f77b82016-04-03 20:52:01 -0700611 CFLAGS_FOR_BUILD="$HOSTCFLAGS" CXXFLAGS="$HOSTCFLAGS" \
612 CXXFLAGS_FOR_BUILD="$HOSTCFLAGS" ../gcc-${GCC_VERSION}/configure \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700613 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
614 --target=${TARGETARCH} --disable-werror --disable-shared \
Stefan Reinauer2f97ebc2015-06-09 12:24:54 -0700615 --enable-lto --enable-plugins --enable-gold --enable-ld=default \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700616 --disable-libssp --disable-bootstrap --disable-nls \
617 --disable-libquadmath --without-headers \
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100618 --disable-threads \
Patrick Georgi1fc02d12016-03-23 21:44:43 +0100619 --enable-interwork --enable-multilib --enable-targets=all \
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100620 --disable-libatomic --disable-libcc1 --disable-decimal-float \
Nico Huberbb313bf2015-09-08 12:30:27 +0200621 ${GCC_OPTIONS} --enable-languages="${LANGUAGES}" \
Ronald G. Minnich7ee16b72016-02-12 21:54:59 +0000622 --with-system-zlib \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700623 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
624 --with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
625 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE" \
626 || touch .failed
627 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-gcc || touch .failed
628 $MAKE install-gcc DESTDIR=$DESTDIR || touch .failed
629
Stefan Reinauerd7649122015-06-09 11:44:25 -0700630 if [ "$(echo $TARGETARCH | grep -c -- -mingw32)" -eq 0 ]; then
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700631 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-target-libgcc || touch .failed
632 $MAKE install-target-libgcc DESTDIR=$DESTDIR || touch .failed
633 fi
634}
635
Nico Huber234d2462016-01-26 16:10:17 +0100636build_GCC() {
637 if [ "$1" = host ]; then
638 bootstrap_GCC
639 else
640 build_cross_GCC
641 fi
642}
643
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700644build_EXPAT() {
645 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${EXPAT_DIR}/configure --disable-shared \
Stefan Reinauera344b682016-04-21 19:29:51 -0700646 --prefix=$TARGETDIR || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700647 $MAKE || touch .failed
648 $MAKE install DESTDIR=$DESTDIR || touch .failed
649
650 normalize_dirs
651}
652
653build_PYTHON() {
654 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${PYTHON_DIR}/configure --prefix=$TARGETDIR \
Stefan Reinauera344b682016-04-21 19:29:51 -0700655 || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700656 $MAKE $JOBS || touch .failed
657 $MAKE install DESTDIR=$DESTDIR || touch .failed
658
659 normalize_dirs
660}
661
662build_GDB() {
663 export PYTHONHOME=$DESTDIR$TARGETDIR
664 if [ $(uname) != "FreeBSD" -a $(uname) != "NetBSD" ]; then
665 LIBDL="-ldl"
666 fi
667 LDFLAGS="-Wl,-rpath,\$\$ORIGIN/../lib/ -L$DESTDIR$TARGETDIR/lib \
668 -lpthread $LIBDL -lutil" \
669 CC="$CC" CFLAGS="$HOSTCFLAGS -I$DESTDIR$TARGETDIR/include" \
Stefan Reinauer7b40e422015-06-09 11:56:32 -0700670 ../${GDB_DIR}/configure --prefix=$TARGETDIR \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700671 --target=${TARGETARCH} --disable-werror --disable-nls
672 $MAKE $JOBS || touch .failed
673 $MAKE install DESTDIR=$DESTDIR || touch .failed
674}
675
676build_IASL() {
677 RDIR=$PWD
678 cd ../$IASL_DIR/generate/unix
679 CFLAGS="$HOSTCFLAGS"
680 HOST="_LINUX"
681 test $UNAME = "Darwin" && HOST="_APPLE"
682 test $UNAME = "FreeBSD" && HOST="_FreeBSD"
683 test $UNAME = "Cygwin" && HOST="_CYGWIN"
Martin Roth6f656132015-12-08 11:14:10 -0700684 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 -0700685 rm -f $DESTDIR$TARGETDIR/bin/iasl || touch $RDIR/.failed
686 cp bin/iasl $DESTDIR$TARGETDIR/bin || touch $RDIR/.failed
687}
688
689build_LLVM() {
690 cd ..
691 ln -sf $PWD/$CFE_DIR $LLVM_DIR/tools/clang
692 ln -sf $PWD/$CTE_DIR $LLVM_DIR/tools/clang/tools/extra
693 ln -sf $PWD/$CRT_DIR $LLVM_DIR/projects/compiler-rt
694 cd -
695
696 $CMAKE -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$DESTDIR$TARGETDIR \
Martin Rothfb5647a2015-12-18 09:26:45 -0700697 -DCLANG_VENDOR="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE - " \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700698 -DCMAKE_BUILD_TYPE=Release ../$LLVM_DIR || touch .failed
699 $MAKE $JOBS || touch .failed
700 $MAKE install || touch .failed
701
702 cp -a ../$CFE_DIR/tools/scan-build/* $DESTDIR$TARGETDIR/bin
703 cp -a ../$CFE_DIR/tools/scan-view/* $DESTDIR$TARGETDIR/bin
Stefan Reinauere9e6e3d2015-07-07 00:20:42 +0200704
705 # create symlinks to work around broken --print-librt-file-name
706 # when used with -target.
707 cd $DESTDIR$TARGETDIR/lib/clang/${CLANG_VERSION}/lib
708 for i in */libclang_rt.builtins*.a; do
709 ln -s $i .
710 done
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700711}
712
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800713build_MAKE() {
714 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${MAKE_DIR}/configure --prefix=$TARGETDIR \
715 --disable-nls || touch .failed
716 $MAKE $JOBS || touch .failed
717 $MAKE install DESTDIR=$DESTDIR || touch .failed
718
719 normalize_dirs
720}
721
Martin Rothedf965a2015-11-25 16:58:33 -0700722print_stable() {
723 case "$PRINTSTABLE" in
724 AUTOCONF|autoconf) printf "%s\n" "$GCC_AUTOCONF_VERSION";;
725 BINUTILS|binutils) printf "%s\n" "$BINUTILS_VERSION";;
726 CLANG|clang) printf "%s\n" "$CLANG_VERSION";;
727 EXPAT|expat) printf "%s\n" "$EXPAT_VERSION";;
728 GCC|gcc) printf "%s\n" "$GCC_VERSION";;
729 GDB|gdb) printf "%s\n" "$GDB_VERSION";;
730 GMP|gmp) printf "%s\n" "$GMP_VERSION";;
731 IASL|iasl) printf "%s\n" "$IASL_VERSION";;
732 LIBELF|libelf) printf "%s\n" "$LIBELF_VERSION";;
733 MPC|mpc) printf "%s\n" "$MPC_VERSION";;
734 MPFR|mpfr) printf "%s\n" "$MPFR_VERSION";;
735 PYTHON|python) printf "%s\n" "$PYTHON_VERSION";;
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800736 MAKE|make) printf "%s\n" "$MAKE_VERSION";;
Martin Rothedf965a2015-11-25 16:58:33 -0700737 *) printf "Unknown tool %s\n" "$PRINTSTABLE";;
738 esac
739}
Stefan Reinauer074d9132009-09-26 16:43:17 +0000740
Zheng Baobb003c82016-08-05 13:41:51 +0800741trap quit 1 2 3 15
742
Patrick Georgi6bba29f2010-01-29 17:40:52 +0000743# Look if we have getopt. If not, build it.
744export PATH=$PATH:.
745getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
746
Stefan Reinauer074d9132009-09-26 16:43:17 +0000747# parse parameters.. try to find out whether we're running GNU getopt
Stefan Reinauerd7649122015-06-09 11:44:25 -0700748getoptbrand="$(getopt -V | sed -e '1!d' -e 's,^\(......\).*,\1,')"
Peter Stuge09377b72011-08-21 06:24:55 +0200749if [ "${getoptbrand}" = "getopt" ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000750 # Detected GNU getopt that supports long options.
Nico Hubercc414dd2016-09-20 13:59:38 +0200751 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 +0200752 getopt_ret=$?
Nico Hubercc414dd2016-09-20 13:59:38 +0200753 eval set -- "$args"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000754else
755 # Detected non-GNU getopt
Nico Huber152e78e2016-09-20 15:38:40 +0200756 args=$(getopt Vhcd:bp:l:P:j:D:tSys:un $*)
Nico Huber78df0bf2016-09-20 14:11:53 +0200757 getopt_ret=$?
Stefan Reinauer074d9132009-09-26 16:43:17 +0000758 set -- $args
759fi
760
Nico Huber78df0bf2016-09-20 14:11:53 +0200761if [ $getopt_ret != 0 ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000762 myhelp
763 exit 1
764fi
765
766while true ; do
767 case "$1" in
768 -V|--version) shift; myversion; exit 0;;
769 -h|--help) shift; myhelp; exit 0;;
Patrick Georgi44af57a2013-12-19 22:06:22 +0100770 -c|--clean) shift; clean=1;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000771 -t|--savetemps) shift; SAVETEMPS=1;;
772 -d|--directory) shift; TARGETDIR="$1"; shift;;
Nico Huber234d2462016-01-26 16:10:17 +0100773 -b|--bootstrap) shift; BOOTSTRAP=1;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000774 -p|--platform) shift; TARGETARCH="$1"; shift;;
Nico Huberbb313bf2015-09-08 12:30:27 +0200775 -l|--languages) shift; LANGUAGES="$1"; shift;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000776 -D|--destdir) shift; DESTDIR="$1"; shift;;
Patrick Georgi73166c72010-04-14 20:42:42 +0000777 -j|--jobs) shift; JOBS="-j $1"; shift;;
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700778 -P|--package) shift; PACKAGE="$1"; shift;;
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100779 -S|--scripting) shift; SKIPPYTHON=0;;
Patrick Georgic1a75b12011-11-04 21:37:14 +0100780 -y|--ccache) shift; USECCACHE=1;;
Martin Rothedf965a2015-11-25 16:58:33 -0700781 -s|--supported) shift; PRINTSTABLE="$1"; shift;;
Nico Huber152e78e2016-09-20 15:38:40 +0200782 -u|--urls) shift; printf "%s\n" "$ALL_ARCHIVES"; exit 0;;
783 -n|--nocolor) shift; \
Martin Roth4a53db02016-03-01 15:21:07 -0700784 unset red RED green GREEN blue BLUE cyan CYAN NC;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200785 --) shift; break;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200786 *) break;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000787 esac
788done
789
Nico Huberbd74d562016-09-20 14:16:43 +0200790if [ $# -gt 0 ]; then
791 printf "Excessive arguments: $*\n"
792 myhelp
793 exit 1
794fi
795
Martin Rothedf965a2015-11-25 16:58:33 -0700796if [ -n "$PRINTSTABLE" ]; then
797 print_stable
798 exit 0
799fi
800
801#print toolchain builder version string as the header
802printversion
803
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700804case "$TARGETARCH" in
Timothy Pearsona9f62352015-12-30 18:59:00 -0600805 x86_64-elf) ;;
806 x86_64*) TARGETARCH=x86_64-elf;;
807 i386-elf) ;;
808 i386-mingw32) ;;
809 mipsel-elf) ;;
Iru Caiae8e3a02016-04-02 10:50:59 +0800810 riscv-elf) TARGETARCH=riscv64-elf;;
Patrick Georgi0a97d7e2016-01-25 09:51:22 +0100811 powerpc64*-linux*) ;;
Timothy Pearsona9f62352015-12-30 18:59:00 -0600812 i386*) TARGETARCH=i386-elf;;
Stefan Reinauer27522ad2016-01-29 17:31:34 -0800813 arm*) TARGETARCH=arm-eabi;;
Timothy Pearsona9f62352015-12-30 18:59:00 -0600814 aarch64*) TARGETARCH=aarch64-elf;;
Martin Roth3e0f74d2016-03-08 12:07:04 -0700815 nds32le-elf) ;;
Timothy Pearsona9f62352015-12-30 18:59:00 -0600816 *) printf "${red}WARNING: Unsupported architecture $TARGETARCH.${NC}\n\n"; ;;
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700817esac
818
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700819# Figure out which packages to build
Stefan Reinauer699ac272015-06-05 12:43:28 -0700820
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700821case "$PACKAGE" in
822 GCC|gcc)
823 echo "Target architecture is now $TARGETARCH"
824 NAME="${TARGETARCH} cross GCC"
Stefan Reinauerbd8b2e32015-06-17 11:55:02 -0700825 PACKAGES="GMP MPFR MPC LIBELF BINUTILS GCC"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700826 ;;
827 GDB|gdb)
828 NAME="${TARGETARCH} cross GDB"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700829 if [ $SKIPPYTHON -eq 0 ]; then
Ronald G. Minnichfb944f42016-02-27 04:06:42 +0000830 PACKAGES="EXPAT PYTHON GDB"
831 else
832 PACKAGES="EXPAT GDB"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700833 fi
834 ;;
835 CLANG|clang)
836 NAME=clang
837 PACKAGES="LLVM CFE CRT CTE"
David Hendrickscd5cdd32015-06-15 17:03:22 -0700838 CMAKE=$(searchtool cmake "cmake") || exit $?
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700839 ;;
840 IASL|iasl)
841 NAME="IASL ACPI compiler"
842 PACKAGES=IASL
843 ;;
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800844 MAKE|make)
845 NAME="GNU Make"
846 PACKAGES=MAKE
847 ;;
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700848 *)
Stefan Reinauera851bbb2016-01-29 18:03:26 -0800849 printf "${red}ERROR: Unsupported package $PACKAGE. (Supported packages are GCC, GDB, CLANG, IASL, MAKE)${NC}\n\n";
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700850 exit 1
851 ;;
852esac
Peter Stugeceacd772011-06-09 04:54:16 +0200853
Stefan Reinauer66e93352015-06-16 22:38:23 +0200854# Find all the required tools:
855
856TAR=$(searchtool tar) || exit $?
857PATCH=$(searchtool patch) || exit $?
858MAKE=$(searchtool make) || exit $?
859SHA1SUM=$(searchtool sha1sum)
zbao5cf758d2015-09-01 22:28:57 -0400860#SHA512SUM=$(searchtool sha512sum)
861#MD5SUM=$(searchtool md5sum)
Stefan Reinauer66e93352015-06-16 22:38:23 +0200862CHECKSUM=$SHA1SUM
Martin Roth95f7b222016-03-20 12:38:48 -0600863LBZIP2=$(searchtool lbzip2 "" nofail)
864PIGZ=$(searchtool pigz "" nofail)
Stefan Reinauer66e93352015-06-16 22:38:23 +0200865
866searchtool m4 > /dev/null
867searchtool bison > /dev/null
868searchtool flex flex > /dev/null
869searchtool g++ "Free Software Foundation" nofail > /dev/null || \
Marc Jones5ee69512015-06-17 15:38:17 -0600870searchtool clang "clang version" nofail > /dev/null || \
Martin Rothe3963172016-01-06 13:54:32 -0700871searchtool clang "LLVM" "" "g++" > /dev/null
Stefan Reinauer66e93352015-06-16 22:38:23 +0200872searchtool wget > /dev/null
873searchtool bzip2 "bzip2," > /dev/null
874
Martin Roth591790f2016-03-20 20:39:04 -0600875check_for_library "-lz" "zlib (zlib1g-dev or zlib-devel)"
876
Nico Huber156d87c2016-09-20 12:59:53 +0200877CC=cc
878check_cc
879
Martin Roth88463822016-01-07 11:03:36 -0700880if [ "$HALT_FOR_TOOLS" -ne 0 ]; then
881 exit 1
882fi
883
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700884# This initial cleanup is useful when updating the toolchain script.
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000885
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700886if [ "$clean" = "1" ]; then
887 cleanup
888fi
Stefan Reinauer074d9132009-09-26 16:43:17 +0000889
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700890# Set up host compiler and flags needed for various OSes
Stefan Reinauer074d9132009-09-26 16:43:17 +0000891
Patrick Georgif2c15f52015-06-11 21:07:31 +0200892if is_package_enabled "GCC"; then
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700893if [ $UNAME = "Darwin" ]; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000894 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
895
896 # generally the OS X compiler can create x64 binaries.
897 # Per default it generated i386 binaries in 10.5 and x64
898 # binaries in 10.6 (even if the kernel is 32bit)
899 # For some weird reason, 10.5 autodetects an ABI=64 though
900 # so we're setting the ABI explicitly here.
Stefan Reinauerd7649122015-06-09 11:44:25 -0700901 if [ $(sysctl -n hw.optional.x86_64 2>/dev/null) -eq 1 ] 2>/dev/null; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000902 OPTIONS="ABI=64"
903 else
904 OPTIONS="ABI=32"
905 fi
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700906
Stefan Reinauer4266b922012-12-05 16:18:32 -0800907 # In Xcode 4.5.2 the default compiler is clang.
908 # However, this compiler fails to compile gcc 4.7.x. As a
909 # workaround it's possible to compile gcc with llvm-gcc.
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700910 if $CC -v 2>&1 | grep -q LLVM; then
Stefan Reinauer4266b922012-12-05 16:18:32 -0800911 CC=llvm-gcc
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700912 fi
zbao11f1d312015-08-05 23:15:54 -0400913elif [ $UNAME = "Linux" -o $UNAME = "Cygwin" ]; then
Patrick Georgiddb8f802015-07-04 17:45:54 +0200914 # gmp is overeager with detecting 64bit CPUs even if they run
915 # a 32bit kernel and userland.
916 if [ "$(uname -m 2>/dev/null)" = "i686" ]; then
917 OPTIONS="ABI=32"
918 fi
Nico Huberde45c592016-01-20 23:22:33 +0100919elif [ $UNAME = "NetBSD" ]; then
920 # same for NetBSD but this one reports an i386
921 if [ "$(uname -m 2>/dev/null)" = "i386" ]; then
922 OPTIONS="ABI=32"
923 fi
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000924fi
Nico Huber66c2c1a2016-09-20 13:09:29 +0200925if [ -z "${LANGUAGES}" ]; then
926 if hostcc_has_gnat1 && \
927 [ "$(hostcc_version)" = "$(buildcc_version)" -o "${BOOTSTRAP}" = "1" ];
928 then
929 printf "\nFound compatible Ada compiler, enabling Ada support by default.\n\n"
930 LANGUAGES="ada,${DEFAULT_LANGUAGES}"
931 else
932 LANGUAGES="${DEFAULT_LANGUAGES}"
933 fi
934fi
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700935fi # GCC
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000936
Nico Huber5f72f962016-01-31 23:05:19 +0100937export HOSTCFLAGS="-Os"
938if have_hostcflags_from_gmp; then
939 set_hostcflags_from_gmp
940fi
941
Patrick Georgic1a75b12011-11-04 21:37:14 +0100942if [ "$USECCACHE" = 1 ]; then
943 CC="ccache $CC"
944fi
945
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700946# Prepare target directory for building GCC
947# (dependencies must be in the PATH)
Stefan Reinauer16bd7892012-12-07 23:57:01 +0100948mkdir -p $DESTDIR$TARGETDIR/bin
Stefan Reinauer88352d72016-04-06 15:57:03 -0700949mkdir -p $DESTDIR$TARGETDIR/share
Stefan Reinauer16bd7892012-12-07 23:57:01 +0100950export PATH=$DESTDIR$TARGETDIR/bin:$PATH
951
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700952# Download, unpack, patch and build all packages
953
Martin Roth03625172016-02-17 14:44:14 -0700954printf "Downloading tarballs ... \n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700955mkdir -p tarballs
956for P in $PACKAGES; do
957 download $P
958done
Martin Roth03625172016-02-17 14:44:14 -0700959printf "Downloaded tarballs ... ${green}ok${NC}\n"
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700960
961printf "Unpacking and patching ... \n"
962for P in $PACKAGES; do
963 unpack_and_patch $P || exit 1
964done
965printf "Unpacked and patched ... ${green}ok${NC}\n"
966
967printf "Building packages ... \n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700968for package in $PACKAGES; do
969 build $package
970done
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700971printf "Packages built ... ${green}ok${NC}\n"
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700972
Stefan Reinauer699ac272015-06-05 12:43:28 -0700973# Adding git information of current tree to target directory
974# for reproducibility
Stefan Reinauerd7649122015-06-09 11:44:25 -0700975PROGNAME=$(basename "$0")
Stefan Reinauer88352d72016-04-06 15:57:03 -0700976rm -f "$DESTDIR$TARGETDIR/share/$PROGNAME-*"
977cp "$PROGNAME" "$DESTDIR$TARGETDIR/share/$PROGNAME-$CROSSGCC_VERSION-$CROSGCC_COMMIT"
Zheng Bao30b895f2013-02-06 18:04:40 +0800978
Stefan Reinauer88e83e52016-04-06 15:39:48 -0700979cleanup
Stefan Reinauer074d9132009-09-26 16:43:17 +0000980
Stefan Reinauer699ac272015-06-05 12:43:28 -0700981printf "\n${green}You can now run your $NAME toolchain from $TARGETDIR.${NC}\n"