Make gcc build script gn-compatible.

Make the build script position-independent (so it can be invoked from
a different directory, and with full support for specifying the output
directory location). Also, add an option to add a link to the
installation dir, to support easily locating output files.

Change-Id: I5abbd0cf9bc3c1b64a65fa5f31bbfb1bbfee4a0f
diff --git a/do-build b/do-build
index e26ae1a..34d7abf 100755
--- a/do-build
+++ b/do-build
@@ -78,6 +78,9 @@
   echo "                      (default is to use the current build machine)"
   echo "  -j <#>              Specify number of parallel build instances"
   echo "  --list-supported    List all supported configurations and exit"
+  echo "  --make-link <LINK>  Create a link in the current directory to the"
+  echo "                      installation directory"
+  echo "  --outdir <DIR>      Generate output in specified directory"
   echo "  --strip             Strip all binaries"
   echo "  --sysroot <DIR>     Specify the location of the sysroot to be"
   echo "                      used for non-native builds" 
@@ -223,6 +226,7 @@
   # Set defaults
   OUT_DIR="$INVOCATION_DIR"
   HOST="$BUILD"
+  MAKE_LINK=""
   STRIP=""
 
   while [ ! -z "$1" ]
@@ -250,6 +254,10 @@
         list_all_supported
         exit 0
         ;;
+      --make-link | --make-link=*)
+        get_argument $*
+        MAKE_LINK="$RESULT"
+        ;;
       --outdir | --outdir=*)
         get_argument $*
         OUT_DIR="$RESULT"
@@ -260,6 +268,8 @@
       --sysroot | --sysroot=*)
         get_argument $*
         SYSROOT="$RESULT"
+        [ -d "$SYSROOT" ] || \
+          die "Sysroot directory $SYSROOT does not appear to exist"
         ;;
       --target | --target=*)
         get_argument $*
@@ -293,12 +303,15 @@
     die "Sysroot expected when host differs from build system. Use --force to try anyway."
   fi
 
+  mkdir -p "$OUT_DIR" || die "Unable to create directory $OUT_DIR"
+  OUT_DIR="$(realpath $OUT_DIR)"
   ARCHIVE_DIR="$OUT_DIR/archives"
+  mkdir -p "$ARCHIVE_DIR" || die "Unable to create directory $ARCHIVE_DIR"
 }
 
 get_tool_verinfo()
 {
-  . toolvers
+  . ${SCRIPT_DIR}/toolvers
   GNU_MIRROR="https://mirrors.kernel.org/gnu"
   BINUTILS_REPO="$GNU_MIRROR/binutils/binutils-$BINUTILS_VER.tar.bz2"
   GCC_REPO="$GNU_MIRROR/gcc/gcc-$GCC_VER/gcc-$GCC_VER.tar.bz2"
@@ -372,7 +385,6 @@
         die "$component_tarfile failed integrity check"
 
       echo "Extracting $component_tarfile"
-      mkdir -p "$OUT_DIR" || die "Unable to create directory $OUT_DIR"
       pushd "$OUT_DIR" > /dev/null || die "Unable to change to $OUT_DIR"
       rm -rf "$component_source_dir" \
         || die "Failed removing $component_source_dir"
@@ -534,6 +546,7 @@
 {
   local component="$1"
   local is_host_lib="$2"
+  local install_dir="$3"
 
   local uppercase_component="$(echo "$component" \
                                | tr '[:lower:]' '[:upper:]')"
@@ -585,7 +598,9 @@
 
 do_configure()
 {
-  build_config_options "$component" "$is_host_lib"
+  local install_dir="$1"
+
+  build_config_options "$component" "$is_host_lib" "$install_dir"
   (IFS=':' && "$OUT_DIR/${component}-${component_ver}/configure" \
               $CONFIG_OPTIONS)
   if [ "$?" != '0' ]
@@ -615,7 +630,7 @@
     mkdir -p "$build_dir" || die "Unable to create directory $build_dir"
     pushd "$build_dir" > /dev/null || die "Unable to pushd $build_dir"
     echo "Configuring $component"
-    do_configure
+    do_configure "$install_dir"
     echo "Building $component"
     make -j ${NUM_PARALLEL_BUILDS} || die "Failed to build $component"
     echo "Installing $component"
@@ -680,5 +695,13 @@
   TARGET="$ORIG_TARGET"
 fi
 build_components
+# In certain unnamed environments, we may need an architecture-
+# independent way to find the results of a build. To help, we
+# optionally create a link to the installation in the cwd.
+if [ -n "$MAKE_LINK" ]
+then
+  get_install_dir
+  ln -fs "$RESULT" "$MAKE_LINK" || die "Failed to make '$MAKE_LINK' link"
+fi
 exit 0