-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.go
1066 lines (949 loc) · 29.7 KB
/
graph.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2011 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 (
"bytes"
"errors"
"fmt"
"os"
"runtime"
"sort"
)
// ExistenceStatus represents the knowledge of the file's existence.
type ExistenceStatus int32
const (
// ExistenceStatusUnknown means the file hasn't been examined.
ExistenceStatusUnknown ExistenceStatus = iota
// ExistenceStatusMissing means the file doesn't exist. MTime will be the
// latest mtime of its dependencies.
ExistenceStatusMissing
// ExistenceStatusExists means the path is an actual file. MTime will be the
// file's mtime.
ExistenceStatusExists
)
// Node represents information about a node in the dependency graph: the file,
// whether it's dirty, mtime, etc.
type Node struct {
// Immutable.
// Path is the path of the file that this node represents.
Path string
// Set bits starting from lowest for backslashes that were normalized to
// forward slashes by CanonicalizePathBits. See |PathDecanonicalized|.
SlashBits uint64
// Mutable.
// The Edge that produces this Node, or NULL when there is no
// known edge to produce it.
InEdge *Edge
// All Edges that use this Node as an input.
OutEdges []*Edge
// All Edges that use this Node as a validation.
ValidationOutEdges []*Edge
// Possible values of MTime:
// -1: file hasn't been examined
// 0: we looked, and file doesn't exist
// >0: actual file's mtime, or the latest mtime of its dependencies if it doesn't exist
MTime TimeStamp
// A dense integer id for the node, assigned and used by DepsLog.
ID int32
Exists ExistenceStatus
// Dirty is true when the underlying file is out-of-date.
// But note that Edge.OutputsReady is also used in judging which
// edges to build.
Dirty bool
// Store whether dyndep information is expected from this node but
// has not yet been loaded.
DyndepPending bool
}
func (n *Node) statIfNecessary(di DiskInterface) error {
if n.Exists != ExistenceStatusUnknown {
return nil
}
return n.Stat(di)
}
// PathDecanonicalized return |Path| but use SlashBits to convert back to
// original slash styles.
func (n *Node) PathDecanonicalized() string {
return PathDecanonicalized(n.Path, n.SlashBits)
}
// Stat stat's the file.
func (n *Node) Stat(di DiskInterface) error {
defer metricRecord("node stat")()
mtime, err := di.Stat(n.Path)
n.MTime = mtime
if mtime == -1 {
return err
}
if n.MTime != 0 {
n.Exists = ExistenceStatusExists
} else {
n.Exists = ExistenceStatusMissing
}
return nil
}
// If the file doesn't exist, set the MTime from its dependencies
func (n *Node) updatePhonyMtime(mtime TimeStamp) {
if n.Exists != ExistenceStatusExists {
if mtime > n.MTime {
n.MTime = mtime
}
}
}
// Dump prints out Node's details to stdout.
func (n *Node) Dump(prefix string) {
s := ""
if n.Exists != ExistenceStatusExists {
s = " (:missing)"
}
t := " clean"
if n.Dirty {
t = " dirty"
}
fmt.Printf("%s <%s 0x%p> mtime: %x%s, (:%s), ", prefix, n.Path, n, n.MTime, s, t)
if n.InEdge != nil {
n.InEdge.Dump("in-edge: ")
} else {
fmt.Printf("no in-edge\n")
}
fmt.Printf(" out edges:\n")
for _, e := range n.OutEdges {
if e == nil {
break
}
e.Dump(" +- ")
}
if len(n.ValidationOutEdges) != 0 {
fmt.Printf(" validation out edges:\n")
for _, e := range n.ValidationOutEdges {
e.Dump(" +- ")
}
}
}
//
// VisitMark is a market to determine if an edge is visited.
type VisitMark int32
// Valid VisitMark values.
const (
VisitNone VisitMark = iota
VisitInStack
VisitDone
)
// Edge is an edge in the dependency graph; links between Nodes using Rules.
type Edge struct {
Inputs []*Node
Outputs []*Node
Validations []*Node
Rule *Rule
Pool *Pool
Dyndep *Node
Env *BindingEnv
Mark VisitMark
ID int32
// There are three types of inputs.
// 1) explicit deps, which show up as $in on the command line;
// 2) implicit deps, which the target depends on implicitly (e.g. C headers),
// and changes in them cause the target to rebuild;
// 3) order-only deps, which are needed before the target builds but which
// don't cause the target to rebuild.
// These are stored in Inputs in that order, and we keep counts of
// #2 and #3 when we need to access the various subsets.
ImplicitDeps int32
OrderOnlyDeps int32
// There are two types of outputs.
// 1) explicit outs, which show up as $out on the command line;
// 2) implicit outs, which the target generates but are not part of $out.
// These are stored in Outputs in that order, and we keep a count of
// #2 to use when we need to access the various subsets.
ImplicitOuts int32
OutputsReady bool
DepsLoaded bool
DepsMissing bool
GeneratedByDepLoader bool
}
// If this ever gets changed, update DelayedEdgesSet to take this into account.
func (e *Edge) weight() int {
return 1
}
// IsImplicit returns if the inputs at the specified index is implicit and not
// for ordering only.
func (e *Edge) IsImplicit(index int) bool {
return index >= len(e.Inputs)-int(e.OrderOnlyDeps)-int(e.ImplicitDeps) && !e.IsOrderOnly(index)
}
// IsOrderOnly returns if the input at the specified index is only used for
// ordering.
func (e *Edge) IsOrderOnly(index int) bool {
return index >= len(e.Inputs)-int(e.OrderOnlyDeps)
}
// isImplicitOut is only used in unit tests.
func (e *Edge) isImplicitOut(index int) bool {
return index >= len(e.Outputs)-int(e.ImplicitOuts)
}
// EvaluateCommand expands all variables in a command and return it as a string.
//
// If inclRspFile is enabled, the string will also contain the
// full contents of a response file (if applicable)
func (e *Edge) EvaluateCommand(inclRspFile bool) string {
command := e.GetBinding("command")
if inclRspFile {
rspfileContent := e.GetBinding("rspfile_content")
if rspfileContent != "" {
command += ";rspfile=" + rspfileContent
}
}
return command
}
// GetBinding returns the shell-escaped value of |key|.
func (e *Edge) GetBinding(key string) string {
env := edgeEnv{
edge: e,
escapeInOut: shellEscape,
}
return env.LookupVariable(key)
}
// GetUnescapedDepfile returns like GetBinding("depfile"), but without shell
// escaping.
func (e *Edge) GetUnescapedDepfile() string {
env := edgeEnv{
edge: e,
escapeInOut: doNotEscape,
}
return env.LookupVariable("depfile")
}
// GetUnescapedDyndep returns like GetBinding("dyndep"), but without shell
// escaping.
func (e *Edge) GetUnescapedDyndep() string {
env := edgeEnv{
edge: e,
escapeInOut: doNotEscape,
}
return env.LookupVariable("dyndep")
}
// GetUnescapedRspfile returns like GetBinding("rspfile"), but without shell
// escaping.
func (e *Edge) GetUnescapedRspfile() string {
env := edgeEnv{
edge: e,
escapeInOut: doNotEscape,
}
return env.LookupVariable("rspfile")
}
// Dump prints the Edge details to stdout.
func (e *Edge) Dump(prefix string) {
fmt.Printf("%s[ ", prefix)
for _, i := range e.Inputs {
if i != nil {
fmt.Printf("%s ", i.Path)
}
}
fmt.Printf("--%s-> ", e.Rule.Name)
for _, i := range e.Outputs {
fmt.Printf("%s ", i.Path)
}
if len(e.Validations) != 0 {
fmt.Printf(" validations ")
for _, i := range e.Validations {
fmt.Printf("%s ", i.Path)
}
}
if e.Pool != nil {
if e.Pool.Name != "" {
fmt.Printf("(in pool '%s')", e.Pool.Name)
}
} else {
fmt.Printf("(null pool?)")
}
fmt.Printf("] 0x%p\n", e)
}
func (e *Edge) maybePhonycycleDiagnostic() bool {
// CMake 2.8.12.x and 3.0.x produced self-referencing phony rules
// of the form "build a: phony ... a ...". Restrict our
// "phonycycle" diagnostic option to the form it used.
return e.Rule == PhonyRule && len(e.Outputs) == 1 && e.ImplicitOuts == 0 && e.ImplicitDeps == 0
}
// Return true if all inputs' in-edges are ready.
func (e *Edge) allInputsReady() bool {
for _, i := range e.Inputs {
if i.InEdge != nil && !i.InEdge.OutputsReady {
return false
}
}
return true
}
//
// EdgeSet acts as a sorted set of *Edge, so map[*Edge]struct{} but with sorted
// pop.
type EdgeSet struct {
edges map[*Edge]struct{}
dirty bool
sorted []*Edge
}
// NewEdgeSet returns an initialized EdgeSet.
func NewEdgeSet() *EdgeSet {
return &EdgeSet{
edges: make(map[*Edge]struct{}),
}
}
// IsEmpty return true if the set is empty.
func (e *EdgeSet) IsEmpty() bool {
return len(e.edges) == 0
}
// Add the edge to the set.
func (e *EdgeSet) Add(ed *Edge) {
e.edges[ed] = struct{}{}
e.dirty = true
}
// Pop returns the lowest ID.
func (e *EdgeSet) Pop() *Edge {
e.recreate()
if len(e.sorted) == 0 {
return nil
}
// Do not set dirty.
ed := e.sorted[len(e.sorted)-1]
e.sorted = e.sorted[:len(e.sorted)-1]
delete(e.edges, ed)
return ed
}
func (e *EdgeSet) recreate() {
if !e.dirty {
return
}
e.dirty = false
if len(e.edges) == 0 {
if len(e.sorted) != 0 {
e.sorted = e.sorted[:0]
}
return
}
// Resize e.sorted to be the same size as e.edges
le := len(e.edges)
if cap(e.sorted) < le {
e.sorted = make([]*Edge, le)
} else {
delta := le - len(e.sorted)
if delta < 0 {
// TODO(maruel): Not sure how to tell the Go compiler to do it as a
// single operation.
for i := 0; i < delta; i++ {
e.sorted = append(e.sorted, nil)
}
} else if delta > 0 {
e.sorted = e.sorted[:le]
}
}
i := 0
for k := range e.edges {
e.sorted[i] = k
i++
}
// Sort in reverse order, so that Pop() removes the last (smallest) item.
sort.Slice(e.sorted, func(i, j int) bool {
return e.sorted[i].ID > e.sorted[j].ID
})
}
//
type escapeKind bool
const (
shellEscape escapeKind = false
doNotEscape escapeKind = true
)
// An Env for an Edge, providing $in and $out.
type edgeEnv struct {
lookups []string
edge *Edge
escapeInOut escapeKind
recursive bool
}
func (e *edgeEnv) LookupVariable(v string) string {
edge := e.edge
switch v {
case "in":
explicitDepsCount := len(edge.Inputs) - int(edge.ImplicitDeps) - int(edge.OrderOnlyDeps)
return makePathList(edge.Inputs[:explicitDepsCount], ' ', e.escapeInOut)
case "in_newline":
explicitDepsCount := len(edge.Inputs) - int(edge.ImplicitDeps) - int(edge.OrderOnlyDeps)
return makePathList(edge.Inputs[:explicitDepsCount], '\n', e.escapeInOut)
case "out":
explicitOutsCount := len(edge.Outputs) - int(edge.ImplicitOuts)
return makePathList(edge.Outputs[:explicitOutsCount], ' ', e.escapeInOut)
default:
// TODO(maruel): Remove here and move to a post parsing evaluation in a
// separate goroutine.
for i := 0; i < len(e.lookups); i++ {
if e.lookups[i] == v {
cycle := ""
for ; i < len(e.lookups); i++ {
cycle += e.lookups[i] + " -> "
}
cycle += v
fatalf("cycle in rule variables: " + cycle)
}
}
// See notes on BindingEnv.lookupWithFallback.
eval := edge.Rule.Bindings[v]
if e.recursive {
if eval != nil {
e.lookups = append(e.lookups, v)
}
} else {
// In practice, variables defined on rules never use another rule variable.
e.recursive = true
}
return edge.Env.lookupWithFallback(v, eval, e)
}
}
// Given a span of Nodes, construct a list of paths suitable for a command
// line.
func makePathList(span []*Node, sep byte, escapeInOut escapeKind) string {
var z [64]string
var s []string
if l := len(span); l <= cap(z) {
s = z[:l]
} else {
s = make([]string, l)
}
total := 0
first := false
for i, x := range span {
path := x.PathDecanonicalized()
if escapeInOut == shellEscape {
if runtime.GOOS == "windows" {
path = getWin32EscapedString(path)
} else {
path = getShellEscapedString(path)
}
}
l := len(path)
if !first {
if l != 0 {
first = true
}
} else {
// For the separator.
total++
}
s[i] = path
total += l
}
out := make([]byte, total)
offset := 0
for _, x := range s {
if offset != 0 {
out[offset] = sep
offset++
}
copy(out[offset:], x)
offset += len(x)
}
return unsafeString(out)
}
// PathDecanonicalized does the reverse process of CanonicalizePath().
//
// Only does anything on Windows.
func PathDecanonicalized(path string, slashBits uint64) string {
if runtime.GOOS != "windows" {
return path
}
result := []byte(path)
mask := uint64(1)
for c := 0; ; c++ {
d := bytes.IndexByte(result[c:], '/')
if d == -1 {
break
}
c += d
if slashBits&mask != 0 {
result[c] = '\\'
}
mask <<= 1
}
return unsafeString(result)
}
//
// DependencyScan manages the process of scanning the files in a graph
// and updating the dirty/outputsReady state of all the nodes and edges.
type DependencyScan struct {
buildLog *BuildLog
di DiskInterface
depLoader implicitDepLoader
dyndepLoader DyndepLoader
}
// NewDependencyScan returns an initialized DependencyScan.
func NewDependencyScan(state *State, buildLog *BuildLog, depsLog *DepsLog, di DiskInterface) DependencyScan {
return DependencyScan{
buildLog: buildLog,
di: di,
depLoader: newImplicitDepLoader(state, depsLog, di),
dyndepLoader: NewDyndepLoader(state, di),
}
}
func (d *DependencyScan) depsLog() *DepsLog {
return d.depLoader.depsLog
}
// RecomputeDirty updates the |dirty| state of the given Node by transitively
// inspecting their input edges.
//
// Examine inputs, outputs, and command lines to judge whether an edge
// needs to be re-run, and update OutputsReady and each outputs' Dirty
// state accordingly.
//
// Appends any validation nodes found to the nodes parameter.
func (d *DependencyScan) RecomputeDirty(initialNode *Node) ([]*Node, error) {
var stack, validationNodes, newValidationNodes []*Node
// The C++ code uses a dequeue.
nodes := []*Node{initialNode}
// recomputeNodeDirty might return new validation nodes that need to be
// checked for dirty state, keep a queue of nodes to visit.
for len(nodes) != 0 {
node := nodes[0]
nodes = nodes[1:]
// Reuse slices to reduce overall memory allocations.
stack = stack[:0]
newValidationNodes = newValidationNodes[:0]
var err error
stack, newValidationNodes, err = d.recomputeNodeDirty(node, stack, newValidationNodes)
if err != nil {
return nil, err
}
nodes = append(nodes, newValidationNodes...)
validationNodes = append(validationNodes, newValidationNodes...)
}
return validationNodes, nil
}
// recomputeNodeDirty updates Node.Dirty.
//
// It is recursive.
func (d *DependencyScan) recomputeNodeDirty(node *Node, stack, validationNodes []*Node) ([]*Node, []*Node, error) {
edge := node.InEdge
if edge == nil {
// If we already visited this leaf node then we are done.
if node.Exists != ExistenceStatusUnknown {
return stack, validationNodes, nil
}
// This node has no in-edge; it is dirty if it is missing.
if err := node.statIfNecessary(d.di); err != nil {
return stack, validationNodes, err
}
if node.Exists != ExistenceStatusExists {
explain("%s has no in-edge and is missing", node.Path)
}
node.Dirty = node.Exists != ExistenceStatusExists
return stack, validationNodes, nil
}
// If we already finished this edge then we are done.
if edge.Mark == VisitDone {
return stack, validationNodes, nil
}
// If we encountered this edge earlier in the call stack we have a cycle.
if err := d.verifyDAG(node, stack); err != nil {
return stack, validationNodes, err
}
// Mark the edge temporarily while in the call stack.
edge.Mark = VisitInStack
stack = append(stack, node)
dirty := false
edge.OutputsReady = true
edge.DepsMissing = false
if !edge.DepsLoaded {
// This is our first encounter with this edge.
// If there is a pending dyndep file, visit it now:
// * If the dyndep file is ready then load it now to get any
// additional inputs and outputs for this and other edges.
// Once the dyndep file is loaded it will no longer be pending
// if any other edges encounter it, but they will already have
// been updated.
// * If the dyndep file is not ready then since is known to be an
// input to this edge, the edge will not be considered ready below.
// Later during the build the dyndep file will become ready and be
// loaded to update this edge before it can possibly be scheduled.
if edge.Dyndep != nil && edge.Dyndep.DyndepPending {
var err error
stack, validationNodes, err = d.recomputeNodeDirty(edge.Dyndep, stack, validationNodes)
if err != nil {
return stack, validationNodes, err
}
if edge.Dyndep.InEdge == nil || edge.Dyndep.InEdge.OutputsReady {
// The dyndep file is ready, so load it now.
if err := d.LoadDyndeps(edge.Dyndep, DyndepFile{}); err != nil {
return stack, validationNodes, err
}
}
}
}
// Load output mtimes so we can compare them to the most recent input below.
for _, o := range edge.Outputs {
if err := o.statIfNecessary(d.di); err != nil {
return stack, validationNodes, err
}
}
if !edge.DepsLoaded {
// This is our first encounter with this edge. Load discovered deps.
edge.DepsLoaded = true
if found, err := d.depLoader.loadDeps(edge); err != nil {
return stack, validationNodes, err
} else if !found {
// Failed to load dependency info: rebuild to regenerate it.
// loadDeps() did Explain() already, no need to do it here.
dirty = true
edge.DepsMissing = true
}
}
// Store any validation nodes from the edge for adding to the initial
// nodes. Don't recurse into them, that would trigger the dependency
// cycle detector if the validation node depends on this node.
// RecomputeDirty will add the validation nodes to the initial nodes
// and recurse into them.
validationNodes = append(validationNodes, edge.Validations...)
// Visit all inputs; we're dirty if any of the inputs are dirty.
var mostRecentInput *Node
for j, i := range edge.Inputs {
// Visit this input.
var err error
stack, validationNodes, err = d.recomputeNodeDirty(i, stack, validationNodes)
if err != nil {
return stack, validationNodes, err
}
// If an input is not ready, neither are our outputs.
if inEdge := i.InEdge; inEdge != nil {
if !inEdge.OutputsReady {
edge.OutputsReady = false
}
}
if !edge.IsOrderOnly(j) {
// If a regular input is dirty (or missing), we're dirty.
// Otherwise consider mtime.
if i.Dirty {
explain("%s is dirty", i.Path)
dirty = true
} else {
if mostRecentInput == nil || i.MTime > mostRecentInput.MTime {
mostRecentInput = i
}
}
}
}
// We may also be dirty due to output state: missing outputs, out of
// date outputs, etc. Visit all outputs and determine whether they're dirty.
if !dirty {
// The C++ code conditions on this but I think there's a bug in there.
dirty = d.recomputeOutputsDirty(edge, mostRecentInput)
}
// Finally, visit each output and update their dirty state if necessary.
for _, o := range edge.Outputs {
if dirty {
o.Dirty = true
}
}
// If an edge is dirty, its outputs are normally not ready. (It's
// possible to be clean but still not be ready in the presence of
// order-only inputs.)
// But phony edges with no inputs have nothing to do, so are always
// ready.
if dirty && !(edge.Rule == PhonyRule && len(edge.Inputs) == 0) {
edge.OutputsReady = false
}
// Mark the edge as finished during this walk now that it will no longer
// be in the call stack.
edge.Mark = VisitDone
// assert(stack[len(stack)-1] == node)
return stack[:len(stack)-1], validationNodes, nil
}
// verifyDAG checks that the node is a directed acyclic graph.
//
// Mutates stack in-place in case of error.
func (d *DependencyScan) verifyDAG(node *Node, stack []*Node) error {
edge := node.InEdge
// If we have no temporary mark on the edge then we do not yet have a cycle.
if edge.Mark != VisitInStack {
return nil
}
// We have this edge earlier in the call stack. Find it.
start := -1
for i := range stack {
if stack[i].InEdge == edge {
start = i
break
}
}
// Make the cycle clear by reporting its start as the node at its end
// instead of some other output of the starting edge. For example,
// running 'ninja b' on
// build a b: cat c
// build c: cat a
// should report a -> c -> a instead of b -> c -> a.
stack[start] = node
// Construct the error message rejecting the cycle.
err := "dependency cycle: "
for i := start; i != len(stack); i++ {
err += stack[i].Path
err += " -> "
}
err += stack[start].Path
if (start+1) == len(stack) && edge.maybePhonycycleDiagnostic() {
// The manifest parser would have filtered out the self-referencing
// input if it were not configured to allow the error.
err += " [-w phonycycle=err]"
}
return errors.New(err)
}
// recomputeOutputsDirty recomputes whether any output of the edge is dirty.
//
// Returns true if dirty.
func (d *DependencyScan) recomputeOutputsDirty(edge *Edge, mostRecentInput *Node) bool {
command := edge.EvaluateCommand(true) // inclRspFile=
for _, o := range edge.Outputs {
if d.recomputeOutputDirty(edge, mostRecentInput, command, o) {
return true
}
}
return false
}
// recomputeOutputDirty recomputes whether a given single output should be
// marked dirty.
//
// Returns true if so.
func (d *DependencyScan) recomputeOutputDirty(edge *Edge, mostRecentInput *Node, command string, output *Node) bool {
if edge.Rule == PhonyRule {
// Phony edges don't write any output. Outputs are only dirty if
// there are no inputs and we're missing the output.
if len(edge.Inputs) == 0 && output.Exists != ExistenceStatusExists {
explain("output %s of phony edge with no inputs doesn't exist", output.Path)
return true
}
// Update the mtime with the newest input. Dependents can thus call mtime()
// on the fake node and get the latest mtime of the dependencies
if mostRecentInput != nil {
output.updatePhonyMtime(mostRecentInput.MTime)
}
// Phony edges are clean, nothing to do.
return false
}
var entry *LogEntry
// Dirty if we're missing the output.
if output.Exists != ExistenceStatusExists {
explain("output %s doesn't exist", output.Path)
return true
}
// Dirty if the output is older than the input.
if mostRecentInput != nil && output.MTime < mostRecentInput.MTime {
outputMtime := output.MTime
// If this is a restat rule, we may have cleaned the output with a restat
// rule in a previous run and stored the most recent input mtime in the
// build log. Use that mtime instead, so that the file will only be
// considered dirty if an input was modified since the previous run.
usedRestat := false
if edge.GetBinding("restat") != "" && d.buildLog != nil {
if entry = d.buildLog.Entries[output.Path]; entry != nil {
outputMtime = entry.mtime
usedRestat = true
}
}
if outputMtime < mostRecentInput.MTime {
s := ""
if usedRestat {
s = "restat of "
}
explain("%soutput %s older than most recent input %s (%x vs %x)", s, output.Path, mostRecentInput.Path, outputMtime, mostRecentInput.MTime)
return true
}
}
if d.buildLog != nil {
generator := edge.GetBinding("generator") != ""
if entry == nil {
entry = d.buildLog.Entries[output.Path]
}
if entry != nil {
if !generator && HashCommand(command) != entry.commandHash {
// May also be dirty due to the command changing since the last build.
// But if this is a generator rule, the command changing does not make us
// dirty.
explain("command line changed for %s", output.Path)
return true
}
if mostRecentInput != nil && entry.mtime < mostRecentInput.MTime {
// May also be dirty due to the mtime in the log being older than the
// mtime of the most recent input. This can occur even when the mtime
// on disk is newer if a previous run wrote to the output file but
// exited with an error or was interrupted.
explain("recorded mtime of %s older than most recent input %s (%x vs %x)", output.Path, mostRecentInput.Path, entry.mtime, mostRecentInput.MTime)
return true
}
}
if entry == nil && !generator {
explain("command line not found in log for %s", output.Path)
return true
}
}
return false
}
// LoadDyndeps loads a dyndep file from the given node's path and update the
// build graph with the new information.
//
// The 'DyndepFile' object stores the information loaded from the dyndep file.
func (d *DependencyScan) LoadDyndeps(node *Node, ddf DyndepFile) error {
return d.dyndepLoader.LoadDyndeps(node, ddf)
}
//
// implicitDepLoader loads implicit dependencies, as referenced via the
// "depfile" attribute in build files.
type implicitDepLoader struct {
state *State
di DiskInterface
depsLog *DepsLog
}
func newImplicitDepLoader(state *State, depsLog *DepsLog, di DiskInterface) implicitDepLoader {
return implicitDepLoader{
state: state,
di: di,
depsLog: depsLog,
}
}
// loadDeps loads implicit dependencies for edge.
//
// Returns false if info is just missing or out of date.
func (i *implicitDepLoader) loadDeps(edge *Edge) (bool, error) {
depsType := edge.GetBinding("deps")
if len(depsType) != 0 {
return i.loadDepsFromLog(edge), nil
}
depfile := edge.GetUnescapedDepfile()
if len(depfile) != 0 {
return i.loadDepFile(edge, depfile)
}
// No deps to load.
return true, nil
}
// loadDepFile loads implicit dependencies for edge from a depfile attribute.
//
// Returns false if info is just missing or on error.
func (i *implicitDepLoader) loadDepFile(edge *Edge, path string) (bool, error) {
defer metricRecord("depfile load")()
// Read depfile content. Treat a missing depfile as empty.
content, err := i.di.ReadFile(path)
if err != nil && !os.IsNotExist(err) {
// TODO(maruel): Use %q for real quoting.
return false, fmt.Errorf("loading '%s': %w", path, err)
}
// On a missing depfile: return false and empty error.
if len(content) == 0 {
// TODO(maruel): Use %q for real quoting.
explain("depfile '%s' is missing", path)
return false, nil
}
depfile := DepfileParser{}
if err := depfile.Parse(content); err != nil {
return false, fmt.Errorf("%s: %w", path, err)
}
if len(depfile.outs) == 0 {
return false, errors.New(path + ": no outputs declared")
}
// Check that this depfile matches the edge's output, if not return false to
// mark the edge as dirty.
firstOutput := edge.Outputs[0]
if primaryOut := CanonicalizePath(depfile.outs[0]); firstOutput.Path != primaryOut {
explain("expected depfile '%s' to mention '%s', got '%s'", path, firstOutput.Path, primaryOut)
return false, nil
}
// Ensure that all mentioned outputs are outputs of the edge.
for _, o := range depfile.outs {
found := false
for _, n := range edge.Outputs {
if n.Path == o {
found = true
break
}
}
if !found {
// TODO(maruel): Use %q for real quoting.
return false, fmt.Errorf("%s: depfile mentions '%s' as an output, but no such output was declared", path, o)
}
}
return i.processDepfileDeps(edge, depfile.ins), nil
}
// processDepfileDeps processes loaded implicit dependencies for edge and
// update the graph.
//
// Returns false with info is just missing.
func (i *implicitDepLoader) processDepfileDeps(edge *Edge, depfileIns []string) bool {
// Preallocate space in edge.Inputs to be filled in below.
implicitDep := i.preallocateSpace(edge, len(depfileIns))
// Add all its in-edges.
for _, j := range depfileIns {
node := i.state.GetNode(CanonicalizePathBits(j))
edge.Inputs[implicitDep] = node
node.OutEdges = append(node.OutEdges, edge)
i.createPhonyInEdge(node)
implicitDep++
}
return true
}