]> Cypherpunks.ru repositories - gostls13.git/blob - misc/cgo/testso/so_test.go
5c460d21b60cea140175e5d5212b28199fead2e9
[gostls13.git] / misc / cgo / testso / so_test.go
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.
4
5 //go:build cgo
6
7 package so_test
8
9 import (
10         "log"
11         "os"
12         "os/exec"
13         "path/filepath"
14         "runtime"
15         "strings"
16         "testing"
17 )
18
19 func requireTestSOSupported(t *testing.T) {
20         t.Helper()
21         switch runtime.GOARCH {
22         case "arm64":
23                 if runtime.GOOS == "darwin" || runtime.GOOS == "ios" {
24                         t.Skip("No exec facility on iOS.")
25                 }
26         case "ppc64":
27                 if runtime.GOOS == "linux" {
28                         t.Skip("External linking not implemented on linux/ppc64 (issue #8912).")
29                 }
30         }
31         if runtime.GOOS == "android" {
32                 t.Skip("No exec facility on Android.")
33         }
34 }
35
36 func TestSO(t *testing.T) {
37         requireTestSOSupported(t)
38
39         GOPATH, err := os.MkdirTemp("", "cgosotest")
40         if err != nil {
41                 log.Fatal(err)
42         }
43         defer os.RemoveAll(GOPATH)
44
45         modRoot := filepath.Join(GOPATH, "src", "cgosotest")
46         if err := overlayDir(modRoot, "testdata"); err != nil {
47                 log.Panic(err)
48         }
49         if err := os.WriteFile(filepath.Join(modRoot, "go.mod"), []byte("module cgosotest\n"), 0666); err != nil {
50                 log.Panic(err)
51         }
52
53         cmd := exec.Command("go", "env", "CC", "GOGCCFLAGS")
54         cmd.Dir = modRoot
55         cmd.Stderr = new(strings.Builder)
56         cmd.Env = append(os.Environ(), "GOPATH="+GOPATH)
57         out, err := cmd.Output()
58         if err != nil {
59                 t.Fatalf("%s: %v\n%s", strings.Join(cmd.Args, " "), err, cmd.Stderr)
60         }
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)
64         }
65
66         cc := lines[0]
67         if cc == "" {
68                 t.Fatal("CC environment variable (go env CC) cannot be empty")
69         }
70         gogccflags := strings.Split(lines[1], " ")
71
72         // build shared object
73         ext := "so"
74         args := append(gogccflags, "-shared")
75         switch runtime.GOOS {
76         case "darwin", "ios":
77                 ext = "dylib"
78                 args = append(args, "-undefined", "suppress", "-flat_namespace")
79         case "windows":
80                 ext = "dll"
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")
86         case "aix":
87                 ext = "so.1"
88         }
89         sofname := "libcgosotest." + ext
90         args = append(args, "-o", sofname, "cgoso_c.c")
91
92         cmd = exec.Command(cc, args...)
93         cmd.Dir = modRoot
94         cmd.Env = append(os.Environ(), "GOPATH="+GOPATH)
95         out, err = cmd.CombinedOutput()
96         if err != nil {
97                 t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
98         }
99         t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
100
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")
104                 cmd.Dir = modRoot
105                 out, err = cmd.CombinedOutput()
106                 if err != nil {
107                         t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
108                 }
109         }
110
111         cmd = exec.Command("go", "build", "-o", "main.exe", "main.go")
112         cmd.Dir = modRoot
113         cmd.Env = append(os.Environ(), "GOPATH="+GOPATH)
114         out, err = cmd.CombinedOutput()
115         if err != nil {
116                 t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
117         }
118         t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
119
120         cmd = exec.Command("./main.exe")
121         cmd.Dir = modRoot
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"
127                 }
128                 cmd.Env = append(os.Environ(), s+"=.")
129
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=.")
134                 }
135         }
136         out, err = cmd.CombinedOutput()
137         if err != nil {
138                 t.Fatalf("%s: %s\n%s", strings.Join(cmd.Args, " "), err, out)
139         }
140         t.Logf("%s:\n%s", strings.Join(cmd.Args, " "), out)
141 }