Skip to content

Commit c1bf152

Browse files
ianlancetaylorandybons
authored andcommitted
[release-branch.go1.9] cmd/go: permit pkg-config flags in any argument position
Fixes golang#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]> Reviewed-on: https://go-review.googlesource.com/103155 Run-TryBot: Andrew Bonventre <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 51fd3d4 commit c1bf152

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
@@ -4496,3 +4496,36 @@ func TestBadCgoDirectives(t *testing.T) {
44964496
tg.run("build", "-n", "x")
44974497
tg.grepStderr("-D@foo", "did not find -D@foo in commands")
44984498
}
4499+
4500+
func TestTwoPkgConfigs(t *testing.T) {
4501+
if !canCgo {
4502+
t.Skip("no cgo")
4503+
}
4504+
if runtime.GOOS == "windows" || runtime.GOOS == "plan9" {
4505+
t.Skipf("no shell scripts on %s", runtime.GOOS)
4506+
}
4507+
tg := testgo(t)
4508+
defer tg.cleanup()
4509+
tg.parallel()
4510+
tg.tempFile("src/x/a.go", `package x
4511+
// #cgo pkg-config: --static a
4512+
import "C"
4513+
`)
4514+
tg.tempFile("src/x/b.go", `package x
4515+
// #cgo pkg-config: --static a
4516+
import "C"
4517+
`)
4518+
tg.tempFile("pkg-config.sh", `#!/bin/sh
4519+
echo $* >>`+tg.path("pkg-config.out"))
4520+
tg.must(os.Chmod(tg.path("pkg-config.sh"), 0755))
4521+
tg.setenv("GOPATH", tg.path("."))
4522+
tg.setenv("PKG_CONFIG", tg.path("pkg-config.sh"))
4523+
tg.run("build", "x")
4524+
out, err := ioutil.ReadFile(tg.path("pkg-config.out"))
4525+
tg.must(err)
4526+
out = bytes.TrimSpace(out)
4527+
want := "--cflags --static --static -- a a\n--libs --static --static -- a a"
4528+
if !bytes.Equal(out, []byte(want)) {
4529+
t.Errorf("got %q want %q", out, want)
4530+
}
4531+
}

src/cmd/go/internal/work/build.go

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

15261526
// Calls pkg-config if needed and returns the cflags/ldflags needed to build the package.
15271527
func (b *Builder) getPkgConfigFlags(p *load.Package) (cflags, ldflags []string, err error) {
1528-
if pkgs := p.CgoPkgConfig; len(pkgs) > 0 {
1528+
if pcargs := p.CgoPkgConfig; len(pcargs) > 0 {
1529+
// pkg-config permits arguments to appear anywhere in
1530+
// the command line. Move them all to the front, before --.
15291531
var pcflags []string
1530-
for len(pkgs) > 0 && strings.HasPrefix(pkgs[0], "--") {
1531-
pcflags = append(pcflags, pkgs[0])
1532-
pkgs = pkgs[1:]
1532+
var pkgs []string
1533+
for _, pcarg := range pcargs {
1534+
if pcarg == "--" {
1535+
// We're going to add our own "--" argument.
1536+
} else if strings.HasPrefix(pcarg, "--") {
1537+
pcflags = append(pcflags, pcarg)
1538+
} else {
1539+
pkgs = append(pkgs, pcarg)
1540+
}
15331541
}
15341542
for _, pkg := range pkgs {
15351543
if !load.SafeArg(pkg) {

0 commit comments

Comments
 (0)