Skip to content

Commit f1c66e0

Browse files
authored
[CMake] Fix Windows Build (#4991)
* Various fixes to allow for building on Windows * Enable shared libraries by default * Resolve Windows compiler flag warnings and fix constant string symbols
1 parent f186ff8 commit f1c66e0

File tree

9 files changed

+36
-15
lines changed

9 files changed

+36
-15
lines changed

CMakeLists.txt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
4646
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
4747
set(CMAKE_Swift_MODULE_DIRECTORY ${CMAKE_BINARY_DIR}/swift)
4848

49+
option(BUILD_SHARED_LIBS "build shared libraries" ON)
50+
4951
# Optionally build tools (on by default) but only when building shared libraries
5052
if(BUILD_SHARED_LIBS)
5153
option(FOUNDATION_BUILD_TOOLS "build tools" ON)
@@ -110,13 +112,24 @@ list(APPEND _Foundation_common_build_flags
110112
"-Wno-unused-function"
111113
"-Wno-microsoft-enum-forward-reference"
112114
"-Wno-int-conversion"
113-
"-fconstant-cfstrings"
114-
"-fexceptions" # TODO: not on OpenBSD
115-
"-fdollars-in-identifiers"
116-
"-fno-common"
117115
"-fcf-runtime-abi=swift"
118116
"-fblocks")
119117

118+
if(NOT "${CMAKE_C_SIMULATE_ID}" STREQUAL "MSVC")
119+
list(APPEND _Foundation_common_build_flags
120+
"-fconstant-cfstrings"
121+
"-fdollars-in-identifiers"
122+
"-fno-common")
123+
124+
if(NOT CMAKE_SYSTEM_NAME STREQUAL OpenBSD)
125+
list(APPEND _Foundation_common_build_flags
126+
"-fexceptions")
127+
endif()
128+
else()
129+
list(APPEND _Foundation_common_build_flags
130+
"/EHsc")
131+
endif()
132+
120133
if(CMAKE_BUILD_TYPE STREQUAL Debug)
121134
list(APPEND _Foundation_common_build_flags
122135
"-DDEBUG")

Sources/CoreFoundation/include/CFRunLoop.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "CFString.h"
1717
#if TARGET_OS_OSX || TARGET_OS_IPHONE
1818
#include <mach/port.h>
19+
#elif TARGET_OS_WIN32
20+
#include <Windows.h>
1921
#endif
2022

2123
CF_IMPLICIT_BRIDGING_ENABLED

Sources/CoreFoundation/include/CFString.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ since it is the default choice with Mac OS X developer tools.
160160
CF_EXPORT void *_CF_CONSTANT_STRING_SWIFT_CLASS[];
161161
#endif
162162

163-
#if DEPLOYMENT_RUNTIME_SWIFT && TARGET_OS_MAC
163+
#if DEPLOYMENT_RUNTIME_SWIFT
164164

165165
struct __CFConstStr {
166166
struct {

Sources/CoreFoundation/include/ForSwiftFoundationOnly.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,11 @@ static inline bool _withStackOrHeapBuffer(size_t amount, void (__attribute__((no
517517
buffer.capacity = amount;
518518
#endif
519519
buffer.onStack = (_CFIsMainThread() != 0 ? buffer.capacity < 2048 : buffer.capacity < 512);
520+
#if TARGET_OS_WIN32
521+
buffer.memory = buffer.onStack ? _alloca(buffer.capacity) : malloc(buffer.capacity);
522+
#else
520523
buffer.memory = buffer.onStack ? alloca(buffer.capacity) : malloc(buffer.capacity);
524+
#endif
521525
if (buffer.memory == NULL) { return false; }
522526
applier(&buffer);
523527
if (!buffer.onStack) {
@@ -540,6 +544,7 @@ CF_CROSS_PLATFORM_EXPORT CFIndex __CFCharDigitValue(UniChar ch);
540544
#pragma mark - File Functions
541545

542546
#if TARGET_OS_WIN32
547+
typedef int mode_t;
543548
CF_CROSS_PLATFORM_EXPORT int _CFOpenFileWithMode(const unsigned short *path, int opts, mode_t mode);
544549
#else
545550
CF_CROSS_PLATFORM_EXPORT int _CFOpenFileWithMode(const char *path, int opts, mode_t mode);

Sources/CoreFoundation/include/module.modulemap

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module CoreFoundation {
22
umbrella header "CoreFoundation.h"
3-
explicit module CFPlugInCOM { header "CFPlugInCOM.h" }
43

54
export *
65
module * {

Sources/CoreFoundation/internalInclude/CFInternal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ CF_PRIVATE Boolean __CFProcessIsRestricted(void);
485485

486486
CF_EXPORT void * __CFConstantStringClassReferencePtr;
487487

488-
#if DEPLOYMENT_RUNTIME_SWIFT && TARGET_OS_MAC
488+
#if DEPLOYMENT_RUNTIME_SWIFT
489489

490490
#if TARGET_OS_LINUX
491491
#define CONST_STRING_SECTION __attribute__((section(".cfstr.data")))

Sources/Foundation/FileManager+Win32.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ extension FileManager {
162162

163163
internal func _recursiveDestinationOfSymbolicLink(atPath path: String) throws -> String {
164164
// Throw error if path is not a symbolic link:
165-
var previousIterationDestination = try _destinationOfSymbolicLink(atPath: path)
165+
var previousIterationDestination = try destinationOfSymbolicLink(atPath: path)
166166

167167
// Same recursion limit as in Darwin:
168168
let symbolicLinkRecursionLimit = 32

Sources/Foundation/FileManager.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,10 @@ extension FileManager {
283283
let hiddenAttrs = isHidden
284284
? attrs | FILE_ATTRIBUTE_HIDDEN
285285
: attrs & ~FILE_ATTRIBUTE_HIDDEN
286-
guard SetFileAttributesW(fsRep, hiddenAttrs) else {
287-
throw _NSErrorWithWindowsError(GetLastError(), reading: false, paths: [path])
286+
try _fileSystemRepresentation(withPath: path) { fsRep in
287+
guard SetFileAttributesW(fsRep, hiddenAttrs) else {
288+
throw _NSErrorWithWindowsError(GetLastError(), reading: false, paths: [path])
289+
}
288290
}
289291
#else
290292
if isHidden {
@@ -308,7 +310,7 @@ extension FileManager {
308310
// Setting these attributes is unsupported on these platforms.
309311
throw NSError(domain: NSCocoaErrorDomain, code: CocoaError.fileWriteUnknown.rawValue)
310312
#else
311-
let stat = try _lstatFile(atPath: path, withFileSystemRepresentation: fsRep)
313+
let stat = try _lstatFile(atPath: path, withFileSystemRepresentation: nil)
312314
var flags = stat.st_flags
313315
flags |= flagsToSet
314316
flags &= ~flagsToUnset

Sources/Foundation/NSUUID.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ open class NSUUID : NSObject, NSCopying, NSSecureCoding, NSCoding {
9090

9191
open override func isEqual(_ value: Any?) -> Bool {
9292
switch value {
93-
case let other as UUID:
93+
case let other as FoundationEssentials.UUID:
9494
return other.uuid.0 == buffer[0] &&
9595
other.uuid.1 == buffer[1] &&
9696
other.uuid.2 == buffer[2] &&
@@ -124,9 +124,9 @@ open class NSUUID : NSObject, NSCopying, NSSecureCoding, NSCoding {
124124
}
125125

126126
extension NSUUID : _StructTypeBridgeable {
127-
public typealias _StructType = UUID
127+
public typealias _StructType = FoundationEssentials.UUID
128128

129-
public func _bridgeToSwift() -> UUID {
130-
return UUID._unconditionallyBridgeFromObjectiveC(self)
129+
public func _bridgeToSwift() -> FoundationEssentials.UUID {
130+
return FoundationEssentials.UUID._unconditionallyBridgeFromObjectiveC(self)
131131
}
132132
}

0 commit comments

Comments
 (0)