]> git.tdb.fi Git - builder.git/blob - source/architecture.cpp
Better way to set cross_prefix in architectures
[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(system.empty())
88                         system = native_arch.system;
89                 if(!bits)
90                 {
91                         if(type==native_arch.type)
92                                 bits = native_arch.bits;
93                         else
94                                 bits = 32;
95                 }
96
97                 if(type!=native_arch.type || system!=native_arch.system)
98                         cross_prefix = format("%s-%s", type, system);
99                 else if(bits==native_arch.bits)
100                         native = true;
101         }
102         name = type;
103         if(!cpu.empty())
104                 name += format("-%s", cpu);
105         name += format("-%d-%s", bits, system);
106
107         if(system=="windows")
108         {
109                 sharedlib_patterns.push_back(Pattern("%.dll"));
110                 sharedlib_patterns.push_back(Pattern("lib%.dll"));
111                 staticlib_patterns.push_back(Pattern("lib%.a"));
112                 staticlib_patterns.push_back(Pattern("lib%.dll.a"));
113                 executable_patterns.push_back(Pattern("%.exe"));
114         }
115         else
116         {
117                 sharedlib_patterns.push_back(Pattern("lib%.so"));
118                 staticlib_patterns.push_back(Pattern("lib%.a"));
119                 executable_patterns.push_back(Pattern("%"));
120         }
121 }
122
123 bool Architecture::match_name(const string &pattern) const
124 {
125         vector<string> parts = split(pattern, "-");
126         for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
127         {
128                 string part = resolve_alias(*i);
129                 if((part=="32" && bits==32) || (part=="64" && bits==64))
130                         ;
131                 else if(part!=type && part!=cpu && part!=system)
132                         return false;
133         }
134         return true;
135 }
136
137 string Architecture::resolve_alias(const string &part) const
138 {
139         for(unsigned j=0; aliases[j]; j+=2)
140                 if(part==aliases[j])
141                         return aliases[j+1];
142
143         return part;
144 }
145
146 void Architecture::parse_specification(const string &spec)
147 {
148         vector<string> parts = split(spec, "-");
149         for(vector<string>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
150         {
151                 string part = resolve_alias(*i);
152
153                 bool ok = false;
154
155                 for(unsigned j=0; (!ok && types[j]); ++j)
156                         if(part==types[j])
157                         {
158                                 if(!type.empty() && part!=type)
159                                         throw invalid_argument("Conflicting type specification");
160                                 type = part;
161                                 ok = true;
162                         }
163
164                 for(unsigned j=0; (!ok && cpus[j]); j+=2)
165                         if(part==cpus[j])
166                         {
167                                 if(type.empty())
168                                         type = cpus[j+1];
169                                 else if(cpus[j+1]!=type)
170                                         throw invalid_argument("Conflicting CPU specification");
171                                 cpu = part;
172                                 ok = true;
173                         }
174
175                 for(unsigned j=0; (!ok && systems[j]); ++j)
176                         if(part==systems[j])
177                         {
178                                 system = part;
179                                 ok = true;
180                         }
181
182                 if(!ok && (part=="32" || part=="64"))
183                 {
184                         bits = lexical_cast<unsigned>(part);
185                         ok = true;
186                 }
187
188                 if(!ok)
189                         throw invalid_argument("Unrecognized part in arch specification: "+*i);
190         }
191 }
192
193
194 Architecture::Loader::Loader(Architecture &a):
195         DataFile::ObjectLoader<Architecture>(a)
196 {
197         add("prefix", &Architecture::cross_prefix);
198 }