2019-02-22 08:50:47 -07:00
|
|
|
// 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.
|
|
|
|
|
all: gofmt more (but vendor, testdata, and top-level test directories)
CL 294430 made packages in std and cmd modules use Go 1.17 gofmt format,
adding //go:build lines. This change applies the same formatting to some
more packages that 'go fmt' missed (e.g., syscall/js, runtime/msan), and
everything else that is easy and safe to modify in bulk.
Consider the top-level test directory, testdata, and vendor directories
out of scope, since there are many files that don't follow strict gofmt
formatting, often for intentional and legitimate reasons (testing gofmt
itself, invalid Go programs that shouldn't crash the compiler, etc.).
That makes it easy and safe to gofmt -w the .go files that are found
with gofmt -l with aforementioned directories filtered out:
$ gofmt -l . 2>/dev/null | \
grep -v '^test/' | \
grep -v '/testdata/' | \
grep -v '/vendor/' | wc -l
51
None of the 51 files are generated. After this change, the same command
prints 0.
For #41184.
Change-Id: Ia96ee2a0f998d6a167d4473bcad17ad09bc1d86e
Reviewed-on: https://go-review.googlesource.com/c/go/+/341009
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Trust: Dmitri Shuralyov <dmitshur@golang.org>
2021-08-09 18:29:14 -06:00
|
|
|
//go:build cgo
|
2019-02-22 08:50:47 -07:00
|
|
|
// +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 {
|
2020-04-03 10:22:27 -06:00
|
|
|
case "arm64":
|
2020-09-16 14:59:58 -06:00
|
|
|
if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
|
2019-02-22 08:50:47 -07:00
|
|
|
t.Skip("No exec facility on iOS.")
|
|
|
|
}
|
|
|
|
case "ppc64":
|
2019-02-21 02:56:35 -07:00
|
|
|
if runtime.GOOS == "linux" {
|
2020-01-30 02:59:11 -07:00
|
|
|
t.Skip("External linking not implemented on linux/ppc64 (issue #8912).")
|
2019-02-21 02:56:35 -07:00
|
|
|
}
|
2019-02-22 08:50:47 -07:00
|
|
|
}
|
|
|
|
if runtime.GOOS == "android" {
|
|
|
|
t.Skip("No exec facility on Android.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSO(t *testing.T) {
|
|
|
|
requireTestSOSupported(t)
|
|
|
|
|
2021-04-03 02:10:47 -06:00
|
|
|
GOPATH, err := os.MkdirTemp("", "cgosotest")
|
2019-02-22 08:50:47 -07:00
|
|
|
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)
|
|
|
|
}
|
2021-04-03 02:10:47 -06:00
|
|
|
if err := os.WriteFile(filepath.Join(modRoot, "go.mod"), []byte("module cgosotest\n"), 0666); err != nil {
|
2019-02-22 08:50:47 -07:00
|
|
|
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 {
|
2020-09-16 14:59:58 -06:00
|
|
|
case "darwin", "ios":
|
2019-02-22 08:50:47 -07:00
|
|
|
ext = "dylib"
|
|
|
|
args = append(args, "-undefined", "suppress", "-flat_namespace")
|
|
|
|
case "windows":
|
|
|
|
ext = "dll"
|
|
|
|
args = append(args, "-DEXPORT_DLL")
|
2021-04-20 14:22:01 -06:00
|
|
|
// 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")
|
2019-03-26 08:00:00 -06:00
|
|
|
case "aix":
|
|
|
|
ext = "so.1"
|
2019-02-22 08:50:47 -07:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
|
2019-03-26 08:00:00 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 08:50:47 -07:00
|
|
|
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"
|
2020-09-16 14:59:58 -06:00
|
|
|
if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
|
2019-02-22 08:50:47 -07:00
|
|
|
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)
|
|
|
|
}
|