build_win.bat 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. @setlocal disableDelayedExpansion enableExtensions
  2. @IF "%PS_ECHO_ON%" NEQ "" (echo on) ELSE (echo off)
  3. @GOTO :MAIN
  4. :HELP
  5. @ECHO.
  6. @ECHO Performs initial build or rebuild of the app (build) and deps (build/deps).
  7. @ECHO Default options are determined from build directories and system state.
  8. @ECHO.
  9. @ECHO Usage: build_win [-ARCH ^<arch^>] [-CONFIG ^<config^>] [-VERSION ^<version^>]
  10. @ECHO [-PRODUCT ^<product^>] [-DESTDIR ^<directory^>]
  11. @ECHO [-STEPS ^<all^|all-dirty^|app^|app-dirty^|deps^|deps-dirty^>]
  12. @ECHO [-RUN ^<console^|custom^|none^|viewer^|window^>]
  13. @ECHO.
  14. @ECHO -a -ARCH Target processor architecture
  15. @ECHO Default: %PS_ARCH_HOST%
  16. @ECHO -c -CONFIG MSVC project config
  17. @ECHO Default: %PS_CONFIG_DEFAULT%
  18. @ECHO -v -VERSION Major version number of MSVC installation to use for build
  19. @ECHO Default: %PS_VERSION_SUPPORTED%
  20. @ECHO -p -PRODUCT Product ID of MSVC installation to use for build
  21. @ECHO Default: %PS_PRODUCT_DEFAULT%
  22. @ECHO -s -STEPS Performs only the specified build steps:
  23. @ECHO all - clean and build deps and app
  24. @ECHO all-dirty - build deps and app without cleaning
  25. @ECHO app - clean and build main applications
  26. @ECHO app-dirty - build main applications without cleaning
  27. @ECHO deps - clean and build deps
  28. @ECHO deps-dirty - build deps without cleaning
  29. @ECHO Default: %PS_STEPS_DEFAULT%
  30. @ECHO -r -RUN Specifies what to perform at the run step:
  31. @ECHO console - run and wait on bambu-studio-console.exe
  32. @ECHO custom - run and wait on your custom build/%PS_CUSTOM_RUN_FILE%
  33. @ECHO ide - open project in Visual Studio if not open (no wait)
  34. @ECHO none - run step does nothing
  35. @ECHO viewer - run bambu-gcodeviewer.exe (no wait)
  36. @ECHO window - run bambu-studio.exe (no wait)
  37. @ECHO Default: none
  38. @ECHO -d -DESTDIR Deps destination directory
  39. @ECHO Warning: Changing destdir path will not delete the old destdir.
  40. @ECHO Default: %PS_DESTDIR_DEFAULT_MSG%
  41. @ECHO.
  42. @ECHO Examples:
  43. @ECHO.
  44. @ECHO Initial build: build_win -d "c:\src\BambuSlicer-deps"
  45. @ECHO Build post deps change: build_win -s all
  46. @ECHO App dirty build: build_win
  47. @ECHO App dirty build ^& run: build_win -r console
  48. @ECHO All clean build ^& run: build_win -s all -r console -d "deps\build\out_deps"
  49. @ECHO.
  50. GOTO :END
  51. :MAIN
  52. REM Script constants
  53. SET START_TIME=%TIME%
  54. SET PS_START_DIR=%CD%
  55. SET PS_SOLUTION_NAME=BambuStudio
  56. SET PS_CHOICE_TIMEOUT=30
  57. SET PS_CUSTOM_RUN_FILE=custom_run.bat
  58. SET PS_DEPS_PATH_FILE_NAME=.DEPS_PATH.txt
  59. SET PS_DEPS_PATH_FILE=%~dp0deps\build\%PS_DEPS_PATH_FILE_NAME%
  60. SET PS_CONFIG_LIST="Debug;MinSizeRel;Release;RelWithDebInfo"
  61. REM The officially supported toolchain version is 16 (Visual Studio 2019)
  62. REM TODO: Update versions after Boost gets rolled to 1.78 or later
  63. SET PS_VERSION_SUPPORTED=16
  64. SET PS_VERSION_EXCEEDED=17
  65. SET VSWHERE=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe
  66. IF NOT EXIST "%VSWHERE%" SET VSWHERE=%ProgramFiles%\Microsoft Visual Studio\Installer\vswhere.exe
  67. FOR /F "tokens=4 USEBACKQ delims=." %%I IN (`"%VSWHERE%" -nologo -property productId`) DO SET PS_PRODUCT_DEFAULT=%%I
  68. IF "%PS_PRODUCT_DEFAULT%" EQU "" (
  69. SET EXIT_STATUS=-1
  70. @ECHO ERROR: No Visual Studio installation found. 1>&2
  71. GOTO :HELP
  72. )
  73. REM Default to the latest supported version if multiple are available
  74. FOR /F "tokens=1 USEBACKQ delims=." %%I IN (
  75. `^""%VSWHERE%" -version "[%PS_VERSION_SUPPORTED%,%PS_VERSION_EXCEEDED%)" -latest -nologo -property catalog_buildVersion^"`
  76. ) DO SET PS_VERSION_SUPPORTED=%%I
  77. REM Probe build directories and system state for reasonable default arguments
  78. pushd %~dp0
  79. SET PS_CONFIG=RelWithDebInfo
  80. SET PS_ARCH=%PROCESSOR_ARCHITECTURE:amd64=x64%
  81. CALL :TOLOWER PS_ARCH
  82. SET PS_RUN=none
  83. SET PS_DESTDIR=
  84. SET PS_VERSION=
  85. SET PS_PRODUCT=%PS_PRODUCT_DEFAULT%
  86. CALL :RESOLVE_DESTDIR_CACHE
  87. REM Set up parameters used by help menu
  88. SET EXIT_STATUS=0
  89. SET PS_CONFIG_DEFAULT=%PS_CONFIG%
  90. SET PS_ARCH_HOST=%PS_ARCH%
  91. (echo " -help /help -h /h -? /? ")| findstr /I /C:" %~1 ">nul && GOTO :HELP
  92. REM Parse arguments
  93. SET EXIT_STATUS=1
  94. SET PS_CURRENT_STEP=arguments
  95. SET PARSER_STATE=
  96. SET PARSER_FAIL=
  97. FOR %%I in (%*) DO CALL :PARSE_OPTION "ARCH CONFIG DESTDIR STEPS RUN VERSION PRODUCT" PARSER_STATE "%%~I"
  98. IF "%PARSER_FAIL%" NEQ "" (
  99. @ECHO ERROR: Invalid switch: %PARSER_FAIL% 1>&2
  100. GOTO :HELP
  101. )ELSE IF "%PARSER_STATE%" NEQ "" (
  102. @ECHO ERROR: Missing parameter for: %PARSER_STATE% 1>&2
  103. GOTO :HELP
  104. )
  105. REM Validate arguments
  106. SET PS_ASK_TO_CONTINUE=
  107. CALL :TOLOWER PS_ARCH
  108. SET PS_ARCH=%PS_ARCH:amd64=x64%
  109. CALL :PARSE_OPTION_VALUE %PS_CONFIG_LIST:;= % PS_CONFIG
  110. IF "%PS_CONFIG%" EQU "" GOTO :HELP
  111. REM RESOLVE_DESTDIR_CACHE must go after PS_ARCH and PS_CONFIG, but before PS STEPS
  112. CALL :RESOLVE_DESTDIR_CACHE
  113. IF "%PS_STEPS%" EQU "" SET PS_STEPS=%PS_STEPS_DEFAULT%
  114. CALL :PARSE_OPTION_VALUE "all all-dirty deps-dirty deps app-dirty app app-cmake" PS_STEPS
  115. IF "%PS_STEPS%" EQU "" GOTO :HELP
  116. (echo %PS_STEPS%)| findstr /I /C:"dirty">nul && SET PS_STEPS_DIRTY=1 || SET PS_STEPS_DIRTY=
  117. IF "%PS_STEPS%" EQU "app-cmake" SET PS_STEPS_DIRTY=1
  118. IF "%PS_DESTDIR%" EQU "" SET PS_DESTDIR=%PS_DESTDIR_CACHED%
  119. IF "%PS_DESTDIR%" EQU "" (
  120. @ECHO ERROR: Parameter required: -DESTDIR 1>&2
  121. GOTO :HELP
  122. )
  123. CALL :CANONICALIZE_PATH PS_DESTDIR "%PS_START_DIR%"
  124. IF "%PS_DESTDIR%" NEQ "%PS_DESTDIR_CACHED%" (
  125. (echo "all deps all-dirty deps-dirty")| findstr /I /C:"%PS_STEPS%">nul || (
  126. IF EXIST "%PS_DESTDIR%" (
  127. @ECHO WARNING: DESTDIR does not match cache: 1>&2
  128. @ECHO WARNING: new: %PS_DESTDIR% 1>&2
  129. @ECHO WARNING: old: %PS_DESTDIR_CACHED% 1>&2
  130. SET PS_ASK_TO_CONTINUE=1
  131. ) ELSE (
  132. @ECHO ERROR: Invalid parameter: DESTDIR=%PS_DESTDIR% 1>&2
  133. GOTO :HELP
  134. )
  135. )
  136. )
  137. SET PS_DESTDIR_DEFAULT_MSG=
  138. CALL :PARSE_OPTION_VALUE "console custom ide none viewer window" PS_RUN
  139. IF "%PS_RUN%" EQU "" GOTO :HELP
  140. IF "%PS_RUN%" NEQ "none" IF "%PS_STEPS:~0,4%" EQU "deps" (
  141. @ECHO ERROR: RUN=%PS_RUN% specified with STEPS=%PS_STEPS%
  142. @ECHO ERROR: RUN=none is the only valid option for STEPS "deps" or "deps-dirty"
  143. GOTO :HELP
  144. )
  145. IF DEFINED PS_VERSION (
  146. SET /A PS_VERSION_EXCEEDED=%PS_VERSION% + 1
  147. ) ELSE SET PS_VERSION=%PS_VERSION_SUPPORTED%
  148. SET MSVC_FILTER=-products Microsoft.VisualStudio.Product.%PS_PRODUCT% -version "[%PS_VERSION%,%PS_VERSION_EXCEEDED%)"
  149. FOR /F "tokens=* USEBACKQ" %%I IN (`^""%VSWHERE%" %MSVC_FILTER% -nologo -property installationPath^"`) DO SET MSVC_DIR=%%I
  150. IF NOT EXIST "%MSVC_DIR%" (
  151. @ECHO ERROR: Compatible Visual Studio installation not found. 1>&2
  152. GOTO :HELP
  153. )
  154. REM Give the user a chance to cancel if we found something odd.
  155. IF "%PS_ASK_TO_CONTINUE%" EQU "" GOTO :BUILD_ENV
  156. @ECHO.
  157. @ECHO Unexpected parameters detected. Build paused for %PS_CHOICE_TIMEOUT% seconds.
  158. choice /T %PS_CHOICE_TIMEOUT% /C YN /D N /M "Continue"
  159. IF %ERRORLEVEL% NEQ 1 GOTO :HELP
  160. REM Set up MSVC environment
  161. :BUILD_ENV
  162. SET EXIT_STATUS=2
  163. SET PS_CURRENT_STEP=environment
  164. @ECHO **********************************************************************
  165. @ECHO ** Build Config: %PS_CONFIG%
  166. @ECHO ** Target Arch: %PS_ARCH%
  167. @ECHO ** Build Steps: %PS_STEPS%
  168. @ECHO ** Run App: %PS_RUN%
  169. @ECHO ** Deps path: %PS_DESTDIR%
  170. @ECHO ** Using Microsoft Visual Studio installation found at:
  171. @ECHO ** %MSVC_DIR%
  172. CALL "%MSVC_DIR%\Common7\Tools\vsdevcmd.bat" -arch=%PS_ARCH% -host_arch=%PS_ARCH_HOST% -app_platform=Desktop
  173. IF %ERRORLEVEL% NEQ 0 GOTO :END
  174. REM Need to reset the echo state after vsdevcmd.bat clobbers it.
  175. @IF "%PS_ECHO_ON%" NEQ "" (echo on) ELSE (echo off)
  176. IF "%PS_DRY_RUN_ONLY%" NEQ "" (
  177. @ECHO Script terminated early because PS_DRY_RUN_ONLY is set. 1>&2
  178. GOTO :END
  179. )
  180. IF /I "%PS_STEPS:~0,3%" EQU "app" GOTO :BUILD_APP
  181. REM Build deps
  182. :BUILD_DEPS
  183. SET EXIT_STATUS=3
  184. SET PS_CURRENT_STEP=deps
  185. IF "%PS_STEPS_DIRTY%" EQU "" (
  186. CALL :MAKE_OR_CLEAN_DIRECTORY deps\build "%PS_DEPS_PATH_FILE_NAME%" .vs
  187. CALL :MAKE_OR_CLEAN_DIRECTORY "%PS_DESTDIR%"
  188. )
  189. cd deps\build || GOTO :END
  190. cmake.exe .. -DDESTDIR="%PS_DESTDIR%"
  191. IF %ERRORLEVEL% NEQ 0 IF "%PS_STEPS_DIRTY%" NEQ "" (
  192. (del CMakeCache.txt && cmake.exe .. -DDESTDIR="%PS_DESTDIR%") || GOTO :END
  193. ) ELSE GOTO :END
  194. (echo %PS_DESTDIR%)> "%PS_DEPS_PATH_FILE%"
  195. msbuild /m ALL_BUILD.vcxproj /p:Configuration=%PS_CONFIG% /v:quiet || GOTO :END
  196. cd ..\..
  197. IF /I "%PS_STEPS:~0,4%" EQU "deps" GOTO :RUN_APP
  198. REM Build app
  199. :BUILD_APP
  200. SET EXIT_STATUS=4
  201. SET PS_CURRENT_STEP=app
  202. IF "%PS_STEPS_DIRTY%" EQU "" CALL :MAKE_OR_CLEAN_DIRECTORY build "%PS_CUSTOM_RUN_FILE%" .vs
  203. cd build || GOTO :END
  204. REM Make sure we have a custom batch file skeleton for the run stage
  205. set PS_CUSTOM_BAT=%PS_CUSTOM_RUN_FILE%
  206. CALL :CANONICALIZE_PATH PS_CUSTOM_BAT
  207. IF NOT EXIST %PS_CUSTOM_BAT% CALL :WRITE_CUSTOM_SCRIPT_SKELETON %PS_CUSTOM_BAT%
  208. SET PS_PROJECT_IS_OPEN=
  209. FOR /F "tokens=2 delims=," %%I in (
  210. 'tasklist /V /FI "IMAGENAME eq devenv.exe " /NH /FO CSV ^| find "%PS_SOLUTION_NAME%"'
  211. ) do SET PS_PROJECT_IS_OPEN=%%~I
  212. cmake.exe .. -DCMAKE_PREFIX_PATH="%PS_DESTDIR%\usr\local" -DCMAKE_CONFIGURATION_TYPES=%PS_CONFIG_LIST%
  213. IF %ERRORLEVEL% NEQ 0 IF "%PS_STEPS_DIRTY%" NEQ "" (
  214. (del CMakeCache.txt && cmake.exe .. -DCMAKE_PREFIX_PATH="%PS_DESTDIR%\usr\local" -DCMAKE_CONFIGURATION_TYPES=%PS_CONFIG_LIST%) || GOTO :END
  215. ) ELSE GOTO :END
  216. REM Skip the build step if we're using the undocumented app-cmake to regenerate the full config from inside devenv
  217. IF "%PS_STEPS%" NEQ "app-cmake" msbuild /m ALL_BUILD.vcxproj /p:Configuration=%PS_CONFIG% /v:quiet || GOTO :END
  218. (echo %PS_DESTDIR%)> "%PS_DEPS_PATH_FILE_FOR_CONFIG%"
  219. REM Run app
  220. :RUN_APP
  221. REM All build steps complete.
  222. CALL :DIFF_TIME ELAPSED_TIME %START_TIME% %TIME%
  223. IF "%PS_CURRENT_STEP%" NEQ "arguments" (
  224. @ECHO.
  225. @ECHO Total Build Time Elapsed %ELAPSED_TIME%
  226. )
  227. SET EXIT_STATUS=5
  228. SET PS_CURRENT_STEP=run
  229. IF "%PS_RUN%" EQU "none" GOTO :PROLOGUE
  230. cd src\%PS_CONFIG% || GOTO :END
  231. SET PS_PROJECT_IS_OPEN=
  232. FOR /F "tokens=2 delims=," %%I in (
  233. 'tasklist /V /FI "IMAGENAME eq devenv.exe " /NH /FO CSV ^| find "%PS_SOLUTION_NAME%"'
  234. ) do SET PS_PROJECT_IS_OPEN=%%~I
  235. @ECHO.
  236. @ECHO Running %PS_RUN% application...
  237. @REM icacls below is just a hack for file-not-found error handling
  238. IF "%PS_RUN%" EQU "console" (
  239. icacls bambu-studio-console.exe >nul || GOTO :END
  240. start /wait /b bambu-studio-console.exe
  241. ) ELSE IF "%PS_RUN%" EQU "window" (
  242. icacls bambu-studio.exe >nul || GOTO :END
  243. start bambu-studio.exe
  244. ) ELSE IF "%PS_RUN%" EQU "viewer" (
  245. icacls bambu-gcodeviewer.exe >nul || GOTO :END
  246. start bambu-gcodeviewer.exe
  247. ) ELSE IF "%PS_RUN%" EQU "custom" (
  248. icacls %PS_CUSTOM_BAT% >nul || GOTO :END
  249. CALL %PS_CUSTOM_BAT%
  250. ) ELSE IF "%PS_RUN%" EQU "ide" (
  251. IF "%PS_PROJECT_IS_OPEN%" NEQ "" (
  252. @ECHO WARNING: Solution is already open in Visual Studio. Skipping ide run step. 1>&2
  253. ) ELSE (
  254. @ECHO Preparing to run Visual Studio...
  255. cd ..\.. || GOTO :END
  256. REM This hack generates a single config for MSVS, guaranteeing it gets set as the active config.
  257. cmake.exe .. -DCMAKE_PREFIX_PATH="%PS_DESTDIR%\usr\local" -DCMAKE_CONFIGURATION_TYPES=%PS_CONFIG% > nul 2> nul || GOTO :END
  258. REM Now launch devenv with the single config (setting it active) and a /command switch to re-run cmake and generate the full config list
  259. start devenv.exe %PS_SOLUTION_NAME%.sln /command ^"shell /o ^^^"%~f0^^^" -d ^^^"%PS_DESTDIR%^^^" -c %PS_CONFIG% -a %PS_ARCH% -r none -s app-cmake^"
  260. REM If devenv fails to launch just directly regenerate the full config list.
  261. IF %ERRORLEVEL% NEQ 0 (
  262. cmake.exe .. -DCMAKE_PREFIX_PATH="%PS_DESTDIR%\usr\local" -DCMAKE_CONFIGURATION_TYPES=%PS_CONFIG_LIST% 2> nul 1> nul || GOTO :END
  263. )
  264. )
  265. )
  266. @REM ********** DON'T ADD ANY CODE BETWEEN THESE TWO SECTIONS **********
  267. @REM RUN_APP may hand off control, so let exit codes fall through to PROLOGUE.
  268. :PROLOGUE
  269. SET EXIT_STATUS=%ERRORLEVEL%
  270. :END
  271. @IF "%PS_ECHO_ON%%PS_DRY_RUN_ONLY%" NEQ "" (
  272. @ECHO **********************************************************************
  273. @ECHO ** Script Parameters:
  274. @ECHO **********************************************************************
  275. @SET PS_
  276. )
  277. IF "%EXIT_STATUS%" NEQ "0" (
  278. IF "%PS_CURRENT_STEP%" NEQ "arguments" (
  279. @ECHO.
  280. @ECHO ERROR: *** Build process failed at %PS_CURRENT_STEP% step. *** 1>&2
  281. )
  282. ) ELSE (
  283. @ECHO All steps completed successfully.
  284. )
  285. popd
  286. exit /B %EXIT_STATUS%
  287. GOTO :EOF
  288. REM Functions and stubs start here.
  289. :RESOLVE_DESTDIR_CACHE
  290. @REM Resolves all DESTDIR cache values and sets PS_STEPS_DEFAULT
  291. @REM Note: This just sets global variables, so it doesn't use setlocal.
  292. SET PS_DEPS_PATH_FILE_FOR_CONFIG=%~dp0build\.vs\%PS_ARCH%\%PS_CONFIG%\%PS_DEPS_PATH_FILE_NAME%
  293. mkdir "%~dp0build\.vs\%PS_ARCH%\%PS_CONFIG%" > nul 2> nul
  294. REM Copy a legacy file if we don't have one in the proper location.
  295. echo f|xcopy /D "%~dp0build\%PS_ARCH%\%PS_CONFIG%\%PS_DEPS_PATH_FILE_NAME%" "%PS_DEPS_PATH_FILE_FOR_CONFIG%" > nul 2> nul
  296. CALL :CANONICALIZE_PATH PS_DEPS_PATH_FILE_FOR_CONFIG
  297. IF EXIST "%PS_DEPS_PATH_FILE_FOR_CONFIG%" (
  298. FOR /F "tokens=* USEBACKQ" %%I IN ("%PS_DEPS_PATH_FILE_FOR_CONFIG%") DO (
  299. SET PS_DESTDIR_CACHED=%%I
  300. SET PS_DESTDIR_DEFAULT_MSG=%%I
  301. )
  302. SET PS_STEPS_DEFAULT=app-dirty
  303. ) ELSE IF EXIST "%PS_DEPS_PATH_FILE%" (
  304. FOR /F "tokens=* USEBACKQ" %%I IN ("%PS_DEPS_PATH_FILE%") DO (
  305. SET PS_DESTDIR_CACHED=%%I
  306. SET PS_DESTDIR_DEFAULT_MSG=%%I
  307. )
  308. SET PS_STEPS_DEFAULT=app
  309. ) ELSE (
  310. SET PS_DESTDIR_CACHED=
  311. SET PS_DESTDIR_DEFAULT_MSG=Cache missing. Argument required.
  312. SET PS_STEPS_DEFAULT=all
  313. )
  314. GOTO :EOF
  315. :PARSE_OPTION
  316. @REM Argument parser called for each argument
  317. @REM %1 - Valid option list
  318. @REM %2 - Variable name for parser state; must be unset when parsing finished
  319. @REM %3 - Current argument value
  320. @REM PARSER_FAIL will be set on an error
  321. @REM Note: Must avoid delayed expansion since filenames may contain ! character
  322. setlocal disableDelayedExpansion
  323. IF "%PARSER_FAIL%" NEQ "" GOTO :EOF
  324. CALL SET LAST_ARG=%%%2%%
  325. IF "%LAST_ARG%" EQU "" (
  326. CALL :PARSE_OPTION_NAME %1 %~2 %~3 1
  327. SET ARG_TYPE=NAME
  328. ) ELSE (
  329. SET PS_SET_COMMAND=SET PS_%LAST_ARG%=%~3
  330. SET ARG_TYPE=LAST_ARG
  331. SET %~2=
  332. )
  333. CALL SET LAST_ARG=%%%2%%
  334. IF "%LAST_ARG%%ARG_TYPE%" EQU "NAME" SET PARSER_FAIL=%~3
  335. (
  336. endlocal
  337. SET PARSER_FAIL=%PARSER_FAIL%
  338. SET %~2=%LAST_ARG%
  339. %PS_SET_COMMAND%
  340. )
  341. GOTO :EOF
  342. :PARSE_OPTION_VALUE
  343. setlocal disableDelayedExpansion
  344. @REM Parses value and verifies it is within the supplied list
  345. @REM %1 - Valid option list
  346. @REM %2 - In/out variable name; unset on error
  347. CALL SET NAME=%~2
  348. CALL SET SAVED_VALUE=%%%NAME%%%
  349. CALL :PARSE_OPTION_NAME %1 %NAME% -%SAVED_VALUE%
  350. CALL SET NEW_VALUE=%%%NAME%%%
  351. IF "%NEW_VALUE%" EQU "" (
  352. @ECHO ERROR: Invalid parameter: %NAME:~3%=%SAVED_VALUE% 1>&2
  353. )
  354. endlocal & SET %NAME%=%NEW_VALUE%
  355. GOTO :EOF
  356. :PARSE_OPTION_NAME
  357. @REM Parses an option name
  358. @REM %1 - Valid option list
  359. @REM %2 - Out variable name; unset on error
  360. @REM %3 - Current argument value
  361. @REM %4 - Boolean indicating single character switches are valid
  362. @REM Note: Delayed expansion safe because ! character is invalid in option name
  363. setlocal enableDelayedExpansion
  364. IF "%4" NEQ "" FOR %%I IN (%~1) DO @(
  365. SET SHORT_NAME=%%~I
  366. SET SHORT_ARG_!SHORT_NAME:~0,1!=%%~I
  367. )
  368. @SET OPTION_NAME=%~3
  369. @(echo %OPTION_NAME%)| findstr /R /C:"[-/]..*">nul || GOTO :PARSE_OPTION_NAME_FAIL
  370. @SET OPTION_NAME=%OPTION_NAME:~1%
  371. IF "%4" NEQ "" (
  372. IF "%OPTION_NAME%" EQU "%OPTION_NAME:~0,1%" (
  373. IF "!SHORT_ARG_%OPTION_NAME:~0,1%!" NEQ "" SET OPTION_NAME=!SHORT_ARG_%OPTION_NAME:~0,1%!
  374. )
  375. )
  376. @(echo %OPTION_NAME%)| findstr /R /C:".[ ][ ]*.">nul && GOTO :PARSE_OPTION_NAME_FAIL
  377. @(echo %~1 )| findstr /I /C:" %OPTION_NAME% ">nul || GOTO :PARSE_OPTION_NAME_FAIL
  378. FOR %%I IN (%~1) DO SET OPTION_NAME=!OPTION_NAME:%%~I=%%~I!
  379. endlocal & SET %~2=%OPTION_NAME%
  380. GOTO :EOF
  381. :PARSE_OPTION_NAME_FAIL
  382. endlocal & SET %~2=
  383. GOTO :EOF
  384. :MAKE_OR_CLEAN_DIRECTORY
  385. @REM Create directory if it doesn't exist or clean it if it does
  386. @REM %1 - Directory path to clean or create
  387. @REM %* - Optional list of files/dirs to keep (in the base directory only)
  388. setlocal disableDelayedExpansion
  389. IF NOT EXIST "%~1" (
  390. @ECHO Creating %~1
  391. mkdir "%~1" && (
  392. endlocal
  393. GOTO :EOF
  394. )
  395. )
  396. @ECHO Cleaning %~1 ...
  397. SET KEEP_LIST=
  398. :MAKE_OR_CLEAN_DIRECTORY_ARG_LOOP
  399. IF "%~2" NEQ "" (
  400. SET KEEP_LIST=%KEEP_LIST% "%~2"
  401. SHIFT /2
  402. GOTO :MAKE_OR_CLEAN_DIRECTORY_ARG_LOOP
  403. )
  404. for /F "usebackq delims=" %%I in (`dir /a /b "%~1"`) do (
  405. (echo %KEEP_LIST%)| findstr /I /L /C:"\"%%I\"">nul || (
  406. rmdir /s /q "%~1\%%I" 2>nul ) || del /q /f "%~1\%%I"
  407. )
  408. endlocal
  409. GOTO :EOF
  410. :TOLOWER
  411. @REM Converts supplied environment variable to lowercase
  412. @REM %1 - Input/output variable name
  413. @REM Note: This is slow on very long strings, but is used only on very short ones
  414. setlocal disableDelayedExpansion
  415. @FOR %%b IN (a b c d e f g h i j k l m n o p q r s t u v w x y z) DO @CALL set %~1=%%%1:%%b=%%b%%
  416. @CALL SET OUTPUT=%%%~1%%
  417. endlocal & SET %~1=%OUTPUT%
  418. GOTO :EOF
  419. :CANONICALIZE_PATH
  420. @REM Canonicalizes the path in the supplied variable
  421. @REM %1 - Input/output variable containing path to canonicalize
  422. @REM %2 - Optional base directory
  423. setlocal
  424. CALL :CANONICALIZE_PATH_INNER %1 %%%~1%% %2
  425. endlocal & SET %~1=%OUTPUT%
  426. GOTO :EOF
  427. :CANONICALIZE_PATH_INNER
  428. if "%~3" NEQ "" (pushd %3 || GOTO :EOF)
  429. SET OUTPUT=%~f2
  430. if "%~3" NEQ "" popd
  431. GOTO :EOF
  432. :DIFF_TIME
  433. @REM Calculates elapsed time between two timestamps (TIME environment variable format)
  434. @REM %1 - Output variable
  435. @REM %2 - Start time
  436. @REM %3 - End time
  437. setlocal EnableDelayedExpansion
  438. set START_ARG=%2
  439. set END_ARG=%3
  440. set END=!END_ARG:%TIME:~8,1%=%%100)*100+1!
  441. set START=!START_ARG:%TIME:~8,1%=%%100)*100+1!
  442. set /A DIFF=((((10!END:%TIME:~2,1%=%%100)*60+1!%%100)-((((10!START:%TIME:~2,1%=%%100)*60+1!%%100), DIFF-=(DIFF^>^>31)*24*60*60*100
  443. set /A CC=DIFF%%100+100,DIFF/=100,SS=DIFF%%60+100,DIFF/=60,MM=DIFF%%60+100,HH=DIFF/60+100
  444. @endlocal & set %1=%HH:~1%%TIME:~2,1%%MM:~1%%TIME:~2,1%%SS:~1%%TIME:~8,1%%CC:~1%
  445. @GOTO :EOF
  446. :WRITE_CUSTOM_SCRIPT_SKELETON
  447. @REM Writes the following text to the supplied file
  448. @REM %1 - Output filename
  449. setlocal
  450. @(
  451. ECHO @ECHO.
  452. ECHO @ECHO ********************************************************************************
  453. ECHO @ECHO ** This is a custom run script skeleton.
  454. ECHO @ECHO ********************************************************************************
  455. ECHO @ECHO.
  456. ECHO @ECHO ********************************************************************************
  457. ECHO @ECHO ** The working directory is:
  458. ECHO @ECHO ********************************************************************************
  459. ECHO dir
  460. ECHO @ECHO.
  461. ECHO @ECHO ********************************************************************************
  462. ECHO @ECHO ** The environment is:
  463. ECHO @ECHO ********************************************************************************
  464. ECHO set
  465. ECHO @ECHO.
  466. ECHO @ECHO ********************************************************************************
  467. ECHO @ECHO ** Edit or replace this script to run custom steps after a successful build:
  468. ECHO @ECHO ** %~1
  469. ECHO @ECHO ********************************************************************************
  470. ECHO @ECHO.
  471. ) > "%~1"
  472. endlocal
  473. GOTO :EOF