Repository: opencv
Issue: contrib module CUDEV uses c++ exceptions but not enabled on Windows
Labels: category: build/install,category: gpu/cuda (contrib),platform: win32

OpenCV compile that includes contrib module CUDEV, the cmake infrastructure disables C++ exceptions but this module's code needs exceptions. Because of this, the code will behave unpredictably if an exception occurs.

#### System information (version)

- OpenCV => 4.5.1 at master b19f8603843ac63274379c9e36331db6d4917001
- Operating System / Platform => Microsoft Windows [Version 10.0.19042.867]
- Compiler => VS2019 v16.9.0  64-bit compile

#### Repro

1. Setup as above. 
2. Enable features related to contrib modulke CUDEV
3. Compile debug

#### Result

This warning is issued during compile:

```
[build] [1645/2349  69% :: 538.614] Building CXX object modules\cudev\test\CMakeFiles\opencv_test_cudev.dir\test_main.cpp.obj
[build] C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29910\include\ostream(411): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
[build] C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29910\include\ostream(404): note: while compiling class template member function 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(double)'
[build] C:\repos-nobackup\opencv\modules\core\include\opencv2/core/utility.hpp(400): note: see reference to function template instantiation 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(double)' being compiled
[build] C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29910\include\ostream(641): note: see reference to class template instantiation 'std::basic_ostream<char,std::char_traits<char>>' being compiled
```

#### Expected

Clean compile, no warnings, and predictable code execution.

#### Likely cause

`compile_commands.json` created by cmake on Windows shows exceptions are not enabled for `cudev/test/test_main.cpp`

```json
{
  "directory": "C:/repos-nobackup/opencv/build",
  "command": "C:\\PROGRA~2\\MICROS~2\\2019\\COMMUN~1\\VC\\Tools\\MSVC\\1428~1.299\\bin\\Hostx64\\x64\\cl.exe  /nologo /TP -D_USE_MATH_DEFINES -D_VARIADIC_MAX=10 -D__OPENCV_BUILD=1 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I. -IC:\\repos-nobackup\\opencv_contrib\\modules\\cudev\\src -IC:\\repos-nobackup\\opencv_contrib\\modules\\cudev\\include -Imodules\\cudev -IC:\\repos-nobackup\\opencv\\modules\\core\\include -Imodules\\cudev\\test -IC:\\repos-nobackup\\opencv\\modules\\imgproc\\include -IC:\\repos-nobackup\\opencv\\modules\\imgcodecs\\include -IC:\\repos-nobackup\\opencv\\modules\\videoio\\include -IC:\\repos-nobackup\\opencv\\modules\\highgui\\include -IC:\\repos-nobackup\\opencv\\modules\\ts\\include -I\"C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v10.1\\include\" -I\"C:\\Program Files (x86)\\Intel\\oneAPI\\ipp\\2021.1.1\\include\"  -DGLOG_NO_ABBREVIATED_SEVERITIES -DWINVER=0x0A00 -D_WIN32_WINNT=0x0A00 -DNTDDI_VERSION=0x0A000005 /arch:AVX  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise /FS         /arch:AVX  /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /wd4189 /wd4505  /MD /O2 /Ob2 /DNDEBUG  /GL /Fomodules\\cudev\\test\\CMakeFiles\\opencv_test_cudev.dir\\test_main.cpp.obj /FdTARGET_COMPILE_PDB /FS -c C:\\repos-nobackup\\opencv_contrib\\modules\\cudev\\test\\test_main.cpp",
  "file": "C:\\repos-nobackup\\opencv_contrib\\modules\\cudev\\test\\test_main.cpp"
},
```

There are only two `cpp` files in the CUDEV module. The compile command for `cudev/src/stub.cpp` correctly includes `/EHa`. A suspicious difference in the two compile commands for each. Notice the two spaces between `AVX` and `/wd4127`. This suggests that somewhere is placement for the missing `/EHa` and cmake chose to not place it yet the surrounding spaces remain. 

```
      below is test_main.cpp
/Gy /bigobj /Oi  /fp:precise /FS         /arch:AVX  /wd4127 /wd4251 /wd4324
/Gy /bigobj /Oi  /fp:precise /FS         /arch:AVX /EHa /wd4127 /wd4251 /wd4324
      above is stub.cpp
```

I suspect the missing flag is due to the following

https://github.com/opencv/opencv/blob/b19f8603843ac63274379c9e36331db6d4917001/cmake/OpenCVDetectCUDA.cmake#L346-L358

If the test code needs exceptions (as this warning declares), then the `/EHa` needs to be used on the compile command. In contrast, if the test code removes its use of exceptions, then exception support is not needed. It is possible to ignore warning 4530, but the test code would still be unpredictable and it might provide invalid test results.

#### Issue submission checklist

 - [X] I report the issue, it's not a question
 - [X] I checked the problem with documentation, FAQ, open issues,
       forum.opencv.org, Stack Overflow, etc and have not found solution
 - [X] I updated to latest OpenCV version and the issue is still there