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"
34 "i586", "pc", "pentium",
35 "pentiumpro", "pc", 0,
36 "i686", "pc", "pentiumpro",
49 const char *systems[] =
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)
103 build_info.cflags.push_back(format("-m%d", bits));
104 build_info.ldflags.push_back(format("-m%d", bits));
110 build_info.cflags.push_back(format("-march=%s", cpu));
114 name += format("-%s", cpu);
115 name += format("-%d-%s", bits, system);
118 void Architecture::set_tool(const string &t, const string &p)
123 void Architecture::set_cross_prefix(const string &p)
128 std::string Architecture::get_tool(const string &t) const
130 StringMap::const_iterator i = tools.find(t);
133 if(i->second[0]=='-')
134 return cross_prefix+i->second;
139 const Architecture &native_arch = builder.get_native_arch();
140 if(this!=&native_arch)
142 if(!cross_prefix.empty())
143 return cross_prefix+"-"+native_arch.get_tool(t);
145 return native_arch.get_tool(t);
148 throw KeyError("Unknown tool", t);
151 bool Architecture::match_name(const string &pattern) const
153 vector<string> parts = split(pattern, "-");
154 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
156 if(*i!=type && *i!=cpu && *i!=system)
162 void Architecture::parse_specification(const string &spec)
164 vector<string> parts = split(spec, "-");
165 for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
168 for(unsigned j=0; (!ok && types[j]); ++j)
171 if(!type.empty() && *i!=type)
172 throw InvalidParameterValue("Conflicting type specification");
176 for(unsigned j=0; (!ok && cpus[j]); j+=3)
181 else if(cpus[j+1]!=type)
182 throw InvalidParameterValue("Conflicting CPU specification");
183 cpu = (cpus[j+2] ? cpus[j+2] : *i);
186 for(unsigned j=0; (!ok && systems[j]); ++j)
192 if(!ok && (*i=="32" || *i=="64"))
194 bits = lexical_cast<unsigned>(*i);
198 throw InvalidParameterValue("Unrecognized part in arch specification: "+*i);
203 Architecture::Loader::Loader(Architecture &a):
206 add("prefix", &Architecture::cross_prefix);
207 add("tool", &Loader::tool);
210 void Architecture::Loader::tool(const string &t, const string &p)