diff --git a/Makefile.defs b/Makefile.defs index f00ff6602..0dd0c0184 100644 --- a/Makefile.defs +++ b/Makefile.defs @@ -8,6 +8,7 @@ else endif UNAME:=$(shell uname -s) +UNAME_ARCH:=$(shell uname -m) # use MAKEFILE_DIR instead of CURDIR for variables that are exported through -include MAKEFILE_DIR=$(dir $(abspath $(lastword $(MAKEFILE_LIST)))) @@ -189,8 +190,14 @@ endif ifneq (,$(filter $(UNAME), Darwin)) export DARWINHOST=1 + ifneq (,$(filter $(UNAME_ARCH), arm64)) + export DARWIN_AARCH64_HOST=1 + endif ifdef EMULATE_READER export DARWIN=1 + ifdef DARWIN_AARCH64_HOST + export DARWIN_AARCH64=1 + endif endif endif @@ -500,6 +507,16 @@ ifdef MACOS HOST_ARCH:=-mtune=generic endif +ifdef DARWIN_AARCH64_HOST +# At the moment of writing, apple-m1 is the lowest common denominator for Apple silicon chips. Should work for M2 as well + HOST_ARCH:=-mcpu=apple-m1 + BREW_PREFIX:=$(shell command brew --prefix 2>/dev/null) + ifneq (,$(BREW_PREFIX)) + export DARWIN_AARCH64_HOST_CFLAG:=-I$(BREW_PREFIX)/include + export DARWIN_AARCH64_HOST_LDFLAG:=-L$(BREW_PREFIX)/lib + endif +endif + # Anything below GCC 6 gets to be forcefully switched to decent standards, because some of our deps require C11 & C++11 support. # NOTE: Technically, this also means we require Clang >= 3.2, but that should more or less hold true on our end, because that's truly ancient. # Tests heavily inspired from Linux's build system ;). diff --git a/Makefile.third b/Makefile.third index 65566118e..f4fa0fa21 100644 --- a/Makefile.third +++ b/Makefile.third @@ -610,7 +610,7 @@ $(OPENSSL_LIB) $(OPENSSL_DIR): $(THIRDPARTY_DIR)/openssl/*.* -DRANLIB="$(RANLIB)" \ -DCHOST="$(CHOST)" \ $(if $(ANDROID),-DNDKABI=$(NDKABI),) \ - -DCONFIG_SCRIPT="$(if $(EMULATE_READER),$(if $(DARWIN),Configure darwin64-x86_64-cc,config),Configure $(if $(WIN32),mingw,))" \ + -DCONFIG_SCRIPT="$(if $(EMULATE_READER),$(if $(DARWIN),Configure $(if $(DARWIN_AARCH64),darwin64-arm64-cc,darwin64-x86_64-cc),config),Configure $(if $(WIN32),mingw,))" \ $(CURDIR)/$(THIRDPARTY_DIR)/openssl && \ $(CMAKE_MAKE_PROGRAM) $(CMAKE_MAKE_PROGRAM_FLAGS) diff --git a/ffi/SDL2_0.lua b/ffi/SDL2_0.lua index 57ee92d37..8dbdac7aa 100644 --- a/ffi/SDL2_0.lua +++ b/ffi/SDL2_0.lua @@ -25,6 +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", + 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 0b46da71f..2271fcd6f 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 +end + require("ffi/posix_h") local util = {} +util.KO_DYLD_PREFIX = getlibprefix() if ffi.os == "Windows" then util.gettime = function() @@ -634,7 +651,7 @@ function util.haveSDL2() if haveSDL2 == nil then local candidates if ffi.os == "OSX" then - candidates = {"libs/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 diff --git a/thirdparty/glib/CMakeLists.txt b/thirdparty/glib/CMakeLists.txt index 459c9a6df..609896b94 100644 --- a/thirdparty/glib/CMakeLists.txt +++ b/thirdparty/glib/CMakeLists.txt @@ -69,8 +69,8 @@ endif() if(NOT DEFINED ENV{DARWIN}) set(LDFLAGS "${LDFLAGS} -Wl,-rpath-link,${ZLIB_DIR}/lib -Wl,-rpath-link,${LIBFFI_DIR}/.libs -Wl,-rpath-link,${BINARY_DIR}/gmodule/.libs") else() - set(CFLAGS "-I/usr/local/opt/gettext/include ${CFLAGS}") - set(LDFLAGS "${LDFLAGS} -L/usr/local/opt/gettext/lib -L${LIBICONV_DIR}/lib -L${ZLIB_DIR}/lib -L${LIBFFI_DIR}/.libs -L${BINARY_DIR}/gmodule/.libs") + set(CFLAGS "-I/usr/local/opt/gettext/include $ENV{DARWIN_AARCH64_HOST_CFLAG} ${CFLAGS}") + set(LDFLAGS "${LDFLAGS} -L/usr/local/opt/gettext/lib $ENV{DARWIN_AARCH64_HOST_LDFLAG} -L${LIBICONV_DIR}/lib -L${ZLIB_DIR}/lib -L${LIBFFI_DIR}/.libs -L${BINARY_DIR}/gmodule/.libs") endif() set(CFG_OPTS "${CFG_OPTS} CFLAGS=\"${CFLAGS}\" LDFLAGS=\"${LDFLAGS}\" ${HOST_OPTS}")