blob: b312194f27e4600851c288bdace3ea5e786475c1 [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 (
18 "github.com/google/blueprint"
19
Sam Delmericoa588d152023-06-16 10:28:04 -040020 "android/soong/android"
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040021 "android/soong/cc"
22)
23
Joel Galensonfa049382021-01-14 16:03:18 -080024var CovLibraryName = "libprofile-clang-extras"
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
41func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps {
42 if cov.Properties.NeedCoverageVariant {
43 ctx.AddVariationDependencies([]blueprint.Variation{
44 {Mutator: "link", Variation: "static"},
45 }, cc.CoverageDepTag, CovLibraryName)
Ivan Lozano9ef9cb82023-02-14 10:56:14 -050046
47 // no_std modules are missing libprofiler_builtins which provides coverage, so we need to add it as a dependency.
48 if rustModule, ok := ctx.Module().(*Module); ok && rustModule.compiler.noStdlibs() {
49 ctx.AddVariationDependencies([]blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}, rlibDepTag, ProfilerBuiltins)
50 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040051 }
52
53 return deps
54}
55
56func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
57
Colin Cross1a6acd42020-06-16 17:51:46 -070058 if !ctx.DeviceConfig().NativeCoverageEnabled() {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040059 return flags, deps
60 }
61
62 if cov.Properties.CoverageEnabled {
63 flags.Coverage = true
64 coverage := ctx.GetDirectDepWithTag(CovLibraryName, cc.CoverageDepTag).(cc.LinkableInterface)
65 flags.RustFlags = append(flags.RustFlags,
Pirama Arumuga Nainarf1f6dd12022-06-07 11:15:59 -070066 "-C instrument-coverage", "-g")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040067 flags.LinkFlags = append(flags.LinkFlags,
Pirama Arumuga Nainar668da232022-01-25 22:32:39 -080068 profileInstrFlag, "-g", coverage.OutputFile().Path().String(), "-Wl,--wrap,open")
Peter Collingbournee7c71c32023-03-31 20:21:19 -070069 deps.LibDeps = append(deps.LibDeps, coverage.OutputFile().Path())
Ivan Lozano9ef9cb82023-02-14 10:56:14 -050070
71 // no_std modules are missing libprofiler_builtins which provides coverage, so we need to add it as a dependency.
72 if rustModule, ok := ctx.Module().(*Module); ok && rustModule.compiler.noStdlibs() {
73 profiler_builtins := ctx.GetDirectDepWithTag(ProfilerBuiltins, rlibDepTag).(*Module)
Sam Delmericoa588d152023-06-16 10:28:04 -040074 deps.Rlibs = android.AddDirectToDepSet[RustLibrary](deps.Rlibs, RustLibrary{
75 Path: profiler_builtins.OutputFile().Path(),
76 CrateName: profiler_builtins.CrateName(),
77 })
Ivan Lozano9ef9cb82023-02-14 10:56:14 -050078 }
79
Pirama Arumuga Nainarb37ae582022-01-26 22:14:32 -080080 if cc.EnableContinuousCoverage(ctx) {
81 flags.RustFlags = append(flags.RustFlags, "-C llvm-args=--runtime-counter-relocation")
82 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-mllvm,-runtime-counter-relocation")
83 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040084 }
85
86 return flags, deps
87}
88
89func (cov *coverage) begin(ctx BaseModuleContext) {
90 if ctx.Host() {
91 // Host coverage not yet supported.
92 } else {
93 // Update useSdk and sdkVersion args if Rust modules become SDK aware.
ThiƩbaud Weksteen1f7f70f2020-06-24 11:32:48 +020094 cov.Properties = cc.SetCoverageProperties(ctx, cov.Properties, ctx.RustModule().nativeCoverage(), false, "")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040095 }
96}