From e3c059eb9bec1ed8c7c0fd001092ace3d807f64f Mon Sep 17 00:00:00 2001 From: Henrik Muehe Date: Thu, 30 Apr 2026 14:43:05 +0200 Subject: [PATCH] Add cached gplan.so build: 30min first time, 3sec after - Dockerfile.gplan: pre-built armv7 image with cbang + camotics objects - build-gplan.sh: relinks against Python 3.5m in ~3sec - Pi Python 3.5 headers cached in .pi/pi-python35.tar.gz (gitignored) --- .gitignore | 1 + .pi/Dockerfile.gplan | 40 ++++++++++++++++++++++++++++++ .pi/build-gplan.sh | 58 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 .pi/Dockerfile.gplan create mode 100755 .pi/build-gplan.sh diff --git a/.gitignore b/.gitignore index 92d0408..6c57627 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,4 @@ src/py/camotics/gplan.so src/avr/emu/bbemu src/avr/emu/build/ +.pi/pi-python35.tar.gz diff --git a/.pi/Dockerfile.gplan b/.pi/Dockerfile.gplan new file mode 100644 index 0000000..88c0e9c --- /dev/null +++ b/.pi/Dockerfile.gplan @@ -0,0 +1,40 @@ +# Pre-built armv7 environment for gplan.so +# Build once: docker build --platform linux/arm/v7 -t onefin-gplan -f .pi/Dockerfile.gplan .pi/ +# Then use: .pi/build-gplan.sh +FROM debian:bullseye + +RUN apt-get update -qq && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends \ + build-essential scons git ca-certificates python3-dev \ + libssl-dev libexpat1-dev libbz2-dev liblz4-dev zlib1g-dev perl file && \ + rm -rf /var/lib/apt/lists/* + +# Clone and build cbang (static lib, ~5min under QEMU) +RUN mkdir -p /opt/cbang && cd /opt/cbang && git init -q && \ + git remote add origin https://github.com/CauldronDevelopmentLLC/cbang && \ + git fetch --depth 1 -q origin 18f1e963107ef26abe750c023355a5c40dd07853 && \ + git reset --hard FETCH_HEAD -q && \ + scons -j2 disable_local="re2 libevent" && \ + rm -rf .git build/dep + +# Clone and patch camotics (source only, don't compile yet — that depends on workspace) +RUN mkdir -p /opt/camotics && cd /opt/camotics && git init -q && \ + git remote add origin https://github.com/CauldronDevelopmentLLC/camotics && \ + git fetch --depth 1 -q origin ec876c80d20fc19837133087cef0c447df5a939d && \ + git reset --hard FETCH_HEAD -q && \ + mkdir -p build && touch build/version.txt && \ + P="src/gcode/plan" && \ + for F in LineCommand.cpp LinePlanner.cpp; do \ + for V in maxVel maxJerk maxAccel; do \ + perl -i -0pe "s/(fabs\((config\.$V\[axis\]) \/ unit\[axis\]\));/std::min(\2, \1);/gm" $P/$F; \ + done; \ + done && \ + rm -rf .git + +# Pre-compile camotics objects (the slow part, ~20min under QEMU) +ENV CBANG_HOME=/opt/cbang +RUN cd /opt/camotics && scons -j2 gplan.so with_gui=0 with_tpl=0 && \ + rm -f gplan.so # remove the python3.9-linked one, we relink at build time + +ENV CBANG_HOME=/opt/cbang +WORKDIR /opt/camotics diff --git a/.pi/build-gplan.sh b/.pi/build-gplan.sh new file mode 100755 index 0000000..4789eb3 --- /dev/null +++ b/.pi/build-gplan.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Build gplan.so for the Onefinity Pi (armv7l, Python 3.5) +# +# First run: ~30min (builds the Docker image with pre-compiled cbang/camotics) +# After that: ~2sec (just relinks against Python 3.5m) +# +# Prerequisites: +# - Docker with QEMU binfmt support (default on Docker Desktop) +# - Python 3.5 headers from the Pi in .pi/pi-python35.tar.gz +# Grab once: ssh bbmc@10.1.10.55 'tar czf - /usr/include/python3.5m \ +# /usr/lib/arm-linux-gnueabihf/libpython3.5m.so*' > .pi/pi-python35.tar.gz + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +PROJECT_DIR="$(dirname "$SCRIPT_DIR")" +IMAGE="onefin-gplan" +HEADERS="$SCRIPT_DIR/pi-python35.tar.gz" +OUTPUT="$PROJECT_DIR/src/py/camotics/gplan.so" + +# Check for Python 3.5 headers +if [[ ! -f "$HEADERS" ]]; then + echo "Python 3.5 headers not found at $HEADERS" + echo "Fetching from Pi..." + ssh bbmc@10.1.10.55 'tar czf - /usr/include/python3.5m \ + /usr/lib/arm-linux-gnueabihf/libpython3.5m.so*' > "$HEADERS" +fi + +# Build image if needed (one-time, ~30min) +if ! docker image inspect "$IMAGE" >/dev/null 2>&1; then + echo "Building $IMAGE Docker image (one-time, ~30min under QEMU)..." + docker build --platform linux/arm/v7 -t "$IMAGE" -f "$SCRIPT_DIR/Dockerfile.gplan" "$SCRIPT_DIR" +fi + +# Relink gplan.so against Python 3.5m (~2sec) +echo "Linking gplan.so against Python 3.5m..." +docker run --rm --platform linux/arm/v7 \ + -v "$HEADERS:/tmp/pi-python35.tar.gz:ro" \ + -v "$PROJECT_DIR:/workspace" \ + "$IMAGE" bash -c ' + tar xzf /tmp/pi-python35.tar.gz -C / + ln -sf /usr/lib/arm-linux-gnueabihf/libpython3.5m.so.1.0 \ + /usr/lib/arm-linux-gnueabihf/libpython3.5m.so + + g++ -o /workspace/src/py/camotics/gplan.so \ + -Wl,--as-needed -Wl,-s -Wl,-x -Wl,--gc-sections -pthread -shared \ + build/gplan.os -L/opt/cbang/lib \ + build/libCAMoticsPy.a build/libCAMotics.a build/libDXF.a \ + build/libSTL.a build/libGCode.a \ + -lstdc++ -lutil -lm -ldl -lz -lcbang -lcbang-boost \ + -lssl -lcrypto -llz4 -lexpat -lbz2 -lcrypt -lpthread \ + -lpython3.5m build/dxflib/libdxflib.a + + file /workspace/src/py/camotics/gplan.so + readelf -d /workspace/src/py/camotics/gplan.so | grep python +' + +echo "✓ Built: $OUTPUT"