[llvm] [clang][CMake] Fix ODR violation with LLVM_LINK_LLVM_DYLIB (PR #186689)

via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 15 11:10:11 PDT 2026


https://github.com/nextsilicon-itay-bookstein created https://github.com/llvm/llvm-project/pull/186689

After 42b638c6b40d ("Propagate dependencies to OBJECT libraries in add_llvm_library"), obj.clangSupport now inherits clangSupport's LINK_LIBRARIES via target_link_libraries, which includes libLLVM.so when LLVM_LINK_LLVM_DYLIB is enabled.

Previously the obj.clangSupport alias path was harmless because the OBJECT library carried no link dependencies. Now, aliasing clangSupport_tablegen to obj.clangSupport in DYLIB mode causes clang-tblgen to transitively link libLLVM.so, while also having LLVM symbols compiled in statically — triggering an ASan ODR violation on globals like llvm::vfs::FileSystem::ID.

Fix by only propagating parts of the compile interface instead of the full link interface - INTERFACE_INCLUDE_DIRECTORIES and INTERFACE_SYSTEM_INCLUDE_DIRECTORIES. Also add a TODO to consider replacing with target_link_libraries($<COMPILE_ONLY:tgt>) once minimum CMake version is 3.27 or higher.

>From 5f977cda696d2cf2a8c15f28cd3db3e01bf6575e Mon Sep 17 00:00:00 2001
From: Itay Bookstein <itay.bookstein at nextsilicon.com>
Date: Sun, 15 Mar 2026 20:05:06 +0200
Subject: [PATCH] [clang][CMake] Fix ODR violation with LLVM_LINK_LLVM_DYLIB
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

After 42b638c6b40d ("Propagate dependencies to OBJECT libraries in
add_llvm_library"), obj.clangSupport now inherits clangSupport's
LINK_LIBRARIES via target_link_libraries, which includes libLLVM.so
when LLVM_LINK_LLVM_DYLIB is enabled.

Previously the obj.clangSupport alias path was harmless because the
OBJECT library carried no link dependencies. Now, aliasing
clangSupport_tablegen to obj.clangSupport in DYLIB mode causes
clang-tblgen to transitively link libLLVM.so, while also having LLVM
symbols compiled in statically — triggering an ASan ODR violation on
globals like llvm::vfs::FileSystem::ID.

Fix by only propagating parts of the compile interface instead of the
full link interface - INTERFACE_INCLUDE_DIRECTORIES and
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES. Also add a TODO to consider
replacing with target_link_libraries($<COMPILE_ONLY:tgt>) once minimum
CMake version is 3.27 or higher.
---
 llvm/cmake/modules/AddLLVM.cmake | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)

diff --git a/llvm/cmake/modules/AddLLVM.cmake b/llvm/cmake/modules/AddLLVM.cmake
index 4538d80179764..e2d0f74f26058 100644
--- a/llvm/cmake/modules/AddLLVM.cmake
+++ b/llvm/cmake/modules/AddLLVM.cmake
@@ -641,13 +641,16 @@ function(llvm_add_library name)
     # Do add_dependencies(obj) later due to CMake issue 14747.
     list(APPEND objlibs ${obj_name})
 
-    # Bring in the target include directories and link info from our original
-    # target. target_link_libraries propagates transitive dependencies with
-    # proper SYSTEM include handling from IMPORTED targets.
-    # target_include_directories propagates include directories set directly on
-    # the target.
-    target_link_libraries(${obj_name} PRIVATE $<TARGET_PROPERTY:${name},LINK_LIBRARIES>)
-    target_include_directories(${obj_name} PRIVATE $<TARGET_PROPERTY:${name},INCLUDE_DIRECTORIES>)
+    # Propagate include directories from our original target.
+    # TODO: Use $<COMPILE_ONLY:${name}> instead of this manual propagation
+    # when minimum required CMake version is 3.27 or higher.
+    target_include_directories(${obj_name} SYSTEM
+      INTERFACE $<TARGET_PROPERTY:${name},INTERFACE_SYSTEM_INCLUDE_DIRECTORIES>
+      )
+    target_include_directories(${obj_name}
+      INTERFACE $<TARGET_PROPERTY:${name},INTERFACE_INCLUDE_DIRECTORIES>
+      PRIVATE $<TARGET_PROPERTY:${name},INCLUDE_DIRECTORIES>
+      )
 
     set_target_properties(${obj_name} PROPERTIES FOLDER "${subproject_title}/Object Libraries")
     if(ARG_DEPENDS)



More information about the llvm-commits mailing list