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 ( |
Yu Liu | 727204c | 2025-01-23 20:58:32 +0000 | [diff] [blame] | 18 | "android/soong/android" |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 19 | "github.com/google/blueprint" |
| 20 | |
| 21 | "android/soong/cc" |
| 22 | ) |
| 23 | |
Joel Galenson | fa04938 | 2021-01-14 16:03:18 -0800 | [diff] [blame] | 24 | var CovLibraryName = "libprofile-clang-extras" |
Ivan Lozano | 9ef9cb8 | 2023-02-14 10:56:14 -0500 | [diff] [blame] | 25 | var ProfilerBuiltins = "libprofiler_builtins.rust_sysroot" |
Joel Galenson | fa04938 | 2021-01-14 16:03:18 -0800 | [diff] [blame] | 26 | |
Pirama Arumuga Nainar | b37ae58 | 2022-01-26 22:14:32 -0800 | [diff] [blame] | 27 | // Add '%c' to default specifier after we resolve http://b/210012154 |
Joel Galenson | fa04938 | 2021-01-14 16:03:18 -0800 | [diff] [blame] | 28 | const profileInstrFlag = "-fprofile-instr-generate=/data/misc/trace/clang-%p-%m.profraw" |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 29 | |
| 30 | type coverage struct { |
| 31 | Properties cc.CoverageProperties |
| 32 | |
| 33 | // Whether binaries containing this module need --coverage added to their ldflags |
| 34 | linkCoverage bool |
| 35 | } |
| 36 | |
| 37 | func (cov *coverage) props() []interface{} { |
| 38 | return []interface{}{&cov.Properties} |
| 39 | } |
| 40 | |
| 41 | func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps { |
| 42 | if cov.Properties.NeedCoverageVariant { |
Yu Liu | f0cf1cc | 2024-04-18 19:35:34 +0000 | [diff] [blame] | 43 | if ctx.Device() { |
| 44 | ctx.AddVariationDependencies([]blueprint.Variation{ |
| 45 | {Mutator: "link", Variation: "static"}, |
| 46 | }, cc.CoverageDepTag, CovLibraryName) |
| 47 | } |
Ivan Lozano | 9ef9cb8 | 2023-02-14 10:56:14 -0500 | [diff] [blame] | 48 | |
| 49 | // no_std modules are missing libprofiler_builtins which provides coverage, so we need to add it as a dependency. |
| 50 | if rustModule, ok := ctx.Module().(*Module); ok && rustModule.compiler.noStdlibs() { |
Colin Cross | 8a49a3d | 2024-05-20 12:22:27 -0700 | [diff] [blame] | 51 | ctx.AddVariationDependencies([]blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}, rlibDepTag, ProfilerBuiltins) |
Ivan Lozano | 9ef9cb8 | 2023-02-14 10:56:14 -0500 | [diff] [blame] | 52 | } |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | return deps |
| 56 | } |
| 57 | |
| 58 | func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) { |
| 59 | |
Colin Cross | 1a6acd4 | 2020-06-16 17:51:46 -0700 | [diff] [blame] | 60 | if !ctx.DeviceConfig().NativeCoverageEnabled() { |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 61 | return flags, deps |
| 62 | } |
| 63 | |
| 64 | if cov.Properties.CoverageEnabled { |
| 65 | flags.Coverage = true |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 66 | flags.RustFlags = append(flags.RustFlags, |
Pirama Arumuga Nainar | f1f6dd1 | 2022-06-07 11:15:59 -0700 | [diff] [blame] | 67 | "-C instrument-coverage", "-g") |
Yu Liu | f0cf1cc | 2024-04-18 19:35:34 +0000 | [diff] [blame] | 68 | if ctx.Device() { |
Yu Liu | 727204c | 2025-01-23 20:58:32 +0000 | [diff] [blame] | 69 | m := ctx.GetDirectDepProxyWithTag(CovLibraryName, cc.CoverageDepTag) |
| 70 | coverage := android.OtherModuleProviderOrDefault(ctx, m, cc.LinkableInfoProvider) |
Yu Liu | f0cf1cc | 2024-04-18 19:35:34 +0000 | [diff] [blame] | 71 | flags.LinkFlags = append(flags.LinkFlags, |
Yu Liu | 727204c | 2025-01-23 20:58:32 +0000 | [diff] [blame] | 72 | profileInstrFlag, "-g", coverage.OutputFile.Path().String(), "-Wl,--wrap,open") |
| 73 | deps.StaticLibs = append(deps.StaticLibs, coverage.OutputFile.Path()) |
Yu Liu | f0cf1cc | 2024-04-18 19:35:34 +0000 | [diff] [blame] | 74 | } |
Ivan Lozano | 9ef9cb8 | 2023-02-14 10:56:14 -0500 | [diff] [blame] | 75 | |
| 76 | // no_std modules are missing libprofiler_builtins which provides coverage, so we need to add it as a dependency. |
| 77 | if rustModule, ok := ctx.Module().(*Module); ok && rustModule.compiler.noStdlibs() { |
Yu Liu | 727204c | 2025-01-23 20:58:32 +0000 | [diff] [blame] | 78 | m := ctx.GetDirectDepProxyWithTag(ProfilerBuiltins, rlibDepTag) |
| 79 | profiler_builtins := android.OtherModuleProviderOrDefault(ctx, m, cc.LinkableInfoProvider) |
| 80 | deps.RLibs = append(deps.RLibs, RustLibrary{Path: profiler_builtins.OutputFile.Path(), CrateName: profiler_builtins.CrateName}) |
Ivan Lozano | 9ef9cb8 | 2023-02-14 10:56:14 -0500 | [diff] [blame] | 81 | } |
| 82 | |
Pirama Arumuga Nainar | b37ae58 | 2022-01-26 22:14:32 -0800 | [diff] [blame] | 83 | if cc.EnableContinuousCoverage(ctx) { |
| 84 | flags.RustFlags = append(flags.RustFlags, "-C llvm-args=--runtime-counter-relocation") |
| 85 | flags.LinkFlags = append(flags.LinkFlags, "-Wl,-mllvm,-runtime-counter-relocation") |
| 86 | } |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | return flags, deps |
| 90 | } |
| 91 | |
| 92 | func (cov *coverage) begin(ctx BaseModuleContext) { |
Yifeng Zeng | d141912 | 2024-04-29 10:59:20 -0700 | [diff] [blame] | 93 | // Update useSdk and sdkVersion args if Rust modules become SDK aware. |
| 94 | cov.Properties = cc.SetCoverageProperties(ctx, cov.Properties, ctx.RustModule().nativeCoverage(), false, "") |
Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 95 | } |