cuda_add_library with xcode fails for custom file extensions
I'm having trouble building CUDA
files with CMake
using a *.cu.cc
file extension (as used in tensorflow) in combination with cuda_add_library
and an Xcode
generator. The CUDA
files are "tagged" using set_source_files_properties(${gpu_source_files} PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ)
.
I have created a minimal project to reproduce the issue with links to full log output here (reproduced inline below):
https://github.com/headupinclouds/cmake_cuda_test
Details:
cmake version 3.10.0
Xcode 8.1 (Build version 8B62)
OSX: 10.12.2
CUDA 9.0
This project simply creates a library using cuda_add_library()
with a CUDA_EXAMPLE_USE_CC_EXT
option to control the operative file extension for otherwise identical files. If the option is set to ON
, then a *.cu.cc
extension will be used, and compilation fails. If it is OFF
, then a more standard *.cu
extension is used, and compilation succeeds.
In the first case there seems to be a first pass from nvcc
that works as expected, and a second unexpected clang
pass that fails with errors indicating it doesn't know about CUDA
. The second stage CUDA
pass doesn't occur when using the more standard *.cu
extension.
The basic project layout is shown below:
CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake/modules")
project(cuda_test)
# NOTE: This doesn't work with xcode generators
#enable_language("CUDA")
find_package(CUDA "9.0" REQUIRED)
option(CUDA_EXAMPLE_USE_CC_EXT "Use CC extensions for CUDA files" ON)
set(cpu_source_files cpu.cc)
if(CUDA_EXAMPLE_USE_CC_EXT)
set(gpu_source_files gpu.cu.cc)
else()
set(gpu_source_files gpu.cu)
endif()
set_source_files_properties(${gpu_source_files} PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ)
cuda_add_library(cuda_lib STATIC ${gpu_source_files} ${cpu_source_files})
add_executable(cuda_exe main.cc)
target_link_libraries(cuda_exe cuda_lib)
target_compile_definitions(cuda_lib PUBLIC USE_CUDA=1)
gpu.cu
#include <iostream>
#include "gpu.hpp"
void printCudaVersion()
{
std::cout << "CUDA Compiled version: " << __CUDACC_VER__ << std::endl;
int runtime_ver;
cudaRuntimeGetVersion(&runtime_ver);
std::cout << "CUDA Runtime version: " << runtime_ver << std::endl;
int driver_ver;
cudaDriverGetVersion(&driver_ver);
std::cout << "CUDA Driver version: " << driver_ver << std::endl;
}
gpu.cu.cc
#include "gpu.cu"
main.cc
#include <iostream>
#include "gpu.hpp"
int main()
{
printCudaVersion();
return 0;
}
This build:
cmake -GXcode -H. -B_builds/xcode -DCMAKE_VERBOSE_MAKEFILE=ON -DCUDA_TOOLKIT_ROOT_DIR=/Developer/NVIDIA/CUDA-9.0 -DCUDA_EXAMPLE_USE_CC_EXT=ON && cd _builds/xcode && cmake --build .
fails with the following log
This build:
cmake -GXcode -H. -B_builds/xcode2 -DCMAKE_VERBOSE_MAKEFILE=ON -DCUDA_TOOLKIT_ROOT_DIR=/Developer/NVIDIA/CUDA-9.0 -DCUDA_EXAMPLE_USE_CC_EXT=OFF && cd _builds/xcode2 && cmake --build .
succeeds with the following log
It seems set_source_files_properties(${gpu_source_files} PROPERTIES CUDA_SOURCE_PROPERTY_FORMAT OBJ)
isn't sufficient. Is there any additional setup that may help support custom CUDA
extensions?
My understanding is that using enable_language("CUDA")
will be a preferred solution in the future. I noticed this isn't implemented yet for Xcode generators (at least), so I'm not sure if it has a similar problem.