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