summaryrefslogtreecommitdiff
path: root/misc/cgo
diff options
context:
space:
mode:
Diffstat (limited to 'misc/cgo')
-rw-r--r--misc/cgo/gmp/Makefile4
-rw-r--r--misc/cgo/gmp/gmp.go2
-rw-r--r--misc/cgo/gmp/pi.go2
-rw-r--r--misc/cgo/life/Makefile4
-rw-r--r--misc/cgo/stdio/Makefile2
-rw-r--r--misc/cgo/stdio/fib.go2
-rw-r--r--misc/cgo/test/Makefile18
-rw-r--r--misc/cgo/test/align.go4
-rw-r--r--misc/cgo/test/basic.go33
-rw-r--r--misc/cgo/test/callback.go4
-rw-r--r--misc/cgo/test/callback_c.c5
-rw-r--r--misc/cgo/test/env.go9
-rw-r--r--misc/cgo/test/helpers.go2
-rw-r--r--misc/cgo/test/issue1560.go10
-rw-r--r--misc/cgo/test/issue2462.go102
-rw-r--r--misc/cgo/test/sleep_windows.go16
-rw-r--r--misc/cgo/testso/Makefile22
-rw-r--r--misc/cgo/testso/cgoso.go16
-rw-r--r--misc/cgo/testso/cgoso_c.c9
-rw-r--r--misc/cgo/testso/main.go11
-rwxr-xr-xmisc/cgo/testso/test.bash9
21 files changed, 255 insertions, 31 deletions
diff --git a/misc/cgo/gmp/Makefile b/misc/cgo/gmp/Makefile
index fc6209f27..d9390c146 100644
--- a/misc/cgo/gmp/Makefile
+++ b/misc/cgo/gmp/Makefile
@@ -28,11 +28,11 @@ include ../../../src/Make.pkg
# Computes 1000 digits of pi; single-threaded.
pi: install pi.go
- $(GC) pi.go
+ $(GC) $(GCFLAGS) $(GCIMPORTS) pi.go
$(LD) -o $@ pi.$O
# Computes 200 Fibonacci numbers; multi-threaded.
fib: install fib.go
- $(GC) fib.go
+ $(GC) $(GCFLAGS) $(GCIMPORTS) fib.go
$(LD) -o $@ fib.$O
diff --git a/misc/cgo/gmp/gmp.go b/misc/cgo/gmp/gmp.go
index 3dbc022ce..9325d8bfd 100644
--- a/misc/cgo/gmp/gmp.go
+++ b/misc/cgo/gmp/gmp.go
@@ -179,7 +179,7 @@ func (z *Int) SetInt64(x int64) *Int {
// SetString interprets s as a number in the given base
// and sets z to that value. The base must be in the range [2,36].
// SetString returns an error if s cannot be parsed or the base is invalid.
-func (z *Int) SetString(s string, base int) os.Error {
+func (z *Int) SetString(s string, base int) error {
z.doinit()
if base < 2 || base > 36 {
return os.EINVAL
diff --git a/misc/cgo/gmp/pi.go b/misc/cgo/gmp/pi.go
index 45f61abbd..3e40624cf 100644
--- a/misc/cgo/gmp/pi.go
+++ b/misc/cgo/gmp/pi.go
@@ -38,8 +38,8 @@ POSSIBILITY OF SUCH DAMAGE.
package main
import (
- big "gmp"
"fmt"
+ big "gmp"
"runtime"
)
diff --git a/misc/cgo/life/Makefile b/misc/cgo/life/Makefile
index 39ec13be2..1568a67f6 100644
--- a/misc/cgo/life/Makefile
+++ b/misc/cgo/life/Makefile
@@ -11,7 +11,7 @@ CGOFILES=\
CGO_OFILES=\
c-life.o\
-
+
ifeq ($(GOOS),windows)
ifeq ($(GOARCH),amd64)
CGO_OFILES+=\
@@ -31,5 +31,5 @@ CLEANFILES+=life
include ../../../src/Make.pkg
life: install main.go
- $(GC) main.go
+ $(GC) $(GCFLAGS) $(GCIMPORTS) main.go
$(LD) -o $@ main.$O
diff --git a/misc/cgo/stdio/Makefile b/misc/cgo/stdio/Makefile
index 3f7a4c01c..586132b3c 100644
--- a/misc/cgo/stdio/Makefile
+++ b/misc/cgo/stdio/Makefile
@@ -13,5 +13,5 @@ CLEANFILES+=hello fib chain run.out
include ../../../src/Make.pkg
%: install %.go
- $(GC) $*.go
+ $(GC) $(GCFLAGS) $(GCIMPORTS) $*.go
$(LD) -o $@ $*.$O
diff --git a/misc/cgo/stdio/fib.go b/misc/cgo/stdio/fib.go
index c02e31fd8..431d9cefe 100644
--- a/misc/cgo/stdio/fib.go
+++ b/misc/cgo/stdio/fib.go
@@ -26,7 +26,7 @@ func fibber(c, out chan int64, i int64) {
}
for {
j := <-c
- stdio.Stdout.WriteString(strconv.Itoa64(j) + "\n")
+ stdio.Stdout.WriteString(strconv.FormatInt(j, 10) + "\n")
out <- j
<-out
i += j
diff --git a/misc/cgo/test/Makefile b/misc/cgo/test/Makefile
index 5617e78c3..4c1680d94 100644
--- a/misc/cgo/test/Makefile
+++ b/misc/cgo/test/Makefile
@@ -16,6 +16,7 @@ CGOFILES=\
issue1222.go\
issue1328.go\
issue1560.go\
+ issue2462.go\
duplicate_symbol.go\
CGO_OFILES=\
@@ -24,4 +25,21 @@ CGO_OFILES=\
OFILES=\
runtime.$O\
+ifeq ($(GOOS),windows)
+GCCVERSION=$(shell gcc -dumpversion)
+ifeq ($(GOARCH),386)
+GCCLIBDIR=/mingw/lib/gcc/mingw32/$(GCCVERSION)
+CHKSTK=_chkstk.o
+else
+GCCLIBDIR=/mingw/lib/gcc/x86_64-w64-mingw32/$(GCCVERSION)
+CHKSTK=_chkstk_ms.o
+endif
+
+CGOFILES+=sleep_windows.go
+CGO_OFILES+=$(CHKSTK)
+
+$(CHKSTK):
+ ar -x "$(GCCLIBDIR)/libgcc.a" $@
+endif
+
include ../../../src/Make.pkg
diff --git a/misc/cgo/test/align.go b/misc/cgo/test/align.go
index 07ab9ef50..a23b44fc3 100644
--- a/misc/cgo/test/align.go
+++ b/misc/cgo/test/align.go
@@ -1,3 +1,7 @@
+// Copyright 2010 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.
+
package cgotest
/*
diff --git a/misc/cgo/test/basic.go b/misc/cgo/test/basic.go
index 626e0e91b..7aaae1522 100644
--- a/misc/cgo/test/basic.go
+++ b/misc/cgo/test/basic.go
@@ -69,18 +69,7 @@ func uuidgen() {
C.uuid_generate(&uuid[0])
}
-func Size(name string) (int64, os.Error) {
- var st C.struct_stat
- p := C.CString(name)
- _, err := C.stat(p, &st)
- C.free(unsafe.Pointer(p))
- if err != nil {
- return 0, err
- }
- return int64(C.ulong(st.st_size)), nil
-}
-
-func Strtol(s string, base int) (int, os.Error) {
+func Strtol(s string, base int) (int, error) {
p := C.CString(s)
n, err := C.strtol(p, nil, C.int(base))
C.free(unsafe.Pointer(p))
@@ -112,9 +101,17 @@ func testAtol(t *testing.T) {
}
func testErrno(t *testing.T) {
- n, err := Strtol("asdf", 123)
- if n != 0 || err != os.EINVAL {
- t.Error("Strtol: ", n, err)
+ p := C.CString("no-such-file")
+ m := C.CString("r")
+ f, err := C.fopen(p, m)
+ C.free(unsafe.Pointer(p))
+ C.free(unsafe.Pointer(m))
+ if err == nil {
+ C.fclose(f)
+ t.Fatalf("C.fopen: should fail")
+ }
+ if err != os.ENOENT {
+ t.Fatalf("C.fopen: unexpected error: ", err)
}
}
@@ -128,9 +125,9 @@ func testMultipleAssign(t *testing.T) {
}
var (
- uint = (C.uint)(0)
- ulong C.ulong
- char C.char
+ cuint = (C.uint)(0)
+ culong C.ulong
+ cchar C.char
)
type Context struct {
diff --git a/misc/cgo/test/callback.go b/misc/cgo/test/callback.go
index d20790e87..ef852561b 100644
--- a/misc/cgo/test/callback.go
+++ b/misc/cgo/test/callback.go
@@ -1,3 +1,7 @@
+// 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.
+
package cgotest
/*
diff --git a/misc/cgo/test/callback_c.c b/misc/cgo/test/callback_c.c
index 5983a5e11..c296d70e0 100644
--- a/misc/cgo/test/callback_c.c
+++ b/misc/cgo/test/callback_c.c
@@ -8,5 +8,10 @@
void
callback(void *f)
{
+ // use some stack space
+ volatile char data[64*1024];
+
+ data[0] = 0;
goCallback(f);
+ data[sizeof(data)-1] = 0;
}
diff --git a/misc/cgo/test/env.go b/misc/cgo/test/env.go
index 1fb4e684c..8d3ba5877 100644
--- a/misc/cgo/test/env.go
+++ b/misc/cgo/test/env.go
@@ -10,12 +10,21 @@ package cgotest
import "C"
import (
"os"
+ "runtime"
"testing"
"unsafe"
)
// This is really an os package test but here for convenience.
func testSetEnv(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ // Go uses SetEnvironmentVariable on windows. Howerver,
+ // C runtime takes a *copy* at process startup of thei
+ // OS environment, and stores it in environ/envp.
+ // It is this copy that getenv/putenv manipulate.
+ t.Logf("skipping test")
+ return
+ }
const key = "CGO_OS_TEST_KEY"
const val = "CGO_OS_TEST_VALUE"
os.Setenv(key, val)
diff --git a/misc/cgo/test/helpers.go b/misc/cgo/test/helpers.go
index de14d19ab..890dcbdf1 100644
--- a/misc/cgo/test/helpers.go
+++ b/misc/cgo/test/helpers.go
@@ -29,7 +29,7 @@ var testPairs = []testPair{
func testHelpers(t *testing.T) {
for _, pair := range testPairs {
if !reflect.DeepEqual(pair.Got, pair.Want) {
- t.Errorf("%s: got %#v, want %#v", pair.Got, pair.Want)
+ t.Errorf("%s: got %#v, want %#v", pair.Name, pair.Got, pair.Want)
}
}
}
diff --git a/misc/cgo/test/issue1560.go b/misc/cgo/test/issue1560.go
index e534cce47..833b14ae6 100644
--- a/misc/cgo/test/issue1560.go
+++ b/misc/cgo/test/issue1560.go
@@ -7,6 +7,8 @@ package cgotest
/*
#include <unistd.h>
+unsigned int sleep(unsigned int seconds);
+
extern void BackgroundSleep(int);
void twoSleep(int n) {
BackgroundSleep(n);
@@ -36,11 +38,11 @@ func BackgroundSleep(n int) {
}
func testParallelSleep(t *testing.T) {
- dt := -time.Nanoseconds()
+ start := time.Now()
parallelSleep(1)
- dt += time.Nanoseconds()
+ dt := time.Now().Sub(start)
// bug used to run sleeps in serial, producing a 2-second delay.
- if dt >= 1.3e9 {
- t.Fatalf("parallel 1-second sleeps slept for %f seconds", float64(dt)/1e9)
+ if dt >= 1300*time.Millisecond {
+ t.Fatalf("parallel 1-second sleeps slept for %f seconds", dt.Seconds())
}
}
diff --git a/misc/cgo/test/issue2462.go b/misc/cgo/test/issue2462.go
new file mode 100644
index 000000000..12cd91d05
--- /dev/null
+++ b/misc/cgo/test/issue2462.go
@@ -0,0 +1,102 @@
+// 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.
+
+package cgotest
+
+import "C"
+
+//export exportbyte
+func exportbyte() byte {
+ return 0
+}
+
+//export exportbool
+func exportbool() bool {
+ return false
+}
+
+//export exportrune
+func exportrune() rune {
+ return 0
+}
+
+//export exporterror
+func exporterror() error {
+ return nil
+}
+
+//export exportint
+func exportint() int {
+ return 0
+}
+
+//export exportuint
+func exportuint() uint {
+ return 0
+}
+
+//export exportuintptr
+func exportuintptr() uintptr {
+ return (uintptr)(0)
+}
+
+//export exportint8
+func exportint8() int8 {
+ return 0
+}
+
+//export exportuint8
+func exportuint8() uint8 {
+ return 0
+}
+
+//export exportint16
+func exportint16() int16 {
+ return 0
+}
+
+//export exportuint16
+func exportuint16() uint16 {
+ return 0
+}
+
+//export exportint32
+func exportint32() int32 {
+ return 0
+}
+
+//export exportuint32
+func exportuint32() uint32 {
+ return 0
+}
+
+//export exportint64
+func exportint64() int64 {
+ return 0
+}
+
+//export exportuint64
+func exportuint64() uint64 {
+ return 0
+}
+
+//export exportfloat32
+func exportfloat32() float32 {
+ return 0
+}
+
+//export exportfloat64
+func exportfloat64() float64 {
+ return 0
+}
+
+//export exportcomplex64
+func exportcomplex64() complex64 {
+ return 0
+}
+
+//export exportcomplex128
+func exportcomplex128() complex128 {
+ return 0
+}
diff --git a/misc/cgo/test/sleep_windows.go b/misc/cgo/test/sleep_windows.go
new file mode 100644
index 000000000..007a1bb4c
--- /dev/null
+++ b/misc/cgo/test/sleep_windows.go
@@ -0,0 +1,16 @@
+// 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.
+
+package cgotest
+
+/*
+#include <windows.h>
+
+unsigned int sleep(unsigned int seconds) {
+ Sleep(1000 * seconds);
+ return 0;
+}
+
+*/
+import "C"
diff --git a/misc/cgo/testso/Makefile b/misc/cgo/testso/Makefile
new file mode 100644
index 000000000..e472cf212
--- /dev/null
+++ b/misc/cgo/testso/Makefile
@@ -0,0 +1,22 @@
+# 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.
+
+include ../../../src/Make.inc
+
+TARG=cgosotest
+
+CGO_DEPS+=libcgoso.so
+CGO_LDFLAGS+=-lcgoso -L.
+CLEANFILES+=out libcgoso.so
+CGOFILES=\
+ cgoso.go\
+
+include ../../../src/Make.pkg
+
+libcgoso.so: cgoso_c.c
+ gcc cgoso_c.c -fPIC -o $@ $(_CGO_CFLAGS_$(GOARCH)) $(_CGO_LDFLAGS_$(GOOS))
+
+out: install main.go
+ $(GC) $(GCFLAGS) $(GCIMPORTS) main.go
+ $(LD) -o $@ main.$O
diff --git a/misc/cgo/testso/cgoso.go b/misc/cgo/testso/cgoso.go
new file mode 100644
index 000000000..6eb9f40e3
--- /dev/null
+++ b/misc/cgo/testso/cgoso.go
@@ -0,0 +1,16 @@
+// 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.
+
+package cgosotest
+
+//void sofunc(void);
+import "C"
+
+func Test() {
+ C.sofunc()
+}
+
+//export goCallback
+func goCallback() {
+}
diff --git a/misc/cgo/testso/cgoso_c.c b/misc/cgo/testso/cgoso_c.c
new file mode 100644
index 000000000..e29f7e807
--- /dev/null
+++ b/misc/cgo/testso/cgoso_c.c
@@ -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.
+
+void sofunc(void)
+{
+ extern void goCallback(void);
+ goCallback();
+}
diff --git a/misc/cgo/testso/main.go b/misc/cgo/testso/main.go
new file mode 100644
index 000000000..672ab262b
--- /dev/null
+++ b/misc/cgo/testso/main.go
@@ -0,0 +1,11 @@
+// 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.
+
+package main
+
+import "cgosotest"
+
+func main() {
+ cgosotest.Test()
+}
diff --git a/misc/cgo/testso/test.bash b/misc/cgo/testso/test.bash
new file mode 100755
index 000000000..f275eb572
--- /dev/null
+++ b/misc/cgo/testso/test.bash
@@ -0,0 +1,9 @@
+#!/bin/sh
+# 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.
+
+set -e
+gomake out
+LD_LIBRARY_PATH=. ./out
+gomake clean