/
init.cxx
358 lines (307 loc) · 10.6 KB
/
init.cxx
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
#include <libbuild2/rust/init.hxx>
#include <libbuild2/scope.hxx>
#include <libbuild2/variable.hxx>
#include <libbuild2/diagnostics.hxx>
#include <libbuild2/config/utility.hxx>
#include <libbuild2/rust/guess.hxx>
#include <libbuild2/rust/module.hxx>
#include <libbuild2/rust/target.hxx>
namespace build2
{
namespace rust
{
//-
// The `rust.guess` module.
//-
bool
guess_init (scope& rs,
scope& bs,
const location& loc,
bool,
bool,
module_init_extra& extra)
{
tracer trace ("rust::guess_init");
l5 ([&]{trace << "for " << bs;});
// We only support root loading (which means there can only be one).
//
if (rs != bs)
fail (loc) << "rust.guess module must be loaded in project root";
context& ctx (rs.ctx);
// Adjust module config.build save priority (compiler).
//
config::save_module (rs, "rust", 250);
// Configuration.
//
using config::lookup_config;
// All the variables we enter are qualified so go straight for the
// public variable pool.
//
auto& vp (rs.var_pool (true /* public */));
//-
// config.rust [strings]
//
// The Rust compiler and mode options. The default is `rustc`.
//
// The compiler mode is the place to specify options such as `+nightly`,
// `--target`, etc., that must be in effect for the entire build and
// that should not be overridden by the buildfiles.
//
// -
bool new_cfg (false);
process_path ppath;
strings mode;
{
auto& var (vp.insert<strings> ("config.rust"));
const auto& val (
cast<strings> (
lookup_config (new_cfg, rs, var, strings {"rustc"})));
// Split the value into the compiler path and mode.
//
const string& s (val.empty () ? string () : val.front ());
path p; try { p = path (s); } catch (const invalid_path&) {}
if (p.empty ())
fail << "invalid path '" << s << "' in " << var;
// Only search in PATH (specifically, omitting the current
// executable's directory on Windows).
//
// Note that the process_path value will be cached so init is false.
//
ppath = run_search (p,
false /* init */,
dir_path () /* fallback */,
true /* path_only */);
mode.assign (++val.begin (), val.end ());
}
// Extract compiler information (version, target, etc).
//
const compiler_info& ci (guess (ppath, mode));
// Hash the environment (used for change detection).
//
string env_cs (hash_environment (ci.environment));
// Split/canonicalize the target.
//
target_triplet tt;
{
// First see if the user asked us to use config.sub.
//
string ct;
if (config_sub)
ct = run<string> (ctx,
3,
*config_sub, ci.target.c_str (),
[] (string& l, bool) {return move (l);});
try
{
tt = target_triplet (ct.empty () ? ci.target : ct);
}
catch (const invalid_argument& e)
{
// This is where we suggest that the user specifies --config-sub to
// help us out.
//
fail << "unable to parse " << ppath << " compiler target '"
<< ci.target << "': " << e <<
info << "consider using the --config-sub option";
}
}
//-
// rust.path [process_path]
// rust.mode [strings]
//
// Compiler process path and mode options. These are components of
// the `config.rust` value.
// -
auto& r_path (rs.assign<process_path_ex> (
"rust.path",
process_path_ex (
move (ppath), "rust", ci.checksum, env_cs)));
auto& r_mode (rs.assign<strings> ("rust.mode", move (mode)));
//-
//
// rust.signature [string]
// rust.checksum [string]
//
// Compiler signature line and checksum.
//
// The signature is the `--version` line that was used to extract the
// compiler version.
//
// The checksum is used to detect compiler changes. It is calculated
// based on the `--version -v` output and the target information. Note
// that it's not bulletproof, for example, it most likely won't detect
// that the underlying linker has changed. However, it should detect
// most common cases, such as an upgrade to a new version or a
// configuration change.
//
// Note also that the above output is obtained with the compiler mode
// options but without the compile option. In particular, this means
// that `--target` should (naturally) be specified as part of
// `config.rust` and not `config.rust.coptions`.
//
// -
rs.assign<string> ("rust.signature") = ci.signature;
rs.assign<string> ("rust.checksum") = ci.checksum;
//-
// rust.version [string]
// rust.version.major [uint64]
// rust.version.minor [uint64]
// rust.version.patch [uint64]
// rust.version.build [string]
//
// Compiler version. These are extracted from the compiler's `--version`
// output.
//
//-
rs.assign<string> ("rust.version") = ci.version.string ();
rs.assign<uint64_t> ("rust.version.major") = ci.version.major;
rs.assign<uint64_t> ("rust.version.minor") = ci.version.minor;
rs.assign<uint64_t> ("rust.version.patch") = ci.version.patch;
rs.assign<string> ("rust.version.build") = ci.version.build;
//-
// rust.target [target_triplet]
// rust.target.cpu [string]
// rust.target.vendor [string]
// rust.target.system [string]
// rust.target.version [string]
// rust.target.class [string]
//
// The compiler's target architecture triplet, either the default or as
// specified with `--target`. Individual components are also available
// for convenience of access. Refer to the `target-triplet` module in
// `libbutl` for details.
//
// -
rs.assign<string> ("rust.target.cpu") = tt.cpu;
rs.assign<string> ("rust.target.vendor") = tt.vendor;
rs.assign<string> ("rust.target.system") = tt.system;
rs.assign<string> ("rust.target.version") = tt.version;
rs.assign<string> ("rust.target.class") = tt.class_;
auto& r_target (rs.assign<target_triplet> ("rust.target", move (tt)));
// Cache some values in the module for easier access in the rule.
//
extra.set_module (
new module (
data {
r_path,
r_mode,
r_target,
r_target.system,
r_target.class_,
move (env_cs)},
new_cfg,
ci));
return true;
}
//-
// The `rust.config` module.
//-
bool
config_init (scope& rs,
scope& bs,
const location& loc,
bool,
bool,
module_init_extra& extra)
{
tracer trace ("rust::const_init");
l5 ([&]{trace << "for " << bs;});
if (rs != bs)
fail (loc) << "rust.config module must be loaded in project root";
// Load rust.guess and share its module instance as ours.
//
extra.module = load_module (rs, rs, "rust.guess", loc, extra.hints);
auto& m (extra.module_as<module> ());
const compiler_info& ci (m.cinfo);
const target_triplet& tt (m.r_target);
// If this is a new value (e.g., we are configuring), then print the
// report at verbosity level 2 and up (-v).
//
if (verb >= (m.new_cfg ? 2 : 3))
{
diag_record dr (text);
{
dr << "rust " << project (rs) << '@' << rs << '\n'
<< " rustc " << m.r_path << '\n';
}
if (!m.r_mode.empty ())
{
dr << " mode "; // One space short.
for (const string& o: m.r_mode)
dr << ' ' << o;
dr << '\n';
}
{
dr << " version " << ci.version.string () << '\n'
<< " major " << ci.version.major << '\n'
<< " minor " << ci.version.minor << '\n'
<< " patch " << ci.version.patch << '\n';
}
if (!ci.version.build.empty ())
{
dr << " build " << ci.version.build << '\n';
}
{
const string& ct (tt.string ()); // Canonical target.
dr << " signature " << ci.signature << '\n'
<< " checksum " << ci.checksum << '\n'
<< " target " << ct;
if (ct != ci.target)
dr << " (" << ci.target << ")";
}
}
config::save_environment (rs, ci.environment);
return true;
}
//-
// The `rust` module.
//-
bool
init (scope& rs,
scope& bs,
const location& loc,
bool,
bool,
module_init_extra& extra)
{
tracer trace ("rust::init");
l5 ([&]{trace << "for " << bs;});
if (rs != bs)
fail (loc) << "rust module must be loaded in project root";
// Load rust.config and share its module instance as ours.
//
extra.module = load_module (rs, rs, "rust.config", loc, extra.hints);
auto& m (extra.module_as<module> ());
//-
// Target types:
//
// `rs{}` -- Rust source file.
//-
rs.insert_target_type<rust::rs> ();
//-
// Rules:
//
// `rust.compile` -- Compile a Rust crate identified as a first `rs{}`
// prerequisite.
//-
rs.insert_rule<exe> (perform_update_id, "rust.compile", m);
rs.insert_rule<exe> (perform_clean_id, "rust.compile", m);
rs.insert_rule<exe> (configure_update_id, "rust.compile", m);
return true;
}
static const module_functions mod_functions[] =
{
// NOTE: don't forget to also update the documentation in init.hxx if
// changing anything here.
{"rust.guess", nullptr, guess_init},
{"rust.config", nullptr, config_init},
{"rust", nullptr, init},
{nullptr, nullptr, nullptr}
};
const module_functions*
build2_rust_load ()
{
return mod_functions;
}
}
}