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