Repository: spack/spack

Issue:
opencv@3.x+dnn+cuda fails due to including the wrong header file
When installing OpenCV 3.4.1 (I could reproduce the same behavior also with version 3.3.0) with +cuda and +dnn, then the compilation failed, because one of the header files from the OpenCV dnn module has the same name as a cuda header file and the cuda header file is included instead of the OpenCV one.

I think that the error is arising, from a conflict between

`/scratch/spackapps/spack-stage/spack-stage-BRyfMH/opencv-3.4.1/modules/dnn/src/ocl4dnn/include/math_functions.hpp`

and

`/cluster/spack/apps/linux-centos7-x86_64/gcc-4.8.5/cuda-9.0.176-uqbi62cpkd645vbehqscqy42zhdvfvqq/include/math_functions.hpp`

for some reason, the CUDA header file is found first, when actually the header file from the dnn module would need to be included.

The cuda header file then includes another CUDA header file func_macro.h which causes the error message

`#error -- incorrect inclusion of a cudart header file`

According to

https://devtalk.nvidia.com/default/topic/952579/when-i-compile-cuda-program-always-get-error-incorrect-inclusion-of-a-cudart-header-file/

func_macro.h should never be included.

### Expected Result

OpenCV 3.4.1 with CUDA 9.0 should build fine.

### Actual Result

