Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[runtimes] Probe for -nostdlib++ and -nostdinc++ with the C compiler #108357

Merged
merged 1 commit into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions libcxx/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@ check_cxx_compiler_flag(-nolibc CXX_SUPPORTS_NOLIBC_FLAG)
# required during compilation (which has the -nostdlib++ or -nodefaultlibs). libc is
# required for the link to go through. We remove sanitizers from the
# configuration checks to avoid spurious link errors.
#
# Adding flags to CMAKE_REQUIRED_FLAGS will include the flags both when testing
# compilation of C and C++. Therefore test to make sure that the flags are
# supported by the C compiler driver, before deciding to include them.

check_cxx_compiler_flag(-nostdlib++ CXX_SUPPORTS_NOSTDLIBXX_FLAG)
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
check_c_compiler_flag(-nostdlib++ C_SUPPORTS_NOSTDLIBXX_FLAG)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we compile any C sources in libc++? Why do we care what flags the C compiler supports for libc++ and libc++abi? (I understand why it matters for libunwind where we do have C sources).

The reason I'm asking is that there are some simplifications we can make if we assume that all supported compilers accept -nostdlib++, and this patch makes it impossible to make that assumption.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that even if we don’t use any C sources outright, cmake uses C compilation tests when you do a check for whether a library exists. And these are subtly broken if building with GCC right now. (To make matters worse, not all GCC versions are affected. But that’s a bug - GCC intends to reject these options in the C frontend.) IIRC one of the issues mentioned in the linked bug was about library detection in libcxxabi.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that even if we don’t use any C sources outright, cmake uses C compilation tests when you do a check for whether a library exists.

Do you mean these checks?

