blob: 59c8864390ad4349e4b8e59648e3f7dbfa213878 [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 Nainar71d697c2021-06-08 16:05:18 -070025// Add '%c' to default specifier after we resolve http://b/210012154
Pirama Arumuga Nainarf45e1522020-08-05 10:08:30 -070026const profileInstrFlag = "-fprofile-instr-generate=/data/misc/trace/clang-%p-%m.profraw"
27
Dan Willemsen581341d2017-02-09 16:16:31 -080028type CoverageProperties struct {
29 Native_coverage *bool
30
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -070031 NeedCoverageVariant bool `blueprint:"mutated"`
32 NeedCoverageBuild bool `blueprint:"mutated"`
33
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -080034 CoverageEnabled bool `blueprint:"mutated"`
35 IsCoverageVariant bool `blueprint:"mutated"`
Dan Willemsen581341d2017-02-09 16:16:31 -080036}
37
38type coverage struct {
39 Properties CoverageProperties
40
41 // Whether binaries containing this module need --coverage added to their ldflags
42 linkCoverage bool
43}
44
45func (cov *coverage) props() []interface{} {
46 return []interface{}{&cov.Properties}
47}
48
Oliver Nguyen1382ab62019-12-06 15:22:41 -080049func getGcovProfileLibraryName(ctx ModuleContextIntf) string {
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070050 // This function should only ever be called for a cc.Module, so the
51 // following statement should always succeed.
52 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 }
67}
68
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -070069func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps {
70 if cov.Properties.NeedCoverageVariant {
71 ctx.AddVariationDependencies([]blueprint.Variation{
72 {Mutator: "link", Variation: "static"},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040073 }, CoverageDepTag, getGcovProfileLibraryName(ctx))
Oliver Nguyen1382ab62019-12-06 15:22:41 -080074 ctx.AddVariationDependencies([]blueprint.Variation{
75 {Mutator: "link", Variation: "static"},
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -040076 }, CoverageDepTag, getClangProfileLibraryName(ctx))
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -070077 }
Dan Willemsen581341d2017-02-09 16:16:31 -080078 return deps
79}
80
Pirama Arumuga Nainar71d697c2021-06-08 16:05:18 -070081func EnableContinuousCoverage(ctx android.BaseModuleContext) bool {
82 // http://b/210012154 Disable continuous coverage if we're instrumenting bionic/libc.
83 return !ctx.DeviceConfig().NativeCoverageEnabledForPath("bionic/libc")
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 Nainar71d697c2021-06-08 16:05:18 -0700107 if EnableContinuousCoverage(ctx) {
108 flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-mllvm", "-runtime-counter-relocation")
109 }
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800110 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800111 }
112
113 // Even if we don't have coverage enabled, if any of our object files were compiled
114 // with coverage, then we need to add --coverage to our ldflags.
115 if !cov.linkCoverage {
116 if ctx.static() && !ctx.staticBinary() {
117 // For static libraries, the only thing that changes our object files
118 // are included whole static libraries, so check to see if any of
119 // those have coverage enabled.
Colin Cross6e511a92020-07-27 21:26:48 -0700120 ctx.VisitDirectDeps(func(m android.Module) {
121 if depTag, ok := ctx.OtherModuleDependencyTag(m).(libraryDependencyTag); ok {
122 if depTag.static() && depTag.wholeStatic {
123 if cc, ok := m.(*Module); ok && cc.coverage != nil {
124 if cc.coverage.linkCoverage {
125 cov.linkCoverage = true
126 }
127 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800128 }
129 }
130 })
131 } else {
132 // For executables and shared libraries, we need to check all of
133 // our static dependencies.
Colin Crossd11fcda2017-10-23 17:59:01 -0700134 ctx.VisitDirectDeps(func(m android.Module) {
Dan Willemsen581341d2017-02-09 16:16:31 -0800135 cc, ok := m.(*Module)
136 if !ok || cc.coverage == nil {
137 return
138 }
139
140 if static, ok := cc.linker.(libraryInterface); !ok || !static.static() {
141 return
142 }
143
144 if cc.coverage.linkCoverage {
145 cov.linkCoverage = true
146 }
147 })
148 }
149 }
150
151 if cov.linkCoverage {
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800152 if gcovCoverage {
153 flags.Local.LdFlags = append(flags.Local.LdFlags, "--coverage")
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700154
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400155 coverage := ctx.GetDirectDepWithTag(getGcovProfileLibraryName(ctx), CoverageDepTag).(*Module)
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800156 deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path())
Pirama Arumuga Nainar100bbdc2019-07-02 23:47:35 -0700157
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800158 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--wrap,getenv")
159 } else if clangCoverage {
Pirama Arumuga Nainarf45e1522020-08-05 10:08:30 -0700160 flags.Local.LdFlags = append(flags.Local.LdFlags, profileInstrFlag)
Pirama Arumuga Nainar71d697c2021-06-08 16:05:18 -0700161 if EnableContinuousCoverage(ctx) {
162 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm=-runtime-counter-relocation")
163 }
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800164
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400165 coverage := ctx.GetDirectDepWithTag(getClangProfileLibraryName(ctx), CoverageDepTag).(*Module)
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800166 deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path())
Pirama Arumuga Nainar9464b6c2020-12-10 10:45:33 -0800167 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--wrap,open")
Oliver Nguyen1382ab62019-12-06 15:22:41 -0800168 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800169 }
170
Pirama Arumuga Nainar82fe59b2019-07-02 14:55:35 -0700171 return flags, deps
Dan Willemsen581341d2017-02-09 16:16:31 -0800172}
173
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700174func (cov *coverage) begin(ctx BaseModuleContext) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400175 if ctx.Host() {
176 // TODO(dwillemsen): because of -nodefaultlibs, we must depend on libclang_rt.profile-*.a
177 // Just turn off for now.
178 } else {
179 cov.Properties = SetCoverageProperties(ctx, cov.Properties, ctx.nativeCoverage(), ctx.useSdk(), ctx.sdkVersion())
180 }
181}
182
183func SetCoverageProperties(ctx android.BaseModuleContext, properties CoverageProperties, moduleTypeHasCoverage bool,
184 useSdk bool, sdkVersion string) CoverageProperties {
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800185 // Coverage is disabled globally
Colin Cross1a6acd42020-06-16 17:51:46 -0700186 if !ctx.DeviceConfig().NativeCoverageEnabled() {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400187 return properties
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800188 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800189
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700190 var needCoverageVariant bool
191 var needCoverageBuild bool
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800192
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400193 if moduleTypeHasCoverage {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700194 // Check if Native_coverage is set to false. This property defaults to true.
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400195 needCoverageVariant = BoolDefault(properties.Native_coverage, true)
196 if useSdk && sdkVersion != "current" {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700197 // Native coverage is not supported for SDK versions < 23
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400198 if fromApi, err := strconv.Atoi(sdkVersion); err == nil && fromApi < 23 {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700199 needCoverageVariant = false
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800200 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800201 }
202
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800203 if needCoverageVariant {
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700204 // Coverage variant is actually built with coverage if enabled for its module path
Roland Levillain4f5297b2020-06-09 12:44:06 +0100205 needCoverageBuild = ctx.DeviceConfig().NativeCoverageEnabledForPath(ctx.ModuleDir())
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700206 }
207 }
208
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400209 properties.NeedCoverageBuild = needCoverageBuild
210 properties.NeedCoverageVariant = needCoverageVariant
211
212 return properties
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700213}
214
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900215// Coverage is an interface for non-CC modules to implement to be mutated for coverage
216type Coverage interface {
217 android.Module
218 IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool
Ivan Lozanod7586b62021-04-01 09:49:36 -0400219 SetPreventInstall()
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900220 HideFromMake()
Jiyong Park83dc74b2020-01-14 18:38:44 +0900221 MarkAsCoverageVariant(bool)
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400222 EnableCoverageIfNeeded()
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900223}
224
Pirama Arumuga Nainaree30d5e2019-03-20 11:31:56 -0700225func coverageMutator(mctx android.BottomUpMutatorContext) {
226 if c, ok := mctx.Module().(*Module); ok && c.coverage != nil {
227 needCoverageVariant := c.coverage.Properties.NeedCoverageVariant
228 needCoverageBuild := c.coverage.Properties.NeedCoverageBuild
229 if needCoverageVariant {
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800230 m := mctx.CreateVariations("", "cov")
231
232 // Setup the non-coverage version and set HideFromMake and
233 // PreventInstall to true.
234 m[0].(*Module).coverage.Properties.CoverageEnabled = false
235 m[0].(*Module).coverage.Properties.IsCoverageVariant = false
236 m[0].(*Module).Properties.HideFromMake = true
237 m[0].(*Module).Properties.PreventInstall = true
238
239 // The coverage-enabled version inherits HideFromMake,
240 // PreventInstall from the original module.
241 m[1].(*Module).coverage.Properties.CoverageEnabled = needCoverageBuild
242 m[1].(*Module).coverage.Properties.IsCoverageVariant = true
Dan Willemsen581341d2017-02-09 16:16:31 -0800243 }
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900244 } else if cov, ok := mctx.Module().(Coverage); ok && cov.IsNativeCoverageNeeded(mctx) {
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400245 // APEX and Rust modules fall here
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900246
247 // Note: variant "" is also created because an APEX can be depended on by another
248 // module which are split into "" and "cov" variants. e.g. when cc_test refers
249 // to an APEX via 'data' property.
250 m := mctx.CreateVariations("", "cov")
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400251 m[0].(Coverage).MarkAsCoverageVariant(false)
Ivan Lozanod7586b62021-04-01 09:49:36 -0400252 m[0].(Coverage).SetPreventInstall()
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900253 m[0].(Coverage).HideFromMake()
Ivan Lozanoa0cd8f92020-04-09 09:56:02 -0400254
255 m[1].(Coverage).MarkAsCoverageVariant(true)
256 m[1].(Coverage).EnableCoverageIfNeeded()
Dan Willemsen581341d2017-02-09 16:16:31 -0800257 }
258}
sophiez4c4f8032021-08-16 22:54:00 -0700259
260func parseSymbolFileForAPICoverage(ctx ModuleContext, symbolFile string) android.ModuleOutPath {
261 apiLevelsJson := android.GetApiLevelsJson(ctx)
262 symbolFilePath := android.PathForModuleSrc(ctx, symbolFile)
263 outputFile := ctx.baseModuleName() + ".xml"
264 parsedApiCoveragePath := android.PathForModuleOut(ctx, outputFile)
265 rule := android.NewRuleBuilder(pctx, ctx)
266 rule.Command().
267 BuiltTool("ndk_api_coverage_parser").
268 Input(symbolFilePath).
269 Output(parsedApiCoveragePath).
270 Implicit(apiLevelsJson).
271 FlagWithArg("--api-map ", apiLevelsJson.String())
272 rule.Build("native_library_api_list", "Generate native API list based on symbol files for coverage measurement")
273 return parsedApiCoveragePath
274}