1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2004 Free Software Foundation, Inc. --
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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
28 with ALI.Util; use ALI.Util;
29 with Binderr; use Binderr;
30 with Butil; use Butil;
31 with Csets; use Csets;
32 with Fname; use Fname;
33 with Gnatvsn; use Gnatvsn;
34 with GNAT.OS_Lib; use GNAT.OS_Lib;
35 with Namet; use Namet;
37 with Osint; use Osint;
38 with Osint.L; use Osint.L;
39 with Output; use Output;
40 with Rident; use Rident;
41 with Targparm; use Targparm;
42 with Types; use Types;
44 with GNAT.Case_Util; use GNAT.Case_Util;
47 pragma Ident (Gnat_Static_Version_String);
49 Max_Column : constant := 80;
51 No_Obj : aliased String := "<no_obj>";
54 OK, -- matching timestamp
55 Checksum_OK, -- only matching checksum
56 Not_Found, -- file not found on source PATH
57 Not_Same, -- neither checksum nor timestamp matching
58 Not_First_On_PATH); -- matching file hidden by Not_Same file on path
61 type Dir_Ref is access Dir_Data;
63 type Dir_Data is record
64 Value : String_Access;
69 First_Source_Dir : Dir_Ref;
70 Last_Source_Dir : Dir_Ref;
71 -- The list of source directories from the command line.
72 -- These directories are added using Osint.Add_Src_Search_Dir
73 -- after those of the GNAT Project File, if any.
75 First_Lib_Dir : Dir_Ref;
76 Last_Lib_Dir : Dir_Ref;
77 -- The list of object directories from the command line.
78 -- These directories are added using Osint.Add_Lib_Search_Dir
79 -- after those of the GNAT Project File, if any.
81 Main_File : File_Name_Type;
82 Ali_File : File_Name_Type;
83 Text : Text_Buffer_Ptr;
86 Too_Long : Boolean := False;
87 -- When True, lines are too long for multi-column output and each
88 -- item of information is on a different line.
90 Selective_Output : Boolean := False;
91 Print_Usage : Boolean := False;
92 Print_Unit : Boolean := True;
93 Print_Source : Boolean := True;
94 Print_Object : Boolean := True;
95 -- Flags controlling the form of the output
97 Dependable : Boolean := False; -- flag -d
98 Also_Predef : Boolean := False;
100 Unit_Start : Integer;
102 Source_Start : Integer;
103 Source_End : Integer;
104 Object_Start : Integer;
105 Object_End : Integer;
106 -- Various column starts and ends
108 Spaces : constant String (1 .. Max_Column) := (others => ' ');
110 RTS_Specified : String_Access := null;
111 -- Used to detect multiple use of --RTS= switch
113 -----------------------
114 -- Local Subprograms --
115 -----------------------
117 procedure Add_Lib_Dir (Dir : String; And_Save : Boolean);
118 -- Add an object directory, using Osint.Add_Lib_Search_Dir
119 -- if And_Save is False or keeping in the list First_Lib_Dir,
120 -- Last_Lib_Dir if And_Save is True.
122 procedure Add_Source_Dir (Dir : String; And_Save : Boolean);
123 -- Add a source directory, using Osint.Add_Src_Search_Dir
124 -- if And_Save is False or keeping in the list First_Source_Dir,
125 -- Last_Source_Dir if And_Save is True.
127 procedure Find_General_Layout;
128 -- Determine the structure of the output (multi columns or not, etc)
130 procedure Find_Status
131 (FS : in out File_Name_Type;
132 Stamp : Time_Stamp_Type;
134 Status : out File_Status);
135 -- Determine the file status (Status) of the file represented by FS
136 -- with the expected Stamp and checksum given as argument. FS will be
137 -- updated to the full file name if available.
139 function Corresponding_Sdep_Entry (A : ALI_Id; U : Unit_Id) return Sdep_Id;
140 -- Give the Sdep entry corresponding to the unit U in ali record A
142 procedure Output_Object (O : File_Name_Type);
143 -- Print out the name of the object when requested
145 procedure Output_Source (Sdep_I : Sdep_Id);
146 -- Print out the name and status of the source corresponding to this
149 procedure Output_Status (FS : File_Status; Verbose : Boolean);
150 -- Print out FS either in a coded form if verbose is false or in an
151 -- expanded form otherwise.
153 procedure Output_Unit (ALI : ALI_Id; U_Id : Unit_Id);
154 -- Print out information on the unit when requested
156 procedure Reset_Print;
157 -- Reset Print flags properly when selective output is chosen
159 procedure Scan_Ls_Arg (Argv : String; And_Save : Boolean);
160 -- Scan and process lser specific arguments. Argv is a single argument
163 -- Print usage message
165 function Image (Restriction : Restriction_Id) return String;
166 -- Returns the capitalized image of Restriction
172 procedure Add_Lib_Dir (Dir : String; And_Save : Boolean) is
175 if First_Lib_Dir = null then
178 (Value => new String'(Dir),
180 Last_Lib_Dir := First_Lib_Dir;
185 (Value => new String'(Dir),
187 Last_Lib_Dir := Last_Lib_Dir.Next;
191 Add_Lib_Search_Dir (Dir);
199 procedure Add_Source_Dir (Dir : String; And_Save : Boolean) is
202 if First_Source_Dir = null then
205 (Value => new String'(Dir),
207 Last_Source_Dir := First_Source_Dir;
210 Last_Source_Dir.Next :=
212 (Value => new String'(Dir),
214 Last_Source_Dir := Last_Source_Dir.Next;
218 Add_Src_Search_Dir (Dir);
222 ------------------------------
223 -- Corresponding_Sdep_Entry --
224 ------------------------------
226 function Corresponding_Sdep_Entry
228 U : Unit_Id) return Sdep_Id
231 for D in ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep loop
232 if Sdep.Table (D).Sfile = Units.Table (U).Sfile then
237 Error_Msg_Name_1 := Units.Table (U).Uname;
238 Error_Msg_Name_2 := ALIs.Table (A).Afile;
240 Error_Msg ("wrong ALI format, can't find dependency line for & in %");
241 Exit_Program (E_Fatal);
242 end Corresponding_Sdep_Entry;
244 -------------------------
245 -- Find_General_Layout --
246 -------------------------
248 procedure Find_General_Layout is
249 Max_Unit_Length : Integer := 11;
250 Max_Src_Length : Integer := 11;
251 Max_Obj_Length : Integer := 11;
257 -- Compute maximum of each column
259 for Id in ALIs.First .. ALIs.Last loop
260 Get_Name_String (Units.Table (ALIs.Table (Id).First_Unit).Uname);
261 if Also_Predef or else not Is_Internal_Unit then
265 Max_Unit_Length := Integer'Max (Max_Unit_Length, Len);
269 FS := Full_Source_Name (ALIs.Table (Id).Sfile);
272 Get_Name_String (ALIs.Table (Id).Sfile);
273 Name_Len := Name_Len + 13;
275 Get_Name_String (FS);
278 Max_Src_Length := Integer'Max (Max_Src_Length, Name_Len + 1);
282 if ALIs.Table (Id).No_Object then
284 Integer'Max (Max_Obj_Length, No_Obj'Length);
286 Get_Name_String (ALIs.Table (Id).Ofile_Full_Name);
287 Max_Obj_Length := Integer'Max (Max_Obj_Length, Name_Len + 1);
293 -- Verify is output is not wider than maximum number of columns
298 (Max_Unit_Length + Max_Src_Length + Max_Obj_Length) > Max_Column;
300 -- Set start and end of columns
303 Object_End := Object_Start - 1;
306 Object_End := Object_Start + Max_Obj_Length;
309 Unit_Start := Object_End + 1;
310 Unit_End := Unit_Start - 1;
313 Unit_End := Unit_Start + Max_Unit_Length;
316 Source_Start := Unit_End + 1;
318 if Source_Start > Spaces'Last then
319 Source_Start := Spaces'Last;
322 Source_End := Source_Start - 1;
325 Source_End := Source_Start + Max_Src_Length;
327 end Find_General_Layout;
333 procedure Find_Status
334 (FS : in out File_Name_Type;
335 Stamp : Time_Stamp_Type;
337 Status : out File_Status)
339 Tmp1 : File_Name_Type;
340 Tmp2 : File_Name_Type;
343 Tmp1 := Full_Source_Name (FS);
345 if Tmp1 = No_File then
348 elsif File_Stamp (Tmp1) = Stamp then
352 elsif Checksums_Match (Get_File_Checksum (FS), Checksum) then
354 Status := Checksum_OK;
357 Tmp2 := Matching_Full_Source_Name (FS, Stamp);
359 if Tmp2 = No_File then
364 Status := Not_First_On_PATH;
374 function Image (Restriction : Restriction_Id) return String is
375 Result : String := Restriction'Img;
376 Skip : Boolean := True;
379 for J in Result'Range loop
382 Result (J) := To_Upper (Result (J));
384 elsif Result (J) = '_' then
388 Result (J) := To_Lower (Result (J));
399 procedure Output_Object (O : File_Name_Type) is
400 Object_Name : String_Access;
406 Object_Name := To_Host_File_Spec (Name_Buffer (1 .. Name_Len));
408 Object_Name := No_Obj'Unchecked_Access;
411 Write_Str (Object_Name.all);
413 if Print_Source or else Print_Unit then
419 (Object_Start + Object_Name'Length .. Object_End));
429 procedure Output_Source (Sdep_I : Sdep_Id) is
430 Stamp : constant Time_Stamp_Type := Sdep.Table (Sdep_I).Stamp;
431 Checksum : constant Word := Sdep.Table (Sdep_I).Checksum;
432 FS : File_Name_Type := Sdep.Table (Sdep_I).Sfile;
433 Status : File_Status;
434 Object_Name : String_Access;
438 Find_Status (FS, Stamp, Checksum, Status);
439 Get_Name_String (FS);
441 Object_Name := To_Host_File_Spec (Name_Buffer (1 .. Name_Len));
444 Write_Str (" Source => ");
445 Write_Str (Object_Name.all);
449 (Spaces (Source_Start + Object_Name'Length .. Source_End));
452 Output_Status (Status, Verbose => True);
457 if not Selective_Output then
458 Output_Status (Status, Verbose => False);
461 Write_Str (Object_Name.all);
470 procedure Output_Status (FS : File_Status; Verbose : Boolean) is
475 Write_Str (" unchanged");
478 Write_Str (" slightly modified");
481 Write_Str (" file not found");
484 Write_Str (" modified");
486 when Not_First_On_PATH =>
487 Write_Str (" unchanged version not first on PATH");
504 when Not_First_On_PATH =>
514 procedure Output_Unit (ALI : ALI_Id; U_Id : Unit_Id) is
516 U : Unit_Record renames Units.Table (U_Id);
520 Get_Name_String (U.Uname);
521 Kind := Name_Buffer (Name_Len);
522 Name_Len := Name_Len - 2;
524 if not Verbose_Mode then
525 Write_Str (Name_Buffer (1 .. Name_Len));
528 Write_Str ("Unit => ");
529 Write_Eol; Write_Str (" Name => ");
530 Write_Str (Name_Buffer (1 .. Name_Len));
531 Write_Eol; Write_Str (" Kind => ");
533 if Units.Table (U_Id).Unit_Kind = 'p' then
534 Write_Str ("package ");
536 Write_Str ("subprogram ");
560 U.Body_Needed_For_SAL or
563 Write_Eol; Write_Str (" Flags =>");
566 Write_Str (" Preelaborable");
570 Write_Str (" No_Elab_Code");
577 if U.Dynamic_Elab then
578 Write_Str (" Dynamic_Elab");
582 Write_Str (" Has_RACW");
585 if U.Remote_Types then
586 Write_Str (" Remote_Types");
589 if U.Shared_Passive then
590 Write_Str (" Shared_Passive");
598 Write_Str (" Predefined");
602 Write_Str (" Internal");
606 Write_Str (" Is_Generic");
609 if U.Init_Scalars then
610 Write_Str (" Init_Scalars");
614 Write_Str (" Interface");
617 if U.Body_Needed_For_SAL then
618 Write_Str (" Body_Needed_For_SAL");
621 if U.Elaborate_Body then
622 Write_Str (" Elaborate Body");
625 if U.Remote_Types then
626 Write_Str (" Remote_Types");
629 if U.Shared_Passive then
630 Write_Str (" Shared_Passive");
634 Write_Str (" Predefined");
640 Restrictions : constant Restrictions_Info :=
641 ALIs.Table (ALI).Restrictions;
643 -- If the source was compiled with pragmas Restrictions,
644 -- Display these restrictions.
646 if Restrictions.Set /= (All_Restrictions => False) then
647 Write_Eol; Write_Str (" Restrictions =>");
649 -- For boolean restrictions, just display the name of the
650 -- restriction; for valued restrictions, also display the
651 -- restriction value.
653 for Restriction in All_Restrictions loop
654 if Restrictions.Set (Restriction) then
657 Write_Str (Image (Restriction));
659 if Restriction in All_Parameter_Restrictions then
661 Write_Str (Restrictions.Value (Restriction)'Img);
671 Write_Eol; Write_Str (" ");
673 Write_Str (Spaces (Unit_Start + Name_Len + 1 .. Unit_End));
683 procedure Reset_Print is
685 if not Selective_Output then
686 Selective_Output := True;
687 Print_Source := False;
688 Print_Object := False;
697 procedure Scan_Ls_Arg (Argv : String; And_Save : Boolean) is
699 pragma Assert (Argv'First = 1);
701 if Argv'Length = 0 then
705 if Argv (1) = '-' then
707 if Argv'Length = 1 then
708 Fail ("switch character cannot be followed by a blank");
710 -- Processing for -I-
712 elsif Argv (2 .. Argv'Last) = "I-" then
713 Opt.Look_In_Primary_Dir := False;
715 -- Forbid -?- or -??- where ? is any character
717 elsif (Argv'Length = 3 and then Argv (3) = '-')
718 or else (Argv'Length = 4 and then Argv (4) = '-')
720 Fail ("Trailing ""-"" at the end of ", Argv, " forbidden.");
722 -- Processing for -Idir
724 elsif Argv (2) = 'I' then
725 Add_Source_Dir (Argv (3 .. Argv'Last), And_Save);
726 Add_Lib_Dir (Argv (3 .. Argv'Last), And_Save);
728 -- Processing for -aIdir (to gcc this is like a -I switch)
730 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aI" then
731 Add_Source_Dir (Argv (4 .. Argv'Last), And_Save);
733 -- Processing for -aOdir
735 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aO" then
736 Add_Lib_Dir (Argv (4 .. Argv'Last), And_Save);
738 -- Processing for -aLdir (to gnatbind this is like a -aO switch)
740 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aL" then
741 Add_Lib_Dir (Argv (4 .. Argv'Last), And_Save);
743 -- Processing for -nostdinc
745 elsif Argv (2 .. Argv'Last) = "nostdinc" then
746 Opt.No_Stdinc := True;
748 -- Processing for one character switches
750 elsif Argv'Length = 2 then
752 when 'a' => Also_Predef := True;
753 when 'h' => Print_Usage := True;
754 when 'u' => Reset_Print; Print_Unit := True;
755 when 's' => Reset_Print; Print_Source := True;
756 when 'o' => Reset_Print; Print_Object := True;
757 when 'v' => Verbose_Mode := True;
758 when 'd' => Dependable := True;
763 -- Processing for --RTS=path
765 elsif Argv'Length >= 5 and then Argv (1 .. 5) = "--RTS" then
766 if Argv'Length <= 6 or else Argv (6) /= '='then
767 Osint.Fail ("missing path for --RTS");
770 -- Check that it is the first time we see this switch or, if
771 -- it is not the first time, the same path is specified.
773 if RTS_Specified = null then
774 RTS_Specified := new String'(Argv (7 .. Argv'Last));
776 elsif RTS_Specified.all /= Argv (7 .. Argv'Last) then
777 Osint.Fail ("--RTS cannot be specified multiple times");
780 -- Valid --RTS switch
782 Opt.No_Stdinc := True;
783 Opt.RTS_Switch := True;
786 Src_Path_Name : constant String_Ptr :=
789 (Argv (7 .. Argv'Last), Include));
790 Lib_Path_Name : constant String_Ptr :=
793 (Argv (7 .. Argv'Last), Objects));
796 if Src_Path_Name /= null
797 and then Lib_Path_Name /= null
799 Add_Search_Dirs (Src_Path_Name, Include);
800 Add_Search_Dirs (Lib_Path_Name, Objects);
802 elsif Src_Path_Name = null
803 and then Lib_Path_Name = null
805 Osint.Fail ("RTS path not valid: missing " &
806 "adainclude and adalib directories");
808 elsif Src_Path_Name = null then
809 Osint.Fail ("RTS path not valid: missing " &
810 "adainclude directory");
812 elsif Lib_Path_Name = null then
813 Osint.Fail ("RTS path not valid: missing " &
820 -- If not a switch, it must be a file name
833 -- Start of processing for Usage
838 Write_Str ("Usage: ");
839 Osint.Write_Program_Name;
840 Write_Str (" switches [list of object files]");
846 Write_Str ("switches:");
851 Write_Str (" -a also output relevant predefined units");
856 Write_Str (" -u output only relevant unit names");
861 Write_Str (" -h output this help message");
866 Write_Str (" -s output only relevant source names");
871 Write_Str (" -o output only relevant object names");
876 Write_Str (" -d output sources on which specified units depend");
881 Write_Str (" -v verbose output, full path and unit information");
885 -- Line for -aI switch
887 Write_Str (" -aIdir specify source files search path");
890 -- Line for -aO switch
892 Write_Str (" -aOdir specify object files search path");
895 -- Line for -I switch
897 Write_Str (" -Idir like -aIdir -aOdir");
900 -- Line for -I- switch
902 Write_Str (" -I- do not look for sources & object files");
903 Write_Str (" in the default directory");
906 -- Line for -nostdinc
908 Write_Str (" -nostdinc do not look for source files");
909 Write_Str (" in the system default directory");
914 Write_Str (" --RTS=dir specify the default source and object search"
918 -- File Status explanation
921 Write_Str (" file status can be:");
924 for ST in File_Status loop
926 Output_Status (ST, Verbose => False);
928 Output_Status (ST, Verbose => True);
934 -- Start of processing for Gnatls
937 -- Initialize standard packages
942 -- Loop to scan out arguments
945 Scan_Args : while Next_Arg < Arg_Count loop
947 Next_Argv : String (1 .. Len_Arg (Next_Arg));
949 Fill_Arg (Next_Argv'Address, Next_Arg);
950 Scan_Ls_Arg (Next_Argv, And_Save => True);
953 Next_Arg := Next_Arg + 1;
956 -- Add the source and object directories specified on the
957 -- command line, if any, to the searched directories.
959 while First_Source_Dir /= null loop
960 Add_Src_Search_Dir (First_Source_Dir.Value.all);
961 First_Source_Dir := First_Source_Dir.Next;
964 while First_Lib_Dir /= null loop
965 Add_Lib_Search_Dir (First_Lib_Dir.Value.all);
966 First_Lib_Dir := First_Lib_Dir.Next;
969 -- Finally, add the default directories and obtain target parameters
971 Osint.Add_Default_Search_Dirs;
974 Targparm.Get_Target_Parameters;
976 -- WARNING: the output of gnatls -v is used during the compilation
977 -- and installation of GLADE to recreate sdefault.adb and locate
978 -- the libgnat.a to use. Any change in the output of gnatls -v must
979 -- be synchronized with the GLADE Dist/config.sdefault shell script.
982 Write_Str ("GNATLS ");
983 Write_Str (Gnat_Version_String);
984 Write_Str (" Copyright 1997-2004 Free Software Foundation, Inc.");
987 Write_Str ("Source Search Path:");
990 for J in 1 .. Nb_Dir_In_Src_Search_Path loop
993 if Dir_In_Src_Search_Path (J)'Length = 0 then
994 Write_Str ("<Current_Directory>");
996 Write_Str (To_Host_Dir_Spec
997 (Dir_In_Src_Search_Path (J).all, True).all);
1005 Write_Str ("Object Search Path:");
1008 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
1011 if Dir_In_Obj_Search_Path (J)'Length = 0 then
1012 Write_Str ("<Current_Directory>");
1014 Write_Str (To_Host_Dir_Spec
1015 (Dir_In_Obj_Search_Path (J).all, True).all);
1024 -- Output usage information when requested
1030 if not More_Lib_Files then
1031 if not Print_Usage and then not Verbose_Mode then
1035 Exit_Program (E_Fatal);
1039 Initialize_ALI_Source;
1041 -- Print out all library for which no ALI files can be located
1043 while More_Lib_Files loop
1044 Main_File := Next_Main_Lib_File;
1045 Ali_File := Full_Lib_File_Name (Lib_File_Name (Main_File));
1047 if Ali_File = No_File then
1048 Write_Str ("Can't find library info for ");
1049 Get_Name_String (Main_File);
1051 Write_Str (Name_Buffer (1 .. Name_Len));
1056 Ali_File := Strip_Directory (Ali_File);
1058 if Get_Name_Table_Info (Ali_File) = 0 then
1059 Text := Read_Library_Info (Ali_File, True);
1063 pragma Unreferenced (Discard);
1071 Ignore_Errors => True);
1079 Find_General_Layout;
1081 for Id in ALIs.First .. ALIs.Last loop
1086 Get_Name_String (Units.Table (ALIs.Table (Id).First_Unit).Uname);
1088 if Also_Predef or else not Is_Internal_Unit then
1089 if ALIs.Table (Id).No_Object then
1090 Output_Object (No_File);
1092 Output_Object (ALIs.Table (Id).Ofile_Full_Name);
1095 -- In verbose mode print all main units in the ALI file, otherwise
1096 -- just print the first one to ease columnwise printout
1098 if Verbose_Mode then
1099 Last_U := ALIs.Table (Id).Last_Unit;
1101 Last_U := ALIs.Table (Id).First_Unit;
1104 for U in ALIs.Table (Id).First_Unit .. Last_U loop
1105 if U /= ALIs.Table (Id).First_Unit
1106 and then Selective_Output
1112 Output_Unit (Id, U);
1114 -- Output source now, unless if it will be done as part of
1115 -- outputing dependencies.
1117 if not (Dependable and then Print_Source) then
1118 Output_Source (Corresponding_Sdep_Entry (Id, U));
1122 -- Print out list of units on which this unit depends (D lines)
1124 if Dependable and then Print_Source then
1125 if Verbose_Mode then
1126 Write_Str ("depends upon");
1135 ALIs.Table (Id).First_Sdep .. ALIs.Table (Id).Last_Sdep
1138 or else not Is_Internal_File_Name (Sdep.Table (D).Sfile)
1140 if Verbose_Mode then
1150 Write_Str (Spaces (1 .. Source_Start - 2));
1163 -- All done. Set proper exit status
1166 Exit_Program (E_Success);