1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2003-2006, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
29 with Gnatvsn
; use Gnatvsn
;
31 with MLib
.Tgt
; use MLib
.Tgt
;
32 with Namet
; use Namet
;
34 with Osint
; use Osint
;
35 with Osint
.M
; use Osint
.M
;
40 with Prj
.Util
; use Prj
.Util
;
43 with Targparm
; use Targparm
;
44 with Types
; use Types
;
46 with Ada
.Command_Line
; use Ada
.Command_Line
;
48 with GNAT
.Directory_Operations
; use GNAT
.Directory_Operations
;
49 with GNAT
.IO
; use GNAT
.IO
;
50 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
54 Initialized
: Boolean := False;
55 -- Set to True by the first call to Initialize.
56 -- To avoid reinitialization of some packages.
58 -- Suffixes of various files
60 Assembly_Suffix
: constant String := ".s";
61 ALI_Suffix
: constant String := ".ali";
62 Tree_Suffix
: constant String := ".adt";
63 Object_Suffix
: constant String := Get_Target_Object_Suffix
.all;
64 Debug_Suffix
: String := ".dg";
65 -- Changed to "_dg" for VMS in the body of the package
67 Repinfo_Suffix
: String := ".rep";
68 -- Changed to "_rep" for VMS in the body of the package
70 B_Start
: String_Ptr
:= new String'("b~");
71 -- Prefix of binder generated file, and number of actual characters used.
72 -- Changed to "b__" for VMS in the body of the package.
74 Object_Directory_Path : String_Access := null;
75 -- The path name of the object directory, set with switch -D
77 Force_Deletions : Boolean := False;
78 -- Set to True by switch -f. When True, attempts to delete non writable
79 -- files will be done.
81 Do_Nothing : Boolean := False;
82 -- Set to True when switch -n is specified. When True, no file is deleted.
83 -- gnatclean only lists the files that would have been deleted if the
84 -- switch -n had not been specified.
86 File_Deleted : Boolean := False;
87 -- Set to True if at least one file has been deleted
89 Copyright_Displayed : Boolean := False;
90 Usage_Displayed : Boolean := False;
92 Project_File_Name : String_Access := null;
94 Project_Tree : constant Prj.Project_Tree_Ref := new Prj.Project_Tree_Data;
96 Main_Project : Prj.Project_Id := Prj.No_Project;
98 All_Projects : Boolean := False;
100 -- Packages of project files where unknown attributes are errors
102 Naming_String : aliased String := "naming";
103 Builder_String : aliased String := "builder";
104 Compiler_String : aliased String := "compiler";
105 Binder_String : aliased String := "binder";
106 Linker_String : aliased String := "linker";
108 Gnatmake_Packages : aliased String_List :=
109 (Naming_String 'Access,
110 Builder_String
'Access,
111 Compiler_String 'Access,
112 Binder_String
'Access,
113 Linker_String 'Access);
115 Packages_To_Check_By_Gnatmake
: constant String_List_Access
:=
116 Gnatmake_Packages
'Access;
118 package Processed_Projects
is new Table
.Table
119 (Table_Component_Type
=> Project_Id
,
120 Table_Index_Type
=> Natural,
121 Table_Low_Bound
=> 0,
123 Table_Increment
=> 10,
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
=> 10,
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.
172 -- Output one line when in verbose mode.
174 procedure Add_Source_Directories
is
175 new Prj
.Env
.For_All_Source_Dirs
(Action
=> Add_Source_Dir
);
177 procedure Add_Object_Dir
(N
: String);
178 -- Call Add_Lib_Search_Dir.
179 -- Output one line when in verbose mode.
181 procedure Add_Object_Directories
is
182 new Prj
.Env
.For_All_Object_Dirs
(Action
=> Add_Object_Dir
);
184 function ALI_File_Name
(Source
: Name_Id
) return String;
185 -- Returns the name of the ALI file corresponding to Source
187 function Assembly_File_Name
(Source
: Name_Id
) return String;
188 -- Returns the assembly file name corresponding to Source
190 procedure Clean_Archive
(Project
: Project_Id
);
191 -- Delete a global archive or a fake library project archive and the
192 -- dependency file, if they exist.
194 procedure Clean_Executables
;
195 -- Do the cleaning work when no project file is specified
197 procedure Clean_Interface_Copy_Directory
(Project
: Project_Id
);
198 -- Delete files in an interface coy directory directory: any file that is
199 -- a copy of a source of the project.
201 procedure Clean_Library_Directory
(Project
: Project_Id
);
202 -- Delete the library file in a library directory and any ALI file
203 -- of a source of the project in a library ALI directory.
205 procedure Clean_Project
(Project
: Project_Id
);
206 -- Do the cleaning work when a project file is specified.
207 -- This procedure calls itself recursively when there are several
208 -- project files in the tree rooted at the main project file and switch -r
209 -- has been specified.
211 function Debug_File_Name
(Source
: Name_Id
) return String;
212 -- Name of the expanded source file corresponding to Source
214 procedure Delete
(In_Directory
: String; File
: String);
215 -- Delete one file, or list the file name if switch -n is specified
217 procedure Delete_Binder_Generated_Files
(Dir
: String; Source
: Name_Id
);
218 -- Delete the binder generated file in directory Dir for Source, if they
219 -- exist: for Unix these are b~<source>.ads, b~<source>.adb,
220 -- b~<source>.ali and b~<source>.o.
222 procedure Display_Copyright
;
223 -- Display the Copyright notice.
224 -- If called several times, display the Copyright notice only the first
227 procedure Initialize
;
228 -- Call the necessary package initializations
230 function Object_File_Name
(Source
: Name_Id
) return String;
231 -- Returns the object file name corresponding to Source
233 procedure Parse_Cmd_Line
;
234 -- Parse the command line
236 function Repinfo_File_Name
(Source
: Name_Id
) return String;
237 -- Returns the repinfo file name corresponding to Source
239 function Tree_File_Name
(Source
: Name_Id
) return String;
240 -- Returns the tree file name corresponding to Source
242 function In_Extension_Chain
243 (Of_Project
: Project_Id
;
244 Prj
: Project_Id
) return Boolean;
245 -- Returns True iff Prj is an extension of Of_Project or if Of_Project is
246 -- an extension of Prj.
248 function Ultimate_Extension_Of
(Project
: Project_Id
) return Project_Id
;
249 -- Returns either Project, if it is not extended by another project, or
250 -- the project that extends Project, directly or indirectly, and that is
251 -- not itself extended. Returns No_Project if Project is No_Project.
254 -- Display the usage.
255 -- If called several times, the usage is displayed only the first time.
261 procedure Add_Object_Dir
(N
: String) is
263 Add_Lib_Search_Dir
(N
);
265 if Opt
.Verbose_Mode
then
266 Put
("Adding object directory """);
277 procedure Add_Source_Dir
(N
: String) is
279 Add_Src_Search_Dir
(N
);
281 if Opt
.Verbose_Mode
then
282 Put
("Adding source directory """);
293 function ALI_File_Name
(Source
: Name_Id
) return String is
294 Src
: constant String := Get_Name_String
(Source
);
297 -- If the source name has an extension, then replace it with
300 for Index
in reverse Src
'First + 1 .. Src
'Last loop
301 if Src
(Index
) = '.' then
302 return Src
(Src
'First .. Index
- 1) & ALI_Suffix
;
306 -- If there is no dot, or if it is the first character, just add the
309 return Src
& ALI_Suffix
;
312 ------------------------
313 -- Assembly_File_Name --
314 ------------------------
316 function Assembly_File_Name
(Source
: Name_Id
) return String is
317 Src
: constant String := Get_Name_String
(Source
);
320 -- If the source name has an extension, then replace it with
321 -- the assembly suffix.
323 for Index
in reverse Src
'First + 1 .. Src
'Last loop
324 if Src
(Index
) = '.' then
325 return Src
(Src
'First .. Index
- 1) & Assembly_Suffix
;
329 -- If there is no dot, or if it is the first character, just add the
332 return Src
& Assembly_Suffix
;
333 end Assembly_File_Name
;
339 procedure Clean_Archive
(Project
: Project_Id
) is
340 Current_Dir
: constant Dir_Name_Str
:= Get_Current_Dir
;
341 Data
: constant Project_Data
:=
342 Project_Tree
.Projects
.Table
(Project
);
344 Archive_Name
: constant String :=
345 "lib" & Get_Name_String
(Data
.Name
) & '.' & Archive_Ext
;
346 -- The name of the archive file for this project
348 Archive_Dep_Name
: constant String :=
349 "lib" & Get_Name_String
(Data
.Name
) & ".deps";
350 -- The name of the archive dependency file for this project
352 Obj_Dir
: constant String := Get_Name_String
(Data
.Object_Directory
);
355 Change_Dir
(Obj_Dir
);
357 if Is_Regular_File
(Archive_Name
) then
358 Delete
(Obj_Dir
, Archive_Name
);
361 if Is_Regular_File
(Archive_Dep_Name
) then
362 Delete
(Obj_Dir
, Archive_Dep_Name
);
365 Change_Dir
(Current_Dir
);
368 -----------------------
369 -- Clean_Executables --
370 -----------------------
372 procedure Clean_Executables
is
373 Main_Source_File
: File_Name_Type
;
374 -- Current main source
376 Main_Lib_File
: File_Name_Type
;
377 -- ALI file of the current main
379 Lib_File
: File_Name_Type
;
382 Full_Lib_File
: File_Name_Type
;
383 -- Full name of the current ALI file
385 Text
: Text_Buffer_Ptr
;
391 -- It does not really matter if there is or not an object file
392 -- corresponding to an ALI file: if there is one, it will be deleted.
394 Opt
.Check_Object_Consistency
:= False;
396 -- Proceed each executable one by one. Each source is marked as it is
397 -- processed, so common sources between executables will not be
398 -- processed several times.
400 for N_File
in 1 .. Osint
.Number_Of_Files
loop
401 Main_Source_File
:= Next_Main_Source
;
402 Main_Lib_File
:= Osint
.Lib_File_Name
403 (Main_Source_File
, Current_File_Index
);
404 Insert_Q
(Main_Lib_File
);
406 while not Empty_Q
loop
407 Sources
.Set_Last
(0);
408 Extract_From_Q
(Lib_File
);
409 Full_Lib_File
:= Osint
.Full_Lib_File_Name
(Lib_File
);
411 -- If we have existing ALI file that is not read-only, process it
413 if Full_Lib_File
/= No_File
414 and then not Is_Readonly_Library
(Full_Lib_File
)
416 Text
:= Read_Library_Info
(Lib_File
);
420 Scan_ALI
(Lib_File
, Text
, Ignore_ED
=> False, Err
=> True);
423 -- If no error was produced while loading this ALI file,
424 -- insert into the queue all the unmarked withed sources.
426 if The_ALI
/= No_ALI_Id
then
427 for J
in ALIs
.Table
(The_ALI
).First_Unit
..
428 ALIs
.Table
(The_ALI
).Last_Unit
430 Sources
.Increment_Last
;
431 Sources
.Table
(Sources
.Last
) :=
432 ALI
.Units
.Table
(J
).Sfile
;
434 for K
in ALI
.Units
.Table
(J
).First_With
..
435 ALI
.Units
.Table
(J
).Last_With
437 Insert_Q
(Withs
.Table
(K
).Afile
);
441 -- Look for subunits and put them in the Sources table
443 for J
in ALIs
.Table
(The_ALI
).First_Sdep
..
444 ALIs
.Table
(The_ALI
).Last_Sdep
446 if Sdep
.Table
(J
).Subunit_Name
/= No_Name
then
447 Sources
.Increment_Last
;
448 Sources
.Table
(Sources
.Last
) :=
449 Sdep
.Table
(J
).Sfile
;
455 -- Now delete all existing files corresponding to this ALI file
458 Obj_Dir
: constant String :=
459 Dir_Name
(Get_Name_String
(Full_Lib_File
));
460 Obj
: constant String := Object_File_Name
(Lib_File
);
461 Adt
: constant String := Tree_File_Name
(Lib_File
);
462 Asm
: constant String := Assembly_File_Name
(Lib_File
);
465 Delete
(Obj_Dir
, Get_Name_String
(Lib_File
));
467 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Obj
) then
468 Delete
(Obj_Dir
, Obj
);
471 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Adt
) then
472 Delete
(Obj_Dir
, Adt
);
475 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Asm
) then
476 Delete
(Obj_Dir
, Asm
);
479 -- Delete expanded source files (.dg) and/or repinfo files
482 for J
in 1 .. Sources
.Last
loop
484 Deb
: constant String :=
485 Debug_File_Name
(Sources
.Table
(J
));
486 Rep
: constant String :=
487 Repinfo_File_Name
(Sources
.Table
(J
));
490 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Deb
) then
491 Delete
(Obj_Dir
, Deb
);
494 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Rep
) then
495 Delete
(Obj_Dir
, Rep
);
503 -- Delete the executable, if it exists, and the binder generated
506 if not Compile_Only
then
508 Source
: constant Name_Id
:= Strip_Suffix
(Main_Lib_File
);
509 Executable
: constant String := Get_Name_String
510 (Executable_Name
(Source
));
512 if Is_Regular_File
(Executable
) then
513 Delete
("", Executable
);
516 Delete_Binder_Generated_Files
(Get_Current_Dir
, Source
);
520 end Clean_Executables
;
522 ------------------------------------
523 -- Clean_Interface_Copy_Directory --
524 ------------------------------------
526 procedure Clean_Interface_Copy_Directory
(Project
: Project_Id
) is
527 Current
: constant String := Get_Current_Dir
;
528 Data
: constant Project_Data
:= Project_Tree
.Projects
.Table
(Project
);
532 Name
: String (1 .. 200);
535 Delete_File
: Boolean;
539 if Data
.Library
and then Data
.Library_Src_Dir
/= No_Name
then
541 Directory
: constant String :=
542 Get_Name_String
(Data
.Library_Src_Dir
);
545 Change_Dir
(Get_Name_String
(Data
.Library_Src_Dir
));
548 -- For each regular file in the directory, if switch -n has not
549 -- been specified, make it writable and delete the file if it is
550 -- a copy of a source of the project.
553 Read
(Direc
, Name
, Last
);
556 if Is_Regular_File
(Name
(1 .. Last
)) then
557 Canonical_Case_File_Name
(Name
(1 .. Last
));
558 Delete_File
:= False;
560 -- Compare with source file names of the project
562 for Index
in 1 .. Unit_Table
.Last
(Project_Tree
.Units
) loop
563 Unit
:= Project_Tree
.Units
.Table
(Index
);
565 if Ultimate_Extension_Of
566 (Unit
.File_Names
(Body_Part
).Project
) = Project
569 (Unit
.File_Names
(Body_Part
).Name
) =
576 if Ultimate_Extension_Of
577 (Unit
.File_Names
(Specification
).Project
) = Project
580 (Unit
.File_Names
(Specification
).Name
) =
589 if not Do_Nothing
then
590 Set_Writable
(Name
(1 .. Last
));
593 Delete
(Directory
, Name
(1 .. Last
));
600 -- Restore the initial working directory
602 Change_Dir
(Current
);
605 end Clean_Interface_Copy_Directory
;
607 -----------------------------
608 -- Clean_Library_Directory --
609 -----------------------------
611 procedure Clean_Library_Directory
(Project
: Project_Id
) is
612 Current
: constant String := Get_Current_Dir
;
613 Data
: constant Project_Data
:= Project_Tree
.Projects
.Table
(Project
);
615 Lib_Filename
: constant String := Get_Name_String
(Data
.Library_Name
);
616 DLL_Name
: constant String :=
617 DLL_Prefix
& Lib_Filename
& "." & DLL_Ext
;
618 Archive_Name
: constant String :=
619 "lib" & Lib_Filename
& "." & Archive_Ext
;
622 Name
: String (1 .. 200);
625 Delete_File
: Boolean;
630 Lib_Directory
: constant String :=
631 Get_Name_String
(Data
.Library_Dir
);
632 Lib_ALI_Directory
: constant String :=
633 Get_Name_String
(Data
.Library_ALI_Dir
);
636 Change_Dir
(Lib_Directory
);
639 -- For each regular file in the directory, if switch -n has not
640 -- been specified, make it writable and delete the file if it is
644 Read
(Direc
, Name
, Last
);
647 if Is_Regular_File
(Name
(1 .. Last
)) then
648 Canonical_Case_File_Name
(Name
(1 .. Last
));
649 Delete_File
:= False;
651 if (Data
.Library_Kind
= Static
and then
652 Name
(1 .. Last
) = Archive_Name
)
654 ((Data
.Library_Kind
= Dynamic
or else
655 Data
.Library_Kind
= Relocatable
)
657 Name
(1 .. Last
) = DLL_Name
)
659 if not Do_Nothing
then
660 Set_Writable
(Name
(1 .. Last
));
663 Delete
(Lib_Directory
, Name
(1 .. Last
));
671 Change_Dir
(Lib_ALI_Directory
);
674 -- For each regular file in the directory, if switch -n has not
675 -- been specified, make it writable and delete the file if it is
676 -- any ALI file of a source of the project.
679 Read
(Direc
, Name
, Last
);
682 if Is_Regular_File
(Name
(1 .. Last
)) then
683 Canonical_Case_File_Name
(Name
(1 .. Last
));
684 Delete_File
:= False;
686 if Last
> 4 and then Name
(Last
- 3 .. Last
) = ".ali" then
690 -- Compare with ALI file names of the project
693 Index
in 1 .. Unit_Table
.Last
(Project_Tree
.Units
)
695 Unit
:= Project_Tree
.Units
.Table
(Index
);
697 if Unit
.File_Names
(Body_Part
).Project
/=
700 if Ultimate_Extension_Of
701 (Unit
.File_Names
(Body_Part
).Project
) =
705 (Unit
.File_Names
(Body_Part
).Name
);
706 Name_Len
:= Name_Len
-
708 (Name
(1 .. Name_Len
))'Length;
709 if Name_Buffer
(1 .. Name_Len
) =
717 elsif Ultimate_Extension_Of
718 (Unit
.File_Names
(Specification
).Project
) =
722 (Unit
.File_Names
(Specification
).Name
);
723 Name_Len
:= Name_Len
-
725 (Name
(1 .. Name_Len
))'Length;
727 if Name_Buffer
(1 .. Name_Len
) =
739 if not Do_Nothing
then
740 Set_Writable
(Name
(1 .. Last
));
743 Delete
(Lib_ALI_Directory
, Name
(1 .. Last
));
751 -- Restore the initial working directory
753 Change_Dir
(Current
);
756 end Clean_Library_Directory
;
762 procedure Clean_Project
(Project
: Project_Id
) is
763 Main_Source_File
: File_Name_Type
;
764 -- Name of executable on the command line without directory info
766 Executable
: Name_Id
;
767 -- Name of the executable file
769 Current_Dir
: constant Dir_Name_Str
:= Get_Current_Dir
;
770 Data
: constant Project_Data
:=
771 Project_Tree
.Projects
.Table
(Project
);
773 File_Name1
: Name_Id
;
775 File_Name2
: Name_Id
;
777 Lib_File
: File_Name_Type
;
779 Source_Id
: Other_Source_Id
;
780 Source
: Other_Source
;
782 Global_Archive
: Boolean := False;
785 -- Check that we don't specify executable on the command line for
786 -- a main library project.
788 if Project
= Main_Project
789 and then Osint
.Number_Of_Files
/= 0
790 and then Data
.Library
793 ("Cannot specify executable(s) for a Library Project File");
796 -- Nothing to clean in an externally built project
798 if Data
.Externally_Built
then
800 Put
("Nothing to do to clean externally built project """);
801 Put
(Get_Name_String
(Data
.Name
));
807 Put
("Cleaning project """);
808 Put
(Get_Name_String
(Data
.Name
));
812 -- Add project to the list of processed projects
814 Processed_Projects
.Increment_Last
;
815 Processed_Projects
.Table
(Processed_Projects
.Last
) := Project
;
817 if Data
.Object_Directory
/= No_Name
then
819 Obj_Dir
: constant String :=
820 Get_Name_String
(Data
.Object_Directory
);
823 Change_Dir
(Obj_Dir
);
825 -- First, deal with Ada
827 -- Look through the units to find those that are either
828 -- immediate sources or inherited sources of the project.
829 -- Extending projects may have no language specified, if
830 -- Source_Dirs or Source_Files is specified as an empty list,
831 -- so always look for Ada units in extending projects.
833 if Data
.Languages
(Ada_Language_Index
)
834 or else Data
.Extends
/= No_Project
836 for Unit
in Unit_Table
.First
..
837 Unit_Table
.Last
(Project_Tree
.Units
)
839 U_Data
:= Project_Tree
.Units
.Table
(Unit
);
840 File_Name1
:= No_Name
;
841 File_Name2
:= No_Name
;
843 -- If either the spec or the body is a source of the
844 -- project, check for the corresponding ALI file in the
847 if In_Extension_Chain
848 (U_Data
.File_Names
(Body_Part
).Project
, Project
)
851 (U_Data
.File_Names
(Specification
).Project
, Project
)
853 File_Name1
:= U_Data
.File_Names
(Body_Part
).Name
;
854 Index1
:= U_Data
.File_Names
(Body_Part
).Index
;
855 File_Name2
:= U_Data
.File_Names
(Specification
).Name
;
856 Index2
:= U_Data
.File_Names
(Specification
).Index
;
858 -- If there is no body file name, then there may be
861 if File_Name1
= No_Name
then
862 File_Name1
:= File_Name2
;
864 File_Name2
:= No_Name
;
869 -- If there is either a spec or a body, look for files
870 -- in the object directory.
872 if File_Name1
/= No_Name
then
873 Lib_File
:= Osint
.Lib_File_Name
(File_Name1
, Index1
);
876 Asm
: constant String :=
877 Assembly_File_Name
(Lib_File
);
878 ALI
: constant String :=
879 ALI_File_Name
(Lib_File
);
880 Obj
: constant String :=
881 Object_File_Name
(Lib_File
);
882 Adt
: constant String :=
883 Tree_File_Name
(Lib_File
);
884 Deb
: constant String :=
885 Debug_File_Name
(File_Name1
);
886 Rep
: constant String :=
887 Repinfo_File_Name
(File_Name1
);
888 Del
: Boolean := True;
891 -- If the ALI file exists and is read-only, no file
894 if Is_Regular_File
(ALI
) then
895 if Is_Writable_File
(ALI
) then
896 Delete
(Obj_Dir
, ALI
);
905 if Obj_Dir
(Obj_Dir
'Last) /=
912 Put_Line
(""" is read-only");
921 if Is_Regular_File
(Obj
) then
922 Delete
(Obj_Dir
, Obj
);
927 if Is_Regular_File
(Asm
) then
928 Delete
(Obj_Dir
, Asm
);
933 if Is_Regular_File
(Adt
) then
934 Delete
(Obj_Dir
, Adt
);
937 -- First expanded source file
939 if Is_Regular_File
(Deb
) then
940 Delete
(Obj_Dir
, Deb
);
945 if Is_Regular_File
(Rep
) then
946 Delete
(Obj_Dir
, Rep
);
949 -- Second expanded source file
951 if File_Name2
/= No_Name
then
953 Deb
: constant String :=
954 Debug_File_Name
(File_Name2
);
955 Rep
: constant String :=
956 Repinfo_File_Name
(File_Name2
);
959 if Is_Regular_File
(Deb
) then
960 Delete
(Obj_Dir
, Deb
);
963 if Is_Regular_File
(Rep
) then
964 Delete
(Obj_Dir
, Rep
);
974 -- Check if a global archive and it dependency file could have
975 -- been created and, if they exist, delete them.
977 if Project
= Main_Project
and then not Data
.Library
then
978 Global_Archive
:= False;
980 for Proj
in Project_Table
.First
..
981 Project_Table
.Last
(Project_Tree
.Projects
)
983 if Project_Tree
.Projects
.Table
984 (Proj
).Other_Sources_Present
986 Global_Archive
:= True;
991 if Global_Archive
then
992 Clean_Archive
(Project
);
996 if Data
.Other_Sources_Present
then
998 -- There is non-Ada code: delete the object files and
999 -- the dependency files if they exist.
1001 Source_Id
:= Data
.First_Other_Source
;
1002 while Source_Id
/= No_Other_Source
loop
1004 Project_Tree
.Other_Sources
.Table
(Source_Id
);
1007 (Get_Name_String
(Source
.Object_Name
))
1009 Delete
(Obj_Dir
, Get_Name_String
(Source
.Object_Name
));
1013 Is_Regular_File
(Get_Name_String
(Source
.Dep_Name
))
1015 Delete
(Obj_Dir
, Get_Name_String
(Source
.Dep_Name
));
1018 Source_Id
:= Source
.Next
;
1021 -- If it is a library with only non Ada sources, delete
1022 -- the fake archive and the dependency file, if they exist.
1025 and then not Data
.Languages
(Ada_Language_Index
)
1027 Clean_Archive
(Project
);
1033 -- If this is a library project, clean the library directory, the
1034 -- interface copy dir and, for a Stand-Alone Library, the binder
1035 -- generated files of the library.
1037 -- The directories are cleaned only if switch -c is not specified
1039 if Data
.Library
then
1040 if not Compile_Only
then
1041 Clean_Library_Directory
(Project
);
1043 if Data
.Library_Src_Dir
/= No_Name
then
1044 Clean_Interface_Copy_Directory
(Project
);
1048 if Data
.Standalone_Library
and then
1049 Data
.Object_Directory
/= No_Name
1051 Delete_Binder_Generated_Files
1052 (Get_Name_String
(Data
.Object_Directory
), Data
.Library_Name
);
1056 if Verbose_Mode
then
1061 -- If switch -r is specified, call Clean_Project recursively for the
1062 -- imported projects and the project being extended.
1064 if All_Projects
then
1066 Imported
: Project_List
:= Data
.Imported_Projects
;
1067 Element
: Project_Element
;
1071 -- For each imported project, call Clean_Project if the project
1072 -- has not been processed already.
1074 while Imported
/= Empty_Project_List
loop
1075 Element
:= Project_Tree
.Project_Lists
.Table
(Imported
);
1076 Imported
:= Element
.Next
;
1080 J
in Processed_Projects
.First
.. Processed_Projects
.Last
1082 if Element
.Project
= Processed_Projects
.Table
(J
) then
1089 Clean_Project
(Element
.Project
);
1093 -- If this project extends another project, call Clean_Project for
1094 -- the project being extended. It is guaranteed that it has not
1095 -- called before, because no other project may import or extend
1098 if Data
.Extends
/= No_Project
then
1099 Clean_Project
(Data
.Extends
);
1104 -- For the main project, delete the executables and the binder
1107 -- The executables are deleted only if switch -c is not specified
1109 if Project
= Main_Project
and then Data
.Exec_Directory
/= No_Name
then
1111 Exec_Dir
: constant String :=
1112 Get_Name_String
(Data
.Exec_Directory
);
1115 Change_Dir
(Exec_Dir
);
1117 for N_File
in 1 .. Osint
.Number_Of_Files
loop
1118 Main_Source_File
:= Next_Main_Source
;
1120 if not Compile_Only
then
1126 Current_File_Index
);
1129 Exec_File_Name
: constant String :=
1130 Get_Name_String
(Executable
);
1133 if Is_Absolute_Path
(Name
=> Exec_File_Name
) then
1134 if Is_Regular_File
(Exec_File_Name
) then
1135 Delete
("", Exec_File_Name
);
1139 if Is_Regular_File
(Exec_File_Name
) then
1140 Delete
(Exec_Dir
, Exec_File_Name
);
1146 if Data
.Object_Directory
/= No_Name
then
1147 Delete_Binder_Generated_Files
1149 (Data
.Object_Directory
),
1150 Strip_Suffix
(Main_Source_File
));
1156 -- Change back to previous directory
1158 Change_Dir
(Current_Dir
);
1161 ---------------------
1162 -- Debug_File_Name --
1163 ---------------------
1165 function Debug_File_Name
(Source
: Name_Id
) return String is
1167 return Get_Name_String
(Source
) & Debug_Suffix
;
1168 end Debug_File_Name
;
1174 procedure Delete
(In_Directory
: String; File
: String) is
1175 Full_Name
: String (1 .. In_Directory
'Length + File
'Length + 1);
1176 Last
: Natural := 0;
1180 -- Indicate that at least one file is deleted or is to be deleted
1182 File_Deleted
:= True;
1184 -- Build the path name of the file to delete
1186 Last
:= In_Directory
'Length;
1187 Full_Name
(1 .. Last
) := In_Directory
;
1189 if Last
> 0 and then Full_Name
(Last
) /= Directory_Separator
then
1191 Full_Name
(Last
) := Directory_Separator
;
1194 Full_Name
(Last
+ 1 .. Last
+ File
'Length) := File
;
1195 Last
:= Last
+ File
'Length;
1197 -- If switch -n was used, simply output the path name
1200 Put_Line
(Full_Name
(1 .. Last
));
1202 -- Otherwise, delete the file if it is writable
1206 or else Is_Writable_File
(Full_Name
(1 .. Last
))
1208 Delete_File
(Full_Name
(1 .. Last
), Success
);
1213 if Verbose_Mode
or else not Quiet_Output
then
1215 Put
("Warning: """);
1216 Put
(Full_Name
(1 .. Last
));
1217 Put_Line
(""" could not be deleted");
1221 Put
(Full_Name
(1 .. Last
));
1222 Put_Line
(""" has been deleted");
1228 -----------------------------------
1229 -- Delete_Binder_Generated_Files --
1230 -----------------------------------
1232 procedure Delete_Binder_Generated_Files
(Dir
: String; Source
: Name_Id
) is
1233 Source_Name
: constant String := Get_Name_String
(Source
);
1234 Current
: constant String := Get_Current_Dir
;
1235 Last
: constant Positive := B_Start
'Length + Source_Name
'Length;
1236 File_Name
: String (1 .. Last
+ 4);
1241 -- Build the file name (before the extension)
1243 File_Name
(1 .. B_Start
'Length) := B_Start
.all;
1244 File_Name
(B_Start
'Length + 1 .. Last
) := Source_Name
;
1248 File_Name
(Last
+ 1 .. Last
+ 4) := ".ads";
1250 if Is_Regular_File
(File_Name
(1 .. Last
+ 4)) then
1251 Delete
(Dir
, File_Name
(1 .. Last
+ 4));
1256 File_Name
(Last
+ 1 .. Last
+ 4) := ".adb";
1258 if Is_Regular_File
(File_Name
(1 .. Last
+ 4)) then
1259 Delete
(Dir
, File_Name
(1 .. Last
+ 4));
1264 File_Name
(Last
+ 1 .. Last
+ 4) := ".ali";
1266 if Is_Regular_File
(File_Name
(1 .. Last
+ 4)) then
1267 Delete
(Dir
, File_Name
(1 .. Last
+ 4));
1272 File_Name
(Last
+ 1 .. Last
+ Object_Suffix
'Length) := Object_Suffix
;
1274 if Is_Regular_File
(File_Name
(1 .. Last
+ Object_Suffix
'Length)) then
1275 Delete
(Dir
, File_Name
(1 .. Last
+ Object_Suffix
'Length));
1278 -- Change back to previous directory
1280 Change_Dir
(Current
);
1281 end Delete_Binder_Generated_Files
;
1283 -----------------------
1284 -- Display_Copyright --
1285 -----------------------
1287 procedure Display_Copyright
is
1289 if not Copyright_Displayed
then
1290 Copyright_Displayed
:= True;
1292 ("GNATCLEAN " & Gnatvsn
.Gnat_Version_String
1293 & " Copyright 2003-"
1295 & " Free Software Foundation, Inc.");
1297 end Display_Copyright
;
1303 function Empty_Q
return Boolean is
1305 return Q_Front
>= Q
.Last
;
1308 --------------------
1309 -- Extract_From_Q --
1310 --------------------
1312 procedure Extract_From_Q
(Lib_File
: out File_Name_Type
) is
1313 Lib
: constant File_Name_Type
:= Q
.Table
(Q_Front
);
1315 Q_Front
:= Q_Front
+ 1;
1323 procedure Gnatclean
is
1325 -- Do the necessary initializations
1329 -- Parse the command line, getting the switches and the executable names
1333 if Verbose_Mode
then
1337 if Project_File_Name
/= null then
1339 -- A project file was specified by a -P switch
1341 if Opt
.Verbose_Mode
then
1343 Put
("Parsing Project File """);
1344 Put
(Project_File_Name
.all);
1349 -- Set the project parsing verbosity to whatever was specified
1350 -- by a possible -vP switch.
1352 Prj
.Pars
.Set_Verbosity
(To
=> Current_Verbosity
);
1354 -- Parse the project file. If there is an error, Main_Project
1355 -- will still be No_Project.
1358 (Project
=> Main_Project
,
1359 In_Tree
=> Project_Tree
,
1360 Project_File_Name
=> Project_File_Name
.all,
1361 Packages_To_Check
=> Packages_To_Check_By_Gnatmake
);
1363 if Main_Project
= No_Project
then
1364 Fail
("""" & Project_File_Name
.all & """ processing failed");
1367 if Opt
.Verbose_Mode
then
1369 Put
("Parsing of Project File """);
1370 Put
(Project_File_Name
.all);
1371 Put
(""" is finished.");
1375 -- Add source directories and object directories to the search paths
1377 Add_Source_Directories
(Main_Project
, Project_Tree
);
1378 Add_Object_Directories
(Main_Project
, Project_Tree
);
1381 Osint
.Add_Default_Search_Dirs
;
1383 -- If a project file was specified, but no executable name, put all
1384 -- the mains of the project file (if any) as if there were on the
1387 if Main_Project
/= No_Project
and then Osint
.Number_Of_Files
= 0 then
1389 Value
: String_List_Id
:=
1390 Project_Tree
.Projects
.Table
(Main_Project
).Mains
;
1391 Main
: String_Element
;
1393 while Value
/= Prj
.Nil_String
loop
1394 Main
:= Project_Tree
.String_Elements
.Table
(Value
);
1396 (File_Name
=> Get_Name_String
(Main
.Value
),
1397 Index
=> Main
.Index
);
1403 -- If neither a project file nor an executable were specified,
1404 -- output the usage and exit.
1406 if Main_Project
= No_Project
and then Osint
.Number_Of_Files
= 0 then
1411 if Verbose_Mode
then
1415 if Main_Project
/= No_Project
then
1417 -- If a project file has been specified, call Clean_Project with the
1418 -- project id of this project file, after resetting the list of
1419 -- processed projects.
1421 Processed_Projects
.Init
;
1422 Clean_Project
(Main_Project
);
1425 -- If no project file has been specified, the work is done in
1426 -- Clean_Executables.
1431 -- In verbose mode, if Delete has not been called, indicate that
1432 -- no file needs to be deleted.
1434 if Verbose_Mode
and (not File_Deleted
) then
1438 Put_Line
("No file needs to be deleted");
1440 Put_Line
("No file has been deleted");
1445 ------------------------
1446 -- In_Extension_Chain --
1447 ------------------------
1449 function In_Extension_Chain
1450 (Of_Project
: Project_Id
;
1451 Prj
: Project_Id
) return Boolean
1453 Data
: Project_Data
;
1456 if Prj
= No_Project
or else Of_Project
= No_Project
then
1460 if Of_Project
= Prj
then
1464 Data
:= Project_Tree
.Projects
.Table
(Of_Project
);
1466 while Data
.Extends
/= No_Project
loop
1467 if Data
.Extends
= Prj
then
1471 Data
:= Project_Tree
.Projects
.Table
(Data
.Extends
);
1474 Data
:= Project_Tree
.Projects
.Table
(Prj
);
1476 while Data
.Extends
/= No_Project
loop
1477 if Data
.Extends
= Of_Project
then
1481 Data
:= Project_Tree
.Projects
.Table
(Data
.Extends
);
1485 end In_Extension_Chain
;
1494 Q
.Set_Last
(Q
.First
);
1501 procedure Initialize
is
1503 if not Initialized
then
1504 Initialized
:= True;
1506 -- Get default search directories to locate system.ads when calling
1507 -- Targparm.Get_Target_Parameters.
1509 Osint
.Add_Default_Search_Dirs
;
1511 -- Initialize some packages
1516 Prj
.Initialize
(Project_Tree
);
1518 -- Check if the platform is VMS and, if it is, change some variables
1520 Targparm
.Get_Target_Parameters
;
1522 if OpenVMS_On_Target
then
1523 Debug_Suffix
(Debug_Suffix
'First) := '_';
1524 Repinfo_Suffix
(Repinfo_Suffix
'First) := '_';
1525 B_Start
:= new String'("b__");
1529 -- Reset global variables
1531 Free (Object_Directory_Path);
1532 Do_Nothing := False;
1533 File_Deleted := False;
1534 Copyright_Displayed := False;
1535 Usage_Displayed := False;
1536 Free (Project_File_Name);
1537 Main_Project := Prj.No_Project;
1538 All_Projects := False;
1545 procedure Insert_Q (Lib_File : File_Name_Type) is
1547 -- Do not insert an empty name or an already marked source
1549 if Lib_File /= No_Name and then not Makeutl.Is_Marked (Lib_File) then
1550 Q.Table (Q.Last) := Lib_File;
1553 -- Mark the source that has been just added to the Q
1555 Makeutl.Mark (Lib_File);
1559 ----------------------
1560 -- Object_File_Name --
1561 ----------------------
1563 function Object_File_Name (Source : Name_Id) return String is
1564 Src : constant String := Get_Name_String (Source);
1567 -- If the source name has an extension, then replace it with
1568 -- the Object suffix.
1570 for Index in reverse Src'First + 1 .. Src'Last loop
1571 if Src (Index) = '.' then
1572 return Src (Src'First .. Index - 1) & Object_Suffix;
1576 -- If there is no dot, or if it is the first character, just add the
1579 return Src & Object_Suffix;
1580 end Object_File_Name;
1582 --------------------
1583 -- Parse_Cmd_Line --
1584 --------------------
1586 procedure Parse_Cmd_Line is
1587 Source_Index : Int := 0;
1588 Index : Positive := 1;
1589 Last : constant Natural := Argument_Count;
1592 while Index <= Last loop
1594 Arg : constant String := Argument (Index);
1596 procedure Bad_Argument;
1597 -- Signal bad argument
1603 procedure Bad_Argument is
1605 Fail ("invalid argument """, Arg, """");
1609 if Arg'Length /= 0 then
1610 if Arg (1) = '-' then
1611 if Arg'Length = 1 then
1617 if Arg'Length < 4 or else Arg (3) /= 'O
' then
1621 Add_Lib_Search_Dir (Arg (3 .. Arg'Last));
1624 Compile_Only := True;
1627 if Object_Directory_Path /= null then
1628 Fail ("duplicate -D switch");
1630 elsif Project_File_Name /= null then
1631 Fail ("-P and -D cannot be used simultaneously");
1634 if Arg'Length > 2 then
1636 Dir : constant String := Arg (3 .. Arg'Last);
1638 if not Is_Directory (Dir) then
1639 Fail (Dir, " is not a directory");
1641 Add_Lib_Search_Dir (Dir);
1646 if Index = Last then
1647 Fail ("no directory specified after -D");
1653 Dir : constant String := Argument (Index);
1655 if not Is_Directory (Dir) then
1656 Fail (Dir, " is not a directory");
1658 Add_Lib_Search_Dir (Dir);
1664 Force_Deletions := True;
1667 Full_Path_Name_For_Brief_Errors := True;
1673 if Arg'Length = 2 then
1679 for J in 3 .. Arg'Last loop
1680 if Arg (J) not in '0' .. '9' then
1685 (20 * Source_Index) +
1686 (Character'Pos (Arg (J)) - Character'Pos ('0'));
1691 Opt.Look_In_Primary_Dir := False;
1694 if Arg'Length = 2 then
1698 Add_Lib_Search_Dir (Arg (3 .. Arg'Last));
1705 if Project_File_Name /= null then
1706 Fail ("multiple -P switches");
1708 elsif Object_Directory_Path /= null then
1709 Fail ("-D and -P cannot be used simultaneously");
1713 if Arg'Length > 2 then
1715 Prj : constant String := Arg (3 .. Arg'Last);
1717 if Prj'Length > 1 and then
1718 Prj (Prj'First) = '='
1720 Project_File_Name :=
1722 (Prj
(Prj
'First + 1 .. Prj
'Last));
1724 Project_File_Name
:= new String'(Prj);
1729 if Index = Last then
1730 Fail ("no project specified after -P");
1734 Project_File_Name := new String'(Argument
(Index
));
1738 Quiet_Output
:= True;
1741 All_Projects
:= True;
1745 Verbose_Mode
:= True;
1747 elsif Arg
= "-vP0" then
1748 Current_Verbosity
:= Prj
.Default
;
1750 elsif Arg
= "-vP1" then
1751 Current_Verbosity
:= Prj
.Medium
;
1753 elsif Arg
= "-vP2" then
1754 Current_Verbosity
:= Prj
.High
;
1761 if Arg
'Length = 2 then
1766 Ext_Asgn
: constant String := Arg
(3 .. Arg
'Last);
1767 Start
: Positive := Ext_Asgn
'First;
1768 Stop
: Natural := Ext_Asgn
'Last;
1769 Equal_Pos
: Natural;
1770 OK
: Boolean := True;
1773 if Ext_Asgn
(Start
) = '"' then
1774 if Ext_Asgn
(Stop
) = '"' then
1785 while Equal_Pos
<= Stop
1786 and then Ext_Asgn
(Equal_Pos
) /= '='
1788 Equal_Pos
:= Equal_Pos
+ 1;
1791 if Equal_Pos
= Start
or else Equal_Pos
> Stop
then
1798 Ext_Asgn
(Start
.. Equal_Pos
- 1),
1800 Ext_Asgn
(Equal_Pos
+ 1 .. Stop
));
1804 ("illegal external assignment '",
1814 Add_File
(Arg
, Source_Index
);
1823 -----------------------
1824 -- Repinfo_File_Name --
1825 -----------------------
1827 function Repinfo_File_Name
(Source
: Name_Id
) return String is
1829 return Get_Name_String
(Source
) & Repinfo_Suffix
;
1830 end Repinfo_File_Name
;
1832 --------------------
1833 -- Tree_File_Name --
1834 --------------------
1836 function Tree_File_Name
(Source
: Name_Id
) return String is
1837 Src
: constant String := Get_Name_String
(Source
);
1840 -- If the source name has an extension, then replace it with
1843 for Index
in reverse Src
'First + 1 .. Src
'Last loop
1844 if Src
(Index
) = '.' then
1845 return Src
(Src
'First .. Index
- 1) & Tree_Suffix
;
1849 -- If there is no dot, or if it is the first character, just add the
1852 return Src
& Tree_Suffix
;
1855 ---------------------------
1856 -- Ultimate_Extension_Of --
1857 ---------------------------
1859 function Ultimate_Extension_Of
(Project
: Project_Id
) return Project_Id
is
1860 Result
: Project_Id
:= Project
;
1861 Data
: Project_Data
;
1864 if Project
/= No_Project
then
1866 Data
:= Project_Tree
.Projects
.Table
(Result
);
1867 exit when Data
.Extended_By
= No_Project
;
1868 Result
:= Data
.Extended_By
;
1873 end Ultimate_Extension_Of
;
1881 if not Usage_Displayed
then
1882 Usage_Displayed
:= True;
1884 Put_Line
("Usage: gnatclean [switches] {[-innn] name}");
1887 Put_Line
(" names is one or more file names from which " &
1888 "the .adb or .ads suffix may be omitted");
1889 Put_Line
(" names may be omitted if -P<project> is specified");
1892 Put_Line
(" -c Only delete compiler generated files");
1893 Put_Line
(" -D dir Specify dir as the object library");
1894 Put_Line
(" -f Force deletions of unwritable files");
1895 Put_Line
(" -F Full project path name " &
1896 "in brief error messages");
1897 Put_Line
(" -h Display this message");
1898 Put_Line
(" -innn Index of unit in source for following names");
1899 Put_Line
(" -n Nothing to do: only list files to delete");
1900 Put_Line
(" -Pproj Use GNAT Project File proj");
1901 Put_Line
(" -q Be quiet/terse");
1902 Put_Line
(" -r Clean all projects recursively");
1903 Put_Line
(" -v Verbose mode");
1904 Put_Line
(" -vPx Specify verbosity when parsing " &
1905 "GNAT Project Files");
1906 Put_Line
(" -Xnm=val Specify an external reference " &
1907 "for GNAT Project Files");
1910 Put_Line
(" -aOdir Specify ALI/object files search path");
1911 Put_Line
(" -Idir Like -aOdir");
1912 Put_Line
(" -I- Don't look for source/library files " &
1913 "in the default directory");