pcbjam/scripts/build-gal-webgl-test.sh
Viktor Vaczi 348596c515 perf(build): use all cores in host builds — wx lib, test apps, gal-webgl
env.sh exports a docker-safe JOBS=1 default, and every host build silently
inherited it (the docker kicad path dodges it by passing -j explicitly):

- build-wx-wasm.sh: the intended "${JOBS:-nproc}" fallback sat BELOW the
  env.sh source, so it was dead code — full wx builds ran make -j1 on the
  Mac and on CI. Compute the all-cores default before env.sh instead
  (explicit JOBS/PARALLEL_JOBS still wins); also -j the PCRE pre-build.
- build-wasm-test.sh: same default fix, plus fan the post-link
  hoist+asyncify loop out across JOBS with xargs -P. Per-app wasm-opt
  can't feed many cores (small modules), so serial stays ~4min even with
  BINARYEN_CORES=16; fanning across the 74 independent apps is what
  scales. Safe: apply-asyncify is in-place per wasm, injector tmp is
  per-js, HOIST_WASMOPT resolved once up front.
- build-gal-webgl-test.sh: make had no -j at all.

Measured on a 16-core M4 Max, clean test-app build: 15m36s (-j1) → 2m08s,
peak RAM 10.7 GB summed across all build processes (64 GB machine; CI
runners have 120 GB). Core counts are always derived from nproc at
runtime — nothing hardcoded, CI workers differ.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XeiSRRScdaox5jBueJNcyG
2026-07-02 10:54:14 +02:00

97 lines
2.7 KiB
Shell
Executable file

#!/bin/bash
#
# Build script for the WebGL GAL test harness (WASM)
#
# This builds a WASM module that renders GAL test scenarios using WebGL,
# allowing comparison against native OpenGL rendering.
#
# Uses a Makefile with direct em++ calls (like build-wasm-test.sh)
# to avoid emcmake Python 3.10+ requirement.
#
# Usage:
# ./scripts/build-gal-webgl-test.sh # Clean build (default)
# ./scripts/build-gal-webgl-test.sh --no-clean # Incremental build
# ./scripts/build-gal-webgl-test.sh --debug # Debug build with source maps
#
# Redirect all output to a log file (re-execs script with redirection)
source "$(dirname "$0")/common/logging.sh"
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Default to all cores BEFORE env.sh (its docker-safe JOBS=1 default would stick
# otherwise); an explicit JOBS/PARALLEL_JOBS from the caller still wins.
JOBS="${JOBS:-$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)}"
# Source common environment (sets up local emsdk)
QUIET=1 source "$SCRIPT_DIR/common/env.sh"
TEST_DIR="$PROJECT_ROOT/tests/gal-regression/wasm"
OUTPUT_DIR="$PROJECT_ROOT/tests/apps/gal-webgl"
echo "Building GAL WebGL Test..."
echo " Test dir: $TEST_DIR"
echo " Output dir: $OUTPUT_DIR"
echo " EMSDK_PYTHON: ${EMSDK_PYTHON:-NOT SET}"
echo " clang: $(which clang)"
# Verify em++ is available
if ! command -v em++ &> /dev/null; then
echo "ERROR: em++ not found. Run: ./scripts/setup-emsdk.sh"
exit 1
fi
echo " Emscripten: $(em++ --version 2>&1 | head -1)"
# Parse arguments
# Default: clean build to avoid stale object file issues (header deps not tracked in old builds)
DEBUG_BUILD=0
CLEAN_BUILD=1
for arg in "$@"; do
if [ "$arg" = "--debug" ]; then
DEBUG_BUILD=1
elif [ "$arg" = "--no-clean" ]; then
CLEAN_BUILD=0
fi
done
# Build using Makefile (compiles WebGL sources directly from kicad/)
cd "$TEST_DIR"
if [ "$CLEAN_BUILD" = "1" ]; then
echo ""
echo "Cleaning..."
make clean 2>/dev/null || true
fi
# Always remove output files to ensure linker flags changes take effect
# (Makefile only tracks object file dependencies, not linker flag changes)
rm -f "$OUTPUT_DIR"/*.js "$OUTPUT_DIR"/*.wasm 2>/dev/null || true
# Generate shaders (converts GLSL 1.20 to GLSL ES 3.00)
# TODO: Eventually these should come from KiCad's build
echo ""
echo "Generating WebGL shaders..."
python3 generate_shaders.py
echo ""
echo "Building..."
if [ "$DEBUG_BUILD" = "1" ]; then
make -j"${JOBS:-1}" DEBUG=1
else
make -j"${JOBS:-1}"
fi
echo ""
echo "Build successful!"
echo ""
echo "Files in $OUTPUT_DIR:"
ls -lh "$OUTPUT_DIR"
echo ""
echo "To test locally:"
echo " cd $PROJECT_ROOT/tests"
echo " npx serve apps"
echo " # Open http://localhost:3000/gal-webgl/gal_webgl_test.html"