aboutsummaryrefslogtreecommitdiff
path: root/paludis/resolver/orderer.cc
blob: a2e12092c7344e4f57c78b5b0eb34047267b4473 (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
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
/* vim: set sw=4 sts=4 et foldmethod=syntax : */

/*
 * Copyright (c) 2010 Ciaran McCreesh
 *
 * This file is part of the Paludis package manager. Paludis is free software;
 * you can redistribute it and/or modify it under the terms of the GNU General
 * Public License version 2, as published by the Free Software Foundation.
 *
 * Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <paludis/resolver/orderer.hh>
#include <paludis/resolver/decision.hh>
#include <paludis/resolver/decisions.hh>
#include <paludis/resolver/resolution.hh>
#include <paludis/resolver/nag.hh>
#include <paludis/resolver/reason.hh>
#include <paludis/resolver/constraint.hh>
#include <paludis/resolver/strongly_connected_component.hh>
#include <paludis/resolver/resolutions_by_resolvent.hh>
#include <paludis/resolver/resolver_functions.hh>
#include <paludis/resolver/job_lists.hh>
#include <paludis/resolver/job_list.hh>
#include <paludis/resolver/job.hh>
#include <paludis/resolver/job_requirements.hh>
#include <paludis/resolver/destination.hh>
#include <paludis/resolver/orderer_notes.hh>
#include <paludis/resolver/change_by_resolvent.hh>
#include <paludis/resolver/labels_classifier.hh>
#include <paludis/util/pimp-impl.hh>
#include <paludis/util/exception.hh>
#include <paludis/util/stringify.hh>
#include <paludis/util/hashes.hh>
#include <paludis/util/join.hh>
#include <paludis/util/make_named_values.hh>
#include <paludis/util/make_shared_copy.hh>
#include <paludis/util/simple_visitor_cast.hh>
#include <paludis/util/tribool.hh>
#include <paludis/util/enum_iterator.hh>
#include <paludis/environment.hh>
#include <paludis/notifier_callback.hh>
#include <paludis/package_id.hh>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <list>

using namespace paludis;
using namespace paludis::resolver;

typedef std::unordered_map<NAGIndex, std::shared_ptr<const ChangeOrRemoveDecision>, Hash<NAGIndex> > ChangeOrRemoveIndices;
typedef std::unordered_map<NAGIndex, JobNumber, Hash<NAGIndex> > ChangeOrRemoveJobNumbers;
typedef std::unordered_map<Resolvent, JobNumber, Hash<Resolvent> > FetchJobNumbers;

namespace paludis
{
    template <>
    struct Imp<Orderer>
    {
        const Environment * const env;
        const ResolverFunctions fns;
        const std::shared_ptr<Resolved> resolved;
        ChangeOrRemoveIndices change_or_remove_indices;
        FetchJobNumbers fetch_job_numbers;
        ChangeOrRemoveJobNumbers change_or_remove_job_numbers;

        Imp(
                const Environment * const e,
                const ResolverFunctions & f,
                const std::shared_ptr<Resolved> & r) :
            env(e),
            fns(f),
            resolved(r)
        {
        }
    };
}

Orderer::Orderer(
        const Environment * const e,
        const ResolverFunctions & f,
        const std::shared_ptr<Resolved> & r) :
    Pimp<Orderer>(e, f, r)
{
}

Orderer::~Orderer()
{
}

namespace
{
    typedef std::unordered_set<Resolvent, Hash<Resolvent> > ResolventsSet;

    struct DecisionDispatcher
    {
        const std::shared_ptr<Resolved> resolved;
        ResolventsSet & ignore_dependencies_from_resolvents;
        ChangeOrRemoveIndices & change_or_remove_indices;
        const Resolvent resolvent;
        const std::shared_ptr<const Decision> decision;

        DecisionDispatcher(
                const std::shared_ptr<Resolved> & r,
                ResolventsSet & i,
                ChangeOrRemoveIndices & c,
                const Resolvent & v,
                const std::shared_ptr<const Decision> & d) :
            resolved(r),
            ignore_dependencies_from_resolvents(i),
            change_or_remove_indices(c),
            resolvent(v),
            decision(d)
        {
        }

        bool visit(const UnableToMakeDecision &)
        {
            if (decision->taken())
                resolved->taken_unable_to_make_decisions()->push_back(
                        std::static_pointer_cast<const UnableToMakeDecision>(decision));
            else
                resolved->untaken_unable_to_make_decisions()->push_back(
                        std::static_pointer_cast<const UnableToMakeDecision>(decision));

            ignore_dependencies_from_resolvents.insert(resolvent);

            return false;
        }

        bool visit(const NothingNoChangeDecision &)
        {
            resolved->nag()->add_node(make_named_values<NAGIndex>(
                        n::resolvent() = resolvent,
                        n::role() = nir_done
                        ));
            return true;
        }

        bool visit(const ExistingNoChangeDecision &)
        {
            resolved->nag()->add_node(make_named_values<NAGIndex>(
                        n::resolvent() = resolvent,
                        n::role() = nir_done
                        ));
            return true;
        }

        bool visit(const ChangesToMakeDecision &)
        {
            if (decision->taken())
            {
                NAGIndex fetched_index(make_named_values<NAGIndex>(
                            n::resolvent() = resolvent,
                            n::role() = nir_fetched
                            ));
                resolved->nag()->add_node(fetched_index);
                change_or_remove_indices.insert(std::make_pair(fetched_index,
                            std::static_pointer_cast<const ChangeOrRemoveDecision>(decision)));

                NAGIndex done_index(make_named_values<NAGIndex>(
                            n::resolvent() = resolvent,
                            n::role() = nir_done
                            ));
                resolved->nag()->add_node(done_index);
                change_or_remove_indices.insert(std::make_pair(done_index,
                            std::static_pointer_cast<const ChangeOrRemoveDecision>(decision)));

                resolved->nag()->add_edge(done_index, fetched_index,
                        make_named_values<NAGEdgeProperties>(
                            n::build() = true,
                            n::build_all_met() = false,
                            n::run() = false,
                            n::run_all_met() = true
                            ));

                return true;
            }
            else
            {
                resolved->untaken_change_or_remove_decisions()->push_back(
                        std::static_pointer_cast<const ChangesToMakeDecision>(decision));
                return false;
            }
        }

        bool visit(const RemoveDecision &)
        {
            if (decision->taken())
            {
                NAGIndex index(make_named_values<NAGIndex>(
                            n::resolvent() = resolvent,
                            n::role() = nir_done
                            ));
                resolved->nag()->add_node(index);
                change_or_remove_indices.insert(std::make_pair(index,
                            std::static_pointer_cast<const ChangeOrRemoveDecision>(decision)));
                return true;
            }
            else
            {
                resolved->untaken_change_or_remove_decisions()->push_back(
                        std::static_pointer_cast<const ChangeOrRemoveDecision>(decision));
                return false;
            }
        }

        bool visit(const BreakDecision & d)
        {
            if (d.required_confirmations_if_any())
                resolved->taken_unconfirmed_decisions()->push_back(
                        std::static_pointer_cast<const BreakDecision>(decision));
            return false;
        }
    };

    struct EdgesFromReasonVisitor
    {
        const std::shared_ptr<NAG> nag;
        const ResolventsSet & ignore_dependencies_from_resolvents;
        const Resolvent resolvent;
        const std::function<NAGIndexRole (const Resolvent &)> role_for_fetching;

        EdgesFromReasonVisitor(
                const std::shared_ptr<NAG> & n,
                const ResolventsSet & i,
                const Resolvent & v,
            const std::function<NAGIndexRole (const Resolvent &)> & f) :
            nag(n),
            ignore_dependencies_from_resolvents(i),
            resolvent(v),
            role_for_fetching(f)
        {
        }

        void visit(const DependencyReason & r)
        {
            /* we may be constrained by a dep from a package that was changed
             * from a non error decision to an unable to make decision */
            if (ignore_dependencies_from_resolvents.end() != ignore_dependencies_from_resolvents.find(r.from_resolvent()))
                return;

            /* what sort of dep are we? */
            LabelsClassifier classifier;
            for (DependenciesLabelSequence::ConstIterator l(r.sanitised_dependency().active_dependency_labels()->begin()),
                    l_end(r.sanitised_dependency().active_dependency_labels()->end()) ;
                    l != l_end ; ++l)
                (*l)->accept(classifier);

            if (classifier.includes_buildish || classifier.includes_non_post_runish)
            {
                bool normal(true);
                if (r.sanitised_dependency().spec().if_block())
                    if (! r.sanitised_dependency().spec().if_block()->strong())
                        normal = false;

                NAGIndex from(make_named_values<NAGIndex>(
                            n::resolvent() = r.from_resolvent(),
                            n::role() = classifier.includes_fetch ? role_for_fetching(r.from_resolvent()) : nir_done
                            ));

                NAGIndex to(make_named_values<NAGIndex>(
                            n::resolvent() = resolvent,
                            n::role() = nir_done
                            ));

                if (normal)
                {
                    nag->add_edge(from, to,
                            make_named_values<NAGEdgeProperties>(
                                n::build() = classifier.includes_buildish,
                                n::build_all_met() = r.already_met() || ! classifier.includes_buildish,
                                n::run() = classifier.includes_non_post_runish,
                                n::run_all_met() = r.already_met() || ! classifier.includes_non_post_runish
                                ));
                }
                else
                {
                    nag->add_edge(to, from,
                            make_named_values<NAGEdgeProperties>(
                                n::build() = false,
                                n::build_all_met() = true,
                                n::run() = false,
                                n::run_all_met() = true
                                ));
                }
            }
            else if (classifier.includes_postish)
            {
                /* we won't add a backwards edge, since most post deps dep upon
                 * the thing requiring them anyway */
                // nag->add_edge(resolvent, r.from_resolvent());
            }
            else
                throw InternalError(PALUDIS_HERE, "No classification");
        }

        void visit(const SetReason & r)
        {
            if (r.reason_for_set())
                r.reason_for_set()->accept(*this);
        }

        void visit(const LikeOtherDestinationTypeReason & r)
        {
            if (r.reason_for_other())
                r.reason_for_other()->accept(*this);
        }

        void visit(const PresetReason &)
        {
        }

        void visit(const ViaBinaryReason &)
        {
            /* we do clever binary arrows later, to get binaries that just
             * happen to work anyway right too */
        }

        void visit(const DependentReason & r)
        {
            NAGIndex from(make_named_values<NAGIndex>(
                        n::resolvent() = r.id_and_resolvent_being_removed().resolvent(),
                        n::role() = nir_done
                        ));

            NAGIndex to(make_named_values<NAGIndex>(
                        n::resolvent() = resolvent,
                        n::role() = nir_done
                        ));

            nag->add_edge(from, to,
                    make_named_values<NAGEdgeProperties>(
                        n::build() = false,
                        n::build_all_met() = true,
                        n::run() = false,
                        n::run_all_met() = true
                        ));
        }

        void visit(const TargetReason &)
        {
        }

        void visit(const WasUsedByReason & r)
        {
            for (ChangeByResolventSequence::ConstIterator i(r.ids_and_resolvents_being_removed()->begin()),
                    i_end(r.ids_and_resolvents_being_removed()->end()) ;
                    i != i_end ; ++i)
            {
                NAGIndex to(make_named_values<NAGIndex>(
                            n::resolvent() = i->resolvent(),
                            n::role() = nir_done
                            ));

                NAGIndex from(make_named_values<NAGIndex>(
                            n::resolvent() = resolvent,
                            n::role() = nir_done
                            ));

                nag->add_edge(from, to,
                        make_named_values<NAGEdgeProperties>(
                            n::build() = false,
                            n::build_all_met() = true,
                            n::run() = false,
                            n::run_all_met() = true
                            ));
            }
        }
    };

    bool no_build_dependencies(
            const Set<NAGIndex> & indices,
            const NAG & nag)
    {
        for (Set<NAGIndex>::ConstIterator r(indices.begin()), r_end(indices.end()) ;
                r != r_end ; ++r)
            for (NAG::EdgesFromConstIterator e(nag.begin_edges_from(*r)), e_end(nag.end_edges_from(*r)) ;
                    e != e_end ; ++e)
                if (e->second.build())
                    return false;

        return true;
    }
}

