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

#include <libbuild2/scope.hxx>
#include <libbuild2/context.hxx> // current_mif

namespace build2
{
  namespace config
  {
    template <typename T>
    pair<lookup, bool>
    required (scope& rs,
              const variable& var,
              T&& def_val,
              bool def_ovr,
              uint64_t sflags)
    {
      // Note: see also omitted() if changing anything here.

      save_variable (rs, var, sflags);

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

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

      // The interaction with command line overrides can get tricky. For
      // example, the override to default 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 (rs)))
      {
        value& v (rs.assign (var) = std::forward<T> (def_val)); // VC14
        v.extra = true; // Default value flag.

        n = (sflags & save_default_commented) == 0; // Absence means default.
        l = lookup (v, var, rs);
        org = make_pair (l, 1); // Lookup depth is 1 since it's in rs.vars.
      }
      // Treat an inherited value that was set to default as new.
      //
      else if (l->extra)
        n = (sflags & save_default_commented) == 0; // Absence means default.

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

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

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