build: WASM npm build runs out-of-source (out-of-tree)

npm/build.sh built in-tree: it cd'd into the source root, ran
`./configure` there, appended the WASM defs.mak to the source-tree
defs.mak, and copied magic.js/.wasm out of the source magic/ dir.

Run each variant in a dedicated out-of-source build dir instead
(build-wasm-<variant>, overridable via WASM_BUILD_DIR):

* configure is invoked as "$REPO_ROOT/configure" from $build_dir, so
  config.status and all generated files land under $build_dir; the
  source tree is only read (VPATH).
* the WASM defs.mak is appended to $build_dir/defs.mak.
* artifacts are copied from $build_dir/magic/.
* a fresh `rm -rf $build_dir` per run replaces the old in-tree
  `make distclean` (which also removes the need to touch the source
  tree, and sidesteps the tracked-rules.mak distclean hazard entirely).

CRLF stripping now targets the source-tree configure inputs explicitly.

Note: build-wasm-<variant> dirs are not gitignored (left per task scope);
they are disposable and recreated each run.

(Static edit; not runtime-verified — emscripten is not available here.)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Darryl L. Miles 2026-07-22 14:00:30 +00:00 committed by R. Timothy Edwards
parent 579c05feea
commit 8309f0a504
1 changed files with 19 additions and 16 deletions

View File

@ -155,29 +155,32 @@ ensure_tcl_built() {
build_variant() {
local variant=$1
local out_dir="$SCRIPT_DIR/$variant"
# Out-of-source build directory (sibling of the TCL WASM build dir). Magic
# now supports VPATH builds, so the WASM build reads from the source tree and
# writes only under $build_dir — the source tree is never mutated. A fresh
# dir per run replaces the old in-tree `make distclean`.
local build_dir="${WASM_BUILD_DIR:-$REPO_ROOT/build-wasm-$variant}"
echo
echo "==============================================================="
echo "=== building variant: $variant"
echo "=== building variant: $variant (build dir: $build_dir)"
echo "==============================================================="
cd "$REPO_ROOT"
rm -rf "$build_dir"
mkdir -p "$build_dir"
# Full clean — distclean removes the generated defs.mak and module objects.
if [ -f defs.mak ]; then
emmake make distclean || true
fi
rm -f defs.mak database/database.h
# Strip Windows CRLF line endings on the configure inputs (in the source tree,
# read by configure; no-op on Linux-native files).
sed_strip_cr "$REPO_ROOT/configure"
find "$REPO_ROOT/scripts/" -type f -print0 | while IFS= read -r -d '' f; do sed_strip_cr "$f"; done
# Strip Windows CRLF line endings (no-op on Linux-native files).
sed_strip_cr configure
find scripts/ -type f -print0 | while IFS= read -r -d '' f; do sed_strip_cr "$f"; done
cd "$build_dir"
if [ "$variant" = "tcl" ]; then
ensure_tcl_built
CFLAGS="--std=c17 -D_DEFAULT_SOURCE=1 -DEMSCRIPTEN=1 ${EXTRA_CFLAGS}" \
LDFLAGS="${EXTRA_LDFLAGS}" \
emconfigure ./configure \
emconfigure "$REPO_ROOT/configure" \
--without-cairo --without-opengl --without-x --without-tk \
--with-tcl="$TCL_WASM_PREFIX/lib" \
--with-tclincls="$TCL_WASM_PREFIX/include" \
@ -189,7 +192,7 @@ build_variant() {
else
CFLAGS="--std=c17 -D_DEFAULT_SOURCE=1 -DEMSCRIPTEN=1 ${EXTRA_CFLAGS}" \
LDFLAGS="${EXTRA_LDFLAGS}" \
emconfigure ./configure \
emconfigure "$REPO_ROOT/configure" \
--without-cairo --without-opengl --without-x \
--without-tk --without-tcl \
--disable-readline --disable-compression \
@ -198,7 +201,7 @@ build_variant() {
${EXTRA_CONFIGURE_ARGS:-}
fi
cat toolchains/emscripten/defs.mak >> defs.mak
cat "$REPO_ROOT/toolchains/emscripten/defs.mak" >> defs.mak
#
echo "===== defs.mak ====="
@ -211,9 +214,9 @@ build_variant() {
emmake make mains
mkdir -p "$out_dir"
cp magic/magic.js "$out_dir/"
cp magic/magic.wasm "$out_dir/"
test -f magic/magic.wasm.map && cp magic/magic.wasm.map "$out_dir/"
cp "$build_dir/magic/magic.js" "$out_dir/"
cp "$build_dir/magic/magic.wasm" "$out_dir/"
test -f "$build_dir/magic/magic.wasm.map" && cp "$build_dir/magic/magic.wasm.map" "$out_dir/"
echo "Copied magic.js + magic.wasm into npm/$variant/"
}