geopro/build.bat

116 lines
4.4 KiB
Batchfile

@echo off
REM ============================================================
REM geopro build helper (Windows / MSVC + Ninja, CMake presets)
REM
REM Usage: build [app | all | test | run | rebuild | configure]
REM app (default) build target geopro_desktop (incremental)
REM all build all targets (incremental)
REM test build + run unit tests via ctest
REM run incremental build + launch geopro_desktop
REM rebuild FORCE clean rebuild (--clean-first) + launch - use when
REM incremental seems stale / changes not showing up
REM configure force re-run CMake configure (after CMakeLists changes)
REM
REM Requires: Visual Studio 2022/2026 (Desktop C++ workload, ships
REM CMake + Ninja) and the VCPKG_ROOT environment variable.
REM Note: cmake/ninja/cl are NOT on PATH on this machine; this script
REM locates VS via vswhere and activates the MSVC env itself.
REM ============================================================
setlocal
set "ROOT=%~dp0"
set "BUILDDIR=%ROOT%build\debug"
set "PRESET=msvc-debug"
REM Locate VS-bundled CMake. Prefer vswhere; fall back to known VS2022 Community path.
set "CMAKE="
set "CTEST="
if exist "%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" (
for /f "usebackq tokens=*" %%i in (`"%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath`) do (
if exist "%%i\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" (
set "CMAKE=%%i\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe"
set "CTEST=%%i\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\ctest.exe"
)
)
)
if "%CMAKE%"=="" (
if exist "D:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe" (
set "CMAKE=D:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe"
set "CTEST=D:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin\ctest.exe"
)
)
if "%CMAKE%"=="" (
echo [build] ERROR: Could not find cmake.exe. Ensure Visual Studio 2022/2026 with C++ workload is installed.
exit /b 1
)
REM Activate MSVC environment so ninja/cl are available.
if exist "D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" (
call "D:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" >nul
)
if /i "%~1"=="release" (
set "BUILDDIR=%ROOT%build\release"
set "PRESET=msvc-release"
shift
)
REM --- environment guardrails (fail loud & early, before cmake configure) ---
if not defined QT_ROOT (
echo [build] QT_ROOT is not set. Run: setx QT_ROOT "D:\Qt\6.11.1\msvc2022_64" ^(see docs/ENV_SETUP_Windows.md^), then reopen the terminal.
exit /b 1
)
if not defined VCPKG_ROOT (
echo [build] VCPKG_ROOT is not set. Run: setx VCPKG_ROOT "C:\dev\vcpkg" ^(see docs/ENV_SETUP_Windows.md^), then reopen the terminal.
exit /b 1
)
set "CMD=%~1"
if "%CMD%"=="" set "CMD=app"
if /i "%CMD%"=="configure" goto :configure
if /i "%CMD%"=="app" goto :app
if /i "%CMD%"=="all" goto :all
if /i "%CMD%"=="test" goto :test
if /i "%CMD%"=="run" goto :run
if /i "%CMD%"=="rebuild" goto :rebuild
echo [build] unknown command "%CMD%". Use: app ^| all ^| test ^| run ^| rebuild ^| configure
exit /b 1
:ensure
if not exist "%BUILDDIR%\CMakeCache.txt" "%CMAKE%" --preset %PRESET%
exit /b 0
:configure
"%CMAKE%" --preset %PRESET%
exit /b %errorlevel%
:app
call :ensure
"%CMAKE%" --build "%BUILDDIR%" --target geopro_desktop
exit /b %errorlevel%
:all
call :ensure
"%CMAKE%" --build "%BUILDDIR%"
exit /b %errorlevel%
:test
call :ensure
"%CMAKE%" --build "%BUILDDIR%" --target geopro_tests || exit /b 1
"%CTEST%" --test-dir "%BUILDDIR%" --output-on-failure
exit /b %errorlevel%
:run
call :ensure
"%CMAKE%" --build "%BUILDDIR%" --target geopro_desktop || exit /b 1
"%BUILDDIR%\src\app\geopro_desktop.exe"
exit /b %errorlevel%
:rebuild
REM Force full clean rebuild (--clean-first) then launch; avoids flaky ninja incremental.
REM If geopro_desktop is already running, link fails (LNK1104, exe locked) - close it first.
call :ensure
"%CMAKE%" --build "%BUILDDIR%" --target geopro_desktop --clean-first || exit /b 1
"%BUILDDIR%\src\app\geopro_desktop.exe"
exit /b %errorlevel%