mesonbuild / meson-python

Meson PEP 517 Python build backend
https://mesonbuild.com/meson-python/
MIT License
118 stars 59 forks source link

Cross compiling build error with fortran, MacOS-arm64, and cibuildwheel. #548

Closed joanibal closed 7 months ago

joanibal commented 7 months ago

Hello,

I'm trying to create apple silicon python wheels for my application using cibuildwheel, meson-python, and a macos-12 github runner. However, I can not seem to figure out how to do this.

In particular I can't get passed this error

 ../meson.build:79:0: ERROR: 'fortran' compiler binary not defined in cross or native file

Indeed the meson cross file does not mention fortran and it would seem that it is hard coded here. I'm new to meson, so my apologies if I overlooked something basic.

   [binaries]
   c = ['cc', '-arch', 'arm64']
   cpp = ['c++', '-arch', 'arm64']
   objc = ['cc', '-arch', 'arm64']
   objcpp = ['c++', '-arch', 'arm64']
   [host_machine]
   system = 'darwin'
   cpu = 'arm64'
   cpu_family = 'aarch64'
   endian = 'little'

Most of the CI I have setup for building wheels is based on Scipy since it has a similar set of dependencies (LAPACK and gfortran). I have tried to mirror what they have done to create MacOS-arm64 wheels, but their process for MacOS-arm64 wheels was much less clear to me.

Thank you for any help!

rgommers commented 7 months ago

Most of the CI I have setup for building wheels is based on Scipy since it has a similar set of dependencies (LAPACK and gfortran). I have tried to mirror what they have done to create MacOS-arm64 wheels, but their process for MacOS-arm64 wheels was much less clear to me.

A quick hint on this: SciPy uses Cirrus CI, which provides native macOS arm64 runners. There's some cibuildwheel config at the bottom of its pyproject.toml, and then:

dnicolodi commented 7 months ago

Does Xcode come with a fortran compiler capable of cross compilation?

joanibal commented 7 months ago

Thanks @rgommers , I hadn't realized that Cirrus Ci had macOS arm64 runners. I was hesitant to add another CI provider, but I now see that it will likely be much easier than cross compiling on macOS.

joanibal commented 7 months ago

Does Xcode come with a fortran compiler capable of cross compilation?

I'm new to apple, but I don't believe Xcode comes with any Fortran compiler. I was planning on using gfortran for that reason. However, I now see that I had taken for granted that gfortran could cross compile from macOS x86_64 to macOS arm64. From what I can find here, even if cross compiling is possible getting a capable gfortran compiler seems complicated.

I'm going to close this issue since I see that it is best to avoid cross compiling altogether in this case. Thank you for your help!

rgommers commented 6 months ago

Fortran compilers are indeed a major issue, on macOS and even more on Windows. Apple doesn't provide one, and the Gfortran builds one can find try to keep compatibility with Apple's Clang, but there are regularly issues when there's an OS update. They also require specific macOS SDK versions, and for wheels require bundling libgcc, libgfortran and libquadmath (delocate will take care of that, but to be a good citizen you should note in your license that these are LGPL components).

This is all messy, and I recommend looking at this very recent PR that streamlined this as much as possible: https://github.com/numpy/numpy/pull/25253.

joanibal commented 6 months ago

Switching to Cirrus CI and following the configuration files from Scipy/Numpy fixed my issues and I can now build wheels for macOS-arm64. Thanks for your help!

Now that macOS-arm64 is in a good place, I'll have to give the window wheels another shot. I got close before (again basically just coping the workflow from Scipy), but got stuck on linking errors related to "relocation truncated to fit: IMAGE_REL_AMD64_REL32 against symbol". I see that you recently modified the meson.build files in Scipy for windows, so I'll make the corresponding changes in my project to see if that does the trick.

Thanks again for all your help!

rgommers commented 6 months ago

That sounds like progress, thanks for the update @joanibal. The "relocation truncated" issue doesn't ring a bell for me.