mirror of
https://github.com/bherbruck/solidcraft.git
synced 2026-11-08 13:01:52 +00:00
73bf876d80
Keep CMake arguments nonempty when Ninja is absent, and check CMake before creating or cleaning output. Add hermetic script regressions on Linux and macOS. Co-Authored-By: GPT-6-Astra <noreply@openai.com>
91 lines
3.5 KiB
Bash
Executable File
91 lines
3.5 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
|
|
|
|
if ! command -v cmake >/dev/null 2>&1; then
|
|
echo "cmake is required (version 3.20 or newer); install it before building the C++ reference." >&2
|
|
exit 1
|
|
fi
|
|
|
|
[ "$CLEAN" = 1 ] && rm -rf "$BUILD"
|
|
mkdir -p "$BUILD" "$OUT/lib"
|
|
|
|
# Bash 3.2 treats an empty array as unset under set -u, so keep required args here.
|
|
CMAKE_ARGS=(-S "$HERE" -B "$BUILD")
|
|
if command -v ninja >/dev/null 2>&1; then CMAKE_ARGS+=(-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 "${CMAKE_ARGS[@]}" \
|
|
-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"
|