Skip to content

Commit 0997f8f

Browse files
mknyszekgopherbot
authored andcommitted
sweet: apply flags to cockroachdb 'go build' based on 'go version'
Currently, the cockroachdb benchmark is built by passing a build tag (untested_go_version) unconditionally, and we also try to build with and without '-checklinkname=0' passed to the linker. These are required because some CockroachDB packages reach into the runtime internals. The current approach doesn't work well across Go versions because setting "untested_go_version" for older toolchains causes a symbol conflict (it ends up enabling multiple files with the same definition). We can fix this upstream, but we also need a fix in the short term. Plus, it'll help to clean up this logic and make it more explicit. So, this CL parses 'go version' in a dead-simple way to determine what to do. If we're using an older version of Go and the version is well-defined, then we never use 'untested_go_version'. But if the version of Go is explicitly a development version or newer than Go 1.24, then we do. Simultaneously, we only pass -checklinkname=0 to the linker if the toolchain version is >= 1.23. We always pass it for development Go versions. Fixes golang/go#68555. Change-Id: Ieaad89cb6a92bdeee2636dc62b5b3a26ac317b5f Cq-Include-Trybots: luci.golang.try:x_benchmarks-gotip-linux-amd64-longtest,x_benchmarks-go1.22-linux-amd64-longtest Reviewed-on: https://go-review.googlesource.com/c/benchmarks/+/600475 Reviewed-by: Austin Clements <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Michael Knyszek <[email protected]>
1 parent 2ef16eb commit 0997f8f

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

sweet/common/gotool.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,17 @@ func (g *Go) BuildPackage(pkg, out string) error {
7272
return g.Do("", "build", "-o", out, pkg)
7373
}
7474

75+
func (g *Go) Version() (string, error) {
76+
cmd := exec.Command(g.Tool, "version")
77+
cmd.Env = g.Env.Collapse()
78+
log.TraceCommand(cmd, false)
79+
out, err := cmd.Output()
80+
if err != nil {
81+
return "", fmt.Errorf("error running 'go version': %w", err)
82+
}
83+
return string(out), nil
84+
}
85+
7586
func (g *Go) BuildPath(path, out string, args ...string) error {
7687
if path[0] != '/' && path[0] != '.' {
7788
path = "./" + path

sweet/harnesses/cockroachdb.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
package harnesses
66

77
import (
8-
"errors"
98
"fmt"
109
"os"
1110
"os/exec"
1211
"path/filepath"
1312
"runtime"
13+
"strings"
1414
"time"
1515

1616
"golang.org/x/benchmarks/sweet/common"
@@ -98,22 +98,32 @@ func (h CockroachDB) Build(cfg *common.Config, bcfg *common.BuildConfig) error {
9898
return err
9999
}
100100

101-
// Finally build the cockroach binary with `go build`. Build the
102-
// cockroach-short binary as it is functionally the same, but
103-
// without the UI, making it much quicker to build.
101+
// Get the Go version. Then, finall build the cockroach binary with `go build`.
104102
//
105-
// As of go1.23, we need to pass the `-ldflags=-checklinkname=0` flag
106-
// to build cockroach. However, benchmark release branches are on older
107-
// versions that don't recognize the flag. Try first with the flag and
108-
// again without if there is an error.
103+
// Build the cockroach-short binary as it is functionally the same, but without
104+
// the UI, making it much quicker to build.
109105
//
110-
// Cockroachdb reaches into runtime internals, so it has a negative build
111-
// tag to exclude unreleased Go versions. Set untested_go_version so we can
112-
// run at tip (and cross our fingers).
113-
if buildWithFlagErr := cfg.GoTool().BuildPath(filepath.Join(bcfg.SrcDir, "pkg/cmd/cockroach-short"), bcfg.BinDir, "-tags=untested_go_version", "-ldflags=-checklinkname=0"); buildWithFlagErr != nil {
114-
if buildWithoutFlagErr := cfg.GoTool().BuildPath(filepath.Join(bcfg.SrcDir, "pkg/cmd/cockroach-short"), bcfg.BinDir, "-tags=untested_go_version"); buildWithoutFlagErr != nil {
115-
return errors.Join(buildWithFlagErr, buildWithoutFlagErr)
116-
}
106+
// CockroachDB reaches into runtime internals, so we need to pass -checklinkname=0
107+
// to the linker to get it to build with Go 1.23+. Also, CockroachDB has a negative
108+
// build tag to exclude unreleased Go versions, so we need to explicitly set it for
109+
// new-enough Go versions.
110+
//
111+
// Note that since the version of CockroachDB is pinned, we can generally rely on
112+
// the checking below. However as CockroachDB and Go evolve, the logic below may need
113+
// to change.
114+
ver, err := cfg.GoTool().Version()
115+
if err != nil {
116+
return fmt.Errorf("getting go version for toolchain: %v", err)
117+
}
118+
var goBuildArgs []string
119+
if v := strings.TrimPrefix(ver, "go version "); strings.HasPrefix(v, "devel ") || v >= "go1.23" {
120+
goBuildArgs = append(goBuildArgs, "-ldflags=-checklinkname=0")
121+
}
122+
if v := strings.TrimPrefix(ver, "go version "); strings.HasPrefix(v, "devel ") || v >= "go1.24" {
123+
goBuildArgs = append(goBuildArgs, "-tags=untested_go_version")
124+
}
125+
if err := cfg.GoTool().BuildPath(filepath.Join(bcfg.SrcDir, "pkg/cmd/cockroach-short"), bcfg.BinDir, goBuildArgs...); err != nil {
126+
return err
117127
}
118128

119129
// Rename the binary from cockroach-short to cockroach for
@@ -126,7 +136,6 @@ func (h CockroachDB) Build(cfg *common.Config, bcfg *common.BuildConfig) error {
126136
if err := cfg.GoTool().BuildPath(bcfg.BenchDir, filepath.Join(bcfg.BinDir, "cockroachdb-bench")); err != nil {
127137
return err
128138
}
129-
130139
return nil
131140
}
132141

0 commit comments

Comments
 (0)