buildgcc: Add option to bootstrap a host gcc

Bootstrapping gcc is the recommended way if your host gcc's version
doesn't match the gcc version you're going to build. While a build
with an outdated host gcc usually succeeds, an outdated gnat seems
to be a bigger issue.

v3: Some library controversy: gcc likes the libraries it ships with
    most but we don't want to install shared libraries. So we build
    them static --disable-shared) and install only the minimum
    (libgcc, libada, libstdc++). However, as the code of these
    libraries might be used to build a shared library we have to
    compile them with `-fPIC`.

v4: o Updated getopt strings.
    o The workaround for clang (-fbracket-depth=1024) isn't needed
      for bootstrapping and also breaks the build, as clang is only
      used for the first stage in that case and gcc doesn't know
      that option.

So far build tested with `make BUILDGCC_OPTIONS="-b -l c,ada"` on
  o Ubuntu 14.04 "Trusty Tahr" (i386)
  o Debian 8 "Jessie" (x86_64) (building python (-S) works too)
  o current Arch Linux (x86_64)
  o FreeBSD 10.3 (x86_64) (with gcc-aux package)

and with clang host compiler, thus C only: `make BUILDGCC_OPTIONS="-b"`
on
  o Debian 8 "Jessie" (x86_64)
  o FreeBSD 10.3 (x86_64)

v5: Rebased after toolchain updates to GCC 5.3.0 etc.

Build tested with `make BUILDGCC_OPTIONS="-b -l c,ada"` on
  o Debian 8 "Jessie" (x86_64)

Change-Id: Icb47d3e9dbafc55737fbc3ce62a084fb9d5f359a
Signed-off-by: Nico Huber <nico.huber@secunet.com>
Reviewed-on: https://review.coreboot.org/13473
Tested-by: build bot (Jenkins)
Reviewed-by: Patrick Georgi <pgeorgi@google.com>
diff --git a/util/crossgcc/buildgcc b/util/crossgcc/buildgcc
index b650c62..3791cc9 100755
--- a/util/crossgcc/buildgcc
+++ b/util/crossgcc/buildgcc
@@ -18,8 +18,8 @@
 
 cd $(dirname $0)
 
-CROSSGCC_DATE="April 16th, 2016"
-CROSSGCC_VERSION="1.39"
+CROSSGCC_DATE="June 7th, 2016"
+CROSSGCC_VERSION="1.40"
 CROSSGCC_COMMIT=$( git describe )
 
 # default settings
@@ -30,6 +30,7 @@
 DESTDIR=
 SAVETEMPS=0
 SKIPPYTHON=1
+BOOTSTRAP=0
 
 # GCC toolchain version numbers
 GMP_VERSION=6.1.0
@@ -341,6 +342,9 @@
 build()
 {
 	if package_uses_targetarch $1; then
+		if [ $BOOTSTRAP -eq 1 -a ! -f "${TARGETDIR}/.GCC.success" ]; then
+			build_for_host GCC
+		fi
 		build_for_target $1
 	else
 		build_for_host $1
@@ -385,6 +389,8 @@
 	printf "    [-P|--package <package>]      Build a specific package: GCC, CLANG, IASL, GDB\n"
 	printf "                                  (defaults to $PACKAGE)\n"
 	printf "GCC specific options:\n"
+	printf "    [-b|--bootstrap]              bootstrap the host compiler before building\n"
+	printf "                                  the cross compiler\n"
 	printf "    [-p|--platform <platform>]    target platform to build cross compiler for\n"
 	printf "                                  (defaults to $TARGETARCH)\n"
 	printf "    [-l|--languages <languages>]  comma separated list of target languages\n"
@@ -497,8 +503,35 @@
 	$MAKE install DESTDIR=$DESTDIR || touch .failed
 }
 
+bootstrap_GCC() {
+	CC="$CC" \
+		CFLAGS="$HOSTCFLAGS" \
+		CFLAGS_FOR_BUILD="$HOSTCFLAGS" \
+		CFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
+		CXXFLAGS="$HOSTCFLAGS" \
+		CXXFLAGS_FOR_BUILD="$HOSTCFLAGS" \
+		CXXFLAGS_FOR_TARGET="$HOSTCFLAGS -fPIC" \
+		../gcc-${GCC_VERSION}/configure \
+		--prefix=$TARGETDIR --libexecdir=$TARGETDIR/lib \
+		--enable-bootstrap \
+		--disable-werror --disable-nls \
+		--disable-shared --disable-multilib \
+		--disable-libssp --disable-libquadmath --disable-libcc1 \
+		${GCC_OPTIONS} --enable-languages="${LANGUAGES}" \
+		--with-gmp=$DESTDIR$TARGETDIR --with-mpfr=$DESTDIR$TARGETDIR \
+		--with-mpc=$DESTDIR$TARGETDIR --with-libelf=$DESTDIR$TARGETDIR \
+		--with-pkgversion="coreboot bootstrap v$CROSSGCC_VERSION $CROSSGCC_DATE" \
+		&& \
+	$MAKE $JOBS BOOT_CFLAGS="$HOSTCFLAGS" BUILD_CONFIG="" bootstrap && \
+	$MAKE	install-gcc \
+		install-target-libgcc \
+		maybe-install-target-libada \
+		maybe-install-target-libstdc++-v3 \
+		DESTDIR=$DESTDIR && \
+	ln -s gcc $DESTDIR$TARGETDIR/bin/cc || touch .failed
+}
 
-build_GCC() {
+build_cross_GCC() {
 	# Work around crazy code generator in GCC that confuses CLANG.
 	$CC --version | grep clang >/dev/null 2>&1 && \
 		HOSTCFLAGS="$HOSTCFLAGS -fbracket-depth=1024"
@@ -536,6 +569,14 @@
 	fi
 }
 
+build_GCC() {
+	if [ "$1" = host ]; then
+		bootstrap_GCC
+	else
+		build_cross_GCC
+	fi
+}
+
 build_EXPAT() {
 	CC="$CC" CFLAGS="$HOSTCFLAGS" ../${EXPAT_DIR}/configure --disable-shared \
 		--prefix=$TARGETDIR || touch .failed
@@ -641,11 +682,11 @@
 getoptbrand="$(getopt -V | sed -e '1!d' -e 's,^\(......\).*,\1,')"
 if [ "${getoptbrand}" = "getopt" ]; then
 	# Detected GNU getopt that supports long options.
-	args=$(getopt -l version,help,clean,directory:,platform:,languages:,package:,jobs:,destdir:,savetemps,scripting,ccache,supported: Vhcd:p:l:P:j:D:tSys: -- "$@")
+	args=$(getopt -l version,help,clean,directory:,bootstrap,platform:,languages:,package:,jobs:,destdir:,savetemps,scripting,ccache,supported: Vhcd:bp:l:P:j:D:tSys: -- "$@")
 	eval set "$args"
 else
 	# Detected non-GNU getopt
-	args=$(getopt Vhcd:p:l:P:j:D:tSys: $*)
+	args=$(getopt Vhcd:bp:l:P:j:D:tSys: $*)
 	set -- $args
 fi
 
@@ -661,6 +702,7 @@
 		-c|--clean)	shift; clean=1;;
 		-t|--savetemps)	shift; SAVETEMPS=1;;
 		-d|--directory)	shift; TARGETDIR="$1"; shift;;
+		-b|--bootstrap) shift; BOOTSTRAP=1;;
 		-p|--platform)	shift; TARGETARCH="$1"; shift;;
 		-l|--languages)	shift; LANGUAGES="$1"; shift;;
 		-D|--destdir)	shift; DESTDIR="$1"; shift;;