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

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

namespace build2
{
  inline target&
  search (prerequisite& p)
  {
    assert (phase == run_phase::search_match);

    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.out, k.tk.name, k.tk.ext}, k.scope});
  }

  inline target&
  search (const target_type& type,
          const dir_path& dir,
          const dir_path& out,
          const string& name,
          const string* ext,
          const scope* scope,
          const optional<string>& proj)
  {
    return search (
      prerequisite_key {
        proj,
        {
          &type,
          &dir,
          &out,
          &name,
          ext != nullptr ? optional<string> (*ext) : nullopt
        },
        scope});
  }

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

  pair<const rule*, match_result>
  match_impl (slock&, action, target&, bool apply, const rule* skip = nullptr);

  inline bool
  match (slock& ml, action a, target& t)
  {
    assert (phase == run_phase::search_match);

    if (!t.recipe (a))
      match_impl (ml, a, t, true);

    dependency_count.fetch_add (1, std::memory_order_release);
    bool r (t.dependents++ != 0); // Safe if someone else is also a dependent.

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

    return r;
  }

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

    assert (phase == run_phase::search_match);

#ifndef NDEBUG
    size_t td (t.dependents--);
    size_t gd (dependency_count--);
    assert (td != 0 && gd != 0);
#else
    t.dependents.fetch_sub (1, std::memory_order_release);
    dependency_count.fetch_sub (1, std::memory_order_release);
#endif
  }

  inline pair<recipe, action>
  match_delegate (slock& ml, action a, target& t, const rule& r)
  {
    assert (phase == run_phase::search_match);

    auto rp (match_impl (ml, a, t, false, &r));
    const match_result& mr (rp.second);
    return make_pair (rp.first->apply (ml, mr.recipe_action, t),
                      mr.recipe_action);
  }

  group_view
  resolve_group_members_impl (slock& ml, action, target&);

  inline group_view
  resolve_group_members (slock& ml, action a, target& g)
  {
    assert (phase == run_phase::search_match);

    group_view r (g.group_members (a));
    return r.members != nullptr ? r : resolve_group_members_impl (ml, a, g);
  }

  void
  search_and_match_prerequisites (slock&, action, target&, const scope*);

  void
  search_and_match_prerequisite_members (slock&, action, target&, const scope*);

  inline void
  search_and_match_prerequisites (slock& ml, action a, target& t)
  {
    search_and_match_prerequisites (
      ml,
      a,
      t,
      (a.operation () != clean_id ? nullptr : &t.root_scope ()));
  }

  inline void
  search_and_match_prerequisite_members (slock& ml, action a, target& t)
  {
    if (a.operation () != clean_id)
      search_and_match_prerequisite_members (ml, a, t, nullptr);
    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 (ml, a, t, &t.root_scope ());
  }

  inline void
  search_and_match_prerequisites (slock& ml,
                                  action a,
                                  target& t,
                                  const scope& s)
  {
    search_and_match_prerequisites (ml, a, t, &s);
  }

  inline void
  search_and_match_prerequisite_members (slock& ml,
                                         action a,
                                         target& t,
                                         const scope& s)
  {
    search_and_match_prerequisite_members (ml, a, t, &s);
  }

  target_state
  execute (action, const target&, size_t, atomic_count*);

  inline target_state
  execute (action a, const target& t)
  {
    return execute (a, t, 0, nullptr);
  }

  inline target_state
  execute_async (action a, const target& t, size_t sc, atomic_count& tc)
  {
    return execute (a, t, sc, &tc);
  }

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

  inline target_state
  straight_execute_prerequisites (action a, const target& t)
  {
    auto& p (t.prerequisite_targets);
    return straight_execute_members (a, t, p.data (), p.size ());
  }

  inline target_state
  reverse_execute_prerequisites (action a, const target& t)
  {
    auto& p (t.prerequisite_targets);
    return reverse_execute_members (a, t, p.data (), p.size ());
  }

  inline target_state
  execute_prerequisites (action a, const target& t)
  {
    auto& p (t.prerequisite_targets);
    return execute_members (a, t, p.data (), p.size ());
  }

  // If the first argument is NULL, then the result is treated as a boolean
  // value.
  //
  pair<const target*, target_state>
  execute_prerequisites (const target_type*,
                         action, const target&,
                         const timestamp&, const prerequisite_filter&);

  inline pair<bool, target_state>
  execute_prerequisites (action a, const target& t,
                         const timestamp& mt, const prerequisite_filter& pf)
  {
    auto p (execute_prerequisites (nullptr, a, t, mt, pf));
    return make_pair (p.first != nullptr, p.second);
  }

  template <typename T>
  inline pair<const T*, target_state>
  execute_prerequisites (action a, const target& t,
                         const timestamp& mt, const prerequisite_filter& pf)
  {
    auto p (execute_prerequisites (T::static_type, a, t, mt, pf));
    return make_pair (static_cast<const T*> (p.first), p.second);
  }

  inline pair<const target*, target_state>
  execute_prerequisites (const target_type& tt,
                         action a, const target& t,
                         const timestamp& mt, const prerequisite_filter& pf)
  {
    return execute_prerequisites (&tt, a, t, mt, pf);
  }

  template <typename T>
  inline pair<const T*, target_state>
  execute_prerequisites (const target_type& tt,
                         action a, const target& t,
                         const timestamp& mt, const prerequisite_filter& pf)
  {
    auto p (execute_prerequisites (tt, a, t, mt, pf));
    return make_pair (static_cast<const T*> (p.first), p.second);
  }

  inline target_state
  execute_members (action a, const target& t, const target* ts[], size_t n)
  {
    return current_mode == execution_mode::first
      ? straight_execute_members (a, t, ts, n)
      : reverse_execute_members (a, t, ts, n);
  }
}