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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
|
// file : libbuild2/rule.cxx -*- C++ -*-
// license : MIT; see accompanying LICENSE file
#include <libbuild2/rule.hxx>
#include <libbuild2/scope.hxx>
#include <libbuild2/target.hxx>
#include <libbuild2/context.hxx>
#include <libbuild2/algorithm.hxx>
#include <libbuild2/filesystem.hxx>
#include <libbuild2/diagnostics.hxx>
using namespace std;
using namespace butl;
namespace build2
{
// rule
//
rule::
~rule ()
{
}
void rule::
apply_posthoc (action, target&, match_extra&) const
{
}
void rule::
reapply (action, target&, match_extra&) const
{
// Unless the rule overrode cur_options, this function should never get
// called. And if it did, then it should override this function.
//
assert (false);
}
const target* rule::
import (const prerequisite_key&,
const optional<string>&,
const location&) const
{
return nullptr;
}
const rule_match*
match_adhoc_recipe (action, target&, match_extra&); // algorithm.cxx
bool rule::
sub_match (const string& n, operation_id o,
action a, target& t, match_extra& me) const
{
// First check for an ad hoc recipe (see match_rule_impl() for details).
//
if (!t.adhoc_recipes.empty ())
{
// Use scratch match_extra since if there is no recipe, then we don't
// want to keep any changes and if there is, then we want it discarded.
//
match_extra s (true /* locked */); // Not called from adhoc_rule::match().
if (match_adhoc_recipe (action (a.meta_operation (), o), t, s) != nullptr)
return false;
}
const string& h (t.find_hint (o));
return name_rule_map::sub (h, n) && match (a, t, h, me);
}
// simple_rule
//
bool simple_rule::
match (action a, target& t, const string&, match_extra&) const
{
return match (a, t);
}
recipe simple_rule::
apply (action a, target& t, match_extra&) const
{
return apply (a, t);
}
bool simple_rule::
sub_match (const string& n, operation_id o,
action a, target& t) const
{
if (!t.adhoc_recipes.empty ())
{
match_extra s (true /* locked */); // Not called from adhoc_rule::match().
if (match_adhoc_recipe (action (a.meta_operation (), o), t, s) != nullptr)
return false;
}
return name_rule_map::sub (t.find_hint (o), n) && match (a, t);
}
// file_rule
//
// Note that this rule is special. It is the last, fallback rule. If
// it doesn't match, then no other rule can possibly match and we have
// an error. It also cannot be ambigious with any other rule. As a
// result the below implementation bends or ignores quite a few rules
// that normal implementations should follow. So you probably shouldn't
// use it as a guide to implement your own, normal, rules.
//
bool file_rule::
match (action a, target& t, const string&, match_extra&) const
{
tracer trace ("file_rule::match");
if (match_type_ && !t.is_a<mtime_target> ())
return false;
// While strictly speaking we should check for the file's existence
// for every action (because that's the condition for us matching),
// for some actions this is clearly a waste. Say, perform_clean: we
// are not doing anything for this action so not checking if the file
// exists seems harmless.
//
// But we also don't want to match real targets and not cleaning their
// output files.
//
switch (a)
{
case perform_clean_id:
return t.decl != target_decl::real;
default:
{
// While normally we shouldn't do any of this in match(), no other
// rule should ever be ambiguous with the fallback one and path/mtime
// access is atomic. In other words, we know what we are doing but
// don't do this in normal rules.
// First check the timestamp. This takes care of the special "trust
// me, this file exists" situations (used, for example, for installed
// stuff where we know it's there, just not exactly where).
//
// See also path_target::path_mtime() for a potential race in this
// logic.
//
mtime_target& mt (t.as<mtime_target> ());
timestamp ts (mt.mtime ());
if (ts != timestamp_unknown)
return ts != timestamp_nonexistent;
// Otherwise, if this is not a path_target, then we don't match.
//
path_target* pt (mt.is_a<path_target> ());
if (pt == nullptr)
return false;
const path* p (&pt->path ());
// Assign the path.
//
if (p->empty ())
{
// Since we cannot come up with an extension, ask the target's
// derivation function to treat this as a prerequisite (just like in
// search_existing_file()).
//
if (const string* e = pt->derive_extension (true))
{
p = &pt->derive_path_with_extension (*e);
}
else
{
l4 ([&]{trace << "no default extension for target " << *pt;});
return false;
}
}
ts = mtime (*p);
pt->mtime (ts);
if (ts != timestamp_nonexistent)
return true;
l4 ([&]{trace << "no existing file for target " << *pt;});
return false;
}
}
}
recipe file_rule::
apply (action a, target& t, match_extra&) const
{
// Update triggers the update of this target's prerequisites so it would
// seem natural that we should also trigger their cleanup. However, this
// possibility is rather theoretical so until we see a real use-case for
// this functionality, we simply ignore the clean operation.
//
if (a.operation () == clean_id)
return noop_recipe;
// If we have no prerequisites, then this means this file is up to date.
// Return noop_recipe which will also cause the target's state to be set
// to unchanged. This is an important optimization on which quite a few
// places that deal with predominantly static content rely.
//
if (!t.has_group_prerequisites ()) // Group as in match_prerequisites().
return noop_recipe;
// Match all the prerequisites.
//
match_prerequisites (a, t);
// Note that we used to provide perform_update() which checked that this
// target is not older than any of its prerequisites. However, later we
// realized this is probably wrong: consider a script with a testscript as
// a prerequisite; chances are the testscript will be newer than the
// script and there is nothing wrong with that.
//
return default_recipe;
}
const file_rule file_rule::instance;
const rule_match file_rule::rule_match ("build.file", file_rule::instance);
// alias_rule
//
bool alias_rule::
match (action, target&) const
{
return true;
}
recipe alias_rule::
apply (action a, target& t) const
{
// Inject dependency on our directory (note: not parent) so that it is
// automatically created on update and removed on clean.
//
inject_fsdir (a, t, true, false);
// Handle the alias match-only level.
//
match_search ms;
if (t.ctx.match_only && *t.ctx.match_only == match_only_level::alias)
{
ms = [] (action,
const target& t,
const prerequisite& p,
include_type i)
{
return prerequisite_target (
p.is_a<alias> () ? &search (t, p) : nullptr,
i);
};
}
match_prerequisites (a, t, ms);
return default_recipe;
}
const alias_rule alias_rule::instance;
// fsdir_rule
//
bool fsdir_rule::
match (action, target&) const
{
return true;
}
recipe fsdir_rule::
apply (action a, target& t) const
{
// Inject dependency on the parent directory. Note that it must be first
// (see perform_update_direct()).
//
inject_fsdir (a, t);
match_prerequisites (a, t);
switch (a)
{
case perform_update_id: return &perform_update;
case perform_clean_id: return &perform_clean;
default: assert (false); return default_recipe;
}
}
static bool
fsdir_mkdir (const target& t, const dir_path& d)
{
// Even with the exists() check below this can still be racy so only print
// things if we actually did create it (similar to build2::mkdir()).
//
auto print = [&t, &d] ()
{
if (verb >= 2)
text << "mkdir " << d;
else if (verb && t.ctx.current_diag_noise)
print_diag ("mkdir", t);
};
// Note: ignoring the dry_run flag.
//
mkdir_status ms;
try
{
ms = try_mkdir (d);
}
catch (const system_error& e)
{
print ();
fail << "unable to create directory " << d << ": " << e << endf;
}
if (ms == mkdir_status::success)
{
print ();
return true;
}
return false;
}
target_state fsdir_rule::
perform_update (action a, const target& t)
{
target_state ts (target_state::unchanged);
// First update prerequisites (e.g. create parent directories) then create
// this directory.
//
// @@ outer: should we assume for simplicity its only prereqs are fsdir{}?
//
if (!t.prerequisite_targets[a].empty ())
ts = straight_execute_prerequisites (a, t);
// The same code as in perform_update_direct() below.
//
const dir_path& d (t.dir); // Everything is in t.dir.
// Generally, it is probably correct to assume that in the majority of
// cases the directory will already exist. If so, then we are going to get
// better performance by first checking if it indeed exists. See
// butl::try_mkdir() for details.
//
// @@ Also skip prerequisites? Can't we return noop in apply?
//
if (!exists (d) && fsdir_mkdir (t, d))
ts |= target_state::changed;
return ts;
}
void fsdir_rule::
perform_update_direct (action a, const target& t)
{
// First create the parent directory. If present, it is always first.
//
const target* p (t.prerequisite_targets[a].empty ()
? nullptr
: t.prerequisite_targets[a][0]);
if (p != nullptr && p->is_a<fsdir> ())
perform_update_direct (a, *p);
// The same code as in perform_update() above.
//
const dir_path& d (t.dir);
if (!exists (d))
fsdir_mkdir (t, d);
}
target_state fsdir_rule::
perform_clean (action a, const target& t)
{
// The reverse order of update: first delete this directory, then clean
// prerequisites (e.g., delete parent directories).
//
// Don't fail if we couldn't remove the directory because it is not empty
// (or is current working directory). In this case rmdir() will issue a
// warning when appropriate.
// The same code as in perform_clean_direct() below.
//
target_state ts (rmdir (t.dir, t, t.ctx.current_diag_noise ? 1 : 2)
? target_state::changed
: target_state::unchanged);
if (!t.prerequisite_targets[a].empty ())
ts |= reverse_execute_prerequisites (a, t);
return ts;
}
void fsdir_rule::
perform_clean_direct (action a, const target& t)
{
// The same code as in perform_clean() above.
//
rmdir (t.dir, t, t.ctx.current_diag_noise ? 1 : 2);
// Then clean the parent directory. If present, it is always first.
//
const target* p (t.prerequisite_targets[a].empty ()
? nullptr
: t.prerequisite_targets[a][0]);
if (p != nullptr && p->is_a<fsdir> ())
perform_clean_direct (a, *p);
}
const fsdir_rule fsdir_rule::instance;
// noop_rule
//
bool noop_rule::
match (action, target&) const
{
return true;
}
recipe noop_rule::
apply (action, target&) const
{
return noop_recipe;
}
const noop_rule noop_rule::instance;
// adhoc_rule
//
const dir_path adhoc_rule::recipes_build_dir ("recipes");
bool adhoc_rule::
reverse_fallback (action, const target_type&) const
{
return false;
}
bool adhoc_rule::
match (action a, target& xt, const string& h, match_extra& me) const
{
const target& t (xt);
return pattern == nullptr || pattern->match (a, t, h, me);
}
void adhoc_rule::
dump_attributes (ostream&) const
{
}
// adhoc_rule_with_deadline (vtable)
//
adhoc_rule_with_deadline::
~adhoc_rule_with_deadline ()
{
}
// Scope operation callback that cleans up recipe builds.
//
target_state adhoc_rule::
clean_recipes_build (action, const scope& rs, const dir&)
{
context& ctx (rs.ctx);
const dir_path& out_root (rs.out_path ());
dir_path d (out_root / rs.root_extra->build_build_dir / recipes_build_dir);
if (exists (d))
{
if (rmdir_r (ctx, d))
{
// Clean up build/build/ if it also became empty.
//
d = out_root / rs.root_extra->build_build_dir;
if (empty (d))
{
rmdir (ctx, d, 2);
// Clean up build/ if it also became empty (e.g., in case of a build
// with a transient configuration).
//
d = out_root / rs.root_extra->build_dir;
if (empty (d))
rmdir (ctx, d, 2);
}
return target_state::changed;
}
}
return target_state::unchanged;
}
// adhoc_rule_pattern (vtable)
//
adhoc_rule_pattern::
~adhoc_rule_pattern ()
{
}
bool adhoc_rule_pattern::fallback_rule::
match (action, target&, const string&, match_extra&) const
{
return false;
}
recipe adhoc_rule_pattern::fallback_rule::
apply (action, target&, match_extra&) const
{
return empty_recipe;
}
}
|