Home | History | Annotate | Download | only in cc
      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"
     19 	"github.com/google/blueprint"
     20 )
     21 
     22 type CoverageProperties struct {
     23 	Native_coverage *bool
     24 
     25 	CoverageEnabled bool `blueprint:"mutated"`
     26 }
     27 
     28 type coverage struct {
     29 	Properties CoverageProperties
     30 
     31 	// Whether binaries containing this module need --coverage added to their ldflags
     32 	linkCoverage bool
     33 }
     34 
     35 func (cov *coverage) props() []interface{} {
     36 	return []interface{}{&cov.Properties}
     37 }
     38 
     39 func (cov *coverage) begin(ctx BaseModuleContext) {}
     40 
     41 func (cov *coverage) deps(ctx BaseModuleContext, deps Deps) Deps {
     42 	return deps
     43 }
     44 
     45 func (cov *coverage) flags(ctx ModuleContext, flags Flags) Flags {
     46 	if !ctx.DeviceConfig().NativeCoverageEnabled() {
     47 		return flags
     48 	}
     49 
     50 	if cov.Properties.CoverageEnabled {
     51 		flags.Coverage = true
     52 		flags.GlobalFlags = append(flags.GlobalFlags, "--coverage", "-O0")
     53 		cov.linkCoverage = true
     54 	}
     55 
     56 	// Even if we don't have coverage enabled, if any of our object files were compiled
     57 	// with coverage, then we need to add --coverage to our ldflags.
     58 	if !cov.linkCoverage {
     59 		if ctx.static() && !ctx.staticBinary() {
     60 			// For static libraries, the only thing that changes our object files
     61 			// are included whole static libraries, so check to see if any of
     62 			// those have coverage enabled.
     63 			ctx.VisitDirectDeps(func(m blueprint.Module) {
     64 				if ctx.OtherModuleDependencyTag(m) != wholeStaticDepTag {
     65 					return
     66 				}
     67 
     68 				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.
     77 			ctx.VisitDirectDeps(func(m blueprint.Module) {
     78 				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 
    101 func 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
    107 		} else if mctx.Host() {
    108 			// TODO(dwillemsen): because of -nodefaultlibs, we must depend on libclang_rt.profile-*.a
    109 			// Just turn off for now.
    110 		} else if c.coverage.Properties.Native_coverage != nil {
    111 			enabled = *c.coverage.Properties.Native_coverage
    112 		} else {
    113 			enabled = mctx.DeviceConfig().CoverageEnabledForPath(mctx.ModuleDir())
    114 		}
    115 
    116 		if enabled {
    117 			// Create a variation so that we don't need to recompile objects
    118 			// when turning on or off coverage. We'll still relink the necessary
    119 			// binaries, since we don't know which ones those are until later.
    120 			m := mctx.CreateLocalVariations("cov")
    121 			m[0].(*Module).coverage.Properties.CoverageEnabled = true
    122 		}
    123 	}
    124 }
    125