|
1 | 1 | #!/bin/bash |
2 | | -# Build a Python wheel for the current platform. |
| 2 | +# Build and test one native Python wheel on the current host. |
3 | 3 | # |
4 | | -# This script compiles the Rust library, generates Python bindings via UniFFI, |
5 | | -# and builds a platform-specific wheel using uv + hatchling. |
6 | | -# |
7 | | -# Run this on each target platform (Linux, macOS) to collect wheels, then use |
8 | | -# scripts/python_publish_package.sh to publish them. |
| 4 | +# Run this script once on each supported native target: |
| 5 | +# Linux x86_64, Linux aarch64, macOS x86_64, macOS arm64. |
| 6 | +# Linux builds require Docker or Podman. macOS builds require Rust 1.95.0. |
9 | 7 |
|
10 | | -set -e |
| 8 | +set -euo pipefail |
11 | 9 |
|
12 | 10 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
13 | 11 | REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 12 | +OUTPUT_DIR="$REPO_ROOT/bindings/python/wheelhouse" |
| 13 | +ALLOW_DIRTY=false |
| 14 | +CIBUILDWHEEL_VERSION="4.1.0" |
| 15 | + |
| 16 | +usage() { |
| 17 | + echo "Usage: $0 [--output-dir DIR] [--allow-dirty]" |
| 18 | +} |
| 19 | + |
| 20 | +while [[ $# -gt 0 ]]; do |
| 21 | + case "$1" in |
| 22 | + --output-dir) |
| 23 | + [[ $# -ge 2 ]] || { usage >&2; exit 2; } |
| 24 | + OUTPUT_DIR="$2" |
| 25 | + shift 2 |
| 26 | + ;; |
| 27 | + --allow-dirty) |
| 28 | + ALLOW_DIRTY=true |
| 29 | + shift |
| 30 | + ;; |
| 31 | + -h|--help) |
| 32 | + usage |
| 33 | + exit 0 |
| 34 | + ;; |
| 35 | + *) |
| 36 | + echo "Unknown argument: $1" >&2 |
| 37 | + usage >&2 |
| 38 | + exit 2 |
| 39 | + ;; |
| 40 | + esac |
| 41 | +done |
| 42 | + |
| 43 | +case "$OUTPUT_DIR" in |
| 44 | + /*) ;; |
| 45 | + *) OUTPUT_DIR="$REPO_ROOT/$OUTPUT_DIR" ;; |
| 46 | +esac |
| 47 | + |
| 48 | +for command_name in git uv; do |
| 49 | + if ! command -v "$command_name" >/dev/null 2>&1; then |
| 50 | + echo "Required command not found: $command_name" >&2 |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | +done |
14 | 54 |
|
15 | 55 | cd "$REPO_ROOT" |
16 | 56 |
|
17 | | -# Generate bindings and compile the native library |
18 | | -echo "Generating Python bindings..." |
19 | | -./scripts/uniffi_bindgen_generate_python.sh |
20 | | - |
21 | | -# Build the wheel |
22 | | -echo "Building wheel..." |
23 | | -cd bindings/python |
24 | | -uv build --wheel |
25 | | - |
26 | | -echo "" |
27 | | -echo "Wheel built successfully:" |
28 | | -ls -1 dist/*.whl |
29 | | -echo "" |
30 | | -echo "Collect wheels from all target platforms into dist/, then run:" |
31 | | -echo " ./scripts/python_publish_package.sh" |
| 57 | +if [[ "$ALLOW_DIRTY" == false ]] && [[ -n "$(git status --porcelain --untracked-files=normal)" ]]; then |
| 58 | + echo "Refusing to build from a dirty worktree; commit the release first." >&2 |
| 59 | + echo "Use --allow-dirty only while developing the build configuration." >&2 |
| 60 | + exit 1 |
| 61 | +fi |
| 62 | + |
| 63 | +case "$(uname -s)" in |
| 64 | + Linux) |
| 65 | + case "${CIBW_CONTAINER_ENGINE:-}" in |
| 66 | + podman*) CONTAINER_COMMAND=podman ;; |
| 67 | + docker*|"") CONTAINER_COMMAND=docker ;; |
| 68 | + *) |
| 69 | + echo "Unsupported CIBW_CONTAINER_ENGINE: $CIBW_CONTAINER_ENGINE" >&2 |
| 70 | + exit 1 |
| 71 | + ;; |
| 72 | + esac |
| 73 | + |
| 74 | + if ! command -v "$CONTAINER_COMMAND" >/dev/null 2>&1; then |
| 75 | + if [[ -z "${CIBW_CONTAINER_ENGINE:-}" ]] && command -v podman >/dev/null 2>&1; then |
| 76 | + export CIBW_CONTAINER_ENGINE=podman |
| 77 | + else |
| 78 | + echo "Linux wheel builds require Docker or Podman." >&2 |
| 79 | + exit 1 |
| 80 | + fi |
| 81 | + fi |
| 82 | + ;; |
| 83 | + Darwin) |
| 84 | + if ! command -v rustup >/dev/null 2>&1 || \ |
| 85 | + ! rustup run 1.95.0 rustc --version >/dev/null 2>&1; then |
| 86 | + echo "macOS wheel builds require the Rust 1.95.0 toolchain." >&2 |
| 87 | + echo "Install it with: rustup toolchain install 1.95.0 --profile minimal" >&2 |
| 88 | + exit 1 |
| 89 | + fi |
| 90 | + ;; |
| 91 | + *) |
| 92 | + echo "Unsupported operating system: $(uname -s)" >&2 |
| 93 | + exit 1 |
| 94 | + ;; |
| 95 | +esac |
| 96 | + |
| 97 | +case "$(uname -m)" in |
| 98 | + x86_64|amd64|aarch64|arm64) ;; |
| 99 | + *) |
| 100 | + echo "Unsupported architecture: $(uname -m)" >&2 |
| 101 | + exit 1 |
| 102 | + ;; |
| 103 | +esac |
| 104 | + |
| 105 | +mkdir -p "$OUTPUT_DIR" |
| 106 | +shopt -s nullglob |
| 107 | +existing_wheels=("$OUTPUT_DIR"/*.whl) |
| 108 | +if [[ ${#existing_wheels[@]} -ne 0 ]]; then |
| 109 | + echo "Output directory already contains wheels: $OUTPUT_DIR" >&2 |
| 110 | + exit 1 |
| 111 | +fi |
| 112 | + |
| 113 | +CARGO_TARGET_DIR="$(mktemp -d /tmp/cargo-target-ldk-node-python-wheels.XXXXXX)" |
| 114 | +export CARGO_TARGET_DIR |
| 115 | +SOURCE_DATE_EPOCH="$(git show -s --format=%ct HEAD)" |
| 116 | +export SOURCE_DATE_EPOCH |
| 117 | + |
| 118 | +cleanup() { |
| 119 | + case "$CARGO_TARGET_DIR" in |
| 120 | + /tmp/cargo-target-ldk-node-python-wheels.*) |
| 121 | + rm -rf -- "$CARGO_TARGET_DIR" |
| 122 | + ;; |
| 123 | + esac |
| 124 | +} |
| 125 | +trap cleanup EXIT |
| 126 | + |
| 127 | +echo "Building Python wheel from commit $(git rev-parse HEAD)" |
| 128 | +echo "Target: $(uname -s) $(uname -m)" |
| 129 | + |
| 130 | +uv tool run --from "cibuildwheel[uv]==$CIBUILDWHEEL_VERSION" cibuildwheel \ |
| 131 | + bindings/python \ |
| 132 | + --config-file bindings/python/pyproject.toml \ |
| 133 | + --output-dir "$OUTPUT_DIR" |
| 134 | + |
| 135 | +built_wheels=("$OUTPUT_DIR"/*.whl) |
| 136 | +if [[ ${#built_wheels[@]} -ne 1 ]]; then |
| 137 | + echo "Expected exactly one wheel, found ${#built_wheels[@]}." >&2 |
| 138 | + exit 1 |
| 139 | +fi |
| 140 | + |
| 141 | +wheel_path="${built_wheels[0]}" |
| 142 | +wheel_name="$(basename "$wheel_path")" |
| 143 | +( |
| 144 | + cd "$OUTPUT_DIR" |
| 145 | + if command -v sha256sum >/dev/null 2>&1; then |
| 146 | + sha256sum "$wheel_name" |
| 147 | + else |
| 148 | + shasum -a 256 "$wheel_name" |
| 149 | + fi |
| 150 | +) > "$wheel_path.sha256" |
| 151 | + |
| 152 | +echo "Wheel built and tested successfully:" |
| 153 | +echo " $wheel_path" |
| 154 | +echo " $wheel_path.sha256" |
0 commit comments