Skip to content

Porting to Cygwin #381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CoreFoundation/Base.subproj/CFInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ CF_EXTERN_C_BEGIN
#include <CoreFoundation/CFRuntime.h>
#include <limits.h>
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_FREEBSD
#if TARGET_OS_CYGWIN
#else
#include <xlocale.h>
#endif
#include <unistd.h>
#include <sys/time.h>
#include <signal.h>
Expand Down
3 changes: 3 additions & 0 deletions CoreFoundation/Base.subproj/CFPlatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ const char *_CFProcessPath(void) {
#endif

#if DEPLOYMENT_TARGET_LINUX
#if TARGET_OS_CYGWIN
#else
#include <unistd.h>
#if __has_include(<syscall.h>)
#include <syscall.h>
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion CoreFoundation/Base.subproj/CFUtilities.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions CoreFoundation/Base.subproj/CoreFoundation_Prefix.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions CoreFoundation/Base.subproj/SwiftRuntime/TargetConditionals.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions CoreFoundation/PlugIn.subproj/CFBundle_Binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -693,6 +699,7 @@ static CFStringRef _CFBundleDlfcnCopyLoadedImagePathForPointer(void *p) {
#endif /* LOG_BUNDLE_LOAD */
return result;
}
#endif

#endif /* !BINARY_SUPPORT_DYLD */
#endif /* BINARY_SUPPORT_DLFCN */
Expand Down
11 changes: 11 additions & 0 deletions CoreFoundation/PlugIn.subproj/CFBundle_InfoPlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
#include <CoreFoundation/CFURLAccess.h>

#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_FREEBSD
#if TARGET_OS_CYGWIN
#else
#include <dirent.h>
#if __has_include(<sys/sysctl.h>)
#include <sys/sysctl.h>
#endif
#include <sys/mman.h>
#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__
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions CoreFoundation/PlugIn.subproj/CFBundle_Internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
12 changes: 12 additions & 0 deletions CoreFoundation/PlugIn.subproj/CFBundle_Resources.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
41 changes: 24 additions & 17 deletions CoreFoundation/RunLoop.subproj/CFRunLoop.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <windows.h>
#include <process.h>
#define DISPATCH_EXPORT extern
DISPATCH_EXPORT HANDLE _dispatch_get_main_queue_handle_4CF(void);
DISPATCH_EXPORT void _dispatch_main_queue_callback_4CF(void*);

Expand All @@ -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 <dlfcn.h>
#include <poll.h>
#include <sys/epoll.h>
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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("<CFRunLoopSource context>{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("<CFRunLoopSource context>{version = %ld, info = %p, callout = %s (%p)}"), rls->_context.version0.version, rls->_context.version0.info, name, addr);
Expand Down Expand Up @@ -3682,7 +3689,7 @@ static CFStringRef __CFRunLoopObserverCopyDescription(CFTypeRef cf) { /* DOES CA
}
#if DEPLOYMENT_TARGET_WINDOWS
result = CFStringCreateWithFormat(kCFAllocatorSystemDefault, NULL, CFSTR("<CFRunLoopObserver %p [%p]>{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 : "???";
Expand Down
Loading