Skip to content

Commit c54b50f

Browse files
mstorsjocopybara-github
authored andcommitted
[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] llvm/llvm-project#90332 (comment) NOKEYCHECK=True GitOrigin-RevId: 75d0281bc81f0040c24d15bdf9c5cc46e9237224
1 parent 59d0517 commit c54b50f

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

cmake/config-ix.cmake

Lines changed: 8 additions & 4 deletions
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 ()

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
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)

0 commit comments

Comments
 (0)