Tribool
Orderer::_order_early(const NAGIndex & i) const
{
    return _imp->fns.order_early_fn()(*_imp->resolved->resolutions_by_resolvent()->find(i.resolvent()));
}

void
Orderer::resolve()
{
    Context context("When resolving ordering:");

    _imp->env->trigger_notifier_callback(NotifierCallbackResolverStageEvent("Nodifying Decisions"));

    ResolventsSet ignore_dependencies_from_resolvents, ignore_edges_from_resolvents;
    for (ResolutionsByResolvent::ConstIterator r(_imp->resolved->resolutions_by_resolvent()->begin()),
            r_end(_imp->resolved->resolutions_by_resolvent()->end()) ;
            r != r_end ; ++r)
    {
        _imp->env->trigger_notifier_callback(NotifierCallbackResolverStepEvent());

        DecisionDispatcher decision_dispatcher(
                _imp->resolved,
                ignore_dependencies_from_resolvents,
                _imp->change_or_remove_indices,
                (*r)->resolvent(),
                (*r)->decision());
        if (! (*r)->decision()->accept_returning<bool>(decision_dispatcher))
            ignore_edges_from_resolvents.insert((*r)->resolvent());
    }

    _imp->env->trigger_notifier_callback(NotifierCallbackResolverStageEvent("Building NAG Edges"));

    for (ResolutionsByResolvent::ConstIterator r(_imp->resolved->resolutions_by_resolvent()->begin()),
            r_end(_imp->resolved->resolutions_by_resolvent()->end()) ;
            r != r_end ; ++r)
    {
        _imp->env->trigger_notifier_callback(NotifierCallbackResolverStepEvent());

        if (ignore_dependencies_from_resolvents.end() != ignore_edges_from_resolvents.find((*r)->resolvent()))
            continue;

        _add_binary_cleverness(*r);

        EdgesFromReasonVisitor edges_from_reason_visitor(_imp->resolved->nag(), ignore_dependencies_from_resolvents, (*r)->resolvent(),
                std::bind(&Orderer::_role_for_fetching, this, std::placeholders::_1));
        for (Constraints::ConstIterator c((*r)->constraints()->begin()),
                c_end((*r)->constraints()->end()) ;
                c != c_end ; ++c)
            (*c)->reason()->accept(edges_from_reason_visitor);
    }

    _imp->resolved->nag()->verify_edges();

    const std::function<Tribool (const NAGIndex &)> order_early_fn(std::bind(&Orderer::_order_early, this, std::placeholders::_1));

    _imp->env->trigger_notifier_callback(NotifierCallbackResolverStageEvent("Finding NAG SCCs"));
    const std::shared_ptr<const SortedStronglyConnectedComponents> ssccs(
            _imp->resolved->nag()->sorted_strongly_connected_components(order_early_fn));

    _imp->env->trigger_notifier_callback(NotifierCallbackResolverStageEvent("Ordering SCCs"));
    for (SortedStronglyConnectedComponents::ConstIterator scc(ssccs->begin()), scc_end(ssccs->end()) ;
            scc != scc_end ; ++scc)
    {
        _imp->env->trigger_notifier_callback(NotifierCallbackResolverStepEvent());

        /* some (or none, or all) of the nodes in our SCC are change or remove
         * nodes. this matters for cycle resolution. we identify them now, even
         * though our scc might just contain a single install, rather than
         * adding in extra useless code for the special easy case. */
        typedef std::unordered_set<NAGIndex, Hash<NAGIndex> > ChangesInSCC;
        ChangesInSCC changes_in_scc;

        for (Set<NAGIndex>::ConstIterator r(scc->nodes()->begin()), r_end(scc->nodes()->end()) ;
                r != r_end ; ++r)
            if (_imp->change_or_remove_indices.end() != _imp->change_or_remove_indices.find(*r))
                changes_in_scc.insert(*r);

        if (changes_in_scc.empty())
        {
            /* two or more installed packages are codependent, but we don't have
             * to care */
        }
        else if (changes_in_scc.size() == 1)
        {
            /* there's only one real package in the component, so there's no
             * need to try anything clever */
            _check_self_deps_and_schedule(*changes_in_scc.begin(),
                    _imp->change_or_remove_indices.find(*changes_in_scc.begin())->second,
                    make_shared_copy(make_named_values<OrdererNotes>(
                            n::cycle_breaking() = ""
                            )));
        }
        else
        {
            Context sub_context("When considering only changes:");

            /* whoop de doo. what do our SCCs look like if we only count change
             * or remove nodes? */
            NAG scc_nag;
            for (ChangesInSCC::const_iterator r(changes_in_scc.begin()), r_end(changes_in_scc.end()) ;
                    r != r_end ; ++r)
            {
                scc_nag.add_node(*r);
                /* we only need edges inside our SCC, and only those to other
                 * change or remove nodes */
                for (NAG::EdgesFromConstIterator e(_imp->resolved->nag()->begin_edges_from(*r)), e_end(_imp->resolved->nag()->end_edges_from(*r)) ;
                        e != e_end ; ++e)
                    if (changes_in_scc.end() != changes_in_scc.find(e->first))
                        scc_nag.add_edge(*r, e->first, e->second);
            }

            scc_nag.verify_edges();

            /* now we try again, hopefully with lots of small SCCs now */
            const std::shared_ptr<const SortedStronglyConnectedComponents> sub_ssccs(scc_nag.sorted_strongly_connected_components(order_early_fn));
            _order_sub_ssccs(scc_nag, *scc, sub_ssccs, true, order_early_fn);
        }
    }
}

