fix(wheels): Repair wheel macos

Script to repair RPATH issues in wheels on macos
This commit is contained in:
2025-12-02 10:04:00 -05:00
parent e0a05bbd1a
commit 8a22496398
3 changed files with 130 additions and 33 deletions

View File

@@ -78,7 +78,7 @@ def fix_rpaths(binary_path):
def main(): def main():
if len(sys.argv) != 2: if len(sys.argv) != 2:
print(f"--- Error: Expected one argument (path to .so file), got {sys.argv}", file=sys.stderr) print(f"--- Error: Expected one argument (path to .dylib/.so file), got {sys.argv}", file=sys.stderr)
sys.exit(1) sys.exit(1)
# Get the file path directly from the command line argument # Get the file path directly from the command line argument

View File

@@ -1,6 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -euo pipefail set -euo pipefail
# 1. Validation
if [[ $(uname -m) != "arm64" ]]; then if [[ $(uname -m) != "arm64" ]]; then
echo "Error: This script is intended to run on an Apple Silicon (arm64) Mac." echo "Error: This script is intended to run on an Apple Silicon (arm64) Mac."
exit 1 exit 1
@@ -11,11 +12,12 @@ if [[ $# -ne 1 ]]; then
exit 1 exit 1
fi fi
# --- Initial Setup --- # 2. Setup Directories
REPO_URL="$1" REPO_URL="$1"
WORK_DIR="$(pwd)" WORK_DIR="$(pwd)"
WHEEL_DIR="${WORK_DIR}/wheels_macos_aarch64_tmp" WHEEL_DIR="${WORK_DIR}/wheels_macos_aarch64_tmp"
FINAL_WHEEL_DIR="${WORK_DIR}/wheels_macos_aarch64" FINAL_WHEEL_DIR="${WORK_DIR}/wheels_macos_aarch64"
RPATH_SCRIPT="${WORK_DIR}/../../build-python/fix_rpaths.py" # Assumes script is in this location relative to execution
echo "➤ Creating wheel output directories" echo "➤ Creating wheel output directories"
mkdir -p "${WHEEL_DIR}" mkdir -p "${WHEEL_DIR}"
@@ -26,10 +28,22 @@ echo "➤ Cloning ${REPO_URL} → ${TMPDIR}/project"
git clone --depth 1 "${REPO_URL}" "${TMPDIR}/project" git clone --depth 1 "${REPO_URL}" "${TMPDIR}/project"
cd "${TMPDIR}/project" cd "${TMPDIR}/project"
# --- macOS Build Configuration --- # 3. Build Configuration
export MACOSX_DEPLOYMENT_TARGET=15.0 export MACOSX_DEPLOYMENT_TARGET=15.0
# Meson options passed to pip via config-settings
# Note: We use an array to keep the command clean
MESON_ARGS=(
"-Csetup-args=-Dunity=off"
"-Csetup-args=-Dbuild-python=true"
"-Csetup-args=-Dbuild-fortran=false"
"-Csetup-args=-Dbuild-tests=false"
"-Csetup-args=-Dpkg-config=false"
"-Csetup-args=-Dunity-safe=true"
)
PYTHON_VERSIONS=("3.8.20" "3.9.23" "3.10.18" "3.11.13" "3.12.11" "3.13.5" "3.13.5t" "3.14.0rc1" "3.14.0rc1t" 'pypy3.10-7.3.19' "pypy3.11-7.3.20") PYTHON_VERSIONS=("3.8.20" "3.9.23" "3.10.18" "3.11.13" "3.12.11" "3.13.5" "3.13.5t" "3.14.0rc1" "3.14.0rc1t" 'pypy3.10-7.3.19' "pypy3.11-7.3.20")
PYTHON_VERSIONS=("3.9.23" "3.10.18" "3.11.13" "3.12.11" "3.13.5" "3.13.5t" "3.14.0rc1" "3.14.0rc1t" 'pypy3.10-7.3.19' "pypy3.11-7.3.20")
if ! command -v pyenv &> /dev/null; then if ! command -v pyenv &> /dev/null; then
echo "Error: pyenv not found. Please install it to manage Python versions." echo "Error: pyenv not found. Please install it to manage Python versions."
@@ -37,55 +51,48 @@ if ! command -v pyenv &> /dev/null; then
fi fi
eval "$(pyenv init -)" eval "$(pyenv init -)"
# 4. Build Loop
for PY_VERSION in "${PYTHON_VERSIONS[@]}"; do for PY_VERSION in "${PYTHON_VERSIONS[@]}"; do
( (
set -e set -e
if ! pyenv versions --bare --filter="${PY_VERSION}." &>/dev/null; then # Check if version exists in pyenv
echo "⚠️ Python version matching '${PY_VERSION}.*' not found by pyenv. Skipping." if ! pyenv versions --bare --filter="${PY_VERSION}" &>/dev/null; then
echo "⚠️ Python version matching '${PY_VERSION}' not found by pyenv. Skipping."
continue continue
fi fi
pyenv shell "${PY_VERSION}" pyenv shell "${PY_VERSION}"
PY="$(pyenv which python)" PY="$(pyenv which python)"
echo "➤ Building for $($PY --version) on macOS arm64 (target: ${MACOSX_DEPLOYMENT_TARGET})"
echo "----------------------------------------------------------------"
echo "➤ Building for $($PY --version) on macOS arm64"
echo "----------------------------------------------------------------"
# Install build deps explicitly so we can skip build isolation
"$PY" -m pip install --upgrade pip setuptools wheel meson meson-python delocate "$PY" -m pip install --upgrade pip setuptools wheel meson meson-python delocate
CC=clang CXX=clang++ "$PY" -m pip wheel . \ # PERF: --no-build-isolation prevents creating a fresh venv and reinstalling meson/ninja
-w "${WHEEL_DIR}" -vv # for every single build, saving significant I/O and network time.
CC="ccache clang" CXX="ccache clang++" "$PY" -m pip wheel . \
echo "➤ Sanitizing RPATHs before delocation..." --no-build-isolation \
"${MESON_ARGS[@]}" \
-w "${WHEEL_DIR}" -vv
# We expect exactly one new wheel in the tmp dir per iteration
CURRENT_WHEEL=$(find "${WHEEL_DIR}" -name "*.whl" | head -n 1) CURRENT_WHEEL=$(find "${WHEEL_DIR}" -name "*.whl" | head -n 1)
if [ -f "$CURRENT_WHEEL" ]; then echo "➤ Repairing wheel with delocate"
"$PY" -m wheel unpack "$CURRENT_WHEEL" -d "${WHEEL_DIR}/unpacked" # Delocate moves the repaired wheel to FINAL_WHEEL_DIR
delocate-wheel -w "${FINAL_WHEEL_DIR}" "$CURRENT_WHEEL"
UNPACKED_ROOT=$(find "${WHEEL_DIR}/unpacked" -mindepth 1 -maxdepth 1 -type d)
find "$UNPACKED_ROOT" -name "*.so" | while read -r SO_FILE; do
echo " Processing: $SO_FILE"
"$PY" "../../build-python/fix_rpaths.py" "$SO_FILE"
done
"$PY" -m wheel pack "$UNPACKED_ROOT" -d "${WHEEL_DIR}"
rm -rf "${WHEEL_DIR}/unpacked"
else
echo "Error: No wheel found to sanitize!"
exit 1
fi
echo "➤ Repairing wheel(s) with delocate"
delocate-wheel -w "${FINAL_WHEEL_DIR}" "${WHEEL_DIR}"/*.whl
rm "${WHEEL_DIR}"/*.whl
# Clean up the intermediate wheel from this iteration so it doesn't confuse the next
rm "$CURRENT_WHEEL"
) )
done done
# Cleanup
rm -rf "${TMPDIR}" rm -rf "${TMPDIR}"
rm -rf "${WHEEL_DIR}" rm -rf "${WHEEL_DIR}"
echo "✅ All builds complete. Artifacts in ${FINAL_WHEEL_DIR}"

View File

@@ -0,0 +1,90 @@
#!/bin/zsh
set -e
# Color codes for output
RED='\033[0;31m'
YELLOW='\033[1;33m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
function fix_file_rpaths() {
local file_path="$1"
echo -e "${YELLOW}Fixing RPATHs in file: $file_path...${NC}"
python3 "$FIX_RPATH_SCRIPT" "$file_path"
if [ $? -ne 0 ]; then
echo -e "${RED}Error: RPATH fix script failed for file: $file_path${NC}"
exit 1
fi
echo -e "${GREEN}RPATHs fixed for file: $file_path${NC}"
}
export -f fix_file_rpaths
echo -e "${YELLOW}"
echo "========================================================================="
echo " TEMPORARY WHEEL REPAIR WORKAROUND"
echo "========================================================================="
echo -e "${NC}"
echo ""
echo -e "${YELLOW}WARNING:${NC} This script applies a temporary patch to fix"
echo "a known issue with meson-python that causes duplicate RPATH entries in"
echo "built Python wheels on macOS, preventing module imports."
echo ""
echo "This workaround will:"
echo " 1. Unzip the wheel file"
echo " 2. Locate the extension modules"
echo " 3. Remove duplicate RPATH entries using install_name_tool"
echo " 4. Resign the wheel if necessary"
echo " 5. Repackage the wheel file"
echo ""
FIX_RPATH_SCRIPT="../../build-python/fix_rpaths.py"
# get the wheel directory to scan through
WHEEL_DIR="$1"
if [ -z "$WHEEL_DIR" ]; then
echo -e "${RED}Error: No wheel directory specified.${NC}"
echo "Usage: $0 /path/to/wheel_directory"
exit 1
fi
REPAIRED_WHEELS_DIR="repaired_wheels"
mkdir -p "$REPAIRED_WHEELS_DIR"
REPAIRED_DELOCATED_WHEELS_DIR="${REPAIRED_WHEELS_DIR}/delocated"
# Scal all files ending in .whl and not starting with a dot
for WHEEL_PATH in "$WHEEL_DIR"/*.whl; do
if [ ! -f "$WHEEL_PATH" ]; then
echo -e "${YELLOW}No wheel files found in directory: $WHEEL_DIR${NC}"
exit 0
fi
echo ""
echo -e "${GREEN}Processing wheel: $WHEEL_PATH${NC}"
WHEEL_NAME=$(basename "$WHEEL_PATH")
TEMP_DIR=$(mktemp -d)
echo -e "${GREEN}Step 1: Unzipping wheel...${NC}"
python -m wheel unpack "$WHEEL_PATH" -d "$TEMP_DIR"
echo -e "${GREEN}Step 2: Locating extension modules...${NC}"
while IFS= read -r -d '' so_file; do
echo "Found library: $so_file"
fix_file_rpaths "$so_file"
done < <(find "$TEMP_DIR" -name "*.so" -print0)
echo -e "${GREEN}Step 4: Repackaging wheel...${NC}"
python -m wheel pack "$TEMP_DIR/gridfire-0.7.4rc2" -d "$REPAIRED_WHEELS_DIR"
REPAIRED_WHEEL_PATH="${REPAIRED_WHEELS_DIR}/${WHEEL_NAME}"
echo -e "${GREEN}Step 5: Delocating wheel...${NC}"
# Ensure delocate is installed
pip install delocate
delocate-wheel -w "$REPAIRED_DELOCATED_WHEELS_DIR" "$REPAIRED_WHEEL_PATH"
echo -e "${GREEN}Repaired wheel saved to: ${REPAIRED_DELOCATED_WHEELS_DIR}/${WHEEL_NAME}${NC}"
# Clean up temporary directory
rm -rf "$TEMP_DIR"
done