blob: db1b573e42095da8e6b40225e86d35ca71cd8829 [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.
Liz Kammer3847f212023-07-14 15:24:35 -040051 // LINT.IfChange
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070052 if ctx.useSdk() {
53 return "libprofile-extras_ndk"
54 } else {
55 return "libprofile-extras"
56 }
57}
58
Oliver Nguyen1382ab62019-12-06 15:22:41 -080059func getClangProfileLibraryName(ctx ModuleContextIntf) string {
60 if ctx.useSdk() {
61 return "libprofile-clang-extras_ndk"
Cindy Zhou5d5cfc12021-01-09 08:25:22 -080062 } else if ctx.isCfiAssemblySupportEnabled() {
63 return "libprofile-clang-extras_cfi_support"
Oliver Nguyen1382ab62019-12-06 15:22:41 -080064 } else {
65 return "libprofile-clang-extras"
66 }
Liz Kammer3847f212023-07-14 15:24:35 -040067 // LINT.ThenChange(library.go)
Oliver Nguyen1382ab62019-12-06 15:22:41 -080068}
69
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070070func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps {
71 if cov.Properties.NeedCoverageVariant {
72 ctx.AddVariationDependencies([]blueprint.Variation{
73 {Mutator: "link", Variation: "static"},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040074 }, CoverageDepTag, getGcovProfileLibraryName(ctx))
Oliver Nguyen1382ab62019-12-06 15:22:41 -080075 ctx.AddVariationDependencies([]blueprint.Variation{
76 {Mutator: "link", Variation: "static"},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040077 }, CoverageDepTag, getClangProfileLibraryName(ctx))
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -070078 }
Dan Willemsen581341d2017-02-09 16:16:31 -080079 return deps
80}
81
Pirama Arumuga Nainarb37ae582022-01-26 22:14:32 -080082func EnableContinuousCoverage(ctx android.BaseModuleContext) bool {
83 return ctx.DeviceConfig().ClangCoverageContinuousMode()
84}
85
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070086func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
Oliver Nguyen1382ab62019-12-06 15:22:41 -080087 clangCoverage := ctx.DeviceConfig().ClangCoverageEnabled()
Colin Cross1a6acd42020-06-16 17:51:46 -070088 gcovCoverage := ctx.DeviceConfig().GcovCoverageEnabled()
Oliver Nguyen1382ab62019-12-06 15:22:41 -080089
90 if !gcovCoverage && !clangCoverage {
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070091 return flags, deps
Dan Willemsen581341d2017-02-09 16:16:31 -080092 }
93
94 if cov.Properties.CoverageEnabled {
Dan Willemsen581341d2017-02-09 16:16:31 -080095 cov.linkCoverage = true
Pirama Arumuga Nainarc7679de2019-02-18 22:23:42 -080096
Oliver Nguyen1382ab62019-12-06 15:22:41 -080097 if gcovCoverage {
Oliver Nguyen04526782020-04-21 12:40:27 -070098 flags.GcovCoverage = true
Oliver Nguyen1382ab62019-12-06 15:22:41 -080099 flags.Local.CommonFlags = append(flags.Local.CommonFlags, "--coverage", "-O0")
100
101 // Override -Wframe-larger-than and non-default optimization
102 // flags that the module may use.
103 flags.Local.CFlags = append(flags.Local.CFlags, "-Wno-frame-larger-than=", "-O0")
104 } else if clangCoverage {
Pirama Arumuga Nainar551b06d2021-04-27 15:47:43 -0700105 flags.Local.CommonFlags = append(flags.Local.CommonFlags, profileInstrFlag,
106 "-fcoverage-mapping", "-Wno-pass-failed", "-D__ANDROID_CLANG_COVERAGE__")
Pirama Arumuga Nainarf776c8c2022-01-27 10:46:26 -0800107 // Override -Wframe-larger-than. We can expect frame size increase after
108 // coverage instrumentation.
109 flags.Local.CFlags = append(flags.Local.CFlags, "-Wno-frame-larger-than=")
Pirama Arumuga Nainarb37ae582022-01-26 22:14:32 -0800110 if EnableContinuousCoverage(ctx) {
111 flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-mllvm", "-runtime-counter-relocation")
112 }
Pirama Arumuga Nainar2bcdf5e2022-10-04 23:52:48 +0000113
114 // http://b/248022906, http://b/247941801 enabling coverage and hwasan-globals
115 // instrumentation together causes duplicate-symbol errors for __llvm_profile_filename.
116 if c, ok := ctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(Hwasan) {
117 flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-mllvm", "-hwasan-globals=0")
118 }
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800119 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800120 }
121
122 // Even if we don't have coverage enabled, if any of our object files were compiled
123 // with coverage, then we need to add --coverage to our ldflags.
124 if !cov.linkCoverage {
125 if ctx.static() && !ctx.staticBinary() {
126 // For static libraries, the only thing that changes our object files
127 // are included whole static libraries, so check to see if any of
128 // those have coverage enabled.
Colin Cross6e511a92020-07-27 21:26:48 -0700129 ctx.VisitDirectDeps(func(m android.Module) {
130 if depTag, ok := ctx.OtherModuleDependencyTag(m).(libraryDependencyTag); ok {
131 if depTag.static() && depTag.wholeStatic {
132 if cc, ok := m.(*Module); ok && cc.coverage != nil {
133 if cc.coverage.linkCoverage {
134 cov.linkCoverage = true
135 }
136 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800137 }
138 }
139 })
140 } else {
141 // For executables and shared libraries, we need to check all of
142 // our static dependencies.
Colin Crossd11fcda2017-10-23 17:59:01 -0700143 ctx.VisitDirectDeps(func(m android.Module) {
Dan Willemsen581341d2017-02-09 16:16:31 -0800144 cc, ok := m.(*Module)
145 if !ok || cc.coverage == nil {
146 return
147 }
148
149 if static, ok := cc.linker.(libraryInterface); !ok || !static.static() {
150 return
151 }
152
153 if cc.coverage.linkCoverage {
154 cov.linkCoverage = true
155 }
156 })
157 }
158 }
159
160 if cov.linkCoverage {
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800161 if gcovCoverage {
162 flags.Local.LdFlags = append(flags.Local.LdFlags, "--coverage")
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700163
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400164 coverage := ctx.GetDirectDepWithTag(getGcovProfileLibraryName(ctx), CoverageDepTag).(*Module)
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800165 deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path())
Pirama Arumuga Nainar100bbdc2019-07-02 23:47:35 -0700166
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800167 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--wrap,getenv")
168 } else if clangCoverage {
Pirama Arumuga Nainarf45e1522020-08-05 10:08:30 -0700169 flags.Local.LdFlags = append(flags.Local.LdFlags, profileInstrFlag)
Pirama Arumuga Nainarb37ae582022-01-26 22:14:32 -0800170 if EnableContinuousCoverage(ctx) {
171 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm=-runtime-counter-relocation")
172 }
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800173
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400174 coverage := ctx.GetDirectDepWithTag(getClangProfileLibraryName(ctx), CoverageDepTag).(*Module)
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800175 deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path())
Pirama Arumuga Nainar9464b6c2020-12-10 10:45:33 -0800176 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--wrap,open")
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800177 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800178 }
179
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -0700180 return flags, deps
Dan Willemsen581341d2017-02-09 16:16:31 -0800181}
182
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700183func (cov *coverage) begin(ctx BaseModuleContext) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400184 if ctx.Host() {
185 // TODO(dwillemsen): because of -nodefaultlibs, we must depend on libclang_rt.profile-*.a
186 // Just turn off for now.
187 } else {
188 cov.Properties = SetCoverageProperties(ctx, cov.Properties, ctx.nativeCoverage(), ctx.useSdk(), ctx.sdkVersion())
189 }
190}
191
192func SetCoverageProperties(ctx android.BaseModuleContext, properties CoverageProperties, moduleTypeHasCoverage bool,
193 useSdk bool, sdkVersion string) CoverageProperties {
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800194 // Coverage is disabled globally
Colin Cross1a6acd42020-06-16 17:51:46 -0700195 if !ctx.DeviceConfig().NativeCoverageEnabled() {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400196 return properties
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800197 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800198
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700199 var needCoverageVariant bool
200 var needCoverageBuild bool
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800201
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400202 if moduleTypeHasCoverage {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700203 // Check if Native_coverage is set to false. This property defaults to true.
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400204 needCoverageVariant = BoolDefault(properties.Native_coverage, true)
205 if useSdk && sdkVersion != "current" {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700206 // Native coverage is not supported for SDK versions < 23
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400207 if fromApi, err := strconv.Atoi(sdkVersion); err == nil && fromApi < 23 {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700208 needCoverageVariant = false
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800209 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800210 }
211
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800212 if needCoverageVariant {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700213 // Coverage variant is actually built with coverage if enabled for its module path
Roland Levillain4f5297b2020-06-09 12:44:06 +0100214 needCoverageBuild = ctx.DeviceConfig().NativeCoverageEnabledForPath(ctx.ModuleDir())
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700215 }
216 }
217
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400218 properties.NeedCoverageBuild = needCoverageBuild
219 properties.NeedCoverageVariant = needCoverageVariant
220
221 return properties
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700222}
223
Jooyung Hane6067592023-03-16 13:11:17 +0900224type UseCoverage interface {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900225 android.Module
Colin Crossf5f4ad32024-01-19 15:41:48 -0800226 IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool
Jooyung Hane6067592023-03-16 13:11:17 +0900227}
228
229// Coverage is an interface for non-CC modules to implement to be mutated for coverage
230type Coverage interface {
231 UseCoverage
Ivan Lozanod7586b62021-04-01 09:49:36 -0400232 SetPreventInstall()
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900233 HideFromMake()
Jiyong Park83dc74b2020-01-14 18:38:44 +0900234 MarkAsCoverageVariant(bool)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400235 EnableCoverageIfNeeded()
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900236}
237
Colin Crossf5f4ad32024-01-19 15:41:48 -0800238type coverageTransitionMutator struct{}
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800239
Colin Crossf5f4ad32024-01-19 15:41:48 -0800240var _ android.TransitionMutator = (*coverageTransitionMutator)(nil)
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800241
Colin Crossf5f4ad32024-01-19 15:41:48 -0800242func (c coverageTransitionMutator) Split(ctx android.BaseModuleContext) []string {
243 if c, ok := ctx.Module().(*Module); ok && c.coverage != nil {
244 if c.coverage.Properties.NeedCoverageVariant {
245 return []string{"", "cov"}
Dan Willemsen581341d2017-02-09 16:16:31 -0800246 }
Colin Crossf5f4ad32024-01-19 15:41:48 -0800247 } else if cov, ok := ctx.Module().(Coverage); ok && cov.IsNativeCoverageNeeded(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400248 // APEX and Rust modules fall here
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900249
250 // Note: variant "" is also created because an APEX can be depended on by another
251 // module which are split into "" and "cov" variants. e.g. when cc_test refers
252 // to an APEX via 'data' property.
Colin Crossf5f4ad32024-01-19 15:41:48 -0800253 return []string{"", "cov"}
254 } else if cov, ok := ctx.Module().(UseCoverage); ok && cov.IsNativeCoverageNeeded(ctx) {
Jooyung Hane6067592023-03-16 13:11:17 +0900255 // Module itself doesn't have to have "cov" variant, but it should use "cov" variants of
256 // deps.
Colin Crossf5f4ad32024-01-19 15:41:48 -0800257 return []string{"cov"}
258 }
259
260 return []string{""}
261}
262
263func (c coverageTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
264 return sourceVariation
265}
266
267func (c coverageTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
268 if c, ok := ctx.Module().(*Module); ok && c.coverage != nil {
269 if !c.coverage.Properties.NeedCoverageVariant {
270 return ""
271 }
272 } else if cov, ok := ctx.Module().(Coverage); ok {
273 if !cov.IsNativeCoverageNeeded(ctx) {
274 return ""
275 }
276 } else if cov, ok := ctx.Module().(UseCoverage); ok && cov.IsNativeCoverageNeeded(ctx) {
277 // Module only has a "cov" variation, so all incoming variations should use "cov".
278 return "cov"
279 } else {
280 return ""
281 }
282
283 return incomingVariation
284}
285
286func (c coverageTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
287 if c, ok := ctx.Module().(*Module); ok && c.coverage != nil {
288 if variation == "" && c.coverage.Properties.NeedCoverageVariant {
289 // Setup the non-coverage version and set HideFromMake and
290 // PreventInstall to true.
291 c.coverage.Properties.CoverageEnabled = false
292 c.coverage.Properties.IsCoverageVariant = false
293 c.Properties.HideFromMake = true
294 c.Properties.PreventInstall = true
295 } else if variation == "cov" {
296 // The coverage-enabled version inherits HideFromMake,
297 // PreventInstall from the original module.
298 c.coverage.Properties.CoverageEnabled = c.coverage.Properties.NeedCoverageBuild
299 c.coverage.Properties.IsCoverageVariant = true
300 }
301 } else if cov, ok := ctx.Module().(Coverage); ok && cov.IsNativeCoverageNeeded(ctx) {
302 // APEX and Rust modules fall here
303
304 // Note: variant "" is also created because an APEX can be depended on by another
305 // module which are split into "" and "cov" variants. e.g. when cc_test refers
306 // to an APEX via 'data' property.
307 if variation == "" {
308 cov.MarkAsCoverageVariant(false)
309 cov.SetPreventInstall()
310 cov.HideFromMake()
311 } else if variation == "cov" {
312 cov.MarkAsCoverageVariant(true)
313 cov.EnableCoverageIfNeeded()
314 }
315 } else if cov, ok := ctx.Module().(UseCoverage); ok && cov.IsNativeCoverageNeeded(ctx) {
316 // Module itself doesn't have to have "cov" variant, but it should use "cov" variants of
317 // deps.
Dan Willemsen581341d2017-02-09 16:16:31 -0800318 }
319}
sophiez4c4f8032021-08-16 22:54:00 -0700320
321func parseSymbolFileForAPICoverage(ctx ModuleContext, symbolFile string) android.ModuleOutPath {
322 apiLevelsJson := android.GetApiLevelsJson(ctx)
323 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
324 outputFile := ctx.baseModuleName() + ".xml"
325 parsedApiCoveragePath := android.PathForModuleOut(ctx, outputFile)
326 rule := android.NewRuleBuilder(pctx, ctx)
327 rule.Command().
328 BuiltTool("ndk_api_coverage_parser").
329 Input(symbolFilePath).
330 Output(parsedApiCoveragePath).
331 Implicit(apiLevelsJson).
332 FlagWithArg("--api-map ", apiLevelsJson.String())
333 rule.Build("native_library_api_list", "Generate native API list based on symbol files for coverage measurement")
334 return parsedApiCoveragePath
335}