Skip to content

Commit

Permalink
Add support for coverage
Browse files Browse the repository at this point in the history
In addition to the the normal setup consisting of implicit attributes
and the use of `coverage_common.instrumented_files_info`, this also
requires working around a bug in `cc_test`:
bazelbuild/bazel#20349
  • Loading branch information
fmeum committed Nov 29, 2023
1 parent 35262b2 commit d9c5efd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
42 changes: 40 additions & 2 deletions with_cfg/private/frontend.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def _frontend_impl(ctx):
data_runfiles = ctx.runfiles([executable]).merge(target[DefaultInfo].data_runfiles)
default_runfiles = ctx.runfiles([executable]).merge(target[DefaultInfo].default_runfiles)

run_environment_info = target[FrontendInfo].run_environment_info or RunEnvironmentInfo(
run_environment_info = _clean_run_environment_info(
target[FrontendInfo].run_environment_info,
) or RunEnvironmentInfo(
environment = ctx.attr.env,
inherited_environment = ctx.attr.env_inherit,
)
Expand All @@ -35,6 +37,10 @@ def _frontend_impl(ctx):
data_runfiles = data_runfiles,
default_runfiles = default_runfiles,
),
coverage_common.instrumented_files_info(
ctx = ctx,
dependency_attributes = ["exports"],
),
] + [
target[provider]
for provider in target[FrontendInfo].providers
Expand All @@ -43,6 +49,25 @@ def _frontend_impl(ctx):
[run_environment_info] if run_environment_info else []
)

# Workaround for https://github.com/bazelbuild/bazel/pull/20349:
# In Bazel 6.4.0, cc_test manually sets LCOV_MERGER to the lcov_merger executable in its own
# configuration, which is not the configuration of the top-level test wrapping it when that happens
# through a transition. Remove the variable to let TestActionBuilder set it to the correct value
# instead.
# TODO: Remove this workaround eventually.
def _clean_run_environment_info(run_environment_info):
if not run_environment_info or not "LCOV_MERGER" in run_environment_info.environment:
return run_environment_info
cleaned_environment = {
key: value
for key, value in run_environment_info.environment.items()
if key != "LCOV_MERGER"
}
return RunEnvironmentInfo(
environment = cleaned_environment,
inherited_environment = run_environment_info.inherited_environment,
)

_frontend_attrs = {
"env": attr.string_dict(),
"env_inherit": attr.string_list(),
Expand All @@ -55,8 +80,21 @@ _frontend_attrs = {
),
}

_frontend_test_attrs = {
"_lcov_merger": attr.label(
default = configuration_field(fragment = "coverage", name = "output_generator"),
executable = True,
cfg = "exec",
),
"_collect_cc_coverage": attr.label(
default = "@bazel_tools//tools/test:collect_cc_coverage",
executable = True,
cfg = "exec",
),
}

_frontend_executable = rule(_frontend_impl, attrs = _frontend_attrs, executable = True)
_frontend_test = rule(_frontend_impl, attrs = _frontend_attrs, test = True)
_frontend_test = rule(_frontend_impl, attrs = _frontend_attrs | _frontend_test_attrs, test = True)

def _frontend_default(*, name, exports, **kwargs):
native.alias(
Expand Down
1 change: 0 additions & 1 deletion with_cfg/private/rule_defaults.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ DEFAULT_PROVIDERS = [
CcInfo,
CcToolchainConfigInfo,
DebugPackageInfo,
InstrumentedFilesInfo,
JavaInfo,
JavaPluginInfo,
OutputGroupInfo,
Expand Down
4 changes: 4 additions & 0 deletions with_cfg/private/transitioning_alias.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ def _transitioning_alias_base_impl(ctx, *, providers):
data_runfiles = target[DefaultInfo].data_runfiles,
default_runfiles = target[DefaultInfo].default_runfiles,
),
coverage_common.instrumented_files_info(
ctx = ctx,
dependency_attributes = ["exports"],
),
]
if not is_reset_rule:
returned_providers.append(FrontendInfo(
Expand Down

0 comments on commit d9c5efd

Please sign in to comment.