blob: 049ace40b39b5b5a627f95cc316cb046a7da2bb0 [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#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010020# Foundation, Inc.
Stefan Reinauer074d9132009-09-26 16:43:17 +000021#
22
Stefan Reinauerd7649122015-06-09 11:44:25 -070023cd $(dirname $0)
Patrick Georgi8135dba2015-03-17 21:05:20 +010024
Patrick Georgiaf473ed2015-07-17 23:35:54 +020025CROSSGCC_DATE="July 17th, 2015"
26CROSSGCC_VERSION="1.32"
Stefan Reinauer074d9132009-09-26 16:43:17 +000027
28# default settings
Stefan Reinauer85b07d62015-06-09 14:45:14 -070029PACKAGE=GCC
Stefan Reinauerd7649122015-06-09 11:44:25 -070030TARGETDIR=$(pwd)/xgcc
Stefan Reinauer074d9132009-09-26 16:43:17 +000031TARGETARCH=i386-elf
Nico Huberbb313bf2015-09-08 12:30:27 +020032LANGUAGES=c
Stefan Reinauer074d9132009-09-26 16:43:17 +000033DESTDIR=
Stefan Reinauer85b07d62015-06-09 14:45:14 -070034SAVETEMPS=0
35SKIPPYTHON=1
Stefan Reinauer074d9132009-09-26 16:43:17 +000036
Stefan Reinauer699ac272015-06-05 12:43:28 -070037# GCC toolchain version numbers
Patrick Georgi53c388f2015-03-07 09:55:18 +010038GMP_VERSION=6.0.0
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070039MPFR_VERSION=3.1.2
Paul Menzele6619422015-02-23 10:33:59 +010040MPC_VERSION=1.0.3
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000041LIBELF_VERSION=0.8.13
Patrick Georgiaf473ed2015-07-17 23:35:54 +020042GCC_VERSION=5.2.0
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070043GCC_AUTOCONF_VERSION=2.69
Patrick Georgi53c388f2015-03-07 09:55:18 +010044BINUTILS_VERSION=2.25
Stefan Reinauerbed95b22015-06-09 13:41:40 -070045GDB_VERSION=7.9.1
Stefan Reinaueradf61e22015-07-07 00:41:42 +020046IASL_VERSION=20150619
Patrick Georgi53c388f2015-03-07 09:55:18 +010047PYTHON_VERSION=3.4.3
Idwer Vollering296a0152012-10-26 01:48:04 +020048EXPAT_VERSION=2.1.0
Stefan Reinauer14ce2132015-06-05 13:01:13 -070049# CLANG version number
50CLANG_VERSION=3.6.1
Stefan Reinauer074d9132009-09-26 16:43:17 +000051
Stefan Reinauer699ac272015-06-05 12:43:28 -070052# GCC toolchain archive locations
Patrick Georgi53c388f2015-03-07 09:55:18 +010053GMP_ARCHIVE="http://ftpmirror.gnu.org/gmp/gmp-${GMP_VERSION}a.tar.bz2"
Patrick Georgi6e61ad32012-05-12 23:19:30 +020054MPFR_ARCHIVE="http://ftpmirror.gnu.org/mpfr/mpfr-${MPFR_VERSION}.tar.bz2"
Idwer Vollering7962fc72012-10-25 02:14:09 +020055MPC_ARCHIVE="http://ftpmirror.gnu.org/mpc/mpc-${MPC_VERSION}.tar.gz"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000056LIBELF_ARCHIVE="http://www.mr511.de/software/libelf-${LIBELF_VERSION}.tar.gz"
Idwer Vollering1cfee0b2012-10-25 02:03:53 +020057GCC_ARCHIVE="http://ftpmirror.gnu.org/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.bz2"
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070058BINUTILS_ARCHIVE="http://ftpmirror.gnu.org/binutils/binutils-${BINUTILS_VERSION}.tar.bz2"
Patrick Georgi53c388f2015-03-07 09:55:18 +010059GDB_ARCHIVE="http://ftpmirror.gnu.org/gdb/gdb-${GDB_VERSION}.tar.xz"
60IASL_ARCHIVE="https://acpica.org/sites/acpica/files/acpica-unix-${IASL_VERSION}.tar.gz"
61PYTHON_ARCHIVE="http://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +010062EXPAT_ARCHIVE="http://downloads.sourceforge.net/sourceforge/expat/expat-${EXPAT_VERSION}.tar.gz"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070063# CLANG toolchain archive locations
64LLVM_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/llvm-${CLANG_VERSION}.src.tar.xz"
65CFE_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/cfe-${CLANG_VERSION}.src.tar.xz"
66CRT_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/compiler-rt-${CLANG_VERSION}.src.tar.xz"
67CTE_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/clang-tools-extra-${CLANG_VERSION}.src.tar.xz"
Stefan Reinauer074d9132009-09-26 16:43:17 +000068
Stefan Reinauer699ac272015-06-05 12:43:28 -070069# GCC toolchain directories
Stefan Reinauer074d9132009-09-26 16:43:17 +000070GMP_DIR="gmp-${GMP_VERSION}"
71MPFR_DIR="mpfr-${MPFR_VERSION}"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000072MPC_DIR="mpc-${MPC_VERSION}"
73LIBELF_DIR="libelf-${LIBELF_VERSION}"
Stefan Reinauer074d9132009-09-26 16:43:17 +000074GCC_DIR="gcc-${GCC_VERSION}"
75BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
76GDB_DIR="gdb-${GDB_VERSION}"
Marc Jones2aac3f62011-08-08 16:07:50 -060077IASL_DIR="acpica-unix-${IASL_VERSION}"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +010078PYTHON_DIR="Python-${PYTHON_VERSION}"
79EXPAT_DIR="expat-${EXPAT_VERSION}"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070080# CLANG toolchain directories
81LLVM_DIR="llvm-${CLANG_VERSION}.src"
82CFE_DIR="cfe-${CLANG_VERSION}.src"
83CRT_DIR="compiler-rt-${CLANG_VERSION}.src"
84CTE_DIR="clang-tools-extra-${CLANG_VERSION}.src"
Stefan Reinauer074d9132009-09-26 16:43:17 +000085
Patrick Georgi6321f522015-02-27 23:36:26 +010086unset MAKELEVEL MAKEFLAGS
87
Patrick Georgi1861ff72011-10-31 12:15:55 +010088red='\033[0;31m'
89RED='\033[1;31m'
90green='\033[0;32m'
91GREEN='\033[1;32m'
92blue='\033[0;34m'
93BLUE='\033[1;34m'
94cyan='\033[0;36m'
95CYAN='\033[1;36m'
96NC='\033[0m' # No Color
Stefan Reinauer074d9132009-09-26 16:43:17 +000097
zbaoe05c1ea2015-08-05 23:10:05 -040098UNAME=$(uname | grep -iq cygwin && echo Cygwin || uname)
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070099
Patrick Georgi198d23c2012-12-08 08:02:44 +0100100normalize_dirs()
101{
102 mkdir -p $DESTDIR$TARGETDIR/lib
103 test -d $DESTDIR$TARGETDIR/lib32 && mv $DESTDIR$TARGETDIR/lib32/* $DESTDIR$TARGETDIR/lib
104 test -d $DESTDIR$TARGETDIR/lib64 && mv $DESTDIR$TARGETDIR/lib64/* $DESTDIR$TARGETDIR/lib
105 rmdir -p $DESTDIR$TARGETDIR/lib32 $DESTDIR$TARGETDIR/lib64
106
107 perl -pi -e "s,/lib32,/lib," $DESTDIR$TARGETDIR/lib/*.la
108 perl -pi -e "s,/lib64,/lib," $DESTDIR$TARGETDIR/lib/*.la
109}
110
Stefan Reinauer93a96302015-06-15 12:36:53 -0700111please_install()
112{
Patrick Georgi5da95dc2015-07-17 23:33:05 +0200113 test -r /etc/os-release && . /etc/os-release
Stefan Reinauer93a96302015-06-15 12:36:53 -0700114 case "$ID_LIKE" in
115 debian) solution="sudo apt-get install $1" ;;
116 suse) solution="sudo zypper install $1" ;;
117 *) solution="using your OS packaging system" ;;
118 esac
119
120 printf "${RED}ERROR:${red} Missing tool: Please install \'$1\' utility. (eg $solution)${NC}\n" >&2
121}
122
Patrick Georgib7062882015-02-24 10:52:14 +0100123searchtool()
Stefan Reinauer074d9132009-09-26 16:43:17 +0000124{
125 # $1 short name
Patrick Georgib7062882015-02-24 10:52:14 +0100126 # $2 search string
127 # $3 soft fail if set
128 # result: file name of that tool on stdout
129 # or no output if nothing suitable was found
130 search=GNU
131 if [ -n "$2" ]; then
132 search="$2"
133 fi
Stefan Reinauer074d9132009-09-26 16:43:17 +0000134 for i in "$1" "g$1" "gnu$1"; do
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700135 if [ -x "$(which $i 2>/dev/null)" ]; then
136 if [ "$(cat /dev/null | $i --version 2>&1 | grep -c "$search")" \
137 -gt 0 ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000138 echo $i
139 return
140 fi
141 fi
142 done
Andrew Wub67e9a12014-04-28 18:13:44 +0800143 # A workaround for OSX 10.9 and some BSDs, whose nongnu
144 # patch and tar also work.
Patrick Georgi7cb26b42015-04-28 21:29:22 +0200145 if [ $UNAME = "Darwin" -o $UNAME = "FreeBSD" -o $UNAME = "NetBSD" -o $UNAME = "OpenBSD" ]; then
Patrick Georgib7062882015-02-24 10:52:14 +0100146 if [ "$1" = "patch" -o "$1" = "tar" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700147 if [ -x "$(which $1 2>/dev/null)" ]; then
Zheng Bao36156ff2012-09-28 16:18:58 +0800148 echo $1
149 return
150 fi
151 fi
152 fi
Stefan Reinauerd7649122015-06-09 11:44:25 -0700153 if [ "$(echo $1 | cut -b -3)" = "sha" ]; then
zbao939dc842015-04-30 04:44:07 +0800154 if [ $UNAME = "FreeBSD" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700155 if [ -x "$(which sha1 2>/dev/null)" ]; then
zbao939dc842015-04-30 04:44:07 +0800156 echo sha1
157 return
158 fi
159 fi
160 if [ $UNAME = "NetBSD" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700161 if [ -x "$(which cksum 2>/dev/null)" ]; then
Stefan Reinauerd7649122015-06-09 11:44:25 -0700162 echo cksum -a $(echo $1 | sed -e 's,sum,,')
zbao939dc842015-04-30 04:44:07 +0800163 return
164 fi
165 fi
166 if [ $UNAME = "Darwin" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700167 if [ -x "$(which openssl 2>/dev/null)" ]; then
Stefan Reinauerd7649122015-06-09 11:44:25 -0700168 echo openssl $(echo $1 | sed -e 's,sum,,')
zbao939dc842015-04-30 04:44:07 +0800169 return
170 fi
171 fi
172 fi
Stefan Reinauer93a96302015-06-15 12:36:53 -0700173 please_install $1
Patrick Georgib7062882015-02-24 10:52:14 +0100174 [ -z "$3" ] && exit 1
175 false
Stefan Reinauer074d9132009-09-26 16:43:17 +0000176}
177
zbao06e85ac2015-08-24 22:08:36 -0400178check_sum() {
179 test -z "$CHECKSUM" || \
180 test "$(cat sum/$1.cksum 2>/dev/null | sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@')" = \
181 "$($CHECKSUM tarballs/$1 2>/dev/null | sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@')"
182}
183
184compute_sum() {
185 test ! -f sum/$1.cksum && test -f tarballs/$1 && \
186 (test -z "$CHECKSUM" || $CHECKSUM tarballs/$1 > sum/$1.cksum ) && \
187 printf "(checksum created. ${RED}Note. Please upload sum/$1.cksum if the corresponding archive is upgraded.)${NC}"
188}
189
Stefan Reinauer88935482015-06-05 09:51:10 -0700190download() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700191 package=$1
192 archive="$(eval echo \$$package"_ARCHIVE")"
Stefan Reinauer699ac272015-06-05 12:43:28 -0700193
Stefan Reinauerd7649122015-06-09 11:44:25 -0700194 FILE=$(basename $archive)
Stefan Reinauer88935482015-06-05 09:51:10 -0700195 printf " * $FILE "
196
zbaod22b9ea2015-08-30 23:35:16 -0400197 if test -f tarballs/$FILE && check_sum $FILE ; then
198 printf "(cached)"
199 else
Paul Menzelb2ba3952015-06-12 11:58:14 +0200200 printf "(downloading from $archive)"
Stefan Reinauer88935482015-06-05 09:51:10 -0700201 rm -f tarballs/$FILE
202 cd tarballs
Stefan Reinauer699ac272015-06-05 12:43:28 -0700203 wget --no-check-certificate -q $archive
zbaod22b9ea2015-08-30 23:35:16 -0400204 wgetret=$?
Stefan Reinauer88935482015-06-05 09:51:10 -0700205 cd ..
zbao06e85ac2015-08-24 22:08:36 -0400206 compute_sum $FILE
zbaod22b9ea2015-08-30 23:35:16 -0400207 fi
208
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700209 if [ ! -f tarballs/$FILE ]; then
zbaod22b9ea2015-08-30 23:35:16 -0400210 printf "\n${RED}Failed to download $FILE. Wget returns $wgetret. See 'man wget'.${NC}\n"
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700211 exit 1
212 fi
Stefan Reinauer88935482015-06-05 09:51:10 -0700213 printf "\n"
214}
215
216unpack_and_patch() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700217 package=$1
218 archive="$(eval echo \$$package"_ARCHIVE")"
219 dir="$(eval echo \$$package"_DIR")"
Stefan Reinauer88935482015-06-05 09:51:10 -0700220 test -d ${dir} && test -f ${dir}/.unpack_success || (
Stefan Reinauerd7649122015-06-09 11:44:25 -0700221 printf " * $(basename $archive)\n"
Stefan Reinauer88935482015-06-05 09:51:10 -0700222 FLAGS=zxf
Stefan Reinauerd7649122015-06-09 11:44:25 -0700223 suffix=$(echo $archive | sed 's,.*\.,,')
Stefan Reinauer88935482015-06-05 09:51:10 -0700224 test "$suffix" = "gz" && FLAGS=zxf
225 test "$suffix" = "bz2" && FLAGS=jxf
226 test "$suffix" = "xz" && FLAGS="--xz -xf"
227 test "$suffix" = "lzma" && FLAGS="--lzma -xf"
Stefan Reinauerd7649122015-06-09 11:44:25 -0700228 $TAR $FLAGS tarballs/$(basename $archive)
Stefan Reinauer88935482015-06-05 09:51:10 -0700229 for patch in patches/${dir}_*.patch; do
230 test -r $patch || continue
Stefan Reinauerd7649122015-06-09 11:44:25 -0700231 printf " o $(basename $patch)\n"
Stefan Reinauer916b8452015-06-09 11:54:49 -0700232 $PATCH -s -N -p0 < $(echo $patch) || {
Stefan Reinauer88935482015-06-05 09:51:10 -0700233 printf "\n${RED}Failed $patch.${NC}\n"
Stefan Reinauer916b8452015-06-09 11:54:49 -0700234 exit 1
235 }
Stefan Reinauer88935482015-06-05 09:51:10 -0700236 done
237 touch ${dir}/.unpack_success
238 )
239}
240
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700241fn_exists()
242{
Patrick Georgic8665912015-06-11 21:04:31 +0200243 type $1 >/dev/null 2>&1
Patrick Georgi74c06422015-03-25 18:40:13 +0100244}
245
Patrick Georgif2c15f52015-06-11 21:07:31 +0200246is_package_enabled()
247{
248 echo "$PACKAGES" |grep -q "\<$1\>"
249}
250
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700251build() {
252 package=$1
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700253
254 fn_exists build_$package || return
255
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700256 version="$(eval echo \$$package"_VERSION")"
257 BUILDDIR=build-${TARGETARCH}-$package
258
259 mkdir -p ${BUILDDIR}
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700260
Patrick Georgif2c15f52015-06-11 21:07:31 +0200261 is_package_enabled "$package" && \
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700262 if [ -f ${BUILDDIR}/.success ]; then
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700263 printf "Skipping $package as it is already built\n"
264 else
265 printf "Building $package $version ... "
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700266 DIR=$PWD
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700267 cd ${BUILDDIR}
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700268 rm -f .failed
269 build_${package} > build.log 2>&1
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700270 cd $DIR/${BUILDDIR}
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700271 if [ ! -f .failed ]; then touch .success; fi
272 cd ..
273
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700274 if [ -r "${BUILDDIR}/.failed" ]; then
275 printf "${RED}failed${NC}. Check ${BUILDDIR}/build.log.\n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700276 exit 1
277 fi
278 printf "${green}ok${NC}\n"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700279 fi
280}
281
Stefan Reinauer074d9132009-09-26 16:43:17 +0000282cleanup()
283{
284 printf "Cleaning up temporary files... "
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700285 for package in $PACKAGES; do
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700286 rm -rf build-${TARGETARCH}-$package $(eval echo \$$package"_DIR")
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700287 done
zbaoff3f15c2015-08-26 22:00:31 -0400288 rm -f getopt
Stefan Reinauer074d9132009-09-26 16:43:17 +0000289 printf "${green}ok${NC}\n"
290}
291
292myhelp()
293{
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700294 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>] [-C] [-G] [-S]\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000295 printf " $0 [-V|--version]\n"
296 printf " $0 [-h|--help]\n\n"
297
298 printf "Options:\n"
299 printf " [-V|--version] print version number and exit\n"
300 printf " [-h|--help] print this help and exit\n"
301 printf " [-c|--clean] remove temporary files before build\n"
302 printf " [-t|--savetemps] don't remove temporary files after build\n"
Patrick Georgic1a75b12011-11-04 21:37:14 +0100303 printf " [-y|--ccache] Use ccache when building cross compiler\n"
Patrick Georgi73166c72010-04-14 20:42:42 +0000304 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000305 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
306 printf " (defaults to $TARGETDIR)\n\n"
307 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100308 printf " (for RPM builds, default unset)\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700309 printf " [-P|--package <package>] Build a specific package: GCC, CLANG, IASL, GDB\n"
310 printf " (defaults to $PACKAGE)\n"
311 printf "GCC specific options:\n"
312 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
313 printf " (defaults to $TARGETARCH)\n"
Nico Huberbb313bf2015-09-08 12:30:27 +0200314 printf " [-l|--languages <languages>] comma separated list of target languages\n"
315 printf " (defaults to $LANGUAGES)\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700316 printf "GDB specific options:\n"
317 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
318 printf " (defaults to $TARGETARCH)\n"
319 printf " [-S|--scripting] build scripting support for GDB\n\n"
Martin Rothd1da74f2015-06-21 12:06:28 -0600320 printf "Platforms for GCC & GDB:\n"
321 printf " x86_64 i386-elf i386-mingw32 mipsel-elf riscv-elf arm aarch64\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000322}
323
324myversion()
325{
326 # version tag is always printed, so just print the license here
327
328 cat << EOF
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000329Copyright (C) 2008-2010 by coresystems GmbH
Marc Jones2aac3f62011-08-08 16:07:50 -0600330Copyright (C) 2011 by Sage Electronic Engineering
Stefan Reinauer074d9132009-09-26 16:43:17 +0000331
332This program is free software; you can redistribute it and/or modify
333it under the terms of the GNU General Public License as published by
334the Free Software Foundation; version 2 of the License.
335
336This program is distributed in the hope that it will be useful,
337but WITHOUT ANY WARRANTY; without even the implied warranty of
338MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
339GNU General Public License for more details.
340
341EOF
342}
343
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700344build_GMP() {
345 CC="$CC" CFLAGS="-Os" ../${GMP_DIR}/configure --disable-shared --enable-fat --prefix=$TARGETDIR $OPTIONS \
346 || touch .failed
347 $MAKE $JOBS || touch .failed
348 $MAKE install DESTDIR=$DESTDIR || touch .failed
349
350 normalize_dirs
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700351
352 # Now set CFLAGS to match GMP CFLAGS but strip out -pedantic
353 # as GCC 4.6.x fails if it's there.
354 export HOSTCFLAGS=$(grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2 |\
355 sed s,-pedantic,,)
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700356}
357
358build_MPFR() {
359 test $UNAME = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
360 CC="$CC" ../${MPFR_DIR}/configure --disable-shared --prefix=$TARGETDIR \
361 --infodir=$TARGETDIR/info \
362 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || \
363 touch .failed
364 $MAKE $JOBS || touch .failed
365 $MAKE install DESTDIR=$DESTDIR || touch .failed
366
367 normalize_dirs
368
369 # work around build problem of libgmp.la
370 if [ "$DESTDIR" != "" ]; then
371 perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
372 fi
373}
374
375build_MPC() {
376 CC="$CC" ../${MPC_DIR}/configure --disable-shared --prefix=$TARGETDIR \
377 --infodir=$TARGETDIR/info --with-mpfr=$DESTDIR$TARGETDIR \
378 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || \
379 touch .failed
380 $MAKE $JOBS || touch .failed
381 $MAKE install DESTDIR=$DESTDIR || touch .failed
382
383 normalize_dirs
384}
385
386build_LIBELF() {
387 CC="$CC" CFLAGS="$HOSTCFLAGS" libelf_cv_elf_h_works=no \
388 ../${LIBELF_DIR}/configure --disable-shared --prefix=$TARGETDIR \
389 --infodir=$TARGETDIR/info CFLAGS="$HOSTCFLAGS" || touch .failed
390 $MAKE $JOBS || touch .failed
391 $MAKE install prefix=$DESTDIR$TARGETDIR || touch .failed
392
393 normalize_dirs
394}
395
396build_BINUTILS() {
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700397 if [ $TARGETARCH == "x86_64-elf" ]; then
398 ADDITIONALTARGET=",i386-elf"
399 fi
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700400 CC="$CC" ../binutils-${BINUTILS_VERSION}/configure --prefix=$TARGETDIR \
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700401 --target=${TARGETARCH} --enable-targets=${TARGETARCH}${ADDITIONALTARGET} \
402 --disable-werror --disable-nls --enable-lto --enable-gold \
403 --enable-plugins --enable-multilibs CFLAGS="$HOSTCFLAGS" || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700404 $MAKE $JOBS || touch .failed
405 $MAKE install DESTDIR=$DESTDIR || touch .failed
406}
407
408
409build_GCC() {
Stefan Reinauerd0f83722015-06-09 11:52:28 -0700410 # GCC does not honor HOSTCFLAGS at all. CFLAGS are used for
411 # both target and host object files.
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700412 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
413 # but it does not seem to work properly. At least the host library
414 # libiberty is not compiled with CFLAGS_FOR_BUILD.
415 CC="$CC" CFLAGS_FOR_TARGET="-O2" CFLAGS="$HOSTCFLAGS" \
416 CFLAGS_FOR_BUILD="$HOSTCFLAGS" ../gcc-${GCC_VERSION}/configure \
417 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
418 --target=${TARGETARCH} --disable-werror --disable-shared \
Stefan Reinauer2f97ebc2015-06-09 12:24:54 -0700419 --enable-lto --enable-plugins --enable-gold --enable-ld=default \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700420 --disable-libssp --disable-bootstrap --disable-nls \
421 --disable-libquadmath --without-headers \
Nico Huberbb313bf2015-09-08 12:30:27 +0200422 ${GCC_OPTIONS} --enable-languages="${LANGUAGES}" \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700423 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
424 --with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
425 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE" \
426 || touch .failed
427 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-gcc || touch .failed
428 $MAKE install-gcc DESTDIR=$DESTDIR || touch .failed
429
Stefan Reinauerd7649122015-06-09 11:44:25 -0700430 if [ "$(echo $TARGETARCH | grep -c -- -mingw32)" -eq 0 ]; then
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700431 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-target-libgcc || touch .failed
432 $MAKE install-target-libgcc DESTDIR=$DESTDIR || touch .failed
433 fi
434}
435
436build_EXPAT() {
437 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${EXPAT_DIR}/configure --disable-shared \
438 --prefix=$TARGETDIR --target=${TARGETARCH} || touch .failed
439 $MAKE || touch .failed
440 $MAKE install DESTDIR=$DESTDIR || touch .failed
441
442 normalize_dirs
443}
444
445build_PYTHON() {
446 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${PYTHON_DIR}/configure --prefix=$TARGETDIR \
447 --target=${TARGETARCH} || touch .failed
448 $MAKE $JOBS || touch .failed
449 $MAKE install DESTDIR=$DESTDIR || touch .failed
450
451 normalize_dirs
452}
453
454build_GDB() {
455 export PYTHONHOME=$DESTDIR$TARGETDIR
456 if [ $(uname) != "FreeBSD" -a $(uname) != "NetBSD" ]; then
457 LIBDL="-ldl"
458 fi
459 LDFLAGS="-Wl,-rpath,\$\$ORIGIN/../lib/ -L$DESTDIR$TARGETDIR/lib \
460 -lpthread $LIBDL -lutil" \
461 CC="$CC" CFLAGS="$HOSTCFLAGS -I$DESTDIR$TARGETDIR/include" \
Stefan Reinauer7b40e422015-06-09 11:56:32 -0700462 ../${GDB_DIR}/configure --prefix=$TARGETDIR \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700463 --target=${TARGETARCH} --disable-werror --disable-nls
464 $MAKE $JOBS || touch .failed
465 $MAKE install DESTDIR=$DESTDIR || touch .failed
466}
467
468build_IASL() {
469 RDIR=$PWD
470 cd ../$IASL_DIR/generate/unix
471 CFLAGS="$HOSTCFLAGS"
472 HOST="_LINUX"
473 test $UNAME = "Darwin" && HOST="_APPLE"
474 test $UNAME = "FreeBSD" && HOST="_FreeBSD"
475 test $UNAME = "Cygwin" && HOST="_CYGWIN"
476 HOST="$HOST" OPT_CFLAGS="-O -D_FORTIFY_SOURCE=2" CFLAGS="$CFLAGS" $MAKE CC="$CC" iasl || touch $RDIR/.failed
477 rm -f $DESTDIR$TARGETDIR/bin/iasl || touch $RDIR/.failed
478 cp bin/iasl $DESTDIR$TARGETDIR/bin || touch $RDIR/.failed
479}
480
481build_LLVM() {
482 cd ..
483 ln -sf $PWD/$CFE_DIR $LLVM_DIR/tools/clang
484 ln -sf $PWD/$CTE_DIR $LLVM_DIR/tools/clang/tools/extra
485 ln -sf $PWD/$CRT_DIR $LLVM_DIR/projects/compiler-rt
486 cd -
487
488 $CMAKE -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$DESTDIR$TARGETDIR \
489 -DCMAKE_BUILD_TYPE=Release ../$LLVM_DIR || touch .failed
490 $MAKE $JOBS || touch .failed
491 $MAKE install || touch .failed
492
493 cp -a ../$CFE_DIR/tools/scan-build/* $DESTDIR$TARGETDIR/bin
494 cp -a ../$CFE_DIR/tools/scan-view/* $DESTDIR$TARGETDIR/bin
Stefan Reinauere9e6e3d2015-07-07 00:20:42 +0200495
496 # create symlinks to work around broken --print-librt-file-name
497 # when used with -target.
498 cd $DESTDIR$TARGETDIR/lib/clang/${CLANG_VERSION}/lib
499 for i in */libclang_rt.builtins*.a; do
500 ln -s $i .
501 done
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700502}
503
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700504printf "${blue}Welcome to the ${red}coreboot${blue} cross toolchain builder v$CROSSGCC_VERSION ($CROSSGCC_DATE)${NC}\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000505
Patrick Georgi6bba29f2010-01-29 17:40:52 +0000506# Look if we have getopt. If not, build it.
507export PATH=$PATH:.
508getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
509
Stefan Reinauer074d9132009-09-26 16:43:17 +0000510# parse parameters.. try to find out whether we're running GNU getopt
Stefan Reinauerd7649122015-06-09 11:44:25 -0700511getoptbrand="$(getopt -V | sed -e '1!d' -e 's,^\(......\).*,\1,')"
Peter Stuge09377b72011-08-21 06:24:55 +0200512if [ "${getoptbrand}" = "getopt" ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000513 # Detected GNU getopt that supports long options.
Nico Huberbb313bf2015-09-08 12:30:27 +0200514 args=$(getopt -l version,help,clean,directory:,platform:,languages:,package:,jobs:,destdir:,savetemps,scripting,ccache Vhcd:p:l:P:j:D:tSy -- "$@")
Stefan Reinauer074d9132009-09-26 16:43:17 +0000515 eval set "$args"
516else
517 # Detected non-GNU getopt
Nico Huberbb313bf2015-09-08 12:30:27 +0200518 args=$(getopt Vhcd:p:l:P:j:D:tSy $*)
Stefan Reinauer074d9132009-09-26 16:43:17 +0000519 set -- $args
520fi
521
522if [ $? != 0 ]; then
523 myhelp
524 exit 1
525fi
526
527while true ; do
528 case "$1" in
529 -V|--version) shift; myversion; exit 0;;
530 -h|--help) shift; myhelp; exit 0;;
Patrick Georgi44af57a2013-12-19 22:06:22 +0100531 -c|--clean) shift; clean=1;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000532 -t|--savetemps) shift; SAVETEMPS=1;;
533 -d|--directory) shift; TARGETDIR="$1"; shift;;
534 -p|--platform) shift; TARGETARCH="$1"; shift;;
Nico Huberbb313bf2015-09-08 12:30:27 +0200535 -l|--languages) shift; LANGUAGES="$1"; shift;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000536 -D|--destdir) shift; DESTDIR="$1"; shift;;
Patrick Georgi73166c72010-04-14 20:42:42 +0000537 -j|--jobs) shift; JOBS="-j $1"; shift;;
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700538 -P|--package) shift; PACKAGE="$1"; shift;;
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100539 -S|--scripting) shift; SKIPPYTHON=0;;
Patrick Georgic1a75b12011-11-04 21:37:14 +0100540 -y|--ccache) shift; USECCACHE=1;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200541 --) shift; break;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000542 -*) printf "Invalid option\n\n"; myhelp; exit 1;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200543 *) break;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000544 esac
545done
546
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700547case "$TARGETARCH" in
Stefan Reinauer24f9cb92015-03-13 22:50:22 +0100548 x86_64-elf) ;;
549 x86_64*) TARGETARCH=x86_64-elf;;
David Hubbard5b0420a2013-05-28 16:33:15 -0600550 i386-elf) ;;
551 i386-mingw32) ;;
Patrick Georgi9b7efa52015-03-08 09:20:36 +0100552 mipsel-elf) ;;
Patrick Georgif0bbc952015-03-07 10:57:25 +0100553 riscv-elf) ;;
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700554 i386*) TARGETARCH=i386-elf;;
Patrick Georgic8883262013-09-19 10:57:58 +0200555 arm*) TARGETARCH=armv7-a-eabi;;
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700556 aarch64*) TARGETARCH=aarch64-elf;;
557 *) printf "${red}WARNING: Unsupported architecture $TARGETARCH.${NC}\n\n"; ;;
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700558esac
559
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700560# Figure out which packages to build
Stefan Reinauer699ac272015-06-05 12:43:28 -0700561
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700562case "$PACKAGE" in
563 GCC|gcc)
564 echo "Target architecture is now $TARGETARCH"
565 NAME="${TARGETARCH} cross GCC"
Stefan Reinauerbd8b2e32015-06-17 11:55:02 -0700566 PACKAGES="GMP MPFR MPC LIBELF BINUTILS GCC"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700567 ;;
568 GDB|gdb)
569 NAME="${TARGETARCH} cross GDB"
570 PACKAGES="GDB"
571 if [ $SKIPPYTHON -eq 0 ]; then
572 PACKAGES="EXPAT PYTHON $PACKAGES"
573 fi
574 ;;
575 CLANG|clang)
576 NAME=clang
577 PACKAGES="LLVM CFE CRT CTE"
David Hendrickscd5cdd32015-06-15 17:03:22 -0700578 CMAKE=$(searchtool cmake "cmake") || exit $?
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700579 ;;
580 IASL|iasl)
581 NAME="IASL ACPI compiler"
582 PACKAGES=IASL
583 ;;
584 *)
585 printf "${red}ERROR: Unsupported package $PACKAGE. (Supported packages are GCC, GDB, CLANG, IASL)${NC}\n\n";
586 exit 1
587 ;;
588esac
Peter Stugeceacd772011-06-09 04:54:16 +0200589
Stefan Reinauer66e93352015-06-16 22:38:23 +0200590# Find all the required tools:
591
592TAR=$(searchtool tar) || exit $?
593PATCH=$(searchtool patch) || exit $?
594MAKE=$(searchtool make) || exit $?
595SHA1SUM=$(searchtool sha1sum)
596SHA512SUM=$(searchtool sha512sum)
597CHECKSUM=$SHA1SUM
598
599searchtool m4 > /dev/null
600searchtool bison > /dev/null
601searchtool flex flex > /dev/null
602searchtool g++ "Free Software Foundation" nofail > /dev/null || \
Marc Jones5ee69512015-06-17 15:38:17 -0600603searchtool clang "clang version" nofail > /dev/null || \
604searchtool clang "LLVM" > /dev/null
Stefan Reinauer66e93352015-06-16 22:38:23 +0200605searchtool wget > /dev/null
606searchtool bzip2 "bzip2," > /dev/null
607
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700608# This initial cleanup is useful when updating the toolchain script.
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000609
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700610if [ "$clean" = "1" ]; then
611 cleanup
612fi
Stefan Reinauer074d9132009-09-26 16:43:17 +0000613
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700614# Set up host compiler and flags needed for various OSes
Stefan Reinauer074d9132009-09-26 16:43:17 +0000615
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700616CC=cc
Patrick Georgif2c15f52015-06-11 21:07:31 +0200617if is_package_enabled "GCC"; then
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700618if [ $UNAME = "Darwin" ]; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000619 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
620
621 # generally the OS X compiler can create x64 binaries.
622 # Per default it generated i386 binaries in 10.5 and x64
623 # binaries in 10.6 (even if the kernel is 32bit)
624 # For some weird reason, 10.5 autodetects an ABI=64 though
625 # so we're setting the ABI explicitly here.
Stefan Reinauerd7649122015-06-09 11:44:25 -0700626 if [ $(sysctl -n hw.optional.x86_64 2>/dev/null) -eq 1 ] 2>/dev/null; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000627 OPTIONS="ABI=64"
628 else
629 OPTIONS="ABI=32"
630 fi
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700631
Stefan Reinauer4266b922012-12-05 16:18:32 -0800632 # In Xcode 4.5.2 the default compiler is clang.
633 # However, this compiler fails to compile gcc 4.7.x. As a
634 # workaround it's possible to compile gcc with llvm-gcc.
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700635 if $CC -v 2>&1 | grep -q LLVM; then
Stefan Reinauer4266b922012-12-05 16:18:32 -0800636 CC=llvm-gcc
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700637 fi
zbao11f1d312015-08-05 23:15:54 -0400638elif [ $UNAME = "Linux" -o $UNAME = "Cygwin" ]; then
Patrick Georgiddb8f802015-07-04 17:45:54 +0200639 # gmp is overeager with detecting 64bit CPUs even if they run
640 # a 32bit kernel and userland.
641 if [ "$(uname -m 2>/dev/null)" = "i686" ]; then
642 OPTIONS="ABI=32"
643 fi
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000644fi
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700645fi # GCC
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000646
Patrick Georgic1a75b12011-11-04 21:37:14 +0100647if [ "$USECCACHE" = 1 ]; then
648 CC="ccache $CC"
649fi
650
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700651# Prepare target directory for building GCC
652# (dependencies must be in the PATH)
Stefan Reinauer16bd7892012-12-07 23:57:01 +0100653mkdir -p $DESTDIR$TARGETDIR/bin
654export PATH=$DESTDIR$TARGETDIR/bin:$PATH
655
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700656# Download, unpack, patch and build all packages
657
658printf "Downloading tar balls ... \n"
659mkdir -p tarballs
660for P in $PACKAGES; do
661 download $P
662done
663printf "Downloaded tar balls ... ${green}ok${NC}\n"
664
665printf "Unpacking and patching ... \n"
666for P in $PACKAGES; do
667 unpack_and_patch $P || exit 1
668done
669printf "Unpacked and patched ... ${green}ok${NC}\n"
670
671printf "Building packages ... \n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700672for package in $PACKAGES; do
673 build $package
674done
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700675printf "Packages built ... ${green}ok${NC}\n"
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700676
Stefan Reinauer699ac272015-06-05 12:43:28 -0700677# Adding git information of current tree to target directory
678# for reproducibility
Stefan Reinauerd7649122015-06-09 11:44:25 -0700679PROGNAME=$(basename "$0")
Patrick Georgi5602f2c2015-03-28 15:48:47 +0100680rm -f "$DESTDIR$TARGETDIR/$PROGNAME".commit.*
Stefan Reinauerd7649122015-06-09 11:44:25 -0700681cp "$PROGNAME" $DESTDIR$TARGETDIR/"$PROGNAME.commit.$(git describe)"
Zheng Bao30b895f2013-02-06 18:04:40 +0800682
Stefan Reinauer074d9132009-09-26 16:43:17 +0000683if [ $SAVETEMPS -eq 0 ]; then
Stefan Reinauer699ac272015-06-05 12:43:28 -0700684 cleanup
Stefan Reinauer074d9132009-09-26 16:43:17 +0000685else
686 printf "Leaving temporary files around... ${green}ok${NC}\n"
687fi
688
Stefan Reinauer699ac272015-06-05 12:43:28 -0700689printf "\n${green}You can now run your $NAME toolchain from $TARGETDIR.${NC}\n"