Skip to content

Commit 4169b52

Browse files
committed
[runtimes] [CMake] Fix checks for -Werror when building with incomplete toolchains
When we add `--unwindlib=none` during the CMake configure phase (to make CMake linking tests succeed before the unwind library has been built for the first time - when bootstrapping a cross toolchain from scratch), we add it to `CMAKE_REQUIRED_FLAGS` to make later CMake tests pass. When the option is added to `CMAKE_REQUIRED_FLAGS`, it gets added to both compilation and linking commands. When --unwindlib=none is added to the compilation command, it causes warnings (about being unused during compilation, as it only affects linking). When all CMake test compilations produce warnings, later CMake tests for `-Werror` fail. Add `--{start,end}-no-unused-arguments` around `--unwindlib=none`, if supported, to avoid unnecessary warnings due to this option. If the CMake requirement is bumped to 3.14, we could use `CMAKE_REQUIRED_LINK_OPTIONS` instead, removing the need for the `--{start,end}-no-unused-arguments` options. (However, do note that `CMAKE_REQUIRED_LINK_OPTIONS` is problematic in combination with `CMAKE_TRY_COMPILE_TARGET_TYPE` set to `STATIC_LIBRARY`; see https://gitlab.kitware.com/cmake/cmake/-/issues/23454.) Differential Revision: https://reviews.llvm.org/D124377
1 parent 0298cce commit 4169b52

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

runtimes/CMakeLists.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,27 @@ if (NOT LLVM_RUNTIMES_LINKING_WORKS)
102102
# e.g. ASAN/TSAN.
103103
llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
104104
if (CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
105+
set(ORIG_CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS}")
105106
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --unwindlib=none")
107+
# TODO: When we can require CMake 3.14, we should use
108+
# CMAKE_REQUIRED_LINK_OPTIONS here. Until then, we need a workaround:
109+
# When using CMAKE_REQUIRED_FLAGS, this option gets added both to
110+
# compilation and linking commands. That causes warnings in the
111+
# compilation commands during cmake tests. This is normally benign, but
112+
# when testing whether -Werror works, that test fails (due to the
113+
# preexisting warning).
114+
#
115+
# Therefore, before we can use CMAKE_REQUIRED_LINK_OPTIONS, check if we
116+
# can use --start-no-unused-arguments to silence the warnings about
117+
# --unwindlib=none during compilation.
118+
#
119+
# We must first add --unwindlib=none to CMAKE_REQUIRED_FLAGS above, to
120+
# allow this subsequent test to succeed, then rewrite CMAKE_REQUIRED_FLAGS
121+
# below.
122+
check_c_compiler_flag("--start-no-unused-arguments" C_SUPPORTS_START_NO_UNUSED_ARGUMENTS)
123+
if (C_SUPPORTS_START_NO_UNUSED_ARGUMENTS)
124+
set(CMAKE_REQUIRED_FLAGS "${ORIG_CMAKE_REQUIRED_FLAGS} --start-no-unused-arguments --unwindlib=none --end-no-unused-arguments")
125+
endif()
106126
endif()
107127
endif()
108128

0 commit comments

Comments
 (0)