]> git.tdb.fi Git - builder.git/blob - source/architecture.cpp
Use default member initializers and constructor delegation
[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         name = type;
142         if(!cpu.empty())
143                 name += format("-%s", cpu);
144         if(!fpu.empty())
145                 name += format("-%s", fpu);
146         name += format("-%d-%s-%s", bits, system, toolchain);
147
148         if(system=="windows")
149         {
150                 add_pattern<SharedLibrary>("%.dll");
151                 if(toolchain=="msvc")
152                 {
153                         add_pattern<ObjectFile>("%.obj");
154                         add_pattern<ImportLibrary>("%.lib");
155                         add_pattern<StaticLibrary>("%_static.lib");
156                 }
157                 else
158                 {
159                         add_pattern<ObjectFile>("%.o");
160                         add_pattern<SharedLibrary>("lib%.dll");
161                         add_pattern<ImportLibrary>("lib%.dll.a");
162                         add_pattern<StaticLibrary>("lib%.a");
163                 }
164                 add_pattern<Executable>("%.exe");
165         }
166         else
167         {
168                 add_pattern<ObjectFile>("%.o");
169                 if(system=="darwin")
170                         add_pattern<SharedLibrary>("lib%.dylib");
171                 else
172                         add_pattern<SharedLibrary>("lib%.so");
173                 add_pattern<StaticLibrary>("lib%.a");
174                 add_pattern<Executable>("%");
175         }
176 }
177
178 bool Architecture::match_name(const string &pattern, unsigned *quality) const
179 {
180         bool negate = (pattern[0]=='!');
181         vector<string> parts = split(pattern.substr(negate), "-");
182         resolve_aliases(parts);
183         for(const string &p: parts)
184         {
185                 if((p=="32" && bits==32) || (p=="64" && bits==64))
186                         ;
187                 else if(p!=type && p!=cpu && p!=fpu && p!=system && p!=toolchain)
188                 {
189                         if(quality)
190                                 *quality = 0;
191                         return negate;
192                 }
193         }
194
195         if(quality)
196                 *quality = parts.size();
197         return !negate;
198 }
199
200 string Architecture::best_match(const vector<string> &names) const
201 {
202         string best;
203         unsigned best_quality = 0;
204         for(const string &n: names)
205         {
206                 unsigned quality;
207                 if(match_name(n, &quality))
208                         if(quality>best_quality)
209                         {
210                                 best = n;
211                                 best_quality = quality;
212                         }
213         }
214
215         return best;
216 }
217
218 template<typename T>
219 void Architecture::add_pattern(const string &pat)
220 {
221         filename_patterns[typeid(T).name()].push_back(Pattern(pat));
222 }
223
224 void Architecture::resolve_aliases(vector<string> &parts)
225 {
226         for(unsigned i=0; i<parts.size(); ++i)
227         {
228                 const string &part = parts[i];
229                 const char *replace = 0;
230                 for(unsigned j=0; (!replace && aliases[j]); j+=2)
231                         if(part==aliases[j])
232                                 replace = aliases[j+1];
233
234                 if(replace)
235                 {
236                         bool has_dash = false;
237                         for(const char *c=replace; (!has_dash && *c); ++c)
238                                 has_dash = (*c=='-');
239
240                         if(has_dash)
241                         {
242                                 vector<string> rparts = split(replace, "-");
243                                 parts[i] = rparts[0];
244                                 parts.insert(parts.begin()+i+1, rparts.begin()+1, rparts.end());
245                                 i += rparts.size()-1;
246                         }
247                         else
248                                 parts[i] = replace;
249                 }
250         }
251 }
252
253 void Architecture::parse_specification(const string &spec)
254 {
255         vector<string> parts = split(spec, "-");
256         resolve_aliases(parts);
257         for(const string &p: parts)
258         {
259                 bool ok = false;
260
261                 for(unsigned j=0; (!ok && types[j]); ++j)
262                         if(p==types[j])
263                         {
264                                 if(!type.empty() && p!=type)
265                                         throw invalid_argument("Conflicting type specification");
266                                 type = p;
267                                 ok = true;
268                         }
269
270                 for(unsigned j=0; (!ok && cpus[j]); j+=2)
271                         if(p==cpus[j])
272                         {
273                                 if(type.empty())
274                                         type = cpus[j+1];
275                                 else if(cpus[j+1]!=type)
276                                         throw invalid_argument("Conflicting CPU specification");
277                                 cpu = p;
278                                 ok = true;
279                         }
280
281                 for(unsigned j=0; (!ok && fpus[j]); j+=2)
282                         if(p==fpus[j])
283                         {
284                                 if(fpus[j+1]!=type)
285                                         throw invalid_argument("Conflicting FPU specification");
286                                 fpu = p;
287                                 ok = true;
288                         }
289
290                 for(unsigned j=0; (!ok && systems[j]); ++j)
291                         if(p==systems[j])
292                         {
293                                 system = p;
294                                 ok = true;
295                         }
296
297                 for(unsigned j=0; (!ok && toolchains[j]); ++j)
298                         if(p==toolchains[j])
299                         {
300                                 toolchain = p;
301                                 ok = true;
302                         }
303
304                 if(!ok && (p=="32" || p=="64"))
305                 {
306                         bits = lexical_cast<unsigned>(p);
307                         ok = true;
308                 }
309
310                 if(!ok)
311                         throw invalid_argument("Unrecognized part in arch specification: "+p);
312         }
313 }
314
315
316 Architecture::Loader::Loader(Architecture &a):
317         DataFile::ObjectLoader<Architecture>(a)
318 {
319         add("prefix", &Architecture::cross_prefix);
320 }