1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2003-2012, 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
;
42 with Switch
; use Switch
;
44 with Targparm
; use Targparm
;
45 with Types
; use Types
;
47 with Ada
.Command_Line
; use Ada
.Command_Line
;
49 with GNAT
.Directory_Operations
; use GNAT
.Directory_Operations
;
50 with GNAT
.IO
; use GNAT
.IO
;
51 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
55 Initialized
: Boolean := False;
56 -- Set to True by the first call to Initialize.
57 -- To avoid reinitialization of some packages.
59 -- Suffixes of various files
61 Assembly_Suffix
: constant String := ".s";
62 ALI_Suffix
: constant String := ".ali";
63 Tree_Suffix
: constant String := ".adt";
64 Object_Suffix
: constant String := Get_Target_Object_Suffix
.all;
65 Debug_Suffix
: String := ".dg";
66 -- Changed to "_dg" for VMS in the body of the package
68 Repinfo_Suffix
: String := ".rep";
69 -- Changed to "_rep" for VMS in the body of the package
71 B_Start
: String_Ptr
:= new String'("b~");
72 -- Prefix of binder generated file, and number of actual characters used.
73 -- Changed to "b__" for VMS in the body of the package.
75 Project_Tree : constant Project_Tree_Ref :=
76 new Project_Tree_Data (Is_Root_Tree => True);
79 Object_Directory_Path : String_Access := null;
80 -- The path name of the object directory, set with switch -D
82 Force_Deletions : Boolean := False;
83 -- Set to True by switch -f. When True, attempts to delete non writable
84 -- files will be done.
86 Do_Nothing : Boolean := False;
87 -- Set to True when switch -n is specified. When True, no file is deleted.
88 -- gnatclean only lists the files that would have been deleted if the
89 -- switch -n had not been specified.
91 File_Deleted : Boolean := False;
92 -- Set to True if at least one file has been deleted
94 Copyright_Displayed : Boolean := False;
95 Usage_Displayed : Boolean := False;
97 Project_File_Name : String_Access := null;
99 Project_Node_Tree : Project_Node_Tree_Ref;
101 Root_Environment : Prj.Tree.Environment;
103 Main_Project : Prj.Project_Id := Prj.No_Project;
105 All_Projects : Boolean := False;
107 -- Packages of project files where unknown attributes are errors
109 Naming_String : aliased String := "naming";
110 Builder_String : aliased String := "builder";
111 Compiler_String : aliased String := "compiler";
112 Binder_String : aliased String := "binder";
113 Linker_String : aliased String := "linker";
115 Gnatmake_Packages : aliased String_List :=
116 (Naming_String 'Access,
117 Builder_String
'Access,
118 Compiler_String 'Access,
119 Binder_String
'Access,
120 Linker_String 'Access);
122 Packages_To_Check_By_Gnatmake
: constant String_List_Access
:=
123 Gnatmake_Packages
'Access;
125 package Processed_Projects
is new Table
.Table
126 (Table_Component_Type
=> Project_Id
,
127 Table_Index_Type
=> Natural,
128 Table_Low_Bound
=> 0,
130 Table_Increment
=> 100,
131 Table_Name
=> "Clean.Processed_Projects");
132 -- Table to keep track of what project files have been processed, when
133 -- switch -r is specified.
135 package Sources
is new Table
.Table
136 (Table_Component_Type
=> File_Name_Type
,
137 Table_Index_Type
=> Natural,
138 Table_Low_Bound
=> 0,
140 Table_Increment
=> 100,
141 Table_Name
=> "Clean.Processed_Projects");
142 -- Table to store all the source files of a library unit: spec, body and
143 -- subunits, to detect .dg files and delete them.
145 -----------------------------
146 -- Other local subprograms --
147 -----------------------------
149 procedure Add_Source_Dir
(N
: String);
150 -- Call Add_Src_Search_Dir and output one line when in verbose mode
152 procedure Add_Source_Directories
is
153 new Prj
.Env
.For_All_Source_Dirs
(Action
=> Add_Source_Dir
);
155 procedure Add_Object_Dir
(N
: String);
156 -- Call Add_Lib_Search_Dir and output one line when in verbose mode
158 procedure Add_Object_Directories
is
159 new Prj
.Env
.For_All_Object_Dirs
(Action
=> Add_Object_Dir
);
161 function ALI_File_Name
(Source
: File_Name_Type
) return String;
162 -- Returns the name of the ALI file corresponding to Source
164 function Assembly_File_Name
(Source
: File_Name_Type
) return String;
165 -- Returns the assembly file name corresponding to Source
167 procedure Clean_Archive
(Project
: Project_Id
; Global
: Boolean);
168 -- Delete a global archive or library project archive and the dependency
169 -- file, if they exist.
171 procedure Clean_Executables
;
172 -- Do the cleaning work when no project file is specified
174 procedure Clean_Interface_Copy_Directory
(Project
: Project_Id
);
175 -- Delete files in an interface copy directory: any file that is a copy of
176 -- a source of the project.
178 procedure Clean_Library_Directory
(Project
: Project_Id
);
179 -- Delete the library file in a library directory and any ALI file of a
180 -- source of the project in a library ALI directory.
182 procedure Clean_Project
(Project
: Project_Id
);
183 -- Do the cleaning work when a project file is specified. This procedure
184 -- calls itself recursively when there are several project files in the
185 -- tree rooted at the main project file and switch -r has been specified.
187 function Debug_File_Name
(Source
: File_Name_Type
) return String;
188 -- Name of the expanded source file corresponding to Source
190 procedure Delete
(In_Directory
: String; File
: String);
191 -- Delete one file, or list the file name if switch -n is specified
193 procedure Delete_Binder_Generated_Files
195 Source
: File_Name_Type
);
196 -- Delete the binder generated file in directory Dir for Source, if they
197 -- exist: for Unix these are b~<source>.ads, b~<source>.adb,
198 -- b~<source>.ali and b~<source>.o.
200 procedure Display_Copyright
;
201 -- Display the Copyright notice. If called several times, display the
202 -- Copyright notice only the first time.
204 procedure Initialize
;
205 -- Call the necessary package initializations
207 function Object_File_Name
(Source
: File_Name_Type
) return String;
208 -- Returns the object file name corresponding to Source
210 procedure Parse_Cmd_Line
;
211 -- Parse the command line
213 function Repinfo_File_Name
(Source
: File_Name_Type
) return String;
214 -- Returns the repinfo file name corresponding to Source
216 function Tree_File_Name
(Source
: File_Name_Type
) return String;
217 -- Returns the tree file name corresponding to Source
219 function In_Extension_Chain
220 (Of_Project
: Project_Id
;
221 Prj
: Project_Id
) return Boolean;
222 -- Returns True iff Prj is an extension of Of_Project or if Of_Project is
223 -- an extension of Prj.
226 -- Display the usage. If called several times, the usage is displayed only
233 procedure Add_Object_Dir
(N
: String) is
235 Add_Lib_Search_Dir
(N
);
237 if Opt
.Verbose_Mode
then
238 Put
("Adding object directory """);
249 procedure Add_Source_Dir
(N
: String) is
251 Add_Src_Search_Dir
(N
);
253 if Opt
.Verbose_Mode
then
254 Put
("Adding source directory """);
265 function ALI_File_Name
(Source
: File_Name_Type
) return String is
266 Src
: constant String := Get_Name_String
(Source
);
269 -- If the source name has an extension, then replace it with
272 for Index
in reverse Src
'First + 1 .. Src
'Last loop
273 if Src
(Index
) = '.' then
274 return Src
(Src
'First .. Index
- 1) & ALI_Suffix
;
278 -- If there is no dot, or if it is the first character, just add the
281 return Src
& ALI_Suffix
;
284 ------------------------
285 -- Assembly_File_Name --
286 ------------------------
288 function Assembly_File_Name
(Source
: File_Name_Type
) return String is
289 Src
: constant String := Get_Name_String
(Source
);
292 -- If the source name has an extension, then replace it with
293 -- the assembly suffix.
295 for Index
in reverse Src
'First + 1 .. Src
'Last loop
296 if Src
(Index
) = '.' then
297 return Src
(Src
'First .. Index
- 1) & Assembly_Suffix
;
301 -- If there is no dot, or if it is the first character, just add the
304 return Src
& Assembly_Suffix
;
305 end Assembly_File_Name
;
311 procedure Clean_Archive
(Project
: Project_Id
; Global
: Boolean) is
312 Current_Dir
: constant Dir_Name_Str
:= Get_Current_Dir
;
314 Lib_Prefix
: String_Access
;
315 Archive_Name
: String_Access
;
316 -- The name of the archive file for this project
318 Archive_Dep_Name
: String_Access
;
319 -- The name of the archive dependency file for this project
321 Obj_Dir
: constant String :=
322 Get_Name_String
(Project
.Object_Directory
.Display_Name
);
325 Change_Dir
(Obj_Dir
);
327 -- First, get the lib prefix, the archive file name and the archive
328 -- dependency file name.
332 new String'("lib" & Get_Name_String (Project.Display_Name));
335 new String'("lib" & Get_Name_String
(Project
.Library_Name
));
338 Archive_Name
:= new String'(Lib_Prefix.all & '.' & Archive_Ext);
339 Archive_Dep_Name := new String'(Lib_Prefix
.all & ".deps");
341 -- Delete the archive file and the archive dependency file, if they
344 if Is_Regular_File
(Archive_Name
.all) then
345 Delete
(Obj_Dir
, Archive_Name
.all);
348 if Is_Regular_File
(Archive_Dep_Name
.all) then
349 Delete
(Obj_Dir
, Archive_Dep_Name
.all);
352 Change_Dir
(Current_Dir
);
355 -----------------------
356 -- Clean_Executables --
357 -----------------------
359 procedure Clean_Executables
is
360 Main_Source_File
: File_Name_Type
;
361 -- Current main source
363 Main_Lib_File
: File_Name_Type
;
364 -- ALI file of the current main
366 Lib_File
: File_Name_Type
;
369 Full_Lib_File
: File_Name_Type
;
370 -- Full name of the current ALI file
372 Text
: Text_Buffer_Ptr
;
375 Source
: Queue
.Source_Info
;
378 Queue
.Initialize
(Queue_Per_Obj_Dir
=> False);
380 -- It does not really matter if there is or not an object file
381 -- corresponding to an ALI file: if there is one, it will be deleted.
383 Opt
.Check_Object_Consistency
:= False;
385 -- Proceed each executable one by one. Each source is marked as it is
386 -- processed, so common sources between executables will not be
387 -- processed several times.
389 for N_File
in 1 .. Osint
.Number_Of_Files
loop
390 Main_Source_File
:= Next_Main_Source
;
392 Osint
.Lib_File_Name
(Main_Source_File
, Current_File_Index
);
394 if Main_Lib_File
/= No_File
then
396 ((Format
=> Format_Gnatmake
,
397 File
=> Main_Lib_File
,
398 Unit
=> No_Unit_Name
,
400 Project
=> No_Project
,
404 while not Queue
.Is_Empty
loop
405 Sources
.Set_Last
(0);
406 Queue
.Extract
(Found
, Source
);
407 pragma Assert
(Found
);
408 pragma Assert
(Source
.File
/= No_File
);
409 Lib_File
:= Source
.File
;
410 Full_Lib_File
:= Osint
.Full_Lib_File_Name
(Lib_File
);
412 -- If we have existing ALI file that is not read-only, process it
414 if Full_Lib_File
/= No_File
415 and then not Is_Readonly_Library
(Full_Lib_File
)
417 Text
:= Read_Library_Info
(Lib_File
);
421 Scan_ALI
(Lib_File
, Text
, Ignore_ED
=> False, Err
=> True);
424 -- If no error was produced while loading this ALI file,
425 -- insert into the queue all the unmarked withed sources.
427 if The_ALI
/= No_ALI_Id
then
428 for J
in ALIs
.Table
(The_ALI
).First_Unit
..
429 ALIs
.Table
(The_ALI
).Last_Unit
431 Sources
.Increment_Last
;
432 Sources
.Table
(Sources
.Last
) :=
433 ALI
.Units
.Table
(J
).Sfile
;
435 for K
in ALI
.Units
.Table
(J
).First_With
..
436 ALI
.Units
.Table
(J
).Last_With
438 if Withs
.Table
(K
).Afile
/= No_File
then
440 ((Format
=> Format_Gnatmake
,
441 File
=> Withs
.Table
(K
).Afile
,
442 Unit
=> No_Unit_Name
,
444 Project
=> No_Project
,
450 -- Look for subunits and put them in the Sources table
452 for J
in ALIs
.Table
(The_ALI
).First_Sdep
..
453 ALIs
.Table
(The_ALI
).Last_Sdep
455 if Sdep
.Table
(J
).Subunit_Name
/= No_Name
then
456 Sources
.Increment_Last
;
457 Sources
.Table
(Sources
.Last
) :=
458 Sdep
.Table
(J
).Sfile
;
464 -- Now delete all existing files corresponding to this ALI file
467 Obj_Dir
: constant String :=
468 Dir_Name
(Get_Name_String
(Full_Lib_File
));
469 Obj
: constant String := Object_File_Name
(Lib_File
);
470 Adt
: constant String := Tree_File_Name
(Lib_File
);
471 Asm
: constant String := Assembly_File_Name
(Lib_File
);
474 Delete
(Obj_Dir
, Get_Name_String
(Lib_File
));
476 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Obj
) then
477 Delete
(Obj_Dir
, Obj
);
480 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Adt
) then
481 Delete
(Obj_Dir
, Adt
);
484 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Asm
) then
485 Delete
(Obj_Dir
, Asm
);
488 -- Delete expanded source files (.dg) and/or repinfo files
491 for J
in 1 .. Sources
.Last
loop
493 Deb
: constant String :=
494 Debug_File_Name
(Sources
.Table
(J
));
495 Rep
: constant String :=
496 Repinfo_File_Name
(Sources
.Table
(J
));
499 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Deb
) then
500 Delete
(Obj_Dir
, Deb
);
503 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Rep
) then
504 Delete
(Obj_Dir
, Rep
);
512 -- Delete the executable, if it exists, and the binder generated
515 if not Compile_Only
then
517 Source
: constant File_Name_Type
:=
518 Strip_Suffix
(Main_Lib_File
);
519 Executable
: constant String :=
520 Get_Name_String
(Executable_Name
(Source
));
522 if Is_Regular_File
(Executable
) then
523 Delete
("", Executable
);
526 Delete_Binder_Generated_Files
(Get_Current_Dir
, Source
);
530 end Clean_Executables
;
532 ------------------------------------
533 -- Clean_Interface_Copy_Directory --
534 ------------------------------------
536 procedure Clean_Interface_Copy_Directory
(Project
: Project_Id
) is
537 Current
: constant String := Get_Current_Dir
;
541 Name
: String (1 .. 200);
544 Delete_File
: Boolean;
549 and then Project
.Library_Src_Dir
/= No_Path_Information
552 Directory
: constant String :=
553 Get_Name_String
(Project
.Library_Src_Dir
.Display_Name
);
556 Change_Dir
(Directory
);
559 -- For each regular file in the directory, if switch -n has not
560 -- been specified, make it writable and delete the file if it is
561 -- a copy of a source of the project.
564 Read
(Direc
, Name
, Last
);
568 Filename
: constant String := Name
(1 .. Last
);
571 if Is_Regular_File
(Filename
) then
572 Canonical_Case_File_Name
(Name
(1 .. Last
));
573 Delete_File
:= False;
575 Unit
:= Units_Htable
.Get_First
(Project_Tree
.Units_HT
);
577 -- Compare with source file names of the project
579 while Unit
/= No_Unit_Index
loop
580 if Unit
.File_Names
(Impl
) /= null
581 and then Ultimate_Extending_Project_Of
582 (Unit
.File_Names
(Impl
).Project
) = Project
584 Get_Name_String
(Unit
.File_Names
(Impl
).File
) =
591 if Unit
.File_Names
(Spec
) /= null
592 and then Ultimate_Extending_Project_Of
593 (Unit
.File_Names
(Spec
).Project
) = Project
596 (Unit
.File_Names
(Spec
).File
) = Name
(1 .. Last
)
602 Unit
:= Units_Htable
.Get_Next
(Project_Tree
.Units_HT
);
606 if not Do_Nothing
then
607 Set_Writable
(Filename
);
610 Delete
(Directory
, Filename
);
618 -- Restore the initial working directory
620 Change_Dir
(Current
);
623 end Clean_Interface_Copy_Directory
;
625 -----------------------------
626 -- Clean_Library_Directory --
627 -----------------------------
629 Empty_String
: aliased String := "";
631 procedure Clean_Library_Directory
(Project
: Project_Id
) is
632 Current
: constant String := Get_Current_Dir
;
634 Lib_Filename
: constant String := Get_Name_String
(Project
.Library_Name
);
636 DLL_Prefix
& Lib_Filename
& "." & DLL_Ext
;
637 Archive_Name
: String :=
638 "lib" & Lib_Filename
& "." & Archive_Ext
;
641 Name
: String (1 .. 200);
644 Delete_File
: Boolean;
646 Minor
: String_Access
:= Empty_String
'Access;
647 Major
: String_Access
:= Empty_String
'Access;
650 if Project
.Library
then
651 if Project
.Library_Kind
/= Static
652 and then MLib
.Tgt
.Library_Major_Minor_Id_Supported
653 and then Project
.Lib_Internal_Name
/= No_Name
655 Minor
:= new String'(Get_Name_String (Project.Lib_Internal_Name));
656 Major := new String'(MLib
.Major_Id_Name
(DLL_Name
, Minor
.all));
660 Lib_Directory
: constant String :=
661 Get_Name_String
(Project
.Library_Dir
.Display_Name
);
662 Lib_ALI_Directory
: constant String :=
663 Get_Name_String
(Project
.Library_ALI_Dir
.Display_Name
);
666 Canonical_Case_File_Name
(Archive_Name
);
667 Canonical_Case_File_Name
(DLL_Name
);
669 Change_Dir
(Lib_Directory
);
672 -- For each regular file in the directory, if switch -n has not
673 -- been specified, make it writable and delete the file if it is
677 Read
(Direc
, Name
, Last
);
681 Filename
: constant String := Name
(1 .. Last
);
684 if Is_Regular_File
(Filename
)
685 or else Is_Symbolic_Link
(Filename
)
687 Canonical_Case_File_Name
(Name
(1 .. Last
));
688 Delete_File
:= False;
690 if (Project
.Library_Kind
= Static
691 and then Name
(1 .. Last
) = Archive_Name
)
693 ((Project
.Library_Kind
= Dynamic
695 Project
.Library_Kind
= Relocatable
)
697 (Name
(1 .. Last
) = DLL_Name
699 Name
(1 .. Last
) = Minor
.all
701 Name
(1 .. Last
) = Major
.all))
703 if not Do_Nothing
then
704 Set_Writable
(Filename
);
707 Delete
(Lib_Directory
, Filename
);
715 Change_Dir
(Lib_ALI_Directory
);
718 -- For each regular file in the directory, if switch -n has not
719 -- been specified, make it writable and delete the file if it is
720 -- any ALI file of a source of the project.
723 Read
(Direc
, Name
, Last
);
727 Filename
: constant String := Name
(1 .. Last
);
729 if Is_Regular_File
(Filename
) then
730 Canonical_Case_File_Name
(Name
(1 .. Last
));
731 Delete_File
:= False;
733 if Last
> 4 and then Name
(Last
- 3 .. Last
) = ".ali" then
737 -- Compare with ALI file names of the project
739 Unit
:= Units_Htable
.Get_First
740 (Project_Tree
.Units_HT
);
741 while Unit
/= No_Unit_Index
loop
742 if Unit
.File_Names
(Impl
) /= null
743 and then Unit
.File_Names
(Impl
).Project
/=
746 if Ultimate_Extending_Project_Of
747 (Unit
.File_Names
(Impl
).Project
) =
751 (Unit
.File_Names
(Impl
).File
);
752 Name_Len
:= Name_Len
-
754 (Name
(1 .. Name_Len
))'Length;
755 if Name_Buffer
(1 .. Name_Len
) =
763 elsif Unit
.File_Names
(Spec
) /= null
764 and then Ultimate_Extending_Project_Of
765 (Unit
.File_Names
(Spec
).Project
) =
769 (Unit
.File_Names
(Spec
).File
);
773 (Name
(1 .. Name_Len
))'Length;
775 if Name_Buffer
(1 .. Name_Len
) =
784 Units_Htable
.Get_Next
(Project_Tree
.Units_HT
);
790 if not Do_Nothing
then
791 Set_Writable
(Filename
);
794 Delete
(Lib_ALI_Directory
, Filename
);
802 -- Restore the initial working directory
804 Change_Dir
(Current
);
807 end Clean_Library_Directory
;
813 procedure Clean_Project
(Project
: Project_Id
) is
814 Main_Source_File
: File_Name_Type
;
815 -- Name of executable on the command line without directory info
817 Executable
: File_Name_Type
;
818 -- Name of the executable file
820 Current_Dir
: constant Dir_Name_Str
:= Get_Current_Dir
;
822 File_Name1
: File_Name_Type
;
824 File_Name2
: File_Name_Type
;
826 Lib_File
: File_Name_Type
;
828 Global_Archive
: Boolean := False;
831 -- Check that we don't specify executable on the command line for
832 -- a main library project.
834 if Project
= Main_Project
835 and then Osint
.Number_Of_Files
/= 0
836 and then Project
.Library
839 ("Cannot specify executable(s) for a Library Project File");
842 -- Nothing to clean in an externally built project
844 if Project
.Externally_Built
then
846 Put
("Nothing to do to clean externally built project """);
847 Put
(Get_Name_String
(Project
.Name
));
853 Put
("Cleaning project """);
854 Put
(Get_Name_String
(Project
.Name
));
858 -- Add project to the list of processed projects
860 Processed_Projects
.Increment_Last
;
861 Processed_Projects
.Table
(Processed_Projects
.Last
) := Project
;
863 if Project
.Object_Directory
/= No_Path_Information
then
865 Obj_Dir
: constant String :=
866 Get_Name_String
(Project
.Object_Directory
.Display_Name
);
869 Change_Dir
(Obj_Dir
);
871 -- First, deal with Ada
873 -- Look through the units to find those that are either
874 -- immediate sources or inherited sources of the project.
875 -- Extending projects may have no language specified, if
876 -- Source_Dirs or Source_Files is specified as an empty list,
877 -- so always look for Ada units in extending projects.
879 if Has_Ada_Sources
(Project
)
880 or else Project
.Extends
/= No_Project
882 Unit
:= Units_Htable
.Get_First
(Project_Tree
.Units_HT
);
883 while Unit
/= No_Unit_Index
loop
884 File_Name1
:= No_File
;
885 File_Name2
:= No_File
;
887 -- If either the spec or the body is a source of the
888 -- project, check for the corresponding ALI file in the
891 if (Unit
.File_Names
(Impl
) /= null
894 (Unit
.File_Names
(Impl
).Project
, Project
))
896 (Unit
.File_Names
(Spec
) /= null
897 and then In_Extension_Chain
898 (Unit
.File_Names
(Spec
).Project
, Project
))
900 if Unit
.File_Names
(Impl
) /= null then
901 File_Name1
:= Unit
.File_Names
(Impl
).File
;
902 Index1
:= Unit
.File_Names
(Impl
).Index
;
904 File_Name1
:= No_File
;
908 if Unit
.File_Names
(Spec
) /= null then
909 File_Name2
:= Unit
.File_Names
(Spec
).File
;
910 Index2
:= Unit
.File_Names
(Spec
).Index
;
912 File_Name2
:= No_File
;
916 -- If there is no body file name, then there may be
919 if File_Name1
= No_File
then
920 File_Name1
:= File_Name2
;
922 File_Name2
:= No_File
;
927 -- If there is either a spec or a body, look for files
928 -- in the object directory.
930 if File_Name1
/= No_File
then
931 Lib_File
:= Osint
.Lib_File_Name
(File_Name1
, Index1
);
934 Asm
: constant String :=
935 Assembly_File_Name
(Lib_File
);
936 ALI
: constant String :=
937 ALI_File_Name
(Lib_File
);
938 Obj
: constant String :=
939 Object_File_Name
(Lib_File
);
940 Adt
: constant String :=
941 Tree_File_Name
(Lib_File
);
942 Deb
: constant String :=
943 Debug_File_Name
(File_Name1
);
944 Rep
: constant String :=
945 Repinfo_File_Name
(File_Name1
);
946 Del
: Boolean := True;
949 -- If the ALI file exists and is read-only, no file
952 if Is_Regular_File
(ALI
) then
953 if Is_Writable_File
(ALI
) then
954 Delete
(Obj_Dir
, ALI
);
963 if Obj_Dir
(Obj_Dir
'Last) /=
970 Put_Line
(""" is read-only");
979 if Is_Regular_File
(Obj
) then
980 Delete
(Obj_Dir
, Obj
);
985 if Is_Regular_File
(Asm
) then
986 Delete
(Obj_Dir
, Asm
);
991 if Is_Regular_File
(Adt
) then
992 Delete
(Obj_Dir
, Adt
);
995 -- First expanded source file
997 if Is_Regular_File
(Deb
) then
998 Delete
(Obj_Dir
, Deb
);
1003 if Is_Regular_File
(Rep
) then
1004 Delete
(Obj_Dir
, Rep
);
1007 -- Second expanded source file
1009 if File_Name2
/= No_File
then
1011 Deb
: constant String :=
1012 Debug_File_Name
(File_Name2
);
1013 Rep
: constant String :=
1014 Repinfo_File_Name
(File_Name2
);
1017 if Is_Regular_File
(Deb
) then
1018 Delete
(Obj_Dir
, Deb
);
1021 if Is_Regular_File
(Rep
) then
1022 Delete
(Obj_Dir
, Rep
);
1030 Unit
:= Units_Htable
.Get_Next
(Project_Tree
.Units_HT
);
1034 -- Check if a global archive and it dependency file could have
1035 -- been created and, if they exist, delete them.
1037 if Project
= Main_Project
and then not Project
.Library
then
1038 Global_Archive
:= False;
1041 Proj
: Project_List
;
1044 Proj
:= Project_Tree
.Projects
;
1045 while Proj
/= null loop
1047 -- For gnatmake, when the project specifies more than
1048 -- just Ada as a language (even if course we could not
1049 -- find any source file for the other languages), we
1050 -- will take all the object files found in the object
1051 -- directories. Since we know the project supports at
1052 -- least Ada, we just have to test whether it has at
1053 -- least two languages, and we do not care about the
1056 if Proj
.Project
.Languages
/= null
1057 and then Proj
.Project
.Languages
.Next
/= null
1059 Global_Archive
:= True;
1067 if Global_Archive
then
1068 Clean_Archive
(Project
, Global
=> True);
1075 -- If this is a library project, clean the library directory, the
1076 -- interface copy dir and, for a Stand-Alone Library, the binder
1077 -- generated files of the library.
1079 -- The directories are cleaned only if switch -c is not specified
1081 if Project
.Library
then
1082 if not Compile_Only
then
1083 Clean_Library_Directory
(Project
);
1085 if Project
.Library_Src_Dir
/= No_Path_Information
then
1086 Clean_Interface_Copy_Directory
(Project
);
1090 if Project
.Standalone_Library
/= No
1091 and then Project
.Object_Directory
/= No_Path_Information
1093 Delete_Binder_Generated_Files
1094 (Get_Name_String
(Project
.Object_Directory
.Display_Name
),
1095 File_Name_Type
(Project
.Library_Name
));
1099 if Verbose_Mode
then
1104 -- If switch -r is specified, call Clean_Project recursively for the
1105 -- imported projects and the project being extended.
1107 if All_Projects
then
1109 Imported
: Project_List
;
1113 -- For each imported project, call Clean_Project if the project
1114 -- has not been processed already.
1116 Imported
:= Project
.Imported_Projects
;
1117 while Imported
/= null loop
1121 J
in Processed_Projects
.First
.. Processed_Projects
.Last
1123 if Imported
.Project
= Processed_Projects
.Table
(J
) then
1130 Clean_Project
(Imported
.Project
);
1133 Imported
:= Imported
.Next
;
1136 -- If this project extends another project, call Clean_Project for
1137 -- the project being extended. It is guaranteed that it has not
1138 -- called before, because no other project may import or extend
1141 if Project
.Extends
/= No_Project
then
1142 Clean_Project
(Project
.Extends
);
1147 -- For the main project, delete the executables and the binder
1150 -- The executables are deleted only if switch -c is not specified
1152 if Project
= Main_Project
1153 and then Project
.Exec_Directory
/= No_Path_Information
1156 Exec_Dir
: constant String :=
1157 Get_Name_String
(Project
.Exec_Directory
.Display_Name
);
1160 Change_Dir
(Exec_Dir
);
1162 for N_File
in 1 .. Osint
.Number_Of_Files
loop
1163 Main_Source_File
:= Next_Main_Source
;
1165 if not Compile_Only
then
1169 Project_Tree
.Shared
,
1171 Current_File_Index
);
1174 Exec_File_Name
: constant String :=
1175 Get_Name_String
(Executable
);
1178 if Is_Absolute_Path
(Name
=> Exec_File_Name
) then
1179 if Is_Regular_File
(Exec_File_Name
) then
1180 Delete
("", Exec_File_Name
);
1184 if Is_Regular_File
(Exec_File_Name
) then
1185 Delete
(Exec_Dir
, Exec_File_Name
);
1191 if Project
.Object_Directory
/= No_Path_Information
then
1192 Delete_Binder_Generated_Files
1193 (Get_Name_String
(Project
.Object_Directory
.Display_Name
),
1194 Strip_Suffix
(Main_Source_File
));
1200 -- Change back to previous directory
1202 Change_Dir
(Current_Dir
);
1205 ---------------------
1206 -- Debug_File_Name --
1207 ---------------------
1209 function Debug_File_Name
(Source
: File_Name_Type
) return String is
1211 return Get_Name_String
(Source
) & Debug_Suffix
;
1212 end Debug_File_Name
;
1218 procedure Delete
(In_Directory
: String; File
: String) is
1219 Full_Name
: String (1 .. In_Directory
'Length + File
'Length + 1);
1220 Last
: Natural := 0;
1224 -- Indicate that at least one file is deleted or is to be deleted
1226 File_Deleted
:= True;
1228 -- Build the path name of the file to delete
1230 Last
:= In_Directory
'Length;
1231 Full_Name
(1 .. Last
) := In_Directory
;
1233 if Last
> 0 and then Full_Name
(Last
) /= Directory_Separator
then
1235 Full_Name
(Last
) := Directory_Separator
;
1238 Full_Name
(Last
+ 1 .. Last
+ File
'Length) := File
;
1239 Last
:= Last
+ File
'Length;
1241 -- If switch -n was used, simply output the path name
1244 Put_Line
(Full_Name
(1 .. Last
));
1246 -- Otherwise, delete the file if it is writable
1250 or else Is_Writable_File
(Full_Name
(1 .. Last
))
1251 or else Is_Symbolic_Link
(Full_Name
(1 .. Last
))
1253 -- On VMS, we have to delete all versions of the file
1255 if OpenVMS_On_Target
then
1256 Delete_File
(Full_Name
(1 .. Last
) & ";*", Success
);
1258 -- Otherwise just delete the specified file
1261 Delete_File
(Full_Name
(1 .. Last
), Success
);
1264 -- Here if no deletion required
1270 if Verbose_Mode
or else not Quiet_Output
then
1272 Put
("Warning: """);
1273 Put
(Full_Name
(1 .. Last
));
1274 Put_Line
(""" could not be deleted");
1278 Put
(Full_Name
(1 .. Last
));
1279 Put_Line
(""" has been deleted");
1285 -----------------------------------
1286 -- Delete_Binder_Generated_Files --
1287 -----------------------------------
1289 procedure Delete_Binder_Generated_Files
1291 Source
: File_Name_Type
)
1293 Source_Name
: constant String := Get_Name_String
(Source
);
1294 Current
: constant String := Get_Current_Dir
;
1295 Last
: constant Positive := B_Start
'Length + Source_Name
'Length;
1296 File_Name
: String (1 .. Last
+ 4);
1301 -- Build the file name (before the extension)
1303 File_Name
(1 .. B_Start
'Length) := B_Start
.all;
1304 File_Name
(B_Start
'Length + 1 .. Last
) := Source_Name
;
1308 File_Name
(Last
+ 1 .. Last
+ 4) := ".ads";
1310 if Is_Regular_File
(File_Name
(1 .. Last
+ 4)) then
1311 Delete
(Dir
, File_Name
(1 .. Last
+ 4));
1316 File_Name
(Last
+ 1 .. Last
+ 4) := ".adb";
1318 if Is_Regular_File
(File_Name
(1 .. Last
+ 4)) then
1319 Delete
(Dir
, File_Name
(1 .. Last
+ 4));
1324 File_Name
(Last
+ 1 .. Last
+ 4) := ".ali";
1326 if Is_Regular_File
(File_Name
(1 .. Last
+ 4)) then
1327 Delete
(Dir
, File_Name
(1 .. Last
+ 4));
1332 File_Name
(Last
+ 1 .. Last
+ Object_Suffix
'Length) := Object_Suffix
;
1334 if Is_Regular_File
(File_Name
(1 .. Last
+ Object_Suffix
'Length)) then
1335 Delete
(Dir
, File_Name
(1 .. Last
+ Object_Suffix
'Length));
1338 -- Change back to previous directory
1340 Change_Dir
(Current
);
1341 end Delete_Binder_Generated_Files
;
1343 -----------------------
1344 -- Display_Copyright --
1345 -----------------------
1347 procedure Display_Copyright
is
1349 if not Copyright_Displayed
then
1350 Copyright_Displayed
:= True;
1351 Display_Version
("GNATCLEAN", "2003");
1353 end Display_Copyright
;
1359 procedure Gnatclean
is
1361 -- Do the necessary initializations
1365 -- Parse the command line, getting the switches and the executable names
1369 if Verbose_Mode
then
1373 if Project_File_Name
/= null then
1375 -- A project file was specified by a -P switch
1377 if Opt
.Verbose_Mode
then
1379 Put
("Parsing Project File """);
1380 Put
(Project_File_Name
.all);
1385 -- Set the project parsing verbosity to whatever was specified
1386 -- by a possible -vP switch.
1388 Prj
.Pars
.Set_Verbosity
(To
=> Current_Verbosity
);
1390 -- Parse the project file. If there is an error, Main_Project
1391 -- will still be No_Project.
1394 (Project
=> Main_Project
,
1395 In_Tree
=> Project_Tree
,
1396 In_Node_Tree
=> Project_Node_Tree
,
1397 Project_File_Name
=> Project_File_Name
.all,
1398 Env
=> Root_Environment
,
1399 Packages_To_Check
=> Packages_To_Check_By_Gnatmake
);
1401 if Main_Project
= No_Project
then
1402 Fail
("""" & Project_File_Name
.all & """ processing failed");
1405 if Opt
.Verbose_Mode
then
1407 Put
("Parsing of Project File """);
1408 Put
(Project_File_Name
.all);
1409 Put
(""" is finished.");
1413 -- Add source directories and object directories to the search paths
1415 Add_Source_Directories
(Main_Project
, Project_Tree
);
1416 Add_Object_Directories
(Main_Project
, Project_Tree
);
1419 Osint
.Add_Default_Search_Dirs
;
1421 -- If a project file was specified, but no executable name, put all
1422 -- the mains of the project file (if any) as if there were on the
1425 if Main_Project
/= No_Project
and then Osint
.Number_Of_Files
= 0 then
1427 Main
: String_Element
;
1428 Value
: String_List_Id
:= Main_Project
.Mains
;
1430 while Value
/= Prj
.Nil_String
loop
1431 Main
:= Project_Tree
.Shared
.String_Elements
.Table
(Value
);
1433 (File_Name
=> Get_Name_String
(Main
.Value
),
1434 Index
=> Main
.Index
);
1440 -- If neither a project file nor an executable were specified, output
1441 -- the usage and exit.
1443 if Main_Project
= No_Project
and then Osint
.Number_Of_Files
= 0 then
1448 if Verbose_Mode
then
1452 if Main_Project
/= No_Project
then
1454 -- If a project file has been specified, call Clean_Project with the
1455 -- project id of this project file, after resetting the list of
1456 -- processed projects.
1458 Processed_Projects
.Init
;
1459 Clean_Project
(Main_Project
);
1462 -- If no project file has been specified, the work is done in
1463 -- Clean_Executables.
1468 -- In verbose mode, if Delete has not been called, indicate that no file
1469 -- needs to be deleted.
1471 if Verbose_Mode
and (not File_Deleted
) then
1475 Put_Line
("No file needs to be deleted");
1477 Put_Line
("No file has been deleted");
1482 ------------------------
1483 -- In_Extension_Chain --
1484 ------------------------
1486 function In_Extension_Chain
1487 (Of_Project
: Project_Id
;
1488 Prj
: Project_Id
) return Boolean
1493 if Prj
= No_Project
or else Of_Project
= No_Project
then
1497 if Of_Project
= Prj
then
1502 while Proj
.Extends
/= No_Project
loop
1503 if Proj
.Extends
= Prj
then
1507 Proj
:= Proj
.Extends
;
1511 while Proj
.Extends
/= No_Project
loop
1512 if Proj
.Extends
= Of_Project
then
1516 Proj
:= Proj
.Extends
;
1520 end In_Extension_Chain
;
1526 procedure Initialize
is
1528 if not Initialized
then
1529 Initialized
:= True;
1531 -- Get default search directories to locate system.ads when calling
1532 -- Targparm.Get_Target_Parameters.
1534 Osint
.Add_Default_Search_Dirs
;
1536 -- Initialize some packages
1541 Prj
.Tree
.Initialize
(Root_Environment
, Gnatmake_Flags
);
1542 Prj
.Env
.Initialize_Default_Project_Path
1543 (Root_Environment
.Project_Path
,
1544 Target_Name
=> Sdefault
.Target_Name
.all);
1546 Project_Node_Tree
:= new Project_Node_Tree_Data
;
1547 Prj
.Tree
.Initialize
(Project_Node_Tree
);
1549 Prj
.Initialize
(Project_Tree
);
1551 -- Check if the platform is VMS and, if it is, change some variables
1553 Targparm
.Get_Target_Parameters
;
1555 if OpenVMS_On_Target
then
1556 Debug_Suffix
(Debug_Suffix
'First) := '_';
1557 Repinfo_Suffix
(Repinfo_Suffix
'First) := '_';
1558 B_Start
:= new String'("b__");
1562 -- Reset global variables
1564 Free (Object_Directory_Path);
1565 Do_Nothing := False;
1566 File_Deleted := False;
1567 Copyright_Displayed := False;
1568 Usage_Displayed := False;
1569 Free (Project_File_Name);
1570 Main_Project := Prj.No_Project;
1571 All_Projects := False;
1574 ----------------------
1575 -- Object_File_Name --
1576 ----------------------
1578 function Object_File_Name (Source : File_Name_Type) return String is
1579 Src : constant String := Get_Name_String (Source);
1582 -- If the source name has an extension, then replace it with
1583 -- the Object suffix.
1585 for Index in reverse Src'First + 1 .. Src'Last loop
1586 if Src (Index) = '.' then
1587 return Src (Src'First .. Index - 1) & Object_Suffix;
1591 -- If there is no dot, or if it is the first character, just add the
1594 return Src & Object_Suffix;
1595 end Object_File_Name;
1597 --------------------
1598 -- Parse_Cmd_Line --
1599 --------------------
1601 procedure Parse_Cmd_Line is
1602 Last : constant Natural := Argument_Count;
1603 Source_Index : Int := 0;
1606 procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
1609 -- First, check for --version and --help
1611 Check_Version_And_Help ("GNATCLEAN", "2003");
1614 while Index <= Last loop
1616 Arg : constant String := Argument (Index);
1618 procedure Bad_Argument;
1619 -- Signal bad argument
1625 procedure Bad_Argument is
1627 Fail ("invalid argument """ & Arg & """");
1631 if Arg'Length /= 0 then
1632 if Arg (1) = '-' then
1633 if Arg'Length = 1 then
1639 if Arg'Length > Subdirs_Option'Length and then
1640 Arg (1 .. Subdirs_Option'Length) = Subdirs_Option
1644 (Arg
(Subdirs_Option
'Length + 1 .. Arg
'Last));
1646 elsif Arg
= Makeutl
.Unchecked_Shared_Lib_Imports
then
1647 Opt
.Unchecked_Shared_Lib_Imports
:= True;
1654 if Arg
'Length < 4 then
1658 if Arg
(3) = 'O' then
1659 Add_Lib_Search_Dir
(Arg
(4 .. Arg
'Last));
1661 elsif Arg
(3) = 'P' then
1662 Prj
.Env
.Add_Directories
1663 (Root_Environment
.Project_Path
,
1664 Arg
(4 .. Arg
'Last));
1671 Compile_Only
:= True;
1674 if Object_Directory_Path
/= null then
1675 Fail
("duplicate -D switch");
1677 elsif Project_File_Name
/= null then
1678 Fail
("-P and -D cannot be used simultaneously");
1681 if Arg
'Length > 2 then
1683 Dir
: constant String := Arg
(3 .. Arg
'Last);
1685 if not Is_Directory
(Dir
) then
1686 Fail
(Dir
& " is not a directory");
1688 Add_Lib_Search_Dir
(Dir
);
1693 if Index
= Last
then
1694 Fail
("no directory specified after -D");
1700 Dir
: constant String := Argument
(Index
);
1702 if not Is_Directory
(Dir
) then
1703 Fail
(Dir
& " is not a directory");
1705 Add_Lib_Search_Dir
(Dir
);
1712 Follow_Links_For_Files
:= True;
1713 Follow_Links_For_Dirs
:= True;
1720 Force_Deletions
:= True;
1723 Full_Path_Name_For_Brief_Errors
:= True;
1729 if Arg
'Length = 2 then
1735 for J
in 3 .. Arg
'Last loop
1736 if Arg
(J
) not in '0' .. '9' then
1741 (20 * Source_Index
) +
1742 (Character'Pos (Arg
(J
)) - Character'Pos ('0'));
1747 Opt
.Look_In_Primary_Dir
:= False;
1750 if Arg
'Length = 2 then
1754 Add_Lib_Search_Dir
(Arg
(3 .. Arg
'Last));
1761 if Project_File_Name
/= null then
1762 Fail
("multiple -P switches");
1764 elsif Object_Directory_Path
/= null then
1765 Fail
("-D and -P cannot be used simultaneously");
1769 if Arg
'Length > 2 then
1771 Prj
: constant String := Arg
(3 .. Arg
'Last);
1773 if Prj
'Length > 1 and then
1774 Prj
(Prj
'First) = '='
1776 Project_File_Name
:=
1778 (Prj (Prj'First + 1 .. Prj'Last));
1780 Project_File_Name := new String'(Prj
);
1785 if Index
= Last
then
1786 Fail
("no project specified after -P");
1790 Project_File_Name
:= new String'(Argument (Index));
1794 Quiet_Output := True;
1797 All_Projects := True;
1801 Verbose_Mode := True;
1803 elsif Arg = "-vP0" then
1804 Current_Verbosity := Prj.Default;
1806 elsif Arg = "-vP1" then
1807 Current_Verbosity := Prj.Medium;
1809 elsif Arg = "-vP2" then
1810 Current_Verbosity := Prj.High;
1817 if Arg'Length = 2 then
1822 Ext_Asgn : constant String := Arg (3 .. Arg'Last);
1823 Start : Positive := Ext_Asgn'First;
1824 Stop : Natural := Ext_Asgn'Last;
1825 OK : Boolean := True;
1828 if Ext_Asgn (Start) = '"' then
1829 if Ext_Asgn (Stop) = '"' then
1840 Prj.Ext.Check (Root_Environment.External,
1841 Ext_Asgn (Start .. Stop))
1844 ("illegal external assignment '"
1855 Add_File (Arg, Source_Index);
1864 -----------------------
1865 -- Repinfo_File_Name --
1866 -----------------------
1868 function Repinfo_File_Name (Source : File_Name_Type) return String is
1870 return Get_Name_String (Source) & Repinfo_Suffix;
1871 end Repinfo_File_Name;
1873 --------------------
1874 -- Tree_File_Name --
1875 --------------------
1877 function Tree_File_Name (Source : File_Name_Type) return String is
1878 Src : constant String := Get_Name_String (Source);
1881 -- If source name has an extension, then replace it with the tree suffix
1883 for Index in reverse Src'First + 1 .. Src'Last loop
1884 if Src (Index) = '.' then
1885 return Src (Src'First .. Index - 1) & Tree_Suffix;
1889 -- If there is no dot, or if it is the first character, just add the
1892 return Src & Tree_Suffix;
1901 if not Usage_Displayed then
1902 Usage_Displayed := True;
1904 Put_Line ("Usage: gnatclean [switches] {[-innn] name}");
1907 Display_Usage_Version_And_Help;
1909 Put_Line (" names is one or more file names from which " &
1910 "the .adb or .ads suffix may be omitted");
1911 Put_Line (" names may be omitted if -P<project> is specified");
1914 Put_Line (" --subdirs=dir real obj/lib/exec dirs are subdirs");
1915 Put_Line (" " & Makeutl.Unchecked_Shared_Lib_Imports);
1916 Put_Line (" Allow shared libraries to import static libraries");
1919 Put_Line (" -c Only delete compiler generated files");
1920 Put_Line (" -D dir Specify dir as the object library");
1921 Put_Line (" -eL Follow symbolic links when processing " &
1923 Put_Line (" -f Force deletions of unwritable files");
1924 Put_Line (" -F Full project path name " &
1925 "in brief error messages");
1926 Put_Line (" -h Display this message");
1927 Put_Line (" -innn Index of unit in source for following names");
1928 Put_Line (" -n Nothing to do: only list files to delete");
1929 Put_Line (" -Pproj Use GNAT Project File proj");
1930 Put_Line (" -q Be quiet/terse");
1931 Put_Line (" -r Clean all projects recursively");
1932 Put_Line (" -v Verbose mode");
1933 Put_Line (" -vPx Specify verbosity when parsing " &
1934 "GNAT Project Files");
1935 Put_Line (" -Xnm=val Specify an external reference " &
1936 "for GNAT Project Files");
1939 Put_Line (" -aPdir Add directory dir to project search path");
1942 Put_Line (" -aOdir Specify ALI/object files search path");
1943 Put_Line (" -Idir Like -aOdir");
1944 Put_Line (" -I- Don't look for source/library files " &
1945 "in the default directory");