From 2aa7ad98d1b9d2b0dc4af184acfadf135a47f317 Mon Sep 17 00:00:00 2001 From: Piotrek Marciniak Date: Mon, 2 Jan 2023 21:15:39 +0100 Subject: [PATCH 1/6] Makefile / library path modifications to make M1 building work (koreader/koreader#8797) --- Makefile.defs | 11 +++++++++++ Makefile.third | 2 +- ffi/SDL2_0.lua | 2 ++ ffi/util.lua | 2 +- thirdparty/glib/CMakeLists.txt | 4 ++-- 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Makefile.defs b/Makefile.defs index f00ff6602..95be650f4 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 DARWINM1HOST=1 + endif ifdef EMULATE_READER export DARWIN=1 + ifdef DARWINM1HOST + export DARWIN_M1=1 + endif endif endif @@ -500,6 +507,10 @@ ifdef MACOS HOST_ARCH:=-mtune=generic endif +ifdef DARWINM1HOST + HOST_ARCH:=-mcpu=apple-m1 +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..194407a22 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_M1),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..749ef26ee 100644 --- a/ffi/SDL2_0.lua +++ b/ffi/SDL2_0.lua @@ -21,6 +21,8 @@ require("ffi/linux_input_h") ----------------------------------------------------------------- local SDL = util.ffiLoadCandidates{ + "/usr/local/opt/lib/libSDL2.dylib", + "/opt/homebrew/lib/libSDL2.dylib", "SDL2", -- this unfortunately needs to be written in full due to the . in the name "libSDL2-2.0.so", diff --git a/ffi/util.lua b/ffi/util.lua index 0b46da71f..b564efac1 100644 --- a/ffi/util.lua +++ b/ffi/util.lua @@ -634,7 +634,7 @@ function util.haveSDL2() if haveSDL2 == nil then local candidates if ffi.os == "OSX" then - candidates = {"libs/libSDL2.dylib", "SDL2"} + candidates = {"/opt/homebrew/lib/libSDL2.dylib", "libs/libSDL2.dylib", "SDL2"} 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..fb839b642 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 -I/opt/homebrew/include ${CFLAGS}") + set(LDFLAGS "${LDFLAGS} -L/usr/local/opt/gettext/lib -L/opt/homebrew/lib -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}") From c02e773d6d6dd75757b25f9c1c8a20661e91cdcf Mon Sep 17 00:00:00 2001 From: Piotrek Marciniak Date: Mon, 2 Jan 2023 22:28:27 +0100 Subject: [PATCH 2/6] fix ffi libsdl2 candidate order and paths --- ffi/SDL2_0.lua | 4 ++-- ffi/util.lua | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ffi/SDL2_0.lua b/ffi/SDL2_0.lua index 749ef26ee..411246f3b 100644 --- a/ffi/SDL2_0.lua +++ b/ffi/SDL2_0.lua @@ -21,12 +21,12 @@ require("ffi/linux_input_h") ----------------------------------------------------------------- local SDL = util.ffiLoadCandidates{ - "/usr/local/opt/lib/libSDL2.dylib", - "/opt/homebrew/lib/libSDL2.dylib", "SDL2", -- 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", } -- Some features (like SDL_GameControllerRumble) may require a minimum version diff --git a/ffi/util.lua b/ffi/util.lua index b564efac1..e333869e6 100644 --- a/ffi/util.lua +++ b/ffi/util.lua @@ -634,7 +634,7 @@ function util.haveSDL2() if haveSDL2 == nil then local candidates if ffi.os == "OSX" then - candidates = {"/opt/homebrew/lib/libSDL2.dylib", "libs/libSDL2.dylib", "SDL2"} + candidates = {"libs/libSDL2.dylib", "/opt/homebrew/lib/libSDL2.dylib", "SDL2"} else candidates = {"SDL2", "libSDL2-2.0.so", "libSDL2-2.0.so.0"} end From 93fefd4517d051eec374396fb506df816238ca27 Mon Sep 17 00:00:00 2001 From: Piotrek Marciniak Date: Fri, 6 Jan 2023 14:24:28 +0100 Subject: [PATCH 3/6] 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 411246f3b..8dbdac7aa 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 e333869e6..fc0192c66 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() @@ -634,7 +651,7 @@ function util.haveSDL2() if haveSDL2 == nil then local candidates if ffi.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 From 590fbc65ef9a1f60c5fa4fc8937590e9c13f70cf Mon Sep 17 00:00:00 2001 From: Piotrek Marciniak Date: Wed, 22 Mar 2023 19:04:18 +0100 Subject: [PATCH 4/6] CR fixes --- Makefile.defs | 6 +++--- Makefile.third | 2 +- ffi/util.lua | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Makefile.defs b/Makefile.defs index 95be650f4..9fd863278 100644 --- a/Makefile.defs +++ b/Makefile.defs @@ -191,11 +191,11 @@ endif ifneq (,$(filter $(UNAME), Darwin)) export DARWINHOST=1 ifneq (,$(filter $(UNAME_ARCH), arm64)) - export DARWINM1HOST=1 + export DARWIN_AARCH64_HOST=1 endif ifdef EMULATE_READER export DARWIN=1 - ifdef DARWINM1HOST + ifdef DARWIN_AARCH64_HOST export DARWIN_M1=1 endif endif @@ -507,7 +507,7 @@ ifdef MACOS HOST_ARCH:=-mtune=generic endif -ifdef DARWINM1HOST +ifdef DARWIN_AARCH64_HOST HOST_ARCH:=-mcpu=apple-m1 endif diff --git a/Makefile.third b/Makefile.third index 194407a22..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 $(if $(DARWIN_M1),darwin64-arm64-cc,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/util.lua b/ffi/util.lua index fc0192c66..5525bd2b3 100644 --- a/ffi/util.lua +++ b/ffi/util.lua @@ -63,7 +63,7 @@ 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"); + local libprefix = os.getenv("KO_DYLD_PREFIX") if not libprefix then local std_out = io.popen("brew --prefix", "r") @@ -73,7 +73,7 @@ local getlibprefix = function() end end - return libprefix or ""; + return libprefix; end require("ffi/posix_h") From ababb4778492479d90914eb736fdc23c8584180e Mon Sep 17 00:00:00 2001 From: Piotrek Marciniak Date: Wed, 22 Mar 2023 19:31:37 +0100 Subject: [PATCH 5/6] Pass brew prefix through a variable in makefiles --- Makefile.defs | 7 ++++++- thirdparty/glib/CMakeLists.txt | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Makefile.defs b/Makefile.defs index 9fd863278..0f3c4bfd6 100644 --- a/Makefile.defs +++ b/Makefile.defs @@ -196,7 +196,7 @@ ifneq (,$(filter $(UNAME), Darwin)) ifdef EMULATE_READER export DARWIN=1 ifdef DARWIN_AARCH64_HOST - export DARWIN_M1=1 + export DARWIN_AARCH64=1 endif endif endif @@ -509,6 +509,11 @@ 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. diff --git a/thirdparty/glib/CMakeLists.txt b/thirdparty/glib/CMakeLists.txt index fb839b642..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 -I/opt/homebrew/include ${CFLAGS}") - set(LDFLAGS "${LDFLAGS} -L/usr/local/opt/gettext/lib -L/opt/homebrew/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}") From 0db16e1051a335a305153166a8c5348060f299e3 Mon Sep 17 00:00:00 2001 From: Piotrek Marciniak Date: Fri, 24 Mar 2023 10:02:55 +0100 Subject: [PATCH 6/6] Nits and comments --- Makefile.defs | 1 + ffi/util.lua | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile.defs b/Makefile.defs index 0f3c4bfd6..0dd0c0184 100644 --- a/Makefile.defs +++ b/Makefile.defs @@ -508,6 +508,7 @@ ifdef MACOS 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)) diff --git a/ffi/util.lua b/ffi/util.lua index 5525bd2b3..2271fcd6f 100644 --- a/ffi/util.lua +++ b/ffi/util.lua @@ -73,7 +73,7 @@ local getlibprefix = function() end end - return libprefix; + return libprefix end require("ffi/posix_h")