aboutsummaryrefslogtreecommitdiff
path: root/build/scope
blob: 0cb319f2c67d6fc64dcb7544a7b0c9b3fda7cd35 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// file      : build/scope -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#ifndef BUILD_SCOPE
#define BUILD_SCOPE

#include <functional>    // function
#include <unordered_set>
#include <unordered_map>

#include <butl/path-map>

#include <build/types>
#include <build/utility>

#include <build/module>
#include <build/variable>
#include <build/prerequisite>
#include <build/target-type>
#include <build/rule-map>
#include <build/operation>

namespace build
{
  class scope
  {
  public:
    // Absolute and normalized.
    //
    const dir_path&
    out_path () const {return *out_path_;}

    const dir_path&
    src_path () const {return *src_path_;}

    // These are pointers to the keys in scope_map.
    //
    const dir_path* out_path_ {nullptr};
    const dir_path* src_path_ {nullptr};

    bool
    root () const {return root_ == this;}

    scope*
    parent_scope () const {return parent_;}

    // Root scope of this scope or NULL if this scope is not (yet)
    // in any (known) project. Note that if the scope itself is
    // root, then this function return this. To get to the outer
    // root, query the root scope of the parent.
    //
    scope*
    root_scope () const {return root_;}

    // Root scope of a strong amalgamation of this scope or NULL if
    // this scope is not (yet) in any (known) project. If there is
    // no strong amalgamation, then this function returns the root
    // scope of the project (in other words, in this case a project
    // is treated as its own strong amalgamation).
    //
    scope*
    strong_scope () const
    {
      return root_ != nullptr
        ? root_->strong_ != nullptr ? root_->strong_ : root_
        : nullptr;
    }

    // Root scope of the outermost amalgamation or NULL if this scope is not
    // (yet) in any (known) project. If there is no amalgamation, then this
    // function returns the root scope of the project (in other words, in this
    // case a project is treated as its own amalgamation).
    //
    scope*
    weak_scope () const
    {
      scope* r (root_);
      if (r != nullptr)
        for (; r->parent_->root_ != nullptr; r = r->parent_->root_) ;
      return r;
    }

    // Variables.
    //
  public:
    variable_map vars;

    // Lookup, including in outer scopes. If you only want to lookup
    // in this scope, do it on the the variables map directly.
    //
    build::lookup<const value>
    operator[] (const variable& var) const
    {
      return lookup (nullptr, nullptr, var);
    }

    build::lookup<const value>
    operator[] (const std::string& name) const
    {
      return operator[] (var_pool.find (name));
    }

    // As above, but includes target type/pattern-specific variables.
    //
    build::lookup<const value>
    lookup (const target_key& tk, const variable& var) const
    {
      return lookup (tk.type, tk.name, var);
    }

    build::lookup<const value>
    lookup (const target_key& tk, const string& var) const
    {
      return lookup (tk, var_pool.find (var));
    }

    build::lookup<const value>
    lookup (const target_type& tt,
            const string& name,
            const variable& var) const
    {
      return lookup (&tt, &name, var);
    }

    build::lookup<const value>
    lookup (const target_type& tt, const string& name, const string& var) const
    {
      return lookup (tt, name, var_pool.find (var));
    }

    build::lookup<const value>
    lookup (const target_type*, const string* name, const variable&) const;

    // Return a value suitable for assignment (or append if you only
    // want to append to the value from this scope). If the variable
    // does not exist in this scope's map, then a new one with the
    // NULL value is added and returned. Otherwise the existing value
    // is returned.
    //
    value&
    assign (const variable& var) {return vars.assign (var).first.get ();}

    value&
    assign (const std::string& name) {return vars.assign (name).first.get ();}

    // Return a value suitable for appending. If the variable does not
    // exist in this scope's map, then outer scopes are searched for
    // the same variable. If found then a new variable with the found
    // value is added to this scope and returned. Otherwise this
    // function proceeds as assign().
    //
    value&
    append (const variable&);

