From 2f3fd551db7fa21fc84eac465f42a94a1562c851 Mon Sep 17 00:00:00 2001 From: "H. Vetinari" Date: Thu, 25 Jul 2024 18:09:01 +1100 Subject: [PATCH] Reinstate LLVMCheckCompilerLinkerFlag, to correctly encapsulate link flag check This partially reverts commit 1515c2cd16911e1297c57fb69d4bb4921a40b8ab. Co-Authored-By: Ryan Prichard --- .../Modules/LLVMCheckCompilerLinkerFlag.cmake | 23 +++++++++++++++++++ compiler-rt/cmake/config-ix.cmake | 16 ++++++------- libcxx/cmake/config-ix.cmake | 4 ++-- libunwind/cmake/config-ix.cmake | 8 +++---- offload/CMakeLists.txt | 4 ++-- openmp/runtime/cmake/config-ix.cmake | 18 +++++++-------- runtimes/CMakeLists.txt | 6 ++--- 7 files changed, 51 insertions(+), 28 deletions(-) create mode 100644 cmake/Modules/LLVMCheckCompilerLinkerFlag.cmake diff --git a/cmake/Modules/LLVMCheckCompilerLinkerFlag.cmake b/cmake/Modules/LLVMCheckCompilerLinkerFlag.cmake new file mode 100644 index 00000000000000..2f1f86835b396f --- /dev/null +++ b/cmake/Modules/LLVMCheckCompilerLinkerFlag.cmake @@ -0,0 +1,23 @@ +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}") + check_compiler_flag("${lang}" "" ${out_var}) + cmake_pop_check_state() +endfunction() diff --git a/compiler-rt/cmake/config-ix.cmake b/compiler-rt/cmake/config-ix.cmake index e86d9730c1d3d4..97204177cde9a4 100644 --- a/compiler-rt/cmake/config-ix.cmake +++ b/compiler-rt/cmake/config-ix.cmake @@ -1,10 +1,10 @@ include(CMakePushCheckState) include(AddLLVM) -include(CheckCompilerFlag) include(CheckCCompilerFlag) include(CheckCXXCompilerFlag) include(CheckIncludeFiles) include(CheckLibraryExists) +include(LLVMCheckCompilerLinkerFlag) include(CheckSymbolExists) include(TestBigEndian) @@ -15,7 +15,7 @@ include(TestBigEndian) # 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 --unwindlib=none. Check if that option works. -check_compiler_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_NONE_FLAG) +llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_NONE_FLAG) check_library_exists(c fopen "" COMPILER_RT_HAS_LIBC) if (COMPILER_RT_USE_BUILTINS_LIBRARY) @@ -190,12 +190,12 @@ check_library_exists(c++ __cxa_throw "" COMPILER_RT_HAS_LIBCXX) check_library_exists(stdc++ __cxa_throw "" COMPILER_RT_HAS_LIBSTDCXX) # Linker flags. -check_compiler_flag(C "-Wl,-z,text" COMPILER_RT_HAS_Z_TEXT) -check_compiler_flag(C "-fuse-ld=lld" COMPILER_RT_HAS_FUSE_LD_LLD_FLAG) +llvm_check_compiler_linker_flag(C "-Wl,-z,text" COMPILER_RT_HAS_Z_TEXT) +llvm_check_compiler_linker_flag(C "-fuse-ld=lld" COMPILER_RT_HAS_FUSE_LD_LLD_FLAG) if(${CMAKE_SYSTEM_NAME} MATCHES "SunOS" AND LLVM_LINKER_IS_SOLARISLD) set(VERS_COMPAT_OPTION "-Wl,-z,gnu-version-script-compat") - check_compiler_flag(C "${VERS_COMPAT_OPTION}" COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT) + llvm_check_compiler_linker_flag(C "${VERS_COMPAT_OPTION}" COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT) endif() set(DUMMY_VERS ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/dummy.vers) @@ -206,10 +206,10 @@ if(COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT) # -z gnu-version-script-compat. string(APPEND VERS_OPTION " ${VERS_COMPAT_OPTION}") endif() -check_compiler_flag(C "${VERS_OPTION}" COMPILER_RT_HAS_VERSION_SCRIPT) +llvm_check_compiler_linker_flag(C "${VERS_OPTION}" COMPILER_RT_HAS_VERSION_SCRIPT) if(ANDROID) - check_compiler_flag(C "-Wl,-z,global" COMPILER_RT_HAS_Z_GLOBAL) + llvm_check_compiler_linker_flag(C "-Wl,-z,global" COMPILER_RT_HAS_Z_GLOBAL) check_library_exists(log __android_log_write "" COMPILER_RT_HAS_LIBLOG) endif() @@ -494,7 +494,7 @@ if(APPLE) -lc++ -lc++abi) - check_compiler_flag(C "-fapplication-extension" COMPILER_RT_HAS_APP_EXTENSION) + llvm_check_compiler_linker_flag(C "-fapplication-extension" COMPILER_RT_HAS_APP_EXTENSION) if(COMPILER_RT_HAS_APP_EXTENSION) list(APPEND DARWIN_COMMON_LINK_FLAGS "-fapplication-extension") endif() diff --git a/libcxx/cmake/config-ix.cmake b/libcxx/cmake/config-ix.cmake index 1012a07ddf4cb7..270d80575adcfd 100644 --- a/libcxx/cmake/config-ix.cmake +++ b/libcxx/cmake/config-ix.cmake @@ -1,7 +1,7 @@ include(CMakePushCheckState) include(CheckLibraryExists) include(CheckSymbolExists) -include(CheckCompilerFlag) +include(LLVMCheckCompilerLinkerFlag) include(CheckCCompilerFlag) include(CheckCXXCompilerFlag) include(CheckCSourceCompiles) @@ -13,7 +13,7 @@ include(CheckCSourceCompiles) # 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 --unwindlib=none. Check if that option works. -check_compiler_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG) +llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG) if (NOT LIBCXX_USE_COMPILER_RT) if(WIN32 AND NOT MINGW) diff --git a/libunwind/cmake/config-ix.cmake b/libunwind/cmake/config-ix.cmake index 77bdd5eb5aaf88..126c872f0d4895 100644 --- a/libunwind/cmake/config-ix.cmake +++ b/libunwind/cmake/config-ix.cmake @@ -2,14 +2,14 @@ include(CMakePushCheckState) include(CheckCCompilerFlag) include(CheckCXXCompilerFlag) include(CheckLibraryExists) -include(CheckCompilerFlag) +include(LLVMCheckCompilerLinkerFlag) include(CheckSymbolExists) include(CheckCSourceCompiles) # The compiler driver may be implicitly trying to link against libunwind, which # might not work if libunwind doesn't exist yet. Try to check if # --unwindlib=none is supported, and use that if possible. -check_compiler_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG) +llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG) if (HAIKU) check_library_exists(root fopen "" LIBUNWIND_HAS_ROOT_LIB) @@ -35,11 +35,11 @@ endif() # required for the link to go through. We remove sanitizers from the # configuration checks to avoid spurious link errors. -check_compiler_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG) +llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG) if (CXX_SUPPORTS_NOSTDLIBXX_FLAG) set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++") else() - check_compiler_flag(C "-nodefaultlibs" C_SUPPORTS_NODEFAULTLIBS_FLAG) + llvm_check_compiler_linker_flag(C "-nodefaultlibs" C_SUPPORTS_NODEFAULTLIBS_FLAG) if (C_SUPPORTS_NODEFAULTLIBS_FLAG) set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs") endif() diff --git a/offload/CMakeLists.txt b/offload/CMakeLists.txt index e285a2c2300d03..4cd97a6a5ff63d 100644 --- a/offload/CMakeLists.txt +++ b/offload/CMakeLists.txt @@ -294,9 +294,9 @@ endmacro() if(OPENMP_STANDALONE_BUILD OR TARGET omp) # Check LIBOMP_HAVE_VERSION_SCRIPT_FLAG - include(CheckCompilerFlag) + include(LLVMCheckCompilerLinkerFlag) if(NOT APPLE) - check_compiler_flag(C "-Wl,--version-script=${CMAKE_CURRENT_LIST_DIR}/../openmp/runtime/src/exports_test_so.txt" LIBOMP_HAVE_VERSION_SCRIPT_FLAG) + llvm_check_compiler_linker_flag(C "-Wl,--version-script=${CMAKE_CURRENT_LIST_DIR}/../openmp/runtime/src/exports_test_so.txt" LIBOMP_HAVE_VERSION_SCRIPT_FLAG) endif() endif() diff --git a/openmp/runtime/cmake/config-ix.cmake b/openmp/runtime/cmake/config-ix.cmake index a00707d9e921fa..0568474f1d74ed 100644 --- a/openmp/runtime/cmake/config-ix.cmake +++ b/openmp/runtime/cmake/config-ix.cmake @@ -8,7 +8,6 @@ #//===----------------------------------------------------------------------===// # -include(CheckCompilerFlag) include(CheckCCompilerFlag) include(CheckCSourceCompiles) include(CheckCXXSourceCompiles) @@ -18,6 +17,7 @@ include(CheckLibraryExists) include(CheckIncludeFiles) include(CheckSymbolExists) include(LibompCheckFortranFlag) +include(LLVMCheckCompilerLinkerFlag) # Check for versioned symbols function(libomp_check_version_symbols retval) @@ -128,13 +128,13 @@ check_symbol_exists(_aligned_malloc "malloc.h" LIBOMP_HAVE__ALIGNED_MALLOC) # Check linker flags if(WIN32) - check_compiler_flag(C /SAFESEH LIBOMP_HAVE_SAFESEH_FLAG) + llvm_check_compiler_linker_flag(C /SAFESEH LIBOMP_HAVE_SAFESEH_FLAG) elseif(NOT APPLE) - check_compiler_flag(C -Wl,-x LIBOMP_HAVE_X_FLAG) - check_compiler_flag(C -Wl,--as-needed LIBOMP_HAVE_AS_NEEDED_FLAG) - check_compiler_flag(C "-Wl,--version-script=${LIBOMP_SRC_DIR}/exports_test_so.txt" LIBOMP_HAVE_VERSION_SCRIPT_FLAG) - check_compiler_flag(C -static-libgcc LIBOMP_HAVE_STATIC_LIBGCC_FLAG) - check_compiler_flag(C -Wl,-z,noexecstack LIBOMP_HAVE_Z_NOEXECSTACK_FLAG) + llvm_check_compiler_linker_flag(C -Wl,-x LIBOMP_HAVE_X_FLAG) + llvm_check_compiler_linker_flag(C -Wl,--as-needed LIBOMP_HAVE_AS_NEEDED_FLAG) + llvm_check_compiler_linker_flag(C "-Wl,--version-script=${LIBOMP_SRC_DIR}/exports_test_so.txt" LIBOMP_HAVE_VERSION_SCRIPT_FLAG) + llvm_check_compiler_linker_flag(C -static-libgcc LIBOMP_HAVE_STATIC_LIBGCC_FLAG) + llvm_check_compiler_linker_flag(C -Wl,-z,noexecstack LIBOMP_HAVE_Z_NOEXECSTACK_FLAG) endif() # Check Intel(R) C Compiler specific flags @@ -145,8 +145,8 @@ if(CMAKE_C_COMPILER_ID STREQUAL "Intel" OR CMAKE_C_COMPILER_ID STREQUAL "IntelLL check_cxx_compiler_flag(-Qoption,cpp,--extended_float_types LIBOMP_HAVE_EXTENDED_FLOAT_TYPES_FLAG) check_cxx_compiler_flag(-falign-stack=maintain-16-byte LIBOMP_HAVE_FALIGN_STACK_FLAG) check_cxx_compiler_flag("-opt-streaming-stores never" LIBOMP_HAVE_OPT_STREAMING_STORES_FLAG) - check_compiler_flag(C -static-intel LIBOMP_HAVE_STATIC_INTEL_FLAG) - check_compiler_flag(C -no-intel-extensions LIBOMP_HAVE_NO_INTEL_EXTENSIONS_FLAG) + llvm_check_compiler_linker_flag(C -static-intel LIBOMP_HAVE_STATIC_INTEL_FLAG) + llvm_check_compiler_linker_flag(C -no-intel-extensions LIBOMP_HAVE_NO_INTEL_EXTENSIONS_FLAG) check_library_exists(irc_pic _intel_fast_memcpy "" LIBOMP_HAVE_IRC_PIC_LIBRARY) endif() diff --git a/runtimes/CMakeLists.txt b/runtimes/CMakeLists.txt index 39a4c5bc383c35..830165c799c2ab 100644 --- a/runtimes/CMakeLists.txt +++ b/runtimes/CMakeLists.txt @@ -79,7 +79,7 @@ set(LLVM_CMAKE_DIR ${LLVM_MAIN_SRC_DIR}/cmake/modules) set(LLVM_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../llvm) include(CheckLibraryExists) -include(CheckCompilerFlag) +include(LLVMCheckCompilerLinkerFlag) include(CheckCCompilerFlag) include(CheckCXXCompilerFlag) @@ -114,7 +114,7 @@ filter_prefixed("${CMAKE_ASM_IMPLICIT_INCLUDE_DIRECTORIES}" ${LLVM_BINARY_DIR} C # Currently, we counteract this issue by adding -fno-sanitize=all flag in # the project specific code within */cmake/config-ix.cmake files but that's # brittle. We should ideally move this to runtimes/CMakeLists.txt. -check_compiler_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG) +llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG) if (CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG) set(ORIG_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}") set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --unwindlib=none") @@ -143,7 +143,7 @@ endif() # Check for -nostdlib++ first; if there's no C++ standard library yet, # all check_cxx_compiler_flag commands will fail until we add -nostdlib++ # (or -nodefaultlibs). -check_compiler_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG) +llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG) if (CXX_SUPPORTS_NOSTDLIBXX_FLAG) set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++") endif()