aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2017-05-25 22:20:27 -0700
committerIan Lance Taylor <iant@golang.org>2017-06-13 18:36:04 +0000
commitb488073d514d06269eab561104c0dc5ff606c4ba (patch)
tree2a9e20b60e8d479f2c610cc8e4b87f723467febe
parentd1211b9a9f65ea3f184ceb34f1833d4d1a623f7c (diff)
downloadgo-b488073d514d06269eab561104c0dc5ff606c4ba.tar.gz
go-b488073d514d06269eab561104c0dc5ff606c4ba.zip
go/build: make -I/-L options in cgo flags absolute
Fixes #20266. Change-Id: I51383820880e3d3566ef3d70650a0863756003ba Reviewed-on: https://go-review.googlesource.com/44291 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org>
-rw-r--r--misc/cgo/test/cgo_test.go1
-rw-r--r--misc/cgo/test/issue20266.go21
-rw-r--r--misc/cgo/test/issue20266/issue20266.h9
-rw-r--r--src/cmd/go/go_test.go7
-rw-r--r--src/go/build/build.go37
5 files changed, 72 insertions, 3 deletions
diff --git a/misc/cgo/test/cgo_test.go b/misc/cgo/test/cgo_test.go
index ddc0258c1a..f248381b14 100644
--- a/misc/cgo/test/cgo_test.go
+++ b/misc/cgo/test/cgo_test.go
@@ -78,5 +78,6 @@ func Test17537(t *testing.T) { test17537(t) }
func Test18126(t *testing.T) { test18126(t) }
func Test20369(t *testing.T) { test20369(t) }
func Test18720(t *testing.T) { test18720(t) }
+func Test20266(t *testing.T) { test20266(t) }
func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) }
diff --git a/misc/cgo/test/issue20266.go b/misc/cgo/test/issue20266.go
new file mode 100644
index 0000000000..9f95086cc7
--- /dev/null
+++ b/misc/cgo/test/issue20266.go
@@ -0,0 +1,21 @@
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Issue 20266: use -I with a relative path.
+
+package cgotest
+
+/*
+#cgo CFLAGS: -I issue20266 -Iissue20266 -Ddef20266
+#include "issue20266.h"
+*/
+import "C"
+
+import "testing"
+
+func test20266(t *testing.T) {
+ if got, want := C.issue20266, 20266; got != want {
+ t.Errorf("got %d, want %d", got, want)
+ }
+}
diff --git a/misc/cgo/test/issue20266/issue20266.h b/misc/cgo/test/issue20266/issue20266.h
new file mode 100644
index 0000000000..8d3258ec6b
--- /dev/null
+++ b/misc/cgo/test/issue20266/issue20266.h
@@ -0,0 +1,9 @@
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+#define issue20266 20266
+
+#ifndef def20266
+#error "expected def20266 to be defined"
+#endif
diff --git a/src/cmd/go/go_test.go b/src/cmd/go/go_test.go
index 90a95fd23d..205a1b14e2 100644
--- a/src/cmd/go/go_test.go
+++ b/src/cmd/go/go_test.go
@@ -4087,6 +4087,7 @@ func TestCgoFlagContainsSpace(t *testing.T) {
import (
"os"
"os/exec"
+ "strings"
)
func main() {
@@ -4105,13 +4106,13 @@ func TestCgoFlagContainsSpace(t *testing.T) {
var success bool
for _, arg := range os.Args {
- switch arg {
- case "-Ic flags":
+ switch {
+ case strings.Contains(arg, "c flags"):
if success {
panic("duplicate CFLAGS")
}
success = true
- case "-Lld flags":
+ case strings.Contains(arg, "ld flags"):
if success {
panic("duplicate LDFLAGS")
}
diff --git a/src/go/build/build.go b/src/go/build/build.go
index 17446ee4ce..fd89871d42 100644
--- a/src/go/build/build.go
+++ b/src/go/build/build.go
@@ -1282,6 +1282,12 @@ func (ctxt *Context) saveCgo(filename string, di *Package, cg *ast.CommentGroup)
}
switch verb {
+ case "CFLAGS", "CPPFLAGS", "CXXFLAGS", "FFLAGS", "LDFLAGS":
+ // Change relative paths to absolute.
+ ctxt.makePathsAbsolute(args, di.Dir)
+ }
+
+ switch verb {
case "CFLAGS":
di.CgoCFLAGS = append(di.CgoCFLAGS, args...)
case "CPPFLAGS":
@@ -1322,6 +1328,37 @@ func expandSrcDir(str string, srcdir string) (string, bool) {
return res, ok && res != ""
}
+// makePathsAbsolute looks for compiler options that take paths and
+// makes them absolute. We do this because through the 1.8 release we
+// ran the compiler in the package directory, so any relative -I or -L
+// options would be relative to that directory. In 1.9 we changed to
+// running the compiler in the build directory, to get consistent
+// build results (issue #19964). To keep builds working, we change any
+// relative -I or -L options to be absolute.
+//
+// Using filepath.IsAbs and filepath.Join here means the results will be
+// different on different systems, but that's OK: -I and -L options are
+// inherently system-dependent.
+func (ctxt *Context) makePathsAbsolute(args []string, srcDir string) {
+ nextPath := false
+ for i, arg := range args {
+ if nextPath {
+ if !filepath.IsAbs(arg) {
+ args[i] = filepath.Join(srcDir, arg)
+ }
+ nextPath = false
+ } else if strings.HasPrefix(arg, "-I") || strings.HasPrefix(arg, "-L") {
+ if len(arg) == 2 {
+ nextPath = true
+ } else {
+ if !filepath.IsAbs(arg[2:]) {
+ args[i] = arg[:2] + filepath.Join(srcDir, arg[2:])
+ }
+ }
+ }
+ }
+}
+
// NOTE: $ is not safe for the shell, but it is allowed here because of linker options like -Wl,$ORIGIN.
// We never pass these arguments to a shell (just to programs we construct argv for), so this should be okay.
// See golang.org/issue/6038.