    value&
    append (const std::string& name)
    {
      return append (var_pool.find (name));
    }

    // Target type/pattern-specific variables.
    //
    variable_type_map target_vars;

    // Prerequisite cache.
    //
  public:
    prerequisite_set prerequisites;

    // Meta/operations supported by this project (set on the root
    // scope only).
    //
    build::meta_operations meta_operations;
    build::operations operations;

    typedef build::path path_type;

    // Set of buildfiles already loaded for this scope. The included
    // buildfiles are checked against the project's root scope while
    // imported -- against the global scope (global_scope).
    //
    std::unordered_set<path_type> buildfiles;

    // Target types.
    //
  public:
    target_type_map target_types;

    const target_type*
    find_target_type (const string&, const scope** = nullptr) const;

    // Given a name, figure out its type, taking into account extensions,
    // special names (e.g., '.' and '..'), or anything else that might be
    // relevant. Also process the name (in place) by extracting the
    // extension, adjusting dir/value, etc., (note that the dir is not
    // necessarily normalized). Return NULL if not found.
    //
    const target_type*
    find_target_type (name&, const string*& ext) const;

    // Rules.
    //
  public:
    rule_map rules;

    // Modules.
    //
  public:
    loaded_module_map modules; // Only on root scope.

  public:
    bool
    empty () const
    {
      return
        vars.empty () &&
        target_vars.empty () &&
        prerequisites.empty () &&
        meta_operations.empty () &&
        operations.empty () &&
        buildfiles.empty () &&
        target_types.empty () &&
        rules.empty () &&
        modules.empty ();
    }

  private:
    friend class scope_map;
    friend class temp_scope;

    // These two from <build/file> set strong_.
    //
    friend void create_bootstrap_outer (scope&);
    friend scope& create_bootstrap_inner (scope&, const dir_path&);

    scope () = default;

    scope* parent_;
    scope* root_;
    scope* strong_ = nullptr; // Only set on root sopes.
                              // NULL means no strong amalgamtion.
  };

  // Temporary scope. The idea is to be able to create a temporary
  // scope in order not to change the variables in the current scope.
  // Such a scope is not entered in to the scope map. As a result it
  // can only be used as a temporary set of variables. In particular,
  // defining targets/prerequisites directly in such a scope will surely
  // end up badly. Defining any nested scopes will be as if defining
  // such a scope in the parent (since path() returns parent's path).
  //
  class temp_scope: public scope
  {
  public:
    temp_scope (scope& p)
    {
      out_path_ = p.out_path_;
      src_path_ = p.src_path_;
      parent_ = &p;
      root_ = p.root_;
      // No need to copy strong_ since we are never root scope.
    }
  };

  class scope_map
  {
  public:
    using map_type = butl::dir_path_map<scope*>;
    using iterator = map_type::iterator;
    using const_iterator = map_type::const_iterator;

    // Note that we assume the first insertion into the map is that
    // of the global scope. If the passed scope pointer is not NULL,
    // then insert this scope instead of a new one.
    //
    iterator
    insert (const dir_path&, scope*, bool parent, bool root);

    // Find the most qualified scope that encompasses this path.
    //
    scope&
    find (const dir_path&) const;

    scope&
    find (const path& p) const
    {
      // Natural thing to do here would be to call find (p.directory ()).
      // However, there could be a situation where the passed path is a
      // directory (i.e., the calling code does not know what it is dealing
      // with), so let's use the whole path.
      //
      return find (dir_path (p.string ()));
    }

    const_iterator begin () const {return map_.begin ();}
    const_iterator end () const {return map_.end ();}

    void
    clear ();

    ~scope_map () {clear ();}

  private:
    map_type map_;
  };

  extern scope_map scopes;
  extern scope* global_scope;
}

#endif // BUILD_SCOPE