aboutsummaryrefslogtreecommitdiff
path: root/build/scope
blob: f9492e68711570c0b48476c19709bb77447b358a (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
// file      : build/scope -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#ifndef BUILD_SCOPE
#define BUILD_SCOPE

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

#include <build/path>
#include <build/path-map>
#include <build/variable>
#include <build/prerequisite>
#include <build/operation>

namespace build
{
  class scope
  {
  public:
    const dir_path&
    path () const {return i_->first;} // Absolute and normalized.

    const dir_path&
    src_path () const {return *src_path_;} // Corresponding src path.

    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_;}

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

    // Variable lookup. Note that this is find, not find or insert like
    // in the variable_map, because we also search in outer scopes. For
    // the latter use the variables map directly.
    //
  public:
    value_proxy
    operator[] (const std::string&);

    value_proxy
    operator[] (const variable&);

    const dir_path* src_path_ {nullptr}; // Cached src_{root,base} var value.

  public:
    variable_map variables;

    const variable_map&
    ro_variables () const {return variables;}

    prerequisite_set prerequisites;

    // Meta/operations supported by this project (set on the root
    // scope only).
    //
    meta_operation_table meta_operations;
    operation_table 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;

    // A map of buildfiles to trigger functions that are executed when
    // such files are sourced. The path must be absolute and normalized.
    //
    // The passed path is the buildfile. If the returned value is true,
    // then the file is sourced. If false -- the file is ignored. Note
    // that currently triggers can only be registered on the project's
    // root scope.
    //
    using trigger_type = std::function<bool (scope&, const path_type&)>;
    std::unordered_map<path_type, trigger_type> triggers;

  private:
    friend class scope_map;
    friend class temp_scope;

    typedef dir_path_map<scope>::const_iterator iterator;

    scope (): variables (*this) {}

    iterator i_;
    scope* parent_;
    scope* root_;
  };

  // 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/prerequsites 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) {i_ = p.i_; parent_ = &p; root_ = p.root_;}
  };

  class scope_map: public dir_path_map<scope>
  {
  public:
    // Note that we assume the first insertion into the map is that
    // of the global scope.
    //
    std::pair<scope&, bool>
    insert (const dir_path&, bool root);

    scope&
    operator[] (const dir_path& p) {return insert (p, false).first;}

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

    scope&
    find (const path& p)
    {
      // 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 ()));
    }

  private:
    typedef dir_path_map<scope> base;
  };

  extern scope_map scopes;
  extern scope* global_scope;
}

#endif // BUILD_SCOPE