2 #include <msp/strings/format.h>
3 #include <msp/strings/utils.h>
4 #include "architecture.h"
6 #include "executable.h"
7 #include "objectfile.h"
8 #include "sharedlibrary.h"
9 #include "staticlibrary.h"
56 const char *systems[] =
66 const char *toolchains[] =
73 const char *aliases[] =
82 "win32", "windows-32",
83 "win64", "windows-64",
84 "power macintosh", "ppc",
88 "mingw", "windows-gnu",
94 Architecture::Architecture(Builder &b, const string &spec):
101 parse_specification(get_system_type());
102 // We really only want to set type for the default arch
104 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
109 parse_specification(spec);
110 const Architecture &native_arch = builder.get_native_arch();
112 type = native_arch.type;
114 system = native_arch.system;
117 if(type==native_arch.type)
118 bits = native_arch.bits;
123 if(type!=native_arch.type || system!=native_arch.system)
124 cross_prefix = format("%s-%s", type, system);
125 else if(bits==native_arch.bits)
129 if(toolchain.empty())
131 if((system=="darwin" || system=="freebsd") && builder.get_vfs().find_binary("clang"))
139 name += format("-%s", cpu);
141 name += format("-%s", fpu);
142 name += format("-%d-%s-%s", bits, system, toolchain);
144 add_pattern<ObjectFile>("%.o");
145 if(system=="windows")
147 add_pattern<SharedLibrary>("%.dll");
148 add_pattern<SharedLibrary>("lib%.dll");
149 /* XXX Hack: Consider import libraries (*.dll.a) as dynamic libraries,
150 even though technically they are linked statically. */
151 add_pattern<SharedLibrary>("lib%.dll.a");
152 add_pattern<StaticLibrary>("lib%.a");
153 add_pattern<StaticLibrary>("%.lib");
154 add_pattern<Executable>("%.exe");
159 add_pattern<SharedLibrary>("lib%.dylib");
161 add_pattern<SharedLibrary>("lib%.so");
162 add_pattern<StaticLibrary>("lib%.a");
163 add_pattern<Executable>("%");
167 bool Architecture::match_name(const string &pattern) const
169 bool negate = (pattern[0]=='!');
170 vector<string> parts = split(pattern.substr(negate), "-");
171 resolve_aliases(parts);
172 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
174 if((*i=="32" && bits==32) || (*i=="64" && bits==64))
176 else if(*i!=type && *i!=cpu && *i!=fpu && *i!=system && *i!=toolchain)
182 string Architecture::best_match(const vector<string> &names) const
185 unsigned best_size = 0;
186 for(vector<string>::const_iterator i=names.begin(); i!=names.end(); ++i)
189 /* TODO Do full parse and alias resolution here? Otherwise x86 and
190 x86_64 are treated as equally good, even though the latter is more
193 for(string::const_iterator j=i->begin(); j!=i->end(); ++j)
207 void Architecture::add_pattern(const string &pat)
209 filename_patterns[typeid(T).name()].push_back(Pattern(pat));
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);