Skip to content

Commit 9647382

Browse files
committed
Unify cache locations and pruning strategies
- Use the same cache path for all tools in the build.This is safe because it's versioned internally - Wire up the cache pruning mechanism to generic task caching to ensure consistent behavior - Improve test coverage of replay in incremental builds
1 parent 6f94303 commit 9647382

18 files changed

+365
-78
lines changed

Sources/SWBApplePlatform/Specs/AssetCatalogCompiler.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -299,17 +299,17 @@ public final class ActoolCompilerSpec : GenericCompilerSpec, SpecIdentifierType,
299299
(macro == BuiltinMacros.ASSETCATALOG_COMPILER_INPUTS) ? cbc.scope.table.namespace.parseLiteralStringList(assetSymbolInputs.map { $0.path.str }) : lookup(macro)
300300
}
301301

302-
let cachingEnabled = cbc.scope.evaluate(BuiltinMacros.ENABLE_GENERIC_TASK_CACHING)
303302
let action: (any PlannedTaskAction)?
304303
if let deferredAction = delegate.taskActionCreationDelegate.createDeferredExecutionTaskActionIfRequested(userPreferences: cbc.producer.userPreferences) {
305304
action = deferredAction
306-
} else if cachingEnabled {
305+
} else if cbc.scope.evaluate(BuiltinMacros.ENABLE_GENERIC_TASK_CACHING), let casOptions = try? CASOptions.create(cbc.scope, nil) {
307306
action = delegate.taskActionCreationDelegate.createGenericCachingTaskAction(
308307
enableCacheDebuggingRemarks: cbc.scope.evaluate(BuiltinMacros.GENERIC_TASK_CACHE_ENABLE_DIAGNOSTIC_REMARKS),
309308
enableTaskSandboxEnforcement: !cbc.scope.evaluate(BuiltinMacros.DISABLE_TASK_SANDBOXING),
310309
sandboxDirectory: cbc.scope.evaluate(BuiltinMacros.TEMP_SANDBOX_DIR),
311310
extraSandboxSubdirectories: [],
312-
developerDirectory: cbc.scope.evaluate(BuiltinMacros.DEVELOPER_DIR)
311+
developerDirectory: cbc.scope.evaluate(BuiltinMacros.DEVELOPER_DIR),
312+
casOptions: casOptions
313313
)
314314
} else {
315315
action = nil
@@ -359,17 +359,17 @@ public final class ActoolCompilerSpec : GenericCompilerSpec, SpecIdentifierType,
359359
var ruleInfo = defaultRuleInfo(cbc, delegate, lookup: lookup)
360360
ruleInfo[0..<1] = ["CompileAssetCatalogVariant", variant.rawValue]
361361

362-
let cachingEnabled = cbc.scope.evaluate(BuiltinMacros.ENABLE_GENERIC_TASK_CACHING)
363362
let action: (any PlannedTaskAction)?
364363
if let deferredAction = delegate.taskActionCreationDelegate.createDeferredExecutionTaskActionIfRequested(userPreferences: cbc.producer.userPreferences) {
365364
action = deferredAction
366-
} else if cachingEnabled {
365+
} else if cbc.scope.evaluate(BuiltinMacros.ENABLE_GENERIC_TASK_CACHING), let casOptions = try? CASOptions.create(cbc.scope, nil) {
367366
action = delegate.taskActionCreationDelegate.createGenericCachingTaskAction(
368367
enableCacheDebuggingRemarks: cbc.scope.evaluate(BuiltinMacros.GENERIC_TASK_CACHE_ENABLE_DIAGNOSTIC_REMARKS),
369368
enableTaskSandboxEnforcement: !cbc.scope.evaluate(BuiltinMacros.DISABLE_TASK_SANDBOXING),
370369
sandboxDirectory: cbc.scope.evaluate(BuiltinMacros.TEMP_SANDBOX_DIR),
371370
extraSandboxSubdirectories: [Path("outputs/0/\(overrideDir.basename)")],
372-
developerDirectory: cbc.scope.evaluate(BuiltinMacros.DEVELOPER_DIR)
371+
developerDirectory: cbc.scope.evaluate(BuiltinMacros.DEVELOPER_DIR),
372+
casOptions: casOptions
373373
)
374374
} else {
375375
action = nil

Sources/SWBApplePlatform/Specs/InterfaceBuilderCompiler.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,14 @@ public final class IbtoolCompilerSpecStoryboard: IbtoolCompilerSpec, SpecIdentif
158158
}
159159

160160
override public func createTaskAction(_ cbc: CommandBuildContext, _ delegate: any TaskGenerationDelegate) -> (any PlannedTaskAction)? {
161-
if cbc.scope.evaluate(BuiltinMacros.ENABLE_GENERIC_TASK_CACHING) {
161+
if cbc.scope.evaluate(BuiltinMacros.ENABLE_GENERIC_TASK_CACHING), let casOptions = try? CASOptions.create(cbc.scope, nil) {
162162
return delegate.taskActionCreationDelegate.createGenericCachingTaskAction(
163163
enableCacheDebuggingRemarks: cbc.scope.evaluate(BuiltinMacros.GENERIC_TASK_CACHE_ENABLE_DIAGNOSTIC_REMARKS),
164164
enableTaskSandboxEnforcement: !cbc.scope.evaluate(BuiltinMacros.DISABLE_TASK_SANDBOXING),
165165
sandboxDirectory: cbc.scope.evaluate(BuiltinMacros.TEMP_SANDBOX_DIR),
166166
extraSandboxSubdirectories: [],
167-
developerDirectory: cbc.scope.evaluate(BuiltinMacros.DEVELOPER_DIR))
167+
developerDirectory: cbc.scope.evaluate(BuiltinMacros.DEVELOPER_DIR),
168+
casOptions: casOptions)
168169
} else {
169170
return nil
170171
}

Sources/SWBApplePlatform/Specs/RealityAssetsCompilerSpec.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,23 @@ package final class RealityAssetsCompilerSpec: GenericCompilerSpec, SpecIdentifi
175175
inputs.append(delegate.createNode(usdaSchemaPath) as (any PlannedNode))
176176
}
177177

178-
let cachingEnabled = cbc.scope.evaluate(BuiltinMacros.ENABLE_GENERIC_TASK_CACHING)
178+
let action: (any PlannedTaskAction)?
179+
let cachingEnabled: Bool
180+
if cbc.scope.evaluate(BuiltinMacros.ENABLE_GENERIC_TASK_CACHING), let casOptions = try? CASOptions.create(cbc.scope, nil) {
181+
action = delegate.taskActionCreationDelegate.createGenericCachingTaskAction(
182+
enableCacheDebuggingRemarks: cbc.scope.evaluate(BuiltinMacros.GENERIC_TASK_CACHE_ENABLE_DIAGNOSTIC_REMARKS),
183+
enableTaskSandboxEnforcement: !cbc.scope.evaluate(BuiltinMacros.DISABLE_TASK_SANDBOXING),
184+
sandboxDirectory: cbc.scope.evaluate(BuiltinMacros.TEMP_SANDBOX_DIR),
185+
extraSandboxSubdirectories: [],
186+
developerDirectory: cbc.scope.evaluate(BuiltinMacros.DEVELOPER_DIR),
187+
casOptions: casOptions
188+
)
189+
cachingEnabled = true
190+
} else {
191+
action = nil
192+
cachingEnabled = false
193+
}
194+
179195

180196
let ruleInfo = ["RealityAssetsCompile", cbc.output.str]
181197
delegate.createTask(type: self,
@@ -190,7 +206,7 @@ package final class RealityAssetsCompilerSpec: GenericCompilerSpec, SpecIdentifi
190206
inputs: inputs,
191207
outputs: outputs,
192208
mustPrecede: [],
193-
action: cachingEnabled ? delegate.taskActionCreationDelegate.createGenericCachingTaskAction(enableCacheDebuggingRemarks: cbc.scope.evaluate(BuiltinMacros.GENERIC_TASK_CACHE_ENABLE_DIAGNOSTIC_REMARKS), enableTaskSandboxEnforcement: !cbc.scope.evaluate(BuiltinMacros.DISABLE_TASK_SANDBOXING), sandboxDirectory: cbc.scope.evaluate(BuiltinMacros.TEMP_SANDBOX_DIR), extraSandboxSubdirectories: [], developerDirectory: cbc.scope.evaluate(BuiltinMacros.DEVELOPER_DIR)) : nil,
209+
action: action,
194210
execDescription: "Compile Reality Asset \(rkAssetsPath.basename)",
195211
preparesForIndexing: true,
196212
enableSandboxing: !cachingEnabled,

Sources/SWBBuildSystem/BuildOperation.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,15 +1339,23 @@ internal final class OperationSystemAdaptor: SWBLLBuild.BuildSystemDelegate, Act
13391339
self.buildOutputDelegate = buildOutputDelegate
13401340
self._progressStatistics = BuildOperation.ProgressStatistics(numCommandsLowerBound: operation.buildDescription.targetTaskCounts.values.reduce(0, { $0 + $1 }))
13411341
self.core = core
1342-
self.dynamicOperationContext = DynamicTaskOperationContext(core: core, definingTargetsByModuleName: operation.buildDescription.definingTargetsByModuleName, cas: Self.setupCAS(core: core, operation: operation))
1342+
let cas: ToolchainCAS?
1343+
do {
1344+
cas = try Self.setupCAS(core: core, operation: operation)
1345+
} catch {
1346+
buildOutputDelegate.error(error.localizedDescription)
1347+
cas = nil
1348+
}
1349+
self.dynamicOperationContext = DynamicTaskOperationContext(core: core, definingTargetsByModuleName: operation.buildDescription.definingTargetsByModuleName, cas: cas)
13431350
self.queue = SWBQueue(label: "SWBBuildSystem.OperationSystemAdaptor.queue", qos: operation.request.qos, autoreleaseFrequency: .workItem)
13441351
}
13451352

1346-
private static func setupCAS(core: Core, operation: BuildOperation) -> ToolchainCAS? {
1353+
private static func setupCAS(core: Core, operation: BuildOperation) throws -> ToolchainCAS? {
13471354
let settings = operation.requestContext.getCachedSettings(operation.request.parameters)
1348-
let cachePath = Path(settings.globalScope.evaluate(BuiltinMacros.COMPILATION_CACHE_CAS_PATH)).join("swiftbuild")
1355+
let casOptions = try CASOptions.create(settings.globalScope, nil)
13491356
guard let casPlugin = core.lookupCASPlugin() else { return nil }
1350-
guard let cas = try? casPlugin.createCAS(path: cachePath, options: [:]) else { return nil }
1357+
guard let cas = try? casPlugin.createCAS(path: casOptions.casPath, options: [:]) else { return nil }
1358+
13511359
return cas
13521360
}
13531361

Sources/SWBCAS/Errors.swift

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,43 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
public enum ToolchainCASPluginError: Error, Sendable {
13+
public enum ToolchainCASPluginError: Error, Sendable, CustomStringConvertible {
1414
case missingRequiredSymbol(String)
1515
case settingCASOptionFailed(String?)
1616
case casCreationFailed(String?)
1717
case storeFailed(String?)
1818
case loadFailed(String?)
1919
case cacheInsertionFailed(String?)
2020
case cacheLookupFailed(String?)
21+
case casSizeOperationUnsupported
22+
case casSizeOperationFailed(String?)
23+
case casPruneOperationUnsupported
24+
case casPruneOperationFailed(String?)
25+
26+
public var description: String {
27+
switch self {
28+
case .missingRequiredSymbol(let symbol):
29+
"missing required symbol: \(symbol)"
30+
case .settingCASOptionFailed(let detail):
31+
"setting CAS option failed: \(detail ?? "unknown error")"
32+
case .casCreationFailed(let detail):
33+
"creating CAS failed: \(detail ?? "unknown error")"
34+
case .storeFailed(let detail):
35+
"CAS store failed: \(detail ?? "unknown error")"
36+
case .loadFailed(let detail):
37+
"CAS load failed: \(detail ?? "unknown error")"
38+
case .cacheInsertionFailed(let detail):
39+
"cache insert failed: \(detail ?? "unknown error")"
40+
case .cacheLookupFailed(let detail):
41+
"cache lookup failed: \(detail ?? "unknown error")"
42+
case .casSizeOperationUnsupported:
43+
"CAS does not support size lookup"
44+
case .casSizeOperationFailed(let detail):
45+
"CAS size operation failed: \(detail ?? "unknown error")"
46+
case .casPruneOperationUnsupported:
47+
"CAS does not support pruning"
48+
case .casPruneOperationFailed(let detail):
49+
"CAS prune operation failed: \(detail ?? "unknown error")"
50+
}
51+
}
2152
}

Sources/SWBCAS/ToolchainCASPlugin.swift

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,61 @@ public final class ToolchainCAS: @unchecked Sendable, CASProtocol, ActionCachePr
172172
})
173173
}
174174

