3 #include <sys/utsname.h>
5 #include <msp/strings/format.h>
6 #include <msp/strings/utils.h>
7 #include "architecture.h"
45 const char *systems[] =
55 const char *aliases[] =
63 "win32", "windows-32",
64 "win64", "windows-64",
65 "power macintosh", "ppc",
73 Architecture::Architecture(Builder &b, const string &spec):
86 system = tolower(un.sysname);
87 parse_specification(tolower(un.machine));
88 // We really only want to set type for the default arch
92 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
97 parse_specification(spec);
98 const Architecture &native_arch = builder.get_native_arch();
100 type = native_arch.type;
102 system = native_arch.system;
105 if(type==native_arch.type)
106 bits = native_arch.bits;
111 if(type!=native_arch.type || system!=native_arch.system)
112 cross_prefix = format("%s-%s", type, system);
113 else if(bits==native_arch.bits)
118 name += format("-%s", cpu);
119 name += format("-%d-%s", bits, system);
121 if(system=="windows")
123 sharedlib_patterns.push_back(Pattern("%.dll"));
124 sharedlib_patterns.push_back(Pattern("lib%.dll"));
125 /* XXX Hack: Consider import libraries (*.dll.a) as dynamic libraries,
126 even though technically they are linked statically. */
127 sharedlib_patterns.push_back(Pattern("lib%.dll.a"));
128 staticlib_patterns.push_back(Pattern("lib%.a"));
129 staticlib_patterns.push_back(Pattern("%.lib"));
130 executable_patterns.push_back(Pattern("%.exe"));
135 sharedlib_patterns.push_back(Pattern("lib%.dylib"));
137 sharedlib_patterns.push_back(Pattern("lib%.so"));
138 staticlib_patterns.push_back(Pattern("lib%.a"));
139 executable_patterns.push_back(Pattern("%"));
143 bool Architecture::match_name(const string &pattern) const
145 bool negate = (pattern[0]=='!');
146 vector<string> parts = split(pattern.substr(negate), "-");
147 resolve_aliases(parts);
148 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
150 if((*i=="32" && bits==32) || (*i=="64" && bits==64))
152 else if(*i!=type && *i!=cpu && *i!=system)
158 string Architecture::best_match(const list<string> &names) const
161 unsigned best_size = 0;
162 for(list<string>::const_iterator i=names.begin(); i!=names.end(); ++i)
165 /* TODO Do full parse and alias resolution here? Otherwise x86 and
166 x86_64 are treated as equally good, even though the latter is more
169 for(string::const_iterator j=i->begin(); j!=i->end(); ++j)
182 void Architecture::resolve_aliases(vector<string> &parts)
184 for(unsigned i=0; i<parts.size(); ++i)
186 const string &part = parts[i];
187 const char *replace = 0;
188 for(unsigned j=0; (!replace && aliases[j]); j+=2)
190 replace = aliases[j+1];
194 bool has_dash = false;
195 for(const char *c=replace; (!has_dash && *c); ++c)
196 has_dash = (*c=='-');
200 vector<string> rparts = split(replace, "-");
201 parts[i] = rparts[0];
202 parts.insert(parts.begin()+i+1, rparts.begin()+1, rparts.end());
203 i += rparts.size()-1;
211 void Architecture::parse_specification(const string &spec)
213 vector<string> parts = split(spec, "-");
214 resolve_aliases(parts);
215 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
219 for(unsigned j=0; (!ok && types[j]); ++j)
222 if(!type.empty() && *i!=type)
223 throw invalid_argument("Conflicting type specification");
228 for(unsigned j=0; (!ok && cpus[j]); j+=2)
233 else if(cpus[j+1]!=type)
234 throw invalid_argument("Conflicting CPU specification");
239 for(unsigned j=0; (!ok && systems[j]); ++j)
246 if(!ok && (*i=="32" || *i=="64"))
248 bits = lexical_cast<unsigned>(*i);
253 throw invalid_argument("Unrecognized part in arch specification: "+*i);
258 Architecture::Loader::Loader(Architecture &a):
259 DataFile::ObjectLoader<Architecture>(a)
261 add("prefix", &Architecture::cross_prefix);