1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2003-2010, 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 ------------------------------------------------------------------------------
28 with Makeutl
; use Makeutl
;
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
.Tree
; use Prj
.Tree
;
39 with Prj
.Util
; use Prj
.Util
;
41 with Switch
; use Switch
;
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_Node_Tree : Project_Node_Tree_Ref;
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.
247 -- Display the usage. If called several times, the usage is displayed only
254 procedure Add_Object_Dir
(N
: String) is
256 Add_Lib_Search_Dir
(N
);
258 if Opt
.Verbose_Mode
then
259 Put
("Adding object directory """);
270 procedure Add_Source_Dir
(N
: String) is
272 Add_Src_Search_Dir
(N
);
274 if Opt
.Verbose_Mode
then
275 Put
("Adding source directory """);
286 function ALI_File_Name
(Source
: File_Name_Type
) return String is
287 Src
: constant String := Get_Name_String
(Source
);
290 -- If the source name has an extension, then replace it with
293 for Index
in reverse Src
'First + 1 .. Src
'Last loop
294 if Src
(Index
) = '.' then
295 return Src
(Src
'First .. Index
- 1) & ALI_Suffix
;
299 -- If there is no dot, or if it is the first character, just add the
302 return Src
& ALI_Suffix
;
305 ------------------------
306 -- Assembly_File_Name --
307 ------------------------
309 function Assembly_File_Name
(Source
: File_Name_Type
) return String is
310 Src
: constant String := Get_Name_String
(Source
);
313 -- If the source name has an extension, then replace it with
314 -- the assembly suffix.
316 for Index
in reverse Src
'First + 1 .. Src
'Last loop
317 if Src
(Index
) = '.' then
318 return Src
(Src
'First .. Index
- 1) & Assembly_Suffix
;
322 -- If there is no dot, or if it is the first character, just add the
325 return Src
& Assembly_Suffix
;
326 end Assembly_File_Name
;
332 procedure Clean_Archive
(Project
: Project_Id
; Global
: Boolean) is
333 Current_Dir
: constant Dir_Name_Str
:= Get_Current_Dir
;
335 Lib_Prefix
: String_Access
;
336 Archive_Name
: String_Access
;
337 -- The name of the archive file for this project
339 Archive_Dep_Name
: String_Access
;
340 -- The name of the archive dependency file for this project
342 Obj_Dir
: constant String :=
343 Get_Name_String
(Project
.Object_Directory
.Display_Name
);
346 Change_Dir
(Obj_Dir
);
348 -- First, get the lib prefix, the archive file name and the archive
349 -- dependency file name.
353 new String'("lib" & Get_Name_String (Project.Display_Name));
356 new String'("lib" & Get_Name_String
(Project
.Library_Name
));
359 Archive_Name
:= new String'(Lib_Prefix.all & '.' & Archive_Ext);
360 Archive_Dep_Name := new String'(Lib_Prefix
.all & ".deps");
362 -- Delete the archive file and the archive dependency file, if they
365 if Is_Regular_File
(Archive_Name
.all) then
366 Delete
(Obj_Dir
, Archive_Name
.all);
369 if Is_Regular_File
(Archive_Dep_Name
.all) then
370 Delete
(Obj_Dir
, Archive_Dep_Name
.all);
373 Change_Dir
(Current_Dir
);
376 -----------------------
377 -- Clean_Executables --
378 -----------------------
380 procedure Clean_Executables
is
381 Main_Source_File
: File_Name_Type
;
382 -- Current main source
384 Main_Lib_File
: File_Name_Type
;
385 -- ALI file of the current main
387 Lib_File
: File_Name_Type
;
390 Full_Lib_File
: File_Name_Type
;
391 -- Full name of the current ALI file
393 Text
: Text_Buffer_Ptr
;
399 -- It does not really matter if there is or not an object file
400 -- corresponding to an ALI file: if there is one, it will be deleted.
402 Opt
.Check_Object_Consistency
:= False;
404 -- Proceed each executable one by one. Each source is marked as it is
405 -- processed, so common sources between executables will not be
406 -- processed several times.
408 for N_File
in 1 .. Osint
.Number_Of_Files
loop
409 Main_Source_File
:= Next_Main_Source
;
410 Main_Lib_File
:= Osint
.Lib_File_Name
411 (Main_Source_File
, Current_File_Index
);
412 Insert_Q
(Main_Lib_File
);
414 while not Empty_Q
loop
415 Sources
.Set_Last
(0);
416 Extract_From_Q
(Lib_File
);
417 Full_Lib_File
:= Osint
.Full_Lib_File_Name
(Lib_File
);
419 -- If we have existing ALI file that is not read-only, process it
421 if Full_Lib_File
/= No_File
422 and then not Is_Readonly_Library
(Full_Lib_File
)
424 Text
:= Read_Library_Info
(Lib_File
);
428 Scan_ALI
(Lib_File
, Text
, Ignore_ED
=> False, Err
=> True);
431 -- If no error was produced while loading this ALI file,
432 -- insert into the queue all the unmarked withed sources.
434 if The_ALI
/= No_ALI_Id
then
435 for J
in ALIs
.Table
(The_ALI
).First_Unit
..
436 ALIs
.Table
(The_ALI
).Last_Unit
438 Sources
.Increment_Last
;
439 Sources
.Table
(Sources
.Last
) :=
440 ALI
.Units
.Table
(J
).Sfile
;
442 for K
in ALI
.Units
.Table
(J
).First_With
..
443 ALI
.Units
.Table
(J
).Last_With
445 Insert_Q
(Withs
.Table
(K
).Afile
);
449 -- Look for subunits and put them in the Sources table
451 for J
in ALIs
.Table
(The_ALI
).First_Sdep
..
452 ALIs
.Table
(The_ALI
).Last_Sdep
454 if Sdep
.Table
(J
).Subunit_Name
/= No_Name
then
455 Sources
.Increment_Last
;
456 Sources
.Table
(Sources
.Last
) :=
457 Sdep
.Table
(J
).Sfile
;
463 -- Now delete all existing files corresponding to this ALI file
466 Obj_Dir
: constant String :=
467 Dir_Name
(Get_Name_String
(Full_Lib_File
));
468 Obj
: constant String := Object_File_Name
(Lib_File
);
469 Adt
: constant String := Tree_File_Name
(Lib_File
);
470 Asm
: constant String := Assembly_File_Name
(Lib_File
);
473 Delete
(Obj_Dir
, Get_Name_String
(Lib_File
));
475 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Obj
) then
476 Delete
(Obj_Dir
, Obj
);
479 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Adt
) then
480 Delete
(Obj_Dir
, Adt
);
483 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Asm
) then
484 Delete
(Obj_Dir
, Asm
);
487 -- Delete expanded source files (.dg) and/or repinfo files
490 for J
in 1 .. Sources
.Last
loop
492 Deb
: constant String :=
493 Debug_File_Name
(Sources
.Table
(J
));
494 Rep
: constant String :=
495 Repinfo_File_Name
(Sources
.Table
(J
));
498 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Deb
) then
499 Delete
(Obj_Dir
, Deb
);
502 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Rep
) then
503 Delete
(Obj_Dir
, Rep
);
511 -- Delete the executable, if it exists, and the binder generated
514 if not Compile_Only
then
516 Source
: constant File_Name_Type
:=
517 Strip_Suffix
(Main_Lib_File
);
518 Executable
: constant String :=
519 Get_Name_String
(Executable_Name
(Source
));
521 if Is_Regular_File
(Executable
) then
522 Delete
("", Executable
);
525 Delete_Binder_Generated_Files
(Get_Current_Dir
, Source
);
529 end Clean_Executables
;
531 ------------------------------------
532 -- Clean_Interface_Copy_Directory --
533 ------------------------------------
535 procedure Clean_Interface_Copy_Directory
(Project
: Project_Id
) is
536 Current
: constant String := Get_Current_Dir
;
540 Name
: String (1 .. 200);
543 Delete_File
: Boolean;
548 and then Project
.Library_Src_Dir
/= No_Path_Information
551 Directory
: constant String :=
552 Get_Name_String
(Project
.Library_Src_Dir
.Display_Name
);
555 Change_Dir
(Directory
);
558 -- For each regular file in the directory, if switch -n has not
559 -- been specified, make it writable and delete the file if it is
560 -- a copy of a source of the project.
563 Read
(Direc
, Name
, Last
);
567 Filename
: constant String := Name
(1 .. Last
);
570 if Is_Regular_File
(Filename
) then
571 Canonical_Case_File_Name
(Name
(1 .. Last
));
572 Delete_File
:= False;
574 Unit
:= Units_Htable
.Get_First
(Project_Tree
.Units_HT
);
576 -- Compare with source file names of the project
578 while Unit
/= No_Unit_Index
loop
579 if Unit
.File_Names
(Impl
) /= null
580 and then Ultimate_Extending_Project_Of
581 (Unit
.File_Names
(Impl
).Project
) = Project
583 Get_Name_String
(Unit
.File_Names
(Impl
).File
) =
590 if Unit
.File_Names
(Spec
) /= null
591 and then Ultimate_Extending_Project_Of
592 (Unit
.File_Names
(Spec
).Project
) = Project
595 (Unit
.File_Names
(Spec
).File
) = Name
(1 .. Last
)
601 Unit
:= Units_Htable
.Get_Next
(Project_Tree
.Units_HT
);
605 if not Do_Nothing
then
606 Set_Writable
(Filename
);
609 Delete
(Directory
, Filename
);
617 -- Restore the initial working directory
619 Change_Dir
(Current
);
622 end Clean_Interface_Copy_Directory
;
624 -----------------------------
625 -- Clean_Library_Directory --
626 -----------------------------
628 Empty_String
: aliased String := "";
630 procedure Clean_Library_Directory
(Project
: Project_Id
) is
631 Current
: constant String := Get_Current_Dir
;
633 Lib_Filename
: constant String := Get_Name_String
(Project
.Library_Name
);
635 DLL_Prefix
& Lib_Filename
& "." & DLL_Ext
;
636 Archive_Name
: String :=
637 "lib" & Lib_Filename
& "." & Archive_Ext
;
640 Name
: String (1 .. 200);
643 Delete_File
: Boolean;
645 Minor
: String_Access
:= Empty_String
'Access;
646 Major
: String_Access
:= Empty_String
'Access;
649 if Project
.Library
then
650 if Project
.Library_Kind
/= Static
651 and then MLib
.Tgt
.Library_Major_Minor_Id_Supported
652 and then Project
.Lib_Internal_Name
/= No_Name
654 Minor
:= new String'(Get_Name_String (Project.Lib_Internal_Name));
655 Major := new String'(MLib
.Major_Id_Name
(DLL_Name
, Minor
.all));
659 Lib_Directory
: constant String :=
661 (Project
.Library_Dir
.Display_Name
);
662 Lib_ALI_Directory
: constant String :=
664 (Project
.Library_ALI_Dir
.Display_Name
);
667 Canonical_Case_File_Name
(Archive_Name
);
668 Canonical_Case_File_Name
(DLL_Name
);
670 Change_Dir
(Lib_Directory
);
673 -- For each regular file in the directory, if switch -n has not
674 -- been specified, make it writable and delete the file if it is
678 Read
(Direc
, Name
, Last
);
682 Filename
: constant String := Name
(1 .. Last
);
685 if Is_Regular_File
(Filename
)
686 or else Is_Symbolic_Link
(Filename
)
688 Canonical_Case_File_Name
(Name
(1 .. Last
));
689 Delete_File
:= False;
691 if (Project
.Library_Kind
= Static
692 and then Name
(1 .. Last
) = Archive_Name
)
694 ((Project
.Library_Kind
= Dynamic
696 Project
.Library_Kind
= Relocatable
)
698 (Name
(1 .. Last
) = DLL_Name
700 Name
(1 .. Last
) = Minor
.all
702 Name
(1 .. Last
) = Major
.all))
704 if not Do_Nothing
then
705 Set_Writable
(Filename
);
708 Delete
(Lib_Directory
, Filename
);
716 Change_Dir
(Lib_ALI_Directory
);
719 -- For each regular file in the directory, if switch -n has not
720 -- been specified, make it writable and delete the file if it is
721 -- any ALI file of a source of the project.
724 Read
(Direc
, Name
, Last
);
728 Filename
: constant String := Name
(1 .. Last
);
730 if Is_Regular_File
(Filename
) then
731 Canonical_Case_File_Name
(Name
(1 .. Last
));
732 Delete_File
:= False;
734 if Last
> 4 and then Name
(Last
- 3 .. Last
) = ".ali" then
738 -- Compare with ALI file names of the project
740 Unit
:= Units_Htable
.Get_First
741 (Project_Tree
.Units_HT
);
742 while Unit
/= No_Unit_Index
loop
743 if Unit
.File_Names
(Impl
) /= null
744 and then Unit
.File_Names
(Impl
).Project
/=
747 if Ultimate_Extending_Project_Of
748 (Unit
.File_Names
(Impl
).Project
) =
752 (Unit
.File_Names
(Impl
).File
);
753 Name_Len
:= Name_Len
-
755 (Name
(1 .. Name_Len
))'Length;
756 if Name_Buffer
(1 .. Name_Len
) =
764 elsif Unit
.File_Names
(Spec
) /= null
765 and then Ultimate_Extending_Project_Of
766 (Unit
.File_Names
(Spec
).Project
) =
770 (Unit
.File_Names
(Spec
).File
);
774 (Name
(1 .. Name_Len
))'Length;
776 if Name_Buffer
(1 .. Name_Len
) =
785 Units_Htable
.Get_Next
(Project_Tree
.Units_HT
);
791 if not Do_Nothing
then
792 Set_Writable
(Filename
);
795 Delete
(Lib_ALI_Directory
, Filename
);
803 -- Restore the initial working directory
805 Change_Dir
(Current
);
808 end Clean_Library_Directory
;
814 procedure Clean_Project
(Project
: Project_Id
) is
815 Main_Source_File
: File_Name_Type
;
816 -- Name of executable on the command line without directory info
818 Executable
: File_Name_Type
;
819 -- Name of the executable file
821 Current_Dir
: constant Dir_Name_Str
:= Get_Current_Dir
;
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 Project
.Library
840 ("Cannot specify executable(s) for a Library Project File");
843 -- Nothing to clean in an externally built project
845 if Project
.Externally_Built
then
847 Put
("Nothing to do to clean externally built project """);
848 Put
(Get_Name_String
(Project
.Name
));
854 Put
("Cleaning project """);
855 Put
(Get_Name_String
(Project
.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 Project
.Object_Directory
/= No_Path_Information
then
866 Obj_Dir
: constant String :=
868 (Project
.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 Has_Ada_Sources
(Project
)
882 or else Project
.Extends
/= No_Project
884 Unit
:= Units_Htable
.Get_First
(Project_Tree
.Units_HT
);
885 while Unit
/= No_Unit_Index
loop
886 File_Name1
:= No_File
;
887 File_Name2
:= No_File
;
889 -- If either the spec or the body is a source of the
890 -- project, check for the corresponding ALI file in the
893 if (Unit
.File_Names
(Impl
) /= null
896 (Unit
.File_Names
(Impl
).Project
, Project
))
898 (Unit
.File_Names
(Spec
) /= null
899 and then In_Extension_Chain
900 (Unit
.File_Names
(Spec
).Project
, Project
))
902 if Unit
.File_Names
(Impl
) /= null then
903 File_Name1
:= Unit
.File_Names
(Impl
).File
;
904 Index1
:= Unit
.File_Names
(Impl
).Index
;
906 File_Name1
:= No_File
;
910 if Unit
.File_Names
(Spec
) /= null then
911 File_Name2
:= Unit
.File_Names
(Spec
).File
;
912 Index2
:= Unit
.File_Names
(Spec
).Index
;
914 File_Name2
:= No_File
;
918 -- If there is no body file name, then there may be
921 if File_Name1
= No_File
then
922 File_Name1
:= File_Name2
;
924 File_Name2
:= No_File
;
929 -- If there is either a spec or a body, look for files
930 -- in the object directory.
932 if File_Name1
/= No_File
then
933 Lib_File
:= Osint
.Lib_File_Name
(File_Name1
, Index1
);
936 Asm
: constant String :=
937 Assembly_File_Name
(Lib_File
);
938 ALI
: constant String :=
939 ALI_File_Name
(Lib_File
);
940 Obj
: constant String :=
941 Object_File_Name
(Lib_File
);
942 Adt
: constant String :=
943 Tree_File_Name
(Lib_File
);
944 Deb
: constant String :=
945 Debug_File_Name
(File_Name1
);
946 Rep
: constant String :=
947 Repinfo_File_Name
(File_Name1
);
948 Del
: Boolean := True;
951 -- If the ALI file exists and is read-only, no file
954 if Is_Regular_File
(ALI
) then
955 if Is_Writable_File
(ALI
) then
956 Delete
(Obj_Dir
, ALI
);
965 if Obj_Dir
(Obj_Dir
'Last) /=
972 Put_Line
(""" is read-only");
981 if Is_Regular_File
(Obj
) then
982 Delete
(Obj_Dir
, Obj
);
987 if Is_Regular_File
(Asm
) then
988 Delete
(Obj_Dir
, Asm
);
993 if Is_Regular_File
(Adt
) then
994 Delete
(Obj_Dir
, Adt
);
997 -- First expanded source file
999 if Is_Regular_File
(Deb
) then
1000 Delete
(Obj_Dir
, Deb
);
1005 if Is_Regular_File
(Rep
) then
1006 Delete
(Obj_Dir
, Rep
);
1009 -- Second expanded source file
1011 if File_Name2
/= No_File
then
1013 Deb
: constant String :=
1014 Debug_File_Name
(File_Name2
);
1015 Rep
: constant String :=
1016 Repinfo_File_Name
(File_Name2
);
1019 if Is_Regular_File
(Deb
) then
1020 Delete
(Obj_Dir
, Deb
);
1023 if Is_Regular_File
(Rep
) then
1024 Delete
(Obj_Dir
, Rep
);
1032 Unit
:= Units_Htable
.Get_Next
(Project_Tree
.Units_HT
);
1036 -- Check if a global archive and it dependency file could have
1037 -- been created and, if they exist, delete them.
1039 if Project
= Main_Project
and then not Project
.Library
then
1040 Global_Archive
:= False;
1043 Proj
: Project_List
;
1046 Proj
:= Project_Tree
.Projects
;
1047 while Proj
/= null loop
1049 -- For gnatmake, when the project specifies more than
1050 -- just Ada as a language (even if course we could not
1051 -- find any source file for the other languages), we
1052 -- will take all the object files found in the object
1053 -- directories. Since we know the project supports at
1054 -- least Ada, we just have to test whether it has at
1055 -- least two languages, and we do not care about the
1058 if Proj
.Project
.Languages
/= null
1059 and then Proj
.Project
.Languages
.Next
/= null
1061 Global_Archive
:= True;
1069 if Global_Archive
then
1070 Clean_Archive
(Project
, Global
=> True);
1077 -- If this is a library project, clean the library directory, the
1078 -- interface copy dir and, for a Stand-Alone Library, the binder
1079 -- generated files of the library.
1081 -- The directories are cleaned only if switch -c is not specified
1083 if Project
.Library
then
1084 if not Compile_Only
then
1085 Clean_Library_Directory
(Project
);
1087 if Project
.Library_Src_Dir
/= No_Path_Information
then
1088 Clean_Interface_Copy_Directory
(Project
);
1092 if Project
.Standalone_Library
and then
1093 Project
.Object_Directory
/= No_Path_Information
1095 Delete_Binder_Generated_Files
1096 (Get_Name_String
(Project
.Object_Directory
.Display_Name
),
1097 File_Name_Type
(Project
.Library_Name
));
1101 if Verbose_Mode
then
1106 -- If switch -r is specified, call Clean_Project recursively for the
1107 -- imported projects and the project being extended.
1109 if All_Projects
then
1111 Imported
: Project_List
;
1115 -- For each imported project, call Clean_Project if the project
1116 -- has not been processed already.
1118 Imported
:= Project
.Imported_Projects
;
1119 while Imported
/= null loop
1123 J
in Processed_Projects
.First
.. Processed_Projects
.Last
1125 if Imported
.Project
= Processed_Projects
.Table
(J
) then
1132 Clean_Project
(Imported
.Project
);
1135 Imported
:= Imported
.Next
;
1138 -- If this project extends another project, call Clean_Project for
1139 -- the project being extended. It is guaranteed that it has not
1140 -- called before, because no other project may import or extend
1143 if Project
.Extends
/= No_Project
then
1144 Clean_Project
(Project
.Extends
);
1149 -- For the main project, delete the executables and the binder
1152 -- The executables are deleted only if switch -c is not specified
1154 if Project
= Main_Project
1155 and then Project
.Exec_Directory
/= No_Path_Information
1158 Exec_Dir
: constant String :=
1159 Get_Name_String
(Project
.Exec_Directory
.Display_Name
);
1162 Change_Dir
(Exec_Dir
);
1164 for N_File
in 1 .. Osint
.Number_Of_Files
loop
1165 Main_Source_File
:= Next_Main_Source
;
1167 if not Compile_Only
then
1173 Current_File_Index
);
1176 Exec_File_Name
: constant String :=
1177 Get_Name_String
(Executable
);
1180 if Is_Absolute_Path
(Name
=> Exec_File_Name
) then
1181 if Is_Regular_File
(Exec_File_Name
) then
1182 Delete
("", Exec_File_Name
);
1186 if Is_Regular_File
(Exec_File_Name
) then
1187 Delete
(Exec_Dir
, Exec_File_Name
);
1193 if Project
.Object_Directory
/= No_Path_Information
then
1194 Delete_Binder_Generated_Files
1195 (Get_Name_String
(Project
.Object_Directory
.Display_Name
),
1196 Strip_Suffix
(Main_Source_File
));
1202 -- Change back to previous directory
1204 Change_Dir
(Current_Dir
);
1207 ---------------------
1208 -- Debug_File_Name --
1209 ---------------------
1211 function Debug_File_Name
(Source
: File_Name_Type
) return String is
1213 return Get_Name_String
(Source
) & Debug_Suffix
;
1214 end Debug_File_Name
;
1220 procedure Delete
(In_Directory
: String; File
: String) is
1221 Full_Name
: String (1 .. In_Directory
'Length + File
'Length + 1);
1222 Last
: Natural := 0;
1226 -- Indicate that at least one file is deleted or is to be deleted
1228 File_Deleted
:= True;
1230 -- Build the path name of the file to delete
1232 Last
:= In_Directory
'Length;
1233 Full_Name
(1 .. Last
) := In_Directory
;
1235 if Last
> 0 and then Full_Name
(Last
) /= Directory_Separator
then
1237 Full_Name
(Last
) := Directory_Separator
;
1240 Full_Name
(Last
+ 1 .. Last
+ File
'Length) := File
;
1241 Last
:= Last
+ File
'Length;
1243 -- If switch -n was used, simply output the path name
1246 Put_Line
(Full_Name
(1 .. Last
));
1248 -- Otherwise, delete the file if it is writable
1252 or else Is_Writable_File
(Full_Name
(1 .. Last
))
1253 or else Is_Symbolic_Link
(Full_Name
(1 .. Last
))
1255 Delete_File
(Full_Name
(1 .. Last
), Success
);
1260 if Verbose_Mode
or else not Quiet_Output
then
1262 Put
("Warning: """);
1263 Put
(Full_Name
(1 .. Last
));
1264 Put_Line
(""" could not be deleted");
1268 Put
(Full_Name
(1 .. Last
));
1269 Put_Line
(""" has been deleted");
1275 -----------------------------------
1276 -- Delete_Binder_Generated_Files --
1277 -----------------------------------
1279 procedure Delete_Binder_Generated_Files
1281 Source
: File_Name_Type
)
1283 Source_Name
: constant String := Get_Name_String
(Source
);
1284 Current
: constant String := Get_Current_Dir
;
1285 Last
: constant Positive := B_Start
'Length + Source_Name
'Length;
1286 File_Name
: String (1 .. Last
+ 4);
1291 -- Build the file name (before the extension)
1293 File_Name
(1 .. B_Start
'Length) := B_Start
.all;
1294 File_Name
(B_Start
'Length + 1 .. Last
) := Source_Name
;
1298 File_Name
(Last
+ 1 .. Last
+ 4) := ".ads";
1300 if Is_Regular_File
(File_Name
(1 .. Last
+ 4)) then
1301 Delete
(Dir
, File_Name
(1 .. Last
+ 4));
1306 File_Name
(Last
+ 1 .. Last
+ 4) := ".adb";
1308 if Is_Regular_File
(File_Name
(1 .. Last
+ 4)) then
1309 Delete
(Dir
, File_Name
(1 .. Last
+ 4));
1314 File_Name
(Last
+ 1 .. Last
+ 4) := ".ali";
1316 if Is_Regular_File
(File_Name
(1 .. Last
+ 4)) then
1317 Delete
(Dir
, File_Name
(1 .. Last
+ 4));
1322 File_Name
(Last
+ 1 .. Last
+ Object_Suffix
'Length) := Object_Suffix
;
1324 if Is_Regular_File
(File_Name
(1 .. Last
+ Object_Suffix
'Length)) then
1325 Delete
(Dir
, File_Name
(1 .. Last
+ Object_Suffix
'Length));
1328 -- Change back to previous directory
1330 Change_Dir
(Current
);
1331 end Delete_Binder_Generated_Files
;
1333 -----------------------
1334 -- Display_Copyright --
1335 -----------------------
1337 procedure Display_Copyright
is
1339 if not Copyright_Displayed
then
1340 Copyright_Displayed
:= True;
1341 Display_Version
("GNATCLEAN", "2003");
1343 end Display_Copyright
;
1349 function Empty_Q
return Boolean is
1351 return Q_Front
>= Q
.Last
;
1354 --------------------
1355 -- Extract_From_Q --
1356 --------------------
1358 procedure Extract_From_Q
(Lib_File
: out File_Name_Type
) is
1359 Lib
: constant File_Name_Type
:= Q
.Table
(Q_Front
);
1361 Q_Front
:= Q_Front
+ 1;
1369 procedure Gnatclean
is
1371 -- Do the necessary initializations
1375 -- Parse the command line, getting the switches and the executable names
1379 if Verbose_Mode
then
1383 if Project_File_Name
/= null then
1385 -- A project file was specified by a -P switch
1387 if Opt
.Verbose_Mode
then
1389 Put
("Parsing Project File """);
1390 Put
(Project_File_Name
.all);
1395 -- Set the project parsing verbosity to whatever was specified
1396 -- by a possible -vP switch.
1398 Prj
.Pars
.Set_Verbosity
(To
=> Current_Verbosity
);
1400 -- Parse the project file. If there is an error, Main_Project
1401 -- will still be No_Project.
1404 (Project
=> Main_Project
,
1405 In_Tree
=> Project_Tree
,
1406 In_Node_Tree
=> Project_Node_Tree
,
1407 Project_File_Name
=> Project_File_Name
.all,
1408 Flags
=> Gnatmake_Flags
,
1409 Packages_To_Check
=> Packages_To_Check_By_Gnatmake
);
1411 if Main_Project
= No_Project
then
1412 Fail
("""" & Project_File_Name
.all & """ processing failed");
1415 if Opt
.Verbose_Mode
then
1417 Put
("Parsing of Project File """);
1418 Put
(Project_File_Name
.all);
1419 Put
(""" is finished.");
1423 -- Add source directories and object directories to the search paths
1425 Add_Source_Directories
(Main_Project
, Project_Tree
);
1426 Add_Object_Directories
(Main_Project
);
1429 Osint
.Add_Default_Search_Dirs
;
1431 -- If a project file was specified, but no executable name, put all
1432 -- the mains of the project file (if any) as if there were on the
1435 if Main_Project
/= No_Project
and then Osint
.Number_Of_Files
= 0 then
1437 Main
: String_Element
;
1438 Value
: String_List_Id
:= Main_Project
.Mains
;
1440 while Value
/= Prj
.Nil_String
loop
1441 Main
:= Project_Tree
.String_Elements
.Table
(Value
);
1443 (File_Name
=> Get_Name_String
(Main
.Value
),
1444 Index
=> Main
.Index
);
1450 -- If neither a project file nor an executable were specified, output
1451 -- the usage and exit.
1453 if Main_Project
= No_Project
and then Osint
.Number_Of_Files
= 0 then
1458 if Verbose_Mode
then
1462 if Main_Project
/= No_Project
then
1464 -- If a project file has been specified, call Clean_Project with the
1465 -- project id of this project file, after resetting the list of
1466 -- processed projects.
1468 Processed_Projects
.Init
;
1469 Clean_Project
(Main_Project
);
1472 -- If no project file has been specified, the work is done in
1473 -- Clean_Executables.
1478 -- In verbose mode, if Delete has not been called, indicate that no file
1479 -- needs to be deleted.
1481 if Verbose_Mode
and (not File_Deleted
) then
1485 Put_Line
("No file needs to be deleted");
1487 Put_Line
("No file has been deleted");
1492 ------------------------
1493 -- In_Extension_Chain --
1494 ------------------------
1496 function In_Extension_Chain
1497 (Of_Project
: Project_Id
;
1498 Prj
: Project_Id
) return Boolean
1503 if Prj
= No_Project
or else Of_Project
= No_Project
then
1507 if Of_Project
= Prj
then
1512 while Proj
.Extends
/= No_Project
loop
1513 if Proj
.Extends
= Prj
then
1517 Proj
:= Proj
.Extends
;
1521 while Proj
.Extends
/= No_Project
loop
1522 if Proj
.Extends
= Of_Project
then
1526 Proj
:= Proj
.Extends
;
1530 end In_Extension_Chain
;
1539 Q
.Set_Last
(Q
.First
);
1546 procedure Initialize
is
1548 if not Initialized
then
1549 Initialized
:= True;
1551 -- Get default search directories to locate system.ads when calling
1552 -- Targparm.Get_Target_Parameters.
1554 Osint
.Add_Default_Search_Dirs
;
1556 -- Initialize some packages
1561 Project_Node_Tree
:= new Project_Node_Tree_Data
;
1562 Prj
.Tree
.Initialize
(Project_Node_Tree
);
1564 Prj
.Initialize
(Project_Tree
);
1566 -- Check if the platform is VMS and, if it is, change some variables
1568 Targparm
.Get_Target_Parameters
;
1570 if OpenVMS_On_Target
then
1571 Debug_Suffix
(Debug_Suffix
'First) := '_';
1572 Repinfo_Suffix
(Repinfo_Suffix
'First) := '_';
1573 B_Start
:= new String'("b__");
1577 -- Reset global variables
1579 Free (Object_Directory_Path);
1580 Do_Nothing := False;
1581 File_Deleted := False;
1582 Copyright_Displayed := False;
1583 Usage_Displayed := False;
1584 Free (Project_File_Name);
1585 Main_Project := Prj.No_Project;
1586 All_Projects := False;
1593 procedure Insert_Q (Lib_File : File_Name_Type) is
1595 -- Do not insert an empty name or an already marked source
1597 if Lib_File /= No_File and then not Makeutl.Is_Marked (Lib_File) then
1598 Q.Table (Q.Last) := Lib_File;
1601 -- Mark the source that has been just added to the Q
1603 Makeutl.Mark (Lib_File);
1607 ----------------------
1608 -- Object_File_Name --
1609 ----------------------
1611 function Object_File_Name (Source : File_Name_Type) return String is
1612 Src : constant String := Get_Name_String (Source);
1615 -- If the source name has an extension, then replace it with
1616 -- the Object suffix.
1618 for Index in reverse Src'First + 1 .. Src'Last loop
1619 if Src (Index) = '.' then
1620 return Src (Src'First .. Index - 1) & Object_Suffix;
1624 -- If there is no dot, or if it is the first character, just add the
1627 return Src & Object_Suffix;
1628 end Object_File_Name;
1630 --------------------
1631 -- Parse_Cmd_Line --
1632 --------------------
1634 procedure Parse_Cmd_Line is
1635 Last : constant Natural := Argument_Count;
1636 Source_Index : Int := 0;
1639 procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
1642 -- First, check for --version and --help
1644 Check_Version_And_Help ("GNATCLEAN", "2003");
1647 while Index <= Last loop
1649 Arg : constant String := Argument (Index);
1651 procedure Bad_Argument;
1652 -- Signal bad argument
1658 procedure Bad_Argument is
1660 Fail ("invalid argument """ & Arg & """");
1664 if Arg'Length /= 0 then
1665 if Arg (1) = '-' then
1666 if Arg'Length = 1 then
1672 if Arg'Length > Subdirs_Option'Length and then
1673 Arg (1 .. Subdirs_Option'Length) = Subdirs_Option
1677 (Arg
(Subdirs_Option
'Length + 1 .. Arg
'Last));
1679 elsif Arg
= Makeutl
.Unchecked_Shared_Lib_Imports
then
1680 Opt
.Unchecked_Shared_Lib_Imports
:= True;
1687 if Arg
'Length < 4 then
1691 if Arg
(3) = 'O' then
1692 Add_Lib_Search_Dir
(Arg
(4 .. Arg
'Last));
1694 elsif Arg
(3) = 'P' then
1695 Prj
.Env
.Add_Directories
1696 (Project_Node_Tree
.Project_Path
,
1697 Arg
(4 .. Arg
'Last));
1704 Compile_Only
:= True;
1707 if Object_Directory_Path
/= null then
1708 Fail
("duplicate -D switch");
1710 elsif Project_File_Name
/= null then
1711 Fail
("-P and -D cannot be used simultaneously");
1714 if Arg
'Length > 2 then
1716 Dir
: constant String := Arg
(3 .. Arg
'Last);
1718 if not Is_Directory
(Dir
) then
1719 Fail
(Dir
& " is not a directory");
1721 Add_Lib_Search_Dir
(Dir
);
1726 if Index
= Last
then
1727 Fail
("no directory specified after -D");
1733 Dir
: constant String := Argument
(Index
);
1735 if not Is_Directory
(Dir
) then
1736 Fail
(Dir
& " is not a directory");
1738 Add_Lib_Search_Dir
(Dir
);
1745 Follow_Links_For_Files
:= True;
1746 Follow_Links_For_Dirs
:= True;
1753 Force_Deletions
:= True;
1756 Full_Path_Name_For_Brief_Errors
:= True;
1762 if Arg
'Length = 2 then
1768 for J
in 3 .. Arg
'Last loop
1769 if Arg
(J
) not in '0' .. '9' then
1774 (20 * Source_Index
) +
1775 (Character'Pos (Arg
(J
)) - Character'Pos ('0'));
1780 Opt
.Look_In_Primary_Dir
:= False;
1783 if Arg
'Length = 2 then
1787 Add_Lib_Search_Dir
(Arg
(3 .. Arg
'Last));
1794 if Project_File_Name
/= null then
1795 Fail
("multiple -P switches");
1797 elsif Object_Directory_Path
/= null then
1798 Fail
("-D and -P cannot be used simultaneously");
1802 if Arg
'Length > 2 then
1804 Prj
: constant String := Arg
(3 .. Arg
'Last);
1806 if Prj
'Length > 1 and then
1807 Prj
(Prj
'First) = '='
1809 Project_File_Name
:=
1811 (Prj (Prj'First + 1 .. Prj'Last));
1813 Project_File_Name := new String'(Prj
);
1818 if Index
= Last
then
1819 Fail
("no project specified after -P");
1823 Project_File_Name
:= new String'(Argument (Index));
1827 Quiet_Output := True;
1830 All_Projects := True;
1834 Verbose_Mode := True;
1836 elsif Arg = "-vP0" then
1837 Current_Verbosity := Prj.Default;
1839 elsif Arg = "-vP1" then
1840 Current_Verbosity := Prj.Medium;
1842 elsif Arg = "-vP2" then
1843 Current_Verbosity := Prj.High;
1850 if Arg'Length = 2 then
1855 Ext_Asgn : constant String := Arg (3 .. Arg'Last);
1856 Start : Positive := Ext_Asgn'First;
1857 Stop : Natural := Ext_Asgn'Last;
1858 Equal_Pos : Natural;
1859 OK : Boolean := True;
1862 if Ext_Asgn (Start) = '"' then
1863 if Ext_Asgn (Stop) = '"' then
1874 while Equal_Pos <= Stop
1875 and then Ext_Asgn (Equal_Pos) /= '='
1877 Equal_Pos := Equal_Pos + 1;
1880 if Equal_Pos = Start or else Equal_Pos > Stop then
1888 Ext_Asgn (Start .. Equal_Pos - 1),
1890 Ext_Asgn (Equal_Pos + 1 .. Stop));
1894 ("illegal external assignment '"
1905 Add_File (Arg, Source_Index);
1914 -----------------------
1915 -- Repinfo_File_Name --
1916 -----------------------
1918 function Repinfo_File_Name (Source : File_Name_Type) return String is
1920 return Get_Name_String (Source) & Repinfo_Suffix;
1921 end Repinfo_File_Name;
1923 --------------------
1924 -- Tree_File_Name --
1925 --------------------
1927 function Tree_File_Name (Source : File_Name_Type) return String is
1928 Src : constant String := Get_Name_String (Source);
1931 -- If source name has an extension, then replace it with the tree suffix
1933 for Index in reverse Src'First + 1 .. Src'Last loop
1934 if Src (Index) = '.' then
1935 return Src (Src'First .. Index - 1) & Tree_Suffix;
1939 -- If there is no dot, or if it is the first character, just add the
1942 return Src & Tree_Suffix;
1951 if not Usage_Displayed then
1952 Usage_Displayed := True;
1954 Put_Line ("Usage: gnatclean [switches] {[-innn] name}");
1957 Put_Line (" names is one or more file names from which " &
1958 "the .adb or .ads suffix may be omitted");
1959 Put_Line (" names may be omitted if -P<project> is specified");
1962 Put_Line (" --subdirs=dir real obj/lib/exec dirs are subdirs");
1963 Put_Line (" " & Makeutl.Unchecked_Shared_Lib_Imports);
1964 Put_Line (" Allow shared libraries to import static libraries");
1967 Put_Line (" -c Only delete compiler generated files");
1968 Put_Line (" -D dir Specify dir as the object library");
1969 Put_Line (" -eL Follow symbolic links when processing " &
1971 Put_Line (" -f Force deletions of unwritable files");
1972 Put_Line (" -F Full project path name " &
1973 "in brief error messages");
1974 Put_Line (" -h Display this message");
1975 Put_Line (" -innn Index of unit in source for following names");
1976 Put_Line (" -n Nothing to do: only list files to delete");
1977 Put_Line (" -Pproj Use GNAT Project File proj");
1978 Put_Line (" -q Be quiet/terse");
1979 Put_Line (" -r Clean all projects recursively");
1980 Put_Line (" -v Verbose mode");
1981 Put_Line (" -vPx Specify verbosity when parsing " &
1982 "GNAT Project Files");
1983 Put_Line (" -Xnm=val Specify an external reference " &
1984 "for GNAT Project Files");
1987 Put_Line (" -aPdir Add directory dir to project search path");
1990 Put_Line (" -aOdir Specify ALI/object files search path");
1991 Put_Line (" -Idir Like -aOdir");
1992 Put_Line (" -I- Don't look for source/library files " &
1993 "in the default directory");