[llvm] [CMake] Enable CMP0179 when enabling CMP0156 to avoid LLD static library link order bug (PR #116497)

Raul Tambre via llvm-commits llvm-commits at lists.llvm.org
Sat Nov 16 09:29:39 PST 2024


https://github.com/tambry created https://github.com/llvm/llvm-project/pull/116497

This fixes the following linking failure for libclang-cpp.so with -DLLVM_LINK_LLVM_DYLIB=ON:
```
ld.lld: error: undefined symbol: llvm::omp::getOpenMPClauseName(llvm::omp::Clause)
>>> referenced by OpenMPKinds.cpp
>>>               tools/clang/lib/Basic/CMakeFiles/obj.clangBasic.dir/OpenMPKinds.cpp.o:(clang::getOpenMPSimpleClauseTypeName(llvm::omp::Clause, unsigned int))
>>> referenced by SemaOpenMP.cpp
>>>               tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaOpenMP.cpp.o:(clang::SemaOpenMP::CheckOMPRequiresDecl(clang::SourceLocation, llvm::ArrayRef<clang::OMPClause*>))
>>> referenced by SemaOpenMP.cpp
>>>               tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaOpenMP.cpp.o:(clang::SemaOpenMP::CheckOMPRequiresDecl(clang::SourceLocation, llvm::ArrayRef<clang::OMPClause*>))
>>> referenced 166 more times

[tons more]
```

This is exhibited under the following conditions:
* LLVM LLD
* CMake 3.31.0
* CMP0156=NEW
* CMP0179=OLD

CMake 3.31's new linker architecture changes the library link order. Per LLD's design documents it should be immune to such reodering like other popular linkers (gold, Apple's), but isn't.

A workaround for LLD is expected in CMake 3.31.1 but CMP0179=NEW changes the linking order such that the bug is exhibited so use it as a workaround. Since deduplication is better in general enable it unconditionally when enabling CMP0156.

See: https://gitlab.kitware.com/cmake/cmake/-/issues/26447
Fixes: cb90d5b3ef463f0a471f9c6d39978c3764021dea

---

Note we're still looking into filing a bug against LLD on the CMake issue so I might amend the commit to add a reference to that to the message.  
But in the meantime I'll at least be able to get some feedback from the CI and you. :slightly_smiling_face: 

>From dc704ac6932a5fadde1921585ffb611b01b8c39b Mon Sep 17 00:00:00 2001
From: Raul Tambre <raul at tambre.ee>
Date: Sat, 16 Nov 2024 15:04:21 +0200
Subject: [PATCH] [CMake] Enable CMP0179 when enabling CMP0156 to avoid LLD
 static library link order bug

This fixes the following linking failure for libclang-cpp.so with -DLLVM_LINK_LLVM_DYLIB=ON:
```
ld.lld: error: undefined symbol: llvm::omp::getOpenMPClauseName(llvm::omp::Clause)
>>> referenced by OpenMPKinds.cpp
>>>               tools/clang/lib/Basic/CMakeFiles/obj.clangBasic.dir/OpenMPKinds.cpp.o:(clang::getOpenMPSimpleClauseTypeName(llvm::omp::Clause, unsigned int))
>>> referenced by SemaOpenMP.cpp
>>>               tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaOpenMP.cpp.o:(clang::SemaOpenMP::CheckOMPRequiresDecl(clang::SourceLocation, llvm::ArrayRef<clang::OMPClause*>))
>>> referenced by SemaOpenMP.cpp
>>>               tools/clang/lib/Sema/CMakeFiles/obj.clangSema.dir/SemaOpenMP.cpp.o:(clang::SemaOpenMP::CheckOMPRequiresDecl(clang::SourceLocation, llvm::ArrayRef<clang::OMPClause*>))
>>> referenced 166 more times

[tons more]
```

This is exhibited under the following conditions:
* LLVM LLD
* CMake 3.31.0
* CMP0156=NEW
* CMP0179=OLD

CMake 3.31's new linker architecture changes the library link order.
Per LLD's design documents it should be immune to such reodering like other popular linkers (gold, Apple's), but isn't.

A workaround for LLD is expected in CMake 3.31.1 but CMP0179=NEW changes the linking order such that the bug is avoided.
Deduplication is better in general so always enable it when possible.

See: https://gitlab.kitware.com/cmake/cmake/-/issues/26447
Fixes: cb90d5b3ef463f0a471f9c6d39978c3764021dea
---
 cmake/Modules/CMakePolicy.cmake | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/cmake/Modules/CMakePolicy.cmake b/cmake/Modules/CMakePolicy.cmake
index d561556b2830f9..956b087da0f9c3 100644
--- a/cmake/Modules/CMakePolicy.cmake
+++ b/cmake/Modules/CMakePolicy.cmake
@@ -37,4 +37,13 @@ endif()
 # building with the Apple linker.
 if(POLICY CMP0156)
   cmake_policy(SET CMP0156 NEW)
+
+  # CMP0179: De-duplication of static libraries on link lines keeps first occurrence.
+  # Dependent on CMP0156=NEW. Keeps the first occurrence of static libraries
+  # during deduplication to unify the behaviour on all platforms.
+  # Also works around LLD bug exhibited since CMake 3.31.0 with an expected
+  # workaround in 3.31.1 (https://gitlab.kitware.com/cmake/cmake/-/issues/26447).
+  if(POLICY CMP0179)
+    cmake_policy(SET CMP0179 NEW)
+  endif()
 endif()



More information about the llvm-commits mailing list