build_win.bat 20 KB

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