diff options
author | 2015-06-01 15:57:50 +0000 | |
---|---|---|
committer | 2015-06-02 14:39:13 +0000 | |
commit | e01aa680eb13dc3c796eeac2393bd2f4ec37fdb7 (patch) | |
tree | f05e95ef33c236ddb175edfb2e3b35a1d873880c /tools/build_rules/genproto.bzl | |
parent | 5fe79331f9160ae8b37f907ddbaa5f080dfc8185 (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
Diffstat (limited to 'tools/build_rules/genproto.bzl')
-rw-r--r-- | tools/build_rules/genproto.bzl | 112 |
1 files changed, 40 insertions, 72 deletions
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"]) |