/
BuildEngineTest.cpp
791 lines (719 loc) · 26.3 KB
/
BuildEngineTest.cpp
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
//===- unittests/Core/BuildEngineTest.cpp ---------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "llbuild/Core/BuildEngine.h"
#include "llbuild/Core/BuildDB.h"
#include "llvm/Support/ErrorHandling.h"
#include "gtest/gtest.h"
#include <unordered_map>
#include <vector>
using namespace llbuild;
using namespace llbuild::core;
namespace {
class SimpleBuildEngineDelegate : public core::BuildEngineDelegate {
/// The cycle, if one was detected during building.
public:
std::vector<std::string> cycle;
private:
virtual core::Rule lookupRule(const core::KeyType& key) override {
// We never expect dynamic rule lookup.
fprintf(stderr, "error: unexpected rule lookup for \"%s\"\n",
key.c_str());
abort();
return core::Rule();
}
virtual void cycleDetected(const std::vector<core::Rule*>& items) override {
cycle.clear();
std::transform(items.begin(), items.end(), std::back_inserter(cycle),
[](auto rule) { return std::string(rule->key); });
}
};
static int32_t intFromValue(const core::ValueType& value) {
assert(value.size() == 4);
return ((value[0] << 0) |
(value[1] << 8) |
(value[2] << 16) |
(value[3] << 24));
}
static core::ValueType intToValue(int32_t value) {
std::vector<uint8_t> result(4);
result[0] = (value >> 0) & 0xFF;
result[1] = (value >> 8) & 0xFF;
result[2] = (value >> 16) & 0xFF;
result[3] = (value >> 24) & 0xFF;
return result;
}
// Simple task implementation which takes a fixed set of dependencies, evaluates
// them all, and then provides the output.
class SimpleTask : public Task {
public:
typedef std::function<std::vector<KeyType>()> InputListingFnType;
typedef std::function<int(const std::vector<int>&)> ComputeFnType;
private:
InputListingFnType listInputs;
std::vector<int> inputValues;
ComputeFnType compute;
public:
SimpleTask(InputListingFnType listInputs, ComputeFnType compute)
: listInputs(listInputs), compute(compute)
{
}
virtual void start(BuildEngine& engine) override {
// Compute the list of inputs.
auto inputs = listInputs();
// Request all of the inputs.
inputValues.resize(inputs.size());
for (int i = 0, e = inputs.size(); i != e; ++i) {
engine.taskNeedsInput(this, inputs[i], i);
}
}
virtual void provideValue(BuildEngine&, uintptr_t inputID,
const ValueType& value) override {
// Update the input values.
assert(inputID < inputValues.size());
inputValues[inputID] = intFromValue(value);
}
virtual void inputsAvailable(core::BuildEngine& engine) override {
engine.taskIsComplete(this, intToValue(compute(inputValues)));
}
};
// Helper function for creating a simple action.
typedef std::function<Task*(BuildEngine&)> ActionFn;
static ActionFn simpleAction(const std::vector<KeyType>& inputs,
SimpleTask::ComputeFnType compute) {
return [=] (BuildEngine& engine) {
return engine.registerTask(new SimpleTask([inputs]{ return inputs; },
compute)); };
}
TEST(BuildEngineTest, basic) {
// Check a trivial build graph.
std::vector<std::string> builtKeys;
SimpleBuildEngineDelegate delegate;
core::BuildEngine engine(delegate);
engine.addRule({
"value-A", simpleAction({}, [&] (const std::vector<int>& inputs) {
builtKeys.push_back("value-A");
return 2; }) });
engine.addRule({
"value-B", simpleAction({}, [&] (const std::vector<int>& inputs) {
builtKeys.push_back("value-B");
return 3; }) });
engine.addRule({
"result",
simpleAction({"value-A", "value-B"},
[&] (const std::vector<int>& inputs) {
EXPECT_EQ(2U, inputs.size());
EXPECT_EQ(2, inputs[0]);
EXPECT_EQ(3, inputs[1]);
builtKeys.push_back("result");
return inputs[0] * inputs[1] * 5;
}) });
// Build the result.
EXPECT_EQ(2 * 3 * 5, intFromValue(engine.build("result")));
EXPECT_EQ(3U, builtKeys.size());
EXPECT_EQ("value-A", builtKeys[0]);
EXPECT_EQ("value-B", builtKeys[1]);
EXPECT_EQ("result", builtKeys[2]);
// Check that we can get results for already built nodes, without building
// anything.
builtKeys.clear();
EXPECT_EQ(2, intFromValue(engine.build("value-A")));
EXPECT_TRUE(builtKeys.empty());
builtKeys.clear();
EXPECT_EQ(3, intFromValue(engine.build("value-B")));
EXPECT_TRUE(builtKeys.empty());
}
TEST(BuildEngineTest, basicWithSharedInput) {
// Check a build graph with a input key shared by multiple rules.
//
// Dependencies:
// value-C: (value-A, value-B)
// value-R: (value-A, value-C)
std::vector<std::string> builtKeys;
SimpleBuildEngineDelegate delegate;
core::BuildEngine engine(delegate);
engine.addRule({
"value-A", simpleAction({}, [&] (const std::vector<int>& inputs) {
builtKeys.push_back("value-A");
return 2; }) });
engine.addRule({
"value-B", simpleAction({}, [&] (const std::vector<int>& inputs) {
builtKeys.push_back("value-B");
return 3; }) });
engine.addRule({
"value-C",
simpleAction({"value-A", "value-B"},
[&] (const std::vector<int>& inputs) {
EXPECT_EQ(2U, inputs.size());
EXPECT_EQ(2, inputs[0]);
EXPECT_EQ(3, inputs[1]);
builtKeys.push_back("value-C");
return inputs[0] * inputs[1] * 5;
}) });
engine.addRule({
"value-R",
simpleAction({"value-A", "value-C"},
[&] (const std::vector<int>& inputs) {
EXPECT_EQ(2U, inputs.size());
EXPECT_EQ(2, inputs[0]);
EXPECT_EQ(2 * 3 * 5, inputs[1]);
builtKeys.push_back("value-R");
return inputs[0] * inputs[1] * 7;
}) });
// Build the result.
EXPECT_EQ(2 * 2 * 3 * 5 * 7, intFromValue(engine.build("value-R")));
EXPECT_EQ(4U, builtKeys.size());
EXPECT_EQ("value-A", builtKeys[0]);
EXPECT_EQ("value-B", builtKeys[1]);
EXPECT_EQ("value-C", builtKeys[2]);
EXPECT_EQ("value-R", builtKeys[3]);
}
TEST(BuildEngineTest, veryBasicIncremental) {
// Check a trivial build graph responds to incremental changes appropriately.
//
// Dependencies:
// value-R: (value-A, value-B)
std::vector<std::string> builtKeys;
SimpleBuildEngineDelegate delegate;
core::BuildEngine engine(delegate);
int valueA = 2;
int valueB = 3;
engine.addRule({
"value-A", simpleAction({}, [&] (const std::vector<int>& inputs) {
builtKeys.push_back("value-A");
return valueA; }),
[&](core::BuildEngine&, const Rule& rule, const ValueType value) {
return valueA == intFromValue(value);
} });
engine.addRule({
"value-B", simpleAction({}, [&] (const std::vector<int>& inputs) {
builtKeys.push_back("value-B");
return valueB; }),
[&](core::BuildEngine&, const Rule& rule, const ValueType& value) {
return valueB == intFromValue(value);
} });
engine.addRule({
"value-R",
simpleAction({"value-A", "value-B"},
[&] (const std::vector<int>& inputs) {
EXPECT_EQ(2U, inputs.size());
EXPECT_EQ(valueA, inputs[0]);
EXPECT_EQ(valueB, inputs[1]);
builtKeys.push_back("value-R");
return inputs[0] * inputs[1] * 5;
}) });
// Build the first result.
builtKeys.clear();
EXPECT_EQ(valueA * valueB * 5, intFromValue(engine.build("value-R")));
EXPECT_EQ(3U, builtKeys.size());
EXPECT_EQ("value-A", builtKeys[0]);
EXPECT_EQ("value-B", builtKeys[1]);
EXPECT_EQ("value-R", builtKeys[2]);
// Mark value-A as having changed, then rebuild and sanity check.
valueA = 7;
builtKeys.clear();
EXPECT_EQ(valueA * valueB * 5, intFromValue(engine.build("value-R")));
EXPECT_EQ(2U, builtKeys.size());
EXPECT_EQ("value-A", builtKeys[0]);
EXPECT_EQ("value-R", builtKeys[1]);
// Check that a subsequent build is null.
builtKeys.clear();
EXPECT_EQ(valueA * valueB * 5, intFromValue(engine.build("value-R")));
EXPECT_EQ(0U, builtKeys.size());
}
TEST(BuildEngineTest, basicIncremental) {
// Check a build graph responds to incremental changes appropriately.
//
// Dependencies:
// value-C: (value-A, value-B)
// value-R: (value-A, value-C)
// value-D: (value-R)
// value-R2: (value-D)
//
// If value-A or value-B change, then value-C and value-R are recomputed when
// value-R is built, but value-R2 is not recomputed.
//
// If value-R2 is built, then value-B changes and value-R is built, then when
// value-R2 is built again only value-D and value-R2 are rebuilt.
std::vector<std::string> builtKeys;
SimpleBuildEngineDelegate delegate;
core::BuildEngine engine(delegate);
int valueA = 2;
int valueB = 3;
engine.addRule({
"value-A", simpleAction({}, [&] (const std::vector<int>& inputs) {
builtKeys.push_back("value-A");
return valueA; }),
[&](core::BuildEngine&, const Rule& rule, const ValueType& value) {
return valueA == intFromValue(value);
} });
engine.addRule({
"value-B", simpleAction({}, [&] (const std::vector<int>& inputs) {
builtKeys.push_back("value-B");
return valueB; }),
[&](core::BuildEngine&, const Rule& rule, const ValueType& value) {
return valueB == intFromValue(value);
} });
engine.addRule({
"value-C",
simpleAction({"value-A", "value-B"},
[&] (const std::vector<int>& inputs) {
EXPECT_EQ(2U, inputs.size());
EXPECT_EQ(valueA, inputs[0]);
EXPECT_EQ(valueB, inputs[1]);
builtKeys.push_back("value-C");
return inputs[0] * inputs[1] * 5;
}) });
engine.addRule({
"value-R",
simpleAction({"value-A", "value-C"},
[&] (const std::vector<int>& inputs) {
EXPECT_EQ(2U, inputs.size());
EXPECT_EQ(valueA, inputs[0]);
EXPECT_EQ(valueA * valueB * 5, inputs[1]);
builtKeys.push_back("value-R");
return inputs[0] * inputs[1] * 7;
}) });
engine.addRule({
"value-D",
simpleAction({"value-R"},
[&] (const std::vector<int>& inputs) {
EXPECT_EQ(1U, inputs.size());
EXPECT_EQ(valueA * valueA * valueB * 5 * 7,
inputs[0]);
builtKeys.push_back("value-D");
return inputs[0] * 11;
}) });
engine.addRule({
"value-R2",
simpleAction({"value-D"},
[&] (const std::vector<int>& inputs) {
EXPECT_EQ(1U, inputs.size());
EXPECT_EQ(valueA * valueA * valueB * 5 * 7 * 11,
inputs[0]);
builtKeys.push_back("value-R2");
return inputs[0] * 13;
}) });
// Build the first result.
builtKeys.clear();
EXPECT_EQ(valueA * valueA * valueB * 5 * 7,
intFromValue(engine.build("value-R")));
EXPECT_EQ(4U, builtKeys.size());
EXPECT_EQ("value-A", builtKeys[0]);
EXPECT_EQ("value-B", builtKeys[1]);
EXPECT_EQ("value-C", builtKeys[2]);
EXPECT_EQ("value-R", builtKeys[3]);
// Mark value-A as having changed, then rebuild and sanity check.
valueA = 17;
builtKeys.clear();
EXPECT_EQ(valueA * valueA * valueB * 5 * 7,
intFromValue(engine.build("value-R")));
EXPECT_EQ(3U, builtKeys.size());
EXPECT_EQ("value-A", builtKeys[0]);
EXPECT_EQ("value-C", builtKeys[1]);
EXPECT_EQ("value-R", builtKeys[2]);
// Mark value-B as having changed, then rebuild and sanity check.
valueB = 19;
builtKeys.clear();
EXPECT_EQ(valueA * valueA * valueB * 5 * 7,
intFromValue(engine.build("value-R")));
EXPECT_EQ(3U, builtKeys.size());
EXPECT_EQ("value-B", builtKeys[0]);
EXPECT_EQ("value-C", builtKeys[1]);
EXPECT_EQ("value-R", builtKeys[2]);
// Build value-R2 for the first time.
builtKeys.clear();
EXPECT_EQ(valueA * valueA * valueB * 5 * 7 * 11 * 13,
intFromValue(engine.build("value-R2")));
EXPECT_EQ(2U, builtKeys.size());
EXPECT_EQ("value-D", builtKeys[0]);
EXPECT_EQ("value-R2", builtKeys[1]);
// Now mark value-B as having changed, then rebuild value-R, then build
// value-R2 and sanity check.
valueB = 23;
builtKeys.clear();
EXPECT_EQ(valueA * valueA * valueB * 5 * 7,
intFromValue(engine.build("value-R")));
EXPECT_EQ(3U, builtKeys.size());
EXPECT_EQ("value-B", builtKeys[0]);
EXPECT_EQ("value-C", builtKeys[1]);
EXPECT_EQ("value-R", builtKeys[2]);
builtKeys.clear();
EXPECT_EQ(valueA * valueA * valueB * 5 * 7 * 11 * 13,
intFromValue(engine.build("value-R2")));
EXPECT_EQ(2U, builtKeys.size());
EXPECT_EQ("value-D", builtKeys[0]);
EXPECT_EQ("value-R2", builtKeys[1]);
// Final sanity check.
builtKeys.clear();
EXPECT_EQ(valueA * valueA * valueB * 5 * 7,
intFromValue(engine.build("value-R")));
EXPECT_EQ(valueA * valueA * valueB * 5 * 7 * 11 * 13,
intFromValue(engine.build("value-R2")));
EXPECT_EQ(0U, builtKeys.size());
}
TEST(BuildEngineTest, incrementalDependency) {
// Check that the engine properly clears the individual result dependencies
// when a rule is rerun.
//
// Dependencies:
// value-R: (value-A)
//
// FIXME: This test is rather cumbersome for all it is trying to check, maybe
// we need a logging BuildDB implementation or something that would be easier
// to test against (or just update the trace to track it).
SimpleBuildEngineDelegate delegate;
core::BuildEngine engine(delegate);
// Attach a custom database, used to get the results.
class CustomDB : public BuildDB {
public:
std::unordered_map<core::KeyType, Result> ruleResults;
virtual uint64_t getCurrentIteration() override {
return 0;
}
virtual void setCurrentIteration(uint64_t value) override {}
virtual bool lookupRuleResult(const Rule& rule,
Result* result_out) override {
return false;
}
virtual void setRuleResult(const Rule& rule,
const Result& result) override {
ruleResults[rule.key] = result;
}
virtual void buildStarted() override {}
virtual void buildComplete() override {}
};
CustomDB *db = new CustomDB();
engine.attachDB(std::unique_ptr<CustomDB>(db));
int valueA = 2;
engine.addRule({
"value-A", simpleAction({}, [&] (const std::vector<int>& inputs) {
return valueA; }),
[&](core::BuildEngine&, const Rule& rule, const ValueType& value) {
return valueA == intFromValue(value);
} });
engine.addRule({
"value-R",
simpleAction({"value-A"},
[&] (const std::vector<int>& inputs) {
EXPECT_EQ(1U, inputs.size());
EXPECT_EQ(valueA, inputs[0]);
return inputs[0] * 3;
}) });
// Build the first result.
EXPECT_EQ(valueA * 3, intFromValue(engine.build("value-R")));
// Mark value-A as having changed, then rebuild.
valueA = 5;
EXPECT_EQ(valueA * 3, intFromValue(engine.build("value-R")));
// Check the rule results.
const Result& valueRResult = db->ruleResults["value-R"];
EXPECT_EQ(valueA * 3, intFromValue(valueRResult.value));
EXPECT_EQ(1U, valueRResult.dependencies.size());
}
TEST(BuildEngineTest, deepDependencyScanningStack) {
// Check that the engine can handle dependency scanning of a very deep stack,
// which would probably crash blowing the stack if the engine used naive
// recursion.
//
// FIXME: It would be nice to run this on a thread with a small stack to
// guarantee this, and to be able to make the depth small so the test runs
// faster.
int depth = 10000;
SimpleBuildEngineDelegate delegate;
core::BuildEngine engine(delegate);
int lastInputValue = 0;
for (int i = 0; i != depth; ++i) {
char name[32];
sprintf(name, "input-%d", i);
if (i != depth-1) {
char inputName[32];
sprintf(inputName, "input-%d", i+1);
engine.addRule({
name, simpleAction({ inputName },
[] (const std::vector<int>& inputs) {
return inputs[0]; }) });
} else {
engine.addRule({
name,
simpleAction({},
[&] (const std::vector<int>& inputs) {
return lastInputValue; }),
[&](core::BuildEngine&, const Rule& rule, const ValueType& value) {
// FIXME: Once we have custom ValueType objects, we would like to
// have timestamps on the value and just compare to a timestamp
// (similar to what we would do for a file).
return lastInputValue == intFromValue(value);
} });
}
}
// Build the first result.
lastInputValue = 42;
EXPECT_EQ(lastInputValue, intFromValue(engine.build("input-0")));
// Perform a null build on the result.
EXPECT_EQ(lastInputValue, intFromValue(engine.build("input-0")));
// Perform a full rebuild on the result.
lastInputValue = 52;
EXPECT_EQ(lastInputValue, intFromValue(engine.build("input-0")));
}
TEST(BuildEngineTest, discoveredDependencies) {
// Check basic support for tasks to report discovered dependencies.
// This models a task which has some out-of-band way to read the input.
class TaskWithDiscoveredDependency : public Task {
int& valueB;
int computedInputValue = -1;
public:
TaskWithDiscoveredDependency(int& valueB) : valueB(valueB) { }
virtual void start(BuildEngine& engine) override {
// Request the known input.
engine.taskNeedsInput(this, "value-A", 0);
}
virtual void provideValue(BuildEngine&, uintptr_t inputID,
const ValueType& value) override {
assert(inputID == 0);
computedInputValue = intFromValue(value);
}
virtual void inputsAvailable(core::BuildEngine& engine) override {
// Report the discovered dependency.
engine.taskDiscoveredDependency(this, "value-B");
engine.taskIsComplete(this, intToValue(computedInputValue * valueB * 5));
}
};
std::vector<std::string> builtKeys;
SimpleBuildEngineDelegate delegate;
core::BuildEngine engine(delegate);
int valueA = 2;
int valueB = 3;
engine.addRule({
"value-A",
simpleAction({ },
[&] (const std::vector<int>& inputs) {
builtKeys.push_back("value-A");
return valueA;
}),
[&](BuildEngine&, const Rule& rule, const ValueType& value) {
return valueA == intFromValue(value);
} });
engine.addRule({
"value-B",
simpleAction({ },
[&] (const std::vector<int>& inputs) {
builtKeys.push_back("value-B");
return valueB;
}),
[&](BuildEngine&, const Rule& rule, const ValueType& value) {
return valueB == intFromValue(value);
} });
engine.addRule({
"output",
[&valueB, &builtKeys] (BuildEngine& engine) {
builtKeys.push_back("output");
return engine.registerTask(new TaskWithDiscoveredDependency(valueB));
} });
// Build the first result.
builtKeys.clear();
EXPECT_EQ(valueA * valueB * 5, intFromValue(engine.build("output")));
EXPECT_EQ(std::vector<std::string>({ "output", "value-A", "value-B" }),
builtKeys);
// Verify that the next build is a null build.
builtKeys.clear();
EXPECT_EQ(valueA * valueB * 5, intFromValue(engine.build("output")));
EXPECT_EQ(std::vector<std::string>(), builtKeys);
// Verify that the build depends on valueB.
valueB = 7;
builtKeys.clear();
EXPECT_EQ(valueA * valueB * 5, intFromValue(engine.build("output")));
EXPECT_EQ(std::vector<std::string>({ "value-B", "output" }),
builtKeys);
// Verify again that the next build is a null build.
builtKeys.clear();
EXPECT_EQ(valueA * valueB * 5, intFromValue(engine.build("output")));
EXPECT_EQ(std::vector<std::string>(), builtKeys);
}
TEST(BuildEngineTest, unchangedOutputs) {
// Check building with unchanged outputs.
std::vector<std::string> builtKeys;
SimpleBuildEngineDelegate delegate;
core::BuildEngine engine(delegate);
engine.addRule({
"value",
simpleAction({}, [&] (const std::vector<int>& inputs) {
builtKeys.push_back("value");
return 2; }),
[&](BuildEngine&, const Rule& rule, const ValueType& value) {
// Always rebuild
return false;
} });
engine.addRule({
"result",
simpleAction({"value"},
[&] (const std::vector<int>& inputs) {
EXPECT_EQ(1U, inputs.size());
EXPECT_EQ(2, inputs[0]);
builtKeys.push_back("result");
return inputs[0] * 3;
}) });
// Build the result.
EXPECT_EQ(2 * 3, intFromValue(engine.build("result")));
EXPECT_EQ(2U, builtKeys.size());
EXPECT_EQ("value", builtKeys[0]);
EXPECT_EQ("result", builtKeys[1]);
// Rebuild the result.
//
// Only "value" should rebuild, as it explicitly declares itself invalid each
// time, but "result" should not need to rerun.
builtKeys.clear();
EXPECT_EQ(2 * 3, intFromValue(engine.build("result")));
EXPECT_EQ(1U, builtKeys.size());
EXPECT_EQ("value", builtKeys[0]);
}
TEST(BuildEngineTest, StatusCallbacks) {
unsigned numScanned = 0;
unsigned numComplete = 0;
SimpleBuildEngineDelegate delegate;
core::BuildEngine engine(delegate);
engine.addRule({
"input",
simpleAction({}, [&] (const std::vector<int>& inputs) {
return 2; }),
nullptr,
[&] (BuildEngine&, core::Rule::StatusKind status) {
if (status == core::Rule::StatusKind::IsScanning) {
++numScanned;
} else {
assert(status == core::Rule::StatusKind::IsComplete);
++numComplete;
}
} });
engine.addRule({
"output",
simpleAction({"input"},
[&] (const std::vector<int>& inputs) {
return inputs[0] * 3;
}),
nullptr,
[&] (BuildEngine&, core::Rule::StatusKind status) {
if (status == core::Rule::StatusKind::IsScanning) {
++numScanned;
} else {
assert(status == core::Rule::StatusKind::IsComplete);
++numComplete;
}
} });
// Build the result.
EXPECT_EQ(2 * 3, intFromValue(engine.build("output")));
EXPECT_EQ(2U, numScanned);
EXPECT_EQ(2U, numComplete);
}
/// Check basic cycle detection.
TEST(BuildEngineTest, SimpleCycle) {
SimpleBuildEngineDelegate delegate;
core::BuildEngine engine(delegate);
engine.addRule({
"A",
simpleAction({"B"}, [&](const std::vector<int>& inputs) {
return 2; }) });
engine.addRule({
"B",
simpleAction({"A"}, [&](const std::vector<int>& inputs) {
return 2; }) });
// Build the result.
auto result = engine.build("A");
EXPECT_EQ(ValueType{}, result);
EXPECT_EQ(std::vector<std::string>({ "A", "B", "A" }), delegate.cycle);
}
/// Check detection of a cycle discovered during scanning from input.
TEST(BuildEngineTest, CycleDuringScanningFromTop) {
SimpleBuildEngineDelegate delegate;
core::BuildEngine engine(delegate);
unsigned iteration = 0;
engine.addRule({
"A",
[&](BuildEngine& engine) {
return engine.registerTask(
new SimpleTask(
[&]() -> std::vector<std::string> {
switch (iteration) {
case 0:
return { "B", "C" };
case 1:
return { "B" };
default:
llvm::report_fatal_error("unexpected iterator");
}
},
[&](const std::vector<int>& inputs) {
return 2; }));
},
[&](BuildEngine&, const Rule& rule, const ValueType& value) {
// Always rebuild
return true;
}
});
engine.addRule({
"B",
[&](BuildEngine& engine) {
return engine.registerTask(
new SimpleTask(
[&]() -> std::vector<std::string> {
switch (iteration) {
case 0:
return { "C" };
case 1:
return { "C" };
default:
llvm::report_fatal_error("unexpected iterator");
}
},
[&](const std::vector<int>& inputs) {
return 2; }));
},
[&](BuildEngine&, const Rule& rule, const ValueType& value) {
// Always rebuild
return true;
}
});
engine.addRule({
"C",
[&](BuildEngine& engine) {
return engine.registerTask(
new SimpleTask(
[&]() -> std::vector<std::string> {
switch (iteration) {
case 0:
return { };
case 1:
return { "B" };
default:
llvm::report_fatal_error("unexpected iterator");
}
},
[&](const std::vector<int>& inputs) {
return 2; }));
},
[&](BuildEngine&, const Rule& rule, const ValueType& value) {
// Always rebuild
return false;
}
});
// Build the result.
{
EXPECT_EQ(2, intFromValue(engine.build("A")));
EXPECT_EQ(std::vector<std::string>({}), delegate.cycle);
}
// Introduce a cycle, and rebuild.
{
iteration = 1;
auto result = engine.build("A");
EXPECT_EQ(ValueType{}, result);
EXPECT_EQ(std::vector<std::string>({ "B", "C", "B" }), delegate.cycle);
}
}
}