1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
19 func requireTestSOSupported(t *testing.T) {
21 switch runtime.GOARCH {
23 if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
24 t.Skip("No exec facility on iOS.")
27 if runtime.GOOS == "linux" {
28 t.Skip("External linking not implemented on linux/ppc64 (issue #8912).")
31 if runtime.GOOS == "android" {
32 t.Skip("No exec facility on Android.")
36 func TestSO(t *testing.T) {
37 requireTestSOSupported(t)
39 GOPATH, err := os.MkdirTemp("", "cgosotest")
43 defer os.RemoveAll(GOPATH)
45 modRoot := filepath.Join(GOPATH, "src", "cgosotest")
46 if err := overlayDir(modRoot, "testdata"); err != nil {
49 if err := os.WriteFile(filepath.Join(modRoot, "go.mod"), []byte("module cgosotest\n"), 0666); err != nil {
53 cmd := exec.Command("go", "env", "CC", "GOGCCFLAGS")
55 cmd.Stderr = new(strings.Builder)
56 cmd.Env = append(os.Environ(), "GOPATH="+GOPATH)
57 out, err := cmd.Output()
59 t.Fatalf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, cmd.Stderr)
61 lines := strings.Split(string(out), "\n")
62 if len(lines) != 3 || lines[2] != "" {
63 t.Fatalf("Unexpected output from %s:\n%s", strings.Join(cmd.Args, " "), lines)
68 t.Fatal("CC environment variable (go env CC) cannot be empty")
70 gogccflags := strings.Split(lines[1], " ")
72 // build shared object
74 args := append(gogccflags, "-shared")
78 args = append(args, "-undefined", "suppress", "-flat_namespace")
81 args = append(args, "-DEXPORT_DLL")
82 // At least in mingw-clang it is not permitted to just name a .dll
83 // on the command line. You must name the corresponding import
84 // library instead, even though the dll is used when the executable is run.
85 args = append(args, "-Wl,-out-implib,libcgosotest.a")
89 sofname := "libcgosotest." + ext
90 args = append(args, "-o", sofname, "cgoso_c.c")
92 cmd = exec.Command(cc, args...)
94 cmd.Env = append(os.Environ(), "GOPATH="+GOPATH)
95 out, err = cmd.CombinedOutput()
97 t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
99 t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
101 if runtime.GOOS == "aix" {
102 // Shared object must be wrapped by an archive
103 cmd = exec.Command("ar", "-X64", "-q", "libcgosotest.a", "libcgosotest.so.1")
105 out, err = cmd.CombinedOutput()
107 t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
111 cmd = exec.Command("go", "build", "-o", "main.exe", "main.go")
113 cmd.Env = append(os.Environ(), "GOPATH="+GOPATH)
114 out, err = cmd.CombinedOutput()
116 t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
118 t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
120 cmd = exec.Command("./main.exe")
122 cmd.Env = append(os.Environ(), "GOPATH="+GOPATH)
123 if runtime.GOOS != "windows" {
124 s := "LD_LIBRARY_PATH"
125 if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
126 s = "DYLD_LIBRARY_PATH"
128 cmd.Env = append(os.Environ(), s+"=.")
130 // On FreeBSD 64-bit architectures, the 32-bit linker looks for
131 // different environment variables.
132 if runtime.GOOS == "freebsd" && runtime.GOARCH == "386" {
133 cmd.Env = append(cmd.Env, "LD_32_LIBRARY_PATH=.")
136 out, err = cmd.CombinedOutput()
138 t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
140 t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)