Windows, bootstrapping: fix bugs to support Cygwin

We can't yet fully bootstrap Bazel on Cygwin, but
can build Bazel from scratch. Building Bazel with
Bazel fails because gcc isn't found where it's
believed to be -- /usr/bin is a mount in Cygwin
(to /bin), not a symlink or directory.

In this change I:
- added support for the Cygwin shell as a
  bootstrap platform (recognize `uname`)
- updateed the bootstrap scripts to use "windows"
  as the PLATFORM string, not "mingw"
- fixed the git lookup code
- removed some hardwired msys-style path
- added a cygpath call to convert $PWD to a
  mixed-style (otherwise the bootstrap script
  passes --client_cwd=/cygdrive/c/... to the
  server and WindowsFileSystem.java wants to make
  that relative to c:/cygwin64)

See https://github.com/bazelbuild/bazel/issues/2885

Change-Id: Icc71261ea4f0c6d4a9c0846551a7977ca6020331
PiperOrigin-RevId: 154273014
diff --git a/scripts/bootstrap/bootstrap.sh b/scripts/bootstrap/bootstrap.sh
index 3832692..c50afc2 100755
--- a/scripts/bootstrap/bootstrap.sh
+++ b/scripts/bootstrap/bootstrap.sh
@@ -62,7 +62,8 @@
         --javacopt="-g -source ${JAVA_VERSION} -target ${JAVA_VERSION}" "${@}"
   }
 else
-  function _run_bootstrapping_bazel() { local command=$1
+  function _run_bootstrapping_bazel() {
+    local command=$1
     shift
     ${BAZEL} --bazelrc=${BAZELRC} ${BAZEL_DIR_STARTUP_OPTIONS} $command \
         ${_BAZEL_ARGS} --verbose_failures \
diff --git a/scripts/bootstrap/buildenv.sh b/scripts/bootstrap/buildenv.sh
index 39a15c4..197eef9 100755
--- a/scripts/bootstrap/buildenv.sh
+++ b/scripts/bootstrap/buildenv.sh
@@ -77,22 +77,22 @@
   fi
   ;;
 
-msys*|mingw*)
+msys*|mingw*|cygwin*)
   # Use a simplified platform string.
-  PLATFORM="mingw"
+  PLATFORM="windows"
   PATHSEP=";"
   # Find the latest available version of the SDK.
-  JAVA_HOME="${JAVA_HOME:-$(ls -d /c/Program\ Files/Java/jdk* | sort | tail -n 1)}"
+  JAVA_HOME="${JAVA_HOME:-$(ls -d C:/Program\ Files/Java/jdk* | sort | tail -n 1)}"
   # Replace backslashes with forward slashes.
   JAVA_HOME="${JAVA_HOME//\\//}"
 esac
 
 EXE_EXT=""
-if [ "${PLATFORM}" == "mingw" ]; then
+if [ "${PLATFORM}" == "windows" ]; then
   # Extension for executables.
   EXE_EXT=".exe"
 
-  # Fix TMPDIR on msys
+  # Fix TMPDIR on windows
   default_tmp=${TMP:-$(cygpath -mO)/Temp}
   TMPDIR=$(cygpath -ml "${TMPDIR:-$default_tmp}")
 fi
@@ -235,13 +235,13 @@
 }
 
 function git_sha1() {
-  if [ -x "$(which git || true)" ] && [ -d .git ]; then
+  if [ -x "$(which git 2>/dev/null)" ] && [ -d .git ]; then
     git rev-parse --short HEAD 2>/dev/null || true
   fi
 }
 
 function git_date() {
-  if [ -x "$(which git || true)" ] && [ -d .git ]; then
+  if [ -x "$(which git 2>/dev/null)" ] && [ -d .git ]; then
     git log -1 --pretty=%ai | cut -d " " -f 1 || true
   fi
 }
diff --git a/scripts/bootstrap/compile.sh b/scripts/bootstrap/compile.sh
index 08687ac..eec6123 100755
--- a/scripts/bootstrap/compile.sh
+++ b/scripts/bootstrap/compile.sh
@@ -272,8 +272,7 @@
 function build_jni() {
   local -r output_dir=$1
 
-  case "${PLATFORM}" in
-  msys*|mingw*)
+  if [ "${PLATFORM}" = "windows" ]; then
     # We need JNI on Windows because some filesystem operations are not (and
     # cannot be) implemented in native Java.
     log "Building Windows JNI library..."
@@ -297,13 +296,10 @@
     chmod 0555 "$output"
 
     JNI_FLAGS="-Dio.bazel.EnableJni=1 -Djava.library.path=${output_dir}"
-    ;;
-
-  *)
+  else
     # We don't need JNI on other platforms.
     JNI_FLAGS="-Dio.bazel.EnableJni=0"
-    ;;
-  esac
+  fi
 }
 
 build_jni "${ARCHIVE_DIR}/_embedded_binaries"
@@ -319,6 +315,12 @@
   cp tools/osx/xcode_locator_stub.sh ${ARCHIVE_DIR}/_embedded_binaries/xcode-locator
 fi
 
+function get_cwd() {
+  local result=${PWD}
+  [ "$PLATFORM" = "windows" ] && result="$(cygpath -m "$result")"
+  echo "$result"
+}
+
 function run_bazel_jar() {
   local command=$1
   shift
@@ -369,7 +371,7 @@
       --install_base=${ARCHIVE_DIR} \
       --output_base=${OUTPUT_DIR}/out \
       --install_md5= \
-      --workspace_directory=${PWD} \
+      --workspace_directory="$(get_cwd)" \
       --nofatal_event_bus_exceptions \
       ${BAZEL_DIR_STARTUP_OPTIONS} \
       ${BAZEL_BOOTSTRAP_STARTUP_OPTIONS:-} \
@@ -378,6 +380,6 @@
       --startup_time=329 --extract_data_time=523 \
       --rc_source=/dev/null --isatty=1 \
       "${client_env[@]}" \
-      --client_cwd=${PWD} \
+      --client_cwd="$(get_cwd)" \
       "${@}"
 }