#!/usr/bin/env bash
# Configure and compile the project (Release by default).
# Usage:
# ./scripts/build.sh # Release → ./build
# ./scripts/build.sh debug # Debug → ./build-debug
# ./scripts/build.sh release ../my-build # custom build directory
set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"
build_type="Release"
build_dir="${ROOT}/build"
if [[ "${1:-}" == "debug" || "${1:-}" == "Debug" ]]; then
build_type="Debug"
build_dir="${ROOT}/build-debug"
shift || true
fi
if [[ -n "${1:-}" ]]; then
build_dir="$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"
shift || true
fi
cmake -S "$ROOT" -B "$build_dir" -DCMAKE_BUILD_TYPE="$build_type"
cmake --build "$build_dir" -j"$(sysctl -n hw.ncpu 2>/dev/null || nproc 2>/dev/null || echo 4)"
echo "Build tree: $build_dir"