Skip to content

Commit d786da4

Browse files
committed
Sync and add check for -nostdlib++ support.
1 parent ff7139d commit d786da4

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

libc/cmake/modules/CheckCompilerFeatures.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,6 @@ message(STATUS "Compiler features available: ${AVAILABLE_COMPILER_FEATURES}")
6262

6363
# clang-8+, gcc-12+
6464
check_cxx_compiler_flag("-ftrivial-auto-var-init=pattern" LIBC_CC_SUPPORTS_PATTERN_INIT)
65+
66+
# clang-6+, gcc-13+
67+
check_cxx_compiler_flag("-nostdlib++" LIBC_COMPILER_SUPPORT_NOSTDLIBPP)

libc/cmake/modules/LLVMLibCTestRules.cmake

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,13 @@ function(add_libc_fuzzer target_name)
435435

436436
endfunction(add_libc_fuzzer)
437437

438+
# Get libgcc_s to be used in hermetic and integration tests.
439+
if(NOT LIBC_COMPILER_SUPPORT_NOSTDLIBPP)
440+
execute_process(COMMAND ${CMAKE_CXX_COMPILER} -print-file-name=libgcc_s.so.1
441+
OUTPUT_VARIABLE LIBGCC_S_LOCATION)
442+
string(STRIP ${LIBGCC_S_LOCATION} LIBGCC_S_LOCATION)
443+
endif()
444+
438445
# DEPRECATED: Use add_hermetic_test instead.
439446
#
440447
# Rule to add an integration test. An integration test is like a unit test
@@ -564,10 +571,13 @@ function(add_integration_test test_name)
564571

565572
if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
566573
target_link_options(${fq_build_target_name} PRIVATE -nostdlib -static)
567-
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
574+
elseif(LIBC_COMPILER_SUPPORT_NOSTDLIBPP)
568575
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
569576
else()
570-
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib -static)
577+
# Older version of gcc does not support `nostdlib++` flag. We use
578+
# `nostdlib` and link against libgcc_s, which cannot be linked statically.
579+
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib)
580+
list(APPEND link_libraries ${LIBGCC_S_LOCATION})
571581
endif()
572582
target_link_libraries(
573583
${fq_build_target_name}
@@ -743,10 +753,13 @@ function(add_libc_hermetic_test test_name)
743753

744754
if(LIBC_TARGET_ARCHITECTURE_IS_GPU)
745755
target_link_options(${fq_build_target_name} PRIVATE -nostdlib -static)
746-
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
756+
elseif(LIBC_COMPILER_SUPPORT_NOSTDLIBPP)
747757
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib++ -static)
748758
else()
749-
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib -static)
759+
# Older version of gcc does not support `nostdlib++` flag. We use
760+
# `nostdlib` and link against libgcc_s, which cannot be linked statically.
761+
target_link_options(${fq_build_target_name} PRIVATE -nolibc -nostartfiles -nostdlib)
762+
list(APPEND link_libraries ${LIBGCC_S_LOCATION})
750763
endif()
751764
target_link_libraries(
752765
${fq_build_target_name}

0 commit comments

Comments
 (0)