"FLAGS" literal is being output in link.txt, causing error

Hello, I’m seeing an odd behavior when trying to build a Shared CUDA library using clang++ with CMake.

Here’s a simple reproducible example if you wanna try it out yourself: GitHub - DavidRV00/cmake-clang-cuda-bug-repro: Simple reproducible example for a CMake + Clang++ + Cuda shared library bug

I have a CMakeLists.txt like this:

cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES)
	set(CMAKE_CUDA_ARCHITECTURES 86)
endif()
PROJECT(bugrepro LANGUAGES C CXX CUDA)

add_library(mycudalib SHARED kernel.cu)

In kernel.cu there is just an empty function (pretty sure it doesn’t matter what’s in there).

I run cmake like this:

cmake \
    -DCUDAToolkit_ROOT=/opt/cuda   \
    -DCMAKE_CUDA_COMPILER=clang++  \
    -S . -B ./build

And then I attemt to build with make with this command:

make -C ./build

But I get this error:

make: Entering directory '/home/.../build'
[ 50%] Linking CUDA shared library libmycudalib.so
clang++: error: no such file or directory: 'FLAGS'
make[2]: *** [CMakeFiles/mycudalib.dir/build.make:101: libmycudalib.so] Error 1
make[1]: *** [CMakeFiles/Makefile2:87: CMakeFiles/mycudalib.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
make: Leaving directory '/home/.../build'

With some investigation, I see that this is caused by the literal string “FLAGS” being output into the clang command in ./build/CMakeFiles/mycudalib.dir/link.txt:

/usr/bin/clang++ FLAGS -fPIC -Xlinker --dependency-file=CMakeFiles/mycudalib.dir/link.d -shared -Wl,-soname,libmycudalib.so -o libmycudalib.so CMakeFiles/mycudalib.dir/kernel.cu.o  -lcudadevrt -lcudart_static -lrt -lpthread -ldl  -L"/opt/cuda/lib64"

And we can verify this is the problem by removing that little “FLAGS” string and retrying:

sed -i 's/FLAGS //g' ./build/CMakeFiles/mycudalib.dir/link.txt
make -C ./build # Success!

So, that works as a workaround, but I don’t believe it should be doing that.

Something else to note, this only happens when building as a Shared library. If I get rid of “SHARED” in the last line of the CMakeLists.txt, we avoid this error as well.

I’m using cmake version 3.31.3-dirty, artix linux.

Anybody have thoughts on this? Is this in need of a bug report?

Reasonable chance this is related to MR 9885. That change added the <FLAGS> placeholder to the Clang CUDA flags for executables and shared libraries.

Thanks, that does seem reasonably likely. Although it doesn’t really look to me like there’s a problem in that commit itself; I’d bet it’s something to do with how that change interacts with the already-existing placeholder expansion logic.

I’ve spent a little time digging into the code, but I’m not familiar with the CMake codebase and I’m starting to a feel a little out of my depth.

I’ll submit a bug report.