| 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 |  | 
| Pirama Arumuga Nainar | f45e152 | 2020-08-05 10:08:30 -0700 | [diff] [blame] | 25 | const profileInstrFlag = "-fprofile-instr-generate=/data/misc/trace/clang-%p-%m.profraw" | 
|  | 26 |  | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 27 | type CoverageProperties struct { | 
|  | 28 | Native_coverage *bool | 
|  | 29 |  | 
| Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame] | 30 | NeedCoverageVariant bool `blueprint:"mutated"` | 
|  | 31 | NeedCoverageBuild   bool `blueprint:"mutated"` | 
|  | 32 |  | 
| Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 33 | CoverageEnabled   bool `blueprint:"mutated"` | 
|  | 34 | IsCoverageVariant bool `blueprint:"mutated"` | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 35 | } | 
|  | 36 |  | 
|  | 37 | type coverage struct { | 
|  | 38 | Properties CoverageProperties | 
|  | 39 |  | 
|  | 40 | // Whether binaries containing this module need --coverage added to their ldflags | 
|  | 41 | linkCoverage bool | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | func (cov *coverage) props() []interface{} { | 
|  | 45 | return []interface{}{&cov.Properties} | 
|  | 46 | } | 
|  | 47 |  | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 48 | func getGcovProfileLibraryName(ctx ModuleContextIntf) string { | 
| Pirama Arumuga Nainar | 82fe59b | 2019-07-02 14:55:35 -0700 | [diff] [blame] | 49 | // 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 Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 58 | func getClangProfileLibraryName(ctx ModuleContextIntf) string { | 
|  | 59 | if ctx.useSdk() { | 
|  | 60 | return "libprofile-clang-extras_ndk" | 
|  | 61 | } else { | 
|  | 62 | return "libprofile-clang-extras" | 
|  | 63 | } | 
|  | 64 | } | 
|  | 65 |  | 
| Pirama Arumuga Nainar | 82fe59b | 2019-07-02 14:55:35 -0700 | [diff] [blame] | 66 | func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps { | 
|  | 67 | if cov.Properties.NeedCoverageVariant { | 
|  | 68 | ctx.AddVariationDependencies([]blueprint.Variation{ | 
|  | 69 | {Mutator: "link", Variation: "static"}, | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 70 | }, CoverageDepTag, getGcovProfileLibraryName(ctx)) | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 71 | ctx.AddVariationDependencies([]blueprint.Variation{ | 
|  | 72 | {Mutator: "link", Variation: "static"}, | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 73 | }, CoverageDepTag, getClangProfileLibraryName(ctx)) | 
| Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 74 | } | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 75 | return deps | 
|  | 76 | } | 
|  | 77 |  | 
| Pirama Arumuga Nainar | 82fe59b | 2019-07-02 14:55:35 -0700 | [diff] [blame] | 78 | func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) { | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 79 | clangCoverage := ctx.DeviceConfig().ClangCoverageEnabled() | 
| Colin Cross | 1a6acd4 | 2020-06-16 17:51:46 -0700 | [diff] [blame] | 80 | gcovCoverage := ctx.DeviceConfig().GcovCoverageEnabled() | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 81 |  | 
|  | 82 | if !gcovCoverage && !clangCoverage { | 
| Pirama Arumuga Nainar | 82fe59b | 2019-07-02 14:55:35 -0700 | [diff] [blame] | 83 | return flags, deps | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
|  | 86 | if cov.Properties.CoverageEnabled { | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 87 | cov.linkCoverage = true | 
| Pirama Arumuga Nainar | c7679de | 2019-02-18 22:23:42 -0800 | [diff] [blame] | 88 |  | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 89 | if gcovCoverage { | 
| Oliver Nguyen | 0452678 | 2020-04-21 12:40:27 -0700 | [diff] [blame] | 90 | flags.GcovCoverage = true | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 91 | flags.Local.CommonFlags = append(flags.Local.CommonFlags, "--coverage", "-O0") | 
|  | 92 |  | 
|  | 93 | // Override -Wframe-larger-than and non-default optimization | 
|  | 94 | // flags that the module may use. | 
|  | 95 | flags.Local.CFlags = append(flags.Local.CFlags, "-Wno-frame-larger-than=", "-O0") | 
|  | 96 | } else if clangCoverage { | 
| Pirama Arumuga Nainar | f45e152 | 2020-08-05 10:08:30 -0700 | [diff] [blame] | 97 | flags.Local.CommonFlags = append(flags.Local.CommonFlags, profileInstrFlag, "-fcoverage-mapping", "-Wno-pass-failed") | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 98 | } | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 99 | } | 
|  | 100 |  | 
|  | 101 | // Even if we don't have coverage enabled, if any of our object files were compiled | 
|  | 102 | // with coverage, then we need to add --coverage to our ldflags. | 
|  | 103 | if !cov.linkCoverage { | 
|  | 104 | if ctx.static() && !ctx.staticBinary() { | 
|  | 105 | // For static libraries, the only thing that changes our object files | 
|  | 106 | // are included whole static libraries, so check to see if any of | 
|  | 107 | // those have coverage enabled. | 
| Colin Cross | 6e511a9 | 2020-07-27 21:26:48 -0700 | [diff] [blame] | 108 | ctx.VisitDirectDeps(func(m android.Module) { | 
|  | 109 | if depTag, ok := ctx.OtherModuleDependencyTag(m).(libraryDependencyTag); ok { | 
|  | 110 | if depTag.static() && depTag.wholeStatic { | 
|  | 111 | if cc, ok := m.(*Module); ok && cc.coverage != nil { | 
|  | 112 | if cc.coverage.linkCoverage { | 
|  | 113 | cov.linkCoverage = true | 
|  | 114 | } | 
|  | 115 | } | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 116 | } | 
|  | 117 | } | 
|  | 118 | }) | 
|  | 119 | } else { | 
|  | 120 | // For executables and shared libraries, we need to check all of | 
|  | 121 | // our static dependencies. | 
| Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 122 | ctx.VisitDirectDeps(func(m android.Module) { | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 123 | cc, ok := m.(*Module) | 
|  | 124 | if !ok || cc.coverage == nil { | 
|  | 125 | return | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | if static, ok := cc.linker.(libraryInterface); !ok || !static.static() { | 
|  | 129 | return | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | if cc.coverage.linkCoverage { | 
|  | 133 | cov.linkCoverage = true | 
|  | 134 | } | 
|  | 135 | }) | 
|  | 136 | } | 
|  | 137 | } | 
|  | 138 |  | 
|  | 139 | if cov.linkCoverage { | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 140 | if gcovCoverage { | 
|  | 141 | flags.Local.LdFlags = append(flags.Local.LdFlags, "--coverage") | 
| Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 142 |  | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 143 | coverage := ctx.GetDirectDepWithTag(getGcovProfileLibraryName(ctx), CoverageDepTag).(*Module) | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 144 | deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path()) | 
| Pirama Arumuga Nainar | 100bbdc | 2019-07-02 23:47:35 -0700 | [diff] [blame] | 145 |  | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 146 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--wrap,getenv") | 
|  | 147 | } else if clangCoverage { | 
| Pirama Arumuga Nainar | f45e152 | 2020-08-05 10:08:30 -0700 | [diff] [blame] | 148 | flags.Local.LdFlags = append(flags.Local.LdFlags, profileInstrFlag) | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 149 |  | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 150 | coverage := ctx.GetDirectDepWithTag(getClangProfileLibraryName(ctx), CoverageDepTag).(*Module) | 
| Oliver Nguyen | 1382ab6 | 2019-12-06 15:22:41 -0800 | [diff] [blame] | 151 | deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path()) | 
|  | 152 | } | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 153 | } | 
|  | 154 |  | 
| Pirama Arumuga Nainar | 82fe59b | 2019-07-02 14:55:35 -0700 | [diff] [blame] | 155 | return flags, deps | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 156 | } | 
|  | 157 |  | 
| Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame] | 158 | func (cov *coverage) begin(ctx BaseModuleContext) { | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 159 | if ctx.Host() { | 
|  | 160 | // TODO(dwillemsen): because of -nodefaultlibs, we must depend on libclang_rt.profile-*.a | 
|  | 161 | // Just turn off for now. | 
|  | 162 | } else { | 
|  | 163 | cov.Properties = SetCoverageProperties(ctx, cov.Properties, ctx.nativeCoverage(), ctx.useSdk(), ctx.sdkVersion()) | 
|  | 164 | } | 
|  | 165 | } | 
|  | 166 |  | 
|  | 167 | func SetCoverageProperties(ctx android.BaseModuleContext, properties CoverageProperties, moduleTypeHasCoverage bool, | 
|  | 168 | useSdk bool, sdkVersion string) CoverageProperties { | 
| Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 169 | // Coverage is disabled globally | 
| Colin Cross | 1a6acd4 | 2020-06-16 17:51:46 -0700 | [diff] [blame] | 170 | if !ctx.DeviceConfig().NativeCoverageEnabled() { | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 171 | return properties | 
| Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 172 | } | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 173 |  | 
| Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame] | 174 | var needCoverageVariant bool | 
|  | 175 | var needCoverageBuild bool | 
| Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 176 |  | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 177 | if moduleTypeHasCoverage { | 
| Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame] | 178 | // 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] | 179 | needCoverageVariant = BoolDefault(properties.Native_coverage, true) | 
|  | 180 | if useSdk && sdkVersion != "current" { | 
| Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame] | 181 | // Native coverage is not supported for SDK versions < 23 | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 182 | if fromApi, err := strconv.Atoi(sdkVersion); err == nil && fromApi < 23 { | 
| Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame] | 183 | needCoverageVariant = false | 
| Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 184 | } | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 185 | } | 
|  | 186 |  | 
| Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 187 | if needCoverageVariant { | 
| Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame] | 188 | // 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] | 189 | needCoverageBuild = ctx.DeviceConfig().NativeCoverageEnabledForPath(ctx.ModuleDir()) | 
| Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame] | 190 | } | 
|  | 191 | } | 
|  | 192 |  | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 193 | properties.NeedCoverageBuild = needCoverageBuild | 
|  | 194 | properties.NeedCoverageVariant = needCoverageVariant | 
|  | 195 |  | 
|  | 196 | return properties | 
| Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame] | 197 | } | 
|  | 198 |  | 
| Jiyong Park | ee9a98d | 2019-08-09 14:44:36 +0900 | [diff] [blame] | 199 | // Coverage is an interface for non-CC modules to implement to be mutated for coverage | 
|  | 200 | type Coverage interface { | 
|  | 201 | android.Module | 
|  | 202 | IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool | 
|  | 203 | PreventInstall() | 
|  | 204 | HideFromMake() | 
| Jiyong Park | 83dc74b | 2020-01-14 18:38:44 +0900 | [diff] [blame] | 205 | MarkAsCoverageVariant(bool) | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 206 | EnableCoverageIfNeeded() | 
| Jiyong Park | ee9a98d | 2019-08-09 14:44:36 +0900 | [diff] [blame] | 207 | } | 
|  | 208 |  | 
| Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame] | 209 | func coverageMutator(mctx android.BottomUpMutatorContext) { | 
|  | 210 | if c, ok := mctx.Module().(*Module); ok && c.coverage != nil { | 
|  | 211 | needCoverageVariant := c.coverage.Properties.NeedCoverageVariant | 
|  | 212 | needCoverageBuild := c.coverage.Properties.NeedCoverageBuild | 
|  | 213 | if needCoverageVariant { | 
| Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 214 | m := mctx.CreateVariations("", "cov") | 
|  | 215 |  | 
|  | 216 | // Setup the non-coverage version and set HideFromMake and | 
|  | 217 | // PreventInstall to true. | 
|  | 218 | m[0].(*Module).coverage.Properties.CoverageEnabled = false | 
|  | 219 | m[0].(*Module).coverage.Properties.IsCoverageVariant = false | 
|  | 220 | m[0].(*Module).Properties.HideFromMake = true | 
|  | 221 | m[0].(*Module).Properties.PreventInstall = true | 
|  | 222 |  | 
|  | 223 | // The coverage-enabled version inherits HideFromMake, | 
|  | 224 | // PreventInstall from the original module. | 
|  | 225 | m[1].(*Module).coverage.Properties.CoverageEnabled = needCoverageBuild | 
|  | 226 | m[1].(*Module).coverage.Properties.IsCoverageVariant = true | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 227 | } | 
| Jiyong Park | ee9a98d | 2019-08-09 14:44:36 +0900 | [diff] [blame] | 228 | } else if cov, ok := mctx.Module().(Coverage); ok && cov.IsNativeCoverageNeeded(mctx) { | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 229 | // APEX and Rust modules fall here | 
| Jiyong Park | ee9a98d | 2019-08-09 14:44:36 +0900 | [diff] [blame] | 230 |  | 
|  | 231 | // Note: variant "" is also created because an APEX can be depended on by another | 
|  | 232 | // module which are split into "" and "cov" variants. e.g. when cc_test refers | 
|  | 233 | // to an APEX via 'data' property. | 
|  | 234 | m := mctx.CreateVariations("", "cov") | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 235 | m[0].(Coverage).MarkAsCoverageVariant(false) | 
| Jiyong Park | ee9a98d | 2019-08-09 14:44:36 +0900 | [diff] [blame] | 236 | m[0].(Coverage).PreventInstall() | 
|  | 237 | m[0].(Coverage).HideFromMake() | 
| Ivan Lozano | a0cd8f9 | 2020-04-09 09:56:02 -0400 | [diff] [blame] | 238 |  | 
|  | 239 | m[1].(Coverage).MarkAsCoverageVariant(true) | 
|  | 240 | m[1].(Coverage).EnableCoverageIfNeeded() | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 241 | } | 
|  | 242 | } |