Merge changes from github.
Change: 152200430
diff --git a/configure b/configure
index e59ee2a..6360641 100755
--- a/configure
+++ b/configure
@@ -8,9 +8,6 @@
 SOURCE_BASE_DIR=`pwd -P`
 popd > /dev/null
 
-# This file contains customized config settings.
-touch .bazelrc
-
 PLATFORM="$(uname -s | tr 'A-Z' 'a-z')"
 
 function is_linux() {
@@ -38,14 +35,6 @@
   fi
 }
 
-function bazel_fetch() {
-  if [ -z "$TF_BAZEL_TARGETS" ]; then
-    bazel fetch "//tensorflow/... -//tensorflow/contrib/nccl/... -//tensorflow/examples/android/..."
-  else
-    bazel fetch $TF_BAZEL_TARGETS
-  fi
-}
-
 function sed_hyphen_i() {
   if is_macos; then
     sed -i '' "$@"
@@ -54,6 +43,21 @@
   fi
 }
 
+function write_to_bazelrc() {
+  echo "$1" >> .tf_configure.bazelrc
+}
+
+function write_action_env_to_bazelrc() {
+  write_to_bazelrc "build --action_env $1=\"$2\""
+}
+
+# This file contains customized config settings.
+rm -f .tf_configure.bazelrc
+touch .tf_configure.bazelrc
+touch .bazelrc
+sed_hyphen_i "/tf_configure/d" .bazelrc
+echo "import .tf_configure.bazelrc" >> .bazelrc
+
 # Delete any leftover BUILD files from the Makefile build, which would interfere
 # with Bazel parsing.
 MAKEFILE_DOWNLOAD_DIR=tensorflow/contrib/makefile/downloads
@@ -164,6 +168,7 @@
   TF_NEED_HDFS=0
   TF_NEED_JEMALLOC=0
   TF_NEED_OPENCL=0
+  TF_CUDA_CLANG=0
 fi
 
 if is_linux; then
@@ -181,9 +186,8 @@
   TF_NEED_JEMALLOC=0
 fi
 
-sed_hyphen_i -e "/with_jemalloc/d" .bazelrc
 if [[ "$TF_NEED_JEMALLOC" == "1" ]]; then
-  echo 'build --define with_jemalloc=true' >>.bazelrc
+  write_to_bazelrc 'build --define with_jemalloc=true'
 fi
 
 while [[ "$TF_NEED_GCP" == "" ]]; do
@@ -200,9 +204,8 @@
   esac
 done
 
-sed_hyphen_i -e "/with_gcp_support/d" .bazelrc
 if [[ "$TF_NEED_GCP" == "1" ]]; then
-  echo 'build --define with_gcp_support=true' >>.bazelrc
+  write_to_bazelrc 'build --define with_gcp_support=true'
 fi
 
 while [[ "$TF_NEED_HDFS" == "" ]]; do
@@ -219,9 +222,8 @@
   esac
 done
 
-sed_hyphen_i -e "/with_hdfs_support/d" .bazelrc
 if [[ "$TF_NEED_HDFS" == "1" ]]; then
-  echo 'build --define with_hdfs_support=true' >>.bazelrc
+  write_to_bazelrc 'build --define with_hdfs_support=true'
 fi
 
 ## Enable XLA.
@@ -235,9 +237,8 @@
   esac
 done
 
-sed_hyphen_i -e "/with_xla_support/d" .bazelrc
 if [[ "$TF_ENABLE_XLA" == "1" ]]; then
-  echo 'build --define with_xla_support=true' >>.bazelrc
+  write_to_bazelrc 'build --define with_xla_support=true'
 fi
 
 
@@ -279,23 +280,11 @@
   esac
 done
 
