Ninja generator for CUDA uses too many dependencies for CXX_SHARED_LIBRARY_DEVICE_LINKER
Short read:
When building a shared library containing CUDA code, Ninja build step to produce the cmake_device_link.obj (via CXX_SHARED_LIBRARY_DEVICE_LINKER__* rule) uses too many files as input (e.g. all the object files resulting from compiling *.cpp files). This is not needed and e.g. Visual Studio generator doesn't do that.
Long read:
I have a Windows shared library project containing CUDA code. When using CMake + Visual Studio 2017 to build, it builds just fine. But when using CMake + Ninja, the build fails when doing the device linking of cmake_device_link.obj because of "The command line is too long" error. Note that I'm already using CMake 3.15 (with the support for the response files). What I found out is that even when using nvcc response file (--options-file
), it seems to forward somewhere the contents as a console arguments.
So I tried to understand why the Visual Studio linking succeeds and Ninja fails. After some investigation, the difference seems to be in the input files taken for linking the cmake_device_link.obj.
- For the Visual Studio device-link (Task "CudaLink") the inputs are only the object files (*.obj) resulting from compilation of *.cu files
- For the Ninja device-link (build.ninja file) the inputs are both the object files (*.obj) resulting from compilation of .cu files AND the object files (.obj) resulting from compilation of *.cpp file (of the same library)
- But the *.obj files (of the *.cpp files) are only needed at the final step of linking the *.dll library (and not at the device-link stage)
Question:
Is this something that could be improved on the CMake level? E.g. to modify the inputs/dependencies of the device-link stage of Ninja generator?