1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2023 The Ebitengine Authors
3
4//go:build darwin || linux
5
6package purego_test
7
8import (
9 "errors"
10 "fmt"
11 "os"
12 "os/exec"
13 "path/filepath"
14 "runtime"
15 "strings"
16 "testing"
17 "unsafe"
18
19 "github.com/ebitengine/purego"
20)
21
22func getSystemLibrary() (string, error) {
23 switch runtime.GOOS {
24 case "darwin":
25 return "/usr/lib/libSystem.B.dylib", nil
26 case "linux":
27 return "libc.so.6", nil
28 default:
29 return "", fmt.Errorf("GOOS=%s is not supported", runtime.GOOS)
30 }
31}
32
33func TestRegisterFunc(t *testing.T) {
34 library, err := getSystemLibrary()
35 if err != nil {
36 t.Errorf("couldn't get system library: %s", err)
37 }
38 libc, err := purego.Dlopen(library, purego.RTLD_NOW|purego.RTLD_GLOBAL)
39 if err != nil {
40 t.Errorf("failed to dlopen: %s", err)
41 }
42 var puts func(string)
43 purego.RegisterLibFunc(&puts, libc, "puts")
44 puts("Calling C from from Go without Cgo!")
45}
46
47func ExampleNewCallback() {
48 if runtime.GOOS == "linux" {
49 // TODO: enable once callbacks are working properly on Linux
50 fmt.Println("1 2 3 4 5 6 7 8 9\n45")
51 return
52 }
53
54 cb := purego.NewCallback(func(a1, a2, a3, a4, a5, a6, a7, a8, a9 int) int {
55 fmt.Println(a1, a2, a3, a4, a5, a6, a7, a8, a9)
56 return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9
57 })
58
59 var fn func(a1, a2, a3, a4, a5, a6, a7, a8, a9 int) int
60 purego.RegisterFunc(&fn, cb)
61
62 ret := fn(1, 2, 3, 4, 5, 6, 7, 8, 9)
63 fmt.Println(ret)
64
65 //Output: 1 2 3 4 5 6 7 8 9
66 // 45
67}
68
69func Test_qsort(t *testing.T) {
70 if runtime.GOOS == "linux" {
71 // TODO: enable once callbacks are working properly on Linux
72 t.SkipNow()
73 }
74
75 library, err := getSystemLibrary()
76 if err != nil {
77 t.Errorf("couldn't get system library: %s", err)
78 }
79 libc, err := purego.Dlopen(library, purego.RTLD_NOW|purego.RTLD_GLOBAL)
80 if err != nil {
81 t.Errorf("failed to dlopen: %s", err)
82 }
83
84 var data = []int{88, 56, 100, 2, 25}
85 var sorted = []int{2, 25, 56, 88, 100}
86 compare := func(a, b *int) int {
87 return *a - *b
88 }
89 var qsort func(data []int, nitms uintptr, size uintptr, compar func(a, b *int) int)
90 purego.RegisterLibFunc(&qsort, libc, "qsort")
91 qsort(data, uintptr(len(data)), unsafe.Sizeof(int(0)), compare)
92 for i := range data {
93 if data[i] != sorted[i] {
94 t.Errorf("got %d wanted %d at %d", data[i], sorted[i], i)
95 }
96 }
97}
98
99func TestNestedDlopenCall(t *testing.T) {
100 libFileName := filepath.Join(t.TempDir(), "libdlnested.so")
101 t.Logf("Build %v", libFileName)
102
103 if err := buildSharedLib("CXX", libFileName, filepath.Join("libdlnested", "nested.cpp")); err != nil {
104 t.Fatal(err)
105 }
106 defer os.Remove(libFileName)
107
108 lib, err := purego.Dlopen(libFileName, purego.RTLD_NOW|purego.RTLD_GLOBAL)
109 if err != nil {
110 t.Fatalf("Dlopen(%q) failed: %v", libFileName, err)
111 }
112
113 purego.Dlclose(lib)
114}
115
116func buildSharedLib(compilerEnv, libFile string, sources ...string) error {
117 out, err := exec.Command("go", "env", compilerEnv).Output()
118 if err != nil {
119 return fmt.Errorf("go env %s error: %w", compilerEnv, err)
120 }
121
122 compiler := strings.TrimSpace(string(out))
123 if compiler == "" {
124 return errors.New("compiler not found")
125 }
126
127 args := []string{"-shared", "-Wall", "-Werror", "-o", libFile}
128
129 // macOS arm64 can run amd64 tests through Rossetta.
130 // Build the shared library based on the GOARCH and not
131 // the default behavior of the compiler.
132 if runtime.GOOS == "darwin" {
133 var arch string
134 switch runtime.GOARCH {
135 case "arm64":
136 arch = "arm64"
137 case "amd64":
138 arch = "x86_64"
139 default:
140 return fmt.Errorf("unknown macOS architecture %s", runtime.GOARCH)
141 }
142 args = append(args, "-arch", arch)
143 }
144 cmd := exec.Command(compiler, append(args, sources...)...)
145 if out, err := cmd.CombinedOutput(); err != nil {
146 return fmt.Errorf("compile lib: %w\n%q\n%s", err, cmd, string(out))
147 }
148
149 return nil
150}