Skip to content

Commit

Permalink
Add automatic exec groups to java_common.pack_sources
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 509529874
Change-Id: I399b4374e5b665774c12d0493d9d5c2928aa505c
  • Loading branch information
kotlaja authored and copybara-github committed Feb 14, 2023
1 parent c15148a commit 4b58dfb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Streams.stream;
import static com.google.devtools.build.lib.packages.ExecGroup.DEFAULT_EXEC_GROUP_NAME;
import static com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider.ClasspathType.BOTH;
import static com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider.ClasspathType.COMPILE_ONLY;
import static com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider.ClasspathType.RUNTIME_ONLY;
Expand Down Expand Up @@ -182,7 +181,8 @@ Artifact packSourceFiles(
Artifact outputSourceJar,
List<Artifact> sourceFiles,
List<Artifact> sourceJars,
JavaToolchainProvider javaToolchain)
JavaToolchainProvider javaToolchain,
String execGroup)
throws EvalException {
if (outputJar == null && outputSourceJar == null) {
throw Starlark.errorf(
Expand All @@ -196,8 +196,6 @@ Artifact packSourceFiles(
if (outputSourceJar == null) {
outputSourceJar = getDerivedSourceJar(actions.getActionConstructionContext(), outputJar);
}
// TODO(b/253376589): Add automatic exec groups instead of DEFAULT_EXEC_GROUP_NAME if
// needed.
SingleJarActionBuilder.createSourceJarAction(
actionRegistry,
actions.getActionConstructionContext(),
Expand All @@ -206,7 +204,7 @@ Artifact packSourceFiles(
NestedSetBuilder.<Artifact>wrap(Order.STABLE_ORDER, sourceJars),
outputSourceJar,
javaToolchain,
DEFAULT_EXEC_GROUP_NAME);
execGroup);
return outputSourceJar;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ public Artifact packSources(
outputSourceJar instanceof Artifact ? (Artifact) outputSourceJar : null,
Sequence.cast(sourceFiles, Artifact.class, "sources"),
Sequence.cast(sourceJars, Artifact.class, "source_jars"),
javaToolchain);
javaToolchain,
getExecGroup(actions.getRuleContext().useAutoExecGroups()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1319,4 +1319,36 @@ public void javaCommonBuildIjar_automaticExecGroupsEnabled_ijarActionsExecuteOnF
assertThat(actions.get(0).getOwner().getExecutionPlatform().label())
.isEqualTo(Label.parseCanonical("//platforms:platform_1"));
}

@Test
public void javaCommonPackSources_automaticExecGroupsEnabled_sourceActionExecutesOnFirstPlatform()
throws Exception {
scratch.file(
"test/defs.bzl",
"def _impl(ctx):",
" output_jar = ctx.actions.declare_file('lib_' + ctx.label.name + '.jar')",
" source_jar = java_common.pack_sources(",
" ctx.actions,",
" output_source_jar = output_jar,",
" java_toolchain = ctx.toolchains['" + TestConstants.JAVA_TOOLCHAIN_TYPE + "'].java,",
" )",
" return []",
"custom_rule = rule(",
" implementation = _impl,",
" toolchains = ['//rule:toolchain_type_2', '" + TestConstants.JAVA_TOOLCHAIN_TYPE + "'],",
" fragments = ['java']",
")");
scratch.file(
"test/BUILD",
"load('//test:defs.bzl', 'custom_rule')",
"custom_rule(name = 'custom_rule_name')");
useConfiguration("--incompatible_auto_exec_groups");

ImmutableList<Action> actions = getActions("//test:custom_rule_name", SpawnAction.class);

assertThat(actions).hasSize(1);
assertThat(actions.get(0).getMnemonic()).isEqualTo("JavaSourceJar");
assertThat(actions.get(0).getOwner().getExecutionPlatform().label())
.isEqualTo(Label.parseCanonical("//platforms:platform_1"));
}
}

0 comments on commit 4b58dfb

Please sign in to comment.