| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 1 | // 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 |  | 
|  | 15 | package cc | 
|  | 16 |  | 
|  | 17 | import ( | 
| Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 18 | "strconv" | 
|  | 19 |  | 
| Pirama Arumuga Nainar | 82fe59b | 2019-07-02 14:55:35 -0700 | [diff] [blame] | 20 | "github.com/google/blueprint" | 
|  | 21 |  | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 22 | "android/soong/android" | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 23 | ) | 
|  | 24 |  | 
| Gabriel Chen | 1589c96 | 2023-11-30 20:29:44 -0800 | [diff] [blame] | 25 | var ( | 
| Colin Cross | e1a8555 | 2024-06-14 12:17:37 -0700 | [diff] [blame] | 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 | } | 
| Gabriel Chen | 1589c96 | 2023-11-30 20:29:44 -0800 | [diff] [blame] | 46 | ) | 
|  | 47 |  | 
| Pirama Arumuga Nainar | f45e152 | 2020-08-05 10:08:30 -0700 | [diff] [blame] | 48 | const profileInstrFlag = "-fprofile-instr-generate=/data/misc/trace/clang-%p-%m.profraw" | 
|  | 49 |  | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 50 | type CoverageProperties struct { | 
|  | 51 | Native_coverage *bool | 
|  | 52 |  | 
| Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame] | 53 | NeedCoverageVariant bool `blueprint:"mutated"` | 
|  | 54 | NeedCoverageBuild   bool `blueprint:"mutated"` | 
|  | 55 |  | 
| Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 56 | CoverageEnabled   bool `blueprint:"mutated"` | 
|  | 57 | IsCoverageVariant bool `blueprint:"mutated"` | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 58 | } | 
|  | 59 |  | 
|  | 60 | type coverage struct { | 
|  | 61 | Properties CoverageProperties | 
|  | 62 |  | 
|  | 63 | // Whether binaries containing this module need --coverage added to their ldflags | 
|  | 64 | linkCoverage bool | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | func (cov *coverage) props() []interface{} { | 
|  | 68 | return []interface{}{&cov.Properties} | 
|  | 69 | } | 
|  | 70 |  | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 71 | func getGcovProfileLibraryName(ctx ModuleContextIntf) string { | 
| Pirama Arumuga Nainar | 82fe59b | 2019-07-02 14:55:35 -0700 | [diff] [blame] | 72 | // This function should only ever be called for a cc.Module, so the | 
|  | 73 | // following statement should always succeed. | 
| Liz Kammer | 3847f21 | 2023-07-14 15:24:35 -0400 | [diff] [blame] | 74 | // LINT.IfChange | 
| Pirama Arumuga Nainar | 82fe59b | 2019-07-02 14:55:35 -0700 | [diff] [blame] | 75 | if ctx.useSdk() { | 
|  | 76 | return "libprofile-extras_ndk" | 
|  | 77 | } else { | 
|  | 78 | return "libprofile-extras" | 
|  | 79 | } | 
|  | 80 | } | 
|  | 81 |  | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 82 | func getClangProfileLibraryName(ctx ModuleContextIntf) string { | 
|  | 83 | if ctx.useSdk() { | 
|  | 84 | return "libprofile-clang-extras_ndk" | 
| Cindy Zhou | 5d5cfc1 | 2021-01-09 08:25:22 -0800 | [diff] [blame] | 85 | } else if ctx.isCfiAssemblySupportEnabled() { | 
|  | 86 | return "libprofile-clang-extras_cfi_support" | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 87 | } else { | 
|  | 88 | return "libprofile-clang-extras" | 
|  | 89 | } | 
| Liz Kammer | 3847f21 | 2023-07-14 15:24:35 -0400 | [diff] [blame] | 90 | // LINT.ThenChange(library.go) | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
| Pirama Arumuga Nainar | 82fe59b | 2019-07-02 14:55:35 -0700 | [diff] [blame] | 93 | func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps { | 
| Yu Liu | aa89f2c | 2023-12-12 23:07:33 +0000 | [diff] [blame] | 94 | if cov.Properties.NeedCoverageVariant && ctx.Device() { | 
| Pirama Arumuga Nainar | 82fe59b | 2019-07-02 14:55:35 -0700 | [diff] [blame] | 95 | ctx.AddVariationDependencies([]blueprint.Variation{ | 
|  | 96 | {Mutator: "link", Variation: "static"}, | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 97 | }, CoverageDepTag, getGcovProfileLibraryName(ctx)) | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 98 | ctx.AddVariationDependencies([]blueprint.Variation{ | 
|  | 99 | {Mutator: "link", Variation: "static"}, | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 100 | }, CoverageDepTag, getClangProfileLibraryName(ctx)) | 
| Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 101 | } | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 102 | return deps | 
|  | 103 | } | 
|  | 104 |  | 
| Pirama Arumuga Nainar | b37ae58 | 2022-01-26 22:14:32 -0800 | [diff] [blame] | 105 | func EnableContinuousCoverage(ctx android.BaseModuleContext) bool { | 
|  | 106 | return ctx.DeviceConfig().ClangCoverageContinuousMode() | 
|  | 107 | } | 
|  | 108 |  | 
| Pirama Arumuga Nainar | 82fe59b | 2019-07-02 14:55:35 -0700 | [diff] [blame] | 109 | func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) { | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 110 | clangCoverage := ctx.DeviceConfig().ClangCoverageEnabled() | 
| Colin Cross | 1a6acd4 | 2020-06-16 17:51:46 -0700 | [diff] [blame] | 111 | gcovCoverage := ctx.DeviceConfig().GcovCoverageEnabled() | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 112 |  | 
|  | 113 | if !gcovCoverage && !clangCoverage { | 
| Pirama Arumuga Nainar | 82fe59b | 2019-07-02 14:55:35 -0700 | [diff] [blame] | 114 | return flags, deps | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 115 | } | 
|  | 116 |  | 
|  | 117 | if cov.Properties.CoverageEnabled { | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 118 | cov.linkCoverage = true | 
| Pirama Arumuga Nainar | c7679de | 2019-02-18 22:23:42 -0800 | [diff] [blame] | 119 |  | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 120 | if gcovCoverage { | 
| Oliver Nguyen | 0452678 | 2020-04-21 12:40:27 -0700 | [diff] [blame] | 121 | flags.GcovCoverage = true | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 122 | 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 Chen | 1589c96 | 2023-11-30 20:29:44 -0800 | [diff] [blame] | 128 | flags.Local.CommonFlags = append(flags.Local.CommonFlags, profileInstrFlag) | 
|  | 129 | flags.Local.CommonFlags = append(flags.Local.CommonFlags, clangCoverageCommonFlags...) | 
| Pirama Arumuga Nainar | f776c8c | 2022-01-27 10:46:26 -0800 | [diff] [blame] | 130 | // Override -Wframe-larger-than.  We can expect frame size increase after | 
|  | 131 | // coverage instrumentation. | 
| Gabriel Chen | 1589c96 | 2023-11-30 20:29:44 -0800 | [diff] [blame] | 132 | flags.Local.CFlags = append(flags.Local.CFlags, clangCoverageCFlags...) | 
| Pirama Arumuga Nainar | b37ae58 | 2022-01-26 22:14:32 -0800 | [diff] [blame] | 133 | if EnableContinuousCoverage(ctx) { | 
| Gabriel Chen | 1589c96 | 2023-11-30 20:29:44 -0800 | [diff] [blame] | 134 | flags.Local.CommonFlags = append(flags.Local.CommonFlags, clangContinuousCoverageFlags...) | 
| Pirama Arumuga Nainar | b37ae58 | 2022-01-26 22:14:32 -0800 | [diff] [blame] | 135 | } | 
| Pirama Arumuga Nainar | 2bcdf5e | 2022-10-04 23:52:48 +0000 | [diff] [blame] | 136 |  | 
|  | 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 Chen | 1589c96 | 2023-11-30 20:29:44 -0800 | [diff] [blame] | 140 | flags.Local.CommonFlags = append(flags.Local.CommonFlags, clangCoverageHWASanFlags...) | 
| Pirama Arumuga Nainar | 2bcdf5e | 2022-10-04 23:52:48 +0000 | [diff] [blame] | 141 | } | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 142 | } | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 143 | } | 
|  | 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 Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 152 | 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 Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 160 | } | 
|  | 161 | } | 
|  | 162 | }) | 
|  | 163 | } else { | 
|  | 164 | // For executables and shared libraries, we need to check all of | 
|  | 165 | // our static dependencies. | 
| Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 166 | ctx.VisitDirectDeps(func(m android.Module) { | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 167 | 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 Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 184 | if gcovCoverage { | 
|  | 185 | flags.Local.LdFlags = append(flags.Local.LdFlags, "--coverage") | 
| Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 186 |  | 
| Yu Liu | aa89f2c | 2023-12-12 23:07:33 +0000 | [diff] [blame] | 187 | 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 Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 192 | } else if clangCoverage { | 
| Pirama Arumuga Nainar | f45e152 | 2020-08-05 10:08:30 -0700 | [diff] [blame] | 193 | flags.Local.LdFlags = append(flags.Local.LdFlags, profileInstrFlag) | 
| Pirama Arumuga Nainar | b37ae58 | 2022-01-26 22:14:32 -0800 | [diff] [blame] | 194 | if EnableContinuousCoverage(ctx) { | 
|  | 195 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-mllvm=-runtime-counter-relocation") | 
|  | 196 | } | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 197 |  | 
| Yu Liu | aa89f2c | 2023-12-12 23:07:33 +0000 | [diff] [blame] | 198 | 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 Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 203 | } | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 204 | } | 
|  | 205 |  | 
| Pirama Arumuga Nainar | 82fe59b | 2019-07-02 14:55:35 -0700 | [diff] [blame] | 206 | return flags, deps | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 207 | } | 
|  | 208 |  | 
| Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame] | 209 | func (cov *coverage) begin(ctx BaseModuleContext) { | 
| Yu Liu | aa89f2c | 2023-12-12 23:07:33 +0000 | [diff] [blame] | 210 | if ctx.Host() && !ctx.Os().Linux() { | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 211 | // 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 |  | 
|  | 218 | func SetCoverageProperties(ctx android.BaseModuleContext, properties CoverageProperties, moduleTypeHasCoverage bool, | 
|  | 219 | useSdk bool, sdkVersion string) CoverageProperties { | 
| Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 220 | // Coverage is disabled globally | 
| Colin Cross | 1a6acd4 | 2020-06-16 17:51:46 -0700 | [diff] [blame] | 221 | if !ctx.DeviceConfig().NativeCoverageEnabled() { | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 222 | return properties | 
| Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 223 | } | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 224 |  | 
| Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame] | 225 | var needCoverageVariant bool | 
|  | 226 | var needCoverageBuild bool | 
| Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 227 |  | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 228 | if moduleTypeHasCoverage { | 
| Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame] | 229 | // Check if Native_coverage is set to false.  This property defaults to true. | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 230 | needCoverageVariant = BoolDefault(properties.Native_coverage, true) | 
|  | 231 | if useSdk && sdkVersion != "current" { | 
| Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame] | 232 | // Native coverage is not supported for SDK versions < 23 | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 233 | if fromApi, err := strconv.Atoi(sdkVersion); err == nil && fromApi < 23 { | 
| Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame] | 234 | needCoverageVariant = false | 
| Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 235 | } | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 236 | } | 
|  | 237 |  | 
| Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 238 | if needCoverageVariant { | 
| Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame] | 239 | // Coverage variant is actually built with coverage if enabled for its module path | 
| Roland Levillain | 4f5297b | 2020-06-09 12:44:06 +0100 | [diff] [blame] | 240 | needCoverageBuild = ctx.DeviceConfig().NativeCoverageEnabledForPath(ctx.ModuleDir()) | 
| Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame] | 241 | } | 
|  | 242 | } | 
|  | 243 |  | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 244 | properties.NeedCoverageBuild = needCoverageBuild | 
|  | 245 | properties.NeedCoverageVariant = needCoverageVariant | 
|  | 246 |  | 
|  | 247 | return properties | 
| Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame] | 248 | } | 
|  | 249 |  | 
| Colin Cross | e1a8555 | 2024-06-14 12:17:37 -0700 | [diff] [blame] | 250 | type IsNativeCoverageNeededContext interface { | 
|  | 251 | Config() android.Config | 
|  | 252 | DeviceConfig() android.DeviceConfig | 
|  | 253 | Device() bool | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | var _ IsNativeCoverageNeededContext = android.IncomingTransitionContext(nil) | 
|  | 257 | var _ IsNativeCoverageNeededContext = android.BaseModuleContext(nil) | 
|  | 258 | var _ IsNativeCoverageNeededContext = android.BottomUpMutatorContext(nil) | 
|  | 259 |  | 
| Jooyung Han | e606759 | 2023-03-16 13:11:17 +0900 | [diff] [blame] | 260 | type UseCoverage interface { | 
| Jiyong Park | ee9a98d | 2019-08-09 14:44:36 +0900 | [diff] [blame] | 261 | android.Module | 
| Colin Cross | e1a8555 | 2024-06-14 12:17:37 -0700 | [diff] [blame] | 262 | IsNativeCoverageNeeded(ctx IsNativeCoverageNeededContext) bool | 
| Jooyung Han | e606759 | 2023-03-16 13:11:17 +0900 | [diff] [blame] | 263 | } | 
|  | 264 |  | 
|  | 265 | // Coverage is an interface for non-CC modules to implement to be mutated for coverage | 
|  | 266 | type Coverage interface { | 
|  | 267 | UseCoverage | 
| Ivan Lozano | d7586b6 | 2021-04-01 09:49:36 -0400 | [diff] [blame] | 268 | SetPreventInstall() | 
| Jiyong Park | ee9a98d | 2019-08-09 14:44:36 +0900 | [diff] [blame] | 269 | HideFromMake() | 
| Jiyong Park | 83dc74b | 2020-01-14 18:38:44 +0900 | [diff] [blame] | 270 | MarkAsCoverageVariant(bool) | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 271 | EnableCoverageIfNeeded() | 
| Jiyong Park | ee9a98d | 2019-08-09 14:44:36 +0900 | [diff] [blame] | 272 | } | 
|  | 273 |  | 
| Colin Cross | f5f4ad3 | 2024-01-19 15:41:48 -0800 | [diff] [blame] | 274 | type coverageTransitionMutator struct{} | 
| Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 275 |  | 
| Colin Cross | f5f4ad3 | 2024-01-19 15:41:48 -0800 | [diff] [blame] | 276 | var _ android.TransitionMutator = (*coverageTransitionMutator)(nil) | 
| Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 277 |  | 
| Colin Cross | f5f4ad3 | 2024-01-19 15:41:48 -0800 | [diff] [blame] | 278 | func (c coverageTransitionMutator) Split(ctx android.BaseModuleContext) []string { | 
|  | 279 | if c, ok := ctx.Module().(*Module); ok && c.coverage != nil { | 
|  | 280 | if c.coverage.Properties.NeedCoverageVariant { | 
|  | 281 | return []string{"", "cov"} | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 282 | } | 
| Colin Cross | f5f4ad3 | 2024-01-19 15:41:48 -0800 | [diff] [blame] | 283 | } else if cov, ok := ctx.Module().(Coverage); ok && cov.IsNativeCoverageNeeded(ctx) { | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 284 | // APEX and Rust modules fall here | 
| Jiyong Park | ee9a98d | 2019-08-09 14:44:36 +0900 | [diff] [blame] | 285 |  | 
|  | 286 | // Note: variant "" is also created because an APEX can be depended on by another | 
|  | 287 | // module which are split into "" and "cov" variants. e.g. when cc_test refers | 
|  | 288 | // to an APEX via 'data' property. | 
| Colin Cross | f5f4ad3 | 2024-01-19 15:41:48 -0800 | [diff] [blame] | 289 | return []string{"", "cov"} | 
|  | 290 | } else if cov, ok := ctx.Module().(UseCoverage); ok && cov.IsNativeCoverageNeeded(ctx) { | 
| Jooyung Han | e606759 | 2023-03-16 13:11:17 +0900 | [diff] [blame] | 291 | // Module itself doesn't have to have "cov" variant, but it should use "cov" variants of | 
|  | 292 | // deps. | 
| Colin Cross | f5f4ad3 | 2024-01-19 15:41:48 -0800 | [diff] [blame] | 293 | return []string{"cov"} | 
|  | 294 | } | 
|  | 295 |  | 
|  | 296 | return []string{""} | 
|  | 297 | } | 
|  | 298 |  | 
|  | 299 | func (c coverageTransitionMutator) OutgoingTransition(ctx android.OutgoingTransitionContext, sourceVariation string) string { | 
|  | 300 | return sourceVariation | 
|  | 301 | } | 
|  | 302 |  | 
|  | 303 | func (c coverageTransitionMutator) IncomingTransition(ctx android.IncomingTransitionContext, incomingVariation string) string { | 
|  | 304 | if c, ok := ctx.Module().(*Module); ok && c.coverage != nil { | 
|  | 305 | if !c.coverage.Properties.NeedCoverageVariant { | 
|  | 306 | return "" | 
|  | 307 | } | 
|  | 308 | } else if cov, ok := ctx.Module().(Coverage); ok { | 
|  | 309 | if !cov.IsNativeCoverageNeeded(ctx) { | 
|  | 310 | return "" | 
|  | 311 | } | 
|  | 312 | } else if cov, ok := ctx.Module().(UseCoverage); ok && cov.IsNativeCoverageNeeded(ctx) { | 
|  | 313 | // Module only has a "cov" variation, so all incoming variations should use "cov". | 
|  | 314 | return "cov" | 
|  | 315 | } else { | 
|  | 316 | return "" | 
|  | 317 | } | 
|  | 318 |  | 
|  | 319 | return incomingVariation | 
|  | 320 | } | 
|  | 321 |  | 
|  | 322 | func (c coverageTransitionMutator) Mutate(ctx android.BottomUpMutatorContext, variation string) { | 
|  | 323 | if c, ok := ctx.Module().(*Module); ok && c.coverage != nil { | 
|  | 324 | if variation == "" && c.coverage.Properties.NeedCoverageVariant { | 
|  | 325 | // Setup the non-coverage version and set HideFromMake and | 
|  | 326 | // PreventInstall to true. | 
|  | 327 | c.coverage.Properties.CoverageEnabled = false | 
|  | 328 | c.coverage.Properties.IsCoverageVariant = false | 
|  | 329 | c.Properties.HideFromMake = true | 
|  | 330 | c.Properties.PreventInstall = true | 
|  | 331 | } else if variation == "cov" { | 
|  | 332 | // The coverage-enabled version inherits HideFromMake, | 
|  | 333 | // PreventInstall from the original module. | 
|  | 334 | c.coverage.Properties.CoverageEnabled = c.coverage.Properties.NeedCoverageBuild | 
|  | 335 | c.coverage.Properties.IsCoverageVariant = true | 
|  | 336 | } | 
|  | 337 | } else if cov, ok := ctx.Module().(Coverage); ok && cov.IsNativeCoverageNeeded(ctx) { | 
|  | 338 | // APEX and Rust modules fall here | 
|  | 339 |  | 
|  | 340 | // Note: variant "" is also created because an APEX can be depended on by another | 
|  | 341 | // module which are split into "" and "cov" variants. e.g. when cc_test refers | 
|  | 342 | // to an APEX via 'data' property. | 
|  | 343 | if variation == "" { | 
|  | 344 | cov.MarkAsCoverageVariant(false) | 
|  | 345 | cov.SetPreventInstall() | 
|  | 346 | cov.HideFromMake() | 
|  | 347 | } else if variation == "cov" { | 
|  | 348 | cov.MarkAsCoverageVariant(true) | 
|  | 349 | cov.EnableCoverageIfNeeded() | 
|  | 350 | } | 
|  | 351 | } else if cov, ok := ctx.Module().(UseCoverage); ok && cov.IsNativeCoverageNeeded(ctx) { | 
|  | 352 | // Module itself doesn't have to have "cov" variant, but it should use "cov" variants of | 
|  | 353 | // deps. | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 354 | } | 
|  | 355 | } | 
| sophiez | 4c4f803 | 2021-08-16 22:54:00 -0700 | [diff] [blame] | 356 |  | 
|  | 357 | func parseSymbolFileForAPICoverage(ctx ModuleContext, symbolFile string) android.ModuleOutPath { | 
|  | 358 | apiLevelsJson := android.GetApiLevelsJson(ctx) | 
|  | 359 | symbolFilePath := android.PathForModuleSrc(ctx, symbolFile) | 
|  | 360 | outputFile := ctx.baseModuleName() + ".xml" | 
|  | 361 | parsedApiCoveragePath := android.PathForModuleOut(ctx, outputFile) | 
|  | 362 | rule := android.NewRuleBuilder(pctx, ctx) | 
|  | 363 | rule.Command(). | 
|  | 364 | BuiltTool("ndk_api_coverage_parser"). | 
|  | 365 | Input(symbolFilePath). | 
|  | 366 | Output(parsedApiCoveragePath). | 
|  | 367 | Implicit(apiLevelsJson). | 
|  | 368 | FlagWithArg("--api-map ", apiLevelsJson.String()) | 
|  | 369 | rule.Build("native_library_api_list", "Generate native API list based on symbol files for coverage measurement") | 
|  | 370 | return parsedApiCoveragePath | 
|  | 371 | } |