aboutsummaryrefslogtreecommitdiff
path: root/build2/algorithm.cxx
blob: 1ede11550747071f1d1b24a925c0e74c63f2d354 (plain)
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
// file      : build2/algorithm.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <build2/algorithm>

#include <build2/scope>
#include <build2/target>
#include <build2/rule>
#include <build2/file> // import()
#include <build2/search>
#include <build2/context>
#include <build2/scheduler>
#include <build2/filesystem>
#include <build2/diagnostics>
#include <build2/prerequisite>

using namespace std;
using namespace butl;

namespace build2
{
  target&
  search (const prerequisite_key& pk)
  {
    assert (phase == run_phase::search_match);

    // If this is a project-qualified prerequisite, then this is import's
    // business.
    //
    if (pk.proj)
      return import (pk);

    if (target* t = pk.tk.type->search (pk))
      return *t;

    return create_new_target (pk);
  }

  target&
  search (name n, const scope& s)
  {
    assert (phase == run_phase::search_match);

    optional<string> ext;
    const target_type* tt (s.find_target_type (n, ext));

    if (tt == nullptr)
      fail << "unknown target type " << n.type << " in name " << n;

    if (!n.dir.empty ())
      n.dir.normalize (false, true); // Current dir collapses to an empty one.

    // @@ OUT: for now we assume the prerequisite's out is undetermined.
    //         Would need to pass a pair of names.
    //
    return search (*tt,
                   n.dir,
                   dir_path (),
                   n.value,
                   ext ? &*ext : nullptr,
                   &s,
                   n.proj);
  }

  const target*
  search_existing (const name& cn, const scope& s, const dir_path& out)
  {
    assert (phase == run_phase::search_match || phase == run_phase::execute);

    // We don't handle this for now.
    //
    if (cn.qualified ())
      return nullptr;

    name n (cn);
    optional<string> ext;
    const target_type* tt (s.find_target_type (n, ext));

    // For now we treat an unknown target type as an unknown target. Seems
    // logical.
    //
    if (tt == nullptr)
      return nullptr;

    if (!n.dir.empty ())
      n.dir.normalize (false, true); // Current dir collapses to an empty one.

    // @@ OUT: for now we assume the prerequisite's out is undetermined.
    //         Would need to pass a pair of names.
    //
    return search_existing_target (
      prerequisite_key {n.proj, {tt, &n.dir, &out, &n.value, ext}, &s});
  }

