From 4fa549b3a46f267ae6cf0de784a88d5462d083bb Mon Sep 17 00:00:00 2001 From: YR Chen Date: Sun, 13 Mar 2022 21:00:38 +0800 Subject: [PATCH 1/3] Fix usability of symbols imported from `WinSDK` Symbols that are re-exported from @implementationOnly modules harms usability, so we explicitly import them from WinSDK. --- Sources/Foundation/Data.swift | 7 +++++++ Sources/Foundation/FileManager+Win32.swift | 6 ++++++ Sources/Foundation/NSSwiftRuntime.swift | 7 ++----- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Sources/Foundation/Data.swift b/Sources/Foundation/Data.swift index a808240b33..706b857bf2 100644 --- a/Sources/Foundation/Data.swift +++ b/Sources/Foundation/Data.swift @@ -26,6 +26,13 @@ @usableFromInline let memset = WASILibc.memset @usableFromInline let memcpy = WASILibc.memcpy @usableFromInline let memcmp = WASILibc.memcmp +#elseif canImport(CRT) +@usableFromInline let calloc = ucrt.calloc +@usableFromInline let malloc = ucrt.malloc +@usableFromInline let free = ucrt.free +@usableFromInline let memset = ucrt.memset +@usableFromInline let memcpy = ucrt.memcpy +@usableFromInline let memcmp = ucrt.memcmp #endif #if !canImport(Darwin) diff --git a/Sources/Foundation/FileManager+Win32.swift b/Sources/Foundation/FileManager+Win32.swift index a3a75f6ae4..fc1b5e87e2 100644 --- a/Sources/Foundation/FileManager+Win32.swift +++ b/Sources/Foundation/FileManager+Win32.swift @@ -10,6 +10,8 @@ @_implementationOnly import CoreFoundation #if os(Windows) +import struct WinSDK.FILETIME + internal func joinPath(prefix: String, suffix: String) -> String { var pszPath: PWSTR? @@ -1035,4 +1037,8 @@ extension FileManager.NSPathDirectoryEnumerator { } +// FIXME: This is a workaround for CoreFoundation/WinSDK overload +// of the same constants with different types. We should prevent +// CoreFoundation from re-exporting them. +fileprivate let INVALID_FILE_ATTRIBUTES = DWORD(bitPattern: -1) #endif diff --git a/Sources/Foundation/NSSwiftRuntime.swift b/Sources/Foundation/NSSwiftRuntime.swift index 04958446d7..57bd2ff738 100644 --- a/Sources/Foundation/NSSwiftRuntime.swift +++ b/Sources/Foundation/NSSwiftRuntime.swift @@ -14,22 +14,19 @@ // This mimics the behavior of the swift sdk overlay on Darwin #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) @_exported import Darwin -#elseif os(Linux) || os(Android) || CYGWIN || os(OpenBSD) +#elseif os(Linux) || os(Android) || os(Cygwin) || os(OpenBSD) @_exported import Glibc #elseif os(WASI) @_exported import WASILibc #elseif os(Windows) @_exported import CRT +@_exported import WinSDK.core #endif #if !os(WASI) @_exported import Dispatch #endif -#if os(Windows) -import WinSDK -#endif - #if !_runtime(_ObjC) /// The Objective-C BOOL type. /// From 18617a3dfee5535a9026085db64a19d789d05deb Mon Sep 17 00:00:00 2001 From: YR Chen Date: Sun, 13 Mar 2022 21:03:58 +0800 Subject: [PATCH 2/3] Fix ambiguous use of overloaded symbols Some constants are exported by CoreFoundation, but they has mismatched type with WinSDK overlay. Set them explicitly. --- Sources/Foundation/FileHandle.swift | 4 ++-- Sources/Foundation/Process.swift | 13 ++++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Sources/Foundation/FileHandle.swift b/Sources/Foundation/FileHandle.swift index e282628602..43666be2a0 100644 --- a/Sources/Foundation/FileHandle.swift +++ b/Sources/Foundation/FileHandle.swift @@ -25,10 +25,10 @@ fileprivate let _close = Glibc.close(_:) #endif #if canImport(WinSDK) -// We used to get the copy that was re-exported by CoreFoundation +// We used to get the alias that was re-exported by CoreFoundation // but we want to explicitly depend on its types in this file, // so we need to make sure Swift doesn't think it's @_implementationOnly. -import WinSDK +import struct WinSDK.HANDLE #endif extension NSError { diff --git a/Sources/Foundation/Process.swift b/Sources/Foundation/Process.swift index 8d4834320e..d4bcc6eb46 100644 --- a/Sources/Foundation/Process.swift +++ b/Sources/Foundation/Process.swift @@ -14,10 +14,10 @@ import Darwin #endif #if canImport(WinSDK) -// We used to get the copy that was re-exported by CoreFoundation +// We used to get the alias that was re-exported by CoreFoundation // but we want to explicitly depend on its types in this file, // so we need to make sure Swift doesn't think it's @_implementationOnly. -import WinSDK +import struct WinSDK.HANDLE #endif extension Process { @@ -1162,6 +1162,13 @@ open class Process: NSObject { } extension Process { - public static let didTerminateNotification = NSNotification.Name(rawValue: "NSTaskDidTerminateNotification") } + +#if os(Windows) +// FIXME: This is a workaround for CoreFoundation/WinSDK overload +// of the same constants with different types. We should prevent +// CoreFoundation from re-exporting them. +fileprivate let HANDLE_FLAG_INHERIT = DWORD(0x00000001) +fileprivate let STARTF_USESTDHANDLES = DWORD(0x00000100) +#endif From 41cb266ba9979da4ec8848363ecdacbee54a4622 Mon Sep 17 00:00:00 2001 From: stevapple Date: Wed, 10 Aug 2022 01:44:12 +0800 Subject: [PATCH 3/3] Unexpose `WinSDK.core` --- Sources/Foundation/FileHandle.swift | 1 + Sources/Foundation/FileManager+Win32.swift | 1 + Sources/Foundation/FileManager.swift | 1 + Sources/Foundation/Host.swift | 4 ++++ Sources/Foundation/NSError.swift | 1 + Sources/Foundation/NSPathUtilities.swift | 5 +++++ Sources/Foundation/NSSwiftRuntime.swift | 5 ++++- Sources/Foundation/NSURL.swift | 2 ++ Sources/Foundation/Process.swift | 2 ++ Sources/Foundation/ProcessInfo.swift | 4 ++++ Sources/Foundation/Thread.swift | 2 ++ 11 files changed, 27 insertions(+), 1 deletion(-) diff --git a/Sources/Foundation/FileHandle.swift b/Sources/Foundation/FileHandle.swift index 43666be2a0..097b18ca1b 100644 --- a/Sources/Foundation/FileHandle.swift +++ b/Sources/Foundation/FileHandle.swift @@ -25,6 +25,7 @@ fileprivate let _close = Glibc.close(_:) #endif #if canImport(WinSDK) +import WinSDK.core // We used to get the alias that was re-exported by CoreFoundation // but we want to explicitly depend on its types in this file, // so we need to make sure Swift doesn't think it's @_implementationOnly. diff --git a/Sources/Foundation/FileManager+Win32.swift b/Sources/Foundation/FileManager+Win32.swift index fc1b5e87e2..0629ab7029 100644 --- a/Sources/Foundation/FileManager+Win32.swift +++ b/Sources/Foundation/FileManager+Win32.swift @@ -10,6 +10,7 @@ @_implementationOnly import CoreFoundation #if os(Windows) +import WinSDK.core import struct WinSDK.FILETIME internal func joinPath(prefix: String, suffix: String) -> String { diff --git a/Sources/Foundation/FileManager.swift b/Sources/Foundation/FileManager.swift index ec0f255817..be3549265d 100644 --- a/Sources/Foundation/FileManager.swift +++ b/Sources/Foundation/FileManager.swift @@ -18,6 +18,7 @@ fileprivate let UF_HIDDEN: Int32 = 1 @_implementationOnly import CoreFoundation #if os(Windows) import CRT +import WinSDK.core #endif #if os(Windows) diff --git a/Sources/Foundation/Host.swift b/Sources/Foundation/Host.swift index d21f9b702a..da2e57afb6 100644 --- a/Sources/Foundation/Host.swift +++ b/Sources/Foundation/Host.swift @@ -9,6 +9,10 @@ @_implementationOnly import CoreFoundation +#if os(Windows) +import WinSDK.core +#endif + #if os(Android) // Android Glibc differs a little with respect to the Linux Glibc. diff --git a/Sources/Foundation/NSError.swift b/Sources/Foundation/NSError.swift index d5a8f67a07..73a4761d76 100644 --- a/Sources/Foundation/NSError.swift +++ b/Sources/Foundation/NSError.swift @@ -16,6 +16,7 @@ import Darwin import Glibc #elseif canImport(CRT) import CRT +import WinSDK #endif @_implementationOnly import CoreFoundation diff --git a/Sources/Foundation/NSPathUtilities.swift b/Sources/Foundation/NSPathUtilities.swift index b4c6a2889e..eb7b112415 100644 --- a/Sources/Foundation/NSPathUtilities.swift +++ b/Sources/Foundation/NSPathUtilities.swift @@ -9,6 +9,11 @@ @_implementationOnly import CoreFoundation +#if os(Windows) +import WinSDK.core +#endif + + #if os(Windows) let validPathSeps: [Character] = ["\\", "/"] #else diff --git a/Sources/Foundation/NSSwiftRuntime.swift b/Sources/Foundation/NSSwiftRuntime.swift index 57bd2ff738..4879ae62bc 100644 --- a/Sources/Foundation/NSSwiftRuntime.swift +++ b/Sources/Foundation/NSSwiftRuntime.swift @@ -20,13 +20,16 @@ @_exported import WASILibc #elseif os(Windows) @_exported import CRT -@_exported import WinSDK.core #endif #if !os(WASI) @_exported import Dispatch #endif +#if os(Windows) +import WinSDK +#endif + #if !_runtime(_ObjC) /// The Objective-C BOOL type. /// diff --git a/Sources/Foundation/NSURL.swift b/Sources/Foundation/NSURL.swift index 38c260d17d..c73864dfc9 100644 --- a/Sources/Foundation/NSURL.swift +++ b/Sources/Foundation/NSURL.swift @@ -17,6 +17,8 @@ internal let kCFURLWindowsPathStyle = CFURLPathStyle.cfurlWindowsPathStyle import Darwin #elseif canImport(Glibc) import Glibc +#elseif canImport(CRT) +import CRT #endif // NOTE: this represents PLATFORM_PATH_STYLE diff --git a/Sources/Foundation/Process.swift b/Sources/Foundation/Process.swift index d4bcc6eb46..7ad81ce3fb 100644 --- a/Sources/Foundation/Process.swift +++ b/Sources/Foundation/Process.swift @@ -11,6 +11,8 @@ #if canImport(Darwin) import Darwin +#elseif canImport(WinSDK) +import WinSDK #endif #if canImport(WinSDK) diff --git a/Sources/Foundation/ProcessInfo.swift b/Sources/Foundation/ProcessInfo.swift index 92ad64ef19..d34dfbd244 100644 --- a/Sources/Foundation/ProcessInfo.swift +++ b/Sources/Foundation/ProcessInfo.swift @@ -9,6 +9,10 @@ @_implementationOnly import CoreFoundation +#if os(Windows) +import WinSDK +#endif + public struct OperatingSystemVersion { public var majorVersion: Int public var minorVersion: Int diff --git a/Sources/Foundation/Thread.swift b/Sources/Foundation/Thread.swift index 7fc74fb160..8ad287cfd7 100644 --- a/Sources/Foundation/Thread.swift +++ b/Sources/Foundation/Thread.swift @@ -11,6 +11,8 @@ #if canImport(Glibc) import Glibc +#elseif canImport(WinSDK) +import WinSDK.core #endif // WORKAROUND_SR9811