Skip to content

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

Merged
Frenzie merged 6 commits into
koreader:masterfrom
ptrm:apple-m1-build
Mar 24, 2023
Merged

Makefile / library path modifications to make M1 building work (koreader/koreader#8797)#1570
Frenzie merged 6 commits into
koreader:masterfrom
ptrm:apple-m1-build

Conversation

@ptrm

@ptrm ptrm commented Jan 2, 2023

Copy link
Copy Markdown
Contributor

These are mostly changes mentioned in issue koreader/koreader#8797 implemented. So far koreader builds and shows the emulator.

I used both /usr/local/opt and /opt/homebrew/ Homebrew prefixes, as the Apple Silicon Homebrew path has been only recently established to /opt/homebrew (https://apple.stackexchange.com/a/410829), though some users may have older brew installation with /usr/local/opt prefix (as opposed to /usr/local/ without opt for Intel brew).

image


This change is Reviewable

@ptrm ptrm changed the title Makefile / library path modifications to make M1 building work (#8797) Makefile / library path modifications to make M1 building work (koreader#8797) Jan 2, 2023
@ptrm ptrm changed the title Makefile / library path modifications to make M1 building work (koreader#8797) Makefile / library path modifications to make M1 building work (koreader/koreader#8797) Jan 2, 2023
@ptrm ptrm changed the title Makefile / library path modifications to make M1 building work (koreader/koreader#8797) Makefile / library path modifications to make M1 building work koreader/koreader#8797 Jan 2, 2023
@ptrm ptrm changed the title Makefile / library path modifications to make M1 building work koreader/koreader#8797 Makefile / library path modifications to make M1 building work (koreader/koreader#8797) Jan 2, 2023
Comment thread ffi/SDL2_0.lua Outdated
Comment on lines +24 to +25
"/usr/local/opt/lib/libSDL2.dylib",
"/opt/homebrew/lib/libSDL2.dylib",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This should go lower since SDL2 is the default. See http://luajit.org/ext_ffi_api.html#ffi_load.

On POSIX systems, if the name contains no dot, the extension .so is appended. Also, the lib prefix is prepended if necessary. So ffi.load("z") looks for "libz.so" in the default shared library search path.

On Windows systems, if the name contains no dot, the extension .dll is appended. So ffi.load("ws2_32") looks for "ws2_32.dll" in the default DLL search path.

Comment thread ffi/util.lua Outdated
local candidates
if jit.os == "OSX" then
candidates = {"libs/libSDL2.dylib", "SDL2"}
candidates = {"/opt/homebrew/lib/libSDL2.dylib", "SDL2"}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It looks like this should be added, not replaced.

Comment thread thirdparty/glib/CMakeLists.txt Outdated
Comment on lines +72 to +73
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")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't like this hardcoding.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You mean /opt/homebrew/lib should get a variable and become ${HOMEBREW_PREFIX}/lib?

Also is it a good idea to check for brew binary in Makefile.defs in case of Darwin host and set the HOMEBREW_PREFIX from $(shell brew --prefix)? And could this be passed to the lua files somehow? This way custom prefixes set during brew installation could be covered, though probably people who bother to set them know how to fix their builds later on ;)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Definitely that, but I'd also prefer to be able to plug in e.g. Nix or gentoo-prefix with little effort if reasonably possible. HOMEBREW_PREFIX might allow for that but then the name (at least further down at this point) should really just be something like MAC_LIBRARY_PREFIX. But you could always write MAC_LIBRARY_PREFIX := HOMEBREW_PREFIX up above in defs or something so it's self-documenting without comments.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IIRC, there's a runtime homebrew command that will actually spit out the right prefix for the current system ;).

@ptrm ptrm Jan 3, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yup, there's brew --prefix. I'll try to unhardcode the paths around evening CET.

What about the paths in lua part, can / should they somehow be based on brew --prefix as well?

@NiLuJe NiLuJe Jan 3, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

But SIP probably means that if you exec from an SIP/hardened app, you're essentially fucked? Yay, Apple.

@NiLuJe NiLuJe Jan 3, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So, yeah, seems like a giant "fuck you, FOSS", from Apple. c.f., Homebrew/brew#13481 for more context (notably the hilarious bit about the consequences of stuff like env being hardened, it's fantastic).

TL;DR: Hardcoded search paths it is. >_<".

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think you still meant to base these ones on brew --prefix or some such as well? :-)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes ;).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, just pushed a commit with variables instead of hardcoded brew path ;)

@Frenzie Frenzie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! Looks very promising.

Comment thread ffi/util.lua
@ptrm ptrm requested review from Frenzie and NiLuJe and removed request for Frenzie January 8, 2023 18:17

@NiLuJe NiLuJe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(goddamnit GH review UI sucks)

Comment thread Makefile.defs Outdated
ifneq (,$(filter $(UNAME), Darwin))
export DARWINHOST=1
ifneq (,$(filter $(UNAME_ARCH), arm64))
export DARWINM1HOST=1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: arm64, m2 chips already exist ;p

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

True, not without a reason being called Apple Sillicon ;> Will fix

Comment thread ffi/util.lua
Comment thread ffi/util.lua
Comment thread ffi/util.lua Outdated
Comment thread ffi/util.lua Outdated

@NiLuJe NiLuJe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reviewed 3 of 5 files at r1, 2 of 2 files at r3, all commit messages.
Reviewable status: all files reviewed, 14 unresolved discussions (waiting on @Frenzie and @ptrm)


Makefile.defs line 11 at r3 (raw file):


UNAME:=$(shell uname -s)
UNAME_ARCH:=$(shell uname -m)

nit: I'd rename that to HOST_ARCH (and I'm mildly intrigued by the fact that we don't already have that stored somewhere?).

EDIT: Oh, yeah, we have it, but we use it for the actual march flag, meh :/. Nevermind that one, then.


Makefile.defs line 194 at r3 (raw file):

	export DARWINHOST=1
	ifneq (,$(filter $(UNAME_ARCH), arm64))
		export DARWINM1HOST=1

nit: DARWIN_AARCH64_HOST (extra columns are free!)


Makefile.defs line 199 at r3 (raw file):

		export DARWIN=1
		ifdef DARWINM1HOST
			export DARWIN_M1=1

nit: DARWIN_AARCH64


Makefile.defs line 511 at r3 (raw file):


ifdef DARWINM1HOST
	HOST_ARCH:=-mcpu=apple-m1

And a comment that M1 is the lowest common denominator for Apple AARCH64 silicon for future archeologists ;).


