1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2003-2008, 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.Object_Directory.Display_Name);
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_Information then
556 Directory : constant String :=
557 Get_Name_String (Data.Library_Src_Dir.Display_Name);
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'Access;
652 Major : String_Access := Empty_String'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 :=
667 (Data.Library_Dir.Display_Name);
668 Lib_ALI_Directory : constant String :=
670 (Data.Library_ALI_Dir.Display_Name);
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 Global_Archive : Boolean := False;
832 -- Check that we don't specify executable on the command line for
833 -- a main library project.
835 if Project = Main_Project
836 and then Osint.Number_Of_Files /= 0
837 and then Data.Library
840 ("Cannot specify executable(s) for a Library Project File");
843 -- Nothing to clean in an externally built project
845 if Data.Externally_Built then
847 Put ("Nothing to do to clean externally built project """);
848 Put (Get_Name_String (Data.Name));
854 Put ("Cleaning project """);
855 Put (Get_Name_String (Data.Name));
859 -- Add project to the list of processed projects
861 Processed_Projects.Increment_Last;
862 Processed_Projects.Table (Processed_Projects.Last) := Project;
864 if Data.Object_Directory /= No_Path_Information then
866 Obj_Dir : constant String :=
868 (Data.Object_Directory.Display_Name);
871 Change_Dir (Obj_Dir);
873 -- First, deal with Ada
875 -- Look through the units to find those that are either
876 -- immediate sources or inherited sources of the project.
877 -- Extending projects may have no language specified, if
878 -- Source_Dirs or Source_Files is specified as an empty list,
879 -- so always look for Ada units in extending projects.
881 if Data.Ada_Sources_Present
882 or else Data.Extends /= No_Project
884 for Unit in Unit_Table.First ..
885 Unit_Table.Last (Project_Tree.Units)
887 U_Data := Project_Tree.Units.Table (Unit);
888 File_Name1 := No_File;
889 File_Name2 := No_File;
891 -- If either the spec or the body is a source of the
892 -- project, check for the corresponding ALI file in the
895 if In_Extension_Chain
896 (U_Data.File_Names (Body_Part).Project, Project)
899 (U_Data.File_Names (Specification).Project, Project)
901 File_Name1 := U_Data.File_Names (Body_Part).Name;
902 Index1 := U_Data.File_Names (Body_Part).Index;
903 File_Name2 := U_Data.File_Names (Specification).Name;
904 Index2 := U_Data.File_Names (Specification).Index;
906 -- If there is no body file name, then there may be
909 if File_Name1 = No_File then
910 File_Name1 := File_Name2;
912 File_Name2 := No_File;
917 -- If there is either a spec or a body, look for files
918 -- in the object directory.
920 if File_Name1 /= No_File then
921 Lib_File := Osint.Lib_File_Name (File_Name1, Index1);
924 Asm : constant String :=
925 Assembly_File_Name (Lib_File);
926 ALI : constant String :=
927 ALI_File_Name (Lib_File);
928 Obj : constant String :=
929 Object_File_Name (Lib_File);
930 Adt : constant String :=
931 Tree_File_Name (Lib_File);
932 Deb : constant String :=
933 Debug_File_Name (File_Name1);
934 Rep : constant String :=
935 Repinfo_File_Name (File_Name1);
936 Del : Boolean := True;
939 -- If the ALI file exists and is read-only, no file
942 if Is_Regular_File (ALI) then
943 if Is_Writable_File (ALI) then
944 Delete (Obj_Dir, ALI);
953 if Obj_Dir (Obj_Dir'Last) /=
960 Put_Line (""" is read-only");
969 if Is_Regular_File (Obj) then
970 Delete (Obj_Dir, Obj);
975 if Is_Regular_File (Asm) then
976 Delete (Obj_Dir, Asm);
981 if Is_Regular_File (Adt) then
982 Delete (Obj_Dir, Adt);
985 -- First expanded source file
987 if Is_Regular_File (Deb) then
988 Delete (Obj_Dir, Deb);
993 if Is_Regular_File (Rep) then
994 Delete (Obj_Dir, Rep);
997 -- Second expanded source file
999 if File_Name2 /= No_File then
1001 Deb : constant String :=
1002 Debug_File_Name (File_Name2);
1003 Rep : constant String :=
1004 Repinfo_File_Name (File_Name2);
1007 if Is_Regular_File (Deb) then
1008 Delete (Obj_Dir, Deb);
1011 if Is_Regular_File (Rep) then
1012 Delete (Obj_Dir, Rep);
1022 -- Check if a global archive and it dependency file could have
1023 -- been created and, if they exist, delete them.
1025 if Project = Main_Project and then not Data.Library then
1026 Global_Archive := False;
1028 for Proj in Project_Table.First ..
1029 Project_Table.Last (Project_Tree.Projects)
1031 if Project_Tree.Projects.Table
1032 (Proj).Other_Sources_Present
1034 Global_Archive := True;
1039 if Global_Archive then
1040 Clean_Archive (Project, Global => True);
1047 -- If this is a library project, clean the library directory, the
1048 -- interface copy dir and, for a Stand-Alone Library, the binder
1049 -- generated files of the library.
1051 -- The directories are cleaned only if switch -c is not specified
1053 if Data.Library then
1054 if not Compile_Only then
1055 Clean_Library_Directory (Project);
1057 if Data.Library_Src_Dir /= No_Path_Information then
1058 Clean_Interface_Copy_Directory (Project);
1062 if Data.Standalone_Library and then
1063 Data.Object_Directory /= No_Path_Information
1065 Delete_Binder_Generated_Files
1066 (Get_Name_String (Data.Object_Directory.Display_Name),
1067 File_Name_Type (Data.Library_Name));
1071 if Verbose_Mode then
1076 -- If switch -r is specified, call Clean_Project recursively for the
1077 -- imported projects and the project being extended.
1079 if All_Projects then
1081 Imported : Project_List := Data.Imported_Projects;
1082 Element : Project_Element;
1086 -- For each imported project, call Clean_Project if the project
1087 -- has not been processed already.
1089 while Imported /= Empty_Project_List loop
1090 Element := Project_Tree.Project_Lists.Table (Imported);
1091 Imported := Element.Next;
1095 J in Processed_Projects.First .. Processed_Projects.Last
1097 if Element.Project = Processed_Projects.Table (J) then
1104 Clean_Project (Element.Project);
1108 -- If this project extends another project, call Clean_Project for
1109 -- the project being extended. It is guaranteed that it has not
1110 -- called before, because no other project may import or extend
1113 if Data.Extends /= No_Project then
1114 Clean_Project (Data.Extends);
1119 -- For the main project, delete the executables and the binder
1122 -- The executables are deleted only if switch -c is not specified
1124 if Project = Main_Project
1125 and then Data.Exec_Directory /= No_Path_Information
1128 Exec_Dir : constant String :=
1129 Get_Name_String (Data.Exec_Directory.Display_Name);
1132 Change_Dir (Exec_Dir);
1134 for N_File in 1 .. Osint.Number_Of_Files loop
1135 Main_Source_File := Next_Main_Source;
1137 if not Compile_Only then
1143 Current_File_Index);
1146 Exec_File_Name : constant String :=
1147 Get_Name_String (Executable);
1150 if Is_Absolute_Path (Name => Exec_File_Name) then
1151 if Is_Regular_File (Exec_File_Name) then
1152 Delete ("", Exec_File_Name);
1156 if Is_Regular_File (Exec_File_Name) then
1157 Delete (Exec_Dir, Exec_File_Name);
1163 if Data.Object_Directory /= No_Path_Information then
1164 Delete_Binder_Generated_Files
1165 (Get_Name_String (Data.Object_Directory.Display_Name),
1166 Strip_Suffix (Main_Source_File));
1172 -- Change back to previous directory
1174 Change_Dir (Current_Dir);
1177 ---------------------
1178 -- Debug_File_Name --
1179 ---------------------
1181 function Debug_File_Name (Source : File_Name_Type) return String is
1183 return Get_Name_String (Source) & Debug_Suffix;
1184 end Debug_File_Name;
1190 procedure Delete (In_Directory : String; File : String) is
1191 Full_Name : String (1 .. In_Directory'Length + File'Length + 1);
1192 Last : Natural := 0;
1196 -- Indicate that at least one file is deleted or is to be deleted
1198 File_Deleted := True;
1200 -- Build the path name of the file to delete
1202 Last := In_Directory'Length;
1203 Full_Name (1 .. Last) := In_Directory;
1205 if Last > 0 and then Full_Name (Last) /= Directory_Separator then
1207 Full_Name (Last) := Directory_Separator;
1210 Full_Name (Last + 1 .. Last + File'Length) := File;
1211 Last := Last + File'Length;
1213 -- If switch -n was used, simply output the path name
1216 Put_Line (Full_Name (1 .. Last));
1218 -- Otherwise, delete the file if it is writable
1222 or else Is_Writable_File (Full_Name (1 .. Last))
1223 or else Is_Symbolic_Link (Full_Name (1 .. Last))
1225 Delete_File (Full_Name (1 .. Last), Success);
1230 if Verbose_Mode or else not Quiet_Output then
1232 Put ("Warning: """);
1233 Put (Full_Name (1 .. Last));
1234 Put_Line (""" could not be deleted");
1238 Put (Full_Name (1 .. Last));
1239 Put_Line (""" has been deleted");
1245 -----------------------------------
1246 -- Delete_Binder_Generated_Files --
1247 -----------------------------------
1249 procedure Delete_Binder_Generated_Files
1251 Source : File_Name_Type)
1253 Source_Name : constant String := Get_Name_String (Source);
1254 Current : constant String := Get_Current_Dir;
1255 Last : constant Positive := B_Start'Length + Source_Name'Length;
1256 File_Name : String (1 .. Last + 4);
1261 -- Build the file name (before the extension)
1263 File_Name (1 .. B_Start'Length) := B_Start.all;
1264 File_Name (B_Start'Length + 1 .. Last) := Source_Name;
1268 File_Name (Last + 1 .. Last + 4) := ".ads";
1270 if Is_Regular_File (File_Name (1 .. Last + 4)) then
1271 Delete (Dir, File_Name (1 .. Last + 4));
1276 File_Name (Last + 1 .. Last + 4) := ".adb";
1278 if Is_Regular_File (File_Name (1 .. Last + 4)) then
1279 Delete (Dir, File_Name (1 .. Last + 4));
1284 File_Name (Last + 1 .. Last + 4) := ".ali";
1286 if Is_Regular_File (File_Name (1 .. Last + 4)) then
1287 Delete (Dir, File_Name (1 .. Last + 4));
1292 File_Name (Last + 1 .. Last + Object_Suffix'Length) := Object_Suffix;
1294 if Is_Regular_File (File_Name (1 .. Last + Object_Suffix'Length)) then
1295 Delete (Dir, File_Name (1 .. Last + Object_Suffix'Length));
1298 -- Change back to previous directory
1300 Change_Dir (Current);
1301 end Delete_Binder_Generated_Files;
1303 -----------------------
1304 -- Display_Copyright --
1305 -----------------------
1307 procedure Display_Copyright is
1309 if not Copyright_Displayed then
1310 Copyright_Displayed := True;
1311 Display_Version ("GNATCLEAN", "2003");
1313 end Display_Copyright;
1319 function Empty_Q return Boolean is
1321 return Q_Front >= Q.Last;
1324 --------------------
1325 -- Extract_From_Q --
1326 --------------------
1328 procedure Extract_From_Q (Lib_File : out File_Name_Type) is
1329 Lib : constant File_Name_Type := Q.Table (Q_Front);
1331 Q_Front := Q_Front + 1;
1339 procedure Gnatclean is
1341 -- Do the necessary initializations
1345 -- Parse the command line, getting the switches and the executable names
1349 if Verbose_Mode then
1353 if Project_File_Name /= null then
1355 -- A project file was specified by a -P switch
1357 if Opt.Verbose_Mode then
1359 Put ("Parsing Project File """);
1360 Put (Project_File_Name.all);
1365 -- Set the project parsing verbosity to whatever was specified
1366 -- by a possible -vP switch.
1368 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
1370 -- Parse the project file. If there is an error, Main_Project
1371 -- will still be No_Project.
1374 (Project => Main_Project,
1375 In_Tree => Project_Tree,
1376 Project_File_Name => Project_File_Name.all,
1377 Packages_To_Check => Packages_To_Check_By_Gnatmake);
1379 if Main_Project = No_Project then
1380 Fail ("""" & Project_File_Name.all & """ processing failed");
1383 if Opt.Verbose_Mode then
1385 Put ("Parsing of Project File """);
1386 Put (Project_File_Name.all);
1387 Put (""" is finished.");
1391 -- Add source directories and object directories to the search paths
1393 Add_Source_Directories (Main_Project, Project_Tree);
1394 Add_Object_Directories (Main_Project, Project_Tree);
1397 Osint.Add_Default_Search_Dirs;
1399 -- If a project file was specified, but no executable name, put all
1400 -- the mains of the project file (if any) as if there were on the
1403 if Main_Project /= No_Project and then Osint.Number_Of_Files = 0 then
1405 Value : String_List_Id :=
1406 Project_Tree.Projects.Table (Main_Project).Mains;
1407 Main : String_Element;
1409 while Value /= Prj.Nil_String loop
1410 Main := Project_Tree.String_Elements.Table (Value);
1412 (File_Name => Get_Name_String (Main.Value),
1413 Index => Main.Index);
1419 -- If neither a project file nor an executable were specified,
1420 -- output the usage and exit.
1422 if Main_Project = No_Project and then Osint.Number_Of_Files = 0 then
1427 if Verbose_Mode then
1431 if Main_Project /= No_Project then
1433 -- If a project file has been specified, call Clean_Project with the
1434 -- project id of this project file, after resetting the list of
1435 -- processed projects.
1437 Processed_Projects.Init;
1438 Clean_Project (Main_Project);
1441 -- If no project file has been specified, the work is done in
1442 -- Clean_Executables.
1447 -- In verbose mode, if Delete has not been called, indicate that
1448 -- no file needs to be deleted.
1450 if Verbose_Mode and (not File_Deleted) then
1454 Put_Line ("No file needs to be deleted");
1456 Put_Line ("No file has been deleted");
1461 ------------------------
1462 -- In_Extension_Chain --
1463 ------------------------
1465 function In_Extension_Chain
1466 (Of_Project : Project_Id;
1467 Prj : Project_Id) return Boolean
1469 Data : Project_Data;
1472 if Prj = No_Project or else Of_Project = No_Project then
1476 if Of_Project = Prj then
1480 Data := Project_Tree.Projects.Table (Of_Project);
1482 while Data.Extends /= No_Project loop
1483 if Data.Extends = Prj then
1487 Data := Project_Tree.Projects.Table (Data.Extends);
1490 Data := Project_Tree.Projects.Table (Prj);
1492 while Data.Extends /= No_Project loop
1493 if Data.Extends = Of_Project then
1497 Data := Project_Tree.Projects.Table (Data.Extends);
1501 end In_Extension_Chain;
1510 Q.Set_Last (Q.First);
1517 procedure Initialize is
1519 if not Initialized then
1520 Initialized := True;
1522 -- Get default search directories to locate system.ads when calling
1523 -- Targparm.Get_Target_Parameters.
1525 Osint.Add_Default_Search_Dirs;
1527 -- Initialize some packages
1532 Prj.Initialize (Project_Tree);
1534 -- Check if the platform is VMS and, if it is, change some variables
1536 Targparm.Get_Target_Parameters;
1538 if OpenVMS_On_Target then
1539 Debug_Suffix (Debug_Suffix'First) := '_';
1540 Repinfo_Suffix (Repinfo_Suffix'First) := '_';
1541 B_Start := new String'("b__");
1545 -- Reset global variables
1547 Free (Object_Directory_Path);
1548 Do_Nothing := False;
1549 File_Deleted := False;
1550 Copyright_Displayed := False;
1551 Usage_Displayed := False;
1552 Free (Project_File_Name);
1553 Main_Project := Prj.No_Project;
1554 All_Projects := False;
1561 procedure Insert_Q (Lib_File : File_Name_Type) is
1563 -- Do not insert an empty name or an already marked source
1565 if Lib_File /= No_File and then not Makeutl.Is_Marked (Lib_File) then
1566 Q.Table (Q.Last) := Lib_File;
1569 -- Mark the source that has been just added to the Q
1571 Makeutl.Mark (Lib_File);
1575 ----------------------
1576 -- Object_File_Name --
1577 ----------------------
1579 function Object_File_Name (Source : File_Name_Type) return String is
1580 Src : constant String := Get_Name_String (Source);
1583 -- If the source name has an extension, then replace it with
1584 -- the Object suffix.
1586 for Index in reverse Src'First + 1 .. Src'Last loop
1587 if Src (Index) = '.' then
1588 return Src (Src'First .. Index - 1) & Object_Suffix;
1592 -- If there is no dot, or if it is the first character, just add the
1595 return Src & Object_Suffix;
1596 end Object_File_Name;
1598 --------------------
1599 -- Parse_Cmd_Line --
1600 --------------------
1602 procedure Parse_Cmd_Line is
1603 Last : constant Natural := Argument_Count;
1604 Source_Index : Int := 0;
1607 procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
1610 -- First, check for --version and --help
1612 Check_Version_And_Help ("GNATCLEAN", "2003");
1615 while Index <= Last loop
1617 Arg : constant String := Argument (Index);
1619 procedure Bad_Argument;
1620 -- Signal bad argument
1626 procedure Bad_Argument is
1628 Fail ("invalid argument """, Arg, """");
1632 if Arg'Length /= 0 then
1633 if Arg (1) = '-' then
1634 if Arg'Length = 1 then
1640 if Arg'Length > Subdirs_Option'Length and then
1641 Arg (1 .. Subdirs_Option'Length) = Subdirs_Option
1645 (Arg (Subdirs_Option'Length + 1 .. Arg'Last));
1652 if Arg'Length < 4 then
1656 if Arg (3) = 'O' then
1657 Add_Lib_Search_Dir (Arg (4 .. Arg'Last));
1659 elsif Arg (3) = 'P' then
1660 Prj.Ext.Add_Search_Project_Directory
1661 (Arg (4 .. Arg'Last));
1668 Compile_Only := True;
1671 if Object_Directory_Path /= null then
1672 Fail ("duplicate -D switch");
1674 elsif Project_File_Name /= null then
1675 Fail ("-P and -D cannot be used simultaneously");
1678 if Arg'Length > 2 then
1680 Dir : constant String := Arg (3 .. Arg'Last);
1682 if not Is_Directory (Dir) then
1683 Fail (Dir, " is not a directory");
1685 Add_Lib_Search_Dir (Dir);
1690 if Index = Last then
1691 Fail ("no directory specified after -D");
1697 Dir : constant String := Argument (Index);
1699 if not Is_Directory (Dir) then
1700 Fail (Dir, " is not a directory");
1702 Add_Lib_Search_Dir (Dir);
1709 Follow_Links_For_Files := True;
1716 Force_Deletions := True;
1719 Full_Path_Name_For_Brief_Errors := True;
1725 if Arg'Length = 2 then
1731 for J in 3 .. Arg'Last loop
1732 if Arg (J) not in '0' .. '9' then
1737 (20 * Source_Index) +
1738 (Character'Pos (Arg (J)) - Character'Pos ('0'));
1743 Opt.Look_In_Primary_Dir := False;
1746 if Arg'Length = 2 then
1750 Add_Lib_Search_Dir (Arg (3 .. Arg'Last));
1757 if Project_File_Name /= null then
1758 Fail ("multiple -P switches");
1760 elsif Object_Directory_Path /= null then
1761 Fail ("-D and -P cannot be used simultaneously");
1765 if Arg'Length > 2 then
1767 Prj : constant String := Arg (3 .. Arg'Last);
1769 if Prj'Length > 1 and then
1770 Prj (Prj'First) = '='
1772 Project_File_Name :=
1774 (Prj (Prj'First + 1 .. Prj'Last));
1776 Project_File_Name := new String'(Prj);
1781 if Index = Last then
1782 Fail ("no project specified after -P");
1786 Project_File_Name := new String'(Argument (Index));
1790 Quiet_Output := True;
1793 All_Projects := True;
1797 Verbose_Mode := True;
1799 elsif Arg = "-vP0" then
1800 Current_Verbosity := Prj.Default;
1802 elsif Arg = "-vP1" then
1803 Current_Verbosity := Prj.Medium;
1805 elsif Arg = "-vP2" then
1806 Current_Verbosity := Prj.High;
1813 if Arg'Length = 2 then
1818 Ext_Asgn : constant String := Arg (3 .. Arg'Last);
1819 Start : Positive := Ext_Asgn'First;
1820 Stop : Natural := Ext_Asgn'Last;
1821 Equal_Pos : Natural;
1822 OK : Boolean := True;
1825 if Ext_Asgn (Start) = '"' then
1826 if Ext_Asgn (Stop) = '"' then
1837 while Equal_Pos <= Stop
1838 and then Ext_Asgn (Equal_Pos) /= '='
1840 Equal_Pos := Equal_Pos + 1;
1843 if Equal_Pos = Start or else Equal_Pos > Stop then
1850 Ext_Asgn (Start .. Equal_Pos - 1),
1852 Ext_Asgn (Equal_Pos + 1 .. Stop));
1856 ("illegal external assignment '",
1866 Add_File (Arg, Source_Index);
1875 -----------------------
1876 -- Repinfo_File_Name --
1877 -----------------------
1879 function Repinfo_File_Name (Source : File_Name_Type) return String is
1881 return Get_Name_String (Source) & Repinfo_Suffix;
1882 end Repinfo_File_Name;
1884 --------------------
1885 -- Tree_File_Name --
1886 --------------------
1888 function Tree_File_Name (Source : File_Name_Type) return String is
1889 Src : constant String := Get_Name_String (Source);
1892 -- If the source name has an extension, then replace it with
1895 for Index in reverse Src'First + 1 .. Src'Last loop
1896 if Src (Index) = '.' then
1897 return Src (Src'First .. Index - 1) & Tree_Suffix;
1901 -- If there is no dot, or if it is the first character, just add the
1904 return Src & Tree_Suffix;
1907 ---------------------------
1908 -- Ultimate_Extension_Of --
1909 ---------------------------
1911 function Ultimate_Extension_Of (Project : Project_Id) return Project_Id is
1912 Result : Project_Id := Project;
1913 Data : Project_Data;
1916 if Project /= No_Project then
1918 Data := Project_Tree.Projects.Table (Result);
1919 exit when Data.Extended_By = No_Project;
1920 Result := Data.Extended_By;
1925 end Ultimate_Extension_Of;
1933 if not Usage_Displayed then
1934 Usage_Displayed := True;
1936 Put_Line ("Usage: gnatclean [switches] {[-innn] name}");
1939 Put_Line (" names is one or more file names from which " &
1940 "the .adb or .ads suffix may be omitted");
1941 Put_Line (" names may be omitted if -P<project> is specified");
1944 Put_Line (" --subdirs=dir real obj/lib/exec dirs are subdirs");
1947 Put_Line (" -c Only delete compiler generated files");
1948 Put_Line (" -D dir Specify dir as the object library");
1949 Put_Line (" -eL Follow symbolic links when processing " &
1951 Put_Line (" -f Force deletions of unwritable files");
1952 Put_Line (" -F Full project path name " &
1953 "in brief error messages");
1954 Put_Line (" -h Display this message");
1955 Put_Line (" -innn Index of unit in source for following names");
1956 Put_Line (" -n Nothing to do: only list files to delete");
1957 Put_Line (" -Pproj Use GNAT Project File proj");
1958 Put_Line (" -q Be quiet/terse");
1959 Put_Line (" -r Clean all projects recursively");
1960 Put_Line (" -v Verbose mode");
1961 Put_Line (" -vPx Specify verbosity when parsing " &
1962 "GNAT Project Files");
1963 Put_Line (" -Xnm=val Specify an external reference " &
1964 "for GNAT Project Files");
1967 Put_Line (" -aPdir Add directory dir to project search path");
1970 Put_Line (" -aOdir Specify ALI/object files search path");
1971 Put_Line (" -Idir Like -aOdir");
1972 Put_Line (" -I- Don't look for source/library files " &
1973 "in the default directory");