cmd/cgo: read CGO_LDFLAGS environment variable

For #66456 we changed from the CGO_LDFLAGS environment variable to
the -ldflags option. This broke Bazel, which uses CGO_LDFLAGS.
So restore reading CGO_LDFLAGS for now.

For #66456

Change-Id: Iebdd8bde1c7c18da09c6370e284c7ac7fa08fc54
Reviewed-on: https://go-review.googlesource.com/c/go/+/596615
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Commit-Queue: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
This commit is contained in:
Ian Lance Taylor 2024-07-03 11:20:54 -07:00 committed by Gopher Robot
parent 5f50b1e3bf
commit 7d19d508a9

View file

@ -343,6 +343,21 @@ func main() {
p.addToFlag("LDFLAGS", args)
}
// For backward compatibility for Bazel, record CGO_LDFLAGS
// from the environment for external linking.
// This should not happen with cmd/go, which removes CGO_LDFLAGS
// from the environment when invoking cgo.
// This can be removed when we no longer need to support
// older versions of Bazel. See issue #66456 and
// https://github.com/bazelbuild/rules_go/issues/3979.
if envFlags := os.Getenv("CGO_LDFLAGS"); envFlags != "" {
args, err := splitQuoted(envFlags)
if err != nil {
fatalf("bad CGO_LDFLAGS: %q (%s)", envFlags, err)
}
p.addToFlag("LDFLAGS", args)
}
// Need a unique prefix for the global C symbols that
// we use to coordinate between gcc and ourselves.
// We already put _cgo_ at the beginning, so the main