void
Orderer::_add_binary_cleverness(const std::shared_ptr<const Resolution> & resolution)
{
    if (resolution->resolvent().destination_type() != dt_create_binary)
        return;

    const ChangesToMakeDecision * changes_decision(simple_visitor_cast<const ChangesToMakeDecision>(*resolution->decision()));
    if (! changes_decision)
        return;

    for (EnumIterator<DestinationType> t, t_end(last_dt) ; t != t_end ; ++t)
    {
        if (*t == dt_create_binary)
            continue;

        Resolvent non_binary_resolvent(resolution->resolvent());
        non_binary_resolvent.destination_type() = *t;

        ResolutionsByResolvent::ConstIterator non_binary_resolution(_imp->resolved->resolutions_by_resolvent()->find(non_binary_resolvent));
        if (_imp->resolved->resolutions_by_resolvent()->end() == non_binary_resolution)
            continue;

        ChangesToMakeDecision * non_binary_changes_decision(simple_visitor_cast<ChangesToMakeDecision>(*(*non_binary_resolution)->decision()));
        if (! non_binary_changes_decision)
            continue;

        if (changes_decision->origin_id() != non_binary_changes_decision->origin_id())
            throw InternalError(PALUDIS_HERE, "chose different origin ids: " + stringify(*changes_decision->origin_id()) + " vs "
                    + stringify(*non_binary_changes_decision->origin_id()));
        non_binary_changes_decision->set_via_new_binary_in(changes_decision->destination()->repository());

        NAGIndex from(make_named_values<NAGIndex>(
                    n::resolvent() = non_binary_resolvent,
                    n::role() = nir_fetched /* can't fetch until our origin exists */
                    ));

        NAGIndex to(make_named_values<NAGIndex>(
                    n::resolvent() = resolution->resolvent(),
                    n::role() = nir_done
                    ));

        _imp->resolved->nag()->add_edge(from, to,
                make_named_values<NAGEdgeProperties>(
                    n::build() = true,
                    n::build_all_met() = false,
                    n::run() = false,
                    n::run_all_met() = true
                    ));
    }
}

