Bootstrapping tools using Bazel

Added target to use java skylark rules to bootstrap
JavaBuilder and SingleJar. Uses thoses target to bootstrap
JavaBuilder and SingleJar and compiles all tools using
Bazel.

--
Change-Id: I5142917c8b31e04015fbab89382df36b4892d8c6
Reviewed-on: https://bazel-review.googlesource.com/#/c/1451/
MOS_MIGRATED_REVID=95281092
diff --git a/tools/build_rules/java_rules_skylark.bzl b/tools/build_rules/java_rules_skylark.bzl
index 1f556ef..16640e9 100644
--- a/tools/build_rules/java_rules_skylark.bzl
+++ b/tools/build_rules/java_rules_skylark.bzl
@@ -31,6 +31,7 @@
 # production ready.
 
 def java_library_impl(ctx):
+  javac_options = ctx.configuration.fragment(java_configuration).default_javac_flags
   class_jar = ctx.outputs.class_jar
   compile_time_jars = set(order="link")
   runtime_jars = set(order="link")
@@ -39,11 +40,14 @@
     runtime_jars += dep.runtime_jars
 
   jars = jar_filetype.filter(ctx.files.jars)
-  compile_time_jars += jars
+  neverlink_jars = jar_filetype.filter(ctx.files.neverlink_jars)
+  compile_time_jars += jars + neverlink_jars
   runtime_jars += jars
   compile_time_jars_list = list(compile_time_jars) # TODO: This is weird.
 
   build_output = class_jar.path + ".build_output"
+  java_output = class_jar.path + ".build_java"
+  javalist_output = class_jar.path + ".build_java_list"
   sources = ctx.files.srcs
 
   sources_param_file = ctx.new_file(
@@ -54,12 +58,22 @@
       executable = False)
 
   # Cleaning build output directory
-  cmd = "set -e;rm -rf " + build_output + ";mkdir " + build_output + "\n"
-  if ctx.files.srcs:
+  cmd = "set -e;rm -rf " + build_output + " " + java_output + " " + javalist_output + "\n"
+  cmd += "mkdir " + build_output + " " + java_output + "\n"
+  files = " @" + sources_param_file.path
+
+  if ctx.files.srcjars:
+    files += " @" + javalist_output
+    for file in ctx.files.srcjars:
+      cmd += "jar tf %s | grep '\.java$' | sed 's|^|%s/|' >> %s\n" % (file.path, java_output, javalist_output)
+      cmd += "unzip %s -d %s >/dev/null\n" % (file.path, java_output)
+
+  if ctx.files.srcs or ctx.files.srcjars:
     cmd += JAVA_PATH + "javac"
+    cmd += " " + " ".join(javac_options)
     if compile_time_jars:
       cmd += " -classpath '" + cmd_helper.join_paths(path_separator(ctx), compile_time_jars) + "'"
-    cmd += " -d " + build_output + " @" + sources_param_file.path + "\n"
+    cmd += " -d " + build_output + files + "\n"
 
   # We haven't got a good story for where these should end up, so
   # stick them in the root of the jar.
@@ -69,7 +83,7 @@
          "touch " + build_output + "\n")
   ctx.action(
     inputs = (sources + compile_time_jars_list + [sources_param_file] +
-              ctx.files.resources),
+              ctx.files.resources + ctx.files.srcjars),
     outputs = [class_jar],
     mnemonic='Javac',
     command=cmd,
@@ -167,9 +181,10 @@
   # TODO(bazel-team): Why do we need to filter here? The attribute
   # already says only jars are allowed.
   jars = set(jar_filetype.filter(ctx.files.jars))
+  neverlink_jars = set(jar_filetype.filter(ctx.files.neverlink_jars))
   runfiles = ctx.runfiles(collect_data = True)
   return struct(files = jars,
-                compile_time_jars = jars,
+                compile_time_jars = jars + neverlink_jars,
                 runtime_jars = jars,
                 runfiles = runfiles)
 
@@ -179,6 +194,8 @@
     "resources": attr.label_list(allow_files=True),
     "srcs": attr.label_list(allow_files=java_filetype),
     "jars": attr.label_list(allow_files=jar_filetype),
+    "neverlink_jars": attr.label_list(allow_files=jar_filetype),
+    "srcjars": attr.label_list(allow_files=srcjar_filetype),
     "deps": attr.label_list(
         allow_files=False,
         providers = ["compile_time_jars", "runtime_jars"]),
@@ -191,6 +208,14 @@
         "class_jar": "lib%{name}.jar",
     })
 
+# A copy to avoid conflict with native rule.
+bootstrap_java_library = rule(
+    java_library_impl,
+    attrs = java_library_attrs,
+    outputs = {
+        "class_jar": "lib%{name}.jar",
+    })
+
 java_binary_attrs_common = java_library_attrs + {
     "jvm_flags": attr.string_list(),
     "jvm": attr.label(default=Label("//tools/jdk:jdk"), allow_files=True),
@@ -213,6 +238,12 @@
    attrs = java_binary_attrs,
    outputs = java_binary_outputs)
 
+# A copy to avoid conflict with native rule
+bootstrap_java_binary = rule(java_binary_impl,
+   executable = True,
+   attrs = java_binary_attrs,
+   outputs = java_binary_outputs)
+
 java_test = rule(java_binary_impl,
    executable = True,
    attrs = java_binary_attrs_common + {
@@ -230,4 +261,5 @@
     attrs = {
         "jars": attr.label_list(allow_files=jar_filetype),
         "srcjar": attr.label(allow_files=srcjar_filetype),
+        "neverlink_jars": attr.label_list(allow_files=jar_filetype, default=[]),
     })