aboutsummaryrefslogtreecommitdiff
path: root/build2/cc/install.cxx
blob: 1280c73b97cba556c526a6fd7f4098f769b39231 (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
// 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;

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

    const target* file_install::
    filter (action a, const target& t, prerequisite_member p) const
    {
      // NOTE: see also alias_install::filter() below if changing anything
      // here.

      otype ot (link_type (t).type);

      // Don't install executable's prerequisite headers.
      //
      if (t.is_a<exe> () && x_header (p))
        return nullptr;

      // 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 several of them).
      //
      if (p.is_a<bmi> () || p.is_a (compile_types (ot).bmi))
      {
        const target& mt (p.search (t));

        for (prerequisite_member mp: group_prerequisite_members (a, mt))
        {
          if (mp.is_a (*x_mod))
            return t.is_a<exe> () ? nullptr : file_rule::filter (a, mt, mp);
        }
      }

      // If this is a shared library prerequisite, install it as long as it
      // is in the same amalgamation as we are.
      //
      // Less obvious: we also want to install a static library prerequisite
      // of a library (since it could be referenced from its .pc file, etc).
      //
      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.

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

        // If this is the lib{}/libu{} group, pick a member which we would
        // link. For libu{} we want to the "see through" logic.
        //
        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> ()))
          return pt->in (t.weak_scope ()) ? pt : nullptr;

        // See through libux{}. Note that we are always in the same project
        // (and thus amalgamation).
        //
        if (pt->is_a<libux> ())
          return pt;
      }

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

    match_result file_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 file_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")
        {
          const string* p (cast_null<string> (t["bin.lib.prefix"]));
          const string* s (cast_null<string> (t["bin.lib.suffix"]));
          t.data (
            link_.derive_libs_paths (*f,
                                     p != nullptr ? p->c_str (): nullptr,
                                     s != nullptr ? s->c_str (): nullptr));
        }
      }

      return r;
    }

    void file_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] (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 file_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] (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;
    }

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

    const target* alias_install::
    filter (action a, const target& t, prerequisite_member p) const
    {
      // The "see through" semantics that should be parallel to file_install
      // above. In particular, here we use libue/libua/libus{} as proxies for
      // exe/liba/libs{} there.

      otype ot (link_type (t).type);

      if (t.is_a<libue> () && x_header (p))
        return nullptr;

      if (p.is_a<bmi> () || p.is_a (compile_types (ot).bmi))
      {
        const target& mt (p.search (t));

        for (prerequisite_member mp: group_prerequisite_members (a, mt))
        {
          if (mp.is_a (*x_mod))
            return t.is_a<libue> () ? nullptr : alias_rule::filter (a, mt, mp);
        }
      }

      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.

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

        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> ()))
          return pt->in (t.weak_scope ()) ? pt : nullptr;

        if (pt->is_a<libux> ())
          return pt;
      }

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

    match_result alias_install::
    match (action a, target& t, const string& hint) const
    {
      // 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 ? alias_rule::match (a, t, "") : r;
    }
  }
}