namespace
{
    std::string nice_index(const NAGIndex & x)
    {
        std::string result(stringify(x.resolvent().package()) + stringify(x.resolvent().slot()));
        if (x.role() == nir_fetched)
            result = result + " (fetch)";
        return result;
    }
}

void
Orderer::_order_sub_ssccs(
        const NAG & scc_nag,
        const StronglyConnectedComponent & top_scc,
        const std::shared_ptr<const SortedStronglyConnectedComponents> & sub_ssccs,
        const bool can_recurse,
        const std::function<Tribool (const NAGIndex &)> & order_early_fn)
{
    Context context("When ordering SSCCs" + std::string(can_recurse ? " for the first time" : " for the second time") + ":");

    for (SortedStronglyConnectedComponents::ConstIterator sub_scc(sub_ssccs->begin()), sub_scc_end(sub_ssccs->end()) ;
            sub_scc != sub_scc_end ; ++sub_scc)
    {
        _imp->env->trigger_notifier_callback(NotifierCallbackResolverStepEvent());

        if (sub_scc->nodes()->size() == 1)
        {
            /* yay. it's all on its own. */
            _check_self_deps_and_schedule(*sub_scc->nodes()->begin(),
                    _imp->change_or_remove_indices.find(*sub_scc->nodes()->begin())->second,
                    make_shared_copy(make_named_values<OrdererNotes>(
                            n::cycle_breaking() = (can_recurse ?
                                "In dependency cycle with existing packages: " + join(scc_nag.begin_nodes(), scc_nag.end_nodes(), ", ", nice_index) :
                                "In dependency cycle with: " + join(top_scc.nodes()->begin(), top_scc.nodes()->end(), ", ", nice_index))
                            )));
        }
        else if (no_build_dependencies(*sub_scc->nodes(), scc_nag))
        {
            /* what's that, timmy? we have directly codependent nodes?
             * well i'm jolly glad that's because they're run
             * dependency cycles which we can order however we like! */
            for (Set<NAGIndex>::ConstIterator r(sub_scc->nodes()->begin()), r_end(sub_scc->nodes()->end()) ;
                    r != r_end ; ++r)
                _check_self_deps_and_schedule(*r,
                        _imp->change_or_remove_indices.find(*r)->second,
                        make_shared_copy(make_named_values<OrdererNotes>(
                                n::cycle_breaking() = "In run dependency cycle with: " + join(
                                    sub_scc->nodes()->begin(), sub_scc->nodes()->end(), ", ", nice_index) + (can_recurse ?
                                    " in dependency cycle with " + join(top_scc.nodes()->begin(), top_scc.nodes()->end(), ", ", nice_index) : "")
                                )));
        }
        else if (can_recurse)
        {
            /* no, at least one of the deps is a build dep. let's try
             * this whole mess again, except without any edges for
             * dependencies that're already met */
            NAG scc_nag_without_met_deps;
            for (Set<NAGIndex>::ConstIterator r(sub_scc->nodes()->begin()), r_end(sub_scc->nodes()->end()) ;
                    r != r_end ; ++r)
            {
                scc_nag_without_met_deps.add_node(*r);
                for (NAG::EdgesFromConstIterator e(scc_nag.begin_edges_from(*r)), e_end(scc_nag.end_edges_from(*r)) ;
                        e != e_end ; ++e)
                    if (sub_scc->nodes()->end() != sub_scc->nodes()->find(e->first))
                        if ((! e->second.build_all_met()) || (! e->second.run_all_met()))
                            scc_nag_without_met_deps.add_edge(*r, e->first, make_named_values<NAGEdgeProperties>(
                                        n::build() = e->second.build() && ! e->second.build_all_met(),
                                        n::build_all_met() = e->second.build_all_met(),
                                        n::run() = e->second.run() && ! e->second.run_all_met(),
                                        n::run_all_met() = e->second.run_all_met()
                                        ));
            }

            scc_nag_without_met_deps.verify_edges();

            const std::shared_ptr<const SortedStronglyConnectedComponents> sub_ssccs_without_met_deps(
                    scc_nag_without_met_deps.sorted_strongly_connected_components(order_early_fn));
            _order_sub_ssccs(scc_nag_without_met_deps, top_scc, sub_ssccs_without_met_deps, false, order_early_fn);
        }
        else
        {
            for (Set<NAGIndex>::ConstIterator r(sub_scc->nodes()->begin()), r_end(sub_scc->nodes()->end()) ;
                    r != r_end ; ++r)
            {
                if (r->role() == nir_fetched && sub_scc->nodes()->end() != std::find(sub_scc->nodes()->begin(),
                            sub_scc->nodes()->end(), make_named_values<NAGIndex>(
                                n::resolvent() = r->resolvent(),
                                n::role() = nir_done
                                )))
                    continue;

                _imp->resolved->taken_unorderable_decisions()->push_back(
                        _imp->change_or_remove_indices.find(*r)->second,
                        make_shared_copy(make_named_values<OrdererNotes>(
                                n::cycle_breaking() = "In unsolvable cycle with " + join(
                                    top_scc.nodes()->begin(), top_scc.nodes()->end(), ", ", nice_index))));
            }
        }
    }
}

