2 #include <msp/strings/format.h>
3 #include <msp/strings/utils.h>
4 #include "architecture.h"
52 const char *systems[] =
62 const char *toolchains[] =
69 const char *aliases[] =
77 "win32", "windows-32",
78 "win64", "windows-64",
79 "power macintosh", "ppc",
83 "mingw", "windows-gnu",
89 Architecture::Architecture(Builder &b, const string &spec):
96 parse_specification(get_system_type());
97 // We really only want to set type for the default arch
99 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
104 parse_specification(spec);
105 const Architecture &native_arch = builder.get_native_arch();
107 type = native_arch.type;
109 system = native_arch.system;
112 if(type==native_arch.type)
113 bits = native_arch.bits;
118 if(type!=native_arch.type || system!=native_arch.system)
119 cross_prefix = format("%s-%s", type, system);
120 else if(bits==native_arch.bits)
124 if(toolchain.empty())
126 if((system=="darwin" || system=="freebsd") && builder.get_vfs().find_binary("clang"))
134 name += format("-%s", cpu);
136 name += format("-%s", fpu);
137 name += format("-%d-%s-%s", bits, system, toolchain);
139 object_patterns.push_back(Pattern("%.o"));
140 if(system=="windows")
142 sharedlib_patterns.push_back(Pattern("%.dll"));
143 sharedlib_patterns.push_back(Pattern("lib%.dll"));
144 /* XXX Hack: Consider import libraries (*.dll.a) as dynamic libraries,
145 even though technically they are linked statically. */
146 sharedlib_patterns.push_back(Pattern("lib%.dll.a"));
147 staticlib_patterns.push_back(Pattern("lib%.a"));
148 staticlib_patterns.push_back(Pattern("%.lib"));
149 executable_patterns.push_back(Pattern("%.exe"));
154 sharedlib_patterns.push_back(Pattern("lib%.dylib"));
156 sharedlib_patterns.push_back(Pattern("lib%.so"));
157 staticlib_patterns.push_back(Pattern("lib%.a"));
158 executable_patterns.push_back(Pattern("%"));
162 bool Architecture::match_name(const string &pattern) const
164 bool negate = (pattern[0]=='!');
165 vector<string> parts = split(pattern.substr(negate), "-");
166 resolve_aliases(parts);
167 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
169 if((*i=="32" && bits==32) || (*i=="64" && bits==64))
171 else if(*i!=type && *i!=cpu && *i!=fpu && *i!=system && *i!=toolchain)
177 string Architecture::best_match(const list<string> &names) const
180 unsigned best_size = 0;
181 for(list<string>::const_iterator i=names.begin(); i!=names.end(); ++i)
184 /* TODO Do full parse and alias resolution here? Otherwise x86 and
185 x86_64 are treated as equally good, even though the latter is more
188 for(string::const_iterator j=i->begin(); j!=i->end(); ++j)
201 void Architecture::resolve_aliases(vector<string> &parts)
203 for(unsigned i=0; i<parts.size(); ++i)
205 const string &part = parts[i];
206 const char *replace = 0;
207 for(unsigned j=0; (!replace && aliases[j]); j+=2)
209 replace = aliases[j+1];
213 bool has_dash = false;
214 for(const char *c=replace; (!has_dash && *c); ++c)
215 has_dash = (*c=='-');
219 vector<string> rparts = split(replace, "-");
220 parts[i] = rparts[0];
221 parts.insert(parts.begin()+i+1, rparts.begin()+1, rparts.end());
222 i += rparts.size()-1;
230 void Architecture::parse_specification(const string &spec)
232 vector<string> parts = split(spec, "-");
233 resolve_aliases(parts);
234 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
238 for(unsigned j=0; (!ok && types[j]); ++j)
241 if(!type.empty() && *i!=type)
242 throw invalid_argument("Conflicting type specification");
247 for(unsigned j=0; (!ok && cpus[j]); j+=2)
252 else if(cpus[j+1]!=type)
253 throw invalid_argument("Conflicting CPU specification");
258 for(unsigned j=0; (!ok && fpus[j]); j+=2)
262 throw invalid_argument("Conflicting FPU specification");
267 for(unsigned j=0; (!ok && systems[j]); ++j)
274 for(unsigned j=0; (!ok && toolchains[j]); ++j)
275 if(*i==toolchains[j])
281 if(!ok && (*i=="32" || *i=="64"))
283 bits = lexical_cast<unsigned>(*i);
288 throw invalid_argument("Unrecognized part in arch specification: "+*i);
293 Architecture::Loader::Loader(Architecture &a):
294 DataFile::ObjectLoader<Architecture>(a)
296 add("prefix", &Architecture::cross_prefix);