Skip to content

Commit 36bb134

Browse files
authored
[libc++] Use -nostdlib++ on GCC unconditionally (#68832)
We support GCC 13, which supports the flag. This allows simplifying the CMake logic around the use of -nostdlib++. Note that there are other places where we don't assume -nostdlib++ yet in the build, but this patch is intentionally trying to be small because this part of our CMake is pretty tricky.
1 parent 844c731 commit 36bb134

File tree

3 files changed

+3
-46
lines changed

3 files changed

+3
-46
lines changed

libcxx/CMakeLists.txt

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -642,18 +642,8 @@ get_sanitizer_flags(SANITIZER_FLAGS "${LLVM_USE_SANITIZER}")
642642

643643
# Link system libraries =======================================================
644644
function(cxx_link_system_libraries target)
645-
646-
# In order to remove just libc++ from the link step
647-
# we need to use -nostdlib++ whenever it is supported.
648-
# Unfortunately this cannot be used universally because for example g++ supports
649-
# only -nodefaultlibs in which case all libraries will be removed and
650-
# all libraries but c++ have to be added in manually.
651-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
652-
target_add_link_flags_if_supported(${target} PRIVATE "-nostdlib++")
653-
else()
654-
target_add_link_flags_if_supported(${target} PRIVATE "-nodefaultlibs")
655-
target_add_compile_flags_if_supported(${target} PRIVATE "/Zl")
656-
target_add_link_flags_if_supported(${target} PRIVATE "/nodefaultlib")
645+
if (NOT MSVC)
646+
target_link_libraries(${target} PRIVATE "-nostdlib++")
657647
endif()
658648

659649
if (CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG AND LIBCXXABI_USE_LLVM_UNWINDER)
@@ -663,24 +653,6 @@ function(cxx_link_system_libraries target)
663653
target_add_link_flags_if_supported(${target} PRIVATE "--unwindlib=none")
664654
endif()
665655

666-
if (NOT APPLE) # On Apple platforms, we always use -nostdlib++ so we don't need to re-add other libraries
667-
if (LIBCXX_HAS_PTHREAD_LIB)
668-
target_link_libraries(${target} PRIVATE pthread)
669-
endif()
670-
671-
if (LIBCXX_HAS_C_LIB)
672-
target_link_libraries(${target} PRIVATE c)
673-
endif()
674-
675-
if (LIBCXX_HAS_M_LIB)
676-
target_link_libraries(${target} PRIVATE m)
677-
endif()
678-
679-
if (LIBCXX_HAS_RT_LIB)
680-
target_link_libraries(${target} PRIVATE rt)
681-
endif()
682-
endif()
683-
684656
if (LIBCXX_USE_COMPILER_RT)
685657
find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY)
686658
if (LIBCXX_BUILTINS_LIBRARY)

libcxx/benchmarks/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ endif()
122122
add_library( cxx-benchmarks-flags-libcxx INTERFACE)
123123
target_link_libraries( cxx-benchmarks-flags-libcxx INTERFACE cxx-benchmarks-flags)
124124
target_compile_options(cxx-benchmarks-flags-libcxx INTERFACE ${SANITIZER_FLAGS} -Wno-user-defined-literals -Wno-suggest-override)
125-
target_link_options( cxx-benchmarks-flags-libcxx INTERFACE -nodefaultlibs "-L${BENCHMARK_LIBCXX_INSTALL}/lib" "-L${BENCHMARK_LIBCXX_INSTALL}/lib64" ${SANITIZER_FLAGS})
125+
target_link_options( cxx-benchmarks-flags-libcxx INTERFACE -nostdlib++ "-L${BENCHMARK_LIBCXX_INSTALL}/lib" "-L${BENCHMARK_LIBCXX_INSTALL}/lib64" ${SANITIZER_FLAGS})
126126

127127
set(libcxx_benchmark_targets)
128128

libcxx/cmake/config-ix.cmake

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@ include(CheckCSourceCompiles)
1414
# link with --uwnindlib=none. Check if that option works.
1515
llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
1616

17-
if(WIN32 AND NOT MINGW)
18-
# NOTE(compnerd) this is technically a lie, there is msvcrt, but for now, lets
19-
# let the default linking take care of that.
20-
set(LIBCXX_HAS_C_LIB NO)
21-
else()
22-
check_library_exists(c fopen "" LIBCXX_HAS_C_LIB)
23-
endif()
24-
2517
if (NOT LIBCXX_USE_COMPILER_RT)
2618
if(WIN32 AND NOT MINGW)
2719
set(LIBCXX_HAS_GCC_S_LIB NO)
@@ -54,9 +46,6 @@ else()
5446
endif()
5547

5648
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
57-
if (LIBCXX_HAS_C_LIB)
58-
list(APPEND CMAKE_REQUIRED_LIBRARIES c)
59-
endif ()
6049
if (LIBCXX_USE_COMPILER_RT)
6150
include(HandleCompilerRT)
6251
find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY
@@ -108,22 +97,18 @@ if(WIN32 AND NOT MINGW)
10897
# TODO(compnerd) do we want to support an emulation layer that allows for the
10998
# use of pthread-win32 or similar libraries to emulate pthreads on Windows?
11099
set(LIBCXX_HAS_PTHREAD_LIB NO)
111-
set(LIBCXX_HAS_M_LIB NO)
112100
set(LIBCXX_HAS_RT_LIB NO)
113101
set(LIBCXX_HAS_ATOMIC_LIB NO)
114102
elseif(APPLE)
115103
set(LIBCXX_HAS_PTHREAD_LIB NO)
116-
set(LIBCXX_HAS_M_LIB NO)
117104
set(LIBCXX_HAS_RT_LIB NO)
118105
set(LIBCXX_HAS_ATOMIC_LIB NO)
119106
elseif(FUCHSIA)
120-
set(LIBCXX_HAS_M_LIB NO)
121107
set(LIBCXX_HAS_PTHREAD_LIB NO)
122108
set(LIBCXX_HAS_RT_LIB NO)
123109
check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB)
124110
else()
125111
check_library_exists(pthread pthread_create "" LIBCXX_HAS_PTHREAD_LIB)
126-
check_library_exists(m ccos "" LIBCXX_HAS_M_LIB)
127112
check_library_exists(rt clock_gettime "" LIBCXX_HAS_RT_LIB)
128113
check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB)
129114
endif()

0 commit comments

Comments
 (0)