[cmake] switch to CMake's native check_{compiler,linker}_flag (#96171)

Broken out from #93429

Somewhat closing the loop opened by 7017e6c9cf.

Co-authored-by: Ryan Prichard <rprichard@google.com>
This commit is contained in:
h-vetinari
2024-08-01 01:31:13 +11:00
committed by GitHub
parent 9b017db6be
commit 89946bda5e
9 changed files with 17 additions and 64 deletions

View File

@@ -69,7 +69,7 @@ export_executable_symbols_for_plugins(clang-repl)
# gold. This flag tells the linker to build a PLT for the full address range.
# Linkers without this flag are assumed to support proper PLTs by default.
set(flag_long_plt "-Wl,--long-plt")
llvm_check_linker_flag(CXX ${flag_long_plt} HAVE_LINKER_FLAG_LONG_PLT)
check_linker_flag(CXX ${flag_long_plt} HAVE_LINKER_FLAG_LONG_PLT)
if(HAVE_LINKER_FLAG_LONG_PLT)
target_link_options(clang-repl PRIVATE ${flag_long_plt})
endif()

View File

@@ -107,7 +107,7 @@ endif()
if(CLANG_ORDER_FILE AND
(LLVM_LINKER_IS_APPLE OR LLVM_LINKER_IS_GOLD OR LLVM_LINKER_IS_LLD))
include(LLVMCheckLinkerFlag)
include(CheckLinkerFlag)
if (LLVM_LINKER_IS_APPLE OR (LLVM_LINKER_IS_LLD AND APPLE))
set(LINKER_ORDER_FILE_OPTION "-Wl,-order_file,${CLANG_ORDER_FILE}")
@@ -118,7 +118,7 @@ if(CLANG_ORDER_FILE AND
endif()
# This is a test to ensure the actual order file works with the linker.
llvm_check_linker_flag(CXX ${LINKER_ORDER_FILE_OPTION} LINKER_ORDER_FILE_WORKS)
check_linker_flag(CXX ${LINKER_ORDER_FILE_OPTION} LINKER_ORDER_FILE_WORKS)
# Passing an empty order file disables some linker layout optimizations.
# To work around this and enable workflows for re-linking when the order file

View File

@@ -1,14 +1,8 @@
include(CMakePushCheckState)
include(CheckCompilerFlag OPTIONAL)
if(NOT COMMAND check_compiler_flag)
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
endif()
include(CheckCompilerFlag)
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
# 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.
@@ -18,18 +12,6 @@ function(llvm_check_compiler_linker_flag lang flag out_var)
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()
check_compiler_flag("${lang}" "" ${out_var})
cmake_pop_check_state()
endfunction()

View File

@@ -1,6 +1,5 @@
include(CMakePushCheckState)
include(AddLLVM)
include(LLVMCheckCompilerLinkerFlag)
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
include(CheckIncludeFiles)
@@ -15,7 +14,7 @@ include(TestBigEndian)
# in tree version of runtimes, we'd be linking against the just-built
# libunwind (and the compiler implicit -lunwind wouldn't succeed as the newly
# built libunwind isn't installed yet). For those cases, it'd be good to
# link with --uwnindlib=none. Check if that option works.
# link with --unwindlib=none. Check if that option works.
llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_NONE_FLAG)
check_library_exists(c fopen "" COMPILER_RT_HAS_LIBC)

View File

@@ -12,7 +12,7 @@ include(CheckCSourceCompiles)
# LIBCXXABI_USE_LLVM_UNWINDER set, we'd be linking against the just-built
# libunwind (and the compiler implicit -lunwind wouldn't succeed as the newly
# built libunwind isn't installed yet). For those cases, it'd be good to
# link with --uwnindlib=none. Check if that option works.
# link with --unwindlib=none. Check if that option works.
llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
if (NOT LIBCXX_USE_COMPILER_RT)

View File

@@ -327,8 +327,8 @@ function(add_link_opts target_name)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS" AND LLVM_LINKER_IS_SOLARISLD)
# Support for ld -z discard-unused=sections was only added in
# Solaris 11.4. GNU ld ignores it, but warns every time.
include(LLVMCheckLinkerFlag)
llvm_check_linker_flag(CXX "-Wl,-z,discard-unused=sections" LINKER_SUPPORTS_Z_DISCARD_UNUSED)
include(CheckLinkerFlag)
check_linker_flag(CXX "-Wl,-z,discard-unused=sections" LINKER_SUPPORTS_Z_DISCARD_UNUSED)
if (LINKER_SUPPORTS_Z_DISCARD_UNUSED)
set_property(TARGET ${target_name} APPEND_STRING PROPERTY
LINK_FLAGS " -Wl,-z,discard-unused=sections")

