Skip to content

Commit

Permalink
Drop coverage linker flags from stdlib build (bazel-contrib#3522)
Browse files Browse the repository at this point in the history
We never compile CGo with coverage instrumentation and the stdlib contains no user-provided C/C++ code, so linking coverage runtimes is never needed.
  • Loading branch information
fmeum authored Apr 7, 2023
1 parent 6e8e7ec commit 3b491f1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
8 changes: 7 additions & 1 deletion go/private/actions/stdlib.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions go/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
7 changes: 5 additions & 2 deletions go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ load(
)
load(
":common.bzl",
"COVERAGE_OPTIONS_DENYLIST",
"as_iterable",
"goos_to_extension",
"goos_to_shared_extension",
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 3b491f1

Please sign in to comment.