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
|
// file : libbuild2/target.ixx -*- C++ -*-
// license : MIT; see accompanying LICENSE file
#include <cstring> // memcpy()
#include <libbuild2/export.hxx>
namespace build2
{
LIBBUILD2_SYMEXPORT timestamp
mtime (const char*); // filesystem.cxx
// target_key
//
inline const string& target_key::
effective_name (string& r, bool force_ext) const
{
const target_type& tt (*type);
// Note that if the name is not empty, then we always use that, even
// if the type is dir/fsdir.
//
if (name->empty () && (tt.is_a<build2::dir> () || tt.is_a<fsdir> ()))
{
r = dir->leaf ().string ();
}
// If we have the extension and the type expects the extension to be
// always specified explicitly by the user, then add it to the name.
//
// Overall, we have the following cases:
//
// 1. Extension is fixed: man1{}.
//
// 2. Extension is always specified by the user: file{}.
//
// 3. Default extension that may be overridden by the user: hxx{}.
//
// 4. Extension assigned by the rule but may be overridden by the
// user: obje{}.
//
// By default we only include the extension for (2).
//
else if (ext && !ext->empty () &&
(force_ext ||
tt.fixed_extension == &target_extension_none ||
tt.fixed_extension == &target_extension_must))
{
r = *name + '.' + *ext;
}
else
return *name; // Use name as is.
return r;
}
// rule_hints
//
inline const string& rule_hints::
find (const target_type& tt, operation_id o, bool ut) const
{
// Look for fallback during the same iteration.
//
const value_type* f (nullptr);
for (const value_type& v: map)
{
if (!(v.type == nullptr ? ut : tt.is_a (*v.type)))
continue;
if (v.operation == o)
return v.hint;
if (f == nullptr &&
v.operation == default_id &&
(o == update_id || o == clean_id))
f = &v;
}
return f != nullptr ? f->hint : empty_string;
}
inline void rule_hints::
insert (const target_type* tt, operation_id o, string h)
{
auto i (find_if (map.begin (), map.end (),
[tt, o] (const value_type& v)
{
return v.operation == o && v.type == tt;
}));
if (i == map.end ())
map.push_back (value_type {tt, o, move (h)});
else
i->hint = move (h);
}
inline const string& target::
find_hint (operation_id o) const
{
using flag = target_type::flag;
const target_type& tt (type ());
// First check the target itself.
//
if (!rule_hints.empty ())
{
// If this is a group that "gave" its untyped hints to the members, then
// ignore untyped entries.
//
bool ut ((tt.flags & flag::member_hint) != flag::member_hint);
const string& r (rule_hints.find (tt, o, ut));
if (!r.empty ())
return r;
}
// Then check the group.
//
if (const target* g = group)
{
if (!g->rule_hints.empty ())
{
// If the group "gave" its untyped hints to the members, then don't
// ignore untyped entries.
//
bool ut ((g->type ().flags & flag::member_hint) == flag::member_hint);
return g->rule_hints.find (tt, o, ut);
}
}
return empty_string;
}
// match_extra
//
inline void match_extra::
reinit (bool f)
{
clear_data ();
fallback = f;
cur_options = all_options;
new_options = 0;
posthoc_prerequisite_targets = nullptr;
}
inline void match_extra::
free ()
{
clear_data ();
}
// target
//
inline const string* target::
ext_locked () const
{
return *ext_ ? &**ext_ : nullptr;
}
inline const string* target::
ext () const
{
slock l (ctx.targets.mutex_);
return ext_locked ();
}
inline target_key target::
key () const
{
const string* e (ext ());
return target_key {
&type (),
&dir,
&out,
&name,
e != nullptr ? optional<string> (*e) : nullopt};
}
inline target_key target::
key_locked () const
{
const string* e (ext_locked ());
return target_key {
&type (),
&dir,
&out,
&name,
e != nullptr ? optional<string> (*e) : nullopt};
}
inline names target::
as_name () const
{
return key ().as_name ();
}
inline void target::
as_name (names& r) const
{
return key ().as_name (r);
}
inline auto target::
prerequisites () const -> const prerequisites_type&
{
return prerequisites_state_.load (memory_order_acquire) == 2
? prerequisites_
: empty_prerequisites_;
}
inline bool target::
prerequisites (prerequisites_type&& p) const
{
target& x (const_cast<target&> (*this)); // MT-aware.
uint8_t e (0);
if (x.prerequisites_state_.compare_exchange_strong (
e,
1,
memory_order_acq_rel,
memory_order_acquire))
{
x.prerequisites_ = move (p);
x.prerequisites_state_.fetch_add (1, memory_order_release);
return true;
}
else
{
// Spin the transition out so that prerequisites() doesn't return empty.
//
for (; e == 1; e = prerequisites_state_.load (memory_order_acquire))
/*this_thread::yield ()*/ ;
return false;
}
}
inline bool target::
matched (action a, memory_order mo) const
{
assert (ctx.phase == run_phase::match ||
ctx.phase == run_phase::execute);
const opstate& s (state[a]);
size_t c (s.task_count.load (mo));
size_t b (ctx.count_base ()); // Note: cannot do (c - b)!
if (ctx.phase == run_phase::match)
{
// While it will normally be applied, it could also be already executed
// or being relocked to reapply match options (see lock_impl() for
// background).
//
// Note that we can't just do >= offset_applied since offset_busy can
// also mean it is being matched.
//
// See also matched_state_impl(), mtime() for similar logic.
//
return (c == (b + offset_applied) ||
c == (b + offset_executed) ||
(c >= (b + offset_busy) &&
s.match_extra.cur_options_.load (memory_order_relaxed) != 0));
}
else
{
// Note that while the target could be being executed, we should see at
// least offset_matched since it must have been "achieved" before the
// phase switch.
//
return c >= (b + offset_matched);
}
}
inline bool target::
group_state (action a) const
{
// We go an extra step and short-circuit to the target state even if the
// raw state is not group provided the recipe is group_recipe and the
// state is unknown (see mtime() for a discussion on why we do it).
//
// Note that additionally s.state may not be target_state::group even
// after execution due to deferment (see execute_impl() for details).
//
// @@ Hm, I wonder why not just return s.recipe_group_action now that we
// cache it.
//
// This special hack allows us to do things like query an ad hoc member's
// state or mtime without matching/executing the member, only the group.
// Requiring matching/executing the member would be too burdensome and
// this feels harmless (ad hoc membership cannot be changed during the
// execute phase).
//
// Note: this test must come first since the member may not be matched and
// thus its state uninitialized.
//
if (ctx.phase == run_phase::execute && adhoc_group_member ())
return true;
const opstate& s (state[a]);
if (s.state == target_state::group)
return true;
if (s.state == target_state::unknown && group != nullptr)
return s.recipe_group_action;
return false;
}
inline pair<bool, target_state> target::
matched_state_impl (action a) const
{
// Note that the "tried" state is "final".
//
const opstate& s (state[a]);
// Note: already synchronized.
//
size_t c (s.task_count.load (memory_order_relaxed));
size_t b (ctx.count_base ()); // Note: cannot do (c - b)!
if (c == (b + offset_tried))
return make_pair (false, target_state::unknown);
else
{
// The same semantics as in target::matched(). Note that in the executed
// case we are guaranteed to be synchronized since we are in the match
// phase.
//
assert (c == (b + offset_applied) ||
c == (b + offset_executed) ||
(c >= (b + offset_busy) &&
s.match_extra.cur_options_.load (memory_order_relaxed) != 0));
return make_pair (true, (group_state (a) ? group->state[a] : s).state);
}
}
inline target_state target::
executed_state_impl (action a) const
{
return (group_state (a) ? group->state : state)[a].state;
}
inline target_state target::
matched_state (action a, bool fail) const
{
assert (ctx.phase == run_phase::match);
// Note that the target could be being asynchronously re-matched.
//
pair<bool, target_state> r (matched_state_impl (a));
if (fail && (!r.first || r.second == target_state::failed))
throw failed ();
return r.second;
}
inline pair<bool, target_state> target::
try_matched_state (action a, bool fail) const
{
assert (ctx.phase == run_phase::match);
pair<bool, target_state> r (matched_state_impl (a));
if (fail && r.first && r.second == target_state::failed)
throw failed ();
return r;
}
inline target_state target::
executed_state (action a, bool fail) const
{
assert (ctx.phase == run_phase::execute || ctx.phase == run_phase::load);
target_state r (executed_state_impl (a));
if (fail && r == target_state::failed)
throw failed ();
return r;
}
inline bool target::
has_prerequisites () const
{
return !prerequisites ().empty ();
}
inline bool target::
has_group_prerequisites () const
{
return has_prerequisites () ||
(group != nullptr && group->has_prerequisites ());
}
inline bool target::
unchanged (action a) const
{
assert (ctx.phase == run_phase::match);
return matched_state_impl (a).second == target_state::unchanged;
}
inline ostream&
operator<< (ostream& os, const target& t)
{
return os << t.key ();
}
// mark()/unmark()
//
// VC15 doesn't like if we use (abstract) target here.
//
static_assert (alignof (file) % 4 == 0, "unexpected target alignment");
inline void
mark (const target*& p, uint8_t m)
{
uintptr_t i (reinterpret_cast<uintptr_t> (p));
i |= m & 0x03;
p = reinterpret_cast<const target*> (i);
}
inline uint8_t
marked (const target* p)
{
uintptr_t i (reinterpret_cast<uintptr_t> (p));
return uint8_t (i & 0x03);
}
inline uint8_t
unmark (const target*& p)
{
uintptr_t i (reinterpret_cast<uintptr_t> (p));
uint8_t m (i & 0x03);
if (m != 0)
{
i &= ~uintptr_t (0x03);
p = reinterpret_cast<const target*> (i);
}
return m;
}
// include()
//
LIBBUILD2_SYMEXPORT include_type
include_impl (action, const target&,
const prerequisite&, const target*,
lookup*);
inline include_type
include (action a, const target& t, const prerequisite& p, lookup* l)
{
// Most of the time no prerequisite-specific variables will be specified,
// so let's optimize for that.
//
return p.vars.empty ()
? include_type (true)
: include_impl (a, t, p, nullptr, l);
}
inline include_type
include (action a, const target& t, const prerequisite_member& pm, lookup* l)
{
return pm.prerequisite.vars.empty ()
? include_type (true)
: include_impl (a, t, pm.prerequisite, pm.member, l);
}
// group_prerequisites
//
inline group_prerequisites::
group_prerequisites (const target& t)
: t_ (t),
g_ (t_.group == nullptr ||
t_.group->adhoc_member != nullptr || // Ad hoc group member.
t_.group->prerequisites ().empty ()
? nullptr : t_.group)
{
}
inline group_prerequisites::
group_prerequisites (const target& t, const target* g)
: t_ (t),
g_ (g == nullptr ||
g->prerequisites ().empty ()
? nullptr : g)
{
}
inline auto group_prerequisites::
begin () const -> iterator
{
auto& c ((g_ != nullptr ? *g_ : t_).prerequisites ());
return iterator (&t_, g_, &c, c.begin ());
}
inline auto group_prerequisites::
end () const -> iterator
{
auto& c (t_.prerequisites ());
return iterator (&t_, g_, &c, c.end ());
}
inline size_t group_prerequisites::
size () const
{
return t_.prerequisites ().size () +
(g_ != nullptr ? g_->prerequisites ().size () : 0);
}
// group_prerequisites::iterator
//
inline auto group_prerequisites::iterator::
operator++ () -> iterator&
{
if (++i_ == c_->end () && c_ != &t_->prerequisites ())
{
c_ = &t_->prerequisites ();
i_ = c_->begin ();
}
return *this;
}
inline auto group_prerequisites::iterator::
operator-- () -> iterator&
{
if (i_ == c_->begin () && c_ == &t_->prerequisites ())
{
c_ = &g_->prerequisites ();
i_ = c_->end ();
}
--i_;
return *this;
}
// prerequisite_member
//
inline prerequisite prerequisite_member::
as_prerequisite () const
{
if (member == nullptr)
return prerequisite;
// An ad hoc group member cannot be used as a prerequisite (use the whole
// group instead).
//
assert (!member->adhoc_group_member ());
// Feels like copying the prerequisite's variables to member is more
// correct than not (consider for_install, for example).
//
prerequisite_type p (*member);
p.vars = prerequisite.vars;
return p;
}
inline prerequisite_key prerequisite_member::
key () const
{
return member != nullptr
? prerequisite_key {prerequisite.proj, member->key (), nullptr}
: prerequisite.key ();
}
// prerequisite_members
//
LIBBUILD2_SYMEXPORT group_view
resolve_members (action, const target&); // <libbuild2/algorithm.hxx>
template <typename T>
inline group_view prerequisite_members_range<T>::iterator::
resolve_members (const prerequisite& p)
{
// We want to allow iteration over members during execute provided the
// same iteration has been performed during match.
//
const target* pt (r_->t_.ctx.phase == run_phase::match
? &search (r_->t_, p)
: search_existing (p));
assert (pt != nullptr);
return build2::resolve_members (r_->a_, *pt);
}
template <typename T>
inline void prerequisite_members_range<T>::iterator::
switch_mode ()
{
g_ = resolve_members (*i_);
if (g_.members != nullptr)
{
// See empty see through groups as groups.
//
for (j_ = 1; j_ <= g_.count && g_.members[j_ - 1] == nullptr; ++j_) ;
if (j_ > g_.count)
g_.count = 0;
}
else
assert (r_->mode_ != members_mode::always); // Group can't be resolved.
}
template <typename T>
inline auto prerequisite_members_range<T>::iterator::
operator++ () -> iterator&
{
if (k_ != nullptr) // Iterating over an ad hoc group.
k_ = k_->adhoc_member;
if (k_ == nullptr && g_.count != 0) // Iterating over a normal group.
{
if (g_.members == nullptr) // Special case, see leave_group().
g_.count = 0;
else
{
for (++j_; j_ <= g_.count && g_.members[j_ - 1] == nullptr; ++j_) ;
if (j_ > g_.count)
g_.count = 0;
}
}
if (k_ == nullptr && g_.count == 0) // Iterating over the range.
{
++i_;
if (r_->mode_ != members_mode::never &&
i_ != r_->e_ &&
i_->type.see_through ())
switch_mode ();
}
return *this;
}
template <typename T>
inline bool prerequisite_members_range<T>::iterator::
enter_group ()
{
assert (k_ == nullptr); // No nested ad hoc group entering.
// First see if we are about to enter an ad hoc group.
//
const target* t (g_.count != 0
? j_ != 0 ? g_.members[j_ - 1] : nullptr
: i_->target.load (memory_order_consume));
if (t != nullptr && t->adhoc_member != nullptr)
k_ = t; // Increment that follows will make it t->member.
else
{
// Otherwise assume it is a normal group.
//
g_ = resolve_members (*i_);
if (g_.members == nullptr) // Members are not know.
{
g_.count = 0;
return false;
}
// Note: 0-based to account for the increment that will follow.
//
for (j_ = 0; j_ != g_.count && g_.members[j_] == nullptr; ++j_) ;
if (j_ == g_.count)
g_.count = 0;
}
return true;
}
template <typename T>
inline void prerequisite_members_range<T>::iterator::
leave_group ()
{
if (k_ != nullptr)
{
// Skip until the last element (next increment will reach the end).
//
for (; k_->adhoc_member != nullptr; k_ = k_->adhoc_member) ;
}
else
{
// Pretend we are on the last member of a normal group.
//
j_ = 0;
g_.count = 1;
g_.members = nullptr; // Ugly "special case signal" for operator++.
}
}
template <typename T>
inline bool prerequisite_members_range<T>::iterator::
group () const
{
// Ad hoc.
//
if (k_ != nullptr)
return k_->adhoc_member;
// Explicit.
//
if (g_.count != 0 && g_.members != nullptr)
{
size_t j (j_ + 1);
for (; j <= g_.count && g_.members[j - 1] == nullptr; ++j) ;
return j <= g_.count;
}
return false;
}
inline auto
prerequisite_members (action a, const target& t, members_mode m)
{
return prerequisite_members (a, t, t.prerequisites (), m);
}
inline auto
reverse_prerequisite_members (action a, const target& t, members_mode m)
{
return prerequisite_members (a, t, reverse_iterate (t.prerequisites ()), m);
}
// mtime_target
//
inline void mtime_target::
mtime (timestamp mt) const
{
mtime_.store (mt.time_since_epoch ().count (), memory_order_release);
}
inline timestamp mtime_target::
load_mtime (const path& p) const
{
// We can only enforce "not group state" during the execute phase. During
// match (e.g., the target is being matched), we will just have to pay
// attention.
//
assert (ctx.phase == run_phase::match ||
(ctx.phase == run_phase::execute &&
!group_state (action () /* inner */)));
duration::rep r (mtime_.load (memory_order_consume));
if (r == timestamp_unknown_rep)
{
assert (!p.empty ());
r = build2::mtime (p.string ().c_str ()).time_since_epoch ().count ();
mtime_.store (r, memory_order_release);
}
return timestamp (duration (r));
}
inline bool mtime_target::
newer (timestamp mt, target_state s) const
{
timestamp mp (mtime ());
// What do we do if timestamps are equal? This can happen, for example,
// on filesystems that don't have subsecond resolution. There is not
// much we can do here except detect the case where the target was
// changed on this run.
//
return mt < mp || (mt == mp && s == target_state::changed);
}
inline bool mtime_target::
newer (timestamp mt) const
{
assert (ctx.phase == run_phase::execute);
return newer (mt, executed_state_impl (action () /* inner */));
}
// path_target
//
inline const path& path_target::
path (memory_order mo) const
{
// You may be wondering why don't we spin the transition out? The reason
// is it shouldn't matter since were we called just a moment earlier, we
// wouldn't have seen it.
//
return path_state_.load (mo) == 2 ? path_ : empty_path;
}
inline const path& path_target::
path (path_type p) const
{
uint8_t e (0);
if (path_state_.compare_exchange_strong (
e,
1,
memory_order_acq_rel,
memory_order_acquire))
{
path_ = move (p);
path_state_.fetch_add (1, memory_order_release);
}
else
{
// Spin the transition out.
//
for (; e == 1; e = path_state_.load (memory_order_acquire))
/*this_thread::yield ()*/ ;
assert (e == 2 && path_ == p);
}
return path_;
}
inline timestamp path_target::
load_mtime () const
{
return mtime_target::load_mtime (path ());
}
inline const path& path_target::
path_mtime (path_type p, timestamp mt) const
{
// Because we use the presence of mtime to indicate the special "trust me,
// this file exists" situation, the order in which we do things is
// important. In particular, the fallback file_rule::match() will skip
// assigning the path if there is a valid timestamp. As a result, with the
// wrong order we may end up in a situation where the rule is matched but
// the path is not assigned.
//
const path_type& r (path (move (p)));
mtime (mt);
return r;
}
// exe
//
inline auto exe::
process_path () const -> process_path_type
{
// It's unfortunate we have to return by value but hopefully the
// compiler will see through it. Note also that returning empty
// process path if path is empty.
//
return process_path_.empty ()
? process_path_type (path ().string ().c_str (),
path_type (),
path_type ())
: process_path_type (process_path_, false /* init */);
}
inline void exe::
process_path (process_path_type p)
{
process_path_ = move (p);
}
}
|