Skip to content

Commit 6fdffd0

Browse files
committed
[runtimes] Append -nostd*++ flags only if necessary
Append `-nostd*++` flags only if we have checked that a trivial C++ program cannot link without them. This is meant as a workaround for #90332. While the flags could still incorrectly be passed to the C compiler, this should at least no longer happen when we are using a C++ compiler with the C++ runtime provided, which is the case Gentoo was hitting. Fixes #90332 Signed-off-by: Michał Górny <[email protected]>
1 parent 1862e3c commit 6fdffd0

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

runtimes/CMakeLists.txt

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,25 @@ endif()
160160
# Check for -nostdlib++ first; if there's no C++ standard library yet,
161161
# all check_cxx_compiler_flag commands will fail until we add -nostdlib++
162162
# (or -nodefaultlibs).
163-
llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG)
164-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
165-
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
166-
endif()
167-
check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG)
168-
if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
169-
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdinc++")
163+
164+
# TODO: this is incorrect, as these variables are passed to the C compiler.
165+
# Until a proper solution is found, append them only if necessary to avoid
166+
# some of the resulting breakage.
167+
# See https://github.com/llvm/llvm-project/issues/90332
168+
check_cxx_source_compiles("int main() {return 0;}" CXX_LINKS_WITHOUT_NOSTD)
169+
if (NOT CXX_LINKS_WITHOUT_NOSTD)
170+
llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG)
171+
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
172+
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
173+
endif()
174+
check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG)
175+
if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
176+
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdinc++")
177+
endif()
178+
check_cxx_source_compiles("int main() {return 0;}" CXX_LINKS_WITH_NOSTD)
179+
if (NOT CXX_LINKS_WITH_NOSTD)
180+
message(FATAL_ERROR "C++ compiler fails to link C++ programs, check CMake logs")
181+
endif()
170182
endif()
171183

172184
# Avoid checking whether the compiler is working.

0 commit comments

Comments
 (0)