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 (
|
cmd/go: cgo export header to be compatible with MSVC complex types
After CL 379474 has landed, the only remaining cgo export header
incompatibility with MSVC is the use of the _Complex macro,
which is not supported in MSVC even when it is part of the ISO C99
standard (1).
Since MSVC 2015 (2), complex math are supported via _Fcomplex and
_Dcomplex, which are equivalent to float _Complex and double _Complex.
As MSVC and C complex types have the same memory layout, we should
be able to typedef GoComplex64 and GoComplex128 to the appropriate
type in MSVC.
It is important to note that this CL is not adding MSVC support to cgo.
C compilers should still be GCC-compatible.
This CL is about allowing to include, without further modifications,
a DLL export header generated by cgo, normally using Mingw-W64 compiler,
into a MSVC project. This was already possible if the export header
changes introduced in this CL were done outside cgo, either manually or
in a post-build script.
Fixes #36233
1: https://docs.microsoft.com/en-us/cpp/c-runtime-library/complex-math-support
2: https://docs.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?c-standard-library-features-1
Change-Id: Iad8f26984b115c728e3b73f3a8334ade7a11cfa1
Reviewed-on: https://go-review.googlesource.com/c/go/+/397134
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Trust: Cherry Mui <cherryyz@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
Auto-Submit: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-03-31 06:15:22 -06:00
|
|
|
"bufio"
|
2019-02-21 14:18:28 -07:00
|
|
|
"bytes"
|
2016-11-27 17:05:01 -07:00
|
|
|
"debug/elf"
|
2020-10-15 15:12:49 -06:00
|
|
|
"debug/pe"
|
|
|
|
"encoding/binary"
|
2019-05-15 12:31:48 -06:00
|
|
|
"flag"
|
2016-11-27 17:05:01 -07:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2019-04-08 07:49:58 -06:00
|
|
|
"runtime"
|
2016-11-27 17:05:01 -07:00
|
|
|
"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)
|
2019-05-15 12:31:48 -06:00
|
|
|
flag.Parse()
|
|
|
|
if testing.Short() && os.Getenv("GO_BUILDER_NAME") == "" {
|
|
|
|
fmt.Printf("SKIP - short mode and $GO_BUILDER_NAME not set\n")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
2022-07-28 22:32:14 -06:00
|
|
|
if runtime.GOOS == "linux" {
|
|
|
|
if _, err := os.Stat("/etc/alpine-release"); err == nil {
|
|
|
|
fmt.Printf("SKIP - skipping failing test on alpine - go.dev/issue/19938\n")
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
}
|
2019-02-21 14:18:28 -07:00
|
|
|
|
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())
|
2019-04-08 07:49:58 -06:00
|
|
|
if runtime.GOOS != GOOS && GOOS == "android" {
|
2019-04-08 07:45:16 -06:00
|
|
|
args := append(adbCmd(), "exec-out", "mkdir", "-p", androiddir)
|
2018-01-20 11:56:51 -07:00
|
|
|
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 {
|
2020-09-16 14:59:58 -06:00
|
|
|
case "darwin", "ios":
|
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":
|
2019-11-13 03:57:17 -07:00
|
|
|
cc = append(cc, "-pie")
|
2016-11-27 17:05:01 -07:00
|
|
|
}
|
|
|
|
libgodir := GOOS + "_" + GOARCH
|
|
|
|
switch GOOS {
|
2020-09-16 14:59:58 -06:00
|
|
|
case "darwin", "ios":
|
2020-04-03 10:22:27 -06:00
|
|
|
if GOARCH == "arm64" {
|
2016-11-27 17:05:01 -07:00
|
|
|
libgodir += "_shared"
|
|
|
|
}
|
2019-04-29 07:50:49 -06:00
|
|
|
case "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "solaris", "illumos":
|
2016-11-27 17:05:01 -07:00
|
|
|
libgodir += "_shared"
|
|
|
|
}
|
|
|
|
cc = append(cc, "-I", filepath.Join("pkg", libgodir))
|
|
|
|
|
2022-02-07 09:33:30 -07:00
|
|
|
// Force reallocation (and avoid aliasing bugs) for parallel tests that append to cc.
|
|
|
|
cc = cc[:len(cc):len(cc)]
|
|
|
|
|
2016-11-27 17:05:01 -07:00
|
|
|
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.
|
|
|
|
|
2021-04-03 02:10:47 -06:00
|
|
|
GOPATH, err := os.MkdirTemp("", "cshared_test")
|
2019-02-21 14:18:28 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(GOPATH)
|
|
|
|
os.Setenv("GOPATH", GOPATH)
|
|
|
|
|
|
|
|
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)
|
2021-04-03 02:10:47 -06:00
|
|
|
if err := os.WriteFile("go.mod", []byte("module testcshared\n"), 0666); err != nil {
|
2019-02-21 14:18:28 -07:00
|
|
|
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",
|
|
|
|
"-f", "{{.Target}}",
|
2022-07-12 11:43:02 -06:00
|
|
|
"runtime/cgo").CombinedOutput()
|
2019-02-21 14:18:28 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Panicf("go list failed: %v\n%s", err, output)
|
|
|
|
}
|
2022-07-12 11:43:02 -06:00
|
|
|
runtimeCgoTarget := string(bytes.TrimSpace(output))
|
|
|
|
libSuffix = strings.TrimPrefix(filepath.Ext(runtimeCgoTarget), ".")
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if installdir != "" {
|
|
|
|
err := os.RemoveAll(installdir)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
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) {
|
2019-04-08 07:49:58 -06:00
|
|
|
if runtime.GOOS == GOOS || GOOS != "android" {
|
2016-11-27 17:05:01 -07:00
|
|
|
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.")
|
|
|
|
}
|
2019-04-08 07:45:16 -06:00
|
|
|
args := append(adbCmd(), "exec-out")
|
2017-08-19 05:11:09 -06:00
|
|
|
// Propagate LD_LIBRARY_PATH to the adb shell invocation.
|
|
|
|
for _, e := range env {
|
2021-09-22 08:46:32 -06:00
|
|
|
if strings.Contains(e, "LD_LIBRARY_PATH=") {
|
2017-08-19 05:11:09 -06:00
|
|
|
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()
|
2019-04-08 07:49:58 -06:00
|
|
|
if runtime.GOOS != GOOS && 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 {
|
2019-11-20 12:39:19 -07:00
|
|
|
// The 'cgo' command generates a number of additional artifacts,
|
|
|
|
// but we're only interested in the header.
|
|
|
|
// Shunt the rest of the outputs to a temporary directory.
|
2021-04-03 02:10:47 -06:00
|
|
|
objDir, err := os.MkdirTemp("", "testcshared_obj")
|
2019-11-20 12:39:19 -07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(objDir)
|
|
|
|
|
|
|
|
// Generate a C header file for p, which is a non-main dependency
|
|
|
|
// of main package libgo.
|
|
|
|
//
|
|
|
|
// TODO(golang.org/issue/35715): This should be simpler.
|
|
|
|
args := []string{"go", "tool", "cgo",
|
|
|
|
"-objdir", objDir,
|
|
|
|
"-exportheader", "p.h",
|
|
|
|
filepath.Join(".", "p", "p.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
|
|
|
|
2019-11-20 12:39:19 -07:00
|
|
|
// Generate a C header file for libgo itself.
|
2022-07-12 11:43:02 -06:00
|
|
|
installdir, err = os.MkdirTemp("", "testcshared")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
libgoname = "libgo." + libSuffix
|
|
|
|
|
|
|
|
args = []string{"go", "build", "-buildmode=c-shared", "-o", filepath.Join(installdir, libgoname), "./libgo"}
|
2019-11-20 12:39:19 -07: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)
|
|
|
|
}
|
|
|
|
|
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")}
|
2021-06-11 09:53:29 -06:00
|
|
|
if GOOS == "windows" && strings.HasSuffix(args[6], ".a") {
|
|
|
|
args[6] = strings.TrimSuffix(args[6], ".a") + ".dll"
|
|
|
|
}
|
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)
|
|
|
|
}
|
2021-06-11 09:53:29 -06:00
|
|
|
if GOOS == "windows" {
|
|
|
|
// We can't simply pass -Wl,--out-implib, because this relies on having imports from multiple packages,
|
|
|
|
// which results in the linkers output implib getting overwritten at each step. So instead build the
|
|
|
|
// import library the traditional way, using a def file.
|
|
|
|
err = os.WriteFile("libgo.def",
|
|
|
|
[]byte("LIBRARY libgo.dll\nEXPORTS\n\tDidInitRun\n\tDidMainRun\n\tDivu\n\tFromPkg\n\t_cgo_dummy_export\n"),
|
|
|
|
0644)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to write def file: %v", err)
|
|
|
|
}
|
|
|
|
out, err = exec.Command(cc[0], append(cc[1:], "-print-prog-name=dlltool")...).CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to find dlltool path: %v\n%s\n", err, out)
|
|
|
|
}
|
|
|
|
args := []string{strings.TrimSpace(string(out)), "-D", args[6], "-l", libgoname, "-d", "libgo.def"}
|
|
|
|
|
|
|
|
// This is an unfortunate workaround for https://github.com/mstorsjo/llvm-mingw/issues/205 in which
|
|
|
|
// we basically reimplement the contents of the dlltool.sh wrapper: https://git.io/JZFlU
|
|
|
|
dlltoolContents, err := os.ReadFile(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to read dlltool: %v\n", err)
|
|
|
|
}
|
|
|
|
if bytes.HasPrefix(dlltoolContents, []byte("#!/bin/sh")) && bytes.Contains(dlltoolContents, []byte("llvm-dlltool")) {
|
|
|
|
base, name := filepath.Split(args[0])
|
|
|
|
args[0] = filepath.Join(base, "llvm-dlltool")
|
|
|
|
var machine string
|
2021-09-22 08:46:32 -06:00
|
|
|
switch prefix, _, _ := strings.Cut(name, "-"); prefix {
|
2021-06-11 09:53:29 -06:00
|
|
|
case "i686":
|
|
|
|
machine = "i386"
|
|
|
|
case "x86_64":
|
|
|
|
machine = "i386:x86-64"
|
|
|
|
case "armv7":
|
|
|
|
machine = "arm"
|
|
|
|
case "aarch64":
|
|
|
|
machine = "arm64"
|
|
|
|
}
|
|
|
|
if len(machine) > 0 {
|
|
|
|
args = append(args, "-m", machine)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err = exec.Command(args[0], args[1:]...).CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to run dlltool to create import library: %v\n%s\n", err, out)
|
|
|
|
}
|
|
|
|
}
|
2016-11-27 17:05:01 -07:00
|
|
|
|
2019-04-08 07:49:58 -06:00
|
|
|
if runtime.GOOS != GOOS && 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 {
|
2022-07-12 11:43:02 -06:00
|
|
|
t.Helper()
|
2017-08-19 04:17:46 -06:00
|
|
|
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
|
|
|
|
}
|
2019-04-08 07:45:16 -06:00
|
|
|
args := append(adbCmd(), "exec-out", "rm", "-rf", androiddir)
|
2018-01-20 11:56:51 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 15:12:49 -06:00
|
|
|
func checkNumberOfExportedFunctionsWindows(t *testing.T, exportAllSymbols bool) {
|
|
|
|
const prog = `
|
|
|
|
package main
|
|
|
|
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
//export GoFunc
|
|
|
|
func GoFunc() {
|
|
|
|
println(42)
|
|
|
|
}
|
|
|
|
|
|
|
|
//export GoFunc2
|
|
|
|
func GoFunc2() {
|
|
|
|
println(24)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
tmpdir := t.TempDir()
|
|
|
|
|
|
|
|
srcfile := filepath.Join(tmpdir, "test.go")
|
|
|
|
objfile := filepath.Join(tmpdir, "test.dll")
|
2021-04-03 02:10:47 -06:00
|
|
|
if err := os.WriteFile(srcfile, []byte(prog), 0666); err != nil {
|
2020-10-15 15:12:49 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
argv := []string{"build", "-buildmode=c-shared"}
|
|
|
|
if exportAllSymbols {
|
|
|
|
argv = append(argv, "-ldflags", "-extldflags=-Wl,--export-all-symbols")
|
|
|
|
}
|
|
|
|
argv = append(argv, "-o", objfile, srcfile)
|
|
|
|
out, err := exec.Command("go", argv...).CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("build failure: %s\n%s\n", err, string(out))
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := pe.Open(objfile)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("pe.Open failed: %v", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
section := f.Section(".edata")
|
|
|
|
if section == nil {
|
2021-06-12 04:34:40 -06:00
|
|
|
t.Skip(".edata section is not present")
|
2020-10-15 15:12:49 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: deduplicate this struct from cmd/link/internal/ld/pe.go
|
|
|
|
type IMAGE_EXPORT_DIRECTORY struct {
|
|
|
|
_ [2]uint32
|
|
|
|
_ [2]uint16
|
|
|
|
_ [2]uint32
|
|
|
|
NumberOfFunctions uint32
|
|
|
|
NumberOfNames uint32
|
|
|
|
_ [3]uint32
|
|
|
|
}
|
|
|
|
var e IMAGE_EXPORT_DIRECTORY
|
|
|
|
if err := binary.Read(section.Open(), binary.LittleEndian, &e); err != nil {
|
|
|
|
t.Fatalf("binary.Read failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-10-22 14:32:20 -06:00
|
|
|
// Only the two exported functions and _cgo_dummy_export should be exported
|
|
|
|
expectedNumber := uint32(3)
|
2020-10-15 15:12:49 -06:00
|
|
|
|
|
|
|
if exportAllSymbols {
|
|
|
|
if e.NumberOfFunctions <= expectedNumber {
|
|
|
|
t.Fatalf("missing exported functions: %v", e.NumberOfFunctions)
|
|
|
|
}
|
|
|
|
if e.NumberOfNames <= expectedNumber {
|
|
|
|
t.Fatalf("missing exported names: %v", e.NumberOfNames)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if e.NumberOfFunctions != expectedNumber {
|
2020-10-22 14:32:20 -06:00
|
|
|
t.Fatalf("got %d exported functions; want %d", e.NumberOfFunctions, expectedNumber)
|
2020-10-15 15:12:49 -06:00
|
|
|
}
|
|
|
|
if e.NumberOfNames != expectedNumber {
|
2020-10-22 14:32:20 -06:00
|
|
|
t.Fatalf("got %d exported names; want %d", e.NumberOfNames, expectedNumber)
|
2020-10-15 15:12:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNumberOfExportedFunctions(t *testing.T) {
|
|
|
|
if GOOS != "windows" {
|
|
|
|
t.Skip("skipping windows only test")
|
|
|
|
}
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("OnlyExported", func(t *testing.T) {
|
|
|
|
checkNumberOfExportedFunctionsWindows(t, false)
|
|
|
|
})
|
|
|
|
t.Run("All", func(t *testing.T) {
|
|
|
|
checkNumberOfExportedFunctionsWindows(t, true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-11-27 17:05:01 -07:00
|
|
|
// 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"
|
2020-09-16 14:59:58 -06:00
|
|
|
if GOOS == "darwin" || GOOS == "ios" {
|
2016-11-27 17:05:01 -07:00
|
|
|
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
|
|
|
|
2019-11-20 12:39:19 -07:00
|
|
|
// Test that installing a second time recreates the header file.
|
2018-01-10 18:48:37 -07:00
|
|
|
func TestCachedInstall(t *testing.T) {
|
2021-04-03 02:10:47 -06:00
|
|
|
tmpdir, err := os.MkdirTemp("", "cshared")
|
2018-01-10 18:48:37 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-03-01 14:01:46 -07:00
|
|
|
defer os.RemoveAll(tmpdir)
|
2018-01-10 18:48:37 -07:00
|
|
|
|
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-11-20 12:39:19 -07:00
|
|
|
buildcmd := []string{"go", "install", "-x", "-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")
|
2022-07-12 11:43:02 -06:00
|
|
|
env := append(cmd.Environ(),
|
|
|
|
"GOPATH="+tmpdir,
|
|
|
|
"GOBIN="+filepath.Join(tmpdir, "bin"),
|
|
|
|
"GO111MODULE=off", // 'go install' only works in GOPATH mode
|
|
|
|
)
|
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 err := os.Remove(libgoh); 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// copyFile copies src to dst.
|
|
|
|
func copyFile(t *testing.T, dst, src string) {
|
|
|
|
t.Helper()
|
2021-04-03 02:10:47 -06:00
|
|
|
data, err := os.ReadFile(src)
|
2018-01-10 18:48:37 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(filepath.Dir(dst), 0777); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-04-03 02:10:47 -06:00
|
|
|
if err := os.WriteFile(dst, data, 0666); err != nil {
|
2018-01-10 18:48:37 -07:00
|
|
|
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 {
|
2021-11-21 17:19:26 -07:00
|
|
|
case "darwin", "ios", "windows":
|
|
|
|
// Non-ELF shared libraries don't support the multiple
|
2018-12-03 12:23:15 -07:00
|
|
|
// copies of the runtime package implied by this test.
|
2021-11-21 17:19:26 -07:00
|
|
|
t.Skipf("linking c-shared into Go programs not supported on %s; issue 29061, 49457", GOOS)
|
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()
|
|
|
|
|
2021-04-03 02:10:47 -06:00
|
|
|
tmpdir, err := os.MkdirTemp("", "cshared-TestGo2C2Go")
|
2018-11-30 15:21:33 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
2019-02-21 14:18:28 -07:00
|
|
|
lib := filepath.Join(tmpdir, "libtestgo2c2go."+libSuffix)
|
2021-06-11 09:53:29 -06:00
|
|
|
var env []string
|
|
|
|
if GOOS == "windows" && strings.HasSuffix(lib, ".a") {
|
|
|
|
env = append(env, "CGO_LDFLAGS=-Wl,--out-implib,"+lib, "CGO_LDFLAGS_ALLOW=.*")
|
|
|
|
lib = strings.TrimSuffix(lib, ".a") + ".dll"
|
|
|
|
}
|
|
|
|
run(t, env, "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)
|
|
|
|
}
|
cmd/go: cgo export header to be compatible with MSVC complex types
After CL 379474 has landed, the only remaining cgo export header
incompatibility with MSVC is the use of the _Complex macro,
which is not supported in MSVC even when it is part of the ISO C99
standard (1).
Since MSVC 2015 (2), complex math are supported via _Fcomplex and
_Dcomplex, which are equivalent to float _Complex and double _Complex.
As MSVC and C complex types have the same memory layout, we should
be able to typedef GoComplex64 and GoComplex128 to the appropriate
type in MSVC.
It is important to note that this CL is not adding MSVC support to cgo.
C compilers should still be GCC-compatible.
This CL is about allowing to include, without further modifications,
a DLL export header generated by cgo, normally using Mingw-W64 compiler,
into a MSVC project. This was already possible if the export header
changes introduced in this CL were done outside cgo, either manually or
in a post-build script.
Fixes #36233
1: https://docs.microsoft.com/en-us/cpp/c-runtime-library/complex-math-support
2: https://docs.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?c-standard-library-features-1
Change-Id: Iad8f26984b115c728e3b73f3a8334ade7a11cfa1
Reviewed-on: https://go-review.googlesource.com/c/go/+/397134
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Trust: Cherry Mui <cherryyz@google.com>
Run-TryBot: Cherry Mui <cherryyz@google.com>
Auto-Submit: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
2022-03-31 06:15:22 -06:00
|
|
|
|
|
|
|
func TestIssue36233(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Test that the export header uses GoComplex64 and GoComplex128
|
|
|
|
// for complex types.
|
|
|
|
|
|
|
|
tmpdir, err := os.MkdirTemp("", "cshared-TestIssue36233")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
|
|
|
const exportHeader = "issue36233.h"
|
|
|
|
|
|
|
|
run(t, nil, "go", "tool", "cgo", "-exportheader", exportHeader, "-objdir", tmpdir, "./issue36233/issue36233.go")
|
|
|
|
data, err := os.ReadFile(exportHeader)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
funcs := []struct{ name, signature string }{
|
|
|
|
{"exportComplex64", "GoComplex64 exportComplex64(GoComplex64 v)"},
|
|
|
|
{"exportComplex128", "GoComplex128 exportComplex128(GoComplex128 v)"},
|
|
|
|
{"exportComplexfloat", "GoComplex64 exportComplexfloat(GoComplex64 v)"},
|
|
|
|
{"exportComplexdouble", "GoComplex128 exportComplexdouble(GoComplex128 v)"},
|
|
|
|
}
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(bytes.NewReader(data))
|
|
|
|
var found int
|
|
|
|
for scanner.Scan() {
|
|
|
|
b := scanner.Bytes()
|
|
|
|
for _, fn := range funcs {
|
|
|
|
if bytes.Contains(b, []byte(fn.name)) {
|
|
|
|
found++
|
|
|
|
if !bytes.Contains(b, []byte(fn.signature)) {
|
|
|
|
t.Errorf("function signature mismatch; got %q, want %q", b, fn.signature)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = scanner.Err(); err != nil {
|
|
|
|
t.Errorf("scanner encountered error: %v", err)
|
|
|
|
}
|
|
|
|
if found != len(funcs) {
|
|
|
|
t.Error("missing functions")
|
|
|
|
}
|
|
|
|
}
|