diff --git a/CoreFoundation/Base.subproj/CFInternal.h b/CoreFoundation/Base.subproj/CFInternal.h index 7154c1a888..eefe4a69cc 100644 --- a/CoreFoundation/Base.subproj/CFInternal.h +++ b/CoreFoundation/Base.subproj/CFInternal.h @@ -98,7 +98,10 @@ CF_EXTERN_C_BEGIN #include #include #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD +#if TARGET_OS_CYGWIN +#else #include +#endif #include #include #include diff --git a/CoreFoundation/Base.subproj/CFPlatform.c b/CoreFoundation/Base.subproj/CFPlatform.c index a5c60bc7b4..c5d2c62638 100644 --- a/CoreFoundation/Base.subproj/CFPlatform.c +++ b/CoreFoundation/Base.subproj/CFPlatform.c @@ -156,6 +156,8 @@ const char *_CFProcessPath(void) { #endif #if DEPLOYMENT_TARGET_LINUX +#if TARGET_OS_CYGWIN +#else #include #if __has_include() #include @@ -166,6 +168,7 @@ const char *_CFProcessPath(void) { Boolean _CFIsMainThread(void) { return syscall(SYS_gettid) == getpid(); } +#endif const char *_CFProcessPath(void) { if (__CFProcessPath) return __CFProcessPath; diff --git a/CoreFoundation/Base.subproj/CFUtilities.c b/CoreFoundation/Base.subproj/CFUtilities.c index 1b66990a56..7bc724958b 100644 --- a/CoreFoundation/Base.subproj/CFUtilities.c +++ b/CoreFoundation/Base.subproj/CFUtilities.c @@ -1302,7 +1302,7 @@ CFDictionaryRef __CFGetEnvironment() { #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED extern char ***_NSGetEnviron(); char **envp = *_NSGetEnviron(); -#elif DEPLOYMENT_TARGET_FREEBSD +#elif DEPLOYMENT_TARGET_FREEBSD || TARGET_OS_CYGWIN extern char **environ; char **envp = environ; #elif DEPLOYMENT_TARGET_LINUX diff --git a/CoreFoundation/Base.subproj/CoreFoundation_Prefix.h b/CoreFoundation/Base.subproj/CoreFoundation_Prefix.h index 08149a7599..8e6e8c2954 100644 --- a/CoreFoundation/Base.subproj/CoreFoundation_Prefix.h +++ b/CoreFoundation/Base.subproj/CoreFoundation_Prefix.h @@ -182,7 +182,7 @@ typedef int boolean_t; typedef unsigned long fd_mask; #endif -#ifndef __ANDROID__ +#if !defined(__ANDROID__) && !TARGET_OS_CYGWIN CF_INLINE size_t strlcpy(char * dst, const char * src, size_t maxlen) { const size_t srclen = strlen(src); @@ -210,7 +210,9 @@ strlcat(char * dst, const char * src, size_t maxlen) { } #endif +#if !TARGET_OS_CYGWIN #define issetugid() 0 +#endif // Implemented in CFPlatform.c bool OSAtomicCompareAndSwapPtr(void *oldp, void *newp, void *volatile *dst); @@ -269,7 +271,14 @@ void OSMemoryBarrier(); #endif -#if DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX +#if TARGET_OS_CYGWIN +#define HAVE_STRUCT_TIMESPEC 1 +#define strncasecmp_l(a, b, c, d) strncasecmp(a, b, c) +#define _NO_BOOL_TYPEDEF +#undef interface +#endif + +#if DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX #if !defined(MIN) #define MIN(A,B) ((A) < (B) ? (A) : (B)) #endif diff --git a/CoreFoundation/Base.subproj/SwiftRuntime/TargetConditionals.h b/CoreFoundation/Base.subproj/SwiftRuntime/TargetConditionals.h index e1007d1fda..b0b5e2c021 100644 --- a/CoreFoundation/Base.subproj/SwiftRuntime/TargetConditionals.h +++ b/CoreFoundation/Base.subproj/SwiftRuntime/TargetConditionals.h @@ -46,6 +46,7 @@ TARGET_OS_WIN32 - Generated code will run under 32-bit Windows TARGET_OS_UNIX - Generated code will run under some Unix (not OSX) + TARGET_OS_CYGWIN - Generated code will run under 64-bit Cygwin TARGET_OS_MAC - Generated code will run under Mac OS X variant TARGET_OS_IPHONE - Generated code for firmware, devices, or simulator TARGET_OS_IOS - Generated code will run under iOS @@ -76,21 +77,31 @@ #define TARGET_OS_LINUX 0 #define TARGET_OS_WINDOWS 0 #define TARGET_OS_BSD 0 +#define TARGET_OS_CYGWIN 0 #elif __linux__ #define TARGET_OS_DARWIN 0 #define TARGET_OS_LINUX 1 #define TARGET_OS_WINDOWS 0 #define TARGET_OS_BSD 0 +#define TARGET_OS_CYGWIN 0 +#elif __CYGWIN__ +#define TARGET_OS_DARWIN 0 +#define TARGET_OS_LINUX 1 +#define TARGET_OS_WINDOWS 0 +#define TARGET_OS_BSD 0 +#define TARGET_OS_CYGWIN 1 #elif _WIN32 || _WIN64 #define TARGET_OS_DARWIN 0 #define TARGET_OS_LINUX 0 #define TARGET_OS_WINDOWS 1 #define TARGET_OS_BSD 0 +#define TARGET_OS_CYGWIN 0 #elif __unix__ #define TARGET_OS_DARWIN 0 #define TARGET_OS_LINUX 0 #define TARGET_OS_WINDOWS 0 #define TARGET_OS_BSD 1 +#define TARGET_OS_CYGWIN 0 #else #error unknown operating system #endif diff --git a/CoreFoundation/PlugIn.subproj/CFBundle_Binary.c b/CoreFoundation/PlugIn.subproj/CFBundle_Binary.c index 2d39438c75..3ce060aa02 100644 --- a/CoreFoundation/PlugIn.subproj/CFBundle_Binary.c +++ b/CoreFoundation/PlugIn.subproj/CFBundle_Binary.c @@ -684,6 +684,12 @@ static void *_CFBundleDlfcnGetSymbolByNameWithSearch(CFBundleRef bundle, CFStrin #if !defined(BINARY_SUPPORT_DYLD) +#if TARGET_OS_CYGWIN +static CFStringRef _CFBundleDlfcnCopyLoadedImagePathForPointer(void *p) { +// Cygwin does not support dladdr() + return NULL; +} +#else static CFStringRef _CFBundleDlfcnCopyLoadedImagePathForPointer(void *p) { CFStringRef result = NULL; Dl_info info; @@ -693,6 +699,7 @@ static CFStringRef _CFBundleDlfcnCopyLoadedImagePathForPointer(void *p) { #endif /* LOG_BUNDLE_LOAD */ return result; } +#endif #endif /* !BINARY_SUPPORT_DYLD */ #endif /* BINARY_SUPPORT_DLFCN */ diff --git a/CoreFoundation/PlugIn.subproj/CFBundle_InfoPlist.c b/CoreFoundation/PlugIn.subproj/CFBundle_InfoPlist.c index f9a053a369..07e184812d 100644 --- a/CoreFoundation/PlugIn.subproj/CFBundle_InfoPlist.c +++ b/CoreFoundation/PlugIn.subproj/CFBundle_InfoPlist.c @@ -24,12 +24,15 @@ #include #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_FREEBSD +#if TARGET_OS_CYGWIN +#else #include #if __has_include() #include #endif #include #endif +#endif // The following strings are initialized 'later' (i.e., not at static initialization time) because static init time is too early for CFSTR to work, on platforms without constant CF strings #if !__CONSTANT_STRINGS__ @@ -144,7 +147,11 @@ CF_EXPORT CFStringRef _CFGetPlatformName(void) { #elif DEPLOYMENT_TARGET_HPUX return _CFBundleHPUXPlatformName; #elif DEPLOYMENT_TARGET_LINUX +#if TARGET_OS_CYGWIN + return _CFBundleCygwinPlatformName; +#else return _CFBundleLinuxPlatformName; +#endif #elif DEPLOYMENT_TARGET_FREEBSD return _CFBundleFreeBSDPlatformName; #else @@ -160,7 +167,11 @@ CF_EXPORT CFStringRef _CFGetAlternatePlatformName(void) { #elif DEPLOYMENT_TARGET_WINDOWS return CFSTR(""); #elif DEPLOYMENT_TARGET_LINUX +#if TARGET_OS_CYGWIN + return CFSTR("Cygwin"); +#else return CFSTR("Linux"); +#endif #elif DEPLOYMENT_TARGET_FREEBSD return CFSTR("FreeBSD"); #else diff --git a/CoreFoundation/PlugIn.subproj/CFBundle_Internal.h b/CoreFoundation/PlugIn.subproj/CFBundle_Internal.h index 16f7e63d5f..67e8668449 100644 --- a/CoreFoundation/PlugIn.subproj/CFBundle_Internal.h +++ b/CoreFoundation/PlugIn.subproj/CFBundle_Internal.h @@ -346,6 +346,7 @@ extern void _CFPlugInRemoveFactory(CFPlugInRef plugIn, _CFPFactoryRef factory); #define _CFBundleSolarisPlatformName CFSTR("solaris") #define _CFBundleLinuxPlatformName CFSTR("linux") #define _CFBundleFreeBSDPlatformName CFSTR("freebsd") +#define _CFBundleCygwinPlatformName CFSTR("cygwin") #define _CFBundleDefaultStringTableName CFSTR("Localizable") #define _CFBundleStringTableType CFSTR("strings") diff --git a/CoreFoundation/PlugIn.subproj/CFBundle_Resources.c b/CoreFoundation/PlugIn.subproj/CFBundle_Resources.c index 620123c248..57ad89127f 100644 --- a/CoreFoundation/PlugIn.subproj/CFBundle_Resources.c +++ b/CoreFoundation/PlugIn.subproj/CFBundle_Resources.c @@ -315,7 +315,11 @@ CF_EXPORT CFStringRef _CFBundleGetCurrentPlatform(void) { #elif DEPLOYMENT_TARGET_HPUX return CFSTR("HPUX"); #elif DEPLOYMENT_TARGET_LINUX +#if TARGET_OS_CYGWIN + return CFSTR("Cygwin"); +#else return CFSTR("Linux"); +#endif #elif DEPLOYMENT_TARGET_FREEBSD return CFSTR("FreeBSD"); #else @@ -333,7 +337,11 @@ CF_PRIVATE CFStringRef _CFBundleGetPlatformExecutablesSubdirectoryName(void) { #elif DEPLOYMENT_TARGET_HPUX return CFSTR("HPUX"); #elif DEPLOYMENT_TARGET_LINUX +#if TARGET_OS_CYGWIN + return CFSTR("Cygwin"); +#else return CFSTR("Linux"); +#endif #elif DEPLOYMENT_TARGET_FREEBSD return CFSTR("FreeBSD"); #else @@ -351,7 +359,11 @@ CF_PRIVATE CFStringRef _CFBundleGetAlternatePlatformExecutablesSubdirectoryName( #elif DEPLOYMENT_TARGET_HPUX return CFSTR("HP-UX"); #elif DEPLOYMENT_TARGET_LINUX +#if TARGET_OS_CYGWIN + return CFSTR("Cygwin"); +#else return CFSTR("Linux"); +#endif #elif DEPLOYMENT_TARGET_FREEBSD return CFSTR("FreeBSD"); #else diff --git a/CoreFoundation/RunLoop.subproj/CFRunLoop.c b/CoreFoundation/RunLoop.subproj/CFRunLoop.c index 4cd09f1ea7..4eec228289 100644 --- a/CoreFoundation/RunLoop.subproj/CFRunLoop.c +++ b/CoreFoundation/RunLoop.subproj/CFRunLoop.c @@ -58,8 +58,10 @@ extern pthread_t pthread_main_thread_np(void); typedef struct voucher_s *voucher_t; extern mach_port_t _dispatch_get_main_queue_port_4CF(void); extern void _dispatch_main_queue_callback_4CF(mach_msg_header_t *msg); -#elif DEPLOYMENT_TARGET_WINDOWS +#elif DEPLOYMENT_TARGET_WINDOWS || TARGET_OS_CYGWIN +#include #include +#define DISPATCH_EXPORT extern DISPATCH_EXPORT HANDLE _dispatch_get_main_queue_handle_4CF(void); DISPATCH_EXPORT void _dispatch_main_queue_callback_4CF(void*); @@ -68,9 +70,11 @@ DISPATCH_EXPORT void _dispatch_main_queue_callback_4CF(void*); #define mach_port_t HANDLE #define _dispatch_get_main_queue_port_4CF _dispatch_get_main_queue_handle_4CF -#define AbsoluteTime LARGE_INTEGER +#define LARGE_INTEGER uint64_t #elif DEPLOYMENT_TARGET_LINUX +#if TARGET_OS_CYGWIN +#else #include #include #include @@ -79,6 +83,7 @@ DISPATCH_EXPORT void _dispatch_main_queue_callback_4CF(void*); #define _dispatch_get_main_queue_port_4CF _dispatch_get_main_queue_handle_4CF #endif +#endif #if DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_IPHONESIMULATOR || DEPLOYMENT_TARGET_LINUX CF_EXPORT pthread_t _CF_pthread_main_thread_np(void); @@ -347,7 +352,7 @@ CF_INLINE void __CFPortSetFree(__CFPortSet portSet) { } } -#elif DEPLOYMENT_TARGET_WINDOWS +#elif DEPLOYMENT_TARGET_WINDOWS || TARGET_OS_CYGWIN typedef HANDLE __CFPort; #define CFPORT_NULL NULL @@ -373,6 +378,8 @@ static __CFPortSet __CFPortSetAllocate(void) { result->used = 0; result->size = 4; result->handles = (HANDLE *)CFAllocatorAllocate(kCFAllocatorSystemDefault, result->size * sizeof(HANDLE), 0); + +#define CF_SPINLOCK_INIT_FOR_STRUCTS(X) InitializeCriticalSection(&X) CF_SPINLOCK_INIT_FOR_STRUCTS(result->lock); return result; } @@ -522,7 +529,7 @@ static uint32_t __CFSendTrivialMachMessage(mach_port_t port, uint32_t msg_id, CF if (result == MACH_SEND_TIMED_OUT) mach_msg_destroy(&header); return result; } -#elif DEPLOYMENT_TARGET_LINUX +#elif DEPLOYMENT_TARGET_LINUX && !TARGET_OS_CYGWIN static int mk_timer_create(void) { return timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC); @@ -552,7 +559,7 @@ CF_INLINE int64_t __CFUInt64ToAbsoluteTime(int64_t x) { return x; } -#elif DEPLOYMENT_TARGET_WINDOWS +#elif DEPLOYMENT_TARGET_WINDOWS || TARGET_OS_CYGWIN static HANDLE mk_timer_create(void) { return CreateWaitableTimer(NULL, FALSE, NULL); @@ -572,13 +579,13 @@ static kern_return_t mk_timer_arm(HANDLE name, LARGE_INTEGER expire_time) { // There is a race we know about here, (timer fire time calculated -> thread suspended -> timer armed == late timer fire), but we don't have a way to avoid it at this time, since the only way to specify an absolute value to the timer is to calculate the relative time first. Fixing that would probably require not using the TSR for timers on Windows. uint64_t now = mach_absolute_time(); if (now > expire_time) { - result.QuadPart = 0; + result = 0; } else { uint64_t timeDiff = expire_time - now; CFTimeInterval amountOfTimeToWait = __CFTSRToTimeInterval(timeDiff); // Result is in 100 ns (10**-7 sec) units to be consistent with a FILETIME. // CFTimeInterval is in seconds. - result.QuadPart = -(amountOfTimeToWait * 10000000); + result = -(amountOfTimeToWait * 10000000); } BOOL res = SetWaitableTimer(name, &result, 0, NULL, NULL, FALSE); @@ -2355,7 +2362,7 @@ static Boolean __CFRunLoopServiceMachPort(mach_port_name_t port, mach_msg_header return false; } -#elif DEPLOYMENT_TARGET_LINUX +#elif DEPLOYMENT_TARGET_LINUX && !TARGET_OS_CYGWIN #define TIMEOUT_INFINITY UINT64_MAX @@ -2444,7 +2451,7 @@ static Boolean __CFRunLoopServiceFileDescriptors(__CFPortSet portSet, __CFPort o return true; } -#elif DEPLOYMENT_TARGET_WINDOWS +#elif DEPLOYMENT_TARGET_WINDOWS || TARGET_OS_CYGWIN #define TIMEOUT_INFINITY INFINITE @@ -2594,7 +2601,7 @@ static int32_t __CFRunLoopRun(CFRunLoopRef rl, CFRunLoopModeRef rlm, CFTimeInter #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI mach_msg_header_t *msg = NULL; mach_port_t livePort = MACH_PORT_NULL; -#elif DEPLOYMENT_TARGET_WINDOWS +#elif DEPLOYMENT_TARGET_WINDOWS || TARGET_OS_CYGWIN HANDLE livePort = NULL; Boolean windowsMessageReceived = false; #elif DEPLOYMENT_TARGET_LINUX @@ -2623,11 +2630,11 @@ static int32_t __CFRunLoopRun(CFRunLoopRef rl, CFRunLoopModeRef rlm, CFTimeInter if (__CFRunLoopServiceMachPort(dispatchPort, &msg, sizeof(msg_buffer), &livePort, 0, &voucherState, NULL)) { goto handle_msg; } -#elif DEPLOYMENT_TARGET_LINUX +#elif DEPLOYMENT_TARGET_LINUX && !TARGET_OS_CYGWIN if (__CFRunLoopServiceFileDescriptors(CFPORTSET_NULL, dispatchPort, 0, &livePort)) { goto handle_msg; } -#elif DEPLOYMENT_TARGET_WINDOWS +#elif DEPLOYMENT_TARGET_WINDOWS || TARGET_OS_CYGWIN if (__CFRunLoopWaitForMultipleObjects(NULL, &dispatchPort, 0, 0, &livePort, NULL)) { goto handle_msg; } @@ -2695,7 +2702,7 @@ static int32_t __CFRunLoopRun(CFRunLoopRef rl, CFRunLoopModeRef rlm, CFTimeInter #elif DEPLOYMENT_TARGET_WINDOWS // Here, use the app-supplied message queue mask. They will set this if they are interested in having this run loop receive windows messages. __CFRunLoopWaitForMultipleObjects(waitSet, NULL, poll ? 0 : TIMEOUT_INFINITY, rlm->_msgQMask, &livePort, &windowsMessageReceived); -#elif DEPLOYMENT_TARGET_LINUX +#elif DEPLOYMENT_TARGET_LINUX && !TARGET_OS_CYGWIN __CFRunLoopServiceFileDescriptors(waitSet, CFPORT_NULL, TIMEOUT_INFINITY, &livePort); #endif @@ -2820,7 +2827,7 @@ static int32_t __CFRunLoopRun(CFRunLoopRef rl, CFRunLoopModeRef rlm, CFTimeInter (void)mach_msg(reply, MACH_SEND_MSG, reply->msgh_size, 0, MACH_PORT_NULL, 0, MACH_PORT_NULL); CFAllocatorDeallocate(kCFAllocatorSystemDefault, reply); } -#elif DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX +#elif DEPLOYMENT_TARGET_WINDOWS || (DEPLOYMENT_TARGET_LINUX && !TARGET_OS_CYGWIN) sourceHandledThisLoop = __CFRunLoopDoSource1(rl, rlm, rls) || sourceHandledThisLoop; #endif } @@ -2936,7 +2943,7 @@ void CFRunLoopWakeUp(CFRunLoopRef rl) { * wakeup pending, since the queue length is 1. */ ret = __CFSendTrivialMachMessage(rl->_wakeUpPort, 0, MACH_SEND_TIMEOUT, 0); if (ret != MACH_MSG_SUCCESS && ret != MACH_SEND_TIMED_OUT) CRASH("*** Unable to send message to wake up port. (%d) ***", ret); -#elif DEPLOYMENT_TARGET_LINUX +#elif DEPLOYMENT_TARGET_LINUX && !TARGET_OS_CYGWIN int ret; do { ret = eventfd_write(rl->_wakeUpPort, 1); @@ -3481,7 +3488,7 @@ static CFStringRef __CFRunLoopSourceCopyDescription(CFTypeRef cf) { /* DOES CALL void *addr = rls->_context.version0.version == 0 ? (void *)rls->_context.version0.perform : (rls->_context.version0.version == 1 ? (void *)rls->_context.version1.perform : NULL); #if DEPLOYMENT_TARGET_WINDOWS contextDesc = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("{version = %ld, info = %p, callout = %p}"), rls->_context.version0.version, rls->_context.version0.info, addr); -#elif DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX +#elif DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || (DEPLOYMENT_TARGET_LINUX && !TARGET_OS_CYGWIN) Dl_info info; const char *name = (dladdr(addr, &info) && info.dli_saddr == addr && info.dli_sname) ? info.dli_sname : "???"; contextDesc = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("{version = %ld, info = %p, callout = %s (%p)}"), rls->_context.version0.version, rls->_context.version0.info, name, addr); @@ -3682,7 +3689,7 @@ static CFStringRef __CFRunLoopObserverCopyDescription(CFTypeRef cf) { /* DOES CA } #if DEPLOYMENT_TARGET_WINDOWS result = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("{valid = %s, activities = 0x%x, repeats = %s, order = %d, callout = %p, context = %@}"), cf, CFGetAllocator(rlo), __CFIsValid(rlo) ? "Yes" : "No", rlo->_activities, __CFRunLoopObserverRepeats(rlo) ? "Yes" : "No", rlo->_order, rlo->_callout, contextDesc); -#elif DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX +#elif DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || (DEPLOYMENT_TARGET_LINUX && !TARGET_OS_CYGWIN) void *addr = rlo->_callout; Dl_info info; const char *name = (dladdr(addr, &info) && info.dli_saddr == addr && info.dli_sname) ? info.dli_sname : "???"; diff --git a/CoreFoundation/RunLoop.subproj/CFSocket.c b/CoreFoundation/RunLoop.subproj/CFSocket.c index 01a7243d1c..d6a51a940a 100644 --- a/CoreFoundation/RunLoop.subproj/CFSocket.c +++ b/CoreFoundation/RunLoop.subproj/CFSocket.c @@ -948,6 +948,9 @@ #include #include #endif +#if TARGET_OS_CYGWIN +#include +#endif #include #include #include @@ -2064,8 +2067,9 @@ manageSelectError() static void *__CFSocketManager(void * arg) { -#if DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD +#if (DEPLOYMENT_TARGET_LINUX && !TARGET_OS_CYGWIN) || DEPLOYMENT_TARGET_FREEBSD pthread_setname_np(pthread_self(), "com.apple.CFSocket.private"); +#elif TARGET_OS_CYGWIN #else pthread_setname_np("com.apple.CFSocket.private"); #endif diff --git a/Foundation/FoundationErrors.swift b/Foundation/FoundationErrors.swift index 7a4d16212e..2e00148a6e 100644 --- a/Foundation/FoundationErrors.swift +++ b/Foundation/FoundationErrors.swift @@ -57,7 +57,7 @@ public var NSCoderValueNotFoundError: Int { return CocoaError #if os(OSX) || os(iOS) import Darwin -#elseif os(Linux) +#elseif os(Linux) || CYGWIN import Glibc #endif diff --git a/Foundation/NSAffineTransform.swift b/Foundation/NSAffineTransform.swift index e6c1022e31..601c715058 100644 --- a/Foundation/NSAffineTransform.swift +++ b/Foundation/NSAffineTransform.swift @@ -9,7 +9,7 @@ #if os(OSX) || os(iOS) import Darwin -#elseif os(Linux) +#elseif os(Linux) || CYGWIN import Glibc #endif diff --git a/Foundation/NSConcreteValue.swift b/Foundation/NSConcreteValue.swift index bcf4cdd100..f7df879400 100644 --- a/Foundation/NSConcreteValue.swift +++ b/Foundation/NSConcreteValue.swift @@ -11,7 +11,7 @@ import CoreFoundation #if os(OSX) || os(iOS) import Darwin -#elseif os(Linux) +#elseif os(Linux) || CYGWIN import Glibc #endif diff --git a/Foundation/NSData.swift b/Foundation/NSData.swift index 813b6ab864..7c368a22c7 100644 --- a/Foundation/NSData.swift +++ b/Foundation/NSData.swift @@ -11,7 +11,7 @@ import CoreFoundation #if os(OSX) || os(iOS) import Darwin -#elseif os(Linux) +#elseif os(Linux) || CYGWIN import Glibc #endif @@ -423,7 +423,7 @@ open class NSData : NSObject, NSCopying, NSMutableCopying, NSSecureCoding { repeat { #if os(OSX) || os(iOS) bytesWritten = Darwin.write(fd, buf.advanced(by: length - bytesRemaining), bytesRemaining) - #elseif os(Linux) || os(Android) + #elseif os(Linux) || os(Android) || CYGWIN bytesWritten = Glibc.write(fd, buf.advanced(by: length - bytesRemaining), bytesRemaining) #endif } while (bytesWritten < 0 && errno == EINTR) diff --git a/Foundation/NSDate.swift b/Foundation/NSDate.swift index b664f09777..7191224cb5 100644 --- a/Foundation/NSDate.swift +++ b/Foundation/NSDate.swift @@ -11,7 +11,7 @@ import CoreFoundation #if os(OSX) || os(iOS) import Darwin -#elseif os(Linux) +#elseif os(Linux) || CYGWIN import Glibc #endif diff --git a/Foundation/NSFileHandle.swift b/Foundation/NSFileHandle.swift index 6339808664..8596e6675d 100644 --- a/Foundation/NSFileHandle.swift +++ b/Foundation/NSFileHandle.swift @@ -11,7 +11,7 @@ import CoreFoundation #if os(OSX) || os(iOS) import Darwin -#elseif os(Linux) +#elseif os(Linux) || CYGWIN import Glibc #endif diff --git a/Foundation/NSFileManager.swift b/Foundation/NSFileManager.swift index 3338dfee1d..08b1f673d7 100644 --- a/Foundation/NSFileManager.swift +++ b/Foundation/NSFileManager.swift @@ -9,7 +9,7 @@ #if os(OSX) || os(iOS) import Darwin -#elseif os(Linux) +#elseif os(Linux) || CYGWIN import Glibc #endif @@ -129,7 +129,7 @@ open class FileManager : NSObject { } #if os(OSX) || os(iOS) let modeT = number.uint16Value - #elseif os(Linux) || os(Android) + #elseif os(Linux) || os(Android) || CYGWIN let modeT = number.uint32Value #endif if chmod(path, mode_t(modeT)) != 0 { @@ -252,7 +252,7 @@ open class FileManager : NSObject { } #if os(OSX) || os(iOS) let tempEntryType = entryType - #elseif os(Linux) || os(Android) + #elseif os(Linux) || os(Android) || CYGWIN let tempEntryType = Int32(entryType) #endif diff --git a/Foundation/NSGeometry.swift b/Foundation/NSGeometry.swift index c089f2a23b..d4aa43b509 100644 --- a/Foundation/NSGeometry.swift +++ b/Foundation/NSGeometry.swift @@ -9,7 +9,7 @@ #if os(OSX) || os(iOS) import Darwin -#elseif os(Linux) +#elseif os(Linux) || CYGWIN import Glibc #endif diff --git a/Foundation/NSHost.swift b/Foundation/NSHost.swift index b2f46c0fad..0876cd60af 100644 --- a/Foundation/NSHost.swift +++ b/Foundation/NSHost.swift @@ -10,7 +10,7 @@ #if os(OSX) || os(iOS) import Darwin -#elseif os(Linux) +#elseif os(Linux) || CYGWIN import Glibc #endif diff --git a/Foundation/NSJSONSerialization.swift b/Foundation/NSJSONSerialization.swift index 31ec8ba385..e35d073f1d 100644 --- a/Foundation/NSJSONSerialization.swift +++ b/Foundation/NSJSONSerialization.swift @@ -11,7 +11,7 @@ import CoreFoundation #if os(OSX) || os(iOS) import Darwin -#elseif os(Linux) +#elseif os(Linux) || CYGWIN import Glibc #endif diff --git a/Foundation/NSLocale.swift b/Foundation/NSLocale.swift index 8c7628a929..e2746da711 100644 --- a/Foundation/NSLocale.swift +++ b/Foundation/NSLocale.swift @@ -18,7 +18,7 @@ open class NSLocale: NSObject, NSCopying, NSSecureCoding { private var _prefs: UnsafeMutableRawPointer? = nil #if os(OSX) || os(iOS) private var _lock = pthread_mutex_t() -#elseif os(Linux) || os(Android) +#elseif os(Linux) || os(Android) || CYGWIN private var _lock = Int32(0) #endif private var _nullLocale = false diff --git a/Foundation/NSLock.swift b/Foundation/NSLock.swift index aa34dec66b..c4a9f6836a 100644 --- a/Foundation/NSLock.swift +++ b/Foundation/NSLock.swift @@ -10,7 +10,7 @@ #if os(OSX) || os(iOS) import Darwin -#elseif os(Linux) +#elseif os(Linux) || CYGWIN import Glibc #endif @@ -22,7 +22,11 @@ public protocol NSLocking { } open class NSLock: NSObject, NSLocking { +#if CYGWIN + internal var mutex = UnsafeMutablePointer.allocate(capacity: 1) +#else internal var mutex = UnsafeMutablePointer.allocate(capacity: 1) +#endif public override init() { pthread_mutex_init(mutex, nil) @@ -139,11 +143,19 @@ open class NSConditionLock : NSObject, NSLocking { } open class NSRecursiveLock: NSObject, NSLocking { +#if CYGWIN + internal var mutex = UnsafeMutablePointer.allocate(capacity: 1) +#else internal var mutex = UnsafeMutablePointer.allocate(capacity: 1) +#endif public override init() { super.init() +#if CYGWIN + var attrib : pthread_mutexattr_t? = nil +#else var attrib = pthread_mutexattr_t() +#endif withUnsafeMutablePointer(to: &attrib) { attrs in pthread_mutexattr_settype(attrs, Int32(PTHREAD_MUTEX_RECURSIVE)) pthread_mutex_init(mutex, attrs) @@ -176,8 +188,13 @@ open class NSRecursiveLock: NSObject, NSLocking { } open class NSCondition: NSObject, NSLocking { +#if CYGWIN + internal var mutex = UnsafeMutablePointer.allocate(capacity: 1) + internal var cond = UnsafeMutablePointer.allocate(capacity: 1) +#else internal var mutex = UnsafeMutablePointer.allocate(capacity: 1) internal var cond = UnsafeMutablePointer.allocate(capacity: 1) +#endif public override init() { pthread_mutex_init(mutex, nil) diff --git a/Foundation/NSPathUtilities.swift b/Foundation/NSPathUtilities.swift index 21f4938157..b7165f00da 100644 --- a/Foundation/NSPathUtilities.swift +++ b/Foundation/NSPathUtilities.swift @@ -11,7 +11,7 @@ import CoreFoundation #if os(OSX) || os(iOS) import Darwin -#elseif os(Linux) +#elseif os(Linux) || CYGWIN import Glibc #endif diff --git a/Foundation/NSProcessInfo.swift b/Foundation/NSProcessInfo.swift index 9e52774848..b2e07e0b56 100644 --- a/Foundation/NSProcessInfo.swift +++ b/Foundation/NSProcessInfo.swift @@ -10,7 +10,7 @@ #if os(OSX) || os(iOS) import Darwin -#elseif os(Linux) +#elseif os(Linux) || CYGWIN import Glibc #endif diff --git a/Foundation/NSSwiftRuntime.swift b/Foundation/NSSwiftRuntime.swift index 73f358140f..8f68f6df79 100644 --- a/Foundation/NSSwiftRuntime.swift +++ b/Foundation/NSSwiftRuntime.swift @@ -14,7 +14,7 @@ import CoreFoundation // This mimics the behavior of the swift sdk overlay on Darwin #if os(OSX) || os(iOS) @_exported import Darwin -#elseif os(Linux) || os(Android) +#elseif os(Linux) || os(Android) || CYGWIN @_exported import Glibc #endif diff --git a/Foundation/NSTask.swift b/Foundation/NSTask.swift index f4078b7f1c..f4da19bd86 100644 --- a/Foundation/NSTask.swift +++ b/Foundation/NSTask.swift @@ -12,7 +12,7 @@ import CoreFoundation #if os(OSX) || os(iOS) import Darwin -#elseif os(Linux) +#elseif os(Linux) || CYGWIN import Glibc #endif @@ -261,10 +261,20 @@ open class Task: NSObject { task.processLaunchedCondition.unlock() var exitCode : Int32 = 0 +#if CYGWIN + let exitCodePtrWrapper = withUnsafeMutablePointer(to: &exitCode) { + exitCodePtr in + __wait_status_ptr_t(__int_ptr: exitCodePtr) + } +#endif var waitResult : Int32 = 0 repeat { +#if CYGWIN + waitResult = waitpid( task.processIdentifier, exitCodePtrWrapper, 0) +#else waitResult = waitpid( task.processIdentifier, &exitCode, 0) +#endif } while ( (waitResult == -1) && (errno == EINTR) ) task.terminationStatus = WEXITSTATUS( exitCode ) @@ -299,7 +309,7 @@ open class Task: NSObject { CFRunLoopAddSource(managerThreadRunLoop?._cfRunLoop, source, kCFRunLoopDefaultMode) // file_actions - #if os(OSX) || os(iOS) + #if os(OSX) || os(iOS) || CYGWIN var fileActions: posix_spawn_file_actions_t? = nil #else var fileActions: posix_spawn_file_actions_t = posix_spawn_file_actions_t() diff --git a/Foundation/NSThread.swift b/Foundation/NSThread.swift index 11f49654b0..3b46fac953 100644 --- a/Foundation/NSThread.swift +++ b/Foundation/NSThread.swift @@ -10,7 +10,7 @@ #if os(OSX) || os(iOS) import Darwin -#elseif os(Linux) +#elseif os(Linux) || CYGWIN import Glibc #endif @@ -131,12 +131,16 @@ open class Thread : NSObject { } internal var _main: (Void) -> Void = {} -#if os(OSX) || os(iOS) +#if os(OSX) || os(iOS) || CYGWIN private var _thread: pthread_t? = nil #elseif os(Linux) || os(Android) private var _thread = pthread_t() #endif +#if CYGWIN + internal var _attr : pthread_attr_t? = nil +#else internal var _attr = pthread_attr_t() +#endif internal var _status = _NSThreadStatus.initialized internal var _cancelled = false /// - Note: this differs from the Darwin implementation in that the keys must be Strings @@ -167,9 +171,19 @@ open class Thread : NSObject { _status = .finished return } +#if CYGWIN + if let attr = self._attr { + _thread = self.withRetainedReference { + return _CFThreadCreate(attr, NSThreadStart, $0) + } + } else { + _thread = nil + } +#else _thread = self.withRetainedReference { return _CFThreadCreate(self._attr, NSThreadStart, $0) } +#endif } open func main() { diff --git a/Foundation/NSURL.swift b/Foundation/NSURL.swift index 9a4dc8e346..91903d27d8 100644 --- a/Foundation/NSURL.swift +++ b/Foundation/NSURL.swift @@ -12,7 +12,7 @@ import CoreFoundation #if os(OSX) || os(iOS) import Darwin -#elseif os(Linux) +#elseif os(Linux) || CYGWIN import Glibc #endif @@ -496,7 +496,7 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying { open var password: String? { let absoluteURL = CFURLCopyAbsoluteURL(_cfObject) -#if os(Linux) || os(Android) +#if os(Linux) || os(Android) || CYGWIN let passwordRange = CFURLGetByteRangeForComponent(absoluteURL, kCFURLComponentPassword, nil) #else let passwordRange = CFURLGetByteRangeForComponent(absoluteURL, .password, nil) diff --git a/Foundation/NSUUID.swift b/Foundation/NSUUID.swift index 0d7e662583..410fc1e297 100644 --- a/Foundation/NSUUID.swift +++ b/Foundation/NSUUID.swift @@ -12,7 +12,7 @@ import CoreFoundation #if os(OSX) || os(iOS) import Darwin.uuid -#elseif os(Linux) +#elseif os(Linux) || CYGWIN import Glibc #endif diff --git a/Foundation/NSXMLParser.swift b/Foundation/NSXMLParser.swift index 6da3dae96b..1c8f3b9208 100644 --- a/Foundation/NSXMLParser.swift +++ b/Foundation/NSXMLParser.swift @@ -15,7 +15,7 @@ #if os(OSX) || os(iOS) import Darwin -#elseif os(Linux) +#elseif os(Linux) || CYGWIN import Glibc #endif import CoreFoundation diff --git a/uuid/uuid.h b/uuid/uuid.h index ce49c2050b..cc997a855b 100644 --- a/uuid/uuid.h +++ b/uuid/uuid.h @@ -41,6 +41,9 @@ #include typedef unsigned char __darwin_uuid_t[16]; typedef char __darwin_uuid_string_t[37]; +#ifdef uuid_t +#undef uuid_t +#endif typedef __darwin_uuid_t uuid_t; #endif