|
| 1 | +// Copyright 2022 The Bazel Authors. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.devtools.build.lib.analysis.test; |
| 16 | + |
| 17 | +import com.google.devtools.build.lib.analysis.config.BuildOptions; |
| 18 | +import com.google.devtools.build.lib.analysis.config.CoreOptionConverters.LabelConverter; |
| 19 | +import com.google.devtools.build.lib.analysis.config.CoreOptions; |
| 20 | +import com.google.devtools.build.lib.analysis.config.Fragment; |
| 21 | +import com.google.devtools.build.lib.analysis.config.FragmentOptions; |
| 22 | +import com.google.devtools.build.lib.analysis.config.RequiresOptions; |
| 23 | +import com.google.devtools.build.lib.analysis.starlark.annotations.StarlarkConfigurationField; |
| 24 | +import com.google.devtools.build.lib.cmdline.Label; |
| 25 | +import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable; |
| 26 | +import com.google.devtools.build.lib.starlarkbuildapi.test.CoverageConfigurationApi; |
| 27 | +import com.google.devtools.common.options.Option; |
| 28 | +import com.google.devtools.common.options.OptionDocumentationCategory; |
| 29 | +import com.google.devtools.common.options.OptionEffectTag; |
| 30 | +import javax.annotation.Nullable; |
| 31 | + |
| 32 | +/** The coverage configuration fragment. */ |
| 33 | +@Immutable |
| 34 | +@RequiresOptions(options = {CoreOptions.class, CoverageConfiguration.CoverageOptions.class}) |
| 35 | +public class CoverageConfiguration extends Fragment implements CoverageConfigurationApi { |
| 36 | + |
| 37 | + /** Command-line options. */ |
| 38 | + public static class CoverageOptions extends FragmentOptions { |
| 39 | + |
| 40 | + @Option( |
| 41 | + name = "coverage_output_generator", |
| 42 | + converter = LabelConverter.class, |
| 43 | + defaultValue = "@bazel_tools//tools/test:lcov_merger", |
| 44 | + documentationCategory = OptionDocumentationCategory.TOOLCHAIN, |
| 45 | + effectTags = { |
| 46 | + OptionEffectTag.CHANGES_INPUTS, |
| 47 | + OptionEffectTag.AFFECTS_OUTPUTS, |
| 48 | + OptionEffectTag.LOADING_AND_ANALYSIS |
| 49 | + }, |
| 50 | + help = |
| 51 | + "Location of the binary that is used to postprocess raw coverage reports. This must " |
| 52 | + + "currently be a filegroup that contains a single file, the binary. Defaults to " |
| 53 | + + "'//tools/test:lcov_merger'.") |
| 54 | + public Label coverageOutputGenerator; |
| 55 | + } |
| 56 | + |
| 57 | + private final CoverageOptions coverageOptions; |
| 58 | + |
| 59 | + public CoverageConfiguration(BuildOptions buildOptions) { |
| 60 | + if (!buildOptions.get(CoreOptions.class).collectCodeCoverage) { |
| 61 | + this.coverageOptions = null; |
| 62 | + return; |
| 63 | + } |
| 64 | + this.coverageOptions = buildOptions.get(CoverageOptions.class); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + @StarlarkConfigurationField( |
| 69 | + name = "output_generator", |
| 70 | + doc = "Label for the coverage output generator.") |
| 71 | + @Nullable |
| 72 | + public Label outputGenerator() { |
| 73 | + if (coverageOptions == null) { |
| 74 | + return null; |
| 75 | + } |
| 76 | + return coverageOptions.coverageOutputGenerator; |
| 77 | + } |
| 78 | +} |
0 commit comments