]> git.tdb.fi Git - builder.git/blob - source/architecture.cpp
Add drivers for the MSVC toolchain
[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         "vfpv3", "arm",
53         "neon",  "arm",
54         0
55 };
56
57 const char *systems[] =
58 {
59         "linux",
60         "freebsd",
61         "darwin",
62         "windows",
63         "android",
64         0
65 };
66
67 const char *toolchains[] =
68 {
69         "gnu",
70         "clang",
71         "msvc",
72         0
73 };
74
75 const char *aliases[] =
76 {
77         "pc",              "x86",
78         "x86_64",          "x86-64",
79         "x64",             "x86-64",
80         "amd64",           "x86-64",
81         "i586",            "pentium",
82         "i686",            "pentiumpro",
83         "corei7",          "nehalem",
84         "win32",           "windows-32",
85         "win64",           "windows-64",
86         "power macintosh", "ppc",
87         "armeabi",         "arm",
88         "v7a",             "armv7a",
89         "gcc",             "gnu",
90         "mingw",           "windows-gnu",
91         0
92 };
93
94 }
95
96 Architecture::Architecture(Builder &b, const string &spec):
97         builder(b),
98         bits(0),
99         native(false)
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) const
179 {
180         bool negate = (pattern[0]=='!');
181         vector<string> parts = split(pattern.substr(negate), "-");
182         resolve_aliases(parts);
183         for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
184         {
185                 if((*i=="32" && bits==32) || (*i=="64" && bits==64))
186                         ;
187                 else if(*i!=type && *i!=cpu && *i!=fpu && *i!=system && *i!=toolchain)
188                         return negate;
189         }
190         return !negate;
191 }
192
193 string Architecture::best_match(const vector<string> &names) const
194 {
195         string best;
196         unsigned best_size = 0;
197         for(vector<string>::const_iterator i=names.begin(); i!=names.end(); ++i)
198                 if(match_name(*i))
199                 {
200                         /* TODO Do full parse and alias resolution here?  Otherwise x86 and
201                         x86_64 are treated as equally good, even though the latter is more
202                         specific. */
203                         unsigned size = 1;
204                         for(string::const_iterator j=i->begin(); j!=i->end(); ++j)
205                                 size += (*j=='-');
206
207                         if(size>best_size)
208                         {
209                                 best = *i;
210                                 best_size = size;
211                         }
212                 }
213
214         return best;
215 }
216
217 template<typename T>
218 void Architecture::add_pattern(const string &pat)
219 {
220         filename_patterns[typeid(T).name()].push_back(Pattern(pat));
221 }
222
223 void Architecture::resolve_aliases(vector<string> &parts)
224 {
225         for(unsigned i=0; i<parts.size(); ++i)
226         {
227                 const string &part = parts[i];
228                 const char *replace = 0;
229                 for(unsigned j=0; (!replace && aliases[j]); j+=2)
230                         if(part==aliases[j])
231                                 replace = aliases[j+1];
232
233                 if(replace)
234                 {
235                         bool has_dash = false;
236                         for(const char *c=replace; (!has_dash && *c); ++c)
237                                 has_dash = (*c=='-');
238
239                         if(has_dash)
240                         {
241                                 vector<string> rparts = split(replace, "-");
242                                 parts[i] = rparts[0];
243                                 parts.insert(parts.begin()+i+1, rparts.begin()+1, rparts.end());
244                                 i += rparts.size()-1;
245                         }
246                         else
247                                 parts[i] = replace;
248                 }
249         }
250 }
251
252 void Architecture::parse_specification(const string &spec)
253 {
254         vector<string> parts = split(spec, "-");
255         resolve_aliases(parts);
256         for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
257         {
258                 bool ok = false;
259
260                 for(unsigned j=0; (!ok && types[j]); ++j)
261                         if(*i==types[j])
262                         {
263                                 if(!type.empty() && *i!=type)
264                                         throw invalid_argument("Conflicting type specification");
265                                 type = *i;
266                                 ok = true;
267                         }
268
269                 for(unsigned j=0; (!ok && cpus[j]); j+=2)
270                         if(*i==cpus[j])
271                         {
272                                 if(type.empty())
273                                         type = cpus[j+1];
274                                 else if(cpus[j+1]!=type)
275                                         throw invalid_argument("Conflicting CPU specification");
276                                 cpu = *i;
277                                 ok = true;
278                         }
279
280                 for(unsigned j=0; (!ok && fpus[j]); j+=2)
281                         if(*i==fpus[j])
282                         {
283                                 if(fpus[j+1]!=type)
284                                         throw invalid_argument("Conflicting FPU specification");
285                                 fpu = *i;
286                                 ok = true;
287                         }
288
289                 for(unsigned j=0; (!ok && systems[j]); ++j)
290                         if(*i==systems[j])
291                         {
292                                 system = *i;
293                                 ok = true;
294                         }
295
296                 for(unsigned j=0; (!ok && toolchains[j]); ++j)
297                         if(*i==toolchains[j])
298                         {
299                                 toolchain = *i;
300                                 ok = true;
301                         }
302
303                 if(!ok && (*i=="32" || *i=="64"))
304                 {
305                         bits = lexical_cast<unsigned>(*i);
306                         ok = true;
307                 }
308
309                 if(!ok)
310                         throw invalid_argument("Unrecognized part in arch specification: "+*i);
311         }
312 }
313
314
315 Architecture::Loader::Loader(Architecture &a):
316         DataFile::ObjectLoader<Architecture>(a)
317 {
318         add("prefix", &Architecture::cross_prefix);
319 }