Skip to content

Commit

Permalink
Makefile / library path modifications to make M1 building work (#1570)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrm committed Mar 24, 2023
1 parent f16948b commit 30bf21c
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
17 changes: 17 additions & 0 deletions Makefile.defs
Expand Up @@ -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))))
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 ;).
Expand Down
2 changes: 1 addition & 1 deletion Makefile.third
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions ffi/SDL2_0.lua
Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion ffi/util.lua
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions thirdparty/glib/CMakeLists.txt
Expand Up @@ -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}")
Expand Down

0 comments on commit 30bf21c

Please sign in to comment.