1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2003-2007, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 ------------------------------------------------------------------------------
29 with Gnatvsn; use Gnatvsn;
31 with MLib.Tgt; use MLib.Tgt;
32 with Namet; use Namet;
34 with Osint; use Osint;
35 with Osint.M; use Osint.M;
40 with Prj.Util; use Prj.Util;
43 with Targparm; use Targparm;
44 with Types; use Types;
46 with Ada.Command_Line; use Ada.Command_Line;
48 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
49 with GNAT.IO; use GNAT.IO;
50 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_Target_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_Ptr := new String'("b~");
71 -- Prefix of binder generated file, and number of actual characters used.
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 Force_Deletions : Boolean := False;
78 -- Set to True by switch -f. When True, attempts to delete non writable
79 -- files will be done.
81 Do_Nothing : Boolean := False;
82 -- Set to True when switch -n is specified. When True, no file is deleted.
83 -- gnatclean only lists the files that would have been deleted if the
84 -- switch -n had not been specified.
86 File_Deleted : Boolean := False;
87 -- Set to True if at least one file has been deleted
89 Copyright_Displayed : Boolean := False;
90 Usage_Displayed : Boolean := False;
92 Project_File_Name : String_Access := null;
94 Project_Tree : constant Prj.Project_Tree_Ref := new Prj.Project_Tree_Data;
96 Main_Project : Prj.Project_Id := Prj.No_Project;
98 All_Projects : Boolean := False;
100 -- Packages of project files where unknown attributes are errors
102 Naming_String : aliased String := "naming";
103 Builder_String : aliased String := "builder";
104 Compiler_String : aliased String := "compiler";
105 Binder_String : aliased String := "binder";
106 Linker_String : aliased String := "linker";
108 Gnatmake_Packages : aliased String_List :=
109 (Naming_String 'Access,
110 Builder_String 'Access,
111 Compiler_String 'Access,
112 Binder_String 'Access,
113 Linker_String 'Access);
115 Packages_To_Check_By_Gnatmake : constant String_List_Access :=
116 Gnatmake_Packages'Access;
118 package Processed_Projects is new Table.Table
119 (Table_Component_Type => Project_Id,
120 Table_Index_Type => Natural,
121 Table_Low_Bound => 0,
123 Table_Increment => 100,
124 Table_Name => "Clean.Processed_Projects");
125 -- Table to keep track of what project files have been processed, when
126 -- switch -r is specified.
128 package Sources is new Table.Table
129 (Table_Component_Type => File_Name_Type,
130 Table_Index_Type => Natural,
131 Table_Low_Bound => 0,
133 Table_Increment => 100,
134 Table_Name => "Clean.Processed_Projects");
135 -- Table to store all the source files of a library unit: spec, body and
136 -- subunits, to detect .dg files and delete them.
138 ----------------------------
139 -- Queue (Q) manipulation --
140 ----------------------------
143 -- Must be called to initialize the Q
145 procedure Insert_Q (Lib_File : File_Name_Type);
146 -- If Lib_File is not marked, inserts it at the end of Q and mark it
148 function Empty_Q return Boolean;
149 -- Returns True if Q is empty
151 procedure Extract_From_Q (Lib_File : out File_Name_Type);
152 -- Extracts the first element from the Q
155 -- Points to the first valid element in the Q
157 package Q is new Table.Table (
158 Table_Component_Type => File_Name_Type,
159 Table_Index_Type => Natural,
160 Table_Low_Bound => 0,
161 Table_Initial => 4000,
162 Table_Increment => 100,
163 Table_Name => "Clean.Q");
164 -- This is the actual queue
166 -----------------------------
167 -- Other local subprograms --
168 -----------------------------
170 procedure Add_Source_Dir (N : String);
171 -- Call Add_Src_Search_Dir and output one line when in verbose mode
173 procedure Add_Source_Directories is
174 new Prj.Env.For_All_Source_Dirs (Action => Add_Source_Dir);
176 procedure Add_Object_Dir (N : String);
177 -- Call Add_Lib_Search_Dir and output one line when in verbose mode
179 procedure Add_Object_Directories is
180 new Prj.Env.For_All_Object_Dirs (Action => Add_Object_Dir);
182 function ALI_File_Name (Source : File_Name_Type) return String;
183 -- Returns the name of the ALI file corresponding to Source
185 function Assembly_File_Name (Source : File_Name_Type) return String;
186 -- Returns the assembly file name corresponding to Source
188 procedure Clean_Archive (Project : Project_Id; Global : Boolean);
189 -- Delete a global archive or library project archive and the dependency
190 -- file, if they exist.
192 procedure Clean_Executables;
193 -- Do the cleaning work when no project file is specified
195 procedure Clean_Interface_Copy_Directory (Project : Project_Id);
196 -- Delete files in an interface copy directory: any file that is a copy of
197 -- a source of the project.
199 procedure Clean_Library_Directory (Project : Project_Id);
200 -- Delete the library file in a library directory and any ALI file of a
201 -- source of the project in a library ALI directory.
203 procedure Clean_Project (Project : Project_Id);
204 -- Do the cleaning work when a project file is specified. This procedure
205 -- calls itself recursively when there are several project files in the
206 -- tree rooted at the main project file and switch -r has been specified.
208 function Debug_File_Name (Source : File_Name_Type) return String;
209 -- Name of the expanded source file corresponding to Source
211 procedure Delete (In_Directory : String; File : String);
212 -- Delete one file, or list the file name if switch -n is specified
214 procedure Delete_Binder_Generated_Files
216 Source : File_Name_Type);
217 -- Delete the binder generated file in directory Dir for Source, if they
218 -- exist: for Unix these are b~<source>.ads, b~<source>.adb,
219 -- b~<source>.ali and b~<source>.o.
221 procedure Display_Copyright;
222 -- Display the Copyright notice. If called several times, display the
223 -- Copyright notice only the first time.
225 procedure Initialize;
226 -- Call the necessary package initializations
228 function Object_File_Name (Source : File_Name_Type) return String;
229 -- Returns the object file name corresponding to Source
231 procedure Parse_Cmd_Line;
232 -- Parse the command line
234 function Repinfo_File_Name (Source : File_Name_Type) return String;
235 -- Returns the repinfo file name corresponding to Source
237 function Tree_File_Name (Source : File_Name_Type) return String;
238 -- Returns the tree file name corresponding to Source
240 function In_Extension_Chain
241 (Of_Project : Project_Id;
242 Prj : Project_Id) return Boolean;
243 -- Returns True iff Prj is an extension of Of_Project or if Of_Project is
244 -- an extension of Prj.
246 function Ultimate_Extension_Of (Project : Project_Id) return Project_Id;
247 -- Returns either Project, if it is not extended by another project, or
248 -- the project that extends Project, directly or indirectly, and that is
249 -- not itself extended. Returns No_Project if Project is No_Project.
252 -- Display the usage. If called several times, the usage is displayed only
259 procedure Add_Object_Dir (N : String) is
261 Add_Lib_Search_Dir (N);
263 if Opt.Verbose_Mode then
264 Put ("Adding object directory """);
275 procedure Add_Source_Dir (N : String) is
277 Add_Src_Search_Dir (N);
279 if Opt.Verbose_Mode then
280 Put ("Adding source directory """);
291 function ALI_File_Name (Source : File_Name_Type) return String is
292 Src : constant String := Get_Name_String (Source);
295 -- If the source name has an extension, then replace it with
298 for Index in reverse Src'First + 1 .. Src'Last loop
299 if Src (Index) = '.' then
300 return Src (Src'First .. Index - 1) & ALI_Suffix;
304 -- If there is no dot, or if it is the first character, just add the
307 return Src & ALI_Suffix;
310 ------------------------
311 -- Assembly_File_Name --
312 ------------------------
314 function Assembly_File_Name (Source : File_Name_Type) return String is
315 Src : constant String := Get_Name_String (Source);
318 -- If the source name has an extension, then replace it with
319 -- the assembly suffix.
321 for Index in reverse Src'First + 1 .. Src'Last loop
322 if Src (Index) = '.' then
323 return Src (Src'First .. Index - 1) & Assembly_Suffix;
327 -- If there is no dot, or if it is the first character, just add the
330 return Src & Assembly_Suffix;
331 end Assembly_File_Name;
337 procedure Clean_Archive (Project : Project_Id; Global : Boolean) is
338 Current_Dir : constant Dir_Name_Str := Get_Current_Dir;
340 Data : constant Project_Data := Project_Tree.Projects.Table (Project);
342 Lib_Prefix : String_Access;
343 Archive_Name : String_Access;
344 -- The name of the archive file for this project
346 Archive_Dep_Name : String_Access;
347 -- The name of the archive dependency file for this project
349 Obj_Dir : constant String :=
350 Get_Name_String (Data.Display_Object_Dir);
353 Change_Dir (Obj_Dir);
355 -- First, get the lib prefix, the archive file name and the archive
356 -- dependency file name.
360 new String'("lib" & Get_Name_String (Data.Display_Name));
363 new String'("lib" & Get_Name_String (Data.Library_Name));
366 Archive_Name := new String'(Lib_Prefix.all & '.' & Archive_Ext);
367 Archive_Dep_Name := new String'(Lib_Prefix.all & ".deps");
369 -- Delete the archive file and the archive dependency file, if they
372 if Is_Regular_File (Archive_Name.all) then
373 Delete (Obj_Dir, Archive_Name.all);
376 if Is_Regular_File (Archive_Dep_Name.all) then
377 Delete (Obj_Dir, Archive_Dep_Name.all);
380 Change_Dir (Current_Dir);
383 -----------------------
384 -- Clean_Executables --
385 -----------------------
387 procedure Clean_Executables is
388 Main_Source_File : File_Name_Type;
389 -- Current main source
391 Main_Lib_File : File_Name_Type;
392 -- ALI file of the current main
394 Lib_File : File_Name_Type;
397 Full_Lib_File : File_Name_Type;
398 -- Full name of the current ALI file
400 Text : Text_Buffer_Ptr;
406 -- It does not really matter if there is or not an object file
407 -- corresponding to an ALI file: if there is one, it will be deleted.
409 Opt.Check_Object_Consistency := False;
411 -- Proceed each executable one by one. Each source is marked as it is
412 -- processed, so common sources between executables will not be
413 -- processed several times.
415 for N_File in 1 .. Osint.Number_Of_Files loop
416 Main_Source_File := Next_Main_Source;
417 Main_Lib_File := Osint.Lib_File_Name
418 (Main_Source_File, Current_File_Index);
419 Insert_Q (Main_Lib_File);
421 while not Empty_Q loop
422 Sources.Set_Last (0);
423 Extract_From_Q (Lib_File);
424 Full_Lib_File := Osint.Full_Lib_File_Name (Lib_File);
426 -- If we have existing ALI file that is not read-only, process it
428 if Full_Lib_File /= No_File
429 and then not Is_Readonly_Library (Full_Lib_File)
431 Text := Read_Library_Info (Lib_File);
435 Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
438 -- If no error was produced while loading this ALI file,
439 -- insert into the queue all the unmarked withed sources.
441 if The_ALI /= No_ALI_Id then
442 for J in ALIs.Table (The_ALI).First_Unit ..
443 ALIs.Table (The_ALI).Last_Unit
445 Sources.Increment_Last;
446 Sources.Table (Sources.Last) :=
447 ALI.Units.Table (J).Sfile;
449 for K in ALI.Units.Table (J).First_With ..
450 ALI.Units.Table (J).Last_With
452 Insert_Q (Withs.Table (K).Afile);
456 -- Look for subunits and put them in the Sources table
458 for J in ALIs.Table (The_ALI).First_Sdep ..
459 ALIs.Table (The_ALI).Last_Sdep
461 if Sdep.Table (J).Subunit_Name /= No_Name then
462 Sources.Increment_Last;
463 Sources.Table (Sources.Last) :=
464 Sdep.Table (J).Sfile;
470 -- Now delete all existing files corresponding to this ALI file
473 Obj_Dir : constant String :=
474 Dir_Name (Get_Name_String (Full_Lib_File));
475 Obj : constant String := Object_File_Name (Lib_File);
476 Adt : constant String := Tree_File_Name (Lib_File);
477 Asm : constant String := Assembly_File_Name (Lib_File);
480 Delete (Obj_Dir, Get_Name_String (Lib_File));
482 if Is_Regular_File (Obj_Dir & Dir_Separator & Obj) then
483 Delete (Obj_Dir, Obj);
486 if Is_Regular_File (Obj_Dir & Dir_Separator & Adt) then
487 Delete (Obj_Dir, Adt);
490 if Is_Regular_File (Obj_Dir & Dir_Separator & Asm) then
491 Delete (Obj_Dir, Asm);
494 -- Delete expanded source files (.dg) and/or repinfo files
497 for J in 1 .. Sources.Last loop
499 Deb : constant String :=
500 Debug_File_Name (Sources.Table (J));
501 Rep : constant String :=
502 Repinfo_File_Name (Sources.Table (J));
505 if Is_Regular_File (Obj_Dir & Dir_Separator & Deb) then
506 Delete (Obj_Dir, Deb);
509 if Is_Regular_File (Obj_Dir & Dir_Separator & Rep) then
510 Delete (Obj_Dir, Rep);
518 -- Delete the executable, if it exists, and the binder generated
521 if not Compile_Only then
523 Source : constant File_Name_Type :=
524 Strip_Suffix (Main_Lib_File);
525 Executable : constant String :=
526 Get_Name_String (Executable_Name (Source));
528 if Is_Regular_File (Executable) then
529 Delete ("", Executable);
532 Delete_Binder_Generated_Files (Get_Current_Dir, Source);
536 end Clean_Executables;
538 ------------------------------------
539 -- Clean_Interface_Copy_Directory --
540 ------------------------------------
542 procedure Clean_Interface_Copy_Directory (Project : Project_Id) is
543 Current : constant String := Get_Current_Dir;
544 Data : constant Project_Data := Project_Tree.Projects.Table (Project);
548 Name : String (1 .. 200);
551 Delete_File : Boolean;
555 if Data.Library and then Data.Library_Src_Dir /= No_Path then
557 Directory : constant String :=
558 Get_Name_String (Data.Display_Library_Src_Dir);
561 Change_Dir (Directory);
564 -- For each regular file in the directory, if switch -n has not
565 -- been specified, make it writable and delete the file if it is
566 -- a copy of a source of the project.
569 Read (Direc, Name, Last);
573 Filename : constant String := Name (1 .. Last);
576 if Is_Regular_File (Filename) then
577 Canonical_Case_File_Name (Name (1 .. Last));
578 Delete_File := False;
580 -- Compare with source file names of the project
583 1 .. Unit_Table.Last (Project_Tree.Units)
585 Unit := Project_Tree.Units.Table (Index);
587 if Ultimate_Extension_Of
588 (Unit.File_Names (Body_Part).Project) = Project
591 (Unit.File_Names (Body_Part).Name) =
598 if Ultimate_Extension_Of
599 (Unit.File_Names (Specification).Project) = Project
602 (Unit.File_Names (Specification).Name) =
611 if not Do_Nothing then
612 Set_Writable (Filename);
615 Delete (Directory, Filename);
623 -- Restore the initial working directory
625 Change_Dir (Current);
628 end Clean_Interface_Copy_Directory;
630 -----------------------------
631 -- Clean_Library_Directory --
632 -----------------------------
634 Empty_String : aliased String := "";
636 procedure Clean_Library_Directory (Project : Project_Id) is
637 Current : constant String := Get_Current_Dir;
638 Data : constant Project_Data := Project_Tree.Projects.Table (Project);
640 Lib_Filename : constant String := Get_Name_String (Data.Library_Name);
642 DLL_Prefix & Lib_Filename & "." & DLL_Ext;
643 Archive_Name : String :=
644 "lib" & Lib_Filename & "." & Archive_Ext;
647 Name : String (1 .. 200);
650 Delete_File : Boolean;
652 Minor : String_Access := Empty_String'Unchecked_Access;
653 Major : String_Access := Empty_String'Unchecked_Access;
657 if Data.Library_Kind /= Static
658 and then MLib.Tgt.Library_Major_Minor_Id_Supported
659 and then Data.Lib_Internal_Name /= No_Name
661 Minor := new String'(Get_Name_String (Data.Lib_Internal_Name));
662 Major := new String'(MLib.Major_Id_Name (DLL_Name, Minor.all));
666 Lib_Directory : constant String :=
667 Get_Name_String (Data.Display_Library_Dir);
668 Lib_ALI_Directory : constant String :=
670 (Data.Display_Library_ALI_Dir);
673 Canonical_Case_File_Name (Archive_Name);
674 Canonical_Case_File_Name (DLL_Name);
676 Change_Dir (Lib_Directory);
679 -- For each regular file in the directory, if switch -n has not
680 -- been specified, make it writable and delete the file if it is
684 Read (Direc, Name, Last);
688 Filename : constant String := Name (1 .. Last);
690 if Is_Regular_File (Filename)
691 or else Is_Symbolic_Link (Filename)
693 Canonical_Case_File_Name (Name (1 .. Last));
694 Delete_File := False;
696 if (Data.Library_Kind = Static
697 and then Name (1 .. Last) = Archive_Name)
699 ((Data.Library_Kind = Dynamic or else
700 Data.Library_Kind = Relocatable)
702 (Name (1 .. Last) = DLL_Name
703 or else Name (1 .. Last) = Minor.all
704 or else Name (1 .. Last) = Major.all))
706 if not Do_Nothing then
707 Set_Writable (Filename);
710 Delete (Lib_Directory, Filename);
718 Change_Dir (Lib_ALI_Directory);
721 -- For each regular file in the directory, if switch -n has not
722 -- been specified, make it writable and delete the file if it is
723 -- any ALI file of a source of the project.
726 Read (Direc, Name, Last);
730 Filename : constant String := Name (1 .. Last);
732 if Is_Regular_File (Filename) then
733 Canonical_Case_File_Name (Name (1 .. Last));
734 Delete_File := False;
736 if Last > 4 and then Name (Last - 3 .. Last) = ".ali" then
740 -- Compare with ALI file names of the project
743 Index in 1 .. Unit_Table.Last (Project_Tree.Units)
745 Unit := Project_Tree.Units.Table (Index);
747 if Unit.File_Names (Body_Part).Project /=
750 if Ultimate_Extension_Of
751 (Unit.File_Names (Body_Part).Project) =
755 (Unit.File_Names (Body_Part).Name);
756 Name_Len := Name_Len -
758 (Name (1 .. Name_Len))'Length;
759 if Name_Buffer (1 .. Name_Len) =
767 elsif Ultimate_Extension_Of
768 (Unit.File_Names (Specification).Project) =
772 (Unit.File_Names (Specification).Name);
773 Name_Len := Name_Len -
775 (Name (1 .. Name_Len))'Length;
777 if Name_Buffer (1 .. Name_Len) =
789 if not Do_Nothing then
790 Set_Writable (Filename);
793 Delete (Lib_ALI_Directory, Filename);
801 -- Restore the initial working directory
803 Change_Dir (Current);
806 end Clean_Library_Directory;
812 procedure Clean_Project (Project : Project_Id) is
813 Main_Source_File : File_Name_Type;
814 -- Name of executable on the command line without directory info
816 Executable : File_Name_Type;
817 -- Name of the executable file
819 Current_Dir : constant Dir_Name_Str := Get_Current_Dir;
820 Data : constant Project_Data :=
821 Project_Tree.Projects.Table (Project);
823 File_Name1 : File_Name_Type;
825 File_Name2 : File_Name_Type;
827 Lib_File : File_Name_Type;
829 Source_Id : Other_Source_Id;
830 Source : Other_Source;
832 Global_Archive : Boolean := False;
835 -- Check that we don't specify executable on the command line for
836 -- a main library project.
838 if Project = Main_Project
839 and then Osint.Number_Of_Files /= 0
840 and then Data.Library
843 ("Cannot specify executable(s) for a Library Project File");
846 -- Nothing to clean in an externally built project
848 if Data.Externally_Built then
850 Put ("Nothing to do to clean externally built project """);
851 Put (Get_Name_String (Data.Name));
857 Put ("Cleaning project """);
858 Put (Get_Name_String (Data.Name));
862 -- Add project to the list of processed projects
864 Processed_Projects.Increment_Last;
865 Processed_Projects.Table (Processed_Projects.Last) := Project;
867 if Data.Object_Directory /= No_Path then
869 Obj_Dir : constant String :=
870 Get_Name_String (Data.Display_Object_Dir);
873 Change_Dir (Obj_Dir);
875 -- First, deal with Ada
877 -- Look through the units to find those that are either
878 -- immediate sources or inherited sources of the project.
879 -- Extending projects may have no language specified, if
880 -- Source_Dirs or Source_Files is specified as an empty list,
881 -- so always look for Ada units in extending projects.
883 if Data.Langs (Ada_Language_Index)
884 or else Data.Extends /= No_Project
886 for Unit in Unit_Table.First ..
887 Unit_Table.Last (Project_Tree.Units)
889 U_Data := Project_Tree.Units.Table (Unit);
890 File_Name1 := No_File;
891 File_Name2 := No_File;
893 -- If either the spec or the body is a source of the
894 -- project, check for the corresponding ALI file in the
897 if In_Extension_Chain
898 (U_Data.File_Names (Body_Part).Project, Project)
901 (U_Data.File_Names (Specification).Project, Project)
903 File_Name1 := U_Data.File_Names (Body_Part).Name;
904 Index1 := U_Data.File_Names (Body_Part).Index;
905 File_Name2 := U_Data.File_Names (Specification).Name;
906 Index2 := U_Data.File_Names (Specification).Index;
908 -- If there is no body file name, then there may be
911 if File_Name1 = No_File then
912 File_Name1 := File_Name2;
914 File_Name2 := No_File;
919 -- If there is either a spec or a body, look for files
920 -- in the object directory.
922 if File_Name1 /= No_File then
923 Lib_File := Osint.Lib_File_Name (File_Name1, Index1);
926 Asm : constant String :=
927 Assembly_File_Name (Lib_File);
928 ALI : constant String :=
929 ALI_File_Name (Lib_File);
930 Obj : constant String :=
931 Object_File_Name (Lib_File);
932 Adt : constant String :=
933 Tree_File_Name (Lib_File);
934 Deb : constant String :=
935 Debug_File_Name (File_Name1);
936 Rep : constant String :=
937 Repinfo_File_Name (File_Name1);
938 Del : Boolean := True;
941 -- If the ALI file exists and is read-only, no file
944 if Is_Regular_File (ALI) then
945 if Is_Writable_File (ALI) then
946 Delete (Obj_Dir, ALI);
955 if Obj_Dir (Obj_Dir'Last) /=
962 Put_Line (""" is read-only");
971 if Is_Regular_File (Obj) then
972 Delete (Obj_Dir, Obj);
977 if Is_Regular_File (Asm) then
978 Delete (Obj_Dir, Asm);
983 if Is_Regular_File (Adt) then
984 Delete (Obj_Dir, Adt);
987 -- First expanded source file
989 if Is_Regular_File (Deb) then
990 Delete (Obj_Dir, Deb);
995 if Is_Regular_File (Rep) then
996 Delete (Obj_Dir, Rep);
999 -- Second expanded source file
1001 if File_Name2 /= No_File then
1003 Deb : constant String :=
1004 Debug_File_Name (File_Name2);
1005 Rep : constant String :=
1006 Repinfo_File_Name (File_Name2);
1009 if Is_Regular_File (Deb) then
1010 Delete (Obj_Dir, Deb);
1013 if Is_Regular_File (Rep) then
1014 Delete (Obj_Dir, Rep);
1024 -- Check if a global archive and it dependency file could have
1025 -- been created and, if they exist, delete them.
1027 if Project = Main_Project and then not Data.Library then
1028 Global_Archive := False;
1030 for Proj in Project_Table.First ..
1031 Project_Table.Last (Project_Tree.Projects)
1033 if Project_Tree.Projects.Table
1034 (Proj).Other_Sources_Present
1036 Global_Archive := True;
1041 if Global_Archive then
1042 Clean_Archive (Project, Global => True);
1046 if Data.Other_Sources_Present then
1048 -- There is non-Ada code: delete the object files and
1049 -- the dependency files if they exist.
1051 Source_Id := Data.First_Other_Source;
1052 while Source_Id /= No_Other_Source loop
1054 Project_Tree.Other_Sources.Table (Source_Id);
1057 (Get_Name_String (Source.Object_Name))
1059 Delete (Obj_Dir, Get_Name_String (Source.Object_Name));
1063 Is_Regular_File (Get_Name_String (Source.Dep_Name))
1065 Delete (Obj_Dir, Get_Name_String (Source.Dep_Name));
1068 Source_Id := Source.Next;
1071 -- If it is a library with only non Ada sources, delete
1072 -- the fake archive and the dependency file, if they exist.
1075 and then not Data.Langs (Ada_Language_Index)
1077 Clean_Archive (Project, Global => False);
1083 -- If this is a library project, clean the library directory, the
1084 -- interface copy dir and, for a Stand-Alone Library, the binder
1085 -- generated files of the library.
1087 -- The directories are cleaned only if switch -c is not specified
1089 if Data.Library then
1090 if not Compile_Only then
1091 Clean_Library_Directory (Project);
1093 if Data.Library_Src_Dir /= No_Path then
1094 Clean_Interface_Copy_Directory (Project);
1098 if Data.Standalone_Library and then
1099 Data.Object_Directory /= No_Path
1101 Delete_Binder_Generated_Files
1102 (Get_Name_String (Data.Display_Object_Dir),
1103 File_Name_Type (Data.Library_Name));
1107 if Verbose_Mode then
1112 -- If switch -r is specified, call Clean_Project recursively for the
1113 -- imported projects and the project being extended.
1115 if All_Projects then
1117 Imported : Project_List := Data.Imported_Projects;
1118 Element : Project_Element;
1122 -- For each imported project, call Clean_Project if the project
1123 -- has not been processed already.
1125 while Imported /= Empty_Project_List loop
1126 Element := Project_Tree.Project_Lists.Table (Imported);
1127 Imported := Element.Next;
1131 J in Processed_Projects.First .. Processed_Projects.Last
1133 if Element.Project = Processed_Projects.Table (J) then
1140 Clean_Project (Element.Project);
1144 -- If this project extends another project, call Clean_Project for
1145 -- the project being extended. It is guaranteed that it has not
1146 -- called before, because no other project may import or extend
1149 if Data.Extends /= No_Project then
1150 Clean_Project (Data.Extends);
1155 -- For the main project, delete the executables and the binder
1158 -- The executables are deleted only if switch -c is not specified
1160 if Project = Main_Project and then Data.Exec_Directory /= No_Path then
1162 Exec_Dir : constant String :=
1163 Get_Name_String (Data.Display_Exec_Dir);
1166 Change_Dir (Exec_Dir);
1168 for N_File in 1 .. Osint.Number_Of_Files loop
1169 Main_Source_File := Next_Main_Source;
1171 if not Compile_Only then
1177 Current_File_Index);
1180 Exec_File_Name : constant String :=
1181 Get_Name_String (Executable);
1184 if Is_Absolute_Path (Name => Exec_File_Name) then
1185 if Is_Regular_File (Exec_File_Name) then
1186 Delete ("", Exec_File_Name);
1190 if Is_Regular_File (Exec_File_Name) then
1191 Delete (Exec_Dir, Exec_File_Name);
1197 if Data.Object_Directory /= No_Path then
1198 Delete_Binder_Generated_Files
1199 (Get_Name_String (Data.Display_Object_Dir),
1200 Strip_Suffix (Main_Source_File));
1206 -- Change back to previous directory
1208 Change_Dir (Current_Dir);
1211 ---------------------
1212 -- Debug_File_Name --
1213 ---------------------
1215 function Debug_File_Name (Source : File_Name_Type) return String is
1217 return Get_Name_String (Source) & Debug_Suffix;
1218 end Debug_File_Name;
1224 procedure Delete (In_Directory : String; File : String) is
1225 Full_Name : String (1 .. In_Directory'Length + File'Length + 1);
1226 Last : Natural := 0;
1230 -- Indicate that at least one file is deleted or is to be deleted
1232 File_Deleted := True;
1234 -- Build the path name of the file to delete
1236 Last := In_Directory'Length;
1237 Full_Name (1 .. Last) := In_Directory;
1239 if Last > 0 and then Full_Name (Last) /= Directory_Separator then
1241 Full_Name (Last) := Directory_Separator;
1244 Full_Name (Last + 1 .. Last + File'Length) := File;
1245 Last := Last + File'Length;
1247 -- If switch -n was used, simply output the path name
1250 Put_Line (Full_Name (1 .. Last));
1252 -- Otherwise, delete the file if it is writable
1256 or else Is_Writable_File (Full_Name (1 .. Last))
1257 or else Is_Symbolic_Link (Full_Name (1 .. Last))
1259 Delete_File (Full_Name (1 .. Last), Success);
1264 if Verbose_Mode or else not Quiet_Output then
1266 Put ("Warning: """);
1267 Put (Full_Name (1 .. Last));
1268 Put_Line (""" could not be deleted");
1272 Put (Full_Name (1 .. Last));
1273 Put_Line (""" has been deleted");
1279 -----------------------------------
1280 -- Delete_Binder_Generated_Files --
1281 -----------------------------------
1283 procedure Delete_Binder_Generated_Files
1285 Source : File_Name_Type)
1287 Source_Name : constant String := Get_Name_String (Source);
1288 Current : constant String := Get_Current_Dir;
1289 Last : constant Positive := B_Start'Length + Source_Name'Length;
1290 File_Name : String (1 .. Last + 4);
1295 -- Build the file name (before the extension)
1297 File_Name (1 .. B_Start'Length) := B_Start.all;
1298 File_Name (B_Start'Length + 1 .. Last) := Source_Name;
1302 File_Name (Last + 1 .. Last + 4) := ".ads";
1304 if Is_Regular_File (File_Name (1 .. Last + 4)) then
1305 Delete (Dir, File_Name (1 .. Last + 4));
1310 File_Name (Last + 1 .. Last + 4) := ".adb";
1312 if Is_Regular_File (File_Name (1 .. Last + 4)) then
1313 Delete (Dir, File_Name (1 .. Last + 4));
1318 File_Name (Last + 1 .. Last + 4) := ".ali";
1320 if Is_Regular_File (File_Name (1 .. Last + 4)) then
1321 Delete (Dir, File_Name (1 .. Last + 4));
1326 File_Name (Last + 1 .. Last + Object_Suffix'Length) := Object_Suffix;
1328 if Is_Regular_File (File_Name (1 .. Last + Object_Suffix'Length)) then
1329 Delete (Dir, File_Name (1 .. Last + Object_Suffix'Length));
1332 -- Change back to previous directory
1334 Change_Dir (Current);
1335 end Delete_Binder_Generated_Files;
1337 -----------------------
1338 -- Display_Copyright --
1339 -----------------------
1341 procedure Display_Copyright is
1343 if not Copyright_Displayed then
1344 Copyright_Displayed := True;
1346 ("GNATCLEAN " & Gnatvsn.Gnat_Version_String
1347 & " Copyright 2003-"
1349 & " Free Software Foundation, Inc.");
1351 end Display_Copyright;
1357 function Empty_Q return Boolean is
1359 return Q_Front >= Q.Last;
1362 --------------------
1363 -- Extract_From_Q --
1364 --------------------
1366 procedure Extract_From_Q (Lib_File : out File_Name_Type) is
1367 Lib : constant File_Name_Type := Q.Table (Q_Front);
1369 Q_Front := Q_Front + 1;
1377 procedure Gnatclean is
1379 -- Do the necessary initializations
1383 -- Parse the command line, getting the switches and the executable names
1387 if Verbose_Mode then
1391 if Project_File_Name /= null then
1393 -- A project file was specified by a -P switch
1395 if Opt.Verbose_Mode then
1397 Put ("Parsing Project File """);
1398 Put (Project_File_Name.all);
1403 -- Set the project parsing verbosity to whatever was specified
1404 -- by a possible -vP switch.
1406 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
1408 -- Parse the project file. If there is an error, Main_Project
1409 -- will still be No_Project.
1412 (Project => Main_Project,
1413 In_Tree => Project_Tree,
1414 Project_File_Name => Project_File_Name.all,
1415 Packages_To_Check => Packages_To_Check_By_Gnatmake);
1417 if Main_Project = No_Project then
1418 Fail ("""" & Project_File_Name.all & """ processing failed");
1421 if Opt.Verbose_Mode then
1423 Put ("Parsing of Project File """);
1424 Put (Project_File_Name.all);
1425 Put (""" is finished.");
1429 -- Add source directories and object directories to the search paths
1431 Add_Source_Directories (Main_Project, Project_Tree);
1432 Add_Object_Directories (Main_Project, Project_Tree);
1435 Osint.Add_Default_Search_Dirs;
1437 -- If a project file was specified, but no executable name, put all
1438 -- the mains of the project file (if any) as if there were on the
1441 if Main_Project /= No_Project and then Osint.Number_Of_Files = 0 then
1443 Value : String_List_Id :=
1444 Project_Tree.Projects.Table (Main_Project).Mains;
1445 Main : String_Element;
1447 while Value /= Prj.Nil_String loop
1448 Main := Project_Tree.String_Elements.Table (Value);
1450 (File_Name => Get_Name_String (Main.Value),
1451 Index => Main.Index);
1457 -- If neither a project file nor an executable were specified,
1458 -- output the usage and exit.
1460 if Main_Project = No_Project and then Osint.Number_Of_Files = 0 then
1465 if Verbose_Mode then
1469 if Main_Project /= No_Project then
1471 -- If a project file has been specified, call Clean_Project with the
1472 -- project id of this project file, after resetting the list of
1473 -- processed projects.
1475 Processed_Projects.Init;
1476 Clean_Project (Main_Project);
1479 -- If no project file has been specified, the work is done in
1480 -- Clean_Executables.
1485 -- In verbose mode, if Delete has not been called, indicate that
1486 -- no file needs to be deleted.
1488 if Verbose_Mode and (not File_Deleted) then
1492 Put_Line ("No file needs to be deleted");
1494 Put_Line ("No file has been deleted");
1499 ------------------------
1500 -- In_Extension_Chain --
1501 ------------------------
1503 function In_Extension_Chain
1504 (Of_Project : Project_Id;
1505 Prj : Project_Id) return Boolean
1507 Data : Project_Data;
1510 if Prj = No_Project or else Of_Project = No_Project then
1514 if Of_Project = Prj then
1518 Data := Project_Tree.Projects.Table (Of_Project);
1520 while Data.Extends /= No_Project loop
1521 if Data.Extends = Prj then
1525 Data := Project_Tree.Projects.Table (Data.Extends);
1528 Data := Project_Tree.Projects.Table (Prj);
1530 while Data.Extends /= No_Project loop
1531 if Data.Extends = Of_Project then
1535 Data := Project_Tree.Projects.Table (Data.Extends);
1539 end In_Extension_Chain;
1548 Q.Set_Last (Q.First);
1555 procedure Initialize is
1557 if not Initialized then
1558 Initialized := True;
1560 -- Get default search directories to locate system.ads when calling
1561 -- Targparm.Get_Target_Parameters.
1563 Osint.Add_Default_Search_Dirs;
1565 -- Initialize some packages
1570 Prj.Initialize (Project_Tree);
1572 -- Check if the platform is VMS and, if it is, change some variables
1574 Targparm.Get_Target_Parameters;
1576 if OpenVMS_On_Target then
1577 Debug_Suffix (Debug_Suffix'First) := '_';
1578 Repinfo_Suffix (Repinfo_Suffix'First) := '_';
1579 B_Start := new String'("b__");
1583 -- Reset global variables
1585 Free (Object_Directory_Path);
1586 Do_Nothing := False;
1587 File_Deleted := False;
1588 Copyright_Displayed := False;
1589 Usage_Displayed := False;
1590 Free (Project_File_Name);
1591 Main_Project := Prj.No_Project;
1592 All_Projects := False;
1599 procedure Insert_Q (Lib_File : File_Name_Type) is
1601 -- Do not insert an empty name or an already marked source
1603 if Lib_File /= No_File and then not Makeutl.Is_Marked (Lib_File) then
1604 Q.Table (Q.Last) := Lib_File;
1607 -- Mark the source that has been just added to the Q
1609 Makeutl.Mark (Lib_File);
1613 ----------------------
1614 -- Object_File_Name --
1615 ----------------------
1617 function Object_File_Name (Source : File_Name_Type) return String is
1618 Src : constant String := Get_Name_String (Source);
1621 -- If the source name has an extension, then replace it with
1622 -- the Object suffix.
1624 for Index in reverse Src'First + 1 .. Src'Last loop
1625 if Src (Index) = '.' then
1626 return Src (Src'First .. Index - 1) & Object_Suffix;
1630 -- If there is no dot, or if it is the first character, just add the
1633 return Src & Object_Suffix;
1634 end Object_File_Name;
1636 --------------------
1637 -- Parse_Cmd_Line --
1638 --------------------
1640 procedure Parse_Cmd_Line is
1641 Last : constant Natural := Argument_Count;
1642 Source_Index : Int := 0;
1643 Index : Positive := 1;
1646 while Index <= Last loop
1648 Arg : constant String := Argument (Index);
1650 procedure Bad_Argument;
1651 -- Signal bad argument
1657 procedure Bad_Argument is
1659 Fail ("invalid argument """, Arg, """");
1663 if Arg'Length /= 0 then
1664 if Arg (1) = '-' then
1665 if Arg'Length = 1 then
1671 if Arg'Length < 4 then
1675 if Arg (3) = 'O' then
1676 Add_Lib_Search_Dir (Arg (4 .. Arg'Last));
1678 elsif Arg (3) = 'P' then
1679 Prj.Ext.Add_Search_Project_Directory
1680 (Arg (4 .. Arg'Last));
1687 Compile_Only := True;
1690 if Object_Directory_Path /= null then
1691 Fail ("duplicate -D switch");
1693 elsif Project_File_Name /= null then
1694 Fail ("-P and -D cannot be used simultaneously");
1697 if Arg'Length > 2 then
1699 Dir : constant String := Arg (3 .. Arg'Last);
1701 if not Is_Directory (Dir) then
1702 Fail (Dir, " is not a directory");
1704 Add_Lib_Search_Dir (Dir);
1709 if Index = Last then
1710 Fail ("no directory specified after -D");
1716 Dir : constant String := Argument (Index);
1718 if not Is_Directory (Dir) then
1719 Fail (Dir, " is not a directory");
1721 Add_Lib_Search_Dir (Dir);
1727 Force_Deletions := True;
1730 Full_Path_Name_For_Brief_Errors := True;
1736 if Arg'Length = 2 then
1742 for J in 3 .. Arg'Last loop
1743 if Arg (J) not in '0' .. '9' then
1748 (20 * Source_Index) +
1749 (Character'Pos (Arg (J)) - Character'Pos ('0'));
1754 Opt.Look_In_Primary_Dir := False;
1757 if Arg'Length = 2 then
1761 Add_Lib_Search_Dir (Arg (3 .. Arg'Last));
1768 if Project_File_Name /= null then
1769 Fail ("multiple -P switches");
1771 elsif Object_Directory_Path /= null then
1772 Fail ("-D and -P cannot be used simultaneously");
1776 if Arg'Length > 2 then
1778 Prj : constant String := Arg (3 .. Arg'Last);
1780 if Prj'Length > 1 and then
1781 Prj (Prj'First) = '='
1783 Project_File_Name :=
1785 (Prj (Prj'First + 1 .. Prj'Last));
1787 Project_File_Name := new String'(Prj);
1792 if Index = Last then
1793 Fail ("no project specified after -P");
1797 Project_File_Name := new String'(Argument (Index));
1801 Quiet_Output := True;
1804 All_Projects := True;
1808 Verbose_Mode := True;
1810 elsif Arg = "-vP0" then
1811 Current_Verbosity := Prj.Default;
1813 elsif Arg = "-vP1" then
1814 Current_Verbosity := Prj.Medium;
1816 elsif Arg = "-vP2" then
1817 Current_Verbosity := Prj.High;
1824 if Arg'Length = 2 then
1829 Ext_Asgn : constant String := Arg (3 .. Arg'Last);
1830 Start : Positive := Ext_Asgn'First;
1831 Stop : Natural := Ext_Asgn'Last;
1832 Equal_Pos : Natural;
1833 OK : Boolean := True;
1836 if Ext_Asgn (Start) = '"' then
1837 if Ext_Asgn (Stop) = '"' then
1848 while Equal_Pos <= Stop
1849 and then Ext_Asgn (Equal_Pos) /= '='
1851 Equal_Pos := Equal_Pos + 1;
1854 if Equal_Pos = Start or else Equal_Pos > Stop then
1861 Ext_Asgn (Start .. Equal_Pos - 1),
1863 Ext_Asgn (Equal_Pos + 1 .. Stop));
1867 ("illegal external assignment '",
1877 Add_File (Arg, Source_Index);
1886 -----------------------
1887 -- Repinfo_File_Name --
1888 -----------------------
1890 function Repinfo_File_Name (Source : File_Name_Type) return String is
1892 return Get_Name_String (Source) & Repinfo_Suffix;
1893 end Repinfo_File_Name;
1895 --------------------
1896 -- Tree_File_Name --
1897 --------------------
1899 function Tree_File_Name (Source : File_Name_Type) return String is
1900 Src : constant String := Get_Name_String (Source);
1903 -- If the source name has an extension, then replace it with
1906 for Index in reverse Src'First + 1 .. Src'Last loop
1907 if Src (Index) = '.' then
1908 return Src (Src'First .. Index - 1) & Tree_Suffix;
1912 -- If there is no dot, or if it is the first character, just add the
1915 return Src & Tree_Suffix;
1918 ---------------------------
1919 -- Ultimate_Extension_Of --
1920 ---------------------------
1922 function Ultimate_Extension_Of (Project : Project_Id) return Project_Id is
1923 Result : Project_Id := Project;
1924 Data : Project_Data;
1927 if Project /= No_Project then
1929 Data := Project_Tree.Projects.Table (Result);
1930 exit when Data.Extended_By = No_Project;
1931 Result := Data.Extended_By;
1936 end Ultimate_Extension_Of;
1944 if not Usage_Displayed then
1945 Usage_Displayed := True;
1947 Put_Line ("Usage: gnatclean [switches] {[-innn] name}");
1950 Put_Line (" names is one or more file names from which " &
1951 "the .adb or .ads suffix may be omitted");
1952 Put_Line (" names may be omitted if -P<project> is specified");
1955 Put_Line (" -c Only delete compiler generated files");
1956 Put_Line (" -D dir Specify dir as the object library");
1957 Put_Line (" -f Force deletions of unwritable files");
1958 Put_Line (" -F Full project path name " &
1959 "in brief error messages");
1960 Put_Line (" -h Display this message");
1961 Put_Line (" -innn Index of unit in source for following names");
1962 Put_Line (" -n Nothing to do: only list files to delete");
1963 Put_Line (" -Pproj Use GNAT Project File proj");
1964 Put_Line (" -q Be quiet/terse");
1965 Put_Line (" -r Clean all projects recursively");
1966 Put_Line (" -v Verbose mode");
1967 Put_Line (" -vPx Specify verbosity when parsing " &
1968 "GNAT Project Files");
1969 Put_Line (" -Xnm=val Specify an external reference " &
1970 "for GNAT Project Files");
1973 Put_Line (" -aOdir Specify ALI/object files search path");
1974 Put_Line (" -Idir Like -aOdir");
1975 Put_Line (" -I- Don't look for source/library files " &
1976 "in the default directory");