From c8f36e32ca6b27ec1d0432fef070ab24925ab6dd Mon Sep 17 00:00:00 2001 From: Piotrek Marciniak Date: Fri, 6 Jan 2023 14:24:28 +0100 Subject: [PATCH] Unhardcode Apple M1 brew paths for SDL2 --- ffi/SDL2_0.lua | 3 +-- ffi/util.lua | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/ffi/SDL2_0.lua b/ffi/SDL2_0.lua index 83ccc5dcb8..51ec7d56c5 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 fc48a8e412..e505eefe75 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