4 * SAT based dependency solver
18 #define RULES_BLOCK 63
20 static Pool *prune_best_version_arch_sortcmp_data;
22 /*-----------------------------------------------------------------*/
25 * prep for prune_best_version_arch
30 prune_best_version_arch_sortcmp(const void *ap, const void *bp)
32 Pool *pool = prune_best_version_arch_sortcmp_data;
35 return pool->solvables[a].name - pool->solvables[b].name;
41 replaces_system(Solver *solv, Id id)
43 Pool *pool = solv->pool;
44 Source *system = solv->system;
45 Id *name = pool->solvables[id].name;
47 FOR_PROVIDES(p, pp, id)
49 s = pool->solvables + p;
52 if (p >= system->start && p < system->start + system->nsolvables)
59 dep_installed(Solver *solv, Id dep)
61 Pool *pool = solv->pool;
66 Reldep *rd = GETRELDEP(pool, dep);
67 if (rd->flags == REL_AND)
69 if (!dep_installed(solv, rd->name))
71 return dep_installed(solv, rd->evr);
73 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
74 return dep_installed(solv, rd->evr);
76 FOR_PROVIDES(p, pp, dep)
78 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
85 dep_fulfilled(Solver *solv, Id dep)
87 Pool *pool = solv->pool;
92 Reldep *rd = GETRELDEP(pool, dep);
93 if (rd->flags == REL_AND)
95 if (!dep_fulfilled(solv, rd->name))
97 return dep_fulfilled(solv, rd->evr);
99 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
100 return dep_installed(solv, rd->evr);
102 FOR_PROVIDES(p, pp, dep)
104 if (solv->decisionmap[p] > 0)
111 dep_possible(Solver *solv, Id dep, Map *m)
113 Pool *pool = solv->pool;
118 Reldep *rd = GETRELDEP(pool, dep);
119 if (rd->flags == REL_AND)
121 if (!dep_possible(solv, rd->name, m))
123 return dep_possible(solv, rd->evr, m);
125 if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
126 return dep_installed(solv, rd->evr);
128 FOR_PROVIDES(p, pp, dep)
137 * prune_to_recommended
139 * XXX: should we prune to requires/suggests that are already
140 * fulfilled by other packages?
143 prune_to_recommended(Solver *solv, Queue *plist)
145 Pool *pool = solv->pool;
148 Id p, *pp, sup, *supp, rec, *recp, sug, *sugp, enh, *enhp;
150 if (solv->recommends_index < 0)
152 MAPZERO(&solv->recommendsmap);
153 MAPZERO(&solv->suggestsmap);
154 solv->recommends_index = 0;
156 while (solv->recommends_index < solv->decisionq.count)
158 p = solv->decisionq.elements[solv->recommends_index++];
161 s = pool->solvables + p;
162 if ((recp = s->recommends) != 0)
163 while ((rec = *recp++) != 0)
164 FOR_PROVIDES(p, pp, rec)
165 MAPSET(&solv->recommendsmap, p);
166 if ((sugp = s->suggests) != 0)
167 while ((sug = *sugp++) != 0)
168 FOR_PROVIDES(p, pp, sug)
169 MAPSET(&solv->suggestsmap, p);
171 /* prune to recommended/supplemented */
172 for (i = j = 0; i < plist->count; i++)
174 p = plist->elements[i];
175 if (MAPTST(&solv->recommendsmap, p))
177 plist->elements[j++] = p;
180 s = pool->solvables + p;
181 if (!s->supplements && !s->freshens)
183 if ((supp = s->supplements) != 0)
185 while ((sup = *supp++) != 0)
186 if (dep_fulfilled(solv, sup))
191 if ((supp = s->freshens) != 0)
193 while ((sup = *supp++) != 0)
194 if (dep_fulfilled(solv, sup))
199 plist->elements[j++] = s - pool->solvables;
204 /* prune to suggested/enhanced*/
205 if (plist->count < 2)
207 for (i = j = 0; i < plist->count; i++)
209 p = plist->elements[i];
210 if (MAPTST(&solv->suggestsmap, p))
212 plist->elements[j++] = p;
215 s = pool->solvables + p;
216 if (!(enhp = s->enhances))
218 while ((enh = *enhp++) != 0)
219 if (dep_fulfilled(solv, enh))
223 plist->elements[j++] = s - pool->solvables;
230 * prune_best_version_arch
232 * sort list of packages (given through plist) by name and evr
233 * return result through plist
237 /* FIXME: must also look at update packages */
240 prune_best_version_arch(Pool *pool, Queue *plist)
247 if (plist->count < 2) /* no need to prune for a single entry */
249 if (pool->verbose) printf("prune_best_version_arch %d\n", plist->count);
251 /* prune to best architecture */
255 for (i = 0; i < plist->count; i++)
257 s = pool->solvables + plist->elements[i];
259 if (a > pool->lastarch)
261 a = pool->id2arch[a];
262 if (!bestscore || (a & 0xffff0000) < bestscore)
263 bestscore = a & 0xffff0000;
265 for (i = j = 0; i < plist->count; i++)
267 s = pool->solvables + plist->elements[i];
269 if (a > pool->lastarch)
271 a = pool->id2arch[a];
272 /* a == 1 -> noarch */
273 if (a != 1 && (a & 0xffff0000) != bestscore)
275 plist->elements[j++] = plist->elements[i];
282 prune_best_version_arch_sortcmp_data = pool;
283 /* sort by name first */
284 qsort(plist->elements, plist->count, sizeof(Id), prune_best_version_arch_sortcmp);
286 /* now find best 'per name' */
287 for (i = j = 0; i < plist->count; i++)
289 s = pool->solvables + plist->elements[i];
291 if (pool->verbose) printf("- %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
293 if (!best) /* if no best yet, the current is best */
295 best = plist->elements[i];
299 /* name switch: re-init */
300 if (pool->solvables[best].name != s->name) /* new name */
302 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));
303 plist->elements[j++] = best; /* move old best to front */
304 best = plist->elements[i]; /* take current as new best */
308 if (pool->solvables[best].evr != s->evr) /* compare evr */
310 if (evrcmp(pool, pool->solvables[best].evr, s->evr) < 0)
311 best = plist->elements[i];
316 best = plist->elements[0];
318 /* XXX also check obsoletes! */
319 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));
321 plist->elements[j++] = best;
326 /*-----------------------------------------------------------------*/
333 printruleelement(Solver *solv, Rule *r, Id v)
335 Pool *pool = solv->pool;
339 s = pool->solvables + -v;
340 printf(" !%s-%s.%s [%d]", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), -v);
344 s = pool->solvables + v;
345 printf(" %s-%s.%s [%d]", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), v);
354 if (solv->decisionmap[s - pool->solvables] > 0)
355 printf(" I.%d", solv->decisionmap[s - pool->solvables]);
356 if (solv->decisionmap[s - pool->solvables] < 0)
357 printf(" C.%d", -solv->decisionmap[s - pool->solvables]);
367 printrule(Solver *solv, Rule *r)
372 if (r >= solv->rules && r < solv->rules + solv->nrules) /* r is a solver rule */
373 printf("Rule #%d:\n", (int)(r - solv->rules));
375 printf("Rule:\n"); /* r is any rule */
380 else if (r->d == ID_NULL)
387 v = solv->pool->whatprovidesdata[r->d + i - 1];
390 printruleelement(solv, r, v);
392 printf(" next: %d %d\n", r->n1, r->n2);
396 /*-----------------------------------------------------------------*/
402 static Pool *unifyrules_sortcmp_data;
405 * compare rules for unification sort
409 unifyrules_sortcmp(const void *ap, const void *bp)
411 Pool *pool = unifyrules_sortcmp_data;
412 Rule *a = (Rule *)ap;
413 Rule *b = (Rule *)bp;
419 return x; /* p differs */
422 if (a->d == 0 && b->d == 0)
423 return a->w2 - b->w2; /* assertion: return w2 diff */
425 if (a->d == 0) /* a is assertion, b not */
427 x = a->w2 - pool->whatprovidesdata[b->d];
431 if (b->d == 0) /* b is assertion, a not */
433 x = pool->whatprovidesdata[a->d] - b->w2;
437 /* compare whatprovidesdata */
438 ad = pool->whatprovidesdata + a->d;
439 bd = pool->whatprovidesdata + b->d;
441 if ((x = *ad++ - *bd++) != 0)
452 unifyrules(Solver *solv)
457 if (solv->nrules <= 1) /* nothing to unify */
460 /* sort rules first */
461 unifyrules_sortcmp_data = solv->pool;
462 qsort(solv->rules + 1, solv->nrules - 1, sizeof(Rule), unifyrules_sortcmp);
469 for (i = j = 1, ir = solv->rules + 1; i < solv->nrules; i++, ir++)
471 if (jr && !unifyrules_sortcmp(ir, jr))
472 continue; /* prune! */
473 jr = solv->rules + j++; /* keep! */
478 /* reduced count from nrules to j rules */
479 if (solv->pool->verbose) printf("pruned rules from %d to %d\n", solv->nrules, j);
481 /* adapt rule buffer */
482 solv->rules = (Rule *)xrealloc(solv->rules, ((solv->nrules + RULES_BLOCK) & ~RULES_BLOCK) * sizeof(Rule));
491 for (i = 1; i < solv->nrules; i++)
494 if (r->d == 0) /* assertion */
499 dp = solv->pool->whatprovidesdata + r->d;
503 if (solv->pool->verbose)
505 printf(" binary: %d\n", binr);
506 printf(" normal: %d\n", solv->nrules - 1 - binr);
507 printf(" normal lits: %d\n", dc);
520 hashrule(Solver *solv, Id p, Id d, int n)
522 unsigned int x = (unsigned int)p;
526 return (x * 37) ^ (unsigned int)d;
527 dp = solv->pool->whatprovidesdata + d;
529 x = (x * 37) ^ (unsigned int)*dp++;
537 * p = direct literal; > 0 for learnt, < 0 for installed pkg (rpm)
538 * d, if < 0 direct literal, if > 0 offset into whatprovides, if == 0 rule is assertion (look at p only)
541 * A requires b, b provided by B1,B2,B3 => (-A|B1|B2|B3)
543 * p < 0 : rule from rpm (installed pkg)
544 * d > 0 : Offset in whatprovidesdata (list of providers)
546 * A conflicts b, b provided by B1,B2,B3 => (-A|-B1), (-A|-B2), (-A|-B3)
547 * d < 0: Id of solvable (e.g. B1)
549 * d == 0: unary rule, assertion => (A) or (-A)
551 * Install: p > 0, d = 0 (A) user requested install
552 * Remove: p < 0, d = 0 (-A) user requested remove
553 * Requires: p < 0, d > 0 (-A|B1|B2|...) d: <list of providers for requirement of p>
554 * Updates: p > 0, d > 0 (A|B1|B2|...) d: <list of updates for solvable p>
555 * Conflicts: p < 0, d < 0 (-A|-B) either p (conflict issuer) or d (conflict provider)
556 * ? p > 0, d < 0 (A|-B)
557 * No-op ?: p = 0, d = 0 (null) (used as policy rule placeholder)
561 addrule(Solver *solv, Id p, Id d)
566 int n = 0; /* number of literals in rule - 1
567 0 = direct assertion (single literal)
571 /* it often happenes that requires lead to adding the same rpm rule
572 * multiple times, so we prune those duplicates right away to make
573 * the work for unifyrules a bit easier */
575 if (solv->nrules && !solv->jobrules)
577 r = solv->rules + solv->nrules - 1; /* get the last added rule */
578 if (r->p == p && r->d == d && d != 0) /* identical and not user requested */
585 return NULL; /* ignore self conflict */
588 else if (d == 0) /* user requested */
592 for (dp = solv->pool->whatprovidesdata + d; *dp; dp++, n++)
594 return NULL; /* rule is self-fulfilling */
599 if (n == 0) /* direct assertion */
603 /* this is a rpm rule assertion, we do not have to allocate it */
604 /* we can identify it by looking at the decision level, it will be 1 */
607 if (solv->decisionmap[-p] > 0) /* */
609 if (solv->decisionmap[-p]) /* */
611 queuepush(&solv->decisionq, p);
612 queuepush(&solv->decisionq_why, 0);
613 solv->decisionmap[-p] = -1;
618 else if (n == 1 && p > d)
620 /* smallest literal first so we can find dups */
624 n = 1; /* re-set n, was used as temp var */
640 Id *dp2 = solv->pool->whatprovidesdata + r->d;
641 for (dp = solv->pool->whatprovidesdata + d; *dp; dp++, dp2++)
654 /* check and extend rule buffer */
655 if ((solv->nrules & RULES_BLOCK) == 0)
657 solv->rules = (Rule *)xrealloc(solv->rules, (solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
660 r = solv->rules + solv->nrules++; /* point to rule space */
665 /* direct assertion, no watch needed */
681 r->w2 = solv->pool->whatprovidesdata[d];
686 /* we don't add the decision for learnt rules, as the code does that
687 * right after calling addrule anyway */
690 && !solv->learntrules)
692 /* must be system or job rule, as there are only negative unary rpm rules */
693 Id vv = p > 0 ? p : -p;
694 if (solv->decisionmap[vv])
697 if (solv->decisionmap[vv] > 0 && p > 0)
699 if (solv->decisionmap[vv] < 0 && p < 0)
701 /* direct conflict! */
702 for (i = 0; i < solv->decisionq.count; i++)
703 if (solv->decisionq.elements[i] == -p)
705 if (i == solv->decisionq.count)
707 if (solv->decisionq_why.elements[i] == 0)
709 /* conflict with rpm rule */
710 queuepush(&solv->problems, r - solv->rules);
711 queuepush(&solv->problems, 0);
712 r->w1 = 0; /* disable */
715 /* conflict with other job or system rule */
716 queuepush(&solv->problems, solv->decisionq_why.elements[i]);
717 queuepush(&solv->problems, r - solv->rules);
718 queuepush(&solv->problems, 0);
719 r->w1 = 0; /* disable */
720 /* also disable conflicting rule */
721 solv->rules[solv->decisionq_why.elements[i]].w1 = 0;
722 /* XXX: remove from decisionq! */
723 printf("XXX remove from decisionq\n");
726 queuepush(&solv->decisionq, p);
727 queuepush(&solv->decisionq_why, r - solv->rules);
728 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? 1 : -1;
735 * add (install) rules for solvable
740 addrulesforsolvable(Solver *solv, Solvable *s, Map *m)
742 Pool *pool = solv->pool;
743 Source *system = solv->system;
756 queueinit_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
757 queuepush(&q, s - pool->solvables); /* push solvable Id */
763 * s: Pointer to solvable
767 if (MAPTST(m, n)) /* continue if already set in map */
771 s = pool->solvables + n; /* s = Solvable in question */
774 if (system /* have rpm */
776 && n >= system->start /* its an rpm rule */
777 && n < system->start + system->nsolvables)
779 dontfix = 1; /* dont care about broken rpm deps */
782 /*-----------------------------------------
783 * check requires of s
786 if ((reqp = s->requires) != ID_NULL)
788 while ((req = *reqp++) != ID_NULL)
790 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
793 dp = GET_PROVIDESP(req, p); /* get providers of req */
795 if (!*dp /* dont care if noone provides rpmlib() */
796 && !strncmp(id2str(pool, req), "rpmlib(", 7))
801 if (dontfix) /* rpm rule, dont care about breakage */
803 for (i = 0; dp[i]; i++)/* for all providers */
805 if (dp[i] >= system->start && dp[i] < system->start + system->nsolvables)
806 break; /* provider is installed */
808 if (!dp[i]) /* no provider found */
810 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));
817 /* nothing provides req! */
819 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));
821 addrule(solv, -n, 0); /* mark requestor as uninstallable */
823 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)));
827 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);
828 for (i = 0; dp[i]; i++)
829 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));
831 /* add 'requires' dependency */
832 addrule(solv, -n, dp - pool->whatprovidesdata); /* rule: (-requestor|provider1|provider2|...|providerN) */
834 /* descend the dependency tree */
835 for (; *dp != ID_NULL; dp++) /* loop through all providers */
841 } /* while, requirements of n */
843 } /* if, requirements */
846 /*-----------------------------------------
847 * check conflicts of s
850 if ((conp = s->conflicts) != ID_NULL)
852 while ((con = *conp++) != ID_NULL)
854 FOR_PROVIDES(p, pp, con) /* loop through all providers of this conflict */
856 /* dontfix: dont care about conflicts with already installed packs */
857 if (dontfix && p >= system->start && p < system->start + system->nsolvables)
859 addrule(solv, -n, -p); /* rule: -n|-p: either solvable _or_ provider of conflict */
864 /*-----------------------------------------
865 * check obsoletes if not installed
867 if (!system || n < system->start || n >= (system->start + system->nsolvables))
868 { /* not installed */
869 if ((obsp = s->obsoletes) != ID_NULL)
871 while ((obs = *obsp++) != ID_NULL)
873 FOR_PROVIDES(p, pp, obs)
874 addrule(solv, -n, -p);
877 FOR_PROVIDES(p, pp, s->name)
879 if (s->name == pool->solvables[p].name)
880 addrule(solv, -n, -p);
884 /*-----------------------------------------
885 * add recommends to the rule list
887 if ((recp = s->recommends) != ID_NULL)
888 while ((rec = *recp++) != ID_NULL)
890 FOR_PROVIDES(p, pp, rec)
899 addrulesforsupplements(Solver *solv, Map *m)
901 Pool *pool = solv->pool;
906 if (pool->verbose) printf("addrulesforsupplements... (%d)\n", solv->nrules);
907 for (i = n = 1; n < pool->nsolvables; i++, n++)
909 if (i == pool->nsolvables)
913 s = pool->solvables + i;
914 if (!pool_installable(pool, s))
917 if ((supp = s->supplements) != 0)
918 while ((sup = *supp++) != ID_NULL)
919 if (dep_possible(solv, sup, m))
921 if (!sup && (supp = s->freshens) != 0)
922 while ((sup = *supp++) != ID_NULL)
923 if (dep_possible(solv, sup, m))
927 addrulesforsolvable(solv, s, m);
930 if (pool->verbose) printf("done. (%d)\n", solv->nrules);
934 addrulesforenhances(Solver *solv, Map *m)
936 Pool *pool = solv->pool;
939 Id p, *pp, enh, *enhp, obs, *obsp, con, *conp;
941 if (pool->verbose) printf("addrulesforenhances... (%d)\n", solv->nrules);
942 for (i = 1; i < pool->nsolvables; i++)
946 s = pool->solvables + i;
947 if ((enhp = s->enhances) == 0)
949 if (!pool_installable(pool, s))
951 while ((enh = *enhp++) != ID_NULL)
952 if (dep_possible(solv, enh, m))
956 if ((conp = s->conflicts) != 0)
957 while ((con = *conp++) != 0)
958 FOR_PROVIDES(p, pp, con)
959 addrule(solv, -i, -p);
960 if ((obsp = s->obsoletes) != 0)
961 while ((obs = *obsp++) != ID_NULL)
962 FOR_PROVIDES(p, pp, obs)
963 addrule(solv, -i, -p);
964 FOR_PROVIDES(p, pp, s->name)
965 if (s->name == pool->solvables[p].name)
966 addrule(solv, -i, -p);
972 archchanges(Pool *pool, Solvable *s1, Solvable *s2)
974 Id a1 = s1->arch, a2 = s2->arch;
976 /* we allow changes to/from noarch */
977 if (a1 == a2 || a1 == ARCH_NOARCH || a2 == ARCH_NOARCH)
981 a1 = a1 <= pool->lastarch ? pool->id2arch[a1] : 0;
982 a2 = a2 <= pool->lastarch ? pool->id2arch[a2] : 0;
983 if (((a1 ^ a2) & 0xffff0000) != 0)
990 findupdatepackages(Solver *solv, Solvable *s, Queue *qs, Map *m, int allowdowngrade, int allowarchchange)
992 /* system packages get a special upgrade allowed rule */
993 Pool *pool = solv->pool;
994 Id p, *pp, n, p2, *pp2;
1002 n = s - pool->solvables;
1003 if (m && !MAPTST(m, n)) /* add rule for s if not already done */
1004 addrulesforsolvable(solv, s, m);
1007 * look for updates for s
1009 FOR_PROVIDES(p, pp, s->name) /* every provider of s' name */
1011 if (p == n) /* skip itself */
1014 if (s->name == pool->solvables[p].name) /* name match */
1016 if (!allowdowngrade /* consider downgrades ? */
1017 && evrcmp(pool, s->evr, pool->solvables[p].evr) > 0)
1020 if (!allowarchchange && archchanges(pool, s, pool->solvables + p))
1023 else if (!solv->noupdateprovide && (obsp = pool->solvables[p].obsoletes) != 0) /* provides/obsoletes combination ? */
1025 while ((obs = *obsp++) != 0) /* for all obsoletes */
1027 FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
1029 if (p2 == n) /* match ! */
1032 if (p2) /* match! */
1035 if (!obs) /* continue if no match */
1037 /* here we have 'p' with a matching provides/obsoletes combination
1038 * thus flagging p as a valid update candidate for s
1045 if (m && !MAPTST(m, p)) /* mark p for install if not already done */
1046 addrulesforsolvable(solv, pool->solvables + p, m);
1048 if (solv->noupdateprovide && solv->obsoletes && solv->obsoletes[n - solv->system->start])
1050 for (pp = solv->obsoletes_data + solv->obsoletes[n - solv->system->start]; (p = *pp++) != 0;)
1053 if (m && !MAPTST(m, p)) /* mark p for install if not already done */
1054 addrulesforsolvable(solv, pool->solvables + p, m);
1060 * add rule for update
1061 * (A|A1|A2|A3...) An = update candidates for A
1063 * s = (installed) solvable
1064 * m = 'addedmap', bit set if 'install' rule for solvable exists
1068 addupdaterule(Solver *solv, Solvable *s, Map *m, int allowdowngrade, int allowarchchange, int dontaddrule)
1070 /* system packages get a special upgrade allowed rule */
1071 Pool *pool = solv->pool;
1077 queueinit_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1078 findupdatepackages(solv, s, &qs, m, allowdowngrade, allowarchchange);
1079 p = s - pool->solvables;
1080 if (dontaddrule) /* we consider update candidates but dont force them */
1086 if (qs.count == 0) /* no updates found */
1089 printf("new update rule: must keep %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1091 addrule(solv, p, 0); /* request 'install' of s */
1096 d = pool_queuetowhatprovides(pool, &qs); /* intern computed provider queue */
1098 r = addrule(solv, p, d); /* allow update of s */
1100 printf("new update rule ");
1107 /*-----------------------------------------------------------------*/
1114 * initial setup for all watches
1118 makewatches(Solver *solv)
1122 int nsolvables = solv->pool->nsolvables;
1124 xfree(solv->watches);
1125 /* lower half for removals, upper half for installs */
1126 solv->watches = (Id *)xcalloc(2 * nsolvables, sizeof(Id));
1128 /* do it reverse so rpm rules get triggered first */
1129 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
1131 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
1134 if (!r->w1 /* rule is disabled */
1135 || !r->w2) /* rule is assertion */
1138 /* see addwatches(solv, r) */
1139 r->n1 = solv->watches[nsolvables + r->w1];
1140 solv->watches[nsolvables + r->w1] = r - solv->rules;
1142 r->n2 = solv->watches[nsolvables + r->w2];
1143 solv->watches[nsolvables + r->w2] = r - solv->rules;
1149 * add watches (for rule)
1153 addwatches(Solver *solv, Rule *r)
1155 int nsolvables = solv->pool->nsolvables;
1157 r->n1 = solv->watches[nsolvables + r->w1];
1158 solv->watches[nsolvables + r->w1] = r - solv->rules;
1160 r->n2 = solv->watches[nsolvables + r->w2];
1161 solv->watches[nsolvables + r->w2] = r - solv->rules;
1165 /*-----------------------------------------------------------------*/
1166 /* rule propagation */
1168 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
1173 * propagate decision to all rules
1177 propagate(Solver *solv, int level)
1179 Pool *pool = solv->pool;
1184 Id *decisionmap = solv->decisionmap;
1185 Id *watches = solv->watches + pool->nsolvables;
1187 while (solv->propagate_index < solv->decisionq.count)
1189 /* negative because our watches trigger if literal goes FALSE */
1190 pkg = -solv->decisionq.elements[solv->propagate_index++];
1192 printf("popagate for decision %d level %d\n", -pkg, level);
1193 printruleelement(solv, 0, -pkg);
1195 for (rp = watches + pkg; *rp; rp = nrp)
1197 r = solv->rules + *rp;
1199 printf(" watch triggered ");
1212 /* if clause is TRUE, nothing to do */
1213 if (DECISIONMAP_TRUE(ow))
1218 /* not a binary clause, check if we need to move our watch */
1219 if (r->p && r->p != ow && !DECISIONMAP_TRUE(-r->p))
1222 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
1223 if (p != ow && !DECISIONMAP_TRUE(-p))
1227 /* p is free to watch, move watch to p */
1230 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));
1232 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));
1246 watches[p] = r - solv->rules;
1250 /* unit clause found, set other watch to TRUE */
1251 if (DECISIONMAP_TRUE(-ow))
1252 return r; /* eek, a conflict! */
1258 decisionmap[ow] = level;
1260 decisionmap[-ow] = -level;
1261 queuepush(&solv->decisionq, ow);
1262 queuepush(&solv->decisionq_why, r - solv->rules);
1265 Solvable *s = pool->solvables + (ow > 0 ? ow : -ow);
1267 printf(" -> decided to install %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1269 printf(" -> decided to conflict %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1274 return 0; /* all is well */
1278 /*-----------------------------------------------------------------*/
1287 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *why)
1289 Pool *pool = solv->pool;
1292 Map seen; /* global? */
1296 int learnt_why = solv->learnt_pool.count;
1297 Id *decisionmap = solv->decisionmap;
1301 if (pool->verbose) printf("ANALYZE at %d ----------------------\n", level);
1302 mapinit(&seen, pool->nsolvables);
1303 idx = solv->decisionq.count;
1307 queuepush(&solv->learnt_pool, c - solv->rules);
1308 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1319 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1321 vv = v > 0 ? v : -v;
1322 if (MAPTST(&seen, vv))
1324 l = solv->decisionmap[vv];
1331 for (j = 0; j < solv->decisionq.count; j++)
1332 if (solv->decisionq.elements[j] == v)
1334 if (j == solv->decisionq.count)
1336 queuepush(&rulq, -(j + 1));
1338 continue; /* initial setting */
1342 num++; /* need to do this one as well */
1347 printf("PUSH %d ", v);
1348 printruleelement(solv, 0, v);
1355 printf("num = %d\n", num);
1361 v = solv->decisionq.elements[--idx];
1362 vv = v > 0 ? v : -v;
1363 if (MAPTST(&seen, vv))
1366 c = solv->rules + solv->decisionq_why.elements[idx];
1374 else if (r.count == 1 && r.elements[0] < 0)
1375 *dr = r.elements[0];
1377 *dr = pool_queuetowhatprovides(pool, &r);
1380 printf("learned rule for level %d (am %d)\n", rlevel, level);
1381 printruleelement(solv, 0, -v);
1382 for (i = 0; i < r.count; i++)
1385 printruleelement(solv, 0, v);
1389 queuepush(&solv->learnt_pool, 0);
1391 for (i = learnt_why; solv->learnt_pool.elements[i]; i++)
1393 printf("learnt_why ");
1394 printrule(solv, solv->rules + solv->learnt_pool.elements[i]);
1405 * reset the solver decisions to right after the rpm rules
1409 reset_solver(Solver *solv)
1415 /* delete all learnt rules */
1416 solv->nrules = solv->learntrules;
1417 QUEUEEMPTY(&solv->learnt_why);
1418 QUEUEEMPTY(&solv->learnt_pool);
1420 /* redo all direct decision without the disabled rules */
1421 for (i = 0; i < solv->decisionq.count; i++)
1423 v = solv->decisionq.elements[i];
1424 solv->decisionmap[v > 0 ? v : -v] = 0;
1426 for (i = 0; i < solv->decisionq_why.count; i++)
1427 if (solv->decisionq_why.elements[i])
1431 v = solv->decisionq.elements[i];
1432 solv->decisionmap[v > 0 ? v : -v] = v > 0 ? 1 : -1;
1435 if (solv->pool->verbose)
1436 printf("decisions done reduced from %d to %d\n", solv->decisionq.count, i);
1438 solv->decisionq_why.count = i;
1439 solv->decisionq.count = i;
1440 solv->recommends_index = -1;
1441 if (i < solv->propagate_index)
1442 solv->propagate_index = i;
1443 /* make direct decisions from enabled unary rules */
1444 for (i = solv->jobrules, r = solv->rules + solv->jobrules; i < solv->nrules; i++, r++)
1446 if (!r->w1 || r->w2)
1452 queuepush(&solv->decisionq, v);
1453 queuepush(&solv->decisionq_why, r - solv->rules);
1454 solv->decisionmap[v > 0 ? v : -v] = v > 0 ? 1 : -1;
1456 if (solv->pool->verbose)
1457 printf("decisions after adding job and system rules: %d\n", solv->decisionq.count);
1458 /* recreate watches */
1464 * analyze_unsolvable_rule
1468 analyze_unsolvable_rule(Solver *solv, Rule *c, int disablerules)
1473 why = c - solv->rules;
1475 if (why >= solv->jobrules && why < solv->systemrules)
1477 if (why >= solv->systemrules && why < solv->learntrules)
1478 printf("SYSTEM %d ", why - solv->systemrules);
1479 if (solv->learntrules && why >= solv->learntrules)
1483 if (solv->learntrules && why >= solv->learntrules)
1485 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1486 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], disablerules);
1489 if (why >= solv->jobrules && why < solv->learntrules)
1493 /* turn off rule for further analysis */
1497 if (solv->problems.count)
1499 for (i = solv->problems.count - 1; i >= 0; i--)
1500 if (solv->problems.elements[i] == 0)
1502 else if (solv->problems.elements[i] == why)
1505 queuepush(&solv->problems, why);
1511 * analyze_unsolvable
1515 analyze_unsolvable(Solver *solv, Rule *c, int disablerules)
1517 Pool *pool = solv->pool;
1518 Map seen; /* global? */
1521 Id *decisionmap = solv->decisionmap;
1524 printf("ANALYZE UNSOLVABLE ----------------------\n");
1526 mapinit(&seen, pool->nsolvables);
1527 analyze_unsolvable_rule(solv, c, disablerules);
1528 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1539 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1541 vv = v > 0 ? v : -v;
1542 l = solv->decisionmap[vv];
1547 idx = solv->decisionq.count;
1550 v = solv->decisionq.elements[--idx];
1551 vv = v > 0 ? v : -v;
1552 if (!MAPTST(&seen, vv))
1554 why = solv->decisionq_why.elements[idx];
1559 printruleelement(solv, 0, v);
1563 c = solv->rules + why;
1564 analyze_unsolvable_rule(solv, c, disablerules);
1565 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1576 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1578 vv = v > 0 ? v : -v;
1579 l = solv->decisionmap[vv];
1586 queuepush(&solv->problems, 0); /* mark end of this problem */
1590 printf("analyze_unsolvables done\n");
1595 /*-----------------------------------------------------------------*/
1596 /* Decision revert */
1600 * revert decision at level
1604 revert(Solver *solv, int level)
1607 while (solv->decisionq.count)
1609 v = solv->decisionq.elements[solv->decisionq.count - 1];
1610 vv = v > 0 ? v : -v;
1611 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1614 printf("reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1616 solv->decisionmap[vv] = 0;
1617 solv->decisionq.count--;
1618 solv->decisionq_why.count--;
1619 solv->propagate_index = solv->decisionq.count;
1621 solv->recommends_index = -1;
1630 watch2onhighest(Solver *solv, Rule *r)
1636 return; /* binary rule, both watches are set */
1637 dp = solv->pool->whatprovidesdata + r->d;
1638 while ((v = *dp++) != 0)
1640 l = solv->decisionmap[v < 0 ? -v : v];
1657 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules)
1667 solv->decisionmap[decision] = level;
1669 solv->decisionmap[-decision] = -level;
1670 queuepush(&solv->decisionq, decision);
1671 queuepush(&solv->decisionq_why, 0);
1675 r = propagate(solv, level);
1680 analyze_unsolvable(solv, r, disablerules);
1685 printf("conflict with rule #%d\n", (int)(r - solv->rules));
1686 l = analyze(solv, level, r, &p, &d, &why);
1687 if (l >= level || l <= 0)
1689 printf("reverting decisions (level %d -> %d)\n", level, l);
1691 revert(solv, level);
1692 r = addrule(solv, p, d); /* p requires d */
1695 if (solv->learnt_why.count != (r - solv->rules) - solv->learntrules)
1697 printf("%d %d\n", solv->learnt_why.count, (int)(r - solv->rules) - solv->learntrules);
1700 queuepush(&solv->learnt_why, why);
1703 /* at least 2 literals, needs watches */
1704 watch2onhighest(solv, r);
1705 addwatches(solv, r);
1707 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1708 queuepush(&solv->decisionq, p);
1709 queuepush(&solv->decisionq_why, r - solv->rules);
1710 printf("decision: ");
1711 printruleelement(solv, 0, p);
1712 printf("new rule: ");
1718 /*-----------------------------------------------------------------*/
1719 /* Main solver interface */
1724 * create solver structure
1726 * pool: all available solvables
1727 * system: installed Solvables
1730 * Upon solving, rules are created to flag the Solvables
1731 * of the 'system' Source as installed.
1735 solver_create(Pool *pool, Source *system)
1738 solv = (Solver *)xcalloc(1, sizeof(Solver));
1740 solv->system = system;
1743 queueinit(&solv->decisionq);
1744 queueinit(&solv->decisionq_why);
1745 queueinit(&solv->problems);
1746 queueinit(&solv->suggestions);
1747 queueinit(&solv->learnt_why);
1748 queueinit(&solv->learnt_pool);
1750 mapinit(&solv->recommendsmap, pool->nsolvables);
1751 mapinit(&solv->suggestsmap, pool->nsolvables);
1752 solv->recommends_index = 0;
1754 solv->decisionmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
1755 solv->rules = (Rule *)xmalloc((solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
1756 memset(solv->rules, 0, sizeof(Rule));
1768 solver_free(Solver *solv)
1770 queuefree(&solv->decisionq);
1771 queuefree(&solv->decisionq_why);
1772 queuefree(&solv->learnt_why);
1773 queuefree(&solv->learnt_pool);
1774 queuefree(&solv->problems);
1775 queuefree(&solv->suggestions);
1777 mapfree(&solv->recommendsmap);
1778 mapfree(&solv->suggestsmap);
1779 xfree(solv->decisionmap);
1781 xfree(solv->watches);
1782 xfree(solv->weaksystemrules);
1783 xfree(solv->obsoletes);
1784 xfree(solv->obsoletes_data);
1792 * r->w1 was set to 0, now find proper value for w1
1796 reenablerule(Solver *solv, Rule *r)
1801 if (!r->w2) /* not a rule, but an assertion */
1811 r->w2 = r->d; /* mls: shouldn't this be r->w1 ? */
1815 /* put it on the first not-false literal */
1821 v = solv->pool->whatprovidesdata[r->d + i];
1829 l = solv->decisionmap[v > 0 ? v : -v];
1830 if (!l || (v < 0 && l < 0) || (v > 0 && l > 0))
1837 /*-------------------------------------------------------*/
1842 * all rules have been set up, not actually run the solver
1847 run_solver(Solver *solv, int disablerules, int doweak)
1855 Pool *pool = solv->pool;
1859 printf("number of rules: %d\n", solv->nrules);
1862 for (i = 0; i < solv->nrules; i++)
1864 printrule(solv, solv->rules + i);
1869 /* all new rules are learnt after this point */
1870 solv->learntrules = solv->nrules;
1871 /* crate watches lists */
1874 if (pool->verbose) printf("initial decisions: %d\n", solv->decisionq.count);
1876 /* start SAT algorithm */
1878 systemlevel = level + 1;
1879 if (pool->verbose) printf("solving...\n");
1890 if (pool->verbose) printf("propagating (%d %d)...\n", solv->propagate_index, solv->decisionq.count);
1891 if ((r = propagate(solv, level)) != 0)
1893 analyze_unsolvable(solv, r, disablerules);
1896 printf("UNSOLVABLE\n");
1906 if (level < systemlevel && solv->system->nsolvables)
1908 if (!solv->updatesystem)
1910 /* try to keep as many packages as possible */
1911 if (pool->verbose) printf("installing system packages\n");
1912 for (i = solv->system->start, n = 0; ; i++, n++)
1914 if (n == solv->system->nsolvables)
1916 if (i == solv->system->start + solv->system->nsolvables)
1917 i = solv->system->start;
1918 s = pool->solvables + i;
1919 if (solv->decisionmap[i] != 0)
1922 printf("system installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1925 level = setpropagatelearn(solv, level, i, disablerules);
1928 printf("UNSOLVABLE\n");
1932 if (level <= olevel)
1936 if (solv->weaksystemrules)
1938 if (pool->verbose) printf("installing weak system packages\n");
1939 for (i = solv->system->start, n = 0; ; i++, n++)
1941 if (n == solv->system->nsolvables)
1943 if (solv->decisionmap[i] > 0 || (solv->decisionmap[i] < 0 && solv->weaksystemrules[i - solv->system->start] == 0))
1946 if (solv->decisionmap[i] == 0)
1948 if (solv->weaksystemrules[i - solv->system->start])
1950 dp = pool->whatprovidesdata + solv->weaksystemrules[i - solv->system->start];
1951 while ((p = *dp++) != 0)
1953 if (solv->decisionmap[p] > 0)
1955 if (solv->decisionmap[p] == 0)
1959 continue; /* rule is already true */
1965 prune_to_recommended(solv, &dq);
1967 prune_best_version_arch(pool, &dq);
1969 s = pool->solvables + dq.elements[0];
1970 printf("weak system installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1973 level = setpropagatelearn(solv, level, dq.elements[0], disablerules);
1976 printf("UNSOLVABLE\n");
1980 if (level <= olevel)
1986 if (n != solv->system->nsolvables)
1989 systemlevel = level;
1996 if (pool->verbose) printf("deciding unresolved rules\n");
1997 for (i = 1, n = 1; ; i++, n++)
1999 if (n == solv->nrules)
2001 if (i == solv->nrules)
2003 r = solv->rules + i;
2009 /* binary or unary rule */
2010 /* need two positive undecided literals */
2011 if (r->p < 0 || r->w2 <= 0)
2013 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
2015 queuepush(&dq, r->p);
2016 queuepush(&dq, r->w2);
2021 * all negative literals are installed
2022 * no positive literal is installed
2023 * i.e. the rule is not fulfilled and we
2024 * just need to decide on the positive literals
2028 if (solv->decisionmap[-r->p] <= 0)
2033 if (solv->decisionmap[r->p] > 0)
2035 if (solv->decisionmap[r->p] == 0)
2036 queuepush(&dq, r->p);
2038 dp = pool->whatprovidesdata + r->d;
2039 while ((p = *dp++) != 0)
2043 if (solv->decisionmap[-p] <= 0)
2048 if (solv->decisionmap[p] > 0)
2050 if (solv->decisionmap[p] == 0)
2059 /* cannot happen as this means that
2060 * the rule is unit */
2064 prune_to_recommended(solv, &dq);
2066 prune_best_version_arch(pool, &dq);
2067 p = dq.elements[dq.count - 1];
2068 s = pool->solvables + p;
2070 printf("installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2073 level = setpropagatelearn(solv, level, p, disablerules);
2076 printf("UNSOLVABLE\n");
2080 if (level < systemlevel)
2083 } /* for(), decide */
2085 if (n != solv->nrules) /* continue if level < systemlevel */
2088 if (doweak && !solv->problems.count)
2092 if (pool->verbose) printf("installing recommended packages\n");
2094 for (i = 1; i < pool->nsolvables; i++)
2096 if (solv->decisionmap[i] < 0)
2098 if (solv->decisionmap[i] > 0)
2100 Id *recp, rec, *pp, p;
2101 s = pool->solvables + i;
2102 /* installed, check for recommends */
2103 /* XXX need to special case AND ? */
2104 if ((recp = s->recommends) != 0)
2106 while ((rec = *recp++) != 0)
2109 FOR_PROVIDES(p, pp, rec)
2111 if (solv->decisionmap[p] > 0)
2116 else if (solv->decisionmap[p] == 0)
2117 queuepushunique(&dq, p);
2125 s = pool->solvables + i;
2126 if (!s->supplements && !s->freshens)
2128 if (!pool_installable(pool, s))
2130 if ((supp = s->supplements) != 0)
2132 while ((sup = *supp++) != 0)
2133 if (dep_fulfilled(solv, sup))
2138 if ((supp = s->freshens) != 0)
2140 while ((sup = *supp++) != 0)
2141 if (dep_fulfilled(solv, sup))
2146 queuepushunique(&dq, i);
2151 prune_best_version_arch(pool, &dq);
2152 p = dq.elements[dq.count - 1];
2153 s = pool->solvables + p;
2155 printf("installing recommended %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2157 level = setpropagatelearn(solv, level, p, 0);
2172 refine_suggestion(Solver *solv, Id *problem, Id sug, Queue *refined)
2180 printf("refine_suggestion start\n");
2181 queueinit(&disabled);
2182 QUEUEEMPTY(refined);
2183 queuepush(refined, sug);
2188 /* re-enable all rules but rule "sug" of the problem */
2189 for (i = 0; problem[i]; i++)
2191 if (problem[i] == sug)
2193 r = solv->rules + problem[i];
2198 reenablerule(solv, r);
2202 QUEUEEMPTY(&solv->problems);
2203 run_solver(solv, 0, 0);
2204 if (!solv->problems.count)
2206 printf("no more problems!\n");
2208 printdecisions(solv);
2210 break; /* great, no more problems */
2212 disabledcnt = disabled.count;
2213 for (i = 0; i < solv->problems.elements[i]; i++)
2215 /* ignore solutions in refined */
2216 v = solv->problems.elements[i];
2217 for (j = 0; problem[j]; j++)
2218 if (problem[j] != sug && problem[j] == v)
2222 queuepush(&disabled, v);
2224 if (disabled.count == disabledcnt)
2226 /* no solution found, this was an invalid suggestion! */
2227 printf("no solution found!\n");
2231 if (disabled.count == disabledcnt + 1)
2233 /* just one solution, add it to refined list */
2234 queuepush(refined, disabled.elements[disabledcnt]);
2238 printf("############################################## more than one solution found.\n");
2240 for (i = 0; i < solv->problems.elements[i]; i++)
2242 printrule(solv, solv->rules + solv->problems.elements[i]);
2244 printf("##############################################\n");
2246 /* more than one solution */
2247 /* for now return */
2249 for (i = disabledcnt; i < disabled.count; i++)
2251 r = solv->rules + disabled.elements[i];;
2259 revert(solv, 1); /* XXX move to reset_solver? */
2262 /* enable refined rules again */
2263 for (i = 0; i < disabled.count; i++)
2264 reenablerule(solv, solv->rules + disabled.elements[i]);
2265 /* disable problem rules again so that we are in the same state as before */
2266 for (i = 0; problem[i]; i++)
2268 r = solv->rules + problem[i];
2271 printf("refine_suggestion end\n");
2280 id2rc(Solver *solv, Id id)
2283 if (solv->rc_output != 2)
2285 evr = id2str(solv->pool, id);
2286 if (*evr < '0' || *evr > '9')
2288 while (*evr >= '0' && *evr <= '9')
2296 printdecisions(Solver *solv)
2298 Pool *pool = solv->pool;
2299 Id p, *obsoletesmap;
2303 obsoletesmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
2304 for (i = 0; i < solv->decisionq.count; i++)
2309 n = solv->decisionq.elements[i];
2312 if (n >= solv->system->start && n < solv->system->start + solv->system->nsolvables)
2314 s = pool->solvables + n;
2315 if ((obsp = s->obsoletes) != 0)
2316 while ((obs = *obsp++) != 0)
2317 FOR_PROVIDES(p, pp, obs)
2319 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2321 obsoletesmap[p] = n;
2325 FOR_PROVIDES(p, pp, s->name)
2326 if (s->name == pool->solvables[p].name)
2328 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2330 obsoletesmap[p] = n;
2336 if (solv->rc_output)
2337 printf(">!> Solution #1:\n");
2339 int installs = 0, uninstalls = 0, upgrades = 0;
2341 /* print solvables to be erased */
2343 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2345 if (solv->decisionmap[i] > 0)
2347 if (obsoletesmap[i])
2349 s = pool->solvables + i;
2350 if (solv->rc_output == 2)
2351 printf(">!> remove %s-%s%s\n", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2352 else if (solv->rc_output)
2353 printf(">!> remove %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2355 printf("erase %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2359 /* print solvables to be installed */
2361 for (i = 0; i < solv->decisionq.count; i++)
2364 p = solv->decisionq.elements[i];
2367 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2369 s = pool->solvables + p;
2371 if (!obsoletesmap[p])
2373 if (solv->rc_output)
2375 printf("install %s-%s%s", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2376 if (solv->rc_output != 2)
2377 printf(".%s", id2str(pool, s->arch));
2380 else if (!solv->rc_output)
2382 printf("update %s-%s.%s (obsoletes", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2383 for (j = solv->system->start; j < solv->system->start + solv->system->nsolvables; j++)
2385 if (obsoletesmap[j] != p)
2387 s = pool->solvables + j;
2388 printf(" %s-%s.%s", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2395 Solvable *f, *fn = 0;
2396 for (j = solv->system->start; j < solv->system->start + solv->system->nsolvables; j++)
2398 if (obsoletesmap[j] != p)
2400 f = pool->solvables + j;
2401 if (fn || f->name != s->name)
2403 if (solv->rc_output == 2)
2404 printf(">!> remove %s-%s%s\n", id2str(pool, f->name), id2rc(solv, f->evr), id2str(pool, f->evr));
2405 else if (solv->rc_output)
2406 printf(">!> remove %s-%s.%s\n", id2str(pool, f->name), id2str(pool, f->evr), id2str(pool, f->arch));
2414 printf(">!> install %s-%s%s", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2415 if (solv->rc_output != 2)
2416 printf(".%s", id2str(pool, s->arch));
2421 if (solv->rc_output == 2)
2422 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));
2424 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));
2428 if (solv->rc_output)
2430 Source *source = pool_source(pool, s);
2431 if (source && strcmp(source_name(source), "locales"))
2432 printf("[%s]", source_name(source));
2437 if (solv->rc_output)
2438 printf(">!> installs=%d, upgrades=%d, uninstalls=%d\n", installs, upgrades, uninstalls);
2440 xfree(obsoletesmap);
2444 create_obsolete_index(Solver *solv)
2446 Pool *pool = solv->pool;
2448 Source *system = solv->system;
2449 Id p, *pp, obs, *obsp, *obsoletes, *obsoletes_data;
2452 /* create reverse obsoletes map for system solvables */
2453 solv->obsoletes = obsoletes = xcalloc(system->nsolvables, sizeof(Id));
2454 for (i = 1; i < pool->nsolvables; i++)
2456 s = pool->solvables + i;
2457 if ((obsp = s->obsoletes) == 0)
2459 if (!pool_installable(pool, s))
2461 while ((obs = *obsp++) != 0)
2462 FOR_PROVIDES(p, pp, obs)
2464 if (p < system->start || p >= system->start + system->nsolvables)
2466 if (pool->solvables[p].name == s->name)
2468 obsoletes[p - system->start]++;
2472 for (i = 0; i < system->nsolvables; i++)
2475 n += obsoletes[i] + 1;
2478 solv->obsoletes_data = obsoletes_data = xcalloc(n + 1, sizeof(Id));
2479 if (pool->verbose) printf("obsoletes data: %d entries\n", n + 1);
2480 for (i = pool->nsolvables - 1; i > 0; i--)
2482 s = pool->solvables + i;
2483 if ((obsp = s->obsoletes) == 0)
2485 if (!pool_installable(pool, s))
2487 while ((obs = *obsp++) != 0)
2488 FOR_PROVIDES(p, pp, obs)
2490 if (p < system->start || p >= system->start + system->nsolvables)
2492 if (pool->solvables[p].name == s->name)
2495 if (obsoletes_data[obsoletes[p]] != i)
2496 obsoletes_data[--obsoletes[p]] = i;
2501 /*-----------------------------------------------------------------*/
2511 solve(Solver *solv, Queue *job)
2513 Pool *pool = solv->pool;
2515 Map addedmap; /* '1' == have rule for solvable */
2516 Map noupdaterule; /* '1' == don't update (scheduled for removal) */
2517 Id how, what, p, *pp, d;
2523 * create basic rule set of all involved packages
2528 mapinit(&addedmap, pool->nsolvables);
2529 mapinit(&noupdaterule, pool->nsolvables);
2534 * create rules for installed solvables -> keep them installed
2535 * so called: rpm rules
2539 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2540 addrulesforsolvable(solv, pool->solvables + i, &addedmap);
2543 * create install rules
2545 * two passes, as we want to keep the rpm rules distinct from the job rules
2549 if (solv->noupdateprovide && solv->system->nsolvables)
2550 create_obsolete_index(solv);
2554 * process job rules for solvables
2557 for (i = 0; i < job->count; i += 2)
2559 how = job->elements[i];
2560 what = job->elements[i + 1];
2564 case SOLVER_INSTALL_SOLVABLE:
2565 addrulesforsolvable(solv, pool->solvables + what, &addedmap);
2567 case SOLVER_INSTALL_SOLVABLE_NAME:
2568 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
2570 FOR_PROVIDES(p, pp, what)
2572 /* if by name, ensure that the name matches */
2573 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
2575 addrulesforsolvable(solv, pool->solvables + p, &addedmap);
2578 case SOLVER_INSTALL_SOLVABLE_UPDATE:
2579 /* dont allow downgrade */
2580 addupdaterule(solv, pool->solvables + what, &addedmap, 0, 0, 1);
2586 * if unstalls are disallowed, add update rules for every
2587 * installed solvables in the hope to circumvent uninstall
2593 if (!solv->allowuninstall)
2595 /* add update rule for every installed package */
2596 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2597 addupdaterule(solv, pool->solvables + i, &addedmap, solv->allowdowngrade, solv->allowarchchange, 1);
2599 #else /* this is just to add the needed rpm rules to our set */
2600 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2601 addupdaterule(solv, pool->solvables + i, &addedmap, 1, 1, 1);
2604 addrulesforsupplements(solv, &addedmap);
2605 addrulesforenhances(solv, &addedmap); /* do this last */
2609 int possible = 0, installable = 0;
2610 for (i = 1; i < pool->nsolvables; i++)
2612 if (pool_installable(pool, pool->solvables + i))
2614 if (MAPTST(&addedmap, i))
2617 printf("%d of %d installable solvables used for solving\n", possible, installable);
2624 * unify existing rules before going over all job rules
2628 unifyrules(solv); /* remove duplicate rpm rules */
2631 * at this point the system is always solvable,
2632 * as an empty system (remove all packages) is a valid solution
2634 if (pool->verbose) printf("decisions based on rpms: %d\n", solv->decisionq.count);
2637 * now add all job rules
2640 solv->jobrules = solv->nrules;
2642 for (i = 0; i < job->count; i += 2)
2644 how = job->elements[i];
2645 what = job->elements[i + 1];
2648 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
2649 if (solv->rc_output) {
2650 Solvable *s = pool->solvables + what;
2651 printf(">!> Installing %s from channel %s\n", id2str(pool, s->name), source_name(pool_source(pool, s)));
2653 addrule(solv, what, 0); /* install by Id */
2655 case SOLVER_ERASE_SOLVABLE:
2656 addrule(solv, -what, 0); /* remove by Id */
2657 MAPSET(&noupdaterule, what);
2659 case SOLVER_INSTALL_SOLVABLE_NAME: /* install by capability */
2660 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
2662 FOR_PROVIDES(p, pp, what) /* check all providers */
2664 /* if by name, ensure that the name matches */
2665 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
2669 if (!q.count) { /* no provider found -> abort */
2670 fprintf(stderr, "Nothing provides '%s'\n", id2str(pool, what));
2671 /* XXX make this a problem! */
2676 p = queueshift(&q); /* get first provider */
2678 d = 0; /* single provider ? -> make assertion */
2680 d = pool_queuetowhatprovides(pool, &q); /* get all providers */
2681 addrule(solv, p, d); /* add 'requires' rule */
2683 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
2684 case SOLVER_ERASE_SOLVABLE_PROVIDES:
2685 FOR_PROVIDES(p, pp, what)
2687 /* if by name, ensure that the name matches */
2688 if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
2691 addrule(solv, -p, 0); /* add 'remove' rule */
2692 MAPSET(&noupdaterule, p);
2695 case SOLVER_INSTALL_SOLVABLE_UPDATE: /* find update for solvable */
2696 addupdaterule(solv, pool->solvables + what, &addedmap, 0, 0, 0);
2701 if (pool->verbose) printf("problems so far: %d\n", solv->problems.count);
2704 * now add policy rules
2708 solv->systemrules = solv->nrules;
2711 * create rules for updating installed solvables
2717 if (!solv->allowuninstall)
2718 { /* loop over all installed solvables */
2719 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2721 if (!MAPTST(&noupdaterule, i)) /* if not marked as 'noupdate' */
2722 addupdaterule(solv, pool->solvables + i, &addedmap, solv->allowdowngrade, solv->allowarchchange, 0);
2724 addrule(solv, 0, 0); /* place holder */
2726 /* consistency check: we added a rule for _every_ system solvable */
2727 if (solv->nrules - solv->systemrules != solv->system->nsolvables)
2731 if (pool->verbose) printf("problems so far: %d\n", solv->problems.count);
2733 /* create special weak system rules */
2734 if (solv->system->nsolvables)
2736 solv->weaksystemrules = xcalloc(solv->system->nsolvables, sizeof(Id));
2737 for (i = 0; i < solv->system->nsolvables; i++)
2739 findupdatepackages(solv, pool->solvables + solv->system->start + i, &q, (Map *)0, 1, 1);
2741 solv->weaksystemrules[i] = pool_queuetowhatprovides(pool, &q);
2745 /* free unneeded memory */
2747 mapfree(&noupdaterule);
2755 run_solver(solv, 1, 1);
2757 /* find suggested packages */
2758 if (!solv->problems.count)
2760 Id sug, *sugp, enh, *enhp, req, *reqp, p, *pp;
2762 /* create map of all suggests that are still open */
2763 solv->recommends_index = -1;
2764 MAPZERO(&solv->suggestsmap);
2765 for (i = 0; i < solv->decisionq.count; i++)
2767 p = solv->decisionq.elements[i];
2770 s = pool->solvables + p;
2771 if ((sugp = s->suggests) != 0)
2772 while ((sug = *sugp++) != 0)
2774 FOR_PROVIDES(p, pp, sug)
2775 if (solv->decisionmap[p] > 0)
2778 continue; /* already fulfilled */
2779 FOR_PROVIDES(p, pp, sug)
2780 MAPSET(&solv->suggestsmap, p);
2783 for (i = 1; i < pool->nsolvables; i++)
2785 if (solv->decisionmap[i] != 0)
2787 s = pool->solvables + i;
2788 if (!MAPTST(&solv->suggestsmap, i))
2790 if ((enhp = s->enhances) == 0)
2792 if (!pool_installable(pool, s))
2794 while ((enh = *enhp++) != 0)
2795 if (dep_fulfilled(solv, enh))
2800 /* check if installation is possible at all */
2801 if ((reqp = s->requires) != 0)
2803 while ((req = *reqp++) != 0)
2805 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
2807 FOR_PROVIDES(p, pp, req)
2808 if (solv->decisionmap[p] >= 0)
2810 if (!p && !strncmp(id2str(pool, req), "rpmlib(", 7))
2813 break; /* no provider installable! */
2818 queuepush(&solv->suggestions, i);
2820 prune_best_version_arch(pool, &solv->suggestions);
2825 * print solver result
2829 if (pool->verbose) printf("-------------------------------------------------------------\n");
2831 if (solv->problems.count)
2841 clonequeue(&problems, &solv->problems);
2842 queueinit(&solution);
2843 printf("Encountered problems! Here are the solutions:\n");
2844 problem = problems.elements;
2845 for (i = 0; i < problems.count; i++)
2847 Id v = problems.elements[i];
2850 printf("====================================\n");
2851 problem = problems.elements + i + 1;
2854 refine_suggestion(solv, problem, v, &solution);
2855 for (j = 0; j < solution.count; j++)
2857 r = solv->rules + solution.elements[j];
2858 why = solution.elements[j];
2862 if (why >= solv->jobrules && why < solv->systemrules)
2864 /* do a sloppy job of analyzing the job rule */
2867 s = pool->solvables + r->p;
2868 printf("- do not install %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2872 s = pool->solvables - r->p;
2873 printf("- do not erase %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2876 else if (why >= solv->systemrules && why < solv->learntrules)
2879 s = pool->solvables + solv->system->start + (why - solv->systemrules);
2880 if (solv->weaksystemrules && solv->weaksystemrules[why - solv->systemrules])
2882 Id *dp = pool->whatprovidesdata + solv->weaksystemrules[why - solv->systemrules];
2884 if (solv->decisionmap[*dp] > 0)
2886 sd = pool->solvables + *dp;
2892 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));
2896 printf("- allow deinstallation of %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2904 printf("------------------------------------\n");
2909 printdecisions(solv);
2910 if (solv->suggestions.count)
2912 printf("\nsuggested packages:\n");
2913 for (i = 0; i < solv->suggestions.count; i++)
2915 s = pool->solvables + solv->suggestions.elements[i];
2916 printf("- %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));