I'm writing some code that can be compiled as C++ or as CUDA. In the latter case, it makes use of CUDA kernels, in the former it just runs conventional code.
Having created a file named test.cpp, I can compile it manually thus:
g++ test.cpp # build as C++ with GCC
nvcc -x cu test.cpp # build as CUDA with NVCC
where -x cu
tells nvcc that although it's a .cpp extension, I'd like it to treat it as CUDA.
So far, so good.
However, when I migrate to using CMake, I don't know how to do the same thing. That is: how to ask CMake to compile the .cpp file with NVCC, rather than GCC.
cmake_minimum_required(VERSION 3.9)
project(cuda_test LANGUAGES CUDA CXX)
add_executable(cuda_test test.cpp) # builds with GCC
If I create a symlink to the original file:
ln -s test.cpp test.cu
then change CMakeLists.txt:
add_executable(cuda_test test.cu) # builds with NVCC
But I'd like to be able to specify the equivalent of NVCC's -x
switch within CMake, rather than playing games with extensions. Something like:
set_target_properties(cuda_test PROPERTIES FORCE_LANGUAGE CUDA)
or even
set_target_properties(test.cpp PROPERTIES FORCE_LANGUAGE CUDA)
Does such an incantation exist?
By default, CMake chooses compiler for a source file according to the file's extension. But you may force CMake to use the compiler you want by setting LANGUAGE property for a file:
(This just calls CUDA compiler for a file using normal compiler options. You still need to pass additional options for that compiler, so it could work with the unusual file's extension.)