Complete Phase 1: Build KiCad with optional dependencies disabled

Add CMake override modules, stub implementations, and patches to build
KiCad without CURL, libgit2, OpenCASCADE, and ngspice dependencies.

- cmake/: Find modules that provide stub targets when deps disabled
- stubs/include/: Header stubs for curl, git2, ngspice, OCC
- stubs/src/: Implementation stubs for disabled features
- patches/: Consolidated patch for KiCad source modifications
- scripts/: Build helper scripts

Verified full native build on macOS with all optional deps disabled.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Viktor Vaczi 2025-11-26 16:33:48 +01:00
commit f1526d8c24
23 changed files with 3688 additions and 13 deletions

162
scripts/build.sh Executable file
View file

@ -0,0 +1,162 @@
#!/bin/bash
#
# KiCad Wasm Port - Build Script
#
# This script builds KiCad with optional dependencies disabled.
#
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
KICAD_DIR="$PROJECT_ROOT/kicad"
BUILD_DIR="$PROJECT_ROOT/build"
CMAKE_MODULE_PATH="$PROJECT_ROOT/cmake"
STUBS_DIR="$PROJECT_ROOT/stubs/include"
# Default options
USE_CURL="${KICAD_USE_CURL:-OFF}"
USE_GIT="${KICAD_USE_GIT:-OFF}"
USE_OCC="${KICAD_USE_OCC:-OFF}"
USE_NGSPICE="${KICAD_USE_NGSPICE:-OFF}"
BUILD_TYPE="${CMAKE_BUILD_TYPE:-Release}"
PARALLEL_JOBS="${PARALLEL_JOBS:-$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)}"
show_help() {
echo "KiCad Wasm Port - Build Script"
echo ""
echo "Usage: $0 [options]"
echo ""
echo "Options:"
echo " --with-curl Enable CURL (network features)"
echo " --with-git Enable libgit2 (git integration)"
echo " --with-occ Enable OpenCASCADE (STEP/3D)"
echo " --with-ngspice Enable ngspice (SPICE simulation)"
echo " --debug Build in Debug mode"
echo " --clean Clean build directory first"
echo " -j N Number of parallel jobs (default: $PARALLEL_JOBS)"
echo " --configure-only Only run cmake, don't build"
echo " -h, --help Show this help"
echo ""
echo "Environment variables:"
echo " KICAD_USE_CURL=ON|OFF (default: OFF)"
echo " KICAD_USE_GIT=ON|OFF (default: OFF)"
echo " KICAD_USE_OCC=ON|OFF (default: OFF)"
echo " KICAD_USE_NGSPICE=ON|OFF (default: OFF)"
echo " CMAKE_BUILD_TYPE=Release|Debug"
echo ""
}
CLEAN_BUILD=0
CONFIGURE_ONLY=0
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--with-curl)
USE_CURL=ON
shift
;;
--with-git)
USE_GIT=ON
shift
;;
--with-occ)
USE_OCC=ON
shift
;;
--with-ngspice)
USE_NGSPICE=ON
shift
;;
--debug)
BUILD_TYPE=Debug
shift
;;
--clean)
CLEAN_BUILD=1
shift
;;
-j)
PARALLEL_JOBS="$2"
shift 2
;;
--configure-only)
CONFIGURE_ONLY=1
shift
;;
-h|--help)
show_help
exit 0
;;
*)
echo "Unknown option: $1"
show_help
exit 1
;;
esac
done
echo "=== KiCad Wasm Port - Build ==="
echo ""
echo "Configuration:"
echo " Build type: $BUILD_TYPE"
echo " CURL: $USE_CURL"
echo " Git: $USE_GIT"
echo " OpenCASCADE: $USE_OCC"
echo " ngspice: $USE_NGSPICE"
echo " Parallel jobs: $PARALLEL_JOBS"
echo ""
# Check if kicad exists
if [ ! -f "$KICAD_DIR/CMakeLists.txt" ]; then
echo "Error: KiCad source not found at $KICAD_DIR"
echo "Run: git submodule update --init --recursive"
exit 1
fi
# Clean if requested
if [ $CLEAN_BUILD -eq 1 ] && [ -d "$BUILD_DIR" ]; then
echo "Cleaning build directory..."
rm -rf "$BUILD_DIR"
fi
# Create build directory
mkdir -p "$BUILD_DIR"
cd "$BUILD_DIR"
# Additional include paths for stubs
EXTRA_INCLUDES=""
if [ "$USE_CURL" = "OFF" ] || [ "$USE_GIT" = "OFF" ]; then
EXTRA_INCLUDES="-DCMAKE_CXX_FLAGS=-I$STUBS_DIR"
fi
# Configure
echo "Running CMake..."
cmake "$KICAD_DIR" \
-DCMAKE_BUILD_TYPE="$BUILD_TYPE" \
-DCMAKE_MODULE_PATH="$CMAKE_MODULE_PATH" \
-DKICAD_USE_CURL="$USE_CURL" \
-DKICAD_USE_GIT="$USE_GIT" \
-DKICAD_USE_OCC="$USE_OCC" \
-DKICAD_USE_NGSPICE="$USE_NGSPICE" \
-DKICAD_SCRIPTING_WXPYTHON=OFF \
-DKICAD_BUILD_QA_TESTS=OFF \
-DKICAD_BUILD_I18N=OFF \
$EXTRA_INCLUDES
if [ $CONFIGURE_ONLY -eq 1 ]; then
echo ""
echo "Configuration complete. Run 'make -j$PARALLEL_JOBS' in $BUILD_DIR to build."
exit 0
fi
# Build
echo ""
echo "Building..."
cmake --build . --parallel "$PARALLEL_JOBS"
echo ""
echo "=== Build Complete ==="
echo ""
echo "Binaries are in: $BUILD_DIR"

