CMake + CUDA + Clang fails

Hello,

I’m trying to compile this project with Clang instead of NVCC. I am clueless why it is not working and could not find a lot of information online.

I’m running CMake 3.26.4, Clang 15.0.7 and CUDA 12.1.
Here is what’s happening:

$ cmake -DCMAKE_BUILD_TYPE=Release -DGPU=ON -Bbuild/gpu-release -DCMAKE_CUDA_COMPILER=clang++ -DCMAKE_CXX_COMPILER=clang++ --trace --trace-redirect=trace.log --trace-expand
Running with trace output on.
Trace will be written to trace.log
Running with expanded trace output on.
-- The CUDA compiler identification is unknown
-- The CXX compiler identification is Clang 15.0.7
-- Detecting CUDA compiler ABI info
CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
Missing variable is:
_CMAKE_CUDA_WHOLE_FLAG
CMake Error at /usr/share/cmake-3.26/Modules/CMakeDetermineCompilerABI.cmake:57 (try_compile):
  Failed to generate test project build system.
Call Stack (most recent call first):
  /usr/share/cmake-3.26/Modules/CMakeTestCUDACompiler.cmake:19 (CMAKE_DETERMINE_COMPILER_ABI)
  CMakeLists.txt:8 (project)


-- Configuring incomplete, errors occurred!

I can’t attach the trace.log because I’m a new user so here it is.
Thanks for any help.

I don’t think that CMake supports clang++ as a CUDA compiler (hence the “CUDA compiler identification is unknown” output).

This issue seems relevant.

Cc: @robert.maynard

According to this, should be supported from CMake 3.18 onwards?

Interesting. I wonder if CUDACC=clang++ is the right thing or whether there’s another wrapper that is expected. Anyways, Rob knows way more about CUDA than I do (I basically just tell CI to turn it on and build/test it).

You need to specify what GPU architecture to compile for via CMAKE_CUDA_ARCHITECTURES.

The error logs indicate that is the failure:

Selected multilib: .;@m64
Found CUDA installation: /usr/local/cuda-12.1, version 
clang: warning: CUDA version is newer than the latest supported version 11.5 [-Wunknown-cuda-version]
clang: error: GPU arch sm_30 is supported by CUDA versions between 7.0 and 11.0 (inclusive), but installation at /usr/local/cuda-12.1 is ; use '--cuda-path' to specify a different CUDA install, pass a different GPU arch with '--cuda-gpu-arch', or pass '--no-cuda-version-check'

Since you are using CUDA 12.1 which doesn’t support the ‘default’ deprecated value of sm_30

Thanks. But why does this flag need to be specified? In the CMakeLists I have:

if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
  set(CMAKE_CUDA_ARCHITECTURES native)
endif()

Also, I ran into other errors but not because of CMake, just in case someone needs to do the same, I had to install sudo apt install libstdc++-12-dev and fix a bug in Clang by modifying /usr/lib/clang/15.0.7/include/__clang_cuda_texture_intrinsics.h (see this commit).
Anyways, it seems that CUDA 12.1 is only supported starting with Clang 17 so I will have to try with it.

Is that code before project(… CUDA) or the enable_language(CUDA) call?

It is before, see this CMakeLists

Don’t set it to native till after compiler detection. clang needs an explicit value since it has no intrinsic support for native.

I hit this as well with clang 18 from git pulled on 8/18.

CMake Error: Error required internal CMake variable not set, cmake may not be built correctly.
Missing variable is:
_CMAKE_CUDA_WHOLE_FLAG

In my case in addition to adding to the cmake command line -DCMAKE_CUDA_ARCHITECTURES=75 I also had to add -DCMAKE_CUDA_FLAGS=-std=c++17. The latter being due to the use of -std=gnu++17 by default by clang and inherited by cmake.

Apparently the error complaining about _CMAKE_CUDA_WHOLE_FLAG comes up when the clang compiler fails on cmake’s internal try compile, which can be for a number of different reasons.

fedora 37, cmake 3.26.2