aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/config/utility.cxx
blob: 34220f8ab12a3c9ba0725d5f1232f3ecb7fc4978 (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
// file      : libbuild2/config/utility.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <libbuild2/config/utility.hxx>

#include <libbuild2/file.hxx>
#include <libbuild2/context.hxx>
#include <libbuild2/filesystem.hxx>
#include <libbuild2/diagnostics.hxx>

#include <libbuild2/config/module.hxx>

using namespace std;

namespace build2
{
  namespace config
  {
    pair<lookup, bool>
    omitted (scope& rs, const variable& var)
    {
      // This is a stripped-down version of the required() twisted
      // implementation.

      pair<lookup, size_t> org (rs.find_original (var));

      bool n (false); // New flag.
      lookup l (org.first);

      // Treat an inherited value that was set to default as new.
      //
      if (l.defined () && l->extra)
        n = true;

      if (var.overrides != nullptr)
      {
        pair<lookup, size_t> ovr (rs.find_override (var, move (org)));

        if (l != ovr.first) // Overriden?
        {
          // Override is always treated as new.
          //
          n = true;
          l = move (ovr.first);
        }
      }

      if (l.defined ())
        save_variable (rs, var);

      return pair<lookup, bool> (l, n);
    }

    lookup
    optional (scope& rs, const variable& var)
    {
      save_variable (rs, var);

      auto l (rs[var]);
      return l.defined ()
        ? l
        : lookup (rs.assign (var), var, rs); // NULL.
    }

    bool
    specified (scope& rs, const string& n)
    {
      // Search all outer scopes for any value in this namespace.
      //
      // What about "pure" overrides, i.e., those without any original values?
      // Well, they will also be found since their names have the original
      // variable as a prefix. But do they apply? Yes, since we haven't found
      // any original values, they will be "visible"; see find_override() for
      // details.
      //
      const variable& vns (rs.ctx.var_pool.rw (rs).insert ("config." + n));
      for (scope* s (&rs); s != nullptr; s = s->parent_scope ())
      {
        for (auto p (s->vars.find_namespace (vns));
             p.first != p.second;
             ++p.first)
        {
          const variable& var (p.first->first);

          // Ignore config.*.configured.
          //
          if (var.name.size () < 11 ||
              var.name.compare (var.name.size () - 11, 11, ".configured") != 0)
            return true;
        }
      }

      return false;
    }

    bool
    unconfigured (scope& rs, const string& n)
    {
      // Pattern-typed in boot() as bool.
      //
      const variable& var (
        rs.ctx.var_pool.rw (rs).insert ("config." + n + ".configured"));

      save_variable (rs, var);

      auto l (rs[var]); // Include inherited values.
      return l && !cast<bool> (l);
    }

    bool
    unconfigured (scope& rs, const string& n, bool v)
    {
      // Pattern-typed in boot() as bool.
      //
      const variable& var (
        rs.ctx.var_pool.rw (rs).insert ("config." + n + ".configured"));

      save_variable (rs, var);

      value& x (rs.assign (var));

      if (x.null || cast<bool> (x) != !v)
      {
        x = !v;
        return true;
      }
      else
        return false;
    }

    void
    save_variable (scope& rs, const variable& var, uint64_t flags)
    {
      if (module* m = rs.find_module<module> (module::name))
        m->save_variable (var, flags);
    }

    void
    save_module (scope& rs, const char* name, int prio)
    {
      if (module* m = rs.find_module<module> (module::name))
        m->save_module (name, prio);
    }

    void
    create_project (const dir_path& d,
                    const build2::optional<dir_path>& amal,
                    const strings& bmod,
                    const string&  rpre,
                    const strings& rmod,
                    const string&  rpos,
                    bool config,
                    bool buildfile,
                    const char* who,
                    uint16_t verbosity)
    {
      string hdr ("# Generated by " + string (who) + ". Edit if you know"
                  " what you are doing.\n"
                  "#");

      // If the directory exists, verify it's empty. Otherwise, create it.
      //
      if (exists (d))
      {
        if (!empty (d))
          fail << "directory " << d << " exists and is not empty";
      }
      else
        mkdir_p (d, verbosity);

      // Create the build/ subdirectory.
      //
      // Note that for now we use the standard build file/directory scheme.
      //
      mkdir (d / std_build_dir, verbosity);

      // Write build/bootstrap.build.
      //
      {
        path f (d / std_bootstrap_file);

        if (verb >= verbosity)
          text << (verb >= 2 ? "cat >" : "save ") << f;

        try
        {
          ofdstream ofs (f);

          ofs << hdr << endl
              << "project =" << endl;

          if (amal)
          {
            ofs << "amalgamation =";

            if (!amal->empty ())
            {
              ofs << ' ';
              to_stream (ofs, *amal, true /* representation */);
            }

            ofs << endl;
          }

          ofs << endl;

          if (config)
            ofs << "using config" << endl;

          for (const string& m: bmod)
          {
            if (!config || m != "config")
              ofs << "using " << m << endl;
          }

          ofs.close ();
        }
        catch (const io_error& e)
        {
          fail << "unable to write to " << f << ": " << e;
        }
      }

      // Write build/root.build.
      //
      {
        path f (d / std_root_file);

        if (verb >= verbosity)
          text << (verb >= 2 ? "cat >" : "save ") << f;

        try
        {
          ofdstream ofs (f);

          ofs << hdr << endl;

          if (!rpre.empty ())
            ofs << rpre << endl
                << endl;

          for (const string& cm: rmod)
          {
            // If the module name start with '?', then use optional load.
            //
            bool opt (cm.front () == '?');
            string m (cm, opt ? 1 : 0);

            // Append .config unless the module name ends with '.', in which
            // case strip it.
            //
            if (m.back () == '.')
              m.pop_back ();
            else
              m += ".config";

            ofs << "using" << (opt ? "?" : "") << " " << m << endl;
          }

          if (!rpos.empty ())
            ofs << endl
                << rpre << endl;

          ofs.close ();
        }
        catch (const io_error& e)
        {
          fail << "unable to write to " << f << ": " << e;
        }
      }

      // Write root buildfile.
      //
      if (buildfile)
      {
        path f (d / std_buildfile_file);

        if (verb >= verbosity)
          text << (verb >= 2 ? "cat >" : "save ") << f;

        try
        {
          ofdstream ofs (f);

          ofs << hdr << endl
              << "./: {*/ -build/}" << endl;

          ofs.close ();
        }
        catch (const io_error& e)
        {
          fail << "unable to write to " << f << ": " << e;
        }
      }
    }
  }
}