13
13
// https://github.com/apple/swift-package-manager/blob/main/Sources/PackagePlugin/Plugin.swift
14
14
15
15
#if swift(>=6.0)
16
+ private import _CShims
16
17
public import SwiftSyntaxMacros
17
18
@_spi ( PluginMessage) private import SwiftCompilerPluginMessageHandling
18
19
#if canImport(Darwin)
@@ -23,6 +24,7 @@ private import Glibc
23
24
private import ucrt
24
25
#endif
25
26
#else
27
+ import _CShims
26
28
import SwiftSyntaxMacros
27
29
@_spi ( PluginMessage) import SwiftCompilerPluginMessageHandling
28
30
#if canImport(Darwin)
@@ -115,29 +117,29 @@ extension CompilerPlugin {
115
117
public static func main( ) throws {
116
118
// Duplicate the `stdin` file descriptor, which we will then use for
117
119
// receiving messages from the plugin host.
118
- let inputFD = dup ( fileno ( stdin ) )
120
+ let inputFD = dup ( fileno ( _ss_stdin ( ) ) )
119
121
guard inputFD >= 0 else {
120
- internalError ( " Could not duplicate `stdin`: \( describe ( errno: errno ) ) . " )
122
+ internalError ( " Could not duplicate `stdin`: \( describe ( errno: _ss_errno ( ) ) ) . " )
121
123
}
122
124
123
125
// Having duplicated the original standard-input descriptor, we close
124
126
// `stdin` so that attempts by the plugin to read console input (which
125
127
// are usually a mistake) return errors instead of blocking.
126
- guard close ( fileno ( stdin ) ) >= 0 else {
127
- internalError ( " Could not close `stdin`: \( describe ( errno: errno ) ) . " )
128
+ guard close ( fileno ( _ss_stdin ( ) ) ) >= 0 else {
129
+ internalError ( " Could not close `stdin`: \( describe ( errno: _ss_errno ( ) ) ) . " )
128
130
}
129
131
130
132
// Duplicate the `stdout` file descriptor, which we will then use for
131
133
// sending messages to the plugin host.
132
- let outputFD = dup ( fileno ( stdout ) )
134
+ let outputFD = dup ( fileno ( _ss_stdout ( ) ) )
133
135
guard outputFD >= 0 else {
134
- internalError ( " Could not dup `stdout`: \( describe ( errno: errno ) ) . " )
136
+ internalError ( " Could not dup `stdout`: \( describe ( errno: _ss_errno ( ) ) ) . " )
135
137
}
136
138
137
139
// Having duplicated the original standard-output descriptor, redirect
138
140
// `stdout` to `stderr` so that all free-form text output goes there.
139
- guard dup2 ( fileno ( stderr ) , fileno ( stdout ) ) >= 0 else {
140
- internalError ( " Could not dup2 `stdout` to `stderr`: \( describe ( errno: errno ) ) . " )
141
+ guard dup2 ( fileno ( _ss_stderr ( ) ) , fileno ( _ss_stdout ( ) ) ) >= 0 else {
142
+ internalError ( " Could not dup2 `stdout` to `stderr`: \( describe ( errno: _ss_errno ( ) ) ) . " )
141
143
}
142
144
143
145
// Turn off full buffering so printed text appears as soon as possible.
@@ -146,9 +148,9 @@ extension CompilerPlugin {
146
148
// buffer. As a result, on Windows, we completely disable all
147
149
// buffering, which means that partial writes are possible.
148
150
#if os(Windows)
149
- setvbuf ( stdout , nil , _IONBF, 0 )
151
+ setvbuf ( _ss_stdout ( ) , nil , _IONBF, 0 )
150
152
#else
151
- setvbuf ( stdout , nil , _IOLBF, 0 )
153
+ setvbuf ( _ss_stdout ( ) , nil , _IOLBF, 0 )
152
154
#endif
153
155
154
156
// Open a message channel for communicating with the plugin host.
@@ -172,15 +174,10 @@ extension CompilerPlugin {
172
174
173
175
// Private function to report internal errors and then exit.
174
176
fileprivate static func internalError( _ message: String ) -> Never {
175
- fputs ( " Internal Error: \( message) \n " , stderr )
177
+ fputs ( " Internal Error: \( message) \n " , _ss_stderr ( ) )
176
178
exit ( 1 )
177
179
}
178
180
179
- // Private function to construct an error message from an `errno` code.
180
- fileprivate static func describe( errno: Int32 ) -> String {
181
- if let cStr = strerror ( errno) { return String ( cString: cStr) }
182
- return String ( describing: errno)
183
- }
184
181
}
185
182
186
183
internal struct PluginHostConnection : MessageConnection {
@@ -234,24 +231,29 @@ internal struct PluginHostConnection: MessageConnection {
234
231
}
235
232
}
236
233
237
- func _write( _ fd: CInt , contentsOf buffer: UnsafeRawBufferPointer ) throws {
234
+ // Private function to construct an error message from an `errno` code.
235
+ private func describe( errno: CInt ) -> String {
236
+ if let cStr = strerror ( errno) { return String ( cString: cStr) }
237
+ return String ( describing: errno)
238
+ }
239
+
240
+ private func _write( _ fd: CInt , contentsOf buffer: UnsafeRawBufferPointer ) throws {
238
241
var ptr = buffer. baseAddress!
239
242
var remaining = buffer. count
240
243
while remaining > 0 {
241
- switch write ( fd, ptr, remaining) {
244
+ switch write ( fd, ptr, numericCast ( remaining) ) {
242
245
case 0 :
243
246
throw CompilerPluginError ( message: " write(2) closed " )
244
247
case - 1 :
245
- let err = String ( cString: strerror ( errno) )
246
- throw CompilerPluginError ( message: " read(2) failed: \( err) " )
248
+ throw CompilerPluginError ( message: " read(2) failed: \( describe ( errno: _ss_errno ( ) ) ) " )
247
249
case let result:
248
250
ptr += Int ( result)
249
251
remaining -= Int ( result)
250
252
}
251
253
}
252
254
}
253
255
254
- func _reading< T> ( _ fd: CInt , count: Int , _ fn: ( UnsafeRawBufferPointer ) throws -> T ) throws -> T {
256
+ private func _reading< T> ( _ fd: CInt , count: Int , _ fn: ( UnsafeRawBufferPointer ) throws -> T ) throws -> T {
255
257
guard count > 0 else {
256
258
return try fn ( UnsafeRawBufferPointer ( start: nil , count: 0 ) )
257
259
}
@@ -261,13 +263,12 @@ func _reading<T>(_ fd: CInt, count: Int, _ fn: (UnsafeRawBufferPointer) throws -
261
263
var ptr = buffer. baseAddress!
262
264
var remaining = buffer. count
263
265
while remaining > 0 {
264
- switch read ( fd, ptr, remaining) {
266
+ switch read ( fd, ptr, numericCast ( remaining) ) {
265
267
case 0 :
266
268
// Input is closed.
267
269
return try fn ( UnsafeRawBufferPointer ( start: nil , count: 0 ) )
268
270
case - 1 :
269
- let err = String ( cString: strerror ( errno) )
270
- throw CompilerPluginError ( message: " read(2) failed: \( err) " )
271
+ throw CompilerPluginError ( message: " read(2) failed: \( describe ( errno: _ss_errno ( ) ) ) " )
271
272
case let result:
272
273
ptr += Int ( result)
273
274
remaining -= Int ( result)
0 commit comments