aboutsummaryrefslogtreecommitdiff
path: root/build2/cxx/init.cxx
blob: 6c8cbadcb20b80db9513bd83dcca27f0c650f643 (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
// file      : build2/cxx/init.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <build2/cxx/init.hxx>

#include <build2/scope.hxx>
#include <build2/context.hxx>
#include <build2/diagnostics.hxx>

#include <build2/cc/guess.hxx>
#include <build2/cc/module.hxx>

#include <build2/cxx/target.hxx>

using namespace std;
using namespace butl;

namespace build2
{
  namespace cxx
  {
    using cc::compiler_id;
    using cc::compiler_info;

    class config_module: public cc::config_module
    {
    public:
      explicit
      config_module (config_data&& d)
          : config_data (move (d)), cc::config_module (move (d)) {}

      strings
      translate_std (const compiler_info&,
                     scope&,
                     const string*) const override;
    };

    using cc::module;

    strings config_module::
    translate_std (const compiler_info& ci, scope& rs, const string* v) const
    {
      strings r;

      auto id (ci.id.value ());
      uint64_t mj (ci.version.major);
      uint64_t mi (ci.version.minor);
      uint64_t p (ci.version.patch);

      // Features.
      //
      auto enter = [&rs] (const char* v) -> const variable&
      {
        return var_pool.rw (rs).insert<bool> (v, variable_visibility::project);
      };

      //bool concepts (false); auto& v_c (enter ("cxx.features.concepts"));
      bool modules (false); auto& v_m (enter ("cxx.features.modules"));

      // Translate "latest" and "experimental" to the compiler/version-
      // appropriate option(s).
      //
      if (v != nullptr && (*v == "latest" || *v == "experimental"))
      {
        // Experimental is like latest with some extra stuff enabled via
        // additional switches.
        //
        const char* o (nullptr);

        switch (id)
        {
        case compiler_id::msvc:
          {
            // VC14u3 and later has /std:c++latest.
            //
            if (mj > 19 || (mj == 19 && (mi > 0 || (mi == 0 && p >= 24215))))
              o = "/std:c++latest";

            break;
          }
        case compiler_id::gcc:
          {
            //if    (mj >= 8)            o = "-std=c++2a"; // 20
            if      (mj >= 5)            o = "-std=c++1z"; // 17
            else if (mj == 4 && mi >= 8) o = "-std=c++1y"; // 14
            else if (mj == 4 && mi >= 4) o = "-std=c++0x"; // 11

            break;
          }
        case compiler_id::clang:
          {
            // Re-map Apple versions to vanilla Clang based on the following
            // release point:
            //
            // 5.1 -> 3.4
            // 6.0 -> 3.5
            //
            // Note that this mapping is also used to enable experimental
            // features below.
            //
            if (ci.id.variant == "apple")
            {
              if      (mj >= 6)            {mj = 3; mi = 5;}
              else if (mj == 5 && mi >= 1) {mj = 3; mi = 4;}
              else                         {mj = 3; mi = 0;}
            }

            if       (mj >= 5)                         o = "-std=c++2a"; // 20
            else if  (mj >  3 || (mj == 3 && mi >= 5)) o = "-std=c++1z"; // 17
            else if  (mj == 3 && mi >= 4)              o = "-std=c++1y"; // 14
            else     /* ??? */                         o = "-std=c++0x"; // 11

            break;
          }
        case compiler_id::icc:
          {
            if      (mj >= 17)                         o = "-std=c++1z"; // 17
            else if (mj >  15 || (mj == 15 && p >= 3)) o = "-std=c++1y"; // 14
            else    /* ??? */                          o = "-std=c++0x"; // 11

            break;
          }
        }

        if (o != nullptr)
          r.push_back (o);

        if (*v == "experimental")
        {
          // Unless disabled by the user, try to enable C++ modules. Here
          // we use a tri-state:
          //
          // - false        - disabled
          // - unspecified  - enabled if practically usable
          // - true         - enabled even if practically unusable
          //
          lookup l;
          if (!(l = rs[v_m]) || cast<bool> (l))
          {
            switch (id)
            {
            case compiler_id::msvc:
              {
                // While modules are supported in VC15u0 (19.10), there is a
                // bug in separate interface/implementation unit support which
                // makes them pretty much unusable. This has been fixed in
                // VC15u3 (19.11)
                //
                if (mj > 19 || (mj == 19 && mi >= (l ? 10 : 11)))
                {
                  r.push_back ("/D__cpp_modules=201703"); // n4647
                  r.push_back ("/experimental:module");
                  modules = true;
                }
                break;
              }
            case compiler_id::gcc:
              {
                // Enable starting with GCC 8.0.0 (currently the c++-modules
                // branch).
                //
                if (l && // Barely usable at the moment.
                    mj >= 8 &&
                    ci.version.build.find ("c++-modules") != string::npos)
                {
                  r.push_back ("-fmodules");
                  modules = true;
                }
                break;
              }
            case compiler_id::clang:
              {
                // Enable starting with Clang 5.0.0.
                //
                // Note that we are using Apple to vanilla Clang version re-
                // map from above so may need to update things there as well.
                //
                // Also see Clang modules support hack in cc::compile.
                //
                if (mj >= 5)
                {
                  r.push_back ("-D__cpp_modules=201704"); // p0629r0
                  r.push_back ("-fmodules-ts");
                  modules = true;
                }
                break;
              }
            case compiler_id::icc:
              break; // No modules support yet.
            }
          }
        }
      }
      else
      {
        // Otherwise translate the standard value.
        //
        switch (id)
        {
        case compiler_id::msvc:
          {
            // C++ standard-wise, with VC you got what you got up until 14u2.
            // Starting with 14u3 there is now the /std: switch which defaults
            // to c++14 but can be set to c++latest.
            //
            // The question is also whether we should verify that the
            // requested standard is provided by this VC version. And if so,
            // from which version should we say VC supports 11, 14, and 17? We
            // should probably be as loose as possible here since the author
            // will always be able to tighten (but not loosen) this in the
            // buildfile (i.e., detect unsupported versions).
            //
            // For now we are not going to bother doing this for C++03.
            //
            if (v == nullptr)
              ;
            else if (*v != "98" && *v != "03")
            {
              bool sup (false);

              if      (*v == "11") // C++11 since VS2010/10.0.
              {
                sup = mj >= 16;
              }
              else if (*v == "14") // C++14 since VS2015/14.0.
              {
                sup = mj >= 19;
              }
              else if (*v == "17") // C++17 since VS2015/14.0u2.
              {
                // Note: the VC15 compiler version is 19.10.
                //
                sup = (mj > 19 ||
                       (mj == 19 && (mi > 0 || (mi == 0 && p >= 23918))));
              }

              if (!sup)
                fail << "C++" << *v << " is not supported by "
                     << ci.signature <<
                  info << "required by " << project (rs) << '@'
                     << rs.out_path ();

              // VC14u3 and later has /std:
              //
              if (mj > 19 || (mj == 19 && (mi > 0 || (mi == 0 && p >= 24215))))
              {
                if (*v == "17")
                  r.push_back ("/std:c++latest");
              }
            }
            break;
          }
        case compiler_id::gcc:
        case compiler_id::clang:
        case compiler_id::icc:
          {
            // Translate 11 to 0x, 14 to 1y, 17 to 1z, and 20 to 2a for
            // compatibility with older versions of the compilers.
            //
            if (v == nullptr)
              ;
            else
            {
              string o ("-std=");

              if      (*v == "98") o += "c++98";
              else if (*v == "03") o += "c++03";
              else if (*v == "11") o += "c++0x";
              else if (*v == "14") o += "c++1y";
              else if (*v == "17") o += "c++1z";
              else if (*v == "20") o += "c++2a";
              else o += *v; // In case the user specifies e.g., 'gnu++17'.

              r.push_back (move (o));
            }
            break;
          }
        }
      }

      rs.assign (v_m) = modules;
      //rs.assign (v_c) = concepts;

      return r;
    }

    bool
    config_init (scope& rs,
                 scope& bs,
                 const location& loc,
                 unique_ptr<module_base>& mod,
                 bool,
                 bool,
                 const variable_map& hints)
    {
      tracer trace ("cxx::config_init");
      l5 ([&]{trace << "for " << bs.out_path ();});

      // We only support root loading (which means there can only be one).
      //
      if (&rs != &bs)
        fail (loc) << "cxx.config module must be loaded in project root";

      // Load cc.core.vars so that we can cache all the cc.* variables.
      //
      if (!cast_false<bool> (rs["cc.core.vars.loaded"]))
        load_module (rs, rs, "cc.core.vars", loc);

      // Enter all the variables and initialize the module data.
      //
      auto& v (var_pool.rw (rs));

      cc::config_data d {
        cc::lang::cxx,

        "cxx",
        "c++",
        "g++",
        ".ii",

        // Note: some overridable, some not.
        //
        v.insert<path>    ("config.cxx",          true),
        v.insert<strings> ("config.cxx.poptions", true),
        v.insert<strings> ("config.cxx.coptions", true),
        v.insert<strings> ("config.cxx.loptions", true),
        v.insert<strings> ("config.cxx.libs",     true),

        v.insert<process_path> ("cxx.path"),
        v.insert<dir_paths>    ("cxx.sys_lib_dirs"),
        v.insert<dir_paths>    ("cxx.sys_inc_dirs"),

        v.insert<strings> ("cxx.poptions"),
        v.insert<strings> ("cxx.coptions"),
        v.insert<strings> ("cxx.loptions"),
        v.insert<strings> ("cxx.libs"),

        v["cc.poptions"],
        v["cc.coptions"],
        v["cc.loptions"],
        v["cc.libs"],

        v.insert<strings>      ("cxx.export.poptions"),
        v.insert<strings>      ("cxx.export.coptions"),
        v.insert<strings>      ("cxx.export.loptions"),
        v.insert<vector<name>> ("cxx.export.libs"),

        v["cc.export.poptions"],
        v["cc.export.coptions"],
        v["cc.export.loptions"],
        v["cc.export.libs"],

        v["cc.type"],
        v["cc.system"],
        v["cc.module_name"],
        v["cc.reprocess"],

        // Ability to indicate that source is already (partially)
        // preprocessed. Valid values are 'none' (not preprocessed),
        // 'includes' (no #include directives in source), 'modules' (as above
        // plus no module declaration depends on preprocessor, e.g., #ifdef,
        // etc), and 'all' (the source is fully preprocessed). Note that for
        // 'all' the source can still contain comments and line
        // continuations. Note also that for some compilers (e.g., VC) there
        // is no way to signal that the source is already preprocessed.
        //
        v.insert<string>   ("cxx.preprocessed"),

        v.insert<string>   ("cxx.std", variable_visibility::project),

        v.insert<string>   ("cxx.id"),
        v.insert<string>   ("cxx.id.type"),
        v.insert<string>   ("cxx.id.variant"),

        v.insert<string>   ("cxx.version"),
        v.insert<uint64_t> ("cxx.version.major"),
        v.insert<uint64_t> ("cxx.version.minor"),
        v.insert<uint64_t> ("cxx.version.patch"),
        v.insert<string>   ("cxx.version.build"),

        v.insert<string>   ("cxx.signature"),
        v.insert<string>   ("cxx.checksum"),

        v.insert<target_triplet> ("cxx.target"),

        v.insert<string>   ("cxx.target.cpu"),
        v.insert<string>   ("cxx.target.vendor"),
        v.insert<string>   ("cxx.target.system"),
        v.insert<string>   ("cxx.target.version"),
        v.insert<string>   ("cxx.target.class")
      };

      assert (mod == nullptr);
      config_module* m (new config_module (move (d)));
      mod.reset (m);
      m->init (rs, loc, hints);
      return true;
    }

    static const target_type* const hdr[] =
    {
      &hxx::static_type,
      &h::static_type,
      &ixx::static_type,
      &txx::static_type,
      &mxx::static_type,
      nullptr
    };

    static const target_type* const inc[] =
    {
      &hxx::static_type,
      &h::static_type,
      &ixx::static_type,
      &txx::static_type,
      &mxx::static_type,
      &cxx::static_type,
      &c::static_type,
      nullptr
    };

    bool
    init (scope& rs,
          scope& bs,
          const location& loc,
          unique_ptr<module_base>& mod,
          bool,
          bool,
          const variable_map& hints)
    {
      tracer trace ("cxx::init");
      l5 ([&]{trace << "for " << bs.out_path ();});

      // We only support root loading (which means there can only be one).
      //
      if (&rs != &bs)
        fail (loc) << "cxx module must be loaded in project root";

      // Load cxx.config.
      //
      if (!cast_false<bool> (rs["cxx.config.loaded"]))
        load_module (rs, rs, "cxx.config", loc, false, hints);

      auto& vp (var_pool.rw (rs));

      bool modules (cast<bool> (rs["cxx.features.modules"]));

      bool symexport (
        modules &&
        cast_false<bool> (
          rs[vp.insert<bool> ("cxx.features.symexport",
                              variable_visibility::project)]));

      config_module& cm (*rs.modules.lookup<config_module> ("cxx.config"));

      cc::data d {
        cm,

        "cxx.compile",
        "cxx.link",
        "cxx.install",
        "cxx.uninstall",

        cm.cid,
        cast<string>         (rs[cm.x_id_variant]),
        cast<uint64_t>       (rs[cm.x_version_major]),
        cast<uint64_t>       (rs[cm.x_version_minor]),
        cast<process_path>   (rs[cm.x_path]),
        cast<target_triplet> (rs[cm.x_target]),

        cm.tstd,

        modules,
        symexport,

        cast_null<process_path> (rs["pkgconfig.path"]),
        cast<dir_paths> (rs[cm.x_sys_lib_dirs]),
        cast<dir_paths> (rs[cm.x_sys_inc_dirs]),

        cxx::static_type,
        modules ? &mxx::static_type : nullptr,
        hdr,
        inc
      };

      assert (mod == nullptr);
      module* m;
      mod.reset (m = new module (move (d)));
      m->init (rs, loc, hints);
      return true;
    }
  }
}