175+
public func getOnDiskSize() throws -> Int64 {
176+
var error: UnsafeMutablePointer<CChar>? = nil
177+
guard let llcas_cas_get_ondisk_size = api.llcas_cas_get_ondisk_size else {
178+
throw ToolchainCASPluginError.casSizeOperationUnsupported
179+
}
180+
let result = llcas_cas_get_ondisk_size(cCas, &error)
181+
switch result {
182+
case -1:
183+
throw ToolchainCASPluginError.casSizeOperationUnsupported
184+
case -2:
185+
if let error = error {
186+
let detailedError = String(cString: error)
187+
api.llcas_string_dispose(error)
188+
throw ToolchainCASPluginError.casSizeOperationFailed(detailedError)
189+
}
190+
throw ToolchainCASPluginError.casSizeOperationFailed(nil)
191+
default:
192+
return result
193+
}
194+
}
195+
196+
public func setOnDiskSizeLimit(_ limit: Int64) throws {
197+
var error: UnsafeMutablePointer<CChar>? = nil
198+
guard let llcas_cas_set_ondisk_size_limit = api.llcas_cas_set_ondisk_size_limit else {
199+
throw ToolchainCASPluginError.casSizeOperationUnsupported
200+
}
201+
if llcas_cas_set_ondisk_size_limit(cCas, limit, &error) {
202+
if let error = error {
203+
let detailedError = String(cString: error)
204+
api.llcas_string_dispose(error)
205+
throw ToolchainCASPluginError.casSizeOperationFailed(detailedError)
206+
}
207+
throw ToolchainCASPluginError.casSizeOperationFailed(nil)
208+
}
209+
}
210+
211+
public func prune() throws {
212+
var error: UnsafeMutablePointer<CChar>? = nil
213+
guard let llcas_cas_prune_ondisk_data = api.llcas_cas_prune_ondisk_data else {
214+
throw ToolchainCASPluginError.casPruneOperationUnsupported
215+
}
216+
if llcas_cas_prune_ondisk_data(cCas, &error) {
217+
if let error = error {
218+
let detailedError = String(cString: error)
219+
api.llcas_string_dispose(error)
220+
throw ToolchainCASPluginError.casPruneOperationFailed(detailedError)
221+
}
222+
throw ToolchainCASPluginError.casPruneOperationFailed(nil)
223+
}
224+
}
225+
226+
public var supportsPruning: Bool {
227+
api.llcas_cas_get_ondisk_size != nil && api.llcas_cas_set_ondisk_size_limit != nil && api.llcas_cas_prune_ondisk_data != nil
228+
}
229+
175230
deinit {
176231
api.llcas_cas_dispose(cCas)
177232
}

Sources/SWBCore/PlannedTaskAction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public protocol TaskActionCreationDelegate
316316
func createDeferredExecutionTaskAction() -> any PlannedTaskAction
317317
func createEmbedSwiftStdLibTaskAction() -> any PlannedTaskAction
318318
func createFileCopyTaskAction(_ context: FileCopyTaskActionContext) -> any PlannedTaskAction
319-
func createGenericCachingTaskAction(enableCacheDebuggingRemarks: Bool, enableTaskSandboxEnforcement: Bool, sandboxDirectory: Path, extraSandboxSubdirectories: [Path], developerDirectory: Path) -> any PlannedTaskAction
319+
func createGenericCachingTaskAction(enableCacheDebuggingRemarks: Bool, enableTaskSandboxEnforcement: Bool, sandboxDirectory: Path, extraSandboxSubdirectories: [Path], developerDirectory: Path, casOptions: CASOptions) -> any PlannedTaskAction
320320
func createInfoPlistProcessorTaskAction(_ contextPath: Path) -> any PlannedTaskAction
321321
func createMergeInfoPlistTaskAction() -> any PlannedTaskAction
322322
func createLinkAssetCatalogTaskAction() -> any PlannedTaskAction

0 commit comments

Comments
 (0)