Skip to content

Commit

Permalink
Collect artifacts from DefaultInfo.files to generate top-level aspe…
Browse files Browse the repository at this point in the history
…cts output files

PiperOrigin-RevId: 675628650
Change-Id: I8d08220150ac0c07a3e3d06a4241ed97c4c40df3
  • Loading branch information
mai93 authored and copybara-github committed Sep 17, 2024
1 parent 2d5591f commit d4cc6a4
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.devtools.build.lib.analysis.configuredtargets.InputFileConfiguredTarget;
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget;
import com.google.devtools.build.lib.analysis.test.TestProvider;
import com.google.devtools.build.lib.collect.nestedset.Depset.TypeException;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSet.Node;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
Expand Down Expand Up @@ -197,22 +198,21 @@ private static void memoizedAddAll(
*/
public static ArtifactsToBuild getAllArtifactsToBuild(
ProviderCollection target, TopLevelArtifactContext context) {
return getAllArtifactsToBuild(
OutputGroupInfo.get(target), target.getProvider(FileProvider.class), context);
return getAllArtifactsToBuild(OutputGroupInfo.get(target), getFilesToBuild(target), context);
}

static ArtifactsToBuild getAllArtifactsToBuild(
@Nullable OutputGroupInfo outputGroupInfo,
@Nullable FileProvider fileProvider,
@Nullable NestedSet<Artifact> filesToBuild,
TopLevelArtifactContext context) {
ImmutableMap.Builder<String, ArtifactsInOutputGroup> allOutputGroups =
ImmutableMap.builderWithExpectedSize(context.outputGroups().size());
boolean allOutputGroupsImportant = true;
for (String outputGroup : context.outputGroups()) {
NestedSetBuilder<Artifact> results = NestedSetBuilder.stableOrder();

if (outputGroup.equals(OutputGroupInfo.DEFAULT) && fileProvider != null) {
results.addTransitive(fileProvider.getFilesToBuild());
if (outputGroup.equals(OutputGroupInfo.DEFAULT) && filesToBuild != null) {
results.addTransitive(filesToBuild);
}

if (outputGroupInfo != null) {
Expand All @@ -239,6 +239,27 @@ static ArtifactsToBuild getAllArtifactsToBuild(
allOutputGroups.buildOrThrow(), /*allOutputGroupsImportant=*/ allOutputGroupsImportant);
}

/**
* Returns files to build directly from {@link FileProvider} or from {@code files} under {@link
* DefaultInfo} provider.
*/
@Nullable
private static NestedSet<Artifact> getFilesToBuild(ProviderCollection target) {
if (target.getProvider(FileProvider.class) != null) {
return target.getProvider(FileProvider.class).getFilesToBuild();
} else if (target.get(DefaultInfo.PROVIDER.getKey()) != null) {
DefaultInfo defaultInfo = (DefaultInfo) target.get(DefaultInfo.PROVIDER.getKey());
if (defaultInfo.getFiles() != null) {
try {
return defaultInfo.getFiles().getSet(Artifact.class);
} catch (TypeException e) {
throw new IllegalStateException("Error getting 'files' field of 'DefaultInfo'", e);
}
}
}
return null;
}

/**
* Returns false if the build outputs provided by the target should never be shown to users.
*
Expand Down
48 changes: 48 additions & 0 deletions src/test/shell/integration/aspect_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1923,4 +1923,52 @@ EOF
expect_log 'in rule_impl of rule: @@\?//test:t1, toolchain_aspect on dep @@\?//test:t2 = \["toolchain_aspect on @@\?//test:t2", "toolchain_aspect on @@\?//test_toolchains:foo", "toolchain_aspect on @@\?//test_toolchains:toolchain_dep"\]'
}

function test_aspect_default_info_files_generated() {
local package="test"
mkdir -p "${package}"
cat > "${package}/defs.bzl" <<EOF
def _aspect_impl(target, ctx):
files = []
if hasattr(ctx.rule.attr, 'dep') and ctx.rule.attr.dep:
files.append(ctx.rule.attr.dep[DefaultInfo].files)
f = ctx.actions.declare_file('from_aspect_on_{}'.format(target.label.name))
ctx.actions.write(f, 'hi')
return [DefaultInfo(files=depset(direct = [f], transitive = files))]
my_aspect = aspect(implementation = _aspect_impl, attr_aspects = ['dep'])
def _impl(ctx):
f = ctx.actions.declare_file('from_rule_{}'.format(ctx.label.name))
ctx.actions.write(f, 'hi')
return [DefaultInfo(files=depset([f]))]
my_rule = rule(
implementation = _impl,
attrs = {
"dep": attr.label()
},
)
EOF

cat > "${package}/BUILD" <<EOF
load("//${package}:defs.bzl", "my_rule")
my_rule(name = 't1', dep = 't2')
my_rule(name = 't2')
EOF

bazel build "//${package}:t1" --aspects="//${package}:defs.bzl%my_aspect"\
&> $TEST_log || fail "Build failed"

assert_nonempty_file 'bazel-bin/test/from_aspect_on_t1'
assert_nonempty_file 'bazel-bin/test/from_rule_t1'

assert_nonempty_file 'bazel-bin/test/from_aspect_on_t2'
assert_nonempty_file 'bazel-bin/test/from_rule_t2'
}

run_suite "Tests for aspects"

0 comments on commit d4cc6a4

Please sign in to comment.