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[] =
49 const char *aliases[] =
60 Architecture::Architecture(Builder &b, const string &spec):
73 system = tolower(un.sysname);
74 parse_specification(tolower(un.machine));
75 // We really only want to set type for the default arch
79 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
84 parse_specification(spec);
85 const Architecture &native_arch = builder.get_native_arch();
87 type = native_arch.type;
89 system = native_arch.system;
92 if(type==native_arch.type)
93 bits = native_arch.bits;
98 if(type!=native_arch.type || system!=native_arch.system)
99 cross_prefix = format("%s-%s", type, system);
100 else if(bits==native_arch.bits)
105 name += format("-%s", cpu);
106 name += format("-%d-%s", bits, system);
108 if(system=="windows")
110 sharedlib_patterns.push_back(Pattern("%.dll"));
111 sharedlib_patterns.push_back(Pattern("lib%.dll"));
112 /* XXX Hack: Consider import libraries (*.dll.a) as dynamic libraries,
113 even though technically they are linked statically. */
114 sharedlib_patterns.push_back(Pattern("lib%.dll.a"));
115 staticlib_patterns.push_back(Pattern("lib%.a"));
116 executable_patterns.push_back(Pattern("%.exe"));
121 sharedlib_patterns.push_back(Pattern("lib%.dylib"));
123 sharedlib_patterns.push_back(Pattern("lib%.so"));
124 staticlib_patterns.push_back(Pattern("lib%.a"));
125 executable_patterns.push_back(Pattern("%"));
129 bool Architecture::match_name(const string &pattern) const
131 bool negate = (pattern[0]=='!');
132 vector<string> parts = split(pattern.substr(negate), "-");
133 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
135 string part = resolve_alias(*i);
136 if((part=="32" && bits==32) || (part=="64" && bits==64))
138 else if(part!=type && part!=cpu && part!=system)
144 string Architecture::resolve_alias(const string &part) const
146 for(unsigned j=0; aliases[j]; j+=2)
153 void Architecture::parse_specification(const string &spec)
155 vector<string> parts = split(spec, "-");
156 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
158 string part = resolve_alias(*i);
162 for(unsigned j=0; (!ok && types[j]); ++j)
165 if(!type.empty() && part!=type)
166 throw invalid_argument("Conflicting type specification");
171 for(unsigned j=0; (!ok && cpus[j]); j+=2)
176 else if(cpus[j+1]!=type)
177 throw invalid_argument("Conflicting CPU specification");
182 for(unsigned j=0; (!ok && systems[j]); ++j)
189 if(!ok && (part=="32" || part=="64"))
191 bits = lexical_cast<unsigned>(part);
196 throw invalid_argument("Unrecognized part in arch specification: "+*i);
201 Architecture::Loader::Loader(Architecture &a):
202 DataFile::ObjectLoader<Architecture>(a)
204 add("prefix", &Architecture::cross_prefix);