cros_workon_make: support passing through env vars to the ebuild

When doing iterative work, you often want to build just one or two
things first before falling back to a full build.  Support passing
through env vars so we can do that.

BUG=chromium:311351
TEST=`cros_workon_make --board x86-generic crash-reporter` still works

Change-Id: I0ec5110fc2f432b8d83e26d7265edbc20c5a104a
Reviewed-on: https://chromium-review.googlesource.com/174677
Reviewed-by: Chris Sosa <sosa@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: Mike Frysinger <vapier@chromium.org>
diff --git a/host/cros_workon_make b/host/cros_workon_make
index 26a859e..905fd1f 100755
--- a/host/cros_workon_make
+++ b/host/cros_workon_make
@@ -26,7 +26,7 @@
 eval set -- "${FLAGS_ARGV}"
 
 if [ $# -lt 1 ]; then
-  echo "Usage: ${0} [OPTIONS] <package (read: ebuild) basename>"
+  echo "Usage: ${0} [OPTIONS] <package (read: ebuild) basename> [target args]"
   exit 1
 fi
 
@@ -39,6 +39,7 @@
 fi
 
 pkg="${1}"
+shift
 if [ "${pkg}" = "." ]; then
   if ! pkg=$(git config workon.pkg); then
     die "workon.pkg not set in git config for this project"
@@ -86,6 +87,15 @@
 export SANDBOX_WRITE=~/trunk
 export CROS_WORKON_INPLACE=1
 export FEATURES
+export CROS_WORKON_MAKE_COMPILE_ARGS="$*"
+
+# Vars that we want to pass through for the user.
+PASS_THROUGH_VARS=(
+  # cros-workon.eclass vars.
+  CROS_WORKON_MAKE_COMPILE_ARGS
+  # Common test vars.
+  GTEST_ARGS
+)
 
 # Determine if we're going to do tests, set up commands appropriately.
 to_do="compile"
@@ -110,5 +120,11 @@
   clean="clean"
 else
   rm -f "${workpath}/.compiled"
+  envf="${workpath}/temp/environment"
+  for v in ${PASS_THROUGH_VARS[@]}; do
+    # We delete it independently in case the var wasn't set initially.
+    sed -i -e "/^declare .. ${v}=/d" "${envf}"
+    printf 'declare -x %s="%s"\n' "${v}" "${!v}" >> "${envf}"
+  done
 fi
 exec "${EBUILDCMD}" "${pkgfile}" ${clean} "${to_do}"