|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SWBUtil |
| 14 | +import Foundation |
| 15 | + |
| 16 | +/// Process-level cache for computed target build graphs. |
| 17 | +/// |
| 18 | +/// Xcode issues multiple `TargetBuildGraph` requests per build action |
| 19 | +/// with different parameters (e.g. dependency graph vs actual build, |
| 20 | +/// index preparation vs normal build). A multi-entry cache ensures |
| 21 | +/// these don't evict each other. |
| 22 | +/// |
| 23 | +/// The cache is static (process-level) because `WorkspaceContext` is |
| 24 | +/// recreated on every PIF transfer, even when nothing has changed. |
| 25 | +public enum TargetBuildGraphCache { |
| 26 | + /// The data we cache — everything needed by the |
| 27 | + /// `TargetBuildGraph` memberwise init except the live context |
| 28 | + /// objects (workspaceContext, buildRequest, buildRequestContext). |
| 29 | + struct CachedTopology: @unchecked Sendable { |
| 30 | + let allTargets: OrderedSet<ConfiguredTarget> |
| 31 | + let targetDependencies: |
| 32 | + [ConfiguredTarget: [ResolvedTargetDependency]] |
| 33 | + let targetsToLinkedReferencesToProducingTargets: |
| 34 | + [ConfiguredTarget: |
| 35 | + [BuildFile.BuildableItem: ResolvedTargetDependency]] |
| 36 | + let dynamicallyBuildingTargets: Set<Target> |
| 37 | + } |
| 38 | + |
| 39 | + /// Maximum number of cached entries. Xcode typically issues 2-4 |
| 40 | + /// distinct graph requests per build action; 8 gives headroom. |
| 41 | + private static let maxEntries = 8 |
| 42 | + |
| 43 | + private static let _entries = |
| 44 | + SWBMutex<[Int: CachedTopology]>([:]) |
| 45 | + |
| 46 | + /// Look up a cached topology by signature. |
| 47 | + static func lookup(signature: Int) -> CachedTopology? { |
| 48 | + _entries.withLock { entries in |
| 49 | + entries[signature] |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// Store a computed topology for the given signature. |
| 54 | + static func store(signature: Int, topology: CachedTopology) { |
| 55 | + _entries.withLock { entries in |
| 56 | + // Evict all entries if we exceed the limit (simple reset |
| 57 | + // policy). This only happens when the PIF changes or the |
| 58 | + // user switches between different build configurations. |
| 59 | + if entries.count >= maxEntries { |
| 60 | + entries.removeAll() |
| 61 | + } |
| 62 | + entries[signature] = topology |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /// Compute a cache signature from the inputs that determine the |
| 67 | + /// dependency graph. |
| 68 | + /// |
| 69 | + /// The dependency graph is a pure function of the PIF structure |
| 70 | + /// and the build request parameters. File contents (source files, |
| 71 | + /// resources) do not affect which targets exist or how they depend |
| 72 | + /// on each other — only the PIF does. |
| 73 | + static func computeSignature( |
| 74 | + workspaceSignature: String, |
| 75 | + buildRequest: BuildRequest, |
| 76 | + purpose: TargetBuildGraph.Purpose |
| 77 | + ) -> Int { |
| 78 | + var hasher = Hasher() |
| 79 | + |
| 80 | + // Normalized PIF signature (strip volatile subobject GUIDs) |
| 81 | + if let range = workspaceSignature.range( |
| 82 | + of: "_subobjects=" |
| 83 | + ) { |
| 84 | + hasher.combine( |
| 85 | + workspaceSignature[..<range.lowerBound]) |
| 86 | + } else { |
| 87 | + hasher.combine(workspaceSignature) |
| 88 | + } |
| 89 | + |
| 90 | + // Global build parameters |
| 91 | + hasher.combine(buildRequest.parameters) |
| 92 | + |
| 93 | + // Top-level build targets and their per-target parameters. |
| 94 | + // Sort by target GUID for order independence. |
| 95 | + for targetInfo in buildRequest.buildTargets.sorted( |
| 96 | + by: { $0.target.guid < $1.target.guid } |
| 97 | + ) { |
| 98 | + hasher.combine(targetInfo.target.guid) |
| 99 | + hasher.combine(targetInfo.parameters) |
| 100 | + } |
| 101 | + |
| 102 | + // Flags that affect graph topology |
| 103 | + hasher.combine(buildRequest.useImplicitDependencies) |
| 104 | + hasher.combine(buildRequest.useParallelTargets) |
| 105 | + hasher.combine(buildRequest.skipDependencies) |
| 106 | + |
| 107 | + // Dependency scope affects pruning |
| 108 | + switch buildRequest.dependencyScope { |
| 109 | + case .workspace: |
| 110 | + hasher.combine(0) |
| 111 | + case .buildRequest: |
| 112 | + hasher.combine(1) |
| 113 | + } |
| 114 | + |
| 115 | + // Build command affects the early-return for |
| 116 | + // assembly/preprocessor |
| 117 | + switch buildRequest.buildCommand { |
| 118 | + case .build: |
| 119 | + hasher.combine("build") |
| 120 | + case .generateAssemblyCode: |
| 121 | + hasher.combine("asm") |
| 122 | + case .generatePreprocessedFile: |
| 123 | + hasher.combine("preprocess") |
| 124 | + case .singleFileBuild: |
| 125 | + hasher.combine("single") |
| 126 | + case .prepareForIndexing: |
| 127 | + hasher.combine("index") |
| 128 | + case .cleanBuildFolder: |
| 129 | + hasher.combine("clean") |
| 130 | + case .preview: |
| 131 | + hasher.combine("preview") |
| 132 | + } |
| 133 | + |
| 134 | + // Purpose affects diagnostic behavior |
| 135 | + switch purpose { |
| 136 | + case .build: |
| 137 | + hasher.combine("build") |
| 138 | + case .dependencyGraph: |
| 139 | + hasher.combine("depgraph") |
| 140 | + } |
| 141 | + |
| 142 | + return hasher.finalize() |
| 143 | + } |
| 144 | +} |
0 commit comments