Remove unused code from ProtoCompileActionBuilder.
PiperOrigin-RevId: 440108556
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 d8ffec5..0b029d3 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
@@ -14,356 +14,12 @@
package com.google.devtools.build.lib.rules.proto;
-import static com.google.common.collect.Iterables.isEmpty;
-
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableMap;
-import com.google.devtools.build.lib.actions.Artifact;
-import com.google.devtools.build.lib.actions.ResourceSet;
-import com.google.devtools.build.lib.actions.ResourceSetOrBuilder;
-import com.google.devtools.build.lib.analysis.ConfiguredTarget;
-import com.google.devtools.build.lib.analysis.FilesToRunProvider;
-import com.google.devtools.build.lib.analysis.RuleContext;
-import com.google.devtools.build.lib.analysis.starlark.Args;
-import com.google.devtools.build.lib.collect.nestedset.Depset;
-import com.google.devtools.build.lib.collect.nestedset.Depset.ElementType;
-import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
-import com.google.devtools.build.lib.collect.nestedset.Order;
-import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
-import com.google.devtools.build.lib.util.OS;
-import java.util.HashSet;
-import java.util.List;
-import net.starlark.java.eval.Dict;
-import net.starlark.java.eval.EvalException;
-import net.starlark.java.eval.Starlark;
-import net.starlark.java.eval.StarlarkCallable;
-import net.starlark.java.eval.StarlarkFloat;
-import net.starlark.java.eval.StarlarkFunction;
-import net.starlark.java.eval.StarlarkInt;
-import net.starlark.java.eval.StarlarkList;
-import net.starlark.java.eval.StarlarkThread;
-import net.starlark.java.eval.Tuple;
/** Constructs actions to run the protocol compiler to generate sources from .proto files. */
public class ProtoCompileActionBuilder {
- private static final String DEFAULT_MNEMONIC = "GenProto";
@VisibleForTesting
public static final String STRICT_DEPS_FLAG_TEMPLATE =
"--direct_dependencies_violation_msg=" + ProtoConstants.STRICT_PROTO_DEPS_VIOLATION_MESSAGE;
-
- private final ProtoInfo protoInfo;
- private final FilesToRunProvider protoCompiler;
- private final String progressMessage;
- private final Iterable<Artifact> outputs;
- private Iterable<Artifact> inputs;
- private FilesToRunProvider langPlugin;
- private String langPluginFormat;
- private Iterable<String> langPluginParameter;
- private String langPluginParameterFormat;
- private boolean hasServices;
- private Iterable<String> additionalCommandLineArguments;
- private Iterable<FilesToRunProvider> additionalTools;
- private String mnemonic;
-
- public ProtoCompileActionBuilder allowServices(boolean hasServices) {
- this.hasServices = hasServices;
- return this;
- }
-
- public ProtoCompileActionBuilder setInputs(Iterable<Artifact> inputs) {
- this.inputs = inputs;
- return this;
- }
-
- public ProtoCompileActionBuilder setLangPlugin(
- FilesToRunProvider langPlugin, String langPluginFormat) {
- this.langPlugin = langPlugin;
- this.langPluginFormat = langPluginFormat;
- return this;
- }
-
- public ProtoCompileActionBuilder setMnemonic(String mnemonic) {
- this.mnemonic = mnemonic;
- return this;
- }
-
- public ProtoCompileActionBuilder setLangPluginParameter(
- Iterable<String> langPluginParameter, String langPluginParameterFormat) {
- this.langPluginParameter = langPluginParameter;
- this.langPluginParameterFormat = langPluginParameterFormat;
- return this;
- }
-
- public ProtoCompileActionBuilder setAdditionalCommandLineArguments(
- Iterable<String> additionalCmdLine) {
- this.additionalCommandLineArguments = additionalCmdLine;
- return this;
- }
-
- public ProtoCompileActionBuilder setAdditionalTools(
- Iterable<FilesToRunProvider> additionalTools) {
- this.additionalTools = additionalTools;
- return this;
- }
-
- public ProtoCompileActionBuilder(
- ConfiguredTarget protoTarget,
- FilesToRunProvider protoCompiler,
- String progressMessage,
- Iterable<Artifact> outputs) {
- this.protoInfo = protoTarget.get(ProtoInfo.PROVIDER);
- this.protoCompiler = protoCompiler;
- this.progressMessage = progressMessage;
- this.outputs = outputs;
- this.mnemonic = DEFAULT_MNEMONIC;
- }
-
- /** Builds a ResourceSet based on the number of inputs. */
- public static class ProtoCompileResourceSetBuilder implements ResourceSetOrBuilder {
- @Override
- public ResourceSet buildResourceSet(OS os, int inputsSize) {
- return ResourceSet.createWithRamCpu(
- /* memoryMb= */ 25 + 0.15 * inputsSize, /* cpuUsage= */ 1);
- }
- }
-
- public void maybeRegister(RuleContext ruleContext)
- throws RuleErrorException, InterruptedException {
- if (isEmpty(outputs)) {
- return;
- }
-
- ruleContext.initStarlarkRuleContext();
- StarlarkThread thread = ruleContext.getStarlarkThread();
- Args additionalArgs = Args.newArgs(thread.mutability(), thread.getSemantics());
-
- try {
- if (langPlugin != null && langPlugin.getExecutable() != null) {
- // We pass a separate langPlugin as there are plugins that cannot be overridden
- // and thus we have to deal with "$xx_plugin" and "xx_plugin".
- additionalArgs.addArgument(
- langPlugin.getExecutable(), /*value=*/ Starlark.UNBOUND, langPluginFormat, thread);
- }
-
- if (langPluginParameter != null) {
- additionalArgs.addJoined(
- StarlarkList.immutableCopyOf(langPluginParameter),
- /*values=*/ Starlark.UNBOUND,
- /*joinWith=*/ "",
- /*mapEach=*/ Starlark.NONE,
- /*formatEach=*/ Starlark.NONE,
- /*formatJoined=*/ langPluginParameterFormat,
- /*omitIfEmpty=*/ true,
- /*uniquify=*/ false,
- /*expandDirectories=*/ true,
- /*allowClosure=*/ false,
- thread);
- }
-
- if (!hasServices) {
- additionalArgs.addArgument(
- "--disallow_services",
- /* value = */ Starlark.UNBOUND,
- /* format = */ Starlark.NONE,
- thread);
- }
-
- if (additionalCommandLineArguments != null) {
- additionalArgs.addAll(
- StarlarkList.immutableCopyOf(additionalCommandLineArguments),
- /*values=*/ Starlark.UNBOUND,
- /*mapEach=*/ Starlark.NONE,
- /*formatEach=*/ Starlark.NONE,
- /*beforeEach=*/ Starlark.NONE,
- /*omitIfEmpty=*/ true,
- /*uniquify=*/ false,
- /*expandDirectories=*/ true,
- /*terminateWith=*/ Starlark.NONE,
- /*allowClosure=*/ false,
- thread);
- }
- } catch (EvalException e) {
- throw ruleContext.throwWithRuleError(e);
- }
-
- ImmutableList.Builder<FilesToRunProvider> plugins = new ImmutableList.Builder<>();
- if (additionalTools != null) {
- plugins.addAll(additionalTools);
- }
- if (langPlugin != null) {
- plugins.add(langPlugin);
- }
-
- StarlarkFunction createProtoCompileAction =
- (StarlarkFunction) ruleContext.getStarlarkDefinedBuiltin("create_proto_compile_action");
-
- ruleContext.callStarlarkOrThrowRuleError(
- createProtoCompileAction,
- ImmutableList.of(
- /* ctx */ ruleContext.getStarlarkRuleContext(),
- /* proto_info */ protoInfo,
- /* proto_compiler */ protoCompiler,
- /* progress_message */ progressMessage,
- /* outputs */ StarlarkList.immutableCopyOf(outputs),
- /* additional_args */ additionalArgs,
- /* plugins */ StarlarkList.immutableCopyOf(plugins.build()),
- /* mnemonic */ mnemonic,
- /* additional_inputs */ inputs == null
- ? Depset.of(ElementType.EMPTY, NestedSetBuilder.emptySet(Order.STABLE_ORDER))
- : Depset.of(Artifact.TYPE, NestedSetBuilder.wrap(Order.STABLE_ORDER, inputs)),
- /* resource_set */
- new StarlarkCallable() {
- @Override
- public String getName() {
- return "proto_compile_resource_set";
- }
-
- @Override
- public Object call(StarlarkThread thread, Tuple args, Dict<String, Object> kwargs) {
- // args are a tuple of OS and inputsSize
- int inputsSize = ((StarlarkInt) args.get(1)).toIntUnchecked();
- return Dict.immutableCopyOf(
- ImmutableMap.of(
- "memory",
- StarlarkFloat.of(25 + 0.15 * inputsSize),
- "cpu",
- StarlarkInt.of(1)));
- }
- }),
- ImmutableMap.of());
- }
-
- /** Whether to allow services in the proto compiler invocation. */
- public enum Services {
- ALLOW,
- DISALLOW,
- }
-
- /**
- * Registers actions to generate code from .proto files.
- *
- * <p>This method uses information from proto_lang_toolchain() rules. New rules should use this
- * method instead of the soup of methods above.
- *
- * @param outputs The artifacts that the resulting action must create.
- * @param progressMessage Please use "Generating {flavorName} proto_library %{label}".
- * @param allowServices If false, the compilation will break if any .proto file has service
- */
- public static void registerActions(
- RuleContext ruleContext,
- List<ToolchainInvocation> toolchainInvocations,
- ConfiguredTarget protoTarget,
- Iterable<Artifact> outputs,
- String progressMessage,
- Services allowServices)
- throws RuleErrorException, InterruptedException {
- if (isEmpty(outputs)) {
- return;
- }
-
- ProtoToolchainInfo protoToolchain = ProtoToolchainInfo.fromRuleContext(ruleContext);
- if (protoToolchain == null) {
- return;
- }
-
- ruleContext.initStarlarkRuleContext();
- StarlarkThread thread = ruleContext.getStarlarkThread();
- Args additionalArgs = Args.newArgs(thread.mutability(), thread.getSemantics());
-
- // A set to check if there are multiple invocations with the same name.
- HashSet<String> invocationNames = new HashSet<>();
- ImmutableList.Builder<Object> plugins = ImmutableList.builder();
-
- try {
- for (ToolchainInvocation invocation : toolchainInvocations) {
- if (!invocationNames.add(invocation.name)) {
- throw new IllegalStateException(
- "Invocation name "
- + invocation.name
- + " appears more than once. "
- + "This could lead to incorrect proto-compiler behavior");
- }
-
- ProtoLangToolchainProvider toolchain = invocation.toolchain;
-
- String format = toolchain.outReplacementFormatFlag();
- additionalArgs.addArgument(
- invocation.outReplacement, /*value=*/ Starlark.UNBOUND, format, thread);
-
- if (toolchain.pluginExecutable() != null) {
- additionalArgs.addArgument(
- toolchain.pluginExecutable().getExecutable(),
- /*value=*/ Starlark.UNBOUND,
- toolchain.pluginFormatFlag(),
- thread);
- plugins.add(toolchain.pluginExecutable());
- }
-
- additionalArgs.addJoined(
- StarlarkList.immutableCopyOf(invocation.protocOpts),
- /*values=*/ Starlark.UNBOUND,
- /*joinWith=*/ "",
- /*mapEach=*/ Starlark.NONE,
- /*formatEach=*/ Starlark.NONE,
- /*formatJoined=*/ Starlark.NONE,
- /*omitIfEmpty=*/ true,
- /*uniquify=*/ false,
- /*expandDirectories=*/ true,
- /*allowClosure=*/ false,
- thread);
- }
-
- if (allowServices == Services.DISALLOW) {
- additionalArgs.addArgument(
- "--disallow_services", /*value=*/ Starlark.UNBOUND, /*format=*/ Starlark.NONE, thread);
- }
- } catch (EvalException e) {
- throw ruleContext.throwWithRuleError(e.getMessageWithStack());
- }
-
- StarlarkFunction createProtoCompileAction =
- (StarlarkFunction) ruleContext.getStarlarkDefinedBuiltin("create_proto_compile_action");
- ruleContext.callStarlarkOrThrowRuleError(
- createProtoCompileAction,
- ImmutableList.of(
- /* ctx */ ruleContext.getStarlarkRuleContext(),
- /* proto_info */ protoTarget.get(ProtoInfo.PROVIDER),
- /* proto_compiler */ protoToolchain.getCompiler(),
- /* progress_message */ progressMessage,
- /* outputs */ StarlarkList.immutableCopyOf(outputs),
- /* additional_args */ additionalArgs,
- /* plugins */ StarlarkList.immutableCopyOf(plugins.build())),
- ImmutableMap.of());
- }
-
- /**
- * Describes a toolchain and the value to replace for a $(OUT) that might appear in its
- * commandLine() (e.g., "bazel-out/foo.srcjar").
- */
- public static class ToolchainInvocation {
- final String name;
- public final ProtoLangToolchainProvider toolchain;
- final CharSequence outReplacement;
- final ImmutableList<String> protocOpts;
-
- public ToolchainInvocation(
- String name, ProtoLangToolchainProvider toolchain, CharSequence outReplacement) {
- this(name, toolchain, outReplacement, ImmutableList.of());
- }
-
- public ToolchainInvocation(
- String name,
- ProtoLangToolchainProvider toolchain,
- CharSequence outReplacement,
- ImmutableList<String> protocOpts) {
- Preconditions.checkState(!name.contains(" "), "Name %s should not contain spaces", name);
- this.name = name;
- this.toolchain = toolchain;
- this.outReplacement = outReplacement;
- this.protocOpts = Preconditions.checkNotNull(protocOpts);
- }
- }
}
diff --git a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoToolchainInfo.java b/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoToolchainInfo.java
deleted file mode 100644
index 45c3e07..0000000
--- a/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoToolchainInfo.java
+++ /dev/null
@@ -1,60 +0,0 @@
-// Copyright 2021 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.rules.proto;
-
-import com.google.auto.value.AutoValue;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
-import com.google.devtools.build.lib.analysis.FilesToRunProvider;
-import com.google.devtools.build.lib.analysis.RuleContext;
-import com.google.devtools.build.lib.packages.BuiltinProvider;
-import com.google.devtools.build.lib.packages.NativeInfo;
-import com.google.devtools.build.lib.starlarkbuildapi.proto.ProtoBootstrap;
-import javax.annotation.Nullable;
-
-/** Toolchain for {@code proto_*} rules. */
-@AutoValue
-public abstract class ProtoToolchainInfo extends NativeInfo {
- public static final ProtoToolchainInfoProvider PROVIDER = new ProtoToolchainInfoProvider();
-
- /** Creates a {@link ProtoToolchainInfo} from a {@link RuleContext}. */
- @Nullable
- public static ProtoToolchainInfo fromRuleContext(RuleContext ruleContext) {
- Preconditions.checkNotNull(ruleContext);
-
- FilesToRunProvider compiler = ruleContext.getExecutablePrerequisite(":proto_compiler");
- if (compiler == null) {
- return null;
- }
-
- ProtoConfiguration protoConfiguration = ruleContext.getFragment(ProtoConfiguration.class);
- if (protoConfiguration == null) {
- return null;
- }
-
- return new AutoValue_ProtoToolchainInfo(PROVIDER, compiler, protoConfiguration.protocOpts());
- }
-
- public abstract FilesToRunProvider getCompiler();
-
- public abstract ImmutableList<String> getCompilerOptions();
-
- /** Provider class for {@link ProtoToolchainInfo} objects. */
- public static class ProtoToolchainInfoProvider extends BuiltinProvider<ProtoToolchainInfo> {
- public ProtoToolchainInfoProvider() {
- super(ProtoBootstrap.PROTO_TOOLCHAIN_INFO_STARLARK_NAME, ProtoToolchainInfo.class);
- }
- }
-}
diff --git a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/proto/ProtoBootstrap.java b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/proto/ProtoBootstrap.java
index 3cc888d..9acc4b2 100644
--- a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/proto/ProtoBootstrap.java
+++ b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/proto/ProtoBootstrap.java
@@ -28,9 +28,6 @@
/** The name of the proto info provider in Starlark. */
public static final String PROTO_INFO_STARLARK_NAME = "ProtoInfo";
- /** The name of the proto toolchain info provider in Starlark. */
- public static final String PROTO_TOOLCHAIN_INFO_STARLARK_NAME = "ProtoToolchainInfo";
-
/** The name of the proto namespace in Starlark. */
public static final String PROTO_COMMON_NAME = "proto_common";
diff --git a/src/main/starlark/builtins_bzl/common/exports.bzl b/src/main/starlark/builtins_bzl/common/exports.bzl
index d96c127..c48c512 100755
--- a/src/main/starlark/builtins_bzl/common/exports.bzl
+++ b/src/main/starlark/builtins_bzl/common/exports.bzl
@@ -23,7 +23,7 @@
load("@_builtins//:common/objc/apple_static_library.bzl", "apple_static_library")
load("@_builtins//:common/objc/compilation_support.bzl", "compilation_support")
load("@_builtins//:common/objc/linking_support.bzl", "linking_support")
-load("@_builtins//:common/proto/proto_common.bzl", "proto_common", "proto_common_do_not_use")
+load("@_builtins//:common/proto/proto_common.bzl", "proto_common_do_not_use")
load("@_builtins//:common/proto/proto_library.bzl", "proto_library")
load("@_builtins//:common/java/proto/java_lite_proto_library.bzl", "java_lite_proto_library")
load("@_builtins//:common/cc/cc_library.bzl", "cc_library")
@@ -59,7 +59,6 @@
# A list of Starlark functions callable from native rules implementation.
exported_to_java = {
"register_compile_and_archive_actions_for_j2objc": compilation_support.register_compile_and_archive_actions_for_j2objc,
- "create_proto_compile_action": proto_common.create_proto_compile_action,
"proto_common_compile": proto_common_do_not_use.compile,
"link_multi_arch_static_library": linking_support.link_multi_arch_static_library,
}
diff --git a/src/test/java/com/google/devtools/build/lib/rules/proto/BUILD b/src/test/java/com/google/devtools/build/lib/rules/proto/BUILD
index d43e132..b831767 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/proto/BUILD
+++ b/src/test/java/com/google/devtools/build/lib/rules/proto/BUILD
@@ -31,36 +31,6 @@
)
java_test(
- name = "ProtoCompileActionBuilderTest",
- srcs = ["ProtoCompileActionBuilderTest.java"],
- deps = [
- "//src/main/java/com/google/devtools/build/lib/actions:artifacts",
- "//src/main/java/com/google/devtools/build/lib/actions:localhost_capacity",
- "//src/main/java/com/google/devtools/build/lib/analysis:analysis_cluster",
- "//src/main/java/com/google/devtools/build/lib/analysis:configured_target",
- "//src/main/java/com/google/devtools/build/lib/analysis:transitive_info_collection",
- "//src/main/java/com/google/devtools/build/lib/cmdline",
- "//src/main/java/com/google/devtools/build/lib/collect/nestedset",
- "//src/main/java/com/google/devtools/build/lib/packages",
- "//src/main/java/com/google/devtools/build/lib/rules/proto",
- "//src/main/java/com/google/devtools/build/lib/util",
- "//src/main/java/com/google/devtools/build/lib/util:os",
- "//src/main/java/com/google/devtools/build/lib/vfs",
- "//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
- "//src/main/java/com/google/devtools/build/lib/vfs/inmemoryfs",
- "//src/main/java/net/starlark/java/eval",
- "//src/test/java/com/google/devtools/build/lib/actions/util",
- "//src/test/java/com/google/devtools/build/lib/analysis/util",
- "//src/test/java/com/google/devtools/build/lib/packages:testutil",
- "//third_party:guava",
- "//third_party:jsr305",
- "//third_party:junit4",
- "//third_party:mockito",
- "//third_party:truth",
- ],
-)
-
-java_test(
name = "ProtoLangToolchainTest",
srcs = ["ProtoLangToolchainTest.java"],
deps = [
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
deleted file mode 100644
index 07c1d06..0000000
--- a/src/test/java/com/google/devtools/build/lib/rules/proto/ProtoCompileActionBuilderTest.java
+++ /dev/null
@@ -1,536 +0,0 @@
-// 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.rules.proto;
-
-import static com.google.common.truth.Truth.assertThat;
-import static com.google.devtools.build.lib.rules.proto.ProtoCompileActionBuilder.registerActions;
-import static org.junit.Assert.assertThrows;
-import static org.mockito.Mockito.mock;
-
-import com.google.common.collect.ImmutableList;
-import com.google.devtools.build.lib.actions.Artifact;
-import com.google.devtools.build.lib.actions.Artifact.DerivedArtifact;
-import com.google.devtools.build.lib.actions.ArtifactRoot;
-import com.google.devtools.build.lib.actions.ArtifactRoot.RootType;
-import com.google.devtools.build.lib.actions.ResourceSet;
-import com.google.devtools.build.lib.actions.util.ActionsTestUtil;
-import com.google.devtools.build.lib.actions.util.LabelArtifactOwner;
-import com.google.devtools.build.lib.analysis.ConfiguredTarget;
-import com.google.devtools.build.lib.analysis.EmptyConfiguredTarget;
-import com.google.devtools.build.lib.analysis.FilesToRunProvider;
-import com.google.devtools.build.lib.analysis.RuleContext;
-import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
-import com.google.devtools.build.lib.analysis.actions.SpawnAction;
-import com.google.devtools.build.lib.analysis.util.AnalysisTestUtil;
-import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
-import com.google.devtools.build.lib.cmdline.Label;
-import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
-import com.google.devtools.build.lib.collect.nestedset.Order;
-import com.google.devtools.build.lib.packages.Info;
-import com.google.devtools.build.lib.packages.Provider;
-import com.google.devtools.build.lib.packages.util.MockProtoSupport;
-import com.google.devtools.build.lib.rules.proto.ProtoCompileActionBuilder.Services;
-import com.google.devtools.build.lib.rules.proto.ProtoCompileActionBuilder.ToolchainInvocation;
-import com.google.devtools.build.lib.util.OS;
-import com.google.devtools.build.lib.util.OnDemandString;
-import com.google.devtools.build.lib.vfs.DigestHashFunction;
-import com.google.devtools.build.lib.vfs.PathFragment;
-import com.google.devtools.build.lib.vfs.Root;
-import com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem;
-import javax.annotation.Nullable;
-import net.starlark.java.eval.StarlarkValue;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.JUnit4;
-
-/** Unit tests for {@link ProtoCompileActionBuilder}. */
-@RunWith(JUnit4.class)
-public class ProtoCompileActionBuilderTest extends BuildViewTestCase {
-
- private static final InMemoryFileSystem FILE_SYSTEM =
- new InMemoryFileSystem(DigestHashFunction.SHA256);
- private final ArtifactRoot sourceRoot =
- ArtifactRoot.asSourceRoot(Root.fromPath(FILE_SYSTEM.getPath("/")));
- private final ArtifactRoot derivedRoot =
- ArtifactRoot.asDerivedRoot(FILE_SYSTEM.getPath("/"), RootType.Output, "out");
-
- private AnalysisTestUtil.CollectingAnalysisEnvironment collectingAnalysisEnvironment;
- private Artifact out;
-
- @Before
- public final void setup() throws Exception {
- MockProtoSupport.setup(mockToolsConfig);
-
- collectingAnalysisEnvironment =
- new AnalysisTestUtil.CollectingAnalysisEnvironment(getTestAnalysisEnvironment());
- scratch.file(
- "foo/BUILD",
- "package(features = ['-feature'])",
- "proto_library(name = 'bar')",
- "exports_files(['out'])");
- out = getBinArtifactWithNoOwner("out");
- }
-
- private ProtoSource protoSource(String importPath) {
- return protoSource(artifact("//:dont-care", importPath));
- }
-
- private ProtoSource protoSource(Artifact protoSource) {
- return protoSource(protoSource, PathFragment.EMPTY_FRAGMENT);
- }
-
- private ProtoSource protoSource(Artifact protoSource, PathFragment sourceRoot) {
- return new ProtoSource(protoSource, sourceRoot);
- }
-
- private ConfiguredTarget protoInfo(
- ImmutableList<ProtoSource> directProtoSources,
- ImmutableList<ProtoSource> transitiveProtoSources,
- ImmutableList<ProtoSource> publicImportProtoSources,
- ImmutableList<ProtoSource> strictImportableSources) {
- return new EmptyConfiguredTarget(null, null) {
- @Override
- protected Info rawGetStarlarkProvider(Provider.Key providerKey) {
- return new ProtoInfo(
- /* directSources */ directProtoSources,
- /* directProtoSourceRoot */ PathFragment.EMPTY_FRAGMENT,
- /* transitiveSources */ NestedSetBuilder.wrap(
- Order.STABLE_ORDER, transitiveProtoSources),
- /* transitiveProtoSources */ NestedSetBuilder.emptySet(Order.STABLE_ORDER),
- /* transitiveProtoSourceRoots */ NestedSetBuilder.emptySet(Order.STABLE_ORDER),
- /* strictImportableProtoSourcesForDependents */ NestedSetBuilder.emptySet(
- Order.STABLE_ORDER),
- /* directDescriptorSet */ artifact("//:direct-descriptor-set", "direct-descriptor-set"),
- /* transitiveDescriptorSets */ NestedSetBuilder.emptySet(Order.STABLE_ORDER),
- /* exportedSources */ NestedSetBuilder.emptySet(Order.STABLE_ORDER),
- /* strictImportableSources */ NestedSetBuilder.wrap(
- Order.STABLE_ORDER, strictImportableSources),
- /* publicImportSources */ NestedSetBuilder.wrap(
- Order.STABLE_ORDER, publicImportProtoSources));
- }
- };
- }
-
- @Test
- public void commandLine_basic() throws Exception {
- FilesToRunProvider plugin =
- new FilesToRunProvider(
- NestedSetBuilder.emptySet(Order.STABLE_ORDER),
- null /* runfilesSupport */,
- artifact("//:dont-care", "protoc-gen-javalite.exe"));
- ProtoLangToolchainProvider toolchainNoPlugin =
- ProtoLangToolchainProvider.create(
- "--java_out=param1,param2:%s",
- /* pluginFormatFlag= */ null,
- /* pluginExecutable= */ null,
- /* runtime= */ mock(TransitiveInfoCollection.class),
- /* providedProtoSources= */ ImmutableList.of(),
- /* protoc= */ FilesToRunProvider.EMPTY,
- /* protocOpts= */ ImmutableList.of(),
- /* progressMessage = */ "",
- /* mnemonic= */ "");
- ProtoLangToolchainProvider toolchainWithPlugin =
- ProtoLangToolchainProvider.create(
- "--PLUGIN_pluginName_out=param3,param4:%s",
- /* pluginFormatFlag= */ "--plugin=protoc-gen-PLUGIN_pluginName=%s",
- plugin,
- /* runtime= */ mock(TransitiveInfoCollection.class),
- /* providedProtoSources= */ ImmutableList.of(),
- /* protoc= */ FilesToRunProvider.EMPTY,
- /* protocOpts= */ ImmutableList.of(),
- /* progressMessage = */ "",
- /* mnemonic= */ "");
- useConfiguration("--strict_proto_deps=OFF");
-
- RuleContext ruleContext =
- getRuleContext(getConfiguredTarget("//foo:bar"), collectingAnalysisEnvironment);
- registerActions(
- ruleContext,
- ImmutableList.of(
- new ToolchainInvocation("dontcare_because_no_plugin", toolchainNoPlugin, "foo.srcjar"),
- new ToolchainInvocation("pluginName", toolchainWithPlugin, "bar.srcjar")),
- protoInfo(
- /* directProtoSources */ ImmutableList.of(protoSource("source_file.proto")),
- /* transitiveProtoSources */ ImmutableList.of(
- protoSource("import1.proto"), protoSource("import2.proto")),
- /* publicImportProtoSources */ ImmutableList.of(),
- /* strictImportableSources */ ImmutableList.of()),
- ImmutableList.of(out),
- "dontcare_because_no_plugin",
- Services.ALLOW);
-
- ImmutableList<String> cmdLine =
- allArgsForAction((SpawnAction) collectingAnalysisEnvironment.getRegisteredActions().get(0));
- assertThat(cmdLine)
- .containsExactly(
- "--java_out=param1,param2:foo.srcjar",
- "--PLUGIN_pluginName_out=param3,param4:bar.srcjar",
- "--plugin=protoc-gen-PLUGIN_pluginName=protoc-gen-javalite.exe",
- "-Iimport1.proto=import1.proto",
- "-Iimport2.proto=import2.proto",
- "source_file.proto")
- .inOrder();
- }
-
- @Test
- public void commandline_derivedArtifact() throws Exception {
- // Verify that the command line contains the correct path to a generated protocol buffers.
- useConfiguration("--strict_proto_deps=OFF");
-
- RuleContext ruleContext =
- getRuleContext(getConfiguredTarget("//foo:bar"), collectingAnalysisEnvironment);
- registerActions(
- ruleContext,
- /* toolchainInvocations= */ ImmutableList.of(),
- protoInfo(
- /* directProtoSources */ ImmutableList.of(
- protoSource(derivedArtifact("source_file.proto"))),
- /* transitiveProtoSources */ ImmutableList.of(),
- /* publicImportProtoSources */ ImmutableList.of(),
- /* strictImportableSources */ ImmutableList.of()),
- ImmutableList.of(out),
- "dontcare_because_no_plugin",
- Services.ALLOW);
-
- ImmutableList<String> cmdLine =
- allArgsForAction((SpawnAction) collectingAnalysisEnvironment.getRegisteredActions().get(0));
- assertThat(cmdLine).containsExactly("out/source_file.proto");
- }
-
- @Test
- public void commandLine_strictDeps() throws Exception {
- ProtoLangToolchainProvider toolchain =
- ProtoLangToolchainProvider.create(
- "--java_out=param1,param2:%s",
- /* pluginFormatFlag= */ null,
- /* pluginExecutable= */ null,
- /* runtime= */ mock(TransitiveInfoCollection.class),
- /* providedProtoSources= */ ImmutableList.of(),
- /* protoc= */ FilesToRunProvider.EMPTY,
- /* protocOpts= */ ImmutableList.of(),
- /* progressMessage = */ "",
- /* mnemonic= */ "");
-
- RuleContext ruleContext =
- getRuleContext(getConfiguredTarget("//foo:bar"), collectingAnalysisEnvironment);
- registerActions(
- ruleContext,
- ImmutableList.of(new ToolchainInvocation("dontcare", toolchain, "foo.srcjar")),
- protoInfo(
- /* directProtoSources */ ImmutableList.of(protoSource("source_file.proto")),
- /* transitiveProtoSources */ ImmutableList.of(
- protoSource("import1.proto"), protoSource("import2.proto")),
- /* publicImportProtoSources */ ImmutableList.of(),
- /* strictImportableSources */ ImmutableList.of(protoSource("import1.proto"))),
- ImmutableList.of(out),
- "dontcare_because_no_plugin",
- Services.ALLOW);
-
- ImmutableList<String> cmdLine =
- allArgsForAction((SpawnAction) collectingAnalysisEnvironment.getRegisteredActions().get(0));
- assertThat(cmdLine)
- .containsExactly(
- "--java_out=param1,param2:foo.srcjar",
- "-Iimport1.proto=import1.proto",
- "-Iimport2.proto=import2.proto",
- "source_file.proto")
- .inOrder();
- }
-
- @Test
- public void commandLine_exports() throws Exception {
- ProtoLangToolchainProvider toolchain =
- ProtoLangToolchainProvider.create(
- "--java_out=param1,param2:%s",
- /* pluginFormatFlag= */ null,
- /* pluginExecutable= */ null,
- /* runtime= */ mock(TransitiveInfoCollection.class),
- /* providedProtoSources= */ ImmutableList.of(),
- /* protoc= */ FilesToRunProvider.EMPTY,
- /* protocOpts= */ ImmutableList.of(),
- /* progressMessage = */ "",
- /* mnemonic= */ "");
- useConfiguration("--strict_proto_deps=OFF");
-
- RuleContext ruleContext =
- getRuleContext(getConfiguredTarget("//foo:bar"), collectingAnalysisEnvironment);
- registerActions(
- ruleContext,
- ImmutableList.of(new ToolchainInvocation("dontcare", toolchain, "foo.srcjar")),
- protoInfo(
- /* directProtoSources */ ImmutableList.of(protoSource("source_file.proto")),
- /* transitiveProtoSources */ ImmutableList.of(
- protoSource("import1.proto"), protoSource("import2.proto")),
- /* publicImportProtoSources */ ImmutableList.of(protoSource("export1.proto")),
- /* strictImportableSources */ ImmutableList.of()),
- ImmutableList.of(out),
- "dontcare_because_no_plugin",
- Services.ALLOW);
-
- ImmutableList<String> cmdLine =
- allArgsForAction((SpawnAction) collectingAnalysisEnvironment.getRegisteredActions().get(0));
- assertThat(cmdLine)
- .containsExactly(
- "--java_out=param1,param2:foo.srcjar",
- "-Iimport1.proto=import1.proto",
- "-Iimport2.proto=import2.proto",
- "source_file.proto")
- .inOrder();
- }
-
- @Test
- public void otherParameters() throws Exception {
- useConfiguration("--protocopt=--foo", "--protocopt=--bar");
-
- RuleContext ruleContext =
- getRuleContext(getConfiguredTarget("//foo:bar"), collectingAnalysisEnvironment);
- registerActions(
- ruleContext,
- ImmutableList.of(),
- protoInfo(
- /* directProtoSources */ ImmutableList.of(),
- /* transitiveProtoSources */ ImmutableList.of(),
- /* publicImportProtoSources */ ImmutableList.of(),
- /* strictImportableSources */ ImmutableList.of()),
- ImmutableList.of(out),
- "dontcare",
- Services.DISALLOW);
-
- ImmutableList<String> cmdLine =
- allArgsForAction((SpawnAction) collectingAnalysisEnvironment.getRegisteredActions().get(0));
- assertThat(cmdLine).containsAtLeast("--disallow_services", "--foo", "--bar");
- }
-
- private static class InterceptOnDemandString extends OnDemandString implements StarlarkValue {
- boolean hasBeenCalled;
-
- @Override
- public String toString() {
- hasBeenCalled = true;
- return "mu";
- }
- }
-
- @Test
- public void outReplacementAreLazilyEvaluated() throws Exception {
- InterceptOnDemandString outReplacement = new InterceptOnDemandString();
- ProtoLangToolchainProvider toolchain =
- ProtoLangToolchainProvider.create(
- "--java_out=param1,param2:%s",
- /* pluginFormatFlag= */ null,
- /* pluginExecutable= */ null,
- /* runtime= */ mock(TransitiveInfoCollection.class),
- /* providedProtoSources= */ ImmutableList.of(),
- /* protoc= */ FilesToRunProvider.EMPTY,
- /* protocOpts= */ ImmutableList.of(),
- /* progressMessage = */ "",
- /* mnemonic= */ "");
-
- RuleContext ruleContext =
- getRuleContext(getConfiguredTarget("//foo:bar"), collectingAnalysisEnvironment);
- registerActions(
- ruleContext,
- ImmutableList.of(new ToolchainInvocation("pluginName", toolchain, outReplacement)),
- protoInfo(
- /* directProtoSources */ ImmutableList.of(),
- /* transitiveProtoSources */ ImmutableList.of(),
- /* publicImportProtoSources */ ImmutableList.of(),
- /* strictImportableSources */ ImmutableList.of()),
- ImmutableList.of(out),
- "flavour",
- Services.ALLOW);
-
- assertThat(outReplacement.hasBeenCalled).isFalse();
-
- allArgsForAction((SpawnAction) collectingAnalysisEnvironment.getRegisteredActions().get(0));
- assertThat(outReplacement.hasBeenCalled).isTrue();
- }
-
- /**
- * Tests that if the same invocation-name is specified by more than one invocation,
- * ProtoCompileActionBuilder throws an exception.
- */
- @Test
- public void exceptionIfSameName() throws Exception {
- ProtoLangToolchainProvider toolchain1 =
- ProtoLangToolchainProvider.create(
- "dontcare=%s",
- /* pluginFormatFlag= */ null,
- /* pluginExecutable= */ null,
- /* runtime= */ mock(TransitiveInfoCollection.class),
- /* providedProtoSources= */ ImmutableList.of(),
- /* protoc= */ FilesToRunProvider.EMPTY,
- /* protocOpts= */ ImmutableList.of(),
- /* progressMessage = */ "",
- /* mnemonic= */ "");
- ProtoLangToolchainProvider toolchain2 =
- ProtoLangToolchainProvider.create(
- "dontcare=%s",
- /* pluginFormatFlag= */ null,
- /* pluginExecutable= */ null,
- /* runtime= */ mock(TransitiveInfoCollection.class),
- /* providedProtoSources= */ ImmutableList.of(),
- /* protoc= */ FilesToRunProvider.EMPTY,
- /* protocOpts= */ ImmutableList.of(),
- /* progressMessage = */ "",
- /* mnemonic= */ "");
-
- RuleContext ruleContext =
- getRuleContext(getConfiguredTarget("//foo:bar"), collectingAnalysisEnvironment);
- IllegalStateException e =
- assertThrows(
- IllegalStateException.class,
- () ->
- registerActions(
- ruleContext,
- ImmutableList.of(
- new ToolchainInvocation("pluginName", toolchain1, "outReplacement"),
- new ToolchainInvocation("pluginName", toolchain2, "outReplacement")),
- protoInfo(
- /* directProtoSources */ ImmutableList.of(),
- /* transitiveProtoSources */ ImmutableList.of(),
- /* publicImportProtoSources */ ImmutableList.of(),
- /* strictImportableSources */ ImmutableList.of()),
- ImmutableList.of(out),
- "dontcare",
- Services.ALLOW));
-
- assertThat(e)
- .hasMessageThat()
- .isEqualTo(
- "Invocation name pluginName appears more than once. "
- + "This could lead to incorrect proto-compiler behavior");
- }
-
- @Test
- public void testProtoCommandLineArgv_noImportable() throws Exception {
- assertThat(
- protoArgv(
- /* transitiveSources */ ImmutableList.of(
- protoSource(derivedArtifact("foo.proto"), derivedRoot.getExecPath())),
- /* importableProtoSources */ null))
- .contains("-Ifoo.proto=out/foo.proto");
- }
-
- @Test
- public void testProtoCommandLineArgv_singleImportable() throws Exception {
- assertThat(
- protoArgv(
- /* transitiveSources */ ImmutableList.of(
- protoSource(derivedArtifact("foo.proto"), derivedRoot.getExecPath())),
- /* importableProtoSources */ ImmutableList.of(
- protoSource(derivedArtifact("foo.proto"), derivedRoot.getExecPath()))))
- .contains("-Ifoo.proto=out/foo.proto");
- }
-
- @Test
- public void testProtoCommandLineArgv_doubleImportable() throws Exception {
- assertThat(
- protoArgv(
- /* transitiveSources */ ImmutableList.of(
- protoSource(derivedArtifact("foo.proto"), derivedRoot.getExecPath())),
- /* importableProtoSources */ ImmutableList.of(
- protoSource(derivedArtifact("foo.proto"), derivedRoot.getExecPath()),
- protoSource(derivedArtifact("bar.proto"), derivedRoot.getExecPath()))))
- .contains("-Ifoo.proto=out/foo.proto");
- }
-
- /**
- * Include-maps are the -Ivirtual=physical arguments passed to proto-compiler. When including a
- * file named 'foo/bar.proto' from an external repository 'bla', the include-map should be
- * -Ifoo/bar.proto=external/bla/foo/bar.proto. That is - 'virtual' should be the path relative to
- * the external repo root, and physical should be the physical file location.
- */
- @Test
- public void testIncludeMapsOfExternalFiles() throws Exception {
- assertThat(
- protoArgv(
- /* transitiveSources */ ImmutableList.of(
- protoSource(
- artifact("@bla//foo:bar", "external/bla/foo/bar.proto"),
- PathFragment.create("external/bla"))),
- /* importableProtoSources */ ImmutableList.of()))
- .contains("-Ifoo/bar.proto=external/bla/foo/bar.proto");
- }
-
- @Test
- public void directDependenciesOnExternalFiles() throws Exception {
- Artifact protoSource = artifact("@bla//foo:bar", "external/bla/foo/bar.proto");
- assertThat(
- protoArgv(
- /* transitiveSources */ ImmutableList.of(
- protoSource(protoSource, PathFragment.create("external/bla"))),
- /* importableProtoSources */ ImmutableList.of(
- protoSource(protoSource, PathFragment.create("external/bla")))))
- .contains("-Ifoo/bar.proto=external/bla/foo/bar.proto");
- }
-
- private Artifact artifact(String ownerLabel, String path) {
- return new Artifact.SourceArtifact(
- sourceRoot,
- sourceRoot.getExecPath().getRelative(path),
- new LabelArtifactOwner(Label.parseAbsoluteUnchecked(ownerLabel)));
- }
-
- /** Creates a dummy artifact with the given path, that actually resides in /out/<path>. */
- private Artifact derivedArtifact(String path) {
- Artifact.DerivedArtifact derivedArtifact =
- DerivedArtifact.create(
- derivedRoot,
- derivedRoot.getExecPath().getRelative(path),
- ActionsTestUtil.NULL_ARTIFACT_OWNER);
- derivedArtifact.setGeneratingActionKey(ActionsTestUtil.NULL_ACTION_LOOKUP_DATA);
- return derivedArtifact;
- }
-
- private ImmutableList<String> protoArgv(
- Iterable<ProtoSource> transitiveSources,
- @Nullable Iterable<ProtoSource> importableProtoSources)
- throws Exception {
- RuleContext ruleContext =
- getRuleContext(getConfiguredTarget("//foo:bar"), collectingAnalysisEnvironment);
- registerActions(
- ruleContext,
- ImmutableList.of(),
- protoInfo(
- /* directProtoSources */ ImmutableList.of(),
- /* transitiveProtoSources */ ImmutableList.copyOf(transitiveSources),
- /* publicImportProtoSources */ ImmutableList.of(),
- /* strictImportableSources */ importableProtoSources == null
- ? ImmutableList.of()
- : ImmutableList.copyOf(importableProtoSources)),
- ImmutableList.of(out),
- "dontcare",
- Services.DISALLOW);
-
- return allArgsForAction(
- (SpawnAction) collectingAnalysisEnvironment.getRegisteredActions().get(0));
- }
-
- @Test
- public void testEstimateResourceConsumptionLocal() throws Exception {
-
- assertThat(
- new ProtoCompileActionBuilder.ProtoCompileResourceSetBuilder()
- .buildResourceSet(OS.DARWIN, 0))
- .isEqualTo(ResourceSet.createWithRamCpu(25, 1));
-
- assertThat(
- new ProtoCompileActionBuilder.ProtoCompileResourceSetBuilder()
- .buildResourceSet(OS.LINUX, 2))
- .isEqualTo(ResourceSet.createWithRamCpu(25.3, 1));
- }
-}