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