aboutsummaryrefslogtreecommitdiff
path: root/libbutl/b.cxx
blob: c55496cf16580fe3e49e7ef51cc7b2b83ba72941 (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
// file      : libbutl/b.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#ifndef __cpp_modules_ts
#include <libbutl/b.mxx>
#endif

// C includes.

#include <cassert>

#ifndef __cpp_lib_modules_ts
#include <string>
#include <vector>
#include <cstddef>
#include <cstdint>
#include <stdexcept>
#include <functional>

#include <ios>     // ios::failure
#include <utility> // move()
#include <sstream>
#include <algorithm>
#endif

// Other includes.

#ifdef __cpp_modules_ts
module butl.b;

// Only imports additional to interface.
#ifdef __clang__
#ifdef __cpp_lib_modules_ts
import std.core;
import std.io;
#endif
import butl.url;
import butl.path;
import butl.process;
import butl.optional;
import butl.project_name;
import butl.standard_version;
#endif

import butl.utility;      // next_word(), eof(), etc
import butl.path_io;
import butl.fdstream;
import butl.process_io;   // operator<<(ostream, process_path)
import butl.small_vector;
#else
#include <libbutl/utility.mxx>
#include <libbutl/path-io.mxx>
#include <libbutl/fdstream.mxx>
#include <libbutl/process-io.mxx>
#include <libbutl/small-vector.mxx>
#endif

using namespace std;

namespace butl
{
  b_error::
  b_error (const string& d, optional<process_exit> e)
      : runtime_error (d),
        exit (move (e))
  {
  }

  [[noreturn]] static inline void
  bad_value (const string& d)
  {
    throw runtime_error ("invalid " + d);
  }

  b_project_info
  b_info (const dir_path& project,
          bool ext_mods,
          uint16_t verb,
          const function<b_callback>& cmd_callback,
          const path& program,
          const dir_path& search_fallback,
          const vector<string>& ops)
  {
    try
    {
      process_path pp (
        process::path_search (program, true /* init */, search_fallback));

      process pr;
      try
      {
        fdpipe pipe (fdopen_pipe ()); // Text mode seems appropriate.

        small_vector<string, 2> vops;

        // Let's suppress warnings the `b info` command may issue, unless the
        // verbosity level is 2 (-v) or higher. For example:
        //
        // warning: configured src_root prj/ does not match forwarded prj/prj/
        //
        if (verb >= 2)
        {
          vops.push_back ("--verbose");
          vops.push_back (std::to_string (verb));
        }
        else
          vops.push_back ("-q");

        pr = process_start_callback (
          cmd_callback ? cmd_callback : [] (const char* const*, size_t) {},
          0 /* stdin */,
          pipe,
          2 /* stderr */,
          pp,
          vops,
          ext_mods ? nullptr : "--no-external-modules",
          "-s",
          ops,
          "info:", "'" + project.representation () + "'");

        pipe.out.close ();
        ifdstream is (move (pipe.in), fdstream_mode::skip, ifdstream::badbit);

        auto parse_name = [] (string&& s, const char* what)
        {
          try
          {
            return project_name (move (s));
          }
          catch (const invalid_argument& e)
          {
            bad_value (string (what) + " name '" + s + "': " + e.what ());
          }
        };

        auto parse_dir = [] (string&& s, const char* what)
        {
          try
          {
            return dir_path (move (s));
          }
          catch (const invalid_path& e)
          {
            bad_value (
              string (what) + " directory '" + e.path + "': " + e.what ());
          }
        };

        b_project_info r;
        for (string l; !eof (getline (is, l)); )
        {
          if (l.compare (0, 9, "project: ") == 0)
          {
            string v (l, 9);
            if (!v.empty ())
              r.project = parse_name (move (v), "project");
          }
          else if (l.compare (0, 9, "version: ") == 0)
          {
            r.version_string = string (l, 9);
          }
          else if (l.compare (0, 9, "summary: ") == 0)
          {
            r.summary = string (l, 9);
          }
          else if (l.compare (0, 5, "url: ") == 0)
          {
            string v (l, 5);
            if (!v.empty ())
            try
            {
              r.url = url (v);
            }
            catch (const invalid_argument& e)
            {
              bad_value ("url '" + v + "': " + e.what ());
            }
          }
          else if (l.compare (0, 10, "src_root: ") == 0)
          {
            r.src_root = parse_dir (string (l, 10), "src_root");
          }
          else if (l.compare (0, 10, "out_root: ") == 0)
          {
            r.out_root = parse_dir (string (l, 10), "out_root");
          }
          else if (l.compare (0, 14, "amalgamation: ") == 0)
          {
            string v (l, 14);
            if (!v.empty ())
              r.amalgamation = parse_dir (move (v), "amalgamation");
          }
          else if (l.compare (0, 13, "subprojects: ") == 0)
          {
            string v (l, 13);

            for (size_t b (0), e (0); next_word (v, b, e); )
            {
              string s (v, b, e - b);
              size_t p (s.find ('@'));

              if (p == string::npos)
                bad_value ("subproject '" + s + "': missing '@'");

              project_name sn;
              if (p != 0)
                sn = parse_name (string (s, 0, p), "subproject");

              r.subprojects.push_back (
                b_project_info::subproject {move (sn),
                                            parse_dir (string (s, p + 1),
                                                       "subproject")});
            }
          }
          else if (l.compare (0, 12, "operations: ") == 0)
          {
            string v (l, 12);
            for (size_t b (0), e (0); next_word (v, b, e); )
              r.operations.push_back (string (v, b, e - b));
          }
          else if (l.compare (0, 17, "meta-operations: ") == 0)
          {
            string v (l, 17);
            for (size_t b (0), e (0); next_word (v, b, e); )
              r.meta_operations.push_back (string (v, b, e - b));
          }
          else if (l.compare (0, 9, "modules: ") == 0)
          {
            string v (l, 9);
            for (size_t b (0), e (0); next_word (v, b, e); )
              r.modules.push_back (string (v, b, e - b));
          }
        }

        is.close (); // Detect errors.

        if (pr.wait ())
        {
          // Parse version string to standard version if the project loaded
          // the version module.
          //
          const auto& ms (r.modules);
          if (find (ms.begin (), ms.end (), "version") != ms.end ())
          {
            try
            {
              r.version = standard_version (r.version_string,
                                            standard_version::allow_stub);
            }
            catch (const invalid_argument& e)
            {
              bad_value ("version '" + r.version_string + "': " + e.what ());
            }
          }

          return r;
        }
      }
      // Note that ios::failure inherits from std::runtime_error, so this
      // catch-clause must go last.
      //
      catch (const ios::failure& e)
      {
        // Note: using ostream to get sanitized representation.
        //
        if (pr.wait ())
        {
          ostringstream os;
          os << "error reading " << pp << " output: " << e;
          throw b_error (os.str (), move (pr.exit));
        }
        else if (!pr.exit) // Pipe opening failed before the process started.
        {
          ostringstream os;
          os << "unable to open pipe: " << e;
          throw b_error (os.str ());
        }

        // Fall through.
      }
      catch (const runtime_error& e)
      {
        // Re-throw as b_error if the process exited normally with zero code.
        //
        if (pr.wait ())
          throw b_error (e.what (), move (pr.exit));

        // Fall through.
      }

      // We should only get here if the child exited with an error status.
      //
      assert (!pr.wait ());

      throw b_error (
        string ("process ") + pp.recall_string () + " " + to_string (*pr.exit),
        move (pr.exit));
    }
    catch (const process_error& e)
    {
      ostringstream os;
      os << "unable to execute " << program << ": " << e;
      throw b_error (os.str ());
    }
  }
}