[bazel] Refactor settings for on-target coverage measurement
Signed-off-by: Alphan Ulusoy <alphan@google.com>
diff --git a/.bazelrc b/.bazelrc
index 4d0116e..b39b00a 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -26,20 +26,25 @@
# --config=riscv32
build:riscv32 --platforms=@crt//platforms/riscv32:opentitan
-# Configuration for clang's source-based coverage instrumentation. Enable with
-# `--config=coverage_clang`. Bazel seems to support this only partially, thus we have to
-# perform some additional processing. See
+# Shared configuration for clang's source-based coverage instrumentation.
+# Bazel seems to support this only partially, thus we have to perform some
+# additional processing. See
# https://github.com/bazelbuild/bazel/commit/21b5eb627d78c09d47c4de957e9e6b56a9ae6fad
# and `util/coverage/coverage_off_target.py`.
-build:coverage_clang --repo_env=CC=clang
-build:coverage_clang --repo_env=BAZEL_USE_LLVM_NATIVE_COVERAGE=1
-build:coverage_clang --java_runtime_version=remotejdk_11
-build:coverage_clang --collect_code_coverage
-coverage:coverage_clang --repo_env=GCOV=/usr/bin/llvm-profdata
-coverage:coverage_clang --repo_env=BAZEL_LLVM_COV=/usr/bin/llvm-cov
-# Docs state that bazel will fail to create coverage information if tests have been
-# cached previously. See https://bazel.build/configure/coverage?hl=en#remote-execution
-coverage:coverage_clang --nocache_test_results
+build:ot_coverage --repo_env='CC=clang'
+build:ot_coverage --repo_env='BAZEL_USE_LLVM_NATIVE_COVERAGE=1'
+build:ot_coverage --java_runtime_version='remotejdk_11'
+# Docs state that bazel will fail to create coverage information if tests have
+# been cached previously. See
+# https://bazel.build/configure/coverage?hl=en#remote-execution
+coverage:ot_coverage --nocache_test_results
+
+# Configuration for measuring off-target coverage. Enable with
+# `--config=ot_coverage_off_target`.
+build:ot_coverage_off_target --config='ot_coverage'
+build:ot_coverage_off_target --collect_code_coverage
+coverage:ot_coverage_off_target --repo_env='GCOV=/usr/bin/llvm-profdata'
+coverage:ot_coverage_off_target --repo_env='BAZEL_LLVM_COV=/usr/bin/llvm-cov'
# Configuration to override resource constrained test
# scheduling. Enable with `--config=local_test_jobs_per_cpus`
diff --git a/util/coverage/coverage_off_target.py b/util/coverage/coverage_off_target.py
index aba3cfb..b660d34 100755
--- a/util/coverage/coverage_off_target.py
+++ b/util/coverage/coverage_off_target.py
@@ -104,7 +104,7 @@
log.info(f"incompatible libraries: {pformat(device_libs_incompat)}")
device_libs = list(set(device_libs_all) - set(device_libs_incompat))
log.info(f"instrumented libraries: {pformat(device_libs)}")
- run(BAZEL, "build", "--config=coverage_clang", *device_libs)
+ run(BAZEL, "build", "--config=ot_coverage_off_target", *device_libs)
return device_libs
@@ -162,7 +162,7 @@
# Gather and run all test targets.
test_targets = run(BAZEL, "query", TEST_TARGETS_QUERY)
log.info(f"test targets: {pformat(test_targets)}")
- run(BAZEL, "coverage", "--config=coverage_clang", *test_targets)
+ run(BAZEL, "coverage", "--config=ot_coverage_off_target", *test_targets)
# Merge profile data of individual tests.
[TESTLOGS] = run(BAZEL, "info", "bazel-testlogs")
profile_files = [
diff --git a/util/coverage/coverage_off_target_test.py b/util/coverage/coverage_off_target_test.py
index 703f6bd..5cf35e7 100755
--- a/util/coverage/coverage_off_target_test.py
+++ b/util/coverage/coverage_off_target_test.py
@@ -75,7 +75,7 @@
self.assertEqual(calls[0][0][:2], (coverage_off_target.BAZEL, "query"))
self.assertEqual(calls[1][0][:2], (coverage_off_target.BAZEL, "cquery"))
self.assertEqual(calls[2][0], (coverage_off_target.BAZEL, "build",
- "--config=coverage_clang", *libs))
+ "--config=ot_coverage_off_target", *libs))
@patch("coverage_off_target.run")
def test_create_merged_library(self, mock_run):
@@ -115,7 +115,7 @@
self.assertEqual(len(calls), 4)
self.assertEqual(calls[0][0][:2], (coverage_off_target.BAZEL, "query"))
self.assertEqual(calls[1][0], (coverage_off_target.BAZEL, "coverage",
- "--config=coverage_clang", *tests))
+ "--config=ot_coverage_off_target", *tests))
self.assertEqual(calls[2][0],
(coverage_off_target.BAZEL, "info", "bazel-testlogs"))
self.assertEqual(