diff options
author | Carmi Grushko <carmi@google.com> | 2016-11-29 01:04:35 +0000 |
---|---|---|
committer | Irina Iancu <elenairina@google.com> | 2016-11-29 08:07:22 +0000 |
commit | f1aa34c87cbd1944f385911244f3828e8d9114eb (patch) | |
tree | b7c656cb89e1f9ebbdd7a13ddbd585a2d61cb512 | |
parent | 2b5038831c2514f65841ce4e40f8e4648250bf01 (diff) |
Make it easier to use protos in Bazel out of the box.
java_xxx_proto_library rules now look for toolchains in the external repo @com_google_protobuf_xxx//:xxx_toolchain
This still requires getting protobuf's GitHub repository to build with Bazel.
--
MOS_MIGRATED_REVID=140420903
8 files changed, 62 insertions, 26 deletions
@@ -59,3 +59,8 @@ bind(name = "xcrunwrapper", actual = "@bazel_tools//tools/objc:xcrunwrapper") bind(name = "protobuf/java_runtime", actual = "//third_party/protobuf:protobuf") bind(name = "protobuf/javalite_runtime", actual = "//third_party/protobuf:protobuf-lite") bind(name = "proto/toolchains/java", actual = "//third_party/protobuf:java_toolchain") +new_local_repository( + name = "com_google_protobuf_java", + path = "./third_party/protobuf/3.0.0/", + build_file = "./third_party/protobuf/3.0.0/com_google_protobuf_java.BUILD", +) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/proto/BazelJavaLiteProtoAspect.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/proto/BazelJavaLiteProtoAspect.java index ac5e751fd0..f628e396b1 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/proto/BazelJavaLiteProtoAspect.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/proto/BazelJavaLiteProtoAspect.java @@ -19,7 +19,11 @@ import com.google.devtools.build.lib.rules.java.proto.JavaLiteProtoAspect; /** An Aspect which BazelJavaLiteProtoLibrary injects to build Java Lite protos. */ public class BazelJavaLiteProtoAspect extends JavaLiteProtoAspect { + + public static final String DEFAULT_PROTO_TOOLCHAIN_LABEL = + "@com_google_protobuf_javalite//:javalite_toolchain"; + public BazelJavaLiteProtoAspect() { - super(BazelJavaSemantics.INSTANCE, null /* jacocoLabel */); + super(BazelJavaSemantics.INSTANCE, null /* jacocoLabel */, DEFAULT_PROTO_TOOLCHAIN_LABEL); } } diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/proto/BazelJavaLiteProtoLibraryRule.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/proto/BazelJavaLiteProtoLibraryRule.java index 739de4b730..a608d6331d 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/proto/BazelJavaLiteProtoLibraryRule.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/proto/BazelJavaLiteProtoLibraryRule.java @@ -14,12 +14,13 @@ package com.google.devtools.build.lib.bazel.rules.java.proto; +import static com.google.devtools.build.lib.bazel.rules.java.proto.BazelJavaLiteProtoAspect.DEFAULT_PROTO_TOOLCHAIN_LABEL; import static com.google.devtools.build.lib.packages.Aspect.INJECTING_RULE_KIND_PARAMETER_KEY; import static com.google.devtools.build.lib.packages.Attribute.attr; import static com.google.devtools.build.lib.packages.BuildType.LABEL; import static com.google.devtools.build.lib.packages.BuildType.LABEL_LIST; import static com.google.devtools.build.lib.rules.java.proto.JavaLiteProtoAspect.PROTO_TOOLCHAIN_ATTR; -import static com.google.devtools.build.lib.rules.java.proto.JavaLiteProtoAspect.PROTO_TOOLCHAIN_LABEL; +import static com.google.devtools.build.lib.rules.java.proto.JavaLiteProtoAspect.getProtoToolchainLabel; import static com.google.devtools.build.lib.syntax.Type.BOOLEAN; import com.google.common.base.Function; @@ -81,7 +82,7 @@ public class BazelJavaLiteProtoLibraryRule implements RuleDefinition { .mandatoryNativeProviders( ImmutableList.<Class<? extends TransitiveInfoProvider>>of( ProtoLangToolchainProvider.class)) - .value(PROTO_TOOLCHAIN_LABEL)) + .value(getProtoToolchainLabel(DEFAULT_PROTO_TOOLCHAIN_LABEL))) .build(); } diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/proto/BazelJavaProtoAspect.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/proto/BazelJavaProtoAspect.java index 8dd2ae2e07..e1f15c064e 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/java/proto/BazelJavaProtoAspect.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/java/proto/BazelJavaProtoAspect.java @@ -37,7 +37,8 @@ public class BazelJavaProtoAspect extends JavaProtoAspect { super( BazelJavaSemantics.INSTANCE, null, /* jacocoAttr */ - new NoopRpcSupport()); + new NoopRpcSupport(), + "@com_google_protobuf_java//:java_toolchain"); } private static class NoopRpcSupport diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java index 7ae7cadd19..e8fa34660a 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java @@ -66,22 +66,29 @@ public class JavaLiteProtoAspect extends NativeAspectClass implements Configured public static final String PROTO_TOOLCHAIN_ATTR = ":aspect_proto_toolchain_for_javalite"; - public static final Attribute.LateBoundLabel<BuildConfiguration> PROTO_TOOLCHAIN_LABEL = - new Attribute.LateBoundLabel<BuildConfiguration>( - "//tools/proto/toolchains:javalite", ProtoConfiguration.class) { - @Override - public Label resolve(Rule rule, AttributeMap attributes, BuildConfiguration configuration) { - return configuration.getFragment(ProtoConfiguration.class).protoToolchainForJavaLite(); - } - }; + public static Attribute.LateBoundLabel<BuildConfiguration> getProtoToolchainLabel( + String defaultValue) { + return new Attribute.LateBoundLabel<BuildConfiguration>( + defaultValue, ProtoConfiguration.class) { + @Override + public Label resolve(Rule rule, AttributeMap attributes, BuildConfiguration configuration) { + return configuration.getFragment(ProtoConfiguration.class).protoToolchainForJavaLite(); + } + }; + } private final JavaSemantics javaSemantics; @Nullable private final String jacocoLabel; + private final String defaultProtoToolchainLabel; - public JavaLiteProtoAspect(JavaSemantics javaSemantics, @Nullable String jacocoLabel) { + public JavaLiteProtoAspect( + JavaSemantics javaSemantics, + @Nullable String jacocoLabel, + String defaultProtoToolchainLabel) { this.javaSemantics = javaSemantics; this.jacocoLabel = jacocoLabel; + this.defaultProtoToolchainLabel = defaultProtoToolchainLabel; } @Override @@ -112,7 +119,7 @@ public class JavaLiteProtoAspect extends NativeAspectClass implements Configured .mandatoryNativeProviders( ImmutableList.<Class<? extends TransitiveInfoProvider>>of( ProtoLangToolchainProvider.class)) - .value(PROTO_TOOLCHAIN_LABEL)) + .value(getProtoToolchainLabel(defaultProtoToolchainLabel))) .add(attr(":host_jdk", LABEL).cfg(HOST).value(JavaSemantics.HOST_JDK)) .add( attr(":java_toolchain", LABEL) diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java index 9de56254df..5c0b29201f 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java +++ b/src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java @@ -97,27 +97,32 @@ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspe } }; - private static final Attribute.LateBoundLabel<BuildConfiguration> SPEED_PROTO_TOOLCHAIN_LABEL = - new Attribute.LateBoundLabel<BuildConfiguration>( - "//tools/proto/toolchains:java", ProtoConfiguration.class) { - @Override - public Label resolve(Rule rule, AttributeMap attributes, BuildConfiguration configuration) { - return configuration.getFragment(ProtoConfiguration.class).protoToolchainForJava(); - } - }; + private static Attribute.LateBoundLabel<BuildConfiguration> getSpeedProtoToolchainLabel( + String defaultValue) { + return new Attribute.LateBoundLabel<BuildConfiguration>( + defaultValue, ProtoConfiguration.class) { + @Override + public Label resolve(Rule rule, AttributeMap attributes, BuildConfiguration configuration) { + return configuration.getFragment(ProtoConfiguration.class).protoToolchainForJava(); + } + }; + } private final JavaSemantics javaSemantics; @Nullable private final String jacocoLabel; private final RpcSupport rpcSupport; + private final String defaultSpeedProtoToolchainLabel; protected JavaProtoAspect( JavaSemantics javaSemantics, @Nullable String jacocoLabel, - RpcSupport rpcSupport) { + RpcSupport rpcSupport, + String defaultSpeedProtoToolchainLabel) { this.javaSemantics = javaSemantics; this.jacocoLabel = jacocoLabel; this.rpcSupport = rpcSupport; + this.defaultSpeedProtoToolchainLabel = defaultSpeedProtoToolchainLabel; } @Override @@ -170,7 +175,7 @@ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspe // TODO(carmi): reinstate mandatoryNativeProviders(ProtoLangToolchainProvider) // once it's in a Bazel release. .legacyAllowAnyFileType() - .value(SPEED_PROTO_TOOLCHAIN_LABEL)) + .value(getSpeedProtoToolchainLabel(defaultSpeedProtoToolchainLabel))) .add(attr(":host_jdk", LABEL).cfg(HOST).value(JavaSemantics.HOST_JDK)) .add( attr(":java_toolchain", LABEL) diff --git a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoConfiguration.java b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoConfiguration.java index 39be8302b6..ed10c4a564 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoConfiguration.java +++ b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoConfiguration.java @@ -115,7 +115,7 @@ public class ProtoConfiguration extends Fragment { @Option( name = "proto_toolchain_for_javalite", - defaultValue = "//tools/proto/toolchains:javalite", + defaultValue = "@com_google_protobuf_javalite//:javalite_toolchain", category = "flags", converter = BuildConfiguration.EmptyToNullLabelConverter.class, help = "Label of proto_lang_toolchain() which describes how to compile JavaLite protos" @@ -124,7 +124,7 @@ public class ProtoConfiguration extends Fragment { @Option( name = "proto_toolchain_for_java", - defaultValue = "//tools/proto/toolchains:java", + defaultValue = "@com_google_protobuf_java//:java_toolchain", category = "flags", converter = BuildConfiguration.EmptyToNullLabelConverter.class, help = "Label of proto_lang_toolchain() which describes how to compile Java protos" diff --git a/third_party/protobuf/3.0.0/com_google_protobuf_java.BUILD b/third_party/protobuf/3.0.0/com_google_protobuf_java.BUILD new file mode 100644 index 0000000000..ce43ac82d4 --- /dev/null +++ b/third_party/protobuf/3.0.0/com_google_protobuf_java.BUILD @@ -0,0 +1,13 @@ +load(":proto_lang_toolchain_if_exists.bzl", "proto_lang_toolchain") + +java_import( + name = "protobuf", + jars = ["protobuf-java-3.0.0.jar"], +) + +proto_lang_toolchain( + name = "java_toolchain", + command_line = "--java_out=shared,immutable:$(OUT)", + runtime = ":protobuf", + visibility = ["//visibility:public"], +) |