Skip to content

Commit c449a82

Browse files
c-mitacopybara-github
authored andcommitted
Internal change
PiperOrigin-RevId: 495012726 Change-Id: Ib02f5c6f844fa42b57e249dc37cd0f413626c8e0
1 parent de4746d commit c449a82

File tree

10 files changed

+140
-43
lines changed

10 files changed

+140
-43
lines changed

src/main/java/com/google/devtools/build/lib/analysis/test/CoverageCommon.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public InstrumentedFilesInfoApi instrumentedFilesInfo(
5656
supportFiles, // Depset<Artifact>|Sequence<Artifact|Depset<Artifact>|FilesToRunProvider>
5757
Dict<?, ?> environment, // <String, String>
5858
Object extensions,
59+
Sequence<?> metadataFiles, // Sequence<Artifact>
5960
StarlarkThread thread)
6061
throws EvalException, TypeException {
6162
List<String> extensionsList =
@@ -96,13 +97,17 @@ public InstrumentedFilesInfoApi instrumentedFilesInfo(
9697
if (!supportFilesBuilder.isEmpty() || !environmentPairs.isEmpty()) {
9798
BuiltinRestriction.throwIfNotBuiltinUsage(thread);
9899
}
100+
if (!metadataFiles.isEmpty()) {
101+
BuiltinRestriction.throwIfNotBuiltinUsage(thread);
102+
}
99103
return createInstrumentedFilesInfo(
100104
starlarkRuleContext.getRuleContext(),
101105
Sequence.cast(sourceAttributes, String.class, "source_attributes"),
102106
Sequence.cast(dependencyAttributes, String.class, "dependency_attributes"),
103107
supportFilesBuilder.build(),
104108
NestedSetBuilder.wrap(Order.COMPILE_ORDER, environmentPairs),
105-
extensionsList);
109+
extensionsList,
110+
Sequence.cast(metadataFiles, Artifact.class, "metadata_files"));
106111
}
107112

108113
/**
@@ -130,7 +135,8 @@ public static InstrumentedFilesInfo createInstrumentedFilesInfo(
130135
dependencyAttributes,
131136
NestedSetBuilder.emptySet(Order.STABLE_ORDER),
132137
NestedSetBuilder.emptySet(Order.STABLE_ORDER),
133-
extensions);
138+
extensions,
139+
null);
134140
}
135141

136142
private static InstrumentedFilesInfo createInstrumentedFilesInfo(
@@ -139,7 +145,8 @@ private static InstrumentedFilesInfo createInstrumentedFilesInfo(
139145
List<String> dependencyAttributes,
140146
NestedSet<Artifact> supportFiles,
141147
NestedSet<Pair<String, String>> environment,
142-
@Nullable List<String> extensions) {
148+
@Nullable List<String> extensions,
149+
@Nullable List<Artifact> metadataFiles) {
143150
FileTypeSet fileTypeSet = FileTypeSet.ANY_FILE;
144151
if (extensions != null) {
145152
if (extensions.isEmpty()) {
@@ -158,11 +165,12 @@ private static InstrumentedFilesInfo createInstrumentedFilesInfo(
158165
ruleContext,
159166
instrumentationSpec,
160167
InstrumentedFilesCollector.NO_METADATA_COLLECTOR,
161-
/* rootFiles = */ ImmutableList.of(),
162-
/* coverageSupportFiles = */ supportFiles,
163-
/* coverageEnvironment = */ environment,
164-
/* withBaselineCoverage = */ !TargetUtils.isTestRule(ruleContext.getTarget()),
165-
/* reportedToActualSources= */ NestedSetBuilder.create(Order.STABLE_ORDER));
168+
/* rootFiles= */ ImmutableList.of(),
169+
/* coverageSupportFiles= */ supportFiles,
170+
/* coverageEnvironment= */ environment,
171+
/* withBaselineCoverage= */ !TargetUtils.isTestRule(ruleContext.getTarget()),
172+
/* reportedToActualSources= */ NestedSetBuilder.create(Order.STABLE_ORDER),
173+
/* additionalMetadata= */ metadataFiles);
166174
}
167175

