[singlejar] Port test_util to Windows

- Port various functions in `test_util` to Windows.
- Change `@local_jdk//:jdk-default` (target does not seem to exist) to `@local_jdk//:jdk`.

See #2241

Closes #6248.

PiperOrigin-RevId: 217138180
diff --git a/src/tools/singlejar/BUILD b/src/tools/singlejar/BUILD
index 5fa3047..1b17ec5 100644
--- a/src/tools/singlejar/BUILD
+++ b/src/tools/singlejar/BUILD
@@ -2,13 +2,6 @@
 #   singlejar C++ implementation.
 package(default_visibility = ["//src:__subpackages__"])
 
-JAR_TOOL_PATH_COPT_TPL = "-DJAR_TOOL_PATH=\\\"external/local_jdk/bin/jar%s\\\""
-
-JAR_TOOL_PATH_COPTS = select({
-    "//src/conditions:windows": [JAR_TOOL_PATH_COPT_TPL % ".exe"],
-    "//conditions:default": [JAR_TOOL_PATH_COPT_TPL % ""],
-})
-
 filegroup(
     name = "srcs",
     srcs = glob(["**"]),
@@ -99,7 +92,7 @@
 cc_test(
     name = "desugar_checking_test",
     srcs = [
-        "desugar_checking_test.cc",
+        "combiners_test.cc",
         ":zip_headers",
         ":zlib_interface",
     ],
@@ -151,13 +144,13 @@
         "input_jar_scan_entries_test.h",
         "input_jar_scan_jartool_test.cc",
     ],
-    copts = JAR_TOOL_PATH_COPTS,
-    data = [
-        "@local_jdk//:jar",
-        "@local_jdk//:jdk",
-    ],
+    # TODO(@rongjiecomputer): update copts to handle Windows and add
+    # ".exe" extension.
+    copts = ["-DJAR_TOOL_PATH=$(JAVA)/bin/jar"],
+    data = ["@bazel_tools//tools/jdk:current_java_runtime"],
     # Timing out, see https://github.com/bazelbuild/bazel/issues/1555
     tags = ["manual"],
+    toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
     deps = [
         ":input_jar",
         ":test_util",
@@ -223,20 +216,23 @@
     srcs = [
         "output_jar_simple_test.cc",
     ],
-    copts = JAR_TOOL_PATH_COPTS,
+    # TODO(@rongjiecomputer): update copts to handle Windows and add
+    # ".exe" extension.
+    copts = ["-DJAR_TOOL_PATH=$(JAVA)/bin/jar"],
     data = [
         ":data1",
         ":data2",
         ":stored_jar",
         ":test1",
         ":test2",
-        "@local_jdk//:jar",
-        "@local_jdk//:jdk-default",
+        "@bazel_tools//tools/jdk:current_java_runtime",
     ],
+    toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
     deps = [
         ":input_jar",
         ":options",
         ":output_jar",
+        ":port",
         ":test_util",
         "//src/main/cpp/util",
         "@com_google_googletest//:gtest_main",
@@ -305,8 +301,8 @@
         "//src/test/shell:bashunit",
         "@bazel_tools//tools/bash/runfiles",
         "@bazel_tools//tools/jdk:current_java_runtime",
-        "@local_jdk//:jar",
     ],
+    toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
     deps = ["//src/test/shell:bashunit"],
 )
 
@@ -409,10 +405,15 @@
 
 cc_library(
     name = "test_util",
+    testonly = 1,
     srcs = ["test_util.cc"],
     hdrs = ["test_util.h"],
     deps = [
         "//src/main/cpp/util",
+        # TODO(laszlocsomor) Use @bazel_tools//tools/cpp/runfiles after Bazel is
+        # released with
+        # https://github.com/bazelbuild/bazel/commit/23bc3bee79d8a8b8dc15bbfb6072ec9f965dff96.
+        "//tools/cpp/runfiles:runfiles_src_for_singlejar_only",
         "@com_google_googletest//:gtest_main",
     ],
 )
@@ -486,6 +487,7 @@
         "@bazel_tools//tools/jdk:current_java_runtime",
     ],
     outs = ["stored.jar"],
-    cmd = "$(location @local_jdk//:jar) -0cf \"$@\" $(location :output_jar.cc)",
-    tools = ["@local_jdk//:jar"],
+    cmd = "$(location @bazel_tools//tools/jdk:current_java_runtime) -0cf \"$@\" $(location :output_jar.cc)",
+    toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
+    tools = ["@bazel_tools//tools/jdk:current_java_runtime"],
 )
diff --git a/src/tools/singlejar/test_util.cc b/src/tools/singlejar/test_util.cc
index 722df5d..131e345 100644
--- a/src/tools/singlejar/test_util.cc
+++ b/src/tools/singlejar/test_util.cc
@@ -18,6 +18,10 @@
 #include <stdlib.h>
 #include <sys/types.h>
 
+#ifndef _WIN32
+#include <unistd.h>
+#endif
+
 #include <string>
 
 #include "src/main/cpp/util/file.h"
@@ -27,9 +31,25 @@
 
 #include "googletest/include/gtest/gtest.h"
 
