2017-02-10 01:16:31 +01: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 00:12:40 +01:00
|
|
|
"strconv"
|
|
|
|
|
2019-07-02 23:55:35 +02:00
|
|
|
"github.com/google/blueprint"
|
|
|
|
|
2017-02-10 01:16:31 +01:00
|
|
|
"android/soong/android"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CoverageProperties struct {
|
|
|
|
Native_coverage *bool
|
|
|
|
|
2019-03-20 19:31:56 +01:00
|
|
|
NeedCoverageVariant bool `blueprint:"mutated"`
|
|
|
|
NeedCoverageBuild bool `blueprint:"mutated"`
|
|
|
|
|
2018-12-11 00:12:40 +01:00
|
|
|
CoverageEnabled bool `blueprint:"mutated"`
|
|
|
|
IsCoverageVariant bool `blueprint:"mutated"`
|
2017-02-10 01:16:31 +01: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-07-02 23:55:35 +02:00
|
|
|
func getProfileLibraryName(ctx ModuleContextIntf) string {
|
|
|
|
// 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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps {
|
|
|
|
if cov.Properties.NeedCoverageVariant {
|
|
|
|
ctx.AddVariationDependencies([]blueprint.Variation{
|
|
|
|
{Mutator: "link", Variation: "static"},
|
|
|
|
}, coverageDepTag, getProfileLibraryName(ctx))
|
2019-03-25 18:21:31 +01:00
|
|
|
}
|
2017-02-10 01:16:31 +01:00
|
|
|
return deps
|
|
|
|
}
|
|
|
|
|
2019-07-02 23:55:35 +02:00
|
|
|
func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
|
2017-02-10 01:16:31 +01:00
|
|
|
if !ctx.DeviceConfig().NativeCoverageEnabled() {
|
2019-07-02 23:55:35 +02:00
|
|
|
return flags, deps
|
2017-02-10 01:16:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if cov.Properties.CoverageEnabled {
|
|
|
|
flags.Coverage = true
|
|
|
|
flags.GlobalFlags = append(flags.GlobalFlags, "--coverage", "-O0")
|
|
|
|
cov.linkCoverage = true
|
2019-02-19 07:23:42 +01:00
|
|
|
|
|
|
|
// Override -Wframe-larger-than and non-default optimization
|
|
|
|
// flags that the module may use.
|
|
|
|
flags.CFlags = append(flags.CFlags, "-Wno-frame-larger-than=", "-O0")
|
2017-02-10 01:16:31 +01: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.
|
2017-12-31 02:54:27 +01:00
|
|
|
ctx.VisitDirectDepsWithTag(wholeStaticDepTag, func(m android.Module) {
|
2017-02-10 01:16:31 +01:00
|
|
|
if cc, ok := m.(*Module); ok && cc.coverage != nil {
|
|
|
|
if cc.coverage.linkCoverage {
|
|
|
|
cov.linkCoverage = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// For executables and shared libraries, we need to check all of
|
|
|
|
// our static dependencies.
|
2017-10-24 02:59:01 +02:00
|
|
|
ctx.VisitDirectDeps(func(m android.Module) {
|
2017-02-10 01:16:31 +01: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 {
|
|
|
|
flags.LdFlags = append(flags.LdFlags, "--coverage")
|
2019-03-25 18:21:31 +01:00
|
|
|
|
2019-07-02 23:55:35 +02:00
|
|
|
coverage := ctx.GetDirectDepWithTag(getProfileLibraryName(ctx), coverageDepTag).(*Module)
|
|
|
|
deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path())
|
2019-07-03 08:47:35 +02:00
|
|
|
|
|
|
|
flags.LdFlags = append(flags.LdFlags, "-Wl,--wrap,getenv")
|
2017-02-10 01:16:31 +01:00
|
|
|
}
|
|
|
|
|
2019-07-02 23:55:35 +02:00
|
|
|
return flags, deps
|
2017-02-10 01:16:31 +01:00
|
|
|
}
|
|
|
|
|
2019-03-20 19:31:56 +01:00
|
|
|
func (cov *coverage) begin(ctx BaseModuleContext) {
|
2018-12-11 00:12:40 +01:00
|
|
|
// Coverage is disabled globally
|
2019-03-20 19:31:56 +01:00
|
|
|
if !ctx.DeviceConfig().NativeCoverageEnabled() {
|
2018-12-11 00:12:40 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-20 19:31:56 +01:00
|
|
|
var needCoverageVariant bool
|
|
|
|
var needCoverageBuild bool
|
|
|
|
|
|
|
|
if ctx.Host() {
|
|
|
|
// TODO(dwillemsen): because of -nodefaultlibs, we must depend on libclang_rt.profile-*.a
|
|
|
|
// Just turn off for now.
|
2019-03-25 18:21:31 +01:00
|
|
|
} else if !ctx.nativeCoverage() {
|
|
|
|
// Native coverage is not supported for this module type.
|
2019-03-20 19:31:56 +01:00
|
|
|
} else {
|
|
|
|
// Check if Native_coverage is set to false. This property defaults to true.
|
|
|
|
needCoverageVariant = BoolDefault(cov.Properties.Native_coverage, true)
|
|
|
|
|
|
|
|
if sdk_version := ctx.sdkVersion(); ctx.useSdk() && sdk_version != "current" {
|
|
|
|
// Native coverage is not supported for SDK versions < 23
|
|
|
|
if fromApi, err := strconv.Atoi(sdk_version); err == nil && fromApi < 23 {
|
|
|
|
needCoverageVariant = false
|
2018-12-11 00:12:40 +01:00
|
|
|
}
|
2019-03-20 19:31:56 +01:00
|
|
|
}
|
2018-12-11 00:12:40 +01:00
|
|
|
|
2019-03-20 19:31:56 +01:00
|
|
|
if needCoverageVariant {
|
|
|
|
// Coverage variant is actually built with coverage if enabled for its module path
|
|
|
|
needCoverageBuild = ctx.DeviceConfig().CoverageEnabledForPath(ctx.ModuleDir())
|
2017-02-10 01:16:31 +01:00
|
|
|
}
|
2019-03-20 19:31:56 +01:00
|
|
|
}
|
2017-02-10 01:16:31 +01:00
|
|
|
|
2019-03-20 19:31:56 +01:00
|
|
|
cov.Properties.NeedCoverageBuild = needCoverageBuild
|
|
|
|
cov.Properties.NeedCoverageVariant = needCoverageVariant
|
|
|
|
}
|
|
|
|
|
|
|
|
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 00:12:40 +01: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 01:16:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|