blob: fabcbf4dca61b18e230e6979b3741d6fbd7f97ef [file] [log] [blame]
Dan Willemsen581341d2017-02-09 16:16:31 -08001// Copyright 2017 Google Inc. All rights reserved.
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 cc
16
17import (
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -080018 "strconv"
19
Dan Willemsen581341d2017-02-09 16:16:31 -080020 "android/soong/android"
Dan Willemsen581341d2017-02-09 16:16:31 -080021)
22
23type CoverageProperties struct {
24 Native_coverage *bool
25
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -070026 NeedCoverageVariant bool `blueprint:"mutated"`
27 NeedCoverageBuild bool `blueprint:"mutated"`
28
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -080029 CoverageEnabled bool `blueprint:"mutated"`
30 IsCoverageVariant bool `blueprint:"mutated"`
Dan Willemsen581341d2017-02-09 16:16:31 -080031}
32
33type coverage struct {
34 Properties CoverageProperties
35
36 // Whether binaries containing this module need --coverage added to their ldflags
37 linkCoverage bool
38}
39
40func (cov *coverage) props() []interface{} {
41 return []interface{}{&cov.Properties}
42}
43
Dan Willemsen581341d2017-02-09 16:16:31 -080044func (cov *coverage) deps(ctx BaseModuleContext, deps Deps) Deps {
45 return deps
46}
47
48func (cov *coverage) flags(ctx ModuleContext, flags Flags) Flags {
49 if !ctx.DeviceConfig().NativeCoverageEnabled() {
50 return flags
51 }
52
53 if cov.Properties.CoverageEnabled {
54 flags.Coverage = true
55 flags.GlobalFlags = append(flags.GlobalFlags, "--coverage", "-O0")
56 cov.linkCoverage = true
Pirama Arumuga Nainarc7679de2019-02-18 22:23:42 -080057
58 // Override -Wframe-larger-than and non-default optimization
59 // flags that the module may use.
60 flags.CFlags = append(flags.CFlags, "-Wno-frame-larger-than=", "-O0")
Dan Willemsen581341d2017-02-09 16:16:31 -080061 }
62
63 // Even if we don't have coverage enabled, if any of our object files were compiled
64 // with coverage, then we need to add --coverage to our ldflags.
65 if !cov.linkCoverage {
66 if ctx.static() && !ctx.staticBinary() {
67 // For static libraries, the only thing that changes our object files
68 // are included whole static libraries, so check to see if any of
69 // those have coverage enabled.
Colin Crossee6143c2017-12-30 17:54:27 -080070 ctx.VisitDirectDepsWithTag(wholeStaticDepTag, func(m android.Module) {
Dan Willemsen581341d2017-02-09 16:16:31 -080071 if cc, ok := m.(*Module); ok && cc.coverage != nil {
72 if cc.coverage.linkCoverage {
73 cov.linkCoverage = true
74 }
75 }
76 })
77 } else {
78 // For executables and shared libraries, we need to check all of
79 // our static dependencies.
Colin Crossd11fcda2017-10-23 17:59:01 -070080 ctx.VisitDirectDeps(func(m android.Module) {
Dan Willemsen581341d2017-02-09 16:16:31 -080081 cc, ok := m.(*Module)
82 if !ok || cc.coverage == nil {
83 return
84 }
85
86 if static, ok := cc.linker.(libraryInterface); !ok || !static.static() {
87 return
88 }
89
90 if cc.coverage.linkCoverage {
91 cov.linkCoverage = true
92 }
93 })
94 }
95 }
96
97 if cov.linkCoverage {
98 flags.LdFlags = append(flags.LdFlags, "--coverage")
99 }
100
101 return flags
102}
103
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700104func (cov *coverage) begin(ctx BaseModuleContext) {
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800105 // Coverage is disabled globally
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700106 if !ctx.DeviceConfig().NativeCoverageEnabled() {
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800107 return
108 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800109
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700110 var needCoverageVariant bool
111 var needCoverageBuild bool
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800112
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700113 if ctx.Host() {
114 // TODO(dwillemsen): because of -nodefaultlibs, we must depend on libclang_rt.profile-*.a
115 // Just turn off for now.
116 } else if ctx.isStubs() {
117 // Do not enable coverage for platform stub libraries
118 } else if ctx.isNDKStubLibrary() {
119 // Do not enable coverage for NDK stub libraries
120 } else {
121 // Check if Native_coverage is set to false. This property defaults to true.
122 needCoverageVariant = BoolDefault(cov.Properties.Native_coverage, true)
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800123
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700124 if sdk_version := ctx.sdkVersion(); ctx.useSdk() && sdk_version != "current" {
125 // Native coverage is not supported for SDK versions < 23
126 if fromApi, err := strconv.Atoi(sdk_version); err == nil && fromApi < 23 {
127 needCoverageVariant = false
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800128 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800129 }
130
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800131 if needCoverageVariant {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700132 // Coverage variant is actually built with coverage if enabled for its module path
133 needCoverageBuild = ctx.DeviceConfig().CoverageEnabledForPath(ctx.ModuleDir())
134 }
135 }
136
137 cov.Properties.NeedCoverageBuild = needCoverageBuild
138 cov.Properties.NeedCoverageVariant = needCoverageVariant
139}
140
141func coverageMutator(mctx android.BottomUpMutatorContext) {
142 if c, ok := mctx.Module().(*Module); ok && c.coverage != nil {
143 needCoverageVariant := c.coverage.Properties.NeedCoverageVariant
144 needCoverageBuild := c.coverage.Properties.NeedCoverageBuild
145 if needCoverageVariant {
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800146 m := mctx.CreateVariations("", "cov")
147
148 // Setup the non-coverage version and set HideFromMake and
149 // PreventInstall to true.
150 m[0].(*Module).coverage.Properties.CoverageEnabled = false
151 m[0].(*Module).coverage.Properties.IsCoverageVariant = false
152 m[0].(*Module).Properties.HideFromMake = true
153 m[0].(*Module).Properties.PreventInstall = true
154
155 // The coverage-enabled version inherits HideFromMake,
156 // PreventInstall from the original module.
157 m[1].(*Module).coverage.Properties.CoverageEnabled = needCoverageBuild
158 m[1].(*Module).coverage.Properties.IsCoverageVariant = true
Dan Willemsen581341d2017-02-09 16:16:31 -0800159 }
160 }
161}