Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Makefile / library path modifications to make M1 building work (koreader/koreader#8797) #1570

Merged
merged 6 commits into from Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 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,15 @@ ifdef MACOS
HOST_ARCH:=-mtune=generic
endif

ifdef DARWIN_AARCH64_HOST
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()
ptrm marked this conversation as resolved.
Show resolved Hide resolved
-- 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;
Copy link
Member

@NiLuJe NiLuJe Mar 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: trailing semicolon

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, better add some more:

Suggested change
return libprefix;
return libprefix;;;;;;;;;;

end

require("ffi/posix_h")

local util = {}

util.KO_DYLD_PREFIX = getlibprefix()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be called except on OS X, so probably on line 654 below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, thanks for the unbreaking commit elsewhere, I didn't consider this might throw an exception ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good thing the CI caught it. ;-)


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