aboutsummaryrefslogtreecommitdiff
path: root/build/install/rule.cxx
blob: 854832c1f84829e9b796e5113b50e237b8eec295 (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// file      : build/install/rule.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <build/install/rule>

#include <butl/process>
#include <butl/filesystem>

#include <build/scope>
#include <build/target>
#include <build/algorithm>
#include <build/diagnostics>

#include <build/config/utility>

using namespace std;
using namespace butl;

namespace build
{
  namespace install
  {
    // Lookup the install or install.* variable. Return NULL if
    // not found or if the value is the special 'false' name (which
    // means do not install). T is either scope or target.
    //
    template <typename T>
    static const dir_path*
    lookup (T& t, const string& var)
    {
      auto l (t[var]);

      if (!l)
        return nullptr;

      const dir_path& r (as<dir_path> (*l));
      return r.simple () && r.string () == "false" ? nullptr : &r;
    }

    // alias_rule
    //
    match_result alias_rule::
    match (action, target& t, const std::string&) const
    {
      return t;
    }

    recipe alias_rule::
    apply (action a, target& t, const match_result&) const
    {
      tracer trace ("install::alias_rule::apply");

      for (prerequisite p: group_prerequisites (t))
      {
        target& pt (search (p));

        // Check if this prerequisite is explicitly "not installable",
        // that is, there is the 'install' variable and its value is
        // false.
        //
        // At first, this might seem redundand since we could have let
        // the file_rule below take care of it. The nuance is this: this
        // prerequsite can be in a different subproject that hasn't loaded
        // the install module (and therefore has no file_rule registered).
        // The typical example would be the 'tests' subproject.
        //
        auto l (pt["install"]);

        if (l && as<dir_path> (*l).string () == "false")
        {
          level5 ([&]{trace << "ignoring " << pt;});
          continue;
        }

        build::match (a, pt);
        t.prerequisite_targets.push_back (&pt);
      }

      return default_recipe;
    }

    // file_rule
    //

    match_result file_rule::
    match (action a, target& t, const std::string&) const
    {
      // First determine if this target should be installed (called
      // "installable" for short).
      //
      match_result mr (t, lookup (t, "install") != nullptr);

      // If this is the update pre-operation, change the recipe action
      // to (update, 0) (i.e., "unconditional update").
      //
      if (mr.bvalue && a.operation () == update_id)
        mr.recipe_action = action (a.meta_operation (), update_id);

      return mr;
    }

    target* file_rule::
    filter (action, target& t, prerequisite_member p) const
    {
      target& pt (p.search ());
      return pt.in (t.root_scope ()) ? &pt : nullptr;
    }

    recipe file_rule::
    apply (action a, target& t, const match_result& mr) const
    {
      if (!mr.bvalue) // Not installable.
        return noop_recipe;

      // Ok, if we are here, then this means:
      //
      // 1. This target is installable.
      // 2. The action is either
      //    a. (perform, install, 0) or
      //    b. (*, update, install)
      //
      // In both cases, the next step is to search, match, and collect
      // all the installable prerequisites.
      //
      // @@ Perhaps if [noinstall] will be handled by the
      // group_prerequisite_members machinery, then we can just
      // run standard search_and_match()? Will need an indicator
      // that it was forced (e.g., [install]) for filter() below.
      //
      for (prerequisite_member p: group_prerequisite_members (a, t))
      {
        // Ignore unresolved targets that are imported from other projects.
        // We are definitely not installing those.
        //
        if (p.proj () != nullptr)
          continue;

        // @@ This is where we will handle [noinstall].
        //

        // Let a customized rule have its say.
        //
        // @@ This will be skipped if forced with [install]?
        //
        target* pt (filter (a, t, p));
        if (pt == nullptr)
          continue;

        build::match (a, *pt);

        // If the matched rule returned noop_recipe, then the target
        // state will be set to unchanged as an optimization. Use this
        // knowledge to optimize things on our side as well since this
        // will help a lot in case of any static installable content
        // (headers, documentation, etc).
        //
        if (pt->state () != target_state::unchanged)
          t.prerequisite_targets.push_back (pt);
        else
          unmatch (a, *pt); // No intent to execute.
      }

      // This is where we diverge depending on the operation. In the
      // update pre-operation, we need to make sure that this target
      // as well as all its installable prerequisites are up to date.
      //
      if (a.operation () == update_id)
      {
        // Save the prerequisite targets that we found since the
        // call to match_delegate() below will wipe them out.
        //
        target::prerequisite_targets_type p;

        if (!t.prerequisite_targets.empty ())
          p.swap (t.prerequisite_targets);

        // Find the "real" update rule, that is, the rule that would
        // have been found if we signalled that we do not match from
        // match() above.
        //
        recipe d (match_delegate (a, t).first);

        // If we have no installable prerequisites, then simply redirect
        // to it.
        //
        if (p.empty ())
          return d;

        // Ok, the worst case scenario: we need to cause update of
        // prerequisite targets and also delegate to the real update.
        //
        return [pt = move (p), dr = move (d)]
          (action a, target& t) mutable -> target_state
        {
          // Do the target update first.
          //
          target_state r (execute_delegate (dr, a, t));

          // Swap our prerequisite targets back in and execute.
          //
          t.prerequisite_targets.swap (pt);
          r |= execute_prerequisites (a, t);
          pt.swap (t.prerequisite_targets); // In case we get re-executed.

          return r;
        };
      }
      else
        return &perform_install;
    }

    struct install_dir
    {
      dir_path dir;
      string sudo;
      string cmd; //@@ VAR type
      const_strings_value options {nullptr};
      string mode;
      string dir_mode;
    };

    // install -d <dir>
    //
    static void
    install (const install_dir& base, const dir_path& d)
    {
      path reld (relative (d));

      cstrings args;

      if (!base.sudo.empty ())
        args.push_back (base.sudo.c_str ());

      args.push_back (base.cmd.c_str ());
      args.push_back ("-d");

      if (base.options.d != nullptr) //@@ VAR
        config::append_options (args, base.options);

      args.push_back ("-m");
      args.push_back (base.dir_mode.c_str ());
      args.push_back (reld.string ().c_str ());
      args.push_back (nullptr);

      if (verb >= 2)
        print_process (args);
      else if (verb)
        text << "install " << d;

      try
      {
        process pr (args.data ());

        if (!pr.wait ())
          throw failed ();
      }
      catch (const process_error& e)
      {
        error << "unable to execute " << args[0] << ": " << e.what ();

        if (e.child ())
          exit (1);

        throw failed ();
      }
    }

    // install <file> <dir>
    //
    static void
    install (const install_dir& base, file& t)
    {
      path reld (relative (base.dir));
      path relf (relative (t.path ()));

      cstrings args;

      if (!base.sudo.empty ())
        args.push_back (base.sudo.c_str ());

      args.push_back (base.cmd.c_str ());

      if (base.options.d != nullptr) //@@ VAR
        config::append_options (args, base.options);

      args.push_back ("-m");
      args.push_back (base.mode.c_str ());
      args.push_back (relf.string ().c_str ());
      args.push_back (reld.string ().c_str ());
      args.push_back (nullptr);

      if (verb >= 2)
        print_process (args);
      else if (verb)
        text << "install " << t;

      try
      {
        process pr (args.data ());

        if (!pr.wait ())
          throw failed ();
      }
      catch (const process_error& e)
      {
        error << "unable to execute " << args[0] << ": " << e.what ();

        if (e.child ())
          exit (1);

        throw failed ();
      }
    }

    // Resolve installation directory name to absolute directory path,
    // creating leading directories as necessary.
    //
    static install_dir
    resolve (scope& s, dir_path d, const string* var = nullptr)
    {
      install_dir r;

      if (d.absolute ())
      {
        d.normalize ();

        // Make sure it already exists (this will normally be
        // install.root with everything else defined in term of it).
        //
        if (!dir_exists (d))
          fail << "installation directory " << d << " does not exist";
      }
      else
      {
        // If it is relative, then the first component is treated
        // as the installation directory name, e.g., bin, sbin, lib,
        // etc. Look it up and recurse.
        //
        const string& sn (*d.begin ());
        const string var ("install." + sn);
        if (const dir_path* dn = lookup (s, var))
        {
          r = resolve (s, *dn, &var);
          d = r.dir / dir_path (++d.begin (), d.end ());
          d.normalize ();

          if (!dir_exists (d))
            install (r, d); // install -d
        }
        else
          fail << "unknown installation directory name " << sn <<
            info << "did you forget to specify config." << var << "?";
      }

      r.dir = move (d);

      // Override components in install_dir if we have our own.
      //
      if (var != nullptr)
      {
        if (auto l = s[*var + ".sudo"])     r.sudo = as<string> (*l);
        if (auto l = s[*var + ".cmd"])      r.cmd = as<string> (*l);
        if (auto l = s[*var + ".mode"])     r.mode = as<string> (*l);
        if (auto l = s[*var + ".dir_mode"]) r.dir_mode = as<string> (*l);
        if (auto l = s[*var + ".options"])  r.options = as<strings> (*l);
      }

      // Set defaults for unspecified components.
      //
      if (r.cmd.empty ()) r.cmd = "install";
      if (r.mode.empty ()) r.mode = "644";
      if (r.dir_mode.empty ()) r.dir_mode = "755";

      return r;
    }

    target_state file_rule::
    perform_install (action a, target& t)
    {
      file& ft (static_cast<file&> (t));
      assert (!ft.path ().empty ()); // Should have been assigned by update.

      // First handle installable prerequisites.
      //
      target_state r (execute_prerequisites (a, t));

      // Resolve and, if necessary, create target directory.
      //
      install_dir d (
        resolve (t.base_scope (),
                 as<dir_path> (*t["install"]))); // We know it's there.

      // Override mode if one was specified.
      //
      if (auto l = t["install.mode"])
        d.mode = as<string> (*l);

      install (d, ft);
      return (r |= target_state::changed);
    }
  }
}