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 Targparm; use Targparm;
41 with Types; use Types;
44 pragma Ident (Gnat_Static_Version_String);
46 Max_Column : constant := 80;
48 No_Obj : aliased String := "<no_obj>";
51 OK, -- matching timestamp
52 Checksum_OK, -- only matching checksum
53 Not_Found, -- file not found on source PATH
54 Not_Same, -- neither checksum nor timestamp matching
55 Not_First_On_PATH); -- matching file hidden by Not_Same file on path
58 type Dir_Ref is access Dir_Data;
60 type Dir_Data is record
61 Value : String_Access;
66 First_Source_Dir : Dir_Ref;
67 Last_Source_Dir : Dir_Ref;
68 -- The list of source directories from the command line.
69 -- These directories are added using Osint.Add_Src_Search_Dir
70 -- after those of the GNAT Project File, if any.
72 First_Lib_Dir : Dir_Ref;
73 Last_Lib_Dir : Dir_Ref;
74 -- The list of object directories from the command line.
75 -- These directories are added using Osint.Add_Lib_Search_Dir
76 -- after those of the GNAT Project File, if any.
78 Main_File : File_Name_Type;
79 Ali_File : File_Name_Type;
80 Text : Text_Buffer_Ptr;
83 Too_Long : Boolean := False;
84 -- When True, lines are too long for multi-column output and each
85 -- item of information is on a different line.
87 Selective_Output : Boolean := False;
88 Print_Usage : Boolean := False;
89 Print_Unit : Boolean := True;
90 Print_Source : Boolean := True;
91 Print_Object : Boolean := True;
92 -- Flags controlling the form of the output
94 Dependable : Boolean := False; -- flag -d
95 Also_Predef : Boolean := False;
99 Source_Start : Integer;
100 Source_End : Integer;
101 Object_Start : Integer;
102 Object_End : Integer;
103 -- Various column starts and ends
105 Spaces : constant String (1 .. Max_Column) := (others => ' ');
107 RTS_Specified : String_Access := null;
108 -- Used to detect multiple use of --RTS= switch
110 -----------------------
111 -- Local Subprograms --
112 -----------------------
114 procedure Add_Lib_Dir (Dir : String; And_Save : Boolean);
115 -- Add an object directory, using Osint.Add_Lib_Search_Dir
116 -- if And_Save is False or keeping in the list First_Lib_Dir,
117 -- Last_Lib_Dir if And_Save is True.
119 procedure Add_Source_Dir (Dir : String; And_Save : Boolean);
120 -- Add a source directory, using Osint.Add_Src_Search_Dir
121 -- if And_Save is False or keeping in the list First_Source_Dir,
122 -- Last_Source_Dir if And_Save is True.
124 procedure Find_General_Layout;
125 -- Determine the structure of the output (multi columns or not, etc)
127 procedure Find_Status
128 (FS : in out File_Name_Type;
129 Stamp : Time_Stamp_Type;
131 Status : out File_Status);
132 -- Determine the file status (Status) of the file represented by FS
133 -- with the expected Stamp and checksum given as argument. FS will be
134 -- updated to the full file name if available.
136 function Corresponding_Sdep_Entry (A : ALI_Id; U : Unit_Id) return Sdep_Id;
137 -- Give the Sdep entry corresponding to the unit U in ali record A
139 procedure Output_Object (O : File_Name_Type);
140 -- Print out the name of the object when requested
142 procedure Output_Source (Sdep_I : Sdep_Id);
143 -- Print out the name and status of the source corresponding to this
146 procedure Output_Status (FS : File_Status; Verbose : Boolean);
147 -- Print out FS either in a coded form if verbose is false or in an
148 -- expanded form otherwise.
150 procedure Output_Unit (U_Id : Unit_Id);
151 -- Print out information on the unit when requested
153 procedure Reset_Print;
154 -- Reset Print flags properly when selective output is chosen
156 procedure Scan_Ls_Arg (Argv : String; And_Save : Boolean);
157 -- Scan and process lser specific arguments. Argv is a single argument
160 -- Print usage message
166 procedure Add_Lib_Dir (Dir : String; And_Save : Boolean) is
169 if First_Lib_Dir = null then
172 (Value => new String'(Dir),
174 Last_Lib_Dir := First_Lib_Dir;
179 (Value => new String'(Dir),
181 Last_Lib_Dir := Last_Lib_Dir.Next;
185 Add_Lib_Search_Dir (Dir);
193 procedure Add_Source_Dir (Dir : String; And_Save : Boolean) is
196 if First_Source_Dir = null then
199 (Value => new String'(Dir),
201 Last_Source_Dir := First_Source_Dir;
204 Last_Source_Dir.Next :=
206 (Value => new String'(Dir),
208 Last_Source_Dir := Last_Source_Dir.Next;
212 Add_Src_Search_Dir (Dir);
216 ------------------------------
217 -- Corresponding_Sdep_Entry --
218 ------------------------------
220 function Corresponding_Sdep_Entry
222 U : Unit_Id) return Sdep_Id
225 for D in ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep loop
226 if Sdep.Table (D).Sfile = Units.Table (U).Sfile then
231 Error_Msg_Name_1 := Units.Table (U).Uname;
232 Error_Msg_Name_2 := ALIs.Table (A).Afile;
234 Error_Msg ("wrong ALI format, can't find dependency line for & in %");
235 Exit_Program (E_Fatal);
236 end Corresponding_Sdep_Entry;
238 -------------------------
239 -- Find_General_Layout --
240 -------------------------
242 procedure Find_General_Layout is
243 Max_Unit_Length : Integer := 11;
244 Max_Src_Length : Integer := 11;
245 Max_Obj_Length : Integer := 11;
251 -- Compute maximum of each column
253 for Id in ALIs.First .. ALIs.Last loop
254 Get_Name_String (Units.Table (ALIs.Table (Id).First_Unit).Uname);
255 if Also_Predef or else not Is_Internal_Unit then
259 Max_Unit_Length := Integer'Max (Max_Unit_Length, Len);
263 FS := Full_Source_Name (ALIs.Table (Id).Sfile);
266 Get_Name_String (ALIs.Table (Id).Sfile);
267 Name_Len := Name_Len + 13;
269 Get_Name_String (FS);
272 Max_Src_Length := Integer'Max (Max_Src_Length, Name_Len + 1);
276 if ALIs.Table (Id).No_Object then
278 Integer'Max (Max_Obj_Length, No_Obj'Length);
280 Get_Name_String (ALIs.Table (Id).Ofile_Full_Name);
281 Max_Obj_Length := Integer'Max (Max_Obj_Length, Name_Len + 1);
287 -- Verify is output is not wider than maximum number of columns
292 (Max_Unit_Length + Max_Src_Length + Max_Obj_Length) > Max_Column;
294 -- Set start and end of columns
297 Object_End := Object_Start - 1;
300 Object_End := Object_Start + Max_Obj_Length;
303 Unit_Start := Object_End + 1;
304 Unit_End := Unit_Start - 1;
307 Unit_End := Unit_Start + Max_Unit_Length;
310 Source_Start := Unit_End + 1;
312 if Source_Start > Spaces'Last then
313 Source_Start := Spaces'Last;
316 Source_End := Source_Start - 1;
319 Source_End := Source_Start + Max_Src_Length;
321 end Find_General_Layout;
327 procedure Find_Status
328 (FS : in out File_Name_Type;
329 Stamp : Time_Stamp_Type;
331 Status : out File_Status)
333 Tmp1 : File_Name_Type;
334 Tmp2 : File_Name_Type;
337 Tmp1 := Full_Source_Name (FS);
339 if Tmp1 = No_File then
342 elsif File_Stamp (Tmp1) = Stamp then
346 elsif Checksums_Match (Get_File_Checksum (FS), Checksum) then
348 Status := Checksum_OK;
351 Tmp2 := Matching_Full_Source_Name (FS, Stamp);
353 if Tmp2 = No_File then
358 Status := Not_First_On_PATH;
368 procedure Output_Object (O : File_Name_Type) is
369 Object_Name : String_Access;
375 Object_Name := To_Host_File_Spec (Name_Buffer (1 .. Name_Len));
377 Object_Name := No_Obj'Unchecked_Access;
380 Write_Str (Object_Name.all);
382 if Print_Source or else Print_Unit then
388 (Object_Start + Object_Name'Length .. Object_End));
398 procedure Output_Source (Sdep_I : Sdep_Id) is
399 Stamp : constant Time_Stamp_Type := Sdep.Table (Sdep_I).Stamp;
400 Checksum : constant Word := Sdep.Table (Sdep_I).Checksum;
401 FS : File_Name_Type := Sdep.Table (Sdep_I).Sfile;
402 Status : File_Status;
403 Object_Name : String_Access;
407 Find_Status (FS, Stamp, Checksum, Status);
408 Get_Name_String (FS);
410 Object_Name := To_Host_File_Spec (Name_Buffer (1 .. Name_Len));
413 Write_Str (" Source => ");
414 Write_Str (Object_Name.all);
418 (Spaces (Source_Start + Object_Name'Length .. Source_End));
421 Output_Status (Status, Verbose => True);
426 if not Selective_Output then
427 Output_Status (Status, Verbose => False);
430 Write_Str (Object_Name.all);
439 procedure Output_Status (FS : File_Status; Verbose : Boolean) is
444 Write_Str (" unchanged");
447 Write_Str (" slightly modified");
450 Write_Str (" file not found");
453 Write_Str (" modified");
455 when Not_First_On_PATH =>
456 Write_Str (" unchanged version not first on PATH");
473 when Not_First_On_PATH =>
483 procedure Output_Unit (U_Id : Unit_Id) is
485 U : Unit_Record renames Units.Table (U_Id);
489 Get_Name_String (U.Uname);
490 Kind := Name_Buffer (Name_Len);
491 Name_Len := Name_Len - 2;
493 if not Verbose_Mode then
494 Write_Str (Name_Buffer (1 .. Name_Len));
497 Write_Str ("Unit => ");
498 Write_Eol; Write_Str (" Name => ");
499 Write_Str (Name_Buffer (1 .. Name_Len));
500 Write_Eol; Write_Str (" Kind => ");
502 if Units.Table (U_Id).Unit_Kind = 'p' then
503 Write_Str ("package ");
505 Write_Str ("subprogram ");
529 U.Body_Needed_For_SAL or
532 Write_Eol; Write_Str (" Flags =>");
535 Write_Str (" Preelaborable");
539 Write_Str (" No_Elab_Code");
546 if U.Dynamic_Elab then
547 Write_Str (" Dynamic_Elab");
551 Write_Str (" Has_RACW");
554 if U.Remote_Types then
555 Write_Str (" Remote_Types");
558 if U.Shared_Passive then
559 Write_Str (" Shared_Passive");
567 Write_Str (" Predefined");
571 Write_Str (" Internal");
575 Write_Str (" Is_Generic");
578 if U.Init_Scalars then
579 Write_Str (" Init_Scalars");
583 Write_Str (" Interface");
586 if U.Body_Needed_For_SAL then
587 Write_Str (" Body_Needed_For_SAL");
590 if U.Elaborate_Body then
591 Write_Str (" Elaborate Body");
594 if U.Remote_Types then
595 Write_Str (" Remote_Types");
598 if U.Shared_Passive then
599 Write_Str (" Shared_Passive");
603 Write_Str (" Predefined");
611 Write_Eol; Write_Str (" ");
613 Write_Str (Spaces (Unit_Start + Name_Len + 1 .. Unit_End));
623 procedure Reset_Print is
625 if not Selective_Output then
626 Selective_Output := True;
627 Print_Source := False;
628 Print_Object := False;
637 procedure Scan_Ls_Arg (Argv : String; And_Save : Boolean) is
639 pragma Assert (Argv'First = 1);
641 if Argv'Length = 0 then
645 if Argv (1) = '-' then
647 if Argv'Length = 1 then
648 Fail ("switch character cannot be followed by a blank");
650 -- Processing for -I-
652 elsif Argv (2 .. Argv'Last) = "I-" then
653 Opt.Look_In_Primary_Dir := False;
655 -- Forbid -?- or -??- where ? is any character
657 elsif (Argv'Length = 3 and then Argv (3) = '-')
658 or else (Argv'Length = 4 and then Argv (4) = '-')
660 Fail ("Trailing ""-"" at the end of ", Argv, " forbidden.");
662 -- Processing for -Idir
664 elsif Argv (2) = 'I' then
665 Add_Source_Dir (Argv (3 .. Argv'Last), And_Save);
666 Add_Lib_Dir (Argv (3 .. Argv'Last), And_Save);
668 -- Processing for -aIdir (to gcc this is like a -I switch)
670 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aI" then
671 Add_Source_Dir (Argv (4 .. Argv'Last), And_Save);
673 -- Processing for -aOdir
675 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aO" then
676 Add_Lib_Dir (Argv (4 .. Argv'Last), And_Save);
678 -- Processing for -aLdir (to gnatbind this is like a -aO switch)
680 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aL" then
681 Add_Lib_Dir (Argv (4 .. Argv'Last), And_Save);
683 -- Processing for -nostdinc
685 elsif Argv (2 .. Argv'Last) = "nostdinc" then
686 Opt.No_Stdinc := True;
688 -- Processing for one character switches
690 elsif Argv'Length = 2 then
692 when 'a' => Also_Predef := True;
693 when 'h' => Print_Usage := True;
694 when 'u' => Reset_Print; Print_Unit := True;
695 when 's' => Reset_Print; Print_Source := True;
696 when 'o' => Reset_Print; Print_Object := True;
697 when 'v' => Verbose_Mode := True;
698 when 'd' => Dependable := True;
703 -- Processing for --RTS=path
705 elsif Argv'Length >= 5 and then Argv (1 .. 5) = "--RTS" then
706 if Argv'Length <= 6 or else Argv (6) /= '='then
707 Osint.Fail ("missing path for --RTS");
710 -- Check that it is the first time we see this switch or, if
711 -- it is not the first time, the same path is specified.
713 if RTS_Specified = null then
714 RTS_Specified := new String'(Argv (7 .. Argv'Last));
716 elsif RTS_Specified.all /= Argv (7 .. Argv'Last) then
717 Osint.Fail ("--RTS cannot be specified multiple times");
720 -- Valid --RTS switch
722 Opt.No_Stdinc := True;
723 Opt.RTS_Switch := True;
726 Src_Path_Name : constant String_Ptr :=
729 (Argv (7 .. Argv'Last), Include));
730 Lib_Path_Name : constant String_Ptr :=
733 (Argv (7 .. Argv'Last), Objects));
736 if Src_Path_Name /= null
737 and then Lib_Path_Name /= null
739 Add_Search_Dirs (Src_Path_Name, Include);
740 Add_Search_Dirs (Lib_Path_Name, Objects);
742 elsif Src_Path_Name = null
743 and then Lib_Path_Name = null
745 Osint.Fail ("RTS path not valid: missing " &
746 "adainclude and adalib directories");
748 elsif Src_Path_Name = null then
749 Osint.Fail ("RTS path not valid: missing " &
750 "adainclude directory");
752 elsif Lib_Path_Name = null then
753 Osint.Fail ("RTS path not valid: missing " &
760 -- If not a switch, it must be a file name
773 -- Start of processing for Usage
778 Write_Str ("Usage: ");
779 Osint.Write_Program_Name;
780 Write_Str (" switches [list of object files]");
786 Write_Str ("switches:");
791 Write_Str (" -a also output relevant predefined units");
796 Write_Str (" -u output only relevant unit names");
801 Write_Str (" -h output this help message");
806 Write_Str (" -s output only relevant source names");
811 Write_Str (" -o output only relevant object names");
816 Write_Str (" -d output sources on which specified units depend");
821 Write_Str (" -v verbose output, full path and unit information");
825 -- Line for -aI switch
827 Write_Str (" -aIdir specify source files search path");
830 -- Line for -aO switch
832 Write_Str (" -aOdir specify object files search path");
835 -- Line for -I switch
837 Write_Str (" -Idir like -aIdir -aOdir");
840 -- Line for -I- switch
842 Write_Str (" -I- do not look for sources & object files");
843 Write_Str (" in the default directory");
846 -- Line for -nostdinc
848 Write_Str (" -nostdinc do not look for source files");
849 Write_Str (" in the system default directory");
854 Write_Str (" --RTS=dir specify the default source and object search"
858 -- File Status explanation
861 Write_Str (" file status can be:");
864 for ST in File_Status loop
866 Output_Status (ST, Verbose => False);
868 Output_Status (ST, Verbose => True);
874 -- Start of processing for Gnatls
877 -- Initialize standard packages
882 -- Loop to scan out arguments
885 Scan_Args : while Next_Arg < Arg_Count loop
887 Next_Argv : String (1 .. Len_Arg (Next_Arg));
889 Fill_Arg (Next_Argv'Address, Next_Arg);
890 Scan_Ls_Arg (Next_Argv, And_Save => True);
893 Next_Arg := Next_Arg + 1;
896 -- Add the source and object directories specified on the
897 -- command line, if any, to the searched directories.
899 while First_Source_Dir /= null loop
900 Add_Src_Search_Dir (First_Source_Dir.Value.all);
901 First_Source_Dir := First_Source_Dir.Next;
904 while First_Lib_Dir /= null loop
905 Add_Lib_Search_Dir (First_Lib_Dir.Value.all);
906 First_Lib_Dir := First_Lib_Dir.Next;
909 -- Finally, add the default directories and obtain target parameters
911 Osint.Add_Default_Search_Dirs;
914 Targparm.Get_Target_Parameters;
916 -- WARNING: the output of gnatls -v is used during the compilation
917 -- and installation of GLADE to recreate sdefault.adb and locate
918 -- the libgnat.a to use. Any change in the output of gnatls -v must
919 -- be synchronized with the GLADE Dist/config.sdefault shell script.
922 Write_Str ("GNATLS ");
923 Write_Str (Gnat_Version_String);
924 Write_Str (" Copyright 1997-2004 Free Software Foundation, Inc.");
927 Write_Str ("Source Search Path:");
930 for J in 1 .. Nb_Dir_In_Src_Search_Path loop
933 if Dir_In_Src_Search_Path (J)'Length = 0 then
934 Write_Str ("<Current_Directory>");
936 Write_Str (To_Host_Dir_Spec
937 (Dir_In_Src_Search_Path (J).all, True).all);
945 Write_Str ("Object Search Path:");
948 for J in 1 .. Nb_Dir_In_Obj_Search_Path loop
951 if Dir_In_Obj_Search_Path (J)'Length = 0 then
952 Write_Str ("<Current_Directory>");
954 Write_Str (To_Host_Dir_Spec
955 (Dir_In_Obj_Search_Path (J).all, True).all);
964 -- Output usage information when requested
970 if not More_Lib_Files then
971 if not Print_Usage and then not Verbose_Mode then
975 Exit_Program (E_Fatal);
979 Initialize_ALI_Source;
981 -- Print out all library for which no ALI files can be located
983 while More_Lib_Files loop
984 Main_File := Next_Main_Lib_File;
985 Ali_File := Full_Lib_File_Name (Lib_File_Name (Main_File));
987 if Ali_File = No_File then
988 Write_Str ("Can't find library info for ");
989 Get_Name_String (Main_File);
991 Write_Str (Name_Buffer (1 .. Name_Len));
996 Ali_File := Strip_Directory (Ali_File);
998 if Get_Name_Table_Info (Ali_File) = 0 then
999 Text := Read_Library_Info (Ali_File, True);
1003 pragma Unreferenced (Discard);
1011 Ignore_Errors => True);
1019 Find_General_Layout;
1021 for Id in ALIs.First .. ALIs.Last loop
1026 Get_Name_String (Units.Table (ALIs.Table (Id).First_Unit).Uname);
1028 if Also_Predef or else not Is_Internal_Unit then
1029 if ALIs.Table (Id).No_Object then
1030 Output_Object (No_File);
1032 Output_Object (ALIs.Table (Id).Ofile_Full_Name);
1035 -- In verbose mode print all main units in the ALI file, otherwise
1036 -- just print the first one to ease columnwise printout
1038 if Verbose_Mode then
1039 Last_U := ALIs.Table (Id).Last_Unit;
1041 Last_U := ALIs.Table (Id).First_Unit;
1044 for U in ALIs.Table (Id).First_Unit .. Last_U loop
1045 if U /= ALIs.Table (Id).First_Unit
1046 and then Selective_Output
1054 -- Output source now, unless if it will be done as part of
1055 -- outputing dependencies.
1057 if not (Dependable and then Print_Source) then
1058 Output_Source (Corresponding_Sdep_Entry (Id, U));
1062 -- Print out list of units on which this unit depends (D lines)
1064 if Dependable and then Print_Source then
1065 if Verbose_Mode then
1066 Write_Str ("depends upon");
1075 ALIs.Table (Id).First_Sdep .. ALIs.Table (Id).Last_Sdep
1078 or else not Is_Internal_File_Name (Sdep.Table (D).Sfile)
1080 if Verbose_Mode then
1090 Write_Str (Spaces (1 .. Source_Start - 2));
1103 -- All done. Set proper exit status
1106 Exit_Program (E_Success);