Commit d06be395 authored by Andrew Wilkins's avatar Andrew Wilkins Committed by Rob Pike

cmd/cgo, cmd/go: remove #cgo directive parsing from cmd/cgo

This change removes processing of #cgo directives from cmd/cgo,
pushing the onus back on cmd/go to pass all necessary flags.

Fixes #5224. See comments for rationale.

R=golang-dev, iant, r
CC=golang-dev
https://golang.org/cl/8610044
parent 082a4a8a
// Copyright 2013 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.
// Test that the #cgo CFLAGS directive works,
// with and without platform filters.
// See http://code.google.com/p/go/issues/detail?id=5224 for details.
package cgotest
/*
#cgo CFLAGS: -DCOMMON_VALUE=123
#cgo windows CFLAGS: -DIS_WINDOWS=1
#cgo !windows CFLAGS: -DIS_WINDOWS=0
int common = COMMON_VALUE;
int is_windows = IS_WINDOWS;
*/
import "C"
import (
"runtime"
"testing"
)
func testCflags(t *testing.T) {
is_windows := C.is_windows == 1
if is_windows != (runtime.GOOS == "windows") {
t.Errorf("is_windows: %v, runtime.GOOS: %s", is_windows, runtime.GOOS)
}
if C.common != 123 {
t.Errorf("common: %v (expected 123)", C.common)
}
}
......@@ -38,5 +38,6 @@ func Test3775(t *testing.T) { test3775(t) }
func TestCthread(t *testing.T) { testCthread(t) }
func TestCallbackCallers(t *testing.T) { testCallbackCallers(t) }
func Test5227(t *testing.T) { test5227(t) }
func TestCflags(t *testing.T) { testCflags(t) }
func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) }
......@@ -66,71 +66,16 @@ func cname(s string) string {
return s
}
// ParseFlags extracts #cgo CFLAGS and LDFLAGS options from the file
// preamble. Multiple occurrences are concatenated with a separating space,
// even across files.
func (p *Package) ParseFlags(f *File, srcfile string) {
// DiscardCgoDirectives processes the import C preamble, and discards
// all #cgo CFLAGS and LDFLAGS directives, so they don't make their
// way into _cgo_export.h.
func (f *File) DiscardCgoDirectives() {
linesIn := strings.Split(f.Preamble, "\n")
linesOut := make([]string, 0, len(linesIn))
NextLine:
for _, line := range linesIn {
l := strings.TrimSpace(line)
if len(l) < 5 || l[:4] != "#cgo" || !unicode.IsSpace(rune(l[4])) {
linesOut = append(linesOut, line)
continue
}
l = strings.TrimSpace(l[4:])
fields := strings.SplitN(l, ":", 2)
if len(fields) != 2 {
fatalf("%s: bad #cgo line: %s", srcfile, line)
}
var k string
kf := strings.Fields(fields[0])
switch len(kf) {
case 1:
k = kf[0]
case 2:
k = kf[1]
switch kf[0] {
case goos:
case goarch:
case goos + "/" + goarch:
default:
continue NextLine
}
default:
fatalf("%s: bad #cgo option: %s", srcfile, fields[0])
}
args, err := splitQuoted(fields[1])
if err != nil {
fatalf("%s: bad #cgo option %s: %s", srcfile, k, err)
}
for _, arg := range args {
if !safeName(arg) {
fatalf("%s: #cgo option %s is unsafe: %s", srcfile, k, arg)
}
}
switch k {
case "CFLAGS", "LDFLAGS":
p.addToFlag(k, args)
case "pkg-config":
cflags, ldflags, err := pkgConfig(args)
if err != nil {
fatalf("%s: bad #cgo option %s: %s", srcfile, k, err)
}
p.addToFlag("CFLAGS", cflags)
p.addToFlag("LDFLAGS", ldflags)
default:
fatalf("%s: unsupported #cgo option %s", srcfile, k)
}
}
f.Preamble = strings.Join(linesOut, "\n")
......@@ -146,36 +91,6 @@ func (p *Package) addToFlag(flag string, args []string) {
}
}
// pkgConfig runs pkg-config and extracts --libs and --cflags information
// for packages.
func pkgConfig(packages []string) (cflags, ldflags []string, err error) {
for _, name := range packages {
if len(name) == 0 || name[0] == '-' {
return nil, nil, errors.New(fmt.Sprintf("invalid name: %q", name))
}
}
args := append([]string{"pkg-config", "--cflags"}, packages...)
stdout, stderr, ok := run(nil, args)
if !ok {
os.Stderr.Write(stderr)
return nil, nil, errors.New("pkg-config failed")
}
cflags, err = splitQuoted(string(stdout))
if err != nil {
return
}
args = append([]string{"pkg-config", "--libs"}, packages...)
stdout, stderr, ok = run(nil, args)
if !ok {
os.Stderr.Write(stderr)
return nil, nil, errors.New("pkg-config failed")
}
ldflags, err = splitQuoted(string(stdout))
return
}
// splitQuoted splits the string s around each instance of one or more consecutive
// white space characters while taking into account quotes and escaping, and
// returns an array of substrings of s or an empty list if s contains only white space.
......
......@@ -235,10 +235,9 @@ func main() {
fs := make([]*File, len(goFiles))
for i, input := range goFiles {
// Parse flags for all files before translating due to CFLAGS.
f := new(File)
f.ReadGo(input)
p.ParseFlags(f, input)
f.DiscardCgoDirectives()
fs[i] = f
}
......@@ -291,11 +290,6 @@ func main() {
// newPackage returns a new Package that will invoke
// gcc with the additional arguments specified in args.
func newPackage(args []string) *Package {
// Copy the gcc options to a new slice so the list
// can grow without overwriting the slice that args is in.
gccOptions := make([]string, len(args))
copy(gccOptions, args)
goarch = runtime.GOARCH
if s := os.Getenv("GOARCH"); s != "" {
goarch = s
......@@ -318,12 +312,12 @@ func newPackage(args []string) *Package {
os.Setenv("LC_ALL", "C")
p := &Package{
PtrSize: ptrSize,
IntSize: intSize,
GccOptions: gccOptions,
CgoFlags: make(map[string][]string),
Written: make(map[string]bool),
PtrSize: ptrSize,
IntSize: intSize,
CgoFlags: make(map[string][]string),
Written: make(map[string]bool),
}
p.addToFlag("CFLAGS", args)
return p
}
......
This diff is collapsed.
......@@ -393,16 +393,26 @@ func runOut(dir string, cmdargs ...interface{}) []byte {
// child will be faster.
func envForDir(dir string) []string {
env := os.Environ()
for i, kv := range env {
if strings.HasPrefix(kv, "PWD=") {
env[i] = "PWD=" + dir
return env
}
}
// Internally we only use rooted paths, so dir is rooted.
// Even if dir is not rooted, no harm done.
env = append(env, "PWD="+dir)
return env
return mergeEnvLists([]string{"PWD=" + dir}, env)
}
// mergeEnvLists merges the two environment lists such that
// variables with the same name in "in" replace those in "out".
func mergeEnvLists(in, out []string) []string {
NextVar:
for _, inkv := range in {
k := strings.SplitAfterN(inkv, "=", 2)[0]
for i, outkv := range out {
if strings.HasPrefix(outkv, k) {
out[i] = inkv
continue NextVar
}
}
out = append(out, inkv)
}
return out
}
// matchPattern(pattern)(name) reports whether
......
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