]> git.tdb.fi Git - builder.git/blob - source/architecture.cpp
Add support for SSE3 and SSE4.1 FPUs
[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) const
181 {
182         bool negate = (pattern[0]=='!');
183         vector<string> parts = split(pattern.substr(negate), "-");
184         resolve_aliases(parts);
185         for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
186         {
187                 if((*i=="32" && bits==32) || (*i=="64" && bits==64))
188                         ;
189                 else if(*i!=type && *i!=cpu && *i!=fpu && *i!=system && *i!=toolchain)
190                         return negate;
191         }
192         return !negate;
193 }
194
195 string Architecture::best_match(const vector<string> &names) const
196 {
197         string best;
198         unsigned best_size = 0;
199         for(vector<string>::const_iterator i=names.begin(); i!=names.end(); ++i)
200                 if(match_name(*i))
201                 {
202                         /* TODO Do full parse and alias resolution here?  Otherwise x86 and
203                         x86_64 are treated as equally good, even though the latter is more
204                         specific. */
205                         unsigned size = 1;
206                         for(string::const_iterator j=i->begin(); j!=i->end(); ++j)
207                                 size += (*j=='-');
208
209                         if(size>best_size)
210                         {
211                                 best = *i;
212                                 best_size = size;
213                         }
214                 }
215
216         return best;
217 }
218
219 template<typename T>
220 void Architecture::add_pattern(const string &pat)
221 {
222         filename_patterns[typeid(T).name()].push_back(Pattern(pat));
223 }
224
225 void Architecture::resolve_aliases(vector<string> &parts)
226 {
227         for(unsigned i=0; i<parts.size(); ++i)
228         {
229                 const string &part = parts[i];
230                 const char *replace = 0;
231                 for(unsigned j=0; (!replace && aliases[j]); j+=2)
232                         if(part==aliases[j])
233                                 replace = aliases[j+1];
234
235                 if(replace)
236                 {
237                         bool has_dash = false;
238                         for(const char *c=replace; (!has_dash && *c); ++c)
239                                 has_dash = (*c=='-');
240
241                         if(has_dash)
242                         {
243                                 vector<string> rparts = split(replace, "-");
244                                 parts[i] = rparts[0];
245                                 parts.insert(parts.begin()+i+1, rparts.begin()+1, rparts.end());
246                                 i += rparts.size()-1;
247                         }
248                         else
249                                 parts[i] = replace;
250                 }
251         }
252 }
253
254 void Architecture::parse_specification(const string &spec)
255 {
256         vector<string> parts = split(spec, "-");
257         resolve_aliases(parts);
258         for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
259         {
260                 bool ok = false;
261
262                 for(unsigned j=0; (!ok && types[j]); ++j)
263                         if(*i==types[j])
264                         {
265                                 if(!type.empty() && *i!=type)
266                                         throw invalid_argument("Conflicting type specification");
267                                 type = *i;
268                                 ok = true;
269                         }
270
271                 for(unsigned j=0; (!ok && cpus[j]); j+=2)
272                         if(*i==cpus[j])
273                         {
274                                 if(type.empty())
275                                         type = cpus[j+1];
276                                 else if(cpus[j+1]!=type)
277                                         throw invalid_argument("Conflicting CPU specification");
278                                 cpu = *i;
279                                 ok = true;
280                         }
281
282                 for(unsigned j=0; (!ok && fpus[j]); j+=2)
283                         if(*i==fpus[j])
284                         {
285                                 if(fpus[j+1]!=type)
286                                         throw invalid_argument("Conflicting FPU specification");
287                                 fpu = *i;
288                                 ok = true;
289                         }
290
291                 for(unsigned j=0; (!ok && systems[j]); ++j)
292                         if(*i==systems[j])
293                         {
294                                 system = *i;
295                                 ok = true;
296                         }
297
298                 for(unsigned j=0; (!ok && toolchains[j]); ++j)
299                         if(*i==toolchains[j])
300                         {
301                                 toolchain = *i;
302                                 ok = true;
303                         }
304
305                 if(!ok && (*i=="32" || *i=="64"))
306                 {
307                         bits = lexical_cast<unsigned>(*i);
308                         ok = true;
309                 }
310
311                 if(!ok)
312                         throw invalid_argument("Unrecognized part in arch specification: "+*i);
313         }
314 }
315
316
317 Architecture::Loader::Loader(Architecture &a):
318         DataFile::ObjectLoader<Architecture>(a)
319 {
320         add("prefix", &Architecture::cross_prefix);
321 }