blob: 0c6193d51719b0fdf18efce849916c76b5fe1622 [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
Stefan Reinauerbed95b22015-06-09 13:41:40 -070025CROSSGCC_DATE="June 9th, 2015"
26CROSSGCC_VERSION="1.30"
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
32DESTDIR=
Stefan Reinauer85b07d62015-06-09 14:45:14 -070033SAVETEMPS=0
34SKIPPYTHON=1
Stefan Reinauer074d9132009-09-26 16:43:17 +000035
Stefan Reinauer699ac272015-06-05 12:43:28 -070036# GCC toolchain version numbers
Patrick Georgi53c388f2015-03-07 09:55:18 +010037GMP_VERSION=6.0.0
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070038MPFR_VERSION=3.1.2
Paul Menzele6619422015-02-23 10:33:59 +010039MPC_VERSION=1.0.3
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000040LIBELF_VERSION=0.8.13
Patrick Georgi53c388f2015-03-07 09:55:18 +010041GCC_VERSION=4.9.2
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070042GCC_AUTOCONF_VERSION=2.69
Patrick Georgi53c388f2015-03-07 09:55:18 +010043BINUTILS_VERSION=2.25
Stefan Reinauerbed95b22015-06-09 13:41:40 -070044GDB_VERSION=7.9.1
Patrick Georgi53c388f2015-03-07 09:55:18 +010045IASL_VERSION=20150204
46PYTHON_VERSION=3.4.3
Idwer Vollering296a0152012-10-26 01:48:04 +020047EXPAT_VERSION=2.1.0
Stefan Reinauer14ce2132015-06-05 13:01:13 -070048# CLANG version number
49CLANG_VERSION=3.6.1
Stefan Reinauer074d9132009-09-26 16:43:17 +000050
Stefan Reinauer699ac272015-06-05 12:43:28 -070051# GCC toolchain archive locations
Patrick Georgi53c388f2015-03-07 09:55:18 +010052GMP_ARCHIVE="http://ftpmirror.gnu.org/gmp/gmp-${GMP_VERSION}a.tar.bz2"
Patrick Georgi6e61ad32012-05-12 23:19:30 +020053MPFR_ARCHIVE="http://ftpmirror.gnu.org/mpfr/mpfr-${MPFR_VERSION}.tar.bz2"
Idwer Vollering7962fc72012-10-25 02:14:09 +020054MPC_ARCHIVE="http://ftpmirror.gnu.org/mpc/mpc-${MPC_VERSION}.tar.gz"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000055LIBELF_ARCHIVE="http://www.mr511.de/software/libelf-${LIBELF_VERSION}.tar.gz"
Idwer Vollering1cfee0b2012-10-25 02:03:53 +020056GCC_ARCHIVE="http://ftpmirror.gnu.org/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.bz2"
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070057BINUTILS_ARCHIVE="http://ftpmirror.gnu.org/binutils/binutils-${BINUTILS_VERSION}.tar.bz2"
Patrick Georgi53c388f2015-03-07 09:55:18 +010058GDB_ARCHIVE="http://ftpmirror.gnu.org/gdb/gdb-${GDB_VERSION}.tar.xz"
59IASL_ARCHIVE="https://acpica.org/sites/acpica/files/acpica-unix-${IASL_VERSION}.tar.gz"
60PYTHON_ARCHIVE="http://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +010061EXPAT_ARCHIVE="http://downloads.sourceforge.net/sourceforge/expat/expat-${EXPAT_VERSION}.tar.gz"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070062# CLANG toolchain archive locations
63LLVM_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/llvm-${CLANG_VERSION}.src.tar.xz"
64CFE_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/cfe-${CLANG_VERSION}.src.tar.xz"
65CRT_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/compiler-rt-${CLANG_VERSION}.src.tar.xz"
66CTE_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/clang-tools-extra-${CLANG_VERSION}.src.tar.xz"
Stefan Reinauer074d9132009-09-26 16:43:17 +000067
Stefan Reinauer699ac272015-06-05 12:43:28 -070068# GCC toolchain directories
Stefan Reinauer074d9132009-09-26 16:43:17 +000069GMP_DIR="gmp-${GMP_VERSION}"
70MPFR_DIR="mpfr-${MPFR_VERSION}"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000071MPC_DIR="mpc-${MPC_VERSION}"
72LIBELF_DIR="libelf-${LIBELF_VERSION}"
Stefan Reinauer074d9132009-09-26 16:43:17 +000073GCC_DIR="gcc-${GCC_VERSION}"
74BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
75GDB_DIR="gdb-${GDB_VERSION}"
Marc Jones2aac3f62011-08-08 16:07:50 -060076IASL_DIR="acpica-unix-${IASL_VERSION}"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +010077PYTHON_DIR="Python-${PYTHON_VERSION}"
78EXPAT_DIR="expat-${EXPAT_VERSION}"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070079# CLANG toolchain directories
80LLVM_DIR="llvm-${CLANG_VERSION}.src"
81CFE_DIR="cfe-${CLANG_VERSION}.src"
82CRT_DIR="compiler-rt-${CLANG_VERSION}.src"
83CTE_DIR="clang-tools-extra-${CLANG_VERSION}.src"
Stefan Reinauer074d9132009-09-26 16:43:17 +000084
Patrick Georgi6321f522015-02-27 23:36:26 +010085unset MAKELEVEL MAKEFLAGS
86
Patrick Georgi1861ff72011-10-31 12:15:55 +010087red='\033[0;31m'
88RED='\033[1;31m'
89green='\033[0;32m'
90GREEN='\033[1;32m'
91blue='\033[0;34m'
92BLUE='\033[1;34m'
93cyan='\033[0;36m'
94CYAN='\033[1;36m'
95NC='\033[0m' # No Color
Stefan Reinauer074d9132009-09-26 16:43:17 +000096
Stefan Reinauerd7649122015-06-09 11:44:25 -070097UNAME=$(uname)
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070098
Patrick Georgi198d23c2012-12-08 08:02:44 +010099normalize_dirs()
100{
101 mkdir -p $DESTDIR$TARGETDIR/lib
102 test -d $DESTDIR$TARGETDIR/lib32 && mv $DESTDIR$TARGETDIR/lib32/* $DESTDIR$TARGETDIR/lib
103 test -d $DESTDIR$TARGETDIR/lib64 && mv $DESTDIR$TARGETDIR/lib64/* $DESTDIR$TARGETDIR/lib
104 rmdir -p $DESTDIR$TARGETDIR/lib32 $DESTDIR$TARGETDIR/lib64
105
106 perl -pi -e "s,/lib32,/lib," $DESTDIR$TARGETDIR/lib/*.la
107 perl -pi -e "s,/lib64,/lib," $DESTDIR$TARGETDIR/lib/*.la
108}
109
Stefan Reinauer93a96302015-06-15 12:36:53 -0700110please_install()
111{
112 test -r /etc/os-release && source /etc/os-release
113 case "$ID_LIKE" in
114 debian) solution="sudo apt-get install $1" ;;
115 suse) solution="sudo zypper install $1" ;;
116 *) solution="using your OS packaging system" ;;
117 esac
118
119 printf "${RED}ERROR:${red} Missing tool: Please install \'$1\' utility. (eg $solution)${NC}\n" >&2
120}
121
Patrick Georgib7062882015-02-24 10:52:14 +0100122searchtool()
Stefan Reinauer074d9132009-09-26 16:43:17 +0000123{
124 # $1 short name
Patrick Georgib7062882015-02-24 10:52:14 +0100125 # $2 search string
126 # $3 soft fail if set
127 # result: file name of that tool on stdout
128 # or no output if nothing suitable was found
129 search=GNU
130 if [ -n "$2" ]; then
131 search="$2"
132 fi
Stefan Reinauer074d9132009-09-26 16:43:17 +0000133 for i in "$1" "g$1" "gnu$1"; do
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700134 if [ -x "$(which $i 2>/dev/null)" ]; then
135 if [ "$(cat /dev/null | $i --version 2>&1 | grep -c "$search")" \
136 -gt 0 ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000137 echo $i
138 return
139 fi
140 fi
141 done
Andrew Wub67e9a12014-04-28 18:13:44 +0800142 # A workaround for OSX 10.9 and some BSDs, whose nongnu
143 # patch and tar also work.
Patrick Georgi7cb26b42015-04-28 21:29:22 +0200144 if [ $UNAME = "Darwin" -o $UNAME = "FreeBSD" -o $UNAME = "NetBSD" -o $UNAME = "OpenBSD" ]; then
Patrick Georgib7062882015-02-24 10:52:14 +0100145 if [ "$1" = "patch" -o "$1" = "tar" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700146 if [ -x "$(which $1 2>/dev/null)" ]; then
Zheng Bao36156ff2012-09-28 16:18:58 +0800147 echo $1
148 return
149 fi
150 fi
151 fi
Stefan Reinauerd7649122015-06-09 11:44:25 -0700152 if [ "$(echo $1 | cut -b -3)" = "sha" ]; then
zbao939dc842015-04-30 04:44:07 +0800153 if [ $UNAME = "FreeBSD" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700154 if [ -x "$(which sha1 2>/dev/null)" ]; then
zbao939dc842015-04-30 04:44:07 +0800155 echo sha1
156 return
157 fi
158 fi
159 if [ $UNAME = "NetBSD" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700160 if [ -x "$(which cksum 2>/dev/null)" ]; then
Stefan Reinauerd7649122015-06-09 11:44:25 -0700161 echo cksum -a $(echo $1 | sed -e 's,sum,,')
zbao939dc842015-04-30 04:44:07 +0800162 return
163 fi
164 fi
165 if [ $UNAME = "Darwin" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700166 if [ -x "$(which openssl 2>/dev/null)" ]; then
Stefan Reinauerd7649122015-06-09 11:44:25 -0700167 echo openssl $(echo $1 | sed -e 's,sum,,')
zbao939dc842015-04-30 04:44:07 +0800168 return
169 fi
170 fi
171 fi
Stefan Reinauer93a96302015-06-15 12:36:53 -0700172 please_install $1
Patrick Georgib7062882015-02-24 10:52:14 +0100173 [ -z "$3" ] && exit 1
174 false
Stefan Reinauer074d9132009-09-26 16:43:17 +0000175}
176
Stefan Reinauerd7649122015-06-09 11:44:25 -0700177TAR=$(searchtool tar) || exit $?
178PATCH=$(searchtool patch) || exit $?
179MAKE=$(searchtool make) || exit $?
Stefan Reinauerd7649122015-06-09 11:44:25 -0700180SHA1SUM=$(searchtool sha1sum)
181SHA512SUM=$(searchtool sha512sum)
Stefan Reinauer699ac272015-06-05 12:43:28 -0700182CHECKSUM=$SHA1SUM
Stefan Reinauer074d9132009-09-26 16:43:17 +0000183
Patrick Georgib7062882015-02-24 10:52:14 +0100184searchtool m4 > /dev/null
185searchtool bison > /dev/null
186searchtool flex flex > /dev/null
187searchtool g++ "Free Software Foundation" nofail > /dev/null || \
Patrick Georgi915c4fc2015-03-09 13:58:23 +0100188searchtool clang "clang version" > /dev/null
189searchtool wget > /dev/null
190searchtool bzip2 "bzip2," > /dev/null
Patrick Georgi5fb2b5c2012-05-12 23:25:23 +0200191
Stefan Reinauer88935482015-06-05 09:51:10 -0700192download() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700193 package=$1
194 archive="$(eval echo \$$package"_ARCHIVE")"
Stefan Reinauer699ac272015-06-05 12:43:28 -0700195
Stefan Reinauerd7649122015-06-09 11:44:25 -0700196 FILE=$(basename $archive)
Stefan Reinauer88935482015-06-05 09:51:10 -0700197 printf " * $FILE "
198
199 test -f tarballs/$FILE && \
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700200 ( test -z "$CHECKSUM" || \
201 test "$(cat sum/$FILE.cksum 2>/dev/null | \
202 sed -e 's,.*\([0-9a-f]\{40\}\).*,\1,')" = \
203 "$($CHECKSUM tarballs/$FILE 2>/dev/null | \
204 sed -e 's,.*\([0-9a-f]\{40\}\).*,\1,')" ) && \
Stefan Reinauer88935482015-06-05 09:51:10 -0700205 printf "(cached)" || (
206 printf "(downloading)"
207 rm -f tarballs/$FILE
208 cd tarballs
Stefan Reinauer699ac272015-06-05 12:43:28 -0700209 wget --no-check-certificate -q $archive
Stefan Reinauer88935482015-06-05 09:51:10 -0700210 cd ..
211 test ! -f sum/$FILE.cksum && test -f tarballs/$FILE && \
212 (test -z "$CHECKSUM" || $CHECKSUM tarballs/$FILE > sum/$FILE.cksum ) && \
213 printf "(checksum created. ${RED}Note. Please upload sum/$FILE.cksum if the corresponding archive is upgraded.${NC})"
214 )
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700215 if [ ! -f tarballs/$FILE ]; then
Stefan Reinauer88935482015-06-05 09:51:10 -0700216 printf "\n${RED}Failed to download $FILE.${NC}\n"
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700217 exit 1
218 fi
Stefan Reinauer88935482015-06-05 09:51:10 -0700219 printf "\n"
220}
221
222unpack_and_patch() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700223 package=$1
224 archive="$(eval echo \$$package"_ARCHIVE")"
225 dir="$(eval echo \$$package"_DIR")"
Stefan Reinauer88935482015-06-05 09:51:10 -0700226 test -d ${dir} && test -f ${dir}/.unpack_success || (
Stefan Reinauerd7649122015-06-09 11:44:25 -0700227 printf " * $(basename $archive)\n"
Stefan Reinauer88935482015-06-05 09:51:10 -0700228 FLAGS=zxf
Stefan Reinauerd7649122015-06-09 11:44:25 -0700229 suffix=$(echo $archive | sed 's,.*\.,,')
Stefan Reinauer88935482015-06-05 09:51:10 -0700230 test "$suffix" = "gz" && FLAGS=zxf
231 test "$suffix" = "bz2" && FLAGS=jxf
232 test "$suffix" = "xz" && FLAGS="--xz -xf"
233 test "$suffix" = "lzma" && FLAGS="--lzma -xf"
Stefan Reinauerd7649122015-06-09 11:44:25 -0700234 $TAR $FLAGS tarballs/$(basename $archive)
Stefan Reinauer88935482015-06-05 09:51:10 -0700235 for patch in patches/${dir}_*.patch; do
236 test -r $patch || continue
Stefan Reinauerd7649122015-06-09 11:44:25 -0700237 printf " o $(basename $patch)\n"
Stefan Reinauer916b8452015-06-09 11:54:49 -0700238 $PATCH -s -N -p0 < $(echo $patch) || {
Stefan Reinauer88935482015-06-05 09:51:10 -0700239 printf "\n${RED}Failed $patch.${NC}\n"
Stefan Reinauer916b8452015-06-09 11:54:49 -0700240 exit 1
241 }
Stefan Reinauer88935482015-06-05 09:51:10 -0700242 done
243 touch ${dir}/.unpack_success
244 )
245}
246
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700247fn_exists()
248{
Patrick Georgic8665912015-06-11 21:04:31 +0200249 type $1 >/dev/null 2>&1
Patrick Georgi74c06422015-03-25 18:40:13 +0100250}
251
Patrick Georgif2c15f52015-06-11 21:07:31 +0200252is_package_enabled()
253{
254 echo "$PACKAGES" |grep -q "\<$1\>"
255}
256
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700257build() {
258 package=$1
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700259
260 fn_exists build_$package || return
261
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700262 version="$(eval echo \$$package"_VERSION")"
263 BUILDDIR=build-${TARGETARCH}-$package
264
265 mkdir -p ${BUILDDIR}
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700266
Patrick Georgif2c15f52015-06-11 21:07:31 +0200267 is_package_enabled "$package" && \
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700268 if [ -f ${BUILDDIR}/.success ]; then
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700269 printf "Skipping $package as it is already built\n"
270 else
271 printf "Building $package $version ... "
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700272 DIR=$PWD
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700273 cd ${BUILDDIR}
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700274 rm -f .failed
275 build_${package} > build.log 2>&1
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700276 cd $DIR/${BUILDDIR}
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700277 if [ ! -f .failed ]; then touch .success; fi
278 cd ..
279
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700280 if [ -r "${BUILDDIR}/.failed" ]; then
281 printf "${RED}failed${NC}. Check ${BUILDDIR}/build.log.\n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700282 exit 1
283 fi
284 printf "${green}ok${NC}\n"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700285 fi
286}
287
Stefan Reinauer074d9132009-09-26 16:43:17 +0000288cleanup()
289{
290 printf "Cleaning up temporary files... "
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700291 for package in $PACKAGES; do
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700292 rm -rf build-${TARGETARCH}-$package $(eval echo \$$package"_DIR")
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700293 done
Stefan Reinauer074d9132009-09-26 16:43:17 +0000294 printf "${green}ok${NC}\n"
295}
296
297myhelp()
298{
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700299 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>] [-C] [-G] [-S]\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000300 printf " $0 [-V|--version]\n"
301 printf " $0 [-h|--help]\n\n"
302
303 printf "Options:\n"
304 printf " [-V|--version] print version number and exit\n"
305 printf " [-h|--help] print this help and exit\n"
306 printf " [-c|--clean] remove temporary files before build\n"
307 printf " [-t|--savetemps] don't remove temporary files after build\n"
Patrick Georgic1a75b12011-11-04 21:37:14 +0100308 printf " [-y|--ccache] Use ccache when building cross compiler\n"
Patrick Georgi73166c72010-04-14 20:42:42 +0000309 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000310 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
311 printf " (defaults to $TARGETDIR)\n\n"
312 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100313 printf " (for RPM builds, default unset)\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700314 printf " [-P|--package <package>] Build a specific package: GCC, CLANG, IASL, GDB\n"
315 printf " (defaults to $PACKAGE)\n"
316 printf "GCC specific options:\n"
317 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
318 printf " (defaults to $TARGETARCH)\n"
319 printf "GDB specific options:\n"
320 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
321 printf " (defaults to $TARGETARCH)\n"
322 printf " [-S|--scripting] build scripting support for GDB\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000323}
324
325myversion()
326{
327 # version tag is always printed, so just print the license here
328
329 cat << EOF
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000330Copyright (C) 2008-2010 by coresystems GmbH
Marc Jones2aac3f62011-08-08 16:07:50 -0600331Copyright (C) 2011 by Sage Electronic Engineering
Stefan Reinauer074d9132009-09-26 16:43:17 +0000332
333This program is free software; you can redistribute it and/or modify
334it under the terms of the GNU General Public License as published by
335the Free Software Foundation; version 2 of the License.
336
337This program is distributed in the hope that it will be useful,
338but WITHOUT ANY WARRANTY; without even the implied warranty of
339MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
340GNU General Public License for more details.
341
342EOF
343}
344
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700345build_GMP() {
346 CC="$CC" CFLAGS="-Os" ../${GMP_DIR}/configure --disable-shared --enable-fat --prefix=$TARGETDIR $OPTIONS \
347 || touch .failed
348 $MAKE $JOBS || touch .failed
349 $MAKE install DESTDIR=$DESTDIR || touch .failed
350
351 normalize_dirs
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700352
353 # Now set CFLAGS to match GMP CFLAGS but strip out -pedantic
354 # as GCC 4.6.x fails if it's there.
355 export HOSTCFLAGS=$(grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2 |\
356 sed s,-pedantic,,)
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700357}
358
359build_MPFR() {
360 test $UNAME = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
361 CC="$CC" ../${MPFR_DIR}/configure --disable-shared --prefix=$TARGETDIR \
362 --infodir=$TARGETDIR/info \
363 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || \
364 touch .failed
365 $MAKE $JOBS || touch .failed
366 $MAKE install DESTDIR=$DESTDIR || touch .failed
367
368 normalize_dirs
369
370 # work around build problem of libgmp.la
371 if [ "$DESTDIR" != "" ]; then
372 perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
373 fi
374}
375
376build_MPC() {
377 CC="$CC" ../${MPC_DIR}/configure --disable-shared --prefix=$TARGETDIR \
378 --infodir=$TARGETDIR/info --with-mpfr=$DESTDIR$TARGETDIR \
379 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || \
380 touch .failed
381 $MAKE $JOBS || touch .failed
382 $MAKE install DESTDIR=$DESTDIR || touch .failed
383
384 normalize_dirs
385}
386
387build_LIBELF() {
388 CC="$CC" CFLAGS="$HOSTCFLAGS" libelf_cv_elf_h_works=no \
389 ../${LIBELF_DIR}/configure --disable-shared --prefix=$TARGETDIR \
390 --infodir=$TARGETDIR/info CFLAGS="$HOSTCFLAGS" || touch .failed
391 $MAKE $JOBS || touch .failed
392 $MAKE install prefix=$DESTDIR$TARGETDIR || touch .failed
393
394 normalize_dirs
395}
396
397build_BINUTILS() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700398 # Now build binutils
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700399 CC="$CC" ../binutils-${BINUTILS_VERSION}/configure --prefix=$TARGETDIR \
400 --target=${TARGETARCH} --disable-werror --disable-nls \
Stefan Reinauer2f97ebc2015-06-09 12:24:54 -0700401 --enable-lto --enable-gold --enable-plugins CFLAGS="$HOSTCFLAGS" || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700402 $MAKE $JOBS || touch .failed
403 $MAKE install DESTDIR=$DESTDIR || touch .failed
404}
405
406
407build_GCC() {
Stefan Reinauerd0f83722015-06-09 11:52:28 -0700408 # GCC does not honor HOSTCFLAGS at all. CFLAGS are used for
409 # both target and host object files.
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700410 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
411 # but it does not seem to work properly. At least the host library
412 # libiberty is not compiled with CFLAGS_FOR_BUILD.
413 CC="$CC" CFLAGS_FOR_TARGET="-O2" CFLAGS="$HOSTCFLAGS" \
414 CFLAGS_FOR_BUILD="$HOSTCFLAGS" ../gcc-${GCC_VERSION}/configure \
415 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
416 --target=${TARGETARCH} --disable-werror --disable-shared \
Stefan Reinauer2f97ebc2015-06-09 12:24:54 -0700417 --enable-lto --enable-plugins --enable-gold --enable-ld=default \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700418 --disable-libssp --disable-bootstrap --disable-nls \
419 --disable-libquadmath --without-headers \
Stefan Reinauer2f97ebc2015-06-09 12:24:54 -0700420 ${GCC_OPTIONS} --enable-languages="c" \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700421 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
422 --with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
423 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE" \
424 || touch .failed
425 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-gcc || touch .failed
426 $MAKE install-gcc DESTDIR=$DESTDIR || touch .failed
427
Stefan Reinauerd7649122015-06-09 11:44:25 -0700428 if [ "$(echo $TARGETARCH | grep -c -- -mingw32)" -eq 0 ]; then
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700429 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-target-libgcc || touch .failed
430 $MAKE install-target-libgcc DESTDIR=$DESTDIR || touch .failed
431 fi
432}
433
434build_EXPAT() {
435 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${EXPAT_DIR}/configure --disable-shared \
436 --prefix=$TARGETDIR --target=${TARGETARCH} || touch .failed
437 $MAKE || touch .failed
438 $MAKE install DESTDIR=$DESTDIR || touch .failed
439
440 normalize_dirs
441}
442
443build_PYTHON() {
444 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${PYTHON_DIR}/configure --prefix=$TARGETDIR \
445 --target=${TARGETARCH} || touch .failed
446 $MAKE $JOBS || touch .failed
447 $MAKE install DESTDIR=$DESTDIR || touch .failed
448
449 normalize_dirs
450}
451
452build_GDB() {
453 export PYTHONHOME=$DESTDIR$TARGETDIR
454 if [ $(uname) != "FreeBSD" -a $(uname) != "NetBSD" ]; then
455 LIBDL="-ldl"
456 fi
457 LDFLAGS="-Wl,-rpath,\$\$ORIGIN/../lib/ -L$DESTDIR$TARGETDIR/lib \
458 -lpthread $LIBDL -lutil" \
459 CC="$CC" CFLAGS="$HOSTCFLAGS -I$DESTDIR$TARGETDIR/include" \
Stefan Reinauer7b40e422015-06-09 11:56:32 -0700460 ../${GDB_DIR}/configure --prefix=$TARGETDIR \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700461 --target=${TARGETARCH} --disable-werror --disable-nls
462 $MAKE $JOBS || touch .failed
463 $MAKE install DESTDIR=$DESTDIR || touch .failed
464}
465
466build_IASL() {
467 RDIR=$PWD
468 cd ../$IASL_DIR/generate/unix
469 CFLAGS="$HOSTCFLAGS"
470 HOST="_LINUX"
471 test $UNAME = "Darwin" && HOST="_APPLE"
472 test $UNAME = "FreeBSD" && HOST="_FreeBSD"
473 test $UNAME = "Cygwin" && HOST="_CYGWIN"
474 HOST="$HOST" OPT_CFLAGS="-O -D_FORTIFY_SOURCE=2" CFLAGS="$CFLAGS" $MAKE CC="$CC" iasl || touch $RDIR/.failed
475 rm -f $DESTDIR$TARGETDIR/bin/iasl || touch $RDIR/.failed
476 cp bin/iasl $DESTDIR$TARGETDIR/bin || touch $RDIR/.failed
477}
478
479build_LLVM() {
480 cd ..
481 ln -sf $PWD/$CFE_DIR $LLVM_DIR/tools/clang
482 ln -sf $PWD/$CTE_DIR $LLVM_DIR/tools/clang/tools/extra
483 ln -sf $PWD/$CRT_DIR $LLVM_DIR/projects/compiler-rt
484 cd -
485
486 $CMAKE -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$DESTDIR$TARGETDIR \
487 -DCMAKE_BUILD_TYPE=Release ../$LLVM_DIR || touch .failed
488 $MAKE $JOBS || touch .failed
489 $MAKE install || touch .failed
490
491 cp -a ../$CFE_DIR/tools/scan-build/* $DESTDIR$TARGETDIR/bin
492 cp -a ../$CFE_DIR/tools/scan-view/* $DESTDIR$TARGETDIR/bin
493}
494
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700495printf "${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 +0000496
Patrick Georgi6bba29f2010-01-29 17:40:52 +0000497# Look if we have getopt. If not, build it.
498export PATH=$PATH:.
499getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
500
Stefan Reinauer074d9132009-09-26 16:43:17 +0000501# parse parameters.. try to find out whether we're running GNU getopt
Stefan Reinauerd7649122015-06-09 11:44:25 -0700502getoptbrand="$(getopt -V | sed -e '1!d' -e 's,^\(......\).*,\1,')"
Peter Stuge09377b72011-08-21 06:24:55 +0200503if [ "${getoptbrand}" = "getopt" ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000504 # Detected GNU getopt that supports long options.
Stefan Reinauerd7649122015-06-09 11:44:25 -0700505 args=$(getopt -l version,help,clean,directory:,platform:,jobs:,destdir:,savetemps,skip-gdb,ccache,clang Vhcd:p:j:D:tGyC -- "$@")
Stefan Reinauer074d9132009-09-26 16:43:17 +0000506 eval set "$args"
507else
508 # Detected non-GNU getopt
Stefan Reinauerd7649122015-06-09 11:44:25 -0700509 args=$(getopt Vhcd:p:j:D:tGyC $*)
Stefan Reinauer074d9132009-09-26 16:43:17 +0000510 set -- $args
511fi
512
513if [ $? != 0 ]; then
514 myhelp
515 exit 1
516fi
517
518while true ; do
519 case "$1" in
520 -V|--version) shift; myversion; exit 0;;
521 -h|--help) shift; myhelp; exit 0;;
Patrick Georgi44af57a2013-12-19 22:06:22 +0100522 -c|--clean) shift; clean=1;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000523 -t|--savetemps) shift; SAVETEMPS=1;;
524 -d|--directory) shift; TARGETDIR="$1"; shift;;
525 -p|--platform) shift; TARGETARCH="$1"; shift;;
526 -D|--destdir) shift; DESTDIR="$1"; shift;;
Patrick Georgi73166c72010-04-14 20:42:42 +0000527 -j|--jobs) shift; JOBS="-j $1"; shift;;
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700528 -P|--package) shift; PACKAGE="$1"; shift;;
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100529 -S|--scripting) shift; SKIPPYTHON=0;;
Patrick Georgic1a75b12011-11-04 21:37:14 +0100530 -y|--ccache) shift; USECCACHE=1;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200531 --) shift; break;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000532 -*) printf "Invalid option\n\n"; myhelp; exit 1;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200533 *) break;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000534 esac
535done
536
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700537case "$TARGETARCH" in
Stefan Reinauer24f9cb92015-03-13 22:50:22 +0100538 x86_64-elf) ;;
539 x86_64*) TARGETARCH=x86_64-elf;;
David Hubbard5b0420a2013-05-28 16:33:15 -0600540 i386-elf) ;;
541 i386-mingw32) ;;
Patrick Georgi9b7efa52015-03-08 09:20:36 +0100542 mipsel-elf) ;;
Patrick Georgif0bbc952015-03-07 10:57:25 +0100543 riscv-elf) ;;
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700544 i386*) TARGETARCH=i386-elf;;
Patrick Georgic8883262013-09-19 10:57:58 +0200545 arm*) TARGETARCH=armv7-a-eabi;;
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700546 aarch64*) TARGETARCH=aarch64-elf;;
547 *) printf "${red}WARNING: Unsupported architecture $TARGETARCH.${NC}\n\n"; ;;
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700548esac
549
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700550# Figure out which packages to build
Stefan Reinauer699ac272015-06-05 12:43:28 -0700551
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700552case "$PACKAGE" in
553 GCC|gcc)
554 echo "Target architecture is now $TARGETARCH"
555 NAME="${TARGETARCH} cross GCC"
556 PACKAGES="GMP MPFR MPC LIBELF BINUTILS GCC IASL"
557 ;;
558 GDB|gdb)
559 NAME="${TARGETARCH} cross GDB"
560 PACKAGES="GDB"
561 if [ $SKIPPYTHON -eq 0 ]; then
562 PACKAGES="EXPAT PYTHON $PACKAGES"
563 fi
564 ;;
565 CLANG|clang)
566 NAME=clang
567 PACKAGES="LLVM CFE CRT CTE"
David Hendrickscd5cdd32015-06-15 17:03:22 -0700568 CMAKE=$(searchtool cmake "cmake") || exit $?
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700569 ;;
570 IASL|iasl)
571 NAME="IASL ACPI compiler"
572 PACKAGES=IASL
573 ;;
574 *)
575 printf "${red}ERROR: Unsupported package $PACKAGE. (Supported packages are GCC, GDB, CLANG, IASL)${NC}\n\n";
576 exit 1
577 ;;
578esac
Peter Stugeceacd772011-06-09 04:54:16 +0200579
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700580# This initial cleanup is useful when updating the toolchain script.
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000581
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700582if [ "$clean" = "1" ]; then
583 cleanup
584fi
Stefan Reinauer074d9132009-09-26 16:43:17 +0000585
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700586# Set up host compiler and flags needed for various OSes
Stefan Reinauer074d9132009-09-26 16:43:17 +0000587
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700588CC=cc
Patrick Georgif2c15f52015-06-11 21:07:31 +0200589if is_package_enabled "GCC"; then
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700590if [ $UNAME = "Darwin" ]; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000591 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
592
593 # generally the OS X compiler can create x64 binaries.
594 # Per default it generated i386 binaries in 10.5 and x64
595 # binaries in 10.6 (even if the kernel is 32bit)
596 # For some weird reason, 10.5 autodetects an ABI=64 though
597 # so we're setting the ABI explicitly here.
Stefan Reinauerd7649122015-06-09 11:44:25 -0700598 if [ $(sysctl -n hw.optional.x86_64 2>/dev/null) -eq 1 ] 2>/dev/null; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000599 OPTIONS="ABI=64"
600 else
601 OPTIONS="ABI=32"
602 fi
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700603
Stefan Reinauer4266b922012-12-05 16:18:32 -0800604 # In Xcode 4.5.2 the default compiler is clang.
605 # However, this compiler fails to compile gcc 4.7.x. As a
606 # workaround it's possible to compile gcc with llvm-gcc.
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700607 if $CC -v 2>&1 | grep -q LLVM; then
Stefan Reinauer4266b922012-12-05 16:18:32 -0800608 CC=llvm-gcc
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700609 fi
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000610fi
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700611fi # GCC
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000612
Patrick Georgic1a75b12011-11-04 21:37:14 +0100613if [ "$USECCACHE" = 1 ]; then
614 CC="ccache $CC"
615fi
616
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700617# Prepare target directory for building GCC
618# (dependencies must be in the PATH)
Stefan Reinauer16bd7892012-12-07 23:57:01 +0100619mkdir -p $DESTDIR$TARGETDIR/bin
620export PATH=$DESTDIR$TARGETDIR/bin:$PATH
621
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700622# Download, unpack, patch and build all packages
623
624printf "Downloading tar balls ... \n"
625mkdir -p tarballs
626for P in $PACKAGES; do
627 download $P
628done
629printf "Downloaded tar balls ... ${green}ok${NC}\n"
630
631printf "Unpacking and patching ... \n"
632for P in $PACKAGES; do
633 unpack_and_patch $P || exit 1
634done
635printf "Unpacked and patched ... ${green}ok${NC}\n"
636
637printf "Building packages ... \n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700638for package in $PACKAGES; do
639 build $package
640done
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700641printf "Packages built ... ${green}ok${NC}\n"
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700642
Stefan Reinauer699ac272015-06-05 12:43:28 -0700643# Adding git information of current tree to target directory
644# for reproducibility
Stefan Reinauerd7649122015-06-09 11:44:25 -0700645PROGNAME=$(basename "$0")
Patrick Georgi5602f2c2015-03-28 15:48:47 +0100646rm -f "$DESTDIR$TARGETDIR/$PROGNAME".commit.*
Stefan Reinauerd7649122015-06-09 11:44:25 -0700647cp "$PROGNAME" $DESTDIR$TARGETDIR/"$PROGNAME.commit.$(git describe)"
Zheng Bao30b895f2013-02-06 18:04:40 +0800648
Stefan Reinauer074d9132009-09-26 16:43:17 +0000649if [ $SAVETEMPS -eq 0 ]; then
Stefan Reinauer699ac272015-06-05 12:43:28 -0700650 cleanup
Stefan Reinauer074d9132009-09-26 16:43:17 +0000651else
652 printf "Leaving temporary files around... ${green}ok${NC}\n"
653fi
654
Stefan Reinauer699ac272015-06-05 12:43:28 -0700655printf "\n${green}You can now run your $NAME toolchain from $TARGETDIR.${NC}\n"