-
Notifications
You must be signed in to change notification settings - Fork 25
333 lines (311 loc) · 12.1 KB
/
Copy pathpython-wheels.yml
File metadata and controls
333 lines (311 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# This file is loosely based on the one generated by maturin v1.9.2 using
#
# maturin generate-ci github
#
# The script has been heavily modified from there.
# In particular, we run `maturin build` manually instead of using the `maturin-action`,
# so we can setup the workspace dependencies (rust, llvm, conan, tket-c-api) correctly.
#
name: Python wheels 🐍
on:
push:
branches:
- main
tags:
- "tket-py-v*"
workflow_dispatch:
permissions:
contents: read
env:
MODULE_DIR: tket-py
# Pinned version for the uv package manager
UV_VERSION: "0.11.15"
UV_FROZEN: 1
# Python version used to build the wheels
# We use abi3-compatible building, so this still supports newer Python versions
PYTHON_VERSION: "3.10"
# sccache config
SCCACHE_GHA_ENABLED: "true"
RUSTC_WRAPPER: "sccache"
jobs:
# Check if the tag matches the package name,
# or if the workflow is running on a non-tag event.
check-tag:
name: Check tag
runs-on: ubuntu-latest
outputs:
run: ${{ steps.check-tag.outputs.run }}
steps:
- name: Check tag
id: check-tag
run: |
echo "run=$SHOULD_RUN" >> $GITHUB_OUTPUT
env:
SHOULD_RUN: ${{ github.ref_type != 'tag' || startsWith(github.ref, 'refs/tags/tket-py-v') }}
build:
name: ${{ matrix.platform.target }}
needs: check-tag
if: ${{ needs.check-tag.outputs.run == 'true' }}
runs-on: ${{ matrix.platform.runner }}
strategy:
fail-fast: false
matrix:
# List of platforms to build the wheels for.
#
# Each entry defines:
# - runner: The github runner to use for the build.
# - target: The target platform used when building the rust code.
# - manylinux: (optional) If doing manylinux-compatible wheels,
# the manylinux version to use. Setting this option causes the wheels
# to be built with zig.
# - test-wheels: Whether to test installing the wheels.
# This is only done when the target platform matches the runner platform.
# - macos-compatibility: the minimum supported macosx version to build macos
# wheels for. Violations (e.g. library dependencies on newer system libraries)
# will result in CI failure.
#
# Note that platform support is limited by the pytket wheels' availability.
# Since we have it as a dependency, wheels built for other platforms will be unusable.
platform:
# ---------------- Linux
- runner: ubuntu-24.04-arm
target: aarch64-unknown-linux-gnu
manylinux: manylinux_2_28
test-wheels: true
- runner: ubuntu-24.04
target: x86_64-unknown-linux-gnu
manylinux: manylinux_2_28
test-wheels: true
# ---------------- Mac
- runner: macos-14
target: aarch64-apple-darwin
macos-compatibility: "11.0"
test-wheels: true
- runner: macos-15-intel
target: x86_64-apple-darwin
macos-compatibility: "11.0"
test-wheels: true
# ---------------- Windows
- runner: windows-2025
target: x86_64-pc-windows-msvc
test-wheels: true
steps:
- uses: actions/checkout@v7
- uses: mozilla-actions/sccache-action@v0.0.10
- name: Install stable toolchain
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.platform.target }}
- name: Set up uv
uses: astral-sh/setup-uv@v7
with:
version: ${{ env.UV_VERSION }}
enable-cache: true
- uses: quantinuum/hugrverse-env/install-hugrenv-action@main
- uses: mlugg/setup-zig@v2
if: ${{ matrix.platform.manylinux }}
- name: Set MACOSX_DEPLOYMENT_TARGET on macos
if: matrix.platform.macos-compatibility
run: echo "MACOSX_DEPLOYMENT_TARGET=${{ matrix.platform.macos-compatibility }}" >> $GITHUB_ENV
- name: Set compatibility flag
id: compatibility-flag
shell: bash
run: |
if [ -n "${{ matrix.platform.manylinux }}" ]; then
WITH_ZIG_FLAG="--with ziglang --with maturin[zig]"
COMPATIBILITY_FLAG="--compatibility ${{ matrix.platform.manylinux }} --zig"
else
WITH_ZIG_FLAG=""
COMPATIBILITY_FLAG=""
fi
echo "COMPATIBILITY_FLAG: $COMPATIBILITY_FLAG"
echo "WITH_ZIG_FLAG: $WITH_ZIG_FLAG"
echo "COMPATIBILITY_FLAG=$COMPATIBILITY_FLAG" >> "$GITHUB_OUTPUT"
echo "WITH_ZIG_FLAG=$WITH_ZIG_FLAG" >> "$GITHUB_OUTPUT"
- name: Copy DLL to package directory (Windows)
if: runner.os == 'Windows'
shell: bash
run: |
# Copy the DLL to the package directory so maturin can include it in the wheel
if [ -f "$TKET_C_API_PATH/bin/tket-c-api.dll" ]; then
cp "$TKET_C_API_PATH/bin/tket-c-api.dll" "$MODULE_DIR/tket/tket-c-api.dll"
echo "Copied DLL to $MODULE_DIR/tket/tket-c-api.dll"
ls -lh "$MODULE_DIR/tket/tket-c-api.dll"
else
echo "ERROR: DLL not found at $TKET_C_API_PATH/bin/tket-c-api.dll"
exit 1
fi
- name: Build wheels
shell: bash
run: |
cd ${{ env.MODULE_DIR }}
uv run --python ${{ env.PYTHON_VERSION }} \
${{ steps.compatibility-flag.outputs.WITH_ZIG_FLAG }} \
maturin build ${{ steps.compatibility-flag.outputs.COMPATIBILITY_FLAG }} \
--release \
--out ../dist \
--target ${{ matrix.platform.target }} \
--strip
- name: Build free-threaded wheels
# TODO: Waiting for hugr 0.16.1 / 0.17.0 to release python 3.14 free-threaded wheels.
# <https://github.com/Quantinuum/hugr/pull/3021>
if: false
shell: bash
run: |
cd ${{ env.MODULE_DIR }}
uv run --python ${{ env.PYTHON_VERSION }}+freethreaded \
${{ steps.compatibility-flag.outputs.WITH_ZIG_FLAG }} \
maturin build \
${{ steps.compatibility-flag.outputs.COMPATIBILITY_FLAG }} \
--release \
--out ../dist \
--target ${{ matrix.platform.target }} \
--strip
- name: Repair macOS wheels with delocate
if: runner.os == 'macOS'
shell: bash
run: |
echo "Repairing macOS wheels with delocate..."
export DYLD_LIBRARY_PATH="$HUGRENV_PATH/lib:$HUGRENV_PATH/lib64:$DYLD_LIBRARY_PATH"
ls -lh dist || true
MAC_WHEELS=$(ls dist/*macosx*.whl 2>/dev/null || true)
if [ -z "$MAC_WHEELS" ]; then
echo "No macOS wheels found to repair; skipping delocate."
exit 0
fi
uvx --from delocate delocate-listdeps $MAC_WHEELS || true
uvx --from delocate delocate-wheel -v -w dist/repaired $MAC_WHEELS
rm -v -f $MAC_WHEELS
mv -v dist/repaired/*.whl dist/
rmdir -v dist/repaired || true
echo "Repaired wheels:"
ls -lh dist/*macosx*.whl
- name: Upload wheels
uses: actions/upload-artifact@v7
with:
name: wheels-${{ matrix.platform.target }}
path: dist
if-no-files-found: error
- name: Test installing the built wheels
shell: bash
run: |
echo "Testing the newly built ${{ matrix.platform.target }} wheels..."
if [ "$TEST_INSTALL" = "true" ]; then
# Remove original libraries from the build environment to ensure that
# the test installation is actually using the built wheels, and not picking up
# the libraries from the hugrverse environment.
rm -rf $HUGRENV_PATH;
unset HUGRENV_PATH;
unset TKET_C_API_PATH;
unset LLVM_SYS_211_PREFIX;
# Import tket from within a fresh environment/directory rather than the source
# directory to avoid source conflicts (the repaired wheel has modified library paths)
tmpdir=$(mktemp -d);
cd $tmpdir;
# To avoid interference from the existing environment, use a python venv.
# Note that this approach is chosen because some unwanted behaviour was seen
# on some platforms when using uv directly.
uv run python -m venv venv
if [ -d "venv/Scripts" ]; then
venv_python="venv/Scripts/python"
venv_pip="venv/Scripts/pip"
else
venv_python="venv/bin/python"
venv_pip="venv/bin/pip"
fi
# First install the pure python packages from this commit; this avoids fetching them
# from pypi and providing false positives or false negatives in the presence of a conflict.
${venv_pip} install ${GITHUB_WORKSPACE}/tket-eccs
${venv_pip} install ${GITHUB_WORKSPACE}/tket-exts
# Install the built wheel
wheel_path=$(ls "$GITHUB_WORKSPACE/dist/"*.whl | head -n 1)
${venv_pip} install ${wheel_path}
# Try importing tket to verify that the installation works.
# Although not a comprehensive test (TODO), this at least verifies
# that the dynamic libraries are correctly linked and can be loaded.
${venv_python} -c "import tket; print(tket.__file__);"
# Now return to the original workspace for further steps.
cd $GITHUB_WORKSPACE;
fi
uvx twine check --strict dist/*
env:
WORKSPACE: ${{ github.workspace }}
TEST_INSTALL: ${{ matrix.platform.test-wheels }}
sdist:
needs: check-tag
if: ${{ needs.check-tag.outputs.run == 'true' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
- uses: mozilla-actions/sccache-action@v0.0.10
- name: Set up uv
uses: astral-sh/setup-uv@v7
with:
version: ${{ env.UV_VERSION }}
enable-cache: true
- name: Build sdist
run: |
cd ${{ env.MODULE_DIR }}
uv run --no-project \
--python ${{ env.PYTHON_VERSION }} \
--with maturin \
maturin sdist \
--out ../dist \
--manifest-path Cargo.toml
- name: Upload sdist
uses: actions/upload-artifact@v7
with:
name: wheels-sdist
path: dist
if-no-files-found: error
release:
name: Release
runs-on: ubuntu-latest
if: ${{ github.ref_type == 'tag' && startsWith(github.ref, 'refs/tags/tket-py-v') }}
needs: [build, sdist]
environment: release
permissions:
# Use to sign the release artifacts
id-token: write
# Used to upload release artifacts
contents: write
# Used to generate artifact attestation
attestations: write
steps:
- uses: actions/checkout@v7
- name: Check if the tag has an associated release
env:
GH_TOKEN: ${{ github.token }}
run: |
if ! gh release view ${{ github.ref_name }}; then
echo "No release found for tag ${{ github.ref_name }}"
exit 1
fi
- uses: actions/download-artifact@v8
with:
path: dist
merge-multiple: true
- name: Generate artifact attestation
uses: actions/attest@v4
with:
subject-path: 'dist/*'
- name: Report
run: |
echo "Publishing to PyPI..."
echo "Based on the following workflow variables, this is a new version tag push:"
echo " - event_name: ${{ github.event_name }}"
echo " - ref_type: ${{ github.ref_type }}"
echo " - ref: ${{ github.ref }}"
- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
verbose: true
skip-existing: true
- name: Upload wheels to the release and un-draft it
env:
GH_TOKEN: ${{ github.token }}
run: |
# Upload wheels to the release assets
gh release upload ${{ github.ref_name }} dist/*
# Un-draft the release on GitHub now that the package has been published to PyPI
gh release edit ${{ github.ref_name }} --draft=false