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[] =
66 Architecture::Architecture(Builder &b, const string &spec):
79 system = tolower(un.sysname);
80 parse_specification(tolower(un.machine));
81 // We really only want to set type for the default arch
85 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
90 parse_specification(spec);
91 const Architecture &native_arch = builder.get_native_arch();
93 type = native_arch.type;
95 cpu = native_arch.cpu;
97 system = native_arch.system;
100 if(type==native_arch.type)
101 bits = native_arch.bits;
106 if(type!=native_arch.type || system!=native_arch.system)
107 cross_prefix = format("%s-%s", type, system);
108 else if(bits!=native_arch.bits)
110 build_info.cflags.push_back(format("-m%d", bits));
111 build_info.ldflags.push_back(format("-m%d", bits));
117 build_info.cflags.push_back(format("-march=%s", cpu));
121 name += format("-%s", cpu);
122 name += format("-%d-%s", bits, system);
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 KeyError("Unknown tool", t);
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!=type && part!=cpu && part!=system)
170 string Architecture::resolve_alias(const string &part) const
172 for(unsigned j=0; aliases[j]; j+=2)
179 void Architecture::parse_specification(const string &spec)
181 vector<string> parts = split(spec, "-");
182 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
184 string part = resolve_alias(*i);
188 for(unsigned j=0; (!ok && types[j]); ++j)
191 if(!type.empty() && part!=type)
192 throw InvalidParameterValue("Conflicting type specification");
197 for(unsigned j=0; (!ok && cpus[j]); j+=2)
202 else if(cpus[j+1]!=type)
203 throw InvalidParameterValue("Conflicting CPU specification");
208 for(unsigned j=0; (!ok && systems[j]); ++j)
215 if(!ok && (part=="32" || part=="64"))
217 bits = lexical_cast<unsigned>(part);
222 throw InvalidParameterValue("Unrecognized part in arch specification: "+*i);
227 Architecture::Loader::Loader(Architecture &a):
230 add("prefix", &Architecture::cross_prefix);
231 add("tool", &Loader::tool);
234 void Architecture::Loader::tool(const string &t, const string &p)