2017-02-10 08:16:31 +08:00
|
|
|
// Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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 cc
|
|
|
|
|
|
|
|
import (
|
2018-12-11 07:12:40 +08:00
|
|
|
"strconv"
|
|
|
|
|
2019-07-03 05:55:35 +08:00
|
|
|
"github.com/google/blueprint"
|
|
|
|
|
2017-02-10 08:16:31 +08:00
|
|
|
"android/soong/android"
|
|
|
|
)
|
|
|
|
|
2020-08-06 01:08:30 +08:00
|
|
|
const profileInstrFlag = "-fprofile-instr-generate=/data/misc/trace/clang-%p-%m.profraw"
|
|
|
|
|
2017-02-10 08:16:31 +08:00
|
|
|
type CoverageProperties struct {
|
|
|
|
Native_coverage *bool
|
|
|
|
|
2019-03-21 02:31:56 +08:00
|
|
|
NeedCoverageVariant bool `blueprint:"mutated"`
|
|
|
|
NeedCoverageBuild bool `blueprint:"mutated"`
|
|
|
|
|
2018-12-11 07:12:40 +08:00
|
|
|
CoverageEnabled bool `blueprint:"mutated"`
|
|
|
|
IsCoverageVariant bool `blueprint:"mutated"`
|
2017-02-10 08:16:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type coverage struct {
|
|
|
|
Properties CoverageProperties
|
|
|
|
|
|
|
|
// Whether binaries containing this module need --coverage added to their ldflags
|
|
|
|
linkCoverage bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cov *coverage) props() []interface{} {
|
|
|
|
return []interface{}{&cov.Properties}
|
|
|
|
}
|
|
|
|
|
2019-12-07 07:22:41 +08:00
|
|
|
func getGcovProfileLibraryName(ctx ModuleContextIntf) string {
|
2019-07-03 05:55:35 +08:00
|
|
|
// This function should only ever be called for a cc.Module, so the
|
|
|
|
// following statement should always succeed.
|
|
|
|
if ctx.useSdk() {
|
|
|
|
return "libprofile-extras_ndk"
|
|
|
|
} else {
|
|
|
|
return "libprofile-extras"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 07:22:41 +08:00
|
|
|
func getClangProfileLibraryName(ctx ModuleContextIntf) string {
|
|
|
|
if ctx.useSdk() {
|
|
|
|
return "libprofile-clang-extras_ndk"
|
2021-01-10 00:25:22 +08:00
|
|
|
} else if ctx.isCfiAssemblySupportEnabled() {
|
|
|
|
return "libprofile-clang-extras_cfi_support"
|
2019-12-07 07:22:41 +08:00
|
|
|
} else {
|
|
|
|
return "libprofile-clang-extras"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-03 05:55:35 +08:00
|
|
|
func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps {
|
|
|
|
if cov.Properties.NeedCoverageVariant {
|
|
|
|
ctx.AddVariationDependencies([]blueprint.Variation{
|
|
|
|
{Mutator: "link", Variation: "static"},
|
2020-04-09 21:56:02 +08:00
|
|
|
}, CoverageDepTag, getGcovProfileLibraryName(ctx))
|
2019-12-07 07:22:41 +08:00
|
|
|
ctx.AddVariationDependencies([]blueprint.Variation{
|
|
|
|
{Mutator: "link", Variation: "static"},
|
2020-04-09 21:56:02 +08:00
|
|
|
}, CoverageDepTag, getClangProfileLibraryName(ctx))
|
2019-03-26 01:21:31 +08:00
|
|
|
}
|
2017-02-10 08:16:31 +08:00
|
|
|
return deps
|
|
|
|
}
|
|
|
|
|
2019-07-03 05:55:35 +08:00
|
|
|
func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
|
2019-12-07 07:22:41 +08:00
|
|
|
clangCoverage := ctx.DeviceConfig().ClangCoverageEnabled()
|
2020-06-17 08:51:46 +08:00
|
|
|
gcovCoverage := ctx.DeviceConfig().GcovCoverageEnabled()
|
2019-12-07 07:22:41 +08:00
|
|
|
|
|
|
|
if !gcovCoverage && !clangCoverage {
|
2019-07-03 05:55:35 +08:00
|
|
|
return flags, deps
|
2017-02-10 08:16:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if cov.Properties.CoverageEnabled {
|
|
|
|
cov.linkCoverage = true
|
2019-02-19 14:23:42 +08:00
|
|
|
|
2019-12-07 07:22:41 +08:00
|
|
|
if gcovCoverage {
|
2020-04-22 03:40:27 +08:00
|
|
|
flags.GcovCoverage = true
|
2019-12-07 07:22:41 +08:00
|
|
|
flags.Local.CommonFlags = append(flags.Local.CommonFlags, "--coverage", "-O0")
|
|
|
|
|
|
|
|
// Override -Wframe-larger-than and non-default optimization
|
|
|
|
// flags that the module may use.
|
|
|
|
flags.Local.CFlags = append(flags.Local.CFlags, "-Wno-frame-larger-than=", "-O0")
|
|
|
|
} else if clangCoverage {
|
2020-08-06 01:08:30 +08:00
|
|
|
flags.Local.CommonFlags = append(flags.Local.CommonFlags, profileInstrFlag, "-fcoverage-mapping", "-Wno-pass-failed")
|
2019-12-07 07:22:41 +08:00
|
|
|
}
|
2017-02-10 08:16:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Even if we don't have coverage enabled, if any of our object files were compiled
|
|
|
|
// with coverage, then we need to add --coverage to our ldflags.
|
|
|
|
if !cov.linkCoverage {
|
|
|
|
if ctx.static() && !ctx.staticBinary() {
|
|
|
|
// For static libraries, the only thing that changes our object files
|
|
|
|
// are included whole static libraries, so check to see if any of
|
|
|
|
// those have coverage enabled.
|
2020-07-28 12:26:48 +08:00
|
|
|
ctx.VisitDirectDeps(func(m android.Module) {
|
|
|
|
if depTag, ok := ctx.OtherModuleDependencyTag(m).(libraryDependencyTag); ok {
|
|
|
|
if depTag.static() && depTag.wholeStatic {
|
|
|
|
if cc, ok := m.(*Module); ok && cc.coverage != nil {
|
|
|
|
if cc.coverage.linkCoverage {
|
|
|
|
cov.linkCoverage = true
|
|
|
|
}
|
|
|
|
}
|
2017-02-10 08:16:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// For executables and shared libraries, we need to check all of
|
|
|
|
// our static dependencies.
|
2017-10-24 08:59:01 +08:00
|
|
|
ctx.VisitDirectDeps(func(m android.Module) {
|
2017-02-10 08:16:31 +08:00
|
|
|
cc, ok := m.(*Module)
|
|
|
|
if !ok || cc.coverage == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if static, ok := cc.linker.(libraryInterface); !ok || !static.static() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if cc.coverage.linkCoverage {
|
|
|
|
cov.linkCoverage = true
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cov.linkCoverage {
|
2019-12-07 07:22:41 +08:00
|
|
|
if gcovCoverage {
|
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, "--coverage")
|
|
|
|
|
2020-04-09 21:56:02 +08:00
|
|
|
coverage := ctx.GetDirectDepWithTag(getGcovProfileLibraryName(ctx), CoverageDepTag).(*Module)
|
2019-12-07 07:22:41 +08:00
|
|
|
deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path())
|
2019-03-26 01:21:31 +08:00
|
|
|
|
2019-12-07 07:22:41 +08:00
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--wrap,getenv")
|
|
|
|
} else if clangCoverage {
|
2020-08-06 01:08:30 +08:00
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, profileInstrFlag)
|
2019-07-03 14:47:35 +08:00
|
|
|
|
2020-04-09 21:56:02 +08:00
|
|
|
coverage := ctx.GetDirectDepWithTag(getClangProfileLibraryName(ctx), CoverageDepTag).(*Module)
|
2019-12-07 07:22:41 +08:00
|
|
|
deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path())
|
2020-12-11 02:45:33 +08:00
|
|
|
flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--wrap,open")
|
2019-12-07 07:22:41 +08:00
|
|
|
}
|
2017-02-10 08:16:31 +08:00
|
|
|
}
|
|
|
|
|
2019-07-03 05:55:35 +08:00
|
|
|
return flags, deps
|
2017-02-10 08:16:31 +08:00
|
|
|
}
|
|
|
|
|
2019-03-21 02:31:56 +08:00
|
|
|
func (cov *coverage) begin(ctx BaseModuleContext) {
|
2020-04-09 21:56:02 +08:00
|
|
|
if ctx.Host() {
|
|
|
|
// TODO(dwillemsen): because of -nodefaultlibs, we must depend on libclang_rt.profile-*.a
|
|
|
|
// Just turn off for now.
|
|
|
|
} else {
|
|
|
|
cov.Properties = SetCoverageProperties(ctx, cov.Properties, ctx.nativeCoverage(), ctx.useSdk(), ctx.sdkVersion())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetCoverageProperties(ctx android.BaseModuleContext, properties CoverageProperties, moduleTypeHasCoverage bool,
|
|
|
|
useSdk bool, sdkVersion string) CoverageProperties {
|
2018-12-11 07:12:40 +08:00
|
|
|
// Coverage is disabled globally
|
2020-06-17 08:51:46 +08:00
|
|
|
if !ctx.DeviceConfig().NativeCoverageEnabled() {
|
2020-04-09 21:56:02 +08:00
|
|
|
return properties
|
2018-12-11 07:12:40 +08:00
|
|
|
}
|
|
|
|
|
2019-03-21 02:31:56 +08:00
|
|
|
var needCoverageVariant bool
|
|
|
|
var needCoverageBuild bool
|
|
|
|
|
2020-04-09 21:56:02 +08:00
|
|
|
if moduleTypeHasCoverage {
|
2019-03-21 02:31:56 +08:00
|
|
|
// Check if Native_coverage is set to false. This property defaults to true.
|
2020-04-09 21:56:02 +08:00
|
|
|
needCoverageVariant = BoolDefault(properties.Native_coverage, true)
|
|
|
|
if useSdk && sdkVersion != "current" {
|
2019-03-21 02:31:56 +08:00
|
|
|
// Native coverage is not supported for SDK versions < 23
|
2020-04-09 21:56:02 +08:00
|
|
|
if fromApi, err := strconv.Atoi(sdkVersion); err == nil && fromApi < 23 {
|
2019-03-21 02:31:56 +08:00
|
|
|
needCoverageVariant = false
|
2018-12-11 07:12:40 +08:00
|
|
|
}
|
2019-03-21 02:31:56 +08:00
|
|
|
}
|
2018-12-11 07:12:40 +08:00
|
|
|
|
2019-03-21 02:31:56 +08:00
|
|
|
if needCoverageVariant {
|
|
|
|
// Coverage variant is actually built with coverage if enabled for its module path
|
2020-06-09 19:44:06 +08:00
|
|
|
needCoverageBuild = ctx.DeviceConfig().NativeCoverageEnabledForPath(ctx.ModuleDir())
|
2017-02-10 08:16:31 +08:00
|
|
|
}
|
2019-03-21 02:31:56 +08:00
|
|
|
}
|
2017-02-10 08:16:31 +08:00
|
|
|
|
2020-04-09 21:56:02 +08:00
|
|
|
properties.NeedCoverageBuild = needCoverageBuild
|
|
|
|
properties.NeedCoverageVariant = needCoverageVariant
|
|
|
|
|
|
|
|
return properties
|
2019-03-21 02:31:56 +08:00
|
|
|
}
|
|
|
|
|
2019-08-09 13:44:36 +08:00
|
|
|
// Coverage is an interface for non-CC modules to implement to be mutated for coverage
|
|
|
|
type Coverage interface {
|
|
|
|
android.Module
|
|
|
|
IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool
|
2021-04-01 21:49:36 +08:00
|
|
|
SetPreventInstall()
|
2019-08-09 13:44:36 +08:00
|
|
|
HideFromMake()
|
2020-01-14 17:38:44 +08:00
|
|
|
MarkAsCoverageVariant(bool)
|
2020-04-09 21:56:02 +08:00
|
|
|
EnableCoverageIfNeeded()
|
2019-08-09 13:44:36 +08:00
|
|
|
}
|
|
|
|
|
2019-03-21 02:31:56 +08:00
|
|
|
func coverageMutator(mctx android.BottomUpMutatorContext) {
|
|
|
|
if c, ok := mctx.Module().(*Module); ok && c.coverage != nil {
|
|
|
|
needCoverageVariant := c.coverage.Properties.NeedCoverageVariant
|
|
|
|
needCoverageBuild := c.coverage.Properties.NeedCoverageBuild
|
2018-12-11 07:12:40 +08:00
|
|
|
if needCoverageVariant {
|
|
|
|
m := mctx.CreateVariations("", "cov")
|
|
|
|
|
|
|
|
// Setup the non-coverage version and set HideFromMake and
|
|
|
|
// PreventInstall to true.
|
|
|
|
m[0].(*Module).coverage.Properties.CoverageEnabled = false
|
|
|
|
m[0].(*Module).coverage.Properties.IsCoverageVariant = false
|
|
|
|
m[0].(*Module).Properties.HideFromMake = true
|
|
|
|
m[0].(*Module).Properties.PreventInstall = true
|
|
|
|
|
|
|
|
// The coverage-enabled version inherits HideFromMake,
|
|
|
|
// PreventInstall from the original module.
|
|
|
|
m[1].(*Module).coverage.Properties.CoverageEnabled = needCoverageBuild
|
|
|
|
m[1].(*Module).coverage.Properties.IsCoverageVariant = true
|
2017-02-10 08:16:31 +08:00
|
|
|
}
|
2019-08-09 13:44:36 +08:00
|
|
|
} else if cov, ok := mctx.Module().(Coverage); ok && cov.IsNativeCoverageNeeded(mctx) {
|
2020-04-09 21:56:02 +08:00
|
|
|
// APEX and Rust modules fall here
|
2019-08-09 13:44:36 +08:00
|
|
|
|
|
|
|
// Note: variant "" is also created because an APEX can be depended on by another
|
|
|
|
// module which are split into "" and "cov" variants. e.g. when cc_test refers
|
|
|
|
// to an APEX via 'data' property.
|
|
|
|
m := mctx.CreateVariations("", "cov")
|
2020-04-09 21:56:02 +08:00
|
|
|
m[0].(Coverage).MarkAsCoverageVariant(false)
|
2021-04-01 21:49:36 +08:00
|
|
|
m[0].(Coverage).SetPreventInstall()
|
2019-08-09 13:44:36 +08:00
|
|
|
m[0].(Coverage).HideFromMake()
|
2020-04-09 21:56:02 +08:00
|
|
|
|
|
|
|
m[1].(Coverage).MarkAsCoverageVariant(true)
|
|
|
|
m[1].(Coverage).EnableCoverageIfNeeded()
|
2017-02-10 08:16:31 +08:00
|
|
|
}
|
|
|
|
}
|