A fast, helpful, and open-source document parser
fix: for musl, use a raw docker-based step as we do for node instead of the maturin action
Clelia (Astra) Bertelli6f24f3ba2409
diff --git a/.github/workflows/ci-python.yml b/.github/workflows/ci-python.ymlindex 9d2dee0..9877023 100644--- a/.github/workflows/ci-python.yml+++ b/.github/workflows/ci-python.yml@@ -95,60 +95,28 @@ jobs:apk add tesseract-ocr-dev leptonica-dev openssl-dev pkgconf || truefi- - name: Build wheels (musl)+ - name: Build wheels (musl container)if: ${{ matrix.musl }}- uses: PyO3/maturin-action@v1- env:- # The musllinux_1_2 cross image (messense/rust-musl-cross) ships a- # musl-cross GCC whose default specs define _FORTIFY_SOURCE=2. That- # makes the C++ build of tesseract (and leptonica) emit calls to- # glibc fortify wrappers (__printf_chk, __memcpy_chk, __fprintf_chk,- # __vsnprintf_chk, ...) — symbols that DO NOT exist in musl libc, so- # linking fails with "undefined reference to __printf_chk".- #- # We point CC/CXX at wrapper scripts (created in before-script-linux)- # that append -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0 to every compile,- # AFTER tesseract-rs's hard-coded -DCMAKE_C_FLAGS / -DCMAKE_CXX_FLAGS- # (which clobber any CFLAGS/CXXFLAGS env var, so flag-based overrides- # don't reach the compiler).- #- # CC_x86_64_unknown_linux_musl / CXX_x86_64_unknown_linux_musl are- # the per-target env vars that both cc-rs and cmake-rs honor.- CC: /tmp/ccwrap/cc- CXX: /tmp/ccwrap/cxx- CC_x86_64_unknown_linux_musl: /tmp/ccwrap/cc- CXX_x86_64_unknown_linux_musl: /tmp/ccwrap/cxx- with:- target: ${{ matrix.target }}- args: --release --out dist --find-interpreter- manylinux: musllinux_1_2- working-directory: packages/python- before-script-linux: |- # See env block above for context.- REAL_CC=$(command -v x86_64-unknown-linux-musl-gcc || command -v musl-gcc || command -v gcc)- REAL_CXX=$(command -v x86_64-unknown-linux-musl-g++ || command -v musl-g++ || command -v g++)- echo "Wrapping CC=$REAL_CC CXX=$REAL_CXX with FORTIFY-disable shim"- mkdir -p /tmp/ccwrap- cat > /tmp/ccwrap/cc <<EOF- #!/bin/sh- exec "$REAL_CC" "\$@" -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0- EOF- cat > /tmp/ccwrap/cxx <<EOF- #!/bin/sh- exec "$REAL_CXX" "\$@" -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=0- EOF- chmod +x /tmp/ccwrap/cc /tmp/ccwrap/cxx-- if command -v apk &> /dev/null; then- apk add --no-cache tesseract-ocr-dev leptonica-dev openssl-dev pkgconf build-base cmake perl- elif command -v yum &> /dev/null; then- yum install -y tesseract-devel leptonica-devel openssl-devel pkgconfig perl-IPC-Cmd || true- elif command -v apt-get &> /dev/null; then- apt-get update && apt-get install -y libtesseract-dev libleptonica-dev libssl-dev pkg-config || true- fi+ # Same pattern as the node musl build: run inside a vanilla+ # python:3-alpine with clang+libc++ from apk. Bypasses maturin-action's+ # musllinux_1_2 cross image, which causes FORTIFY_SOURCE issues with+ # tesseract's C++ build and broken host-openssl for build-deps.+ run: |+ chmod +x scripts/build-musl-py.sh+ docker run --rm \+ -v "$GITHUB_WORKSPACE":/work \+ -v "$HOME/.cargo/registry":/root/.cargo/registry \+ -v "$HOME/.cargo/git":/root/.cargo/git \+ -v "$HOME/.cache/pdfium-rs":/root/.cache/pdfium-rs \+ -w /work \+ python:3.12-alpine \+ /work/scripts/build-musl-py.sh+ # The container runs as root; fix perms so subsequent steps can+ # read/upload the produced wheel.+ sudo chmod -R a+rw packages/python/dist/ || true- name: Copy pdfium dylib into wheel (Unix)- if: runner.os != 'Windows'+ if: runner.os != 'Windows' && !matrix.muslworking-directory: packages/pythonrun: |# Fix permissions from Docker/maturin builddiff --git a/scripts/build-musl-py.sh b/scripts/build-musl-py.shnew file mode 100644index 0000000..4a7eb52--- /dev/null+++ b/scripts/build-musl-py.sh@@ -0,0 +1,122 @@+#!/bin/sh+set -eux++# Mirrors scripts/build-musl-node.sh but for the Python wheel.+#+# Why a hand-rolled Alpine container instead of maturin-action's+# musllinux_1_2 image:+# - musllinux_1_2 is Alpine-based but ships a musl-cross GCC whose default+# specs define _FORTIFY_SOURCE=2. tesseract's C++ then references glibc+# fortify wrappers (__printf_chk, __fprintf_chk, ...) that don't exist in+# musl libc, so linking fails.+# - tesseract-rs hard-codes clang++ + libc++ for its CMake build, so we want+# the real Alpine clang/libc++ toolchain, not a musl-cross-gcc.+# - tesseract-rs's build-deps (reqwest → native-tls → openssl-sys) get+# compiled for the host. In the musllinux_1_2 cross container the host+# openssl install is half-broken; native Alpine clang against system+# openssl-dev just works.+#+# So we replicate the node build container pattern: a vanilla python:3-alpine,+# install clang/libc++/tesseract dev libs from apk, then run maturin against+# a self-contained venv.++apk add --no-cache \+ build-base cmake git curl pkgconf perl \+ clang libc++-dev llvm-libunwind-dev \+ tesseract-ocr-dev leptonica-dev \+ openssl-dev openssl-libs-static zlib-static \+ patchelf++curl --proto "=https" --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain stable -t x86_64-unknown-linux-musl+. /root/.cargo/env++python3 -m venv /tmp/venv+. /tmp/venv/bin/activate+pip install --upgrade pip maturin++# cdylib MUST be dynamically linked against libc.+export RUSTFLAGS="-C target-feature=-crt-static"++cd packages/python+rm -rf dist++# pyproject.toml's [tool.maturin] include rule expects liteparse/libpdfium.so+# to exist at build time so it gets packed into the wheel. pdfium-sys's build+# script downloads pdfium into ~/.cache/pdfium-rs; we need to compile once+# (which triggers the download) and then copy the .so into place before the+# real maturin build picks it up via include. Easiest: just run+# scripts/copy-pdfium.sh after a probe build, OR run copy-pdfium.sh directly+# pointing at the cache. The script auto-detects the cache path.+#+# Order: trigger the pdfium download via a cargo metadata-only compile of+# pdfium-sys (cheap), then copy, then real maturin build.+( cd ../../crates/pdfium-sys && cargo build --release --target x86_64-unknown-linux-musl )+bash scripts/copy-pdfium.sh++# --find-interpreter would try to build for every python on PATH; we only+# have the container's python (3.12 for python:3.12-alpine, etc.). Letting+# maturin auto-detect produces a single wheel for that interpreter, which is+# what we want for the musl matrix entry.+maturin build --release --out dist --target x86_64-unknown-linux-musl++WHEEL=$(ls dist/*.whl | head -n1)+echo "Built wheel: $WHEEL"++# The extension module lives at liteparse/_liteparse*.so inside the wheel.+# Inspect its DT_NEEDED entries (clang + libc++ runtime) and bundle anything+# non-system next to it, same logic as build-musl-node.sh. We use a scratch+# dir to unpack/repack the wheel.+WORK=$(mktemp -d)+unzip -q "$WHEEL" -d "$WORK"++EXT=$(find "$WORK/liteparse" -maxdepth 1 -name '_liteparse*.so' | head -n1)+[ -n "$EXT" ] || { echo "ERROR: could not find _liteparse*.so in wheel"; exit 1; }+echo "Extension module: $EXT"+echo "DT_NEEDED for extension:"+ldd "$EXT" || true++DEST=$(dirname "$EXT")+for needed in $(ldd "$EXT" 2>/dev/null | awk '{print $1}' | grep -E '^lib'); do+ case "$needed" in+ libc.musl-*.so.* | ld-musl-*.so.* | libc.so | libdl.so* | libm.so* | libpthread.so* | librt.so*)+ continue+ ;;+ # libpdfium is already bundled by maturin via pyproject.toml include rules.+ libpdfium.so)+ continue+ ;;+ esac+ if [ -f "$DEST/$needed" ]; then+ echo "Already bundled: $needed"+ continue+ fi+ src=$(find /usr/lib /usr/lib64 -maxdepth 3 -name "$needed" 2>/dev/null | head -n1)+ if [ -z "$src" ]; then+ echo "WARN: could not locate $needed under /usr/lib, skipping"+ continue+ fi+ src=$(readlink -f "$src")+ [ -f "$src" ] || { echo "ERROR: $src does not resolve to a file"; exit 1; }+ cp -v "$src" "$DEST/$needed"+done++echo "Bundled libs next to extension:"+ls -la "$DEST"++# Repack the wheel. Use python's zipfile to preserve wheel layout / metadata.+python3 - <<PY+import os, zipfile, sys+src = "$WORK"+out = "$WHEEL"+os.remove(out)+with zipfile.ZipFile(out, "w", zipfile.ZIP_DEFLATED) as z:+ for root, _, files in os.walk(src):+ for f in files:+ full = os.path.join(root, f)+ rel = os.path.relpath(full, src)+ z.write(full, rel)+print(f"Repacked {out}")+PY++echo "Final wheel contents:"+unzip -l "$WHEEL"