From 6fdffd08ef687bf86213caf1900ef810d15eaeab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Mon, 4 Aug 2025 11:06:13 +0200 Subject: [PATCH] [runtimes] Append `-nostd*++` flags only if necessary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- runtimes/CMakeLists.txt | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/runtimes/CMakeLists.txt b/runtimes/CMakeLists.txt index bfb4341b172cc..27658832e93e1 100644 --- a/runtimes/CMakeLists.txt +++ b/runtimes/CMakeLists.txt @@ -160,13 +160,25 @@ endif() # Check for -nostdlib++ first; if there's no C++ standard library yet, # all check_cxx_compiler_flag commands will fail until we add -nostdlib++ # (or -nodefaultlibs). -llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG) -if (CXX_SUPPORTS_NOSTDLIBXX_FLAG) - set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++") -endif() -check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG) -if (CXX_SUPPORTS_NOSTDINCXX_FLAG) - set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdinc++") + +# TODO: this is incorrect, as these variables are passed to the C compiler. +# Until a proper solution is found, append them only if necessary to avoid +# some of the resulting breakage. +# See https://github.com/llvm/llvm-project/issues/90332 +check_cxx_source_compiles("int main() {return 0;}" CXX_LINKS_WITHOUT_NOSTD) +if (NOT CXX_LINKS_WITHOUT_NOSTD) + llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG) + if (CXX_SUPPORTS_NOSTDLIBXX_FLAG) + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++") + endif() + check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG) + if (CXX_SUPPORTS_NOSTDINCXX_FLAG) + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdinc++") + endif() + check_cxx_source_compiles("int main() {return 0;}" CXX_LINKS_WITH_NOSTD) + if (NOT CXX_LINKS_WITH_NOSTD) + message(FATAL_ERROR "C++ compiler fails to link C++ programs, check CMake logs") + endif() endif() # Avoid checking whether the compiler is working.