build_win.bat 21 KB

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