blob: 5b5ccf2f03de88b673e310366c2a5eb868179248 [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"
Cindy Zhou5d5cfc12021-01-09 08:25:22 -080061 } else if ctx.isCfiAssemblySupportEnabled() {
62 return "libprofile-clang-extras_cfi_support"
Oliver Nguyen1382ab62019-12-06 15:22:41 -080063 } else {
64 return "libprofile-clang-extras"
65 }
66}
67
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070068func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps {
69 if cov.Properties.NeedCoverageVariant {
70 ctx.AddVariationDependencies([]blueprint.Variation{
71 {Mutator: "link", Variation: "static"},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040072 }, CoverageDepTag, getGcovProfileLibraryName(ctx))
Oliver Nguyen1382ab62019-12-06 15:22:41 -080073 ctx.AddVariationDependencies([]blueprint.Variation{
74 {Mutator: "link", Variation: "static"},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040075 }, CoverageDepTag, getClangProfileLibraryName(ctx))
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -070076 }
Dan Willemsen581341d2017-02-09 16:16:31 -080077 return deps
78}
79
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070080func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
Oliver Nguyen1382ab62019-12-06 15:22:41 -080081 clangCoverage := ctx.DeviceConfig().ClangCoverageEnabled()
Colin Cross1a6acd42020-06-16 17:51:46 -070082 gcovCoverage := ctx.DeviceConfig().GcovCoverageEnabled()
Oliver Nguyen1382ab62019-12-06 15:22:41 -080083
84 if !gcovCoverage && !clangCoverage {
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070085 return flags, deps
Dan Willemsen581341d2017-02-09 16:16:31 -080086 }
87
88 if cov.Properties.CoverageEnabled {
Dan Willemsen581341d2017-02-09 16:16:31 -080089 cov.linkCoverage = true
Pirama Arumuga Nainarc7679de2019-02-18 22:23:42 -080090
Oliver Nguyen1382ab62019-12-06 15:22:41 -080091 if gcovCoverage {
Oliver Nguyen04526782020-04-21 12:40:27 -070092 flags.GcovCoverage = true
Oliver Nguyen1382ab62019-12-06 15:22:41 -080093 flags.Local.CommonFlags = append(flags.Local.CommonFlags, "--coverage", "-O0")
94
95 // Override -Wframe-larger-than and non-default optimization
96 // flags that the module may use.
97 flags.Local.CFlags = append(flags.Local.CFlags, "-Wno-frame-larger-than=", "-O0")
98 } else if clangCoverage {
Pirama Arumuga Nainarf45e1522020-08-05 10:08:30 -070099 flags.Local.CommonFlags = append(flags.Local.CommonFlags, profileInstrFlag, "-fcoverage-mapping", "-Wno-pass-failed")
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800100 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800101 }
102
103 // Even if we don't have coverage enabled, if any of our object files were compiled
104 // with coverage, then we need to add --coverage to our ldflags.
105 if !cov.linkCoverage {
106 if ctx.static() && !ctx.staticBinary() {
107 // For static libraries, the only thing that changes our object files
108 // are included whole static libraries, so check to see if any of
109 // those have coverage enabled.
Colin Cross6e511a92020-07-27 21:26:48 -0700110 ctx.VisitDirectDeps(func(m android.Module) {
111 if depTag, ok := ctx.OtherModuleDependencyTag(m).(libraryDependencyTag); ok {
112 if depTag.static() && depTag.wholeStatic {
113 if cc, ok := m.(*Module); ok && cc.coverage != nil {
114 if cc.coverage.linkCoverage {
115 cov.linkCoverage = true
116 }
117 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800118 }
119 }
120 })
121 } else {
122 // For executables and shared libraries, we need to check all of
123 // our static dependencies.
Colin Crossd11fcda2017-10-23 17:59:01 -0700124 ctx.VisitDirectDeps(func(m android.Module) {
Dan Willemsen581341d2017-02-09 16:16:31 -0800125 cc, ok := m.(*Module)
126 if !ok || cc.coverage == nil {
127 return
128 }
129
130 if static, ok := cc.linker.(libraryInterface); !ok || !static.static() {
131 return
132 }
133
134 if cc.coverage.linkCoverage {
135 cov.linkCoverage = true
136 }
137 })
138 }
139 }
140
141 if cov.linkCoverage {
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800142 if gcovCoverage {
143 flags.Local.LdFlags = append(flags.Local.LdFlags, "--coverage")
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700144
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400145 coverage := ctx.GetDirectDepWithTag(getGcovProfileLibraryName(ctx), CoverageDepTag).(*Module)
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800146 deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path())
Pirama Arumuga Nainar100bbdc2019-07-02 23:47:35 -0700147
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800148 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--wrap,getenv")
149 } else if clangCoverage {
Pirama Arumuga Nainarf45e1522020-08-05 10:08:30 -0700150 flags.Local.LdFlags = append(flags.Local.LdFlags, profileInstrFlag)
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800151
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400152 coverage := ctx.GetDirectDepWithTag(getClangProfileLibraryName(ctx), CoverageDepTag).(*Module)
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800153 deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path())
Pirama Arumuga Nainar9464b6c2020-12-10 10:45:33 -0800154 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--wrap,open")
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800155 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800156 }
157
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -0700158 return flags, deps
Dan Willemsen581341d2017-02-09 16:16:31 -0800159}
160
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700161func (cov *coverage) begin(ctx BaseModuleContext) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400162 if ctx.Host() {
163 // TODO(dwillemsen): because of -nodefaultlibs, we must depend on libclang_rt.profile-*.a
164 // Just turn off for now.
165 } else {
166 cov.Properties = SetCoverageProperties(ctx, cov.Properties, ctx.nativeCoverage(), ctx.useSdk(), ctx.sdkVersion())
167 }
168}
169
170func SetCoverageProperties(ctx android.BaseModuleContext, properties CoverageProperties, moduleTypeHasCoverage bool,
171 useSdk bool, sdkVersion string) CoverageProperties {
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800172 // Coverage is disabled globally
Colin Cross1a6acd42020-06-16 17:51:46 -0700173 if !ctx.DeviceConfig().NativeCoverageEnabled() {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400174 return properties
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800175 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800176
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700177 var needCoverageVariant bool
178 var needCoverageBuild bool
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800179
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400180 if moduleTypeHasCoverage {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700181 // Check if Native_coverage is set to false. This property defaults to true.
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400182 needCoverageVariant = BoolDefault(properties.Native_coverage, true)
183 if useSdk && sdkVersion != "current" {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700184 // Native coverage is not supported for SDK versions < 23
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400185 if fromApi, err := strconv.Atoi(sdkVersion); err == nil && fromApi < 23 {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700186 needCoverageVariant = false
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800187 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800188 }
189
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800190 if needCoverageVariant {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700191 // Coverage variant is actually built with coverage if enabled for its module path
Roland Levillain4f5297b2020-06-09 12:44:06 +0100192 needCoverageBuild = ctx.DeviceConfig().NativeCoverageEnabledForPath(ctx.ModuleDir())
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700193 }
194 }
195
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400196 properties.NeedCoverageBuild = needCoverageBuild
197 properties.NeedCoverageVariant = needCoverageVariant
198
199 return properties
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700200}
201
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900202// Coverage is an interface for non-CC modules to implement to be mutated for coverage
203type Coverage interface {
204 android.Module
205 IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool
206 PreventInstall()
207 HideFromMake()
Jiyong Park83dc74b2020-01-14 18:38:44 +0900208 MarkAsCoverageVariant(bool)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400209 EnableCoverageIfNeeded()
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900210}
211
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700212func coverageMutator(mctx android.BottomUpMutatorContext) {
213 if c, ok := mctx.Module().(*Module); ok && c.coverage != nil {
214 needCoverageVariant := c.coverage.Properties.NeedCoverageVariant
215 needCoverageBuild := c.coverage.Properties.NeedCoverageBuild
216 if needCoverageVariant {
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800217 m := mctx.CreateVariations("", "cov")
218
219 // Setup the non-coverage version and set HideFromMake and
220 // PreventInstall to true.
221 m[0].(*Module).coverage.Properties.CoverageEnabled = false
222 m[0].(*Module).coverage.Properties.IsCoverageVariant = false
223 m[0].(*Module).Properties.HideFromMake = true
224 m[0].(*Module).Properties.PreventInstall = true
225
226 // The coverage-enabled version inherits HideFromMake,
227 // PreventInstall from the original module.
228 m[1].(*Module).coverage.Properties.CoverageEnabled = needCoverageBuild
229 m[1].(*Module).coverage.Properties.IsCoverageVariant = true
Dan Willemsen581341d2017-02-09 16:16:31 -0800230 }
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900231 } else if cov, ok := mctx.Module().(Coverage); ok && cov.IsNativeCoverageNeeded(mctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400232 // APEX and Rust modules fall here
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900233
234 // Note: variant "" is also created because an APEX can be depended on by another
235 // module which are split into "" and "cov" variants. e.g. when cc_test refers
236 // to an APEX via 'data' property.
237 m := mctx.CreateVariations("", "cov")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400238 m[0].(Coverage).MarkAsCoverageVariant(false)
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900239 m[0].(Coverage).PreventInstall()
240 m[0].(Coverage).HideFromMake()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400241
242 m[1].(Coverage).MarkAsCoverageVariant(true)
243 m[1].(Coverage).EnableCoverageIfNeeded()
Dan Willemsen581341d2017-02-09 16:16:31 -0800244 }
245}