diff --git a/cmake/Modules/LLVMCheckCompilerLinkerFlag.cmake b/cmake/Modules/LLVMCheckCompilerLinkerFlag.cmake --- a/cmake/Modules/LLVMCheckCompilerLinkerFlag.cmake +++ b/cmake/Modules/LLVMCheckCompilerLinkerFlag.cmake @@ -1,27 +1,21 @@ -include(CMakePushCheckState) +include(CheckLinkerFlag OPTIONAL) -include(CheckCompilerFlag OPTIONAL) +if (COMMAND check_linker_flag) + macro(llvm_check_compiler_linker_flag) + check_linker_flag(${ARGN}) + endmacro() +else() + # Until the minimum CMAKE version is 3.19 -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. + include(CMakePushCheckState) - 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 + function(llvm_check_compiler_linker_flag lang flag out_var) + cmake_policy(PUSH) + cmake_policy(SET CMP0056 NEW) + cmake_push_check_state() + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${flag}") # cmake builtin compatible, except we assume lang is C or CXX if("${lang}" STREQUAL "C") check_c_compiler_flag("" ${out_var}) @@ -30,6 +24,6 @@ else() message(FATAL_ERROR "\"${lang}\" is not C or CXX") endif() - endif() - cmake_pop_check_state() -endfunction() + cmake_pop_check_state() + endfunction() +endif() diff --git a/runtimes/CMakeLists.txt b/runtimes/CMakeLists.txt --- a/runtimes/CMakeLists.txt +++ b/runtimes/CMakeLists.txt @@ -81,6 +81,7 @@ include(LLVMCheckCompilerLinkerFlag) include(CheckCCompilerFlag) include(CheckCXXCompilerFlag) +include(CMakePushCheckState) # CMake omits default compiler include paths, but in runtimes build, we use # -nostdinc and -nostdinc++ and control include paths manually so this behavior @@ -103,8 +104,8 @@ filter_prefixed("${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES}" ${LLVM_BINARY_DIR} CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES) filter_prefixed("${CMAKE_ASM_IMPLICIT_INCLUDE_DIRECTORIES}" ${LLVM_BINARY_DIR} CMAKE_ASM_IMPLICIT_INCLUDE_DIRECTORIES) -check_c_compiler_flag("" LLVM_RUNTIMES_LINKING_WORKS) -if (NOT LLVM_RUNTIMES_LINKING_WORKS) +check_c_compiler_flag("" C_LINKING_WORKS) +if (NOT C_LINKING_WORKS) # 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. @@ -112,8 +113,9 @@ # e.g. ASAN/TSAN. 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") + cmake_push_check_state() + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --start-no-unused-arguments --unwindlib=none --end-no-unused-arguments") + # TODO: When we can require CMake 3.14, we should use # CMAKE_REQUIRED_LINK_OPTIONS here. Until then, we need a workaround: # When using CMAKE_REQUIRED_FLAGS, this option gets added both to @@ -129,24 +131,30 @@ # We must first add --unwindlib=none to CMAKE_REQUIRED_FLAGS above, to # allow this subsequent test to succeed, then rewrite CMAKE_REQUIRED_FLAGS # below. - check_c_compiler_flag("--start-no-unused-arguments" C_SUPPORTS_START_NO_UNUSED_ARGUMENTS) + check_c_compiler_flag("" C_SUPPORTS_START_NO_UNUSED_ARGUMENTS) + cmake_pop_check_state() if (C_SUPPORTS_START_NO_UNUSED_ARGUMENTS) - set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS} --start-no-unused-arguments --unwindlib=none --end-no-unused-arguments") + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --start-no-unused-arguments --unwindlib=none --end-no-unused-arguments") endif() endif() endif() -# Disable use of the installed C++ standard library when building runtimes. -# 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). -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() -check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG) -if (CXX_SUPPORTS_NOSTDINCXX_FLAG) - set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdinc++") +check_cxx_compiler_flag("" CXX_LINKING_WORKS) +if (NOT CXX_LINKING_WORKS) + # Disable use of the installed C++ standard library when building runtimes. + # 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). + llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG) + if (CXX_SUPPORTS_NOSTDLIBXX_FLAG) + cmake_push_check_state() + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --start-no-unused-arguments -nostdlib++ --end-no-unused-arguments") + check_cxx_compiler_flag("" CXX_SUPPORTS_START_NO_UNUSED_ARGUMENTS) + cmake_pop_check_state() + if (CXX_SUPPORTS_START_NO_UNUSED_ARGUMENTS) + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --start-no-unused-arguments -nostdlib++ --end-no-unused-arguments") + endif() + endif() endif() # Avoid checking whether the compiler is working.