Skip to content

Commit 75d0281

Browse files
authored
[runtimes] Probe for -nostdlib++ and -nostdinc++ with the C compiler (#108357)
While these flags semantically are relevant only for C++, we do add them to CMAKE_REQUIRED_FLAGS if they are detected. All flags in that variable are used both when testing compilation of C and C++ (and for detecting libraries, which uses the C compiler driver). Therefore, to be sure we safely can add the flags to CMAKE_REQUIRED_FLAGS, test for the option with the C language. This should fix compilation with GCC; newer versions of GCC do support the -nostdlib++ option, but it's only supported by the C++ compiler driver, not the C driver. (However, many builds of GCC also do accept the option with the C driver, if GCC was compiled with Ada support enabled, see [1]. That's why this issue isn't noticed in all configurations with GCC.) Clang does support these options in both C and C++ driver modes. This should fix #90332. [1] #90332 (comment)
1 parent a14a83d commit 75d0281

File tree

6 files changed

+35
-19
lines changed

6 files changed

+35
-19
lines changed

libcxx/cmake/config-ix.cmake

+8-4
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,13 @@ check_cxx_compiler_flag(-nolibc CXX_SUPPORTS_NOLIBC_FLAG)
3838
# required during compilation (which has the -nostdlib++ or -nodefaultlibs). libc is
3939
# required for the link to go through. We remove sanitizers from the
4040
# configuration checks to avoid spurious link errors.
41+
#
42+
# Adding flags to CMAKE_REQUIRED_FLAGS will include the flags both when testing
43+
# compilation of C and C++. Therefore test to make sure that the flags are
44+
# supported by the C compiler driver, before deciding to include them.
4145

42-
check_cxx_compiler_flag(-nostdlib++ CXX_SUPPORTS_NOSTDLIBXX_FLAG)
43-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
46+
check_c_compiler_flag(-nostdlib++ C_SUPPORTS_NOSTDLIBXX_FLAG)
47+
if (C_SUPPORTS_NOSTDLIBXX_FLAG)
4448
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
4549
else()
4650
check_c_compiler_flag(-nodefaultlibs C_SUPPORTS_NODEFAULTLIBS_FLAG)
@@ -51,7 +55,7 @@ endif()
5155

5256
# Only link against compiler-rt manually if we use -nodefaultlibs, since
5357
# otherwise the compiler will do the right thing on its own.
54-
if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
58+
if (NOT C_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
5559
if (LIBCXX_USE_COMPILER_RT)
5660
include(HandleCompilerRT)
5761
find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY
@@ -81,7 +85,7 @@ if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
8185
endif()
8286
endif()
8387

84-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
88+
if (C_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
8589
if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
8690
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
8791
endif ()

libcxxabi/cmake/config-ix.cmake

+8-4
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ endif ()
2222
# required during compilation (which has the -nodefaultlibs). libc is
2323
# required for the link to go through. We remove sanitizers from the
2424
# configuration checks to avoid spurious link errors.
25+
#
26+
# Adding flags to CMAKE_REQUIRED_FLAGS will include the flags both when testing
27+
# compilation of C and C++. Therefore test to make sure that the flags are
28+
# supported by the C compiler driver, before deciding to include them.
2529

26-
check_cxx_compiler_flag(-nostdlib++ CXX_SUPPORTS_NOSTDLIBXX_FLAG)
27-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
30+
check_c_compiler_flag(-nostdlib++ C_SUPPORTS_NOSTDLIBXX_FLAG)
31+
if (C_SUPPORTS_NOSTDLIBXX_FLAG)
2832
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
2933
else()
3034
check_c_compiler_flag(-nodefaultlibs C_SUPPORTS_NODEFAULTLIBS_FLAG)
@@ -35,7 +39,7 @@ endif()
3539

3640
# Only link against compiler-rt manually if we use -nodefaultlibs, since
3741
# otherwise the compiler will do the right thing on its own.
38-
if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
42+
if (NOT C_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
3943
if (LIBCXXABI_HAS_C_LIB)
4044
list(APPEND CMAKE_REQUIRED_LIBRARIES c)
4145
endif ()
@@ -71,7 +75,7 @@ if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
7175
endif()
7276
endif()
7377

74-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
78+
if (C_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
7579
if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
7680
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
7781
endif ()

libcxxabi/src/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ if (ANDROID AND ANDROID_PLATFORM_LEVEL LESS 21)
9191
endif()
9292

9393
# Setup flags.
94-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
94+
if (C_SUPPORTS_NOSTDLIBXX_FLAG)
9595
add_link_flags_if_supported(-nostdlib++)
9696
else()
9797
add_link_flags_if_supported(-nodefaultlibs)
@@ -167,7 +167,7 @@ if (LIBCXXABI_USE_LLVM_UNWINDER)
167167
endif()
168168
endif()
169169
target_link_libraries(cxxabi_shared_objects PRIVATE cxx-headers ${LIBCXXABI_LIBRARIES})
170-
if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG)
170+
if (NOT C_SUPPORTS_NOSTDLIBXX_FLAG)
171171
target_link_libraries(cxxabi_shared_objects PRIVATE ${LIBCXXABI_BUILTINS_LIBRARY})
172172
endif()
173173
target_link_libraries(cxxabi_shared_objects PUBLIC cxxabi-headers)

libunwind/cmake/config-ix.cmake

+8-4
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ endif()
3434
# required during compilation (which has the -nostdlib++ or -nodefaultlibs). libc is
3535
# required for the link to go through. We remove sanitizers from the
3636
# configuration checks to avoid spurious link errors.
37+
#
38+
# Adding flags to CMAKE_REQUIRED_FLAGS will include the flags both when testing
39+
# compilation of C and C++. Therefore test to make sure that the flags are
40+
# supported by the C compiler driver, before deciding to include them.
3741

38-
llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG)
39-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
42+
llvm_check_compiler_linker_flag(C "-nostdlib++" C_SUPPORTS_NOSTDLIBXX_FLAG)
43+
if (C_SUPPORTS_NOSTDLIBXX_FLAG)
4044
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
4145
else()
4246
llvm_check_compiler_linker_flag(C "-nodefaultlibs" C_SUPPORTS_NODEFAULTLIBS_FLAG)
@@ -47,7 +51,7 @@ endif()
4751

4852
# Only link against compiler-rt manually if we use -nodefaultlibs, since
4953
# otherwise the compiler will do the right thing on its own.
50-
if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
54+
if (NOT C_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
5155
if (LIBUNWIND_HAS_C_LIB)
5256
list(APPEND CMAKE_REQUIRED_LIBRARIES c)
5357
endif ()
@@ -82,7 +86,7 @@ if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
8286
endif()
8387
endif()
8488

85-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
89+
if (C_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
8690
if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
8791
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
8892
endif ()

libunwind/src/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ set(LIBUNWIND_SOURCES
6666
${LIBUNWIND_ASM_SOURCES})
6767

6868
# Generate library list.
69-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
69+
if (C_SUPPORTS_NOSTDLIBXX_FLAG)
7070
add_link_flags_if_supported(-nostdlib++)
7171
else()
7272
if (LIBUNWIND_USE_COMPILER_RT)

runtimes/CMakeLists.txt

+8-4
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,16 @@ endif()
143143
# Check for -nostdlib++ first; if there's no C++ standard library yet,
144144
# all check_cxx_compiler_flag commands will fail until we add -nostdlib++
145145
# (or -nodefaultlibs).
146-
llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG)
147-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
146+
#
147+
# Adding flags to CMAKE_REQUIRED_FLAGS will include the flags both when testing
148+
# compilation of C and C++. Therefore test to make sure that the flags are
149+
# supported by the C compiler driver, before deciding to include them.
150+
llvm_check_compiler_linker_flag(C "-nostdlib++" C_SUPPORTS_NOSTDLIBXX_FLAG)
151+
if (C_SUPPORTS_NOSTDLIBXX_FLAG)
148152
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
149153
endif()
150-
check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG)
151-
if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
154+
check_c_compiler_flag(-nostdinc++ C_SUPPORTS_NOSTDINCXX_FLAG)
155+
if (C_SUPPORTS_NOSTDINCXX_FLAG)
152156
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdinc++")
153157
endif()
154158

0 commit comments

Comments
 (0)