Skip to content

Instantly share code, notes, and snippets.

@saracen24
Last active December 20, 2020 17:20
Show Gist options
  • Select an option

  • Save saracen24/2c28838a27d90e22d01d155e17b7cc1b to your computer and use it in GitHub Desktop.

Select an option

Save saracen24/2c28838a27d90e22d01d155e17b7cc1b to your computer and use it in GitHub Desktop.
Build OpenCV with contrib and CUDA 11 in Windows. CMake, Microsoft Visual Studio 2019 and vswhere required.
:: Possible configuration (contrib, cuda, openvino):
:: NVidia CUDA Toolkit
:: NVidia cuDNN
:: Intel C++ Compiler Redistributable Libraries
:: Intel MKL
:: Intel TBB
:: Intel OpenVINO toolkit
::
:: Current configuration (contrib, cuda):
:: NVidia CUDA Toolkit 11.1.1
:: NVidia cuDNN v8.0.5.39 (November 9th, 2020), for CUDA 11.1
@echo off
setlocal
:: Options.
set OPENCV_TAG=4.5.0
set OPENCV_NAME=opencv-%OPENCV_TAG%
set BUILD_DIR=build_%OPENCV_NAME%
set INSTALL_DIR=%cd%/%OPENCV_NAME%
:: Remove build and install directories.
if exist "%BUILD_DIR%" rmdir /s /q "%BUILD_DIR%"
if exist "%INSTALL_DIR%" rmdir /s /q "%INSTALL_DIR%"
:: Clone.
if not exist "opencv" (
git clone "https://github.com/opencv/opencv.git"
pushd "opencv"
git checkout "%OPENCV_TAG%"
git submodule update --init --recursive
popd
)
if not exist "opencv_contrib" (
git clone "https://github.com/opencv/opencv_contrib.git"
pushd "opencv_contrib"
git checkout "%OPENCV_TAG%"
git submodule update --init --recursive
popd
)
:: Set MSBuild environment variables.
set VSWHERE="%ProgramFiles(x86)%/Microsoft Visual Studio/Installer/vswhere.exe"
set VSWHEREOPT=-version ^^^[16.0^^^,17.0^^^) -requires Microsoft.Component.MSBuild -property installationPath
for /f "usebackq tokens=*" %%i in (`%VSWHERE% %VSWHEREOPT%`) do (
if exist "%%i" set VS2019INSTALLDIR=%%i
)
:: Setup developer command prompt for Visual Studio, Intel and other.
call "%VS2019INSTALLDIR%/Common7/Tools/VsDevCmd.bat" -arch=x64
:: call conda activate py39
:: call "%ProgramFiles(x86)%/IntelSWTools/compilers_and_libraries/windows/bin/compilervars.bat" intel64 vs2019
:: call "%ProgramFiles(x86)%/IntelSWTools/openvino/bin/setupvars.bat"
:: Build targets.
call :build_target Debug
call :build_target Release
goto eof
:: Usage: call :build_target <BUILD_TYPE(Debug|Release)>
:build_target
setlocal
:: Check arguments.
if not "%1" == "Debug" if not "%1" == "Release" exit /b 1
:: Options.
set BUILD_TYPE=%1
:: Build.
cmake -S "opencv" -B "%BUILD_DIR%/%BUILD_TYPE%" -G "Visual Studio 16 2019" -A x64 -T host=x64 ^
-DCMAKE_BUILD_TYPE:STRING=%BUILD_TYPE% ^
-DCMAKE_INSTALL_PREFIX:PATH="%INSTALL_DIR%" ^
-DBUILD_SHARED_LIBS:BOOL=ON ^
-DBUILD_TESTS:BOOL=OFF ^
-DOPENCV_EXTRA_MODULES_PATH:PATH="%cd%/opencv_contrib/modules" ^
-DWITH_OPENMP:BOOL=ON ^
-DWITH_TBB:BOOL=OFF ^
-DWITH_OPENGL:BOOL=ON ^
-DWITH_QT:BOOL=OFF ^
-DWITH_INF_ENGINE:BOOL=OFF ^
-DWITH_CUDA:BOOL=ON ^
-DWITH_CUDNN:BOOL=ON ^
-DWITH_CUBLAS:BOOL=ON ^
-DWITH_CUFFT:BOOL=ON ^
-DOPENCV_DNN_CUDA:BOOL=ON ^
-DCUDA_FAST_MATH:BOOL=ON ^
-DCUDA_GENERATION:STRING="" ^
-DCUDA_ARCH_BIN:STRING="3.5 3.7 5.0 5.2 6.0 6.1 7.0 7.5 8.0 8.6" ^
-DCUDA_ARCH_PTX:STRING="" ^
-DBUILD_opencv_python2:BOOL=OFF ^
-DBUILD_opencv_python3:BOOL=OFF
cmake --build "%BUILD_DIR%/%BUILD_TYPE%" --config %BUILD_TYPE% --parallel 4
cmake --install "%BUILD_DIR%/%BUILD_TYPE%" --config %BUILD_TYPE% --strip
endlocal
exit /b 0
:eof
endlocal
pause
exit /b %ERRORLEVEL%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment