aboutsummaryrefslogtreecommitdiff
path: root/bpkg/package-configuration.hxx
blob: 3ff7b5985b91101dca2b1a2699a5011aa1ff9ebf (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
// file      : bpkg/package-configuration.hxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef BPKG_PACKAGE_CONFIGURATION_HXX
#define BPKG_PACKAGE_CONFIGURATION_HXX

#include <map>

#include <libbuild2/types.hxx>        // build2::names
#include <libbuild2/config/types.hxx> // build2::config::variable_origin

#include <bpkg/types.hxx>
#include <bpkg/utility.hxx>

#include <bpkg/package.hxx>

using namespace std;

namespace bpkg
{
  class package_skeleton;

  struct config_variable_value
  {
    string name;

    // The variable_origin values have the following meaning:
    //
    // default    -- default value from the config directive
    // buildfile  -- dependent configuration (config_source::dependent)
    // override   -- user configuration      (config_source::user)
    // undefined  -- none of the above
    //
    build2::config::variable_origin origin;

    // Variable type name with absent signifying untyped.
    //
    optional<string> type;

    // If origin is not undefined, then this is the reversed variable value
    // with absent signifying NULL.
    //
    optional<build2::names> value;

    // If origin is buildfile, then this is the "originating dependent" which
    // first set this variable to this value.
    //
    optional<package_key> dependent;

    // If origin is buildfile, then this flag indicates whether the
    // originating dependent has been encountered during the negotiation
    // retry.
    //
    bool confirmed;

  public:
    void
    undefine ()
    {
      origin = build2::config::variable_origin::undefined;
      value = nullopt;
      dependent = nullopt;
      confirmed = false;
    }

    // Serialize the variable value as a command line override.
    //
    string
    serialize_cmdline () const;
  };

  void
  to_checksum (sha256&, const config_variable_value&);

  // A subset of config_variable_value for variable values set by the
  // dependents (origin is buildfile). Used to track change history.
  //
  struct dependent_config_variable_value
  {
    string                  name;
    optional<build2::names> value;
    package_key             dependent;

  public:
    string
    serialize_cmdline () const;
  };

  inline bool
  operator== (const dependent_config_variable_value& x,
              const dependent_config_variable_value& y)
  {
    return x.name == y.name && x.value == y.value && x.dependent == y.dependent;
  }

  class dependent_config_variable_values:
    public small_vector<dependent_config_variable_value, 1>
  {
  public:
    const dependent_config_variable_value*
    find (const string& name) const
    {
      auto i (find_if (begin (), end (),
                       [&name] (const dependent_config_variable_value& v)
                       {
                         return v.name == name;
                       }));
      return i != end () ? &*i : nullptr;
    }
  };

  class package_configuration: public vector<config_variable_value>
  {
  public:
    package_key package;

    explicit
    package_configuration (package_key p): package (move (p)) {}

    config_variable_value*
    find (const string& name)
    {
      auto i (find_if (begin (), end (),
                       [&name] (const config_variable_value& v)
                       {
                         return v.name == name;
                       }));
      return i != end () ? &*i : nullptr;
    }

    const config_variable_value*
    find (const string& name) const
    {
      auto i (find_if (begin (), end (),
                       [&name] (const config_variable_value& v)
                       {
                         return v.name == name;
                       }));
      return i != end () ? &*i : nullptr;
    }

    // Print buildfile and override configuration variable values as command
    // line overrides one per line with the specified indentation. After each
    // variable also print in parenthesis its origin. If overrides is not
    // NULL, then it is used to override the value/dependent information.
    //
    void
    print (diag_record&, const char* indent,
           const dependent_config_variable_values* overrides = nullptr) const;
  };

  class package_configurations: public small_vector<package_configuration, 1>
  {
  public:
    // Note: may invalidate references.
    //
    package_configuration&
    operator[] (const package_key& p)
    {
      auto i (find_if (begin (), end (),
                       [&p] (const package_configuration& pc)
                       {
                         return pc.package == p;
                       }));
      if (i != end ())
        return *i;

      push_back (package_configuration (p));
      return back ();
    }

    void
    clear ()
    {
      small_vector<package_configuration, 1>::clear ();
      change_history_.clear ();
    }

    // Implementation details.
    //
  public:
    // Note: dependent_config_variable_values must be sorted by name.
    //
    small_vector<dependent_config_variable_values, 2> change_history_;
  };

  // Up-negotiate the configuration for the specified dependencies of the
  // specified dependent. Return true if the configuration has changed.
  //
  bool
  up_negotiate_configuration (
    package_configurations&,
    package_skeleton& dependent,
    pair<size_t, size_t> position,
    const small_vector<reference_wrapper<package_skeleton>, 1>& dependencies);
}

#endif // BPKG_PACKAGE_CONFIGURATION_HXX