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