[bazel] Generate HTML coverage reports
1. Add `lcov` to `apt-requirements.txt` to generate HTML coverage reports.
2. Set up the coverage options in `.bazelrc`.
3. Write a script to generate coverage and emit an HTML report.
This PR does not add coverage support for on-chip tests; that will be added
in a follow-up PR.
Signed-off-by: Chris Frantz <cfrantz@google.com>
diff --git a/rules/quality.bzl b/rules/quality.bzl
index 9bb5b83..b52d43c 100644
--- a/rules/quality.bzl
+++ b/rules/quality.bzl
@@ -111,3 +111,29 @@
},
executable = True,
)
+
+def _html_coverage_report_impl(ctx):
+ out_file = ctx.actions.declare_file(ctx.label.name + ".bash")
+ substitutions = {}
+ ctx.actions.expand_template(
+ template = ctx.file._runner,
+ output = out_file,
+ substitutions = substitutions,
+ is_executable = True,
+ )
+
+ return DefaultInfo(
+ files = depset([out_file]),
+ executable = out_file,
+ )
+
+html_coverage_report = rule(
+ implementation = _html_coverage_report_impl,
+ attrs = {
+ "_runner": attr.label(
+ default = "//rules/scripts:html_coverage_report.template.sh",
+ allow_single_file = True,
+ ),
+ },
+ executable = True,
+)
diff --git a/rules/scripts/html_coverage_report.template.sh b/rules/scripts/html_coverage_report.template.sh
new file mode 100644
index 0000000..880abcc
--- /dev/null
+++ b/rules/scripts/html_coverage_report.template.sh
@@ -0,0 +1,22 @@
+#!/usr/bin/env bash
+#
+# Copyright lowRISC contributors.
+# Licensed under the Apache License, Version 2.0, see LICENSE for details.
+# SPDX-License-Identifier: Apache-2.0
+
+set -e
+
+if [[ $# == 0 ]]; then
+ ARGS=("//...")
+else
+ ARGS="$@"
+fi
+
+if ! cd "$BUILD_WORKSPACE_DIRECTORY"; then
+ echo "Unable to change to workspace (BUILD_WORKSPACE_DIRECTORY: ${BUILD_WORKSPACE_DIRECTORY})"
+ exit 1
+fi
+
+bazel coverage "${ARGS[@]}"
+genhtml -o bazel-out/_coverage/ bazel-out/_coverage/_coverage_report.dat
+echo "Coverage report: file://$(pwd)/bazel-out/_coverage/index.html"