]> git.tdb.fi Git - builder.git/blob - source/architecture.cpp
Add gcc's private library directory to ClangLinker's system path
[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 "executable.h"
7 #include "importlibrary.h"
8 #include "objectfile.h"
9 #include "sharedlibrary.h"
10 #include "staticlibrary.h"
11 #include "sysutils.h"
12
13 using namespace std;
14 using namespace Msp;
15
16 namespace {
17
18 const char *types[] =
19 {
20         "x86",
21         "arm",
22         "ppc",
23         0
24 };
25
26 const char *cpus[] =
27 {
28         "i386",       "x86",
29         "i486",       "x86",
30         "pentium",    "x86",
31         "pentiumpro", "x86",
32         "pentium2",   "x86",
33         "pentium3",   "x86",
34         "pentium4",   "x86",
35         "core2",      "x86",
36         "nehalem",    "x86",
37         "k6",         "x86",
38         "athlon",     "x86",
39         "athlonxp",   "x86",
40         "athlon64",   "x86",
41         "armv5",      "arm",
42         "armv6",      "arm",
43         "armv7",      "arm",
44         "armv7a",     "arm",
45         0
46 };
47
48 const char *fpus[] =
49 {
50         "387",   "x86",
51         "sse",   "x86",
52         "sse3",  "x86",
53         "sse4.1", "x86",
54         "vfpv3", "arm",
55         "neon",  "arm",
56         0
57 };
58
59 const char *systems[] =
60 {
61         "linux",
62         "freebsd",
63         "darwin",
64         "windows",
65         "android",
66         0
67 };
68
69 const char *toolchains[] =
70 {
71         "gnu",
72         "clang",
73         "msvc",
74         0
75 };
76
77 const char *aliases[] =
78 {
79         "pc",              "x86",
80         "x86_64",          "x86-64",
81         "x64",             "x86-64",
82         "amd64",           "x86-64",
83         "i586",            "pentium",
84         "i686",            "pentiumpro",
85         "corei7",          "nehalem",
86         "win32",           "windows-32",
87         "win64",           "windows-64",
88         "power macintosh", "ppc",
89         "armeabi",         "arm",
90         "v7a",             "armv7a",
91         "gcc",             "gnu",
92         "mingw",           "windows-gnu",
93         0
94 };
95
96 }
97
98 Architecture::Architecture(Builder &b, const string &spec):
99         builder(b)
100 {
101         if(spec.empty())
102         {
103                 parse_specification(get_system_type());
104                 // We really only want to set type for the default arch
105                 cpu.clear();
106                 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
107                 native = true;
108         }
109         else
110         {
111                 parse_specification(spec);
112                 const Architecture &native_arch = builder.get_native_arch();
113                 if(type.empty())
114                         type = native_arch.type;
115                 if(system.empty())
116                         system = native_arch.system;
117                 if(!bits)
118                 {
119                         if(type==native_arch.type)
120                                 bits = native_arch.bits;
121                         else
122                                 bits = 32;
123                 }
124
125                 if(type!=native_arch.type || system!=native_arch.system)
126                         cross_prefix = format("%s-%s", type, system);
127                 else if(bits==native_arch.bits)
128                         native = true;
129         }
130
131         update();
132 }
133
134 void Architecture::refine(const string &spec)
135 {
136         parse_specification(spec);
137         update();
138 }
139
140 void Architecture::update()
141 {
142         name = type;
143         if(!cpu.empty())
144                 name += format("-%s", cpu);
145         if(!fpu.empty())
146                 name += format("-%s", fpu);
147         name += format("-%d-%s", bits, system);
148         if(!toolchain.empty())
149                 name += format("-%s", toolchain);
150
151         filename_patterns.clear();
152         if(system=="windows")
153         {
154                 add_pattern<SharedLibrary>("%.dll");
155                 if(toolchain=="msvc")
156                 {
157                         add_pattern<ObjectFile>("%.obj");
158                         add_pattern<ImportLibrary>("%.lib");
159                         add_pattern<StaticLibrary>("%_static.lib");
160                 }
161                 else
162                 {
163                         add_pattern<ObjectFile>("%.o");
164                         add_pattern<SharedLibrary>("lib%.dll");
165                         add_pattern<ImportLibrary>("lib%.dll.a");
166                         add_pattern<StaticLibrary>("lib%.a");
167                 }
168                 add_pattern<Executable>("%.exe");
169         }
170         else
171         {
172                 add_pattern<ObjectFile>("%.o");
173                 if(system=="darwin")
174                         add_pattern<SharedLibrary>("lib%.dylib");
175                 else
176                         add_pattern<SharedLibrary>("lib%.so");
177                 add_pattern<StaticLibrary>("lib%.a");
178                 add_pattern<Executable>("%");
179         }
180 }
181
182 bool Architecture::match_name(const string &pattern, unsigned *quality) const
183 {
184         bool negate = (pattern[0]=='!');
185         vector<string> parts = split(pattern.substr(negate), "-");
186         resolve_aliases(parts);
187         for(const string &p: parts)
188         {
189                 if((p=="32" && bits==32) || (p=="64" && bits==64))
190                         ;
191                 else if(p!=type && p!=cpu && p!=fpu && p!=system && p!=toolchain)
192                 {
193                         if(quality)
194                                 *quality = 0;
195                         return negate;
196                 }
197         }
198
199         if(quality)
200                 *quality = parts.size();
201         return !negate;
202 }
203
204 string Architecture::best_match(const vector<string> &names) const
205 {
206         string best;
207         unsigned best_quality = 0;
208         for(const string &n: names)
209         {
210                 unsigned quality;
211                 if(match_name(n, &quality))
212                         if(quality>best_quality)
213                         {
214                                 best = n;
215                                 best_quality = quality;
216                         }
217         }
218
219         return best;
220 }
221
222 template<typename T>
223 void Architecture::add_pattern(const string &pat)
224 {
225         filename_patterns[typeid(T).name()].push_back(Pattern(pat));
226 }
227
228 void Architecture::resolve_aliases(vector<string> &parts)
229 {
230         for(unsigned i=0; i<parts.size(); ++i)
231         {
232                 const string &part = parts[i];
233                 const char *replace = 0;
234                 for(unsigned j=0; (!replace && aliases[j]); j+=2)
235                         if(part==aliases[j])
236                                 replace = aliases[j+1];
237
238                 if(replace)
239                 {
240                         bool has_dash = false;
241                         for(const char *c=replace; (!has_dash && *c); ++c)
242                                 has_dash = (*c=='-');
243
244                         if(has_dash)
245                         {
246                                 vector<string> rparts = split(replace, "-");
247                                 parts[i] = rparts[0];
248                                 parts.insert(parts.begin()+i+1, rparts.begin()+1, rparts.end());
249                                 i += rparts.size()-1;
250                         }
251                         else
252                                 parts[i] = replace;
253                 }
254         }
255 }
256
257 void Architecture::parse_specification(const string &spec)
258 {
259         vector<string> parts = split(spec, "-");
260         resolve_aliases(parts);
261         for(const string &p: parts)
262         {
263                 bool ok = false;
264
265                 for(unsigned j=0; (!ok && types[j]); ++j)
266                         if(p==types[j])
267                         {
268                                 if(!type.empty() && p!=type)
269                                         throw invalid_argument("Conflicting type specification");
270                                 type = p;
271                                 ok = true;
272                         }
273
274                 for(unsigned j=0; (!ok && cpus[j]); j+=2)
275                         if(p==cpus[j])
276                         {
277                                 if(type.empty())
278                                         type = cpus[j+1];
279                                 else if(cpus[j+1]!=type)
280                                         throw invalid_argument("Conflicting CPU specification");
281                                 cpu = p;
282                                 ok = true;
283                         }
284
285                 for(unsigned j=0; (!ok && fpus[j]); j+=2)
286                         if(p==fpus[j])
287                         {
288                                 if(fpus[j+1]!=type)
289                                         throw invalid_argument("Conflicting FPU specification");
290                                 fpu = p;
291                                 ok = true;
292                         }
293
294                 for(unsigned j=0; (!ok && systems[j]); ++j)
295                         if(p==systems[j])
296                         {
297                                 if(!system.empty() && p!=system)
298                                         throw invalid_argument("Conflicting system specification");
299                                 system = p;
300                                 ok = true;
301                         }
302
303                 for(unsigned j=0; (!ok && toolchains[j]); ++j)
304                         if(p==toolchains[j])
305                         {
306                                 if(!toolchain.empty() && p!=toolchain)
307                                         throw invalid_argument("Conflicting toolchain specification");
308                                 toolchain = p;
309                                 ok = true;
310                         }
311
312                 if(!ok && (p=="32" || p=="64"))
313                 {
314                         unsigned b = lexical_cast<unsigned>(p);
315                         if(bits && b!=bits)
316                                 throw invalid_argument("Conflicting bits specification");
317                         bits = b;
318                         ok = true;
319                 }
320
321                 if(!ok)
322                         throw invalid_argument("Unrecognized part in arch specification: "+p);
323         }
324 }
325
326
327 Architecture::Loader::Loader(Architecture &a):
328         DataFile::ObjectLoader<Architecture>(a)
329 {
330         add("prefix", &Loader::cross_prefix);
331 }
332
333 void Architecture::Loader::cross_prefix(const string &p)
334 {
335         if(!obj.native)
336                 obj.cross_prefix = p;
337 }