From 8fe03fc4d1d1236bbfbf81f0fe3140ace4e7a900 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Fri, 7 Apr 2023 19:00:05 +0200 Subject: [PATCH] Drop coverage linker flags from stdlib build (#3522) We never compile CGo with coverage instrumentation and the stdlib contains no user-provided C/C++ code, so linking coverage runtimes is never needed. --- go/private/actions/stdlib.bzl | 8 +++++++- go/private/common.bzl | 9 +++++++++ go/private/context.bzl | 7 +++++-- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/go/private/actions/stdlib.bzl b/go/private/actions/stdlib.bzl index f20bfd5ac6..a8097e1697 100644 --- a/go/private/actions/stdlib.bzl +++ b/go/private/actions/stdlib.bzl @@ -12,6 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +load( + "//go/private:common.bzl", + "COVERAGE_OPTIONS_DENYLIST", +) load( "//go/private:providers.bzl", "GoStdLib", @@ -98,10 +102,12 @@ def _build_stdlib(go): else: # NOTE(#2545): avoid unnecessary dynamic link # go std library doesn't use C++, so should not have -lstdc++ + # Also drop coverage flags as nothing in the stdlib is compiled with + # coverage - we disable it for all CGo code anyway. ldflags = [ option for option in extldflags_from_cc_toolchain(go) - if option not in ("-lstdc++", "-lc++") + if option not in ("-lstdc++", "-lc++") and option not in COVERAGE_OPTIONS_DENYLIST ] env.update({ "CGO_ENABLED": "1", diff --git a/go/private/common.bzl b/go/private/common.bzl index e47ebd18c2..5d331b72c6 100644 --- a/go/private/common.bzl +++ b/go/private/common.bzl @@ -243,3 +243,12 @@ def count_group_matches(v, prefix, suffix): count = count + 1 return count + +# C/C++ compiler and linker options related to coverage instrumentation. +COVERAGE_OPTIONS_DENYLIST = { + "--coverage": None, + "-ftest-coverage": None, + "-fprofile-arcs": None, + "-fprofile-instr-generate": None, + "-fcoverage-mapping": None, +} diff --git a/go/private/context.bzl b/go/private/context.bzl index 1451895a41..65f73b5767 100644 --- a/go/private/context.bzl +++ b/go/private/context.bzl @@ -51,6 +51,7 @@ load( ) load( ":common.bzl", + "COVERAGE_OPTIONS_DENYLIST", "as_iterable", "goos_to_extension", "goos_to_shared_extension", @@ -86,7 +87,7 @@ _UNSUPPORTED_C_COMPILERS = { "clang-cl": None, } -_COMPILER_OPTIONS_DENYLIST = { +_COMPILER_OPTIONS_DENYLIST = dict({ # cgo parses the error messages from the compiler. It can't handle colors. # Ignore both variants of the diagnostics color flag. "-fcolor-diagnostics": None, @@ -105,7 +106,9 @@ _COMPILER_OPTIONS_DENYLIST = { "--coverage": None, "-ftest-coverage": None, "-fprofile-arcs": None, -} + "-fprofile-instr-generate": None, + "-fcoverage-mapping": None, +}, **COVERAGE_OPTIONS_DENYLIST) _LINKER_OPTIONS_DENYLIST = { "-Wl,--gc-sections": None,