1
0
mirror of https://github.com/golang/go synced 2024-11-11 21:20:21 -07:00

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>
This commit is contained in:
Ian Lance Taylor 2017-05-25 22:20:27 -07:00
parent d1211b9a9f
commit b488073d51
5 changed files with 72 additions and 3 deletions

View 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) }

View File

@ -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)
}
}

View File

@ -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

View 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")
}

View 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.