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"
59 const char *systems[] =
69 const char *toolchains[] =
77 const char *aliases[] =
86 "win32", "windows-32",
87 "win64", "windows-64",
88 "power macintosh", "ppc",
92 "mingw", "windows-gnu",
98 Architecture::Architecture(Builder &b, const string &spec):
105 parse_specification(get_system_type());
106 // We really only want to set type for the default arch
108 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
113 parse_specification(spec);
114 const Architecture &native_arch = builder.get_native_arch();
116 type = native_arch.type;
118 system = native_arch.system;
121 if(type==native_arch.type)
122 bits = native_arch.bits;
127 if(type!=native_arch.type || system!=native_arch.system)
128 cross_prefix = format("%s-%s", type, system);
129 else if(bits==native_arch.bits)
133 if(toolchain.empty())
135 if((system=="darwin" || system=="freebsd") && builder.get_vfs().find_binary("clang"))
137 else if(system=="windows" && native)
145 name += format("-%s", cpu);
147 name += format("-%s", fpu);
148 name += format("-%d-%s-%s", bits, system, toolchain);
150 if(system=="windows")
152 add_pattern<SharedLibrary>("%.dll");
153 if(toolchain=="msvc")
155 add_pattern<ObjectFile>("%.obj");
156 add_pattern<ImportLibrary>("%.lib");
157 add_pattern<StaticLibrary>("%_static.lib");
161 add_pattern<ObjectFile>("%.o");
162 add_pattern<SharedLibrary>("lib%.dll");
163 add_pattern<ImportLibrary>("lib%.dll.a");
164 add_pattern<StaticLibrary>("lib%.a");
166 add_pattern<Executable>("%.exe");
170 add_pattern<ObjectFile>("%.o");
172 add_pattern<SharedLibrary>("lib%.dylib");
174 add_pattern<SharedLibrary>("lib%.so");
175 add_pattern<StaticLibrary>("lib%.a");
176 add_pattern<Executable>("%");
180 bool Architecture::match_name(const string &pattern) const
182 bool negate = (pattern[0]=='!');
183 vector<string> parts = split(pattern.substr(negate), "-");
184 resolve_aliases(parts);
185 for(const string &p: parts)
187 if((p=="32" && bits==32) || (p=="64" && bits==64))
189 else if(p!=type && p!=cpu && p!=fpu && p!=system && p!=toolchain)
195 string Architecture::best_match(const vector<string> &names) const
198 unsigned best_size = 0;
199 for(const string &n: names)
202 /* TODO Do full parse and alias resolution here? Otherwise x86 and
203 x86_64 are treated as equally good, even though the latter is more
220 void Architecture::add_pattern(const string &pat)
222 filename_patterns[typeid(T).name()].push_back(Pattern(pat));
225 void Architecture::resolve_aliases(vector<string> &parts)
227 for(unsigned i=0; i<parts.size(); ++i)
229 const string &part = parts[i];
230 const char *replace = 0;
231 for(unsigned j=0; (!replace && aliases[j]); j+=2)
233 replace = aliases[j+1];
237 bool has_dash = false;
238 for(const char *c=replace; (!has_dash && *c); ++c)
239 has_dash = (*c=='-');
243 vector<string> rparts = split(replace, "-");
244 parts[i] = rparts[0];
245 parts.insert(parts.begin()+i+1, rparts.begin()+1, rparts.end());
246 i += rparts.size()-1;
254 void Architecture::parse_specification(const string &spec)
256 vector<string> parts = split(spec, "-");
257 resolve_aliases(parts);
258 for(const string &p: parts)
262 for(unsigned j=0; (!ok && types[j]); ++j)
265 if(!type.empty() && p!=type)
266 throw invalid_argument("Conflicting type specification");
271 for(unsigned j=0; (!ok && cpus[j]); j+=2)
276 else if(cpus[j+1]!=type)
277 throw invalid_argument("Conflicting CPU specification");
282 for(unsigned j=0; (!ok && fpus[j]); j+=2)
286 throw invalid_argument("Conflicting FPU specification");
291 for(unsigned j=0; (!ok && systems[j]); ++j)
298 for(unsigned j=0; (!ok && toolchains[j]); ++j)
305 if(!ok && (p=="32" || p=="64"))
307 bits = lexical_cast<unsigned>(p);
312 throw invalid_argument("Unrecognized part in arch specification: "+p);
317 Architecture::Loader::Loader(Architecture &a):
318 DataFile::ObjectLoader<Architecture>(a)
320 add("prefix", &Architecture::cross_prefix);