aboutsummaryrefslogtreecommitdiff
path: root/build2/test/rule.cxx
blob: c1336985cc4e7c1fbb98cf60e7577b40c8f7f3ba (plain)
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
// file      : build2/test/rule.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <build2/test/rule>

#include <build2/scope>
#include <build2/target>
#include <build2/algorithm>
#include <build2/diagnostics>

#include <build2/test/target>

#include <build2/test/script/parser>
#include <build2/test/script/runner>

using namespace std;
using namespace butl;

namespace build2
{
  namespace test
  {
    struct match_data
    {
      bool test = false;
      bool script = false;
    };

    static_assert (sizeof (match_data) <= target::data_size,
                   "insufficient space");

    match_result rule::
    match (action a, target& t, const string&) const
    {
      match_data md;

      // We have two very different cases: testscript and simple test (plus it
      // may not be a testable target at all). So as the first step determine
      // which case this is. If we have any prerequisites of the test{} type,
      // then this is the testscript case.
      //
      for (prerequisite_member p: group_prerequisite_members (a, t))
      {
        if (p.is_a<testscript> ())
        {
          md.script = true;
          break;
        }
      }

      if (md.script)
      {
        // We treat this target as testable unless the test variable is
        // explicitly set to false.
        //
        lookup l (t["test"]);
        md.test = !l || cast<bool> (l);
      }
      else
      {
        // For the simple case whether this is a test is controlled by the
        // test variable. Also, it feels redundant to specify, say, "test =
        // true" and "test.output = test.out" -- the latter already says this
        // is a test.
        //

        // Use lookup depths to figure out who "overrides" whom.
        //
        auto p (t.find ("test"));

        if (p.first && cast<bool> (p.first))
          md.test = true;
        else
        {
          auto test = [&t, &p] (const char* n)
          {
            return t.find (n).second < p.second;
          };

          md.test =
            test ("test.input")     ||
            test ("test.output")    ||
            test ("test.roundtrip") ||
            test ("test.options")   ||
            test ("test.arguments");
        }
      }

      match_result mr (t);

      // If this target is testable and this is the update pre-operation, then
      // all we really need to do is say we are not a match and the standard
      // matching machinery will (hopefully) find the rule to update this
      // target.
      //
      if (md.test && a.operation () == update_id)
      {
        // And this is exactly what we do for the testscript case.
        //
        if (md.script)
          return nullptr;
        else
          // For the simple case there is one thing that compilates this
          // simple approach: test input/output. While normally they will be
          // existing (in src_base) files, they could also be auto-generated.
          // In fact, they could only be needed for testing, which means the
          // normall update won't even know about them (nor clean, for that
          // matter; this is why we need cleantest).
          //
          // @@ Maybe we should just say if input/output are generated, then
          //    they must be explicitly listed as prerequisites? Then no need
          //    for cleantest but they will be updated even when not needed.
          //
          // To make generated input/output work we will have to cause their
          // update ourselves. In other words, we may have to do some actual
          // work for (update, test), and not simply "guide" (update, 0) as to
          // which targets need updating. For how exactly we are going to do
          // it, see apply() below.
          //
          // At this stage we need to change the recipe action to (update, 0)
          // (i.e., "unconditional update").
          //
          mr.recipe_action = action (a.meta_operation (), update_id);
      }

      // Note that we match even if this target is not testable so that we
      // can ignore it (see apply()).
      //
      t.data (md); // Save the data in the target's auxilary storage.
      return mr;
    }

