WindowsFileSystem: use JNI impl. of isJunction

Also build the JNI library while bootstraping.
This was once submitted in commit 4a249b6962d32ed4cfd4165dfdae4a555b00bb69 but got
rolled back due to some test breakage that's long
since fixed. In this change I'm slightly modifying
the original code in compile.sh.

Using JNI methods however is necessary because we
can't implement WindowsFileOperations.GetLongPath
in native Java, and having that code is a
prerequisite for the fix of https://github.com/bazelbuild/bazel/issues/2145

See also https://github.com/bazelbuild/bazel/issues/2238

--
PiperOrigin-RevId: 142535019
MOS_MIGRATED_REVID=142535019
diff --git a/scripts/bootstrap/compile.sh b/scripts/bootstrap/compile.sh
index 2d4026c..8508682 100755
--- a/scripts/bootstrap/compile.sh
+++ b/scripts/bootstrap/compile.sh
@@ -284,6 +284,45 @@
 EOF
 chmod 0755 ${ARCHIVE_DIR}/_embedded_binaries/process-wrapper${EXE_EXT}
 
+function build_jni() {
+  local -r output_dir=$1
+
+  case "${PLATFORM}" in
+  msys*|mingw*)
+    # We need JNI on Windows because some filesystem operations are not (and
+    # cannot be) implemented in native Java.
+    log "Building Windows JNI library..."
+
+    local -r jni_lib_name="windows_jni.dll"
+    local -r output="${output_dir}/${jni_lib_name}"
+    local -r tmp_output="${NEW_TMPDIR}/jni/${jni_lib_name}"
+    mkdir -p "$(dirname "$tmp_output")"
+    mkdir -p "$(dirname "$output")"
+
+    # Keep this `find` command in sync with the `srcs` of
+    # //src/main/native:windows_jni
+    local srcs=$(find src/main/native \
+        -name 'windows_*.cc' -o -name 'windows_*.h')
+    [ -n "$srcs" ] || fail "Could not find sources for Windows JNI library"
+
+    # do not quote $srcs because we need to expand it to multiple args
+    src/main/native/build_windows_jni.sh "$tmp_output" ${srcs}
+
+    cp "$tmp_output" "$output"
+    chmod 0555 "$output"
+
+    JNI_FLAGS="-Dio.bazel.EnableJni=1 -Djava.library.path=${output_dir}"
+    ;;
+
+  *)
+    # We don't need JNI on other platforms.
+    JNI_FLAGS="-Dio.bazel.EnableJni=0"
+    ;;
+  esac
+}
+
+build_jni "${ARCHIVE_DIR}/_embedded_binaries"
+
 cp src/main/tools/build_interface_so ${ARCHIVE_DIR}/_embedded_binaries/build_interface_so
 cp src/main/tools/jdk.BUILD ${ARCHIVE_DIR}/_embedded_binaries/jdk.BUILD
 cp $OUTPUT_DIR/libblaze.jar ${ARCHIVE_DIR}
@@ -303,7 +342,7 @@
       -XX:+HeapDumpOnOutOfMemoryError -Xverify:none -Dfile.encoding=ISO-8859-1 \
       -XX:HeapDumpPath=${OUTPUT_DIR} \
       -Djava.util.logging.config.file=${OUTPUT_DIR}/javalog.properties \
-      -Dio.bazel.EnableJni=0 \
+      ${JNI_FLAGS} \
       -jar ${ARCHIVE_DIR}/libblaze.jar \
       --batch \
       --install_base=${ARCHIVE_DIR} \