Skip to content

Commit d7611ca

Browse files
committed
Internalize SystemInternals
Scrap the separate module SystemInternals and instead have it be a subdirectory. Adjust all access control accordingly. One day, when Swift gets proper submodules or supermodules (i.e. resilience domains), we can reinstate it. For now, a separate module makes binary distribution much more complicated.
1 parent b635546 commit d7611ca

File tree

14 files changed

+93
-130
lines changed

14 files changed

+93
-130
lines changed

Package.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,14 @@ import PackageDescription
1515
let targets: [PackageDescription.Target] = [
1616
.target(
1717
name: "SystemPackage",
18-
dependencies: ["SystemInternals"],
19-
path: "Sources/System"),
20-
.target(
21-
name: "SystemInternals",
2218
dependencies: ["CSystem"],
19+
path: "Sources/System",
2320
swiftSettings: [
2421
.define("ENABLE_MOCKING", .when(configuration: .debug))
2522
]),
2623
.target(
2724
name: "CSystem",
2825
dependencies: []),
29-
3026
.testTarget(
3127
name: "SystemTests",
3228
dependencies: ["SystemPackage"],

Sources/System/Errno.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,6 @@ extension Errno {
14721472
#endif
14731473
}
14741474

1475-
@_implementationOnly import SystemInternals
14761475
// @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
14771476
extension Errno {
14781477
// TODO: We want to provide safe access to `errno`, but we need a

Sources/System/FileOperations.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
See https://swift.org/LICENSE.txt for license information
88
*/
99

10-
@_implementationOnly import SystemInternals
11-
1210
// @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
1311
extension FileDescriptor {
1412
/// Opens or creates a file for reading or writing.
@@ -122,7 +120,7 @@ extension FileDescriptor {
122120
internal func _seek(
123121
offset: Int64, from whence: FileDescriptor.SeekOrigin
124122
) -> Result<Int64, Errno> {
125-
let newOffset = system_lseek(self.rawValue, COffT(offset), whence.rawValue)
123+
let newOffset = system_lseek(self.rawValue, _COffT(offset), whence.rawValue)
126124
return valueOrErrno(Int64(newOffset))
127125
}
128126

@@ -210,7 +208,7 @@ extension FileDescriptor {
210208
retryOnInterrupt: Bool
211209
) -> Result<Int, Errno> {
212210
valueOrErrno(retryOnInterrupt: retryOnInterrupt) {
213-
system_pread(self.rawValue, buffer.baseAddress, buffer.count, COffT(offset))
211+
system_pread(self.rawValue, buffer.baseAddress, buffer.count, _COffT(offset))
214212
}
215213
}
216214

@@ -292,7 +290,7 @@ extension FileDescriptor {
292290
retryOnInterrupt: Bool
293291
) -> Result<Int, Errno> {
294292
valueOrErrno(retryOnInterrupt: retryOnInterrupt) {
295-
system_pwrite(self.rawValue, buffer.baseAddress, buffer.count, COffT(offset))
293+
system_pwrite(self.rawValue, buffer.baseAddress, buffer.count, _COffT(offset))
296294
}
297295
}
298296

Sources/System/FilePath/FilePathParsing.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,6 @@ extension FilePath {
322322
}
323323

324324
// Whether we are providing Windows paths
325-
@_implementationOnly import var SystemInternals.forceWindowsPaths
326325
@inline(__always)
327326
internal var _windowsPaths: Bool {
328327
#if os(Windows)

Sources/SystemInternals/Exports.swift renamed to Sources/System/Internals/Exports.swift

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,26 @@ import CSystem
1515
// TODO: Should CSystem just include all the header files we need?
1616

1717
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
18-
import Darwin
18+
@_implementationOnly import Darwin
1919
#elseif os(Linux) || os(FreeBSD) || os(Android)
20-
import Glibc
20+
@_implementationOnly import Glibc
2121
#elseif os(Windows)
22-
import ucrt
22+
@_implementationOnly import ucrt
2323
#else
2424
#error("Unsupported Platform")
2525
#endif
2626

27-
public typealias COffT = off_t
28-
29-
#if os(Windows)
30-
public typealias CModeT = CInt
31-
#else
32-
public typealias CModeT = mode_t
33-
#endif
27+
internal typealias _COffT = off_t
3428

3529
// MARK: syscalls and variables
3630

3731
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
38-
public var system_errno: CInt {
32+
internal var system_errno: CInt {
3933
get { Darwin.errno }
4034
set { Darwin.errno = newValue }
4135
}
4236
#elseif os(Windows)
43-
public var system_errno: CInt {
37+
internal var system_errno: CInt {
4438
get {
4539
var value: CInt = 0
4640
// TODO(compnerd) handle the error?
@@ -52,7 +46,7 @@ public var system_errno: CInt {
5246
}
5347
}
5448
#else
55-
public var system_errno: CInt {
49+
internal var system_errno: CInt {
5650
get { Glibc.errno }
5751
set { Glibc.errno = newValue }
5852
}
@@ -62,33 +56,21 @@ public var system_errno: CInt {
6256

6357
// Convention: `system_foo` is system's wrapper for `foo`.
6458

65-
public func system_strerror(_ __errnum: Int32) -> UnsafeMutablePointer<Int8>! {
59+
internal func system_strerror(_ __errnum: Int32) -> UnsafeMutablePointer<Int8>! {
6660
strerror(__errnum)
6761
}
6862

69-
public func system_strlen(_ s: UnsafePointer<Int8>) -> Int {
63+
internal func system_strlen(_ s: UnsafePointer<Int8>) -> Int {
7064
strlen(s)
7165
}
7266

73-
#if os(Windows)
74-
public typealias _PlatformChar = UInt16
75-
#else
76-
public typealias _PlatformChar = CChar
77-
#endif
78-
#if os(Windows)
79-
public typealias _PlatformUnicodeEncoding = UTF16
80-
#else
81-
public typealias _PlatformUnicodeEncoding = UTF8
82-
#endif
83-
84-
8567
// Convention: `system_platform_foo` is a
8668
// platform-representation-abstracted wrapper around `foo`-like functionality.
8769
// Type and layout differences such as the `char` vs `wchar` are abstracted.
8870
//
8971

9072
// strlen for the platform string
91-
public func system_platform_strlen(_ s: UnsafePointer<_PlatformChar>) -> Int {
73+
internal func system_platform_strlen(_ s: UnsafePointer<CInterop.PlatformChar>) -> Int {
9274
#if os(Windows)
9375
return wcslen(s)
9476
#else
@@ -98,8 +80,8 @@ public func system_platform_strlen(_ s: UnsafePointer<_PlatformChar>) -> Int {
9880

9981
// Interop between String and platfrom string
10082
extension String {
101-
public func _withPlatformString<Result>(
102-
_ body: (UnsafePointer<_PlatformChar>) throws -> Result
83+
internal func _withPlatformString<Result>(
84+
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
10385
) rethrows -> Result {
10486
// Need to #if because CChar may be signed
10587
#if os(Windows)
@@ -109,7 +91,7 @@ extension String {
10991
#endif
11092
}
11193

112-
public init?(_platformString platformString: UnsafePointer<_PlatformChar>) {
94+
internal init?(_platformString platformString: UnsafePointer<CInterop.PlatformChar>) {
11395
// Need to #if because CChar may be signed
11496
#if os(Windows)
11597
guard let strRes = String.decodeCString(
@@ -126,8 +108,8 @@ extension String {
126108
#endif
127109
}
128110

129-
public init(
130-
_errorCorrectingPlatformString platformString: UnsafePointer<_PlatformChar>
111+
internal init(
112+
_errorCorrectingPlatformString platformString: UnsafePointer<CInterop.PlatformChar>
131113
) {
132114
// Need to #if because CChar may be signed
133115
#if os(Windows)

Sources/SystemInternals/Mocking.swift renamed to Sources/System/Internals/Mocking.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ internal enum ForceErrno: Equatable {
5353
}
5454

5555
// Provide access to the driver, context, and trace stack of mocking
56-
public class MockingDriver {
56+
internal class MockingDriver {
5757
// Record syscalls and their arguments
5858
internal var trace = Trace()
5959

@@ -109,7 +109,7 @@ private var contextualMockingEnabled: Bool {
109109
extension MockingDriver {
110110
internal static var enabled: Bool { mockingEnabled }
111111

112-
public static var forceWindowsPaths: Bool {
112+
internal static var forceWindowsPaths: Bool {
113113
currentMockingDriver?.forceWindowsSyntaxForPaths ?? false
114114
}
115115
}
@@ -126,8 +126,8 @@ internal var mockingEnabled: Bool {
126126
#endif
127127
}
128128

129-
@inline(__always) @inlinable
130-
public var forceWindowsPaths: Bool {
129+
@inline(__always)
130+
internal var forceWindowsPaths: Bool {
131131
#if !ENABLE_MOCKING
132132
return false
133133
#else
@@ -183,8 +183,8 @@ internal func _mockInt(
183183

184184
internal func _mockOffT(
185185
name: String = #function, _ args: AnyHashable...
186-
) -> COffT {
187-
COffT(mockImpl(name: name, args))
186+
) -> _COffT {
187+
_COffT(mockImpl(name: name, args))
188188
}
189189
#endif // ENABLE_MOCKING
190190

Sources/SystemInternals/Syscalls.swift renamed to Sources/System/Internals/Syscalls.swift

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
*/
99

1010
#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
11-
import Darwin
11+
@_implementationOnly import Darwin
1212
#elseif os(Linux) || os(FreeBSD) || os(Android)
13-
import Glibc
13+
@_implementationOnly import Glibc
1414
#elseif os(Windows)
15-
import ucrt
15+
@_implementationOnly import ucrt
1616
#else
1717
#error("Unsupported Platform")
1818
#endif
@@ -21,8 +21,8 @@ import ucrt
2121
// amount of code size, so we hand outline that code for every syscall
2222

2323
// open
24-
public func system_open(
25-
_ path: UnsafePointer<_PlatformChar>, _ oflag: Int32
24+
internal func system_open(
25+
_ path: UnsafePointer<CInterop.PlatformChar>, _ oflag: Int32
2626
) -> CInt {
2727
#if ENABLE_MOCKING
2828
if mockingEnabled {
@@ -32,8 +32,9 @@ public func system_open(
3232
return open(path, oflag)
3333
}
3434

35-
public func system_open(
36-
_ path: UnsafePointer<_PlatformChar>, _ oflag: Int32, _ mode: CModeT
35+
internal func system_open(
36+
_ path: UnsafePointer<CInterop.PlatformChar>,
37+
_ oflag: Int32, _ mode: CInterop.Mode
3738
) -> CInt {
3839
#if ENABLE_MOCKING
3940
if mockingEnabled {
@@ -44,15 +45,15 @@ public func system_open(
4445
}
4546

4647
// close
47-
public func system_close(_ fd: Int32) -> Int32 {
48+
internal func system_close(_ fd: Int32) -> Int32 {
4849
#if ENABLE_MOCKING
4950
if mockingEnabled { return _mock(fd) }
5051
#endif
5152
return close(fd)
5253
}
5354

5455
// read
55-
public func system_read(
56+
internal func system_read(
5657
_ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int
5758
) -> Int {
5859
#if ENABLE_MOCKING
@@ -62,7 +63,7 @@ public func system_read(
6263
}
6364

6465
// pread
65-
public func system_pread(
66+
internal func system_pread(
6667
_ fd: Int32, _ buf: UnsafeMutableRawPointer!, _ nbyte: Int, _ offset: off_t
6768
) -> Int {
6869
#if ENABLE_MOCKING
@@ -72,7 +73,7 @@ public func system_pread(
7273
}
7374

7475
// lseek
75-
public func system_lseek(
76+
internal func system_lseek(
7677
_ fd: Int32, _ off: off_t, _ whence: Int32
7778
) -> off_t {
7879
#if ENABLE_MOCKING
@@ -82,7 +83,7 @@ public func system_lseek(
8283
}
8384

8485
// write
85-
public func system_write(
86+
internal func system_write(
8687
_ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int
8788
) -> Int {
8889
#if ENABLE_MOCKING
@@ -92,7 +93,7 @@ public func system_write(
9293
}
9394

9495
// pwrite
95-
public func system_pwrite(
96+
internal func system_pwrite(
9697
_ fd: Int32, _ buf: UnsafeRawPointer!, _ nbyte: Int, _ offset: off_t
9798
) -> Int {
9899
#if ENABLE_MOCKING

Sources/SystemInternals/WindowsSyscallAdapters.swift renamed to Sources/System/Internals/WindowsSyscallAdapters.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
#if os(Windows)
1111

12-
import ucrt
13-
import WinSDK
12+
@_implementationOnly import ucrt
13+
@_implementationOnly import WinSDK
1414

1515
@inline(__always)
1616
internal func open(
17-
_ path: UnsafePointer<_PlatformChar>, _ oflag: Int32
17+
_ path: UnsafePointer<CInterop.PlatformChar>, _ oflag: Int32
1818
) -> CInt {
1919
var fh: CInt = -1
2020
_ = _wsopen_s(&fh, path, oflag, _SH_DENYNO, _S_IREAD | _S_IWRITE)
@@ -23,7 +23,7 @@ internal func open(
2323

2424
@inline(__always)
2525
internal func open(
26-
_ path: UnsafePointer<_PlatformChar>, _ oflag: Int32, _ mode: CModeT
26+
_ path: UnsafePointer<CInterop.PlatformChar>, _ oflag: Int32, _ mode: CModeT
2727
) -> CInt {
2828
// TODO(compnerd): Apply read/write permissions
2929
var fh: CInt = -1

Sources/System/Platform/Platform.swift

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
See https://swift.org/LICENSE.txt for license information
88
*/
99

10-
@_implementationOnly import SystemInternals
11-
12-
// Public typealiases that can't be reexported from SystemInternals
10+
// MARK: - Public typealiases
1311

1412
/// The C `mode_t` type.
1513
// @available(macOS 10.16, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
@@ -32,4 +30,30 @@ public enum CInterop {
3230

3331
/// The C `char` type
3432
public typealias Char = CChar
33+
34+
#if os(Windows)
35+
/// The platform's preferred character type. On Unix, this is an 8-bit C
36+
/// `char` (which may be signed or unsigned, depending on platform). On
37+
/// Windows, this is `UInt16` (a "wide" character).
38+
public typealias PlatformChar = UInt16
39+
#else
40+
/// The platform's preferred character type. On Unix, this is an 8-bit C
41+
/// `char` (which may be signed or unsigned, depending on platform). On
42+
/// Windows, this is `UInt16` (a "wide" character).
43+
public typealias PlatformChar = CInterop.Char
44+
#endif
45+
46+
#if os(Windows)
47+
/// The platform's preferred Unicode encoding. On Unix this is UTF-8 and on
48+
/// Windows it is UTF-16. Native strings may contain invalid Unicode,
49+
/// which will be handled by either error-correction or failing, depending
50+
/// on API.
51+
public typealias PlatformUnicodeEncoding = UTF16
52+
#else
53+
/// The platform's preferred Unicode encoding. On Unix this is UTF-8 and on
54+
/// Windows it is UTF-16. Native strings may contain invalid Unicode,
55+
/// which will be handled by either error-correction or failing, depending
56+
/// on API.
57+
public typealias PlatformUnicodeEncoding = UTF8
58+
#endif
3559
}

0 commit comments

Comments
 (0)