From: David Crawshaw Date: Fri, 26 Aug 2016 13:04:27 +0000 (-0400) Subject: misc/cgo/testplugin: add test of -buildmode=plugin X-Git-Tag: go1.8beta1~1273 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=1df438f79c440ddf9bdd342f089a55567254bc9a;p=gostls13.git misc/cgo/testplugin: add test of -buildmode=plugin Change-Id: Ie9fea9814c850b084562ab2349b54d9ad9fa1f4a Reviewed-on: https://go-review.googlesource.com/27825 Run-TryBot: David Crawshaw TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- diff --git a/misc/cgo/testplugin/src/common/common.go b/misc/cgo/testplugin/src/common/common.go new file mode 100644 index 0000000000..b064e6bccf --- /dev/null +++ b/misc/cgo/testplugin/src/common/common.go @@ -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 index 0000000000..d11d660d95 --- /dev/null +++ b/misc/cgo/testplugin/src/host/host.go @@ -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 index 0000000000..704959672f --- /dev/null +++ b/misc/cgo/testplugin/src/plugin1/plugin1.go @@ -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 index 0000000000..452d5c0a59 --- /dev/null +++ b/misc/cgo/testplugin/test.bash @@ -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 diff --git a/src/cmd/dist/test.go b/src/cmd/dist/test.go index fbb4b40209..4a1aa2ece2 100644 --- a/src/cmd/dist/test.go +++ b/src/cmd/dist/test.go @@ -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