feat(wasm): kicad_editor — merge the pcbnew+eeschema kifaces into ONE bundle (Part 2)

All four editors (PCB / Footprint / Schematic / Symbol) are now runtime --frame
choices of a single kicad_editor.wasm (178 MB at -O1 vs 147+82 separate; shared
wx/common/boost linked once). One editor per page load, as before; frames pcb /
fpedit / sch / symedit.

- wasm/editor/: the merged executable target (single_top + both kiface library sets,
  whole-archive pcbcommon) + the safety-net focus-walk Kiface() dispatch TU. Gated by
  KICAD_WASM_MERGED_EDITOR (kicad submodule bump carries the fork side: per-engine
  Kiface/getter binding + ODR renames + dual-kiface launcher).
- wasm/bindings/: per-editor collab entries renamed pcbCollab*/schCollab* (JS names
  unchanged); duplicate kicadOpenFile/kicadCollabOnSave + shared-name registrations
  guarded behind KICAD_MERGED_EMBIND; new kicad_editor_embind.cpp registers each
  shared JS name once, dispatching on the live frame.
- Build: kicad_editor app (build wrapper, target case arms, 3-object embind compile
  with the ABI-critical flags, STUB_APP=pcbnew); docker/build.sh "all" =
  kicad_editor calculator pl_editor gerbview (pcbnew/eeschema stay as explicit debug
  apps); scripts/kicad/audit-merged-symbols.sh = repeatable ODR-collision audit (run
  on kicad bumps).
- Frontend: Bundle type (bundle ≠ tool); TOOL_BUNDLE maps all four editors to
  kicad_editor; explicit --frame tokens for pcbnew (pcb) and eeschema (sch); publish
  list = the 4 real bundles.
- Tests/CI: five harnesses load kicad_editor.js with explicit frame tokens;
  PCBNEW_FAMILY_SPECS renamed BIG_MODULE_SPECS + the 8 eeschema-family specs (they
  now boot the merged module — SpiderMonkey x86 CI OOM routing); frame-runtime spec
  covers all four frames from the one bundle.

Validated so far: frame-runtime 4/4 (each frame boots with the right title, no
aborts, no duplicate embind registration); 24-spec merged-module regression green;
3D raytracer renders. Known pre-existing failure: 3d-viewer title-bar drag deadlock,
fixed on main by 7630c7e (2N+8 pthread pre-warm) — picked up by the follow-up rebase.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Istvan Matejcsok 2026-07-02 14:48:11 +02:00
commit bbeef6d20e
23 changed files with 691 additions and 149 deletions

View file

