From 7f7f26b908f86bc6e8bd25c39377bee949941d22 Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Wed, 3 Jan 2024 14:04:19 -0800 Subject: [PATCH] Use CMake to check for strlcat/strlcpy Rather than try to hard-code whether or not each version of each platform has strlcat and strlcpy, this patch has CMake check to see if it's available after including string.h. If they are, then we should not redefine them. This comes after trying to build Foundation against glibc 2.38, which finally added these two functions, causing CoreFoundation to fail to compile. --- CoreFoundation/Base.subproj/CoreFoundation_Prefix.h | 6 ++++-- CoreFoundation/CMakeLists.txt | 12 +++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CoreFoundation/Base.subproj/CoreFoundation_Prefix.h b/CoreFoundation/Base.subproj/CoreFoundation_Prefix.h index bf6f203b5f..d996a77666 100644 --- a/CoreFoundation/Base.subproj/CoreFoundation_Prefix.h +++ b/CoreFoundation/Base.subproj/CoreFoundation_Prefix.h @@ -189,7 +189,7 @@ static dispatch_queue_t __ ## PREFIX ## Queue(void) { \ #define CF_RETAIN_BALANCED_ELSEWHERE(obj, identified_location) do { } while (0) #endif -#if (TARGET_OS_LINUX && !TARGET_OS_ANDROID && !TARGET_OS_CYGWIN) || TARGET_OS_WIN32 +#ifndef HAVE_STRING_STRLCPY CF_INLINE size_t strlcpy(char * dst, const char * src, size_t maxlen) { const size_t srclen = strlen(src); @@ -201,7 +201,9 @@ strlcpy(char * dst, const char * src, size_t maxlen) { } return srclen; } +#endif // HAVE_STRING_STRLCPY +#ifndef HAVE_STRING_STRLCAT CF_INLINE size_t strlcat(char * dst, const char * src, size_t maxlen) { const size_t srclen = strlen(src); @@ -215,7 +217,7 @@ strlcat(char * dst, const char * src, size_t maxlen) { } return dstlen + srclen; } -#endif +#endif // HAVE_STRING_STRLCAT #if TARGET_OS_WIN32 // Compatibility with boolean.h diff --git a/CoreFoundation/CMakeLists.txt b/CoreFoundation/CMakeLists.txt index beb48d6c86..909b9dd439 100644 --- a/CoreFoundation/CMakeLists.txt +++ b/CoreFoundation/CMakeLists.txt @@ -66,11 +66,21 @@ if(CF_DEPLOYMENT_SWIFT) add_compile_options($<$:$<$:/clang:>-fcf-runtime-abi=swift>) endif() +include(CheckSymbolExists) +check_symbol_exists("strlcat" "string.h" HAVE_STRING_STRLCAT) +if(HAVE_STRING_STRLCAT) + add_compile_definitions($<$:HAVE_STRING_STRLCAT>) +endif() + +check_symbol_exists("strlcpy" "string.h" HAVE_STRING_STRLCPY) +if(HAVE_STRING_STRLCPY) + add_compile_definitions($<$:HAVE_STRING_STRLCPY>) +endif() + if(CMAKE_SYSTEM_NAME STREQUAL Linux OR CMAKE_SYSTEM_NAME STREQUAL Android) add_compile_definitions($<$:_GNU_SOURCE>) if(CMAKE_SYSTEM_NAME STREQUAL Linux) - include(CheckSymbolExists) include(CheckIncludeFile) list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE) check_include_file("sched.h" HAVE_SCHED_H)