_winpack_build_sample.cmd 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. :: Usage:
  2. :: - Drag & drop .cpp file on this file from Windows explorer
  3. :: - Run from cmd/powershell:
  4. :: - > _winpack_build_sample.cmd cpp\opencv_version.cpp
  5. :: Requires:
  6. :: - CMake
  7. :: - MSVS 2015/2017/2019
  8. :: (tools are searched on default paths or environment should be pre-configured)
  9. @echo off
  10. setlocal
  11. SET SCRIPT_DIR=%~dp0
  12. SET "OPENCV_SETUPVARS_SCRIPT=setup_vars_opencv4.cmd"
  13. SET "PACKAGE_BUILD_DIR=%SCRIPT_DIR%\..\..\build"
  14. IF NOT EXIST "%PACKAGE_BUILD_DIR%\%OPENCV_SETUPVARS_SCRIPT%" (
  15. :: Winpack DLDT
  16. SET "PACKAGE_BUILD_DIR=%SCRIPT_DIR%\..\..\..\build"
  17. )
  18. IF NOT EXIST "%PACKAGE_BUILD_DIR%\%OPENCV_SETUPVARS_SCRIPT%" (
  19. set "MSG=OpenCV Winpack installation is required"
  20. goto die
  21. )
  22. :: normalize path
  23. for %%i in ("%PACKAGE_BUILD_DIR%") do SET "PACKAGE_BUILD_DIR=%%~fi"
  24. if [%1]==[] (
  25. set "MSG=Sample path is required"
  26. goto die
  27. )
  28. if exist %1\* (
  29. set "MSG=Only .cpp samples are allowed (not a directory): %1"
  30. goto die
  31. )
  32. if NOT "%~x1" == ".cpp" (
  33. set "MSG=Only .cpp samples are allowed: %~x1"
  34. goto die
  35. )
  36. set SRC_FILENAME=%~dpnx1
  37. echo SRC_FILENAME=%SRC_FILENAME%
  38. call :dirname "%SRC_FILENAME%" SRC_DIR
  39. echo SRC_DIR=%SRC_DIR%
  40. set "SRC_NAME=%~n1"
  41. echo SRC_NAME=%SRC_NAME%
  42. echo ================================================================================
  43. :: Path to root 'bin' dir
  44. set "PATH=%PACKAGE_BUILD_DIR%\bin;%PATH%"
  45. :: Detect compiler
  46. cl /? >NUL 2>NUL <NUL
  47. if %ERRORLEVEL% == 0 (
  48. goto detect_cmake
  49. )
  50. PUSHD %CD%
  51. CALL :try_call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvars64.bat"
  52. IF ERRORLEVEL 1 (
  53. CALL :try_call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvars64.bat"
  54. )
  55. IF ERRORLEVEL 1 (
  56. CALL :try_call "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvars64.bat"
  57. )
  58. IF ERRORLEVEL 1 (
  59. CALL :try_call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvars64.bat"
  60. )
  61. IF ERRORLEVEL 1 (
  62. CALL :try_call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvars64.bat"
  63. )
  64. IF ERRORLEVEL 1 (
  65. CALL :try_call "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvars64.bat"
  66. )
  67. IF ERRORLEVEL 1 (
  68. CALL :try_call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64
  69. )
  70. POPD
  71. cl /? >NUL 2>NUL <NUL
  72. if %ERRORLEVEL% NEQ 0 (
  73. set "MSG=Can't detect Microsoft Visual Studio C++ compiler (cl.exe). MSVS 2015/2017/2019 are supported only from standard locations"
  74. goto die
  75. )
  76. :: Detect CMake
  77. :detect_cmake
  78. cmake --version >NUL 2>NUL
  79. if %ERRORLEVEL% EQU 0 GOTO :CMAKE_FOUND
  80. if NOT exist "C:\Program Files\CMake\bin" GOTO CMAKE_NOT_FOUND
  81. set "PATH=%PATH%;C:\Program Files\CMake\bin"
  82. cmake --version >NUL 2>NUL
  83. if %ERRORLEVEL% EQU 0 GOTO :CMAKE_FOUND
  84. :CMAKE_NOT_FOUND
  85. set "MSG=CMake is required to build OpenCV samples. Download it from here: https://cmake.org/download/ and install into 'C:\Program Files\CMake'"
  86. goto die
  87. :CMAKE_FOUND
  88. set CMAKE_FOUND=1
  89. call :execute cmake --version
  90. echo CMake is detected
  91. where cmake
  92. :: Detect available MSVS version
  93. if NOT DEFINED VisualStudioVersion (
  94. set "MSG=Can't determine MSVS version. 'VisualStudioVersion' is not defined"
  95. goto die
  96. )
  97. if "%VisualStudioVersion%" == "14.0" (
  98. set "CMAKE_GENERATOR=-G^"Visual Studio 14 Win64^""
  99. set "BUILD_DIR_SUFFIX=.vc14"
  100. if EXIST "%PACKAGE_BUILD_DIR%\x64\vc14\bin" (
  101. set "PATH=%PACKAGE_BUILD_DIR%\x64\vc14\bin;%PATH%"
  102. )
  103. ) else (
  104. if "%VisualStudioVersion%" == "15.0" (
  105. set "CMAKE_GENERATOR=-G^"Visual Studio 15 Win64^""
  106. set "BUILD_DIR_SUFFIX=.vc15"
  107. set "PATH=%PATH%;%SCRIPTDIR%\..\..\build\x64\vc15\bin\"
  108. if EXIST "%PACKAGE_BUILD_DIR%\x64\vc15\bin" (
  109. set "PATH=%PACKAGE_BUILD_DIR%\x64\vc15\bin;%PATH%"
  110. ) else (
  111. if EXIST "%PACKAGE_BUILD_DIR%\x64\vc14\bin" (
  112. set "PATH=%PACKAGE_BUILD_DIR%\x64\vc14\bin;%PATH%"
  113. )
  114. )
  115. ) else (
  116. if "%VisualStudioVersion%" == "16.0" (
  117. echo.==========================================
  118. echo.* Note: MSVS 2019 requires CMake 3.14+ *
  119. echo.==========================================
  120. set "CMAKE_GENERATOR=-G^"Visual Studio 16 2019^" -A x64"
  121. set "BUILD_DIR_SUFFIX=.vc16"
  122. if EXIST "%PACKAGE_BUILD_DIR%\x64\vc16\bin" (
  123. set "PATH=%PACKAGE_BUILD_DIR%\x64\vc16\bin;%PATH%"
  124. ) else (
  125. if EXIST "%PACKAGE_BUILD_DIR%\x64\vc15\bin" (
  126. set "PATH=%PACKAGE_BUILD_DIR%\x64\vc15\bin;%PATH%"
  127. ) else (
  128. if EXIST "%PACKAGE_BUILD_DIR%\x64\vc14\bin" (
  129. set "PATH=%PACKAGE_BUILD_DIR%\x64\vc14\bin;%PATH%"
  130. )
  131. )
  132. )
  133. ) else (
  134. set "MSG=Unsupported MSVS version. VisualStudioVersion=%VisualStudioVersion%"
  135. goto die
  136. )
  137. )
  138. )
  139. set "BUILD_DIR=%SRC_DIR%\build_%SRC_NAME%%BUILD_DIR_SUFFIX%"
  140. call :set_title Create build directory
  141. if NOT exist "%BUILD_DIR%" ( call :execute md "%BUILD_DIR%" )
  142. PUSHD "%BUILD_DIR%"
  143. if NOT exist "%BUILD_DIR%/sample" ( call :execute md "%BUILD_DIR%/sample" )
  144. call :execute copy /Y "%SCRIPT_DIR%/CMakeLists.example.in" "%BUILD_DIR%/sample/CMakeLists.txt"
  145. call :set_title Configuring via CMake
  146. call :execute cmake %CMAKE_GENERATOR% "%BUILD_DIR%\sample" -DEXAMPLE_NAME=%SRC_NAME% "-DEXAMPLE_FILE=%SRC_FILENAME%"
  147. if %ERRORLEVEL% NEQ 0 (
  148. set "MSG=CMake configuration step failed: %BUILD_DIR%"
  149. goto die
  150. )
  151. call :set_title Build sample project via CMake
  152. call :execute cmake --build . --config Release
  153. if %ERRORLEVEL% NEQ 0 (
  154. set "MSG=Build step failed: %BUILD_DIR%"
  155. goto die
  156. )
  157. call :set_title Launch %SRC_NAME%
  158. if NOT exist "%BUILD_DIR%\Release\%SRC_NAME%.exe" (
  159. echo. "ERROR: Can't find executable file (build seems broken): %SRC_NAME%.exe"
  160. ) else (
  161. cd "%BUILD_DIR%\Release"
  162. call :execute "%SRC_NAME%.exe" --help
  163. echo ================================================================================
  164. echo ** Type '%SRC_NAME%.exe' to run sample application
  165. echo ** Type '%SRC_NAME%.exe --help' to get list of available options (if available)
  166. echo ** Type 'start ..\%SRC_NAME%.sln' to launch MSVS IDE
  167. echo ** Type 'cmake --build .. --config Release' to rebuild sample
  168. echo ** Type 'exit' to exit from interactive shell and open the build directory
  169. echo ================================================================================
  170. )
  171. call :set_title Hands-on: %SRC_NAME%
  172. cmd /k echo Current directory: %CD%
  173. call :set_title Done: %SRC_NAME%
  174. echo Opening build directory with project files...
  175. explorer "%BUILD_DIR%"
  176. POPD
  177. echo Done%
  178. pause
  179. exit /B 0
  180. ::
  181. :: Helper routines
  182. ::
  183. :set_title
  184. title OpenCV sample: %*
  185. EXIT /B 0
  186. :execute
  187. echo =================================================================================
  188. setlocal
  189. echo %*
  190. call %*
  191. endlocal
  192. EXIT /B %ERRORLEVEL%
  193. :dirname file resultVar
  194. setlocal
  195. set _dir=%~dp1
  196. set _dir=%_dir:~0,-1%
  197. endlocal & set %2=%_dir%
  198. EXIT /B 0
  199. :try_call
  200. IF EXIST %1 (
  201. CALL %*
  202. EXIT /B
  203. ) ELSE (
  204. EXIT /B 1
  205. )
  206. :: 'goto die' instead of 'call'
  207. :die
  208. TITLE OpenCV sample: ERROR: %MSG%
  209. echo ERROR: %MSG%
  210. pause
  211. EXIT /B 1