Cross-compiled SDL2 on macOS from x86_64 into ARM64 fails to work


Recently I left the issue that explains my problem. I have the cross-platform game that uses SDL2. I do build my own copy of SDL2 from the source via CMake together with my game’s build. I do use the SDL 2.0.14 stable version copy at me. I link the SDL2 into my game statically.

So, when I compile the game on x86_64 into ARM64 (by specifying the -DCMAKE_OSX_ARCHITECTURES="arm64" cmake flag), the final result won’t work: it fails to run the thread because of no threads support, and it fails to create the window at all. My friend who owns the M1 machine, had a theory that this failure is a result because I used the Catalina as a build environment. He did an attempt to build the library and the game using his x86_64 BigSur machine. However, this build also won’t work on the M1. However, when building the game on the M1 itself, it works successfully. The faulty build appears when do the cross-compilation from x86_64.

This is a big problem, because all public macOS CI builders I know using x86_64 architecture only, and I don’t own M1 by myself. So, the only a cross-compilation allows me to build the game for M1 architecture. Is this a failure of the Apple side, or there is some thing possible at SDL2 CMake build? Anyway, when I built the SDL2, the log has shown that all parts got found, built and linked properly, but in the runtime it won’t work at all.

Have you tried building SDL with the Xcode project file that the source ships with?

I have just reproduced the same issue. Strange.

I know the official binaries work, and the Xcode project probably works. Here’s another thing, for the project WohlstandFox is building, they are also building SDL2 for static linking. Does it matter for license concerns when it’s an Open Source project? It may matter for the building concern as well. Does the Xcode project shipping with it support static builds?

they are also building SDL2 for static linking. Does it matter for license concerns when it’s an Open Source project?

SDL2 is under the zlib license; you can statically link it, even to closed-source software.

Okay, the past-due have been found the reason for the bug and gave the fix for it, I made the build with that patch and @kode54 have been tested the result, it works!

I made the pull request with past-due’s patch here: build: A quick fix of the macOS CMake cross-build by Wohlstand · Pull Request #4282 · libsdl-org/SDL · GitHub

1 Like

Yep, I can confirm from my testing that this simple fix works. Would be nice to get it merged.

I’ve also submitted it to the vcpkg port: [sdl2] Fix pthread detection on macOS by past-due · Pull Request #17106 · microsoft/vcpkg · GitHub