3 #include <sys/utsname.h>
5 #include <msp/strings/format.h>
6 #include <msp/strings/utils.h>
7 #include "architecture.h"
54 const char *systems[] =
64 const char *toolchains[] =
71 const char *aliases[] =
79 "win32", "windows-32",
80 "win64", "windows-64",
81 "power macintosh", "ppc",
85 "mingw", "windows-gnu",
91 Architecture::Architecture(Builder &b, const string &spec):
104 system = tolower(un.sysname);
105 parse_specification(tolower(un.machine));
106 // We really only want to set type for the default arch
110 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
115 parse_specification(spec);
116 const Architecture &native_arch = builder.get_native_arch();
118 type = native_arch.type;
120 system = native_arch.system;
123 if(type==native_arch.type)
124 bits = native_arch.bits;
129 if(type!=native_arch.type || system!=native_arch.system)
130 cross_prefix = format("%s-%s", type, system);
131 else if(bits==native_arch.bits)
135 if(toolchain.empty())
137 if((system=="darwin" || system=="freebsd") && builder.get_vfs().find_binary("clang"))
145 name += format("-%s", cpu);
147 name += format("-%s", fpu);
148 name += format("-%d-%s-%s", bits, system, toolchain);
150 object_patterns.push_back(Pattern("%.o"));
151 if(system=="windows")
153 sharedlib_patterns.push_back(Pattern("%.dll"));
154 sharedlib_patterns.push_back(Pattern("lib%.dll"));
155 /* XXX Hack: Consider import libraries (*.dll.a) as dynamic libraries,
156 even though technically they are linked statically. */
157 sharedlib_patterns.push_back(Pattern("lib%.dll.a"));
158 staticlib_patterns.push_back(Pattern("lib%.a"));
159 staticlib_patterns.push_back(Pattern("%.lib"));
160 executable_patterns.push_back(Pattern("%.exe"));
165 sharedlib_patterns.push_back(Pattern("lib%.dylib"));
167 sharedlib_patterns.push_back(Pattern("lib%.so"));
168 staticlib_patterns.push_back(Pattern("lib%.a"));
169 executable_patterns.push_back(Pattern("%"));
173 bool Architecture::match_name(const string &pattern) const
175 bool negate = (pattern[0]=='!');
176 vector<string> parts = split(pattern.substr(negate), "-");
177 resolve_aliases(parts);
178 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
180 if((*i=="32" && bits==32) || (*i=="64" && bits==64))
182 else if(*i!=type && *i!=cpu && *i!=fpu && *i!=system && *i!=toolchain)
188 string Architecture::best_match(const list<string> &names) const
191 unsigned best_size = 0;
192 for(list<string>::const_iterator i=names.begin(); i!=names.end(); ++i)
195 /* TODO Do full parse and alias resolution here? Otherwise x86 and
196 x86_64 are treated as equally good, even though the latter is more
199 for(string::const_iterator j=i->begin(); j!=i->end(); ++j)
212 void Architecture::resolve_aliases(vector<string> &parts)
214 for(unsigned i=0; i<parts.size(); ++i)
216 const string &part = parts[i];
217 const char *replace = 0;
218 for(unsigned j=0; (!replace && aliases[j]); j+=2)
220 replace = aliases[j+1];
224 bool has_dash = false;
225 for(const char *c=replace; (!has_dash && *c); ++c)
226 has_dash = (*c=='-');
230 vector<string> rparts = split(replace, "-");
231 parts[i] = rparts[0];
232 parts.insert(parts.begin()+i+1, rparts.begin()+1, rparts.end());
233 i += rparts.size()-1;
241 void Architecture::parse_specification(const string &spec)
243 vector<string> parts = split(spec, "-");
244 resolve_aliases(parts);
245 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
249 for(unsigned j=0; (!ok && types[j]); ++j)
252 if(!type.empty() && *i!=type)
253 throw invalid_argument("Conflicting type specification");
258 for(unsigned j=0; (!ok && cpus[j]); j+=2)
263 else if(cpus[j+1]!=type)
264 throw invalid_argument("Conflicting CPU specification");
269 for(unsigned j=0; (!ok && fpus[j]); j+=2)
273 throw invalid_argument("Conflicting FPU specification");
278 for(unsigned j=0; (!ok && systems[j]); ++j)
285 for(unsigned j=0; (!ok && toolchains[j]); ++j)
286 if(*i==toolchains[j])
292 if(!ok && (*i=="32" || *i=="64"))
294 bits = lexical_cast<unsigned>(*i);
299 throw invalid_argument("Unrecognized part in arch specification: "+*i);
304 Architecture::Loader::Loader(Architecture &a):
305 DataFile::ObjectLoader<Architecture>(a)
307 add("prefix", &Architecture::cross_prefix);