[libcxx-commits] [clang] [compiler-rt] [libcxx] [libunwind] [llvm] [openmp] [cmake] switch to CMake's native `check_{compiler, linker}_flag` (PR #96171)
Ryan Prichard via libcxx-commits
libcxx-commits at lists.llvm.org
Wed Jul 24 21:24:48 PDT 2024
rprichard wrote:
The previous `llvm_check_compiler_linker_flag` passes the same flag to both the compile step and the linker step. Apparently, it's like a combined `check_compiler_flag` and `check_linker_flag`. CMake apparently doesn't implement this directly (https://stackoverflow.com/questions/78503180/cmake-how-to-check-for-a-compiler-and-linker-flag-at-the-same-time), but it's not too hard to implement it ourselves. The LLVM implementation is much better than the one proposed on StackOverflow.
If we just want to do a cleanup, then `llvm_check_compiler_linker_flag` can be simplified a lot:
```cmake
include(CMakePushCheckState)
include(CheckCompilerFlag)
function(llvm_check_compiler_linker_flag lang flag out_var)
# If testing a flag with check_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_compiler_flag("${lang}" "" ${out_var})
cmake_pop_check_state()
endfunction()
```
Almost all the uses of `llvm_check_compiler_linker_flag` that https://github.com/llvm/llvm-project/pull/96171/commits/1515c2cd16911e1297c57fb69d4bb4921a40b8ab switches to `check_compiler_flag` are actually testing a linker flag, so they need to use `check_linker_flag` instead.
There were a few flags that I wasn't familiar with, so maybe they could be `check_compiler_flag`, or maybe we actually need to do both:
* Darwin's `-fapplication-extension`
* `/SAFESEH` to MSVC in openmp/runtime/cmake/config-ix.cmake
* ICC's `-static-intel`
* ICC's `-no-intel-extensions`
Ignoring those cases, the Android tests pass with this PR after switching `llvm_check_compiler_linker_flag` call sites to `check_linker_flag`.
https://github.com/llvm/llvm-project/pull/96171
More information about the libcxx-commits
mailing list