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 ( |
| 18 | "android/soong/android" |
Pirama Arumuga Nainar | 358056c | 2018-04-14 01:03:35 -0700 | [diff] [blame^] | 19 | "android/soong/cc/config" |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | type CoverageProperties struct { |
| 23 | Native_coverage *bool |
| 24 | |
| 25 | CoverageEnabled bool `blueprint:"mutated"` |
| 26 | } |
| 27 | |
| 28 | type coverage struct { |
| 29 | Properties CoverageProperties |
| 30 | |
| 31 | // Whether binaries containing this module need --coverage added to their ldflags |
| 32 | linkCoverage bool |
| 33 | } |
| 34 | |
| 35 | func (cov *coverage) props() []interface{} { |
| 36 | return []interface{}{&cov.Properties} |
| 37 | } |
| 38 | |
| 39 | func (cov *coverage) begin(ctx BaseModuleContext) {} |
| 40 | |
| 41 | func (cov *coverage) deps(ctx BaseModuleContext, deps Deps) Deps { |
Pirama Arumuga Nainar | 358056c | 2018-04-14 01:03:35 -0700 | [diff] [blame^] | 42 | if cov.Properties.CoverageEnabled { |
| 43 | runtimeLibrary := config.ProfileRuntimeLibrary(ctx.toolchain()) |
| 44 | deps.LateStaticLibs = append(deps.LateStaticLibs, runtimeLibrary) |
| 45 | } |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 46 | return deps |
| 47 | } |
| 48 | |
| 49 | func (cov *coverage) flags(ctx ModuleContext, flags Flags) Flags { |
| 50 | if !ctx.DeviceConfig().NativeCoverageEnabled() { |
| 51 | return flags |
| 52 | } |
| 53 | |
| 54 | if cov.Properties.CoverageEnabled { |
| 55 | flags.Coverage = true |
| 56 | flags.GlobalFlags = append(flags.GlobalFlags, "--coverage", "-O0") |
| 57 | cov.linkCoverage = true |
| 58 | } |
| 59 | |
| 60 | // Even if we don't have coverage enabled, if any of our object files were compiled |
| 61 | // with coverage, then we need to add --coverage to our ldflags. |
| 62 | if !cov.linkCoverage { |
| 63 | if ctx.static() && !ctx.staticBinary() { |
| 64 | // For static libraries, the only thing that changes our object files |
| 65 | // are included whole static libraries, so check to see if any of |
| 66 | // those have coverage enabled. |
Colin Cross | ee6143c | 2017-12-30 17:54:27 -0800 | [diff] [blame] | 67 | ctx.VisitDirectDepsWithTag(wholeStaticDepTag, func(m android.Module) { |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 68 | if cc, ok := m.(*Module); ok && cc.coverage != nil { |
| 69 | if cc.coverage.linkCoverage { |
| 70 | cov.linkCoverage = true |
| 71 | } |
| 72 | } |
| 73 | }) |
| 74 | } else { |
| 75 | // For executables and shared libraries, we need to check all of |
| 76 | // our static dependencies. |
Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 77 | ctx.VisitDirectDeps(func(m android.Module) { |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 78 | cc, ok := m.(*Module) |
| 79 | if !ok || cc.coverage == nil { |
| 80 | return |
| 81 | } |
| 82 | |
| 83 | if static, ok := cc.linker.(libraryInterface); !ok || !static.static() { |
| 84 | return |
| 85 | } |
| 86 | |
| 87 | if cc.coverage.linkCoverage { |
| 88 | cov.linkCoverage = true |
| 89 | } |
| 90 | }) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if cov.linkCoverage { |
| 95 | flags.LdFlags = append(flags.LdFlags, "--coverage") |
| 96 | } |
| 97 | |
| 98 | return flags |
| 99 | } |
| 100 | |
| 101 | func coverageLinkingMutator(mctx android.BottomUpMutatorContext) { |
| 102 | if c, ok := mctx.Module().(*Module); ok && c.coverage != nil { |
| 103 | var enabled bool |
| 104 | |
| 105 | if !mctx.DeviceConfig().NativeCoverageEnabled() { |
| 106 | // Coverage is disabled globally |
Pirama Arumuga Nainar | 358056c | 2018-04-14 01:03:35 -0700 | [diff] [blame^] | 107 | } else if mctx.Darwin() || mctx.Windows() { |
| 108 | // Coverage not supported for Darwin and Windows |
Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 109 | } else if c.coverage.Properties.Native_coverage != nil { |
| 110 | enabled = *c.coverage.Properties.Native_coverage |
| 111 | } else { |
| 112 | enabled = mctx.DeviceConfig().CoverageEnabledForPath(mctx.ModuleDir()) |
| 113 | } |
| 114 | |
| 115 | if enabled { |
| 116 | // Create a variation so that we don't need to recompile objects |
| 117 | // when turning on or off coverage. We'll still relink the necessary |
| 118 | // binaries, since we don't know which ones those are until later. |
| 119 | m := mctx.CreateLocalVariations("cov") |
| 120 | m[0].(*Module).coverage.Properties.CoverageEnabled = true |
| 121 | } |
| 122 | } |
| 123 | } |