@echo off setlocal enabledelayedexpansion :: ============================================================================ :: build.bat :: ============================================================================ :: Get script directory (project root) set SCRIPT_DIR=%~dp0 set SCRIPT_DIR=%SCRIPT_DIR:~0,-1% :: --------------------------------------------------------------------------- :: Default values :: --------------------------------------------------------------------------- set BUILD_TYPE=Release set CLEAN_BUILD=0 :: -------------------------------------------------------------------------- :: Parse arguments :: -------------------------------------------------------------------------- :parse_args if "%~1"=="" goto :main if /i "%~1"=="Release" ( set BUILD_TYPE=Release shift goto :parse_args ) if /i "%~1"=="Debug" ( set BUILD_TYPE=Debug shift goto :parse_args ) if /i "%~1"=="clean" ( set CLEAN_BUILD=1 shift goto :parse_args ) echo Unknown argument: %~1 echo Usage: build [Release^|Debug] [clean] exit /b 1 :main set BUILD_DIR=%SCRIPT_DIR%\build\windows\%BUILD_TYPE% echo === Building filehasher (%BUILD_TYPE%) === :: -------------------------------------------------------------------------- :: Clean if requested :: -------------------------------------------------------------------------- if %CLEAN_BUILD%==1 ( echo Cleaning... if exist "%BUILD_DIR%" rmdir /s /q "%BUILD_DIR%" 2>nul ) :: -------------------------------------------------------------------------- :: Create build directory :: -------------------------------------------------------------------------- if not exist "%BUILD_DIR%" mkdir "%BUILD_DIR%" pushd "%BUILD_DIR%" :: -------------------------------------------------------------------------- :: Compiler selection :: -------------------------------------------------------------------------- set CC= where clang-cl >nul 2>&1 if !ERRORLEVEL! equ 0 ( echo Compiler: clang-cl ^(preferred^) set "CC=-DCMAKE_C_COMPILER=clang-cl" goto :find_generator ) where gcc >nul 2>&1 if !ERRORLEVEL! equ 0 ( echo Compiler: gcc ^(fallback^) set "CC=-DCMAKE_C_COMPILER=gcc" goto :find_generator ) where clang >nul 2>&1 if !ERRORLEVEL! equ 0 ( echo Compiler: clang ^(last resort^) set "CC=-DCMAKE_C_COMPILER=clang" goto :find_generator ) echo ERROR: No suitable compiler found! popd exit /b 1 :: -------------------------------------------------------------------------- :: Generator selection (prefer ninja) :: -------------------------------------------------------------------------- :find_generator set GEN= where ninja >nul 2>&1 if !ERRORLEVEL! equ 0 ( echo Generator: Ninja set "GEN=-G Ninja" ) else ( echo Generator: Default ) :: -------------------------------------------------------------------------- :: Configure :: -------------------------------------------------------------------------- echo. echo Configuring CMake... :: -------------------------------------------------------------------------- :: compile_commands.json logic :: -------------------------------------------------------------------------- set EXPORT_COMPILE_COMMANDS=OFF if /i "%BUILD_TYPE%"=="Release" ( if exist "%SCRIPT_DIR%\compile_commands.json" ( echo compile_commands.json already exists - skipping generation ) else ( echo compile_commands.json will be generated set EXPORT_COMPILE_COMMANDS=ON ) ) set CMD=cmake "%SCRIPT_DIR%" %GEN% %CC% -DCMAKE_BUILD_TYPE=%BUILD_TYPE% -DCMAKE_EXPORT_COMPILE_COMMANDS=%EXPORT_COMPILE_COMMANDS% echo !CMD! !CMD! if !ERRORLEVEL! neq 0 ( echo ERROR: Configuration failed popd exit /b 1 ) :: -------------------------------------------------------------------------- :: Build :: -------------------------------------------------------------------------- echo. echo Building... cmake --build . --config %BUILD_TYPE% if !ERRORLEVEL! neq 0 ( echo ERROR: Build failed popd exit /b 1 ) :: -------------------------------------------------------------------------- :: Copy compile_commands.json (only if generated) :: -------------------------------------------------------------------------- if /i "%EXPORT_COMPILE_COMMANDS%"=="ON" ( if exist "compile_commands.json" ( echo. echo clangd: compile_commands.json generated copy /Y "compile_commands.json" "%SCRIPT_DIR%\compile_commands.json" >nul 2>&1 if !ERRORLEVEL! equ 0 ( echo clangd: Copied to project root ) else ( echo clangd: Copy failed ) ) ) popd echo. echo === Build Complete === echo Executable: %BUILD_DIR%\filehasher.exe