cmake: use cmake_push_check_state() around feature checks

Enclose
`CMAKE_EXTRA_INCLUDE_FILES`,
`CMAKE_REQUIRED_DEFINITIONS`,
`CMAKE_REQUIRED_FLAGS`,
`CMAKE_REQUIRED_INCLUDES`,
`CMAKE_REQUIRED_LIBRARIES`,
`CMAKE_REQUIRED_LINK_OPTIONS`,
settings within `cmake_push_check_state()`/`cmake_pop_check_state()`
calls. It prevents spilling them into other feature checks. It also
replaces manual resets found in some places (which can have
the undesired side-effect of destroying values meant for global use.)

Cherry-picked from #15157
Closes #15251
This commit is contained in:
Viktor Szakats 2024-10-05 01:12:44 +02:00
parent ae5e538e57
commit 91519bfb74
No known key found for this signature in database
GPG Key ID: B5ABD165E2AEF201
4 changed files with 21 additions and 19 deletions

View File

@ -176,6 +176,7 @@ if(NOT _GSS_FOUND) # Not found by pkg-config. Let us take more traditional appr
)
if(_GSS_INCLUDE_DIRS) # jay, we have found something
cmake_push_check_state()
set(CMAKE_REQUIRED_INCLUDES "${_GSS_INCLUDE_DIRS}")
check_include_files("gssapi/gssapi_generic.h;gssapi/gssapi_krb5.h" _gss_have_mit_headers)
@ -190,8 +191,8 @@ if(NOT _GSS_FOUND) # Not found by pkg-config. Let us take more traditional appr
if(_gss_have_roken_h OR _gss_have_heimdal_roken_h)
set(GSS_FLAVOUR "Heimdal")
endif()
list(REMOVE_ITEM CMAKE_REQUIRED_DEFINITIONS "-D__ROKEN_H__")
endif()
cmake_pop_check_state()
else()
# I am not convinced if this is the right way but this is what autotools do at the moment
find_path(_GSS_INCLUDE_DIRS NAMES "gssapi.h"

View File

@ -35,6 +35,7 @@ endmacro()
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
if(NOT DEFINED HAVE_STRUCT_SOCKADDR_STORAGE)
cmake_push_check_state()
unset(CMAKE_EXTRA_INCLUDE_FILES)
if(WIN32)
set(CMAKE_EXTRA_INCLUDE_FILES "winsock2.h")
@ -45,7 +46,7 @@ if(NOT DEFINED HAVE_STRUCT_SOCKADDR_STORAGE)
endif()
check_type_size("struct sockaddr_storage" SIZEOF_STRUCT_SOCKADDR_STORAGE)
set(HAVE_STRUCT_SOCKADDR_STORAGE ${HAVE_SIZEOF_STRUCT_SOCKADDR_STORAGE})
set(CMAKE_EXTRA_INCLUDE_FILES "")
cmake_pop_check_state()
endif()
if(NOT WIN32)

View File

@ -608,6 +608,7 @@ if(CURL_USE_OPENSSL)
endif()
set(_curl_ca_bundle_supported TRUE)
cmake_push_check_state()
set(CMAKE_REQUIRED_INCLUDES ${OPENSSL_INCLUDE_DIR})
if(NOT DEFINED HAVE_BORINGSSL)
check_symbol_exists("OPENSSL_IS_BORINGSSL" "openssl/base.h" HAVE_BORINGSSL)
@ -615,6 +616,7 @@ if(CURL_USE_OPENSSL)
if(NOT DEFINED HAVE_AWSLC)
check_symbol_exists("OPENSSL_IS_AWSLC" "openssl/base.h" HAVE_AWSLC)
endif()
cmake_pop_check_state()
endif()
if(CURL_USE_MBEDTLS)
@ -986,7 +988,7 @@ if(NOT CURL_DISABLE_LDAP)
# Now that we know, we are not using Windows LDAP...
if(NOT USE_WIN32_LDAP)
# Check for LDAP
unset(CMAKE_REQUIRED_LIBRARIES)
cmake_push_check_state()
if(USE_OPENSSL)
set(CMAKE_REQUIRED_LIBRARIES ${OPENSSL_LIBRARIES})
endif()
@ -997,7 +999,6 @@ if(NOT CURL_DISABLE_LDAP)
check_library_exists("${CMAKE_LBER_LIB}" "ber_init" "" HAVE_LIBLBER)
endif()
set(CMAKE_REQUIRED_INCLUDES_BAK ${CMAKE_REQUIRED_INCLUDES})
set(CMAKE_LDAP_INCLUDE_DIR "" CACHE STRING "Path to LDAP include directory")
if(CMAKE_LDAP_INCLUDE_DIR)
list(APPEND CMAKE_REQUIRED_INCLUDES ${CMAKE_LDAP_INCLUDE_DIR})
@ -1008,13 +1009,9 @@ if(NOT CURL_DISABLE_LDAP)
if(NOT HAVE_LDAP_H)
message(STATUS "LDAP_H not found CURL_DISABLE_LDAP set ON")
set(CURL_DISABLE_LDAP ON CACHE BOOL "" FORCE)
set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES_BAK}) # LDAP includes will not be used
unset(CMAKE_REQUIRED_LIBRARIES)
elseif(NOT HAVE_LIBLDAP)
message(STATUS "LDAP library '${CMAKE_LDAP_LIB}' not found CURL_DISABLE_LDAP set ON")
set(CURL_DISABLE_LDAP ON CACHE BOOL "" FORCE)
set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES_BAK}) # LDAP includes will not be used
unset(CMAKE_REQUIRED_LIBRARIES)
else()
if(CMAKE_LDAP_INCLUDE_DIR)
include_directories(SYSTEM ${CMAKE_LDAP_INCLUDE_DIR})
@ -1031,8 +1028,6 @@ if(NOT CURL_DISABLE_LDAP)
check_function_exists("ldap_url_parse" HAVE_LDAP_URL_PARSE)
check_function_exists("ldap_init_fd" HAVE_LDAP_INIT_FD)
unset(CMAKE_REQUIRED_LIBRARIES)
check_include_file("ldap_ssl.h" HAVE_LDAP_SSL_H)
if(HAVE_LDAP_INIT_FD)
@ -1043,6 +1038,7 @@ if(NOT CURL_DISABLE_LDAP)
set(HAVE_LDAP_SSL ON)
endif()
endif()
cmake_pop_check_state()
endif()
endif()
@ -1195,6 +1191,7 @@ if(CURL_USE_GSSAPI)
set(HAVE_GSSAPI ${GSS_FOUND})
if(GSS_FOUND)
cmake_push_check_state()
list(APPEND CMAKE_REQUIRED_INCLUDES ${GSS_INCLUDE_DIRS})
string(REPLACE ";" " " GSS_CFLAGS "${GSS_CFLAGS}")
@ -1225,13 +1222,13 @@ if(CURL_USE_GSSAPI)
set(CMAKE_REQUIRED_FLAGS "${GSS_CFLAGS} ${GSS_LDFLAGS}")
set(CMAKE_REQUIRED_LIBRARIES ${GSS_LIBRARIES})
check_symbol_exists("GSS_C_NT_HOSTBASED_SERVICE" ${_include_list} HAVE_GSS_C_NT_HOSTBASED_SERVICE)
unset(CMAKE_REQUIRED_LIBRARIES)
endif()
if(NOT HAVE_GSS_C_NT_HOSTBASED_SERVICE)
set(HAVE_OLD_GSSMIT ON)
endif()
endif()
endif()
cmake_pop_check_state()
include_directories(SYSTEM ${GSS_INCLUDE_DIRS})
link_directories(${GSS_LIBRARY_DIRS})
@ -1275,13 +1272,11 @@ endif()
option(USE_LIBRTMP "Enable librtmp from rtmpdump" OFF)
if(USE_LIBRTMP)
cmake_push_check_state()
set(_extra_libs "rtmp")
if(WIN32)
list(APPEND _extra_libs "winmm")
endif()
openssl_check_symbol_exists("RTMP_Init" "librtmp/rtmp.h" HAVE_LIBRTMP "${_extra_libs}")
cmake_pop_check_state()
if(HAVE_LIBRTMP)
list(APPEND CURL_LIBS "rtmp")
list(APPEND LIBCURL_PC_REQUIRES_PRIVATE "librtmp")
@ -1589,16 +1584,18 @@ if(HAVE_FSETXATTR)
curl_internal_test(HAVE_FSETXATTR_6)
endif()
cmake_push_check_state()
set(CMAKE_EXTRA_INCLUDE_FILES "sys/socket.h")
check_type_size("sa_family_t" SIZEOF_SA_FAMILY_T)
set(HAVE_SA_FAMILY_T ${HAVE_SIZEOF_SA_FAMILY_T})
set(CMAKE_EXTRA_INCLUDE_FILES "")
cmake_pop_check_state()
if(WIN32)
cmake_push_check_state()
set(CMAKE_EXTRA_INCLUDE_FILES "winsock2.h")
check_type_size("ADDRESS_FAMILY" SIZEOF_ADDRESS_FAMILY)
set(HAVE_ADDRESS_FAMILY ${HAVE_SIZEOF_ADDRESS_FAMILY})
set(CMAKE_EXTRA_INCLUDE_FILES "")
cmake_pop_check_state()
endif()
# Do curl specific tests
@ -1627,6 +1624,7 @@ foreach(_curl_test IN ITEMS
curl_internal_test(${_curl_test})
endforeach()
cmake_push_check_state()
if(HAVE_FILE_OFFSET_BITS)
set(_FILE_OFFSET_BITS 64)
set(CMAKE_REQUIRED_FLAGS "-D_FILE_OFFSET_BITS=64")
@ -1643,12 +1641,14 @@ if(HAVE_FSEEKO)
endif()
# Include this header to get the type
cmake_push_check_state()
set(CMAKE_REQUIRED_INCLUDES "${CURL_SOURCE_DIR}/include")
set(CMAKE_EXTRA_INCLUDE_FILES "curl/system.h")
check_type_size("curl_off_t" SIZEOF_CURL_OFF_T)
set(CMAKE_EXTRA_INCLUDE_FILES "curl/curl.h")
check_type_size("curl_socket_t" SIZEOF_CURL_SOCKET_T)
set(CMAKE_EXTRA_INCLUDE_FILES "")
cmake_pop_check_state() # pop curl system headers
cmake_pop_check_state() # pop -D_FILE_OFFSET_BITS=64
if(NOT WIN32 AND NOT CMAKE_CROSSCOMPILING)
# On non-Windows and not cross-compiling, check for writable argv[]
@ -1662,8 +1662,6 @@ if(NOT WIN32 AND NOT CMAKE_CROSSCOMPILING)
}" HAVE_WRITABLE_ARGV)
endif()
unset(CMAKE_REQUIRED_FLAGS)
curl_internal_test(HAVE_GLIBC_STRERROR_R)
curl_internal_test(HAVE_POSIX_STRERROR_R)

View File

@ -249,7 +249,9 @@ if(BUILD_SHARED_LIBS)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/libcurl.vers.in"
"${CMAKE_CURRENT_BINARY_DIR}/libcurl.vers" @ONLY)
include(CMakePushCheckState)
include(CheckCSourceCompiles)
cmake_push_check_state()
set(CMAKE_REQUIRED_LINK_OPTIONS "-Wl,--version-script=${CMAKE_CURRENT_BINARY_DIR}/libcurl.vers")
check_c_source_compiles("int main(void) { return 0; }" HAVE_VERSIONED_SYMBOLS)
if(HAVE_VERSIONED_SYMBOLS)
@ -258,7 +260,7 @@ if(BUILD_SHARED_LIBS)
else()
message(WARNING "Versioned symbols requested, but not supported by the toolchain.")
endif()
unset(CMAKE_REQUIRED_LINK_OPTIONS)
cmake_pop_check_state()
endif()
endif()