  pair<const rule*, match_result>
  match_impl (slock& ml, action a, target& t, bool apply, const rule* skip)
  {
    pair<const rule*, match_result> r (nullptr, false);

    // By default, clear the resolved targets list before calling
    // match(). The rule is free to modify this list in match()
    // (provided that it matches) in order to, for example, prepare
    // it for apply().
    //
    t.reset (a);

    // If this is a nested operation, first try the outer operation.
    // This allows a rule to implement a "precise match", that is,
    // both inner and outer operations match.
    //
    for (operation_id oo (a.outer_operation ()), io (a.operation ()),
           o (oo != 0 ? oo : io);
         o != 0;
         o = (oo != 0 && o != io ? io : 0))
    {
      // Adjust action for recipe: on the first iteration we want it
      // {inner, outer} (which is the same as 'a') while on the second
      // -- {inner, 0}. Note that {inner, 0} is the same or "stronger"
      // (i.e., overrides; see action::operator<()) than 'a'. This
      // allows "unconditional inner" to override "inner for outer"
      // recipes.
      //
      action ra (a.meta_operation (), io, o != oo ? 0 : oo);

      const scope& bs (t.base_scope ());

      for (auto tt (&t.type ()); tt != nullptr; tt = tt->base)
      {
        // Search scopes outwards, stopping at the project root.
        //
        for (const scope* s (&bs);
             s != nullptr;
             s = s->root () ? global_scope : s->parent_scope ())
        {
          const operation_rule_map* om (s->rules[a.meta_operation ()]);

          if (om == nullptr)
            continue; // No entry for this meta-operation id.

          // First try the map for the actual operation. If that doesn't yeld
          // anything, try the wildcard map.
          //
          for (size_t oi (o), oip (o); oip != 0; oip = oi, oi = 0)
          {
            const target_type_rule_map* ttm ((*om)[oi]);

            if (ttm == nullptr)
              continue; // No entry for this operation id.

            if (ttm->empty ())
              continue; // Empty map for this operation id.

            auto i (ttm->find (tt));

            if (i == ttm->end () || i->second.empty ())
              continue; // No rules registered for this target type.

            const auto& rules (i->second); // Hint map.

            // @@ TODO
            //
            // Different rules can be used for different operations (update
            // vs test is a good example). So, at some point, we will probably
            // have to support a list of hints or even an operation-hint map
            // (e.g., 'hint=cxx test=foo' if cxx supports the test operation
            // but we want the foo rule instead). This is also the place where
            // the '{build clean}=cxx' construct (which we currently do not
            // support) can come handy.
            //
            // Also, ignore the hint (that is most likely ment for a different
            // operation) if this is a unique match.
            //
            string hint;
            auto rs (rules.size () == 1
                     ? make_pair (rules.begin (), rules.end ())
                     : rules.find_prefix (hint));

            for (auto i (rs.first); i != rs.second; ++i)
            {
              const string& n (i->first);
              const rule& ru (i->second);

              if (&ru == skip)
                continue;

              match_result m (false);
              {
                auto g (
                  make_exception_guard (
                    [ra, &t, &n]()
                    {
                      info << "while matching rule " << n << " to "
                           << diag_do (ra, t);
                    }));

                if (!(m = ru.match (ml, ra, t, hint)))
                  continue;

                if (!m.recipe_action.valid ())
                  m.recipe_action = ra; // Default, if not set.
              }

              // Do the ambiguity test.
              //
              bool ambig (false);

              diag_record dr;

              for (++i; i != rs.second; ++i)
              {
                const string& n1 (i->first);
                const rule& ru1 (i->second);

                {
                  auto g (
                    make_exception_guard (
                      [ra, &t, &n1]()
                      {
                        info << "while matching rule " << n1 << " to "
                             << diag_do (ra, t);
                      }));

                  if (!ru1.match (ml, ra, t, hint))
                    continue;
                }

                if (!ambig)
                {
                  dr << fail << "multiple rules matching "
                     << diag_doing (ra, t)
                     << info << "rule " << n << " matches";
                  ambig = true;
                }

                dr << info << "rule " << n1 << " also matches";
              }

              if (!ambig)
              {
                ra = m.recipe_action; // Use custom, if set.

                if (apply)
                {
                  auto g (
                    make_exception_guard (
                      [ra, &t, &n]()
                      {
                        info << "while applying rule " << n << " to "
                             << diag_do (ra, t);
                      }));

                  // @@ We could also allow the rule to change the recipe
                  // action in apply(). Could be useful with delegates.
                  //
                  t.recipe (ra, ru.apply (ml, ra, t));
                }
                else
                {
                  r.first = &ru;
                  r.second = move (m);
                }

                return r;
              }
              else
                dr << info << "use rule hint to disambiguate this match";
            }
          }
        }
      }
    }

    diag_record dr;
    dr << fail << "no rule to " << diag_do (a, t);

    if (verb < 4)
      dr << info << "re-run with --verbose 4 for more information";

    dr << endf;
  }

