blob: cf67c9f0cc8812441baabccf5469ef8e1dcb7092 [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
56 }
57
58 // Even if we don't have coverage enabled, if any of our object files were compiled
59 // with coverage, then we need to add --coverage to our ldflags.
60 if !cov.linkCoverage {
61 if ctx.static() && !ctx.staticBinary() {
62 // For static libraries, the only thing that changes our object files
63 // are included whole static libraries, so check to see if any of
64 // those have coverage enabled.
Colin Crossee6143c2017-12-30 17:54:27 -080065 ctx.VisitDirectDepsWithTag(wholeStaticDepTag, func(m android.Module) {
Dan Willemsen581341d2017-02-09 16:16:31 -080066 if cc, ok := m.(*Module); ok && cc.coverage != nil {
67 if cc.coverage.linkCoverage {
68 cov.linkCoverage = true
69 }
70 }
71 })
72 } else {
73 // For executables and shared libraries, we need to check all of
74 // our static dependencies.
Colin Crossd11fcda2017-10-23 17:59:01 -070075 ctx.VisitDirectDeps(func(m android.Module) {
Dan Willemsen581341d2017-02-09 16:16:31 -080076 cc, ok := m.(*Module)
77 if !ok || cc.coverage == nil {
78 return
79 }
80
81 if static, ok := cc.linker.(libraryInterface); !ok || !static.static() {
82 return
83 }
84
85 if cc.coverage.linkCoverage {
86 cov.linkCoverage = true
87 }
88 })
89 }
90 }
91
92 if cov.linkCoverage {
93 flags.LdFlags = append(flags.LdFlags, "--coverage")
94 }
95
96 return flags
97}
98
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -080099func coverageMutator(mctx android.BottomUpMutatorContext) {
100 // Coverage is disabled globally
101 if !mctx.DeviceConfig().NativeCoverageEnabled() {
102 return
103 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800104
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800105 if c, ok := mctx.Module().(*Module); ok {
106 var needCoverageVariant bool
107 var needCoverageBuild bool
108
109 if mctx.Host() {
Pirama Arumuga Nainar0b882f02018-04-23 22:44:39 +0000110 // TODO(dwillemsen): because of -nodefaultlibs, we must depend on libclang_rt.profile-*.a
111 // Just turn off for now.
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800112 } else if c.useVndk() || c.hasVendorVariant() {
113 // Do not enable coverage for VNDK libraries
Pirama Arumuga Nainar81138352019-02-14 15:38:02 -0800114 } else if c.IsStubs() {
115 // Do not enable coverage for platform stub libraries
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800116 } else if c.isNDKStubLibrary() {
117 // Do not enable coverage for NDK stub libraries
118 } else if c.coverage != nil {
119 // Check if Native_coverage is set to false. This property defaults to true.
120 needCoverageVariant = BoolDefault(c.coverage.Properties.Native_coverage, true)
121
122 if sdk_version := String(c.Properties.Sdk_version); sdk_version != "current" {
123 // Native coverage is not supported for SDK versions < 23
124 if fromApi, err := strconv.Atoi(sdk_version); err == nil && fromApi < 23 {
125 needCoverageVariant = false
126 }
127 }
128
129 if needCoverageVariant {
130 // Coverage variant is actually built with coverage if enabled for its module path
131 needCoverageBuild = mctx.DeviceConfig().CoverageEnabledForPath(mctx.ModuleDir())
132 }
Dan Willemsen581341d2017-02-09 16:16:31 -0800133 }
134
Pirama Arumuga Nainar1acd4472018-12-10 15:12:40 -0800135 if needCoverageVariant {
136 m := mctx.CreateVariations("", "cov")
137
138 // Setup the non-coverage version and set HideFromMake and
139 // PreventInstall to true.
140 m[0].(*Module).coverage.Properties.CoverageEnabled = false
141 m[0].(*Module).coverage.Properties.IsCoverageVariant = false
142 m[0].(*Module).Properties.HideFromMake = true
143 m[0].(*Module).Properties.PreventInstall = true
144
145 // The coverage-enabled version inherits HideFromMake,
146 // PreventInstall from the original module.
147 m[1].(*Module).coverage.Properties.CoverageEnabled = needCoverageBuild
148 m[1].(*Module).coverage.Properties.IsCoverageVariant = true
Dan Willemsen581341d2017-02-09 16:16:31 -0800149 }
150 }
151}