blob: 651ce6e1666c70f914dc24814e6ed1bb1f926a73 [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
20 "android/soong/cc"
21)
22
Joel Galensonfa049382021-01-14 16:03:18 -080023var CovLibraryName = "libprofile-clang-extras"
24
Pirama Arumuga Nainarb37ae582022-01-26 22:14:32 -080025// Add '%c' to default specifier after we resolve http://b/210012154
Joel Galensonfa049382021-01-14 16:03:18 -080026const profileInstrFlag = "-fprofile-instr-generate=/data/misc/trace/clang-%p-%m.profraw"
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040027
28type coverage struct {
29 Properties cc.CoverageProperties
30
31 // Whether binaries containing this module need --coverage added to their ldflags
32 linkCoverage bool
33}
34
35func (cov *coverage) props() []interface{} {
36 return []interface{}{&cov.Properties}
37}
38
39func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps {
40 if cov.Properties.NeedCoverageVariant {
41 ctx.AddVariationDependencies([]blueprint.Variation{
42 {Mutator: "link", Variation: "static"},
43 }, cc.CoverageDepTag, CovLibraryName)
44 }
45
46 return deps
47}
48
49func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
50
Colin Cross1a6acd42020-06-16 17:51:46 -070051 if !ctx.DeviceConfig().NativeCoverageEnabled() {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040052 return flags, deps
53 }
54
55 if cov.Properties.CoverageEnabled {
56 flags.Coverage = true
57 coverage := ctx.GetDirectDepWithTag(CovLibraryName, cc.CoverageDepTag).(cc.LinkableInterface)
58 flags.RustFlags = append(flags.RustFlags,
Pirama Arumuga Nainarb13e8ac2021-07-15 10:10:29 -070059 "-Z instrument-coverage", "-g")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040060 flags.LinkFlags = append(flags.LinkFlags,
Pirama Arumuga Nainar668da232022-01-25 22:32:39 -080061 profileInstrFlag, "-g", coverage.OutputFile().Path().String(), "-Wl,--wrap,open")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040062 deps.StaticLibs = append(deps.StaticLibs, coverage.OutputFile().Path())
Pirama Arumuga Nainarb37ae582022-01-26 22:14:32 -080063 if cc.EnableContinuousCoverage(ctx) {
64 flags.RustFlags = append(flags.RustFlags, "-C llvm-args=--runtime-counter-relocation")
65 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-mllvm,-runtime-counter-relocation")
66 }
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040067 }
68
69 return flags, deps
70}
71
72func (cov *coverage) begin(ctx BaseModuleContext) {
73 if ctx.Host() {
74 // Host coverage not yet supported.
75 } else {
76 // Update useSdk and sdkVersion args if Rust modules become SDK aware.
ThiƩbaud Weksteen1f7f70f2020-06-24 11:32:48 +020077 cov.Properties = cc.SetCoverageProperties(ctx, cov.Properties, ctx.RustModule().nativeCoverage(), false, "")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040078 }
79}