blob: ad2f1e4b391c90d4cb54ace7f34340b545b90479 [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 (
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -080018 "strconv"
19
Dan Willemsen581341d2017-02-09 16:16:31 -080020 "android/soong/android"
Dan Willemsen581341d2017-02-09 16:16:31 -080021)
22
23type CoverageProperties struct {
24 Native_coverage *bool
25
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -080026 CoverageEnabled bool `blueprint:"mutated"`
27 IsCoverageVariant bool `blueprint:"mutated"`
Dan Willemsen581341d2017-02-09 16:16:31 -080028}
29
30type coverage struct {
31 Properties CoverageProperties
32
33 // Whether binaries containing this module need --coverage added to their ldflags
34 linkCoverage bool
35}
36
37func (cov *coverage) props() []interface{} {
38 return []interface{}{&cov.Properties}
39}
40
41func (cov *coverage) begin(ctx BaseModuleContext) {}
42
43func (cov *coverage) deps(ctx BaseModuleContext, deps Deps) Deps {
44 return deps
45}
46
47func (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
Pirama Arumuga Nainarc7679de2019-02-18 22:23:42 -080056
57 // Override -Wframe-larger-than and non-default optimization
58 // flags that the module may use.
59 flags.CFlags = append(flags.CFlags, "-Wno-frame-larger-than=", "-O0")
Dan Willemsen581341d2017-02-09 16:16:31 -080060 }
61
62 // Even if we don't have coverage enabled, if any of our object files were compiled
63 // with coverage, then we need to add --coverage to our ldflags.
64 if !cov.linkCoverage {
65 if ctx.static() && !ctx.staticBinary() {
66 // For static libraries, the only thing that changes our object files
67 // are included whole static libraries, so check to see if any of
68 // those have coverage enabled.
Colin Crossee6143c2017-12-30 17:54:27 -080069 ctx.VisitDirectDepsWithTag(wholeStaticDepTag, func(m android.Module) {
Dan Willemsen581341d2017-02-09 16:16:31 -080070 if cc, ok := m.(*Module); ok && cc.coverage != nil {
71 if cc.coverage.linkCoverage {
72 cov.linkCoverage = true
73 }
74 }
75 })
76 } else {
77 // For executables and shared libraries, we need to check all of
78 // our static dependencies.
Colin Crossd11fcda2017-10-23 17:59:01 -070079 ctx.VisitDirectDeps(func(m android.Module) {
Dan Willemsen581341d2017-02-09 16:16:31 -080080 cc, ok := m.(*Module)
81 if !ok || cc.coverage == nil {
82 return
83 }
84
85 if static, ok := cc.linker.(libraryInterface); !ok || !static.static() {
86 return
87 }
88
89 if cc.coverage.linkCoverage {
90 cov.linkCoverage = true
91 }
92 })
93 }
94 }
95
96 if cov.linkCoverage {
97 flags.LdFlags = append(flags.LdFlags, "--coverage")
98 }
99
100 return flags
101}
102
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800103func coverageMutator(mctx android.BottomUpMutatorContext) {
104 // Coverage is disabled globally
105 if !mctx.DeviceConfig().NativeCoverageEnabled() {
106 return
107 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800108
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800109 if c, ok := mctx.Module().(*Module); ok {
110 var needCoverageVariant bool
111 var needCoverageBuild bool
112
113 if mctx.Host() {
Pirama Arumuga Nainar0b882f02018-04-23 22:44:39 +0000114 // TODO(dwillemsen): because of -nodefaultlibs, we must depend on libclang_rt.profile-*.a
115 // Just turn off for now.
Pirama Arumuga Nainar81138352019-02-14 15:38:02 -0800116 } else if c.IsStubs() {
117 // Do not enable coverage for platform stub libraries
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800118 } else if c.isNDKStubLibrary() {
119 // Do not enable coverage for NDK stub libraries
120 } else if c.coverage != nil {
121 // Check if Native_coverage is set to false. This property defaults to true.
122 needCoverageVariant = BoolDefault(c.coverage.Properties.Native_coverage, true)
123
124 if sdk_version := String(c.Properties.Sdk_version); sdk_version != "current" {
125 // Native coverage is not supported for SDK versions < 23
126 if fromApi, err := strconv.Atoi(sdk_version); err == nil && fromApi < 23 {
127 needCoverageVariant = false
128 }
129 }
130
131 if needCoverageVariant {
132 // Coverage variant is actually built with coverage if enabled for its module path
133 needCoverageBuild = mctx.DeviceConfig().CoverageEnabledForPath(mctx.ModuleDir())
134 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800135 }
136
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800137 if needCoverageVariant {
138 m := mctx.CreateVariations("", "cov")
139
140 // Setup the non-coverage version and set HideFromMake and
141 // PreventInstall to true.
142 m[0].(*Module).coverage.Properties.CoverageEnabled = false
143 m[0].(*Module).coverage.Properties.IsCoverageVariant = false
144 m[0].(*Module).Properties.HideFromMake = true
145 m[0].(*Module).Properties.PreventInstall = true
146
147 // The coverage-enabled version inherits HideFromMake,
148 // PreventInstall from the original module.
149 m[1].(*Module).coverage.Properties.CoverageEnabled = needCoverageBuild
150 m[1].(*Module).coverage.Properties.IsCoverageVariant = true
Dan Willemsen581341d2017-02-09 16:16:31 -0800151 }
152 }
153}