```
[ 36%] Building CXX object modules/dnn/CMakeFiles/opencv_dnn.dir/src/layers/permute_layer.cpp.o
cd /scratch/spackapps/spack-stage/spack-stage-BRyfMH/opencv-3.4.1/spack-build/modules/dnn && /cluster/apps/spack/lib/spack/env/gcc/g++  -DCVAPI_EXPORTS -DHAVE_PROTOBUF=1 -DOPENCV_TRAITS_ENABLE_DEPRECATED -D_USE_MATH_DEFINES -D__OPENCV_BUILD=1 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -isystem /scratch/spackapps/spack-stage/spack-stage-BRyfMH/opencv-3.4.1/spack-build -I/cluster/apps/spack/var/spack/stage/opencv-3.4.1-zxmgmfmwzkooupmg24dzntqp2h2dqy3k/opencv-3.4.1/modules/dnn/include -I/cluster/apps/spack/var/spack/stage/opencv-3.4.1-zxmgmfmwzkooupmg24dzntqp2h2dqy3k/opencv-3.4.1/modules/dnn/src -I/scratch/spackapps/spack-stage/spack-stage-BRyfMH/opencv-3.4.1/spack-build/modules/dnn -I/cluster/apps/spack/var/spack/stage/opencv-3.4.1-zxmgmfmwzkooupmg24dzntqp2h2dqy3k/opencv-3.4.1/modules/cudev/include -I/cluster/apps/spack/var/spack/stage/opencv-3.4.1-zxmgmfmwzkooupmg24dzntqp2h2dqy3k/opencv-3.4.1/modules/core/include -I/cluster/apps/spack/var/spack/stage/opencv-3.4.1-zxmgmfmwzkooupmg24dzntqp2h2dqy3k/opencv-3.4.1/modules/imgproc/include -I/cluster/apps/spack/var/spack/stage/opencv-3.4.1-zxmgmfmwzkooupmg24dzntqp2h2dqy3k/opencv-3.4.1/modules/dnn/src/ocl4dnn/include -I/cluster/apps/spack/var/spack/stage/opencv-3.4.1-zxmgmfmwzkooupmg24dzntqp2h2dqy3k/opencv-3.4.1/modules/ts/include -I/cluster/apps/spack/var/spack/stage/opencv-3.4.1-zxmgmfmwzkooupmg24dzntqp2h2dqy3k/opencv-3.4.1/modules/imgcodecs/include -I/cluster/apps/spack/var/spack/stage/opencv-3.4.1-zxmgmfmwzkooupmg24dzntqp2h2dqy3k/opencv-3.4.1/modules/videoio/include -I/cluster/apps/spack/var/spack/stage/opencv-3.4.1-zxmgmfmwzkooupmg24dzntqp2h2dqy3k/opencv-3.4.1/modules/highgui/include -I/cluster/apps/spack/var/spack/stage/opencv-3.4.1-zxmgmfmwzkooupmg24dzntqp2h2dqy3k/opencv-3.4.1/modules/dnn/test -I/cluster/apps/spack/var/spack/stage/opencv-3.4.1-zxmgmfmwzkooupmg24dzntqp2h2dqy3k/opencv-3.4.1/modules/dnn/perf -isystem /cluster/spack/apps/linux-centos7-x86_64/gcc-4.8.5/protobuf-3.1.0-l4okq4ruz7pclkxnr7kcsoghssoprscg/include  -fsigned-char -W -Wall -Werror=return-type  -Werror=address -Werror=sequence-point -Wformat -Werror=format-security   -Winit-self -Wpointer-arith   -Wuninitialized -Winit-self -Wno-narrowing -Wno-delete-non-virtual-dtor -Wno-comment -fdiagnostics-show-option -Wno-long-long -pthread -fomit-frame-pointer -ffunction-sections -fdata-sections  -msse -msse2 -msse3 -fvisibility=hidden -fvisibility-inlines-hidden -Wno-shadow -Wno-parentheses -Wno-maybe-uninitialized -Wno-sign-promo -Wno-missing-declarations -Wno-deprecated -Wno-missing-declarations -Wno-shadow -Wno-unused-parameter -Wno-unused-local-typedefs -Wno-sign-compare -Wno-sign-promo -Wno-undef -Wno-ignored-qualifiers -Wno-extra -Wno-unused-function -Wno-deprecated-declarations -Wno-error=non-virtual-dtor -Wno-unused-parameter -Wno-undef -Wno-ignored-qualifiers -Wno-enum-compare -Wno-deprecated-declarations -Wno-invalid-offsetof -O2 -g -DNDEBUG -fPIC   -o CMakeFiles/opencv_dnn.dir/src/layers/permute_layer.cpp.o -c /cluster/apps/spack/var/spack/stage/opencv-3.4.1-zxmgmfmwzkooupmg24dzntqp2h2dqy3k/opencv-3.4.1/modules/dnn/src/layers/permute_layer.cpp
In file included from /cluster/spack/apps/linux-centos7-x86_64/gcc-4.8.5/cuda-8.0.61-mxdqob5lveng3f2utljrysrx4vjp7g3m/include/math_functions.hpp:1674:0,
                 from /cluster/apps/spack/var/spack/stage/opencv-3.4.1-zxmgmfmwzkooupmg24dzntqp2h2dqy3k/opencv-3.4.1/modules/dnn/src/layers/mvn_layer.cpp:46:
/cluster/spack/apps/linux-centos7-x86_64/gcc-4.8.5/cuda-8.0.61-mxdqob5lveng3f2utljrysrx4vjp7g3m/include/crt/func_macro.h:50:2: error: #error -- incorrect inclusion of a cudart header file
 #error -- incorrect inclusion of a cudart header file
  ^
```

### Steps to reproduce the issue

`spack install --no-checksum opencv@3.4.1+core+cuda+dnn+gtk+png+highgui+imgproc+shared+tiff+zlib~python~eigen ^cmake@3.8.0+ncurses+openssl ^protobuf@3.1.0+shared ^cuda@8.0.61 ^gtkplus@2.24.31+X ^libtiff@4.0.6 ^libpng@1.6.27 ^python@2.7.13+tk ^libpthread-stubs@0.3 ^glib@2.49.7+libmount ^xproto@7.0.29 ^libx11@1.6.3 ^zlib@1.2.11 ^flex@2.6.1 ^gettext@0.19.8.1+bzip2+curses+git+libunistring+libxml2+tar+xz ^util-linux@2.29.1 ^freetype@2.7 ^fontconfig@2.11.1 %gcc@4.8.5 arch=linux-centos7-x86-64`

### Information on your system

