aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/bin/init.cxx
blob: 7d1f17133bac06b4bf63d6195bfe3e4bc71f03f6 (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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
// file      : libbuild2/bin/init.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <libbuild2/bin/init.hxx>

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

#include <libbuild2/config/utility.hxx>

#include <libbuild2/test/module.hxx>

#include <libbuild2/install/rule.hxx>
#include <libbuild2/install/utility.hxx>

#include <libbuild2/bin/rule.hxx>
#include <libbuild2/bin/def-rule.hxx>
#include <libbuild2/bin/guess.hxx>
#include <libbuild2/bin/target.hxx>
#include <libbuild2/bin/utility.hxx>

using namespace std;
using namespace butl;

namespace build2
{
  namespace bin
  {
    static const obj_rule obj_;
    static const libul_rule libul_;
    static const lib_rule lib_;
    static const def_rule def_;

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

    bool
    vars_init (scope& rs,
               scope&,
               const location&,
               bool first,
               bool,
               module_init_extra&)
    {
      tracer trace ("bin::vars_init");
      l5 ([&]{trace << "for " << rs;});

      assert (first);

      // Enter variables.
      //
      // Target is a string and not target_triplet because it can be
      // specified by the user.
      //
      auto& vp (rs.var_pool ());

      vp.insert<string>    ("config.bin.target");
      vp.insert<string>    ("config.bin.pattern");

      // Library types to build.
      //
      vp.insert<string>    ("config.bin.lib");

      // Library types to use (in priority order).
      //
      vp.insert<strings>   ("config.bin.exe.lib");
      vp.insert<strings>   ("config.bin.liba.lib");
      vp.insert<strings>   ("config.bin.libs.lib");

      // The rpath[_link].auto flag controls automatic rpath behavior, for
      // example, addition of rpaths for prerequisite libraries (see the cc
      // module for an example). Default is true.
      //
      vp.insert<dir_paths> ("config.bin.rpath");
      vp.insert<bool>      ("config.bin.rpath.auto");

      vp.insert<dir_paths> ("config.bin.rpath_link");
      vp.insert<bool>      ("config.bin.rpath_link.auto");

      vp.insert<string>    ("config.bin.prefix");
      vp.insert<string>    ("config.bin.suffix");
      vp.insert<string>    ("config.bin.lib.prefix");
      vp.insert<string>    ("config.bin.lib.suffix");
      vp.insert<string>    ("config.bin.exe.prefix");
      vp.insert<string>    ("config.bin.exe.suffix");

      vp.insert<string>    ("bin.lib");

      vp.insert<strings>   ("bin.exe.lib");
      vp.insert<strings>   ("bin.liba.lib");
      vp.insert<strings>   ("bin.libs.lib");

      vp.insert<dir_paths> ("bin.rpath");
      vp.insert<bool>      ("bin.rpath.auto");

      vp.insert<dir_paths> ("bin.rpath_link");
      vp.insert<bool>      ("bin.rpath_link.auto");

      // Link whole archive. Note: with target visibility.
      //
      // The lookup semantics is as follows: we first look for a prerequisite-
      // specific value, then for a target-specific value in the library being
      // linked, and then for target type/pattern-specific value starting from
      // the scope of the target being linked-to. In that final lookup we do
      // not look in the target being linked-to itself since that is used to
      // indicate how this target should be linked to other targets. For
      // example:
      //
      // exe{test}: liba{foo}
      // liba{foo}: libua{foo1 foo2}
      // liba{foo}: bin.whole = false # Affects test but not foo1 and foo2.
      //
      // If unspecified, defaults to false for liba{} and to true for libu*{}.
      //
      vp.insert<bool> ("bin.whole", variable_visibility::target);

      // Mark library as binless.
      //
      // For example, the user can mark a C++ library consisting of only
      // module interfaces as binless so it becomes a modules equivalent to
      // header-only library (which we will call a module interface-only
      // library).
      //
      vp.insert<bool> ("bin.binless", variable_visibility::target);

      // Executable and library name prefixes and suffixes.
      //
      vp.insert<string> ("bin.exe.prefix");
      vp.insert<string> ("bin.exe.suffix");
      vp.insert<string> ("bin.lib.prefix");
      vp.insert<string> ("bin.lib.suffix");

      // The optional custom clean patterns should be just the pattern stem,
      // without the library prefix/name or extension. For example, `-[A-Z]`
      // instead of `libfoo-[A-Z].so`. Note that the custom version pattern is
      // only used for platform-independent versions (for platforms-specific
      // versions we can always derive the pattern automatically).
      //
      vp.insert<string> ("bin.lib.load_suffix");
      vp.insert<string> ("bin.lib.load_suffix_pattern");

      // @@ TMP: update bdep-new generated projects, documentation not to use
      //         @ for platform-independent version.
      //
      vp.insert<map<optional<string>, string>> ("bin.lib.version");
      vp.insert<string>                        ("bin.lib.version_pattern");

      return true;
    }

    void
    functions (function_map&); // functions.cxx

    bool
    config_init (scope& rs,
                 scope& bs,
                 const location& loc,
                 bool,
                 bool,
                 module_init_extra& extra)
    {
      tracer trace ("bin::config_init");
      l5 ([&]{trace << "for " << bs;});

      // We only support root loading (which means there can only be one).
      //
      if (rs != bs)
        fail (loc) << "bin.config module must be loaded in project root";

      context& ctx (rs.ctx);

      // Register the bin function family if this is the first instance of the
      // bin modules.
      //
      if (!function_family::defined (ctx.functions, "bin"))
        functions (ctx.functions);

      // Load bin.vars.
      //
      load_module (rs, rs, "bin.vars", loc);

      // Configuration.
      //
      using config::lookup_config;

      // Adjust module priority (binutils).
      //
      config::save_module (rs, "bin", 350);

      bool new_cfg (false); // Any new configuration values?

      // config.bin.target
      //
      const target_triplet* tgt (nullptr);
      {
        const variable& var (ctx.var_pool["config.bin.target"]);

        // We first see if the value was specified via the configuration
        // mechanism.
        //
        lookup l (lookup_config (new_cfg, rs, var));

        // Then see if there is a config hint (e.g., from the cc module).
        //
        bool hint (false);
        if (!l)
        {
          // Note: new_cfg is false for a hinted value.
          //
          if (auto hl = extra.hints[var])
          {
            l = hl;
            hint = true;
          }
        }

        if (!l)
          fail (loc) << "unable to determine binutils target" <<
            info << "consider specifying it with " << var <<
            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> (l));

        // 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 && config_sub)
        {
          s = run<string> (3,
                           *config_sub,
                           s.c_str (),
                           [] (string& l, bool) {return move (l);});
          l5 ([&]{trace << "config.sub target: '" << s << "'";});
        }

        try
        {
          target_triplet t (s);

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

          assert (!hint || s == t.representation ());

          // Also enter as bin.target.{cpu,vendor,system,version,class}
          // for convenience of access.
          //
          rs.assign<string> ("bin.target.cpu")     = t.cpu;
          rs.assign<string> ("bin.target.vendor")  = t.vendor;
          rs.assign<string> ("bin.target.system")  = t.system;
          rs.assign<string> ("bin.target.version") = t.version;
          rs.assign<string> ("bin.target.class")   = t.class_;

          tgt = &rs.assign<target_triplet> ("bin.target", move (t));
        }
        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 <<
            info << "consider using the --config-sub option";
        }
      }

      // config.bin.pattern
      //
      const string* pat (nullptr);
      {
        const variable& var (ctx.var_pool["config.bin.pattern"]);

        // We first see if the value was specified via the configuration
        // mechanism.
        //
        lookup l (lookup_config (new_cfg, rs, var));

        // Then see if there is a config hint (e.g., from the C++ module).
        //
        if (!l)
        {
          // Note: new_cfg is false for a hinted value.
          //
          if (auto hl = extra.hints[var])
            l = hl;
        }

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

          if (s.empty () ||
              (!path::traits_type::is_separator (s.back ()) &&
               s.find ('*') == string::npos))
          {
            fail << "missing '*' or trailing '"
                 << char (path::traits_type::directory_separator)
                 << "' in binutils pattern '" << s << "'";
          }

          pat = &rs.assign<string> ("bin.pattern", s);
        }
      }

      // 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.
      //

      // config.bin.lib
      //
      // By default it's both unless the target doesn't support one of the
      // variants.
      //
      {
        value& v (rs.assign ("bin.lib"));
        if (!v)
          v = *lookup_config (rs,
                              "config.bin.lib",
                              tgt->system == "emscripten" ? "static" :
                              "both");
      }

      // config.bin.exe.lib
      //
      {
        value& v (rs.assign ("bin.exe.lib"));
        if (!v)
          v = *lookup_config (rs, "config.bin.exe.lib", exe_lib);
      }

      // config.bin.liba.lib
      //
      {
        value& v (rs.assign ("bin.liba.lib"));
        if (!v)
          v = *lookup_config (rs, "config.bin.liba.lib", liba_lib);
      }

      // config.bin.libs.lib
      //
      {
        value& v (rs.assign ("bin.libs.lib"));
        if (!v)
          v = *lookup_config (rs, "config.bin.libs.lib", libs_lib);
      }

      // config.bin.rpath[_link]
      //
      // These ones are optional and we merge them into bin.rpath[_link], if
      // any.
      //
      rs.assign ("bin.rpath") += cast_null<dir_paths> (
        lookup_config (rs, "config.bin.rpath", nullptr));

      rs.assign ("bin.rpath_link") += cast_null<dir_paths> (
        lookup_config (rs, "config.bin.rpath_link", nullptr));

      // config.bin.rpath[_link].auto
      //
      {
        lookup l;

        rs.assign ("bin.rpath.auto") =
          (l = lookup_config (rs, "config.bin.rpath.auto"))
          ? cast<bool> (l)
          : true;

        rs.assign ("bin.rpath_link.auto") =
          (l = lookup_config (rs, "config.bin.rpath_link.auto"))
          ? cast<bool> (l)
          : true;
      }

      // config.bin.{lib,exe}.{prefix,suffix}
      //
      // These ones are not used very often so we will omit them from the
      // config.build if not specified. We also override any existing value
      // that might have been specified before loading the module.
      //
      {
        lookup p (lookup_config (rs, "config.bin.prefix"));
        lookup s (lookup_config (rs, "config.bin.suffix"));

        auto set = [&rs] (const char* bv, const char* cv, lookup l)
        {
          if (lookup o = lookup_config (rs, cv))
            l = o;

          if (l)
            rs.assign (bv) = *l;
        };

        set ("bin.lib.prefix", "config.bin.lib.prefix", p);
        set ("bin.lib.suffix", "config.bin.lib.suffix", s);

        set ("bin.exe.prefix", "config.bin.exe.prefix", p);
        set ("bin.exe.suffix", "config.bin.exe.suffix", s);
      }

      // If this is a configuration with new values, then print the report
      // at verbosity level 2 and up (-v).
      //
      if (verb >= (new_cfg ? 2 : 3))
      {
        diag_record dr (text);

        dr << "bin " << project (rs) << '@' << rs << '\n'
           << "  target     " << *tgt;

        if (pat != nullptr)
          dr << '\n'
             << "  pattern    " << *pat;
      }

      return true;
    }

    extern const char wasm_ext[] = "wasm"; // VC14 rejects constexpr.

    bool
    init (scope& rs,
          scope& bs,
          const location& loc,
          bool first,
          bool,
          module_init_extra& extra)
    {
      tracer trace ("bin::init");
      l5 ([&]{trace << "for " << bs;});

      // Load bin.config.
      //
      load_module (rs, rs, "bin.config", loc, extra.hints);

      // Cache some config values we will be needing below.
      //
      const target_triplet& tgt (cast<target_triplet> (rs["bin.target"]));

      // Register target types and configure their default "installability".
      //
      bool install_loaded (cast_false<bool> (rs["install.loaded"]));
      {
        using namespace install;

        if (first)
        {
          rs.insert_target_type<obj>  ();
          rs.insert_target_type<obje> ();
          rs.insert_target_type<obja> ();
          rs.insert_target_type<objs> ();

          rs.insert_target_type<bmi>  ();
          rs.insert_target_type<bmie> ();
          rs.insert_target_type<bmia> ();
          rs.insert_target_type<bmis> ();

          rs.insert_target_type<hbmi>  ();
          rs.insert_target_type<hbmie> ();
          rs.insert_target_type<hbmia> ();
          rs.insert_target_type<hbmis> ();

          rs.insert_target_type<libul> ();
          rs.insert_target_type<libue> ();
          rs.insert_target_type<libua> ();
          rs.insert_target_type<libus> ();

          rs.insert_target_type<lib>  ();
          rs.insert_target_type<liba> ();
          rs.insert_target_type<libs> ();

          // Register the def{} target type. Note that we do it here since it
          // is input and can be specified unconditionally (i.e., not only
          // when building for Windows).
          //
          rs.insert_target_type<def> ();
        }

        // Note: libu*{} members are not installable.
        //
        if (install_loaded)
        {
          install_path<liba> (bs, dir_path ("lib")); // Install in install.lib.
          install_mode<liba> (bs, "644");
        }

        // Should shared libraries have the 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:
        //
        // libs{foo}: install.mode=755
        //
        // Everyone is happy then? On Windows libs{} is the DLL and goes to
        // bin/, not lib/.
        //
        if (install_loaded)
          install_path<libs> (
            bs, dir_path (tgt.class_ == "windows" ? "bin" : "lib"));

        // Create additional target types for certain targets.
        //
        if (tgt.class_ == "windows")
        {
          // Import library.
          //
          if (first)
            rs.insert_target_type<libi> ();

          if (install_loaded)
          {
            install_path<libi> (bs, dir_path ("lib"));
            install_mode<libi> (bs, "644");
          }
        }

        if (tgt.cpu == "wasm32" || tgt.cpu == "wasm64")
        {
          const target_type& wasm (
            rs.derive_target_type(
              target_type {
                "wasm",
                &file::static_type,
                nullptr, /* factory */
                &target_extension_fix<wasm_ext>,
                nullptr, /* default_extension */
                &target_pattern_fix<wasm_ext>,
                &target_print_0_ext_verb, // Fixed extension, no use printing.
                &file_search,
                false /* see_through */}));

          if (install_loaded)
          {
            // Note that we keep the executable bit on the .wasm file, see
            // Emscripten issue 12707 for background.
            //
            install_path (bs, wasm, dir_path ("bin"));
          }
        }
      }

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

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

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

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

        r.insert<libul> (perform_update_id, "bin.libul", libul_);
        r.insert<libul> (perform_clean_id,  "bin.libul", libul_);

        // Similar to alias.
        //

        //@@ outer
        r.insert<lib> (perform_id,   0, "bin.lib", lib_);
        r.insert<lib> (configure_id, 0, "bin.lib", lib_);

        // Treat as a see through group for install, test, and dist.
        //
        if (install_loaded)
        {
          auto& gr (install::group_rule::instance);

          r.insert<lib> (perform_install_id,   "bin.lib", gr);
          r.insert<lib> (perform_uninstall_id, "bin.lib", gr);
        }

        if (const test::module* m = rs.find_module<test::module> ("test"))
        {
          r.insert<lib> (perform_test_id, "bin.lib", m->group_rule ());
        }

        if (rs.find_module ("dist"))
        {
          r.insert<lib> (dist_id, 0, "bin.lib", lib_);
        }
      }

      return true;
    }

    bool
    ar_config_init (scope& rs,
                    scope& bs,
                    const location& loc,
                    bool first,
                    bool,
                    module_init_extra& extra)
    {
      tracer trace ("bin::ar_config_init");
      l5 ([&]{trace << "for " << bs;});

      // Make sure bin.config is loaded.
      //
      load_module (rs, bs, "bin.config", loc, extra.hints);

      // Enter configuration variables.
      //
      if (first)
      {
        auto& vp (rs.var_pool ());

        vp.insert<path> ("config.bin.ar");
        vp.insert<path> ("config.bin.ranlib");
      }

      // Configuration.
      //
      if (first)
      {
        using config::lookup_config;

        bool new_cfg (false); // Any new configuration values?

        // 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.
        //

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

        // This can be either a pattern or search path(s).
        //
        pattern_paths pat (lookup_pattern (rs));

        // Don't save the default value to config.build so that if the user
        // changes, say, the C++ compiler (which hinted the pattern), then
        // ar will automatically change as well.
        //
        const path& ar (
          cast<path> (
            lookup_config (new_cfg,
                           rs,
                           "config.bin.ar",
                           path (apply_pattern (ar_d, pat.pattern)),
                           config::save_default_commented)));

        const path* ranlib (
          cast_null<path> (
            lookup_config (new_cfg,
                           rs,
                           "config.bin.ranlib",
                           nullptr,
                           config::save_default_commented)));

        const ar_info& ari (guess_ar (ar, ranlib, pat.paths));

        // If this is a configuration with new values, then print the report
        // at verbosity level 2 and up (-v).
        //
        if (verb >= (new_cfg ? 2 : 3))
        {
          diag_record dr (text);

          {
            dr << "bin.ar " << project (rs) << '@' << rs << '\n'
               << "  ar         " << ari.ar_path << '\n'
               << "  id         " << ari.ar_id << '\n'
               << "  version    " << ari.ar_version.string () << '\n'
               << "  major      " << ari.ar_version.major << '\n'
               << "  minor      " << ari.ar_version.minor << '\n'
               << "  patch      " << ari.ar_version.patch << '\n';
          }

          if (!ari.ar_version.build.empty ())
          {
            dr << "  build      " << ari.ar_version.build << '\n';
          }

          {
            dr << "  signature  " << ari.ar_signature << '\n'
               << "  checksum   " << ari.ar_checksum;
          }

          if (ranlib != nullptr)
          {
            dr << '\n'
               << "  ranlib     " << ari.ranlib_path << '\n'
               << "  id         " << ari.ranlib_id << '\n'
               << "  signature  " << ari.ranlib_signature << '\n'
               << "  checksum   " << ari.ranlib_checksum;
          }
        }

        rs.assign<process_path_ex> ("bin.ar.path")   = process_path_ex (
          ari.ar_path,
          "ar",
          ari.ar_checksum,
          hash_environment (ari.ar_environment));
        rs.assign<string>       ("bin.ar.id")        = ari.ar_id;
        rs.assign<string>       ("bin.ar.signature") = ari.ar_signature;
        rs.assign<string>       ("bin.ar.checksum")  = ari.ar_checksum;

        {
          const semantic_version& v (ari.ar_version);

          rs.assign<string>   ("bin.ar.version")       = v.string ();
          rs.assign<uint64_t> ("bin.ar.version.major") = v.major;
          rs.assign<uint64_t> ("bin.ar.version.minor") = v.minor;
          rs.assign<uint64_t> ("bin.ar.version.patch") = v.patch;
          rs.assign<string>   ("bin.ar.version.build") = v.build;
        }

        config::save_environment (rs, ari.ar_environment);

        if (ranlib != nullptr)
        {
          rs.assign<process_path_ex> ("bin.ranlib.path")   = process_path_ex (
            ari.ranlib_path,
            "ranlib",
            ari.ranlib_checksum,
            hash_environment (ari.ranlib_environment));
          rs.assign<string>       ("bin.ranlib.id")        = ari.ranlib_id;
          rs.assign<string>       ("bin.ranlib.signature") = ari.ranlib_signature;
          rs.assign<string>       ("bin.ranlib.checksum")  = ari.ranlib_checksum;

          config::save_environment (rs, ari.ranlib_environment);
        }
      }

      return true;
    }

    bool
    ar_init (scope& rs,
             scope& bs,
             const location& loc,
             bool,
             bool,
             module_init_extra& extra)
    {
      tracer trace ("bin::ar_init");
      l5 ([&]{trace << "for " << bs;});

      // Make sure the bin core and ar.config are loaded.
      //
      load_module (rs, bs, "bin",           loc, extra.hints);
      load_module (rs, bs, "bin.ar.config", loc, extra.hints);

      return true;
    }

    bool
    ld_config_init (scope& rs,
                    scope& bs,
                    const location& loc,
                    bool first,
                    bool,
                    module_init_extra& extra)
    {
      tracer trace ("bin::ld_config_init");
      l5 ([&]{trace << "for " << bs;});

      // Make sure bin.config is loaded.
      //
      load_module (rs, rs, "bin.config", loc, extra.hints);

      // Enter configuration variables.
      //
      if (first)
      {
        auto& vp (rs.var_pool ());

        vp.insert<path> ("config.bin.ld");
      }

      // Configuration.
      //
      if (first)
      {
        using config::lookup_config;

        bool new_cfg (false); // Any new configuration values?

        // config.bin.ld
        //
        // Use the target to decide on the default ld name.
        //
        const string& tsys (cast<string> (rs["bin.target.system"]));
        const char* ld_d (tsys == "win32-msvc" ? "link" : "ld");

        // This can be either a pattern or search path(s).
        //
        pattern_paths pat (lookup_pattern (rs));

        const path& ld (
          cast<path> (
            lookup_config (new_cfg,
                           rs,
                           "config.bin.ld",
                           path (apply_pattern (ld_d, pat.pattern)),
                           config::save_default_commented)));

        const ld_info& ldi (guess_ld (ld, pat.paths));

        // If this is a configuration with new values, then print the report
        // at verbosity level 2 and up (-v).
        //
        if (verb >= (new_cfg ? 2 : 3))
        {
          diag_record dr (text);

          {
            dr << "bin.ld " << project (rs) << '@' << rs << '\n'
               << "  ld         " << ldi.path << '\n'
               << "  id         " << ldi.id << '\n';
          }

          if (ldi.version)
          {
            dr << "  version    " << ldi.version->string () << '\n'
               << "  major      " << ldi.version->major << '\n'
               << "  minor      " << ldi.version->minor << '\n'
               << "  patch      " << ldi.version->patch << '\n';
          }

          if (ldi.version && !ldi.version->build.empty ())
          {
            dr << "  build      " << ldi.version->build << '\n';
          }

          dr << "  signature  " << ldi.signature << '\n'
             << "  checksum   " << ldi.checksum;
        }

        rs.assign<process_path_ex> ("bin.ld.path")   = process_path_ex (
          ldi.path,
          "ld",
          ldi.checksum,
          hash_environment (ldi.environment));
        rs.assign<string>       ("bin.ld.id")        = ldi.id;
        rs.assign<string>       ("bin.ld.signature") = ldi.signature;
        rs.assign<string>       ("bin.ld.checksum")  = ldi.checksum;

        if (ldi.version)
        {
          const semantic_version& v (*ldi.version);

          rs.assign<string>   ("bin.ld.version")       = v.string ();
          rs.assign<uint64_t> ("bin.ld.version.major") = v.major;
          rs.assign<uint64_t> ("bin.ld.version.minor") = v.minor;
          rs.assign<uint64_t> ("bin.ld.version.patch") = v.patch;
          rs.assign<string>   ("bin.ld.version.build") = v.build;
        }

        config::save_environment (rs, ldi.environment);
      }

      return true;
    }

    extern const char pdb_ext[] = "pdb"; // VC14 rejects constexpr.

    bool
    ld_init (scope& rs,
             scope& bs,
             const location& loc,
             bool,
             bool,
             module_init_extra& extra)
    {
      tracer trace ("bin::ld_init");
      l5 ([&]{trace << "for " << bs;});

      // Make sure the bin core and ld.config are loaded.
      //
      load_module (rs, bs, "bin",           loc, extra.hints);
      load_module (rs, bs, "bin.ld.config", loc, extra.hints);

      const string& lid (cast<string> (rs["bin.ld.id"]));

      // Register the pdb{} target if using the VC toolchain.
      //
      using namespace install;

      if (lid == "msvc")
      {
        const target_type& pdb (
          rs.derive_target_type(
            target_type {
              "pdb",
              &file::static_type,
              nullptr, /* factory */
              &target_extension_fix<pdb_ext>,
              nullptr, /* default_extension */
              &target_pattern_fix<pdb_ext>,
              &target_print_0_ext_verb, // Fixed extension, no use printing.
              &file_search,
              false /* see_through */}));

        if (cast_false<bool> (rs["install.loaded"]))
        {
          install_path (bs, pdb, dir_path ("bin")); // Goes to install.bin
          install_mode (bs, pdb, "644");            // But not executable.
        }
      }

      // Register .def file rule.
      //
      if (lid == "msvc" || lid == "msvc-lld")
      {
        // If we are using link.exe, then we can access dumpbin via the
        // link.exe /DUMP option. But for lld-link we need llvm-nm.
        //
        if (lid == "msvc-lld")
          load_module (rs, bs, "bin.nm.config", loc, extra.hints);

        bs.insert_rule<def> (perform_update_id,   "bin.def", def_);
        bs.insert_rule<def> (perform_clean_id,    "bin.def", def_);
        bs.insert_rule<def> (configure_update_id, "bin.def", def_);
      }

      return true;
    }

    bool
    rc_config_init (scope& rs,
                    scope& bs,
                    const location& loc,
                    bool first,
                    bool,
                    module_init_extra& extra)
    {
      tracer trace ("bin::rc_config_init");
      l5 ([&]{trace << "for " << bs;});

      // Make sure bin.config is loaded.
      //
      load_module (rs, bs, "bin.config", loc, extra.hints);

      // Enter configuration variables.
      //
      if (first)
      {
        auto& vp (rs.var_pool ());

        vp.insert<path> ("config.bin.rc");
      }

      // Configuration.
      //
      if (first)
      {
        using config::lookup_config;

        bool new_cfg (false); // Any new configuration values?

        // config.bin.rc
        //
        // Use the target to decide on the default rc name.
        //
        const string& tsys (cast<string> (rs["bin.target.system"]));
        const char* rc_d (tsys == "win32-msvc" ? "rc" : "windres");

        // This can be either a pattern or search path(s).
        //
        pattern_paths pat (lookup_pattern (rs));

        const path& rc (
          cast<path> (
            lookup_config (new_cfg,
                           rs,
                           "config.bin.rc",
                           path (apply_pattern (rc_d, pat.pattern)),
                           config::save_default_commented)));

        const rc_info& rci (guess_rc (rc, pat.paths));

        // If this is a configuration with new values, then print the report
        // at verbosity level 2 and up (-v).
        //
        if (verb >= (new_cfg ? 2 : 3))
        {
          text << "bin.rc " << project (rs) << '@' << rs << '\n'
               << "  rc         " << rci.path << '\n'
               << "  id         " << rci.id << '\n'
               << "  signature  " << rci.signature << '\n'
               << "  checksum   " << rci.checksum;
        }

        rs.assign<process_path_ex> ("bin.rc.path")   = process_path_ex (
          rci.path,
          "rc",
          rci.checksum,
          hash_environment (rci.environment));
        rs.assign<string>       ("bin.rc.id")        = rci.id;
        rs.assign<string>       ("bin.rc.signature") = rci.signature;
        rs.assign<string>       ("bin.rc.checksum")  = rci.checksum;

        config::save_environment (rs, rci.environment);
      }

      return true;
    }

    bool
    rc_init (scope& rs,
             scope& bs,
             const location& loc,
             bool,
             bool,
             module_init_extra& extra)
    {
      tracer trace ("bin::rc_init");
      l5 ([&]{trace << "for " << bs;});

      // Make sure the bin core and rc.config are loaded.
      //
      load_module (rs, bs, "bin",           loc, extra.hints);
      load_module (rs, bs, "bin.rc.config", loc, extra.hints);

      return true;
    }

    bool
    nm_config_init (scope& rs,
                    scope& bs,
                    const location& loc,
                    bool first,
                    bool,
                    module_init_extra& extra)
    {
      tracer trace ("bin::nm_config_init");
      l5 ([&]{trace << "for " << bs;});

      // Make sure bin.config is loaded.
      //
      load_module (rs, bs, "bin.config", loc, extra.hints);

      // Enter configuration variables.
      //
      if (first)
      {
        auto& vp (rs.var_pool ());

        vp.insert<path> ("config.bin.nm");
      }

      // Configuration.
      //
      if (first)
      {
        using config::lookup_config;

        bool new_cfg (false); // Any new configuration values?

        // config.bin.nm
        //
        // Use the target to decide on the default nm name. Note that in case
        // of win32-msvc this is insufficient and we fallback to the linker
        // type (if available) to decide between dumpbin and llvm-nm.
        //
        // Finally note that the dumpbin.exe functionality is available via
        // link.exe /DUMP.
        //
        const string& tsys (cast<string> (rs["bin.target.system"]));
        const char* nm_d (tsys == "win32-msvc"
                          ? (cast_empty<string> (rs["bin.ld.id"]) == "msvc-lld"
                             ? "llvm-nm"
                             : "dumpbin")
                          : "nm");

        // This can be either a pattern or search path(s).
        //
        pattern_paths pat (lookup_pattern (rs));

        const path& nm (
          cast<path> (
            lookup_config (new_cfg,
                           rs,
                           "config.bin.nm",
                           path (apply_pattern (nm_d, pat.pattern)),
                           config::save_default_commented)));

        const nm_info& nmi (guess_nm (nm, pat.paths));

        // If this is a configuration with new values, then print the report
        // at verbosity level 2 and up (-v).
        //
        if (verb >= (new_cfg ? 2 : 3))
        {
          text << "bin.nm " << project (rs) << '@' << rs << '\n'
               << "  nm         " << nmi.path << '\n'
               << "  id         " << nmi.id << '\n'
               << "  signature  " << nmi.signature << '\n'
               << "  checksum   " << nmi.checksum;
        }

        rs.assign<process_path_ex> ("bin.nm.path")   = process_path_ex (
          nmi.path,
          "nm",
          nmi.checksum,
          hash_environment (nmi.environment));
        rs.assign<string>       ("bin.nm.id")        = nmi.id;
        rs.assign<string>       ("bin.nm.signature") = nmi.signature;
        rs.assign<string>       ("bin.nm.checksum")  = nmi.checksum;

        config::save_environment (rs, nmi.environment);
      }

      return true;
    }

    bool
    nm_init (scope& rs,
             scope& bs,
             const location& loc,
             bool,
             bool,
             module_init_extra& extra)
    {
      tracer trace ("bin::nm_init");
      l5 ([&]{trace << "for " << bs;});

      // Make sure the bin core and nm.config are loaded.
      //
      load_module (rs, bs, "bin",           loc, extra.hints);
      load_module (rs, bs, "bin.nm.config", loc, extra.hints);

      return true;
    }

    static const module_functions mod_functions[] =
    {
      // NOTE: don't forget to also update the documentation in init.hxx if
      //       changing anything here.

      {"bin.vars",      nullptr, vars_init},
      {"bin.config",    nullptr, config_init},
      {"bin",           nullptr, init},
      {"bin.ar.config", nullptr, ar_config_init},
      {"bin.ar",        nullptr, ar_init},
      {"bin.ld.config", nullptr, ld_config_init},
      {"bin.ld",        nullptr, ld_init},
      {"bin.rc.config", nullptr, rc_config_init},
      {"bin.rc",        nullptr, rc_init},
      {"bin.nm.config", nullptr, nm_config_init},
      {"bin.nm",        nullptr, nm_init},
      {nullptr,         nullptr, nullptr}
    };

    const module_functions*
    build2_bin_load ()
    {
      return mod_functions;
    }
  }
}