aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/cc/install-rule.cxx
blob: 0065c0a723c4d5889635052a54d3ad024061e792 (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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
// file      : libbuild2/cc/install-rule.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <libbuild2/cc/install-rule.hxx>

#include <libbuild2/algorithm.hxx>

#include <libbuild2/bin/target.hxx>

#include <libbuild2/cc/utility.hxx>
#include <libbuild2/cc/link-rule.hxx> // match()

using namespace std;

namespace build2
{
  namespace cc
  {
    using namespace bin;

    // install_rule
    //
    install_rule::
    install_rule (data&& d, const link_rule& l)
        : common (move (d)), link_ (l) {}

    // Wrap the file_rule's recipe into a data-carrying recipe.
    //
    struct install_match_data
    {
      build2::recipe        recipe;
      uint64_t              options; // Match options.
      link_rule::libs_paths libs_paths;

      target_state
      operator() (action a, const target& t)
      {
        return recipe (a, t);
      }
    };

    bool install_rule::
    filter (action a, const target& t, const target& m) const
    {
      if (!t.is_a<exe> ())
      {
        // If runtime-only, filter out all known buildtime target types.
        //
        const auto& md (t.data<install_match_data> (a));

        if ((md.options & lib::option_install_buildtime) == 0)
        {
          if (m.is_a<liba> () || // Staic library.
              m.is_a<pc> ()   || // pkg-config file.
              m.is_a<libi> ())   // Import library.
            return false;
        }
      }

      return true;
    }

    pair<const target*, uint64_t> install_rule::
    filter (const scope* is,
            action a, const target& t, prerequisite_iterator& i,
            match_extra& me) const
    {
      // NOTE: see libux_install_rule::filter() if changing anything here.

      const prerequisite& p (i->prerequisite);

      uint64_t options (match_extra::all_options);

      otype ot (link_type (t).type);

      // @@ TMP: drop eventually.
      //
#if 0
      // If this is a shared library prerequisite, install it as long as it is
      // in the installation scope.
      //
      // Less obvious: we also want to install a static library prerequisite
      // of a library (since it could be referenced from its .pc file, etc).
      //
      // Note: for now we assume these prerequisites never come from see-
      // through groups.
      //
      // Note: we install ad hoc prerequisites by default.
      //

      // Note: at least one must be true since we only register this rule for
      // exe{}, and lib[as]{} (this makes sure the following if-condition will
      // always be true for libx{}).
      //
      bool st (t.is_a<exe>  () || t.is_a<libs> ()); // Target needs shared.
      bool at (t.is_a<liba> () || t.is_a<libs> ()); // Target needs static.
      assert (st || at);

      if ((st && (p.is_a<libx> () || p.is_a<libs> ())) ||
          (at && (p.is_a<libx> () || p.is_a<liba> ())))
      {
        const target* pt (&search (t, p));

        // If this is the lib{}/libu*{} group, pick a member which we would
        // link. For libu*{} we want the "see through" logic.
        //
        if (const libx* l = pt->is_a<libx> ())
          pt = link_member (*l, a, link_info (t.base_scope (), ot));

        // Note: not redundant since we could be returning a member.
        //
        if ((st && pt->is_a<libs> ()) || (at && pt->is_a<liba> ()))
        {
          // Adjust match options.
          //
          if (a.operation () != update_id)
          {
            if (t.is_a<exe> ())
              options = lib::option_install_runtime;
            else
            {
              // This is a library prerequisite of a library target and
              // runtime-only begets runtime-only.
              //
              if (me.cur_options == lib::option_install_runtime)
                options = lib::option_install_runtime;
            }
          }

          return make_pair (is == nullptr || pt->in (*is) ? pt : nullptr,
                            options);
        }

        // See through to libu*{} members. Note that we are always in the same
        // project (and thus amalgamation).
        //
        if (pt->is_a<libux> ())
        {
          // Adjust match options (similar to above).
          //
          if (a.operation () != update_id && !pt->is_a<libue> ())
          {
            if (t.is_a<exe> ())
              options = lib::option_install_runtime;
            else
            {
              if (me.cur_options == lib::option_install_runtime)
                options = lib::option_install_runtime;
            }
          }

          return make_pair (pt, options);
        }
      }
#else
      // Note that at first it may seem like we don't need to install static
      // library prerequisites of executables. But such libraries may still
      // have prerequisites that are needed at runtime (say, some data files).
      // So we install all libraries as long as they are in the installation
      // scope and deal with runtime vs buildtime distiction using match
      // options.
      //
      // Note: for now we assume these prerequisites never come from see-
      // through groups.
      //
      // Note: we install ad hoc prerequisites by default.
      //
      if (p.is_a<libx> () || p.is_a<libs> () || p.is_a<liba> ())
      {
        const target* pt (&search (t, p));

        // If this is the lib{}/libu*{} group, pick a member which we would
        // link. For libu*{} we want the "see through" logic.
        //
        if (const libx* l = pt->is_a<libx> ())
          pt = link_member (*l, a, link_info (t.base_scope (), ot));

        // Adjust match options.
        //
        if (a.operation () != update_id)
        {
          if (t.is_a<exe> ())
            options = lib::option_install_runtime;
          else
          {
            // This is a library prerequisite of a library target and
            // runtime-only begets runtime-only.
            //
            if (me.cur_options == lib::option_install_runtime)
              options = lib::option_install_runtime;
          }
        }

        // Note: not redundant since we could be returning a member.
        //
        if (pt->is_a<libs> () || pt->is_a<liba> ())
        {
          return make_pair (is == nullptr || pt->in (*is) ? pt : nullptr,
                            options);
        }
        else // libua{} or libus{}
        {
          // See through to libu*{} members. Note that we are always in the
          // same project (and thus amalgamation).
          //
          return make_pair (pt, options);
        }
      }
#endif

      // The rest of the tests only succeed if the base filter() succeeds.
      //
      const target* pt (file_rule::filter (is, a, t, p, me).first);
      if (pt == nullptr)
        return make_pair (pt, options);

      // Don't install executable's or runtime-only library's prerequisite
      // headers and module interfaces.
      //
      // Note that if they come from a group, then we assume the entire
      // group is not to be installed.
      //
      // We also skip sources since they may "pull" a header if they are a
      // member of an ad hoc group.
      //
      auto header_source = [this] (const auto& p)
      {
        return (x_header (p)   ||
                p.is_a (x_src) ||
                (x_mod != nullptr && p.is_a (*x_mod)) ||
                (x_asp != nullptr && p.is_a (*x_asp)) ||
                (x_obj != nullptr && p.is_a (*x_obj)));
      };

      if (t.is_a<exe> () ||
          (a.operation () != update_id &&
           me.cur_options == lib::option_install_runtime))
      {
        if (header_source (p))
          pt = nullptr;
        else if (p.type.see_through ())
        {
          for (i.enter_group (); i.group (); )
          {
            ++i; // Note that we have to iterate until the end of the group.
            if (pt != nullptr && header_source (*i))
              pt = nullptr;
          }
        }

        if (pt == nullptr)
          return make_pair (pt, options);
      }

      // Here is a problem: if the user spells the obj*/bmi*{} targets
      // explicitly, then the source files, including headers/modules may be
      // specified as preprequisites of those targets and not of this target.
      // While this can be worked around for headers by also listing them as
      // prerequisites of this target, this won't work for modules (since they
      // are compiled). So what we are going to do here is detect bmi*{} and
      // translate them to their mxx{} (this doesn't quite work for headers
      // since there would normally be many of them).
      //
      // Note: for now we assume bmi*{} never come from see-through groups.
      //
      bool g (false);
      if (p.is_a<bmi> () || (g = p.is_a (compile_types (ot).bmi)))
      {
        if (g)
          resolve_group (a, *pt);

        for (prerequisite_member pm:
               group_prerequisite_members (a, *pt, members_mode::maybe))
        {
          // This is tricky: we need to "look" inside groups for mxx{} but if
          // found, remap to the group, not member.
          //
          if (pm.is_a (*x_mod))
          {
            pt = t.is_a<exe> ()
              ? nullptr
              : file_rule::filter (is, a, *pt, pm.prerequisite, me).first;
            break;
          }
        }

        if (pt == nullptr)
          return make_pair (pt, options);
      }

      return make_pair (pt, options);
    }

    bool install_rule::
    match (action a, target& t, const string&, match_extra& me) const
    {
      // We only want to handle installation if we are also the ones building
      // this target. So first run link's match().
      //
      return link_.sub_match (x_link, update_id, a, t, me) &&
        file_rule::match (a, t);
    }

    recipe install_rule::
    apply (action a, target& t, match_extra& me) const
    {
      // Handle match options.
      //
      // Do it before calling apply_impl() since we need this information
      // in the filter() callbacks.
      //
      if (a.operation () != update_id)
      {
        if (!t.is_a<exe> ())
        {
          if (me.new_options == 0)
            me.new_options = lib::option_install_runtime; // Minimum we can do.

          me.cur_options = me.new_options;
        }
      }

      recipe r (file_rule::apply_impl (
                  a, t, me,
                  me.cur_options != match_extra::all_options /* reapply */));

      if (r == nullptr)
      {
        me.cur_options = match_extra::all_options; // Noop for all options.
        return noop_recipe;
      }

      if (a.operation () == update_id)
      {
        // Signal to the link rule that this is update for install. And if the
        // update has already been executed, verify it was done for install.
        //
        auto& md (t.data<link_rule::match_data> (a.inner_action ()));

        if (md.for_install)
        {
          // Note: see also append_libraries() for the other half.
          //
          if (!*md.for_install)
            fail << "incompatible " << t << " build" <<
              info << "target already built not for install";
        }
        else
          md.for_install = true;
      }
      else // install or uninstall
      {
        file* ls;
        if ((ls = t.is_a<libs> ()) || t.is_a<liba> ())
        {
          // Derive shared library paths and cache them in the target's aux
          // storage if we are un/installing (used in the *_extra() functions
          // below).
          //
          link_rule::libs_paths lsp;
          if (ls != nullptr && !ls->path ().empty ()) // Not binless.
          {
            // Note: we could omit deriving the paths if cur_options doesn't
            // have the buildtime option. But then we would have to duplicate
            // this code in reapply() below (where this bit could be added).
            // So let's keep it simple if a bit inefficient for now.
            //
            const string* p (cast_null<string> (t["bin.lib.prefix"]));
            const string* s (cast_null<string> (t["bin.lib.suffix"]));

            lsp = link_.derive_libs_paths (*ls,
                                           p != nullptr ? p->c_str (): nullptr,
                                           s != nullptr ? s->c_str (): nullptr);
          }

          return install_match_data {move (r), me.cur_options, move (lsp)};
        }
      }

      return r;
    }

    void install_rule::
    reapply (action a, target& t, match_extra& me) const
    {
      assert (a.operation () != update_id && !t.is_a<exe> ());

      text << "REMATCH " << t
           << ' ' << me.cur_options
           << ' ' << me.new_options; // @@ TMP

      me.cur_options |= me.new_options;

      // We also need to update options in install_match_data.
      //
      t.data<install_match_data> (a).options = me.cur_options;

      if ((me.new_options & lib::option_install_buildtime) != 0)
      {
        // If we are rematched with the buildtime option, propagate it to our
        // prerequisite libraries.
        //
        for (const target* pt: t.prerequisite_targets[a])
        {
          if (pt != nullptr && (pt->is_a<liba> ()  || pt->is_a<libs> () ||
                                pt->is_a<libua> () || pt->is_a<libus> ()))
          {
            // Go for all options instead of just install_buildtime to avoid
            // any further relocking/reapply (we only support runtime-only or
            // everything).
            //
            rematch_sync (a, *pt, match_extra::all_options);
          }
        }

        // Also match any additional prerequisites (e.g., headers).
        //
        file_rule::reapply_impl (a, t, me);
      }
    }

    bool install_rule::
    install_extra (const file& t, const install_dir& id) const
    {
      bool r (false);

      if (t.is_a<libs> ())
      {
        const auto& md (t.data<install_match_data> (perform_install_id));

        if ((md.options & lib::option_install_buildtime) != 0)
        {
          // Here we may have a bunch of symlinks that we need to install.
          //
          const scope& rs (t.root_scope ());
          const link_rule::libs_paths& lp (md.libs_paths);

          auto ln = [&t, &rs, &id] (const path& f, const path& l)
          {
            install_l (rs, id, l.leaf (), t, f.leaf (), 2 /* verbosity */);
            return true;
          };

          const path& lk (lp.link);
          const path& ld (lp.load);
          const path& so (lp.soname);
          const path& in (lp.interm);

          const path* f (lp.real);

          if (!in.empty ()) {r = ln (*f, in) || r; f = &in;}
          if (!so.empty ()) {r = ln (*f, so) || r; f = &so;}
          if (!ld.empty ()) {r = ln (*f, ld) || r; f = &ld;}
          if (!lk.empty ()) {r = ln (*f, lk) || r;         }
        }
      }

      return r;
    }

    bool install_rule::
    uninstall_extra (const file& t, const install_dir& id) const
    {
      bool r (false);

      if (t.is_a<libs> ())
      {
        const auto& md (t.data<install_match_data> (perform_uninstall_id));

        if ((md.options & lib::option_install_buildtime) != 0)
        {
          // Here we may have a bunch of symlinks that we need to uninstall.
          //
          const scope& rs (t.root_scope ());
          const link_rule::libs_paths& lp (md.libs_paths);

          auto rm = [&rs, &id] (const path& f, const path& l)
          {
            return uninstall_l (rs, id, l.leaf (), f.leaf (), 2 /* verbosity */);
          };

          const path& lk (lp.link);
          const path& ld (lp.load);
          const path& so (lp.soname);
          const path& in (lp.interm);

          const path* f (lp.real);

          if (!in.empty ()) {r = rm (*f, in) || r; f = &in;}
          if (!so.empty ()) {r = rm (*f, so) || r; f = &so;}
          if (!ld.empty ()) {r = rm (*f, ld) || r; f = &ld;}
          if (!lk.empty ()) {r = rm (*f, lk) || r;         }
        }
      }

      return r;
    }

    // libux_install_rule
    //
    libux_install_rule::
    libux_install_rule (data&& d, const link_rule& l)
        : common (move (d)), link_ (l) {}

    pair<const target*, uint64_t> libux_install_rule::
    filter (const scope* is,
            action a, const target& t, prerequisite_iterator& i,
            match_extra& me) const
    {
      using file_rule = install::file_rule;

      const prerequisite& p (i->prerequisite);

      uint64_t options (match_extra::all_options);

      otype ot (link_type (t).type);

      // The "see through" semantics that should be parallel to install_rule
      // above. In particular, here we use libue/libua/libus{} as proxies for
      // exe/liba/libs{} there.
      //

      // @@ TMP: drop eventually.
      //
#if 0
      bool st (t.is_a<libue> () || t.is_a<libus> ()); // Target needs shared.
      bool at (t.is_a<libua> () || t.is_a<libus> ()); // Target needs static.
      assert (st || at);

      if ((st && (p.is_a<libx> () || p.is_a<libs> ())) ||
          (at && (p.is_a<libx> () || p.is_a<liba> ())))
      {
        const target* pt (&search (t, p));

        if (const libx* l = pt->is_a<libx> ())
          pt = link_member (*l, a, link_info (t.base_scope (), ot));

        if ((st && pt->is_a<libs> ()) || (at && pt->is_a<liba> ()))
        {
          if (a.operation () != update_id)
          {
            if (t.is_a<libue> ())
              options = lib::option_install_runtime;
            else
            {
              if (me.cur_options == lib::option_install_runtime)
                options = lib::option_install_runtime;
            }
          }

          return make_pair (is == nullptr || pt->in (*is) ? pt : nullptr,
                            options);
        }

        if (pt->is_a<libux> ())
        {
          if (a.operation () != update_id && !pt->is_a<libue> ())
          {
            if (t.is_a<libue> ())
              options = lib::option_install_runtime;
            else
            {
              if (me.cur_options == lib::option_install_runtime)
                options = lib::option_install_runtime;
            }
          }

          return make_pair (pt, options);
        }
      }
#else
      if (p.is_a<libx> () || p.is_a<libs> () || p.is_a<liba> ())
      {
        const target* pt (&search (t, p));

        if (const libx* l = pt->is_a<libx> ())
          pt = link_member (*l, a, link_info (t.base_scope (), ot));

        if (a.operation () != update_id)
        {
          if (t.is_a<libue> ())
            options = lib::option_install_runtime;
          else
          {
            if (me.cur_options == lib::option_install_runtime)
              options = lib::option_install_runtime;
          }
        }

        if (pt->is_a<libs> () || pt->is_a<liba> ())
        {
          return make_pair (is == nullptr || pt->in (*is) ? pt : nullptr,
                            options);
        }
        else
          return make_pair (pt, options);
      }
#endif

      const target* pt (file_rule::instance.filter (is, a, t, p, me).first);
      if (pt == nullptr)
        return make_pair (pt, options);

      auto header_source = [this] (const auto& p)
      {
        return (x_header (p)   ||
                p.is_a (x_src) ||
                (x_mod != nullptr && p.is_a (*x_mod)) ||
                (x_asp != nullptr && p.is_a (*x_asp)) ||
                (x_obj != nullptr && p.is_a (*x_obj)));
      };

      if (t.is_a<libue> () ||
          (a.operation () != update_id &&
           me.cur_options == lib::option_install_runtime))
      {
        if (header_source (p))
          pt = nullptr;
        else if (p.type.see_through ())
        {
          for (i.enter_group (); i.group (); )
          {
            ++i;
            if (pt != nullptr && header_source (*i))
              pt = nullptr;
          }
        }

        if (pt == nullptr)
          return make_pair (pt, options);
      }

      bool g (false);
      if (p.is_a<bmi> () || (g = p.is_a (compile_types (ot).bmi)))
      {
        if (g)
          resolve_group (a, *pt);

        for (prerequisite_member pm:
               group_prerequisite_members (a, *pt, members_mode::maybe))
        {
          if (pm.is_a (*x_mod))
          {
            pt = t.is_a<libue> ()
              ? nullptr
              : file_rule::instance.filter (
                  is, a, *pt, pm.prerequisite, me).first;
            break;
          }
        }

        if (pt == nullptr)
          return make_pair (pt, options);
      }

      return make_pair (pt, options);
    }

    bool libux_install_rule::
    match (action a, target& t, const string&, match_extra& me) const
    {
      // We only want to handle installation if we are also the ones building
      // this target. So first run link's match().
      //
      return link_.sub_match (x_link, update_id, a, t, me) &&
        alias_rule::match (a, t);
    }

    recipe libux_install_rule::
    apply (action a, target& t, match_extra& me) const
    {
      if (a.operation () != update_id)
      {
        if (!t.is_a<libue> ())
        {
          if (me.new_options == 0)
            me.new_options = lib::option_install_runtime;

          me.cur_options = me.new_options;
        }
      }

      return alias_rule::apply (a, t, me);
    }

    void libux_install_rule::
    reapply (action a, target& t, match_extra& me) const
    {
      assert (a.operation () != update_id && !t.is_a<libue> ());

      text << "REMATCH " << t
           << ' ' << me.cur_options
           << ' ' << me.new_options; // @@ TMP

      // If we are rematched with the buildtime option, propagate it to our
      // prerequisite libraries.
      //
      // @@ Also libux?
      //
      if ((me.new_options & lib::option_install_buildtime) != 0)
      {
        for (const target* pt: t.prerequisite_targets[a])
        {
          if (pt != nullptr && (pt->is_a<liba> ()  || pt->is_a<libs> () ||
                                pt->is_a<libua> () || pt->is_a<libus> ()))
            rematch_sync (a, *pt, lib::option_install_buildtime);
        }
      }

      // @@ TODO: match additional prerequisites if required.

      me.cur_options |= me.new_options;
    }
  }
}