8000 cmake : detect host compiler and cuda compiler separately · Pints-AI/llama.cpp@a81a34a · GitHub
[go: up one dir, main page]

Skip to content

Commit a81a34a

Browse files
committed
cmake : detect host compiler and cuda compiler separately
1 parent abacb27 commit a81a34a

File tree

1 file changed

+70
-31
lines changed

1 file changed

+70
-31
lines changed

CMakeLists.txt

Lines changed: 70 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -397,54 +397,93 @@ if (LLAMA_HIPBLAS)
397397
endif()
398398
endif()
399399

400+
function(get_flags ccid ccver)
401+
set(c_flags "")
402+
set(cxx_flags "")
403+
404+
if (ccid MATCHES "Clang")
405+
set(c_flags -Wunreachable-code-break -Wunreachable-code-return)
406+
set(cxx_flags -Wunreachable-code-break -Wunreachable-code-return -Wmissing-prototypes -Wextra-semi)
407+
408+
if (
409+
(ccid STREQUAL "Clang" AND ccver VERSION_GREATER_EQUAL 3.8.0) OR
410+
(ccid STREQUAL "AppleClang" AND ccver VERSION_GREATER_EQUAL 7.3.0)
411+
)
412+
set(c_flags ${c_flags} -Wdouble-promotion)
413+
endif()
414+
elseif (ccid STREQUAL "GNU")
415+
set(c_flags -Wdouble-promotion)
416+
set(cxx_flags -Wno-array-bounds)
417+
418+
if (ccver VERSION_GREATER_EQUAL 7.1.0)
419+
set(cxx_flags ${cxx_flags} -Wno-format-truncation)
420+
endif()
421+
if (ccver VERSION_GREATER_EQUAL 8.1.0)
422+
set(cxx_flags ${cxx_flags} -Wextra-semi)
423+
endif()
424+
endif()
425+
426+
set(gf_c_flags ${c_flags} PARENT_SCOPE)
427+
set(gf_cxx_flags ${cxx_flags} PARENT_SCOPE)
428+
endfunction()
429+
400430
if (LLAMA_ALL_WARNINGS)
401431
if (NOT MSVC)
402432
set(warning_flags -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function)
403433
set(c_flags -Wshadow -Wstrict-prototypes -Wpointer-arith -Wmissing-prototypes -Werror=implicit-int -Werror=implicit-function-declaration)
404434
set(cxx_flags -Wmissing-declarations -Wmissing-noreturn)
405-
set(host_cxx_flags "")
406435

407-
if (CMAKE_C_COMPILER_ID MATCHES "Clang")
408-
set(warning_flags ${warning_flags} -Wunreachable-code-break -Wunreachable-code-return)
409-
set(host_cxx_flags ${host_cxx_flags} -Wmissing-prototypes -Wextra-semi)
410-
411-
if (
412-
(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 3.8.0) OR
413-
(CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 7.3.0)
414-
)
415-
set(c_flags ${c_flags} -Wdouble-promotion)
416-
endif()
417-
elseif (CMAKE_C_COMPILER_ID STREQUAL "GNU")
418-
set(c_flags ${c_flags} -Wdouble-promotion)
419-
set(host_cxx_flags ${host_cxx_flags} -Wno-array-bounds)
436+
get_flags(${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION})
420437

421-
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 7.1.0)
422-
set(host_cxx_flags ${host_cxx_flags} -Wno-format-truncation)
423-
endif()
424-
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 8.1.0)
425-
set(host_cxx_flags ${host_cxx_flags} -Wextra-semi)
426-
endif()
427-
endif()
438+
set(c_flags ${c_flags} ${warning_flags})
439+
set(cxx_flags ${cxx_flags} ${warning_flags})
440+
add_compile_options("$<$<COMPILE_LANGUAGE:C>:${c_flags} ${gf_c_flags}>"
441+
"$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags} ${gf_cxx_flags}>")
428442
else()
429443
# todo : msvc
444+
set(c_flags "")
445+
set(cxx_flags "")
430446
endif()
447+
endif()
431448

432-
set(c_flags ${c_flags} ${warning_flags})
433-
set(cxx_flags ${cxx_flags} ${warning_flags})
434-
add_compile_options("$<$<COMPILE_LANGUAGE:C>:${c_flags}>"
435-
"$<$<COMPILE_LANGUAGE:CXX>:${cxx_flags}>"
436-
"$<$<COMPILE_LANGUAGE:CXX>:${host_cxx_flags}>")
449+
set(cuda_flags ${cxx_flags} -use_fast_math)
450+
if (NOT MSVC)
451+
set(cuda_flags ${cuda_flags} -Wno-pedantic)
452+
endif()
437453

454+
set(nvcc_cmd ${CMAKE_CUDA_COMPILER} .c)
455+
if (NOT CMAKE_CUDA_HOST_COMPILER STREQUAL "")
456+
set(nvcc_cmd ${nvcc_cmd} -ccbin ${CMAKE_CUDA_HOST_COMPILER})
438457
endif()
439458

440-
if (NOT MSVC)
441-
set(cuda_flags -Wno-pedantic)
459+
execute_process(
460+
COMMAND ${nvcc_cmd} -Xcompiler --version
461+
OUTPUT_VARIABLE cuda_ccfullver
462+
ERROR_QUIET
463+
)
464+
465+
if (NOT cuda_ccfullver MATCHES clang)
466+
set(cuda_ccid "GNU")
467+
execute_process(
468+
COMMAND ${nvcc_cmd} -Xcompiler "-dumpfullversion -dumpversion"
469+
OUTPUT_VARIABLE cuda_ccver
470+
ERROR_QUIET
471+
)
472+
else()
473+
if (cuda_ccfullver MATCHES Apple)
474+
set(cuda_ccid "AppleClang")
475+
else()
476+
set(cuda_ccid "Clang")
477+
endif()
478+
string(REGEX REPLACE "^.* version ([0-9.]*).*$" "\\1" cuda_ccver ${cuda_ccfullver})
442479
endif()
443-
set(cuda_flags ${cxx_flags} -use_fast_math ${cuda_flags})
444480

445-
list(JOIN host_cxx_flags " " cuda_host_flags) # pass host compiler flags as a single argument
481+
message("-- CUDA host compiler is " ${cuda_ccid} " " ${cuda_ccver})
482+
483+
get_flags(${cuda_ccid} ${cuda_ccver})
484+
list(JOIN gf_cxx_flags " " cuda_cxx_flags) # pass host compiler flags as a single argument
446485
if (NOT cuda_host_flags STREQUAL "")
447-
set(cuda_flags ${cuda_flags} -Xcompiler ${cuda_host_flags})
486+
set(cuda_flags ${cuda_flags} -Xcompiler ${cuda_cxx_flags})
448487
endif()
449488

450489
add_compile_options("$<$<COMPILE_LANGUAGE:CUDA>:${cuda_flags}>")

0 commit comments

Comments
 (0)
0