summaryrefslogtreecommitdiffstats
path: root/misc/cgo/testsovar
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--misc/cgo/testsovar/noso_test.go10
-rw-r--r--misc/cgo/testsovar/overlaydir_test.go78
-rw-r--r--misc/cgo/testsovar/so_test.go142
-rw-r--r--misc/cgo/testsovar/testdata/cgoso.go44
-rw-r--r--misc/cgo/testsovar/testdata/cgoso_c.c7
-rw-r--r--misc/cgo/testsovar/testdata/cgoso_c.h17
-rw-r--r--misc/cgo/testsovar/testdata/main.go13
7 files changed, 311 insertions, 0 deletions
diff --git a/misc/cgo/testsovar/noso_test.go b/misc/cgo/testsovar/noso_test.go
new file mode 100644
index 0000000..1014534
--- /dev/null
+++ b/misc/cgo/testsovar/noso_test.go
@@ -0,0 +1,10 @@
+// Copyright 2019 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.
+
+//go:build !cgo
+// +build !cgo
+
+package so_test
+
+// Nothing to test.
diff --git a/misc/cgo/testsovar/overlaydir_test.go b/misc/cgo/testsovar/overlaydir_test.go
new file mode 100644
index 0000000..09a1d51
--- /dev/null
+++ b/misc/cgo/testsovar/overlaydir_test.go
@@ -0,0 +1,78 @@
+// Copyright 2019 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 so_test
+
+import (
+ "io"
+ "os"
+ "path/filepath"
+ "strings"
+)
+
+// overlayDir makes a minimal-overhead copy of srcRoot in which new files may be added.
+//
+// TODO: Once we no longer need to support the misc module in GOPATH mode,
+// factor this function out into a package to reduce duplication.
+func overlayDir(dstRoot, srcRoot string) error {
+ dstRoot = filepath.Clean(dstRoot)
+ if err := os.MkdirAll(dstRoot, 0777); err != nil {
+ return err
+ }
+
+ srcRoot, err := filepath.Abs(srcRoot)
+ if err != nil {
+ return err
+ }
+
+ return filepath.Walk(srcRoot, func(srcPath string, info os.FileInfo, err error) error {
+ if err != nil || srcPath == srcRoot {
+ return err
+ }
+
+ suffix := strings.TrimPrefix(srcPath, srcRoot)
+ for len(suffix) > 0 && suffix[0] == filepath.Separator {
+ suffix = suffix[1:]
+ }
+ dstPath := filepath.Join(dstRoot, suffix)
+
+ perm := info.Mode() & os.ModePerm
+ if info.Mode()&os.ModeSymlink != 0 {
+ info, err = os.Stat(srcPath)
+ if err != nil {
+ return err
+ }
+ perm = info.Mode() & os.ModePerm
+ }
+
+ // Always copy directories (don't symlink them).
+ // If we add a file in the overlay, we don't want to add it in the original.
+ if info.IsDir() {
+ return os.MkdirAll(dstPath, perm|0200)
+ }
+
+ // If the OS supports symlinks, use them instead of copying bytes.
+ if err := os.Symlink(srcPath, dstPath); err == nil {
+ return nil
+ }
+
+ // Otherwise, copy the bytes.
+ src, err := os.Open(srcPath)
+ if err != nil {
+ return err
+ }
+ defer src.Close()
+
+ dst, err := os.OpenFile(dstPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, perm)
+ if err != nil {
+ return err
+ }
+
+ _, err = io.Copy(dst, src)
+ if closeErr := dst.Close(); err == nil {
+ err = closeErr
+ }
+ return err
+ })
+}
diff --git a/misc/cgo/testsovar/so_test.go b/misc/cgo/testsovar/so_test.go
new file mode 100644
index 0000000..6d14e32
--- /dev/null
+++ b/misc/cgo/testsovar/so_test.go
@@ -0,0 +1,142 @@
+// Copyright 2019 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.
+
+//go:build cgo
+// +build cgo
+
+package so_test
+
+import (
+ "log"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "runtime"
+ "strings"
+ "testing"
+)
+
+func requireTestSOSupported(t *testing.T) {
+ t.Helper()
+ switch runtime.GOARCH {
+ case "arm64":
+ if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
+ t.Skip("No exec facility on iOS.")
+ }
+ case "ppc64":
+ if runtime.GOOS == "linux" {
+ t.Skip("External linking not implemented on linux/ppc64 (issue #8912).")
+ }
+ }
+ if runtime.GOOS == "android" {
+ t.Skip("No exec facility on Android.")
+ }
+}
+
+func TestSO(t *testing.T) {
+ requireTestSOSupported(t)
+
+ GOPATH, err := os.MkdirTemp("", "cgosotest")
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer os.RemoveAll(GOPATH)
+
+ modRoot := filepath.Join(GOPATH, "src", "cgosotest")
+ if err := overlayDir(modRoot, "testdata"); err != nil {
+ log.Panic(err)
+ }
+ if err := os.WriteFile(filepath.Join(modRoot, "go.mod"), []byte("module cgosotest\n"), 0666); err != nil {
+ log.Panic(err)
+ }
+
+ cmd := exec.Command("go", "env", "CC", "GOGCCFLAGS")
+ cmd.Dir = modRoot
+ cmd.Stderr = new(strings.Builder)
+ cmd.Env = append(os.Environ(), "GOPATH="+GOPATH)
+ out, err := cmd.Output()
+ if err != nil {
+ t.Fatalf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, cmd.Stderr)
+ }
+ lines := strings.Split(string(out), "\n")
+ if len(lines) != 3 || lines[2] != "" {
+ t.Fatalf("Unexpected output from %s:\n%s", strings.Join(cmd.Args, " "), lines)
+ }
+
+ cc := lines[0]
+ if cc == "" {
+ t.Fatal("CC environment variable (go env CC) cannot be empty")
+ }
+ gogccflags := strings.Split(lines[1], " ")
+
+ // build shared object
+ ext := "so"
+ args := append(gogccflags, "-shared")
+ switch runtime.GOOS {
+ case "darwin", "ios":
+ ext = "dylib"
+ args = append(args, "-undefined", "suppress", "-flat_namespace")
+ case "windows":
+ ext = "dll"
+ args = append(args, "-DEXPORT_DLL")
+ // At least in mingw-clang it is not permitted to just name a .dll
+ // on the command line. You must name the corresponding import
+ // library instead, even though the dll is used when the executable is run.
+ args = append(args, "-Wl,-out-implib,libcgosotest.a")
+ case "aix":
+ ext = "so.1"
+ }
+ sofname := "libcgosotest." + ext
+ args = append(args, "-o", sofname, "cgoso_c.c")
+
+ cmd = exec.Command(cc, args...)
+ cmd.Dir = modRoot
+ cmd.Env = append(os.Environ(), "GOPATH="+GOPATH)
+ out, err = cmd.CombinedOutput()
+ if err != nil {
+ t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
+ }
+ t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
+
+ if runtime.GOOS == "aix" {
+ // Shared object must be wrapped by an archive
+ cmd = exec.Command("ar", "-X64", "-q", "libcgosotest.a", "libcgosotest.so.1")
+ cmd.Dir = modRoot
+ out, err = cmd.CombinedOutput()
+ if err != nil {
+ t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
+ }
+ }
+
+ cmd = exec.Command("go", "build", "-o", "main.exe", "main.go")
+ cmd.Dir = modRoot
+ cmd.Env = append(os.Environ(), "GOPATH="+GOPATH)
+ out, err = cmd.CombinedOutput()
+ if err != nil {
+ t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
+ }
+ t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
+
+ cmd = exec.Command("./main.exe")
+ cmd.Dir = modRoot
+ cmd.Env = append(os.Environ(), "GOPATH="+GOPATH)
+ if runtime.GOOS != "windows" {
+ s := "LD_LIBRARY_PATH"
+ if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
+ s = "DYLD_LIBRARY_PATH"
+ }
+ cmd.Env = append(os.Environ(), s+"=.")
+
+ // On FreeBSD 64-bit architectures, the 32-bit linker looks for
+ // different environment variables.
+ if runtime.GOOS == "freebsd" && runtime.GOARCH == "386" {
+ cmd.Env = append(cmd.Env, "LD_32_LIBRARY_PATH=.")
+ }
+ }
+ out, err = cmd.CombinedOutput()
+ if err != nil {
+ t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
+ }
+ t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
+}
diff --git a/misc/cgo/testsovar/testdata/cgoso.go b/misc/cgo/testsovar/testdata/cgoso.go
new file mode 100644
index 0000000..d9deb55
--- /dev/null
+++ b/misc/cgo/testsovar/testdata/cgoso.go
@@ -0,0 +1,44 @@
+// Copyright 2015 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
+
+// This test verifies that Go can access C variables
+// in shared object file via cgo.
+
+/*
+// intentionally write the same LDFLAGS differently
+// to test correct handling of LDFLAGS.
+#cgo windows CFLAGS: -DIMPORT_DLL
+#cgo linux LDFLAGS: -L. -lcgosotest
+#cgo dragonfly LDFLAGS: -L. -l cgosotest
+#cgo freebsd LDFLAGS: -L. -l cgosotest
+#cgo openbsd LDFLAGS: -L. -l cgosotest
+#cgo solaris LDFLAGS: -L. -lcgosotest
+#cgo netbsd LDFLAGS: -L. libcgosotest.so
+#cgo darwin LDFLAGS: -L. libcgosotest.dylib
+#cgo windows LDFLAGS: -L. libcgosotest.a
+#cgo aix LDFLAGS: -L. -l cgosotest
+
+#include "cgoso_c.h"
+
+const char* getVar() {
+ return exported_var;
+}
+*/
+import "C"
+
+import "fmt"
+
+func Test() {
+ const want = "Hello world"
+ got := C.GoString(C.getVar())
+ if got != want {
+ panic(fmt.Sprintf("testExportedVar: got %q, but want %q", got, want))
+ }
+ got = C.GoString(C.exported_var)
+ if got != want {
+ panic(fmt.Sprintf("testExportedVar: got %q, but want %q", got, want))
+ }
+}
diff --git a/misc/cgo/testsovar/testdata/cgoso_c.c b/misc/cgo/testsovar/testdata/cgoso_c.c
new file mode 100644
index 0000000..a448c01
--- /dev/null
+++ b/misc/cgo/testsovar/testdata/cgoso_c.c
@@ -0,0 +1,7 @@
+// Copyright 2015 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.
+
+// +build ignore
+
+const char *exported_var = "Hello world";
diff --git a/misc/cgo/testsovar/testdata/cgoso_c.h b/misc/cgo/testsovar/testdata/cgoso_c.h
new file mode 100644
index 0000000..640db7b
--- /dev/null
+++ b/misc/cgo/testsovar/testdata/cgoso_c.h
@@ -0,0 +1,17 @@
+// Copyright 2015 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.
+
+// +build ignore
+
+#ifdef WIN32
+#if defined(EXPORT_DLL)
+# define VAR __declspec(dllexport)
+#elif defined(IMPORT_DLL)
+# define VAR __declspec(dllimport)
+#endif
+#else
+# define VAR extern
+#endif
+
+VAR const char *exported_var;
diff --git a/misc/cgo/testsovar/testdata/main.go b/misc/cgo/testsovar/testdata/main.go
new file mode 100644
index 0000000..87b52ce
--- /dev/null
+++ b/misc/cgo/testsovar/testdata/main.go
@@ -0,0 +1,13 @@
+// Copyright 2015 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.
+
+// +build ignore
+
+package main
+
+import "cgosotest"
+
+func main() {
+ cgosotest.Test()
+}