Files
mapper/scripts/build.sh

302 lines
12 KiB
Bash

#!/bin/bash
# MPVJ Build Script
# Runs on x86_64 (e.g., Docker) to cross-compile artifacts for aarch64 (RPi 3B).
# Outputs (committed back to the repo):
# frontend/dist/ — pre-built Vite bundle
# bin/ofxPiMapper — aarch64 binary
#
# Usage:
# bash build.sh <REPO_URL> # clones repo into ./mapper, then builds
# bash build.sh <REPO_URL> <DIR> # clones into DIR, then builds
set -e
REPO_URL="${1:-}"
REPO_DIR="${2:-$(pwd)/mapper}"
OF_VERSION="0.12.1"
OF_DIR="$REPO_DIR/.build/openFrameworks"
OF_FILE="of_v${OF_VERSION}_linuxaarch64_release.tar.gz"
OF_URL="https://github.com/openframeworks/openFrameworks/releases/download/${OF_VERSION}/${OF_FILE}"
BIN_OUT="$REPO_DIR/bin"
if [ -z "$REPO_URL" ]; then
echo "Usage: bash $0 <REPO_URL> [CLONE_DIR]"
exit 1
fi
# ── Clone repo if needed ─────────────────────────────────────────────────────
if [ ! -d "$REPO_DIR/frontend" ] || [ ! -d "$REPO_DIR/backend" ]; then
echo "Cloning $REPO_URL into $REPO_DIR ..."
git clone "$REPO_URL" "$REPO_DIR"
fi
echo "Building from: $REPO_DIR"
# ── 1. Install build dependencies ───────────────────────────────────────────
echo "Installing build dependencies..."
if command -v apt-get > /dev/null 2>&1; then
# Enable arm64 packages alongside x86_64 (Debian multiarch)
dpkg --add-architecture arm64
apt-get update -y
# Cross-compiler toolchain (host)
DEBIAN_FRONTEND=noninteractive apt-get install -y \
git curl wget unzip build-essential pkg-config \
gcc-aarch64-linux-gnu g++-aarch64-linux-gnu \
binutils-aarch64-linux-gnu
# aarch64 dev libraries (provides the .pc files pkg-config needs)
# Note: some arm64 packages share man pages with their amd64 counterparts —
# --force-overwrite is safe here as we only care about headers/.so/.pc files.
DEBIAN_FRONTEND=noninteractive apt-get install -y \
-o Dpkg::Options::="--force-overwrite" \
libmpg123-dev:arm64 libsndfile1-dev:arm64 libopenal-dev:arm64 libassimp-dev:arm64 \
librtaudio-dev:arm64 \
libglew-dev:arm64 libglfw3-dev:arm64 liburiparser-dev:arm64 \
libcurl4-openssl-dev:arm64 libpugixml-dev:arm64 libasound2-dev:arm64 \
libgstreamer1.0-dev:arm64 libgstreamer-plugins-base1.0-dev:arm64 \
libgtk-3-dev:arm64 libboost-filesystem-dev:arm64 \
libfontconfig1-dev:arm64 libfreetype-dev:arm64 libx11-dev:arm64 \
libxrandr-dev:arm64 libxinerama-dev:arm64 libxcursor-dev:arm64 libxi-dev:arm64 \
libpulse-dev:arm64 libudev-dev:arm64 libfreeimage-dev:arm64 \
freeglut3-dev:arm64 libxxf86vm-dev:arm64 \
libcairo2-dev:arm64
# These conflict with amd64 -dev headers; install runtime-only for .so + .pc
DEBIAN_FRONTEND=noninteractive apt-get install -y \
-o Dpkg::Options::="--force-overwrite" \
libzmq5:arm64 libpsl5:arm64 || true
# Repair any partially-broken state from conflicts
apt-get -f install -y
# Generate minimal .pc files for runtime-only arm64 packages that lack one
if [ ! -f /usr/lib/aarch64-linux-gnu/pkgconfig/libzmq.pc ]; then
ZMQ_VER=$(dpkg -l libzmq5:arm64 2>/dev/null | awk '/^ii/{print $3}' | cut -d- -f1 || echo "4.3.5")
mkdir -p /usr/lib/aarch64-linux-gnu/pkgconfig
cat > /usr/lib/aarch64-linux-gnu/pkgconfig/libzmq.pc <<EOF
prefix=/usr
exec_prefix=\${prefix}
libdir=\${prefix}/lib/aarch64-linux-gnu
includedir=\${prefix}/include
Name: libzmq
Description: ZeroMQ messaging library
Version: $ZMQ_VER
Libs: -L\${libdir} -lzmq
Cflags: -I\${includedir}
EOF
fi
fi
# Install Node.js if missing
if ! command -v node > /dev/null 2>&1; then
echo "Installing Node.js 20..."
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt-get install -y nodejs
fi
# ── 2. Build frontend ────────────────────────────────────────────────────────
echo ""
echo "=== Building frontend ==="
cd "$REPO_DIR/frontend"
rm -rf node_modules package-lock.json
npm install --package-lock=false
NODE_OPTIONS="--max-old-space-size=512" npm run build
echo "Frontend built → $REPO_DIR/frontend/dist/"
# ── 3. Download OpenFrameworks aarch64 release ──────────────────────────────
echo ""
echo "=== Downloading OpenFrameworks ${OF_VERSION} (aarch64) ==="
mkdir -p "$REPO_DIR/.build"
if [ ! -d "$OF_DIR" ]; then
cd "$REPO_DIR/.build"
[ ! -f "$OF_FILE" ] && (wget "$OF_URL" || curl -L -O "$OF_URL")
tar -xzf "$OF_FILE"
mv "of_v${OF_VERSION}_linux"*"_release" openFrameworks
rm -f "$OF_FILE"
echo "OpenFrameworks extracted to $OF_DIR"
else
echo "OpenFrameworks already present, skipping download."
fi
# ── 4. Clone & patch ofxPiMapper ────────────────────────────────────────────
echo ""
echo "=== Cloning & patching ofxPiMapper ==="
ADDON_DIR="$OF_DIR/addons/ofxPiMapper"
rm -rf "$ADDON_DIR"
git clone --depth 1 https://github.com/kr15h/ofxPiMapper.git "$ADDON_DIR"
cd "$ADDON_DIR"
# 4a. OscControl.cpp highlight patch
OSC_FILE=$(find src -name "OscControl.cpp" | head -n 1)
if [ -n "$OSC_FILE" ]; then
sed -i '/if (m.getAddress() == "\/ofxPiMapper\/surface\/select"){/r /dev/stdin' "$OSC_FILE" <<'EOF_PATCH'
if (m.getAddress() == "/ofxPiMapper/surface/highlight"){
int surfaceIndex = m.getArgAsInt32(0);
mapper->getSurfaceManager()->selectSurface(surfaceIndex);
return;
}
EOF_PATCH
fi
# 4b. C++11 compat header
cat <<'EOF_COMPAT' > src/cpp11_compat.h
#ifndef OFXPIMAPPER_CPP11_COMPAT_H
#define OFXPIMAPPER_CPP11_COMPAT_H
#include <memory>
#include <utility>
#include <vector>
#include <string>
#include <algorithm>
#if __cplusplus < 201402L
namespace std {
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
}
#endif
using std::unique_ptr;
using std::make_unique;
using std::vector;
using std::string;
#endif
EOF_COMPAT
sed -i '1i #include "cpp11_compat.h"' src/ofxPiMapper.h
find src -type f \( -name "*.h" -o -name "*.cpp" \) \
-exec sed -i 's/\([^:a-zA-Z0-9]\)unique_ptr\b/\1std::unique_ptr/g' {} + \
-exec sed -i 's/\([^:a-zA-Z0-9]\)make_unique\b/\1std::make_unique/g' {} +
for f in $(grep -l "unique_ptr" src/*.h 2>/dev/null || true); do
grep -q "<memory>" "$f" || sed -i '1i #include <memory>' "$f"
done
# ── 5. Cross-compile ofxPiMapper for aarch64 ────────────────────────────────
# OF's Makefile supports PLATFORM_ARCH and CROSS_COMPILING as make variables —
# no need to fake uname. Passing them directly bypasses the uname -m check.
echo ""
echo "=== Cross-compiling ofxPiMapper (aarch64) ==="
if ! command -v aarch64-linux-gnu-g++ > /dev/null 2>&1; then
echo "ERROR: aarch64-linux-gnu-g++ not found. Install gcc-aarch64-linux-gnu."
exit 1
fi
cd "$ADDON_DIR/example_basic"
GLOBAL_FLAGS="-Wno-error -Wno-reorder -Wno-sign-compare -Wno-delete-non-virtual-dtor -std=c++11 -O0 -fno-tree-vrp"
# The OF Makefile has a bug: it calls `export $(PKG_CONFIG_PATH)` which passes the
# VALUE as a variable name under /bin/sh, causing "bad variable name" errors.
# Workaround: don't set PKG_CONFIG_PATH in the environment at all.
# Instead, create a thin wrapper script and pass it via OF's PKG_CONFIG make variable.
PKGCFG_WRAPPER="/usr/local/bin/aarch64-pkg-config"
cat > "$PKGCFG_WRAPPER" << 'PKGCFG_EOF'
#!/bin/sh
exec env \
PKG_CONFIG_PATH=/usr/lib/aarch64-linux-gnu/pkgconfig:/usr/share/pkgconfig \
PKG_CONFIG_LIBDIR=/usr/lib/aarch64-linux-gnu/pkgconfig:/usr/share/pkgconfig \
PKG_CONFIG_SYSROOT_DIR="" \
pkg-config "$@"
PKGCFG_EOF
chmod +x "$PKGCFG_WRAPPER"
# Ensure PKG_CONFIG_PATH is not set in our environment (avoid the OF Makefile
# "export $(PKG_CONFIG_PATH)" bug which treats its value as a variable name).
unset PKG_CONFIG_PATH
unset PKG_CONFIG_LIBDIR
ARM64_PKG_CONFIG_PATH="/usr/lib/aarch64-linux-gnu/pkgconfig:/usr/share/pkgconfig"
# OF's config.shared.mk uses $(shell find ...) to discover OF source subdirectory
# include paths and thirdparty lib headers. This can return empty in Docker
# cross-compile environments, causing "ofConstants.h: No such file or directory".
# Work around it by computing the include paths here and injecting them via
# PLATFORM_CFLAGS (which appears in the project compile command regardless).
OF_EXTRA_INCLUDES="-I$OF_DIR/libs/openFrameworks"
# OF source subdirectories (app, utils, math, gl, graphics, events, etc.)
for d in "$OF_DIR/libs/openFrameworks"/*/; do
[ -d "$d" ] && OF_EXTRA_INCLUDES="$OF_EXTRA_INCLUDES -I${d%/}"
done
# Thirdparty header-only libs bundled with OF (glm, json, kiss, tess2, utf8, ...)
for lib in "$OF_DIR/libs"/*/; do
libname=$(basename "$lib")
[ "$libname" = "openFrameworks" ] && continue
[ "$libname" = "openFrameworksCompiled" ] && continue
incdir="${lib}include"
[ -d "$incdir" ] || continue
while IFS= read -r subdir; do
OF_EXTRA_INCLUDES="$OF_EXTRA_INCLUDES -I$subdir"
done < <(find "$incdir" -type d)
done
# ofxPiMapper addon subdirectories (Sources/, Surfaces/, Textures/, etc.)
while IFS= read -r subdir; do
OF_EXTRA_INCLUDES="$OF_EXTRA_INCLUDES -I$subdir"
done < <(find "$ADDON_DIR/src" -type d)
# Bundled OF addons that ofxPiMapper depends on (e.g. ofxXmlSettings, ofxOsc, ...)
# Read the list from addons.make if present, otherwise include all bundled addon src dirs.
ADDONS_MAKE="$ADDON_DIR/example_basic/addons.make"
if [ -f "$ADDONS_MAKE" ]; then
while IFS= read -r addon_name; do
addon_name="${addon_name%%#*}" # strip comments
addon_name="$(echo "$addon_name" | tr -d '[:space:]')"
[ -z "$addon_name" ] && continue
addon_src="$OF_DIR/addons/$addon_name/src"
[ -d "$addon_src" ] || continue
while IFS= read -r subdir; do
OF_EXTRA_INCLUDES="$OF_EXTRA_INCLUDES -I$subdir"
done < <(find "$addon_src" -type d)
# Also include any libs/ bundled inside the addon (e.g. tinyxml in ofxXmlSettings)
addon_libs="$OF_DIR/addons/$addon_name/libs"
if [ -d "$addon_libs" ]; then
for alib in "$addon_libs"/*/; do
alib_inc="${alib}include"
[ -d "$alib_inc" ] || continue
while IFS= read -r subdir; do
OF_EXTRA_INCLUDES="$OF_EXTRA_INCLUDES -I$subdir"
done < <(find "$alib_inc" -type d)
done
fi
done < "$ADDONS_MAKE"
fi
make clean 2>/dev/null || true
make -j1 \
PKG_CONFIG="$PKGCFG_WRAPPER" \
PKG_CONFIG_LIBDIR="$ARM64_PKG_CONFIG_PATH" \
GST_VERSION=1.0 \
PLATFORM_ARCH=aarch64 \
CROSS_COMPILING=1 \
CC=aarch64-linux-gnu-gcc \
CXX=aarch64-linux-gnu-g++ \
AR=aarch64-linux-gnu-ar \
LD=aarch64-linux-gnu-ld \
PLATFORM_CFLAGS="$GLOBAL_FLAGS $OF_EXTRA_INCLUDES -I$ADDON_DIR/src" \
USER_CFLAGS="$GLOBAL_FLAGS" \
USER_CPPFLAGS="-std=c++11" \
OF_PROJECT_OPTIMIZATION_FLAGS="-O0"
# ── 6. Collect artifacts ─────────────────────────────────────────────────────
echo ""
echo "=== Collecting artifacts ==="
mkdir -p "$BIN_OUT"
cp bin/example_basic "$BIN_OUT/ofxPiMapper"
chmod +x "$BIN_OUT/ofxPiMapper"
# ── 7. Commit and push artifacts ─────────────────────────────────────────────
echo ""
echo "=== Pushing artifacts to remote ==="
cd "$REPO_DIR"
git config user.email "build-bot@mpvj" 2>/dev/null || true
git config user.name "MPVJ Build Bot" 2>/dev/null || true
git add frontend/dist/ bin/ofxPiMapper
git commit -m "ci: add pre-built artifacts (frontend/dist + bin/ofxPiMapper)" || echo "(nothing new to commit)"
git push
echo ""
echo "============================================"
echo " BUILD COMPLETE + PUSHED"
echo "============================================"
echo " Frontend: $REPO_DIR/frontend/dist/"
echo " ofxPiMapper: $BIN_OUT/ofxPiMapper (aarch64)"
echo ""
echo " Run setup.sh on the Pi to deploy."
echo "============================================"