Skip to content

Commit

Permalink
Open-source java_lite_proto_library.
Browse files Browse the repository at this point in the history
The new rule doesn't currently function - it requires adding a JavaLite protoc plugin to Bazel, and an additional change to this code to load and use it.

--
MOS_MIGRATED_REVID=130440693
  • Loading branch information
cgrushko authored and philwo committed Aug 17, 2016
1 parent d8e0037 commit 2f36f31
Show file tree
Hide file tree
Showing 8 changed files with 576 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/google/devtools/build/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,8 @@ java_library(
"rules/java/ProguardLibraryRule.java",
"rules/java/ProguardSpecProvider.java",
"rules/java/proto/JavaCompilationArgsAspectProvider.java",
"rules/java/proto/JavaLiteProtoAspect.java",
"rules/java/proto/JavaLiteProtoLibrary.java",
"rules/java/proto/JavaProtoAspect.java",
"rules/java/proto/JavaProtoLibrary.java",
"rules/java/proto/JavaProtoLibraryTransitiveFilesToBuildProvider.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import com.google.devtools.build.lib.bazel.rules.java.BazelJavaPluginRule;
import com.google.devtools.build.lib.bazel.rules.java.BazelJavaRuleClasses;
import com.google.devtools.build.lib.bazel.rules.java.BazelJavaTestRule;
import com.google.devtools.build.lib.bazel.rules.java.proto.BazelJavaLiteProtoAspect;
import com.google.devtools.build.lib.bazel.rules.java.proto.BazelJavaLiteProtoLibraryRule;
import com.google.devtools.build.lib.bazel.rules.java.proto.BazelJavaProtoAspect;
import com.google.devtools.build.lib.bazel.rules.java.proto.BazelJavaProtoLibraryRule;
import com.google.devtools.build.lib.bazel.rules.objc.BazelJ2ObjcLibraryRule;
Expand Down Expand Up @@ -396,12 +398,14 @@ private static void initEverythingElse(ConfiguredRuleClassProvider.Builder build
new AndroidStudioInfoAspect(toolsRepository, new BazelAndroidStudioInfoSemantics());
ObjcProtoAspect objcProtoAspect = new ObjcProtoAspect();
BazelJavaProtoAspect bazelJavaProtoAspect = new BazelJavaProtoAspect();
BazelJavaLiteProtoAspect bazelJavaLiteProtoAspect = new BazelJavaLiteProtoAspect();

builder.addNativeAspectClass(bazelJ2ObjcProtoAspect);
builder.addNativeAspectClass(j2ObjcAspect);
builder.addNativeAspectClass(androidStudioInfoAspect);
builder.addNativeAspectClass(objcProtoAspect);
builder.addNativeAspectClass(bazelJavaProtoAspect);
builder.addNativeAspectClass(bazelJavaLiteProtoAspect);

builder.addRuleDefinition(new BazelShRuleClasses.ShRule());
builder.addRuleDefinition(new BazelShLibraryRule());
Expand Down Expand Up @@ -479,6 +483,7 @@ private static void initEverythingElse(ConfiguredRuleClassProvider.Builder build
builder.addRuleDefinition(new AndroidSdkRepositoryRule());
builder.addRuleDefinition(new AndroidNdkRepositoryRule());
builder.addRuleDefinition(new BazelJavaProtoLibraryRule(bazelJavaProtoAspect));
builder.addRuleDefinition(new BazelJavaLiteProtoLibraryRule(bazelJavaLiteProtoAspect));

builder.addConfigurationFragment(new PythonConfigurationLoader(Functions.<String>identity()));
builder.addConfigurationFragment(new BazelPythonConfiguration.Loader());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public Metadata getMetadata() {
"java_import",
"java_library",
"java_proto_library",
"java_lite_proto_library",
"proto_library",
"sh_binary",
"sh_library");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2016 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.bazel.rules.java.proto;

import com.google.devtools.build.lib.bazel.rules.java.BazelJavaSemantics;
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 {
static final String LITE_PROTO_RUNTIME_ATTR = "$aspect_java_lib";
static final String LITE_PROTO_RUNTIME_LABEL = "//third_party/protobuf:protobuf";

public BazelJavaLiteProtoAspect() {
super(
BazelJavaSemantics.INSTANCE,
LITE_PROTO_RUNTIME_ATTR,
LITE_PROTO_RUNTIME_LABEL,
null /* jacocoLabel */);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2016 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.bazel.rules.java.proto;

import com.google.devtools.build.lib.rules.java.proto.JavaLiteProtoLibrary;

/** Implementation of the java_lite_proto_library rule. */
public class BazelJavaLiteProtoLibrary extends JavaLiteProtoLibrary {
public BazelJavaLiteProtoLibrary() {
super(BazelJavaLiteProtoAspect.LITE_PROTO_RUNTIME_ATTR);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// Copyright 2016 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.bazel.rules.java.proto;

import static com.google.devtools.build.lib.bazel.rules.java.proto.BazelJavaLiteProtoAspect.LITE_PROTO_RUNTIME_ATTR;
import static com.google.devtools.build.lib.bazel.rules.java.proto.BazelJavaLiteProtoAspect.LITE_PROTO_RUNTIME_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.syntax.Type.BOOLEAN;

import com.google.common.base.Function;
import com.google.devtools.build.lib.analysis.BaseRuleClasses;
import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.AspectParameters;
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.rules.java.JavaConfiguration;
import javax.annotation.Nullable;

/** Declaration of the {@code java_lite_proto_library} rule. */
public class BazelJavaLiteProtoLibraryRule implements RuleDefinition {

private static final Function<Rule, AspectParameters> ASPECT_PARAMETERS =
new Function<Rule, AspectParameters>() {
@Nullable
@Override
public AspectParameters apply(@Nullable Rule rule) {
return new AspectParameters.Builder()
.addAttribute(INJECTING_RULE_KIND_PARAMETER_KEY, "java_lite_proto_library")
.build();
}
};

private final BazelJavaLiteProtoAspect javaProtoAspect;

public BazelJavaLiteProtoLibraryRule(BazelJavaLiteProtoAspect javaProtoAspect) {
this.javaProtoAspect = javaProtoAspect;
}

@Override
public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment environment) {
return builder
// This rule isn't ready for use yet.
.setUndocumented()
.requiresConfigurationFragments(JavaConfiguration.class)
/* <!-- #BLAZE_RULE(java_lite_proto_library).ATTRIBUTE(deps) -->
The list of <a href="protocol-buffer.html#proto_library"><code>proto_library</code></a>
rules to generate Java code for.
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.override(
attr("deps", LABEL_LIST)
.allowedRuleClasses("proto_library")
.allowedFileTypes()
.aspect(javaProtoAspect, ASPECT_PARAMETERS))
/* <!-- #BLAZE_RULE(java_lite_proto_library).ATTRIBUTE(deps) -->
When True, this rule only exposes the protos that it wraps directly. Depending on indirect
protos will break the build and print an 'add_dep' command to correct the build.
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(attr("strict_deps", BOOLEAN))
.add(
attr(LITE_PROTO_RUNTIME_ATTR, LABEL)
.legacyAllowAnyFileType()
.value(Label.parseAbsoluteUnchecked(LITE_PROTO_RUNTIME_LABEL)))
.build();
}

@Override
public Metadata getMetadata() {
return RuleDefinition.Metadata.builder()
.name("java_lite_proto_library")
.factoryClass(BazelJavaLiteProtoLibrary.class)
.ancestors(BaseRuleClasses.RuleBase.class)
.build();
}
}

/*<!-- #BLAZE_RULE (NAME = java_lite_proto_library, TYPE = LIBRARY, FAMILY = Java) -->
<p>
<code>java_lite_proto_library</code> generates Java code from <code>.proto</code> files.
</p>
<p>
<code>deps</code> must point to <a href="protocol-buffer.html#proto_library"><code>proto_library
</code></a> rules.
</p>
<p>
Example:
</p>
<pre class="code">
java_library(
name = "lib",
deps = [":foo"],
)
java_lite_proto_library(
name = "foo",
deps = [":bar"],
)
proto_library(
name = "bar",
)
</pre>
<!-- #END_BLAZE_RULE -->*/
Loading

0 comments on commit 2f36f31

Please sign in to comment.