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

#include <build2/scope>
#include <build2/context>

namespace build2
{
  namespace config
  {
    template <typename T>
    pair<reference_wrapper<const value>, bool>
    required (scope& root, const variable& var, const T& def_val, bool def_ovr)
    {
      if (current_mif->id == configure_id)
        save_variable (root, var);

      pair<lookup, size_t> org (root.find_original (var));
      lookup l (org.first);
      bool n (false);

      // The interaction with command line overrides can get tricky. For
      // example, the override to defaul value could make (non-recursive)
      // command line override in the outer scope no longer apply. So what we
      // are going to do is first ignore overrides and perform the normal
      // logic on the original. Then we apply the overrides on the result.
      //
      if (!l.defined () || (def_ovr && !l.belongs (root)))
      {
        l = lookup ((root.assign (var) = def_val), root);
        org = make_pair (l, 1); // Lookup depth is 1 since in root.vars.
        n = true;
      }

      if (var.override != nullptr)
      {
        pair<lookup, size_t> ovr (root.find_override (var, move (org)));

        if (l != ovr.first) // Overriden?
        {
          l = move (ovr.first);

          // Overriden and not inherited (same logic as in save_config()).
          //
          n = l.belongs (root) || l.belongs (*global_scope);
        }
      }

      return pair<reference_wrapper<const value>, bool> (*l, n);
    }
  }
}