Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame^] | 1 | // 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
| 18 | "github.com/google/blueprint" |
| 19 | |
| 20 | "android/soong/cc" |
| 21 | ) |
| 22 | |
| 23 | var CovLibraryName = "libprofile-extras" |
| 24 | |
| 25 | type coverage struct { |
| 26 | Properties cc.CoverageProperties |
| 27 | |
| 28 | // Whether binaries containing this module need --coverage added to their ldflags |
| 29 | linkCoverage bool |
| 30 | } |
| 31 | |
| 32 | func (cov *coverage) props() []interface{} { |
| 33 | return []interface{}{&cov.Properties} |
| 34 | } |
| 35 | |
| 36 | func (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 | |
| 46 | func (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 | |
| 65 | func (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 | } |