168176
@Override

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

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import com.google.devtools.build.lib.cmdline.PackageIdentifier;
4747
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
4848
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
49-
import com.google.devtools.build.lib.collect.nestedset.Order;
5049
import com.google.devtools.build.lib.packages.BuildType;
5150
import com.google.devtools.build.lib.packages.Rule;
5251
import com.google.devtools.build.lib.packages.RuleClass;
@@ -779,23 +778,6 @@ Artifact getDefParser() {
779778
return ruleContext.getPrerequisiteArtifact("$def_parser");
780779
}
781780

782-
@StarlarkMethod(
783-
name = "instrumented_files_info",
784-
documented = false,
785-
parameters = {
786-
@Param(name = "files", positional = false, named = true),
787-
@Param(name = "with_base_line_coverage", positional = false, named = true),
788-
})
789-
public InstrumentedFilesInfo getInstrumentedFilesProviderForStarlark(
790-
Sequence<?> files, boolean withBaselineCoverage) throws EvalException {
791-
try {
792-
return getInstrumentedFilesProvider(
793-
Sequence.cast(files, Artifact.class, "files"), withBaselineCoverage);
794-
} catch (RuleErrorException e) {
795-
throw new EvalException(e);
796-
}
797-
}
798-
799781
@StarlarkMethod(
800782
name = "instrumented_files_info_from_compilation_context",
801783
documented = false,
@@ -827,16 +809,6 @@ public InstrumentedFilesInfo getInstrumentedFilesProviderFromCompilationContextF
827809
}
828810
}
829811

830-
/** Provides support for instrumentation. */
831-
public InstrumentedFilesInfo getInstrumentedFilesProvider(
832-
Iterable<Artifact> files, boolean withBaselineCoverage) throws RuleErrorException {
833-
return getInstrumentedFilesProvider(
834-
files,
835-
withBaselineCoverage,
836-
/* virtualToOriginalHeaders= */ NestedSetBuilder.emptySet(Order.STABLE_ORDER),
837-
/* additionalMetadata= */ null);
838-
}
839-
840812
public InstrumentedFilesInfo getInstrumentedFilesProvider(
841813
Iterable<Artifact> files,
842814
boolean withBaselineCoverage,

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
@@ -2012,6 +2012,9 @@ private ImmutableList<Artifact> createSourceAction(
20122012
// Host targets don't produce .dwo files.
20132013
result.addPicDwoFile(dwoFile);
20142014
}
2015+
if (gcnoFile != null) {
2016+
result.addPicGcnoFile(gcnoFile);
2017+
}
20152018
}
20162019