View File

@@ -1061,8 +1061,8 @@ if (LLVM_USE_SPLIT_DWARF AND
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR
CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options($<$<COMPILE_LANGUAGE:C,CXX>:-gsplit-dwarf>)
include(LLVMCheckLinkerFlag)
llvm_check_linker_flag(CXX "-Wl,--gdb-index" LINKER_SUPPORTS_GDB_INDEX)
include(CheckLinkerFlag)
check_linker_flag(CXX "-Wl,--gdb-index" LINKER_SUPPORTS_GDB_INDEX)
append_if(LINKER_SUPPORTS_GDB_INDEX "-Wl,--gdb-index"
CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
endif()
@@ -1083,8 +1083,8 @@ endif()
# lld doesn't print colored diagnostics when invoked from Ninja
if (UNIX AND CMAKE_GENERATOR MATCHES "Ninja")
include(LLVMCheckLinkerFlag)
llvm_check_linker_flag(CXX "-Wl,--color-diagnostics" LINKER_SUPPORTS_COLOR_DIAGNOSTICS)
include(CheckLinkerFlag)
check_linker_flag(CXX "-Wl,--color-diagnostics" LINKER_SUPPORTS_COLOR_DIAGNOSTICS)
append_if(LINKER_SUPPORTS_COLOR_DIAGNOSTICS "-Wl,--color-diagnostics"
CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS)
endif()

View File

@@ -13,12 +13,12 @@ if(NOT DEFINED LLVM_STDLIB_HANDLED)
endfunction()
include(CheckCXXCompilerFlag)
include(LLVMCheckLinkerFlag)
include(CheckLinkerFlag)
set(LLVM_LIBCXX_USED 0)
if(LLVM_ENABLE_LIBCXX)
if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
check_cxx_compiler_flag("-stdlib=libc++" CXX_COMPILER_SUPPORTS_STDLIB)
llvm_check_linker_flag(CXX "-stdlib=libc++" CXX_LINKER_SUPPORTS_STDLIB)
check_linker_flag(CXX "-stdlib=libc++" CXX_LINKER_SUPPORTS_STDLIB)
if(CXX_COMPILER_SUPPORTS_STDLIB AND CXX_LINKER_SUPPORTS_STDLIB)
append("-stdlib=libc++"
CMAKE_CXX_FLAGS CMAKE_EXE_LINKER_FLAGS CMAKE_SHARED_LINKER_FLAGS
@@ -36,7 +36,7 @@ if(NOT DEFINED LLVM_STDLIB_HANDLED)
if(LLVM_COMPILER_IS_GCC_COMPATIBLE)
check_cxx_compiler_flag("-static-libstdc++"
CXX_COMPILER_SUPPORTS_STATIC_STDLIB)
llvm_check_linker_flag(CXX "-static-libstdc++" CXX_LINKER_SUPPORTS_STATIC_STDLIB)
check_linker_flag(CXX "-static-libstdc++" CXX_LINKER_SUPPORTS_STATIC_STDLIB)
if(CXX_COMPILER_SUPPORTS_STATIC_STDLIB AND
CXX_LINKER_SUPPORTS_STATIC_STDLIB)
append("-static-libstdc++"

View File

@@ -1,28 +0,0 @@
include(CheckLinkerFlag OPTIONAL)
if (COMMAND check_linker_flag)
macro(llvm_check_linker_flag)
check_linker_flag(${ARGN})
endmacro()
else()
# Until the minimum CMAKE version is 3.18
include(CheckCXXCompilerFlag)
# cmake builtin compatible, except we assume lang is C or CXX
function(llvm_check_linker_flag lang flag out_var)
cmake_policy(PUSH)
cmake_policy(SET CMP0056 NEW)
set(_CMAKE_EXE_LINKER_FLAGS_SAVE ${CMAKE_EXE_LINKER_FLAGS})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${flag}")
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()
set(CMAKE_EXE_LINKER_FLAGS ${_CMAKE_EXE_LINKER_FLAGS_SAVE})
cmake_policy(POP)
endfunction()
endif()