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
|
// file : build/cli/rule.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#include <build/cli/rule>
#include <butl/process>
#include <build/scope>
#include <build/target>
#include <build/context>
#include <build/algorithm>
#include <build/diagnostics>
#include <build/cli/target>
#include <build/config/utility>
using namespace std;
using namespace butl;
namespace build
{
namespace cli
{
using config::append_options;
void* compile::
match (action a, target& xt, const std::string&) const
{
tracer trace ("cli::compile::match");
if (cli_cxx* pt = xt.is_a<cli_cxx> ())
{
cli_cxx& t (*pt);
// See if we have a .cli source file.
//
prerequisite* r (nullptr);
for (prerequisite& p: group_prerequisites (t))
{
if (p.is_a<cli> ())
{
//@@ Need to verify input and output stems match.
r = &p;
break;
}
}
if (r == nullptr)
{
level3 ([&]{trace << "no .cli source file for target " << t;});
return nullptr;
}
// If we still haven't figured out the member list, we can do
// that now. Specifically, at this stage no further changes to
// cli.options are possible and we can determine whether the
// --suppress-inline option is present.
//
if (t.h () == nullptr)
{
cxx::hxx& h (search<cxx::hxx> (t.dir, t.name, nullptr, nullptr));
h.group = &t;
t.h (h);
cxx::cxx& c (search<cxx::cxx> (t.dir, t.name, nullptr, nullptr));
c.group = &t;
t.c (c);
bool inl (true);
if (auto val = t["cli.options"])
{
for (const name& n: val.template as<const list_value&> ())
{
if (n.value == "--suppress-inline")
{
inl = false;
break;
}
}
}
if (inl)
{
cxx::ixx& i (search<cxx::ixx> (t.dir, t.name, nullptr, nullptr));
i.group = &t;
t.i (i);
}
}
return r;
}
else
{
// One of the ?xx{} members.
//
target& t (xt);
// First see if we are already linked-up to the cli.cxx{} group.
// If it is some other group, then we are definitely not a match.
//
if (t.group != nullptr)
return t.group->is_a<cli_cxx> ();
// Then see if there is a corresponding cli.cxx{} group.
//
cli_cxx* g (targets.find<cli_cxx> (t.dir, t.name));
// Finally, if this target has a cli{} prerequisite, synthesize
// the group.
//
if (g == nullptr)
{
for (prerequisite& p: group_prerequisites (t))
{
if (p.is_a<cli> ()) // @@ Need to check that stems match.
{
g = &targets.insert<cli_cxx> (t.dir, t.name, trace);
g->prerequisites.emplace_back (p);
break;
}
}
}
if (g != nullptr)
{
// Resolve the group's members. This should link us up to
// the group.
//
resolve_group_members (a, *g);
// For ixx{}, verify it is part of the group.
//
if (t.is_a<cxx::ixx> () && g->i () == nullptr)
{
level3 ([&]{trace << "generation of inline file " << t
<< " is disabled with --suppress-inline";});
g = nullptr;
}
}
assert (t.group == g);
return g;
}
}
recipe compile::
apply (action a, target& xt, void* vp) const
{
if (cli_cxx* pt = xt.is_a<cli_cxx> ())
{
cli_cxx& t (*pt);
// Derive file names for the members.
//
t.h ()->derive_path ();
t.c ()->derive_path ();
if (t.i () != nullptr)
t.i ()->derive_path ();
// Inject dependency on the output directory.
//
inject_parent_fsdir (a, t);
// Search and match prerequisites.
//
switch (a.operation ())
{
case default_id:
case update_id: search_and_match (a, t); break;
case clean_id: search_and_match (a, t, t.dir); break;
default: assert (false); // We didn't register for this.
}
switch (a)
{
case perform_update_id: return &perform_update;
case perform_clean_id: return &perform_clean;
default: return default_recipe; // Forward to prerequisites.
}
}
else
{
cli_cxx& g (*static_cast<cli_cxx*> (vp));
build::match (a, g);
return &delegate;
}
}
static void
append_extension (vector<const char*>& args,
path_target& t,
const char* opt,
const char* def)
{
assert (t.ext != nullptr); // Should have been figured out in apply().
if (*t.ext != def)
{
// CLI needs the extension with the leading dot (unless it is empty)
// while we store the extension without. But if there is an extension,
// then we can get it (with the dot) from the file name.
//
args.push_back (opt);
args.push_back (t.ext->empty ()
? t.ext->c_str ()
: t.path ().extension () - 1);
}
}
target_state compile::
perform_update (action a, target& xt)
{
cli_cxx& t (static_cast<cli_cxx&> (xt));
// Execute our prerequsites and check if we are out of date.
//
cli* s (execute_prerequisites<cli> (a, t, t.h ()->mtime ()));
target_state ts;
if (s == nullptr)
ts = target_state::unchanged;
else
{
// Translate source path to relative (to working directory). This
// results in easier to read diagnostics.
//
path relo (relative (t.dir));
path rels (relative (s->path ()));
scope& rs (t.root_scope ());
const string& cli (rs["config.cli"].as<const string&> ());
vector<const char*> args {cli.c_str ()};
// See if we need to pass any --?xx-suffix options.
//
append_extension (args, *t.h (), "--hxx-suffix", "hxx");
append_extension (args, *t.c (), "--cxx-suffix", "cxx");
if (t.i () != nullptr)
append_extension (args, *t.i (), "--ixx-suffix", "ixx");
append_options (args, t, "cli.options");
if (!relo.empty ())
{
args.push_back ("-o");
args.push_back (relo.string ().c_str ());
}
args.push_back (rels.string ().c_str ());
args.push_back (nullptr);
if (verb)
print_process (args);
else
text << "cli " << *s;
try
{
process pr (args.data ());
if (!pr.wait ())
throw failed ();
timestamp s (system_clock::now ());
// Update member timestamps.
//
t.h ()->mtime (s);
t.c ()->mtime (s);
if (t.i () != nullptr)
t.i ()->mtime (s);
ts = target_state::changed;
}
catch (const process_error& e)
{
error << "unable to execute " << args[0] << ": " << e.what ();
if (e.child ())
exit (1);
throw failed ();
}
}
// Update member recipes. Without that the state update below
// won't stick.
//
if (!t.h ()->recipe (a))
t.h ()->recipe (a, &delegate);
if (!t.c ()->recipe (a))
t.c ()->recipe (a, &delegate);
if (t.i () != nullptr && !t.i ()->recipe (a))
t.i ()->recipe (a, &delegate);
// Update member states.
//
t.h ()->state = ts;
t.c ()->state = ts;
if (t.i () != nullptr)
t.i ()->state = ts;
return ts;
}
target_state compile::
perform_clean (action a, target& xt)
{
cli_cxx& t (static_cast<cli_cxx&> (xt));
// The reverse order of update: first delete the files, then clean
// prerequisites. Also update timestamp in case there are operations
// after us that could use the information.
//
//
bool r (false);
if (t.i () != nullptr)
{
r = rmfile (t.i ()->path (), *t.i ()) || r;
t.i ()->mtime (timestamp_nonexistent);
}
r = rmfile (t.c ()->path (), *t.c ()) || r;
t.c ()->mtime (timestamp_nonexistent);
r = rmfile (t.h ()->path (), *t.h ()) || r;
t.h ()->mtime (timestamp_nonexistent);
// Clean prerequisites.
//
target_state ts (reverse_execute_prerequisites (a, t));
return r ? target_state::changed : ts;
}
target_state compile::
delegate (action a, target& t)
{
// Delegate to our group.
//
return execute (a, *t.group);
}
}
}
|