    recipe rule::
    apply (action a, target& t, const match_result&) const
    {
      tracer trace ("test::rule::apply");

      match_data md (move (t.data<match_data> ()));
      t.clear_data (); // In case delegated-to rule also uses aux storage.

      if (!md.test)
        return noop_recipe;

      // If we are here, then the target is testable.
      //
      if (md.script)
      {
        // If we are here, then the action is (perform, test, 0).
        //
        // Collect all the testscript targets in prerequisite_targets.
        //
        for (prerequisite_member p: group_prerequisite_members (a, t))
        {
          if (p.is_a<testscript> ())
            t.prerequisite_targets.push_back (&p.search ());
        }

        return &perform_script;
      }
      else
      {
        // If we are here, then the action is either
        //   a. (perform, test, 0) or
        //   b. (*, update, 0)
        //
        // In both cases, the next step is to see if we have test.{input,
        // output,roundtrip}.
        //

        // We should have either arguments or input/roundtrip. Again, use
        // lookup depth to figure out who takes precedence.
        //
        auto ip (t.find ("test.input"));
        auto op (t.find ("test.output"));
        auto rp (t.find ("test.roundtrip"));
        auto ap (t.find ("test.arguments"));

        auto test = [&t] (pair<lookup, size_t>& x, const char* xn,
                          pair<lookup, size_t>& y, const char* yn)
        {
          if (x.first && y.first)
          {
            if (x.second == y.second)
              fail << "both " << xn << " and " << yn << " specified for "
                   << "target " << t;

            (x.second < y.second ? y : x) = make_pair (lookup (), size_t (~0));
          }
        };

        test (ip, "test.input",     ap, "test.arguments");
        test (rp, "test.roundtrip", ap, "test.arguments");
        test (ip, "test.input",     rp, "test.roundtrip");
        test (op, "test.output",    rp, "test.roundtrip");

        const name* in;
        const name* on;

        // Reduce the roundtrip case to input/output.
        //
        if (rp.first)
        {
          in = on = &cast<name> (rp.first);
        }
        else
        {
          in = ip.first ? &cast<name> (ip.first) : nullptr;
          on = op.first ? &cast<name> (op.first) : nullptr;
        }

        // Resolve them to targets, which normally would be existing files
        // but could also be targets that need updating.
        //
        scope& bs (t.base_scope ());

        // @@ OUT: what if this is a @-qualified pair or names?
        //
        target* it (in != nullptr ? &search (*in, bs) : nullptr);
        target* ot (on != nullptr
                    ? in == on ? it : &search (*on, bs)
                    : nullptr);

        if (a.operation () == update_id)
        {
          // First see if input/output are existing, up-to-date files. This
          // is a common case optimization.
          //
          if (it != nullptr)
          {
            build2::match (a, *it);

            if (it->state () == target_state::unchanged)
            {
              unmatch (a, *it);
              it = nullptr;
            }
          }

          if (ot != nullptr)
          {
            if (in != on)
            {
              build2::match (a, *ot);

              if (ot->state () == target_state::unchanged)
              {
                unmatch (a, *ot);
                ot = nullptr;
              }
            }
            else
              ot = it;
          }

          // Find the "real" update rule, that is, the rule that would have
          // been found if we signalled that we do not match from match()
          // above.
          //
          recipe d (match_delegate (a, t).first);

          // If we have no input/output that needs updating, then simply
          // redirect to it.
          //
          if (it == nullptr && ot == nullptr)
            return d;

          // Ok, time to handle the worst case scenario: we need to cause
          // update of input/output targets and also delegate to the real
          // update.
          //
          return [it, ot, dr = move (d)] (action a, target& t) -> target_state
          {
            // Do the general update first.
            //
            target_state r (execute_delegate (dr, a, t));

            if (it != nullptr)
              r |= execute (a, *it);

            if (ot != nullptr)
              r |= execute (a, *ot);

            return r;
          };
        }
        else
        {
          // Cache the targets in our prerequsite targets lists where they can
          // be found by perform_test(). If we have either or both, then the
          // first entry is input and the second -- output (either can be
          // NULL).
          //
          if (it != nullptr || ot != nullptr)
          {
            auto& pts (t.prerequisite_targets);
            pts.resize (2, nullptr);
            pts[0] = it;
            pts[1] = ot;
          }

          return &perform_test;
        }
      }
    }

