-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanifest_parser_serial.go
594 lines (532 loc) · 14.7 KB
/
manifest_parser_serial.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
// 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 (
"fmt"
"strconv"
)
// manifestParserSerial parses .ninja files.
type manifestParserSerial struct {
// Immutable
fr FileReader
options ParseManifestOpts
// Mutable.
lexer lexer
state *State
env *BindingEnv
subninjas chan subninja
subninjasEnqueued int32
}
// parse parses a file, given its contents as a string.
func (m *manifestParserSerial) parse(filename string, input []byte) error {
defer metricRecord(".ninja parse")()
m.subninjas = make(chan subninja)
if err := m.lexer.Start(filename, input); err != nil {
return err
}
// subninja files are read as soon as the statement is parsed but they are
// only processed once the current file is done. This enables lower latency
// overall.
var err error
loop:
for err == nil {
switch token := m.lexer.ReadToken(); token {
case POOL:
err = m.parsePool()
case BUILD:
err = m.parseEdge()
case RULE:
err = m.parseRule()
case DEFAULT:
err = m.parseDefault()
case IDENT:
err = m.parseIdent()
case INCLUDE:
err = m.parseInclude()
case SUBNINJA:
err = m.parseSubninja()
case ERROR:
err = m.lexer.Error(m.lexer.DescribeLastError())
case TEOF:
break loop
case NEWLINE:
default:
err = m.lexer.Error("unexpected " + token.String())
}
}
// At this point, m.env is completely immutable and can be accessed
// concurrently.
// Did the loop complete because of an error?
if err != nil {
// Do not forget to unblock the goroutines.
for i := int32(0); i < m.subninjasEnqueued; i++ {
<-m.subninjas
}
return err
}
// Finish the processing by parsing the subninja files.
return m.processSubninjaQueue()
}
// parsePool parses a "pool" statement.
func (m *manifestParserSerial) parsePool() error {
name := m.lexer.readIdent()
if name == "" {
return m.lexer.Error("expected pool name")
}
if err := m.expectToken(NEWLINE); err != nil {
return err
}
if m.state.Pools[name] != nil {
// TODO(maruel): Use %q for real quoting.
return m.lexer.Error(fmt.Sprintf("duplicate pool '%s'", name))
}
depth := -1
for m.lexer.PeekToken(INDENT) {
key, value, err := m.parseLet()
if err != nil {
return err
}
if key != "depth" {
// TODO(maruel): Use %q for real quoting.
return m.lexer.Error(fmt.Sprintf("unexpected variable '%s'", key))
}
// TODO(maruel): Do we want to use ParseInt() here? Aka support hex.
if depth, err = strconv.Atoi(value.Evaluate(m.env)); depth < 0 || err != nil {
return m.lexer.Error("invalid pool depth")
}
}
if depth < 0 {
return m.lexer.Error("expected 'depth =' line")
}
m.state.Pools[name] = NewPool(name, depth)
return nil
}
// parseRule parses a "rule" statement.
func (m *manifestParserSerial) parseRule() error {
name := m.lexer.readIdent()
if name == "" {
return m.lexer.Error("expected rule name")
}
if err := m.expectToken(NEWLINE); err != nil {
return err
}
if m.env.Rules[name] != nil {
// TODO(maruel): Use %q for real quoting.
return m.lexer.Error(fmt.Sprintf("duplicate rule '%s'", name))
}
rule := NewRule(name)
for m.lexer.PeekToken(INDENT) {
key, value, err := m.parseLet()
if err != nil {
return err
}
if !IsReservedBinding(key) {
// Die on other keyvals for now; revisit if we want to add a
// scope here.
// TODO(maruel): Use %q for real quoting.
return m.lexer.Error(fmt.Sprintf("unexpected variable '%s'", key))
}
rule.Bindings[key] = &value
}
b1, ok1 := rule.Bindings["rspfile"]
b2, ok2 := rule.Bindings["rspfile_content"]
if ok1 != ok2 || (ok1 && (len(b1.Parsed) == 0) != (len(b2.Parsed) == 0)) {
return m.lexer.Error("rspfile and rspfile_content need to be both specified")
}
b, ok := rule.Bindings["command"]
if !ok || len(b.Parsed) == 0 {
return m.lexer.Error("expected 'command =' line")
}
m.env.Rules[rule.Name] = rule
return nil
}
// parseDefault parses a "default" statement.
func (m *manifestParserSerial) parseDefault() error {
eval, err := m.lexer.readEvalString(true)
if err != nil {
return err
}
if len(eval.Parsed) == 0 {
return m.lexer.Error("expected target name")
}
for {
path := eval.Evaluate(m.env)
if len(path) == 0 {
return m.lexer.Error("empty path")
}
if err = m.state.addDefault(CanonicalizePath(path)); err != nil {
return m.lexer.Error(err.Error())
}
eval, err = m.lexer.readEvalString(true)
if err != nil {
return err
}
if len(eval.Parsed) == 0 {
break
}
}
return m.expectToken(NEWLINE)
}
// parseIdent parses a generic statement as a fallback.
func (m *manifestParserSerial) parseIdent() error {
m.lexer.UnreadToken()
name, letValue, err := m.parseLet()
if err != nil {
return err
}
value := letValue.Evaluate(m.env)
// Check ninjaRequiredVersion immediately so we can exit
// before encountering any syntactic surprises.
if name == "ninja_required_version" {
if err := checkNinjaVersion(value); err != nil {
return err
}
}
m.env.Bindings[name] = value
return nil
}
// parseEdge parses a "build" statement that results into an edge, which
// defines inputs and outputs.
func (m *manifestParserSerial) parseEdge() error {
var outs []EvalString
for {
ev, err := m.lexer.readEvalString(true)
if err != nil {
return err
}
if len(ev.Parsed) == 0 {
break
}
outs = append(outs, ev)
}
// Add all implicit outs, counting how many as we go.
implicitOuts := 0
if m.lexer.PeekToken(PIPE) {
for {
ev, err := m.lexer.readEvalString(true)
if err != nil {
return err
}
if len(ev.Parsed) == 0 {
break
}
outs = append(outs, ev)
implicitOuts++
}
}
if len(outs) == 0 {
return m.lexer.Error("expected path")
}
if err := m.expectToken(COLON); err != nil {
return err
}
ruleName := m.lexer.readIdent()
if ruleName == "" {
return m.lexer.Error("expected build command name")
}
rule := m.env.LookupRule(ruleName)
if rule == nil {
// TODO(maruel): Use %q for real quoting.
return m.lexer.Error(fmt.Sprintf("unknown build rule '%s'", ruleName))
}
var ins []EvalString
for {
// XXX should we require one path here?
ev, err := m.lexer.readEvalString(true)
if err != nil {
return err
}
if len(ev.Parsed) == 0 {
break
}
ins = append(ins, ev)
}
// Add all implicit deps, counting how many as we go.
implicit := 0
if m.lexer.PeekToken(PIPE) {
for {
ev, err := m.lexer.readEvalString(true)
if err != nil {
return err
}
if len(ev.Parsed) == 0 {
break
}
ins = append(ins, ev)
implicit++
}
}
// Add all order-only deps, counting how many as we go.
orderOnly := 0
if m.lexer.PeekToken(PIPE2) {
for {
ev, err := m.lexer.readEvalString(true)
if err != nil {
return err
}
if len(ev.Parsed) == 0 {
break
}
ins = append(ins, ev)
orderOnly++
}
}
// Add all validations, counting how many as we go.
var validations []EvalString
if m.lexer.PeekToken(PIPEAT) {
for {
ev, err := m.lexer.readEvalString(true)
if err != nil {
return err
}
if len(ev.Parsed) == 0 {
break
}
validations = append(validations, ev)
}
}
if err := m.expectToken(NEWLINE); err != nil {
return err
}
// Bindings on edges are rare, so allocate per-edge envs only when needed.
hasIndentToken := m.lexer.PeekToken(INDENT)
env := m.env
if hasIndentToken {
env = NewBindingEnv(m.env)
}
for hasIndentToken {
key, val, err := m.parseLet()
if err != nil {
return err
}
env.Bindings[key] = val.Evaluate(m.env)
hasIndentToken = m.lexer.PeekToken(INDENT)
}
edge := m.state.addEdge(rule)
edge.Env = env
poolName := edge.GetBinding("pool")
if poolName != "" {
pool := m.state.Pools[poolName]
if pool == nil {
// TODO(maruel): Use %q for real quoting.
return m.lexer.Error(fmt.Sprintf("unknown pool name '%s'", poolName))
}
edge.Pool = pool
}
edge.Outputs = make([]*Node, 0, len(outs))
for i := range outs {
path := outs[i].Evaluate(env)
if len(path) == 0 {
return m.lexer.Error("empty path")
}
path, slashBits := CanonicalizePathBits(path)
if !m.state.addOut(edge, path, slashBits) {
if m.options.ErrOnDupeEdge {
return m.lexer.Error("multiple rules generate " + path)
}
if !m.options.Quiet {
warningf("multiple rules generate %s. builds involving this target will not be correct; continuing anyway", path)
}
if len(outs)-i <= implicitOuts {
implicitOuts--
}
}
}
if len(edge.Outputs) == 0 {
// All outputs of the edge are already created by other edges. Don't add
// this edge. Do this check before input nodes are connected to the edge.
m.state.Edges = m.state.Edges[:len(m.state.Edges)-1]
return nil
}
edge.ImplicitOuts = int32(implicitOuts)
edge.Inputs = make([]*Node, 0, len(ins))
for _, i := range ins {
path := i.Evaluate(env)
if len(path) == 0 {
return m.lexer.Error("empty path")
}
path, slashBits := CanonicalizePathBits(path)
m.state.addIn(edge, path, slashBits)
}
edge.ImplicitDeps = int32(implicit)
edge.OrderOnlyDeps = int32(orderOnly)
edge.Validations = make([]*Node, 0, len(validations))
for _, v := range validations {
path := v.Evaluate(env)
if path == "" {
return m.lexer.Error("empty path")
}
path, slashBits := CanonicalizePathBits(path)
m.state.addValidation(edge, path, slashBits)
}
if !m.options.ErrOnPhonyCycle && edge.maybePhonycycleDiagnostic() {
// CMake 2.8.12.x and 3.0.x incorrectly write phony build statements
// that reference themselves. Ninja used to tolerate these in the
// build graph but that has since been fixed. Filter them out to
// support users of those old CMake versions.
out := edge.Outputs[0]
for i, n := range edge.Inputs {
if n == out {
copy(edge.Inputs[i:], edge.Inputs[i+1:])
edge.Inputs = edge.Inputs[:len(edge.Inputs)-1]
if !m.options.Quiet {
warningf("phony target '%s' names itself as an input; ignoring [-w phonycycle=warn]", out.Path)
}
break
}
}
}
// Lookup, validate, and save any dyndep binding. It will be used later
// to load generated dependency information dynamically, but it must
// be one of our manifest-specified inputs.
dyndep := edge.GetUnescapedDyndep()
if len(dyndep) != 0 {
n := m.state.GetNode(CanonicalizePathBits(dyndep))
n.DyndepPending = true
edge.Dyndep = n
found := false
for _, x := range edge.Inputs {
if x == n {
found = true
break
}
}
if !found {
// TODO(maruel): Use %q for real quoting.
return m.lexer.Error(fmt.Sprintf("dyndep '%s' is not an input", dyndep))
}
}
return nil
}
// parseInclude parses a "include" line.
func (m *manifestParserSerial) parseInclude() error {
eval, err := m.lexer.readEvalString(true)
if err != nil {
return err
}
ls := m.lexer.lexerState
if err = m.expectToken(NEWLINE); err != nil {
return err
}
// Process state.
path := eval.Evaluate(m.env)
input, err := m.fr.ReadFile(path)
if err != nil {
// Wrap it.
// TODO(maruel): Use %q for real quoting.
return m.error(fmt.Sprintf("loading '%s': %s", path, err), ls)
}
// Manually construct the object instead of using ParseManifest(), because
// m.env may not equal to m.state.Bindings. This happens when the include
// statement is inside a subninja.
subparser := manifestParserSerial{
fr: m.fr,
options: m.options,
state: m.state,
env: m.env,
}
// Recursively parse the input into the current state.
if err = subparser.parse(path, input); err != nil {
// Do not wrap error inside the included ninja.
return err
}
return nil
}
// parseSubninja parses a "subninja" statement.
//
// If options.Concurrency != ParseManifestSerial, it starts a goroutine that
// reads the file and send the content to the channel, but not process it.
//
// Otherwise, it processes it serially.
func (m *manifestParserSerial) parseSubninja() error {
eval, err := m.lexer.readEvalString(true)
if err != nil {
return err
}
filename := eval.Evaluate(m.env)
ls := m.lexer.lexerState
if err = m.expectToken(NEWLINE); err != nil {
return err
}
if m.options.Concurrency != ParseManifestSerial {
// Start the goroutine to read it asynchronously. It will be processed
// after the main manifest.
go readSubninjaAsync(m.fr, filename, m.subninjas, ls, m.env)
m.subninjasEnqueued++
return nil
}
// Process the subninja right away. This is the most compatible way.
input, err := m.fr.ReadFile(filename)
if err != nil {
// Wrap it.
return m.error(fmt.Sprintf("loading '%s': %s", filename, err.Error()), ls)
}
return m.processOneSubninja(filename, input, m.env)
}
// processSubninjaQueue empties the queue of subninja files to process.
func (m *manifestParserSerial) processSubninjaQueue() error {
// Out of order flow. This is the faster but may be incompatible?
var err error
for i := int32(0); i < m.subninjasEnqueued; i++ {
s := <-m.subninjas
if err != nil {
continue
}
if s.err != nil {
// Wrap it.
// TODO(maruel): Use %q for real quoting.
err = m.error(fmt.Sprintf("loading '%s': %s", s.filename, s.err.Error()), s.ls)
continue
}
err = m.processOneSubninja(s.filename, s.input, s.env)
}
return err
}
func (m *manifestParserSerial) processOneSubninja(filename string, input []byte, env *BindingEnv) error {
subparser := manifestParserSerial{
fr: m.fr,
options: m.options,
state: m.state,
// Reset the binding fresh with a temporary one that will not affect the
// root one.
env: NewBindingEnv(env),
}
// Do not wrap error inside the subninja.
return subparser.parse(filename, input)
}
func (m *manifestParserSerial) parseLet() (string, EvalString, error) {
eval := EvalString{}
key := m.lexer.readIdent()
if key == "" {
return key, eval, m.lexer.Error("expected variable name")
}
var err error
if err = m.expectToken(EQUALS); err == nil {
eval, err = m.lexer.readEvalString(false)
}
return key, eval, err
}
// expectToken produces an error string if the next token is not expected.
//
// The error says "expected foo, got bar".
func (m *manifestParserSerial) expectToken(expected Token) error {
if token := m.lexer.ReadToken(); token != expected {
msg := "expected " + expected.String() + ", got " + token.String() + expected.errorHint()
return m.lexer.Error(msg)
}
return nil
}
func (m *manifestParserSerial) error(msg string, ls lexerState) error {
return ls.error(msg, m.lexer.filename, m.lexer.input)
}