diff --git a/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValue.java b/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValue.java index a39d84f01f27f1..16b508a4bbde74 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValue.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfigurationValue.java @@ -536,6 +536,11 @@ public boolean shouldInstrumentTestTargets() { return options.instrumentTestTargets; } + /** Returns a boolean of whether to collect code coverage for generated files or not. */ + public boolean shouldCollectCodeCoverageForGeneratedFiles() { + return options.collectCodeCoverageForGeneratedFiles; + } + /** * Returns a new, unordered mapping of names to values of "Make" variables defined by this * configuration. diff --git a/src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java b/src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java index 4e175abf8c6efb..6ba9cae6f71f55 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java @@ -425,6 +425,16 @@ public ExecConfigurationDistinguisherSchemeConverter() { + " not be specified directly - 'bazel coverage' command should be used instead.") public boolean collectCodeCoverage; + @Option( + name = "experimental_collect_code_coverage_for_generated_files", + defaultValue = "false", + documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS, + effectTags = {OptionEffectTag.AFFECTS_OUTPUTS}, + help = + "If specified, Bazel will also generate collect coverage information for generated" + + " files.") + public boolean collectCodeCoverageForGeneratedFiles; + @Option( name = "build_runfile_manifests", defaultValue = "true", diff --git a/src/main/java/com/google/devtools/build/lib/analysis/test/InstrumentedFilesCollector.java b/src/main/java/com/google/devtools/build/lib/analysis/test/InstrumentedFilesCollector.java index 7dca3df13a8663..8a7dac95af91be 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/test/InstrumentedFilesCollector.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/test/InstrumentedFilesCollector.java @@ -213,8 +213,8 @@ public static InstrumentedFilesInfo collect( for (TransitiveInfoCollection dep : getPrerequisitesForAttributes(ruleContext, spec.sourceAttributes)) { for (Artifact artifact : dep.getProvider(FileProvider.class).getFilesToBuild().toList()) { - if (artifact.isSourceArtifact() && - spec.instrumentedFileTypes.matches(artifact.getFilename())) { + if (shouldIncludeArtifact(ruleContext.getConfiguration(), artifact) + && spec.instrumentedFileTypes.matches(artifact.getFilename())) { localSourcesBuilder.add(artifact); } } @@ -262,6 +262,14 @@ public static boolean shouldIncludeLocalSources( && config.getInstrumentationFilter().isIncluded(label.toString())); } + /** + * Return whether the artifact should be collected based on the origin of the artifact and the + * --experimental_collect_code_coverage_for_generated_files config setting. + */ + public static boolean shouldIncludeArtifact(BuildConfigurationValue config, Artifact artifact) { + return artifact.isSourceArtifact() || config.shouldCollectCodeCoverageForGeneratedFiles(); + } + /** * The set of file types and attributes to visit to collect instrumented files for a certain rule * type. The class is intentionally immutable, so that a single instance is sufficient for all