namespace
{
    typedef std::unordered_set<NAGIndex, Hash<NAGIndex> > RecursedRequirements;

    void populate_requirements(
            const std::shared_ptr<const NAG> & nag,
            const ChangeOrRemoveJobNumbers & change_or_remove_job_numbers,
            const NAGIndex & index,
            const std::shared_ptr<JobRequirements> & requirements,
            const bool is_uninstall,
            const bool is_fetch,
            const bool recursing,
            RecursedRequirements & recursed)
    {
        JobRequirementIfs basic_required_ifs;
        if (is_fetch)
            basic_required_ifs += jri_fetching;

        if (! recursing)
            for (NAG::EdgesFromConstIterator e(nag->begin_edges_from(index)),
                    e_end(nag->end_edges_from(index)) ;
                    e != e_end ; ++e)
            {
                if ((! e->second.build_all_met()) || (! e->second.run_all_met()) || is_uninstall)
                {
                    ChangeOrRemoveJobNumbers::const_iterator n(change_or_remove_job_numbers.find(e->first));
                    if (n != change_or_remove_job_numbers.end())
                        requirements->push_back(make_named_values<JobRequirement>(
                                    n::job_number() = n->second,
                                    n::required_if() = basic_required_ifs + jri_require_for_satisfied
                                    ));
                }
            }

        if ((! is_uninstall) && recursed.insert(index).second)
            for (NAG::EdgesFromConstIterator e(nag->begin_edges_from(index)),
                    e_end(nag->end_edges_from(index)) ;
                    e != e_end ; ++e)
            {
                ChangeOrRemoveJobNumbers::const_iterator n(change_or_remove_job_numbers.find(e->first));
                if (n != change_or_remove_job_numbers.end())
                    requirements->push_back(make_named_values<JobRequirement>(
                                n::job_number() = n->second,
                                n::required_if() = basic_required_ifs + jri_require_for_independent
                                ));
                populate_requirements(nag, change_or_remove_job_numbers, e->first, requirements,
                        is_uninstall, is_fetch, true, recursed);
            }
    }
}

