Merge pull request #39 from andreas-schwab/master
[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   "ppc64le",    "ppc64le",
36   "aarch64",    "aarch64",
37   "armv6hl",    "armv6hl",
38   "armv7hnl",   "armv7hnl:armv7hl:armv6hl",
39   "armv7hl",    "armv7hl:armv6hl",
40   "armv7l",     "armv7l:armv6l:armv5tejl:armv5tel:armv5l:armv4tl:armv4l:armv3l",
41   "armv6l",     "armv6l:armv5tejl:armv5tel:armv5l:armv4tl:armv4l:armv3l",
42   "armv5tejl",  "armv5tejl:armv5tel:armv5l:armv4tl:armv4l:armv3l",
43   "armv5tel",   "armv5tel:armv5l:armv4tl:armv4l:armv3l",
44   "armv5tl",    "armv5l:armv4tl:armv4l:armv3l",
45   "armv5l",     "armv5l:armv4tl:armv4l:armv3l",
46   "armv4tl",    "armv4tl:armv4l:armv3l",
47   "armv4l",     "armv4l:armv3l",
48   "armv3l",     "armv3l",
49   "sh3",        "sh3",
50   "sh4",        "sh4",
51   "sh4a",       "sh4a:sh4",
52   "sparc64v",   "sparc64v:sparc64:sparcv9v:sparcv9:sparcv8:sparc",
53   "sparc64",    "sparc64:sparcv9:sparcv8:sparc",
54   "sparcv9v",   "sparcv9v:sparcv9:sparcv8:sparc",
55   "sparcv9",    "sparcv9:sparcv8:sparc",
56   "sparcv8",    "sparcv8:sparc",
57   "sparc",      "sparc",
58   "mipsel",     "mipsel",
59   "m68k",       "m68k",
60   0
61 };
62
63 void
64 pool_setarch(Pool *pool, const char *arch)
65 {
66   if (arch)
67     {
68       int i;
69
70       /* convert arch to known policy */
71       for (i = 0; archpolicies[i]; i += 2)
72         if (!strcmp(archpolicies[i], arch))
73           break;
74       if (archpolicies[i])
75         arch = archpolicies[i + 1];
76       else
77         arch = "";
78     }
79   pool_setarchpolicy(pool, arch);
80 }
81
82 /*
83  * we support three relations:
84  *
85  * a = b   both architectures a and b are treated as equivalent
86  * a > b   a is considered a "better" architecture, the solver
87  *         should change from a to b, but must not change from b to a
88  * a : b   a is considered a "better" architecture, the solver
89  *         must not change the architecture from a to b or b to a
90  */
91 void
92 pool_setarchpolicy(Pool *pool, const char *arch)
93 {
94   unsigned int score = 0x10001;
95   size_t l;
96   char d;
97   Id *id2arch;
98   Id id, lastarch;
99
100   pool->id2arch = solv_free(pool->id2arch);
101   pool->id2color = solv_free(pool->id2color);
102   if (!arch)
103     {
104       pool->lastarch = 0;
105       return;
106     }
107   id = pool->noarchid;
108   lastarch = id + 255;
109   id2arch = solv_calloc(lastarch + 1, sizeof(Id));
110   id2arch[id] = 1;      /* the "noarch" class */
111
112   d = 0;
113   while (*arch)
114     {
115       l = strcspn(arch, ":=>");
116       if (l)
117         {
118           id = pool_strn2id(pool, arch, l, 1);
119           if (id > lastarch)
120             {
121               id2arch = solv_realloc2(id2arch, (id + 255 + 1), sizeof(Id));
122               memset(id2arch + lastarch + 1, 0, (id + 255 - lastarch) * sizeof(Id));
123               lastarch = id + 255;
124             }
125           if (id2arch[id] == 0)
126             {
127               if (d == ':')
128                 score += 0x10000;
129               else if (d == '>')
130                 score += 0x00001;
131               id2arch[id] = score;
132             }
133         }
134       arch += l;
135       if ((d = *arch++) == 0)
136         break;
137     }
138   pool->id2arch = id2arch;
139   pool->lastarch = lastarch;
140 }
141
142 unsigned char
143 pool_arch2color_slow(Pool *pool, Id arch)
144 {
145   const char *s;
146   unsigned char color;
147
148   if (arch > pool->lastarch)
149     return ARCHCOLOR_ALL;
150   if (!pool->id2color)
151     pool->id2color = solv_calloc(pool->lastarch + 1, 1);
152   s = pool_id2str(pool, arch);
153   if (arch == ARCH_NOARCH || arch == ARCH_ALL || arch == ARCH_ANY)
154     color = ARCHCOLOR_ALL;
155   else if (!strcmp(s, "s390x") || strstr(s, "64"))
156     color = ARCHCOLOR_64;
157   else
158     color = ARCHCOLOR_32;
159   pool->id2color[arch] = color;
160   return color;
161 }
162