check_library_exists(pthread pthread_create "" LIBCXX_HAS_PTHREAD_LIB)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly those; any of those basic compiler tests, check_library_exists (used a couple of times in libcxxabi and libcxx), check_include_file (not used in libcxxabi/libcxx, but used in openmp, and also struck by the same issue, in #90332), are executed as plain C compilation, and they will fail if we have stuck C++-only flags in CMAKE_REQUIRED_FLAGS.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ping

if (C_SUPPORTS_NOSTDLIBXX_FLAG)
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
else()
check_c_compiler_flag(-nodefaultlibs C_SUPPORTS_NODEFAULTLIBS_FLAG)
Expand All @@ -51,7 +55,7 @@ endif()

# Only link against compiler-rt manually if we use -nodefaultlibs, since
# otherwise the compiler will do the right thing on its own.
if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
if (NOT C_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
if (LIBCXX_USE_COMPILER_RT)
include(HandleCompilerRT)
find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY
Expand Down Expand Up @@ -81,7 +85,7 @@ if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
endif()
endif()

if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
if (C_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
endif ()
Expand Down
12 changes: 8 additions & 4 deletions libcxxabi/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ endif ()
# required during compilation (which has the -nodefaultlibs). libc is
# required for the link to go through. We remove sanitizers from the
# configuration checks to avoid spurious link errors.
#
# Adding flags to CMAKE_REQUIRED_FLAGS will include the flags both when testing
# compilation of C and C++. Therefore test to make sure that the flags are
# supported by the C compiler driver, before deciding to include them.

check_cxx_compiler_flag(-nostdlib++ CXX_SUPPORTS_NOSTDLIBXX_FLAG)
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
check_c_compiler_flag(-nostdlib++ C_SUPPORTS_NOSTDLIBXX_FLAG)
if (C_SUPPORTS_NOSTDLIBXX_FLAG)
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
else()
check_c_compiler_flag(-nodefaultlibs C_SUPPORTS_NODEFAULTLIBS_FLAG)
Expand All @@ -35,7 +39,7 @@ endif()

# Only link against compiler-rt manually if we use -nodefaultlibs, since
# otherwise the compiler will do the right thing on its own.
if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
if (NOT C_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
if (LIBCXXABI_HAS_C_LIB)
list(APPEND CMAKE_REQUIRED_LIBRARIES c)
endif ()
Expand Down Expand Up @@ -71,7 +75,7 @@ if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
endif()
endif()

if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
if (C_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
endif ()
Expand Down
4 changes: 2 additions & 2 deletions libcxxabi/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ if (ANDROID AND ANDROID_PLATFORM_LEVEL LESS 21)
endif()

# Setup flags.
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
if (C_SUPPORTS_NOSTDLIBXX_FLAG)
add_link_flags_if_supported(-nostdlib++)
else()
add_link_flags_if_supported(-nodefaultlibs)
Expand Down Expand Up @@ -167,7 +167,7 @@ if (LIBCXXABI_USE_LLVM_UNWINDER)
endif()
endif()
target_link_libraries(cxxabi_shared_objects PRIVATE cxx-headers ${LIBCXXABI_LIBRARIES})
if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG)
if (NOT C_SUPPORTS_NOSTDLIBXX_FLAG)
target_link_libraries(cxxabi_shared_objects PRIVATE ${LIBCXXABI_BUILTINS_LIBRARY})
endif()
target_link_libraries(cxxabi_shared_objects PUBLIC cxxabi-headers)
Expand Down
12 changes: 8 additions & 4 deletions libunwind/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,13 @@ endif()
# required during compilation (which has the -nostdlib++ or -nodefaultlibs). libc is
# required for the link to go through. We remove sanitizers from the
# configuration checks to avoid spurious link errors.
#
# Adding flags to CMAKE_REQUIRED_FLAGS will include the flags both when testing
# compilation of C and C++. Therefore test to make sure that the flags are
# supported by the C compiler driver, before deciding to include them.

llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG)
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
llvm_check_compiler_linker_flag(C "-nostdlib++" C_SUPPORTS_NOSTDLIBXX_FLAG)
if (C_SUPPORTS_NOSTDLIBXX_FLAG)
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
else()
llvm_check_compiler_linker_flag(C "-nodefaultlibs" C_SUPPORTS_NODEFAULTLIBS_FLAG)
Expand All @@ -47,7 +51,7 @@ endif()

# Only link against compiler-rt manually if we use -nodefaultlibs, since
# otherwise the compiler will do the right thing on its own.
if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
if (NOT C_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
if (LIBUNWIND_HAS_C_LIB)
list(APPEND CMAKE_REQUIRED_LIBRARIES c)
endif ()
Expand Down Expand Up @@ -82,7 +86,7 @@ if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
endif()
endif()

if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
if (C_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
endif ()
Expand Down
2 changes: 1 addition & 1 deletion libunwind/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ set(LIBUNWIND_SOURCES
${LIBUNWIND_ASM_SOURCES})

# Generate library list.
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
if (C_SUPPORTS_NOSTDLIBXX_FLAG)
add_link_flags_if_supported(-nostdlib++)
else()
if (LIBUNWIND_USE_COMPILER_RT)
Expand Down
12 changes: 8 additions & 4 deletions runtimes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,16 @@ endif()
# Check for -nostdlib++ first; if there's no C++ standard library yet,
# all check_cxx_compiler_flag commands will fail until we add -nostdlib++
# (or -nodefaultlibs).
llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG)
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
#
# Adding flags to CMAKE_REQUIRED_FLAGS will include the flags both when testing
# compilation of C and C++. Therefore test to make sure that the flags are
# supported by the C compiler driver, before deciding to include them.
llvm_check_compiler_linker_flag(C "-nostdlib++" C_SUPPORTS_NOSTDLIBXX_FLAG)
if (C_SUPPORTS_NOSTDLIBXX_FLAG)
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
endif()
check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG)
if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
check_c_compiler_flag(-nostdinc++ C_SUPPORTS_NOSTDINCXX_FLAG)
if (C_SUPPORTS_NOSTDINCXX_FLAG)
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdinc++")
endif()

Expand Down
Loading