[llvm] [cmake] Silence a duplicate libraries warning from Apple's linker (PR #85012)

Jon Roelofs via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 12 19:02:52 PDT 2024


https://github.com/jroelofs created https://github.com/llvm/llvm-project/pull/85012

ld: warning: ignoring duplicate libraries:

This triggers quite frequently in llvm's build because CMake's library depends mechanism doesn't de-duplicate libraries on the link line.  Duplication is necessary for ELF platforms, but means something subtly different on Darwin platforms, hence the warning.  Since we don't have much control over that from CMake, just disable the warning wholesale whenever the linker is detected to support it.

>From d1160a56f9430fdbcf19f8b5fa9efa6bebf86e20 Mon Sep 17 00:00:00 2001
From: Jon Roelofs <jonathan_roelofs at apple.com>
Date: Tue, 12 Mar 2024 18:58:27 -0700
Subject: [PATCH] [cmake] Silence a duplicate libraries warning from Apple's
 linker

ld: warning: ignoring duplicate libraries:

This triggers quite frequently in llvm's build because CMake's library depends
mechanism doesn't de-duplicate libraries on the link line.  Duplication is
necessary for ELF platforms, but means something subtly different on Darwin
platforms, hence the warning.  Since we don't have much control over that from
CMake, just disable the warning wholesale whenever the linker is detected to
support it.
---
 llvm/cmake/modules/AddLLVM.cmake | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake
index 828de4bd9940d6..6a651e3d9b7a97 100644
--- a/llvm/cmake/modules/AddLLVM.cmake
+++ b/llvm/cmake/modules/AddLLVM.cmake
@@ -257,6 +257,16 @@ if (NOT DEFINED LLVM_LINKER_DETECTED AND NOT WIN32)
       message(STATUS "Linker detection: unknown")
     endif()
   endif()
+
+  # Apple's linker complains about duplicate libraries, which CMake likes to do
+  # to support ELF platforms. To silence that warning, we can use
+  # -no_warn_duplicate_libraries, but only in versions of the linker that
+  # support that flag.
+  if(NOT LLVM_USE_LINKER AND ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
+    check_linker_flag(C "-Wl,-no_warn_duplicate_libraries" LLVM_LINKER_SUPPORTS_NO_WARN_DUPLICATE_LIBRARIES)
+  else()
+    set(LLVM_LINKER_SUPPORTS_NO_WARN_DUPLICATE_LIBRARIES OFF CACHE INTERNAL "")
+  endif()
 endif()
 
 function(add_link_opts target_name)
@@ -310,6 +320,11 @@ function(add_link_opts target_name)
     endif()
   endif()
 
+  if(LLVM_LINKER_SUPPORTS_NO_WARN_DUPLICATE_LIBRARIES)
+    set_property(TARGET ${target_name} APPEND_STRING PROPERTY
+                 LINK_FLAGS " -Wl,-no_warn_duplicate_libraries")
+  endif()
+
   if(ARG_SUPPORT_PLUGINS AND ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
     set_property(TARGET ${target_name} APPEND_STRING PROPERTY
                  LINK_FLAGS " -Wl,-brtl")



More information about the llvm-commits mailing list