diff --git a/Package.swift b/Package.swift index 561194b..8ebc98b 100644 --- a/Package.swift +++ b/Package.swift @@ -15,18 +15,14 @@ import PackageDescription let targets: [PackageDescription.Target] = [ .target( name: "SystemPackage", - dependencies: ["SystemInternals"], - path: "Sources/System"), - .target( - name: "SystemInternals", dependencies: ["CSystem"], + path: "Sources/System", swiftSettings: [ .define("ENABLE_MOCKING", .when(configuration: .debug)) ]), .target( name: "CSystem", dependencies: []), - .testTarget( name: "SystemTests", dependencies: ["SystemPackage"], diff --git a/Sources/System/Errno.swift b/Sources/System/Errno.swift index aeddbc3..d74acef 100644 --- a/Sources/System/Errno.swift +++ b/Sources/System/Errno.swift @@ -1472,7 +1472,6 @@ extension Errno { #endif } -@_implementationOnly import SystemInternals // @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *) extension Errno { // TODO: We want to provide safe access to `errno`, but we need a diff --git a/Sources/System/FileOperations.swift b/Sources/System/FileOperations.swift index 88ec377..e51a979 100644 --- a/Sources/System/FileOperations.swift +++ b/Sources/System/FileOperations.swift @@ -7,8 +7,6 @@ See https://swift.org/LICENSE.txt for license information */ -@_implementationOnly import SystemInternals - // @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *) extension FileDescriptor { /// Opens or creates a file for reading or writing. @@ -122,7 +120,7 @@ extension FileDescriptor { internal func _seek( offset: Int64, from whence: FileDescriptor.SeekOrigin ) -> Result { - let newOffset = system_lseek(self.rawValue, COffT(offset), whence.rawValue) + let newOffset = system_lseek(self.rawValue, _COffT(offset), whence.rawValue) return valueOrErrno(Int64(newOffset)) } @@ -210,7 +208,7 @@ extension FileDescriptor { retryOnInterrupt: Bool ) -> Result { valueOrErrno(retryOnInterrupt: retryOnInterrupt) { - system_pread(self.rawValue, buffer.baseAddress, buffer.count, COffT(offset)) + system_pread(self.rawValue, buffer.baseAddress, buffer.count, _COffT(offset)) } } @@ -292,7 +290,7 @@ extension FileDescriptor { retryOnInterrupt: Bool ) -> Result { valueOrErrno(retryOnInterrupt: retryOnInterrupt) { - system_pwrite(self.rawValue, buffer.baseAddress, buffer.count, COffT(offset)) + system_pwrite(self.rawValue, buffer.baseAddress, buffer.count, _COffT(offset)) } } diff --git a/Sources/System/FilePath/FilePathParsing.swift b/Sources/System/FilePath/FilePathParsing.swift index e65833a..2675398 100644 --- a/Sources/System/FilePath/FilePathParsing.swift +++ b/Sources/System/FilePath/FilePathParsing.swift @@ -322,7 +322,6 @@ extension FilePath { } // Whether we are providing Windows paths -@_implementationOnly import var SystemInternals.forceWindowsPaths @inline(__always) internal var _windowsPaths: Bool { #if os(Windows) diff --git a/Sources/SystemInternals/Exports.swift b/Sources/System/Internals/Exports.swift similarity index 78% rename from Sources/SystemInternals/Exports.swift rename to Sources/System/Internals/Exports.swift index 5cd7091..91c9e01 100644 --- a/Sources/SystemInternals/Exports.swift +++ b/Sources/System/Internals/Exports.swift @@ -15,32 +15,26 @@ import CSystem // TODO: Should CSystem just include all the header files we need? #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) -import Darwin +@_implementationOnly import Darwin #elseif os(Linux) || os(FreeBSD) || os(Android) -import Glibc +@_implementationOnly import Glibc #elseif os(Windows) -import ucrt +@_implementationOnly import ucrt #else #error("Unsupported Platform") #endif -public typealias COffT = off_t - -#if os(Windows) -public typealias CModeT = CInt -#else -public typealias CModeT = mode_t -#endif +internal typealias _COffT = off_t // MARK: syscalls and variables #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) -public var system_errno: CInt { +internal var system_errno: CInt { get { Darwin.errno } set { Darwin.errno = newValue } } #elseif os(Windows) -public var system_errno: CInt { +internal var system_errno: CInt { get { var value: CInt = 0 // TODO(compnerd) handle the error? @@ -52,7 +46,7 @@ public var system_errno: CInt { } } #else -public var system_errno: CInt { +internal var system_errno: CInt { get { Glibc.errno } set { Glibc.errno = newValue } } @@ -62,33 +56,21 @@ public var system_errno: CInt { // Convention: `system_foo` is system's wrapper for `foo`. -public func system_strerror(_ __errnum: Int32) -> UnsafeMutablePointer! { +internal func system_strerror(_ __errnum: Int32) -> UnsafeMutablePointer! { strerror(__errnum) } -public func system_strlen(_ s: UnsafePointer) -> Int { +internal func system_strlen(_ s: UnsafePointer) -> Int { strlen(s) } -#if os(Windows) -public typealias _PlatformChar = UInt16 -#else -public typealias _PlatformChar = CChar -#endif -#if os(Windows) -public typealias _PlatformUnicodeEncoding = UTF16 -#else -public typealias _PlatformUnicodeEncoding = UTF8 -#endif - - // Convention: `system_platform_foo` is a // platform-representation-abstracted wrapper around `foo`-like functionality. // Type and layout differences such as the `char` vs `wchar` are abstracted. // // strlen for the platform string -public func system_platform_strlen(_ s: UnsafePointer<_PlatformChar>) -> Int { +internal func system_platform_strlen(_ s: UnsafePointer) -> Int { #if os(Windows) return wcslen(s) #else @@ -98,8 +80,8 @@ public func system_platform_strlen(_ s: UnsafePointer<_PlatformChar>) -> Int { // Interop between String and platfrom string extension String { - public func _withPlatformString( - _ body: (UnsafePointer<_PlatformChar>) throws -> Result + internal func _withPlatformString( + _ body: (UnsafePointer) throws -> Result ) rethrows -> Result { // Need to #if because CChar may be signed #if os(Windows) @@ -109,7 +91,7 @@ extension String { #endif } - public init?(_platformString platformString: UnsafePointer<_PlatformChar>) { + internal init?(_platformString platformString: UnsafePointer) { // Need to #if because CChar may be signed #if os(Windows) guard let strRes = String.decodeCString( @@ -126,8 +108,8 @@ extension String { #endif } - public init( - _errorCorrectingPlatformString platformString: UnsafePointer<_PlatformChar> + internal init( + _errorCorrectingPlatformString platformString: UnsafePointer ) { // Need to #if because CChar may be signed #if os(Windows) diff --git a/Sources/SystemInternals/Mocking.swift b/Sources/System/Internals/Mocking.swift similarity index 96% rename from Sources/SystemInternals/Mocking.swift rename to Sources/System/Internals/Mocking.swift index 41549d4..3af5493 100644 --- a/Sources/SystemInternals/Mocking.swift +++ b/Sources/System/Internals/Mocking.swift @@ -53,7 +53,7 @@ internal enum ForceErrno: Equatable { } // Provide access to the driver, context, and trace stack of mocking -public class MockingDriver { +internal class MockingDriver { // Record syscalls and their arguments internal var trace = Trace() @@ -109,7 +109,7 @@ private var contextualMockingEnabled: Bool { extension MockingDriver { internal static var enabled: Bool { mockingEnabled } - public static var forceWindowsPaths: Bool { + internal static var forceWindowsPaths: Bool { currentMockingDriver?.forceWindowsSyntaxForPaths ?? false } } @@ -126,8 +126,8 @@ internal var mockingEnabled: Bool { #endif } -@inline(__always) @inlinable -public var forceWindowsPaths: Bool { +@inline(__always) +internal var forceWindowsPaths: Bool { #if !ENABLE_MOCKING return false #else @@ -183,8 +183,8 @@ internal func _mockInt( internal func _mockOffT( name: String = #function, _ args: AnyHashable... -) -> COffT { - COffT(mockImpl(name: name, args)) +) -> _COffT { + _COffT(mockImpl(name: name, args)) } #endif // ENABLE_MOCKING diff --git a/Sources/SystemInternals/Syscalls.swift b/Sources/System/Internals/Syscalls.swift similarity index 82% rename from Sources/SystemInternals/Syscalls.swift rename to Sources/System/Internals/Syscalls.swift index 2294082..e0f2ceb 100644 --- a/Sources/SystemInternals/Syscalls.swift +++ b/Sources/System/Internals/Syscalls.swift @@ -8,11 +8,11 @@ */ #if os(macOS) || os(iOS) || os(watchOS) || os(tvOS) -import Darwin +@_implementationOnly import Darwin #elseif os(Linux) || os(FreeBSD) || os(Android) -import Glibc +@_implementationOnly import Glibc #elseif os(Windows) -import ucrt +@_implementationOnly import ucrt #else #error("Unsupported Platform") #endif @@ -21,8 +21,8 @@ import ucrt // amount of code size, so we hand outline that code for every syscall // open -public func system_open( - _ path: UnsafePointer<_PlatformChar>, _ oflag: Int32 +internal func system_open( + _ path: UnsafePointer, _ oflag: Int32 ) -> CInt { #if ENABLE_MOCKING if mockingEnabled { @@ -32,8 +32,9 @@ public func system_open( return open(path, oflag) } -public func system_open( - _ path: UnsafePointer<_PlatformChar>, _ oflag: Int32, _ mode: CModeT +internal func system_open( + _ path: UnsafePointer, + _ oflag: Int32, _ mode: CInterop.Mode ) -> CInt { #if ENABLE_MOCKING if mockingEnabled { @@ -44,7 +45,7 @@ public func system_open( } // close -public func system_close(_ fd: Int32) -> Int32 { +internal func system_close(_ fd: Int32) -> Int32 { #if ENABLE_MOCKING if mockingEnabled { return _mock(fd) } #endif @@ -52,7 +53,7 @@ public func system_close(_ fd: Int32) -> Int32 { } // read -public func system_read( +internal func system_read( _ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int ) -> Int { #if ENABLE_MOCKING @@ -62,7 +63,7 @@ public func system_read( } // pread -public func system_pread( +internal func system_pread( _ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int, _ offset: off_t ) -> Int { #if ENABLE_MOCKING @@ -72,7 +73,7 @@ public func system_pread( } // lseek -public func system_lseek( +internal func system_lseek( _ fd: Int32, _ off: off_t, _ whence: Int32 ) -> off_t { #if ENABLE_MOCKING @@ -82,7 +83,7 @@ public func system_lseek( } // write -public func system_write( +internal func system_write( _ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int ) -> Int { #if ENABLE_MOCKING @@ -92,7 +93,7 @@ public func system_write( } // pwrite -public func system_pwrite( +internal func system_pwrite( _ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int, _ offset: off_t ) -> Int { #if ENABLE_MOCKING diff --git a/Sources/SystemInternals/WindowsSyscallAdapters.swift b/Sources/System/Internals/WindowsSyscallAdapters.swift similarity index 92% rename from Sources/SystemInternals/WindowsSyscallAdapters.swift rename to Sources/System/Internals/WindowsSyscallAdapters.swift index f1d74f0..48c2845 100644 --- a/Sources/SystemInternals/WindowsSyscallAdapters.swift +++ b/Sources/System/Internals/WindowsSyscallAdapters.swift @@ -9,12 +9,12 @@ #if os(Windows) -import ucrt -import WinSDK +@_implementationOnly import ucrt +@_implementationOnly import WinSDK @inline(__always) internal func open( - _ path: UnsafePointer<_PlatformChar>, _ oflag: Int32 + _ path: UnsafePointer, _ oflag: Int32 ) -> CInt { var fh: CInt = -1 _ = _wsopen_s(&fh, path, oflag, _SH_DENYNO, _S_IREAD | _S_IWRITE) @@ -23,7 +23,7 @@ internal func open( @inline(__always) internal func open( - _ path: UnsafePointer<_PlatformChar>, _ oflag: Int32, _ mode: CModeT + _ path: UnsafePointer, _ oflag: Int32, _ mode: CModeT ) -> CInt { // TODO(compnerd): Apply read/write permissions var fh: CInt = -1 diff --git a/Sources/System/Platform/Platform.swift b/Sources/System/Platform/Platform.swift index 069f90b..0cad6b6 100644 --- a/Sources/System/Platform/Platform.swift +++ b/Sources/System/Platform/Platform.swift @@ -7,9 +7,7 @@ See https://swift.org/LICENSE.txt for license information */ -@_implementationOnly import SystemInternals - -// Public typealiases that can't be reexported from SystemInternals +// MARK: - Public typealiases /// The C `mode_t` type. // @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *) @@ -32,4 +30,30 @@ public enum CInterop { /// The C `char` type public typealias Char = CChar + + #if os(Windows) + /// The platform's preferred character type. On Unix, this is an 8-bit C + /// `char` (which may be signed or unsigned, depending on platform). On + /// Windows, this is `UInt16` (a "wide" character). + public typealias PlatformChar = UInt16 + #else + /// The platform's preferred character type. On Unix, this is an 8-bit C + /// `char` (which may be signed or unsigned, depending on platform). On + /// Windows, this is `UInt16` (a "wide" character). + public typealias PlatformChar = CInterop.Char + #endif + + #if os(Windows) + /// The platform's preferred Unicode encoding. On Unix this is UTF-8 and on + /// Windows it is UTF-16. Native strings may contain invalid Unicode, + /// which will be handled by either error-correction or failing, depending + /// on API. + public typealias PlatformUnicodeEncoding = UTF16 + #else + /// The platform's preferred Unicode encoding. On Unix this is UTF-8 and on + /// Windows it is UTF-16. Native strings may contain invalid Unicode, + /// which will be handled by either error-correction or failing, depending + /// on API. + public typealias PlatformUnicodeEncoding = UTF8 + #endif } diff --git a/Sources/System/Platform/PlatformString.swift b/Sources/System/Platform/PlatformString.swift index d68575f..6464869 100644 --- a/Sources/System/Platform/PlatformString.swift +++ b/Sources/System/Platform/PlatformString.swift @@ -7,54 +7,6 @@ See https://swift.org/LICENSE.txt for license information */ -// @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) -extension CInterop { - #if os(Windows) - /// The platform's preferred character type. On Unix, this is an 8-bit C - /// `char` (which may be signed or unsigned, depending on platform). On - /// Windows, this is `UInt16` (a "wide" character). - public typealias PlatformChar = UInt16 - #else - /// The platform's preferred character type. On Unix, this is an 8-bit C - /// `char` (which may be signed or unsigned, depending on platform). On - /// Windows, this is `UInt16` (a "wide" character). - public typealias PlatformChar = CInterop.Char - #endif - - #if os(Windows) - /// The platform's preferred Unicode encoding. On Unix this is UTF-8 and on - /// Windows it is UTF-16. Native strings may contain invalid Unicode, - /// which will be handled by either error-correction or failing, depending - /// on API. - public typealias PlatformUnicodeEncoding = UTF16 - #else - /// The platform's preferred Unicode encoding. On Unix this is UTF-8 and on - /// Windows it is UTF-16. Native strings may contain invalid Unicode, - /// which will be handled by either error-correction or failing, depending - /// on API. - public typealias PlatformUnicodeEncoding = UTF8 - #endif -} - -extension CInterop.PlatformChar { - internal var _platformCodeUnit: CInterop.PlatformUnicodeEncoding.CodeUnit { - #if os(Windows) - return self - #else - return CInterop.PlatformUnicodeEncoding.CodeUnit(bitPattern: self) - #endif - } -} -extension CInterop.PlatformUnicodeEncoding.CodeUnit { - internal var _platformChar: CInterop.PlatformChar { - #if os(Windows) - return self - #else - return CInterop.PlatformChar(bitPattern: self) - #endif - } -} - // @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) extension String { /// Creates a string by interpreting the null-terminated platform string as @@ -106,6 +58,25 @@ extension String { } +extension CInterop.PlatformChar { + internal var _platformCodeUnit: CInterop.PlatformUnicodeEncoding.CodeUnit { + #if os(Windows) + return self + #else + return CInterop.PlatformUnicodeEncoding.CodeUnit(bitPattern: self) + #endif + } +} +extension CInterop.PlatformUnicodeEncoding.CodeUnit { + internal var _platformChar: CInterop.PlatformChar { + #if os(Windows) + return self + #else + return CInterop.PlatformChar(bitPattern: self) + #endif + } +} + internal protocol _PlatformStringable { func _withPlatformString( _ body: (UnsafePointer) throws -> Result diff --git a/Sources/System/SystemString.swift b/Sources/System/SystemString.swift index 4ab9309..507c896 100644 --- a/Sources/System/SystemString.swift +++ b/Sources/System/SystemString.swift @@ -239,7 +239,6 @@ extension SystemString: CustomStringConvertible, CustomDebugStringConvertible { internal var debugDescription: String { description.debugDescription } } -@_implementationOnly import func SystemInternals.system_platform_strlen extension SystemString { /// Creates a system string by copying bytes from a null-terminated platform string. /// diff --git a/Tests/SystemTests/MockingTest.swift b/Tests/SystemTests/MockingTest.swift index b377b09..3854598 100644 --- a/Tests/SystemTests/MockingTest.swift +++ b/Tests/SystemTests/MockingTest.swift @@ -10,13 +10,11 @@ import XCTest #if SYSTEM_PACKAGE -import SystemPackage +@testable import SystemPackage #else -import System +@testable import System #endif -@testable import SystemInternals - // @available(macOS 9999, iOS 9999, watchOS 9999, tvOS 9999, *) final class MockingTest: XCTestCase { func testMocking() { diff --git a/Tests/SystemTests/SystemStringTests.swift b/Tests/SystemTests/SystemStringTests.swift index 2c409ff..bcfe632 100644 --- a/Tests/SystemTests/SystemStringTests.swift +++ b/Tests/SystemTests/SystemStringTests.swift @@ -42,8 +42,6 @@ import XCTest @testable import System #endif -import SystemInternals - private func makeRaw( _ str: String ) -> [CInterop.PlatformChar] { diff --git a/Tests/SystemTests/TestingInfrastructure.swift b/Tests/SystemTests/TestingInfrastructure.swift index fa44bb0..53bb12f 100644 --- a/Tests/SystemTests/TestingInfrastructure.swift +++ b/Tests/SystemTests/TestingInfrastructure.swift @@ -9,12 +9,10 @@ import XCTest -@testable import SystemInternals - #if SYSTEM_PACKAGE -import SystemPackage +@testable import SystemPackage #else -import System +@testable import System #endif // To aid debugging, force failures to fatal error