[CMake][llvm] avoid conflict w/ (and use when available) new builtin check_linker_flag
authorJim Radford <jradford@apple.com>
Sat, 3 Apr 2021 06:51:17 +0000 (23:51 -0700)
committerFrancis Visoiu Mistrih <francisvm@yahoo.com>
Tue, 27 Apr 2021 23:41:28 +0000 (16:41 -0700)
Match the API for the new check_linker_flag and use it directly when
available, leaving the old code as a fallback.

Differential Revision: https://reviews.llvm.org/D100901

clang/tools/driver/CMakeLists.txt
llvm/cmake/modules/AddLLVM.cmake
llvm/cmake/modules/CheckLinkerFlag.cmake [deleted file]
llvm/cmake/modules/HandleLLVMOptions.cmake
llvm/cmake/modules/HandleLLVMStdlib.cmake
llvm/cmake/modules/LLVMCheckLinkerFlag.cmake [new file with mode: 0644]

index 01efebd..7c32aad 100644 (file)
@@ -93,7 +93,7 @@ endif()
 
 if(CLANG_ORDER_FILE AND
     (LLVM_LINKER_IS_LD64 OR LLVM_LINKER_IS_GOLD OR LLVM_LINKER_IS_LLD))
-  include(CheckLinkerFlag)
+  include(LLVMCheckLinkerFlag)
 
   if (LLVM_LINKER_IS_LD64)
     set(LINKER_ORDER_FILE_OPTION "-Wl,-order_file,${CLANG_ORDER_FILE}")
@@ -104,7 +104,7 @@ if(CLANG_ORDER_FILE AND
   endif()
 
   # This is a test to ensure the actual order file works with the linker.
-  check_linker_flag(${LINKER_ORDER_FILE_OPTION} LINKER_ORDER_FILE_WORKS)
+  llvm_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
index 2f055c7..9bfef6d 100644 (file)
@@ -236,8 +236,8 @@ function(add_link_opts target_name)
       elseif(${CMAKE_SYSTEM_NAME} MATCHES "SunOS")
         # Support for ld -z discard-unused=sections was only added in
         # Solaris 11.4.
-        include(CheckLinkerFlag)
-        check_linker_flag("-Wl,-z,discard-unused=sections" LINKER_SUPPORTS_Z_DISCARD_UNUSED)
+        include(LLVMCheckLinkerFlag)
+        llvm_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")
diff --git a/llvm/cmake/modules/CheckLinkerFlag.cmake b/llvm/cmake/modules/CheckLinkerFlag.cmake
deleted file mode 100644 (file)
index fe9d01a..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-include(CheckCXXCompilerFlag)
-
-function(check_linker_flag flag out_var)
-  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${flag}")
-  check_cxx_compiler_flag("" ${out_var})
-endfunction()
index e7c30e9..ee91386 100644 (file)
@@ -888,8 +888,8 @@ endif()
 
 # lld doesn't print colored diagnostics when invoked from Ninja
 if (UNIX AND CMAKE_GENERATOR STREQUAL "Ninja")
-  include(CheckLinkerFlag)
-  check_linker_flag("-Wl,--color-diagnostics" LINKER_SUPPORTS_COLOR_DIAGNOSTICS)
+  include(LLVMCheckLinkerFlag)
+  llvm_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()
index b67d87d..7afc10c 100644 (file)
@@ -13,12 +13,12 @@ if(NOT DEFINED LLVM_STDLIB_HANDLED)
   endfunction()
 
   include(CheckCXXCompilerFlag)
-  include(CheckLinkerFlag)
+  include(LLVMCheckLinkerFlag)
   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)
-      check_linker_flag("-stdlib=libc++" CXX_LINKER_SUPPORTS_STDLIB)
+      llvm_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)
-      check_linker_flag("-static-libstdc++" CXX_LINKER_SUPPORTS_STATIC_STDLIB)
+      llvm_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++"
diff --git a/llvm/cmake/modules/LLVMCheckLinkerFlag.cmake b/llvm/cmake/modules/LLVMCheckLinkerFlag.cmake
new file mode 100644 (file)
index 0000000..90d7520
--- /dev/null
@@ -0,0 +1,17 @@
+include(CheckLinkerFlag OPTIONAL)
+
+if (COMMAND check_linker_flag)
+  macro(llvm_check_linker_flag)
+    check_linker_flag(${ARGN})
+  endmacro()
+else()
+  include(CheckCXXCompilerFlag)
+
+  # cmake builtin compatible, except we assume lang is CXX
+  function(llvm_check_linker_flag lang flag out_var)
+    cmake_push_check_state()
+    set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${flag}")
+    check_cxx_compiler_flag("" ${out_var})
+    cmake_pop_check_state()
+  endfunction()
+endif()