@@ -432,20 +432,6 @@ package final class BuildOperation: BuildSystemOperation {
432
432
433
433
let dbPath = persistent ? buildDescription. buildDatabasePath : Path ( " " )
434
434
435
- func saveBuildDebuggingData( from sourcePath: Path , to filename: String , type: String , debuggingDataPath: Path ? ) {
436
- guard let debuggingDataPath else {
437
- return
438
- }
439
- do {
440
- try fs. createDirectory ( debuggingDataPath, recursive: true )
441
- let savedPath = debuggingDataPath. join ( filename)
442
- try fs. copy ( sourcePath, to: savedPath)
443
- self . buildOutputDelegate. note ( " build debugging is enabled, \( type) : ' \( savedPath. str) ' " )
444
- } catch {
445
- self . buildOutputDelegate. warning ( " unable to preserve \( type) for post-mortem debugging: \( error) " )
446
- }
447
- }
448
-
449
435
// Enable build debugging, if requested.
450
436
let traceFile : Path ?
451
437
let debuggingDataPath : Path ?
@@ -501,6 +487,33 @@ package final class BuildOperation: BuildSystemOperation {
501
487
self . buildOutputDelegate. error ( " unable to retrieve additional environment variables via the EnvironmentExtensionPoint. " )
502
488
}
503
489
490
+ // If we use a cached build system, be sure to release it on build completion.
491
+ if userPreferences. enableBuildSystemCaching {
492
+ // Get the build system to use, keyed by the directory containing the (sole) database.
493
+ let entry = cachedBuildSystems. getOrInsert ( buildDescription. buildDatabasePath. dirname, { SystemCacheEntry ( ) } )
494
+ return await entry. lock. withLock { [ buildEnvironment] _ in
495
+ await _build ( cacheEntry: entry, dbPath: dbPath, traceFile: traceFile, debuggingDataPath: debuggingDataPath, buildEnvironment: buildEnvironment)
496
+ }
497
+ } else {
498
+ return await _build ( cacheEntry: nil , dbPath: dbPath, traceFile: traceFile, debuggingDataPath: debuggingDataPath, buildEnvironment: buildEnvironment)
499
+ }
500
+ }
501
+
502
+ private func saveBuildDebuggingData( from sourcePath: Path , to filename: String , type: String , debuggingDataPath: Path ? ) {
503
+ guard let debuggingDataPath else {
504
+ return
505
+ }
506
+ do {
507
+ try fs. createDirectory ( debuggingDataPath, recursive: true )
508
+ let savedPath = debuggingDataPath. join ( filename)
509
+ try fs. copy ( sourcePath, to: savedPath)
510
+ self . buildOutputDelegate. note ( " build debugging is enabled, \( type) : ' \( savedPath. str) ' " )
511
+ } catch {
512
+ self . buildOutputDelegate. warning ( " unable to preserve \( type) for post-mortem debugging: \( error) " )
513
+ }
514
+ }
515
+
516
+ private func _build( cacheEntry entry: SystemCacheEntry ? , dbPath: Path , traceFile: Path ? , debuggingDataPath: Path ? , buildEnvironment: [ String : String ] ) async -> BuildOperationEnded . Status {
504
517
let algorithm : BuildSystem . SchedulerAlgorithm = {
505
518
if let algorithmString = UserDefaults . schedulerAlgorithm {
506
519
if let algorithm = BuildSystem . SchedulerAlgorithm ( rawValue: algorithmString) {
@@ -517,13 +530,7 @@ package final class BuildOperation: BuildSystemOperation {
517
530
// Create the low-level build system.
518
531
let adaptor : OperationSystemAdaptor
519
532
let system : BuildSystem
520
-
521
- // If we use a cached build system, be sure to release it on build completion.
522
- var cacheEntry : SystemCacheEntry ? = nil
523
- defer {
524
- cacheEntry? . lock. signal ( )
525
- }
526
-
533
+
527
534
let llbQoS : SWBLLBuild . BuildSystem . QualityOfService ?
528
535
switch request. qos {
529
536
case . default: llbQoS = . default
@@ -533,16 +540,7 @@ package final class BuildOperation: BuildSystemOperation {
533
540
default : llbQoS = nil
534
541
}
535
542
536
- if userPreferences. enableBuildSystemCaching {
537
- // Get the build system to use, keyed by the directory containing the (sole) database.
538
- let key = buildDescription. buildDatabasePath. dirname
539
- let entry = cachedBuildSystems. getOrInsert ( key) { SystemCacheEntry ( ) }
540
- cacheEntry = entry
541
-
542
- // Wait for exclusive lock on this cache entry to prevent concurrent
543
- // use/free of accompanying objects.
544
- await entry. lock. wait ( )
545
-
543
+ if let entry {
546
544
// If the entry is valid, reuse it.
547
545
if let cachedAdaptor = entry. adaptor, entry. environment == buildEnvironment, entry. buildDescription! === buildDescription, entry. llbQoS == llbQoS {
548
546
adaptor = cachedAdaptor
0 commit comments