|
1 | 1 | #!/bin/bash |
2 | | -# Build a Python wheel for the current platform. |
| 2 | +# Build and test native Python wheels 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 build host: |
| 5 | +# Linux x86_64, Linux aarch64, macOS arm64. |
| 6 | +# Each Linux host builds its native wheel. The macOS arm64 host builds both |
| 7 | +# arm64 and x86_64 wheels and requires Rust 1.95.0 and Rosetta 2. |
9 | 8 |
|
10 | | -set -e |
| 9 | +set -euo pipefail |
11 | 10 |
|
12 | 11 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
13 | 12 | REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 13 | +OUTPUT_DIR="$REPO_ROOT/bindings/python/wheelhouse" |
| 14 | +ALLOW_DIRTY=false |
| 15 | +CIBUILDWHEEL_VERSION="4.1.0" |
| 16 | + |
| 17 | +usage() { |
| 18 | + echo "Usage: $0 [--output-dir DIR] [--allow-dirty]" |
| 19 | +} |
| 20 | + |
| 21 | +while [[ $# -gt 0 ]]; do |
| 22 | + case "$1" in |
| 23 | + --output-dir) |
| 24 | + [[ $# -ge 2 ]] || { usage >&2; exit 2; } |
| 25 | + OUTPUT_DIR="$2" |
| 26 | + shift 2 |
| 27 | + ;; |
| 28 | + --allow-dirty) |
| 29 | + ALLOW_DIRTY=true |
| 30 | + shift |
| 31 | + ;; |
| 32 | + -h|--help) |
| 33 | + usage |
| 34 | + exit 0 |
| 35 | + ;; |
| 36 | + *) |
| 37 | + echo "Unknown argument: $1" >&2 |
| 38 | + usage >&2 |
| 39 | + exit 2 |
| 40 | + ;; |
| 41 | + esac |
| 42 | +done |
| 43 | + |
| 44 | +case "$OUTPUT_DIR" in |
| 45 | + /*) ;; |
| 46 | + *) OUTPUT_DIR="$REPO_ROOT/$OUTPUT_DIR" ;; |
| 47 | +esac |
| 48 | + |
| 49 | +for command_name in git uv; do |
| 50 | + if ! command -v "$command_name" >/dev/null 2>&1; then |
| 51 | + echo "Required command not found: $command_name" >&2 |
| 52 | + exit 1 |
| 53 | + fi |
| 54 | +done |
14 | 55 |
|
15 | 56 | cd "$REPO_ROOT" |
16 | 57 |
|
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" |
| 58 | +HOST_OS="$(uname -s)" |
| 59 | +HOST_ARCH="$(uname -m)" |
| 60 | + |
| 61 | +case "$HOST_OS:$HOST_ARCH" in |
| 62 | + Linux:x86_64|Linux:amd64|Linux:aarch64|Linux:arm64|Darwin:aarch64|Darwin:arm64) ;; |
| 63 | + *) |
| 64 | + echo "Unsupported build host: $HOST_OS $HOST_ARCH" >&2 |
| 65 | + exit 1 |
| 66 | + ;; |
| 67 | +esac |
| 68 | + |
| 69 | +if [[ "$ALLOW_DIRTY" == false ]] && [[ -n "$(git status --porcelain --untracked-files=normal)" ]]; then |
| 70 | + echo "Refusing to build from a dirty worktree; commit the release first." >&2 |
| 71 | + echo "Use --allow-dirty only while developing the build configuration." >&2 |
| 72 | + exit 1 |
| 73 | +fi |
| 74 | + |
| 75 | +case "$HOST_OS" in |
| 76 | + Linux) |
| 77 | + EXPECTED_WHEEL_COUNT=1 |
| 78 | + case "${CIBW_CONTAINER_ENGINE:-}" in |
| 79 | + podman*) CONTAINER_COMMAND=podman ;; |
| 80 | + docker*|"") CONTAINER_COMMAND=docker ;; |
| 81 | + *) |
| 82 | + echo "Unsupported CIBW_CONTAINER_ENGINE: $CIBW_CONTAINER_ENGINE" >&2 |
| 83 | + exit 1 |
| 84 | + ;; |
| 85 | + esac |
| 86 | + |
| 87 | + if ! command -v "$CONTAINER_COMMAND" >/dev/null 2>&1; then |
| 88 | + if [[ -z "${CIBW_CONTAINER_ENGINE:-}" ]] && command -v podman >/dev/null 2>&1; then |
| 89 | + export CIBW_CONTAINER_ENGINE=podman |
| 90 | + else |
| 91 | + echo "Linux wheel builds require Docker or Podman." >&2 |
| 92 | + exit 1 |
| 93 | + fi |
| 94 | + fi |
| 95 | + ;; |
| 96 | + Darwin) |
| 97 | + EXPECTED_WHEEL_COUNT=2 |
| 98 | + if ! command -v rustup >/dev/null 2>&1 || \ |
| 99 | + ! rustup run 1.95.0 rustc --version >/dev/null 2>&1; then |
| 100 | + echo "macOS wheel builds require the Rust 1.95.0 toolchain." >&2 |
| 101 | + echo "Install it with: rustup toolchain install 1.95.0 --profile minimal" >&2 |
| 102 | + exit 1 |
| 103 | + fi |
| 104 | + if ! /usr/bin/arch -x86_64 /usr/bin/true >/dev/null 2>&1; then |
| 105 | + echo "macOS x86_64 wheel tests require Rosetta 2." >&2 |
| 106 | + echo "Install it with: softwareupdate --install-rosetta" >&2 |
| 107 | + exit 1 |
| 108 | + fi |
| 109 | + ;; |
| 110 | + *) |
| 111 | + echo "Unsupported operating system: $HOST_OS" >&2 |
| 112 | + exit 1 |
| 113 | + ;; |
| 114 | +esac |
| 115 | + |
| 116 | +mkdir -p "$OUTPUT_DIR" |
| 117 | +shopt -s nullglob |
| 118 | +existing_wheels=("$OUTPUT_DIR"/*.whl) |
| 119 | +if [[ ${#existing_wheels[@]} -ne 0 ]]; then |
| 120 | + echo "Output directory already contains wheels: $OUTPUT_DIR" >&2 |
| 121 | + exit 1 |
| 122 | +fi |
| 123 | + |
| 124 | +CARGO_TARGET_DIR="$(mktemp -d /tmp/cargo-target-ldk-node-python-wheels.XXXXXX)" |
| 125 | +export CARGO_TARGET_DIR |
| 126 | +BUILD_SOURCE_DIR="$(mktemp -d /tmp/ldk-node-python-wheels-source.XXXXXX)" |
| 127 | +SOURCE_DATE_EPOCH="$(git show -s --format=%ct HEAD)" |
| 128 | +export SOURCE_DATE_EPOCH |
| 129 | + |
| 130 | +cleanup() { |
| 131 | + case "$CARGO_TARGET_DIR" in |
| 132 | + /tmp/cargo-target-ldk-node-python-wheels.*) |
| 133 | + rm -rf -- "$CARGO_TARGET_DIR" |
| 134 | + ;; |
| 135 | + esac |
| 136 | + case "$BUILD_SOURCE_DIR" in |
| 137 | + /tmp/ldk-node-python-wheels-source.*) |
| 138 | + rm -rf -- "$BUILD_SOURCE_DIR" |
| 139 | + ;; |
| 140 | + esac |
| 141 | +} |
| 142 | +trap cleanup EXIT |
| 143 | + |
| 144 | +git ls-files --cached --others --exclude-standard -z | while IFS= read -r -d '' source_path; do |
| 145 | + if [[ -e "$source_path" || -L "$source_path" ]]; then |
| 146 | + printf '%s\0' "$source_path" |
| 147 | + fi |
| 148 | +done | tar --null --files-from=- -cf - | tar -xf - -C "$BUILD_SOURCE_DIR" |
| 149 | + |
| 150 | +echo "Building Python wheel from commit $(git rev-parse HEAD)" |
| 151 | +echo "Build host: $HOST_OS $HOST_ARCH" |
| 152 | + |
| 153 | +( |
| 154 | + cd "$BUILD_SOURCE_DIR" |
| 155 | + uv tool run --python 3.11 --from "cibuildwheel[uv]==$CIBUILDWHEEL_VERSION" cibuildwheel \ |
| 156 | + bindings/python \ |
| 157 | + --config-file bindings/python/pyproject.toml \ |
| 158 | + --output-dir "$OUTPUT_DIR" |
| 159 | +) |
| 160 | + |
| 161 | +built_wheels=("$OUTPUT_DIR"/*.whl) |
| 162 | +if [[ ${#built_wheels[@]} -ne $EXPECTED_WHEEL_COUNT ]]; then |
| 163 | + echo "Expected $EXPECTED_WHEEL_COUNT wheels, found ${#built_wheels[@]}." >&2 |
| 164 | + exit 1 |
| 165 | +fi |
| 166 | + |
| 167 | +for wheel_path in "${built_wheels[@]}"; do |
| 168 | + wheel_name="$(basename "$wheel_path")" |
| 169 | + ( |
| 170 | + cd "$OUTPUT_DIR" |
| 171 | + if command -v sha256sum >/dev/null 2>&1; then |
| 172 | + sha256sum "$wheel_name" |
| 173 | + else |
| 174 | + shasum -a 256 "$wheel_name" |
| 175 | + fi |
| 176 | + ) > "$wheel_path.sha256" |
| 177 | +done |
| 178 | + |
| 179 | +echo "Wheels built and tested successfully:" |
| 180 | +for wheel_path in "${built_wheels[@]}"; do |
| 181 | + echo " $wheel_path" |
| 182 | + echo " $wheel_path.sha256" |
| 183 | +done |
0 commit comments