From 7017e6c9cfd2de3122ce9528f338a97d61e96373 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Fri, 21 Jan 2022 19:21:19 -0500 Subject: [cmake] Partially deduplicate `{llvm,compiler_rt}_check_linker_flag` for runtime libs and llvm We previously had a few varied definitions of this floating around. I had tried to make the one installed with LLVM handle all the cases, and then made the others use it, but this ran into issues with `HandleOutOfTreeLLVM` not working for compiler-rt, and also `CMAKE_EXE_LINKER_FLAGS` not working right without `CMP0056` set to the new behavior. My compromise solution is this: - No not completely deduplicate: the runtime libs will instead use a version that still exists as part of the internal and not installed common shared CMake utilities. This avoids `HandleOutOfTreeLLVM` or a workaround for compiler-rt. - Continue to use `CMAKE_REQUIRED_FLAGS`, which effects compilation and linking. Maybe this is unnecessary, but it's safer to leave that as a future change. Also means we can avoid `CMP0056` for now, to try out later, which is good incrementality too. - Call it `llvm_check_compiler_linker_flag` since it, in fact is about both per its implementation (before and after this patch), so there is no name collision. In the future, we might still enable CMP0056 and make compiler-rt work with HandleOutOfTreeLLVM, which case we delete `llvm_check_compiler_flag` and go back to the old way (as these are, in fact, linking related flags), but that I leave for someone else as future work. The original issue was reported to me in https://reviews.llvm.org/D116521#3248117 as D116521 made clang and LLVM use the common cmake utils. Reviewed By: sebastian-ne, phosek, #libunwind, #libc, #libc_abi, ldionne Differential Revision: https://reviews.llvm.org/D117537 --- cmake/Modules/CheckLinkerFlag.cmake | 17 ------------ cmake/Modules/LLVMCheckCompilerLinkerFlag.cmake | 35 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 17 deletions(-) delete mode 100644 cmake/Modules/CheckLinkerFlag.cmake create mode 100644 cmake/Modules/LLVMCheckCompilerLinkerFlag.cmake (limited to 'cmake') diff --git a/cmake/Modules/CheckLinkerFlag.cmake b/cmake/Modules/CheckLinkerFlag.cmake deleted file mode 100644 index 722fe5b1b8ea..000000000000 --- a/cmake/Modules/CheckLinkerFlag.cmake +++ /dev/null @@ -1,17 +0,0 @@ -include(CMakePushCheckState) -include(CheckCCompilerFlag) - -function(llvm_check_linker_flag flag dest) - # If testing a flag with check_c_compiler_flag, it gets added to the compile - # command only, but not to the linker command in that test. If the flag - # is vital for linking to succeed, the test would fail even if it would - # have succeeded if it was included on both commands. - # - # Therefore, try adding the flag to CMAKE_REQUIRED_FLAGS, which gets - # added to both compiling and linking commands in the tests. - - cmake_push_check_state() - set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${flag}") - check_c_compiler_flag("" ${dest}) - cmake_pop_check_state() -endfunction() diff --git a/cmake/Modules/LLVMCheckCompilerLinkerFlag.cmake b/cmake/Modules/LLVMCheckCompilerLinkerFlag.cmake new file mode 100644 index 000000000000..f61ec0585f9a --- /dev/null +++ b/cmake/Modules/LLVMCheckCompilerLinkerFlag.cmake @@ -0,0 +1,35 @@ +include(CMakePushCheckState) + +include(CheckCompilerFlag OPTIONAL) + +if(NOT COMMAND check_compiler_flag) + include(CheckCCompilerFlag) + include(CheckCXXCompilerFlag) +endif() + +function(llvm_check_compiler_linker_flag lang flag out_var) + # If testing a flag with check_c_compiler_flag, it gets added to the compile + # command only, but not to the linker command in that test. If the flag + # is vital for linking to succeed, the test would fail even if it would + # have succeeded if it was included on both commands. + # + # Therefore, try adding the flag to CMAKE_REQUIRED_FLAGS, which gets + # added to both compiling and linking commands in the tests. + + cmake_push_check_state() + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${flag}") + if(COMMAND check_compiler_flag) + check_compiler_flag("${lang}" "" ${out_var}) + else() + # Until the minimum CMAKE version is 3.19 + # cmake builtin compatible, except we assume lang is C or CXX + if("${lang}" STREQUAL "C") + check_c_compiler_flag("" ${out_var}) + elseif("${lang}" STREQUAL "CXX") + check_cxx_compiler_flag("" ${out_var}) + else() + message(FATAL_ERROR "\"${lang}\" is not C or CXX") + endif() + endif() + cmake_pop_check_state() +endfunction() -- cgit v1.2.1