blob: 798b21d780c61f44261ae26fb4350fc7071d09e6 [file] [log] [blame]
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -04001// Copyright 2020 The Android Open Source Project
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
15package rust
16
17import (
Yu Liu727204c2025-01-23 20:58:32 +000018 "android/soong/android"
Ivan Lozanocf7c1322025-02-05 21:19:24 +000019
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040020 "github.com/google/blueprint"
21
22 "android/soong/cc"
23)
24
Ivan Lozano9ef9cb82023-02-14 10:56:14 -050025var ProfilerBuiltins = "libprofiler_builtins.rust_sysroot"
Joel Galensonfa049382021-01-14 16:03:18 -080026
Pirama Arumuga Nainarb37ae582022-01-26 22:14:32 -080027// Add '%c' to default specifier after we resolve http://b/210012154
Joel Galensonfa049382021-01-14 16:03:18 -080028const profileInstrFlag = "-fprofile-instr-generate=/data/misc/trace/clang-%p-%m.profraw"
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040029
30type coverage struct {
31 Properties cc.CoverageProperties
32
33 // Whether binaries containing this module need --coverage added to their ldflags
34 linkCoverage bool
35}
36
37func (cov *coverage) props() []interface{} {
38 return []interface{}{&cov.Properties}
39}
40
Ivan Lozanocf7c1322025-02-05 21:19:24 +000041func getClangProfileLibraryName(ctx ModuleContextIntf) string {
42 if ctx.RustModule().UseSdk() {
43 return "libprofile-clang-extras_ndk"
44 } else {
45 return "libprofile-clang-extras"
46 }
47}
48
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040049func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps {
50 if cov.Properties.NeedCoverageVariant {
Yu Liuf0cf1cc2024-04-18 19:35:34 +000051 if ctx.Device() {
52 ctx.AddVariationDependencies([]blueprint.Variation{
53 {Mutator: "link", Variation: "static"},
Ivan Lozanocf7c1322025-02-05 21:19:24 +000054 }, cc.CoverageDepTag, getClangProfileLibraryName(ctx))
Yu Liuf0cf1cc2024-04-18 19:35:34 +000055 }
Ivan Lozano9ef9cb82023-02-14 10:56:14 -050056
57 // no_std modules are missing libprofiler_builtins which provides coverage, so we need to add it as a dependency.
58 if rustModule, ok := ctx.Module().(*Module); ok && rustModule.compiler.noStdlibs() {
Colin Cross8a49a3d2024-05-20 12:22:27 -070059 ctx.AddVariationDependencies([]blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}, rlibDepTag, ProfilerBuiltins)
Ivan Lozano9ef9cb82023-02-14 10:56:14 -050060 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040061 }
62
63 return deps
64}
65
66func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
67
Colin Cross1a6acd42020-06-16 17:51:46 -070068 if !ctx.DeviceConfig().NativeCoverageEnabled() {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040069 return flags, deps
70 }
71
72 if cov.Properties.CoverageEnabled {
73 flags.Coverage = true
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040074 flags.RustFlags = append(flags.RustFlags,
Pirama Arumuga Nainarf1f6dd12022-06-07 11:15:59 -070075 "-C instrument-coverage", "-g")
Yu Liuf0cf1cc2024-04-18 19:35:34 +000076 if ctx.Device() {
Ivan Lozanocf7c1322025-02-05 21:19:24 +000077 m := ctx.GetDirectDepProxyWithTag(getClangProfileLibraryName(ctx), cc.CoverageDepTag)
Yu Liu727204c2025-01-23 20:58:32 +000078 coverage := android.OtherModuleProviderOrDefault(ctx, m, cc.LinkableInfoProvider)
Yu Liuf0cf1cc2024-04-18 19:35:34 +000079 flags.LinkFlags = append(flags.LinkFlags,
Yu Liu727204c2025-01-23 20:58:32 +000080 profileInstrFlag, "-g", coverage.OutputFile.Path().String(), "-Wl,--wrap,open")
81 deps.StaticLibs = append(deps.StaticLibs, coverage.OutputFile.Path())
Yu Liuf0cf1cc2024-04-18 19:35:34 +000082 }
Ivan Lozano9ef9cb82023-02-14 10:56:14 -050083
84 // no_std modules are missing libprofiler_builtins which provides coverage, so we need to add it as a dependency.
85 if rustModule, ok := ctx.Module().(*Module); ok && rustModule.compiler.noStdlibs() {
Yu Liu727204c2025-01-23 20:58:32 +000086 m := ctx.GetDirectDepProxyWithTag(ProfilerBuiltins, rlibDepTag)
87 profiler_builtins := android.OtherModuleProviderOrDefault(ctx, m, cc.LinkableInfoProvider)
88 deps.RLibs = append(deps.RLibs, RustLibrary{Path: profiler_builtins.OutputFile.Path(), CrateName: profiler_builtins.CrateName})
Ivan Lozano9ef9cb82023-02-14 10:56:14 -050089 }
90
Pirama Arumuga Nainarb37ae582022-01-26 22:14:32 -080091 if cc.EnableContinuousCoverage(ctx) {
92 flags.RustFlags = append(flags.RustFlags, "-C llvm-args=--runtime-counter-relocation")
93 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-mllvm,-runtime-counter-relocation")
94 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040095 }
96
97 return flags, deps
98}
99
100func (cov *coverage) begin(ctx BaseModuleContext) {
Yifeng Zengd1419122024-04-29 10:59:20 -0700101 // Update useSdk and sdkVersion args if Rust modules become SDK aware.
102 cov.Properties = cc.SetCoverageProperties(ctx, cov.Properties, ctx.RustModule().nativeCoverage(), false, "")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400103}