Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Sources/SWBBuildSystem/BuildOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions Sources/SWBCore/Settings/BuiltinMacros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions Sources/SWBCore/Settings/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down