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