]> git.tdb.fi Git - builder.git/blob - source/architecture.cpp
Let the tools deal with cross-compiling flags
[builder.git] / source / architecture.cpp
1 #include <limits>
2 #ifndef WIN32
3 #include <sys/utsname.h>
4 #endif
5 #include <msp/strings/format.h>
6 #include <msp/strings/utils.h>
7 #include "architecture.h"
8 #include "builder.h"
9
10 using namespace std;
11 using namespace Msp;
12
13 namespace {
14
15 const char *types[] =
16 {
17         "pc",
18         "arm",
19         0
20 };
21
22 const char *cpus[] =
23 {
24         "i386",       "pc",
25         "i486",       "pc",
26         "pentium",    "pc",
27         "pentiumpro", "pc",
28         "pentium2",   "pc",
29         "pentium3",   "pc",
30         "pentium4",   "pc",
31         "core2",      "pc",
32         "k6",         "pc",
33         "athlon",     "pc",
34         "athlonxp",   "pc",
35         "athlon64",   "pc",
36         "armv5",      "arm",
37         0
38 };
39
40 const char *systems[] =
41 {
42         "linux",
43         "freebsd",
44         "windows",
45         0
46 };
47
48 const char *aliases[] =
49 {
50         "i586",  "pentium",
51         "i686",  "pentiumpro",
52         "x86_64", "athlon64",
53         "win32", "windows",
54         0
55 };
56
57 }
58
59 Architecture::Architecture(Builder &b, const string &spec):
60         builder(b),
61         bits(0),
62         native(false)
63 {
64         if(spec.empty())
65         {
66 #ifdef WIN32
67                 system = "windows";
68 #else
69                 utsname un;
70                 if(uname(&un)==0)
71                 {
72                         system = tolower(un.sysname);
73                         parse_specification(tolower(un.machine));
74                         // We really only want to set type for the default arch
75                         cpu.clear();
76                 }
77 #endif
78                 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
79                 native = true;
80         }
81         else
82         {
83                 parse_specification(spec);
84                 const Architecture &native_arch = builder.get_native_arch();
85                 if(type.empty())
86                         type = native_arch.type;
87                 if(cpu.empty())
88                         cpu = native_arch.cpu;
89                 if(system.empty())
90                         system = native_arch.system;
91                 if(!bits)
92                 {
93                         if(type==native_arch.type)
94                                 bits = native_arch.bits;
95                         else
96                                 bits = 32;
97                 }
98
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)
102                         native = true;
103         }
104         name = type;
105         if(!cpu.empty())
106                 name += format("-%s", cpu);
107         name += format("-%d-%s", bits, system);
108 }
109
110 void Architecture::set_tool(const string &t, const string &p)
111 {
112         tools[t] = p;
113 }
114
115 void Architecture::set_cross_prefix(const string &p)
116 {
117         cross_prefix = p;
118 }
119
120 std::string Architecture::get_tool(const string &t) const
121 {
122         StringMap::const_iterator i = tools.find(t);
123         if(i!=tools.end())
124         {
125                 if(i->second[0]=='-')
126                         return cross_prefix+i->second;
127                 else
128                         return i->second;
129         }
130
131         const Architecture &native_arch = builder.get_native_arch();
132         if(this!=&native_arch)
133         {
134                 if(!cross_prefix.empty())
135                         return cross_prefix+"-"+native_arch.get_tool(t);
136                 else
137                         return native_arch.get_tool(t);
138         }
139         else
140                 throw invalid_argument("Unknown tool");
141 }
142
143 bool Architecture::match_name(const string &pattern) const
144 {
145         vector<string> parts = split(pattern, "-");
146         for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
147         {
148                 string part = resolve_alias(*i);
149                 if((part=="32" && bits==32) || (part=="64" && bits==64))
150                         ;
151                 else if(part!=type && part!=cpu && part!=system)
152                         return false;
153         }
154         return true;
155 }
156
157 string Architecture::resolve_alias(const string &part) const
158 {
159         for(unsigned j=0; aliases[j]; j+=2)
160                 if(part==aliases[j])
161                         return aliases[j+1];
162
163         return part;
164 }
165
166 void Architecture::parse_specification(const string &spec)
167 {
168         vector<string> parts = split(spec, "-");
169         for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
170         {
171                 string part = resolve_alias(*i);
172
173                 bool ok = false;
174
175                 for(unsigned j=0; (!ok && types[j]); ++j)
176                         if(part==types[j])
177                         {
178                                 if(!type.empty() && part!=type)
179                                         throw invalid_argument("Conflicting type specification");
180                                 type = part;
181                                 ok = true;
182                         }
183
184                 for(unsigned j=0; (!ok && cpus[j]); j+=2)
185                         if(part==cpus[j])
186                         {
187                                 if(type.empty())
188                                         type = cpus[j+1];
189                                 else if(cpus[j+1]!=type)
190                                         throw invalid_argument("Conflicting CPU specification");
191                                 cpu = part;
192                                 ok = true;
193                         }
194
195                 for(unsigned j=0; (!ok && systems[j]); ++j)
196                         if(part==systems[j])
197                         {
198                                 system = part;
199                                 ok = true;
200                         }
201
202                 if(!ok && (part=="32" || part=="64"))
203                 {
204                         bits = lexical_cast<unsigned>(part);
205                         ok = true;
206                 }
207
208                 if(!ok)
209                         throw invalid_argument("Unrecognized part in arch specification: "+*i);
210         }
211 }
212
213
214 Architecture::Loader::Loader(Architecture &a):
215         arch(a)
216 {
217         add("prefix", &Architecture::cross_prefix);
218         add("tool",   &Loader::tool);
219 }
220
221 void Architecture::Loader::tool(const string &t, const string &p)
222 {
223         arch.tools[t] = p;
224 }