Skip to content

Commit 0e838c4

Browse files
authored
Merge pull request #3428 from compnerd/msvc
Platform: port to msvcrt, add msvcrt module
2 parents 93c8784 + a05fd17 commit 0e838c4

File tree

11 files changed

+301
-19
lines changed

11 files changed

+301
-19
lines changed

cmake/modules/SwiftSource.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ function(handle_swift_sources
5454
if (NOT SWIFTSOURCES_IS_MAIN)
5555
list(APPEND swift_compile_flags "-module-link-name" "${name}")
5656
endif()
57+
if("${SWIFTSOURCES_SDK}" STREQUAL "CYGWIN")
58+
list(APPEND swift_compile_flags -DCYGWIN)
59+
endif()
5760

5861
if(swift_sources)
5962
# Special-case hack to create Swift.o for the core standard library.

stdlib/private/SwiftPrivateLibcExtras/Subprocess.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
// Spawn is not available on Android.
14-
#if !defined(__ANDROID__)
13+
// posix_spawn is not available on Android or Windows (MSVC).
14+
#if !defined(__ANDROID__) && (!defined(_WIN32) || defined(__CYGWIN__))
1515

1616
#include "swift/Runtime/Config.h"
1717

@@ -65,5 +65,5 @@ char ***swift_SwiftPrivateLibcExtras_NSGetEnviron(void) {
6565
return _NSGetEnviron();
6666
}
6767
#endif // defined(__APPLE__)
68-
#endif // defined(__ANDROID__)
68+
#endif // !defined(__ANDROID__) && (!defined(_WIN32) || defined(__CGYWIN__))
6969

stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import Glibc
1818
#endif
1919

2020

21-
// posix_spawn is not available on Android.
21+
#if !os(Windows) || CYGWIN
22+
// posix_spawn is not available on Android or Windows.
2223
#if !os(Android)
2324
// swift_posix_spawn isn't available in the public watchOS SDK, we sneak by the
2425
// unavailable attribute declaration here of the APIs that we need.
@@ -293,3 +294,5 @@ internal func _getEnviron() -> UnsafeMutablePointer<UnsafeMutablePointer<CChar>?
293294
return __environ
294295
#endif
295296
}
297+
#endif
298+

stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import Darwin
1717
import Glibc
1818
#endif
1919

20+
#if !os(Windows) || CYGWIN
2021
public func _stdlib_mkstemps(_ template: inout String, _ suffixlen: CInt) -> CInt {
2122
#if os(Android)
2223
preconditionFailure("mkstemps doesn't work on Android")
@@ -32,6 +33,7 @@ public func _stdlib_mkstemps(_ template: inout String, _ suffixlen: CInt) -> CIn
3233
return fd
3334
#endif
3435
}
36+
#endif
3537

3638
public var _stdlib_FD_SETSIZE: CInt {
3739
return 1024
@@ -81,6 +83,7 @@ public struct _stdlib_fd_set {
8183
}
8284
}
8385

86+
#if !os(Windows) || CYGWIN
8487
public func _stdlib_select(
8588
_ readfds: inout _stdlib_fd_set, _ writefds: inout _stdlib_fd_set,
8689
_ errorfds: inout _stdlib_fd_set, _ timeout: UnsafeMutablePointer<timeval>?
@@ -104,6 +107,7 @@ public func _stdlib_select(
104107
}
105108
}
106109
}
110+
#endif
107111

108112
//
109113
// Functions missing in `Darwin` module.

stdlib/public/Platform/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ add_swift_library(swiftGlibc ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
2222
TARGET_SDKS ANDROID CYGWIN FREEBSD LINUX
2323
DEPENDS glibc_modulemap)
2424