  group_view
  resolve_group_members_impl (slock& ml, action a, target& g)
  {
    group_view r;

    // Unless we already have a recipe, try matching the target to
    // the rule.
    //
    if (!g.recipe (a))
    {
      auto rp (match_impl (ml, a, g, false));

      r = g.group_members (a);
      if (r.members != nullptr)
        return r;

      // That didn't help, so apply the rule and go to the building
      // phase.
      //
      const match_result& mr (rp.second);
      g.recipe (mr.recipe_action,
                rp.first->apply (ml, mr.recipe_action, g));
    }

    // Note that we use execute_direct() rather than execute() here to
    // sidestep the dependents count logic. In this context, this is by
    // definition the first attempt to execute this rule (otherwise we
    // would have already known the members list) and we really do need
    // to execute it now.
    //
    execute_direct (a, g);

    r = g.group_members (a);
    return r; // Might still be unresolved.
  }

  void
  search_and_match_prerequisites (slock& ml,
                                  action a,
                                  target& t,
                                  const scope* s)
  {
    for (prerequisite& p: group_prerequisites (t))
    {
      target& pt (search (p));

      if (s == nullptr || pt.in (*s))
      {
        match (ml, a, pt);
        t.prerequisite_targets.push_back (&pt);
      }
    }
  }

  void
  search_and_match_prerequisite_members (slock& ml,
                                         action a,
                                         target& t,
                                         const scope* s)
  {
    for (prerequisite_member p: group_prerequisite_members (ml, a, t))
    {
      target& pt (p.search ());

      if (s == nullptr || pt.in (*s))
      {
        match (ml, a, pt);
        t.prerequisite_targets.push_back (&pt);
      }
    }
  }

  fsdir*
  inject_fsdir (slock& ml, action a, target& t, bool parent)
  {
    tracer trace ("inject_fsdir");

    const scope& bs (t.base_scope ());
    const scope* rs (bs.root_scope ());

    // Handle the outside of any project case. Note that we also used to bail
    // our of this is the root of the project. But that proved not to be such
    // a great idea in case of subprojects (e.g., tests/).
    //
    if (rs == nullptr)
      return nullptr;

    // If t is a directory (name is empty), say foo/bar/, then t is bar and
    // its parent directory is foo/.
    //
    const dir_path& d (parent && t.name.empty () ? t.dir.directory () : t.dir);

    // Handle the src = out.
    //
    if (d.sub (rs->src_path ()))
      return nullptr;

    l6 ([&]{trace << d << " for " << t;});

    // Target is in the out tree, so out directory is empty.
    //
    fsdir* r (&search<fsdir> (d, dir_path (), string (), nullptr, nullptr));
    match (ml, a, *r);
    t.prerequisite_targets.emplace_back (r);
    return r;
  }

  inline target_state
  execute_impl (action a, target& t)
  {
    // Task count should be count_executing.
    //
    assert (t.state_ == target_state::unknown);

    auto g (
      make_exception_guard (
        [a, &t]()
        {
          t.state_ = target_state::failed;

          if (verb != 0)
            info << "while " << diag_doing (a, t);
        }));

    target_state ts (t.recipe (a) (a, t));
    assert (ts != target_state::unknown && ts != target_state::failed);

    t.state_ = ts;
    if (ts == target_state::group)
      ts = t.group->state_;

    // Decrement the task count (to count_executed) and wake up any threads
    // that might be waiting for this target.
    //
    size_t tc (t.task_count--);
    assert (tc == target::count_executing);
    sched.resume (t.task_count);

    return ts;
  }

