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