libgpr2_24.0.0_eda3c693/tools/src/gprconfig.adb

   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
------------------------------------------------------------------------------
--                                                                          --
--                           GPR2 PROJECT MANAGER                           --
--                                                                          --
--                     Copyright (C) 2021-2023, AdaCore                     --
--                                                                          --
-- This is  free  software;  you can redistribute it and/or modify it under --
-- terms of the  GNU  General Public License as published by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  This software is distributed in the hope  that it will be useful, --
-- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public --
-- License for more details.  You should have received  a copy of the  GNU  --
-- General Public License distributed with GNAT; see file  COPYING. If not, --
-- see <http://www.gnu.org/licenses/>.                                      --
--                                                                          --
------------------------------------------------------------------------------

with Ada.Command_Line;
with Ada.Containers.Indefinite_Ordered_Maps;
with Ada.Characters.Handling;
with Ada.Directories;
with Ada.Integer_Text_IO;
with Ada.IO_Exceptions;
with Ada.Exceptions;
with Ada.Text_IO;
with Ada.Strings.Fixed;
with Ada.Strings.Unbounded;

with GNAT.Command_Line;
with GNAT.Directory_Operations;
with GNAT.OS_Lib;
with GNAT.String_Split;

with GNATCOLL.Traces;

with GPR2.Containers;
with GPR2.KB;
with GPR2.Log;
with GPR2.Message;
with GPR2.Path_Name.Set;
with GPR2.Project.Configuration;
with GPR2.Version;

with GPRtools.Util;

