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.
20 func requireTestSOSupported(t *testing.T) {
22 switch runtime.GOARCH {
24 if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
25 t.Skip("No exec facility on iOS.")
28 if runtime.GOOS == "linux" {
29 t.Skip("External linking not implemented on linux/ppc64 (issue #8912).")
32 if runtime.GOOS == "android" {
33 t.Skip("No exec facility on Android.")
37 func TestSO(t *testing.T) {
38 requireTestSOSupported(t)
40 GOPATH, err := os.MkdirTemp("", "cgosotest")
44 defer os.RemoveAll(GOPATH)
46 modRoot := filepath.Join(GOPATH, "src", "cgosotest")
47 if err := overlayDir(modRoot, "testdata"); err != nil {
50 if err := os.WriteFile(filepath.Join(modRoot, "go.mod"), []byte("module cgosotest\n"), 0666); err != nil {
54 cmd := exec.Command("go", "env", "CC", "GOGCCFLAGS")
56 cmd.Stderr = new(strings.Builder)
57 cmd.Env = append(os.Environ(), "GOPATH="+GOPATH)
58 out, err := cmd.Output()
60 t.Fatalf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, cmd.Stderr)
62 lines := strings.Split(string(out), "\n")
63 if len(lines) != 3 || lines[2] != "" {
64 t.Fatalf("Unexpected output from %s:\n%s", strings.Join(cmd.Args, " "), lines)
69 t.Fatal("CC environment variable (go env CC) cannot be empty")
71 gogccflags := strings.Split(lines[1], " ")
73 // build shared object
75 args := append(gogccflags, "-shared")
79 args = append(args, "-undefined", "suppress", "-flat_namespace")
82 args = append(args, "-DEXPORT_DLL")
83 // At least in mingw-clang it is not permitted to just name a .dll
84 // on the command line. You must name the corresponding import
85 // library instead, even though the dll is used when the executable is run.
86 args = append(args, "-Wl,-out-implib,libcgosotest.a")
90 sofname := "libcgosotest." + ext
91 args = append(args, "-o", sofname, "cgoso_c.c")
93 cmd = exec.Command(cc, args...)
95 cmd.Env = append(os.Environ(), "GOPATH="+GOPATH)
96 out, err = cmd.CombinedOutput()
98 t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
100 t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
102 if runtime.GOOS == "aix" {
103 // Shared object must be wrapped by an archive
104 cmd = exec.Command("ar", "-X64", "-q", "libcgosotest.a", "libcgosotest.so.1")
106 out, err = cmd.CombinedOutput()
108 t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
112 cmd = exec.Command("go", "build", "-o", "main.exe", "main.go")
114 cmd.Env = append(os.Environ(), "GOPATH="+GOPATH)
115 out, err = cmd.CombinedOutput()
117 t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
119 t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
121 cmd = exec.Command("./main.exe")
123 cmd.Env = append(os.Environ(), "GOPATH="+GOPATH)
124 if runtime.GOOS != "windows" {
125 s := "LD_LIBRARY_PATH"
126 if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
127 s = "DYLD_LIBRARY_PATH"
129 cmd.Env = append(os.Environ(), s+"=.")
131 // On FreeBSD 64-bit architectures, the 32-bit linker looks for
132 // different environment variables.
133 if runtime.GOOS == "freebsd" && runtime.GOARCH == "386" {
134 cmd.Env = append(cmd.Env, "LD_32_LIBRARY_PATH=.")
137 out, err = cmd.CombinedOutput()
139 t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
141 t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)