observations-on-the-sublime.../build.bat

333 lines
7.7 KiB
Batchfile

@echo off
setlocal enabledelayedexpansion
REM Build script for "Observations on the Sublime Dynamics of Eroding Matter"
REM A C++ project using SDL2
set "SCRIPT_NAME=%~n0"
set "PROJECT_NAME=observations-on-the-sublime-dynamics-of-eroding-matter"
REM Colors for output (Windows 10+)
set "RED=[91m"
set "GREEN=[92m"
set "YELLOW=[93m"
set "BLUE=[94m"
set "NC=[0m"
REM Function to print colored output
:print_status
echo %BLUE%[INFO]%NC% %~1
goto :eof
:print_success
echo %GREEN%[SUCCESS]%NC% %~1
goto :eof
:print_warning
echo %YELLOW%[WARNING]%NC% %~1
goto :eof
:print_error
echo %RED%[ERROR]%NC% %~1
goto :eof
REM Function to check if command exists
:command_exists
where %1 >nul 2>&1
if %errorlevel% equ 0 (
set "COMMAND_EXISTS=true"
) else (
set "COMMAND_EXISTS=false"
)
goto :eof
REM Function to check SDL2 installation
:check_sdl2
call :print_status "Checking SDL2 installation..."
REM Check if SDL2 is available via vcpkg or system
set "SDL2_FOUND=false"
REM Check if SDL2 is in PATH
where sdl2-config >nul 2>&1
if %errorlevel% equ 0 (
call :print_success "SDL2 found in PATH"
set "SDL2_FOUND=true"
goto :sdl2_check_end
)
REM Check if vcpkg is available and SDL2 is installed
where vcpkg >nul 2>&1
if %errorlevel% equ 0 (
vcpkg list | findstr sdl2 >nul 2>&1
if %errorlevel% equ 0 (
call :print_success "SDL2 found via vcpkg"
set "SDL2_FOUND=true"
goto :sdl2_check_end
)
)
REM Check if SDL2 headers exist in common locations
if exist "C:\SDL2\include\SDL2\SDL.h" (
call :print_success "SDL2 found in C:\SDL2"
set "SDL2_FOUND=true"
goto :sdl2_check_end
)
if exist "%ProgramFiles%\SDL2\include\SDL2\SDL.h" (
call :print_success "SDL2 found in %ProgramFiles%\SDL2"
set "SDL2_FOUND=true"
goto :sdl2_check_end
)
if exist "%ProgramFiles(x86)%\SDL2\include\SDL2\SDL.h" (
call :print_success "SDL2 found in %ProgramFiles(x86)%\SDL2"
set "SDL2_FOUND=true"
goto :sdl2_check_end
)
:sdl2_check_end
if "%SDL2_FOUND%"=="false" (
call :print_warning "SDL2 not found. Please install SDL2:"
call :print_status " 1. Download from: https://www.libsdl.org/download-2.0.php"
call :print_status " 2. Or use vcpkg: vcpkg install sdl2"
call :print_status " 3. Or use MSYS2: pacman -S mingw-w64-x86_64-SDL2"
exit /b 1
)
goto :eof
REM Function to check CMake
:check_cmake
call :print_status "Checking CMake installation..."
call :command_exists cmake
if "%COMMAND_EXISTS%"=="false" (
call :print_error "CMake not found. Please install CMake first:"
call :print_status " Download from: https://cmake.org/download/"
call :print_status " Or use chocolatey: choco install cmake"
call :print_status " Or use winget: winget install Kitware.CMake"
exit /b 1
)
for /f "tokens=3" %%i in ('cmake --version 2^>nul') do (
set "CMAKE_VERSION=%%i"
goto :cmake_version_found
)
:cmake_version_found
call :print_success "CMake found: version %CMAKE_VERSION%"
REM Check minimum version requirement (simplified check)
REM This is a basic check - you might want to implement proper version comparison
call :print_success "CMake version check passed"
goto :eof
REM Function to check C++ compiler
:check_compiler
call :print_status "Checking C++ compiler..."
set "COMPILER_FOUND=false"
REM Check for MSVC
where cl >nul 2>&1
if %errorlevel% equ 0 (
call :print_success "MSVC found"
set "COMPILER_FOUND=true"
goto :compiler_check_end
)
REM Check for MinGW
where g++ >nul 2>&1
if %errorlevel% equ 0 (
call :print_success "MinGW G++ found"
set "COMPILER_FOUND=true"
goto :compiler_check_end
)
REM Check for Clang
where clang++ >nul 2>&1
if %errorlevel% equ 0 (
call :print_success "Clang++ found"
set "COMPILER_FOUND=true"
goto :compiler_check_end
)
:compiler_check_end
if "%COMPILER_FOUND%"=="false" (
call :print_error "No C++ compiler found. Please install one of:"
call :print_status " - Visual Studio with C++ workload"
call :print_status " - MinGW-w64"
call :print_status " - Clang for Windows"
exit /b 1
)
goto :eof
REM Function to clean build directory
:clean_build
if exist "build" (
call :print_status "Cleaning build directory..."
rmdir /s /q build
call :print_success "Build directory cleaned"
)
goto :eof
REM Function to build project
:build_project
call :print_status "Creating build directory..."
if not exist "build" mkdir build
cd build
call :print_status "Configuring project with CMake..."
cmake .. -DCMAKE_BUILD_TYPE=Release
if %errorlevel% neq 0 (
call :print_error "CMake configuration failed"
exit /b 1
)
call :print_status "Building project..."
cmake --build . --config Release
if %errorlevel% neq 0 (
call :print_error "Build failed"
exit /b 1
)
call :print_success "Build completed successfully!"
REM Check if executable was created
if exist "%PROJECT_NAME%.exe" (
call :print_success "Executable created: %PROJECT_NAME%.exe"
call :print_status "You can run it with: %PROJECT_NAME%.exe"
) else (
call :print_error "Executable not found after build"
exit /b 1
)
cd ..
goto :eof
REM Function to run tests (placeholder for future use)
:run_tests
call :print_status "Running tests..."
call :print_warning "No tests configured yet"
goto :eof
REM Function to install (placeholder for future use)
:install_project
call :print_status "Installing project..."
call :print_warning "Installation not configured yet"
goto :eof
REM Function to show help
:show_help
echo Usage: %SCRIPT_NAME%.bat [OPTIONS]
echo.
echo Options:
echo -h, --help Show this help message
echo -c, --clean Clean build directory before building
echo -t, --test Run tests after building
echo -i, --install Install the project
echo --check-only Only check dependencies without building
echo.
echo Examples:
echo %SCRIPT_NAME%.bat # Build the project
echo %SCRIPT_NAME%.bat -c # Clean and build
echo %SCRIPT_NAME%.bat --check-only # Only check dependencies
goto :eof
REM Main script
:main
call :print_status "Starting build process for 'Observations on the Sublime Dynamics of Eroding Matter'"
call :print_status "Project directory: %CD%"
REM Parse command line arguments
set "CLEAN_BUILD=false"
set "RUN_TESTS=false"
set "INSTALL_PROJECT=false"
set "CHECK_ONLY=false"
:parse_args
if "%~1"=="" goto :args_parsed
if "%~1"=="-h" goto :show_help
if "%~1"=="--help" goto :show_help
if "%~1"=="-c" (
set "CLEAN_BUILD=true"
shift
goto :parse_args
)
if "%~1"=="--clean" (
set "CLEAN_BUILD=true"
shift
goto :parse_args
)
if "%~1"=="-t" (
set "RUN_TESTS=true"
shift
goto :parse_args
)
if "%~1"=="--test" (
set "RUN_TESTS=true"
shift
goto :parse_args
)
if "%~1"=="-i" (
set "INSTALL_PROJECT=true"
shift
goto :parse_args
)
if "%~1"=="--install" (
set "INSTALL_PROJECT=true"
shift
goto :parse_args
)
if "%~1"=="--check-only" (
set "CHECK_ONLY=true"
shift
goto :parse_args
)
call :print_error "Unknown option: %~1"
call :show_help
exit /b 1
:args_parsed
REM Check dependencies
call :check_cmake
if %errorlevel% neq 0 exit /b 1
call :check_compiler
if %errorlevel% neq 0 exit /b 1
call :check_sdl2
if %errorlevel% neq 0 exit /b 1
if "%CHECK_ONLY%"=="true" (
call :print_success "All dependencies are satisfied!"
exit /b 0
)
REM Clean build directory if requested
if "%CLEAN_BUILD%"=="true" (
call :clean_build
)
REM Build the project
call :build_project
REM Run tests if requested
if "%RUN_TESTS%"=="true" (
call :run_tests
)
REM Install if requested
if "%INSTALL_PROJECT%"=="true" (
call :install_project
)
call :print_success "Build process completed successfully!"
exit /b 0
REM Entry point
if "%~1"=="-h" goto :show_help
if "%~1"=="--help" goto :show_help
goto :main