Repository: opencv
Issue: Ninja-based build fails on Windows when building with CUDA
Labels: bug,priority: low,category: build/install,category: gpu/cuda (contrib)

##### System information (version)
- OpenCV => 3.1
- Operating System / Platform => Windows 64 Bit
- Compiler => Visual Studio 12.0
##### Detailed description

I am running into problems building OpenCV 3.1.0 with CUDA support on Windows. The problem seems to stem from the path to the CUDA libraries having spaces in it:

```
[740/1182] Linking CXX shared library bin\opencv_world310.dll
FAILED: bin/opencv_world310.dll lib/opencv_world310.lib
cmd.exe /C "cd . && "C:\Program Files (x86)\CMake\bin\cmake.exe" -E vs_link_dll
--intdir=modules\world\CMakeFiles\opencv_world.dir --manifests  -- C:\PROGRA~2\M
ICROS~2.0\VC\bin\amd64\link.exe /nologo @CMakeFiles/opencv_world.rsp  /out:bin\o
pencv_world310.dll /implib:lib\opencv_world310.lib /pdb:bin\opencv_world310.pdb
/dll /version:3.1 /machine:x64 /INCREMENTAL:NO /NODEFAULTLIB:atlthunk.lib /NODEF
AULTLIB:atlsd.lib /NODEFAULTLIB:libcmt.lib /DEBUG   && cmd.exe /C "cd /D C:\User
s\rjray\opencv\build\modules\world && "C:\Program Files (x86)\CMake\bin\cmake.ex
e" -E copy_if_different C:/Users/rjray/opencv/opencv/3rdparty/ffmpeg/opencv_ffmp
eg_64.dll C:/Users/rjray/opencv/build/bin/opencv_ffmpeg310_64.dll""
LINK : warning LNK4044: unrecognized option '/LC:/Program'; ignored
LINK : fatal error LNK1181: cannot open input file 'Files/NVIDIA.obj'
LINK failed. with 1181
ninja: build stopped: subcommand failed.
```

We are using Ninja as the build system, with CMake using Ninja as the target (`-GNinja`). The command used to configure CMake is:

```
cmake \
    -GNinja \
    -DCMAKE_BUILD_TYPE=Release \
    -DINSTALL_TESTS=ON \
    -DWITH_OPENCL=OFF \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTS=ON \
    -DBUILD_opencv_python2=OFF \
    -DBUILD_opencv_python3=OFF \
    -DBUILD_PERF_TESTS=ON \
    -DWITH_FFMPEG=ON \
    -DINSTALL_CREATE_DISTRIB=ON \
    -DENABLE_SSE=ON \
    -DENABLE_SSE2=ON \
    -DWITH_CUDA=ON \
    "-DCUDA_TOOLKIT_ROOT_DIR=C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0" \
    -DBUILD_opencv_java=OFF \
    -DWITH_MSMF=OFF \
    -DWITH_TBB=OFF \
    -DWITH_1394=OFF \
    -DWITH_VFW=OFF \
    -DBUILD_DOCS=OFF \
    -DBUILD_EXAMPLES=OFF \
    -DWITH_VTK=OFF \
    ..\opencv
```

(Actual command is one line, the backslashes and line-breaks are because the above comes from a guide I am writing, and are there for readability.)

I have also tried the above command with the quoting of the `CUDA_TOOLKIT_ROOT_DIR` element escaped:

```
^"-DCUDA_TOOLKIT_ROOT_DIR=C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0^"
```

This did not help. Our build system is able to build the 2.4.13 source without a problem on the same Windows machine with the same toolchain, so I suspect that this is something introduced later than the 2.4 branch. Our builds of 2.4.13 are using the same toolkit-root declaration as above (the quoted-escaped version of the line is what our build system is using).

The error message from linking implies that the toolkit root path is being passed to a `/L` option without escaping the spaces in the path. In the files generated by CMake for 2.4.13, the reference to the toolkit dir had the spaces escaped with `$` characters, but I cannot find the declaration in the files generated for 3.1.0, so I can't see how it is being declared.

I am including the files CMakeCache.txt and build.ninja (as `build.ninja.txt`, so that GitHub will upload it), in case they can offer some insight.

[CMakeCache.txt](https://github.com/opencv/opencv/files/457954/CMakeCache.txt)

[build.ninja.txt](https://github.com/opencv/opencv/files/457958/build.ninja.txt)