]> git.tdb.fi Git - builder.git/blob - source/architecture.cpp
Add a toolchain field to Architecture
[builder.git] / source / architecture.cpp
1 #include <limits>
2 #ifndef _WIN32
3 #include <sys/utsname.h>
4 #endif
5 #include <msp/strings/format.h>
6 #include <msp/strings/utils.h>
7 #include "architecture.h"
8 #include "builder.h"
9
10 using namespace std;
11 using namespace Msp;
12
13 namespace {
14
15 const char *types[] =
16 {
17         "x86",
18         "arm",
19         "ppc",
20         0
21 };
22
23 const char *cpus[] =
24 {
25         "i386",       "x86",
26         "i486",       "x86",
27         "pentium",    "x86",
28         "pentiumpro", "x86",
29         "pentium2",   "x86",
30         "pentium3",   "x86",
31         "pentium4",   "x86",
32         "core2",      "x86",
33         "nehalem",    "x86",
34         "k6",         "x86",
35         "athlon",     "x86",
36         "athlonxp",   "x86",
37         "athlon64",   "x86",
38         "armv5",      "arm",
39         "armv6",      "arm",
40         "armv7",      "arm",
41         "armv7a",     "arm",
42         0
43 };
44
45 const char *fpus[] =
46 {
47         "387",   "x86",
48         "sse",   "x86",
49         "vfpv3", "arm",
50         "neon",  "arm",
51         0
52 };
53
54 const char *systems[] =
55 {
56         "linux",
57         "freebsd",
58         "darwin",
59         "windows",
60         "android",
61         0
62 };
63
64 const char *toolchains[] =
65 {
66         "gnu",
67         "clang",
68         0
69 };
70
71 const char *aliases[] =
72 {
73         "pc",              "x86",
74         "x86_64",          "x86-64",
75         "amd64",           "x86-64",
76         "i586",            "pentium",
77         "i686",            "pentiumpro",
78         "corei7",          "nehalem",
79         "win32",           "windows-32",
80         "win64",           "windows-64",
81         "power macintosh", "ppc",
82         "armeabi",         "arm",
83         "v7a",             "armv7a",
84         "gcc",             "gnu",
85         "mingw",           "windows-gnu",
86         0
87 };
88
89 }
90
91 Architecture::Architecture(Builder &b, const string &spec):
92         builder(b),
93         bits(0),
94         native(false)
95 {
96         if(spec.empty())
97         {
98 #ifdef _WIN32
99                 system = "windows";
100 #else
101                 utsname un;
102                 if(uname(&un)==0)
103                 {
104                         system = tolower(un.sysname);
105                         parse_specification(tolower(un.machine));
106                         // We really only want to set type for the default arch
107                         cpu.clear();
108                 }
109 #endif
110                 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
111                 native = true;
112         }
113         else
114         {
115                 parse_specification(spec);
116                 const Architecture &native_arch = builder.get_native_arch();
117                 if(type.empty())
118                         type = native_arch.type;
119                 if(system.empty())
120                         system = native_arch.system;
121                 if(!bits)
122                 {
123                         if(type==native_arch.type)
124                                 bits = native_arch.bits;
125                         else
126                                 bits = 32;
127                 }
128
129                 if(type!=native_arch.type || system!=native_arch.system)
130                         cross_prefix = format("%s-%s", type, system);
131                 else if(bits==native_arch.bits)
132                         native = true;
133         }
134
135         if(toolchain.empty())
136         {
137                 if((system=="darwin" || system=="freebsd") && builder.get_vfs().find_binary("clang"))
138                         toolchain = "clang";
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                 sharedlib_patterns.push_back(Pattern("%.dll"));
153                 sharedlib_patterns.push_back(Pattern("lib%.dll"));
154                 /* XXX Hack: Consider import libraries (*.dll.a) as dynamic libraries,
155                 even though technically they are linked statically. */
156                 sharedlib_patterns.push_back(Pattern("lib%.dll.a"));
157                 staticlib_patterns.push_back(Pattern("lib%.a"));
158                 staticlib_patterns.push_back(Pattern("%.lib"));
159                 executable_patterns.push_back(Pattern("%.exe"));
160         }
161         else
162         {
163                 if(system=="darwin")
164                         sharedlib_patterns.push_back(Pattern("lib%.dylib"));
165                 else
166                         sharedlib_patterns.push_back(Pattern("lib%.so"));
167                 staticlib_patterns.push_back(Pattern("lib%.a"));
168                 executable_patterns.push_back(Pattern("%"));
169         }
170 }
171
172 bool Architecture::match_name(const string &pattern) const
173 {
174         bool negate = (pattern[0]=='!');
175         vector<string> parts = split(pattern.substr(negate), "-");
176         resolve_aliases(parts);
177         for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
178         {
179                 if((*i=="32" && bits==32) || (*i=="64" && bits==64))
180                         ;
181                 else if(*i!=type && *i!=cpu && *i!=fpu && *i!=system && *i!=toolchain)
182                         return negate;
183         }
184         return !negate;
185 }
186
187 string Architecture::best_match(const list<string> &names) const
188 {
189         string best;
190         unsigned best_size = 0;
191         for(list<string>::const_iterator i=names.begin(); i!=names.end(); ++i)
192                 if(match_name(*i))
193                 {
194                         /* TODO Do full parse and alias resolution here?  Otherwise x86 and
195                         x86_64 are treated as equally good, even though the latter is more
196                         specific. */
197                         unsigned size = 1;
198                         for(string::const_iterator j=i->begin(); j!=i->end(); ++j)
199                                 size += (*j=='-');
200
201                         if(size>best_size)
202                         {
203                                 best = *i;
204                                 best_size = size;
205                         }
206                 }
207
208         return best;
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 }