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[] =
48 const char *aliases[] =
59 Architecture::Architecture(Builder &b, const string &spec):
72 system = tolower(un.sysname);
73 parse_specification(tolower(un.machine));
74 // We really only want to set type for the default arch
78 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
83 parse_specification(spec);
84 const Architecture &native_arch = builder.get_native_arch();
86 type = native_arch.type;
88 cpu = native_arch.cpu;
90 system = native_arch.system;
93 if(type==native_arch.type)
94 bits = native_arch.bits;
99 if(type!=native_arch.type || system!=native_arch.system)
100 cross_prefix = format("%s-%s", type, system);
101 else if(bits==native_arch.bits)
106 name += format("-%s", cpu);
107 name += format("-%d-%s", bits, system);
109 if(system=="windows")
111 sharedlib_patterns.push_back(Pattern("%.dll"));
112 sharedlib_patterns.push_back(Pattern("lib%.dll"));
113 staticlib_patterns.push_back(Pattern("lib%.a"));
114 staticlib_patterns.push_back(Pattern("lib%.dll.a"));
115 executable_patterns.push_back(Pattern("%.exe"));
119 sharedlib_patterns.push_back(Pattern("lib%.so"));
120 staticlib_patterns.push_back(Pattern("lib%.a"));
121 executable_patterns.push_back(Pattern("%"));
125 void Architecture::set_tool(const string &t, const string &p)
130 void Architecture::set_cross_prefix(const string &p)
135 std::string Architecture::get_tool(const string &t) const
137 StringMap::const_iterator i = tools.find(t);
140 if(i->second[0]=='-')
141 return cross_prefix+i->second;
146 const Architecture &native_arch = builder.get_native_arch();
147 if(this!=&native_arch)
149 if(!cross_prefix.empty())
150 return cross_prefix+"-"+native_arch.get_tool(t);
152 return native_arch.get_tool(t);
155 throw invalid_argument("Unknown tool");
158 bool Architecture::match_name(const string &pattern) const
160 vector<string> parts = split(pattern, "-");
161 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
163 string part = resolve_alias(*i);
164 if((part=="32" && bits==32) || (part=="64" && bits==64))
166 else if(part!=type && part!=cpu && part!=system)
172 string Architecture::resolve_alias(const string &part) const
174 for(unsigned j=0; aliases[j]; j+=2)
181 void Architecture::parse_specification(const string &spec)
183 vector<string> parts = split(spec, "-");
184 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
186 string part = resolve_alias(*i);
190 for(unsigned j=0; (!ok && types[j]); ++j)
193 if(!type.empty() && part!=type)
194 throw invalid_argument("Conflicting type specification");
199 for(unsigned j=0; (!ok && cpus[j]); j+=2)
204 else if(cpus[j+1]!=type)
205 throw invalid_argument("Conflicting CPU specification");
210 for(unsigned j=0; (!ok && systems[j]); ++j)
217 if(!ok && (part=="32" || part=="64"))
219 bits = lexical_cast<unsigned>(part);
224 throw invalid_argument("Unrecognized part in arch specification: "+*i);
229 Architecture::Loader::Loader(Architecture &a):
232 add("prefix", &Architecture::cross_prefix);
233 add("tool", &Loader::tool);
236 void Architecture::Loader::tool(const string &t, const string &p)