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 15package rust 16 17import ( 18 "android/soong/android" 19 20 "github.com/google/blueprint" 21 22 "android/soong/cc" 23) 24 25var ProfilerBuiltins = "libprofiler_builtins.rust_sysroot" 26 27// Add '%c' to default specifier after we resolve http://b/210012154 28const profileInstrFlag = "-fprofile-instr-generate=/data/misc/trace/clang-%p-%m.profraw" 29 30type coverage struct { 31 Properties cc.CoverageProperties 32 33 // Whether binaries containing this module need --coverage added to their ldflags 34 linkCoverage bool 35} 36 37func (cov *coverage) props() []interface{} { 38 return []interface{}{&cov.Properties} 39} 40 41func getClangProfileLibraryName(ctx ModuleContextIntf) string { 42 if ctx.RustModule().UseSdk() { 43 return "libprofile-clang-extras_ndk" 44 } else { 45 return "libprofile-clang-extras" 46 } 47} 48 49func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps { 50 if cov.Properties.NeedCoverageVariant { 51 if ctx.Device() { 52 ctx.AddVariationDependencies([]blueprint.Variation{ 53 {Mutator: "link", Variation: "static"}, 54 }, cc.CoverageDepTag, getClangProfileLibraryName(ctx)) 55 } 56 57 // no_std modules are missing libprofiler_builtins which provides coverage, so we need to add it as a dependency. 58 if rustModule, ok := ctx.Module().(*Module); ok && rustModule.compiler.noStdlibs() { 59 ctx.AddVariationDependencies([]blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}, rlibDepTag, ProfilerBuiltins) 60 } 61 } 62 63 return deps 64} 65 66func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) { 67 68 if !ctx.DeviceConfig().NativeCoverageEnabled() { 69 return flags, deps 70 } 71 72 if cov.Properties.CoverageEnabled { 73 flags.Coverage = true 74 flags.RustFlags = append(flags.RustFlags, 75 "-C instrument-coverage", "-g") 76 if ctx.Device() { 77 m := ctx.GetDirectDepProxyWithTag(getClangProfileLibraryName(ctx), cc.CoverageDepTag) 78 coverage := android.OtherModuleProviderOrDefault(ctx, m, cc.LinkableInfoProvider) 79 flags.LinkFlags = append(flags.LinkFlags, 80 profileInstrFlag, "-g", coverage.OutputFile.Path().String(), "-Wl,--wrap,open") 81 deps.StaticLibs = append(deps.StaticLibs, coverage.OutputFile.Path()) 82 } 83 84 // no_std modules are missing libprofiler_builtins which provides coverage, so we need to add it as a dependency. 85 if rustModule, ok := ctx.Module().(*Module); ok && rustModule.compiler.noStdlibs() { 86 m := ctx.GetDirectDepProxyWithTag(ProfilerBuiltins, rlibDepTag) 87 profiler_builtins := android.OtherModuleProviderOrDefault(ctx, m, cc.LinkableInfoProvider) 88 deps.RLibs = append(deps.RLibs, RustLibrary{Path: profiler_builtins.OutputFile.Path(), CrateName: profiler_builtins.CrateName}) 89 } 90 91 if cc.EnableContinuousCoverage(ctx) { 92 flags.RustFlags = append(flags.RustFlags, "-C llvm-args=--runtime-counter-relocation") 93 flags.LinkFlags = append(flags.LinkFlags, "-Wl,-mllvm,-runtime-counter-relocation") 94 } 95 } 96 97 return flags, deps 98} 99 100func (cov *coverage) begin(ctx BaseModuleContext) { 101 // Update useSdk and sdkVersion args if Rust modules become SDK aware. 102 cov.Properties = cc.SetCoverageProperties(ctx, cov.Properties, ctx.RustModule().nativeCoverage(), false, "") 103} 104