blob: f378e0d20a7d790583065aa1c9c279bef6f7dd63 [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
Pirama Arumuga Nainarf45e1522020-08-05 10:08:30 -070025const profileInstrFlag = "-fprofile-instr-generate=/data/misc/trace/clang-%p-%m.profraw"
26
Dan Willemsen581341d2017-02-09 16:16:31 -080027type CoverageProperties struct {
28 Native_coverage *bool
29
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -070030 NeedCoverageVariant bool `blueprint:"mutated"`
31 NeedCoverageBuild bool `blueprint:"mutated"`
32
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -080033 CoverageEnabled bool `blueprint:"mutated"`
34 IsCoverageVariant bool `blueprint:"mutated"`
Dan Willemsen581341d2017-02-09 16:16:31 -080035}
36
37type coverage struct {
38 Properties CoverageProperties
39
40 // Whether binaries containing this module need --coverage added to their ldflags
41 linkCoverage bool
42}
43
44func (cov *coverage) props() []interface{} {
45 return []interface{}{&cov.Properties}
46}
47
Oliver Nguyen1382ab62019-12-06 15:22:41 -080048func getGcovProfileLibraryName(ctx ModuleContextIntf) string {
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070049 // This function should only ever be called for a cc.Module, so the
50 // following statement should always succeed.
51 if ctx.useSdk() {
52 return "libprofile-extras_ndk"
53 } else {
54 return "libprofile-extras"
55 }
56}
57
Oliver Nguyen1382ab62019-12-06 15:22:41 -080058func getClangProfileLibraryName(ctx ModuleContextIntf) string {
59 if ctx.useSdk() {
60 return "libprofile-clang-extras_ndk"
61 } else {
62 return "libprofile-clang-extras"
63 }
64}
65
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070066func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps {
67 if cov.Properties.NeedCoverageVariant {
68 ctx.AddVariationDependencies([]blueprint.Variation{
69 {Mutator: "link", Variation: "static"},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040070 }, CoverageDepTag, getGcovProfileLibraryName(ctx))
Oliver Nguyen1382ab62019-12-06 15:22:41 -080071 ctx.AddVariationDependencies([]blueprint.Variation{
72 {Mutator: "link", Variation: "static"},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040073 }, CoverageDepTag, getClangProfileLibraryName(ctx))
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -070074 }
Dan Willemsen581341d2017-02-09 16:16:31 -080075 return deps
76}
77
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070078func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
Oliver Nguyen1382ab62019-12-06 15:22:41 -080079 clangCoverage := ctx.DeviceConfig().ClangCoverageEnabled()
Colin Cross1a6acd42020-06-16 17:51:46 -070080 gcovCoverage := ctx.DeviceConfig().GcovCoverageEnabled()
Oliver Nguyen1382ab62019-12-06 15:22:41 -080081
82 if !gcovCoverage && !clangCoverage {
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070083 return flags, deps
Dan Willemsen581341d2017-02-09 16:16:31 -080084 }
85
86 if cov.Properties.CoverageEnabled {
Dan Willemsen581341d2017-02-09 16:16:31 -080087 cov.linkCoverage = true
Pirama Arumuga Nainarc7679de2019-02-18 22:23:42 -080088
Oliver Nguyen1382ab62019-12-06 15:22:41 -080089 if gcovCoverage {
Oliver Nguyen04526782020-04-21 12:40:27 -070090 flags.GcovCoverage = true
Oliver Nguyen1382ab62019-12-06 15:22:41 -080091 flags.Local.CommonFlags = append(flags.Local.CommonFlags, "--coverage", "-O0")
92
93 // Override -Wframe-larger-than and non-default optimization
94 // flags that the module may use.
95 flags.Local.CFlags = append(flags.Local.CFlags, "-Wno-frame-larger-than=", "-O0")
96 } else if clangCoverage {
Pirama Arumuga Nainarf45e1522020-08-05 10:08:30 -070097 flags.Local.CommonFlags = append(flags.Local.CommonFlags, profileInstrFlag, "-fcoverage-mapping", "-Wno-pass-failed")
Oliver Nguyen1382ab62019-12-06 15:22:41 -080098 }
Dan Willemsen581341d2017-02-09 16:16:31 -080099 }
100
101 // Even if we don't have coverage enabled, if any of our object files were compiled
102 // with coverage, then we need to add --coverage to our ldflags.
103 if !cov.linkCoverage {
104 if ctx.static() && !ctx.staticBinary() {
105 // For static libraries, the only thing that changes our object files
106 // are included whole static libraries, so check to see if any of
107 // those have coverage enabled.
Colin Crossee6143c2017-12-30 17:54:27 -0800108 ctx.VisitDirectDepsWithTag(wholeStaticDepTag, func(m android.Module) {
Dan Willemsen581341d2017-02-09 16:16:31 -0800109 if cc, ok := m.(*Module); ok && cc.coverage != nil {
110 if cc.coverage.linkCoverage {
111 cov.linkCoverage = true
112 }
113 }
114 })
115 } else {
116 // For executables and shared libraries, we need to check all of
117 // our static dependencies.
Colin Crossd11fcda2017-10-23 17:59:01 -0700118 ctx.VisitDirectDeps(func(m android.Module) {
Dan Willemsen581341d2017-02-09 16:16:31 -0800119 cc, ok := m.(*Module)
120 if !ok || cc.coverage == nil {
121 return
122 }
123
124 if static, ok := cc.linker.(libraryInterface); !ok || !static.static() {
125 return
126 }
127
128 if cc.coverage.linkCoverage {
129 cov.linkCoverage = true
130 }
131 })
132 }
133 }
134
135 if cov.linkCoverage {
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800136 if gcovCoverage {
137 flags.Local.LdFlags = append(flags.Local.LdFlags, "--coverage")
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700138
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400139 coverage := ctx.GetDirectDepWithTag(getGcovProfileLibraryName(ctx), CoverageDepTag).(*Module)
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800140 deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path())
Pirama Arumuga Nainar100bbdc2019-07-02 23:47:35 -0700141
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800142 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--wrap,getenv")
143 } else if clangCoverage {
Pirama Arumuga Nainarf45e1522020-08-05 10:08:30 -0700144 flags.Local.LdFlags = append(flags.Local.LdFlags, profileInstrFlag)
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800145
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400146 coverage := ctx.GetDirectDepWithTag(getClangProfileLibraryName(ctx), CoverageDepTag).(*Module)
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800147 deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path())
148 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800149 }
150
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -0700151 return flags, deps
Dan Willemsen581341d2017-02-09 16:16:31 -0800152}
153
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700154func (cov *coverage) begin(ctx BaseModuleContext) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400155 if ctx.Host() {
156 // TODO(dwillemsen): because of -nodefaultlibs, we must depend on libclang_rt.profile-*.a
157 // Just turn off for now.
158 } else {
159 cov.Properties = SetCoverageProperties(ctx, cov.Properties, ctx.nativeCoverage(), ctx.useSdk(), ctx.sdkVersion())
160 }
161}
162
163func SetCoverageProperties(ctx android.BaseModuleContext, properties CoverageProperties, moduleTypeHasCoverage bool,
164 useSdk bool, sdkVersion string) CoverageProperties {
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800165 // Coverage is disabled globally
Colin Cross1a6acd42020-06-16 17:51:46 -0700166 if !ctx.DeviceConfig().NativeCoverageEnabled() {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400167 return properties
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800168 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800169
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700170 var needCoverageVariant bool
171 var needCoverageBuild bool
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800172
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400173 if moduleTypeHasCoverage {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700174 // Check if Native_coverage is set to false. This property defaults to true.
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400175 needCoverageVariant = BoolDefault(properties.Native_coverage, true)
176 if useSdk && sdkVersion != "current" {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700177 // Native coverage is not supported for SDK versions < 23
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400178 if fromApi, err := strconv.Atoi(sdkVersion); err == nil && fromApi < 23 {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700179 needCoverageVariant = false
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800180 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800181 }
182
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800183 if needCoverageVariant {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700184 // Coverage variant is actually built with coverage if enabled for its module path
Roland Levillain4f5297b2020-06-09 12:44:06 +0100185 needCoverageBuild = ctx.DeviceConfig().NativeCoverageEnabledForPath(ctx.ModuleDir())
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700186 }
187 }
188
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400189 properties.NeedCoverageBuild = needCoverageBuild
190 properties.NeedCoverageVariant = needCoverageVariant
191
192 return properties
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700193}
194
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900195// Coverage is an interface for non-CC modules to implement to be mutated for coverage
196type Coverage interface {
197 android.Module
198 IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool
199 PreventInstall()
200 HideFromMake()
Jiyong Park83dc74b2020-01-14 18:38:44 +0900201 MarkAsCoverageVariant(bool)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400202 EnableCoverageIfNeeded()
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900203}
204
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700205func coverageMutator(mctx android.BottomUpMutatorContext) {
206 if c, ok := mctx.Module().(*Module); ok && c.coverage != nil {
207 needCoverageVariant := c.coverage.Properties.NeedCoverageVariant
208 needCoverageBuild := c.coverage.Properties.NeedCoverageBuild
209 if needCoverageVariant {
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800210 m := mctx.CreateVariations("", "cov")
211
212 // Setup the non-coverage version and set HideFromMake and
213 // PreventInstall to true.
214 m[0].(*Module).coverage.Properties.CoverageEnabled = false
215 m[0].(*Module).coverage.Properties.IsCoverageVariant = false
216 m[0].(*Module).Properties.HideFromMake = true
217 m[0].(*Module).Properties.PreventInstall = true
218
219 // The coverage-enabled version inherits HideFromMake,
220 // PreventInstall from the original module.
221 m[1].(*Module).coverage.Properties.CoverageEnabled = needCoverageBuild
222 m[1].(*Module).coverage.Properties.IsCoverageVariant = true
Dan Willemsen581341d2017-02-09 16:16:31 -0800223 }
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900224 } else if cov, ok := mctx.Module().(Coverage); ok && cov.IsNativeCoverageNeeded(mctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400225 // APEX and Rust modules fall here
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900226
227 // Note: variant "" is also created because an APEX can be depended on by another
228 // module which are split into "" and "cov" variants. e.g. when cc_test refers
229 // to an APEX via 'data' property.
230 m := mctx.CreateVariations("", "cov")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400231 m[0].(Coverage).MarkAsCoverageVariant(false)
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900232 m[0].(Coverage).PreventInstall()
233 m[0].(Coverage).HideFromMake()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400234
235 m[1].(Coverage).MarkAsCoverageVariant(true)
236 m[1].(Coverage).EnableCoverageIfNeeded()
Dan Willemsen581341d2017-02-09 16:16:31 -0800237 }
238}