Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Build it locally with `bazel run //docs:docs` (output in `docs/_build/`) or
## Quick start

```bash
bazel coverage --config=llvm_cov //... --build_tests_only
bazel coverage --config=llvm_cov //...
bazel run @score_coverage//:generate_coverage_html -- \
--yaml tools/coverage/coverage_justifications.yaml --archive-dir coverage_artifact
```
Expand Down
7 changes: 5 additions & 2 deletions defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ def score_coverage_reporter(
Args:
name: Target name, referenced by --coverage_report_generator.
coverage_scope: A score_coverage_scope target listing the production
targets that define the coverage scope.
targets that define the coverage scope. It is NOT a dependency of
the reporter (which Bazel analyses in the exec configuration):
build it in the same `bazel coverage` invocation as the tests, so
it is analysed in the tests' configuration.
llvm_cov: Label of the llvm-cov binary (the consumer's LLVM toolchain,
e.g. "@llvm_toolchain//:llvm-cov"). Must come from the same LLVM
major version that produced the coverage instrumentation.
Expand Down Expand Up @@ -115,7 +118,7 @@ def score_coverage_reporter(
_reporter_wrapper(
name = name,
backend = backend,
coverage_scope = coverage_scope,
coverage_scope = str(native.package_relative_label(coverage_scope)),
module_bazel = module_bazel,
llvm_cov = llvm_cov,
llvm_profdata = llvm_profdata,
Expand Down
17 changes: 9 additions & 8 deletions docs/architecture/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,15 @@ targets are excluded, but a workspace target can explicitly declare a header
from an external repository and thereby include it in scope. Merely forwarding
an external library's headers does not include them.

The scope is analysed in Bazel's **exec configuration**: Bazel attaches
``--coverage_report_generator`` to every test with an exec transition, and the
scope is a dependency of that generator. The aspect therefore resolves
``select()`` and toolchains for the host platform unless the scope's
``platform`` attribute names the run's target platform; its transition sets
``--platforms`` next to ``--collect_code_coverage`` for the traversal, the
baseline objects and the gcno notes (ERR-14). The report generator itself
keeps running on the host.
Bazel analyses ``--coverage_report_generator`` in the **exec configuration**:
it attaches the generator to every test with an exec transition. A scope that
the generator depended on would be analysed there too, and its aspect would
resolve ``select()`` and toolchains for the host platform (ERR-14). The scope
is therefore not a dependency of the generator: it is built in the same
``bazel coverage`` invocation as the tests, in their configuration, and the
reporters locate its outputs (allowlist, path map, baseline objects, gcno
notes) in that configuration's ``bin`` directory (``scope_root``). The report
generator itself keeps running on the host.

The scope exports a file allowlist, the source files, a header path map and
backend-specific baseline manifests. The path map translates Bazel-generated
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Quick reference

.. code-block:: bash

bazel coverage --config=llvm_cov //... --build_tests_only
bazel coverage --config=llvm_cov //...
bazel run @score_coverage//:generate_coverage_html -- \
--yaml tools/coverage/coverage_justifications.yaml --archive-dir coverage_artifact

Expand Down
16 changes: 10 additions & 6 deletions docs/manual/constraints.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,16 @@ reachable universe against the scope. Mitigates ERR-01.

.. _cstr_coverage_build_tests_only:

CSTR-03 Run coverage with ``--build_tests_only``
------------------------------------------------

Always invoke ``bazel coverage --config=llvm_cov <targets> --build_tests_only``.
Without the flag Bazel builds manual-tagged or incompatible test binaries, which
fails or pollutes the run. Mitigates ERR-08.
CSTR-03 Build the scope in the coverage invocation, without ``--build_tests_only``
----------------------------------------------------------------------------------

Always build the scope target in the same ``bazel coverage`` invocation as the
tests, for example ``bazel coverage --config=llvm_cov //...`` or
``bazel coverage --config=qnx //score/... //tools/coverage:coverage_scope``.
Do not pass ``--build_tests_only``: it skips the scope, and the reporters would
silently read stale outputs of an earlier build. Select the tests with
patterns that do not match manual-tagged or incompatible test binaries.
Mitigates ERR-08 and ERR-14.

.. _cstr_coverage_toolchain_config:

Expand Down
25 changes: 11 additions & 14 deletions docs/manual/known_problems.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,22 @@ stay listed with their upstream references.
- Exclude containerised or system tests from the coverage run; they keep
running in the regular test jobs.
* - **A QNX report lists Linux variants at 0 % and misses the QNX variants.**
The scope was declared without ``platform``: Bazel analyses it in the
exec configuration and every ``select()`` on the platform resolves for
the host.
The scope was analysed in the exec configuration (for example as a
dependency of the reporter, or built without the tests' platform):
every ``select()`` on the platform resolves for the host.
- Files such as ``score/os/linux/*`` at 0 % and no ``score/os/qnx/*`` in
the QNX report; the allowlist of the QNX run equals the Linux run's.
- Declare a scope per platform with ``platform = <the run's
--platforms label>`` (user manual, step 3) and point the gcov reporter
at it.
- Build the scope in the same ``bazel coverage`` invocation as the
tests, not as a dependency of the reporter (user manual, step 3).
* - **A coverage run executes no test and writes no report.** A root of
the scope is incompatible with the scope's platform (on QNX typically
the scope is incompatible with the run's platform (on QNX typically
a Rust root: crate_universe marks crates incompatible with platforms
outside rules_rust's triple list). The scope becomes incompatible,
the report generator with it, and every test, which depends on the
generator, is skipped instead of failing.
- ``Executed 0 out of N tests: N were skipped`` under
``bazel coverage``; ``generate_coverage_html`` exits 2 with
``Coverage report not found``.
outside rules_rust's triple list). The scope becomes incompatible
and is not built, so no scope outputs exist.
- Bazel reports the scope as incompatible, or skips it;
``generate_coverage_html`` fails because the scope outputs are missing.
- ``bazel build --config=<QNX config> --collect_code_coverage
//tools/coverage:coverage_scope_qnx`` prints the dependency chain to
//tools/coverage:coverage_scope`` prints the dependency chain to
the offending constraint; put that root behind a ``select()``.
* - **QNX on-target coverage covers C++ only.** rustc emits no gcov
counters, and the LLVM profile transport from the QEMU guest is not
Expand Down
91 changes: 25 additions & 66 deletions docs/manual/user_manual.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,70 +161,29 @@ library must be added, and exclusions need a written decision.

.. _scope_platform:

**The scope is evaluated for a platform.** Bazel analyses the coverage report
generator, and with it the scope, in the exec configuration, that is for the
host platform, whatever ``--platforms`` the coverage run uses. On the Linux
run host and target coincide. For a run that targets another platform the
scope must say so, otherwise every ``select()`` on the platform resolves for
the host: the Linux variants of platform-specific code appear at 0 % and the
target's variants are missing. Declare one scope per platform:

.. code-block:: starlark

score_coverage_scope(
name = "coverage_scope_qnx",
testonly = True,
platform = "@score_bazel_platforms//:x86_64-qnx-sdp_8.0.0-posix",
tags = ["manual"], # see below
deps = SCOPE_DEPS,
)

``platform`` is the label the run passes as ``--platforms``. Roots that exist
on one platform only must sit behind a ``select()`` in ``deps``; that
``select()`` is resolved for ``platform`` too, because the transition applies
to the scope rule itself, not only to its dependencies. A root that
is incompatible with the platform makes the scope incompatible, and that
does **not** fail the coverage run: every test depends on the report
generator and inherits the incompatibility, so Bazel skips all tests
(``Executed 0 out of N tests: N were skipped``) and no report is written.
Build the scope explicitly to get the dependency chain to the offending
constraint:
**The scope is built with the tests.** Bazel analyses the coverage report
generator in the exec configuration, that is for the host platform, whatever
``--platforms`` the coverage run uses. A scope that the generator depended on
would be analysed there too, and every ``select()`` on the platform would
resolve for the host: on a QNX run the Linux variants of platform-specific
code would appear at 0 % and the QNX variants would be missing. The scope is
therefore **not** a dependency of the reporter. Build it in the same
``bazel coverage`` invocation as the tests, so that it is analysed in their
configuration, and pass its label to ``score_coverage_reporter``; the reporter
finds the outputs there:

.. code-block:: shell

bazel build --config=<your QNX build config> --collect_code_coverage //tools/coverage:coverage_scope_qnx

On a Linux host without the QNX SDP the same analysis runs in seconds with
a stand-in: lend the host's GCC ``cc_toolchain`` to the QNX platform in a
scratch package (not committed) and pass it together with the QNX Rust
toolchain. Incompatibility and visibility are decided by constraints and
labels, not by the compiler, so the chain Bazel prints is the real one.

.. code-block:: starlark

toolchain(
name = "fake_qnx_cc",
target_compatible_with = ["@platforms//cpu:x86_64", "@platforms//os:qnx"],
toolchain = "@score_gcc_x86_64_toolchain//:cc_toolchain",
toolchain_type = "@bazel_tools//tools/cpp:toolchain_type",
)

.. code-block:: shell
bazel coverage --config=qnx //score/... //tools/coverage:coverage_scope

bazel build --nobuild --collect_code_coverage \
--platforms=@score_bazel_platforms//:x86_64-qnx-sdp_8.0.0-posix \
--extra_toolchains=//scratch:fake_qnx_cc \
--extra_toolchains=@score_toolchains_rust//toolchains/ferrocene:ferrocene_x86_64_pc_nto_qnx800 \
//tools/coverage:coverage_scope_qnx

Rust roots are the usual culprits on QNX: crate_universe marks crates
incompatible with platforms outside rules_rust's triple list, and the gcov
backend cannot measure Rust anyway, so keep Rust roots out of a QNX scope.
Tag such a scope ``manual``, like the gcov reporter: a wildcard ``bazel build //...``
on a Linux host would otherwise analyse it for the other platform in a
configuration where that platform's toolchains are not registered and fail
toolchain resolution. The coverage run names the reporter, and through it
the scope, explicitly, so the tag does not affect it.
On the Linux run ``//...`` already contains the scope. Do not pass
``--build_tests_only``: it skips the scope and the report would use stale
outputs of an earlier build. Roots that exist on one platform only must sit
behind a ``select()`` in ``deps``; it is resolved for the platform of the run.
A root that is incompatible with that platform makes the scope incompatible
and Bazel skips it; keep Rust roots out of a QNX scope, as the gcov backend
cannot measure Rust anyway, and use the platform-specific root behind a
``select()``.

Binaries in the scope
~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -313,7 +272,7 @@ the toolchain extension):
name = "gcov_reporter_wrapper",
testonly = True,
backend = "gcov",
coverage_scope = ":coverage_scope_qnx", # platform = the QNX platform, see step 3
coverage_scope = ":coverage_scope", # built with the tests, see step 3
gcov = "@score_qcc_x86_64_toolchain_pkg//:gcov",
tags = ["manual"], # keeps `bazel build //...` on a Linux host from fetching the QNX SDP
)
Expand Down Expand Up @@ -361,7 +320,7 @@ Run and report as on Linux, with the platform filter for justifications:

.. code-block:: shell

bazel coverage --config=qnx //score/... --build_tests_only
bazel coverage --config=qnx //score/... //tools/coverage:coverage_scope
bazel run @score_coverage//:generate_coverage_html -- --platform qnx \
--yaml tools/coverage/coverage_justifications.yaml --archive-dir coverage_qnx_artifacts

Expand Down Expand Up @@ -412,7 +371,7 @@ Step 6: run it

.. code-block:: bash

bazel coverage --config=llvm_cov //... --build_tests_only
bazel coverage --config=llvm_cov //...

bazel run @score_coverage//:generate_coverage_html -- \
--yaml tools/coverage/coverage_justifications.yaml
Expand All @@ -424,9 +383,9 @@ Step 6: run it

``--yaml`` is optional: without it, justification processing is skipped and the
gate applies to the **raw** line coverage computed from the LCOV data.
``--build_tests_only`` is mandatory: without it, coverage builds every target
matched by the pattern, including manual-tagged or platform-incompatible test
binaries.
Do not pass ``--build_tests_only``: the scope must be built by the run (see
step 3). Select targets with patterns that exclude manual-tagged or
platform-incompatible test binaries.

Inside GitHub Actions a markdown summary is appended to ``GITHUB_STEP_SUMMARY``
automatically when ``--summary-md`` is absent. The summary is written before the
Expand Down
4 changes: 2 additions & 2 deletions docs/requirements/potential_errors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ requirements and constraints of use that mitigate the error.
- good
- :need:`tool_req__coverage_scope_platform`,
:need:`tool_req__coverage_validation_ground_truth`,
user manual step 3 (one scope per platform)
user manual step 3 (build the scope with the tests)

Classification
--------------
Expand All @@ -171,7 +171,7 @@ Tool error detection before qualification: **no** for ERR-02, ERR-03 and
ERR-07; **weak** for ERR-11 and ERR-12 (a lost per-test record on QNX is
only noticed by comparing against the Linux report); ERR-13 under-reports and is
detected by the reporters' warning and the integration test; ERR-14 is
prevented by declaring the platform on the scope and verified by analysis
prevented by building the scope in the configuration of the tests and verified by analysis
and integration tests. The expected tool confidence level is therefore **TCL LOW**, and the
qualification method of the S-CORE process, validation of the software tool,
applies. The evaluation itself is recorded in the Tool Verification Report.
15 changes: 8 additions & 7 deletions docs/requirements/tool_requirements.rst
Original file line number Diff line number Diff line change
Expand Up @@ -607,13 +607,14 @@ Summary and archive
:safety: ASIL_B
:satisfies: stkh_req__coverage__uc_scope_completeness

``score_coverage_scope`` shall accept a ``platform`` label and shall
evaluate its dependency traversal, baseline objects and gcno notes for
that platform (``--platforms``) although Bazel analyses the scope in the
exec configuration: a source selected by a ``select()`` on that platform
shall be in the allowlist and the host's alternative shall not. Without
``platform`` the scope shall be evaluated for the platform of the exec
configuration.
``score_coverage_scope`` shall be an ordinary target that is analysed in
the configuration of the tests it is built with, and the reporters shall
read its outputs from that configuration, so that its dependency
traversal, baseline objects and gcno notes are those of the run's
``--platforms``: a source selected by a ``select()`` on that platform
shall be in the allowlist and the host's alternative shall not. The scope
shall not be a dependency of the report generator, which Bazel analyses in
the exec configuration.

Validation
----------
Expand Down
46 changes: 9 additions & 37 deletions score_coverage/coverage_scope.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def _coverage_scope_impl(ctx):
)

return [
DefaultInfo(files = depset([output, map_output, objects_output, gcno_output], transitive = [object_depset])),
DefaultInfo(files = depset([output, map_output, objects_output, gcno_output], transitive = [object_depset, gcno_depset, source_depset])),
OutputGroupInfo(
allowlist = depset([output]),
path_map = depset([map_output]),
Expand All @@ -289,24 +289,8 @@ def _coverage_scope_impl(ctx):
]

def _coverage_transition_impl(settings, attr):
"""Build the scope's deps with coverage on, and for the declared target platform.

Bazel analyses ``--coverage_report_generator`` (and therefore the scope
it depends on) in the *exec* configuration, i.e. for the host platform.
Without ``platform`` every ``select()`` on the platform resolves for the
host, so a QNX run would list the Linux variants of platform-specific
code and miss the QNX variants (coverage_tool#23). ``platform`` pins the
configuration the scope is evaluated in.

The transition is applied to the rule itself (incoming edge), not only
to ``deps``: a ``select()`` in the ``deps`` attribute is resolved in the
configuration of the rule that owns the attribute, so a per-platform
root list would otherwise resolve for the host while the roots
themselves are built for the target platform.
"""
"""Build the wrapped target with coverage on, keeping the invocation's platforms."""
platforms = settings["//command_line_option:platforms"]
if attr.platform:
platforms = [str(attr.platform)]
return {
"//command_line_option:collect_code_coverage": True,
"//command_line_option:platforms": platforms,
Expand Down Expand Up @@ -357,30 +341,18 @@ coverage_scope = rule(
files, reports headers behind include prefixes under their declared path,
and stages the sources so every HTML page can be rendered.

``platform`` names the target platform the scope is evaluated for. Bazel
analyses the coverage report generator, and with it this rule, in the
exec configuration; without ``platform`` every ``select()`` on the
platform resolves for the host. A scope for a QNX run therefore sets
``platform`` to the QNX platform label, so that platform-specific
sources, baseline objects and gcno notes are those of QNX. A root that
is incompatible with the platform fails the analysis: platform-only
roots belong behind a ``select()`` in ``deps``.
The scope is an ordinary target: build it in the same invocation, and
therefore the same configuration, as the tests (``bazel coverage
//tests/... //tools/coverage:coverage_scope``). Do not make it a dependency
of the report generator: that is analysed in the exec configuration, where
every ``select()`` on the platform resolves for the host. The reporter
finds the outputs in the configuration of the tests' coverage data.
""",
cfg = coverage_transition,
attrs = {
"deps": attr.label_list(
mandatory = True,
aspects = [_coverage_scope_aspect],
doc = "Implementation targets whose transitive deps define the coverage scope. " +
"A select() here is resolved for ``platform`` as well.",
),
"platform": attr.label(
default = None,
doc = "Target platform the scope is evaluated for (``--platforms`` of the coverage run). " +
"Unset: the platform of the exec configuration, i.e. the host.",
),
"_allowlist_function_transition": attr.label(
default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
doc = "Implementation targets whose transitive deps define the coverage scope.",
),
},
)
Loading
Loading