]> git.tdb.fi Git - builder.git/blob - source/architecture.cpp
Tweak VC project files to better match what the software expects
[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 "sysutils.h"
7
8 using namespace std;
9 using namespace Msp;
10
11 namespace {
12
13 const char *types[] =
14 {
15         "x86",
16         "arm",
17         "ppc",
18         0
19 };
20
21 const char *cpus[] =
22 {
23         "i386",       "x86",
24         "i486",       "x86",
25         "pentium",    "x86",
26         "pentiumpro", "x86",
27         "pentium2",   "x86",
28         "pentium3",   "x86",
29         "pentium4",   "x86",
30         "core2",      "x86",
31         "nehalem",    "x86",
32         "k6",         "x86",
33         "athlon",     "x86",
34         "athlonxp",   "x86",
35         "athlon64",   "x86",
36         "armv5",      "arm",
37         "armv6",      "arm",
38         "armv7",      "arm",
39         "armv7a",     "arm",
40         0
41 };
42
43 const char *fpus[] =
44 {
45         "387",   "x86",
46         "sse",   "x86",
47         "vfpv3", "arm",
48         "neon",  "arm",
49         0
50 };
51
52 const char *systems[] =
53 {
54         "linux",
55         "freebsd",
56         "darwin",
57         "windows",
58         "android",
59         0
60 };
61
62 const char *toolchains[] =
63 {
64         "gnu",
65         "clang",
66         0
67 };
68
69 const char *aliases[] =
70 {
71         "pc",              "x86",
72         "x86_64",          "x86-64",
73         "x64",             "x86-64",
74         "amd64",           "x86-64",
75         "i586",            "pentium",
76         "i686",            "pentiumpro",
77         "corei7",          "nehalem",
78         "win32",           "windows-32",
79         "win64",           "windows-64",
80         "power macintosh", "ppc",
81         "armeabi",         "arm",
82         "v7a",             "armv7a",
83         "gcc",             "gnu",
84         "mingw",           "windows-gnu",
85         0
86 };
87
88 }
89
90 Architecture::Architecture(Builder &b, const string &spec):
91         builder(b),
92         bits(0),
93         native(false)
94 {
95         if(spec.empty())
96         {
97                 parse_specification(get_system_type());
98                 // We really only want to set type for the default arch
99                 cpu.clear();
100                 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
101                 native = true;
102         }
103         else
104         {
105                 parse_specification(spec);
106                 const Architecture &native_arch = builder.get_native_arch();
107                 if(type.empty())
108                         type = native_arch.type;
109                 if(system.empty())
110                         system = native_arch.system;
111                 if(!bits)
112                 {
113                         if(type==native_arch.type)
114                                 bits = native_arch.bits;
115                         else
116                                 bits = 32;
117                 }
118
119                 if(type!=native_arch.type || system!=native_arch.system)
120                         cross_prefix = format("%s-%s", type, system);
121                 else if(bits==native_arch.bits)
122                         native = true;
123         }
124
125         if(toolchain.empty())
126         {
127                 if((system=="darwin" || system=="freebsd") && builder.get_vfs().find_binary("clang"))
128                         toolchain = "clang";
129                 else
130                         toolchain = "gnu";
131         }
132
133         name = type;
134         if(!cpu.empty())
135                 name += format("-%s", cpu);
136         if(!fpu.empty())
137                 name += format("-%s", fpu);
138         name += format("-%d-%s-%s", bits, system, toolchain);
139
140         object_patterns.push_back(Pattern("%.o"));
141         if(system=="windows")
142         {
143                 sharedlib_patterns.push_back(Pattern("%.dll"));
144                 sharedlib_patterns.push_back(Pattern("lib%.dll"));
145                 /* XXX Hack: Consider import libraries (*.dll.a) as dynamic libraries,
146                 even though technically they are linked statically. */
147                 sharedlib_patterns.push_back(Pattern("lib%.dll.a"));
148                 staticlib_patterns.push_back(Pattern("lib%.a"));
149                 staticlib_patterns.push_back(Pattern("%.lib"));
150                 executable_patterns.push_back(Pattern("%.exe"));
151         }
152         else
153         {
154                 if(system=="darwin")
155                         sharedlib_patterns.push_back(Pattern("lib%.dylib"));
156                 else
157                         sharedlib_patterns.push_back(Pattern("lib%.so"));
158                 staticlib_patterns.push_back(Pattern("lib%.a"));
159                 executable_patterns.push_back(Pattern("%"));
160         }
161 }
162
163 bool Architecture::match_name(const string &pattern) const
164 {
165         bool negate = (pattern[0]=='!');
166         vector<string> parts = split(pattern.substr(negate), "-");
167         resolve_aliases(parts);
168         for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
169         {
170                 if((*i=="32" && bits==32) || (*i=="64" && bits==64))
171                         ;
172                 else if(*i!=type && *i!=cpu && *i!=fpu && *i!=system && *i!=toolchain)
173                         return negate;
174         }
175         return !negate;
176 }
177
178 string Architecture::best_match(const vector<string> &names) const
179 {
180         string best;
181         unsigned best_size = 0;
182         for(vector<string>::const_iterator i=names.begin(); i!=names.end(); ++i)
183                 if(match_name(*i))
184                 {
185                         /* TODO Do full parse and alias resolution here?  Otherwise x86 and
186                         x86_64 are treated as equally good, even though the latter is more
187                         specific. */
188                         unsigned size = 1;
189                         for(string::const_iterator j=i->begin(); j!=i->end(); ++j)
190                                 size += (*j=='-');
191
192                         if(size>best_size)
193                         {
194                                 best = *i;
195                                 best_size = size;
196                         }
197                 }
198
199         return best;
200 }
201
202 void Architecture::resolve_aliases(vector<string> &parts)
203 {
204         for(unsigned i=0; i<parts.size(); ++i)
205         {
206                 const string &part = parts[i];
207                 const char *replace = 0;
208                 for(unsigned j=0; (!replace && aliases[j]); j+=2)
209                         if(part==aliases[j])
210                                 replace = aliases[j+1];
211
212                 if(replace)
213                 {
214                         bool has_dash = false;
215                         for(const char *c=replace; (!has_dash && *c); ++c)
216                                 has_dash = (*c=='-');
217
218                         if(has_dash)
219                         {
220                                 vector<string> rparts = split(replace, "-");
221                                 parts[i] = rparts[0];
222                                 parts.insert(parts.begin()+i+1, rparts.begin()+1, rparts.end());
223                                 i += rparts.size()-1;
224                         }
225                         else
226                                 parts[i] = replace;
227                 }
228         }
229 }
230
231 void Architecture::parse_specification(const string &spec)
232 {
233         vector<string> parts = split(spec, "-");
234         resolve_aliases(parts);
235         for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
236         {
237                 bool ok = false;
238
239                 for(unsigned j=0; (!ok && types[j]); ++j)
240                         if(*i==types[j])
241                         {
242                                 if(!type.empty() && *i!=type)
243                                         throw invalid_argument("Conflicting type specification");
244                                 type = *i;
245                                 ok = true;
246                         }
247
248                 for(unsigned j=0; (!ok && cpus[j]); j+=2)
249                         if(*i==cpus[j])
250                         {
251                                 if(type.empty())
252                                         type = cpus[j+1];
253                                 else if(cpus[j+1]!=type)
254                                         throw invalid_argument("Conflicting CPU specification");
255                                 cpu = *i;
256                                 ok = true;
257                         }
258
259                 for(unsigned j=0; (!ok && fpus[j]); j+=2)
260                         if(*i==fpus[j])
261                         {
262                                 if(fpus[j+1]!=type)
263                                         throw invalid_argument("Conflicting FPU specification");
264                                 fpu = *i;
265                                 ok = true;
266                         }
267
268                 for(unsigned j=0; (!ok && systems[j]); ++j)
269                         if(*i==systems[j])
270                         {
271                                 system = *i;
272                                 ok = true;
273                         }
274
275                 for(unsigned j=0; (!ok && toolchains[j]); ++j)
276                         if(*i==toolchains[j])
277                         {
278                                 toolchain = *i;
279                                 ok = true;
280                         }
281
282                 if(!ok && (*i=="32" || *i=="64"))
283                 {
284                         bits = lexical_cast<unsigned>(*i);
285                         ok = true;
286                 }
287
288                 if(!ok)
289                         throw invalid_argument("Unrecognized part in arch specification: "+*i);
290         }
291 }
292
293
294 Architecture::Loader::Loader(Architecture &a):
295         DataFile::ObjectLoader<Architecture>(a)
296 {
297         add("prefix", &Architecture::cross_prefix);
298 }