Skip to content

Commit

Permalink
[cmake] switch to CMake's native check_{compiler,linker}_flag (llvm…
Browse files Browse the repository at this point in the history
…#96171)

Broken out from llvm#93429

Somewhat closing the loop opened by 7017e6c.

Co-authored-by: Ryan Prichard <[email protected]>
  • Loading branch information
2 people authored and clementval committed Jul 31, 2024
1 parent 4fa34ed commit 7bcc567
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 64 deletions.
2 changes: 1 addition & 1 deletion clang/tools/clang-repl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
4 changes: 2 additions & 2 deletions clang/tools/driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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
Expand Down
24 changes: 3 additions & 21 deletions cmake/Modules/LLVMCheckCompilerLinkerFlag.cmake
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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()
3 changes: 1 addition & 2 deletions compiler-rt/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
include(CMakePushCheckState)
include(AddLLVM)
include(LLVMCheckCompilerLinkerFlag)
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
include(CheckIncludeFiles)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion libcxx/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions llvm/cmake/modules/AddLLVM.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 4 additions & 4 deletions llvm/cmake/modules/HandleLLVMOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions llvm/cmake/modules/HandleLLVMStdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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++"
Expand Down
28 changes: 0 additions & 28 deletions llvm/cmake/modules/LLVMCheckLinkerFlag.cmake

This file was deleted.

0 comments on commit 7bcc567

Please sign in to comment.