-sed_hyphen_i -e "/--action_env TF_NEED_CUDA/d" .bazelrc
-sed_hyphen_i -e "/--action_env CUD/d" .bazelrc
-sed_hyphen_i -e "/--action_env GCC_HOST/d" .bazelrc
-sed_hyphen_i -e "/--action_env TF_CUD/d" .bazelrc
-sed_hyphen_i -e "/--action_env CLANG_CUDA/d" .bazelrc
-
 export TF_NEED_CUDA
-echo "build --action_env TF_NEED_CUDA=$TF_NEED_CUDA" >>.bazelrc
+write_action_env_to_bazelrc "TF_NEED_CUDA" "$TF_NEED_CUDA"
 
 export TF_NEED_OPENCL
 
-if [[ "$TF_NEED_CUDA" == "0" ]] && [[ "$TF_NEED_OPENCL" == "0" ]]; then
-  echo "Configuration finished"
-  bazel_fetch
-  exit
-fi
-
 if [ "$TF_NEED_CUDA" == "1" ]; then
 while [[ "$TF_CUDA_CLANG" == "" ]]; do
   read -p "Do you want to use clang as CUDA compiler? [y/N] " INPUT
@@ -308,7 +297,7 @@
 done
 
 export TF_CUDA_CLANG
-echo "build --action_env TF_CUDA_CLANG=$TF_CUDA_CLANG" >>.bazelrc
+write_action_env_to_bazelrc "TF_CUDA_CLANG" "$TF_CUDA_CLANG"
 
 # Set up which gcc nvcc should use as the host compiler
 # No need to set this on Windows
@@ -324,7 +313,7 @@
   fi
   if [ -e "$GCC_HOST_COMPILER_PATH" ]; then
     export GCC_HOST_COMPILER_PATH
-    echo "build --action_env GCC_HOST_COMPILER_PATH=\"$GCC_HOST_COMPILER_PATH\"" >>.bazelrc
+    write_action_env_to_bazelrc "GCC_HOST_COMPILER_PATH" "$GCC_HOST_COMPILER_PATH"
     break
   fi
   echo "Invalid gcc path. ${GCC_HOST_COMPILER_PATH} cannot be found" 1>&2
@@ -348,7 +337,7 @@
   fi
   if [ -e "$CLANG_CUDA_COMPILER_PATH" ]; then
     export CLANG_CUDA_COMPILER_PATH
-    echo "build --action_env CLANG_CUDA_COMPILER_PATH=\"$CLANG_CUDA_COMPILER_PATH\"" >>.bazelrc
+    write_action_env_to_bazelrc "CLANG_CUDA_COMPILER_PATH" "$CLANG_CUDA_COMPILER_PATH"
     break
   fi
   echo "Invalid clang path. ${CLANG_CUDA_COMPILER_PATH} cannot be found" 1>&2
@@ -399,10 +388,9 @@
 
   if [ -e "${CUDA_TOOLKIT_PATH}/${CUDA_RT_LIB_PATH}" ]; then
     export CUDA_TOOLKIT_PATH
-    echo "build --action_env CUDA_TOOLKIT_PATH=\"$CUDA_TOOLKIT_PATH\"" >>.bazelrc
-
+    write_action_env_to_bazelrc "CUDA_TOOLKIT_PATH" "$CUDA_TOOLKIT_PATH"
     export TF_CUDA_VERSION
-    echo "build --action_env TF_CUDA_VERSION=$TF_CUDA_VERSION" >>.bazelrc
+    write_action_env_to_bazelrc "TF_CUDA_VERSION" "$TF_CUDA_VERSION"
     break
   fi
   echo "Invalid path to CUDA $TF_CUDA_VERSION toolkit. ${CUDA_TOOLKIT_PATH}/${CUDA_RT_LIB_PATH} cannot be found"
@@ -417,9 +405,9 @@
 
 # Find out where the cuDNN library is installed
 while true; do
-  # Configure the Cudnn version to use.
+  # Configure the cuDNN version to use.
   if [ -z "$TF_CUDNN_VERSION" ]; then