20172020
if (generateNoPicAction) {
@@ -2080,6 +2083,9 @@ private ImmutableList<Artifact> createSourceAction(
20802083
// Host targets don't produce .dwo files.
20812084
result.addDwoFile(noPicDwoFile);
20822085
}
2086+
if (gcnoFile != null) {
2087+
result.addGcnoFile(gcnoFile);
2088+
}
20832089
}
20842090
return directOutputs.build();
20852091
}

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ public class CcCompilationOutputs implements CcCompilationOutputsApi<Artifact> {
6060
*/
6161
private final ImmutableList<Artifact> picDwoFiles;
6262

63+
/** All .gcno files built by the target, corresponding to .o outputs. */
64+
private final ImmutableList<Artifact> gcnoFiles;
65+
66+
/** All .pic.gcno files built by the target, corresponding to .pic.gcno outputs. */
67+
private final ImmutableList<Artifact> picGcnoFiles;
68+
6369
/**
6470
* All artifacts that are created if "--save_temps" is true.
6571
*/
@@ -76,13 +82,17 @@ private CcCompilationOutputs(
7682
LtoCompilationContext ltoCompilationContext,
7783
ImmutableList<Artifact> dwoFiles,
7884
ImmutableList<Artifact> picDwoFiles,
85+
ImmutableList<Artifact> gcnoFiles,
86+
ImmutableList<Artifact> picGcnoFiles,
7987
NestedSet<Artifact> temps,
8088
ImmutableList<Artifact> headerTokenFiles) {
8189
this.objectFiles = objectFiles;
8290
this.picObjectFiles = picObjectFiles;
8391
this.ltoCompilationContext = ltoCompilationContext;
8492
this.dwoFiles = dwoFiles;
8593
this.picDwoFiles = picDwoFiles;
94+
this.gcnoFiles = gcnoFiles;
95+
this.picGcnoFiles = picGcnoFiles;
8696
this.temps = temps;
8797
this.headerTokenFiles = headerTokenFiles;
8898
}
@@ -170,6 +180,28 @@ public ImmutableList<Artifact> getPicDwoFiles() {
170180
return picDwoFiles;
171181
}
172182

183+
@Override
184+
public Sequence<Artifact> getStarlarkGcnoFiles(StarlarkThread thread) throws EvalException {
185+
CcModule.checkPrivateStarlarkificationAllowlist(thread);
186+
return StarlarkList.immutableCopyOf(getGcnoFiles());
187+
}
188+
189+
@Override
190+
public Sequence<Artifact> getStarlarkPicGcnoFiles(StarlarkThread thread) throws EvalException {
191+
CcModule.checkPrivateStarlarkificationAllowlist(thread);
192+
return StarlarkList.immutableCopyOf(getPicGcnoFiles());
193+
}
194+
195+
/** Returns an unmodifiable view of the .gcno files set. */
196+
public ImmutableList<Artifact> getGcnoFiles() {
197+
return gcnoFiles;
198+
}
199+
200+
/** Returns an unmodifiable view of the .pic.gcno files set. */
201+
public ImmutableList<Artifact> getPicGcnoFiles() {
202+
return picGcnoFiles;
203+
}
204+
173205
/**
174206
* Returns an unmodifiable view of the temp files set.
175207
*/
@@ -207,6 +239,8 @@ public static final class Builder {
207239
new LtoCompilationContext.Builder();
208240
private final Set<Artifact> dwoFiles = new LinkedHashSet<>();
209241
private final Set<Artifact> picDwoFiles = new LinkedHashSet<>();
242+
private final Set<Artifact> gcnoFiles = new LinkedHashSet<>();
243+
private final Set<Artifact> picGcnoFiles = new LinkedHashSet<>();
210244
private final NestedSetBuilder<Artifact> temps = NestedSetBuilder.stableOrder();
211245
private final Set<Artifact> headerTokenFiles = new LinkedHashSet<>();
212246

@@ -221,6 +255,8 @@ public CcCompilationOutputs build() {
221255
ltoCompilationContext.build(),
222256
ImmutableList.copyOf(dwoFiles),
223257
ImmutableList.copyOf(picDwoFiles),
258+
ImmutableList.copyOf(gcnoFiles),
259+
ImmutableList.copyOf(picGcnoFiles),
224260
temps.build(),
225261
ImmutableList.copyOf(headerTokenFiles));
226262
}
@@ -231,6 +267,8 @@ public Builder merge(CcCompilationOutputs outputs) {
231267
this.picObjectFiles.addAll(outputs.picObjectFiles);
232268
this.dwoFiles.addAll(outputs.dwoFiles);
233269
this.picDwoFiles.addAll(outputs.picDwoFiles);
270+
this.gcnoFiles.addAll(outputs.gcnoFiles);
271+
this.picGcnoFiles.addAll(outputs.picGcnoFiles);
234272
this.temps.addTransitive(outputs.temps);
235273
this.headerTokenFiles.addAll(outputs.headerTokenFiles);
236274
this.ltoCompilationContext.addAll(outputs.ltoCompilationContext);
@@ -301,6 +339,18 @@ public Builder addPicDwoFile(Artifact artifact) {
301339
return this;
302340
}
303341

342+
@CanIgnoreReturnValue
343+
public Builder addGcnoFile(Artifact artifact) {
344+
gcnoFiles.add(artifact);
345+
return this;
346+
}
347+
348+
@CanIgnoreReturnValue
349+
public Builder addPicGcnoFile(Artifact artifact) {
350+
picGcnoFiles.add(artifact);
351+
return this;
352+
}
353+
304354
/** Adds temp files. */
305355
@CanIgnoreReturnValue
306356
public Builder addTemps(Iterable<Artifact> artifacts) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,7 @@ public boolean generateLlvmLcovStarlark(StarlarkThread thread) throws EvalExcept
882882
return generateLlvmLCov();
883883
}
884884

885+
@Nullable
885886
@Override
886887
public String fdoInstrumentStarlark(StarlarkThread thread) throws EvalException {
887888
checkInExpandedApiAllowlist(thread, "fdo_instrument");

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,10 @@ Depset getStarlarkFilesToCompile(boolean parseHeaders, boolean usePic, StarlarkT
8282

8383
@StarlarkMethod(name = "pic_dwo_files", documented = false, useStarlarkThread = true)
8484
Sequence<FileT> getStarlarkPicDwoFiles(StarlarkThread thread) throws EvalException;
85+
86+
@StarlarkMethod(name = "gcno_files", documented = false, useStarlarkThread = true)
87+
Sequence<FileT> getStarlarkGcnoFiles(StarlarkThread thread) throws EvalException;
88+
89+
@StarlarkMethod(name = "pic_gcno_files", documented = false, useStarlarkThread = true)
90+
Sequence<FileT> getStarlarkPicGcnoFiles(StarlarkThread thread) throws EvalException;
8591
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ public interface CppConfigurationApi<InvalidConfigurationExceptionT extends Exce
130130
@StarlarkMethod(name = "generate_llvm_lcov", documented = false, useStarlarkThread = true)
131131
boolean generateLlvmLcovStarlark(StarlarkThread thread) throws EvalException;
132132

133-
@StarlarkMethod(name = "fdo_instrument", documented = false, useStarlarkThread = true)
133+
@Nullable
134+
@StarlarkMethod(
135+
name = "fdo_instrument",
136+
documented = false,
137+
useStarlarkThread = true,
138+
allowReturnNones = true)
134139
String fdoInstrumentStarlark(StarlarkThread thread) throws EvalException;
135140

136141
@StarlarkMethod(

src/main/java/com/google/devtools/build/lib/starlarkbuildapi/test/CoverageCommonApi.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ public interface CoverageCommonApi<
9595
positional = false,
9696
named = true,
9797
defaultValue = "None"),
98+
@Param(
99+
name = "metadata_files",
100+
named = true,
101+
positional = false,
102+
documented = false,
103+
defaultValue = "[]",
104+
allowedTypes = {
105+
@ParamType(type = Sequence.class, generic1 = FileApi.class),
106+
})
98107
},
99108
useStarlarkThread = true)
100109
InstrumentedFilesInfoApi instrumentedFilesInfo(
@@ -104,6 +113,7 @@ InstrumentedFilesInfoApi instrumentedFilesInfo(
104113
Object supportFiles, // Sequence or Depset of <FileApi> expected
105114
Dict<?, ?> environment, // <String, String>
106115
Object extensions,
116+
Sequence<?> metadataFiles,
107117
StarlarkThread thread)
108118
throws EvalException, TypeException;
109119
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ cc_common = _builtins.toplevel.cc_common
2121
cc_internal = _builtins.internal.cc_internal
2222
CcNativeLibraryInfo = _builtins.internal.CcNativeLibraryInfo
2323
config_common = _builtins.toplevel.config_common
24+
coverage_common = _builtins.toplevel.coverage_common
2425
platform_common = _builtins.toplevel.platform_common
2526

2627
artifact_category = struct(
@@ -1165,6 +1166,43 @@ def _system_include_dirs(ctx, additional_make_variable_substitutions):
11651166
result.append(_get_relative(ctx.bin_dir.path, out_includes_path))
11661167
return result
11671168

1169+
def _get_coverage_environment(ctx, cc_config, cc_toolchain):
1170+
if not ctx.configuration.coverage_enabled:
1171+
return {}
1172+
env = {
1173+
"COVERAGE_GCOV_PATH": cc_toolchain.tool_path(tool = "GCOV"),
1174+
"LLVM_COV": cc_toolchain.tool_path(tool = "LLVM_COV"),
1175+
"LLVM_PROFDATA": cc_toolchain.tool_path(tool = "LLVM_PROFDATA"),
1176+
"GENERATE_LLVM_LCOV": "1" if cc_config.generate_llvm_lcov() else "0",
1177+
}
1178+
for k in list(env.keys()):
1179+
if env[k] == None:
1180+
env[k] = ""
1181+
if cc_config.fdo_instrument():
1182+
env["FDO_DIR"] = cc_config.fdo_instrument()
1183+
return env
1184+
1185+
def _create_cc_instrumented_files_info(ctx, cc_config, cc_toolchain, metadata_files):
1186+
extensions = CC_SOURCE + \
1187+
C_SOURCE + \
1188+
CC_HEADER + \
1189+
ASSESMBLER_WITH_C_PREPROCESSOR + \
1190+
ASSEMBLER
1191+
coverage_environment = {}
1192+
if ctx.coverage_instrumented():
1193+
coverage_environment = _get_coverage_environment(ctx, cc_config, cc_toolchain)
1194+
coverage_support_files = cc_toolchain.coverage_files() if ctx.coverage_instrumented() else depset([])
1195+
info = coverage_common.instrumented_files_info(
1196+
ctx = ctx,
1197+
source_attributes = ["srcs", "hdrs"],
1198+
dependency_attributes = ["implementation_deps", "deps", "data"],
1199+
extensions = extensions,
1200+
metadata_files = metadata_files,
1201+
coverage_support_files = coverage_support_files,
1202+
coverage_environment = coverage_environment,
1203+
)
1204+
return info
1205+
11681206
cc_helper = struct(
11691207
merge_cc_debug_contexts = _merge_cc_debug_contexts,
11701208
is_code_coverage_enabled = _is_code_coverage_enabled,
@@ -1217,4 +1255,6 @@ cc_helper = struct(
12171255
get_public_hdrs = _get_public_hdrs,
12181256
report_invalid_options = _report_invalid_options,
12191257
system_include_dirs = _system_include_dirs,
1258+
get_coverage_environment = _get_coverage_environment,
1259+
create_cc_instrumented_files_info = _create_cc_instrumented_files_info,
12201260
)

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,11 @@ def _cc_library_impl(ctx):
256256
elif artifacts_to_build.interface_library != None:
257257
files_builder.append(artifacts_to_build.interface_library)
258258

259-
instrumented_object_files = []
260-
instrumented_object_files.extend(compilation_outputs.objects)
261-
instrumented_object_files.extend(compilation_outputs.pic_objects)
262-
instrumented_files_info = common.instrumented_files_info(
263-
files = instrumented_object_files,
264-
with_base_line_coverage = True,
259+
instrumented_files_info = cc_helper.create_cc_instrumented_files_info(
260+
ctx = ctx,
261+
cc_config = ctx.fragments.cpp,
262+
cc_toolchain = cc_toolchain,
263+
metadata_files = compilation_outputs.gcno_files() + compilation_outputs.pic_gcno_files(),
265264
)
266265

267266
runfiles_list = []

0 commit comments

Comments
 (0)