Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.common.collect.Interner;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.CommandLine;
import com.google.devtools.build.lib.actions.ExecutionRequirements;
import com.google.devtools.build.lib.actions.ParamFileInfo;
import com.google.devtools.build.lib.actions.ParameterFile;
Expand Down Expand Up @@ -73,6 +74,7 @@ public final class JavaCompilationHelper {
private final ImmutableList<Artifact> additionalInputsForDatabinding;
private boolean enableJspecify = true;
private boolean enableDirectClasspath = true;
private ImmutableList<CommandLine> extraCommandLineArgs = ImmutableList.of();
private final String execGroup;

public JavaCompilationHelper(
Expand Down Expand Up @@ -104,6 +106,10 @@ public void enableJspecify(boolean enableJspecify) {
this.enableJspecify = enableJspecify;
}

public void setExtraCommandLineArgs(ImmutableList<CommandLine> extraCommandLineArgs) {
this.extraCommandLineArgs = extraCommandLineArgs;
}

JavaTargetAttributes getAttributes() {
if (builtAttributes == null) {
builtAttributes = attributes.build();
Expand Down Expand Up @@ -298,6 +304,7 @@ && getJavaConfiguration().experimentalEnableJspecify()
.getFixDepsTool(ruleContext.getRule(), getJavaConfiguration())
.ifPresent(builder::setFixDepsTool);
builder.setCompileTimeDependencyArtifacts(attributes.getCompileTimeDependencyArtifacts());
builder.setExtraCommandLineArgs(extraCommandLineArgs);
builder.setTargetLabel(
attributes.getTargetLabel() == null ? label : attributes.getTargetLabel());
builder.setInjectingRuleKind(attributes.getInjectingRuleKind());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ enum CompilationType {
private final ImmutableMap<String, String> executionInfo;
private final CommandLine executableLine;
private final CommandLine flagLine;
private final ImmutableList<CommandLine> extraCommandLineArgs;
Comment thread
hvadehra marked this conversation as resolved.
private final BuildConfigurationValue configuration;
private final OnDemandString progressMessage;

Expand All @@ -149,6 +150,7 @@ public JavaCompileAction(
ExtraActionInfoSupplier extraActionInfoSupplier,
CommandLine executableLine,
CommandLine flagLine,
ImmutableList<CommandLine> extraCommandLineArgs,
BuildConfigurationValue configuration,
NestedSet<Artifact> dependencyArtifacts,
Artifact outputDepsProto,
Expand All @@ -171,6 +173,7 @@ public JavaCompileAction(
configuration.modifiedExecutionInfo(executionInfo, compilationType.mnemonic);
this.executableLine = executableLine;
this.flagLine = flagLine;
this.extraCommandLineArgs = extraCommandLineArgs;
this.configuration = configuration;
this.progressMessage = progressMessage;
this.extraActionInfoSupplier = extraActionInfoSupplier;
Expand Down Expand Up @@ -230,6 +233,10 @@ protected void computeKey(
actionKeyContext, inputMetadataProvider, effectiveOutputPathsMode, fp);
flagLine.addToFingerprint(
actionKeyContext, inputMetadataProvider, effectiveOutputPathsMode, fp);
for (CommandLine extraCommandLine : extraCommandLineArgs) {
extraCommandLine.addToFingerprint(
actionKeyContext, inputMetadataProvider, effectiveOutputPathsMode, fp);
}
// As the classpath is no longer part of commandLines implicitly, we need to explicitly add
// the transitive inputs to the key here.
actionKeyContext.addNestedSetToFingerprint(fp, transitiveInputs);
Expand Down Expand Up @@ -309,12 +316,15 @@ private JavaSpawn getReducedSpawn(
classpathLine.add("--reduce_classpath_mode", fallback ? "BAZEL_FALLBACK" : "BAZEL_REDUCED");
}

CommandLines reducedCommandLine =
CommandLines.Builder commandLinesBuilder =
CommandLines.builder()
.addCommandLine(executableLine)
.addCommandLine(flagLine, PARAM_FILE_INFO)
.addCommandLine(classpathLine.build(), PARAM_FILE_INFO)
.build();
.addCommandLine(classpathLine.build(), PARAM_FILE_INFO);
for (CommandLine extraCommandLine : extraCommandLineArgs) {
commandLinesBuilder.addCommandLine(extraCommandLine, PARAM_FILE_INFO);
}
CommandLines reducedCommandLine = commandLinesBuilder.build();
CommandLines.ExpandedCommandLines expandedCommandLines =
reducedCommandLine.expand(
actionExecutionContext.getInputMetadataProvider(),
Expand Down Expand Up @@ -552,11 +562,14 @@ private static boolean appendCount(StringBuilder sb, boolean first, int count, S
public ExtraActionInfo.Builder getExtraActionInfo(ActionKeyContext actionKeyContext)
throws CommandLineExpansionException, InterruptedException {
ExtraActionInfo.Builder builder = super.getExtraActionInfo(actionKeyContext);
CommandLines commandLinesWithoutExecutable =
CommandLines.Builder commandLinesBuilder =
CommandLines.builder()
.addCommandLine(flagLine)
.addCommandLine(getFullClasspathLine())
.build();
.addCommandLine(getFullClasspathLine());
for (CommandLine extraCommandLine : extraCommandLineArgs) {
commandLinesBuilder.addCommandLine(extraCommandLine);
}
CommandLines commandLinesWithoutExecutable = commandLinesBuilder.build();
if (extraActionInfoSupplier != null) {
extraActionInfoSupplier.extend(builder, commandLinesWithoutExecutable.allArguments());
}
Expand Down Expand Up @@ -604,11 +617,15 @@ public PathMapper getPathMapper() {

@VisibleForTesting
public CommandLines getCommandLines() {
return CommandLines.builder()
.addCommandLine(executableLine)
.addCommandLine(flagLine, PARAM_FILE_INFO)
.addCommandLine(getFullClasspathLine(), PARAM_FILE_INFO)
.build();
CommandLines.Builder builder =
CommandLines.builder()
.addCommandLine(executableLine)
.addCommandLine(flagLine, PARAM_FILE_INFO)
.addCommandLine(getFullClasspathLine(), PARAM_FILE_INFO);
for (CommandLine extraCommandLine : extraCommandLineArgs) {
builder.addCommandLine(extraCommandLine, PARAM_FILE_INFO);
}
return builder.build();
}

private CommandLine getFullClasspathLine() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.devtools.build.lib.actions.ActionAnalysisMetadata;
import com.google.devtools.build.lib.actions.ActionEnvironment;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.CommandLine;
import com.google.devtools.build.lib.actions.extra.ExtraActionInfo;
import com.google.devtools.build.lib.actions.extra.JavaCompileInfo;
import com.google.devtools.build.lib.analysis.RuleContext;
Expand Down Expand Up @@ -157,6 +158,7 @@ public void extend(ExtraActionInfo.Builder builder, ImmutableList<String> argume
private NestedSet<Artifact> extraData = NestedSetBuilder.emptySet(Order.NAIVE_LINK_ORDER);
private Label targetLabel;
@Nullable private String injectingRuleKind;
private ImmutableList<CommandLine> extraCommandLineArgs = ImmutableList.of();
private ImmutableList<Artifact> additionalInputs = ImmutableList.of();
private Artifact genSourceOutput;
private JavaCompileOutputs<Artifact> outputs;
Expand Down Expand Up @@ -260,6 +262,7 @@ public JavaCompileAction build() throws RuleErrorException, InterruptedException
/* extraActionInfoSupplier= */ extraActionInfoSupplier,
/* executableLine= */ executableLine,
/* flagLine= */ buildParamFileContents(javacOpts),
/* extraCommandLineArgs= */ extraCommandLineArgs,
/* configuration= */ ruleContext.getConfiguration(),
/* dependencyArtifacts= */ compileTimeDependencyArtifacts,
/* outputDepsProto= */ outputs.depsProto(),
Expand Down Expand Up @@ -378,6 +381,13 @@ public JavaCompileActionBuilder setCompileTimeDependencyArtifacts(
return this;
}

@CanIgnoreReturnValue
public JavaCompileActionBuilder setExtraCommandLineArgs(
ImmutableList<CommandLine> extraCommandLineArgs) {
this.extraCommandLineArgs = extraCommandLineArgs;
return this;
}

@CanIgnoreReturnValue
public JavaCompileActionBuilder setJavacOpts(ImmutableList<String> copts) {
this.javacOpts = Preconditions.checkNotNull(copts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,7 @@ public void build(JavaToolchainProvider javaToolchain)
/* extraActionInfoSupplier= */ null,
/* executableLine= */ executableLine,
/* flagLine= */ commandLine.build(),
/* extraCommandLineArgs= */ ImmutableList.of(),
/* configuration= */ ruleContext.getConfiguration(),
/* dependencyArtifacts= */ compileTimeDependencyArtifacts,
/* outputDepsProto= */ outputDepsProto,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.CommandLine;
import com.google.devtools.build.lib.analysis.starlark.Args;
import com.google.devtools.build.lib.starlarkbuildapi.CommandLineArgsApi;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.Expander;
import com.google.devtools.build.lib.analysis.RuleContext;
Expand Down Expand Up @@ -51,6 +55,7 @@
import com.google.devtools.build.lib.rules.cpp.CppFileTypes;
import com.google.devtools.build.lib.starlarkbuildapi.core.ProviderApi;
import com.google.devtools.build.lib.starlarkbuildapi.java.JavaCommonApi;
import net.starlark.java.eval.Dict;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.Sequence;
import net.starlark.java.eval.Starlark;
Expand Down Expand Up @@ -201,7 +206,8 @@ public void createCompilationAction(
boolean enableJSpecify,
boolean enableDirectClasspath,
Sequence<?> additionalInputs,
Sequence<?> additionalOutputs)
Sequence<?> additionalOutputs,
Sequence<?> extraArgs)
throws EvalException,
TypeException,
RuleErrorException,
Expand All @@ -217,6 +223,11 @@ public void createCompilationAction(
.nativeHeader(nativeHeader == Starlark.NONE ? null : (Artifact) nativeHeader)
.manifestProto(manifestProto)
.build();
ImmutableList.Builder<CommandLine> extraCommandLineArgs = ImmutableList.builder();
for (Args args : Sequence.cast(extraArgs, Args.class, "extra_args")) {
extraCommandLineArgs.add(
args.build(ctx.getRuleContext().getAnalysisEnvironment()::getMainRepoMapping));
}
JavaTargetAttributes.Builder attributesBuilder =
new JavaTargetAttributes.Builder()
.addSourceJars(Sequence.cast(sourceJars, Artifact.class, "source_jars"))
Expand Down Expand Up @@ -261,6 +272,7 @@ public void createCompilationAction(
Depset.cast(javaBuilderJvmFlags, String.class, "javabuilder_jvm_flags"));
compilationHelper.enableJspecify(enableJSpecify);
compilationHelper.enableDirectClasspath(enableDirectClasspath);
compilationHelper.setExtraCommandLineArgs(extraCommandLineArgs.build());
compilationHelper.createCompileAction(outputs);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.devtools.build.lib.collect.nestedset.Depset.TypeException;
import com.google.devtools.build.lib.packages.Info;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.starlarkbuildapi.CommandLineArgsApi;
import com.google.devtools.build.lib.starlarkbuildapi.FileApi;
import com.google.devtools.build.lib.starlarkbuildapi.StarlarkActionFactoryApi;
import com.google.devtools.build.lib.starlarkbuildapi.StarlarkRuleContextApi;
Expand All @@ -31,6 +32,7 @@
import net.starlark.java.annot.ParamType;
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.annot.StarlarkMethod;
import net.starlark.java.eval.Dict;
import net.starlark.java.eval.EvalException;
import net.starlark.java.eval.NoneType;
import net.starlark.java.eval.Sequence;
Expand Down Expand Up @@ -524,6 +526,14 @@ void createHeaderCompilationAction(
@Param(name = "enable_direct_classpath", defaultValue = "True", named = true),
@Param(name = "additional_inputs", defaultValue = "[]", named = true),
@Param(name = "additional_outputs", defaultValue = "[]", named = true),
@Param(
name = "extra_args",
allowedTypes = {
@ParamType(type = Sequence.class, generic1 = CommandLineArgsApi.class),
},
defaultValue = "[]",
named = true,
positional = false),
})
void createCompilationAction(
StarlarkRuleContextT ctx,
Expand Down Expand Up @@ -553,7 +563,8 @@ void createCompilationAction(
boolean enableJSpecify,
boolean enableDirectClasspath,
Sequence<?> additionalInputs,
Sequence<?> additionalOutputs)
Sequence<?> additionalOutputs,
Sequence<?> extraArgs)
throws EvalException,
TypeException,
RuleErrorException,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ java_test(
"//src/main/java/com/google/devtools/build/lib/actions",
"//src/main/java/com/google/devtools/build/lib/actions:action_input_prefetcher",
"//src/main/java/com/google/devtools/build/lib/actions:artifacts",
"//src/main/java/com/google/devtools/build/lib/analysis:configured_target",
"//src/main/java/com/google/devtools/build/lib/actions:file_metadata",
"//src/main/java/com/google/devtools/build/lib/cmdline",
"//src/main/java/com/google/devtools/build/lib/remote:remote_action_file_system",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,64 @@ private static BulkTransferException lostInputException(Artifact artifact, Strin
cacheMiss.setFilename(artifact.getExecPathString());
return new BulkTransferException(cacheMiss);
}

@Test
public void testExtraArgsPropagated() throws Exception {
scratch.file("third_party/bazel_rules/rules_java/BUILD");
scratch.file(
"third_party/bazel_rules/rules_java/rule.bzl",
"""
load("@rules_java//java:defs.bzl", "JavaPluginInfo", rules_java_common = "java_common")

def _my_rule_impl(ctx):
output = ctx.outputs.jar
manifest = ctx.actions.declare_file(ctx.label.name + ".manifest")
internal_common = java_common.internal_DO_NOT_USE()
args = ctx.actions.args()
args.add("--foo=bar")
internal_common.create_compilation_action(
ctx,
ctx.attr._java_toolchain[rules_java_common.JavaToolchainInfo],
output,
manifest,
JavaPluginInfo(runtime_deps = []),
depset(),
depset(),
depset(),
depset(),
depset(),
depset(),
"ERROR",
ctx.label,
extra_args = [args],
)
return [DefaultInfo(files = depset([output]))]

my_rule = rule(
implementation = _my_rule_impl,
outputs = {
"jar": "%{name}.jar",
},
attrs = {
"_java_toolchain": attr.label(default = "@bazel_tools//tools/jdk:current_java_toolchain"),
},
fragments = ["java"],
toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
)
""");
scratch.file(
"java/com/google/test/BUILD",
"""
load("//third_party/bazel_rules/rules_java:rule.bzl", "my_rule")

my_rule(
name = "a",
)
""");

JavaCompileAction compileAction =
(JavaCompileAction) getGeneratingActionForLabel("//java/com/google/test:a.jar");
List<String> command = getJavacArguments(compileAction);
assertThat(command).contains("--foo=bar");
}
}
Loading
Loading