aboutsummaryrefslogtreecommitdiff
path: root/bdep/project.cxx
blob: 7321cc4121c079b782a9f6a08e52bae119403914 (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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
// file      : bdep/project.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <bdep/project.hxx>
#include <bdep/project-odb.hxx>

#include <libbutl/manifest-parser.hxx>

#include <libbpkg/manifest.hxx>

#include <bdep/database.hxx>
#include <bdep/diagnostics.hxx>

using namespace std;
using namespace butl;

namespace bdep
{
  pair<configurations, bool>
  find_configurations (const project_options& po,
                       const dir_path& prj,
                       transaction& t,
                       bool fallback_default,
                       bool validate,
                       bool allow_none)
  {
    configurations r;
    bool fallback (false);

    // Weed out duplicates.
    //
    auto add = [&r] (shared_ptr<configuration> c)
    {
      if (find_if (r.begin (),
                   r.end (),
                   [&c] (const shared_ptr<configuration>& e)
                   {
                     return *c->id == *e->id;
                   }) == r.end ())
        r.push_back (move (c));
    };

    database& db (t.database ());
    using query = bdep::query<configuration>;

    {
      // @<cfg-name>
      //
      const vector<pair<string, size_t>>& ns (po.config_name ());
      auto ni (ns.begin ());
      auto ne (ns.end ());

      // --config <cfg-dir>
      //
      const vector<pair<dir_path, size_t>>& ds (po.config ());
      auto di (ds.begin ());
      auto de (ds.end ());

      // --config-id <cfg-num>
      //
      const vector<pair<uint64_t, size_t>>& is (po.config_id ());
      auto ii (is.begin ());
      auto ie (is.end ());

      // Return true if the first options range is not empty and position of
      // its leftmost option is less than positions in two other option
      // ranges.
      //
      auto lt = [] (auto oi1, auto oe1, auto oi2, auto oe2, auto oi3, auto oe3)
      {
        return oi1 != oe1 &&
               (oi2 == oe2 || oi2->second > oi1->second) &&
               (oi3 == oe3 || oi3->second > oi1->second);
      };

      while (ni != ne || di != de || ii != ie)
      {
        if (lt (ni, ne, di, de, ii, ie))
        {
          const string& n (ni->first);

          if (auto c = db.query_one<configuration> (query::name == n))
            add (move (c));
          else
            fail << "no configuration name '" << n << "' in project " << prj;

          ++ni;
        }
        else if (lt (di, de, ii, ie, ni, ne))
        {
          dir_path d (di->first);

          normalize (d, "configuration directory");

          if (auto c = db.query_one<configuration> (query::path ==
                                                    d.string ()))
            add (move (c));
          else
            fail << "no configuration directory " << d << " in project "
                 << prj;

          ++di;
        }
        else if (lt (ii, ie, ni, ne, di, de))
        {
          uint64_t id (ii->first);

          if (auto c = db.find<configuration> (id))
            add (move (c));
          else
            fail << "no configuration id " << id << " in project " << prj;

          ++ii;
        }
        else
          assert (false);
      }
    }

    // --all
    //
    // Add configurations unordered.
    //
    if (po.all ())
    {
      for (auto c: pointer_result (db.query<configuration> ()))
        add (move (c));

      if (r.empty ())
      {
        if (allow_none)
          return make_pair (move (r), false /* fallback */);

        fail << "no existing configurations";
      }
    }

    // default
    //
    // Add configurations unordered.
    //
    if (r.empty ())
    {
      if (fallback_default)
      {
        for (shared_ptr<configuration> c:
               pointer_result (db.query<configuration> (query::default_)))
          add (move (c));

        if (r.empty ())
          fail << "no default configuration in project " << prj <<
            info << "use (@<cfg-name> | --config|-c <cfg-dir> | --all|-a) to "
                 << "specify configuration explicitly";

        fallback = true;
      }
      else
        fail << "no configurations specified";
    }

    // Validate all the returned configuration directories are still there.
    //
    if (validate)
    {
      for (const shared_ptr<configuration>& c: r)
      {
        if (!exists (c->path))
          fail << "configuration directory " << c->path << " no longer exists" <<
            info << "use config move command if it has been moved/renamed" <<
            info << "use config remove commands if it has been removed";
      }
    }

    return make_pair (move (r), fallback);
  }

  project_package
  find_project_package (const dir_path& start,
                        bool allow_subdir,
                        bool ignore_nf)
  {
    dir_path prj;
    optional<dir_path> pkg;

    dir_path d (normalize (start, "project directory"));
    for (; !d.empty (); d = d.directory ())
    {
      // Ignore errors when checking for file existence since we may be
      // iterating over directories past any reasonable project boundaries.
      //
      bool p (exists (d / manifest_file, true));
      if (p)
      {
        if (pkg)
        {
          fail << "multiple package manifests between " << start
               << " and project root" <<
            info << "first  manifest is in " << *pkg <<
            info << "second manifest is in " << d;
        }

        pkg = d;

        // Fall through (can also be the project root).
      }

      // Check for the database file first since an (initialized) simple
      // project most likely won't have any *.manifest files.
      //
      if (exists (d / bdep_file, true)     ||
          exists (d / packages_file, true) ||
          //
          // We should only consider {repositories,configurations}.manifest if
          // we have either packages.manifest or manifest (i.e., this is a
          // valid project root).
          //
          (p && (exists (d / repositories_file, true) ||
                 exists (d / configurations_file, true))))
      {
        prj = move (d);
        break;
      }

      // If this directory is not a package nor project root and search from
      // within their subdirs is disallowed, then bail out to fail.
      //
      if (!allow_subdir && !pkg)
        break;
    }

    if (prj.empty ())
    {
      if (!pkg)
      {
        if (ignore_nf)
          return project_package ();

        fail << start << " is not a "
             << (allow_subdir
                 ? "(sub)directory of a package or project"
                 : "package or project directory");
      }

      // Project and package are the same.
      //
      prj = move (*pkg);
      pkg = dir_path ();
    }
    else if (pkg)
      pkg = pkg->leaf (prj);

    return project_package {move (prj), move (pkg)};
  }

  static package_locations
  load_package_locations (const dir_path& prj, bool allow_empty = false)
  {
    package_locations pls;

    // If exists, load packages.manifest from the project root. Otherwise,
    // this must be a simple, single-package project.
    //
    path f (prj / packages_file);

    if (exists (f))
    {
      using bpkg::package_manifest;
      using bpkg::dir_package_manifests;

      auto ms (parse_manifest<dir_package_manifests> (f, "packages"));

      // While an empty repository is legal, in our case it doesn't make much
      // sense and will just further complicate things.
      //
      if (ms.empty () && !allow_empty)
        fail << "no packages listed in " << f;

      for (package_manifest& m: ms)
      {
        // Convert the package location from POSIX to the host form and make
        // sure the current directory is represented as an empty path.
        //
        assert (m.location);
        dir_path d (path_cast<dir_path> (move (*m.location)));
        d.normalize (false /* actualize */, true /* cur_empty */);

        pls.push_back (package_location {package_name (), nullopt, move (d)});
      }
    }
    else if (exists (prj / manifest_file))
    {
      pls.push_back (package_location {package_name (), nullopt, dir_path ()});
    }
    else if (!allow_empty)
      fail << "no packages in project " << prj;

    return pls;
  }

  static void
  load_package_names (const dir_path& prj, package_locations& pls)
  {
    // Load each package's manifest and obtain its name and project (they are
    // normally at the beginning of the manifest so we could optimize this, if
    // necessary).
    //
    for (package_location& pl: pls)
    {
      path f (prj / pl.path / manifest_file);

      if (!exists (f))
        fail << "package manifest file " << f << " does not exist";

      try
      {
        ifdstream is (f);
        manifest_parser p (is, f.string ());

        bpkg::package_manifest m (p,
                                  false /* ignore_unknown */,
                                  false /* complete_depends */);

        pl.name = move (m.name);
        pl.project = move (m.project);
      }
      catch (const manifest_parsing& e)
      {
        fail << "invalid package manifest: " << f << ':'
             << e.line << ':' << e.column << ": " << e.description << endf;
      }
      catch (const io_error& e)
      {
        fail << "unable to read " << f << ": " << e << endf;
      }
    }
  }

  package_locations
  load_packages (const dir_path& prj, bool allow_empty)
  {
    package_locations pls (load_package_locations (prj, allow_empty));
    load_package_names (prj, pls);
    return pls;
  }

  project_packages
  find_project_packages (const dir_paths& dirs,
                         bool ignore_packages,
                         bool load_packages,
                         bool allow_empty)
  {
    project_packages r;

    if (!dirs.empty ())
    {
      for (const dir_path& d: dirs)
      {
        if (!exists (d))
          fail << "project/package directory " << d << " does not exist";

        project_package p (
          find_project_package (d, false /* allow_subdir */));

        // We only work on one project at a time.
        //
        if (r.project.empty ())
        {
          r.project = move (p.project);
        }
        else if (r.project != p.project)
        {
          fail << "multiple project directories specified" <<
            info << r.project <<
            info << p.project;
        }

        if (!ignore_packages && p.package)
        {
          // Suppress duplicate packages.
          //
          if (find_if (r.packages.begin (),
                       r.packages.end (),
                       [&p] (const package_location& pl)
                       {
                         return *p.package == pl.path;
                       }) == r.packages.end ())
          {
            // Name/project is to be extracted later.
            //
            r.packages.push_back (
              package_location {package_name (), nullopt, move (*p.package)});
          }
        }
      }
    }
    else
    {
      project_package p (
        find_project_package (current_directory (), true /* allow_subdir */));

      r.project = move (p.project);

      if (!ignore_packages && p.package)
      {
        // Name/project is to be extracted later.
        //
        r.packages.push_back (
          package_location {package_name (), nullopt, move (*p.package)});
      }
    }

    if (!ignore_packages)
    {
      // Load the package locations and either verify that the discovered
      // packages are in it or, if nothing was discovered, use it as the
      // source for the package list.
      //
      package_locations pls (load_package_locations (r.project, allow_empty));

      if (!r.packages.empty ())
      {
        for (const package_location& x: r.packages)
        {
          if (find_if (pls.begin (),
                       pls.end (),
                       [&x] (const package_location& y)
                       {
                         return x.path == y.path;
                       }) == pls.end ())
          {
            fail << "package directory " << x.path << " is not listed in "
                 << r.project;
          }
        }
      }
      else if (load_packages)
      {
        // Names to be extracted later.
        //
        r.packages = move (pls);
      }

      if (!r.packages.empty ())
        load_package_names (r.project, r.packages);
    }

    return r;
  }

  void
  verify_project_packages (const project_packages& pp,
                           const pair<configurations, bool>& cfgs)
  {
    for (const package_location& p: pp.packages)
    {
      bool init (false);

      for (const shared_ptr<configuration>& c: cfgs.first)
      {
        if (find_if (c->packages.begin (),
                     c->packages.end (),
                     [&p] (const package_state& s)
                     {
                       return p.name == s.name;
                     }) != c->packages.end ())
        {
          init = true;
          break;
        }
      }

      if (!init)
      {
        diag_record dr (fail);

        dr << "package " << p.name << " is not initialized in ";

        if (cfgs.second)
          dr << "any default configuration(s)";
        else if (cfgs.first.size () == 1)
          dr << "configuration " << *cfgs.first.front ();
        else
          dr << "any specified configurations";
      }
    }
  }

  package_info
  package_b_info (const common_options& o, const dir_path& d, b_info_flags fl)
  {
    try
    {
      return b_info (d,
                     fl,
                     verb,
                     [] (const char* const args[], size_t n)
                     {
                       if (verb >= 3)
                         print_process (args, n);
                     },
                     path (name_b (o)),
                     exec_dir,
                     o.build_option ());
    }
    catch (const b_error& e)
    {
      if (e.normal ())
        throw failed (); // Assume the build2 process issued diagnostics.

      fail << "unable to obtain project info for package directory " << d
           << ": " << e << endf;
    }
  }

  standard_version
  package_version (const common_options& o, const dir_path& d)
  {
    package_info pi (package_b_info (o, d, b_info_flags::none));

    if (pi.version.empty ())
      fail << "package in directory " << d << " does not use standard version" <<
        info << "perhaps the package does not load the version module?";

    return move (pi.version);
  }

  standard_version
  package_version (const common_options& o,
                   const dir_path& cfg,
                   const package_name& p)
  {
    // We could have used bpkg-pkg-status but then we would have to deal with
    // iterations. So we use the build system's info meta-operation directly.
    //
    // Note: the package directory inside the configuration is a bit of an
    // assumption.
    //
    package_info pi (package_b_info (o,
                                     (dir_path (cfg) /= p.string ()),
                                     b_info_flags::none));
    verify_package_info (pi, p);

    return move (pi.version);
  }

  void
  verify_package_info (const package_info& pi, const package_name& p)
  {
    if (pi.project != p)
      fail << "name mismatch for package " << p;

    if (pi.version.empty ())
      fail << "package " << p << " does not use standard version" <<
        info << "perhaps the package does not load the version module?";
  }
}