Skip to content

Commit a956723

Browse files
committed
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.
1 parent 1b514e4 commit a956723

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

CoreFoundation/Base.subproj/CoreFoundation_Prefix.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ static dispatch_queue_t __ ## PREFIX ## Queue(void) { \
189189
#define CF_RETAIN_BALANCED_ELSEWHERE(obj, identified_location) do { } while (0)
190190
#endif
191191

192-
#if (TARGET_OS_LINUX && !TARGET_OS_ANDROID && !TARGET_OS_CYGWIN) || TARGET_OS_WIN32
192+
#ifndef HAVE_STRING_STRLCPY
193193
CF_INLINE size_t
194194
strlcpy(char * dst, const char * src, size_t maxlen) {
195195
const size_t srclen = strlen(src);
@@ -201,7 +201,9 @@ strlcpy(char * dst, const char * src, size_t maxlen) {
201201
}
202202
return srclen;
203203
}
204+
#endif // HAVE_STRING_STRLCPY
204205

206+
#ifndef HAVE_STRING_STRLCAT
205207
CF_INLINE size_t
206208
strlcat(char * dst, const char * src, size_t maxlen) {
207209
const size_t srclen = strlen(src);
@@ -215,7 +217,7 @@ strlcat(char * dst, const char * src, size_t maxlen) {
215217
}
216218
return dstlen + srclen;
217219
}
218-
#endif
220+
#endif // HAVE_STRING_STRLCAT
219221

220222
#if TARGET_OS_WIN32
221223
// Compatibility with boolean.h

CoreFoundation/CMakeLists.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,21 @@ if(CF_DEPLOYMENT_SWIFT)
6666
add_compile_options($<$<COMPILE_LANGUAGE:C>:$<$<STREQUAL:${CMAKE_C_SIMULATE_ID},MSVC>:/clang:>-fcf-runtime-abi=swift>)
6767
endif()
6868

69+
include(CheckSymbolExists)
70+
check_symbol_exists("strlcat" "string.h" HAVE_STRING_STRLCAT)
71+
if(HAVE_STRING_STRLCAT)
72+
add_compile_definitions($<COMPILE_LANGUAGE:C>:HAVE_STRING_STRLCAT>)
73+
endif()
74+
75+
check_symbol_exists("strlcpy" "string.h" HAVE_STRING_STRLCPY)
76+
if(HAVE_STRING_STRLCPY)
77+
add_compile_definitions($<COMPILE_LANGUAGE:C>:HAVE_STRING_STRLCPY>)
78+
endif()
79+
6980
if(CMAKE_SYSTEM_NAME STREQUAL Linux OR CMAKE_SYSTEM_NAME STREQUAL Android)
7081
add_compile_definitions($<$<COMPILE_LANGUAGE:C>:_GNU_SOURCE>)
7182

7283
if(CMAKE_SYSTEM_NAME STREQUAL Linux)
73-
include(CheckSymbolExists)
7484
include(CheckIncludeFile)
7585
list(APPEND CMAKE_REQUIRED_DEFINITIONS -D_GNU_SOURCE)
7686
check_include_file("sched.h" HAVE_SCHED_H)

0 commit comments

Comments
 (0)