1 // Copyright 2023 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 TestStdio(t *testing.T) {
21 testenv.MustHaveCGO(t)
23 run(t, "testdata/stdio", false)
26 func TestCall(t *testing.T) {
27 testenv.MustHaveCGO(t)
30 run(t, "testdata/callback", false, "Call")
31 t.Run("lto", func(t *testing.T) { run(t, "testdata/callback", true, "Call") })
34 func TestCallback(t *testing.T) {
35 testenv.MustHaveCGO(t)
38 run(t, "testdata/callback", false, "Callback")
39 t.Run("lto", func(t *testing.T) { run(t, "testdata/callback", true, "Callback") })
42 func run(t *testing.T, dir string, lto bool, args ...string) {
43 runArgs := append([]string{"run", "."}, args...)
44 cmd := exec.Command("go", runArgs...)
47 const cflags = "-flto -Wno-lto-type-mismatch -Wno-unknown-warning-option"
48 cmd.Env = append(cmd.Environ(),
50 "CGO_CXXFLAGS="+cflags,
51 "CGO_LDFLAGS="+cflags)
53 out, err := cmd.CombinedOutput()
54 if string(out) != "OK\n" {
55 t.Errorf("%s", string(out))
62 func mustHaveCxx(t *testing.T) {
63 // Ask the go tool for the CXX it's configured to use.
64 cxx, err := exec.Command("go", "env", "CXX").CombinedOutput()
66 t.Fatalf("go env CXX failed: %s", err)
68 args, err := quoted.Split(string(cxx))
70 t.Skipf("could not parse 'go env CXX' output %q: %s", string(cxx), err)
73 t.Skip("no C++ compiler")
75 testenv.MustHaveExecPath(t, string(args[0]))
83 func mustHaveSwig(t *testing.T) {
88 // The first call will skip t with a nice message. On later calls, we just skip.
90 t.Skip("swig not found")
94 func mustHaveSwigOnce(t *testing.T) {
95 swig, err := exec.LookPath("swig")
97 t.Skipf("swig not in PATH: %s", err)
100 // Check that swig was installed with Go support by checking
101 // that a go directory exists inside the swiglib directory.
102 // See https://golang.org/issue/23469.
103 output, err := exec.Command(swig, "-go", "-swiglib").Output()
105 t.Skip("swig is missing Go support")
107 swigDir := strings.TrimSpace(string(output))
109 _, err = os.Stat(filepath.Join(swigDir, "go"))
111 t.Skip("swig is missing Go support")
114 // Check that swig has a new enough version.
115 // See https://golang.org/issue/22858.
116 out, err := exec.Command(swig, "-version").CombinedOutput()
118 t.Skipf("failed to get swig version:%s\n%s", err, string(out))
121 re := regexp.MustCompile(`[vV]ersion +(\d+)([.]\d+)?([.]\d+)?`)
122 matches := re.FindSubmatch(out)
124 // Can't find version number; hope for the best.
125 t.Logf("failed to find swig version, continuing")
130 atoi := func(s string) int {
131 x, err := strconv.Atoi(s)
132 if err != nil && parseError == nil {
137 var major, minor, patch int
138 major = atoi(string(matches[1]))
139 if len(matches[2]) > 0 {
140 minor = atoi(string(matches[2][1:]))
142 if len(matches[3]) > 0 {
143 patch = atoi(string(matches[3][1:]))
145 if parseError != nil {
146 t.Logf("error parsing swig version %q, continuing anyway: %s", string(matches[0]), parseError)
149 t.Logf("found swig version %d.%d.%d", major, minor, patch)
150 if major < 3 || (major == 3 && minor == 0 && patch < 6) {
151 t.Skip("test requires swig 3.0.6 or later")