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)
131 if(toolchain.empty())
133 if((system=="darwin" || system=="freebsd") && builder.get_vfs().find_binary("clang"))
135 else if(system=="windows" && native)
143 name += format("-%s", cpu);
145 name += format("-%s", fpu);
146 name += format("-%d-%s-%s", bits, system, toolchain);
148 if(system=="windows")
150 add_pattern<SharedLibrary>("%.dll");
151 if(toolchain=="msvc")
153 add_pattern<ObjectFile>("%.obj");
154 add_pattern<ImportLibrary>("%.lib");
155 add_pattern<StaticLibrary>("%_static.lib");
159 add_pattern<ObjectFile>("%.o");
160 add_pattern<SharedLibrary>("lib%.dll");
161 add_pattern<ImportLibrary>("lib%.dll.a");
162 add_pattern<StaticLibrary>("lib%.a");
164 add_pattern<Executable>("%.exe");
168 add_pattern<ObjectFile>("%.o");
170 add_pattern<SharedLibrary>("lib%.dylib");
172 add_pattern<SharedLibrary>("lib%.so");
173 add_pattern<StaticLibrary>("lib%.a");
174 add_pattern<Executable>("%");
178 bool Architecture::match_name(const string &pattern, unsigned *quality) const
180 bool negate = (pattern[0]=='!');
181 vector<string> parts = split(pattern.substr(negate), "-");
182 resolve_aliases(parts);
183 for(const string &p: parts)
185 if((p=="32" && bits==32) || (p=="64" && bits==64))
187 else if(p!=type && p!=cpu && p!=fpu && p!=system && p!=toolchain)
196 *quality = parts.size();
200 string Architecture::best_match(const vector<string> &names) const
203 unsigned best_quality = 0;
204 for(const string &n: names)
207 if(match_name(n, &quality))
208 if(quality>best_quality)
211 best_quality = quality;
219 void Architecture::add_pattern(const string &pat)
221 filename_patterns[typeid(T).name()].push_back(Pattern(pat));
224 void Architecture::resolve_aliases(vector<string> &parts)
226 for(unsigned i=0; i<parts.size(); ++i)
228 const string &part = parts[i];
229 const char *replace = 0;
230 for(unsigned j=0; (!replace && aliases[j]); j+=2)
232 replace = aliases[j+1];
236 bool has_dash = false;
237 for(const char *c=replace; (!has_dash && *c); ++c)
238 has_dash = (*c=='-');
242 vector<string> rparts = split(replace, "-");
243 parts[i] = rparts[0];
244 parts.insert(parts.begin()+i+1, rparts.begin()+1, rparts.end());
245 i += rparts.size()-1;
253 void Architecture::parse_specification(const string &spec)
255 vector<string> parts = split(spec, "-");
256 resolve_aliases(parts);
257 for(const string &p: parts)
261 for(unsigned j=0; (!ok && types[j]); ++j)
264 if(!type.empty() && p!=type)
265 throw invalid_argument("Conflicting type specification");
270 for(unsigned j=0; (!ok && cpus[j]); j+=2)
275 else if(cpus[j+1]!=type)
276 throw invalid_argument("Conflicting CPU specification");
281 for(unsigned j=0; (!ok && fpus[j]); j+=2)
285 throw invalid_argument("Conflicting FPU specification");
290 for(unsigned j=0; (!ok && systems[j]); ++j)
297 for(unsigned j=0; (!ok && toolchains[j]); ++j)
304 if(!ok && (p=="32" || p=="64"))
306 bits = lexical_cast<unsigned>(p);
311 throw invalid_argument("Unrecognized part in arch specification: "+p);
316 Architecture::Loader::Loader(Architecture &a):
317 DataFile::ObjectLoader<Architecture>(a)
319 add("prefix", &Architecture::cross_prefix);