Allow gcovr >= 3.1 to be used to generate html coverage report

Modern gcovr includes html generation support so if lcov and
genhtml are not available fallback to gcovr.

Kept lcov and genhtml as default so to not surprise existing
users of coverage-html with the different output of gcovr.

gcovr added html support in 3.0 but as there already is a test
for 3.1 because of the changes to -r/--rootdir I opted to only
allow html generation for >= 3.1 to keep things simple.
pull/3145/head
Joel Klinghed 7 years ago
parent da01770261
commit 6266089866
  1. 2
      docs/markdown/Feature-autodetection.md
  2. 2
      docs/markdown/Unit-tests.md
  3. 13
      mesonbuild/backend/ninjabackend.py
  4. 10
      mesonbuild/scripts/coverage.py
  5. 7
      run_unittests.py

@ -16,4 +16,4 @@ If you do not wish to use CCache for some reason, just specify your compiler wit
Coverage
--
When doing a code coverage build, Meson will check the existence of binaries `gcovr`, `lcov` and `genhtml`. If the first one is found, it will create targets called *coverage-text* and *coverage-xml*. If the latter two are found, it generates the target *coverage-html*. You can then generate coverage reports just by calling e.g. `ninja coverage-xml`.
When doing a code coverage build, Meson will check the existence of binaries `gcovr`, `lcov` and `genhtml`. If the first one is found, it will create targets called *coverage-text* and *coverage-xml*. If the latter two or a new enough `gcovr` is found, it generates the target *coverage-html*. You can then generate coverage reports just by calling e.g. `ninja coverage-xml`.

@ -30,7 +30,7 @@ Note how you need to specify multiple values as an array.
Coverage
--
If you enable coverage measurements by giving Meson the command line flag `-Db_coverage=true`, you can generate coverage reports. Meson will autodetect what coverage generator tools you have installed and will generate the corresponding targets. These targets are `coverage-xml` and `coverage-text` which are both provided by [Gcovr](http://gcovr.com) and `coverage-html`, which requires [Lcov](https://ltp.sourceforge.io/coverage/lcov.php) and [GenHTML](https://linux.die.net/man/1/genhtml).
If you enable coverage measurements by giving Meson the command line flag `-Db_coverage=true`, you can generate coverage reports. Meson will autodetect what coverage generator tools you have installed and will generate the corresponding targets. These targets are `coverage-xml` and `coverage-text` which are both provided by [Gcovr](http://gcovr.com) and `coverage-html`, which requires [Lcov](https://ltp.sourceforge.io/coverage/lcov.php) and [GenHTML](https://linux.die.net/man/1/genhtml) or [Gcovr](http://gcovr.com) with html support.
The output of these commands is written to the log directory `meson-logs` in your build directory.

@ -668,6 +668,19 @@ int dummy;
elem.add_item('COMMAND', command)
elem.add_item('DESC', 'Generating HTML coverage report.')
elem.write(outfile)
elif gcovr_exe and gcovr_3_1:
added_rule = True
htmloutdir = os.path.join(self.environment.get_log_dir(), 'coveragereport')
phony_elem = NinjaBuildElement(self.all_outputs, 'meson-coverage-html', 'phony', os.path.join(htmloutdir, 'index.html'))
phony_elem.write(outfile)
# Alias that runs the target defined above
self.create_target_alias('meson-coverage-html', outfile)
elem = NinjaBuildElement(self.all_outputs, os.path.join(htmloutdir, 'index.html'), 'CUSTOM_COMMAND', '')
command = [gcovr_exe, '--html', '--html-details', '-r', self.environment.get_build_dir(),
'-o', os.path.join(htmloutdir, 'index.html')]
elem.add_item('COMMAND', command)
elem.add_item('DESC', 'Generating HTML coverage report.')
elem.write(outfile)
if not added_rule:
mlog.warning('coverage requested but neither gcovr nor lcov/genhtml found.')

@ -70,13 +70,21 @@ def coverage(source_root, build_root, log_dir):
'--show-details',
'--branch-coverage',
covinfo])
elif gcovr_exe and gcovr_3_1:
htmloutdir = os.path.join(log_dir, 'coveragereport')
subprocess.check_call([gcovr_exe,
'--html',
'--html-details',
'-r', build_root,
'-o', os.path.join(htmloutdir, 'index.html'),
])
if gcovr_exe:
print('')
print('XML coverage report can be found at',
pathlib.Path(log_dir, 'coverage.xml').as_uri())
print('Text coverage report can be found at',
pathlib.Path(log_dir, 'coverage.txt').as_uri())
if lcov_exe and genhtml_exe:
if (lcov_exe and genhtml_exe) or (gcovr_exe and gcovr_3_1):
print('Html coverage report can be found at',
pathlib.Path(htmloutdir, 'index.html').as_uri())
return 0

@ -2554,10 +2554,11 @@ class LinuxlikeTests(BasePlatformTests):
self.assertIn("-fsanitize=address", i["command"])
def test_coverage(self):
if not shutil.which('gcovr'):
gcovr_exe, gcovr_3_1 = mesonbuild.environment.detect_gcovr()
if not gcovr_exe:
raise unittest.SkipTest('gcovr not found')
if not shutil.which('genhtml'):
raise unittest.SkipTest('genhtml not found')
if not shutil.which('genhtml') and not gcovr_3_1:
raise unittest.SkipTest('genhtml not found and gcovr is too old')
if 'clang' in os.environ.get('CC', ''):
# We need to use llvm-cov instead of gcovr with clang
raise unittest.SkipTest('Coverage does not work with clang right now, help wanted!')

Loading…
Cancel
Save