]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: read CGO_LDFLAGS environment variable
authorIan Lance Taylor <iant@golang.org>
Wed, 3 Jul 2024 18:20:54 +0000 (11:20 -0700)
committerGopher Robot <gobot@golang.org>
Wed, 3 Jul 2024 19:02:39 +0000 (19:02 +0000)
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>
src/cmd/cgo/main.go

index 156038b33b410c51f5c03d2c363497905bc0b98b..5699cc55be0e66ea38fa108b2904293d167bd88e 100644 (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