]> git.tdb.fi Git - builder.git/blob - source/architecture.cpp
Improve architecture name matching
[builder.git] / source / architecture.cpp
1 #include <limits>
2 #include <msp/strings/format.h>
3 #include <msp/strings/utils.h>
4 #include "architecture.h"
5 #include "builder.h"
6 #include "executable.h"
7 #include "importlibrary.h"
8 #include "objectfile.h"
9 #include "sharedlibrary.h"
10 #include "staticlibrary.h"
11 #include "sysutils.h"
12
13 using namespace std;
14 using namespace Msp;
15
16 namespace {
17
18 const char *types[] =
19 {
20         "x86",
21         "arm",
22         "ppc",
23         0
24 };
25
26 const char *cpus[] =
27 {
28         "i386",       "x86",
29         "i486",       "x86",
30         "pentium",    "x86",
31         "pentiumpro", "x86",
32         "pentium2",   "x86",
33         "pentium3",   "x86",
34         "pentium4",   "x86",
35         "core2",      "x86",
36         "nehalem",    "x86",
37         "k6",         "x86",
38         "athlon",     "x86",
39         "athlonxp",   "x86",
40         "athlon64",   "x86",
41         "armv5",      "arm",
42         "armv6",      "arm",
43         "armv7",      "arm",
44         "armv7a",     "arm",
45         0
46 };
47
48 const char *fpus[] =
49 {
50         "387",   "x86",
51         "sse",   "x86",
52         "sse3",  "x86",
53         "sse4.1", "x86",
54         "vfpv3", "arm",
55         "neon",  "arm",
56         0
57 };
58
59 const char *systems[] =
60 {
61         "linux",
62         "freebsd",
63         "darwin",
64         "windows",
65         "android",
66         0
67 };
68
69 const char *toolchains[] =
70 {
71         "gnu",
72         "clang",
73         "msvc",
74         0
75 };
76
77 const char *aliases[] =
78 {
79         "pc",              "x86",
80         "x86_64",          "x86-64",
81         "x64",             "x86-64",
82         "amd64",           "x86-64",
83         "i586",            "pentium",
84         "i686",            "pentiumpro",
85         "corei7",          "nehalem",
86         "win32",           "windows-32",
87         "win64",           "windows-64",
88         "power macintosh", "ppc",
89         "armeabi",         "arm",
90         "v7a",             "armv7a",
91         "gcc",             "gnu",
92         "mingw",           "windows-gnu",
93         0
94 };
95
96 }
97
98 Architecture::Architecture(Builder &b, const string &spec):
99         builder(b),
100         bits(0),
101         native(false)
102 {
103         if(spec.empty())
104         {
105                 parse_specification(get_system_type());
106                 // We really only want to set type for the default arch
107                 cpu.clear();
108                 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
109                 native = true;
110         }
111         else
112         {
113                 parse_specification(spec);
114                 const Architecture &native_arch = builder.get_native_arch();
115                 if(type.empty())
116                         type = native_arch.type;
117                 if(system.empty())
118                         system = native_arch.system;
119                 if(!bits)
120                 {
121                         if(type==native_arch.type)
122                                 bits = native_arch.bits;
123                         else
124                                 bits = 32;
125                 }
126
127                 if(type!=native_arch.type || system!=native_arch.system)
128                         cross_prefix = format("%s-%s", type, system);
129                 else if(bits==native_arch.bits)
130                         native = true;
131         }
132
133         if(toolchain.empty())
134         {
135                 if((system=="darwin" || system=="freebsd") && builder.get_vfs().find_binary("clang"))
136                         toolchain = "clang";
137                 else if(system=="windows" && native)
138                         toolchain = "msvc";
139                 else
140                         toolchain = "gnu";
141         }
142
143         name = type;
144         if(!cpu.empty())
145                 name += format("-%s", cpu);
146         if(!fpu.empty())
147                 name += format("-%s", fpu);
148         name += format("-%d-%s-%s", bits, system, toolchain);
149
150         if(system=="windows")
151         {
152                 add_pattern<SharedLibrary>("%.dll");
153                 if(toolchain=="msvc")
154                 {
155                         add_pattern<ObjectFile>("%.obj");
156                         add_pattern<ImportLibrary>("%.lib");
157                         add_pattern<StaticLibrary>("%_static.lib");
158                 }
159                 else
160                 {
161                         add_pattern<ObjectFile>("%.o");
162                         add_pattern<SharedLibrary>("lib%.dll");
163                         add_pattern<ImportLibrary>("lib%.dll.a");
164                         add_pattern<StaticLibrary>("lib%.a");
165                 }
166                 add_pattern<Executable>("%.exe");
167         }
168         else
169         {
170                 add_pattern<ObjectFile>("%.o");
171                 if(system=="darwin")
172                         add_pattern<SharedLibrary>("lib%.dylib");
173                 else
174                         add_pattern<SharedLibrary>("lib%.so");
175                 add_pattern<StaticLibrary>("lib%.a");
176                 add_pattern<Executable>("%");
177         }
178 }
179
180 bool Architecture::match_name(const string &pattern, unsigned *quality) const
181 {
182         bool negate = (pattern[0]=='!');
183         vector<string> parts = split(pattern.substr(negate), "-");
184         resolve_aliases(parts);
185         for(const string &p: parts)
186         {
187                 if((p=="32" && bits==32) || (p=="64" && bits==64))
188                         ;
189                 else if(p!=type && p!=cpu && p!=fpu && p!=system && p!=toolchain)
190                 {
191                         if(quality)
192                                 *quality = 0;
193                         return negate;
194                 }
195         }
196
197         if(quality)
198                 *quality = parts.size();
199         return !negate;
200 }
201
202 string Architecture::best_match(const vector<string> &names) const
203 {
204         string best;
205         unsigned best_quality = 0;
206         for(const string &n: names)
207         {
208                 unsigned quality;
209                 if(match_name(n, &quality))
210                         if(quality>best_quality)
211                         {
212                                 best = n;
213                                 best_quality = quality;
214                         }
215         }
216
217         return best;
218 }
219
220 template<typename T>
221 void Architecture::add_pattern(const string &pat)
222 {
223         filename_patterns[typeid(T).name()].push_back(Pattern(pat));
224 }
225
226 void Architecture::resolve_aliases(vector<string> &parts)
227 {
228         for(unsigned i=0; i<parts.size(); ++i)
229         {
230                 const string &part = parts[i];
231                 const char *replace = 0;
232                 for(unsigned j=0; (!replace && aliases[j]); j+=2)
233                         if(part==aliases[j])
234                                 replace = aliases[j+1];
235
236                 if(replace)
237                 {
238                         bool has_dash = false;
239                         for(const char *c=replace; (!has_dash && *c); ++c)
240                                 has_dash = (*c=='-');
241
242                         if(has_dash)
243                         {
244                                 vector<string> rparts = split(replace, "-");
245                                 parts[i] = rparts[0];
246                                 parts.insert(parts.begin()+i+1, rparts.begin()+1, rparts.end());
247                                 i += rparts.size()-1;
248                         }
249                         else
250                                 parts[i] = replace;
251                 }
252         }
253 }
254
255 void Architecture::parse_specification(const string &spec)
256 {
257         vector<string> parts = split(spec, "-");
258         resolve_aliases(parts);
259         for(const string &p: parts)
260         {
261                 bool ok = false;
262
263                 for(unsigned j=0; (!ok && types[j]); ++j)
264                         if(p==types[j])
265                         {
266                                 if(!type.empty() && p!=type)
267                                         throw invalid_argument("Conflicting type specification");
268                                 type = p;
269                                 ok = true;
270                         }
271
272                 for(unsigned j=0; (!ok && cpus[j]); j+=2)
273                         if(p==cpus[j])
274                         {
275                                 if(type.empty())
276                                         type = cpus[j+1];
277                                 else if(cpus[j+1]!=type)
278                                         throw invalid_argument("Conflicting CPU specification");
279                                 cpu = p;
280                                 ok = true;
281                         }
282
283                 for(unsigned j=0; (!ok && fpus[j]); j+=2)
284                         if(p==fpus[j])
285                         {
286                                 if(fpus[j+1]!=type)
287                                         throw invalid_argument("Conflicting FPU specification");
288                                 fpu = p;
289                                 ok = true;
290                         }
291
292                 for(unsigned j=0; (!ok && systems[j]); ++j)
293                         if(p==systems[j])
294                         {
295                                 system = p;
296                                 ok = true;
297                         }
298
299                 for(unsigned j=0; (!ok && toolchains[j]); ++j)
300                         if(p==toolchains[j])
301                         {
302                                 toolchain = p;
303                                 ok = true;
304                         }
305
306                 if(!ok && (p=="32" || p=="64"))
307                 {
308                         bits = lexical_cast<unsigned>(p);
309                         ok = true;
310                 }
311
312                 if(!ok)
313                         throw invalid_argument("Unrecognized part in arch specification: "+p);
314         }
315 }
316
317
318 Architecture::Loader::Loader(Architecture &a):
319         DataFile::ObjectLoader<Architecture>(a)
320 {
321         add("prefix", &Architecture::cross_prefix);
322 }