]> git.tdb.fi Git - builder.git/blob - source/architecture.cpp
07f5dbfb0c09159e7fa4fcbb14e7d9e3e6ff03af
[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         "darwin",
45         "windows",
46         "android",
47         0
48 };
49
50 const char *aliases[] =
51 {
52         "x86",   "pc",
53         "i586",  "pentium",
54         "i686",  "pentiumpro",
55         "x86_64", "athlon64",
56         "win32", "windows",
57         0
58 };
59
60 }
61
62 Architecture::Architecture(Builder &b, const string &spec):
63         builder(b),
64         bits(0),
65         native(false)
66 {
67         if(spec.empty())
68         {
69 #ifdef WIN32
70                 system = "windows";
71 #else
72                 utsname un;
73                 if(uname(&un)==0)
74                 {
75                         system = tolower(un.sysname);
76                         parse_specification(tolower(un.machine));
77                         // We really only want to set type for the default arch
78                         cpu.clear();
79                 }
80 #endif
81                 bits = sizeof(void *)*numeric_limits<unsigned char>::digits;
82                 native = true;
83         }
84         else
85         {
86                 parse_specification(spec);
87                 const Architecture &native_arch = builder.get_native_arch();
88                 if(type.empty())
89                         type = native_arch.type;
90                 if(system.empty())
91                         system = native_arch.system;
92                 if(!bits)
93                 {
94                         if(type==native_arch.type)
95                                 bits = native_arch.bits;
96                         else
97                                 bits = 32;
98                 }
99
100                 if(type!=native_arch.type || system!=native_arch.system)
101                         cross_prefix = format("%s-%s", type, system);
102                 else if(bits==native_arch.bits)
103                         native = true;
104         }
105         name = type;
106         if(!cpu.empty())
107                 name += format("-%s", cpu);
108         name += format("-%d-%s", bits, system);
109
110         if(system=="windows")
111         {
112                 sharedlib_patterns.push_back(Pattern("%.dll"));
113                 sharedlib_patterns.push_back(Pattern("lib%.dll"));
114                 /* XXX Hack: Consider import libraries (*.dll.a) as dynamic libraries,
115                 even though technically they are linked statically. */
116                 sharedlib_patterns.push_back(Pattern("lib%.dll.a"));
117                 staticlib_patterns.push_back(Pattern("lib%.a"));
118                 executable_patterns.push_back(Pattern("%.exe"));
119         }
120         else
121         {
122                 if(system=="darwin")
123                         sharedlib_patterns.push_back(Pattern("lib%.dylib"));
124                 else
125                         sharedlib_patterns.push_back(Pattern("lib%.so"));
126                 staticlib_patterns.push_back(Pattern("lib%.a"));
127                 executable_patterns.push_back(Pattern("%"));
128         }
129 }
130
131 bool Architecture::match_name(const string &pattern) const
132 {
133         bool negate = (pattern[0]=='!');
134         vector<string> parts = split(pattern.substr(negate), "-");
135         for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
136         {
137                 string part = resolve_alias(*i);
138                 if((part=="32" && bits==32) || (part=="64" && bits==64))
139                         ;
140                 else if(part!=type && part!=cpu && part!=system)
141                         return negate;
142         }
143         return !negate;
144 }
145
146 string Architecture::resolve_alias(const string &part) const
147 {
148         for(unsigned j=0; aliases[j]; j+=2)
149                 if(part==aliases[j])
150                         return aliases[j+1];
151
152         return part;
153 }
154
155 void Architecture::parse_specification(const string &spec)
156 {
157         vector<string> parts = split(spec, "-");
158         for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
159         {
160                 string part = resolve_alias(*i);
161
162                 bool ok = false;
163
164                 for(unsigned j=0; (!ok && types[j]); ++j)
165                         if(part==types[j])
166                         {
167                                 if(!type.empty() && part!=type)
168                                         throw invalid_argument("Conflicting type specification");
169                                 type = part;
170                                 ok = true;
171                         }
172
173                 for(unsigned j=0; (!ok && cpus[j]); j+=2)
174                         if(part==cpus[j])
175                         {
176                                 if(type.empty())
177                                         type = cpus[j+1];
178                                 else if(cpus[j+1]!=type)
179                                         throw invalid_argument("Conflicting CPU specification");
180                                 cpu = part;
181                                 ok = true;
182                         }
183
184                 for(unsigned j=0; (!ok && systems[j]); ++j)
185                         if(part==systems[j])
186                         {
187                                 system = part;
188                                 ok = true;
189                         }
190
191                 if(!ok && (part=="32" || part=="64"))
192                 {
193                         bits = lexical_cast<unsigned>(part);
194                         ok = true;
195                 }
196
197                 if(!ok)
198                         throw invalid_argument("Unrecognized part in arch specification: "+*i);
199         }
200 }
201
202
203 Architecture::Loader::Loader(Architecture &a):
204         DataFile::ObjectLoader<Architecture>(a)
205 {
206         add("prefix", &Architecture::cross_prefix);
207 }