diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java index b5be413ffd1d03..23388bff631ae2 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationHelper.java @@ -1626,6 +1626,9 @@ private ImmutableList createSourceAction( if (gcnoFile != null) { result.addPicGcnoFile(gcnoFile); } + if (outputCategory == ArtifactCategory.CPP_MODULE) { + result.addModuleFile(picAction.getPrimaryOutput()); + } } if (generateNoPicAction) { @@ -1699,6 +1702,9 @@ private ImmutableList createSourceAction( if (gcnoFile != null) { result.addGcnoFile(gcnoFile); } + if (outputCategory == ArtifactCategory.CPP_MODULE) { + result.addModuleFile(compileAction.getPrimaryOutput()); + } } return directOutputs.build(); } diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationOutputs.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationOutputs.java index 5df5b73319532c..b65048cb29cd62 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationOutputs.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCompilationOutputs.java @@ -76,6 +76,9 @@ public class CcCompilationOutputs implements CcCompilationOutputsApi { */ private final ImmutableList headerTokenFiles; + /** All .pcm files built by the target. */ + private final ImmutableList moduleFiles; + private CcCompilationOutputs( ImmutableList objectFiles, ImmutableList picObjectFiles, @@ -85,7 +88,8 @@ private CcCompilationOutputs( ImmutableList gcnoFiles, ImmutableList picGcnoFiles, NestedSet temps, - ImmutableList headerTokenFiles) { + ImmutableList headerTokenFiles, + ImmutableList moduleFiles) { this.objectFiles = objectFiles; this.picObjectFiles = picObjectFiles; this.ltoCompilationContext = ltoCompilationContext; @@ -95,6 +99,7 @@ private CcCompilationOutputs( this.picGcnoFiles = picGcnoFiles; this.temps = temps; this.headerTokenFiles = headerTokenFiles; + this.moduleFiles = moduleFiles; } /** @@ -135,6 +140,12 @@ public Sequence getStarlarkHeaderTokens(StarlarkThread thread) throws return StarlarkList.immutableCopyOf(getHeaderTokenFiles()); } + @Override + public Sequence 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; @@ -209,6 +220,11 @@ public Iterable getHeaderTokenFiles() { return headerTokenFiles; } + /** Returns an unmodifiable view of the .pcm files. */ + public Iterable getModuleFiles() { + return moduleFiles; + } + /** Returns the output files that are considered "compiled" by this C++ compile action. */ NestedSet getFilesToCompile(boolean parseHeaders, boolean usePic) { NestedSetBuilder files = NestedSetBuilder.stableOrder(); @@ -236,6 +252,7 @@ public static final class Builder { private final Set picGcnoFiles = new LinkedHashSet<>(); private final NestedSetBuilder temps = NestedSetBuilder.stableOrder(); private final Set headerTokenFiles = new LinkedHashSet<>(); + private final Set moduleFiles = new LinkedHashSet<>(); private Builder() { // private to avoid class initialization deadlock between this class and its outer class @@ -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 @@ -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; } @@ -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; + } } } diff --git a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/CcCompilationOutputsApi.java b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/CcCompilationOutputsApi.java index 75286805ad6778..4e7e93597cff26 100644 --- a/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/CcCompilationOutputsApi.java +++ b/src/main/java/com/google/devtools/build/lib/starlarkbuildapi/cpp/CcCompilationOutputsApi.java @@ -74,6 +74,9 @@ Depset getStarlarkFilesToCompile(boolean parseHeaders, boolean usePic, StarlarkT @StarlarkMethod(name = "header_tokens", documented = false, useStarlarkThread = true) Sequence getStarlarkHeaderTokens(StarlarkThread thread) throws EvalException; + @StarlarkMethod(name = "module_files", documented = false, useStarlarkThread = true) + Sequence getStarlarkModuleFiles(StarlarkThread thread) throws EvalException; + @StarlarkMethod(name = "lto_compilation_context", documented = false, useStarlarkThread = true) Object getLtoCompilationContextForStarlark(StarlarkThread thread) throws EvalException; diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl index 93dbc230613842..405c33bceb0f67 100644 --- a/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl @@ -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 @@ -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( diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/CppOutputGroupsTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/CppOutputGroupsTest.java index 7350e04546101f..a7363f05529279 100644 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/CppOutputGroupsTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/CppOutputGroupsTest.java @@ -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"); + } } diff --git a/src/test/java/com/google/devtools/build/lib/rules/cpp/StarlarkCcCommonTest.java b/src/test/java/com/google/devtools/build/lib/rules/cpp/StarlarkCcCommonTest.java index 514564975d4a0e..b1469e32c449bd 100755 --- a/src/test/java/com/google/devtools/build/lib/rules/cpp/StarlarkCcCommonTest.java +++ b/src/test/java/com/google/devtools/build/lib/rules/cpp/StarlarkCcCommonTest.java @@ -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", diff --git a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkIntegrationTest.java b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkIntegrationTest.java index 97b514f18c116c..0fcca7887bb331 100644 --- a/src/test/java/com/google/devtools/build/lib/starlark/StarlarkIntegrationTest.java +++ b/src/test/java/com/google/devtools/build/lib/starlark/StarlarkIntegrationTest.java @@ -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