diff options
| author | Ian Lance Taylor <iant@golang.org> | 2017-05-25 22:20:27 -0700 |
|---|---|---|
| committer | Ian Lance Taylor <iant@golang.org> | 2017-06-13 18:36:04 +0000 |
| commit | b488073d514d06269eab561104c0dc5ff606c4ba (patch) | |
| tree | 2a9e20b60e8d479f2c610cc8e4b87f723467febe /src | |
| parent | d1211b9a9f65ea3f184ceb34f1833d4d1a623f7c (diff) | |
| download | go-b488073d514d06269eab561104c0dc5ff606c4ba.tar.xz | |
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>
Diffstat (limited to 'src')
| -rw-r--r-- | src/cmd/go/go_test.go | 7 | ||||
| -rw-r--r-- | src/go/build/build.go | 37 |
2 files changed, 41 insertions, 3 deletions
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. |
