@@ -57,44 +57,52 @@ func withService(
57
57
await service. close ( )
58
58
}
59
59
60
- func withSession (
60
+ public func createSession (
61
61
service: SWBBuildService ,
62
62
name: String ,
63
63
toolchainPath: Basics . AbsolutePath ,
64
- packageManagerResourcesDirectory: Basics . AbsolutePath ? ,
65
- body: @escaping (
66
- _ session: SWBBuildServiceSession ,
67
- _ diagnostics: [ SwiftBuild . SwiftBuildMessage . DiagnosticInfo ]
68
- ) async throws -> Void
69
- ) async throws {
64
+ packageManagerResourcesDirectory: Basics . AbsolutePath ?
65
+ ) async throws -> ( SWBBuildServiceSession , [ SwiftBuildMessage . DiagnosticInfo ] ) {
70
66
// SWIFT_EXEC and SWIFT_EXEC_MANIFEST may need to be overridden in debug scenarios in order to pick up Open Source toolchains
71
67
let sessionResult = if toolchainPath. components. contains ( where: { $0. hasSuffix ( " .xctoolchain " ) } ) {
72
68
await service. createSession ( name: name, developerPath: nil , resourceSearchPaths: packageManagerResourcesDirectory. map { [ $0. pathString] } ?? [ ] , cachePath: nil , inferiorProductsPath: nil , environment: nil )
73
69
} else {
74
70
await service. createSession ( name: name, swiftToolchainPath: toolchainPath. pathString, resourceSearchPaths: packageManagerResourcesDirectory. map { [ $0. pathString] } ?? [ ] , cachePath: nil , inferiorProductsPath: nil , environment: nil )
75
71
}
76
-
77
72
switch sessionResult {
78
73
case ( . success( let session) , let diagnostics) :
79
- do {
80
- try await body ( session, diagnostics)
81
- } catch {
82
- do {
83
- try await session. close ( )
84
- } catch _ {
85
- // Assumption is that the first error is the most important one
86
- throw SessionFailedError ( error: error, diagnostics: diagnostics)
87
- }
88
-
89
- throw SessionFailedError ( error: error, diagnostics: diagnostics)
90
- }
74
+ return ( session, diagnostics)
75
+ case ( . failure( let error) , let diagnostics) :
76
+ throw SessionFailedError ( error: error, diagnostics: diagnostics)
77
+ }
78
+ }
91
79
80
+ func withSession(
81
+ service: SWBBuildService ,
82
+ name: String ,
83
+ toolchainPath: Basics . AbsolutePath ,
84
+ packageManagerResourcesDirectory: Basics . AbsolutePath ? ,
85
+ body: @escaping (
86
+ _ session: SWBBuildServiceSession ,
87
+ _ diagnostics: [ SwiftBuild . SwiftBuildMessage . DiagnosticInfo ]
88
+ ) async throws -> Void
89
+ ) async throws {
90
+ let ( session, diagnostics) = try await createSession ( service: service, name: name, toolchainPath: toolchainPath, packageManagerResourcesDirectory: packageManagerResourcesDirectory)
91
+ do {
92
+ try await body ( session, diagnostics)
93
+ } catch let bodyError {
92
94
do {
93
95
try await session. close ( )
94
- } catch {
95
- throw SessionFailedError ( error: error, diagnostics: diagnostics)
96
+ } catch _ {
97
+ // Assumption is that the first error is the most important one
98
+ throw bodyError
96
99
}
97
- case ( . failure( let error) , let diagnostics) :
100
+
101
+ throw bodyError
102
+ }
103
+ do {
104
+ try await session. close ( )
105
+ } catch {
98
106
throw SessionFailedError ( error: error, diagnostics: diagnostics)
99
107
}
100
108
}
@@ -272,13 +280,7 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
272
280
return
273
281
}
274
282
275
- let pifBuilder = try await getPIFBuilder ( )
276
- let pif = try await pifBuilder. generatePIF (
277
- printPIFManifestGraphviz: buildParameters. printPIFManifestGraphviz,
278
- buildParameters: buildParameters,
279
- )
280
-
281
- try self . fileSystem. writeIfChanged ( path: buildParameters. pifManifest, string: pif)
283
+ try await writePIF ( buildParameters: buildParameters)
282
284
283
285
try await startSWBuildOperation ( pifTargetName: subset. pifTargetName)
284
286
}
@@ -287,8 +289,7 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
287
289
let buildStartTime = ContinuousClock . Instant. now
288
290
289
291
try await withService ( connectionMode: . inProcessStatic( swiftbuildServiceEntryPoint) ) { service in
290
- let parameters = try self . makeBuildParameters ( )
291
- let derivedDataPath = self . buildParameters. dataPath. pathString
292
+ let derivedDataPath = self . buildParameters. dataPath
292
293
293
294
let progressAnimation = ProgressAnimation . percent (
294
295
stream: self . outputStream,
@@ -311,7 +312,7 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
311
312
}
312
313
313
314
// Find the targets to build.
314
- let configuredTargets : [ SWBConfiguredTarget ]
315
+ let configuredTargets : [ SWBTargetGUID ]
315
316
do {
316
317
let workspaceInfo = try await session. workspaceInfo ( )
317
318
@@ -322,7 +323,7 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
322
323
self . observabilityScope. emit ( error: " Could not find target named ' \( targetName) ' " )
323
324
throw Diagnostics . fatalError
324
325
case 1 :
325
- return SWBConfiguredTarget ( guid : infos [ 0 ] . guid, parameters : parameters )
326
+ return SWBTargetGUID ( rawValue : infos [ 0 ] . guid)
326
327
default :
327
328
self . observabilityScope. emit ( error: " Found multiple targets named ' \( targetName) ' " )
328
329
throw Diagnostics . fatalError
@@ -333,44 +334,7 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
333
334
throw error
334
335
}
335
336
336
- var request = SWBBuildRequest ( )
337
- request. parameters = parameters
338
- request. configuredTargets = configuredTargets
339
- request. useParallelTargets = true
340
- request. useImplicitDependencies = false
341
- request. useDryRun = false
342
- request. hideShellScriptEnvironment = true
343
- request. showNonLoggedProgress = true
344
-
345
- // Override the arena. We need to apply the arena info to both the request-global build
346
- // parameters as well as the target-specific build parameters, since they may have been
347
- // deserialized from the build request file above overwriting the build parameters we set
348
- // up earlier in this method.
349
-
350
- #if os(Windows)
351
- let ddPathPrefix = derivedDataPath. replacingOccurrences ( of: " \\ " , with: " / " )
352
- #else
353
- let ddPathPrefix = derivedDataPath
354
- #endif
355
-
356
- let arenaInfo = SWBArenaInfo (
357
- derivedDataPath: ddPathPrefix,
358
- buildProductsPath: ddPathPrefix + " /Products " ,
359
- buildIntermediatesPath: ddPathPrefix + " /Intermediates.noindex " ,
360
- pchPath: ddPathPrefix + " /PCH " ,
361
- indexRegularBuildProductsPath: nil ,
362
- indexRegularBuildIntermediatesPath: nil ,
363
- indexPCHPath: ddPathPrefix,
364
- indexDataStoreFolderPath: ddPathPrefix,
365
- indexEnableDataStore: request. parameters. arenaInfo? . indexEnableDataStore ?? false
366
- )
367
-
368
- request. parameters. arenaInfo = arenaInfo
369
- request. configuredTargets = request. configuredTargets. map { configuredTarget in
370
- var configuredTarget = configuredTarget
371
- configuredTarget. parameters? . arenaInfo = arenaInfo
372
- return configuredTarget
373
- }
337
+ let request = try self . makeBuildRequest ( configuredTargets: configuredTargets, derivedDataPath: derivedDataPath)
374
338
375
339
struct BuildState {
376
340
private var activeTasks : [ Int : SwiftBuild . SwiftBuildMessage . TaskStartedInfo ] = [ : ]
@@ -495,7 +459,7 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
495
459
}
496
460
}
497
461
498
- func makeRunDestination( ) -> SwiftBuild . SWBRunDestinationInfo {
462
+ private func makeRunDestination( ) -> SwiftBuild . SWBRunDestinationInfo {
499
463
let platformName : String
500
464
let sdkName : String
501
465
if self . buildParameters. triple. isAndroid ( ) {
@@ -528,7 +492,7 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
528
492
)
529
493
}
530
494
531
- func makeBuildParameters( ) throws -> SwiftBuild . SWBBuildParameters {
495
+ private func makeBuildParameters( ) throws -> SwiftBuild . SWBBuildParameters {
532
496
// Generate the run destination parameters.
533
497
let runDestination = makeRunDestination ( )
534
498
@@ -609,6 +573,49 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
609
573
return params
610
574
}
611
575
576
+ public func makeBuildRequest( configuredTargets: [ SWBTargetGUID ] , derivedDataPath: Basics . AbsolutePath ) throws -> SWBBuildRequest {
577
+ var request = SWBBuildRequest ( )
578
+ request. parameters = try makeBuildParameters ( )
579
+ request. configuredTargets = configuredTargets. map { SWBConfiguredTarget ( guid: $0. rawValue, parameters: request. parameters) }
580
+ request. useParallelTargets = true
581
+ request. useImplicitDependencies = false
582
+ request. useDryRun = false
583
+ request. hideShellScriptEnvironment = true
584
+ request. showNonLoggedProgress = true
585
+
586
+ // Override the arena. We need to apply the arena info to both the request-global build
587
+ // parameters as well as the target-specific build parameters, since they may have been
588
+ // deserialized from the build request file above overwriting the build parameters we set
589
+ // up earlier in this method.
590
+
591
+ #if os(Windows)
592
+ let ddPathPrefix = derivedDataPath. pathString. replacingOccurrences ( of: " \\ " , with: " / " )
593
+ #else
594
+ let ddPathPrefix = derivedDataPath. pathString
595
+ #endif
596
+
597
+ let arenaInfo = SWBArenaInfo (
598
+ derivedDataPath: ddPathPrefix,
599
+ buildProductsPath: ddPathPrefix + " /Products " ,
600
+ buildIntermediatesPath: ddPathPrefix + " /Intermediates.noindex " ,
601
+ pchPath: ddPathPrefix + " /PCH " ,
602
+ indexRegularBuildProductsPath: nil ,
603
+ indexRegularBuildIntermediatesPath: nil ,
604
+ indexPCHPath: ddPathPrefix,
605
+ indexDataStoreFolderPath: ddPathPrefix,
606
+ indexEnableDataStore: request. parameters. arenaInfo? . indexEnableDataStore ?? false
607
+ )
608
+
609
+ request. parameters. arenaInfo = arenaInfo
610
+ request. configuredTargets = request. configuredTargets. map { configuredTarget in
611
+ var configuredTarget = configuredTarget
612
+ configuredTarget. parameters? . arenaInfo = arenaInfo
613
+ return configuredTarget
614
+ }
615
+
616
+ return request
617
+ }
618
+
612
619
private static func constructDebuggingSettingsOverrides( from parameters: BuildParameters . Debugging ) -> [ String : String ] {
613
620
var settings : [ String : String ] = [ : ]
614
621
// TODO: debugInfoFormat: https://github.com/swiftlang/swift-build/issues/560
@@ -700,6 +707,16 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
700
707
}
701
708
}
702
709
710
+ public func writePIF( buildParameters: BuildParameters ) async throws {
711
+ let pifBuilder = try await getPIFBuilder ( )
712
+ let pif = try await pifBuilder. generatePIF (
713
+ printPIFManifestGraphviz: buildParameters. printPIFManifestGraphviz,
714
+ buildParameters: buildParameters,
715
+ )
716
+
717
+ try self . fileSystem. writeIfChanged ( path: buildParameters. pifManifest, string: pif)
718
+ }
719
+
703
720
public func cancel( deadline: DispatchTime ) throws { }
704
721
705
722
/// Returns the package graph using the graph loader closure.
0 commit comments