void
Orderer::_check_self_deps_and_schedule(
        const NAGIndex & index,
        const std::shared_ptr<const ChangeOrRemoveDecision> & d,
        const std::shared_ptr<OrdererNotes> & n)
{
    /* do we dep directly upon ourself? */
    bool direct_self_dep(false), self_dep_is_met(true), self_dep_is_not_build(true);
    for (NAG::EdgesFromConstIterator e(_imp->resolved->nag()->begin_edges_from(index)),
            e_end(_imp->resolved->nag()->end_edges_from(index)) ;
            e != e_end ; ++e)
    {
        if (e->first == index)
        {
            direct_self_dep = true;
            self_dep_is_met = self_dep_is_met && e->second.build_all_met() && e->second.run_all_met();
            self_dep_is_not_build = self_dep_is_not_build && ! e->second.build();
        }
    }

    if (direct_self_dep)
    {
        if (! n->cycle_breaking().empty())
            n->cycle_breaking().append("; ");

        if (self_dep_is_met)
            n->cycle_breaking().append("Self dependent (already met)");
        else if (self_dep_is_not_build)
            n->cycle_breaking().append("Self dependent (runtime only)");
        else
            n->cycle_breaking().append("Self dependent (unsolvable)");
    }

    if (direct_self_dep && ! self_dep_is_met && ! self_dep_is_not_build)
    {
        _imp->resolved->taken_unorderable_decisions()->push_back(
                _imp->change_or_remove_indices.find(index)->second,
                n);
    }
    else
        _schedule(index, d, n);
}