@ -0,0 +1,98 @@
#!/bin/bash
# Duplicate-symbol audit for the merged kicad_editor image (editor-unification Part 2).
#
# The pcbnew and eeschema kifaces are statically linked into one WASM image with
# --allow-multiple-definition (required for wx/KiCad dupes), so a symbol both engines
# define binds SILENTLY to the first definition — a wrong-behavior/memory-corruption
# bug, never a link error. This script intersects the two kifaces' defined symbols so
# every collision is a conscious decision (the known set is renamed per-engine via
# KICAD_WASM_PCB_SIDE_RENAMES in kicad/CMakeLists.txt).
#
# Run INSIDE the build container (needs $EMSDK llvm-nm and the per-app build trees):
# docker compose -f docker/docker-compose.yml exec kicad-wasm-builder \
# /workspace/scripts/kicad/audit-merged-symbols.sh
# Re-run whenever the kicad submodule is bumped. Expected output: EMPTY intersection
# (all known collisions renamed). Any line of output is a new upstream collision to
# add to the rename list.
#
# Method (see docs/features/editor-unification/06-part2-implementation.md):
# - strong (T/D/B/R) defined external symbols: direct first-def-wins collisions.
# - weak (W/V) symbols are only dangerous when the SAME name has DIFFERENT
# definitions per module (same-named classes with inline members). Weak symbols
# also defined by the shared libs are one shared definition — filtered out. The
# remainder is mostly benign template dedup (magic_enum etc.); review NEW entries.
set -euo pipefail
BUILD_ROOT="${BUILD_ROOT:-/workspace/build-wasm}"
P="${BUILD_ROOT}/kicad-pcbnew"
E="${BUILD_ROOT}/kicad-eeschema"
NM="${EMSDK:?EMSDK not set}/upstream/bin/llvm-nm"
OUT="${TMPDIR:-/tmp}/merged-symbol-audit"
mkdir -p "${OUT}"
if [ ! -d "${P}/pcbnew" ] || [ ! -d "${E}/eeschema" ]; then
echo "ERROR: need built kicad-pcbnew and kicad-eeschema trees under ${BUILD_ROOT}" >&2
echo " (./docker/build.sh pcbnew,eeschema --compile-only)" >&2
exit 1
fi
pcb_files() {
find "${P}/pcbnew/CMakeFiles/pcbnew_kiface_objects.dir" -name '*.o'
ls "${P}/common/libpcbcommon.a" \
"${P}/pcbnew/connectivity/libconnectivity.a" \
"${P}/pcbnew/router/libpnsrouter.a" \
"${P}/pcbnew/navlib/libpcbnew_navlib.a" \
"${P}/utils/idftools/libidf3.a" \
"${P}"/pcbnew/pcb_io/*/*.a 2>/dev/null || true
find "${P}/3d-viewer" -name '*.o' 2>/dev/null || true
}
sch_files() {
find "${E}/eeschema/CMakeFiles/eeschema_kiface_objects.dir" -name '*.o'
ls "${E}/eeschema/navlib/libeeschema_navlib.a" 2>/dev/null || true
}
# Weak symbols defined by the SHARED libs (one definition linked once — safe dedup).
shared_syms() {
${NM} --defined-only --extern-only --format=posix \
"${P}/common/libcommon.a" "${P}/common/libkicommon.a" \
"${P}/common/gal/libkigal.a" "${P}/libs/core/libcore.a" \
"${P}/libs/kimath/libkimath.a" "${P}/libs/kiplatform/libkiplatform.a" \
"${P}/scripting/libscripting.a" "${P}/api/libkiapi.a" \
"${P}/libs/sexpr/libsexpr.a" 2>/dev/null | awk '{print $1}' | sort -u
}
echo "== collecting symbols (llvm-nm)..." >&2
${NM} --defined-only --extern-only --format=posix $(pcb_files) 2>/dev/null \
| awk '$2 ~ /^[TDBR]$/ {print $1}' | sort -u > "${OUT}/pcb_strong.txt"
${NM} --defined-only --extern-only --format=posix $(sch_files) 2>/dev/null \
| awk '$2 ~ /^[TDBR]$/ {print $1}' | sort -u > "${OUT}/sch_strong.txt"
${NM} --defined-only --extern-only --format=posix $(pcb_files) 2>/dev/null \
| awk '$2 ~ /^[WVwv]$/ {print $1}' | sort -u > "${OUT}/pcb_weak.txt"
${NM} --defined-only --extern-only --format=posix $(sch_files) 2>/dev/null \
| awk '$2 ~ /^[WVwv]$/ {print $1}' | sort -u > "${OUT}/sch_weak.txt"
shared_syms > "${OUT}/shared.txt"
# Known-renamed / expected entries. In OPTION-OFF per-app trees (the normal state of
# kicad-pcbnew / kicad-eeschema) the renames are NOT applied, so the known set shows
# up here — filter it; the merged tree compiles with the renames so these cannot
# collide there. KIFACE_1/Kiface get per-engine getter/accessor names; the class/data
# names are prefixed PCB_/pcb (KICAD_WASM_PCB_SIDE_RENAMES).
KNOWN='^(KIFACE_1$|_Z6Kifacev$|_Z16checkOverwriteDb|pcbAllowedActions$|allowedActions$|g_excludedLayers$|_Z[A-Z0-9]*[0-9]+(PCB_)?(DIALOG_TEXT_PROPERTIES|DIALOG_SHAPE_PROPERTIES|DIALOG_TABLE_PROPERTIES|DIALOG_TABLECELL_PROPERTIES|DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS|PANEL_SETUP_FORMATTING|TEXT_SEARCH_HANDLER|GROUP_SEARCH_HANDLER|TEXTBOX_POINT_EDIT_BEHAVIOR|RECTANGLE_POINT_EDIT_BEHAVIOR|FILEDLG_HOOK_SAVE_PROJECT|FOOTPRINT_INFO_GENERATOR))'
# Mangled-name filter needs the raw class tokens too (ZTV/ZTI/ZTS/ZN prefixes).
CLASSES='DIALOG_TEXT_PROPERTIES|DIALOG_SHAPE_PROPERTIES|DIALOG_TABLE_PROPERTIES|DIALOG_TABLECELL_PROPERTIES|DIALOG_GLOBAL_EDIT_TEXT_AND_GRAPHICS|PANEL_SETUP_FORMATTING|TEXT_SEARCH_HANDLER|GROUP_SEARCH_HANDLER|TEXTBOX_POINT_EDIT_BEHAVIOR|RECTANGLE_POINT_EDIT_BEHAVIOR|FILEDLG_HOOK_SAVE_PROJECT|FOOTPRINT_INFO_GENERATOR|checkOverwriteDb|allowedActions|g_excludedLayers'
echo "== STRONG duplicates (unexpected — must be renamed or justified):"
comm -12 "${OUT}/pcb_strong.txt" "${OUT}/sch_strong.txt" \
| grep -vE "${CLASSES}" | grep -vE '^KIFACE_1$|^_Z6Kifacev$' || true
echo "== WEAK duplicates not from shared libs, mentioning NEW class tokens"
echo " (known colliding classes filtered; review anything printed):"
comm -12 "${OUT}/pcb_weak.txt" "${OUT}/sch_weak.txt" \
| comm -23 - "${OUT}/shared.txt" \
| grep -vE "${CLASSES}" \
| grep -E '_Z(TV|TI|TS|N)[0-9]+[A-Z]' \
| grep -vE 'magic_enum|wxEventFunctorMethod|wxNavigationEnabled|wxSimplebook|wxDataView|wxMenuBar|wxVector|KIFACE|COLLECTOR|RC_JSON|PARAM_SCALED|EDA_|BOX2|WX_MENUBAR|SEARCH_HANDLER' || true
echo "== audit done (details in ${OUT})"

View file

@ -31,7 +31,7 @@
set -e
if [ -z "$1" ]; then
echo "Error: missing <app> argument (pcbnew | eeschema | calculator | pl_editor | gerbview)" >&2
echo "Error: missing <app> argument (kicad_editor | pcbnew | eeschema | calculator | pl_editor | gerbview)" >&2
exit 1
fi
APP_NAME="$1"
@ -45,6 +45,15 @@ shift
# The footprint/symbol editors have no target of their own — the pcbnew/eeschema
# bundle opens them at runtime via single_top.cpp's --frame flag.
case "$APP_NAME" in
kicad_editor)
# Merged pcbnew+eeschema image (editor-unification Part 2): one executable
# linking BOTH kifaces, frame chosen at runtime by --frame (pcb/fpedit/sch/
# symedit). Target lives in wasm/editor/ (added by the fork's top-level
# CMakeLists under -DKICAD_WASM_MERGED_EDITOR=ON); its binary dir doubles as
# the artifact subdir.
KICAD_TARGET="kicad_editor"
KICAD_SUBDIR="kicad_editor"
;;
pcbnew|eeschema|gerbview)
KICAD_TARGET="$APP_NAME"
KICAD_SUBDIR="$APP_NAME"
@ -65,7 +74,7 @@ case "$APP_NAME" in
KICAD_SUBDIR="eeschema"
;;
*)
echo "Error: unknown app '$APP_NAME' (expected: pcbnew | eeschema | calculator | pl_editor | gerbview | sym_convert)" >&2
echo "Error: unknown app '$APP_NAME' (expected: kicad_editor | pcbnew | eeschema | calculator | pl_editor | gerbview | sym_convert)" >&2
exit 1
;;
esac
@ -80,9 +89,12 @@ esac
# Which app's WASM stub libraries (scripting/frame placeholders) to link.
# sym_convert links the eeschema kiface objects, so it needs eeschema's frame
# stub (eeschema_frame_stub.cpp).
# stub (eeschema_frame_stub.cpp). kicad_editor links pcbnew's kiface objects, which
# reference the action-plugin scripting placeholders (pcbnewGet*); eeschema's frame
# stub arrives via CMake (target_sources on eeschema_kiface_objects), not this path.
case "$APP_NAME" in
sym_convert) STUB_APP="eeschema" ;;
kicad_editor) STUB_APP="pcbnew" ;;
*) STUB_APP="$APP_NAME" ;;
esac
@ -370,7 +382,15 @@ log_info "KiCad WASM support verified"
# linker line below references "${STUBS_BUILD}/${APP_NAME}_embind.o" so we
# create an empty placeholder when the source is missing, to keep the link
# line stable across apps.
EMBIND_OBJ="${STUBS_BUILD}/${EMBIND_APP}_embind.o"
# kicad_editor (merged image) links THREE embind objects: both per-editor TUs
# (compiled with -DKICAD_MERGED_EMBIND, which compiles out their duplicate
# definitions and shared-name registrations) plus the dispatcher TU that registers
# the shared JS names once — see wasm/bindings/kicad_editor_embind.cpp.
if [ "${APP_NAME}" = "kicad_editor" ]; then
EMBIND_OBJ="${STUBS_BUILD}/pcbnew_embind.o ${STUBS_BUILD}/eeschema_embind.o ${STUBS_BUILD}/kicad_editor_embind.o"
else
EMBIND_OBJ="${STUBS_BUILD}/${EMBIND_APP}_embind.o"
fi
EMBIND_SRC="${PROJECT_ROOT}/wasm/bindings/${EMBIND_APP}_embind.cpp"
# Step 7: Configure KiCad with CMake
@ -392,6 +412,15 @@ if [ "${APP_NAME}" = "sym_convert" ]; then
SYM_CONVERTER_CMAKE_FLAG="-DKICAD_SYM_CONVERTER_WASM=ON"
fi
# Merged pcbnew+eeschema editor: gates the wasm/editor/ subdir, the per-engine
# Kiface()/KIFACE_GETTER renames, and the PCB-side ODR symbol renames in the fork's
# CMake (see KICAD_WASM_MERGED_EDITOR in kicad/CMakeLists.txt). Only this tree
# (build-wasm/kicad-kicad_editor) configures with it ON.
MERGED_EDITOR_CMAKE_FLAG=""
if [ "${APP_NAME}" = "kicad_editor" ]; then
MERGED_EDITOR_CMAKE_FLAG="-DKICAD_WASM_MERGED_EDITOR=ON"
fi
# 3D viewer: built by DEFAULT (BUILD_3D_VIEWER=ON). Opt out with BUILD_3D_VIEWER=OFF, which links the
# 3D stubs instead. The 3D viewer renders with the GL-free CPU raytracer (RENDER_3D_RAYTRACE_RAM)
# blitted to the canvas through a plain WebGL2 textured quad — no -sLEGACY_GL_EMULATION. KiCad's
@ -442,6 +471,7 @@ fi
emcmake cmake "${KICAD_DIR}" \
${CCACHE_OPTS} \
${SYM_CONVERTER_CMAKE_FLAG} \
${MERGED_EDITOR_CMAKE_FLAG} \
-DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
-DCMAKE_INSTALL_PREFIX="${SYSROOT}" \
-DCMAKE_MODULE_PATH="${WASM_LAYER}/cmake" \
@ -495,7 +525,51 @@ emcmake cmake "${KICAD_DIR}" \
# Exposes KiCad objects to JavaScript for future Pyodide integration.
# When no app-specific source exists, build an empty object so the linker line
# referencing ${APP_NAME}_embind.o doesn't break.
if [ -f "${EMBIND_SRC}" ]; then
# Compile one embind TU with the same includes and ABI-critical flags KiCad uses
# (KICAD_TU_ABI_FLAGS must match the core's -DDEBUG state or vtable dispatch skews —
# task #54). Args: <src> <obj> <app-include-subdir> [extra-defines]
compile_embind_tu() {
local _src="$1" _obj="$2" _subdir="$3" _defines="${4:-}"
local _includes="-I${KICAD_BUILD} -I${KICAD_DIR}/include -I${KICAD_DIR}/${_subdir} -I${KICAD_DIR}/common"
# Generated DSN-lexer headers (e.g. pcb_lexer.h, used transitively via kicad_clipboard.h →
# pcb_io_kicad_sexpr_parser.h) are emitted into the common build subdir by make_lexer.
_includes+=" -I${KICAD_BUILD}/common"
_includes+=" -I${KICAD_DIR}/libs/core/include -I${KICAD_DIR}/libs/kimath/include -I${KICAD_DIR}/libs/kiplatform/include"
_includes+=" -I${KICAD_DIR}/thirdparty/clipper2/Clipper2Lib/include"
_includes+=" -I${KICAD_DIR}/thirdparty/nlohmann_json"
_includes+=" -I${KICAD_DIR}/thirdparty/expected/include"
_includes+=" -I${KICAD_DIR}/thirdparty/rtree"
_includes+=" -I${KICAD_DIR}/thirdparty/fmt"
_includes+=" -I${KICAD_DIR}/thirdparty/dynamic_bitset"
_includes+=" -I${KICAD_DIR}/thirdparty/nanodbc"
_includes+=" -I${KICAD_DIR}/thirdparty/picosha2"
_includes+=" -I${KICAD_DIR}/thirdparty"
# libcontext.h lives one level deeper; tool/coroutine.h does #include <libcontext.h>
_includes+=" -I${KICAD_DIR}/thirdparty/libcontext"
_includes+=" -I${SYSROOT}/include"
# KiCad requires C++20 for concepts
em++ -std=c++20 -c ${EXTRA_FLAGS} ${KICAD_TU_ABI_FLAGS} ${WX_CXXFLAGS} ${_defines} ${_includes} "${_src}" -o "${_obj}"
}
if [ "${APP_NAME}" = "kicad_editor" ]; then
# Merged image: both per-editor TUs (with -DKICAD_MERGED_EMBIND compiling out
# their duplicate definitions / shared-name registrations) + the dispatcher TU
# registering the shared JS names once (wasm/bindings/kicad_editor_embind.cpp).
# pcbnew's TU needs the generated lexer headers — pre-build pcbcommon (no wasted
# work: kicad_editor depends on pcbcommon anyway; incremental no-op).
log_info "Pre-building pcbcommon so generated lexer headers exist for the embind compile..."
emmake make -j${JOBS} pcbcommon
log_info "Compiling merged Embind bindings (pcbnew + eeschema + dispatcher)..."
compile_embind_tu "${PROJECT_ROOT}/wasm/bindings/pcbnew_embind.cpp" \
"${STUBS_BUILD}/pcbnew_embind.o" pcbnew "-DKICAD_MERGED_EMBIND"
compile_embind_tu "${PROJECT_ROOT}/wasm/bindings/eeschema_embind.cpp" \
"${STUBS_BUILD}/eeschema_embind.o" eeschema "-DKICAD_MERGED_EMBIND"
compile_embind_tu "${PROJECT_ROOT}/wasm/bindings/kicad_editor_embind.cpp" \
"${STUBS_BUILD}/kicad_editor_embind.o" common
elif [ -f "${EMBIND_SRC}" ]; then
# pcbnew's embind TU transitively includes generated lexer headers
# (kicad_clipboard.h → pcb_io_kicad_sexpr_parser.h → pcb_lexer.h, emitted into
# ${KICAD_BUILD}/common by make_lexer custom commands on the pcbcommon target).
@ -508,26 +582,7 @@ if [ -f "${EMBIND_SRC}" ]; then
emmake make -j${JOBS} pcbcommon
fi
log_info "Compiling Embind bindings (${APP_NAME})..."
# Use the same includes and flags that KiCad uses
KICAD_INCLUDES="-I${KICAD_BUILD} -I${KICAD_DIR}/include -I${KICAD_DIR}/${KICAD_SUBDIR} -I${KICAD_DIR}/common"
# Generated DSN-lexer headers (e.g. pcb_lexer.h, used transitively via kicad_clipboard.h →
# pcb_io_kicad_sexpr_parser.h) are emitted into the common build subdir by make_lexer.
KICAD_INCLUDES+=" -I${KICAD_BUILD}/common"
KICAD_INCLUDES+=" -I${KICAD_DIR}/libs/core/include -I${KICAD_DIR}/libs/kimath/include -I${KICAD_DIR}/libs/kiplatform/include"
KICAD_INCLUDES+=" -I${KICAD_DIR}/thirdparty/clipper2/Clipper2Lib/include"
KICAD_INCLUDES+=" -I${KICAD_DIR}/thirdparty/nlohmann_json"
KICAD_INCLUDES+=" -I${KICAD_DIR}/thirdparty/expected/include"
KICAD_INCLUDES+=" -I${KICAD_DIR}/thirdparty/rtree"
KICAD_INCLUDES+=" -I${KICAD_DIR}/thirdparty/fmt"
KICAD_INCLUDES+=" -I${KICAD_DIR}/thirdparty/dynamic_bitset"
KICAD_INCLUDES+=" -I${KICAD_DIR}/thirdparty/nanodbc"
KICAD_INCLUDES+=" -I${KICAD_DIR}/thirdparty/picosha2"
KICAD_INCLUDES+=" -I${KICAD_DIR}/thirdparty"
# libcontext.h lives one level deeper; tool/coroutine.h does #include <libcontext.h>
KICAD_INCLUDES+=" -I${KICAD_DIR}/thirdparty/libcontext"
KICAD_INCLUDES+=" -I${SYSROOT}/include"
# KiCad requires C++20 for concepts
em++ -std=c++20 -c ${EXTRA_FLAGS} ${KICAD_TU_ABI_FLAGS} ${WX_CXXFLAGS} ${KICAD_INCLUDES} "${EMBIND_SRC}" -o "${EMBIND_OBJ}"
compile_embind_tu "${EMBIND_SRC}" "${EMBIND_OBJ}" "${KICAD_SUBDIR}"
else
log_info "No embind source for ${APP_NAME} (expected at ${EMBIND_SRC}); using empty placeholder"
EMPTY_C="${STUBS_BUILD}/${APP_NAME}_embind_empty.c"
@ -545,10 +600,14 @@ log_info "Building ${APP_NAME} (CMake target: ${KICAD_TARGET})..."
# whenever the freshly-compiled embind object is newer than the linked output.
LINK_OUT_JS="${KICAD_BUILD}/${KICAD_SUBDIR}/${APP_NAME}.js"
LINK_OUT_WASM="${KICAD_BUILD}/${KICAD_SUBDIR}/${APP_NAME}.wasm"
if [ -f "${EMBIND_OBJ}" ] && [ -f "${LINK_OUT_JS}" ] && [ "${EMBIND_OBJ}" -nt "${LINK_OUT_JS}" ]; then
log_info "Embind object newer than ${APP_NAME}.js — forcing relink to pick up new bindings"
rm -f "${LINK_OUT_JS}" "${LINK_OUT_WASM}"
fi
# EMBIND_OBJ may hold several objects (kicad_editor) — check each.
for _embind_obj in ${EMBIND_OBJ}; do
if [ -f "${_embind_obj}" ] && [ -f "${LINK_OUT_JS}" ] && [ "${_embind_obj}" -nt "${LINK_OUT_JS}" ]; then
log_info "Embind object newer than ${APP_NAME}.js — forcing relink to pick up new bindings"
rm -f "${LINK_OUT_JS}" "${LINK_OUT_WASM}"
break
fi
done
emmake make -j${JOBS} "${KICAD_TARGET}"

View file

@ -0,0 +1,9 @@
#!/bin/bash
# Build the merged KiCad editor (pcbnew + eeschema kifaces in ONE image) for
# WebAssembly. The editor frame (PCB / Footprint / Schematic / Symbol) is chosen
# at runtime via single_top.cpp's --frame flag (editor-unification Part 2).
# Thin wrapper around build-kicad-target.sh — see that script for options.
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
exec "${SCRIPT_DIR}/build-kicad-target.sh" kicad_editor "$@"