How to make CMake include all cl.exe switches in double quotes when generating nvcc command

  c++, cl, cl.exe, cmake, cuda

I am trying to compile a project with CMake and CUDA.

I get an error as described here because the nvcc command that gets generated tries to pass with -forward-unknown-to-host-compiler some cl.exe switches which make nvcc produce an error as they start with / and not with - as it expects.

This is my CUDA-related CMake code (the error is the same even without the set_target_properties statements in case you wonder):

if(USE_CUDA)
  enable_language(CUDA)
  target_sources(mylib PRIVATE mylib_gpu.cu)

  target_compile_definitions(mylib PUBLIC -DUSE_CUDA)
  set_target_properties(mylib PROPERTIES CUDA_RESOLVE_DEVICE_SYMBOLS ON)
  
  set_target_properties(mylib PROPERTIES CUDA_SEPARABLE_COMPILATION ON)
endif(USE_CUDA)

And this is the error + command:

FAILED: libs/mylib/CMakeFiles/mylib.dir/mylib_gpu.cu.obj
C:PROGRA~1NVIDIA~2CUDAv11.2binnvcc.exe -forward-unknown-to-host-compiler -DUSE_CUDA  -arch=sm_75 -Xcompiler="-MDd -Zi -Ob0 -Od /RTC1" /D_CRT_SECURE_NO_WARNINGS /EHsc -std=c++17 -MD -MT libsmylibCMakeFilesmylib.dirmylib_gpu.cu.obj -MF libsmylibCMakeFilesmylib.dirmylib_gpu.cu.obj.d -x cu -dc ..libsmylibmylib_gpu.cu -o libsmylibCMakeFilesmylib.dirmylib_gpu.cu.obj -Xcompiler=-FdlibsmylibCMakeFilesmylib.dirmylib.pdb,-FS
nvcc fatal   : A single input file is required for a non-link phase when an outputfile is specified

As far as I understand this could be easily solved if instead of

-Xcompiler="-MDd -Zi -Ob0 -Od /RTC1" /D_CRT_SECURE_NO_WARNINGS /EHsc

CMake produced the following command:

-Xcompiler="-MDd -Zi -Ob0 -Od /RTC1 /D_CRT_SECURE_NO_WARNINGS /EHsc"

so that nvcc -forward-unknown-to-host-compiler would not be bothered by the cl.exe switches starting with /.

Is there an easy way to make CMake fix this behaviour?

Source: Windows Questions C++

LEAVE A COMMENT