From 7d19d508a994be9731e27b14b6ecc13ccf527138 Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Wed, 3 Jul 2024 11:20:54 -0700 Subject: [PATCH] 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 Reviewed-by: Ian Lance Taylor Auto-Submit: Ian Lance Taylor Commit-Queue: Ian Lance Taylor Auto-Submit: Ian Lance Taylor Reviewed-by: Cherry Mui --- src/cmd/cgo/main.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/cmd/cgo/main.go b/src/cmd/cgo/main.go index 156038b33b..5699cc55be 100644 --- a/src/cmd/cgo/main.go +++ b/src/cmd/cgo/main.go @@ -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 -- 2.48.1