[runtimes] Fix building initial libunwind+libcxxabi+libcxx with compiler implied -lunwind
This does mostly the same as D112126, but for the runtimes cmake files.
Most of that is straightforward, but the interdependency between
libcxx and libunwind is tricky:
Libunwind is built at the same time as libcxx, but libunwind is not
installed yet. LIBCXXABI_USE_LLVM_UNWINDER makes libcxx link directly
against the just-built libunwind, but the compiler implicit -lunwind
isn't found. This patch avoids that by adding --unwindlib=none if
supported, if we are going to link explicitly against a newly built
unwinder anyway.
Differential Revision: https://reviews.llvm.org/D113253
GitOrigin-RevId: 7c3d19ab7bcb79636bd65ee55a0fefef224fcb25
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 5ab8a30..dafc6ce 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -773,6 +773,13 @@
target_add_link_flags_if_supported(${target} PRIVATE "/nodefaultlib")
endif()
+ if (LIBCXX_SUPPORTS_UNWINDLIB_NONE_FLAG AND LIBCXXABI_USE_LLVM_UNWINDER)
+ # If we're linking directly against the libunwind that we're building
+ # in the same invocation, don't try to link in the toolchain's
+ # default libunwind (which may be missing still).
+ target_add_link_flags_if_supported(${target} PRIVATE "--unwindlib=none")
+ endif()
+
if (LIBCXX_HAS_SYSTEM_LIB)
target_link_libraries(${target} PRIVATE System)
endif()
diff --git a/cmake/config-ix.cmake b/cmake/config-ix.cmake
index 8ca8b14..167ea81 100644
--- a/cmake/config-ix.cmake
+++ b/cmake/config-ix.cmake
@@ -1,9 +1,22 @@
include(CMakePushCheckState)
include(CheckLibraryExists)
+include(CheckLinkerFlag)
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
include(CheckCSourceCompiles)
+# The compiler driver may be implicitly trying to link against libunwind.
+# This is normally ok (libcxx relies on an unwinder), but if libunwind is
+# built in the same cmake invocation as libcxx and we've got
+# LIBCXXABI_USE_LLVM_UNWINDER set, we'd be linking against the just-built
+# libunwind (and the compiler implicit -lunwind wouldn't succeed as the newly
+# built libunwind isn't installed yet). For those cases, it'd be good to
+# link with --uwnindlib=none. Check if that option works.
+llvm_check_linker_flag("--unwindlib=none" LIBCXX_SUPPORTS_UNWINDLIB_NONE_FLAG)
+if (LIBCXX_SUPPORTS_UNWINDLIB_NONE_FLAG)
+ set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --unwindlib=none")
+endif()
+
if(WIN32 AND NOT MINGW)
# NOTE(compnerd) this is technically a lie, there is msvcrt, but for now, lets
# let the default linking take care of that.