Skip to content

Commit 0e83408

Browse files
committed
[runtimes] Append -nostd*++ flags only for Clang
Append `-nostdlib++` and `-nostdinc++` flags to `CMAKE_REQUIRED_FLAGS` only if we are actually building with Clang. These flags are also passed to the C compiler, which is not allowed in GCC. Since CMake implicitly performs some tests using the C compiler, this can lead to incorrect check results. This should be safe, since FWIU we only need them when bootstrapping Clang. Even though we know that Clang supports these flags, we still need to explicitly check if they work, as in some scenarios adding `-nostdlib++` actually breaks the build. See PR #108357 for examples of that. Fixes #90332 Signed-off-by: Michał Górny <[email protected]>
1 parent f3bf8e0 commit 0e83408

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

runtimes/CMakeLists.txt

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,26 @@ 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+
# CMAKE_REQUIRED_FLAGS is used both for C and C++ compilation. This breaks
165+
# compiling with GCC (and we should not need to force -nostd* for GCC anyway),
166+
# so apply it only with Clang. However, since there are cases when appending
167+
# the flag actually breaks the build, still perform the checks rather than
168+
# appending it unconditionally.
169+
#
170+
# TODO: find a better solution. See the discussion on:
171+
# https://github.com/llvm/llvm-project/issues/90332
172+
# https://github.com/llvm/llvm-project/pull/108357
173+
174+
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
175+
llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG)
176+
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
177+
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
178+
endif()
179+
check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG)
180+
if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
181+
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdinc++")
182+
endif()
170183
endif()
171184

172185
# Avoid checking whether the compiler is working.

0 commit comments

Comments
 (0)