Skip to content

Commit 8e402dc

Browse files
cmd/go: permit pkg-config flags in any argument position
Fixes #23875 Change-Id: I503af71f44d11cd6b787fef100246b55735614a0 Reviewed-on: https://go-review.googlesource.com/94896 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent b26a4a1 commit 8e402dc

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

src/cmd/go/go_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5888,3 +5888,36 @@ func TestBadCgoDirectives(t *testing.T) {
58885888
tg.run("build", "-n", "x")
58895889
tg.grepStderr("-D@foo", "did not find -D@foo in commands")
58905890
}
5891+
5892+
func TestTwoPkgConfigs(t *testing.T) {
5893+
if !canCgo {
5894+
t.Skip("no cgo")
5895+
}
5896+
if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
5897+
t.Skipf("no shell scripts on %s", runtime.GOOS)
5898+
}
5899+
tg := testgo(t)
5900+
defer tg.cleanup()
5901+
tg.parallel()
5902+
tg.tempFile("src/x/a.go", `package x
5903+
// #cgo pkg-config: --static a
5904+
import "C"
5905+
`)
5906+
tg.tempFile("src/x/b.go", `package x
5907+
// #cgo pkg-config: --static a
5908+
import "C"
5909+
`)
5910+
tg.tempFile("pkg-config.sh", `#!/bin/sh
5911+
echo $* >>`+tg.path("pkg-config.out"))
5912+
tg.must(os.Chmod(tg.path("pkg-config.sh"), 0755))
5913+
tg.setenv("GOPATH", tg.path("."))
5914+
tg.setenv("PKG_CONFIG", tg.path("pkg-config.sh"))
5915+
tg.run("build", "x")
5916+
out, err := ioutil.ReadFile(tg.path("pkg-config.out"))
5917+
tg.must(err)
5918+
out = bytes.TrimSpace(out)
5919+
want := "--cflags --static --static -- a a\n--libs --static --static -- a a"
5920+
if !bytes.Equal(out, []byte(want)) {
5921+
t.Errorf("got %q want %q", out, want)
5922+
}
5923+
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -934,11 +934,19 @@ func splitPkgConfigOutput(out []byte) []string {
934934

935935
// Calls pkg-config if needed and returns the cflags/ldflags needed to build the package.
936936
func (b *Builder) getPkgConfigFlags(p *load.Package) (cflags, ldflags []string, err error) {
937-
if pkgs := p.CgoPkgConfig; len(pkgs) > 0 {
937+
if pcargs := p.CgoPkgConfig; len(pcargs) > 0 {
938+
// pkg-config permits arguments to appear anywhere in
939+
// the command line. Move them all to the front, before --.
938940
var pcflags []string
939-
for len(pkgs) > 0 && strings.HasPrefix(pkgs[0], "--") {
940-
pcflags = append(pcflags, pkgs[0])
941-
pkgs = pkgs[1:]
941+
var pkgs []string
942+
for _, pcarg := range pcargs {
943+
if pcarg == "--" {
944+
// We're going to add our own "--" argument.
945+
} else if strings.HasPrefix(pcarg, "--") {
946+
pcflags = append(pcflags, pcarg)
947+
} else {
948+
pkgs = append(pkgs, pcarg)
949+
}
942950
}
943951
for _, pkg := range pkgs {
944952
if !load.SafeArg(pkg) {

0 commit comments

Comments
 (0)