Skip to content

Commit 7444457

Browse files
committed
[cmake] run _all_ flag checks in runtimes/CMakeLists.txt without linker
1 parent a8dd830 commit 7444457

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

libunwind/CMakeLists.txt

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,23 @@ if (LIBUNWIND_USE_COMPILER_RT AND NOT LIBUNWIND_HAS_NODEFAULTLIBS_FLAG)
175175
list(APPEND LIBUNWIND_LINK_FLAGS "-rtlib=compiler-rt")
176176
endif()
177177

178+
# Disable linker for running CMake checks
179+
#
180+
# This was originally added because when building libunwind for ARM Linux,
181+
# we need to pass the -funwind-tables flag in order for it to work properly
182+
# with ARM EHABI. However, this produces a false negative when performing CMake
183+
# checks, causing libunwind to not be built with this flag.
184+
#
185+
# A similar dynamic occurred with the advent of CMAKE_REQUIRED_LINK_OPTIONS
186+
# (c.f. CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG), which causes flag checks for static
187+
# targets to fail due to https://gitlab.kitware.com/cmake/cmake/-/issues/23454.
188+
# Thus, to avoid failures with static targets, we cache the target type here,
189+
# and reset it after the various flag support checks have been performed.
190+
set(_previous_CMAKE_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE})
191+
set(_previous_CMAKE_REQUIRED_LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
192+
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
193+
set(CMAKE_REQUIRED_LINK_OPTIONS)
194+
178195
add_compile_flags_if_supported(-Werror=return-type)
179196

180197
if (LIBUNWIND_ENABLE_CET)
@@ -207,38 +224,16 @@ endif()
207224
add_cxx_compile_flags_if_supported(-fstrict-aliasing)
208225
add_cxx_compile_flags_if_supported(-EHsc)
209226

210-
# Don't run the linker in this CMake check.
211-
#
212-
# The reason why this was added is that when building libunwind for
213-
# ARM Linux, we need to pass the -funwind-tables flag in order for it to
214-
# work properly with ARM EHABI.
215-
#
216-
# However, when performing CMake checks, adding this flag causes the check
217-
# to produce a false negative, because the compiler generates calls
218-
# to __aeabi_unwind_cpp_pr0, which is defined in libunwind itself,
219-
# which isn't built yet, so the linker complains about undefined symbols.
220-
#
221-
# This leads to libunwind not being built with this flag, which makes
222-
# libunwind quite useless in this setup.
223-
#
224-
# NOTE: we need to work around https://gitlab.kitware.com/cmake/cmake/-/issues/23454
225-
# because CMAKE_REQUIRED_LINK_OPTIONS (c.f. CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
226-
# is incompatible with CMAKE_TRY_COMPILE_TARGET_TYPE==STATIC_LIBRARY.
227-
set(_previous_CMAKE_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE})
228-
set(_previous_CMAKE_REQUIRED_LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
229-
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
230-
set(CMAKE_REQUIRED_LINK_OPTIONS)
231227
add_compile_flags_if_supported(-funwind-tables)
232-
add_cxx_compile_flags_if_supported(-fno-exceptions)
233-
add_cxx_compile_flags_if_supported(-fno-rtti)
234-
set(CMAKE_TRY_COMPILE_TARGET_TYPE ${_previous_CMAKE_TRY_COMPILE_TARGET_TYPE})
235-
set(CMAKE_REQUIRED_LINK_OPTIONS ${_previous_CMAKE_REQUIRED_LINK_OPTIONS})
236228

237229
if (LIBUNWIND_USES_ARM_EHABI AND NOT CXX_SUPPORTS_FUNWIND_TABLES_FLAG)
238230
message(SEND_ERROR "The -funwind-tables flag must be supported "
239231
"because this target uses ARM Exception Handling ABI")
240232
endif()
241233

234+
add_cxx_compile_flags_if_supported(-fno-exceptions)
235+
add_cxx_compile_flags_if_supported(-fno-rtti)
236+
242237
# Ensure that we don't depend on C++ standard library.
243238
if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
244239
list(APPEND LIBUNWIND_COMPILE_FLAGS -nostdinc++)
@@ -292,6 +287,10 @@ if (LIBUNWIND_ENABLE_ARM_WMMX)
292287
add_compile_flags(-D__ARM_WMMX)
293288
endif()
294289

290+
# reset CMAKE_TRY_COMPILE_TARGET_TYPE & CMAKE_REQUIRED_LINK_OPTIONS after flag checks
291+
set(CMAKE_TRY_COMPILE_TARGET_TYPE ${_previous_CMAKE_TRY_COMPILE_TARGET_TYPE})
292+
set(CMAKE_REQUIRED_LINK_OPTIONS ${_previous_CMAKE_REQUIRED_LINK_OPTIONS})
293+
295294
if(LIBUNWIND_IS_BAREMETAL)
296295
add_compile_definitions(_LIBUNWIND_IS_BAREMETAL)
297296
endif()

0 commit comments

Comments
 (0)