Skip to content

Commit 2074ea6

Browse files
authored
feat: add coverage support to QNX toolchain (#62)
Add coverage/gcc_coverage_map_format features to QNX cc_toolchain_config template, mirroring the Linux toolchain's coverage support. This enables `bazel coverage` to instrument QNX-cross-compiled binaries.
1 parent d4a95c3 commit 2074ea6

7 files changed

Lines changed: 74 additions & 1 deletion

File tree

examples/MODULE.bazel.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/qnx/aarch64/sdp/7.1.0/sdp.BUILD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ filegroup(
6868
srcs = ["target/qnx"],
6969
)
7070

71+
filegroup(
72+
name = "gcov",
73+
srcs = ["host/linux/x86_64/usr/bin/aarch64-unknown-nto-qnx7.1.0-gcov"],
74+
)
75+
7176
filegroup(
7277
name = "mkifs",
7378
srcs = ["host/linux/x86_64/usr/bin/mkifs"],

packages/qnx/aarch64/sdp/8.0.0/sdp.BUILD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ filegroup(
6868
srcs = ["target/qnx"],
6969
)
7070

71+
filegroup(
72+
name = "gcov",
73+
srcs = ["host/linux/x86_64/usr/bin/aarch64-unknown-nto-qnx8.0.0-gcov"],
74+
)
75+
7176
filegroup(
7277
name = "mkifs",
7378
srcs = ["host/linux/x86_64/usr/bin/mkifs"],

packages/qnx/x86_64/sdp/7.1.0/sdp.BUILD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ filegroup(
6868
srcs = ["target/qnx"],
6969
)
7070

71+
filegroup(
72+
name = "gcov",
73+
srcs = ["host/linux/x86_64/usr/bin/x86_64-pc-nto-qnx7.1.0-gcov"],
74+
)
75+
7176
filegroup(
7277
name = "mkifs",
7378
srcs = ["host/linux/x86_64/usr/bin/mkifs"],

packages/qnx/x86_64/sdp/8.0.0/sdp.BUILD

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ filegroup(
6868
srcs = ["target/qnx"],
6969
)
7070

71+
filegroup(
72+
name = "gcov",
73+
srcs = ["host/linux/x86_64/usr/bin/x86_64-pc-nto-qnx8.0.0-gcov"],
74+
)
75+
7176
filegroup(
7277
name = "mkifs",
7378
srcs = ["host/linux/x86_64/usr/bin/mkifs"],

rules/gcc.bzl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ filegroup(
6969
name = "all_files",
7070
srcs = [
7171
"@{tc_pkg_repo}//:all_files",
72+
"gcov_wrapper",
7273
]
7374
)
7475
@@ -77,6 +78,7 @@ cc_toolchain_config(
7778
ar_binary = "@{tc_pkg_repo}//:ar",
7879
cc_binary = "@{tc_pkg_repo}//:cc",
7980
cxx_binary = "@{tc_pkg_repo}//:cxx",
81+
gcov_binary = "@{tc_pkg_repo}//:gcov",
8082
strip_binary = "@{tc_pkg_repo}//:strip",
8183
host_dir = "@{tc_pkg_repo}//:host_dir",
8284
target_dir = "@{tc_pkg_repo}//:target_dir",
@@ -211,6 +213,24 @@ def _impl(rctx):
211213
),
212214
},
213215
)
216+
elif rctx.attr.tc_os == "qnx":
217+
# Generate gcov wrapper for QNX toolchains to enable `bazel coverage`.
218+
# See: https://github.com/bazelbuild/rules_cc/issues/351
219+
sdp_version = "8.0.0" if rctx.attr.sdp_version == "8.0.4" else rctx.attr.sdp_version # FIXME: currently we do not support constraint "8.0.4".
220+
if rctx.attr.tc_cpu == "aarch64":
221+
gcov_triple = "aarch64-unknown-nto-qnx{sdp}".format(sdp = sdp_version)
222+
else:
223+
gcov_triple = "{cpu}-pc-nto-qnx{sdp}".format(cpu = rctx.attr.tc_cpu, sdp = sdp_version)
224+
rctx.template(
225+
"gcov_wrapper",
226+
rctx.attr._cc_gcov_wrapper_script,
227+
{
228+
"%{tc_gcov_path}": "external/score_bazel_cpp_toolchains++gcc+{repo}/host/linux/x86_64/usr/bin/{triple}-gcov".format(
229+
repo = rctx.attr.tc_pkg_repo,
230+
triple = gcov_triple,
231+
),
232+
},
233+
)
214234

215235
gcc_toolchain = repository_rule(
216236
implementation = _impl,

templates/qnx/cc_toolchain_config.bzl.template

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ load("@bazel_tools//tools/cpp:cc_toolchain_config_lib.bzl",
1919
"env_entry",
2020
"env_set",
2121
"feature",
22+
"feature_set",
2223
"flag_group",
2324
"flag_set",
2425
"tool",
@@ -424,6 +425,31 @@ def _impl(ctx):
424425
],
425426
)
426427

428+
coverage_feature = feature(name = "coverage")
429+
gcc_coverage_map_format_feature = feature(
430+
name = "gcc_coverage_map_format",
431+
provides = ["profile"],
432+
flag_sets = [
433+
flag_set(
434+
actions = [
435+
ACTION_NAMES.c_compile,
436+
ACTION_NAMES.cpp_compile,
437+
ACTION_NAMES.cpp_module_compile,
438+
ACTION_NAMES.preprocess_assemble,
439+
],
440+
flag_groups = [flag_group(
441+
expand_if_available = "gcov_gcno_file",
442+
flags = ["-fprofile-arcs", "-ftest-coverage"],
443+
)],
444+
),
445+
flag_set(
446+
actions = all_link_actions,
447+
flag_groups = [flag_group(flags = ["-lgcov"])],
448+
),
449+
],
450+
requires = [feature_set(features = ["coverage"])],
451+
)
452+
427453
# The order of the features is relevant, they are applied in this specific order.
428454
# A command line parameter from a feature at the end of the list will appear
429455
# after a command line parameter from a feature at the beginning of the list.
@@ -447,13 +473,18 @@ def _impl(ctx):
447473
supports_dynamic_linker_feature,
448474
supports_pic_feature,
449475
runtime_library_search_directories_feature,
476+
coverage_feature,
477+
gcc_coverage_map_format_feature,
450478
]
451479

452480
cxx_builtin_include_directories = [
453481
"/proc/self/cwd/{}".format(include_directory.path)
454482
for include_directory in ctx.files.cxx_builtin_include_directories
455483
]
456484

485+
# TODO: Once https://github.com/bazelbuild/rules_cc/issues/351 is fixed remove this.
486+
tool_paths = [tool_path(name = "gcov", path = "gcov_wrapper")]
487+
457488
return cc_common.create_cc_toolchain_config_info(
458489
ctx = ctx,
459490
abi_version = "%{tc_cpu}-qnx%{sdp_version}",
@@ -467,6 +498,7 @@ def _impl(ctx):
467498
target_cpu = "%{tc_cpu}",
468499
target_libc = "unknown",
469500
toolchain_identifier = "%{tc_cpu}-qnx%{sdp_version}",
501+
tool_paths = tool_paths,
470502
)
471503

472504
cc_toolchain_config = rule(
@@ -476,6 +508,7 @@ cc_toolchain_config = rule(
476508
"ar_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True),
477509
"cc_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True),
478510
"cxx_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True),
511+
"gcov_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True),
479512
"strip_binary": attr.label(allow_single_file = True, executable = True, cfg = "exec", mandatory = True),
480513
"host_dir": attr.label(allow_single_file = True, mandatory = True),
481514
"target_dir": attr.label(allow_single_file = True, mandatory = True),

0 commit comments

Comments
 (0)