@@ -63,7 +63,7 @@ struct CMakeSmokeTest: CommandPlugin {
6363 }
6464
6565 var sharedSwiftFlags = [
66- " -module-cache-path " , moduleCachePath
66+ " -module-cache-path " , moduleCachePath,
6767 ]
6868
6969 if let sysrootPath {
@@ -75,15 +75,16 @@ struct CMakeSmokeTest: CommandPlugin {
7575 " -DLLBuild_DIR= \( llbuildBuildURL. appending ( components: " cmake " , " modules " ) . filePath) " ,
7676 " -DTSC_DIR= \( swiftToolsSupportCoreBuildURL. appending ( components: " cmake " , " modules " ) . filePath) " ,
7777 " -DSwiftDriver_DIR= \( swiftDriverBuildURL. appending ( components: " cmake " , " modules " ) . filePath) " ,
78- " -DSwiftSystem_DIR= \( swiftSystemBuildURL. appending ( components: " cmake " , " modules " ) . filePath) "
78+ " -DSwiftSystem_DIR= \( swiftSystemBuildURL. appending ( components: " cmake " , " modules " ) . filePath) " ,
7979 ]
8080
81- let sharedCMakeArgs = [
82- " -G " , " Ninja " ,
83- " -DCMAKE_MAKE_PROGRAM= \( ninjaPath) " ,
84- " -DCMAKE_BUILD_TYPE:=Debug " ,
85- " -DCMAKE_Swift_FLAGS=' \( sharedSwiftFlags. joined ( separator: " " ) ) ' "
86- ] + cMakeProjectArgs + extraCMakeArgs
81+ let sharedCMakeArgs =
82+ [
83+ " -G " , " Ninja " ,
84+ " -DCMAKE_MAKE_PROGRAM= \( ninjaPath) " ,
85+ " -DCMAKE_BUILD_TYPE:=Debug " ,
86+ " -DCMAKE_Swift_FLAGS=' \( sharedSwiftFlags. joined ( separator: " " ) ) ' " ,
87+ ] + cMakeProjectArgs + extraCMakeArgs
8788
8889 Diagnostics . progress ( " Building swift-tools-support-core " )
8990 try await Process . checkNonZeroExit ( url: cmakeURL, arguments: sharedCMakeArgs + [ swiftToolsSupportCoreURL. filePath] , workingDirectory: swiftToolsSupportCoreBuildURL)
@@ -159,13 +160,13 @@ enum OS {
159160
160161 static func host( ) throws -> Self {
161162 #if os(macOS)
162- return . macOS
163+ return . macOS
163164 #elseif os(Linux)
164- return . linux
165+ return . linux
165166 #elseif os(Windows)
166- return . windows
167+ return . windows
167168 #else
168- throw Errors . unimplementedForHostOS
169+ throw Errors . unimplementedForHostOS
169170 #endif
170171 }
171172}
@@ -202,100 +203,104 @@ extension Process {
202203 static func checkNonZeroExit( url: URL , arguments: [ String ] , workingDirectory: URL , environment: [ String : String ] ? = nil ) async throws {
203204 try Diagnostics . progress ( " \( url. filePath) \( arguments. joined ( separator: " " ) ) " )
204205 #if USE_PROCESS_SPAWNING_WORKAROUND && !os(Windows)
205- Diagnostics . progress ( " Using process spawning workaround " )
206- // Linux workaround for https://github.com/swiftlang/swift-corelibs-foundation/issues/4772
207- // Foundation.Process on Linux seems to inherit the Process.run()-calling thread's signal mask, creating processes that even have SIGTERM blocked
208- // This manifests as CMake getting stuck when invoking 'uname' with incorrectly configured signal handlers.
209- var fileActions = posix_spawn_file_actions_t ( )
210- defer { posix_spawn_file_actions_destroy ( & fileActions) }
211- var attrs : posix_spawnattr_t = posix_spawnattr_t ( )
212- defer { posix_spawnattr_destroy ( & attrs) }
213- posix_spawn_file_actions_init ( & fileActions)
214- try posix_spawn_file_actions_addchdir_np ( & fileActions, workingDirectory. filePath)
215-
216- posix_spawnattr_init ( & attrs)
217- posix_spawnattr_setpgroup ( & attrs, 0 )
218- var noSignals = sigset_t ( )
219- sigemptyset ( & noSignals)
220- posix_spawnattr_setsigmask ( & attrs, & noSignals)
221-
222- var mostSignals = sigset_t ( )
223- sigemptyset ( & mostSignals)
224- for i in 1 ..< SIGSYS {
225- if i == SIGKILL || i == SIGSTOP {
226- continue
227- }
228- sigaddset ( & mostSignals, i)
229- }
230- posix_spawnattr_setsigdefault ( & attrs, & mostSignals)
231- posix_spawnattr_setflags ( & attrs, numericCast ( POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK) )
232- var pid : pid_t = - 1
233- try withArrayOfCStrings ( [ url. filePath] + arguments) { arguments in
234- try withArrayOfCStrings ( ( environment ?? [ : ] ) . map { key, value in " \( key) = \( value) " } ) { environment in
235- let spawnResult = try posix_spawn ( & pid, url. filePath, /*file_actions=*/& fileActions, /*attrp=*/& attrs, arguments, nil ) ;
236- var exitCode : Int32 = - 1
237- var result = wait4 ( pid, & exitCode, 0 , nil ) ;
238- while ( result == - 1 && errno == EINTR) {
239- result = wait4 ( pid, & exitCode, 0 , nil )
240- }
241- guard result != - 1 else {
242- throw Errors . miscError ( " wait failed " )
206+ Diagnostics . progress ( " Using process spawning workaround " )
207+ // Linux workaround for https://github.com/swiftlang/swift-corelibs-foundation/issues/4772
208+ // Foundation.Process on Linux seems to inherit the Process.run()-calling thread's signal mask, creating processes that even have SIGTERM blocked
209+ // This manifests as CMake getting stuck when invoking 'uname' with incorrectly configured signal handlers.
210+ var fileActions = posix_spawn_file_actions_t ( )
211+ defer { posix_spawn_file_actions_destroy ( & fileActions) }
212+ var attrs : posix_spawnattr_t = posix_spawnattr_t ( )
213+ defer { posix_spawnattr_destroy ( & attrs) }
214+ posix_spawn_file_actions_init ( & fileActions)
215+ try posix_spawn_file_actions_addchdir_np ( & fileActions, workingDirectory. filePath)
216+
217+ posix_spawnattr_init ( & attrs)
218+ posix_spawnattr_setpgroup ( & attrs, 0 )
219+ var noSignals = sigset_t ( )
220+ sigemptyset ( & noSignals)
221+ posix_spawnattr_setsigmask ( & attrs, & noSignals)
222+
223+ var mostSignals = sigset_t ( )
224+ sigemptyset ( & mostSignals)
225+ for i in 1 ..< SIGSYS {
226+ if i == SIGKILL || i == SIGSTOP {
227+ continue
243228 }
244- guard exitCode == 0 else {
245- throw Errors . miscError ( " exit code nonzero " )
229+ sigaddset ( & mostSignals, i)
230+ }
231+ posix_spawnattr_setsigdefault ( & attrs, & mostSignals)
232+ posix_spawnattr_setflags ( & attrs, numericCast ( POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK) )
233+ var pid : pid_t = - 1
234+ try withArrayOfCStrings ( [ url. filePath] + arguments) { arguments in
235+ try withArrayOfCStrings ( ( environment ?? [ : ] ) . map { key, value in " \( key) = \( value) " } ) { environment in
236+ let spawnResult = try posix_spawn ( & pid, url. filePath, /*file_actions=*/ & fileActions, /*attrp=*/ & attrs, arguments, nil ) ;
237+ var exitCode : Int32 = - 1
238+ var result = wait4 ( pid, & exitCode, 0 , nil ) ;
239+ while ( result == - 1 && errno == EINTR) {
240+ result = wait4 ( pid, & exitCode, 0 , nil )
241+ }
242+ guard result != - 1 else {
243+ throw Errors . miscError ( " wait failed " )
244+ }
245+ guard exitCode == 0 else {
246+ throw Errors . miscError ( " exit code nonzero " )
247+ }
246248 }
247249 }
248- }
249250 #else
250- let process = Process ( )
251- process. executableURL = url
252- process. arguments = arguments
253- process. currentDirectoryURL = workingDirectory
254- process. environment = environment
255- try await process. run ( )
256- if process. terminationStatus != 0 {
257- throw Errors . processError ( terminationReason: process. terminationReason, terminationStatus: process. terminationStatus)
258- }
251+ let process = Process ( )
252+ process. executableURL = url
253+ process. arguments = arguments
254+ process. currentDirectoryURL = workingDirectory
255+ process. environment = environment
256+ try await process. run ( )
257+ if process. terminationStatus != 0 {
258+ throw Errors . processError ( terminationReason: process. terminationReason, terminationStatus: process. terminationStatus)
259+ }
259260 #endif
260261 }
261262}
262263
263264#if USE_PROCESS_SPAWNING_WORKAROUND && !os(Windows)
264- func scan< S: Sequence , U> ( _ seq: S , _ initial: U , _ combine: ( U , S . Element ) -> U ) -> [ U ] {
265- var result : [ U ] = [ ]
266- result. reserveCapacity ( seq. underestimatedCount)
267- var runningResult = initial
268- for element in seq {
269- runningResult = combine ( runningResult, element)
270- result. append ( runningResult)
271- }
272- return result
273- }
265+ func scan< S: Sequence , U> ( _ seq: S , _ initial: U , _ combine: ( U , S . Element ) -> U ) -> [ U ] {
266+ var result : [ U ] = [ ]
267+ result. reserveCapacity ( seq. underestimatedCount)
268+ var runningResult = initial
269+ for element in seq {
270+ runningResult = combine ( runningResult, element)
271+ result. append ( runningResult)
272+ }
273+ return result
274+ }
274275
275- func withArrayOfCStrings< T> (
276- _ args: [ String ] ,
277- _ body: ( UnsafePointer < UnsafeMutablePointer < Int8 > ? > ) throws -> T
278- ) throws -> T {
279- let argsCounts = Array ( args. map { $0. utf8. count + 1 } )
280- let argsOffsets = [ 0 ] + scan( argsCounts, 0 , + )
281- let argsBufferSize = argsOffsets. last!
282- var argsBuffer : [ UInt8 ] = [ ]
283- argsBuffer. reserveCapacity ( argsBufferSize)
284- for arg in args {
285- argsBuffer. append ( contentsOf: arg. utf8)
286- argsBuffer. append ( 0 )
287- }
288- return try argsBuffer. withUnsafeMutableBufferPointer {
289- ( argsBuffer) in
290- let ptr = UnsafeRawPointer ( argsBuffer. baseAddress!) . bindMemory (
291- to: Int8 . self, capacity: argsBuffer. count)
292- var cStrings : [ UnsafePointer < Int8 > ? ] = argsOffsets. map { ptr + $0 }
293- cStrings [ cStrings. count - 1 ] = nil
294- return try cStrings. withUnsafeMutableBufferPointer {
295- let unsafeString = UnsafeMutableRawPointer ( $0. baseAddress!) . bindMemory (
296- to: UnsafeMutablePointer< Int8>? . self , capacity: $0. count)
297- return try body ( unsafeString)
276+ func withArrayOfCStrings< T> (
277+ _ args: [ String ] ,
278+ _ body: ( UnsafePointer < UnsafeMutablePointer < Int8 > ? > ) throws -> T
279+ ) throws -> T {
280+ let argsCounts = Array ( args. map { $0. utf8. count + 1 } )
281+ let argsOffsets = [ 0 ] + scan( argsCounts, 0 , + )
282+ let argsBufferSize = argsOffsets. last!
283+ var argsBuffer : [ UInt8 ] = [ ]
284+ argsBuffer. reserveCapacity ( argsBufferSize)
285+ for arg in args {
286+ argsBuffer. append ( contentsOf: arg. utf8)
287+ argsBuffer. append ( 0 )
288+ }
289+ return try argsBuffer. withUnsafeMutableBufferPointer {
290+ ( argsBuffer) in
291+ let ptr = UnsafeRawPointer ( argsBuffer. baseAddress!) . bindMemory (
292+ to: Int8 . self,
293+ capacity: argsBuffer. count
294+ )
295+ var cStrings : [ UnsafePointer < Int8 > ? ] = argsOffsets. map { ptr + $0 }
296+ cStrings [ cStrings. count - 1 ] = nil
297+ return try cStrings. withUnsafeMutableBufferPointer {
298+ let unsafeString = UnsafeMutableRawPointer ( $0. baseAddress!) . bindMemory (
299+ to: UnsafeMutablePointer< Int8>? . self ,
300+ capacity: $0. count
301+ )
302+ return try body ( unsafeString)
303+ }
304+ }
298305 }
299- }
300- }
301306#endif
0 commit comments