xref: /aosp_15_r20/build/soong/rust/coverage.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker// Copyright 2020 The Android Open Source Project
2*333d2b36SAndroid Build Coastguard Worker//
3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*333d2b36SAndroid Build Coastguard Worker//
7*333d2b36SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*333d2b36SAndroid Build Coastguard Worker//
9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*333d2b36SAndroid Build Coastguard Worker// limitations under the License.
14*333d2b36SAndroid Build Coastguard Worker
15*333d2b36SAndroid Build Coastguard Workerpackage rust
16*333d2b36SAndroid Build Coastguard Worker
17*333d2b36SAndroid Build Coastguard Workerimport (
18*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint"
19*333d2b36SAndroid Build Coastguard Worker
20*333d2b36SAndroid Build Coastguard Worker	"android/soong/cc"
21*333d2b36SAndroid Build Coastguard Worker)
22*333d2b36SAndroid Build Coastguard Worker
23*333d2b36SAndroid Build Coastguard Workervar CovLibraryName = "libprofile-clang-extras"
24*333d2b36SAndroid Build Coastguard Workervar ProfilerBuiltins = "libprofiler_builtins.rust_sysroot"
25*333d2b36SAndroid Build Coastguard Worker
26*333d2b36SAndroid Build Coastguard Worker// Add '%c' to default specifier after we resolve http://b/210012154
27*333d2b36SAndroid Build Coastguard Workerconst profileInstrFlag = "-fprofile-instr-generate=/data/misc/trace/clang-%p-%m.profraw"
28*333d2b36SAndroid Build Coastguard Worker
29*333d2b36SAndroid Build Coastguard Workertype coverage struct {
30*333d2b36SAndroid Build Coastguard Worker	Properties cc.CoverageProperties
31*333d2b36SAndroid Build Coastguard Worker
32*333d2b36SAndroid Build Coastguard Worker	// Whether binaries containing this module need --coverage added to their ldflags
33*333d2b36SAndroid Build Coastguard Worker	linkCoverage bool
34*333d2b36SAndroid Build Coastguard Worker}
35*333d2b36SAndroid Build Coastguard Worker
36*333d2b36SAndroid Build Coastguard Workerfunc (cov *coverage) props() []interface{} {
37*333d2b36SAndroid Build Coastguard Worker	return []interface{}{&cov.Properties}
38*333d2b36SAndroid Build Coastguard Worker}
39*333d2b36SAndroid Build Coastguard Worker
40*333d2b36SAndroid Build Coastguard Workerfunc (cov *coverage) deps(ctx DepsContext, deps Deps) Deps {
41*333d2b36SAndroid Build Coastguard Worker	if cov.Properties.NeedCoverageVariant {
42*333d2b36SAndroid Build Coastguard Worker		if ctx.Device() {
43*333d2b36SAndroid Build Coastguard Worker			ctx.AddVariationDependencies([]blueprint.Variation{
44*333d2b36SAndroid Build Coastguard Worker				{Mutator: "link", Variation: "static"},
45*333d2b36SAndroid Build Coastguard Worker			}, cc.CoverageDepTag, CovLibraryName)
46*333d2b36SAndroid Build Coastguard Worker		}
47*333d2b36SAndroid Build Coastguard Worker
48*333d2b36SAndroid Build Coastguard Worker		// no_std modules are missing libprofiler_builtins which provides coverage, so we need to add it as a dependency.
49*333d2b36SAndroid Build Coastguard Worker		if rustModule, ok := ctx.Module().(*Module); ok && rustModule.compiler.noStdlibs() {
50*333d2b36SAndroid Build Coastguard Worker			ctx.AddVariationDependencies([]blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}, rlibDepTag, ProfilerBuiltins)
51*333d2b36SAndroid Build Coastguard Worker		}
52*333d2b36SAndroid Build Coastguard Worker	}
53*333d2b36SAndroid Build Coastguard Worker
54*333d2b36SAndroid Build Coastguard Worker	return deps
55*333d2b36SAndroid Build Coastguard Worker}
56*333d2b36SAndroid Build Coastguard Worker
57*333d2b36SAndroid Build Coastguard Workerfunc (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
58*333d2b36SAndroid Build Coastguard Worker
59*333d2b36SAndroid Build Coastguard Worker	if !ctx.DeviceConfig().NativeCoverageEnabled() {
60*333d2b36SAndroid Build Coastguard Worker		return flags, deps
61*333d2b36SAndroid Build Coastguard Worker	}
62*333d2b36SAndroid Build Coastguard Worker
63*333d2b36SAndroid Build Coastguard Worker	if cov.Properties.CoverageEnabled {
64*333d2b36SAndroid Build Coastguard Worker		flags.Coverage = true
65*333d2b36SAndroid Build Coastguard Worker		flags.RustFlags = append(flags.RustFlags,
66*333d2b36SAndroid Build Coastguard Worker			"-C instrument-coverage", "-g")
67*333d2b36SAndroid Build Coastguard Worker		if ctx.Device() {
68*333d2b36SAndroid Build Coastguard Worker			coverage := ctx.GetDirectDepWithTag(CovLibraryName, cc.CoverageDepTag).(cc.LinkableInterface)
69*333d2b36SAndroid Build Coastguard Worker			flags.LinkFlags = append(flags.LinkFlags,
70*333d2b36SAndroid Build Coastguard Worker				profileInstrFlag, "-g", coverage.OutputFile().Path().String(), "-Wl,--wrap,open")
71*333d2b36SAndroid Build Coastguard Worker			deps.StaticLibs = append(deps.StaticLibs, coverage.OutputFile().Path())
72*333d2b36SAndroid Build Coastguard Worker		}
73*333d2b36SAndroid Build Coastguard Worker
74*333d2b36SAndroid Build Coastguard Worker		// no_std modules are missing libprofiler_builtins which provides coverage, so we need to add it as a dependency.
75*333d2b36SAndroid Build Coastguard Worker		if rustModule, ok := ctx.Module().(*Module); ok && rustModule.compiler.noStdlibs() {
76*333d2b36SAndroid Build Coastguard Worker			profiler_builtins := ctx.GetDirectDepWithTag(ProfilerBuiltins, rlibDepTag).(*Module)
77*333d2b36SAndroid Build Coastguard Worker			deps.RLibs = append(deps.RLibs, RustLibrary{Path: profiler_builtins.OutputFile().Path(), CrateName: profiler_builtins.CrateName()})
78*333d2b36SAndroid Build Coastguard Worker		}
79*333d2b36SAndroid Build Coastguard Worker
80*333d2b36SAndroid Build Coastguard Worker		if cc.EnableContinuousCoverage(ctx) {
81*333d2b36SAndroid Build Coastguard Worker			flags.RustFlags = append(flags.RustFlags, "-C llvm-args=--runtime-counter-relocation")
82*333d2b36SAndroid Build Coastguard Worker			flags.LinkFlags = append(flags.LinkFlags, "-Wl,-mllvm,-runtime-counter-relocation")
83*333d2b36SAndroid Build Coastguard Worker		}
84*333d2b36SAndroid Build Coastguard Worker	}
85*333d2b36SAndroid Build Coastguard Worker
86*333d2b36SAndroid Build Coastguard Worker	return flags, deps
87*333d2b36SAndroid Build Coastguard Worker}
88*333d2b36SAndroid Build Coastguard Worker
89*333d2b36SAndroid Build Coastguard Workerfunc (cov *coverage) begin(ctx BaseModuleContext) {
90*333d2b36SAndroid Build Coastguard Worker	// Update useSdk and sdkVersion args if Rust modules become SDK aware.
91*333d2b36SAndroid Build Coastguard Worker	cov.Properties = cc.SetCoverageProperties(ctx, cov.Properties, ctx.RustModule().nativeCoverage(), false, "")
92*333d2b36SAndroid Build Coastguard Worker}
93