Skip to content

Commit

Permalink
Add module_files output group, used to build PCMs from cc_library etc…
Browse files Browse the repository at this point in the history
… directly

Today, `bazel build :target` on a cc_library will not build its PCM.
To debug problems with module compiles we resort to building a target
that depends on the module. This gets in the way, particularly when a *minimal*
such target doesn't exist and others need to reproduce the failure.

With this change, `bazel build :target --output_groups=module_files` will produce the PCM.

PiperOrigin-RevId: 570629210
Change-Id: Ic07509da42637321a0c65f41b0bba8864e3b6a83
  • Loading branch information
sam-mccall authored and copybara-github committed Oct 4, 2023
1 parent ac10bac commit 9b30bf7
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1626,6 +1626,9 @@ private ImmutableList<Artifact> createSourceAction(
if (gcnoFile != null) {
result.addPicGcnoFile(gcnoFile);
}
if (outputCategory == ArtifactCategory.CPP_MODULE) {
result.addModuleFile(picAction.getPrimaryOutput());
}
}

if (generateNoPicAction) {
Expand Down Expand Up @@ -1699,6 +1702,9 @@ private ImmutableList<Artifact> createSourceAction(
if (gcnoFile != null) {
result.addGcnoFile(gcnoFile);
}
if (outputCategory == ArtifactCategory.CPP_MODULE) {
result.addModuleFile(compileAction.getPrimaryOutput());
}
}
return directOutputs.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public class CcCompilationOutputs implements CcCompilationOutputsApi<Artifact> {
*/
private final ImmutableList<Artifact> headerTokenFiles;

/** All .pcm files built by the target. */
private final ImmutableList<Artifact> moduleFiles;

private CcCompilationOutputs(
ImmutableList<Artifact> objectFiles,
ImmutableList<Artifact> picObjectFiles,
Expand All @@ -85,7 +88,8 @@ private CcCompilationOutputs(
ImmutableList<Artifact> gcnoFiles,
ImmutableList<Artifact> picGcnoFiles,
NestedSet<Artifact> temps,
ImmutableList<Artifact> headerTokenFiles) {
ImmutableList<Artifact> headerTokenFiles,
ImmutableList<Artifact> moduleFiles) {
this.objectFiles = objectFiles;
this.picObjectFiles = picObjectFiles;
this.ltoCompilationContext = ltoCompilationContext;
Expand All @@ -95,6 +99,7 @@ private CcCompilationOutputs(
this.picGcnoFiles = picGcnoFiles;
this.temps = temps;
this.headerTokenFiles = headerTokenFiles;
this.moduleFiles = moduleFiles;
}

/**
Expand Down Expand Up @@ -135,6 +140,12 @@ public Sequence<Artifact> getStarlarkHeaderTokens(StarlarkThread thread) throws
return StarlarkList.immutableCopyOf(getHeaderTokenFiles());
}

@Override
public Sequence<Artifact> getStarlarkModuleFiles(StarlarkThread thread) throws EvalException {
CcModule.checkPrivateStarlarkificationAllowlist(thread);
return StarlarkList.immutableCopyOf(getModuleFiles());
}

/** Returns information about bitcode object files resulting from compilation. */
public LtoCompilationContext getLtoCompilationContext() {
return ltoCompilationContext;
Expand Down Expand Up @@ -209,6 +220,11 @@ public Iterable<Artifact> getHeaderTokenFiles() {
return headerTokenFiles;
}

/** Returns an unmodifiable view of the .pcm files. */
public Iterable<Artifact> getModuleFiles() {
return moduleFiles;
}

/** Returns the output files that are considered "compiled" by this C++ compile action. */
NestedSet<Artifact> getFilesToCompile(boolean parseHeaders, boolean usePic) {
NestedSetBuilder<Artifact> files = NestedSetBuilder.stableOrder();
Expand Down Expand Up @@ -236,6 +252,7 @@ public static final class Builder {
private final Set<Artifact> picGcnoFiles = new LinkedHashSet<>();
private final NestedSetBuilder<Artifact> temps = NestedSetBuilder.stableOrder();
private final Set<Artifact> headerTokenFiles = new LinkedHashSet<>();
private final Set<Artifact> moduleFiles = new LinkedHashSet<>();

private Builder() {
// private to avoid class initialization deadlock between this class and its outer class
Expand All @@ -251,7 +268,8 @@ public CcCompilationOutputs build() {
ImmutableList.copyOf(gcnoFiles),
ImmutableList.copyOf(picGcnoFiles),
temps.build(),
ImmutableList.copyOf(headerTokenFiles));
ImmutableList.copyOf(headerTokenFiles),
ImmutableList.copyOf(moduleFiles));
}

@CanIgnoreReturnValue
Expand All @@ -264,6 +282,7 @@ public Builder merge(CcCompilationOutputs outputs) {
this.picGcnoFiles.addAll(outputs.picGcnoFiles);
this.temps.addTransitive(outputs.temps);
this.headerTokenFiles.addAll(outputs.headerTokenFiles);
this.moduleFiles.addAll(outputs.moduleFiles);
this.ltoCompilationContext.addAll(outputs.ltoCompilationContext);
return this;
}
Expand Down Expand Up @@ -356,5 +375,11 @@ public Builder addHeaderTokenFile(Artifact artifact) {
headerTokenFiles.add(artifact);
return this;
}

@CanIgnoreReturnValue
public Builder addModuleFile(Artifact artifact) {
moduleFiles.add(artifact);
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ Depset getStarlarkFilesToCompile(boolean parseHeaders, boolean usePic, StarlarkT
@StarlarkMethod(name = "header_tokens", documented = false, useStarlarkThread = true)
Sequence<FileT> getStarlarkHeaderTokens(StarlarkThread thread) throws EvalException;

@StarlarkMethod(name = "module_files", documented = false, useStarlarkThread = true)
Sequence<FileT> getStarlarkModuleFiles(StarlarkThread thread) throws EvalException;

@StarlarkMethod(name = "lto_compilation_context", documented = false, useStarlarkThread = true)
Object getLtoCompilationContextForStarlark(StarlarkThread thread) throws EvalException;

Expand Down
7 changes: 4 additions & 3 deletions src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@

"""Utility functions for C++ rules."""

load(":common/objc/semantics.bzl", objc_semantics = "semantics")
load(":common/paths.bzl", "paths")
load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/cc/cc_common.bzl", "cc_common")
load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/objc/objc_common.bzl", "objc_common")
load(":common/objc/semantics.bzl", objc_semantics = "semantics")
load(":common/paths.bzl", "paths")

cc_internal = _builtins.internal.cc_internal
CcNativeLibraryInfo = _builtins.internal.CcNativeLibraryInfo
Expand Down Expand Up @@ -285,6 +285,7 @@ def _build_output_groups_for_emitting_compile_providers(
)
output_groups_builder["compilation_outputs"] = files_to_compile
output_groups_builder["compilation_prerequisites_INTERNAL_"] = _collect_compilation_prerequisites(ctx = ctx, compilation_context = compilation_context)
output_groups_builder["module_files"] = depset(compilation_outputs.module_files())

if generate_hidden_top_level_group:
output_groups_builder["_hidden_top_level_INTERNAL_"] = _collect_library_hidden_top_level_artifacts(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,23 @@ public void testSharedAndDynamicLibraryOutputGroups() throws Exception {
assertThat(ActionsTestUtil.prettyArtifactNames(getFilesToBuild(groupDynamic)))
.contains("a/liblib.so");
}

@Test
public void testModuleOutputGroups() throws Exception {
getAnalysisMock()
.ccSupport()
.setupCcToolchainConfig(
mockToolsConfig,
CcToolchainConfig.builder().withFeatures("header_modules_feature_configuration"));
scratch.file("header.h");
scratch.file(
"a/BUILD",
"cc_library(name='lib', hdrs=['src.h'], features=['header_modules'])",
"filegroup(name='group_modules', srcs=[':lib'], output_group = 'module_files')");

ConfiguredTarget groupArchive = getConfiguredTarget("//a:group_modules");

assertThat(ActionsTestUtil.prettyArtifactNames(getFilesToBuild(groupArchive)))
.containsExactly("a/_objs/lib/lib.pcm");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7207,7 +7207,8 @@ public void testExpandedCcCompilationOutputsApiRaisesError() throws Exception {
ImmutableList.of(
"comp_outputs.temps()",
"comp_outputs.files_to_compile(parse_headers=False, use_pic=True)",
"comp_outputs.header_tokens()");
"comp_outputs.header_tokens()",
"comp_outputs.module_files()");
for (String call : calls) {
scratch.overwriteFile(
"b/rule.bzl",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,8 @@ public void testOutputGroupsAsDictionary() throws Exception {
OutputGroupInfo.COMPILATION_PREREQUISITES,
OutputGroupInfo.FILES_TO_COMPILE,
OutputGroupInfo.TEMP_FILES,
OutputGroupInfo.VALIDATION);
OutputGroupInfo.VALIDATION,
"module_files");
}

@Test
Expand Down

0 comments on commit 9b30bf7

Please sign in to comment.