120
scripts/prepare.sh Executable file
View file

@ -0,0 +1,120 @@
#!/bin/bash
#
# KiCad Wasm Port - Prepare Script
#
# This script applies patches to the KiCad source code to enable
# building with optional dependencies disabled.
#
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
KICAD_DIR="$PROJECT_ROOT/kicad"
PATCHES_DIR="$PROJECT_ROOT/patches"
echo "=== KiCad Wasm Port - Prepare ==="
echo "Project root: $PROJECT_ROOT"
echo "KiCad source: $KICAD_DIR"
echo ""
# Check if kicad submodule is initialized
if [ ! -f "$KICAD_DIR/CMakeLists.txt" ]; then
echo "Error: KiCad submodule not initialized."
echo "Run: git submodule update --init --recursive"
exit 1
fi
# Check if patches directory exists
if [ ! -d "$PATCHES_DIR" ]; then
echo "Error: Patches directory not found: $PATCHES_DIR"
exit 1
fi
# Function to check if a patch is already applied
patch_already_applied() {
local patch_file="$1"
# Try to reverse-apply the patch (dry run)
# If it succeeds, the patch is already applied
cd "$KICAD_DIR"
if git apply --reverse --check "$patch_file" 2>/dev/null; then
return 0
else
return 1
fi
}
# Function to apply a patch
apply_patch() {
local patch_file="$1"
local patch_name="$(basename "$patch_file")"
echo "Checking patch: $patch_name"
if patch_already_applied "$patch_file"; then
echo " -> Already applied, skipping"
return 0
fi
echo " -> Applying..."
cd "$KICAD_DIR"
# Try to apply the patch
if git apply --check "$patch_file" 2>/dev/null; then
git apply "$patch_file"
echo " -> Success"
else
echo " -> Warning: Patch may not apply cleanly, trying with -3 (three-way merge)"
if git apply -3 "$patch_file" 2>/dev/null; then
echo " -> Success with three-way merge"
else
echo " -> Error: Patch failed to apply"
echo " You may need to manually resolve conflicts or update the patch"
return 1
fi
fi
}
# Apply all patches in order
echo "Applying patches..."
echo ""
patch_count=0
patch_failed=0
for patch_file in "$PATCHES_DIR"/*.patch; do
if [ -f "$patch_file" ]; then
if apply_patch "$patch_file"; then
((patch_count++))
else
((patch_failed++))
fi
echo ""
fi
done
echo "=== Summary ==="
echo "Patches applied: $patch_count"
echo "Patches failed: $patch_failed"
if [ $patch_failed -gt 0 ]; then
echo ""
echo "Some patches failed to apply. Please check the errors above."
exit 1
fi
echo ""
echo "=== Next Steps ==="
echo ""
echo "Build with optional dependencies disabled:"
echo ""
echo " mkdir build && cd build"
echo " cmake ../kicad \\"
echo " -DKICAD_USE_CURL=OFF \\"
echo " -DKICAD_USE_GIT=OFF \\"
echo " -DKICAD_USE_OCC=OFF \\"
echo " -DKICAD_USE_NGSPICE=OFF \\"
echo " -DCMAKE_PREFIX_PATH=\"\$(pwd)/../stubs/include\""
echo " make -j\$(nproc)"
echo ""
echo "Done!"

137
scripts/update-kicad.sh Executable file
View file

@ -0,0 +1,137 @@
#!/bin/bash
#
# KiCad Wasm Port - Update KiCad Submodule
#
# This script updates the KiCad submodule to a new version and
# helps regenerate patches if needed.
#
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
KICAD_DIR="$PROJECT_ROOT/kicad"
PATCHES_DIR="$PROJECT_ROOT/patches"
echo "=== KiCad Wasm Port - Update KiCad ==="
echo ""
# Check if kicad submodule exists
if [ ! -d "$KICAD_DIR/.git" ] && [ ! -f "$KICAD_DIR/.git" ]; then
echo "Error: KiCad submodule not initialized."
echo "Run: git submodule update --init --recursive"
exit 1
fi
# Parse arguments
TARGET_REF="${1:-}"
FORCE_UPDATE=0
if [ "$1" = "--force" ] || [ "$2" = "--force" ]; then
FORCE_UPDATE=1
fi
show_usage() {
echo "Usage: $0 [<ref>] [--force]"
echo ""
echo "Arguments:"
echo " <ref> Git reference (branch, tag, or commit) to update to"
echo " Examples: 8.0, master, v8.0.0, abc1234"
echo " --force Force update even if patches need manual intervention"
echo ""
echo "If no ref is provided, shows current KiCad version."
}
# Show current version
show_current_version() {
cd "$KICAD_DIR"
echo "Current KiCad version:"
echo " Branch: $(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo 'detached')"
echo " Commit: $(git rev-parse --short HEAD)"
echo " Date: $(git log -1 --format=%ci)"
# Check if there are local modifications
if ! git diff --quiet; then
echo ""
echo "Warning: There are uncommitted changes in the KiCad directory"
git status --short
fi
}
# Revert patches before update
revert_patches() {
echo "Reverting existing patches..."
cd "$KICAD_DIR"
for patch_file in "$PATCHES_DIR"/*.patch; do
if [ -f "$patch_file" ]; then
patch_name="$(basename "$patch_file")"
# Try to reverse the patch
if git apply --reverse --check "$patch_file" 2>/dev/null; then
echo " Reverting: $patch_name"
git apply --reverse "$patch_file"
else
echo " Skipping (not applied): $patch_name"
fi
fi
done
}
# Update submodule
update_submodule() {
local ref="$1"
echo ""
echo "Updating KiCad to: $ref"
cd "$KICAD_DIR"
# Fetch latest
echo "Fetching from origin..."
git fetch origin
# Checkout the target ref
echo "Checking out $ref..."
git checkout "$ref"
# If it's a branch, pull latest
if git rev-parse --verify "origin/$ref" >/dev/null 2>&1; then
echo "Pulling latest from origin/$ref..."
git pull origin "$ref"
fi
echo ""
show_current_version
}
# Main logic
if [ -z "$TARGET_REF" ] || [ "$TARGET_REF" = "--force" ]; then
show_current_version
echo ""
show_usage
exit 0
fi
echo "Preparing to update KiCad..."
echo ""
# Show current version
show_current_version
echo ""
# Revert patches first
revert_patches
echo ""
# Update submodule
update_submodule "$TARGET_REF"
echo ""
# Re-apply patches
echo "Re-applying patches..."
"$SCRIPT_DIR/prepare.sh"
echo ""
echo "=== Update Complete ==="
echo ""
echo "Don't forget to test the build:"
echo " cd build && cmake --build . --clean-first"