blob: 671353c9dd7c4ef9ff8a7cd28c572896f0199aba [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"
Pirama Arumuga Nainar358056c2018-04-14 01:03:35 -070019 "android/soong/cc/config"
Dan Willemsen581341d2017-02-09 16:16:31 -080020)
21
22type CoverageProperties struct {
23 Native_coverage *bool
24
25 CoverageEnabled bool `blueprint:"mutated"`
26}
27
28type coverage struct {
29 Properties CoverageProperties
30
31 // Whether binaries containing this module need --coverage added to their ldflags
32 linkCoverage bool
33}
34
35func (cov *coverage) props() []interface{} {
36 return []interface{}{&cov.Properties}
37}
38
39func (cov *coverage) begin(ctx BaseModuleContext) {}
40
41func (cov *coverage) deps(ctx BaseModuleContext, deps Deps) Deps {
Pirama Arumuga Nainar358056c2018-04-14 01:03:35 -070042 if cov.Properties.CoverageEnabled {
43 runtimeLibrary := config.ProfileRuntimeLibrary(ctx.toolchain())
44 deps.LateStaticLibs = append(deps.LateStaticLibs, runtimeLibrary)
45 }
Dan Willemsen581341d2017-02-09 16:16:31 -080046 return deps
47}
48
49func (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 Crossee6143c2017-12-30 17:54:27 -080067 ctx.VisitDirectDepsWithTag(wholeStaticDepTag, func(m android.Module) {
Dan Willemsen581341d2017-02-09 16:16:31 -080068 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 Crossd11fcda2017-10-23 17:59:01 -070077 ctx.VisitDirectDeps(func(m android.Module) {
Dan Willemsen581341d2017-02-09 16:16:31 -080078 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
101func 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 Nainar358056c2018-04-14 01:03:35 -0700107 } else if mctx.Darwin() || mctx.Windows() {
108 // Coverage not supported for Darwin and Windows
Dan Willemsen581341d2017-02-09 16:16:31 -0800109 } 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}