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[] =
74 const char *aliases[] =
83 "win32", "windows-32",
84 "win64", "windows-64",
85 "power macintosh", "ppc",
89 "mingw", "windows-gnu",
95 Architecture::Architecture(Builder &b, const string &spec):
102 parse_specification(get_system_type());
103 // We really only want to set type for the default arch
105 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
110 parse_specification(spec);
111 const Architecture &native_arch = builder.get_native_arch();
113 type = native_arch.type;
115 system = native_arch.system;
118 if(type==native_arch.type)
119 bits = native_arch.bits;
124 if(type!=native_arch.type || system!=native_arch.system)
125 cross_prefix = format("%s-%s", type, system);
126 else if(bits==native_arch.bits)
130 if(toolchain.empty())
132 if((system=="darwin" || system=="freebsd") && builder.get_vfs().find_binary("clang"))
140 name += format("-%s", cpu);
142 name += format("-%s", fpu);
143 name += format("-%d-%s-%s", bits, system, toolchain);
145 add_pattern<ObjectFile>("%.o");
146 if(system=="windows")
148 add_pattern<SharedLibrary>("%.dll");
149 add_pattern<SharedLibrary>("lib%.dll");
150 add_pattern<ImportLibrary>("lib%.dll.a");
151 add_pattern<StaticLibrary>("lib%.a");
152 add_pattern<StaticLibrary>("%.lib");
153 add_pattern<Executable>("%.exe");
158 add_pattern<SharedLibrary>("lib%.dylib");
160 add_pattern<SharedLibrary>("lib%.so");
161 add_pattern<StaticLibrary>("lib%.a");
162 add_pattern<Executable>("%");
166 bool Architecture::match_name(const string &pattern) const
168 bool negate = (pattern[0]=='!');
169 vector<string> parts = split(pattern.substr(negate), "-");
170 resolve_aliases(parts);
171 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
173 if((*i=="32" && bits==32) || (*i=="64" && bits==64))
175 else if(*i!=type && *i!=cpu && *i!=fpu && *i!=system && *i!=toolchain)
181 string Architecture::best_match(const vector<string> &names) const
184 unsigned best_size = 0;
185 for(vector<string>::const_iterator i=names.begin(); i!=names.end(); ++i)
188 /* TODO Do full parse and alias resolution here? Otherwise x86 and
189 x86_64 are treated as equally good, even though the latter is more
192 for(string::const_iterator j=i->begin(); j!=i->end(); ++j)
206 void Architecture::add_pattern(const string &pat)
208 filename_patterns[typeid(T).name()].push_back(Pattern(pat));
211 void Architecture::resolve_aliases(vector<string> &parts)
213 for(unsigned i=0; i<parts.size(); ++i)
215 const string &part = parts[i];
216 const char *replace = 0;
217 for(unsigned j=0; (!replace && aliases[j]); j+=2)
219 replace = aliases[j+1];
223 bool has_dash = false;
224 for(const char *c=replace; (!has_dash && *c); ++c)
225 has_dash = (*c=='-');
229 vector<string> rparts = split(replace, "-");
230 parts[i] = rparts[0];
231 parts.insert(parts.begin()+i+1, rparts.begin()+1, rparts.end());
232 i += rparts.size()-1;
240 void Architecture::parse_specification(const string &spec)
242 vector<string> parts = split(spec, "-");
243 resolve_aliases(parts);
244 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
248 for(unsigned j=0; (!ok && types[j]); ++j)
251 if(!type.empty() && *i!=type)
252 throw invalid_argument("Conflicting type specification");
257 for(unsigned j=0; (!ok && cpus[j]); j+=2)
262 else if(cpus[j+1]!=type)
263 throw invalid_argument("Conflicting CPU specification");
268 for(unsigned j=0; (!ok && fpus[j]); j+=2)
272 throw invalid_argument("Conflicting FPU specification");
277 for(unsigned j=0; (!ok && systems[j]); ++j)
284 for(unsigned j=0; (!ok && toolchains[j]); ++j)
285 if(*i==toolchains[j])
291 if(!ok && (*i=="32" || *i=="64"))
293 bits = lexical_cast<unsigned>(*i);
298 throw invalid_argument("Unrecognized part in arch specification: "+*i);
303 Architecture::Loader::Loader(Architecture &a):
304 DataFile::ObjectLoader<Architecture>(a)
306 add("prefix", &Architecture::cross_prefix);