Skip to main content

A PEP517 build frontend for cross-compiling wheels.

Project description

xbuild

Python Versions PyPI Version Maturity BSD License Discord server

xbuild is PEP517 build frontend that has additions and extensions to support cross-compiling wheels for platforms where compilation cannot be performed natively - most notably:

  • Android
  • Emscripten (WASM)
  • iOS

Usage

To use xbuild, you need:

  • an install of Python for the platform where you will be performing the build (e.g., macOS, Linux or Windows)
  • a distribution of Python that has been compiled for your target platform (e.g., Android, Emscripten or iOS)

Build a package

Create a virtual environment for your build platform (i.e., the platform where you will be compiling), and install xbuild:

$ python3 -m venv venv
$ source venv/bin/activate
(venv) $ python -m pip install xbuild

You can then run xbuild from the root directory of the project you want to build. You must pass in the --sysconfig argument, providing the path to the sysconfig_vars JSON file for the target platform, or the equivalent sysconfigdata python configuration.

(venv) $ python -m xbuild --sysconfig path/to/_sysconfig_vars__...json

This will create an isolated cross-platform virtual environment, and trigger a PEP 517 build in that environment. Any build requires will be installed for the build platform. For example, if you're running on macOS, building for an ARM64 iPhone simulator, and your project lists ninja as a requirement, the macOS version of ninja will be installed. This ensures that the binary will be executable during the build.

If, for some reason, you require the iOS version of a build requirement to be installed, you can specify target-requires in your build-system table. For example, to add the iOS version of "target-tool" to your isolated cross-build environment, you might use:

[build-system]
requires = ["setuptools"]
target-requires = ["target-tool"]
build_backenmd = "setuptools.build_meta"

In order for a cross-build to succeed, your environment must be configured appropriately for the platform you're targeting.

Android

To build an Android wheel, you must:

  • Have Android Studio or the Android Command-line Tools installed
  • Have ANDROID_HOME configured in your environment
  • Have a Java SDK installed
  • Have JAVA_HOME defined in your environment

iOS

You must have Xcode installed, with the iOS SDK added.

It is also strongly advised that you:

  • Add the path to the iOS binary shims to your path. These are provided in the Python.xcframework/ios-arm64/bin and Python.xcframework/ios-arm64_x86_64-simulator/bin folder for the iOS support package that you have downloaded.
  • Clear your path of any other dependencies. It is very easy for macOS ARM64 binaries from Homebrew and other sources to leak into iOS builds if they are present on the path; the safest approach is to set your path so it only contains:
    • The path for the Python binary (ideally, your virtual environment's bin directory)
    • /usr/bin
    • /bin
    • /usr/sbin
    • /sbin
    • /Library/Apple/usr/bin

Emscripten

TODO

Creating a cross virtual environment

To explicitly create a cross-platform virtual environment, start by creating a virtual environment for your build platform (i.e., the platform where you will be compiling), then use the xvenv script to convert that virtual environment in to a cross environment.

$ python3 -m venv venv
$ source venv/bin/activate
(venv) $ python -m pip install xbuild
(venv) $ python -m venv x-venv
(venv) $ python -m xvenv --sysconfig path/to/_sysconfig_vars__...json x-venv

You can then deactivate the environment that was used to create the cross-platform environment, and activate the cross-platform virtual environment. For example, if x-venv was constructed using an iOS simulator sysconfig vars file (_sysconfig_vars__ios_arm64-iphonesimulator.json), you would see output like:

(venv) $ deactivate
$ source x-venv/bin/activate
(x-venv) $ python -c "import sys; print(sys.platform)"
ios
(x-venv) $ python -c "import sys; print(sys.implementation._multiarch)"
arm64-iphonesimulator

This should now print the platform identifier for the target platform, not your build platform.

You can also configure xvenv with a _sysconfigdata Python file (e.g., _sysconfigdata__ios_arm64-iphonesimulator.py), instead of the _sysconfig_var JSON file. You'll have to use _sysconfigdata if you're on Python 3.13 (as the JSON format was only introduced in Python 3.14)

If you are in the cross-platform environment, and you need to temporarily convert it back to the build platform, you can do so with the XBUILD_ENV environment variable. For example, if x-venv is an iOS cross-platform environment:

$ source x-venv/bin/activate
(x-venv) $ python -c "import sys; print(sys.platform)"
ios
(x-venv) $ XBUILD_ENV=off python -c "import sys; print(sys.platform)"
darwin

If you have an active cross-platform virtual environment, you can run xbuild without providing the --sysconfig variable. The configuration of your existing cross virtual environment will copied into the isolated environment for the build. Alternatively, you can also use the --no-isolation flag to disable the creation of a isolated cross-platform build environment. This will use your existing cross-platform environment as the build environment.

How this works

The cross build environment does not run the target platform binaries on the build platform - it uses binaries for the build platform, but monkey-patches the Python interpreter at startup so that any question asking details about the platform returns details about the target platform. For example, if you create an iOS cross-platform environment on a macOS machine, you'll be using the macOS python.exe; but if you ask for sys.platform, the answer will be ios, not darwin.

Contributing

To set up a development environment:

$ python3 -m venv venv
$ source venv/bin/activate
(venv) $ python -m pip install -U pip
(venv) $ python -m pip install -e . --group dev

Community

xbuild is part of the BeeWare suite. You can talk to the community through:

We foster a welcoming and respectful community as described in our BeeWare Community Code of Conduct.

Contributing

If you experience problems with xbuild, log them on GitHub. If you want to contribute code, please fork the code and submit a pull request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

xbuild-0.2.0.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

xbuild-0.2.0-py3-none-any.whl (18.2 kB view details)

Uploaded Python 3

File details

Details for the file xbuild-0.2.0.tar.gz.

File metadata

  • Download URL: xbuild-0.2.0.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for xbuild-0.2.0.tar.gz
Algorithm Hash digest
SHA256 b13e3c38bc0a6c606552e610adb2e489bf9bcd490fbc120c31e092672585bbeb
MD5 3846f71410c02bde7bbed24d5fbfc10f
BLAKE2b-256 dd75f1d297820c547fe43adfdc5ba2e614243044fedc5e1fbacf7a37066420e6

See more details on using hashes here.

Provenance

The following attestation bundles were made for xbuild-0.2.0.tar.gz:

Publisher: publish.yml on beeware/xbuild

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file xbuild-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: xbuild-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 18.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for xbuild-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 67dae669ecf03580a6fd1361316392d120d218c0a5736e96c4317b482a3f2674
MD5 6539fd8e411196bf2bce3e940ba841d4
BLAKE2b-256 48c5f776f74f4930317e722eb7f568f572b0c18a8a8ff31d531a90f31b3d8105

See more details on using hashes here.

Provenance

The following attestation bundles were made for xbuild-0.2.0-py3-none-any.whl:

Publisher: publish.yml on beeware/xbuild

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page