Skip to content

Commit b77fdec

Browse files
XMuliclaude
andcommitted
build: multi-config build script with version override and parallel compilation
Rewrite build.bat to build 4 configurations (Qt6 MSVC x64, Qt6 MinGW x64, Qt5 MSVC x64, Qt5 MSVC x86) in one run, producing 8 release artifacts (.7z portable + .exe installer per config). Key changes: - Add VERSION_OVERRIDE variable for easy version bumping without editing CMakeLists.txt (falls back to auto-parsing if left empty) - Enable multi-core compilation via cmake --parallel - Clean shared bin/ between configs to prevent cross-contamination - Save/restore PATH around MinGW builds to avoid polluting MSVC configs - Add SKIP_DEPLOY cmake option to disable POST_BUILD windeployqt when the batch script handles deployment itself - Auto-cleanup intermediate build/bin directories after packaging - Parameterize setup_package_user.iss with #ifndef guards so ISCC /D flags can override version, architecture, compiler, and output paths Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent f73bd2a commit b77fdec

File tree

3 files changed

+286
-38
lines changed

3 files changed

+286
-38
lines changed

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,13 +246,15 @@ if (WIN32)
246246
endif()
247247

248248
#******************************************* Qt runtime dependency deployment **********************************************
249+
option(SKIP_DEPLOY "Skip POST_BUILD Qt runtime deployment (for batch build scripts)" OFF)
250+
249251
set(_qt_deploy_hints)
250252
if(QT_BIN_DIR)
251253
list(APPEND _qt_deploy_hints "${QT_BIN_DIR}")
252254
endif()
253255

