aboutsummaryrefslogtreecommitdiff
path: root/build/rule.cxx
blob: fb5af3f020b9cda7f7a8bd00d6b5fbf47e8ce52f (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// file      : build/rule.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#include <build/rule>

#include <utility>      // move()
#include <system_error>

#include <build/scope>
#include <build/algorithm>
#include <build/diagnostics>
#include <build/timestamp>
#include <build/filesystem>
#include <build/context>

using namespace std;

namespace build
{
  operation_rule_map rules;

  // path_rule
  //
  // Note that this rule is special. It is the last, fallback rule. If
  // it doesn't match, then no other rule can possibly match and we have
  // an error. It also cannot be ambigious with any other rule. As a
  // result the below implementation bends or ignores quite a few rules
  // that normal implementations should follow. So you probably shouldn't
  // use it as a guide to implement your own, normal, rules.
  //
  void* path_rule::
  match (action a, target& t, const string&) const
  {
    // While strictly speaking we should check for the file's existence
    // for every action (because that's the condition for us matching),
    // for some actions this is clearly a waste. Say, perform_clean: we
    // are not doing anything for this action so not checking if the file
    // exists seems harmless. What about, say, configure_update? Again,
    // whether we match or not, there is nothing to be done for this
    // action. And who knows, maybe the file doesn't exist during
    // configure_update but will magically appear during perform_update.
    // So the overall guideline seems to be this: if we don't do anything
    // for the action (other than performing it on the prerequisites),
    // then we match.
    //
    switch (a)
    {
    case perform_update_id:
      {
        // @@ TODO:
        //
        // - need to try all the target-type-specific extensions, just
        //   like search_existing_file().
        //
        path_target& pt (dynamic_cast<path_target&> (t));

        if (pt.path ().empty ())
        {
          path p (t.dir / path (pt.name));

          // @@ TMP: target name as an extension.
          //
          const string& e (pt.ext != nullptr ? *pt.ext : pt.type ().name);

          if (!e.empty ())
          {
            p += '.';
            p += e;
          }

          // While strictly speaking we shouldn't do this in match(),
          // no other rule should ever be ambiguous with the fallback
          // one.
          //
          pt.path (move (p));
        }

        return pt.mtime () != timestamp_nonexistent ? &t : nullptr;
      }
    default:
      return &t;
    }
  }

  recipe path_rule::
  apply (action a, target& t, void*) const
  {
    // Update triggers the update of this target's prerequisites
    // so it would seem natural that we should also trigger their
    // cleanup. However, this possibility is rather theoretical
    // since such an update would render this target out of date
    // which in turn would lead to an error. So until we see a
    // real use-case for this functionality, we simply ignore
    // the clean operation.
    //
    if (a.operation () == clean_id)
      return noop_recipe;

    // Search and match all the prerequisites.
    //
    search_and_match (a, t);

    return a == perform_update_id
      ? &perform_update
      : t.prerequisites.empty () ? noop_recipe : default_recipe;
  }

  target_state path_rule::
  perform_update (action a, target& t)
  {
    // Make sure the target is not older than any of its prerequisites.
    //
    timestamp mt (dynamic_cast<path_target&> (t).mtime ());

    for (const prerequisite& p: t.prerequisites)
    {
      target& pt (*p.target);
      target_state ts (execute (a, pt));

      // If this is an mtime-based target, then compare timestamps.
      //
      if (auto mpt = dynamic_cast<const mtime_target*> (&pt))
      {
        timestamp mp (mpt->mtime ());

        if (mt < mp)
          fail << "no recipe to update target " << t <<
            info << "prerequisite " << pt << " is ahead of " << t
               << " by " << (mp - mt);
      }
      else
      {
        // Otherwise we assume the prerequisite is newer if it was changed.
        //
        if (ts == target_state::changed)
          fail << "no recipe to update target " << t <<
            info << "prerequisite " << pt << " is ahead of " << t
               << " because it was updated";
      }
    }

    return target_state::unchanged;
  }

  // dir_rule
  //
  void* dir_rule::
  match (action a, target& t, const string&) const
  {
    return &t;
  }

  recipe dir_rule::
  apply (action a, target& t, void*) const
  {
    // When cleaning, ignore prerequisites that are not in the same
    // or a subdirectory of ours. For default, we don't do anything
    // other than letting our prerequisites do their thing.
    //
    switch (a.operation ())
    {
    case default_id:
    case update_id: search_and_match (a, t); break;
    case clean_id: search_and_match (a, t, t.dir); break;
    default: assert (false);
    }

    return default_recipe;
  }

  // fsdir_rule
  //
  void* fsdir_rule::
  match (action a, target& t, const string&) const
  {
    return &t;
  }

  recipe fsdir_rule::
  apply (action a, target& t, void*) const
  {
    switch (a.operation ())
    {
    // For default, we don't do anything other than letting our
    // prerequisites do their thing.
    //
    case default_id:
    case update_id:
      search_and_match (a, t);
      break;
    // For clean, ignore prerequisites that are not in the same or a
    // subdirectory of ours (if t.dir is foo/bar/, then "we" are bar
    // and our directory is foo/). Just meditate on it a bit and you
    // will see the light.
    //
    case clean_id:
      search_and_match (a, t, t.dir.root () ? t.dir : t.dir.directory ());
      break;
    default:
      assert (false);
    }

    // Inject dependency on the parent directory.
    //
    inject_parent_fsdir (a, t);

    switch (a)
    {
    case perform_update_id: return &perform_update;
    case perform_clean_id: return &perform_clean;
    default: return default_recipe; // Forward to prerequisites.
    }
  }

  target_state fsdir_rule::
  perform_update (action a, target& t)
  {
    target_state ts (target_state::unchanged);

    // First update prerequisites (e.g. create parent directories)
    // then create this directory.
    //
    if (!t.prerequisites.empty ())
      ts = execute_prerequisites (a, t);

    const path& d (t.dir); // Everything is in t.dir.

    // Generally, it is probably correct to assume that in the majority
    // of cases the directory will already exist. If so, then we are
    // going to get better performance by first checking if it indeed
    // exists. See try_mkdir() for details.
    //
    if (!dir_exists (d))
    {
      if (verb >= 1)
        text << "mkdir " << d.string ();
      else
        text << "mkdir " << t;

      try
      {
        try_mkdir (d);
      }
      catch (const system_error& e)
      {
        fail << "unable to create directory " << d.string () << ": "
             << e.what ();
      }

      ts = target_state::changed;
    }

    return ts;
  }

  target_state fsdir_rule::
  perform_clean (action a, target& t)
  {
    // The reverse order of update: first delete this directory,
    // then clean prerequisites (e.g., delete parent directories).
    //
    rmdir_status rs (rmdir (t.dir, t));

    target_state ts (target_state::unchanged);

    if (!t.prerequisites.empty ())
      ts = reverse_execute_prerequisites (a, t);

    // If we couldn't remove the directory, return postponed meaning
    // that the operation could not be performed at this time.
    //
    switch (rs)
    {
    case rmdir_status::success: return target_state::changed;
    case rmdir_status::not_empty: return target_state::postponed;
    default: return ts;
    }
  }
}