2 * Copyright (c) 2007-2008, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * SAT based dependency solver
21 #include "solver_private.h"
27 #include "solverdebug.h"
29 #define RULES_BLOCK 63
31 /********************************************************************
33 * dependency check helpers
37 /*-------------------------------------------------------------------
38 * handle split provides
40 * a splitprovides dep looks like
41 * namespace:splitprovides(pkg REL_WITH path)
42 * and is only true if pkg is installed and contains the specified path.
43 * we also make sure that pkg is selected for an update, otherwise the
44 * update would always be forced onto the user.
47 solver_splitprovides(Solver *solv, Id dep)
49 Pool *pool = solv->pool;
54 if (!solv->dosplitprovides || !solv->installed || (!solv->updatemap_all && !solv->updatemap.size))
58 rd = GETRELDEP(pool, dep);
59 if (rd->flags != REL_WITH)
61 FOR_PROVIDES(p, pp, dep)
63 /* here we have packages that provide the correct name and contain the path,
64 * now do extra filtering */
65 s = pool->solvables + p;
66 if (s->repo == solv->installed && s->name == rd->name &&
67 (solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, p - solv->installed->start))))
74 /*-------------------------------------------------------------------
75 * solver_dep_installed
79 solver_dep_installed(Solver *solv, Id dep)
82 Pool *pool = solv->pool;
87 Reldep *rd = GETRELDEP(pool, dep);
88 if (rd->flags == REL_AND)
90 if (!solver_dep_installed(solv, rd->name))
92 return solver_dep_installed(solv, rd->evr);
94 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
95 return solver_dep_installed(solv, rd->evr);
97 FOR_PROVIDES(p, pp, dep)
99 if (p == SYSTEMSOLVABLE || (solv->installed && pool->solvables[p].repo == solv->installed))
106 /* mirrors solver_dep_installed, but returns 2 if a
107 * dependency listed in solv->installsuppdepq was involved */
109 solver_check_installsuppdepq_dep(Solver *solv, Id dep)
111 Pool *pool = solv->pool;
117 Reldep *rd = GETRELDEP(pool, dep);
118 if (rd->flags == REL_AND)
120 int r2, r1 = solver_check_installsuppdepq_dep(solv, rd->name);
123 r2 = solver_check_installsuppdepq_dep(solv, rd->evr);
126 return r1 == 2 || r2 == 2 ? 2 : 1;
128 if (rd->flags == REL_OR)
130 int r2, r1 = solver_check_installsuppdepq_dep(solv, rd->name);
131 r2 = solver_check_installsuppdepq_dep(solv, rd->evr);
134 return r1 == 2 || r2 == 2 ? 2 : 1;
136 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
137 return solver_splitprovides(solv, rd->evr);
138 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
139 return solver_dep_installed(solv, rd->evr);
140 if (rd->flags == REL_NAMESPACE && (q = solv->installsuppdepq) != 0)
143 for (i = 0; i < q->count; i++)
144 if (q->elements[i] == dep || q->elements[i] == rd->name)
148 FOR_PROVIDES(p, pp, dep)
149 if (solv->decisionmap[p] > 0)
155 solver_check_installsuppdepq(Solver *solv, Solvable *s)
158 supp = s->repo->idarraydata + s->supplements;
159 while ((sup = *supp++) != 0)
160 if (solver_check_installsuppdepq_dep(solv, sup) == 2)
166 autouninstall(Solver *solv, Id *problem)
168 Pool *pool = solv->pool;
170 int lastfeature = 0, lastupdate = 0;
174 for (i = 0; (v = problem[i]) != 0; i++)
177 extraflags &= solv->job.elements[-v - 1];
178 if (v >= solv->featurerules && v < solv->featurerules_end)
181 if (v >= solv->updaterules && v < solv->updaterules_end)
183 /* check if identical to feature rule */
184 Id p = solv->rules[v].p;
188 r = solv->rules + solv->featurerules + (p - solv->installed->start);
191 /* update rule == feature rule */
200 if (!lastupdate && !lastfeature)
202 v = lastupdate ? lastupdate : lastfeature;
203 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "allowuninstall disabling ");
204 solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + v);
205 solver_disableproblem(solv, v);
206 if (extraflags != -1 && (extraflags & SOLVER_CLEANDEPS) != 0 && solv->cleandepsmap.size)
208 /* add the package to the updatepkgs list, this will automatically turn
209 * on cleandeps mode */
210 Id p = solv->rules[v].p;
211 if (!solv->cleandeps_updatepkgs)
213 solv->cleandeps_updatepkgs = solv_calloc(1, sizeof(Queue));
214 queue_init(solv->cleandeps_updatepkgs);
218 int oldupdatepkgscnt = solv->cleandeps_updatepkgs->count;
219 queue_pushunique(solv->cleandeps_updatepkgs, p);
220 if (solv->cleandeps_updatepkgs->count != oldupdatepkgscnt)
221 solver_disablepolicyrules(solv);
227 /************************************************************************/
230 * enable/disable learnt rules
232 * we have enabled or disabled some of our rules. We now reenable all
233 * of our learnt rules except the ones that were learnt from rules that
237 enabledisablelearntrules(Solver *solv)
239 Pool *pool = solv->pool;
244 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "enabledisablelearntrules called\n");
245 for (i = solv->learntrules, r = solv->rules + i; i < solv->nrules; i++, r++)
247 whyp = solv->learnt_pool.elements + solv->learnt_why.elements[i - solv->learntrules];
248 while ((why = *whyp++) != 0)
250 assert(why > 0 && why < i);
251 if (solv->rules[why].d < 0)
254 /* why != 0: we found a disabled rule, disable the learnt rule */
255 if (why && r->d >= 0)
257 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
259 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "disabling ");
260 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
262 solver_disablerule(solv, r);
264 else if (!why && r->d < 0)
266 IF_POOLDEBUG (SOLV_DEBUG_SOLUTIONS)
268 POOL_DEBUG(SOLV_DEBUG_SOLUTIONS, "re-enabling ");
269 solver_printruleclass(solv, SOLV_DEBUG_SOLUTIONS, r);
271 solver_enablerule(solv, r);
278 * make assertion rules into decisions
280 * Go through rules and add direct assertions to the decisionqueue.
281 * If we find a conflict, disable rules and add them to problem queue.
285 makeruledecisions(Solver *solv)
287 Pool *pool = solv->pool;
292 int record_proof = 1;
294 int havedisabled = 0;
296 /* The system solvable is always installed first */
297 assert(solv->decisionq.count == 0);
298 queue_push(&solv->decisionq, SYSTEMSOLVABLE);
299 queue_push(&solv->decisionq_why, 0);
300 solv->decisionmap[SYSTEMSOLVABLE] = 1; /* installed at level '1' */
302 decisionstart = solv->decisionq.count;
305 /* if we needed to re-run, back up decisions to decisionstart */
306 while (solv->decisionq.count > decisionstart)
308 v = solv->decisionq.elements[--solv->decisionq.count];
309 --solv->decisionq_why.count;
311 solv->decisionmap[vv] = 0;
314 /* note that the ruleassertions queue is ordered */
315 for (ii = 0; ii < solv->ruleassertions.count; ii++)
317 ri = solv->ruleassertions.elements[ii];
318 r = solv->rules + ri;
320 if (havedisabled && ri >= solv->learntrules)
322 /* just started with learnt rule assertions. If we have disabled
323 * some rules, adapt the learnt rule status */
324 enabledisablelearntrules(solv);
328 if (r->d < 0 || !r->p || r->w2) /* disabled, dummy or no assertion */
331 /* do weak rules in phase 2 */
332 if (ri < solv->learntrules && MAPTST(&solv->weakrulemap, ri))
338 if (!solv->decisionmap[vv]) /* if not yet decided */
340 queue_push(&solv->decisionq, v);
341 queue_push(&solv->decisionq_why, r - solv->rules);
342 solv->decisionmap[vv] = v > 0 ? 1 : -1;
343 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
345 Solvable *s = solv->pool->solvables + vv;
347 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "conflicting %s (assertion)\n", pool_solvable2str(solv->pool, s));
349 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "installing %s (assertion)\n", pool_solvable2str(solv->pool, s));
354 /* check against previous decision: is there a conflict? */
355 if (v > 0 && solv->decisionmap[vv] > 0) /* ok to install */
357 if (v < 0 && solv->decisionmap[vv] < 0) /* ok to remove */
363 * The rule (r) we're currently processing says something
364 * different (v = r->p) than a previous decision (decisionmap[abs(v)])
368 if (ri >= solv->learntrules)
370 /* conflict with a learnt rule */
371 /* can happen when packages cannot be installed for multiple reasons. */
372 /* we disable the learnt rule in this case */
373 /* (XXX: we should really call analyze_unsolvable_rule here!) */
374 solver_disablerule(solv, r);
379 * find the decision which is the "opposite" of the rule
381 for (i = 0; i < solv->decisionq.count; i++)
382 if (solv->decisionq.elements[i] == -v)
384 assert(i < solv->decisionq.count); /* assert that we found it */
385 oldproblemcount = solv->problems.count;
388 * conflict with system solvable ?
390 if (v == -SYSTEMSOLVABLE)
394 queue_push(&solv->problems, solv->learnt_pool.count);
395 queue_push(&solv->learnt_pool, ri);
396 queue_push(&solv->learnt_pool, 0);
399 queue_push(&solv->problems, 0);
400 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflict with system solvable, disabling rule #%d\n", ri);
401 if (ri >= solv->jobrules && ri < solv->jobrules_end)
402 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
405 queue_push(&solv->problems, v);
406 queue_push(&solv->problems, 0);
407 if (solv->allowuninstall && v >= solv->featurerules && v < solv->updaterules_end)
408 solv->problems.count = oldproblemcount;
409 solver_disableproblem(solv, v);
411 break; /* start over */
414 assert(solv->decisionq_why.elements[i] > 0);
417 * conflict with an rpm rule ?
419 if (solv->decisionq_why.elements[i] < solv->rpmrules_end)
423 queue_push(&solv->problems, solv->learnt_pool.count);
424 queue_push(&solv->learnt_pool, ri);
425 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
426 queue_push(&solv->learnt_pool, 0);
429 queue_push(&solv->problems, 0);
430 assert(v > 0 || v == -SYSTEMSOLVABLE);
431 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflict with rpm rule, disabling rule #%d\n", ri);
432 if (ri >= solv->jobrules && ri < solv->jobrules_end)
433 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
436 queue_push(&solv->problems, v);
437 queue_push(&solv->problems, 0);
438 if (solv->allowuninstall && v >= solv->featurerules && v < solv->updaterules_end)
439 solv->problems.count = oldproblemcount;
440 solver_disableproblem(solv, v);
442 break; /* start over */
446 * conflict with another job or update/feature rule
452 queue_push(&solv->problems, solv->learnt_pool.count);
453 queue_push(&solv->learnt_pool, ri);
454 queue_push(&solv->learnt_pool, solv->decisionq_why.elements[i]);
455 queue_push(&solv->learnt_pool, 0);
458 queue_push(&solv->problems, 0);
460 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "conflicting update/job assertions over literal %d\n", vv);
463 * push all of our rules (can only be feature or job rules)
464 * asserting this literal on the problem stack
466 for (i = solv->featurerules, rr = solv->rules + i; i < solv->learntrules; i++, rr++)
468 if (rr->d < 0 /* disabled */
469 || rr->w2) /* or no assertion */
471 if (rr->p != vv /* not affecting the literal */
474 if (MAPTST(&solv->weakrulemap, i)) /* weak: silently ignore */
477 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, " - disabling rule #%d\n", i);
478 solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + i);
481 if (i >= solv->jobrules && i < solv->jobrules_end)
482 v = -(solv->ruletojob.elements[i - solv->jobrules] + 1);
483 queue_push(&solv->problems, v);
485 queue_push(&solv->problems, 0);
487 if (solv->allowuninstall && (v = autouninstall(solv, solv->problems.elements + oldproblemcount + 1)) != 0)
488 solv->problems.count = oldproblemcount;
490 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
491 solver_disableproblem(solv, solv->problems.elements[i]);
493 break; /* start over */
495 if (ii < solv->ruleassertions.count)
499 * phase 2: now do the weak assertions
501 for (ii = 0; ii < solv->ruleassertions.count; ii++)
503 ri = solv->ruleassertions.elements[ii];
504 r = solv->rules + ri;
505 if (r->d < 0 || r->w2) /* disabled or no assertion */
507 if (ri >= solv->learntrules || !MAPTST(&solv->weakrulemap, ri)) /* skip non-weak */
512 if (!solv->decisionmap[vv]) /* if not yet decided */
514 queue_push(&solv->decisionq, v);
515 queue_push(&solv->decisionq_why, r - solv->rules);
516 solv->decisionmap[vv] = v > 0 ? 1 : -1;
517 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
519 Solvable *s = solv->pool->solvables + vv;
521 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "conflicting %s (weak assertion)\n", pool_solvable2str(solv->pool, s));
523 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "installing %s (weak assertion)\n", pool_solvable2str(solv->pool, s));
527 /* check against previous decision: is there a conflict? */
528 if (v > 0 && solv->decisionmap[vv] > 0)
530 if (v < 0 && solv->decisionmap[vv] < 0)
533 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "assertion conflict, but I am weak, disabling ");
534 solver_printrule(solv, SOLV_DEBUG_UNSOLVABLE, r);
536 if (ri >= solv->jobrules && ri < solv->jobrules_end)
537 v = -(solv->ruletojob.elements[ri - solv->jobrules] + 1);
540 solver_disableproblem(solv, v);
542 solver_reenablepolicyrules(solv, -v);
544 break; /* start over */
546 if (ii == solv->ruleassertions.count)
547 break; /* finished! */
552 /********************************************************************/
556 /*-------------------------------------------------------------------
559 * initial setup for all watches
563 makewatches(Solver *solv)
567 int nsolvables = solv->pool->nsolvables;
569 solv_free(solv->watches);
570 /* lower half for removals, upper half for installs */
571 solv->watches = solv_calloc(2 * nsolvables, sizeof(Id));
573 /* do it reverse so rpm rules get triggered first (XXX: obsolete?) */
574 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
576 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
579 if (!r->w2) /* assertions do not need watches */
582 /* see addwatches_rule(solv, r) */
583 r->n1 = solv->watches[nsolvables + r->w1];
584 solv->watches[nsolvables + r->w1] = r - solv->rules;
586 r->n2 = solv->watches[nsolvables + r->w2];
587 solv->watches[nsolvables + r->w2] = r - solv->rules;
592 /*-------------------------------------------------------------------
594 * add watches (for a new learned rule)
595 * sets up watches for a single rule
597 * see also makewatches() above.
601 addwatches_rule(Solver *solv, Rule *r)
603 int nsolvables = solv->pool->nsolvables;
605 r->n1 = solv->watches[nsolvables + r->w1];
606 solv->watches[nsolvables + r->w1] = r - solv->rules;
608 r->n2 = solv->watches[nsolvables + r->w2];
609 solv->watches[nsolvables + r->w2] = r - solv->rules;
613 /********************************************************************/
619 /* shortcuts to check if a literal (positive or negative) assignment
620 * evaluates to 'true' or 'false'
622 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
623 #define DECISIONMAP_FALSE(p) ((p) > 0 ? (decisionmap[p] < 0) : (decisionmap[-p] > 0))
624 #define DECISIONMAP_UNDEF(p) (decisionmap[(p) > 0 ? (p) : -(p)] == 0)
626 /*-------------------------------------------------------------------
630 * make decision and propagate to all rules
632 * Evaluate each term affected by the decision (linked through watches).
633 * If we find unit rules we make new decisions based on them.
635 * return : 0 = everything is OK
636 * rule = conflict found in this rule
640 propagate(Solver *solv, int level)
642 Pool *pool = solv->pool;
643 Id *rp, *next_rp; /* rule pointer, next rule pointer in linked list */
645 Id p, pkg, other_watch;
647 Id *decisionmap = solv->decisionmap;
649 Id *watches = solv->watches + pool->nsolvables; /* place ptr in middle */
651 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "----- propagate -----\n");
653 /* foreach non-propagated decision */
654 while (solv->propagate_index < solv->decisionq.count)
657 * 'pkg' was just decided
658 * negate because our watches trigger if literal goes FALSE
660 pkg = -solv->decisionq.elements[solv->propagate_index++];
662 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
664 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "propagate for decision %d level %d\n", -pkg, level);
665 solver_printruleelement(solv, SOLV_DEBUG_PROPAGATE, 0, -pkg);
668 /* foreach rule where 'pkg' is now FALSE */
669 for (rp = watches + pkg; *rp; rp = next_rp)
671 r = solv->rules + *rp;
674 /* rule is disabled, goto next */
682 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
684 POOL_DEBUG(SOLV_DEBUG_PROPAGATE," watch triggered ");
685 solver_printrule(solv, SOLV_DEBUG_PROPAGATE, r);
688 /* 'pkg' was just decided (was set to FALSE)
690 * now find other literal watch, check clause
691 * and advance on linked list
705 * This term is already true (through the other literal)
706 * so we have nothing to do
708 if (DECISIONMAP_TRUE(other_watch))
712 * The other literal is FALSE or UNDEF
718 /* Not a binary clause, try to move our watch.
720 * Go over all literals and find one that is
724 * (TRUE is also ok, in that case the rule is fulfilled)
726 if (r->p /* we have a 'p' */
727 && r->p != other_watch /* which is not watched */
728 && !DECISIONMAP_FALSE(r->p)) /* and not FALSE */
732 else /* go find a 'd' to make 'true' */
735 we just iterate sequentially, doing it in another order just changes the order of decisions, not the decisions itself
737 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
739 if (p != other_watch /* which is not watched */
740 && !DECISIONMAP_FALSE(p)) /* and not FALSE */
748 * if we found some p that is UNDEF or TRUE, move
751 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
754 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, " -> move w%d to %s\n", (pkg == r->w1 ? 1 : 2), pool_solvid2str(pool, p));
756 POOL_DEBUG(SOLV_DEBUG_PROPAGATE," -> move w%d to !%s\n", (pkg == r->w1 ? 1 : 2), pool_solvid2str(pool, -p));
772 watches[p] = r - solv->rules;
775 /* search failed, thus all unwatched literals are FALSE */
780 * unit clause found, set literal other_watch to TRUE
783 if (DECISIONMAP_FALSE(other_watch)) /* check if literal is FALSE */
784 return r; /* eek, a conflict! */
786 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
788 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, " unit ");
789 solver_printrule(solv, SOLV_DEBUG_PROPAGATE, r);
793 decisionmap[other_watch] = level; /* install! */
795 decisionmap[-other_watch] = -level; /* remove! */
797 queue_push(&solv->decisionq, other_watch);
798 queue_push(&solv->decisionq_why, r - solv->rules);
800 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
803 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, " -> decided to install %s\n", pool_solvid2str(pool, other_watch));
805 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, " -> decided to conflict %s\n", pool_solvid2str(pool, -other_watch));
808 } /* foreach rule involving 'pkg' */
810 } /* while we have non-decided decisions */
812 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "----- propagate end-----\n");
814 return 0; /* all is well */
818 /********************************************************************/
821 /*-------------------------------------------------------------------
828 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *whyp)
830 Pool *pool = solv->pool;
834 Map seen; /* global? */
835 Id d, v, vv, *dp, why;
837 int num = 0, l1num = 0;
838 int learnt_why = solv->learnt_pool.count;
839 Id *decisionmap = solv->decisionmap;
841 queue_init_buffer(&r, r_buf, sizeof(r_buf)/sizeof(*r_buf));
843 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "ANALYZE at %d ----------------------\n", level);
844 map_init(&seen, pool->nsolvables);
845 idx = solv->decisionq.count;
848 IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
849 solver_printruleclass(solv, SOLV_DEBUG_ANALYZE, c);
850 queue_push(&solv->learnt_pool, c - solv->rules);
851 d = c->d < 0 ? -c->d - 1 : c->d;
852 dp = d ? pool->whatprovidesdata + d : 0;
853 /* go through all literals of the rule */
865 if (DECISIONMAP_TRUE(v)) /* the one true literal */
868 if (MAPTST(&seen, vv))
870 l = solv->decisionmap[vv];
873 MAPSET(&seen, vv); /* mark that we also need to look at this literal */
875 l1num++; /* need to do this one in level1 pass */
877 num++; /* need to do this one as well */
880 queue_push(&r, v); /* not level1 or conflict level, add to new rule */
886 if (!num && !--l1num)
887 break; /* all level 1 literals done */
889 /* find the next literal to investigate */
890 /* (as num + l1num > 0, we know that we'll always find one) */
894 v = solv->decisionq.elements[--idx];
896 if (MAPTST(&seen, vv))
901 if (num && --num == 0)
903 *pr = -v; /* so that v doesn't get lost */
906 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "got %d involved level 1 decisions\n", l1num);
907 /* clear non-l1 bits from seen map */
908 for (i = 0; i < r.count; i++)
911 MAPCLR(&seen, v > 0 ? v : -v);
913 /* only level 1 marks left in seen map */
914 l1num++; /* as l1retry decrements it */
918 why = solv->decisionq_why.elements[idx];
919 if (why <= 0) /* just in case, maybe for SYSTEMSOLVABLE */
921 c = solv->rules + why;
927 else if (r.count == 1 && r.elements[0] < 0)
930 *dr = pool_queuetowhatprovides(pool, &r);
931 IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
933 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "learned rule for level %d (am %d)\n", rlevel, level);
934 solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, *pr);
935 for (i = 0; i < r.count; i++)
936 solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, r.elements[i]);
938 /* push end marker on learnt reasons stack */
939 queue_push(&solv->learnt_pool, 0);
943 solv->stats_learned++;
948 /*-------------------------------------------------------------------
952 * reset all solver decisions
953 * called after rules have been enabled/disabled
957 solver_reset(Solver *solv)
959 Pool *pool = solv->pool;
963 /* rewind all decisions */
964 for (i = solv->decisionq.count - 1; i >= 0; i--)
966 v = solv->decisionq.elements[i];
967 solv->decisionmap[v > 0 ? v : -v] = 0;
969 queue_empty(&solv->decisionq_why);
970 queue_empty(&solv->decisionq);
971 solv->recommends_index = -1;
972 solv->propagate_index = 0;
973 queue_empty(&solv->branches);
975 /* adapt learnt rule status to new set of enabled/disabled rules */
976 enabledisablelearntrules(solv);
978 /* redo all assertion rule decisions */
979 makeruledecisions(solv);
980 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "decisions so far: %d\n", solv->decisionq.count);
984 /*-------------------------------------------------------------------
986 * analyze_unsolvable_rule
988 * recursion helper used by analyze_unsolvable
992 analyze_unsolvable_rule(Solver *solv, Rule *r, Id *lastweakp, Map *rseen)
994 Pool *pool = solv->pool;
996 Id why = r - solv->rules;
998 IF_POOLDEBUG (SOLV_DEBUG_UNSOLVABLE)
999 solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, r);
1000 if (solv->learntrules && why >= solv->learntrules)
1002 if (MAPTST(rseen, why - solv->learntrules))
1004 MAPSET(rseen, why - solv->learntrules);
1005 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1006 if (solv->learnt_pool.elements[i] > 0)
1007 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], lastweakp, rseen);
1010 if (MAPTST(&solv->weakrulemap, why))
1011 if (!*lastweakp || why > *lastweakp)
1013 /* do not add rpm rules to problem */
1014 if (why < solv->rpmrules_end)
1016 /* turn rule into problem */
1017 if (why >= solv->jobrules && why < solv->jobrules_end)
1018 why = -(solv->ruletojob.elements[why - solv->jobrules] + 1);
1019 /* normalize dup/infarch rules */
1020 if (why > solv->infarchrules && why < solv->infarchrules_end)
1022 Id name = pool->solvables[-solv->rules[why].p].name;
1023 while (why > solv->infarchrules && pool->solvables[-solv->rules[why - 1].p].name == name)
1026 if (why > solv->duprules && why < solv->duprules_end)
1028 Id name = pool->solvables[-solv->rules[why].p].name;
1029 while (why > solv->duprules && pool->solvables[-solv->rules[why - 1].p].name == name)
1033 /* return if problem already countains our rule */
1034 if (solv->problems.count)
1036 for (i = solv->problems.count - 1; i >= 0; i--)
1037 if (solv->problems.elements[i] == 0) /* end of last problem reached? */
1039 else if (solv->problems.elements[i] == why)
1042 queue_push(&solv->problems, why);
1046 /*-------------------------------------------------------------------
1048 * analyze_unsolvable
1050 * We know that the problem is not solvable. Record all involved
1051 * rules (i.e. the "proof") into solv->learnt_pool.
1052 * Record the learnt pool index and all non-rpm rules into
1053 * solv->problems. (Our solutions to fix the problems are to
1054 * disable those rules.)
1056 * If the proof contains at least one weak rule, we disable the
1059 * Otherwise we return 0 if disablerules is not set or disable
1060 * _all_ of the problem rules and return 1.
1062 * return: 1 - disabled some rules, try again
1067 analyze_unsolvable(Solver *solv, Rule *cr, int disablerules)
1069 Pool *pool = solv->pool;
1071 Map seen; /* global to speed things up? */
1073 Id d, v, vv, *dp, why;
1075 Id *decisionmap = solv->decisionmap;
1076 int oldproblemcount;
1077 int oldlearntpoolcount;
1079 int record_proof = 1;
1081 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "ANALYZE UNSOLVABLE ----------------------\n");
1082 solv->stats_unsolvable++;
1083 oldproblemcount = solv->problems.count;
1084 oldlearntpoolcount = solv->learnt_pool.count;
1086 /* make room for proof index */
1087 /* must update it later, as analyze_unsolvable_rule would confuse
1088 * it with a rule index if we put the real value in already */
1089 queue_push(&solv->problems, 0);
1092 map_init(&seen, pool->nsolvables);
1093 map_init(&rseen, solv->learntrules ? solv->nrules - solv->learntrules : 0);
1095 queue_push(&solv->learnt_pool, r - solv->rules);
1097 analyze_unsolvable_rule(solv, r, &lastweak, &rseen);
1098 d = r->d < 0 ? -r->d - 1 : r->d;
1099 dp = d ? pool->whatprovidesdata + d : 0;
1110 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1112 vv = v > 0 ? v : -v;
1113 l = solv->decisionmap[vv];
1118 idx = solv->decisionq.count;
1121 v = solv->decisionq.elements[--idx];
1122 vv = v > 0 ? v : -v;
1123 if (!MAPTST(&seen, vv))
1125 why = solv->decisionq_why.elements[idx];
1128 queue_push(&solv->learnt_pool, why);
1129 r = solv->rules + why;
1130 analyze_unsolvable_rule(solv, r, &lastweak, &rseen);
1131 d = r->d < 0 ? -r->d - 1 : r->d;
1132 dp = d ? pool->whatprovidesdata + d : 0;
1143 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1145 vv = v > 0 ? v : -v;
1146 l = solv->decisionmap[vv];
1154 queue_push(&solv->problems, 0); /* mark end of this problem */
1158 /* disable last weak rule */
1159 solv->problems.count = oldproblemcount;
1160 solv->learnt_pool.count = oldlearntpoolcount;
1161 if (lastweak >= solv->jobrules && lastweak < solv->jobrules_end)
1162 v = -(solv->ruletojob.elements[lastweak - solv->jobrules] + 1);
1165 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "disabling ");
1166 solver_printruleclass(solv, SOLV_DEBUG_UNSOLVABLE, solv->rules + lastweak);
1167 if (lastweak >= solv->choicerules && lastweak < solv->choicerules_end)
1168 solver_disablechoicerules(solv, solv->rules + lastweak);
1169 solver_disableproblem(solv, v);
1171 solver_reenablepolicyrules(solv, -v);
1176 if (solv->allowuninstall && (v = autouninstall(solv, solv->problems.elements + oldproblemcount + 1)) != 0)
1178 solv->problems.count = oldproblemcount;
1179 solv->learnt_pool.count = oldlearntpoolcount;
1187 queue_push(&solv->learnt_pool, 0);
1188 solv->problems.elements[oldproblemcount] = oldlearntpoolcount;
1191 /* + 2: index + trailing zero */
1192 if (disablerules && oldproblemcount + 2 < solv->problems.count)
1194 for (i = oldproblemcount + 1; i < solv->problems.count - 1; i++)
1195 solver_disableproblem(solv, solv->problems.elements[i]);
1196 /* XXX: might want to enable all weak rules again */
1200 POOL_DEBUG(SOLV_DEBUG_UNSOLVABLE, "UNSOLVABLE\n");
1205 /********************************************************************/
1206 /* Decision revert */
1208 /*-------------------------------------------------------------------
1211 * revert decisionq to a level
1215 revert(Solver *solv, int level)
1217 Pool *pool = solv->pool;
1219 while (solv->decisionq.count)
1221 v = solv->decisionq.elements[solv->decisionq.count - 1];
1222 vv = v > 0 ? v : -v;
1223 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1225 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1226 solv->decisionmap[vv] = 0;
1227 solv->decisionq.count--;
1228 solv->decisionq_why.count--;
1229 solv->propagate_index = solv->decisionq.count;
1231 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] <= -level)
1233 solv->branches.count--;
1234 while (solv->branches.count && solv->branches.elements[solv->branches.count - 1] >= 0)
1235 solv->branches.count--;
1237 solv->recommends_index = -1;
1241 /*-------------------------------------------------------------------
1243 * watch2onhighest - put watch2 on literal with highest level
1247 watch2onhighest(Solver *solv, Rule *r)
1252 d = r->d < 0 ? -r->d - 1 : r->d;
1254 return; /* binary rule, both watches are set */
1255 dp = solv->pool->whatprovidesdata + d;
1256 while ((v = *dp++) != 0)
1258 l = solv->decisionmap[v < 0 ? -v : v];
1270 /*-------------------------------------------------------------------
1274 * add free decision (solvable to install) to decisionq
1275 * increase level and propagate decision
1276 * return if no conflict.
1278 * in conflict case, analyze conflict rule, add resulting
1279 * rule to learnt rule set, make decision from learnt
1280 * rule (always unit) and re-propagate.
1282 * returns the new solver level or 0 if unsolvable
1287 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules, Id ruleid)
1289 Pool *pool = solv->pool;
1294 assert(ruleid >= 0);
1299 solv->decisionmap[decision] = level;
1301 solv->decisionmap[-decision] = -level;
1302 queue_push(&solv->decisionq, decision);
1303 queue_push(&solv->decisionq_why, -ruleid); /* <= 0 -> free decision */
1307 r = propagate(solv, level);
1311 return analyze_unsolvable(solv, r, disablerules);
1312 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "conflict with rule #%d\n", (int)(r - solv->rules));
1313 l = analyze(solv, level, r, &p, &d, &why); /* learnt rule in p and d */
1314 assert(l > 0 && l < level);
1315 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "reverting decisions (level %d -> %d)\n", level, l);
1317 revert(solv, level);
1318 r = solver_addrule(solv, p, d);
1320 assert(solv->learnt_why.count == (r - solv->rules) - solv->learntrules);
1321 queue_push(&solv->learnt_why, why);
1324 /* at least 2 literals, needs watches */
1325 watch2onhighest(solv, r);
1326 addwatches_rule(solv, r);
1330 /* learnt rule is an assertion */
1331 queue_push(&solv->ruleassertions, r - solv->rules);
1333 /* the new rule is unit by design */
1334 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1335 queue_push(&solv->decisionq, p);
1336 queue_push(&solv->decisionq_why, r - solv->rules);
1337 IF_POOLDEBUG (SOLV_DEBUG_ANALYZE)
1339 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "decision: ");
1340 solver_printruleelement(solv, SOLV_DEBUG_ANALYZE, 0, p);
1341 POOL_DEBUG(SOLV_DEBUG_ANALYZE, "new rule: ");
1342 solver_printrule(solv, SOLV_DEBUG_ANALYZE, r);
1349 /*-------------------------------------------------------------------
1351 * select and install
1353 * install best package from the queue. We add an extra package, inst, if
1354 * provided. See comment in weak install section.
1356 * returns the new solver level or 0 if unsolvable
1361 selectandinstall(Solver *solv, int level, Queue *dq, int disablerules, Id ruleid)
1363 Pool *pool = solv->pool;
1368 policy_filter_unwanted(solv, dq, POLICY_MODE_CHOOSE);
1371 /* XXX: didn't we already do that? */
1372 /* XXX: shouldn't we prefer installed packages? */
1373 /* XXX: move to policy.c? */
1374 /* choose the supplemented one */
1375 for (i = 0; i < dq->count; i++)
1376 if (solver_is_supplementing(solv, pool->solvables + dq->elements[i]))
1378 dq->elements[0] = dq->elements[i];
1385 /* multiple candidates, open a branch */
1386 for (i = 1; i < dq->count; i++)
1387 queue_push(&solv->branches, dq->elements[i]);
1388 queue_push(&solv->branches, -level);
1390 p = dq->elements[0];
1392 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing %s\n", pool_solvid2str(pool, p));
1394 return setpropagatelearn(solv, level, p, disablerules, ruleid);
1398 /********************************************************************/
1399 /* Main solver interface */
1402 /*-------------------------------------------------------------------
1405 * create solver structure
1407 * pool: all available solvables
1408 * installed: installed Solvables
1411 * Upon solving, rules are created to flag the Solvables
1412 * of the 'installed' Repo as installed.
1416 solver_create(Pool *pool)
1419 solv = (Solver *)solv_calloc(1, sizeof(Solver));
1421 solv->installed = pool->installed;
1423 solv->allownamechange = 1;
1425 solv->dup_allowdowngrade = 1;
1426 solv->dup_allownamechange = 1;
1427 solv->dup_allowarchchange = 1;
1428 solv->dup_allowvendorchange = 1;
1430 queue_init(&solv->ruletojob);
1431 queue_init(&solv->decisionq);
1432 queue_init(&solv->decisionq_why);
1433 queue_init(&solv->problems);
1434 queue_init(&solv->orphaned);
1435 queue_init(&solv->learnt_why);
1436 queue_init(&solv->learnt_pool);
1437 queue_init(&solv->branches);
1438 queue_init(&solv->weakruleq);
1439 queue_init(&solv->ruleassertions);
1440 queue_init(&solv->addedmap_deduceq);
1442 queue_push(&solv->learnt_pool, 0); /* so that 0 does not describe a proof */
1444 map_init(&solv->recommendsmap, pool->nsolvables);
1445 map_init(&solv->suggestsmap, pool->nsolvables);
1446 map_init(&solv->noupdate, solv->installed ? solv->installed->end - solv->installed->start : 0);
1447 solv->recommends_index = 0;
1449 solv->decisionmap = (Id *)solv_calloc(pool->nsolvables, sizeof(Id));
1451 solv->rules = solv_extend_resize(solv->rules, solv->nrules, sizeof(Rule), RULES_BLOCK);
1452 memset(solv->rules, 0, sizeof(Rule));
1458 /*-------------------------------------------------------------------
1464 solver_free(Solver *solv)
1466 queue_free(&solv->job);
1467 queue_free(&solv->ruletojob);
1468 queue_free(&solv->decisionq);
1469 queue_free(&solv->decisionq_why);
1470 queue_free(&solv->learnt_why);
1471 queue_free(&solv->learnt_pool);
1472 queue_free(&solv->problems);
1473 queue_free(&solv->solutions);
1474 queue_free(&solv->orphaned);
1475 queue_free(&solv->branches);
1476 queue_free(&solv->weakruleq);
1477 queue_free(&solv->ruleassertions);
1478 queue_free(&solv->addedmap_deduceq);
1479 if (solv->cleandeps_updatepkgs)
1481 queue_free(solv->cleandeps_updatepkgs);
1482 solv->cleandeps_updatepkgs = solv_free(solv->cleandeps_updatepkgs);
1484 if (solv->cleandeps_mistakes)
1486 queue_free(solv->cleandeps_mistakes);
1487 solv->cleandeps_mistakes = solv_free(solv->cleandeps_mistakes);
1489 if (solv->update_targets)
1491 queue_free(solv->update_targets);
1492 solv->update_targets = solv_free(solv->update_targets);
1494 if (solv->installsuppdepq)
1496 queue_free(solv->installsuppdepq);
1497 solv->installsuppdepq = solv_free(solv->installsuppdepq);
1500 map_free(&solv->recommendsmap);
1501 map_free(&solv->suggestsmap);
1502 map_free(&solv->noupdate);
1503 map_free(&solv->weakrulemap);
1504 map_free(&solv->multiversion);
1506 map_free(&solv->updatemap);
1507 map_free(&solv->bestupdatemap);
1508 map_free(&solv->fixmap);
1509 map_free(&solv->dupmap);
1510 map_free(&solv->dupinvolvedmap);
1511 map_free(&solv->droporphanedmap);
1512 map_free(&solv->cleandepsmap);
1514 solv_free(solv->decisionmap);
1515 solv_free(solv->rules);
1516 solv_free(solv->watches);
1517 solv_free(solv->obsoletes);
1518 solv_free(solv->obsoletes_data);
1519 solv_free(solv->multiversionupdaters);
1520 solv_free(solv->choicerules_ref);
1521 solv_free(solv->bestrules_pkg);
1526 solver_get_flag(Solver *solv, int flag)
1530 case SOLVER_FLAG_ALLOW_DOWNGRADE:
1531 return solv->allowdowngrade;
1532 case SOLVER_FLAG_ALLOW_NAMECHANGE:
1533 return solv->allownamechange;
1534 case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1535 return solv->allowarchchange;
1536 case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1537 return solv->allowvendorchange;
1538 case SOLVER_FLAG_ALLOW_UNINSTALL:
1539 return solv->allowuninstall;
1540 case SOLVER_FLAG_NO_UPDATEPROVIDE:
1541 return solv->noupdateprovide;
1542 case SOLVER_FLAG_SPLITPROVIDES:
1543 return solv->dosplitprovides;
1544 case SOLVER_FLAG_IGNORE_RECOMMENDED:
1545 return solv->dontinstallrecommended;
1546 case SOLVER_FLAG_ADD_ALREADY_RECOMMENDED:
1547 return solv->addalreadyrecommended;
1548 case SOLVER_FLAG_NO_INFARCHCHECK:
1549 return solv->noinfarchcheck;
1550 case SOLVER_FLAG_KEEP_EXPLICIT_OBSOLETES:
1551 return solv->keepexplicitobsoletes;
1552 case SOLVER_FLAG_BEST_OBEY_POLICY:
1553 return solv->bestobeypolicy;
1554 case SOLVER_FLAG_NO_AUTOTARGET:
1555 return solv->noautotarget;
1563 solver_set_flag(Solver *solv, int flag, int value)
1565 int old = solver_get_flag(solv, flag);
1568 case SOLVER_FLAG_ALLOW_DOWNGRADE:
1569 solv->allowdowngrade = value;
1571 case SOLVER_FLAG_ALLOW_NAMECHANGE:
1572 solv->allownamechange = value;
1574 case SOLVER_FLAG_ALLOW_ARCHCHANGE:
1575 solv->allowarchchange = value;
1577 case SOLVER_FLAG_ALLOW_VENDORCHANGE:
1578 solv->allowvendorchange = value;
1580 case SOLVER_FLAG_ALLOW_UNINSTALL:
1581 solv->allowuninstall = value;
1583 case SOLVER_FLAG_NO_UPDATEPROVIDE:
1584 solv->noupdateprovide = value;
1586 case SOLVER_FLAG_SPLITPROVIDES:
1587 solv->dosplitprovides = value;
1589 case SOLVER_FLAG_IGNORE_RECOMMENDED:
1590 solv->dontinstallrecommended = value;
1592 case SOLVER_FLAG_ADD_ALREADY_RECOMMENDED:
1593 solv->addalreadyrecommended = value;
1595 case SOLVER_FLAG_NO_INFARCHCHECK:
1596 solv->noinfarchcheck = value;
1598 case SOLVER_FLAG_KEEP_EXPLICIT_OBSOLETES:
1599 solv->keepexplicitobsoletes = value;
1601 case SOLVER_FLAG_BEST_OBEY_POLICY:
1602 solv->bestobeypolicy = value;
1604 case SOLVER_FLAG_NO_AUTOTARGET:
1605 solv->noautotarget = value;
1614 cleandeps_check_mistakes(Solver *solv, int level)
1616 Pool *pool = solv->pool;
1620 int mademistake = 0;
1622 if (!solv->cleandepsmap.size)
1624 /* check for mistakes */
1625 for (i = solv->installed->start; i < solv->installed->end; i++)
1627 if (!MAPTST(&solv->cleandepsmap, i - solv->installed->start))
1629 r = solv->rules + solv->featurerules + (i - solv->installed->start);
1630 /* a mistake is when the featurerule is true but the updaterule is false */
1633 FOR_RULELITERALS(p, pp, r)
1634 if (p > 0 && solv->decisionmap[p] > 0)
1637 continue; /* feature rule is not true */
1638 r = solv->rules + solv->updaterules + (i - solv->installed->start);
1641 FOR_RULELITERALS(p, pp, r)
1642 if (p > 0 && solv->decisionmap[p] > 0)
1645 continue; /* update rule is true */
1646 POOL_DEBUG(SOLV_DEBUG_SOLVER, "cleandeps mistake: ");
1647 solver_printruleclass(solv, SOLV_DEBUG_SOLVER, r);
1648 POOL_DEBUG(SOLV_DEBUG_SOLVER, "feature rule: ");
1649 solver_printruleclass(solv, SOLV_DEBUG_SOLVER, solv->rules + solv->featurerules + (i - solv->installed->start));
1650 if (!solv->cleandeps_mistakes)
1652 solv->cleandeps_mistakes = solv_calloc(1, sizeof(Queue));
1653 queue_init(solv->cleandeps_mistakes);
1655 queue_push(solv->cleandeps_mistakes, i);
1656 MAPCLR(&solv->cleandepsmap, i - solv->installed->start);
1657 solver_reenablepolicyrules_cleandeps(solv, i);
1666 prune_to_update_targets(Solver *solv, Id *cp, Queue *q)
1670 for (i = j = 0; i < q->count; i++)
1673 for (cp2 = cp; *cp2; cp2++)
1676 q->elements[j++] = p;
1680 queue_truncate(q, j);
1683 /*-------------------------------------------------------------------
1687 * all rules have been set up, now actually run the solver
1692 solver_run_sat(Solver *solv, int disablerules, int doweak)
1694 Queue dq; /* local decisionqueue */
1695 Queue dqs; /* local decisionqueue for supplements */
1701 Pool *pool = solv->pool;
1703 int minimizationsteps;
1704 int installedpos = solv->installed ? solv->installed->start : 0;
1706 IF_POOLDEBUG (SOLV_DEBUG_RULE_CREATION)
1708 POOL_DEBUG (SOLV_DEBUG_RULE_CREATION, "number of rules: %d\n", solv->nrules);
1709 for (i = 1; i < solv->nrules; i++)
1710 solver_printruleclass(solv, SOLV_DEBUG_RULE_CREATION, solv->rules + i);
1713 POOL_DEBUG(SOLV_DEBUG_SOLVER, "initial decisions: %d\n", solv->decisionq.count);
1715 /* start SAT algorithm */
1717 systemlevel = level + 1;
1718 POOL_DEBUG(SOLV_DEBUG_SOLVER, "solving...\n");
1724 * here's the main loop:
1725 * 1) propagate new decisions (only needed once)
1727 * 3) try to keep installed packages
1728 * 4) fulfill all unresolved rules
1729 * 5) install recommended packages
1730 * 6) minimalize solution if we had choices
1731 * if we encounter a problem, we rewind to a safe level and restart
1735 minimizationsteps = 0;
1739 * initial propagation of the assertions
1743 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "propagating (propagate_index: %d; size decisionq: %d)...\n", solv->propagate_index, solv->decisionq.count);
1744 if ((r = propagate(solv, level)) != 0)
1746 if (analyze_unsolvable(solv, r, disablerules))
1749 break; /* unsolvable */
1754 * resolve jobs first
1756 if (level < systemlevel)
1758 POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving job rules\n");
1759 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
1762 if (r->d < 0) /* ignore disabled rules */
1765 FOR_RULELITERALS(l, pp, r)
1769 if (solv->decisionmap[-l] <= 0)
1774 if (solv->decisionmap[l] > 0)
1776 if (solv->decisionmap[l] == 0)
1782 /* prune to installed if not updating */
1783 if (dq.count > 1 && solv->installed && !solv->updatemap_all &&
1784 !(solv->job.elements[solv->ruletojob.elements[i - solv->jobrules]] & SOLVER_ORUPDATE))
1787 for (j = k = 0; j < dq.count; j++)
1789 Solvable *s = pool->solvables + dq.elements[j];
1790 if (s->repo == solv->installed)
1792 dq.elements[k++] = dq.elements[j];
1793 if (solv->updatemap.size && MAPTST(&solv->updatemap, dq.elements[j] - solv->installed->start))
1795 k = 0; /* package wants to be updated, do not prune */
1804 level = selectandinstall(solv, level, &dq, disablerules, i);
1807 if (level <= olevel)
1811 break; /* unsolvable */
1812 systemlevel = level + 1;
1813 if (i < solv->jobrules_end)
1815 solv->decisioncnt_update = solv->decisionq.count;
1816 solv->decisioncnt_keep = solv->decisionq.count;
1820 * installed packages
1822 if (level < systemlevel && solv->installed && solv->installed->nsolvables && !solv->installed->disabled)
1824 Repo *installed = solv->installed;
1827 POOL_DEBUG(SOLV_DEBUG_SOLVER, "resolving installed packages\n");
1828 /* we use two passes if we need to update packages
1829 * to create a better user experience */
1830 for (pass = solv->updatemap.size ? 0 : 1; pass < 2; pass++)
1832 int passlevel = level;
1834 solv->decisioncnt_keep = solv->decisionq.count;
1835 /* start with installedpos, the position that gave us problems last time */
1836 for (i = installedpos, n = installed->start; n < installed->end; i++, n++)
1841 if (i == installed->end)
1842 i = installed->start;
1843 s = pool->solvables + i;
1844 if (s->repo != installed)
1847 if (solv->decisionmap[i] > 0)
1849 if (!pass && solv->updatemap.size && !MAPTST(&solv->updatemap, i - installed->start))
1850 continue; /* updates first */
1851 r = solv->rules + solv->updaterules + (i - installed->start);
1853 if (!rr->p || rr->d < 0) /* disabled -> look at feature rule */
1854 rr -= solv->installed->end - solv->installed->start;
1855 if (!rr->p) /* identical to update rule? */
1858 continue; /* orpaned package */
1860 /* XXX: noupdate check is probably no longer needed, as all jobs should
1861 * already be satisfied */
1862 /* Actually we currently still need it because of erase jobs */
1863 /* if noupdate is set we do not look at update candidates */
1865 if (!MAPTST(&solv->noupdate, i - installed->start) && (solv->decisionmap[i] < 0 || solv->updatemap_all || (solv->updatemap.size && MAPTST(&solv->updatemap, i - installed->start)) || rr->p != i))
1867 if (solv->multiversion.size && solv->multiversionupdaters
1868 && (d = solv->multiversionupdaters[i - installed->start]) != 0)
1870 /* special multiversion handling, make sure best version is chosen */
1872 while ((p = pool->whatprovidesdata[d++]) != 0)
1873 if (solv->decisionmap[p] >= 0)
1875 if (dq.count && solv->update_targets && solv->update_targets->elements[i - installed->start])
1876 prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[i - installed->start], &dq);
1879 policy_filter_unwanted(solv, &dq, POLICY_MODE_CHOOSE);
1881 if (p != i && solv->decisionmap[p] == 0)
1883 rr = solv->rules + solv->featurerules + (i - solv->installed->start);
1884 if (!rr->p) /* update rule == feature rule? */
1885 rr = rr - solv->featurerules + solv->updaterules;
1894 /* update to best package */
1895 FOR_RULELITERALS(p, pp, rr)
1897 if (solv->decisionmap[p] > 0)
1899 dq.count = 0; /* already fulfilled */
1902 if (!solv->decisionmap[p])
1907 if (dq.count && solv->update_targets && solv->update_targets->elements[i - installed->start])
1908 prune_to_update_targets(solv, solv->update_targets->elements + solv->update_targets->elements[i - installed->start], &dq);
1909 /* install best version */
1913 level = selectandinstall(solv, level, &dq, disablerules, rr - solv->rules);
1920 if (level <= olevel)
1922 if (level == 1 || level < passlevel)
1923 break; /* trouble */
1925 n = installed->start; /* redo all */
1931 /* if still undecided keep package */
1932 if (solv->decisionmap[i] == 0)
1935 if (solv->cleandepsmap.size && MAPTST(&solv->cleandepsmap, i - installed->start))
1938 POOL_DEBUG(SOLV_DEBUG_POLICY, "cleandeps erasing %s\n", pool_solvid2str(pool, i));
1939 level = setpropagatelearn(solv, level, -i, disablerules, 0);
1946 POOL_DEBUG(SOLV_DEBUG_POLICY, "keeping %s\n", pool_solvid2str(pool, i));
1947 level = setpropagatelearn(solv, level, i, disablerules, r - solv->rules);
1951 if (level <= olevel)
1953 if (level == 1 || level < passlevel)
1954 break; /* trouble */
1956 n = installed->start; /* redo all */
1959 continue; /* retry with learnt rule */
1963 if (n < installed->end)
1965 installedpos = i; /* retry problem solvable next time */
1966 break; /* ran into trouble */
1968 installedpos = installed->start; /* reset installedpos */
1971 break; /* unsolvable */
1972 systemlevel = level + 1;
1974 continue; /* had trouble, retry */
1977 if (level < systemlevel)
1978 systemlevel = level;
1983 solv->decisioncnt_resolve = solv->decisionq.count;
1984 POOL_DEBUG(SOLV_DEBUG_POLICY, "deciding unresolved rules\n");
1985 for (i = 1, n = 1; n < solv->nrules; i++, n++)
1987 if (i == solv->nrules)
1989 r = solv->rules + i;
1990 if (r->d < 0) /* ignore disabled rules */
1995 /* binary or unary rule */
1996 /* need two positive undecided literals */
1997 if (r->p < 0 || r->w2 <= 0)
1999 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
2001 queue_push(&dq, r->p);
2002 queue_push(&dq, r->w2);
2007 * all negative literals are installed
2008 * no positive literal is installed
2009 * i.e. the rule is not fulfilled and we
2010 * just need to decide on the positive literals
2014 if (solv->decisionmap[-r->p] <= 0)
2019 if (solv->decisionmap[r->p] > 0)
2021 if (solv->decisionmap[r->p] == 0)
2022 queue_push(&dq, r->p);
2024 dp = pool->whatprovidesdata + r->d;
2025 while ((p = *dp++) != 0)
2029 if (solv->decisionmap[-p] <= 0)
2034 if (solv->decisionmap[p] > 0)
2036 if (solv->decisionmap[p] == 0)
2043 IF_POOLDEBUG (SOLV_DEBUG_PROPAGATE)
2045 POOL_DEBUG(SOLV_DEBUG_PROPAGATE, "unfulfilled ");
2046 solver_printruleclass(solv, SOLV_DEBUG_PROPAGATE, r);
2048 /* dq.count < 2 cannot happen as this means that
2049 * the rule is unit */
2050 assert(dq.count > 1);
2052 /* prune to cleandeps packages */
2053 if (solv->cleandepsmap.size && solv->installed)
2055 Repo *installed = solv->installed;
2056 for (j = 0; j < dq.count; j++)
2057 if (pool->solvables[dq.elements[j]].repo == installed && MAPTST(&solv->cleandepsmap, dq.elements[j] - installed->start))
2061 dq.elements[0] = dq.elements[j];
2062 queue_truncate(&dq, 1);
2067 level = selectandinstall(solv, level, &dq, disablerules, r - solv->rules);
2069 break; /* unsolvable */
2070 if (level < systemlevel || level == 1)
2071 break; /* trouble */
2072 /* something changed, so look at all rules again */
2076 if (n != solv->nrules) /* ran into trouble? */
2079 break; /* unsolvable */
2080 continue; /* start over */
2083 /* at this point we have a consistent system. now do the extras... */
2085 /* first decide leftover cleandeps packages */
2086 if (solv->cleandepsmap.size && solv->installed)
2088 for (p = solv->installed->start; p < solv->installed->end; p++)
2090 s = pool->solvables + p;
2091 if (s->repo != solv->installed)
2093 if (solv->decisionmap[p] == 0 && MAPTST(&solv->cleandepsmap, p - solv->installed->start))
2095 POOL_DEBUG(SOLV_DEBUG_POLICY, "cleandeps erasing %s\n", pool_solvid2str(pool, p));
2097 level = setpropagatelearn(solv, level, -p, 0, 0);
2102 if (p < solv->installed->end)
2106 solv->decisioncnt_weak = solv->decisionq.count;
2111 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended packages\n");
2112 queue_empty(&dq); /* recommended packages */
2113 queue_empty(&dqs); /* supplemented packages */
2114 for (i = 1; i < pool->nsolvables; i++)
2116 if (solv->decisionmap[i] < 0)
2118 if (solv->decisionmap[i] > 0)
2120 /* installed, check for recommends */
2121 Id *recp, rec, pp, p;
2122 s = pool->solvables + i;
2123 if (!solv->addalreadyrecommended && s->repo == solv->installed)
2125 /* XXX need to special case AND ? */
2128 recp = s->repo->idarraydata + s->recommends;
2129 while ((rec = *recp++) != 0)
2132 FOR_PROVIDES(p, pp, rec)
2134 if (solv->decisionmap[p] > 0)
2139 else if (solv->decisionmap[p] == 0)
2141 if (solv->dupmap_all && solv->installed && pool->solvables[p].repo == solv->installed && (solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))))
2143 queue_pushunique(&dq, p);
2151 s = pool->solvables + i;
2152 if (!s->supplements)
2154 if (!pool_installable(pool, s))
2156 if (!solver_is_supplementing(solv, s))
2158 if (solv->dupmap_all && solv->installed && s->repo == solv->installed && (solv->droporphanedmap_all || (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, i - solv->installed->start))))
2160 queue_push(&dqs, i);
2164 /* filter out all packages obsoleted by installed packages */
2165 /* this is no longer needed if we have reverse obsoletes */
2166 if ((dqs.count || dq.count) && solv->installed)
2169 Id obs, *obsp, po, ppo;
2171 map_init(&obsmap, pool->nsolvables);
2172 for (p = solv->installed->start; p < solv->installed->end; p++)
2174 s = pool->solvables + p;
2175 if (s->repo != solv->installed || !s->obsoletes)
2177 if (solv->decisionmap[p] <= 0)
2179 if (solv->multiversion.size && MAPTST(&solv->multiversion, p))
2181 obsp = s->repo->idarraydata + s->obsoletes;
2182 /* foreach obsoletes */
2183 while ((obs = *obsp++) != 0)
2184 FOR_PROVIDES(po, ppo, obs)
2185 MAPSET(&obsmap, po);
2187 for (i = j = 0; i < dqs.count; i++)
2188 if (!MAPTST(&obsmap, dqs.elements[i]))
2189 dqs.elements[j++] = dqs.elements[i];
2191 for (i = j = 0; i < dq.count; i++)
2192 if (!MAPTST(&obsmap, dq.elements[i]))
2193 dq.elements[j++] = dq.elements[i];
2198 /* filter out all already supplemented packages if requested */
2199 if (!solv->addalreadyrecommended && dqs.count)
2201 int dosplitprovides_old = solv->dosplitprovides;
2202 /* turn off all new packages */
2203 for (i = 0; i < solv->decisionq.count; i++)
2205 p = solv->decisionq.elements[i];
2208 s = pool->solvables + p;
2209 if (s->repo && s->repo != solv->installed)
2210 solv->decisionmap[p] = -solv->decisionmap[p];
2212 solv->dosplitprovides = 0;
2213 /* filter out old supplements */
2214 for (i = j = 0; i < dqs.count; i++)
2216 p = dqs.elements[i];
2217 s = pool->solvables + p;
2218 if (!s->supplements)
2220 if (!solver_is_supplementing(solv, s))
2221 dqs.elements[j++] = p;
2222 else if (solv->installsuppdepq && solver_check_installsuppdepq(solv, s))
2223 dqs.elements[j++] = p;
2226 /* undo turning off */
2227 for (i = 0; i < solv->decisionq.count; i++)
2229 p = solv->decisionq.elements[i];
2232 s = pool->solvables + p;
2233 if (s->repo && s->repo != solv->installed)
2234 solv->decisionmap[p] = -solv->decisionmap[p];
2236 solv->dosplitprovides = dosplitprovides_old;
2239 /* multiversion doesn't mix well with supplements.
2240 * filter supplemented packages where we already decided
2241 * to install a different version (see bnc#501088) */
2242 if (dqs.count && solv->multiversion.size)
2244 for (i = j = 0; i < dqs.count; i++)
2246 p = dqs.elements[i];
2247 if (MAPTST(&solv->multiversion, p))
2250 s = pool->solvables + p;
2251 FOR_PROVIDES(p2, pp2, s->name)
2252 if (solv->decisionmap[p2] > 0 && pool->solvables[p2].name == s->name)
2255 continue; /* ignore this package */
2257 dqs.elements[j++] = p;
2262 /* make dq contain both recommended and supplemented pkgs */
2265 for (i = 0; i < dqs.count; i++)
2266 queue_pushunique(&dq, dqs.elements[i]);
2272 int decisioncount = solv->decisionq.count;
2276 /* simple case, just one package. no need to choose to best version */
2279 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
2281 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2282 level = setpropagatelearn(solv, level, p, 0, 0);
2285 continue; /* back to main loop */
2288 /* filter packages, this gives us the best versions */
2289 policy_filter_unwanted(solv, &dq, POLICY_MODE_RECOMMEND);
2291 /* create map of result */
2292 map_init(&dqmap, pool->nsolvables);
2293 for (i = 0; i < dq.count; i++)
2294 MAPSET(&dqmap, dq.elements[i]);
2296 /* install all supplemented packages */
2297 for (i = 0; i < dqs.count; i++)
2299 p = dqs.elements[i];
2300 if (solv->decisionmap[p] || !MAPTST(&dqmap, p))
2302 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing supplemented %s\n", pool_solvid2str(pool, p));
2304 level = setpropagatelearn(solv, level, p, 0, 0);
2305 if (level <= olevel)
2308 if (i < dqs.count || solv->decisionq.count < decisioncount)
2316 /* install all recommended packages */
2317 /* more work as we want to created branches if multiple
2318 * choices are valid */
2319 for (i = 0; i < decisioncount; i++)
2322 p = solv->decisionq.elements[i];
2325 s = pool->solvables + p;
2326 if (!s->repo || (!solv->addalreadyrecommended && s->repo == solv->installed))
2330 recp = s->repo->idarraydata + s->recommends;
2331 while ((rec = *recp++) != 0)
2334 FOR_PROVIDES(p, pp, rec)
2336 if (solv->decisionmap[p] > 0)
2341 else if (solv->decisionmap[p] == 0 && MAPTST(&dqmap, p))
2342 queue_pushunique(&dq, p);
2348 /* multiple candidates, open a branch */
2349 for (i = 1; i < dq.count; i++)
2350 queue_push(&solv->branches, dq.elements[i]);
2351 queue_push(&solv->branches, -level);
2354 POOL_DEBUG(SOLV_DEBUG_POLICY, "installing recommended %s\n", pool_solvid2str(pool, p));
2356 level = setpropagatelearn(solv, level, p, 0, 0);
2357 if (level <= olevel || solv->decisionq.count < decisioncount)
2358 break; /* we had to revert some decisions */
2361 break; /* had a problem above, quit loop */
2366 continue; /* back to main loop so that all deps are checked */
2370 solv->decisioncnt_orphan = solv->decisionq.count;
2371 if (solv->dupmap_all && solv->installed)
2373 int installedone = 0;
2375 /* let's see if we can install some unsupported package */
2376 POOL_DEBUG(SOLV_DEBUG_SOLVER, "deciding orphaned packages\n");
2377 for (i = 0; i < solv->orphaned.count; i++)
2379 p = solv->orphaned.elements[i];
2380 if (solv->decisionmap[p])
2381 continue; /* already decided */
2382 if (solv->droporphanedmap_all)
2384 if (solv->droporphanedmap.size && MAPTST(&solv->droporphanedmap, p - solv->installed->start))
2386 POOL_DEBUG(SOLV_DEBUG_SOLVER, "keeping orphaned %s\n", pool_solvid2str(pool, p));
2388 level = setpropagatelearn(solv, level, p, 0, 0);
2393 if (installedone || i < solv->orphaned.count)
2397 continue; /* back to main loop */
2399 for (i = 0; i < solv->orphaned.count; i++)
2401 p = solv->orphaned.elements[i];
2402 if (solv->decisionmap[p])
2403 continue; /* already decided */
2404 POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing orphaned %s\n", pool_solvid2str(pool, p));
2406 level = setpropagatelearn(solv, level, -p, 0, 0);
2410 if (i < solv->orphaned.count)
2414 continue; /* back to main loop */
2418 if (solv->installed && solv->cleandepsmap.size)
2420 if (cleandeps_check_mistakes(solv, level))
2422 level = 1; /* restart from scratch */
2423 systemlevel = level + 1;
2428 if (solv->solution_callback)
2430 solv->solution_callback(solv, solv->solution_callback_data);
2431 if (solv->branches.count)
2433 int i = solv->branches.count - 1;
2434 int l = -solv->branches.elements[i];
2438 if (solv->branches.elements[i - 1] < 0)
2440 p = solv->branches.elements[i];
2441 POOL_DEBUG(SOLV_DEBUG_SOLVER, "branching with %s\n", pool_solvid2str(pool, p));
2443 for (j = i + 1; j < solv->branches.count; j++)
2444 queue_push(&dq, solv->branches.elements[j]);
2445 solv->branches.count = i;
2447 revert(solv, level);
2449 for (j = 0; j < dq.count; j++)
2450 queue_push(&solv->branches, dq.elements[j]);
2452 why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2454 level = setpropagatelearn(solv, level, p, disablerules, why);
2459 /* all branches done, we're finally finished */
2463 /* auto-minimization step */
2464 if (solv->branches.count)
2466 int l = 0, lasti = -1, lastl = -1;
2470 for (i = solv->branches.count - 1; i >= 0; i--)
2472 p = solv->branches.elements[i];
2475 else if (p > 0 && solv->decisionmap[p] > l + 1)
2483 /* kill old solvable so that we do not loop */
2484 p = solv->branches.elements[lasti];
2485 solv->branches.elements[lasti] = 0;
2486 POOL_DEBUG(SOLV_DEBUG_SOLVER, "minimizing %d -> %d with %s\n", solv->decisionmap[p], lastl, pool_solvid2str(pool, p));
2487 minimizationsteps++;
2490 revert(solv, level);
2491 why = -solv->decisionq_why.elements[solv->decisionq_why.count];
2494 level = setpropagatelearn(solv, level, p, disablerules, why);
2497 continue; /* back to main loop */
2500 /* no minimization found, we're finally finished! */
2504 POOL_DEBUG(SOLV_DEBUG_STATS, "solver statistics: %d learned rules, %d unsolvable, %d minimization steps\n", solv->stats_learned, solv->stats_unsolvable, minimizationsteps);
2506 POOL_DEBUG(SOLV_DEBUG_STATS, "done solving.\n\n");
2512 solv->decisioncnt_update = solv->decisionq.count;
2513 solv->decisioncnt_keep = solv->decisionq.count;
2514 solv->decisioncnt_resolve = solv->decisionq.count;
2515 solv->decisioncnt_weak = solv->decisionq.count;
2516 solv->decisioncnt_orphan = solv->decisionq.count;
2519 solver_printdecisionq(solv, SOLV_DEBUG_RESULT);
2524 /*-------------------------------------------------------------------
2526 * remove disabled conflicts
2528 * purpose: update the decisionmap after some rules were disabled.
2529 * this is used to calculate the suggested/recommended package list.
2530 * Also returns a "removed" list to undo the discisionmap changes.
2534 removedisabledconflicts(Solver *solv, Queue *removed)
2536 Pool *pool = solv->pool;
2541 Id *decisionmap = solv->decisionmap;
2543 queue_empty(removed);
2544 for (i = 0; i < solv->decisionq.count; i++)
2546 p = solv->decisionq.elements[i];
2548 continue; /* conflicts only, please */
2549 why = solv->decisionq_why.elements[i];
2552 /* no rule involved, must be a orphan package drop */
2555 /* we never do conflicts on free decisions, so there
2556 * must have been an unit rule */
2558 r = solv->rules + why;
2559 if (r->d < 0 && decisionmap[-p])
2561 /* rule is now disabled, remove from decisionmap */
2562 POOL_DEBUG(SOLV_DEBUG_SOLVER, "removing conflict for package %s[%d]\n", pool_solvid2str(pool, -p), -p);
2563 queue_push(removed, -p);
2564 queue_push(removed, decisionmap[-p]);
2565 decisionmap[-p] = 0;
2568 if (!removed->count)
2570 /* we removed some confliced packages. some of them might still
2571 * be in conflict, so search for unit rules and re-conflict */
2573 for (i = n = 1, r = solv->rules + i; n < solv->nrules; i++, r++, n++)
2575 if (i == solv->nrules)
2578 r = solv->rules + i;
2584 if (r->p < 0 && !decisionmap[-r->p])
2590 if (r->p < 0 && decisionmap[-r->p] == 0 && DECISIONMAP_FALSE(r->w2))
2592 else if (r->w2 < 0 && decisionmap[-r->w2] == 0 && DECISIONMAP_FALSE(r->p))
2597 if (r->p < 0 && decisionmap[-r->p] == 0)
2599 if (new || DECISIONMAP_FALSE(r->p))
2601 dp = pool->whatprovidesdata + r->d;
2602 while ((p = *dp++) != 0)
2604 if (new && p == new)
2606 if (p < 0 && decisionmap[-p] == 0)
2615 else if (!DECISIONMAP_FALSE(p))
2625 POOL_DEBUG(SOLV_DEBUG_SOLVER, "re-conflicting package %s[%d]\n", pool_solvid2str(pool, -new), -new);
2626 decisionmap[-new] = -1;
2628 n = 0; /* redo all rules */
2634 undo_removedisabledconflicts(Solver *solv, Queue *removed)
2637 for (i = 0; i < removed->count; i += 2)
2638 solv->decisionmap[removed->elements[i]] = removed->elements[i + 1];
2642 /*-------------------------------------------------------------------
2644 * weaken solvable dependencies
2648 weaken_solvable_deps(Solver *solv, Id p)
2653 for (i = 1, r = solv->rules + i; i < solv->rpmrules_end; i++, r++)
2657 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2658 continue; /* conflict */
2659 queue_push(&solv->weakruleq, i);
2664 /********************************************************************/
2669 solver_calculate_multiversionmap(Pool *pool, Queue *job, Map *multiversionmap)
2672 Id how, what, select;
2674 for (i = 0; i < job->count; i += 2)
2676 how = job->elements[i];
2677 if ((how & SOLVER_JOBMASK) != SOLVER_MULTIVERSION)
2679 what = job->elements[i + 1];
2680 select = how & SOLVER_SELECTMASK;
2681 if (!multiversionmap->size)
2682 map_grow(multiversionmap, pool->nsolvables);
2683 if (select == SOLVER_SOLVABLE_ALL)
2685 FOR_POOL_SOLVABLES(p)
2686 MAPSET(multiversionmap, p);
2688 else if (select == SOLVER_SOLVABLE_REPO)
2691 Repo *repo = pool_id2repo(pool, what);
2693 FOR_REPO_SOLVABLES(repo, p, s)
2694 MAPSET(multiversionmap, p);
2696 FOR_JOB_SELECT(p, pp, select, what)
2697 MAPSET(multiversionmap, p);
2702 solver_calculate_noobsmap(Pool *pool, Queue *job, Map *multiversionmap)
2704 solver_calculate_multiversionmap(pool, job, multiversionmap);
2708 * add a rule created by a job, record job number and weak flag
2711 solver_addjobrule(Solver *solv, Id p, Id d, Id job, int weak)
2713 solver_addrule(solv, p, d);
2714 queue_push(&solv->ruletojob, job);
2716 queue_push(&solv->weakruleq, solv->nrules - 1);
2720 add_cleandeps_package(Solver *solv, Id p)
2722 if (!solv->cleandeps_updatepkgs)
2724 solv->cleandeps_updatepkgs = solv_calloc(1, sizeof(Queue));
2725 queue_init(solv->cleandeps_updatepkgs);
2727 queue_pushunique(solv->cleandeps_updatepkgs, p);
2731 add_update_target(Solver *solv, Id p, Id how)
2733 Pool *pool = solv->pool;
2734 Solvable *s = pool->solvables + p;
2735 Repo *installed = solv->installed;
2737 if (!solv->update_targets)
2739 solv->update_targets = solv_calloc(1, sizeof(Queue));
2740 queue_init(solv->update_targets);
2742 if (s->repo == installed)
2744 queue_push2(solv->update_targets, p, p);
2747 FOR_PROVIDES(pi, pip, s->name)
2749 Solvable *si = pool->solvables + pi;
2750 if (si->repo != installed || si->name != s->name)
2752 if (how & SOLVER_FORCEBEST)
2754 if (!solv->bestupdatemap.size)
2755 map_grow(&solv->bestupdatemap, installed->end - installed->start);
2756 MAPSET(&solv->bestupdatemap, pi - installed->start);
2758 if (how & SOLVER_CLEANDEPS)
2759 add_cleandeps_package(solv, pi);
2760 queue_push2(solv->update_targets, pi, p);
2761 /* check if it's ok to keep the installed package */
2762 if (s->evr == si->evr && solvable_identical(s, si))
2763 queue_push2(solv->update_targets, pi, pi);
2767 Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
2768 while ((obs = *obsp++) != 0)
2770 FOR_PROVIDES(pi, pip, obs)
2772 Solvable *si = pool->solvables + pi;
2773 if (si->repo != installed)
2775 if (si->name == s->name)
2776 continue; /* already handled above */
2777 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, si, obs))
2779 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, si))
2781 if (how & SOLVER_FORCEBEST)
2783 if (!solv->bestupdatemap.size)
2784 map_grow(&solv->bestupdatemap, installed->end - installed->start);
2785 MAPSET(&solv->bestupdatemap, pi - installed->start);
2787 if (how & SOLVER_CLEANDEPS)
2788 add_cleandeps_package(solv, pi);
2789 queue_push2(solv->update_targets, pi, p);
2796 transform_update_targets_sortfn(const void *ap, const void *bp, void *dp)
2806 transform_update_targets(Solver *solv)
2808 Repo *installed = solv->installed;
2809 Queue *update_targets = solv->update_targets;
2811 Id p, q, lastp, lastq;
2813 if (!update_targets->count)
2815 queue_free(update_targets);
2816 solv->update_targets = solv_free(update_targets);
2819 if (update_targets->count > 2)
2820 solv_sort(update_targets->elements, update_targets->count >> 1, 2 * sizeof(Id), transform_update_targets_sortfn, solv);
2821 queue_insertn(update_targets, 0, installed->end - installed->start);
2823 for (i = j = installed->end - installed->start; i < update_targets->count; i += 2)
2825 if ((p = update_targets->elements[i]) != lastp)
2827 if (!solv->updatemap.size)
2828 map_grow(&solv->updatemap, installed->end - installed->start);
2829 MAPSET(&solv->updatemap, p - installed->start);
2830 update_targets->elements[j++] = 0; /* finish old set */
2831 update_targets->elements[p - installed->start] = j; /* start new set */
2835 if ((q = update_targets->elements[i + 1]) != lastq)
2837 update_targets->elements[j++] = q;
2841 queue_truncate(update_targets, j);
2842 queue_push(update_targets, 0); /* finish last set */
2847 addedmap2deduceq(Solver *solv, Map *addedmap)
2849 Pool *pool = solv->pool;
2854 queue_empty(&solv->addedmap_deduceq);
2855 for (i = 2, j = solv->rpmrules_end - 1; i < pool->nsolvables && j > 0; j--)
2857 r = solv->rules + j;
2860 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2863 if (!MAPTST(addedmap, p))
2865 /* should never happen, but... */
2866 if (!solv->addedmap_deduceq.count || solv->addedmap_deduceq.elements[solv->addedmap_deduceq.count - 1] != -p)
2867 queue_push(&solv->addedmap_deduceq, -p);
2871 if (MAPTST(addedmap, i))
2872 queue_push(&solv->addedmap_deduceq, i);
2876 for (; i < pool->nsolvables; i++)
2877 if (MAPTST(addedmap, i))
2878 queue_push(&solv->addedmap_deduceq, i);
2880 for (i = 2; i < pool->nsolvables; i++)
2881 if (MAPTST(addedmap, i))
2886 deduceq2addedmap(Solver *solv, Map *addedmap)
2891 for (j = solv->rpmrules_end - 1; j > 0; j--)
2893 r = solv->rules + j;
2894 if (r->d < 0 && r->p)
2895 solver_enablerule(solv, r);
2898 if ((r->d == 0 || r->d == -1) && r->w2 < 0)
2901 MAPSET(addedmap, p);
2903 for (j = 0; j < solv->addedmap_deduceq.count; j++)
2905 p = solv->addedmap_deduceq.elements[j];
2907 MAPSET(addedmap, p);
2909 MAPCLR(addedmap, p);
2921 solver_solve(Solver *solv, Queue *job)
2923 Pool *pool = solv->pool;
2924 Repo *installed = solv->installed;
2926 int oldnrules, initialnrules;
2927 Map addedmap; /* '1' == have rpm-rules for solvable */
2928 Map installcandidatemap;
2929 Id how, what, select, name, weak, p, pp, d;
2933 int now, solve_start;
2935 int hasbestinstalljob = 0;
2937 solve_start = solv_timems(0);
2939 /* log solver options */
2940 POOL_DEBUG(SOLV_DEBUG_STATS, "solver started\n");
2941 POOL_DEBUG(SOLV_DEBUG_STATS, "dosplitprovides=%d, noupdateprovide=%d, noinfarchcheck=%d\n", solv->dosplitprovides, solv->noupdateprovide, solv->noinfarchcheck);
2942 POOL_DEBUG(SOLV_DEBUG_STATS, "allowuninstall=%d, allowdowngrade=%d, allownamechange=%d, allowarchchange=%d, allowvendorchange=%d\n", solv->allowuninstall, solv->allowdowngrade, solv->allownamechange, solv->allowarchchange, solv->allowvendorchange);
2943 POOL_DEBUG(SOLV_DEBUG_STATS, "promoteepoch=%d, forbidselfconflicts=%d\n", pool->promoteepoch, pool->forbidselfconflicts);
2944 POOL_DEBUG(SOLV_DEBUG_STATS, "obsoleteusesprovides=%d, implicitobsoleteusesprovides=%d, obsoleteusescolors=%d\n", pool->obsoleteusesprovides, pool->implicitobsoleteusesprovides, pool->obsoleteusescolors);
2945 POOL_DEBUG(SOLV_DEBUG_STATS, "dontinstallrecommended=%d, addalreadyrecommended=%d\n", solv->dontinstallrecommended, solv->addalreadyrecommended);
2947 /* create whatprovides if not already there */
2948 if (!pool->whatprovides)
2949 pool_createwhatprovides(pool);
2951 /* create obsolete index */
2952 policy_create_obsolete_index(solv);
2955 queue_free(&solv->job);
2956 queue_init_clone(&solv->job, job);
2957 solv->pooljobcnt = pool->pooljobs.count;
2958 if (pool->pooljobs.count)
2960 queue_insertn(&solv->job, 0, pool->pooljobs.count);
2961 memcpy(solv->job.elements, pool->pooljobs.elements, pool->pooljobs.count * sizeof(Id));
2965 /* free old stuff */
2966 if (solv->update_targets)
2968 queue_free(solv->update_targets);
2969 solv->update_targets = solv_free(solv->update_targets);
2971 if (solv->cleandeps_updatepkgs)
2973 queue_free(solv->cleandeps_updatepkgs);
2974 solv->cleandeps_updatepkgs = solv_free(solv->cleandeps_updatepkgs);
2976 queue_empty(&solv->ruleassertions);
2977 solv->bestrules_pkg = solv_free(solv->bestrules_pkg);
2978 solv->choicerules_ref = solv_free(solv->choicerules_ref);
2979 if (solv->noupdate.size)
2980 map_empty(&solv->noupdate);
2981 if (solv->multiversion.size)
2983 map_free(&solv->multiversion);
2984 map_init(&solv->multiversion, 0);
2986 solv->updatemap_all = 0;
2987 if (solv->updatemap.size)
2989 map_free(&solv->updatemap);
2990 map_init(&solv->updatemap, 0);
2992 solv->bestupdatemap_all = 0;
2993 if (solv->bestupdatemap.size)
2995 map_free(&solv->bestupdatemap);
2996 map_init(&solv->bestupdatemap, 0);
2998 solv->fixmap_all = 0;
2999 if (solv->fixmap.size)
3001 map_free(&solv->fixmap);
3002 map_init(&solv->fixmap, 0);
3004 solv->dupmap_all = 0;
3005 if (solv->dupmap.size)
3007 map_free(&solv->dupmap);
3008 map_init(&solv->dupmap, 0);
3010 if (solv->dupinvolvedmap.size)
3012 map_free(&solv->dupinvolvedmap);
3013 map_init(&solv->dupinvolvedmap, 0);
3015 solv->droporphanedmap_all = 0;
3016 if (solv->droporphanedmap.size)
3018 map_free(&solv->droporphanedmap);
3019 map_init(&solv->droporphanedmap, 0);
3021 if (solv->cleandepsmap.size)
3023 map_free(&solv->cleandepsmap);
3024 map_init(&solv->cleandepsmap, 0);
3027 queue_empty(&solv->weakruleq);
3028 solv->watches = solv_free(solv->watches);
3029 queue_empty(&solv->ruletojob);
3030 if (solv->decisionq.count)
3031 memset(solv->decisionmap, 0, pool->nsolvables * sizeof(Id));
3032 queue_empty(&solv->decisionq);
3033 queue_empty(&solv->decisionq_why);
3034 solv->decisioncnt_update = solv->decisioncnt_keep = solv->decisioncnt_resolve = solv->decisioncnt_weak = solv->decisioncnt_orphan = 0;
3035 queue_empty(&solv->learnt_why);
3036 queue_empty(&solv->learnt_pool);
3037 queue_empty(&solv->branches);
3038 solv->propagate_index = 0;
3039 queue_empty(&solv->problems);
3040 queue_empty(&solv->solutions);
3041 queue_empty(&solv->orphaned);
3042 solv->stats_learned = solv->stats_unsolvable = 0;
3043 if (solv->recommends_index)
3045 map_empty(&solv->recommendsmap);
3046 map_empty(&solv->suggestsmap);
3047 solv->recommends_index = 0;
3049 solv->multiversionupdaters = solv_free(solv->multiversionupdaters);
3053 * create basic rule set of all involved packages
3054 * use addedmap bitmap to make sure we don't create rules twice
3057 /* create multiversion map if needed */
3058 solver_calculate_multiversionmap(pool, job, &solv->multiversion);
3060 map_init(&addedmap, pool->nsolvables);
3061 MAPSET(&addedmap, SYSTEMSOLVABLE);
3063 map_init(&installcandidatemap, pool->nsolvables);
3066 now = solv_timems(0);
3068 * create rules for all package that could be involved with the solving
3069 * so called: rpm rules
3072 initialnrules = solv->rpmrules_end ? solv->rpmrules_end : 1;
3073 if (initialnrules > 1)
3074 deduceq2addedmap(solv, &addedmap);
3075 if (solv->nrules != initialnrules)
3076 solver_shrinkrules(solv, initialnrules);
3077 solv->nrules = initialnrules;
3078 solv->rpmrules_end = 0;
3082 /* check for update/verify jobs as they need to be known early */
3083 for (i = 0; i < job->count; i += 2)
3085 how = job->elements[i];
3086 what = job->elements[i + 1];
3087 select = how & SOLVER_SELECTMASK;
3088 switch (how & SOLVER_JOBMASK)
3091 if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && installed && what == installed->repoid))
3092 solv->fixmap_all = 1;
3093 FOR_JOB_SELECT(p, pp, select, what)
3095 s = pool->solvables + p;
3096 if (s->repo != installed)
3098 if (!solv->fixmap.size)
3099 map_grow(&solv->fixmap, installed->end - installed->start);
3100 MAPSET(&solv->fixmap, p - installed->start);
3104 if (select == SOLVER_SOLVABLE_ALL)
3106 solv->updatemap_all = 1;
3107 if (how & SOLVER_FORCEBEST)
3108 solv->bestupdatemap_all = 1;
3109 if (how & SOLVER_CLEANDEPS)
3111 FOR_REPO_SOLVABLES(installed, p, s)
3112 add_cleandeps_package(solv, p);
3115 else if (select == SOLVER_SOLVABLE_REPO)
3117 Repo *repo = pool_id2repo(pool, what);
3120 if (repo == installed && !(how & SOLVER_TARGETED))
3122 solv->updatemap_all = 1;
3123 if (how & SOLVER_FORCEBEST)
3124 solv->bestupdatemap_all = 1;
3125 if (how & SOLVER_CLEANDEPS)
3127 FOR_REPO_SOLVABLES(installed, p, s)
3128 add_cleandeps_package(solv, p);
3132 if (solv->noautotarget && !(how & SOLVER_TARGETED))
3134 /* targeted update */
3135 FOR_REPO_SOLVABLES(repo, p, s)
3136 add_update_target(solv, p, how);
3140 if (!(how & SOLVER_TARGETED))
3143 FOR_JOB_SELECT(p, pp, select, what)
3145 s = pool->solvables + p;
3146 if (s->repo != installed)
3148 if (!solv->updatemap.size)
3149 map_grow(&solv->updatemap, installed->end - installed->start);
3150 MAPSET(&solv->updatemap, p - installed->start);
3151 if (how & SOLVER_FORCEBEST)
3153 if (!solv->bestupdatemap.size)
3154 map_grow(&solv->bestupdatemap, installed->end - installed->start);
3155 MAPSET(&solv->bestupdatemap, p - installed->start);
3157 if (how & SOLVER_CLEANDEPS)
3158 add_cleandeps_package(solv, p);
3161 if (!targeted || solv->noautotarget)
3164 FOR_JOB_SELECT(p, pp, select, what)
3165 add_update_target(solv, p, how);
3173 if (solv->update_targets)
3174 transform_update_targets(solv);
3176 oldnrules = solv->nrules;
3177 FOR_REPO_SOLVABLES(installed, p, s)
3178 solver_addrpmrulesforsolvable(solv, s, &addedmap);
3179 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for installed solvables\n", solv->nrules - oldnrules);
3180 oldnrules = solv->nrules;
3181 FOR_REPO_SOLVABLES(installed, p, s)
3182 solver_addrpmrulesforupdaters(solv, s, &addedmap, 1);
3183 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for updaters of installed solvables\n", solv->nrules - oldnrules);
3187 * create rules for all packages involved in the job
3188 * (to be installed or removed)
3191 oldnrules = solv->nrules;
3192 for (i = 0; i < job->count; i += 2)
3194 how = job->elements[i];
3195 what = job->elements[i + 1];
3196 select = how & SOLVER_SELECTMASK;
3198 switch (how & SOLVER_JOBMASK)
3200 case SOLVER_INSTALL:
3201 FOR_JOB_SELECT(p, pp, select, what)
3203 MAPSET(&installcandidatemap, p);
3204 solver_addrpmrulesforsolvable(solv, pool->solvables + p, &addedmap);
3207 case SOLVER_DISTUPGRADE:
3208 if (select == SOLVER_SOLVABLE_ALL)
3210 solv->dupmap_all = 1;
3211 solv->updatemap_all = 1;
3212 if (how & SOLVER_FORCEBEST)
3213 solv->bestupdatemap_all = 1;
3215 if (!solv->dupmap_all)
3222 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules for packages involved in a job\n", solv->nrules - oldnrules);
3226 * add rules for suggests, enhances
3228 oldnrules = solv->nrules;
3229 solver_addrpmrulesforweak(solv, &addedmap);
3230 POOL_DEBUG(SOLV_DEBUG_STATS, "added %d rpm rules because of weak dependencies\n", solv->nrules - oldnrules);
3233 * first pass done, we now have all the rpm rules we need.
3234 * unify existing rules before going over all job rules and
3236 * at this point the system is always solvable,
3237 * as an empty system (remove all packages) is a valid solution
3240 IF_POOLDEBUG (SOLV_DEBUG_STATS)
3242 int possible = 0, installable = 0;
3243 for (i = 1; i < pool->nsolvables; i++)
3245 if (pool_installable(pool, pool->solvables + i))
3247 if (MAPTST(&addedmap, i))
3250 POOL_DEBUG(SOLV_DEBUG_STATS, "%d of %d installable solvables considered for solving\n", possible, installable);
3253 if (solv->nrules > initialnrules)
3254 solver_unifyrules(solv); /* remove duplicate rpm rules */
3255 solv->rpmrules_end = solv->nrules; /* mark end of rpm rules */
3257 if (solv->nrules > initialnrules)
3258 addedmap2deduceq(solv, &addedmap); /* so that we can recreate the addedmap */
3260 POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule memory used: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
3261 POOL_DEBUG(SOLV_DEBUG_STATS, "rpm rule creation took %d ms\n", solv_timems(now));
3263 /* create dup maps if needed. We need the maps early to create our
3266 solver_createdupmaps(solv);
3269 * create feature rules
3271 * foreach installed:
3272 * create assertion (keep installed, if no update available)
3274 * create update rule (A|update1(A)|update2(A)|...)
3276 * those are used later on to keep a version of the installed packages in
3280 solv->featurerules = solv->nrules; /* mark start of feature rules */
3283 /* foreach possibly installed solvable */
3284 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3286 if (s->repo != installed)
3288 solver_addrule(solv, 0, 0); /* create dummy rule */
3291 solver_addupdaterule(solv, s, 1); /* allow s to be updated */
3293 /* make sure we accounted for all rules */
3294 assert(solv->nrules - solv->featurerules == installed->end - installed->start);
3296 solv->featurerules_end = solv->nrules;
3299 * Add update rules for installed solvables
3301 * almost identical to feature rules
3302 * except that downgrades/archchanges/vendorchanges are not allowed
3305 solv->updaterules = solv->nrules;
3308 { /* foreach installed solvables */
3309 /* we create all update rules, but disable some later on depending on the job */
3310 for (i = installed->start, s = pool->solvables + i; i < installed->end; i++, s++)
3314 if (s->repo != installed)
3316 solver_addrule(solv, 0, 0); /* create dummy rule */
3319 solver_addupdaterule(solv, s, 0); /* allowall = 0: downgrades not allowed */
3321 * check for and remove duplicate
3323 r = solv->rules + solv->nrules - 1; /* r: update rule */
3324 sr = r - (installed->end - installed->start); /* sr: feature rule */
3325 /* it's orphaned if there is no feature rule or the feature rule
3326 * consists just of the installed package */
3327 if (!sr->p || (sr->p == i && !sr->d && !sr->w2))
3328 queue_push(&solv->orphaned, i);
3331 assert(solv->dupmap_all && !sr->p);
3334 if (!solver_rulecmp(solv, r, sr))
3335 memset(sr, 0, sizeof(*sr)); /* delete unneeded feature rule */
3337 solver_disablerule(solv, sr); /* disable feature rule */
3339 /* consistency check: we added a rule for _every_ installed solvable */
3340 assert(solv->nrules - solv->updaterules == installed->end - installed->start);
3342 solv->updaterules_end = solv->nrules;
3346 * now add all job rules
3349 solv->jobrules = solv->nrules;
3350 for (i = 0; i < job->count; i += 2)
3352 oldnrules = solv->nrules;
3354 if (i && i == solv->pooljobcnt)
3355 POOL_DEBUG(SOLV_DEBUG_JOB, "end of pool jobs\n");
3356 how = job->elements[i];
3357 what = job->elements[i + 1];
3358 weak = how & SOLVER_WEAK;
3359 select = how & SOLVER_SELECTMASK;
3360 switch (how & SOLVER_JOBMASK)
3362 case SOLVER_INSTALL:
3363 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sinstall %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3364 if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && installed)
3365 map_grow(&solv->cleandepsmap, installed->end - installed->start);
3366 if (select == SOLVER_SOLVABLE)
3374 FOR_JOB_SELECT(p, pp, select, what)
3378 /* no candidate found, make this an impossible rule */
3379 queue_push(&q, -SYSTEMSOLVABLE);
3381 p = queue_shift(&q); /* get first candidate */
3382 d = !q.count ? 0 : pool_queuetowhatprovides(pool, &q); /* internalize */
3384 /* force install of namespace supplements hack */
3385 if (select == SOLVER_SOLVABLE_PROVIDES && !d && (p == SYSTEMSOLVABLE || p == -SYSTEMSOLVABLE) && ISRELDEP(what))
3387 Reldep *rd = GETRELDEP(pool, what);
3388 if (rd->flags == REL_NAMESPACE)
3391 if (!solv->installsuppdepq)
3393 solv->installsuppdepq = solv_calloc(1, sizeof(Queue));
3394 queue_init(solv->installsuppdepq);
3396 queue_pushunique(solv->installsuppdepq, rd->evr == 0 ? rd->name : what);
3399 solver_addjobrule(solv, p, d, i, weak);
3400 if (how & SOLVER_FORCEBEST)
3401 hasbestinstalljob = 1;
3404 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %s%serase %s\n", weak ? "weak " : "", how & SOLVER_CLEANDEPS ? "clean deps " : "", solver_select2str(pool, select, what));
3405 if ((how & SOLVER_CLEANDEPS) != 0 && !solv->cleandepsmap.size && installed)
3406 map_grow(&solv->cleandepsmap, installed->end - installed->start);
3407 /* specific solvable: by id or by nevra */
3408 name = (select == SOLVER_SOLVABLE || (select == SOLVER_SOLVABLE_NAME && ISRELDEP(what))) ? 0 : -1;
3409 if (select == SOLVER_SOLVABLE_ALL) /* hmmm ;) */
3411 FOR_POOL_SOLVABLES(p)
3412 solver_addjobrule(solv, -p, 0, i, weak);
3414 else if (select == SOLVER_SOLVABLE_REPO)
3416 Repo *repo = pool_id2repo(pool, what);
3418 FOR_REPO_SOLVABLES(repo, p, s)
3419 solver_addjobrule(solv, -p, 0, i, weak);
3421 FOR_JOB_SELECT(p, pp, select, what)
3423 s = pool->solvables + p;
3424 if (installed && s->repo == installed)
3425 name = !name ? s->name : -1;
3426 solver_addjobrule(solv, -p, 0, i, weak);
3428 /* special case for "erase a specific solvable": we also
3429 * erase all other solvables with that name, so that they
3430 * don't get picked up as replacement.
3431 * name is > 0 if exactly one installed solvable matched.
3433 /* XXX: look also at packages that obsolete this package? */
3438 FOR_PROVIDES(p, pp, name)
3440 s = pool->solvables + p;
3441 if (s->name != name)
3443 /* keep other versions installed */
3444 if (s->repo == installed)
3446 /* keep installcandidates of other jobs */
3447 if (MAPTST(&installcandidatemap, p))
3449 /* don't add the same rule twice */
3450 for (j = oldnrules; j < k; j++)
3451 if (solv->rules[j].p == -p)
3454 solver_addjobrule(solv, -p, 0, i, weak); /* remove by id */
3460 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %supdate %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3463 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sverify %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3465 case SOLVER_WEAKENDEPS:
3466 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %sweaken deps %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3467 if (select != SOLVER_SOLVABLE)
3469 s = pool->solvables + what;
3470 weaken_solvable_deps(solv, what);
3472 case SOLVER_MULTIVERSION:
3473 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %smultiversion %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3476 POOL_DEBUG(SOLV_DEBUG_JOB, "job: %slock %s\n", weak ? "weak " : "", solver_select2str(pool, select, what));
3477 if (select == SOLVER_SOLVABLE_ALL)
3479 FOR_POOL_SOLVABLES(p)
3480 solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3482 else if (select == SOLVER_SOLVABLE_REPO)
3484 Repo *repo = pool_id2repo(pool, what);
3486 FOR_REPO_SOLVABLES(repo, p, s)
3487 solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3489 FOR_JOB_SELECT(p, pp, select, what)
3490 solver_addjobrule(solv, installed && pool->solvables[p].repo == installed ? p : -p, 0, i, weak);
3492 case SOLVER_DISTUPGRADE:
3493 POOL_DEBUG(SOLV_DEBUG_JOB, "job: distupgrade %s\n", solver_select2str(pool, select, what));
3495 case SOLVER_DROP_ORPHANED:
3496 POOL_DEBUG(SOLV_DEBUG_JOB, "job: drop orphaned %s\n", solver_select2str(pool, select, what));
3497 if (select == SOLVER_SOLVABLE_ALL || (select == SOLVER_SOLVABLE_REPO && installed && what == installed->repoid))
3498 solv->droporphanedmap_all = 1;
3499 FOR_JOB_SELECT(p, pp, select, what)
3501 s = pool->solvables + p;
3502 if (!installed || s->repo != installed)
3504 if (!solv->droporphanedmap.size)
3505 map_grow(&solv->droporphanedmap, installed->end - installed->start);
3506 MAPSET(&solv->droporphanedmap, p - installed->start);
3509 case SOLVER_USERINSTALLED:
3510 POOL_DEBUG(SOLV_DEBUG_JOB, "job: user installed %s\n", solver_select2str(pool, select, what));
3513 POOL_DEBUG(SOLV_DEBUG_JOB, "job: unknown job\n");
3521 IF_POOLDEBUG (SOLV_DEBUG_JOB)
3524 if (solv->nrules == oldnrules)
3525 POOL_DEBUG(SOLV_DEBUG_JOB, " - no rule created\n");
3526 for (j = oldnrules; j < solv->nrules; j++)
3528 POOL_DEBUG(SOLV_DEBUG_JOB, " - job ");
3529 solver_printrule(solv, SOLV_DEBUG_JOB, solv->rules + j);
3533 assert(solv->ruletojob.count == solv->nrules - solv->jobrules);
3534 solv->jobrules_end = solv->nrules;
3536 /* now create infarch and dup rules */
3537 if (!solv->noinfarchcheck)
3539 solver_addinfarchrules(solv, &addedmap);
3540 if (pool->obsoleteusescolors)
3542 /* currently doesn't work well with infarch rules, so make
3544 for (i = solv->infarchrules; i < solv->infarchrules_end; i++)
3545 queue_push(&solv->weakruleq, i);
3549 solv->infarchrules = solv->infarchrules_end = solv->nrules;
3552 solver_addduprules(solv, &addedmap);
3554 solv->duprules = solv->duprules_end = solv->nrules;
3556 if (solv->bestupdatemap_all || solv->bestupdatemap.size || hasbestinstalljob)
3557 solver_addbestrules(solv, hasbestinstalljob);
3559 solv->bestrules = solv->bestrules_end = solv->nrules;
3562 solver_freedupmaps(solv); /* no longer needed */
3565 solver_addchoicerules(solv);
3567 solv->choicerules = solv->choicerules_end = solv->nrules;
3571 for (i = solv->featurerules; i < solv->nrules; i++)
3572 solver_printruleclass(solv, SOLV_DEBUG_RESULT, solv->rules + i);
3574 /* all rules created
3575 * --------------------------------------------------------------
3576 * prepare for solving
3579 /* free unneeded memory */
3580 map_free(&addedmap);
3581 map_free(&installcandidatemap);
3584 POOL_DEBUG(SOLV_DEBUG_STATS, "%d rpm rules, 2 * %d update rules, %d job rules, %d infarch rules, %d dup rules, %d choice rules, %d best rules\n", solv->rpmrules_end - 1, solv->updaterules_end - solv->updaterules, solv->jobrules_end - solv->jobrules, solv->infarchrules_end - solv->infarchrules, solv->duprules_end - solv->duprules, solv->choicerules_end - solv->choicerules, solv->bestrules_end - solv->bestrules);
3585 POOL_DEBUG(SOLV_DEBUG_STATS, "overall rule memory used: %d K\n", solv->nrules * (int)sizeof(Rule) / 1024);
3587 /* create weak map */
3588 map_init(&solv->weakrulemap, solv->nrules);
3589 for (i = 0; i < solv->weakruleq.count; i++)
3591 p = solv->weakruleq.elements[i];
3592 MAPSET(&solv->weakrulemap, p);
3595 /* enable cleandepsmap creation if we have updatepkgs */
3596 if (solv->cleandeps_updatepkgs && !solv->cleandepsmap.size)
3597 map_grow(&solv->cleandepsmap, installed->end - installed->start);
3599 if (solv->cleandeps_mistakes)
3601 queue_free(solv->cleandeps_mistakes);
3602 solv->cleandeps_mistakes = solv_free(solv->cleandeps_mistakes);
3605 /* all new rules are learnt after this point */
3606 solv->learntrules = solv->nrules;
3608 /* create watches chains */
3611 /* create assertion index. it is only used to speed up
3612 * makeruledecsions() a bit */
3613 for (i = 1, r = solv->rules + i; i < solv->nrules; i++, r++)
3614 if (r->p && !r->w2 && (r->d == 0 || r->d == -1))
3615 queue_push(&solv->ruleassertions, i);
3617 /* disable update rules that conflict with our job */
3618 solver_disablepolicyrules(solv);
3620 /* make initial decisions based on assertion rules */
3621 makeruledecisions(solv);
3622 POOL_DEBUG(SOLV_DEBUG_SOLVER, "problems so far: %d\n", solv->problems.count);
3625 * ********************************************
3627 * ********************************************
3630 now = solv_timems(0);
3631 solver_run_sat(solv, 1, solv->dontinstallrecommended ? 0 : 1);
3632 POOL_DEBUG(SOLV_DEBUG_STATS, "solver took %d ms\n", solv_timems(now));
3635 * prepare solution queue if there were problems
3637 solver_prepare_solutions(solv);
3639 POOL_DEBUG(SOLV_DEBUG_STATS, "final solver statistics: %d problems, %d learned rules, %d unsolvable\n", solv->problems.count / 2, solv->stats_learned, solv->stats_unsolvable);
3640 POOL_DEBUG(SOLV_DEBUG_STATS, "solver_solve took %d ms\n", solv_timems(solve_start));
3642 /* return number of problems */
3643 return solv->problems.count ? solv->problems.count / 2 : 0;
3647 solver_create_transaction(Solver *solv)
3649 return transaction_create_decisionq(solv->pool, &solv->decisionq, &solv->multiversion);
3652 void solver_get_orphaned(Solver *solv, Queue *orphanedq)
3654 queue_free(orphanedq);
3655 queue_init_clone(orphanedq, &solv->orphaned);
3658 void solver_get_recommendations(Solver *solv, Queue *recommendationsq, Queue *suggestionsq, int noselected)
3660 Pool *pool = solv->pool;
3661 Queue redoq, disabledq;
3667 if (!recommendationsq && !suggestionsq)
3670 map_init(&obsmap, pool->nsolvables);
3671 if (solv->installed)
3673 Id obs, *obsp, p, po, ppo;
3674 for (p = solv->installed->start; p < solv->installed->end; p++)
3676 s = pool->solvables + p;
3677 if (s->repo != solv->installed || !s->obsoletes)
3679 if (solv->decisionmap[p] <= 0)
3681 if (solv->multiversion.size && MAPTST(&solv->multiversion, p))
3683 obsp = s->repo->idarraydata + s->obsoletes;
3684 /* foreach obsoletes */
3685 while ((obs = *obsp++) != 0)
3686 FOR_PROVIDES(po, ppo, obs)
3687 MAPSET(&obsmap, po);
3692 queue_init(&disabledq);
3694 /* disable all erase jobs (including weak "keep uninstalled" rules) */
3695 for (i = solv->jobrules, r = solv->rules + i; i < solv->jobrules_end; i++, r++)
3697 if (r->d < 0) /* disabled ? */
3699 if (r->p >= 0) /* install job? */
3701 queue_push(&disabledq, i);
3702 solver_disablerule(solv, r);
3708 enabledisablelearntrules(solv);
3709 removedisabledconflicts(solv, &redoq);
3713 * find recommended packages
3715 if (recommendationsq)
3717 Id rec, *recp, p, pp;
3719 queue_empty(recommendationsq);
3720 /* create map of all recommened packages */
3721 solv->recommends_index = -1;
3722 MAPZERO(&solv->recommendsmap);
3724 /* put all packages the solver already chose in the map */
3725 if (solv->decisioncnt_weak)
3727 for (i = solv->decisioncnt_weak; i < solv->decisioncnt_orphan; i++)
3730 why = solv->decisionq_why.elements[i];
3732 continue; /* forced by unit rule */
3733 p = solv->decisionq.elements[i];
3736 MAPSET(&solv->recommendsmap, p);
3740 for (i = 0; i < solv->decisionq.count; i++)
3742 p = solv->decisionq.elements[i];
3745 s = pool->solvables + p;
3748 recp = s->repo->idarraydata + s->recommends;
3749 while ((rec = *recp++) != 0)
3751 FOR_PROVIDES(p, pp, rec)
3752 if (solv->decisionmap[p] > 0)
3758 FOR_PROVIDES(p, pp, rec)
3759 if (solv->decisionmap[p] > 0)
3760 MAPSET(&solv->recommendsmap, p);
3762 continue; /* p != 0: already fulfilled */
3764 FOR_PROVIDES(p, pp, rec)
3765 MAPSET(&solv->recommendsmap, p);
3769 for (i = 1; i < pool->nsolvables; i++)
3771 if (solv->decisionmap[i] < 0)
3773 if (solv->decisionmap[i] > 0 && noselected)
3775 if (MAPTST(&obsmap, i))
3777 s = pool->solvables + i;
3778 if (!MAPTST(&solv->recommendsmap, i))
3780 if (!s->supplements)
3782 if (!pool_installable(pool, s))
3784 if (!solver_is_supplementing(solv, s))
3787 queue_push(recommendationsq, i);
3789 /* we use MODE_SUGGEST here so that repo prio is ignored */
3790 policy_filter_unwanted(solv, recommendationsq, POLICY_MODE_SUGGEST);
3794 * find suggested packages
3799 Id sug, *sugp, p, pp;
3801 queue_empty(suggestionsq);
3802 /* create map of all suggests that are still open */
3803 solv->recommends_index = -1;
3804 MAPZERO(&solv->suggestsmap);
3805 for (i = 0; i < solv->decisionq.count; i++)
3807 p = solv->decisionq.elements[i];
3810 s = pool->solvables + p;
3813 sugp = s->repo->idarraydata + s->suggests;
3814 while ((sug = *sugp++) != 0)
3816 FOR_PROVIDES(p, pp, sug)
3817 if (solv->decisionmap[p] > 0)
3823 FOR_PROVIDES(p, pp, sug)
3824 if (solv->decisionmap[p] > 0)
3825 MAPSET(&solv->suggestsmap, p);
3827 continue; /* already fulfilled */
3829 FOR_PROVIDES(p, pp, sug)
3830 MAPSET(&solv->suggestsmap, p);
3834 for (i = 1; i < pool->nsolvables; i++)
3836 if (solv->decisionmap[i] < 0)
3838 if (solv->decisionmap[i] > 0 && noselected)
3840 if (MAPTST(&obsmap, i))
3842 s = pool->solvables + i;
3843 if (!MAPTST(&solv->suggestsmap, i))
3847 if (!pool_installable(pool, s))
3849 if (!solver_is_enhancing(solv, s))
3852 queue_push(suggestionsq, i);
3854 policy_filter_unwanted(solv, suggestionsq, POLICY_MODE_SUGGEST);
3857 /* undo removedisabledconflicts */
3859 undo_removedisabledconflicts(solv, &redoq);
3862 /* undo job rule disabling */
3863 for (i = 0; i < disabledq.count; i++)
3864 solver_enablerule(solv, solv->rules + disabledq.elements[i]);
3865 queue_free(&disabledq);
3870 /***********************************************************************/
3871 /* disk usage computations */
3873 /*-------------------------------------------------------------------
3875 * calculate DU changes
3879 solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps)
3883 solver_create_state_maps(solv, &installedmap, 0);
3884 pool_calc_duchanges(solv->pool, &installedmap, mps, nmps);
3885 map_free(&installedmap);
3889 /*-------------------------------------------------------------------
3891 * calculate changes in install size
3895 solver_calc_installsizechange(Solver *solv)
3900 solver_create_state_maps(solv, &installedmap, 0);
3901 change = pool_calc_installsizechange(solv->pool, &installedmap);
3902 map_free(&installedmap);
3907 solver_create_state_maps(Solver *solv, Map *installedmap, Map *conflictsmap)
3909 pool_create_state_maps(solv->pool, &solv->decisionq, installedmap, conflictsmap);
3913 solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res)
3915 Pool *pool = solv->pool;
3918 pool_create_state_maps(pool, &solv->decisionq, &installedmap, 0);
3919 pool_trivial_installable_multiversionmap(pool, &installedmap, pkgs, res, solv->multiversion.size ? &solv->multiversion : 0);
3920 for (i = 0; i < res->count; i++)
3921 if (res->elements[i] != -1)
3923 Solvable *s = pool->solvables + pkgs->elements[i];
3924 if (!strncmp("patch:", pool_id2str(pool, s->name), 6) && solvable_is_irrelevant_patch(s, &installedmap))
3925 res->elements[i] = -1;
3927 map_free(&installedmap);
3930 /*-------------------------------------------------------------------
3932 * decision introspection
3936 solver_get_decisionlevel(Solver *solv, Id p)
3938 return solv->decisionmap[p];
3942 solver_get_decisionqueue(Solver *solv, Queue *decisionq)
3944 queue_free(decisionq);
3945 queue_init_clone(decisionq, &solv->decisionq);
3949 solver_get_lastdecisionblocklevel(Solver *solv)
3952 if (solv->decisionq.count == 0)
3954 p = solv->decisionq.elements[solv->decisionq.count - 1];
3957 return solv->decisionmap[p] < 0 ? -solv->decisionmap[p] : solv->decisionmap[p];
3961 solver_get_decisionblock(Solver *solv, int level, Queue *decisionq)
3966 queue_empty(decisionq);
3967 for (i = 0; i < solv->decisionq.count; i++)
3969 p = solv->decisionq.elements[i];
3972 if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
3975 if (i == solv->decisionq.count)
3977 for (i = 0; i < solv->decisionq.count; i++)
3979 p = solv->decisionq.elements[i];
3982 if (solv->decisionmap[p] == level || solv->decisionmap[p] == -level)
3983 queue_push(decisionq, p);
3990 solver_describe_decision(Solver *solv, Id p, Id *infop)
3997 if (!solv->decisionmap[p])
3998 return SOLVER_REASON_UNRELATED;
3999 pp = solv->decisionmap[p] < 0 ? -p : p;
4000 for (i = 0; i < solv->decisionq.count; i++)
4001 if (solv->decisionq.elements[i] == pp)
4003 if (i == solv->decisionq.count) /* just in case... */
4004 return SOLVER_REASON_UNRELATED;
4005 why = solv->decisionq_why.elements[i];
4010 return SOLVER_REASON_UNIT_RULE;
4013 if (i < solv->decisioncnt_update)
4018 *infop = SYSTEMSOLVABLE;
4019 return SOLVER_REASON_KEEP_INSTALLED;
4023 return SOLVER_REASON_RESOLVE_JOB;
4025 if (i < solv->decisioncnt_keep)
4027 if (why == 0 && pp < 0)
4028 return SOLVER_REASON_CLEANDEPS_ERASE;
4031 if (why >= solv->updaterules && why < solv->updaterules_end)
4032 *infop = why - solv->updaterules;
4033 else if (why >= solv->featurerules && why < solv->featurerules_end)
4034 *infop = why - solv->featurerules;
4036 return SOLVER_REASON_UPDATE_INSTALLED;
4038 if (i < solv->decisioncnt_resolve)
4040 if (why == 0 && pp < 0)
4041 return SOLVER_REASON_CLEANDEPS_ERASE;
4044 if (why >= solv->updaterules && why < solv->updaterules_end)
4045 *infop = why - solv->updaterules;
4046 else if (why >= solv->featurerules && why < solv->featurerules_end)
4047 *infop = why - solv->featurerules;
4049 return SOLVER_REASON_KEEP_INSTALLED;
4051 if (i < solv->decisioncnt_weak)
4055 return SOLVER_REASON_RESOLVE;
4057 if (solv->decisionq.count < solv->decisioncnt_orphan)
4058 return SOLVER_REASON_WEAKDEP;
4059 return SOLVER_REASON_RESOLVE_ORPHAN;
4064 solver_describe_weakdep_decision(Solver *solv, Id p, Queue *whyq)
4066 Pool *pool = solv->pool;
4068 int level = solv->decisionmap[p];
4075 for (decisionno = 0; decisionno < solv->decisionq.count; decisionno++)
4076 if (solv->decisionq.elements[decisionno] == p)
4078 if (decisionno == solv->decisionq.count)
4080 if (decisionno < solv->decisioncnt_weak || decisionno >= solv->decisioncnt_orphan)
4083 /* 1) list all packages that recommend us */
4084 for (i = 1; i < pool->nsolvables; i++)
4086 Id *recp, rec, pp2, p2;
4087 if (solv->decisionmap[i] < 0 || solv->decisionmap[i] >= level)
4089 s = pool->solvables + i;
4092 if (!solv->addalreadyrecommended && s->repo == solv->installed)
4094 recp = s->repo->idarraydata + s->recommends;
4095 while ((rec = *recp++) != 0)
4098 FOR_PROVIDES(p2, pp2, rec)
4104 /* if p2 is already installed, this recommends is ignored */
4105 if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
4111 queue_push(whyq, SOLVER_REASON_RECOMMENDED);
4112 queue_push2(whyq, p2, rec);
4116 /* 2) list all supplements */
4117 s = pool->solvables + p;
4118 if (s->supplements && level > 0)
4120 Id *supp, sup, pp2, p2;
4121 /* this is a hack. to use solver_dep_fulfilled we temporarily clear
4122 * everything above our level in the decisionmap */
4123 for (i = decisionno; i < solv->decisionq.count; i++ )
4125 p2 = solv->decisionq.elements[i];
4127 solv->decisionmap[p2] = -solv->decisionmap[p2];
4129 supp = s->repo->idarraydata + s->supplements;
4130 while ((sup = *supp++) != 0)
4131 if (solver_dep_fulfilled(solv, sup))
4134 /* let's see if this is an easy supp */
4135 FOR_PROVIDES(p2, pp2, sup)
4137 if (!solv->addalreadyrecommended && solv->installed)
4139 if (pool->solvables[p2].repo == solv->installed)
4142 if (solv->decisionmap[p2] > 0 && solv->decisionmap[p2] < level)
4144 queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
4145 queue_push2(whyq, p2, sup);
4151 /* hard case, just note dependency with no package */
4152 queue_push(whyq, SOLVER_REASON_SUPPLEMENTED);
4153 queue_push2(whyq, 0, sup);
4156 for (i = decisionno; i < solv->decisionq.count; i++)
4158 p2 = solv->decisionq.elements[i];
4160 solv->decisionmap[p2] = -solv->decisionmap[p2];
4166 pool_job2solvables(Pool *pool, Queue *pkgs, Id how, Id what)
4169 how &= SOLVER_SELECTMASK;
4171 if (how == SOLVER_SOLVABLE_ALL)
4173 FOR_POOL_SOLVABLES(p)
4174 queue_push(pkgs, p);
4176 else if (how == SOLVER_SOLVABLE_REPO)
4178 Repo *repo = pool_id2repo(pool, what);
4181 FOR_REPO_SOLVABLES(repo, p, s)
4182 queue_push(pkgs, p);
4186 FOR_JOB_SELECT(p, pp, how, what)
4187 queue_push(pkgs, p);
4192 pool_isemptyupdatejob(Pool *pool, Id how, Id what)
4195 Id select = how & SOLVER_SELECTMASK;
4196 if ((how & SOLVER_JOBMASK) != SOLVER_UPDATE)
4198 if (select == SOLVER_SOLVABLE_ALL || select == SOLVER_SOLVABLE_REPO)
4200 if (!pool->installed)
4202 FOR_JOB_SELECT(p, pp, select, what)
4203 if (pool->solvables[p].repo == pool->installed)
4206 FOR_JOB_SELECT(p, pp, select, what)
4208 Solvable *s = pool->solvables + p;
4209 FOR_PROVIDES(pi, pip, s->name)
4211 Solvable *si = pool->solvables + pi;
4212 if (si->repo != pool->installed || si->name != s->name)
4218 Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
4219 while ((obs = *obsp++) != 0)
4221 FOR_PROVIDES(pi, pip, obs)
4223 Solvable *si = pool->solvables + pi;
4224 if (si->repo != pool->installed)
4226 if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, si, obs))
4228 if (pool->obsoleteusescolors && !pool_colormatch(pool, s, si))