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

#include <build2/bin/module>

#include <butl/triplet>

#include <build2/scope>
#include <build2/variable>
#include <build2/diagnostics>

#include <build2/config/utility>
#include <build2/install/utility>

#include <build2/bin/rule>
#include <build2/bin/guess>
#include <build2/bin/target>

using namespace std;
using namespace butl;

namespace build2
{
  namespace bin
  {
    static obj_rule obj_;
    static lib_rule lib_;

    // Default config.bin.*.lib values.
    //
    static const strings exe_lib {"shared", "static"};
    static const strings liba_lib {"static"};
    static const strings libso_lib {"shared"};

    // Apply the specified stem to the config.bin.pattern. If there is no
    // pattern, then return the stem itself. Assume the pattern is valid,
    // i.e., contains single '*'.
    //
    static string
    apply (const lookup& pattern, const char* stem)
    {
      if (!pattern)
        return stem;

      const string& p (cast<string> (pattern));
      size_t i (p.find ('*'));
      assert (i != string::npos);

      string r (p, 0, i++);
      r.append (stem);
      r.append (p, i, p.size () - i);
      return r;
    }

    bool
    init (scope& r,
          scope& b,
          const location& loc,
          unique_ptr<module_base>&,
          bool first,
          bool,
          const variable_map& config_hints)
    {
      tracer trace ("bin::init");
      l5 ([&]{trace << "for " << b.out_path ();});

      // Enter module variables.
      //
      if (first)
      {
        auto& v (var_pool);

        // Note: some overridable, some not.
        //
        v.insert<string>    ("config.bin.target",    true);
        v.insert<string>    ("config.bin.pattern",   true);

        v.insert<path>      ("config.bin.ar",        true);
        v.insert<path>      ("config.bin.ranlib",    true);

        v.insert<string>    ("config.bin.lib",       true);
        v.insert<strings>   ("config.bin.exe.lib",   true);
        v.insert<strings>   ("config.bin.liba.lib",  true);
        v.insert<strings>   ("config.bin.libso.lib", true);
        v.insert<dir_paths> ("config.bin.rpath",     true);

        v.insert<string>    ("bin.lib");
        v.insert<strings>   ("bin.exe.lib");
        v.insert<strings>   ("bin.liba.lib");
        v.insert<strings>   ("bin.libso.lib");
        v.insert<dir_paths> ("bin.rpath");

        v.insert<string>    ("bin.libprefix",        true);
      }

      // Configure.
      //
      using config::required;
      using config::optional;

      // The idea here is as follows: if we already have one of
      // the bin.* variables set, then we assume this is static
      // project configuration and don't bother setting the
      // corresponding config.bin.* variable.
      //
      //@@ Need to validate the values. Would be more efficient
      //   to do it once on assignment than every time on query.
      //   Custom var type?
      //

      // config.bin.lib
      //
      {
        value& v (b.assign ("bin.lib"));
        if (!v)
          v = required (r, "config.bin.lib", "both").first;
      }

      // config.bin.exe.lib
      //
      {
        value& v (b.assign ("bin.exe.lib"));
        if (!v)
          v = required (r, "config.bin.exe.lib", exe_lib).first;
      }

      // config.bin.liba.lib
      //
      {
        value& v (b.assign ("bin.liba.lib"));
        if (!v)
          v = required (r, "config.bin.liba.lib", liba_lib).first;
      }

      // config.bin.libso.lib
      //
      {
        value& v (b.assign ("bin.libso.lib"));
        if (!v)
          v = required (r, "config.bin.libso.lib", libso_lib).first;
      }

      // config.bin.rpath
      //
      // This one is optional and we merge it into bin.rpath, if any.
      // See the cxx module for details on merging.
      //
      b.assign ("bin.rpath") += cast_null<dir_paths> (
        optional (r, "config.bin.rpath"));

      if (first)
      {
        bool new_val (false); // Set any new values?

        // config.bin.target
        //
        {
          const variable& var (var_pool.find ("config.bin.target"));

          // We first see if the value was specified via the configuration
          // mechanism.
          //
          auto p (required (r, var));
          const value* v (p.first);

          // Then see if there is a config hint (e.g., from the C++ module).
          //
          bool hint (false);
          if (v == nullptr)
          {
            if (auto l = config_hints[var])
            {
              v = l.value;
              hint = true;
            }
          }

          if (v == nullptr)
            fail (loc) << "unable to determine binutils target" <<
              info << "consider specifying it with " << var.name <<
              info << "or first load a module that can provide it as a hint, "
                       << "such as c or cxx";

          // Split/canonicalize the target.
          //
          string s (cast<string> (*v));

          // Did the user ask us to use config.sub? If this is a hinted value,
          // then we assume it has already been passed through config.sub.
          //
          if (!hint && ops.config_sub_specified ())
          {
            s = run<string> (ops.config_sub (),
                             s.c_str (),
                             [] (string& l) {return move (l);});
            l5 ([&]{trace << "config.sub target: '" << s << "'";});
          }

          try
          {
            string canon;
            triplet t (s, canon);

            l5 ([&]{trace << "canonical target: '" << canon << "'; "
                          << "class: " << t.class_;});

            assert (!hint || s == canon);

            // Enter as bin.target.{cpu,vendor,system,version,class}.
            //
            r.assign<string> ("bin.target") = move (canon);
            r.assign<string> ("bin.target.cpu") = move (t.cpu);
            r.assign<string> ("bin.target.vendor") = move (t.vendor);
            r.assign<string> ("bin.target.system") = move (t.system);
            r.assign<string> ("bin.target.version") = move (t.version);
            r.assign<string> ("bin.target.class") = move (t.class_);
          }
          catch (const invalid_argument& e)
          {
            // This is where we suggest that the user specifies --config-sub
            // to help us out.
            //
            fail << "unable to parse binutils target '" << s << "': "
                 << e.what () <<
              info << "consider using the --config-sub option";
          }

          new_val = new_val || p.second; // False for a hinted value.
        }

        // config.bin.pattern
        //
        {
          const variable& var (var_pool.find ("config.bin.pattern"));

          // We first see if the value was specified via the configuration
          // mechanism.
          //
          auto p (required (r, var));
          const value* v (p.first);

          // Then see if there is a config hint (e.g., from the C++ module).
          //
          if (v == nullptr)
          {
            if (auto l = config_hints[var])
              v = l.value;
          }

          // For ease of use enter it as bin.pattern (it can come from
          // different places).
          //
          if (v != nullptr)
          {
            const string& s (cast<string> (*v));

            if (s.find ('*') == string::npos)
              fail << "missing '*' in binutils pattern '" << s << "'";

            r.assign<string> ("bin.pattern") = s;
            new_val = new_val || p.second; // False for a hinted value.
          }
        }

        // If we set any new values (e.g., we are configuring), then print the
        // report at verbosity level 2 and up (-v).
        //
        if (verb >= (new_val ? 2 : 3))
        {
          diag_record dr (text);

          dr << "bin\n"
             << "  target     " << cast<string> (r["bin.target"]);

          if (auto l = r["bin.pattern"])
            dr << '\n'
               << "  pattern    " << cast<string> (l);
        }

        // config.bin.ar
        // config.bin.ranlib
        //
        // For config.bin.ar we have the default (plus the pattern) while
        // ranlib should be explicitly specified by the user in order for us
        // to use it (all targets that we currently care to support have the
        // ar -s option but if that changes we can always force the use of
        // ranlib for certain targets).
        //
        // Another idea is to refuse to use default 'ar' (without the pattern)
        // if the host/build targets don't match. On the other hand, a cross-
        // toolchain can be target-unprefixed. Also, without canonicalization,
        // comparing targets will be unreliable.
        //
        auto pattern (r["bin.pattern"]);

        // Use the target to decide on the default binutils program names.
        //
        const string& tsys (cast<string> (r["bin.target.system"]));
        const char* ar_d (tsys == "win32-msvc" ? "lib" : "ar");

        auto p (required (r, "config.bin.ar", path (apply (pattern, ar_d))));
        auto& v (optional (r, "config.bin.ranlib"));

        const path& ar (cast<path> (p.first));
        const path& ranlib (v ? cast<path> (v) : path ());

        bin_info bi (guess (ar, ranlib));

        // If this is a new value (e.g., we are configuring), then print the
        // report at verbosity level 2 and up (-v).
        //
        if (verb >= (p.second ? 2 : 3))
        {
          //@@ Print project out root or name? See cxx.

          text << "bin.ar\n"
               << "  exe        " << ar << '\n'
               << "  id         " << bi.ar_id << '\n'
               << "  signature  " << bi.ar_signature << '\n'
               << "  checksum   " << bi.ar_checksum;

          if (!ranlib.empty ())
          {
            text << "bin.ranlib\n"
                 << "  exe        " << ranlib << '\n'
                 << "  id         " << bi.ranlib_id << '\n'
                 << "  signature  " << bi.ranlib_signature << '\n'
                 << "  checksum   " << bi.ranlib_checksum;
          }
        }

        r.assign<string> ("bin.ar.id") = move (bi.ar_id);
        r.assign<string> ("bin.ar.signature") = move (bi.ar_signature);
        r.assign<string> ("bin.ar.checksum") = move (bi.ar_checksum);

        if (!ranlib.empty ())
        {
          r.assign<string> ("bin.ranlib.id") = move (bi.ranlib_id);
          r.assign<string> ("bin.ranlib.signature") =
            move (bi.ranlib_signature);
          r.assign<string> ("bin.ranlib.checksum") = move (bi.ranlib_checksum);
        }
      }

      // Cache some config values we will be needing below.
      //
      const string& tclass (cast<string> (r["bin.target.class"]));

      // Register target types.
      //
      {
        auto& t (b.target_types);

        t.insert<obja>  ();
        t.insert<objso> ();
        t.insert<obj>   ();
        t.insert<exe>   ();
        t.insert<liba>  ();
        t.insert<libso> ();
        t.insert<lib>   ();
      }

      // Register rules.
      //
      {
        auto& r (b.rules);

        r.insert<obj> (perform_update_id, "bin.obj", obj_);
        r.insert<obj> (perform_clean_id, "bin.obj", obj_);

        r.insert<lib> (perform_update_id, "bin.lib", lib_);
        r.insert<lib> (perform_clean_id, "bin.lib", lib_);

        // Configure member.
        //
        r.insert<lib> (configure_update_id, "bin.lib", lib_);

        //@@ Should we check if the install module was loaded
        //   (by checking if install operation is registered
        //   for this project)? If we do that, then install
        //   will have to be loaded before bin. Perhaps we
        //   should enforce loading of all operation-defining
        //   modules before all others?
        //
        r.insert<lib> (perform_install_id, "bin.lib", lib_);
      }

      // Configure "installability" of our target types.
      //
      using namespace install;

      install_path<exe> (b, dir_path ("bin"));  // Install into install.bin.

      // Should shared libraries have executable bit? That depends on
      // who you ask. In Debian, for example, it should not unless, it
      // really is executable (i.e., has main()). On the other hand, on
      // some systems, this may be required in order for the dynamic
      // linker to be able to load the library. So, by default, we will
      // keep it executable, especially seeing that this is also the
      // behavior of autotools. At the same time, it is easy to override
      // this, for example:
      //
      // config.install.lib.mode=644
      //
      // And a library that wants to override any such overrides (e.g.,
      // because it does have main()) can do:
      //
      // libso{foo}: install.mode=755
      //
      // Everyone is happy then? Not Windows users. When targeting Windows
      // libso{} is an import library and shouldn't be exec.
      //
      install_path<libso> (b, dir_path ("lib")); // Install into install.lib.

      if (tclass == "windows")
        install_mode<libso> (b, "644");

      install_path<liba> (b, dir_path ("lib"));  // Install into install.lib.
      install_mode<liba> (b, "644");

      return true;
    }
  }
}