Skip to content

Commit e267ca9

Browse files
committed
WIP/Foundation: port to Windows
Unformalised changes to port Foundation to Windows
1 parent 60651e2 commit e267ca9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1250
-265
lines changed

CMakeLists.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,23 @@ set(libdispatch_ldflags)
7171
if(FOUNDATION_ENABLE_LIBDISPATCH)
7272
set(deployment_enable_libdispatch -DDEPLOYMENT_ENABLE_LIBDISPATCH)
7373
set(libdispatch_cflags -I;${FOUNDATION_PATH_TO_LIBDISPATCH_SOURCE};-I;${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD}/src/swift;-Xcc;-fblocks)
74-
set(libdispatch_ldflags -L;${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD};-L;${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD}/src;-ldispatch)
74+
set(libdispatch_ldflags -L;${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD};-L;${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD}/src;-ldispatch;-lswiftDispatch)
7575
if(CMAKE_SYSTEM_NAME STREQUAL Linux OR CMAKE_SYSTEM_NAME STREQUAL Android OR CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
7676
list(APPEND libdispatch_ldflags -Xlinker;-rpath;-Xlinker;${FOUNDATION_PATH_TO_LIBDISPATCH_BUILD}/src)
7777
endif()
7878
endif()
7979

8080
if(CMAKE_SYSTEM_NAME STREQUAL Android OR CMAKE_SYSTEM_NAME STREQUAL Linux)
8181
set(deployment_target -DDEPLOYMENT_TARGET_LINUX)
82+
set(rpath_flags -Xlinker;-rpath;-Xlinker;"\\\$\$ORIGIN")
8283
elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
8384
set(deployment_target -DDEPLOYMENT_TARGET_MACOSX)
8485
elseif(CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
8586
set(deployment_target -DDEPLOYMENT_TARGET_FREEBSD)
87+
set(rpath_flags -Xlinker;-rpath;-Xlinker;"\\\$\$ORIGIN")
8688
elseif(CMAKE_SYSTEM_NAME STREQUAL Windows)
8789
set(deployment_target -DDEPLOYMENT_TARGET_WINDOWS)
90+
set(CoreFoundation_INTERFACE_LIBRARIES -lWS2_32 -lUser32 -lmsvcrt -ladvapi32 -lshell32 -lrpcrt4 -lsecur32 -lPathcch -lOle32 -lCrypt32 -lwldap32 -lNormaliz)
8891
endif()
8992

9093
add_swift_library(Foundation
@@ -263,14 +266,16 @@ add_swift_library(Foundation
263266
${LIBXML2_LIBRARIES}
264267
${libdispatch_ldflags}
265268
${uuid_LIBRARIES}
266-
-Xlinker;-rpath;-Xlinker;"\\\$\$ORIGIN"
269+
${rpath_flags}
270+
${CoreFoundation_INTERFACE_LIBRARIES}
267271
SWIFT_FLAGS
268272
-DDEPLOYMENT_RUNTIME_SWIFT
269273
${deployment_enable_libdispatch}
270274
-I;${ICU_INCLUDE_DIR}
271275
${libdispatch_cflags}
272276
${swift_enable_testing}
273277
${swift_optimization_flags}
278+
-Xcc -D_DLL
274279
DEPENDS
275280
CoreFoundation)
276281

CoreFoundation/Base.subproj/CFBase.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@
129129
#if TARGET_OS_WIN32
130130
#if !defined(CF_EXPORT)
131131
#if defined(CF_BUILDING_CF) && defined(__cplusplus)
132-
#define CF_EXPORT extern "C" __declspec(dllexport)
132+
#define CF_EXPORT extern "C" /*__declspec(dllexport)*/
133133
#elif defined(CF_BUILDING_CF) && !defined(__cplusplus)
134-
#define CF_EXPORT extern __declspec(dllexport)
134+
#define CF_EXPORT extern /*__declspec(dllexport)*/
135135
#elif defined(__cplusplus)
136-
#define CF_EXPORT extern "C" __declspec(dllimport)
136+
#define CF_EXPORT extern "C" /*__declspec(dllimport)*/
137137
#else
138-
#define CF_EXPORT extern __declspec(dllimport)
138+
#define CF_EXPORT extern /*__declspec(dllimport)*/
139139
#endif
140140
#endif
141141
#else

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,13 @@ const char *_CFProcessPath(void) {
126126
}
127127
#endif
128128

129-
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
129+
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_WINDOWS
130130
CF_CROSS_PLATFORM_EXPORT Boolean _CFIsMainThread(void) {
131131
return pthread_main_np() == 1;
132132
}
133+
#endif
133134

135+
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
134136
const char *_CFProcessPath(void) {
135137
if (__CFProcessPath) return __CFProcessPath;
136138
#if DEPLOYMENT_TARGET_MACOSX

CoreFoundation/Base.subproj/CFPriv.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,12 @@ CF_EXPORT void _CFRuntimeSetCFMPresent(void *a);
5757
CF_EXPORT const char *_CFProcessPath(void);
5858
CF_EXPORT const char **_CFGetProcessPath(void);
5959
CF_EXPORT const char **_CFGetProgname(void);
60+
61+
#if !TARGET_OS_WIN32
6062
CF_EXPORT void _CFGetUGIDs(uid_t *euid, gid_t *egid);
6163
CF_EXPORT uid_t _CFGetEUID(void);
6264
CF_EXPORT uid_t _CFGetEGID(void);
65+
#endif
6366

6467

6568
#if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_LINUX))
@@ -220,8 +223,10 @@ typedef CF_OPTIONS(CFOptionFlags, CFSearchPathDomainMask) {
220223
kCFAllDomainsMask = 0x0ffff /* all domains: all of the above and more, future items */
221224
};
222225

226+
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
223227
CF_EXPORT
224228
CFArrayRef CFCopySearchPathForDirectoriesInDomains(CFSearchPathDirectory directory, CFSearchPathDomainMask domainMask, Boolean expandTilde);
229+
#endif
225230

226231

227232
/* Obsolete keys */
@@ -609,7 +614,6 @@ CF_EXPORT CFPropertyListRef _CFBundleCreateFilteredLocalizedInfoPlist(CFBundleRe
609614

610615
CF_EXPORT CFStringRef _CFGetWindowsAppleAppDataDirectory(void);
611616
CF_EXPORT CFArrayRef _CFGetWindowsBinaryDirectories(void);
612-
CF_EXPORT CFStringRef _CFGetWindowsAppleSystemLibraryDirectory(void);
613617

614618
// If your Windows application does not use a CFRunLoop on the main thread (perhaps because it is reserved for handling UI events via Windows API), then call this function to make distributed notifications arrive using a different run loop.
615619
CF_EXPORT void _CFNotificationCenterSetRunLoop(CFNotificationCenterRef nc, CFRunLoopRef rl);

CoreFoundation/Base.subproj/CFRuntime.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,11 +1193,6 @@ void __CFInitialize(void) {
11931193
CFNumberGetTypeID(); // NB: This does other work
11941194

11951195
__CFCharacterSetInitialize();
1196-
1197-
#if DEPLOYMENT_TARGET_WINDOWS
1198-
__CFWindowsNamedPipeInitialize();
1199-
#endif
1200-
12011196
__CFDateInitialize();
12021197

12031198
#if DEPLOYMENT_RUNTIME_SWIFT

CoreFoundation/Base.subproj/CFRuntime_Internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ CF_PRIVATE void __CFCharacterSetInitialize(void);
151151

152152
#if TARGET_OS_WIN32
153153
CF_PRIVATE void __CFTSDWindowsInitialize(void);
154-
CF_PRIVATE void __CFWindowsNamedPipeInitialize(void);
155154
#endif
156155

157156
#if TARGET_OS_MAC || TARGET_OS_IPHONE || TARGET_OS_WIN32

CoreFoundation/Base.subproj/CFSystemDirectories.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ CFSearchPathEnumerationState __CFGetNextSearchPathEnumeration(CFSearchPathEnumer
5252
#endif
5353

5454

55-
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS
55+
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
5656

5757
CFArrayRef CFCopySearchPathForDirectoriesInDomains(CFSearchPathDirectory directory, CFSearchPathDomainMask domainMask, Boolean expandTilde) {
5858
CFMutableArrayRef array;

CoreFoundation/Base.subproj/ForFoundationOnly.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ CF_EXPORT void CFCharacterSetFast(CFMutableCharacterSetRef theSet);
464464
CF_EXPORT const void *_CFArrayCheckAndGetValueAtIndex(CFArrayRef array, CFIndex idx, Boolean *outOfBounds);
465465
CF_EXPORT void _CFArrayReplaceValues(CFMutableArrayRef array, CFRange range, const void *_Nullable * _Nullable newValues, CFIndex newCount);
466466

467-
467+
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
468468
/* Enumeration
469469
Call CFStartSearchPathEnumeration() once, then call
470470
CFGetNextSearchPathEnumeration() one or more times with the returned state.
@@ -475,6 +475,7 @@ CF_EXPORT void _CFArrayReplaceValues(CFMutableArrayRef array, CFRange range, con
475475
typedef CFIndex CFSearchPathEnumerationState;
476476
CF_EXPORT CFSearchPathEnumerationState __CFStartSearchPathEnumeration(CFSearchPathDirectory dir, CFSearchPathDomainMask domainMask);
477477
CF_EXPORT CFSearchPathEnumerationState __CFGetNextSearchPathEnumeration(CFSearchPathEnumerationState state, UInt8 *path, CFIndex pathSize);
478+
#endif
478479

479480
/* For use by NSNumber and CFNumber.
480481
Hashing algorithm for CFNumber:

CoreFoundation/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,12 @@ if(CF_DEPLOYMENT_SWIFT)
343343
target_compile_definitions(CoreFoundation
344344
PRIVATE
345345
-DDEPLOYMENT_RUNTIME_SWIFT)
346+
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
347+
target_compile_definitions(CoreFoundation
348+
PRIVATE
349+
-D_DLL
350+
-DCURL_STATICLIB)
351+
endif()
346352
else()
347353
target_compile_definitions(CoreFoundation
348354
PRIVATE

CoreFoundation/Error.subproj/CFError.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ static CFTypeRef _CFErrorPOSIXCallBack(CFErrorRef err, CFStringRef key) CF_RETUR
495495
if (!errStr) return NULL;
496496
if (CFEqual(key, kCFErrorDescriptionKey)) return errStr; // If all we wanted was the non-localized description, we're done
497497

498-
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS
498+
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
499499
// We need a kCFErrorLocalizedFailureReasonKey, so look up a possible localization for the error message
500500
// Look for the bundle in /System/Library/CoreServices/CoreTypes.bundle
501501
CFArrayRef paths = CFCopySearchPathForDirectoriesInDomains(kCFLibraryDirectory, kCFSystemDomainMask, false);

CoreFoundation/Locale.subproj/CFLocaleKeys.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ CONST_STRING_DECL(kCFCalendarIdentifierEthiopicAmeteMihret, "ethiopic");
133133
CONST_STRING_DECL(kCFCalendarIdentifierEthiopicAmeteAlem, "ethiopic-amete-alem");
134134

135135
// Aliases for Linux
136-
#if DEPLOYMENT_TARGET_LINUX
136+
#if DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_WINDOWS
137137

138138
CF_EXPORT CFStringRef const kCFBuddhistCalendar __attribute__((alias ("kCFCalendarIdentifierBuddhist")));
139139
CF_EXPORT CFStringRef const kCFChineseCalendar __attribute__((alias ("kCFCalendarIdentifierChinese")));

CoreFoundation/NumberDate.subproj/CFTimeZone.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -679,14 +679,10 @@ CFTimeZoneRef CFTimeZoneCreateWithWindowsName(CFAllocatorRef allocator, CFString
679679
return retval;
680680
}
681681

682-
extern CFStringRef _CFGetWindowsAppleSystemLibraryDirectory(void);
683682
static void __InitTZStrings(void) {
684683
static CFLock_t __CFTZDirLock = CFLockInit;
685684
__CFLock(&__CFTZDirLock);
686-
if (!__tzZoneInfo) {
687-
CFStringRef winDir = _CFGetWindowsAppleSystemLibraryDirectory();
688-
__tzZoneInfo = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@\\etc\\zoneinfo"), winDir);
689-
}
685+
// FIXME(compnerd) identify __tzZoneInfo
690686
if (!__tzDir && __tzZoneInfo) {
691687
int length = CFStringGetLength(__tzZoneInfo) + sizeof("\\zone.tab") + 1;
692688
__tzDir = malloc(length); // If we don't use ascii, we'll need to malloc more space

Foundation/AffineTransform.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import Darwin
1212
#elseif os(Linux) || CYGWIN
1313
import Glibc
14+
#elseif os(Windows)
15+
import MSVCRT
1416
#endif
1517

1618
private let ε: CGFloat = CGFloat(2.22045e-16)

Foundation/ByteCountFormatter.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10+
#if os(Windows)
11+
import MSVCRT
12+
#endif
1013

1114
extension ByteCountFormatter {
1215
public struct Units : OptionSet {

Foundation/CGFloat.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
88
//
99

10+
#if os(Windows)
11+
import MSVCRT
12+
#endif
13+
1014
@_fixed_layout
1115
public struct CGFloat {
1216
#if arch(i386) || arch(arm)
@@ -863,11 +867,13 @@ public func scalbn(_ x: CGFloat, _ n: Int) -> CGFloat {
863867
return CGFloat(scalbn(x.native, n))
864868
}
865869

870+
#if !os(Windows)
866871
@_transparent
867872
public func lgamma(_ x: CGFloat) -> (CGFloat, Int) {
868873
let (value, sign) = lgamma(x.native)
869874
return (CGFloat(value), sign)
870875
}
876+
#endif
871877

872878
@_transparent
873879
public func remquo(_ x: CGFloat, _ y: CGFloat) -> (CGFloat, Int) {

Foundation/Data.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@
1616
import Darwin
1717
#elseif os(Linux)
1818
import Glibc
19+
#elseif os(Windows)
20+
import MSVCRT
21+
import WinSDK
1922
#endif
2023

2124
import CoreFoundation
2225

2326
internal func __NSDataInvokeDeallocatorUnmap(_ mem: UnsafeMutableRawPointer, _ length: Int) {
27+
#if os(Windows)
28+
UnmapViewOfFile(mem)
29+
#else
2430
munmap(mem, length)
31+
#endif
2532
}
2633

2734
internal func __NSDataInvokeDeallocatorFree(_ mem: UnsafeMutableRawPointer, _ length: Int) {
@@ -187,7 +194,7 @@ public final class _DataStorage {
187194
var buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: range.count, alignment: MemoryLayout<UInt>.alignment)
188195
defer { buffer.deallocate() }
189196

190-
let sliceRange = NSRange(location: range.lowerBound - _offset, length: range.count)
197+
_ = NSRange(location: range.lowerBound - _offset, length: range.count)
191198
var enumerated = 0
192199
d.enumerateBytes { (ptr, byteRange, stop) in
193200
if byteRange.upperBound - _offset < range.lowerBound {

Foundation/DateFormatter.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,9 @@ open class DateFormatter : Formatter {
1414
private var __cfObject: CFType?
1515
private var _cfObject: CFType {
1616
guard let obj = __cfObject else {
17-
#if os(macOS) || os(iOS)
18-
let dateStyle = CFDateFormatterStyle(rawValue: CFIndex(self.dateStyle.rawValue))!
19-
let timeStyle = CFDateFormatterStyle(rawValue: CFIndex(self.timeStyle.rawValue))!
20-
#else
21-
let dateStyle = CFDateFormatterStyle(self.dateStyle.rawValue)
22-
let timeStyle = CFDateFormatterStyle(self.timeStyle.rawValue)
23-
#endif
24-
17+
let dateStyle = CFDateFormatterStyle(rawValue: CFIndex(self.dateStyle.rawValue))!
18+
let timeStyle = CFDateFormatterStyle(rawValue: CFIndex(self.timeStyle.rawValue))!
19+
2520
let obj = CFDateFormatterCreate(kCFAllocatorSystemDefault, locale._cfObject, dateStyle, timeStyle)!
2621
_setFormatterAttributes(obj)
2722
if let dateFormat = _dateFormat {

0 commit comments

Comments
 (0)