blob: a30ef24e4b980d208587977c972b45e6edcd2dec [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 Reinauer14ce2132015-06-05 13:01:13 -070025CROSSGCC_DATE="June 4th, 2015"
26CROSSGCC_VERSION="1.29"
Stefan Reinauer074d9132009-09-26 16:43:17 +000027
28# default settings
Stefan Reinauerd7649122015-06-09 11:44:25 -070029TARGETDIR=$(pwd)/xgcc
Stefan Reinauer074d9132009-09-26 16:43:17 +000030TARGETARCH=i386-elf
31DESTDIR=
32
Stefan Reinauer699ac272015-06-05 12:43:28 -070033# GCC toolchain version numbers
Patrick Georgi53c388f2015-03-07 09:55:18 +010034GMP_VERSION=6.0.0
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070035MPFR_VERSION=3.1.2
Paul Menzele6619422015-02-23 10:33:59 +010036MPC_VERSION=1.0.3
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000037LIBELF_VERSION=0.8.13
Patrick Georgi53c388f2015-03-07 09:55:18 +010038GCC_VERSION=4.9.2
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070039GCC_AUTOCONF_VERSION=2.69
Patrick Georgi53c388f2015-03-07 09:55:18 +010040BINUTILS_VERSION=2.25
41GDB_VERSION=7.9
42IASL_VERSION=20150204
43PYTHON_VERSION=3.4.3
Idwer Vollering296a0152012-10-26 01:48:04 +020044EXPAT_VERSION=2.1.0
Stefan Reinauer14ce2132015-06-05 13:01:13 -070045# CLANG version number
46CLANG_VERSION=3.6.1
Stefan Reinauer074d9132009-09-26 16:43:17 +000047
Stefan Reinauer699ac272015-06-05 12:43:28 -070048# GCC toolchain archive locations
Patrick Georgi53c388f2015-03-07 09:55:18 +010049GMP_ARCHIVE="http://ftpmirror.gnu.org/gmp/gmp-${GMP_VERSION}a.tar.bz2"
Patrick Georgi6e61ad32012-05-12 23:19:30 +020050MPFR_ARCHIVE="http://ftpmirror.gnu.org/mpfr/mpfr-${MPFR_VERSION}.tar.bz2"
Idwer Vollering7962fc72012-10-25 02:14:09 +020051MPC_ARCHIVE="http://ftpmirror.gnu.org/mpc/mpc-${MPC_VERSION}.tar.gz"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000052LIBELF_ARCHIVE="http://www.mr511.de/software/libelf-${LIBELF_VERSION}.tar.gz"
Idwer Vollering1cfee0b2012-10-25 02:03:53 +020053GCC_ARCHIVE="http://ftpmirror.gnu.org/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.bz2"
Stefan Reinauer0d2119d2013-07-10 14:27:56 -070054BINUTILS_ARCHIVE="http://ftpmirror.gnu.org/binutils/binutils-${BINUTILS_VERSION}.tar.bz2"
Patrick Georgi53c388f2015-03-07 09:55:18 +010055GDB_ARCHIVE="http://ftpmirror.gnu.org/gdb/gdb-${GDB_VERSION}.tar.xz"
56IASL_ARCHIVE="https://acpica.org/sites/acpica/files/acpica-unix-${IASL_VERSION}.tar.gz"
57PYTHON_ARCHIVE="http://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tar.xz"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +010058EXPAT_ARCHIVE="http://downloads.sourceforge.net/sourceforge/expat/expat-${EXPAT_VERSION}.tar.gz"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070059# CLANG toolchain archive locations
60LLVM_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/llvm-${CLANG_VERSION}.src.tar.xz"
61CFE_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/cfe-${CLANG_VERSION}.src.tar.xz"
62CRT_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/compiler-rt-${CLANG_VERSION}.src.tar.xz"
63CTE_ARCHIVE="http://llvm.org/releases/${CLANG_VERSION}/clang-tools-extra-${CLANG_VERSION}.src.tar.xz"
Stefan Reinauer074d9132009-09-26 16:43:17 +000064
Stefan Reinauer699ac272015-06-05 12:43:28 -070065# GCC toolchain directories
Stefan Reinauer074d9132009-09-26 16:43:17 +000066GMP_DIR="gmp-${GMP_VERSION}"
67MPFR_DIR="mpfr-${MPFR_VERSION}"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +000068MPC_DIR="mpc-${MPC_VERSION}"
69LIBELF_DIR="libelf-${LIBELF_VERSION}"
Stefan Reinauer074d9132009-09-26 16:43:17 +000070GCC_DIR="gcc-${GCC_VERSION}"
71BINUTILS_DIR="binutils-${BINUTILS_VERSION}"
72GDB_DIR="gdb-${GDB_VERSION}"
Marc Jones2aac3f62011-08-08 16:07:50 -060073IASL_DIR="acpica-unix-${IASL_VERSION}"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +010074PYTHON_DIR="Python-${PYTHON_VERSION}"
75EXPAT_DIR="expat-${EXPAT_VERSION}"
Stefan Reinauer14ce2132015-06-05 13:01:13 -070076# CLANG toolchain directories
77LLVM_DIR="llvm-${CLANG_VERSION}.src"
78CFE_DIR="cfe-${CLANG_VERSION}.src"
79CRT_DIR="compiler-rt-${CLANG_VERSION}.src"
80CTE_DIR="clang-tools-extra-${CLANG_VERSION}.src"
Stefan Reinauer074d9132009-09-26 16:43:17 +000081
Patrick Georgi6321f522015-02-27 23:36:26 +010082unset MAKELEVEL MAKEFLAGS
83
Stefan Reinauer074d9132009-09-26 16:43:17 +000084SAVETEMPS=0
Stefan Reinauer14ce2132015-06-05 13:01:13 -070085BUILDCLANG=0
Stefan Reinauer2c3cd122011-11-01 21:43:50 +010086SKIPGDB=1
87SKIPPYTHON=1
Stefan Reinauer074d9132009-09-26 16:43:17 +000088
Patrick Georgi1861ff72011-10-31 12:15:55 +010089red='\033[0;31m'
90RED='\033[1;31m'
91green='\033[0;32m'
92GREEN='\033[1;32m'
93blue='\033[0;34m'
94BLUE='\033[1;34m'
95cyan='\033[0;36m'
96CYAN='\033[1;36m'
97NC='\033[0m' # No Color
Stefan Reinauer074d9132009-09-26 16:43:17 +000098
Stefan Reinauerd7649122015-06-09 11:44:25 -070099UNAME=$(uname)
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700100
Patrick Georgi198d23c2012-12-08 08:02:44 +0100101normalize_dirs()
102{
103 mkdir -p $DESTDIR$TARGETDIR/lib
104 test -d $DESTDIR$TARGETDIR/lib32 && mv $DESTDIR$TARGETDIR/lib32/* $DESTDIR$TARGETDIR/lib
105 test -d $DESTDIR$TARGETDIR/lib64 && mv $DESTDIR$TARGETDIR/lib64/* $DESTDIR$TARGETDIR/lib
106 rmdir -p $DESTDIR$TARGETDIR/lib32 $DESTDIR$TARGETDIR/lib64
107
108 perl -pi -e "s,/lib32,/lib," $DESTDIR$TARGETDIR/lib/*.la
109 perl -pi -e "s,/lib64,/lib," $DESTDIR$TARGETDIR/lib/*.la
110}
111
Patrick Georgib7062882015-02-24 10:52:14 +0100112searchtool()
Stefan Reinauer074d9132009-09-26 16:43:17 +0000113{
114 # $1 short name
Patrick Georgib7062882015-02-24 10:52:14 +0100115 # $2 search string
116 # $3 soft fail if set
117 # result: file name of that tool on stdout
118 # or no output if nothing suitable was found
119 search=GNU
120 if [ -n "$2" ]; then
121 search="$2"
122 fi
Stefan Reinauer074d9132009-09-26 16:43:17 +0000123 for i in "$1" "g$1" "gnu$1"; do
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700124 if [ -x "$(which $i 2>/dev/null)" ]; then
125 if [ "$(cat /dev/null | $i --version 2>&1 | grep -c "$search")" \
126 -gt 0 ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000127 echo $i
128 return
129 fi
130 fi
131 done
Andrew Wub67e9a12014-04-28 18:13:44 +0800132 # A workaround for OSX 10.9 and some BSDs, whose nongnu
133 # patch and tar also work.
Patrick Georgi7cb26b42015-04-28 21:29:22 +0200134 if [ $UNAME = "Darwin" -o $UNAME = "FreeBSD" -o $UNAME = "NetBSD" -o $UNAME = "OpenBSD" ]; then
Patrick Georgib7062882015-02-24 10:52:14 +0100135 if [ "$1" = "patch" -o "$1" = "tar" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700136 if [ -x "$(which $1 2>/dev/null)" ]; then
Zheng Bao36156ff2012-09-28 16:18:58 +0800137 echo $1
138 return
139 fi
140 fi
141 fi
Stefan Reinauerd7649122015-06-09 11:44:25 -0700142 if [ "$(echo $1 | cut -b -3)" = "sha" ]; then
zbao939dc842015-04-30 04:44:07 +0800143 if [ $UNAME = "FreeBSD" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700144 if [ -x "$(which sha1 2>/dev/null)" ]; then
zbao939dc842015-04-30 04:44:07 +0800145 echo sha1
146 return
147 fi
148 fi
149 if [ $UNAME = "NetBSD" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700150 if [ -x "$(which cksum 2>/dev/null)" ]; then
Stefan Reinauerd7649122015-06-09 11:44:25 -0700151 echo cksum -a $(echo $1 | sed -e 's,sum,,')
zbao939dc842015-04-30 04:44:07 +0800152 return
153 fi
154 fi
155 if [ $UNAME = "Darwin" ]; then
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700156 if [ -x "$(which openssl 2>/dev/null)" ]; then
Stefan Reinauerd7649122015-06-09 11:44:25 -0700157 echo openssl $(echo $1 | sed -e 's,sum,,')
zbao939dc842015-04-30 04:44:07 +0800158 return
159 fi
160 fi
161 fi
Patrick Georgiae5ab602015-04-29 23:30:15 +0200162 printf "${RED}ERROR:${red} Missing tool: Please install $1 (eg using your OS packaging system)${NC}\n" >&2
Patrick Georgib7062882015-02-24 10:52:14 +0100163 [ -z "$3" ] && exit 1
164 false
Stefan Reinauer074d9132009-09-26 16:43:17 +0000165}
166
Stefan Reinauerd7649122015-06-09 11:44:25 -0700167TAR=$(searchtool tar) || exit $?
168PATCH=$(searchtool patch) || exit $?
169MAKE=$(searchtool make) || exit $?
170CMAKE=$(searchtool cmake "cmake") || exit $?
171SHA1SUM=$(searchtool sha1sum)
172SHA512SUM=$(searchtool sha512sum)
Stefan Reinauer699ac272015-06-05 12:43:28 -0700173CHECKSUM=$SHA1SUM
Stefan Reinauer074d9132009-09-26 16:43:17 +0000174
Patrick Georgib7062882015-02-24 10:52:14 +0100175searchtool m4 > /dev/null
176searchtool bison > /dev/null
177searchtool flex flex > /dev/null
178searchtool g++ "Free Software Foundation" nofail > /dev/null || \
Patrick Georgi915c4fc2015-03-09 13:58:23 +0100179searchtool clang "clang version" > /dev/null
180searchtool wget > /dev/null
181searchtool bzip2 "bzip2," > /dev/null
Patrick Georgi5fb2b5c2012-05-12 23:25:23 +0200182
Stefan Reinauer88935482015-06-05 09:51:10 -0700183download() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700184 package=$1
185 archive="$(eval echo \$$package"_ARCHIVE")"
Stefan Reinauer699ac272015-06-05 12:43:28 -0700186
Stefan Reinauerd7649122015-06-09 11:44:25 -0700187 FILE=$(basename $archive)
Stefan Reinauer88935482015-06-05 09:51:10 -0700188 printf " * $FILE "
189
190 test -f tarballs/$FILE && \
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700191 ( test -z "$CHECKSUM" || \
192 test "$(cat sum/$FILE.cksum 2>/dev/null | \
193 sed -e 's,.*\([0-9a-f]\{40\}\).*,\1,')" = \
194 "$($CHECKSUM tarballs/$FILE 2>/dev/null | \
195 sed -e 's,.*\([0-9a-f]\{40\}\).*,\1,')" ) && \
Stefan Reinauer88935482015-06-05 09:51:10 -0700196 printf "(cached)" || (
197 printf "(downloading)"
198 rm -f tarballs/$FILE
199 cd tarballs
Stefan Reinauer699ac272015-06-05 12:43:28 -0700200 wget --no-check-certificate -q $archive
Stefan Reinauer88935482015-06-05 09:51:10 -0700201 cd ..
202 test ! -f sum/$FILE.cksum && test -f tarballs/$FILE && \
203 (test -z "$CHECKSUM" || $CHECKSUM tarballs/$FILE > sum/$FILE.cksum ) && \
204 printf "(checksum created. ${RED}Note. Please upload sum/$FILE.cksum if the corresponding archive is upgraded.${NC})"
205 )
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700206 if [ ! -f tarballs/$FILE ]; then
Stefan Reinauer88935482015-06-05 09:51:10 -0700207 printf "\n${RED}Failed to download $FILE.${NC}\n"
Stefan Reinauerdbc00872015-06-09 11:50:05 -0700208 exit 1
209 fi
Stefan Reinauer88935482015-06-05 09:51:10 -0700210 printf "\n"
211}
212
213unpack_and_patch() {
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700214 package=$1
215 archive="$(eval echo \$$package"_ARCHIVE")"
216 dir="$(eval echo \$$package"_DIR")"
Stefan Reinauer88935482015-06-05 09:51:10 -0700217 test -d ${dir} && test -f ${dir}/.unpack_success || (
Stefan Reinauerd7649122015-06-09 11:44:25 -0700218 printf " * $(basename $archive)\n"
Stefan Reinauer88935482015-06-05 09:51:10 -0700219 FLAGS=zxf
Stefan Reinauerd7649122015-06-09 11:44:25 -0700220 suffix=$(echo $archive | sed 's,.*\.,,')
Stefan Reinauer88935482015-06-05 09:51:10 -0700221 test "$suffix" = "gz" && FLAGS=zxf
222 test "$suffix" = "bz2" && FLAGS=jxf
223 test "$suffix" = "xz" && FLAGS="--xz -xf"
224 test "$suffix" = "lzma" && FLAGS="--lzma -xf"
Stefan Reinauerd7649122015-06-09 11:44:25 -0700225 $TAR $FLAGS tarballs/$(basename $archive)
Stefan Reinauer88935482015-06-05 09:51:10 -0700226 for patch in patches/${dir}_*.patch; do
227 test -r $patch || continue
Stefan Reinauerd7649122015-06-09 11:44:25 -0700228 printf " o $(basename $patch)\n"
229 $PATCH -s -N -p0 < $(echo $patch) || \
Stefan Reinauer88935482015-06-05 09:51:10 -0700230 printf "\n${RED}Failed $patch.${NC}\n"
231 done
232 touch ${dir}/.unpack_success
233 )
234}
235
Patrick Georgi74c06422015-03-25 18:40:13 +0100236wait_for_build() {
237 # $1: directory in which log file and failure marker are stored
238 cat > "$1/crossgcc-build.log"
239 test -r "$1/.failed" && printf "${RED}failed${NC}. Check $1/crossgcc-build.log.\n" || \
240 printf "${green}ok${NC}\n"
241 test -r "$1/.failed" && exit 1
242 true
243}
244
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700245build() {
246 package=$1
247 version="$(eval echo \$$package"_VERSION")"
248 [[ "$PACKAGES" == *$package* ]] && \
249 if [ -f ${BUILDDIRPREFIX}-$package/.success ]; then
250 printf "Skipping $package as it is already built\n"
251 else
252 printf "Building $package $version ... "
253 (
254 cd ${BUILDDIRPREFIX}-$package
255 rm -f .failed
256 build_${package}
257 if [ ! -f .failed ]; then touch .success; fi
258 ) 2>&1 | wait_for_build "${BUILDDIRPREFIX}-$package" || exit 1
259 fi
260}
261
Stefan Reinauer074d9132009-09-26 16:43:17 +0000262cleanup()
263{
264 printf "Cleaning up temporary files... "
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700265 for package in $PACKAGES; do
266 rm -rf ${BUILDDIRPREFIX}-$package $(eval echo \$$package"_DIR")
267 done
Stefan Reinauer074d9132009-09-26 16:43:17 +0000268 printf "${green}ok${NC}\n"
269}
270
271myhelp()
272{
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700273 printf "Usage: $0 [-V] [-c] [-p <platform>] [-d <target directory>] [-D <dest dir>] [-C] [-G] [-S]\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000274 printf " $0 [-V|--version]\n"
275 printf " $0 [-h|--help]\n\n"
276
277 printf "Options:\n"
278 printf " [-V|--version] print version number and exit\n"
279 printf " [-h|--help] print this help and exit\n"
280 printf " [-c|--clean] remove temporary files before build\n"
281 printf " [-t|--savetemps] don't remove temporary files after build\n"
Patrick Georgic1a75b12011-11-04 21:37:14 +0100282 printf " [-y|--ccache] Use ccache when building cross compiler\n"
Patrick Georgi73166c72010-04-14 20:42:42 +0000283 printf " [-j|--jobs <num>] run <num> jobs in parallel in make\n"
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700284 printf " [-C|--clang] build CLANG toolchain"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000285 printf " [-p|--platform <platform>] target platform to build cross compiler for\n"
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700286 printf " (defaults to $TARGETARCH) *)\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000287 printf " [-d|--directory <target dir>] target directory to install cross compiler to\n"
288 printf " (defaults to $TARGETDIR)\n\n"
289 printf " [-D|--destdir <dest dir>] destination directory to install cross compiler to\n"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100290 printf " (for RPM builds, default unset)\n"
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700291 printf " [-G|--gdb] build GNU debugger *)\n"
292 printf " [-S|--scripting] build scripting support for GDB *)\n\n"
293 printf " *) option only available when building GCC toolchain (not with CLANG)\n\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000294}
295
296myversion()
297{
298 # version tag is always printed, so just print the license here
299
300 cat << EOF
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000301Copyright (C) 2008-2010 by coresystems GmbH
Marc Jones2aac3f62011-08-08 16:07:50 -0600302Copyright (C) 2011 by Sage Electronic Engineering
Stefan Reinauer074d9132009-09-26 16:43:17 +0000303
304This program is free software; you can redistribute it and/or modify
305it under the terms of the GNU General Public License as published by
306the Free Software Foundation; version 2 of the License.
307
308This program is distributed in the hope that it will be useful,
309but WITHOUT ANY WARRANTY; without even the implied warranty of
310MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
311GNU General Public License for more details.
312
313EOF
314}
315
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700316build_GMP() {
317 CC="$CC" CFLAGS="-Os" ../${GMP_DIR}/configure --disable-shared --enable-fat --prefix=$TARGETDIR $OPTIONS \
318 || touch .failed
319 $MAKE $JOBS || touch .failed
320 $MAKE install DESTDIR=$DESTDIR || touch .failed
321
322 normalize_dirs
323}
324
325build_MPFR() {
326 test $UNAME = "Darwin" && CFLAGS="$CFLAGS -force_cpusubtype_ALL"
327 CC="$CC" ../${MPFR_DIR}/configure --disable-shared --prefix=$TARGETDIR \
328 --infodir=$TARGETDIR/info \
329 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || \
330 touch .failed
331 $MAKE $JOBS || touch .failed
332 $MAKE install DESTDIR=$DESTDIR || touch .failed
333
334 normalize_dirs
335
336 # work around build problem of libgmp.la
337 if [ "$DESTDIR" != "" ]; then
338 perl -pi -e "s,$DESTDIR,," $DESTDIR$TARGETDIR/libgmp.la
339 fi
340}
341
342build_MPC() {
343 CC="$CC" ../${MPC_DIR}/configure --disable-shared --prefix=$TARGETDIR \
344 --infodir=$TARGETDIR/info --with-mpfr=$DESTDIR$TARGETDIR \
345 --with-gmp=$DESTDIR$TARGETDIR CFLAGS="$HOSTCFLAGS" || \
346 touch .failed
347 $MAKE $JOBS || touch .failed
348 $MAKE install DESTDIR=$DESTDIR || touch .failed
349
350 normalize_dirs
351}
352
353build_LIBELF() {
354 CC="$CC" CFLAGS="$HOSTCFLAGS" libelf_cv_elf_h_works=no \
355 ../${LIBELF_DIR}/configure --disable-shared --prefix=$TARGETDIR \
356 --infodir=$TARGETDIR/info CFLAGS="$HOSTCFLAGS" || touch .failed
357 $MAKE $JOBS || touch .failed
358 $MAKE install prefix=$DESTDIR$TARGETDIR || touch .failed
359
360 normalize_dirs
361}
362
363build_BINUTILS() {
364 # What a pain: binutils don't come with configure
365 # script anymore. Create it:
366 cd ../binutils-${BINUTILS_VERSION}/
367 autoconf
368 cd -
369 # Now build binutils
370 rm -f .failed
371 CC="$CC" ../binutils-${BINUTILS_VERSION}/configure --prefix=$TARGETDIR \
372 --target=${TARGETARCH} --disable-werror --disable-nls \
373 $USE_GOLD CFLAGS="$HOSTCFLAGS" || touch .failed
374 $MAKE $JOBS || touch .failed
375 $MAKE install DESTDIR=$DESTDIR || touch .failed
376}
377
378
379build_GCC() {
380 # Even worse than binutils: GCC does not come with configure
381 # script anymore, but also enforces an obsolete autoconf version
382 # to create it. This is a poster child of how autotools help make
383 # software portable.
384 cd ../gcc-${GCC_VERSION}
385 sed '/dnl Ensure exactly this Autoconf version is used/d' \
386 config/override.m4 > config/override.m4.new
Stefan Reinauerd7649122015-06-09 11:44:25 -0700387 autoconf_version=$(autoconf -V | grep "autoconf" | tr ' ' '\n' | tail -1)
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700388 sed "s/${GCC_AUTOCONF_VERSION}/${autoconf_version}/g" \
389 config/override.m4.new > config/override.m4
390 autoconf
391 cd -
392
393 # Now, finally, we can build gcc:
394 # GCC does not honour HOSTCFLAGS at all. CFLAGS are used for
395 # both target and host object files. This is pretty misdesigned.
396 # There's a work-around called CFLAGS_FOR_BUILD and CFLAGS_FOR_TARGET
397 # but it does not seem to work properly. At least the host library
398 # libiberty is not compiled with CFLAGS_FOR_BUILD.
399 CC="$CC" CFLAGS_FOR_TARGET="-O2" CFLAGS="$HOSTCFLAGS" \
400 CFLAGS_FOR_BUILD="$HOSTCFLAGS" ../gcc-${GCC_VERSION}/configure \
401 --prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
402 --target=${TARGETARCH} --disable-werror --disable-shared \
403 --disable-libssp --disable-bootstrap --disable-nls \
404 --disable-libquadmath --without-headers \
405 $GCC_OPTIONS --enable-languages="c" $USE_GOLD \
406 --with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
407 --with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
408 --with-pkgversion="coreboot toolchain v$CROSSGCC_VERSION $CROSSGCC_DATE" \
409 || touch .failed
410 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-gcc || touch .failed
411 $MAKE install-gcc DESTDIR=$DESTDIR || touch .failed
412
Stefan Reinauerd7649122015-06-09 11:44:25 -0700413 if [ "$(echo $TARGETARCH | grep -c -- -mingw32)" -eq 0 ]; then
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700414 $MAKE $JOBS CFLAGS_FOR_BUILD="$HOSTCFLAGS" all-target-libgcc || touch .failed
415 $MAKE install-target-libgcc DESTDIR=$DESTDIR || touch .failed
416 fi
417}
418
419build_EXPAT() {
420 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${EXPAT_DIR}/configure --disable-shared \
421 --prefix=$TARGETDIR --target=${TARGETARCH} || touch .failed
422 $MAKE || touch .failed
423 $MAKE install DESTDIR=$DESTDIR || touch .failed
424
425 normalize_dirs
426}
427
428build_PYTHON() {
429 CC="$CC" CFLAGS="$HOSTCFLAGS" ../${PYTHON_DIR}/configure --prefix=$TARGETDIR \
430 --target=${TARGETARCH} || touch .failed
431 $MAKE $JOBS || touch .failed
432 $MAKE install DESTDIR=$DESTDIR || touch .failed
433
434 normalize_dirs
435}
436
437build_GDB() {
438 export PYTHONHOME=$DESTDIR$TARGETDIR
439 if [ $(uname) != "FreeBSD" -a $(uname) != "NetBSD" ]; then
440 LIBDL="-ldl"
441 fi
442 LDFLAGS="-Wl,-rpath,\$\$ORIGIN/../lib/ -L$DESTDIR$TARGETDIR/lib \
443 -lpthread $LIBDL -lutil" \
444 CC="$CC" CFLAGS="$HOSTCFLAGS -I$DESTDIR$TARGETDIR/include" \
445 ../gdb-${GDB_VERSION}/configure --prefix=$TARGETDIR \
446 --target=${TARGETARCH} --disable-werror --disable-nls
447 $MAKE $JOBS || touch .failed
448 $MAKE install DESTDIR=$DESTDIR || touch .failed
449}
450
451build_IASL() {
452 RDIR=$PWD
453 cd ../$IASL_DIR/generate/unix
454 CFLAGS="$HOSTCFLAGS"
455 HOST="_LINUX"
456 test $UNAME = "Darwin" && HOST="_APPLE"
457 test $UNAME = "FreeBSD" && HOST="_FreeBSD"
458 test $UNAME = "Cygwin" && HOST="_CYGWIN"
459 HOST="$HOST" OPT_CFLAGS="-O -D_FORTIFY_SOURCE=2" CFLAGS="$CFLAGS" $MAKE CC="$CC" iasl || touch $RDIR/.failed
460 rm -f $DESTDIR$TARGETDIR/bin/iasl || touch $RDIR/.failed
461 cp bin/iasl $DESTDIR$TARGETDIR/bin || touch $RDIR/.failed
462}
463
464build_LLVM() {
465 cd ..
466 ln -sf $PWD/$CFE_DIR $LLVM_DIR/tools/clang
467 ln -sf $PWD/$CTE_DIR $LLVM_DIR/tools/clang/tools/extra
468 ln -sf $PWD/$CRT_DIR $LLVM_DIR/projects/compiler-rt
469 cd -
470
471 $CMAKE -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=$DESTDIR$TARGETDIR \
472 -DCMAKE_BUILD_TYPE=Release ../$LLVM_DIR || touch .failed
473 $MAKE $JOBS || touch .failed
474 $MAKE install || touch .failed
475
476 cp -a ../$CFE_DIR/tools/scan-build/* $DESTDIR$TARGETDIR/bin
477 cp -a ../$CFE_DIR/tools/scan-view/* $DESTDIR$TARGETDIR/bin
478}
479
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700480printf "${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 +0000481
Patrick Georgi6bba29f2010-01-29 17:40:52 +0000482# Look if we have getopt. If not, build it.
483export PATH=$PATH:.
484getopt - > /dev/null 2>/dev/null || gcc -o getopt getopt.c
485
Stefan Reinauer074d9132009-09-26 16:43:17 +0000486# parse parameters.. try to find out whether we're running GNU getopt
Stefan Reinauerd7649122015-06-09 11:44:25 -0700487getoptbrand="$(getopt -V | sed -e '1!d' -e 's,^\(......\).*,\1,')"
Peter Stuge09377b72011-08-21 06:24:55 +0200488if [ "${getoptbrand}" = "getopt" ]; then
Stefan Reinauer074d9132009-09-26 16:43:17 +0000489 # Detected GNU getopt that supports long options.
Stefan Reinauerd7649122015-06-09 11:44:25 -0700490 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 +0000491 eval set "$args"
492else
493 # Detected non-GNU getopt
Stefan Reinauerd7649122015-06-09 11:44:25 -0700494 args=$(getopt Vhcd:p:j:D:tGyC $*)
Stefan Reinauer074d9132009-09-26 16:43:17 +0000495 set -- $args
496fi
497
498if [ $? != 0 ]; then
499 myhelp
500 exit 1
501fi
502
503while true ; do
504 case "$1" in
505 -V|--version) shift; myversion; exit 0;;
506 -h|--help) shift; myhelp; exit 0;;
Patrick Georgi44af57a2013-12-19 22:06:22 +0100507 -c|--clean) shift; clean=1;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000508 -t|--savetemps) shift; SAVETEMPS=1;;
509 -d|--directory) shift; TARGETDIR="$1"; shift;;
510 -p|--platform) shift; TARGETARCH="$1"; shift;;
511 -D|--destdir) shift; DESTDIR="$1"; shift;;
Patrick Georgi73166c72010-04-14 20:42:42 +0000512 -j|--jobs) shift; JOBS="-j $1"; shift;;
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700513 -C|--clang) shift; BUILDCLANG=1;;
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100514 -G|--gdb) shift; SKIPGDB=0;;
515 -S|--scripting) shift; SKIPPYTHON=0;;
Patrick Georgic1a75b12011-11-04 21:37:14 +0100516 -y|--ccache) shift; USECCACHE=1;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200517 --) shift; break;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000518 -*) printf "Invalid option\n\n"; myhelp; exit 1;;
Patrick Georgid2e0dd52013-06-09 08:05:45 +0200519 *) break;;
Stefan Reinauer074d9132009-09-26 16:43:17 +0000520 esac
521done
522
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700523case "$TARGETARCH" in
Stefan Reinauer24f9cb92015-03-13 22:50:22 +0100524 x86_64-elf) ;;
525 x86_64*) TARGETARCH=x86_64-elf;;
David Hubbard5b0420a2013-05-28 16:33:15 -0600526 i386-elf) ;;
527 i386-mingw32) ;;
Patrick Georgi9b7efa52015-03-08 09:20:36 +0100528 mipsel-elf) ;;
Patrick Georgif0bbc952015-03-07 10:57:25 +0100529 riscv-elf) ;;
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700530 i386*) TARGETARCH=i386-elf;;
Patrick Georgic8883262013-09-19 10:57:58 +0200531 arm*) TARGETARCH=armv7-a-eabi;;
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700532 aarch64*) TARGETARCH=aarch64-elf;;
533 *) printf "${red}WARNING: Unsupported architecture $TARGETARCH.${NC}\n\n"; ;;
Ronald G. Minnichb460a662013-05-26 05:33:35 -0700534esac
535
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700536if [ "$BUILDCLANG" -eq 0 ]; then
537echo "Target architecture is now $TARGETARCH"
Stefan Reinauer699ac272015-06-05 12:43:28 -0700538NAME="${TARGETARCH} cross"
539PACKAGES="GMP MPFR MPC LIBELF BINUTILS GCC IASL"
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700540else
541NAME=clang
542PACKAGES="LLVM CFE CRT CTE"
543fi
Stefan Reinauer699ac272015-06-05 12:43:28 -0700544
Patrick Georgi3af0aa22013-09-17 20:59:52 +0200545BUILDDIRPREFIX=build-${TARGETARCH}
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700546BUILDDIR=$PWD/build-clang
Patrick Georgi3af0aa22013-09-17 20:59:52 +0200547
Patrick Georgi44af57a2013-12-19 22:06:22 +0100548if [ "$clean" = "1" ]; then
549 cleanup
550fi
551
Peter Stugeceacd772011-06-09 04:54:16 +0200552if [ $SKIPGDB -eq 1 ]; then
553 printf "Will skip GDB ... ${green}ok${NC}\n"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100554 if [ $SKIPPYTHON -eq 0 ]; then
555 printf "Python scripting needs GDB ... disabling ... ${green}ok${NC}\n"
556 SKIPPYTHON=1
557 fi
Stefan Reinauer699ac272015-06-05 12:43:28 -0700558else
559 PACKAGES="$PACKAGES GDB"
Stefan Reinauer2c3cd122011-11-01 21:43:50 +0100560fi
Stefan Reinauer699ac272015-06-05 12:43:28 -0700561if [ $SKIPPYTHON -eq 0 ]; then
562 PACKAGES="$PACKAGES EXPAT PYTHON"
Peter Stugeceacd772011-06-09 04:54:16 +0200563fi
564
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700565# coreboot does not like the GOLD linker
566# USE_GOLD="--enable-gold"
567USE_GOLD=""
568GCC_OPTIONS="--enable-lto"
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000569
Stefan Reinauer074d9132009-09-26 16:43:17 +0000570printf "Downloading tar balls ... \n"
571mkdir -p tarballs
Stefan Reinauer699ac272015-06-05 12:43:28 -0700572for P in $PACKAGES; do
573 download $P
Stefan Reinauer074d9132009-09-26 16:43:17 +0000574done
Stefan Reinauer699ac272015-06-05 12:43:28 -0700575printf "Downloaded tar balls ... ${green}ok${NC}\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000576
577printf "Unpacking and patching ... \n"
Stefan Reinauer699ac272015-06-05 12:43:28 -0700578for P in $PACKAGES; do
579 unpack_and_patch $P
Stefan Reinauer074d9132009-09-26 16:43:17 +0000580done
Stefan Reinauer699ac272015-06-05 12:43:28 -0700581printf "Unpacked and patched ... ${green}ok${NC}\n"
Stefan Reinauer074d9132009-09-26 16:43:17 +0000582
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700583if [[ "$PACKAGES" == *GCC* ]]; then
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700584CC=cc
Stefan Reinauer0d2119d2013-07-10 14:27:56 -0700585if [ $UNAME = "Darwin" ]; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000586 #GCC_OPTIONS="$GCC_OPTIONS --enable-threads=posix"
587
588 # generally the OS X compiler can create x64 binaries.
589 # Per default it generated i386 binaries in 10.5 and x64
590 # binaries in 10.6 (even if the kernel is 32bit)
591 # For some weird reason, 10.5 autodetects an ABI=64 though
592 # so we're setting the ABI explicitly here.
Stefan Reinauerd7649122015-06-09 11:44:25 -0700593 if [ $(sysctl -n hw.optional.x86_64 2>/dev/null) -eq 1 ] 2>/dev/null; then
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000594 OPTIONS="ABI=64"
595 else
596 OPTIONS="ABI=32"
597 fi
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700598
Stefan Reinauer4266b922012-12-05 16:18:32 -0800599 # In Xcode 4.5.2 the default compiler is clang.
600 # However, this compiler fails to compile gcc 4.7.x. As a
601 # workaround it's possible to compile gcc with llvm-gcc.
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700602 if $CC -v 2>&1 | grep -q LLVM; then
Stefan Reinauer4266b922012-12-05 16:18:32 -0800603 CC=llvm-gcc
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700604 fi
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000605fi
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700606fi # GCC
Stefan Reinauer5f1ad892010-05-17 11:02:25 +0000607
Patrick Georgic1a75b12011-11-04 21:37:14 +0100608if [ "$USECCACHE" = 1 ]; then
609 CC="ccache $CC"
610fi
611
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700612for package in $PACKAGES; do
613 mkdir -p ${BUILDDIRPREFIX}-$package
614done
Stefan Reinauer16bd7892012-12-07 23:57:01 +0100615
616mkdir -p $DESTDIR$TARGETDIR/bin
617export PATH=$DESTDIR$TARGETDIR/bin:$PATH
618
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700619build GMP
Stefan Reinauer074d9132009-09-26 16:43:17 +0000620
Stefan Reinauer9491b4d2011-10-11 22:37:59 -0700621# Now set CFLAGS to match GMP CFLAGS but strip out -pedantic
622# as GCC 4.6.x fails if it's there.
Stefan Reinauerd7649122015-06-09 11:44:25 -0700623HOSTCFLAGS=$(grep __GMP_CFLAGS $DESTDIR$TARGETDIR/include/gmp.h |cut -d\" -f2 |\
624 sed s,-pedantic,,)
Stefan Reinauer074d9132009-09-26 16:43:17 +0000625
Stefan Reinauere7757bd2015-06-05 13:59:44 -0700626build MPFR
627build MPC
628build LIBELF
629build BINUTILS
630build GCC
631build EXPAT
632build PYTHON
633build GDB
634build IASL
635build LLVM
Stefan Reinauer14ce2132015-06-05 13:01:13 -0700636
Stefan Reinauer699ac272015-06-05 12:43:28 -0700637# Adding git information of current tree to target directory
638# for reproducibility
Stefan Reinauerd7649122015-06-09 11:44:25 -0700639PROGNAME=$(basename "$0")
Patrick Georgi5602f2c2015-03-28 15:48:47 +0100640rm -f "$DESTDIR$TARGETDIR/$PROGNAME".commit.*
Stefan Reinauerd7649122015-06-09 11:44:25 -0700641cp "$PROGNAME" $DESTDIR$TARGETDIR/"$PROGNAME.commit.$(git describe)"
Zheng Bao30b895f2013-02-06 18:04:40 +0800642
Stefan Reinauer074d9132009-09-26 16:43:17 +0000643if [ $SAVETEMPS -eq 0 ]; then
Stefan Reinauer699ac272015-06-05 12:43:28 -0700644 cleanup
Stefan Reinauer074d9132009-09-26 16:43:17 +0000645else
646 printf "Leaving temporary files around... ${green}ok${NC}\n"
647fi
648
Stefan Reinauer699ac272015-06-05 12:43:28 -0700649printf "\n${green}You can now run your $NAME toolchain from $TARGETDIR.${NC}\n"