1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2003, 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 ------------------------------------------------------------------------------
31 with Namet; use Namet;
33 with Osint; use Osint;
34 with Osint.M; use Osint.M;
40 with Prj.Util; use Prj.Util;
44 with Types; use Types;
46 with GNAT.Command_Line; use GNAT.Command_Line;
47 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
48 with GNAT.IO; use GNAT.IO;
49 with GNAT.OS_Lib; use GNAT.OS_Lib;
54 Initialized : Boolean := False;
55 -- Set to True by the first call to Initialize.
56 -- To avoid reinitialization of some packages.
58 -- Suffixes of various files
60 Assembly_Suffix : constant String := ".s";
61 ALI_Suffix : constant String := ".ali";
62 Tree_Suffix : constant String := ".adt";
63 Object_Suffix : constant String := Get_Object_Suffix.all;
64 Debug_Suffix : String := ".dg";
65 -- Changed to "_dg" for VMS in the body of the package
67 Repinfo_Suffix : String := ".rep";
68 -- Changed to "_rep" for VMS in the body of the package
70 B_Start : String := "b~";
71 -- Prefix of binder generated file.
72 -- Changed to "b$" for VMS in the body of the package.
74 Object_Directory_Path : String_Access := null;
75 -- The path name of the object directory, set with switch -D
77 Do_Nothing : Boolean := False;
78 -- Set to True when switch -n is specified.
79 -- When True, no file is deleted. gnatclean only lists the files that
80 -- would have been deleted if the switch -n had not been specified.
82 File_Deleted : Boolean := False;
83 -- Set to True if at least one file has been deleted
85 Copyright_Displayed : Boolean := False;
86 Usage_Displayed : Boolean := False;
88 Project_File_Name : String_Access := null;
90 Main_Project : Prj.Project_Id := Prj.No_Project;
92 All_Projects : Boolean := False;
94 -- Packages of project files where unknown attributes are errors.
96 Naming_String : aliased String := "naming";
97 Builder_String : aliased String := "builder";
98 Compiler_String : aliased String := "compiler";
99 Binder_String : aliased String := "binder";
100 Linker_String : aliased String := "linker";
102 Gnatmake_Packages : aliased String_List :=
103 (Naming_String 'Access,
104 Builder_String 'Access,
105 Compiler_String 'Access,
106 Binder_String 'Access,
107 Linker_String 'Access);
109 Packages_To_Check_By_Gnatmake : constant String_List_Access :=
110 Gnatmake_Packages'Access;
112 package Processed_Projects is new Table.Table
113 (Table_Component_Type => Project_Id,
114 Table_Index_Type => Natural,
115 Table_Low_Bound => 0,
117 Table_Increment => 10,
118 Table_Name => "Clean.Processed_Projects");
119 -- Table to keep track of what project files have been processed, when
120 -- switch -r is specified.
122 package Sources is new Table.Table
123 (Table_Component_Type => File_Name_Type,
124 Table_Index_Type => Natural,
125 Table_Low_Bound => 0,
127 Table_Increment => 10,
128 Table_Name => "Clean.Processed_Projects");
129 -- Table to store all the source files of a library unit: spec, body and
130 -- subunits, to detect .dg files and delete them.
132 ----------------------------
133 -- Queue (Q) manipulation --
134 ----------------------------
137 -- Must be called to initialize the Q
140 (Source_File : File_Name_Type);
141 -- If Source_File is not marked, inserts it at the end of Q and mark it
143 function Empty_Q return Boolean;
144 -- Returns True if Q is empty.
146 procedure Extract_From_Q
147 (Source_File : out File_Name_Type);
148 -- Extracts the first element from the Q.
151 -- Points to the first valid element in the Q.
153 package Q is new Table.Table (
154 Table_Component_Type => File_Name_Type,
155 Table_Index_Type => Natural,
156 Table_Low_Bound => 0,
157 Table_Initial => 4000,
158 Table_Increment => 100,
159 Table_Name => "Clean.Q");
160 -- This is the actual queue
162 -----------------------------
163 -- Other local subprograms --
164 -----------------------------
166 procedure Add_Source_Dir (N : String);
167 -- Call Add_Src_Search_Dir.
168 -- Output one line when in verbose mode.
170 procedure Add_Source_Directories is
171 new Prj.Env.For_All_Source_Dirs (Action => Add_Source_Dir);
173 procedure Add_Object_Dir (N : String);
174 -- Call Add_Lib_Search_Dir.
175 -- Output one line when in verbose mode.
177 procedure Add_Object_Directories is
178 new Prj.Env.For_All_Object_Dirs (Action => Add_Object_Dir);
180 function ALI_File_Name (Source : Name_Id) return String;
181 -- Returns the name of the ALI file corresponding to Source
183 function Assembly_File_Name (Source : Name_Id) return String;
184 -- Returns the assembly file name corresponding to Source
186 procedure Clean_Directory (Dir : Name_Id);
187 -- Delete all regular files in a library directory or in a library
190 procedure Clean_Executables;
191 -- Do the cleaning work when no project file is specified
193 procedure Clean_Project (Project : Project_Id);
194 -- Do the cleaning work when a project file is specified.
195 -- This procedure calls itself recursively when there are several
196 -- project files in the tree rooted at the main project file and switch -r
197 -- has been specified.
199 function Debug_File_Name (Source : Name_Id) return String;
200 -- Name of the expanded source file corresponding to Source
202 procedure Delete (In_Directory : String; File : String);
203 -- Delete one file, or list the file name if switch -n is specified
205 procedure Delete_Binder_Generated_Files (Dir : String; Source : Name_Id);
206 -- Delete the binder generated file in directory Dir for Source, if they
207 -- exist: for Unix these are b~<source>.ads, b~<source>.adb,
208 -- b~<source>.ali and b~<source>.o.
210 procedure Display_Copyright;
211 -- Display the Copyright notice.
212 -- If called several times, display the Copyright notice only the first
215 procedure Initialize;
216 -- Call the necessary package initializations
218 function Object_File_Name (Source : Name_Id) return String;
219 -- Returns the object file name corresponding to Source
221 procedure Parse_Cmd_Line;
222 -- Parse the command line
224 function Repinfo_File_Name (Source : Name_Id) return String;
225 -- Returns the repinfo file name corresponding to Source
227 function Tree_File_Name (Source : Name_Id) return String;
228 -- Returns the tree file name corresponding to Source
230 function In_Extension_Chain
231 (Of_Project : Project_Id;
232 Prj : Project_Id) return Boolean;
233 -- Returns True iff Prj is an extension of Of_Project or if Of_Project is
234 -- an extension of Prj.
237 -- Display the usage.
238 -- If called several times, the usage is displayed only the first time.
244 procedure Add_Object_Dir (N : String) is
246 Add_Lib_Search_Dir (N);
248 if Opt.Verbose_Mode then
249 Put ("Adding object directory """);
260 procedure Add_Source_Dir (N : String) is
262 Add_Src_Search_Dir (N);
264 if Opt.Verbose_Mode then
265 Put ("Adding source directory """);
276 function ALI_File_Name (Source : Name_Id) return String is
277 Src : constant String := Get_Name_String (Source);
280 -- If the source name has an extension, then replace it with
283 for Index in reverse Src'First + 1 .. Src'Last loop
284 if Src (Index) = '.' then
285 return Src (Src'First .. Index - 1) & ALI_Suffix;
289 -- If there is no dot, or if it is the first character, just add the
292 return Src & ALI_Suffix;
295 ------------------------
296 -- Assembly_File_Name --
297 ------------------------
299 function Assembly_File_Name (Source : Name_Id) return String is
300 Src : constant String := Get_Name_String (Source);
303 -- If the source name has an extension, then replace it with
304 -- the assembly suffix.
306 for Index in reverse Src'First + 1 .. Src'Last loop
307 if Src (Index) = '.' then
308 return Src (Src'First .. Index - 1) & Assembly_Suffix;
312 -- If there is no dot, or if it is the first character, just add the
315 return Src & Assembly_Suffix;
316 end Assembly_File_Name;
318 ---------------------
319 -- Clean_Directory --
320 ---------------------
322 procedure Clean_Directory (Dir : Name_Id) is
323 Directory : constant String := Get_Name_String (Dir);
324 Current : constant Dir_Name_Str := Get_Current_Dir;
328 Name : String (1 .. 200);
331 procedure Set_Writable (Name : System.Address);
332 pragma Import (C, Set_Writable, "__gnat_set_writable");
335 Change_Dir (Directory);
338 -- For each regular file in the directory, if switch -n has not been
339 -- specified, make it writable and delete the file.
342 Read (Direc, Name, Last);
345 if Is_Regular_File (Name (1 .. Last)) then
346 if not Do_Nothing then
347 Name (Last + 1) := ASCII.NUL;
348 Set_Writable (Name (1)'Address);
351 Delete (Directory, Name (1 .. Last));
357 -- Restore the initial working directory
359 Change_Dir (Current);
362 -----------------------
363 -- Clean_Executables --
364 -----------------------
366 procedure Clean_Executables is
367 Main_Source_File : File_Name_Type;
368 -- Current main source
370 Source_File : File_Name_Type;
371 -- Current source file
373 Full_Source_File : File_Name_Type;
374 -- Full name of the current source file
376 Lib_File : File_Name_Type;
377 -- Current library file
379 Full_Lib_File : File_Name_Type;
380 -- Full name of the current library file
382 Text : Text_Buffer_Ptr;
388 -- It does not really matter if there is or not an object file
389 -- corresponding to an ALI file: if there is one, it will be deleted.
391 Opt.Check_Object_Consistency := False;
393 -- Proceed each executable one by one. Each source is marked as it is
394 -- processed, so common sources between executables will not be
395 -- processed several times.
397 for N_File in 1 .. Osint.Number_Of_Files loop
398 Main_Source_File := Next_Main_Source;
399 Insert_Q (Main_Source_File);
401 while not Empty_Q loop
402 Sources.Set_Last (0);
403 Extract_From_Q (Source_File);
404 Full_Source_File := Osint.Full_Source_Name (Source_File);
405 Lib_File := Osint.Lib_File_Name (Source_File);
406 Full_Lib_File := Osint.Full_Lib_File_Name (Lib_File);
408 -- If we have an existing ALI file that is not read-only,
411 if Full_Lib_File /= No_File
412 and then not Is_Readonly_Library (Full_Lib_File)
414 Text := Read_Library_Info (Lib_File);
418 Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
421 -- If no error was produced while loading this ALI file,
422 -- insert into the queue all the unmarked withed sources.
424 if The_ALI /= No_ALI_Id then
425 for J in ALIs.Table (The_ALI).First_Unit ..
426 ALIs.Table (The_ALI).Last_Unit
428 Sources.Increment_Last;
429 Sources.Table (Sources.Last) :=
430 ALI.Units.Table (J).Sfile;
432 for K in ALI.Units.Table (J).First_With ..
433 ALI.Units.Table (J).Last_With
435 Insert_Q (Withs.Table (K).Sfile);
439 -- Look for subunits and put them in the Sources table
441 for J in ALIs.Table (The_ALI).First_Sdep ..
442 ALIs.Table (The_ALI).Last_Sdep
444 if Sdep.Table (J).Subunit_Name /= No_Name then
445 Sources.Increment_Last;
446 Sources.Table (Sources.Last) :=
447 Sdep.Table (J).Sfile;
453 -- Now, delete all the existing files corresponding to this
457 Obj_Dir : constant String :=
458 Dir_Name (Get_Name_String (Full_Lib_File));
459 Obj : constant String := Object_File_Name (Lib_File);
460 Adt : constant String := Tree_File_Name (Lib_File);
461 Asm : constant String := Assembly_File_Name (Lib_File);
464 Delete (Obj_Dir, Get_Name_String (Lib_File));
466 if Is_Regular_File (Obj_Dir & Dir_Separator & Obj) then
467 Delete (Obj_Dir, Obj);
470 if Is_Regular_File (Obj_Dir & Dir_Separator & Adt) then
471 Delete (Obj_Dir, Adt);
474 if Is_Regular_File (Obj_Dir & Dir_Separator & Asm) then
475 Delete (Obj_Dir, Asm);
478 -- Delete expanded source files (.dg) and/or repinfo files
481 for J in 1 .. Sources.Last loop
483 Deb : constant String :=
484 Debug_File_Name (Sources.Table (J));
485 Rep : constant String :=
486 Repinfo_File_Name (Sources.Table (J));
488 if Is_Regular_File (Obj_Dir & Dir_Separator & Deb) then
489 Delete (Obj_Dir, Deb);
492 if Is_Regular_File (Obj_Dir & Dir_Separator & Rep) then
493 Delete (Obj_Dir, Rep);
501 -- Delete the executable, if it exists, and the binder generated
504 if not Compile_Only then
506 Source : constant Name_Id := Strip_Suffix (Main_Source_File);
507 Executable : constant String := Get_Name_String
508 (Executable_Name (Source));
510 if Is_Regular_File (Executable) then
511 Delete ("", Executable);
514 Delete_Binder_Generated_Files (Get_Current_Dir, Source);
518 end Clean_Executables;
524 procedure Clean_Project (Project : Project_Id) is
525 Main_Source_File : File_Name_Type;
526 -- Name of the executable on the command line, without directory
529 Executable : Name_Id;
530 -- Name of the executable file
532 Current_Dir : constant Dir_Name_Str := Get_Current_Dir;
533 Data : constant Project_Data := Projects.Table (Project);
534 U_Data : Prj.Com.Unit_Data;
535 File_Name1 : Name_Id;
536 File_Name2 : Name_Id;
541 -- Check that we don't specify executable on the command line for
542 -- a main library project.
544 if Project = Main_Project
545 and then Osint.Number_Of_Files /= 0
546 and then Data.Library
549 ("Cannot specify executable(s) for a Library Project File");
553 Put ("Cleaning project """);
554 Put (Get_Name_String (Data.Name));
558 -- Add project to the list of proceesed projects
560 Processed_Projects.Increment_Last;
561 Processed_Projects.Table (Processed_Projects.Last) := Project;
563 if Data.Object_Directory /= No_Name then
565 Obj_Dir : constant String :=
566 Get_Name_String (Data.Object_Directory);
569 Change_Dir (Obj_Dir);
571 -- Look through the units to find those that are either immediate
572 -- sources or inherited sources of the project.
574 for Unit in 1 .. Prj.Com.Units.Last loop
575 U_Data := Prj.Com.Units.Table (Unit);
576 File_Name1 := No_Name;
577 File_Name2 := No_Name;
579 -- If either the spec or the body is a source of the project,
580 -- check for the corresponding ALI file in the object
583 if In_Extension_Chain
584 (U_Data.File_Names (Body_Part).Project, Project)
587 (U_Data.File_Names (Specification).Project, Project)
589 File_Name1 := U_Data.File_Names (Body_Part).Name;
590 File_Name2 := U_Data.File_Names (Specification).Name;
592 -- If there is no body file name, then there may be only a
595 if File_Name1 = No_Name then
596 File_Name1 := File_Name2;
597 File_Name2 := No_Name;
601 -- If there is either a spec or a body, look for files in the
604 if File_Name1 /= No_Name then
606 Asm : constant String := Assembly_File_Name (File_Name1);
607 ALI : constant String := ALI_File_Name (File_Name1);
608 Obj : constant String := Object_File_Name (File_Name1);
609 Adt : constant String := Tree_File_Name (File_Name1);
610 Deb : constant String := Debug_File_Name (File_Name1);
611 Rep : constant String := Repinfo_File_Name (File_Name1);
612 Del : Boolean := True;
615 -- If the ALI file exists and is read-only, no file is
618 if Is_Regular_File (ALI) then
619 if Is_Writable_File (ALI) then
620 Delete (Obj_Dir, ALI);
629 if Obj_Dir (Obj_Dir'Last) /= Dir_Separator then
634 Put_Line (""" is read-only");
643 if Is_Regular_File (Obj) then
644 Delete (Obj_Dir, Obj);
649 if Is_Regular_File (Asm) then
650 Delete (Obj_Dir, Asm);
655 if Is_Regular_File (Adt) then
656 Delete (Obj_Dir, Adt);
659 -- First expanded source file
661 if Is_Regular_File (Deb) then
662 Delete (Obj_Dir, Deb);
667 if Is_Regular_File (Rep) then
668 Delete (Obj_Dir, Rep);
671 -- Second expanded source file
673 if File_Name2 /= No_Name then
675 Deb : constant String :=
676 Debug_File_Name (File_Name2);
677 Rep : constant String :=
678 Repinfo_File_Name (File_Name2);
680 if Is_Regular_File (Deb) then
681 Delete (Obj_Dir, Deb);
684 if Is_Regular_File (Rep) then
685 Delete (Obj_Dir, Rep);
700 -- If switch -r is specified, call Clean_Project recursively for the
701 -- imported projects and the project being extended.
705 Imported : Project_List := Data.Imported_Projects;
706 Element : Project_Element;
710 -- For each imported project, call Clean_Project if the project
711 -- has not been processed already.
713 while Imported /= Empty_Project_List loop
714 Element := Project_Lists.Table (Imported);
715 Imported := Element.Next;
719 J in Processed_Projects.First .. Processed_Projects.Last
721 if Element.Project = Processed_Projects.Table (J) then
728 Clean_Project (Element.Project);
732 -- If this project extends another project, call Clean_Project for
733 -- the project being extended. It is guaranteed that it has not
734 -- called before, because no other project may import or extend
737 if Data.Extends /= No_Project then
738 Clean_Project (Data.Extends);
743 -- If this is a library project, clean the library directory, the
744 -- interface copy dir and, for a Stand-Alone Library, the binder
745 -- generated files of the library.
747 -- The directories are cleaned only if switch -c is not specified.
750 if not Compile_Only then
751 Clean_Directory (Data.Library_Dir);
753 if Data.Library_Src_Dir /= No_Name
754 and then Data.Library_Src_Dir /= Data.Library_Dir
756 Clean_Directory (Data.Library_Src_Dir);
760 if Data.Standalone_Library and then
761 Data.Object_Directory /= No_Name
763 Delete_Binder_Generated_Files
764 (Get_Name_String (Data.Object_Directory), Data.Library_Name);
767 -- Otherwise, for the main project, delete the executables and the
768 -- binder generated files.
770 -- The executables are deleted only if switch -c is not specified.
772 elsif Project = Main_Project and then Data.Exec_Directory /= No_Name then
774 Exec_Dir : constant String :=
775 Get_Name_String (Data.Exec_Directory);
777 Change_Dir (Exec_Dir);
779 for N_File in 1 .. Osint.Number_Of_Files loop
780 Main_Source_File := Next_Main_Source;
782 if not Compile_Only then
783 Executable := Executable_Of (Main_Project, Main_Source_File);
785 if Is_Regular_File (Get_Name_String (Executable)) then
786 Delete (Exec_Dir, Get_Name_String (Executable));
790 if Data.Object_Directory /= No_Name then
791 Delete_Binder_Generated_Files
793 (Data.Object_Directory),
794 Strip_Suffix (Main_Source_File));
800 -- Change back to previous directory
802 Change_Dir (Current_Dir);
805 ---------------------
806 -- Debug_File_Name --
807 ---------------------
809 function Debug_File_Name (Source : Name_Id) return String is
811 return Get_Name_String (Source) & Debug_Suffix;
818 procedure Delete (In_Directory : String; File : String) is
819 Full_Name : String (1 .. In_Directory'Length + File'Length + 1);
824 -- Indicate that at least one file is deleted or is to be deleted
826 File_Deleted := True;
828 -- Build the path name of the file to delete
830 Last := In_Directory'Length;
831 Full_Name (1 .. Last) := In_Directory;
833 if Last > 0 and then Full_Name (Last) /= Directory_Separator then
835 Full_Name (Last) := Directory_Separator;
838 Full_Name (Last + 1 .. Last + File'Length) := File;
839 Last := Last + File'Length;
841 -- If switch -n was used, simply output the path name
844 Put_Line (Full_Name (1 .. Last));
846 -- Otherwise, delete the file
849 Delete_File (Full_Name (1 .. Last), Success);
853 Put (Full_Name (1 .. Last));
854 Put_Line (""" could not be deleted");
856 elsif Verbose_Mode or else not Quiet_Output then
858 Put (Full_Name (1 .. Last));
859 Put_Line (""" has been deleted");
864 -----------------------------------
865 -- Delete_Binder_Generated_Files --
866 -----------------------------------
868 procedure Delete_Binder_Generated_Files (Dir : String; Source : Name_Id) is
869 Source_Name : constant String := Get_Name_String (Source);
870 Current : constant String := Get_Current_Dir;
871 Last : constant Positive := B_Start'Length + Source_Name'Length;
872 File_Name : String (1 .. Last + 4);
877 -- Build the file name (before the extension)
879 File_Name (1 .. B_Start'Length) := B_Start;
880 File_Name (B_Start'Length + 1 .. Last) := Source_Name;
884 File_Name (Last + 1 .. Last + 4) := ".ads";
886 if Is_Regular_File (File_Name (1 .. Last + 4)) then
887 Delete (Dir, File_Name (1 .. Last + 4));
892 File_Name (Last + 1 .. Last + 4) := ".adb";
894 if Is_Regular_File (File_Name (1 .. Last + 4)) then
895 Delete (Dir, File_Name (1 .. Last + 4));
900 File_Name (Last + 1 .. Last + 4) := ".ali";
902 if Is_Regular_File (File_Name (1 .. Last + 4)) then
903 Delete (Dir, File_Name (1 .. Last + 4));
908 File_Name (Last + 1 .. Last + Object_Suffix'Length) := Object_Suffix;
910 if Is_Regular_File (File_Name (1 .. Last + Object_Suffix'Length)) then
911 Delete (Dir, File_Name (1 .. Last + Object_Suffix'Length));
914 -- Change back to previous directory
916 Change_Dir (Current);
917 end Delete_Binder_Generated_Files;
919 -----------------------
920 -- Display_Copyright --
921 -----------------------
923 procedure Display_Copyright is
925 if not Copyright_Displayed then
926 Copyright_Displayed := True;
927 Put_Line ("GNATCLEAN " & Gnatvsn.Gnat_Version_String
928 & " Copyright 2003 Free Software Foundation, Inc.");
930 end Display_Copyright;
936 function Empty_Q return Boolean is
938 return Q_Front >= Q.Last;
945 procedure Extract_From_Q (Source_File : out File_Name_Type) is
946 File : constant File_Name_Type := Q.Table (Q_Front);
949 Q_Front := Q_Front + 1;
957 procedure Gnatclean is
959 -- Do the necessary initializations
963 -- Parse the command line, getting the switches and the executable names
971 if Project_File_Name /= null then
973 -- A project file was specified by a -P switch
975 if Opt.Verbose_Mode then
977 Put ("Parsing Project File """);
978 Put (Project_File_Name.all);
983 -- Set the project parsing verbosity to whatever was specified
984 -- by a possible -vP switch.
986 Prj.Pars.Set_Verbosity (To => Prj.Com.Current_Verbosity);
988 -- Parse the project file.
989 -- If there is an error, Main_Project will still be No_Project.
992 (Project => Main_Project,
993 Project_File_Name => Project_File_Name.all,
994 Packages_To_Check => Packages_To_Check_By_Gnatmake);
996 if Main_Project = No_Project then
997 Fail ("""" & Project_File_Name.all &
998 """ processing failed");
1001 if Opt.Verbose_Mode then
1003 Put ("Parsing of Project File """);
1004 Put (Project_File_Name.all);
1005 Put (""" is finished.");
1009 -- We add the source directories and the object directories
1010 -- to the search paths.
1012 Add_Source_Directories (Main_Project);
1013 Add_Object_Directories (Main_Project);
1017 Osint.Add_Default_Search_Dirs;
1019 -- If a project file was specified, but no executable name, put all
1020 -- the mains of the project file (if any) as if there were on the
1023 if Main_Project /= No_Project and then Osint.Number_Of_Files = 0 then
1025 Value : String_List_Id := Projects.Table (Main_Project).Mains;
1028 while Value /= Prj.Nil_String loop
1029 Get_Name_String (String_Elements.Table (Value).Value);
1030 Osint.Add_File (Name_Buffer (1 .. Name_Len));
1031 Value := String_Elements.Table (Value).Next;
1036 -- If neither a project file nor an executable were specified,
1037 -- output the usage and exit.
1039 if Main_Project = No_Project and then Osint.Number_Of_Files = 0 then
1044 if Verbose_Mode then
1048 if Main_Project /= No_Project then
1050 -- If a project file has been specified, call Clean_Project with the
1051 -- project id of this project file, after resetting the list of
1052 -- processed projects.
1054 Processed_Projects.Init;
1055 Clean_Project (Main_Project);
1058 -- If no project file has been specified, the work is done in
1059 -- Clean_Executables.
1064 -- In verbose mode, if Delete has not been called, indicate that
1065 -- no file needs to be deleted.
1067 if Verbose_Mode and (not File_Deleted) then
1071 Put_Line ("No file needs to be deleted");
1073 Put_Line ("No file has been deleted");
1078 ------------------------
1079 -- In_Extension_Chain --
1080 ------------------------
1082 function In_Extension_Chain
1083 (Of_Project : Project_Id;
1084 Prj : Project_Id) return Boolean
1086 Data : Project_Data;
1089 if Of_Project = Prj then
1093 Data := Projects.Table (Of_Project);
1095 while Data.Extends /= No_Project loop
1096 if Data.Extends = Prj then
1100 Data := Projects.Table (Data.Extends);
1103 Data := Projects.Table (Prj);
1105 while Data.Extends /= No_Project loop
1106 if Data.Extends = Of_Project then
1110 Data := Projects.Table (Data.Extends);
1114 end In_Extension_Chain;
1123 Q.Set_Last (Q.First);
1130 procedure Initialize is
1132 if not Initialized then
1133 Initialized := True;
1135 -- Initialize some packages
1143 -- Reset global variables
1145 Free (Object_Directory_Path);
1146 Do_Nothing := False;
1147 File_Deleted := False;
1148 Copyright_Displayed := False;
1149 Usage_Displayed := False;
1150 Free (Project_File_Name);
1151 Main_Project := Prj.No_Project;
1152 All_Projects := False;
1160 (Source_File : File_Name_Type)
1163 -- Do not insert an empty name or an already marked source
1165 if Source_File /= No_Name
1166 and then Get_Name_Table_Byte (Source_File) = 0
1168 Q.Table (Q.Last) := Source_File;
1171 -- Mark the source that has been just added to the Q
1173 Set_Name_Table_Byte (Source_File, 1);
1177 ----------------------
1178 -- Object_File_Name --
1179 ----------------------
1181 function Object_File_Name (Source : Name_Id) return String is
1182 Src : constant String := Get_Name_String (Source);
1184 -- If the source name has an extension, then replace it with
1185 -- the Object suffix.
1187 for Index in reverse Src'First + 1 .. Src'Last loop
1188 if Src (Index) = '.' then
1189 return Src (Src'First .. Index - 1) & Object_Suffix;
1193 -- If there is no dot, or if it is the first character, just add the
1196 return Src & Object_Suffix;
1197 end Object_File_Name;
1199 --------------------
1200 -- Parse_Cmd_Line --
1201 --------------------
1203 procedure Parse_Cmd_Line is
1207 GNAT.Command_Line.Getopt
1208 ("aO: c D: F h I: I- n P: q r v vP0 vP1 vP2 X:")
1214 Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
1217 Compile_Only := True;
1221 Dir : constant String := GNAT.Command_Line.Parameter;
1224 if Object_Directory_Path /= null then
1225 Fail ("duplicate -D switch");
1227 elsif Project_File_Name /= null then
1228 Fail ("-P and -D cannot be used simultaneously");
1230 elsif not Is_Directory (Dir) then
1231 Fail (Dir, " is not a directory");
1234 Add_Lib_Search_Dir (Dir);
1239 Full_Path_Name_For_Brief_Errors := True;
1245 if Full_Switch = "I-" then
1246 Opt.Look_In_Primary_Dir := False;
1249 Add_Lib_Search_Dir (GNAT.Command_Line.Parameter);
1256 if Project_File_Name /= null then
1257 Fail ("multiple -P switches");
1259 elsif Object_Directory_Path /= null then
1260 Fail ("-D and -P cannot be used simultaneously");
1264 Prj : constant String := GNAT.Command_Line.Parameter;
1266 if Prj'Length > 1 and then Prj (Prj'First) = '=' then
1267 Project_File_Name :=
1268 new String'(Prj (Prj'First + 1 .. Prj'Last));
1271 Project_File_Name := new String'(Prj);
1277 Quiet_Output := True;
1280 All_Projects := True;
1283 if Full_Switch = "v" then
1284 Verbose_Mode := True;
1286 elsif Full_Switch = "vP0" then
1287 Prj.Com.Current_Verbosity := Prj.Default;
1289 elsif Full_Switch = "vP1" then
1290 Prj.Com.Current_Verbosity := Prj.Medium;
1293 Prj.Com.Current_Verbosity := Prj.High;
1298 Ext_Asgn : constant String := GNAT.Command_Line.Parameter;
1299 Start : Positive := Ext_Asgn'First;
1300 Stop : Natural := Ext_Asgn'Last;
1301 Equal_Pos : Natural;
1302 OK : Boolean := True;
1305 if Ext_Asgn (Start) = '"' then
1306 if Ext_Asgn (Stop) = '"' then
1317 while Equal_Pos <= Stop and then
1318 Ext_Asgn (Equal_Pos) /= '='
1320 Equal_Pos := Equal_Pos + 1;
1323 if Equal_Pos = Start or else Equal_Pos > Stop then
1329 (External_Name => Ext_Asgn (Start .. Equal_Pos - 1),
1330 Value => Ext_Asgn (Equal_Pos + 1 .. Stop));
1333 Fail ("illegal external assignment '", Ext_Asgn, "'");
1338 Fail ("INTERNAL ERROR, please report");
1342 -- Get the file names
1346 S : constant String := GNAT.Command_Line.Get_Argument;
1349 exit when S'Length = 0;
1356 when GNAT.Command_Line.Invalid_Switch =>
1358 Fail ("invalid switch : "& GNAT.Command_Line.Full_Switch);
1360 when GNAT.Command_Line.Invalid_Parameter =>
1362 Fail ("parameter missing for : " & GNAT.Command_Line.Full_Switch);
1365 -----------------------
1366 -- Repinfo_File_Name --
1367 -----------------------
1369 function Repinfo_File_Name (Source : Name_Id) return String is
1371 return Get_Name_String (Source) & Repinfo_Suffix;
1372 end Repinfo_File_Name;
1374 --------------------
1375 -- Tree_File_Name --
1376 --------------------
1378 function Tree_File_Name (Source : Name_Id) return String is
1379 Src : constant String := Get_Name_String (Source);
1382 -- If the source name has an extension, then replace it with
1385 for Index in reverse Src'First + 1 .. Src'Last loop
1386 if Src (Index) = '.' then
1387 return Src (Src'First .. Index - 1) & Tree_Suffix;
1391 -- If there is no dot, or if it is the first character, just add the
1394 return Src & Tree_Suffix;
1403 if not Usage_Displayed then
1404 Usage_Displayed := True;
1406 Put_Line ("Usage: gnatclean [switches] names");
1409 Put_Line (" names is one or more file names from which " &
1410 "the .adb or .ads suffix may be omitted");
1411 Put_Line (" names may be omitted if -P<project> is specified");
1414 Put_Line (" -c Only delete compiler generated files");
1415 Put_Line (" -D dir Specify dir as the object library");
1416 Put_Line (" -F Full project path name " &
1417 "in brief error messages");
1418 Put_Line (" -h Display this message");
1419 Put_Line (" -n Nothing to do: only list files to delete");
1420 Put_Line (" -Pproj Use GNAT Project File proj");
1421 Put_Line (" -q Be quiet/terse");
1422 Put_Line (" -r Clean all projects recursively");
1423 Put_Line (" -v Verbose mode");
1424 Put_Line (" -vPx Specify verbosity when parsing " &
1425 "GNAT Project Files");
1426 Put_Line (" -Xnm=val Specify an external reference " &
1427 "for GNAT Project Files");
1430 Put_Line (" -aOdir Specify ALI/object files search path");
1431 Put_Line (" -Idir Like -aOdir");
1432 Put_Line (" -I- Don't look for source/library files " &
1433 "in the default directory");
1439 if Hostparm.OpenVMS then
1440 Debug_Suffix (Debug_Suffix'First) := '_';
1441 Repinfo_Suffix (Repinfo_Suffix'First) := '_';
1442 B_Start (B_Start'Last) := '$';