From 56909379cbdfbd3a3da60b999cb037184a9a1e73 Mon Sep 17 00:00:00 2001 From: Zhongpeng Lin Date: Mon, 27 Feb 2023 20:54:55 -0800 Subject: [PATCH 1/2] disable coverage when a C library is not instrumented --- foreign_cc/private/cc_toolchain_util.bzl | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/foreign_cc/private/cc_toolchain_util.bzl b/foreign_cc/private/cc_toolchain_util.bzl index 27fc97b1d..959b6188d 100644 --- a/foreign_cc/private/cc_toolchain_util.bzl +++ b/foreign_cc/private/cc_toolchain_util.bzl @@ -49,11 +49,23 @@ FOREIGN_CC_DISABLED_FEATURES = [ ] def _configure_features(ctx, cc_toolchain): + disabled_features = ctx.disabled_features + FOREIGN_CC_DISABLED_FEATURES + instrumented_files_info = coverage_common.instrumented_files_info( + ctx, + source_attributes = ["lib_source"], + dependency_attributes = ["deps"], + ) + if not instrumented_files_info.instrumented_files: + # cc_common.configure_features() adds coverage related flags, such as --coverage + # to the compiler and linker. However, if there is nothing in this library or its + # deps that are instrumented, we don't need to pass those flags, and avoid unncessary + # rebuilds. + disabled_features.append("coverage") return cc_common.configure_features( ctx = ctx, cc_toolchain = cc_toolchain, requested_features = ctx.features, - unsupported_features = ctx.disabled_features + FOREIGN_CC_DISABLED_FEATURES, + unsupported_features = disabled_features, ) def _create_libraries_to_link(ctx, files): From e716af3ae9c9476aa154c15d98faabea3eec32ee Mon Sep 17 00:00:00 2001 From: Zhongpeng Lin Date: Mon, 27 Feb 2023 21:57:29 -0800 Subject: [PATCH 2/2] simplify --- foreign_cc/private/cc_toolchain_util.bzl | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/foreign_cc/private/cc_toolchain_util.bzl b/foreign_cc/private/cc_toolchain_util.bzl index 959b6188d..1bd872dd9 100644 --- a/foreign_cc/private/cc_toolchain_util.bzl +++ b/foreign_cc/private/cc_toolchain_util.bzl @@ -50,16 +50,10 @@ FOREIGN_CC_DISABLED_FEATURES = [ def _configure_features(ctx, cc_toolchain): disabled_features = ctx.disabled_features + FOREIGN_CC_DISABLED_FEATURES - instrumented_files_info = coverage_common.instrumented_files_info( - ctx, - source_attributes = ["lib_source"], - dependency_attributes = ["deps"], - ) - if not instrumented_files_info.instrumented_files: - # cc_common.configure_features() adds coverage related flags, such as --coverage - # to the compiler and linker. However, if there is nothing in this library or its - # deps that are instrumented, we don't need to pass those flags, and avoid unncessary - # rebuilds. + if not ctx.coverage_instrumented(): + # In coverage mode, cc_common.configure_features() adds coverage related flags, + # such as --coverage to the compiler and linker. However, if this library is not + # instrumented, we don't need to pass those flags, and avoid unncessary rebuilds. disabled_features.append("coverage") return cc_common.configure_features( ctx = ctx,