11@ echo off
22setlocal EnableDelayedExpansion
33
4- :: --- Validate task parameter (clean, build, rebuild ) ---
4+ :: --- Validate preset parameter (required ) ---
55if " %~1 " == " " (
6- echo [ERROR] Task not specified.
7- echo Usage: %~nx0 [clean^ |build^ |rebuild] [BuildType]
6+ echo [ERROR] Preset not specified.
7+ echo Usage: %~nx0 ^ < Preset^ > [Target]
8+ echo ^ < Preset^ > : Required. The CMake configure/build preset to use.
9+ echo [Target] : Optional. The specific target to build. If omitted, the default target will be built.
810 exit /b 1
911)
1012
11- set " TASK = %~1 "
12-
13- if /I not " %TASK% " == " clean" if /I not " %TASK% " == " build" if /I not " %TASK% " == " rebuild" (
14- echo [ERROR] Invalid task: " %TASK% "
15- echo Valid tasks are: clean, build, rebuild
16- exit /b 1
17- )
18-
19- :: --- Validate build type parameter (must be only letters) ---
20- if " %~2 " == " " (
21- echo [ERROR] Build type not specified.
22- echo Usage: %~nx0 [clean^ |build^ |rebuild] [BuildType]
23- exit /b 1
24- )
25-
26- :: --- Validate that is is at least all letters, also need to use "." hack due to piping issue with *$ in findstr (. consumes \r) ---
27- echo %~2 | findstr /R " ^[a-zA-Z][a-zA-Z]*.$" > nul
28- if errorlevel 1 (
29- echo [ERROR] Invalid build type: " %~2 "
30- echo It must only contain letters ^ (e.g., Debug, Release^ ).
31- exit /b 1
32- )
33-
34- :: --- Set build directory ---
35- set " BUILD_DIR = build-%~2 "
36-
37- :: --- Clean build directory if task is clean or rebuild ---
38- if /I not " %TASK% " == " build" (
39- if exist " %BUILD_DIR% " (
40- echo Cleaning build directory " %BUILD_DIR% " ...
41- rmdir /S /Q " %BUILD_DIR% "
42- if errorlevel 1 (
43- echo [ERROR] Failed to clean build directory " %BUILD_DIR% " .
44- exit /b 1
45- )
46- echo Clean completed successfully.
47- ) else (
48- echo Build directory " %BUILD_DIR% " does not exist. Nothing to clean.
49- )
50- )
51-
52- :: --- If task is clean only, exit after cleaning ---
53- if /I " %TASK% " == " clean" (
54- exit /b 0
55- )
56-
57- :: --- Check and load old environment into context ---
58- set " VSVARS = MSVC1400\vsvars32-portable.bat"
59- if not exist " %VSVARS% " (
60- echo [ERROR] Could not find environment setup script: %VSVARS%
61- exit /b 1
62- )
63- call " %VSVARS% "
64- if errorlevel 1 (
65- echo [ERROR] Failed to execute: %VSVARS%
66- exit /b 2
67- )
13+ set " PRESET = %~1 "
14+ set " TARGET = %~2 "
6815
6916:: --- Check for cmake ---
7017where cmake > nul 2 > nul
@@ -74,56 +21,55 @@ if errorlevel 1 (
7421 echo [ERROR] 'vswhere' not found, cannot locate 'cmake'.
7522 exit /b 1
7623 )
77- FOR /F " tokens=* USEBACKQ" %%g IN (`" C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -latest -property installationPath -requires Microsoft.VisualStudio.Component.VC.CMake.Project`) do (SET " CMAKE=%%g \Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin" )
78- echo [INFO] Detected: " !CMAKE! "
24+ FOR /F " tokens=* USEBACKQ" %%g IN (`" C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -latest -property installationPath -requires Microsoft.VisualStudio.Component.VC.CMake.Project`) do (
25+ SET " CMAKE = %%g \Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin"
26+ )
27+ if not exist " !CMAKE! " (
28+ FOR /F " tokens=* USEBACKQ" %%g IN (`" C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe" -latest -property installationPath -requires Microsoft.VisualStudio.Component.VC.CMake.Project -products Microsoft.VisualStudio.Product.BuildTools` ) do (
29+ SET " CMAKE = %%g \Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin"
30+ )
31+ )
7932 if not exist " !CMAKE! " (
8033 echo [ERROR] 'cmake' is not found in PATH and not found using vswhere.
8134 exit /b 1
8235 )
36+ echo [INFO] Detected: " !CMAKE! "
8337 set " PATH = !CMAKE! ;%PATH% "
8438)
8539
86- :: --- Check for nmake ---
87- where nmake > nul 2 > nul
88- if errorlevel 1 (
89- echo [ERROR] 'nmake' is not found in PATH.
90- exit /b 1
91- )
92-
9340:: --- Kill mspdbsrv.exe if it's running ---
9441tasklist | find /I " mspdbsrv.exe" > nul
9542if not errorlevel 1 (
9643 echo Stopping mspdbsrv.exe...
9744 taskkill /f /t /im mspdbsrv.exe > nul
9845 if errorlevel 1 (
9946 echo [WARNING] Failed to stop mspdbsrv.exe, continuing...
100- )
101- )
102-
103- :: --- Create build directory ---
104- if not exist " %BUILD_DIR% " (
105- mkdir " %BUILD_DIR% " > nul 2 >& 1
106- if errorlevel 1 (
107- echo [ERROR] Failed to create build directory " %BUILD_DIR% " .
108- exit /b 1
10947 )
11048)
11149
112- :: --- Run cmake and nmake ---
113- pushd " %BUILD_DIR% "
114- cmake .. -G " NMake Makefiles " -DCMAKE_BUILD_TYPE= %~2
50+ :: --- Run cmake configure using preset ---
51+ echo [INFO] Configuring with preset " %PRESET% " ...
52+ cmake --preset " %PRESET% "
11553if errorlevel 1 (
116- echo [ERROR] cmake failed.
117- popd
54+ echo [ERROR] CMake configure failed for preset " %PRESET% " .
11855 exit /b 1
11956)
12057
121- nmake
122- if errorlevel 1 (
123- echo [ERROR] nmake build failed.
124- popd
125- exit /b 1
58+ :: --- Run cmake build using preset ---
59+ if " %TARGET% " == " " (
60+ echo [INFO] Building preset " %PRESET% " with default target...
61+ cmake --build --preset " %PRESET% "
62+ if errorlevel 1 (
63+ echo [ERROR] CMake build failed for preset " %PRESET% " .
64+ exit /b 1
65+ )
66+ echo Build completed successfully for preset " %PRESET% " .
67+ ) else (
68+ echo [INFO] Building preset " %PRESET% " with target " %TARGET% " ...
69+ cmake --build --preset " %PRESET% " --target " %TARGET% "
70+ if errorlevel 1 (
71+ echo [ERROR] CMake build failed for preset " %PRESET% " target " %TARGET% " .
72+ exit /b 1
73+ )
74+ echo Build completed successfully for preset " %PRESET% " target " %TARGET% " .
12675)
127-
128- popd
129- echo Build completed successfully for " %~2 " configuration.
0 commit comments