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

#include <libbuild2/install/init.hxx>

#include <libbuild2/scope.hxx>
#include <libbuild2/target.hxx>
#include <libbuild2/rule.hxx>
#include <libbuild2/function.hxx>
#include <libbuild2/operation.hxx>
#include <libbuild2/diagnostics.hxx>

#include <libbuild2/config/utility.hxx>

#include <libbuild2/install/rule.hxx>
#include <libbuild2/install/utility.hxx>
#include <libbuild2/install/operation.hxx>

using namespace std;
using namespace butl;

namespace build2
{
  namespace install
  {
    // Set install.<name>.* values based on config.install.<name>.* ones
    // or the defaults. If none of config.install.* values were specified,
    // then we do omitted/delayed configuration. Note that we still need
    // to set all the install.* values to defaults, as if we had the
    // default configuration.
    //
    // If override is true, then override values that came from outer
    // configurations. We have to do this for paths that contain the
    // package name.
    //
    // For global values we only set config.install.* variables. Non-global
    // values with NULL defaults are omitted.
    //
    template <typename T, typename CT>
    static void
    set_var (bool spec,
             scope& rs,
             const char* name,
             const char* var,
             const CT* dv,
             bool override = false)
    {
      string vn;
      lookup l;

      bool global (*name == '\0');

      if (spec)
      {
        vn = "config.install";
        if (!global)
        {
          vn += '.';
          vn += name;
        }
        vn += var;
        const variable& vr (rs.var_pool ().insert<CT> (move (vn)));

        using config::lookup_config;

        l = dv != nullptr
          ? lookup_config (rs, vr, *dv, 0 /* save_flags */, override)
          : (global
             ? lookup_config (rs, vr, nullptr)
             : lookup_config (rs, vr));
      }

      if (global)
        return;

      vn = "install.";
      vn += name;
      vn += var;
      const variable& vr (rs.var_pool ().insert<T> (move (vn)));

      value& v (rs.assign (vr));

      if (spec)
      {
        if (l)
          v = cast<T> (l); // Strip CT to T.
      }
      else
      {
        if (dv != nullptr)
          v = *dv;
      }
    }

    template <typename T>
    static void
    set_dir (bool s,                                  // specified
             scope& rs,                               // root scope
             const char* n,                           // var name
             const T& p,                              // path
             bool o = false,                          // override
             const string& fm = string (),            // file mode
             const string& dm = string (),            // dir mode
             const build2::path& c = build2::path ()) // command
    {
      using build2::path;

      bool global (*n == '\0');

      if (!global)
        set_var<dir_path> (s, rs, n, "",        p.empty ()  ? nullptr : &p, o);

      set_var<path>     (s, rs, n, ".cmd",      c.empty ()  ? nullptr : &c);
      set_var<strings>  (s, rs, n, ".options",  (strings*) (nullptr));
      set_var<string>   (s, rs, n, ".mode",     fm.empty () ? nullptr : &fm);
      set_var<string>   (s, rs, n, ".dir_mode", dm.empty () ? nullptr : &dm);
      set_var<string>   (s, rs, n, ".sudo",     (string*) (nullptr));

      // This one doesn't have config.* value (only set in a buildfile).
      //
      if (!global)
        rs.var_pool ().insert<bool> (string ("install.") + n + ".subdirs");
    }

    void
    functions (function_map&); // functions.cxx

    bool
    boot (scope& rs, const location&, module_boot_extra&)
    {
      tracer trace ("install::boot");
      l5 ([&]{trace << "for " << rs;});

      context& ctx (rs.ctx);

      // Register the install function family if this is the first instance of
      // the install modules.
      //
      if (!function_family::defined (ctx.functions, "install"))
        functions (ctx.functions);

      // Register our operations.
      //
      rs.insert_operation (install_id,            op_install);
      rs.insert_operation (uninstall_id,          op_uninstall);
      rs.insert_operation (update_for_install_id, op_update_for_install);

      return false;
    }

    static const path cmd ("install");

    static const dir_path dir_root ("root");

    static const dir_path dir_sbin      (dir_path ("exec_root") /= "sbin");
    static const dir_path dir_bin       (dir_path ("exec_root") /= "bin");
    static const dir_path dir_lib       (dir_path ("exec_root") /= "lib");
    static const dir_path dir_libexec   (dir_path ("exec_root") /= "libexec");
    static const dir_path dir_pkgconfig (dir_path ("lib") /= "pkgconfig");

    static const dir_path dir_data    (dir_path ("data_root") /= "share");
    static const dir_path dir_include (dir_path ("data_root") /= "include");

