1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
// file : build2/cc/init.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#include <build2/cc/init>
#include <butl/triplet>
#include <build2/scope>
#include <build2/context>
#include <build2/diagnostics>
#include <build2/config/utility>
#include <build2/cc/target>
using namespace std;
using namespace butl;
namespace build2
{
namespace cc
{
bool
core_vars_init (scope& r,
scope&,
const location&,
unique_ptr<module_base>&,
bool first,
bool,
const variable_map&)
{
tracer trace ("cc::core_vars_init");
l5 ([&]{trace << "for " << r.out_path ();});
assert (first);
// Enter variables. Note: some overridable, some not.
//
auto& v (var_pool);
v.insert<strings> ("config.cc.poptions", true);
v.insert<strings> ("config.cc.coptions", true);
v.insert<strings> ("config.cc.loptions", true);
v.insert<strings> ("config.cc.libs", true);
v.insert<strings> ("cc.poptions");
v.insert<strings> ("cc.coptions");
v.insert<strings> ("cc.loptions");
v.insert<strings> ("cc.libs");
v.insert<strings> ("cc.export.poptions");
v.insert<strings> ("cc.export.coptions");
v.insert<strings> ("cc.export.loptions");
v.insert<names> ("cc.export.libs");
// Hint variables (not overridable).
//
v.insert<string> ("config.cc.id");
v.insert<string> ("config.cc.target");
v.insert<string> ("config.cc.pattern");
// Target type, for example, "C library" or "C++ library". Should be set
// on the target by the matching rule to the name of the module (e.g.,
// "c", "cxx"). Currenly only set for libraries and is used to decide
// which *.libs to use during static linking.
//
v.insert<string> ("cc.type");
return true;
}
bool
core_config_init (scope& r,
scope& b,
const location& loc,
unique_ptr<module_base>&,
bool first,
bool,
const variable_map& hints)
{
tracer trace ("cc::core_config_init");
l5 ([&]{trace << "for " << b.out_path ();});
// Load cc.core.vars.
//
if (first)
{
if (!cast_false<bool> (b["cc.core.vars.loaded"]))
load_module ("cc.core.vars", r, b, loc);
}
// Configure.
//
if (first)
{
// Adjust module priority (compiler).
//
config::save_module (r, "cc", 250);
// config.cc.id
//
{
// This value must be hinted.
//
r.assign<string> ("cc.id") = cast<string> (hints["config.cc.id"]);
}
// config.cc.target
//
{
// This value must be hinted and already canonicalized.
//
const string& s (cast<string> (hints["config.cc.target"]));
try
{
//@@ We do it in the hinting module and here. Any way not to
// duplicate the effort? Maybe move the splitting here and
// simply duplicate the values there?
//
triplet t (s);
// Enter as cc.target.{cpu,vendor,system,version,class}.
//
r.assign<string> ("cc.target") = s;
r.assign<string> ("cc.target.cpu") = move (t.cpu);
r.assign<string> ("cc.target.vendor") = move (t.vendor);
r.assign<string> ("cc.target.system") = move (t.system);
r.assign<string> ("cc.target.version") = move (t.version);
r.assign<string> ("cc.target.class") = move (t.class_);
}
catch (const invalid_argument& e)
{
assert (false); // Should have been caught by the hinting module.
}
}
// config.cc.pattern
//
{
// This value could be hinted.
//
if (auto l = hints["config.cc.pattern"])
r.assign<string> ("cc.pattern") = cast<string> (l);
}
// Note that we are not having a config report since it will just
// duplicate what has already been printed by the hinting module.
}
// config.cc.{p,c,l}options
// config.cc.libs
//
// @@ Same nonsense as in module.
//
//
b.assign ("cc.poptions") += cast_null<strings> (
config::optional (r, "config.cc.poptions"));
b.assign ("cc.coptions") += cast_null<strings> (
config::optional (r, "config.cc.coptions"));
b.assign ("cc.loptions") += cast_null<strings> (
config::optional (r, "config.cc.loptions"));
b.assign ("cc.libs") += cast_null<strings> (
config::optional (r, "config.cc.libs"));
// Load the bin.config module.
//
if (!cast_false<bool> (b["bin.config.loaded"]))
{
// Prepare configuration hints. They are only used on the first load
// of bin.config so we only populate them on our first load.
//
variable_map h;
if (first)
{
h.assign ("config.bin.target") = cast<string> (r["cc.target"]);
if (auto l = hints["config.bin.pattern"])
h.assign ("config.bin.pattern") = cast<string> (l);
}
load_module ("bin.config", r, b, loc, false, h);
}
// Verify bin's target matches ours (we do it even if we loaded it
// ourselves since the target can come from the configuration and not
// our hint).
//
if (first)
{
const string& ct (cast<string> (r["cc.target"]));
const string& bt (cast<string> (r["bin.target"]));
if (bt != ct)
fail (loc) << "cc and bin module target mismatch" <<
info << "cc.target is " << ct <<
info << "bin.target is " << bt;
}
const string& cid (cast<string> (r["cc.id"]));
const string& tsys (cast<string> (r["cc.target.system"]));
// Load bin.*.config for bin.* modules we may need (see core_init()
// below).
//
if (auto l = r["config.bin.lib"])
{
if (cast<string> (l) != "shared")
{
if (!cast_false<bool> (b["bin.ar.config.loaded"]))
load_module ("bin.ar.config", r, b, loc);
}
}
if (cid == "msvc")
{
if (!cast_false<bool> (b["bin.ld.config.loaded"]))
load_module ("bin.ld.config", r, b, loc);
}
if (tsys == "mingw32")
{
if (!cast_false<bool> (b["bin.rc.config.loaded"]))
load_module ("bin.rc.config", r, b, loc);
}
return true;
}
bool
core_init (scope& r,
scope& b,
const location& loc,
unique_ptr<module_base>&,
bool,
bool,
const variable_map& hints)
{
tracer trace ("cc::core_init");
l5 ([&]{trace << "for " << b.out_path ();});
// Load cc.core.config.
//
if (!cast_false<bool> (b["cc.core.config.loaded"]))
load_module ("cc.core.config", r, b, loc, false, hints);
// Load the bin module.
//
if (!cast_false<bool> (b["bin.loaded"]))
load_module ("bin", r, b, loc);
const string& cid (cast<string> (r["cc.id"]));
const string& tsys (cast<string> (r["cc.target.system"]));
// Load the bin.ar module unless we were asked to only build shared
// libraries.
//
if (auto l = r["config.bin.lib"])
{
if (cast<string> (l) != "shared")
{
if (!cast_false<bool> (b["bin.ar.loaded"]))
load_module ("bin.ar", r, b, loc);
}
}
// In the VC world you link things directly with link.exe so load the
// bin.ld module.
//
if (cid == "msvc")
{
if (!cast_false<bool> (b["bin.ld.loaded"]))
load_module ("bin.ld", r, b, loc);
}
// If our target is MinGW, then we will need the resource compiler
// (windres) in order to embed manifests into executables.
//
if (tsys == "mingw32")
{
if (!cast_false<bool> (b["bin.rc.loaded"]))
load_module ("bin.rc", r, b, loc);
}
return true;
}
// The cc module is an "alias" for c and cxx. Its intended use is to make
// sure that the C/C++ configuration is captured in an amalgamation rather
// than subprojects.
//
static inline bool
init_alias (tracer& trace,
const char* c,
const char* c_loaded,
const char* cxx,
const char* cxx_loaded,
scope& r,
scope& b,
const location& loc,
const variable_map& hints)
{
l5 ([&]{trace << "for " << b.out_path ();});
// We want to order the loading to match what user specified on the
// command line (config.c or config.cxx). This way the first loaded
// module (with user-specified config.*) will hint the compiler to the
// second.
//
bool lc (!cast_false<bool> (b[c_loaded]));
bool lp (!cast_false<bool> (b[cxx_loaded]));
// If none of them are already loaded, load c first only if config.c
// is specified.
//
if (lc && lp && r["config.c"])
{
load_module (c, r, b, loc, false, hints);
load_module (cxx, r, b, loc, false, hints);
}
else
{
if (lp) load_module (cxx, r, b, loc, false, hints);
if (lc) load_module (c, r, b, loc, false, hints);
}
return true;
}
bool
config_init (scope& r,
scope& b,
const location& loc,
unique_ptr<module_base>&,
bool,
bool,
const variable_map& hints)
{
tracer trace ("cc::config_init");
return init_alias (trace,
"c.config", "c.config.loaded",
"cxx.config", "cxx.config.loaded",
r, b,
loc,
hints);
}
bool
init (scope& r,
scope& b,
const location& loc,
unique_ptr<module_base>&,
bool,
bool,
const variable_map& hints)
{
tracer trace ("cc::init");
return init_alias (trace,
"c", "c.loaded",
"cxx", "cxx.loaded",
r, b,
loc,
hints);
}
}
}
|