Skip to content

Commit 9b6ffb8

Browse files
authored
Merge pull request #776 from owenv/owenv/module-cache-setting
Add a mechanism to clear the global module cache at the end of a build for testing
2 parents 6045cd0 + 7935840 commit 9b6ffb8

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

Sources/SWBBuildSystem/BuildOperation.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,6 +1555,7 @@ internal final class OperationSystemAdaptor: SWBLLBuild.BuildSystemDelegate, Act
15551555
func waitForCompletion(buildSucceeded: Bool) async {
15561556
let completionToken = await dynamicOperationContext.waitForCompletion()
15571557
cleanupCompilationCache()
1558+
cleanupGlobalModuleCache()
15581559

15591560
await queue.sync {
15601561
self.isCompleted = true
@@ -1601,6 +1602,33 @@ internal final class OperationSystemAdaptor: SWBLLBuild.BuildSystemDelegate, Act
16011602
}
16021603
}
16031604

1605+
func cleanupGlobalModuleCache() {
1606+
let settings = operation.requestContext.getCachedSettings(operation.request.parameters)
1607+
if settings.globalScope.evaluate(BuiltinMacros.KEEP_GLOBAL_MODULE_CACHE_DIRECTORY) {
1608+
return // Keep the cache directory.
1609+
}
1610+
1611+
let cachePath = settings.globalScope.evaluate(BuiltinMacros.MODULE_CACHE_DIR)
1612+
guard !cachePath.isEmpty, operation.fs.exists(cachePath) else {
1613+
return
1614+
}
1615+
1616+
let signatureCtx = InsecureHashContext()
1617+
signatureCtx.add(string: "CleanupGlobalModuleCache")
1618+
signatureCtx.add(string: cachePath.str)
1619+
let signature = signatureCtx.signature
1620+
1621+
withActivity(ruleInfo: "CleanupGlobalModuleCache \(cachePath.str)", executionDescription: "Cleanup global module cache at \(cachePath)", signature: signature, target: nil, parentActivity: nil) { activity in
1622+
do {
1623+
try operation.fs.removeDirectory(cachePath)
1624+
} catch {
1625+
// Log error but do not fail the build.
1626+
emit(diagnostic: Diagnostic.init(behavior: .warning, location: .unknown, data: DiagnosticData("Failed to remove \(cachePath): \(error.localizedDescription)")), for: activity, signature: signature)
1627+
}
1628+
return .succeeded
1629+
}
1630+
}
1631+
16041632
/// Get the active output delegate for an executing command.
16051633
///
16061634
/// - returns: The active delegate, or nil if not found.

Sources/SWBCore/Settings/BuiltinMacros.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,7 @@ public final class BuiltinMacros {
547547
public static let COMPILATION_CACHE_ENABLE_PLUGIN = BuiltinMacros.declareBooleanMacro("COMPILATION_CACHE_ENABLE_PLUGIN")
548548
public static let COMPILATION_CACHE_ENABLE_STRICT_CAS_ERRORS = BuiltinMacros.declareBooleanMacro("COMPILATION_CACHE_ENABLE_STRICT_CAS_ERRORS")
549549
public static let COMPILATION_CACHE_KEEP_CAS_DIRECTORY = BuiltinMacros.declareBooleanMacro("COMPILATION_CACHE_KEEP_CAS_DIRECTORY")
550+
public static let KEEP_GLOBAL_MODULE_CACHE_DIRECTORY = BuiltinMacros.declareBooleanMacro("SCANNING_PCM_KEEP_CACHE_DIRECTORY")
550551
public static let COMPILATION_CACHE_CAS_PATH = BuiltinMacros.declareStringMacro("COMPILATION_CACHE_CAS_PATH")
551552
public static let COMPILATION_CACHE_LIMIT_PERCENT = BuiltinMacros.declareStringMacro("COMPILATION_CACHE_LIMIT_PERCENT")
552553
public static let COMPILATION_CACHE_LIMIT_SIZE = BuiltinMacros.declareStringMacro("COMPILATION_CACHE_LIMIT_SIZE")
@@ -1552,6 +1553,7 @@ public final class BuiltinMacros {
15521553
COMPILATION_CACHE_ENABLE_PLUGIN,
15531554
COMPILATION_CACHE_ENABLE_STRICT_CAS_ERRORS,
15541555
COMPILATION_CACHE_KEEP_CAS_DIRECTORY,
1556+
KEEP_GLOBAL_MODULE_CACHE_DIRECTORY,
15551557
COMPILATION_CACHE_LIMIT_PERCENT,
15561558
COMPILATION_CACHE_LIMIT_SIZE,
15571559
COMPILATION_CACHE_CAS_PATH,

Sources/SWBCore/Settings/Settings.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1976,6 +1976,11 @@ private class SettingsBuilder {
19761976
exportedMacroNames.formUnion(info.exportedMacros)
19771977
errors.append(contentsOf:info.errors)
19781978

1979+
// Default to preserving the module cache directory.
1980+
pushTable(.exported) {
1981+
$0.push(BuiltinMacros.KEEP_GLOBAL_MODULE_CACHE_DIRECTORY, literal: true)
1982+
}
1983+
19791984
if self.parameters.action == .indexBuild {
19801985
pushTable(.exported) {
19811986
$0.push(BuiltinMacros.INDEX_ENABLE_BUILD_ARENA, literal: true)

0 commit comments

Comments
 (0)