Skip to content

Commit 9b30bf7

Browse files
sam-mccallcopybara-github
authored andcommitted
Add module_files output group, used to build PCMs from cc_library etc 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
1 parent ac10bac commit 9b30bf7

7 files changed

Lines changed: 63 additions & 7 deletions

File tree

src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,6 +1626,9 @@ private ImmutableList<Artifact> createSourceAction(
16261626
if (gcnoFile != null) {
16271627
result.addPicGcnoFile(gcnoFile);
16281628
}
1629+
if (outputCategory == ArtifactCategory.CPP_MODULE) {
1630+
result.addModuleFile(picAction.getPrimaryOutput());
1631+
}
16291632
}
16301633

16311634
if (generateNoPicAction) {
@@ -1699,6 +1702,9 @@ private ImmutableList<Artifact> createSourceAction(
16991702
if (gcnoFile != null) {
17001703
result.addGcnoFile(gcnoFile);
17011704
}
1705+
if (outputCategory == ArtifactCategory.CPP_MODULE) {
1706+
result.addModuleFile(compileAction.getPrimaryOutput());
1707+
}
17021708
}
17031709
return directOutputs.build();
17041710
}

src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationOutputs.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ public class CcCompilationOutputs implements CcCompilationOutputsApi<Artifact> {
7676
*/
7777
private final ImmutableList<Artifact> headerTokenFiles;
7878

79+
/** All .pcm files built by the target. */
80+
private final ImmutableList<Artifact> moduleFiles;
81+
7982
private CcCompilationOutputs(
8083
ImmutableList<Artifact> objectFiles,
8184
ImmutableList<Artifact> picObjectFiles,
@@ -85,7 +88,8 @@ private CcCompilationOutputs(
8588
ImmutableList<Artifact> gcnoFiles,
8689
ImmutableList<Artifact> picGcnoFiles,
8790
NestedSet<Artifact> temps,
88-
ImmutableList<Artifact> headerTokenFiles) {
91+
ImmutableList<Artifact> headerTokenFiles,
92+
ImmutableList<Artifact> moduleFiles) {
8993
this.objectFiles = objectFiles;
9094
this.picObjectFiles = picObjectFiles;
9195
this.ltoCompilationContext = ltoCompilationContext;
@@ -95,6 +99,7 @@ private CcCompilationOutputs(
9599
this.picGcnoFiles = picGcnoFiles;
96100
this.temps = temps;
97101
this.headerTokenFiles = headerTokenFiles;
102+
this.moduleFiles = moduleFiles;
98103
}
99104

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

143+
@Override
144+
public Sequence<Artifact> getStarlarkModuleFiles(StarlarkThread thread) throws EvalException {
145+
CcModule.checkPrivateStarlarkificationAllowlist(thread);
146+
return StarlarkList.immutableCopyOf(getModuleFiles());
147+
}
148+
138149
/** Returns information about bitcode object files resulting from compilation. */
139150
public LtoCompilationContext getLtoCompilationContext() {
140151
return ltoCompilationContext;
@@ -209,6 +220,11 @@ public Iterable<Artifact> getHeaderTokenFiles() {
209220
return headerTokenFiles;
210221
}
211222

223+
/** Returns an unmodifiable view of the .pcm files. */
224+
public Iterable<Artifact> getModuleFiles() {
225+
return moduleFiles;
226+
}
227+
212228
/** Returns the output files that are considered "compiled" by this C++ compile action. */
213229
NestedSet<Artifact> getFilesToCompile(boolean parseHeaders, boolean usePic) {
214230
NestedSetBuilder<Artifact> files = NestedSetBuilder.stableOrder();
@@ -236,6 +252,7 @@ public static final class Builder {
236252
private final Set<Artifact> picGcnoFiles = new LinkedHashSet<>();
237253
private final NestedSetBuilder<Artifact> temps = NestedSetBuilder.stableOrder();
238254
private final Set<Artifact> headerTokenFiles = new LinkedHashSet<>();
255+
private final Set<Artifact> moduleFiles = new LinkedHashSet<>();
239256

240257
private Builder() {
241258
// private to avoid class initialization deadlock between this class and its outer class
@@ -251,7 +268,8 @@ public CcCompilationOutputs build() {
251268
ImmutableList.copyOf(gcnoFiles),
252269
ImmutableList.copyOf(picGcnoFiles),
253270
temps.build(),
254-
ImmutableList.copyOf(headerTokenFiles));
271+
ImmutableList.copyOf(headerTokenFiles),
272+
ImmutableList.copyOf(moduleFiles));
255273
}
256274

257275
@CanIgnoreReturnValue
@@ -264,6 +282,7 @@ public Builder merge(CcCompilationOutputs outputs) {
264282
this.picGcnoFiles.addAll(outputs.picGcnoFiles);
265283
this.temps.addTransitive(outputs.temps);
266284
this.headerTokenFiles.addAll(outputs.headerTokenFiles);
285+
this.moduleFiles.addAll(outputs.moduleFiles);
267286
this.ltoCompilationContext.addAll(outputs.ltoCompilationContext);
268287
return this;
269288
}
@@ -356,5 +375,11 @@ public Builder addHeaderTokenFile(Artifact artifact) {
356375
headerTokenFiles.add(artifact);
357376
return this;
358377
}
378+
379+
@CanIgnoreReturnValue
380+
public Builder addModuleFile(Artifact artifact) {
381+
moduleFiles.add(artifact);
382+
return this;
383+
}
359384
}
360385
}

src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/CcCompilationOutputsApi.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ Depset getStarlarkFilesToCompile(boolean parseHeaders, boolean usePic, StarlarkT
7474
@StarlarkMethod(name = "header_tokens", documented = false, useStarlarkThread = true)
7575
Sequence<FileT> getStarlarkHeaderTokens(StarlarkThread thread) throws EvalException;
7676

77+
@StarlarkMethod(name = "module_files", documented = false, useStarlarkThread = true)
78+
Sequence<FileT> getStarlarkModuleFiles(StarlarkThread thread) throws EvalException;
79+
7780
@StarlarkMethod(name = "lto_compilation_context", documented = false, useStarlarkThread = true)
7881
Object getLtoCompilationContextForStarlark(StarlarkThread thread) throws EvalException;
7982

src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414

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

17-
load(":common/objc/semantics.bzl", objc_semantics = "semantics")
18-
load(":common/paths.bzl", "paths")
19-
load(":common/cc/cc_info.bzl", "CcInfo")
2017
load(":common/cc/cc_common.bzl", "cc_common")
18+
load(":common/cc/cc_info.bzl", "CcInfo")
2119
load(":common/objc/objc_common.bzl", "objc_common")
20+
load(":common/objc/semantics.bzl", objc_semantics = "semantics")
21+
load(":common/paths.bzl", "paths")
2222

2323
cc_internal = _builtins.internal.cc_internal
2424
CcNativeLibraryInfo = _builtins.internal.CcNativeLibraryInfo
@@ -285,6 +285,7 @@ def _build_output_groups_for_emitting_compile_providers(
285285
)
286286
output_groups_builder["compilation_outputs"] = files_to_compile
287287
output_groups_builder["compilation_prerequisites_INTERNAL_"] = _collect_compilation_prerequisites(ctx = ctx, compilation_context = compilation_context)
288+
output_groups_builder["module_files"] = depset(compilation_outputs.module_files())
288289

289290
if generate_hidden_top_level_group:
290291
output_groups_builder["_hidden_top_level_INTERNAL_"] = _collect_library_hidden_top_level_artifacts(

src/test/java/com/google/devtools/build/lib/rules/cpp/CppOutputGroupsTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,23 @@ public void testSharedAndDynamicLibraryOutputGroups() throws Exception {
121121
assertThat(ActionsTestUtil.prettyArtifactNames(getFilesToBuild(groupDynamic)))
122122
.contains("a/liblib.so");
123123
}
124+
125+
@Test
126+
public void testModuleOutputGroups() throws Exception {
127+
getAnalysisMock()
128+
.ccSupport()
129+
.setupCcToolchainConfig(
130+
mockToolsConfig,
131+
CcToolchainConfig.builder().withFeatures("header_modules_feature_configuration"));
132+
scratch.file("header.h");
133+
scratch.file(
134+
"a/BUILD",
135+
"cc_library(name='lib', hdrs=['src.h'], features=['header_modules'])",
136+
"filegroup(name='group_modules', srcs=[':lib'], output_group = 'module_files')");
137+
138+
ConfiguredTarget groupArchive = getConfiguredTarget("//a:group_modules");
139+
140+
assertThat(ActionsTestUtil.prettyArtifactNames(getFilesToBuild(groupArchive)))
141+
.containsExactly("a/_objs/lib/lib.pcm");
142+
}
124143
}

src/test/java/com/google/devtools/build/lib/rules/cpp/StarlarkCcCommonTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7207,7 +7207,8 @@ public void testExpandedCcCompilationOutputsApiRaisesError() throws Exception {
72077207
ImmutableList.of(
72087208
"comp_outputs.temps()",
72097209
"comp_outputs.files_to_compile(parse_headers=False, use_pic=True)",
7210-
"comp_outputs.header_tokens()");
7210+
"comp_outputs.header_tokens()",
7211+
"comp_outputs.module_files()");
72117212
for (String call : calls) {
72127213
scratch.overwriteFile(
72137214
"b/rule.bzl",

src/test/java/com/google/devtools/build/lib/starlark/StarlarkIntegrationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ public void testOutputGroupsAsDictionary() throws Exception {
359359
OutputGroupInfo.COMPILATION_PREREQUISITES,
360360
OutputGroupInfo.FILES_TO_COMPILE,
361361
OutputGroupInfo.TEMP_FILES,
362-
OutputGroupInfo.VALIDATION);
362+
OutputGroupInfo.VALIDATION,
363+
"module_files");
363364
}
364365

365366
@Test

0 commit comments

Comments
 (0)