aboutsummaryrefslogtreecommitdiff
path: root/build2/algorithm.ixx
blob: b8c33d4bd5ad60304a06afd9955d4b5f1ca1f577 (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
// 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 void
  match (slock& ml, action a, target& t)
  {
    assert (phase == run_phase::search_match);

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

    //@@ MT
    //
    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 (phase == run_phase::search_match);

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

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

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

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

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

  // 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);
  }
}