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 *aliases[] =
72 "win32", "windows-32",
73 "win64", "windows-64",
74 "power macintosh", "ppc",
82 Architecture::Architecture(Builder &b, const string &spec):
95 system = tolower(un.sysname);
96 parse_specification(tolower(un.machine));
97 // We really only want to set type for the default arch
101 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
106 parse_specification(spec);
107 const Architecture &native_arch = builder.get_native_arch();
109 type = native_arch.type;
111 system = native_arch.system;
114 if(type==native_arch.type)
115 bits = native_arch.bits;
120 if(type!=native_arch.type || system!=native_arch.system)
121 cross_prefix = format("%s-%s", type, system);
122 else if(bits==native_arch.bits)
127 name += format("-%s", cpu);
129 name += format("-%s", fpu);
130 name += format("-%d-%s", bits, system);
132 if(system=="windows")
134 sharedlib_patterns.push_back(Pattern("%.dll"));
135 sharedlib_patterns.push_back(Pattern("lib%.dll"));
136 /* XXX Hack: Consider import libraries (*.dll.a) as dynamic libraries,
137 even though technically they are linked statically. */
138 sharedlib_patterns.push_back(Pattern("lib%.dll.a"));
139 staticlib_patterns.push_back(Pattern("lib%.a"));
140 staticlib_patterns.push_back(Pattern("%.lib"));
141 executable_patterns.push_back(Pattern("%.exe"));
146 sharedlib_patterns.push_back(Pattern("lib%.dylib"));
148 sharedlib_patterns.push_back(Pattern("lib%.so"));
149 staticlib_patterns.push_back(Pattern("lib%.a"));
150 executable_patterns.push_back(Pattern("%"));
154 bool Architecture::match_name(const string &pattern) const
156 bool negate = (pattern[0]=='!');
157 vector<string> parts = split(pattern.substr(negate), "-");
158 resolve_aliases(parts);
159 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
161 if((*i=="32" && bits==32) || (*i=="64" && bits==64))
163 else if(*i!=type && *i!=cpu && *i!=fpu && *i!=system)
169 string Architecture::best_match(const list<string> &names) const
172 unsigned best_size = 0;
173 for(list<string>::const_iterator i=names.begin(); i!=names.end(); ++i)
176 /* TODO Do full parse and alias resolution here? Otherwise x86 and
177 x86_64 are treated as equally good, even though the latter is more
180 for(string::const_iterator j=i->begin(); j!=i->end(); ++j)
193 void Architecture::resolve_aliases(vector<string> &parts)
195 for(unsigned i=0; i<parts.size(); ++i)
197 const string &part = parts[i];
198 const char *replace = 0;
199 for(unsigned j=0; (!replace && aliases[j]); j+=2)
201 replace = aliases[j+1];
205 bool has_dash = false;
206 for(const char *c=replace; (!has_dash && *c); ++c)
207 has_dash = (*c=='-');
211 vector<string> rparts = split(replace, "-");
212 parts[i] = rparts[0];
213 parts.insert(parts.begin()+i+1, rparts.begin()+1, rparts.end());
214 i += rparts.size()-1;
222 void Architecture::parse_specification(const string &spec)
224 vector<string> parts = split(spec, "-");
225 resolve_aliases(parts);
226 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
230 for(unsigned j=0; (!ok && types[j]); ++j)
233 if(!type.empty() && *i!=type)
234 throw invalid_argument("Conflicting type specification");
239 for(unsigned j=0; (!ok && cpus[j]); j+=2)
244 else if(cpus[j+1]!=type)
245 throw invalid_argument("Conflicting CPU specification");
250 for(unsigned j=0; (!ok && fpus[j]); j+=2)
254 throw invalid_argument("Conflicting FPU specification");
259 for(unsigned j=0; (!ok && systems[j]); ++j)
266 if(!ok && (*i=="32" || *i=="64"))
268 bits = lexical_cast<unsigned>(*i);
273 throw invalid_argument("Unrecognized part in arch specification: "+*i);
278 Architecture::Loader::Loader(Architecture &a):
279 DataFile::ObjectLoader<Architecture>(a)
281 add("prefix", &Architecture::cross_prefix);