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 * prune_to_recommended
115 prune_to_recommended(Solver *solv, Queue *plist)
117 Pool *pool = solv->pool;
120 Id p, *pp, sup, *supp, rec, *recp, sug, *sugp, enh, *enhp;
122 if (solv->recommends_index < 0)
124 MAPZERO(&solv->recommends);
125 MAPZERO(&solv->suggests);
126 solv->recommends_index = 0;
128 while (solv->recommends_index < solv->decisionq.count)
130 p = solv->decisionq.elements[solv->recommends_index++];
133 s = pool->solvables + p;
134 if ((recp = s->recommends) != 0)
135 while ((rec = *recp++) != 0)
136 FOR_PROVIDES(p, pp, rec)
137 MAPSET(&solv->recommends, p);
138 if ((sugp = s->suggests) != 0)
139 while ((sug = *sugp++) != 0)
140 FOR_PROVIDES(p, pp, sug)
141 MAPSET(&solv->suggests, p);
143 /* prune to recommended/supplemented */
144 for (i = j = 0; i < plist->count; i++)
146 p = plist->elements[i];
147 if (MAPTST(&solv->recommends, p))
149 plist->elements[j++] = p;
152 s = pool->solvables + p;
153 if (!s->supplements && !s->freshens)
155 if ((supp = s->supplements) != 0)
157 while ((sup = *supp++) != 0)
158 if (dep_fulfilled(solv, sup))
163 if ((supp = s->freshens) != 0)
165 while ((sup = *supp++) != 0)
166 if (dep_fulfilled(solv, sup))
171 plist->elements[j++] = s - pool->solvables;
176 /* prune to suggested/enhanced*/
177 if (plist->count < 2)
179 for (i = j = 0; i < plist->count; i++)
181 p = plist->elements[i];
182 if (MAPTST(&solv->suggests, p))
184 plist->elements[j++] = p;
187 s = pool->solvables + p;
188 if (!(enhp = s->enhances))
190 while ((enh = *enhp++) != 0)
191 if (dep_fulfilled(solv, enh))
195 plist->elements[j++] = s - pool->solvables;
202 * prune_best_version_arch
204 * sort list of packages (given through plist) by name and evr
205 * return result through plist
209 /* FIXME: must also look at update packages */
212 prune_best_version_arch(Pool *pool, Queue *plist)
219 if (plist->count < 2) /* no need to prune for a single entry */
221 if (pool->verbose) printf("prune_best_version_arch %d\n", plist->count);
223 /* prune to best architecture */
227 for (i = 0; i < plist->count; i++)
229 s = pool->solvables + plist->elements[i];
231 if (a > pool->lastarch)
233 a = pool->id2arch[a];
234 if (!bestscore || (a & 0xffff0000) < bestscore)
235 bestscore = a & 0xffff0000;
237 for (i = j = 0; i < plist->count; i++)
239 s = pool->solvables + plist->elements[i];
241 if (a > pool->lastarch)
243 a = pool->id2arch[a];
244 /* a == 1 -> noarch */
245 if (a != 1 && (a & 0xffff0000) != bestscore)
247 plist->elements[j++] = plist->elements[i];
254 prune_best_version_arch_sortcmp_data = pool;
255 /* sort by name first */
256 qsort(plist->elements, plist->count, sizeof(Id), prune_best_version_arch_sortcmp);
258 /* now find best 'per name' */
259 for (i = j = 0; i < plist->count; i++)
261 s = pool->solvables + plist->elements[i];
262 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
265 if (pool->verbose) printf("- %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
267 if (!best) /* if no best yet, the current is best */
269 best = plist->elements[i];
273 /* name switch: re-init */
274 if (pool->solvables[best].name != s->name) /* new name */
276 if (pool->verbose) printf("BEST: %s-%s.%s\n", id2str(pool, pool->solvables[best].name), id2str(pool, pool->solvables[best].evr), id2str(pool, pool->solvables[best].arch));
277 plist->elements[j++] = best; /* move old best to front */
278 best = plist->elements[i]; /* take current as new best */
282 if (pool->solvables[best].evr != s->evr) /* compare evr */
284 if (evrcmp(pool, pool->solvables[best].evr, s->evr) < 0)
285 best = plist->elements[i];
290 best = plist->elements[0];
292 /* XXX also check obsoletes! */
293 if (pool->verbose) printf("BEST: %s-%s.%s\n", id2str(pool, pool->solvables[best].name), id2str(pool, pool->solvables[best].evr), id2str(pool, pool->solvables[best].arch));
295 plist->elements[j++] = best;
300 /*-----------------------------------------------------------------*/
307 printruleelement(Solver *solv, Rule *r, Id v)
309 Pool *pool = solv->pool;
313 s = pool->solvables + -v;
314 printf(" !%s-%s.%s [%d]", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), -v);
318 s = pool->solvables + v;
319 printf(" %s-%s.%s [%d]", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), v);
328 if (solv->decisionmap[s - pool->solvables] > 0)
329 printf(" I.%d", solv->decisionmap[s - pool->solvables]);
330 if (solv->decisionmap[s - pool->solvables] < 0)
331 printf(" C.%d", -solv->decisionmap[s - pool->solvables]);
341 printrule(Solver *solv, Rule *r)
346 if (r >= solv->rules && r < solv->rules + solv->nrules) /* r is a solver rule */
347 printf("Rule #%d:\n", (int)(r - solv->rules));
349 printf("Rule:\n"); /* r is any rule */
354 else if (r->d == ID_NULL)
361 v = solv->pool->whatprovidesdata[r->d + i - 1];
364 printruleelement(solv, r, v);
366 printf(" next: %d %d\n", r->n1, r->n2);
370 /*-----------------------------------------------------------------*/
376 static Pool *unifyrules_sortcmp_data;
379 * compare rules for unification sort
383 unifyrules_sortcmp(const void *ap, const void *bp)
385 Pool *pool = unifyrules_sortcmp_data;
386 Rule *a = (Rule *)ap;
387 Rule *b = (Rule *)bp;
393 return x; /* p differs */
396 if (a->d == 0 && b->d == 0)
397 return a->w2 - b->w2; /* assertion: return w2 diff */
399 if (a->d == 0) /* a is assertion, b not */
401 x = a->w2 - pool->whatprovidesdata[b->d];
405 if (b->d == 0) /* b is assertion, a not */
407 x = pool->whatprovidesdata[a->d] - b->w2;
411 /* compare whatprovidesdata */
412 ad = pool->whatprovidesdata + a->d;
413 bd = pool->whatprovidesdata + b->d;
414 for (; *ad && *ad == *bd; ad++, bd++)
425 unifyrules(Solver *solv)
430 if (solv->nrules <= 1) /* nothing to unify */
433 /* sort rules first */
434 unifyrules_sortcmp_data = solv->pool;
435 qsort(solv->rules + 1, solv->nrules - 1, sizeof(Rule), unifyrules_sortcmp);
442 for (i = j = 1, ir = solv->rules + 1; i < solv->nrules; i++, ir++)
444 if (jr && !unifyrules_sortcmp(ir, jr))
445 continue; /* prune! */
446 jr = solv->rules + j++; /* keep! */
451 /* reduced count from nrules to j rules */
452 if (solv->pool->verbose) printf("pruned rules from %d to %d\n", solv->nrules, j);
454 /* adapt rule buffer */
455 solv->rules = (Rule *)xrealloc(solv->rules, ((solv->nrules + RULES_BLOCK) & ~RULES_BLOCK) * sizeof(Rule));
464 for (i = 1; i < solv->nrules; i++)
467 if (r->d == 0) /* assertion */
472 dp = solv->pool->whatprovidesdata + r->d;
476 if (solv->pool->verbose)
478 printf(" binary: %d\n", binr);
479 printf(" normal: %d\n", solv->nrules - 1 - binr);
480 printf(" normal lits: %d\n", dc);
493 hashrule(Solver *solv, Id p, Id d, int n)
495 unsigned int x = (unsigned int)p;
499 return (x * 37) ^ (unsigned int)d;
500 dp = solv->pool->whatprovidesdata + d;
502 x = (x * 37) ^ (unsigned int)*dp++;
510 * p = direct literal; > 0 for learnt, < 0 for installed pkg (rpm)
511 * d, if < 0 direct literal, if > 0 offset into whatprovides, if == 0 rule is assertion (look at p only)
514 * A requires b, b provided by B1,B2,B3 => (-A|B1|B2|B3)
516 * p < 0 : rule from rpm (installed pkg)
517 * d > 0 : Offset in whatprovidesdata (list of providers)
519 * A conflicts b, b provided by B1,B2,B3 => (-A|-B1), (-A|-B2), (-A|-B3)
520 * d < 0: Id of solvable (e.g. B1)
522 * d == 0: unary rule, assertion => (A) or (-A)
524 * Install: p > 0, d = 0 (A) user requested install
525 * Remove: p < 0, d = 0 (-A) user requested remove
526 * Requires: p < 0, d > 0 (-A|B1|B2|...) d: <list of providers for requirement of p>
527 * Updates: p > 0, d > 0 (A|B1|B2|...) d: <list of updates for solvable p>
528 * Conflicts: p < 0, d < 0 (-A|-B) either p (conflict issuer) or d (conflict provider)
529 * ? p > 0, d < 0 (A|-B)
530 * No-op ?: p = 0, d = 0 (null) (used as policy rule placeholder)
534 addrule(Solver *solv, Id p, Id d)
539 int n = 0; /* number of literals in rule - 1
540 0 = direct assertion (single literal)
544 /* it often happenes that requires lead to adding the same rpm rule
545 * multiple times, so we prune those duplicates right away to make
546 * the work for unifyrules a bit easier */
548 if (solv->nrules && !solv->jobrules)
550 r = solv->rules + solv->nrules - 1; /* get the last added rule */
551 if (r->p == p && r->d == d && d != 0) /* identical and not user requested */
558 return NULL; /* ignore self conflict */
561 else if (d == 0) /* user requested */
565 for (dp = solv->pool->whatprovidesdata + d; *dp; dp++, n++)
567 return NULL; /* rule is self-fulfilling */
572 if (n == 0) /* direct assertion */
576 /* this is a rpm rule assertion, we do not have to allocate it */
577 /* we can identify it by looking at the decision level, it will be 1 */
580 if (solv->decisionmap[-p] > 0) /* */
582 if (solv->decisionmap[-p]) /* */
584 queuepush(&solv->decisionq, p);
585 queuepush(&solv->decisionq_why, 0);
586 solv->decisionmap[-p] = -1;
591 else if (n == 1 && p > d)
593 /* smallest literal first so we can find dups */
597 n = 1; /* re-set n, was used as temp var */
613 Id *dp2 = solv->pool->whatprovidesdata + r->d;
614 for (dp = solv->pool->whatprovidesdata + d; *dp; dp++, dp2++)
627 /* check and extend rule buffer */
628 if ((solv->nrules & RULES_BLOCK) == 0)
630 solv->rules = (Rule *)xrealloc(solv->rules, (solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
633 r = solv->rules + solv->nrules++; /* point to rule space */
638 /* direct assertion, no watch needed */
654 r->w2 = solv->pool->whatprovidesdata[d];
659 /* we don't add the decision for learnt rules, as the code does that
660 * right after calling addrule anyway */
663 && !solv->learntrules)
665 /* must be system or job rule, as there are only negative unary rpm rules */
666 Id vv = p > 0 ? p : -p;
667 if (solv->decisionmap[vv])
670 if (solv->decisionmap[vv] > 0 && p > 0)
672 if (solv->decisionmap[vv] < 0 && p < 0)
674 /* direct conflict! */
675 for (i = 0; i < solv->decisionq.count; i++)
676 if (solv->decisionq.elements[i] == -p)
678 if (i == solv->decisionq.count)
680 if (solv->decisionq_why.elements[i] == 0)
682 /* conflict with rpm rule */
683 queuepush(&solv->problems, r - solv->rules);
684 queuepush(&solv->problems, 0);
685 r->w1 = 0; /* disable */
688 /* conflict with other job or system rule */
689 queuepush(&solv->problems, solv->decisionq_why.elements[i]);
690 queuepush(&solv->problems, r - solv->rules);
691 queuepush(&solv->problems, 0);
692 r->w1 = 0; /* disable */
693 /* also disable conflicting rule */
694 solv->rules[solv->decisionq_why.elements[i]].w1 = 0;
695 /* XXX: remove from decisionq! */
696 printf("XXX remove from decisionq\n");
699 queuepush(&solv->decisionq, p);
700 queuepush(&solv->decisionq_why, r - solv->rules);
701 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? 1 : -1;
708 * add (install) rules for solvable
713 addrulesforsolvable(Solver *solv, Solvable *s, Map *m)
715 Pool *pool = solv->pool;
716 Source *system = solv->system;
729 queueinit_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
730 queuepush(&q, s - pool->solvables); /* push solvable Id */
736 * s: Pointer to solvable
740 if (MAPTST(m, n)) /* continue if already set in map */
744 s = pool->solvables + n; /* s = Solvable in question */
747 if (system /* have rpm */
749 && n >= system->start /* its an rpm rule */
750 && n < system->start + system->nsolvables)
752 dontfix = 1; /* dont care about broken rpm deps */
755 /*-----------------------------------------
756 * check requires of s
759 if ((reqp = s->requires) != ID_NULL)
761 while ((req = *reqp++) != ID_NULL)
763 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
766 dp = GET_PROVIDESP(req, p); /* get providers of req */
768 if (!*dp /* dont care if noone provides rpmlib() */
769 && !strncmp(id2str(pool, req), "rpmlib(", 7))
774 if (dontfix) /* rpm rule, dont care about breakage */
776 for (i = 0; dp[i]; i++)/* for all providers */
778 if (dp[i] >= system->start && dp[i] < system->start + system->nsolvables)
779 break; /* provider is installed */
781 if (!dp[i]) /* no provider found */
783 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));
790 /* nothing provides req! */
792 if (pool->verbose) printf("package %s-%s.%s is not installable (%s)\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), dep2str(pool, req));
794 addrule(solv, -n, 0); /* mark requestor as uninstallable */
796 printf(">!> !unflag %s-%s.%s[%s]\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), source_name(pool_source(pool, s)));
800 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);
801 for (i = 0; dp[i]; i++)
802 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));
804 /* add 'requires' dependency */
805 addrule(solv, -n, dp - pool->whatprovidesdata); /* rule: (-requestor|provider1|provider2|...|providerN) */
807 /* descend the dependency tree */
808 for (; *dp != ID_NULL; dp++) /* loop through all providers */
814 } /* while, requirements of n */
816 } /* if, requirements */
819 /*-----------------------------------------
820 * check conflicts of s
823 if ((conp = s->conflicts) != ID_NULL)
825 while ((con = *conp++) != ID_NULL)
827 FOR_PROVIDES(p, pp, con) /* loop through all providers of this conflict */
829 /* dontfix: dont care about conflicts with already installed packs */
830 if (dontfix && p >= system->start && p < system->start + system->nsolvables)
832 addrule(solv, -n, -p); /* rule: -n|-p: either solvable _or_ provider of conflict */
837 /*-----------------------------------------
838 * check obsoletes if not installed
840 if (!system || n < system->start || n >= (system->start + system->nsolvables))
841 { /* not installed */
842 if ((obsp = s->obsoletes) != ID_NULL)
844 while ((obs = *obsp++) != ID_NULL)
846 FOR_PROVIDES(p, pp, obs)
847 addrule(solv, -n, -p);
850 FOR_PROVIDES(p, pp, s->name)
852 if (s->name == pool->solvables[p].name)
853 addrule(solv, -n, -p);
857 /*-----------------------------------------
858 * add recommends to the rule list
860 if ((recp = s->recommends) != ID_NULL)
861 while ((rec = *recp++) != ID_NULL)
863 FOR_PROVIDES(p, pp, rec)
872 addrulesforsupplements(Solver *solv, Map *m)
874 Pool *pool = solv->pool;
880 if (pool->verbose) printf("addrulesforsupplements... (%d)\n", solv->nrules);
881 for (i = n = 1; n < pool->nsolvables; i++, n++)
883 if (i == pool->nsolvables)
887 s = pool->solvables + i;
888 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
890 if (pool->id2arch && (s->arch > pool->lastarch || !pool->id2arch[s->arch]))
893 if ((supp = s->supplements) != 0)
895 while ((sup = *supp++) != ID_NULL)
897 FOR_PROVIDES(p, pp, sup)
904 if (!sup && (supp = s->freshens) != 0)
906 while ((sup = *supp++) != ID_NULL)
908 FOR_PROVIDES(p, pp, sup)
917 addrulesforsolvable(solv, s, m);
920 if (pool->verbose) printf("done. (%d)\n", solv->nrules);
925 archchanges(Pool *pool, Solvable *s1, Solvable *s2)
927 Id a1 = s1->arch, a2 = s2->arch;
929 /* we allow changes to/from noarch */
930 if (a1 == a2 || a1 == ARCH_NOARCH || a2 == ARCH_NOARCH)
934 a1 = a1 <= pool->lastarch ? pool->id2arch[a1] : 0;
935 a2 = a2 <= pool->lastarch ? pool->id2arch[a2] : 0;
936 if (((a1 ^ a2) & 0xffff0000) != 0)
943 findupdatepackages(Solver *solv, Solvable *s, Queue *qs, Map *m, int allowdowngrade, int allowarchchange)
945 /* system packages get a special upgrade allowed rule */
946 Pool *pool = solv->pool;
947 Id p, *pp, n, p2, *pp2;
955 n = s - pool->solvables;
956 if (m && !MAPTST(m, n)) /* add rule for s if not already done */
957 addrulesforsolvable(solv, s, m);
959 #if defined(GNADENLOS)
960 for (p = 1; p < pool->nsolvables; ++p)
965 if (s->name == pool->solvables[p].name)
968 if ((obsp = pool->solvables[p].obsoletes) != 0) /* provides/obsoletes combination ? */
970 while ((obs = *obsp++) != 0) /* for all obsoletes */
972 FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
974 if (p2 == n) /* match ! */
980 if (!obs) /* continue if no match */
989 * look for updates for s
991 FOR_PROVIDES(p, pp, s->name) /* every provider of s' name */
993 if (p == n) /* skip itself */
996 if (s->name == pool->solvables[p].name) /* name match */
998 if (!allowdowngrade /* consider downgrades ? */
999 && evrcmp(pool, s->evr, pool->solvables[p].evr) > 0)
1002 if (!allowarchchange && archchanges(pool, s, pool->solvables + p))
1005 #if !defined(GNADENLOS)
1006 else if ((obsp = pool->solvables[p].obsoletes) != 0) /* provides/obsoletes combination ? */
1008 while ((obs = *obsp++) != 0) /* for all obsoletes */
1010 FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
1012 if (p2 == n) /* match ! */
1015 if (p2) /* match! */
1018 if (!obs) /* continue if no match */
1020 /* here we have 'p' with a matching provides/obsoletes combination
1021 * thus flagging p as a valid update candidate for s
1029 if (m && !MAPTST(m, p)) /* mark p for install if not already done */
1030 addrulesforsolvable(solv, pool->solvables + p, m);
1035 * add rule for update
1036 * (A|A1|A2|A3...) An = update candidates for A
1038 * s = (installed) solvable
1039 * m = 'addedmap', bit set if 'install' rule for solvable exists
1043 addupdaterule(Solver *solv, Solvable *s, Map *m, int allowdowngrade, int allowarchchange, int dontaddrule)
1045 /* system packages get a special upgrade allowed rule */
1046 Pool *pool = solv->pool;
1052 queueinit_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1053 findupdatepackages(solv, s, &qs, m, allowdowngrade, allowarchchange);
1054 p = s - pool->solvables;
1055 if (dontaddrule) /* we consider update candidates but dont force them */
1061 if (qs.count == 0) /* no updates found */
1064 printf("new update rule: must keep %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1066 addrule(solv, p, 0); /* request 'install' of s */
1071 d = pool_queuetowhatprovides(pool, &qs); /* intern computed provider queue */
1073 r = addrule(solv, p, d); /* allow update of s */
1075 printf("new update rule ");
1082 /*-----------------------------------------------------------------*/
1089 * initial setup for all watches
1093 makewatches(Solver *solv)
1097 int nsolvables = solv->pool->nsolvables;
1099 xfree(solv->watches);
1100 /* lower half for removals, upper half for installs */
1101 solv->watches = (Id *)xcalloc(2 * nsolvables, sizeof(Id));
1103 /* do it reverse so rpm rules get triggered first */
1104 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
1106 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
1109 if (!r->w1 /* rule is disabled */
1110 || !r->w2) /* rule is assertion */
1113 /* see addwatches(solv, r) */
1114 r->n1 = solv->watches[nsolvables + r->w1];
1115 solv->watches[nsolvables + r->w1] = r - solv->rules;
1117 r->n2 = solv->watches[nsolvables + r->w2];
1118 solv->watches[nsolvables + r->w2] = r - solv->rules;
1124 * add watches (for rule)
1128 addwatches(Solver *solv, Rule *r)
1130 int nsolvables = solv->pool->nsolvables;
1132 r->n1 = solv->watches[nsolvables + r->w1];
1133 solv->watches[nsolvables + r->w1] = r - solv->rules;
1135 r->n2 = solv->watches[nsolvables + r->w2];
1136 solv->watches[nsolvables + r->w2] = r - solv->rules;
1140 /*-----------------------------------------------------------------*/
1141 /* rule propagation */
1143 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
1148 * propagate decision to all rules
1152 propagate(Solver *solv, int level)
1154 Pool *pool = solv->pool;
1159 Id *decisionmap = solv->decisionmap;
1160 Id *watches = solv->watches + pool->nsolvables;
1162 while (solv->propagate_index < solv->decisionq.count)
1164 /* negative because our watches trigger if literal goes FALSE */
1165 pkg = -solv->decisionq.elements[solv->propagate_index++];
1167 printf("popagate for decision %d level %d\n", -pkg, level);
1168 printruleelement(solv, 0, -pkg);
1170 for (rp = watches + pkg; *rp; rp = nrp)
1172 r = solv->rules + *rp;
1174 printf(" watch triggered ");
1187 /* if clause is TRUE, nothing to do */
1188 if (DECISIONMAP_TRUE(ow))
1193 /* not a binary clause, check if we need to move our watch */
1194 if (r->p && r->p != ow && !DECISIONMAP_TRUE(-r->p))
1197 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
1198 if (p != ow && !DECISIONMAP_TRUE(-p))
1202 /* p is free to watch, move watch to p */
1205 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));
1207 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));
1221 watches[p] = r - solv->rules;
1225 /* unit clause found, set other watch to TRUE */
1226 if (DECISIONMAP_TRUE(-ow))
1227 return r; /* eek, a conflict! */
1233 decisionmap[ow] = level;
1235 decisionmap[-ow] = -level;
1236 queuepush(&solv->decisionq, ow);
1237 queuepush(&solv->decisionq_why, r - solv->rules);
1240 Solvable *s = pool->solvables + (ow > 0 ? ow : -ow);
1242 printf(" -> decided to install %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1244 printf(" -> decided to conflict %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1249 return 0; /* all is well */
1253 /*-----------------------------------------------------------------*/
1262 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *why)
1264 Pool *pool = solv->pool;
1267 Map seen; /* global? */
1271 int learnt_why = solv->learnt_pool.count;
1272 Id *decisionmap = solv->decisionmap;
1276 if (pool->verbose) printf("ANALYZE at %d ----------------------\n", level);
1277 mapinit(&seen, pool->nsolvables);
1278 idx = solv->decisionq.count;
1282 queuepush(&solv->learnt_pool, c - solv->rules);
1283 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1294 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1296 vv = v > 0 ? v : -v;
1297 if (MAPTST(&seen, vv))
1299 l = solv->decisionmap[vv];
1306 for (j = 0; j < solv->decisionq.count; j++)
1307 if (solv->decisionq.elements[j] == v)
1309 if (j == solv->decisionq.count)
1311 queuepush(&rulq, -(j + 1));
1313 continue; /* initial setting */
1317 num++; /* need to do this one as well */
1322 printf("PUSH %d ", v);
1323 printruleelement(solv, 0, v);
1330 printf("num = %d\n", num);
1336 v = solv->decisionq.elements[--idx];
1337 vv = v > 0 ? v : -v;
1338 if (MAPTST(&seen, vv))
1341 c = solv->rules + solv->decisionq_why.elements[idx];
1349 else if (r.count == 1 && r.elements[0] < 0)
1350 *dr = r.elements[0];
1352 *dr = pool_queuetowhatprovides(pool, &r);
1355 printf("learned rule for level %d (am %d)\n", rlevel, level);
1356 printruleelement(solv, 0, -v);
1357 for (i = 0; i < r.count; i++)
1360 printruleelement(solv, 0, v);
1364 queuepush(&solv->learnt_pool, 0);
1366 for (i = learnt_why; solv->learnt_pool.elements[i]; i++)
1368 printf("learnt_why ");
1369 printrule(solv, solv->rules + solv->learnt_pool.elements[i]);
1380 * reset the solver decisions to right after the rpm rules
1384 reset_solver(Solver *solv)
1390 /* delete all learnt rules */
1391 solv->nrules = solv->learntrules;
1392 QUEUEEMPTY(&solv->learnt_why);
1393 QUEUEEMPTY(&solv->learnt_pool);
1395 /* redo all direct decision without the disabled rules */
1396 for (i = 0; i < solv->decisionq.count; i++)
1398 v = solv->decisionq.elements[i];
1399 solv->decisionmap[v > 0 ? v : -v] = 0;
1401 for (i = 0; i < solv->decisionq_why.count; i++)
1402 if (solv->decisionq_why.elements[i])
1406 v = solv->decisionq.elements[i];
1407 solv->decisionmap[v > 0 ? v : -v] = v > 0 ? 1 : -1;
1410 if (solv->pool->verbose)
1411 printf("decisions done reduced from %d to %d\n", solv->decisionq.count, i);
1413 solv->decisionq_why.count = i;
1414 solv->decisionq.count = i;
1415 solv->recommends_index = -1;
1416 if (i < solv->propagate_index)
1417 solv->propagate_index = i;
1418 /* make direct decisions from enabled unary rules */
1419 for (i = solv->jobrules, r = solv->rules + solv->jobrules; i < solv->nrules; i++, r++)
1421 if (!r->w1 || r->w2)
1427 queuepush(&solv->decisionq, v);
1428 queuepush(&solv->decisionq_why, r - solv->rules);
1429 solv->decisionmap[v > 0 ? v : -v] = v > 0 ? 1 : -1;
1431 if (solv->pool->verbose)
1432 printf("decisions after adding job and system rules: %d\n", solv->decisionq.count);
1433 /* recreate watches */
1439 * analyze_unsolvable_rule
1443 analyze_unsolvable_rule(Solver *solv, Rule *c, int disablerules)
1448 why = c - solv->rules;
1450 if (why >= solv->jobrules && why < solv->systemrules)
1452 if (why >= solv->systemrules && why < solv->learntrules)
1453 printf("SYSTEM %d ", why - solv->systemrules);
1454 if (solv->learntrules && why >= solv->learntrules)
1458 if (solv->learntrules && why >= solv->learntrules)
1460 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1461 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], disablerules);
1464 if (why >= solv->jobrules && why < solv->learntrules)
1468 /* turn off rule for further analysis */
1472 if (solv->problems.count)
1474 for (i = solv->problems.count - 1; i >= 0; i--)
1475 if (solv->problems.elements[i] == 0)
1477 else if (solv->problems.elements[i] == why)
1480 queuepush(&solv->problems, why);
1486 * analyze_unsolvable
1490 analyze_unsolvable(Solver *solv, Rule *c, int disablerules)
1492 Pool *pool = solv->pool;
1493 Map seen; /* global? */
1496 Id *decisionmap = solv->decisionmap;
1499 printf("ANALYZE UNSOLVABLE ----------------------\n");
1501 mapinit(&seen, pool->nsolvables);
1502 analyze_unsolvable_rule(solv, c, disablerules);
1503 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1514 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1516 vv = v > 0 ? v : -v;
1517 l = solv->decisionmap[vv];
1522 idx = solv->decisionq.count;
1525 v = solv->decisionq.elements[--idx];
1526 vv = v > 0 ? v : -v;
1527 if (!MAPTST(&seen, vv))
1529 why = solv->decisionq_why.elements[idx];
1534 printruleelement(solv, 0, v);
1538 c = solv->rules + why;
1539 analyze_unsolvable_rule(solv, c, disablerules);
1540 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1551 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1553 vv = v > 0 ? v : -v;
1554 l = solv->decisionmap[vv];
1561 queuepush(&solv->problems, 0); /* mark end of this problem */
1565 printf("analyze_unsolvables done\n");
1570 /*-----------------------------------------------------------------*/
1571 /* Decision revert */
1575 * revert decision at level
1579 revert(Solver *solv, int level)
1582 while (solv->decisionq.count)
1584 v = solv->decisionq.elements[solv->decisionq.count - 1];
1585 vv = v > 0 ? v : -v;
1586 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1589 printf("reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1591 solv->decisionmap[vv] = 0;
1592 solv->decisionq.count--;
1593 solv->decisionq_why.count--;
1594 solv->propagate_index = solv->decisionq.count;
1596 solv->recommends_index = -1;
1605 watch2onhighest(Solver *solv, Rule *r)
1611 return; /* binary rule, both watches are set */
1612 dp = solv->pool->whatprovidesdata + r->d;
1613 while ((v = *dp++) != 0)
1615 l = solv->decisionmap[v < 0 ? -v : v];
1632 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules)
1642 solv->decisionmap[decision] = level;
1644 solv->decisionmap[-decision] = -level;
1645 queuepush(&solv->decisionq, decision);
1646 queuepush(&solv->decisionq_why, 0);
1650 r = propagate(solv, level);
1655 analyze_unsolvable(solv, r, disablerules);
1660 printf("conflict with rule #%d\n", (int)(r - solv->rules));
1661 l = analyze(solv, level, r, &p, &d, &why);
1662 if (l >= level || l <= 0)
1664 printf("reverting decisions (level %d -> %d)\n", level, l);
1666 revert(solv, level);
1667 r = addrule(solv, p, d); /* p requires d */
1670 if (solv->learnt_why.count != (r - solv->rules) - solv->learntrules)
1672 printf("%d %d\n", solv->learnt_why.count, (int)(r - solv->rules) - solv->learntrules);
1675 queuepush(&solv->learnt_why, why);
1678 /* at least 2 literals, needs watches */
1679 watch2onhighest(solv, r);
1680 addwatches(solv, r);
1682 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1683 queuepush(&solv->decisionq, p);
1684 queuepush(&solv->decisionq_why, r - solv->rules);
1685 printf("decision: ");
1686 printruleelement(solv, 0, p);
1687 printf("new rule: ");
1693 /*-----------------------------------------------------------------*/
1694 /* Main solver interface */
1699 * create solver structure
1701 * pool: all available solvables
1702 * system: installed Solvables
1705 * Upon solving, rules are created to flag the Solvables
1706 * of the 'system' Source as installed.
1710 solver_create(Pool *pool, Source *system)
1713 solv = (Solver *)xcalloc(1, sizeof(Solver));
1715 solv->system = system;
1718 queueinit(&solv->decisionq);
1719 queueinit(&solv->decisionq_why);
1720 queueinit(&solv->problems);
1721 queueinit(&solv->learnt_why);
1722 queueinit(&solv->learnt_pool);
1724 mapinit(&solv->recommends, pool->nsolvables);
1725 mapinit(&solv->suggests, pool->nsolvables);
1726 solv->recommends_index = 0;
1728 solv->decisionmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
1729 solv->rules = (Rule *)xmalloc((solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
1730 memset(solv->rules, 0, sizeof(Rule));
1742 solver_free(Solver *solv)
1744 queuefree(&solv->decisionq);
1745 queuefree(&solv->decisionq_why);
1746 queuefree(&solv->learnt_why);
1747 queuefree(&solv->learnt_pool);
1748 mapfree(&solv->recommends);
1749 mapfree(&solv->suggests);
1750 xfree(solv->decisionmap);
1752 xfree(solv->watches);
1753 xfree(solv->weaksystemrules);
1761 * r->w1 was set to 0, now find proper value for w1
1765 reenablerule(Solver *solv, Rule *r)
1770 if (!r->w2) /* not a rule, but an assertion */
1780 r->w2 = r->d; /* mls: shouldn't this be r->w1 ? */
1784 /* put it on the first not-false literal */
1790 v = solv->pool->whatprovidesdata[r->d + i];
1798 l = solv->decisionmap[v > 0 ? v : -v];
1799 if (!l || (v < 0 && l < 0) || (v > 0 && l > 0))
1806 /*-------------------------------------------------------*/
1811 * all rules have been set up, not actually run the solver
1816 run_solver(Solver *solv, int disablerules, int doweak)
1824 Pool *pool = solv->pool;
1828 printf("number of rules: %d\n", solv->nrules);
1831 for (i = 0; i < solv->nrules; i++)
1833 printrule(solv, solv->rules + i);
1838 /* all new rules are learnt after this point */
1839 solv->learntrules = solv->nrules;
1840 /* crate watches lists */
1843 if (pool->verbose) printf("initial decisions: %d\n", solv->decisionq.count);
1845 /* start SAT algorithm */
1847 systemlevel = level + 1;
1848 if (pool->verbose) printf("solving...\n");
1859 if (pool->verbose) printf("propagating (%d %d)...\n", solv->propagate_index, solv->decisionq.count);
1860 if ((r = propagate(solv, level)) != 0)
1862 analyze_unsolvable(solv, r, disablerules);
1865 printf("UNSOLVABLE\n");
1875 if (level < systemlevel && solv->system->nsolvables)
1877 if (!solv->updatesystem)
1879 /* try to keep as many packages as possible */
1880 if (pool->verbose) printf("installing system packages\n");
1881 for (i = solv->system->start, n = 0; ; i++, n++)
1883 if (n == solv->system->nsolvables)
1885 if (i == solv->system->start + solv->system->nsolvables)
1886 i = solv->system->start;
1887 s = pool->solvables + i;
1888 if (solv->decisionmap[i] != 0)
1891 printf("system installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1894 level = setpropagatelearn(solv, level, i, disablerules);
1897 printf("UNSOLVABLE\n");
1901 if (level <= olevel)
1905 if (solv->weaksystemrules)
1907 if (pool->verbose) printf("installing weak system packages\n");
1908 for (i = solv->system->start, n = 0; ; i++, n++)
1910 if (n == solv->system->nsolvables)
1912 if (solv->decisionmap[i] > 0 || (solv->decisionmap[i] < 0 && solv->weaksystemrules[i - solv->system->start] == 0))
1915 if (solv->decisionmap[i] == 0)
1917 if (solv->weaksystemrules[i - solv->system->start])
1919 dp = pool->whatprovidesdata + solv->weaksystemrules[i - solv->system->start];
1920 while ((p = *dp++) != 0)
1922 if (solv->decisionmap[p] > 0)
1924 if (solv->decisionmap[p] == 0)
1928 continue; /* rule is already true */
1934 prune_to_recommended(solv, &dq);
1936 prune_best_version_arch(pool, &dq);
1938 s = pool->solvables + dq.elements[0];
1939 printf("weak system installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1942 level = setpropagatelearn(solv, level, dq.elements[0], disablerules);
1945 printf("UNSOLVABLE\n");
1949 if (level <= olevel)
1955 if (n != solv->system->nsolvables)
1958 systemlevel = level;
1965 if (pool->verbose) printf("deciding unresolved rules\n");
1966 for (i = 1, n = 1; ; i++, n++)
1968 if (n == solv->nrules)
1970 if (i == solv->nrules)
1972 r = solv->rules + i;
1978 /* binary or unary rule */
1979 /* need two positive undecided literals */
1980 if (r->p < 0 || r->w2 <= 0)
1982 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
1984 queuepush(&dq, r->p);
1985 queuepush(&dq, r->w2);
1990 * all negative literals are installed
1991 * no positive literal is installed
1992 * i.e. the rule is not fulfilled and we
1993 * just need to decide on the positive literals
1997 if (solv->decisionmap[-r->p] <= 0)
2002 if (solv->decisionmap[r->p] > 0)
2004 if (solv->decisionmap[r->p] == 0)
2005 queuepush(&dq, r->p);
2007 dp = pool->whatprovidesdata + r->d;
2008 while ((p = *dp++) != 0)
2012 if (solv->decisionmap[-p] <= 0)
2017 if (solv->decisionmap[p] > 0)
2019 if (solv->decisionmap[p] == 0)
2028 /* cannot happen as this means that
2029 * the rule is unit */
2033 prune_to_recommended(solv, &dq);
2035 prune_best_version_arch(pool, &dq);
2036 p = dq.elements[dq.count - 1];
2037 s = pool->solvables + p;
2039 printf("installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2042 level = setpropagatelearn(solv, level, p, disablerules);
2045 printf("UNSOLVABLE\n");
2049 if (level < systemlevel)
2051 if (level <= olevel)
2053 } /* for(), decide */
2055 if (n != solv->nrules) /* continue if level < systemlevel */
2058 if (doweak && !solv->problems.count)
2062 if (pool->verbose) printf("installing recommended packages\n");
2064 for (i = 1; i < pool->nsolvables; i++)
2066 if (solv->decisionmap[i] < 0)
2068 if (solv->decisionmap[i] > 0)
2070 Id *recp, rec, *pp, p;
2071 s = pool->solvables + i;
2072 /* installed, check for recommends */
2073 /* XXX need to special case AND ? */
2074 if ((recp = s->recommends) != 0)
2076 while ((rec = *recp++) != 0)
2079 FOR_PROVIDES(p, pp, rec)
2081 if (solv->decisionmap[p] > 0)
2086 else if (solv->decisionmap[p] == 0)
2087 queuepushunique(&dq, p);
2095 s = pool->solvables + i;
2096 if (!s->supplements && !s->freshens)
2098 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
2100 if (pool->id2arch && (s->arch > pool->lastarch || !pool->id2arch[s->arch]))
2102 if ((supp = s->supplements) != 0)
2104 while ((sup = *supp++) != 0)
2105 if (dep_fulfilled(solv, sup))
2110 if ((supp = s->freshens) != 0)
2112 while ((sup = *supp++) != 0)
2113 if (dep_fulfilled(solv, sup))
2118 queuepushunique(&dq, i);
2123 prune_best_version_arch(pool, &dq);
2124 p = dq.elements[dq.count - 1];
2125 s = pool->solvables + p;
2127 printf("installing recommended %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2129 level = setpropagatelearn(solv, level, p, 0);
2144 refine_suggestion(Solver *solv, Id *problem, Id sug, Queue *refined)
2152 printf("refine_suggestion start\n");
2153 queueinit(&disabled);
2154 QUEUEEMPTY(refined);
2155 queuepush(refined, sug);
2157 /* re-enable all rules but rule "sug" of the problem */
2158 for (i = 0; problem[i]; i++)
2160 if (problem[i] == sug)
2162 r = solv->rules + problem[i];
2167 reenablerule(solv, r);
2171 revert(solv, 1); /* XXX move to reset_solver? */
2173 QUEUEEMPTY(&solv->problems);
2174 run_solver(solv, 0, 0);
2175 if (!solv->problems.count)
2177 printf("no more problems!\n");
2179 printdecisions(solv);
2181 break; /* great, no more problems */
2183 disabledcnt = disabled.count;
2184 for (i = 0; i < solv->problems.elements[i]; i++)
2186 /* ignore solutions in refined */
2187 v = solv->problems.elements[i];
2188 for (j = 0; problem[j]; j++)
2189 if (problem[j] != sug && problem[j] == v)
2193 queuepush(&disabled, v);
2195 if (disabled.count == disabledcnt)
2197 /* no solution found, this was an invalid suggestion! */
2198 printf("no solution found!\n");
2199 for (i = 0; i < refined->count; i++)
2200 reenablerule(solv, solv->rules + refined->elements[i]);
2204 if (disabled.count == disabledcnt + 1)
2206 /* just one solution, add it to refined list */
2207 queuepush(refined, disabled.elements[disabledcnt]);
2211 printf("############################################## more than one solution found.\n");
2213 for (i = 0; i < solv->problems.elements[i]; i++)
2215 printrule(solv, solv->rules + solv->problems.elements[i]);
2217 printf("##############################################\n");
2219 /* more than one solution */
2220 /* for now return */
2222 for (i = disabledcnt; i < disabled.count; i++)
2224 r = solv->rules + disabled.elements[i];;
2233 /* enable refined rules again */
2234 for (i = 0; i < disabled.count; i++)
2235 reenablerule(solv, solv->rules + disabled.elements[i]);
2236 /* disable problem rules again so that we are in the same state as before */
2237 for (i = 0; problem[i]; i++)
2239 r = solv->rules + problem[i];
2242 printf("refine_suggestion end\n");
2251 id2rc(Solver *solv, Id id)
2254 if (solv->rc_output != 2)
2256 evr = id2str(solv->pool, id);
2257 if (*evr < '0' || *evr > '9')
2259 while (*evr >= '0' && *evr <= '9')
2267 printdecisions(Solver *solv)
2269 Pool *pool = solv->pool;
2270 Id p, *obsoletesmap;
2274 obsoletesmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
2275 for (i = 0; i < solv->decisionq.count; i++)
2280 n = solv->decisionq.elements[i];
2283 if (n >= solv->system->start && n < solv->system->start + solv->system->nsolvables)
2285 s = pool->solvables + n;
2286 if ((obsp = s->obsoletes) != 0)
2287 while ((obs = *obsp++) != 0)
2288 FOR_PROVIDES(p, pp, obs)
2290 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2292 obsoletesmap[p] = n;
2296 FOR_PROVIDES(p, pp, s->name)
2297 if (s->name == pool->solvables[p].name)
2299 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2301 obsoletesmap[p] = n;
2307 if (solv->rc_output)
2308 printf(">!> Solution #1:\n");
2310 int installs = 0, uninstalls = 0, upgrades = 0;
2312 /* print solvables to be erased */
2314 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2316 if (solv->decisionmap[i] > 0)
2318 if (obsoletesmap[i])
2320 s = pool->solvables + i;
2321 if (solv->rc_output == 2)
2322 printf(">!> remove %s-%s%s\n", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2323 else if (solv->rc_output)
2324 printf(">!> remove %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2326 printf("erase %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2330 /* print solvables to be installed */
2332 for (i = 0; i < solv->decisionq.count; i++)
2335 p = solv->decisionq.elements[i];
2338 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2340 s = pool->solvables + p;
2342 if (!obsoletesmap[p])
2344 if (solv->rc_output)
2346 printf("install %s-%s%s", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2347 if (solv->rc_output != 2)
2348 printf(".%s", id2str(pool, s->arch));
2351 else if (!solv->rc_output)
2353 printf("update %s-%s.%s (obsoletes", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2354 for (j = solv->system->start; j < solv->system->start + solv->system->nsolvables; j++)
2356 if (obsoletesmap[j] != p)
2358 s = pool->solvables + j;
2359 printf(" %s-%s.%s", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2366 Solvable *f, *fn = 0;
2367 for (j = solv->system->start; j < solv->system->start + solv->system->nsolvables; j++)
2369 if (obsoletesmap[j] != p)
2371 f = pool->solvables + j;
2372 if (fn || f->name != s->name)
2374 if (solv->rc_output == 2)
2375 printf(">!> remove %s-%s%s\n", id2str(pool, f->name), id2rc(solv, f->evr), id2str(pool, f->evr));
2376 else if (solv->rc_output)
2377 printf(">!> remove %s-%s.%s\n", id2str(pool, f->name), id2str(pool, f->evr), id2str(pool, f->arch));
2385 printf(">!> install %s-%s%s", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2386 if (solv->rc_output != 2)
2387 printf(".%s", id2str(pool, s->arch));
2392 if (solv->rc_output == 2)
2393 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));
2395 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));
2399 if (solv->rc_output)
2401 Source *source = pool_source(pool, s);
2402 if (source && strcmp(source_name(source), "locales"))
2403 printf("[%s]", source_name(source));
2408 if (solv->rc_output)
2409 printf(">!> installs=%d, upgrades=%d, uninstalls=%d\n", installs, upgrades, uninstalls);
2411 xfree(obsoletesmap);
2415 /*-----------------------------------------------------------------*/
2425 solve(Solver *solv, Queue *job)
2427 Pool *pool = solv->pool;
2429 Map addedmap; /* '1' == have rule for solvable */
2430 Map noupdaterule; /* '1' == don't update (scheduled for removal) */
2431 Id how, what, p, *pp, d;
2437 * create basic rule set of all involved packages
2442 mapinit(&addedmap, pool->nsolvables);
2443 mapinit(&noupdaterule, pool->nsolvables);
2448 * create rules for installed solvables -> keep them installed
2449 * so called: rpm rules
2453 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2454 addrulesforsolvable(solv, pool->solvables + i, &addedmap);
2457 * create install rules
2459 * two passes, as we want to keep the rpm rules distinct from the job rules
2465 * process job rules for solvables
2468 for (i = 0; i < job->count; i += 2)
2470 how = job->elements[i];
2471 what = job->elements[i + 1];
2475 case SOLVER_INSTALL_SOLVABLE:
2476 addrulesforsolvable(solv, pool->solvables + what, &addedmap);
2478 case SOLVER_INSTALL_SOLVABLE_NAME:
2479 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
2481 FOR_PROVIDES(p, pp, what)
2483 /* if by name, ensure that the name matches */
2484 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
2486 addrulesforsolvable(solv, pool->solvables + p, &addedmap);
2489 case SOLVER_INSTALL_SOLVABLE_UPDATE:
2490 /* dont allow downgrade */
2491 addupdaterule(solv, pool->solvables + what, &addedmap, 0, 0, 1);
2497 * if unstalls are disallowed, add update rules for every
2498 * installed solvables in the hope to circumvent uninstall
2504 if (!solv->allowuninstall)
2506 /* add update rule for every installed package */
2507 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2508 addupdaterule(solv, pool->solvables + i, &addedmap, solv->allowdowngrade, solv->allowarchchange, 1);
2510 #else /* this is just to add the needed rpm rules to our set */
2511 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2512 addupdaterule(solv, pool->solvables + i, &addedmap, 1, 1, 1);
2515 addrulesforsupplements(solv, &addedmap);
2520 * unify existing rules before going over all job rules
2524 unifyrules(solv); /* remove duplicate rpm rules */
2527 * at this point the system is always solvable,
2528 * as an empty system (remove all packages) is a valid solution
2530 if (pool->verbose) printf("decisions based on rpms: %d\n", solv->decisionq.count);
2533 * now add all job rules
2536 solv->jobrules = solv->nrules;
2538 for (i = 0; i < job->count; i += 2)
2540 how = job->elements[i];
2541 what = job->elements[i + 1];
2544 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
2545 if (solv->rc_output) {
2546 Solvable *s = pool->solvables + what;
2547 printf(">!> Installing %s from channel %s\n", id2str(pool, s->name), source_name(pool_source(pool, s)));
2549 addrule(solv, what, 0); /* install by Id */
2551 case SOLVER_ERASE_SOLVABLE:
2552 addrule(solv, -what, 0); /* remove by Id */
2553 MAPSET(&noupdaterule, what);
2555 case SOLVER_INSTALL_SOLVABLE_NAME: /* install by capability */
2556 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
2558 FOR_PROVIDES(p, pp, what) /* check all providers */
2560 /* if by name, ensure that the name matches */
2561 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
2565 if (!q.count) { /* no provider found -> abort */
2566 fprintf(stderr, "Nothing provides '%s'\n", id2str(pool, what));
2567 /* XXX make this a problem! */
2572 p = queueshift(&q); /* get first provider */
2574 d = 0; /* single provider ? -> make assertion */
2576 d = pool_queuetowhatprovides(pool, &q); /* get all providers */
2577 addrule(solv, p, d); /* add 'requires' rule */
2579 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
2580 case SOLVER_ERASE_SOLVABLE_PROVIDES:
2581 FOR_PROVIDES(p, pp, what)
2583 /* if by name, ensure that the name matches */
2584 if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
2587 addrule(solv, -p, 0); /* add 'remove' rule */
2588 MAPSET(&noupdaterule, p);
2591 case SOLVER_INSTALL_SOLVABLE_UPDATE: /* find update for solvable */
2592 addupdaterule(solv, pool->solvables + what, &addedmap, 0, 0, 0);
2597 if (pool->verbose) printf("problems so far: %d\n", solv->problems.count);
2600 * now add policy rules
2604 solv->systemrules = solv->nrules;
2607 * create rules for updating installed solvables
2613 if (!solv->allowuninstall)
2614 { /* loop over all installed solvables */
2615 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2617 if (!MAPTST(&noupdaterule, i)) /* if not marked as 'noupdate' */
2618 addupdaterule(solv, pool->solvables + i, &addedmap, solv->allowdowngrade, solv->allowarchchange, 0);
2620 addrule(solv, 0, 0); /* place holder */
2622 /* consistency check: we added a rule for _every_ system solvable */
2623 if (solv->nrules - solv->systemrules != solv->system->nsolvables)
2627 if (pool->verbose) printf("problems so far: %d\n", solv->problems.count);
2629 /* create special weak system rules */
2630 if (solv->system->nsolvables)
2632 solv->weaksystemrules = xcalloc(solv->system->nsolvables, sizeof(Id));
2633 for (i = 0; i < solv->system->nsolvables; i++)
2635 findupdatepackages(solv, pool->solvables + solv->system->start + i, &q, (Map *)0, 1, 1);
2637 solv->weaksystemrules[i] = pool_queuetowhatprovides(pool, &q);
2641 /* free unneeded memory */
2643 mapfree(&noupdaterule);
2651 run_solver(solv, 1, 1);
2655 * print solver result
2659 if (pool->verbose) printf("-------------------------------------------------------------\n");
2661 if (solv->problems.count)
2671 clonequeue(&problems, &solv->problems);
2672 queueinit(&solution);
2673 printf("Encountered problems! Here are the solutions:\n");
2674 problem = problems.elements;
2675 for (i = 0; i < problems.count; i++)
2677 Id v = problems.elements[i];
2680 printf("====================================\n");
2681 problem = problems.elements + i + 1;
2684 refine_suggestion(solv, problem, v, &solution);
2685 for (j = 0; j < solution.count; j++)
2687 r = solv->rules + solution.elements[j];
2688 why = solution.elements[j];
2692 if (why >= solv->jobrules && why < solv->systemrules)
2694 /* do a sloppy job of analyzing the job rule */
2697 s = pool->solvables + r->p;
2698 printf("- do not install %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2702 s = pool->solvables - r->p;
2703 printf("- do not erase %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2706 else if (why >= solv->systemrules && why < solv->learntrules)
2709 s = pool->solvables + solv->system->start + (why - solv->systemrules);
2710 if (solv->weaksystemrules && solv->weaksystemrules[why - solv->systemrules])
2712 Id *dp = pool->whatprovidesdata + solv->weaksystemrules[why - solv->systemrules];
2714 if (solv->decisionmap[*dp] > 0)
2716 sd = pool->solvables + *dp;
2722 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));
2726 printf("- allow deinstallation of %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2734 printf("------------------------------------\n");
2739 printdecisions(solv);