Files
solvecraft/harness/cpp/build.sh
T
bherbruck baca790471 harness/cpp: key the CMake build dir by source tree
build.sh used $OUT/build for every source tree, so a worktree build with
SOLIDCORE_CPP_DIR pointing at the main tree's bin/cpp hit CMake's
"source does not match" error. The build tree is now
$OUT/build-<sha256(harness path)[:8]> (a legacy $OUT/build configured for
the same source is reused); lib/, ffi.env and compile_commands.json stay
at fixed paths for solidcore-ffi. Header documents SOLIDCORE_CPP_DIR;
AGENTS.md section 2 says to build from the main tree.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01V8ohWGLCzFPoZriCeSSero
2026-10-13 15:08:22 -04:00

85 lines
3.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Build the upstream SMLib/SM_API C++ reference kernel (and its test apps)
# into $OUT: $SOLIDCORE_CPP_DIR if set (the same override solidcore-ffi honours),
# else <repo>/bin/cpp. See plan/baseline.md.
#
# Usage: harness/cpp/build.sh [--no-tests] [--jobs N] [--clean]
#
# Run it from the main tree; git worktrees reuse that build by exporting
# SOLIDCORE_CPP_DIR=<main tree>/bin/cpp. Running it from a worktree with that override
# is also safe: the CMake build tree is keyed by this harness's source path, so each
# source tree gets its own build dir and CMake never sees a mismatched source.
#
# Outputs (in $OUT):
# build-<hash>/ CMake/ninja build tree; <hash> = first 8 hex of the
# sha256 of this harness's absolute path. A legacy
# build/ whose CMake source is this harness is reused.
# compile_commands.json copied right after configure
# ffi.env settings for crates/solidcore-ffi/build.rs
# lib/libSMLib.a static libs (link order: SM_API then SMLib)
# lib/libSM_API.a
# tests/SM_API_test_app, tests/prog_test_app
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "$HERE/../.." && pwd)"
OUT="${SOLIDCORE_CPP_DIR:-$ROOT/bin/cpp}" # same override solidcore-ffi honours
# One CMake build tree per source tree (see header). Installed libs, ffi.env and
# compile_commands.json stay at fixed paths in $OUT so solidcore-ffi finds them.
SRC_KEY="$(printf '%s' "$HERE" | sha256sum | cut -c1-8)"
BUILD="$OUT/build-$SRC_KEY"
LEGACY="$OUT/build"
if [ ! -d "$BUILD" ] && [ -f "$LEGACY/CMakeCache.txt" ] \
&& grep -qxF "CMAKE_HOME_DIRECTORY:INTERNAL=$HERE" "$LEGACY/CMakeCache.txt"; then
BUILD="$LEGACY"
fi
JOBS=6
TESTS=ON
CLEAN=0
while [ $# -gt 0 ]; do
case "$1" in
--no-tests) TESTS=OFF ;;
--jobs) JOBS="$2"; shift ;;
--clean) CLEAN=1 ;;
*) echo "unknown argument: $1" >&2; exit 2 ;;
esac
shift
done
[ "$CLEAN" = 1 ] && rm -rf "$BUILD"
mkdir -p "$BUILD" "$OUT/lib"
GEN=()
if command -v ninja >/dev/null 2>&1; then GEN=(-G Ninja); fi
# g++ (GNU) is the reference compiler; override with CXX=... if needed.
export CXX="${CXX:-g++}"
start=$(date +%s)
nice -n 10 cmake -S "$HERE" -B "$BUILD" "${GEN[@]}" \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX="$OUT" \
-DSMLIB_UPSTREAM="$ROOT/upstream" \
-DSC_BUILD_TESTS="$TESTS"
cp -f "$BUILD/compile_commands.json" "$OUT/compile_commands.json"
echo "compile_commands.json -> $OUT/compile_commands.json"
# Settings consumed by crates/solidcore-ffi/build.rs (format documented there).
# Must match the flags in CMakeLists.txt so shims see the same ABI/defines.
cat > "$OUT/ffi.env" <<ENV
# @generated by harness/cpp/build.sh -- do not edit
INCLUDE_DIRS=upstream/source/SM_API/inc upstream/source/SMLib/inc
DEFINES=NDEBUG NOMINMAX SM_GRAPHICS_CALLBACKS=1 _GLIBCXX_USE_CXX11_ABI=1
CXXFLAGS=-std=c++17 -O2 -ffp-contract=off -fno-fast-math -fPIC -w
LIBS=SM_API SMLib
# No TBB: upstream TBB code is only compiled under SM_USE_TBB, which is never defined.
SYSLIBS=pthread
ENV
echo "ffi.env -> $OUT/ffi.env"
nice -n 10 cmake --build "$BUILD" --parallel "$JOBS"
cmake --install "$BUILD" >/dev/null
end=$(date +%s)
echo "C++ build finished in $((end - start)) s; libs in $OUT/lib"