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"
57 const char *systems[] =
67 const char *toolchains[] =
75 const char *aliases[] =
84 "win32", "windows-32",
85 "win64", "windows-64",
86 "power macintosh", "ppc",
90 "mingw", "windows-gnu",
96 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) const
180 bool negate = (pattern[0]=='!');
181 vector<string> parts = split(pattern.substr(negate), "-");
182 resolve_aliases(parts);
183 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
185 if((*i=="32" && bits==32) || (*i=="64" && bits==64))
187 else if(*i!=type && *i!=cpu && *i!=fpu && *i!=system && *i!=toolchain)
193 string Architecture::best_match(const vector<string> &names) const
196 unsigned best_size = 0;
197 for(vector<string>::const_iterator i=names.begin(); i!=names.end(); ++i)
200 /* TODO Do full parse and alias resolution here? Otherwise x86 and
201 x86_64 are treated as equally good, even though the latter is more
204 for(string::const_iterator j=i->begin(); j!=i->end(); ++j)
218 void Architecture::add_pattern(const string &pat)
220 filename_patterns[typeid(T).name()].push_back(Pattern(pat));
223 void Architecture::resolve_aliases(vector<string> &parts)
225 for(unsigned i=0; i<parts.size(); ++i)
227 const string &part = parts[i];
228 const char *replace = 0;
229 for(unsigned j=0; (!replace && aliases[j]); j+=2)
231 replace = aliases[j+1];
235 bool has_dash = false;
236 for(const char *c=replace; (!has_dash && *c); ++c)
237 has_dash = (*c=='-');
241 vector<string> rparts = split(replace, "-");
242 parts[i] = rparts[0];
243 parts.insert(parts.begin()+i+1, rparts.begin()+1, rparts.end());
244 i += rparts.size()-1;
252 void Architecture::parse_specification(const string &spec)
254 vector<string> parts = split(spec, "-");
255 resolve_aliases(parts);
256 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
260 for(unsigned j=0; (!ok && types[j]); ++j)
263 if(!type.empty() && *i!=type)
264 throw invalid_argument("Conflicting type specification");
269 for(unsigned j=0; (!ok && cpus[j]); j+=2)
274 else if(cpus[j+1]!=type)
275 throw invalid_argument("Conflicting CPU specification");
280 for(unsigned j=0; (!ok && fpus[j]); j+=2)
284 throw invalid_argument("Conflicting FPU specification");
289 for(unsigned j=0; (!ok && systems[j]); ++j)
296 for(unsigned j=0; (!ok && toolchains[j]); ++j)
297 if(*i==toolchains[j])
303 if(!ok && (*i=="32" || *i=="64"))
305 bits = lexical_cast<unsigned>(*i);
310 throw invalid_argument("Unrecognized part in arch specification: "+*i);
315 Architecture::Loader::Loader(Architecture &a):
316 DataFile::ObjectLoader<Architecture>(a)
318 add("prefix", &Architecture::cross_prefix);