blob: 9be57dccf60f1017f4dd9329701fcb6a4798e1ae [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
23var CovLibraryName = "libprofile-extras"
24
25type coverage struct {
26 Properties cc.CoverageProperties
27
28 // Whether binaries containing this module need --coverage added to their ldflags
29 linkCoverage bool
30}
31
32func (cov *coverage) props() []interface{} {
33 return []interface{}{&cov.Properties}
34}
35
36func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps {
37 if cov.Properties.NeedCoverageVariant {
38 ctx.AddVariationDependencies([]blueprint.Variation{
39 {Mutator: "link", Variation: "static"},
40 }, cc.CoverageDepTag, CovLibraryName)
41 }
42
43 return deps
44}
45
46func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
47
48 if !ctx.DeviceConfig().NativeCoverageEnabled() && !ctx.DeviceConfig().ClangCoverageEnabled() {
49 return flags, deps
50 }
51
52 if cov.Properties.CoverageEnabled {
53 flags.Coverage = true
54 coverage := ctx.GetDirectDepWithTag(CovLibraryName, cc.CoverageDepTag).(cc.LinkableInterface)
55 flags.RustFlags = append(flags.RustFlags,
56 "-Z profile", "-g", "-C opt-level=0", "-C link-dead-code", "-Z no-landing-pads")
57 flags.LinkFlags = append(flags.LinkFlags,
58 "--coverage", "-g", coverage.OutputFile().Path().String(), "-Wl,--wrap,getenv")
59 deps.StaticLibs = append(deps.StaticLibs, coverage.OutputFile().Path())
60 }
61
62 return flags, deps
63}
64
65func (cov *coverage) begin(ctx BaseModuleContext) {
66 if ctx.Host() {
67 // Host coverage not yet supported.
68 } else {
69 // Update useSdk and sdkVersion args if Rust modules become SDK aware.
70 cov.Properties = cc.SetCoverageProperties(ctx, cov.Properties, ctx.nativeCoverage(), false, "")
71 }
72}