-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmissing_deps.go
209 lines (191 loc) · 6.33 KB
/
missing_deps.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
// Copyright 2019 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nin
import "fmt"
// MissingDependencyScannerDelegate is a callback when a missing dependency is
// found.
type MissingDependencyScannerDelegate interface {
OnMissingDep(node *Node, path string, generator *Rule)
}
// MissingDependencyScanner is a scanner for missing dependencies.
type MissingDependencyScanner struct {
delegate MissingDependencyScannerDelegate
depsLog *DepsLog
state *State
di DiskInterface
seen map[*Node]struct{}
nodesMissingDeps map[*Node]struct{}
generatedNodes map[*Node]struct{}
generatorRules map[*Rule]struct{}
missingDepPathCount int
adjacencyMap map[*Edge]map[*Edge]bool
}
// HadMissingDeps return true if there were any missing dependencies found.
func (m *MissingDependencyScanner) HadMissingDeps() bool {
return len(m.nodesMissingDeps) != 0
}
// ImplicitDepLoader variant that stores dep nodes into the given output
// without updating graph deps like the base loader does.
type nodeStoringImplicitDepLoader struct {
// TODO(maruel): This has to be a form of virtual function for
// processDepfileDeps. This is why
// TestBuildTest_DyndepBuildDiscoverNowWantEdgeAndDependent is failing.
implicitDepLoader
depNodesOutput []*Node
}
func newNodeStoringImplicitDepLoader(state *State, depsLog *DepsLog, di DiskInterface, depNodesOutput []*Node) nodeStoringImplicitDepLoader {
return nodeStoringImplicitDepLoader{
implicitDepLoader: newImplicitDepLoader(state, depsLog, di),
depNodesOutput: depNodesOutput,
}
}
func (n *nodeStoringImplicitDepLoader) processDepfileDeps(edge *Edge, depfileIns []string) bool {
for _, i := range depfileIns {
node := n.state.GetNode(CanonicalizePathBits(i))
n.depNodesOutput = append(n.depNodesOutput, node)
}
return true
}
//
// NewMissingDependencyScanner returns an initialized MissingDependencyScanner.
func NewMissingDependencyScanner(delegate MissingDependencyScannerDelegate, depsLog *DepsLog, state *State, di DiskInterface) MissingDependencyScanner {
return MissingDependencyScanner{
delegate: delegate,
depsLog: depsLog,
state: state,
di: di,
seen: map[*Node]struct{}{},
nodesMissingDeps: map[*Node]struct{}{},
generatedNodes: map[*Node]struct{}{},
generatorRules: map[*Rule]struct{}{},
adjacencyMap: map[*Edge]map[*Edge]bool{},
}
}
// ProcessNode does something?
//
// TODO(maruel): Figure out.
func (m *MissingDependencyScanner) ProcessNode(node *Node) {
if node == nil {
return
}
edge := node.InEdge
if edge == nil {
return
}
if _, ok := m.seen[node]; ok {
return
}
m.seen[node] = struct{}{}
for _, in := range edge.Inputs {
m.ProcessNode(in)
}
depsType := edge.GetBinding("deps")
if len(depsType) != 0 {
deps := m.depsLog.GetDeps(node)
if deps != nil {
m.processNodeDeps(node, deps.Nodes)
}
} else {
var depfileDeps []*Node
depLoader := newNodeStoringImplicitDepLoader(m.state, m.depsLog, m.di, depfileDeps)
_, _ = depLoader.loadDeps(edge)
if len(depfileDeps) != 0 {
m.processNodeDeps(node, depfileDeps)
}
}
}
func (m *MissingDependencyScanner) processNodeDeps(node *Node, depNodes []*Node) {
edge := node.InEdge
deplogEdges := map[*Edge]struct{}{}
for i := 0; i < len(depNodes); i++ {
deplogNode := depNodes[i]
// Special exception: A dep on build.ninja can be used to mean "always
// rebuild this target when the build is reconfigured", but build.ninja is
// often generated by a configuration tool like cmake or gn. The rest of
// the build "implicitly" depends on the entire build being reconfigured,
// so a missing dep path to build.ninja is not an actual missing dependency
// problem.
if deplogNode.Path == "build.ninja" {
return
}
deplogEdge := deplogNode.InEdge
if deplogEdge != nil {
deplogEdges[deplogEdge] = struct{}{}
}
}
var missingDeps []*Edge
for de := range deplogEdges {
if !m.pathExistsBetween(de, edge) {
missingDeps = append(missingDeps, de)
}
}
if len(missingDeps) != 0 {
missingDepsRuleNames := map[string]struct{}{}
for _, ne := range missingDeps {
if ne == nil {
panic("M-A")
}
for i := 0; i < len(depNodes); i++ {
if depNodes[i].InEdge == nil {
panic("M-A")
}
if m.delegate == nil {
panic("M-A")
}
if depNodes[i].InEdge == ne {
m.generatedNodes[depNodes[i]] = struct{}{}
m.generatorRules[ne.Rule] = struct{}{}
missingDepsRuleNames[ne.Rule.Name] = struct{}{}
m.delegate.OnMissingDep(node, depNodes[i].Path, ne.Rule)
}
}
}
m.missingDepPathCount += len(missingDepsRuleNames)
m.nodesMissingDeps[node] = struct{}{}
}
}
// PrintStats prints statistics to stdout.
func (m *MissingDependencyScanner) PrintStats() {
fmt.Printf("Processed %d nodes.\n", len(m.seen))
if m.HadMissingDeps() {
fmt.Printf("Error: There are %d missing dependency paths.\n", m.missingDepPathCount)
fmt.Printf("%d targets had depfile dependencies on %d distinct generated inputs (from %d rules) without a non-depfile dep path to the generator.\n",
len(m.nodesMissingDeps), len(m.generatedNodes), len(m.generatorRules))
fmt.Printf("There might be build flakiness if any of the targets listed above are built alone, or not late enough, in a clean output directory.\n")
} else {
fmt.Printf("No missing dependencies on generated files found.\n")
}
}
func (m *MissingDependencyScanner) pathExistsBetween(from *Edge, to *Edge) bool {
it, ok := m.adjacencyMap[from]
if ok {
innerIt, ok := it[to]
if ok {
return innerIt
}
} else {
it = map[*Edge]bool{}
m.adjacencyMap[from] = it
}
found := false
for i := 0; i < len(to.Inputs); i++ {
e := to.Inputs[i].InEdge
if e != nil && (e == from || m.pathExistsBetween(from, e)) {
found = true
break
}
}
it[to] = found
return found
}