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");
146 else if (r->d == ID_NULL)
153 v = solv->pool->whatprovidesdata[r->d + i - 1];
156 printruleelement(solv, type, r, v);
158 POOL_DEBUG(type, " next: %d %d\n", r->n1, r->n2);
162 printruleclass(Solver *solv, int type, Rule *r)
164 Pool *pool = solv->pool;
165 if (r - solv->rules >= solv->learntrules)
166 POOL_DEBUG(type, "LEARNT ");
167 else if (r - solv->rules >= solv->weakrules)
168 POOL_DEBUG(type, "WEAK ");
169 else if (r - solv->rules >= solv->systemrules)
170 POOL_DEBUG(type, "SYSTEM ");
171 else if (r - solv->rules >= solv->jobrules)
172 POOL_DEBUG(type, "JOB ");
173 printrule(solv, type, r);
177 /*-----------------------------------------------------------------*/
183 static Pool *unifyrules_sortcmp_data;
186 * compare rules for unification sort
190 unifyrules_sortcmp(const void *ap, const void *bp)
192 Pool *pool = unifyrules_sortcmp_data;
193 Rule *a = (Rule *)ap;
194 Rule *b = (Rule *)bp;
200 return x; /* p differs */
203 if (a->d == 0 && b->d == 0)
204 return a->w2 - b->w2; /* assertion: return w2 diff */
206 if (a->d == 0) /* a is assertion, b not */
208 x = a->w2 - pool->whatprovidesdata[b->d];
212 if (b->d == 0) /* b is assertion, a not */
214 x = pool->whatprovidesdata[a->d] - b->w2;
218 /* compare whatprovidesdata */
219 ad = pool->whatprovidesdata + a->d;
220 bd = pool->whatprovidesdata + b->d;
222 if ((x = *ad++ - *bd++) != 0)
233 unifyrules(Solver *solv)
235 Pool *pool = solv->pool;
239 if (solv->nrules <= 1) /* nothing to unify */
242 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- unifyrules -----\n");
244 /* sort rules first */
245 unifyrules_sortcmp_data = solv->pool;
246 qsort(solv->rules + 1, solv->nrules - 1, sizeof(Rule), unifyrules_sortcmp);
253 for (i = j = 1, ir = solv->rules + 1; i < solv->nrules; i++, ir++)
255 if (jr && !unifyrules_sortcmp(ir, jr))
256 continue; /* prune! */
257 jr = solv->rules + j++; /* keep! */
262 /* reduced count from nrules to j rules */
263 POOL_DEBUG(SAT_DEBUG_STATS, "pruned rules from %d to %d\n", solv->nrules, j);
265 /* adapt rule buffer */
267 solv->rules = (Rule *)xrealloc(solv->rules, ((solv->nrules + RULES_BLOCK) & ~RULES_BLOCK) * sizeof(Rule));
268 IF_POOLDEBUG (SAT_DEBUG_STATS)
275 for (i = 1; i < solv->nrules; i++)
282 dp = solv->pool->whatprovidesdata + r->d;
287 POOL_DEBUG(SAT_DEBUG_STATS, " binary: %d\n", binr);
288 POOL_DEBUG(SAT_DEBUG_STATS, " normal: %d, %d literals\n", solv->nrules - 1 - binr, lits);
290 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- unifyrules end -----\n");
300 hashrule(Solver *solv, Id p, Id d, int n)
302 unsigned int x = (unsigned int)p;
306 return (x * 37) ^ (unsigned int)d;
307 dp = solv->pool->whatprovidesdata + d;
309 x = (x * 37) ^ (unsigned int)*dp++;
317 * p = direct literal; always < 0 for installed rpm rules
318 * d, if < 0 direct literal, if > 0 offset into whatprovides, if == 0 rule is assertion (look at p only)
321 * A requires b, b provided by B1,B2,B3 => (-A|B1|B2|B3)
323 * p < 0 : pkg id of A
324 * d > 0 : Offset in whatprovidesdata (list of providers of b)
326 * A conflicts b, b provided by B1,B2,B3 => (-A|-B1), (-A|-B2), (-A|-B3)
327 * p < 0 : pkg id of A
328 * d < 0 : Id of solvable (e.g. B1)
330 * d == 0: unary rule, assertion => (A) or (-A)
332 * Install: p > 0, d = 0 (A) user requested install
333 * Remove: p < 0, d = 0 (-A) user requested remove
334 * Requires: p < 0, d > 0 (-A|B1|B2|...) d: <list of providers for requirement of p>
335 * Updates: p > 0, d > 0 (A|B1|B2|...) d: <list of updates for solvable p>
336 * Conflicts: p < 0, d < 0 (-A|-B) either p (conflict issuer) or d (conflict provider) (binary rule)
337 * ? p > 0, d < 0 (A|-B)
338 * No-op ?: p = 0, d = 0 (null) (used as policy rule placeholder)
342 * Direct assertion (no watch needed)( if d <0 ) --> d = 0, w1 = p, w2 = 0
343 * Binary rule: p = first literal, d = 0, w2 = second literal, w1 = p
344 * every other : w1 = p, w2 = whatprovidesdata[d];
345 * Disabled rule: w1 = 0
347 * always returns a rule for non-rpm rules
351 addrule(Solver *solv, Id p, Id d)
353 Pool *pool = solv->pool;
357 int n = 0; /* number of literals in rule - 1
358 0 = direct assertion (single literal)
362 /* it often happenes that requires lead to adding the same rpm rule
363 * multiple times, so we prune those duplicates right away to make
364 * the work for unifyrules a bit easier */
366 if (solv->nrules && !solv->jobrules)
368 r = solv->rules + solv->nrules - 1; /* get the last added rule */
369 if (r->p == p && r->d == d && d != 0) /* identical and not user requested */
375 /* always a binary rule */
377 return 0; /* ignore self conflict */
382 for (dp = pool->whatprovidesdata + d; *dp; dp++, n++)
384 return 0; /* rule is self-fulfilling */
389 if (n == 0 && !solv->jobrules)
391 /* this is a rpm rule assertion, we do not have to allocate it */
392 /* it can be identified by a level of 1 and a zero reason */
393 /* we must not drop those rules from the decisionq when rewinding! */
396 if (solv->decisionmap[-p] > 0 || solv->decisionmap[-p] < -1)
398 if (solv->decisionmap[-p])
399 return 0; /* already got that one */
400 queue_push(&solv->decisionq, p);
401 queue_push(&solv->decisionq_why, 0);
402 solv->decisionmap[-p] = -1;
407 /* smallest literal first so we can find dups */
411 n = 1; /* re-set n, was used as temp var */
414 /* check if the last added rule is exactly the same as what we're looking for. */
415 if (r && n == 1 && !r->d && r->p == p && r->w2 == d)
418 if (r && n > 1 && r->d && r->p == p)
423 dp2 = pool->whatprovidesdata + r->d;
424 for (dp = pool->whatprovidesdata + d; *dp; dp++, dp2++)
435 /* check and extend rule buffer */
436 if ((solv->nrules & RULES_BLOCK) == 0)
438 solv->rules = (Rule *)xrealloc(solv->rules, (solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
441 r = solv->rules + solv->nrules++; /* point to rule space */
446 /* direct assertion, no watch needed */
462 r->w2 = pool->whatprovidesdata[d];
467 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
469 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, " Add rule: ");
470 printrule(solv, SAT_DEBUG_RULE_CREATION, r);
477 disablerule(Solver *solv, Rule *r)
483 enablerule(Solver *solv, Rule *r)
485 if (r->d == 0 || r->w2 != r->p)
488 r->w1 = solv->pool->whatprovidesdata[r->d];
492 /**********************************************************************************/
494 /* a problem is an item on the solver's problem list. It can either be >0, in that
495 * case it is a system (upgrade) rule, or it can be <0, which makes it refer to a job
496 * consisting of multiple job rules.
500 disableproblem(Solver *solv, Id v)
508 disablerule(solv, solv->rules + v);
512 jp = solv->ruletojob.elements;
513 for (i = solv->jobrules, r = solv->rules + i; i < solv->systemrules; i++, r++, jp++)
515 disablerule(solv, r);
519 enableproblem(Solver *solv, Id v)
527 enablerule(solv, solv->rules + v);
531 jp = solv->ruletojob.elements;
532 for (i = solv->jobrules, r = solv->rules + i; i < solv->systemrules; i++, r++, jp++)
538 printproblem(Solver *solv, Id v)
540 Pool *pool = solv->pool;
546 printrule(solv, SAT_DEBUG_SOLUTIONS, solv->rules + v);
550 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "JOB %d\n", v);
551 jp = solv->ruletojob.elements;
552 for (i = solv->jobrules, r = solv->rules + i; i < solv->systemrules; i++, r++, jp++)
555 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "- ");
556 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
558 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "ENDJOB\n");
564 /************************************************************************/
566 /* go through system and job rules and add direct assertions
567 * to the decisionqueue. If we find a conflict, disable rules and
568 * add them to problem queue.
569 * (disablerules is always true for the cases when we get called)
572 makeruledecisions(Solver *solv)
574 Pool *pool = solv->pool;
580 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- makeruledecisions ; size decisionq: %d -----\n",solv->decisionq.count);
582 decisionstart = solv->decisionq.count;
583 /* rpm rules don't have assertions, so we can start with the job
585 for (ri = solv->jobrules, r = solv->rules + ri; ri < solv->nrules; ri++, r++)
587 if (!r->w1 || r->w2) /* disabled or no assertion */
591 if (solv->decisionmap[vv] == 0)
593 queue_push(&solv->decisionq, v);
594 queue_push(&solv->decisionq_why, r - solv->rules);
595 solv->decisionmap[vv] = v > 0 ? 1 : -1;
596 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
598 Solvable *s = solv->pool->solvables + vv;
600 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "conflicting %s (assertion)\n", solvable2str(solv->pool, s));
602 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "installing %s (assertion)\n", solvable2str(solv->pool, s));
606 if (v > 0 && solv->decisionmap[vv] > 0)
608 if (v < 0 && solv->decisionmap[vv] < 0)
610 /* found a conflict! */
611 if (ri >= solv->learntrules)
613 /* cannot happen, as this would mean that the problem
614 * was not solvable, so we wouldn't have created the
615 * learnt rule at all */
618 /* if we are weak, just disable ourself */
619 if (ri >= solv->weakrules)
621 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "assertion conflict, but I am weak, disabling ");
622 printrule(solv, SAT_DEBUG_UNSOLVABLE, r);
623 disablerule(solv, r);
627 /* only job and system rules left */
628 for (i = 0; i < solv->decisionq.count; i++)
629 if (solv->decisionq.elements[i] == -v)
631 if (i == solv->decisionq.count)
633 if (solv->decisionq_why.elements[i] == 0)
635 /* conflict with rpm rule, need only disable our rule */
636 if (v < 0 && v != -SYSTEMSOLVABLE)
639 queue_push(&solv->problems, solv->learnt_pool.count);
640 queue_push(&solv->learnt_pool, ri);
641 queue_push(&solv->learnt_pool, v != -SYSTEMSOLVABLE ? -v : v);
642 queue_push(&solv->learnt_pool, 0);
643 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflict with rpm rule, disabling rule #%d\n", ri);
644 if (ri < solv->systemrules)
645 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
648 queue_push(&solv->problems, v);
649 disableproblem(solv, v);
650 queue_push(&solv->problems, 0);
654 queue_push(&solv->problems, solv->learnt_pool.count);
655 queue_push(&solv->learnt_pool, ri);
656 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
657 queue_push(&solv->learnt_pool, 0);
659 /* conflict with another job or system rule */
660 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "conflicting system/job assertions over literal %d\n", vv);
661 /* push all of our rules asserting this literal on the problem stack */
662 for (i = solv->jobrules, rr = solv->rules + i; i < solv->nrules; i++, rr++)
664 if (!rr->w1 || rr->w2)
666 if (rr->p != vv && rr->p != -vv)
668 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, " - disabling rule #%d\n", i);
670 if (i < solv->systemrules)
671 v = -(solv->ruletojob.elements[i - solv->jobrules] + 1);
672 queue_push(&solv->problems, v);
673 disableproblem(solv, v);
675 queue_push(&solv->problems, 0);
678 while (solv->decisionq.count > decisionstart)
680 v = solv->decisionq.elements[--solv->decisionq.count];
681 --solv->decisionq_why.count;
683 solv->decisionmap[vv] = 0;
685 ri = solv->jobrules - 1;
686 r = solv->rules + ri;
689 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- makeruledecisions end; size decisionq: %d -----\n",solv->decisionq.count);
693 * we have enabled or disabled some of our rules. We now reenable all
694 * of our learnt rules but the ones that were learnt from rules that
698 enabledisablelearntrules(Solver *solv)
700 Pool *pool = solv->pool;
705 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "enabledisablelearntrules called\n");
706 for (i = solv->learntrules, r = solv->rules + i; i < solv->nrules; i++, r++)
708 whyp = solv->learnt_pool.elements + solv->learnt_why.elements[i - solv->learntrules];
709 while ((why = *whyp++) != 0)
711 if (why < 0 || why >= i)
712 abort(); /* cannot reference newer learnt rules */
713 if (!solv->rules[why].w1)
716 /* why != 0: we found a disabled rule, disable the learnt rule */
719 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
721 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "disabling learnt ");
722 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
724 disablerule(solv, r);
726 else if (!why && !r->w1)
728 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
730 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "re-enabling learnt ");
731 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
739 /* FIXME: bad code ahead, replace as soon as possible */
741 disableupdaterules(Solver *solv, Queue *job, int jobidx)
743 Pool *pool = solv->pool;
745 Id how, what, p, *pp;
751 installed = solv->installed;
757 how = job->elements[jobidx];
760 case SOLVER_INSTALL_SOLVABLE:
761 case SOLVER_ERASE_SOLVABLE:
762 case SOLVER_ERASE_SOLVABLE_NAME:
763 case SOLVER_ERASE_SOLVABLE_PROVIDES:
769 /* go through all enabled job rules */
770 MAPZERO(&solv->noupdate);
771 for (i = solv->jobrules; i < solv->systemrules; i++)
774 if (!r->w1) /* disabled? */
776 j = solv->ruletojob.elements[i - solv->jobrules];
780 how = job->elements[j];
781 what = job->elements[j + 1];
784 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
785 s = pool->solvables + what;
786 FOR_PROVIDES(p, pp, s->name)
788 if (pool->solvables[p].name != s->name)
790 if (pool->solvables[p].repo == installed)
791 MAPSET(&solv->noupdate, p - installed->start);
794 case SOLVER_ERASE_SOLVABLE:
795 s = pool->solvables + what;
796 if (s->repo == installed)
797 MAPSET(&solv->noupdate, what - installed->start);
799 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
800 case SOLVER_ERASE_SOLVABLE_PROVIDES:
801 FOR_PROVIDES(p, pp, what)
803 if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
805 if (pool->solvables[p].repo == installed)
806 MAPSET(&solv->noupdate, p - installed->start);
814 /* fixup update rule status */
815 if (solv->allowuninstall)
816 return; /* no update rules at all */
820 /* we just disabled job #jobidx. enable all update rules
821 * that aren't disabled by the remaining job rules */
822 how = job->elements[jobidx];
823 what = job->elements[jobidx + 1];
826 case SOLVER_INSTALL_SOLVABLE:
827 s = pool->solvables + what;
828 FOR_PROVIDES(p, pp, s->name)
830 if (pool->solvables[p].name != s->name)
832 if (pool->solvables[p].repo != installed)
834 if (MAPTST(&solv->noupdate, p - installed->start))
836 r = solv->rules + solv->systemrules + (p - installed->start);
840 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
842 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
843 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
847 case SOLVER_ERASE_SOLVABLE:
848 s = pool->solvables + what;
849 if (s->repo != installed)
851 if (MAPTST(&solv->noupdate, what - installed->start))
853 r = solv->rules + solv->systemrules + (what - installed->start);
857 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
859 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
860 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
863 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
864 case SOLVER_ERASE_SOLVABLE_PROVIDES:
865 FOR_PROVIDES(p, pp, what)
867 if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
869 if (pool->solvables[p].repo != installed)
871 if (MAPTST(&solv->noupdate, p - installed->start))
873 r = solv->rules + solv->systemrules + (p - installed->start);
877 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
879 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "@@@ re-enabling ");
880 printrule(solv, SAT_DEBUG_SOLUTIONS, r);
890 for (i = 0; i < installed->nsolvables; i++)
892 r = solv->rules + solv->systemrules + i;
893 if (r->w1 && MAPTST(&solv->noupdate, i))
894 disablerule(solv, r); /* was enabled, need to disable */
900 * add (install) rules for solvable
901 * for unfulfilled requirements, conflicts, obsoletes,....
902 * add a negative assertion for solvables that are not installable
906 addrpmrulesforsolvable(Solver *solv, Solvable *s, Map *m)
908 Pool *pool = solv->pool;
909 Repo *installed = solv->installed;
923 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable -----\n");
925 queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
926 queue_push(&q, s - pool->solvables); /* push solvable Id */
932 * s: Pointer to solvable
936 if (MAPTST(m, n)) /* continue if already done */
940 s = pool->solvables + n; /* s = Solvable in question */
943 if (installed /* Installed system available */
944 && !solv->fixsystem /* NOT repair errors in rpm dependency graph */
945 && s->repo == installed) /* solvable is installed? */
947 dontfix = 1; /* dont care about broken rpm deps */
950 if (!dontfix && s->arch != ARCH_SRC && s->arch != ARCH_NOSRC && !pool_installable(pool, s))
952 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable\n", solvable2str(pool, s), (Id)(s - pool->solvables));
953 addrule(solv, -n, 0); /* uninstallable */
956 /*-----------------------------------------
957 * check requires of s
962 reqp = s->repo->idarraydata + s->requires;
963 while ((req = *reqp++) != 0) /* go throw all requires */
965 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
968 dp = pool_whatprovides(pool, req);
970 if (*dp == SYSTEMSOLVABLE) /* always installed */
975 /* the strategy here is to not insist on dependencies
976 * that are already broken. so if we find one provider
977 * that was already installed, we know that the
978 * dependency was not broken before so we enforce it */
979 for (i = 0; (p = dp[i]) != 0; i++) /* for all providers */
981 if (pool->solvables[p].repo == installed)
982 break; /* provider was installed */
984 if (!p) /* previously broken dependency */
986 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "ignoring broken requires %s of installed package %s\n", dep2str(pool, req), solvable2str(pool, s));
993 /* nothing provides req! */
994 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, "package %s [%d] is not installable (%s)\n", solvable2str(pool, s), (Id)(s - pool->solvables), dep2str(pool, req));
995 addrule(solv, -n, 0); /* mark requestor as uninstallable */
999 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
1001 POOL_DEBUG(SAT_DEBUG_RULE_CREATION," %s requires %s\n", solvable2str(pool, s), dep2str(pool, req));
1002 for (i = 0; dp[i]; i++)
1003 POOL_DEBUG(SAT_DEBUG_RULE_CREATION, " provided by %s\n", solvable2str(pool, pool->solvables + dp[i]));
1006 /* add 'requires' dependency */
1007 /* rule: (-requestor|provider1|provider2|...|providerN) */
1008 addrule(solv, -n, dp - pool->whatprovidesdata);
1010 /* descend the dependency tree */
1011 for (; *dp; dp++) /* loop through all providers */
1013 if (!MAPTST(m, *dp))
1014 queue_push(&q, *dp);
1017 } /* while, requirements of n */
1019 } /* if, requirements */
1021 /* that's all we check for src packages */
1022 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
1025 /*-----------------------------------------
1026 * check conflicts of s
1031 conp = s->repo->idarraydata + s->conflicts;
1032 while ((con = *conp++) != 0)
1034 FOR_PROVIDES(p, pp, con)
1036 /* dontfix: dont care about conflicts with already installed packs */
1037 if (dontfix && pool->solvables[p].repo == installed)
1039 /* rule: -n|-p: either solvable _or_ provider of conflict */
1040 addrule(solv, -n, -p);
1045 /*-----------------------------------------
1046 * check obsoletes if not installed
1048 if (!installed || pool->solvables[n].repo != installed)
1049 { /* not installed */
1052 obsp = s->repo->idarraydata + s->obsoletes;
1053 while ((obs = *obsp++) != 0)
1055 FOR_PROVIDES(p, pp, obs)
1056 addrule(solv, -n, -p);
1059 FOR_PROVIDES(p, pp, s->name)
1061 if (s->name == pool->solvables[p].name)
1062 addrule(solv, -n, -p);
1066 /*-----------------------------------------
1067 * add recommends to the rule list
1071 recp = s->repo->idarraydata + s->recommends;
1072 while ((rec = *recp++) != 0)
1074 FOR_PROVIDES(p, pp, rec)
1081 sugp = s->repo->idarraydata + s->suggests;
1082 while ((sug = *sugp++) != 0)
1084 FOR_PROVIDES(p, pp, sug)
1091 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforsolvable end -----\n");
1095 addrpmrulesforweak(Solver *solv, Map *m)
1097 Pool *pool = solv->pool;
1102 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak -----\n");
1103 for (i = n = 1; n < pool->nsolvables; i++, n++)
1105 if (i == pool->nsolvables)
1109 s = pool->solvables + i;
1110 if (!pool_installable(pool, s))
1115 supp = s->repo->idarraydata + s->supplements;
1116 while ((sup = *supp++) != ID_NULL)
1117 if (dep_possible(solv, sup, m))
1120 if (!sup && s->freshens)
1122 supp = s->repo->idarraydata + s->freshens;
1123 while ((sup = *supp++) != ID_NULL)
1124 if (dep_possible(solv, sup, m))
1127 if (!sup && s->enhances)
1129 supp = s->repo->idarraydata + s->enhances;
1130 while ((sup = *supp++) != ID_NULL)
1131 if (dep_possible(solv, sup, m))
1136 addrpmrulesforsolvable(solv, s, m);
1139 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforweak end -----\n");
1143 addrpmrulesforupdaters(Solver *solv, Solvable *s, Map *m, int allowall)
1145 Pool *pool = solv->pool;
1150 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
1152 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1153 policy_findupdatepackages(solv, s, &qs, allowall);
1154 if (!MAPTST(m, s - pool->solvables)) /* add rule for s if not already done */
1155 addrpmrulesforsolvable(solv, s, m);
1156 for (i = 0; i < qs.count; i++)
1157 if (!MAPTST(m, qs.elements[i]))
1158 addrpmrulesforsolvable(solv, pool->solvables + qs.elements[i], m);
1161 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addrpmrulesforupdaters -----\n");
1165 * add rule for update
1166 * (A|A1|A2|A3...) An = update candidates for A
1168 * s = (installed) solvable
1172 addupdaterule(Solver *solv, Solvable *s, int allowall)
1174 /* installed packages get a special upgrade allowed rule */
1175 Pool *pool = solv->pool;
1180 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule -----\n");
1182 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1183 policy_findupdatepackages(solv, s, &qs, allowall);
1184 if (qs.count == 0) /* no updaters found */
1187 d = pool_queuetowhatprovides(pool, &qs); /* intern computed queue */
1189 addrule(solv, s - pool->solvables, d); /* allow update of s */
1190 POOL_DEBUG(SAT_DEBUG_SCHUBI, "----- addupdaterule end -----\n");
1194 /*-----------------------------------------------------------------*/
1201 * initial setup for all watches
1205 makewatches(Solver *solv)
1209 int nsolvables = solv->pool->nsolvables;
1211 xfree(solv->watches);
1212 /* lower half for removals, upper half for installs */
1213 solv->watches = (Id *)xcalloc(2 * nsolvables, sizeof(Id));
1215 /* do it reverse so rpm rules get triggered first */
1216 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
1218 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
1221 if (!r->w1 /* rule is disabled */
1222 || !r->w2) /* rule is assertion */
1225 /* see addwatches(solv, r) */
1226 r->n1 = solv->watches[nsolvables + r->w1];
1227 solv->watches[nsolvables + r->w1] = r - solv->rules;
1229 r->n2 = solv->watches[nsolvables + r->w2];
1230 solv->watches[nsolvables + r->w2] = r - solv->rules;
1236 * add watches (for rule)
1240 addwatches(Solver *solv, Rule *r)
1242 int nsolvables = solv->pool->nsolvables;
1244 r->n1 = solv->watches[nsolvables + r->w1];
1245 solv->watches[nsolvables + r->w1] = r - solv->rules;
1247 r->n2 = solv->watches[nsolvables + r->w2];
1248 solv->watches[nsolvables + r->w2] = r - solv->rules;
1252 /*-----------------------------------------------------------------*/
1253 /* rule propagation */
1255 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
1260 * propagate decision to all rules
1264 propagate(Solver *solv, int level)
1266 Pool *pool = solv->pool;
1271 Id *decisionmap = solv->decisionmap;
1272 Id *watches = solv->watches + pool->nsolvables;
1274 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate -----\n");
1276 while (solv->propagate_index < solv->decisionq.count)
1278 /* negate because our watches trigger if literal goes FALSE */
1279 pkg = -solv->decisionq.elements[solv->propagate_index++];
1280 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1282 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "popagate for decision %d level %d\n", -pkg, level);
1283 printruleelement(solv, SAT_DEBUG_PROPAGATE, 0, -pkg);
1286 for (rp = watches + pkg; *rp; rp = nrp)
1288 r = solv->rules + *rp;
1290 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1292 POOL_DEBUG(SAT_DEBUG_PROPAGATE," watch triggered ");
1293 printrule(solv, SAT_DEBUG_PROPAGATE, r);
1306 /* if clause is TRUE, nothing to do */
1307 if (DECISIONMAP_TRUE(ow))
1312 /* not a binary clause, check if we need to move our watch */
1313 /* search for a literal that is not ow and not false */
1314 /* (true is also ok, in that case the rule is fulfilled) */
1315 if (r->p && r->p != ow && !DECISIONMAP_TRUE(-r->p))
1318 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
1319 if (p != ow && !DECISIONMAP_TRUE(-p))
1323 /* p is free to watch, move watch to p */
1324 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1327 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables + p));
1329 POOL_DEBUG(SAT_DEBUG_PROPAGATE," -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), solvable2str(pool, pool->solvables - p));
1343 watches[p] = r - solv->rules;
1347 /* unit clause found, set other watch to TRUE */
1348 if (DECISIONMAP_TRUE(-ow))
1349 return r; /* eek, a conflict! */
1350 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1352 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "unit ");
1353 printrule(solv, SAT_DEBUG_PROPAGATE, r);
1356 decisionmap[ow] = level;
1358 decisionmap[-ow] = -level;
1359 queue_push(&solv->decisionq, ow);
1360 queue_push(&solv->decisionq_why, r - solv->rules);
1361 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
1363 Solvable *s = pool->solvables + (ow > 0 ? ow : -ow);
1365 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to install %s\n", solvable2str(pool, s));
1367 POOL_DEBUG(SAT_DEBUG_PROPAGATE, " -> decided to conflict %s\n", solvable2str(pool, s));
1371 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "----- propagate end-----\n");
1373 return 0; /* all is well */
1377 /*-----------------------------------------------------------------*/
1386 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *why)
1388 Pool *pool = solv->pool;
1391 Map seen; /* global? */
1395 int learnt_why = solv->learnt_pool.count;
1396 Id *decisionmap = solv->decisionmap;
1400 POOL_DEBUG(SAT_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
1401 map_init(&seen, pool->nsolvables);
1402 idx = solv->decisionq.count;
1405 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1406 printruleclass(solv, SAT_DEBUG_ANALYZE, c);
1407 queue_push(&solv->learnt_pool, c - solv->rules);
1408 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1419 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1421 vv = v > 0 ? v : -v;
1422 if (MAPTST(&seen, vv))
1424 l = solv->decisionmap[vv];
1431 for (j = 0; j < solv->decisionq.count; j++)
1432 if (solv->decisionq.elements[j] == v)
1434 if (j == solv->decisionq.count)
1436 queue_push(&rulq, -(j + 1));
1438 continue; /* initial setting */
1442 num++; /* need to do this one as well */
1454 v = solv->decisionq.elements[--idx];
1455 vv = v > 0 ? v : -v;
1456 if (MAPTST(&seen, vv))
1459 c = solv->rules + solv->decisionq_why.elements[idx];
1467 else if (r.count == 1 && r.elements[0] < 0)
1468 *dr = r.elements[0];
1470 *dr = pool_queuetowhatprovides(pool, &r);
1471 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1473 POOL_DEBUG(SAT_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
1474 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, -v);
1475 for (i = 0; i < r.count; i++)
1476 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, r.elements[i]);
1479 /* push end marker on learnt reasons stack */
1480 queue_push(&solv->learnt_pool, 0);
1481 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1483 for (i = learnt_why; solv->learnt_pool.elements[i]; i++)
1485 POOL_DEBUG(SAT_DEBUG_ANALYZE, "learnt_why ");
1486 printrule(solv, SAT_DEBUG_ANALYZE, solv->rules + solv->learnt_pool.elements[i]);
1497 * reset the solver decisions to right after the rpm rules.
1498 * called after rules have been enabled/disabled
1502 reset_solver(Solver *solv)
1504 Pool *pool = solv->pool;
1508 enabledisablelearntrules(solv);
1510 /* redo all direct rpm rule decisions */
1511 /* we break at the first decision with a why attached, this is
1512 * either a job/system rule assertion or a propagated decision */
1513 for (i = 0; i < solv->decisionq.count; i++)
1515 v = solv->decisionq.elements[i];
1516 solv->decisionmap[v > 0 ? v : -v] = 0;
1518 for (i = 0; i < solv->decisionq_why.count; i++)
1519 if (solv->decisionq_why.elements[i])
1523 v = solv->decisionq.elements[i];
1524 solv->decisionmap[v > 0 ? v : -v] = v > 0 ? 1 : -1;
1527 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions done reduced from %d to %d\n", solv->decisionq.count, i);
1529 solv->decisionq_why.count = i;
1530 solv->decisionq.count = i;
1531 solv->recommends_index = -1;
1532 solv->propagate_index = 0;
1534 /* redo all job/system decisions */
1535 makeruledecisions(solv);
1536 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
1537 /* recreate watch chains */
1543 * analyze_unsolvable_rule
1547 analyze_unsolvable_rule(Solver *solv, Rule *r)
1549 Pool *pool = solv->pool;
1551 Id why = r - solv->rules;
1552 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
1553 printruleclass(solv, SAT_DEBUG_UNSOLVABLE, r);
1554 if (solv->learntrules && why >= solv->learntrules)
1556 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1557 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i]);
1560 /* do not add rpm rules to problem */
1561 if (why < solv->jobrules)
1563 /* turn rule into problem */
1564 if (why >= solv->jobrules && why < solv->systemrules)
1565 why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
1566 /* return if problem already countains our rule */
1567 if (solv->problems.count)
1569 for (i = solv->problems.count - 1; i >= 0; i--)
1570 if (solv->problems.elements[i] == 0) /* end of last problem reached? */
1572 else if (solv->problems.elements[i] == why)
1575 queue_push(&solv->problems, why);
1580 * analyze_unsolvable
1582 * return: 1 - disabled some rules, try again
1587 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
1589 Pool *pool = solv->pool;
1591 Map seen; /* global to speed things up? */
1594 Id *decisionmap = solv->decisionmap;
1595 int oldproblemcount;
1596 int oldlearntpoolcount;
1599 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
1600 oldproblemcount = solv->problems.count;
1601 oldlearntpoolcount = solv->learnt_pool.count;
1603 /* make room for proof index */
1604 /* must update it later, as analyze_unsolvable_rule would confuse
1605 * it with a rule index if we put the real value in already */
1606 queue_push(&solv->problems, 0);
1609 map_init(&seen, pool->nsolvables);
1610 queue_push(&solv->learnt_pool, r - solv->rules);
1611 analyze_unsolvable_rule(solv, r);
1612 dp = r->d ? pool->whatprovidesdata + r->d : 0;
1623 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1625 vv = v > 0 ? v : -v;
1626 l = solv->decisionmap[vv];
1631 idx = solv->decisionq.count;
1634 v = solv->decisionq.elements[--idx];
1635 vv = v > 0 ? v : -v;
1636 if (!MAPTST(&seen, vv))
1638 why = solv->decisionq_why.elements[idx];
1641 /* level 1 and no why, must be an rpm assertion */
1642 IF_POOLDEBUG (SAT_DEBUG_UNSOLVABLE)
1644 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "RPM ");
1645 printruleelement(solv, SAT_DEBUG_UNSOLVABLE, 0, v);
1647 /* this is the only positive rpm assertion */
1648 if (v == SYSTEMSOLVABLE)
1652 queue_push(&solv->learnt_pool, v);
1655 r = solv->rules + why;
1656 queue_push(&solv->learnt_pool, why);
1657 analyze_unsolvable_rule(solv, r);
1658 dp = r->d ? pool->whatprovidesdata + r->d : 0;
1669 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1671 vv = v > 0 ? v : -v;
1672 l = solv->decisionmap[vv];
1679 queue_push(&solv->problems, 0); /* mark end of this problem */
1682 if (solv->weakrules != solv->learntrules)
1684 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1686 why = solv->problems.elements[i];
1687 if (why < solv->weakrules || why >= solv->learntrules)
1689 if (!lastweak || lastweak < why)
1695 /* disable last weak rule */
1696 solv->problems.count = oldproblemcount;
1697 solv->learnt_pool.count = oldlearntpoolcount;
1698 r = solv->rules + lastweak;
1699 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "disabling weak ");
1700 printrule(solv, SAT_DEBUG_UNSOLVABLE, r);
1701 disablerule(solv, r);
1707 queue_push(&solv->learnt_pool, 0);
1708 solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
1712 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1713 disableproblem(solv, solv->problems.elements[i]);
1717 POOL_DEBUG(SAT_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
1722 /*-----------------------------------------------------------------*/
1723 /* Decision revert */
1727 * revert decision at level
1731 revert(Solver *solv, int level)
1733 Pool *pool = solv->pool;
1735 while (solv->decisionq.count)
1737 v = solv->decisionq.elements[solv->decisionq.count - 1];
1738 vv = v > 0 ? v : -v;
1739 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1741 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1742 solv->decisionmap[vv] = 0;
1743 solv->decisionq.count--;
1744 solv->decisionq_why.count--;
1745 solv->propagate_index = solv->decisionq.count;
1747 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
1749 solv->branches.count--;
1750 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
1751 solv->branches.count--;
1753 solv->recommends_index = -1;
1758 * watch2onhighest - put watch2 on literal with highest level
1762 watch2onhighest(Solver *solv, Rule *r)
1768 return; /* binary rule, both watches are set */
1769 dp = solv->pool->whatprovidesdata + r->d;
1770 while ((v = *dp++) != 0)
1772 l = solv->decisionmap[v < 0 ? -v : v];
1787 * add free decision to decision q, increase level
1788 * propagate decision, return if no conflict.
1789 * in conflict case, analyze conflict rule, add resulting
1790 * rule to learnt rule set, make decision from learnt
1791 * rule (always unit) and re-propagate.
1795 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules)
1797 Pool *pool = solv->pool;
1806 solv->decisionmap[decision] = level;
1808 solv->decisionmap[-decision] = -level;
1809 queue_push(&solv->decisionq, decision);
1810 queue_push(&solv->decisionq_why, 0);
1814 r = propagate(solv, level);
1818 return analyze_unsolvable(solv, r, disablerules);
1819 POOL_DEBUG(SAT_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
1820 l = analyze(solv, level, r, &p, &d, &why); /* learnt rule in p and d */
1821 if (l >= level || l <= 0)
1823 POOL_DEBUG(SAT_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
1825 revert(solv, level);
1826 r = addrule(solv, p, d); /* p requires d */
1829 if (solv->learnt_why.count != (r - solv->rules) - solv->learntrules)
1831 queue_push(&solv->learnt_why, why);
1834 /* at least 2 literals, needs watches */
1835 watch2onhighest(solv, r);
1836 addwatches(solv, r);
1838 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1839 queue_push(&solv->decisionq, p);
1840 queue_push(&solv->decisionq_why, r - solv->rules);
1841 IF_POOLDEBUG (SAT_DEBUG_ANALYZE)
1843 POOL_DEBUG(SAT_DEBUG_ANALYZE, "decision: ");
1844 printruleelement(solv, SAT_DEBUG_ANALYZE, 0, p);
1845 POOL_DEBUG(SAT_DEBUG_ANALYZE, "new rule: ");
1846 printrule(solv, SAT_DEBUG_ANALYZE, r);
1854 * install best package from the queue. We add an extra package, inst, if
1855 * provided. See comment in weak install section.
1858 selectandinstall(Solver *solv, int level, Queue *dq, Id inst, int disablerules)
1860 Pool *pool = solv->pool;
1864 if (dq->count > 1 || inst)
1865 policy_filter_unwanted(solv, dq, inst, POLICY_MODE_CHOOSE);
1870 /* choose the supplemented one */
1871 for (i = 0; i < dq->count; i++)
1872 if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
1876 for (i = 1; i < dq->count; i++)
1877 queue_push(&solv->branches, dq->elements[i]);
1878 queue_push(&solv->branches, -level);
1882 p = dq->elements[i];
1884 POOL_DEBUG(SAT_DEBUG_POLICY, "installing %s\n", solvable2str(pool, pool->solvables + p));
1886 return setpropagatelearn(solv, level, p, disablerules);
1890 /*-----------------------------------------------------------------*/
1891 /* Main solver interface */
1896 * create solver structure
1898 * pool: all available solvables
1899 * installed: installed Solvables
1902 * Upon solving, rules are created to flag the Solvables
1903 * of the 'installed' Repo as installed.
1907 solver_create(Pool *pool, Repo *installed)
1910 solv = (Solver *)xcalloc(1, sizeof(Solver));
1912 solv->installed = installed;
1914 queue_init(&solv->ruletojob);
1915 queue_init(&solv->decisionq);
1916 queue_init(&solv->decisionq_why);
1917 queue_init(&solv->problems);
1918 queue_init(&solv->suggestions);
1919 queue_init(&solv->learnt_why);
1920 queue_init(&solv->learnt_pool);
1921 queue_init(&solv->branches);
1923 map_init(&solv->recommendsmap, pool->nsolvables);
1924 map_init(&solv->suggestsmap, pool->nsolvables);
1925 map_init(&solv->noupdate, installed ? installed->end - installed->start : 0);
1926 solv->recommends_index = 0;
1928 solv->decisionmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
1929 solv->rules = (Rule *)xmalloc((solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
1930 memset(solv->rules, 0, sizeof(Rule));
1942 solver_free(Solver *solv)
1944 queue_free(&solv->ruletojob);
1945 queue_free(&solv->decisionq);
1946 queue_free(&solv->decisionq_why);
1947 queue_free(&solv->learnt_why);
1948 queue_free(&solv->learnt_pool);
1949 queue_free(&solv->problems);
1950 queue_free(&solv->suggestions);
1951 queue_free(&solv->branches);
1953 map_free(&solv->recommendsmap);
1954 map_free(&solv->suggestsmap);
1955 map_free(&solv->noupdate);
1956 xfree(solv->decisionmap);
1958 xfree(solv->watches);
1959 xfree(solv->weaksystemrules);
1960 xfree(solv->obsoletes);
1961 xfree(solv->obsoletes_data);
1966 /*-------------------------------------------------------*/
1971 * all rules have been set up, now actually run the solver
1976 run_solver(Solver *solv, int disablerules, int doweak)
1984 Pool *pool = solv->pool;
1987 IF_POOLDEBUG (SAT_DEBUG_RULE_CREATION)
1989 POOL_DEBUG (SAT_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
1990 for (i = 0; i < solv->nrules; i++)
1991 printrule(solv, SAT_DEBUG_RULE_CREATION, solv->rules + i);
1994 /* create watches chains */
1997 POOL_DEBUG(SAT_DEBUG_STATS, "initial decisions: %d\n", solv->decisionq.count);
1999 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2000 printdecisions(solv);
2002 /* start SAT algorithm */
2004 systemlevel = level + 1;
2005 POOL_DEBUG(SAT_DEBUG_STATS, "solving...\n");
2016 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
2017 if ((r = propagate(solv, level)) != 0)
2019 if (analyze_unsolvable(solv, r, disablerules))
2027 * installed packages
2030 if (level < systemlevel && solv->installed && solv->installed->nsolvables)
2032 if (!solv->updatesystem)
2034 /* try to keep as many packages as possible */
2035 POOL_DEBUG(SAT_DEBUG_STATS, "installing system packages\n");
2036 for (i = solv->installed->start, n = 0; ; i++)
2038 if (n == solv->installed->nsolvables)
2040 if (i == solv->installed->end)
2041 i = solv->installed->start;
2042 s = pool->solvables + i;
2043 if (s->repo != solv->installed)
2046 if (solv->decisionmap[i] != 0)
2048 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "keeping %s\n", solvable2str(pool, s));
2050 level = setpropagatelearn(solv, level, i, disablerules);
2056 if (level <= olevel)
2060 if (solv->weaksystemrules)
2062 POOL_DEBUG(SAT_DEBUG_STATS, "installing weak system packages\n");
2063 for (i = solv->installed->start; i < solv->installed->end; i++)
2065 if (pool->solvables[i].repo != solv->installed)
2067 if (solv->decisionmap[i] > 0 || (solv->decisionmap[i] < 0 && solv->weaksystemrules[i - solv->installed->start] == 0))
2069 /* noupdate is set if a job is erasing the installed solvable or installing a specific version */
2070 if (MAPTST(&solv->noupdate, i - solv->installed->start))
2073 if (solv->weaksystemrules[i - solv->installed->start])
2075 dp = pool->whatprovidesdata + solv->weaksystemrules[i - solv->installed->start];
2076 while ((p = *dp++) != 0)
2078 if (solv->decisionmap[p] > 0)
2080 if (solv->decisionmap[p] == 0)
2084 continue; /* update package already installed */
2086 if (!dq.count && solv->decisionmap[i] != 0)
2089 /* FIXME: i is handled a bit different because we do not want
2090 * to have it pruned just because it is not recommened.
2091 * we should not prune installed packages instead */
2092 level = selectandinstall(solv, level, &dq, (solv->decisionmap[i] ? 0 : i), disablerules);
2098 if (level <= olevel)
2101 if (i < solv->installed->end)
2104 systemlevel = level;
2111 POOL_DEBUG(SAT_DEBUG_STATS, "deciding unresolved rules\n");
2112 for (i = 1, n = 1; ; i++, n++)
2114 if (n == solv->nrules)
2116 if (i == solv->nrules)
2118 r = solv->rules + i;
2124 /* binary or unary rule */
2125 /* need two positive undecided literals */
2126 if (r->p < 0 || r->w2 <= 0)
2128 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
2130 queue_push(&dq, r->p);
2131 queue_push(&dq, r->w2);
2136 * all negative literals are installed
2137 * no positive literal is installed
2138 * i.e. the rule is not fulfilled and we
2139 * just need to decide on the positive literals
2143 if (solv->decisionmap[-r->p] <= 0)
2148 if (solv->decisionmap[r->p] > 0)
2150 if (solv->decisionmap[r->p] == 0)
2151 queue_push(&dq, r->p);
2153 dp = pool->whatprovidesdata + r->d;
2154 while ((p = *dp++) != 0)
2158 if (solv->decisionmap[-p] <= 0)
2163 if (solv->decisionmap[p] > 0)
2165 if (solv->decisionmap[p] == 0)
2174 /* cannot happen as this means that
2175 * the rule is unit */
2176 printrule(solv, SAT_FATAL, r);
2179 IF_POOLDEBUG (SAT_DEBUG_PROPAGATE)
2181 POOL_DEBUG(SAT_DEBUG_PROPAGATE, "unfulfilled ");
2182 printrule(solv, SAT_DEBUG_PROPAGATE, r);
2186 level = selectandinstall(solv, level, &dq, 0, disablerules);
2192 if (level < systemlevel)
2195 } /* for(), decide */
2197 if (n != solv->nrules) /* continue if level < systemlevel */
2200 if (doweak && !solv->problems.count)
2204 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended packages\n");
2205 if (!REGARD_RECOMMENDS_OF_INSTALLED_ITEMS)
2207 for (i = 0; i < solv->decisionq.count; i++)
2209 p = solv->decisionq.elements[i];
2210 if (p > 0 && pool->solvables[p].repo == solv->installed)
2211 solv->decisionmap[p] = -solv->decisionmap[p];
2215 for (i = 1; i < pool->nsolvables; i++)
2217 if (solv->decisionmap[i] < 0)
2219 if (solv->decisionmap[i] > 0)
2221 Id *recp, rec, *pp, p;
2222 s = pool->solvables + i;
2223 /* installed, check for recommends */
2224 /* XXX need to special case AND ? */
2227 recp = s->repo->idarraydata + s->recommends;
2228 while ((rec = *recp++) != 0)
2231 FOR_PROVIDES(p, pp, rec)
2233 if (solv->decisionmap[p] > 0)
2238 else if (solv->decisionmap[p] == 0)
2240 queue_pushunique(&dq, p);
2248 s = pool->solvables + i;
2249 if (!s->supplements && !s->freshens)
2251 if (!pool_installable(pool, s))
2253 if (solver_is_supplementing(solv, s))
2254 queue_pushunique(&dq, i);
2257 if (!REGARD_RECOMMENDS_OF_INSTALLED_ITEMS)
2259 for (i = 0; i < solv->decisionq.count; i++)
2261 p = solv->decisionq.elements[i];
2262 if (p > 0 && pool->solvables[p].repo == solv->installed)
2263 solv->decisionmap[p] = -solv->decisionmap[p];
2269 policy_filter_unwanted(solv, &dq, 0, POLICY_MODE_RECOMMEND);
2271 POOL_DEBUG(SAT_DEBUG_STATS, "installing recommended %s\n", solvable2str(pool, pool->solvables + p));
2272 level = setpropagatelearn(solv, level, p, 0);
2277 if (solv->solution_callback)
2279 solv->solution_callback(solv, solv->solution_callback_data);
2280 if (solv->branches.count)
2282 int i = solv->branches.count - 1;
2283 int l = -solv->branches.elements[i];
2285 if (solv->branches.elements[i - 1] < 0)
2287 p = solv->branches.elements[i];
2288 POOL_DEBUG(SAT_DEBUG_STATS, "branching with %s\n", solvable2str(pool, pool->solvables + p));
2290 for (j = i + 1; j < solv->branches.count; j++)
2291 queue_push(&dq, solv->branches.elements[j]);
2292 solv->branches.count = i;
2294 revert(solv, level);
2296 for (j = 0; j < dq.count; j++)
2297 queue_push(&solv->branches, dq.elements[j]);
2299 level = setpropagatelearn(solv, level, p, disablerules);
2307 /* all branches done, we're finally finished */
2311 /* minimization step */
2312 if (solv->branches.count)
2314 int l = 0, lasti = -1, lastl = -1;
2316 for (i = solv->branches.count - 1; i >= 0; i--)
2318 p = solv->branches.elements[i];
2321 else if (p > 0 && solv->decisionmap[p] > l + 1)
2329 /* kill old solvable so that we do not loop */
2330 p = solv->branches.elements[lasti];
2331 solv->branches.elements[lasti] = 0;
2332 POOL_DEBUG(SAT_DEBUG_STATS, "minimizing %d -> %d with %s\n", solv->decisionmap[p], l, solvable2str(pool, pool->solvables + p));
2335 revert(solv, level);
2337 level = setpropagatelearn(solv, level, p, disablerules);
2354 * at this point, all rules that led to conflicts are disabled.
2355 * we re-enable all rules of a problem set but rule "sug", then
2356 * continue to disable more rules until there as again a solution.
2359 /* FIXME: think about conflicting assertions */
2362 refine_suggestion(Solver *solv, Queue *job, Id *problem, Id sug, Queue *refined)
2364 Pool *pool = solv->pool;
2371 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
2373 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion start\n");
2374 for (i = 0; problem[i]; i++)
2376 if (problem[i] == sug)
2377 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "=> ");
2378 printproblem(solv, problem[i]);
2381 queue_init(&disabled);
2382 queue_empty(refined);
2383 queue_push(refined, sug);
2385 /* re-enable all problem rules with the exception of "sug" */
2389 for (i = 0; problem[i]; i++)
2390 if (problem[i] != sug)
2391 enableproblem(solv, problem[i]);
2394 disableupdaterules(solv, job, -(sug + 1));
2398 /* re-enable as many weak rules as possible */
2399 for (i = solv->weakrules; i < solv->learntrules; i++)
2401 r = solv->rules + i;
2403 enablerule(solv, r);
2406 queue_empty(&solv->problems);
2407 revert(solv, 1); /* XXX move to reset_solver? */
2410 run_solver(solv, 0, 0);
2411 if (!solv->problems.count)
2413 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no more problems!\n");
2414 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
2415 printdecisions(solv);
2416 break; /* great, no more problems */
2418 disabledcnt = disabled.count;
2419 /* skip over proof index */
2420 for (i = 1; i < solv->problems.count - 1; i++)
2422 /* ignore solutions in refined */
2423 v = solv->problems.elements[i];
2425 break; /* end of problem reached */
2426 for (j = 0; problem[j]; j++)
2427 if (problem[j] != sug && problem[j] == v)
2431 queue_push(&disabled, v);
2433 if (disabled.count == disabledcnt)
2435 /* no solution found, this was an invalid suggestion! */
2436 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "no solution found!\n");
2440 if (disabled.count == disabledcnt + 1)
2442 /* just one suggestion, add it to refined list */
2443 v = disabled.elements[disabledcnt];
2444 queue_push(refined, v);
2445 disableproblem(solv, v);
2447 disableupdaterules(solv, job, -(v + 1));
2451 /* more than one solution, disable all */
2452 /* do not push anything on refine list */
2453 IF_POOLDEBUG (SAT_DEBUG_SOLUTIONS)
2455 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "more than one solution found:\n");
2456 for (i = disabledcnt; i < disabled.count; i++)
2457 printproblem(solv, disabled.elements[i]);
2459 for (i = disabledcnt; i < disabled.count; i++)
2460 disableproblem(solv, disabled.elements[i]);
2463 /* all done, get us back into the same state as before */
2464 /* enable refined rules again */
2465 for (i = 0; i < disabled.count; i++)
2466 enableproblem(solv, disabled.elements[i]);
2467 /* disable problem rules again */
2468 for (i = 0; problem[i]; i++)
2469 disableproblem(solv, problem[i]);
2470 disableupdaterules(solv, job, -1);
2471 POOL_DEBUG(SAT_DEBUG_SOLUTIONS, "refine_suggestion end\n");
2475 problems_to_solutions(Solver *solv, Queue *job)
2477 Pool *pool = solv->pool;
2485 if (!solv->problems.count)
2487 queue_clone(&problems, &solv->problems);
2488 queue_init(&solution);
2489 queue_init(&solutions);
2490 /* copy over proof index */
2491 queue_push(&solutions, problems.elements[0]);
2492 problem = problems.elements + 1;
2493 for (i = 1; i < problems.count; i++)
2495 Id v = problems.elements[i];
2498 /* mark end of this problem */
2499 queue_push(&solutions, 0);
2500 queue_push(&solutions, 0);
2501 if (i + 1 == problems.count)
2503 /* copy over proof of next problem */
2504 queue_push(&solutions, problems.elements[i + 1]);
2506 problem = problems.elements + i + 1;
2509 refine_suggestion(solv, job, problem, v, &solution);
2510 if (!solution.count)
2511 continue; /* this solution didn't work out */
2513 for (j = 0; j < solution.count; j++)
2515 why = solution.elements[j];
2517 printproblem(solv, why);
2521 queue_push(&solutions, 0);
2522 queue_push(&solutions, -why);
2524 else if (why >= solv->systemrules && why < solv->weakrules)
2527 p = solv->installed->start + (why - solv->systemrules);
2528 if (solv->weaksystemrules && solv->weaksystemrules[why - solv->systemrules])
2530 Id *dp = pool->whatprovidesdata + solv->weaksystemrules[why - solv->systemrules];
2533 if (*dp >= solv->installed->start && *dp < solv->installed->start + solv->installed->nsolvables)
2535 if (solv->decisionmap[*dp] > 0)
2542 queue_push(&solutions, p);
2543 queue_push(&solutions, rp);
2548 /* mark end of this solution */
2549 queue_push(&solutions, 0);
2550 queue_push(&solutions, 0);
2552 queue_free(&solution);
2553 queue_free(&problems);
2554 /* copy queue over to solutions */
2555 queue_free(&solv->problems);
2556 queue_clone(&solv->problems, &solutions);
2558 /* bring solver back into problem state */
2559 revert(solv, 1); /* XXX move to reset_solver? */
2562 if (solv->problems.count != solutions.count)
2564 queue_free(&solutions);
2568 solver_next_problem(Solver *solv, Id problem)
2572 return solv->problems.count ? 1 : 0;
2573 pp = solv->problems.elements + problem;
2574 while (pp[0] || pp[1])
2578 while (pp[0] || pp[1])
2583 problem = pp - solv->problems.elements;
2584 if (problem >= solv->problems.count)
2590 solver_next_solution(Solver *solv, Id problem, Id solution)
2596 pp = solv->problems.elements + solution;
2597 return pp[0] || pp[1] ? solution : 0;
2599 pp = solv->problems.elements + solution;
2600 while (pp[0] || pp[1])
2603 solution = pp - solv->problems.elements;
2604 return pp[0] || pp[1] ? solution : 0;
2608 solver_next_solutionelement(Solver *solv, Id problem, Id solution, Id element, Id *p, Id *rp)
2611 element = element ? element + 2 : solution;
2612 pp = solv->problems.elements + element;
2613 if (!(pp[0] || pp[1]))
2628 printdecisions(Solver *solv)
2630 Pool *pool = solv->pool;
2631 Repo *installed = solv->installed;
2632 Id p, *obsoletesmap;
2636 obsoletesmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
2639 for (i = 0; i < solv->decisionq.count; i++)
2643 n = solv->decisionq.elements[i];
2646 if (n == SYSTEMSOLVABLE)
2648 s = pool->solvables + n;
2649 if (s->repo == installed) /* obsoletes don't count for already installed packages */
2651 FOR_PROVIDES(p, pp, s->name)
2652 if (s->name == pool->solvables[p].name)
2654 if (pool->solvables[p].repo == installed && !obsoletesmap[p])
2656 obsoletesmap[p] = n;
2661 for (i = 0; i < solv->decisionq.count; i++)
2666 n = solv->decisionq.elements[i];
2669 if (n == SYSTEMSOLVABLE)
2671 s = pool->solvables + n;
2672 if (s->repo == installed) /* obsoletes don't count for already installed packages */
2676 obsp = s->repo->idarraydata + s->obsoletes;
2677 while ((obs = *obsp++) != 0)
2678 FOR_PROVIDES(p, pp, obs)
2680 if (pool->solvables[p].repo == installed && !obsoletesmap[p])
2682 obsoletesmap[p] = n;
2689 /* print solvables to be erased */
2693 FOR_REPO_SOLVABLES(installed, p, s)
2695 if (solv->decisionmap[p] >= 0)
2697 if (obsoletesmap[p])
2699 POOL_DEBUG(SAT_DEBUG_RESULT, "erase %s\n", solvable2str(pool, s));
2703 /* print solvables to be installed */
2705 for (i = 0; i < solv->decisionq.count; i++)
2708 p = solv->decisionq.elements[i];
2711 if (p == SYSTEMSOLVABLE)
2713 s = pool->solvables + p;
2714 if (installed && s->repo == installed)
2717 if (!obsoletesmap[p])
2719 POOL_DEBUG(SAT_DEBUG_RESULT, "install %s", solvable2str(pool, s));
2723 POOL_DEBUG(SAT_DEBUG_RESULT, "update %s", solvable2str(pool, s));
2724 POOL_DEBUG(SAT_DEBUG_RESULT, " (obsoletes");
2725 for (j = installed->start; j < installed->end; j++)
2726 if (obsoletesmap[j] == p)
2727 POOL_DEBUG(SAT_DEBUG_RESULT, " %s", solvable2str(pool, pool->solvables + j));
2728 POOL_DEBUG(SAT_DEBUG_RESULT, ")");
2730 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
2733 xfree(obsoletesmap);
2735 if (solv->suggestions.count)
2737 POOL_DEBUG(SAT_DEBUG_RESULT, "\nsuggested packages:\n");
2738 for (i = 0; i < solv->suggestions.count; i++)
2740 s = pool->solvables + solv->suggestions.elements[i];
2741 POOL_DEBUG(SAT_DEBUG_RESULT, "- %s\n", solvable2str(pool, s));
2746 /* this is basically the reverse of addrpmrulesforsolvable */
2748 solver_problemruleinfo(Solver *solv, Queue *job, Id rid, Id *depp, Id *sourcep, Id *targetp)
2750 Pool *pool = solv->pool;
2751 Repo *installed = solv->installed;
2755 Id p, *pp, req, *reqp, con, *conp, obs, *obsp, *dp;
2757 if (rid >= solv->weakrules)
2759 if (rid >= solv->systemrules)
2762 *sourcep = solv->installed->start + (rid - solv->systemrules);
2764 return SOLVER_PROBLEM_UPDATE_RULE;
2766 if (rid >= solv->jobrules)
2769 r = solv->rules + rid;
2770 p = solv->ruletojob.elements[rid - solv->jobrules];
2771 *depp = job->elements[p + 1];
2773 *targetp = job->elements[p];
2774 if (r->d == 0 && r->w2 == 0 && r->p == -SYSTEMSOLVABLE)
2775 return SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP;
2776 return SOLVER_PROBLEM_JOB_RULE;
2780 /* a rpm rule assertion */
2781 if (rid == -SYSTEMSOLVABLE)
2782 abort(); /* can happen only for job rules */
2783 s = pool->solvables - rid;
2784 if (installed && !solv->fixsystem && s->repo == installed)
2786 if (dontfix) /* dontfix packages never have a neg assertion */
2788 /* see why the package is not installable */
2789 if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC && !pool_installable(pool, s))
2790 return SOLVER_PROBLEM_NOT_INSTALLABLE;
2791 /* check requires */
2794 reqp = s->repo->idarraydata + s->requires;
2795 while ((req = *reqp++) != 0)
2797 if (req == SOLVABLE_PREREQMARKER)
2799 dp = pool_whatprovides(pool, req);
2805 return SOLVER_PROBLEM_NOTHING_PROVIDES_DEP;
2810 r = solv->rules + rid;
2812 abort(); /* not a rpm rule */
2813 if (r->d == 0 && r->w2 == 0)
2815 /* an assertion. we don't store them as rpm rules, so
2819 s = pool->solvables - r->p;
2820 if (installed && !solv->fixsystem && s->repo == installed)
2822 if (r->d == 0 && r->w2 < 0)
2824 /* a package conflict */
2825 Solvable *s2 = pool->solvables - r->w2;
2828 if (installed && !solv->fixsystem && s2->repo == installed)
2831 /* if both packages have the same name and at least one of them
2832 * is not installed, they conflict */
2833 if (s->name == s2->name && (!installed || (s->repo != installed || s2->repo != installed)))
2838 return SOLVER_PROBLEM_SAME_NAME;
2841 /* check conflicts in both directions */
2844 conp = s->repo->idarraydata + s->conflicts;
2845 while ((con = *conp++) != 0)
2847 FOR_PROVIDES(p, pp, con)
2849 if (dontfix && pool->solvables[p].repo == installed)
2856 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
2862 conp = s2->repo->idarraydata + s2->conflicts;
2863 while ((con = *conp++) != 0)
2865 FOR_PROVIDES(p, pp, con)
2867 if (dontfix2 && pool->solvables[p].repo == installed)
2874 return SOLVER_PROBLEM_PACKAGE_CONFLICT;
2878 /* check obsoletes in both directions */
2879 if ((!installed || s->repo != installed) && s->obsoletes)
2881 obsp = s->repo->idarraydata + s->obsoletes;
2882 while ((obs = *obsp++) != 0)
2884 FOR_PROVIDES(p, pp, obs)
2891 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
2895 if ((!installed || s2->repo != installed) && s2->obsoletes)
2897 obsp = s2->repo->idarraydata + s2->obsoletes;
2898 while ((obs = *obsp++) != 0)
2900 FOR_PROVIDES(p, pp, obs)
2907 return SOLVER_PROBLEM_PACKAGE_OBSOLETES;
2911 /* all cases checked, can't happen */
2914 /* simple requires */
2917 reqp = s->repo->idarraydata + s->requires;
2918 while ((req = *reqp++) != 0)
2920 if (req == SOLVABLE_PREREQMARKER)
2922 dp = pool_whatprovides(pool, req);
2925 if (*dp == r->w2 && dp[1] == 0)
2928 else if (dp - pool->whatprovidesdata == r->d)
2936 return SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE;
2940 findproblemrule_internal(Solver *solv, Id idx, Id *reqrp, Id *conrp, Id *sysrp, Id *jobrp)
2943 Id lreqr, lconr, lsysr, ljobr;
2947 lreqr = lconr = lsysr = ljobr = 0;
2948 while ((rid = solv->learnt_pool.elements[idx++]) != 0)
2950 if (rid >= solv->learntrules)
2951 findproblemrule_internal(solv, solv->learnt_why.elements[rid - solv->learntrules], &lreqr, &lconr, &lsysr, &ljobr);
2952 else if (rid >= solv->systemrules)
2957 else if (rid >= solv->jobrules)
2964 r = solv->rules + rid;
2965 if (!r->d && r->w2 < 0)
2978 /* assertion, counts as require rule */
2979 /* ignore system solvable as we need useful info */
2980 if (rid == -SYSTEMSOLVABLE)
2982 if (!*reqrp || !reqassert)
2989 if (!*reqrp && lreqr)
2991 if (!*conrp && lconr)
2993 if (!*jobrp && ljobr)
2995 if (!*sysrp && lsysr)
3000 solver_findproblemrule(Solver *solv, Id problem)
3002 Id reqr, conr, sysr, jobr;
3003 Id idx = solv->problems.elements[problem - 1];
3004 reqr = conr = sysr = jobr = 0;
3005 findproblemrule_internal(solv, idx, &reqr, &conr, &sysr, &jobr);
3018 printprobleminfo(Solver *solv, Queue *job, Id problem)
3020 Pool *pool = solv->pool;
3022 Id dep, source, target;
3025 probr = solver_findproblemrule(solv, problem);
3026 switch (solver_problemruleinfo(solv, job, probr, &dep, &source, &target))
3028 case SOLVER_PROBLEM_UPDATE_RULE:
3029 s = pool_id2solvable(pool, source);
3030 POOL_DEBUG(SAT_DEBUG_RESULT, "problem with installed package %s\n", solvable2str(pool, s));
3032 case SOLVER_PROBLEM_JOB_RULE:
3033 POOL_DEBUG(SAT_DEBUG_RESULT, "conflicting requests\n");
3035 case SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP:
3036 POOL_DEBUG(SAT_DEBUG_RESULT, "nothing provides requested %s\n", dep2str(pool, dep));
3038 case SOLVER_PROBLEM_NOT_INSTALLABLE:
3039 s = pool_id2solvable(pool, source);
3040 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s is not installable\n", solvable2str(pool, s));
3042 case SOLVER_PROBLEM_NOTHING_PROVIDES_DEP:
3043 s = pool_id2solvable(pool, source);
3044 POOL_DEBUG(SAT_DEBUG_RESULT, "nothing provides %s needed by %s\n", dep2str(pool, dep), solvable2str(pool, s));
3046 case SOLVER_PROBLEM_SAME_NAME:
3047 s = pool_id2solvable(pool, source);
3048 s2 = pool_id2solvable(pool, target);
3049 POOL_DEBUG(SAT_DEBUG_RESULT, "cannot install both %s and %s\n", solvable2str(pool, s), solvable2str(pool, s2));
3051 case SOLVER_PROBLEM_PACKAGE_CONFLICT:
3052 s = pool_id2solvable(pool, source);
3053 s2 = pool_id2solvable(pool, target);
3054 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s conflicts with %s provided by %s\n", solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
3056 case SOLVER_PROBLEM_PACKAGE_OBSOLETES:
3057 s = pool_id2solvable(pool, source);
3058 s2 = pool_id2solvable(pool, target);
3059 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s obsoletes %s provided by %s\n", solvable2str(pool, s), dep2str(pool, dep), solvable2str(pool, s2));
3061 case SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE:
3062 s = pool_id2solvable(pool, source);
3063 POOL_DEBUG(SAT_DEBUG_RESULT, "package %s requires %s, but none of the providers can be installed\n", solvable2str(pool, s), dep2str(pool, dep));
3069 printsolutions(Solver *solv, Queue *job)
3071 Pool *pool = solv->pool;
3074 Id problem, solution, element;
3077 POOL_DEBUG(SAT_DEBUG_RESULT, "Encountered problems! Here are the solutions:\n\n");
3080 while ((problem = solver_next_problem(solv, problem)) != 0)
3082 POOL_DEBUG(SAT_DEBUG_RESULT, "Problem %d:\n", pcnt++);
3083 POOL_DEBUG(SAT_DEBUG_RESULT, "====================================\n");
3084 printprobleminfo(solv, job, problem);
3085 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
3087 while ((solution = solver_next_solution(solv, problem, solution)) != 0)
3090 while ((element = solver_next_solutionelement(solv, problem, solution, element, &p, &rp)) != 0)
3094 /* job, rp is index into job queue */
3095 what = job->elements[rp];
3096 switch (job->elements[rp - 1])
3098 case SOLVER_INSTALL_SOLVABLE:
3099 s = pool->solvables + what;
3100 if (solv->installed && s->repo == solv->installed)
3101 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not keep %s installed\n", solvable2str(pool, s));
3103 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install %s\n", solvable2str(pool, s));
3105 case SOLVER_ERASE_SOLVABLE:
3106 s = pool->solvables + what;
3107 if (solv->installed && s->repo == solv->installed)
3108 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall %s\n", solvable2str(pool, s));
3110 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not forbid installation of %s\n", solvable2str(pool, s));
3112 case SOLVER_INSTALL_SOLVABLE_NAME:
3113 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install %s\n", id2str(pool, what));
3115 case SOLVER_ERASE_SOLVABLE_NAME:
3116 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall %s\n", id2str(pool, what));
3118 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3119 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install a solvable providing %s\n", dep2str(pool, what));
3121 case SOLVER_ERASE_SOLVABLE_PROVIDES:
3122 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not deinstall all solvables providing %s\n", dep2str(pool, what));
3124 case SOLVER_INSTALL_SOLVABLE_UPDATE:
3125 s = pool->solvables + what;
3126 POOL_DEBUG(SAT_DEBUG_RESULT, "- do not install most recent version of %s\n", solvable2str(pool, s));
3129 POOL_DEBUG(SAT_DEBUG_RESULT, "- do something different\n");
3135 /* policy, replace p with rp */
3136 s = pool->solvables + p;
3137 sd = rp ? pool->solvables + rp : 0;
3141 if (!solv->allowdowngrade && evrcmp(pool, s->evr, sd->evr) > 0)
3143 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow downgrade of %s to %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3146 if (!solv->allowarchchange && s->name == sd->name && s->arch != sd->arch && policy_illegal_archchange(pool, s, sd))
3148 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow architecture change of %s to %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3151 if (!solv->allowvendorchange && s->name == sd->name && s->vendor != sd->vendor && policy_illegal_vendorchange(pool, s, sd))
3154 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));
3156 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));
3160 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow replacement of %s with %s\n", solvable2str(pool, s), solvable2str(pool, sd));
3164 POOL_DEBUG(SAT_DEBUG_RESULT, "- allow deinstallation of %s\n", solvable2str(pool, s));
3169 POOL_DEBUG(SAT_DEBUG_RESULT, "\n");
3175 /* for each installed solvable find which packages with *different* names
3176 * obsolete the solvable.
3177 * this index is used in policy_findupdatepackages if noupdateprovide is set.
3181 create_obsolete_index(Solver *solv)
3183 Pool *pool = solv->pool;
3185 Repo *installed = solv->installed;
3186 Id p, *pp, obs, *obsp, *obsoletes, *obsoletes_data;
3189 if (!installed || !installed->nsolvables)
3191 /* create reverse obsoletes map for installed solvables */
3192 solv->obsoletes = obsoletes = xcalloc(installed->end - installed->start, sizeof(Id));
3193 for (i = 1; i < pool->nsolvables; i++)
3195 s = pool->solvables + i;
3196 if (s->repo == installed)
3200 if (!pool_installable(pool, s))
3202 obsp = s->repo->idarraydata + s->obsoletes;
3203 while ((obs = *obsp++) != 0)
3204 FOR_PROVIDES(p, pp, obs)
3206 if (pool->solvables[p].repo != installed)
3208 if (pool->solvables[p].name == s->name)
3210 obsoletes[p - installed->start]++;
3214 for (i = 0; i < installed->nsolvables; i++)
3217 n += obsoletes[i] + 1;
3220 solv->obsoletes_data = obsoletes_data = xcalloc(n + 1, sizeof(Id));
3221 POOL_DEBUG(SAT_DEBUG_STATS, "obsoletes data: %d entries\n", n + 1);
3222 for (i = pool->nsolvables - 1; i > 0; i--)
3224 s = pool->solvables + i;
3225 if (s->repo == installed)
3229 if (!pool_installable(pool, s))
3231 obsp = s->repo->idarraydata + s->obsoletes;
3232 while ((obs = *obsp++) != 0)
3233 FOR_PROVIDES(p, pp, obs)
3235 if (pool->solvables[p].repo != installed)
3237 if (pool->solvables[p].name == s->name)
3239 p -= installed->start;
3240 if (obsoletes_data[obsoletes[p]] != i)
3241 obsoletes_data[--obsoletes[p]] = i;
3247 /*-----------------------------------------------------------------*/
3257 solver_solve(Solver *solv, Queue *job)
3259 Pool *pool = solv->pool;
3260 Repo *installed = solv->installed;
3263 Map addedmap; /* '1' == have rpm-rules for solvable */
3264 Id how, what, p, *pp, d;
3268 /* create whatprovides if not already there */
3269 if (pool->whatprovides)
3270 pool_createwhatprovides(pool);
3272 /* create obsolete index if needed */
3273 if (solv->noupdateprovide)
3274 create_obsolete_index(solv);
3277 * create basic rule set of all involved packages
3278 * use addedmap bitmap to make sure we don't create rules twice
3282 map_init(&addedmap, pool->nsolvables);
3286 * always install our system solvable
3288 MAPSET(&addedmap, SYSTEMSOLVABLE);
3289 queue_push(&solv->decisionq, SYSTEMSOLVABLE);
3290 queue_push(&solv->decisionq_why, 0);
3291 solv->decisionmap[SYSTEMSOLVABLE] = 1;
3294 * create rules for all package that could be involved with the solving
3295 * so called: rpm rules
3300 oldnrules = solv->nrules;
3301 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for installed solvables ***\n");
3302 FOR_REPO_SOLVABLES(installed, p, s)
3303 addrpmrulesforsolvable(solv, s, &addedmap);
3304 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
3305 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for updaters of installed solvables ***\n");
3306 oldnrules = solv->nrules;
3307 FOR_REPO_SOLVABLES(installed, p, s)
3308 addrpmrulesforupdaters(solv, s, &addedmap, 1);
3309 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
3312 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for packages involved with a job ***\n");
3313 oldnrules = solv->nrules;
3314 for (i = 0; i < job->count; i += 2)
3316 how = job->elements[i];
3317 what = job->elements[i + 1];
3321 case SOLVER_INSTALL_SOLVABLE:
3322 addrpmrulesforsolvable(solv, pool->solvables + what, &addedmap);
3324 case SOLVER_INSTALL_SOLVABLE_NAME:
3325 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3326 FOR_PROVIDES(p, pp, what)
3328 /* if by name, ensure that the name matches */
3329 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
3331 addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
3334 case SOLVER_INSTALL_SOLVABLE_UPDATE:
3335 /* dont allow downgrade */
3336 addrpmrulesforupdaters(solv, pool->solvables + what, &addedmap, 0);
3340 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
3342 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** create rpm rules for recommended/suggested packages ***\n");
3344 oldnrules = solv->nrules;
3345 addrpmrulesforweak(solv, &addedmap);
3346 POOL_DEBUG(SAT_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
3348 IF_POOLDEBUG (SAT_DEBUG_STATS)
3350 int possible = 0, installable = 0;
3351 for (i = 1; i < pool->nsolvables; i++)
3353 if (pool_installable(pool, pool->solvables + i))
3355 if (MAPTST(&addedmap, i))
3358 POOL_DEBUG(SAT_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
3362 * first pass done, we now have all the rpm rules we need.
3363 * unify existing rules before going over all job rules and
3365 * at this point the system is always solvable,
3366 * as an empty system (remove all packages) is a valid solution
3369 unifyrules(solv); /* remove duplicate rpm rules */
3371 POOL_DEBUG(SAT_DEBUG_STATS, "decisions so far: %d\n", solv->decisionq.count);
3372 IF_POOLDEBUG (SAT_DEBUG_SCHUBI)
3373 printdecisions (solv);
3376 * now add all job rules
3379 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add JOB rules ***\n");
3381 solv->jobrules = solv->nrules;
3383 for (i = 0; i < job->count; i += 2)
3385 int oldnrules = solv->nrules;
3387 how = job->elements[i];
3388 what = job->elements[i + 1];
3391 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
3392 s = pool->solvables + what;
3393 POOL_DEBUG(SAT_DEBUG_JOB, "job: install solvable %s\n", solvable2str(pool, s));
3394 addrule(solv, what, 0); /* install by Id */
3395 queue_push(&solv->ruletojob, i);
3397 case SOLVER_ERASE_SOLVABLE:
3398 s = pool->solvables + what;
3399 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase solvable %s\n", solvable2str(pool, s));
3400 addrule(solv, -what, 0); /* remove by Id */
3401 queue_push(&solv->ruletojob, i);
3403 case SOLVER_INSTALL_SOLVABLE_NAME: /* install by capability */
3404 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3405 if (how == SOLVER_INSTALL_SOLVABLE_NAME)
3406 POOL_DEBUG(SAT_DEBUG_JOB, "job: install name %s\n", id2str(pool, what));
3407 if (how == SOLVER_INSTALL_SOLVABLE_PROVIDES)
3408 POOL_DEBUG(SAT_DEBUG_JOB, "job: install provides %s\n", dep2str(pool, what));
3410 FOR_PROVIDES(p, pp, what)
3412 /* if by name, ensure that the name matches */
3413 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
3419 /* no provider, make this an impossible rule */
3420 queue_push(&q, -SYSTEMSOLVABLE);
3423 p = queue_shift(&q); /* get first provider */
3425 d = 0; /* single provider ? -> make assertion */
3427 d = pool_queuetowhatprovides(pool, &q); /* get all providers */
3428 addrule(solv, p, d); /* add 'requires' rule */
3429 queue_push(&solv->ruletojob, i);
3431 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
3432 case SOLVER_ERASE_SOLVABLE_PROVIDES:
3433 if (how == SOLVER_ERASE_SOLVABLE_NAME)
3434 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase name %s\n", id2str(pool, what));
3435 if (how == SOLVER_ERASE_SOLVABLE_PROVIDES)
3436 POOL_DEBUG(SAT_DEBUG_JOB, "job: erase provides %s\n", dep2str(pool, what));
3437 FOR_PROVIDES(p, pp, what)
3439 /* if by name, ensure that the name matches */
3440 if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
3442 addrule(solv, -p, 0); /* add 'remove' rule */
3443 queue_push(&solv->ruletojob, i);
3446 case SOLVER_INSTALL_SOLVABLE_UPDATE: /* find update for solvable */
3447 s = pool->solvables + what;
3448 POOL_DEBUG(SAT_DEBUG_JOB, "job: update %s\n", solvable2str(pool, s));
3449 addupdaterule(solv, s, 0);
3450 queue_push(&solv->ruletojob, i);
3453 IF_POOLDEBUG (SAT_DEBUG_JOB)
3456 if (solv->nrules == oldnrules)
3457 POOL_DEBUG(SAT_DEBUG_JOB, " - no rule created");
3458 for (j = oldnrules; j < solv->nrules; j++)
3460 POOL_DEBUG(SAT_DEBUG_JOB, " - job ");
3461 printrule(solv, SAT_DEBUG_JOB, solv->rules + j);
3466 if (solv->ruletojob.count != solv->nrules - solv->jobrules)
3470 * now add system rules
3474 POOL_DEBUG(SAT_DEBUG_SCHUBI, "*** Add system rules ***\n");
3477 solv->systemrules = solv->nrules;
3480 * create rules for updating installed solvables
3484 if (installed && !solv->allowuninstall)
3485 { /* loop over all installed solvables */
3486 /* we create all update rules, but disable some later on depending on the job */
3487 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3488 if (s->repo == installed)
3489 addupdaterule(solv, s, 0); /* allowall = 0 */
3491 addupdaterule(solv, 0, 0); /* create dummy rule; allowall = 0 */
3492 /* consistency check: we added a rule for _every_ system solvable */
3493 if (solv->nrules - solv->systemrules != installed->end - installed->start)
3497 /* create special weak system rules */
3498 /* those are used later on to keep a version of the installed packages in
3500 if (installed && installed->nsolvables)
3502 solv->weaksystemrules = xcalloc(installed->end - installed->start, sizeof(Id));
3503 FOR_REPO_SOLVABLES(installed, p, s)
3505 policy_findupdatepackages(solv, s, &q, 1);
3507 solv->weaksystemrules[p - installed->start] = pool_queuetowhatprovides(pool, &q);
3511 /* free unneeded memory */
3512 map_free(&addedmap);
3515 solv->weakrules = solv->nrules;
3517 /* try real hard to keep packages installed */
3520 FOR_REPO_SOLVABLES(installed, p, s)
3522 /* FIXME: can't work with refine_suggestion! */
3523 /* need to always add the rule but disable it */
3524 if (MAPTST(&solv->noupdate, p - installed->start))
3526 d = solv->weaksystemrules[p - installed->start];
3527 addrule(solv, p, d);
3531 /* all new rules are learnt after this point */
3532 solv->learntrules = solv->nrules;
3539 disableupdaterules(solv, job, -1);
3540 makeruledecisions(solv);
3542 POOL_DEBUG(SAT_DEBUG_STATS, "problems so far: %d\n", solv->problems.count);
3544 run_solver(solv, 1, 1);
3546 /* find suggested packages */
3547 if (!solv->problems.count)
3549 Id sug, *sugp, p, *pp;
3551 /* create map of all suggests that are still open */
3552 solv->recommends_index = -1;
3553 MAPZERO(&solv->suggestsmap);
3554 for (i = 0; i < solv->decisionq.count; i++)
3556 p = solv->decisionq.elements[i];
3559 s = pool->solvables + p;
3562 sugp = s->repo->idarraydata + s->suggests;
3563 while ((sug = *sugp++) != 0)
3565 FOR_PROVIDES(p, pp, sug)
3566 if (solv->decisionmap[p] > 0)
3569 continue; /* already fulfilled */
3570 FOR_PROVIDES(p, pp, sug)
3571 MAPSET(&solv->suggestsmap, p);
3575 for (i = 1; i < pool->nsolvables; i++)
3577 if (solv->decisionmap[i] != 0)
3579 s = pool->solvables + i;
3580 if (!MAPTST(&solv->suggestsmap, i))
3584 if (!pool_installable(pool, s))
3586 if (!solver_is_enhancing(solv, s))
3589 queue_push(&solv->suggestions, i);
3591 policy_filter_unwanted(solv, &solv->suggestions, 0, POLICY_MODE_SUGGEST);
3594 if (solv->problems.count)
3595 problems_to_solutions(solv, job);