Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 495012726
Change-Id: Ib02f5c6f844fa42b57e249dc37cd0f413626c8e0
  • Loading branch information
c-mita authored and hvadehra committed Feb 14, 2023
1 parent f3646cb commit adb6286
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public InstrumentedFilesInfoApi instrumentedFilesInfo(
supportFiles, // Depset<Artifact>|Sequence<Artifact|Depset<Artifact>|FilesToRunProvider>
Dict<?, ?> environment, // <String, String>
Object extensions,
Sequence<?> metadataFiles, // Sequence<Artifact>
StarlarkThread thread)
throws EvalException, TypeException {
List<String> extensionsList =
Expand Down Expand Up @@ -96,13 +97,17 @@ public InstrumentedFilesInfoApi instrumentedFilesInfo(
if (!supportFilesBuilder.isEmpty() || !environmentPairs.isEmpty()) {
BuiltinRestriction.throwIfNotBuiltinUsage(thread);
}
if (!metadataFiles.isEmpty()) {
BuiltinRestriction.throwIfNotBuiltinUsage(thread);
}
return createInstrumentedFilesInfo(
starlarkRuleContext.getRuleContext(),
Sequence.cast(sourceAttributes, String.class, "source_attributes"),
Sequence.cast(dependencyAttributes, String.class, "dependency_attributes"),
supportFilesBuilder.build(),
NestedSetBuilder.wrap(Order.COMPILE_ORDER, environmentPairs),
extensionsList);
extensionsList,
Sequence.cast(metadataFiles, Artifact.class, "metadata_files"));
}

/**
Expand Down Expand Up @@ -130,7 +135,8 @@ public static InstrumentedFilesInfo createInstrumentedFilesInfo(
dependencyAttributes,
NestedSetBuilder.emptySet(Order.STABLE_ORDER),
NestedSetBuilder.emptySet(Order.STABLE_ORDER),
extensions);
extensions,
null);
}

private static InstrumentedFilesInfo createInstrumentedFilesInfo(
Expand All @@ -139,7 +145,8 @@ private static InstrumentedFilesInfo createInstrumentedFilesInfo(
List<String> dependencyAttributes,
NestedSet<Artifact> supportFiles,
NestedSet<Pair<String, String>> environment,
@Nullable List<String> extensions) {
@Nullable List<String> extensions,
@Nullable List<Artifact> metadataFiles) {
FileTypeSet fileTypeSet = FileTypeSet.ANY_FILE;
if (extensions != null) {
if (extensions.isEmpty()) {
Expand All @@ -158,11 +165,12 @@ private static InstrumentedFilesInfo createInstrumentedFilesInfo(
ruleContext,
instrumentationSpec,
InstrumentedFilesCollector.NO_METADATA_COLLECTOR,
/* rootFiles = */ ImmutableList.of(),
/* coverageSupportFiles = */ supportFiles,
/* coverageEnvironment = */ environment,
/* withBaselineCoverage = */ !TargetUtils.isTestRule(ruleContext.getTarget()),
/* reportedToActualSources= */ NestedSetBuilder.create(Order.STABLE_ORDER));
/* rootFiles= */ ImmutableList.of(),
/* coverageSupportFiles= */ supportFiles,
/* coverageEnvironment= */ environment,
/* withBaselineCoverage= */ !TargetUtils.isTestRule(ruleContext.getTarget()),
/* reportedToActualSources= */ NestedSetBuilder.create(Order.STABLE_ORDER),
/* additionalMetadata= */ metadataFiles);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import com.google.devtools.build.lib.cmdline.PackageIdentifier;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder;
import com.google.devtools.build.lib.collect.nestedset.Order;
import com.google.devtools.build.lib.packages.BuildType;
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.packages.RuleClass;
Expand Down Expand Up @@ -779,23 +778,6 @@ Artifact getDefParser() {
return ruleContext.getPrerequisiteArtifact("$def_parser");
}

