From 7935840b52e612506d7c63587c51760a4784a2ed Mon Sep 17 00:00:00 2001 From: Owen Voorhees Date: Thu, 4 Sep 2025 10:32:06 -0700 Subject: [PATCH] Add a mechanism to clear the global module cache at the end of a build for testing --- Sources/SWBBuildSystem/BuildOperation.swift | 28 ++++++++++++++++++++ Sources/SWBCore/Settings/BuiltinMacros.swift | 2 ++ Sources/SWBCore/Settings/Settings.swift | 5 ++++ 3 files changed, 35 insertions(+) diff --git a/Sources/SWBBuildSystem/BuildOperation.swift b/Sources/SWBBuildSystem/BuildOperation.swift index 3f2afc02..8bf3c230 100644 --- a/Sources/SWBBuildSystem/BuildOperation.swift +++ b/Sources/SWBBuildSystem/BuildOperation.swift @@ -1555,6 +1555,7 @@ internal final class OperationSystemAdaptor: SWBLLBuild.BuildSystemDelegate, Act func waitForCompletion(buildSucceeded: Bool) async { let completionToken = await dynamicOperationContext.waitForCompletion() cleanupCompilationCache() + cleanupGlobalModuleCache() await queue.sync { self.isCompleted = true @@ -1601,6 +1602,33 @@ internal final class OperationSystemAdaptor: SWBLLBuild.BuildSystemDelegate, Act } } + func cleanupGlobalModuleCache() { + let settings = operation.requestContext.getCachedSettings(operation.request.parameters) + if settings.globalScope.evaluate(BuiltinMacros.KEEP_GLOBAL_MODULE_CACHE_DIRECTORY) { + return // Keep the cache directory. + } + + let cachePath = settings.globalScope.evaluate(BuiltinMacros.MODULE_CACHE_DIR) + guard !cachePath.isEmpty, operation.fs.exists(cachePath) else { + return + } + + let signatureCtx = InsecureHashContext() + signatureCtx.add(string: "CleanupGlobalModuleCache") + signatureCtx.add(string: cachePath.str) + let signature = signatureCtx.signature + + withActivity(ruleInfo: "CleanupGlobalModuleCache \(cachePath.str)", executionDescription: "Cleanup global module cache at \(cachePath)", signature: signature, target: nil, parentActivity: nil) { activity in + do { + try operation.fs.removeDirectory(cachePath) + } catch { + // Log error but do not fail the build. + emit(diagnostic: Diagnostic.init(behavior: .warning, location: .unknown, data: DiagnosticData("Failed to remove \(cachePath): \(error.localizedDescription)")), for: activity, signature: signature) + } + return .succeeded + } + } + /// Get the active output delegate for an executing command. /// /// - returns: The active delegate, or nil if not found. diff --git a/Sources/SWBCore/Settings/BuiltinMacros.swift b/Sources/SWBCore/Settings/BuiltinMacros.swift index 2d3865cf..faba9ec0 100644 --- a/Sources/SWBCore/Settings/BuiltinMacros.swift +++ b/Sources/SWBCore/Settings/BuiltinMacros.swift @@ -547,6 +547,7 @@ public final class BuiltinMacros { public static let COMPILATION_CACHE_ENABLE_PLUGIN = BuiltinMacros.declareBooleanMacro("COMPILATION_CACHE_ENABLE_PLUGIN") public static let COMPILATION_CACHE_ENABLE_STRICT_CAS_ERRORS = BuiltinMacros.declareBooleanMacro("COMPILATION_CACHE_ENABLE_STRICT_CAS_ERRORS") public static let COMPILATION_CACHE_KEEP_CAS_DIRECTORY = BuiltinMacros.declareBooleanMacro("COMPILATION_CACHE_KEEP_CAS_DIRECTORY") + public static let KEEP_GLOBAL_MODULE_CACHE_DIRECTORY = BuiltinMacros.declareBooleanMacro("SCANNING_PCM_KEEP_CACHE_DIRECTORY") public static let COMPILATION_CACHE_CAS_PATH = BuiltinMacros.declareStringMacro("COMPILATION_CACHE_CAS_PATH") public static let COMPILATION_CACHE_LIMIT_PERCENT = BuiltinMacros.declareStringMacro("COMPILATION_CACHE_LIMIT_PERCENT") public static let COMPILATION_CACHE_LIMIT_SIZE = BuiltinMacros.declareStringMacro("COMPILATION_CACHE_LIMIT_SIZE") @@ -1551,6 +1552,7 @@ public final class BuiltinMacros { COMPILATION_CACHE_ENABLE_PLUGIN, COMPILATION_CACHE_ENABLE_STRICT_CAS_ERRORS, COMPILATION_CACHE_KEEP_CAS_DIRECTORY, + KEEP_GLOBAL_MODULE_CACHE_DIRECTORY, COMPILATION_CACHE_LIMIT_PERCENT, COMPILATION_CACHE_LIMIT_SIZE, COMPILATION_CACHE_CAS_PATH, diff --git a/Sources/SWBCore/Settings/Settings.swift b/Sources/SWBCore/Settings/Settings.swift index 1844fd71..fe9e2b87 100644 --- a/Sources/SWBCore/Settings/Settings.swift +++ b/Sources/SWBCore/Settings/Settings.swift @@ -1983,6 +1983,11 @@ private class SettingsBuilder { exportedMacroNames.formUnion(info.exportedMacros) errors.append(contentsOf:info.errors) + // Default to preserving the module cache directory. + pushTable(.exported) { + $0.push(BuiltinMacros.KEEP_GLOBAL_MODULE_CACHE_DIRECTORY, literal: true) + } + if self.parameters.action == .indexBuild { pushTable(.exported) { $0.push(BuiltinMacros.INDEX_ENABLE_BUILD_ARENA, literal: true)