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;
122 for (i = j = 0; i < plist->count; i++)
124 s = pool->solvables + plist->elements[i];
125 if (!s->supplements && !s->freshens)
127 if ((supp = s->supplements) != 0)
129 while ((sup = *supp++) != 0)
130 if (dep_fulfilled(solv, sup))
135 if ((supp = s->freshens) != 0)
137 while ((sup = *supp++) != 0)
138 if (dep_fulfilled(solv, sup))
143 plist->elements[j++] = s - pool->solvables;
150 * prune_best_version_arch
152 * sort list of packages (given through plist) by name and evr
153 * return result through plist
157 /* FIXME: must also look at update packages */
160 prune_best_version_arch(Pool *pool, Queue *plist)
167 if (plist->count < 2) /* no need to prune for a single entry */
169 if (pool->verbose) printf("prune_best_version_arch %d\n", plist->count);
171 /* prune to best architecture */
175 for (i = 0; i < plist->count; i++)
177 s = pool->solvables + plist->elements[i];
179 if (a > pool->lastarch)
181 a = pool->id2arch[a];
182 if (!bestscore || (a & 0xffff0000) < bestscore)
183 bestscore = a & 0xffff0000;
185 for (i = j = 0; i < plist->count; i++)
187 s = pool->solvables + plist->elements[i];
189 if (a > pool->lastarch)
191 a = pool->id2arch[a];
192 /* a == 1 -> noarch */
193 if (a != 1 && (a & 0xffff0000) != bestscore)
195 plist->elements[j++] = plist->elements[i];
202 prune_best_version_arch_sortcmp_data = pool;
203 /* sort by name first */
204 qsort(plist->elements, plist->count, sizeof(Id), prune_best_version_arch_sortcmp);
206 /* now find best 'per name' */
207 for (i = j = 0; i < plist->count; i++)
209 s = pool->solvables + plist->elements[i];
210 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
213 if (pool->verbose) printf("- %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
215 if (!best) /* if no best yet, the current is best */
217 best = plist->elements[i];
221 /* name switch: re-init */
222 if (pool->solvables[best].name != s->name) /* new name */
224 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));
225 plist->elements[j++] = best; /* move old best to front */
226 best = plist->elements[i]; /* take current as new best */
230 if (pool->solvables[best].evr != s->evr) /* compare evr */
232 if (evrcmp(pool, pool->solvables[best].evr, s->evr) < 0)
233 best = plist->elements[i];
238 best = plist->elements[0];
240 /* XXX also check obsoletes! */
241 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));
243 plist->elements[j++] = best;
248 /*-----------------------------------------------------------------*/
255 printruleelement(Solver *solv, Rule *r, Id v)
257 Pool *pool = solv->pool;
261 s = pool->solvables + -v;
262 printf(" !%s-%s.%s [%d]", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), -v);
266 s = pool->solvables + v;
267 printf(" %s-%s.%s [%d]", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), v);
276 if (solv->decisionmap[s - pool->solvables] > 0)
277 printf(" I.%d", solv->decisionmap[s - pool->solvables]);
278 if (solv->decisionmap[s - pool->solvables] < 0)
279 printf(" C.%d", -solv->decisionmap[s - pool->solvables]);
289 printrule(Solver *solv, Rule *r)
294 if (r >= solv->rules && r < solv->rules + solv->nrules) /* r is a solver rule */
295 printf("Rule #%d:\n", (int)(r - solv->rules));
297 printf("Rule:\n"); /* r is any rule */
302 else if (r->d == ID_NULL)
309 v = solv->pool->whatprovidesdata[r->d + i - 1];
312 printruleelement(solv, r, v);
314 printf(" next: %d %d\n", r->n1, r->n2);
318 /*-----------------------------------------------------------------*/
324 static Pool *unifyrules_sortcmp_data;
327 * compare rules for unification sort
331 unifyrules_sortcmp(const void *ap, const void *bp)
333 Pool *pool = unifyrules_sortcmp_data;
334 Rule *a = (Rule *)ap;
335 Rule *b = (Rule *)bp;
341 return x; /* p differs */
344 if (a->d == 0 && b->d == 0)
345 return a->w2 - b->w2; /* assertion: return w2 diff */
347 if (a->d == 0) /* a is assertion, b not */
349 x = a->w2 - pool->whatprovidesdata[b->d];
353 if (b->d == 0) /* b is assertion, a not */
355 x = pool->whatprovidesdata[a->d] - b->w2;
359 /* compare whatprovidesdata */
360 ad = pool->whatprovidesdata + a->d;
361 bd = pool->whatprovidesdata + b->d;
362 for (; *ad && *ad == *bd; ad++, bd++)
373 unifyrules(Solver *solv)
378 if (solv->nrules <= 1) /* nothing to unify */
381 /* sort rules first */
382 unifyrules_sortcmp_data = solv->pool;
383 qsort(solv->rules + 1, solv->nrules - 1, sizeof(Rule), unifyrules_sortcmp);
390 for (i = j = 1, ir = solv->rules + 1; i < solv->nrules; i++, ir++)
392 if (jr && !unifyrules_sortcmp(ir, jr))
393 continue; /* prune! */
394 jr = solv->rules + j++; /* keep! */
399 /* reduced count from nrules to j rules */
400 if (solv->pool->verbose) printf("pruned rules from %d to %d\n", solv->nrules, j);
402 /* adapt rule buffer */
403 solv->rules = (Rule *)xrealloc(solv->rules, ((solv->nrules + RULES_BLOCK) & ~RULES_BLOCK) * sizeof(Rule));
412 for (i = 1; i < solv->nrules; i++)
415 if (r->d == 0) /* assertion */
420 dp = solv->pool->whatprovidesdata + r->d;
424 if (solv->pool->verbose)
426 printf(" binary: %d\n", binr);
427 printf(" normal: %d\n", solv->nrules - 1 - binr);
428 printf(" normal lits: %d\n", dc);
441 hashrule(Solver *solv, Id p, Id d, int n)
443 unsigned int x = (unsigned int)p;
447 return (x * 37) ^ (unsigned int)d;
448 dp = solv->pool->whatprovidesdata + d;
450 x = (x * 37) ^ (unsigned int)*dp++;
458 * p = direct literal; > 0 for learnt, < 0 for installed pkg (rpm)
459 * d, if < 0 direct literal, if > 0 offset into whatprovides, if == 0 rule is assertion (look at p only)
462 * A requires b, b provided by B1,B2,B3 => (-A|B1|B2|B3)
464 * p < 0 : rule from rpm (installed pkg)
465 * d > 0 : Offset in whatprovidesdata (list of providers)
467 * A conflicts b, b provided by B1,B2,B3 => (-A|-B1), (-A|-B2), (-A|-B3)
468 * d < 0: Id of solvable (e.g. B1)
470 * d == 0: unary rule, assertion => (A) or (-A)
472 * Install: p > 0, d = 0 (A) user requested install
473 * Remove: p < 0, d = 0 (-A) user requested remove
474 * Requires: p < 0, d > 0 (-A|B1|B2|...) d: <list of providers for requirement of p>
475 * Updates: p > 0, d > 0 (A|B1|B2|...) d: <list of updates for solvable p>
476 * Conflicts: p < 0, d < 0 (-A|-B) either p (conflict issuer) or d (conflict provider)
477 * ? p > 0, d < 0 (A|-B)
478 * No-op ?: p = 0, d = 0 (null) (used as policy rule placeholder)
482 addrule(Solver *solv, Id p, Id d)
487 int n = 0; /* number of literals in rule - 1
488 0 = direct assertion (single literal)
492 /* it often happenes that requires lead to adding the same rpm rule
493 * multiple times, so we prune those duplicates right away to make
494 * the work for unifyrules a bit easier */
496 if (solv->nrules && !solv->jobrules)
498 r = solv->rules + solv->nrules - 1; /* get the last added rule */
499 if (r->p == p && r->d == d && d != 0) /* identical and not user requested */
506 return NULL; /* ignore self conflict */
509 else if (d == 0) /* user requested */
513 for (dp = solv->pool->whatprovidesdata + d; *dp; dp++, n++)
515 return NULL; /* rule is self-fulfilling */
520 if (n == 0) /* direct assertion */
524 /* this is a rpm rule assertion, we do not have to allocate it */
525 /* we can identify it by looking at the decision level, it will be 1 */
528 if (solv->decisionmap[-p] > 0) /* */
530 if (solv->decisionmap[-p]) /* */
532 queuepush(&solv->decisionq, p);
533 queuepush(&solv->decisionq_why, 0);
534 solv->decisionmap[-p] = -1;
539 else if (n == 1 && p > d)
541 /* smallest literal first so we can find dups */
545 n = 1; /* re-set n, was used as temp var */
561 Id *dp2 = solv->pool->whatprovidesdata + r->d;
562 for (dp = solv->pool->whatprovidesdata + d; *dp; dp++, dp2++)
575 /* check and extend rule buffer */
576 if ((solv->nrules & RULES_BLOCK) == 0)
578 solv->rules = (Rule *)xrealloc(solv->rules, (solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
581 r = solv->rules + solv->nrules++; /* point to rule space */
586 /* direct assertion, no watch needed */
602 r->w2 = solv->pool->whatprovidesdata[d];
607 /* we don't add the decision for learnt rules, as the code does that
608 * right after calling addrule anyway */
611 && !solv->learntrules)
613 /* must be system or job rule, as there are only negative unary rpm rules */
614 Id vv = p > 0 ? p : -p;
615 if (solv->decisionmap[vv])
618 if (solv->decisionmap[vv] > 0 && p > 0)
620 if (solv->decisionmap[vv] < 0 && p < 0)
622 /* direct conflict! */
623 for (i = 0; i < solv->decisionq.count; i++)
625 if (solv->decisionq.elements[i] == -p)
628 if (i == solv->decisionq.count)
630 if (solv->decisionq_why.elements[i] == 0)
632 /* conflict with rpm rule */
633 queuepush(&solv->problems, r - solv->rules);
634 queuepush(&solv->problems, 0);
635 r->w1 = 0; /* disable */
638 /* conflict with other job or system rule */
639 queuepush(&solv->problems, solv->decisionq_why.elements[i]);
640 queuepush(&solv->problems, r - solv->rules);
641 queuepush(&solv->problems, 0);
642 r->w1 = 0; /* disable */
643 /* also disable conflicting rule */
644 solv->rules[solv->decisionq_why.elements[i]].w1 = 0;
645 /* XXX: remove from decisionq! */
646 printf("XXX remove from decisionq\n");
649 queuepush(&solv->decisionq, p);
650 queuepush(&solv->decisionq_why, r - solv->rules);
651 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? 1 : -1;
658 * add (install) rules for solvable
663 addrulesforsolvable(Solver *solv, Solvable *s, Map *m)
665 Pool *pool = solv->pool;
666 Source *system = solv->system;
679 queueinit_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
680 queuepush(&q, s - pool->solvables); /* push solvable Id */
686 * s: Pointer to solvable
690 if (MAPTST(m, n)) /* continue if already set in map */
694 s = pool->solvables + n; /* s = Solvable in question */
697 if (system /* have rpm */
699 && n >= system->start /* its an rpm rule */
700 && n < system->start + system->nsolvables)
702 dontfix = 1; /* dont care about broken rpm deps */
705 /*-----------------------------------------
706 * check requires of s
709 if ((reqp = s->requires) != ID_NULL)
711 while ((req = *reqp++) != ID_NULL)
713 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
716 dp = GET_PROVIDESP(req, p); /* get providers of req */
718 if (!*dp /* dont care if noone provides rpmlib() */
719 && !strncmp(id2str(pool, req), "rpmlib(", 7))
724 if (dontfix) /* rpm rule, dont care about breakage */
726 for (i = 0; dp[i]; i++)/* for all providers */
728 if (dp[i] >= system->start && dp[i] < system->start + system->nsolvables)
729 break; /* provider is installed */
731 if (!dp[i]) /* no provider found */
733 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));
740 /* nothing provides req! */
742 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));
744 addrule(solv, -n, 0); /* mark requestor as uninstallable */
746 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)));
750 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);
751 for (i = 0; dp[i]; i++)
752 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));
754 /* add 'requires' dependency */
755 addrule(solv, -n, dp - pool->whatprovidesdata); /* rule: (-requestor|provider1|provider2|...|providerN) */
757 /* descend the dependency tree */
758 for (; *dp != ID_NULL; dp++) /* loop through all providers */
764 } /* while, requirements of n */
766 } /* if, requirements */
769 /*-----------------------------------------
770 * check conflicts of s
773 if ((conp = s->conflicts) != ID_NULL)
775 while ((con = *conp++) != ID_NULL)
777 FOR_PROVIDES(p, pp, con) /* loop through all providers of this conflict */
779 /* dontfix: dont care about conflicts with already installed packs */
780 if (dontfix && p >= system->start && p < system->start + system->nsolvables)
782 addrule(solv, -n, -p); /* rule: -n|-p: either solvable _or_ provider of conflict */
787 /*-----------------------------------------
788 * check obsoletes if not installed
790 if (!system || n < system->start || n >= (system->start + system->nsolvables))
791 { /* not installed */
792 if ((obsp = s->obsoletes) != ID_NULL)
794 while ((obs = *obsp++) != ID_NULL)
796 FOR_PROVIDES(p, pp, obs)
797 addrule(solv, -n, -p);
800 FOR_PROVIDES(p, pp, s->name)
802 if (s->name == pool->solvables[p].name)
803 addrule(solv, -n, -p);
807 /*-----------------------------------------
808 * add recommends to the rule list
810 if ((recp = s->recommends) != ID_NULL)
811 while ((rec = *recp++) != ID_NULL)
813 FOR_PROVIDES(p, pp, rec)
822 addrulesforsupplements(Solver *solv, Map *m)
824 Pool *pool = solv->pool;
830 if (pool->verbose) printf("addrulesforsupplements... (%d)\n", solv->nrules);
831 for (i = n = 1; n < pool->nsolvables; i++, n++)
833 if (i == pool->nsolvables)
837 s = pool->solvables + i;
838 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
840 if (pool->id2arch && (s->arch > pool->lastarch || !pool->id2arch[s->arch]))
843 if ((supp = s->supplements) != 0)
845 while ((sup = *supp++) != ID_NULL)
847 FOR_PROVIDES(p, pp, sup)
854 if (!sup && (supp = s->freshens) != 0)
856 while ((sup = *supp++) != ID_NULL)
858 FOR_PROVIDES(p, pp, sup)
867 addrulesforsolvable(solv, s, m);
870 if (pool->verbose) printf("done. (%d)\n", solv->nrules);
875 archchanges(Pool *pool, Solvable *s1, Solvable *s2)
877 Id a1 = s1->arch, a2 = s2->arch;
879 /* we allow changes to/from noarch */
880 if (a1 == a2 || a1 == ARCH_NOARCH || a2 == ARCH_NOARCH)
884 a1 = a1 <= pool->lastarch ? pool->id2arch[a1] : 0;
885 a2 = a2 <= pool->lastarch ? pool->id2arch[a2] : 0;
886 if (((a1 ^ a2) & 0xffff0000) != 0)
893 findupdatepackages(Solver *solv, Solvable *s, Queue *qs, Map *m, int allowdowngrade, int allowarchchange)
895 /* system packages get a special upgrade allowed rule */
896 Pool *pool = solv->pool;
897 Id p, *pp, n, p2, *pp2;
905 n = s - pool->solvables;
906 if (m && !MAPTST(m, n)) /* add rule for s if not already done */
907 addrulesforsolvable(solv, s, m);
909 #if defined(GNADENLOS)
910 for (p = 1; p < pool->nsolvables; ++p)
915 if (s->name == pool->solvables[p].name)
918 if ((obsp = pool->solvables[p].obsoletes) != 0) /* provides/obsoletes combination ? */
920 while ((obs = *obsp++) != 0) /* for all obsoletes */
922 FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
924 if (p2 == n) /* match ! */
930 if (!obs) /* continue if no match */
939 * look for updates for s
941 FOR_PROVIDES(p, pp, s->name) /* every provider of s' name */
943 if (p == n) /* skip itself */
946 if (s->name == pool->solvables[p].name) /* name match */
948 if (!allowdowngrade /* consider downgrades ? */
949 && evrcmp(pool, s->evr, pool->solvables[p].evr) > 0)
952 if (!allowarchchange && archchanges(pool, s, pool->solvables + p))
955 #if !defined(GNADENLOS)
956 else if ((obsp = pool->solvables[p].obsoletes) != 0) /* provides/obsoletes combination ? */
958 while ((obs = *obsp++) != 0) /* for all obsoletes */
960 FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
962 if (p2 == n) /* match ! */
968 if (!obs) /* continue if no match */
970 /* here we have 'p' with a matching provides/obsoletes combination
971 * thus flagging p as a valid update candidate for s
979 if (m && !MAPTST(m, p)) /* mark p for install if not already done */
980 addrulesforsolvable(solv, pool->solvables + p, m);
985 * add rule for update
986 * (A|A1|A2|A3...) An = update candidates for A
988 * s = (installed) solvable
989 * m = 'addedmap', bit set if 'install' rule for solvable exists
993 addupdaterule(Solver *solv, Solvable *s, Map *m, int allowdowngrade, int allowarchchange, int dontaddrule)
995 /* system packages get a special upgrade allowed rule */
996 Pool *pool = solv->pool;
1002 queueinit_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1003 findupdatepackages(solv, s, &qs, m, allowdowngrade, allowarchchange);
1004 p = s - pool->solvables;
1005 if (dontaddrule) /* we consider update candidates but dont force them */
1011 if (qs.count == 0) /* no updates found */
1014 printf("new update rule: must keep %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1016 addrule(solv, p, 0); /* request 'install' of s */
1021 d = pool_queuetowhatprovides(pool, &qs); /* intern computed provider queue */
1023 r = addrule(solv, p, d); /* allow update of s */
1025 printf("new update rule ");
1032 /*-----------------------------------------------------------------*/
1039 * initial setup for all watches
1043 makewatches(Solver *solv)
1047 int nsolvables = solv->pool->nsolvables;
1049 xfree(solv->watches);
1050 /* lower half for removals, upper half for installs */
1051 solv->watches = (Id *)xcalloc(2 * nsolvables, sizeof(Id));
1053 /* do it reverse so rpm rules get triggered first */
1054 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
1056 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
1059 if (!r->w1 /* rule is disabled */
1060 || !r->w2) /* rule is assertion */
1063 /* see addwatches(solv, r) */
1064 r->n1 = solv->watches[nsolvables + r->w1];
1065 solv->watches[nsolvables + r->w1] = r - solv->rules;
1067 r->n2 = solv->watches[nsolvables + r->w2];
1068 solv->watches[nsolvables + r->w2] = r - solv->rules;
1074 * add watches (for rule)
1078 addwatches(Solver *solv, Rule *r)
1080 int nsolvables = solv->pool->nsolvables;
1082 r->n1 = solv->watches[nsolvables + r->w1];
1083 solv->watches[nsolvables + r->w1] = r - solv->rules;
1085 r->n2 = solv->watches[nsolvables + r->w2];
1086 solv->watches[nsolvables + r->w2] = r - solv->rules;
1090 /*-----------------------------------------------------------------*/
1091 /* rule propagation */
1093 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
1098 * propagate decision to all rules
1102 propagate(Solver *solv, int level)
1104 Pool *pool = solv->pool;
1109 Id *decisionmap = solv->decisionmap;
1110 Id *watches = solv->watches + pool->nsolvables;
1112 while (solv->propagate_index < solv->decisionq.count)
1114 /* negative because our watches trigger if literal goes FALSE */
1115 pkg = -solv->decisionq.elements[solv->propagate_index++];
1117 printf("popagate for decision %d level %d\n", -pkg, level);
1118 printruleelement(solv, 0, -pkg);
1120 for (rp = watches + pkg; *rp; rp = nrp)
1122 r = solv->rules + *rp;
1124 printf(" watch triggered ");
1137 /* if clause is TRUE, nothing to do */
1138 if (DECISIONMAP_TRUE(ow))
1143 /* not a binary clause, check if we need to move our watch */
1144 if (r->p && r->p != ow && !DECISIONMAP_TRUE(-r->p))
1147 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
1148 if (p != ow && !DECISIONMAP_TRUE(-p))
1152 /* p is free to watch, move watch to p */
1155 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));
1157 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));
1171 watches[p] = r - solv->rules;
1175 /* unit clause found, set other watch to TRUE */
1176 if (DECISIONMAP_TRUE(-ow))
1177 return r; /* eek, a conflict! */
1183 decisionmap[ow] = level;
1185 decisionmap[-ow] = -level;
1186 queuepush(&solv->decisionq, ow);
1187 queuepush(&solv->decisionq_why, r - solv->rules);
1190 Solvable *s = pool->solvables + (ow > 0 ? ow : -ow);
1192 printf(" -> decided to install %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1194 printf(" -> decided to conflict %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1199 return 0; /* all is well */
1203 /*-----------------------------------------------------------------*/
1212 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *why)
1214 Pool *pool = solv->pool;
1217 Map seen; /* global? */
1221 int learnt_why = solv->learnt_pool.count;
1222 Id *decisionmap = solv->decisionmap;
1226 if (pool->verbose) printf("ANALYZE at %d ----------------------\n", level);
1227 mapinit(&seen, pool->nsolvables);
1228 idx = solv->decisionq.count;
1232 queuepush(&solv->learnt_pool, c - solv->rules);
1233 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1244 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1246 vv = v > 0 ? v : -v;
1247 if (MAPTST(&seen, vv))
1249 l = solv->decisionmap[vv];
1256 for (j = 0; j < solv->decisionq.count; j++)
1257 if (solv->decisionq.elements[j] == v)
1259 if (j == solv->decisionq.count)
1261 queuepush(&rulq, -(j + 1));
1263 continue; /* initial setting */
1267 num++; /* need to do this one as well */
1272 printf("PUSH %d ", v);
1273 printruleelement(solv, 0, v);
1280 printf("num = %d\n", num);
1286 v = solv->decisionq.elements[--idx];
1287 vv = v > 0 ? v : -v;
1288 if (MAPTST(&seen, vv))
1291 c = solv->rules + solv->decisionq_why.elements[idx];
1299 else if (r.count == 1 && r.elements[0] < 0)
1300 *dr = r.elements[0];
1302 *dr = pool_queuetowhatprovides(pool, &r);
1305 printf("learned rule for level %d (am %d)\n", rlevel, level);
1306 printruleelement(solv, 0, -v);
1307 for (i = 0; i < r.count; i++)
1310 printruleelement(solv, 0, v);
1314 queuepush(&solv->learnt_pool, 0);
1316 for (i = learnt_why; solv->learnt_pool.elements[i]; i++)
1318 printf("learnt_why ");
1319 printrule(solv, solv->rules + solv->learnt_pool.elements[i]);
1330 * reset the solver decisions to right after the rpm rules
1334 reset_solver(Solver *solv)
1340 /* delete all learnt rules */
1341 solv->nrules = solv->learntrules;
1342 QUEUEEMPTY(&solv->learnt_why);
1343 QUEUEEMPTY(&solv->learnt_pool);
1345 /* redo all direct decision without the disabled rules */
1346 for (i = 0; i < solv->decisionq.count; i++)
1348 v = solv->decisionq.elements[i];
1349 solv->decisionmap[v > 0 ? v : -v] = 0;
1351 for (i = 0; i < solv->decisionq_why.count; i++)
1352 if (solv->decisionq_why.elements[i])
1356 v = solv->decisionq.elements[i];
1357 solv->decisionmap[v > 0 ? v : -v] = v > 0 ? 1 : -1;
1360 if (solv->pool->verbose)
1361 printf("decisions done reduced from %d to %d\n", solv->decisionq.count, i);
1363 solv->decisionq_why.count = i;
1364 solv->decisionq.count = i;
1365 if (i < solv->propagate_index)
1366 solv->propagate_index = i;
1367 /* make direct decisions from enabled unary rules */
1368 for (i = solv->jobrules, r = solv->rules + solv->jobrules; i < solv->nrules; i++, r++)
1370 if (!r->w1 || r->w2)
1376 queuepush(&solv->decisionq, v);
1377 queuepush(&solv->decisionq_why, r - solv->rules);
1378 solv->decisionmap[v > 0 ? v : -v] = v > 0 ? 1 : -1;
1380 if (solv->pool->verbose)
1381 printf("decisions after adding job and system rules: %d\n", solv->decisionq.count);
1382 /* recreate watches */
1388 * analyze_unsolvable_rule
1392 analyze_unsolvable_rule(Solver *solv, Rule *c, int disablerules)
1397 why = c - solv->rules;
1399 if (why >= solv->jobrules && why < solv->systemrules)
1401 if (why >= solv->systemrules && why < solv->learntrules)
1402 printf("SYSTEM %d ", why - solv->systemrules);
1403 if (solv->learntrules && why >= solv->learntrules)
1407 if (solv->learntrules && why >= solv->learntrules)
1409 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1410 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], disablerules);
1413 if (why >= solv->jobrules && why < solv->learntrules)
1417 /* turn off rule for further analysis */
1421 if (solv->problems.count)
1423 for (i = solv->problems.count - 1; i >= 0; i--)
1424 if (solv->problems.elements[i] == 0)
1426 else if (solv->problems.elements[i] == why)
1429 queuepush(&solv->problems, why);
1435 * analyze_unsolvable
1439 analyze_unsolvable(Solver *solv, Rule *c, int disablerules)
1441 Pool *pool = solv->pool;
1442 Map seen; /* global? */
1445 Id *decisionmap = solv->decisionmap;
1448 printf("ANALYZE UNSOLVABLE ----------------------\n");
1450 mapinit(&seen, pool->nsolvables);
1451 analyze_unsolvable_rule(solv, c, disablerules);
1452 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1463 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1465 vv = v > 0 ? v : -v;
1466 l = solv->decisionmap[vv];
1471 idx = solv->decisionq.count;
1474 v = solv->decisionq.elements[--idx];
1475 vv = v > 0 ? v : -v;
1476 if (!MAPTST(&seen, vv))
1478 why = solv->decisionq_why.elements[idx];
1483 printruleelement(solv, 0, v);
1487 c = solv->rules + why;
1488 analyze_unsolvable_rule(solv, c, disablerules);
1489 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1500 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1502 vv = v > 0 ? v : -v;
1503 l = solv->decisionmap[vv];
1510 queuepush(&solv->problems, 0); /* mark end of this problem */
1514 printf("analyze_unsolvables done\n");
1519 /*-----------------------------------------------------------------*/
1520 /* Decision revert */
1524 * revert decision at level
1528 revert(Solver *solv, int level)
1531 while (solv->decisionq.count)
1533 v = solv->decisionq.elements[solv->decisionq.count - 1];
1534 vv = v > 0 ? v : -v;
1535 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1538 printf("reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1540 solv->decisionmap[vv] = 0;
1541 solv->decisionq.count--;
1542 solv->decisionq_why.count--;
1543 solv->propagate_index = solv->decisionq.count;
1553 watch2onhighest(Solver *solv, Rule *r)
1559 return; /* binary rule, both watches are set */
1560 dp = solv->pool->whatprovidesdata + r->d;
1561 while ((v = *dp++) != 0)
1563 l = solv->decisionmap[v < 0 ? -v : v];
1580 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules)
1590 solv->decisionmap[decision] = level;
1592 solv->decisionmap[-decision] = -level;
1593 queuepush(&solv->decisionq, decision);
1594 queuepush(&solv->decisionq_why, 0);
1598 r = propagate(solv, level);
1603 analyze_unsolvable(solv, r, disablerules);
1608 printf("conflict with rule #%d\n", (int)(r - solv->rules));
1609 l = analyze(solv, level, r, &p, &d, &why);
1610 if (l >= level || l <= 0)
1612 printf("reverting decisions (level %d -> %d)\n", level, l);
1614 revert(solv, level);
1615 r = addrule(solv, p, d); /* p requires d */
1618 if (solv->learnt_why.count != (r - solv->rules) - solv->learntrules)
1620 printf("%d %d\n", solv->learnt_why.count, (int)(r - solv->rules) - solv->learntrules);
1623 queuepush(&solv->learnt_why, why);
1626 /* at least 2 literals, needs watches */
1627 watch2onhighest(solv, r);
1628 addwatches(solv, r);
1630 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1631 queuepush(&solv->decisionq, p);
1632 queuepush(&solv->decisionq_why, r - solv->rules);
1633 printf("decision: ");
1634 printruleelement(solv, 0, p);
1635 printf("new rule: ");
1641 /*-----------------------------------------------------------------*/
1642 /* Main solver interface */
1647 * create solver structure
1649 * pool: all available solvables
1650 * system: installed Solvables
1653 * Upon solving, rules are created to flag the Solvables
1654 * of the 'system' Source as installed.
1658 solver_create(Pool *pool, Source *system)
1661 solv = (Solver *)xcalloc(1, sizeof(Solver));
1663 solv->system = system;
1666 queueinit(&solv->decisionq);
1667 queueinit(&solv->decisionq_why);
1668 queueinit(&solv->problems);
1669 queueinit(&solv->learnt_why);
1670 queueinit(&solv->learnt_pool);
1672 solv->decisionmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
1673 solv->rules = (Rule *)xmalloc((solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
1674 memset(solv->rules, 0, sizeof(Rule));
1686 solver_free(Solver *solv)
1688 queuefree(&solv->decisionq);
1689 queuefree(&solv->decisionq_why);
1690 queuefree(&solv->learnt_why);
1691 queuefree(&solv->learnt_pool);
1692 xfree(solv->decisionmap);
1694 xfree(solv->watches);
1695 xfree(solv->weaksystemrules);
1703 * r->w1 was set to 0, now find proper value for w1
1707 reenablerule(Solver *solv, Rule *r)
1712 if (!r->w2) /* not a rule, but an assertion */
1722 r->w2 = r->d; /* mls: shouldn't this be r->w1 ? */
1726 /* put it on the first not-false literal */
1732 v = solv->pool->whatprovidesdata[r->d + i];
1740 l = solv->decisionmap[v > 0 ? v : -v];
1741 if (!l || (v < 0 && l < 0) || (v > 0 && l > 0))
1748 /*-------------------------------------------------------*/
1753 * all rules have been set up, not actually run the solver
1758 run_solver(Solver *solv, int disablerules, int doweak)
1766 Pool *pool = solv->pool;
1770 printf("number of rules: %d\n", solv->nrules);
1773 for (i = 0; i < solv->nrules; i++)
1775 printrule(solv, solv->rules + i);
1780 /* all new rules are learnt after this point */
1781 solv->learntrules = solv->nrules;
1782 /* crate watches lists */
1785 if (pool->verbose) printf("initial decisions: %d\n", solv->decisionq.count);
1787 /* start SAT algorithm */
1789 systemlevel = level + 1;
1790 if (pool->verbose) printf("solving...\n");
1801 if (pool->verbose) printf("propagating (%d %d)...\n", solv->propagate_index, solv->decisionq.count);
1802 if ((r = propagate(solv, level)) != 0)
1804 analyze_unsolvable(solv, r, disablerules);
1807 printf("UNSOLVABLE\n");
1817 if (level < systemlevel && solv->system->nsolvables)
1819 if (!solv->updatesystem)
1821 /* try to keep as many packages as possible */
1822 if (pool->verbose) printf("installing system packages\n");
1823 for (i = solv->system->start, n = 0; ; i++, n++)
1825 if (n == solv->system->nsolvables)
1827 if (i == solv->system->start + solv->system->nsolvables)
1828 i = solv->system->start;
1829 s = pool->solvables + i;
1830 if (solv->decisionmap[i] != 0)
1833 printf("system installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1836 level = setpropagatelearn(solv, level, i, disablerules);
1839 printf("UNSOLVABLE\n");
1843 if (level <= olevel)
1847 if (solv->weaksystemrules)
1849 if (pool->verbose) printf("installing weak system packages\n");
1850 for (i = solv->system->start, n = 0; ; i++, n++)
1852 if (n == solv->system->nsolvables)
1854 if (solv->decisionmap[i] > 0 || (solv->decisionmap[i] < 0 && solv->weaksystemrules[i - solv->system->start] == 0))
1857 if (solv->decisionmap[i] == 0)
1859 if (solv->weaksystemrules[i - solv->system->start])
1861 dp = pool->whatprovidesdata + solv->weaksystemrules[i - solv->system->start];
1862 while ((p = *dp++) != 0)
1864 if (solv->decisionmap[p] > 0)
1866 if (solv->decisionmap[p] == 0)
1870 continue; /* rule is already true */
1876 prune_to_recommended(solv, &dq);
1878 prune_best_version_arch(pool, &dq);
1880 s = pool->solvables + dq.elements[0];
1881 printf("weak system installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1884 level = setpropagatelearn(solv, level, dq.elements[0], disablerules);
1887 printf("UNSOLVABLE\n");
1891 if (level <= olevel)
1897 if (n != solv->system->nsolvables)
1900 systemlevel = level;
1907 if (pool->verbose) printf("deciding unresolved rules\n");
1908 for (i = 1, n = 1; ; i++, n++)
1910 if (n == solv->nrules)
1912 if (i == solv->nrules)
1914 r = solv->rules + i;
1920 /* binary or unary rule */
1921 /* need two positive undecided literals */
1922 if (r->p < 0 || r->w2 <= 0)
1924 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
1926 queuepush(&dq, r->p);
1927 queuepush(&dq, r->w2);
1932 * all negative literals are installed
1933 * no positive literal is installed
1934 * i.e. the rule is not fulfilled and we
1935 * just need to decide on the positive literals
1939 if (solv->decisionmap[-r->p] <= 0)
1944 if (solv->decisionmap[r->p] > 0)
1946 if (solv->decisionmap[r->p] == 0)
1947 queuepush(&dq, r->p);
1949 dp = pool->whatprovidesdata + r->d;
1950 while ((p = *dp++) != 0)
1954 if (solv->decisionmap[-p] <= 0)
1959 if (solv->decisionmap[p] > 0)
1961 if (solv->decisionmap[p] == 0)
1970 /* cannot happen as this means that
1971 * the rule is unit */
1975 prune_to_recommended(solv, &dq);
1977 prune_best_version_arch(pool, &dq);
1978 p = dq.elements[dq.count - 1];
1979 s = pool->solvables + p;
1981 printf("installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1984 level = setpropagatelearn(solv, level, p, disablerules);
1987 printf("UNSOLVABLE\n");
1991 if (level < systemlevel)
1993 if (level <= olevel)
1995 } /* for(), decide */
1997 if (n != solv->nrules) /* continue if level < systemlevel */
2000 if (doweak && !solv->problems.count)
2004 if (pool->verbose) printf("installing recommended packages\n");
2006 for (i = 1; i < pool->nsolvables; i++)
2008 if (solv->decisionmap[i] < 0)
2010 if (solv->decisionmap[i] > 0)
2012 Id *recp, rec, *pp, p;
2013 s = pool->solvables + i;
2014 /* installed, check for recommends */
2015 /* XXX need to special case AND ? */
2016 if ((recp = s->recommends) != 0)
2018 while ((rec = *recp++) != 0)
2021 FOR_PROVIDES(p, pp, rec)
2023 if (solv->decisionmap[p] > 0)
2028 else if (solv->decisionmap[p] == 0)
2029 queuepushunique(&dq, p);
2037 s = pool->solvables + i;
2038 if (!s->supplements && !s->freshens)
2040 if (s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
2042 if (pool->id2arch && (s->arch > pool->lastarch || !pool->id2arch[s->arch]))
2044 if ((supp = s->supplements) != 0)
2046 while ((sup = *supp++) != 0)
2047 if (dep_fulfilled(solv, sup))
2052 if ((supp = s->freshens) != 0)
2054 while ((sup = *supp++) != 0)
2055 if (dep_fulfilled(solv, sup))
2060 queuepushunique(&dq, i);
2065 prune_best_version_arch(pool, &dq);
2066 p = dq.elements[dq.count - 1];
2067 s = pool->solvables + p;
2069 printf("installing recommended %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2071 level = setpropagatelearn(solv, level, p, 0);
2086 refine_suggestion(Solver *solv, Id *problem, Id sug, Queue *refined)
2094 printf("refine_suggestion start\n");
2095 queueinit(&disabled);
2096 QUEUEEMPTY(refined);
2097 queuepush(refined, sug);
2099 /* re-enable all rules but rule "sug" of the problem */
2100 for (i = 0; problem[i]; i++)
2102 if (problem[i] == sug)
2104 r = solv->rules + problem[i];
2109 reenablerule(solv, r);
2113 revert(solv, 1); /* XXX move to reset_solver? */
2115 QUEUEEMPTY(&solv->problems);
2116 run_solver(solv, 0, 0);
2117 if (!solv->problems.count)
2119 printf("no more problems!\n");
2121 printdecisions(solv);
2123 break; /* great, no more problems */
2125 disabledcnt = disabled.count;
2126 for (i = 0; i < solv->problems.elements[i]; i++)
2128 /* ignore solutions in refined */
2129 v = solv->problems.elements[i];
2130 for (j = 0; problem[j]; j++)
2131 if (problem[j] != sug && problem[j] == v)
2135 queuepush(&disabled, v);
2137 if (disabled.count == disabledcnt)
2139 /* no solution found, this was an invalid suggestion! */
2140 printf("no solution found!\n");
2141 for (i = 0; i < refined->count; i++)
2142 reenablerule(solv, solv->rules + refined->elements[i]);
2146 if (disabled.count == disabledcnt + 1)
2148 /* just one solution, add it to refined list */
2149 queuepush(refined, disabled.elements[disabledcnt]);
2153 printf("############################################## more than one solution found.\n");
2155 for (i = 0; i < solv->problems.elements[i]; i++)
2157 printrule(solv, solv->rules + solv->problems.elements[i]);
2159 printf("##############################################\n");
2161 /* more than one solution */
2162 /* for now return */
2164 for (i = disabledcnt; i < disabled.count; i++)
2166 r = solv->rules + disabled.elements[i];;
2175 /* enable refined rules again */
2176 for (i = 0; i < disabled.count; i++)
2177 reenablerule(solv, solv->rules + disabled.elements[i]);
2178 /* disable problem rules again so that we are in the same state as before */
2179 for (i = 0; problem[i]; i++)
2181 r = solv->rules + problem[i];
2184 printf("refine_suggestion end\n");
2193 id2rc(Solver *solv, Id id)
2196 if (solv->rc_output != 2)
2198 evr = id2str(solv->pool, id);
2199 if (*evr < '0' || *evr > '9')
2201 while (*evr >= '0' && *evr <= '9')
2209 printdecisions(Solver *solv)
2211 Pool *pool = solv->pool;
2212 Id p, *obsoletesmap;
2216 obsoletesmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
2217 for (i = 0; i < solv->decisionq.count; i++)
2222 n = solv->decisionq.elements[i];
2225 if (n >= solv->system->start && n < solv->system->start + solv->system->nsolvables)
2227 s = pool->solvables + n;
2228 if ((obsp = s->obsoletes) != 0)
2229 while ((obs = *obsp++) != 0)
2230 FOR_PROVIDES(p, pp, obs)
2232 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2234 obsoletesmap[p] = n;
2238 FOR_PROVIDES(p, pp, s->name)
2239 if (s->name == pool->solvables[p].name)
2241 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2243 obsoletesmap[p] = n;
2249 if (solv->rc_output)
2250 printf(">!> Solution #1:\n");
2252 int installs = 0, uninstalls = 0, upgrades = 0;
2254 /* print solvables to be erased */
2256 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2258 if (solv->decisionmap[i] > 0)
2260 if (obsoletesmap[i])
2262 s = pool->solvables + i;
2263 if (solv->rc_output == 2)
2264 printf(">!> remove %s-%s%s\n", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2265 else if (solv->rc_output)
2266 printf(">!> remove %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2268 printf("erase %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2272 /* print solvables to be installed */
2274 for (i = 0; i < solv->decisionq.count; i++)
2277 p = solv->decisionq.elements[i];
2280 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2282 s = pool->solvables + p;
2284 if (!obsoletesmap[p])
2286 if (solv->rc_output)
2288 printf("install %s-%s%s", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2289 if (solv->rc_output != 2)
2290 printf(".%s", id2str(pool, s->arch));
2293 else if (!solv->rc_output)
2295 printf("update %s-%s.%s (obsoletes", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2296 for (j = solv->system->start; j < solv->system->start + solv->system->nsolvables; j++)
2298 if (obsoletesmap[j] != p)
2300 s = pool->solvables + j;
2301 printf(" %s-%s.%s", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2308 Solvable *f, *fn = 0;
2309 for (j = solv->system->start; j < solv->system->start + solv->system->nsolvables; j++)
2311 if (obsoletesmap[j] != p)
2313 f = pool->solvables + j;
2314 if (fn || f->name != s->name)
2316 if (solv->rc_output == 2)
2317 printf(">!> remove %s-%s%s\n", id2str(pool, f->name), id2rc(solv, f->evr), id2str(pool, f->evr));
2318 else if (solv->rc_output)
2319 printf(">!> remove %s-%s.%s\n", id2str(pool, f->name), id2str(pool, f->evr), id2str(pool, f->arch));
2327 printf(">!> install %s-%s%s", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2328 if (solv->rc_output != 2)
2329 printf(".%s", id2str(pool, s->arch));
2334 if (solv->rc_output == 2)
2335 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));
2337 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));
2341 if (solv->rc_output)
2343 Source *source = pool_source(pool, s);
2344 if (source && strcmp(source_name(source), "locales"))
2345 printf("[%s]", source_name(source));
2350 if (solv->rc_output)
2351 printf(">!> installs=%d, upgrades=%d, uninstalls=%d\n", installs, upgrades, uninstalls);
2353 xfree(obsoletesmap);
2357 /*-----------------------------------------------------------------*/
2367 solve(Solver *solv, Queue *job)
2369 Pool *pool = solv->pool;
2371 Map addedmap; /* '1' == have rule for solvable */
2372 Map noupdaterule; /* '1' == don't update (scheduled for removal) */
2373 Id how, what, p, *pp, d;
2379 * create basic rule set of all involved packages
2384 mapinit(&addedmap, pool->nsolvables);
2385 mapinit(&noupdaterule, pool->nsolvables);
2390 * create rules for installed solvables -> keep them installed
2391 * so called: rpm rules
2395 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2396 addrulesforsolvable(solv, pool->solvables + i, &addedmap);
2399 * create install rules
2401 * two passes, as we want to keep the rpm rules distinct from the job rules
2407 * process job rules for solvables
2410 for (i = 0; i < job->count; i += 2)
2412 how = job->elements[i];
2413 what = job->elements[i + 1];
2417 case SOLVER_INSTALL_SOLVABLE:
2418 addrulesforsolvable(solv, pool->solvables + what, &addedmap);
2420 case SOLVER_INSTALL_SOLVABLE_NAME:
2421 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
2423 FOR_PROVIDES(p, pp, what)
2425 /* if by name, ensure that the name matches */
2426 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
2428 addrulesforsolvable(solv, pool->solvables + p, &addedmap);
2431 case SOLVER_INSTALL_SOLVABLE_UPDATE:
2432 /* dont allow downgrade */
2433 addupdaterule(solv, pool->solvables + what, &addedmap, 0, 0, 1);
2439 * if unstalls are disallowed, add update rules for every
2440 * installed solvables in the hope to circumvent uninstall
2446 if (!solv->allowuninstall)
2448 /* add update rule for every installed package */
2449 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2450 addupdaterule(solv, pool->solvables + i, &addedmap, solv->allowdowngrade, solv->allowarchchange, 1);
2452 #else /* this is just to add the needed rpm rules to our set */
2453 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2454 addupdaterule(solv, pool->solvables + i, &addedmap, 1, 1, 1);
2457 addrulesforsupplements(solv, &addedmap);
2462 * unify existing rules before going over all job rules
2466 unifyrules(solv); /* remove duplicate rpm rules */
2469 * at this point the system is always solvable,
2470 * as an empty system (remove all packages) is a valid solution
2472 if (pool->verbose) printf("decisions based on rpms: %d\n", solv->decisionq.count);
2475 * now add all job rules
2478 solv->jobrules = solv->nrules;
2480 for (i = 0; i < job->count; i += 2)
2482 how = job->elements[i];
2483 what = job->elements[i + 1];
2486 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
2487 if (solv->rc_output) {
2488 Solvable *s = pool->solvables + what;
2489 printf(">!> Installing %s from channel %s\n", id2str(pool, s->name), source_name(pool_source(pool, s)));
2491 addrule(solv, what, 0); /* install by Id */
2493 case SOLVER_ERASE_SOLVABLE:
2494 addrule(solv, -what, 0); /* remove by Id */
2495 MAPSET(&noupdaterule, what);
2497 case SOLVER_INSTALL_SOLVABLE_NAME: /* install by capability */
2498 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
2500 FOR_PROVIDES(p, pp, what) /* check all providers */
2502 /* if by name, ensure that the name matches */
2503 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
2507 if (!q.count) { /* no provider found -> abort */
2508 fprintf(stderr, "Nothing provides '%s'\n", id2str(pool, what));
2509 /* XXX make this a problem! */
2514 p = queueshift(&q); /* get first provider */
2516 d = 0; /* single provider ? -> make assertion */
2518 d = pool_queuetowhatprovides(pool, &q); /* get all providers */
2519 addrule(solv, p, d); /* add 'requires' rule */
2521 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
2522 case SOLVER_ERASE_SOLVABLE_PROVIDES:
2523 FOR_PROVIDES(p, pp, what)
2525 /* if by name, ensure that the name matches */
2526 if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
2529 addrule(solv, -p, 0); /* add 'remove' rule */
2530 MAPSET(&noupdaterule, p);
2533 case SOLVER_INSTALL_SOLVABLE_UPDATE: /* find update for solvable */
2534 addupdaterule(solv, pool->solvables + what, &addedmap, 0, 0, 0);
2539 if (pool->verbose) printf("problems so far: %d\n", solv->problems.count);
2542 * now add policy rules
2546 solv->systemrules = solv->nrules;
2549 * create rules for updating installed solvables
2555 if (!solv->allowuninstall)
2556 { /* loop over all installed solvables */
2557 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2559 if (!MAPTST(&noupdaterule, i)) /* if not marked as 'noupdate' */
2560 addupdaterule(solv, pool->solvables + i, &addedmap, solv->allowdowngrade, solv->allowarchchange, 0);
2562 addrule(solv, 0, 0); /* place holder */
2564 /* consistency check: we added a rule for _every_ system solvable */
2565 if (solv->nrules - solv->systemrules != solv->system->nsolvables)
2569 if (pool->verbose) printf("problems so far: %d\n", solv->problems.count);
2571 /* create special weak system rules */
2572 if (solv->system->nsolvables)
2574 solv->weaksystemrules = xcalloc(solv->system->nsolvables, sizeof(Id));
2575 for (i = 0; i < solv->system->nsolvables; i++)
2577 findupdatepackages(solv, pool->solvables + solv->system->start + i, &q, (Map *)0, 1, 1);
2579 solv->weaksystemrules[i] = pool_queuetowhatprovides(pool, &q);
2583 /* free unneeded memory */
2585 mapfree(&noupdaterule);
2593 run_solver(solv, 1, 1);
2597 * print solver result
2601 if (pool->verbose) printf("-------------------------------------------------------------\n");
2603 if (solv->problems.count)
2613 clonequeue(&problems, &solv->problems);
2614 queueinit(&solution);
2615 printf("Encountered problems! Here are the solutions:\n");
2616 problem = problems.elements;
2617 for (i = 0; i < problems.count; i++)
2619 Id v = problems.elements[i];
2622 printf("====================================\n");
2623 problem = problems.elements + i + 1;
2626 refine_suggestion(solv, problem, v, &solution);
2627 for (j = 0; j < solution.count; j++)
2629 r = solv->rules + solution.elements[j];
2630 why = solution.elements[j];
2634 if (why >= solv->jobrules && why < solv->systemrules)
2636 /* do a sloppy job of analyzing the job rule */
2639 s = pool->solvables + r->p;
2640 printf("- do not install %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2644 s = pool->solvables - r->p;
2645 printf("- do not erase %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2648 else if (why >= solv->systemrules && why < solv->learntrules)
2651 s = pool->solvables + solv->system->start + (why - solv->systemrules);
2652 if (solv->weaksystemrules && solv->weaksystemrules[why - solv->systemrules])
2654 Id *dp = pool->whatprovidesdata + solv->weaksystemrules[why - solv->systemrules];
2656 if (solv->decisionmap[*dp] > 0)
2658 sd = pool->solvables + *dp;
2664 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));
2668 printf("- allow deinstallation of %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2676 printf("------------------------------------\n");
2681 printdecisions(solv);