From 2e30b5a1e2ca21555bb3aa50f91e87f4d2821bb6 Mon Sep 17 00:00:00 2001 From: Weston Schmidt Date: Tue, 13 Jul 2021 03:17:27 -0700 Subject: [PATCH] Add support for gcovr --sonarqube report Sonarcloud.io only can read the sonarqube based report that gcovr can produce. This change enables support for this output in meson and ninja. Signed-off-by: Weston Schmidt --- docs/markdown/Feature-autodetection.md | 2 ++ docs/markdown/Unit-tests.md | 1 + mesonbuild/backend/ninjabackend.py | 7 +++++++ mesonbuild/scripts/coverage.py | 14 ++++++++++++++ .../coverage-sonarqube/meson.build | 1 + 5 files changed, 25 insertions(+) create mode 100644 test cases/common/150 reserved targets/coverage-sonarqube/meson.build diff --git a/docs/markdown/Feature-autodetection.md b/docs/markdown/Feature-autodetection.md index 4d366d939..237122662 100644 --- a/docs/markdown/Feature-autodetection.md +++ b/docs/markdown/Feature-autodetection.md @@ -26,6 +26,8 @@ Coverage When doing a code coverage build, Meson will check for existence of the binaries `gcovr`, `lcov` and `genhtml`. If version 3.3 or higher of the first is found, targets called *coverage-text*, *coverage-xml* +and *coverage-html* are generated. If version 4.2 or higher of the +first is found, targets *coverage-text*, *coverage-xml*, *coverage-sonarqube* and *coverage-html* are generated. Alternatively, if the latter two are found, only the target *coverage-html* is generated. Coverage reports can then be produced simply by calling e.g. `meson compile diff --git a/docs/markdown/Unit-tests.md b/docs/markdown/Unit-tests.md index 5233a4fe8..c9856d4b3 100644 --- a/docs/markdown/Unit-tests.md +++ b/docs/markdown/Unit-tests.md @@ -47,6 +47,7 @@ functions that get called). 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) (version 3.3 or higher) +`coverage-sonarqube` which is provided by [Gcovr](http://gcovr.com) (version 4.2 or higher) and `coverage-html`, which requires [Lcov](https://ltp.sourceforge.io/coverage/lcov.php) and [GenHTML](https://linux.die.net/man/1/genhtml) or diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index 85a5f363c..31d981313 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -1064,6 +1064,13 @@ class NinjaBackend(backends.Backend): # Alias that runs the target defined above self.create_target_alias('meson-coverage-xml') + e = NinjaBuildElement(self.all_outputs, 'meson-coverage-sonarqube', 'CUSTOM_COMMAND', 'PHONY') + self.generate_coverage_command(e, ['--sonarqube']) + e.add_item('description', 'Generates Sonarqube XML coverage report') + self.add_build(e) + # Alias that runs the target defined above + self.create_target_alias('meson-coverage-sonarqube') + e = NinjaBuildElement(self.all_outputs, 'meson-coverage-text', 'CUSTOM_COMMAND', 'PHONY') self.generate_coverage_command(e, ['--text']) e.add_item('description', 'Generates text coverage report') diff --git a/mesonbuild/scripts/coverage.py b/mesonbuild/scripts/coverage.py index b60d5d1e3..2bd93e8a5 100644 --- a/mesonbuild/scripts/coverage.py +++ b/mesonbuild/scripts/coverage.py @@ -46,6 +46,18 @@ def coverage(outputs: T.List[str], source_root: str, subproject_root: str, build print('gcovr >= 3.3 needed to generate Xml coverage report') exitcode = 1 + if not outputs or 'sonarqube' in outputs: + if gcovr_exe: + subprocess.check_call(gcovr_base_cmd + + ['--sonarqube', + '-o', os.path.join(log_dir, 'sonarqube.xml'), + '-e', subproject_root + ] + gcov_exe_args) + outfiles.append(('Sonarqube', pathlib.Path(log_dir, 'sonarqube.xml'))) + elif outputs: + print('gcovr >= 4.2 needed to generate Xml coverage report') + exitcode = 1 + if not outputs or 'text' in outputs: if gcovr_exe: subprocess.check_call(gcovr_base_cmd + @@ -156,6 +168,8 @@ def run(args: T.List[str]) -> int: const='text', help='generate Text report') parser.add_argument('--xml', dest='outputs', action='append_const', const='xml', help='generate Xml report') + parser.add_argument('--sonarqube', dest='outputs', action='append_const', + const='sonarqube', help='generate Sonarqube Xml report') parser.add_argument('--html', dest='outputs', action='append_const', const='html', help='generate Html report') parser.add_argument('--use_llvm_cov', action='store_true', diff --git a/test cases/common/150 reserved targets/coverage-sonarqube/meson.build b/test cases/common/150 reserved targets/coverage-sonarqube/meson.build new file mode 100644 index 000000000..ac4c0d26e --- /dev/null +++ b/test cases/common/150 reserved targets/coverage-sonarqube/meson.build @@ -0,0 +1 @@ +executable('test-coverage-sonarqube', '../test.c')