[libunwind] [libcxx] [libcxxabi] [runtimes] Don't link against compiler-rt explicitly when we use -nostdlib++ (PR #75089)

2023-12-14 Thread Petr Hosek via cfe-commits

petrhosek wrote:

> > I'm trying to implement support for building libunwind, libc++abi and 
> > libc++ against LLVM libc in which case we won't be able to rely on 
> > `-nostdlib++`, we'll need to use `-nostdlib` to avoid linking the C 
> > library. We can still use `-nostdlib++` when LLVM libc isn't being used 
> > used, but a lot of this logic will need to be refactored to support the new 
> > use case. With that in mind, I'm fine with change as an interim solution.
> 
> Is there a reason why `-nostdlib` also drops compiler-rt? If `-nostdlib++` 
> affects only the C++ library, it would make sense that `-nostdlib` affects 
> only the C library?

The reason is matching the behavior of GCC, but given that I don't think we can 
change the current semantics.

https://github.com/llvm/llvm-project/pull/75089
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[libunwind] [libcxx] [libcxxabi] [runtimes] Don't link against compiler-rt explicitly when we use -nostdlib++ (PR #75089)

2023-12-11 Thread via cfe-commits

llvmbot wrote:



@llvm/pr-subscribers-libcxxabi

@llvm/pr-subscribers-libcxx

Author: Louis Dionne (ldionne)


Changes

When we use the -nostdlib++ flag, we don't need to explicitly link against 
compiler-rt, since the compiler already links against it by default. This 
simplifies the flags that we need to use when building with Clang and GCC, and 
opens the door to further simplifications since most platforms won't need to 
detect whether libgcc and libgcc_s are supported anymore.

Furthermore, on platforms where -nostdlib++ is used, this patch prevents 
manually linking compiler-rt *before* other system libraries. For example, 
Apple platforms have several compiler-rt symbols defined in libSystem.dylib. If 
we manually link against compiler-rt, we end up overriding the default link 
order preferred by the compiler and potentially using the symbols from the 
clang-provided libclang_rt.a library instead of the system provided one.

Note that we don't touch how libunwind links against compiler-rt when it builds 
the .so/.a because libunwind currently doesn't use -nodefaultlibs and we want 
to avoid rocking the boat too much.

rdar://119506163

---
Full diff: https://github.com/llvm/llvm-project/pull/75089.diff


5 Files Affected:

- (modified) libcxx/CMakeLists.txt (+10-8) 
- (modified) libcxx/cmake/config-ix.cmake (+6-1) 
- (modified) libcxxabi/cmake/config-ix.cmake (+6-1) 
- (modified) libcxxabi/src/CMakeLists.txt (+4-1) 
- (modified) libunwind/cmake/config-ix.cmake (+6-1) 


``diff
diff --git a/libcxx/CMakeLists.txt b/libcxx/CMakeLists.txt
index 31fc9cec1c57f..2a1458a098d2e 100644
--- a/libcxx/CMakeLists.txt
+++ b/libcxx/CMakeLists.txt
@@ -655,15 +655,17 @@ function(cxx_link_system_libraries target)
 target_add_link_flags_if_supported(${target} PRIVATE "--unwindlib=none")
   endif()
 
-  if (LIBCXX_USE_COMPILER_RT)
-find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY)
-if (LIBCXX_BUILTINS_LIBRARY)
-  target_link_libraries(${target} PRIVATE "${LIBCXX_BUILTINS_LIBRARY}")
+  if (MSVC)
+if (LIBCXX_USE_COMPILER_RT)
+  find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY)
+  if (LIBCXX_BUILTINS_LIBRARY)
+target_link_libraries(${target} PRIVATE "${LIBCXX_BUILTINS_LIBRARY}")
+  endif()
+elseif (LIBCXX_HAS_GCC_LIB)
+  target_link_libraries(${target} PRIVATE gcc)
+elseif (LIBCXX_HAS_GCC_S_LIB)
+  target_link_libraries(${target} PRIVATE gcc_s)
 endif()
-  elseif (LIBCXX_HAS_GCC_LIB)
-target_link_libraries(${target} PRIVATE gcc)
-  elseif (LIBCXX_HAS_GCC_S_LIB)
-target_link_libraries(${target} PRIVATE gcc_s)
   endif()
 
   if (LIBCXX_HAS_ATOMIC_LIB)
diff --git a/libcxx/cmake/config-ix.cmake b/libcxx/cmake/config-ix.cmake
index a365517936e75..1e8c2f5ce4632 100644
--- a/libcxx/cmake/config-ix.cmake
+++ b/libcxx/cmake/config-ix.cmake
@@ -45,7 +45,9 @@ else()
   endif()
 endif()
 
-if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
+# Only link against compiler-rt manually if we use -nodefaultlibs, since
+# otherwise the compiler will do the right thing on its own.
+if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
   if (LIBCXX_USE_COMPILER_RT)
 include(HandleCompilerRT)
 find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY
@@ -73,6 +75,9 @@ if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR 
C_SUPPORTS_NODEFAULTLIBS_FLAG)
 moldname mingwex msvcrt)
 list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})
   endif()
+endif()
+
+if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
   if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
   endif ()
diff --git a/libcxxabi/cmake/config-ix.cmake b/libcxxabi/cmake/config-ix.cmake
index 39b9284b780eb..10f2087c68c5e 100644
--- a/libcxxabi/cmake/config-ix.cmake
+++ b/libcxxabi/cmake/config-ix.cmake
@@ -33,7 +33,9 @@ else()
   endif()
 endif()
 
-if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
+# Only link against compiler-rt manually if we use -nodefaultlibs, since
+# otherwise the compiler will do the right thing on its own.
+if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
   if (LIBCXXABI_HAS_C_LIB)
 list(APPEND CMAKE_REQUIRED_LIBRARIES c)
   endif ()
@@ -67,6 +69,9 @@ if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR 
C_SUPPORTS_NODEFAULTLIBS_FLAG)
 moldname mingwex msvcrt)
 list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})
   endif()
+endif()
+
+if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
   if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
   endif ()
diff --git a/libcxxabi/src/CMakeLists.txt b/libcxxabi/src/CMakeLists.txt
index 02031b69fd915..4198827203fc8 100644
--- a/libcxxabi/src/CMakeLists.txt
+++