4 * SAT based dependency solver
18 #define RULES_BLOCK 63
20 static Pool *prune_best_version_arch_sortcmp_data;
22 /*-----------------------------------------------------------------*/
25 * prep for prune_best_version_arch
30 prune_best_version_arch_sortcmp(const void *ap, const void *bp)
32 Pool *pool = prune_best_version_arch_sortcmp_data;
35 return pool->solvables[a].name - pool->solvables[b].name;
41 replaces_system(Solver *solv, Id id)
43 Pool *pool = solv->pool;
44 Source *system = solv->system;
45 Id *name = pool->solvables[id].name;
47 FOR_PROVIDES(p, pp, id)
49 s = pool->solvables + p;
52 if (p >= system->start && p < system->start + system->nsolvables)
59 dep_installed(Solver *solv, Id dep)
61 Pool *pool = solv->pool;
66 Reldep *rd = GETRELDEP(pool, dep);
67 if (rd->flags == REL_AND)
69 if (!dep_installed(solv, rd->name))
71 return dep_installed(solv, rd->evr);
73 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
74 return dep_installed(solv, rd->evr);
76 FOR_PROVIDES(p, pp, dep)
78 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
85 dep_fulfilled(Solver *solv, Id dep)
87 Pool *pool = solv->pool;
92 Reldep *rd = GETRELDEP(pool, dep);
93 if (rd->flags == REL_AND)
95 if (!dep_fulfilled(solv, rd->name))
97 return dep_fulfilled(solv, rd->evr);
99 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
100 return dep_installed(solv, rd->evr);
102 FOR_PROVIDES(p, pp, dep)
104 if (solv->decisionmap[p] > 0)
111 dep_possible(Solver *solv, Id dep, Map *m)
113 Pool *pool = solv->pool;
118 Reldep *rd = GETRELDEP(pool, dep);
119 if (rd->flags == REL_AND)
121 if (!dep_possible(solv, rd->name, m))
123 return dep_possible(solv, rd->evr, m);
125 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
126 return dep_installed(solv, rd->evr);
128 FOR_PROVIDES(p, pp, dep)
137 prune_to_highest_prio(Pool *pool, Queue *plist)
143 /* prune to highest priority */
144 for (i = 0; i < plist->count; i++)
146 s = pool->solvables + plist->elements[i];
147 if (i == 0 || s->source->priority > bestprio)
148 bestprio = s->source->priority;
150 for (i = j = 0; i < plist->count; i++)
152 s = pool->solvables + plist->elements[i];
153 if (s->source->priority == bestprio)
154 plist->elements[j++] = plist->elements[i];
160 * prune_to_recommended
162 * XXX: should we prune to requires/suggests that are already
163 * fulfilled by other packages?
166 prune_to_recommended(Solver *solv, Queue *plist)
168 Pool *pool = solv->pool;
171 Id p, *pp, sup, *supp, rec, *recp, sug, *sugp, enh, *enhp;
173 if (solv->recommends_index < 0)
175 MAPZERO(&solv->recommendsmap);
176 MAPZERO(&solv->suggestsmap);
177 solv->recommends_index = 0;
179 while (solv->recommends_index < solv->decisionq.count)
181 p = solv->decisionq.elements[solv->recommends_index++];
184 s = pool->solvables + p;
187 recp = s->source->idarraydata + s->recommends;
188 while ((rec = *recp++) != 0)
189 FOR_PROVIDES(p, pp, rec)
190 MAPSET(&solv->recommendsmap, p);
194 sugp = s->source->idarraydata + s->suggests;
195 while ((sug = *sugp++) != 0)
196 FOR_PROVIDES(p, pp, sug)
197 MAPSET(&solv->suggestsmap, p);
200 /* prune to recommended/supplemented */
201 for (i = j = 0; i < plist->count; i++)
203 p = plist->elements[i];
204 if (MAPTST(&solv->recommendsmap, p))
206 plist->elements[j++] = p;
209 s = pool->solvables + p;
210 if (!s->supplements && !s->freshens)
214 supp = s->source->idarraydata + s->supplements;
215 while ((sup = *supp++) != 0)
216 if (dep_fulfilled(solv, sup))
223 supp = s->source->idarraydata + s->freshens;
224 while ((sup = *supp++) != 0)
225 if (dep_fulfilled(solv, sup))
230 plist->elements[j++] = s - pool->solvables;
235 /* prune to suggested/enhanced*/
236 if (plist->count < 2)
238 for (i = j = 0; i < plist->count; i++)
240 p = plist->elements[i];
241 if (MAPTST(&solv->suggestsmap, p))
243 plist->elements[j++] = p;
246 s = pool->solvables + p;
249 enhp = s->source->idarraydata + s->enhances;
250 while ((enh = *enhp++) != 0)
251 if (dep_fulfilled(solv, enh))
255 plist->elements[j++] = s - pool->solvables;
262 * prune_best_version_arch
264 * sort list of packages (given through plist) by name and evr
265 * return result through plist
269 /* FIXME: must also look at update packages */
272 prune_best_version_arch(Pool *pool, Queue *plist)
279 if (plist->count < 2) /* no need to prune for a single entry */
281 if (pool->verbose) printf("prune_best_version_arch %d\n", plist->count);
283 /* prune to best architecture */
287 for (i = 0; i < plist->count; i++)
289 s = pool->solvables + plist->elements[i];
291 a = a <= pool->lastarch ? pool->id2arch[a] : 0;
292 if (a && a != 1 && (!bestscore || a < bestscore))
295 for (i = j = 0; i < plist->count; i++)
297 s = pool->solvables + plist->elements[i];
299 if (a > pool->lastarch)
301 a = pool->id2arch[a];
302 /* a == 1 -> noarch */
303 if (a != 1 && ((a ^ bestscore) & 0xffff0000) != 0)
305 plist->elements[j++] = plist->elements[i];
311 prune_best_version_arch_sortcmp_data = pool;
312 /* sort by name first */
313 qsort(plist->elements, plist->count, sizeof(Id), prune_best_version_arch_sortcmp);
315 /* now find best 'per name' */
316 /* FIXME: also check obsoletes! */
317 for (i = j = 0; i < plist->count; i++)
319 s = pool->solvables + plist->elements[i];
321 if (pool->verbose) printf("- %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
323 if (!best) /* if no best yet, the current is best */
325 best = plist->elements[i];
329 /* name switch: re-init */
330 if (pool->solvables[best].name != s->name) /* new name */
332 plist->elements[j++] = best; /* move old best to front */
333 best = plist->elements[i]; /* take current as new best */
337 if (pool->solvables[best].evr != s->evr) /* compare evr */
339 if (evrcmp(pool, pool->solvables[best].evr, s->evr) < 0)
340 best = plist->elements[i];
345 best = plist->elements[0];
347 plist->elements[j++] = best;
352 /*-----------------------------------------------------------------*/
359 printruleelement(Solver *solv, Rule *r, Id v)
361 Pool *pool = solv->pool;
365 s = pool->solvables + -v;
366 printf(" !%s-%s.%s [%d]", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), -v);
370 s = pool->solvables + v;
371 printf(" %s-%s.%s [%d]", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), v);
380 if (solv->decisionmap[s - pool->solvables] > 0)
381 printf(" I.%d", solv->decisionmap[s - pool->solvables]);
382 if (solv->decisionmap[s - pool->solvables] < 0)
383 printf(" C.%d", -solv->decisionmap[s - pool->solvables]);
393 printrule(Solver *solv, Rule *r)
398 if (r >= solv->rules && r < solv->rules + solv->nrules) /* r is a solver rule */
399 printf("Rule #%d:\n", (int)(r - solv->rules));
401 printf("Rule:\n"); /* r is any rule */
406 else if (r->d == ID_NULL)
413 v = solv->pool->whatprovidesdata[r->d + i - 1];
416 printruleelement(solv, r, v);
418 printf(" next: %d %d\n", r->n1, r->n2);
422 /*-----------------------------------------------------------------*/
428 static Pool *unifyrules_sortcmp_data;
431 * compare rules for unification sort
435 unifyrules_sortcmp(const void *ap, const void *bp)
437 Pool *pool = unifyrules_sortcmp_data;
438 Rule *a = (Rule *)ap;
439 Rule *b = (Rule *)bp;
445 return x; /* p differs */
448 if (a->d == 0 && b->d == 0)
449 return a->w2 - b->w2; /* assertion: return w2 diff */
451 if (a->d == 0) /* a is assertion, b not */
453 x = a->w2 - pool->whatprovidesdata[b->d];
457 if (b->d == 0) /* b is assertion, a not */
459 x = pool->whatprovidesdata[a->d] - b->w2;
463 /* compare whatprovidesdata */
464 ad = pool->whatprovidesdata + a->d;
465 bd = pool->whatprovidesdata + b->d;
467 if ((x = *ad++ - *bd++) != 0)
478 unifyrules(Solver *solv)
483 if (solv->nrules <= 1) /* nothing to unify */
486 /* sort rules first */
487 unifyrules_sortcmp_data = solv->pool;
488 qsort(solv->rules + 1, solv->nrules - 1, sizeof(Rule), unifyrules_sortcmp);
495 for (i = j = 1, ir = solv->rules + 1; i < solv->nrules; i++, ir++)
497 if (jr && !unifyrules_sortcmp(ir, jr))
498 continue; /* prune! */
499 jr = solv->rules + j++; /* keep! */
504 /* reduced count from nrules to j rules */
505 if (solv->pool->verbose) printf("pruned rules from %d to %d\n", solv->nrules, j);
507 /* adapt rule buffer */
508 solv->rules = (Rule *)xrealloc(solv->rules, ((solv->nrules + RULES_BLOCK) & ~RULES_BLOCK) * sizeof(Rule));
517 for (i = 1; i < solv->nrules; i++)
520 if (r->d == 0) /* assertion */
525 dp = solv->pool->whatprovidesdata + r->d;
529 if (solv->pool->verbose)
531 printf(" binary: %d\n", binr);
532 printf(" normal: %d\n", solv->nrules - 1 - binr);
533 printf(" normal lits: %d\n", dc);
546 hashrule(Solver *solv, Id p, Id d, int n)
548 unsigned int x = (unsigned int)p;
552 return (x * 37) ^ (unsigned int)d;
553 dp = solv->pool->whatprovidesdata + d;
555 x = (x * 37) ^ (unsigned int)*dp++;
563 * p = direct literal; > 0 for learnt, < 0 for installed pkg (rpm)
564 * d, if < 0 direct literal, if > 0 offset into whatprovides, if == 0 rule is assertion (look at p only)
567 * A requires b, b provided by B1,B2,B3 => (-A|B1|B2|B3)
569 * p < 0 : rule from rpm (installed pkg)
570 * d > 0 : Offset in whatprovidesdata (list of providers)
572 * A conflicts b, b provided by B1,B2,B3 => (-A|-B1), (-A|-B2), (-A|-B3)
573 * d < 0: Id of solvable (e.g. B1)
575 * d == 0: unary rule, assertion => (A) or (-A)
577 * Install: p > 0, d = 0 (A) user requested install
578 * Remove: p < 0, d = 0 (-A) user requested remove
579 * Requires: p < 0, d > 0 (-A|B1|B2|...) d: <list of providers for requirement of p>
580 * Updates: p > 0, d > 0 (A|B1|B2|...) d: <list of updates for solvable p>
581 * Conflicts: p < 0, d < 0 (-A|-B) either p (conflict issuer) or d (conflict provider)
582 * ? p > 0, d < 0 (A|-B)
583 * No-op ?: p = 0, d = 0 (null) (used as policy rule placeholder)
587 addrule(Solver *solv, Id p, Id d)
592 int n = 0; /* number of literals in rule - 1
593 0 = direct assertion (single literal)
597 /* it often happenes that requires lead to adding the same rpm rule
598 * multiple times, so we prune those duplicates right away to make
599 * the work for unifyrules a bit easier */
601 if (solv->nrules && !solv->jobrules)
603 r = solv->rules + solv->nrules - 1; /* get the last added rule */
604 if (r->p == p && r->d == d && d != 0) /* identical and not user requested */
611 return 0; /* ignore self conflict */
614 else if (d == 0) /* user requested */
618 for (dp = solv->pool->whatprovidesdata + d; *dp; dp++, n++)
620 return 0; /* rule is self-fulfilling */
625 if (n == 0) /* direct assertion */
629 /* this is a rpm rule assertion, we do not have to allocate it */
630 /* it can be identified by a level of 1 and a zero reason */
633 if (solv->decisionmap[-p] > 0 || solv->decisionmap[-p] < -1)
635 if (solv->decisionmap[-p])
637 queuepush(&solv->decisionq, p);
638 queuepush(&solv->decisionq_why, 0);
639 solv->decisionmap[-p] = -1;
643 else if (n == 1 && p > d)
645 /* smallest literal first so we can find dups */
649 n = 1; /* re-set n, was used as temp var */
652 /* check if the last added rule is exactly the same as what we're looking for. */
653 if (r && n == 1 && !r->d && r->p == p && r->w2 == d)
656 if (r && n > 1 && r->d && r->p == p)
661 dp2 = solv->pool->whatprovidesdata + r->d;
662 for (dp = solv->pool->whatprovidesdata + d; *dp; dp++, dp2++)
673 /* check and extend rule buffer */
674 if ((solv->nrules & RULES_BLOCK) == 0)
676 solv->rules = (Rule *)xrealloc(solv->rules, (solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
679 r = solv->rules + solv->nrules++; /* point to rule space */
684 /* direct assertion, no watch needed */
700 r->w2 = solv->pool->whatprovidesdata[d];
708 /* go through system and job rules and add direct assertions
709 * to the decisionqueue. If we find a conflict, disable rules and
710 * add them to problem queue.
713 makeruledecisions(Solver *solv)
719 /* no learnt rules for now */
720 if (solv->learntrules && solv->learntrules != solv->nrules)
723 for (ri = solv->jobrules, r = solv->rules + ri; ri < solv->nrules; ri++, r++)
729 if (solv->decisionmap[vv] == 0)
731 queuepush(&solv->decisionq, v);
732 queuepush(&solv->decisionq_why, r - solv->rules);
733 solv->decisionmap[vv] = v > 0 ? 1 : -1;
736 if (v > 0 && solv->decisionmap[vv] > 0)
738 if (v < 0 && solv->decisionmap[vv] < 0)
740 /* found a conflict! */
741 /* if we are weak, just disable ourself */
742 if (ri >= solv->weakrules)
744 printf("conflict, but I am weak, disabling ");
749 for (i = 0; i < solv->decisionq.count; i++)
750 if (solv->decisionq.elements[i] == -v)
752 if (i == solv->decisionq.count)
754 if (solv->decisionq_why.elements[i] == 0)
756 /* conflict with rpm rule, need only disable our rule */
757 printf("conflict with rpm rule, disabling rule #%d\n", ri);
758 queuepush(&solv->problems, r - solv->rules);
759 queuepush(&solv->problems, 0);
760 r->w1 = 0; /* disable */
763 /* conflict with another job or system rule */
764 /* remove old decision */
765 printf("conflicting system/job rules over literal %d\n", vv);
766 solv->decisionmap[vv] = 0;
767 for (; i + 1 < solv->decisionq.count; i++)
769 solv->decisionq.elements[i] = solv->decisionq.elements[i + 1];
770 solv->decisionq_why.elements[i] = solv->decisionq_why.elements[i + 1];
772 solv->decisionq.count--;
773 solv->decisionq_why.count--;
774 /* push all of our rules asserting this literal on the problem stack */
775 for (i = solv->jobrules, rr = solv->rules + i; i < solv->nrules; i++, rr++)
777 if (!rr->w1 || rr->w2)
779 if (rr->p != v && rr->p != -v)
781 printf(" - disabling rule #%d\n", i);
782 queuepush(&solv->problems, i);
783 rr->w1 = 0; /* disable */
785 queuepush(&solv->problems, 0);
791 * add (install) rules for solvable
796 addrulesforsolvable(Solver *solv, Solvable *s, Map *m)
798 Pool *pool = solv->pool;
799 Source *system = solv->system;
813 queueinit_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
814 queuepush(&q, s - pool->solvables); /* push solvable Id */
820 * s: Pointer to solvable
824 if (MAPTST(m, n)) /* continue if already done */
828 s = pool->solvables + n; /* s = Solvable in question */
833 && n >= system->start /* is it installed? */
834 && n < system->start + system->nsolvables)
836 dontfix = 1; /* dont care about broken rpm deps */
839 /*-----------------------------------------
840 * check requires of s
845 reqp = s->source->idarraydata + s->requires;
846 while ((req = *reqp++) != 0)
848 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
851 dp = GET_PROVIDESP(req, p); /* get providers of req */
853 if (dontfix) /* dont care about breakage */
855 /* the strategy here is to not insist on dependencies
856 * that are already broken. so if we find one provider
857 * that was already installed, we know that the
858 * dependency was not broken before so we enforce it */
859 for (i = 0; dp[i]; i++) /* for all providers */
861 if (dp[i] >= system->start && dp[i] < system->start + system->nsolvables)
862 break; /* provider is installed */
864 if (!dp[i]) /* previously broken dependency */
866 if (pool->verbose) printf("ignoring broken requires %s of system package %s-%s.%s\n", dep2str(pool, req), id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
873 /* nothing provides req! */
875 if (pool->verbose) printf("package %s-%s.%s [%ld] is not installable (%s)\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), (long int)(s - pool->solvables), dep2str(pool, req));
877 addrule(solv, -n, 0); /* mark requestor as uninstallable */
879 printf(">!> !unflag %s-%s.%s[%s]\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), source_name(s->source));
883 printf("addrule %s-%s.%s %s %d %d\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), dep2str(pool, req), -n, dp - pool->whatprovidesdata);
884 for (i = 0; dp[i]; i++)
885 printf(" %s-%s.%s\n", id2str(pool, pool->solvables[dp[i]].name), id2str(pool, pool->solvables[dp[i]].evr), id2str(pool, pool->solvables[dp[i]].arch));
887 /* add 'requires' dependency */
888 /* rule: (-requestor|provider1|provider2|...|providerN) */
889 addrule(solv, -n, dp - pool->whatprovidesdata);
891 /* descend the dependency tree */
892 for (; *dp; dp++) /* loop through all providers */
898 } /* while, requirements of n */
900 } /* if, requirements */
903 /*-----------------------------------------
904 * check conflicts of s
909 conp = s->source->idarraydata + s->conflicts;
910 while ((con = *conp++) != 0)
912 FOR_PROVIDES(p, pp, con)
914 /* dontfix: dont care about conflicts with already installed packs */
915 if (dontfix && p >= system->start && p < system->start + system->nsolvables)
917 /* rule: -n|-p: either solvable _or_ provider of conflict */
918 addrule(solv, -n, -p);
923 /*-----------------------------------------
924 * check obsoletes if not installed
926 if (!system || n < system->start || n >= (system->start + system->nsolvables))
927 { /* not installed */
930 obsp = s->source->idarraydata + s->obsoletes;
931 while ((obs = *obsp++) != 0)
933 FOR_PROVIDES(p, pp, obs)
934 addrule(solv, -n, -p);
937 FOR_PROVIDES(p, pp, s->name)
939 if (s->name == pool->solvables[p].name)
940 addrule(solv, -n, -p);
944 /*-----------------------------------------
945 * add recommends to the rule list
949 recp = s->source->idarraydata + s->recommends;
950 while ((rec = *recp++) != 0)
952 FOR_PROVIDES(p, pp, rec)
959 sugp = s->source->idarraydata + s->suggests;
960 while ((sug = *sugp++) != 0)
962 FOR_PROVIDES(p, pp, sug)
972 addrulesforweak(Solver *solv, Map *m)
974 Pool *pool = solv->pool;
979 if (pool->verbose) printf("addrulesforweak... (%d)\n", solv->nrules);
980 for (i = n = 1; n < pool->nsolvables; i++, n++)
982 if (i == pool->nsolvables)
986 s = pool->solvables + i;
987 if (!pool_installable(pool, s))
992 supp = s->source->idarraydata + s->supplements;
993 while ((sup = *supp++) != ID_NULL)
994 if (dep_possible(solv, sup, m))
997 if (!sup && s->freshens)
999 supp = s->source->idarraydata + s->freshens;
1000 while ((sup = *supp++) != ID_NULL)
1001 if (dep_possible(solv, sup, m))
1004 if (!sup && s->enhances)
1006 supp = s->source->idarraydata + s->enhances;
1007 while ((sup = *supp++) != ID_NULL)
1008 if (dep_possible(solv, sup, m))
1013 addrulesforsolvable(solv, s, m);
1016 if (pool->verbose) printf("done. (%d)\n", solv->nrules);
1020 archchanges(Pool *pool, Solvable *s1, Solvable *s2)
1022 Id a1 = s1->arch, a2 = s2->arch;
1024 /* we allow changes to/from noarch */
1025 if (a1 == a2 || a1 == ARCH_NOARCH || a2 == ARCH_NOARCH)
1029 a1 = a1 <= pool->lastarch ? pool->id2arch[a1] : 0;
1030 a2 = a2 <= pool->lastarch ? pool->id2arch[a2] : 0;
1031 if (((a1 ^ a2) & 0xffff0000) != 0)
1038 findupdatepackages(Solver *solv, Solvable *s, Queue *qs, Map *m, int allowdowngrade, int allowarchchange)
1040 /* system packages get a special upgrade allowed rule */
1041 Pool *pool = solv->pool;
1042 Id p, *pp, n, p2, *pp2;
1051 n = s - pool->solvables;
1052 if (m && !MAPTST(m, n)) /* add rule for s if not already done */
1053 addrulesforsolvable(solv, s, m);
1056 * look for updates for s
1058 FOR_PROVIDES(p, pp, s->name) /* every provider of s' name */
1060 if (p == n) /* skip itself */
1063 ps = pool->solvables + p;
1064 if (s->name == ps->name) /* name match */
1066 if (!allowdowngrade /* consider downgrades ? */
1067 && evrcmp(pool, s->evr, ps->evr) > 0)
1070 if (!allowarchchange && archchanges(pool, s, ps))
1073 else if (!solv->noupdateprovide && ps->obsoletes) /* provides/obsoletes combination ? */
1075 obsp = ps->source->idarraydata + ps->obsoletes;
1076 while ((obs = *obsp++) != 0) /* for all obsoletes */
1078 FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
1080 if (p2 == n) /* match ! */
1083 if (p2) /* match! */
1086 if (!obs) /* continue if no match */
1088 /* here we have 'p' with a matching provides/obsoletes combination
1089 * thus flagging p as a valid update candidate for s
1096 if (m && !MAPTST(m, p)) /* mark p for install if not already done */
1097 addrulesforsolvable(solv, pool->solvables + p, m);
1099 if (solv->noupdateprovide && solv->obsoletes && solv->obsoletes[n - solv->system->start])
1101 for (pp = solv->obsoletes_data + solv->obsoletes[n - solv->system->start]; (p = *pp++) != 0;)
1104 if (m && !MAPTST(m, p)) /* mark p for install if not already done */
1105 addrulesforsolvable(solv, pool->solvables + p, m);
1111 * add rule for update
1112 * (A|A1|A2|A3...) An = update candidates for A
1114 * s = (installed) solvable
1115 * m = 'addedmap', bit set if 'install' rule for solvable exists
1119 addupdaterule(Solver *solv, Solvable *s, Map *m, int allowdowngrade, int allowarchchange, int dontaddrule)
1121 /* system packages get a special upgrade allowed rule */
1122 Pool *pool = solv->pool;
1128 queueinit_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1129 findupdatepackages(solv, s, &qs, m, allowdowngrade, allowarchchange);
1130 p = s - pool->solvables;
1131 if (dontaddrule) /* we consider update candidates but dont force them */
1137 if (qs.count == 0) /* no updates found */
1140 printf("new update rule: must keep %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1142 addrule(solv, p, 0); /* request 'install' of s */
1147 d = pool_queuetowhatprovides(pool, &qs); /* intern computed provider queue */
1149 r = addrule(solv, p, d); /* allow update of s */
1151 printf("new update rule ");
1158 /*-----------------------------------------------------------------*/
1165 * initial setup for all watches
1169 makewatches(Solver *solv)
1173 int nsolvables = solv->pool->nsolvables;
1175 xfree(solv->watches);
1176 /* lower half for removals, upper half for installs */
1177 solv->watches = (Id *)xcalloc(2 * nsolvables, sizeof(Id));
1179 /* do it reverse so rpm rules get triggered first */
1180 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
1182 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
1185 if (!r->w1 /* rule is disabled */
1186 || !r->w2) /* rule is assertion */
1189 /* see addwatches(solv, r) */
1190 r->n1 = solv->watches[nsolvables + r->w1];
1191 solv->watches[nsolvables + r->w1] = r - solv->rules;
1193 r->n2 = solv->watches[nsolvables + r->w2];
1194 solv->watches[nsolvables + r->w2] = r - solv->rules;
1200 * add watches (for rule)
1204 addwatches(Solver *solv, Rule *r)
1206 int nsolvables = solv->pool->nsolvables;
1208 r->n1 = solv->watches[nsolvables + r->w1];
1209 solv->watches[nsolvables + r->w1] = r - solv->rules;
1211 r->n2 = solv->watches[nsolvables + r->w2];
1212 solv->watches[nsolvables + r->w2] = r - solv->rules;
1216 /*-----------------------------------------------------------------*/
1217 /* rule propagation */
1219 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
1224 * propagate decision to all rules
1228 propagate(Solver *solv, int level)
1230 Pool *pool = solv->pool;
1235 Id *decisionmap = solv->decisionmap;
1236 Id *watches = solv->watches + pool->nsolvables;
1238 while (solv->propagate_index < solv->decisionq.count)
1240 /* negative because our watches trigger if literal goes FALSE */
1241 pkg = -solv->decisionq.elements[solv->propagate_index++];
1243 printf("popagate for decision %d level %d\n", -pkg, level);
1244 printruleelement(solv, 0, -pkg);
1246 for (rp = watches + pkg; *rp; rp = nrp)
1248 r = solv->rules + *rp;
1250 printf(" watch triggered ");
1263 /* if clause is TRUE, nothing to do */
1264 if (DECISIONMAP_TRUE(ow))
1269 /* not a binary clause, check if we need to move our watch */
1270 if (r->p && r->p != ow && !DECISIONMAP_TRUE(-r->p))
1273 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
1274 if (p != ow && !DECISIONMAP_TRUE(-p))
1278 /* p is free to watch, move watch to p */
1281 printf(" -> move w%d to %s-%s.%s\n", (pkg == r->w1 ? 1 : 2), id2str(pool, pool->solvables[p].name), id2str(pool, pool->solvables[p].evr), id2str(pool, pool->solvables[p].arch));
1283 printf(" -> move w%d to !%s-%s.%s\n", (pkg == r->w1 ? 1 : 2), id2str(pool, pool->solvables[-p].name), id2str(pool, pool->solvables[-p].evr), id2str(pool, pool->solvables[-p].arch));
1297 watches[p] = r - solv->rules;
1301 /* unit clause found, set other watch to TRUE */
1302 if (DECISIONMAP_TRUE(-ow))
1303 return r; /* eek, a conflict! */
1309 decisionmap[ow] = level;
1311 decisionmap[-ow] = -level;
1312 queuepush(&solv->decisionq, ow);
1313 queuepush(&solv->decisionq_why, r - solv->rules);
1316 Solvable *s = pool->solvables + (ow > 0 ? ow : -ow);
1318 printf(" -> decided to install %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1320 printf(" -> decided to conflict %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1325 return 0; /* all is well */
1329 /*-----------------------------------------------------------------*/
1338 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *why)
1340 Pool *pool = solv->pool;
1343 Map seen; /* global? */
1347 int learnt_why = solv->learnt_pool.count;
1348 Id *decisionmap = solv->decisionmap;
1352 if (pool->verbose) printf("ANALYZE at %d ----------------------\n", level);
1353 mapinit(&seen, pool->nsolvables);
1354 idx = solv->decisionq.count;
1358 queuepush(&solv->learnt_pool, c - solv->rules);
1359 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1370 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1372 vv = v > 0 ? v : -v;
1373 if (MAPTST(&seen, vv))
1375 l = solv->decisionmap[vv];
1382 for (j = 0; j < solv->decisionq.count; j++)
1383 if (solv->decisionq.elements[j] == v)
1385 if (j == solv->decisionq.count)
1387 queuepush(&rulq, -(j + 1));
1389 continue; /* initial setting */
1393 num++; /* need to do this one as well */
1398 printf("PUSH %d ", v);
1399 printruleelement(solv, 0, v);
1406 printf("num = %d\n", num);
1412 v = solv->decisionq.elements[--idx];
1413 vv = v > 0 ? v : -v;
1414 if (MAPTST(&seen, vv))
1417 c = solv->rules + solv->decisionq_why.elements[idx];
1425 else if (r.count == 1 && r.elements[0] < 0)
1426 *dr = r.elements[0];
1428 *dr = pool_queuetowhatprovides(pool, &r);
1431 printf("learned rule for level %d (am %d)\n", rlevel, level);
1432 printruleelement(solv, 0, -v);
1433 for (i = 0; i < r.count; i++)
1436 printruleelement(solv, 0, v);
1440 queuepush(&solv->learnt_pool, 0);
1442 for (i = learnt_why; solv->learnt_pool.elements[i]; i++)
1444 printf("learnt_why ");
1445 printrule(solv, solv->rules + solv->learnt_pool.elements[i]);
1456 * reset the solver decisions to right after the rpm rules
1460 reset_solver(Solver *solv)
1465 /* delete all learnt rules */
1466 solv->nrules = solv->learntrules;
1467 QUEUEEMPTY(&solv->learnt_why);
1468 QUEUEEMPTY(&solv->learnt_pool);
1470 /* redo all direct rpm rule decisions */
1471 /* we break at the first decision with a why attached, this is
1472 * either a job/system rule decision of a propagated decision */
1473 for (i = 0; i < solv->decisionq.count; i++)
1475 v = solv->decisionq.elements[i];
1476 solv->decisionmap[v > 0 ? v : -v] = 0;
1478 for (i = 0; i < solv->decisionq_why.count; i++)
1479 if (solv->decisionq_why.elements[i])
1483 v = solv->decisionq.elements[i];
1484 solv->decisionmap[v > 0 ? v : -v] = v > 0 ? 1 : -1;
1487 if (solv->pool->verbose)
1488 printf("decisions done reduced from %d to %d\n", solv->decisionq.count, i);
1490 solv->decisionq_why.count = i;
1491 solv->decisionq.count = i;
1492 solv->recommends_index = -1;
1493 solv->propagate_index = 0;
1495 /* redo all job/system decisions */
1496 makeruledecisions(solv);
1497 if (solv->pool->verbose)
1498 printf("decisions after adding job and system rules: %d\n", solv->decisionq.count);
1499 /* recreate watches */
1505 * analyze_unsolvable_rule
1509 analyze_unsolvable_rule(Solver *solv, Rule *r)
1512 Id why = r - solv->rules;
1514 if (why >= solv->jobrules && why < solv->systemrules)
1516 if (why >= solv->systemrules && why < solv->weakrules)
1517 printf("SYSTEM %d ", why - solv->systemrules);
1518 if (why >= solv->weakrules && why < solv->learntrules)
1520 if (solv->learntrules && why >= solv->learntrules)
1524 if (solv->learntrules && why >= solv->learntrules)
1526 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1527 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i]);
1530 /* do not add rpm rules to problem */
1531 if (why < solv->jobrules)
1533 /* return if problem already countains the rule */
1534 if (solv->problems.count)
1536 for (i = solv->problems.count - 1; i >= 0; i--)
1537 if (solv->problems.elements[i] == 0)
1539 else if (solv->problems.elements[i] == why)
1542 queuepush(&solv->problems, why);
1547 * analyze_unsolvable
1549 * return: 1 - disabled some rules, try again
1554 analyze_unsolvable(Solver *solv, Rule *r, int disablerules)
1556 Pool *pool = solv->pool;
1557 Map seen; /* global? */
1560 Id *decisionmap = solv->decisionmap;
1561 int oldproblemcount;
1565 printf("ANALYZE UNSOLVABLE ----------------------\n");
1567 oldproblemcount = solv->problems.count;
1568 mapinit(&seen, pool->nsolvables);
1569 analyze_unsolvable_rule(solv, r);
1570 dp = r->d ? pool->whatprovidesdata + r->d : 0;
1581 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1583 vv = v > 0 ? v : -v;
1584 l = solv->decisionmap[vv];
1589 idx = solv->decisionq.count;
1592 v = solv->decisionq.elements[--idx];
1593 vv = v > 0 ? v : -v;
1594 if (!MAPTST(&seen, vv))
1596 why = solv->decisionq_why.elements[idx];
1601 printruleelement(solv, 0, v);
1605 r = solv->rules + why;
1606 analyze_unsolvable_rule(solv, r);
1607 dp = r->d ? pool->whatprovidesdata + r->d : 0;
1618 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1620 vv = v > 0 ? v : -v;
1621 l = solv->decisionmap[vv];
1628 queuepush(&solv->problems, 0); /* mark end of this problem */
1631 if (solv->weakrules != solv->learntrules)
1633 for (i = oldproblemcount; i < solv->problems.count - 1; i++)
1635 why = solv->problems.elements[i];
1636 if (why < solv->weakrules || why >= solv->learntrules)
1638 if (!lastweak || lastweak < why)
1644 /* disable last weak rule */
1645 solv->problems.count = oldproblemcount;
1646 r = solv->rules + lastweak;
1647 printf("disabling weak ");
1653 else if (disablerules)
1655 for (i = oldproblemcount; i < solv->problems.count - 1; i++)
1657 r = solv->rules + solv->problems.elements[i];
1667 /*-----------------------------------------------------------------*/
1668 /* Decision revert */
1672 * revert decision at level
1676 revert(Solver *solv, int level)
1679 while (solv->decisionq.count)
1681 v = solv->decisionq.elements[solv->decisionq.count - 1];
1682 vv = v > 0 ? v : -v;
1683 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1686 printf("reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1688 solv->decisionmap[vv] = 0;
1689 solv->decisionq.count--;
1690 solv->decisionq_why.count--;
1691 solv->propagate_index = solv->decisionq.count;
1693 solv->recommends_index = -1;
1698 * watch2onhighest - put watch2 on literal with highest level
1702 watch2onhighest(Solver *solv, Rule *r)
1708 return; /* binary rule, both watches are set */
1709 dp = solv->pool->whatprovidesdata + r->d;
1710 while ((v = *dp++) != 0)
1712 l = solv->decisionmap[v < 0 ? -v : v];
1729 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules)
1739 solv->decisionmap[decision] = level;
1741 solv->decisionmap[-decision] = -level;
1742 queuepush(&solv->decisionq, decision);
1743 queuepush(&solv->decisionq_why, 0);
1747 r = propagate(solv, level);
1751 return analyze_unsolvable(solv, r, disablerules);
1752 printf("conflict with rule #%d\n", (int)(r - solv->rules));
1753 l = analyze(solv, level, r, &p, &d, &why);
1754 if (l >= level || l <= 0)
1756 printf("reverting decisions (level %d -> %d)\n", level, l);
1758 revert(solv, level);
1759 r = addrule(solv, p, d); /* p requires d */
1762 if (solv->learnt_why.count != (r - solv->rules) - solv->learntrules)
1764 printf("%d %d\n", solv->learnt_why.count, (int)(r - solv->rules) - solv->learntrules);
1767 queuepush(&solv->learnt_why, why);
1770 /* at least 2 literals, needs watches */
1771 watch2onhighest(solv, r);
1772 addwatches(solv, r);
1774 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1775 queuepush(&solv->decisionq, p);
1776 queuepush(&solv->decisionq_why, r - solv->rules);
1777 printf("decision: ");
1778 printruleelement(solv, 0, p);
1779 printf("new rule: ");
1785 /*-----------------------------------------------------------------*/
1786 /* Main solver interface */
1791 * create solver structure
1793 * pool: all available solvables
1794 * system: installed Solvables
1797 * Upon solving, rules are created to flag the Solvables
1798 * of the 'system' Source as installed.
1802 solver_create(Pool *pool, Source *system)
1805 solv = (Solver *)xcalloc(1, sizeof(Solver));
1807 solv->system = system;
1810 queueinit(&solv->ruletojob);
1811 queueinit(&solv->decisionq);
1812 queueinit(&solv->decisionq_why);
1813 queueinit(&solv->problems);
1814 queueinit(&solv->suggestions);
1815 queueinit(&solv->learnt_why);
1816 queueinit(&solv->learnt_pool);
1818 mapinit(&solv->recommendsmap, pool->nsolvables);
1819 mapinit(&solv->suggestsmap, pool->nsolvables);
1820 solv->recommends_index = 0;
1822 solv->decisionmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
1823 solv->rules = (Rule *)xmalloc((solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
1824 memset(solv->rules, 0, sizeof(Rule));
1836 solver_free(Solver *solv)
1838 queuefree(&solv->ruletojob);
1839 queuefree(&solv->decisionq);
1840 queuefree(&solv->decisionq_why);
1841 queuefree(&solv->learnt_why);
1842 queuefree(&solv->learnt_pool);
1843 queuefree(&solv->problems);
1844 queuefree(&solv->suggestions);
1846 mapfree(&solv->recommendsmap);
1847 mapfree(&solv->suggestsmap);
1848 xfree(solv->decisionmap);
1850 xfree(solv->watches);
1851 xfree(solv->weaksystemrules);
1852 xfree(solv->obsoletes);
1853 xfree(solv->obsoletes_data);
1858 /*-------------------------------------------------------*/
1863 * all rules have been set up, not actually run the solver
1868 run_solver(Solver *solv, int disablerules, int doweak)
1876 Pool *pool = solv->pool;
1880 printf("number of rules: %d\n", solv->nrules);
1883 for (i = 0; i < solv->nrules; i++)
1885 printrule(solv, solv->rules + i);
1890 /* all new rules are learnt after this point */
1891 solv->learntrules = solv->nrules;
1892 /* crate watches lists */
1895 if (pool->verbose) printf("initial decisions: %d\n", solv->decisionq.count);
1897 /* start SAT algorithm */
1899 systemlevel = level + 1;
1900 if (pool->verbose) printf("solving...\n");
1911 if (pool->verbose) printf("propagating (%d %d)...\n", solv->propagate_index, solv->decisionq.count);
1912 if ((r = propagate(solv, level)) != 0)
1914 if (analyze_unsolvable(solv, r, disablerules))
1916 printf("UNSOLVABLE\n");
1926 if (level < systemlevel && solv->system->nsolvables)
1928 if (!solv->updatesystem)
1930 /* try to keep as many packages as possible */
1931 if (pool->verbose) printf("installing system packages\n");
1932 for (i = solv->system->start, n = 0; ; i++, n++)
1934 if (n == solv->system->nsolvables)
1936 if (i == solv->system->start + solv->system->nsolvables)
1937 i = solv->system->start;
1938 s = pool->solvables + i;
1939 if (solv->decisionmap[i] != 0)
1942 printf("system installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1945 level = setpropagatelearn(solv, level, i, disablerules);
1948 printf("UNSOLVABLE\n");
1952 if (level <= olevel)
1956 if (solv->weaksystemrules)
1958 if (pool->verbose) printf("installing weak system packages\n");
1959 for (i = solv->system->start, n = 0; ; i++, n++)
1961 if (n == solv->system->nsolvables)
1963 if (solv->decisionmap[i] > 0 || (solv->decisionmap[i] < 0 && solv->weaksystemrules[i - solv->system->start] == 0))
1966 if (solv->decisionmap[i] == 0)
1968 if (solv->weaksystemrules[i - solv->system->start])
1970 dp = pool->whatprovidesdata + solv->weaksystemrules[i - solv->system->start];
1971 while ((p = *dp++) != 0)
1973 if (solv->decisionmap[p] > 0)
1975 if (solv->decisionmap[p] == 0)
1979 continue; /* rule is already true */
1985 prune_to_highest_prio(pool, &dq);
1987 prune_to_recommended(solv, &dq);
1989 prune_best_version_arch(pool, &dq);
1991 s = pool->solvables + dq.elements[0];
1992 printf("weak system installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1995 level = setpropagatelearn(solv, level, dq.elements[0], disablerules);
1998 printf("UNSOLVABLE\n");
2002 if (level <= olevel)
2008 if (n != solv->system->nsolvables)
2011 systemlevel = level;
2018 if (pool->verbose) printf("deciding unresolved rules\n");
2019 for (i = 1, n = 1; ; i++, n++)
2021 if (n == solv->nrules)
2023 if (i == solv->nrules)
2025 r = solv->rules + i;
2031 /* binary or unary rule */
2032 /* need two positive undecided literals */
2033 if (r->p < 0 || r->w2 <= 0)
2035 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
2037 queuepush(&dq, r->p);
2038 queuepush(&dq, r->w2);
2043 * all negative literals are installed
2044 * no positive literal is installed
2045 * i.e. the rule is not fulfilled and we
2046 * just need to decide on the positive literals
2050 if (solv->decisionmap[-r->p] <= 0)
2055 if (solv->decisionmap[r->p] > 0)
2057 if (solv->decisionmap[r->p] == 0)
2058 queuepush(&dq, r->p);
2060 dp = pool->whatprovidesdata + r->d;
2061 while ((p = *dp++) != 0)
2065 if (solv->decisionmap[-p] <= 0)
2070 if (solv->decisionmap[p] > 0)
2072 if (solv->decisionmap[p] == 0)
2081 /* cannot happen as this means that
2082 * the rule is unit */
2086 prune_to_highest_prio(pool, &dq);
2088 prune_to_recommended(solv, &dq);
2090 prune_best_version_arch(pool, &dq);
2091 p = dq.elements[dq.count - 1];
2092 s = pool->solvables + p;
2094 printf("installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2097 level = setpropagatelearn(solv, level, p, disablerules);
2100 printf("UNSOLVABLE\n");
2104 if (level < systemlevel)
2107 } /* for(), decide */
2109 if (n != solv->nrules) /* continue if level < systemlevel */
2112 if (doweak && !solv->problems.count)
2116 if (pool->verbose) printf("installing recommended packages\n");
2118 for (i = 1; i < pool->nsolvables; i++)
2120 if (solv->decisionmap[i] < 0)
2122 if (solv->decisionmap[i] > 0)
2124 Id *recp, rec, *pp, p;
2125 s = pool->solvables + i;
2126 /* installed, check for recommends */
2127 /* XXX need to special case AND ? */
2130 recp = s->source->idarraydata + s->recommends;
2131 while ((rec = *recp++) != 0)
2134 FOR_PROVIDES(p, pp, rec)
2136 if (solv->decisionmap[p] > 0)
2141 else if (solv->decisionmap[p] == 0)
2142 queuepushunique(&dq, p);
2150 s = pool->solvables + i;
2151 if (!s->supplements && !s->freshens)
2153 if (!pool_installable(pool, s))
2157 supp = s->source->idarraydata + s->supplements;
2158 while ((sup = *supp++) != 0)
2159 if (dep_fulfilled(solv, sup))
2166 supp = s->source->idarraydata + s->freshens;
2167 while ((sup = *supp++) != 0)
2168 if (dep_fulfilled(solv, sup))
2173 queuepushunique(&dq, i);
2179 prune_to_highest_prio(pool, &dq);
2181 prune_best_version_arch(pool, &dq);
2182 p = dq.elements[dq.count - 1];
2183 s = pool->solvables + p;
2185 printf("installing recommended %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2187 level = setpropagatelearn(solv, level, p, 0);
2202 refine_suggestion(Solver *solv, Id *problem, Id sug, Queue *refined)
2205 int i, j, sugseen, sugjob = -1;
2210 printf("refine_suggestion start\n");
2212 if (sug >= solv->jobrules && sug < solv->systemrules)
2213 sugjob = solv->ruletojob.elements[sug - solv->jobrules];
2215 queueinit(&disabled);
2216 QUEUEEMPTY(refined);
2217 queuepush(refined, sug);
2219 /* re-enable all rules but rule "sug" of the problem */
2225 r = solv->rules + sug;
2229 for (i = 0; problem[i]; i++)
2231 if (problem[i] == sug)
2236 if (sugjob >= 0 && problem[i] >= solv->jobrules && problem[i] < solv->systemrules && sugjob == solv->ruletojob.elements[problem[i] - solv->jobrules])
2238 /* rule belongs to same job */
2241 r = solv->rules + problem[i];
2248 /* direct assertion */
2249 if (r->p == sugassert && sugseen)
2251 /* also leave this assertion disabled */
2254 v = r->p > 0 ? r->p : -r->p;
2255 if (solv->decisionmap[v])
2257 if ((solv->decisionmap[v] > 0 && r->p < 0) ||
2258 (solv->decisionmap[v] < 0 && r->p > 0))
2260 printf("direct assertion failure, no solution found!\n");
2263 r = solv->rules + problem[i];
2270 if (r->d == 0 || r->w2 != r->p)
2273 r->w1 = solv->pool->whatprovidesdata[r->d];
2277 /* re-enable as many weak rules as possible */
2278 for (i = solv->weakrules; i < solv->learntrules; i++)
2280 r = solv->rules + i;
2283 if (r->d == 0 || r->w2 != r->p)
2286 r->w1 = solv->pool->whatprovidesdata[r->d];
2289 QUEUEEMPTY(&solv->problems);
2290 revert(solv, 1); /* XXX move to reset_solver? */
2292 run_solver(solv, 0, 0);
2293 if (!solv->problems.count)
2295 printf("no more problems!\n");
2297 printdecisions(solv);
2299 break; /* great, no more problems */
2301 disabledcnt = disabled.count;
2302 for (i = 0; i < solv->problems.elements[i]; i++)
2304 /* ignore solutions in refined */
2305 v = solv->problems.elements[i];
2306 for (j = 0; problem[j]; j++)
2307 if (problem[j] != sug && problem[j] == v)
2311 queuepush(&disabled, v);
2312 queuepush(&disabled, 0); /* room for watch */
2314 if (disabled.count == disabledcnt)
2316 /* no solution found, this was an invalid suggestion! */
2317 printf("no solution found!\n");
2321 if (disabled.count == disabledcnt + 2)
2323 /* just one suggestion, add it to refined list */
2324 queuepush(refined, disabled.elements[disabledcnt]);
2328 printf("############################################## more than one solution found.\n");
2330 for (i = 0; i < solv->problems.elements[i]; i++)
2331 printrule(solv, solv->rules + solv->problems.elements[i]);
2332 printf("##############################################\n");
2334 /* more than one solution, keep all disabled */
2336 for (i = disabledcnt; i < disabled.count; i += 2)
2339 r = solv->rules + disabled.elements[i];
2340 disabled.elements[i + 1] = r->w1;
2348 /* enable refined rules again */
2349 for (i = 0; i < disabled.count; i += 2)
2351 r = solv->rules + disabled.elements[i];
2352 r->w1 = disabled.elements[i + 1];
2354 /* disable problem rules again so that we are in the same state as before */
2355 for (i = 0; problem[i]; i++)
2357 r = solv->rules + problem[i];
2360 printf("refine_suggestion end\n");
2369 id2rc(Solver *solv, Id id)
2372 if (solv->rc_output != 2)
2374 evr = id2str(solv->pool, id);
2375 if (*evr < '0' || *evr > '9')
2377 while (*evr >= '0' && *evr <= '9')
2385 printdecisions(Solver *solv)
2387 Pool *pool = solv->pool;
2388 Id p, *obsoletesmap;
2392 obsoletesmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
2393 for (i = 0; i < solv->decisionq.count; i++)
2398 n = solv->decisionq.elements[i];
2401 if (n == SYSTEMSOLVABLE)
2403 if (n >= solv->system->start && n < solv->system->start + solv->system->nsolvables)
2405 s = pool->solvables + n;
2408 obsp = s->source->idarraydata + s->obsoletes;
2409 while ((obs = *obsp++) != 0)
2410 FOR_PROVIDES(p, pp, obs)
2412 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2414 obsoletesmap[p] = n;
2419 FOR_PROVIDES(p, pp, s->name)
2420 if (s->name == pool->solvables[p].name)
2422 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2424 obsoletesmap[p] = n;
2430 if (solv->rc_output)
2431 printf(">!> Solution #1:\n");
2433 int installs = 0, uninstalls = 0, upgrades = 0;
2435 /* print solvables to be erased */
2437 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2439 if (solv->decisionmap[i] > 0)
2441 if (obsoletesmap[i])
2443 s = pool->solvables + i;
2444 if (solv->rc_output == 2)
2445 printf(">!> remove %s-%s%s\n", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2446 else if (solv->rc_output)
2447 printf(">!> remove %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2449 printf("erase %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2453 /* print solvables to be installed */
2455 for (i = 0; i < solv->decisionq.count; i++)
2458 p = solv->decisionq.elements[i];
2461 if (p == SYSTEMSOLVABLE)
2463 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2465 s = pool->solvables + p;
2467 if (!obsoletesmap[p])
2469 if (solv->rc_output)
2471 printf("install %s-%s%s", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2472 if (solv->rc_output != 2)
2473 printf(".%s", id2str(pool, s->arch));
2476 else if (!solv->rc_output)
2478 printf("update %s-%s.%s (obsoletes", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2479 for (j = solv->system->start; j < solv->system->start + solv->system->nsolvables; j++)
2481 if (obsoletesmap[j] != p)
2483 s = pool->solvables + j;
2484 printf(" %s-%s.%s", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2491 Solvable *f, *fn = 0;
2492 for (j = solv->system->start; j < solv->system->start + solv->system->nsolvables; j++)
2494 if (obsoletesmap[j] != p)
2496 f = pool->solvables + j;
2497 if (fn || f->name != s->name)
2499 if (solv->rc_output == 2)
2500 printf(">!> remove %s-%s%s\n", id2str(pool, f->name), id2rc(solv, f->evr), id2str(pool, f->evr));
2501 else if (solv->rc_output)
2502 printf(">!> remove %s-%s.%s\n", id2str(pool, f->name), id2str(pool, f->evr), id2str(pool, f->arch));
2510 printf(">!> install %s-%s%s", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2511 if (solv->rc_output != 2)
2512 printf(".%s", id2str(pool, s->arch));
2517 if (solv->rc_output == 2)
2518 printf(">!> upgrade %s-%s => %s-%s%s", id2str(pool, fn->name), id2str(pool, fn->evr), id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2520 printf(">!> upgrade %s-%s.%s => %s-%s.%s", id2str(pool, fn->name), id2str(pool, fn->evr), id2str(pool, fn->arch), id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2524 if (solv->rc_output)
2526 Source *source = s->source;
2527 if (source && strcmp(source_name(source), "locales"))
2528 printf("[%s]", source_name(source));
2533 if (solv->rc_output)
2534 printf(">!> installs=%d, upgrades=%d, uninstalls=%d\n", installs, upgrades, uninstalls);
2536 xfree(obsoletesmap);
2540 create_obsolete_index(Solver *solv)
2542 Pool *pool = solv->pool;
2544 Source *system = solv->system;
2545 Id p, *pp, obs, *obsp, *obsoletes, *obsoletes_data;
2548 /* create reverse obsoletes map for system solvables */
2549 solv->obsoletes = obsoletes = xcalloc(system->nsolvables, sizeof(Id));
2550 for (i = 1; i < pool->nsolvables; i++)
2552 s = pool->solvables + i;
2555 if (!pool_installable(pool, s))
2557 obsp = s->source->idarraydata + s->obsoletes;
2558 while ((obs = *obsp++) != 0)
2559 FOR_PROVIDES(p, pp, obs)
2561 if (p < system->start || p >= system->start + system->nsolvables)
2563 if (pool->solvables[p].name == s->name)
2565 obsoletes[p - system->start]++;
2569 for (i = 0; i < system->nsolvables; i++)
2572 n += obsoletes[i] + 1;
2575 solv->obsoletes_data = obsoletes_data = xcalloc(n + 1, sizeof(Id));
2576 if (pool->verbose) printf("obsoletes data: %d entries\n", n + 1);
2577 for (i = pool->nsolvables - 1; i > 0; i--)
2579 s = pool->solvables + i;
2582 if (!pool_installable(pool, s))
2584 obsp = s->source->idarraydata + s->obsoletes;
2585 while ((obs = *obsp++) != 0)
2586 FOR_PROVIDES(p, pp, obs)
2588 if (p < system->start || p >= system->start + system->nsolvables)
2590 if (pool->solvables[p].name == s->name)
2593 if (obsoletes_data[obsoletes[p]] != i)
2594 obsoletes_data[--obsoletes[p]] = i;
2599 /*-----------------------------------------------------------------*/
2609 solve(Solver *solv, Queue *job)
2611 Pool *pool = solv->pool;
2613 Map addedmap; /* '1' == have rule for solvable */
2614 Map noupdaterule; /* '1' == don't update (scheduled for removal) */
2615 Id how, what, p, *pp, d;
2621 * create basic rule set of all involved packages
2626 mapinit(&addedmap, pool->nsolvables);
2627 mapinit(&noupdaterule, pool->nsolvables);
2632 * always install our system solvable
2634 MAPSET(&addedmap, SYSTEMSOLVABLE);
2635 queuepush(&solv->decisionq, SYSTEMSOLVABLE);
2636 queuepush(&solv->decisionq_why, 0);
2637 solv->decisionmap[SYSTEMSOLVABLE] = 1;
2640 * create rules for installed solvables -> keep them installed
2641 * so called: rpm rules
2645 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2646 addrulesforsolvable(solv, pool->solvables + i, &addedmap);
2649 * create install rules
2651 * two passes, as we want to keep the rpm rules distinct from the job rules
2655 if (solv->noupdateprovide && solv->system->nsolvables)
2656 create_obsolete_index(solv);
2660 * process job rules for solvables
2663 for (i = 0; i < job->count; i += 2)
2665 how = job->elements[i];
2666 what = job->elements[i + 1];
2670 case SOLVER_INSTALL_SOLVABLE:
2671 addrulesforsolvable(solv, pool->solvables + what, &addedmap);
2673 case SOLVER_INSTALL_SOLVABLE_NAME:
2674 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
2676 FOR_PROVIDES(p, pp, what)
2678 /* if by name, ensure that the name matches */
2679 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
2681 addrulesforsolvable(solv, pool->solvables + p, &addedmap);
2684 case SOLVER_INSTALL_SOLVABLE_UPDATE:
2685 /* dont allow downgrade */
2686 addupdaterule(solv, pool->solvables + what, &addedmap, 0, 0, 1);
2692 * if unstalls are disallowed, add update rules for every
2693 * installed solvables in the hope to circumvent uninstall
2699 if (!solv->allowuninstall)
2701 /* add update rule for every installed package */
2702 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2703 addupdaterule(solv, pool->solvables + i, &addedmap, solv->allowdowngrade, solv->allowarchchange, 1);
2705 #else /* this is just to add the needed rpm rules to our set */
2706 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2707 addupdaterule(solv, pool->solvables + i, &addedmap, 1, 1, 1);
2710 addrulesforweak(solv, &addedmap);
2714 int possible = 0, installable = 0;
2715 for (i = 1; i < pool->nsolvables; i++)
2717 if (pool_installable(pool, pool->solvables + i))
2719 if (MAPTST(&addedmap, i))
2722 printf("%d of %d installable solvables used for solving\n", possible, installable);
2729 * unify existing rules before going over all job rules
2733 unifyrules(solv); /* remove duplicate rpm rules */
2736 * at this point the system is always solvable,
2737 * as an empty system (remove all packages) is a valid solution
2739 if (pool->verbose) printf("decisions based on rpms: %d\n", solv->decisionq.count);
2742 * now add all job rules
2745 solv->jobrules = solv->nrules;
2747 for (i = 0; i < job->count; i += 2)
2749 how = job->elements[i];
2750 what = job->elements[i + 1];
2753 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
2754 if (solv->rc_output) {
2755 Solvable *s = pool->solvables + what;
2756 printf(">!> Installing %s from channel %s\n", id2str(pool, s->name), source_name(s->source));
2758 addrule(solv, what, 0); /* install by Id */
2759 queuepush(&solv->ruletojob, i);
2761 case SOLVER_ERASE_SOLVABLE:
2762 addrule(solv, -what, 0); /* remove by Id */
2763 queuepush(&solv->ruletojob, i);
2764 MAPSET(&noupdaterule, what);
2766 case SOLVER_INSTALL_SOLVABLE_NAME: /* install by capability */
2767 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
2769 FOR_PROVIDES(p, pp, what) /* check all providers */
2771 /* if by name, ensure that the name matches */
2772 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
2776 if (!q.count) { /* no provider found -> abort */
2777 fprintf(stderr, "Nothing provides '%s'\n", id2str(pool, what));
2778 /* XXX make this a problem! */
2783 p = queueshift(&q); /* get first provider */
2785 d = 0; /* single provider ? -> make assertion */
2787 d = pool_queuetowhatprovides(pool, &q); /* get all providers */
2788 addrule(solv, p, d); /* add 'requires' rule */
2789 queuepush(&solv->ruletojob, i);
2791 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
2792 case SOLVER_ERASE_SOLVABLE_PROVIDES:
2793 FOR_PROVIDES(p, pp, what)
2795 /* if by name, ensure that the name matches */
2796 if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
2799 addrule(solv, -p, 0); /* add 'remove' rule */
2800 queuepush(&solv->ruletojob, i);
2801 MAPSET(&noupdaterule, p);
2804 case SOLVER_INSTALL_SOLVABLE_UPDATE: /* find update for solvable */
2805 addupdaterule(solv, pool->solvables + what, &addedmap, 0, 0, 0);
2806 queuepush(&solv->ruletojob, i);
2811 if (solv->ruletojob.count != solv->nrules - solv->jobrules)
2814 if (pool->verbose) printf("problems so far: %d\n", solv->problems.count);
2817 * now add policy rules
2821 solv->systemrules = solv->nrules;
2824 * create rules for updating installed solvables
2830 if (!solv->allowuninstall)
2831 { /* loop over all installed solvables */
2832 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2834 if (!MAPTST(&noupdaterule, i)) /* if not marked as 'noupdate' */
2835 addupdaterule(solv, pool->solvables + i, &addedmap, solv->allowdowngrade, solv->allowarchchange, 0);
2837 addrule(solv, 0, 0); /* place holder */
2839 /* consistency check: we added a rule for _every_ system solvable */
2840 if (solv->nrules - solv->systemrules != solv->system->nsolvables)
2844 if (pool->verbose) printf("problems so far: %d\n", solv->problems.count);
2846 /* create special weak system rules */
2847 if (solv->system->nsolvables)
2849 solv->weaksystemrules = xcalloc(solv->system->nsolvables, sizeof(Id));
2850 for (i = 0; i < solv->system->nsolvables; i++)
2852 if (MAPTST(&noupdaterule, solv->system->start + i))
2854 findupdatepackages(solv, pool->solvables + solv->system->start + i, &q, (Map *)0, 1, 1);
2856 solv->weaksystemrules[i] = pool_queuetowhatprovides(pool, &q);
2860 /* free unneeded memory */
2862 mapfree(&noupdaterule);
2865 solv->weakrules = solv->nrules;
2867 /* try real hard to keep packages installed */
2870 for (i = 0; i < solv->system->nsolvables; i++)
2872 d = solv->weaksystemrules[i];
2873 addrule(solv, solv->system->start + i, d);
2882 makeruledecisions(solv);
2883 run_solver(solv, 1, 1);
2885 /* find suggested packages */
2886 if (!solv->problems.count)
2888 Id sug, *sugp, enh, *enhp, p, *pp;
2890 /* create map of all suggests that are still open */
2891 solv->recommends_index = -1;
2892 MAPZERO(&solv->suggestsmap);
2893 for (i = 0; i < solv->decisionq.count; i++)
2895 p = solv->decisionq.elements[i];
2898 s = pool->solvables + p;
2901 sugp = s->source->idarraydata + s->suggests;
2902 while ((sug = *sugp++) != 0)
2904 FOR_PROVIDES(p, pp, sug)
2905 if (solv->decisionmap[p] > 0)
2908 continue; /* already fulfilled */
2909 FOR_PROVIDES(p, pp, sug)
2910 MAPSET(&solv->suggestsmap, p);
2914 for (i = 1; i < pool->nsolvables; i++)
2916 if (solv->decisionmap[i] != 0)
2918 s = pool->solvables + i;
2919 if (!MAPTST(&solv->suggestsmap, i))
2923 if (!pool_installable(pool, s))
2925 enhp = s->source->idarraydata + s->enhances;
2926 while ((enh = *enhp++) != 0)
2927 if (dep_fulfilled(solv, enh))
2932 queuepush(&solv->suggestions, i);
2934 prune_best_version_arch(pool, &solv->suggestions);
2939 * print solver result
2943 if (pool->verbose) printf("-------------------------------------------------------------\n");
2945 if (solv->problems.count)
2955 clonequeue(&problems, &solv->problems);
2956 queueinit(&solution);
2957 printf("Encountered problems! Here are the solutions:\n");
2958 problem = problems.elements;
2959 for (i = 0; i < problems.count; i++)
2961 Id v = problems.elements[i];
2964 printf("====================================\n");
2965 problem = problems.elements + i + 1;
2968 if (v >= solv->jobrules && v < solv->systemrules)
2970 ji = solv->ruletojob.elements[v - solv->jobrules];
2973 if (problem[j] >= solv->jobrules && problem[j] < solv->systemrules && ji == solv->ruletojob.elements[problem[j] - solv->jobrules])
2976 if (problem + j < problems.elements + i)
2979 refine_suggestion(solv, problem, v, &solution);
2980 for (j = 0; j < solution.count; j++)
2982 r = solv->rules + solution.elements[j];
2983 why = solution.elements[j];
2987 if (why >= solv->jobrules && why < solv->systemrules)
2989 ji = solv->ruletojob.elements[why - solv->jobrules];
2990 what = job->elements[ji + 1];
2991 switch (job->elements[ji])
2993 case SOLVER_INSTALL_SOLVABLE:
2994 s = pool->solvables + what;
2995 printf("- do not install %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2997 case SOLVER_ERASE_SOLVABLE:
2998 s = pool->solvables + what;
2999 printf("- do not deinstall %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
3001 case SOLVER_INSTALL_SOLVABLE_NAME:
3002 printf("- do not install %s\n", id2str(pool, what));
3004 case SOLVER_ERASE_SOLVABLE_NAME:
3005 printf("- do not deinstall %s\n", id2str(pool, what));
3007 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3008 printf("- do not install a solvable providing %s\n", dep2str(pool, what));
3010 case SOLVER_ERASE_SOLVABLE_PROVIDES:
3011 printf("- do not deinstall all solvables providing %s\n", dep2str(pool, what));
3013 case SOLVER_INSTALL_SOLVABLE_UPDATE:
3014 s = pool->solvables + what;
3015 printf("- do not install most recent version of %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
3018 printf("- do something different\n");
3022 else if (why >= solv->systemrules && why < solv->weakrules)
3025 s = pool->solvables + solv->system->start + (why - solv->systemrules);
3026 if (solv->weaksystemrules && solv->weaksystemrules[why - solv->systemrules])
3028 Id *dp = pool->whatprovidesdata + solv->weaksystemrules[why - solv->systemrules];
3031 if (*dp >= solv->system->start && *dp < solv->system->start + solv->system->nsolvables)
3033 if (solv->decisionmap[*dp] > 0)
3035 sd = pool->solvables + *dp;
3043 if (evrcmp(pool, sd->evr, s->evr) < 0)
3045 printf("- allow downgrade of %s-%s.%s to %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), id2str(pool, sd->name), id2str(pool, sd->evr), id2str(pool, sd->arch));
3048 if (!solv->allowarchchange && archchanges(pool, sd, s))
3050 printf("- allow architecture change of %s-%s.%s to %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), id2str(pool, sd->name), id2str(pool, sd->evr), id2str(pool, sd->arch));
3054 printf("- allow replacement of %s-%s.%s with %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), id2str(pool, sd->name), id2str(pool, sd->evr), id2str(pool, sd->arch));
3058 printf("- allow deinstallation of %s-%s.%s [%ld]\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), (long int)(s - pool->solvables));
3066 printf("------------------------------------\n");
3071 printdecisions(solv);
3072 if (solv->suggestions.count)
3074 printf("\nsuggested packages:\n");
3075 for (i = 0; i < solv->suggestions.count; i++)
3077 s = pool->solvables + solv->suggestions.elements[i];
3078 printf("- %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));