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 Repo *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->repo->priority > bestprio)
148 bestprio = s->repo->priority;
150 for (i = j = 0; i < plist->count; i++)
152 s = pool->solvables + plist->elements[i];
153 if (s->repo->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->repo->idarraydata + s->recommends;
188 while ((rec = *recp++) != 0)
189 FOR_PROVIDES(p, pp, rec)
190 MAPSET(&solv->recommendsmap, p);
194 sugp = s->repo->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->repo->idarraydata + s->supplements;
215 while ((sup = *supp++) != 0)
216 if (dep_fulfilled(solv, sup))
223 supp = s->repo->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->repo->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));
511 if (solv->pool->verbose)
518 for (i = 1; i < solv->nrules; i++)
525 dp = solv->pool->whatprovidesdata + r->d;
530 printf(" binary: %d\n", binr);
531 printf(" normal: %d, %d literals\n", solv->nrules - 1 - binr, lits);
543 hashrule(Solver *solv, Id p, Id d, int n)
545 unsigned int x = (unsigned int)p;
549 return (x * 37) ^ (unsigned int)d;
550 dp = solv->pool->whatprovidesdata + d;
552 x = (x * 37) ^ (unsigned int)*dp++;
560 * p = direct literal; > 0 for learnt, < 0 for installed pkg (rpm)
561 * d, if < 0 direct literal, if > 0 offset into whatprovides, if == 0 rule is assertion (look at p only)
564 * A requires b, b provided by B1,B2,B3 => (-A|B1|B2|B3)
566 * p < 0 : rule from rpm (installed pkg)
567 * d > 0 : Offset in whatprovidesdata (list of providers)
569 * A conflicts b, b provided by B1,B2,B3 => (-A|-B1), (-A|-B2), (-A|-B3)
570 * d < 0: Id of solvable (e.g. B1)
572 * d == 0: unary rule, assertion => (A) or (-A)
574 * Install: p > 0, d = 0 (A) user requested install
575 * Remove: p < 0, d = 0 (-A) user requested remove
576 * Requires: p < 0, d > 0 (-A|B1|B2|...) d: <list of providers for requirement of p>
577 * Updates: p > 0, d > 0 (A|B1|B2|...) d: <list of updates for solvable p>
578 * Conflicts: p < 0, d < 0 (-A|-B) either p (conflict issuer) or d (conflict provider)
579 * ? p > 0, d < 0 (A|-B)
580 * No-op ?: p = 0, d = 0 (null) (used as policy rule placeholder)
584 addrule(Solver *solv, Id p, Id d)
589 int n = 0; /* number of literals in rule - 1
590 0 = direct assertion (single literal)
594 /* it often happenes that requires lead to adding the same rpm rule
595 * multiple times, so we prune those duplicates right away to make
596 * the work for unifyrules a bit easier */
598 if (solv->nrules && !solv->jobrules)
600 r = solv->rules + solv->nrules - 1; /* get the last added rule */
601 if (r->p == p && r->d == d && d != 0) /* identical and not user requested */
608 return 0; /* ignore self conflict */
611 else if (d == 0) /* user requested */
615 for (dp = solv->pool->whatprovidesdata + d; *dp; dp++, n++)
617 return 0; /* rule is self-fulfilling */
622 if (n == 0) /* direct assertion */
626 /* this is a rpm rule assertion, we do not have to allocate it */
627 /* it can be identified by a level of 1 and a zero reason */
628 /* we must not drop those rules from the decisionq when rewinding! */
631 if (solv->decisionmap[-p] > 0 || solv->decisionmap[-p] < -1)
633 if (solv->decisionmap[-p])
635 queue_push(&solv->decisionq, p);
636 queue_push(&solv->decisionq_why, 0);
637 solv->decisionmap[-p] = -1;
641 else if (n == 1 && p > d)
643 /* smallest literal first so we can find dups */
647 n = 1; /* re-set n, was used as temp var */
650 /* check if the last added rule is exactly the same as what we're looking for. */
651 if (r && n == 1 && !r->d && r->p == p && r->w2 == d)
654 if (r && n > 1 && r->d && r->p == p)
659 dp2 = solv->pool->whatprovidesdata + r->d;
660 for (dp = solv->pool->whatprovidesdata + d; *dp; dp++, dp2++)
671 /* check and extend rule buffer */
672 if ((solv->nrules & RULES_BLOCK) == 0)
674 solv->rules = (Rule *)xrealloc(solv->rules, (solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
677 r = solv->rules + solv->nrules++; /* point to rule space */
682 /* direct assertion, no watch needed */
698 r->w2 = solv->pool->whatprovidesdata[d];
706 /* go through system and job rules and add direct assertions
707 * to the decisionqueue. If we find a conflict, disable rules and
708 * add them to problem queue.
711 makeruledecisions(Solver *solv)
717 /* no learnt rules for now */
718 if (solv->learntrules && solv->learntrules != solv->nrules)
721 for (ri = solv->jobrules, r = solv->rules + ri; ri < solv->nrules; ri++, r++)
727 if (solv->decisionmap[vv] == 0)
729 queue_push(&solv->decisionq, v);
730 queue_push(&solv->decisionq_why, r - solv->rules);
731 solv->decisionmap[vv] = v > 0 ? 1 : -1;
734 if (v > 0 && solv->decisionmap[vv] > 0)
736 if (v < 0 && solv->decisionmap[vv] < 0)
738 /* found a conflict! */
739 /* if we are weak, just disable ourself */
740 if (ri >= solv->weakrules)
742 printf("conflict, but I am weak, disabling ");
747 for (i = 0; i < solv->decisionq.count; i++)
748 if (solv->decisionq.elements[i] == -v)
750 if (i == solv->decisionq.count)
752 if (solv->decisionq_why.elements[i] == 0)
754 /* conflict with rpm rule, need only disable our rule */
755 printf("conflict with rpm rule, disabling rule #%d\n", ri);
756 queue_push(&solv->problems, r - solv->rules);
757 queue_push(&solv->problems, 0);
758 r->w1 = 0; /* disable */
761 /* conflict with another job or system rule */
762 /* remove old decision */
763 printf("conflicting system/job rules over literal %d\n", vv);
764 solv->decisionmap[vv] = 0;
765 for (; i + 1 < solv->decisionq.count; i++)
767 solv->decisionq.elements[i] = solv->decisionq.elements[i + 1];
768 solv->decisionq_why.elements[i] = solv->decisionq_why.elements[i + 1];
770 solv->decisionq.count--;
771 solv->decisionq_why.count--;
772 /* push all of our rules asserting this literal on the problem stack */
773 for (i = solv->jobrules, rr = solv->rules + i; i < solv->nrules; i++, rr++)
775 if (!rr->w1 || rr->w2)
777 if (rr->p != v && rr->p != -v)
779 printf(" - disabling rule #%d\n", i);
780 queue_push(&solv->problems, i);
781 rr->w1 = 0; /* disable */
783 queue_push(&solv->problems, 0);
789 * add (install) rules for solvable
794 addrulesforsolvable(Solver *solv, Solvable *s, Map *m)
796 Pool *pool = solv->pool;
797 Repo *system = solv->system;
811 queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
812 queue_push(&q, s - pool->solvables); /* push solvable Id */
818 * s: Pointer to solvable
822 if (MAPTST(m, n)) /* continue if already done */
826 s = pool->solvables + n; /* s = Solvable in question */
831 && n >= system->start /* is it installed? */
832 && n < system->start + system->nsolvables)
834 dontfix = 1; /* dont care about broken rpm deps */
837 /*-----------------------------------------
838 * check requires of s
843 reqp = s->repo->idarraydata + s->requires;
844 while ((req = *reqp++) != 0)
846 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
849 dp = GET_PROVIDESP(req, p); /* get providers of req */
851 if (*dp == SYSTEMSOLVABLE) /* always installed */
856 /* the strategy here is to not insist on dependencies
857 * that are already broken. so if we find one provider
858 * that was already installed, we know that the
859 * dependency was not broken before so we enforce it */
860 for (i = 0; dp[i]; i++) /* for all providers */
862 if (dp[i] >= system->start && dp[i] < system->start + system->nsolvables)
863 break; /* provider was installed */
865 if (!dp[i]) /* previously broken dependency */
867 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));
874 /* nothing provides req! */
876 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));
878 addrule(solv, -n, 0); /* mark requestor as uninstallable */
880 printf(">!> !unflag %s-%s.%s[%s]\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), repo_name(s->repo));
885 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);
886 for (i = 0; dp[i]; i++)
887 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));
889 /* add 'requires' dependency */
890 /* rule: (-requestor|provider1|provider2|...|providerN) */
891 addrule(solv, -n, dp - pool->whatprovidesdata);
893 /* descend the dependency tree */
894 for (; *dp; dp++) /* loop through all providers */
900 } /* while, requirements of n */
902 } /* if, requirements */
905 /*-----------------------------------------
906 * check conflicts of s
911 conp = s->repo->idarraydata + s->conflicts;
912 while ((con = *conp++) != 0)
914 FOR_PROVIDES(p, pp, con)
916 /* dontfix: dont care about conflicts with already installed packs */
917 if (dontfix && p >= system->start && p < system->start + system->nsolvables)
919 /* rule: -n|-p: either solvable _or_ provider of conflict */
920 addrule(solv, -n, -p);
925 /*-----------------------------------------
926 * check obsoletes if not installed
928 if (!system || n < system->start || n >= (system->start + system->nsolvables))
929 { /* not installed */
932 obsp = s->repo->idarraydata + s->obsoletes;
933 while ((obs = *obsp++) != 0)
935 FOR_PROVIDES(p, pp, obs)
936 addrule(solv, -n, -p);
939 FOR_PROVIDES(p, pp, s->name)
941 if (s->name == pool->solvables[p].name)
942 addrule(solv, -n, -p);
946 /*-----------------------------------------
947 * add recommends to the rule list
951 recp = s->repo->idarraydata + s->recommends;
952 while ((rec = *recp++) != 0)
954 FOR_PROVIDES(p, pp, rec)
961 sugp = s->repo->idarraydata + s->suggests;
962 while ((sug = *sugp++) != 0)
964 FOR_PROVIDES(p, pp, sug)
974 addrulesforweak(Solver *solv, Map *m)
976 Pool *pool = solv->pool;
981 if (pool->verbose) printf("addrulesforweak... (%d)\n", solv->nrules);
982 for (i = n = 1; n < pool->nsolvables; i++, n++)
984 if (i == pool->nsolvables)
988 s = pool->solvables + i;
989 if (!pool_installable(pool, s))
994 supp = s->repo->idarraydata + s->supplements;
995 while ((sup = *supp++) != ID_NULL)
996 if (dep_possible(solv, sup, m))
999 if (!sup && s->freshens)
1001 supp = s->repo->idarraydata + s->freshens;
1002 while ((sup = *supp++) != ID_NULL)
1003 if (dep_possible(solv, sup, m))
1006 if (!sup && s->enhances)
1008 supp = s->repo->idarraydata + s->enhances;
1009 while ((sup = *supp++) != ID_NULL)
1010 if (dep_possible(solv, sup, m))
1015 addrulesforsolvable(solv, s, m);
1018 if (pool->verbose) printf("done. (%d)\n", solv->nrules);
1022 archchanges(Pool *pool, Solvable *s1, Solvable *s2)
1024 Id a1 = s1->arch, a2 = s2->arch;
1026 /* we allow changes to/from noarch */
1027 if (a1 == a2 || a1 == ARCH_NOARCH || a2 == ARCH_NOARCH)
1031 a1 = a1 <= pool->lastarch ? pool->id2arch[a1] : 0;
1032 a2 = a2 <= pool->lastarch ? pool->id2arch[a2] : 0;
1033 if (((a1 ^ a2) & 0xffff0000) != 0)
1040 findupdatepackages(Solver *solv, Solvable *s, Queue *qs, Map *m, int allowdowngrade, int allowarchchange)
1042 /* system packages get a special upgrade allowed rule */
1043 Pool *pool = solv->pool;
1044 Id p, *pp, n, p2, *pp2;
1053 n = s - pool->solvables;
1054 if (m && !MAPTST(m, n)) /* add rule for s if not already done */
1055 addrulesforsolvable(solv, s, m);
1058 * look for updates for s
1060 FOR_PROVIDES(p, pp, s->name) /* every provider of s' name */
1062 if (p == n) /* skip itself */
1065 ps = pool->solvables + p;
1066 if (s->name == ps->name) /* name match */
1068 if (!allowdowngrade /* consider downgrades ? */
1069 && evrcmp(pool, s->evr, ps->evr) > 0)
1072 if (!allowarchchange && archchanges(pool, s, ps))
1075 else if (!solv->noupdateprovide && ps->obsoletes) /* provides/obsoletes combination ? */
1077 obsp = ps->repo->idarraydata + ps->obsoletes;
1078 while ((obs = *obsp++) != 0) /* for all obsoletes */
1080 FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
1082 if (p2 == n) /* match ! */
1085 if (p2) /* match! */
1088 if (!obs) /* continue if no match */
1090 /* here we have 'p' with a matching provides/obsoletes combination
1091 * thus flagging p as a valid update candidate for s
1098 if (m && !MAPTST(m, p)) /* mark p for install if not already done */
1099 addrulesforsolvable(solv, pool->solvables + p, m);
1101 if (solv->noupdateprovide && solv->obsoletes && solv->obsoletes[n - solv->system->start])
1103 for (pp = solv->obsoletes_data + solv->obsoletes[n - solv->system->start]; (p = *pp++) != 0;)
1106 if (m && !MAPTST(m, p)) /* mark p for install if not already done */
1107 addrulesforsolvable(solv, pool->solvables + p, m);
1113 * add rule for update
1114 * (A|A1|A2|A3...) An = update candidates for A
1116 * s = (installed) solvable
1117 * m = 'addedmap', bit set if 'install' rule for solvable exists
1121 addupdaterule(Solver *solv, Solvable *s, Map *m, int allowdowngrade, int allowarchchange, int dontaddrule)
1123 /* system packages get a special upgrade allowed rule */
1124 Pool *pool = solv->pool;
1130 queue_init_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1131 findupdatepackages(solv, s, &qs, m, allowdowngrade, allowarchchange);
1132 p = s - pool->solvables;
1133 if (dontaddrule) /* we consider update candidates but dont force them */
1139 if (qs.count == 0) /* no updates found */
1142 printf("new update rule: must keep %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1144 addrule(solv, p, 0); /* request 'install' of s */
1149 d = pool_queuetowhatprovides(pool, &qs); /* intern computed provider queue */
1151 r = addrule(solv, p, d); /* allow update of s */
1153 printf("new update rule ");
1160 /*-----------------------------------------------------------------*/
1167 * initial setup for all watches
1171 makewatches(Solver *solv)
1175 int nsolvables = solv->pool->nsolvables;
1177 xfree(solv->watches);
1178 /* lower half for removals, upper half for installs */
1179 solv->watches = (Id *)xcalloc(2 * nsolvables, sizeof(Id));
1181 /* do it reverse so rpm rules get triggered first */
1182 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
1184 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
1187 if (!r->w1 /* rule is disabled */
1188 || !r->w2) /* rule is assertion */
1191 /* see addwatches(solv, r) */
1192 r->n1 = solv->watches[nsolvables + r->w1];
1193 solv->watches[nsolvables + r->w1] = r - solv->rules;
1195 r->n2 = solv->watches[nsolvables + r->w2];
1196 solv->watches[nsolvables + r->w2] = r - solv->rules;
1202 * add watches (for rule)
1206 addwatches(Solver *solv, Rule *r)
1208 int nsolvables = solv->pool->nsolvables;
1210 r->n1 = solv->watches[nsolvables + r->w1];
1211 solv->watches[nsolvables + r->w1] = r - solv->rules;
1213 r->n2 = solv->watches[nsolvables + r->w2];
1214 solv->watches[nsolvables + r->w2] = r - solv->rules;
1218 /*-----------------------------------------------------------------*/
1219 /* rule propagation */
1221 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
1226 * propagate decision to all rules
1230 propagate(Solver *solv, int level)
1232 Pool *pool = solv->pool;
1237 Id *decisionmap = solv->decisionmap;
1238 Id *watches = solv->watches + pool->nsolvables;
1240 while (solv->propagate_index < solv->decisionq.count)
1242 /* negative because our watches trigger if literal goes FALSE */
1243 pkg = -solv->decisionq.elements[solv->propagate_index++];
1245 printf("popagate for decision %d level %d\n", -pkg, level);
1246 printruleelement(solv, 0, -pkg);
1248 for (rp = watches + pkg; *rp; rp = nrp)
1250 r = solv->rules + *rp;
1252 printf(" watch triggered ");
1265 /* if clause is TRUE, nothing to do */
1266 if (DECISIONMAP_TRUE(ow))
1271 /* not a binary clause, check if we need to move our watch */
1272 if (r->p && r->p != ow && !DECISIONMAP_TRUE(-r->p))
1275 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
1276 if (p != ow && !DECISIONMAP_TRUE(-p))
1280 /* p is free to watch, move watch to p */
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));
1285 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));
1299 watches[p] = r - solv->rules;
1303 /* unit clause found, set other watch to TRUE */
1304 if (DECISIONMAP_TRUE(-ow))
1305 return r; /* eek, a conflict! */
1311 decisionmap[ow] = level;
1313 decisionmap[-ow] = -level;
1314 queue_push(&solv->decisionq, ow);
1315 queue_push(&solv->decisionq_why, r - solv->rules);
1318 Solvable *s = pool->solvables + (ow > 0 ? ow : -ow);
1320 printf(" -> decided to install %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1322 printf(" -> decided to conflict %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1327 return 0; /* all is well */
1331 /*-----------------------------------------------------------------*/
1340 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *why)
1342 Pool *pool = solv->pool;
1345 Map seen; /* global? */
1349 int learnt_why = solv->learnt_pool.count;
1350 Id *decisionmap = solv->decisionmap;
1354 if (pool->verbose) printf("ANALYZE at %d ----------------------\n", level);
1355 map_init(&seen, pool->nsolvables);
1356 idx = solv->decisionq.count;
1360 queue_push(&solv->learnt_pool, c - solv->rules);
1361 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1372 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1374 vv = v > 0 ? v : -v;
1375 if (MAPTST(&seen, vv))
1377 l = solv->decisionmap[vv];
1384 for (j = 0; j < solv->decisionq.count; j++)
1385 if (solv->decisionq.elements[j] == v)
1387 if (j == solv->decisionq.count)
1389 queue_push(&rulq, -(j + 1));
1391 continue; /* initial setting */
1395 num++; /* need to do this one as well */
1400 printf("PUSH %d ", v);
1401 printruleelement(solv, 0, v);
1408 printf("num = %d\n", num);
1414 v = solv->decisionq.elements[--idx];
1415 vv = v > 0 ? v : -v;
1416 if (MAPTST(&seen, vv))
1419 c = solv->rules + solv->decisionq_why.elements[idx];
1427 else if (r.count == 1 && r.elements[0] < 0)
1428 *dr = r.elements[0];
1430 *dr = pool_queuetowhatprovides(pool, &r);
1433 printf("learned rule for level %d (am %d)\n", rlevel, level);
1434 printruleelement(solv, 0, -v);
1435 for (i = 0; i < r.count; i++)
1438 printruleelement(solv, 0, v);
1442 queue_push(&solv->learnt_pool, 0);
1444 for (i = learnt_why; solv->learnt_pool.elements[i]; i++)
1446 printf("learnt_why ");
1447 printrule(solv, solv->rules + solv->learnt_pool.elements[i]);
1458 * reset the solver decisions to right after the rpm rules
1462 reset_solver(Solver *solv)
1467 /* delete all learnt rules */
1468 solv->nrules = solv->learntrules;
1469 queue_empty(&solv->learnt_why);
1470 queue_empty(&solv->learnt_pool);
1472 /* redo all direct rpm rule decisions */
1473 /* we break at the first decision with a why attached, this is
1474 * either a job/system rule decision of a propagated decision */
1475 for (i = 0; i < solv->decisionq.count; i++)
1477 v = solv->decisionq.elements[i];
1478 solv->decisionmap[v > 0 ? v : -v] = 0;
1480 for (i = 0; i < solv->decisionq_why.count; i++)
1481 if (solv->decisionq_why.elements[i])
1485 v = solv->decisionq.elements[i];
1486 solv->decisionmap[v > 0 ? v : -v] = v > 0 ? 1 : -1;
1489 if (solv->pool->verbose)
1490 printf("decisions done reduced from %d to %d\n", solv->decisionq.count, i);
1492 solv->decisionq_why.count = i;
1493 solv->decisionq.count = i;
1494 solv->recommends_index = -1;
1495 solv->propagate_index = 0;
1497 /* redo all job/system decisions */
1498 makeruledecisions(solv);
1499 if (solv->pool->verbose)
1500 printf("decisions after adding job and system rules: %d\n", solv->decisionq.count);
1501 /* recreate watches */
1507 * analyze_unsolvable_rule
1511 analyze_unsolvable_rule(Solver *solv, Rule *r)
1514 Id why = r - solv->rules;
1516 if (why >= solv->jobrules && why < solv->systemrules)
1518 if (why >= solv->systemrules && why < solv->weakrules)
1519 printf("SYSTEM %d ", why - solv->systemrules);
1520 if (why >= solv->weakrules && why < solv->learntrules)
1522 if (solv->learntrules && why >= solv->learntrules)
1526 if (solv->learntrules && why >= solv->learntrules)
1528 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1529 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i]);
1532 /* do not add rpm rules to problem */
1533 if (why < solv->jobrules)
1535 /* return if problem already countains the rule */
1536 if (solv->problems.count)
1538 for (i = solv->problems.count - 1; i >= 0; i--)
1539 if (solv->problems.elements[i] == 0)
1541 else if (solv->problems.elements[i] == why)
1544 queue_push(&solv->problems, why);
1549 * analyze_unsolvable
1551 * return: 1 - disabled some rules, try again
1556 analyze_unsolvable(Solver *solv, Rule *r, int disablerules)
1558 Pool *pool = solv->pool;
1559 Map seen; /* global? */
1562 Id *decisionmap = solv->decisionmap;
1563 int oldproblemcount;
1567 printf("ANALYZE UNSOLVABLE ----------------------\n");
1569 oldproblemcount = solv->problems.count;
1570 map_init(&seen, pool->nsolvables);
1571 analyze_unsolvable_rule(solv, r);
1572 dp = r->d ? pool->whatprovidesdata + r->d : 0;
1583 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1585 vv = v > 0 ? v : -v;
1586 l = solv->decisionmap[vv];
1591 idx = solv->decisionq.count;
1594 v = solv->decisionq.elements[--idx];
1595 vv = v > 0 ? v : -v;
1596 if (!MAPTST(&seen, vv))
1598 why = solv->decisionq_why.elements[idx];
1603 printruleelement(solv, 0, v);
1607 r = solv->rules + why;
1608 analyze_unsolvable_rule(solv, r);
1609 dp = r->d ? pool->whatprovidesdata + r->d : 0;
1620 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1622 vv = v > 0 ? v : -v;
1623 l = solv->decisionmap[vv];
1630 queue_push(&solv->problems, 0); /* mark end of this problem */
1633 if (solv->weakrules != solv->learntrules)
1635 for (i = oldproblemcount; i < solv->problems.count - 1; i++)
1637 why = solv->problems.elements[i];
1638 if (why < solv->weakrules || why >= solv->learntrules)
1640 if (!lastweak || lastweak < why)
1646 /* disable last weak rule */
1647 solv->problems.count = oldproblemcount;
1648 r = solv->rules + lastweak;
1649 printf("disabling weak ");
1655 else if (disablerules)
1657 for (i = oldproblemcount; i < solv->problems.count - 1; i++)
1659 r = solv->rules + solv->problems.elements[i];
1669 /*-----------------------------------------------------------------*/
1670 /* Decision revert */
1674 * revert decision at level
1678 revert(Solver *solv, int level)
1681 while (solv->decisionq.count)
1683 v = solv->decisionq.elements[solv->decisionq.count - 1];
1684 vv = v > 0 ? v : -v;
1685 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1688 printf("reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1690 solv->decisionmap[vv] = 0;
1691 solv->decisionq.count--;
1692 solv->decisionq_why.count--;
1693 solv->propagate_index = solv->decisionq.count;
1695 solv->recommends_index = -1;
1700 * watch2onhighest - put watch2 on literal with highest level
1704 watch2onhighest(Solver *solv, Rule *r)
1710 return; /* binary rule, both watches are set */
1711 dp = solv->pool->whatprovidesdata + r->d;
1712 while ((v = *dp++) != 0)
1714 l = solv->decisionmap[v < 0 ? -v : v];
1731 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules)
1741 solv->decisionmap[decision] = level;
1743 solv->decisionmap[-decision] = -level;
1744 queue_push(&solv->decisionq, decision);
1745 queue_push(&solv->decisionq_why, 0);
1749 r = propagate(solv, level);
1753 return analyze_unsolvable(solv, r, disablerules);
1754 printf("conflict with rule #%d\n", (int)(r - solv->rules));
1755 l = analyze(solv, level, r, &p, &d, &why);
1756 if (l >= level || l <= 0)
1758 printf("reverting decisions (level %d -> %d)\n", level, l);
1760 revert(solv, level);
1761 r = addrule(solv, p, d); /* p requires d */
1764 if (solv->learnt_why.count != (r - solv->rules) - solv->learntrules)
1766 printf("%d %d\n", solv->learnt_why.count, (int)(r - solv->rules) - solv->learntrules);
1769 queue_push(&solv->learnt_why, why);
1772 /* at least 2 literals, needs watches */
1773 watch2onhighest(solv, r);
1774 addwatches(solv, r);
1776 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1777 queue_push(&solv->decisionq, p);
1778 queue_push(&solv->decisionq_why, r - solv->rules);
1779 printf("decision: ");
1780 printruleelement(solv, 0, p);
1781 printf("new rule: ");
1787 /*-----------------------------------------------------------------*/
1788 /* Main solver interface */
1793 * create solver structure
1795 * pool: all available solvables
1796 * system: installed Solvables
1799 * Upon solving, rules are created to flag the Solvables
1800 * of the 'system' Repo as installed.
1804 solver_create(Pool *pool, Repo *system)
1807 solv = (Solver *)xcalloc(1, sizeof(Solver));
1809 solv->system = system;
1812 queue_init(&solv->ruletojob);
1813 queue_init(&solv->decisionq);
1814 queue_init(&solv->decisionq_why);
1815 queue_init(&solv->problems);
1816 queue_init(&solv->suggestions);
1817 queue_init(&solv->learnt_why);
1818 queue_init(&solv->learnt_pool);
1820 map_init(&solv->recommendsmap, pool->nsolvables);
1821 map_init(&solv->suggestsmap, pool->nsolvables);
1822 solv->recommends_index = 0;
1824 solv->decisionmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
1825 solv->rules = (Rule *)xmalloc((solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
1826 memset(solv->rules, 0, sizeof(Rule));
1838 solver_free(Solver *solv)
1840 queue_free(&solv->ruletojob);
1841 queue_free(&solv->decisionq);
1842 queue_free(&solv->decisionq_why);
1843 queue_free(&solv->learnt_why);
1844 queue_free(&solv->learnt_pool);
1845 queue_free(&solv->problems);
1846 queue_free(&solv->suggestions);
1848 map_free(&solv->recommendsmap);
1849 map_free(&solv->suggestsmap);
1850 xfree(solv->decisionmap);
1852 xfree(solv->watches);
1853 xfree(solv->weaksystemrules);
1854 xfree(solv->obsoletes);
1855 xfree(solv->obsoletes_data);
1860 /*-------------------------------------------------------*/
1865 * all rules have been set up, not actually run the solver
1870 run_solver(Solver *solv, int disablerules, int doweak)
1878 Pool *pool = solv->pool;
1882 printf("number of rules: %d\n", solv->nrules);
1885 for (i = 0; i < solv->nrules; i++)
1887 printrule(solv, solv->rules + i);
1892 /* all new rules are learnt after this point */
1893 solv->learntrules = solv->nrules;
1894 /* crate watches lists */
1897 if (pool->verbose) printf("initial decisions: %d\n", solv->decisionq.count);
1899 /* start SAT algorithm */
1901 systemlevel = level + 1;
1902 if (pool->verbose) printf("solving...\n");
1913 if (pool->verbose) printf("propagating (%d %d)...\n", solv->propagate_index, solv->decisionq.count);
1914 if ((r = propagate(solv, level)) != 0)
1916 if (analyze_unsolvable(solv, r, disablerules))
1918 printf("UNSOLVABLE\n");
1928 if (level < systemlevel && solv->system->nsolvables)
1930 if (!solv->updatesystem)
1932 /* try to keep as many packages as possible */
1933 if (pool->verbose) printf("installing system packages\n");
1934 for (i = solv->system->start, n = 0; ; i++, n++)
1936 if (n == solv->system->nsolvables)
1938 if (i == solv->system->start + solv->system->nsolvables)
1939 i = solv->system->start;
1940 s = pool->solvables + i;
1941 if (solv->decisionmap[i] != 0)
1944 printf("system installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1947 level = setpropagatelearn(solv, level, i, disablerules);
1950 printf("UNSOLVABLE\n");
1954 if (level <= olevel)
1958 if (solv->weaksystemrules)
1960 if (pool->verbose) printf("installing weak system packages\n");
1961 for (i = solv->system->start, n = 0; ; i++, n++)
1963 if (n == solv->system->nsolvables)
1965 if (solv->decisionmap[i] > 0 || (solv->decisionmap[i] < 0 && solv->weaksystemrules[i - solv->system->start] == 0))
1968 if (solv->decisionmap[i] == 0)
1970 if (solv->weaksystemrules[i - solv->system->start])
1972 dp = pool->whatprovidesdata + solv->weaksystemrules[i - solv->system->start];
1973 while ((p = *dp++) != 0)
1975 if (solv->decisionmap[p] > 0)
1977 if (solv->decisionmap[p] == 0)
1981 continue; /* rule is already true */
1987 prune_to_highest_prio(pool, &dq);
1989 prune_to_recommended(solv, &dq);
1991 prune_best_version_arch(pool, &dq);
1993 s = pool->solvables + dq.elements[0];
1994 printf("weak system installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1997 level = setpropagatelearn(solv, level, dq.elements[0], disablerules);
2000 printf("UNSOLVABLE\n");
2004 if (level <= olevel)
2010 if (n != solv->system->nsolvables)
2013 systemlevel = level;
2020 if (pool->verbose) printf("deciding unresolved rules\n");
2021 for (i = 1, n = 1; ; i++, n++)
2023 if (n == solv->nrules)
2025 if (i == solv->nrules)
2027 r = solv->rules + i;
2033 /* binary or unary rule */
2034 /* need two positive undecided literals */
2035 if (r->p < 0 || r->w2 <= 0)
2037 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
2039 queue_push(&dq, r->p);
2040 queue_push(&dq, r->w2);
2045 * all negative literals are installed
2046 * no positive literal is installed
2047 * i.e. the rule is not fulfilled and we
2048 * just need to decide on the positive literals
2052 if (solv->decisionmap[-r->p] <= 0)
2057 if (solv->decisionmap[r->p] > 0)
2059 if (solv->decisionmap[r->p] == 0)
2060 queue_push(&dq, r->p);
2062 dp = pool->whatprovidesdata + r->d;
2063 while ((p = *dp++) != 0)
2067 if (solv->decisionmap[-p] <= 0)
2072 if (solv->decisionmap[p] > 0)
2074 if (solv->decisionmap[p] == 0)
2083 /* cannot happen as this means that
2084 * the rule is unit */
2088 prune_to_highest_prio(pool, &dq);
2090 prune_to_recommended(solv, &dq);
2092 prune_best_version_arch(pool, &dq);
2093 p = dq.elements[dq.count - 1];
2094 s = pool->solvables + p;
2096 printf("installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2099 level = setpropagatelearn(solv, level, p, disablerules);
2102 printf("UNSOLVABLE\n");
2106 if (level < systemlevel)
2109 } /* for(), decide */
2111 if (n != solv->nrules) /* continue if level < systemlevel */
2114 if (doweak && !solv->problems.count)
2118 if (pool->verbose) printf("installing recommended packages\n");
2120 for (i = 1; i < pool->nsolvables; i++)
2122 if (solv->decisionmap[i] < 0)
2124 if (solv->decisionmap[i] > 0)
2126 Id *recp, rec, *pp, p;
2127 s = pool->solvables + i;
2128 /* installed, check for recommends */
2129 /* XXX need to special case AND ? */
2132 recp = s->repo->idarraydata + s->recommends;
2133 while ((rec = *recp++) != 0)
2136 FOR_PROVIDES(p, pp, rec)
2138 if (solv->decisionmap[p] > 0)
2143 else if (solv->decisionmap[p] == 0)
2144 queue_pushunique(&dq, p);
2152 s = pool->solvables + i;
2153 if (!s->supplements && !s->freshens)
2155 if (!pool_installable(pool, s))
2159 supp = s->repo->idarraydata + s->supplements;
2160 while ((sup = *supp++) != 0)
2161 if (dep_fulfilled(solv, sup))
2168 supp = s->repo->idarraydata + s->freshens;
2169 while ((sup = *supp++) != 0)
2170 if (dep_fulfilled(solv, sup))
2175 queue_pushunique(&dq, i);
2181 prune_to_highest_prio(pool, &dq);
2183 prune_best_version_arch(pool, &dq);
2184 p = dq.elements[dq.count - 1];
2185 s = pool->solvables + p;
2187 printf("installing recommended %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2189 level = setpropagatelearn(solv, level, p, 0);
2204 refine_suggestion(Solver *solv, Id *problem, Id sug, Queue *refined)
2207 int i, j, sugseen, sugjob = -1;
2212 printf("refine_suggestion start\n");
2214 if (sug >= solv->jobrules && sug < solv->systemrules)
2215 sugjob = solv->ruletojob.elements[sug - solv->jobrules];
2217 queue_init(&disabled);
2218 queue_empty(refined);
2219 queue_push(refined, sug);
2221 /* re-enable all rules but rule "sug" of the problem */
2227 r = solv->rules + sug;
2231 for (i = 0; problem[i]; i++)
2233 if (problem[i] == sug)
2238 if (sugjob >= 0 && problem[i] >= solv->jobrules && problem[i] < solv->systemrules && sugjob == solv->ruletojob.elements[problem[i] - solv->jobrules])
2240 /* rule belongs to same job */
2243 r = solv->rules + problem[i];
2250 /* direct assertion */
2251 if (r->p == sugassert && sugseen)
2253 /* also leave this assertion disabled */
2256 v = r->p > 0 ? r->p : -r->p;
2257 if (solv->decisionmap[v])
2259 if ((solv->decisionmap[v] > 0 && r->p < 0) ||
2260 (solv->decisionmap[v] < 0 && r->p > 0))
2262 printf("direct assertion failure, no solution found!\n");
2265 r = solv->rules + problem[i];
2272 if (r->d == 0 || r->w2 != r->p)
2275 r->w1 = solv->pool->whatprovidesdata[r->d];
2279 /* re-enable as many weak rules as possible */
2280 for (i = solv->weakrules; i < solv->learntrules; i++)
2282 r = solv->rules + i;
2285 if (r->d == 0 || r->w2 != r->p)
2288 r->w1 = solv->pool->whatprovidesdata[r->d];
2291 queue_empty(&solv->problems);
2292 revert(solv, 1); /* XXX move to reset_solver? */
2294 run_solver(solv, 0, 0);
2295 if (!solv->problems.count)
2297 printf("no more problems!\n");
2299 printdecisions(solv);
2301 break; /* great, no more problems */
2303 disabledcnt = disabled.count;
2304 for (i = 0; i < solv->problems.elements[i]; i++)
2306 /* ignore solutions in refined */
2307 v = solv->problems.elements[i];
2308 for (j = 0; problem[j]; j++)
2309 if (problem[j] != sug && problem[j] == v)
2313 queue_push(&disabled, v);
2314 queue_push(&disabled, 0); /* room for watch */
2316 if (disabled.count == disabledcnt)
2318 /* no solution found, this was an invalid suggestion! */
2319 printf("no solution found!\n");
2323 if (disabled.count == disabledcnt + 2)
2325 /* just one suggestion, add it to refined list */
2326 queue_push(refined, disabled.elements[disabledcnt]);
2330 printf("############################################## more than one solution found.\n");
2332 for (i = 0; i < solv->problems.elements[i]; i++)
2333 printrule(solv, solv->rules + solv->problems.elements[i]);
2334 printf("##############################################\n");
2336 /* more than one solution, keep all disabled */
2338 for (i = disabledcnt; i < disabled.count; i += 2)
2341 r = solv->rules + disabled.elements[i];
2342 disabled.elements[i + 1] = r->w1;
2350 /* enable refined rules again */
2351 for (i = 0; i < disabled.count; i += 2)
2353 r = solv->rules + disabled.elements[i];
2354 r->w1 = disabled.elements[i + 1];
2356 /* disable problem rules again so that we are in the same state as before */
2357 for (i = 0; problem[i]; i++)
2359 r = solv->rules + problem[i];
2362 printf("refine_suggestion end\n");
2371 id2rc(Solver *solv, Id id)
2374 if (solv->rc_output != 2)
2376 evr = id2str(solv->pool, id);
2377 if (*evr < '0' || *evr > '9')
2379 while (*evr >= '0' && *evr <= '9')
2387 printdecisions(Solver *solv)
2389 Pool *pool = solv->pool;
2390 Id p, *obsoletesmap;
2394 obsoletesmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
2395 for (i = 0; i < solv->decisionq.count; i++)
2400 n = solv->decisionq.elements[i];
2403 if (n == SYSTEMSOLVABLE)
2405 if (n >= solv->system->start && n < solv->system->start + solv->system->nsolvables)
2407 s = pool->solvables + n;
2410 obsp = s->repo->idarraydata + s->obsoletes;
2411 while ((obs = *obsp++) != 0)
2412 FOR_PROVIDES(p, pp, obs)
2414 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2416 obsoletesmap[p] = n;
2421 FOR_PROVIDES(p, pp, s->name)
2422 if (s->name == pool->solvables[p].name)
2424 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2426 obsoletesmap[p] = n;
2432 if (solv->rc_output)
2433 printf(">!> Solution #1:\n");
2435 int installs = 0, uninstalls = 0, upgrades = 0;
2437 /* print solvables to be erased */
2439 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2441 if (solv->decisionmap[i] > 0)
2443 if (obsoletesmap[i])
2445 s = pool->solvables + i;
2446 if (solv->rc_output == 2)
2447 printf(">!> remove %s-%s%s\n", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2448 else if (solv->rc_output)
2449 printf(">!> remove %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2451 printf("erase %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2455 /* print solvables to be installed */
2457 for (i = 0; i < solv->decisionq.count; i++)
2460 p = solv->decisionq.elements[i];
2463 if (p == SYSTEMSOLVABLE)
2465 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2467 s = pool->solvables + p;
2469 if (!obsoletesmap[p])
2471 if (solv->rc_output)
2473 printf("install %s-%s%s", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2474 if (solv->rc_output != 2)
2475 printf(".%s", id2str(pool, s->arch));
2478 else if (!solv->rc_output)
2480 printf("update %s-%s.%s (obsoletes", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2481 for (j = solv->system->start; j < solv->system->start + solv->system->nsolvables; j++)
2483 if (obsoletesmap[j] != p)
2485 s = pool->solvables + j;
2486 printf(" %s-%s.%s", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2493 Solvable *f, *fn = 0;
2494 for (j = solv->system->start; j < solv->system->start + solv->system->nsolvables; j++)
2496 if (obsoletesmap[j] != p)
2498 f = pool->solvables + j;
2499 if (fn || f->name != s->name)
2501 if (solv->rc_output == 2)
2502 printf(">!> remove %s-%s%s\n", id2str(pool, f->name), id2rc(solv, f->evr), id2str(pool, f->evr));
2503 else if (solv->rc_output)
2504 printf(">!> remove %s-%s.%s\n", id2str(pool, f->name), id2str(pool, f->evr), id2str(pool, f->arch));
2512 printf(">!> install %s-%s%s", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2513 if (solv->rc_output != 2)
2514 printf(".%s", id2str(pool, s->arch));
2519 if (solv->rc_output == 2)
2520 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));
2522 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));
2526 if (solv->rc_output)
2528 Repo *repo = s->repo;
2529 if (repo && strcmp(repo_name(repo), "locales"))
2530 printf("[%s]", repo_name(repo));
2535 if (solv->rc_output)
2536 printf(">!> installs=%d, upgrades=%d, uninstalls=%d\n", installs, upgrades, uninstalls);
2538 xfree(obsoletesmap);
2542 create_obsolete_index(Solver *solv)
2544 Pool *pool = solv->pool;
2546 Repo *system = solv->system;
2547 Id p, *pp, obs, *obsp, *obsoletes, *obsoletes_data;
2550 /* create reverse obsoletes map for system solvables */
2551 solv->obsoletes = obsoletes = xcalloc(system->nsolvables, sizeof(Id));
2552 for (i = 1; i < pool->nsolvables; i++)
2554 s = pool->solvables + i;
2557 if (!pool_installable(pool, s))
2559 obsp = s->repo->idarraydata + s->obsoletes;
2560 while ((obs = *obsp++) != 0)
2561 FOR_PROVIDES(p, pp, obs)
2563 if (p < system->start || p >= system->start + system->nsolvables)
2565 if (pool->solvables[p].name == s->name)
2567 obsoletes[p - system->start]++;
2571 for (i = 0; i < system->nsolvables; i++)
2574 n += obsoletes[i] + 1;
2577 solv->obsoletes_data = obsoletes_data = xcalloc(n + 1, sizeof(Id));
2578 if (pool->verbose) printf("obsoletes data: %d entries\n", n + 1);
2579 for (i = pool->nsolvables - 1; i > 0; i--)
2581 s = pool->solvables + i;
2584 if (!pool_installable(pool, s))
2586 obsp = s->repo->idarraydata + s->obsoletes;
2587 while ((obs = *obsp++) != 0)
2588 FOR_PROVIDES(p, pp, obs)
2590 if (p < system->start || p >= system->start + system->nsolvables)
2592 if (pool->solvables[p].name == s->name)
2595 if (obsoletes_data[obsoletes[p]] != i)
2596 obsoletes_data[--obsoletes[p]] = i;
2601 /*-----------------------------------------------------------------*/
2611 solve(Solver *solv, Queue *job)
2613 Pool *pool = solv->pool;
2615 Map addedmap; /* '1' == have rule for solvable */
2616 Map noupdaterule; /* '1' == don't update (scheduled for removal) */
2617 Id how, what, p, *pp, d;
2623 * create basic rule set of all involved packages
2628 map_init(&addedmap, pool->nsolvables);
2629 map_init(&noupdaterule, pool->nsolvables);
2634 * always install our system solvable
2636 MAPSET(&addedmap, SYSTEMSOLVABLE);
2637 queue_push(&solv->decisionq, SYSTEMSOLVABLE);
2638 queue_push(&solv->decisionq_why, 0);
2639 solv->decisionmap[SYSTEMSOLVABLE] = 1;
2642 * create rules for installed solvables -> keep them installed
2643 * so called: rpm rules
2647 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2648 addrulesforsolvable(solv, pool->solvables + i, &addedmap);
2651 * create install rules
2653 * two passes, as we want to keep the rpm rules distinct from the job rules
2657 if (solv->noupdateprovide && solv->system->nsolvables)
2658 create_obsolete_index(solv);
2662 * process job rules for solvables
2665 for (i = 0; i < job->count; i += 2)
2667 how = job->elements[i];
2668 what = job->elements[i + 1];
2672 case SOLVER_INSTALL_SOLVABLE:
2673 addrulesforsolvable(solv, pool->solvables + what, &addedmap);
2675 case SOLVER_INSTALL_SOLVABLE_NAME:
2676 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
2678 FOR_PROVIDES(p, pp, what)
2680 /* if by name, ensure that the name matches */
2681 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
2683 addrulesforsolvable(solv, pool->solvables + p, &addedmap);
2686 case SOLVER_INSTALL_SOLVABLE_UPDATE:
2687 /* dont allow downgrade */
2688 addupdaterule(solv, pool->solvables + what, &addedmap, 0, 0, 1);
2694 * if unstalls are disallowed, add update rules for every
2695 * installed solvables in the hope to circumvent uninstall
2701 if (!solv->allowuninstall)
2703 /* add update rule for every installed package */
2704 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2705 addupdaterule(solv, pool->solvables + i, &addedmap, solv->allowdowngrade, solv->allowarchchange, 1);
2707 #else /* this is just to add the needed rpm rules to our set */
2708 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2709 addupdaterule(solv, pool->solvables + i, &addedmap, 1, 1, 1);
2712 addrulesforweak(solv, &addedmap);
2716 int possible = 0, installable = 0;
2717 for (i = 1; i < pool->nsolvables; i++)
2719 if (pool_installable(pool, pool->solvables + i))
2721 if (MAPTST(&addedmap, i))
2724 printf("%d of %d installable solvables used for solving\n", possible, installable);
2731 * unify existing rules before going over all job rules
2735 unifyrules(solv); /* remove duplicate rpm rules */
2738 * at this point the system is always solvable,
2739 * as an empty system (remove all packages) is a valid solution
2741 if (pool->verbose) printf("decisions based on rpms: %d\n", solv->decisionq.count);
2744 * now add all job rules
2747 solv->jobrules = solv->nrules;
2749 for (i = 0; i < job->count; i += 2)
2751 how = job->elements[i];
2752 what = job->elements[i + 1];
2755 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
2756 if (solv->rc_output) {
2757 Solvable *s = pool->solvables + what;
2758 printf(">!> Installing %s from channel %s\n", id2str(pool, s->name), repo_name(s->repo));
2760 addrule(solv, what, 0); /* install by Id */
2761 queue_push(&solv->ruletojob, i);
2763 case SOLVER_ERASE_SOLVABLE:
2764 addrule(solv, -what, 0); /* remove by Id */
2765 queue_push(&solv->ruletojob, i);
2766 MAPSET(&noupdaterule, what);
2768 case SOLVER_INSTALL_SOLVABLE_NAME: /* install by capability */
2769 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
2771 FOR_PROVIDES(p, pp, what)
2773 /* if by name, ensure that the name matches */
2774 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
2780 /* no provider, make this an impossible rule */
2781 queue_push(&q, -SYSTEMSOLVABLE);
2784 p = queue_shift(&q); /* get first provider */
2786 d = 0; /* single provider ? -> make assertion */
2788 d = pool_queuetowhatprovides(pool, &q); /* get all providers */
2789 addrule(solv, p, d); /* add 'requires' rule */
2790 queue_push(&solv->ruletojob, i);
2792 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
2793 case SOLVER_ERASE_SOLVABLE_PROVIDES:
2794 FOR_PROVIDES(p, pp, what)
2796 /* if by name, ensure that the name matches */
2797 if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
2800 addrule(solv, -p, 0); /* add 'remove' rule */
2801 queue_push(&solv->ruletojob, i);
2802 MAPSET(&noupdaterule, p);
2805 case SOLVER_INSTALL_SOLVABLE_UPDATE: /* find update for solvable */
2806 addupdaterule(solv, pool->solvables + what, &addedmap, 0, 0, 0);
2807 queue_push(&solv->ruletojob, i);
2812 if (solv->ruletojob.count != solv->nrules - solv->jobrules)
2815 if (pool->verbose) printf("problems so far: %d\n", solv->problems.count);
2818 * now add policy rules
2822 solv->systemrules = solv->nrules;
2825 * create rules for updating installed solvables
2831 if (!solv->allowuninstall)
2832 { /* loop over all installed solvables */
2833 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2835 if (!MAPTST(&noupdaterule, i)) /* if not marked as 'noupdate' */
2836 addupdaterule(solv, pool->solvables + i, &addedmap, solv->allowdowngrade, solv->allowarchchange, 0);
2838 addrule(solv, 0, 0); /* place holder */
2840 /* consistency check: we added a rule for _every_ system solvable */
2841 if (solv->nrules - solv->systemrules != solv->system->nsolvables)
2845 if (pool->verbose) printf("problems so far: %d\n", solv->problems.count);
2847 /* create special weak system rules */
2848 if (solv->system->nsolvables)
2850 solv->weaksystemrules = xcalloc(solv->system->nsolvables, sizeof(Id));
2851 for (i = 0; i < solv->system->nsolvables; i++)
2853 if (MAPTST(&noupdaterule, solv->system->start + i))
2855 findupdatepackages(solv, pool->solvables + solv->system->start + i, &q, (Map *)0, 1, 1);
2857 solv->weaksystemrules[i] = pool_queuetowhatprovides(pool, &q);
2861 /* free unneeded memory */
2862 map_free(&addedmap);
2863 map_free(&noupdaterule);
2866 solv->weakrules = solv->nrules;
2868 /* try real hard to keep packages installed */
2871 for (i = 0; i < solv->system->nsolvables; i++)
2873 d = solv->weaksystemrules[i];
2874 addrule(solv, solv->system->start + i, d);
2883 makeruledecisions(solv);
2884 run_solver(solv, 1, 1);
2886 /* find suggested packages */
2887 if (!solv->problems.count)
2889 Id sug, *sugp, enh, *enhp, p, *pp;
2891 /* create map of all suggests that are still open */
2892 solv->recommends_index = -1;
2893 MAPZERO(&solv->suggestsmap);
2894 for (i = 0; i < solv->decisionq.count; i++)
2896 p = solv->decisionq.elements[i];
2899 s = pool->solvables + p;
2902 sugp = s->repo->idarraydata + s->suggests;
2903 while ((sug = *sugp++) != 0)
2905 FOR_PROVIDES(p, pp, sug)
2906 if (solv->decisionmap[p] > 0)
2909 continue; /* already fulfilled */
2910 FOR_PROVIDES(p, pp, sug)
2911 MAPSET(&solv->suggestsmap, p);
2915 for (i = 1; i < pool->nsolvables; i++)
2917 if (solv->decisionmap[i] != 0)
2919 s = pool->solvables + i;
2920 if (!MAPTST(&solv->suggestsmap, i))
2924 if (!pool_installable(pool, s))
2926 enhp = s->repo->idarraydata + s->enhances;
2927 while ((enh = *enhp++) != 0)
2928 if (dep_fulfilled(solv, enh))
2933 queue_push(&solv->suggestions, i);
2935 prune_best_version_arch(pool, &solv->suggestions);
2940 * print solver result
2944 if (pool->verbose) printf("-------------------------------------------------------------\n");
2946 if (solv->problems.count)
2956 queue_clone(&problems, &solv->problems);
2957 queue_init(&solution);
2958 printf("Encountered problems! Here are the solutions:\n");
2959 problem = problems.elements;
2960 for (i = 0; i < problems.count; i++)
2962 Id v = problems.elements[i];
2965 printf("====================================\n");
2966 problem = problems.elements + i + 1;
2969 if (v >= solv->jobrules && v < solv->systemrules)
2971 ji = solv->ruletojob.elements[v - solv->jobrules];
2974 if (problem[j] >= solv->jobrules && problem[j] < solv->systemrules && ji == solv->ruletojob.elements[problem[j] - solv->jobrules])
2977 if (problem + j < problems.elements + i)
2980 refine_suggestion(solv, problem, v, &solution);
2981 for (j = 0; j < solution.count; j++)
2983 r = solv->rules + solution.elements[j];
2984 why = solution.elements[j];
2988 if (why >= solv->jobrules && why < solv->systemrules)
2990 ji = solv->ruletojob.elements[why - solv->jobrules];
2991 what = job->elements[ji + 1];
2992 switch (job->elements[ji])
2994 case SOLVER_INSTALL_SOLVABLE:
2995 s = pool->solvables + what;
2996 printf("- do not install %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2998 case SOLVER_ERASE_SOLVABLE:
2999 s = pool->solvables + what;
3000 printf("- do not deinstall %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
3002 case SOLVER_INSTALL_SOLVABLE_NAME:
3003 printf("- do not install %s\n", id2str(pool, what));
3005 case SOLVER_ERASE_SOLVABLE_NAME:
3006 printf("- do not deinstall %s\n", id2str(pool, what));
3008 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
3009 printf("- do not install a solvable providing %s\n", dep2str(pool, what));
3011 case SOLVER_ERASE_SOLVABLE_PROVIDES:
3012 printf("- do not deinstall all solvables providing %s\n", dep2str(pool, what));
3014 case SOLVER_INSTALL_SOLVABLE_UPDATE:
3015 s = pool->solvables + what;
3016 printf("- do not install most recent version of %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
3019 printf("- do something different\n");
3023 else if (why >= solv->systemrules && why < solv->weakrules)
3026 s = pool->solvables + solv->system->start + (why - solv->systemrules);
3027 if (solv->weaksystemrules && solv->weaksystemrules[why - solv->systemrules])
3029 Id *dp = pool->whatprovidesdata + solv->weaksystemrules[why - solv->systemrules];
3032 if (*dp >= solv->system->start && *dp < solv->system->start + solv->system->nsolvables)
3034 if (solv->decisionmap[*dp] > 0)
3036 sd = pool->solvables + *dp;
3044 if (evrcmp(pool, sd->evr, s->evr) < 0)
3046 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));
3049 if (!solv->allowarchchange && archchanges(pool, sd, s))
3051 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));
3055 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));
3059 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));
3067 printf("------------------------------------\n");
3068 queue_free(&problems);
3069 queue_free(&solution);
3074 printdecisions(solv);
3075 if (solv->suggestions.count)
3077 printf("\nsuggested packages:\n");
3078 for (i = 0; i < solv->suggestions.count; i++)
3080 s = pool->solvables + solv->suggestions.elements[i];
3081 printf("- %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));