ffi/SDL2_0.lua line 28 at r3 (raw file):

    "libSDL2-2.0.so",
    "libSDL2-2.0.so.0",
    util.KO_DYLD_PREFIX .. "/lib/libSDL2.dylib",

Mildly mystified why this one doesn't have the libs/libSDL2.dylib variant found in util?


ffi/util.lua line 63 at r3 (raw file):

]]

local getlibprefix = function()

Missing a jit.os == "OSX" branch, so we don't try to run brew everywhere.


ffi/util.lua line 64 at r3 (raw file):

local getlibprefix = function()
    -- Apple M1 homebrew installs libraries outside of default searchpaths,

nit: AArch64 ;)


thirdparty/glib/CMakeLists.txt line 73 at r1 (raw file):

Previously, Frenzie (Frans de Jonge) wrote…

I think you still meant to base these ones on brew --prefix or some such as well? :-)

These are still hard-coded ;).

@NiLuJe

NiLuJe commented Jan 8, 2023

Copy link
Copy Markdown
Member

My GH "review" comments may be outdated, I had once again forgotten to hit the big green button.

The Reviewable stuff is current ;).

@NiLuJe

NiLuJe commented Jan 9, 2023

Copy link
Copy Markdown
Member

And I just noticed that I mixed aarch64 w/ arm64 in my renaming requests; aarch64 is technically more correct, but arm64 might be easier on the eyes? ;).

@ptrm ptrm requested a review from NiLuJe March 22, 2023 18:35
Comment thread ffi/util.lua Outdated
end
end

return libprefix;

@NiLuJe NiLuJe Mar 23, 2023

Copy link
Copy Markdown
Member

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
Copy Markdown
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;;;;;;;;;;

@NiLuJe NiLuJe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM besides that very small nit ;).

@ptrm

ptrm commented Mar 24, 2023

Copy link
Copy Markdown
Contributor Author

Removed the semicolon, I'm blind to them at EOL ;) Also added a comment proposed in Reviewable about apple-m1 being lowest common denominator for Apple silicon chips.

@ptrm ptrm requested a review from NiLuJe March 24, 2023 09:12
Comment thread ffi/util.lua

local util = {}

util.KO_DYLD_PREFIX = getlibprefix()

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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. ;-)

Frenzie added a commit to Frenzie/koreader-base that referenced this pull request Apr 2, 2023
Frenzie added a commit that referenced this pull request Apr 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants