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 | |
Joel Galenson | fa04938 | 2021-01-14 16:03:18 -0800 | [diff] [blame] | 23 | var CovLibraryName = "libprofile-clang-extras" |
| 24 | |
| 25 | const profileInstrFlag = "-fprofile-instr-generate=/data/misc/trace/clang-%p-%m.profraw" |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 26 | |
| 27 | type coverage struct { |
| 28 | Properties cc.CoverageProperties |
| 29 | |
| 30 | // Whether binaries containing this module need --coverage added to their ldflags |
| 31 | linkCoverage bool |
| 32 | } |
| 33 | |
| 34 | func (cov *coverage) props() []interface{} { |
| 35 | return []interface{}{&cov.Properties} |
| 36 | } |
| 37 | |
| 38 | func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps { |
| 39 | if cov.Properties.NeedCoverageVariant { |
| 40 | ctx.AddVariationDependencies([]blueprint.Variation{ |
| 41 | {Mutator: "link", Variation: "static"}, |
| 42 | }, cc.CoverageDepTag, CovLibraryName) |
| 43 | } |
| 44 | |
| 45 | return deps |
| 46 | } |
| 47 | |
| 48 | func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) { |
| 49 | |
Colin Cross | 1a6acd4 | 2020-06-16 17:51:46 -0700 | [diff] [blame] | 50 | if !ctx.DeviceConfig().NativeCoverageEnabled() { |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 51 | return flags, deps |
| 52 | } |
| 53 | |
| 54 | if cov.Properties.CoverageEnabled { |
| 55 | flags.Coverage = true |
| 56 | coverage := ctx.GetDirectDepWithTag(CovLibraryName, cc.CoverageDepTag).(cc.LinkableInterface) |
| 57 | flags.RustFlags = append(flags.RustFlags, |
Pirama Arumuga Nainar | b13e8ac | 2021-07-15 10:10:29 -0700 | [diff] [blame] | 58 | "-Z instrument-coverage", "-g") |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 59 | flags.LinkFlags = append(flags.LinkFlags, |
Pirama Arumuga Nainar | 1799f9d | 2021-09-10 09:29:38 -0700 | [diff] [blame] | 60 | profileInstrFlag, "-g", coverage.OutputFile().Path().String(), "-Wl,--wrap,open", |
| 61 | // Upstream LLVM change 6d2d3bd0a6 made |
| 62 | // -z,start-stop-gc the default. It drops metadata |
| 63 | // sections like __llvm_prf_data unless they are marked |
| 64 | // SHF_GNU_RETAIN. https://reviews.llvm.org/D97448 |
| 65 | // marks generated sections, including __llvm_prf_data |
| 66 | // as SHF_GNU_RETAIN. However this change is not in |
| 67 | // the Rust toolchain. Since we link Rust libs with |
| 68 | // new lld, we should use nostart-stop-gc until the |
| 69 | // Rust toolchain updates past D97448. |
| 70 | "-Wl,-z,nostart-stop-gc", |
| 71 | ) |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 72 | deps.StaticLibs = append(deps.StaticLibs, coverage.OutputFile().Path()) |
| 73 | } |
| 74 | |
| 75 | return flags, deps |
| 76 | } |
| 77 | |
| 78 | func (cov *coverage) begin(ctx BaseModuleContext) { |
| 79 | if ctx.Host() { |
| 80 | // Host coverage not yet supported. |
| 81 | } else { |
| 82 | // Update useSdk and sdkVersion args if Rust modules become SDK aware. |
ThiƩbaud Weksteen | 1f7f70f | 2020-06-24 11:32:48 +0200 | [diff] [blame] | 83 | cov.Properties = cc.SetCoverageProperties(ctx, cov.Properties, ctx.RustModule().nativeCoverage(), false, "") |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 84 | } |
| 85 | } |