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 | |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 20 | "android/soong/android" |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | type CoverageProperties struct { |
| 24 | Native_coverage *bool |
| 25 | |
Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame^] | 26 | NeedCoverageVariant bool `blueprint:"mutated"` |
| 27 | NeedCoverageBuild bool `blueprint:"mutated"` |
| 28 | |
Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 29 | CoverageEnabled bool `blueprint:"mutated"` |
| 30 | IsCoverageVariant bool `blueprint:"mutated"` |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | type coverage struct { |
| 34 | Properties CoverageProperties |
| 35 | |
| 36 | // Whether binaries containing this module need --coverage added to their ldflags |
| 37 | linkCoverage bool |
| 38 | } |
| 39 | |
| 40 | func (cov *coverage) props() []interface{} { |
| 41 | return []interface{}{&cov.Properties} |
| 42 | } |
| 43 | |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 44 | func (cov *coverage) deps(ctx BaseModuleContext, deps Deps) Deps { |
| 45 | return deps |
| 46 | } |
| 47 | |
| 48 | func (cov *coverage) flags(ctx ModuleContext, flags Flags) Flags { |
| 49 | if !ctx.DeviceConfig().NativeCoverageEnabled() { |
| 50 | return flags |
| 51 | } |
| 52 | |
| 53 | if cov.Properties.CoverageEnabled { |
| 54 | flags.Coverage = true |
| 55 | flags.GlobalFlags = append(flags.GlobalFlags, "--coverage", "-O0") |
| 56 | cov.linkCoverage = true |
Pirama Arumuga Nainar | c7679de | 2019-02-18 22:23:42 -0800 | [diff] [blame] | 57 | |
| 58 | // Override -Wframe-larger-than and non-default optimization |
| 59 | // flags that the module may use. |
| 60 | flags.CFlags = append(flags.CFlags, "-Wno-frame-larger-than=", "-O0") |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | // Even if we don't have coverage enabled, if any of our object files were compiled |
| 64 | // with coverage, then we need to add --coverage to our ldflags. |
| 65 | if !cov.linkCoverage { |
| 66 | if ctx.static() && !ctx.staticBinary() { |
| 67 | // For static libraries, the only thing that changes our object files |
| 68 | // are included whole static libraries, so check to see if any of |
| 69 | // those have coverage enabled. |
Colin Cross | ee6143c | 2017-12-30 17:54:27 -0800 | [diff] [blame] | 70 | ctx.VisitDirectDepsWithTag(wholeStaticDepTag, func(m android.Module) { |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 71 | if cc, ok := m.(*Module); ok && cc.coverage != nil { |
| 72 | if cc.coverage.linkCoverage { |
| 73 | cov.linkCoverage = true |
| 74 | } |
| 75 | } |
| 76 | }) |
| 77 | } else { |
| 78 | // For executables and shared libraries, we need to check all of |
| 79 | // our static dependencies. |
Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 80 | ctx.VisitDirectDeps(func(m android.Module) { |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 81 | cc, ok := m.(*Module) |
| 82 | if !ok || cc.coverage == nil { |
| 83 | return |
| 84 | } |
| 85 | |
| 86 | if static, ok := cc.linker.(libraryInterface); !ok || !static.static() { |
| 87 | return |
| 88 | } |
| 89 | |
| 90 | if cc.coverage.linkCoverage { |
| 91 | cov.linkCoverage = true |
| 92 | } |
| 93 | }) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if cov.linkCoverage { |
| 98 | flags.LdFlags = append(flags.LdFlags, "--coverage") |
| 99 | } |
| 100 | |
| 101 | return flags |
| 102 | } |
| 103 | |
Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame^] | 104 | func (cov *coverage) begin(ctx BaseModuleContext) { |
Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 105 | // Coverage is disabled globally |
Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame^] | 106 | if !ctx.DeviceConfig().NativeCoverageEnabled() { |
Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 107 | return |
| 108 | } |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 109 | |
Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame^] | 110 | var needCoverageVariant bool |
| 111 | var needCoverageBuild bool |
Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 112 | |
Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame^] | 113 | if ctx.Host() { |
| 114 | // TODO(dwillemsen): because of -nodefaultlibs, we must depend on libclang_rt.profile-*.a |
| 115 | // Just turn off for now. |
| 116 | } else if ctx.isStubs() { |
| 117 | // Do not enable coverage for platform stub libraries |
| 118 | } else if ctx.isNDKStubLibrary() { |
| 119 | // Do not enable coverage for NDK stub libraries |
| 120 | } else { |
| 121 | // Check if Native_coverage is set to false. This property defaults to true. |
| 122 | needCoverageVariant = BoolDefault(cov.Properties.Native_coverage, true) |
Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 123 | |
Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame^] | 124 | if sdk_version := ctx.sdkVersion(); ctx.useSdk() && sdk_version != "current" { |
| 125 | // Native coverage is not supported for SDK versions < 23 |
| 126 | if fromApi, err := strconv.Atoi(sdk_version); err == nil && fromApi < 23 { |
| 127 | needCoverageVariant = false |
Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 128 | } |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 131 | if needCoverageVariant { |
Pirama Arumuga Nainar | ee30d5e | 2019-03-20 11:31:56 -0700 | [diff] [blame^] | 132 | // Coverage variant is actually built with coverage if enabled for its module path |
| 133 | needCoverageBuild = ctx.DeviceConfig().CoverageEnabledForPath(ctx.ModuleDir()) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | cov.Properties.NeedCoverageBuild = needCoverageBuild |
| 138 | cov.Properties.NeedCoverageVariant = needCoverageVariant |
| 139 | } |
| 140 | |
| 141 | func coverageMutator(mctx android.BottomUpMutatorContext) { |
| 142 | if c, ok := mctx.Module().(*Module); ok && c.coverage != nil { |
| 143 | needCoverageVariant := c.coverage.Properties.NeedCoverageVariant |
| 144 | needCoverageBuild := c.coverage.Properties.NeedCoverageBuild |
| 145 | if needCoverageVariant { |
Pirama Arumuga Nainar | 1acd447 | 2018-12-10 15:12:40 -0800 | [diff] [blame] | 146 | m := mctx.CreateVariations("", "cov") |
| 147 | |
| 148 | // Setup the non-coverage version and set HideFromMake and |
| 149 | // PreventInstall to true. |
| 150 | m[0].(*Module).coverage.Properties.CoverageEnabled = false |
| 151 | m[0].(*Module).coverage.Properties.IsCoverageVariant = false |
| 152 | m[0].(*Module).Properties.HideFromMake = true |
| 153 | m[0].(*Module).Properties.PreventInstall = true |
| 154 | |
| 155 | // The coverage-enabled version inherits HideFromMake, |
| 156 | // PreventInstall from the original module. |
| 157 | m[1].(*Module).coverage.Properties.CoverageEnabled = needCoverageBuild |
| 158 | m[1].(*Module).coverage.Properties.IsCoverageVariant = true |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |