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
15package cc
16
17import (
18	"strconv"
19
20	"github.com/google/blueprint"
21
22	"android/soong/android"
23)
24
25type CoverageProperties struct {
26	Native_coverage *bool
27
28	NeedCoverageVariant bool `blueprint:"mutated"`
29	NeedCoverageBuild   bool `blueprint:"mutated"`
30
31	CoverageEnabled   bool `blueprint:"mutated"`
32	IsCoverageVariant bool `blueprint:"mutated"`
33}
34
35type coverage struct {
36	Properties CoverageProperties
37
38	// Whether binaries containing this module need --coverage added to their ldflags
39	linkCoverage bool
40}
41
42func (cov *coverage) props() []interface{} {
43	return []interface{}{&cov.Properties}
44}
45
46func getGcovProfileLibraryName(ctx ModuleContextIntf) string {
47	// This function should only ever be called for a cc.Module, so the
48	// following statement should always succeed.
49	if ctx.useSdk() {
50		return "libprofile-extras_ndk"
51	} else {
52		return "libprofile-extras"
53	}
54}
55
56func getClangProfileLibraryName(ctx ModuleContextIntf) string {
57	if ctx.useSdk() {
58		return "libprofile-clang-extras_ndk"
59	} else {
60		return "libprofile-clang-extras"
61	}
62}
63
64func (cov *coverage) deps(ctx DepsContext, deps Deps) Deps {
65	if cov.Properties.NeedCoverageVariant {
66		ctx.AddVariationDependencies([]blueprint.Variation{
67			{Mutator: "link", Variation: "static"},
68		}, CoverageDepTag, getGcovProfileLibraryName(ctx))
69		ctx.AddVariationDependencies([]blueprint.Variation{
70			{Mutator: "link", Variation: "static"},
71		}, CoverageDepTag, getClangProfileLibraryName(ctx))
72	}
73	return deps
74}
75
76func (cov *coverage) flags(ctx ModuleContext, flags Flags, deps PathDeps) (Flags, PathDeps) {
77	clangCoverage := ctx.DeviceConfig().ClangCoverageEnabled()
78	gcovCoverage := ctx.DeviceConfig().GcovCoverageEnabled()
79
80	if !gcovCoverage && !clangCoverage {
81		return flags, deps
82	}
83
84	if cov.Properties.CoverageEnabled {
85		cov.linkCoverage = true
86
87		if gcovCoverage {
88			flags.GcovCoverage = true
89			flags.Local.CommonFlags = append(flags.Local.CommonFlags, "--coverage", "-O0")
90
91			// Override -Wframe-larger-than and non-default optimization
92			// flags that the module may use.
93			flags.Local.CFlags = append(flags.Local.CFlags, "-Wno-frame-larger-than=", "-O0")
94		} else if clangCoverage {
95			flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-fprofile-instr-generate", "-fcoverage-mapping", "-Wno-pass-failed")
96		}
97	}
98
99	// Even if we don't have coverage enabled, if any of our object files were compiled
100	// with coverage, then we need to add --coverage to our ldflags.
101	if !cov.linkCoverage {
102		if ctx.static() && !ctx.staticBinary() {
103			// For static libraries, the only thing that changes our object files
104			// are included whole static libraries, so check to see if any of
105			// those have coverage enabled.
106			ctx.VisitDirectDepsWithTag(wholeStaticDepTag, func(m android.Module) {
107				if cc, ok := m.(*Module); ok && cc.coverage != nil {
108					if cc.coverage.linkCoverage {
109						cov.linkCoverage = true
110					}
111				}
112			})
113		} else {
114			// For executables and shared libraries, we need to check all of
115			// our static dependencies.
116			ctx.VisitDirectDeps(func(m android.Module) {
117				cc, ok := m.(*Module)
118				if !ok || cc.coverage == nil {
119					return
120				}
121
122				if static, ok := cc.linker.(libraryInterface); !ok || !static.static() {
123					return
124				}
125
126				if cc.coverage.linkCoverage {
127					cov.linkCoverage = true
128				}
129			})
130		}
131	}
132
133	if cov.linkCoverage {
134		if gcovCoverage {
135			flags.Local.LdFlags = append(flags.Local.LdFlags, "--coverage")
136
137			coverage := ctx.GetDirectDepWithTag(getGcovProfileLibraryName(ctx), CoverageDepTag).(*Module)
138			deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path())
139
140			flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,--wrap,getenv")
141		} else if clangCoverage {
142			flags.Local.LdFlags = append(flags.Local.LdFlags, "-fprofile-instr-generate")
143
144			coverage := ctx.GetDirectDepWithTag(getClangProfileLibraryName(ctx), CoverageDepTag).(*Module)
145			deps.WholeStaticLibs = append(deps.WholeStaticLibs, coverage.OutputFile().Path())
146		}
147	}
148
149	return flags, deps
150}
151
152func (cov *coverage) begin(ctx BaseModuleContext) {
153	if ctx.Host() {
154		// TODO(dwillemsen): because of -nodefaultlibs, we must depend on libclang_rt.profile-*.a
155		// Just turn off for now.
156	} else {
157		cov.Properties = SetCoverageProperties(ctx, cov.Properties, ctx.nativeCoverage(), ctx.useSdk(), ctx.sdkVersion())
158	}
159}
160
161func SetCoverageProperties(ctx android.BaseModuleContext, properties CoverageProperties, moduleTypeHasCoverage bool,
162	useSdk bool, sdkVersion string) CoverageProperties {
163	// Coverage is disabled globally
164	if !ctx.DeviceConfig().NativeCoverageEnabled() {
165		return properties
166	}
167
168	var needCoverageVariant bool
169	var needCoverageBuild bool
170
171	if moduleTypeHasCoverage {
172		// Check if Native_coverage is set to false.  This property defaults to true.
173		needCoverageVariant = BoolDefault(properties.Native_coverage, true)
174		if useSdk && sdkVersion != "current" {
175			// Native coverage is not supported for SDK versions < 23
176			if fromApi, err := strconv.Atoi(sdkVersion); err == nil && fromApi < 23 {
177				needCoverageVariant = false
178			}
179		}
180
181		if needCoverageVariant {
182			// Coverage variant is actually built with coverage if enabled for its module path
183			needCoverageBuild = ctx.DeviceConfig().NativeCoverageEnabledForPath(ctx.ModuleDir())
184		}
185	}
186
187	properties.NeedCoverageBuild = needCoverageBuild
188	properties.NeedCoverageVariant = needCoverageVariant
189
190	return properties
191}
192
193// Coverage is an interface for non-CC modules to implement to be mutated for coverage
194type Coverage interface {
195	android.Module
196	IsNativeCoverageNeeded(ctx android.BaseModuleContext) bool
197	PreventInstall()
198	HideFromMake()
199	MarkAsCoverageVariant(bool)
200	EnableCoverageIfNeeded()
201}
202
203func coverageMutator(mctx android.BottomUpMutatorContext) {
204	if c, ok := mctx.Module().(*Module); ok && c.coverage != nil {
205		needCoverageVariant := c.coverage.Properties.NeedCoverageVariant
206		needCoverageBuild := c.coverage.Properties.NeedCoverageBuild
207		if needCoverageVariant {
208			m := mctx.CreateVariations("", "cov")
209
210			// Setup the non-coverage version and set HideFromMake and
211			// PreventInstall to true.
212			m[0].(*Module).coverage.Properties.CoverageEnabled = false
213			m[0].(*Module).coverage.Properties.IsCoverageVariant = false
214			m[0].(*Module).Properties.HideFromMake = true
215			m[0].(*Module).Properties.PreventInstall = true
216
217			// The coverage-enabled version inherits HideFromMake,
218			// PreventInstall from the original module.
219			m[1].(*Module).coverage.Properties.CoverageEnabled = needCoverageBuild
220			m[1].(*Module).coverage.Properties.IsCoverageVariant = true
221		}
222	} else if cov, ok := mctx.Module().(Coverage); ok && cov.IsNativeCoverageNeeded(mctx) {
223		// APEX and Rust modules fall here
224
225		// Note: variant "" is also created because an APEX can be depended on by another
226		// module which are split into "" and "cov" variants. e.g. when cc_test refers
227		// to an APEX via 'data' property.
228		m := mctx.CreateVariations("", "cov")
229		m[0].(Coverage).MarkAsCoverageVariant(false)
230		m[0].(Coverage).PreventInstall()
231		m[0].(Coverage).HideFromMake()
232
233		m[1].(Coverage).MarkAsCoverageVariant(true)
234		m[1].(Coverage).EnableCoverageIfNeeded()
235	}
236}
237