| 1 | // iterative depth-first ordering follows pacman lib/libalpm/deps.c. |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | module engine |
| 4 | |
| 5 | pub struct DependencyCycle { |
| 6 | pub: |
| 7 | package &Package |
| 8 | dependency &Package |
| 9 | } |
| 10 | |
| 11 | pub struct DependencyOrder { |
| 12 | pub: |
| 13 | packages []&Package |
| 14 | cycles []DependencyCycle |
| 15 | } |
| 16 | |
| 17 | enum DependencyVertexState { |
| 18 | unprocessed |
| 19 | processing |
| 20 | processed |
| 21 | } |
| 22 | |
| 23 | struct DependencyVertex { |
| 24 | pkg &Package |
| 25 | mut: |
| 26 | end_child int |
| 27 | parent int = -1 |
| 28 | next_child int |
| 29 | state DependencyVertexState |
| 30 | } |
| 31 | |
| 32 | // Installed intermediaries are discovered after the target vertices. Keep |
| 33 | // their assigned positions so each row can recreate the original edge order. |
| 34 | fn link_loaded_dependencies(known_packages []&Package, local []&Package, mut vertices []DependencyVertex, mut edges []int) { |
| 35 | index := dependency_index(known_packages, local, true) |
| 36 | mut local_vertex := []int{len: local.len} |
| 37 | mut matches := []int{} |
| 38 | for row := 1; row < vertices.len; row++ { |
| 39 | pkg := vertices[row].pkg |
| 40 | if pkg.owner != unsafe { nil } { |
| 41 | mut owner := pkg.owner |
| 42 | owner.check() or { continue } |
| 43 | } |
| 44 | vertices[row].next_child = edges.len |
| 45 | matches.clear() |
| 46 | for dep in pkg.depends { |
| 47 | for link := index.head(dep.name()); link != 0; { |
| 48 | entry := index.links[link - 1] |
| 49 | link = entry.next |
| 50 | slot := entry.position |
| 51 | if !index.package_at(slot).satisfies(dep) { continue } |
| 52 | if slot < known_packages.len { |
| 53 | matches << slot |
| 54 | } else { |
| 55 | offset := slot - known_packages.len |
| 56 | known := local_vertex[offset] |
| 57 | matches << if known > 0 { |
| 58 | known |
| 59 | } else { |
| 60 | known_packages.len + local.len + offset |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | matches.sort() |
| 66 | mut previous := -1 |
| 67 | for match_position in matches { |
| 68 | if previous == match_position { continue } |
| 69 | previous = match_position |
| 70 | if match_position < known_packages.len + local.len { |
| 71 | edges << match_position |
| 72 | } else { |
| 73 | offset := match_position - known_packages.len - local.len |
| 74 | local_vertex[offset] = vertices.len |
| 75 | edges << vertices.len |
| 76 | vertices << DependencyVertex{ pkg: local[offset] } |
| 77 | } |
| 78 | } |
| 79 | vertices[row].end_child = edges.len |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | fn dependency_vertices(targets []&Package, installed []&Package, ignore []&Package, mut edges []int) []DependencyVertex { |
| 84 | if targets.len == 0 { return [] } |
| 85 | mut local := []&Package{} |
| 86 | if installed.len > 0 { |
| 87 | excluded := dependency_index(targets, ignore, false) |
| 88 | local = installed.filter(!excluded.has_name(it.name)) |
| 89 | } |
| 90 | mut vertices := []DependencyVertex{cap: targets.len} |
| 91 | for pkg in targets { vertices << DependencyVertex{ pkg: pkg } } |
| 92 | for index := 0; index < vertices.len; index++ { |
| 93 | // The first scan reads lazy metadata in the original order. Once those |
| 94 | // reads succeeded, the remaining rows can use an index without changing |
| 95 | // callbacks or diagnostics. A single leaf never needs an index. |
| 96 | if index == 1 && vertices.all(it.pkg.desc_loaded) && local.all(it.desc_loaded) { |
| 97 | known := if vertices.len == targets.len { targets } else { vertices.map(it.pkg) } |
| 98 | link_loaded_dependencies(known, local, mut vertices, mut edges) |
| 99 | break |
| 100 | } |
| 101 | pkg := vertices[index].pkg |
| 102 | vertices[index].next_child = edges.len |
| 103 | for child := 0; child < vertices.len; child++ { |
| 104 | if depends_on(pkg, vertices[child].pkg) { |
| 105 | edges << child |
| 106 | } |
| 107 | } |
| 108 | // installed intermediaries can express ordering between targets without |
| 109 | // becoming transaction targets themselves. each is added only once. |
| 110 | mut kept := 0 |
| 111 | for i, candidate in local { |
| 112 | if depends_on(pkg, candidate) { |
| 113 | edges << vertices.len |
| 114 | vertices << DependencyVertex{ pkg: candidate } |
| 115 | } else { |
| 116 | if kept != i { local[kept] = candidate } |
| 117 | kept++ |
| 118 | } |
| 119 | } |
| 120 | local.trim(kept) |
| 121 | vertices[index].end_child = edges.len |
| 122 | } |
| 123 | return vertices |
| 124 | } |
| 125 | |
| 126 | pub fn order_dependencies(targets []&Package, installed []&Package, ignore []&Package, reverse bool) DependencyOrder { |
| 127 | mut edges := []int{cap: targets.len} |
| 128 | mut vertices := dependency_vertices(targets, installed, ignore, mut edges) |
| 129 | mut result := []&Package{cap: targets.len} |
| 130 | mut cycles := []DependencyCycle{} |
| 131 | for top in 0 .. vertices.len { |
| 132 | if vertices[top].state != .unprocessed { continue } |
| 133 | mut current := top |
| 134 | for current >= 0 { |
| 135 | vertices[current].state = .processing |
| 136 | mut descend := false |
| 137 | for vertices[current].next_child < vertices[current].end_child { |
| 138 | child := edges[vertices[current].next_child] |
| 139 | vertices[current].next_child++ |
| 140 | if vertices[child].state == .unprocessed { |
| 141 | vertices[child].parent = current |
| 142 | current = child |
| 143 | descend = true |
| 144 | break |
| 145 | } |
| 146 | // Targets occupy the initial vertices; installed intermediaries are |
| 147 | // appended later, so membership needs no scan of the target list. |
| 148 | if vertices[child].state == .processing && child < targets.len { |
| 149 | mut ancestor := current |
| 150 | for ancestor >= targets.len { |
| 151 | ancestor = vertices[ancestor].parent |
| 152 | } |
| 153 | if ancestor >= 0 && ancestor != child { |
| 154 | cycles << DependencyCycle{ package: vertices[ancestor].pkg, dependency: vertices[child].pkg } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | if !descend { |
| 159 | if current < targets.len { |
| 160 | result << vertices[current].pkg |
| 161 | } |
| 162 | vertices[current].state = .processed |
| 163 | current = vertices[current].parent |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | if reverse { result.reverse_in_place() } |
| 168 | return DependencyOrder{ packages: result, cycles: cycles } |
| 169 | } |
| 170 | |