uspgamedev / luasteam

Bindings to connect lua with Steam API
https://luasteam.readthedocs.io/en/stable/
MIT License
134 stars 25 forks source link

Apple Silicon Support #39

Closed ellraiser closed 3 months ago

ellraiser commented 5 months ago

Heya!

Currently making a game with LÖVE on an Apple Silicon machine, so when trying to require() the luasteam.so in the 3.X releases I get the following:

error loading module 'luasteam' from file './luasteam.so':
'./luasteam.so' (mach-o file, but is an incompatible architecture (have (x86_64), need (arm64e))),

Building locally with make osx errored with the following:

ld: warning: ignoring file ./third-party/lib/libluajit-5.1.a, 
building for macOS-arm64 but attempting to link with file built for macOS-x86_64
Undefined symbols for architecture arm64:
...lots of lua_* symbols here...

Which makes sense as I just downloaded the libluajit-5.1.a from the repo here not realising that was x86_64 too - if I instead use the libluajit-5.1.a that I get from installing locally with brew I can build a new luasteam.so for arm64 using the same unmodified make osx

Looking on the apple docs for silicon they mention that you can create two binaries, a standard + silicon one, and then combine them together to make a 'universal' binary to work for all mac architectures: https://developer.apple.com/documentation/apple-silicon/building-a-universal-macos-binary

I needed the two different libluajit-5.1.a files but I was able to build a universal library by modifying the cmake file to be:

osx:
    $(CXX) $(SRC) $(CPP_FLAGS) ${STEAM_LIB}/osx/libsteam_api.dylib ${THIRD_PARTY}/lib/libluajit-5.1-x86_64.a -o x86_64_temp -shared -fPIC $(OSX_FLAGS) -target x86_64-apple-macos
    $(CXX) $(SRC) $(CPP_FLAGS) ${STEAM_LIB}/osx/libsteam_api.dylib ${THIRD_PARTY}/lib/libluajit-5.1-arm64.a -o arm64_temp -shared -fPIC $(OSX_FLAGS) -target arm64-apple-macos
    lipo -create -output $(OSX_OUT) x86_64_temp arm64_temp

Didn't want to raise a PR as I don't know much about Travis (not sure if -target arm64-apple-macos would require a silicon device to work?), I dunno how the include/ works for being able to add the 2 diff. luajit versions and also (most importantly) because I can't test the resulting universal .so on a x86_64 mac device yet - so I just wanted to raise it here in case it's useful for other silicon devs or if it's planned to be added in future eventually (even if it's just to the building docs)

1bardesign commented 5 months ago

This is going to be very helpful for us, thanks

yancouto commented 5 months ago

Thanks for the instructions! I mostly maintain this repo, but currently don't have a Mac to test this stuff out, or test it in Travis (which is kind of tricky...), and I have 0 knowledge of this universal binary stuff. Feel free to submit a PR either for the build or for the instructions in the documentation, I'll test to the best of my abilites.

jamespanic commented 3 months ago

I just had to go through this process as well by recompiling luajit as a universal and then updating the third-party folder in this repo. I can provide a PR if its desired.

yancouto commented 3 months ago

If you wish, go for it! The trickiest part would be to get it working on Travis, as testing on Travis is tricky, but if you understand that, I can review the PR and help see if Travis is working out.

jamespanic commented 3 months ago

I just submitted two PRs. This one needs to be merged into the submodule and the submodule pointer updated

https://github.com/uspgamedev/luasteam-third-party/pull/1

And this one updates the makefile

https://github.com/uspgamedev/luasteam/pull/45