namespace
{
    PackageDepSpec make_origin_spec(const ChangesToMakeDecision & changes_to_make_decision)
    {
        PartiallyMadePackageDepSpec result(changes_to_make_decision.origin_id()->uniquely_identifying_spec());

        if (changes_to_make_decision.if_via_new_binary_in())
            result.in_repository(*changes_to_make_decision.if_via_new_binary_in());

        return result;
    }

    struct ExtraScheduler
    {
        const std::shared_ptr<const Resolved> resolved;
        FetchJobNumbers & fetch_job_numbers;
        ChangeOrRemoveJobNumbers & change_or_remove_job_numbers;
        const NAGIndex index;

        ExtraScheduler(
                const std::shared_ptr<const Resolved> & r,
                FetchJobNumbers & f,
                ChangeOrRemoveJobNumbers & i,
                const NAGIndex & v) :
            resolved(r),
            fetch_job_numbers(f),
            change_or_remove_job_numbers(i),
            index(v)
        {
        }

        void visit(const ChangesToMakeDecision & changes_to_make_decision) const
        {
            switch (index.role())
            {
                case nir_done:
                    {
                        FetchJobNumbers::const_iterator fetch_job_n(fetch_job_numbers.find(index.resolvent()));
                        if (fetch_job_n == fetch_job_numbers.end())
                            throw InternalError(PALUDIS_HERE, "haven't scheduled the fetch for " + stringify(index.resolvent()) + " yet");

                        resolved->job_lists()->pretend_job_list()->append(std::make_shared<PretendJob>(
                                        changes_to_make_decision.origin_id()->uniquely_identifying_spec()));

                        const std::shared_ptr<JobRequirements> requirements(std::make_shared<JobRequirements>());
                        requirements->push_back(make_named_values<JobRequirement>(
                                    n::job_number() = fetch_job_n->second,
                                    n::required_if() = JobRequirementIfs() + jri_require_for_satisfied + jri_require_for_independent
                                        + jri_require_always + jri_fetching
                                    ));

                        RecursedRequirements recursed;
                        populate_requirements(
                                resolved->nag(),
                                change_or_remove_job_numbers,
                                index,
                                requirements,
                                false,
                                false,
                                false,
                                recursed
                                );

                        const std::shared_ptr<Sequence<PackageDepSpec> > replacing(std::make_shared<Sequence<PackageDepSpec>>());
                        for (PackageIDSequence::ConstIterator i(changes_to_make_decision.destination()->replacing()->begin()),
                                i_end(changes_to_make_decision.destination()->replacing()->end()) ;
                                i != i_end ; ++i)
                            replacing->push_back((*i)->uniquely_identifying_spec());

                        JobNumber install_job_n(resolved->job_lists()->execute_job_list()->append(std::make_shared<InstallJob>(
                                            requirements,
                                            make_origin_spec(changes_to_make_decision),
                                            changes_to_make_decision.destination()->repository(),
                                            changes_to_make_decision.resolvent().destination_type(),
                                            replacing
                                            )));

                        change_or_remove_job_numbers.insert(std::make_pair(index, install_job_n));
                    }
                    return;

                case nir_fetched:
                    {
                        const std::shared_ptr<JobRequirements> requirements(std::make_shared<JobRequirements>());

                        RecursedRequirements recursed;
                        populate_requirements(
                                resolved->nag(),
                                change_or_remove_job_numbers,
                                index,
                                requirements,
                                false,
                                true,
                                false,
                                recursed
                                );

                        JobNumber fetch_job_n(resolved->job_lists()->execute_job_list()->append(std::make_shared<FetchJob>(
                                            requirements,
                                            make_origin_spec(changes_to_make_decision))));
                        fetch_job_numbers.insert(std::make_pair(index.resolvent(), fetch_job_n));
                    }
                    return;

                case last_nir:
                    break;
            }

            throw InternalError(PALUDIS_HERE, "bad index.role");
        }

