aboutsummaryrefslogtreecommitdiff
path: root/build2/test/script/script.cxx
blob: b206e4fd0c0556d5a52f22e4c136cc08165a28cd (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
// 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 <build2/target>

using namespace std;

namespace build2
{
  namespace test
  {
    namespace script
    {
      script::
      script (target& tt, target& st)
          : test_target (tt), script_target (st)
      {
        // 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.
        //
        {
          // Note: use the same variable type as in buildfile.
          //
          const variable& var (var_pool.insert<path> ("test"));

          if (!find (var))
          {
            value& v (assign (var));

            // If this is a path-based target, then we use the path. If this
            // is a directory (alias) target, 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<dir> ())
              v = path (tt.dir.string ()); // Strip trailing slash.
          }
        }
      }

      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;
      }
    }
  }
}