-    read -p "Please specify the Cudnn version you want to use. [Leave empty to use system default]: " TF_CUDNN_VERSION
+    read -p "Please specify the cuDNN version you want to use. [Leave empty to use system default]: " TF_CUDNN_VERSION
   fi
 
   fromuser=""
@@ -454,10 +442,9 @@
 
   if [ -e "$CUDNN_INSTALL_PATH/${CUDA_DNN_LIB_ALT_PATH}" -o -e "$CUDNN_INSTALL_PATH/${CUDA_DNN_LIB_PATH}" ]; then
     export TF_CUDNN_VERSION
-    echo "build --action_env TF_CUDNN_VERSION=$TF_CUDNN_VERSION" >>.bazelrc
-
+    write_action_env_to_bazelrc "TF_CUDNN_VERSION" "$TF_CUDNN_VERSION"
     export CUDNN_INSTALL_PATH
-    echo "build --action_env CUDNN_INSTALL_PATH=\"$CUDNN_INSTALL_PATH\"" >>.bazelrc
+    write_action_env_to_bazelrc "CUDNN_INSTALL_PATH" "$CUDNN_INSTALL_PATH"
     break
   fi
 
@@ -470,10 +457,9 @@
     CUDNN_PATH_FROM_LDCONFIG="$($LDCONFIG_BIN -p | sed -n 's/.*libcudnn.so .* => \(.*\)/\1/p')"
     if [ -e "${CUDNN_PATH_FROM_LDCONFIG}${TF_CUDNN_EXT}" ]; then
       export TF_CUDNN_VERSION
-      echo "build --action_env TF_CUDNN_VERSION=$TF_CUDNN_VERSION" >>.bazelrc
-
+      write_action_env_to_bazelrc "TF_CUDNN_VERSION" "$TF_CUDNN_VERSION"
       export CUDNN_INSTALL_PATH="$(dirname ${CUDNN_PATH_FROM_LDCONFIG})"
-      echo "build --action_env CUDNN_INSTALL_PATH=\"$CUDNN_INSTALL_PATH\"" >>.bazelrc
+      write_action_env_to_bazelrc "CUDNN_INSTALL_PATH" "$CUDNN_INSTALL_PATH"
       break
     fi
   fi
@@ -525,7 +511,7 @@
     fi
   else
     export TF_CUDA_COMPUTE_CAPABILITIES
-    echo "build --action_env TF_CUDA_COMPUTE_CAPABILITIES=$TF_CUDA_COMPUTE_CAPABILITIES" >>.bazelrc
+    write_action_env_to_bazelrc "TF_CUDA_COMPUTE_CAPABILITIES" "$TF_CUDA_COMPUTE_CAPABILITIES"
     break
   fi
   TF_CUDA_COMPUTE_CAPABILITIES=""
@@ -536,9 +522,9 @@
   export CUDA_PATH="$CUDA_TOOLKIT_PATH"
   export CUDA_COMPUTE_CAPABILITIES="$TF_CUDA_COMPUTE_CAPABILITIES"
   export NO_WHOLE_ARCHIVE_OPTION=1
-
-  # Set GCC_HOST_COMPILER_PATH to keep cuda_configure.bzl happy
-  export GCC_HOST_COMPILER_PATH="/usr/bin/dummy_compiler"
+  write_action_env_to_bazelrc "CUDA_PATH" "$CUDA_PATH"
+  write_action_env_to_bazelrc "CUDA_COMPUTE_CAPABILITIES" "$CUDA_COMPUTE_CAPABILITIES"
+  write_action_env_to_bazelrc "NO_WHOLE_ARCHIVE_OPTION" "1"
 fi
 
 # end of if "$TF_NEED_CUDA" == "1"
@@ -629,6 +615,6 @@
 # end of if "$TF_NEED_OPENCL" == "1"
 fi
 
-bazel_fetch
-
+# TODO(gunan): Remove once bazel correctly handles changes in remote repositories.
+bazel clean
 echo "Configuration finished"