254256
set(_qt_runtime_deploy_handled FALSE)
255-
if(QT_VERSION_MAJOR EQUAL 6)
257+
if(NOT SKIP_DEPLOY AND QT_VERSION_MAJOR EQUAL 6)
256258
include(Qt6DeploySupport OPTIONAL RESULT_VARIABLE _qt6_deploysupport_included)
257259
if(_qt6_deploysupport_included AND NOT (_qt6_deploysupport_included MATCHES "NOTFOUND") AND COMMAND qt_generate_deploy_app_script)
258260
qt_generate_deploy_app_script(
@@ -271,7 +273,7 @@ if(QT_VERSION_MAJOR EQUAL 6)
271273
endif()
272274
endif()
273275

274-
if(NOT _qt_runtime_deploy_handled)
276+
if(NOT SKIP_DEPLOY AND NOT _qt_runtime_deploy_handled)
275277
if(QT_VERSION_MAJOR EQUAL 6 AND (NOT DEFINED _qt6_deploysupport_included OR _qt6_deploysupport_included MATCHES "NOTFOUND"))
276278
message(WARNING "Qt6DeploySupport.cmake unavailable; falling back to classic deploy helpers.")
277279
endif()

build.bat

Lines changed: 252 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,264 @@
11
@echo off
2-
32
setlocal enabledelayedexpansion
43

5-
:: Define the new path you want to prepend
6-
set new_path=C:\Qt\6.8.3\msvc2022_64\bin
4+
:: ============================================================
5+
:: ChineseChess Multi-Config Build ^& Package Script
6+
:: Usage:
7+
:: build.bat - Build all configs + package
8+
:: build.bat pack - Package only (skip build)
9+
:: build.bat delete - Clean all build directories
10+
:: ============================================================
11+
12+
set APP_NAME=ChineseChess
13+
set "ISCC_PATH=C:\Program Files (x86)\Inno Setup 6\ISCC.exe"
14+
set "SEVENZIP=C:\Program Files\7-Zip\7z.exe"
15+
set "VCVARSALL=C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat"
16+
17+
:: ============================================================
18+
:: Version override: set a value here to skip CMakeLists.txt parsing
19+
:: e.g. set "VERSION_OVERRIDE=7.3.0"
20+
:: Leave empty to auto-detect from CMakeLists.txt
21+
:: ============================================================
22+
set "VERSION_OVERRIDE=7.4.0"
723

8-
:: Check if the path already exists in PATH
9-
echo !PATH! | findstr /C:"%new_path%" > nul
10-
if !errorlevel! == 1 (
11-
:: If the path doesn't exist, prepend it to PATH
12-
set "PATH=%new_path%;!PATH!"
13-
echo Updated PATH: !PATH!
24+
if defined VERSION_OVERRIDE (
25+
for /f "tokens=1,2,3 delims=." %%a in ("!VERSION_OVERRIDE!") do (
26+
set VER_MAJOR=%%a
27+
set VER_MINOR=%%b
28+
set VER_PATCH=%%c
29+
)
1430
) else (
15-
echo PATH already contains %new_path%
31+
:: Parse version from CMakeLists.txt
32+
for /f %%v in ('powershell -NoProfile -Command "(Select-String -Path CMakeLists.txt -Pattern 'set\(project_version_major (\d+)\)').Matches[0].Groups[1].Value"') do set VER_MAJOR=%%v
33+
for /f %%v in ('powershell -NoProfile -Command "(Select-String -Path CMakeLists.txt -Pattern 'set\(project_version_minor (\d+)\)').Matches[0].Groups[1].Value"') do set VER_MINOR=%%v
34+
for /f %%v in ('powershell -NoProfile -Command "(Select-String -Path CMakeLists.txt -Pattern 'set\(project_version_patch (\d+)\)').Matches[0].Groups[1].Value"') do set VER_PATCH=%%v
1635
)
36+
set VERSION=!VER_MAJOR!.!VER_MINOR!
37+
set VERSION_FULL=!VER_MAJOR!.!VER_MINOR!.!VER_PATCH!
1738

18-
::delete build dir
19-
if "%1"=="delete" goto _DEL
39+
echo ============================================================
40+
echo %APP_NAME% v!VERSION_FULL! Multi-Config Build
41+
echo ============================================================
2042

21-
if not exist build md build
43+
:: Handle "delete" command
44+
if "%1"=="delete" (
45+
echo [Clean] Removing all build directories...
46+
for %%d in (build_qt6_msvc_x64 build_qt6_mingw_x64 build_qt5_msvc_x64 build_qt5_msvc_x86) do (
47+
if exist %%d rd /s /q %%d
48+
)
49+
for %%d in (bin_qt6_msvc_x64 bin_qt6_mingw_x64 bin_qt5_msvc_x64 bin_qt5_msvc_x86) do (
50+
if exist %%d rd /s /q %%d
51+
)
52+
echo Done.
53+
goto _END
54+
)
2255

23-
cd build
24-
cmake -G "Visual Studio 17 2022" -A x64 ..
25-
goto _END
56+
if not exist release md release
57+
58+
:: Skip build if "pack" command
59+
if "%1"=="pack" goto _PACK_ALL
60+
61+
:: ============================================================
62+
:: Config 1: Qt6 + MSVC2022 + x64
63+
:: ============================================================
64+
echo.
65+
echo [Config 1/4] Qt6 + MSVC2022 + x64
66+
set "QT_PREFIX=C:\Qt\6.8.3\msvc2022_64"
67+
set "BUILD_DIR=build_qt6_msvc_x64"
68+
set "BIN_DIR=bin_qt6_msvc_x64"
69+
set "CMAKE_GEN=Visual Studio 17 2022"
70+
set "CMAKE_ARCH=x64"
71+
set "VCARCH=x64"
72+
call :BUILD_MSVC
73+
if !errorlevel! neq 0 echo WARNING: Config 1 build failed, skipping...
74+
75+
:: ============================================================
76+
:: Config 2: Qt6 + MinGW13 + x64
77+
:: ============================================================
78+
echo.
79+
echo [Config 2/4] Qt6 + MinGW13 + x64
80+
set "QT_PREFIX=C:\Qt\6.8.3\mingw_64"
81+
set "MINGW_PATH=C:\Qt\Tools\mingw1310_64\bin"
82+
set "BUILD_DIR=build_qt6_mingw_x64"
83+
set "BIN_DIR=bin_qt6_mingw_x64"
84+
call :BUILD_MINGW
85+
if !errorlevel! neq 0 echo WARNING: Config 2 build failed, skipping...
86+
87+
:: ============================================================
88+
:: Config 3: Qt5 + MSVC2019(via VS2022) + x64
89+
:: ============================================================
90+
echo.
91+
echo [Config 3/4] Qt5 + MSVC + x64
92+
set "QT_PREFIX=C:\Qt\5.15.2\msvc2019_64"
93+
set "BUILD_DIR=build_qt5_msvc_x64"
94+
set "BIN_DIR=bin_qt5_msvc_x64"
95+
set "CMAKE_GEN=Visual Studio 17 2022"
96+
set "CMAKE_ARCH=x64"
97+
set "VCARCH=x64"
98+
call :BUILD_MSVC
99+
if !errorlevel! neq 0 echo WARNING: Config 3 build failed, skipping...
100+
101+
:: ============================================================
102+
:: Config 4: Qt5 + MSVC2019(via VS2022) + x86
103+
:: ============================================================
104+
echo.
105+
echo [Config 4/4] Qt5 + MSVC + x86
106+
set "QT_PREFIX=C:\Qt\5.15.2\msvc2019"
107+
set "BUILD_DIR=build_qt5_msvc_x86"
108+
set "BIN_DIR=bin_qt5_msvc_x86"
109+
set "CMAKE_GEN=Visual Studio 17 2022"
110+
set "CMAKE_ARCH=Win32"
111+
set "VCARCH=x86"
112+
call :BUILD_MSVC
113+
if !errorlevel! neq 0 echo WARNING: Config 4 build failed, skipping...
114+
115+
:_PACK_ALL
116+
echo.
117+
echo ============================================================
118+
echo Packaging all configs...
119+
echo ============================================================
26120

27-
:_DEL
28-
rd build/s/q
121+
:: Pack Config 1: Qt6 MSVC x64
122+
call :PACK_ONE "bin_qt6_msvc_x64" "qt6.8_msvc2022" "x64" "msvc" "x64"
123+
124+
:: Pack Config 2: Qt6 MinGW x64
125+
call :PACK_ONE "bin_qt6_mingw_x64" "qt6.8_mingw13" "x64" "mingw" "x64"
126+
127+
:: Pack Config 3: Qt5 MSVC x64
128+
call :PACK_ONE "bin_qt5_msvc_x64" "qt5.15_msvc2022" "x64" "msvc" "x64"
129+
130+
:: Pack Config 4: Qt5 MSVC x86
131+
call :PACK_ONE "bin_qt5_msvc_x86" "qt5.15_msvc2022" "x86" "msvc" "x86"
132+
133+
:: --- Cleanup intermediate directories ---
134+
echo.
135+
echo [Cleanup] Removing intermediate build/bin directories...
136+
for %%d in (build_qt6_msvc_x64 build_qt6_mingw_x64 build_qt5_msvc_x64 build_qt5_msvc_x86) do (
137+
if exist %%d rd /s /q %%d
138+
)
139+
for %%d in (bin_qt6_msvc_x64 bin_qt6_mingw_x64 bin_qt5_msvc_x64 bin_qt5_msvc_x86) do (
140+
if exist %%d rd /s /q %%d
141+
)
142+
if exist bin rd /s /q bin
143+
144+
:: --- Summary ---
145+
echo.
146+
echo ============================================================
147+
echo Output files in release/:
148+
echo ============================================================
149+
dir /b release\
150+
echo.
29151
goto _END
30152

31-
:_END
153+
:: ============================================================
154+
:: SUBROUTINE: BUILD_MSVC
155+
:: Uses: BUILD_DIR, BIN_DIR, QT_PREFIX, CMAKE_GEN, CMAKE_ARCH, VCARCH
156+
:: ============================================================
157+
:BUILD_MSVC
158+
if not exist "!BUILD_DIR!" md "!BUILD_DIR!"
159+
160+
:: Clean shared bin/ to avoid cross-contamination between configs
161+
if exist bin rd /s /q bin
162+
163+
:: Configure (disable CMakeLists.txt POST_BUILD deploy, we do it ourselves)
164+
echo [Configure] ...
165+
cmake -G "!CMAKE_GEN!" -A !CMAKE_ARCH! -DCMAKE_PREFIX_PATH="!QT_PREFIX!" -DSKIP_DEPLOY=ON -S . -B "!BUILD_DIR!"
166+
if !errorlevel! neq 0 exit /b 1
167+
168+
:: Build (multi-core)
169+
echo [Build] ...
170+
cmake --build "!BUILD_DIR!" --config Release --parallel
171+
if !errorlevel! neq 0 exit /b 1
172+
173+
:: Collect output: copy bin/ to BIN_DIR
174+
echo [Collect] ...
175+
if exist "!BIN_DIR!" rd /s /q "!BIN_DIR!"
176+
xcopy /e /i /q bin "!BIN_DIR!" >nul
177+
178+
:: Deploy Qt runtime
179+
echo [Deploy] ...
180+
"!QT_PREFIX!\bin\windeployqt.exe" "!BIN_DIR!\%APP_NAME%.exe"
181+
exit /b 0
182+
183+
:: ============================================================
184+
:: SUBROUTINE: BUILD_MINGW
185+
:: Uses: BUILD_DIR, BIN_DIR, QT_PREFIX, MINGW_PATH
186+
:: ============================================================
187+
:BUILD_MINGW
188+
if not exist "!BUILD_DIR!" md "!BUILD_DIR!"
189+
190+
:: Clean shared bin/ to avoid cross-contamination between configs
191+
if exist bin rd /s /q bin
192+
193+
:: Save PATH and add MinGW for this build only
194+
set "SAVED_PATH=!PATH!"
195+
set "PATH=!MINGW_PATH!;!QT_PREFIX!\bin;!PATH!"
196+
197+
:: Configure (disable CMakeLists.txt POST_BUILD deploy, we do it ourselves)
198+
echo [Configure] ...
199+
cmake -G "MinGW Makefiles" -DCMAKE_PREFIX_PATH="!QT_PREFIX!" -DCMAKE_BUILD_TYPE=Release -DSKIP_DEPLOY=ON -S . -B "!BUILD_DIR!"
200+
if !errorlevel! neq 0 exit /b 1
201+
202+
:: Build (multi-core)
203+
echo [Build] ...
204+
cmake --build "!BUILD_DIR!" --config Release --parallel
205+
if !errorlevel! neq 0 exit /b 1
206+
207+
:: Collect output
208+
echo [Collect] ...
209+
if exist "!BIN_DIR!" rd /s /q "!BIN_DIR!"
210+
xcopy /e /i /q bin "!BIN_DIR!" >nul
211+
212+
:: Deploy Qt runtime
213+
echo [Deploy] ...
214+
"!QT_PREFIX!\bin\windeployqt.exe" "!BIN_DIR!\%APP_NAME%.exe"
215+
216+
:: Restore PATH
217+
set "PATH=!SAVED_PATH!"
218+
exit /b 0
219+
220+
:: ============================================================
221+
:: SUBROUTINE: PACK_ONE
222+
:: %~1=BIN_DIR %~2=qt_compiler_tag %~3=arch %~4=compiler_id %~5=iss_arch
223+
:: ============================================================
224+
:PACK_ONE
225+
set "P_BIN=%~1"
226+
set "P_TAG=%~2"
227+
set "P_ARCH=%~3"
228+
set "P_COMPILER=%~4"
229+
set "P_ISS_ARCH=%~5"
230+
231+
if not exist "!P_BIN!\%APP_NAME%.exe" (
232+
echo - SKIP: !P_BIN! not found
233+
exit /b 0
234+
)
235+
236+
set "PORTABLE_NAME=%APP_NAME%_portable_v!VERSION!_!P_ARCH!_!P_TAG!"
237+
set "SETUP_NAME=%APP_NAME%_setup_v!VERSION!_!P_ARCH!_!P_TAG!"
238+
239+
:: .7z portable
240+
echo - Creating !PORTABLE_NAME!.7z ...
241+
if exist "release\!PORTABLE_NAME!.7z" del "release\!PORTABLE_NAME!.7z"
242+
"!SEVENZIP!" a -t7z "release\!PORTABLE_NAME!.7z" ".\!P_BIN!\*" >nul
243+
if !errorlevel! neq 0 (
244+
echo FAIL
245+
) else (
246+
echo OK
247+
)
248+
249+
:: .exe installer via Inno Setup
250+
if exist "!ISCC_PATH!" (
251+
echo - Creating !SETUP_NAME!.exe ...
252+
"!ISCC_PATH!" /DMyAppVersion="!VERSION_FULL!" /DMyArchitecture="!P_ISS_ARCH!" /DMyCOMPILER_ID="!P_COMPILER!" /DMyBinDir="!P_BIN!" /DMyOutputDir="./release" /DOutputBaseFilename="!SETUP_NAME!" setup_package_user.iss >nul
253+
if !errorlevel! neq 0 (
254+
echo FAIL
255+
) else (
256+
echo OK
257+
)
258+
) else (
259+
echo - SKIP: Inno Setup not found
260+
)
261+
exit /b 0
262+
263+
:_END
264+
endlocal

0 commit comments

Comments
 (0)