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

#include <build2/cc/install.hxx>

#include <build2/algorithm.hxx>

#include <build2/bin/target.hxx>

#include <build2/cc/link.hxx>    // match()
#include <build2/cc/utility.hxx>

using namespace std;

namespace build2
{
  namespace cc
  {
    using namespace bin;

    install::
    install (data&& d, const link& l): common (move (d)), link_ (l) {}

    const target* install::
    filter (action a, const target& t, prerequisite_member p) const
    {
      if (t.is_a<exe> ())
      {
        // Don't install executable's prerequisite headers.
        //
        if (x_header (p))
          return nullptr;
      }

      // If this is a shared library prerequisite, install it as long as it
      // is in the same amalgamation as we are.
      //
      // @@ Shouldn't we also install a static library prerequisite of a
      //    static library?
      //
      if ((t.is_a<exe> () || t.is_a<libs> ()) &&
          (p.is_a<lib> () || p.is_a<libs> ()))
      {
        const target* pt (&p.search (t));

        // If this is the lib{} group, pick a member which we would link.
        //
        if (const lib* l = pt->is_a<lib> ())
          pt = &link_member (
            *l, a, link_order (t.base_scope (), link_type (t)));

        if (pt->is_a<libs> ()) // Can be liba{}.
          return pt->in (t.weak_scope ()) ? pt : nullptr;
      }

      return file_rule::filter (a, t, p);
    }

    match_result install::
    match (action a, target& t, const string& hint) const
    {
      // @@ How do we split the hint between the two?
      //

      // We only want to handle installation if we are also the ones building
      // this target. So first run link's match().
      //
      match_result r (link_.match (a, t, hint));
      return r ? file_rule::match (a, t, "") : r;
    }

    recipe install::
    apply (action a, target& t) const
    {
      recipe r (file_rule::apply (a, t));

      // Derive shared library paths and cache them in the target's aux
      // storage if we are (un)installing (used in *_extra() functions below).
      //
      if (a.operation () == install_id || a.operation () == uninstall_id)
      {
        file* f;
        if ((f = t.is_a<libs> ()) != nullptr && tclass != "windows")
          t.data (link_.derive_libs_paths (*f));
      }

      return r;
    }

    void install::
    install_extra (const file& t, const install_dir& id) const
    {
      if (t.is_a<libs> () && tclass != "windows")
      {
        // Here we may have a bunch of symlinks that we need to install.
        //
        auto& lp (t.data<link::libs_paths> ());

        auto ln = [&id, this] (const path& f, const path& l)
        {
          install_l (id, f.leaf (), l.leaf (), false);
        };

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

        const path* f (&lp.real);

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

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

      if (t.is_a<libs> () && tclass != "windows")
      {
        // Here we may have a bunch of symlinks that we need to uninstall.
        //
        auto& lp (t.data<link::libs_paths> ());

        auto rm = [&id, this] (const path& l)
        {
          return uninstall_f (id, nullptr, l.leaf (), false);
        };

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

        if (!lk.empty ()) r = rm (lk) || r;
        if (!so.empty ()) r = rm (so) || r;
        if (!in.empty ()) r = rm (in) || r;
      }

      return r;
    }
  }
}