aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar twerth <twerth@google.com>2018-08-14 00:00:02 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-08-14 00:01:58 -0700
commite5719662a91a7eb310eb0ea528992c49090784c9 (patch)
tree3a7ec7df05198ce8acca7700d5d4dd5cdd51bb61
parent1f253d2a1a44bd53910e26a891e1ef88301e934d (diff)
Add exports attribute to proto_library.
Note that it is currently only used by the java_proto_library family of rules (if enabled per flag). RELNOTES: None PiperOrigin-RevId: 208601730
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java8
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java10
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaLiteProtoAspect.java32
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/java/proto/JavaProtoAspect.java35
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/proto/BazelProtoLibrary.java4
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java43
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java59
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/proto/ProtoConfiguration.java15
-rw-r--r--src/main/java/com/google/devtools/build/lib/rules/proto/SupportData.java18
-rw-r--r--src/test/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilderTest.java178
10 files changed, 314 insertions, 88 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java
index 6bc1ed10d7..97cb73987f 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaConfiguration.java
@@ -154,6 +154,7 @@ public final class JavaConfiguration extends Fragment implements JavaConfigurati
private final boolean generateJavaDeps;
private final boolean strictDepsJavaProtos;
private final boolean protoGeneratedStrictDeps;
+ private final boolean isJavaProtoExportsEnabled;
private final OneVersionEnforcementLevel enforceOneVersion;
private final boolean enforceOneVersionOnJavaTests;
private final ImportDepsCheckingLevel importDepsCheckingLevel;
@@ -204,6 +205,7 @@ public final class JavaConfiguration extends Fragment implements JavaConfigurati
this.useLegacyBazelJavaTest = javaOptions.legacyBazelJavaTest;
this.strictDepsJavaProtos = javaOptions.strictDepsJavaProtos;
this.protoGeneratedStrictDeps = javaOptions.protoGeneratedStrictDeps;
+ this.isJavaProtoExportsEnabled = javaOptions.isJavaProtoExportsEnabled;
this.enforceOneVersion = javaOptions.enforceOneVersion;
this.enforceOneVersionOnJavaTests = javaOptions.enforceOneVersionOnJavaTests;
this.importDepsCheckingLevel = javaOptions.importDepsCheckingLevel;
@@ -246,6 +248,7 @@ public final class JavaConfiguration extends Fragment implements JavaConfigurati
boolean generateJavaDeps,
boolean strictDepsJavaProtos,
boolean protoGeneratedStrictDeps,
+ boolean isJavaProtoExportsEnabled,
OneVersionEnforcementLevel enforceOneVersion,
boolean enforceOneVersionOnJavaTests,
ImportDepsCheckingLevel importDepsCheckingLevel,
@@ -276,6 +279,7 @@ public final class JavaConfiguration extends Fragment implements JavaConfigurati
this.generateJavaDeps = generateJavaDeps;
this.strictDepsJavaProtos = strictDepsJavaProtos;
this.protoGeneratedStrictDeps = protoGeneratedStrictDeps;
+ this.isJavaProtoExportsEnabled = isJavaProtoExportsEnabled;
this.enforceOneVersion = enforceOneVersion;
this.enforceOneVersionOnJavaTests = enforceOneVersionOnJavaTests;
this.importDepsCheckingLevel = importDepsCheckingLevel;
@@ -512,6 +516,10 @@ public final class JavaConfiguration extends Fragment implements JavaConfigurati
return protoGeneratedStrictDeps;
}
+ public boolean isJavaProtoExportsEnabled() {
+ return isJavaProtoExportsEnabled;
+ }
+
public boolean jplPropagateCcLinkParamsStore() {
return jplPropagateCcLinkParamsStore;
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java b/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java
index 5bb7e08106..2b9cbecf05 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/java/JavaOptions.java
@@ -467,6 +467,16 @@ public class JavaOptions extends FragmentOptions {
public boolean protoGeneratedStrictDeps;
@Option(
+ name = "experimental_enable_java_proto_exports",
+ defaultValue = "false",
+ documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
+ effectTags = {OptionEffectTag.BUILD_FILE_SEMANTICS, OptionEffectTag.EAGERNESS_TO_EXIT},
+ help =
+ "Enables exports forwarding for proto_library targets depended on by "
+ + "java_proto_library targets.")
+ public boolean isJavaProtoExportsEnabled;
+
+ @Option(
name = "experimental_java_header_compilation_disable_javac_fallback",
defaultValue = "false",
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
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 09cf02d0d1..a2649622d5 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
@@ -113,6 +113,7 @@ public class JavaLiteProtoAspect extends NativeAspectClass implements Configured
AspectDefinition.Builder result =
new AspectDefinition.Builder(this)
.propagateAlongAttribute("deps")
+ .propagateAlongAttribute("exports")
.requiresConfigurationFragments(JavaConfiguration.class, ProtoConfiguration.class)
.requireProviders(ProtoSourcesProvider.class)
.advertiseProvider(JavaProtoLibraryAspectProvider.class)
@@ -154,9 +155,14 @@ public class JavaLiteProtoAspect extends NativeAspectClass implements Configured
*/
private final JavaCompilationArgsProvider dependencyCompilationArgs;
+ // Compilation-args from all exports, merged together.
+ private final JavaCompilationArgsProvider exportsCompilationArgs;
+
private final JavaProtoAspectCommon aspectCommon;
private final Iterable<JavaProtoLibraryAspectProvider> javaProtoLibraryAspectProviders;
+ private final boolean isJavaProtoExportsEnabled;
+
Impl(
RuleContext ruleContext,
SupportData supportData,
@@ -172,6 +178,22 @@ public class JavaLiteProtoAspect extends NativeAspectClass implements Configured
JavaCompilationArgsProvider.merge(
WrappingProvider.Helper.unwrapProviders(
javaProtoLibraryAspectProviders, JavaCompilationArgsProvider.class));
+
+ this.isJavaProtoExportsEnabled =
+ ruleContext.getFragment(JavaConfiguration.class).isJavaProtoExportsEnabled();
+
+ if (this.isJavaProtoExportsEnabled) {
+ this.exportsCompilationArgs =
+ JavaCompilationArgsProvider.merge(
+ WrappingProvider.Helper.unwrapProviders(
+ ruleContext.getPrerequisites(
+ "exports",
+ RuleConfiguredTarget.Mode.TARGET,
+ JavaProtoLibraryAspectProvider.class),
+ JavaCompilationArgsProvider.class));
+ } else {
+ this.exportsCompilationArgs = null;
+ }
}
void addProviders(ConfiguredAspect.Builder aspect) {
@@ -225,6 +247,12 @@ public class JavaLiteProtoAspect extends NativeAspectClass implements Configured
javaProvidersBuilder.add(JavaRuleOutputJarsProvider.EMPTY);
}
+ if (isJavaProtoExportsEnabled) {
+ generatedCompilationArgsProvider =
+ JavaCompilationArgsProvider.merge(
+ ImmutableList.of(generatedCompilationArgsProvider, exportsCompilationArgs));
+ }
+
javaProvidersBuilder.add(generatedCompilationArgsProvider);
javaProvidersBuilder.add(createCcLinkParamsStore(
ruleContext, aspectCommon.getProtoRuntimeDeps()));
@@ -260,7 +288,9 @@ public class JavaLiteProtoAspect extends NativeAspectClass implements Configured
ruleContext.getLabel(),
ImmutableList.of(sourceJar),
"JavaLite",
- /* allowServices= */ true);
+ /* allowServices= */ true,
+ supportData.getProtosInExports(),
+ supportData.getExportedProtoSourceRoots());
}
}
}
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 a86f703e9b..131c617650 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
@@ -121,6 +121,7 @@ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspe
AspectDefinition.Builder result =
new AspectDefinition.Builder(this)
.propagateAlongAttribute("deps")
+ .propagateAlongAttribute("exports")
.requiresConfigurationFragments(JavaConfiguration.class, ProtoConfiguration.class)
.requireProviders(ProtoSourcesProvider.class)
.advertiseProvider(JavaProtoLibraryAspectProvider.class)
@@ -163,8 +164,13 @@ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspe
*/
private final JavaCompilationArgsProvider dependencyCompilationArgs;
+ // Compilation-args from all exports, merged together.
+ private final JavaCompilationArgsProvider exportsCompilationArgs;
+
private final Iterable<JavaProtoLibraryAspectProvider> javaProtoLibraryAspectProviders;
+ private final boolean isJavaProtoExportsEnabled;
+
Impl(
RuleContext ruleContext,
SupportData supportData,
@@ -178,10 +184,27 @@ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspe
ruleContext.getPrerequisites(
"deps", RuleConfiguredTarget.Mode.TARGET, JavaProtoLibraryAspectProvider.class);
- dependencyCompilationArgs =
+ this.dependencyCompilationArgs =
JavaCompilationArgsProvider.merge(
WrappingProvider.Helper.unwrapProviders(
javaProtoLibraryAspectProviders, JavaCompilationArgsProvider.class));
+
+ this.isJavaProtoExportsEnabled =
+ ruleContext.getFragment(JavaConfiguration.class).isJavaProtoExportsEnabled();
+
+ if (this.isJavaProtoExportsEnabled) {
+ this.exportsCompilationArgs =
+ JavaCompilationArgsProvider.merge(
+ WrappingProvider.Helper.unwrapProviders(
+ ruleContext.getPrerequisites(
+ "exports",
+ RuleConfiguredTarget.Mode.TARGET,
+ JavaProtoLibraryAspectProvider.class),
+ JavaCompilationArgsProvider.class));
+ } else {
+ this.exportsCompilationArgs = null;
+ }
+
}
void addProviders(ConfiguredAspect.Builder aspect) {
@@ -235,6 +258,12 @@ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspe
javaProvidersBuilder.add(JavaRuleOutputJarsProvider.EMPTY);
}
+ if (isJavaProtoExportsEnabled) {
+ generatedCompilationArgsProvider =
+ JavaCompilationArgsProvider.merge(
+ ImmutableList.of(generatedCompilationArgsProvider, exportsCompilationArgs));
+ }
+
javaProvidersBuilder.add(generatedCompilationArgsProvider);
javaProvidersBuilder.add(createCcLinkParamsStore(
ruleContext, aspectCommon.getProtoRuntimeDeps()));
@@ -289,7 +318,9 @@ public class JavaProtoAspect extends NativeAspectClass implements ConfiguredAspe
ruleContext.getLabel(),
ImmutableList.of(sourceJar),
"Java (Immutable)",
- rpcSupport.allowServices(ruleContext));
+ rpcSupport.allowServices(ruleContext),
+ supportData.getProtosInExports(),
+ supportData.getExportedProtoSourceRoots());
}
}
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/proto/BazelProtoLibrary.java b/src/main/java/com/google/devtools/build/lib/rules/proto/BazelProtoLibrary.java
index 000aa5ec53..ac8fcc8a85 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/proto/BazelProtoLibrary.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/proto/BazelProtoLibrary.java
@@ -62,7 +62,9 @@ public class BazelProtoLibrary implements RuleConfiguredTargetFactory {
protoPathFlags,
protoSourceRoot,
directProtoSourceRoots,
- !protoSources.isEmpty());
+ !protoSources.isEmpty(),
+ /* protosInExports= */ null,
+ /* exportedProtoSourceRoots= */ null);
Artifact descriptorSetOutput =
ruleContext.getGenfilesArtifact(
diff --git a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java
index f3cc3d6751..cb9dbb6818 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java
@@ -140,19 +140,19 @@ public class ProtoCommon {
/**
* Returns a set of the {@code proto_source_root} collected from the current library and the
- * direct dependencies.
+ * specified attribute.
*
* <p>Assumes {@code currentProtoSourceRoot} is the same as the package name.
*/
- public static NestedSet<String> getProtoSourceRootsOfDirectDependencies(
- RuleContext ruleContext, String currentProtoSourceRoot) {
+ private static NestedSet<String> getProtoSourceRootsOfAttribute(
+ RuleContext ruleContext, String currentProtoSourceRoot, String attributeName) {
NestedSetBuilder<String> protoSourceRoots = NestedSetBuilder.stableOrder();
if (currentProtoSourceRoot != null && !currentProtoSourceRoot.isEmpty()) {
protoSourceRoots.add(currentProtoSourceRoot);
}
for (ProtoSourcesProvider provider :
- ruleContext.getPrerequisites("deps", Mode.TARGET, ProtoSourcesProvider.class)) {
+ ruleContext.getPrerequisites(attributeName, Mode.TARGET, ProtoSourcesProvider.class)) {
String protoSourceRoot = provider.getProtoSourceRoot();
if (protoSourceRoot != null && !protoSourceRoot.isEmpty()) {
protoSourceRoots.add(provider.getProtoSourceRoot());
@@ -162,6 +162,28 @@ public class ProtoCommon {
return protoSourceRoots.build();
}
+ /**
+ * Returns a set of the {@code proto_source_root} collected from the current library and the
+ * direct dependencies.
+ *
+ * <p>Assumes {@code currentProtoSourceRoot} is the same as the package name.
+ */
+ public static NestedSet<String> getProtoSourceRootsOfDirectDependencies(
+ RuleContext ruleContext, String currentProtoSourceRoot) {
+ return getProtoSourceRootsOfAttribute(ruleContext, currentProtoSourceRoot, "deps");
+ }
+
+ /**
+ * Returns a set of the {@code proto_source_root} collected from the current library and the
+ * exported dependencies.
+ *
+ * <p>Assumes {@code currentProtoSourceRoot} is the same as the package name.
+ */
+ public static NestedSet<String> getProtoSourceRootsOfExportedDependencies(
+ RuleContext ruleContext, String currentProtoSourceRoot) {
+ return getProtoSourceRootsOfAttribute(ruleContext, currentProtoSourceRoot, "exports");
+ }
+
private static void checkProtoSourceRootIsTheSameAsPackage(
String protoSourceRoot, RuleContext ruleContext) {
if (!ruleContext.getLabel().getPackageName().equals(protoSourceRoot)) {
@@ -272,6 +294,19 @@ public class ProtoCommon {
}
/**
+ * Returns the .proto files that are the direct srcs of the exported dependencies of this rule.
+ */
+ @Nullable
+ public static NestedSet<Artifact> computeProtosInExportedDeps(RuleContext ruleContext) {
+ NestedSetBuilder<Artifact> result = NestedSetBuilder.stableOrder();
+ for (ProtoSupportDataProvider provider :
+ ruleContext.getPrerequisites("exports", TARGET, ProtoSupportDataProvider.class)) {
+ result.addTransitive(provider.getSupportData().getProtosInDirectDeps());
+ }
+ return result.build();
+ }
+
+ /**
* Decides whether this proto_library should check for strict proto deps.
*
* <p>Takes into account command-line flags, package-level attributes and rule attributes.
diff --git a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java
index da43b6a2a4..9f2217cd62 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilder.java
@@ -351,8 +351,10 @@ public class ProtoCompileActionBuilder {
protosToCompile,
transitiveSources,
protosInDirectDeps,
+ /* protosInExports= */ null,
protoSourceRoots,
directProtoSourceRoots,
+ /* exportedProtoSourceRoots= */ null,
ruleContext.getLabel(),
ImmutableList.of(output),
"Descriptor Set",
@@ -381,6 +383,34 @@ public class ProtoCompileActionBuilder {
outReplacement);
}
+ public static void registerActions(
+ RuleContext ruleContext,
+ List<ToolchainInvocation> toolchainInvocations,
+ Iterable<Artifact> protosToCompile,
+ NestedSet<Artifact> transitiveSources,
+ NestedSet<Artifact> protosInDirectDeps,
+ NestedSet<String> protoSourceRoots,
+ NestedSet<String> directProtoSourceRoots,
+ Label ruleLabel,
+ Iterable<Artifact> outputs,
+ String flavorName,
+ boolean allowServices) {
+ registerActions(
+ ruleContext,
+ toolchainInvocations,
+ protosToCompile,
+ transitiveSources,
+ protosInDirectDeps,
+ protoSourceRoots,
+ directProtoSourceRoots,
+ ruleLabel,
+ outputs,
+ flavorName,
+ allowServices,
+ /* protosInExports= */ null,
+ /* exportedProtoSourceRoots= */ null);
+ }
+
/**
* Registers actions to generate code from .proto files.
*
@@ -404,7 +434,9 @@ public class ProtoCompileActionBuilder {
Label ruleLabel,
Iterable<Artifact> outputs,
String flavorName,
- boolean allowServices) {
+ boolean allowServices,
+ NestedSet<Artifact> protosInExports,
+ NestedSet<String> exportedProtoSourceRoots) {
SpawnAction.Builder actions =
createActions(
ruleContext,
@@ -412,8 +444,10 @@ public class ProtoCompileActionBuilder {
protosToCompile,
transitiveSources,
protosInDirectDeps,
+ protosInExports,
protoSourceRoots,
directProtoSourceRoots,
+ exportedProtoSourceRoots,
ruleLabel,
outputs,
flavorName,
@@ -430,8 +464,10 @@ public class ProtoCompileActionBuilder {
Iterable<Artifact> protosToCompile,
NestedSet<Artifact> transitiveSources,
@Nullable NestedSet<Artifact> protosInDirectDeps,
+ @Nullable NestedSet<Artifact> protosInExports,
NestedSet<String> protoSourceRoots,
NestedSet<String> directProtoSourceRoots,
+ @Nullable NestedSet<String> exportedProtoSourceRoots,
Label ruleLabel,
Iterable<Artifact> outputs,
String flavorName,
@@ -468,7 +504,9 @@ public class ProtoCompileActionBuilder {
transitiveSources,
protoSourceRoots,
directProtoSourceRoots,
+ exportedProtoSourceRoots,
areDepsStrict(ruleContext) ? protosInDirectDeps : null,
+ arePublicImportsStrict(ruleContext) ? protosInExports : null,
ruleLabel,
allowServices,
ruleContext.getFragment(ProtoConfiguration.class).protocOpts()),
@@ -479,6 +517,10 @@ public class ProtoCompileActionBuilder {
return result;
}
+ public static boolean arePublicImportsStrict(RuleContext ruleContext) {
+ return ruleContext.getFragment(ProtoConfiguration.class).strictPublicImports();
+ }
+
/**
* Constructs command-line arguments to execute proto-compiler.
*
@@ -506,7 +548,9 @@ public class ProtoCompileActionBuilder {
NestedSet<Artifact> transitiveSources,
NestedSet<String> transitiveProtoPathFlags,
NestedSet<String> directProtoSourceRoots,
+ NestedSet<String> exportedProtoSourceRoots,
@Nullable NestedSet<Artifact> protosInDirectDeps,
+ @Nullable NestedSet<Artifact> protosInExports,
Label ruleLabel,
boolean allowServices,
ImmutableList<String> protocOpts) {
@@ -554,6 +598,19 @@ public class ProtoCompileActionBuilder {
cmdLine.addFormatted(STRICT_DEPS_FLAG_TEMPLATE, ruleLabel);
}
+ if (protosInExports != null) {
+ if (protosInExports.isEmpty()) {
+ // This line is necessary to trigger the check.
+ cmdLine.add("--allowed_public_imports=");
+ } else {
+ cmdLine.addAll(
+ "--allowed_public_imports",
+ VectorArg.join(":")
+ .each(protosInExports)
+ .mapped(new ExpandToPathFn(exportedProtoSourceRoots)));
+ }
+ }
+
for (Artifact src : protosToCompile) {
cmdLine.addPath(src.getExecPath());
}
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 3d19e8c7e7..8731315b51 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
@@ -146,6 +146,15 @@ public class ProtoConfiguration extends Fragment implements ProtoConfigurationAp
)
public List<String> ccProtoLibrarySourceSuffixes;
+ @Option(
+ name = "experimental_java_proto_add_allowed_public_imports",
+ defaultValue = "false",
+ documentationCategory = OptionDocumentationCategory.INPUT_STRICTNESS,
+ effectTags = {OptionEffectTag.AFFECTS_OUTPUTS, OptionEffectTag.LOADING_AND_ANALYSIS},
+ metadataTags = {OptionMetadataTag.EXPERIMENTAL},
+ help = "If true, add --allowed_public_imports to the java compile actions.")
+ public boolean experimentalJavaProtoAddAllowedPublicImports;
+
@Override
public FragmentOptions getHost() {
Options host = (Options) super.getHost();
@@ -160,6 +169,8 @@ public class ProtoConfiguration extends Fragment implements ProtoConfigurationAp
host.strictProtoDeps = strictProtoDeps;
host.ccProtoLibraryHeaderSuffixes = ccProtoLibraryHeaderSuffixes;
host.ccProtoLibrarySourceSuffixes = ccProtoLibrarySourceSuffixes;
+ host.experimentalJavaProtoAddAllowedPublicImports =
+ experimentalJavaProtoAddAllowedPublicImports;
return host;
}
}
@@ -242,4 +253,8 @@ public class ProtoConfiguration extends Fragment implements ProtoConfigurationAp
public List<String> ccProtoLibrarySourceSuffixes() {
return ccProtoLibrarySourceSuffixes;
}
+
+ public boolean strictPublicImports() {
+ return options.experimentalJavaProtoAddAllowedPublicImports;
+ }
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/proto/SupportData.java b/src/main/java/com/google/devtools/build/lib/rules/proto/SupportData.java
index b3644d4c17..35c0cee485 100644
--- a/src/main/java/com/google/devtools/build/lib/rules/proto/SupportData.java
+++ b/src/main/java/com/google/devtools/build/lib/rules/proto/SupportData.java
@@ -22,6 +22,7 @@ import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
+import javax.annotation.Nullable;
/** A helper class for the *Support classes containing some data from ProtoLibrary. */
@AutoCodec
@@ -37,7 +38,9 @@ public abstract class SupportData {
NestedSet<String> transitiveProtoPathFlags,
String protoSourceRoot,
NestedSet<String> directProtoSourceRoots,
- boolean hasProtoSources) {
+ boolean hasProtoSources,
+ @Nullable NestedSet<Artifact> protosInExports,
+ @Nullable NestedSet<String> exportedProtoSourceRoots) {
return new AutoValue_SupportData(
nonWeakDepsPredicate,
directProtoSources,
@@ -46,7 +49,9 @@ public abstract class SupportData {
transitiveProtoPathFlags,
protoSourceRoot,
directProtoSourceRoots,
- hasProtoSources);
+ hasProtoSources,
+ protosInExports,
+ exportedProtoSourceRoots);
}
public abstract Predicate<TransitiveInfoCollection> getNonWeakDepsPredicate();
@@ -76,5 +81,14 @@ public abstract class SupportData {
public abstract boolean hasProtoSources();
+ /** .proto files in the exported dependencies of this proto_library. */
+ public abstract @Nullable NestedSet<Artifact> getProtosInExports();
+
+ /**
+ * The {@code proto_source_root}'s collected from the current library and the exported
+ * dependencies.
+ */
+ public abstract @Nullable NestedSet<String> getExportedProtoSourceRoots();
+
SupportData() {}
}
diff --git a/src/test/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilderTest.java b/src/test/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilderTest.java
index 2044f279ad..e992960a72 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilderTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilderTest.java
@@ -55,7 +55,7 @@ public class ProtoCompileActionBuilderTest {
public void commandLine_basic() throws Exception {
FilesToRunProvider plugin =
new FilesToRunProvider(
- NestedSetBuilder.<Artifact>emptySet(Order.STABLE_ORDER),
+ NestedSetBuilder.emptySet(Order.STABLE_ORDER),
null /* runfilesSupport */,
artifact("//:dont-care", "protoc-gen-javalite.exe"));
@@ -64,28 +64,30 @@ public class ProtoCompileActionBuilderTest {
"--java_out=param1,param2:$(OUT)",
/* pluginExecutable= */ null,
/* runtime= */ mock(TransitiveInfoCollection.class),
- /* blacklistedProtos= */ NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER));
+ /* blacklistedProtos= */ NestedSetBuilder.emptySet(STABLE_ORDER));
ProtoLangToolchainProvider toolchainWithPlugin =
ProtoLangToolchainProvider.create(
"--$(PLUGIN_OUT)=param3,param4:$(OUT)",
plugin,
/* runtime= */ mock(TransitiveInfoCollection.class),
- /* blacklistedProtos= */ NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER));
+ /* blacklistedProtos= */ NestedSetBuilder.emptySet(STABLE_ORDER));
SupportData supportData =
SupportData.create(
- Predicates.<TransitiveInfoCollection>alwaysFalse(),
+ Predicates.alwaysFalse(),
ImmutableList.of(artifact("//:dont-care", "source_file.proto")),
- /* protosInDirectDeps= */ NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER),
+ /* protosInDirectDeps= */ NestedSetBuilder.emptySet(STABLE_ORDER),
NestedSetBuilder.create(
STABLE_ORDER,
artifact("//:dont-care", "import1.proto"),
artifact("//:dont-care", "import2.proto")),
- /*transitiveProtoPathFlags=*/ NestedSetBuilder.<String>emptySet(STABLE_ORDER),
- /*protoSourceRoot=*/ "",
- /*directProtoSourceRoots=*/ NestedSetBuilder.<String>stableOrder().build(),
- /* hasProtoSources= */ true);
+ /* transitiveProtoPathFlags= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* protoSourceRoot= */ "",
+ /* directProtoSourceRoots= */ NestedSetBuilder.<String>stableOrder().build(),
+ /* hasProtoSources= */ true,
+ /* protosInExports= */ null,
+ /* exportedProtoSourceRoots= */ null);
CustomCommandLine cmdLine =
createCommandLineFromToolchains(
@@ -95,12 +97,14 @@ public class ProtoCompileActionBuilderTest {
new ToolchainInvocation("pluginName", toolchainWithPlugin, "bar.srcjar")),
supportData.getDirectProtoSources(),
supportData.getTransitiveImports(),
- /*transitiveProtoPathFlags=*/ NestedSetBuilder.<String>stableOrder().build(),
- /*directProtoSourceRoots=*/ NestedSetBuilder.<String>stableOrder().build(),
- null /* protosInDirectDeps */,
+ /* transitiveProtoPathFlags= */ NestedSetBuilder.<String>stableOrder().build(),
+ /* directProtoSourceRoots= */ NestedSetBuilder.<String>stableOrder().build(),
+ /* exportedProtoSourceRoots= */ null,
+ /* protosInDirectDeps= */ null,
+ /* protosInExports= */ null,
Label.parseAbsoluteUnchecked("//foo:bar"),
- true /* allowServices */,
- ImmutableList.<String>of() /* protocOpts */);
+ /* allowServices= */ true,
+ /* protocOpts= */ ImmutableList.of());
assertThat(cmdLine.arguments())
.containsExactly(
@@ -118,26 +122,30 @@ public class ProtoCompileActionBuilderTest {
// Verify that the command line contains the correct path to a generated protocol buffers.
SupportData supportData =
SupportData.create(
- Predicates.<TransitiveInfoCollection>alwaysFalse(),
+ Predicates.alwaysFalse(),
ImmutableList.of(derivedArtifact("//:dont-care", "source_file.proto")),
- /* protosInDirectDeps= */ NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER),
- /* transitiveImports= */ NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER),
- /*transitiveProtoPathFlags=*/ NestedSetBuilder.<String>emptySet(STABLE_ORDER),
- /*protoSourceRoot=*/ "",
- /*directProtoSourceRoots=*/ NestedSetBuilder.<String>stableOrder().build(),
- /* hasProtoSources= */ true);
+ /* protosInDirectDeps= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* transitiveImports= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* transitiveProtoPathFlags= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* protoSourceRoot= */ "",
+ /* directProtoSourceRoots= */ NestedSetBuilder.<String>stableOrder().build(),
+ /* hasProtoSources= */ true,
+ /* protosInExports= */ null,
+ /* exportedProtoSourceRoots= */ null);
CustomCommandLine cmdLine =
createCommandLineFromToolchains(
- ImmutableList.<ToolchainInvocation>of() /* toolchainInvocations */,
+ /* toolchainInvocations= */ ImmutableList.of(),
supportData.getDirectProtoSources(),
supportData.getTransitiveImports(),
- /*transitiveProtoPathFlags=*/ NestedSetBuilder.<String>emptySet(STABLE_ORDER),
- /*directProtoSourceRoots=*/ NestedSetBuilder.<String>emptySet(STABLE_ORDER),
- null /* protosInDirectDeps */,
+ /* transitiveProtoPathFlags= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* directProtoSourceRoots= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* exportedProtoSourceRoots= */ null,
+ /* protosInDirectDeps= */ null,
+ /* protosInExports= */ null,
Label.parseAbsoluteUnchecked("//foo:bar"),
- true /* allowServices */,
- ImmutableList.<String>of() /* protocOpts */);
+ /* allowServices= */ true,
+ /* protocOpts= */ ImmutableList.of());
assertThat(cmdLine.arguments()).containsExactly("out/source_file.proto");
}
@@ -149,33 +157,37 @@ public class ProtoCompileActionBuilderTest {
"--java_out=param1,param2:$(OUT)",
/* pluginExecutable= */ null,
/* runtime= */ mock(TransitiveInfoCollection.class),
- /* blacklistedProtos= */ NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER));
+ /* blacklistedProtos= */ NestedSetBuilder.emptySet(STABLE_ORDER));
SupportData supportData =
SupportData.create(
- Predicates.<TransitiveInfoCollection>alwaysFalse(),
+ Predicates.alwaysFalse(),
ImmutableList.of(artifact("//:dont-care", "source_file.proto")),
NestedSetBuilder.create(STABLE_ORDER, artifact("//:dont-care", "import1.proto")),
NestedSetBuilder.create(
STABLE_ORDER,
artifact("//:dont-care", "import1.proto"),
artifact("//:dont-care", "import2.proto")),
- /*transitiveProtoPathFlags=*/ NestedSetBuilder.<String>emptySet(STABLE_ORDER),
- /*protoSourceRoot=*/ "",
- /*directProtoSourceRoots=*/ NestedSetBuilder.<String>stableOrder().build(),
- /* hasProtoSources= */ true);
+ /* transitiveProtoPathFlags= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* protoSourceRoot= */ "",
+ /* directProtoSourceRoots= */ NestedSetBuilder.<String>stableOrder().build(),
+ /* hasProtoSources= */ true,
+ /* protosInExports= */ null,
+ /* exportedProtoSourceRoots= */ null);
CustomCommandLine cmdLine =
createCommandLineFromToolchains(
ImmutableList.of(new ToolchainInvocation("dontcare", toolchain, "foo.srcjar")),
supportData.getDirectProtoSources(),
supportData.getTransitiveImports(),
- /*transitiveProtoPathFlags=*/ NestedSetBuilder.<String>emptySet(STABLE_ORDER),
- /*directProtoSourceRoots=*/ NestedSetBuilder.<String>emptySet(STABLE_ORDER),
+ /* transitiveProtoPathFlags= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* directProtoSourceRoots= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* exportedProtoSourceRoots= */ null,
supportData.getProtosInDirectDeps(),
+ /* protosInExports= */ null,
Label.parseAbsoluteUnchecked("//foo:bar"),
- true /* allowServices */,
- ImmutableList.<String>of() /* protocOpts */);
+ /* allowServices= */ true,
+ /* protocOpts= */ ImmutableList.of());
assertThat(cmdLine.arguments())
.containsExactly(
@@ -193,26 +205,30 @@ public class ProtoCompileActionBuilderTest {
public void otherParameters() throws Exception {
SupportData supportData =
SupportData.create(
- Predicates.<TransitiveInfoCollection>alwaysFalse(),
- ImmutableList.<Artifact>of(),
- /* protosInDirectDeps= */ NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER),
- NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER),
- /*transitiveProtoPathFlags=*/ NestedSetBuilder.<String>emptySet(STABLE_ORDER),
- /*protoSourceRoot=*/ "",
- /*directProtoSourceRoots=*/ NestedSetBuilder.<String>stableOrder().build(),
- /* hasProtoSources= */ true);
+ Predicates.alwaysFalse(),
+ ImmutableList.of(),
+ /* protosInDirectDeps= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* transitiveProtoPathFlags= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* protoSourceRoot= */ "",
+ /* directProtoSourceRoots= */ NestedSetBuilder.<String>stableOrder().build(),
+ /* hasProtoSources= */ true,
+ /* protosInExports= */ null,
+ /* exportedProtoSourceRoots= */ null);
CustomCommandLine cmdLine =
createCommandLineFromToolchains(
- ImmutableList.<ToolchainInvocation>of(),
+ ImmutableList.of(),
supportData.getDirectProtoSources(),
supportData.getTransitiveImports(),
- /*transitiveProtoPathFlags=*/ NestedSetBuilder.<String>emptySet(STABLE_ORDER),
- /*directProtoSourceRoots=*/ NestedSetBuilder.<String>emptySet(STABLE_ORDER),
+ /* transitiveProtoPathFlags= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* directProtoSourceRoots= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* exportedProtoSourceRoots= */ null,
supportData.getProtosInDirectDeps(),
+ /* protosInExports= */ null,
Label.parseAbsoluteUnchecked("//foo:bar"),
- false /* allowServices */,
- ImmutableList.of("--foo", "--bar") /* protocOpts */);
+ /* allowServices= */ false,
+ /* protocOpts= */ ImmutableList.of("--foo", "--bar"));
assertThat(cmdLine.arguments()).containsAllOf("--disallow_services", "--foo", "--bar");
}
@@ -236,30 +252,34 @@ public class ProtoCompileActionBuilderTest {
"--java_out=param1,param2:$(OUT)",
/* pluginExecutable= */ null,
/* runtime= */ mock(TransitiveInfoCollection.class),
- /* blacklistedProtos= */ NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER));
+ /* blacklistedProtos= */ NestedSetBuilder.emptySet(STABLE_ORDER));
SupportData supportData =
SupportData.create(
- Predicates.<TransitiveInfoCollection>alwaysFalse(),
- ImmutableList.<Artifact>of(),
- /* protosInDirectDeps= */ NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER),
- NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER),
- /*transitiveProtoPathFlags=*/ NestedSetBuilder.<String>emptySet(STABLE_ORDER),
- /*protoSourceRoot=*/ "",
- /*directProtoSourceRoots=*/ NestedSetBuilder.<String>stableOrder().build(),
- /* hasProtoSources= */ true);
+ Predicates.alwaysFalse(),
+ ImmutableList.of(),
+ /* protosInDirectDeps= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* transitiveProtoPathFlags= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* protoSourceRoot= */ "",
+ /* directProtoSourceRoots= */ NestedSetBuilder.<String>stableOrder().build(),
+ /* hasProtoSources= */ true,
+ /* protosInExports= */ null,
+ /* exportedProtoSourceRoots= */ null);
CustomCommandLine cmdLine =
createCommandLineFromToolchains(
ImmutableList.of(new ToolchainInvocation("pluginName", toolchain, outReplacement)),
supportData.getDirectProtoSources(),
supportData.getTransitiveImports(),
- /*transitiveProtoPathFlags=*/ NestedSetBuilder.<String>emptySet(STABLE_ORDER),
- /*directProtoSourceRoots=*/ NestedSetBuilder.<String>emptySet(STABLE_ORDER),
+ /* transitiveProtoPathFlags= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* directProtoSourceRoots= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* exportedProtoSourceRoots= */ null,
supportData.getProtosInDirectDeps(),
+ /* protosInExports= */ null,
Label.parseAbsoluteUnchecked("//foo:bar"),
- true /* allowServices */,
- ImmutableList.<String>of() /* protocOpts */);
+ /* allowServices= */ true,
+ /* protocOpts= */ ImmutableList.of());
assertThat(hasBeenCalled[0]).isFalse();
cmdLine.arguments();
@@ -274,28 +294,30 @@ public class ProtoCompileActionBuilderTest {
public void exceptionIfSameName() throws Exception {
SupportData supportData =
SupportData.create(
- Predicates.<TransitiveInfoCollection>alwaysFalse(),
- ImmutableList.<Artifact>of(),
- /* protosInDirectDeps= */ NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER),
- NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER),
- /*transitiveProtoPathFlags=*/ NestedSetBuilder.<String>emptySet(STABLE_ORDER),
- /*protoSourceRoot=*/ "",
- /*directProtoSourceRoots=*/ NestedSetBuilder.<String>stableOrder().build(),
- /* hasProtoSources= */ true);
+ Predicates.alwaysFalse(),
+ ImmutableList.of(),
+ /* protosInDirectDeps= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* transitiveProtoPathFlags= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* protoSourceRoot= */ "",
+ /* directProtoSourceRoots= */ NestedSetBuilder.<String>stableOrder().build(),
+ /* hasProtoSources= */ true,
+ /* protosInExports= */ null,
+ /* exportedProtoSourceRoots= */ null);
ProtoLangToolchainProvider toolchain1 =
ProtoLangToolchainProvider.create(
"dontcare",
/* pluginExecutable= */ null,
/* runtime= */ mock(TransitiveInfoCollection.class),
- /* blacklistedProtos= */ NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER));
+ /* blacklistedProtos= */ NestedSetBuilder.emptySet(STABLE_ORDER));
ProtoLangToolchainProvider toolchain2 =
ProtoLangToolchainProvider.create(
"dontcare",
/* pluginExecutable= */ null,
/* runtime= */ mock(TransitiveInfoCollection.class),
- /* blacklistedProtos= */ NestedSetBuilder.<Artifact>emptySet(STABLE_ORDER));
+ /* blacklistedProtos= */ NestedSetBuilder.emptySet(STABLE_ORDER));
try {
createCommandLineFromToolchains(
@@ -304,12 +326,14 @@ public class ProtoCompileActionBuilderTest {
new ToolchainInvocation("pluginName", toolchain2, "outReplacement")),
supportData.getDirectProtoSources(),
supportData.getTransitiveImports(),
- /*transitiveProtoPathFlags=*/ NestedSetBuilder.<String>emptySet(STABLE_ORDER),
- /*directProtoSourceRoots=*/ NestedSetBuilder.<String>emptySet(STABLE_ORDER),
+ /* transitiveProtoPathFlags= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* directProtoSourceRoots= */ NestedSetBuilder.emptySet(STABLE_ORDER),
+ /* exportedProtoSourceRoots= */ null,
supportData.getProtosInDirectDeps(),
+ /* protosInExports= */ null,
Label.parseAbsoluteUnchecked("//foo:bar"),
- true /* allowServices */,
- ImmutableList.<String>of() /* protocOpts */);
+ /* allowServices= */ true,
+ /* protocOpts= */ ImmutableList.of());
fail("Expected an exception");
} catch (IllegalStateException e) {
assertThat(e)