Plattform:  CentOS Linux release 7.4.1708 (Core) 
Kernel: 3.10.0-693.11.6.el7.x86_64

-----




Discussion:
opencv/opencv#11745

The opencv developers fixed this issue in a newer version of the code.
With the help of a OpenCV developer, I wrote a patch that solves this issue:

```
[spackapps@lo-login-02 opencv]$ cat dnn_cuda.patch 
--- opencv-3.4.1/modules/dnn/src/layers/mvn_layer.cpp.orig      2018-06-14 10:46:28.025703093 +0200
+++ opencv-3.4.1/modules/dnn/src/layers/mvn_layer.cpp   2018-06-14 10:40:56.266069123 +0200
@@ -43,7 +43,7 @@
 #include "../precomp.hpp"
 #include "layers_common.hpp"
 #include <opencv2/dnn/shape_utils.hpp>
-#include "math_functions.hpp"
+#include "../ocl4dnn/include/math_functions.hpp"
 #include "opencl_kernels_dnn.hpp"
 
 namespace cv
--- opencv-3.4.1/modules/dnn/src/ocl4dnn/src/math_functions.cpp.orig    2018-06-14 10:45:23.565162940 +0200
+++ opencv-3.4.1/modules/dnn/src/ocl4dnn/src/math_functions.cpp 2018-06-14 10:44:48.513412965 +0200
@@ -42,7 +42,7 @@
 
 #include "../../precomp.hpp"
 #include "common.hpp"
-#include "math_functions.hpp"
+#include "../include/math_functions.hpp"
 #include <vector>
 #include "opencl_kernels_dnn.hpp"
 
--- opencv-3.4.1/modules/dnn/src/ocl4dnn/src/ocl4dnn_conv_spatial.cpp.orig      2018-06-14 10:42:11.319534007 +0200
+++ opencv-3.4.1/modules/dnn/src/ocl4dnn/src/ocl4dnn_conv_spatial.cpp   2018-06-14 10:42:52.988236879 +0200
@@ -52,7 +52,7 @@
 #include "common.hpp"
 #include "ocl4dnn.hpp"
 #include "opencl_kernels_dnn.hpp"
-#include "math_functions.hpp"
+#include "../include/math_functions.hpp"
 #include "default_kernel_config.hpp"
 
 #if defined WIN32 || defined _WIN32
--- opencv-3.4.1/modules/dnn/src/ocl4dnn/src/ocl4dnn_inner_product.cpp.orig     2018-06-14 10:45:41.245036822 +0200
+++ opencv-3.4.1/modules/dnn/src/ocl4dnn/src/ocl4dnn_inner_product.cpp  2018-06-14 10:45:55.543934818 +0200
@@ -43,7 +43,7 @@
 #include "../../precomp.hpp"
 #include "common.hpp"
 #include "ocl4dnn.hpp"
-#include "math_functions.hpp"
+#include "../include/math_functions.hpp"
 
 #ifdef HAVE_OPENCL
 namespace cv { namespace dnn { namespace ocl4dnn {
[spackapps@lo-login-02 opencv]$
```

Resolving patch:
diff --git a/var/spack/repos/builtin/packages/opencv/package.py b/var/spack/repos/builtin/packages/opencv/package.py
--- a/var/spack/repos/builtin/packages/opencv/package.py
+++ b/var/spack/repos/builtin/packages/opencv/package.py
@@ -105,6 +105,11 @@ class Opencv(CMakePackage):
     variant('vtk', default=True, description='Activates support for VTK')
     variant('zlib', default=True, description='Build zlib from source')
 
+    # Patch to fix conflict between CUDA and OpenCV (reproduced with 3.3.0
+    # and 3.4.1) header file that have the same name.Problem is fixed in
+    # the current development branch of OpenCV. See #8461 for more information.
+    patch('dnn_cuda.patch', when='@3.3.0:3.4.1+cuda+dnn')
+
     depends_on('eigen~mpfr', when='+eigen', type='build')
 
     depends_on('zlib', when='+zlib')


Test patch: