Commit b488073d authored by Ian Lance Taylor's avatar Ian Lance Taylor

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: default avatarRuss Cox <rsc@golang.org>
parent d1211b9a
...@@ -78,5 +78,6 @@ func Test17537(t *testing.T) { test17537(t) } ...@@ -78,5 +78,6 @@ func Test17537(t *testing.T) { test17537(t) }
func Test18126(t *testing.T) { test18126(t) } func Test18126(t *testing.T) { test18126(t) }
func Test20369(t *testing.T) { test20369(t) } func Test20369(t *testing.T) { test20369(t) }
func Test18720(t *testing.T) { test18720(t) } func Test18720(t *testing.T) { test18720(t) }
func Test20266(t *testing.T) { test20266(t) }
func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) } func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) }
// 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)
}
}
// 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
...@@ -4087,6 +4087,7 @@ func TestCgoFlagContainsSpace(t *testing.T) { ...@@ -4087,6 +4087,7 @@ func TestCgoFlagContainsSpace(t *testing.T) {
import ( import (
"os" "os"
"os/exec" "os/exec"
"strings"
) )
func main() { func main() {
...@@ -4105,13 +4106,13 @@ func TestCgoFlagContainsSpace(t *testing.T) { ...@@ -4105,13 +4106,13 @@ func TestCgoFlagContainsSpace(t *testing.T) {
var success bool var success bool
for _, arg := range os.Args { for _, arg := range os.Args {
switch arg { switch {
case "-Ic flags": case strings.Contains(arg, "c flags"):
if success { if success {
panic("duplicate CFLAGS") panic("duplicate CFLAGS")
} }
success = true success = true
case "-Lld flags": case strings.Contains(arg, "ld flags"):
if success { if success {
panic("duplicate LDFLAGS") panic("duplicate LDFLAGS")
} }
......
...@@ -1281,6 +1281,12 @@ func (ctxt *Context) saveCgo(filename string, di *Package, cg *ast.CommentGroup) ...@@ -1281,6 +1281,12 @@ func (ctxt *Context) saveCgo(filename string, di *Package, cg *ast.CommentGroup)
args[i] = arg args[i] = arg
} }
switch verb {
case "CFLAGS", "CPPFLAGS", "CXXFLAGS", "FFLAGS", "LDFLAGS":
// Change relative paths to absolute.
ctxt.makePathsAbsolute(args, di.Dir)
}
switch verb { switch verb {
case "CFLAGS": case "CFLAGS":
di.CgoCFLAGS = append(di.CgoCFLAGS, args...) di.CgoCFLAGS = append(di.CgoCFLAGS, args...)
...@@ -1322,6 +1328,37 @@ func expandSrcDir(str string, srcdir string) (string, bool) { ...@@ -1322,6 +1328,37 @@ func expandSrcDir(str string, srcdir string) (string, bool) {
return res, ok && res != "" 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. // 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. // 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. // See golang.org/issue/6038.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment