Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,029 changes: 116 additions & 913 deletions CMakeLists.txt

Large diffs are not rendered by default.

3,206 changes: 32 additions & 3,174 deletions README.md

Large diffs are not rendered by default.

78 changes: 7 additions & 71 deletions build.ps1
Original file line number Diff line number Diff line change
@@ -1,86 +1,22 @@
param(
[ValidateSet("native", "windows-x64")]
[string] $Target = "native",
[ValidateSet("debug", "release", "production", "sanitize")]
[ValidateSet("debug", "release", "sanitize")]
[string] $Configuration = "release",
[switch] $WarningsAsErrors,
[switch] $Test,
[switch] $Dist,
[switch] $Beta
[switch] $Test
)

$ErrorActionPreference = "Stop"

$cmakeCommand = Get-Command cmake -ErrorAction SilentlyContinue
$cmakePath = if ($cmakeCommand) { $cmakeCommand.Source } else { $null }
if (-not $cmakePath -and $env:VSINSTALLDIR) {
$candidate = Join-Path $env:VSINSTALLDIR "Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe"
if (Test-Path $candidate) { $cmakePath = $candidate }
}
if (-not $cmakePath) {
foreach ($version in "18", "17") {
foreach ($edition in "Community", "Professional", "Enterprise", "BuildTools") {
$candidate = Join-Path ${env:ProgramFiles} "Microsoft Visual Studio\$version\$edition\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe"
if (Test-Path $candidate) { $cmakePath = $candidate; break }
}
if ($cmakePath) { break }
}
}
if (-not $cmakePath) { throw "CMake was not found. Install Visual Studio C++ CMake tools or add cmake to PATH." }

# CMake is the only supported native build graph. It owns SentencePiece and
# the generated TokenId vocabulary, avoiding a stale direct compiler list.
if ($Target -notin @("native", "windows-x64")) {
throw "Unsupported target '$Target': provide a CMake toolchain/preset for SentencePiece cross-compilation."
}
if (-not $env:FELIDAE_LIBTORCH_PATH) {
throw "Windows builds require FELIDAE_LIBTORCH_PATH to name the provisioned CPU LibTorch installation."
}

$cmake = (Get-Command cmake -ErrorAction Stop).Source
$root = Split-Path -Parent $MyInvocation.MyCommand.Path
$buildType = if ($Configuration -eq "debug" -or $Configuration -eq "sanitize") { "Debug" } else { "Release" }
$buildDir = if ($env:FELIDAE_BUILD_DIR) { $env:FELIDAE_BUILD_DIR } else { Join-Path $root "build\windows-x64\$($buildType.ToLowerInvariant())" }
$configureArgs = @(
"-S", $root,
"-B", $buildDir,
"-DCMAKE_BUILD_TYPE=$buildType"
)
if ($Test) {
$configureArgs += "-DFELIDAE_BUILD_TESTS=ON"
}
if ($WarningsAsErrors) {
$configureArgs += "-DFELIDAE_ENABLE_STRICT_WARNINGS=ON"
}
if ($env:FELIDAE_LIBTORCH_PATH) {
$configureArgs += "-DCMAKE_PREFIX_PATH=$env:FELIDAE_LIBTORCH_PATH"
}
if ($env:FELIDAE_VERSION) {
$configureArgs += "-DFELIDAE_VERSION=$env:FELIDAE_VERSION"
}
if ($Configuration -eq "sanitize") {
$configureArgs += "-DFELIDAE_ENABLE_SANITIZERS=ON"
}
$buildDir = Join-Path $root "build\windows-x64\$($buildType.ToLowerInvariant())"

& $cmakePath @configureArgs
& $cmake -S $root -B $buildDir "-DCMAKE_BUILD_TYPE=$buildType" "-DFELIDAE_BUILD_TESTS=$($Test.IsPresent)"
if ($LASTEXITCODE -ne 0) { throw "CMake configure failed" }

$jobs = if ($env:FELIDAE_JOBS) { $env:FELIDAE_JOBS } else { "1" }
$targets = if ($Beta) { @("felidae_beta") } elseif ($Dist) { @("felidae_dist") } elseif ($Test) { @("felidae_compiler", "felidae_vm", "felidae_debugger", "felidae_tests") } else { @("felidae_compiler", "felidae_vm", "felidae_debugger") }
& $cmakePath --build $buildDir --config $buildType --target $targets --parallel $jobs
if ($LASTEXITCODE -ne 0) { throw "CMake build failed" }
& $cmake --build $buildDir --config $buildType --target felidae --parallel 1
if ($LASTEXITCODE -ne 0) { throw "Felidae interpreter build failed" }
if ($Test) {
& ctest --test-dir $buildDir --build-config $buildType --output-on-failure
if ($LASTEXITCODE -ne 0) { throw "CTest failed" }
}

if ($Dist -or $Beta) {
foreach ($executable in @("felidae_compiler.exe", "felidae_vm.exe", "felidae_debugger.exe")) {
$path = Join-Path $buildDir "dist\bin\$executable"
if (-not (Test-Path $path)) {
throw "Distribution staging completed without expected executable: $path"
}
}
}
if (($Dist -or $Beta) -and -not (Test-Path (Join-Path $buildDir "dist\models\felidae.model"))) {
throw "Distribution staging completed without dist\models\felidae.model"
}
210 changes: 28 additions & 182 deletions build.sh
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,205 +1,51 @@
#!/usr/bin/env bash
set -euo pipefail

# User configuration. Environment variables or command-line options may
# override these values without creating another build script.
MODE="${FELIDAE_MODE:-test}" # test | release
ENABLE_TRAINING="${FELIDAE_ENABLE_TRAINING:-OFF}" # ON | OFF
ENABLE_LIBTORCH="${FELIDAE_ENABLE_LIBTORCH:-auto}" # auto | ON | OFF
PLATFORM="${FELIDAE_PLATFORM:-auto}" # auto | linux | macos | windows | android | generic
ARCHITECTURE="${FELIDAE_ARCH:-auto}" # auto | x86_64 | arm64 | armv7
ANDROID_API="${FELIDAE_ANDROID_API:-24}"
JOBS="${FELIDAE_JOBS:-auto}"
SANITIZE=0

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Direct-interpreter build entry point. It has no compiler, VM, model
# training, RocksDB, or external runtime options.
MODE="debug"
RUN_TESTS=0
JOBS=""

usage() {
echo "usage: ./build.sh [--mode test|release] [--training ON|OFF] [--libtorch ON|OFF]"
echo " [--platform auto|linux|macos|windows|android|generic]"
echo " [--arch auto|x86_64|arm64|armv7] [--android-api N]"
echo " [--jobs N|auto] [--sanitize]"
}

normalize_boolean() {
case "${1,,}" in
1|on|true|yes) echo ON ;;
0|off|false|no) echo OFF ;;
*) echo "Invalid boolean value: $1" >&2; exit 2 ;;
esac
echo "usage: ./build.sh [debug|release|sanitize] [--test] [--jobs N]"
}

while [[ $# -gt 0 ]]; do
case "$1" in
--mode) MODE="${2:-}"; shift 2 ;;
--training) ENABLE_TRAINING="${2:-}"; shift 2 ;;
--libtorch) ENABLE_LIBTORCH="${2:-}"; shift 2 ;;
--platform) PLATFORM="${2:-}"; shift 2 ;;
--arch) ARCHITECTURE="${2:-}"; shift 2 ;;
--android-api) ANDROID_API="${2:-}"; shift 2 ;;
--jobs) JOBS="${2:-}"; shift 2 ;;
--sanitize) SANITIZE=1; shift ;;
--configuration)
case "${2:-}" in
debug) MODE=test ;;
release|production) MODE=release ;;
sanitize) MODE=test; SANITIZE=1 ;;
*) echo "Unknown configuration: ${2:-}" >&2; exit 2 ;;
esac
debug|release|sanitize) MODE="$1"; shift ;;
--test) RUN_TESTS=1; shift ;;
--jobs)
JOBS="${2:-}"
[[ "$JOBS" =~ ^[1-9][0-9]*$ ]] || { echo "--jobs expects a positive integer" >&2; exit 2; }
shift 2
;;
test|debug) MODE=test; shift ;;
release|production) MODE=release; shift ;;
sanitize) MODE=test; SANITIZE=1; shift ;;
--help|-h) usage; exit 0 ;;
--warnings-as-errors|native) shift ;;
--target)
if [[ "${2:-}" != "native" ]]; then
echo "Only the CMake native target is supported" >&2
exit 2
fi
shift 2
;;
*) echo "Unknown build option: $1" >&2; usage >&2; exit 2 ;;
*) echo "Unknown option: $1" >&2; usage >&2; exit 2 ;;
esac
done

case "${MODE,,}" in
test) MODE=test; CONFIGURATION=Debug; BUILD_TESTS=ON ;;
release) MODE=release; CONFIGURATION=Release; BUILD_TESTS=OFF ;;
*) echo "Invalid MODE '$MODE'; expected test or release" >&2; exit 2 ;;
esac

if [[ "${PLATFORM,,}" == auto ]]; then
case "$(uname -s)" in
Linux) PLATFORM=linux ;;
Darwin) PLATFORM=macos ;;
MINGW*|MSYS*|CYGWIN*) PLATFORM=windows ;;
*) PLATFORM=generic ;;
esac
else
PLATFORM="${PLATFORM,,}"
fi
case "$PLATFORM" in linux|macos|windows|android|generic) ;; *) echo "Invalid platform '$PLATFORM'" >&2; exit 2 ;; esac

if [[ "${ARCHITECTURE,,}" == auto ]]; then
case "$(uname -m)" in
x86_64|amd64) ARCHITECTURE=x86_64 ;;
arm64|aarch64) ARCHITECTURE=arm64 ;;
armv7*|armhf) ARCHITECTURE=armv7 ;;
*) ARCHITECTURE="$(uname -m)" ;;
esac
else
ARCHITECTURE="${ARCHITECTURE,,}"
fi
case "$(uname -m)" in
x86_64|amd64) HOST_ARCHITECTURE=x86_64 ;;
arm64|aarch64) HOST_ARCHITECTURE=arm64 ;;
armv7*|armhf) HOST_ARCHITECTURE=armv7 ;;
*) HOST_ARCHITECTURE="$(uname -m)" ;;
case "$MODE" in
debug) BUILD_TYPE=Debug; BUILD_DIR=build/debug; SANITIZERS=OFF ;;
release) BUILD_TYPE=Release; BUILD_DIR=build/release; SANITIZERS=OFF ;;
sanitize) BUILD_TYPE=Debug; BUILD_DIR=build/asan; SANITIZERS=ON ;;
esac

ENABLE_TRAINING="$(normalize_boolean "$ENABLE_TRAINING")"
if [[ "${ENABLE_LIBTORCH,,}" == auto ]]; then
if [[ "$PLATFORM" == android ]]; then
ENABLE_LIBTORCH=OFF
else
ENABLE_LIBTORCH=ON
fi
else
ENABLE_LIBTORCH="$(normalize_boolean "$ENABLE_LIBTORCH")"
fi
if [[ "${JOBS,,}" == auto ]]; then
if command -v getconf >/dev/null 2>&1; then
JOBS="$(getconf _NPROCESSORS_ONLN)"
elif command -v sysctl >/dev/null 2>&1; then
JOBS="$(sysctl -n hw.ncpu)"
else
JOBS=1
fi
fi
if [[ ! "$JOBS" =~ ^[1-9][0-9]*$ ]]; then
echo "Invalid job count '$JOBS'; expected auto or a positive integer" >&2
exit 2
fi
if [[ "$ENABLE_TRAINING" == ON && "$ENABLE_LIBTORCH" != ON ]]; then
echo "Training requires LibTorch; set ENABLE_LIBTORCH=ON" >&2
exit 2
fi

if [[ "$PLATFORM" == linux && "$ARCHITECTURE" == "$HOST_ARCHITECTURE" ]]; then
DEFAULT_BUILD_DIR="${ROOT_DIR}/build/${MODE}"
else
DEFAULT_BUILD_DIR="${ROOT_DIR}/build/${PLATFORM}-${ARCHITECTURE}/${MODE}"
fi
BUILD_DIR="${FELIDAE_BUILD_DIR:-$DEFAULT_BUILD_DIR}"
BUILD_TESTS=OFF
if [[ "$RUN_TESTS" -eq 1 ]]; then BUILD_TESTS=ON; fi
CMAKE_ARGS=(
-S "$ROOT_DIR"
-S .
-B "$BUILD_DIR"
"-DCMAKE_BUILD_TYPE=${CONFIGURATION}"
"-DFELIDAE_BUILD_TESTS=${BUILD_TESTS}"
"-DFELIDAE_ENABLE_LIBTORCH=${ENABLE_LIBTORCH}"
"-DFELIDAE_ENABLE_TRAINING=${ENABLE_TRAINING}"
"-DCMAKE_BUILD_TYPE=$BUILD_TYPE"
"-DFELIDAE_BUILD_TESTS=$BUILD_TESTS"
"-DFELIDAE_ENABLE_SANITIZERS=$SANITIZERS"
)
CAN_RUN_TESTS=1
case "$PLATFORM" in
macos)
case "$ARCHITECTURE" in
arm64) CMAKE_ARGS+=("-DCMAKE_OSX_ARCHITECTURES=arm64") ;;
x86_64) CMAKE_ARGS+=("-DCMAKE_OSX_ARCHITECTURES=x86_64") ;;
*) echo "Unsupported macOS architecture '$ARCHITECTURE'" >&2; exit 2 ;;
esac
;;
android)
NDK_DIR="${ANDROID_NDK_HOME:-${ANDROID_NDK_ROOT:-}}"
if [[ -z "$NDK_DIR" || ! -f "$NDK_DIR/build/cmake/android.toolchain.cmake" ]]; then
echo "Android builds require ANDROID_NDK_HOME (or ANDROID_NDK_ROOT)" >&2
exit 2
fi
case "$ARCHITECTURE" in
arm64) ANDROID_ABI=arm64-v8a ;;
armv7) ANDROID_ABI=armeabi-v7a ;;
x86_64) ANDROID_ABI=x86_64 ;;
*) echo "Unsupported Android architecture '$ARCHITECTURE'" >&2; exit 2 ;;
esac
CMAKE_ARGS+=(
"-DCMAKE_TOOLCHAIN_FILE=${NDK_DIR}/build/cmake/android.toolchain.cmake"
"-DANDROID_ABI=${ANDROID_ABI}"
"-DANDROID_PLATFORM=android-${ANDROID_API}"
)
CAN_RUN_TESTS=0
;;
esac
if [[ -n "${FELIDAE_TOOLCHAIN_FILE:-}" ]]; then
CMAKE_ARGS+=("-DCMAKE_TOOLCHAIN_FILE=${FELIDAE_TOOLCHAIN_FILE}")
CAN_RUN_TESTS=0
fi
if [[ "$ENABLE_LIBTORCH" == ON ]]; then
if [[ -z "${FELIDAE_LIBTORCH_PATH:-}" ]]; then
echo "LibTorch builds require FELIDAE_LIBTORCH_PATH to name the provisioned CPU LibTorch installation" >&2
exit 2
fi
CMAKE_ARGS+=("-DCMAKE_PREFIX_PATH=${FELIDAE_LIBTORCH_PATH}")
fi
if [[ -n "${FELIDAE_VERSION:-}" ]]; then
CMAKE_ARGS+=("-DFELIDAE_VERSION=${FELIDAE_VERSION}")
fi
if [[ "$SANITIZE" -eq 1 ]]; then
CMAKE_ARGS+=("-DFELIDAE_ENABLE_SANITIZERS=ON")
fi

echo "Felidae platform=${PLATFORM} arch=${ARCHITECTURE} mode=${MODE} configuration=${CONFIGURATION} training=${ENABLE_TRAINING} libtorch=${ENABLE_LIBTORCH} jobs=${JOBS}"
cmake "${CMAKE_ARGS[@]}"

if [[ "$MODE" == test ]]; then
cmake --build "$BUILD_DIR" --config "$CONFIGURATION" \
--target felidae_compiler felidae_vm felidae_debugger felidae_tests --parallel "$JOBS"
if [[ "$CAN_RUN_TESTS" -eq 1 ]]; then
ctest --test-dir "$BUILD_DIR" --build-config "$CONFIGURATION" --output-on-failure
else
echo "Cross-compiled tests were built but not executed on the host"
fi
else
cmake --build "$BUILD_DIR" --config "$CONFIGURATION" \
--target felidae_dist --parallel "$JOBS"
BUILD_ARGS=(--build "$BUILD_DIR" --target felidae)
if [[ -n "$JOBS" ]]; then BUILD_ARGS+=(--parallel "$JOBS"); fi
cmake "${BUILD_ARGS[@]}"

if [[ "$RUN_TESTS" -eq 1 ]]; then
ctest --test-dir "$BUILD_DIR" --output-on-failure
fi
Loading
Loading