Windows,bootstrapping: fix build_windows_jni.sh

Also:
- check as the first thing in compile.sh that we
  can locate the GNU coreutils, and remove the
  duplicate check for the same thing on Windows
- check early in compile.sh that we can access
  python.exe

Fixes https://github.com/bazelbuild/bazel/issues/3863

Change-Id: Ib48b405cf93eafd48e21b280bcbab4d45117c1d9
PiperOrigin-RevId: 171291435
diff --git a/compile.sh b/compile.sh
index c72a802..076dd24 100755
--- a/compile.sh
+++ b/compile.sh
@@ -22,26 +22,28 @@
 
 set -o errexit
 
-# Correct PATH on Windows, to avoid using "FIND.EXE" instead of "/usr/bin/find"
-# etc, leading to confusing errors.
-export BAZEL_OLD_PATH=$PATH
-case "$(uname -s | tr [:upper:] [:lower:])" in
-msys*|mingw*|cygwin*)
-  # Check that the PATH is set up correctly by attempting to locate `[`.
-  # This ensures that `which` is installed correctly and can succeed, while
-  # also avoids accidentally locating a tool that exists in plain Windows too
-  # (like "find" for "FIND.EXE").
-  which [ >&/dev/null || export PATH="/bin:/usr/bin:$PATH"
-esac
-
 # Check that the bintools can be found, otherwise we would see very confusing
 # error messages.
+# For example on Windows we would find "FIND.EXE" instead of "/usr/bin/find"
+# when running "find".
 hash tr >&/dev/null || {
   echo >&2 "ERROR: cannot locate GNU coreutils; check your PATH."
   echo >&2 "       (You may need to run 'export PATH=/bin:/usr/bin:\$PATH)'"
   exit 1
 }
 
+# Ensure Python is on the PATH on Windows,otherwise we would see
+# "LAUNCHER ERROR" messages from py_binary exe launchers.
+case "$(uname -s | tr [:upper:] [:lower:])" in
+msys*|mingw*|cygwin*)
+  which python.exe >&/dev/null || {
+    echo >&2 "ERROR: cannot locate python.exe; check your PATH."
+    echo >&2 "       (You may need to run 'export PATH=/c/Python27:\$PATH)' or similar,"
+    echo >&2 "       depending on where you installed Python)."
+    exit 1
+  }
+esac
+
 cd "$(dirname "$0")"
 
 # Set the default verbose mode in buildenv.sh so that we do not display command
diff --git a/scripts/bootstrap/buildenv.sh b/scripts/bootstrap/buildenv.sh
index 5c36821..cbb1365 100755
--- a/scripts/bootstrap/buildenv.sh
+++ b/scripts/bootstrap/buildenv.sh
@@ -129,11 +129,6 @@
   ATEXIT_HANDLERS="${ATEXIT_HANDLERS} ${handler}"
 }
 
-function restore_saved_path() {
-  export PATH=$BAZEL_OLD_PATH
-  export BAZEL_OLD_PATH=
-}
-
 # Exit routine to run all registered atexit handlers.
 #
 # If the program exited with an error, this exit routine will also exit with the
@@ -183,7 +178,6 @@
 }
 
 atexit cleanup_phasefile
-atexit restore_saved_path
 
 # Excutes a command respecting the current verbosity settings.
 #
diff --git a/src/main/native/windows/build_windows_jni.sh b/src/main/native/windows/build_windows_jni.sh
index 5d39b8f..ad52518 100644
--- a/src/main/native/windows/build_windows_jni.sh
+++ b/src/main/native/windows/build_windows_jni.sh
@@ -99,7 +99,7 @@
 @call "${VSVARS}" amd64
 @cd $(cygpath -a -w "${PWD}")
 @set TMP=$(cygpath -a -w "${VSTEMP}")
-@CL /O2 /EHsc /LD /Fe:"$(cygpath -a -w ${DLL})" /I "${VSTEMP}" /I . ${WINDOWS_SOURCES[*]}
+@CL /O2 /EHsc /LD /Fe:"$(cygpath -a -w ${DLL})" /I "%TMP%" /I . ${WINDOWS_SOURCES[*]}
 EOF
 
 # Invoke the file and hopefully generate the .DLL .