2 #include <msp/strings/format.h>
3 #include <msp/strings/utils.h>
4 #include "architecture.h"
6 #include "executable.h"
7 #include "importlibrary.h"
8 #include "objectfile.h"
9 #include "sharedlibrary.h"
10 #include "staticlibrary.h"
59 const char *systems[] =
69 const char *toolchains[] =
77 const char *aliases[] =
86 "win32", "windows-32",
87 "win64", "windows-64",
88 "power macintosh", "ppc",
92 "mingw", "windows-gnu",
98 Architecture::Architecture(Builder &b, const string &spec):
103 parse_specification(get_system_type());
104 // We really only want to set type for the default arch
106 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
111 parse_specification(spec);
112 const Architecture &native_arch = builder.get_native_arch();
114 type = native_arch.type;
116 system = native_arch.system;
119 if(type==native_arch.type)
120 bits = native_arch.bits;
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)
134 void Architecture::refine(const string &spec)
136 parse_specification(spec);
140 void Architecture::update()
144 name += format("-%s", cpu);
146 name += format("-%s", fpu);
147 name += format("-%d-%s", bits, system);
148 if(!toolchain.empty())
149 name += format("-%s", toolchain);
151 filename_patterns.clear();
152 if(system=="windows")
154 add_pattern<SharedLibrary>("%.dll");
155 if(toolchain=="msvc")
157 add_pattern<ObjectFile>("%.obj");
158 add_pattern<ImportLibrary>("%.lib");
159 add_pattern<StaticLibrary>("%_static.lib");
163 add_pattern<ObjectFile>("%.o");
164 add_pattern<SharedLibrary>("lib%.dll");
165 add_pattern<ImportLibrary>("lib%.dll.a");
166 add_pattern<StaticLibrary>("lib%.a");
168 add_pattern<Executable>("%.exe");
172 add_pattern<ObjectFile>("%.o");
174 add_pattern<SharedLibrary>("lib%.dylib");
176 add_pattern<SharedLibrary>("lib%.so");
177 add_pattern<StaticLibrary>("lib%.a");
178 add_pattern<Executable>("%");
182 bool Architecture::match_name(const string &pattern, unsigned *quality) const
184 bool negate = (pattern[0]=='!');
185 vector<string> parts = split(pattern.substr(negate), "-");
186 resolve_aliases(parts);
187 for(const string &p: parts)
189 if((p=="32" && bits==32) || (p=="64" && bits==64))
191 else if(p!=type && p!=cpu && p!=fpu && p!=system && p!=toolchain)
200 *quality = parts.size();
204 string Architecture::best_match(const vector<string> &names) const
207 unsigned best_quality = 0;
208 for(const string &n: names)
211 if(match_name(n, &quality))
212 if(quality>best_quality)
215 best_quality = quality;
223 void Architecture::add_pattern(const string &pat)
225 filename_patterns[typeid(T).name()].push_back(Pattern(pat));
228 void Architecture::resolve_aliases(vector<string> &parts)
230 for(unsigned i=0; i<parts.size(); ++i)
232 const string &part = parts[i];
233 const char *replace = 0;
234 for(unsigned j=0; (!replace && aliases[j]); j+=2)
236 replace = aliases[j+1];
240 bool has_dash = false;
241 for(const char *c=replace; (!has_dash && *c); ++c)
242 has_dash = (*c=='-');
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;
257 void Architecture::parse_specification(const string &spec)
259 vector<string> parts = split(spec, "-");
260 resolve_aliases(parts);
261 for(const string &p: parts)
265 for(unsigned j=0; (!ok && types[j]); ++j)
268 if(!type.empty() && p!=type)
269 throw invalid_argument("Conflicting type specification");
274 for(unsigned j=0; (!ok && cpus[j]); j+=2)
279 else if(cpus[j+1]!=type)
280 throw invalid_argument("Conflicting CPU specification");
285 for(unsigned j=0; (!ok && fpus[j]); j+=2)
289 throw invalid_argument("Conflicting FPU specification");
294 for(unsigned j=0; (!ok && systems[j]); ++j)
297 if(!system.empty() && p!=system)
298 throw invalid_argument("Conflicting system specification");
303 for(unsigned j=0; (!ok && toolchains[j]); ++j)
306 if(!toolchain.empty() && p!=toolchain)
307 throw invalid_argument("Conflicting toolchain specification");
312 if(!ok && (p=="32" || p=="64"))
314 unsigned b = lexical_cast<unsigned>(p);
316 throw invalid_argument("Conflicting bits specification");
322 throw invalid_argument("Unrecognized part in arch specification: "+p);
327 Architecture::Loader::Loader(Architecture &a):
328 DataFile::ObjectLoader<Architecture>(a)
330 add("prefix", &Loader::cross_prefix);
333 void Architecture::Loader::cross_prefix(const string &p)
336 obj.cross_prefix = p;