]> git.tdb.fi Git - builder.git/blob - source/architecture.cpp
Allow architecture specifications to be refined after creation
[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 {
101         if(spec.empty())
102         {
103                 parse_specification(get_system_type());
104                 // We really only want to set type for the default arch
105                 cpu.clear();
106                 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
107                 native = true;
108         }
109         else
110         {
111                 parse_specification(spec);
112                 const Architecture &native_arch = builder.get_native_arch();
113                 if(type.empty())
114                         type = native_arch.type;
115                 if(system.empty())
116                         system = native_arch.system;
117                 if(!bits)
118                 {
119                         if(type==native_arch.type)
120                                 bits = native_arch.bits;
121                         else
122                                 bits = 32;
123                 }
124
125                 if(type!=native_arch.type || system!=native_arch.system)
126                         cross_prefix = format("%s-%s", type, system);
127                 else if(bits==native_arch.bits)
128                         native = true;
129         }
130
131         if(toolchain.empty())
132         {
133                 if((system=="darwin" || system=="freebsd") && builder.get_vfs().find_binary("clang"))
134                         toolchain = "clang";
135                 else if(system=="windows" && native)
136                         toolchain = "msvc";
137                 else
138                         toolchain = "gnu";
139         }
140
141         update();
142 }
143
144 void Architecture::refine(const string &spec)
145 {
146         parse_specification(spec);
147         update();
148 }
149
150 void Architecture::update()
151 {
152         name = type;
153         if(!cpu.empty())
154                 name += format("-%s", cpu);
155         if(!fpu.empty())
156                 name += format("-%s", fpu);
157         name += format("-%d-%s-%s", bits, system, toolchain);
158
159         filename_patterns.clear();
160         if(system=="windows")
161         {
162                 add_pattern<SharedLibrary>("%.dll");
163                 if(toolchain=="msvc")
164                 {
165                         add_pattern<ObjectFile>("%.obj");
166                         add_pattern<ImportLibrary>("%.lib");
167                         add_pattern<StaticLibrary>("%_static.lib");
168                 }
169                 else
170                 {
171                         add_pattern<ObjectFile>("%.o");
172                         add_pattern<SharedLibrary>("lib%.dll");
173                         add_pattern<ImportLibrary>("lib%.dll.a");
174                         add_pattern<StaticLibrary>("lib%.a");
175                 }
176                 add_pattern<Executable>("%.exe");
177         }
178         else
179         {
180                 add_pattern<ObjectFile>("%.o");
181                 if(system=="darwin")
182                         add_pattern<SharedLibrary>("lib%.dylib");
183                 else
184                         add_pattern<SharedLibrary>("lib%.so");
185                 add_pattern<StaticLibrary>("lib%.a");
186                 add_pattern<Executable>("%");
187         }
188 }
189
190 bool Architecture::match_name(const string &pattern, unsigned *quality) const
191 {
192         bool negate = (pattern[0]=='!');
193         vector<string> parts = split(pattern.substr(negate), "-");
194         resolve_aliases(parts);
195         for(const string &p: parts)
196         {
197                 if((p=="32" && bits==32) || (p=="64" && bits==64))
198                         ;
199                 else if(p!=type && p!=cpu && p!=fpu && p!=system && p!=toolchain)
200                 {
201                         if(quality)
202                                 *quality = 0;
203                         return negate;
204                 }
205         }
206
207         if(quality)
208                 *quality = parts.size();
209         return !negate;
210 }
211
212 string Architecture::best_match(const vector<string> &names) const
213 {
214         string best;
215         unsigned best_quality = 0;
216         for(const string &n: names)
217         {
218                 unsigned quality;
219                 if(match_name(n, &quality))
220                         if(quality>best_quality)
221                         {
222                                 best = n;
223                                 best_quality = quality;
224                         }
225         }
226
227         return best;
228 }
229
230 template<typename T>
231 void Architecture::add_pattern(const string &pat)
232 {
233         filename_patterns[typeid(T).name()].push_back(Pattern(pat));
234 }
235
236 void Architecture::resolve_aliases(vector<string> &parts)
237 {
238         for(unsigned i=0; i<parts.size(); ++i)
239         {
240                 const string &part = parts[i];
241                 const char *replace = 0;
242                 for(unsigned j=0; (!replace && aliases[j]); j+=2)
243                         if(part==aliases[j])
244                                 replace = aliases[j+1];
245
246                 if(replace)
247                 {
248                         bool has_dash = false;
249                         for(const char *c=replace; (!has_dash && *c); ++c)
250                                 has_dash = (*c=='-');
251
252                         if(has_dash)
253                         {
254                                 vector<string> rparts = split(replace, "-");
255                                 parts[i] = rparts[0];
256                                 parts.insert(parts.begin()+i+1, rparts.begin()+1, rparts.end());
257                                 i += rparts.size()-1;
258                         }
259                         else
260                                 parts[i] = replace;
261                 }
262         }
263 }
264
265 void Architecture::parse_specification(const string &spec)
266 {
267         vector<string> parts = split(spec, "-");
268         resolve_aliases(parts);
269         for(const string &p: parts)
270         {
271                 bool ok = false;
272
273                 for(unsigned j=0; (!ok && types[j]); ++j)
274                         if(p==types[j])
275                         {
276                                 if(!type.empty() && p!=type)
277                                         throw invalid_argument("Conflicting type specification");
278                                 type = p;
279                                 ok = true;
280                         }
281
282                 for(unsigned j=0; (!ok && cpus[j]); j+=2)
283                         if(p==cpus[j])
284                         {
285                                 if(type.empty())
286                                         type = cpus[j+1];
287                                 else if(cpus[j+1]!=type)
288                                         throw invalid_argument("Conflicting CPU specification");
289                                 cpu = p;
290                                 ok = true;
291                         }
292
293                 for(unsigned j=0; (!ok && fpus[j]); j+=2)
294                         if(p==fpus[j])
295                         {
296                                 if(fpus[j+1]!=type)
297                                         throw invalid_argument("Conflicting FPU specification");
298                                 fpu = p;
299                                 ok = true;
300                         }
301
302                 for(unsigned j=0; (!ok && systems[j]); ++j)
303                         if(p==systems[j])
304                         {
305                                 system = p;
306                                 ok = true;
307                         }
308
309                 for(unsigned j=0; (!ok && toolchains[j]); ++j)
310                         if(p==toolchains[j])
311                         {
312                                 toolchain = p;
313                                 ok = true;
314                         }
315
316                 if(!ok && (p=="32" || p=="64"))
317                 {
318                         bits = lexical_cast<unsigned>(p);
319                         ok = true;
320                 }
321
322                 if(!ok)
323                         throw invalid_argument("Unrecognized part in arch specification: "+p);
324         }
325 }
326
327
328 Architecture::Loader::Loader(Architecture &a):
329         DataFile::ObjectLoader<Architecture>(a)
330 {
331         add("prefix", &Architecture::cross_prefix);
332 }