Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup compilepkg flags handling #4043

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions go/private/actions/compilepkg.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
load("//go/private:common.bzl", "GO_TOOLCHAIN_LABEL", "SUPPORTS_PATH_MAPPING_REQUIREMENT")
load(
"//go/private:mode.bzl",
"link_mode_args",
"link_mode_arg",
)
load("//go/private/actions:utils.bzl", "quote_opts")

Expand Down Expand Up @@ -143,20 +143,22 @@ def emit_compilepkg(
if testfilter:
args.add("-testfilter", testfilter)

gc_flags = list(gc_goopts)
gc_flags.extend(go.mode.gc_goopts)
asm_flags = []
link_mode_flag = link_mode_arg(go.mode)

gc_flags = gc_goopts + go.mode.gc_goopts
if go.mode.race:
gc_flags.append("-race")
if go.mode.msan:
gc_flags.append("-msan")
if go.mode.debug:
gc_flags.extend(["-N", "-l"])
gc_flags.extend(go.toolchain.flags.compile)
gc_flags.extend(link_mode_args(go.mode))
asm_flags.extend(link_mode_args(go.mode))
if link_mode_flag:
gc_flags.append(link_mode_flag)
args.add("-gcflags", quote_opts(gc_flags))
args.add("-asmflags", quote_opts(asm_flags))

if link_mode_flag:
args.add("-asmflags", link_mode_flag)

# cgo and the linker action don't support path mapping yet
# TODO: Remove the second condition after https://github.com/bazelbuild/bazel/pull/21921.
Expand Down
7 changes: 5 additions & 2 deletions go/private/actions/stdlib.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ load(
"//go/private:mode.bzl",
"LINKMODE_NORMAL",
"extldflags_from_cc_toolchain",
"link_mode_args",
"link_mode_arg",
)
load(
"//go/private:providers.bzl",
Expand Down Expand Up @@ -133,7 +133,10 @@ def _build_stdlib(go):
args.add("-package", "std")
if not go.mode.pure:
args.add("-package", "runtime/cgo")
args.add_all(link_mode_args(go.mode))

link_mode_flag = link_mode_arg(go.mode)
if link_mode_flag:
args.add(link_mode_flag)
fmeum marked this conversation as resolved.
Show resolved Hide resolved

args.add("-gcflags", quote_opts(go.mode.gc_goopts))

Expand Down
13 changes: 6 additions & 7 deletions go/private/mode.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -211,24 +211,23 @@ _LINK_PIE_PLATFORMS = {
"freebsd/amd64": None,
}

def link_mode_args(mode):
def link_mode_arg(mode):
# based on buildModeInit in cmd/go/internal/work/init.go
platform = mode.goos + "/" + mode.goarch
args = []
if mode.link == LINKMODE_C_ARCHIVE:
if (platform in _LINK_C_ARCHIVE_PLATFORMS or
mode.goos in _LINK_C_ARCHIVE_GOOS and platform != "linux/ppc64"):
args.append("-shared")
return "-shared"
elif mode.link == LINKMODE_C_SHARED:
if mode.goos in _LINK_C_SHARED_GOOS:
args.append("-shared")
return "-shared"
elif mode.link == LINKMODE_PLUGIN:
if platform in _LINK_PLUGIN_PLATFORMS:
args.append("-dynlink")
return "-dynlink"
elif mode.link == LINKMODE_PIE:
if platform in _LINK_PIE_PLATFORMS:
args.append("-shared")
return args
return "-shared"
return None

def extldflags_from_cc_toolchain(go):
if not go.cgo_tools:
Expand Down