turtle-build 0.5.6

Ninja-compatible build system for high-level programming languages
Documentation
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
Feature: C and C++ header dependencies

  Scenario: Rebuild after a header dependency from a depfile is updated
    Given a file named "build.ninja" with:
      """
      rule cc
        command = printf 'building\n' && printf '$out: $in header.h\n' > $out.d && cp $in $out
        depfile = $out.d
        deps = gcc

      build foo.o: cc source.c

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    And a file named "header.h" with "#define FOO 1"
    When I successfully run `turtle`
    And a file named "header.h" with "#define FOO 2"
    And I successfully run `turtle`
    Then the stdout should contain exactly:
      """
      building
      building
      """
    And the file named "foo.o.d" should not exist

  Scenario: Rebuild every object sharing an updated header dependency
    Given a file named "build.ninja" with:
      """
      rule cc
        command = printf 'building\n' && printf '$out: $in header.h\n' > $out.d && cp $in $out
        depfile = $out.d
        deps = gcc

      build foo.o: cc foo.c
      build bar.o: cc bar.c

      """
    And a file named "foo.c" with "int main(void) { return 0; }"
    And a file named "bar.c" with "int main(void) { return 0; }"
    And a file named "header.h" with "#define FOO 1"
    When I successfully run `turtle`
    And a file named "header.h" with "#define FOO 2"
    And I successfully run `turtle`
    Then the stdout should contain exactly:
      """
      building
      building
      building
      building
      """

  Scenario: Rebuild after a header dependency from a depfile without deps is updated
    Given a file named "build.ninja" with:
      """
      rule cc
        command = printf 'building\n' && printf '$out: $in header.h\n' > $out.d && cp $in $out
        depfile = $out.d

      build foo.o: cc source.c

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    And a file named "header.h" with "#define FOO 1"
    When I successfully run `turtle`
    And a file named "header.h" with "#define FOO 2"
    And I successfully run `turtle`
    Then the stdout should contain exactly:
      """
      building
      building
      """
    And the file named "foo.o.d" should exist

  Scenario: Rebuild after a header dependency from a depfile declared in a build statement is updated
    Given a file named "build.ninja" with:
      """
      rule cc
        command = printf 'building\n' && printf '$out: $in header.h\n' > $out.d && cp $in $out

      build foo.o: cc source.c
        depfile = foo.o.d
        deps = gcc

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    And a file named "header.h" with "#define FOO 1"
    When I successfully run `turtle`
    And a file named "header.h" with "#define FOO 2"
    And I successfully run `turtle`
    Then the stdout should contain exactly:
      """
      building
      building
      """
    And the file named "foo.o.d" should not exist

  Scenario: Rebuild after a header dependency from MSVC is updated
    Given a file named "build.ninja" with:
      """
      rule cc
        command = printf 'Note: including file: header.h\ncompiled\n' && cp $in $out
        deps = msvc

      build foo.o: cc source.c

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    And a file named "header.h" with "#define FOO 1"
    When I successfully run `turtle`
    And a file named "header.h" with "#define FOO 2"
    And I successfully run `turtle`
    Then the stdout should contain exactly:
      """
      compiled
      compiled
      """

  Scenario: Read a header dependency with a custom msvc_deps_prefix declared on the rule
    Given a file named "build.ninja" with:
      """
      rule cc
        command = printf 'Remarque : inclusion du fichier : header.h\ncompiled\n' && cp $in $out
        deps = msvc
        msvc_deps_prefix = Remarque : inclusion du fichier :

      build foo.o: cc source.c

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    And a file named "header.h" with "#define FOO 1"
    When I successfully run `turtle`
    And a file named "header.h" with "#define FOO 2"
    And I successfully run `turtle`
    Then the stdout should contain exactly:
      """
      compiled
      compiled
      """

  Scenario: Read a header dependency with a custom msvc_deps_prefix declared at the top level
    Given a file named "build.ninja" with:
      """
      msvc_deps_prefix = Remarque : inclusion du fichier :

      rule cc
        command = printf 'Remarque : inclusion du fichier : header.h\ncompiled\n' && cp $in $out
        deps = msvc

      build foo.o: cc source.c

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    And a file named "header.h" with "#define FOO 1"
    When I successfully run `turtle`
    And a file named "header.h" with "#define FOO 2"
    And I successfully run `turtle`
    Then the stdout should contain exactly:
      """
      compiled
      compiled
      """

  Scenario: Prefer a build-level msvc_deps_prefix over a conflicting rule-level one
    Given a file named "build.ninja" with:
      """
      rule cc
        command = printf 'Remarque : inclusion du fichier : header.h\ncompiled\n' && cp $in $out
        deps = msvc
        msvc_deps_prefix = WRONG:

      build foo.o: cc source.c
        msvc_deps_prefix = Remarque : inclusion du fichier :

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    And a file named "header.h" with "#define FOO 1"
    When I successfully run `turtle`
    And a file named "header.h" with "#define FOO 2"
    And I successfully run `turtle`
    Then the stdout should contain exactly:
      """
      compiled
      compiled
      """

  Scenario: Prefer a rule-level msvc_deps_prefix over a conflicting top-level one
    Given a file named "build.ninja" with:
      """
      msvc_deps_prefix = WRONG:

      rule cc
        command = printf 'Remarque : inclusion du fichier : header.h\ncompiled\n' && cp $in $out
        deps = msvc
        msvc_deps_prefix = Remarque : inclusion du fichier :

      build foo.o: cc source.c
        cflags = -O2

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    And a file named "header.h" with "#define FOO 1"
    When I successfully run `turtle`
    And a file named "header.h" with "#define FOO 2"
    And I successfully run `turtle`
    Then the stdout should contain exactly:
      """
      compiled
      compiled
      """

  Scenario: Do not rebuild with unchanged header dependencies from gcc
    Given a file named "build.ninja" with:
      """
      rule cc
        command = printf 'building\n' && printf '$out: $in header.h\n' > $out.d && cat $in > $out
        depfile = $out.d
        deps = gcc

      build foo.o: cc source.c

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    And a file named "header.h" with "#define FOO 1"
    When I successfully run `turtle`
    And I successfully run `turtle`
    Then the stdout should contain exactly:
      """
      building
      """

  Scenario: Do not rebuild with unchanged header dependencies from MSVC
    Given a file named "build.ninja" with:
      """
      rule cc
        command = printf 'Note: including file: header.h\ncompiled\n' && cat $in > $out
        deps = msvc

      build foo.o: cc source.c

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    And a file named "header.h" with "#define FOO 1"
    When I successfully run `turtle`
    And I successfully run `turtle`
    Then the stdout should contain exactly:
      """
      compiled
      """

  Scenario: Tolerate a command that does not write its declared depfile
    Given a file named "build.ninja" with:
      """
      rule cc
        command = printf 'building\n' && cp $in $out
        depfile = $out.d
        deps = gcc

      build foo.o: cc source.c

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    When I successfully run `turtle`
    Then the file named "foo.o" should exist

  Scenario: Recover after a header dependency is deleted
    Given a file named "build.ninja" with:
      """
      rule cc
        command = printf 'building\n' && printf '$out: $in header.h\n' > $out.d && cp $in $out
        depfile = $out.d
        deps = gcc

      build foo.o: cc source.c

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    And a file named "header.h" with "#define FOO 1"
    When I successfully run `turtle`
    And I remove the file "header.h"
    And I successfully run `turtle`
    Then the stdout should contain exactly:
      """
      building
      building
      """

  @turtle
  Scenario: Stabilize after a header dependency is deleted
    Given a file named "build.ninja" with:
      """
      rule cc
        command = printf 'building\n' && printf '$out: $in header.h\n' > $out.d && cp $in $out
        depfile = $out.d
        deps = gcc

      build foo.o: cc source.c

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    And a file named "header.h" with "#define FOO 1"
    When I successfully run `turtle`
    And I remove the file "header.h"
    And I successfully run `turtle`
    And I successfully run `turtle`
    Then the stdout should contain exactly:
      """
      building
      building
      """

  Scenario: Tolerate a header dependency deleted while a generated header dependency is built
    Given a file named "build.ninja" with:
      """
      rule gen
        command = sleep 1 && rm -f header.h && cp $in $out

      rule cc
        command = printf '$out: $in gen.h header.h\n' > $out.d && cp $in $out
        depfile = $out.d
        deps = gcc

      build gen.h: gen gen.h.in
      build foo.o: cc source.c

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    And a file named "gen.h.in" with "#define G 1"
    And a file named "header.h" with "#define FOO 1"
    When I successfully run `turtle foo.o`
    And I successfully run `turtle foo.o`
    Then the file named "header.h" should not exist

  Scenario: Build foo.o on a clean checkout before its generated header exists
    Given a file named "build.ninja" with:
      """
      rule gen
        command = printf '#define G 1\n' > $out

      rule cc
        command = printf 'building\n' && printf '$out: $in gen.h\n' > $out.d && cp $in $out
        depfile = $out.d
        deps = gcc

      build gen.h: gen
      build foo.o: cc source.c

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    When I successfully run `turtle foo.o`
    Then the file named "foo.o" should exist

  Scenario: Do not rebuild with an unchanged header dependency that is a phony output
    Given a file named "build.ninja" with:
      """
      rule cc
        command = printf 'building\n' && printf '$out: $in header.h\n' > $out.d && cp $in $out
        depfile = $out.d
        deps = gcc

      build header.h: phony
      build foo.o: cc source.c

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    And a file named "header.h" with "#define FOO 1"
    When I successfully run `turtle foo.o`
    And I successfully run `turtle foo.o`
    Then the stdout should contain exactly "building"

  Scenario: Rebuild after a header dependency that is a phony output is updated
    Given a file named "build.ninja" with:
      """
      rule cc
        command = printf '$out: $in header.h\n' > $out.d && cat $in header.h > $out
        depfile = $out.d
        deps = gcc

      build header.h: phony header.h.in
      build foo.o: cc source.c

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    And a file named "header.h" with "#define FOO 1"
    And a file named "header.h.in" with "#define FOO 1"
    When I successfully run `turtle foo.o`
    And I successfully run `turtle foo.o`
    And a file named "header.h" with "#define FOO 2"
    And I successfully run `turtle foo.o`
    Then the file named "foo.o" should contain "#define FOO 2"

  Scenario: Do not rebuild with an unchanged header dependency generated behind a phony output
    Given a file named "build.ninja" with:
      """
      rule gen
        command = cp gen.h.in gen.h && touch $out

      rule cc
        command = printf 'building\n' && printf '$out: $in gen.h\n' > $out.d && cat $in gen.h > $out
        depfile = $out.d
        deps = gcc

      build gen.stamp: gen gen.h.in
      build gen.h: phony gen.stamp
      build foo.o: cc foo.c || gen.h

      """
    And a file named "foo.c" with "int main(void) { return 0; }"
    And a file named "gen.h.in" with "#define G 1"
    When I successfully run `turtle foo.o`
    And I successfully run `turtle foo.o`
    Then the stdout should contain exactly "building"

  Scenario: Rebuild after a header dependency generated behind a phony output is updated
    Given a file named "build.ninja" with:
      """
      rule gen
        command = cp gen.h.in gen.h && touch $out

      rule cc
        command = printf '$out: $in gen.h\n' > $out.d && cat $in gen.h > $out
        depfile = $out.d
        deps = gcc

      build gen.stamp: gen gen.h.in
      build gen.h: phony gen.stamp
      build foo.o: cc foo.c || gen.h

      """
    And a file named "foo.c" with "int main(void) { return 0; }"
    And a file named "gen.h.in" with "#define G 1"
    When I successfully run `turtle gen.h`
    And I successfully run `turtle foo.o`
    And a file named "gen.h.in" with "#define G 2"
    And I successfully run `turtle foo.o`
    Then the file named "foo.o" should contain "#define G 2"

  Scenario: Rebuild after a header dependency generated behind a rule output is updated
    Given a file named "build.ninja" with:
      """
      rule gen
        command = cp gen.h.in gen.h && touch $out

      rule alias
        command = true

      rule cc
        command = printf '$out: $in gen.h\n' > $out.d && cat $in gen.h > $out
        depfile = $out.d
        deps = gcc

      build gen.stamp: gen gen.h.in
      build gen.h: alias gen.stamp
      build foo.o: cc foo.c || gen.h

      """
    And a file named "foo.c" with "int main(void) { return 0; }"
    And a file named "gen.h.in" with "#define G 1"
    When I successfully run `turtle gen.h`
    And I successfully run `turtle foo.o`
    And a file named "gen.h.in" with "#define G 2"
    And I successfully run `turtle foo.o`
    Then the file named "foo.o" should contain "#define G 2"

  Scenario: Do not rebuild with an unchanged generated header dependency
    Given a file named "build.ninja" with:
      """
      rule gen
        command = cp $in $out

      rule cc
        command = printf 'building\n' && printf '$out: $in gen.h\n' > $out.d && cat $in gen.h > $out
        depfile = $out.d
        deps = gcc

      build gen.h: gen gen.h.in
      build foo.o: cc foo.c || gen.h

      """
    And a file named "foo.c" with "int main(void) { return 0; }"
    And a file named "gen.h.in" with "#define G 1"
    When I successfully run `turtle foo.o`
    And I successfully run `turtle foo.o`
    Then the stdout should contain exactly "building"

  Scenario: Rebuild after a generated header dependency is updated
    Given a file named "build.ninja" with:
      """
      rule gen
        command = cp $in $out

      rule cc
        command = printf '$out: $in gen.h\n' > $out.d && cat $in gen.h > $out
        depfile = $out.d
        deps = gcc

      build gen.h: gen gen.h.in
      build foo.o: cc foo.c

      """
    And a file named "foo.c" with "int main(void) { return 0; }"
    And a file named "gen.h.in" with "#define G 1"
    When I successfully run `turtle gen.h`
    And a file named "gen.h.in" with "#define G 2"
    And I successfully run `turtle foo.o`
    And I successfully run `turtle foo.o`
    Then the file named "foo.o" should contain "#define G 2"

  Scenario: Rebuild objects sharing a regenerated header dependency
    Given a file named "build.ninja" with:
      """
      rule gen
        command = cp $in $out

      rule cc
        command = printf '$out: $in gen.h\n' > $out.d && cat $in gen.h > $out
        depfile = $out.d
        deps = gcc

      build gen.h: gen gen.h.in
      build foo.o: cc foo.c
      build bar.o: cc bar.c

      """
    And a file named "foo.c" with "int main(void) { return 0; }"
    And a file named "bar.c" with "int main(void) { return 0; }"
    And a file named "gen.h.in" with "#define G 1"
    When I successfully run `turtle gen.h`
    And a file named "gen.h.in" with "#define G 2"
    And I successfully run `turtle foo.o bar.o`
    And I successfully run `turtle foo.o bar.o`
    Then the file named "foo.o" should contain "#define G 2"
    And the file named "bar.o" should contain "#define G 2"

  Scenario: Rebuild with a regenerated input discovered as a header dependency by another build
    Given a file named "build.ninja" with:
      """
      rule cp
        command = cp $in $out

      rule gen
        command = sleep 1 && cp $in $out

      rule cc
        command = printf '$out: %s\n' "$$(cat $in)" > $out.d && cp $in $out
        depfile = $out.d
        deps = gcc

      rule cat
        command = cat $in > $out

      build gen.h.tmp: cp gen.h.in
      build gen.h: gen gen.h.tmp
      build foo.o: cc foo.c
      build bar.o: cat bar.c gen.h

      """
    And a file named "foo.c" with "foo.c"
    And a file named "bar.c" with "int main(void) { return 0; }"
    And a file named "gen.h.in" with "#define G 1"
    When I successfully run `turtle`
    And a file named "foo.c" with "gen.h"
    And a file named "gen.h.in" with "#define G 2"
    And I successfully run `turtle`
    Then the file named "bar.o" should contain "#define G 2"

  Scenario: Rebuild with a header dependency regenerated behind a phony output and discovered by another build
    Given a file named "build.ninja" with:
      """
      rule cp
        command = cp $in $out

      rule gen
        command = sleep 1 && cp $in gen.h && touch $out

      rule cc
        command = printf '$out: %s\n' "$$(cat $in)" > $out.d && cp $in $out
        depfile = $out.d
        deps = gcc

      rule cc_header
        command = printf '$out: $in gen.h\n' > $out.d && cat $in gen.h > $out
        depfile = $out.d
        deps = gcc

      build gen.h.tmp: cp gen.h.in
      build gen.stamp: gen gen.h.tmp
      build gen.h: phony gen.stamp
      build foo.o: cc foo.c
      build bar.o: cc_header bar.c || gen.h

      """
    And a file named "foo.c" with "foo.c"
    And a file named "bar.c" with "int main(void) { return 0; }"
    And a file named "gen.h.in" with "#define G 1"
    When I successfully run `turtle`
    And a file named "foo.c" with "gen.h"
    And a file named "gen.h.in" with "#define G 2"
    And I successfully run `turtle`
    Then the file named "bar.o" should contain "#define G 2"

  Scenario: Report an error for a cycle through a header dependency on the next run
    Given a file named "build.ninja" with:
      """
      rule gen
        command = cp $in $out

      rule cc
        command = printf '$out: $in gen.h\n' > $out.d && cp $in $out
        depfile = $out.d
        deps = gcc

      build gen.h: gen foo.o
      build foo.o: cc source.c

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    When I successfully run `turtle foo.o`
    And I run `turtle foo.o`
    Then the exit status should not be 0
    And the stderr should contain "dependency cycle"

  Scenario: Accept a header path with an escaped space in a depfile
    Given a file named "build.ninja" with:
      """
      rule cc
        command = printf 'building\n' && printf '$out: $in my\\ header.h\n' > $out.d && cat $in > $out
        depfile = $out.d
        deps = gcc

      build foo.o: cc source.c

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    And a file named "my header.h" with "#define FOO 1"
    When I successfully run `turtle`
    And I successfully run `turtle`
    Then the stdout should contain exactly:
      """
      building
      """

  Scenario: Ignore a depfile set alongside deps = msvc
    Given a file named "build.ninja" with:
      """
      rule cc
        command = printf 'Note: including file: header.h\ncompiled\n' && printf '$out: $in nonexistent.h\n' > $out.d && cp $in $out
        depfile = $out.d
        deps = msvc

      build foo.o: cc source.c

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    And a file named "header.h" with "#define FOO 1"
    When I successfully run `turtle`
    Then the stdout should contain exactly "compiled"

  Scenario: Reject deps = gcc without a depfile
    Given a file named "build.ninja" with:
      """
      rule cc
        command = printf 'building\n' && cp $in $out
        deps = gcc

      build foo.o: cc source.c

      """
    And a file named "source.c" with "int main(void) { return 0; }"
    When I run `turtle`
    Then the exit status should not be 0