+#ifdef _WIN32
+#define popen _popen
+#define pclose _pclose
+#endif
+
 namespace singlejar_test_util {
 
 bool AllocateFile(const string &name, size_t size) {
+#ifdef _WIN32
+  int fd = _sopen(name.c_str(), _O_RDWR | _O_CREAT | _O_BINARY, _SH_DENYNO,
+                  _S_IREAD | _S_IWRITE);
+  int success = _chsize_s(fd, size);
+  _close(fd);
+  if (success < 0) {
+    perror(strerror(errno));
+    return false;
+  }
+  return true;
+#else
   int fd = open(name.c_str(), O_CREAT | O_RDWR | O_TRUNC, 0777);
   if (fd < 0) {
     perror(name.c_str());
@@ -47,6 +67,7 @@
   } else {
     return close(fd) == 0;
   }
+#endif
 }
 
 int RunCommand(const char *cmd, ...) {
@@ -87,7 +108,13 @@
   string command;
   blaze_util::StringPrintf(&command, "unzip -p %s %s", zip_path.c_str(),
                            entry_name.c_str());
+#ifdef _WIN32
+  // "b" is Microsoft-specific. It's necessary, because without it the popen
+  // implementation will strip "\r\n" to "\n" and make many tests fail.
+  FILE *fp = popen(command.c_str(), "rb");
+#else
   FILE *fp = popen(command.c_str(), "r");
+#endif
   if (!fp) {
     ADD_FAILURE() << "Command " << command << " failed.";
     return string("");
diff --git a/src/tools/singlejar/test_util.h b/src/tools/singlejar/test_util.h
index 184e40f..fbe2661 100644
--- a/src/tools/singlejar/test_util.h
+++ b/src/tools/singlejar/test_util.h
@@ -14,9 +14,10 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-#include <unistd.h>
 #include <string>
 
+#include "tools/cpp/runfiles/runfiles_src.h"
+
 namespace singlejar_test_util {
   using std::string;
 
diff --git a/tools/BUILD b/tools/BUILD
index 684b5d4..c9ffaf5 100644
--- a/tools/BUILD
+++ b/tools/BUILD
@@ -20,7 +20,6 @@
         "//tools/platforms:srcs",
         "//tools/genrule:srcs",
         "//tools/cpp:srcs",
-        "//tools/cpp/runfiles:srcs",
         "//tools/j2objc:srcs",
         "//tools/objc:srcs",
         "//tools/osx:srcs",
@@ -45,8 +44,7 @@
         "//tools/build_rules:embedded_tools_srcs",
         "//tools/buildstamp:srcs",
         "//tools/coverage:srcs",
-        "//tools/cpp:srcs",
-        "//tools/cpp/runfiles:embedded_tools",
+        "//tools/cpp:embedded_tools",
         "//tools/genrule:srcs",
         "//tools/java:embedded_tools",
         "//tools/j2objc:srcs",
diff --git a/tools/cpp/BUILD b/tools/cpp/BUILD
index ee8f1d2..6707fd7 100644
--- a/tools/cpp/BUILD
+++ b/tools/cpp/BUILD
@@ -356,7 +356,16 @@
 
 filegroup(
     name = "srcs",
-    srcs = glob(["**"]),
+    srcs = glob(["**"]) + [
+        "//tools/cpp/runfiles:srcs",
+    ],
+)
+
+filegroup(
+    name = "embedded_tools",
+    srcs = glob(["**"]) + [
+        "//tools/cpp/runfiles:embedded_tools",
+    ],
 )
 
 filegroup(
diff --git a/tools/cpp/runfiles/BUILD b/tools/cpp/runfiles/BUILD
index 3a7c7d8..814144d 100644
--- a/tools/cpp/runfiles/BUILD
+++ b/tools/cpp/runfiles/BUILD
@@ -9,7 +9,7 @@
             "*~",
         ],
     ),
-    visibility = ["//tools:__pkg__"],
+    visibility = ["//tools/cpp:__pkg__"],
 )
 
 filegroup(
@@ -18,7 +18,7 @@
         "BUILD.tools",
         ":srcs_for_embedded_tools",
     ],
-    visibility = ["//tools:__pkg__"],
+    visibility = ["//tools/cpp:__pkg__"],
 )
 
 genrule(
@@ -44,6 +44,23 @@
     hdrs = ["runfiles_src.h"],
 )
 
+# As of 2018-10-01, singlejar needs to temporarily use the C++ runfiles library
+# from source, not from @bazel_tools.
+# Nothing else should depend on this target.
+#
+# TODO(laszlocsomor): remove this library after Bazel is released with
+# https://github.com/bazelbuild/bazel/commit/23bc3bee79d8a8b8dc15bbfb6072ec9f965dff96.
+# Once Bazel is released with that patch, do the following:
+# 1. remove this library
+# 2. let Singlejar depend on @bazel_tools//tools/cpp/runfiles
+# 3. change Singlejar's sources to include "tools/cpp/runfiles.h" instead of
+#    "tools/cpp/runfiles_src.h"
+cc_library(
+    name = "runfiles_src_for_singlejar_only",
+    visibility = ["//src/tools/singlejar:__pkg__"],
+    deps = [":runfiles"],
+)
+
 cc_test(
     name = "runfiles_test",
     srcs = ["runfiles_test.cc"],