Skip to content

Commit 7af584e

Browse files
committed
[libunwind] Try to add --unwindlib=none while configuring and building libunwind
If Clang is set up to link directly against libunwind (via the --unwindlib option, or the corresponding builtin default option), configuring libunwind will fail while bootstrapping (before the initial libunwind is built), because every cmake test will fail due to -lunwind not being found, and linking the shared library will fail similarly. Check if --unwindlib=none is supported, and add it in that case. Using check_c_compiler_flag on its own doesn't work, because that only adds the tested flag to the compilation command, and if -lunwind is missing, the linking step would still fail - instead try adding it to CMAKE_REQUIRED_FLAGS and restore the variable if it doesn't work. This avoids having to pass --unwindlib=none while building libunwind. Differential Revision: https://reviews.llvm.org/D112126
1 parent 7e34d5e commit 7af584e

File tree

5 files changed

+54
-3
lines changed

5 files changed

+54
-3
lines changed

cmake/Modules/CheckLinkerFlag.cmake

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
include(CMakePushCheckState)
2+
include(CheckCCompilerFlag)
3+
4+
function(llvm_check_linker_flag flag dest)
5+
# If testing a flag with check_c_compiler_flag, it gets added to the compile
6+
# command only, but not to the linker command in that test. If the flag
7+
# is vital for linking to succeed, the test would fail even if it would
8+
# have succeeded if it was included on both commands.
9+
#
10+
# Therefore, try adding the flag to CMAKE_REQUIRED_FLAGS, which gets
11+
# added to both compiling and linking commands in the tests.
12+
13+
cmake_push_check_state()
14+
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${flag}")
15+
check_c_compiler_flag("" ${dest})
16+
cmake_pop_check_state()
17+
endfunction()
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
macro(llvm_enable_language_nolink)
2+
# Set CMAKE_TRY_COMPILE_TARGET_TYPE to STATIC_LIBRARY to disable linking
3+
# in the compiler sanity checks. When bootstrapping the toolchain,
4+
# the toolchain itself is still incomplete and sanity checks that include
5+
# linking may fail.
6+
set(__SAVED_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE})
7+
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
8+
enable_language(${ARGV})
9+
set(CMAKE_TRY_COMPILE_TARGET_TYPE ${__SAVED_TRY_COMPILE_TARGET_TYPE})
10+
unset(__SAVED_TRY_COMPILE_TARGET_TYPE)
11+
endmacro()

libunwind/CMakeLists.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ set(LIBUNWIND_LIBCXX_PATH "${CMAKE_CURRENT_LIST_DIR}/../libcxx" CACHE PATH
2323
"Specify path to libc++ source.")
2424

2525
if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR LIBUNWIND_STANDALONE_BUILD)
26-
project(libunwind LANGUAGES C CXX ASM)
26+
# We may have an incomplete toolchain - do language support tests without
27+
# linking.
28+
include(EnableLanguageNolink)
29+
project(libunwind LANGUAGES NONE)
30+
llvm_enable_language_nolink(C CXX ASM)
2731

2832
set(PACKAGE_NAME libunwind)
2933
set(PACKAGE_VERSION 14.0.0git)
@@ -179,6 +183,14 @@ include(HandleLibunwindFlags)
179183
# Get required flags.
180184
add_target_flags_if(LIBUNWIND_BUILD_32_BITS "-m32")
181185

186+
# Compiler tests may be failing if the compiler implicitly links in libunwind,
187+
# which doesn't exist yet. This gets waived by --unwindlib=none
188+
# later in config-ix below, but the tests for --target etc before that may
189+
# be failing due to it. Only test compilation, not linking, for these
190+
# tests here now.
191+
set(CMAKE_TRY_COMPILE_TARGET_TYPE_ORIG ${CMAKE_TRY_COMPILE_TARGET_TYPE})
192+
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
193+
182194
if(LIBUNWIND_TARGET_TRIPLE)
183195
add_target_flags_if_supported("--target=${LIBUNWIND_TARGET_TRIPLE}")
184196
endif()
@@ -188,6 +200,7 @@ endif()
188200
if(LIBUNWIND_SYSROOT)
189201
add_target_flags_if_supported("--sysroot=${LIBUNWIND_SYSROOT}")
190202
endif()
203+
set(CMAKE_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE_ORIG})
191204

192205
# Configure compiler.
193206
include(config-ix)

libunwind/cmake/config-ix.cmake

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@ include(CMakePushCheckState)
22
include(CheckCCompilerFlag)
33
include(CheckCXXCompilerFlag)
44
include(CheckLibraryExists)
5+
include(CheckLinkerFlag)
56
include(CheckSymbolExists)
67
include(CheckCSourceCompiles)
78

9+
# The compiler driver may be implicitly trying to link against libunwind, which
10+
# might not work if libunwind doesn't exist yet. Try to check if
11+
# --unwindlib=none is supported, and use that if possible.
12+
llvm_check_linker_flag("--unwindlib=none" LIBUNWIND_SUPPORTS_UNWINDLIB_NONE_FLAG)
13+
if (LIBUNWIND_SUPPORTS_UNWINDLIB_NONE_FLAG)
14+
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --unwindlib=none")
15+
endif()
16+
817
check_library_exists(c fopen "" LIBUNWIND_HAS_C_LIB)
918

1019
if (NOT LIBUNWIND_USE_COMPILER_RT)
@@ -25,11 +34,11 @@ endif()
2534
# required for the link to go through. We remove sanitizers from the
2635
# configuration checks to avoid spurious link errors.
2736

28-
check_c_compiler_flag(-nostdlib++ LIBUNWIND_SUPPORTS_NOSTDLIBXX_FLAG)
37+
llvm_check_linker_flag(-nostdlib++ LIBUNWIND_SUPPORTS_NOSTDLIBXX_FLAG)
2938
if (LIBUNWIND_SUPPORTS_NOSTDLIBXX_FLAG)
3039
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
3140
else()
32-
check_c_compiler_flag(-nodefaultlibs LIBUNWIND_SUPPORTS_NODEFAULTLIBS_FLAG)
41+
llvm_check_linker_flag(-nodefaultlibs LIBUNWIND_SUPPORTS_NODEFAULTLIBS_FLAG)
3342
if (LIBUNWIND_SUPPORTS_NODEFAULTLIBS_FLAG)
3443
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs")
3544
endif()

libunwind/src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ if (LIBUNWIND_ENABLE_THREADS)
8383
endif()
8484

8585
# Setup flags.
86+
add_link_flags_if(LIBUNWIND_SUPPORTS_UNWINDLIB_NONE_FLAG --unwindlib=none)
8687
if (LIBUNWIND_SUPPORTS_NOSTDLIBXX_FLAG)
8788
add_link_flags_if_supported(-nostdlib++)
8889
else()

0 commit comments

Comments
 (0)