aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/config/utility.cxx
blob: 2ce94ffc8d109a10de6ba058bafa773e243cc166 (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
// file      : libbuild2/config/utility.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <libbuild2/config/utility.hxx>

using namespace std;

namespace build2
{
  void (*config_save_variable) (scope&, const variable&, optional<uint64_t>);
  void (*config_save_environment) (scope&, const char*);
  void (*config_save_module) (scope&, const char*, int);
  const string& (*config_preprocess_create) (context&,
                                             values&,
                                             vector_view<opspec>&,
                                             bool,
                                             const location&);
  bool (*config_configure_post) (scope&, bool (*)(action, const scope&));
  bool (*config_disfigure_pre) (scope&, bool (*)(action, const scope&));

  namespace config
  {
    pair<lookup, bool>
    lookup_config_impl (scope& rs, const variable& var, uint64_t sflags)
    {
      // This is a stripped-down version of the default value case.

      pair<lookup, size_t> org (rs.lookup_original (var));

      bool n (false); // New flag.
      lookup l (org.first);

      // Treat an inherited value that was set to default as new.
      //
      if (l.defined () && l->extra == 1)
        n = true;

      if (var.overrides != nullptr)
      {
        // This is tricky: if we didn't find the original, pretend we have set
        // the default value for the purpose of override lookup in order to
        // have consistent semantics with the default value case (see notes in
        // that implementation for background).
        //
        // In particular, this makes sure we can first do the lookup without
        // the default value and then, if there is no value, call the version
        // with the default value and end up with the same result if we called
        // the default value version straight away.
        //
        // Note that we need to detect both when the default value is not
        // overridden as well as when the override is based on it (e.g., via
        // append; think config.cxx+=-m32).
        //
        // @@ Maybe a callback that computes the default value on demand is a
        //    better way?
        //
        variable_map::value_data v; // NULL value, but must be with version.
        if (!l.defined ())
          org = make_pair (lookup (v, var, rs), 1); // As default value case.

        scope::override_info li (rs.lookup_override_info (var, move (org)));
        pair<lookup, size_t>& ovr (li.lookup);

        if (l.defined () ? l != ovr.first : !li.original) // Overriden?
        {
          // Override is always treated as new.
          //
          n = true;
          l = move (ovr.first);
        }
      }

      if (l.defined ())
        save_variable (rs, var, sflags);

      return pair<lookup, bool> (l, n);
    }

    bool
    specified_config (scope& rs,
                      const string& n,
                      initializer_list<const char*> ig)
    {
      auto& vp (rs.var_pool ());

      // Search all outer scopes for any value in this namespace.
      //
      // What about "pure" overrides, i.e., those without any original values?
      // Well, they will also be found since their names have the original
      // variable as a prefix. But do they apply? Yes, since we haven't found
      // any original values, they will be "visible"; see find_override() for
      // details.
      //
      const string ns ("config." + n);
      for (scope* s (&rs); s != nullptr; s = s->parent_scope ())
      {
        for (auto p (s->vars.lookup_namespace (ns));
             p.first != p.second;
             ++p.first)
        {
          const variable* v (&p.first->first.get ());

          // This can be one of the overrides (__override, __prefix, etc).
          //
          if (size_t n = v->override ())
            v = vp.find (string (v->name, 0, n));

          auto match_tail = [&ns, v] (const char* t)
          {
            return v->name.compare (ns.size () + 1, string::npos, t) == 0;
          };

          // Ignore config.*.configured and user-supplied names.
          //
          if (v->name.size () <= ns.size () ||
              (!match_tail ("configured") &&
               find_if (ig.begin (), ig.end (), match_tail) == ig.end ()))
            return true;
        }
      }

      return false;
    }

    bool
    unconfigured (scope& rs, const string& n)
    {
      // Pattern-typed as bool.
      //
      const variable& var (
        rs.var_pool ().insert ("config." + n + ".configured"));

      save_variable (rs, var);

      auto l (rs[var]); // Include inherited values.
      return l && !cast<bool> (l);
    }

    bool
    unconfigured (scope& rs, const string& n, bool v)
    {
      // Pattern-typed as bool.
      //
      const variable& var (
        rs.var_pool ().insert ("config." + n + ".configured"));

      save_variable (rs, var);

      value& x (rs.assign (var));

      if (x.null || cast<bool> (x) != !v)
      {
        x = !v;
        return true;
      }
      else
        return false;
    }

    pair<variable_origin, lookup>
    origin (const scope& rs, const string& n)
    {
      const variable* var (rs.ctx.var_pool.find (n));

      if (var == nullptr)
      {
        if (n.compare (0, 7, "config.") != 0)
          throw invalid_argument ("config.* variable expected");

        return make_pair (variable_origin::undefined, lookup ());
      }

      return origin (rs, *var);
    }

    pair<variable_origin, lookup>
    origin (const scope& rs, const variable& var)
    {
      // Make sure this is a config.* variable. This could matter since we
      // rely on the semantics of value::extra. We could also detect
      // special variables like config.booted, some config.config.*, etc.,
      // (see config_save() for details) but that seems harmless.
      //
      if (var.name.compare (0, 7, "config.") != 0)
        throw invalid_argument ("config.* variable expected");

      return origin (rs, var, rs.lookup_original (var));
    }

    pair<variable_origin, lookup>
    origin (const scope& rs, const variable& var, pair<lookup, size_t> org)
    {
      pair<lookup, size_t> ovr (var.overrides == nullptr
                                ? org
                                : rs.lookup_override (var, org));

      if (!ovr.first.defined ())
        return make_pair (variable_origin::undefined, lookup ());

      if (org.first != ovr.first)
        return make_pair (variable_origin::override_, ovr.first);

      return make_pair (org.first->extra == 1
                        ? variable_origin::default_
                        : variable_origin::buildfile,
                        org.first);
    }
  }
}