Provide target libraries for cuda libraries
With the new builtin support for compiling CUDA programs, it would be nice to have targets pre-defined for the various CUDA libraries. Currently, if I have a library or executable that links against the cuda runtime, libnvcuvid, cuBLAS, etc., I need to either either change one of it's source file's language to be "CUDA" via set_source_files_properties(file.cpp PROPERTIES LANGUAGE CUDA)
(which is kind of strange since it doesn't need to be compiled with nvcc so I wouldn't think of it as a "CUDA language" source) or use FindCUDA or similar functionality to discover the directory libcudart lives in and add that and the include directory (from ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}
) manually. This also means I need to list CUDA as a language in my project even if I don't have anything that requires nvcc to compile.
So in essence it'd be nice to go from something like this (which is hacky, see comment below from henryiii for better/more correct but more verbose way):
cmake_minimum_required(VERSION 3.8)
project(cmake_cuda_test LANGUAGES CXX CUDA)
add_executable(cmake_cuda_test cmake_cuda_test.cpp)
# below is not needed in this particular case, but is needed if there are other .cpp files
# that include the lib headers but are not set to CUDA language.
target_include_directories(cmake_cuda_test PUBLIC ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
set_source_files_properties(cmake_cuda_test.cpp PROPERTIES LANGUAGE CUDA)
To something like this:
cmake_minimum_required(VERSION 3.8)
project(cmake_cuda_test LANGUAGES CXX)
add_executable(cmake_cuda_test cmake_cuda_test.cpp)
target_link_libraries(cmake_cuda_test CUDA::cudart)
I could also include this target in target_link_libraries
for a target with CUDA sources and not have to manually add ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}
. Or even better it gets included automatically in that case.
It'd be reasonable to require a find_package(CUDA)
, in which case maybe FindCUDA just needs to be revamped to create the appropriate imported targets and not duplicate functionality now included in the CUDA language support (except for backwards compatibility). In this case it should coordinate with the CUDA language code to link against the same runtime (cudart
vs cudart_static
) in the case that the imported target (CUDA::cudart
or whatever) is in the link_libraries of a target that has CUDA sources.