|
| 1 | +# Building wheels |
| 2 | + |
| 3 | +Because `setuptools_rust` is an extension to `setuptools`, the standard `setup.py bdist_wheel` command is used to build wheels which can be uploaded to pypy. |
| 4 | + |
| 5 | +This doc suggests two ways to go about this. |
| 6 | + |
| 7 | +## Using `cibuildwheel` |
| 8 | + |
| 9 | +[`cibuildwheel`][cibuildwheel] is a tool to build wheels for multiple platforms using Github Actions. |
| 10 | + |
| 11 | +The [`rtoml` package does this, for example](https://github.com/samuelcolvin/rtoml/blob/143ee0907bba616cbcd5cc58eefe9000fcc2b5f2/.github/workflows/ci.yml#L99-L195). |
| 12 | + |
| 13 | +## Building manually |
| 14 | + |
| 15 | +Place a script called `build-wheels.sh` with the following contents in your project root (next to the `setup.py` file): |
| 16 | + |
| 17 | +```bash |
| 18 | +#!/bin/bash |
| 19 | +set -ex |
| 20 | + |
| 21 | +curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain stable -y |
| 22 | +export PATH="$HOME/.cargo/bin:$PATH" |
| 23 | + |
| 24 | +cd /io |
| 25 | + |
| 26 | +for PYBIN in /opt/python/cp{36,37,38,39}*/bin; do |
| 27 | + "${PYBIN}/pip" install -U setuptools wheel setuptools-rust |
| 28 | + "${PYBIN}/python" setup.py bdist_wheel |
| 29 | +done |
| 30 | + |
| 31 | +for whl in dist/*.whl; do |
| 32 | + auditwheel repair "$whl" -w dist/ |
| 33 | +done |
| 34 | +``` |
| 35 | + |
| 36 | +This script can be used to produce wheels for multiple Python versions. |
| 37 | + |
| 38 | +### Binary wheels on linux |
| 39 | + |
| 40 | +To build binary wheels on linux, you need to use the [manylinux docker container](https://github.com/pypa/manylinux). You also need a `build-wheels.sh` similar to [the one in the example](https://github.com/PyO3/setuptools-rust/blob/main/examples/html-py-ever/build-wheels.sh), which will be run in that container. |
| 41 | + |
| 42 | +First, pull the `manylinux2014` Docker image: |
| 43 | + |
| 44 | +```bash |
| 45 | +docker pull quay.io/pypa/manylinux2014_x86_64 |
| 46 | +``` |
| 47 | + |
| 48 | +Then use the following command to build wheels for supported Python versions: |
| 49 | + |
| 50 | +```bash |
| 51 | +docker run --rm -v `pwd`:/io quay.io/pypa/manylinux2014_x86_64 bash /io/build-wheels.sh |
| 52 | +``` |
| 53 | + |
| 54 | +This will create wheels in the `dist` directory: |
| 55 | + |
| 56 | +```bash |
| 57 | +$ ls dist |
| 58 | +hello_rust-0.1.0-cp36-cp36m-linux_x86_64.whl hello_rust-0.1.0-cp36-cp36m-manylinux2014_x86_64.whl |
| 59 | +hello_rust-0.1.0-cp37-cp37m-linux_x86_64.whl hello_rust-0.1.0-cp37-cp37m-manylinux2014_x86_64.whl |
| 60 | +hello_rust-0.1.0-cp38-cp38-linux_x86_64.whl hello_rust-0.1.0-cp38-cp38-manylinux2014_x86_64.whl |
| 61 | +hello_rust-0.1.0-cp39-cp39-linux_x86_64.whl hello_rust-0.1.0-cp39-cp39-manylinux2014_x86_64.whl |
| 62 | +``` |
| 63 | + |
| 64 | +You can then upload the `manylinux2014` wheels to pypi using [twine](https://github.com/pypa/twine). |
| 65 | + |
| 66 | +It is possible to use any of the `manylinux` docker images: `manylinux1`, `manylinux2010` or `manylinux2014`. (Just replace `manylinux2014` in the above instructions with the alternative version you wish to use.) |
| 67 | + |
| 68 | +### Binary wheels on macOS |
| 69 | + |
| 70 | +For building wheels on macOS it is sufficient to run the `bdist_wheel` command, i.e. `setup.py bdist_wheel`. |
| 71 | + |
| 72 | +To build `universal2` wheels set the `ARCHFLAGS` environment variable to contain both `x86_64` and `arm64`, for example `ARCHFLAGS="-arch x86_64 -arch arm64"`. Wheel-building solutions such as [`cibuildwheel`][cibuildwheel] set this environment variable automatically. |
| 73 | + |
| 74 | +[cibuildwheel]: https://github.com/pypa/cibuildwheel |
0 commit comments