cmake: fix clang build when CUDA is enabled (#4208)

Don't use the cxx_flags for .cu files so that -Wunreachable-code-break
and -Wunreachable-code-return would not be sent to to GCC which
doesn't understand them.
By default, nvcc uses gcc as the preprocessor even when Clang is used
for .c/.cpp.

This should fix the CUDA build with clang 14 (when gcc is used with nvcc).
With more complex configuration, it probably would be possible to use clang
itself (instead of nvcc) to compile CUDA code or use clang as the preprocessor for
nvcc. But these are probably to be quite rare use cases.
It seems that Clang 16 would require even more changes.
This commit is contained in:
Johannes Aalto 2023-12-04 10:44:18 +02:00
parent 4fa44e84ad
commit 4db34a3458

View file

@ -399,13 +399,17 @@ endif()
if (LLAMA_ALL_WARNINGS) if (LLAMA_ALL_WARNINGS)
if (NOT MSVC) if (NOT MSVC)
# warning_flags is common to C and C++ but not CUDA
set(warning_flags -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function) set(warning_flags -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function)
set(c_flags -Wshadow -Wstrict-prototypes -Wpointer-arith -Wmissing-prototypes -Werror=implicit-int -Werror=implicit-function-declaration) set(c_flags -Wshadow -Wstrict-prototypes -Wpointer-arith -Wmissing-prototypes -Werror=implicit-int -Werror=implicit-function-declaration)
set(cxx_flags -Wmissing-declarations -Wmissing-noreturn) # host_cxx_flags gets passed to nvcc after '-Xcompiler', i.e., it then gets sent to gcc (which is the default
set(host_cxx_flags "") # preprocessor for nvcc, even when clang would be used .c/.cpp files)
set(host_cxx_flags -Wmissing-declarations -Wmissing-noreturn)
if (CMAKE_C_COMPILER_ID MATCHES "Clang") if (CMAKE_C_COMPILER_ID MATCHES "Clang")
set(warning_flags ${warning_flags} -Wunreachable-code-break -Wunreachable-code-return) set(c_flags ${c_flags} -Wunreachable-code-break -Wunreachable-code-return)
# cxx_flags are used for C++ files but not for CUDA
set(cxx_flags -Wunreachable-code-break -Wunreachable-code-return)
set(host_cxx_flags ${host_cxx_flags} -Wmissing-prototypes -Wextra-semi) set(host_cxx_flags ${host_cxx_flags} -Wmissing-prototypes -Wextra-semi)
if ( if (
@ -440,7 +444,7 @@ endif()
if (NOT MSVC) if (NOT MSVC)
set(cuda_flags -Wno-pedantic) set(cuda_flags -Wno-pedantic)
endif() endif()
set(cuda_flags ${cxx_flags} -use_fast_math ${cuda_flags}) set(cuda_flags -use_fast_math ${cuda_flags})
list(JOIN host_cxx_flags " " cuda_host_flags) # pass host compiler flags as a single argument list(JOIN host_cxx_flags " " cuda_host_flags) # pass host compiler flags as a single argument
if (NOT cuda_host_flags STREQUAL "") if (NOT cuda_host_flags STREQUAL "")