  target_state
  execute (action a,
           const target& ct,
           size_t start_count,
           atomic_count* task_count)
  {
    target& t (const_cast<target&> (ct)); // MT-aware.

    // text << "E " << t << ": " << t.dependents << " " << dependency_count;

    // Update dependency counts and make sure they are not skew.
    //
    size_t d;
    {
      // Note: memory order can probably be relaxed.
      //
      size_t g (dependency_count--);
      d = t.dependents--;
      assert (g != 0 && d != 0);
      d--;
    }

    // Handle the "last" execution mode.
    //
    // This gets interesting when we consider interaction with
    // groups. It seem to make sense to treat group members as
    // dependents of the group, so, for example, if we try to
    // clean the group via three of its members, only the last
    // attempt will actually execute the clean. This means that
    // when we match a group member, inside we should also match
    // the group in order to increment the dependents count. This
    // seems to be a natural requirement: if we are delegating to
    // the group, we need to find a recipe for it, just like we
    // would for a prerequisite.
    //
    // Note that below we are going to change the group state
    // to postponed. This is not a mistake: until we execute
    // the recipe, we want to keep returning postponed. And
    // once the recipe is executed, it will reset the state
    // to group (see group_action()). To put it another way,
    // the execution of this member is postponed, not of the
    // group.
    //
    // One important invariant to keep in mind: the return
    // value from execute() should always be the same as what
    // would get returned by a subsequent call to state().
    //
    size_t tc (target::count_unexecuted);
    if (current_mode == execution_mode::last && d != 0)
    {
      // Try to atomically change unexecuted to postponed.
      //
      if (t.task_count.compare_exchange_strong (tc, target::count_postponed))
        tc = target::count_postponed;
    }
    else
    {
      // Try to atomically change unexecuted to executing. But it can also be
      // postponed to executing.
      //
      if (t.task_count.compare_exchange_strong (tc, target::count_executing) ||
          (tc == target::count_postponed &&
           t.task_count.compare_exchange_strong (tc, target::count_executing)))
      {
        if (task_count == nullptr)
          return execute_impl (a, t);

        sched.async (start_count,
                     *task_count,
                     [a] (target& t)
                     {
                       execute_impl (a, t); // @@ MT exception handling.
                     },
                     ref (t));

        return target_state::unknown;
      }
    }

    switch (tc)
    {
    case target::count_unexecuted: assert (false);
    case target::count_postponed:  return target_state::postponed;
    case target::count_executed:   return t.synchronized_state ();
    default:                       return target_state::busy;
    }
  }

  target_state
  execute_direct (action a, const target& ct)
  {
    target& t (const_cast<target&> (ct)); // MT-aware.

    size_t tc (target::count_unexecuted);
    if (t.task_count.compare_exchange_strong (tc, target::count_executing))
      return execute_impl (a, t);

    // If the target is busy, wait for it.
    //
    switch (tc)
    {
    case target::count_unexecuted:
    case target::count_postponed:  assert (false);
    case target::count_executed:   break;
    default:                       sched.wait (target::count_executed,
                                               t.task_count);
    }

    return t.synchronized_state ();
  }

  // We use the last bit of a pointer to target in prerequisite_targets as a
  // flag to indicate whether the target was already busy. Probably not worth
  // it (we are saving an atomic load), but what the hell.
  //
  // VC15 doesn't like if we use (abstract) target here.
  //
  static_assert (alignof (file) % 2 == 0, "unexpected target alignment");

  static inline void
  set_busy (const target*& p)
  {
    uintptr_t i (reinterpret_cast<uintptr_t> (p));
    i |= 0x01;
    p = reinterpret_cast<const target*> (i);
  }

  static inline bool
  get_busy (const target*& p)
  {
    uintptr_t i (reinterpret_cast<uintptr_t> (p));

    if ((i & 0x01) != 0)
    {
      i &= ~uintptr_t (0x01);
      p = reinterpret_cast<const target*> (i);
      return true;
    }

    return false;
  }

  target_state
  straight_execute_members (action a,
                            const target& t,
                            const target* ts[],
                            size_t n)
  {
    target_state r (target_state::unchanged);

    // Start asynchronous execution of prerequisites.
    //
    for (size_t i (0); i != n; ++i)
    {
      const target*& mt (ts[i]);

      if (mt == nullptr) // Skipped.
        continue;

      target_state s (
        execute_async (
          a, *mt, target::count_executing, t.task_count));

      if (s == target_state::postponed)
      {
        r |= s;
        mt = nullptr;
      }
      else if (s == target_state::busy)
        set_busy (mt);
    }
    sched.wait (target::count_executing, t.task_count);

    // Now all the targets in prerequisite_targets must be executed and
    // synchronized (and we have blanked out all the postponed ones).
    //
    for (size_t i (0); i != n; ++i)
    {
      const target*& mt (ts[i]);

      if (mt == nullptr)
        continue;

      // If the target was already busy, wait for its completion.
      //
      if (get_busy (mt))
        sched.wait (target::count_executed, mt->task_count);

      r |= mt->synchronized_state ();
    }

    return r;
  }

