]> git.tdb.fi Git - builder.git/blob - source/architecture.cpp
Make object file names configurable through 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         object_patterns.push_back(Pattern("%.o"));
151         if(system=="windows")
152         {
153                 sharedlib_patterns.push_back(Pattern("%.dll"));
154                 sharedlib_patterns.push_back(Pattern("lib%.dll"));
155                 /* XXX Hack: Consider import libraries (*.dll.a) as dynamic libraries,
156                 even though technically they are linked statically. */
157                 sharedlib_patterns.push_back(Pattern("lib%.dll.a"));
158                 staticlib_patterns.push_back(Pattern("lib%.a"));
159                 staticlib_patterns.push_back(Pattern("%.lib"));
160                 executable_patterns.push_back(Pattern("%.exe"));
161         }
162         else
163         {
164                 if(system=="darwin")
165                         sharedlib_patterns.push_back(Pattern("lib%.dylib"));
166                 else
167                         sharedlib_patterns.push_back(Pattern("lib%.so"));
168                 staticlib_patterns.push_back(Pattern("lib%.a"));
169                 executable_patterns.push_back(Pattern("%"));
170         }
171 }
172
173 bool Architecture::match_name(const string &pattern) const
174 {
175         bool negate = (pattern[0]=='!');
176         vector<string> parts = split(pattern.substr(negate), "-");
177         resolve_aliases(parts);
178         for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
179         {
180                 if((*i=="32" && bits==32) || (*i=="64" && bits==64))
181                         ;
182                 else if(*i!=type && *i!=cpu && *i!=fpu && *i!=system && *i!=toolchain)
183                         return negate;
184         }
185         return !negate;
186 }
187
188 string Architecture::best_match(const list<string> &names) const
189 {
190         string best;
191         unsigned best_size = 0;
192         for(list<string>::const_iterator i=names.begin(); i!=names.end(); ++i)
193                 if(match_name(*i))
194                 {
195                         /* TODO Do full parse and alias resolution here?  Otherwise x86 and
196                         x86_64 are treated as equally good, even though the latter is more
197                         specific. */
198                         unsigned size = 1;
199                         for(string::const_iterator j=i->begin(); j!=i->end(); ++j)
200                                 size += (*j=='-');
201
202                         if(size>best_size)
203                         {
204                                 best = *i;
205                                 best_size = size;
206                         }
207                 }
208
209         return best;
210 }
211
212 void Architecture::resolve_aliases(vector<string> &parts)
213 {
214         for(unsigned i=0; i<parts.size(); ++i)
215         {
216                 const string &part = parts[i];
217                 const char *replace = 0;
218                 for(unsigned j=0; (!replace && aliases[j]); j+=2)
219                         if(part==aliases[j])
220                                 replace = aliases[j+1];
221
222                 if(replace)
223                 {
224                         bool has_dash = false;
225                         for(const char *c=replace; (!has_dash && *c); ++c)
226                                 has_dash = (*c=='-');
227
228                         if(has_dash)
229                         {
230                                 vector<string> rparts = split(replace, "-");
231                                 parts[i] = rparts[0];
232                                 parts.insert(parts.begin()+i+1, rparts.begin()+1, rparts.end());
233                                 i += rparts.size()-1;
234                         }
235                         else
236                                 parts[i] = replace;
237                 }
238         }
239 }
240
241 void Architecture::parse_specification(const string &spec)
242 {
243         vector<string> parts = split(spec, "-");
244         resolve_aliases(parts);
245         for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
246         {
247                 bool ok = false;
248
249                 for(unsigned j=0; (!ok && types[j]); ++j)
250                         if(*i==types[j])
251                         {
252                                 if(!type.empty() && *i!=type)
253                                         throw invalid_argument("Conflicting type specification");
254                                 type = *i;
255                                 ok = true;
256                         }
257
258                 for(unsigned j=0; (!ok && cpus[j]); j+=2)
259                         if(*i==cpus[j])
260                         {
261                                 if(type.empty())
262                                         type = cpus[j+1];
263                                 else if(cpus[j+1]!=type)
264                                         throw invalid_argument("Conflicting CPU specification");
265                                 cpu = *i;
266                                 ok = true;
267                         }
268
269                 for(unsigned j=0; (!ok && fpus[j]); j+=2)
270                         if(*i==fpus[j])
271                         {
272                                 if(fpus[j+1]!=type)
273                                         throw invalid_argument("Conflicting FPU specification");
274                                 fpu = *i;
275                                 ok = true;
276                         }
277
278                 for(unsigned j=0; (!ok && systems[j]); ++j)
279                         if(*i==systems[j])
280                         {
281                                 system = *i;
282                                 ok = true;
283                         }
284
285                 for(unsigned j=0; (!ok && toolchains[j]); ++j)
286                         if(*i==toolchains[j])
287                         {
288                                 toolchain = *i;
289                                 ok = true;
290                         }
291
292                 if(!ok && (*i=="32" || *i=="64"))
293                 {
294                         bits = lexical_cast<unsigned>(*i);
295                         ok = true;
296                 }
297
298                 if(!ok)
299                         throw invalid_argument("Unrecognized part in arch specification: "+*i);
300         }
301 }
302
303
304 Architecture::Loader::Loader(Architecture &a):
305         DataFile::ObjectLoader<Architecture>(a)
306 {
307         add("prefix", &Architecture::cross_prefix);
308 }