OSDN Git Service

gcc/fortran/
[pf3gnuchains/gcc-fork.git] / gcc / ada / makeutl.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              M A K E U T L                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 2004-2010, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with ALI;      use ALI;
27 with Debug;
28 with Fname;
29 with Hostparm;
30 with Osint;    use Osint;
31 with Output;   use Output;
32 with Opt;      use Opt;
33 with Prj.Ext;
34 with Prj.Util;
35 with Snames;   use Snames;
36 with Table;
37 with Tempdir;
38
39 with Ada.Command_Line;  use Ada.Command_Line;
40
41 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
42
43 with GNAT.Case_Util; use GNAT.Case_Util;
44 with GNAT.HTable;
45
46 package body Makeutl is
47
48    type Mark_Key is record
49       File  : File_Name_Type;
50       Index : Int;
51    end record;
52    --  Identify either a mono-unit source (when Index = 0) or a specific unit
53    --  (index = 1's origin index of unit) in a multi-unit source.
54
55    --  There follow many global undocumented declarations, comments needed ???
56
57    Max_Mask_Num : constant := 2048;
58
59    subtype Mark_Num is Union_Id range 0 .. Max_Mask_Num - 1;
60
61    function Hash (Key : Mark_Key) return Mark_Num;
62
63    package Marks is new GNAT.HTable.Simple_HTable
64      (Header_Num => Mark_Num,
65       Element    => Boolean,
66       No_Element => False,
67       Key        => Mark_Key,
68       Hash       => Hash,
69       Equal      => "=");
70    --  A hash table to keep tracks of the marked units
71
72    type Linker_Options_Data is record
73       Project : Project_Id;
74       Options : String_List_Id;
75    end record;
76
77    Linker_Option_Initial_Count : constant := 20;
78
79    Linker_Options_Buffer : String_List_Access :=
80      new String_List (1 .. Linker_Option_Initial_Count);
81
82    Last_Linker_Option : Natural := 0;
83
84    package Linker_Opts is new Table.Table (
85      Table_Component_Type => Linker_Options_Data,
86      Table_Index_Type     => Integer,
87      Table_Low_Bound      => 1,
88      Table_Initial        => 10,
89      Table_Increment      => 100,
90      Table_Name           => "Make.Linker_Opts");
91
92    procedure Add_Linker_Option (Option : String);
93
94    ---------
95    -- Add --
96    ---------
97
98    procedure Add
99      (Option : String_Access;
100       To     : in out String_List_Access;
101       Last   : in out Natural)
102    is
103    begin
104       if Last = To'Last then
105          declare
106             New_Options : constant String_List_Access :=
107                             new String_List (1 .. To'Last * 2);
108
109          begin
110             New_Options (To'Range) := To.all;
111
112             --  Set all elements of the original options to null to avoid
113             --  deallocation of copies.
114
115             To.all := (others => null);
116
117             Free (To);
118             To := New_Options;
119          end;
120       end if;
121
122       Last := Last + 1;
123       To (Last) := Option;
124    end Add;
125
126    procedure Add
127      (Option : String;
128       To     : in out String_List_Access;
129       Last   : in out Natural)
130    is
131    begin
132       Add (Option => new String'(Option), To => To, Last => Last);
133    end Add;
134
135    -----------------------
136    -- Add_Linker_Option --
137    -----------------------
138
139    procedure Add_Linker_Option (Option : String) is
140    begin
141       if Option'Length > 0 then
142          if Last_Linker_Option = Linker_Options_Buffer'Last then
143             declare
144                New_Buffer : constant String_List_Access :=
145                               new String_List
146                                 (1 .. Linker_Options_Buffer'Last +
147                                         Linker_Option_Initial_Count);
148             begin
149                New_Buffer (Linker_Options_Buffer'Range) :=
150                  Linker_Options_Buffer.all;
151                Linker_Options_Buffer.all := (others => null);
152                Free (Linker_Options_Buffer);
153                Linker_Options_Buffer := New_Buffer;
154             end;
155          end if;
156
157          Last_Linker_Option := Last_Linker_Option + 1;
158          Linker_Options_Buffer (Last_Linker_Option) := new String'(Option);
159       end if;
160    end Add_Linker_Option;
161
162    -------------------------
163    -- Base_Name_Index_For --
164    -------------------------
165
166    function Base_Name_Index_For
167      (Main            : String;
168       Main_Index      : Int;
169       Index_Separator : Character) return File_Name_Type
170    is
171       Result : File_Name_Type;
172
173    begin
174       Name_Len := 0;
175       Add_Str_To_Name_Buffer (Base_Name (Main));
176
177       --  Remove the extension, if any, that is the last part of the base name
178       --  starting with a dot and following some characters.
179
180       for J in reverse 2 .. Name_Len loop
181          if Name_Buffer (J) = '.' then
182             Name_Len := J - 1;
183             exit;
184          end if;
185       end loop;
186
187       --  Add the index info, if index is different from 0
188
189       if Main_Index > 0 then
190          Add_Char_To_Name_Buffer (Index_Separator);
191
192          declare
193             Img : constant String := Main_Index'Img;
194          begin
195             Add_Str_To_Name_Buffer (Img (2 .. Img'Last));
196          end;
197       end if;
198
199       Result := Name_Find;
200       return Result;
201    end Base_Name_Index_For;
202
203    ------------------------------
204    -- Check_Source_Info_In_ALI --
205    ------------------------------
206
207    function Check_Source_Info_In_ALI (The_ALI : ALI_Id) return Boolean is
208       Unit_Name : Name_Id;
209
210    begin
211       --  Loop through units
212
213       for U in ALIs.Table (The_ALI).First_Unit ..
214                ALIs.Table (The_ALI).Last_Unit
215       loop
216          --  Check if the file name is one of the source of the unit
217
218          Get_Name_String (Units.Table (U).Uname);
219          Name_Len  := Name_Len - 2;
220          Unit_Name := Name_Find;
221
222          if File_Not_A_Source_Of (Unit_Name, Units.Table (U).Sfile) then
223             return False;
224          end if;
225
226          --  Loop to do same check for each of the withed units
227
228          for W in Units.Table (U).First_With .. Units.Table (U).Last_With loop
229             declare
230                WR : ALI.With_Record renames Withs.Table (W);
231
232             begin
233                if WR.Sfile /= No_File then
234                   Get_Name_String (WR.Uname);
235                   Name_Len  := Name_Len - 2;
236                   Unit_Name := Name_Find;
237
238                   if File_Not_A_Source_Of (Unit_Name, WR.Sfile) then
239                      return False;
240                   end if;
241                end if;
242             end;
243          end loop;
244       end loop;
245
246       --  Loop to check subunits
247
248       for D in ALIs.Table (The_ALI).First_Sdep ..
249                ALIs.Table (The_ALI).Last_Sdep
250       loop
251          declare
252             SD : Sdep_Record renames Sdep.Table (D);
253
254          begin
255             Unit_Name := SD.Subunit_Name;
256
257             if Unit_Name /= No_Name then
258
259                --  For separates, the file is no longer associated with the
260                --  unit ("proc-sep.adb" is not associated with unit "proc.sep")
261                --  so we need to check whether the source file still exists in
262                --  the source tree: it will if it matches the naming scheme
263                --  (and then will be for the same unit).
264
265                if Find_Source
266                     (In_Tree   => Project_Tree,
267                      Project   => No_Project,
268                      Base_Name => SD.Sfile) = No_Source
269                then
270                   --  If this is not a runtime file or if, when gnatmake switch
271                   --  -a is used, we are not able to find this subunit in the
272                   --  source directories, then recompilation is needed.
273
274                   if not Fname.Is_Internal_File_Name (SD.Sfile)
275                     or else
276                       (Check_Readonly_Files
277                         and then Full_Source_Name (SD.Sfile) = No_File)
278                   then
279                      if Verbose_Mode then
280                         Write_Line
281                           ("While parsing ALI file, file "
282                            & Get_Name_String (SD.Sfile)
283                            & " is indicated as containing subunit "
284                            & Get_Name_String (Unit_Name)
285                            & " but this does not match what was found while"
286                            & " parsing the project. Will recompile");
287                      end if;
288
289                      return False;
290                   end if;
291                end if;
292             end if;
293          end;
294       end loop;
295
296       return True;
297    end Check_Source_Info_In_ALI;
298
299    --------------------------------
300    -- Create_Binder_Mapping_File --
301    --------------------------------
302
303    function Create_Binder_Mapping_File return Path_Name_Type is
304       Mapping_Path : Path_Name_Type := No_Path;
305
306       Mapping_FD : File_Descriptor := Invalid_FD;
307       --  A File Descriptor for an eventual mapping file
308
309       ALI_Unit : Unit_Name_Type := No_Unit_Name;
310       --  The unit name of an ALI file
311
312       ALI_Name : File_Name_Type := No_File;
313       --  The file name of the ALI file
314
315       ALI_Project : Project_Id := No_Project;
316       --  The project of the ALI file
317
318       Bytes : Integer;
319       OK    : Boolean := False;
320       Unit  : Unit_Index;
321
322       Status : Boolean;
323       --  For call to Close
324
325    begin
326       Tempdir.Create_Temp_File (Mapping_FD, Mapping_Path);
327       Record_Temp_File (Project_Tree, Mapping_Path);
328
329       if Mapping_FD /= Invalid_FD then
330          OK := True;
331
332          --  Traverse all units
333
334          Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
335          while Unit /= No_Unit_Index loop
336             if Unit.Name /= No_Name then
337
338                --  If there is a body, put it in the mapping
339
340                if Unit.File_Names (Impl) /= No_Source
341                  and then Unit.File_Names (Impl).Project /= No_Project
342                then
343                   Get_Name_String (Unit.Name);
344                   Add_Str_To_Name_Buffer ("%b");
345                   ALI_Unit := Name_Find;
346                   ALI_Name :=
347                     Lib_File_Name (Unit.File_Names (Impl).Display_File);
348                   ALI_Project := Unit.File_Names (Impl).Project;
349
350                   --  Otherwise, if there is a spec, put it in the mapping
351
352                elsif Unit.File_Names (Spec) /= No_Source
353                  and then Unit.File_Names (Spec).Project /= No_Project
354                then
355                   Get_Name_String (Unit.Name);
356                   Add_Str_To_Name_Buffer ("%s");
357                   ALI_Unit := Name_Find;
358                   ALI_Name :=
359                     Lib_File_Name (Unit.File_Names (Spec).Display_File);
360                   ALI_Project := Unit.File_Names (Spec).Project;
361
362                else
363                   ALI_Name := No_File;
364                end if;
365
366                --  If we have something to put in the mapping then do it now.
367                --  However, if the project is extended, we don't put anything
368                --  in the mapping file, since we don't know where the ALI file
369                --  is: it might be in the extended project object directory as
370                --  well as in the extending project object directory.
371
372                if ALI_Name /= No_File
373                  and then ALI_Project.Extended_By = No_Project
374                  and then ALI_Project.Extends = No_Project
375                then
376                   --  First check if the ALI file exists. If it does not, do
377                   --  not put the unit in the mapping file.
378
379                   declare
380                      ALI : constant String := Get_Name_String (ALI_Name);
381
382                   begin
383                      --  For library projects, use the library ALI directory,
384                      --  for other projects, use the object directory.
385
386                      if ALI_Project.Library then
387                         Get_Name_String
388                           (ALI_Project.Library_ALI_Dir.Display_Name);
389                      else
390                         Get_Name_String
391                           (ALI_Project.Object_Directory.Display_Name);
392                      end if;
393
394                      if not
395                        Is_Directory_Separator (Name_Buffer (Name_Len))
396                      then
397                         Add_Char_To_Name_Buffer (Directory_Separator);
398                      end if;
399
400                      Add_Str_To_Name_Buffer (ALI);
401                      Add_Char_To_Name_Buffer (ASCII.LF);
402
403                      declare
404                         ALI_Path_Name : constant String :=
405                           Name_Buffer (1 .. Name_Len);
406
407                      begin
408                         if Is_Regular_File
409                              (ALI_Path_Name (1 .. ALI_Path_Name'Last - 1))
410                         then
411                            --  First line is the unit name
412
413                            Get_Name_String (ALI_Unit);
414                            Add_Char_To_Name_Buffer (ASCII.LF);
415                            Bytes :=
416                              Write
417                                (Mapping_FD,
418                                 Name_Buffer (1)'Address,
419                                 Name_Len);
420                            OK := Bytes = Name_Len;
421
422                            exit when not OK;
423
424                            --  Second line it the ALI file name
425
426                            Get_Name_String (ALI_Name);
427                            Add_Char_To_Name_Buffer (ASCII.LF);
428                            Bytes :=
429                              Write
430                                (Mapping_FD,
431                                 Name_Buffer (1)'Address,
432                                 Name_Len);
433                            OK := (Bytes = Name_Len);
434
435                            exit when not OK;
436
437                            --  Third line it the ALI path name
438
439                            Bytes :=
440                              Write
441                                (Mapping_FD,
442                                 ALI_Path_Name (1)'Address,
443                                 ALI_Path_Name'Length);
444                            OK := (Bytes = ALI_Path_Name'Length);
445
446                            --  If OK is False, it means we were unable to
447                            --  write a line. No point in continuing with the
448                            --  other units.
449
450                            exit when not OK;
451                         end if;
452                      end;
453                   end;
454                end if;
455             end if;
456
457             Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
458          end loop;
459
460          Close (Mapping_FD, Status);
461
462          OK := OK and Status;
463       end if;
464
465       --  If the creation of the mapping file was successful, we add the switch
466       --  to the arguments of gnatbind.
467
468       if OK then
469          return Mapping_Path;
470
471       else
472          return No_Path;
473       end if;
474    end Create_Binder_Mapping_File;
475
476    -----------------
477    -- Create_Name --
478    -----------------
479
480    function Create_Name (Name : String) return File_Name_Type is
481    begin
482       Name_Len := 0;
483       Add_Str_To_Name_Buffer (Name);
484       return Name_Find;
485    end Create_Name;
486
487    function Create_Name (Name : String) return Name_Id is
488    begin
489       Name_Len := 0;
490       Add_Str_To_Name_Buffer (Name);
491       return Name_Find;
492    end Create_Name;
493
494    function Create_Name (Name : String) return Path_Name_Type is
495    begin
496       Name_Len := 0;
497       Add_Str_To_Name_Buffer (Name);
498       return Name_Find;
499    end Create_Name;
500
501    ----------------------
502    -- Delete_All_Marks --
503    ----------------------
504
505    procedure Delete_All_Marks is
506    begin
507       Marks.Reset;
508    end Delete_All_Marks;
509
510    ----------------------------
511    -- Executable_Prefix_Path --
512    ----------------------------
513
514    function Executable_Prefix_Path return String is
515       Exec_Name : constant String := Command_Name;
516
517       function Get_Install_Dir (S : String) return String;
518       --  S is the executable name preceded by the absolute or relative path,
519       --  e.g. "c:\usr\bin\gcc.exe". Returns the absolute directory where "bin"
520       --  lies (in the example "C:\usr"). If the executable is not in a "bin"
521       --  directory, return "".
522
523       ---------------------
524       -- Get_Install_Dir --
525       ---------------------
526
527       function Get_Install_Dir (S : String) return String is
528          Exec      : String  := S;
529          Path_Last : Integer := 0;
530
531       begin
532          for J in reverse Exec'Range loop
533             if Exec (J) = Directory_Separator then
534                Path_Last := J - 1;
535                exit;
536             end if;
537          end loop;
538
539          if Path_Last >= Exec'First + 2 then
540             To_Lower (Exec (Path_Last - 2 .. Path_Last));
541          end if;
542
543          if Path_Last < Exec'First + 2
544            or else Exec (Path_Last - 2 .. Path_Last) /= "bin"
545            or else (Path_Last - 3 >= Exec'First
546                      and then Exec (Path_Last - 3) /= Directory_Separator)
547          then
548             return "";
549          end if;
550
551          return Normalize_Pathname
552                   (Exec (Exec'First .. Path_Last - 4),
553                    Resolve_Links => Opt.Follow_Links_For_Dirs)
554            & Directory_Separator;
555       end Get_Install_Dir;
556
557    --  Beginning of Executable_Prefix_Path
558
559    begin
560       --  For VMS, the path returned is always /gnu/
561
562       if Hostparm.OpenVMS then
563          return "/gnu/";
564       end if;
565
566       --  First determine if a path prefix was placed in front of the
567       --  executable name.
568
569       for J in reverse Exec_Name'Range loop
570          if Exec_Name (J) = Directory_Separator then
571             return Get_Install_Dir (Exec_Name);
572          end if;
573       end loop;
574
575       --  If we get here, the user has typed the executable name with no
576       --  directory prefix.
577
578       declare
579          Path : String_Access := Locate_Exec_On_Path (Exec_Name);
580       begin
581          if Path = null then
582             return "";
583          else
584             declare
585                Dir : constant String := Get_Install_Dir (Path.all);
586             begin
587                Free (Path);
588                return Dir;
589             end;
590          end if;
591       end;
592    end Executable_Prefix_Path;
593
594    --------------------------
595    -- File_Not_A_Source_Of --
596    --------------------------
597
598    function File_Not_A_Source_Of
599      (Uname : Name_Id;
600       Sfile : File_Name_Type) return Boolean
601    is
602       Unit : constant Unit_Index :=
603                Units_Htable.Get (Project_Tree.Units_HT, Uname);
604
605       At_Least_One_File : Boolean := False;
606
607    begin
608       if Unit /= No_Unit_Index then
609          for F in Unit.File_Names'Range loop
610             if Unit.File_Names (F) /= null then
611                At_Least_One_File := True;
612                if Unit.File_Names (F).File = Sfile then
613                   return False;
614                end if;
615             end if;
616          end loop;
617
618          if not At_Least_One_File then
619
620             --  The unit was probably created initially for a separate unit
621             --  (which are initially created as IMPL when both suffixes are the
622             --  same). Later on, Override_Kind changed the type of the file,
623             --  and the unit is no longer valid in fact.
624
625             return False;
626          end if;
627
628          Verbose_Msg (Uname, "sources do not include ", Name_Id (Sfile));
629          return True;
630       end if;
631
632       return False;
633    end File_Not_A_Source_Of;
634
635    ----------
636    -- Hash --
637    ----------
638
639    function Hash (Key : Mark_Key) return Mark_Num is
640    begin
641       return Union_Id (Key.File) mod Max_Mask_Num;
642    end Hash;
643
644    ------------
645    -- Inform --
646    ------------
647
648    procedure Inform (N : File_Name_Type; Msg : String) is
649    begin
650       Inform (Name_Id (N), Msg);
651    end Inform;
652
653    procedure Inform (N : Name_Id := No_Name; Msg : String) is
654    begin
655       Osint.Write_Program_Name;
656
657       Write_Str (": ");
658
659       if N /= No_Name then
660          Write_Str ("""");
661
662          declare
663             Name : constant String := Get_Name_String (N);
664          begin
665             if Debug.Debug_Flag_F and then Is_Absolute_Path (Name) then
666                Write_Str (File_Name (Name));
667             else
668                Write_Str (Name);
669             end if;
670          end;
671
672          Write_Str (""" ");
673       end if;
674
675       Write_Str (Msg);
676       Write_Eol;
677    end Inform;
678
679    ----------------------------
680    -- Is_External_Assignment --
681    ----------------------------
682
683    function Is_External_Assignment
684      (Tree : Prj.Tree.Project_Node_Tree_Ref;
685       Argv : String) return Boolean
686    is
687       Start     : Positive := 3;
688       Finish    : Natural := Argv'Last;
689
690       pragma Assert (Argv'First = 1);
691       pragma Assert (Argv (1 .. 2) = "-X");
692
693    begin
694       if Argv'Last < 5 then
695          return False;
696
697       elsif Argv (3) = '"' then
698          if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
699             return False;
700          else
701             Start := 4;
702             Finish := Argv'Last - 1;
703          end if;
704       end if;
705
706       return Prj.Ext.Check
707         (Tree        => Tree,
708          Declaration => Argv (Start .. Finish));
709    end Is_External_Assignment;
710
711    ---------------
712    -- Is_Marked --
713    ---------------
714
715    function Is_Marked
716      (Source_File : File_Name_Type;
717       Index       : Int := 0) return Boolean
718    is
719    begin
720       return Marks.Get (K => (File => Source_File, Index => Index));
721    end Is_Marked;
722
723    -----------------------------
724    -- Linker_Options_Switches --
725    -----------------------------
726
727    function Linker_Options_Switches
728      (Project  : Project_Id;
729       In_Tree  : Project_Tree_Ref) return String_List
730    is
731       procedure Recursive_Add (Proj : Project_Id; Dummy : in out Boolean);
732       --  The recursive routine used to add linker options
733
734       -------------------
735       -- Recursive_Add --
736       -------------------
737
738       procedure Recursive_Add (Proj : Project_Id; Dummy : in out Boolean) is
739          pragma Unreferenced (Dummy);
740
741          Linker_Package : Package_Id;
742          Options        : Variable_Value;
743
744       begin
745          Linker_Package :=
746            Prj.Util.Value_Of
747              (Name        => Name_Linker,
748               In_Packages => Proj.Decl.Packages,
749               In_Tree     => In_Tree);
750
751          Options :=
752            Prj.Util.Value_Of
753              (Name                    => Name_Ada,
754               Index                   => 0,
755               Attribute_Or_Array_Name => Name_Linker_Options,
756               In_Package              => Linker_Package,
757               In_Tree                 => In_Tree);
758
759          --  If attribute is present, add the project with
760          --  the attribute to table Linker_Opts.
761
762          if Options /= Nil_Variable_Value then
763             Linker_Opts.Increment_Last;
764             Linker_Opts.Table (Linker_Opts.Last) :=
765               (Project => Proj, Options => Options.Values);
766          end if;
767       end Recursive_Add;
768
769       procedure For_All_Projects is
770         new For_Every_Project_Imported (Boolean, Recursive_Add);
771
772       Dummy : Boolean := False;
773
774    --  Start of processing for Linker_Options_Switches
775
776    begin
777       Linker_Opts.Init;
778
779       For_All_Projects (Project, Dummy, Imported_First => True);
780
781       Last_Linker_Option := 0;
782
783       for Index in reverse 1 .. Linker_Opts.Last loop
784          declare
785             Options : String_List_Id;
786             Proj    : constant Project_Id :=
787                         Linker_Opts.Table (Index).Project;
788             Option  : Name_Id;
789             Dir_Path : constant String :=
790                          Get_Name_String (Proj.Directory.Name);
791
792          begin
793             Options := Linker_Opts.Table (Index).Options;
794             while Options /= Nil_String loop
795                Option := In_Tree.String_Elements.Table (Options).Value;
796                Get_Name_String (Option);
797
798                --  Do not consider empty linker options
799
800                if Name_Len /= 0 then
801                   Add_Linker_Option (Name_Buffer (1 .. Name_Len));
802
803                   --  Object files and -L switches specified with relative
804                   --  paths must be converted to absolute paths.
805
806                   Test_If_Relative_Path
807                     (Switch => Linker_Options_Buffer (Last_Linker_Option),
808                      Parent => Dir_Path,
809                      Including_L_Switch => True);
810                end if;
811
812                Options := In_Tree.String_Elements.Table (Options).Next;
813             end loop;
814          end;
815       end loop;
816
817       return Linker_Options_Buffer (1 .. Last_Linker_Option);
818    end Linker_Options_Switches;
819
820    -----------
821    -- Mains --
822    -----------
823
824    package body Mains is
825
826       type File_And_Loc is record
827          File_Name : File_Name_Type;
828          Index     : Int := 0;
829          Location  : Source_Ptr := No_Location;
830       end record;
831
832       package Names is new Table.Table
833         (Table_Component_Type => File_And_Loc,
834          Table_Index_Type     => Integer,
835          Table_Low_Bound      => 1,
836          Table_Initial        => 10,
837          Table_Increment      => 100,
838          Table_Name           => "Makeutl.Mains.Names");
839       --  The table that stores the mains
840
841       Current : Natural := 0;
842       --  The index of the last main retrieved from the table
843
844       --------------
845       -- Add_Main --
846       --------------
847
848       procedure Add_Main (Name : String) is
849       begin
850          Name_Len := 0;
851          Add_Str_To_Name_Buffer (Name);
852          Names.Increment_Last;
853          Names.Table (Names.Last) := (Name_Find, 0, No_Location);
854       end Add_Main;
855
856       ------------
857       -- Delete --
858       ------------
859
860       procedure Delete is
861       begin
862          Names.Set_Last (0);
863          Mains.Reset;
864       end Delete;
865
866       ---------------
867       -- Get_Index --
868       ---------------
869
870       function Get_Index return Int is
871       begin
872          if Current in Names.First .. Names.Last then
873             return Names.Table (Current).Index;
874          else
875             return 0;
876          end if;
877       end Get_Index;
878
879       ------------------
880       -- Get_Location --
881       ------------------
882
883       function Get_Location return Source_Ptr is
884       begin
885          if Current in Names.First .. Names.Last then
886             return Names.Table (Current).Location;
887          else
888             return No_Location;
889          end if;
890       end Get_Location;
891
892       ---------------
893       -- Next_Main --
894       ---------------
895
896       function Next_Main return String is
897       begin
898          if Current >= Names.Last then
899             return "";
900          else
901             Current := Current + 1;
902             return Get_Name_String (Names.Table (Current).File_Name);
903          end if;
904       end Next_Main;
905
906       ---------------------
907       -- Number_Of_Mains --
908       ---------------------
909
910       function Number_Of_Mains return Natural is
911       begin
912          return Names.Last;
913       end Number_Of_Mains;
914
915       -----------
916       -- Reset --
917       -----------
918
919       procedure Reset is
920       begin
921          Current := 0;
922       end Reset;
923
924       ---------------
925       -- Set_Index --
926       ---------------
927
928       procedure Set_Index (Index : Int) is
929       begin
930          if Names.Last > 0 then
931             Names.Table (Names.Last).Index := Index;
932          end if;
933       end Set_Index;
934
935       ------------------
936       -- Set_Location --
937       ------------------
938
939       procedure Set_Location (Location : Source_Ptr) is
940       begin
941          if Names.Last > 0 then
942             Names.Table (Names.Last).Location := Location;
943          end if;
944       end Set_Location;
945
946       -----------------
947       -- Update_Main --
948       -----------------
949
950       procedure Update_Main (Name : String) is
951       begin
952          if Current in Names.First .. Names.Last then
953             Name_Len := 0;
954             Add_Str_To_Name_Buffer (Name);
955             Names.Table (Current).File_Name := Name_Find;
956          end if;
957       end Update_Main;
958    end Mains;
959
960    ----------
961    -- Mark --
962    ----------
963
964    procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
965    begin
966       Marks.Set (K => (File => Source_File, Index => Index), E => True);
967    end Mark;
968
969    -----------------------
970    -- Path_Or_File_Name --
971    -----------------------
972
973    function Path_Or_File_Name (Path : Path_Name_Type) return String is
974       Path_Name : constant String := Get_Name_String (Path);
975    begin
976       if Debug.Debug_Flag_F then
977          return File_Name (Path_Name);
978       else
979          return Path_Name;
980       end if;
981    end Path_Or_File_Name;
982
983    ---------------------------
984    -- Test_If_Relative_Path --
985    ---------------------------
986
987    procedure Test_If_Relative_Path
988      (Switch               : in out String_Access;
989       Parent               : String;
990       Including_L_Switch   : Boolean := True;
991       Including_Non_Switch : Boolean := True;
992       Including_RTS        : Boolean := False)
993    is
994    begin
995       if Switch /= null then
996          declare
997             Sw    : String (1 .. Switch'Length);
998             Start : Positive;
999
1000          begin
1001             Sw := Switch.all;
1002
1003             if Sw (1) = '-' then
1004                if Sw'Length >= 3
1005                  and then (Sw (2) = 'A'
1006                             or else Sw (2) = 'I'
1007                             or else (Including_L_Switch and then Sw (2) = 'L'))
1008                then
1009                   Start := 3;
1010
1011                   if Sw = "-I-" then
1012                      return;
1013                   end if;
1014
1015                elsif Sw'Length >= 4
1016                  and then (Sw (2 .. 3) = "aL"
1017                             or else Sw (2 .. 3) = "aO"
1018                             or else Sw (2 .. 3) = "aI")
1019                then
1020                   Start := 4;
1021
1022                elsif Including_RTS
1023                  and then Sw'Length >= 7
1024                  and then Sw (2 .. 6) = "-RTS="
1025                then
1026                   Start := 7;
1027
1028                else
1029                   return;
1030                end if;
1031
1032                --  Because relative path arguments to --RTS= may be relative
1033                --  to the search directory prefix, those relative path
1034                --  arguments are converted only when they include directory
1035                --  information.
1036
1037                if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
1038                   if Parent'Length = 0 then
1039                      Do_Fail
1040                        ("relative search path switches ("""
1041                         & Sw
1042                         & """) are not allowed");
1043
1044                   elsif Including_RTS then
1045                      for J in Start .. Sw'Last loop
1046                         if Sw (J) = Directory_Separator then
1047                            Switch :=
1048                              new String'
1049                                (Sw (1 .. Start - 1) &
1050                                 Parent &
1051                                 Directory_Separator &
1052                                 Sw (Start .. Sw'Last));
1053                            return;
1054                         end if;
1055                      end loop;
1056
1057                   else
1058                      Switch :=
1059                        new String'
1060                          (Sw (1 .. Start - 1) &
1061                           Parent &
1062                           Directory_Separator &
1063                           Sw (Start .. Sw'Last));
1064                   end if;
1065                end if;
1066
1067             elsif Including_Non_Switch then
1068                if not Is_Absolute_Path (Sw) then
1069                   if Parent'Length = 0 then
1070                      Do_Fail
1071                        ("relative paths (""" & Sw & """) are not allowed");
1072                   else
1073                      Switch := new String'(Parent & Directory_Separator & Sw);
1074                   end if;
1075                end if;
1076             end if;
1077          end;
1078       end if;
1079    end Test_If_Relative_Path;
1080
1081    -------------------
1082    -- Unit_Index_Of --
1083    -------------------
1084
1085    function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
1086       Start  : Natural;
1087       Finish : Natural;
1088       Result : Int := 0;
1089
1090    begin
1091       Get_Name_String (ALI_File);
1092
1093       --  First, find the last dot
1094
1095       Finish := Name_Len;
1096
1097       while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
1098          Finish := Finish - 1;
1099       end loop;
1100
1101       if Finish = 1 then
1102          return 0;
1103       end if;
1104
1105       --  Now check that the dot is preceded by digits
1106
1107       Start := Finish;
1108       Finish := Finish - 1;
1109
1110       while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
1111          Start := Start - 1;
1112       end loop;
1113
1114       --  If there are no digits, or if the digits are not preceded by the
1115       --  character that precedes a unit index, this is not the ALI file of
1116       --  a unit in a multi-unit source.
1117
1118       if Start > Finish
1119         or else Start = 1
1120         or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
1121       then
1122          return 0;
1123       end if;
1124
1125       --  Build the index from the digit(s)
1126
1127       while Start <= Finish loop
1128          Result := Result * 10 +
1129                      Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');
1130          Start := Start + 1;
1131       end loop;
1132
1133       return Result;
1134    end Unit_Index_Of;
1135
1136    -----------------
1137    -- Verbose_Msg --
1138    -----------------
1139
1140    procedure Verbose_Msg
1141      (N1                : Name_Id;
1142       S1                : String;
1143       N2                : Name_Id := No_Name;
1144       S2                : String  := "";
1145       Prefix            : String := "  -> ";
1146       Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
1147    is
1148    begin
1149       if not Opt.Verbose_Mode
1150         or else Minimum_Verbosity > Opt.Verbosity_Level
1151       then
1152          return;
1153       end if;
1154
1155       Write_Str (Prefix);
1156       Write_Str ("""");
1157       Write_Name (N1);
1158       Write_Str (""" ");
1159       Write_Str (S1);
1160
1161       if N2 /= No_Name then
1162          Write_Str (" """);
1163          Write_Name (N2);
1164          Write_Str (""" ");
1165       end if;
1166
1167       Write_Str (S2);
1168       Write_Eol;
1169    end Verbose_Msg;
1170
1171    procedure Verbose_Msg
1172      (N1                : File_Name_Type;
1173       S1                : String;
1174       N2                : File_Name_Type := No_File;
1175       S2                : String  := "";
1176       Prefix            : String := "  -> ";
1177       Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
1178    is
1179    begin
1180       Verbose_Msg
1181         (Name_Id (N1), S1, Name_Id (N2), S2, Prefix, Minimum_Verbosity);
1182    end Verbose_Msg;
1183
1184 end Makeutl;