        void visit(const RemoveDecision & remove_decision) const
        {
            const std::shared_ptr<Sequence<PackageDepSpec> > removing(std::make_shared<Sequence<PackageDepSpec>>());
            for (PackageIDSequence::ConstIterator i(remove_decision.ids()->begin()),
                    i_end(remove_decision.ids()->end()) ;
                    i != i_end ; ++i)
                removing->push_back((*i)->uniquely_identifying_spec());

            const std::shared_ptr<JobRequirements> requirements(std::make_shared<JobRequirements>());
            RecursedRequirements recursed;
            populate_requirements(
                    resolved->nag(),
                    change_or_remove_job_numbers,
                    index,
                    requirements,
                    true,
                    false,
                    false,
                    recursed
                    );

            JobNumber uninstall_job_n(resolved->job_lists()->execute_job_list()->append(std::make_shared<UninstallJob>(
                                requirements,
                                removing
                                )));

            change_or_remove_job_numbers.insert(std::make_pair(index, uninstall_job_n));
        }
    };
}

void
Orderer::_schedule(
        const NAGIndex & index,
        const std::shared_ptr<const ChangeOrRemoveDecision> & d,
        const std::shared_ptr<const OrdererNotes> & n)
{
    do
    {
        switch (index.role())
        {
            case nir_done:
                _imp->resolved->taken_change_or_remove_decisions()->push_back(d, n);
                if (d->required_confirmations_if_any())
                    _imp->resolved->taken_unconfirmed_decisions()->push_back(d);
                continue;

            case nir_fetched:
                continue;

            case last_nir:
                break;
        }

        throw InternalError(PALUDIS_HERE, "bad index.role");
    } while (false);

    d->accept(ExtraScheduler(_imp->resolved, _imp->fetch_job_numbers, _imp->change_or_remove_job_numbers, index));
}

namespace
{
    struct RoleForFetchingVisitor
    {
        NAGIndexRole visit(const ChangesToMakeDecision &) const
        {
            return nir_fetched;
        }

        NAGIndexRole visit(const ExistingNoChangeDecision &) const
        {
            return nir_done;
        }

        NAGIndexRole visit(const UnableToMakeDecision &) const
        {
            return nir_done;
        }

        NAGIndexRole visit(const BreakDecision &) const
        {
            return nir_done;
        }

        NAGIndexRole visit(const RemoveDecision &) const
        {
            return nir_done;
        }

        NAGIndexRole visit(const NothingNoChangeDecision &) const
        {
            return nir_done;
        }
    };
}

NAGIndexRole
Orderer::_role_for_fetching(
        const Resolvent & resolvent) const
{
    return (*_imp->resolved->resolutions_by_resolvent()->find(
                resolvent))->decision()->accept_returning<NAGIndexRole>(RoleForFetchingVisitor());
}