3 This file is part of builder
4 Copyright © 2007-2010 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
10 #include <sys/utsname.h>
12 #include <msp/strings/formatter.h>
13 #include <msp/strings/utils.h>
14 #include "architecture.h"
47 const char *systems[] =
55 const char *aliases[] =
65 Architecture::Architecture(Builder &b, const string &spec):
78 system = tolower(un.sysname);
79 parse_specification(tolower(un.machine));
80 // We really only want to set type for the default arch
84 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
89 parse_specification(spec);
90 const Architecture &native_arch = builder.get_native_arch();
92 type = native_arch.type;
94 cpu = native_arch.cpu;
96 system = native_arch.system;
99 if(type==native_arch.type)
100 bits = native_arch.bits;
105 if(type!=native_arch.type || system!=native_arch.system)
106 cross_prefix = format("%s-%s", type, system);
107 else if(bits!=native_arch.bits)
109 build_info.cflags.push_back(format("-m%d", bits));
110 build_info.ldflags.push_back(format("-m%d", bits));
116 build_info.cflags.push_back(format("-march=%s", cpu));
120 name += format("-%s", cpu);
121 name += format("-%d-%s", bits, system);
124 void Architecture::set_tool(const string &t, const string &p)
129 void Architecture::set_cross_prefix(const string &p)
134 std::string Architecture::get_tool(const string &t) const
136 StringMap::const_iterator i = tools.find(t);
139 if(i->second[0]=='-')
140 return cross_prefix+i->second;
145 const Architecture &native_arch = builder.get_native_arch();
146 if(this!=&native_arch)
148 if(!cross_prefix.empty())
149 return cross_prefix+"-"+native_arch.get_tool(t);
151 return native_arch.get_tool(t);
154 throw KeyError("Unknown tool", t);
157 bool Architecture::match_name(const string &pattern) const
159 vector<string> parts = split(pattern, "-");
160 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
162 string part = resolve_alias(*i);
163 if(part!=type && part!=cpu && part!=system)
169 string Architecture::resolve_alias(const string &part) const
171 for(unsigned j=0; aliases[j]; j+=2)
178 void Architecture::parse_specification(const string &spec)
180 vector<string> parts = split(spec, "-");
181 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
183 string part = resolve_alias(*i);
187 for(unsigned j=0; (!ok && types[j]); ++j)
190 if(!type.empty() && part!=type)
191 throw InvalidParameterValue("Conflicting type specification");
196 for(unsigned j=0; (!ok && cpus[j]); j+=2)
201 else if(cpus[j+1]!=type)
202 throw InvalidParameterValue("Conflicting CPU specification");
207 for(unsigned j=0; (!ok && systems[j]); ++j)
214 if(!ok && (part=="32" || part=="64"))
216 bits = lexical_cast<unsigned>(part);
221 throw InvalidParameterValue("Unrecognized part in arch specification: "+*i);
226 Architecture::Loader::Loader(Architecture &a):
229 add("prefix", &Architecture::cross_prefix);
230 add("tool", &Loader::tool);
233 void Architecture::Loader::tool(const string &t, const string &p)