blob: bde07fd6358c0f96d0d71c41ba0d503ce916f9e5 [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
Oliver Nguyen1382ab62019-12-06 15:22:41 -080046func getGcovProfileLibraryName(ctx ModuleContextIntf) string {
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070047 // 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
Oliver Nguyen1382ab62019-12-06 15:22:41 -080056func getClangProfileLibraryName(ctx ModuleContextIntf) string {
57 if ctx.useSdk() {
58 return "libprofile-clang-extras_ndk"
59 } else {
60 return "libprofile-clang-extras"
61 }
62}
63
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070064func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps {
65 if cov.Properties.NeedCoverageVariant {
66 ctx.AddVariationDependencies([]blueprint.Variation{
67 {Mutator: "link", Variation: "static"},
Oliver Nguyen1382ab62019-12-06 15:22:41 -080068 }, coverageDepTag, getGcovProfileLibraryName(ctx))
69 ctx.AddVariationDependencies([]blueprint.Variation{
70 {Mutator: "link", Variation: "static"},
71 }, coverageDepTag, getClangProfileLibraryName(ctx))
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -070072 }
Dan Willemsen581341d2017-02-09 16:16:31 -080073 return deps
74}
75
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070076func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
Oliver Nguyen1382ab62019-12-06 15:22:41 -080077 gcovCoverage := ctx.DeviceConfig().NativeCoverageEnabled()
78 clangCoverage := ctx.DeviceConfig().ClangCoverageEnabled()
79
80 if !gcovCoverage && !clangCoverage {
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070081 return flags, deps
Dan Willemsen581341d2017-02-09 16:16:31 -080082 }
83
84 if cov.Properties.CoverageEnabled {
Dan Willemsen581341d2017-02-09 16:16:31 -080085 cov.linkCoverage = true
Pirama Arumuga Nainarc7679de2019-02-18 22:23:42 -080086
Oliver Nguyen1382ab62019-12-06 15:22:41 -080087 if gcovCoverage {
Oliver Nguyen04526782020-04-21 12:40:27 -070088 flags.GcovCoverage = true
Oliver Nguyen1382ab62019-12-06 15:22:41 -080089 flags.Local.CommonFlags = append(flags.Local.CommonFlags, "--coverage", "-O0")
90
91 // Override -Wframe-larger-than and non-default optimization
92 // flags that the module may use.
93 flags.Local.CFlags = append(flags.Local.CFlags, "-Wno-frame-larger-than=", "-O0")
94 } else if clangCoverage {
95 flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-fprofile-instr-generate", "-fcoverage-mapping")
96 }
Dan Willemsen581341d2017-02-09 16:16:31 -080097 }
98
99 // Even if we don't have coverage enabled, if any of our object files were compiled
100 // with coverage, then we need to add --coverage to our ldflags.
101 if !cov.linkCoverage {
102 if ctx.static() && !ctx.staticBinary() {
103 // For static libraries, the only thing that changes our object files
104 // are included whole static libraries, so check to see if any of
105 // those have coverage enabled.
Colin Crossee6143c2017-12-30 17:54:27 -0800106 ctx.VisitDirectDepsWithTag(wholeStaticDepTag, func(m android.Module) {
Dan Willemsen581341d2017-02-09 16:16:31 -0800107 if cc, ok := m.(*Module); ok && cc.coverage != nil {
108 if cc.coverage.linkCoverage {
109 cov.linkCoverage = true
110 }
111 }
112 })
113 } else {
114 // For executables and shared libraries, we need to check all of
115 // our static dependencies.
Colin Crossd11fcda2017-10-23 17:59:01 -0700116 ctx.VisitDirectDeps(func(m android.Module) {
Dan Willemsen581341d2017-02-09 16:16:31 -0800117 cc, ok := m.(*Module)
118 if !ok || cc.coverage == nil {
119 return
120 }
121
122 if static, ok := cc.linker.(libraryInterface); !ok || !static.static() {
123 return
124 }
125
126 if cc.coverage.linkCoverage {
127 cov.linkCoverage = true
128 }
129 })
130 }
131 }
132
133 if cov.linkCoverage {
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800134 if gcovCoverage {
135 flags.Local.LdFlags = append(flags.Local.LdFlags, "--coverage")
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700136
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800137 coverage := ctx.GetDirectDepWithTag(getGcovProfileLibraryName(ctx), coverageDepTag).(*Module)
138 deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path())
Pirama Arumuga Nainar100bbdc2019-07-02 23:47:35 -0700139
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800140 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--wrap,getenv")
141 } else if clangCoverage {
142 flags.Local.LdFlags = append(flags.Local.LdFlags, "-fprofile-instr-generate")
143
144 coverage := ctx.GetDirectDepWithTag(getClangProfileLibraryName(ctx), coverageDepTag).(*Module)
145 deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path())
146 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800147 }
148
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -0700149 return flags, deps
Dan Willemsen581341d2017-02-09 16:16:31 -0800150}
151
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700152func (cov *coverage) begin(ctx BaseModuleContext) {
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800153 // Coverage is disabled globally
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800154 if !ctx.DeviceConfig().NativeCoverageEnabled() && !ctx.DeviceConfig().ClangCoverageEnabled() {
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800155 return
156 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800157
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700158 var needCoverageVariant bool
159 var needCoverageBuild bool
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800160
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700161 if ctx.Host() {
162 // TODO(dwillemsen): because of -nodefaultlibs, we must depend on libclang_rt.profile-*.a
163 // Just turn off for now.
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700164 } else if !ctx.nativeCoverage() {
165 // Native coverage is not supported for this module type.
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700166 } else {
167 // Check if Native_coverage is set to false. This property defaults to true.
168 needCoverageVariant = BoolDefault(cov.Properties.Native_coverage, true)
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700169 if sdk_version := ctx.sdkVersion(); ctx.useSdk() && sdk_version != "current" {
170 // Native coverage is not supported for SDK versions < 23
171 if fromApi, err := strconv.Atoi(sdk_version); err == nil && fromApi < 23 {
172 needCoverageVariant = false
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800173 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800174 }
175
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800176 if needCoverageVariant {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700177 // Coverage variant is actually built with coverage if enabled for its module path
178 needCoverageBuild = ctx.DeviceConfig().CoverageEnabledForPath(ctx.ModuleDir())
179 }
180 }
181
182 cov.Properties.NeedCoverageBuild = needCoverageBuild
183 cov.Properties.NeedCoverageVariant = needCoverageVariant
184}
185
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900186// Coverage is an interface for non-CC modules to implement to be mutated for coverage
187type Coverage interface {
188 android.Module
189 IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool
190 PreventInstall()
191 HideFromMake()
Jiyong Park83dc74b2020-01-14 18:38:44 +0900192 MarkAsCoverageVariant(bool)
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900193}
194
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700195func coverageMutator(mctx android.BottomUpMutatorContext) {
196 if c, ok := mctx.Module().(*Module); ok && c.coverage != nil {
197 needCoverageVariant := c.coverage.Properties.NeedCoverageVariant
198 needCoverageBuild := c.coverage.Properties.NeedCoverageBuild
199 if needCoverageVariant {
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800200 m := mctx.CreateVariations("", "cov")
201
202 // Setup the non-coverage version and set HideFromMake and
203 // PreventInstall to true.
204 m[0].(*Module).coverage.Properties.CoverageEnabled = false
205 m[0].(*Module).coverage.Properties.IsCoverageVariant = false
206 m[0].(*Module).Properties.HideFromMake = true
207 m[0].(*Module).Properties.PreventInstall = true
208
209 // The coverage-enabled version inherits HideFromMake,
210 // PreventInstall from the original module.
211 m[1].(*Module).coverage.Properties.CoverageEnabled = needCoverageBuild
212 m[1].(*Module).coverage.Properties.IsCoverageVariant = true
Dan Willemsen581341d2017-02-09 16:16:31 -0800213 }
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900214 } else if cov, ok := mctx.Module().(Coverage); ok && cov.IsNativeCoverageNeeded(mctx) {
215 // APEX modules fall here
216
217 // Note: variant "" is also created because an APEX can be depended on by another
218 // module which are split into "" and "cov" variants. e.g. when cc_test refers
219 // to an APEX via 'data' property.
220 m := mctx.CreateVariations("", "cov")
Jiyong Park83dc74b2020-01-14 18:38:44 +0900221 m[0].(Coverage).MarkAsCoverageVariant(true)
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900222 m[0].(Coverage).PreventInstall()
223 m[0].(Coverage).HideFromMake()
Dan Willemsen581341d2017-02-09 16:16:31 -0800224 }
225}