]> Cypherpunks repositories - gostls13.git/commitdiff
go/build: make -I/-L options in cgo flags absolute
authorIan Lance Taylor <iant@golang.org>
Fri, 26 May 2017 05:20:27 +0000 (22:20 -0700)
committerIan Lance Taylor <iant@golang.org>
Tue, 13 Jun 2017 18:36:04 +0000 (18:36 +0000)
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>
misc/cgo/test/cgo_test.go
misc/cgo/test/issue20266.go [new file with mode: 0644]
misc/cgo/test/issue20266/issue20266.h [new file with mode: 0644]
src/cmd/go/go_test.go
src/go/build/build.go

index ddc0258c1a704042fa2c26587368b476bf27c73b..f248381b148f2eef79a8b8731b6471df651bff60 100644 (file)
@@ -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 (file)
index 0000000..9f95086
--- /dev/null
@@ -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 (file)
index 0000000..8d3258e
--- /dev/null
@@ -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
index 90a95fd23d2dc206a708de51e5ac7c16ff85eac0..205a1b14e2df8b76f2ff209b52e288d3364cc264 100644 (file)
@@ -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")
                                        }
index 17446ee4ceb48a7384876348c4772d8d4cd3282b..fd89871d42408ed93fd5a172d23f16ed80cfba85 100644 (file)
@@ -1281,6 +1281,12 @@ func (ctxt *Context) saveCgo(filename string, di *Package, cg *ast.CommentGroup)
                        args[i] = arg
                }
 
+               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...)
@@ -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.