|
| 1 | +//===-------------- ClangVersionedDependencyResolution.swift --------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Foundation |
| 14 | + |
| 15 | +/// A map from a module identifier to a set of module dependency graphs |
| 16 | +/// Used to compute distinct graphs corresponding to different target versions for a given clang module |
| 17 | +public typealias ModuleVersionedGraphMap = [ModuleDependencyId: [InterModuleDependencyGraph]] |
| 18 | + |
| 19 | +internal extension Driver { |
| 20 | + // Dependency scanning results may vary depending on the target version specified on the |
| 21 | + // dependency scanning action. If the scanning action is performed at a fixed target, and the |
| 22 | + // scanned module is later compiled with a higher version target, miscomputation may occur |
| 23 | + // due to dependencies present only at the higher version number and thus not detected by |
| 24 | + // the dependency scanner. We must ensure to re-scan Clang modules at all targets at which |
| 25 | + // they will be compiled and record a super-set of the module's dependencies at all targets. |
| 26 | + /// For each clang module, compute its dependencies at all targets at which it will be compiled. |
| 27 | + mutating func resolveVersionedClangDependencies(dependencyGraph: inout InterModuleDependencyGraph) |
| 28 | + throws { |
| 29 | + // Traverse the dependency graph, collecting extraPCMArgs along each path |
| 30 | + // to all Clang modules, and compute a set of distinct PCMArgs across all paths to a |
| 31 | + // given Clang module in the graph. |
| 32 | + let modulePCMArgsSetMap = try dependencyGraph.computePCMArgSetsForClangModules() |
| 33 | + var moduleVersionedGraphMap: [ModuleDependencyId: [InterModuleDependencyGraph]] = [:] |
| 34 | + for (moduleId, pcmArgSet) in modulePCMArgsSetMap { |
| 35 | + for pcmArgs in pcmArgSet { |
| 36 | + let pcmSpecificDepGraph = try scanClangModule(moduleId: moduleId, |
| 37 | + pcmArgs: pcmArgs) |
| 38 | + if moduleVersionedGraphMap[moduleId] != nil { |
| 39 | + moduleVersionedGraphMap[moduleId]!.append(pcmSpecificDepGraph) |
| 40 | + } else { |
| 41 | + moduleVersionedGraphMap[moduleId] = [pcmSpecificDepGraph] |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + try dependencyGraph.resolveVersionedClangModules(using: moduleVersionedGraphMap) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +private extension InterModuleDependencyGraph { |
| 51 | + /// For each module scanned at multiple target versions, combine their dependencies across version-specific graphs. |
| 52 | + mutating func resolveVersionedClangModules(using versionedGraphMap: ModuleVersionedGraphMap) |
| 53 | + throws { |
| 54 | + // Process each re-scanned module and its collection of graphs |
| 55 | + for (moduleId, graphList) in versionedGraphMap { |
| 56 | + for versionedGraph in graphList { |
| 57 | + // We must update dependencies for each module in the versioned graph, not just |
| 58 | + // the top-level re-scanned module itself. |
| 59 | + for rescannedModuleId in versionedGraph.modules.keys { |
| 60 | + guard let versionedModuleInfo = versionedGraph.modules[rescannedModuleId] else { |
| 61 | + throw Driver.Error.missingModuleDependency(moduleId.moduleName) |
| 62 | + } |
| 63 | + // If the main graph already contains this module, update its dependencies |
| 64 | + if var currentModuleInfo = modules[rescannedModuleId] { |
| 65 | + versionedModuleInfo.directDependencies?.forEach { dependencyId in |
| 66 | + // If a not-seen-before dependency has been found, add it to the info |
| 67 | + if !currentModuleInfo.directDependencies!.contains(dependencyId) { |
| 68 | + currentModuleInfo.directDependencies!.append(dependencyId) |
| 69 | + } |
| 70 | + } |
| 71 | + // Update the moduleInfo with the one whose dependencies consist of a super-set |
| 72 | + // of dependencies across all of the versioned dependency graphs |
| 73 | + modules[rescannedModuleId] = currentModuleInfo |
| 74 | + } else { |
| 75 | + // If the main graph does not yet contain this module, add it to the graph |
| 76 | + modules[rescannedModuleId] = versionedModuleInfo |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /// DFS from the main module to all clang modules, accumulating distinct |
| 84 | + /// PCMArgs along all paths to a given Clang module |
| 85 | + func computePCMArgSetsForClangModules() throws -> [ModuleDependencyId : Set<[String]>] { |
| 86 | + let mainModuleId: ModuleDependencyId = .swift(mainModuleName) |
| 87 | + var pcmArgSetMap: [ModuleDependencyId : Set<[String]>] = [:] |
| 88 | + |
| 89 | + func visit(_ moduleId: ModuleDependencyId, |
| 90 | + pathPCMArtSet: Set<[String]>, |
| 91 | + pcmArgSetMap: inout [ModuleDependencyId : Set<[String]>]) |
| 92 | + throws { |
| 93 | + switch moduleId { |
| 94 | + case .swift: |
| 95 | + // Add extraPCMArgs of the visited node to the current path set |
| 96 | + // and proceed to visit all direct dependencies |
| 97 | + let modulePCMArgs = try swiftModulePCMArgs(of: moduleId) |
| 98 | + var newPathPCMArgSet = pathPCMArtSet |
| 99 | + newPathPCMArgSet.insert(modulePCMArgs) |
| 100 | + for dependencyId in try moduleInfo(of: moduleId).directDependencies! { |
| 101 | + try visit(dependencyId, |
| 102 | + pathPCMArtSet: newPathPCMArgSet, |
| 103 | + pcmArgSetMap: &pcmArgSetMap) |
| 104 | + } |
| 105 | + case .clang: |
| 106 | + // Add current path's PCMArgs to the SetMap and stop traversal |
| 107 | + if pcmArgSetMap[moduleId] != nil { |
| 108 | + pathPCMArtSet.forEach { pcmArgSetMap[moduleId]!.insert($0) } |
| 109 | + } else { |
| 110 | + pcmArgSetMap[moduleId] = pathPCMArtSet |
| 111 | + } |
| 112 | + return |
| 113 | + case .swiftPlaceholder: |
| 114 | + fatalError("Unresolved placeholder dependencies at planning stage: \(moduleId)") |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + try visit(mainModuleId, |
| 119 | + pathPCMArtSet: [], |
| 120 | + pcmArgSetMap: &pcmArgSetMap) |
| 121 | + return pcmArgSetMap |
| 122 | + } |
| 123 | +} |
0 commit comments