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