3 #include <sys/utsname.h>
5 #include <msp/strings/format.h>
6 #include <msp/strings/utils.h>
7 #include "architecture.h"
40 const char *systems[] =
50 const char *aliases[] =
62 Architecture::Architecture(Builder &b, const string &spec):
75 system = tolower(un.sysname);
76 parse_specification(tolower(un.machine));
77 // We really only want to set type for the default arch
81 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
86 parse_specification(spec);
87 const Architecture &native_arch = builder.get_native_arch();
89 type = native_arch.type;
91 system = native_arch.system;
94 if(type==native_arch.type)
95 bits = native_arch.bits;
100 if(type!=native_arch.type || system!=native_arch.system)
101 cross_prefix = format("%s-%s", type, system);
102 else if(bits==native_arch.bits)
107 name += format("-%s", cpu);
108 name += format("-%d-%s", bits, system);
110 if(system=="windows")
112 sharedlib_patterns.push_back(Pattern("%.dll"));
113 sharedlib_patterns.push_back(Pattern("lib%.dll"));
114 /* XXX Hack: Consider import libraries (*.dll.a) as dynamic libraries,
115 even though technically they are linked statically. */
116 sharedlib_patterns.push_back(Pattern("lib%.dll.a"));
117 staticlib_patterns.push_back(Pattern("lib%.a"));
118 executable_patterns.push_back(Pattern("%.exe"));
123 sharedlib_patterns.push_back(Pattern("lib%.dylib"));
125 sharedlib_patterns.push_back(Pattern("lib%.so"));
126 staticlib_patterns.push_back(Pattern("lib%.a"));
127 executable_patterns.push_back(Pattern("%"));
131 bool Architecture::match_name(const string &pattern) const
133 bool negate = (pattern[0]=='!');
134 vector<string> parts = split(pattern.substr(negate), "-");
135 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
137 string part = resolve_alias(*i);
138 if((part=="32" && bits==32) || (part=="64" && bits==64))
140 else if(part!=type && part!=cpu && part!=system)
146 string Architecture::resolve_alias(const string &part) const
148 for(unsigned j=0; aliases[j]; j+=2)
155 void Architecture::parse_specification(const string &spec)
157 vector<string> parts = split(spec, "-");
158 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
160 string part = resolve_alias(*i);
164 for(unsigned j=0; (!ok && types[j]); ++j)
167 if(!type.empty() && part!=type)
168 throw invalid_argument("Conflicting type specification");
173 for(unsigned j=0; (!ok && cpus[j]); j+=2)
178 else if(cpus[j+1]!=type)
179 throw invalid_argument("Conflicting CPU specification");
184 for(unsigned j=0; (!ok && systems[j]); ++j)
191 if(!ok && (part=="32" || part=="64"))
193 bits = lexical_cast<unsigned>(part);
198 throw invalid_argument("Unrecognized part in arch specification: "+*i);
203 Architecture::Loader::Loader(Architecture &a):
204 DataFile::ObjectLoader<Architecture>(a)
206 add("prefix", &Architecture::cross_prefix);