mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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 <iant@golang.org> Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
This commit is contained in:
parent
4f63e0a1f8
commit
782cf560db
2 changed files with 59 additions and 0 deletions
|
|
@ -2883,6 +2883,21 @@ func (b *Builder) cgo(a *Action, cgoExe, objdir string, pcCFLAGS, pcLDFLAGS, cgo
|
||||||
idx = bytes.Index(src, []byte(cgoLdflag))
|
idx = bytes.Index(src, []byte(cgoLdflag))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We expect to find the contents of cgoLDFLAGS in flags.
|
||||||
|
if len(cgoLDFLAGS) > 0 {
|
||||||
|
outer:
|
||||||
|
for i := range flags {
|
||||||
|
for j, f := range cgoLDFLAGS {
|
||||||
|
if f != flags[i+j] {
|
||||||
|
continue outer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
flags = append(flags[:i], flags[i+len(cgoLDFLAGS):]...)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err := checkLinkerFlags("LDFLAGS", "go:cgo_ldflag", flags); err != nil {
|
if err := checkLinkerFlags("LDFLAGS", "go:cgo_ldflag", flags); err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
44
src/cmd/go/testdata/script/ldflag.txt
vendored
Normal file
44
src/cmd/go/testdata/script/ldflag.txt
vendored
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
# Issue #42565
|
||||||
|
|
||||||
|
[!cgo] skip
|
||||||
|
|
||||||
|
# We can't build package bad, which uses #cgo LDFLAGS.
|
||||||
|
cd bad
|
||||||
|
! go build
|
||||||
|
stderr no-such-warning
|
||||||
|
|
||||||
|
# We can build package ok with the same flags in CGO_LDFLAGS.
|
||||||
|
env CGO_LDFLAGS=-Wno-such-warning -Wno-unknown-warning-option
|
||||||
|
cd ../ok
|
||||||
|
go build
|
||||||
|
|
||||||
|
# Build a main program that actually uses LDFLAGS.
|
||||||
|
cd ..
|
||||||
|
go build -ldflags=-v
|
||||||
|
|
||||||
|
# Because we passed -v the Go linker should print the external linker
|
||||||
|
# command which should include the flag we passed in CGO_LDFLAGS.
|
||||||
|
stderr no-such-warning
|
||||||
|
|
||||||
|
-- go.mod --
|
||||||
|
module ldflag
|
||||||
|
|
||||||
|
-- bad/bad.go --
|
||||||
|
package bad
|
||||||
|
|
||||||
|
// #cgo LDFLAGS: -Wno-such-warning -Wno-unknown-warning
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
func F() {}
|
||||||
|
-- ok/ok.go --
|
||||||
|
package ok
|
||||||
|
|
||||||
|
import "C"
|
||||||
|
|
||||||
|
func F() {}
|
||||||
|
-- main.go --
|
||||||
|
package main
|
||||||
|
|
||||||
|
import _ "ldflag/ok"
|
||||||
|
|
||||||
|
func main() {}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue