-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
Making cffi work on Mac M1 #190
Comments
I read in one of the linked issues that the libffi from homebrew could be troublesome. So I tried removing libffi but ended up removing both my x86 and arm installations of brew completely because for whatever reason I could not get pyenv to build and install a python version anymore :/ I now have only one brew installation (arm64) where I just installed the following packages: Guess what: I ran the test suite with a lot less problems (I did add the changes of @marcdownie)! It is not perfect yet (there is still quite some stuff failing) but at least cffi seems to work this way. |
I can confirm that having |
Yep, the second brew installation seems to be the reason why I got the error with Edit: weird: |
So could the order in which the linker iterates through the matching binaries be the source of the issue? |
Yeah, I guess. I could try to find out how the linker locates the binaries to see if I can fix it with the right configuration. |
Ok, here is what I know: Apparently, linking to the libffi from brew on arm64 is not a good idea because it throws the ImportError. It is possible to provide Gonna try now! 🤞 |
No luck... The weird thing is that I don't see my env var show up in the generated clang command: clang -Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -DNDEBUG -g -fwrapv -O3 -Wall -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include -DUSE__THREAD -DHAVE_SYNC_SYNCHRONIZE -I/opt/homebrew/Cellar/libffi/3.3_3/include -I/Users/cg/Library/Caches/pypoetry/virtualenvs/wgpu-5bSf_T1V-py3.9/include -I/Users/cg/.pyenv/versions/3.9.7/Library/Frameworks/Python.framework/Versions/3.9/include/python3.9 -c c/_cffi_backend.c -o build/temp.macosx-11.6-arm64-3.9/c/_cffi_backend.o -iwithsysroot/usr/include/ffi
clang -bundle -undefined dynamic_lookup -L/opt/homebrew/opt/readline/lib -L/opt/homebrew/opt/readline/lib -L/Users/cg/.pyenv/versions/3.9.7/lib -L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib -L/opt/homebrew/opt/readline/lib -L/opt/homebrew/opt/readline/lib -L/Users/cg/.pyenv/versions/3.9.7/lib -L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib build/temp.macosx-11.6-arm64-3.9/c/_cffi_backend.o -L/opt/homebrew/Cellar/libffi/3.3_3/lib -lffi -o build/lib.macosx-11.6-arm64-3.9/_cffi_backend.cpython-39-darwin.so Build command I used was: PKG_CONFIG_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/ffi poetry run pip install -vvv cffi --no-binary :all: I've also tried to export the PKG_CONFIG_PATH var, but to no avail. |
Maybe this is silly, but PKG_CONFIG_PATH appears to point to an executable called |
You probably have a point. But then I'm out of ideas of what to specify there. I can't find any For now I can work around it by either not putting brew on my path or removing |
As far as I can see they are asking brew for the path to libffi via Also.. the |
def use_pkg_config():
if sys.platform == 'darwin' and os.path.exists('/usr/local/bin/brew'):
use_homebrew_for_libffi() this will skip using the homebrew libffi for Arm users because the homebrew path is maybe the problem partly comes if you also have an x64 homebrew installed (e.g. to run in a copy of terminal app using Rosetta)... There were some blog posts advising a setup like that when the M1 Mac first came out, when a lot of things didn't work under Arm yet (I started out that way too but since got rid of the x64 stuff and just use the Arm native homebrew now) |
The right brew is selected. And linking to the brew version instead of system makes cffi fail. |
Problem 1: building cffi and linking to arm brew libffi results in failure (I don't know how to fix this) Problem 2: code in setup.py of cffi has a hardcoded path check to default x86 version to see if homebrew is installed (and will link to brew version of libffi, if present) (I can fix this and it provides me a work-around(s) to bypass linking to libffi) |
fwiw:
this much all works for me (on M1) without errors, and I can I also don't get the error shown here https://foss.heptapod.net/pypy/cffi/-/issues/506 where where it breaks down is if I then copy the files from the wgpu
this is the error I noted on the other issue #136 (comment) Sorry, I don't know if I've added anything to the discussion here |
@anentropic Looks like you've used pip to install wgpu, which is good. However, the macos arm64 wheels aren't on pypi yet, so you got an sdist (source distribution). Those don't include the binaries. That's to be expected, sort of. I could try to make setup.py execute the binary downloading process so that sdists should also "just work"... I'm working on providing the wheels you need in #194 though! |
If you build against the x86 brew libffi, and then link to the arm brew libffi at runtime (or the other way around), failure is expected. So the way to fix it would be to ensure you're building and linking against the same libffi. |
Yes, it works! I also notice that arm wheels are available now for macOS, so no local builds are performed now. Shall I bump the version in |
WOOHOO! Please bump the minimum required version! |
I believe that the prerelease cffi has fixed the |
I did not expect the memoryerror to disappear so easily, but good news! :) |
Yep, examples are working for me now if I checkout latest src and follow https://github.com/pygfx/wgpu-py#developers 👍 |
I was facing a similar error while building cffi on MAC with M1 chip. But then I used environment that uses python 3.11 and somehow the issue got fixed. Just commenting a possible alternative here in case someone faces similar issue long after this issue is closed. |
See (the top post of) #136 for the bigger picture. I'm trying to collect what we know here (and may update).
It looks like we have two problems with CFFI.
1. It won't install correctly by default
Problem
There are no binary wheels provided for macos_arm64, so it tries to use the x86 one (or something like this). Also see the corresponding issue: https://foss.heptapod.net/pypy/cffi/-/issues/506 - a workaround has been proposed, and it looks like they'll wait for someone else to fix it.
This error that you'd get:
Solutions
Ideally someone (not me) steps up and helps cffi build wheels for macos_arm64. No idea how hard this is.
A workaround has been proposed:
And a somewhat more complicated version:
I've heard mixed results so far though.
It looks like installing via conda produces a working version.
If we can confirm that the above workaround works, we can add that in our installation instructions: "bla use conda or pip install cffi --no-binary :all:".
2. Callbacks are broken
Something to do with how callbacks work in cffi, and Mac nagging that the binary is unsigned. Read more here: https://cffi.readthedocs.io/en/latest/using.html#callbacks-old-style
The error that you'd get:
The cffi docs mention that the "new callback mechanism" does not have this problem. We only have 2 callbacks, so we might be able to fix this. I'll look into this.
cc @Korijn @berendkleinhaneveld @marcdownie @SuperSimon81 @anentropic
The text was updated successfully, but these errors were encountered: