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

Support setting GOAMD64 #111

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion .plzconfig
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Help = Changes the test working directory to be the package to be more inline wi
[PluginConfig "cpp_coverage"]
Type = bool
DefaultValue = false
Help = Whether to build C components with coverage
Help = Whether to build C components with coverage

[PluginConfig "c_flags"]
Inherit = true
Expand Down Expand Up @@ -132,5 +132,10 @@ DefaultValue = false
Optional = true
Help = Compile for the Go race detector

[PluginConfig "goamd64"]
DefaultValue = "v1"
Inherit = true
Help = Value to set for GOAMD64, determining what era of CPU to target.

[Plugin "shell"]
Target = //plugins:shell
16 changes: 12 additions & 4 deletions build_defs/go.build_defs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def _go_toolchain(name:str, srcs:list, copy_cmd:str, visibility:list, architectu
"CC": [CONFIG.GO.CC_TOOL],
} if CONFIG.GO.CGO_ENABLED == "1" else {},
binary = True,
env = {'GOAMD64': CONFIG.GO.GOAMD64},
visibility = visibility,
building_description = "Installing...",
)
Expand Down Expand Up @@ -192,6 +193,7 @@ def go_stdlib(name:str, tags:list=[], visibility:list=["PUBLIC"]):
"GOARCH": CONFIG.ARCH,
"GODEBUG": "installgoroot=all",
"CGO_ENABLED": CONFIG.GO.CGO_ENABLED,
"GOAMD64": CONFIG.GO.GOAMD64,
},
labels = ["go_pkg_info", "go_stdlib", "go"],
output_is_complete = True,
Expand Down Expand Up @@ -446,6 +448,7 @@ def go_library(name:str, srcs:list, resources:list=[], asm_srcs:list=None, hdrs:
requires = ['go'],
provides = provides,
labels = labels+link_labels + [f"go_package:{package_path}"] if _generate_import_config else [],
env = {"GOAMD64": CONFIG.GO.GOAMD64},
test_only = test_only,
tools = tools,
needs_transitive_deps = _needs_transitive_deps,
Expand Down Expand Up @@ -597,6 +600,7 @@ def go_binary(name:str, srcs:list=[], resources:list=None, asm_srcs:list=[], out
provides={
'go': lib,
},
env = {'GOAMD64': CONFIG.GO.GOAMD64},
pre_build=_collect_linker_flags(static, definitions),
stamp = stamp,
)
Expand Down Expand Up @@ -635,7 +639,7 @@ def go_test(name:str, srcs:list, resources:list=None, data:list|dict=None, deps:
flags:str='', sandbox:bool=None, cgo:bool=False, filter_srcs:bool=True,
external:bool=False, timeout:int=0, flaky:bool|int=0, test_outputs:list=[],
labels:list&features&tags=[], size:str=None, static:bool=CONFIG.GO.DEFAULT_STATIC,
definitions:str|list|dict=None, env:dict=None):
definitions:str|list|dict=None, env:dict={}):
"""Defines a Go test rule.

Args:
Expand Down Expand Up @@ -792,7 +796,7 @@ def go_test(name:str, srcs:list, resources:list=None, data:list|dict=None, deps:
needs_transitive_deps = True,
output_is_complete = True,
pre_build = _collect_linker_flags(static, definitions),
env = env,
env = env | {'GOAMD64': CONFIG.GO.GOAMD64},
)


Expand Down Expand Up @@ -908,6 +912,7 @@ def go_benchmark(name:str, srcs:list, data:list|dict=None, deps:list=[], visibil
output_is_complete=True,
test_only=test_only,
pre_build = _collect_linker_flags(static, definitions),
env = {'GOAMD64': CONFIG.GO.GOAMD64},
)

def go_test_main(name:str, srcs:list, test_package:str="", test_only:bool=False, external=False,
Expand Down Expand Up @@ -1085,7 +1090,7 @@ def _go_install_module(name:str, module:str, install:list, src:str, outs:list, d
needs_transitive_deps = True,
licences = licences,
post_build = _add_ld_flags,
env = env,
env = env | {'GOAMD64': CONFIG.GO.GOAMD64},
)

def _add_ld_flags(name:str, stdout:list):
Expand Down Expand Up @@ -1425,7 +1430,10 @@ def _go_modinfo(name:str, test_only:bool=False, deps:list):
'go': [CONFIG.GO_TOOL],
'plz': [CONFIG.GO.PLEASE_GO_TOOL],
},
env = {'CGO_ENABLED': '1' if CONFIG.GO.CGO_ENABLED else '0'},
env = {
'CGO_ENABLED': '1' if CONFIG.GO.CGO_ENABLED else '0',
'GOAMD64': CONFIG.GO.GOAMD64,
},
test_only = test_only,
deps = deps,
requires = ['modinfo'],
Expand Down
6 changes: 5 additions & 1 deletion tools/please_go/modinfo/modinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// WriteModInfo writes mod info to the given output file
func WriteModInfo(goTool, modulePath, pkgPath, buildMode, cgoEnabled, goos, goarch, outputFile string) error {
func WriteModInfo(goTool, modulePath, pkgPath, buildMode, cgoEnabled, goos, goarch, goamd64, outputFile string) error {
if buildMode == "" {
buildMode = "exe"
}
Expand All @@ -38,6 +38,10 @@ func WriteModInfo(goTool, modulePath, pkgPath, buildMode, cgoEnabled, goos, goar
{Key: "GOOS", Value: goos},
},
}
// Only set GOAMD64 if we are targeting AMD64 #smart
if goarch == "amd64" {
bi.Settings = append(bi.Settings, debug.BuildSetting{Key: "GOAMD64", Value: goamd64})
}
if err := filepath.WalkDir(".", func(path string, d fs.DirEntry, err error) error {
if err != nil || d.IsDir() || !strings.HasSuffix(path, ".modinfo") {
return err
Expand Down
3 changes: 2 additions & 1 deletion tools/please_go/please_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ var opts = struct {
CgoEnabled string `short:"c" long:"cgo_enabled" env:"CGO_ENABLED" description:"Whether cgo is enabled or not"`
GoOS string `long:"goos" env:"OS" description:"OS we're compiling for"`
GoArch string `long:"goarch" env:"ARCH" description:"Architecture we're compiling for"`
GoAMD64 string `long:"goamd64" env:"GOAMD64" description:"GOAMD64 target"`
} `command:"modinfo" description:"Generates Go modinfo for the linter"`
}{
Usage: `
Expand Down Expand Up @@ -213,7 +214,7 @@ var subCommands = map[string]func() int{
}
os.Stderr.Write([]byte(info.String() + "\n"))
}
if err := modinfo.WriteModInfo(mi.GoTool, mi.ModulePath, filepath.Join(mi.ModulePath, mi.Pkg), mi.BuildMode, mi.CgoEnabled, mi.GoOS, mi.GoArch, mi.Out); err != nil {
if err := modinfo.WriteModInfo(mi.GoTool, mi.ModulePath, filepath.Join(mi.ModulePath, mi.Pkg), mi.BuildMode, mi.CgoEnabled, mi.GoOS, mi.GoArch, mi.GoAMD64, mi.Out); err != nil {
log.Fatalf("failed to write modinfo: %s", err)
}
return 0
Expand Down