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