CMake和VisualStudio编译脚本示例

@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

echo ********************************************************************************
echo *                      Visual Studio CMake Build Script                        *
echo ********************************************************************************

:: 1. Setup build configurations
@REM Current script directory
set "SCRIPT_DIR=%~dp0"
@REM Project source code directory which contains the root CMakeLists.txt
set "SOURCE_DIR=%SCRIPT_DIR:~0,-1%"
@REM Set the target build directory
set "BUILD_DIR=%SOURCE_DIR%\..\build\spdlog"
@REM Set the target install directory
set "INSTALL_DIR=%SOURCE_DIR%\..\output"
@REM Set the target build type: Debug or Release
set "BUILD_TYPE=Release"
@REM Set the target architecture: x86 / x64 
set "TARGET_ARCH=x64"
@REM Set the Visual Studio generator(Must be installed on your system). Version ref: 
@REM     VS2015 (MSVC140) -> "Visual Studio 14 2015"
@REM     VS2017 (MSVC141) -> "Visual Studio 15 2017"
@REM     VS2019 (MSVC142) -> "Visual Studio 16 2019"
@REM     VS2022 (MSVC143) -> "Visual Studio 17 2022"
set "VS_GENERATOR=Visual Studio 17 2022"
@REM Compiler flags for utf-8 support (Universal way for 3rd-party libs)
set "CFLAGS=/utf-8"
set "CXXFLAGS=/utf-8"

if not exist "%BUILD_DIR%"   ( mkdir "%BUILD_DIR%" )
if not exist "%INSTALL_DIR%" ( mkdir "%INSTALL_DIR%" )

:: Parse command line arguments
@REM arg1: Visual Studio Version
if not "%~1"=="" (
    if /i "%~1"=="2017" (
        set "VS_GENERATOR=Visual Studio 15 2017"
    ) else if /i "%~1"=="2019" (
        set "VS_GENERATOR=Visual Studio 16 2019"
    ) else if /i "%~1"=="2022" (
        set "VS_GENERATOR=Visual Studio 17 2022"
    ) else if /i "%~1"=="-h" (
       call :show_usage && exit /b 0
    ) else if /i "%~1"=="--help" (
        call :show_usage && exit /b 0
    ) else (
        call :LOG_WARN "Invalid Visual Studio version: %~1. Using default (%VS_GENERATOR%)."
    )
)
@REM arg2: Build Type
if not "%~2"=="" (
    if /i "%~2"=="Debug" (
        set "BUILD_TYPE=Debug"
    ) else if /i "%~2"=="Release" (
        set "BUILD_TYPE=Release"
    ) else (
        call :LOG_WARN "Invalid build type: %~2. Using default (%BUILD_TYPE%)."
    )
)
@REM arg3: Target Architecture
if not "%~3"=="" (
    if /i "%~3"=="x86" (
        set "TARGET_ARCH=x86"
    ) else if /i "%~3"=="x64" (
        set "TARGET_ARCH=x64"
    ) else (
        call :LOG_WARN "Invalid architecture: %~3. Using default (%TARGET_ARCH%)."
    )
)

echo.
call :LOG_INFO "****** Build Configurations ******"
call :LOG_INFO "Source Directory    : %SOURCE_DIR%"
call :LOG_INFO "Build Directory     : %BUILD_DIR%"
call :LOG_INFO "Install Directory   : %INSTALL_DIR%"
call :LOG_INFO "Build Type          : %BUILD_TYPE%"
call :LOG_INFO "Target Architecture : %TARGET_ARCH%"
call :LOG_INFO "Visual Studio       : %VS_GENERATOR%"

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: 2. CMake Tool Check
echo.
@REM Try to print CMake version (cmake --version >nul 2>&1)
cmake --version
if errorlevel 1 (
    call :LOG_ERROR "Could not find CMake tool. Please make sure CMake is installed."
    exit /b 1
)

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: 3. CMake project configuration and generation
cmake -S "%SOURCE_DIR%" -B "%BUILD_DIR%" ^
    -DSPDLOG_INSTALL=ON ^
    -DSPDLOG_BUILD_SHARED=ON ^
    -DSPDLOG_BUILD_PIC=ON ^
    -DSPDLOG_BUILD_EXAMPLE=OFF ^
    -DSPDLOG_BUILD_TESTS=OFF ^
    -DCMAKE_DEBUG_POSTFIX=d ^
    -DCMAKE_POSITION_INDEPENDENT_CODE=ON ^
    -DCMAKE_INSTALL_PREFIX="%INSTALL_DIR%" ^
    -G "%VS_GENERATOR%" -A %TARGET_ARCH%

if errorlevel 1 (
    echo.
    call :LOG_ERROR "CMake configuration or generation failed."
    exit /b 1
)

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: 4. Build & Install
cmake --build "%BUILD_DIR%" --config %BUILD_TYPE% --target install --parallel 8

echo.
if errorlevel 1 (
    call :LOG_ERROR "Build or Install %BUILD_TYPE% target failed."
    exit /b 1
)
call :LOG_INFO "======================================================"
call :LOG_INFO "Build and Install %BUILD_TYPE% target successfully!"
call :LOG_INFO "Installation directory: %INSTALL_DIR%"
call :LOG_INFO "======================================================"
exit /b 0


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Function to display usage information
:show_usage
echo.
echo Usage:
echo   build_xxx.bat [Visual Studio Version] [Build Type] [Target Architecture]
echo.
echo Options:
echo   Visual Studio Version Option:
echo     - 2017 : Visual Studio 2017 (Visual Studio 15 2017) MSVC141
echo     - 2019 : Visual Studio 2019 (Visual Studio 16 2019) MSVC142
echo     - 2022 : Visual Studio 2022 (Visual Studio 17 2022) MSVC143
echo     - (default) : 2022 if not specified or invalid input
echo     - Note: The Visual Studio must be have installed on your system.
echo.
echo   Build Type Option:
echo     - Release   : Optimized release build
echo     - Debug     : Debug build with symbols
echo     - (default) : Release if not specified or invalid input
echo.
echo   Target Architecture Option:
echo     - x86       : 32-bit target
echo     - x64       : 64-bit target
echo     - (default) : x64 if not specified or invalid input
echo.
echo Examples:
echo   build_xxx.bat                    : Build with default settings (VS2022, Release, x64)
echo   build_xxx.bat VS2019             : Build with VS2019, Release, x64
echo   build_xxx.bat VS2019 Debug       : Build with VS2019, Debug, x64
echo   build_xxx.bat VS2019 Release x86   : Build with VS2019, Release, x86
echo.
exit /b 0
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Log functions with ANSI color codes
:LOG_RAW
echo %~1
exit /b 0

:LOG_DEBUG
echo | set /p dummy=[DEBUG] 
echo %~1
exit /b 0

:LOG_INFO
echo | set /p dummy=[INFO] 
echo %~1
exit /b 0

:LOG_WARN
echo | set /p dummy=[WARN] 
echo %~1
exit /b 0

:LOG_ERROR
echo | set /p dummy=[ERROR] 
echo %~1
exit /b 1
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

 

posted @ 2026-05-20 22:46  Jame-DENG  阅读(19)  评论(0)    收藏  举报