Skip to content

Commit 042b266

Browse files
committed
[runtimes] Probe for -nostdlib++ and -nostdinc++ with the C compiler
If they are detected, we add them to CMAKE_REQUIRED_FLAGS. 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.) [1] llvm#90332 (comment)
1 parent 6009708 commit 042b266

File tree

6 files changed

+31
-15
lines changed

6 files changed

+31
-15
lines changed

libcxx/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ function(cxx_link_system_libraries target)
631631
# Unfortunately this cannot be used universally because for example g++ supports
632632
# only -nodefaultlibs in which case all libraries will be removed and
633633
# all libraries but c++ have to be added in manually.
634-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
634+
if (C_SUPPORTS_NOSTDLIBXX_FLAG)
635635
target_add_link_flags_if_supported(${target} PRIVATE "-nostdlib++")
636636
else()
637637
target_add_link_flags_if_supported(${target} PRIVATE "-nodefaultlibs")

libcxx/cmake/config-ix.cmake

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ endif()
4242
# required during compilation (which has the -nostdlib++ or -nodefaultlibs). libc is
4343
# required for the link to go through. We remove sanitizers from the
4444
# configuration checks to avoid spurious link errors.
45+
#
46+
# Adding flags to CMAKE_REQUIRED_FLAGS will include the flags both when testing
47+
# compilation of C and C++. Therefore test to make sure that the flags are
48+
# supported by the C compiler driver, before deciding to include them.
4549

46-
check_cxx_compiler_flag(-nostdlib++ CXX_SUPPORTS_NOSTDLIBXX_FLAG)
47-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
50+
check_c_compiler_flag(-nostdlib++ C_SUPPORTS_NOSTDLIBXX_FLAG)
51+
if (C_SUPPORTS_NOSTDLIBXX_FLAG)
4852
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
4953
else()
5054
check_c_compiler_flag(-nodefaultlibs C_SUPPORTS_NODEFAULTLIBS_FLAG)
@@ -53,7 +57,7 @@ else()
5357
endif()
5458
endif()
5559

56-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
60+
if (C_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
5761
if (LIBCXX_HAS_C_LIB)
5862
list(APPEND CMAKE_REQUIRED_LIBRARIES c)
5963
endif ()

libcxxabi/cmake/config-ix.cmake

Lines changed: 7 additions & 3 deletions
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)
@@ -33,7 +37,7 @@ else()
3337
endif()
3438
endif()
3539

36-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
40+
if (C_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
3741
if (LIBCXXABI_HAS_C_LIB)
3842
list(APPEND CMAKE_REQUIRED_LIBRARIES c)
3943
endif ()

libcxxabi/src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ if (ANDROID AND ANDROID_PLATFORM_LEVEL LESS 21)
9393
endif()
9494

9595
# Setup flags.
96-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
96+
if (C_SUPPORTS_NOSTDLIBXX_FLAG)
9797
add_link_flags_if_supported(-nostdlib++)
9898
else()
9999
add_link_flags_if_supported(-nodefaultlibs)

libunwind/cmake/config-ix.cmake

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ endif()
3030
# required during compilation (which has the -nostdlib++ or -nodefaultlibs). libc is
3131
# required for the link to go through. We remove sanitizers from the
3232
# configuration checks to avoid spurious link errors.
33+
#
34+
# Adding flags to CMAKE_REQUIRED_FLAGS will include the flags both when testing
35+
# compilation of C and C++. Therefore test to make sure that the flags are
36+
# supported by the C compiler driver, before deciding to include them.
3337

34-
llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG)
35-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
38+
llvm_check_compiler_linker_flag(C "-nostdlib++" C_SUPPORTS_NOSTDLIBXX_FLAG)
39+
if (C_SUPPORTS_NOSTDLIBXX_FLAG)
3640
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
3741
else()
3842
llvm_check_compiler_linker_flag(C "-nodefaultlibs" C_SUPPORTS_NODEFAULTLIBS_FLAG)
@@ -41,7 +45,7 @@ else()
4145
endif()
4246
endif()
4347

44-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
48+
if (C_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
4549
if (LIBUNWIND_HAS_C_LIB)
4650
list(APPEND CMAKE_REQUIRED_LIBRARIES c)
4751
endif ()

runtimes/CMakeLists.txt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,16 @@ endif()
139139
# Check for -nostdlib++ first; if there's no C++ standard library yet,
140140
# all check_cxx_compiler_flag commands will fail until we add -nostdlib++
141141
# (or -nodefaultlibs).
142-
llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG)
143-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
142+
#
143+
# Adding flags to CMAKE_REQUIRED_FLAGS will include the flags both when testing
144+
# compilation of C and C++. Therefore test to make sure that the flags are
145+
# supported by the C compiler driver, before deciding to include them.
146+
llvm_check_compiler_linker_flag(C "-nostdlib++" C_SUPPORTS_NOSTDLIBXX_FLAG)
147+
if (C_SUPPORTS_NOSTDLIBXX_FLAG)
144148
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
145149
endif()
146-
check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG)
147-
if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
150+
check_c_compiler_flag(-nostdinc++ C_SUPPORTS_NOSTDINCXX_FLAG)
151+
if (C_SUPPORTS_NOSTDINCXX_FLAG)
148152
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdinc++")
149153
endif()
150154

0 commit comments

Comments
 (0)