mirror of https://github.com/golang/go.git
misc/cgo/testplugin: add test of -buildmode=plugin
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>
This commit is contained in:
parent
c19382319a
commit
1df438f79c
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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")
|
||||
}
|
||||
|
|
@ -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")
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue