diff --git a/ffi/SDL2_0.lua b/ffi/SDL2_0.lua index 83ccc5dcb..51ec7d56c 100644 --- a/ffi/SDL2_0.lua +++ b/ffi/SDL2_0.lua @@ -25,8 +25,7 @@ local SDL = util.ffiLoadCandidates{ -- this unfortunately needs to be written in full due to the . in the name "libSDL2-2.0.so", "libSDL2-2.0.so.0", - "/usr/local/opt/lib/libSDL2.dylib", - "/opt/homebrew/lib/libSDL2.dylib", + util.KO_DYLD_PREFIX .. "/lib/libSDL2.dylib", } -- Some features (like SDL_GameControllerRumble) may require a minimum version diff --git a/ffi/util.lua b/ffi/util.lua index fc48a8e41..e505eefe7 100644 --- a/ffi/util.lua +++ b/ffi/util.lua @@ -60,10 +60,27 @@ int WideCharToMultiByte( ); ]] +local getlibprefix = function() + -- Apple M1 homebrew installs libraries outside of default searchpaths, + -- and dyld environment variables are sip-protected on MacOS, cf. https://github.com/Homebrew/brew/issues/13481#issuecomment-1181592842 + local libprefix = os.getenv("KO_DYLD_PREFIX"); + + if not libprefix then + local std_out = io.popen("brew --prefix", "r") + if std_out then + libprefix = std_out:read("*line") + std_out:close() + end + end + + return libprefix or ""; +end + require("ffi/posix_h") local util = {} +util.KO_DYLD_PREFIX = getlibprefix() if ffi.os == "Windows" then util.gettime = function() @@ -619,7 +636,7 @@ function util.haveSDL2() if haveSDL2 == nil then local candidates if jit.os == "OSX" then - candidates = {"libs/libSDL2.dylib", "/opt/homebrew/lib/libSDL2.dylib", "SDL2"} + candidates = {"libs/libSDL2.dylib", "SDL2", util.KO_DYLD_PREFIX .. "/lib/libSDL2.dylib"} else candidates = {"SDL2", "libSDL2-2.0.so", "libSDL2-2.0.so.0"} end