1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2003-2015, 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
;
43 with Switch
; use Switch
;
45 with Targparm
; use Targparm
;
46 with Types
; use Types
;
48 with Ada
.Command_Line
; use Ada
.Command_Line
;
50 with GNAT
.Command_Line
; use GNAT
.Command_Line
;
51 with GNAT
.Directory_Operations
; use GNAT
.Directory_Operations
;
52 with GNAT
.IO
; use GNAT
.IO
;
53 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
57 Initialized
: Boolean := False;
58 -- Set to True by the first call to Initialize to avoid reinitialization
61 -- Suffixes of various files
63 Assembly_Suffix
: constant String := ".s";
64 ALI_Suffix
: constant String := ".ali";
65 Tree_Suffix
: constant String := ".adt";
66 Object_Suffix
: constant String := Get_Target_Object_Suffix
.all;
67 Debug_Suffix
: constant String := ".dg";
68 Repinfo_Suffix
: constant String := ".rep";
69 -- Suffix of representation info files
71 B_Start
: constant String := "b~";
72 -- Prefix of binder generated file, and number of actual characters used
74 Project_Tree
: constant Project_Tree_Ref
:=
75 new Project_Tree_Data
(Is_Root_Tree
=> True);
78 Object_Directory_Path
: String_Access
:= null;
79 -- The path name of the object directory, set with switch -D
81 Force_Deletions
: Boolean := False;
82 -- Set to True by switch -f. When True, attempts to delete non writable
83 -- files will be done.
85 Do_Nothing
: Boolean := False;
86 -- Set to True when switch -n is specified. When True, no file is deleted.
87 -- gnatclean only lists the files that would have been deleted if the
88 -- switch -n had not been specified.
90 File_Deleted
: Boolean := False;
91 -- Set to True if at least one file has been deleted
93 Copyright_Displayed
: Boolean := False;
94 Usage_Displayed
: Boolean := False;
96 Project_File_Name
: String_Access
:= null;
98 Project_Node_Tree
: Project_Node_Tree_Ref
;
100 Main_Project
: Prj
.Project_Id
:= Prj
.No_Project
;
102 All_Projects
: Boolean := False;
104 -- Packages of project files where unknown attributes are errors
106 Naming_String
: aliased String := "naming";
107 Builder_String
: aliased String := "builder";
108 Compiler_String
: aliased String := "compiler";
109 Binder_String
: aliased String := "binder";
110 Linker_String
: aliased String := "linker";
112 Gnatmake_Packages
: aliased String_List
:=
113 (Naming_String
'Access,
114 Builder_String 'Access,
115 Compiler_String
'Access,
116 Binder_String 'Access,
117 Linker_String
'Access);
119 Packages_To_Check_By_Gnatmake : constant String_List_Access :=
120 Gnatmake_Packages'Access;
122 package Processed_Projects is new Table.Table
123 (Table_Component_Type => Project_Id,
124 Table_Index_Type => Natural,
125 Table_Low_Bound => 0,
127 Table_Increment => 100,
128 Table_Name => "Clean.Processed_Projects");
129 -- Table to keep track of what project files have been processed, when
130 -- switch -r is specified.
132 package Sources is new Table.Table
133 (Table_Component_Type => File_Name_Type,
134 Table_Index_Type => Natural,
135 Table_Low_Bound => 0,
137 Table_Increment => 100,
138 Table_Name => "Clean.Processed_Projects");
139 -- Table to store all the source files of a library unit: spec, body and
140 -- subunits, to detect .dg files and delete them.
142 -----------------------------
143 -- Other local subprograms --
144 -----------------------------
146 procedure Add_Source_Dir (N : String);
147 -- Call Add_Src_Search_Dir and output one line when in verbose mode
149 procedure Add_Source_Directories is
150 new Prj.Env.For_All_Source_Dirs (Action => Add_Source_Dir);
152 procedure Add_Object_Dir (N : String);
153 -- Call Add_Lib_Search_Dir and output one line when in verbose mode
155 procedure Add_Object_Directories is
156 new Prj.Env.For_All_Object_Dirs (Action => Add_Object_Dir);
158 function ALI_File_Name (Source : File_Name_Type) return String;
159 -- Returns the name of the ALI file corresponding to Source
161 function Assembly_File_Name (Source : File_Name_Type) return String;
162 -- Returns the assembly file name corresponding to Source
164 procedure Clean_Archive (Project : Project_Id; Global : Boolean);
165 -- Delete a global archive or library project archive and the dependency
166 -- file, if they exist.
168 procedure Clean_Executables;
169 -- Do the cleaning work when no project file is specified
171 procedure Clean_Interface_Copy_Directory (Project : Project_Id);
172 -- Delete files in an interface copy directory: any file that is a copy of
173 -- a source of the project.
175 procedure Clean_Library_Directory (Project : Project_Id);
176 -- Delete the library file in a library directory and any ALI file of a
177 -- source of the project in a library ALI directory.
179 procedure Clean_Project (Project : Project_Id);
180 -- Do the cleaning work when a project file is specified. This procedure
181 -- calls itself recursively when there are several project files in the
182 -- tree rooted at the main project file and switch -r has been specified.
184 function Debug_File_Name (Source : File_Name_Type) return String;
185 -- Name of the expanded source file corresponding to Source
187 procedure Delete (In_Directory : String; File : String);
188 -- Delete one file, or list the file name if switch -n is specified
190 procedure Delete_Binder_Generated_Files
192 Source : File_Name_Type);
193 -- Delete the binder generated file in directory Dir for Source, if they
194 -- exist: for Unix these are b~<source>.ads, b~<source>.adb,
195 -- b~<source>.ali and b~<source>.o.
197 procedure Display_Copyright;
198 -- Display the Copyright notice. If called several times, display the
199 -- Copyright notice only the first time.
201 procedure Initialize;
202 -- Call the necessary package initializations
204 function Object_File_Name (Source : File_Name_Type) return String;
205 -- Returns the object file name corresponding to Source
207 procedure Parse_Cmd_Line;
208 -- Parse the command line
210 function Repinfo_File_Name (Source : File_Name_Type) return String;
211 -- Returns the repinfo file name corresponding to Source
213 function Tree_File_Name (Source : File_Name_Type) return String;
214 -- Returns the tree file name corresponding to Source
216 function In_Extension_Chain
217 (Of_Project : Project_Id;
218 Prj : Project_Id) return Boolean;
219 -- Returns True iff Prj is an extension of Of_Project or if Of_Project is
220 -- an extension of Prj.
223 -- Display the usage. If called several times, the usage is displayed only
230 procedure Add_Object_Dir (N : String) is
232 Add_Lib_Search_Dir (N);
234 if Opt.Verbose_Mode then
235 Put ("Adding object directory """);
246 procedure Add_Source_Dir (N : String) is
248 Add_Src_Search_Dir (N);
250 if Opt.Verbose_Mode then
251 Put ("Adding source directory """);
262 function ALI_File_Name (Source : File_Name_Type) return String is
263 Src : constant String := Get_Name_String (Source);
266 -- If the source name has an extension, then replace it with
269 for Index in reverse Src'First + 1 .. Src'Last loop
270 if Src (Index) = '.' then
271 return Src (Src'First .. Index - 1) & ALI_Suffix;
275 -- If there is no dot, or if it is the first character, just add the
278 return Src & ALI_Suffix;
281 ------------------------
282 -- Assembly_File_Name --
283 ------------------------
285 function Assembly_File_Name (Source : File_Name_Type) return String is
286 Src : constant String := Get_Name_String (Source);
289 -- If the source name has an extension, then replace it with
290 -- the assembly suffix.
292 for Index in reverse Src'First + 1 .. Src'Last loop
293 if Src (Index) = '.' then
294 return Src (Src'First .. Index - 1) & Assembly_Suffix;
298 -- If there is no dot, or if it is the first character, just add the
301 return Src & Assembly_Suffix;
302 end Assembly_File_Name;
308 procedure Clean_Archive (Project : Project_Id; Global : Boolean) is
309 Current_Dir : constant Dir_Name_Str := Get_Current_Dir;
311 Lib_Prefix : String_Access;
312 Archive_Name : String_Access;
313 -- The name of the archive file for this project
315 Archive_Dep_Name : String_Access;
316 -- The name of the archive dependency file for this project
318 Obj_Dir : constant String :=
319 Get_Name_String (Project.Object_Directory.Display_Name);
322 Change_Dir (Obj_Dir);
324 -- First, get the lib prefix, the archive file name and the archive
325 -- dependency file name.
329 new String'("lib" & Get_Name_String
(Project
.Display_Name
));
332 new String'("lib" & Get_Name_String (Project.Library_Name));
335 Archive_Name := new String'(Lib_Prefix
.all & '.' & Archive_Ext
);
336 Archive_Dep_Name
:= new String'(Lib_Prefix.all & ".deps");
338 -- Delete the archive file and the archive dependency file, if they
341 if Is_Regular_File (Archive_Name.all) then
342 Delete (Obj_Dir, Archive_Name.all);
345 if Is_Regular_File (Archive_Dep_Name.all) then
346 Delete (Obj_Dir, Archive_Dep_Name.all);
349 Change_Dir (Current_Dir);
352 -----------------------
353 -- Clean_Executables --
354 -----------------------
356 procedure Clean_Executables is
357 Main_Source_File : File_Name_Type;
358 -- Current main source
360 Main_Lib_File : File_Name_Type;
361 -- ALI file of the current main
363 Lib_File : File_Name_Type;
366 Full_Lib_File : File_Name_Type;
367 -- Full name of the current ALI file
369 Text : Text_Buffer_Ptr;
372 Source : Queue.Source_Info;
375 Queue.Initialize (Queue_Per_Obj_Dir => False);
377 -- It does not really matter if there is or not an object file
378 -- corresponding to an ALI file: if there is one, it will be deleted.
380 Opt.Check_Object_Consistency := False;
382 -- Proceed each executable one by one. Each source is marked as it is
383 -- processed, so common sources between executables will not be
384 -- processed several times.
386 for N_File in 1 .. Osint.Number_Of_Files loop
387 Main_Source_File := Next_Main_Source;
389 Osint.Lib_File_Name (Main_Source_File, Current_File_Index);
391 if Main_Lib_File /= No_File then
393 ((Format => Format_Gnatmake,
394 File => Main_Lib_File,
395 Unit => No_Unit_Name,
397 Project => No_Project,
401 while not Queue.Is_Empty loop
402 Sources.Set_Last (0);
403 Queue.Extract (Found, Source);
404 pragma Assert (Found);
405 pragma Assert (Source.File /= No_File);
406 Lib_File := Source.File;
407 Full_Lib_File := Osint.Full_Lib_File_Name (Lib_File);
409 -- If we have existing ALI file that is not read-only, process it
411 if Full_Lib_File /= No_File
412 and then not Is_Readonly_Library (Full_Lib_File)
414 Text := Read_Library_Info (Lib_File);
418 Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
421 -- If no error was produced while loading this ALI file,
422 -- insert into the queue all the unmarked withed sources.
424 if The_ALI /= No_ALI_Id then
425 for J in ALIs.Table (The_ALI).First_Unit ..
426 ALIs.Table (The_ALI).Last_Unit
428 Sources.Increment_Last;
429 Sources.Table (Sources.Last) :=
430 ALI.Units.Table (J).Sfile;
432 for K in ALI.Units.Table (J).First_With ..
433 ALI.Units.Table (J).Last_With
435 if Withs.Table (K).Afile /= No_File then
437 ((Format => Format_Gnatmake,
438 File => Withs.Table (K).Afile,
439 Unit => No_Unit_Name,
441 Project => No_Project,
447 -- Look for subunits and put them in the Sources table
449 for J in ALIs.Table (The_ALI).First_Sdep ..
450 ALIs.Table (The_ALI).Last_Sdep
452 if Sdep.Table (J).Subunit_Name /= No_Name then
453 Sources.Increment_Last;
454 Sources.Table (Sources.Last) :=
455 Sdep.Table (J).Sfile;
461 -- Now delete all existing files corresponding to this ALI file
464 Obj_Dir : constant String :=
465 Dir_Name (Get_Name_String (Full_Lib_File));
466 Obj : constant String := Object_File_Name (Lib_File);
467 Adt : constant String := Tree_File_Name (Lib_File);
468 Asm : constant String := Assembly_File_Name (Lib_File);
471 Delete (Obj_Dir, Get_Name_String (Lib_File));
473 if Is_Regular_File (Obj_Dir & Dir_Separator & Obj) then
474 Delete (Obj_Dir, Obj);
477 if Is_Regular_File (Obj_Dir & Dir_Separator & Adt) then
478 Delete (Obj_Dir, Adt);
481 if Is_Regular_File (Obj_Dir & Dir_Separator & Asm) then
482 Delete (Obj_Dir, Asm);
485 -- Delete expanded source files (.dg) and/or repinfo files
488 for J in 1 .. Sources.Last loop
490 Deb : constant String :=
491 Debug_File_Name (Sources.Table (J));
492 Rep : constant String :=
493 Repinfo_File_Name (Sources.Table (J));
496 if Is_Regular_File (Obj_Dir & Dir_Separator & Deb) then
497 Delete (Obj_Dir, Deb);
500 if Is_Regular_File (Obj_Dir & Dir_Separator & Rep) then
501 Delete (Obj_Dir, Rep);
509 -- Delete the executable, if it exists, and the binder generated
512 if not Compile_Only then
514 Source : constant File_Name_Type :=
515 Strip_Suffix (Main_Lib_File);
516 Executable : constant String :=
517 Get_Name_String (Executable_Name (Source));
519 if Is_Regular_File (Executable) then
520 Delete ("", Executable);
523 Delete_Binder_Generated_Files (Get_Current_Dir, Source);
527 end Clean_Executables;
529 ------------------------------------
530 -- Clean_Interface_Copy_Directory --
531 ------------------------------------
533 procedure Clean_Interface_Copy_Directory (Project : Project_Id) is
534 Current : constant String := Get_Current_Dir;
538 Name : String (1 .. 200);
541 Delete_File : Boolean;
546 and then Project.Library_Src_Dir /= No_Path_Information
549 Directory : constant String :=
550 Get_Name_String (Project.Library_Src_Dir.Display_Name);
553 Change_Dir (Directory);
556 -- For each regular file in the directory, if switch -n has not
557 -- been specified, make it writable and delete the file if it is
558 -- a copy of a source of the project.
561 Read (Direc, Name, Last);
565 Filename : constant String := Name (1 .. Last);
568 if Is_Regular_File (Filename) then
569 Canonical_Case_File_Name (Name (1 .. Last));
570 Delete_File := False;
572 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
574 -- Compare with source file names of the project
576 while Unit /= No_Unit_Index loop
577 if Unit.File_Names (Impl) /= null
578 and then Ultimate_Extending_Project_Of
579 (Unit.File_Names (Impl).Project) = Project
581 Get_Name_String (Unit.File_Names (Impl).File) =
588 if Unit.File_Names (Spec) /= null
589 and then Ultimate_Extending_Project_Of
590 (Unit.File_Names (Spec).Project) = Project
593 (Unit.File_Names (Spec).File) = Name (1 .. Last)
599 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
603 if not Do_Nothing then
604 Set_Writable (Filename);
607 Delete (Directory, Filename);
615 -- Restore the initial working directory
617 Change_Dir (Current);
620 end Clean_Interface_Copy_Directory;
622 -----------------------------
623 -- Clean_Library_Directory --
624 -----------------------------
626 Empty_String : aliased String := "";
628 procedure Clean_Library_Directory (Project : Project_Id) is
629 Current : constant String := Get_Current_Dir;
631 Lib_Filename : constant String := Get_Name_String (Project.Library_Name);
633 DLL_Prefix & Lib_Filename & "." & DLL_Ext;
634 Archive_Name : String :=
635 "lib" & Lib_Filename & "." & Archive_Ext;
638 Name : String (1 .. 200);
641 Delete_File : Boolean;
643 Minor : String_Access := Empty_String'Access;
644 Major : String_Access := Empty_String'Access;
647 if Project.Library then
648 if Project.Library_Kind /= Static
649 and then MLib.Tgt.Library_Major_Minor_Id_Supported
650 and then Project.Lib_Internal_Name /= No_Name
652 Minor := new String'(Get_Name_String
(Project
.Lib_Internal_Name
));
653 Major
:= new String'(MLib.Major_Id_Name (DLL_Name, Minor.all));
657 Lib_Directory : constant String :=
658 Get_Name_String (Project.Library_Dir.Display_Name);
659 Lib_ALI_Directory : constant String :=
660 Get_Name_String (Project.Library_ALI_Dir.Display_Name);
663 Canonical_Case_File_Name (Archive_Name);
664 Canonical_Case_File_Name (DLL_Name);
666 if Is_Directory (Lib_Directory) then
667 Change_Dir (Lib_Directory);
670 -- For each regular file in the directory, if switch -n has not
671 -- not been specified, make it writable and delete the file if
672 -- it is the library file.
675 Read (Direc, Name, Last);
679 Filename : constant String := Name (1 .. Last);
682 if Is_Regular_File (Filename)
683 or else Is_Symbolic_Link (Filename)
685 Canonical_Case_File_Name (Name (1 .. Last));
686 Delete_File := False;
688 if (Project.Library_Kind = Static
689 and then Name (1 .. Last) = Archive_Name)
691 ((Project.Library_Kind = Dynamic
693 Project.Library_Kind = Relocatable)
695 (Name (1 .. Last) = DLL_Name
697 Name (1 .. Last) = Minor.all
699 Name (1 .. Last) = Major.all))
701 if not Do_Nothing then
702 Set_Writable (Filename);
705 Delete (Lib_Directory, Filename);
714 if not Is_Directory (Lib_ALI_Directory) then
715 -- Nothing more to do, return now
719 Change_Dir (Lib_ALI_Directory);
722 -- For each regular file in the directory, if switch -n has not
723 -- been specified, make it writable and delete the file if it is
724 -- any ALI file of a source of the project.
727 Read (Direc, Name, Last);
731 Filename : constant String := Name (1 .. Last);
733 if Is_Regular_File (Filename) then
734 Canonical_Case_File_Name (Name (1 .. Last));
735 Delete_File := False;
737 if Last > 4 and then Name (Last - 3 .. Last) = ".ali" then
742 -- Compare with ALI file names of the project
745 Units_Htable.Get_First (Project_Tree.Units_HT);
746 while Unit /= No_Unit_Index loop
747 if Unit.File_Names (Impl) /= null
748 and then Unit.File_Names (Impl).Project /=
751 if Ultimate_Extending_Project_Of
752 (Unit.File_Names (Impl).Project) =
756 (Unit.File_Names (Impl).File);
760 (Name (1 .. Name_Len))'Length;
761 if Name_Buffer (1 .. Name_Len) =
769 elsif Unit.File_Names (Spec) /= null
770 and then Ultimate_Extending_Project_Of
771 (Unit.File_Names (Spec).Project) =
774 Get_Name_String (Unit.File_Names (Spec).File);
778 (Name (1 .. Name_Len))'Length;
780 if Name_Buffer (1 .. Name_Len) =
789 Units_Htable.Get_Next (Project_Tree.Units_HT);
795 if not Do_Nothing then
796 Set_Writable (Filename);
799 Delete (Lib_ALI_Directory, Filename);
807 -- Restore the initial working directory
809 Change_Dir (Current);
812 end Clean_Library_Directory;
818 procedure Clean_Project (Project : Project_Id) is
819 Main_Source_File : File_Name_Type;
820 -- Name of executable on the command line without directory info
822 Executable : File_Name_Type;
823 -- Name of the executable file
825 Current_Dir : constant Dir_Name_Str := Get_Current_Dir;
827 File_Name1 : File_Name_Type;
829 File_Name2 : File_Name_Type;
831 Lib_File : File_Name_Type;
833 Global_Archive : Boolean := False;
836 -- Check that we don't specify executable on the command line for
837 -- a main library project.
839 if Project = Main_Project
840 and then Osint.Number_Of_Files /= 0
841 and then Project.Library
844 ("Cannot specify executable(s) for a Library Project File");
847 -- Nothing to clean in an externally built project
849 if Project.Externally_Built then
851 Put ("Nothing to do to clean externally built project """);
852 Put (Get_Name_String (Project.Name));
858 Put ("Cleaning project """);
859 Put (Get_Name_String (Project.Name));
863 -- Add project to the list of processed projects
865 Processed_Projects.Increment_Last;
866 Processed_Projects.Table (Processed_Projects.Last) := Project;
868 if Project.Object_Directory /= No_Path_Information
869 and then Is_Directory
870 (Get_Name_String (Project.Object_Directory.Display_Name))
873 Obj_Dir : constant String :=
874 Get_Name_String (Project.Object_Directory.Display_Name);
877 Change_Dir (Obj_Dir);
879 -- First, deal with Ada
881 -- Look through the units to find those that are either
882 -- immediate sources or inherited sources of the project.
883 -- Extending projects may have no language specified, if
884 -- Source_Dirs or Source_Files is specified as an empty list,
885 -- so always look for Ada units in extending projects.
887 if Has_Ada_Sources (Project)
888 or else Project.Extends /= No_Project
890 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
891 while Unit /= No_Unit_Index loop
892 File_Name1 := No_File;
893 File_Name2 := No_File;
895 -- If either the spec or the body is a source of the
896 -- project, check for the corresponding ALI file in the
899 if (Unit.File_Names (Impl) /= null
902 (Unit.File_Names (Impl).Project, Project))
904 (Unit.File_Names (Spec) /= null
907 (Unit.File_Names (Spec).Project, Project))
909 if Unit.File_Names (Impl) /= null then
910 File_Name1 := Unit.File_Names (Impl).File;
911 Index1 := Unit.File_Names (Impl).Index;
913 File_Name1 := No_File;
917 if Unit.File_Names (Spec) /= null then
918 File_Name2 := Unit.File_Names (Spec).File;
919 Index2 := Unit.File_Names (Spec).Index;
921 File_Name2 := No_File;
925 -- If there is no body file name, then there may be
928 if File_Name1 = No_File then
929 File_Name1 := File_Name2;
931 File_Name2 := No_File;
936 -- If there is either a spec or a body, look for files
937 -- in the object directory.
939 if File_Name1 /= No_File then
940 Lib_File := Osint.Lib_File_Name (File_Name1, Index1);
943 Asm : constant String :=
944 Assembly_File_Name (Lib_File);
945 ALI : constant String :=
946 ALI_File_Name (Lib_File);
947 Obj : constant String :=
948 Object_File_Name (Lib_File);
949 Adt : constant String :=
950 Tree_File_Name (Lib_File);
951 Deb : constant String :=
952 Debug_File_Name (File_Name1);
953 Rep : constant String :=
954 Repinfo_File_Name (File_Name1);
955 Del : Boolean := True;
958 -- If the ALI file exists and is read-only, no file
961 if Is_Regular_File (ALI) then
962 if Is_Writable_File (ALI) then
963 Delete (Obj_Dir, ALI);
972 if Obj_Dir (Obj_Dir'Last) /=
979 Put_Line (""" is read
-only
");
988 if Is_Regular_File (Obj) then
989 Delete (Obj_Dir, Obj);
994 if Is_Regular_File (Asm) then
995 Delete (Obj_Dir, Asm);
1000 if Is_Regular_File (Adt) then
1001 Delete (Obj_Dir, Adt);
1004 -- First expanded source file
1006 if Is_Regular_File (Deb) then
1007 Delete (Obj_Dir, Deb);
1012 if Is_Regular_File (Rep) then
1013 Delete (Obj_Dir, Rep);
1016 -- Second expanded source file
1018 if File_Name2 /= No_File then
1020 Deb : constant String :=
1021 Debug_File_Name (File_Name2);
1022 Rep : constant String :=
1023 Repinfo_File_Name (File_Name2);
1026 if Is_Regular_File (Deb) then
1027 Delete (Obj_Dir, Deb);
1030 if Is_Regular_File (Rep) then
1031 Delete (Obj_Dir, Rep);
1039 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
1043 -- Check if a global archive and it dependency file could have
1044 -- been created and, if they exist, delete them.
1046 if Project = Main_Project and then not Project.Library then
1047 Global_Archive := False;
1050 Proj : Project_List;
1053 Proj := Project_Tree.Projects;
1054 while Proj /= null loop
1056 -- For gnatmake, when the project specifies more than
1057 -- just Ada as a language (even if course we could not
1058 -- find any source file for the other languages), we
1059 -- will take all the object files found in the object
1060 -- directories. Since we know the project supports at
1061 -- least Ada, we just have to test whether it has at
1062 -- least two languages, and we do not care about the
1065 if Proj.Project.Languages /= null
1066 and then Proj.Project.Languages.Next /= null
1068 Global_Archive := True;
1076 if Global_Archive then
1077 Clean_Archive (Project, Global => True);
1084 -- If this is a library project, clean the library directory, the
1085 -- interface copy dir and, for a Stand-Alone Library, the binder
1086 -- generated files of the library.
1088 -- The directories are cleaned only if switch -c is not specified
1090 if Project.Library then
1091 if not Compile_Only then
1092 Clean_Library_Directory (Project);
1094 if Project.Library_Src_Dir /= No_Path_Information then
1095 Clean_Interface_Copy_Directory (Project);
1099 if Project.Standalone_Library /= No
1100 and then Project.Object_Directory /= No_Path_Information
1102 Delete_Binder_Generated_Files
1103 (Get_Name_String (Project.Object_Directory.Display_Name),
1104 File_Name_Type (Project.Library_Name));
1108 if Verbose_Mode then
1113 -- If switch -r is specified, call Clean_Project recursively for the
1114 -- imported projects and the project being extended.
1116 if All_Projects then
1118 Imported : Project_List;
1122 -- For each imported project, call Clean_Project if the project
1123 -- has not been processed already.
1125 Imported := Project.Imported_Projects;
1126 while Imported /= null loop
1130 J in Processed_Projects.First .. Processed_Projects.Last
1132 if Imported.Project = Processed_Projects.Table (J) then
1139 Clean_Project (Imported.Project);
1142 Imported := Imported.Next;
1145 -- If this project extends another project, call Clean_Project for
1146 -- the project being extended. It is guaranteed that it has not
1147 -- called before, because no other project may import or extend
1150 if Project.Extends /= No_Project then
1151 Clean_Project (Project.Extends);
1156 -- For the main project, delete the executables and the binder
1159 -- The executables are deleted only if switch -c is not specified
1161 if Project = Main_Project
1162 and then Project.Exec_Directory /= No_Path_Information
1165 Exec_Dir : constant String :=
1166 Get_Name_String (Project.Exec_Directory.Display_Name);
1169 Change_Dir (Exec_Dir);
1171 for N_File in 1 .. Osint.Number_Of_Files loop
1172 Main_Source_File := Next_Main_Source;
1174 if not Compile_Only then
1178 Project_Tree.Shared,
1180 Current_File_Index);
1183 Exec_File_Name : constant String :=
1184 Get_Name_String (Executable);
1187 if Is_Absolute_Path (Name => Exec_File_Name) then
1188 if Is_Regular_File (Exec_File_Name) then
1189 Delete ("", Exec_File_Name);
1193 if Is_Regular_File (Exec_File_Name) then
1194 Delete (Exec_Dir, Exec_File_Name);
1200 if Project.Object_Directory /= No_Path_Information
1203 (Get_Name_String (Project.Object_Directory.Display_Name))
1205 Delete_Binder_Generated_Files
1206 (Get_Name_String (Project.Object_Directory.Display_Name),
1207 Strip_Suffix (Main_Source_File));
1213 -- Change back to previous directory
1215 Change_Dir (Current_Dir);
1218 ---------------------
1219 -- Debug_File_Name --
1220 ---------------------
1222 function Debug_File_Name (Source : File_Name_Type) return String is
1224 return Get_Name_String (Source) & Debug_Suffix;
1225 end Debug_File_Name;
1231 procedure Delete (In_Directory : String; File : String) is
1232 Full_Name : String (1 .. In_Directory'Length + File'Length + 1);
1233 Last : Natural := 0;
1237 -- Indicate that at least one file is deleted or is to be deleted
1239 File_Deleted := True;
1241 -- Build the path name of the file to delete
1243 Last := In_Directory'Length;
1244 Full_Name (1 .. Last) := In_Directory;
1246 if Last > 0 and then Full_Name (Last) /= Directory_Separator then
1248 Full_Name (Last) := Directory_Separator;
1251 Full_Name (Last + 1 .. Last + File'Length) := File;
1252 Last := Last + File'Length;
1254 -- If switch -n was used, simply output the path name
1257 Put_Line (Full_Name (1 .. Last));
1259 -- Otherwise, delete the file if it is writable
1263 or else Is_Writable_File (Full_Name (1 .. Last))
1264 or else Is_Symbolic_Link (Full_Name (1 .. Last))
1266 Delete_File (Full_Name (1 .. Last), Success);
1268 -- Here if no deletion required
1274 if Verbose_Mode or else not Quiet_Output then
1276 Put ("Warning
: """);
1277 Put (Full_Name (1 .. Last));
1278 Put_Line (""" could
not be deleted
");
1282 Put (Full_Name (1 .. Last));
1283 Put_Line (""" has been deleted
");
1289 -----------------------------------
1290 -- Delete_Binder_Generated_Files --
1291 -----------------------------------
1293 procedure Delete_Binder_Generated_Files
1295 Source : File_Name_Type)
1297 Source_Name : constant String := Get_Name_String (Source);
1298 Current : constant String := Get_Current_Dir;
1299 Last : constant Positive := B_Start'Length + Source_Name'Length;
1300 File_Name : String (1 .. Last + 4);
1305 -- Build the file name (before the extension)
1307 File_Name (1 .. B_Start'Length) := B_Start;
1308 File_Name (B_Start'Length + 1 .. Last) := Source_Name;
1312 File_Name (Last + 1 .. Last + 4) := ".ads
";
1314 if Is_Regular_File (File_Name (1 .. Last + 4)) then
1315 Delete (Dir, File_Name (1 .. Last + 4));
1320 File_Name (Last + 1 .. Last + 4) := ".adb
";
1322 if Is_Regular_File (File_Name (1 .. Last + 4)) then
1323 Delete (Dir, File_Name (1 .. Last + 4));
1328 File_Name (Last + 1 .. Last + 4) := ".ali
";
1330 if Is_Regular_File (File_Name (1 .. Last + 4)) then
1331 Delete (Dir, File_Name (1 .. Last + 4));
1336 File_Name (Last + 1 .. Last + Object_Suffix'Length) := Object_Suffix;
1338 if Is_Regular_File (File_Name (1 .. Last + Object_Suffix'Length)) then
1339 Delete (Dir, File_Name (1 .. Last + Object_Suffix'Length));
1342 -- Change back to previous directory
1344 Change_Dir (Current);
1345 end Delete_Binder_Generated_Files;
1347 -----------------------
1348 -- Display_Copyright --
1349 -----------------------
1351 procedure Display_Copyright is
1353 if not Copyright_Displayed then
1354 Copyright_Displayed := True;
1355 Display_Version ("GNATCLEAN
", "2003");
1357 end Display_Copyright;
1363 procedure Gnatclean is
1365 -- Do the necessary initializations
1369 -- Parse the command line, getting the switches and the executable names
1373 -- Add the default project search directories now, after the directories
1374 -- that have been specified by switches -aP<dir>.
1376 Prj.Env.Initialize_Default_Project_Path
1377 (Root_Environment.Project_Path,
1378 Target_Name => Sdefault.Target_Name.all);
1380 if Verbose_Mode then
1384 if Project_File_Name /= null then
1386 -- Warn about 'gnatclean -P'
1388 if Project_File_Name /= null then
1390 ("warning
: gnatclean
-P
is obsolete
and will
not be available
" &
1391 " in the next release
; use gprclean instead
.");
1394 -- A project file was specified by a -P switch
1396 if Opt.Verbose_Mode then
1398 Put ("Parsing Project File
""");
1399 Put (Project_File_Name.all);
1404 -- Set the project parsing verbosity to whatever was specified
1405 -- by a possible -vP switch.
1407 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
1409 -- Parse the project file. If there is an error, Main_Project
1410 -- will still be No_Project.
1413 (Project => Main_Project,
1414 In_Tree => Project_Tree,
1415 In_Node_Tree => Project_Node_Tree,
1416 Project_File_Name => Project_File_Name.all,
1417 Env => Root_Environment,
1418 Packages_To_Check => Packages_To_Check_By_Gnatmake);
1420 if Main_Project = No_Project then
1421 Fail ("""" & Project_File_Name.all & """ processing failed
");
1423 elsif Main_Project.Qualifier = Aggregate then
1424 Fail ("aggregate projects are
not supported
");
1426 elsif Aggregate_Libraries_In (Project_Tree) then
1427 Fail ("aggregate library projects are
not supported
");
1430 if Opt.Verbose_Mode then
1432 Put ("Parsing
of Project File
""");
1433 Put (Project_File_Name.all);
1434 Put (""" is finished
.");
1438 -- Add source directories and object directories to the search paths
1440 Add_Source_Directories (Main_Project, Project_Tree);
1441 Add_Object_Directories (Main_Project, Project_Tree);
1444 Osint.Add_Default_Search_Dirs;
1446 -- If a project file was specified, but no executable name, put all
1447 -- the mains of the project file (if any) as if there were on the
1450 if Main_Project /= No_Project and then Osint.Number_Of_Files = 0 then
1452 Main : String_Element;
1453 Value : String_List_Id := Main_Project.Mains;
1455 while Value /= Prj.Nil_String loop
1456 Main := Project_Tree.Shared.String_Elements.Table (Value);
1458 (File_Name => Get_Name_String (Main.Value),
1459 Index => Main.Index);
1465 -- If neither a project file nor an executable were specified, exit
1466 -- displaying the usage if there were no arguments on the command line.
1468 if Main_Project = No_Project and then Osint.Number_Of_Files = 0 then
1469 if Argument_Count = 0 then
1478 if Verbose_Mode then
1482 if Main_Project /= No_Project then
1484 -- If a project file has been specified, call Clean_Project with the
1485 -- project id of this project file, after resetting the list of
1486 -- processed projects.
1488 Processed_Projects.Init;
1489 Clean_Project (Main_Project);
1492 -- If no project file has been specified, the work is done in
1493 -- Clean_Executables.
1498 -- In verbose mode, if Delete has not been called, indicate that no file
1499 -- needs to be deleted.
1501 if Verbose_Mode and (not File_Deleted) then
1505 Put_Line ("No file needs to be deleted
");
1507 Put_Line ("No file has been deleted
");
1512 ------------------------
1513 -- In_Extension_Chain --
1514 ------------------------
1516 function In_Extension_Chain
1517 (Of_Project : Project_Id;
1518 Prj : Project_Id) return Boolean
1523 if Prj = No_Project or else Of_Project = No_Project then
1527 if Of_Project = Prj then
1532 while Proj.Extends /= No_Project loop
1533 if Proj.Extends = Prj then
1537 Proj := Proj.Extends;
1541 while Proj.Extends /= No_Project loop
1542 if Proj.Extends = Of_Project then
1546 Proj := Proj.Extends;
1550 end In_Extension_Chain;
1556 procedure Initialize is
1558 if not Initialized then
1559 Initialized := True;
1561 -- Get default search directories to locate system.ads when calling
1562 -- Targparm.Get_Target_Parameters.
1564 Osint.Add_Default_Search_Dirs;
1566 -- Initialize some packages
1572 Prj.Tree.Initialize (Root_Environment, Gnatmake_Flags);
1574 Project_Node_Tree := new Project_Node_Tree_Data;
1575 Prj.Tree.Initialize (Project_Node_Tree);
1577 Prj.Initialize (Project_Tree);
1578 Targparm.Get_Target_Parameters;
1581 -- Reset global variables
1583 Free (Object_Directory_Path);
1584 Do_Nothing := False;
1585 File_Deleted := False;
1586 Copyright_Displayed := False;
1587 Usage_Displayed := False;
1588 Free (Project_File_Name);
1589 Main_Project := Prj.No_Project;
1590 All_Projects := False;
1593 ----------------------
1594 -- Object_File_Name --
1595 ----------------------
1597 function Object_File_Name (Source : File_Name_Type) return String is
1598 Src : constant String := Get_Name_String (Source);
1601 -- If the source name has an extension, then replace it with
1602 -- the Object suffix.
1604 for Index in reverse Src'First + 1 .. Src'Last loop
1605 if Src (Index) = '.' then
1606 return Src (Src'First .. Index - 1) & Object_Suffix;
1610 -- If there is no dot, or if it is the first character, just add the
1613 return Src & Object_Suffix;
1614 end Object_File_Name;
1616 --------------------
1617 -- Parse_Cmd_Line --
1618 --------------------
1620 procedure Parse_Cmd_Line is
1621 Last : constant Natural := Argument_Count;
1623 Source_Index : Int := 0;
1625 procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
1628 -- First, check for --version and --help
1630 Check_Version_And_Help ("GNATCLEAN
", "2003");
1632 -- First, check for switch -P and, if found and gprclean is available,
1633 -- silently invoke gprclean, with switch --target if not on a native
1637 Arg_Len : Positive := Argument_Count;
1638 Call_Gprclean : Boolean := False;
1639 Gprclean : String_Access := null;
1642 Target : String_Access := null;
1648 and then Name_Buffer (Name_Len - 8 .. Name_Len) = "gnatclean
"
1650 if Name_Len > 9 then
1651 Target := new String'(Name_Buffer (1 .. Name_Len - 10));
1652 Arg_Len := Arg_Len + 1;
1655 for J in 1 .. Argument_Count loop
1657 Arg : constant String := Argument (J);
1660 and then Arg (Arg'First .. Arg'First + 1) = "-P
"
1662 Call_Gprclean := True;
1668 if Call_Gprclean then
1669 Gprclean := Locate_Exec_On_Path (Exec_Name => "gprclean
");
1671 if Gprclean /= null then
1673 Args : Argument_List (1 .. Arg_Len);
1675 if Target /= null then
1676 Args (1) := new String'("--target=" & Target.all);
1680 for J
in 1 .. Argument_Count
loop
1682 Args
(Pos
) := new String'(Argument (J));
1685 Spawn (Gprclean.all, Args, Success);
1690 Exit_Program (E_Success);
1699 while Index <= Last loop
1701 Arg : constant String := Argument (Index);
1703 procedure Bad_Argument;
1704 -- Signal bad argument
1710 procedure Bad_Argument is
1712 Fail ("invalid argument """ & Arg & """");
1716 if Arg'Length /= 0 then
1717 if Arg (1) = '-' then
1718 if Arg'Length = 1 then
1724 if Arg'Length > Subdirs_Option'Length
1726 Arg (1 .. Subdirs_Option'Length) = Subdirs_Option
1730 (Arg
(Subdirs_Option
'Length + 1 .. Arg
'Last));
1732 elsif Arg
= Makeutl
.Unchecked_Shared_Lib_Imports
then
1733 Opt
.Unchecked_Shared_Lib_Imports
:= True;
1740 if Arg
'Length < 4 then
1744 if Arg
(3) = 'O' then
1745 Add_Lib_Search_Dir
(Arg
(4 .. Arg
'Last));
1747 elsif Arg
(3) = 'P' then
1748 Prj
.Env
.Add_Directories
1749 (Root_Environment
.Project_Path
,
1750 Arg
(4 .. Arg
'Last));
1757 Compile_Only
:= True;
1760 if Object_Directory_Path
/= null then
1761 Fail
("duplicate -D switch");
1763 elsif Project_File_Name
/= null then
1764 Fail
("-P and -D cannot be used simultaneously");
1767 if Arg
'Length > 2 then
1769 Dir
: constant String := Arg
(3 .. Arg
'Last);
1771 if not Is_Directory
(Dir
) then
1772 Fail
(Dir
& " is not a directory");
1774 Add_Lib_Search_Dir
(Dir
);
1779 if Index
= Last
then
1780 Fail
("no directory specified after -D");
1786 Dir
: constant String := Argument
(Index
);
1788 if not Is_Directory
(Dir
) then
1789 Fail
(Dir
& " is not a directory");
1791 Add_Lib_Search_Dir
(Dir
);
1798 Follow_Links_For_Files
:= True;
1799 Follow_Links_For_Dirs
:= True;
1806 Force_Deletions
:= True;
1807 Directories_Must_Exist_In_Projects
:= False;
1810 Full_Path_Name_For_Brief_Errors
:= True;
1816 if Arg
'Length = 2 then
1822 for J
in 3 .. Arg
'Last loop
1823 if Arg
(J
) not in '0' .. '9' then
1828 (20 * Source_Index
) +
1829 (Character'Pos (Arg
(J
)) - Character'Pos ('0'));
1834 Opt
.Look_In_Primary_Dir
:= False;
1837 if Arg
'Length = 2 then
1841 Add_Lib_Search_Dir
(Arg
(3 .. Arg
'Last));
1848 if Project_File_Name
/= null then
1849 Fail
("multiple -P switches");
1851 elsif Object_Directory_Path
/= null then
1852 Fail
("-D and -P cannot be used simultaneously");
1856 if Arg
'Length > 2 then
1858 Prj
: constant String := Arg
(3 .. Arg
'Last);
1861 and then Prj
(Prj
'First) = '='
1863 Project_File_Name
:=
1865 (Prj (Prj'First + 1 .. Prj'Last));
1867 Project_File_Name := new String'(Prj
);
1872 if Index
= Last
then
1873 Fail
("no project specified after -P");
1877 Project_File_Name
:= new String'(Argument (Index));
1881 Quiet_Output := True;
1884 All_Projects := True;
1888 Verbose_Mode := True;
1890 elsif Arg = "-vP0" then
1891 Current_Verbosity := Prj.Default;
1893 elsif Arg = "-vP1" then
1894 Current_Verbosity := Prj.Medium;
1896 elsif Arg = "-vP2" then
1897 Current_Verbosity := Prj.High;
1904 if Arg'Length = 2 then
1909 Ext_Asgn : constant String := Arg (3 .. Arg'Last);
1910 Start : Positive := Ext_Asgn'First;
1911 Stop : Natural := Ext_Asgn'Last;
1912 OK : Boolean := True;
1915 if Ext_Asgn (Start) = '"' then
1916 if Ext_Asgn (Stop) = '"' then
1927 Prj.Ext.Check (Root_Environment.External,
1928 Ext_Asgn (Start .. Stop))
1931 ("illegal external assignment '"
1942 Add_File (Arg, Source_Index);
1951 -----------------------
1952 -- Repinfo_File_Name --
1953 -----------------------
1955 function Repinfo_File_Name (Source : File_Name_Type) return String is
1957 return Get_Name_String (Source) & Repinfo_Suffix;
1958 end Repinfo_File_Name;
1960 --------------------
1961 -- Tree_File_Name --
1962 --------------------
1964 function Tree_File_Name (Source : File_Name_Type) return String is
1965 Src : constant String := Get_Name_String (Source);
1968 -- If source name has an extension, then replace it with the tree suffix
1970 for Index in reverse Src'First + 1 .. Src'Last loop
1971 if Src (Index) = '.' then
1972 return Src (Src'First .. Index - 1) & Tree_Suffix;
1976 -- If there is no dot, or if it is the first character, just add the
1979 return Src & Tree_Suffix;
1988 if not Usage_Displayed then
1989 Usage_Displayed := True;
1991 Put_Line ("Usage: gnatclean [switches] {[-innn] name}");
1994 Display_Usage_Version_And_Help;
1996 Put_Line (" names is one or more file names from which " &
1997 "the .adb or .ads suffix may be omitted");
1998 Put_Line (" names may be omitted if -P<project> is specified");
2001 Put_Line (" --subdirs=dir real obj/lib/exec dirs are subdirs");
2002 Put_Line (" " & Makeutl.Unchecked_Shared_Lib_Imports);
2003 Put_Line (" Allow shared libraries to import static libraries");
2006 Put_Line (" -c Only delete compiler generated files");
2007 Put_Line (" -D dir Specify dir as the object library");
2008 Put_Line (" -eL Follow symbolic links when processing " &
2010 Put_Line (" -f Force deletions of unwritable files");
2011 Put_Line (" -F Full project path name " &
2012 "in brief error messages");
2013 Put_Line (" -h Display this message");
2014 Put_Line (" -innn Index of unit in source for following names");
2015 Put_Line (" -n Nothing to do: only list files to delete");
2016 Put_Line (" -Pproj Use GNAT Project File proj");
2017 Put_Line (" -q Be quiet/terse");
2018 Put_Line (" -r Clean all projects recursively");
2019 Put_Line (" -v Verbose mode");
2020 Put_Line (" -vPx Specify verbosity when parsing " &
2021 "GNAT Project Files");
2022 Put_Line (" -Xnm=val Specify an external reference " &
2023 "for GNAT Project Files");
2026 Put_Line (" -aPdir Add directory dir to project search path");
2029 Put_Line (" -aOdir Specify ALI/object files search path");
2030 Put_Line (" -Idir Like -aOdir");
2031 Put_Line (" -I- Don't look for source/library files " &
2032 "in the default directory");