2020-04-09 15:56:02 +02:00
|
|
|
// Copyright 2020 The Android Open Source Project
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package rust
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/google/blueprint"
|
|
|
|
|
|
|
|
"android/soong/cc"
|
|
|
|
)
|
|
|
|
|
2021-01-15 01:03:18 +01:00
|
|
|
var CovLibraryName = "libprofile-clang-extras"
|
2023-02-14 16:56:14 +01:00
|
|
|
var ProfilerBuiltins = "libprofiler_builtins.rust_sysroot"
|
2021-01-15 01:03:18 +01:00
|
|
|
|
2022-01-27 07:14:32 +01:00
|
|
|
// Add '%c' to default specifier after we resolve http://b/210012154
|
2021-01-15 01:03:18 +01:00
|
|
|
const profileInstrFlag = "-fprofile-instr-generate=/data/misc/trace/clang-%p-%m.profraw"
|
2020-04-09 15:56:02 +02:00
|
|
|
|
|
|
|
type coverage struct {
|
|
|
|
Properties cc.CoverageProperties
|
|
|
|
|
|
|
|
// Whether binaries containing this module need --coverage added to their ldflags
|
|
|
|
linkCoverage bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cov *coverage) props() []interface{} {
|
|
|
|
return []interface{}{&cov.Properties}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps {
|
|
|
|
if cov.Properties.NeedCoverageVariant {
|
|
|
|
ctx.AddVariationDependencies([]blueprint.Variation{
|
|
|
|
{Mutator: "link", Variation: "static"},
|
|
|
|
}, cc.CoverageDepTag, CovLibraryName)
|
2023-02-14 16:56:14 +01:00
|
|
|
|
|
|
|
// no_std modules are missing libprofiler_builtins which provides coverage, so we need to add it as a dependency.
|
|
|
|
if rustModule, ok := ctx.Module().(*Module); ok && rustModule.compiler.noStdlibs() {
|
|
|
|
ctx.AddVariationDependencies([]blueprint.Variation{{Mutator: "rust_libraries", Variation: "rlib"}}, rlibDepTag, ProfilerBuiltins)
|
|
|
|
}
|
2020-04-09 15:56:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return deps
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
|
|
|
|
|
2020-06-17 02:51:46 +02:00
|
|
|
if !ctx.DeviceConfig().NativeCoverageEnabled() {
|
2020-04-09 15:56:02 +02:00
|
|
|
return flags, deps
|
|
|
|
}
|
|
|
|
|
|
|
|
if cov.Properties.CoverageEnabled {
|
|
|
|
flags.Coverage = true
|
|
|
|
coverage := ctx.GetDirectDepWithTag(CovLibraryName, cc.CoverageDepTag).(cc.LinkableInterface)
|
|
|
|
flags.RustFlags = append(flags.RustFlags,
|
2022-06-07 20:15:59 +02:00
|
|
|
"-C instrument-coverage", "-g")
|
2020-04-09 15:56:02 +02:00
|
|
|
flags.LinkFlags = append(flags.LinkFlags,
|
2022-01-26 07:32:39 +01:00
|
|
|
profileInstrFlag, "-g", coverage.OutputFile().Path().String(), "-Wl,--wrap,open")
|
2023-10-02 20:39:17 +02:00
|
|
|
deps.StaticLibs = append(deps.StaticLibs, coverage.OutputFile().Path())
|
2023-02-14 16:56:14 +01:00
|
|
|
|
|
|
|
// no_std modules are missing libprofiler_builtins which provides coverage, so we need to add it as a dependency.
|
|
|
|
if rustModule, ok := ctx.Module().(*Module); ok && rustModule.compiler.noStdlibs() {
|
|
|
|
profiler_builtins := ctx.GetDirectDepWithTag(ProfilerBuiltins, rlibDepTag).(*Module)
|
2023-09-22 05:58:59 +02:00
|
|
|
deps.RLibs = append(deps.RLibs, RustLibrary{Path: profiler_builtins.OutputFile().Path(), CrateName: profiler_builtins.CrateName()})
|
2023-02-14 16:56:14 +01:00
|
|
|
}
|
|
|
|
|
2022-01-27 07:14:32 +01:00
|
|
|
if cc.EnableContinuousCoverage(ctx) {
|
|
|
|
flags.RustFlags = append(flags.RustFlags, "-C llvm-args=--runtime-counter-relocation")
|
|
|
|
flags.LinkFlags = append(flags.LinkFlags, "-Wl,-mllvm,-runtime-counter-relocation")
|
|
|
|
}
|
2020-04-09 15:56:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return flags, deps
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cov *coverage) begin(ctx BaseModuleContext) {
|
|
|
|
if ctx.Host() {
|
|
|
|
// Host coverage not yet supported.
|
|
|
|
} else {
|
|
|
|
// Update useSdk and sdkVersion args if Rust modules become SDK aware.
|
2020-06-24 11:32:48 +02:00
|
|
|
cov.Properties = cc.SetCoverageProperties(ctx, cov.Properties, ctx.RustModule().nativeCoverage(), false, "")
|
2020-04-09 15:56:02 +02:00
|
|
|
}
|
|
|
|
}
|