1 From 39f8e98946a0958a4f69ec28c78b8e5d46125e44 Mon Sep 17 00:00:00 2001
2 From: Ian Lance Taylor <iant@golang.org>
3 Date: Fri, 13 Nov 2020 11:05:37 -0800
4 Subject: [PATCH] [release-branch.go1.15] cmd/go: permit CGO_LDFLAGS to appear in //go:ldflag
9 Change-Id: If7cf39905d124dbd54dfac6a53ee38270498efed
10 Reviewed-on: https://go-review.googlesource.com/c/go/+/269818
11 Trust: Ian Lance Taylor <iant@golang.org>
12 Run-TryBot: Ian Lance Taylor <iant@golang.org>
13 TryBot-Result: Go Bot <gobot@golang.org>
14 Reviewed-by: Jay Conrod <jayconrod@google.com>
15 (cherry picked from commit 782cf560db4c919790fdb476d1bbe18e5ddf5ffd)
18 diff --git a/src/cmd/go/internal/work/exec.go b/src/cmd/go/internal/work/exec.go
19 index 13d4c8c..dc0c4fc 100644
20 --- a/src/cmd/go/internal/work/exec.go
21 +++ b/src/cmd/go/internal/work/exec.go
22 @@ -2766,6 +2766,21 @@
23 idx = bytes.Index(src, []byte(cgoLdflag))
27 + // We expect to find the contents of cgoLDFLAGS in flags.
28 + if len(cgoLDFLAGS) > 0 {
30 + for i := range flags {
31 + for j, f := range cgoLDFLAGS {
32 + if f != flags[i+j] {
36 + flags = append(flags[:i], flags[i+len(cgoLDFLAGS):]...)
41 if err := checkLinkerFlags("LDFLAGS", "go:cgo_ldflag", flags); err != nil {
44 diff --git a/src/cmd/go/testdata/script/ldflag.txt b/src/cmd/go/testdata/script/ldflag.txt
46 index 0000000..6ceb33b
48 +++ b/src/cmd/go/testdata/script/ldflag.txt
54 +# We can't build package bad, which uses #cgo LDFLAGS.
57 +stderr no-such-warning
59 +# We can build package ok with the same flags in CGO_LDFLAGS.
60 +env CGO_LDFLAGS=-Wno-such-warning -Wno-unknown-warning-option
64 +# Build a main program that actually uses LDFLAGS.
68 +# Because we passed -v the Go linker should print the external linker
69 +# command which should include the flag we passed in CGO_LDFLAGS.
70 +stderr no-such-warning
78 +// #cgo LDFLAGS: -Wno-such-warning -Wno-unknown-warning