@StarlarkMethod(
name = "instrumented_files_info",
documented = false,
parameters = {
@Param(name = "files", positional = false, named = true),
@Param(name = "with_base_line_coverage", positional = false, named = true),
})
public InstrumentedFilesInfo getInstrumentedFilesProviderForStarlark(
Sequence<?> files, boolean withBaselineCoverage) throws EvalException {
try {
return getInstrumentedFilesProvider(
Sequence.cast(files, Artifact.class, "files"), withBaselineCoverage);
} catch (RuleErrorException e) {
throw new EvalException(e);
}
}

@StarlarkMethod(
name = "instrumented_files_info_from_compilation_context",
documented = false,
Expand Down Expand Up @@ -827,16 +809,6 @@ public InstrumentedFilesInfo getInstrumentedFilesProviderFromCompilationContextF
}
}

/** Provides support for instrumentation. */
public InstrumentedFilesInfo getInstrumentedFilesProvider(
Iterable<Artifact> files, boolean withBaselineCoverage) throws RuleErrorException {
return getInstrumentedFilesProvider(
files,
withBaselineCoverage,
/* virtualToOriginalHeaders= */ NestedSetBuilder.emptySet(Order.STABLE_ORDER),
/* additionalMetadata= */ null);
}

public InstrumentedFilesInfo getInstrumentedFilesProvider(
Iterable<Artifact> files,
boolean withBaselineCoverage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2012,6 +2012,9 @@ private ImmutableList<Artifact> createSourceAction(
// Host targets don't produce .dwo files.
result.addPicDwoFile(dwoFile);
}
if (gcnoFile != null) {
result.addPicGcnoFile(gcnoFile);
}
}

