aboutsummaryrefslogtreecommitdiff
path: root/bpkg/pkg-command.cxx
blob: 1d53e039b99218916459fdc2c78f8ae5d7f6facb (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
// file      : bpkg/pkg-command.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2018 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <bpkg/pkg-command.hxx>

#include <algorithm> // find_if()

#include <bpkg/package.hxx>
#include <bpkg/package-odb.hxx>
#include <bpkg/database.hxx>
#include <bpkg/diagnostics.hxx>
#include <bpkg/manifest-utility.hxx>

using namespace std;
using namespace butl;

namespace bpkg
{
  void
  pkg_command (const string& cmd,
               const dir_path& c,
               const common_options& o,
               const string& cmd_v,
               const strings& cvars,
               const vector<pkg_command_vars>& ps)
  {
    tracer trace ("pkg_command");

    l4 ([&]{trace << "command: " << cmd;});

    // This one is a bit tricky: we can only update all the packages at once
    // if they don't have any package-specific variables. But let's try to
    // handle this with the same logic (being clever again).
    //
    // @@ If the build system supported command line variable grouping, then
    //    we could always build at once.
    //
    string bspec;

    auto run = [&trace, &o, &cvars, &bspec] (const strings& vars = strings ())
    {
      if (!bspec.empty ())
      {
        bspec += ')';
        l4 ([&]{trace << "buildspec: " << bspec;});

        run_b (o,
               verb_b::normal,
               (o.jobs_specified ()
                ? strings ({"-j", to_string (o.jobs ())})
                : strings ()),
               cvars,
               vars,
               bspec);

        bspec.clear ();
      }
    };

    for (const pkg_command_vars& pv: ps)
    {
      if (!pv.vars.empty ())
        run (); // Run previously collected packages.

      if (bspec.empty ())
      {
        bspec = cmd;

        if (!cmd_v.empty ())
        {
          bspec += "-for-";
          bspec += cmd_v;
        }

        bspec += '(';
      }

      const shared_ptr<selected_package>& p (pv.pkg);

      assert (p->state == package_state::configured);
      assert (p->out_root); // Should be present since configured.

      dir_path out_root (p->effective_out_root (c));
      l4 ([&]{trace << p->name << " out_root: " << out_root;});

      if (bspec.back () != '(')
        bspec += ' ';

      // Use path representation to get canonical trailing slash.
      //
      bspec += '\'';
      bspec += out_root.representation ();
      bspec += '\'';

      if (!pv.vars.empty ())
        run (pv.vars); // Run this package.
    }

    run ();
  }

  static void
  collect_dependencies (const shared_ptr<selected_package>& p,
                        bool recursive,
                        vector<pkg_command_vars>& ps)
  {
    for (const auto& pr: p->prerequisites)
    {
      shared_ptr<selected_package> d (pr.first.load ());

      // The selected package can only be configured if all its dependencies
      // are configured.
      //
      assert (d->state == package_state::configured);

      // Skip configured as system and duplicate dependencies.
      //
      if (d->substate != package_substate::system &&
          find_if (ps.begin (), ps.end (),
                   [&d] (const pkg_command_vars& i) {return i.pkg == d;}) ==
          ps.end ())
      {
        // Note: no package-specific variables (global ones still apply).
        //
        ps.push_back (pkg_command_vars {d, strings () /* vars */});

        if (recursive)
          collect_dependencies (d, recursive, ps);
      }
    }
  }

  int
  pkg_command (const string& cmd,
               const configuration_options& o,
               const string& cmd_v,
               bool recursive,
               bool immediate,
               bool all,
               cli::group_scanner& args)
  {
    tracer trace ("pkg_command");

    const dir_path& c (o.directory ());
    l4 ([&]{trace << "configuration: " << c;});

    // First sort arguments into the package names and variables.
    //
    strings cvars;
    bool sep (false); // Seen '--'.

    struct pkg_arg
    {
      package_name name;
      strings      vars;
    };
    vector<pkg_arg> pkg_args;

    while (args.more ())
    {
      string a (args.next ());

      // If we see the "--" separator, then we are done parsing common
      // variables.
      //
      if (!sep && a == "--")
      {
        sep = true;
        continue;
      }

      if (!sep && a.find ('=') != string::npos)
      {
        // Make sure this is not a (misspelled) package name with an option
        // group.
        //
        if (args.group ().more ())
          fail << "unexpected options group for variable '" << a << "'";

        cvars.push_back (move (a));
      }
      else
      {
        package_name n (parse_package_name (a, false /* allow_version */));

        // Read package-specific variables.
        //
        strings vars;
        for (cli::scanner& ag (args.group ()); ag.more (); )
        {
          string a (ag.next ());
          if (a.find ('=') == string::npos)
            fail << "unexpected group argument '" << a << "'";

          vars.push_back (move (a));
        }

        pkg_args.push_back (pkg_arg {move (n), move (vars)});
      }
    }

    // Check that options and arguments are consistent.
    //
    // Note that we can as well count on the option names that correspond to
    // the immediate, recursive, and all parameters.
    //
    {
      diag_record dr;

      if (immediate && recursive)
        dr << fail << "both --immediate|-i and --recursive|-r specified";
      else if (all)
      {
        if (!pkg_args.empty ())
          dr << fail << "both --all|-a and package argument specified";
      }
      else if (pkg_args.empty ())
        dr << fail << "package name argument expected";

      if (!dr.empty ())
        dr << info << "run 'bpkg help pkg-" << cmd << "' for more information";
    }

    vector<pkg_command_vars> ps;
    {
      database db (open (c, trace));
      transaction t (db);

      // We need to suppress duplicate dependencies for the recursive command
      // execution.
      //
      session ses;

      auto add = [&ps, recursive, immediate] (
        const shared_ptr<selected_package>& p,
        strings vars)
      {
        ps.push_back (pkg_command_vars {p, move (vars)});

        // Note that it can only be recursive or immediate but not both.
        //
        if (recursive || immediate)
          collect_dependencies (p, recursive, ps);
      };

      if (all)
      {
        using query = query<selected_package>;

        query q (query::hold_package &&
                 query::state == "configured" &&
                 query::substate != "system");

        for (shared_ptr<selected_package> p:
               pointer_result (db.query<selected_package> (q)))
        {
          l4 ([&]{trace << *p;});

          add (p, strings ());
        }

        if (ps.empty ())
          info << "nothing to " << cmd;
      }
      else
      {
        for (auto& a: pkg_args)
        {
          shared_ptr<selected_package> p (db.find<selected_package> (a.name));

          if (p == nullptr)
            fail << "package " << a.name << " does not exist in "
                 << "configuration " << c;

          if (p->state != package_state::configured)
            fail << "package " << a.name << " is " << p->state <<
              info << "expected it to be configured";

          if (p->substate == package_substate::system)
            fail << "cannot " << cmd << " system package " << a.name;

          l4 ([&]{trace << *p;});

          add (p, move (a.vars));
        }
      }

      t.commit ();
    }

    pkg_command (cmd, c, o, cmd_v, cvars, ps);

    if (verb && !o.no_result ())
    {
      for (const pkg_command_vars& pv: ps)
        text << cmd << (cmd.back () != 'e' ? "ed " : "d ") << *pv.pkg;
    }

    return 0;
  }
}