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 rules: %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 "opposite" 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 /*-----------------------------------------------------------------*/
1208 printWatches(Solver *solv, int type)
1210 Pool *pool = solv->pool;
1213 POOL_DEBUG(type, "Watches: \n");
1215 for (counter = -(pool->nsolvables); counter <= pool->nsolvables; counter ++)
1217 POOL_DEBUG(type, " solvable [%d] -- rule [%d]\n", counter, solv->watches[counter+pool->nsolvables]);
1224 * initial setup for all watches
1228 makewatches(Solver *solv)
1232 int nsolvables = solv->pool->nsolvables;
1234 xfree(solv->watches);
1235 /* lower half for removals, upper half for installs */
1236 solv->watches = (Id *)xcalloc(2 * nsolvables, sizeof(Id));
1238 /* do it reverse so rpm rules get triggered first */
1239 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
1241 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
1244 if (!r->w1 /* rule is disabled */
1245 || !r->w2) /* rule is assertion */
1248 /* see addwatches(solv, r) */
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;
1259 * add watches (for rule)
1263 addwatches(Solver *solv, Rule *r)
1265 int nsolvables = solv->pool->nsolvables;
1267 r->n1 = solv->watches[nsolvables + r->w1];
1268 solv->watches[nsolvables + r->w1] = r - solv->rules;
1270 r->n2 = solv->watches[nsolvables + r->w2];
1271 solv->watches[nsolvables + r->w2] = r - solv->rules;
1275 /*-----------------------------------------------------------------*/
1276 /* rule propagation */
1278 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
1283 * propagate decision to all rules
1284 * return : 0 = everything is OK
1285 * watched rule = there is a conflict
1289 propagate(Solver *solv, int level)
1291 Pool *pool = solv->pool;
1296 Id *decisionmap = solv->decisionmap;
1297 Id *watches = solv->watches + pool->nsolvables;
1299 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate -----\n");
1301 while (solv->propagate_index < solv->decisionq.count)
1303 /* negate because our watches trigger if literal goes FALSE */
1304 pkg = -solv->decisionq.elements[solv->propagate_index++];
1305 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1307 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "popagate for decision %d level %d\n", -pkg, level);
1308 printruleelement(solv, SAT_DEBUG_PROPAGATE, 0, -pkg);
1309 printWatches(solv, SAT_DEBUG_SCHUBI);
1312 for (rp = watches + pkg; *rp; rp = nrp)
1314 r = solv->rules + *rp;
1316 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1318 POOL_DEBUG(SAT_DEBUG_PROPAGATE," watch triggered ");
1319 printrule(solv, SAT_DEBUG_PROPAGATE, r);
1324 ow = r->w2; /* regard the second watchpoint to come to a solution */
1329 ow = r->w1; /* regard the first watchpoint to come to a solution */
1332 /* if clause is TRUE, nothing to do */
1333 if (DECISIONMAP_TRUE(ow))
1338 /* not a binary clause, check if we need to move our watch */
1339 /* search for a literal that is not ow and not false */
1340 /* (true is also ok, in that case the rule is fulfilled) */
1341 if (r->p && r->p != ow && !DECISIONMAP_TRUE(-r->p))
1344 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
1345 if (p != ow && !DECISIONMAP_TRUE(-p))
1349 /* p is free to watch, move watch to p */
1350 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1353 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables + p));
1355 POOL_DEBUG(SAT_DEBUG_PROPAGATE," -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables - p));
1369 watches[p] = r - solv->rules;
1373 /* unit clause found, set other watch to TRUE */
1374 if (DECISIONMAP_TRUE(-ow))
1375 return r; /* eek, a conflict! */
1376 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1378 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " unit ");
1379 printrule(solv, SAT_DEBUG_PROPAGATE, r);
1382 decisionmap[ow] = level;
1384 decisionmap[-ow] = -level;
1385 queue_push(&solv->decisionq, ow);
1386 queue_push(&solv->decisionq_why, r - solv->rules);
1387 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1389 Solvable *s = pool->solvables + (ow > 0 ? ow : -ow);
1391 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to install %s\n", solvable2str(pool, s));
1393 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to conflict %s\n", solvable2str(pool, s));
1397 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate end-----\n");
1399 return 0; /* all is well */
1403 /*-----------------------------------------------------------------*/
1412 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *why)
1414 Pool *pool = solv->pool;
1417 Map seen; /* global? */
1421 int learnt_why = solv->learnt_pool.count;
1422 Id *decisionmap = solv->decisionmap;
1426 POOL_DEBUG(SAT_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
1427 map_init(&seen, pool->nsolvables);
1428 idx = solv->decisionq.count;
1431 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1432 printruleclass(solv, SAT_DEBUG_ANALYZE, c);
1433 queue_push(&solv->learnt_pool, c - solv->rules);
1434 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1445 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1447 vv = v > 0 ? v : -v;
1448 if (MAPTST(&seen, vv))
1450 l = solv->decisionmap[vv];
1457 for (j = 0; j < solv->decisionq.count; j++)
1458 if (solv->decisionq.elements[j] == v)
1460 if (j == solv->decisionq.count)
1462 queue_push(&rulq, -(j + 1));
1464 continue; /* initial setting */
1468 num++; /* need to do this one as well */
1480 v = solv->decisionq.elements[--idx];
1481 vv = v > 0 ? v : -v;
1482 if (MAPTST(&seen, vv))
1485 c = solv->rules + solv->decisionq_why.elements[idx];
1493 else if (r.count == 1 && r.elements[0] < 0)
1494 *dr = r.elements[0];
1496 *dr = pool_queuetowhatprovides(pool, &r);
1497 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1499 POOL_DEBUG(SAT_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
1500 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, -v);
1501 for (i = 0; i < r.count; i++)
1502 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, r.elements[i]);
1505 /* push end marker on learnt reasons stack */
1506 queue_push(&solv->learnt_pool, 0);
1507 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1509 for (i = learnt_why; solv->learnt_pool.elements[i]; i++)
1511 POOL_DEBUG(SAT_DEBUG_ANALYZE, "learnt_why ");
1512 printrule(solv, SAT_DEBUG_ANALYZE, solv->rules + solv->learnt_pool.elements[i]);
1523 * reset the solver decisions to right after the rpm rules.
1524 * called after rules have been enabled/disabled
1528 reset_solver(Solver *solv)
1530 Pool *pool = solv->pool;
1534 enabledisablelearntrules(solv);
1536 /* redo all direct rpm rule decisions */
1537 /* we break at the first decision with a why attached, this is
1538 * either a job/system rule assertion or a propagated decision */
1539 for (i = 0; i < solv->decisionq.count; i++)
1541 v = solv->decisionq.elements[i];
1542 solv->decisionmap[v > 0 ? v : -v] = 0;
1544 for (i = 0; i < solv->decisionq_why.count; i++)
1545 if (solv->decisionq_why.elements[i])
1549 v = solv->decisionq.elements[i];
1550 solv->decisionmap[v > 0 ? v : -v] = v > 0 ? 1 : -1;
1553 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions done reduced from %d to %d\n", solv->decisionq.count, i);
1555 solv->decisionq_why.count = i;
1556 solv->decisionq.count = i;
1557 solv->recommends_index = -1;
1558 solv->propagate_index = 0;
1560 /* redo all job/system decisions */
1561 makeruledecisions(solv);
1562 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
1563 /* recreate watch chains */
1569 * analyze_unsolvable_rule
1573 analyze_unsolvable_rule(Solver *solv, Rule *r)
1575 Pool *pool = solv->pool;
1577 Id why = r - solv->rules;
1578 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
1579 printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
1580 if (solv->learntrules && why >= solv->learntrules)
1582 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1583 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i]);
1586 /* do not add rpm rules to problem */
1587 if (why < solv->jobrules)
1589 /* turn rule into problem */
1590 if (why >= solv->jobrules && why < solv->systemrules)
1591 why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
1592 /* return if problem already countains our rule */
1593 if (solv->problems.count)
1595 for (i = solv->problems.count - 1; i >= 0; i--)
1596 if (solv->problems.elements[i] == 0) /* end of last problem reached? */
1598 else if (solv->problems.elements[i] == why)
1601 queue_push(&solv->problems, why);
1606 * analyze_unsolvable
1608 * return: 1 - disabled some rules, try again
1613 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
1615 Pool *pool = solv->pool;
1617 Map seen; /* global to speed things up? */
1620 Id *decisionmap = solv->decisionmap;
1621 int oldproblemcount;
1622 int oldlearntpoolcount;
1625 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
1626 oldproblemcount = solv->problems.count;
1627 oldlearntpoolcount = solv->learnt_pool.count;
1629 /* make room for proof index */
1630 /* must update it later, as analyze_unsolvable_rule would confuse
1631 * it with a rule index if we put the real value in already */
1632 queue_push(&solv->problems, 0);
1635 map_init(&seen, pool->nsolvables);
1636 queue_push(&solv->learnt_pool, r - solv->rules);
1637 analyze_unsolvable_rule(solv, r);
1638 dp = r->d ? pool->whatprovidesdata + r->d : 0;
1649 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1651 vv = v > 0 ? v : -v;
1652 l = solv->decisionmap[vv];
1657 idx = solv->decisionq.count;
1660 v = solv->decisionq.elements[--idx];
1661 vv = v > 0 ? v : -v;
1662 if (!MAPTST(&seen, vv))
1664 why = solv->decisionq_why.elements[idx];
1667 /* level 1 and no why, must be an rpm assertion */
1668 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
1670 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "RPM ");
1671 printruleelement(solv, SAT_DEBUG_UNSOLVABLE, 0, v);
1673 /* this is the only positive rpm assertion */
1674 if (v == SYSTEMSOLVABLE)
1678 queue_push(&solv->learnt_pool, v);
1681 r = solv->rules + why;
1682 queue_push(&solv->learnt_pool, why);
1683 analyze_unsolvable_rule(solv, r);
1684 dp = r->d ? pool->whatprovidesdata + r->d : 0;
1695 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1697 vv = v > 0 ? v : -v;
1698 l = solv->decisionmap[vv];
1705 queue_push(&solv->problems, 0); /* mark end of this problem */
1708 if (solv->weakrules != solv->learntrules)
1710 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1712 why = solv->problems.elements[i];
1713 if (why < solv->weakrules || why >= solv->learntrules)
1715 if (!lastweak || lastweak < why)
1721 /* disable last weak rule */
1722 solv->problems.count = oldproblemcount;
1723 solv->learnt_pool.count = oldlearntpoolcount;
1724 r = solv->rules + lastweak;
1725 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "disabling weak ");
1726 printrule(solv, SAT_DEBUG_UNSOLVABLE, r);
1727 disablerule(solv, r);
1733 queue_push(&solv->learnt_pool, 0);
1734 solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
1738 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1739 disableproblem(solv, solv->problems.elements[i]);
1743 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
1748 /*-----------------------------------------------------------------*/
1749 /* Decision revert */
1753 * revert decision at level
1757 revert(Solver *solv, int level)
1759 Pool *pool = solv->pool;
1761 while (solv->decisionq.count)
1763 v = solv->decisionq.elements[solv->decisionq.count - 1];
1764 vv = v > 0 ? v : -v;
1765 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1767 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1768 solv->decisionmap[vv] = 0;
1769 solv->decisionq.count--;
1770 solv->decisionq_why.count--;
1771 solv->propagate_index = solv->decisionq.count;
1773 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
1775 solv->branches.count--;
1776 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
1777 solv->branches.count--;
1779 solv->recommends_index = -1;
1784 * watch2onhighest - put watch2 on literal with highest level
1788 watch2onhighest(Solver *solv, Rule *r)
1794 return; /* binary rule, both watches are set */
1795 dp = solv->pool->whatprovidesdata + r->d;
1796 while ((v = *dp++) != 0)
1798 l = solv->decisionmap[v < 0 ? -v : v];
1813 * add free decision to decision q, increase level
1814 * propagate decision, return if no conflict.
1815 * in conflict case, analyze conflict rule, add resulting
1816 * rule to learnt rule set, make decision from learnt
1817 * rule (always unit) and re-propagate.
1821 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules)
1823 Pool *pool = solv->pool;
1832 solv->decisionmap[decision] = level;
1834 solv->decisionmap[-decision] = -level;
1835 queue_push(&solv->decisionq, decision);
1836 queue_push(&solv->decisionq_why, 0);
1840 r = propagate(solv, level);
1844 return analyze_unsolvable(solv, r, disablerules);
1845 POOL_DEBUG(SAT_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
1846 l = analyze(solv, level, r, &p, &d, &why); /* learnt rule in p and d */
1847 if (l >= level || l <= 0)
1849 POOL_DEBUG(SAT_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
1851 revert(solv, level);
1852 r = addrule(solv, p, d); /* p requires d */
1855 if (solv->learnt_why.count != (r - solv->rules) - solv->learntrules)
1857 queue_push(&solv->learnt_why, why);
1860 /* at least 2 literals, needs watches */
1861 watch2onhighest(solv, r);
1862 addwatches(solv, r);
1864 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1865 queue_push(&solv->decisionq, p);
1866 queue_push(&solv->decisionq_why, r - solv->rules);
1867 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1869 POOL_DEBUG(SAT_DEBUG_ANALYZE, "decision: ");
1870 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, p);
1871 POOL_DEBUG(SAT_DEBUG_ANALYZE, "new rule: ");
1872 printrule(solv, SAT_DEBUG_ANALYZE, r);
1880 * install best package from the queue. We add an extra package, inst, if
1881 * provided. See comment in weak install section.
1884 selectandinstall(Solver *solv, int level, Queue *dq, Id inst, int disablerules)
1886 Pool *pool = solv->pool;
1890 if (dq->count > 1 || inst)
1891 policy_filter_unwanted(solv, dq, inst, POLICY_MODE_CHOOSE);
1896 /* choose the supplemented one */
1897 for (i = 0; i < dq->count; i++)
1898 if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
1902 for (i = 1; i < dq->count; i++)
1903 queue_push(&solv->branches, dq->elements[i]);
1904 queue_push(&solv->branches, -level);
1908 p = dq->elements[i];
1910 POOL_DEBUG(SAT_DEBUG_POLICY, "installing %s\n", solvable2str(pool, pool->solvables + p));
1912 return setpropagatelearn(solv, level, p, disablerules);
1916 /*-----------------------------------------------------------------*/
1917 /* Main solver interface */
1922 * create solver structure
1924 * pool: all available solvables
1925 * installed: installed Solvables
1928 * Upon solving, rules are created to flag the Solvables
1929 * of the 'installed' Repo as installed.
1933 solver_create(Pool *pool, Repo *installed)
1936 solv = (Solver *)xcalloc(1, sizeof(Solver));
1938 solv->installed = installed;
1940 queue_init(&solv->ruletojob);
1941 queue_init(&solv->decisionq);
1942 queue_init(&solv->decisionq_why);
1943 queue_init(&solv->problems);
1944 queue_init(&solv->suggestions);
1945 queue_init(&solv->learnt_why);
1946 queue_init(&solv->learnt_pool);
1947 queue_init(&solv->branches);
1949 map_init(&solv->recommendsmap, pool->nsolvables);
1950 map_init(&solv->suggestsmap, pool->nsolvables);
1951 map_init(&solv->noupdate, installed ? installed->end - installed->start : 0);
1952 solv->recommends_index = 0;
1954 solv->decisionmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
1955 solv->rules = (Rule *)xmalloc((solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
1956 memset(solv->rules, 0, sizeof(Rule));
1968 solver_free(Solver *solv)
1970 queue_free(&solv->ruletojob);
1971 queue_free(&solv->decisionq);
1972 queue_free(&solv->decisionq_why);
1973 queue_free(&solv->learnt_why);
1974 queue_free(&solv->learnt_pool);
1975 queue_free(&solv->problems);
1976 queue_free(&solv->suggestions);
1977 queue_free(&solv->branches);
1979 map_free(&solv->recommendsmap);
1980 map_free(&solv->suggestsmap);
1981 map_free(&solv->noupdate);
1982 xfree(solv->decisionmap);
1984 xfree(solv->watches);
1985 xfree(solv->weaksystemrules);
1986 xfree(solv->obsoletes);
1987 xfree(solv->obsoletes_data);
1992 /*-------------------------------------------------------*/
1997 * all rules have been set up, now actually run the solver
2002 run_solver(Solver *solv, int disablerules, int doweak)
2010 Pool *pool = solv->pool;
2013 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
2015 POOL_DEBUG (SAT_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
2016 for (i = 0; i < solv->nrules; i++)
2017 printrule(solv, SAT_DEBUG_RULE_CREATION, solv->rules + i);
2020 /* create watches chains */
2023 POOL_DEBUG(SAT_DEBUG_STATS, "initial decisions: %d\n", solv->decisionq.count);
2025 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2026 printdecisions(solv);
2028 /* start SAT algorithm */
2030 systemlevel = level + 1;
2031 POOL_DEBUG(SAT_DEBUG_STATS, "solving...\n");
2042 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
2043 if ((r = propagate(solv, level)) != 0)
2045 if (analyze_unsolvable(solv, r, disablerules))
2053 * installed packages
2056 if (level < systemlevel && solv->installed && solv->installed->nsolvables)
2058 if (!solv->updatesystem)
2060 /* try to keep as many packages as possible */
2061 POOL_DEBUG(SAT_DEBUG_STATS, "installing system packages\n");
2062 for (i = solv->installed->start, n = 0; ; i++)
2064 if (n == solv->installed->nsolvables)
2066 if (i == solv->installed->end)
2067 i = solv->installed->start;
2068 s = pool->solvables + i;
2069 if (s->repo != solv->installed)
2072 if (solv->decisionmap[i] != 0)
2074 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "keeping %s\n", solvable2str(pool, s));
2076 level = setpropagatelearn(solv, level, i, disablerules);
2082 if (level <= olevel)
2086 if (solv->weaksystemrules)
2088 POOL_DEBUG(SAT_DEBUG_STATS, "installing weak system packages\n");
2089 for (i = solv->installed->start; i < solv->installed->end; i++)
2091 if (pool->solvables[i].repo != solv->installed)
2093 if (solv->decisionmap[i] > 0 || (solv->decisionmap[i] < 0 && solv->weaksystemrules[i - solv->installed->start] == 0))
2095 /* noupdate is set if a job is erasing the installed solvable or installing a specific version */
2096 if (MAPTST(&solv->noupdate, i - solv->installed->start))
2099 if (solv->weaksystemrules[i - solv->installed->start])
2101 dp = pool->whatprovidesdata + solv->weaksystemrules[i - solv->installed->start];
2102 while ((p = *dp++) != 0)
2104 if (solv->decisionmap[p] > 0)
2106 if (solv->decisionmap[p] == 0)
2110 continue; /* update package already installed */
2112 if (!dq.count && solv->decisionmap[i] != 0)
2115 /* FIXME: i is handled a bit different because we do not want
2116 * to have it pruned just because it is not recommened.
2117 * we should not prune installed packages instead */
2118 level = selectandinstall(solv, level, &dq, (solv->decisionmap[i] ? 0 : i), disablerules);
2124 if (level <= olevel)
2127 if (i < solv->installed->end)
2130 systemlevel = level;
2137 POOL_DEBUG(SAT_DEBUG_STATS, "deciding unresolved rules\n");
2138 for (i = 1, n = 1; ; i++, n++)
2140 if (n == solv->nrules)
2142 if (i == solv->nrules)
2144 r = solv->rules + i;
2150 /* binary or unary rule */
2151 /* need two positive undecided literals */
2152 if (r->p < 0 || r->w2 <= 0)
2154 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
2156 queue_push(&dq, r->p);
2157 queue_push(&dq, r->w2);
2162 * all negative literals are installed
2163 * no positive literal is installed
2164 * i.e. the rule is not fulfilled and we
2165 * just need to decide on the positive literals
2169 if (solv->decisionmap[-r->p] <= 0)
2174 if (solv->decisionmap[r->p] > 0)
2176 if (solv->decisionmap[r->p] == 0)
2177 queue_push(&dq, r->p);
2179 dp = pool->whatprovidesdata + r->d;
2180 while ((p = *dp++) != 0)
2184 if (solv->decisionmap[-p] <= 0)
2189 if (solv->decisionmap[p] > 0)
2191 if (solv->decisionmap[p] == 0)
2200 /* cannot happen as this means that
2201 * the rule is unit */
2202 printrule(solv, SAT_FATAL, r);
2205 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
2207 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "unfulfilled ");
2208 printrule(solv, SAT_DEBUG_PROPAGATE, r);
2212 level = selectandinstall(solv, level, &dq, 0, disablerules);
2218 if (level < systemlevel)
2221 } /* for(), decide */
2223 if (n != solv->nrules) /* continue if level < systemlevel */
2226 if (doweak && !solv->problems.count)
2230 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended packages\n");
2231 if (!REGARD_RECOMMENDS_OF_INSTALLED_ITEMS)
2233 for (i = 0; i < solv->decisionq.count; i++)
2235 p = solv->decisionq.elements[i];
2236 if (p > 0 && pool->solvables[p].repo == solv->installed)
2237 solv->decisionmap[p] = -solv->decisionmap[p];
2241 for (i = 1; i < pool->nsolvables; i++)
2243 if (solv->decisionmap[i] < 0)
2245 if (solv->decisionmap[i] > 0)
2247 Id *recp, rec, *pp, p;
2248 s = pool->solvables + i;
2249 /* installed, check for recommends */
2250 /* XXX need to special case AND ? */
2253 recp = s->repo->idarraydata + s->recommends;
2254 while ((rec = *recp++) != 0)
2257 FOR_PROVIDES(p, pp, rec)
2259 if (solv->decisionmap[p] > 0)
2264 else if (solv->decisionmap[p] == 0)
2266 queue_pushunique(&dq, p);
2274 s = pool->solvables + i;
2275 if (!s->supplements && !s->freshens)
2277 if (!pool_installable(pool, s))
2279 if (solver_is_supplementing(solv, s))
2280 queue_pushunique(&dq, i);
2283 if (!REGARD_RECOMMENDS_OF_INSTALLED_ITEMS)
2285 for (i = 0; i < solv->decisionq.count; i++)
2287 p = solv->decisionq.elements[i];
2288 if (p > 0 && pool->solvables[p].repo == solv->installed)
2289 solv->decisionmap[p] = -solv->decisionmap[p];
2295 policy_filter_unwanted(solv, &dq, 0, POLICY_MODE_RECOMMEND);
2297 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended %s\n", solvable2str(pool, pool->solvables + p));
2298 level = setpropagatelearn(solv, level, p, 0);
2303 if (solv->solution_callback)
2305 solv->solution_callback(solv, solv->solution_callback_data);
2306 if (solv->branches.count)
2308 int i = solv->branches.count - 1;
2309 int l = -solv->branches.elements[i];
2311 if (solv->branches.elements[i - 1] < 0)
2313 p = solv->branches.elements[i];
2314 POOL_DEBUG(SAT_DEBUG_STATS, "branching with %s\n", solvable2str(pool, pool->solvables + p));
2316 for (j = i + 1; j < solv->branches.count; j++)
2317 queue_push(&dq, solv->branches.elements[j]);
2318 solv->branches.count = i;
2320 revert(solv, level);
2322 for (j = 0; j < dq.count; j++)
2323 queue_push(&solv->branches, dq.elements[j]);
2325 level = setpropagatelearn(solv, level, p, disablerules);
2333 /* all branches done, we're finally finished */
2337 /* minimization step */
2338 if (solv->branches.count)
2340 int l = 0, lasti = -1, lastl = -1;
2342 for (i = solv->branches.count - 1; i >= 0; i--)
2344 p = solv->branches.elements[i];
2347 else if (p > 0 && solv->decisionmap[p] > l + 1)
2355 /* kill old solvable so that we do not loop */
2356 p = solv->branches.elements[lasti];
2357 solv->branches.elements[lasti] = 0;
2358 POOL_DEBUG(SAT_DEBUG_STATS, "minimizing %d -> %d with %s\n", solv->decisionmap[p], l, solvable2str(pool, pool->solvables + p));
2361 revert(solv, level);
2363 level = setpropagatelearn(solv, level, p, disablerules);
2380 * at this point, all rules that led to conflicts are disabled.
2381 * we re-enable all rules of a problem set but rule "sug", then
2382 * continue to disable more rules until there as again a solution.
2385 /* FIXME: think about conflicting assertions */
2388 refine_suggestion(Solver *solv, Queue *job, Id *problem, Id sug, Queue *refined)
2390 Pool *pool = solv->pool;
2397 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
2399 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion start\n");
2400 for (i = 0; problem[i]; i++)
2402 if (problem[i] == sug)
2403 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "=> ");
2404 printproblem(solv, problem[i]);
2407 queue_init(&disabled);
2408 queue_empty(refined);
2409 queue_push(refined, sug);
2411 /* re-enable all problem rules with the exception of "sug" */
2415 for (i = 0; problem[i]; i++)
2416 if (problem[i] != sug)
2417 enableproblem(solv, problem[i]);
2420 disableupdaterules(solv, job, -(sug + 1));
2424 /* re-enable as many weak rules as possible */
2425 for (i = solv->weakrules; i < solv->learntrules; i++)
2427 r = solv->rules + i;
2429 enablerule(solv, r);
2432 queue_empty(&solv->problems);
2433 revert(solv, 1); /* XXX move to reset_solver? */
2436 run_solver(solv, 0, 0);
2437 if (!solv->problems.count)
2439 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no more problems!\n");
2440 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2441 printdecisions(solv);
2442 break; /* great, no more problems */
2444 disabledcnt = disabled.count;
2445 /* skip over proof index */
2446 for (i = 1; i < solv->problems.count - 1; i++)
2448 /* ignore solutions in refined */
2449 v = solv->problems.elements[i];
2451 break; /* end of problem reached */
2452 for (j = 0; problem[j]; j++)
2453 if (problem[j] != sug && problem[j] == v)
2457 queue_push(&disabled, v);
2459 if (disabled.count == disabledcnt)
2461 /* no solution found, this was an invalid suggestion! */
2462 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no solution found!\n");
2466 if (disabled.count == disabledcnt + 1)
2468 /* just one suggestion, add it to refined list */
2469 v = disabled.elements[disabledcnt];
2470 queue_push(refined, v);
2471 disableproblem(solv, v);
2473 disableupdaterules(solv, job, -(v + 1));
2477 /* more than one solution, disable all */
2478 /* do not push anything on refine list */
2479 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
2481 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "more than one solution found:\n");
2482 for (i = disabledcnt; i < disabled.count; i++)
2483 printproblem(solv, disabled.elements[i]);
2485 for (i = disabledcnt; i < disabled.count; i++)
2486 disableproblem(solv, disabled.elements[i]);
2489 /* all done, get us back into the same state as before */
2490 /* enable refined rules again */
2491 for (i = 0; i < disabled.count; i++)
2492 enableproblem(solv, disabled.elements[i]);
2493 /* disable problem rules again */
2494 for (i = 0; problem[i]; i++)
2495 disableproblem(solv, problem[i]);
2496 disableupdaterules(solv, job, -1);
2497 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion end\n");
2501 problems_to_solutions(Solver *solv, Queue *job)
2503 Pool *pool = solv->pool;
2511 if (!solv->problems.count)
2513 queue_clone(&problems, &solv->problems);
2514 queue_init(&solution);
2515 queue_init(&solutions);
2516 /* copy over proof index */
2517 queue_push(&solutions, problems.elements[0]);
2518 problem = problems.elements + 1;
2519 for (i = 1; i < problems.count; i++)
2521 Id v = problems.elements[i];
2524 /* mark end of this problem */
2525 queue_push(&solutions, 0);
2526 queue_push(&solutions, 0);
2527 if (i + 1 == problems.count)
2529 /* copy over proof of next problem */
2530 queue_push(&solutions, problems.elements[i + 1]);
2532 problem = problems.elements + i + 1;
2535 refine_suggestion(solv, job, problem, v, &solution);
2536 if (!solution.count)
2537 continue; /* this solution didn't work out */
2539 for (j = 0; j < solution.count; j++)
2541 why = solution.elements[j];
2543 printproblem(solv, why);
2547 queue_push(&solutions, 0);
2548 queue_push(&solutions, -why);
2550 else if (why >= solv->systemrules && why < solv->weakrules)
2553 p = solv->installed->start + (why - solv->systemrules);
2554 if (solv->weaksystemrules && solv->weaksystemrules[why - solv->systemrules])
2556 Id *dp = pool->whatprovidesdata + solv->weaksystemrules[why - solv->systemrules];
2559 if (*dp >= solv->installed->start && *dp < solv->installed->start + solv->installed->nsolvables)
2561 if (solv->decisionmap[*dp] > 0)
2568 queue_push(&solutions, p);
2569 queue_push(&solutions, rp);
2574 /* mark end of this solution */
2575 queue_push(&solutions, 0);
2576 queue_push(&solutions, 0);
2578 queue_free(&solution);
2579 queue_free(&problems);
2580 /* copy queue over to solutions */
2581 queue_free(&solv->problems);
2582 queue_clone(&solv->problems, &solutions);
2584 /* bring solver back into problem state */
2585 revert(solv, 1); /* XXX move to reset_solver? */
2588 if (solv->problems.count != solutions.count)
2590 queue_free(&solutions);
2594 solver_next_problem(Solver *solv, Id problem)
2598 return solv->problems.count ? 1 : 0;
2599 pp = solv->problems.elements + problem;
2600 while (pp[0] || pp[1])
2604 while (pp[0] || pp[1])
2609 problem = pp - solv->problems.elements;
2610 if (problem >= solv->problems.count)
2616 solver_next_solution(Solver *solv, Id problem, Id solution)
2622 pp = solv->problems.elements + solution;
2623 return pp[0] || pp[1] ? solution : 0;
2625 pp = solv->problems.elements + solution;
2626 while (pp[0] || pp[1])
2629 solution = pp - solv->problems.elements;
2630 return pp[0] || pp[1] ? solution : 0;
2634 solver_next_solutionelement(Solver *solv, Id problem, Id solution, Id element, Id *p, Id *rp)
2637 element = element ? element + 2 : solution;
2638 pp = solv->problems.elements + element;
2639 if (!(pp[0] || pp[1]))
2648 * create obsoletesmap from solver decisions
2649 * required for decision handling
2653 create_obsoletesmap(Solver *solv)
2655 Pool *pool = solv->pool;
2656 Repo *installed = solv->installed;
2657 Id p, *obsoletesmap = NULL;
2661 obsoletesmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
2664 for (i = 0; i < solv->decisionq.count; i++)
2668 n = solv->decisionq.elements[i];
2671 if (n == SYSTEMSOLVABLE)
2673 s = pool->solvables + n;
2674 if (s->repo == installed) /* obsoletes don't count for already installed packages */
2676 FOR_PROVIDES(p, pp, s->name)
2677 if (s->name == pool->solvables[p].name)
2679 if (pool->solvables[p].repo == installed && !obsoletesmap[p])
2681 obsoletesmap[p] = n;
2686 for (i = 0; i < solv->decisionq.count; i++)
2691 n = solv->decisionq.elements[i];
2694 if (n == SYSTEMSOLVABLE)
2696 s = pool->solvables + n;
2697 if (s->repo == installed) /* obsoletes don't count for already installed packages */
2701 obsp = s->repo->idarraydata + s->obsoletes;
2702 while ((obs = *obsp++) != 0)
2703 FOR_PROVIDES(p, pp, obs)
2705 if (pool->solvables[p].repo == installed && !obsoletesmap[p])
2707 obsoletesmap[p] = n;
2713 return obsoletesmap;
2722 printdecisions(Solver *solv)
2724 Pool *pool = solv->pool;
2725 Repo *installed = solv->installed;
2726 Id p, *obsoletesmap = create_obsoletesmap( solv );
2730 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- Decisions -----\n");
2732 /* print solvables to be erased */
2736 FOR_REPO_SOLVABLES(installed, p, s)
2738 if (solv->decisionmap[p] >= 0)
2740 if (obsoletesmap[p])
2742 POOL_DEBUG(SAT_DEBUG_RESULT, "erase %s\n", solvable2str(pool, s));
2746 /* print solvables to be installed */
2748 for (i = 0; i < solv->decisionq.count; i++)
2751 p = solv->decisionq.elements[i];
2754 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2757 s = pool->solvables + p;
2758 POOL_DEBUG(SAT_DEBUG_SCHUBI, "level of %s is %d\n", solvable2str(pool, s), p);
2762 if (p == SYSTEMSOLVABLE)
2764 POOL_DEBUG(SAT_DEBUG_SCHUBI, "SYSTEMSOLVABLE\n");
2767 s = pool->solvables + p;
2768 if (installed && s->repo == installed)
2771 if (!obsoletesmap[p])
2773 POOL_DEBUG(SAT_DEBUG_RESULT, "install %s", solvable2str(pool, s));
2777 POOL_DEBUG(SAT_DEBUG_RESULT, "update %s", solvable2str(pool, s));
2778 POOL_DEBUG(SAT_DEBUG_RESULT, " (obsoletes");
2779 for (j = installed->start; j < installed->end; j++)
2780 if (obsoletesmap[j] == p)
2781 POOL_DEBUG(SAT_DEBUG_RESULT, " %s", solvable2str(pool, pool->solvables + j));
2782 POOL_DEBUG(SAT_DEBUG_RESULT, ")");
2784 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
2787 xfree(obsoletesmap);
2789 if (solv->suggestions.count)
2791 POOL_DEBUG(SAT_DEBUG_RESULT, "\nsuggested packages:\n");
2792 for (i = 0; i < solv->suggestions.count; i++)
2794 s = pool->solvables + solv->suggestions.elements[i];
2795 POOL_DEBUG(SAT_DEBUG_RESULT, "- %s\n", solvable2str(pool, s));
2798 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- Decisions end -----\n");
2801 /* this is basically the reverse of addrpmrulesforsolvable */
2803 solver_problemruleinfo(Solver *solv, Queue *job, Id rid, Id *depp, Id *sourcep, Id *targetp)
2805 Pool *pool = solv->pool;
2806 Repo *installed = solv->installed;
2810 Id p, *pp, req, *reqp, con, *conp, obs, *obsp, *dp;
2812 if (rid >= solv->weakrules)
2814 if (rid >= solv->systemrules)
2817 *sourcep = solv->installed->start + (rid - solv->systemrules);
2819 return SOLVER_PROBLEM_UPDATE_RULE;
2821 if (rid >= solv->jobrules)
2824 r = solv->rules + rid;
2825 p = solv->ruletojob.elements[rid - solv->jobrules];
2826 *depp = job->elements[p + 1];
2828 *targetp = job->elements[p];
2829 if (r->d == 0 && r->w2 == 0 && r->p == -SYSTEMSOLVABLE)
2830 return SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP;
2831 return SOLVER_PROBLEM_JOB_RULE;
2835 /* a rpm rule assertion */
2836 if (rid == -SYSTEMSOLVABLE)
2837 abort(); /* can happen only for job rules */
2838 s = pool->solvables - rid;
2839 if (installed && !solv->fixsystem && s->repo == installed)
2841 if (dontfix) /* dontfix packages never have a neg assertion */
2843 /* see why the package is not installable */
2844 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC && !pool_installable(pool, s))
2845 return SOLVER_PROBLEM_NOT_INSTALLABLE;
2846 /* check requires */
2849 reqp = s->repo->idarraydata + s->requires;
2850 while ((req = *reqp++) != 0)
2852 if (req == SOLVABLE_PREREQMARKER)
2854 dp = pool_whatprovides(pool, req);
2860 return SOLVER_PROBLEM_NOTHING_PROVIDES_DEP;
2865 r = solv->rules + rid;
2867 abort(); /* not a rpm rule */
2868 if (r->d == 0 && r->w2 == 0)
2870 /* an assertion. we don't store them as rpm rules, so
2874 s = pool->solvables - r->p;
2875 if (installed && !solv->fixsystem && s->repo == installed)
2877 if (r->d == 0 && r->w2 < 0)
2879 /* a package conflict */
2880 Solvable *s2 = pool->solvables - r->w2;
2883 if (installed && !solv->fixsystem && s2->repo == installed)
2886 /* if both packages have the same name and at least one of them
2887 * is not installed, they conflict */
2888 if (s->name == s2->name && (!installed || (s->repo != installed || s2->repo != installed)))
2893 return SOLVER_PROBLEM_SAME_NAME;
2896 /* check conflicts in both directions */
2899 conp = s->repo->idarraydata + s->conflicts;
2900 while ((con = *conp++) != 0)
2902 FOR_PROVIDES(p, pp, con)
2904 if (dontfix && pool->solvables[p].repo == installed)
2911 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
2917 conp = s2->repo->idarraydata + s2->conflicts;
2918 while ((con = *conp++) != 0)
2920 FOR_PROVIDES(p, pp, con)
2922 if (dontfix2 && pool->solvables[p].repo == installed)
2929 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
2933 /* check obsoletes in both directions */
2934 if ((!installed || s->repo != installed) && s->obsoletes)
2936 obsp = s->repo->idarraydata + s->obsoletes;
2937 while ((obs = *obsp++) != 0)
2939 FOR_PROVIDES(p, pp, obs)
2946 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
2950 if ((!installed || s2->repo != installed) && s2->obsoletes)
2952 obsp = s2->repo->idarraydata + s2->obsoletes;
2953 while ((obs = *obsp++) != 0)
2955 FOR_PROVIDES(p, pp, obs)
2962 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
2966 /* all cases checked, can't happen */
2969 /* simple requires */
2972 reqp = s->repo->idarraydata + s->requires;
2973 while ((req = *reqp++) != 0)
2975 if (req == SOLVABLE_PREREQMARKER)
2977 dp = pool_whatprovides(pool, req);
2980 if (*dp == r->w2 && dp[1] == 0)
2983 else if (dp - pool->whatprovidesdata == r->d)
2991 return SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE;
2995 findproblemrule_internal(Solver *solv, Id idx, Id *reqrp, Id *conrp, Id *sysrp, Id *jobrp)
2998 Id lreqr, lconr, lsysr, ljobr;
3002 lreqr = lconr = lsysr = ljobr = 0;
3003 while ((rid = solv->learnt_pool.elements[idx++]) != 0)
3005 if (rid >= solv->learntrules)
3006 findproblemrule_internal(solv, solv->learnt_why.elements[rid - solv->learntrules], &lreqr, &lconr, &lsysr, &ljobr);
3007 else if (rid >= solv->systemrules)
3012 else if (rid >= solv->jobrules)
3019 r = solv->rules + rid;
3020 if (!r->d && r->w2 < 0)
3033 /* assertion, counts as require rule */
3034 /* ignore system solvable as we need useful info */
3035 if (rid == -SYSTEMSOLVABLE)
3037 if (!*reqrp || !reqassert)
3044 if (!*reqrp && lreqr)
3046 if (!*conrp && lconr)
3048 if (!*jobrp && ljobr)
3050 if (!*sysrp && lsysr)
3055 solver_findproblemrule(Solver *solv, Id problem)
3057 Id reqr, conr, sysr, jobr;
3058 Id idx = solv->problems.elements[problem - 1];
3059 reqr = conr = sysr = jobr = 0;
3060 findproblemrule_internal(solv, idx, &reqr, &conr, &sysr, &jobr);
3073 printprobleminfo(Solver *solv, Queue *job, Id problem)
3075 Pool *pool = solv->pool;
3077 Id dep, source, target;
3080 probr = solver_findproblemrule(solv, problem);
3081 switch (solver_problemruleinfo(solv, job, probr, &dep, &source, &target))
3083 case SOLVER_PROBLEM_UPDATE_RULE:
3084 s = pool_id2solvable(pool, source);
3085 POOL_DEBUG(SAT_DEBUG_RESULT, "problem with installed package %s\n", solvable2str(pool, s));
3087 case SOLVER_PROBLEM_JOB_RULE:
3088 POOL_DEBUG(SAT_DEBUG_RESULT, "conflicting requests\n");
3090 case SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP:
3091 POOL_DEBUG(SAT_DEBUG_RESULT, "nothing provides requested %s\n", dep2str(pool, dep));
3093 case SOLVER_PROBLEM_NOT_INSTALLABLE:
3094 s = pool_id2solvable(pool, source);
3095 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s is not installable\n", solvable2str(pool, s));
3097 case SOLVER_PROBLEM_NOTHING_PROVIDES_DEP:
3098 s = pool_id2solvable(pool, source);
3099 POOL_DEBUG(SAT_DEBUG_RESULT, "nothing provides %s needed by %s\n", dep2str(pool, dep), solvable2str(pool, s));
3101 case SOLVER_PROBLEM_SAME_NAME:
3102 s = pool_id2solvable(pool, source);
3103 s2 = pool_id2solvable(pool, target);
3104 POOL_DEBUG(SAT_DEBUG_RESULT, "cannot install both %s and %s\n", solvable2str(pool, s), solvable2str(pool, s2));
3106 case SOLVER_PROBLEM_PACKAGE_CONFLICT:
3107 s = pool_id2solvable(pool, source);
3108 s2 = pool_id2solvable(pool, target);
3109 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s conflicts with %s provided by %s\n", solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
3111 case SOLVER_PROBLEM_PACKAGE_OBSOLETES:
3112 s = pool_id2solvable(pool, source);
3113 s2 = pool_id2solvable(pool, target);
3114 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s obsoletes %s provided by %s\n", solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
3116 case SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE:
3117 s = pool_id2solvable(pool, source);
3118 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s requires %s, but none of the providers can be installed\n", solvable2str(pool, s), dep2str(pool, dep));
3124 printsolutions(Solver *solv, Queue *job)
3126 Pool *pool = solv->pool;
3129 Id problem, solution, element;
3132 POOL_DEBUG(SAT_DEBUG_RESULT, "Encountered problems! Here are the solutions:\n\n");
3135 while ((problem = solver_next_problem(solv, problem)) != 0)
3137 POOL_DEBUG(SAT_DEBUG_RESULT, "Problem %d:\n", pcnt++);
3138 POOL_DEBUG(SAT_DEBUG_RESULT, "====================================\n");
3139 printprobleminfo(solv, job, problem);
3140 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
3142 while ((solution = solver_next_solution(solv, problem, solution)) != 0)
3145 while ((element = solver_next_solutionelement(solv, problem, solution, element, &p, &rp)) != 0)
3149 /* job, rp is index into job queue */
3150 what = job->elements[rp];
3151 switch (job->elements[rp - 1])
3153 case SOLVER_INSTALL_SOLVABLE:
3154 s = pool->solvables + what;
3155 if (solv->installed && s->repo == solv->installed)
3156 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not keep %s installed\n", solvable2str(pool, s));
3158 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install %s\n", solvable2str(pool, s));
3160 case SOLVER_ERASE_SOLVABLE:
3161 s = pool->solvables + what;
3162 if (solv->installed && s->repo == solv->installed)
3163 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall %s\n", solvable2str(pool, s));
3165 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not forbid installation of %s\n", solvable2str(pool, s));
3167 case SOLVER_INSTALL_SOLVABLE_NAME:
3168 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install %s\n", id2str(pool, what));
3170 case SOLVER_ERASE_SOLVABLE_NAME:
3171 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall %s\n", id2str(pool, what));
3173 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3174 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install a solvable providing %s\n", dep2str(pool, what));
3176 case SOLVER_ERASE_SOLVABLE_PROVIDES:
3177 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall all solvables providing %s\n", dep2str(pool, what));
3179 case SOLVER_INSTALL_SOLVABLE_UPDATE:
3180 s = pool->solvables + what;
3181 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install most recent version of %s\n", solvable2str(pool, s));
3184 POOL_DEBUG(SAT_DEBUG_RESULT, "- do something different\n");
3190 /* policy, replace p with rp */
3191 s = pool->solvables + p;
3192 sd = rp ? pool->solvables + rp : 0;
3196 if (!solv->allowdowngrade && evrcmp(pool, s->evr, sd->evr, EVRCMP_MATCH_RELEASE) > 0)
3198 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow downgrade of %s to %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3201 if (!solv->allowarchchange && s->name == sd->name && s->arch != sd->arch && policy_illegal_archchange(pool, s, sd))
3203 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow architecture change of %s to %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3206 if (!solv->allowvendorchange && s->name == sd->name && s->vendor != sd->vendor && policy_illegal_vendorchange(pool, s, sd))
3209 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));
3211 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));
3215 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow replacement of %s with %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3219 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow deinstallation of %s\n", solvable2str(pool, s));
3224 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
3230 /* for each installed solvable find which packages with *different* names
3231 * obsolete the solvable.
3232 * this index is used in policy_findupdatepackages if noupdateprovide is set.
3236 create_obsolete_index(Solver *solv)
3238 Pool *pool = solv->pool;
3240 Repo *installed = solv->installed;
3241 Id p, *pp, obs, *obsp, *obsoletes, *obsoletes_data;
3244 if (!installed || !installed->nsolvables)
3246 /* create reverse obsoletes map for installed solvables */
3247 solv->obsoletes = obsoletes = xcalloc(installed->end - installed->start, sizeof(Id));
3248 for (i = 1; i < pool->nsolvables; i++)
3250 s = pool->solvables + i;
3251 if (s->repo == installed)
3255 if (!pool_installable(pool, s))
3257 obsp = s->repo->idarraydata + s->obsoletes;
3258 while ((obs = *obsp++) != 0)
3259 FOR_PROVIDES(p, pp, obs)
3261 if (pool->solvables[p].repo != installed)
3263 if (pool->solvables[p].name == s->name)
3265 obsoletes[p - installed->start]++;
3269 for (i = 0; i < installed->nsolvables; i++)
3272 n += obsoletes[i] + 1;
3275 solv->obsoletes_data = obsoletes_data = xcalloc(n + 1, sizeof(Id));
3276 POOL_DEBUG(SAT_DEBUG_STATS, "obsoletes data: %d entries\n", n + 1);
3277 for (i = pool->nsolvables - 1; i > 0; i--)
3279 s = pool->solvables + i;
3280 if (s->repo == installed)
3284 if (!pool_installable(pool, s))
3286 obsp = s->repo->idarraydata + s->obsoletes;
3287 while ((obs = *obsp++) != 0)
3288 FOR_PROVIDES(p, pp, obs)
3290 if (pool->solvables[p].repo != installed)
3292 if (pool->solvables[p].name == s->name)
3294 p -= installed->start;
3295 if (obsoletes_data[obsoletes[p]] != i)
3296 obsoletes_data[--obsoletes[p]] = i;
3302 /*-----------------------------------------------------------------*/
3312 solver_solve(Solver *solv, Queue *job)
3314 Pool *pool = solv->pool;
3315 Repo *installed = solv->installed;
3318 Map addedmap; /* '1' == have rpm-rules for solvable */
3319 Id how, what, p, *pp, d;
3323 /* create whatprovides if not already there */
3324 if (!pool->whatprovides)
3325 pool_createwhatprovides(pool);
3327 /* create obsolete index if needed */
3328 if (solv->noupdateprovide)
3329 create_obsolete_index(solv);
3332 * create basic rule set of all involved packages
3333 * use addedmap bitmap to make sure we don't create rules twice
3337 map_init(&addedmap, pool->nsolvables);
3341 * always install our system solvable
3343 MAPSET(&addedmap, SYSTEMSOLVABLE);
3344 queue_push(&solv->decisionq, SYSTEMSOLVABLE);
3345 queue_push(&solv->decisionq_why, 0);
3346 solv->decisionmap[SYSTEMSOLVABLE] = 1;
3349 * create rules for all package that could be involved with the solving
3350 * so called: rpm rules
3355 oldnrules = solv->nrules;
3356 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for installed solvables ***\n");
3357 FOR_REPO_SOLVABLES(installed, p, s)
3358 addrpmrulesforsolvable(solv, s, &addedmap);
3359 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
3360 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for updaters of installed solvables ***\n");
3361 oldnrules = solv->nrules;
3362 FOR_REPO_SOLVABLES(installed, p, s)
3363 addrpmrulesforupdaters(solv, s, &addedmap, 1);
3364 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
3367 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for packages involved with a job ***\n");
3368 oldnrules = solv->nrules;
3369 for (i = 0; i < job->count; i += 2)
3371 how = job->elements[i];
3372 what = job->elements[i + 1];
3376 case SOLVER_INSTALL_SOLVABLE:
3377 addrpmrulesforsolvable(solv, pool->solvables + what, &addedmap);
3379 case SOLVER_INSTALL_SOLVABLE_NAME:
3380 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3381 FOR_PROVIDES(p, pp, what)
3383 /* if by name, ensure that the name matches */
3384 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
3386 addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
3389 case SOLVER_INSTALL_SOLVABLE_UPDATE:
3390 /* dont allow downgrade */
3391 addrpmrulesforupdaters(solv, pool->solvables + what, &addedmap, 0);
3395 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
3397 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for recommended/suggested packages ***\n");
3399 oldnrules = solv->nrules;
3400 addrpmrulesforweak(solv, &addedmap);
3401 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
3403 IF_POOLDEBUG (SAT_DEBUG_STATS)
3405 int possible = 0, installable = 0;
3406 for (i = 1; i < pool->nsolvables; i++)
3408 if (pool_installable(pool, pool->solvables + i))
3410 if (MAPTST(&addedmap, i))
3413 POOL_DEBUG(SAT_DEBUG_STATS, "%d of %d installable solvables considered for solving (rules has been generated for)\n", possible, installable);
3417 * first pass done, we now have all the rpm rules we need.
3418 * unify existing rules before going over all job rules and
3420 * at this point the system is always solvable,
3421 * as an empty system (remove all packages) is a valid solution
3424 unifyrules(solv); /* remove duplicate rpm rules */
3426 POOL_DEBUG(SAT_DEBUG_STATS, "decisions so far: %d\n", solv->decisionq.count);
3427 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
3428 printdecisions (solv);
3431 * now add all job rules
3434 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add JOB rules ***\n");
3436 solv->jobrules = solv->nrules;
3438 for (i = 0; i < job->count; i += 2)
3440 oldnrules = solv->nrules;
3442 how = job->elements[i];
3443 what = job->elements[i + 1];
3446 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
3447 s = pool->solvables + what;
3448 POOL_DEBUG(SAT_DEBUG_JOB, "job: install solvable %s\n", solvable2str(pool, s));
3449 addrule(solv, what, 0); /* install by Id */
3450 queue_push(&solv->ruletojob, i);
3452 case SOLVER_ERASE_SOLVABLE:
3453 s = pool->solvables + what;
3454 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase solvable %s\n", solvable2str(pool, s));
3455 addrule(solv, -what, 0); /* remove by Id */
3456 queue_push(&solv->ruletojob, i);
3458 case SOLVER_INSTALL_SOLVABLE_NAME: /* install by capability */
3459 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3460 if (how == SOLVER_INSTALL_SOLVABLE_NAME)
3461 POOL_DEBUG(SAT_DEBUG_JOB, "job: install name %s\n", id2str(pool, what));
3462 if (how == SOLVER_INSTALL_SOLVABLE_PROVIDES)
3463 POOL_DEBUG(SAT_DEBUG_JOB, "job: install provides %s\n", dep2str(pool, what));
3465 FOR_PROVIDES(p, pp, what)
3467 /* if by name, ensure that the name matches */
3468 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
3474 /* no provider, make this an impossible rule */
3475 queue_push(&q, -SYSTEMSOLVABLE);
3478 p = queue_shift(&q); /* get first provider */
3480 d = 0; /* single provider ? -> make assertion */
3482 d = pool_queuetowhatprovides(pool, &q); /* get all providers */
3483 addrule(solv, p, d); /* add 'requires' rule */
3484 queue_push(&solv->ruletojob, i);
3486 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
3487 case SOLVER_ERASE_SOLVABLE_PROVIDES:
3488 if (how == SOLVER_ERASE_SOLVABLE_NAME)
3489 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase name %s\n", id2str(pool, what));
3490 if (how == SOLVER_ERASE_SOLVABLE_PROVIDES)
3491 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase provides %s\n", dep2str(pool, what));
3492 FOR_PROVIDES(p, pp, what)
3494 /* if by name, ensure that the name matches */
3495 if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
3497 addrule(solv, -p, 0); /* add 'remove' rule */
3498 queue_push(&solv->ruletojob, i);
3501 case SOLVER_INSTALL_SOLVABLE_UPDATE: /* find update for solvable */
3502 s = pool->solvables + what;
3503 POOL_DEBUG(SAT_DEBUG_JOB, "job: update %s\n", solvable2str(pool, s));
3504 addupdaterule(solv, s, 0);
3505 queue_push(&solv->ruletojob, i);
3508 IF_POOLDEBUG (SAT_DEBUG_JOB)
3511 if (solv->nrules == oldnrules)
3512 POOL_DEBUG(SAT_DEBUG_JOB, " - no rule created");
3513 for (j = oldnrules; j < solv->nrules; j++)
3515 POOL_DEBUG(SAT_DEBUG_JOB, " - job ");
3516 printrule(solv, SAT_DEBUG_JOB, solv->rules + j);
3521 if (solv->ruletojob.count != solv->nrules - solv->jobrules)
3525 * now add system rules
3529 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add system rules ***\n");
3532 solv->systemrules = solv->nrules;
3535 * create rules for updating installed solvables
3539 if (installed && !solv->allowuninstall)
3540 { /* loop over all installed solvables */
3541 /* we create all update rules, but disable some later on depending on the job */
3542 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3543 if (s->repo == installed)
3544 addupdaterule(solv, s, 0); /* allowall = 0 */
3546 addupdaterule(solv, 0, 0); /* create dummy rule; allowall = 0 */
3547 /* consistency check: we added a rule for _every_ system solvable */
3548 if (solv->nrules - solv->systemrules != installed->end - installed->start)
3552 /* create special weak system rules */
3553 /* those are used later on to keep a version of the installed packages in
3555 if (installed && installed->nsolvables)
3557 solv->weaksystemrules = xcalloc(installed->end - installed->start, sizeof(Id));
3558 FOR_REPO_SOLVABLES(installed, p, s)
3560 policy_findupdatepackages(solv, s, &q, 1);
3562 solv->weaksystemrules[p - installed->start] = pool_queuetowhatprovides(pool, &q);
3566 /* free unneeded memory */
3567 map_free(&addedmap);
3570 solv->weakrules = solv->nrules;
3572 /* try real hard to keep packages installed */
3575 FOR_REPO_SOLVABLES(installed, p, s)
3577 /* FIXME: can't work with refine_suggestion! */
3578 /* need to always add the rule but disable it */
3579 if (MAPTST(&solv->noupdate, p - installed->start))
3581 d = solv->weaksystemrules[p - installed->start];
3582 addrule(solv, p, d);
3586 /* all new rules are learnt after this point */
3587 solv->learntrules = solv->nrules;
3594 disableupdaterules(solv, job, -1);
3595 makeruledecisions(solv);
3597 POOL_DEBUG(SAT_DEBUG_STATS, "problems so far: %d\n", solv->problems.count);
3599 run_solver(solv, 1, 1);
3601 /* find suggested packages */
3602 if (!solv->problems.count)
3604 Id sug, *sugp, p, *pp;
3606 /* create map of all suggests that are still open */
3607 solv->recommends_index = -1;
3608 MAPZERO(&solv->suggestsmap);
3609 for (i = 0; i < solv->decisionq.count; i++)
3611 p = solv->decisionq.elements[i];
3614 s = pool->solvables + p;
3617 sugp = s->repo->idarraydata + s->suggests;
3618 while ((sug = *sugp++) != 0)
3620 FOR_PROVIDES(p, pp, sug)
3621 if (solv->decisionmap[p] > 0)
3624 continue; /* already fulfilled */
3625 FOR_PROVIDES(p, pp, sug)
3626 MAPSET(&solv->suggestsmap, p);
3630 for (i = 1; i < pool->nsolvables; i++)
3632 if (solv->decisionmap[i] != 0)
3634 s = pool->solvables + i;
3635 if (!MAPTST(&solv->suggestsmap, i))
3639 if (!pool_installable(pool, s))
3641 if (!solver_is_enhancing(solv, s))
3644 queue_push(&solv->suggestions, i);
3646 policy_filter_unwanted(solv, &solv->suggestions, 0, POLICY_MODE_SUGGEST);
3649 if (solv->problems.count)
3650 problems_to_solutions(solv, job);