procedure GPRconfig is

   use Ada;
   use Ada.Containers;
   use Ada.Strings.Unbounded;

   use GNAT.Command_Line;

   use GPR2;
   use GPR2.KB;
   use GPR2.Project.Configuration;

   Knowledge_Base : KB.Object;

   type Verbosity_Kind is (Quiet, Default, Verbose);

   Opt_Verbosity    : Verbosity_Kind := Default;
   Opt_Version      : aliased Boolean;
   Opt_Target       : aliased GNAT.OS_Lib.String_Access;
   Opt_Show_Targets : aliased Boolean;
   Opt_Show_MI      : aliased Boolean;
   Opt_Show_Known   : aliased Boolean;
   Opt_Batch        : aliased Boolean;
   Opt_O            : aliased GNAT.OS_Lib.String_Access;
   Opt_DB           : Boolean := False;
   Opt_Validate     : aliased Boolean;
   Opt_Fallback     : aliased Boolean;

   KB_Flags         : Parsing_Flags := Default_Flags;

   Cmd_Config       : Command_Line_Configuration;

   KB_Locations     : GPR2.Path_Name.Set.Object;

   function "=" (L, R : Description) return Boolean is
     (Language (L) = Language (R));
   --  Compares descriptions. Only one description per language is expected

   package Description_Maps is new Ada.Containers.Indefinite_Ordered_Maps
     (Language_Id, Description);

   Description_Map : Description_Maps.Map;

   procedure Register_Cmd_Options;
   --  Registers accepted gprconfig switches

   procedure Value_Callback (Switch, Value : String);
   --  Parses command line switches

   procedure Report_Error_And_Exit (Msg : String);
   --  Outputs error message and raises Exit_From_Command_Line

   function Parse_Config_Parameter (Config : String) return Description;
   --  Parses the value of --config switch into Decription

   function Get_Settings (Map : Description_Maps.Map) return Description_Set;
   --  Turns the map of processed config parameters into Description_Set

   procedure Display_Compilers_For_Parser
     (Base       : KB.Object;
      Compilers  : in out Compiler_Array;
      For_Target : Name_Type);
   --  Display the list of found compilers for use by an external parser

   procedure Select_Compilers_Interactively
     (Base       : in out KB.Object;
      Compilers  : in out Compiler_Array;
      For_Target : Name_Type);
   --  Asks the user for compilers to be selected

   procedure Show_Command_Line_Config
     (Compilers : Compiler_Array; Target : String);
   --  Displays the batch command line that would have the same effect as the
   --  current selection of compilers.

   ----------------------------------
   -- Display_Compilers_For_Parser --
   ----------------------------------

   procedure Display_Compilers_For_Parser
     (Base       : KB.Object;
      Compilers  : in out Compiler_Array;
      For_Target : Name_Type)
   is
      Comp   : Compiler;

      function "&" (L : String; R : Optional_Name_Type) return String is
        (L & String (R));

      procedure Put_Rank (Comp : Compiler; Idx : Positive);
      --  Outputs prefix with rank and selection

      --------------
      -- Put_Rank --
      --------------

      procedure Put_Rank (Comp : Compiler; Idx : Positive) is
      begin
         if Is_Selected (Comp) then
            Text_IO.Put ("*");
            Integer_Text_IO.Put (Idx, Width => 3);
         else
            Integer_Text_IO.Put (Idx, Width => 4);
         end if;
      end Put_Rank;

   begin
      Base.Filter_Compilers_List (Compilers, For_Target);

      for Idx in Compilers'Range loop

         Comp := Compilers (Idx);

         if Is_Selectable (Comp) and then Requires_Compiler (Comp) then
            Put_Rank (Comp, Idx);
            Text_IO.Put_Line (" target:" & Target (Comp));
            Put_Rank (Comp, Idx);
            Text_IO.Put_Line
              (" normalized_target:" & Base.Normalized_Target (Target (Comp)));
            Put_Rank (Comp, Idx);
            Text_IO.Put_Line (" executable:" & Executable (Comp));
            Put_Rank (Comp, Idx);
            Text_IO.Put_Line (" path:" & Path (Comp));
            Put_Rank (Comp, Idx);
            Text_IO.Put_Line (" lang:" & Image (Language (Comp)));
            Put_Rank (Comp, Idx);
            Text_IO.Put_Line (" name:" & Name (Comp));
            Put_Rank (Comp, Idx);
            Text_IO.Put_Line (" version:" & KB.Version (Comp));
            Put_Rank (Comp, Idx);
            Text_IO.Put_Line (" runtime:" & Runtime (Comp));
            Put_Rank (Comp, Idx);
            Text_IO.Put_Line
              (" native:"
               & Boolean'Image
                 (Base.Normalized_Target (Default_Target) =
                      Base.Normalized_Target (Target (Comp))));

         elsif Is_Selectable (Comp) then

            Put_Rank (Comp, Idx);
            Text_IO.Put_Line (" target:");
            Put_Rank (Comp, Idx);
            Text_IO.Put_Line
              (" normalized_target:unknown");
            Put_Rank (Comp, Idx);
            Text_IO.Put_Line (" executable:");
            Put_Rank (Comp, Idx);
            Text_IO.Put_Line (" path:");
            Put_Rank (Comp, Idx);
            Text_IO.Put_Line (" lang:" & String (Name (Language (Comp))));
            Put_Rank (Comp, Idx);
            Text_IO.Put_Line (" name:");
            Put_Rank (Comp, Idx);
            Text_IO.Put_Line (" version:");
            Put_Rank (Comp, Idx);
            Text_IO.Put_Line (" runtime:");
            Put_Rank (Comp, Idx);
            Text_IO.Put_Line (" native:FALSE");
         end if;

      end loop;
   end Display_Compilers_For_Parser;

   ------------------
   -- Get_Settings --
   ------------------

   function Get_Settings (Map : Description_Maps.Map) return Description_Set is
      Result : Description_Set (1 .. Integer (Map.Length));
      Idx    : Positive := 1;
   begin
      for Descr of Map loop
         Result (Idx) := Descr;
         Idx := Idx + 1;
      end loop;

      return Result;
   end Get_Settings;

   ----------------------------
   -- Parse_Config_Parameter --
   ----------------------------

   function Parse_Config_Parameter (Config : String) return Description is
      use Ada.Characters.Handling;
      use GNAT.String_Split;

      function Positional_Parameters return Boolean;
      --  Returns True if configuration parameters are given in a positional
      --  form,  i.e. --config=language:ada,runtime:sjlj.
      --  Also checks that the two modes are not mixed up,
      --  reports error otherwise.

      procedure Check_Positional;
      --  Checks that positional prefixes are not duplicated

      function Get_Description_Param
        (Slices : Slice_Set; Pos : Slice_Number) return Optional_Name_Type
      is
        (if not Has_Element (Slices, Pos) or else Slice (Slices, Pos) = ""
         then No_Name else Optional_Name_Type (Slice (Slices, Pos)));
      --  Returns parameter of configuration for given position or No_Name
      --  if corresponding position is empty/absent. Reports error ans fails
      --  if prefix is not followed by a value.

      function Get_Description_Param
        (Slices : Slice_Set; Prefix : String) return Optional_Name_Type;
      --  Returns parameter of configuration for given Prefix or No_Name
      --  if corresponding position is empty/absent.

      Slices : constant Slice_Set := Create (Config, ",");

      ----------------------
      -- Check_Positional --
      ----------------------

      procedure Check_Positional is
         use Ada.Strings.Fixed;

         Language_Set : Boolean := False;
         Version_Set  : Boolean := False;
         Runtime_Set  : Boolean := False;
         Path_Set     : Boolean := False;
         Name_Set     : Boolean := False;
      begin
         for Slice of Slices loop
            if To_Lower (Head (Slice, 9)) = "language:" then
               if Language_Set then
                  Report_Error_And_Exit
                    ("Configuration parameter language specified twice in "
                     & Config
                     & ASCII.LF
                     & "Invalid configuration specified with --config");
               else
                  Language_Set := True;
               end if;

            elsif To_Lower (Head (Slice, 8)) = "version:" then
               if Version_Set then
                  Report_Error_And_Exit
                    ("Configuration parameter version specified twice in "
                     & Config
                     & ASCII.LF
                     & "Invalid configuration specified with --config");
               else
                  Version_Set := True;
               end if;

            elsif To_Lower (Head (Slice, 8)) = "runtime:" then
               if Runtime_Set then
                  Report_Error_And_Exit
                    ("Configuration parameter runtime specified twice in "
                     & Config
                     & ASCII.LF
                     & "Invalid configuration specified with --config");
               else
                  Runtime_Set := True;
               end if;

            elsif To_Lower (Head (Slice, 5)) = "path:" then
               if Path_Set then
                  Report_Error_And_Exit
                    ("Configuration parameter path specified twice in "
                     & Config
                     & ASCII.LF
                     & "Invalid configuration specified with --config");
               else
                  Path_Set := True;
               end if;

            elsif To_Lower (Head (Slice, 5)) = "name:" then
               if Name_Set then
                  Report_Error_And_Exit
                    ("Configuration parameter name specified twice in "
                     & Config
                     & ASCII.LF
                     & "Invalid configuration specified with --config");
               else
                  Name_Set := True;
               end if;

            end if;
         end loop;
      end Check_Positional;

      ---------------------------
      -- Get_Description_Param --
      ---------------------------

      function Get_Description_Param
        (Slices : Slice_Set; Prefix : String) return Optional_Name_Type
      is
         use Ada.Strings.Fixed;

         Pref     : constant String := Prefix & ":";
         Pref_Len : constant Positive := Pref'Length;
      begin
         for Slice of Slices loop
            if To_Lower (Head (Slice, Pref_Len)) = Pref then
               if Slice = Pref then
                  Report_Error_And_Exit
                    ("Parameter value for " & Prefix & " not specified in """
                     & Config & """" & ASCII.LF
                     & "Invalid configuration specified with --config");
               end if;

               return
                 Optional_Name_Type
                   (Slice (Slice'First + Pref_Len .. Slice'Last));
            end if;
         end loop;

         return No_Name;
      end Get_Description_Param;

      ---------------------------
      -- Positional_Parameters --
      ---------------------------

      function Positional_Parameters return Boolean is
         use Ada.Strings.Fixed;

         Positional_Present     : Boolean := False;
         Not_Positional_Present : Boolean := False;
      begin
         for Slice of Slices loop
            if Slice = ""
              or else (To_Lower (Head (Slice, 9)) /= "language:"
                        and then To_Lower (Head (Slice, 8)) /= "version:"
                        and then To_Lower (Head (Slice, 8)) /= "runtime:"
                        and then To_Lower (Head (Slice, 5)) /= "path:"
                        and then To_Lower (Head (Slice, 5)) /= "name:")
            then
               Not_Positional_Present := True;
            else
               Positional_Present := True;
            end if;

            if Not_Positional_Present and then Positional_Present then
               Report_Error_And_Exit
                 ("Mixing positional and not positional parameters in """
                  & Config & """"
                  & ASCII.LF
                  & "Invalid configuration specified with --config");
            end if;
         end loop;

         return Positional_Present;
      end Positional_Parameters;

      Result : Description;
   begin
      if Slice_Count (Slices) > 5 then
         Report_Error_And_Exit
           ("Too many arguments in configuration """ & Config & """"
            & ASCII.LF
            & "Invalid configuration specified with --config");
      end if;

      if Positional_Parameters then
         Check_Positional;

         if Get_Description_Param (Slices, "language") = No_Name then
            Report_Error_And_Exit
              ("Language not specified if " & Config
               & ASCII.LF
               & "Invalid configuration specified with --config");
         end if;

         Result := Project.Configuration.Create
           (Language => +Get_Description_Param (Slices, "language"),
            Version  => Get_Description_Param (Slices, "version"),
            Runtime  => Get_Description_Param (Slices, "runtime"),
            Path     => Filename_Optional
                          (Get_Description_Param (Slices, "path")),
            Name     => Get_Description_Param (Slices, "name"));
      else
         if Get_Description_Param (Slices, 1) = No_Name then
            Report_Error_And_Exit
              ("Language not specified if " & Config
               & ASCII.LF
               & "Invalid configuration specified with --config");
         end if;

         Result := Project.Configuration.Create
           (Language => +Get_Description_Param (Slices, 1),
            Version  => Get_Description_Param (Slices, 2),
            Runtime  => Get_Description_Param (Slices, 3),
            Path     => Filename_Optional (Get_Description_Param (Slices, 4)),
            Name     => Get_Description_Param (Slices, 5));
      end if;

      return Result;
   end Parse_Config_Parameter;

   --------------------------
   -- Register_Cmd_Options --
   --------------------------

   procedure Register_Cmd_Options is
   begin
      Define_Switch
        (Cmd_Config, Opt_Version'Access,
         Long_Switch => "--version",
         Help        => "Display version and exit");
      Define_Switch
        (Cmd_Config,
         Switch      => "-h",
         Long_Switch => "--help",
         Help        => "Display usage and exit");
      Define_Switch
        (Cmd_Config, Opt_Target'Access,
         Long_Switch => "--target=",
         Help        =>
           "Select specified target or ""all"" for any target" & ASCII.LF
         & "   ("
         & String (Default_Target)
         & " by default)",
         Argument    => "target");
      Define_Switch
        (Cmd_Config, Opt_Show_Targets'Access,
         Long_Switch => "--show-targets",
         Help        => "List all compiler targets available");
      Define_Switch
        (Cmd_Config, Opt_Show_MI'Access,
         Long_Switch => "--mi-show-compilers",
         Help        =>
           "List all compilers available in a parser-friendly way");
      Define_Switch
        (Cmd_Config, Opt_Show_Known'Access,
         Long_Switch => "--show-known-compilers",
         Help        =>
           "List names of all known compilers");
      Define_Switch
        (Cmd_Config, Opt_Batch'Access,
         Long_Switch => "--batch",
         Help        => "Batch mode, no interactive compiler selection");
      Define_Switch
        (Cmd_Config, Value_Callback'Unrestricted_Access,
         Switch => "-v",
         Help   => "Verbose mode");
      Define_Switch
        (Cmd_Config, Value_Callback'Unrestricted_Access,
         Switch => "-q",
         Help   => "Quiet mode");
      Define_Switch
        (Cmd_Config, Opt_O'Access,
         Switch   => "-o:",
         Help     =>
           "Name and directory of the output file"  & ASCII.LF
           & "  (default is default.cgpr)",
         Argument => "file");
      Define_Switch
        (Cmd_Config, Value_Callback'Unrestricted_Access,
         Long_Switch => "--db:",
         Help        =>
           "Parse DIR as an additional knowledge base" & ASCII.LF
           & " --db-                  Do not load the standard knowledge base",
         Argument    => "dir");
      --  ??? Do we still want to point to the KB in file format
      --  if it can be found?
      Define_Switch
        (Cmd_Config, Opt_Validate'Access,
         Long_Switch => "--validate",
         Help        =>
           "Validate contents of the knowledge base before loading");
      Define_Switch
        (Cmd_Config, Opt_Fallback'Access,
         Long_Switch => "--fallback-targets",
         Help        =>
           "Look for native toolchains of different architecture");
      Define_Switch
        (Cmd_Config, Value_Callback'Unrestricted_Access,
         Long_Switch => "--config=",
         Help        =>
           "Preselect a compiler."
           & ASCII.LF
           & "  CONFIG=language[,version[,runtime[,path[,name]]]]"
           & ASCII.LF
           & "  Name is either one of the names of the blocks"
           & " in the knowledge base"
           & ASCII.LF
           & "  ('GCC', 'GCC-28',...) or the base name of an executable"
           & " ('gcc', 'gnatmake')."
           & ASCII.LF
           & "  An empty string can be specified for any of the"
           & " optional parameters,"
           & ASCII.LF
           & "  otherwise positional prefixes can be used:"
           & ASCII.LF
           & "  --config=language:ada,runtime:zfp equals to "
           & "--config=ada,,zfp.",
         Argument    => "config");

      Set_Usage (Cmd_Config, Usage => "[switches]");

   end Register_Cmd_Options;

   ---------------------------
   -- Report_Error_And_Exit --
   ---------------------------

   procedure Report_Error_And_Exit (Msg : String) is
   begin
      Text_IO.Put_Line (Text_IO.Standard_Error, Msg);
      raise Exit_From_Command_Line;
   end Report_Error_And_Exit;

   ------------------------------------
   -- Select_Compilers_Interactively --
   ------------------------------------

   procedure Select_Compilers_Interactively
     (Base       : in out KB.Object;
      Compilers  : in out Compiler_Array;
      For_Target : Name_Type)
   is
      Input : Unbounded_String;
      Comp  : Compiler;
   begin
      loop
         Base.Filter_Compilers_List (Compilers, For_Target);

         Text_IO.Put_Line
           ("--------------------------------------------------");
         Text_IO.Put_Line
           ("gprconfig has found the following compilers on your PATH.");
         Text_IO.Put_Line
           ("Only those matching the target and the selected compilers"
            & " are displayed.");

         for Idx in Compilers'Range loop
            Comp := Compilers (Idx);

            if Is_Selectable (Comp) then

               if Is_Selected (Comp) then
                  Text_IO.Put ("*");
                  Integer_Text_IO.Put (Idx, Width => 3);
               else
                  Integer_Text_IO. Put (Idx, Width => 4);
               end if;

               Text_IO.Put (". ");

               if Requires_Compiler (Comp) then
                  Text_IO.Put
                    (String (Name (Comp))
                     & " for "
                     & Image (Language (Comp))
                     & " in "
                     & String (Path (Comp)));

                  if For_Target = "all" then
                     Text_IO.Put (" on " & String (Target (Comp)));
                  end if;

                  Text_IO.Put
                    (" version " & String (KB.Version (Comp)));

                  if Runtime (Comp, True) = No_Name then
                     Text_IO.New_Line;
                  else
                     Text_IO.Put_Line
                       (" ("
                        & String (Runtime (Comp, True))
                        & " runtime)");
                  end if;

               else
                  Text_IO.Put_Line
                    (Image (Language (Comp)) &
                       " (no compiler required)");
               end if;

            end if;
         end loop;

         Text_IO.Put
           ("Select or unselect the following compiler (or ""s"" to save): ");
         Input := To_Unbounded_String (Text_IO.Get_Line);

         exit when To_String (Input) = "s";

         declare
            Choice : Positive;
         begin
            Choice := Positive'Value (To_String (Input));

            if Choice > Compilers'Last then
               Text_IO.Put_Line ("Unrecognized choice");

            else
               if Is_Selected (Compilers (Choice)) then
                  Set_Selection (Compilers (Choice), False);
               else

                  Set_Selection (Compilers (Choice), True);
               end if;
            end if;

         exception
            when Constraint_Error =>
               Text_IO.Put_Line ("Unrecognized choice");
         end;

      end loop;
   end Select_Compilers_Interactively;

   ------------------------------
   -- Show_Command_Line_Config --
   ------------------------------

   procedure Show_Command_Line_Config
     (Compilers : Compiler_Array; Target : String) is
   begin
      if Compilers = No_Compilers then
         return;
      end if;

      Text_IO.New_Line;
      Text_IO.Put_Line
        ("You can regenerate the same config file in batch mode");
      Text_IO.Put_Line (" with the following command line:");
      Text_IO.Put ("gprconfig --batch");
      Text_IO.Put (" --target=");
      Text_IO.Put (Target);

      for Comp of Compilers loop
         if Is_Selected (Comp) then
            Text_IO.Put (" --config=");

            if Requires_Compiler (Comp) then
               Text_IO.Put
                 (Image (Language (Comp)) & ","
                  & String (KB.Version (Comp)) & ","
                  & String (Runtime (Comp)) & ","
                  & String (Path (Comp)) & ","
                  & String (Name (Comp)));
            else
               Text_IO.Put (Image (Language (Comp)) & ",,,,");
            end if;
         end if;
      end loop;

      Text_IO.New_Line;
      Text_IO.New_Line;
   end Show_Command_Line_Config;

   --------------------
   -- Value_Callback --
   --------------------

   procedure Value_Callback (Switch, Value : String) is
      Descr : Project.Configuration.Description;
   begin
      if Switch = "--db" then
         if Value = "-" then
            Opt_DB := True;

         else
            declare
               KB_Norm : constant String :=
                           GNAT.OS_Lib.Normalize_Pathname (Value);
               KB_Path : GPR2.Path_Name.Object;
            begin
               if GNAT.OS_Lib.Is_Directory (KB_Norm) then
                  KB_Path :=
                    GPR2.Path_Name.Create_Directory
                      (GPR2.Filename_Type (KB_Norm));

               elsif GNAT.OS_Lib.Is_Regular_File (KB_Norm) then
                  KB_Path :=
                    GPR2.Path_Name.Create_File (GPR2.Filename_Type (KB_Norm));

               else
                  Report_Error_And_Exit
                    (KB_Norm & " is not a file or directory");
               end if;

               KB_Locations.Append (KB_Path);
            end;
         end if;

      elsif Switch = "--config" then
         Descr := Parse_Config_Parameter (Value);
         if Description_Map.Contains (Language (Descr)) then
            Report_Error_And_Exit
              ("Multiple --config specified for " & Image (Language (Descr)));
         end if;
         Description_Map.Include (Language (Descr), Descr);

      elsif Switch = "-q" then
         Opt_Verbosity := Quiet;

      elsif Switch = "-v" then
         Opt_Verbosity := Verbose;

      end if;
   end Value_Callback;

   Config_Contents          : Unbounded_String;
   Selected_Target          : Unbounded_String;
   Output_File              : Unbounded_String;
   Config_Log               : Log.Object;
   Output                   : Text_IO.File_Type;
   Default_Config_File_Name : constant String := "default.cgpr";

begin
   begin
      GNATCOLL.Traces.Parse_Config_File;
   exception
      when E : others =>
         Text_IO.Put_Line
           (Text_IO.Standard_Error,
            "Cannot parse trace configuration file "
            & "(traces may work incorrectly):");
         Text_IO.Put_Line
           (Text_IO.Standard_Error,
            Ada.Exceptions.Exception_Message (E));
   end;
   GPRtools.Util.Set_Program_Name ("gprconfig");

   Register_Cmd_Options;

   Getopt (Cmd_Config);

   if Opt_Version then
      GPR2.Version.Display
        ("GPRCONFIG", "2006",  Version_String => GPR2.Version.Long_Value);
      GPR2.Version.Display_Free_Software;
      return;
   end if;

   if Opt_Batch and then Opt_Target.all = "all" then
      if Opt_Verbosity > Quiet then
         Text_IO.Put_Line
           (Text_IO.Standard_Error,
            "--target=all ignored in --batch mode");
      end if;

      GNAT.OS_Lib.Free (Opt_Target);
      Opt_Target := new String'("");
   end if;

   KB_Flags (Validation) := Opt_Validate;

   if Opt_Verbosity = Verbose then
      GNATCOLL.Traces.Set_Active
        (GNATCOLL.Traces.Create
           ("KNOWLEDGE_BASE"), True);
      GNATCOLL.Traces.Set_Active
        (GNATCOLL.Traces.Create
           ("KNOWLEDGE_BASE.PARSING_TRACE"), True);
      GNATCOLL.Traces.Set_Active
        (GNATCOLL.Traces.Create
           ("KNOWLEDGE_BASE.MATHCING"), True);
      GNATCOLL.Traces.Set_Active
        (GNATCOLL.Traces.Create
           ("KNOWLEDGE_BASE.COMPILER_ITERATOR"), True);
   end if;

   if Opt_DB then
      Knowledge_Base := Create_Empty;
   else
      Knowledge_Base := Create_Default (KB_Flags);
   end if;

   for KB_Location of KB_Locations loop
      Knowledge_Base.Add (KB_Flags, KB_Location);
   end loop;

   for Msg_Cur in Knowledge_Base.Log_Messages.Iterate
     (Information => Opt_Verbosity = Verbose,
      Warning     => Opt_Verbosity = Verbose)
   loop
      Log.Element (Msg_Cur).Output;
   end loop;

   if Knowledge_Base.Has_Error then
      Text_IO.Put_Line
        (Text_IO.Standard_Error,
         "Invalid setup of the gprconfig knowledge base");
      GNAT.OS_Lib.OS_Exit (1);
   end if;

   if Opt_Show_Known then
      Text_IO.Put_Line
        ("The known compilers are: "
         & To_String (Knowledge_Base.Known_Compiler_Names));
      return;
   end if;

   if Opt_Target.all = "" then
      Selected_Target :=
        To_Unbounded_String
          (String
             (Knowledge_Base.Normalized_Target
                (Default_Target)));
   else
      Selected_Target := To_Unbounded_String (Opt_Target.all);
   end if;

   if Opt_O.all = "" then
      if Opt_Target.all = "" then
         Output_File := To_Unbounded_String (Default_Config_File_Name);
      else
         Output_File := To_Unbounded_String (Opt_Target.all & ".cgpr");
      end if;
   else
      Output_File := To_Unbounded_String (Opt_O.all);
   end if;

   if Opt_Batch then
      Config_Contents := Configuration
        (Self     => Knowledge_Base,
         Settings => Get_Settings (Description_Map),
         Target   => Name_Type (To_String (Selected_Target)),
         Messages => Config_Log,
         Fallback => Opt_Fallback);

   else
      if Opt_Show_Targets then
         Selected_Target := To_Unbounded_String ("all");
      end if;

      declare
         Compilers : Compiler_Array := Knowledge_Base.All_Compilers
           (Settings => Get_Settings (Description_Map),
            Target   => Name_Type (To_String (Selected_Target)),
            Messages => Config_Log);

         Set_Of_Targets : GPR2.Containers.Name_Set;
      begin
         if Knowledge_Base.Has_Error then
            for Msg_Cur in Knowledge_Base.Log_Messages.Iterate
              (Information => Opt_Verbosity > Quiet,
               Warning     => Opt_Verbosity > Quiet)
            loop
               Log.Element (Msg_Cur).Output;
            end loop;

            Text_IO.Put_Line
              (Text_IO.Standard_Error,
               "Invalid setup of the gprconfig knowledge base");
            GNAT.OS_Lib.OS_Exit (1);
         end if;

         if Opt_Show_Targets or else Opt_Verbosity = Verbose then

            Text_IO.Put_Line ("List of targets supported by a compiler:");

            for Comp of Compilers loop
               Set_Of_Targets.Include
                 (Knowledge_Base.Normalized_Target (Target (Comp)));
            end loop;

            for Tgt of Set_Of_Targets loop

               Text_IO.Put (String (Tgt));

               if Tgt = Default_Target then
                  Text_IO.Put_Line (" (native target)");
               else
                  Text_IO.New_Line;
               end if;
            end loop;

         end if;

         if Opt_Show_Targets then
            return;
         end if;

         if Compilers'Length = 0 then
            if Selected_Target = Null_Unbounded_String then
               Text_IO.Put_Line
                 (Text_IO.Standard_Error,
                  "No compilers found for target "
                  & To_String (Selected_Target));
            else
               Text_IO.Put_Line
                 (Text_IO.Standard_Error, "No compilers found");
            end if;
            GNAT.OS_Lib.OS_Exit (1);
         end if;

         if Opt_Show_MI then
            Display_Compilers_For_Parser
              (Knowledge_Base, Compilers,
               Name_Type (To_String (Selected_Target)));
            return;
         else
            Select_Compilers_Interactively
              (Knowledge_Base, Compilers,
               Name_Type (To_String (Selected_Target)));
            Show_Command_Line_Config (Compilers, To_String (Selected_Target));
         end if;

         Config_Contents := Configuration
           (Self      => Knowledge_Base,
            Selection => Compilers,
            Target    => Name_Type (To_String (Selected_Target)),
            Messages  => Config_Log);
      end;

   end if;

   if Config_Log.Has_Error then
      for Msg_Cur in Config_Log.Iterate
        (Information => Opt_Verbosity > Quiet,
         Warning     => Opt_Verbosity > Quiet,
         Read        => False)
      loop
         Log.Element (Msg_Cur).Output;
      end loop;

      Text_IO.Put_Line
        (Text_IO.Standard_Error,
         "Generation of configuration files failed");

      GNAT.OS_Lib.OS_Exit (1);

   elsif Knowledge_Base.Has_Error then

      for Msg_Cur in Knowledge_Base.Log_Messages.Iterate
        (Information => Opt_Verbosity > Quiet,
         Warning     => Opt_Verbosity > Quiet)
      loop
         Log.Element (Msg_Cur).Output;
      end loop;

      Text_IO.Put_Line
        (Text_IO.Standard_Error,
         "Invalid setup of the gprconfig knowledge base");

      GNAT.OS_Lib.OS_Exit (1);

   else
      for Msg_Cur in Config_Log.Iterate
        (Information => Opt_Verbosity > Quiet)
      loop
         Log.Element (Msg_Cur).Output;
      end loop;
   end if;

   if Config_Contents /= Null_Unbounded_String then
      Text_IO.Create
        (Output, Text_IO.Out_File, To_String (Output_File));

      Text_IO.Put_Line
        (Output, "--  This gpr configuration file was generated by gprconfig");
      Text_IO.Put_Line (Output, "--  using this command line:");
      Text_IO.Put (Output, "--  " & Ada.Command_Line.Command_Name);

      for I in 1 .. Ada.Command_Line.Argument_Count loop
         Text_IO.Put (Output, ' ');
         Text_IO.Put (Output, Ada.Command_Line.Argument (I));
      end loop;

      Text_IO.New_Line (Output);
      Text_IO.Put (Output, "--  from ");
      Text_IO.Put (Output, GNAT.Directory_Operations.Get_Current_Dir);
      Text_IO.New_Line (Output);

      Text_IO.Put_Line (Output, To_String (Config_Contents));
      Text_IO.Close (Output);
   end if;

   GNAT.Command_Line.Free (Config => Cmd_Config);

exception
   when Ada.Directories.Name_Error | Ada.IO_Exceptions.Use_Error =>
      GNAT.Command_Line.Free (Config => Cmd_Config);
      Text_IO.Put_Line
        (Text_IO.Standard_Error,
         "Could not create the file " & To_String (Output_File));

   when Invalid_Switch | Exit_From_Command_Line =>
      GNAT.Command_Line.Free (Config => Cmd_Config);
      GNAT.OS_Lib.OS_Exit (1);

   when Invalid_Parameter =>
      GNAT.Command_Line.Free (Config => Cmd_Config);
      Text_IO.Put_Line
        (Text_IO.Standard_Error,
         "Missing parameter for switch: -" & Full_Switch);
      Text_IO.Put_Line
        (Text_IO.Standard_Error,
         "try ""gprconfig --help"" for more information.");

   when E : others =>
      Text_IO.Put_Line
        (Text_IO.Standard_Error,
         "Unrecoverable error in GPRconfig: "
         & Ada.Exceptions.Exception_Information (E));
      GNAT.Command_Line.Free (Config => Cmd_Config);
      GNAT.OS_Lib.OS_Exit (1);
end GPRconfig;