2016-11-27 17:05:01 -07:00
|
|
|
// Copyright 2017 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 cshared_test
|
|
|
|
|
|
|
|
import (
|
2019-02-21 14:18:28 -07:00
|
|
|
"bytes"
|
2016-11-27 17:05:01 -07:00
|
|
|
"debug/elf"
|
|
|
|
"fmt"
|
2018-01-10 18:48:37 -07:00
|
|
|
"io/ioutil"
|
2016-11-27 17:05:01 -07:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2017-08-19 04:17:46 -06:00
|
|
|
"sync"
|
2016-11-27 17:05:01 -07:00
|
|
|
"testing"
|
|
|
|
"unicode"
|
|
|
|
)
|
|
|
|
|
|
|
|
// C compiler with args (from $(go env CC) $(go env GOGCCFLAGS)).
|
|
|
|
var cc []string
|
|
|
|
|
|
|
|
// ".exe" on Windows.
|
|
|
|
var exeSuffix string
|
|
|
|
|
|
|
|
var GOOS, GOARCH, GOROOT string
|
2017-08-19 05:11:09 -06:00
|
|
|
var installdir, androiddir string
|
2016-11-27 17:05:01 -07:00
|
|
|
var libSuffix, libgoname string
|
|
|
|
|
2017-08-19 04:17:46 -06:00
|
|
|
func TestMain(m *testing.M) {
|
2019-02-21 14:18:28 -07:00
|
|
|
os.Exit(testMain(m))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMain(m *testing.M) int {
|
|
|
|
log.SetFlags(log.Lshortfile)
|
|
|
|
|
2016-11-27 17:05:01 -07:00
|
|
|
GOOS = goEnv("GOOS")
|
|
|
|
GOARCH = goEnv("GOARCH")
|
|
|
|
GOROOT = goEnv("GOROOT")
|
|
|
|
|
|
|
|
if _, err := os.Stat(GOROOT); os.IsNotExist(err) {
|
|
|
|
log.Fatalf("Unable able to find GOROOT at '%s'", GOROOT)
|
|
|
|
}
|
|
|
|
|
|
|
|
androiddir = fmt.Sprintf("/data/local/tmp/testcshared-%d", os.Getpid())
|
2017-09-09 23:26:19 -06:00
|
|
|
if GOOS == "android" {
|
2018-01-20 11:56:51 -07:00
|
|
|
args := append(adbCmd(), "shell", "mkdir", "-p", androiddir)
|
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
2017-09-09 23:26:19 -06:00
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("setupAndroid failed: %v\n%s\n", err, out)
|
|
|
|
}
|
2019-02-21 14:18:28 -07:00
|
|
|
defer cleanupAndroid()
|
2017-09-09 23:26:19 -06:00
|
|
|
}
|
|
|
|
|
2017-09-10 19:58:17 -06:00
|
|
|
cc = []string{goEnv("CC")}
|
2016-11-27 17:05:01 -07:00
|
|
|
|
|
|
|
out := goEnv("GOGCCFLAGS")
|
|
|
|
quote := '\000'
|
|
|
|
start := 0
|
|
|
|
lastSpace := true
|
|
|
|
backslash := false
|
|
|
|
s := string(out)
|
|
|
|
for i, c := range s {
|
|
|
|
if quote == '\000' && unicode.IsSpace(c) {
|
|
|
|
if !lastSpace {
|
|
|
|
cc = append(cc, s[start:i])
|
|
|
|
lastSpace = true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if lastSpace {
|
|
|
|
start = i
|
|
|
|
lastSpace = false
|
|
|
|
}
|
|
|
|
if quote == '\000' && !backslash && (c == '"' || c == '\'') {
|
|
|
|
quote = c
|
|
|
|
backslash = false
|
|
|
|
} else if !backslash && quote == c {
|
|
|
|
quote = '\000'
|
|
|
|
} else if (quote == '\000' || quote == '"') && !backslash && c == '\\' {
|
|
|
|
backslash = true
|
|
|
|
} else {
|
|
|
|
backslash = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !lastSpace {
|
|
|
|
cc = append(cc, s[start:])
|
|
|
|
}
|
|
|
|
|
2017-08-19 05:11:09 -06:00
|
|
|
switch GOOS {
|
|
|
|
case "darwin":
|
2016-11-27 17:05:01 -07:00
|
|
|
// For Darwin/ARM.
|
|
|
|
// TODO(crawshaw): can we do better?
|
|
|
|
cc = append(cc, []string{"-framework", "CoreFoundation", "-framework", "Foundation"}...)
|
2017-08-19 05:11:09 -06:00
|
|
|
case "android":
|
|
|
|
cc = append(cc, "-pie", "-fuse-ld=gold")
|
2016-11-27 17:05:01 -07:00
|
|
|
}
|
|
|
|
libgodir := GOOS + "_" + GOARCH
|
|
|
|
switch GOOS {
|
|
|
|
case "darwin":
|
|
|
|
if GOARCH == "arm" || GOARCH == "arm64" {
|
|
|
|
libgodir += "_shared"
|
|
|
|
}
|
|
|
|
case "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "solaris":
|
|
|
|
libgodir += "_shared"
|
|
|
|
}
|
|
|
|
cc = append(cc, "-I", filepath.Join("pkg", libgodir))
|
|
|
|
|
|
|
|
if GOOS == "windows" {
|
|
|
|
exeSuffix = ".exe"
|
|
|
|
}
|
2017-08-19 04:17:46 -06:00
|
|
|
|
2019-02-21 14:18:28 -07:00
|
|
|
// Copy testdata into GOPATH/src/testcshared, along with a go.mod file
|
|
|
|
// declaring the same path.
|
|
|
|
|
|
|
|
GOPATH, err := ioutil.TempDir("", "cshared_test")
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(GOPATH)
|
|
|
|
os.Setenv("GOPATH", GOPATH)
|
|
|
|
|
|
|
|
// Copy testdata into GOPATH/src/testarchive, along with a go.mod file
|
|
|
|
// declaring the same path.
|
|
|
|
modRoot := filepath.Join(GOPATH, "src", "testcshared")
|
|
|
|
if err := overlayDir(modRoot, "testdata"); err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
if err := os.Chdir(modRoot); err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2019-02-25 20:09:46 -07:00
|
|
|
os.Setenv("PWD", modRoot)
|
2019-02-21 14:18:28 -07:00
|
|
|
if err := ioutil.WriteFile("go.mod", []byte("module testcshared\n"), 0666); err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2017-08-19 04:17:46 -06:00
|
|
|
|
2019-02-21 14:18:28 -07:00
|
|
|
// Directory where cgo headers and outputs will be installed.
|
|
|
|
// The installation directory format varies depending on the platform.
|
|
|
|
output, err := exec.Command("go", "list",
|
|
|
|
"-buildmode=c-shared",
|
|
|
|
"-installsuffix", "testcshared",
|
|
|
|
"-f", "{{.Target}}",
|
|
|
|
"./libgo").CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("go list failed: %v\n%s", err, output)
|
|
|
|
}
|
|
|
|
target := string(bytes.TrimSpace(output))
|
|
|
|
libgoname = filepath.Base(target)
|
|
|
|
installdir = filepath.Dir(target)
|
|
|
|
libSuffix = strings.TrimPrefix(filepath.Ext(target), ".")
|
2017-08-19 04:17:46 -06:00
|
|
|
|
2019-02-21 14:18:28 -07:00
|
|
|
return m.Run()
|
2016-11-27 17:05:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func goEnv(key string) string {
|
|
|
|
out, err := exec.Command("go", "env", key).Output()
|
|
|
|
if err != nil {
|
2019-02-21 14:18:28 -07:00
|
|
|
log.Printf("go env %s failed:\n%s", key, err)
|
|
|
|
log.Panicf("%s", err.(*exec.ExitError).Stderr)
|
2016-11-27 17:05:01 -07:00
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(out))
|
|
|
|
}
|
|
|
|
|
2017-09-10 19:58:17 -06:00
|
|
|
func cmdToRun(name string) string {
|
|
|
|
return "./" + name + exeSuffix
|
2016-11-27 17:05:01 -07:00
|
|
|
}
|
|
|
|
|
2018-01-20 11:56:51 -07:00
|
|
|
func adbCmd() []string {
|
|
|
|
cmd := []string{"adb"}
|
|
|
|
if flags := os.Getenv("GOANDROID_ADB_FLAGS"); flags != "" {
|
|
|
|
cmd = append(cmd, strings.Split(flags, " ")...)
|
|
|
|
}
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2016-11-27 17:05:01 -07:00
|
|
|
func adbPush(t *testing.T, filename string) {
|
|
|
|
if GOOS != "android" {
|
|
|
|
return
|
|
|
|
}
|
2018-01-20 11:56:51 -07:00
|
|
|
args := append(adbCmd(), "push", filename, fmt.Sprintf("%s/%s", androiddir, filename))
|
2016-11-27 17:05:01 -07:00
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
|
|
if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
|
t.Fatalf("adb command failed: %v\n%s\n", err, out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-19 05:11:09 -06:00
|
|
|
func adbRun(t *testing.T, env []string, adbargs ...string) string {
|
2016-11-27 17:05:01 -07:00
|
|
|
if GOOS != "android" {
|
|
|
|
t.Fatalf("trying to run adb command when operating system is not android.")
|
|
|
|
}
|
2018-01-20 11:56:51 -07:00
|
|
|
args := append(adbCmd(), "shell")
|
2017-08-19 05:11:09 -06:00
|
|
|
// Propagate LD_LIBRARY_PATH to the adb shell invocation.
|
|
|
|
for _, e := range env {
|
|
|
|
if strings.Index(e, "LD_LIBRARY_PATH=") != -1 {
|
|
|
|
adbargs = append([]string{e}, adbargs...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-08-18 00:34:44 -06:00
|
|
|
shellcmd := fmt.Sprintf("cd %s; %s", androiddir, strings.Join(adbargs, " "))
|
|
|
|
args = append(args, shellcmd)
|
2016-11-27 17:05:01 -07:00
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("adb command failed: %v\n%s\n", err, out)
|
|
|
|
}
|
|
|
|
return strings.Replace(string(out), "\r", "", -1)
|
|
|
|
}
|
|
|
|
|
2019-02-21 14:18:28 -07:00
|
|
|
func run(t *testing.T, extraEnv []string, args ...string) string {
|
2017-10-05 00:43:38 -06:00
|
|
|
t.Helper()
|
2016-11-27 17:05:01 -07:00
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
2019-02-21 14:18:28 -07:00
|
|
|
if len(extraEnv) > 0 {
|
|
|
|
cmd.Env = append(os.Environ(), extraEnv...)
|
|
|
|
}
|
2018-08-01 15:50:11 -06:00
|
|
|
|
|
|
|
if GOOS != "windows" {
|
|
|
|
// TestUnexportedSymbols relies on file descriptor 30
|
|
|
|
// being closed when the program starts, so enforce
|
|
|
|
// that in all cases. (The first three descriptors are
|
|
|
|
// stdin/stdout/stderr, so we just need to make sure
|
|
|
|
// that cmd.ExtraFiles[27] exists and is nil.)
|
|
|
|
cmd.ExtraFiles = make([]*os.File, 28)
|
|
|
|
}
|
|
|
|
|
2016-11-27 17:05:01 -07:00
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("command failed: %v\n%v\n%s\n", args, err, out)
|
|
|
|
} else {
|
|
|
|
t.Logf("run: %v", args)
|
|
|
|
}
|
|
|
|
return string(out)
|
|
|
|
}
|
|
|
|
|
2019-02-21 14:18:28 -07:00
|
|
|
func runExe(t *testing.T, extraEnv []string, args ...string) string {
|
2017-10-05 00:43:38 -06:00
|
|
|
t.Helper()
|
2017-08-19 05:11:09 -06:00
|
|
|
if GOOS == "android" {
|
2019-02-21 14:18:28 -07:00
|
|
|
return adbRun(t, append(os.Environ(), extraEnv...), args...)
|
2017-08-19 05:11:09 -06:00
|
|
|
}
|
2019-02-21 14:18:28 -07:00
|
|
|
return run(t, extraEnv, args...)
|
2016-11-27 17:05:01 -07:00
|
|
|
}
|
|
|
|
|
2017-09-10 19:58:17 -06:00
|
|
|
func runCC(t *testing.T, args ...string) string {
|
2017-10-05 00:43:38 -06:00
|
|
|
t.Helper()
|
2017-10-08 23:00:34 -06:00
|
|
|
// This function is run in parallel, so append to a copy of cc
|
|
|
|
// rather than cc itself.
|
|
|
|
return run(t, nil, append(append([]string(nil), cc...), args...)...)
|
2016-11-27 17:05:01 -07:00
|
|
|
}
|
|
|
|
|
2017-08-19 04:17:46 -06:00
|
|
|
func createHeaders() error {
|
cmd/go: do not install dependencies during "go install"
This CL makes "go install" behave the way many users expect:
install only the things named on the command line.
Future builds still run as fast, thanks to the new build cache (CL 75473).
To install dependencies as well (the old behavior), use "go install -i".
Actual definitions aside, what most users know and expect of "go install"
is that (1) it installs what you asked, and (2) it's fast, unlike "go build".
It was fast because it installed dependencies, but installing dependencies
confused users repeatedly (see for example #5065, #6424, #10998, #12329,
"go build" and "go test" so that they could be "fast" too, but that only
created new opportunities for confusion. We also had to add -installsuffix
and then -pkgdir, to allow "fast" even when dependencies could not be
installed in the usual place.
The recent introduction of precise content-based staleness logic means that
the go command detects the need for rebuilding packages more often than it
used to, with the consequence that "go install" rebuilds and reinstalls
dependencies more than it used to. This will create more new opportunities
for confusion and will certainly lead to more issues filed like the ones
listed above.
CL 75743 introduced a build cache, separate from the install locations.
That cache makes all operations equally incremental and fast, whether or
not the operation is "install" or "build", and whether or not "-i" is used.
Installing dependencies is no longer necessary for speed, it has confused
users in the past, and the more accurate rebuilds mean that it will confuse
users even more often in the future. This CL aims to end all that confusion
by not installing dependencies by default.
By analogy with "go build -i" and "go test -i", which still install
dependencies, this CL introduces "go install -i", which installs
dependencies in addition to the things named on the command line.
Fixes #5065.
Fixes #6424.
Fixes #10998.
Fixes #12329.
Fixes #18981.
Fixes #22469.
Another step toward #4719.
Change-Id: I3d7bc145c3a680e2f26416e182fa0dcf1e2a15e5
Reviewed-on: https://go-review.googlesource.com/75850
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-11-02 23:24:19 -06:00
|
|
|
args := []string{"go", "install", "-i", "-buildmode=c-shared",
|
2019-02-21 14:18:28 -07:00
|
|
|
"-installsuffix", "testcshared", "./libgo"}
|
2017-08-19 04:17:46 -06:00
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("command failed: %v\n%v\n%s\n", args, err, out)
|
|
|
|
}
|
2016-11-27 17:05:01 -07:00
|
|
|
|
2017-08-19 04:17:46 -06:00
|
|
|
args = []string{"go", "build", "-buildmode=c-shared",
|
|
|
|
"-installsuffix", "testcshared",
|
|
|
|
"-o", libgoname,
|
2019-02-21 14:18:28 -07:00
|
|
|
filepath.Join(".", "libgo", "libgo.go")}
|
2017-08-19 04:17:46 -06:00
|
|
|
cmd = exec.Command(args[0], args[1:]...)
|
|
|
|
out, err = cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("command failed: %v\n%v\n%s\n", args, err, out)
|
|
|
|
}
|
2016-11-27 17:05:01 -07:00
|
|
|
|
2017-08-19 04:17:46 -06:00
|
|
|
if GOOS == "android" {
|
2018-01-20 11:56:51 -07:00
|
|
|
args = append(adbCmd(), "push", libgoname, fmt.Sprintf("%s/%s", androiddir, libgoname))
|
2017-08-19 04:17:46 -06:00
|
|
|
cmd = exec.Command(args[0], args[1:]...)
|
|
|
|
out, err = cmd.CombinedOutput()
|
2016-11-27 17:05:01 -07:00
|
|
|
if err != nil {
|
2017-08-19 04:17:46 -06:00
|
|
|
return fmt.Errorf("adb command failed: %v\n%s\n", err, out)
|
2016-11-27 17:05:01 -07:00
|
|
|
}
|
|
|
|
}
|
2017-08-19 04:17:46 -06:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
headersOnce sync.Once
|
|
|
|
headersErr error
|
|
|
|
)
|
|
|
|
|
|
|
|
func createHeadersOnce(t *testing.T) {
|
|
|
|
headersOnce.Do(func() {
|
|
|
|
headersErr = createHeaders()
|
|
|
|
})
|
|
|
|
if headersErr != nil {
|
|
|
|
t.Fatal(headersErr)
|
|
|
|
}
|
2016-11-27 17:05:01 -07:00
|
|
|
}
|
|
|
|
|
2017-08-19 04:17:46 -06:00
|
|
|
func cleanupAndroid() {
|
2016-11-27 17:05:01 -07:00
|
|
|
if GOOS != "android" {
|
|
|
|
return
|
|
|
|
}
|
2018-01-20 11:56:51 -07:00
|
|
|
args := append(adbCmd(), "shell", "rm", "-rf", androiddir)
|
|
|
|
cmd := exec.Command(args[0], args[1:]...)
|
2017-08-19 04:17:46 -06:00
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
2019-02-21 14:18:28 -07:00
|
|
|
log.Panicf("cleanupAndroid failed: %v\n%s\n", err, out)
|
2017-08-19 04:17:46 -06:00
|
|
|
}
|
2016-11-27 17:05:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// test0: exported symbols in shared lib are accessible.
|
|
|
|
func TestExportedSymbols(t *testing.T) {
|
2017-09-09 23:26:19 -06:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cmd := "testp0"
|
2017-10-05 19:55:20 -06:00
|
|
|
bin := cmdToRun(cmd)
|
2016-11-27 17:05:01 -07:00
|
|
|
|
2017-08-19 04:17:46 -06:00
|
|
|
createHeadersOnce(t)
|
2016-11-27 17:05:01 -07:00
|
|
|
|
2017-09-10 19:58:17 -06:00
|
|
|
runCC(t, "-I", installdir, "-o", cmd, "main0.c", libgoname)
|
2016-11-27 17:05:01 -07:00
|
|
|
adbPush(t, cmd)
|
|
|
|
|
2017-10-05 19:55:20 -06:00
|
|
|
defer os.Remove(bin)
|
2016-11-27 17:05:01 -07:00
|
|
|
|
2019-02-21 14:18:28 -07:00
|
|
|
out := runExe(t, []string{"LD_LIBRARY_PATH=."}, bin)
|
2016-11-27 17:05:01 -07:00
|
|
|
if strings.TrimSpace(out) != "PASS" {
|
|
|
|
t.Error(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test1: shared library can be dynamically loaded and exported symbols are accessible.
|
|
|
|
func TestExportedSymbolsWithDynamicLoad(t *testing.T) {
|
2017-09-09 23:26:19 -06:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-10-05 00:43:38 -06:00
|
|
|
if GOOS == "windows" {
|
|
|
|
t.Logf("Skipping on %s", GOOS)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-09 23:26:19 -06:00
|
|
|
cmd := "testp1"
|
2017-10-05 19:55:20 -06:00
|
|
|
bin := cmdToRun(cmd)
|
2016-11-27 17:05:01 -07:00
|
|
|
|
2017-08-19 04:17:46 -06:00
|
|
|
createHeadersOnce(t)
|
2016-11-27 17:05:01 -07:00
|
|
|
|
2018-02-13 20:00:17 -07:00
|
|
|
if GOOS != "freebsd" {
|
|
|
|
runCC(t, "-o", cmd, "main1.c", "-ldl")
|
|
|
|
} else {
|
|
|
|
runCC(t, "-o", cmd, "main1.c")
|
|
|
|
}
|
2016-11-27 17:05:01 -07:00
|
|
|
adbPush(t, cmd)
|
|
|
|
|
2017-10-05 19:55:20 -06:00
|
|
|
defer os.Remove(bin)
|
2016-11-27 17:05:01 -07:00
|
|
|
|
2017-10-05 19:55:20 -06:00
|
|
|
out := runExe(t, nil, bin, "./"+libgoname)
|
2016-11-27 17:05:01 -07:00
|
|
|
if strings.TrimSpace(out) != "PASS" {
|
|
|
|
t.Error(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test2: tests libgo2 which does not export any functions.
|
|
|
|
func TestUnexportedSymbols(t *testing.T) {
|
2017-09-09 23:26:19 -06:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-10-05 00:43:38 -06:00
|
|
|
if GOOS == "windows" {
|
|
|
|
t.Logf("Skipping on %s", GOOS)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-11-27 17:05:01 -07:00
|
|
|
cmd := "testp2"
|
2017-10-05 19:55:20 -06:00
|
|
|
bin := cmdToRun(cmd)
|
2016-11-27 17:05:01 -07:00
|
|
|
libname := "libgo2." + libSuffix
|
|
|
|
|
2017-09-10 19:58:17 -06:00
|
|
|
run(t,
|
2019-02-21 14:18:28 -07:00
|
|
|
nil,
|
2016-11-27 17:05:01 -07:00
|
|
|
"go", "build",
|
|
|
|
"-buildmode=c-shared",
|
|
|
|
"-installsuffix", "testcshared",
|
2019-02-21 14:18:28 -07:00
|
|
|
"-o", libname, "./libgo2",
|
2016-11-27 17:05:01 -07:00
|
|
|
)
|
|
|
|
adbPush(t, libname)
|
|
|
|
|
|
|
|
linkFlags := "-Wl,--no-as-needed"
|
|
|
|
if GOOS == "darwin" {
|
|
|
|
linkFlags = ""
|
|
|
|
}
|
|
|
|
|
2017-09-10 19:58:17 -06:00
|
|
|
runCC(t, "-o", cmd, "main2.c", linkFlags, libname)
|
2016-11-27 17:05:01 -07:00
|
|
|
adbPush(t, cmd)
|
|
|
|
|
|
|
|
defer os.Remove(libname)
|
2017-10-05 19:55:20 -06:00
|
|
|
defer os.Remove(bin)
|
2016-11-27 17:05:01 -07:00
|
|
|
|
2019-02-21 14:18:28 -07:00
|
|
|
out := runExe(t, []string{"LD_LIBRARY_PATH=."}, bin)
|
2016-11-27 17:05:01 -07:00
|
|
|
|
|
|
|
if strings.TrimSpace(out) != "PASS" {
|
|
|
|
t.Error(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// test3: tests main.main is exported on android.
|
|
|
|
func TestMainExportedOnAndroid(t *testing.T) {
|
2017-09-09 23:26:19 -06:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-10-05 00:43:38 -06:00
|
|
|
switch GOOS {
|
|
|
|
case "android":
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
t.Logf("Skipping on %s", GOOS)
|
2016-11-27 17:05:01 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd := "testp3"
|
2017-10-05 19:55:20 -06:00
|
|
|
bin := cmdToRun(cmd)
|
2016-11-27 17:05:01 -07:00
|
|
|
|
2017-08-19 04:17:46 -06:00
|
|
|
createHeadersOnce(t)
|
2016-11-27 17:05:01 -07:00
|
|
|
|
2017-09-10 19:58:17 -06:00
|
|
|
runCC(t, "-o", cmd, "main3.c", "-ldl")
|
2016-11-27 17:05:01 -07:00
|
|
|
adbPush(t, cmd)
|
|
|
|
|
2017-10-05 19:55:20 -06:00
|
|
|
defer os.Remove(bin)
|
2016-11-27 17:05:01 -07:00
|
|
|
|
2017-10-05 19:55:20 -06:00
|
|
|
out := runExe(t, nil, bin, "./"+libgoname)
|
2016-11-27 17:05:01 -07:00
|
|
|
if strings.TrimSpace(out) != "PASS" {
|
|
|
|
t.Error(out)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-10 19:58:17 -06:00
|
|
|
func testSignalHandlers(t *testing.T, pkgname, cfile, cmd string) {
|
|
|
|
libname := pkgname + "." + libSuffix
|
|
|
|
run(t,
|
2019-02-21 14:18:28 -07:00
|
|
|
nil,
|
2016-11-27 17:05:01 -07:00
|
|
|
"go", "build",
|
|
|
|
"-buildmode=c-shared",
|
|
|
|
"-installsuffix", "testcshared",
|
2017-09-10 19:58:17 -06:00
|
|
|
"-o", libname, pkgname,
|
2016-11-27 17:05:01 -07:00
|
|
|
)
|
|
|
|
adbPush(t, libname)
|
2018-02-13 20:00:17 -07:00
|
|
|
if GOOS != "freebsd" {
|
|
|
|
runCC(t, "-pthread", "-o", cmd, cfile, "-ldl")
|
|
|
|
} else {
|
|
|
|
runCC(t, "-pthread", "-o", cmd, cfile)
|
|
|
|
}
|
2016-11-27 17:05:01 -07:00
|
|
|
adbPush(t, cmd)
|
|
|
|
|
2017-10-05 19:55:20 -06:00
|
|
|
bin := cmdToRun(cmd)
|
|
|
|
|
2016-11-27 17:05:01 -07:00
|
|
|
defer os.Remove(libname)
|
2017-10-05 19:55:20 -06:00
|
|
|
defer os.Remove(bin)
|
2017-09-10 19:58:17 -06:00
|
|
|
defer os.Remove(pkgname + ".h")
|
2016-11-27 17:05:01 -07:00
|
|
|
|
2017-09-10 19:58:17 -06:00
|
|
|
out := runExe(t, nil, bin, "./"+libname)
|
2016-11-27 17:05:01 -07:00
|
|
|
if strings.TrimSpace(out) != "PASS" {
|
2017-09-10 19:58:17 -06:00
|
|
|
t.Error(run(t, nil, bin, libname, "verbose"))
|
2016-11-27 17:05:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-10 19:58:17 -06:00
|
|
|
// test4: test signal handlers
|
|
|
|
func TestSignalHandlers(t *testing.T) {
|
|
|
|
t.Parallel()
|
2017-10-05 00:43:38 -06:00
|
|
|
if GOOS == "windows" {
|
|
|
|
t.Logf("Skipping on %s", GOOS)
|
|
|
|
return
|
|
|
|
}
|
2019-02-21 14:18:28 -07:00
|
|
|
testSignalHandlers(t, "./libgo4", "main4.c", "testp4")
|
2017-09-10 19:58:17 -06:00
|
|
|
}
|
|
|
|
|
2016-11-27 17:05:01 -07:00
|
|
|
// test5: test signal handlers with os/signal.Notify
|
|
|
|
func TestSignalHandlersWithNotify(t *testing.T) {
|
2017-09-09 23:26:19 -06:00
|
|
|
t.Parallel()
|
2017-10-05 00:43:38 -06:00
|
|
|
if GOOS == "windows" {
|
|
|
|
t.Logf("Skipping on %s", GOOS)
|
|
|
|
return
|
|
|
|
}
|
2019-02-21 14:18:28 -07:00
|
|
|
testSignalHandlers(t, "./libgo5", "main5.c", "testp5")
|
2016-11-27 17:05:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPIE(t *testing.T) {
|
2017-09-09 23:26:19 -06:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-11-27 17:05:01 -07:00
|
|
|
switch GOOS {
|
|
|
|
case "linux", "android":
|
|
|
|
break
|
|
|
|
default:
|
2017-10-05 00:43:38 -06:00
|
|
|
t.Logf("Skipping on %s", GOOS)
|
2016-11-27 17:05:01 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-19 04:17:46 -06:00
|
|
|
createHeadersOnce(t)
|
2016-11-27 17:05:01 -07:00
|
|
|
|
|
|
|
f, err := elf.Open(libgoname)
|
|
|
|
if err != nil {
|
2017-08-19 04:17:46 -06:00
|
|
|
t.Fatalf("elf.Open failed: %v", err)
|
2016-11-27 17:05:01 -07:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
ds := f.SectionByType(elf.SHT_DYNAMIC)
|
|
|
|
if ds == nil {
|
2017-08-19 04:17:46 -06:00
|
|
|
t.Fatalf("no SHT_DYNAMIC section")
|
2016-11-27 17:05:01 -07:00
|
|
|
}
|
|
|
|
d, err := ds.Data()
|
|
|
|
if err != nil {
|
2017-08-19 04:17:46 -06:00
|
|
|
t.Fatalf("can't read SHT_DYNAMIC contents: %v", err)
|
2016-11-27 17:05:01 -07:00
|
|
|
}
|
|
|
|
for len(d) > 0 {
|
2017-08-19 04:17:46 -06:00
|
|
|
var tag elf.DynTag
|
2016-11-27 17:05:01 -07:00
|
|
|
switch f.Class {
|
|
|
|
case elf.ELFCLASS32:
|
2017-08-19 04:17:46 -06:00
|
|
|
tag = elf.DynTag(f.ByteOrder.Uint32(d[:4]))
|
2016-11-27 17:05:01 -07:00
|
|
|
d = d[8:]
|
|
|
|
case elf.ELFCLASS64:
|
2017-08-19 04:17:46 -06:00
|
|
|
tag = elf.DynTag(f.ByteOrder.Uint64(d[:8]))
|
2016-11-27 17:05:01 -07:00
|
|
|
d = d[16:]
|
|
|
|
}
|
2017-08-19 04:17:46 -06:00
|
|
|
if tag == elf.DT_TEXTREL {
|
|
|
|
t.Fatalf("%s has DT_TEXTREL flag", libgoname)
|
2016-11-27 17:05:01 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-10 18:48:37 -07:00
|
|
|
|
|
|
|
// Test that installing a second time recreates the header files.
|
|
|
|
func TestCachedInstall(t *testing.T) {
|
|
|
|
tmpdir, err := ioutil.TempDir("", "cshared")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// defer os.RemoveAll(tmpdir)
|
|
|
|
|
2019-02-21 14:18:28 -07:00
|
|
|
copyFile(t, filepath.Join(tmpdir, "src", "testcshared", "go.mod"), "go.mod")
|
|
|
|
copyFile(t, filepath.Join(tmpdir, "src", "testcshared", "libgo", "libgo.go"), filepath.Join("libgo", "libgo.go"))
|
|
|
|
copyFile(t, filepath.Join(tmpdir, "src", "testcshared", "p", "p.go"), filepath.Join("p", "p.go"))
|
2018-01-10 18:48:37 -07:00
|
|
|
|
2019-02-21 14:18:28 -07:00
|
|
|
env := append(os.Environ(), "GOPATH="+tmpdir, "GOBIN="+filepath.Join(tmpdir, "bin"))
|
2018-01-10 18:48:37 -07:00
|
|
|
|
2019-02-21 14:18:28 -07:00
|
|
|
buildcmd := []string{"go", "install", "-x", "-i", "-buildmode=c-shared", "-installsuffix", "testcshared", "./libgo"}
|
2018-01-10 18:48:37 -07:00
|
|
|
|
|
|
|
cmd := exec.Command(buildcmd[0], buildcmd[1:]...)
|
2019-02-21 14:18:28 -07:00
|
|
|
cmd.Dir = filepath.Join(tmpdir, "src", "testcshared")
|
2018-01-10 18:48:37 -07:00
|
|
|
cmd.Env = env
|
|
|
|
t.Log(buildcmd)
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
|
|
t.Logf("%s", out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var libgoh, ph string
|
|
|
|
|
|
|
|
walker := func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var ps *string
|
|
|
|
switch filepath.Base(path) {
|
|
|
|
case "libgo.h":
|
|
|
|
ps = &libgoh
|
|
|
|
case "p.h":
|
|
|
|
ps = &ph
|
|
|
|
}
|
|
|
|
if ps != nil {
|
|
|
|
if *ps != "" {
|
|
|
|
t.Fatalf("%s found again", *ps)
|
|
|
|
}
|
|
|
|
*ps = path
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := filepath.Walk(tmpdir, walker); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if libgoh == "" {
|
|
|
|
t.Fatal("libgo.h not installed")
|
|
|
|
}
|
|
|
|
if ph == "" {
|
|
|
|
t.Fatal("p.h not installed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.Remove(libgoh); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := os.Remove(ph); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd = exec.Command(buildcmd[0], buildcmd[1:]...)
|
2019-02-21 14:18:28 -07:00
|
|
|
cmd.Dir = filepath.Join(tmpdir, "src", "testcshared")
|
2018-01-10 18:48:37 -07:00
|
|
|
cmd.Env = env
|
|
|
|
t.Log(buildcmd)
|
|
|
|
out, err = cmd.CombinedOutput()
|
|
|
|
t.Logf("%s", out)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(libgoh); err != nil {
|
|
|
|
t.Errorf("libgo.h not installed in second run: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(ph); err != nil {
|
|
|
|
t.Errorf("p.h not installed in second run: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// copyFile copies src to dst.
|
|
|
|
func copyFile(t *testing.T, dst, src string) {
|
|
|
|
t.Helper()
|
|
|
|
data, err := ioutil.ReadFile(src)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(filepath.Dir(dst), 0777); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := ioutil.WriteFile(dst, data, 0666); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2018-11-30 15:21:33 -07:00
|
|
|
|
|
|
|
func TestGo2C2Go(t *testing.T) {
|
2018-12-03 16:12:54 -07:00
|
|
|
switch GOOS {
|
|
|
|
case "darwin":
|
2018-12-03 12:23:15 -07:00
|
|
|
// Darwin shared libraries don't support the multiple
|
|
|
|
// copies of the runtime package implied by this test.
|
|
|
|
t.Skip("linking c-shared into Go programs not supported on Darwin; issue 29061")
|
2018-12-03 16:12:54 -07:00
|
|
|
case "android":
|
|
|
|
t.Skip("test fails on android; issue 29087")
|
2018-12-03 12:23:15 -07:00
|
|
|
}
|
|
|
|
|
2018-11-30 15:21:33 -07:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
tmpdir, err := ioutil.TempDir("", "cshared-TestGo2C2Go")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
2019-02-21 14:18:28 -07:00
|
|
|
lib := filepath.Join(tmpdir, "libtestgo2c2go."+libSuffix)
|
|
|
|
run(t, nil, "go", "build", "-buildmode=c-shared", "-o", lib, "./go2c2go/go")
|
2018-11-30 15:21:33 -07:00
|
|
|
|
|
|
|
cgoCflags := os.Getenv("CGO_CFLAGS")
|
|
|
|
if cgoCflags != "" {
|
|
|
|
cgoCflags += " "
|
|
|
|
}
|
|
|
|
cgoCflags += "-I" + tmpdir
|
|
|
|
|
|
|
|
cgoLdflags := os.Getenv("CGO_LDFLAGS")
|
|
|
|
if cgoLdflags != "" {
|
|
|
|
cgoLdflags += " "
|
|
|
|
}
|
|
|
|
cgoLdflags += "-L" + tmpdir + " -ltestgo2c2go"
|
|
|
|
|
2019-02-21 14:18:28 -07:00
|
|
|
goenv := []string{"CGO_CFLAGS=" + cgoCflags, "CGO_LDFLAGS=" + cgoLdflags}
|
2018-11-30 15:21:33 -07:00
|
|
|
|
|
|
|
ldLibPath := os.Getenv("LD_LIBRARY_PATH")
|
|
|
|
if ldLibPath != "" {
|
|
|
|
ldLibPath += ":"
|
|
|
|
}
|
|
|
|
ldLibPath += tmpdir
|
|
|
|
|
2019-02-21 14:18:28 -07:00
|
|
|
runenv := []string{"LD_LIBRARY_PATH=" + ldLibPath}
|
2018-11-30 15:21:33 -07:00
|
|
|
|
|
|
|
bin := filepath.Join(tmpdir, "m1") + exeSuffix
|
2019-02-21 14:18:28 -07:00
|
|
|
run(t, goenv, "go", "build", "-o", bin, "./go2c2go/m1")
|
2018-11-30 15:21:33 -07:00
|
|
|
runExe(t, runenv, bin)
|
|
|
|
|
|
|
|
bin = filepath.Join(tmpdir, "m2") + exeSuffix
|
2019-02-21 14:18:28 -07:00
|
|
|
run(t, goenv, "go", "build", "-o", bin, "./go2c2go/m2")
|
2018-11-30 15:21:33 -07:00
|
|
|
runExe(t, runenv, bin)
|
|
|
|
}
|