aboutsummaryrefslogtreecommitdiff
path: root/build/scope
blob: f86d0ed3e0a26da83f736e58f04f44076c302e2b (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
// 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>

namespace build
{
  class scope
  {
  public:
    typedef build::path path_type;

    const path_type&
    path () const {return i_->first;} // Absolute and normalized.

    scope*
    parent () const {return parent_;}

    // Variable lookup.
    //
  public:
    value_proxy
    operator[] (const std::string&);

    value_proxy
    operator[] (const variable&);

  private:
    friend class scope_map;

    typedef path_map<scope>::const_iterator iterator;

    scope (): variables (*this) {}

    void
    init (const iterator& i, scope* p) {i_ = i; parent_ = p;}

    void
    parent (scope& p) {parent_ = &p;}

  public:
    variable_map variables;
    prerequisite_set prerequisites;

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


    // A map of buildfiles to trigger functions that are executed when
    // such files are sourced. The path is is assumed to be relative to
    // the src directory corresponding to this scope.
    //
    // The passed path is the actual, absolute buildfile path. 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 root scope.
    //
    using trigger_type = std::function<bool (scope&, const path_type&)>;
    std::unordered_map<path_type, trigger_type> triggers;

  private:
    iterator i_;
    scope* parent_;
  };

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

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

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

  private:
    typedef path_map<scope> base;
  };

  extern scope_map scopes;
  extern scope* root_scope;
}

#endif // BUILD_SCOPE