    static const dir_path dir_doc  (dir_path (dir_data) /= "doc");
    static const dir_path dir_man  (dir_path (dir_data) /= "man");
    static const dir_path dir_man1 (dir_path ("man") /= "man1");

    static const group_rule group_rule_ (true /* see_through_only */);

    bool
    init (scope& rs,
          scope& bs,
          const location& l,
          bool first,
          bool,
          module_init_extra&)
    {
      tracer trace ("install::init");

      if (!first)
      {
        warn (l) << "multiple install module initializations";
        return true;
      }

      l5 ([&]{trace << "for " << rs;});

      // Enter module variables.
      //
      auto& vp (rs.var_pool ());

      // Note that the set_dir() calls below enter some more.
      //
      {
        // The install variable is a path, not dir_path, since it can be used
        // to both specify the target directory (to install with the same file
        // name) or target file (to install with a different name). And the
        // way we distinguish between the two is via the presence/absence of
        // the trailing directory separator.
        //
        vp.insert<path>   ("install",     variable_visibility::target);
        vp.insert<bool>   ("for_install", variable_visibility::prereq);
        vp.insert<string> ("install.mode");
        vp.insert<bool>   ("install.subdirs");
      }

      // Register our rules.
      //
      {
        const auto& ar (alias_rule::instance);
        const auto& dr (fsdir_rule::instance);
        const auto& fr (file_rule::instance);
        const auto& gr (group_rule_);

        bs.insert_rule<alias> (perform_install_id,   "install.alias", ar);
        bs.insert_rule<alias> (perform_uninstall_id, "uninstall.alias", ar);

        bs.insert_rule<fsdir> (perform_install_id,   "install.fsdir", dr);
        bs.insert_rule<fsdir> (perform_uninstall_id, "install.fsdir", dr);

        bs.insert_rule<file> (perform_install_id,   "install.file", fr);
        bs.insert_rule<file> (perform_uninstall_id, "uninstall.file", fr);

        bs.insert_rule<target> (perform_install_id,   "install.file", gr);
        bs.insert_rule<target> (perform_uninstall_id, "uninstall.file", gr);
     }

      // Configuration.
      //
      // Note that we don't use any defaults for root -- the location
      // must be explicitly specified or the installer will complain
      // if and when we try to install.
      //
      {
        using build2::path;
        using config::lookup_config;
        using config::specified_config;

        bool s (specified_config (rs, "install"));

        // Adjust module priority so that the (numerous) config.install.*
        // values are saved at the end of config.build.
        //
        if (s)
          config::save_module (rs, "install", INT32_MAX);

        const string& n (project (rs).string ());

        // Global config.install.* values.
        //
        set_dir (s, rs, "",          abs_dir_path (), false, "644", "755", cmd);

        set_dir (s, rs, "root",      abs_dir_path ());

        set_dir (s, rs, "data_root", dir_root);
        set_dir (s, rs, "exec_root", dir_root, false, "755");

        set_dir (s, rs, "sbin",      dir_sbin);
        set_dir (s, rs, "bin",       dir_bin);
        set_dir (s, rs, "lib",       dir_lib);
        set_dir (s, rs, "libexec",   dir_path (dir_libexec) /= n, true);
        set_dir (s, rs, "pkgconfig", dir_pkgconfig, false, "644");

        set_dir (s, rs, "data",      dir_path (dir_data) /= n, true);
        set_dir (s, rs, "include",   dir_include);

        set_dir (s, rs, "doc",       dir_path (dir_doc) /= n, true);
        set_dir (s, rs, "man",       dir_man);
        set_dir (s, rs, "man1",      dir_man1);

        // Support for chroot'ed install (aka DESTDIR).
        //
        {
          auto& var  (vp.insert<dir_path>     (       "install.chroot"));
          auto& cvar (vp.insert<abs_dir_path> ("config.install.chroot"));

          value& v (rs.assign (var));

          if (s)
          {
            if (lookup l = lookup_config (rs, cvar, nullptr))
              v = cast<dir_path> (l); // Strip abs_dir_path.
          }
        }
      }

      // Configure "installability" for built-in target types.
      //
      install_path<exe>  (bs, dir_path ("bin"));  // Install into install.bin.
      install_path<doc>  (bs, dir_path ("doc"));  // Install into install.doc.
      install_path<man>  (bs, dir_path ("man"));  // Install into install.man.
      install_path<man1> (bs, dir_path ("man1")); // Install into install.man1.

      return true;
    }

    static const module_functions mod_functions[] =
    {
      {"install", &boot,   &init},
      {nullptr,   nullptr, nullptr}
    };

    const module_functions*
    build2_install_load ()
    {
      return mod_functions;
    }
  }
}