aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/cc/functions.cxx
blob: 6b646775d017e19181bbb1a7b4c6ccf5d2cb8b11 (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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// file      : libbuild2/cc/functions.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <libbuild2/cc/link-rule.hxx>
#include <libbuild2/cc/compile-rule.hxx>

#include <libbuild2/function.hxx>
#include <libbuild2/variable.hxx>

#include <libbuild2/bin/target.hxx>
#include <libbuild2/bin/utility.hxx>

#include <libbuild2/cc/module.hxx>
#include <libbuild2/cc/utility.hxx>

#include <libbuild2/functions-name.hxx> // to_target()

namespace build2
{
  namespace cc
  {
    using namespace bin;

    // Common thunk for $x.*(<targets> [, ...]) functions.
    //
    struct thunk_data
    {
      const char* x;
      void (*f) (strings&,
                 const vector_view<value>&, const module&, const scope&,
                 action, const target&);
    };

    static value
    thunk (const scope* bs,
           vector_view<value> vs,
           const function_overload& f)
    {
      const auto& d (*reinterpret_cast<const thunk_data*> (&f.data));

      if (bs == nullptr)
        fail << f.name << " called out of scope";

      const scope* rs (bs->root_scope ());

      if (rs == nullptr)
        fail << f.name << " called out of project";

      // Note that we also allow calling this during match since an ad hoc
      // recipe with dynamic dependency extraction (depdb-dyndep) executes its
      // depdb preamble during match (after matching all the prerequisites).
      //
      if (bs->ctx.phase != run_phase::match &&
          bs->ctx.phase != run_phase::execute)
        fail << f.name << " can only be called during execution";

      const module* m (rs->find_module<module> (d.x));

      if (m == nullptr)
        fail << f.name << " called without " << d.x << " module loaded";

      // We can assume these are present due to function's types signature.
      //
      names& ts_ns (vs[0].as<names> ()); // <targets>

      // In a somewhat hackish way strip the outer operation to match how we
      // call the underlying functions in the compile/link rules. This should
      // be harmless since ad hoc recipes are always for the inner operation.
      //
      action a (rs->ctx.current_action ().inner_action ());

      strings r;
      for (auto i (ts_ns.begin ()); i != ts_ns.end (); ++i)
      {
        name& n (*i), o;
        const target& t (to_target (*bs, move (n), move (n.pair ? *++i : o)));

        if (!t.matched (a))
          fail << t << " is not matched" <<
            info << "make sure this target is listed as prerequisite";

        d.f (r, vs, *m, *bs, a, t);
      }

      return value (move (r));
    }

    // Common thunk for $x.lib_*(<targets>, <otype> [, ...]) functions.
    //
    struct lib_thunk_data
    {
      const char* x;
      void (*f) (void*, strings&,
                 const vector_view<value>&, const module&, const scope&,
                 action, const file&, bool, linfo);
    };

    static value
    lib_thunk_impl (void* ls,
                    const scope* bs,
                    vector_view<value> vs,
                    const function_overload& f)
    {
      const auto& d (*reinterpret_cast<const lib_thunk_data*> (&f.data));

      if (bs == nullptr)
        fail << f.name << " called out of scope";

      const scope* rs (bs->root_scope ());

      if (rs == nullptr)
        fail << f.name << " called out of project";

      if (bs->ctx.phase != run_phase::match && // See above.
          bs->ctx.phase != run_phase::execute)
        fail << f.name << " can only be called during execution";

      const module* m (rs->find_module<module> (d.x));

      if (m == nullptr)
        fail << f.name << " called without " << d.x << " module loaded";

      // We can assume these are present due to function's types signature.
      //
      names& ts_ns (vs[0].as<names> ()); // <targets>
      names& ot_ns (vs[1].as<names> ()); // <otype>

      linfo li;
      {
        string t (convert<string> (move (ot_ns)));

        const target_type* tt (bs->find_target_type (t));

        if (tt == nullptr)
          fail << "unknown target type '" << t << "'";

        // Try both linker and compiler output types.
        //
        otype ot (link_type (*tt).type);

        switch (ot)
        {
        case otype::e:
        case otype::a:
        case otype::s:
          break;
        default:
          ot = compile_type (*tt);
          switch (ot)
          {
          case otype::e:
          case otype::a:
          case otype::s:
            break;
          default:
            fail << "target type " << t << " is not compiler/linker output";
          }
        }

        li = link_info (*bs, ot);
      }

      // In a somewhat hackish way strip the outer operation to match how we
      // call the underlying functions in the compile/link rules. This should
      // be harmless since ad hoc recipes are always for the inner operation.
      //
      action a (rs->ctx.current_action ().inner_action ());

      strings r;
      for (auto i (ts_ns.begin ()); i != ts_ns.end (); ++i)
      {
        name& n (*i), o;
        const target& t (to_target (*bs, move (n), move (n.pair ? *++i : o)));

        const file* f;
        bool la (false);

        if ((la = (f = t.is_a<libux> ())) ||
            (la = (f = t.is_a<liba>  ())) ||
            (     (f = t.is_a<libs>  ())))
        {
          if (!t.matched (a))
            fail << t << " is not matched" <<
              info << "make sure this target is listed as prerequisite";

          d.f (ls, r, vs, *m, *bs, a, *f, la, li);
        }
        else
          fail << t << " is not a library target";
      }

      return value (move (r));
    }

    template <typename L>
    static value
    lib_thunk (const scope* bs,
               vector_view<value> vs,
               const function_overload& f)
    {
      L ls;
      return lib_thunk_impl (&ls, bs, vs, f);
    }

    // @@ Maybe we should provide wrapper functions that return all the
    //    compile options (including from *.?options, mode, etc) and all the
    //    link arguments in the correct order, etc. Can call them:
    //
    //    compile_options()
    //    link_arguments()
    //

    void compile_rule::
    functions (function_family& f, const char* x)
    {
      // $<module>.lib_poptions(<lib-targets>, <otype>)
      //
      // Return the preprocessor options that should be passed when compiling
      // sources that depend on the specified libraries. The second argument
      // is the output target type (obje, objs, etc).
      //
      // Note that passing multiple targets at once is not a mere convenience:
      // this also allows for more effective duplicate suppression.
      //
      // Note also that this function can only be called during execution
      // after all the specified library targets have been matched. Normally
      // it is used in ad hoc recipes to implement custom compilation.
      //
      // Note that this function is not pure.
      //
      f.insert (".lib_poptions", false).
        insert<lib_thunk_data, names, names> (
        &lib_thunk<appended_libraries>,
        lib_thunk_data {
          x,
          [] (void* ls, strings& r,
              const vector_view<value>&, const module& m, const scope& bs,
              action a, const file& l, bool la, linfo li)
          {
            m.append_library_options (
              *static_cast<appended_libraries*> (ls), r,
              bs, a, l, la, li);
          }});

      // $<module>.find_system_header(<name>)
      //
      // Return the header path if the specified header exists in one of the
      // system header search directories and NULL otherwise. System header
      // search directories are those that the compiler searches by default
      // plus directories specified as part of the compiler mode options (but
      // not *.poptions).
      //
      // Note that this function is not pure.
      //
      f.insert (".find_system_header", false).
        insert<const char*, names> (
          [] (const scope* bs,
              vector_view<value> vs,
              const function_overload& f) -> value
          {
            const char* x (*reinterpret_cast<const char* const*> (&f.data));

            if (bs == nullptr)
              fail << f.name << " called out of scope";

            const scope* rs (bs->root_scope ());

            if (rs == nullptr)
              fail << f.name << " called out of project";

            const module* m (rs->find_module<module> (x));

            if (m == nullptr)
              fail << f.name << " called without " << x << " module loaded";

            // We can assume the argument is present due to function's types
            // signature.
            //
            auto r (m->find_system_header (convert<path> (move (vs[0]))));
            return r ? value (move (*r)) : value (nullptr);
          },
          x);
    }

    void link_rule::
    functions (function_family& f, const char* x)
    {
      // $<module>.lib_libs(<lib-targets>, <otype> [, <flags> [, <self>]])
      //
      // Return the libraries (and any associated options) that should be
      // passed when linking targets that depend on the specified libraries.
      // The second argument is the output target type (exe, libs, etc).
      //
      // The following flags are supported:
      //
      // whole    - link the specified libraries in the whole archive mode
      //
      // absolute - return absolute paths to the libraries
      //
      // If the last argument is false, then do not return the specified
      // libraries themselves.
      //
      // Note that passing multiple targets at once is not a mere convenience:
      // this also allows for more effective duplicate suppression.
      //
      // Note also that this function can only be called during execution
      // after all the specified library targets have been matched. Normally
      // it is used in ad hoc recipes to implement custom linking.
      //
      // Note that this function is not pure.
      //
      f.insert (".lib_libs", false).
        insert<lib_thunk_data, names, names, optional<names>, optional<names>> (
        &lib_thunk<appended_libraries>,
        lib_thunk_data {
          x,
          [] (void* ls, strings& r,
              const vector_view<value>& vs, const module& m, const scope& bs,
              action a, const file& l, bool la, linfo li)
          {
            lflags lf (0);
            bool rel (true);
            if (vs.size () > 2)
            {
              for (const name& f: vs[2].as<names> ())
              {
                string s (convert<string> (name (f)));

                if (s == "whole")
                  lf |= lflag_whole;
                else if (s == "absolute")
                  rel = false;
                else
                  fail << "invalid flag '" << s << "'";
              }
            }

            bool self (vs.size () > 3 ? convert<bool> (vs[3]) : true);

            m.append_libraries (
              *static_cast<appended_libraries*> (ls), r,
              nullptr /* sha256 */, nullptr /* update */, timestamp_unknown,
              bs, a, l, la, lf, li, nullopt /* for_install */, self, rel);
          }});

      // $<module>.lib_rpaths(<lib-targets>, <otype> [, <link> [, <self>]])
      //
      // Return the rpath options that should be passed when linking targets
      // that depend on the specified libraries. The second argument is the
      // output target type (exe, libs, etc).
      //
      // If the third argument is true, then use rpath-link options rather
      // than rpath (which is what should normally be used when linking for
      // install, for example).
      //
      // If the last argument is false, then do not return the options for the
      // specified libraries themselves.
      //
      // Note that passing multiple targets at once is not a mere convenience:
      // this also allows for more effective duplicate suppression.
      //
      // Note also that this function can only be called during execution
      // after all the specified library targets have been matched. Normally
      // it is used in ad hoc recipes to implement custom linking.
      //
      // Note that this function is not pure.
      //
      f.insert (".lib_rpaths", false).
        insert<lib_thunk_data, names, names, optional<names>, optional<names>> (
        &lib_thunk<rpathed_libraries>,
        lib_thunk_data {
          x,
          [] (void* ls, strings& r,
              const vector_view<value>& vs, const module& m, const scope& bs,
              action a, const file& l, bool la, linfo li)
          {
            bool link (vs.size () > 2 ? convert<bool> (vs[2]) : false);
            bool self (vs.size () > 3 ? convert<bool> (vs[3]) : true);
            m.rpath_libraries (*static_cast<rpathed_libraries*> (ls), r,
                               bs,
                               a, l, la, li, link, self);
          }});

      // $cxx.obj_modules(<obj-targets>)
      //
      // Return object files corresponding to module interfaces that are used
      // by the specified object files and that belong to binless libraries.
      //
      // Note that passing multiple targets at once is not a mere convenience:
      // this also allows for more effective duplicate suppression.
      //
      // Note also that this function can only be called during execution
      // after all the specified object file targets have been matched.
      // Normally it is used in ad hoc recipes to implement custom linking.
      //
      // Note that this function is not pure.
      //
      f.insert (".obj_modules", false).
        insert<thunk_data, names> (
        &thunk,
        thunk_data {
          x,
          [] (strings& r,
              const vector_view<value>&, const module& m, const scope& bs,
              action a, const target& t)
          {
            if (const file* f = t.is_a<objx> ())
            {
              if (m.modules)
                m.append_binless_modules (r, nullptr /* sha256 */, bs, a, *f);
            }
            else
              fail << t << " is not an object file target";
          }});

      // $<module>.deduplicate_export_libs(<names>)
      //
      // Deduplicate interface library dependencies by removing libraries that
      // are also interface dependencies of the specified libraries. This can
      // result in significantly better build performance for heavily
      // interface-interdependent library families (for example, like Boost).
      // Typical usage:
      //
      // import intf_libs  = ...
      // import intf_libs += ...
      // ...
      // import intf_libs += ...
      // intf_libs = $cxx.deduplicate_export_libs($intf_libs)
      //
      // Notes:
      //
      // 1. We only consider unqualified absolute/normalized target names (the
      //    idea is that the installed case will already be deduplicated).
      //
      // 2. We assume all the libraries listed are of the module type and only
      //    look for cc.export.libs and <module>.export.libs.
      //
      // 3. No member/group selection/linkup: we resolve *.export.libs on
      //    whatever is listed (so no liba{}/libs{} overrides will be
      //    considered).
      //
      // Because of (2) and (3), this functionality should only be used on a
      // controlled list of libraries (usually libraries that belong to the
      // same family as this library).
      //
      // Note that a similar deduplication is also performed when processing
      // the libraries. However, it may still make sense to do it once at the
      // source for really severe cases (like Boost).
      //
      // Note that this function is not pure.
      //
      f.insert (".deduplicate_export_libs", false).
        insert<const char*, names> (
          [] (const scope* bs,
              vector_view<value> vs,
              const function_overload& f) -> value
          {
            const char* x (*reinterpret_cast<const char* const*> (&f.data));

            if (bs == nullptr)
              fail << f.name << " called out of scope";

            const scope* rs (bs->root_scope ());

            if (rs == nullptr)
              fail << f.name << " called out of project";

            const module* m (rs->find_module<module> (x));

            if (m == nullptr)
              fail << f.name << " called without " << x << " module loaded";

            // We can assume the argument is present due to function's types
            // signature.
            //
            names& r (vs[0].as<names> ());
            m->deduplicate_export_libs (*bs,
                                        vector<name> (r.begin (), r.end ()),
                                        r);
            return value (move (r));
          },
          x);

      // $<module>.find_system_library(<name>)
      //
      // Return the library path if the specified library exists in one of the
      // system library search directories. System library search directories
      // are those that the compiler searches by default plus directories
      // specified as part of the compiler mode options (but not *.loptions).
      //
      // The library can be specified in the same form as expected by the
      // linker (-lfoo for POSIX, foo.lib for MSVC) or as a complete name.
      //
      // Note that this function is not pure.
      //
      f.insert (".find_system_library", false).
        insert<const char*, names> (
          [] (const scope* bs,
              vector_view<value> vs,
              const function_overload& f) -> value
          {
            const char* x (*reinterpret_cast<const char* const*> (&f.data));

            if (bs == nullptr)
              fail << f.name << " called out of scope";

            const scope* rs (bs->root_scope ());

            if (rs == nullptr)
              fail << f.name << " called out of project";

            const module* m (rs->find_module<module> (x));

            if (m == nullptr)
              fail << f.name << " called without " << x << " module loaded";

            // We can assume the argument is present due to function's types
            // signature.
            //
            auto r (m->find_system_library (convert<strings> (move (vs[0]))));
            return r ? value (move (*r)) : value (nullptr);
          },
          x);
    }
  }
}