Skip to content

Commit e83e69b

Browse files
akoeplingerCopilot
andauthored
Unify native API availability probes (dotnet#134073)
Share deployment-target availability checks across native builds and use consistent pipe2 probes (_GNU_SOURCE feature definitions are needed for Linux support). Co-authored-by: Copilot <[email protected]> Copilot-Session: efb15d16-e184-4ac5-9673-69a0e061f9bb
1 parent d642f9c commit e83e69b

4 files changed

Lines changed: 25 additions & 22 deletions

File tree

‎eng/native/configurecompiler.cmake‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ include(CheckCCompilerFlag)
2222
include(CheckCXXCompilerFlag)
2323
include(CheckLinkerFlag)
2424

25+
# Apple platforms like macOS/iOS allow targeting older operating system versions with a single SDK,
26+
# so the mere presence of a symbol in the SDK does not tell us whether the deployment target supports it.
27+
# The compiler warns when using an API unavailable at the deployment target. Turn that into an error
28+
# for feature probes via CMAKE_REQUIRED_FLAGS, so check_symbol_exists() and other compile checks
29+
# correctly identify whether the API is supported on the target.
30+
check_c_compiler_flag("-Werror=unguarded-availability" COMPILER_SUPPORTS_W_ERROR_UNGUARDED_AVAILABILITY)
31+
if(COMPILER_SUPPORTS_W_ERROR_UNGUARDED_AVAILABILITY)
32+
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unguarded-availability")
33+
endif()
34+
2535
# "configureoptimization.cmake" must be included after CLR_CMAKE_HOST_UNIX has been set.
2636
include(${CMAKE_CURRENT_LIST_DIR}/configureoptimization.cmake)
2737

‎src/coreclr/pal/src/configure.cmake‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ include(CheckIncludeFiles)
77
include(CheckStructHasMember)
88
include(CheckTypeSize)
99
include(CheckLibraryExists)
10+
include(CMakePushCheckState)
1011

1112
if(CLR_CMAKE_TARGET_FREEBSD OR CLR_CMAKE_TARGET_OPENBSD)
1213
set(CMAKE_REQUIRED_INCLUDES ${CROSS_ROOTFS}/usr/local/include)
@@ -142,17 +143,10 @@ check_function_exists(semget HAS_SYSV_SEMAPHORES)
142143
check_function_exists(pthread_mutex_init HAS_PTHREAD_MUTEXES)
143144
check_function_exists(ttrace HAVE_TTRACE)
144145

145-
set(PREVIOUS_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
146-
if(CLR_CMAKE_HOST_APPLE)
147-
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unguarded-availability-new")
148-
endif()
149-
check_cxx_symbol_exists(pipe2 unistd.h PAL_HAVE_PIPE2)
150-
if(PAL_HAVE_PIPE2)
151-
set(HAVE_PIPE2 1 CACHE INTERNAL "" FORCE)
152-
else()
153-
set(HAVE_PIPE2 0 CACHE INTERNAL "" FORCE)
154-
endif()
155-
set(CMAKE_REQUIRED_FLAGS ${PREVIOUS_CMAKE_REQUIRED_FLAGS})
146+
cmake_push_check_state()
147+
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
148+
check_symbol_exists(pipe2 unistd.h HAVE_PIPE2)
149+
cmake_pop_check_state()
156150

157151
check_cxx_source_compiles("
158152
#include <pthread_np.h>

‎src/native/external/libunwind_extras/configure.cmake‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
include(CheckCSourceCompiles)
22
include(CheckIncludeFiles)
3-
include(CheckFunctionExists)
3+
include(CheckSymbolExists)
4+
include(CMakePushCheckState)
45

56
if(CLR_CMAKE_HOST_WIN32)
67
# Our posix abstraction layer will provide these headers
@@ -36,7 +37,10 @@ endif(CLR_CMAKE_HOST_WIN32)
3637
check_include_files(link.h HAVE_LINK_H)
3738
check_include_files(sys/link.h HAVE_SYS_LINK_H)
3839

39-
check_function_exists(pipe2 HAVE_PIPE2)
40+
cmake_push_check_state()
41+
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
42+
check_symbol_exists(pipe2 unistd.h HAVE_PIPE2)
43+
cmake_pop_check_state()
4044

4145
check_c_source_compiles("
4246
int main(int argc, char **argv)

‎src/native/libs/configure.cmake‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ include(CheckSymbolExists)
88
include(CheckTypeSize)
99
include(CheckLibraryExists)
1010
include(CheckFunctionExists)
11+
include(CMakePushCheckState)
1112

1213
if (CLR_CMAKE_TARGET_APPLE)
1314
# Xcode's clang does not include /usr/local/include by default, but brew's does.
@@ -45,15 +46,6 @@ if (CMAKE_C_COMPILER_ID MATCHES "Clang")
4546
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Wno-error=builtin-requires-header")
4647
endif()
4748

48-
# Apple platforms like macOS/iOS allow targeting older operating system versions with a single SDK,
49-
# the mere presence of a symbol in the SDK doesn't tell us whether the deployment target really supports it.
50-
# The compiler raises a warning when using an unsupported API, turn that into an error so check_symbol_exists()
51-
# can correctly identify whether the API is supported on the target.
52-
check_c_compiler_flag("-Wunguarded-availability" "C_SUPPORTS_WUNGUARDED_AVAILABILITY")
53-
if(C_SUPPORTS_WUNGUARDED_AVAILABILITY)
54-
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Wunguarded-availability")
55-
endif()
56-
5749
# in_pktinfo: Find whether this struct exists
5850
check_include_files(
5951
"sys/socket.h;linux/in.h"
@@ -206,10 +198,13 @@ check_symbol_exists(
206198
unistd.h
207199
HAVE_PIPE)
208200

201+
cmake_push_check_state()
202+
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
209203
check_symbol_exists(
210204
pipe2
211205
unistd.h
212206
HAVE_PIPE2)
207+
cmake_pop_check_state()
213208

214209
# close_range is available as a function on FreeBSD 12.2+ and Linux (glibc >= 2.34).
215210
# On Linux with older glibc it is still accessible via the __NR_close_range syscall number.

0 commit comments

Comments
 (0)