  target_state
  reverse_execute_members (action a,
                           const target& t,
                           const target* ts[],
                           size_t n)
  {
    // Pretty much as straight_execute_members() but in reverse order.
    //
    target_state r (target_state::unchanged);

    for (size_t i (n); i != 0; --i)
    {
      const target*& mt (ts[i - 1]);

      if (mt == nullptr)
        continue;

      target_state s (
        execute_async (
          a, *mt, target::count_executing, t.task_count));

      if (s == target_state::postponed)
      {
        r |= s;
        mt = nullptr;
      }
      else if (s == target_state::busy)
        set_busy (mt);
    }
    sched.wait (target::count_executing, t.task_count);

    for (size_t i (n); i != 0; --i)
    {
      const target*& mt (ts[i - 1]);

      if (mt == nullptr)
        continue;

      // If the target was already busy, wait for its completion.
      //
      if (get_busy (mt))
        sched.wait (target::count_executed, mt->task_count);

      r |= mt->synchronized_state ();
    }

    return r;
  }

  pair<const target*, target_state>
  execute_prerequisites (const target_type* tt,
                         action a, const target& t,
                         const timestamp& mt, const prerequisite_filter& pf)
  {
    // Pretty much as straight_execute_members() but hairier.
    //
    target_state rs (target_state::unchanged);

    for (const target*& pt: t.prerequisite_targets)
    {
      if (pt == nullptr) // Skipped.
        continue;

      target_state s (
        execute_async (
          a, *pt, target::count_executing, t.task_count));

      if (s == target_state::postponed)
      {
        rs |= s;
        pt = nullptr;
      }
      else if (s == target_state::busy)
        set_busy (pt);
    }
    sched.wait (target::count_executing, t.task_count);

    bool e (mt == timestamp_nonexistent);
    const target* rt (tt != nullptr ? nullptr : &t);

    for (const target*& pt: t.prerequisite_targets)
    {
      if (pt == nullptr)
        continue;

      // If the target was already busy, wait for its completion.
      //
      if (get_busy (pt))
        sched.wait (target::count_executed, pt->task_count);

      target_state s (pt->synchronized_state ());
      rs |= s;

      // Should we compare the timestamp to this target's?
      //
      if (!e && (!pf || pf (*pt)))
      {
        // If this is an mtime-based target, then compare timestamps.
        //
        if (auto mpt = dynamic_cast<const mtime_target*> (pt))
        {
          timestamp mp (mpt->mtime ());

          // The same logic as in mtime_target::newer() (but avoids a call to
          // state()).
          //
          if (mt < mp || (mt == mp && s == target_state::changed))
            e = true;
        }
        else
        {
          // Otherwise we assume the prerequisite is newer if it was changed.
          //
          if (s == target_state::changed)
            e = true;
        }
      }

      if (rt == nullptr && pt->is_a (*tt))
        rt = pt;
    }

    assert (rt != nullptr);
    return make_pair (e ? rt : nullptr, rs);
  }

  target_state
  noop_action (action a, const target& t)
  {
    text << "noop action triggered for " << diag_doing (a, t);
    assert (false); // We shouldn't be called, see target::recipe().
    return target_state::unchanged;
  }

  target_state
  group_action (action a, const target& t)
  {
    // If the group is busy, we wait, similar to prerequisites.
    //
    const target& g (*t.group);
    if (execute (a, g) == target_state::busy)
      sched.wait (target::count_executed, g.task_count);

    // Indicate to execute() that this target's state comes from the group.
    //
    return target_state::group;
  }

