coverage.go 3.3 KB

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