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