2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * SAT based dependency solver
26 #define RULES_BLOCK 63
27 #define REGARD_RECOMMENDS_OF_INSTALLED_ITEMS 0
31 solver_dep_installed(Solver *solv, Id dep)
33 /* disable for now, splitprovides don't work anyway and it breaks
36 Pool *pool = solv->pool;
41 Reldep *rd = GETRELDEP(pool, dep);
42 if (rd->flags == REL_AND)
44 if (!solver_dep_installed(solv, rd->name))
46 return solver_dep_installed(solv, rd->evr);
48 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
49 return solver_dep_installed(solv, rd->evr);
51 FOR_PROVIDES(p, pp, dep)
53 if (p == SYSTEMSOLVABLE || (solv->installed && pool->solvables[p].repo == solv->installed))
61 /* this mirrors solver_dep_fulfilled but uses map m instead of the decisionmap */
63 dep_possible(Solver *solv, Id dep, Map *m)
65 Pool *pool = solv->pool;
70 Reldep *rd = GETRELDEP(pool, dep);
71 if (rd->flags == REL_AND)
73 if (!dep_possible(solv, rd->name, m))
75 return dep_possible(solv, rd->evr, m);
77 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
78 return solver_dep_installed(solv, rd->evr);
80 FOR_PROVIDES(p, pp, dep)
88 /*-----------------------------------------------------------------*/
95 printruleelement(Solver *solv, int type, Rule *r, Id v)
97 Pool *pool = solv->pool;
101 s = pool->solvables + -v;
102 POOL_DEBUG(type, " !%s [%d]", solvable2str(pool, s), -v);
106 s = pool->solvables + v;
107 POOL_DEBUG(type, " %s [%d]", solvable2str(pool, s), v);
112 POOL_DEBUG(type, " (w1)");
114 POOL_DEBUG(type, " (w2)");
116 if (solv->decisionmap[s - pool->solvables] > 0)
117 POOL_DEBUG(type, " Install.level%d", solv->decisionmap[s - pool->solvables]);
118 if (solv->decisionmap[s - pool->solvables] < 0)
119 POOL_DEBUG(type, " Conflict.level%d", -solv->decisionmap[s - pool->solvables]);
120 POOL_DEBUG(type, "\n");
129 printrule(Solver *solv, int type, Rule *r)
131 Pool *pool = solv->pool;
135 if (r >= solv->rules && r < solv->rules + solv->nrules) /* r is a solver rule */
136 POOL_DEBUG(type, "Rule #%d:", (int)(r - solv->rules));
138 POOL_DEBUG(type, "Rule:"); /* r is any rule */
140 POOL_DEBUG(type, " (disabled)");
141 POOL_DEBUG(type, "\n");
145 /* print direct literal */
147 else if (r->d == ID_NULL)
151 /* binary rule --> print w2 as second literal */
155 /* every other which is in d */
156 v = solv->pool->whatprovidesdata[r->d + i - 1];
159 printruleelement(solv, type, r, v);
161 POOL_DEBUG(type, " next: %d %d\n", r->n1, r->n2);
165 printruleclass(Solver *solv, int type, Rule *r)
167 Pool *pool = solv->pool;
168 if (r - solv->rules >= solv->learntrules)
169 POOL_DEBUG(type, "LEARNT ");
170 else if (r - solv->rules >= solv->weakrules)
171 POOL_DEBUG(type, "WEAK ");
172 else if (r - solv->rules >= solv->systemrules)
173 POOL_DEBUG(type, "SYSTEM ");
174 else if (r - solv->rules >= solv->jobrules)
175 POOL_DEBUG(type, "JOB ");
176 printrule(solv, type, r);
180 /*-----------------------------------------------------------------*/
186 static Pool *unifyrules_sortcmp_data;
189 * compare rules for unification sort
193 unifyrules_sortcmp(const void *ap, const void *bp)
195 Pool *pool = unifyrules_sortcmp_data;
196 Rule *a = (Rule *)ap;
197 Rule *b = (Rule *)bp;
203 return x; /* p differs */
206 if (a->d == 0 && b->d == 0)
207 return a->w2 - b->w2; /* assertion: return w2 diff */
209 if (a->d == 0) /* a is assertion, b not */
211 x = a->w2 - pool->whatprovidesdata[b->d];
215 if (b->d == 0) /* b is assertion, a not */
217 x = pool->whatprovidesdata[a->d] - b->w2;
221 /* compare whatprovidesdata */
222 ad = pool->whatprovidesdata + a->d;
223 bd = pool->whatprovidesdata + b->d;
225 if ((x = *ad++ - *bd++) != 0)
236 unifyrules(Solver *solv)
238 Pool *pool = solv->pool;
242 if (solv->nrules <= 1) /* nothing to unify */
245 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- unifyrules -----\n");
247 /* sort rules first */
248 unifyrules_sortcmp_data = solv->pool;
249 qsort(solv->rules + 1, solv->nrules - 1, sizeof(Rule), unifyrules_sortcmp);
256 for (i = j = 1, ir = solv->rules + 1; i < solv->nrules; i++, ir++)
258 if (jr && !unifyrules_sortcmp(ir, jr))
259 continue; /* prune! */
260 jr = solv->rules + j++; /* keep! */
265 /* reduced count from nrules to j rules */
266 POOL_DEBUG(SAT_DEBUG_STATS, "pruned rules from %d to %d\n", solv->nrules, j);
268 /* adapt rule buffer */
270 solv->rules = (Rule *)xrealloc(solv->rules, ((solv->nrules + RULES_BLOCK) & ~RULES_BLOCK) * sizeof(Rule));
271 IF_POOLDEBUG (SAT_DEBUG_STATS)
278 for (i = 1; i < solv->nrules; i++)
285 dp = solv->pool->whatprovidesdata + r->d;
290 POOL_DEBUG(SAT_DEBUG_STATS, " binary: %d\n", binr);
291 POOL_DEBUG(SAT_DEBUG_STATS, " normal: %d, %d literals\n", solv->nrules - 1 - binr, lits);
293 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- unifyrules end -----\n");
303 hashrule(Solver *solv, Id p, Id d, int n)
305 unsigned int x = (unsigned int)p;
309 return (x * 37) ^ (unsigned int)d;
310 dp = solv->pool->whatprovidesdata + d;
312 x = (x * 37) ^ (unsigned int)*dp++;
320 * p = direct literal; always < 0 for installed rpm rules
321 * d, if < 0 direct literal, if > 0 offset into whatprovides, if == 0 rule is assertion (look at p only)
324 * A requires b, b provided by B1,B2,B3 => (-A|B1|B2|B3)
326 * p < 0 : pkg id of A
327 * d > 0 : Offset in whatprovidesdata (list of providers of b)
329 * A conflicts b, b provided by B1,B2,B3 => (-A|-B1), (-A|-B2), (-A|-B3)
330 * p < 0 : pkg id of A
331 * d < 0 : Id of solvable (e.g. B1)
333 * d == 0: unary rule, assertion => (A) or (-A)
335 * Install: p > 0, d = 0 (A) user requested install
336 * Remove: p < 0, d = 0 (-A) user requested remove
337 * Requires: p < 0, d > 0 (-A|B1|B2|...) d: <list of providers for requirement of p>
338 * Updates: p > 0, d > 0 (A|B1|B2|...) d: <list of updates for solvable p>
339 * Conflicts: p < 0, d < 0 (-A|-B) either p (conflict issuer) or d (conflict provider) (binary rule)
340 * ? p > 0, d < 0 (A|-B)
341 * No-op ?: p = 0, d = 0 (null) (used as policy rule placeholder)
345 * Direct assertion (no watch needed)( if d <0 ) --> d = 0, w1 = p, w2 = 0
346 * Binary rule: p = first literal, d = 0, w2 = second literal, w1 = p
347 * every other : w1 = p, w2 = whatprovidesdata[d];
348 * Disabled rule: w1 = 0
350 * always returns a rule for non-rpm rules
354 addrule(Solver *solv, Id p, Id d)
356 Pool *pool = solv->pool;
360 int n = 0; /* number of literals in rule - 1
361 0 = direct assertion (single literal)
365 /* it often happenes that requires lead to adding the same rpm rule
366 * multiple times, so we prune those duplicates right away to make
367 * the work for unifyrules a bit easier */
369 if (solv->nrules && !solv->jobrules)
371 r = solv->rules + solv->nrules - 1; /* get the last added rule */
372 if (r->p == p && r->d == d && d != 0) /* identical and not user requested */
378 /* always a binary rule */
380 return 0; /* ignore self conflict */
385 for (dp = pool->whatprovidesdata + d; *dp; dp++, n++)
387 return 0; /* rule is self-fulfilling */
389 d = dp[-1]; /* take single literal */
392 if (n == 0 && !solv->jobrules)
394 /* this is a rpm rule assertion, we do not have to allocate it */
395 /* it can be identified by a level of 1 and a zero reason */
396 /* we must not drop those rules from the decisionq when rewinding! */
399 if (solv->decisionmap[-p] > 0 || solv->decisionmap[-p] < -1)
401 if (solv->decisionmap[-p])
402 return 0; /* already got that one */
403 queue_push(&solv->decisionq, p);
404 queue_push(&solv->decisionq_why, 0);
405 solv->decisionmap[-p] = -1;
410 /* smallest literal first so we can find dups */
414 n = 1; /* re-set n, was used as temp var */
417 /* check if the last added rule is exactly the same as what we're looking for. */
418 if (r && n == 1 && !r->d && r->p == p && r->w2 == d)
419 return r; /* binary rule */
421 if (r && n > 1 && r->d && r->p == p)
423 /* Rule where d is an offset in whatprovidesdata */
427 dp2 = pool->whatprovidesdata + r->d;
428 for (dp = pool->whatprovidesdata + d; *dp; dp++, dp2++)
439 /* check and extend rule buffer */
440 if ((solv->nrules & RULES_BLOCK) == 0)
442 solv->rules = (Rule *)xrealloc(solv->rules, (solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
445 r = solv->rules + solv->nrules++; /* point to rule space */
450 /* direct assertion, no watch needed */
466 r->w2 = pool->whatprovidesdata[d];
471 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
473 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, " Add rule: ");
474 printrule(solv, SAT_DEBUG_RULE_CREATION, r);
481 disablerule(Solver *solv, Rule *r)
487 enablerule(Solver *solv, Rule *r)
489 if (r->d == 0 || r->w2 != r->p)
492 r->w1 = solv->pool->whatprovidesdata[r->d];
496 /**********************************************************************************/
498 /* a problem is an item on the solver's problem list. It can either be >0, in that
499 * case it is a system (upgrade) rule, or it can be <0, which makes it refer to a job
500 * consisting of multiple job rules.
504 disableproblem(Solver *solv, Id v)
512 disablerule(solv, solv->rules + v);
516 jp = solv->ruletojob.elements;
517 for (i = solv->jobrules, r = solv->rules + i; i < solv->systemrules; i++, r++, jp++)
519 disablerule(solv, r);
523 enableproblem(Solver *solv, Id v)
531 enablerule(solv, solv->rules + v);
535 jp = solv->ruletojob.elements;
536 for (i = solv->jobrules, r = solv->rules + i; i < solv->systemrules; i++, r++, jp++)
542 printproblem(Solver *solv, Id v)
544 Pool *pool = solv->pool;
550 printrule(solv, SAT_DEBUG_SOLUTIONS, solv->rules + v);
554 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "JOB %d\n", v);
555 jp = solv->ruletojob.elements;
556 for (i = solv->jobrules, r = solv->rules + i; i < solv->systemrules; i++, r++, jp++)
559 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "- ");
560 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
562 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "ENDJOB\n");
568 /************************************************************************/
570 /* go through system and job rules and add direct assertions
571 * to the decisionqueue. If we find a conflict, disable rules and
572 * add them to problem queue.
573 * (disablerules is always true for the cases when we get called)
576 makeruledecisions(Solver *solv)
578 Pool *pool = solv->pool;
584 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- makeruledecisions ; size decisionq: %d -----\n",solv->decisionq.count);
586 decisionstart = solv->decisionq.count;
587 /* rpm rules don't have assertions, so we can start with the job
589 for (ri = solv->jobrules, r = solv->rules + ri; ri < solv->nrules; ri++, r++)
591 if (!r->w1 || r->w2) /* disabled or no assertion */
595 if (solv->decisionmap[vv] == 0)
597 queue_push(&solv->decisionq, v);
598 queue_push(&solv->decisionq_why, r - solv->rules);
599 solv->decisionmap[vv] = v > 0 ? 1 : -1;
600 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
602 Solvable *s = solv->pool->solvables + vv;
604 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "conflicting %s (assertion)\n", solvable2str(solv->pool, s));
606 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "installing %s (assertion)\n", solvable2str(solv->pool, s));
610 if (v > 0 && solv->decisionmap[vv] > 0)
612 if (v < 0 && solv->decisionmap[vv] < 0)
614 /* found a conflict! */
615 if (ri >= solv->learntrules)
617 /* cannot happen, as this would mean that the problem
618 * was not solvable, so we wouldn't have created the
619 * learnt rule at all */
622 /* if we are weak, just disable ourself */
623 if (ri >= solv->weakrules)
625 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "assertion conflict, but I am weak, disabling ");
626 printrule(solv, SAT_DEBUG_UNSOLVABLE, r);
627 disablerule(solv, r);
631 /* only job and system rules left in the decisionq*/
632 /* find the decision which is the "oposite" of the jobrule */
633 for (i = 0; i < solv->decisionq.count; i++)
634 if (solv->decisionq.elements[i] == -v)
636 if (i == solv->decisionq.count)
638 if (solv->decisionq_why.elements[i] == 0)
640 /* conflict with rpm rule, need only disable our rule */
641 if (v < 0 && v != -SYSTEMSOLVABLE)
644 queue_push(&solv->problems, solv->learnt_pool.count);
645 queue_push(&solv->learnt_pool, ri);
646 queue_push(&solv->learnt_pool, v != -SYSTEMSOLVABLE ? -v : v);
647 queue_push(&solv->learnt_pool, 0);
648 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflict with rpm rule, disabling rule #%d\n", ri);
649 if (ri < solv->systemrules)
650 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
653 queue_push(&solv->problems, v);
654 disableproblem(solv, v);
655 queue_push(&solv->problems, 0);
659 queue_push(&solv->problems, solv->learnt_pool.count);
660 queue_push(&solv->learnt_pool, ri);
661 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
662 queue_push(&solv->learnt_pool, 0);
664 /* conflict with another job or system rule */
665 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflicting system/job assertions over literal %d\n", vv);
666 /* push all of our rules asserting this literal on the problem stack */
667 for (i = solv->jobrules, rr = solv->rules + i; i < solv->nrules; i++, rr++)
669 if (!rr->w1 || rr->w2)
671 if (rr->p != vv && rr->p != -vv)
673 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, " - disabling rule #%d\n", i);
675 if (i < solv->systemrules)
676 v = -(solv->ruletojob.elements[i - solv->jobrules] + 1);
677 queue_push(&solv->problems, v);
678 disableproblem(solv, v);
680 queue_push(&solv->problems, 0);
683 while (solv->decisionq.count > decisionstart)
685 v = solv->decisionq.elements[--solv->decisionq.count];
686 --solv->decisionq_why.count;
688 solv->decisionmap[vv] = 0;
690 ri = solv->jobrules - 1;
691 r = solv->rules + ri;
694 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- makeruledecisions end; size decisionq: %d -----\n",solv->decisionq.count);
698 * we have enabled or disabled some of our rules. We now reenable all
699 * of our learnt rules but the ones that were learnt from rules that
703 enabledisablelearntrules(Solver *solv)
705 Pool *pool = solv->pool;
710 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "enabledisablelearntrules called\n");
711 for (i = solv->learntrules, r = solv->rules + i; i < solv->nrules; i++, r++)
713 whyp = solv->learnt_pool.elements + solv->learnt_why.elements[i - solv->learntrules];
714 while ((why = *whyp++) != 0)
716 if (why < 0 || why >= i)
717 abort(); /* cannot reference newer learnt rules */
718 if (!solv->rules[why].w1)
721 /* why != 0: we found a disabled rule, disable the learnt rule */
724 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
726 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "disabling learnt ");
727 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
729 disablerule(solv, r);
731 else if (!why && !r->w1)
733 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
735 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "re-enabling learnt ");
736 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
744 /* FIXME: bad code ahead, replace as soon as possible */
746 disableupdaterules(Solver *solv, Queue *job, int jobidx)
748 Pool *pool = solv->pool;
750 Id how, what, p, *pp;
756 installed = solv->installed;
762 how = job->elements[jobidx];
765 case SOLVER_INSTALL_SOLVABLE:
766 case SOLVER_ERASE_SOLVABLE:
767 case SOLVER_ERASE_SOLVABLE_NAME:
768 case SOLVER_ERASE_SOLVABLE_PROVIDES:
774 /* go through all enabled job rules */
775 MAPZERO(&solv->noupdate);
776 for (i = solv->jobrules; i < solv->systemrules; i++)
779 if (!r->w1) /* disabled? */
781 j = solv->ruletojob.elements[i - solv->jobrules];
785 how = job->elements[j];
786 what = job->elements[j + 1];
789 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
790 s = pool->solvables + what;
791 FOR_PROVIDES(p, pp, s->name)
793 if (pool->solvables[p].name != s->name)
795 if (pool->solvables[p].repo == installed)
796 MAPSET(&solv->noupdate, p - installed->start);
799 case SOLVER_ERASE_SOLVABLE:
800 s = pool->solvables + what;
801 if (s->repo == installed)
802 MAPSET(&solv->noupdate, what - installed->start);
804 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
805 case SOLVER_ERASE_SOLVABLE_PROVIDES:
806 FOR_PROVIDES(p, pp, what)
808 if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
810 if (pool->solvables[p].repo == installed)
811 MAPSET(&solv->noupdate, p - installed->start);
819 /* fixup update rule status */
820 if (solv->allowuninstall)
821 return; /* no update rules at all */
825 /* we just disabled job #jobidx. enable all update rules
826 * that aren't disabled by the remaining job rules */
827 how = job->elements[jobidx];
828 what = job->elements[jobidx + 1];
831 case SOLVER_INSTALL_SOLVABLE:
832 s = pool->solvables + what;
833 FOR_PROVIDES(p, pp, s->name)
835 if (pool->solvables[p].name != s->name)
837 if (pool->solvables[p].repo != installed)
839 if (MAPTST(&solv->noupdate, p - installed->start))
841 r = solv->rules + solv->systemrules + (p - installed->start);
845 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
847 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
848 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
852 case SOLVER_ERASE_SOLVABLE:
853 s = pool->solvables + what;
854 if (s->repo != installed)
856 if (MAPTST(&solv->noupdate, what - installed->start))
858 r = solv->rules + solv->systemrules + (what - installed->start);
862 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
864 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
865 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
868 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
869 case SOLVER_ERASE_SOLVABLE_PROVIDES:
870 FOR_PROVIDES(p, pp, what)
872 if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
874 if (pool->solvables[p].repo != installed)
876 if (MAPTST(&solv->noupdate, p - installed->start))
878 r = solv->rules + solv->systemrules + (p - installed->start);
882 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
884 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
885 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
895 for (i = 0; i < installed->nsolvables; i++)
897 r = solv->rules + solv->systemrules + i;
898 if (r->w1 && MAPTST(&solv->noupdate, i))
899 disablerule(solv, r); /* was enabled, need to disable */
905 * add (install) rules for solvable
906 * for unfulfilled requirements, conflicts, obsoletes,....
907 * add a negative assertion for solvables that are not installable
911 addrpmrulesforsolvable(Solver *solv, Solvable *s, Map *m)
913 Pool *pool = solv->pool;
914 Repo *installed = solv->installed;
928 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable -----\n");
930 queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
931 queue_push(&q, s - pool->solvables); /* push solvable Id */
937 * s: Pointer to solvable
941 if (MAPTST(m, n)) /* continue if already done */
945 s = pool->solvables + n; /* s = Solvable in question */
948 if (installed /* Installed system available */
949 && !solv->fixsystem /* NOT repair errors in rpm dependency graph */
950 && s->repo == installed) /* solvable is installed? */
952 dontfix = 1; /* dont care about broken rpm deps */
955 if (!dontfix && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC && !pool_installable(pool, s))
957 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable\n", solvable2str(pool, s), (Id)(s - pool->solvables));
958 addrule(solv, -n, 0); /* uninstallable */
961 /*-----------------------------------------
962 * check requires of s
967 reqp = s->repo->idarraydata + s->requires;
968 while ((req = *reqp++) != 0) /* go throw all requires */
970 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
973 dp = pool_whatprovides(pool, req);
975 if (*dp == SYSTEMSOLVABLE) /* always installed */
980 /* the strategy here is to not insist on dependencies
981 * that are already broken. so if we find one provider
982 * that was already installed, we know that the
983 * dependency was not broken before so we enforce it */
984 for (i = 0; (p = dp[i]) != 0; i++) /* for all providers */
986 if (pool->solvables[p].repo == installed)
987 break; /* provider was installed */
989 if (!p) /* previously broken dependency */
991 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "ignoring broken requires %s of installed package %s\n", dep2str(pool, req), solvable2str(pool, s));
998 /* nothing provides req! */
999 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable (%s)\n", solvable2str(pool, s), (Id)(s - pool->solvables), dep2str(pool, req));
1000 addrule(solv, -n, 0); /* mark requestor as uninstallable */
1004 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
1006 POOL_DEBUG(SAT_DEBUG_RULE_CREATION," %s requires %s\n", solvable2str(pool, s), dep2str(pool, req));
1007 for (i = 0; dp[i]; i++)
1008 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, " provided by %s\n", solvable2str(pool, pool->solvables + dp[i]));
1011 /* add 'requires' dependency */
1012 /* rule: (-requestor|provider1|provider2|...|providerN) */
1013 addrule(solv, -n, dp - pool->whatprovidesdata);
1015 /* descend the dependency tree */
1016 for (; *dp; dp++) /* loop through all providers */
1018 if (!MAPTST(m, *dp))
1019 queue_push(&q, *dp);
1022 } /* while, requirements of n */
1024 } /* if, requirements */
1026 /* that's all we check for src packages */
1027 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
1030 /*-----------------------------------------
1031 * check conflicts of s
1036 conp = s->repo->idarraydata + s->conflicts;
1037 while ((con = *conp++) != 0)
1039 FOR_PROVIDES(p, pp, con)
1041 /* dontfix: dont care about conflicts with already installed packs */
1042 if (dontfix && pool->solvables[p].repo == installed)
1044 /* rule: -n|-p: either solvable _or_ provider of conflict */
1045 addrule(solv, -n, -p);
1050 /*-----------------------------------------
1051 * check obsoletes if not installed
1053 if (!installed || pool->solvables[n].repo != installed)
1054 { /* not installed */
1057 obsp = s->repo->idarraydata + s->obsoletes;
1058 while ((obs = *obsp++) != 0)
1060 FOR_PROVIDES(p, pp, obs)
1061 addrule(solv, -n, -p);
1064 FOR_PROVIDES(p, pp, s->name)
1066 if (s->name == pool->solvables[p].name)
1067 addrule(solv, -n, -p);
1071 /*-----------------------------------------
1072 * add recommends to the rule list
1076 recp = s->repo->idarraydata + s->recommends;
1077 while ((rec = *recp++) != 0)
1079 FOR_PROVIDES(p, pp, rec)
1086 sugp = s->repo->idarraydata + s->suggests;
1087 while ((sug = *sugp++) != 0)
1089 FOR_PROVIDES(p, pp, sug)
1096 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable end -----\n");
1100 addrpmrulesforweak(Solver *solv, Map *m)
1102 Pool *pool = solv->pool;
1107 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak -----\n");
1108 for (i = n = 1; n < pool->nsolvables; i++, n++)
1110 if (i == pool->nsolvables)
1114 s = pool->solvables + i;
1115 if (!pool_installable(pool, s))
1120 supp = s->repo->idarraydata + s->supplements;
1121 while ((sup = *supp++) != ID_NULL)
1122 if (dep_possible(solv, sup, m))
1125 if (!sup && s->freshens)
1127 supp = s->repo->idarraydata + s->freshens;
1128 while ((sup = *supp++) != ID_NULL)
1129 if (dep_possible(solv, sup, m))
1132 if (!sup && s->enhances)
1134 supp = s->repo->idarraydata + s->enhances;
1135 while ((sup = *supp++) != ID_NULL)
1136 if (dep_possible(solv, sup, m))
1141 addrpmrulesforsolvable(solv, s, m);
1144 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak end -----\n");
1148 addrpmrulesforupdaters(Solver *solv, Solvable *s, Map *m, int allowall)
1150 Pool *pool = solv->pool;
1155 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
1157 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1158 policy_findupdatepackages(solv, s, &qs, allowall);
1159 if (!MAPTST(m, s - pool->solvables)) /* add rule for s if not already done */
1160 addrpmrulesforsolvable(solv, s, m);
1161 for (i = 0; i < qs.count; i++)
1162 if (!MAPTST(m, qs.elements[i]))
1163 addrpmrulesforsolvable(solv, pool->solvables + qs.elements[i], m);
1166 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
1170 * add rule for update
1171 * (A|A1|A2|A3...) An = update candidates for A
1173 * s = (installed) solvable
1177 addupdaterule(Solver *solv, Solvable *s, int allowall)
1179 /* installed packages get a special upgrade allowed rule */
1180 Pool *pool = solv->pool;
1185 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule -----\n");
1187 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1188 policy_findupdatepackages(solv, s, &qs, allowall);
1189 if (qs.count == 0) /* no updaters found */
1192 d = pool_queuetowhatprovides(pool, &qs); /* intern computed queue */
1194 addrule(solv, s - pool->solvables, d); /* allow update of s */
1195 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule end -----\n");
1199 /*-----------------------------------------------------------------*/
1206 * initial setup for all watches
1210 makewatches(Solver *solv)
1214 int nsolvables = solv->pool->nsolvables;
1216 xfree(solv->watches);
1217 /* lower half for removals, upper half for installs */
1218 solv->watches = (Id *)xcalloc(2 * nsolvables, sizeof(Id));
1220 /* do it reverse so rpm rules get triggered first */
1221 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
1223 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
1226 if (!r->w1 /* rule is disabled */
1227 || !r->w2) /* rule is assertion */
1230 /* see addwatches(solv, r) */
1231 r->n1 = solv->watches[nsolvables + r->w1];
1232 solv->watches[nsolvables + r->w1] = r - solv->rules;
1234 r->n2 = solv->watches[nsolvables + r->w2];
1235 solv->watches[nsolvables + r->w2] = r - solv->rules;
1241 * add watches (for rule)
1245 addwatches(Solver *solv, Rule *r)
1247 int nsolvables = solv->pool->nsolvables;
1249 r->n1 = solv->watches[nsolvables + r->w1];
1250 solv->watches[nsolvables + r->w1] = r - solv->rules;
1252 r->n2 = solv->watches[nsolvables + r->w2];
1253 solv->watches[nsolvables + r->w2] = r - solv->rules;
1257 /*-----------------------------------------------------------------*/
1258 /* rule propagation */
1260 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
1265 * propagate decision to all rules
1269 propagate(Solver *solv, int level)
1271 Pool *pool = solv->pool;
1276 Id *decisionmap = solv->decisionmap;
1277 Id *watches = solv->watches + pool->nsolvables;
1279 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate -----\n");
1281 while (solv->propagate_index < solv->decisionq.count)
1283 /* negate because our watches trigger if literal goes FALSE */
1284 pkg = -solv->decisionq.elements[solv->propagate_index++];
1285 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1287 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "popagate for decision %d level %d\n", -pkg, level);
1288 printruleelement(solv, SAT_DEBUG_PROPAGATE, 0, -pkg);
1291 for (rp = watches + pkg; *rp; rp = nrp)
1293 r = solv->rules + *rp;
1295 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1297 POOL_DEBUG(SAT_DEBUG_PROPAGATE," watch triggered ");
1298 printrule(solv, SAT_DEBUG_PROPAGATE, r);
1311 /* if clause is TRUE, nothing to do */
1312 if (DECISIONMAP_TRUE(ow))
1317 /* not a binary clause, check if we need to move our watch */
1318 /* search for a literal that is not ow and not false */
1319 /* (true is also ok, in that case the rule is fulfilled) */
1320 if (r->p && r->p != ow && !DECISIONMAP_TRUE(-r->p))
1323 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
1324 if (p != ow && !DECISIONMAP_TRUE(-p))
1328 /* p is free to watch, move watch to p */
1329 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1332 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables + p));
1334 POOL_DEBUG(SAT_DEBUG_PROPAGATE," -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables - p));
1348 watches[p] = r - solv->rules;
1352 /* unit clause found, set other watch to TRUE */
1353 if (DECISIONMAP_TRUE(-ow))
1354 return r; /* eek, a conflict! */
1355 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1357 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "unit ");
1358 printrule(solv, SAT_DEBUG_PROPAGATE, r);
1361 decisionmap[ow] = level;
1363 decisionmap[-ow] = -level;
1364 queue_push(&solv->decisionq, ow);
1365 queue_push(&solv->decisionq_why, r - solv->rules);
1366 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1368 Solvable *s = pool->solvables + (ow > 0 ? ow : -ow);
1370 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to install %s\n", solvable2str(pool, s));
1372 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to conflict %s\n", solvable2str(pool, s));
1376 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate end-----\n");
1378 return 0; /* all is well */
1382 /*-----------------------------------------------------------------*/
1391 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *why)
1393 Pool *pool = solv->pool;
1396 Map seen; /* global? */
1400 int learnt_why = solv->learnt_pool.count;
1401 Id *decisionmap = solv->decisionmap;
1405 POOL_DEBUG(SAT_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
1406 map_init(&seen, pool->nsolvables);
1407 idx = solv->decisionq.count;
1410 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1411 printruleclass(solv, SAT_DEBUG_ANALYZE, c);
1412 queue_push(&solv->learnt_pool, c - solv->rules);
1413 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1424 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1426 vv = v > 0 ? v : -v;
1427 if (MAPTST(&seen, vv))
1429 l = solv->decisionmap[vv];
1436 for (j = 0; j < solv->decisionq.count; j++)
1437 if (solv->decisionq.elements[j] == v)
1439 if (j == solv->decisionq.count)
1441 queue_push(&rulq, -(j + 1));
1443 continue; /* initial setting */
1447 num++; /* need to do this one as well */
1459 v = solv->decisionq.elements[--idx];
1460 vv = v > 0 ? v : -v;
1461 if (MAPTST(&seen, vv))
1464 c = solv->rules + solv->decisionq_why.elements[idx];
1472 else if (r.count == 1 && r.elements[0] < 0)
1473 *dr = r.elements[0];
1475 *dr = pool_queuetowhatprovides(pool, &r);
1476 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1478 POOL_DEBUG(SAT_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
1479 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, -v);
1480 for (i = 0; i < r.count; i++)
1481 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, r.elements[i]);
1484 /* push end marker on learnt reasons stack */
1485 queue_push(&solv->learnt_pool, 0);
1486 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1488 for (i = learnt_why; solv->learnt_pool.elements[i]; i++)
1490 POOL_DEBUG(SAT_DEBUG_ANALYZE, "learnt_why ");
1491 printrule(solv, SAT_DEBUG_ANALYZE, solv->rules + solv->learnt_pool.elements[i]);
1502 * reset the solver decisions to right after the rpm rules.
1503 * called after rules have been enabled/disabled
1507 reset_solver(Solver *solv)
1509 Pool *pool = solv->pool;
1513 enabledisablelearntrules(solv);
1515 /* redo all direct rpm rule decisions */
1516 /* we break at the first decision with a why attached, this is
1517 * either a job/system rule assertion or a propagated decision */
1518 for (i = 0; i < solv->decisionq.count; i++)
1520 v = solv->decisionq.elements[i];
1521 solv->decisionmap[v > 0 ? v : -v] = 0;
1523 for (i = 0; i < solv->decisionq_why.count; i++)
1524 if (solv->decisionq_why.elements[i])
1528 v = solv->decisionq.elements[i];
1529 solv->decisionmap[v > 0 ? v : -v] = v > 0 ? 1 : -1;
1532 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions done reduced from %d to %d\n", solv->decisionq.count, i);
1534 solv->decisionq_why.count = i;
1535 solv->decisionq.count = i;
1536 solv->recommends_index = -1;
1537 solv->propagate_index = 0;
1539 /* redo all job/system decisions */
1540 makeruledecisions(solv);
1541 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
1542 /* recreate watch chains */
1548 * analyze_unsolvable_rule
1552 analyze_unsolvable_rule(Solver *solv, Rule *r)
1554 Pool *pool = solv->pool;
1556 Id why = r - solv->rules;
1557 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
1558 printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
1559 if (solv->learntrules && why >= solv->learntrules)
1561 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1562 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i]);
1565 /* do not add rpm rules to problem */
1566 if (why < solv->jobrules)
1568 /* turn rule into problem */
1569 if (why >= solv->jobrules && why < solv->systemrules)
1570 why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
1571 /* return if problem already countains our rule */
1572 if (solv->problems.count)
1574 for (i = solv->problems.count - 1; i >= 0; i--)
1575 if (solv->problems.elements[i] == 0) /* end of last problem reached? */
1577 else if (solv->problems.elements[i] == why)
1580 queue_push(&solv->problems, why);
1585 * analyze_unsolvable
1587 * return: 1 - disabled some rules, try again
1592 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
1594 Pool *pool = solv->pool;
1596 Map seen; /* global to speed things up? */
1599 Id *decisionmap = solv->decisionmap;
1600 int oldproblemcount;
1601 int oldlearntpoolcount;
1604 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
1605 oldproblemcount = solv->problems.count;
1606 oldlearntpoolcount = solv->learnt_pool.count;
1608 /* make room for proof index */
1609 /* must update it later, as analyze_unsolvable_rule would confuse
1610 * it with a rule index if we put the real value in already */
1611 queue_push(&solv->problems, 0);
1614 map_init(&seen, pool->nsolvables);
1615 queue_push(&solv->learnt_pool, r - solv->rules);
1616 analyze_unsolvable_rule(solv, r);
1617 dp = r->d ? pool->whatprovidesdata + r->d : 0;
1628 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1630 vv = v > 0 ? v : -v;
1631 l = solv->decisionmap[vv];
1636 idx = solv->decisionq.count;
1639 v = solv->decisionq.elements[--idx];
1640 vv = v > 0 ? v : -v;
1641 if (!MAPTST(&seen, vv))
1643 why = solv->decisionq_why.elements[idx];
1646 /* level 1 and no why, must be an rpm assertion */
1647 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
1649 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "RPM ");
1650 printruleelement(solv, SAT_DEBUG_UNSOLVABLE, 0, v);
1652 /* this is the only positive rpm assertion */
1653 if (v == SYSTEMSOLVABLE)
1657 queue_push(&solv->learnt_pool, v);
1660 r = solv->rules + why;
1661 queue_push(&solv->learnt_pool, why);
1662 analyze_unsolvable_rule(solv, r);
1663 dp = r->d ? pool->whatprovidesdata + r->d : 0;
1674 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1676 vv = v > 0 ? v : -v;
1677 l = solv->decisionmap[vv];
1684 queue_push(&solv->problems, 0); /* mark end of this problem */
1687 if (solv->weakrules != solv->learntrules)
1689 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1691 why = solv->problems.elements[i];
1692 if (why < solv->weakrules || why >= solv->learntrules)
1694 if (!lastweak || lastweak < why)
1700 /* disable last weak rule */
1701 solv->problems.count = oldproblemcount;
1702 solv->learnt_pool.count = oldlearntpoolcount;
1703 r = solv->rules + lastweak;
1704 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "disabling weak ");
1705 printrule(solv, SAT_DEBUG_UNSOLVABLE, r);
1706 disablerule(solv, r);
1712 queue_push(&solv->learnt_pool, 0);
1713 solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
1717 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1718 disableproblem(solv, solv->problems.elements[i]);
1722 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
1727 /*-----------------------------------------------------------------*/
1728 /* Decision revert */
1732 * revert decision at level
1736 revert(Solver *solv, int level)
1738 Pool *pool = solv->pool;
1740 while (solv->decisionq.count)
1742 v = solv->decisionq.elements[solv->decisionq.count - 1];
1743 vv = v > 0 ? v : -v;
1744 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1746 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1747 solv->decisionmap[vv] = 0;
1748 solv->decisionq.count--;
1749 solv->decisionq_why.count--;
1750 solv->propagate_index = solv->decisionq.count;
1752 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
1754 solv->branches.count--;
1755 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
1756 solv->branches.count--;
1758 solv->recommends_index = -1;
1763 * watch2onhighest - put watch2 on literal with highest level
1767 watch2onhighest(Solver *solv, Rule *r)
1773 return; /* binary rule, both watches are set */
1774 dp = solv->pool->whatprovidesdata + r->d;
1775 while ((v = *dp++) != 0)
1777 l = solv->decisionmap[v < 0 ? -v : v];
1792 * add free decision to decision q, increase level
1793 * propagate decision, return if no conflict.
1794 * in conflict case, analyze conflict rule, add resulting
1795 * rule to learnt rule set, make decision from learnt
1796 * rule (always unit) and re-propagate.
1800 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules)
1802 Pool *pool = solv->pool;
1811 solv->decisionmap[decision] = level;
1813 solv->decisionmap[-decision] = -level;
1814 queue_push(&solv->decisionq, decision);
1815 queue_push(&solv->decisionq_why, 0);
1819 r = propagate(solv, level);
1823 return analyze_unsolvable(solv, r, disablerules);
1824 POOL_DEBUG(SAT_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
1825 l = analyze(solv, level, r, &p, &d, &why); /* learnt rule in p and d */
1826 if (l >= level || l <= 0)
1828 POOL_DEBUG(SAT_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
1830 revert(solv, level);
1831 r = addrule(solv, p, d); /* p requires d */
1834 if (solv->learnt_why.count != (r - solv->rules) - solv->learntrules)
1836 queue_push(&solv->learnt_why, why);
1839 /* at least 2 literals, needs watches */
1840 watch2onhighest(solv, r);
1841 addwatches(solv, r);
1843 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1844 queue_push(&solv->decisionq, p);
1845 queue_push(&solv->decisionq_why, r - solv->rules);
1846 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1848 POOL_DEBUG(SAT_DEBUG_ANALYZE, "decision: ");
1849 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, p);
1850 POOL_DEBUG(SAT_DEBUG_ANALYZE, "new rule: ");
1851 printrule(solv, SAT_DEBUG_ANALYZE, r);
1859 * install best package from the queue. We add an extra package, inst, if
1860 * provided. See comment in weak install section.
1863 selectandinstall(Solver *solv, int level, Queue *dq, Id inst, int disablerules)
1865 Pool *pool = solv->pool;
1869 if (dq->count > 1 || inst)
1870 policy_filter_unwanted(solv, dq, inst, POLICY_MODE_CHOOSE);
1875 /* choose the supplemented one */
1876 for (i = 0; i < dq->count; i++)
1877 if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
1881 for (i = 1; i < dq->count; i++)
1882 queue_push(&solv->branches, dq->elements[i]);
1883 queue_push(&solv->branches, -level);
1887 p = dq->elements[i];
1889 POOL_DEBUG(SAT_DEBUG_POLICY, "installing %s\n", solvable2str(pool, pool->solvables + p));
1891 return setpropagatelearn(solv, level, p, disablerules);
1895 /*-----------------------------------------------------------------*/
1896 /* Main solver interface */
1901 * create solver structure
1903 * pool: all available solvables
1904 * installed: installed Solvables
1907 * Upon solving, rules are created to flag the Solvables
1908 * of the 'installed' Repo as installed.
1912 solver_create(Pool *pool, Repo *installed)
1915 solv = (Solver *)xcalloc(1, sizeof(Solver));
1917 solv->installed = installed;
1919 queue_init(&solv->ruletojob);
1920 queue_init(&solv->decisionq);
1921 queue_init(&solv->decisionq_why);
1922 queue_init(&solv->problems);
1923 queue_init(&solv->suggestions);
1924 queue_init(&solv->learnt_why);
1925 queue_init(&solv->learnt_pool);
1926 queue_init(&solv->branches);
1928 map_init(&solv->recommendsmap, pool->nsolvables);
1929 map_init(&solv->suggestsmap, pool->nsolvables);
1930 map_init(&solv->noupdate, installed ? installed->end - installed->start : 0);
1931 solv->recommends_index = 0;
1933 solv->decisionmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
1934 solv->rules = (Rule *)xmalloc((solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
1935 memset(solv->rules, 0, sizeof(Rule));
1947 solver_free(Solver *solv)
1949 queue_free(&solv->ruletojob);
1950 queue_free(&solv->decisionq);
1951 queue_free(&solv->decisionq_why);
1952 queue_free(&solv->learnt_why);
1953 queue_free(&solv->learnt_pool);
1954 queue_free(&solv->problems);
1955 queue_free(&solv->suggestions);
1956 queue_free(&solv->branches);
1958 map_free(&solv->recommendsmap);
1959 map_free(&solv->suggestsmap);
1960 map_free(&solv->noupdate);
1961 xfree(solv->decisionmap);
1963 xfree(solv->watches);
1964 xfree(solv->weaksystemrules);
1965 xfree(solv->obsoletes);
1966 xfree(solv->obsoletes_data);
1971 /*-------------------------------------------------------*/
1976 * all rules have been set up, now actually run the solver
1981 run_solver(Solver *solv, int disablerules, int doweak)
1989 Pool *pool = solv->pool;
1992 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
1994 POOL_DEBUG (SAT_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
1995 for (i = 0; i < solv->nrules; i++)
1996 printrule(solv, SAT_DEBUG_RULE_CREATION, solv->rules + i);
1999 /* create watches chains */
2002 POOL_DEBUG(SAT_DEBUG_STATS, "initial decisions: %d\n", solv->decisionq.count);
2004 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2005 printdecisions(solv);
2007 /* start SAT algorithm */
2009 systemlevel = level + 1;
2010 POOL_DEBUG(SAT_DEBUG_STATS, "solving...\n");
2021 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
2022 if ((r = propagate(solv, level)) != 0)
2024 if (analyze_unsolvable(solv, r, disablerules))
2032 * installed packages
2035 if (level < systemlevel && solv->installed && solv->installed->nsolvables)
2037 if (!solv->updatesystem)
2039 /* try to keep as many packages as possible */
2040 POOL_DEBUG(SAT_DEBUG_STATS, "installing system packages\n");
2041 for (i = solv->installed->start, n = 0; ; i++)
2043 if (n == solv->installed->nsolvables)
2045 if (i == solv->installed->end)
2046 i = solv->installed->start;
2047 s = pool->solvables + i;
2048 if (s->repo != solv->installed)
2051 if (solv->decisionmap[i] != 0)
2053 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "keeping %s\n", solvable2str(pool, s));
2055 level = setpropagatelearn(solv, level, i, disablerules);
2061 if (level <= olevel)
2065 if (solv->weaksystemrules)
2067 POOL_DEBUG(SAT_DEBUG_STATS, "installing weak system packages\n");
2068 for (i = solv->installed->start; i < solv->installed->end; i++)
2070 if (pool->solvables[i].repo != solv->installed)
2072 if (solv->decisionmap[i] > 0 || (solv->decisionmap[i] < 0 && solv->weaksystemrules[i - solv->installed->start] == 0))
2074 /* noupdate is set if a job is erasing the installed solvable or installing a specific version */
2075 if (MAPTST(&solv->noupdate, i - solv->installed->start))
2078 if (solv->weaksystemrules[i - solv->installed->start])
2080 dp = pool->whatprovidesdata + solv->weaksystemrules[i - solv->installed->start];
2081 while ((p = *dp++) != 0)
2083 if (solv->decisionmap[p] > 0)
2085 if (solv->decisionmap[p] == 0)
2089 continue; /* update package already installed */
2091 if (!dq.count && solv->decisionmap[i] != 0)
2094 /* FIXME: i is handled a bit different because we do not want
2095 * to have it pruned just because it is not recommened.
2096 * we should not prune installed packages instead */
2097 level = selectandinstall(solv, level, &dq, (solv->decisionmap[i] ? 0 : i), disablerules);
2103 if (level <= olevel)
2106 if (i < solv->installed->end)
2109 systemlevel = level;
2116 POOL_DEBUG(SAT_DEBUG_STATS, "deciding unresolved rules\n");
2117 for (i = 1, n = 1; ; i++, n++)
2119 if (n == solv->nrules)
2121 if (i == solv->nrules)
2123 r = solv->rules + i;
2129 /* binary or unary rule */
2130 /* need two positive undecided literals */
2131 if (r->p < 0 || r->w2 <= 0)
2133 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
2135 queue_push(&dq, r->p);
2136 queue_push(&dq, r->w2);
2141 * all negative literals are installed
2142 * no positive literal is installed
2143 * i.e. the rule is not fulfilled and we
2144 * just need to decide on the positive literals
2148 if (solv->decisionmap[-r->p] <= 0)
2153 if (solv->decisionmap[r->p] > 0)
2155 if (solv->decisionmap[r->p] == 0)
2156 queue_push(&dq, r->p);
2158 dp = pool->whatprovidesdata + r->d;
2159 while ((p = *dp++) != 0)
2163 if (solv->decisionmap[-p] <= 0)
2168 if (solv->decisionmap[p] > 0)
2170 if (solv->decisionmap[p] == 0)
2179 /* cannot happen as this means that
2180 * the rule is unit */
2181 printrule(solv, SAT_FATAL, r);
2184 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
2186 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "unfulfilled ");
2187 printrule(solv, SAT_DEBUG_PROPAGATE, r);
2191 level = selectandinstall(solv, level, &dq, 0, disablerules);
2197 if (level < systemlevel)
2200 } /* for(), decide */
2202 if (n != solv->nrules) /* continue if level < systemlevel */
2205 if (doweak && !solv->problems.count)
2209 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended packages\n");
2210 if (!REGARD_RECOMMENDS_OF_INSTALLED_ITEMS)
2212 for (i = 0; i < solv->decisionq.count; i++)
2214 p = solv->decisionq.elements[i];
2215 if (p > 0 && pool->solvables[p].repo == solv->installed)
2216 solv->decisionmap[p] = -solv->decisionmap[p];
2220 for (i = 1; i < pool->nsolvables; i++)
2222 if (solv->decisionmap[i] < 0)
2224 if (solv->decisionmap[i] > 0)
2226 Id *recp, rec, *pp, p;
2227 s = pool->solvables + i;
2228 /* installed, check for recommends */
2229 /* XXX need to special case AND ? */
2232 recp = s->repo->idarraydata + s->recommends;
2233 while ((rec = *recp++) != 0)
2236 FOR_PROVIDES(p, pp, rec)
2238 if (solv->decisionmap[p] > 0)
2243 else if (solv->decisionmap[p] == 0)
2245 queue_pushunique(&dq, p);
2253 s = pool->solvables + i;
2254 if (!s->supplements && !s->freshens)
2256 if (!pool_installable(pool, s))
2258 if (solver_is_supplementing(solv, s))
2259 queue_pushunique(&dq, i);
2262 if (!REGARD_RECOMMENDS_OF_INSTALLED_ITEMS)
2264 for (i = 0; i < solv->decisionq.count; i++)
2266 p = solv->decisionq.elements[i];
2267 if (p > 0 && pool->solvables[p].repo == solv->installed)
2268 solv->decisionmap[p] = -solv->decisionmap[p];
2274 policy_filter_unwanted(solv, &dq, 0, POLICY_MODE_RECOMMEND);
2276 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended %s\n", solvable2str(pool, pool->solvables + p));
2277 level = setpropagatelearn(solv, level, p, 0);
2282 if (solv->solution_callback)
2284 solv->solution_callback(solv, solv->solution_callback_data);
2285 if (solv->branches.count)
2287 int i = solv->branches.count - 1;
2288 int l = -solv->branches.elements[i];
2290 if (solv->branches.elements[i - 1] < 0)
2292 p = solv->branches.elements[i];
2293 POOL_DEBUG(SAT_DEBUG_STATS, "branching with %s\n", solvable2str(pool, pool->solvables + p));
2295 for (j = i + 1; j < solv->branches.count; j++)
2296 queue_push(&dq, solv->branches.elements[j]);
2297 solv->branches.count = i;
2299 revert(solv, level);
2301 for (j = 0; j < dq.count; j++)
2302 queue_push(&solv->branches, dq.elements[j]);
2304 level = setpropagatelearn(solv, level, p, disablerules);
2312 /* all branches done, we're finally finished */
2316 /* minimization step */
2317 if (solv->branches.count)
2319 int l = 0, lasti = -1, lastl = -1;
2321 for (i = solv->branches.count - 1; i >= 0; i--)
2323 p = solv->branches.elements[i];
2326 else if (p > 0 && solv->decisionmap[p] > l + 1)
2334 /* kill old solvable so that we do not loop */
2335 p = solv->branches.elements[lasti];
2336 solv->branches.elements[lasti] = 0;
2337 POOL_DEBUG(SAT_DEBUG_STATS, "minimizing %d -> %d with %s\n", solv->decisionmap[p], l, solvable2str(pool, pool->solvables + p));
2340 revert(solv, level);
2342 level = setpropagatelearn(solv, level, p, disablerules);
2359 * at this point, all rules that led to conflicts are disabled.
2360 * we re-enable all rules of a problem set but rule "sug", then
2361 * continue to disable more rules until there as again a solution.
2364 /* FIXME: think about conflicting assertions */
2367 refine_suggestion(Solver *solv, Queue *job, Id *problem, Id sug, Queue *refined)
2369 Pool *pool = solv->pool;
2376 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
2378 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion start\n");
2379 for (i = 0; problem[i]; i++)
2381 if (problem[i] == sug)
2382 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "=> ");
2383 printproblem(solv, problem[i]);
2386 queue_init(&disabled);
2387 queue_empty(refined);
2388 queue_push(refined, sug);
2390 /* re-enable all problem rules with the exception of "sug" */
2394 for (i = 0; problem[i]; i++)
2395 if (problem[i] != sug)
2396 enableproblem(solv, problem[i]);
2399 disableupdaterules(solv, job, -(sug + 1));
2403 /* re-enable as many weak rules as possible */
2404 for (i = solv->weakrules; i < solv->learntrules; i++)
2406 r = solv->rules + i;
2408 enablerule(solv, r);
2411 queue_empty(&solv->problems);
2412 revert(solv, 1); /* XXX move to reset_solver? */
2415 run_solver(solv, 0, 0);
2416 if (!solv->problems.count)
2418 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no more problems!\n");
2419 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2420 printdecisions(solv);
2421 break; /* great, no more problems */
2423 disabledcnt = disabled.count;
2424 /* skip over proof index */
2425 for (i = 1; i < solv->problems.count - 1; i++)
2427 /* ignore solutions in refined */
2428 v = solv->problems.elements[i];
2430 break; /* end of problem reached */
2431 for (j = 0; problem[j]; j++)
2432 if (problem[j] != sug && problem[j] == v)
2436 queue_push(&disabled, v);
2438 if (disabled.count == disabledcnt)
2440 /* no solution found, this was an invalid suggestion! */
2441 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no solution found!\n");
2445 if (disabled.count == disabledcnt + 1)
2447 /* just one suggestion, add it to refined list */
2448 v = disabled.elements[disabledcnt];
2449 queue_push(refined, v);
2450 disableproblem(solv, v);
2452 disableupdaterules(solv, job, -(v + 1));
2456 /* more than one solution, disable all */
2457 /* do not push anything on refine list */
2458 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
2460 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "more than one solution found:\n");
2461 for (i = disabledcnt; i < disabled.count; i++)
2462 printproblem(solv, disabled.elements[i]);
2464 for (i = disabledcnt; i < disabled.count; i++)
2465 disableproblem(solv, disabled.elements[i]);
2468 /* all done, get us back into the same state as before */
2469 /* enable refined rules again */
2470 for (i = 0; i < disabled.count; i++)
2471 enableproblem(solv, disabled.elements[i]);
2472 /* disable problem rules again */
2473 for (i = 0; problem[i]; i++)
2474 disableproblem(solv, problem[i]);
2475 disableupdaterules(solv, job, -1);
2476 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion end\n");
2480 problems_to_solutions(Solver *solv, Queue *job)
2482 Pool *pool = solv->pool;
2490 if (!solv->problems.count)
2492 queue_clone(&problems, &solv->problems);
2493 queue_init(&solution);
2494 queue_init(&solutions);
2495 /* copy over proof index */
2496 queue_push(&solutions, problems.elements[0]);
2497 problem = problems.elements + 1;
2498 for (i = 1; i < problems.count; i++)
2500 Id v = problems.elements[i];
2503 /* mark end of this problem */
2504 queue_push(&solutions, 0);
2505 queue_push(&solutions, 0);
2506 if (i + 1 == problems.count)
2508 /* copy over proof of next problem */
2509 queue_push(&solutions, problems.elements[i + 1]);
2511 problem = problems.elements + i + 1;
2514 refine_suggestion(solv, job, problem, v, &solution);
2515 if (!solution.count)
2516 continue; /* this solution didn't work out */
2518 for (j = 0; j < solution.count; j++)
2520 why = solution.elements[j];
2522 printproblem(solv, why);
2526 queue_push(&solutions, 0);
2527 queue_push(&solutions, -why);
2529 else if (why >= solv->systemrules && why < solv->weakrules)
2532 p = solv->installed->start + (why - solv->systemrules);
2533 if (solv->weaksystemrules && solv->weaksystemrules[why - solv->systemrules])
2535 Id *dp = pool->whatprovidesdata + solv->weaksystemrules[why - solv->systemrules];
2538 if (*dp >= solv->installed->start && *dp < solv->installed->start + solv->installed->nsolvables)
2540 if (solv->decisionmap[*dp] > 0)
2547 queue_push(&solutions, p);
2548 queue_push(&solutions, rp);
2553 /* mark end of this solution */
2554 queue_push(&solutions, 0);
2555 queue_push(&solutions, 0);
2557 queue_free(&solution);
2558 queue_free(&problems);
2559 /* copy queue over to solutions */
2560 queue_free(&solv->problems);
2561 queue_clone(&solv->problems, &solutions);
2563 /* bring solver back into problem state */
2564 revert(solv, 1); /* XXX move to reset_solver? */
2567 if (solv->problems.count != solutions.count)
2569 queue_free(&solutions);
2573 solver_next_problem(Solver *solv, Id problem)
2577 return solv->problems.count ? 1 : 0;
2578 pp = solv->problems.elements + problem;
2579 while (pp[0] || pp[1])
2583 while (pp[0] || pp[1])
2588 problem = pp - solv->problems.elements;
2589 if (problem >= solv->problems.count)
2595 solver_next_solution(Solver *solv, Id problem, Id solution)
2601 pp = solv->problems.elements + solution;
2602 return pp[0] || pp[1] ? solution : 0;
2604 pp = solv->problems.elements + solution;
2605 while (pp[0] || pp[1])
2608 solution = pp - solv->problems.elements;
2609 return pp[0] || pp[1] ? solution : 0;
2613 solver_next_solutionelement(Solver *solv, Id problem, Id solution, Id element, Id *p, Id *rp)
2616 element = element ? element + 2 : solution;
2617 pp = solv->problems.elements + element;
2618 if (!(pp[0] || pp[1]))
2627 * create obsoletesmap from solver decisions
2628 * required for decision handling
2632 create_obsoletesmap(Solver *solv)
2634 Pool *pool = solv->pool;
2635 Repo *installed = solv->installed;
2636 Id p, *obsoletesmap = NULL;
2640 obsoletesmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
2643 for (i = 0; i < solv->decisionq.count; i++)
2647 n = solv->decisionq.elements[i];
2650 if (n == SYSTEMSOLVABLE)
2652 s = pool->solvables + n;
2653 if (s->repo == installed) /* obsoletes don't count for already installed packages */
2655 FOR_PROVIDES(p, pp, s->name)
2656 if (s->name == pool->solvables[p].name)
2658 if (pool->solvables[p].repo == installed && !obsoletesmap[p])
2660 obsoletesmap[p] = n;
2665 for (i = 0; i < solv->decisionq.count; i++)
2670 n = solv->decisionq.elements[i];
2673 if (n == SYSTEMSOLVABLE)
2675 s = pool->solvables + n;
2676 if (s->repo == installed) /* obsoletes don't count for already installed packages */
2680 obsp = s->repo->idarraydata + s->obsoletes;
2681 while ((obs = *obsp++) != 0)
2682 FOR_PROVIDES(p, pp, obs)
2684 if (pool->solvables[p].repo == installed && !obsoletesmap[p])
2686 obsoletesmap[p] = n;
2692 return obsoletesmap;
2701 printdecisions(Solver *solv)
2703 Pool *pool = solv->pool;
2704 Repo *installed = solv->installed;
2705 Id p, *obsoletesmap = create_obsoletesmap( solv );
2709 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- Decisions -----\n");
2711 /* print solvables to be erased */
2715 FOR_REPO_SOLVABLES(installed, p, s)
2717 if (solv->decisionmap[p] >= 0)
2719 if (obsoletesmap[p])
2721 POOL_DEBUG(SAT_DEBUG_RESULT, "erase %s\n", solvable2str(pool, s));
2725 /* print solvables to be installed */
2727 for (i = 0; i < solv->decisionq.count; i++)
2730 p = solv->decisionq.elements[i];
2733 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2736 s = pool->solvables + p;
2737 POOL_DEBUG(SAT_DEBUG_SCHUBI, "level of %s is %d\n", solvable2str(pool, s), p);
2741 if (p == SYSTEMSOLVABLE)
2743 POOL_DEBUG(SAT_DEBUG_SCHUBI, "SYSTEMSOLVABLE\n");
2746 s = pool->solvables + p;
2747 if (installed && s->repo == installed)
2750 if (!obsoletesmap[p])
2752 POOL_DEBUG(SAT_DEBUG_RESULT, "install %s", solvable2str(pool, s));
2756 POOL_DEBUG(SAT_DEBUG_RESULT, "update %s", solvable2str(pool, s));
2757 POOL_DEBUG(SAT_DEBUG_RESULT, " (obsoletes");
2758 for (j = installed->start; j < installed->end; j++)
2759 if (obsoletesmap[j] == p)
2760 POOL_DEBUG(SAT_DEBUG_RESULT, " %s", solvable2str(pool, pool->solvables + j));
2761 POOL_DEBUG(SAT_DEBUG_RESULT, ")");
2763 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
2766 xfree(obsoletesmap);
2768 if (solv->suggestions.count)
2770 POOL_DEBUG(SAT_DEBUG_RESULT, "\nsuggested packages:\n");
2771 for (i = 0; i < solv->suggestions.count; i++)
2773 s = pool->solvables + solv->suggestions.elements[i];
2774 POOL_DEBUG(SAT_DEBUG_RESULT, "- %s\n", solvable2str(pool, s));
2777 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- Decisions end -----\n");
2780 /* this is basically the reverse of addrpmrulesforsolvable */
2782 solver_problemruleinfo(Solver *solv, Queue *job, Id rid, Id *depp, Id *sourcep, Id *targetp)
2784 Pool *pool = solv->pool;
2785 Repo *installed = solv->installed;
2789 Id p, *pp, req, *reqp, con, *conp, obs, *obsp, *dp;
2791 if (rid >= solv->weakrules)
2793 if (rid >= solv->systemrules)
2796 *sourcep = solv->installed->start + (rid - solv->systemrules);
2798 return SOLVER_PROBLEM_UPDATE_RULE;
2800 if (rid >= solv->jobrules)
2803 r = solv->rules + rid;
2804 p = solv->ruletojob.elements[rid - solv->jobrules];
2805 *depp = job->elements[p + 1];
2807 *targetp = job->elements[p];
2808 if (r->d == 0 && r->w2 == 0 && r->p == -SYSTEMSOLVABLE)
2809 return SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP;
2810 return SOLVER_PROBLEM_JOB_RULE;
2814 /* a rpm rule assertion */
2815 if (rid == -SYSTEMSOLVABLE)
2816 abort(); /* can happen only for job rules */
2817 s = pool->solvables - rid;
2818 if (installed && !solv->fixsystem && s->repo == installed)
2820 if (dontfix) /* dontfix packages never have a neg assertion */
2822 /* see why the package is not installable */
2823 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC && !pool_installable(pool, s))
2824 return SOLVER_PROBLEM_NOT_INSTALLABLE;
2825 /* check requires */
2828 reqp = s->repo->idarraydata + s->requires;
2829 while ((req = *reqp++) != 0)
2831 if (req == SOLVABLE_PREREQMARKER)
2833 dp = pool_whatprovides(pool, req);
2839 return SOLVER_PROBLEM_NOTHING_PROVIDES_DEP;
2844 r = solv->rules + rid;
2846 abort(); /* not a rpm rule */
2847 if (r->d == 0 && r->w2 == 0)
2849 /* an assertion. we don't store them as rpm rules, so
2853 s = pool->solvables - r->p;
2854 if (installed && !solv->fixsystem && s->repo == installed)
2856 if (r->d == 0 && r->w2 < 0)
2858 /* a package conflict */
2859 Solvable *s2 = pool->solvables - r->w2;
2862 if (installed && !solv->fixsystem && s2->repo == installed)
2865 /* if both packages have the same name and at least one of them
2866 * is not installed, they conflict */
2867 if (s->name == s2->name && (!installed || (s->repo != installed || s2->repo != installed)))
2872 return SOLVER_PROBLEM_SAME_NAME;
2875 /* check conflicts in both directions */
2878 conp = s->repo->idarraydata + s->conflicts;
2879 while ((con = *conp++) != 0)
2881 FOR_PROVIDES(p, pp, con)
2883 if (dontfix && pool->solvables[p].repo == installed)
2890 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
2896 conp = s2->repo->idarraydata + s2->conflicts;
2897 while ((con = *conp++) != 0)
2899 FOR_PROVIDES(p, pp, con)
2901 if (dontfix2 && pool->solvables[p].repo == installed)
2908 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
2912 /* check obsoletes in both directions */
2913 if ((!installed || s->repo != installed) && s->obsoletes)
2915 obsp = s->repo->idarraydata + s->obsoletes;
2916 while ((obs = *obsp++) != 0)
2918 FOR_PROVIDES(p, pp, obs)
2925 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
2929 if ((!installed || s2->repo != installed) && s2->obsoletes)
2931 obsp = s2->repo->idarraydata + s2->obsoletes;
2932 while ((obs = *obsp++) != 0)
2934 FOR_PROVIDES(p, pp, obs)
2941 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
2945 /* all cases checked, can't happen */
2948 /* simple requires */
2951 reqp = s->repo->idarraydata + s->requires;
2952 while ((req = *reqp++) != 0)
2954 if (req == SOLVABLE_PREREQMARKER)
2956 dp = pool_whatprovides(pool, req);
2959 if (*dp == r->w2 && dp[1] == 0)
2962 else if (dp - pool->whatprovidesdata == r->d)
2970 return SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE;
2974 findproblemrule_internal(Solver *solv, Id idx, Id *reqrp, Id *conrp, Id *sysrp, Id *jobrp)
2977 Id lreqr, lconr, lsysr, ljobr;
2981 lreqr = lconr = lsysr = ljobr = 0;
2982 while ((rid = solv->learnt_pool.elements[idx++]) != 0)
2984 if (rid >= solv->learntrules)
2985 findproblemrule_internal(solv, solv->learnt_why.elements[rid - solv->learntrules], &lreqr, &lconr, &lsysr, &ljobr);
2986 else if (rid >= solv->systemrules)
2991 else if (rid >= solv->jobrules)
2998 r = solv->rules + rid;
2999 if (!r->d && r->w2 < 0)
3012 /* assertion, counts as require rule */
3013 /* ignore system solvable as we need useful info */
3014 if (rid == -SYSTEMSOLVABLE)
3016 if (!*reqrp || !reqassert)
3023 if (!*reqrp && lreqr)
3025 if (!*conrp && lconr)
3027 if (!*jobrp && ljobr)
3029 if (!*sysrp && lsysr)
3034 solver_findproblemrule(Solver *solv, Id problem)
3036 Id reqr, conr, sysr, jobr;
3037 Id idx = solv->problems.elements[problem - 1];
3038 reqr = conr = sysr = jobr = 0;
3039 findproblemrule_internal(solv, idx, &reqr, &conr, &sysr, &jobr);
3052 printprobleminfo(Solver *solv, Queue *job, Id problem)
3054 Pool *pool = solv->pool;
3056 Id dep, source, target;
3059 probr = solver_findproblemrule(solv, problem);
3060 switch (solver_problemruleinfo(solv, job, probr, &dep, &source, &target))
3062 case SOLVER_PROBLEM_UPDATE_RULE:
3063 s = pool_id2solvable(pool, source);
3064 POOL_DEBUG(SAT_DEBUG_RESULT, "problem with installed package %s\n", solvable2str(pool, s));
3066 case SOLVER_PROBLEM_JOB_RULE:
3067 POOL_DEBUG(SAT_DEBUG_RESULT, "conflicting requests\n");
3069 case SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP:
3070 POOL_DEBUG(SAT_DEBUG_RESULT, "nothing provides requested %s\n", dep2str(pool, dep));
3072 case SOLVER_PROBLEM_NOT_INSTALLABLE:
3073 s = pool_id2solvable(pool, source);
3074 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s is not installable\n", solvable2str(pool, s));
3076 case SOLVER_PROBLEM_NOTHING_PROVIDES_DEP:
3077 s = pool_id2solvable(pool, source);
3078 POOL_DEBUG(SAT_DEBUG_RESULT, "nothing provides %s needed by %s\n", dep2str(pool, dep), solvable2str(pool, s));
3080 case SOLVER_PROBLEM_SAME_NAME:
3081 s = pool_id2solvable(pool, source);
3082 s2 = pool_id2solvable(pool, target);
3083 POOL_DEBUG(SAT_DEBUG_RESULT, "cannot install both %s and %s\n", solvable2str(pool, s), solvable2str(pool, s2));
3085 case SOLVER_PROBLEM_PACKAGE_CONFLICT:
3086 s = pool_id2solvable(pool, source);
3087 s2 = pool_id2solvable(pool, target);
3088 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s conflicts with %s provided by %s\n", solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
3090 case SOLVER_PROBLEM_PACKAGE_OBSOLETES:
3091 s = pool_id2solvable(pool, source);
3092 s2 = pool_id2solvable(pool, target);
3093 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s obsoletes %s provided by %s\n", solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
3095 case SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE:
3096 s = pool_id2solvable(pool, source);
3097 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s requires %s, but none of the providers can be installed\n", solvable2str(pool, s), dep2str(pool, dep));
3103 printsolutions(Solver *solv, Queue *job)
3105 Pool *pool = solv->pool;
3108 Id problem, solution, element;
3111 POOL_DEBUG(SAT_DEBUG_RESULT, "Encountered problems! Here are the solutions:\n\n");
3114 while ((problem = solver_next_problem(solv, problem)) != 0)
3116 POOL_DEBUG(SAT_DEBUG_RESULT, "Problem %d:\n", pcnt++);
3117 POOL_DEBUG(SAT_DEBUG_RESULT, "====================================\n");
3118 printprobleminfo(solv, job, problem);
3119 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
3121 while ((solution = solver_next_solution(solv, problem, solution)) != 0)
3124 while ((element = solver_next_solutionelement(solv, problem, solution, element, &p, &rp)) != 0)
3128 /* job, rp is index into job queue */
3129 what = job->elements[rp];
3130 switch (job->elements[rp - 1])
3132 case SOLVER_INSTALL_SOLVABLE:
3133 s = pool->solvables + what;
3134 if (solv->installed && s->repo == solv->installed)
3135 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not keep %s installed\n", solvable2str(pool, s));
3137 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install %s\n", solvable2str(pool, s));
3139 case SOLVER_ERASE_SOLVABLE:
3140 s = pool->solvables + what;
3141 if (solv->installed && s->repo == solv->installed)
3142 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall %s\n", solvable2str(pool, s));
3144 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not forbid installation of %s\n", solvable2str(pool, s));
3146 case SOLVER_INSTALL_SOLVABLE_NAME:
3147 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install %s\n", id2str(pool, what));
3149 case SOLVER_ERASE_SOLVABLE_NAME:
3150 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall %s\n", id2str(pool, what));
3152 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3153 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install a solvable providing %s\n", dep2str(pool, what));
3155 case SOLVER_ERASE_SOLVABLE_PROVIDES:
3156 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall all solvables providing %s\n", dep2str(pool, what));
3158 case SOLVER_INSTALL_SOLVABLE_UPDATE:
3159 s = pool->solvables + what;
3160 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install most recent version of %s\n", solvable2str(pool, s));
3163 POOL_DEBUG(SAT_DEBUG_RESULT, "- do something different\n");
3169 /* policy, replace p with rp */
3170 s = pool->solvables + p;
3171 sd = rp ? pool->solvables + rp : 0;
3175 if (!solv->allowdowngrade && evrcmp(pool, s->evr, sd->evr, EVRCMP_MATCH_RELEASE) > 0)
3177 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow downgrade of %s to %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3180 if (!solv->allowarchchange && s->name == sd->name && s->arch != sd->arch && policy_illegal_archchange(pool, s, sd))
3182 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow architecture change of %s to %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3185 if (!solv->allowvendorchange && s->name == sd->name && s->vendor != sd->vendor && policy_illegal_vendorchange(pool, s, sd))
3188 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow vendor change from '%s' (%s) to '%s' (%s)\n", id2str(pool, s->vendor), solvable2str(pool, s), id2str(pool, sd->vendor), solvable2str(pool, sd));
3190 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow vendor change from '%s' (%s) to no vendor (%s)\n", id2str(pool, s->vendor), solvable2str(pool, s), solvable2str(pool, sd));
3194 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow replacement of %s with %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3198 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow deinstallation of %s\n", solvable2str(pool, s));
3203 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
3209 /* for each installed solvable find which packages with *different* names
3210 * obsolete the solvable.
3211 * this index is used in policy_findupdatepackages if noupdateprovide is set.
3215 create_obsolete_index(Solver *solv)
3217 Pool *pool = solv->pool;
3219 Repo *installed = solv->installed;
3220 Id p, *pp, obs, *obsp, *obsoletes, *obsoletes_data;
3223 if (!installed || !installed->nsolvables)
3225 /* create reverse obsoletes map for installed solvables */
3226 solv->obsoletes = obsoletes = xcalloc(installed->end - installed->start, sizeof(Id));
3227 for (i = 1; i < pool->nsolvables; i++)
3229 s = pool->solvables + i;
3230 if (s->repo == installed)
3234 if (!pool_installable(pool, s))
3236 obsp = s->repo->idarraydata + s->obsoletes;
3237 while ((obs = *obsp++) != 0)
3238 FOR_PROVIDES(p, pp, obs)
3240 if (pool->solvables[p].repo != installed)
3242 if (pool->solvables[p].name == s->name)
3244 obsoletes[p - installed->start]++;
3248 for (i = 0; i < installed->nsolvables; i++)
3251 n += obsoletes[i] + 1;
3254 solv->obsoletes_data = obsoletes_data = xcalloc(n + 1, sizeof(Id));
3255 POOL_DEBUG(SAT_DEBUG_STATS, "obsoletes data: %d entries\n", n + 1);
3256 for (i = pool->nsolvables - 1; i > 0; i--)
3258 s = pool->solvables + i;
3259 if (s->repo == installed)
3263 if (!pool_installable(pool, s))
3265 obsp = s->repo->idarraydata + s->obsoletes;
3266 while ((obs = *obsp++) != 0)
3267 FOR_PROVIDES(p, pp, obs)
3269 if (pool->solvables[p].repo != installed)
3271 if (pool->solvables[p].name == s->name)
3273 p -= installed->start;
3274 if (obsoletes_data[obsoletes[p]] != i)
3275 obsoletes_data[--obsoletes[p]] = i;
3281 /*-----------------------------------------------------------------*/
3291 solver_solve(Solver *solv, Queue *job)
3293 Pool *pool = solv->pool;
3294 Repo *installed = solv->installed;
3297 Map addedmap; /* '1' == have rpm-rules for solvable */
3298 Id how, what, p, *pp, d;
3302 /* create whatprovides if not already there */
3303 if (!pool->whatprovides)
3304 pool_createwhatprovides(pool);
3306 /* create obsolete index if needed */
3307 if (solv->noupdateprovide)
3308 create_obsolete_index(solv);
3311 * create basic rule set of all involved packages
3312 * use addedmap bitmap to make sure we don't create rules twice
3316 map_init(&addedmap, pool->nsolvables);
3320 * always install our system solvable
3322 MAPSET(&addedmap, SYSTEMSOLVABLE);
3323 queue_push(&solv->decisionq, SYSTEMSOLVABLE);
3324 queue_push(&solv->decisionq_why, 0);
3325 solv->decisionmap[SYSTEMSOLVABLE] = 1;
3328 * create rules for all package that could be involved with the solving
3329 * so called: rpm rules
3334 oldnrules = solv->nrules;
3335 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for installed solvables ***\n");
3336 FOR_REPO_SOLVABLES(installed, p, s)
3337 addrpmrulesforsolvable(solv, s, &addedmap);
3338 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
3339 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for updaters of installed solvables ***\n");
3340 oldnrules = solv->nrules;
3341 FOR_REPO_SOLVABLES(installed, p, s)
3342 addrpmrulesforupdaters(solv, s, &addedmap, 1);
3343 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
3346 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for packages involved with a job ***\n");
3347 oldnrules = solv->nrules;
3348 for (i = 0; i < job->count; i += 2)
3350 how = job->elements[i];
3351 what = job->elements[i + 1];
3355 case SOLVER_INSTALL_SOLVABLE:
3356 addrpmrulesforsolvable(solv, pool->solvables + what, &addedmap);
3358 case SOLVER_INSTALL_SOLVABLE_NAME:
3359 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3360 FOR_PROVIDES(p, pp, what)
3362 /* if by name, ensure that the name matches */
3363 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
3365 addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
3368 case SOLVER_INSTALL_SOLVABLE_UPDATE:
3369 /* dont allow downgrade */
3370 addrpmrulesforupdaters(solv, pool->solvables + what, &addedmap, 0);
3374 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
3376 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for recommended/suggested packages ***\n");
3378 oldnrules = solv->nrules;
3379 addrpmrulesforweak(solv, &addedmap);
3380 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
3382 IF_POOLDEBUG (SAT_DEBUG_STATS)
3384 int possible = 0, installable = 0;
3385 for (i = 1; i < pool->nsolvables; i++)
3387 if (pool_installable(pool, pool->solvables + i))
3389 if (MAPTST(&addedmap, i))
3392 POOL_DEBUG(SAT_DEBUG_STATS, "%d of %d installable solvables considered for solving (rules has been generated for)\n", possible, installable);
3396 * first pass done, we now have all the rpm rules we need.
3397 * unify existing rules before going over all job rules and
3399 * at this point the system is always solvable,
3400 * as an empty system (remove all packages) is a valid solution
3403 unifyrules(solv); /* remove duplicate rpm rules */
3405 POOL_DEBUG(SAT_DEBUG_STATS, "decisions so far: %d\n", solv->decisionq.count);
3406 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
3407 printdecisions (solv);
3410 * now add all job rules
3413 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add JOB rules ***\n");
3415 solv->jobrules = solv->nrules;
3417 for (i = 0; i < job->count; i += 2)
3419 oldnrules = solv->nrules;
3421 how = job->elements[i];
3422 what = job->elements[i + 1];
3425 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
3426 s = pool->solvables + what;
3427 POOL_DEBUG(SAT_DEBUG_JOB, "job: install solvable %s\n", solvable2str(pool, s));
3428 addrule(solv, what, 0); /* install by Id */
3429 queue_push(&solv->ruletojob, i);
3431 case SOLVER_ERASE_SOLVABLE:
3432 s = pool->solvables + what;
3433 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase solvable %s\n", solvable2str(pool, s));
3434 addrule(solv, -what, 0); /* remove by Id */
3435 queue_push(&solv->ruletojob, i);
3437 case SOLVER_INSTALL_SOLVABLE_NAME: /* install by capability */
3438 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3439 if (how == SOLVER_INSTALL_SOLVABLE_NAME)
3440 POOL_DEBUG(SAT_DEBUG_JOB, "job: install name %s\n", id2str(pool, what));
3441 if (how == SOLVER_INSTALL_SOLVABLE_PROVIDES)
3442 POOL_DEBUG(SAT_DEBUG_JOB, "job: install provides %s\n", dep2str(pool, what));
3444 FOR_PROVIDES(p, pp, what)
3446 /* if by name, ensure that the name matches */
3447 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
3453 /* no provider, make this an impossible rule */
3454 queue_push(&q, -SYSTEMSOLVABLE);
3457 p = queue_shift(&q); /* get first provider */
3459 d = 0; /* single provider ? -> make assertion */
3461 d = pool_queuetowhatprovides(pool, &q); /* get all providers */
3462 addrule(solv, p, d); /* add 'requires' rule */
3463 queue_push(&solv->ruletojob, i);
3465 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
3466 case SOLVER_ERASE_SOLVABLE_PROVIDES:
3467 if (how == SOLVER_ERASE_SOLVABLE_NAME)
3468 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase name %s\n", id2str(pool, what));
3469 if (how == SOLVER_ERASE_SOLVABLE_PROVIDES)
3470 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase provides %s\n", dep2str(pool, what));
3471 FOR_PROVIDES(p, pp, what)
3473 /* if by name, ensure that the name matches */
3474 if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
3476 addrule(solv, -p, 0); /* add 'remove' rule */
3477 queue_push(&solv->ruletojob, i);
3480 case SOLVER_INSTALL_SOLVABLE_UPDATE: /* find update for solvable */
3481 s = pool->solvables + what;
3482 POOL_DEBUG(SAT_DEBUG_JOB, "job: update %s\n", solvable2str(pool, s));
3483 addupdaterule(solv, s, 0);
3484 queue_push(&solv->ruletojob, i);
3487 IF_POOLDEBUG (SAT_DEBUG_JOB)
3490 if (solv->nrules == oldnrules)
3491 POOL_DEBUG(SAT_DEBUG_JOB, " - no rule created");
3492 for (j = oldnrules; j < solv->nrules; j++)
3494 POOL_DEBUG(SAT_DEBUG_JOB, " - job ");
3495 printrule(solv, SAT_DEBUG_JOB, solv->rules + j);
3500 if (solv->ruletojob.count != solv->nrules - solv->jobrules)
3504 * now add system rules
3508 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add system rules ***\n");
3511 solv->systemrules = solv->nrules;
3514 * create rules for updating installed solvables
3518 if (installed && !solv->allowuninstall)
3519 { /* loop over all installed solvables */
3520 /* we create all update rules, but disable some later on depending on the job */
3521 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3522 if (s->repo == installed)
3523 addupdaterule(solv, s, 0); /* allowall = 0 */
3525 addupdaterule(solv, 0, 0); /* create dummy rule; allowall = 0 */
3526 /* consistency check: we added a rule for _every_ system solvable */
3527 if (solv->nrules - solv->systemrules != installed->end - installed->start)
3531 /* create special weak system rules */
3532 /* those are used later on to keep a version of the installed packages in
3534 if (installed && installed->nsolvables)
3536 solv->weaksystemrules = xcalloc(installed->end - installed->start, sizeof(Id));
3537 FOR_REPO_SOLVABLES(installed, p, s)
3539 policy_findupdatepackages(solv, s, &q, 1);
3541 solv->weaksystemrules[p - installed->start] = pool_queuetowhatprovides(pool, &q);
3545 /* free unneeded memory */
3546 map_free(&addedmap);
3549 solv->weakrules = solv->nrules;
3551 /* try real hard to keep packages installed */
3554 FOR_REPO_SOLVABLES(installed, p, s)
3556 /* FIXME: can't work with refine_suggestion! */
3557 /* need to always add the rule but disable it */
3558 if (MAPTST(&solv->noupdate, p - installed->start))
3560 d = solv->weaksystemrules[p - installed->start];
3561 addrule(solv, p, d);
3565 /* all new rules are learnt after this point */
3566 solv->learntrules = solv->nrules;
3573 disableupdaterules(solv, job, -1);
3574 makeruledecisions(solv);
3576 POOL_DEBUG(SAT_DEBUG_STATS, "problems so far: %d\n", solv->problems.count);
3578 run_solver(solv, 1, 1);
3580 /* find suggested packages */
3581 if (!solv->problems.count)
3583 Id sug, *sugp, p, *pp;
3585 /* create map of all suggests that are still open */
3586 solv->recommends_index = -1;
3587 MAPZERO(&solv->suggestsmap);
3588 for (i = 0; i < solv->decisionq.count; i++)
3590 p = solv->decisionq.elements[i];
3593 s = pool->solvables + p;
3596 sugp = s->repo->idarraydata + s->suggests;
3597 while ((sug = *sugp++) != 0)
3599 FOR_PROVIDES(p, pp, sug)
3600 if (solv->decisionmap[p] > 0)
3603 continue; /* already fulfilled */
3604 FOR_PROVIDES(p, pp, sug)
3605 MAPSET(&solv->suggestsmap, p);
3609 for (i = 1; i < pool->nsolvables; i++)
3611 if (solv->decisionmap[i] != 0)
3613 s = pool->solvables + i;
3614 if (!MAPTST(&solv->suggestsmap, i))
3618 if (!pool_installable(pool, s))
3620 if (!solver_is_enhancing(solv, s))
3623 queue_push(&solv->suggestions, i);
3625 policy_filter_unwanted(solv, &solv->suggestions, 0, POLICY_MODE_SUGGEST);
3628 if (solv->problems.count)
3629 problems_to_solutions(solv, job);