if (generateNoPicAction) {
Expand Down Expand Up @@ -2080,6 +2083,9 @@ private ImmutableList<Artifact> createSourceAction(
// Host targets don't produce .dwo files.
result.addDwoFile(noPicDwoFile);
}
if (gcnoFile != null) {
result.addGcnoFile(gcnoFile);
}
}
return directOutputs.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public class CcCompilationOutputs implements CcCompilationOutputsApi<Artifact> {
*/
private final ImmutableList<Artifact> picDwoFiles;

/** All .gcno files built by the target, corresponding to .o outputs. */
private final ImmutableList<Artifact> gcnoFiles;

/** All .pic.gcno files built by the target, corresponding to .pic.gcno outputs. */
private final ImmutableList<Artifact> picGcnoFiles;

/**
* All artifacts that are created if "--save_temps" is true.
*/
Expand All @@ -76,13 +82,17 @@ private CcCompilationOutputs(
LtoCompilationContext ltoCompilationContext,
ImmutableList<Artifact> dwoFiles,
ImmutableList<Artifact> picDwoFiles,
ImmutableList<Artifact> gcnoFiles,
ImmutableList<Artifact> picGcnoFiles,
NestedSet<Artifact> temps,
ImmutableList<Artifact> headerTokenFiles) {
this.objectFiles = objectFiles;
this.picObjectFiles = picObjectFiles;
this.ltoCompilationContext = ltoCompilationContext;
this.dwoFiles = dwoFiles;
this.picDwoFiles = picDwoFiles;
this.gcnoFiles = gcnoFiles;
this.picGcnoFiles = picGcnoFiles;
this.temps = temps;
this.headerTokenFiles = headerTokenFiles;
}
Expand Down Expand Up @@ -170,6 +180,28 @@ public ImmutableList<Artifact> getPicDwoFiles() {
return picDwoFiles;
}

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

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

/** Returns an unmodifiable view of the .gcno files set. */
public ImmutableList<Artifact> getGcnoFiles() {
return gcnoFiles;
}

/** Returns an unmodifiable view of the .pic.gcno files set. */
public ImmutableList<Artifact> getPicGcnoFiles() {
return picGcnoFiles;
}

/**
* Returns an unmodifiable view of the temp files set.
*/
Expand Down Expand Up @@ -207,6 +239,8 @@ public static final class Builder {
new LtoCompilationContext.Builder();
private final Set<Artifact> dwoFiles = new LinkedHashSet<>();
private final Set<Artifact> picDwoFiles = new LinkedHashSet<>();
private final Set<Artifact> gcnoFiles = new LinkedHashSet<>();
private final Set<Artifact> picGcnoFiles = new LinkedHashSet<>();
private final NestedSetBuilder<Artifact> temps = NestedSetBuilder.stableOrder();
private final Set<Artifact> headerTokenFiles = new LinkedHashSet<>();

Expand All @@ -221,6 +255,8 @@ public CcCompilationOutputs build() {
ltoCompilationContext.build(),
ImmutableList.copyOf(dwoFiles),
ImmutableList.copyOf(picDwoFiles),
ImmutableList.copyOf(gcnoFiles),
ImmutableList.copyOf(picGcnoFiles),
temps.build(),
ImmutableList.copyOf(headerTokenFiles));
}
Expand All @@ -231,6 +267,8 @@ public Builder merge(CcCompilationOutputs outputs) {
this.picObjectFiles.addAll(outputs.picObjectFiles);
this.dwoFiles.addAll(outputs.dwoFiles);
this.picDwoFiles.addAll(outputs.picDwoFiles);
this.gcnoFiles.addAll(outputs.gcnoFiles);
this.picGcnoFiles.addAll(outputs.picGcnoFiles);
this.temps.addTransitive(outputs.temps);
this.headerTokenFiles.addAll(outputs.headerTokenFiles);
this.ltoCompilationContext.addAll(outputs.ltoCompilationContext);
Expand Down Expand Up @@ -301,6 +339,18 @@ public Builder addPicDwoFile(Artifact artifact) {
return this;
}

@CanIgnoreReturnValue
public Builder addGcnoFile(Artifact artifact) {
gcnoFiles.add(artifact);
return this;
}

@CanIgnoreReturnValue
public Builder addPicGcnoFile(Artifact artifact) {
picGcnoFiles.add(artifact);
return this;
}

/** Adds temp files. */
@CanIgnoreReturnValue
public Builder addTemps(Iterable<Artifact> artifacts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,7 @@ public boolean generateLlvmLcovStarlark(StarlarkThread thread) throws EvalExcept
return generateLlvmLCov();
}

@Nullable
@Override
public String fdoInstrumentStarlark(StarlarkThread thread) throws EvalException {
checkInExpandedApiAllowlist(thread, "fdo_instrument");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,10 @@ Depset getStarlarkFilesToCompile(boolean parseHeaders, boolean usePic, StarlarkT

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

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

@StarlarkMethod(name = "pic_gcno_files", documented = false, useStarlarkThread = true)
Sequence<FileT> getStarlarkPicGcnoFiles(StarlarkThread thread) throws EvalException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,12 @@ public interface CppConfigurationApi<InvalidConfigurationExceptionT extends Exce
@StarlarkMethod(name = "generate_llvm_lcov", documented = false, useStarlarkThread = true)
boolean generateLlvmLcovStarlark(StarlarkThread thread) throws EvalException;

@StarlarkMethod(name = "fdo_instrument", documented = false, useStarlarkThread = true)
@Nullable
@StarlarkMethod(
name = "fdo_instrument",
documented = false,
useStarlarkThread = true,
allowReturnNones = true)
String fdoInstrumentStarlark(StarlarkThread thread) throws EvalException;

@StarlarkMethod(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ public interface CoverageCommonApi<
positional = false,
named = true,
defaultValue = "None"),
@Param(
name = "metadata_files",
named = true,
positional = false,
documented = false,
defaultValue = "[]",
allowedTypes = {
@ParamType(type = Sequence.class, generic1 = FileApi.class),
})
},
useStarlarkThread = true)
InstrumentedFilesInfoApi instrumentedFilesInfo(
Expand All @@ -104,6 +113,7 @@ InstrumentedFilesInfoApi instrumentedFilesInfo(
Object supportFiles, // Sequence or Depset of <FileApi> expected
Dict<?, ?> environment, // <String, String>
Object extensions,
Sequence<?> metadataFiles,
StarlarkThread thread)
throws EvalException, TypeException;
}
40 changes: 40 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ cc_common = _builtins.toplevel.cc_common
cc_internal = _builtins.internal.cc_internal
CcNativeLibraryInfo = _builtins.internal.CcNativeLibraryInfo
config_common = _builtins.toplevel.config_common
coverage_common = _builtins.toplevel.coverage_common
platform_common = _builtins.toplevel.platform_common

artifact_category = struct(
Expand Down Expand Up @@ -1165,6 +1166,43 @@ def _system_include_dirs(ctx, additional_make_variable_substitutions):
result.append(_get_relative(ctx.bin_dir.path, out_includes_path))
return result

def _get_coverage_environment(ctx, cc_config, cc_toolchain):
if not ctx.configuration.coverage_enabled:
return {}
env = {
"COVERAGE_GCOV_PATH": cc_toolchain.tool_path(tool = "GCOV"),
"LLVM_COV": cc_toolchain.tool_path(tool = "LLVM_COV"),
"LLVM_PROFDATA": cc_toolchain.tool_path(tool = "LLVM_PROFDATA"),
"GENERATE_LLVM_LCOV": "1" if cc_config.generate_llvm_lcov() else "0",
}
for k in list(env.keys()):
if env[k] == None:
env[k] = ""
if cc_config.fdo_instrument():
env["FDO_DIR"] = cc_config.fdo_instrument()
return env

def _create_cc_instrumented_files_info(ctx, cc_config, cc_toolchain, metadata_files):
extensions = CC_SOURCE + \
C_SOURCE + \
CC_HEADER + \
ASSESMBLER_WITH_C_PREPROCESSOR + \
ASSEMBLER
coverage_environment = {}
if ctx.coverage_instrumented():
coverage_environment = _get_coverage_environment(ctx, cc_config, cc_toolchain)
coverage_support_files = cc_toolchain.coverage_files() if ctx.coverage_instrumented() else depset([])
info = coverage_common.instrumented_files_info(
ctx = ctx,
source_attributes = ["srcs", "hdrs"],
dependency_attributes = ["implementation_deps", "deps", "data"],
extensions = extensions,
metadata_files = metadata_files,
coverage_support_files = coverage_support_files,
coverage_environment = coverage_environment,
)
return info

cc_helper = struct(
merge_cc_debug_contexts = _merge_cc_debug_contexts,
is_code_coverage_enabled = _is_code_coverage_enabled,
Expand Down Expand Up @@ -1217,4 +1255,6 @@ cc_helper = struct(
get_public_hdrs = _get_public_hdrs,
report_invalid_options = _report_invalid_options,
system_include_dirs = _system_include_dirs,
get_coverage_environment = _get_coverage_environment,
create_cc_instrumented_files_info = _create_cc_instrumented_files_info,
)
11 changes: 5 additions & 6 deletions src/main/starlark/builtins_bzl/common/cc/cc_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,11 @@ def _cc_library_impl(ctx):
elif artifacts_to_build.interface_library != None:
files_builder.append(artifacts_to_build.interface_library)

instrumented_object_files = []
instrumented_object_files.extend(compilation_outputs.objects)
instrumented_object_files.extend(compilation_outputs.pic_objects)
instrumented_files_info = common.instrumented_files_info(
files = instrumented_object_files,
with_base_line_coverage = True,
instrumented_files_info = cc_helper.create_cc_instrumented_files_info(
ctx = ctx,
cc_config = ctx.fragments.cpp,
cc_toolchain = cc_toolchain,
metadata_files = compilation_outputs.gcno_files() + compilation_outputs.pic_gcno_files(),
)

runfiles_list = []
Expand Down

0 comments on commit adb6286

Please sign in to comment.