Skip to content

Commit

Permalink
Refactoring the extld related logic (bazelbuild#3400)
Browse files Browse the repository at this point in the history
  • Loading branch information
linzhp authored and healthy-pod committed Feb 17, 2023
1 parent 06ed447 commit a2771ab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
47 changes: 24 additions & 23 deletions go/private/actions/link.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -89,35 +89,36 @@ def emit_link(
tool_args = go.tool_args(go)

# Add in any mode specific behaviours
extld = extld_from_cc_toolchain(go)
tool_args.add_all(extld)
if go.mode.race:
tool_args.add("-race")
if go.mode.msan:
tool_args.add("-msan")
if ((go.mode.static and not go.mode.pure) or
(go.mode.race and extld) or
go.mode.link != LINKMODE_NORMAL or
go.mode.goos == "windows" and (go.mode.race or go.mode.msan)):
# Force external linking for the following conditions:
# * Mode is static but not pure: -static must be passed to the C
# linker if the binary contains cgo code. See #2168, #2216.
# * Non-normal build mode: may not be strictly necessary, especially
# for modes like "pie".
# * Race or msan build for Windows: Go linker has pairwise
# incompatibilities with mingw, and we get link errors in race mode.
# Using the C linker avoids that. Race and msan always require a
# a C toolchain. See #2614.
# * Linux race builds: we get linker errors during build with Go's
# internal linker. For example, when using zig cc v0.10
# (clang-15.0.3):
#
# runtime/cgo(.text): relocation target memset not defined
tool_args.add("-linkmode", "external")

if go.mode.pure:
# Force internal linking in pure mode. We don't have a C toolchain,
# so external linking is not possible.
tool_args.add("-linkmode", "internal")
else:
extld = extld_from_cc_toolchain(go)
tool_args.add_all(extld)
if extld and (go.mode.static or
go.mode.race or
go.mode.link != LINKMODE_NORMAL or
go.mode.goos == "windows" and go.mode.msan):
# Force external linking for the following conditions:
# * Mode is static but not pure: -static must be passed to the C
# linker if the binary contains cgo code. See #2168, #2216.
# * Non-normal build mode: may not be strictly necessary, especially
# for modes like "pie".
# * Race or msan build for Windows: Go linker has pairwise
# incompatibilities with mingw, and we get link errors in race mode.
# Using the C linker avoids that. Race and msan always require a
# a C toolchain. See #2614.
# * Linux race builds: we get linker errors during build with Go's
# internal linker. For example, when using zig cc v0.10
# (clang-15.0.3):
#
# runtime/cgo(.text): relocation target memset not defined
tool_args.add("-linkmode", "external")

if go.mode.static:
extldflags.append("-static")
if go.mode.link != LINKMODE_NORMAL:
Expand Down
2 changes: 1 addition & 1 deletion go/private/context.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ def go_context(ctx, attr = None):
# See https://github.com/golang/go/wiki/MinimumRequirements#amd64
if mode.amd64:
env["GOAMD64"] = mode.amd64
if mode.pure:
if not cgo_context_info:
crosstool = []
cgo_tools = None
else:
Expand Down

0 comments on commit a2771ab

Please sign in to comment.