Skip to content

Commit

Permalink
Improve error when linkmode requires cgo (#3482)
Browse files Browse the repository at this point in the history
Previously this would fail only during linking with an error such as:
```
external/go_sdk/pkg/tool/linux_amd64/link: internal linking requested but external linking required: buildmode=shared
```
  • Loading branch information
fmeum authored Mar 17, 2023
1 parent 190f72c commit 1c23302
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions go/private/mode.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ LINKMODES = [LINKMODE_NORMAL, LINKMODE_PLUGIN, LINKMODE_C_SHARED, LINKMODE_C_ARC
# All link modes that produce executables to be run with bazel run.
LINKMODES_EXECUTABLE = [LINKMODE_NORMAL, LINKMODE_PIE]

# All link modes that require external linking and thus a cgo context.
LINKMODES_REQUIRING_EXTERNAL_LINKING = [
LINKMODE_PLUGIN,
LINKMODE_C_ARCHIVE,
LINKMODE_C_SHARED,
]

def mode_string(mode):
result = [mode.goos, mode.goarch]
if mode.static:
Expand Down Expand Up @@ -92,6 +99,9 @@ def get_mode(ctx, go_toolchain, cgo_context_info, go_config_info):
fail("race instrumentation can't be enabled when cgo is disabled. Check that pure is not set to \"off\" and a C/C++ toolchain is configured.")
if pure and msan:
fail("msan instrumentation can't be enabled when cgo is disabled. Check that pure is not set to \"off\" and a C/C++ toolchain is configured.")
if pure and linkmode in LINKMODES_REQUIRING_EXTERNAL_LINKING:
fail(("linkmode '{}' can't be used when cgo is disabled. Check that pure is not set to \"off\" and that a C/C++ toolchain is configured for " +
"your current platform. If you defined a custom platform, make sure that it has the @io_bazel_rules_go//go/toolchain:cgo_on constraint value.").format(linkmode))

gc_linkopts = list(go_config_info.gc_linkopts) if go_config_info else []
tags = list(go_config_info.tags) if go_config_info else []
Expand Down

0 comments on commit 1c23302

Please sign in to comment.