hatch icon indicating copy to clipboard operation
hatch copied to clipboard

Cross Compilation in Build Hook

Open JP-Ellis opened this issue 1 year ago • 5 comments

Summary

What are the best practices for detecting the target architecture when cross-compiling a platform-specific wheel using a Hatch build script?

Background

I am building a wheel which includes a binary extension compiled using cffi. I have a custom build hook which performs the following steps:

  1. Detect the architecture, using packaging's sys_tags
  2. Download the pre-compiled library for the target architecture
  3. Build the binary extension

I recently tried to enable support for Windows ARM builds; however, ran into issues when trying to build the wheels using cibuildwheel due to a mismatch between what sys_tags returns, and the actual target achitecture. I was wondering first whether it was an issue with cibuildwheel and asked the question over at

  • pypa/cibuildwheel#1942.

Also from the cibuildwheel docs:

Cross-compilation on Windows relies on a supported build backend. Supported backends use an environment variable to specify their target platform (the one they are compiling native modules for, as opposed to the one they are running on), which is set in cibuildwheels/windows.py before building. Currently, setuptools>=65.4.1 and setuptools_rust are the only supported backends.

It is unclear, at this stage, what should be changed. I can see a couple options:

  1. cibuildwheel is updated to ensure compatibility with packaging (or vice-versa),
  2. hatch exposes an API so that a build script can know what target is being built.

I do wonder whether you think (2) should be implemented regardless. Hatch evidently has some logic to determine the tags, but that is (afaik) hidden and not otherwise accessible.

JP-Ellis avatar Jul 23 '24 01:07 JP-Ellis

https://hatch.pypa.io/latest/plugins/build-hook/reference/#hatchling.builders.hooks.plugin.interface.BuildHookInterface.target_name

ofek avatar Jul 23 '24 03:07 ofek

It isn't obvious to me that target_name will give my the interpreter/ABI/platform triple 🤔.

If it does have all that information, it might be worth updating the docs and/or providing examples of values this target_name might take.

I'll take a look this afternoon though 👍

JP-Ellis avatar Jul 23 '24 03:07 JP-Ellis

sorry that is correct, I skimmed and saw the word target which refers to something specific in Hatch-land, I will read this further tomorrow

ofek avatar Jul 23 '24 04:07 ofek

I just did a really quick check, and I'm finding that target_name takes on the value 'wheel' which is not quite what I'm looking for :/

Appreciate you looking into this though!

JP-Ellis avatar Jul 23 '24 06:07 JP-Ellis

Just following up on this ticket in case you identified anything useful?

JP-Ellis avatar Sep 04 '24 04:09 JP-Ellis