| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 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 |  | 
 | 15 | package cc | 
 | 16 |  | 
 | 17 | import ( | 
 | 18 | 	"android/soong/android" | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 19 | ) | 
 | 20 |  | 
 | 21 | type CoverageProperties struct { | 
 | 22 | 	Native_coverage *bool | 
 | 23 |  | 
 | 24 | 	CoverageEnabled bool `blueprint:"mutated"` | 
 | 25 | } | 
 | 26 |  | 
 | 27 | type coverage struct { | 
 | 28 | 	Properties CoverageProperties | 
 | 29 |  | 
 | 30 | 	// Whether binaries containing this module need --coverage added to their ldflags | 
 | 31 | 	linkCoverage bool | 
 | 32 | } | 
 | 33 |  | 
 | 34 | func (cov *coverage) props() []interface{} { | 
 | 35 | 	return []interface{}{&cov.Properties} | 
 | 36 | } | 
 | 37 |  | 
 | 38 | func (cov *coverage) begin(ctx BaseModuleContext) {} | 
 | 39 |  | 
 | 40 | func (cov *coverage) deps(ctx BaseModuleContext, deps Deps) Deps { | 
 | 41 | 	return deps | 
 | 42 | } | 
 | 43 |  | 
 | 44 | func (cov *coverage) flags(ctx ModuleContext, flags Flags) Flags { | 
 | 45 | 	if !ctx.DeviceConfig().NativeCoverageEnabled() { | 
 | 46 | 		return flags | 
 | 47 | 	} | 
 | 48 |  | 
 | 49 | 	if cov.Properties.CoverageEnabled { | 
 | 50 | 		flags.Coverage = true | 
 | 51 | 		flags.GlobalFlags = append(flags.GlobalFlags, "--coverage", "-O0") | 
 | 52 | 		cov.linkCoverage = true | 
 | 53 | 	} | 
 | 54 |  | 
 | 55 | 	// Even if we don't have coverage enabled, if any of our object files were compiled | 
 | 56 | 	// with coverage, then we need to add --coverage to our ldflags. | 
 | 57 | 	if !cov.linkCoverage { | 
 | 58 | 		if ctx.static() && !ctx.staticBinary() { | 
 | 59 | 			// For static libraries, the only thing that changes our object files | 
 | 60 | 			// are included whole static libraries, so check to see if any of | 
 | 61 | 			// those have coverage enabled. | 
| Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 62 | 			ctx.VisitDirectDeps(func(m android.Module) { | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 63 | 				if ctx.OtherModuleDependencyTag(m) != wholeStaticDepTag { | 
 | 64 | 					return | 
 | 65 | 				} | 
 | 66 |  | 
 | 67 | 				if cc, ok := m.(*Module); ok && cc.coverage != nil { | 
 | 68 | 					if cc.coverage.linkCoverage { | 
 | 69 | 						cov.linkCoverage = true | 
 | 70 | 					} | 
 | 71 | 				} | 
 | 72 | 			}) | 
 | 73 | 		} else { | 
 | 74 | 			// For executables and shared libraries, we need to check all of | 
 | 75 | 			// our static dependencies. | 
| Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 76 | 			ctx.VisitDirectDeps(func(m android.Module) { | 
| Dan Willemsen | 581341d | 2017-02-09 16:16:31 -0800 | [diff] [blame] | 77 | 				cc, ok := m.(*Module) | 
 | 78 | 				if !ok || cc.coverage == nil { | 
 | 79 | 					return | 
 | 80 | 				} | 
 | 81 |  | 
 | 82 | 				if static, ok := cc.linker.(libraryInterface); !ok || !static.static() { | 
 | 83 | 					return | 
 | 84 | 				} | 
 | 85 |  | 
 | 86 | 				if cc.coverage.linkCoverage { | 
 | 87 | 					cov.linkCoverage = true | 
 | 88 | 				} | 
 | 89 | 			}) | 
 | 90 | 		} | 
 | 91 | 	} | 
 | 92 |  | 
 | 93 | 	if cov.linkCoverage { | 
 | 94 | 		flags.LdFlags = append(flags.LdFlags, "--coverage") | 
 | 95 | 	} | 
 | 96 |  | 
 | 97 | 	return flags | 
 | 98 | } | 
 | 99 |  | 
 | 100 | func coverageLinkingMutator(mctx android.BottomUpMutatorContext) { | 
 | 101 | 	if c, ok := mctx.Module().(*Module); ok && c.coverage != nil { | 
 | 102 | 		var enabled bool | 
 | 103 |  | 
 | 104 | 		if !mctx.DeviceConfig().NativeCoverageEnabled() { | 
 | 105 | 			// Coverage is disabled globally | 
 | 106 | 		} else if mctx.Host() { | 
 | 107 | 			// TODO(dwillemsen): because of -nodefaultlibs, we must depend on libclang_rt.profile-*.a | 
 | 108 | 			// Just turn off for now. | 
 | 109 | 		} 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 | } |