25+
add_swift_library(swiftMSVCRT ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_SDK_OVERLAY
26+
msvcrt.swift
27+
${swift_platform_sources}
28+
TARGET_SDKS WINDOWS)
29+
2530
set(glibc_modulemap_target_list)
2631
foreach(sdk ${SWIFT_SDKS})
2732
if("${sdk}" STREQUAL "LINUX" OR

stdlib/public/Platform/Misc.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,33 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include <fcntl.h>
14+
#if !defined(_WIN32) || defined(__CYGWIN__)
1415
#include <semaphore.h>
16+
#endif
17+
#if defined(_WIN32) && !defined(__CYGWIN__)
18+
#include <io.h>
19+
#endif
1520
#include <sys/types.h>
1621
#include <sys/stat.h>
22+
#if !defined(_WIN32) || defined(__CYGWIN__)
1723
#include <sys/ioctl.h>
24+
#endif
1825

1926
#include "swift/Runtime/Config.h"
2027

28+
#if !defined(_WIN32) || defined(__CYGWIN__)
2129
SWIFT_CC(swift)
2230
extern int _swift_Platform_open(const char *path, int oflag, mode_t mode) {
2331
return open(path, oflag, mode);
2432
}
33+
#else
34+
SWIFT_CC(swift)
35+
extern int _swift_Platform_open(const char *path, int oflag, int mode) {
36+
return _open(path, oflag, mode);
37+
}
38+
#endif
2539

40+
#if !defined(_WIN32) || defined(__CYGWIN__)
2641
SWIFT_CC(swift)
2742
extern int _swift_Platform_openat(int fd, const char *path, int oflag,
2843
mode_t mode) {
@@ -61,7 +76,8 @@ extern int
6176
_swift_Platform_ioctlPtr(int fd, unsigned long int request, void* ptr) {
6277
return ioctl(fd, request, ptr);
6378
}
64-
79+
#endif
80+
6581
#if defined(__APPLE__)
6682
#define _REENTRANT
6783
#include <math.h>

stdlib/public/Platform/Platform.swift

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,29 @@ public var errno : Int32 {
9898
get {
9999
#if os(OSX) || os(iOS) || os(watchOS) || os(tvOS) || os(FreeBSD) || os(PS4)
100100
return __error().pointee
101-
// FIXME: os(Windows) should be replaced, such as triple(Cygwin)
102-
#elseif os(Android) || os(Windows)
101+
#elseif os(Android)
103102
return __errno().pointee
103+
#elseif os(Windows)
104+
#if CYGWIN
105+
return __errno().pointee
106+
#else
107+
return _errno().pointee
108+
#endif
104109
#else
105110
return __errno_location().pointee
106111
#endif
107112
}
108113
set(val) {
109114
#if os(OSX) || os(iOS) || os(watchOS) || os(tvOS) || os(FreeBSD) || os(PS4)
110115
return __error().pointee = val
111-
#elseif os(Android) || os(Windows)
116+
#elseif os(Android)
117+
return __errno().pointee = val
118+
#elseif os(Windows)
119+
#if CYGWIN
112120
return __errno().pointee = val
121+
#else
122+
return _errno().pointee = val
123+
#endif
113124
#else
114125
return __errno_location().pointee = val
115126
#endif
@@ -165,20 +176,31 @@ public func snprintf(ptr: UnsafeMutablePointer<Int8>, _ len: Int, _ format: Unsa
165176
// fcntl.h
166177
//===----------------------------------------------------------------------===//
167178

179+
#if !os(Windows) || CYGWIN
168180
@_silgen_name("_swift_Platform_open")
169181
func _swift_Platform_open(
170182
_ path: UnsafePointer<CChar>,
171183
_ oflag: Int32,
172184
_ mode: mode_t
173185
) -> Int32
186+
#else
187+
@_silgen_name("_swift_Platform_open")
188+
func _swift_Platform_open(
189+
_ path: UnsafePointer<CChar>,
190+
_ oflag: Int32,
191+
_ mode: Int32
192+
) -> Int32
193+
#endif
174194

195+
#if !os(Windows) || CYGWIN
175196
@_silgen_name("_swift_Platform_openat")
176197
func _swift_Platform_openat(
177198
_ fd: Int32,
178199
_ path: UnsafePointer<CChar>,
179200
_ oflag: Int32,
180201
_ mode: mode_t
181202
) -> Int32
203+
#endif
182204

183205
public func open(
184206
_ path: UnsafePointer<CChar>,
@@ -187,6 +209,7 @@ public func open(
187209
return _swift_Platform_open(path, oflag, 0)
188210
}
189211

212+
#if !os(Windows) || CYGWIN
190213
public func open(
191214
_ path: UnsafePointer<CChar>,
192215
_ oflag: Int32,
@@ -211,7 +234,17 @@ public func openat(
211234
) -> Int32 {
212235
return _swift_Platform_openat(fd, path, oflag, mode)
213236
}
237+
#else
238+
public func open(
239+
_ path: UnsafePointer<CChar>,
240+
_ oflag: Int32,
241+
_ mode: Int32
242+
) -> Int32 {
243+
return _swift_Platform_open(path, oflag, mode)
244+
}
245+
#endif
214246

247+
#if !os(Windows) || CYGWIN
215248
@_silgen_name("_swift_Platform_fcntl")
216249
internal func _swift_Platform_fcntl(
217250
_ fd: Int32,
@@ -248,7 +281,9 @@ public func fcntl(
248281
) -> Int32 {
249282
return _swift_Platform_fcntlPtr(fd, cmd, ptr)
250283
}
284+
#endif
251285

286+
#if !os(Windows) || CYGWIN
252287
public var S_IFMT: mode_t { return mode_t(0o170000) }
253288
public var S_IFIFO: mode_t { return mode_t(0o010000) }
254289
public var S_IFCHR: mode_t { return mode_t(0o020000) }
@@ -286,11 +321,24 @@ public var S_IREAD: mode_t { return S_IRUSR }
286321
public var S_IWRITE: mode_t { return S_IWUSR }
287322
public var S_IEXEC: mode_t { return S_IXUSR }
288323
#endif
324+
#else
325+
public var S_IFMT: Int32 { return Int32(0xf000) }
326+
327+
public var S_IFREG: Int32 { return Int32(0x8000) }
328+
public var S_IFDIR: Int32 { return Int32(0x4000) }
329+
public var S_IFCHR: Int32 { return Int32(0x2000) }
330+
public var S_IFIFO: Int32 { return Int32(0x1000) }
331+
332+
public var S_IREAD: Int32 { return Int32(0x0100) }
333+
public var S_IWRITE: Int32 { return Int32(0x0080) }
334+
public var S_IEXEC: Int32 { return Int32(0x0040) }
335+
#endif
289336

290337
//===----------------------------------------------------------------------===//
291338
// ioctl.h
292339
//===----------------------------------------------------------------------===//
293340

341+
#if !os(Windows) || CYGWIN
294342
@_silgen_name("_swift_Platform_ioctl")
295343
internal func _swift_Platform_ioctl(
296344
_ fd: CInt,
@@ -327,8 +375,8 @@ public func ioctl(
327375
) -> CInt {
328376
return _swift_Platform_ioctl(fd, request, 0)
329377
}
330-
331-
378+
#endif
379+
332380
//===----------------------------------------------------------------------===//
333381
// unistd.h
334382
//===----------------------------------------------------------------------===//
@@ -354,14 +402,22 @@ public var SIG_DFL: sig_t? { return nil }
354402
public var SIG_IGN: sig_t { return unsafeBitCast(1, to: sig_t.self) }
355403
public var SIG_ERR: sig_t { return unsafeBitCast(-1, to: sig_t.self) }
356404
public var SIG_HOLD: sig_t { return unsafeBitCast(5, to: sig_t.self) }
357-
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Windows)
358-
#if os(Windows)
359-
// In Cygwin, the below SIG_* have the same value with Linux.
360-
// Verified with libstdc++6 v5.3.0 in Cygwin v2.4.1 64bit.
361-
public typealias sighandler_t = _sig_func_ptr
362-
#else
405+
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android)
406+
public typealias sighandler_t = __sighandler_t
407+
408+
public var SIG_DFL: sighandler_t? { return nil }
409+
public var SIG_IGN: sighandler_t {
410+
return unsafeBitCast(1, to: sighandler_t.self)
411+
}
412+
public var SIG_ERR: sighandler_t {
413+
return unsafeBitCast(-1, to: sighandler_t.self)
414+
}
415+
public var SIG_HOLD: sighandler_t {
416+
return unsafeBitCast(2, to: sighandler_t.self)
417+
}
418+
#elseif os(Windows)
419+
#if CYGWIN
363420
public typealias sighandler_t = __sighandler_t
364-
#endif
365421

366422
public var SIG_DFL: sighandler_t? { return nil }
367423
public var SIG_IGN: sighandler_t {
@@ -374,21 +430,38 @@ public var SIG_HOLD: sighandler_t {
374430
return unsafeBitCast(2, to: sighandler_t.self)
375431
}
376432
#else
433+
public var SIG_DFL: _crt_signal_t? { return nil }
434+
public var SIG_IGN: _crt_signal_t {
435+
return unsafeBitCast(1, to: _crt_signal_t.self)
436+
}
437+
public var SIG_ERR: _crt_signal_t {
438+
return unsafeBitCast(-1, to: _crt_signal_t.self)
439+
}
440+
#endif
441+
#else
377442
internal var _ignore = _UnsupportedPlatformError()
378443
#endif
379444

380445
//===----------------------------------------------------------------------===//
381446
// semaphore.h
382447
//===----------------------------------------------------------------------===//
383448

449+
#if !os(Windows) || CYGWIN
384450
/// The value returned by `sem_open()` in the case of failure.
385451
public var SEM_FAILED: UnsafeMutablePointer<sem_t>? {
386452
#if os(OSX) || os(iOS) || os(watchOS) || os(tvOS)
387453
// The value is ABI. Value verified to be correct for OS X, iOS, watchOS, tvOS.
388454
return UnsafeMutablePointer<sem_t>(bitPattern: -1)
389-
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Windows)
455+
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android)
390456
// The value is ABI. Value verified to be correct on Glibc.
391457
return UnsafeMutablePointer<sem_t>(bitPattern: 0)
458+
#elseif os(Windows)
459+
#if CYGWIN
460+
// The value is ABI. Value verified to be correct on Glibc.
461+
return UnsafeMutablePointer<sem_t>(bitPattern: 0)
462+
#else
463+
_UnsupportedPlatformError()
464+
#endif
392465
#else
393466
_UnsupportedPlatformError()
394467
#endif
@@ -423,6 +496,7 @@ public func sem_open(
423496
) -> UnsafeMutablePointer<sem_t>? {
424497
return _swift_Platform_sem_open4(name, oflag, mode, value)
425498
}
499+
#endif
426500

427501
//===----------------------------------------------------------------------===//
428502
// Misc.

stdlib/public/Platform/msvcrt.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
@_exported import ucrt // Clang module
14+
@_exported import visualc // Clang module
15+

0 commit comments

Comments
 (0)