summaryrefslogtreecommitdiff
path: root/src/cmd/cgo
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2011-02-18 09:50:58 +0100
committerOndřej Surý <ondrej@sury.org>2011-02-18 09:50:58 +0100
commitc072558b90f1bbedc2022b0f30c8b1ac4712538e (patch)
tree67767591619e4bd8111fb05fac185cde94fb7378 /src/cmd/cgo
parent5859517b767c99749a45651c15d4bae5520ebae8 (diff)
downloadgolang-c072558b90f1bbedc2022b0f30c8b1ac4712538e.tar.gz
Imported Upstream version 2011.02.15upstream/2011.02.15
Diffstat (limited to 'src/cmd/cgo')
-rw-r--r--src/cmd/cgo/doc.go5
-rw-r--r--src/cmd/cgo/gcc.go59
-rw-r--r--src/cmd/cgo/util.go5
3 files changed, 54 insertions, 15 deletions
diff --git a/src/cmd/cgo/doc.go b/src/cmd/cgo/doc.go
index c4868345c..b3aa9aded 100644
--- a/src/cmd/cgo/doc.go
+++ b/src/cmd/cgo/doc.go
@@ -25,9 +25,12 @@ the package. For example:
CFLAGS and LDFLAGS may be defined with pseudo #cgo directives
within these comments to tweak the behavior of gcc. Values defined
-in multiple directives are concatenated together. For example:
+in multiple directives are concatenated together. Options prefixed
+by $GOOS, $GOARCH, or $GOOS/$GOARCH are only defined in matching
+systems. For example:
// #cgo CFLAGS: -DPNG_DEBUG=1
+ // #cgo linux CFLAGS: -DLINUX=1
// #cgo LDFLAGS: -lpng
// #include <png.h>
import "C"
diff --git a/src/cmd/cgo/gcc.go b/src/cmd/cgo/gcc.go
index cadc6fae9..e6ce21ed3 100644
--- a/src/cmd/cgo/gcc.go
+++ b/src/cmd/cgo/gcc.go
@@ -19,6 +19,7 @@ import (
"go/parser"
"go/token"
"os"
+ "runtime"
"strconv"
"strings"
"unicode"
@@ -66,6 +67,8 @@ func cname(s string) string {
func (p *Package) ParseFlags(f *File, srcfile string) {
linesIn := strings.Split(f.Preamble, "\n", -1)
linesOut := make([]string, 0, len(linesIn))
+
+NextLine:
for _, line := range linesIn {
l := strings.TrimSpace(line)
if len(l) < 5 || l[:4] != "#cgo" || !unicode.IsSpace(int(l[4])) {
@@ -79,11 +82,29 @@ func (p *Package) ParseFlags(f *File, srcfile string) {
fatal("%s: bad #cgo line: %s", srcfile, line)
}
- k := fields[0]
- v := strings.TrimSpace(fields[1])
+ 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 runtime.GOOS:
+ case runtime.GOARCH:
+ case runtime.GOOS + "/" + runtime.GOARCH:
+ default:
+ continue NextLine
+ }
+ default:
+ fatal("%s: bad #cgo option: %s", srcfile, fields[0])
+ }
+
if k != "CFLAGS" && k != "LDFLAGS" {
fatal("%s: unsupported #cgo option %s", srcfile, k)
}
+
+ v := strings.TrimSpace(fields[1])
args, err := splitQuoted(v)
if err != nil {
fatal("%s: bad #cgo option %s: %s", srcfile, k, err.String())
@@ -288,7 +309,7 @@ func (p *Package) guessKinds(f *File) []*Name {
var b bytes.Buffer
b.WriteString(builtinProlog)
b.WriteString(f.Preamble)
- b.WriteString("void f(void) {\n")
+ b.WriteString("void __cgo__f__(void) {\n")
b.WriteString("#line 0 \"cgo-test\"\n")
for i, n := range toSniff {
fmt.Fprintf(&b, "%s; enum { _cgo_enum_%d = %s }; /* cgo-test:%d */\n", n.C, i, n.C, i)
@@ -753,6 +774,8 @@ var dwarfToName = map[string]string{
"double complex": "complexdouble",
}
+const signedDelta = 64
+
// Type returns a *Type with the same memory layout as
// dtype when used as the type of a variable or a struct field.
func (c *typeConv) Type(dtype dwarf.Type) *Type {
@@ -818,7 +841,19 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type {
t.Align = 1
case *dwarf.EnumType:
- switch t.Size {
+ if t.Align = t.Size; t.Align >= c.ptrSize {
+ t.Align = c.ptrSize
+ }
+ t.C = "enum " + dt.EnumName
+ signed := 0
+ t.EnumValues = make(map[string]int64)
+ for _, ev := range dt.Val {
+ t.EnumValues[ev.Name] = ev.Val
+ if ev.Val < 0 {
+ signed = signedDelta
+ }
+ }
+ switch t.Size + int64(signed) {
default:
fatal("unexpected: %d-byte enum type - %s", t.Size, dtype)
case 1:
@@ -829,14 +864,14 @@ func (c *typeConv) Type(dtype dwarf.Type) *Type {
t.Go = c.uint32
case 8:
t.Go = c.uint64
- }
- if t.Align = t.Size; t.Align >= c.ptrSize {
- t.Align = c.ptrSize
- }
- t.C = "enum " + dt.EnumName
- t.EnumValues = make(map[string]int64)
- for _, ev := range dt.Val {
- t.EnumValues[ev.Name] = ev.Val
+ case 1 + signedDelta:
+ t.Go = c.int8
+ case 2 + signedDelta:
+ t.Go = c.int16
+ case 4 + signedDelta:
+ t.Go = c.int32
+ case 8 + signedDelta:
+ t.Go = c.int64
}
case *dwarf.FloatType:
diff --git a/src/cmd/cgo/util.go b/src/cmd/cgo/util.go
index a6f509dc4..59529a6d2 100644
--- a/src/cmd/cgo/util.go
+++ b/src/cmd/cgo/util.go
@@ -32,10 +32,11 @@ func run(stdin []byte, argv []string) (stdout, stderr []byte, ok bool) {
if err != nil {
fatal("%s", err)
}
- pid, err := os.ForkExec(cmd, argv, os.Environ(), "", []*os.File{r0, w1, w2})
+ p, err := os.StartProcess(cmd, argv, os.Environ(), "", []*os.File{r0, w1, w2})
if err != nil {
fatal("%s", err)
}
+ defer p.Release()
r0.Close()
w1.Close()
w2.Close()
@@ -55,7 +56,7 @@ func run(stdin []byte, argv []string) (stdout, stderr []byte, ok bool) {
<-c
<-c
- w, err := os.Wait(pid, 0)
+ w, err := p.Wait(0)
if err != nil {
fatal("%s", err)
}