blob: 49e41e537dfcd306bb853041b6f2585cd2bfb08f [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
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 Georgiaf473ed2015-07-17 23:35:54 +020041GCC_VERSION=5.2.0
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
Stefan Reinaueradf61e22015-07-07 00:41:42 +020045IASL_VERSION=20150619
Patrick Georgi53c388f2015-03-07 09:55:18 +010046PYTHON_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
zbaoe05c1ea2015-08-05 23:10:05 -040097UNAME=$(uname | grep -iq cygwin && echo Cygwin || 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{
Patrick Georgi5da95dc2015-07-17 23:33:05 +0200112 test -r /etc/os-release && . /etc/os-release
Stefan Reinauer93a96302015-06-15 12:36:53 -0700113 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
zbao06e85ac2015-08-24 22:08:36 -0400177check_sum() {
178 test -z "$CHECKSUM" || \
179 test "$(cat sum/$1.cksum 2>/dev/null | sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@')" = \
180 "$($CHECKSUM tarballs/$1 2>/dev/null | sed -e 's@.*\([0-9a-f]\{40,\}\).*@\1@')"
181}
182
183compute_sum() {
184 test ! -f sum/$1.cksum && test -f tarballs/$1 && \
185 (test -z "$CHECKSUM" || $CHECKSUM tarballs/$1 > sum/$1.cksum ) && \
186 printf "(checksum created. ${RED}Note. Please upload sum/$1.cksum if the corresponding archive is upgraded.)${NC}"
187}
188
Stefan Reinauer88935482015-06-05 09:51:10 -0700189download() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700190 package=$1
191 archive="$(eval echo \$$package"_ARCHIVE")"
Stefan Reinauer699ac272015-06-05 12:43:28 -0700192
Stefan Reinauerd7649122015-06-09 11:44:25 -0700193 FILE=$(basename $archive)
Stefan Reinauer88935482015-06-05 09:51:10 -0700194 printf " * $FILE "
195
zbaod22b9ea2015-08-30 23:35:16 -0400196 if test -f tarballs/$FILE && check_sum $FILE ; then
197 printf "(cached)"
198 else
Paul Menzelb2ba3952015-06-12 11:58:14 +0200199 printf "(downloading from $archive)"
Stefan Reinauer88935482015-06-05 09:51:10 -0700200 rm -f tarballs/$FILE
201 cd tarballs
Stefan Reinauer699ac272015-06-05 12:43:28 -0700202 wget --no-check-certificate -q $archive
zbaod22b9ea2015-08-30 23:35:16 -0400203 wgetret=$?
Stefan Reinauer88935482015-06-05 09:51:10 -0700204 cd ..
zbao06e85ac2015-08-24 22:08:36 -0400205 compute_sum $FILE
zbaod22b9ea2015-08-30 23:35:16 -0400206 fi
207
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700208 if [ ! -f tarballs/$FILE ]; then
zbaod22b9ea2015-08-30 23:35:16 -0400209 printf "\n${RED}Failed to download $FILE. Wget returns $wgetret. See 'man wget'.${NC}\n"
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700210 exit 1
211 fi
Stefan Reinauer88935482015-06-05 09:51:10 -0700212 printf "\n"
213}
214
215unpack_and_patch() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700216 package=$1
217 archive="$(eval echo \$$package"_ARCHIVE")"
218 dir="$(eval echo \$$package"_DIR")"
Stefan Reinauer88935482015-06-05 09:51:10 -0700219 test -d ${dir} && test -f ${dir}/.unpack_success || (
Stefan Reinauerd7649122015-06-09 11:44:25 -0700220 printf " * $(basename $archive)\n"
Stefan Reinauer88935482015-06-05 09:51:10 -0700221 FLAGS=zxf
Stefan Reinauerd7649122015-06-09 11:44:25 -0700222 suffix=$(echo $archive | sed 's,.*\.,,')
Stefan Reinauer88935482015-06-05 09:51:10 -0700223 test "$suffix" = "gz" && FLAGS=zxf
224 test "$suffix" = "bz2" && FLAGS=jxf
225 test "$suffix" = "xz" && FLAGS="--xz -xf"
226 test "$suffix" = "lzma" && FLAGS="--lzma -xf"
Stefan Reinauerd7649122015-06-09 11:44:25 -0700227 $TAR $FLAGS tarballs/$(basename $archive)
Stefan Reinauer88935482015-06-05 09:51:10 -0700228 for patch in patches/${dir}_*.patch; do
229 test -r $patch || continue
Stefan Reinauerd7649122015-06-09 11:44:25 -0700230 printf " o $(basename $patch)\n"
Stefan Reinauer916b8452015-06-09 11:54:49 -0700231 $PATCH -s -N -p0 < $(echo $patch) || {
Stefan Reinauer88935482015-06-05 09:51:10 -0700232 printf "\n${RED}Failed $patch.${NC}\n"
Stefan Reinauer916b8452015-06-09 11:54:49 -0700233 exit 1
234 }
Stefan Reinauer88935482015-06-05 09:51:10 -0700235 done
236 touch ${dir}/.unpack_success
237 )
238}
239
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700240fn_exists()
241{
Patrick Georgic8665912015-06-11 21:04:31 +0200242 type $1 >/dev/null 2>&1
Patrick Georgi74c06422015-03-25 18:40:13 +0100243}
244
Patrick Georgif2c15f52015-06-11 21:07:31 +0200245is_package_enabled()
246{
247 echo "$PACKAGES" |grep -q "\<$1\>"
248}
249
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700250build() {
251 package=$1
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700252
253 fn_exists build_$package || return
254
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700255 version="$(eval echo \$$package"_VERSION")"
256 BUILDDIR=build-${TARGETARCH}-$package
257
258 mkdir -p ${BUILDDIR}
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700259
Patrick Georgif2c15f52015-06-11 21:07:31 +0200260 is_package_enabled "$package" && \
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700261 if [ -f ${BUILDDIR}/.success ]; then
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700262 printf "Skipping $package as it is already built\n"
263 else
264 printf "Building $package $version ... "
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700265 DIR=$PWD
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700266 cd ${BUILDDIR}
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700267 rm -f .failed
268 build_${package} > build.log 2>&1
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700269 cd $DIR/${BUILDDIR}
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700270 if [ ! -f .failed ]; then touch .success; fi
271 cd ..
272
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700273 if [ -r "${BUILDDIR}/.failed" ]; then
274 printf "${RED}failed${NC}. Check ${BUILDDIR}/build.log.\n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700275 exit 1
276 fi
277 printf "${green}ok${NC}\n"
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700278 fi
279}
280
Stefan Reinauer074d9132009-09-26 16:43:17 +0000281cleanup()
282{
283 printf "Cleaning up temporary files... "
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700284 for package in $PACKAGES; do
Stefan Reinauer05bbc9202015-06-09 12:21:42 -0700285 rm -rf build-${TARGETARCH}-$package $(eval echo \$$package"_DIR")
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700286 done
zbaoff3f15c2015-08-26 22:00:31 -0400287 rm -f getopt
Stefan Reinauer074d9132009-09-26 16:43:17 +0000288 printf "${green}ok${NC}\n"
289}
290
291myhelp()
292{
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700293 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>] [-C] [-G] [-S]\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000294 printf " $0 [-V|--version]\n"
295 printf " $0 [-h|--help]\n\n"
296
297 printf "Options:\n"
298 printf " [-V|--version] print version number and exit\n"
299 printf " [-h|--help] print this help and exit\n"
300 printf " [-c|--clean] remove temporary files before build\n"
301 printf " [-t|--savetemps] don't remove temporary files after build\n"
Patrick Georgic1a75b12011-11-04 21:37:14 +0100302 printf " [-y|--ccache] Use ccache when building cross compiler\n"
Patrick Georgi73166c72010-04-14 20:42:42 +0000303 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000304 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
305 printf " (defaults to $TARGETDIR)\n\n"
306 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100307 printf " (for RPM builds, default unset)\n"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700308 printf " [-P|--package <package>] Build a specific package: GCC, CLANG, IASL, GDB\n"
309 printf " (defaults to $PACKAGE)\n"
310 printf "GCC specific options:\n"
311 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
312 printf " (defaults to $TARGETARCH)\n"
313 printf "GDB specific options:\n"
314 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
315 printf " (defaults to $TARGETARCH)\n"
316 printf " [-S|--scripting] build scripting support for GDB\n\n"
Martin Rothd1da74f2015-06-21 12:06:28 -0600317 printf "Platforms for GCC & GDB:\n"
318 printf " x86_64 i386-elf i386-mingw32 mipsel-elf riscv-elf arm aarch64\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000319}
320
321myversion()
322{
323 # version tag is always printed, so just print the license here
324
325 cat << EOF
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000326Copyright (C) 2008-2010 by coresystems GmbH
Marc Jones2aac3f62011-08-08 16:07:50 -0600327Copyright (C) 2011 by Sage Electronic Engineering
Stefan Reinauer074d9132009-09-26 16:43:17 +0000328
329This program is free software; you can redistribute it and/or modify
330it under the terms of the GNU General Public License as published by
331the Free Software Foundation; version 2 of the License.
332
333This program is distributed in the hope that it will be useful,
334but WITHOUT ANY WARRANTY; without even the implied warranty of
335MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
336GNU General Public License for more details.
337
338EOF
339}
340
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700341build_GMP() {
342 CC="$CC" CFLAGS="-Os" ../${GMP_DIR}/configure --disable-shared --enable-fat --prefix=$TARGETDIR $OPTIONS \
343 || touch .failed
344 $MAKE $JOBS || touch .failed
345 $MAKE install DESTDIR=$DESTDIR || touch .failed
346
347 normalize_dirs
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700348
349 # Now set CFLAGS to match GMP CFLAGS but strip out -pedantic
350 # as GCC 4.6.x fails if it's there.
351 export HOSTCFLAGS=$(grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2 |\
352 sed s,-pedantic,,)
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700353}
354
355build_MPFR() {
356 test $UNAME = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
357 CC="$CC" ../${MPFR_DIR}/configure --disable-shared --prefix=$TARGETDIR \
358 --infodir=$TARGETDIR/info \
359 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || \
360 touch .failed
361 $MAKE $JOBS || touch .failed
362 $MAKE install DESTDIR=$DESTDIR || touch .failed
363
364 normalize_dirs
365
366 # work around build problem of libgmp.la
367 if [ "$DESTDIR" != "" ]; then
368 perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
369 fi
370}
371
372build_MPC() {
373 CC="$CC" ../${MPC_DIR}/configure --disable-shared --prefix=$TARGETDIR \
374 --infodir=$TARGETDIR/info --with-mpfr=$DESTDIR$TARGETDIR \
375 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || \
376 touch .failed
377 $MAKE $JOBS || touch .failed
378 $MAKE install DESTDIR=$DESTDIR || touch .failed
379
380 normalize_dirs
381}
382
383build_LIBELF() {
384 CC="$CC" CFLAGS="$HOSTCFLAGS" libelf_cv_elf_h_works=no \
385 ../${LIBELF_DIR}/configure --disable-shared --prefix=$TARGETDIR \
386 --infodir=$TARGETDIR/info CFLAGS="$HOSTCFLAGS" || touch .failed
387 $MAKE $JOBS || touch .failed
388 $MAKE install prefix=$DESTDIR$TARGETDIR || touch .failed
389
390 normalize_dirs
391}
392
393build_BINUTILS() {
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700394 if [ $TARGETARCH == "x86_64-elf" ]; then
395 ADDITIONALTARGET=",i386-elf"
396 fi
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700397 CC="$CC" ../binutils-${BINUTILS_VERSION}/configure --prefix=$TARGETDIR \
Stefan Reinauer682a90c2015-06-09 14:59:58 -0700398 --target=${TARGETARCH} --enable-targets=${TARGETARCH}${ADDITIONALTARGET} \
399 --disable-werror --disable-nls --enable-lto --enable-gold \
400 --enable-plugins --enable-multilibs CFLAGS="$HOSTCFLAGS" || touch .failed
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700401 $MAKE $JOBS || touch .failed
402 $MAKE install DESTDIR=$DESTDIR || touch .failed
403}
404
405
406build_GCC() {
Stefan Reinauerd0f83722015-06-09 11:52:28 -0700407 # GCC does not honor HOSTCFLAGS at all. CFLAGS are used for
408 # both target and host object files.
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700409 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
410 # but it does not seem to work properly. At least the host library
411 # libiberty is not compiled with CFLAGS_FOR_BUILD.
412 CC="$CC" CFLAGS_FOR_TARGET="-O2" CFLAGS="$HOSTCFLAGS" \
413 CFLAGS_FOR_BUILD="$HOSTCFLAGS" ../gcc-${GCC_VERSION}/configure \
414 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
415 --target=${TARGETARCH} --disable-werror --disable-shared \
Stefan Reinauer2f97ebc2015-06-09 12:24:54 -0700416 --enable-lto --enable-plugins --enable-gold --enable-ld=default \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700417 --disable-libssp --disable-bootstrap --disable-nls \
418 --disable-libquadmath --without-headers \
Stefan Reinauer2f97ebc2015-06-09 12:24:54 -0700419 ${GCC_OPTIONS} --enable-languages="c" \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700420 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
421 --with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
422 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE" \
423 || touch .failed
424 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-gcc || touch .failed
425 $MAKE install-gcc DESTDIR=$DESTDIR || touch .failed
426
Stefan Reinauerd7649122015-06-09 11:44:25 -0700427 if [ "$(echo $TARGETARCH | grep -c -- -mingw32)" -eq 0 ]; then
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700428 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-target-libgcc || touch .failed
429 $MAKE install-target-libgcc DESTDIR=$DESTDIR || touch .failed
430 fi
431}
432
433build_EXPAT() {
434 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${EXPAT_DIR}/configure --disable-shared \
435 --prefix=$TARGETDIR --target=${TARGETARCH} || touch .failed
436 $MAKE || touch .failed
437 $MAKE install DESTDIR=$DESTDIR || touch .failed
438
439 normalize_dirs
440}
441
442build_PYTHON() {
443 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${PYTHON_DIR}/configure --prefix=$TARGETDIR \
444 --target=${TARGETARCH} || touch .failed
445 $MAKE $JOBS || touch .failed
446 $MAKE install DESTDIR=$DESTDIR || touch .failed
447
448 normalize_dirs
449}
450
451build_GDB() {
452 export PYTHONHOME=$DESTDIR$TARGETDIR
453 if [ $(uname) != "FreeBSD" -a $(uname) != "NetBSD" ]; then
454 LIBDL="-ldl"
455 fi
456 LDFLAGS="-Wl,-rpath,\$\$ORIGIN/../lib/ -L$DESTDIR$TARGETDIR/lib \
457 -lpthread $LIBDL -lutil" \
458 CC="$CC" CFLAGS="$HOSTCFLAGS -I$DESTDIR$TARGETDIR/include" \
Stefan Reinauer7b40e422015-06-09 11:56:32 -0700459 ../${GDB_DIR}/configure --prefix=$TARGETDIR \
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700460 --target=${TARGETARCH} --disable-werror --disable-nls
461 $MAKE $JOBS || touch .failed
462 $MAKE install DESTDIR=$DESTDIR || touch .failed
463}
464
465build_IASL() {
466 RDIR=$PWD
467 cd ../$IASL_DIR/generate/unix
468 CFLAGS="$HOSTCFLAGS"
469 HOST="_LINUX"
470 test $UNAME = "Darwin" && HOST="_APPLE"
471 test $UNAME = "FreeBSD" && HOST="_FreeBSD"
472 test $UNAME = "Cygwin" && HOST="_CYGWIN"
473 HOST="$HOST" OPT_CFLAGS="-O -D_FORTIFY_SOURCE=2" CFLAGS="$CFLAGS" $MAKE CC="$CC" iasl || touch $RDIR/.failed
474 rm -f $DESTDIR$TARGETDIR/bin/iasl || touch $RDIR/.failed
475 cp bin/iasl $DESTDIR$TARGETDIR/bin || touch $RDIR/.failed
476}
477
478build_LLVM() {
479 cd ..
480 ln -sf $PWD/$CFE_DIR $LLVM_DIR/tools/clang
481 ln -sf $PWD/$CTE_DIR $LLVM_DIR/tools/clang/tools/extra
482 ln -sf $PWD/$CRT_DIR $LLVM_DIR/projects/compiler-rt
483 cd -
484
485 $CMAKE -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$DESTDIR$TARGETDIR \
486 -DCMAKE_BUILD_TYPE=Release ../$LLVM_DIR || touch .failed
487 $MAKE $JOBS || touch .failed
488 $MAKE install || touch .failed
489
490 cp -a ../$CFE_DIR/tools/scan-build/* $DESTDIR$TARGETDIR/bin
491 cp -a ../$CFE_DIR/tools/scan-view/* $DESTDIR$TARGETDIR/bin
Stefan Reinauere9e6e3d2015-07-07 00:20:42 +0200492
493 # create symlinks to work around broken --print-librt-file-name
494 # when used with -target.
495 cd $DESTDIR$TARGETDIR/lib/clang/${CLANG_VERSION}/lib
496 for i in */libclang_rt.builtins*.a; do
497 ln -s $i .
498 done
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700499}
500
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700501printf "${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 +0000502
Patrick Georgi6bba29f2010-01-29 17:40:52 +0000503# Look if we have getopt. If not, build it.
504export PATH=$PATH:.
505getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
506
Stefan Reinauer074d9132009-09-26 16:43:17 +0000507# parse parameters.. try to find out whether we're running GNU getopt
Stefan Reinauerd7649122015-06-09 11:44:25 -0700508getoptbrand="$(getopt -V | sed -e '1!d' -e 's,^\(......\).*,\1,')"
Peter Stuge09377b72011-08-21 06:24:55 +0200509if [ "${getoptbrand}" = "getopt" ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000510 # Detected GNU getopt that supports long options.
zbao433fa572015-08-11 02:43:46 -0400511 args=$(getopt -l version,help,clean,directory:,platform:,package:,jobs:,destdir:,savetemps,scripting,ccache Vhcd:p:P:j:D:tSy -- "$@")
Stefan Reinauer074d9132009-09-26 16:43:17 +0000512 eval set "$args"
513else
514 # Detected non-GNU getopt
zbao433fa572015-08-11 02:43:46 -0400515 args=$(getopt Vhcd:p:P:j:D:tSy $*)
Stefan Reinauer074d9132009-09-26 16:43:17 +0000516 set -- $args
517fi
518
519if [ $? != 0 ]; then
520 myhelp
521 exit 1
522fi
523
524while true ; do
525 case "$1" in
526 -V|--version) shift; myversion; exit 0;;
527 -h|--help) shift; myhelp; exit 0;;
Patrick Georgi44af57a2013-12-19 22:06:22 +0100528 -c|--clean) shift; clean=1;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000529 -t|--savetemps) shift; SAVETEMPS=1;;
530 -d|--directory) shift; TARGETDIR="$1"; shift;;
531 -p|--platform) shift; TARGETARCH="$1"; shift;;
532 -D|--destdir) shift; DESTDIR="$1"; shift;;
Patrick Georgi73166c72010-04-14 20:42:42 +0000533 -j|--jobs) shift; JOBS="-j $1"; shift;;
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700534 -P|--package) shift; PACKAGE="$1"; shift;;
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100535 -S|--scripting) shift; SKIPPYTHON=0;;
Patrick Georgic1a75b12011-11-04 21:37:14 +0100536 -y|--ccache) shift; USECCACHE=1;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200537 --) shift; break;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000538 -*) printf "Invalid option\n\n"; myhelp; exit 1;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200539 *) break;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000540 esac
541done
542
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700543case "$TARGETARCH" in
Stefan Reinauer24f9cb92015-03-13 22:50:22 +0100544 x86_64-elf) ;;
545 x86_64*) TARGETARCH=x86_64-elf;;
David Hubbard5b0420a2013-05-28 16:33:15 -0600546 i386-elf) ;;
547 i386-mingw32) ;;
Patrick Georgi9b7efa52015-03-08 09:20:36 +0100548 mipsel-elf) ;;
Patrick Georgif0bbc952015-03-07 10:57:25 +0100549 riscv-elf) ;;
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700550 i386*) TARGETARCH=i386-elf;;
Patrick Georgic8883262013-09-19 10:57:58 +0200551 arm*) TARGETARCH=armv7-a-eabi;;
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700552 aarch64*) TARGETARCH=aarch64-elf;;
553 *) printf "${red}WARNING: Unsupported architecture $TARGETARCH.${NC}\n\n"; ;;
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700554esac
555
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700556# Figure out which packages to build
Stefan Reinauer699ac272015-06-05 12:43:28 -0700557
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700558case "$PACKAGE" in
559 GCC|gcc)
560 echo "Target architecture is now $TARGETARCH"
561 NAME="${TARGETARCH} cross GCC"
Stefan Reinauerbd8b2e32015-06-17 11:55:02 -0700562 PACKAGES="GMP MPFR MPC LIBELF BINUTILS GCC"
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700563 ;;
564 GDB|gdb)
565 NAME="${TARGETARCH} cross GDB"
566 PACKAGES="GDB"
567 if [ $SKIPPYTHON -eq 0 ]; then
568 PACKAGES="EXPAT PYTHON $PACKAGES"
569 fi
570 ;;
571 CLANG|clang)
572 NAME=clang
573 PACKAGES="LLVM CFE CRT CTE"
David Hendrickscd5cdd32015-06-15 17:03:22 -0700574 CMAKE=$(searchtool cmake "cmake") || exit $?
Stefan Reinauer85b07d62015-06-09 14:45:14 -0700575 ;;
576 IASL|iasl)
577 NAME="IASL ACPI compiler"
578 PACKAGES=IASL
579 ;;
580 *)
581 printf "${red}ERROR: Unsupported package $PACKAGE. (Supported packages are GCC, GDB, CLANG, IASL)${NC}\n\n";
582 exit 1
583 ;;
584esac
Peter Stugeceacd772011-06-09 04:54:16 +0200585
Stefan Reinauer66e93352015-06-16 22:38:23 +0200586# Find all the required tools:
587
588TAR=$(searchtool tar) || exit $?
589PATCH=$(searchtool patch) || exit $?
590MAKE=$(searchtool make) || exit $?
591SHA1SUM=$(searchtool sha1sum)
592SHA512SUM=$(searchtool sha512sum)
593CHECKSUM=$SHA1SUM
594
595searchtool m4 > /dev/null
596searchtool bison > /dev/null
597searchtool flex flex > /dev/null
598searchtool g++ "Free Software Foundation" nofail > /dev/null || \
Marc Jones5ee69512015-06-17 15:38:17 -0600599searchtool clang "clang version" nofail > /dev/null || \
600searchtool clang "LLVM" > /dev/null
Stefan Reinauer66e93352015-06-16 22:38:23 +0200601searchtool wget > /dev/null
602searchtool bzip2 "bzip2," > /dev/null
603
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700604# This initial cleanup is useful when updating the toolchain script.
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000605
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700606if [ "$clean" = "1" ]; then
607 cleanup
608fi
Stefan Reinauer074d9132009-09-26 16:43:17 +0000609
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700610# Set up host compiler and flags needed for various OSes
Stefan Reinauer074d9132009-09-26 16:43:17 +0000611
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700612CC=cc
Patrick Georgif2c15f52015-06-11 21:07:31 +0200613if is_package_enabled "GCC"; then
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700614if [ $UNAME = "Darwin" ]; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000615 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
616
617 # generally the OS X compiler can create x64 binaries.
618 # Per default it generated i386 binaries in 10.5 and x64
619 # binaries in 10.6 (even if the kernel is 32bit)
620 # For some weird reason, 10.5 autodetects an ABI=64 though
621 # so we're setting the ABI explicitly here.
Stefan Reinauerd7649122015-06-09 11:44:25 -0700622 if [ $(sysctl -n hw.optional.x86_64 2>/dev/null) -eq 1 ] 2>/dev/null; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000623 OPTIONS="ABI=64"
624 else
625 OPTIONS="ABI=32"
626 fi
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700627
Stefan Reinauer4266b922012-12-05 16:18:32 -0800628 # In Xcode 4.5.2 the default compiler is clang.
629 # However, this compiler fails to compile gcc 4.7.x. As a
630 # workaround it's possible to compile gcc with llvm-gcc.
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700631 if $CC -v 2>&1 | grep -q LLVM; then
Stefan Reinauer4266b922012-12-05 16:18:32 -0800632 CC=llvm-gcc
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700633 fi
zbao11f1d312015-08-05 23:15:54 -0400634elif [ $UNAME = "Linux" -o $UNAME = "Cygwin" ]; then
Patrick Georgiddb8f802015-07-04 17:45:54 +0200635 # gmp is overeager with detecting 64bit CPUs even if they run
636 # a 32bit kernel and userland.
637 if [ "$(uname -m 2>/dev/null)" = "i686" ]; then
638 OPTIONS="ABI=32"
639 fi
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000640fi
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700641fi # GCC
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000642
Patrick Georgic1a75b12011-11-04 21:37:14 +0100643if [ "$USECCACHE" = 1 ]; then
644 CC="ccache $CC"
645fi
646
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700647# Prepare target directory for building GCC
648# (dependencies must be in the PATH)
Stefan Reinauer16bd7892012-12-07 23:57:01 +0100649mkdir -p $DESTDIR$TARGETDIR/bin
650export PATH=$DESTDIR$TARGETDIR/bin:$PATH
651
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700652# Download, unpack, patch and build all packages
653
654printf "Downloading tar balls ... \n"
655mkdir -p tarballs
656for P in $PACKAGES; do
657 download $P
658done
659printf "Downloaded tar balls ... ${green}ok${NC}\n"
660
661printf "Unpacking and patching ... \n"
662for P in $PACKAGES; do
663 unpack_and_patch $P || exit 1
664done
665printf "Unpacked and patched ... ${green}ok${NC}\n"
666
667printf "Building packages ... \n"
Stefan Reinauer1c70e052015-06-09 11:47:28 -0700668for package in $PACKAGES; do
669 build $package
670done
Stefan Reinauer5dd26352015-06-09 14:52:22 -0700671printf "Packages built ... ${green}ok${NC}\n"
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700672
Stefan Reinauer699ac272015-06-05 12:43:28 -0700673# Adding git information of current tree to target directory
674# for reproducibility
Stefan Reinauerd7649122015-06-09 11:44:25 -0700675PROGNAME=$(basename "$0")
Patrick Georgi5602f2c2015-03-28 15:48:47 +0100676rm -f "$DESTDIR$TARGETDIR/$PROGNAME".commit.*
Stefan Reinauerd7649122015-06-09 11:44:25 -0700677cp "$PROGNAME" $DESTDIR$TARGETDIR/"$PROGNAME.commit.$(git describe)"
Zheng Bao30b895f2013-02-06 18:04:40 +0800678
Stefan Reinauer074d9132009-09-26 16:43:17 +0000679if [ $SAVETEMPS -eq 0 ]; then
Stefan Reinauer699ac272015-06-05 12:43:28 -0700680 cleanup
Stefan Reinauer074d9132009-09-26 16:43:17 +0000681else
682 printf "Leaving temporary files around... ${green}ok${NC}\n"
683fi
684
Stefan Reinauer699ac272015-06-05 12:43:28 -0700685printf "\n${green}You can now run your $NAME toolchain from $TARGETDIR.${NC}\n"