Skip to content

Commit b4a1f66

Browse files
purego: enable testing of amd64 with Rosseta on arm64 macOS (ebitengine#122)
With Rosetta on arm64 it was possible to test amd64 just by setting GOARCH=amd64. However, since the shared library tests were added it is no longer possible because the default compiler will build for the host architecture and not the cross-compiled (amd64) architecture. This commit tells the C/C++ compiler to build for amd64 when running an amd64 binary. This only effects macOS.
1 parent dd97be5 commit b4a1f66

File tree

2 files changed

+41
-39
lines changed

2 files changed

+41
-39
lines changed

callback_test.go

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@
66
package purego_test
77

88
import (
9-
"errors"
10-
"fmt"
119
"os"
12-
"os/exec"
1310
"path/filepath"
14-
"strings"
1511
"testing"
1612
"unsafe"
1713

@@ -24,7 +20,7 @@ func TestCallGoFromSharedLib(t *testing.T) {
2420
libFileName := filepath.Join(t.TempDir(), "libcbtest.so")
2521
t.Logf("Build %v", libFileName)
2622

27-
if err := buildSharedLib(libFileName, filepath.Join("libcbtest", "callback.c")); err != nil {
23+
if err := buildSharedLib("CC", libFileName, filepath.Join("libcbtest", "callback.c")); err != nil {
2824
t.Fatal(err)
2925
}
3026
defer os.Remove(libFileName)
@@ -53,26 +49,6 @@ func TestCallGoFromSharedLib(t *testing.T) {
5349
}
5450
}
5551

56-
func buildSharedLib(libFile string, sources ...string) error {
57-
out, err := exec.Command("go", "env", "CC").Output()
58-
if err != nil {
59-
return fmt.Errorf("go env error: %w", err)
60-
}
61-
62-
cc := strings.TrimSpace(string(out))
63-
if cc == "" {
64-
return errors.New("CC not found")
65-
}
66-
67-
args := append([]string{"-shared", "-Wall", "-Werror", "-o", libFile}, sources...)
68-
cmd := exec.Command(cc, args...)
69-
if out, err := cmd.CombinedOutput(); err != nil {
70-
return fmt.Errorf("compile lib: %w\n%q\n%s", err, cmd, string(out))
71-
}
72-
73-
return nil
74-
}
75-
7652
func TestNewCallbackFloat64(t *testing.T) {
7753
// This tests the maximum number of arguments a function to NewCallback can take
7854
const (

dlfcn_test.go

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package purego_test
77

88
import (
9+
"errors"
910
"fmt"
1011
"os"
1112
"os/exec"
@@ -99,20 +100,8 @@ func TestNestedDlopenCall(t *testing.T) {
99100
libFileName := filepath.Join(t.TempDir(), "libdlnested.so")
100101
t.Logf("Build %v", libFileName)
101102

102-
out, err := exec.Command("go", "env", "CXX").Output()
103-
if err != nil {
104-
t.Fatalf("go env error: %v", err)
105-
}
106-
107-
cxx := strings.TrimSpace(string(out))
108-
if cxx == "" {
109-
t.Fatal("CXX not found")
110-
}
111-
112-
args := []string{"-shared", "-Wall", "-Werror", "-o", libFileName, filepath.Join("libdlnested", "nested.cpp")}
113-
cmd := exec.Command(cxx, args...)
114-
if out, err := cmd.CombinedOutput(); err != nil {
115-
t.Fatalf("compile lib: %v\n%q\n%s", err, cmd, string(out))
103+
if err := buildSharedLib("CXX", libFileName, filepath.Join("libdlnested", "nested.cpp")); err != nil {
104+
t.Fatal(err)
116105
}
117106
defer os.Remove(libFileName)
118107

@@ -123,3 +112,40 @@ func TestNestedDlopenCall(t *testing.T) {
123112

124113
purego.Dlclose(lib)
125114
}
115+
116+
func 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+
var archFlag, arch string
133+
if runtime.GOOS == "darwin" {
134+
archFlag = "-arch"
135+
switch runtime.GOARCH {
136+
case "arm64":
137+
arch = "arm64"
138+
case "amd64":
139+
arch = "x86_64"
140+
default:
141+
return fmt.Errorf("unknown macOS architecture %s", runtime.GOARCH)
142+
}
143+
args = append(args, archFlag, arch)
144+
}
145+
cmd := exec.Command(compiler, append(args, sources...)...)
146+
if out, err := cmd.CombinedOutput(); err != nil {
147+
return fmt.Errorf("compile lib: %w\n%q\n%s", err, cmd, string(out))
148+
}
149+
150+
return nil
151+
}

0 commit comments

Comments
 (0)