@@ -66,7 +66,7 @@ struct CMakeSmokeTest: CommandPlugin {
6666 }
6767
6868 var sharedSwiftFlags = [
69- " -module-cache-path " , moduleCachePath
69+ " -module-cache-path " , moduleCachePath,
7070 ]
7171
7272 if let sysrootPath {
@@ -79,15 +79,16 @@ struct CMakeSmokeTest: CommandPlugin {
7979 " -DTSC_DIR= \( swiftToolsSupportCoreBuildURL. appending ( components: " cmake " , " modules " ) . filePath) " ,
8080 " -DSwiftDriver_DIR= \( swiftDriverBuildURL. appending ( components: " cmake " , " modules " ) . filePath) " ,
8181 " -DSwiftSystem_DIR= \( swiftSystemBuildURL. appending ( components: " cmake " , " modules " ) . filePath) " ,
82- " -DSwiftToolsProtocols_DIR= \( swiftToolsProtocolsBuildURL. appending ( components: " cmake " , " modules " ) . filePath) "
82+ " -DSwiftToolsProtocols_DIR= \( swiftToolsProtocolsBuildURL. appending ( components: " cmake " , " modules " ) . filePath) " ,
8383 ]
8484
85- let sharedCMakeArgs = [
86- " -G " , " Ninja " ,
87- " -DCMAKE_MAKE_PROGRAM= \( ninjaPath) " ,
88- " -DCMAKE_BUILD_TYPE:=Debug " ,
89- " -DCMAKE_Swift_FLAGS=' \( sharedSwiftFlags. joined ( separator: " " ) ) ' "
90- ] + cMakeProjectArgs + extraCMakeArgs
85+ let sharedCMakeArgs =
86+ [
87+ " -G " , " Ninja " ,
88+ " -DCMAKE_MAKE_PROGRAM= \( ninjaPath) " ,
89+ " -DCMAKE_BUILD_TYPE:=Debug " ,
90+ " -DCMAKE_Swift_FLAGS=' \( sharedSwiftFlags. joined ( separator: " " ) ) ' " ,
91+ ] + cMakeProjectArgs + extraCMakeArgs
9192
9293 Diagnostics . progress ( " Building swift-tools-support-core " )
9394 try await Process . checkNonZeroExit ( url: cmakeURL, arguments: sharedCMakeArgs + [ swiftToolsSupportCoreURL. filePath] , workingDirectory: swiftToolsSupportCoreBuildURL)
@@ -168,13 +169,13 @@ enum OS {
168169
169170 static func host( ) throws -> Self {
170171 #if os(macOS)
171- return . macOS
172+ return . macOS
172173 #elseif os(Linux)
173- return . linux
174+ return . linux
174175 #elseif os(Windows)
175- return . windows
176+ return . windows
176177 #else
177- throw Errors . unimplementedForHostOS
178+ throw Errors . unimplementedForHostOS
178179 #endif
179180 }
180181}
@@ -211,100 +212,104 @@ extension Process {
211212 static func checkNonZeroExit( url: URL , arguments: [ String ] , workingDirectory: URL , environment: [ String : String ] ? = nil ) async throws {
212213 try Diagnostics . progress ( " \( url. filePath) \( arguments. joined ( separator: " " ) ) " )
213214 #if USE_PROCESS_SPAWNING_WORKAROUND && !os(Windows)
214- Diagnostics . progress ( " Using process spawning workaround " )
215- // Linux workaround for https://github.com/swiftlang/swift-corelibs-foundation/issues/4772
216- // Foundation.Process on Linux seems to inherit the Process.run()-calling thread's signal mask, creating processes that even have SIGTERM blocked
217- // This manifests as CMake getting stuck when invoking 'uname' with incorrectly configured signal handlers.
218- var fileActions = posix_spawn_file_actions_t ( )
219- defer { posix_spawn_file_actions_destroy ( & fileActions) }
220- var attrs : posix_spawnattr_t = posix_spawnattr_t ( )
221- defer { posix_spawnattr_destroy ( & attrs) }
222- posix_spawn_file_actions_init ( & fileActions)
223- try posix_spawn_file_actions_addchdir_np ( & fileActions, workingDirectory. filePath)
224-
225- posix_spawnattr_init ( & attrs)
226- posix_spawnattr_setpgroup ( & attrs, 0 )
227- var noSignals = sigset_t ( )
228- sigemptyset ( & noSignals)
229- posix_spawnattr_setsigmask ( & attrs, & noSignals)
230-
231- var mostSignals = sigset_t ( )
232- sigemptyset ( & mostSignals)
233- for i in 1 ..< SIGSYS {
234- if i == SIGKILL || i == SIGSTOP {
235- continue
236- }
237- sigaddset ( & mostSignals, i)
238- }
239- posix_spawnattr_setsigdefault ( & attrs, & mostSignals)
240- posix_spawnattr_setflags ( & attrs, numericCast ( POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK) )
241- var pid : pid_t = - 1
242- try withArrayOfCStrings ( [ url. filePath] + arguments) { arguments in
243- try withArrayOfCStrings ( ( environment ?? [ : ] ) . map { key, value in " \( key) = \( value) " } ) { environment in
244- let spawnResult = try posix_spawn ( & pid, url. filePath, /*file_actions=*/& fileActions, /*attrp=*/& attrs, arguments, nil ) ;
245- var exitCode : Int32 = - 1
246- var result = wait4 ( pid, & exitCode, 0 , nil ) ;
247- while ( result == - 1 && errno == EINTR) {
248- result = wait4 ( pid, & exitCode, 0 , nil )
249- }
250- guard result != - 1 else {
251- throw Errors . miscError ( " wait failed " )
215+ Diagnostics . progress ( " Using process spawning workaround " )
216+ // Linux workaround for https://github.com/swiftlang/swift-corelibs-foundation/issues/4772
217+ // Foundation.Process on Linux seems to inherit the Process.run()-calling thread's signal mask, creating processes that even have SIGTERM blocked
218+ // This manifests as CMake getting stuck when invoking 'uname' with incorrectly configured signal handlers.
219+ var fileActions = posix_spawn_file_actions_t ( )
220+ defer { posix_spawn_file_actions_destroy ( & fileActions) }
221+ var attrs : posix_spawnattr_t = posix_spawnattr_t ( )
222+ defer { posix_spawnattr_destroy ( & attrs) }
223+ posix_spawn_file_actions_init ( & fileActions)
224+ try posix_spawn_file_actions_addchdir_np ( & fileActions, workingDirectory. filePath)
225+
226+ posix_spawnattr_init ( & attrs)
227+ posix_spawnattr_setpgroup ( & attrs, 0 )
228+ var noSignals = sigset_t ( )
229+ sigemptyset ( & noSignals)
230+ posix_spawnattr_setsigmask ( & attrs, & noSignals)
231+
232+ var mostSignals = sigset_t ( )
233+ sigemptyset ( & mostSignals)
234+ for i in 1 ..< SIGSYS {
235+ if i == SIGKILL || i == SIGSTOP {
236+ continue
252237 }
253- guard exitCode == 0 else {
254- throw Errors . miscError ( " exit code nonzero " )
238+ sigaddset ( & mostSignals, i)
239+ }
240+ posix_spawnattr_setsigdefault ( & attrs, & mostSignals)
241+ posix_spawnattr_setflags ( & attrs, numericCast ( POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK) )
242+ var pid : pid_t = - 1
243+ try withArrayOfCStrings ( [ url. filePath] + arguments) { arguments in
244+ try withArrayOfCStrings ( ( environment ?? [ : ] ) . map { key, value in " \( key) = \( value) " } ) { environment in
245+ let spawnResult = try posix_spawn ( & pid, url. filePath, /*file_actions=*/ & fileActions, /*attrp=*/ & attrs, arguments, nil ) ;
246+ var exitCode : Int32 = - 1
247+ var result = wait4 ( pid, & exitCode, 0 , nil ) ;
248+ while ( result == - 1 && errno == EINTR) {
249+ result = wait4 ( pid, & exitCode, 0 , nil )
250+ }
251+ guard result != - 1 else {
252+ throw Errors . miscError ( " wait failed " )
253+ }
254+ guard exitCode == 0 else {
255+ throw Errors . miscError ( " exit code nonzero " )
256+ }
255257 }
256258 }
257- }
258259 #else
259- let process = Process ( )
260- process. executableURL = url
261- process. arguments = arguments
262- process. currentDirectoryURL = workingDirectory
263- process. environment = environment
264- try await process. run ( )
265- if process. terminationStatus != 0 {
266- throw Errors . processError ( terminationReason: process. terminationReason, terminationStatus: process. terminationStatus)
267- }
260+ let process = Process ( )
261+ process. executableURL = url
262+ process. arguments = arguments
263+ process. currentDirectoryURL = workingDirectory
264+ process. environment = environment
265+ try await process. run ( )
266+ if process. terminationStatus != 0 {
267+ throw Errors . processError ( terminationReason: process. terminationReason, terminationStatus: process. terminationStatus)
268+ }
268269 #endif
269270 }
270271}
271272
272273#if USE_PROCESS_SPAWNING_WORKAROUND && !os(Windows)
273- func scan< S: Sequence , U> ( _ seq: S , _ initial: U , _ combine: ( U , S . Element ) -> U ) -> [ U ] {
274- var result : [ U ] = [ ]
275- result. reserveCapacity ( seq. underestimatedCount)
276- var runningResult = initial
277- for element in seq {
278- runningResult = combine ( runningResult, element)
279- result. append ( runningResult)
280- }
281- return result
282- }
274+ func scan< S: Sequence , U> ( _ seq: S , _ initial: U , _ combine: ( U , S . Element ) -> U ) -> [ U ] {
275+ var result : [ U ] = [ ]
276+ result. reserveCapacity ( seq. underestimatedCount)
277+ var runningResult = initial
278+ for element in seq {
279+ runningResult = combine ( runningResult, element)
280+ result. append ( runningResult)
281+ }
282+ return result
283+ }
283284
284- func withArrayOfCStrings< T> (
285- _ args: [ String ] ,
286- _ body: ( UnsafePointer < UnsafeMutablePointer < Int8 > ? > ) throws -> T
287- ) throws -> T {
288- let argsCounts = Array ( args. map { $0. utf8. count + 1 } )
289- let argsOffsets = [ 0 ] + scan( argsCounts, 0 , + )
290- let argsBufferSize = argsOffsets. last!
291- var argsBuffer : [ UInt8 ] = [ ]
292- argsBuffer. reserveCapacity ( argsBufferSize)
293- for arg in args {
294- argsBuffer. append ( contentsOf: arg. utf8)
295- argsBuffer. append ( 0 )
296- }
297- return try argsBuffer. withUnsafeMutableBufferPointer {
298- ( argsBuffer) in
299- let ptr = UnsafeRawPointer ( argsBuffer. baseAddress!) . bindMemory (
300- to: Int8 . self, capacity: argsBuffer. count)
301- var cStrings : [ UnsafePointer < Int8 > ? ] = argsOffsets. map { ptr + $0 }
302- cStrings [ cStrings. count - 1 ] = nil
303- return try cStrings. withUnsafeMutableBufferPointer {
304- let unsafeString = UnsafeMutableRawPointer ( $0. baseAddress!) . bindMemory (
305- to: UnsafeMutablePointer< Int8>? . self , capacity: $0. count)
306- return try body ( unsafeString)
285+ func withArrayOfCStrings< T> (
286+ _ args: [ String ] ,
287+ _ body: ( UnsafePointer < UnsafeMutablePointer < Int8 > ? > ) throws -> T
288+ ) throws -> T {
289+ let argsCounts = Array ( args. map { $0. utf8. count + 1 } )
290+ let argsOffsets = [ 0 ] + scan( argsCounts, 0 , + )
291+ let argsBufferSize = argsOffsets. last!
292+ var argsBuffer : [ UInt8 ] = [ ]
293+ argsBuffer. reserveCapacity ( argsBufferSize)
294+ for arg in args {
295+ argsBuffer. append ( contentsOf: arg. utf8)
296+ argsBuffer. append ( 0 )
297+ }
298+ return try argsBuffer. withUnsafeMutableBufferPointer {
299+ ( argsBuffer) in
300+ let ptr = UnsafeRawPointer ( argsBuffer. baseAddress!) . bindMemory (
301+ to: Int8 . self,
302+ capacity: argsBuffer. count
303+ )
304+ var cStrings : [ UnsafePointer < Int8 > ? ] = argsOffsets. map { ptr + $0 }
305+ cStrings [ cStrings. count - 1 ] = nil
306+ return try cStrings. withUnsafeMutableBufferPointer {
307+ let unsafeString = UnsafeMutableRawPointer ( $0. baseAddress!) . bindMemory (
308+ to: UnsafeMutablePointer< Int8>? . self ,
309+ capacity: $0. count
310+ )
311+ return try body ( unsafeString)
312+ }
313+ }
307314 }
308- }
309- }
310315#endif
0 commit comments