[llvm] [llvm-libgcc] Fix symlink path for libcc when LLVM_ENABLE_PER_TARGET_… (PR #165487)

Khem Raj via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 28 19:05:07 PDT 2025


https://github.com/kraj updated https://github.com/llvm/llvm-project/pull/165487

>From d21ab66fd88377ee4a9247e67b28cadfeed4bd3b Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem at gmail.com>
Date: Tue, 28 Oct 2025 15:44:49 -0700
Subject: [PATCH 1/2] Fix symlink path for libgcc

The symlink for libgcc_so.1.0 is made to point to libunwind.so
which is functionally correct but it fails some linux distro packaging
complain because libunwind.so is made part of -dev package but
libgcc_so.1.0 ends up in the real package, and creates an unneeded
package -> dev dependency

create the symlink to point to libunwind.so.1 instead then the boundaries
of packaging are not crossed and all is well.
---
 llvm-libgcc/CMakeLists.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm-libgcc/CMakeLists.txt b/llvm-libgcc/CMakeLists.txt
index 47208fc198692..cd9c5011d4105 100644
--- a/llvm-libgcc/CMakeLists.txt
+++ b/llvm-libgcc/CMakeLists.txt
@@ -137,7 +137,7 @@ add_custom_target(llvm-libgcc ALL
   DEPENDS unwind_shared unwind_static clang_rt.builtins-${COMPILER_RT_DEFAULT_TARGET_ARCH}
   COMMAND ${CMAKE_COMMAND} -E create_symlink ${LLVM_LIBGCC_COMPILER_RT} libgcc.a
   COMMAND ${CMAKE_COMMAND} -E create_symlink libunwind.a libgcc_eh.a
-  COMMAND ${CMAKE_COMMAND} -E create_symlink libunwind.so libgcc_s.so.1.0
+  COMMAND ${CMAKE_COMMAND} -E create_symlink libunwind.so.1 libgcc_s.so.1.0
   COMMAND ${CMAKE_COMMAND} -E create_symlink libgcc_s.so.1.0 libgcc_s.so.1
   COMMAND ${CMAKE_COMMAND} -E create_symlink libgcc_s.so.1 libgcc_s.so
 )

>From 919fcd11ad53bdfab9a14d5df6de0895bf24e456 Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem at gmail.com>
Date: Tue, 28 Oct 2025 19:02:44 -0700
Subject: [PATCH 2/2] [llvm-libgcc] Fix libgcc.a symlink path when
 LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF

The llvm-libgcc installation was creating incorrect symlinks for libgcc.a
when built with LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF, particularly seen with
Yocto cross-compilation environments.

Issues seen:

-  Absolute path in symlink: The previous code didn't handle absolute paths
   returned by get_compiler_rt_install_dir() in staging/sysroot environments,
   resulting in symlinks like:
   libgcc.a -> /absolute/path/to/sysroot/usr/lib/clang/.../libclang_rt.builtins.a

-  Missing architecture suffix: When the install path contained "clang", the
   code would skip setting builtins_suffix, creating:
   libgcc.a -> clang/21.1.4/lib/linux/libclang_rt.builtins.a
   instead of:
   libgcc.a -> clang/21.1.4/lib/linux/libclang_rt.builtins-aarch64.a

-  Incorrect relative path calculation: The original regex stripped too much
   or too little of the path, either creating symlinks pointing to non-existent
   locations or stripping the clang/ directory prefix entirely.

Solution:

- Extract the relative path starting from 'clang/' when present in absolute
  paths, ensuring the symlink points to the correct location within the
  installation hierarchy
- Always append the architecture suffix to the builtins library name
- Only prepend '../' to the path when LLVM_ENABLE_PER_TARGET_RUNTIME_DIR is
  enabled, as this is when the builtins are in a different directory level

The symlink now correctly points to the existing compiler-rt builtins library
without creating duplicate copies.

Tested with LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=OFF in cross-compilation
environments (aarch64 target).
---
 llvm-libgcc/CMakeLists.txt | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/llvm-libgcc/CMakeLists.txt b/llvm-libgcc/CMakeLists.txt
index cd9c5011d4105..ee7fe768bda04 100644
--- a/llvm-libgcc/CMakeLists.txt
+++ b/llvm-libgcc/CMakeLists.txt
@@ -124,11 +124,27 @@ target_link_libraries(unwind_shared PUBLIC
 #===============================================================================
 
 get_compiler_rt_install_dir(${COMPILER_RT_DEFAULT_TARGET_ARCH} install_dir_builtins)
-string(REGEX REPLACE "^lib/" "" install_dir_builtins "${install_dir_builtins}")
-string(FIND "${install_dir_builtins}" "clang" install_path_contains_triple)
-if(install_path_contains_triple EQUAL -1)
-  set(builtins_suffix "-${COMPILER_RT_DEFAULT_TARGET_ARCH}")
+
+# Extract the relative path starting from 'clang' or after 'lib/'
+if(IS_ABSOLUTE "${install_dir_builtins}")
+  # For absolute paths, extract starting from 'clang/' if it exists
+  string(REGEX MATCH "clang/.*$" install_dir_builtins_temp "${install_dir_builtins}")
+  if(install_dir_builtins_temp)
+    set(install_dir_builtins "${install_dir_builtins_temp}")
+  else()
+    # Fallback: strip up to first lib/
+    string(REGEX REPLACE "^.*/lib/" "" install_dir_builtins "${install_dir_builtins}")
+  endif()
 else()
+  string(REGEX REPLACE "^lib/" "" install_dir_builtins "${install_dir_builtins}")
+endif()
+
+# Always add the architecture suffix
+set(builtins_suffix "-${COMPILER_RT_DEFAULT_TARGET_ARCH}")
+
+# Only prepend ../ when using per-target runtime directories
+string(FIND "${install_dir_builtins}" "clang" install_path_contains_triple)
+if(install_path_contains_triple GREATER -1 AND LLVM_ENABLE_PER_TARGET_RUNTIME_DIR)
   string(PREPEND install_dir_builtins "../")
 endif()
 set(LLVM_LIBGCC_COMPILER_RT ${install_dir_builtins}/libclang_rt.builtins${builtins_suffix}.a)



More information about the llvm-commits mailing list