Skip to content

Commit 782cf56

Browse files
cmd/go: permit CGO_LDFLAGS to appear in //go:ldflag
Fixes #42565 Change-Id: If7cf39905d124dbd54dfac6a53ee38270498efed Reviewed-on: https://go-review.googlesource.com/c/go/+/269818 Trust: Ian Lance Taylor <[email protected]> Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 4f63e0a commit 782cf56

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/cmd/go/internal/work/exec.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2883,6 +2883,21 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
28832883
idx = bytes.Index(src, []byte(cgoLdflag))
28842884
}
28852885
}
2886+
2887+
// We expect to find the contents of cgoLDFLAGS in flags.
2888+
if len(cgoLDFLAGS) > 0 {
2889+
outer:
2890+
for i := range flags {
2891+
for j, f := range cgoLDFLAGS {
2892+
if f != flags[i+j] {
2893+
continue outer
2894+
}
2895+
}
2896+
flags = append(flags[:i], flags[i+len(cgoLDFLAGS):]...)
2897+
break
2898+
}
2899+
}
2900+
28862901
if err := checkLinkerFlags("LDFLAGS", "go:cgo_ldflag", flags); err != nil {
28872902
return nil, nil, err
28882903
}

src/cmd/go/testdata/script/ldflag.txt

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Issue #42565
2+
3+
[!cgo] skip
4+
5+
# We can't build package bad, which uses #cgo LDFLAGS.
6+
cd bad
7+
! go build
8+
stderr no-such-warning
9+
10+
# We can build package ok with the same flags in CGO_LDFLAGS.
11+
env CGO_LDFLAGS=-Wno-such-warning -Wno-unknown-warning-option
12+
cd ../ok
13+
go build
14+
15+
# Build a main program that actually uses LDFLAGS.
16+
cd ..
17+
go build -ldflags=-v
18+
19+
# Because we passed -v the Go linker should print the external linker
20+
# command which should include the flag we passed in CGO_LDFLAGS.
21+
stderr no-such-warning
22+
23+
-- go.mod --
24+
module ldflag
25+
26+
-- bad/bad.go --
27+
package bad
28+
29+
// #cgo LDFLAGS: -Wno-such-warning -Wno-unknown-warning
30+
import "C"
31+
32+
func F() {}
33+
-- ok/ok.go --
34+
package ok
35+
36+
import "C"
37+
38+
func F() {}
39+
-- main.go --
40+
package main
41+
42+
import _ "ldflag/ok"
43+
44+
func main() {}

0 commit comments

Comments
 (0)