2 # Copyright 2009, Google Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
15 # * Neither the name of Google Inc. nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 """SCons tool for generating code coverage.
33 This module enhances a debug environment to add code coverage.
34 It is used as follows:
35 coverage_env = dbg_env.Clone(tools = ['code_coverage'])
39 def AddCoverageSetup(env
):
40 """Add coverage related build steps and dependency links.
43 env: a leaf environment ready to have coverage steps added.
45 # Add a step to start coverage (for instance windows needs this).
46 # This step should get run before and tests are run.
47 if env
.get('COVERAGE_START_CMD', None):
48 start
= env
.Command('$COVERAGE_START_FILE', [], '$COVERAGE_START_CMD')
49 env
.AlwaysBuild(start
)
53 # Add a step to end coverage (used on basically all platforms).
54 # This step should get after all the tests have run.
55 if env
.get('COVERAGE_STOP_CMD', None):
56 stop
= env
.Command('$COVERAGE_OUTPUT_FILE', [], '$COVERAGE_STOP_CMD')
61 # start must happen before tests run, stop must happen after.
62 for group
in env
.SubstList2('$COVERAGE_TARGETS'):
63 group_alias
= env
.Alias(group
)
64 # Force each alias to happen after start but before stop.
65 env
.Requires(group_alias
, start
)
66 env
.Requires(stop
, group_alias
)
67 # Force each source of the aliases to happen after start but before stop.
68 # This is needed to work around non-standard aliases in some projects.
69 for test
in group_alias
:
70 for s
in test
.sources
:
71 env
.Requires(s
, start
)
74 # Add an alias for coverage.
75 env
.Alias('coverage', [start
, stop
])
79 # NOTE: SCons requires the use of this name, which fails gpylint.
80 """SCons entry point for this tool."""
82 env
['COVERAGE_ENABLED'] = True
85 # Setup up coverage related tool paths.
86 # These can be overridden elsewhere, if needed, to relocate the tools.
88 COVERAGE_GENHTML
='genhtml',
89 COVERAGE_ANALYZER
='coverage_analyzer.exe',
90 COVERAGE_VSPERFCMD
='VSPerfCmd.exe',
91 COVERAGE_VSINSTR
='vsinstr.exe',
93 # Setup coverage related locations.
94 COVERAGE_DIR
='$TARGET_ROOT/coverage',
95 COVERAGE_HTML_DIR
='$COVERAGE_DIR/html',
96 COVERAGE_START_FILE
='$COVERAGE_DIR/start.junk',
97 COVERAGE_OUTPUT_FILE
='$COVERAGE_DIR/coverage.lcov',
99 # The list of aliases containing test execution targets.
100 COVERAGE_TARGETS
=['run_all_tests'],
103 # Add in coverage flags. These come from target_platform_xxx.
105 CCFLAGS
='$COVERAGE_CCFLAGS',
106 LIBS
='$COVERAGE_LIBS',
107 LINKFLAGS
='$COVERAGE_LINKFLAGS',
108 SHLINKFLAGS
='$COVERAGE_SHLINKFLAGS',
111 # Change the definition of Install if required by the platform.
112 if env
.get('COVERAGE_INSTALL'):
113 env
['PRECOVERAGE_INSTALL'] = env
['INSTALL']
114 env
['INSTALL'] = env
['COVERAGE_INSTALL']
116 # Add any extra paths.
117 env
.AppendENVPath('PATH', env
.SubstList2('$COVERAGE_EXTRA_PATHS'))
119 # Add coverage start/stop and processing in deferred steps.
120 env
.Defer(AddCoverageSetup
)