aboutsummaryrefslogtreecommitdiff
path: root/build/file.cxx
blob: 5f899de5fea1f545916f703e390bab6c032be04c (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
// file      : build/file.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <build/file>

#include <fstream>

#include <butl/filesystem>

#include <build/scope>
#include <build/context>
#include <build/parser>
#include <build/diagnostics>

using namespace std;
using namespace butl;

namespace build
{
  bool
  is_src_root (const dir_path& d)
  {
    return file_exists (d / path ("build/bootstrap.build")) ||
      file_exists (d / path ("build/root.build"));
  }

  bool
  is_out_root (const dir_path& d)
  {
    return file_exists (d / path ("build/bootstrap/src-root.build"));
  }

  void
  source (const path& bf, scope& root, scope& base)
  {
    tracer trace ("source");

    ifstream ifs (bf.string ());
    if (!ifs.is_open ())
      fail << "unable to open " << bf;

    level4 ([&]{trace << "sourcing " << bf;});

    ifs.exceptions (ifstream::failbit | ifstream::badbit);
    parser p;

    try
    {
      p.parse_buildfile (ifs, bf, root, base);
    }
    catch (const std::ios_base::failure&)
    {
      fail << "failed to read from " << bf;
    }
  }

  void
  source_once (const path& bf, scope& root, scope& base, scope& once)
  {
    tracer trace ("source_once");

    if (!once.buildfiles.insert (bf).second)
    {
      level4 ([&]{trace << "skipping already sourced " << bf;});
      return;
    }

    source (bf, root, base);
  }

  scope&
  create_root (const dir_path& out_root, const dir_path& src_root)
  {
    scope& rs (scopes.insert (out_root, true).first);

    // Enter built-in meta-operation and operation names. Note that
    // the order of registration should match the id constants; see
    // <operation> for details. Loading of modules (via the src
    // bootstrap; see below) can result in additional names being
    // added.
    //
    if (rs.meta_operations.empty ())
    {
      assert (rs.meta_operations.insert (perform) == perform_id);

      assert (rs.operations.insert (default_) == default_id);
      assert (rs.operations.insert (update) == update_id);
      assert (rs.operations.insert (clean) == clean_id);
    }

    // If this is already a root scope, verify that things are
    // consistent.
    //
    {
      auto v (rs.assign ("out_root"));

      if (!v)
        v = out_root;
      else
      {
        const dir_path& p (v.as<const dir_path&> ());

        if (p != out_root)
          fail << "new out_root " << out_root << " does not match "
               << "existing " << p;
      }
    }

    if (!src_root.empty ())
    {
      auto v (rs.assign ("src_root"));

      if (!v)
        v = src_root;
      else
      {
        const dir_path& p (v.as<const dir_path&> ());

        if (p != src_root)
          fail << "new src_root " << src_root << " does not match "
               << "existing " << p;
      }
    }

    return rs;
  }

  void
  bootstrap_out (scope& root)
  {
    path bf (root.path () / path ("build/bootstrap/src-root.build"));

    if (!file_exists (bf))
      return;

    //@@ TODO: if bootstrap files can source other bootstrap files
    //   (the way to express dependecies), then we need a way to
    //   prevent multiple sourcing. We handle it here but we still
    //   need something like source_once (once [scope] source).
    //
    source_once (bf, root, root);
  }

  bool
  bootstrap_src (scope& root)
  {
    tracer trace ("bootstrap_src");

    path bf (root.src_path () / path ("build/bootstrap.build"));

    if (!file_exists (bf))
      return false;

    // We assume that bootstrap out cannot load this file explicitly. It
    // feels wrong to allow this since that makes the whole bootstrap
    // process hard to reason about. But we may try to bootstrap the
    // same root scope multiple time.
    //
    source_once (bf, root, root);
    return true;
  }

  void
  create_bootstrap_outer (scope& root)
  {
    auto v (root.vars["amalgamation"]);

    if (!v)
      return;

    const dir_path& d (v.as<const dir_path&> ());
    dir_path out_root (root.path () / d);
    dir_path src_root (root.src_path () / d);
    out_root.normalize ();
    src_root.normalize ();

    scope& rs (create_root (out_root, src_root));

    bootstrap_out (rs);

    // Check if the bootstrap process changed src_root.
    //
    const dir_path& p (rs.vars["src_root"].as<const dir_path&> ());

    if (src_root != p)
      fail << "bootstrapped src_root " << p << " does not match "
           << "amalgamated " << src_root;

    rs.src_path_ = &p;

    bootstrap_src (rs);
    create_bootstrap_outer (rs);
  }

  scope&
  create_bootstrap_inner (scope& root, const dir_path& out_base)
  {
    if (auto v = root.vars["subprojects"])
    {
      for (const name& n: v.as<const list_value&> ())
      {
        // Should be a list of directories.
        //
        if (!n.type.empty () || !n.value.empty () || n.dir.empty ())
          fail << "expected directory in subprojects variable "
               << "instead of " << n;

        dir_path out_root (root.path () / n.dir);

        if (!out_base.sub (out_root))
          continue;

        dir_path src_root (root.src_path () / n.dir);
        scope& rs (create_root (out_root, src_root));

        bootstrap_out (rs);

        // Check if the bootstrap process changed src_root.
        //
        const dir_path& p (rs.vars["src_root"].as<const dir_path&> ());

        if (src_root != p)
          fail << "bootstrapped src_root " << p << " does not match "
               << "subproject " << src_root;

        rs.src_path_ = &p;

        bootstrap_src (rs);

        // See if there are more inner roots.
        //
        return create_bootstrap_inner (rs, out_base);
      }
    }

    return root;
  }

  void
  load_root_pre (scope& root)
  {
    tracer trace ("root_pre");

    // First load outer roots, if any.
    //
    if (scope* rs = root.parent_scope ()->root_scope ())
      load_root_pre (*rs);

    path bf (root.src_path () / path ("build/root.build"));

    if (file_exists (bf))
      source_once (bf, root, root);
  }

  list_value
  import (scope& ibase, const name& n, const location& l)
  {
    tracer trace ("import");

    // Split the name into the project and target.
    //
    string project;
    name target;

    if (n.dir.empty ())
    {
      if (!n.simple ())
        fail << "project name expected before imported target " << n;

      // Note that value can be foo/bar/baz; in this case probably
      // means sub-projects? Or only to a certain point, then
      // (untyped) target? Looks like I will need to scan anything
      // that looks like a directory checking if this is a subproject.
      // If not, then that's part of the target.
      //
      project = n.value;
    }
    else
    {
      //@@ This can be a path inside a sub-project. So, eventually,
      //   we should find the innermost sub-project and load the
      //   export stub from there (will probably still have to
      //   resolve root from the top-level project). For now we
      //   assume the project is always top-level.
      //
      project = *n.dir.begin ();

      target.dir = n.dir.leaf (dir_path (project));
      target.type = n.type;
      target.value = n.value;
    }

    scope& iroot (*ibase.root_scope ());

    // Figure out this project's out_root.
    //
    dir_path out_root;
    string var ("config.import." + project);

    if (auto v = iroot[var])
    {
      if (!v.belongs (*global_scope)) // A value from (some) config.build.
        out_root = v.as<const dir_path&> ();
      else
      {
        // Process the path by making it absolute and normalized. Also,
        // for usability's sake, treat a simple name that doesn't end
        // with '/' as a directory.
        //
        list_value& lv (v.as<list_value&> ());

        if (lv.size () != 1 || lv[0].empty () || !lv[0].type.empty ())
          fail (l) << "invalid " << var << " value " << lv;

        name& n (lv[0]);

        if (n.directory ())
          out_root = n.dir;
        else
          out_root = dir_path (n.value);

        if (out_root.relative ())
          out_root = work / out_root;

        out_root.normalize ();
        iroot.assign (var) = out_root;

        // Also update the command-line value. This is necessary to avoid
        // a warning issued by the config module about global/root scope
        // value mismatch.
        //
        if (n.dir != out_root)
          n = name (out_root);
      }
    }
    else
      fail (l) << "unable to find out_root for imported " << project <<
        info << "consider explicitly configuring its out_root via the "
               << var << " command line variable";

    // Bootstrap the imported root scope. This is pretty similar to
    // what we do in main() except that here we don't try to guess
    // src_root.
    //
    dir_path src_root (is_src_root (out_root) ? out_root : dir_path ());
    scope& root (create_root (out_root, src_root));

    bootstrap_out (root);

    // Check that the bootstrap process set src_root.
    //
    if (auto v = root.vars["src_root"])
    {
      const dir_path& p (v.as<const dir_path&> ());

      if (!src_root.empty () && p != src_root)
        fail << "bootstrapped src_root " << p << " does not match "
             << "discovered " << src_root;

      root.src_path_ = &p;
    }
    else
      fail (l) << "unable to determine src_root for imported " << project <<
        info << "consider configuring " << out_root;

    bootstrap_src (root);

    // Bootstrap outer roots if any. Loading will be done by
    // load_root_pre() below.
    //
    create_bootstrap_outer (root);

    // Load the imported root scope.
    //
    load_root_pre (root);

    // Create a temporary scope so that the export stub does not mess
    // up any of our variables.
    //
    temp_scope ts (ibase);

    // "Pass" the imported project's roots to the stub.
    //
    ts.assign ("out_root") = move (out_root);
    ts.assign ("src_root") = move (src_root);

    // Also pass the target being imported.
    //
    {
      auto v (ts.assign ("target"));

      if (!target.empty ()) // Otherwise leave NULL.
        v = list_value {move (target)};
    }

    // Load the export stub. Note that it is loaded in the context
    // of the importing project, not the imported one. The export
    // stub will normally switch to the imported root scope at some
    // point.
    //
    path es (root.src_path () / path ("build/export.build"));
    ifstream ifs (es.string ());
    if (!ifs.is_open ())
      fail << "unable to open " << es;

    level4 ([&]{trace << "importing " << es;});

    ifs.exceptions (ifstream::failbit | ifstream::badbit);
    parser p;

    try
    {
      p.parse_buildfile (ifs, es, iroot, ts);
    }
    catch (const std::ios_base::failure&)
    {
      fail << "failed to read from " << es;
    }

    return p.export_value ();
  }
}