]> Cypherpunks repositories - gostls13.git/commitdiff
misc/cgo/testplugin: add test of -buildmode=plugin
authorDavid Crawshaw <crawshaw@golang.org>
Fri, 26 Aug 2016 13:04:27 +0000 (09:04 -0400)
committerDavid Crawshaw <crawshaw@golang.org>
Fri, 16 Sep 2016 17:55:24 +0000 (17:55 +0000)
Change-Id: Ie9fea9814c850b084562ab2349b54d9ad9fa1f4a
Reviewed-on: https://go-review.googlesource.com/27825
Run-TryBot: David Crawshaw <crawshaw@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
misc/cgo/testplugin/src/common/common.go [new file with mode: 0644]
misc/cgo/testplugin/src/host/host.go [new file with mode: 0644]
misc/cgo/testplugin/src/plugin1/plugin1.go [new file with mode: 0644]
misc/cgo/testplugin/test.bash [new file with mode: 0755]
src/cmd/dist/test.go

diff --git a/misc/cgo/testplugin/src/common/common.go b/misc/cgo/testplugin/src/common/common.go
new file mode 100644 (file)
index 0000000..b064e6b
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright 2016 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 common
+
+var X int
+
+func init() {
+       X = 3
+}
diff --git a/misc/cgo/testplugin/src/host/host.go b/misc/cgo/testplugin/src/host/host.go
new file mode 100644 (file)
index 0000000..d11d660
--- /dev/null
@@ -0,0 +1,51 @@
+// Copyright 2016 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 (
+       "fmt"
+       "log"
+       "plugin"
+
+       "common"
+)
+
+func init() {
+       common.X *= 5
+}
+
+func main() {
+       if got, want := common.X, 3*5; got != want {
+               log.Fatalf("before plugin load common.X=%d, want %d", got, want)
+       }
+
+       p, err := plugin.Open("plugin1.so")
+       if err != nil {
+               log.Fatalf("plugin.Open failed: %v", err)
+       }
+
+       const wantX = 3 * 5 * 7
+       if got := common.X; got != wantX {
+               log.Fatalf("after plugin load common.X=%d, want %d", got, wantX)
+       }
+
+       seven, err := p.Lookup("Seven")
+       if err != nil {
+               log.Fatalf(`Lookup("Seven") failed: %v`, err)
+       }
+       if got, want := *seven.(*int), 7; got != want {
+               log.Fatalf("via lookup plugin1.Seven=%d, want %d", got, want)
+       }
+
+       readFunc, err := p.Lookup("ReadCommonX")
+       if err != nil {
+               log.Fatalf(`Lookup("ReadCommonX") failed: %v`, err)
+       }
+       if got := readFunc.(func() int)(); got != wantX {
+               log.Fatalf("via lookup plugin1.ReadCommonX()=%d, want %d", got, wantX)
+       }
+
+       fmt.Println("PASS")
+}
diff --git a/misc/cgo/testplugin/src/plugin1/plugin1.go b/misc/cgo/testplugin/src/plugin1/plugin1.go
new file mode 100644 (file)
index 0000000..7049596
--- /dev/null
@@ -0,0 +1,25 @@
+// Copyright 2016 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
+
+// // No C code required.
+import "C"
+
+import "common"
+
+func ReadCommonX() int {
+       return common.X
+}
+
+var Seven int
+
+func init() {
+       Seven = 7
+       common.X *= Seven
+}
+
+func main() {
+       panic("plugin1.main called")
+}
diff --git a/misc/cgo/testplugin/test.bash b/misc/cgo/testplugin/test.bash
new file mode 100755 (executable)
index 0000000..452d5c0
--- /dev/null
@@ -0,0 +1,27 @@
+#!/usr/bin/env bash
+# Copyright 2016 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
+
+if [ ! -f src/host/host.go ]; then
+       cwd=$(pwd)
+       echo "misc/cgo/testplugin/test.bash is running in $cwd" 1>&2
+       exit 1
+fi
+
+goos=$(go env GOOS)
+goarch=$(go env GOARCH)
+
+function cleanup() {
+       rm -f plugin1.so host pkg
+}
+trap cleanup EXIT
+
+rm -rf pkg
+
+GOPATH=$(pwd) go build -buildmode=plugin plugin1
+GOPATH=$(pwd) go build host
+
+LD_LIBRARY_PATH=$(pwd) ./host
index fbb4b40209f55b8de0eb99043e475fae6f93a6ba..4a1aa2ece296f5e6f9f7bf82e99afecee08a33d2 100644 (file)
@@ -540,6 +540,9 @@ func (t *tester) registerTests() {
                if t.supportedBuildmode("shared") {
                        t.registerTest("testshared", "../misc/cgo/testshared", "go", "test")
                }
+               if t.supportedBuildmode("plugin") {
+                       t.registerTest("testplugin", "../misc/cgo/testplugin", "./test.bash")
+               }
                if t.gohostos == "linux" && t.goarch == "amd64" {
                        t.registerTest("testasan", "../misc/cgo/testasan", "go", "run", "main.go")
                }
@@ -724,6 +727,12 @@ func (t *tester) supportedBuildmode(mode string) bool {
                        return true
                }
                return false
+       case "plugin":
+               switch pair {
+               case "linux-386", "linux-amd64", "linux-arm", "linux-arm64", "linux-ppc64le", "linux-s390x":
+                       return true
+               }
+               return false
        default:
                log.Fatalf("internal error: unknown buildmode %s", mode)
                return false