From 4ffcc62e9b6bb3bee3dcf1d1dc03c5684cd26e6a Mon Sep 17 00:00:00 2001 From: Saleem Abdulrasool Date: Wed, 7 Oct 2020 08:34:17 -0700 Subject: [PATCH] Initial port to Windows This adds enough shims and tweaks the types sufficiently to allow building System on Windows. --- Sources/CSystem/include/CSystemWindows.h | 17 + Sources/CSystem/shims.c | 6 +- Sources/System/Errno.swift | 14 + Sources/System/FileDescriptor.swift | 13 + Sources/System/Platform.swift | 2 + Sources/System/WindowsPlatformConstants.swift | 328 ++++++++++++++++++ Sources/SystemInternals/Exports.swift | 14 + Sources/SystemInternals/Syscalls.swift | 4 +- 8 files changed, 396 insertions(+), 2 deletions(-) create mode 100644 Sources/CSystem/include/CSystemWindows.h create mode 100644 Sources/System/WindowsPlatformConstants.swift diff --git a/Sources/CSystem/include/CSystemWindows.h b/Sources/CSystem/include/CSystemWindows.h new file mode 100644 index 0000000..7719be9 --- /dev/null +++ b/Sources/CSystem/include/CSystemWindows.h @@ -0,0 +1,17 @@ +/* + This source file is part of the Swift System open source project + + Copyright (c) 2020 Apple Inc. and the Swift System project authors + Licensed under Apache License v2.0 with Runtime Library Exception + + See https://swift.org/LICENSE.txt for license information +*/ + +#if defined(_WIN32) + +#define NOMINMAX +#define WIN32_LEAN_AND_MEAN +#define VC_EXTRA_LEAN +#include + +#endif diff --git a/Sources/CSystem/shims.c b/Sources/CSystem/shims.c index 1f9eb59..f492a2a 100644 --- a/Sources/CSystem/shims.c +++ b/Sources/CSystem/shims.c @@ -11,4 +11,8 @@ #include -#endif \ No newline at end of file +#endif + +#if defined(_WIN32) +#include +#endif diff --git a/Sources/System/Errno.swift b/Sources/System/Errno.swift index 9a6ef68..aeddbc3 100644 --- a/Sources/System/Errno.swift +++ b/Sources/System/Errno.swift @@ -229,6 +229,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @available(*, unavailable, renamed: "badAddress") public static var EFAULT: Errno { badAddress } +#if !os(Windows) /// Not a block device. /// /// You attempted a block device operation on a nonblock device or file. @@ -240,6 +241,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "notBlockDevice") public static var ENOTBLK: Errno { notBlockDevice } +#endif /// Resource busy. /// @@ -360,6 +362,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @available(*, unavailable, renamed: "tooManyOpenFiles") public static var EMFILE: Errno { tooManyOpenFiles } +#if !os(Windows) /// Inappropriate control function. /// /// You attempted a control function @@ -388,6 +391,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "textFileBusy") public static var ETXTBSY: Errno { textFileBusy } +#endif /// The file is too large. /// @@ -630,6 +634,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @available(*, unavailable, renamed: "socketTypeNotSupported") public static var ESOCKTNOSUPPORT: Errno { socketTypeNotSupported } +#if !os(Windows) /// Not supported. /// /// The attempted operation isn't supported @@ -642,6 +647,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "notSupported") public static var ENOTSUP: Errno { notSupported } +#endif /// Protocol family not supported. /// @@ -1123,6 +1129,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { public static var EDEVERR: Errno { deviceError } #endif +#if !os(Windows) /// Value too large to be stored in data type. /// /// A numerical result of the function @@ -1135,6 +1142,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "overflow") public static var EOVERFLOW: Errno { overflow } +#endif #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) /// Bad executable or shared library. @@ -1199,6 +1207,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @available(*, unavailable, renamed: "canceled") public static var ECANCELED: Errno { canceled } +#if !os(Windows) /// Identifier removed. /// /// An IPC identifier was removed while the current process was waiting on it. @@ -1223,6 +1232,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "noMessage") public static var ENOMSG: Errno { noMessage } +#endif /// Illegal byte sequence. /// @@ -1252,6 +1262,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { public static var ENOATTR: Errno { attributeNotFound } #endif +#if !os(Windows) /// Bad message. /// /// The message to be received is inappropriate @@ -1350,6 +1361,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "timeout") public static var ETIME: Errno { timeout } +#endif /// Operation not supported on socket. /// @@ -1411,6 +1423,7 @@ extension Errno { public static var ENOPOLICY: Errno { noSuchPolicy } #endif +#if !os(Windows) /// State not recoverable. /// /// The corresponding C error is `ENOTRECOVERABLE`. @@ -1430,6 +1443,7 @@ extension Errno { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "previousOwnerDied") public static var EOWNERDEAD: Errno { previousOwnerDied } +#endif #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) /// Interface output queue is full. diff --git a/Sources/System/FileDescriptor.swift b/Sources/System/FileDescriptor.swift index 7bb9feb..031a6ab 100644 --- a/Sources/System/FileDescriptor.swift +++ b/Sources/System/FileDescriptor.swift @@ -83,6 +83,7 @@ extension FileDescriptor { @_alwaysEmitIntoClient private init(_ raw: CInt) { self.init(rawValue: raw) } +#if !os(Windows) /// Indicates that opening the file doesn't /// wait for the file or device to become available. /// @@ -103,6 +104,7 @@ extension FileDescriptor { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "nonBlocking") public static var O_NONBLOCK: OpenOptions { nonBlocking } +#endif /// Indicates that each write operation appends to the file. /// @@ -201,6 +203,7 @@ extension FileDescriptor { public static var O_EXLOCK: OpenOptions { exclusiveLock } #endif +#if !os(Windows) /// Indicates that opening the file doesn't follow symlinks. /// /// If you specify this option @@ -216,6 +219,7 @@ extension FileDescriptor { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "noFollow") public static var O_NOFOLLOW: OpenOptions { noFollow } +#endif #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) /// Indicates that opening the file @@ -252,6 +256,7 @@ extension FileDescriptor { public static var O_EVTONLY: OpenOptions { eventOnly } #endif +#if !os(Windows) /// Indicates that executing a program closes the file. /// /// Normally, file descriptors remain open @@ -271,6 +276,7 @@ extension FileDescriptor { @_alwaysEmitIntoClient @available(*, unavailable, renamed: "closeOnExec") public static var O_CLOEXEC: OpenOptions { closeOnExec } +#endif } /// Options for specifying what a file descriptor's offset is relative to. @@ -413,6 +419,13 @@ extension FileDescriptor.OpenOptions (.eventOnly, ".eventOnly"), (.closeOnExec, ".closeOnExec") ] +#elseif os(Windows) + let descriptions: [(Element, StaticString)] = [ + (.append, ".append"), + (.create, ".create"), + (.truncate, ".truncate"), + (.exclusiveCreate, ".exclusiveCreate"), + ] #else let descriptions: [(Element, StaticString)] = [ (.nonBlocking, ".nonBlocking"), diff --git a/Sources/System/Platform.swift b/Sources/System/Platform.swift index 41fef79..73e436d 100644 --- a/Sources/System/Platform.swift +++ b/Sources/System/Platform.swift @@ -15,6 +15,8 @@ #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) // @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *) public typealias CModeT = UInt16 +#elseif os(Windows) +public typealias CModeT = Int32 #else public typealias CModeT = UInt32 #endif diff --git a/Sources/System/WindowsPlatformConstants.swift b/Sources/System/WindowsPlatformConstants.swift new file mode 100644 index 0000000..7b26eda --- /dev/null +++ b/Sources/System/WindowsPlatformConstants.swift @@ -0,0 +1,328 @@ +/* + This source file is part of the Swift System open source project + + Copyright (c) 2020 Apple Inc. and the Swift System project authors + Licensed under Apache License v2.0 with Runtime Library Exception + + See https://swift.org/LICENSE.txt for license information +*/ + +#if os(Windows) + +@_alwaysEmitIntoClient +internal var _EPERM: CInt { 1 } + +@_alwaysEmitIntoClient +internal var _ENOENT: CInt { 2 } + +@_alwaysEmitIntoClient +internal var _ESRCH: CInt { 3 } + +@_alwaysEmitIntoClient +internal var _EINTR: CInt { 4 } + +@_alwaysEmitIntoClient +internal var _EIO: CInt { 5 } + +@_alwaysEmitIntoClient +internal var _ENXIO: CInt { 6 } + +@_alwaysEmitIntoClient +internal var _E2BIG: CInt { 7 } + +@_alwaysEmitIntoClient +internal var _ENOEXEC: CInt { 8 } + +@_alwaysEmitIntoClient +internal var _EBADF: CInt { 9 } + +@_alwaysEmitIntoClient +internal var _ECHILD: CInt { 10 } + +@_alwaysEmitIntoClient +internal var _EAGAIN: CInt { 11 } + +@_alwaysEmitIntoClient +internal var _ENOMEM: CInt { 12 } + +@_alwaysEmitIntoClient +internal var _EACCES: CInt { 13 } + +@_alwaysEmitIntoClient +internal var _EFAULT: CInt { 14 } + +@_alwaysEmitIntoClient +internal var _EBUSY: CInt { 16 } + +@_alwaysEmitIntoClient +internal var _EEXIST: CInt { 17 } + +@_alwaysEmitIntoClient +internal var _EXDEV: CInt { 18 } + +@_alwaysEmitIntoClient +internal var _ENODEV: CInt { 19 } + +@_alwaysEmitIntoClient +internal var _ENOTDIR: CInt { 20 } + +@_alwaysEmitIntoClient +internal var _EISDIR: CInt { 21 } + +@_alwaysEmitIntoClient +internal var _EINVAL: CInt { 22 } + +@_alwaysEmitIntoClient +internal var _ENFILE: CInt { 23 } + +@_alwaysEmitIntoClient +internal var _EMFILE: CInt { 24 } + +@_alwaysEmitIntoClient +internal var _ENOTIFY: CInt { 25 } + +@_alwaysEmitIntoClient +internal var _EFBIG: CInt { 27 } + +@_alwaysEmitIntoClient +internal var _ENOSPC: CInt { 28 } + +@_alwaysEmitIntoClient +internal var _ESPIPE: CInt { 29 } + +@_alwaysEmitIntoClient +internal var _EROFS: CInt { 30 } + +@_alwaysEmitIntoClient +internal var _EMLINK: CInt { 31 } + +@_alwaysEmitIntoClient +internal var _EPIPE: CInt { 32 } + +@_alwaysEmitIntoClient +internal var _EDOM: CInt { 33 } + +@_alwaysEmitIntoClient +internal var _ERANGE: CInt { 34 } + +@_alwaysEmitIntoClient +internal var _EDEADLK: CInt { 36 } + +@_alwaysEmitIntoClient +internal var _EDEADLOCK: CInt { 36 } + +@_alwaysEmitIntoClient +internal var _ENAMETOOLONG: CInt { 38 } + +@_alwaysEmitIntoClient +internal var _ENOLCK: CInt { 39 } + +@_alwaysEmitIntoClient +internal var _ENOSYS: CInt { 40 } + +@_alwaysEmitIntoClient +internal var _ENOTEMPTY: CInt { 41 } + +@_alwaysEmitIntoClient +internal var _EILSEQ: CInt { 42 } + +@_alwaysEmitIntoClient +internal var _STRUNCATE: CInt { 80 } + + +@_alwaysEmitIntoClient +internal var _O_RDONLY: CInt { 0x0000 } + +@_alwaysEmitIntoClient +internal var _O_WRONLY: CInt { 0x0001 } + +@_alwaysEmitIntoClient +internal var _O_RDWR: CInt { 0x0002 } + +@_alwaysEmitIntoClient +internal var _O_APPEND: CInt { 0x0008 } + +@_alwaysEmitIntoClient +internal var _O_CREAT: CInt { 0x0100 } + +@_alwaysEmitIntoClient +internal var _O_TRUNC: CInt { 0x0200 } + +@_alwaysEmitIntoClient +internal var _O_EXCL: CInt { 0x0400 } + + +@_alwaysEmitIntoClient +internal var _SEEK_SET: CInt { 0 } + +@_alwaysEmitIntoClient +internal var _SEEK_CUR: CInt { 1 } + +@_alwaysEmitIntoClient +internal var _SEEK_END: CInt { 2 } + + +// WinSock2 + +@_alwaysEmitIntoClient +internal var __EINTR: CInt { 10004 } + +@_alwaysEmitIntoClient +internal var __EBADF: CInt { 10009 } + +@_alwaysEmitIntoClient +internal var __EACCES: CInt { 10013 } + +@_alwaysEmitIntoClient +internal var __EFAULT: CInt { 10014 } + +@_alwaysEmitIntoClient +internal var __EINVAL: CInt { 10022 } + +@_alwaysEmitIntoClient +internal var __EMFILE: CInt { 10024 } + + +@_alwaysEmitIntoClient +internal var _EWOULDBLOCK: CInt { 10035 } + +@_alwaysEmitIntoClient +internal var _EINPROGRESS: CInt { 10036 } + +@_alwaysEmitIntoClient +internal var _EALREADY: CInt { 10037 } + +@_alwaysEmitIntoClient +internal var _ENOTSOCK: CInt { 10038 } + +@_alwaysEmitIntoClient +internal var _EDESTADDRREQ: CInt { 10039 } + +@_alwaysEmitIntoClient +internal var _EMSGSIZE: CInt { 10040 } + +@_alwaysEmitIntoClient +internal var _EPROTOTYPE: CInt { 10041 } + +@_alwaysEmitIntoClient +internal var _ENOPROTOOPT: CInt { 10042 } + +@_alwaysEmitIntoClient +internal var _EPROTONOSUPPORT: CInt { 10043 } + +@_alwaysEmitIntoClient +internal var _ESOCKTNOSUPPORT: CInt { 10044 } + +@_alwaysEmitIntoClient +internal var _EOPNOTSUPP: CInt { 10045 } + +@_alwaysEmitIntoClient +internal var _EPFNOSUPPORT: CInt { 10046 } + +@_alwaysEmitIntoClient +internal var _EAFNOSUPPORT: CInt { 10047 } + +@_alwaysEmitIntoClient +internal var _EADDRINUSE: CInt { 10048 } + +@_alwaysEmitIntoClient +internal var _EADDRNOTAVAIL: CInt { 10049 } + +@_alwaysEmitIntoClient +internal var _ENETDOWN: CInt { 10050 } + +@_alwaysEmitIntoClient +internal var _ENETUNREACH: CInt { 10051 } + +@_alwaysEmitIntoClient +internal var _ENETRESET: CInt { 10052 } + +@_alwaysEmitIntoClient +internal var _ECONNABORTED: CInt { 10053 } + +@_alwaysEmitIntoClient +internal var _ECONNRESET: CInt { 10054 } + +@_alwaysEmitIntoClient +internal var _ENOBUFS: CInt { 10055 } + +@_alwaysEmitIntoClient +internal var _EISCONN: CInt { 10056 } + +@_alwaysEmitIntoClient +internal var _ENOTCONN: CInt { 10057 } + +@_alwaysEmitIntoClient +internal var _ESHUTDOWN: CInt { 10058 } + +@_alwaysEmitIntoClient +internal var _ETOOMANYREFS: CInt { 10059 } + +@_alwaysEmitIntoClient +internal var _ETIMEDOUT: CInt { 10060 } + +@_alwaysEmitIntoClient +internal var _ECONNREFUSED: CInt { 10061 } + +@_alwaysEmitIntoClient +internal var _ELOOP: CInt { 10062 } + +@_alwaysEmitIntoClient +internal var __ENAMETOOLONG: CInt { 10063 } + +@_alwaysEmitIntoClient +internal var _EHOSTDOWN: CInt { 10064 } + +@_alwaysEmitIntoClient +internal var _EHOSTUNREACH: CInt { 10065 } + +@_alwaysEmitIntoClient +internal var __ENOTEMPTY: CInt { 10066 } + +@_alwaysEmitIntoClient +internal var _EPROCLIM: CInt { 10067 } + +@_alwaysEmitIntoClient +internal var _EUSERS: CInt { 10068 } + +@_alwaysEmitIntoClient +internal var _EDQUOT: CInt { 10069 } + +@_alwaysEmitIntoClient +internal var _ESTALE: CInt { 10070 } + +@_alwaysEmitIntoClient +internal var _EREMOTE: CInt { 10071 } + +// WSASYSNOTREADY = 10091 +// WSAVERNOTSUPPORTED = 10092 +// WSANOTINITIALIZED = 10093 + +@_alwaysEmitIntoClient +internal var _EDISCON: CInt { 10101 } + +@_alwaysEmitIntoClient +internal var _ENOMORE: CInt { 10102 } + +@_alwaysEmitIntoClient +internal var _ECANCELED: CInt { 10103 } + +@_alwaysEmitIntoClient +internal var _EINVALIDPROCTABLE: CInt { 10104 } + +@_alwaysEmitIntoClient +internal var _EINVALIDPROVIDER: CInt { 10105 } + +@_alwaysEmitIntoClient +internal var _EPROVIDERFAILEDINIT: CInt { 10106 } + +// WSASYSCALLFAILURE = 10107 +// WSASERVICE_NOT_FOUND = 10108 +// WSATYPE_NOT_FOUND = 10109 +// WSA_E_NO_MORE = 10110 +// WSA_E_CANCELLED = 10111 + +@_alwaysEmitIntoClient +internal var _EREFUSED: CInt { 10112 } + +#endif diff --git a/Sources/SystemInternals/Exports.swift b/Sources/SystemInternals/Exports.swift index e03c96f..6162b8b 100644 --- a/Sources/SystemInternals/Exports.swift +++ b/Sources/SystemInternals/Exports.swift @@ -15,6 +15,8 @@ import CSystem import Darwin #elseif os(Linux) || os(FreeBSD) || os(Android) import Glibc +#elseif os(Windows) +import ucrt #else #error("Unsupported Platform") #endif @@ -34,6 +36,18 @@ public var system_errno: CInt { get { Darwin.errno } set { Darwin.errno = newValue } } +#elseif os(Windows) +public var system_errno: CInt { + get { + var value: CInt = 0 + // TODO(compnerd) handle the error? + _ = ucrt._get_errno(&value) + return value + } + set { + _ = ucrt._set_errno(newValue) + } +} #else public var system_errno: CInt { get { Glibc.errno } diff --git a/Sources/SystemInternals/Syscalls.swift b/Sources/SystemInternals/Syscalls.swift index a78760d..694018c 100644 --- a/Sources/SystemInternals/Syscalls.swift +++ b/Sources/SystemInternals/Syscalls.swift @@ -11,6 +11,8 @@ import Darwin #elseif os(Linux) || os(FreeBSD) || os(Android) import Glibc +#elseif os(Windows) +import ucrt #else #error("Unsupported Platform") #endif @@ -78,7 +80,7 @@ public func system_open(_ path: UnsafePointer, _ oflag: Int32) -> CInt { } public func system_open( - _ path: UnsafePointer, _ oflag: Int32, _ mode: mode_t + _ path: UnsafePointer, _ oflag: Int32, _ mode: CModeT ) -> CInt { #if ENABLE_MOCKING if mockingEnabled { return mock(String(cString: path), oflag, mode) }