aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Han-Wen Nienhuys <hanwen@google.com>2015-06-01 15:57:50 +0000
committerGravatar Han-Wen Nienhuys <hanwen@google.com>2015-06-02 14:39:13 +0000
commite01aa680eb13dc3c796eeac2393bd2f4ec37fdb7 (patch)
treef05e95ef33c236ddb175edfb2e3b35a1d873880c
parent5fe79331f9160ae8b37f907ddbaa5f080dfc8185 (diff)
Simplify genproto.bzl.
Generate a srcjar artifact, and use native.java_library to emit the actual jar file. Post-review modifications: - Formatted genproto.bzl - Use //external:jar instead of relying on PATH - Make proto library a java_import instead of a filegroup in test setup. -- Change-Id: I005f801f09439bbca43e483ad4a47da4588fea94 Reviewed-on: https://bazel-review.googlesource.com/1362 MOS_MIGRATED_REVID=94914935
-rw-r--r--examples/proto/BUILD4
-rw-r--r--src/main/protobuf/BUILD14
-rw-r--r--src/main/tools/jdk.BUILD5
-rw-r--r--src/main/tools/jdk.WORKSPACE5
-rwxr-xr-xsrc/test/shell/bazel/test-setup.sh4
-rw-r--r--tools/build_rules/genproto.bzl112
6 files changed, 62 insertions, 82 deletions
diff --git a/examples/proto/BUILD b/examples/proto/BUILD
index cae538d877..7ce5659ea6 100644
--- a/examples/proto/BUILD
+++ b/examples/proto/BUILD
@@ -1,8 +1,8 @@
package(default_visibility = ["//visibility:public"])
-load("/tools/build_rules/genproto", "genproto")
+load("/tools/build_rules/genproto", "proto_java_library")
-genproto(
+proto_java_library(
name = "test_proto",
src = "test.proto",
)
diff --git a/src/main/protobuf/BUILD b/src/main/protobuf/BUILD
index 717d9bf4fc..c319e34d92 100644
--- a/src/main/protobuf/BUILD
+++ b/src/main/protobuf/BUILD
@@ -1,11 +1,8 @@
package(default_visibility = ["//visibility:public"])
-load("/tools/build_rules/genproto", "genproto")
+load("/tools/build_rules/genproto", "proto_java_library")
-[genproto(
- name = "proto_" + proto_file,
- src = proto_file + ".proto",
-) for proto_file in [
+FILES = [
"build",
"deps",
"crosstool_config",
@@ -13,7 +10,12 @@ load("/tools/build_rules/genproto", "genproto")
"test_status",
"bundlemerge",
"xcodegen",
-]]
+]
+
+[proto_java_library(
+ name = "proto_" + s,
+ src = s + ".proto",
+) for s in FILES]
filegroup(
name = "srcs",
diff --git a/src/main/tools/jdk.BUILD b/src/main/tools/jdk.BUILD
index c3c7fd558e..9d0979f254 100644
--- a/src/main/tools/jdk.BUILD
+++ b/src/main/tools/jdk.BUILD
@@ -20,6 +20,11 @@ filegroup(
srcs = ["bin/java"],
)
+filegroup(
+ name = "jar",
+ srcs = ["bin/jar"],
+)
+
BOOTCLASS_JARS = [
"rt.jar",
"resources.jar",
diff --git a/src/main/tools/jdk.WORKSPACE b/src/main/tools/jdk.WORKSPACE
index d8fb5e5154..c26eaa3133 100644
--- a/src/main/tools/jdk.WORKSPACE
+++ b/src/main/tools/jdk.WORKSPACE
@@ -43,6 +43,11 @@ bind(
)
bind(
+ name = "jar",
+ actual = "@local-jdk//:jar",
+)
+
+bind(
name = "jdk-default",
actual = "@local-jdk//:jdk-default",
)
diff --git a/src/test/shell/bazel/test-setup.sh b/src/test/shell/bazel/test-setup.sh
index a0fe04aa00..ae134866cc 100755
--- a/src/test/shell/bazel/test-setup.sh
+++ b/src/test/shell/bazel/test-setup.sh
@@ -53,9 +53,9 @@ function setup_protoc_support() {
cat <<EOF > third_party/BUILD
package(default_visibility = ["//visibility:public"])
exports_files(["protoc"])
-filegroup(
+java_import(
name = "protobuf",
- srcs = [ "protobuf-java.jar"])
+ jars = ["protobuf-java.jar"])
EOF
}
diff --git a/tools/build_rules/genproto.bzl b/tools/build_rules/genproto.bzl
index f479027f57..a2e2d735a9 100644
--- a/tools/build_rules/genproto.bzl
+++ b/tools/build_rules/genproto.bzl
@@ -15,82 +15,50 @@
# This is a quick and dirty rule to make Bazel compile itself. It
# only supports Java.
-
-jar_filetype = FileType([".jar"])
-
proto_filetype = FileType([".proto"])
-def java_compile_command(ctx, classdir, classpath, output):
- java = ctx.file._java.path
- langtools = ctx.file._java_langtools.path
- javabuilder = ctx.file._javabuilder.path
- return ("%s -Xbootclasspath/p:%s -jar %s " % (java, langtools, javabuilder) +
- "--classdir %s --classpath %s " % (classdir, classpath) +
- "--output %s " % (output) +
- "--javacopts -source 1.8 -target 1.8 --compress_jar --sources ${JAVA_FILES}")
-
-def genproto_impl(ctx):
- src = ctx.file.src
+def gensrcjar_impl(ctx):
+ out = ctx.outputs.srcjar
+ proto_output = out.path + ".proto_output"
proto_compiler = ctx.file._proto_compiler
- proto_dep = ctx.file._proto_dep
- class_jar = ctx.outputs.java
- proto_output = class_jar.path + ".proto_output"
- build_output = class_jar.path + ".build_output"
- build_output = class_jar.path + ".build_output"
-
- inputs = [src, proto_dep, proto_compiler]
- proto_compiler_path = proto_compiler.path
-
- javapath = "tools/jdk/jdk/bin/"
- cmd = ("set -e;" +
- "rm -rf " + proto_output + ";" +
- "mkdir " + proto_output + ";" +
- "rm -rf " + build_output + ";" +
- "mkdir " + build_output + "\n" +
- proto_compiler_path + " --java_out=" +
- proto_output +" " + src.path + "\n" +
- "JAVA_FILES=$(find " + proto_output + " -name '*.java')\n" +
- java_compile_command(ctx, build_output, proto_dep.path, class_jar.path))
+ sub_commands = [
+ "rm -rf " + proto_output,
+ "mkdir " + proto_output,
+ ' '.join([proto_compiler.path, "--java_out=" + proto_output,
+ ctx.file.src.path]),
+ "touch -t 198001010000 $(find " + proto_output + ")",
+ ctx.file._jar.path + " cMf " + out.path + " -C " + proto_output + " .",
+ ]
ctx.action(
- inputs = inputs,
- outputs = [class_jar],
- mnemonic = 'CompileProtos',
- command = cmd,
- use_default_shell_env = True)
-
- return struct(compile_time_jars = set([class_jar]),
- runtime_jars = set([class_jar, proto_dep], order="link"))
+ command=" && ".join(sub_commands),
+ inputs=[ctx.file.src, proto_compiler, ctx.file._jar],
+ outputs=[out],
+ mnemonic="GenProtoSrcJar",
+ use_default_shell_env = True)
-
-genproto = rule(genproto_impl,
- # There should be a flag like gen_java, and only generate the jar if it's
- # set. Skylark needs a bit of improvement first (concat structs).
- attrs = {
- "src": attr.label(allow_files=proto_filetype, single_file=True),
- # TODO(bazel-team): this should be a hidden attribute with a default
- # value, but Skylark needs to support select first.
- "_proto_compiler": attr.label(
- default=Label("//third_party:protoc"),
- allow_files=True,
- single_file=True),
- "_proto_dep": attr.label(
- default=Label("//third_party:protobuf"),
- single_file=True,
- allow_files=jar_filetype,
- ),
- "_javabuilder": attr.label(
- default=Label("//tools/defaults:javabuilder"),
- single_file=True,
- ),
- "_java_langtools": attr.label(
- default=Label("//tools/defaults:java_langtools"),
- single_file=True,
- ),
- "_java": attr.label(
- default=Label("//tools/jdk:java"),
- single_file=True,
- ),
- },
- outputs = {"java": "lib%{name}.jar"},
+gensrcjar = rule(
+ gensrcjar_impl,
+ attrs={
+ "src": attr.label(allow_files=proto_filetype, single_file=True),
+ # TODO(bazel-team): this should be a hidden attribute with a default
+ # value, but Skylark needs to support select first.
+ "_proto_compiler": attr.label(
+ default=Label("//third_party:protoc"),
+ allow_files=True,
+ single_file=True),
+ "_jar": attr.label(
+ default=Label("//external:jar"),
+ allow_files=True,
+ single_file=True),
+ },
+ outputs={"srcjar": "lib%{name}.srcjar"},
)
+
+# TODO(bazel-team): support proto => proto dependencies too
+def proto_java_library(name, src):
+ gensrcjar(name=name + "_srcjar", src=src)
+ native.java_library(
+ name=name,
+ srcs=[name + "_srcjar"],
+ deps=["//third_party:protobuf"])