    target_state rule::
    perform_script (action, target& t)
    {
      using namespace script;

      for (target* pt: t.prerequisite_targets)
      {
        testscript& st (*pt->is_a<testscript> ());
        const path& sp (st.path ());
        assert (!sp.empty ()); // Should have been assigned by update.

        text << "test " << t << " with " << st;

        try
        {
          ifdstream ifs (sp);
          parser p;
          concurrent_runner run;

          p.parse (ifs, sp, t, st, run);
        }
        catch (const io_error& e)
        {
          fail << "unable to read testscript " << sp << ": " << e.what ();
        }
      }

      return target_state::changed;
    }

    // The format of args shall be:
    //
    // name1 arg arg ... nullptr
    // name2 arg arg ... nullptr
    // ...
    // nameN arg arg ... nullptr nullptr
    //
    static bool
    run_test (target& t,
              diag_record& dr,
              char const** args,
              process* prev = nullptr)
    {
      // Find the next process, if any.
      //
      char const** next (args);
      for (next++; *next != nullptr; next++) ;
      next++;

      // Redirect stdout to a pipe unless we are last, in which
      // case redirect it to stderr.
      //
      int out (*next == nullptr ? 2 : -1);
      bool pr, wr;

      try
      {
        if (prev == nullptr)
        {
          // First process.
          //
          process p (args, 0, out);
          pr = *next == nullptr || run_test (t, dr, next, &p);
          wr = p.wait ();
        }
        else
        {
          // Next process.
          //
          process p (args, *prev, out);
          pr = *next == nullptr || run_test (t, dr, next, &p);
          wr = p.wait ();
        }
      }
      catch (const process_error& e)
      {
        error << "unable to execute " << args[0] << ": " << e.what ();

        if (e.child ())
          exit (1);

        throw failed ();
      }

      if (!wr)
      {
        if (pr) // First failure?
          dr << fail << "test " << t << " failed"; // Multi test: test 1.

        dr << error << "non-zero exit status: ";
        print_process (dr, args);
      }

      return pr && wr;
    }

    target_state rule::
    perform_test (action, target& t)
    {
      // @@ Would be nice to print what signal/core was dumped.
      //
      // @@ Doesn't have to be a file target if we have test.cmd (or
      //    just use test which is now path).
      //

      file& ft (static_cast<file&> (t));
      assert (!ft.path ().empty ()); // Should have been assigned by update.

      process_path fpp (run_search (ft.path (), true));
      cstrings args {fpp.recall_string ()};

      // Do we have options?
      //
      if (auto l = t["test.options"])
        append_options (args, cast<strings> (l));

      // Do we have input?
      //
      auto& pts (t.prerequisite_targets);
      if (pts.size () != 0 && pts[0] != nullptr)
      {
        file& it (static_cast<file&> (*pts[0]));
        assert (!it.path ().empty ()); // Should have been assigned by update.
        args.push_back (it.path ().string ().c_str ());
      }
      // Maybe arguments then?
      //
      else
      {
        if (auto l = t["test.arguments"])
          append_options (args, cast<strings> (l));
      }

      args.push_back (nullptr);

      // Do we have output?
      //
      path dp ("diff");
      process_path dpp;
      if (pts.size () != 0 && pts[1] != nullptr)
      {
        file& ot (static_cast<file&> (*pts[1]));
        assert (!ot.path ().empty ()); // Should have been assigned by update.

        dpp = run_search (dp, true);

        args.push_back (dpp.recall_string ());
        args.push_back ("--strip-trailing-cr"); //@@ TMP: see module.cxx
        args.push_back ("-u");
        args.push_back (ot.path ().string ().c_str ());
        args.push_back ("-");
        args.push_back (nullptr);
      }

      args.push_back (nullptr); // Second.

      if (verb >= 2)
        print_process (args);
      else if (verb)
        text << "test " << t;

      {
        diag_record dr;

        if (!run_test (t, dr, args.data ()))
        {
          dr << info << "test command line: ";
          print_process (dr, args);
        }
      }

      return target_state::changed;
    }
  }
}