aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/install/rule.hxx
blob: b023af5ca3e9959242f6942a96556e875e4b77d7 (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
// file      : libbuild2/install/rule.hxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef LIBBUILD2_INSTALL_RULE_HXX
#define LIBBUILD2_INSTALL_RULE_HXX

#include <libbuild2/types.hxx>
#include <libbuild2/utility.hxx>

#include <libbuild2/rule.hxx>
#include <libbuild2/action.hxx>
#include <libbuild2/target.hxx>
#include <libbuild2/filesystem.hxx>

#include <libbuild2/export.hxx>

namespace build2
{
  namespace install
  {
    class LIBBUILD2_SYMEXPORT alias_rule: public simple_rule
    {
    public:
      virtual bool
      match (action, target&) const override;

      // Return NULL if this prerequisite should be ignored and pointer to its
      // target otherwise. In the latter case, return the match options that
      // should be used for this prerequisite (use match_extra::all_options
      // and not 0 if no match options are needed).
      //
      // The default implementation ignores prerequsites that are outside of
      // the installation scope (see install_scope() for details).
      //
      // The default implementation always returns match_extra::all_options.
      // The match_extra argument is not used by the default implementation.
      //
      // The prerequisite is passed as an iterator allowing the filter to
      // "see" inside groups.
      //
      using prerequisite_iterator =
        prerequisite_members_range<group_prerequisites>::iterator;

      virtual pair<const target*, uint64_t>
      filter (const scope*,
              action, const target&, prerequisite_iterator&,
              match_extra&) const;

      virtual pair<const target*, uint64_t>
      filter (const scope*,
              action, const target&, const prerequisite&,
              match_extra&) const;

      // Note: rule::apply() override (with match_extra).
      //
      virtual recipe
      apply (action, target&, match_extra&) const override;

      // Implementation of apply().
      //
      // If the implementation may call reapply_impl(), then the reapply
      // argument to apply_impl() must be true. Note that in this case, the
      // *_impl() functions use the prerequisite_target::data member for own
      // housekeeping.
      //
      recipe
      apply_impl (action, target&, match_extra&, bool reapply = false) const;

      // Implementation of reapply() that re-tries prerequisites that have
      // been filtered out during the reapply() call. Note that currently not
      // supported for update, only for install/uninstall.
      //
      void
      reapply_impl (action, target&, match_extra&) const;

      alias_rule () {}
      static const alias_rule instance;

    private:
      virtual recipe
      apply (action, target&) const override; // Dummy simple_rule override.
    };

    // In addition to the alias rule's semantics, this rule sees through to
    // the group's members.
    //
    // The default group_rule::instance matches any target for which it was
    // registered. It is to be used for non-see-through groups that should
    // exhibit the see-through behavior for install (see lib{} in the bin
    // module for an example).
    //
    // We also register (for all targets) another instance of this rule that
    // only matches see-through groups.
    //
    class LIBBUILD2_SYMEXPORT group_rule: public alias_rule
    {
    public:
      virtual bool
      match (action, target&) const override;

      // Return false if this group member should be ignored and true
      // otherwise. Note that this filter is called during apply().
      //
      // The default implementation accepts all members.
      //
      virtual bool
      filter (action, const target&, const target& group_member) const;

      // Return NULL if this prerequisite should be ignored and pointer to its
      // target otherwise. The same semantics as in file_rule below.
      //
      virtual pair<const target*, uint64_t>
      filter (const scope*,
              action, const target&, const prerequisite&,
              match_extra&) const override;

      using alias_rule::filter; // "Unhide" to make Clang happy.

      virtual recipe
      apply (action, target&, match_extra&) const override;

      group_rule (bool sto): see_through_only (sto) {}
      static const group_rule instance;

      bool see_through_only;
    };

    struct install_dir;

    class LIBBUILD2_SYMEXPORT file_rule: public simple_rule
    {
    public:
      virtual bool
      match (action, target&) const override;

      // Return false if this ad hoc group member should be ignored and true
      // otherwise. Note that this filter is called during execute and only
      // for install/uninstall (and not update). For generality, it is also
      // (first) called  on the target itself (can be detected by comparing
      // the second and third arguments).
      //
      // The default implementation accepts all members.
      //
      virtual bool
      filter (action, const target&, const target& adhoc_group_member) const;

      // Return NULL if this prerequisite should be ignored and pointer to its
      // target otherwise. In the latter case, return the match options that
      // should be used for this prerequisite (use match_extra::all_options
      // and not 0 if no match options are needed).
      //
      // The default implementation ignores prerequsites that are outside of
      // the installation scope (see install_scope() for details). It also
      // ignores exe{} prerequisites assuming an exe{} listed for a file
      // target is there to execute (e.g., to generate that target) and
      // normally should not be installed (an exe{} would typically be
      // installed via the dir{./} alias). But this can be overridden with a
      // prerequisite-specific install=true, for example:
      //
      // exe{foo}: exe{bar}: install = true # foo runs bar
      //
      // The default implementation always returns match_extra::all_options.
      // The match_extra argument is not used by the default implementation.
      //
      // The prerequisite is passed as an iterator allowing the filter to
      // "see" inside groups.
      //
      using prerequisite_iterator =
        prerequisite_members_range<group_prerequisites>::iterator;

      virtual pair<const target*, uint64_t>
      filter (const scope*,
              action, const target&, prerequisite_iterator&,
              match_extra&) const;

      virtual pair<const target*, uint64_t>
      filter (const scope*,
              action, const target&, const prerequisite&,
              match_extra&) const;

      // Note: rule::apply() override (with match_extra).
      //
      virtual recipe
      apply (action, target&, match_extra&) const override;

      // Implementation of apply() that returns empty_recipe (i.e., NULL) if
      // the target is not installable.
      //
      // If the implementation may call reapply_impl(), then the reapply
      // argument to apply_impl() must be true. Note that in this case, the
      // *_impl() functions use the prerequisite_target::data member for own
      // housekeeping.
      //
      recipe
      apply_impl (action, target&, match_extra&, bool reapply = false) const;

      // Implementation of reapply() that re-tries prerequisites that have
      // been filtered out during the reapply() call. Note that currently not
      // supported for update, only for install/uninstall.
      //
      void
      reapply_impl (action, target&, match_extra&) const;

      static target_state
      perform_update (action, const target&);

      // Extra un/installation hooks. Return true if anything was actually
      // un/installed.
      //
      using install_dir = install::install_dir; // For derived rules.

      virtual bool
      install_extra (const file&, const install_dir&) const;

      virtual bool
      uninstall_extra (const file&, const install_dir&) const;

      // Lower-level pre/post installation hooks that can be used to override
      // the source file path being installed (for example, to implement
      // post-processing, etc).
      //
      // Note that one cannot generally perform post-processing in-place
      // because of permissions.
      //
      virtual auto_rmfile
      install_pre (const file&, const install_dir&) const;

      virtual bool
      install_post (const file&, const install_dir&, auto_rmfile&&) const;

      // Installation/uninstallation "commands".
      //
      // The verbosity argument specified the level to start printing the
      // command at. Note that these functions respect the dry_run flag.

      // Install (create) a directory:
      //
      // install -d <dir>
      //
      // Note: <dir> is expected to be absolute.
      //
      // Note that the target argument only specifies which target caused
      // this directory to be created.
      //
      static void
      install_d (const scope& rs,
                 const install_dir& base,
                 const dir_path& dir,
                 const file& target,
                 uint16_t verbosity = 1);

      // Install a file:
      //
      // install <file> <base>/        # if <name> is empty
      // install <file> <base>/<name>  # if <name> is not empty
      //
      // Note that <name> should be a simple path.
      //
      static void
      install_f (const scope& rs,
                 const install_dir& base,
                 const path& name,
                 const file& target,
                 const path& file,
                 uint16_t verbosity = 1);

      // Install (make) a symlink:
      //
      // install -l <link_target> <base>/<link>
      //
      // Which is essentially:
      //
      // ln -s <link_target> <base>/<link>
      //
      // Note that <link> should be a simple path. Note that <link_target>
      // must not be absolute if relocatable installation is requested
      // (config.install.relocatable).
      //
      // Note that the target argument only specifies which target this
      // symlink "belongs" to.
      //
      static void
      install_l (const scope& rs,
                 const install_dir& base,
                 const path& link,
                 const file& target,
                 const path& link_target,
                 uint16_t verbosity = 1);

      // Uninstall (remove) a file or symlink:
      //
      // uninstall <target> <base>/  # rm <base>/<target>.leaf (); name empty
      // uninstall <target> <name>   # rm <base>/<name>; target can be NULL
      //
      // Return false if nothing has been removed (i.e., the file does not
      // exist).
      //
      static bool
      uninstall_f (const scope& rs,
                   const install_dir& base,
                   const file* target,
                   const path& name,
                   uint16_t verbosity = 1);

      // Uninstall (remove) a symlink.
      //
      // This is essentially unistall_f() but with better low-verbosity
      // diagnostics.
      //
      static bool
      uninstall_l (const scope& rs,
                   const install_dir& base,
                   const path& link,
                   const path& link_target,
                   uint16_t verbosity = 1);


      // Uninstall (remove) an empty directory.
      //
      // uninstall -d <dir>
      //
      // We try to remove all the directories between base and dir but not
      // base itself unless base == dir. Return false if nothing has been
      // removed (i.e., the directories do not exist or are not empty).
      //
      static bool
      uninstall_d (const scope& rs,
                   const install_dir& base,
                   const dir_path& dir,
                   uint16_t verbosity = 1);

      target_state
      perform_install (action, const target&) const;

      target_state
      perform_uninstall (action, const target&) const;

      static const file_rule instance;
      file_rule () {}

    private:
      virtual recipe
      apply (action, target&) const override; // Dummy simple_rule override.
    };

    class fsdir_rule: public simple_rule
    {
    public:
      virtual bool
      match (action, target&) const override;

      virtual recipe
      apply (action, target&) const override;

      fsdir_rule () {}
      static const fsdir_rule instance;
    };
  }
}

#endif // LIBBUILD2_INSTALL_RULE_HXX