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 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
29 with MLib.Tgt; use MLib.Tgt;
30 with Namet; use Namet;
32 with Osint; use Osint;
33 with Osint.M; use Osint.M;
38 with Prj.Util; use Prj.Util;
40 with Switch; use Switch;
42 with Targparm; use Targparm;
43 with Types; use Types;
45 with Ada.Command_Line; use Ada.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;
53 Initialized : Boolean := False;
54 -- Set to True by the first call to Initialize.
55 -- To avoid reinitialization of some packages.
57 -- Suffixes of various files
59 Assembly_Suffix : constant String := ".s";
60 ALI_Suffix : constant String := ".ali";
61 Tree_Suffix : constant String := ".adt";
62 Object_Suffix : constant String := Get_Target_Object_Suffix.all;
63 Debug_Suffix : String := ".dg";
64 -- Changed to "_dg" for VMS in the body of the package
66 Repinfo_Suffix : String := ".rep";
67 -- Changed to "_rep" for VMS in the body of the package
69 B_Start : String_Ptr := new String'("b~");
70 -- Prefix of binder generated file, and number of actual characters used.
71 -- Changed to "b__" for VMS in the body of the package.
73 Object_Directory_Path : String_Access := null;
74 -- The path name of the object directory, set with switch -D
76 Force_Deletions : Boolean := False;
77 -- Set to True by switch -f. When True, attempts to delete non writable
78 -- files will be done.
80 Do_Nothing : Boolean := False;
81 -- Set to True when switch -n is specified. When True, no file is deleted.
82 -- gnatclean only lists the files that would have been deleted if the
83 -- switch -n had not been specified.
85 File_Deleted : Boolean := False;
86 -- Set to True if at least one file has been deleted
88 Copyright_Displayed : Boolean := False;
89 Usage_Displayed : Boolean := False;
91 Project_File_Name : String_Access := null;
93 Project_Tree : constant Prj.Project_Tree_Ref := new Prj.Project_Tree_Data;
95 Main_Project : Prj.Project_Id := Prj.No_Project;
97 All_Projects : Boolean := False;
99 -- Packages of project files where unknown attributes are errors
101 Naming_String : aliased String := "naming";
102 Builder_String : aliased String := "builder";
103 Compiler_String : aliased String := "compiler";
104 Binder_String : aliased String := "binder";
105 Linker_String : aliased String := "linker";
107 Gnatmake_Packages : aliased String_List :=
108 (Naming_String 'Access,
109 Builder_String 'Access,
110 Compiler_String 'Access,
111 Binder_String 'Access,
112 Linker_String 'Access);
114 Packages_To_Check_By_Gnatmake : constant String_List_Access :=
115 Gnatmake_Packages'Access;
117 package Processed_Projects is new Table.Table
118 (Table_Component_Type => Project_Id,
119 Table_Index_Type => Natural,
120 Table_Low_Bound => 0,
122 Table_Increment => 100,
123 Table_Name => "Clean.Processed_Projects");
124 -- Table to keep track of what project files have been processed, when
125 -- switch -r is specified.
127 package Sources is new Table.Table
128 (Table_Component_Type => File_Name_Type,
129 Table_Index_Type => Natural,
130 Table_Low_Bound => 0,
132 Table_Increment => 100,
133 Table_Name => "Clean.Processed_Projects");
134 -- Table to store all the source files of a library unit: spec, body and
135 -- subunits, to detect .dg files and delete them.
137 ----------------------------
138 -- Queue (Q) manipulation --
139 ----------------------------
142 -- Must be called to initialize the Q
144 procedure Insert_Q (Lib_File : File_Name_Type);
145 -- If Lib_File is not marked, inserts it at the end of Q and mark it
147 function Empty_Q return Boolean;
148 -- Returns True if Q is empty
150 procedure Extract_From_Q (Lib_File : out File_Name_Type);
151 -- Extracts the first element from the Q
154 -- Points to the first valid element in the Q
156 package Q is new Table.Table (
157 Table_Component_Type => File_Name_Type,
158 Table_Index_Type => Natural,
159 Table_Low_Bound => 0,
160 Table_Initial => 4000,
161 Table_Increment => 100,
162 Table_Name => "Clean.Q");
163 -- This is the actual queue
165 -----------------------------
166 -- Other local subprograms --
167 -----------------------------
169 procedure Add_Source_Dir (N : String);
170 -- Call Add_Src_Search_Dir and output one line when in verbose mode
172 procedure Add_Source_Directories is
173 new Prj.Env.For_All_Source_Dirs (Action => Add_Source_Dir);
175 procedure Add_Object_Dir (N : String);
176 -- Call Add_Lib_Search_Dir and output one line when in verbose mode
178 procedure Add_Object_Directories is
179 new Prj.Env.For_All_Object_Dirs (Action => Add_Object_Dir);
181 function ALI_File_Name (Source : File_Name_Type) return String;
182 -- Returns the name of the ALI file corresponding to Source
184 function Assembly_File_Name (Source : File_Name_Type) return String;
185 -- Returns the assembly file name corresponding to Source
187 procedure Clean_Archive (Project : Project_Id; Global : Boolean);
188 -- Delete a global archive or library project archive and the dependency
189 -- file, if they exist.
191 procedure Clean_Executables;
192 -- Do the cleaning work when no project file is specified
194 procedure Clean_Interface_Copy_Directory (Project : Project_Id);
195 -- Delete files in an interface copy directory: any file that is a copy of
196 -- a source of the project.
198 procedure Clean_Library_Directory (Project : Project_Id);
199 -- Delete the library file in a library directory and any ALI file of a
200 -- source of the project in a library ALI directory.
202 procedure Clean_Project (Project : Project_Id);
203 -- Do the cleaning work when a project file is specified. This procedure
204 -- calls itself recursively when there are several project files in the
205 -- tree rooted at the main project file and switch -r has been specified.
207 function Debug_File_Name (Source : File_Name_Type) return String;
208 -- Name of the expanded source file corresponding to Source
210 procedure Delete (In_Directory : String; File : String);
211 -- Delete one file, or list the file name if switch -n is specified
213 procedure Delete_Binder_Generated_Files
215 Source : File_Name_Type);
216 -- Delete the binder generated file in directory Dir for Source, if they
217 -- exist: for Unix these are b~<source>.ads, b~<source>.adb,
218 -- b~<source>.ali and b~<source>.o.
220 procedure Display_Copyright;
221 -- Display the Copyright notice. If called several times, display the
222 -- Copyright notice only the first time.
224 procedure Initialize;
225 -- Call the necessary package initializations
227 function Object_File_Name (Source : File_Name_Type) return String;
228 -- Returns the object file name corresponding to Source
230 procedure Parse_Cmd_Line;
231 -- Parse the command line
233 function Repinfo_File_Name (Source : File_Name_Type) return String;
234 -- Returns the repinfo file name corresponding to Source
236 function Tree_File_Name (Source : File_Name_Type) return String;
237 -- Returns the tree file name corresponding to Source
239 function In_Extension_Chain
240 (Of_Project : Project_Id;
241 Prj : Project_Id) return Boolean;
242 -- Returns True iff Prj is an extension of Of_Project or if Of_Project is
243 -- an extension of Prj.
245 function Ultimate_Extension_Of (Project : Project_Id) return Project_Id;
246 -- Returns either Project, if it is not extended by another project, or
247 -- the project that extends Project, directly or indirectly, and that is
248 -- not itself extended. Returns No_Project if Project is No_Project.
251 -- Display the usage. If called several times, the usage is displayed only
258 procedure Add_Object_Dir (N : String) is
260 Add_Lib_Search_Dir (N);
262 if Opt.Verbose_Mode then
263 Put ("Adding object directory """);
274 procedure Add_Source_Dir (N : String) is
276 Add_Src_Search_Dir (N);
278 if Opt.Verbose_Mode then
279 Put ("Adding source directory """);
290 function ALI_File_Name (Source : File_Name_Type) return String is
291 Src : constant String := Get_Name_String (Source);
294 -- If the source name has an extension, then replace it with
297 for Index in reverse Src'First + 1 .. Src'Last loop
298 if Src (Index) = '.' then
299 return Src (Src'First .. Index - 1) & ALI_Suffix;
303 -- If there is no dot, or if it is the first character, just add the
306 return Src & ALI_Suffix;
309 ------------------------
310 -- Assembly_File_Name --
311 ------------------------
313 function Assembly_File_Name (Source : File_Name_Type) return String is
314 Src : constant String := Get_Name_String (Source);
317 -- If the source name has an extension, then replace it with
318 -- the assembly suffix.
320 for Index in reverse Src'First + 1 .. Src'Last loop
321 if Src (Index) = '.' then
322 return Src (Src'First .. Index - 1) & Assembly_Suffix;
326 -- If there is no dot, or if it is the first character, just add the
329 return Src & Assembly_Suffix;
330 end Assembly_File_Name;
336 procedure Clean_Archive (Project : Project_Id; Global : Boolean) is
337 Current_Dir : constant Dir_Name_Str := Get_Current_Dir;
339 Data : constant Project_Data := Project_Tree.Projects.Table (Project);
341 Lib_Prefix : String_Access;
342 Archive_Name : String_Access;
343 -- The name of the archive file for this project
345 Archive_Dep_Name : String_Access;
346 -- The name of the archive dependency file for this project
348 Obj_Dir : constant String :=
349 Get_Name_String (Data.Display_Object_Dir);
352 Change_Dir (Obj_Dir);
354 -- First, get the lib prefix, the archive file name and the archive
355 -- dependency file name.
359 new String'("lib" & Get_Name_String (Data.Display_Name));
362 new String'("lib" & Get_Name_String (Data.Library_Name));
365 Archive_Name := new String'(Lib_Prefix.all & '.' & Archive_Ext);
366 Archive_Dep_Name := new String'(Lib_Prefix.all & ".deps");
368 -- Delete the archive file and the archive dependency file, if they
371 if Is_Regular_File (Archive_Name.all) then
372 Delete (Obj_Dir, Archive_Name.all);
375 if Is_Regular_File (Archive_Dep_Name.all) then
376 Delete (Obj_Dir, Archive_Dep_Name.all);
379 Change_Dir (Current_Dir);
382 -----------------------
383 -- Clean_Executables --
384 -----------------------
386 procedure Clean_Executables is
387 Main_Source_File : File_Name_Type;
388 -- Current main source
390 Main_Lib_File : File_Name_Type;
391 -- ALI file of the current main
393 Lib_File : File_Name_Type;
396 Full_Lib_File : File_Name_Type;
397 -- Full name of the current ALI file
399 Text : Text_Buffer_Ptr;
405 -- It does not really matter if there is or not an object file
406 -- corresponding to an ALI file: if there is one, it will be deleted.
408 Opt.Check_Object_Consistency := False;
410 -- Proceed each executable one by one. Each source is marked as it is
411 -- processed, so common sources between executables will not be
412 -- processed several times.
414 for N_File in 1 .. Osint.Number_Of_Files loop
415 Main_Source_File := Next_Main_Source;
416 Main_Lib_File := Osint.Lib_File_Name
417 (Main_Source_File, Current_File_Index);
418 Insert_Q (Main_Lib_File);
420 while not Empty_Q loop
421 Sources.Set_Last (0);
422 Extract_From_Q (Lib_File);
423 Full_Lib_File := Osint.Full_Lib_File_Name (Lib_File);
425 -- If we have existing ALI file that is not read-only, process it
427 if Full_Lib_File /= No_File
428 and then not Is_Readonly_Library (Full_Lib_File)
430 Text := Read_Library_Info (Lib_File);
434 Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
437 -- If no error was produced while loading this ALI file,
438 -- insert into the queue all the unmarked withed sources.
440 if The_ALI /= No_ALI_Id then
441 for J in ALIs.Table (The_ALI).First_Unit ..
442 ALIs.Table (The_ALI).Last_Unit
444 Sources.Increment_Last;
445 Sources.Table (Sources.Last) :=
446 ALI.Units.Table (J).Sfile;
448 for K in ALI.Units.Table (J).First_With ..
449 ALI.Units.Table (J).Last_With
451 Insert_Q (Withs.Table (K).Afile);
455 -- Look for subunits and put them in the Sources table
457 for J in ALIs.Table (The_ALI).First_Sdep ..
458 ALIs.Table (The_ALI).Last_Sdep
460 if Sdep.Table (J).Subunit_Name /= No_Name then
461 Sources.Increment_Last;
462 Sources.Table (Sources.Last) :=
463 Sdep.Table (J).Sfile;
469 -- Now delete all existing files corresponding to this ALI file
472 Obj_Dir : constant String :=
473 Dir_Name (Get_Name_String (Full_Lib_File));
474 Obj : constant String := Object_File_Name (Lib_File);
475 Adt : constant String := Tree_File_Name (Lib_File);
476 Asm : constant String := Assembly_File_Name (Lib_File);
479 Delete (Obj_Dir, Get_Name_String (Lib_File));
481 if Is_Regular_File (Obj_Dir & Dir_Separator & Obj) then
482 Delete (Obj_Dir, Obj);
485 if Is_Regular_File (Obj_Dir & Dir_Separator & Adt) then
486 Delete (Obj_Dir, Adt);
489 if Is_Regular_File (Obj_Dir & Dir_Separator & Asm) then
490 Delete (Obj_Dir, Asm);
493 -- Delete expanded source files (.dg) and/or repinfo files
496 for J in 1 .. Sources.Last loop
498 Deb : constant String :=
499 Debug_File_Name (Sources.Table (J));
500 Rep : constant String :=
501 Repinfo_File_Name (Sources.Table (J));
504 if Is_Regular_File (Obj_Dir & Dir_Separator & Deb) then
505 Delete (Obj_Dir, Deb);
508 if Is_Regular_File (Obj_Dir & Dir_Separator & Rep) then
509 Delete (Obj_Dir, Rep);
517 -- Delete the executable, if it exists, and the binder generated
520 if not Compile_Only then
522 Source : constant File_Name_Type :=
523 Strip_Suffix (Main_Lib_File);
524 Executable : constant String :=
525 Get_Name_String (Executable_Name (Source));
527 if Is_Regular_File (Executable) then
528 Delete ("", Executable);
531 Delete_Binder_Generated_Files (Get_Current_Dir, Source);
535 end Clean_Executables;
537 ------------------------------------
538 -- Clean_Interface_Copy_Directory --
539 ------------------------------------
541 procedure Clean_Interface_Copy_Directory (Project : Project_Id) is
542 Current : constant String := Get_Current_Dir;
543 Data : constant Project_Data := Project_Tree.Projects.Table (Project);
547 Name : String (1 .. 200);
550 Delete_File : Boolean;
554 if Data.Library and then Data.Library_Src_Dir /= No_Path then
556 Directory : constant String :=
557 Get_Name_String (Data.Display_Library_Src_Dir);
560 Change_Dir (Directory);
563 -- For each regular file in the directory, if switch -n has not
564 -- been specified, make it writable and delete the file if it is
565 -- a copy of a source of the project.
568 Read (Direc, Name, Last);
572 Filename : constant String := Name (1 .. Last);
575 if Is_Regular_File (Filename) then
576 Canonical_Case_File_Name (Name (1 .. Last));
577 Delete_File := False;
579 -- Compare with source file names of the project
582 1 .. Unit_Table.Last (Project_Tree.Units)
584 Unit := Project_Tree.Units.Table (Index);
586 if Ultimate_Extension_Of
587 (Unit.File_Names (Body_Part).Project) = Project
590 (Unit.File_Names (Body_Part).Name) =
597 if Ultimate_Extension_Of
598 (Unit.File_Names (Specification).Project) = Project
601 (Unit.File_Names (Specification).Name) =
610 if not Do_Nothing then
611 Set_Writable (Filename);
614 Delete (Directory, Filename);
622 -- Restore the initial working directory
624 Change_Dir (Current);
627 end Clean_Interface_Copy_Directory;
629 -----------------------------
630 -- Clean_Library_Directory --
631 -----------------------------
633 Empty_String : aliased String := "";
635 procedure Clean_Library_Directory (Project : Project_Id) is
636 Current : constant String := Get_Current_Dir;
637 Data : constant Project_Data := Project_Tree.Projects.Table (Project);
639 Lib_Filename : constant String := Get_Name_String (Data.Library_Name);
641 DLL_Prefix & Lib_Filename & "." & DLL_Ext;
642 Archive_Name : String :=
643 "lib" & Lib_Filename & "." & Archive_Ext;
646 Name : String (1 .. 200);
649 Delete_File : Boolean;
651 Minor : String_Access := Empty_String'Unchecked_Access;
652 Major : String_Access := Empty_String'Unchecked_Access;
656 if Data.Library_Kind /= Static
657 and then MLib.Tgt.Library_Major_Minor_Id_Supported
658 and then Data.Lib_Internal_Name /= No_Name
660 Minor := new String'(Get_Name_String (Data.Lib_Internal_Name));
661 Major := new String'(MLib.Major_Id_Name (DLL_Name, Minor.all));
665 Lib_Directory : constant String :=
666 Get_Name_String (Data.Display_Library_Dir);
667 Lib_ALI_Directory : constant String :=
669 (Data.Display_Library_ALI_Dir);
672 Canonical_Case_File_Name (Archive_Name);
673 Canonical_Case_File_Name (DLL_Name);
675 Change_Dir (Lib_Directory);
678 -- For each regular file in the directory, if switch -n has not
679 -- been specified, make it writable and delete the file if it is
683 Read (Direc, Name, Last);
687 Filename : constant String := Name (1 .. Last);
689 if Is_Regular_File (Filename)
690 or else Is_Symbolic_Link (Filename)
692 Canonical_Case_File_Name (Name (1 .. Last));
693 Delete_File := False;
695 if (Data.Library_Kind = Static
696 and then Name (1 .. Last) = Archive_Name)
698 ((Data.Library_Kind = Dynamic or else
699 Data.Library_Kind = Relocatable)
701 (Name (1 .. Last) = DLL_Name
702 or else Name (1 .. Last) = Minor.all
703 or else Name (1 .. Last) = Major.all))
705 if not Do_Nothing then
706 Set_Writable (Filename);
709 Delete (Lib_Directory, Filename);
717 Change_Dir (Lib_ALI_Directory);
720 -- For each regular file in the directory, if switch -n has not
721 -- been specified, make it writable and delete the file if it is
722 -- any ALI file of a source of the project.
725 Read (Direc, Name, Last);
729 Filename : constant String := Name (1 .. Last);
731 if Is_Regular_File (Filename) then
732 Canonical_Case_File_Name (Name (1 .. Last));
733 Delete_File := False;
735 if Last > 4 and then Name (Last - 3 .. Last) = ".ali" then
739 -- Compare with ALI file names of the project
742 Index in 1 .. Unit_Table.Last (Project_Tree.Units)
744 Unit := Project_Tree.Units.Table (Index);
746 if Unit.File_Names (Body_Part).Project /=
749 if Ultimate_Extension_Of
750 (Unit.File_Names (Body_Part).Project) =
754 (Unit.File_Names (Body_Part).Name);
755 Name_Len := Name_Len -
757 (Name (1 .. Name_Len))'Length;
758 if Name_Buffer (1 .. Name_Len) =
766 elsif Ultimate_Extension_Of
767 (Unit.File_Names (Specification).Project) =
771 (Unit.File_Names (Specification).Name);
772 Name_Len := Name_Len -
774 (Name (1 .. Name_Len))'Length;
776 if Name_Buffer (1 .. Name_Len) =
788 if not Do_Nothing then
789 Set_Writable (Filename);
792 Delete (Lib_ALI_Directory, Filename);
800 -- Restore the initial working directory
802 Change_Dir (Current);
805 end Clean_Library_Directory;
811 procedure Clean_Project (Project : Project_Id) is
812 Main_Source_File : File_Name_Type;
813 -- Name of executable on the command line without directory info
815 Executable : File_Name_Type;
816 -- Name of the executable file
818 Current_Dir : constant Dir_Name_Str := Get_Current_Dir;
819 Data : constant Project_Data :=
820 Project_Tree.Projects.Table (Project);
822 File_Name1 : File_Name_Type;
824 File_Name2 : File_Name_Type;
826 Lib_File : File_Name_Type;
828 Source_Id : Other_Source_Id;
829 Source : Other_Source;
831 Global_Archive : Boolean := False;
834 -- Check that we don't specify executable on the command line for
835 -- a main library project.
837 if Project = Main_Project
838 and then Osint.Number_Of_Files /= 0
839 and then Data.Library
842 ("Cannot specify executable(s) for a Library Project File");
845 -- Nothing to clean in an externally built project
847 if Data.Externally_Built then
849 Put ("Nothing to do to clean externally built project """);
850 Put (Get_Name_String (Data.Name));
856 Put ("Cleaning project """);
857 Put (Get_Name_String (Data.Name));
861 -- Add project to the list of processed projects
863 Processed_Projects.Increment_Last;
864 Processed_Projects.Table (Processed_Projects.Last) := Project;
866 if Data.Object_Directory /= No_Path then
868 Obj_Dir : constant String :=
869 Get_Name_String (Data.Display_Object_Dir);
872 Change_Dir (Obj_Dir);
874 -- First, deal with Ada
876 -- Look through the units to find those that are either
877 -- immediate sources or inherited sources of the project.
878 -- Extending projects may have no language specified, if
879 -- Source_Dirs or Source_Files is specified as an empty list,
880 -- so always look for Ada units in extending projects.
882 if Data.Langs (Ada_Language_Index)
883 or else Data.Extends /= No_Project
885 for Unit in Unit_Table.First ..
886 Unit_Table.Last (Project_Tree.Units)
888 U_Data := Project_Tree.Units.Table (Unit);
889 File_Name1 := No_File;
890 File_Name2 := No_File;
892 -- If either the spec or the body is a source of the
893 -- project, check for the corresponding ALI file in the
896 if In_Extension_Chain
897 (U_Data.File_Names (Body_Part).Project, Project)
900 (U_Data.File_Names (Specification).Project, Project)
902 File_Name1 := U_Data.File_Names (Body_Part).Name;
903 Index1 := U_Data.File_Names (Body_Part).Index;
904 File_Name2 := U_Data.File_Names (Specification).Name;
905 Index2 := U_Data.File_Names (Specification).Index;
907 -- If there is no body file name, then there may be
910 if File_Name1 = No_File then
911 File_Name1 := File_Name2;
913 File_Name2 := No_File;
918 -- If there is either a spec or a body, look for files
919 -- in the object directory.
921 if File_Name1 /= No_File then
922 Lib_File := Osint.Lib_File_Name (File_Name1, Index1);
925 Asm : constant String :=
926 Assembly_File_Name (Lib_File);
927 ALI : constant String :=
928 ALI_File_Name (Lib_File);
929 Obj : constant String :=
930 Object_File_Name (Lib_File);
931 Adt : constant String :=
932 Tree_File_Name (Lib_File);
933 Deb : constant String :=
934 Debug_File_Name (File_Name1);
935 Rep : constant String :=
936 Repinfo_File_Name (File_Name1);
937 Del : Boolean := True;
940 -- If the ALI file exists and is read-only, no file
943 if Is_Regular_File (ALI) then
944 if Is_Writable_File (ALI) then
945 Delete (Obj_Dir, ALI);
954 if Obj_Dir (Obj_Dir'Last) /=
961 Put_Line (""" is read-only");
970 if Is_Regular_File (Obj) then
971 Delete (Obj_Dir, Obj);
976 if Is_Regular_File (Asm) then
977 Delete (Obj_Dir, Asm);
982 if Is_Regular_File (Adt) then
983 Delete (Obj_Dir, Adt);
986 -- First expanded source file
988 if Is_Regular_File (Deb) then
989 Delete (Obj_Dir, Deb);
994 if Is_Regular_File (Rep) then
995 Delete (Obj_Dir, Rep);
998 -- Second expanded source file
1000 if File_Name2 /= No_File then
1002 Deb : constant String :=
1003 Debug_File_Name (File_Name2);
1004 Rep : constant String :=
1005 Repinfo_File_Name (File_Name2);
1008 if Is_Regular_File (Deb) then
1009 Delete (Obj_Dir, Deb);
1012 if Is_Regular_File (Rep) then
1013 Delete (Obj_Dir, Rep);
1023 -- Check if a global archive and it dependency file could have
1024 -- been created and, if they exist, delete them.
1026 if Project = Main_Project and then not Data.Library then
1027 Global_Archive := False;
1029 for Proj in Project_Table.First ..
1030 Project_Table.Last (Project_Tree.Projects)
1032 if Project_Tree.Projects.Table
1033 (Proj).Other_Sources_Present
1035 Global_Archive := True;
1040 if Global_Archive then
1041 Clean_Archive (Project, Global => True);
1045 if Data.Other_Sources_Present then
1047 -- There is non-Ada code: delete the object files and
1048 -- the dependency files if they exist.
1050 Source_Id := Data.First_Other_Source;
1051 while Source_Id /= No_Other_Source loop
1053 Project_Tree.Other_Sources.Table (Source_Id);
1056 (Get_Name_String (Source.Object_Name))
1058 Delete (Obj_Dir, Get_Name_String (Source.Object_Name));
1062 Is_Regular_File (Get_Name_String (Source.Dep_Name))
1064 Delete (Obj_Dir, Get_Name_String (Source.Dep_Name));
1067 Source_Id := Source.Next;
1070 -- If it is a library with only non Ada sources, delete
1071 -- the fake archive and the dependency file, if they exist.
1074 and then not Data.Langs (Ada_Language_Index)
1076 Clean_Archive (Project, Global => False);
1082 -- If this is a library project, clean the library directory, the
1083 -- interface copy dir and, for a Stand-Alone Library, the binder
1084 -- generated files of the library.
1086 -- The directories are cleaned only if switch -c is not specified
1088 if Data.Library then
1089 if not Compile_Only then
1090 Clean_Library_Directory (Project);
1092 if Data.Library_Src_Dir /= No_Path then
1093 Clean_Interface_Copy_Directory (Project);
1097 if Data.Standalone_Library and then
1098 Data.Object_Directory /= No_Path
1100 Delete_Binder_Generated_Files
1101 (Get_Name_String (Data.Display_Object_Dir),
1102 File_Name_Type (Data.Library_Name));
1106 if Verbose_Mode then
1111 -- If switch -r is specified, call Clean_Project recursively for the
1112 -- imported projects and the project being extended.
1114 if All_Projects then
1116 Imported : Project_List := Data.Imported_Projects;
1117 Element : Project_Element;
1121 -- For each imported project, call Clean_Project if the project
1122 -- has not been processed already.
1124 while Imported /= Empty_Project_List loop
1125 Element := Project_Tree.Project_Lists.Table (Imported);
1126 Imported := Element.Next;
1130 J in Processed_Projects.First .. Processed_Projects.Last
1132 if Element.Project = Processed_Projects.Table (J) then
1139 Clean_Project (Element.Project);
1143 -- If this project extends another project, call Clean_Project for
1144 -- the project being extended. It is guaranteed that it has not
1145 -- called before, because no other project may import or extend
1148 if Data.Extends /= No_Project then
1149 Clean_Project (Data.Extends);
1154 -- For the main project, delete the executables and the binder
1157 -- The executables are deleted only if switch -c is not specified
1159 if Project = Main_Project and then Data.Exec_Directory /= No_Path then
1161 Exec_Dir : constant String :=
1162 Get_Name_String (Data.Display_Exec_Dir);
1165 Change_Dir (Exec_Dir);
1167 for N_File in 1 .. Osint.Number_Of_Files loop
1168 Main_Source_File := Next_Main_Source;
1170 if not Compile_Only then
1176 Current_File_Index);
1179 Exec_File_Name : constant String :=
1180 Get_Name_String (Executable);
1183 if Is_Absolute_Path (Name => Exec_File_Name) then
1184 if Is_Regular_File (Exec_File_Name) then
1185 Delete ("", Exec_File_Name);
1189 if Is_Regular_File (Exec_File_Name) then
1190 Delete (Exec_Dir, Exec_File_Name);
1196 if Data.Object_Directory /= No_Path then
1197 Delete_Binder_Generated_Files
1198 (Get_Name_String (Data.Display_Object_Dir),
1199 Strip_Suffix (Main_Source_File));
1205 -- Change back to previous directory
1207 Change_Dir (Current_Dir);
1210 ---------------------
1211 -- Debug_File_Name --
1212 ---------------------
1214 function Debug_File_Name (Source : File_Name_Type) return String is
1216 return Get_Name_String (Source) & Debug_Suffix;
1217 end Debug_File_Name;
1223 procedure Delete (In_Directory : String; File : String) is
1224 Full_Name : String (1 .. In_Directory'Length + File'Length + 1);
1225 Last : Natural := 0;
1229 -- Indicate that at least one file is deleted or is to be deleted
1231 File_Deleted := True;
1233 -- Build the path name of the file to delete
1235 Last := In_Directory'Length;
1236 Full_Name (1 .. Last) := In_Directory;
1238 if Last > 0 and then Full_Name (Last) /= Directory_Separator then
1240 Full_Name (Last) := Directory_Separator;
1243 Full_Name (Last + 1 .. Last + File'Length) := File;
1244 Last := Last + File'Length;
1246 -- If switch -n was used, simply output the path name
1249 Put_Line (Full_Name (1 .. Last));
1251 -- Otherwise, delete the file if it is writable
1255 or else Is_Writable_File (Full_Name (1 .. Last))
1256 or else Is_Symbolic_Link (Full_Name (1 .. Last))
1258 Delete_File (Full_Name (1 .. Last), Success);
1263 if Verbose_Mode or else not Quiet_Output then
1265 Put ("Warning: """);
1266 Put (Full_Name (1 .. Last));
1267 Put_Line (""" could not be deleted");
1271 Put (Full_Name (1 .. Last));
1272 Put_Line (""" has been deleted");
1278 -----------------------------------
1279 -- Delete_Binder_Generated_Files --
1280 -----------------------------------
1282 procedure Delete_Binder_Generated_Files
1284 Source : File_Name_Type)
1286 Source_Name : constant String := Get_Name_String (Source);
1287 Current : constant String := Get_Current_Dir;
1288 Last : constant Positive := B_Start'Length + Source_Name'Length;
1289 File_Name : String (1 .. Last + 4);
1294 -- Build the file name (before the extension)
1296 File_Name (1 .. B_Start'Length) := B_Start.all;
1297 File_Name (B_Start'Length + 1 .. Last) := Source_Name;
1301 File_Name (Last + 1 .. Last + 4) := ".ads";
1303 if Is_Regular_File (File_Name (1 .. Last + 4)) then
1304 Delete (Dir, File_Name (1 .. Last + 4));
1309 File_Name (Last + 1 .. Last + 4) := ".adb";
1311 if Is_Regular_File (File_Name (1 .. Last + 4)) then
1312 Delete (Dir, File_Name (1 .. Last + 4));
1317 File_Name (Last + 1 .. Last + 4) := ".ali";
1319 if Is_Regular_File (File_Name (1 .. Last + 4)) then
1320 Delete (Dir, File_Name (1 .. Last + 4));
1325 File_Name (Last + 1 .. Last + Object_Suffix'Length) := Object_Suffix;
1327 if Is_Regular_File (File_Name (1 .. Last + Object_Suffix'Length)) then
1328 Delete (Dir, File_Name (1 .. Last + Object_Suffix'Length));
1331 -- Change back to previous directory
1333 Change_Dir (Current);
1334 end Delete_Binder_Generated_Files;
1336 -----------------------
1337 -- Display_Copyright --
1338 -----------------------
1340 procedure Display_Copyright is
1342 if not Copyright_Displayed then
1343 Copyright_Displayed := True;
1344 Display_Version ("GNATCLEAN", "2003");
1346 end Display_Copyright;
1352 function Empty_Q return Boolean is
1354 return Q_Front >= Q.Last;
1357 --------------------
1358 -- Extract_From_Q --
1359 --------------------
1361 procedure Extract_From_Q (Lib_File : out File_Name_Type) is
1362 Lib : constant File_Name_Type := Q.Table (Q_Front);
1364 Q_Front := Q_Front + 1;
1372 procedure Gnatclean is
1374 -- Do the necessary initializations
1378 -- Parse the command line, getting the switches and the executable names
1382 if Verbose_Mode then
1386 if Project_File_Name /= null then
1388 -- A project file was specified by a -P switch
1390 if Opt.Verbose_Mode then
1392 Put ("Parsing Project File """);
1393 Put (Project_File_Name.all);
1398 -- Set the project parsing verbosity to whatever was specified
1399 -- by a possible -vP switch.
1401 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
1403 -- Parse the project file. If there is an error, Main_Project
1404 -- will still be No_Project.
1407 (Project => Main_Project,
1408 In_Tree => Project_Tree,
1409 Project_File_Name => Project_File_Name.all,
1410 Packages_To_Check => Packages_To_Check_By_Gnatmake);
1412 if Main_Project = No_Project then
1413 Fail ("""" & Project_File_Name.all & """ processing failed");
1416 if Opt.Verbose_Mode then
1418 Put ("Parsing of Project File """);
1419 Put (Project_File_Name.all);
1420 Put (""" is finished.");
1424 -- Add source directories and object directories to the search paths
1426 Add_Source_Directories (Main_Project, Project_Tree);
1427 Add_Object_Directories (Main_Project, Project_Tree);
1430 Osint.Add_Default_Search_Dirs;
1432 -- If a project file was specified, but no executable name, put all
1433 -- the mains of the project file (if any) as if there were on the
1436 if Main_Project /= No_Project and then Osint.Number_Of_Files = 0 then
1438 Value : String_List_Id :=
1439 Project_Tree.Projects.Table (Main_Project).Mains;
1440 Main : String_Element;
1442 while Value /= Prj.Nil_String loop
1443 Main := Project_Tree.String_Elements.Table (Value);
1445 (File_Name => Get_Name_String (Main.Value),
1446 Index => Main.Index);
1452 -- If neither a project file nor an executable were specified,
1453 -- output the usage and exit.
1455 if Main_Project = No_Project and then Osint.Number_Of_Files = 0 then
1460 if Verbose_Mode then
1464 if Main_Project /= No_Project then
1466 -- If a project file has been specified, call Clean_Project with the
1467 -- project id of this project file, after resetting the list of
1468 -- processed projects.
1470 Processed_Projects.Init;
1471 Clean_Project (Main_Project);
1474 -- If no project file has been specified, the work is done in
1475 -- Clean_Executables.
1480 -- In verbose mode, if Delete has not been called, indicate that
1481 -- no file needs to be deleted.
1483 if Verbose_Mode and (not File_Deleted) then
1487 Put_Line ("No file needs to be deleted");
1489 Put_Line ("No file has been deleted");
1494 ------------------------
1495 -- In_Extension_Chain --
1496 ------------------------
1498 function In_Extension_Chain
1499 (Of_Project : Project_Id;
1500 Prj : Project_Id) return Boolean
1502 Data : Project_Data;
1505 if Prj = No_Project or else Of_Project = No_Project then
1509 if Of_Project = Prj then
1513 Data := Project_Tree.Projects.Table (Of_Project);
1515 while Data.Extends /= No_Project loop
1516 if Data.Extends = Prj then
1520 Data := Project_Tree.Projects.Table (Data.Extends);
1523 Data := Project_Tree.Projects.Table (Prj);
1525 while Data.Extends /= No_Project loop
1526 if Data.Extends = Of_Project then
1530 Data := Project_Tree.Projects.Table (Data.Extends);
1534 end In_Extension_Chain;
1543 Q.Set_Last (Q.First);
1550 procedure Initialize is
1552 if not Initialized then
1553 Initialized := True;
1555 -- Get default search directories to locate system.ads when calling
1556 -- Targparm.Get_Target_Parameters.
1558 Osint.Add_Default_Search_Dirs;
1560 -- Initialize some packages
1565 Prj.Initialize (Project_Tree);
1567 -- Check if the platform is VMS and, if it is, change some variables
1569 Targparm.Get_Target_Parameters;
1571 if OpenVMS_On_Target then
1572 Debug_Suffix (Debug_Suffix'First) := '_';
1573 Repinfo_Suffix (Repinfo_Suffix'First) := '_';
1574 B_Start := new String'("b__");
1578 -- Reset global variables
1580 Free (Object_Directory_Path);
1581 Do_Nothing := False;
1582 File_Deleted := False;
1583 Copyright_Displayed := False;
1584 Usage_Displayed := False;
1585 Free (Project_File_Name);
1586 Main_Project := Prj.No_Project;
1587 All_Projects := False;
1594 procedure Insert_Q (Lib_File : File_Name_Type) is
1596 -- Do not insert an empty name or an already marked source
1598 if Lib_File /= No_File and then not Makeutl.Is_Marked (Lib_File) then
1599 Q.Table (Q.Last) := Lib_File;
1602 -- Mark the source that has been just added to the Q
1604 Makeutl.Mark (Lib_File);
1608 ----------------------
1609 -- Object_File_Name --
1610 ----------------------
1612 function Object_File_Name (Source : File_Name_Type) return String is
1613 Src : constant String := Get_Name_String (Source);
1616 -- If the source name has an extension, then replace it with
1617 -- the Object suffix.
1619 for Index in reverse Src'First + 1 .. Src'Last loop
1620 if Src (Index) = '.' then
1621 return Src (Src'First .. Index - 1) & Object_Suffix;
1625 -- If there is no dot, or if it is the first character, just add the
1628 return Src & Object_Suffix;
1629 end Object_File_Name;
1631 --------------------
1632 -- Parse_Cmd_Line --
1633 --------------------
1635 procedure Parse_Cmd_Line is
1636 Last : constant Natural := Argument_Count;
1637 Source_Index : Int := 0;
1641 -- First, check for --version and --help
1643 Check_Version_And_Help ("GNATCLEAN", "2003", Usage'Access);
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");