aboutsummaryrefslogtreecommitdiff
path: root/build2/algorithm.ixx
blob: cdb76b5db65647d1b02dbbf34ff5b5ba76d8fa4d (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
// file      : build2/algorithm.ixx -*- C++ -*-
// copyright : Copyright (c) 2014-2016 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <utility> // pair

#include <build2/rule>
#include <build2/prerequisite>
#include <build2/context>

namespace build2
{
  inline target&
  search (prerequisite& p)
  {
    if (p.target == nullptr)
      p.target = &search (p.key ());

    return *p.target;
  }

  inline target&
  search (const target_type& t, const prerequisite_key& k)
  {
    return search (
      prerequisite_key
        {k.proj, {&t, k.tk.dir, k.tk.name, k.tk.ext}, k.scope});
  }

  inline target&
  search (const target_type& type,
          const dir_path& dir,
          const std::string& name,
          const std::string* ext,
          scope* scope)
  {
    return search (
      prerequisite_key {nullptr, {&type, &dir, &name, ext}, scope});
  }

  template <typename T>
  inline T&
  search (const dir_path& dir,
          const std::string& name,
          const std::string* ext,
          scope* scope)
  {
    return static_cast<T&> (search (T::static_type, dir, name, ext, scope));
  }

  std::pair<const rule*, match_result>
  match_impl (action, target&, bool apply);

  inline void
  match (action a, target& t)
  {
    if (!t.recipe (a))
      match_impl (a, t, true);

    t.dependents++;
    dependency_count++;

    // text << "M " << t << ": " << t.dependents << " " << dependency_count;
  }

  inline void
  unmatch (action, target& t)
  {
    // text << "U " << t << ": " << t.dependents << " " << dependency_count;

    assert (t.dependents != 0 && dependency_count != 0);
    t.dependents--;
    dependency_count--;
  }

  inline void
  match_only (action a, target& t)
  {
    if (!t.recipe (a))
      match_impl (a, t, false);
  }

  inline std::pair<recipe, action>
  match_delegate (action a, target& t)
  {
    auto rp (match_impl (a, t, false));
    const match_result& mr (rp.second);
    return std::make_pair (rp.first->apply (mr.recipe_action, t, mr),
                           mr.recipe_action);
  }

  group_view
  resolve_group_members_impl (action, target&);

  inline group_view
  resolve_group_members (action a, target& g)
  {
    group_view r (g.group_members (a));
    return r.members != nullptr ? r : resolve_group_members_impl (a, g);
  }

  inline void
  search_and_match_prerequisites (action a, target& t)
  {
    search_and_match_prerequisites (
      a,
      t,
      a.operation () != clean_id
      ? dir_path ()
      : t.strong_scope ().out_path ());
  }

  inline void
  search_and_match_prerequisite_members (action a, target& t)
  {
    if (a.operation () != clean_id)
      search_and_match_prerequisite_members (a, t, dir_path ());
    else
      // Note that here we don't iterate over members even for see-
      // through groups since the group target should clean eveything
      // up. A bit of an optimization.
      //
      search_and_match_prerequisites (a, t, t.strong_scope ().out_path ());
  }

  target_state
  execute_impl (action, target&);

  inline target_state
  execute (action a, target& t)
  {
    // text << "E " << t << ": " << t.dependents << " " << dependency_count;

    if (dependency_count != 0) // Re-examination of a postponed target?
    {
      assert (t.dependents != 0);
      t.dependents--;
      dependency_count--;
    }

    switch (target_state ts = t.state ())
    {
    case target_state::unchanged:
    case target_state::changed:
      return ts;
    default:
      {
        // Handle the "last" execution mode.
        //
        // This gets interesting when we consider interaction with
        // groups. It seem to make sense to treat group members as
        // dependents of the group, so, for example, if we try to
        // clean the group via three of its members, only the last
        // attempt will actually execute the clean. This means that
        // when we match a group member, inside we should also match
        // the group in order to increment the dependents count. This
        // seems to be a natural requirement: if we are delegating to
        // the group, we need to find a recipe for it, just like we
        // would for a prerequisite.
        //
        // Note that below we are going to change the group state
        // to postponed. This is not a mistake: until we execute
        // the recipe, we want to keep returning postponed. And
        // once the recipe is executed, it will reset the state
        // to group (see group_action()). To put it another way,
        // the execution of this member is postponed, not of the
        // group.
        //
        // One important invariant to keep in mind: the return
        // value from execute() should always be the same as what
        // would get returned by a subsequent call to state().
        //
        if (current_mode == execution_mode::last && t.dependents != 0)
          return (t.raw_state = target_state::postponed);

        return execute_impl (a, t);
      }
    }
  }

  inline target_state
  execute_delegate (const recipe& r, action a, target& t)
  {
    return r (a, t);
  }

  inline target_state
  execute_direct (action a, target& t)
  {
    switch (target_state ts = t.state ())
    {
    case target_state::unchanged:
    case target_state::changed: return ts;
    default: return execute_impl (a, t);
    }
  }
}