Imported Upstream version 0.7.27
[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 #if defined(FEDORA) || defined(MAGEIA)
25   "x86_64_v4",  "x86_64_v4:x86_64_v3:x86_64_v2:x86_64:athlon:i686:i586:i486:i386",
26   "x86_64_v3",  "x86_64_v3:x86_64_v2:x86_64:athlon:i686:i586:i486:i386",
27   "x86_64_v2",  "x86_64_v2:x86_64:athlon:i686:i586:i486:i386",
28   "x86_64",     "x86_64:athlon:i686:i586:i486:i386",
29 #else
30   "x86_64_v4",  "x86_64_v4:x86_64_v3:x86_64_v2:x86_64:i686:i586:i486:i386",
31   "x86_64_v3",  "x86_64_v3:x86_64_v2:x86_64:i686:i586:i486:i386",
32   "x86_64_v2",  "x86_64_v2:x86_64:i686:i586:i486:i386",
33   "x86_64",     "x86_64:i686:i586:i486:i386",
34 #endif
35   "i686",       "i686:i586:i486:i386",
36   "i586",       "i586:i486:i386",
37   "i486",       "i486:i386",
38   "s390x",      "s390x:s390",
39   "ppc64",      "ppc64:ppc",
40   "ppc64p7",    "ppc64p7:ppc64:ppc",
41   "ia64",       "ia64:i686:i586:i486:i386",
42   "armv8hcnl",  "armv8hcnl:armv8hnl:armv8hl:armv7hnl:armv7hl:armv6hl",
43   "armv8hnl",   "armv8hnl:armv8hl:armv7hnl:armv7hl:armv6hl",
44   "armv8hl",    "armv8hl:armv7hl:armv6hl",
45   "armv8l",     "armv8l:armv7l:armv6l:armv5tejl:armv5tel:armv5tl:armv5l:armv4tl:armv4l:armv3l",
46   "armv7hnl",   "armv7hnl:armv7hl:armv6hl",
47   "armv7hl",    "armv7hl:armv6hl",
48   "armv7l",     "armv7l:armv6l:armv5tejl:armv5tel:armv5tl:armv5l:armv4tl:armv4l:armv3l",
49   "armv6l",     "armv6l:armv5tejl:armv5tel:armv5tl:armv5l:armv4tl:armv4l:armv3l",
50   "armv5tejl",  "armv5tejl:armv5tel:armv5tl:armv5l:armv4tl:armv4l:armv3l",
51   "armv5tel",   "armv5tel:armv5tl:armv5l:armv4tl:armv4l:armv3l",
52   "armv5tl",    "armv5tl:armv5l:armv4tl:armv4l:armv3l",
53   "armv5l",     "armv5l:armv4tl:armv4l:armv3l",
54   "armv4tl",    "armv4tl:armv4l:armv3l",
55   "armv4l",     "armv4l:armv3l",
56   "sh4a",       "sh4a:sh4",
57   "sparc64v",   "sparc64v:sparc64:sparcv9v:sparcv9:sparcv8:sparc",
58   "sparc64",    "sparc64:sparcv9:sparcv8:sparc",
59   "sparcv9v",   "sparcv9v:sparcv9:sparcv8:sparc",
60   "sparcv9",    "sparcv9:sparcv8:sparc",
61   "sparcv8",    "sparcv8:sparc",
62 #if defined(FEDORA) || defined(MAGEIA)
63   "ia32e",      "ia32e:x86_64:athlon:i686:i586:i486:i386",
64   "athlon",     "athlon:i686:i586:i486:i386",
65   "amd64",      "amd64:x86_64:athlon:i686:i586:i486:i386",
66   "geode",      "geode:i586:i486:i386",
67   "ppc64iseries", "ppc64iseries:ppc64:ppc",
68   "ppc64pseries", "ppc64pseries:ppc64:ppc",
69 #endif
70   0
71 };
72
73 void
74 pool_setarch(Pool *pool, const char *arch)
75 {
76   if (arch)
77     {
78       int i;
79       /* convert arch to known policy */
80       for (i = 0; archpolicies[i]; i += 2)
81         if (!strcmp(archpolicies[i], arch))
82           {
83             arch = archpolicies[i + 1];
84             break;
85           }
86     }
87   pool_setarchpolicy(pool, arch);
88 }
89
90 /*
91  * we support three relations:
92  *
93  * a = b   both architectures a and b are treated as equivalent
94  * a > b   a is considered a "better" architecture, the solver
95  *         should change from a to b, but must not change from b to a
96  * a : b   a is considered a "better" architecture, the solver
97  *         must not change the architecture from a to b or b to a
98  */
99 void
100 pool_setarchpolicy(Pool *pool, const char *arch)
101 {
102   unsigned int score = 0x10001;
103   size_t l;
104   char d;
105   Id *id2arch;
106   Id id, lastarch;
107
108   pool->id2arch = solv_free(pool->id2arch);
109   pool->id2color = solv_free(pool->id2color);
110   if (!arch)
111     {
112       pool->lastarch = 0;
113       return;
114     }
115   id = pool->noarchid;
116   lastarch = id + 255;
117   /* note that we overallocate one element to be compatible with
118    * old versions that accessed id2arch[lastarch].
119    * id2arch[lastarch] will always be zero */
120   id2arch = solv_calloc(lastarch + 1, sizeof(Id));
121   id2arch[id] = 1;      /* the "noarch" class */
122
123   d = 0;
124   while (*arch)
125     {
126       l = strcspn(arch, ":=>");
127       if (l)
128         {
129           id = pool_strn2id(pool, arch, l, 1);
130           if (id >= lastarch)
131             {
132               id2arch = solv_realloc2(id2arch, (id + 255 + 1), sizeof(Id));
133               memset(id2arch + lastarch + 1, 0, (id + 255 - lastarch) * sizeof(Id));
134               lastarch = id + 255;
135             }
136           if (id2arch[id] == 0)
137             {
138               if (d == ':')
139                 score += 0x10000;
140               else if (d == '>')
141                 score += 0x00001;
142               id2arch[id] = score;
143             }
144         }
145       arch += l;
146       if ((d = *arch++) == 0)
147         break;
148     }
149   pool->id2arch = id2arch;
150   pool->lastarch = lastarch;
151 }
152
153 unsigned char
154 pool_arch2color_slow(Pool *pool, Id arch)
155 {
156   const char *s;
157   unsigned char color;
158
159   if ((unsigned int)arch >= (unsigned int)pool->lastarch)
160     return ARCHCOLOR_ALL;
161   if (!pool->id2color)
162     pool->id2color = solv_calloc(pool->lastarch + 1, 1);
163   s = pool_id2str(pool, arch);
164   if (arch == ARCH_NOARCH || arch == ARCH_ALL || arch == ARCH_ANY)
165     color = ARCHCOLOR_ALL;
166   else if (!strcmp(s, "s390x") || strstr(s, "64"))
167     color = ARCHCOLOR_64;
168   else
169     color = ARCHCOLOR_32;
170   pool->id2color[arch] = color;
171   return color;
172 }
173