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 done */
771 s = pool->solvables + n; /* s = Solvable in question */
776 && n >= system->start /* is it installed? */
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) /* dont care about breakage */
803 /* the strategy here is to not insist on dependencies
804 * that are already broken. so if we find one provider
805 * that was already installed, we know that the
806 * dependency was not broken before so we enforce it */
807 for (i = 0; dp[i]; i++) /* for all providers */
809 if (dp[i] >= system->start && dp[i] < system->start + system->nsolvables)
810 break; /* provider is installed */
812 if (!dp[i]) /* previously broken dependency */
814 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));
821 /* nothing provides req! */
823 if (pool->verbose) printf("package %s-%s.%s [%d] is not installable (%s)\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), s - pool->solvables, dep2str(pool, req));
825 addrule(solv, -n, 0); /* mark requestor as uninstallable */
827 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)));
831 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);
832 for (i = 0; dp[i]; i++)
833 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));
835 /* add 'requires' dependency */
836 /* rule: (-requestor|provider1|provider2|...|providerN) */
837 addrule(solv, -n, dp - pool->whatprovidesdata);
839 /* descend the dependency tree */
840 for (; *dp; dp++) /* loop through all providers */
846 } /* while, requirements of n */
848 } /* if, requirements */
851 /*-----------------------------------------
852 * check conflicts of s
855 if ((conp = s->conflicts) != 0)
857 while ((con = *conp++) != 0)
859 FOR_PROVIDES(p, pp, con)
861 /* dontfix: dont care about conflicts with already installed packs */
862 if (dontfix && p >= system->start && p < system->start + system->nsolvables)
864 /* rule: -n|-p: either solvable _or_ provider of conflict */
865 addrule(solv, -n, -p);
870 /*-----------------------------------------
871 * check obsoletes if not installed
873 if (!system || n < system->start || n >= (system->start + system->nsolvables))
874 { /* not installed */
875 if ((obsp = s->obsoletes) != 0)
877 while ((obs = *obsp++) != 0)
879 FOR_PROVIDES(p, pp, obs)
880 addrule(solv, -n, -p);
883 FOR_PROVIDES(p, pp, s->name)
885 if (s->name == pool->solvables[p].name)
886 addrule(solv, -n, -p);
890 /*-----------------------------------------
891 * add recommends to the rule list
893 if ((recp = s->recommends) != 0)
894 while ((rec = *recp++) != 0)
896 FOR_PROVIDES(p, pp, rec)
905 addrulesforsupplements(Solver *solv, Map *m)
907 Pool *pool = solv->pool;
912 if (pool->verbose) printf("addrulesforsupplements... (%d)\n", solv->nrules);
913 for (i = n = 1; n < pool->nsolvables; i++, n++)
915 if (i == pool->nsolvables)
919 s = pool->solvables + i;
920 if (!pool_installable(pool, s))
923 if ((supp = s->supplements) != 0)
924 while ((sup = *supp++) != ID_NULL)
925 if (dep_possible(solv, sup, m))
927 if (!sup && (supp = s->freshens) != 0)
928 while ((sup = *supp++) != ID_NULL)
929 if (dep_possible(solv, sup, m))
933 addrulesforsolvable(solv, s, m);
936 if (pool->verbose) printf("done. (%d)\n", solv->nrules);
940 addrulesforenhances(Solver *solv, Map *m)
942 Pool *pool = solv->pool;
945 Id p, *pp, enh, *enhp, obs, *obsp, con, *conp;
947 if (pool->verbose) printf("addrulesforenhances... (%d)\n", solv->nrules);
948 for (i = 1; i < pool->nsolvables; i++)
952 s = pool->solvables + i;
953 if ((enhp = s->enhances) == 0)
955 if (!pool_installable(pool, s))
957 while ((enh = *enhp++) != ID_NULL)
958 if (dep_possible(solv, enh, m))
962 if ((conp = s->conflicts) != 0)
963 while ((con = *conp++) != 0)
964 FOR_PROVIDES(p, pp, con)
965 addrule(solv, -i, -p);
966 if ((obsp = s->obsoletes) != 0)
967 while ((obs = *obsp++) != ID_NULL)
968 FOR_PROVIDES(p, pp, obs)
969 addrule(solv, -i, -p);
970 FOR_PROVIDES(p, pp, s->name)
971 if (s->name == pool->solvables[p].name)
972 addrule(solv, -i, -p);
978 archchanges(Pool *pool, Solvable *s1, Solvable *s2)
980 Id a1 = s1->arch, a2 = s2->arch;
982 /* we allow changes to/from noarch */
983 if (a1 == a2 || a1 == ARCH_NOARCH || a2 == ARCH_NOARCH)
987 a1 = a1 <= pool->lastarch ? pool->id2arch[a1] : 0;
988 a2 = a2 <= pool->lastarch ? pool->id2arch[a2] : 0;
989 if (((a1 ^ a2) & 0xffff0000) != 0)
996 findupdatepackages(Solver *solv, Solvable *s, Queue *qs, Map *m, int allowdowngrade, int allowarchchange)
998 /* system packages get a special upgrade allowed rule */
999 Pool *pool = solv->pool;
1000 Id p, *pp, n, p2, *pp2;
1008 n = s - pool->solvables;
1009 if (m && !MAPTST(m, n)) /* add rule for s if not already done */
1010 addrulesforsolvable(solv, s, m);
1013 * look for updates for s
1015 FOR_PROVIDES(p, pp, s->name) /* every provider of s' name */
1017 if (p == n) /* skip itself */
1020 if (s->name == pool->solvables[p].name) /* name match */
1022 if (!allowdowngrade /* consider downgrades ? */
1023 && evrcmp(pool, s->evr, pool->solvables[p].evr) > 0)
1026 if (!allowarchchange && archchanges(pool, s, pool->solvables + p))
1029 else if (!solv->noupdateprovide && (obsp = pool->solvables[p].obsoletes) != 0) /* provides/obsoletes combination ? */
1031 while ((obs = *obsp++) != 0) /* for all obsoletes */
1033 FOR_PROVIDES(p2, pp2, obs) /* and all matching providers of the obsoletes */
1035 if (p2 == n) /* match ! */
1038 if (p2) /* match! */
1041 if (!obs) /* continue if no match */
1043 /* here we have 'p' with a matching provides/obsoletes combination
1044 * thus flagging p as a valid update candidate for s
1051 if (m && !MAPTST(m, p)) /* mark p for install if not already done */
1052 addrulesforsolvable(solv, pool->solvables + p, m);
1054 if (solv->noupdateprovide && solv->obsoletes && solv->obsoletes[n - solv->system->start])
1056 for (pp = solv->obsoletes_data + solv->obsoletes[n - solv->system->start]; (p = *pp++) != 0;)
1059 if (m && !MAPTST(m, p)) /* mark p for install if not already done */
1060 addrulesforsolvable(solv, pool->solvables + p, m);
1066 * add rule for update
1067 * (A|A1|A2|A3...) An = update candidates for A
1069 * s = (installed) solvable
1070 * m = 'addedmap', bit set if 'install' rule for solvable exists
1074 addupdaterule(Solver *solv, Solvable *s, Map *m, int allowdowngrade, int allowarchchange, int dontaddrule)
1076 /* system packages get a special upgrade allowed rule */
1077 Pool *pool = solv->pool;
1083 queueinit_buffer(&qs, qsbuf, sizeof(qsbuf)/sizeof(*qsbuf));
1084 findupdatepackages(solv, s, &qs, m, allowdowngrade, allowarchchange);
1085 p = s - pool->solvables;
1086 if (dontaddrule) /* we consider update candidates but dont force them */
1092 if (qs.count == 0) /* no updates found */
1095 printf("new update rule: must keep %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1097 addrule(solv, p, 0); /* request 'install' of s */
1102 d = pool_queuetowhatprovides(pool, &qs); /* intern computed provider queue */
1104 r = addrule(solv, p, d); /* allow update of s */
1106 printf("new update rule ");
1113 /*-----------------------------------------------------------------*/
1120 * initial setup for all watches
1124 makewatches(Solver *solv)
1128 int nsolvables = solv->pool->nsolvables;
1130 xfree(solv->watches);
1131 /* lower half for removals, upper half for installs */
1132 solv->watches = (Id *)xcalloc(2 * nsolvables, sizeof(Id));
1134 /* do it reverse so rpm rules get triggered first */
1135 for (i = 1, r = solv->rules + solv->nrules - 1; i < solv->nrules; i++, r--)
1137 for (i = 1, r = solv->rules + 1; i < solv->nrules; i++, r++)
1140 if (!r->w1 /* rule is disabled */
1141 || !r->w2) /* rule is assertion */
1144 /* see addwatches(solv, r) */
1145 r->n1 = solv->watches[nsolvables + r->w1];
1146 solv->watches[nsolvables + r->w1] = r - solv->rules;
1148 r->n2 = solv->watches[nsolvables + r->w2];
1149 solv->watches[nsolvables + r->w2] = r - solv->rules;
1155 * add watches (for rule)
1159 addwatches(Solver *solv, Rule *r)
1161 int nsolvables = solv->pool->nsolvables;
1163 r->n1 = solv->watches[nsolvables + r->w1];
1164 solv->watches[nsolvables + r->w1] = r - solv->rules;
1166 r->n2 = solv->watches[nsolvables + r->w2];
1167 solv->watches[nsolvables + r->w2] = r - solv->rules;
1171 /*-----------------------------------------------------------------*/
1172 /* rule propagation */
1174 #define DECISIONMAP_TRUE(p) ((p) > 0 ? (decisionmap[p] > 0) : (decisionmap[-p] < 0))
1179 * propagate decision to all rules
1183 propagate(Solver *solv, int level)
1185 Pool *pool = solv->pool;
1190 Id *decisionmap = solv->decisionmap;
1191 Id *watches = solv->watches + pool->nsolvables;
1193 while (solv->propagate_index < solv->decisionq.count)
1195 /* negative because our watches trigger if literal goes FALSE */
1196 pkg = -solv->decisionq.elements[solv->propagate_index++];
1198 printf("popagate for decision %d level %d\n", -pkg, level);
1199 printruleelement(solv, 0, -pkg);
1201 for (rp = watches + pkg; *rp; rp = nrp)
1203 r = solv->rules + *rp;
1205 printf(" watch triggered ");
1218 /* if clause is TRUE, nothing to do */
1219 if (DECISIONMAP_TRUE(ow))
1224 /* not a binary clause, check if we need to move our watch */
1225 if (r->p && r->p != ow && !DECISIONMAP_TRUE(-r->p))
1228 for (dp = pool->whatprovidesdata + r->d; (p = *dp++) != 0;)
1229 if (p != ow && !DECISIONMAP_TRUE(-p))
1233 /* p is free to watch, move watch to p */
1236 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));
1238 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));
1252 watches[p] = r - solv->rules;
1256 /* unit clause found, set other watch to TRUE */
1257 if (DECISIONMAP_TRUE(-ow))
1258 return r; /* eek, a conflict! */
1264 decisionmap[ow] = level;
1266 decisionmap[-ow] = -level;
1267 queuepush(&solv->decisionq, ow);
1268 queuepush(&solv->decisionq_why, r - solv->rules);
1271 Solvable *s = pool->solvables + (ow > 0 ? ow : -ow);
1273 printf(" -> decided to install %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1275 printf(" -> decided to conflict %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1280 return 0; /* all is well */
1284 /*-----------------------------------------------------------------*/
1293 analyze(Solver *solv, int level, Rule *c, int *pr, int *dr, int *why)
1295 Pool *pool = solv->pool;
1298 Map seen; /* global? */
1302 int learnt_why = solv->learnt_pool.count;
1303 Id *decisionmap = solv->decisionmap;
1307 if (pool->verbose) printf("ANALYZE at %d ----------------------\n", level);
1308 mapinit(&seen, pool->nsolvables);
1309 idx = solv->decisionq.count;
1313 queuepush(&solv->learnt_pool, c - solv->rules);
1314 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1325 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1327 vv = v > 0 ? v : -v;
1328 if (MAPTST(&seen, vv))
1330 l = solv->decisionmap[vv];
1337 for (j = 0; j < solv->decisionq.count; j++)
1338 if (solv->decisionq.elements[j] == v)
1340 if (j == solv->decisionq.count)
1342 queuepush(&rulq, -(j + 1));
1344 continue; /* initial setting */
1348 num++; /* need to do this one as well */
1353 printf("PUSH %d ", v);
1354 printruleelement(solv, 0, v);
1361 printf("num = %d\n", num);
1367 v = solv->decisionq.elements[--idx];
1368 vv = v > 0 ? v : -v;
1369 if (MAPTST(&seen, vv))
1372 c = solv->rules + solv->decisionq_why.elements[idx];
1380 else if (r.count == 1 && r.elements[0] < 0)
1381 *dr = r.elements[0];
1383 *dr = pool_queuetowhatprovides(pool, &r);
1386 printf("learned rule for level %d (am %d)\n", rlevel, level);
1387 printruleelement(solv, 0, -v);
1388 for (i = 0; i < r.count; i++)
1391 printruleelement(solv, 0, v);
1395 queuepush(&solv->learnt_pool, 0);
1397 for (i = learnt_why; solv->learnt_pool.elements[i]; i++)
1399 printf("learnt_why ");
1400 printrule(solv, solv->rules + solv->learnt_pool.elements[i]);
1411 * reset the solver decisions to right after the rpm rules
1415 reset_solver(Solver *solv)
1421 /* delete all learnt rules */
1422 solv->nrules = solv->learntrules;
1423 QUEUEEMPTY(&solv->learnt_why);
1424 QUEUEEMPTY(&solv->learnt_pool);
1426 /* redo all direct rpm rule decisions */
1427 /* we break at the first decision with a why attached, this is
1428 * either a job/system rule decision of a propagated decision */
1429 for (i = 0; i < solv->decisionq.count; i++)
1431 v = solv->decisionq.elements[i];
1432 solv->decisionmap[v > 0 ? v : -v] = 0;
1434 for (i = 0; i < solv->decisionq_why.count; i++)
1435 if (solv->decisionq_why.elements[i])
1439 v = solv->decisionq.elements[i];
1440 solv->decisionmap[v > 0 ? v : -v] = v > 0 ? 1 : -1;
1443 if (solv->pool->verbose)
1444 printf("decisions done reduced from %d to %d\n", solv->decisionq.count, i);
1446 solv->decisionq_why.count = i;
1447 solv->decisionq.count = i;
1448 solv->recommends_index = -1;
1449 solv->propagate_index = 0;
1451 /* make direct decisions from enabled unary rules */
1452 for (i = solv->jobrules, r = solv->rules + solv->jobrules; i < solv->nrules; i++, r++)
1454 if (!r->w1 || r->w2)
1460 vv = v > 0 ? v : -v;
1461 if (solv->decisionmap[vv])
1463 /* do not create conflicts */
1464 if (solv->decisionmap[vv] > 0 && v < 0)
1466 if (solv->decisionmap[vv] < 0 && v > 0)
1469 queuepush(&solv->decisionq, v);
1470 queuepush(&solv->decisionq_why, r - solv->rules);
1471 solv->decisionmap[v > 0 ? v : -v] = v > 0 ? 1 : -1;
1473 if (solv->pool->verbose)
1474 printf("decisions after adding job and system rules: %d\n", solv->decisionq.count);
1475 /* recreate watches */
1481 * analyze_unsolvable_rule
1485 analyze_unsolvable_rule(Solver *solv, Rule *c, int disablerules)
1490 why = c - solv->rules;
1492 if (why >= solv->jobrules && why < solv->systemrules)
1494 if (why >= solv->systemrules && why < solv->learntrules)
1495 printf("SYSTEM %d ", why - solv->systemrules);
1496 if (solv->learntrules && why >= solv->learntrules)
1500 if (solv->learntrules && why >= solv->learntrules)
1502 for (i = solv->learnt_why.elements[why - solv->learntrules]; solv->learnt_pool.elements[i]; i++)
1503 analyze_unsolvable_rule(solv, solv->rules + solv->learnt_pool.elements[i], disablerules);
1506 if (why >= solv->jobrules && why < solv->learntrules)
1510 /* turn off rule for further analysis */
1514 if (solv->problems.count)
1516 for (i = solv->problems.count - 1; i >= 0; i--)
1517 if (solv->problems.elements[i] == 0)
1519 else if (solv->problems.elements[i] == why)
1522 queuepush(&solv->problems, why);
1528 * analyze_unsolvable
1532 analyze_unsolvable(Solver *solv, Rule *c, int disablerules)
1534 Pool *pool = solv->pool;
1535 Map seen; /* global? */
1538 Id *decisionmap = solv->decisionmap;
1541 printf("ANALYZE UNSOLVABLE ----------------------\n");
1543 mapinit(&seen, pool->nsolvables);
1544 analyze_unsolvable_rule(solv, c, disablerules);
1545 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1556 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1558 vv = v > 0 ? v : -v;
1559 l = solv->decisionmap[vv];
1564 idx = solv->decisionq.count;
1567 v = solv->decisionq.elements[--idx];
1568 vv = v > 0 ? v : -v;
1569 if (!MAPTST(&seen, vv))
1571 why = solv->decisionq_why.elements[idx];
1576 printruleelement(solv, 0, v);
1580 c = solv->rules + why;
1581 analyze_unsolvable_rule(solv, c, disablerules);
1582 dp = c->d ? pool->whatprovidesdata + c->d : 0;
1593 if (DECISIONMAP_TRUE(v)) /* the one true literal */
1595 vv = v > 0 ? v : -v;
1596 l = solv->decisionmap[vv];
1603 queuepush(&solv->problems, 0); /* mark end of this problem */
1607 printf("analyze_unsolvables done\n");
1612 /*-----------------------------------------------------------------*/
1613 /* Decision revert */
1617 * revert decision at level
1621 revert(Solver *solv, int level)
1624 while (solv->decisionq.count)
1626 v = solv->decisionq.elements[solv->decisionq.count - 1];
1627 vv = v > 0 ? v : -v;
1628 if (solv->decisionmap[vv] <= level && solv->decisionmap[vv] >= -level)
1631 printf("reverting decision %d at %d\n", v, solv->decisionmap[vv]);
1633 solv->decisionmap[vv] = 0;
1634 solv->decisionq.count--;
1635 solv->decisionq_why.count--;
1636 solv->propagate_index = solv->decisionq.count;
1638 solv->recommends_index = -1;
1643 * watch2onhighest - put watch2 on literal with highest level
1647 watch2onhighest(Solver *solv, Rule *r)
1653 return; /* binary rule, both watches are set */
1654 dp = solv->pool->whatprovidesdata + r->d;
1655 while ((v = *dp++) != 0)
1657 l = solv->decisionmap[v < 0 ? -v : v];
1674 setpropagatelearn(Solver *solv, int level, Id decision, int disablerules)
1684 solv->decisionmap[decision] = level;
1686 solv->decisionmap[-decision] = -level;
1687 queuepush(&solv->decisionq, decision);
1688 queuepush(&solv->decisionq_why, 0);
1692 r = propagate(solv, level);
1697 analyze_unsolvable(solv, r, disablerules);
1702 printf("conflict with rule #%d\n", (int)(r - solv->rules));
1703 l = analyze(solv, level, r, &p, &d, &why);
1704 if (l >= level || l <= 0)
1706 printf("reverting decisions (level %d -> %d)\n", level, l);
1708 revert(solv, level);
1709 r = addrule(solv, p, d); /* p requires d */
1712 if (solv->learnt_why.count != (r - solv->rules) - solv->learntrules)
1714 printf("%d %d\n", solv->learnt_why.count, (int)(r - solv->rules) - solv->learntrules);
1717 queuepush(&solv->learnt_why, why);
1720 /* at least 2 literals, needs watches */
1721 watch2onhighest(solv, r);
1722 addwatches(solv, r);
1724 solv->decisionmap[p > 0 ? p : -p] = p > 0 ? level : -level;
1725 queuepush(&solv->decisionq, p);
1726 queuepush(&solv->decisionq_why, r - solv->rules);
1727 printf("decision: ");
1728 printruleelement(solv, 0, p);
1729 printf("new rule: ");
1735 /*-----------------------------------------------------------------*/
1736 /* Main solver interface */
1741 * create solver structure
1743 * pool: all available solvables
1744 * system: installed Solvables
1747 * Upon solving, rules are created to flag the Solvables
1748 * of the 'system' Source as installed.
1752 solver_create(Pool *pool, Source *system)
1755 solv = (Solver *)xcalloc(1, sizeof(Solver));
1757 solv->system = system;
1760 queueinit(&solv->decisionq);
1761 queueinit(&solv->decisionq_why);
1762 queueinit(&solv->problems);
1763 queueinit(&solv->suggestions);
1764 queueinit(&solv->learnt_why);
1765 queueinit(&solv->learnt_pool);
1767 mapinit(&solv->recommendsmap, pool->nsolvables);
1768 mapinit(&solv->suggestsmap, pool->nsolvables);
1769 solv->recommends_index = 0;
1771 solv->decisionmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
1772 solv->rules = (Rule *)xmalloc((solv->nrules + (RULES_BLOCK + 1)) * sizeof(Rule));
1773 memset(solv->rules, 0, sizeof(Rule));
1785 solver_free(Solver *solv)
1787 queuefree(&solv->decisionq);
1788 queuefree(&solv->decisionq_why);
1789 queuefree(&solv->learnt_why);
1790 queuefree(&solv->learnt_pool);
1791 queuefree(&solv->problems);
1792 queuefree(&solv->suggestions);
1794 mapfree(&solv->recommendsmap);
1795 mapfree(&solv->suggestsmap);
1796 xfree(solv->decisionmap);
1798 xfree(solv->watches);
1799 xfree(solv->weaksystemrules);
1800 xfree(solv->obsoletes);
1801 xfree(solv->obsoletes_data);
1806 /*-------------------------------------------------------*/
1811 * all rules have been set up, not actually run the solver
1816 run_solver(Solver *solv, int disablerules, int doweak)
1824 Pool *pool = solv->pool;
1828 printf("number of rules: %d\n", solv->nrules);
1831 for (i = 0; i < solv->nrules; i++)
1833 printrule(solv, solv->rules + i);
1838 /* all new rules are learnt after this point */
1839 solv->learntrules = solv->nrules;
1840 /* crate watches lists */
1843 if (pool->verbose) printf("initial decisions: %d\n", solv->decisionq.count);
1845 /* start SAT algorithm */
1847 systemlevel = level + 1;
1848 if (pool->verbose) printf("solving...\n");
1859 if (pool->verbose) printf("propagating (%d %d)...\n", solv->propagate_index, solv->decisionq.count);
1860 if ((r = propagate(solv, level)) != 0)
1862 analyze_unsolvable(solv, r, disablerules);
1865 printf("UNSOLVABLE\n");
1875 if (level < systemlevel && solv->system->nsolvables)
1877 if (!solv->updatesystem)
1879 /* try to keep as many packages as possible */
1880 if (pool->verbose) printf("installing system packages\n");
1881 for (i = solv->system->start, n = 0; ; i++, n++)
1883 if (n == solv->system->nsolvables)
1885 if (i == solv->system->start + solv->system->nsolvables)
1886 i = solv->system->start;
1887 s = pool->solvables + i;
1888 if (solv->decisionmap[i] != 0)
1891 printf("system installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1894 level = setpropagatelearn(solv, level, i, disablerules);
1897 printf("UNSOLVABLE\n");
1901 if (level <= olevel)
1905 if (solv->weaksystemrules)
1907 if (pool->verbose) printf("installing weak system packages\n");
1908 for (i = solv->system->start, n = 0; ; i++, n++)
1910 if (n == solv->system->nsolvables)
1912 if (solv->decisionmap[i] > 0 || (solv->decisionmap[i] < 0 && solv->weaksystemrules[i - solv->system->start] == 0))
1915 if (solv->decisionmap[i] == 0)
1917 if (solv->weaksystemrules[i - solv->system->start])
1919 dp = pool->whatprovidesdata + solv->weaksystemrules[i - solv->system->start];
1920 while ((p = *dp++) != 0)
1922 if (solv->decisionmap[p] > 0)
1924 if (solv->decisionmap[p] == 0)
1928 continue; /* rule is already true */
1934 prune_to_recommended(solv, &dq);
1936 prune_best_version_arch(pool, &dq);
1938 s = pool->solvables + dq.elements[0];
1939 printf("weak system installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
1942 level = setpropagatelearn(solv, level, dq.elements[0], disablerules);
1945 printf("UNSOLVABLE\n");
1949 if (level <= olevel)
1955 if (n != solv->system->nsolvables)
1958 systemlevel = level;
1965 if (pool->verbose) printf("deciding unresolved rules\n");
1966 for (i = 1, n = 1; ; i++, n++)
1968 if (n == solv->nrules)
1970 if (i == solv->nrules)
1972 r = solv->rules + i;
1978 /* binary or unary rule */
1979 /* need two positive undecided literals */
1980 if (r->p < 0 || r->w2 <= 0)
1982 if (solv->decisionmap[r->p] || solv->decisionmap[r->w2])
1984 queuepush(&dq, r->p);
1985 queuepush(&dq, r->w2);
1990 * all negative literals are installed
1991 * no positive literal is installed
1992 * i.e. the rule is not fulfilled and we
1993 * just need to decide on the positive literals
1997 if (solv->decisionmap[-r->p] <= 0)
2002 if (solv->decisionmap[r->p] > 0)
2004 if (solv->decisionmap[r->p] == 0)
2005 queuepush(&dq, r->p);
2007 dp = pool->whatprovidesdata + r->d;
2008 while ((p = *dp++) != 0)
2012 if (solv->decisionmap[-p] <= 0)
2017 if (solv->decisionmap[p] > 0)
2019 if (solv->decisionmap[p] == 0)
2028 /* cannot happen as this means that
2029 * the rule is unit */
2033 prune_to_recommended(solv, &dq);
2035 prune_best_version_arch(pool, &dq);
2036 p = dq.elements[dq.count - 1];
2037 s = pool->solvables + p;
2039 printf("installing %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2042 level = setpropagatelearn(solv, level, p, disablerules);
2045 printf("UNSOLVABLE\n");
2049 if (level < systemlevel)
2052 } /* for(), decide */
2054 if (n != solv->nrules) /* continue if level < systemlevel */
2057 if (doweak && !solv->problems.count)
2061 if (pool->verbose) printf("installing recommended packages\n");
2063 for (i = 1; i < pool->nsolvables; i++)
2065 if (solv->decisionmap[i] < 0)
2067 if (solv->decisionmap[i] > 0)
2069 Id *recp, rec, *pp, p;
2070 s = pool->solvables + i;
2071 /* installed, check for recommends */
2072 /* XXX need to special case AND ? */
2073 if ((recp = s->recommends) != 0)
2075 while ((rec = *recp++) != 0)
2078 FOR_PROVIDES(p, pp, rec)
2080 if (solv->decisionmap[p] > 0)
2085 else if (solv->decisionmap[p] == 0)
2086 queuepushunique(&dq, p);
2094 s = pool->solvables + i;
2095 if (!s->supplements && !s->freshens)
2097 if (!pool_installable(pool, s))
2099 if ((supp = s->supplements) != 0)
2101 while ((sup = *supp++) != 0)
2102 if (dep_fulfilled(solv, sup))
2107 if ((supp = s->freshens) != 0)
2109 while ((sup = *supp++) != 0)
2110 if (dep_fulfilled(solv, sup))
2115 queuepushunique(&dq, i);
2120 prune_best_version_arch(pool, &dq);
2121 p = dq.elements[dq.count - 1];
2122 s = pool->solvables + p;
2124 printf("installing recommended %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2126 level = setpropagatelearn(solv, level, p, 0);
2141 refine_suggestion(Solver *solv, Id *problem, Id sug, Queue *refined)
2149 printf("refine_suggestion start\n");
2150 queueinit(&disabled);
2151 QUEUEEMPTY(refined);
2152 queuepush(refined, sug);
2154 /* re-enable all rules but rule "sug" of the problem */
2157 for (i = 0; problem[i]; i++)
2159 if (problem[i] == sug)
2161 r = solv->rules + problem[i];
2168 /* direct assertion */
2169 v = r->p > 0 ? r->p : -r->p;
2170 if (solv->decisionmap[v])
2172 if ((solv->decisionmap[v] > 0 && r->p < 0) ||
2173 (solv->decisionmap[v] < 0 && r->p > 0))
2175 printf("direct assertion failure, no solution found!\n");
2178 r = solv->rules + problem[i];
2185 if (r->d == 0 || r->w2 != r->p)
2188 r->w1 = solv->pool->whatprovidesdata[r->d];
2193 QUEUEEMPTY(&solv->problems);
2194 run_solver(solv, 0, 0);
2195 if (!solv->problems.count)
2197 printf("no more problems!\n");
2199 printdecisions(solv);
2201 break; /* great, no more problems */
2203 disabledcnt = disabled.count;
2204 for (i = 0; i < solv->problems.elements[i]; i++)
2206 /* ignore solutions in refined */
2207 v = solv->problems.elements[i];
2208 for (j = 0; problem[j]; j++)
2209 if (problem[j] != sug && problem[j] == v)
2213 queuepush(&disabled, v);
2214 queuepush(&disabled, 0); /* room for watch */
2216 if (disabled.count == disabledcnt)
2218 /* no solution found, this was an invalid suggestion! */
2219 printf("no solution found!\n");
2223 if (disabled.count == disabledcnt + 2)
2225 /* just one solution, add it to refined list */
2226 queuepush(refined, disabled.elements[disabledcnt]);
2230 printf("############################################## more than one solution found.\n");
2232 for (i = 0; i < solv->problems.elements[i]; i++)
2234 printrule(solv, solv->rules + solv->problems.elements[i]);
2236 printf("##############################################\n");
2238 /* more than one solution */
2239 /* for now return */
2241 for (i = disabledcnt; i < disabled.count; i += 2)
2244 r = solv->rules + disabled.elements[i];
2245 disabled.elements[i + 1] = r->w1;
2252 revert(solv, 1); /* XXX move to reset_solver? */
2255 /* enable refined rules again */
2256 for (i = 0; i < disabled.count; i += 2)
2258 r = solv->rules + disabled.elements[i];
2259 r->w1 = disabled.elements[i + 1];
2261 /* disable problem rules again so that we are in the same state as before */
2262 for (i = 0; problem[i]; i++)
2264 r = solv->rules + problem[i];
2267 printf("refine_suggestion end\n");
2276 id2rc(Solver *solv, Id id)
2279 if (solv->rc_output != 2)
2281 evr = id2str(solv->pool, id);
2282 if (*evr < '0' || *evr > '9')
2284 while (*evr >= '0' && *evr <= '9')
2292 printdecisions(Solver *solv)
2294 Pool *pool = solv->pool;
2295 Id p, *obsoletesmap;
2299 obsoletesmap = (Id *)xcalloc(pool->nsolvables, sizeof(Id));
2300 for (i = 0; i < solv->decisionq.count; i++)
2305 n = solv->decisionq.elements[i];
2308 if (n >= solv->system->start && n < solv->system->start + solv->system->nsolvables)
2310 s = pool->solvables + n;
2311 if ((obsp = s->obsoletes) != 0)
2312 while ((obs = *obsp++) != 0)
2313 FOR_PROVIDES(p, pp, obs)
2315 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2317 obsoletesmap[p] = n;
2321 FOR_PROVIDES(p, pp, s->name)
2322 if (s->name == pool->solvables[p].name)
2324 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2326 obsoletesmap[p] = n;
2332 if (solv->rc_output)
2333 printf(">!> Solution #1:\n");
2335 int installs = 0, uninstalls = 0, upgrades = 0;
2337 /* print solvables to be erased */
2339 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2341 if (solv->decisionmap[i] > 0)
2343 if (obsoletesmap[i])
2345 s = pool->solvables + i;
2346 if (solv->rc_output == 2)
2347 printf(">!> remove %s-%s%s\n", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2348 else if (solv->rc_output)
2349 printf(">!> remove %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2351 printf("erase %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2355 /* print solvables to be installed */
2357 for (i = 0; i < solv->decisionq.count; i++)
2360 p = solv->decisionq.elements[i];
2363 if (p >= solv->system->start && p < solv->system->start + solv->system->nsolvables)
2365 s = pool->solvables + p;
2367 if (!obsoletesmap[p])
2369 if (solv->rc_output)
2371 printf("install %s-%s%s", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2372 if (solv->rc_output != 2)
2373 printf(".%s", id2str(pool, s->arch));
2376 else if (!solv->rc_output)
2378 printf("update %s-%s.%s (obsoletes", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2379 for (j = solv->system->start; j < solv->system->start + solv->system->nsolvables; j++)
2381 if (obsoletesmap[j] != p)
2383 s = pool->solvables + j;
2384 printf(" %s-%s.%s", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2391 Solvable *f, *fn = 0;
2392 for (j = solv->system->start; j < solv->system->start + solv->system->nsolvables; j++)
2394 if (obsoletesmap[j] != p)
2396 f = pool->solvables + j;
2397 if (fn || f->name != s->name)
2399 if (solv->rc_output == 2)
2400 printf(">!> remove %s-%s%s\n", id2str(pool, f->name), id2rc(solv, f->evr), id2str(pool, f->evr));
2401 else if (solv->rc_output)
2402 printf(">!> remove %s-%s.%s\n", id2str(pool, f->name), id2str(pool, f->evr), id2str(pool, f->arch));
2410 printf(">!> install %s-%s%s", id2str(pool, s->name), id2rc(solv, s->evr), id2str(pool, s->evr));
2411 if (solv->rc_output != 2)
2412 printf(".%s", id2str(pool, s->arch));
2417 if (solv->rc_output == 2)
2418 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));
2420 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));
2424 if (solv->rc_output)
2426 Source *source = pool_source(pool, s);
2427 if (source && strcmp(source_name(source), "locales"))
2428 printf("[%s]", source_name(source));
2433 if (solv->rc_output)
2434 printf(">!> installs=%d, upgrades=%d, uninstalls=%d\n", installs, upgrades, uninstalls);
2436 xfree(obsoletesmap);
2440 create_obsolete_index(Solver *solv)
2442 Pool *pool = solv->pool;
2444 Source *system = solv->system;
2445 Id p, *pp, obs, *obsp, *obsoletes, *obsoletes_data;
2448 /* create reverse obsoletes map for system solvables */
2449 solv->obsoletes = obsoletes = xcalloc(system->nsolvables, sizeof(Id));
2450 for (i = 1; i < pool->nsolvables; i++)
2452 s = pool->solvables + i;
2453 if ((obsp = s->obsoletes) == 0)
2455 if (!pool_installable(pool, s))
2457 while ((obs = *obsp++) != 0)
2458 FOR_PROVIDES(p, pp, obs)
2460 if (p < system->start || p >= system->start + system->nsolvables)
2462 if (pool->solvables[p].name == s->name)
2464 obsoletes[p - system->start]++;
2468 for (i = 0; i < system->nsolvables; i++)
2471 n += obsoletes[i] + 1;
2474 solv->obsoletes_data = obsoletes_data = xcalloc(n + 1, sizeof(Id));
2475 if (pool->verbose) printf("obsoletes data: %d entries\n", n + 1);
2476 for (i = pool->nsolvables - 1; i > 0; i--)
2478 s = pool->solvables + i;
2479 if ((obsp = s->obsoletes) == 0)
2481 if (!pool_installable(pool, s))
2483 while ((obs = *obsp++) != 0)
2484 FOR_PROVIDES(p, pp, obs)
2486 if (p < system->start || p >= system->start + system->nsolvables)
2488 if (pool->solvables[p].name == s->name)
2491 if (obsoletes_data[obsoletes[p]] != i)
2492 obsoletes_data[--obsoletes[p]] = i;
2497 /*-----------------------------------------------------------------*/
2507 solve(Solver *solv, Queue *job)
2509 Pool *pool = solv->pool;
2511 Map addedmap; /* '1' == have rule for solvable */
2512 Map noupdaterule; /* '1' == don't update (scheduled for removal) */
2513 Id how, what, p, *pp, d;
2519 * create basic rule set of all involved packages
2524 mapinit(&addedmap, pool->nsolvables);
2525 mapinit(&noupdaterule, pool->nsolvables);
2530 * create rules for installed solvables -> keep them installed
2531 * so called: rpm rules
2535 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2536 addrulesforsolvable(solv, pool->solvables + i, &addedmap);
2539 * create install rules
2541 * two passes, as we want to keep the rpm rules distinct from the job rules
2545 if (solv->noupdateprovide && solv->system->nsolvables)
2546 create_obsolete_index(solv);
2550 * process job rules for solvables
2553 for (i = 0; i < job->count; i += 2)
2555 how = job->elements[i];
2556 what = job->elements[i + 1];
2560 case SOLVER_INSTALL_SOLVABLE:
2561 addrulesforsolvable(solv, pool->solvables + what, &addedmap);
2563 case SOLVER_INSTALL_SOLVABLE_NAME:
2564 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
2566 FOR_PROVIDES(p, pp, what)
2568 /* if by name, ensure that the name matches */
2569 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
2571 addrulesforsolvable(solv, pool->solvables + p, &addedmap);
2574 case SOLVER_INSTALL_SOLVABLE_UPDATE:
2575 /* dont allow downgrade */
2576 addupdaterule(solv, pool->solvables + what, &addedmap, 0, 0, 1);
2582 * if unstalls are disallowed, add update rules for every
2583 * installed solvables in the hope to circumvent uninstall
2589 if (!solv->allowuninstall)
2591 /* add update rule for every installed package */
2592 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2593 addupdaterule(solv, pool->solvables + i, &addedmap, solv->allowdowngrade, solv->allowarchchange, 1);
2595 #else /* this is just to add the needed rpm rules to our set */
2596 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2597 addupdaterule(solv, pool->solvables + i, &addedmap, 1, 1, 1);
2600 addrulesforsupplements(solv, &addedmap);
2601 addrulesforenhances(solv, &addedmap); /* do this last */
2605 int possible = 0, installable = 0;
2606 for (i = 1; i < pool->nsolvables; i++)
2608 if (pool_installable(pool, pool->solvables + i))
2610 if (MAPTST(&addedmap, i))
2613 printf("%d of %d installable solvables used for solving\n", possible, installable);
2620 * unify existing rules before going over all job rules
2624 unifyrules(solv); /* remove duplicate rpm rules */
2627 * at this point the system is always solvable,
2628 * as an empty system (remove all packages) is a valid solution
2630 if (pool->verbose) printf("decisions based on rpms: %d\n", solv->decisionq.count);
2633 * now add all job rules
2636 solv->jobrules = solv->nrules;
2638 for (i = 0; i < job->count; i += 2)
2640 how = job->elements[i];
2641 what = job->elements[i + 1];
2644 case SOLVER_INSTALL_SOLVABLE: /* install specific solvable */
2645 if (solv->rc_output) {
2646 Solvable *s = pool->solvables + what;
2647 printf(">!> Installing %s from channel %s\n", id2str(pool, s->name), source_name(pool_source(pool, s)));
2649 addrule(solv, what, 0); /* install by Id */
2651 case SOLVER_ERASE_SOLVABLE:
2652 addrule(solv, -what, 0); /* remove by Id */
2653 MAPSET(&noupdaterule, what);
2655 case SOLVER_INSTALL_SOLVABLE_NAME: /* install by capability */
2656 case SOLVER_INSTALL_SOLVABLE_PROVIDES:
2658 FOR_PROVIDES(p, pp, what) /* check all providers */
2660 /* if by name, ensure that the name matches */
2661 if (how == SOLVER_INSTALL_SOLVABLE_NAME && pool->solvables[p].name != what)
2665 if (!q.count) { /* no provider found -> abort */
2666 fprintf(stderr, "Nothing provides '%s'\n", id2str(pool, what));
2667 /* XXX make this a problem! */
2672 p = queueshift(&q); /* get first provider */
2674 d = 0; /* single provider ? -> make assertion */
2676 d = pool_queuetowhatprovides(pool, &q); /* get all providers */
2677 addrule(solv, p, d); /* add 'requires' rule */
2679 case SOLVER_ERASE_SOLVABLE_NAME: /* remove by capability */
2680 case SOLVER_ERASE_SOLVABLE_PROVIDES:
2681 FOR_PROVIDES(p, pp, what)
2683 /* if by name, ensure that the name matches */
2684 if (how == SOLVER_ERASE_SOLVABLE_NAME && pool->solvables[p].name != what)
2687 addrule(solv, -p, 0); /* add 'remove' rule */
2688 MAPSET(&noupdaterule, p);
2691 case SOLVER_INSTALL_SOLVABLE_UPDATE: /* find update for solvable */
2692 addupdaterule(solv, pool->solvables + what, &addedmap, 0, 0, 0);
2697 if (pool->verbose) printf("problems so far: %d\n", solv->problems.count);
2700 * now add policy rules
2704 solv->systemrules = solv->nrules;
2707 * create rules for updating installed solvables
2713 if (!solv->allowuninstall)
2714 { /* loop over all installed solvables */
2715 for (i = solv->system->start; i < solv->system->start + solv->system->nsolvables; i++)
2717 if (!MAPTST(&noupdaterule, i)) /* if not marked as 'noupdate' */
2718 addupdaterule(solv, pool->solvables + i, &addedmap, solv->allowdowngrade, solv->allowarchchange, 0);
2720 addrule(solv, 0, 0); /* place holder */
2722 /* consistency check: we added a rule for _every_ system solvable */
2723 if (solv->nrules - solv->systemrules != solv->system->nsolvables)
2727 if (pool->verbose) printf("problems so far: %d\n", solv->problems.count);
2729 /* create special weak system rules */
2730 if (solv->system->nsolvables)
2732 solv->weaksystemrules = xcalloc(solv->system->nsolvables, sizeof(Id));
2733 for (i = 0; i < solv->system->nsolvables; i++)
2735 findupdatepackages(solv, pool->solvables + solv->system->start + i, &q, (Map *)0, 1, 1);
2737 solv->weaksystemrules[i] = pool_queuetowhatprovides(pool, &q);
2741 /* free unneeded memory */
2743 mapfree(&noupdaterule);
2751 run_solver(solv, 1, 1);
2753 /* find suggested packages */
2754 if (!solv->problems.count)
2756 Id sug, *sugp, enh, *enhp, req, *reqp, p, *pp;
2758 /* create map of all suggests that are still open */
2759 solv->recommends_index = -1;
2760 MAPZERO(&solv->suggestsmap);
2761 for (i = 0; i < solv->decisionq.count; i++)
2763 p = solv->decisionq.elements[i];
2766 s = pool->solvables + p;
2767 if ((sugp = s->suggests) != 0)
2768 while ((sug = *sugp++) != 0)
2770 FOR_PROVIDES(p, pp, sug)
2771 if (solv->decisionmap[p] > 0)
2774 continue; /* already fulfilled */
2775 FOR_PROVIDES(p, pp, sug)
2776 MAPSET(&solv->suggestsmap, p);
2779 for (i = 1; i < pool->nsolvables; i++)
2781 if (solv->decisionmap[i] != 0)
2783 s = pool->solvables + i;
2784 if (!MAPTST(&solv->suggestsmap, i))
2786 if ((enhp = s->enhances) == 0)
2788 if (!pool_installable(pool, s))
2790 while ((enh = *enhp++) != 0)
2791 if (dep_fulfilled(solv, enh))
2796 /* check if installation is possible at all */
2797 if ((reqp = s->requires) != 0)
2799 while ((req = *reqp++) != 0)
2801 if (req == SOLVABLE_PREREQMARKER) /* skip the marker */
2803 FOR_PROVIDES(p, pp, req)
2804 if (solv->decisionmap[p] >= 0)
2806 if (!p && !strncmp(id2str(pool, req), "rpmlib(", 7))
2809 break; /* no provider installable! */
2814 queuepush(&solv->suggestions, i);
2816 prune_best_version_arch(pool, &solv->suggestions);
2821 * print solver result
2825 if (pool->verbose) printf("-------------------------------------------------------------\n");
2827 if (solv->problems.count)
2837 clonequeue(&problems, &solv->problems);
2838 queueinit(&solution);
2839 printf("Encountered problems! Here are the solutions:\n");
2840 problem = problems.elements;
2841 for (i = 0; i < problems.count; i++)
2843 Id v = problems.elements[i];
2846 printf("====================================\n");
2847 problem = problems.elements + i + 1;
2850 refine_suggestion(solv, problem, v, &solution);
2851 for (j = 0; j < solution.count; j++)
2853 r = solv->rules + solution.elements[j];
2854 why = solution.elements[j];
2858 if (why >= solv->jobrules && why < solv->systemrules)
2860 /* do a sloppy job of analyzing the job rule */
2863 s = pool->solvables + r->p;
2864 printf("- do not install %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2868 s = pool->solvables - r->p;
2869 printf("- do not erase %s-%s.%s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
2872 else if (why >= solv->systemrules && why < solv->learntrules)
2875 s = pool->solvables + solv->system->start + (why - solv->systemrules);
2876 if (solv->weaksystemrules && solv->weaksystemrules[why - solv->systemrules])
2878 Id *dp = pool->whatprovidesdata + solv->weaksystemrules[why - solv->systemrules];
2881 if (*dp >= solv->system->start && *dp < solv->system->start + solv->system->nsolvables)
2883 if (solv->decisionmap[*dp] > 0)
2885 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 [%d]\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch), s - pool->solvables);
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));