blob: b6451eea1303a0d0e472d757bea9cbaae0721fa0 [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
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070020 "github.com/google/blueprint"
21
Dan Willemsen581341d2017-02-09 16:16:31 -080022 "android/soong/android"
Dan Willemsen581341d2017-02-09 16:16:31 -080023)
24
25type CoverageProperties struct {
26 Native_coverage *bool
27
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -070028 NeedCoverageVariant bool `blueprint:"mutated"`
29 NeedCoverageBuild bool `blueprint:"mutated"`
30
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -080031 CoverageEnabled bool `blueprint:"mutated"`
32 IsCoverageVariant bool `blueprint:"mutated"`
Dan Willemsen581341d2017-02-09 16:16:31 -080033}
34
35type coverage struct {
36 Properties CoverageProperties
37
38 // Whether binaries containing this module need --coverage added to their ldflags
39 linkCoverage bool
40}
41
42func (cov *coverage) props() []interface{} {
43 return []interface{}{&cov.Properties}
44}
45
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070046func getProfileLibraryName(ctx ModuleContextIntf) string {
47 // This function should only ever be called for a cc.Module, so the
48 // following statement should always succeed.
49 if ctx.useSdk() {
50 return "libprofile-extras_ndk"
51 } else {
52 return "libprofile-extras"
53 }
54}
55
56func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps {
57 if cov.Properties.NeedCoverageVariant {
58 ctx.AddVariationDependencies([]blueprint.Variation{
59 {Mutator: "link", Variation: "static"},
60 }, coverageDepTag, getProfileLibraryName(ctx))
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -070061 }
Dan Willemsen581341d2017-02-09 16:16:31 -080062 return deps
63}
64
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070065func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
Dan Willemsen581341d2017-02-09 16:16:31 -080066 if !ctx.DeviceConfig().NativeCoverageEnabled() {
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070067 return flags, deps
Dan Willemsen581341d2017-02-09 16:16:31 -080068 }
69
70 if cov.Properties.CoverageEnabled {
71 flags.Coverage = true
Colin Cross4af21ed2019-11-04 09:37:55 -080072 flags.Local.CommonFlags = append(flags.Local.CommonFlags, "--coverage", "-O0")
Dan Willemsen581341d2017-02-09 16:16:31 -080073 cov.linkCoverage = true
Pirama Arumuga Nainarc7679de2019-02-18 22:23:42 -080074
75 // Override -Wframe-larger-than and non-default optimization
76 // flags that the module may use.
Colin Cross4af21ed2019-11-04 09:37:55 -080077 flags.Local.CFlags = append(flags.Local.CFlags, "-Wno-frame-larger-than=", "-O0")
Dan Willemsen581341d2017-02-09 16:16:31 -080078 }
79
80 // Even if we don't have coverage enabled, if any of our object files were compiled
81 // with coverage, then we need to add --coverage to our ldflags.
82 if !cov.linkCoverage {
83 if ctx.static() && !ctx.staticBinary() {
84 // For static libraries, the only thing that changes our object files
85 // are included whole static libraries, so check to see if any of
86 // those have coverage enabled.
Colin Crossee6143c2017-12-30 17:54:27 -080087 ctx.VisitDirectDepsWithTag(wholeStaticDepTag, func(m android.Module) {
Dan Willemsen581341d2017-02-09 16:16:31 -080088 if cc, ok := m.(*Module); ok && cc.coverage != nil {
89 if cc.coverage.linkCoverage {
90 cov.linkCoverage = true
91 }
92 }
93 })
94 } else {
95 // For executables and shared libraries, we need to check all of
96 // our static dependencies.
Colin Crossd11fcda2017-10-23 17:59:01 -070097 ctx.VisitDirectDeps(func(m android.Module) {
Dan Willemsen581341d2017-02-09 16:16:31 -080098 cc, ok := m.(*Module)
99 if !ok || cc.coverage == nil {
100 return
101 }
102
103 if static, ok := cc.linker.(libraryInterface); !ok || !static.static() {
104 return
105 }
106
107 if cc.coverage.linkCoverage {
108 cov.linkCoverage = true
109 }
110 })
111 }
112 }
113
114 if cov.linkCoverage {
Colin Cross4af21ed2019-11-04 09:37:55 -0800115 flags.Local.LdFlags = append(flags.Local.LdFlags, "--coverage")
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700116
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -0700117 coverage := ctx.GetDirectDepWithTag(getProfileLibraryName(ctx), coverageDepTag).(*Module)
118 deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path())
Pirama Arumuga Nainar100bbdc2019-07-02 23:47:35 -0700119
Colin Cross4af21ed2019-11-04 09:37:55 -0800120 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--wrap,getenv")
Dan Willemsen581341d2017-02-09 16:16:31 -0800121 }
122
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -0700123 return flags, deps
Dan Willemsen581341d2017-02-09 16:16:31 -0800124}
125
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700126func (cov *coverage) begin(ctx BaseModuleContext) {
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800127 // Coverage is disabled globally
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700128 if !ctx.DeviceConfig().NativeCoverageEnabled() {
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800129 return
130 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800131
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700132 var needCoverageVariant bool
133 var needCoverageBuild bool
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800134
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700135 if ctx.Host() {
136 // TODO(dwillemsen): because of -nodefaultlibs, we must depend on libclang_rt.profile-*.a
137 // Just turn off for now.
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700138 } else if !ctx.nativeCoverage() {
139 // Native coverage is not supported for this module type.
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700140 } else {
141 // Check if Native_coverage is set to false. This property defaults to true.
142 needCoverageVariant = BoolDefault(cov.Properties.Native_coverage, true)
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700143 if sdk_version := ctx.sdkVersion(); ctx.useSdk() && sdk_version != "current" {
144 // Native coverage is not supported for SDK versions < 23
145 if fromApi, err := strconv.Atoi(sdk_version); err == nil && fromApi < 23 {
146 needCoverageVariant = false
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800147 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800148 }
149
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800150 if needCoverageVariant {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700151 // Coverage variant is actually built with coverage if enabled for its module path
152 needCoverageBuild = ctx.DeviceConfig().CoverageEnabledForPath(ctx.ModuleDir())
153 }
154 }
155
156 cov.Properties.NeedCoverageBuild = needCoverageBuild
157 cov.Properties.NeedCoverageVariant = needCoverageVariant
158}
159
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900160// Coverage is an interface for non-CC modules to implement to be mutated for coverage
161type Coverage interface {
162 android.Module
163 IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool
164 PreventInstall()
165 HideFromMake()
Jiyong Park83dc74b2020-01-14 18:38:44 +0900166 MarkAsCoverageVariant(bool)
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900167}
168
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700169func coverageMutator(mctx android.BottomUpMutatorContext) {
170 if c, ok := mctx.Module().(*Module); ok && c.coverage != nil {
171 needCoverageVariant := c.coverage.Properties.NeedCoverageVariant
172 needCoverageBuild := c.coverage.Properties.NeedCoverageBuild
173 if needCoverageVariant {
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800174 m := mctx.CreateVariations("", "cov")
175
176 // Setup the non-coverage version and set HideFromMake and
177 // PreventInstall to true.
178 m[0].(*Module).coverage.Properties.CoverageEnabled = false
179 m[0].(*Module).coverage.Properties.IsCoverageVariant = false
180 m[0].(*Module).Properties.HideFromMake = true
181 m[0].(*Module).Properties.PreventInstall = true
182
183 // The coverage-enabled version inherits HideFromMake,
184 // PreventInstall from the original module.
185 m[1].(*Module).coverage.Properties.CoverageEnabled = needCoverageBuild
186 m[1].(*Module).coverage.Properties.IsCoverageVariant = true
Dan Willemsen581341d2017-02-09 16:16:31 -0800187 }
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900188 } else if cov, ok := mctx.Module().(Coverage); ok && cov.IsNativeCoverageNeeded(mctx) {
189 // APEX modules fall here
190
191 // Note: variant "" is also created because an APEX can be depended on by another
192 // module which are split into "" and "cov" variants. e.g. when cc_test refers
193 // to an APEX via 'data' property.
194 m := mctx.CreateVariations("", "cov")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900195 m[0].(Coverage).MarkAsCoverageVariant(true)
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900196 m[0].(Coverage).PreventInstall()
197 m[0].(Coverage).HideFromMake()
Dan Willemsen581341d2017-02-09 16:16:31 -0800198 }
199}