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
|
// file : build2/test/script/script.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license : MIT; see accompanying LICENSE file
#include <build2/test/script/script>
#include <sstream>
#include <algorithm> // find()
#include <build2/target>
using namespace std;
namespace build2
{
namespace test
{
namespace script
{
// Utility functions
//
// Quote if empty or contains spaces or any of the special characters.
//
// @@ What if it contains quotes, escapes?
//
static void
to_stream_q (ostream& o, const string& s)
{
if (s.empty () || s.find_first_of (" |&<>=") != string::npos)
o << '"' << s << '"';
else
o << s;
};
void
to_stream (ostream& o, const command& c, command_to_stream m)
{
auto print_path = [&o] (const path& p)
{
using build2::operator<<;
ostringstream s;
stream_verb (s, stream_verb (o));
s << p;
to_stream_q (o, s.str ());
};
auto print_redirect =
[&o, print_path] (const redirect& r, const char* prefix)
{
o << ' ' << prefix;
size_t n (string::traits_type::length (prefix));
assert (n > 0);
char d (prefix[n - 1]); // Redirect direction.
switch (r.type)
{
case redirect_type::none: assert (false); break;
case redirect_type::pass: o << '+'; break;
case redirect_type::null: o << '-'; break;
case redirect_type::merge: o << '&' << r.fd; break;
case redirect_type::here_string:
{
const string& v (r.str);
bool nl (!v.empty () && v.back () == '\n');
if (!nl)
o << ':';
to_stream_q (o, nl ? string (v, 0, v.size () - 1) : v);
break;
}
case redirect_type::here_document:
{
const string& v (r.doc.doc);
bool nl (!v.empty () && v.back () == '\n');
// Add another '>' or '<'. Note that here end marker never
// needs to be quoted.
//
o << d << (nl ? "" : ":") << r.doc.end;
break;
}
case redirect_type::file:
{
// Add '>>' or '<<' (and so make it '<<<' or '>>>').
//
o << d << d << (r.file.append ? "&" : "");
print_path (r.file.path);
break;
}
}
};
auto print_doc = [&o] (const redirect& r)
{
const string& v (r.doc.doc);
bool nl (!v.empty () && v.back () == '\n');
o << endl << v << (nl ? "" : "\n") << r.doc.end;
};
if ((m & command_to_stream::header) == command_to_stream::header)
{
// Program.
//
to_stream_q (o, c.program.string ());
// Arguments.
//
for (const string& a: c.arguments)
{
o << ' ';
to_stream_q (o, a);
}
// Redirects.
//
if (c.in.type != redirect_type::none) print_redirect (c.in, "<");
if (c.out.type != redirect_type::none) print_redirect (c.out, ">");
if (c.err.type != redirect_type::none) print_redirect (c.err, "2>");
for (const auto& p: c.cleanups)
{
o << " &";
if (p.type != cleanup_type::always)
o << (p.type == cleanup_type::maybe ? '?' : '!');
print_path (p.path);
}
if (c.exit.comparison != exit_comparison::eq || c.exit.status != 0)
{
switch (c.exit.comparison)
{
case exit_comparison::eq: o << " == "; break;
case exit_comparison::ne: o << " != "; break;
}
o << static_cast<uint16_t> (c.exit.status);
}
}
if ((m & command_to_stream::here_doc) == command_to_stream::here_doc)
{
// Here-documents.
//
if (c.in.type == redirect_type::here_document) print_doc (c.in);
if (c.out.type == redirect_type::here_document) print_doc (c.out);
if (c.err.type == redirect_type::here_document) print_doc (c.err);
}
}
// redirect
//
redirect::
redirect (redirect_type t)
: type (t)
{
switch (type)
{
case redirect_type::none:
case redirect_type::pass:
case redirect_type::null:
case redirect_type::merge: break;
case redirect_type::here_string: new (&str) string (); break;
case redirect_type::here_document: new (&doc) doc_type (); break;
case redirect_type::file: new (&file) file_type (); break;
}
}
redirect::
redirect (redirect&& r)
: type (r.type)
{
switch (type)
{
case redirect_type::none:
case redirect_type::pass:
case redirect_type::null: break;
case redirect_type::merge: fd = r.fd; break;
case redirect_type::here_string:
{
new (&str) string (move (r.str));
break;
}
case redirect_type::here_document:
{
new (&doc) doc_type (move (r.doc));
break;
}
case redirect_type::file:
{
new (&file) file_type (move (r.file));
break;
}
}
}
redirect::
redirect (const redirect& r)
: type (r.type)
{
switch (type)
{
case redirect_type::none:
case redirect_type::pass:
case redirect_type::null: break;
case redirect_type::merge: fd = r.fd; break;
case redirect_type::here_string: new (&str) string (r.str); break;
case redirect_type::here_document: new (&doc) doc_type (r.doc); break;
case redirect_type::file:
{
new (&file) file_type (r.file);
break;
}
}
}
redirect::
~redirect ()
{
switch (type)
{
case redirect_type::none:
case redirect_type::pass:
case redirect_type::null:
case redirect_type::merge: break;
case redirect_type::here_string: str.~string (); break;
case redirect_type::here_document: doc.~doc_type (); break;
case redirect_type::file: file.~file_type (); break;
}
}
redirect& redirect::
operator= (redirect&& r)
{
if (this != &r)
{
this->~redirect ();
new (this) redirect (move (r)); // Assume noexcept move-constructor.
}
return *this;
}
redirect& redirect::
operator= (const redirect& r)
{
if (this != &r)
*this = redirect (r); // Reduce to move-assignment.
return *this;
}
// scope
//
scope::
scope (const string& id, scope* p)
: parent (p),
root (p != nullptr ? p->root : static_cast<script*> (this)),
id_path (cast<path> (assign (root->id_var) = path ())),
wd_path (cast<dir_path> (assign (root->wd_var) = dir_path ()))
{
// Construct the id_path as a string to ensure POSIX form. In fact,
// the only reason we keep it as a path is to be able to easily get id
// by calling leaf().
//
{
string s (p != nullptr ? p->id_path.string () : string ());
if (!s.empty () && !id.empty ())
s += '/';
s += id;
const_cast<path&> (id_path) = path (move (s));
}
// Calculate the working directory path unless this is the root scope
// (handled in an ad hoc way).
//
if (p != nullptr)
const_cast<dir_path&> (wd_path) = dir_path (p->wd_path) /= id;
}
void scope::
clean (cleanup c, bool implicit)
{
using std::find; // Hidden by scope::find().
assert (!implicit || c.type == cleanup_type::always);
auto pr = [&c] (const cleanup& v) -> bool {return v.path == c.path;};
auto i (find_if (cleanups.begin (), cleanups.end (), pr));
if (i == cleanups.end ())
cleanups.emplace_back (move (c));
else if (!implicit)
i->type = c.type;
}
// script_base
//
script_base::
script_base ()
: // Enter the test* variables with the same variable types as in
// buildfiles.
//
test_var (var_pool.insert<path> ("test")),
opts_var (var_pool.insert<strings> ("test.options")),
args_var (var_pool.insert<strings> ("test.arguments")),
cmd_var (var_pool.insert<strings> ("*")),
wd_var (var_pool.insert<dir_path> ("~")),
id_var (var_pool.insert<path> ("@")) {}
// script
//
static inline string
script_id (const path& p)
{
string r (p.leaf ().string ());
if (r == "testscript")
return string ();
size_t n (path::traits::find_extension (r));
assert (n != string::npos);
r.resize (n);
return r;
}
script::
script (target& tt, testscript& st, const dir_path& rwd)
: group (script_id (st.path ())),
test_target (tt), script_target (st)
{
// Set the script working dir ($~) to $out_base/test/<id> (id_path
// for root is just the id which is empty if st is 'testscript').
//
const_cast<dir_path&> (wd_path) = dir_path (rwd) /= id_path.string ();
// Unless we have the test variable set on the test or script target,
// set it at the script level to the test target's path.
//
if (!find (test_var))
{
value& v (assign (test_var));
// If this is a path-based target, then we use the path. If this is
// an alias target (e.g., dir{}), then we use the directory path.
// Otherwise, we leave it NULL expecting the testscript to set it to
// something appropriate, if used.
//
if (auto* p = tt.is_a<path_target> ())
v = p->path ();
else if (tt.is_a<alias> ())
v = path (tt.dir.string ()); // Strip trailing slash.
}
// Also add the NULL $* value that signals it needs to be recalculated
// on first access.
//
assign (cmd_var) = nullptr;
}
lookup scope::
find (const variable& var) const
{
// Search script scopes until we hit the root.
//
const scope* p (this);
do
{
if (const value* v = p->vars.find (var))
return lookup (v, &p->vars);
}
while (p->parent != nullptr ? (p = p->parent) : nullptr);
// Switch to the corresponding buildfile variable. Note that we don't
// want to insert a new variable into the pool (we might be running
// concurrently). Plus, if there is no such variable, then we cannot
// possibly find any value.
//
const variable* pvar (build2::var_pool.find (var.name));
if (pvar == nullptr)
return lookup ();
const script& s (static_cast<const script&> (*p));
{
const variable& var (*pvar);
// First check the target we are testing.
//
{
// Note that we skip applying the override if we did not find any
// value. In this case, presumably the override also affects the
// script target and we will pick it up there. A bit fuzzy.
//
auto p (s.test_target.find_original (var, true));
if (p.first)
{
if (var.override != nullptr)
p = s.test_target.base_scope ().find_override (
var, move (p), true);
return p.first;
}
}
// Then the script target followed by the scopes it is in. Note that
// while unlikely it is possible the test and script targets will be
// in different scopes which brings the question of which scopes we
// should search.
//
return s.script_target[var];
}
}
value& scope::
append (const variable& var)
{
lookup l (find (var));
if (l.defined () && l.belongs (*this)) // Existing var in this scope.
return const_cast<value&> (*l);
value& r (assign (var)); // NULL.
//@@ I guess this is where we convert untyped value to strings?
//
if (l.defined ())
r = *l; // Copy value (and type) from the outer scope.
return r;
}
}
}
}
|