2025-12-08 12:07:01 +01:00
|
|
|
#!/bin/bash
|
2026-05-29 12:01:44 +02:00
|
|
|
# Build a KiCad app (pcbnew, eeschema, calculator) inside Docker, then run
|
|
|
|
|
# asyncify and friends on the host.
|
2026-05-29 06:39:23 +02:00
|
|
|
#
|
|
|
|
|
# Usage:
|
2026-05-29 12:01:44 +02:00
|
|
|
# ./docker/build.sh <app> [args...]
|
|
|
|
|
#
|
|
|
|
|
# Apps:
|
2026-06-02 13:35:37 +02:00
|
|
|
# pcbnew PCB editor
|
|
|
|
|
# eeschema schematic editor
|
|
|
|
|
# calculator PCB calculator
|
|
|
|
|
# pl_editor drawing-sheet editor
|
|
|
|
|
# symbol_editor symbol editor (eeschema kiface, FRAME_SCH_SYMBOL_EDITOR)
|
|
|
|
|
# all build all of the above sequentially
|
2026-05-29 12:01:44 +02:00
|
|
|
#
|
|
|
|
|
# Any extra args are forwarded to scripts/kicad/build-<app>.sh (e.g. -j 8,
|
|
|
|
|
# --full, --release, --diag=gal).
|
2025-12-13 22:13:31 +01:00
|
|
|
#
|
|
|
|
|
# The build is split into two phases:
|
|
|
|
|
# 1. Docker: Compile KiCad to WASM (without asyncify)
|
|
|
|
|
# 2. Host: Apply asyncify transformation (uses Binaryen v121)
|
|
|
|
|
#
|
|
|
|
|
# Binaryen is downloaded automatically - no prerequisites needed.
|
|
|
|
|
|
2026-06-01 17:31:03 +02:00
|
|
|
# Auto-launch the live progress dashboard in this terminal (handled by logging.sh,
|
|
|
|
|
# which owns the TTY before it re-execs us with output redirected). Set KICAD_NO_MONITOR=1
|
|
|
|
|
# to disable. MUST be set before sourcing logging.sh — that's where the dashboard is
|
|
|
|
|
# launched, in the pre-re-exec process.
|
|
|
|
|
export KICAD_MONITOR=1
|
|
|
|
|
|
2026-05-29 12:01:44 +02:00
|
|
|
# Redirect all output to a log file (re-execs script with redirection).
|
|
|
|
|
# MUST be sourced before arg parsing — the re-exec relies on the original
|
|
|
|
|
# "$@", so any shifts before this point would strip args from the re-exec.
|
2026-05-29 06:39:23 +02:00
|
|
|
source "$(dirname "$0")/../scripts/common/logging.sh"
|
|
|
|
|
|
2026-06-01 17:31:03 +02:00
|
|
|
# Build-progress markers (parsed by scripts/build-monitor.sh).
|
|
|
|
|
source "$(dirname "$0")/../scripts/common/stages.sh"
|
|
|
|
|
|
2025-12-08 12:07:01 +01:00
|
|
|
set -e
|
|
|
|
|
|
2026-06-01 17:31:03 +02:00
|
|
|
# Emit a completion/failure marker no matter how the build ends, so the monitor
|
|
|
|
|
# can stop on a clean "done" or show an aborted state instead of hanging.
|
|
|
|
|
trap '_rc=$?; if [ $_rc -eq 0 ]; then kw_done; else kw_fail $_rc; fi' EXIT
|
|
|
|
|
# On Ctrl-C, mark the build aborted so the final dashboard frame shows failed
|
|
|
|
|
# (not a stale "running" state). The EXIT trap above also fires; the monitor reads
|
|
|
|
|
# the last marker, so the duplicate is harmless.
|
|
|
|
|
trap 'kw_fail 130; exit 130' INT TERM
|
|
|
|
|
|
2025-12-08 12:07:01 +01:00
|
|
|
cd "$(dirname "$0")/.."
|
|
|
|
|
|
2026-06-02 13:35:37 +02:00
|
|
|
VALID_APPS="pcbnew | eeschema | calculator | pl_editor | symbol_editor | all"
|
2026-05-29 12:01:44 +02:00
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
|
echo "Usage: ./docker/build.sh <app> [args...]" >&2
|
|
|
|
|
echo " <app>: ${VALID_APPS}" >&2
|
|
|
|
|
echo " args: forwarded to scripts/kicad/build-<app>.sh (e.g. -j 8, --release)" >&2
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# First positional arg must be the app name. No default — picking one would
|
|
|
|
|
# silently build the wrong thing for someone who forgot the argument.
|
|
|
|
|
if [[ "${1:-}" == "-h" ]] || [[ "${1:-}" == "--help" ]]; then
|
|
|
|
|
usage
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
if [[ $# -lt 1 ]] || [[ "$1" == -* ]]; then
|
|
|
|
|
echo "Error: missing <app> argument" >&2
|
|
|
|
|
usage
|
|
|
|
|
exit 1
|
2026-05-29 06:39:23 +02:00
|
|
|
fi
|
2026-05-29 12:01:44 +02:00
|
|
|
APP_NAME="$1"
|
|
|
|
|
shift
|
2026-05-29 06:39:23 +02:00
|
|
|
|
|
|
|
|
case "$APP_NAME" in
|
2026-06-02 13:35:37 +02:00
|
|
|
pcbnew|eeschema|calculator|pl_editor|symbol_editor|all) ;;
|
2026-05-29 06:39:23 +02:00
|
|
|
*)
|
2026-05-29 12:01:44 +02:00
|
|
|
echo "Error: unknown app '$APP_NAME' (expected: ${VALID_APPS})" >&2
|
|
|
|
|
usage
|
2026-05-29 06:39:23 +02:00
|
|
|
exit 1
|
|
|
|
|
;;
|
|
|
|
|
esac
|
|
|
|
|
|
2026-06-01 16:30:29 +02:00
|
|
|
# Use branch name as Docker Compose project name for isolated containers/volumes.
|
|
|
|
|
# Honor a pre-set COMPOSE_PROJECT_NAME so a build can target an existing volume
|
|
|
|
|
# (e.g. reuse another branch's already-provisioned deps).
|
2026-01-03 07:31:46 +01:00
|
|
|
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD | tr '/' '-' | tr '[:upper:]' '[:lower:]')
|
2026-06-01 16:30:29 +02:00
|
|
|
export COMPOSE_PROJECT_NAME="${COMPOSE_PROJECT_NAME:-kicad-wasm-${BRANCH_NAME}}"
|
2026-01-03 07:31:46 +01:00
|
|
|
echo "Using Docker project: ${COMPOSE_PROJECT_NAME}"
|
2026-05-29 06:39:23 +02:00
|
|
|
echo "Building app: ${APP_NAME}"
|
2026-01-03 07:31:46 +01:00
|
|
|
|
2025-12-15 11:47:50 +01:00
|
|
|
# Add -j 10 by default if no -j flag is given
|
|
|
|
|
ARGS=("$@")
|
|
|
|
|
if [[ ! " ${ARGS[*]} " =~ " -j " ]]; then
|
|
|
|
|
ARGS+=("-j" "10")
|
|
|
|
|
fi
|
|
|
|
|
|
2025-12-08 12:07:01 +01:00
|
|
|
# Start container if not running
|
|
|
|
|
docker compose -f docker/docker-compose.yml up -d
|
|
|
|
|
|
2026-01-05 10:20:08 +01:00
|
|
|
# Sync source code to container volume (fixes macOS Docker VirtioFS issues)
|
|
|
|
|
# Use --checksum to only transfer files with different CONTENT, not timestamps.
|
|
|
|
|
# This avoids the timestamp mismatch cycle that caused full rebuilds every time.
|
|
|
|
|
# Transferred files get current container time, so make detects them correctly.
|
2026-06-01 17:31:03 +02:00
|
|
|
kw_stage container-sync
|
2025-12-27 01:07:44 +01:00
|
|
|
echo "Syncing source code to container..."
|
2026-05-28 17:21:26 +02:00
|
|
|
# rsync into the macOS-backed volume intermittently hits transient VirtioFS glitches:
|
|
|
|
|
# temp-file rename failures (exit 23) or vanished-source files (exit 24, harmless).
|
|
|
|
|
# --inplace avoids the temp-file+rename pattern that triggers exit 23; retry up to 3x
|
|
|
|
|
# for any residual flakiness (--checksum makes each retry skip already-synced files).
|
|
|
|
|
sync_rc=0
|
|
|
|
|
for sync_attempt in 1 2 3; do
|
|
|
|
|
if docker compose -f docker/docker-compose.yml exec kicad-wasm-builder \
|
|
|
|
|
rsync -r --delete --checksum --inplace \
|
|
|
|
|
--exclude="build-wasm" \
|
|
|
|
|
--exclude="output" \
|
|
|
|
|
--exclude=".git" \
|
|
|
|
|
--exclude="logs" \
|
|
|
|
|
--exclude=".idea" \
|
|
|
|
|
--exclude="node_modules" \
|
|
|
|
|
--exclude="tools/emsdk" \
|
|
|
|
|
/workspace-host/ /workspace/
|
|
|
|
|
then
|
|
|
|
|
sync_rc=0
|
|
|
|
|
else
|
|
|
|
|
sync_rc=$?
|
|
|
|
|
fi
|
|
|
|
|
{ [ $sync_rc -eq 0 ] || [ $sync_rc -eq 24 ]; } && break
|
|
|
|
|
echo "rsync attempt ${sync_attempt} failed (exit ${sync_rc}); retrying in 2s..."
|
|
|
|
|
sleep 2
|
|
|
|
|
done
|
|
|
|
|
if [ $sync_rc -ne 0 ] && [ $sync_rc -ne 24 ]; then
|
|
|
|
|
echo "ERROR: source sync failed after retries (exit ${sync_rc})"; exit 1
|
|
|
|
|
fi
|
2025-12-27 01:07:44 +01:00
|
|
|
|
2026-05-29 12:01:44 +02:00
|
|
|
# Map an app name to its inner CMake build subdirectory. Most apps share their
|
|
|
|
|
# subdir name with the app name; pcb_calculator emits OUTPUT_NAME=calculator
|
|
|
|
|
# but lives under the pcb_calculator/ subtree.
|
|
|
|
|
kicad_subdir_for() {
|
|
|
|
|
case "$1" in
|
2026-06-02 13:35:37 +02:00
|
|
|
calculator) echo "pcb_calculator" ;;
|
|
|
|
|
pl_editor) echo "pagelayout_editor" ;;
|
|
|
|
|
symbol_editor) echo "eeschema" ;;
|
|
|
|
|
*) echo "$1" ;;
|
2026-05-29 12:01:44 +02:00
|
|
|
esac
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-29 06:39:23 +02:00
|
|
|
# Build one app: compile in container, then run host-side post-processing.
|
2026-06-01 17:31:03 +02:00
|
|
|
# Args: <app> [index] [total] — index/total drive the monitor's app counter.
|
2026-05-29 06:39:23 +02:00
|
|
|
build_app() {
|
|
|
|
|
local app="$1"
|
2026-06-01 17:31:03 +02:00
|
|
|
local index="${2:-1}"
|
|
|
|
|
local total="${3:-1}"
|
2026-05-29 12:01:44 +02:00
|
|
|
local subdir
|
|
|
|
|
subdir=$(kicad_subdir_for "$app")
|
2026-06-01 17:31:03 +02:00
|
|
|
kw_app "$app" "$index" "$total"
|
2026-05-29 06:39:23 +02:00
|
|
|
echo ""
|
2026-06-01 17:31:03 +02:00
|
|
|
echo "=== Building ${app} (${index}/${total}) ==="
|
2026-05-29 06:39:23 +02:00
|
|
|
|
2026-05-29 12:01:44 +02:00
|
|
|
# Run build inside the container.
|
2026-05-29 06:39:23 +02:00
|
|
|
# -e EMSDK=/emsdk: `docker compose exec` bypasses the entrypoint that sources
|
2026-05-29 12:01:44 +02:00
|
|
|
# emsdk_env.sh, so the build shell would lack emcc/embuilder on PATH. Setting
|
|
|
|
|
# EMSDK lets scripts/common/env.sh source /emsdk/emsdk_env.sh and activate the toolchain.
|
|
|
|
|
docker compose -f docker/docker-compose.yml exec -e EMSDK=/emsdk kicad-wasm-builder \
|
2026-05-29 06:39:23 +02:00
|
|
|
"/workspace/scripts/kicad/build-${app}.sh" "${ARGS[@]}"
|
|
|
|
|
|
|
|
|
|
# Copy output to host-accessible directory.
|
|
|
|
|
# ${app}.wasm.debug.wasm contains DWARF debug info (when built with -gseparate-dwarf).
|
2026-06-01 17:31:03 +02:00
|
|
|
kw_stage copy-output
|
2026-05-29 06:39:23 +02:00
|
|
|
echo "Copying ${app} build output to ./output/..."
|
|
|
|
|
docker compose -f docker/docker-compose.yml exec kicad-wasm-builder \
|
|
|
|
|
bash -c "mkdir -p /workspace/output && \
|
2026-05-29 12:01:44 +02:00
|
|
|
cp /workspace/build-wasm/kicad-${app}/${subdir}/${app}.{js,wasm,wasm.debug.wasm,wasm.map,worker.js} /workspace/output/ 2>/dev/null || \
|
|
|
|
|
cp /workspace/build-wasm/kicad-${app}/${subdir}/${app}.{js,wasm} /workspace/output/; \
|
2026-05-29 06:39:23 +02:00
|
|
|
cp /workspace/build-wasm/kicad-${app}/resources/images.tar.gz /workspace/output/ 2>/dev/null || true; \
|
|
|
|
|
cp /workspace/build-wasm/wxwidgets/build/wasm/wx.js /workspace/output/ 2>/dev/null || true"
|
|
|
|
|
|
|
|
|
|
# Inject dynCall shims (fixes "dynCall_* is not defined" errors in Emscripten 4.x)
|
2026-06-01 17:31:03 +02:00
|
|
|
kw_stage dyncall-shims
|
2026-05-29 06:39:23 +02:00
|
|
|
./scripts/common/inject-dyncall-shims.sh "output/${app}.js"
|
|
|
|
|
|
|
|
|
|
# Apply wasm-emscripten-finalize on host (skipped in Docker due to memory limits)
|
2026-06-01 17:31:03 +02:00
|
|
|
kw_stage finalize
|
2026-05-29 06:39:23 +02:00
|
|
|
./scripts/common/apply-finalize.sh "output/${app}.wasm" "output/${app}.wasm"
|
|
|
|
|
|
|
|
|
|
# Apply asyncify transformation on host
|
2026-06-01 17:31:03 +02:00
|
|
|
kw_stage asyncify
|
2026-05-29 06:39:23 +02:00
|
|
|
./scripts/common/apply-asyncify.sh "output/${app}.wasm" "output/${app}.wasm"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if [[ "${APP_NAME}" == "all" ]]; then
|
2026-06-02 13:35:37 +02:00
|
|
|
build_app pcbnew 1 5
|
|
|
|
|
build_app eeschema 2 5
|
|
|
|
|
build_app calculator 3 5
|
|
|
|
|
build_app pl_editor 4 5
|
|
|
|
|
build_app symbol_editor 5 5
|
2026-05-29 06:39:23 +02:00
|
|
|
else
|
2026-06-01 17:31:03 +02:00
|
|
|
build_app "${APP_NAME}" 1 1
|
2026-05-29 06:39:23 +02:00
|
|
|
fi
|
2025-12-13 22:13:31 +01:00
|
|
|
|
|
|
|
|
echo ""
|
2025-12-08 12:07:01 +01:00
|
|
|
echo "Build complete. Output files in ./output/"
|
2025-12-13 22:13:31 +01:00
|
|
|
ls -lh output/
|