blob: f6092e4473b247de038e30f73fb25e88069bb250 [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
Gabriel Chen1589c962023-11-30 20:29:44 -080025var (
26 clangCoverageHostLdFlags = []string{
27 "-Wl,--no-as-needed",
28 "-Wl,--wrap,open",
29 }
30 clangContinuousCoverageFlags = []string{
31 "-mllvm",
32 "-runtime-counter-relocation",
33 }
34 clangCoverageCFlags = []string{
35 "-Wno-frame-larger-than=",
36 }
37 clangCoverageCommonFlags = []string{
38 "-fcoverage-mapping",
39 "-Wno-pass-failed",
40 "-D__ANDROID_CLANG_COVERAGE__",
41 }
42 clangCoverageHWASanFlags = []string{
43 "-mllvm",
44 "-hwasan-globals=0",
45 }
46)
47
Pirama Arumuga Nainarf45e1522020-08-05 10:08:30 -070048const profileInstrFlag = "-fprofile-instr-generate=/data/misc/trace/clang-%p-%m.profraw"
49
Dan Willemsen581341d2017-02-09 16:16:31 -080050type CoverageProperties struct {
51 Native_coverage *bool
52
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -070053 NeedCoverageVariant bool `blueprint:"mutated"`
54 NeedCoverageBuild bool `blueprint:"mutated"`
55
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -080056 CoverageEnabled bool `blueprint:"mutated"`
57 IsCoverageVariant bool `blueprint:"mutated"`
Dan Willemsen581341d2017-02-09 16:16:31 -080058}
59
60type coverage struct {
61 Properties CoverageProperties
62
63 // Whether binaries containing this module need --coverage added to their ldflags
64 linkCoverage bool
65}
66
67func (cov *coverage) props() []interface{} {
68 return []interface{}{&cov.Properties}
69}
70
Oliver Nguyen1382ab62019-12-06 15:22:41 -080071func getGcovProfileLibraryName(ctx ModuleContextIntf) string {
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070072 // This function should only ever be called for a cc.Module, so the
73 // following statement should always succeed.
Liz Kammer3847f212023-07-14 15:24:35 -040074 // LINT.IfChange
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070075 if ctx.useSdk() {
76 return "libprofile-extras_ndk"
77 } else {
78 return "libprofile-extras"
79 }
80}
81
Oliver Nguyen1382ab62019-12-06 15:22:41 -080082func getClangProfileLibraryName(ctx ModuleContextIntf) string {
83 if ctx.useSdk() {
84 return "libprofile-clang-extras_ndk"
Cindy Zhou5d5cfc12021-01-09 08:25:22 -080085 } else if ctx.isCfiAssemblySupportEnabled() {
86 return "libprofile-clang-extras_cfi_support"
Oliver Nguyen1382ab62019-12-06 15:22:41 -080087 } else {
88 return "libprofile-clang-extras"
89 }
Liz Kammer3847f212023-07-14 15:24:35 -040090 // LINT.ThenChange(library.go)
Oliver Nguyen1382ab62019-12-06 15:22:41 -080091}
92
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070093func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps {
Yu Liuaa89f2c2023-12-12 23:07:33 +000094 if cov.Properties.NeedCoverageVariant && ctx.Device() {
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070095 ctx.AddVariationDependencies([]blueprint.Variation{
96 {Mutator: "link", Variation: "static"},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040097 }, CoverageDepTag, getGcovProfileLibraryName(ctx))
Oliver Nguyen1382ab62019-12-06 15:22:41 -080098 ctx.AddVariationDependencies([]blueprint.Variation{
99 {Mutator: "link", Variation: "static"},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400100 }, CoverageDepTag, getClangProfileLibraryName(ctx))
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700101 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800102 return deps
103}
104
Pirama Arumuga Nainarb37ae582022-01-26 22:14:32 -0800105func EnableContinuousCoverage(ctx android.BaseModuleContext) bool {
106 return ctx.DeviceConfig().ClangCoverageContinuousMode()
107}
108
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -0700109func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800110 clangCoverage := ctx.DeviceConfig().ClangCoverageEnabled()
Colin Cross1a6acd42020-06-16 17:51:46 -0700111 gcovCoverage := ctx.DeviceConfig().GcovCoverageEnabled()
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800112
113 if !gcovCoverage && !clangCoverage {
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -0700114 return flags, deps
Dan Willemsen581341d2017-02-09 16:16:31 -0800115 }
116
117 if cov.Properties.CoverageEnabled {
Dan Willemsen581341d2017-02-09 16:16:31 -0800118 cov.linkCoverage = true
Pirama Arumuga Nainarc7679de2019-02-18 22:23:42 -0800119
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800120 if gcovCoverage {
Oliver Nguyen04526782020-04-21 12:40:27 -0700121 flags.GcovCoverage = true
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800122 flags.Local.CommonFlags = append(flags.Local.CommonFlags, "--coverage", "-O0")
123
124 // Override -Wframe-larger-than and non-default optimization
125 // flags that the module may use.
126 flags.Local.CFlags = append(flags.Local.CFlags, "-Wno-frame-larger-than=", "-O0")
127 } else if clangCoverage {
Gabriel Chen1589c962023-11-30 20:29:44 -0800128 flags.Local.CommonFlags = append(flags.Local.CommonFlags, profileInstrFlag)
129 flags.Local.CommonFlags = append(flags.Local.CommonFlags, clangCoverageCommonFlags...)
Pirama Arumuga Nainarf776c8c2022-01-27 10:46:26 -0800130 // Override -Wframe-larger-than. We can expect frame size increase after
131 // coverage instrumentation.
Gabriel Chen1589c962023-11-30 20:29:44 -0800132 flags.Local.CFlags = append(flags.Local.CFlags, clangCoverageCFlags...)
Pirama Arumuga Nainarb37ae582022-01-26 22:14:32 -0800133 if EnableContinuousCoverage(ctx) {
Gabriel Chen1589c962023-11-30 20:29:44 -0800134 flags.Local.CommonFlags = append(flags.Local.CommonFlags, clangContinuousCoverageFlags...)
Pirama Arumuga Nainarb37ae582022-01-26 22:14:32 -0800135 }
Pirama Arumuga Nainar2bcdf5e2022-10-04 23:52:48 +0000136
137 // http://b/248022906, http://b/247941801 enabling coverage and hwasan-globals
138 // instrumentation together causes duplicate-symbol errors for __llvm_profile_filename.
139 if c, ok := ctx.Module().(*Module); ok && c.sanitize.isSanitizerEnabled(Hwasan) {
Gabriel Chen1589c962023-11-30 20:29:44 -0800140 flags.Local.CommonFlags = append(flags.Local.CommonFlags, clangCoverageHWASanFlags...)
Pirama Arumuga Nainar2bcdf5e2022-10-04 23:52:48 +0000141 }
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800142 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800143 }
144
145 // Even if we don't have coverage enabled, if any of our object files were compiled
146 // with coverage, then we need to add --coverage to our ldflags.
147 if !cov.linkCoverage {
148 if ctx.static() && !ctx.staticBinary() {
149 // For static libraries, the only thing that changes our object files
150 // are included whole static libraries, so check to see if any of
151 // those have coverage enabled.
Colin Cross6e511a92020-07-27 21:26:48 -0700152 ctx.VisitDirectDeps(func(m android.Module) {
153 if depTag, ok := ctx.OtherModuleDependencyTag(m).(libraryDependencyTag); ok {
154 if depTag.static() && depTag.wholeStatic {
155 if cc, ok := m.(*Module); ok && cc.coverage != nil {
156 if cc.coverage.linkCoverage {
157 cov.linkCoverage = true
158 }
159 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800160 }
161 }
162 })
163 } else {
164 // For executables and shared libraries, we need to check all of
165 // our static dependencies.
Colin Crossd11fcda2017-10-23 17:59:01 -0700166 ctx.VisitDirectDeps(func(m android.Module) {
Dan Willemsen581341d2017-02-09 16:16:31 -0800167 cc, ok := m.(*Module)
168 if !ok || cc.coverage == nil {
169 return
170 }
171
172 if static, ok := cc.linker.(libraryInterface); !ok || !static.static() {
173 return
174 }
175
176 if cc.coverage.linkCoverage {
177 cov.linkCoverage = true
178 }
179 })
180 }
181 }
182
183 if cov.linkCoverage {
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800184 if gcovCoverage {
185 flags.Local.LdFlags = append(flags.Local.LdFlags, "--coverage")
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700186
Yu Liuaa89f2c2023-12-12 23:07:33 +0000187 if ctx.Device() {
188 coverage := ctx.GetDirectDepWithTag(getGcovProfileLibraryName(ctx), CoverageDepTag).(*Module)
189 deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path())
190 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--wrap,getenv")
191 }
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800192 } else if clangCoverage {
Pirama Arumuga Nainarf45e1522020-08-05 10:08:30 -0700193 flags.Local.LdFlags = append(flags.Local.LdFlags, profileInstrFlag)
Pirama Arumuga Nainarb37ae582022-01-26 22:14:32 -0800194 if EnableContinuousCoverage(ctx) {
195 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm=-runtime-counter-relocation")
196 }
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800197
Yu Liuaa89f2c2023-12-12 23:07:33 +0000198 if ctx.Device() {
199 coverage := ctx.GetDirectDepWithTag(getClangProfileLibraryName(ctx), CoverageDepTag).(*Module)
200 deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path())
201 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--wrap,open")
202 }
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800203 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800204 }
205
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -0700206 return flags, deps
Dan Willemsen581341d2017-02-09 16:16:31 -0800207}
208
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700209func (cov *coverage) begin(ctx BaseModuleContext) {
Yu Liuaa89f2c2023-12-12 23:07:33 +0000210 if ctx.Host() && !ctx.Os().Linux() {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400211 // TODO(dwillemsen): because of -nodefaultlibs, we must depend on libclang_rt.profile-*.a
212 // Just turn off for now.
213 } else {
214 cov.Properties = SetCoverageProperties(ctx, cov.Properties, ctx.nativeCoverage(), ctx.useSdk(), ctx.sdkVersion())
215 }
216}
217
218func SetCoverageProperties(ctx android.BaseModuleContext, properties CoverageProperties, moduleTypeHasCoverage bool,
219 useSdk bool, sdkVersion string) CoverageProperties {
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800220 // Coverage is disabled globally
Colin Cross1a6acd42020-06-16 17:51:46 -0700221 if !ctx.DeviceConfig().NativeCoverageEnabled() {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400222 return properties
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800223 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800224
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700225 var needCoverageVariant bool
226 var needCoverageBuild bool
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800227
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400228 if moduleTypeHasCoverage {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700229 // Check if Native_coverage is set to false. This property defaults to true.
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400230 needCoverageVariant = BoolDefault(properties.Native_coverage, true)
231 if useSdk && sdkVersion != "current" {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700232 // Native coverage is not supported for SDK versions < 23
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400233 if fromApi, err := strconv.Atoi(sdkVersion); err == nil && fromApi < 23 {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700234 needCoverageVariant = false
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800235 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800236 }
237
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800238 if needCoverageVariant {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700239 // Coverage variant is actually built with coverage if enabled for its module path
Roland Levillain4f5297b2020-06-09 12:44:06 +0100240 needCoverageBuild = ctx.DeviceConfig().NativeCoverageEnabledForPath(ctx.ModuleDir())
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700241 }
242 }
243
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400244 properties.NeedCoverageBuild = needCoverageBuild
245 properties.NeedCoverageVariant = needCoverageVariant
246
247 return properties
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700248}
249
Jooyung Hane6067592023-03-16 13:11:17 +0900250type UseCoverage interface {
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900251 android.Module
Colin Crossf5f4ad32024-01-19 15:41:48 -0800252 IsNativeCoverageNeeded(ctx android.IncomingTransitionContext) bool
Jooyung Hane6067592023-03-16 13:11:17 +0900253}
254
255// Coverage is an interface for non-CC modules to implement to be mutated for coverage
256type Coverage interface {
257 UseCoverage
Ivan Lozanod7586b62021-04-01 09:49:36 -0400258 SetPreventInstall()
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900259 HideFromMake()
Jiyong Park83dc74b2020-01-14 18:38:44 +0900260 MarkAsCoverageVariant(bool)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400261 EnableCoverageIfNeeded()
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900262}
263
Colin Crossf5f4ad32024-01-19 15:41:48 -0800264type coverageTransitionMutator struct{}
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800265
Colin Crossf5f4ad32024-01-19 15:41:48 -0800266var _ android.TransitionMutator = (*coverageTransitionMutator)(nil)
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800267
Colin Crossf5f4ad32024-01-19 15:41:48 -0800268func (c coverageTransitionMutator) Split(ctx android.BaseModuleContext) []string {
269 if c, ok := ctx.Module().(*Module); ok && c.coverage != nil {
270 if c.coverage.Properties.NeedCoverageVariant {
271 return []string{"", "cov"}
Dan Willemsen581341d2017-02-09 16:16:31 -0800272 }
Colin Crossf5f4ad32024-01-19 15:41:48 -0800273 } else if cov, ok := ctx.Module().(Coverage); ok && cov.IsNativeCoverageNeeded(ctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400274 // APEX and Rust modules fall here
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900275
276 // Note: variant "" is also created because an APEX can be depended on by another
277 // module which are split into "" and "cov" variants. e.g. when cc_test refers
278 // to an APEX via 'data' property.
Colin Crossf5f4ad32024-01-19 15:41:48 -0800279 return []string{"", "cov"}
280 } else if cov, ok := ctx.Module().(UseCoverage); ok && cov.IsNativeCoverageNeeded(ctx) {
Jooyung Hane6067592023-03-16 13:11:17 +0900281 // Module itself doesn't have to have "cov" variant, but it should use "cov" variants of
282 // deps.
Colin Crossf5f4ad32024-01-19 15:41:48 -0800283 return []string{"cov"}
284 }
285
286 return []string{""}
287}
288
289func (c coverageTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string {
290 return sourceVariation
291}
292
293func (c coverageTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string {
294 if c, ok := ctx.Module().(*Module); ok && c.coverage != nil {
295 if !c.coverage.Properties.NeedCoverageVariant {
296 return ""
297 }
298 } else if cov, ok := ctx.Module().(Coverage); ok {
299 if !cov.IsNativeCoverageNeeded(ctx) {
300 return ""
301 }
302 } else if cov, ok := ctx.Module().(UseCoverage); ok && cov.IsNativeCoverageNeeded(ctx) {
303 // Module only has a "cov" variation, so all incoming variations should use "cov".
304 return "cov"
305 } else {
306 return ""
307 }
308
309 return incomingVariation
310}
311
312func (c coverageTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) {
313 if c, ok := ctx.Module().(*Module); ok && c.coverage != nil {
314 if variation == "" && c.coverage.Properties.NeedCoverageVariant {
315 // Setup the non-coverage version and set HideFromMake and
316 // PreventInstall to true.
317 c.coverage.Properties.CoverageEnabled = false
318 c.coverage.Properties.IsCoverageVariant = false
319 c.Properties.HideFromMake = true
320 c.Properties.PreventInstall = true
321 } else if variation == "cov" {
322 // The coverage-enabled version inherits HideFromMake,
323 // PreventInstall from the original module.
324 c.coverage.Properties.CoverageEnabled = c.coverage.Properties.NeedCoverageBuild
325 c.coverage.Properties.IsCoverageVariant = true
326 }
327 } else if cov, ok := ctx.Module().(Coverage); ok && cov.IsNativeCoverageNeeded(ctx) {
328 // APEX and Rust modules fall here
329
330 // Note: variant "" is also created because an APEX can be depended on by another
331 // module which are split into "" and "cov" variants. e.g. when cc_test refers
332 // to an APEX via 'data' property.
333 if variation == "" {
334 cov.MarkAsCoverageVariant(false)
335 cov.SetPreventInstall()
336 cov.HideFromMake()
337 } else if variation == "cov" {
338 cov.MarkAsCoverageVariant(true)
339 cov.EnableCoverageIfNeeded()
340 }
341 } else if cov, ok := ctx.Module().(UseCoverage); ok && cov.IsNativeCoverageNeeded(ctx) {
342 // Module itself doesn't have to have "cov" variant, but it should use "cov" variants of
343 // deps.
Dan Willemsen581341d2017-02-09 16:16:31 -0800344 }
345}
sophiez4c4f8032021-08-16 22:54:00 -0700346
347func parseSymbolFileForAPICoverage(ctx ModuleContext, symbolFile string) android.ModuleOutPath {
348 apiLevelsJson := android.GetApiLevelsJson(ctx)
349 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
350 outputFile := ctx.baseModuleName() + ".xml"
351 parsedApiCoveragePath := android.PathForModuleOut(ctx, outputFile)
352 rule := android.NewRuleBuilder(pctx, ctx)
353 rule.Command().
354 BuiltTool("ndk_api_coverage_parser").
355 Input(symbolFilePath).
356 Output(parsedApiCoveragePath).
357 Implicit(apiLevelsJson).
358 FlagWithArg("--api-map ", apiLevelsJson.String())
359 rule.Build("native_library_api_list", "Generate native API list based on symbol files for coverage measurement")
360 return parsedApiCoveragePath
361}