1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2003-2013, 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
1257 Host_Full_Name : constant String_Access :=
1258 To_Host_File_Spec (Full_Name (1 .. Last));
1260 if Host_Full_Name = null
1261 or else Host_Full_Name'Length = 0
1265 Delete_File (Host_Full_Name.all & ";*", Success);
1269 -- Otherwise just delete the specified file
1272 Delete_File (Full_Name (1 .. Last), Success);
1275 -- Here if no deletion required
1281 if Verbose_Mode or else not Quiet_Output then
1283 Put ("Warning: """);
1284 Put (Full_Name (1 .. Last));
1285 Put_Line (""" could not be deleted");
1289 Put (Full_Name (1 .. Last));
1290 Put_Line (""" has been deleted");
1296 -----------------------------------
1297 -- Delete_Binder_Generated_Files --
1298 -----------------------------------
1300 procedure Delete_Binder_Generated_Files
1302 Source : File_Name_Type)
1304 Source_Name : constant String := Get_Name_String (Source);
1305 Current : constant String := Get_Current_Dir;
1306 Last : constant Positive := B_Start'Length + Source_Name'Length;
1307 File_Name : String (1 .. Last + 4);
1312 -- Build the file name (before the extension)
1314 File_Name (1 .. B_Start'Length) := B_Start.all;
1315 File_Name (B_Start'Length + 1 .. Last) := Source_Name;
1319 File_Name (Last + 1 .. Last + 4) := ".ads";
1321 if Is_Regular_File (File_Name (1 .. Last + 4)) then
1322 Delete (Dir, File_Name (1 .. Last + 4));
1327 File_Name (Last + 1 .. Last + 4) := ".adb";
1329 if Is_Regular_File (File_Name (1 .. Last + 4)) then
1330 Delete (Dir, File_Name (1 .. Last + 4));
1335 File_Name (Last + 1 .. Last + 4) := ".ali";
1337 if Is_Regular_File (File_Name (1 .. Last + 4)) then
1338 Delete (Dir, File_Name (1 .. Last + 4));
1343 File_Name (Last + 1 .. Last + Object_Suffix'Length) := Object_Suffix;
1345 if Is_Regular_File (File_Name (1 .. Last + Object_Suffix'Length)) then
1346 Delete (Dir, File_Name (1 .. Last + Object_Suffix'Length));
1349 -- Change back to previous directory
1351 Change_Dir (Current);
1352 end Delete_Binder_Generated_Files;
1354 -----------------------
1355 -- Display_Copyright --
1356 -----------------------
1358 procedure Display_Copyright is
1360 if not Copyright_Displayed then
1361 Copyright_Displayed := True;
1362 Display_Version ("GNATCLEAN", "2003");
1364 end Display_Copyright;
1370 procedure Gnatclean is
1372 -- Do the necessary initializations
1376 -- Parse the command line, getting the switches and the executable names
1380 if Verbose_Mode then
1384 if Project_File_Name /= null then
1386 -- A project file was specified by a -P switch
1388 if Opt.Verbose_Mode then
1390 Put ("Parsing Project File """);
1391 Put (Project_File_Name.all);
1396 -- Set the project parsing verbosity to whatever was specified
1397 -- by a possible -vP switch.
1399 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
1401 -- Parse the project file. If there is an error, Main_Project
1402 -- will still be No_Project.
1405 (Project => Main_Project,
1406 In_Tree => Project_Tree,
1407 In_Node_Tree => Project_Node_Tree,
1408 Project_File_Name => Project_File_Name.all,
1409 Env => Root_Environment,
1410 Packages_To_Check => Packages_To_Check_By_Gnatmake);
1412 if Main_Project = No_Project then
1413 Fail ("""" & Project_File_Name.all & """ processing failed");
1416 if Opt.Verbose_Mode then
1418 Put ("Parsing of Project File """);
1419 Put (Project_File_Name.all);
1420 Put (""" is finished.");
1424 -- Add source directories and object directories to the search paths
1426 Add_Source_Directories (Main_Project, Project_Tree);
1427 Add_Object_Directories (Main_Project, Project_Tree);
1430 Osint.Add_Default_Search_Dirs;
1432 -- If a project file was specified, but no executable name, put all
1433 -- the mains of the project file (if any) as if there were on the
1436 if Main_Project /= No_Project and then Osint.Number_Of_Files = 0 then
1438 Main : String_Element;
1439 Value : String_List_Id := Main_Project.Mains;
1441 while Value /= Prj.Nil_String loop
1442 Main := Project_Tree.Shared.String_Elements.Table (Value);
1444 (File_Name => Get_Name_String (Main.Value),
1445 Index => Main.Index);
1451 -- If neither a project file nor an executable were specified, output
1452 -- the usage and exit.
1454 if Main_Project = No_Project and then Osint.Number_Of_Files = 0 then
1459 if Verbose_Mode then
1463 if Main_Project /= No_Project then
1465 -- If a project file has been specified, call Clean_Project with the
1466 -- project id of this project file, after resetting the list of
1467 -- processed projects.
1469 Processed_Projects.Init;
1470 Clean_Project (Main_Project);
1473 -- If no project file has been specified, the work is done in
1474 -- Clean_Executables.
1479 -- In verbose mode, if Delete has not been called, indicate that no file
1480 -- needs to be deleted.
1482 if Verbose_Mode and (not File_Deleted) then
1486 Put_Line ("No file needs to be deleted");
1488 Put_Line ("No file has been deleted");
1493 ------------------------
1494 -- In_Extension_Chain --
1495 ------------------------
1497 function In_Extension_Chain
1498 (Of_Project : Project_Id;
1499 Prj : Project_Id) return Boolean
1504 if Prj = No_Project or else Of_Project = No_Project then
1508 if Of_Project = Prj then
1513 while Proj.Extends /= No_Project loop
1514 if Proj.Extends = Prj then
1518 Proj := Proj.Extends;
1522 while Proj.Extends /= No_Project loop
1523 if Proj.Extends = Of_Project then
1527 Proj := Proj.Extends;
1531 end In_Extension_Chain;
1537 procedure Initialize is
1539 if not Initialized then
1540 Initialized := True;
1542 -- Get default search directories to locate system.ads when calling
1543 -- Targparm.Get_Target_Parameters.
1545 Osint.Add_Default_Search_Dirs;
1547 -- Initialize some packages
1552 Prj.Tree.Initialize (Root_Environment, Gnatmake_Flags);
1553 Prj.Env.Initialize_Default_Project_Path
1554 (Root_Environment.Project_Path,
1555 Target_Name => Sdefault.Target_Name.all);
1557 Project_Node_Tree := new Project_Node_Tree_Data;
1558 Prj.Tree.Initialize (Project_Node_Tree);
1560 Prj.Initialize (Project_Tree);
1562 -- Check if the platform is VMS and, if it is, change some variables
1564 Targparm.Get_Target_Parameters;
1566 if OpenVMS_On_Target then
1567 Debug_Suffix (Debug_Suffix'First) := '_';
1568 Repinfo_Suffix (Repinfo_Suffix'First) := '_';
1569 B_Start := new String'("b__");
1573 -- Reset global variables
1575 Free (Object_Directory_Path);
1576 Do_Nothing := False;
1577 File_Deleted := False;
1578 Copyright_Displayed := False;
1579 Usage_Displayed := False;
1580 Free (Project_File_Name);
1581 Main_Project := Prj.No_Project;
1582 All_Projects := False;
1585 ----------------------
1586 -- Object_File_Name --
1587 ----------------------
1589 function Object_File_Name (Source : File_Name_Type) return String is
1590 Src : constant String := Get_Name_String (Source);
1593 -- If the source name has an extension, then replace it with
1594 -- the Object suffix.
1596 for Index in reverse Src'First + 1 .. Src'Last loop
1597 if Src (Index) = '.' then
1598 return Src (Src'First .. Index - 1) & Object_Suffix;
1602 -- If there is no dot, or if it is the first character, just add the
1605 return Src & Object_Suffix;
1606 end Object_File_Name;
1608 --------------------
1609 -- Parse_Cmd_Line --
1610 --------------------
1612 procedure Parse_Cmd_Line is
1613 Last : constant Natural := Argument_Count;
1614 Source_Index : Int := 0;
1617 procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
1620 -- First, check for --version and --help
1622 Check_Version_And_Help ("GNATCLEAN", "2003");
1625 while Index <= Last loop
1627 Arg : constant String := Argument (Index);
1629 procedure Bad_Argument;
1630 -- Signal bad argument
1636 procedure Bad_Argument is
1638 Fail ("invalid argument """ & Arg & """");
1642 if Arg'Length /= 0 then
1643 if Arg (1) = '-' then
1644 if Arg'Length = 1 then
1650 if Arg'Length > Subdirs_Option'Length and then
1651 Arg (1 .. Subdirs_Option'Length) = Subdirs_Option
1655 (Arg (Subdirs_Option'Length + 1 .. Arg'Last));
1657 elsif Arg = Makeutl.Unchecked_Shared_Lib_Imports then
1658 Opt.Unchecked_Shared_Lib_Imports := True;
1665 if Arg'Length < 4 then
1669 if Arg (3) = 'O' then
1670 Add_Lib_Search_Dir (Arg (4 .. Arg'Last));
1672 elsif Arg (3) = 'P' then
1673 Prj.Env.Add_Directories
1674 (Root_Environment.Project_Path,
1675 Arg (4 .. Arg'Last));
1682 Compile_Only := True;
1685 if Object_Directory_Path /= null then
1686 Fail ("duplicate -D switch");
1688 elsif Project_File_Name /= null then
1689 Fail ("-P and -D cannot be used simultaneously");
1692 if Arg'Length > 2 then
1694 Dir : constant String := Arg (3 .. Arg'Last);
1696 if not Is_Directory (Dir) then
1697 Fail (Dir & " is not a directory");
1699 Add_Lib_Search_Dir (Dir);
1704 if Index = Last then
1705 Fail ("no directory specified after -D");
1711 Dir : constant String := Argument (Index);
1713 if not Is_Directory (Dir) then
1714 Fail (Dir & " is not a directory");
1716 Add_Lib_Search_Dir (Dir);
1723 Follow_Links_For_Files := True;
1724 Follow_Links_For_Dirs := True;
1731 Force_Deletions := True;
1734 Full_Path_Name_For_Brief_Errors := True;
1740 if Arg'Length = 2 then
1746 for J in 3 .. Arg'Last loop
1747 if Arg (J) not in '0' .. '9' then
1752 (20 * Source_Index) +
1753 (Character'Pos (Arg (J)) - Character'Pos ('0'));
1758 Opt.Look_In_Primary_Dir := False;
1761 if Arg'Length = 2 then
1765 Add_Lib_Search_Dir (Arg (3 .. Arg'Last));
1772 if Project_File_Name /= null then
1773 Fail ("multiple -P switches");
1775 elsif Object_Directory_Path /= null then
1776 Fail ("-D and -P cannot be used simultaneously");
1780 if Arg'Length > 2 then
1782 Prj : constant String := Arg (3 .. Arg'Last);
1784 if Prj'Length > 1 and then
1785 Prj (Prj'First) = '='
1787 Project_File_Name :=
1789 (Prj (Prj'First + 1 .. Prj'Last));
1791 Project_File_Name := new String'(Prj);
1796 if Index = Last then
1797 Fail ("no project specified after -P");
1801 Project_File_Name := new String'(Argument (Index));
1805 Quiet_Output := True;
1808 All_Projects := True;
1812 Verbose_Mode := True;
1814 elsif Arg = "-vP0" then
1815 Current_Verbosity := Prj.Default;
1817 elsif Arg = "-vP1" then
1818 Current_Verbosity := Prj.Medium;
1820 elsif Arg = "-vP2" then
1821 Current_Verbosity := Prj.High;
1828 if Arg'Length = 2 then
1833 Ext_Asgn : constant String := Arg (3 .. Arg'Last);
1834 Start : Positive := Ext_Asgn'First;
1835 Stop : Natural := Ext_Asgn'Last;
1836 OK : Boolean := True;
1839 if Ext_Asgn (Start) = '"' then
1840 if Ext_Asgn (Stop) = '"' then
1851 Prj.Ext.Check (Root_Environment.External,
1852 Ext_Asgn (Start .. Stop))
1855 ("illegal external assignment '"
1866 Add_File (Arg, Source_Index);
1875 -----------------------
1876 -- Repinfo_File_Name --
1877 -----------------------
1879 function Repinfo_File_Name (Source : File_Name_Type) return String is
1881 return Get_Name_String (Source) & Repinfo_Suffix;
1882 end Repinfo_File_Name;
1884 --------------------
1885 -- Tree_File_Name --
1886 --------------------
1888 function Tree_File_Name (Source : File_Name_Type) return String is
1889 Src : constant String := Get_Name_String (Source);
1892 -- If source name has an extension, then replace it with the tree suffix
1894 for Index in reverse Src'First + 1 .. Src'Last loop
1895 if Src (Index) = '.' then
1896 return Src (Src'First .. Index - 1) & Tree_Suffix;
1900 -- If there is no dot, or if it is the first character, just add the
1903 return Src & Tree_Suffix;
1912 if not Usage_Displayed then
1913 Usage_Displayed := True;
1915 Put_Line ("Usage: gnatclean [switches] {[-innn] name}");
1918 Display_Usage_Version_And_Help;
1920 Put_Line (" names is one or more file names from which " &
1921 "the .adb or .ads suffix may be omitted");
1922 Put_Line (" names may be omitted if -P<project> is specified");
1925 Put_Line (" --subdirs=dir real obj/lib/exec dirs are subdirs");
1926 Put_Line (" " & Makeutl.Unchecked_Shared_Lib_Imports);
1927 Put_Line (" Allow shared libraries to import static libraries");
1930 Put_Line (" -c Only delete compiler generated files");
1931 Put_Line (" -D dir Specify dir as the object library");
1932 Put_Line (" -eL Follow symbolic links when processing " &
1934 Put_Line (" -f Force deletions of unwritable files");
1935 Put_Line (" -F Full project path name " &
1936 "in brief error messages");
1937 Put_Line (" -h Display this message");
1938 Put_Line (" -innn Index of unit in source for following names");
1939 Put_Line (" -n Nothing to do: only list files to delete");
1940 Put_Line (" -Pproj Use GNAT Project File proj");
1941 Put_Line (" -q Be quiet/terse");
1942 Put_Line (" -r Clean all projects recursively");
1943 Put_Line (" -v Verbose mode");
1944 Put_Line (" -vPx Specify verbosity when parsing " &
1945 "GNAT Project Files");
1946 Put_Line (" -Xnm=val Specify an external reference " &
1947 "for GNAT Project Files");
1950 Put_Line (" -aPdir Add directory dir to project search path");
1953 Put_Line (" -aOdir Specify ALI/object files search path");
1954 Put_Line (" -Idir Like -aOdir");
1955 Put_Line (" -I- Don't look for source/library files " &
1956 "in the default directory");