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

#include <bpkg/pkg-configure>

#include <bpkg/package>
#include <bpkg/package-odb>
#include <bpkg/database>
#include <bpkg/diagnostics>
#include <bpkg/satisfaction>

#include <bpkg/pkg-verify>
#include <bpkg/pkg-disfigure>

using namespace std;
using namespace butl;

namespace bpkg
{
  void
  pkg_configure (const dir_path& c,
                 const common_options& o,
                 transaction& t,
                 const shared_ptr<selected_package>& p,
                 const strings& vars)
  {
    tracer trace ("pkg_configure");

    assert (p->state == package_state::unpacked);
    assert (p->src_root); // Must be set since unpacked.

    database& db (t.database ());
    tracer_guard tg (db, trace);

    // Calculate package's src_root and out_root.
    //
    dir_path src_root (p->src_root->absolute ()
                       ? *p->src_root
                       : c / *p->src_root);
    dir_path out_root (c / dir_path (p->name + "-" + p->version.string ()));

    l4 ([&]{trace << "src_root: " << src_root << ", "
                  << "out_root: " << out_root;});

    // Verify all our prerequisites are configured and populate the
    // prerequisites list.
    //
    {
      assert (p->prerequisites.empty ());

      package_manifest m (pkg_verify (src_root, true));

      for (const dependency_alternatives& da: m.dependencies)
      {
        assert (!da.conditional); //@@ TODO

        bool satisfied (false);
        for (const dependency& d: da)
        {
          const string& n (d.name);

          if (shared_ptr<selected_package> dp = db.find<selected_package> (n))
          {
            if (dp->state != package_state::configured)
              continue;

            if (!satisfies (dp->version, d.constraint))
              continue;

            auto r (p->prerequisites.emplace (dp, d.constraint));

            // Currently we can only capture a single constraint, so if we
            // already have a dependency on this package and one constraint is
            // not a subset of the other, complain.
            //
            if (!r.second)
            {
              auto& c (r.first->second);

              bool s1 (satisfies (c, d.constraint));
              bool s2 (satisfies (d.constraint, c));

              if (!s1 && !s2)
                fail << "multiple dependencies on package " << n <<
                  info << n << " " << *c <<
                  info << n << " " << *d.constraint;

              if (s2 && !s1)
                c = d.constraint;
            }

            satisfied = true;
            break;
          }
        }

        if (!satisfied)
          fail << "no configured package satisfies dependency on " << da;
      }
    }

    // Form the buildspec.
    //
    string bspec;

    if (src_root == out_root)
      bspec = "configure('" + out_root.string () + "/')";
    else
      bspec = "configure('" +
        src_root.string () + "/'@'" +
        out_root.string () + "/')";

    l4 ([&]{trace << "buildspec: " << bspec;});

    // Configure.
    //
    try
    {
      run_b (o, c, bspec, true, vars); // Run quiet.
    }
    catch (const failed&)
    {
      // If we failed to configure the package, make sure we revert
      // it back to the unpacked state by running disfigure (it is
      // valid to run disfigure on an un-configured build). And if
      // disfigure fails as well, then the package will be set into
      // the broken state.
      //

      // Indicate to pkg_disfigure() we are partially configured.
      //
      p->out_root = out_root.leaf ();
      p->state = package_state::broken;

      pkg_disfigure (c, o, t, p); // Commits the transaction.
      throw;
    }

    p->out_root = out_root.leaf ();
    p->state = package_state::configured;

    db.update (p);
    t.commit ();
  }

  int
  pkg_configure (const pkg_configure_options& o, cli::scanner& args)
  {
    tracer trace ("pkg_configure");

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

    // Sort arguments into the package name and configuration variables.
    //
    string n;
    strings vars;

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

      if (a.find ('=') != string::npos)
        vars.push_back (move (a));
      else if (n.empty ())
        n = move (a);
      else
        fail << "unexpected argument '" << a << "'";
    }

    if (n.empty ())
      fail << "package name argument expected" <<
        info << "run 'bpkg help pkg-configure' for more information";

    database db (open (c, trace));
    transaction t (db.begin ());
    session s;

    shared_ptr<selected_package> p (db.find<selected_package> (n));

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

    if (p->state != package_state::unpacked)
      fail << "package " << n << " is " << p->state <<
        info << "expected it to be unpacked";

    l4 ([&]{trace << p->name << " " << p->version;});

    pkg_configure (c, o, t, p, vars); // Commits the transaction.

    if (verb)
      text << "configured " << p->name << " " << p->version;

    return 0;
  }
}