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