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

#include <libbuild2/config/module.hxx>

#include <libbuild2/scope.hxx>

using namespace std;

namespace build2
{
  namespace config
  {
    bool module::
    save_variable (const variable& var,
                   optional<uint64_t> flags,
                   save_variable_function* save)
    {
      const string& n (var.name);

      // First try to find the module with the name that is the longest
      // prefix of this variable name.
      //
      auto& sm (saved_modules);
      auto i (sm.find_sup (n));

      // If no module matched, then create one based on the variable name.
      //
      if (i == sm.end ())
      {
        // Note: with 'config.' prefix.
        //
        i = sm.insert (string (n, 0, n.find ('.', 7))).first;
      }

      // Don't insert duplicates. The config.import.* vars are particularly
      // susceptible to duplication.
      //
      saved_variables& sv (i->second);
      auto j (sv.find (var));

      if (j != sv.end ())
      {
        assert (!j->flags == !flags && (!flags || *j->flags == *flags));
        return false;
      }

      sv.push_back (saved_variable {var, flags, save});
      return true;
    }

    void module::
    save_variable (scope& rs, const variable& var, optional<uint64_t> flags)
    {
      if (module* m = rs.find_module<module> (module::name))
        m->save_variable (var, flags);
    }

    bool module::
    save_module (const char* name, int prio)
    {
      return saved_modules.insert (string ("config.") += name, prio).second;
    }

    void module::
    save_module (scope& rs, const char* name, int prio)
    {
      if (module* m = rs.find_module<module> (module::name))
        m->save_module (name, prio);
    }

    bool module::
    configure_post (scope& rs, configure_post_hook* h)
    {
      if (module* m = rs.find_module<module> (module::name))
      {
        m->configure_post_.push_back (h);
        return true;
      }

      return false;
    }

    bool module::
    disfigure_pre (scope& rs, disfigure_pre_hook* h)
    {
      if (module* m = rs.find_module<module> (module::name))
      {
        m->disfigure_pre_.push_back (h);
        return true;
      }

      return false;
    }

    const string module::name ("config");
    const uint64_t module::version (1);
  }
}