- start transaction ordering
[platform/upstream/libsolv.git] / src / solver.h
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * solver.h
10  *
11  */
12
13 #ifndef SATSOLVER_SOLVER_H
14 #define SATSOLVER_SOLVER_H
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 #include "pooltypes.h"
21 #include "pool.h"
22 #include "repo.h"
23 #include "queue.h"
24 #include "bitmap.h"
25 #include "transaction.h"
26 /*
27  * Callback definitions in order to "overwrite" the policies by an external application.
28  */
29  
30 typedef void  (*BestSolvableCb) (Pool *pool, Queue *canditates);
31 typedef int  (*ArchCheckCb) (Pool *pool, Solvable *solvable1, Solvable *solvable2);
32 typedef int  (*VendorCheckCb) (Pool *pool, Solvable *solvable1, Solvable *solvable2);
33 typedef void (*UpdateCandidateCb) (Pool *pool, Solvable *solvable, Queue *canditates);
34
35
36 /* ----------------------------------------------
37  * Rule
38  *
39  *   providerN(B) == Package Id of package providing tag B
40  *   N = 1, 2, 3, in case of multiple providers
41  *
42  * A requires B : !A | provider1(B) | provider2(B)
43  *
44  * A conflicts B : (!A | !provider1(B)) & (!A | !provider2(B)) ...
45  *
46  * 'not' is encoded as a negative Id
47  * 
48  * Binary rule: p = first literal, d = 0, w2 = second literal, w1 = p
49  */
50
51 typedef struct rule {
52   Id p;                 /* first literal in rule */
53   Id d;                 /* Id offset into 'list of providers terminated by 0' as used by whatprovides; pool->whatprovides + d */
54                         /* in case of binary rules, d == 0, w1 == p, w2 == other literal */
55                         /* in case of disabled rules: ~d, aka -d - 1 */
56   Id w1, w2;            /* watches, literals not-yet-decided */
57                                        /* if !w2, assertion, not rule */
58   Id n1, n2;            /* next rules in linked list, corresponding to w1,w2 */
59 } Rule;
60
61 struct _Solver;
62
63 typedef struct _Solver {
64   Pool *pool;
65   Queue job;                            /* copy of the job we're solving */
66
67   Queue transaction;                    /* solver result */
68   Queue transaction_info;               /* transaction obsoletes info */
69   Id *transaction_installed;            /* data for installed packages */
70
71   Repo *installed;                      /* copy of pool->installed */
72   
73   /* list of rules, ordered
74    * rpm rules first, then features, updates, jobs, learnt
75    * see start/end offsets below
76    */
77   Rule *rules;                          /* all rules */
78   Id nrules;                            /* [Offset] index of the last rule */
79
80   Queue ruleassertions;                 /* Queue of all assertion rules */
81
82   /* start/end offset for rule 'areas' */
83     
84   Id rpmrules_end;                      /* [Offset] rpm rules end */
85     
86   Id featurerules;                      /* feature rules start/end */
87   Id featurerules_end;
88     
89   Id updaterules;                       /* policy rules, e.g. keep packages installed or update. All literals > 0 */
90   Id updaterules_end;
91     
92   Id jobrules;                          /* user rules */
93   Id jobrules_end;
94
95   Id infarchrules;                      /* inferior arch rules */
96   Id infarchrules_end;
97
98   Id duprules;                          /* dist upgrade rules */
99   Id duprules_end;
100     
101   Id learntrules;                       /* learnt rules, (end == nrules) */
102
103   Map noupdate;                         /* don't try to update these
104                                            installed solvables */
105   Map noobsoletes;                      /* ignore obsoletes for these
106                                            (multiinstall) */
107
108   Map updatemap;                        /* bring those packages to the newest version */
109   Queue weakruleq;                      /* index into 'rules' for weak ones */
110   Map weakrulemap;                      /* map rule# to '1' for weak rules, 1..learntrules */
111
112   Id *watches;                          /* Array of rule offsets
113                                          * watches has nsolvables*2 entries and is addressed from the middle
114                                          * middle-solvable : decision to conflict, offset point to linked-list of rules
115                                          * middle+solvable : decision to install: offset point to linked-list of rules
116                                          */
117
118   Queue ruletojob;                      /* index into job queue: jobs for which a rule exits */
119
120   /* our decisions: */
121   Queue decisionq;                      /* >0:install, <0:remove/conflict */
122   Queue decisionq_why;                  /* index of rule, Offset into rules */
123   int directdecisions;                  /* number of decisions with no rule */
124
125   Id *decisionmap;                      /* map for all available solvables,
126                                          * = 0: undecided
127                                          * > 0: level of decision when installed,
128                                          * < 0: level of decision when conflict */
129
130   /* learnt rule history */
131   Queue learnt_why;
132   Queue learnt_pool;
133
134   Queue branches;
135   int (*solution_callback)(struct _Solver *solv, void *data);
136   void *solution_callback_data;
137
138   int propagate_index;                  /* index into decisionq for non-propagated decisions */
139
140   Queue problems;                       /* list of lists of conflicting rules, < 0 for job rules */
141   Queue solutions;                      /* refined problem storage space */
142
143   Queue recommendations;                /* recommended packages */
144   Queue suggestions;                    /* suggested packages */
145   Queue orphaned;                       /* orphaned packages */
146
147   int stats_learned;                    /* statistic */
148   int stats_unsolvable;                 /* statistic */
149
150   Map recommendsmap;                    /* recommended packages from decisionmap */
151   Map suggestsmap;                      /* suggested packages from decisionmap */
152   int recommends_index;                 /* recommendsmap/suggestsmap is created up to this level */
153
154   Id *obsoletes;                        /* obsoletes for each installed solvable */
155   Id *obsoletes_data;                   /* data area for obsoletes */
156   Id *multiversionupdaters;             /* updaters for multiversion packages in updatesystem mode */
157
158   /*-------------------------------------------------------------------------------------------------------------
159    * Solver configuration
160    *-------------------------------------------------------------------------------------------------------------*/
161
162   int fixsystem;                        /* repair errors in rpm dependency graph */
163   int allowdowngrade;                   /* allow to downgrade installed solvable */
164   int allowarchchange;                  /* allow to change architecture of installed solvables */
165   int allowvendorchange;                /* allow to change vendor of installed solvables */
166   int allowuninstall;                   /* allow removal of installed solvables */
167   int updatesystem;                     /* update all packages to the newest version */
168   int allowvirtualconflicts;            /* false: conflicts on package name, true: conflicts on package provides */
169   int allowselfconflicts;               /* true: packages wich conflict with itself are installable */
170   int obsoleteusesprovides;             /* true: obsoletes are matched against provides, not names */
171   int implicitobsoleteusesprovides;     /* true: implicit obsoletes due to same name are matched against provides, not names */
172   int noupdateprovide;                  /* true: update packages needs not to provide old package */
173   int dosplitprovides;                  /* true: consider legacy split provides */
174   int dontinstallrecommended;           /* true: do not install recommended packages */
175   int ignorealreadyrecommended;         /* true: ignore recommended packages that were already recommended by the installed packages */
176   int dontshowinstalledrecommended;     /* true: do not show recommended packages that are already installed */
177   
178   /* distupgrade also needs updatesystem and dosplitprovides */
179   int distupgrade;
180   int distupgrade_removeunsupported;
181
182   int noinfarchcheck;                   /* true: do not forbid inferior architectures */
183
184   /* Callbacks for defining the bahaviour of the SAT solver */
185
186   /* Finding best candidate
187    *
188    * Callback definition:
189    * void  bestSolvable (Pool *pool, Queue *canditates)
190    *     candidates       : List of canditates which has to be sorted by the function call
191    *     return candidates: Sorted list of the candidates(first is the best).
192    */
193    BestSolvableCb bestSolvableCb;
194
195   /* Checking if two solvables has compatible architectures
196    *
197    * Callback definition:
198    *     int  archCheck (Pool *pool, Solvable *solvable1, Solvable *solvable2);
199    *     
200    *     return 0 it the two solvables has compatible architectures
201    */
202    ArchCheckCb archCheckCb;
203
204   /* Checking if two solvables has compatible vendors
205    *
206    * Callback definition:
207    *     int  vendorCheck (Pool *pool, Solvable *solvable1, Solvable *solvable2);
208    *     
209    *     return 0 it the two solvables has compatible architectures
210    */
211    VendorCheckCb vendorCheckCb;
212     
213   /* Evaluate update candidate
214    *
215    * Callback definition:
216    * void pdateCandidateCb (Pool *pool, Solvable *solvable, Queue *canditates)
217    *     solvable   : for which updates should be search
218    *     candidates : List of candidates (This list depends on other
219    *                  restrictions like architecture and vendor policies too)
220    */
221    UpdateCandidateCb   updateCandidateCb;
222     
223
224   /* some strange queue that doesn't belong here */
225   Queue covenantq;                      /* Covenants honored by this solver (generic locks) */
226
227   
228   Map dupmap;                           /* packages from dup repos */
229   Map dupinvolvedmap;                   /* packages involved in dup process */
230
231   Queue *ruleinfoq;                     /* tmp space for solver_ruleinfo() */
232 } Solver;
233
234 /*
235  * queue commands
236  */
237
238 #define SOLVER_SOLVABLE                 0x01
239 #define SOLVER_SOLVABLE_NAME            0x02
240 #define SOLVER_SOLVABLE_PROVIDES        0x03
241 #define SOLVER_SOLVABLE_ONE_OF          0x04
242
243 #define SOLVER_SELECTMASK               0xff
244
245 #define SOLVER_NOOP                     0x0000
246 #define SOLVER_INSTALL                  0x0100
247 #define SOLVER_ERASE                    0x0200
248 #define SOLVER_UPDATE                   0x0300
249 #define SOLVER_WEAKENDEPS               0x0400
250 #define SOLVER_NOOBSOLETES              0x0500
251 #define SOLVER_LOCK                     0x0600
252 #define SOLVER_DISTUPGRADE              0x0700
253
254 #define SOLVER_JOBMASK                  0xff00
255
256 #define SOLVER_WEAK                     0x010000
257 #define SOLVER_ESSENTIAL                0x020000
258
259 /* old API compatibility, do not use in new code */
260 #if 1
261 #define SOLVER_INSTALL_SOLVABLE (SOLVER_INSTALL|SOLVER_SOLVABLE)
262 #define SOLVER_ERASE_SOLVABLE (SOLVER_ERASE|SOLVER_SOLVABLE)
263 #define SOLVER_INSTALL_SOLVABLE_NAME (SOLVER_INSTALL|SOLVER_SOLVABLE_NAME)
264 #define SOLVER_ERASE_SOLVABLE_NAME (SOLVER_ERASE|SOLVER_SOLVABLE_NAME)
265 #define SOLVER_INSTALL_SOLVABLE_PROVIDES (SOLVER_INSTALL|SOLVER_SOLVABLE_PROVIDES)
266 #define SOLVER_ERASE_SOLVABLE_PROVIDES (SOLVER_ERASE|SOLVER_SOLVABLE_PROVIDES)
267 #define SOLVER_INSTALL_SOLVABLE_UPDATE (SOLVER_UPDATE|SOLVER_SOLVABLE)
268 #define SOLVER_INSTALL_SOLVABLE_ONE_OF (SOLVER_INSTALL|SOLVER_SOLVABLE_ONE_OF)
269 #define SOLVER_WEAKEN_SOLVABLE_DEPS (SOLVER_WEAKENDEPS|SOLVER_SOLVABLE)
270 #define SOLVER_NOOBSOLETES_SOLVABLE (SOLVER_NOOBSOLETES|SOLVER_SOLVABLE)
271 #define SOLVER_NOOBSOLETES_SOLVABLE_NAME (SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_NAME)
272 #define SOLVER_NOOBSOLETES_SOLVABLE_PROVIDES (SOLVER_NOOBSOLETES|SOLVER_SOLVABLE_PROVIDES)
273 #endif
274
275 typedef enum {
276   SOLVER_PROBLEM_UPDATE_RULE,
277   SOLVER_PROBLEM_JOB_RULE,
278   SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP,
279   SOLVER_PROBLEM_NOT_INSTALLABLE,
280   SOLVER_PROBLEM_NOTHING_PROVIDES_DEP,
281   SOLVER_PROBLEM_SAME_NAME,
282   SOLVER_PROBLEM_PACKAGE_CONFLICT,
283   SOLVER_PROBLEM_PACKAGE_OBSOLETES,
284   SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE,
285   SOLVER_PROBLEM_SELF_CONFLICT,
286   SOLVER_PROBLEM_RPM_RULE,
287   SOLVER_PROBLEM_DISTUPGRADE_RULE,
288   SOLVER_PROBLEM_INFARCH_RULE
289 } SolverProbleminfo;
290
291 typedef enum {
292   SOLVER_RULE_UNKNOWN = 0,
293   SOLVER_RULE_RPM = 0x100,
294   SOLVER_RULE_RPM_NOT_INSTALLABLE,
295   SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP,
296   SOLVER_RULE_RPM_PACKAGE_REQUIRES,
297   SOLVER_RULE_RPM_SELF_CONFLICT,
298   SOLVER_RULE_RPM_PACKAGE_CONFLICT,
299   SOLVER_RULE_RPM_SAME_NAME,
300   SOLVER_RULE_RPM_PACKAGE_OBSOLETES,
301   SOLVER_RULE_RPM_IMPLICIT_OBSOLETES,
302   SOLVER_RULE_UPDATE = 0x200,
303   SOLVER_RULE_FEATURE = 0x300,
304   SOLVER_RULE_JOB = 0x400,
305   SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP,
306   SOLVER_RULE_DISTUPGRADE = 0x500,
307   SOLVER_RULE_INFARCH = 0x600,
308   SOLVER_RULE_LEARNT = 0x700
309 } SolverRuleinfo;
310
311 #define SOLVER_RULE_TYPEMASK    0xff00
312
313 /* backward compatibility */
314 #define SOLVER_PROBLEM_UPDATE_RULE              SOLVER_RULE_UPDATE
315 #define SOLVER_PROBLEM_JOB_RULE                 SOLVER_RULE_JOB
316 #define SOLVER_PROBLEM_JOB_NOTHING_PROVIDES_DEP SOLVER_RULE_JOB_NOTHING_PROVIDES_DEP
317 #define SOLVER_PROBLEM_NOT_INSTALLABLE          SOLVER_RULE_RPM_NOT_INSTALLABLE
318 #define SOLVER_PROBLEM_NOTHING_PROVIDES_DEP     SOLVER_RULE_RPM_NOTHING_PROVIDES_DEP
319 #define SOLVER_PROBLEM_SAME_NAME                SOLVER_RULE_RPM_SAME_NAME
320 #define SOLVER_PROBLEM_PACKAGE_CONFLICT         SOLVER_RULE_RPM_PACKAGE_CONFLICT
321 #define SOLVER_PROBLEM_PACKAGE_OBSOLETES        SOLVER_RULE_RPM_PACKAGE_OBSOLETES
322 #define SOLVER_PROBLEM_DEP_PROVIDERS_NOT_INSTALLABLE SOLVER_RULE_RPM_PACKAGE_REQUIRES
323 #define SOLVER_PROBLEM_SELF_CONFLICT            SOLVER_RULE_RPM_SELF_CONFLICT
324 #define SOLVER_PROBLEM_RPM_RULE                 SOLVER_RULE_RPM
325 #define SOLVER_PROBLEM_DISTUPGRADE_RULE         SOLVER_RULE_DISTUPGRADE
326 #define SOLVER_PROBLEM_INFARCH_RULE             SOLVER_RULE_INFARCH
327
328
329 #define SOLVER_SOLUTION_JOB             (0)
330 #define SOLVER_SOLUTION_DISTUPGRADE     (-1)
331 #define SOLVER_SOLUTION_INFARCH         (-2)
332
333 extern Solver *solver_create(Pool *pool);
334 extern void solver_free(Solver *solv);
335 extern void solver_solve(Solver *solv, Queue *job);
336
337 extern int solver_dep_installed(Solver *solv, Id dep);
338 extern int solver_splitprovides(Solver *solv, Id dep);
339
340 extern Id solver_problem_count(Solver *solv);
341 extern Id solver_next_problem(Solver *solv, Id problem);
342 extern Id solver_solution_count(Solver *solv, Id problem);
343 extern Id solver_next_solution(Solver *solv, Id problem, Id solution);
344 extern Id solver_solutionelement_count(Solver *solv, Id problem, Id solution);
345 extern Id solver_next_solutionelement(Solver *solv, Id problem, Id solution, Id element, Id *p, Id *rp);
346 extern Id solver_findproblemrule(Solver *solv, Id problem);
347 extern void solver_findallproblemrules(Solver *solv, Id problem, Queue *rules);
348
349 extern SolverRuleinfo solver_problemruleinfo(Solver *solv, Queue *job, Id rid, Id *depp, Id *sourcep, Id *targetp);
350 extern SolverRuleinfo solver_ruleinfo(Solver *solv, Id rid, Id *fromp, Id *top, Id *depp);
351 extern int solver_allruleinfos(Solver *solv, Id rid, Queue *rq);
352
353 /* XXX: why is this not static? */
354 Id *solver_create_decisions_obsoletesmap(Solver *solv);
355
356 static inline int
357 solver_dep_fulfilled(Solver *solv, Id dep)
358 {
359   Pool *pool = solv->pool;
360   Id p, pp;
361
362   if (ISRELDEP(dep))
363     {
364       Reldep *rd = GETRELDEP(pool, dep);
365       if (rd->flags == REL_AND)
366         {
367           if (!solver_dep_fulfilled(solv, rd->name))
368             return 0;
369           return solver_dep_fulfilled(solv, rd->evr);
370         }
371       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_SPLITPROVIDES)
372         return solver_splitprovides(solv, rd->evr);
373       if (rd->flags == REL_NAMESPACE && rd->name == NAMESPACE_INSTALLED)
374         return solver_dep_installed(solv, rd->evr);
375     }
376   FOR_PROVIDES(p, pp, dep)
377     {
378       if (solv->decisionmap[p] > 0)
379         return 1;
380     }
381   return 0;
382 }
383
384 static inline int
385 solver_is_supplementing(Solver *solv, Solvable *s)
386 {
387   Id sup, *supp;
388   if (!s->supplements)
389     return 0;
390   supp = s->repo->idarraydata + s->supplements;
391   while ((sup = *supp++) != 0)
392     if (solver_dep_fulfilled(solv, sup))
393       return 1;
394   return 0;
395 }
396
397 static inline int
398 solver_is_enhancing(Solver *solv, Solvable *s)
399 {
400   Id enh, *enhp;
401   if (!s->enhances)
402     return 0;
403   enhp = s->repo->idarraydata + s->enhances;
404   while ((enh = *enhp++) != 0)
405     if (solver_dep_fulfilled(solv, enh))
406       return 1;
407   return 0;
408 }
409
410 void solver_calc_duchanges(Solver *solv, DUChanges *mps, int nmps);
411 int solver_calc_installsizechange(Solver *solv);
412 void solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res);
413
414 void solver_find_involved(Solver *solv, Queue *installedq, Solvable *s, Queue *q);
415
416 static inline void
417 solver_create_state_maps(Solver *solv, Map *installedmap, Map *conflictsmap)
418 {
419   pool_create_state_maps(solv->pool, &solv->decisionq, installedmap, conflictsmap);
420 }
421
422 /* iterate over all literals of a rule */
423 /* WARNING: loop body must not relocate whatprovidesdata, e.g. by
424  * looking up the providers of a dependency */
425 #define FOR_RULELITERALS(l, dp, r)                              \
426     for (l = r->d < 0 ? -r->d - 1 : r->d,                       \
427          dp = !l ? &r->w2 : pool->whatprovidesdata + l,         \
428          l = r->p; l; l = (dp != &r->w2 + 1 ? *dp++ : 0))
429
430 /* iterate over all packages selected by a job */
431 #define FOR_JOB_SELECT(p, pp, select, what) \
432     for (pp = (select == SOLVER_SOLVABLE ? 0 :  \
433                select == SOLVER_SOLVABLE_ONE_OF ? what : \
434                pool_whatprovides(pool, what)),                          \
435          p = (select == SOLVER_SOLVABLE ? what : pool->whatprovidesdata[pp++]) ; p ; p = pool->whatprovidesdata[pp++]) \
436       if (select != SOLVER_SOLVABLE_NAME || pool_match_nevr(pool, pool->solvables + p, what))
437
438 #ifdef __cplusplus
439 }
440 #endif
441
442 #endif /* SATSOLVER_SOLVER_H */