  target_state
  default_action (action a, const target& t)
  {
    return current_mode == execution_mode::first
      ? execute_prerequisites (a, t)
      : reverse_execute_prerequisites (a, t);
  }

  target_state
  clean_extra (action a,
               const file& ft,
               initializer_list<initializer_list<const char*>> extra)
  {
    // Clean the extras first and don't print the commands at verbosity level
    // below 3. Note the first extra file/directory that actually got removed
    // for diagnostics below.
    //
    target_state er (target_state::unchanged);
    bool ed (false);
    path ep;

    auto clean = [&er, &ed, &ep] (const file& f,
                                  initializer_list<const char*> es)
    {
      for (const char* e: es)
      {
        size_t n;
        if (e == nullptr || (n = strlen (e)) == 0)
          continue;

        path p;
        bool d;

        if (path::traits::absolute (e))
        {
          p = path (e);
          d = p.to_directory ();
        }
        else
        {
          if ((d = (e[n - 1] == '/')))
            --n;

          p = f.path ();
          for (; *e == '-'; ++e)
            p = p.base ();

          p.append (e, n);
        }

        target_state r (target_state::unchanged);

        if (d)
        {
          dir_path dp (path_cast<dir_path> (p));

          switch (build2::rmdir_r (dp, true, 3))
          {
          case rmdir_status::success:
            {
              r = target_state::changed;
              break;
            }
          case rmdir_status::not_empty:
            {
              if (verb >= 3)
                text << dp << " is current working directory, not removing";
              break;
            }
          case rmdir_status::not_exist:
            break;
          }
        }
        else
        {
          if (rmfile (p, 3))
            r = target_state::changed;
        }

        if (r == target_state::changed && ep.empty ())
        {
          ed = d;
          ep = move (p);
        }

        er |= r;
      }
    };

    auto ei (extra.begin ()), ee (extra.end ());

    if (ei != ee)
      clean (ft, *ei++);

    // Now clean the ad hoc group file members, if any.
    //
    for (const target* m (ft.member); m != nullptr; m = m->member)
    {
      const file* fm (dynamic_cast<const file*> (m));

      if (fm == nullptr || fm->path ().empty ())
        continue;

      if (ei != ee)
        clean (*fm, *ei++);

      const path& f (fm->path ());

      target_state r (rmfile (f, 3)
                      ? target_state::changed
                      : target_state::unchanged);

      if (r == target_state::changed && ep.empty ())
        ep = f;

      er |= r;
    }

    // Now clean the primary target and its prerequisited in the reverse order
    // of update: first remove the file, then clean the prerequisites.
    //
    target_state tr (rmfile (ft.path (), ft)
                     ? target_state::changed
                     : target_state::unchanged);

    // Update timestamp in case there are operations after us that could use
    // the information.
    //
    ft.mtime (timestamp_nonexistent);

    // Clean prerequisites.
    //
    tr |= reverse_execute_prerequisites (a, ft);

    // Factor the result of removing the extra files into the target state.
    // While strictly speaking removing them doesn't change the target state,
    // if we don't do this, then we may end up removing the file but still
    // saying that everything is clean (e.g., if someone removes the target
    // file but leaves the extra laying around). That would be confusing.
    //
    // What would also be confusing is if we didn't print any commands in
    // this case.
    //
    if (tr != target_state::changed && er == target_state::changed)
    {
      if (verb > 0 && verb < 3)
      {
        if (ed)
          text << "rm -r " << path_cast<dir_path> (ep);
        else
          text << "rm " << ep;
      }
    }

    tr |= er;
    return tr;
  }

  target_state
  perform_clean (action a, const target& t)
  {
    return clean_extra (a, dynamic_cast<const file&> (t), {nullptr});
  }

  target_state
  perform_clean_depdb (action a, const target& t)
  {
    return clean_extra (a, dynamic_cast<const file&> (t), {".d"});
  }
}