]> Cypherpunks repositories - gostls13.git/commitdiff
go/build: include processing of .c files for cgo packages
authorAlex Brainman <alex.brainman@gmail.com>
Tue, 12 Jul 2011 07:27:07 +0000 (17:27 +1000)
committerAlex Brainman <alex.brainman@gmail.com>
Tue, 12 Jul 2011 07:27:07 +0000 (17:27 +1000)
R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/4696041

src/pkg/go/build/build.go
src/pkg/go/build/cgotest/cgotest.c [new file with mode: 0644]
src/pkg/go/build/cgotest/cgotest.go
src/pkg/go/build/cgotest/cgotest.h [new file with mode: 0644]

index de22a5a65f24bb0bd206ff24b71a336a94d3e962..97f92bfb6e7a352d0e476ef0791d7f76a669929c 100644 (file)
@@ -61,7 +61,9 @@ func Build(tree *Tree, pkg string, info *DirInfo) (*Script, os.Error) {
        if len(info.CgoFiles) > 0 {
                cgoFiles := b.abss(info.CgoFiles...)
                s.addInput(cgoFiles...)
-               outGo, outObj := b.cgo(cgoFiles)
+               cgoCFiles := b.abss(info.CFiles...)
+               s.addInput(cgoCFiles...)
+               outGo, outObj := b.cgo(cgoFiles, cgoCFiles)
                gofiles = append(gofiles, outGo...)
                ofiles = append(ofiles, outObj...)
                s.addIntermediate(outGo...)
@@ -370,7 +372,7 @@ func (b *build) gccArgs(args ...string) []string {
 
 var cgoRe = regexp.MustCompile(`[/\\:]`)
 
-func (b *build) cgo(cgofiles []string) (outGo, outObj []string) {
+func (b *build) cgo(cgofiles, cgocfiles []string) (outGo, outObj []string) {
        // cgo
        // TODO(adg): CGOPKGPATH
        // TODO(adg): CGO_FLAGS
@@ -413,6 +415,12 @@ func (b *build) cgo(cgofiles []string) (outGo, outObj []string) {
                        b.script.addIntermediate(ofile)
                }
        }
+       for _, cfile := range cgocfiles {
+               ofile := b.obj + cgoRe.ReplaceAllString(cfile[:len(cfile)-1], "_") + "o"
+               b.gccCompile(ofile, cfile)
+               linkobj = append(linkobj, ofile)
+               outObj = append(outObj, ofile)
+       }
        dynObj := b.obj + "_cgo_.o"
        b.gccLink(dynObj, linkobj...)
        b.script.addIntermediate(dynObj)
diff --git a/src/pkg/go/build/cgotest/cgotest.c b/src/pkg/go/build/cgotest/cgotest.c
new file mode 100644 (file)
index 0000000..b13acb2
--- /dev/null
@@ -0,0 +1,9 @@
+// Copyright 2011 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.
+
+int
+Add(int x, int y, int *sum)
+{
+       sum = x+y;
+}
index 32b9318614a07da8612a3543af3805740b0196b3..93bbf06883f39b3730a55732e23a23a873162373 100644 (file)
@@ -7,6 +7,13 @@ package cgotest
 /*
 char* greeting = "hello, world";
 */
+// #include "cgotest.h"
 import "C"
+import "unsafe"
 
 var Greeting = C.GoString(C.greeting)
+
+func DoAdd(x, y int) (sum int) {
+       C.Add(C.int(x), C.int(y), (*C.int)(unsafe.Pointer(&sum)))
+       return
+}
diff --git a/src/pkg/go/build/cgotest/cgotest.h b/src/pkg/go/build/cgotest/cgotest.h
new file mode 100644 (file)
index 0000000..9c73643
--- /dev/null
@@ -0,0 +1,5 @@
+// Copyright 2011 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.
+
+extern int Add(int, int, int *);