plug weakrulemap memory map if the solver gets re-run
[platform/upstream/libsolv.git] / src / poolarch.c
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  * poolarch.c
10  *
11  * create architecture policies
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include "pool.h"
19 #include "poolid.h"
20 #include "poolarch.h"
21 #include "util.h"
22
23 static const char *archpolicies[] = {
24   "x86_64",     "x86_64:i686:i586:i486:i386",
25   "i686",       "i686:i586:i486:i386",
26   "i586",       "i586:i486:i386",
27   "i486",       "i486:i386",
28   "i386",       "i386",
29   "s390x",      "s390x:s390",
30   "s390",       "s390",
31   "ia64",       "ia64:i686:i586:i486:i386",
32   "ppc64",      "ppc64:ppc",
33   "ppc",        "ppc",
34   "ppc64p7",    "ppc64p7:ppc64:ppc",
35   "aarch64",    "aarch64",
36   "armv6hl",    "armv6hl",
37   "armv7hnl",   "armv7hnl:armv7hl:armv6hl",
38   "armv7hl",    "armv7hl:armv6hl",
39   "armv7l",     "armv7l:armv6l:armv5tejl:armv5tel:armv5l:armv4tl:armv4l:armv3l",
40   "armv6l",     "armv6l:armv5tejl:armv5tel:armv5l:armv4tl:armv4l:armv3l",
41   "armv5tejl",  "armv5tejl:armv5tel:armv5l:armv4tl:armv4l:armv3l",
42   "armv5tel",   "armv5tel:armv5l:armv4tl:armv4l:armv3l",
43   "armv5tl",    "armv5l:armv4tl:armv4l:armv3l",
44   "armv5l",     "armv5l:armv4tl:armv4l:armv3l",
45   "armv4tl",    "armv4tl:armv4l:armv3l",
46   "armv4l",     "armv4l:armv3l",
47   "armv3l",     "armv3l",
48   "sh3",        "sh3",
49   "sh4",        "sh4",
50   "sh4a",       "sh4a:sh4",
51   "sparc64v",   "sparc64v:sparc64:sparcv9v:sparcv9:sparcv8:sparc",
52   "sparc64",    "sparc64:sparcv9:sparcv8:sparc",
53   "sparcv9v",   "sparcv9v:sparcv9:sparcv8:sparc",
54   "sparcv9",    "sparcv9:sparcv8:sparc",
55   "sparcv8",    "sparcv8:sparc",
56   "sparc",      "sparc",
57   "mipsel",     "mipsel",
58   0
59 };
60
61 void
62 pool_setarch(Pool *pool, const char *arch)
63 {
64   if (arch)
65     {
66       int i;
67
68       /* convert arch to known policy */
69       for (i = 0; archpolicies[i]; i += 2)
70         if (!strcmp(archpolicies[i], arch))
71           break;
72       if (archpolicies[i])
73         arch = archpolicies[i + 1];
74       else
75         arch = "";
76     }
77   pool_setarchpolicy(pool, arch);
78 }
79
80 /*
81  * we support three relations:
82  *
83  * a = b   both architectures a and b are treated as equivalent
84  * a > b   a is considered a "better" architecture, the solver
85  *         should change from a to b, but must not change from b to a
86  * a : b   a is considered a "better" architecture, the solver
87  *         must not change the architecture from a to b or b to a
88  */
89 void
90 pool_setarchpolicy(Pool *pool, const char *arch)
91 {
92   unsigned int score = 0x10001;
93   size_t l;
94   char d;
95   Id *id2arch;
96   Id id, lastarch;
97
98   pool->id2arch = solv_free(pool->id2arch);
99   pool->id2color = solv_free(pool->id2color);
100   if (!arch)
101     {
102       pool->lastarch = 0;
103       return;
104     }
105   id = pool->noarchid;
106   lastarch = id + 255;
107   id2arch = solv_calloc(lastarch + 1, sizeof(Id));
108   id2arch[id] = 1;      /* the "noarch" class */
109
110   d = 0;
111   while (*arch)
112     {
113       l = strcspn(arch, ":=>");
114       if (l)
115         {
116           id = pool_strn2id(pool, arch, l, 1);
117           if (id > lastarch)
118             {
119               id2arch = solv_realloc2(id2arch, (id + 255 + 1), sizeof(Id));
120               memset(id2arch + lastarch + 1, 0, (id + 255 - lastarch) * sizeof(Id));
121               lastarch = id + 255;
122             }
123           if (id2arch[id] == 0)
124             {
125               if (d == ':')
126                 score += 0x10000;
127               else if (d == '>')
128                 score += 0x00001;
129               id2arch[id] = score;
130             }
131         }
132       arch += l;
133       if ((d = *arch++) == 0)
134         break;
135     }
136   pool->id2arch = id2arch;
137   pool->lastarch = lastarch;
138 }
139
140 unsigned char
141 pool_arch2color_slow(Pool *pool, Id arch)
142 {
143   const char *s;
144   unsigned char color;
145
146   if (arch > pool->lastarch)
147     return ARCHCOLOR_ALL;
148   if (!pool->id2color)
149     pool->id2color = solv_calloc(pool->lastarch + 1, 1);
150   s = pool_id2str(pool, arch);
151   if (arch == ARCH_NOARCH || arch == ARCH_ALL || arch == ARCH_ANY)
152     color = ARCHCOLOR_ALL;
153   else if (!strcmp(s, "s390x") || strstr(s, "64"))
154     color = ARCHCOLOR_64;
155   else
156     color = ARCHCOLOR_32;
157   pool->id2color[arch] = color;
158   return color;
159 }
160