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 Main_Project : Prj.Project_Id := Prj.No_Project;
103 All_Projects : Boolean := False;
105 -- Packages of project files where unknown attributes are errors
107 Naming_String : aliased String := "naming";
108 Builder_String : aliased String := "builder";
109 Compiler_String : aliased String := "compiler";
110 Binder_String : aliased String := "binder";
111 Linker_String : aliased String := "linker";
113 Gnatmake_Packages : aliased String_List :=
114 (Naming_String 'Access,
115 Builder_String
'Access,
116 Compiler_String 'Access,
117 Binder_String
'Access,
118 Linker_String 'Access);
120 Packages_To_Check_By_Gnatmake
: constant String_List_Access
:=
121 Gnatmake_Packages
'Access;
123 package Processed_Projects
is new Table
.Table
124 (Table_Component_Type
=> Project_Id
,
125 Table_Index_Type
=> Natural,
126 Table_Low_Bound
=> 0,
128 Table_Increment
=> 100,
129 Table_Name
=> "Clean.Processed_Projects");
130 -- Table to keep track of what project files have been processed, when
131 -- switch -r is specified.
133 package Sources
is new Table
.Table
134 (Table_Component_Type
=> File_Name_Type
,
135 Table_Index_Type
=> Natural,
136 Table_Low_Bound
=> 0,
138 Table_Increment
=> 100,
139 Table_Name
=> "Clean.Processed_Projects");
140 -- Table to store all the source files of a library unit: spec, body and
141 -- subunits, to detect .dg files and delete them.
143 -----------------------------
144 -- Other local subprograms --
145 -----------------------------
147 procedure Add_Source_Dir
(N
: String);
148 -- Call Add_Src_Search_Dir and output one line when in verbose mode
150 procedure Add_Source_Directories
is
151 new Prj
.Env
.For_All_Source_Dirs
(Action
=> Add_Source_Dir
);
153 procedure Add_Object_Dir
(N
: String);
154 -- Call Add_Lib_Search_Dir and output one line when in verbose mode
156 procedure Add_Object_Directories
is
157 new Prj
.Env
.For_All_Object_Dirs
(Action
=> Add_Object_Dir
);
159 function ALI_File_Name
(Source
: File_Name_Type
) return String;
160 -- Returns the name of the ALI file corresponding to Source
162 function Assembly_File_Name
(Source
: File_Name_Type
) return String;
163 -- Returns the assembly file name corresponding to Source
165 procedure Clean_Archive
(Project
: Project_Id
; Global
: Boolean);
166 -- Delete a global archive or library project archive and the dependency
167 -- file, if they exist.
169 procedure Clean_Executables
;
170 -- Do the cleaning work when no project file is specified
172 procedure Clean_Interface_Copy_Directory
(Project
: Project_Id
);
173 -- Delete files in an interface copy directory: any file that is a copy of
174 -- a source of the project.
176 procedure Clean_Library_Directory
(Project
: Project_Id
);
177 -- Delete the library file in a library directory and any ALI file of a
178 -- source of the project in a library ALI directory.
180 procedure Clean_Project
(Project
: Project_Id
);
181 -- Do the cleaning work when a project file is specified. This procedure
182 -- calls itself recursively when there are several project files in the
183 -- tree rooted at the main project file and switch -r has been specified.
185 function Debug_File_Name
(Source
: File_Name_Type
) return String;
186 -- Name of the expanded source file corresponding to Source
188 procedure Delete
(In_Directory
: String; File
: String);
189 -- Delete one file, or list the file name if switch -n is specified
191 procedure Delete_Binder_Generated_Files
193 Source
: File_Name_Type
);
194 -- Delete the binder generated file in directory Dir for Source, if they
195 -- exist: for Unix these are b~<source>.ads, b~<source>.adb,
196 -- b~<source>.ali and b~<source>.o.
198 procedure Display_Copyright
;
199 -- Display the Copyright notice. If called several times, display the
200 -- Copyright notice only the first time.
202 procedure Initialize
;
203 -- Call the necessary package initializations
205 function Object_File_Name
(Source
: File_Name_Type
) return String;
206 -- Returns the object file name corresponding to Source
208 procedure Parse_Cmd_Line
;
209 -- Parse the command line
211 function Repinfo_File_Name
(Source
: File_Name_Type
) return String;
212 -- Returns the repinfo file name corresponding to Source
214 function Tree_File_Name
(Source
: File_Name_Type
) return String;
215 -- Returns the tree file name corresponding to Source
217 function In_Extension_Chain
218 (Of_Project
: Project_Id
;
219 Prj
: Project_Id
) return Boolean;
220 -- Returns True iff Prj is an extension of Of_Project or if Of_Project is
221 -- an extension of Prj.
224 -- Display the usage. If called several times, the usage is displayed only
231 procedure Add_Object_Dir
(N
: String) is
233 Add_Lib_Search_Dir
(N
);
235 if Opt
.Verbose_Mode
then
236 Put
("Adding object directory """);
247 procedure Add_Source_Dir
(N
: String) is
249 Add_Src_Search_Dir
(N
);
251 if Opt
.Verbose_Mode
then
252 Put
("Adding source directory """);
263 function ALI_File_Name
(Source
: File_Name_Type
) return String is
264 Src
: constant String := Get_Name_String
(Source
);
267 -- If the source name has an extension, then replace it with
270 for Index
in reverse Src
'First + 1 .. Src
'Last loop
271 if Src
(Index
) = '.' then
272 return Src
(Src
'First .. Index
- 1) & ALI_Suffix
;
276 -- If there is no dot, or if it is the first character, just add the
279 return Src
& ALI_Suffix
;
282 ------------------------
283 -- Assembly_File_Name --
284 ------------------------
286 function Assembly_File_Name
(Source
: File_Name_Type
) return String is
287 Src
: constant String := Get_Name_String
(Source
);
290 -- If the source name has an extension, then replace it with
291 -- the assembly suffix.
293 for Index
in reverse Src
'First + 1 .. Src
'Last loop
294 if Src
(Index
) = '.' then
295 return Src
(Src
'First .. Index
- 1) & Assembly_Suffix
;
299 -- If there is no dot, or if it is the first character, just add the
302 return Src
& Assembly_Suffix
;
303 end Assembly_File_Name
;
309 procedure Clean_Archive
(Project
: Project_Id
; Global
: Boolean) is
310 Current_Dir
: constant Dir_Name_Str
:= Get_Current_Dir
;
312 Lib_Prefix
: String_Access
;
313 Archive_Name
: String_Access
;
314 -- The name of the archive file for this project
316 Archive_Dep_Name
: String_Access
;
317 -- The name of the archive dependency file for this project
319 Obj_Dir
: constant String :=
320 Get_Name_String
(Project
.Object_Directory
.Display_Name
);
323 Change_Dir
(Obj_Dir
);
325 -- First, get the lib prefix, the archive file name and the archive
326 -- dependency file name.
330 new String'("lib" & Get_Name_String (Project.Display_Name));
333 new String'("lib" & Get_Name_String
(Project
.Library_Name
));
336 Archive_Name
:= new String'(Lib_Prefix.all & '.' & Archive_Ext);
337 Archive_Dep_Name := new String'(Lib_Prefix
.all & ".deps");
339 -- Delete the archive file and the archive dependency file, if they
342 if Is_Regular_File
(Archive_Name
.all) then
343 Delete
(Obj_Dir
, Archive_Name
.all);
346 if Is_Regular_File
(Archive_Dep_Name
.all) then
347 Delete
(Obj_Dir
, Archive_Dep_Name
.all);
350 Change_Dir
(Current_Dir
);
353 -----------------------
354 -- Clean_Executables --
355 -----------------------
357 procedure Clean_Executables
is
358 Main_Source_File
: File_Name_Type
;
359 -- Current main source
361 Main_Lib_File
: File_Name_Type
;
362 -- ALI file of the current main
364 Lib_File
: File_Name_Type
;
367 Full_Lib_File
: File_Name_Type
;
368 -- Full name of the current ALI file
370 Text
: Text_Buffer_Ptr
;
373 Source
: Queue
.Source_Info
;
376 Queue
.Initialize
(Queue_Per_Obj_Dir
=> False);
378 -- It does not really matter if there is or not an object file
379 -- corresponding to an ALI file: if there is one, it will be deleted.
381 Opt
.Check_Object_Consistency
:= False;
383 -- Proceed each executable one by one. Each source is marked as it is
384 -- processed, so common sources between executables will not be
385 -- processed several times.
387 for N_File
in 1 .. Osint
.Number_Of_Files
loop
388 Main_Source_File
:= Next_Main_Source
;
390 Osint
.Lib_File_Name
(Main_Source_File
, Current_File_Index
);
392 if Main_Lib_File
/= No_File
then
394 ((Format
=> Format_Gnatmake
,
395 File
=> Main_Lib_File
,
396 Unit
=> No_Unit_Name
,
398 Project
=> No_Project
,
402 while not Queue
.Is_Empty
loop
403 Sources
.Set_Last
(0);
404 Queue
.Extract
(Found
, Source
);
405 pragma Assert
(Found
);
406 pragma Assert
(Source
.File
/= No_File
);
407 Lib_File
:= Source
.File
;
408 Full_Lib_File
:= Osint
.Full_Lib_File_Name
(Lib_File
);
410 -- If we have existing ALI file that is not read-only, process it
412 if Full_Lib_File
/= No_File
413 and then not Is_Readonly_Library
(Full_Lib_File
)
415 Text
:= Read_Library_Info
(Lib_File
);
419 Scan_ALI
(Lib_File
, Text
, Ignore_ED
=> False, Err
=> True);
422 -- If no error was produced while loading this ALI file,
423 -- insert into the queue all the unmarked withed sources.
425 if The_ALI
/= No_ALI_Id
then
426 for J
in ALIs
.Table
(The_ALI
).First_Unit
..
427 ALIs
.Table
(The_ALI
).Last_Unit
429 Sources
.Increment_Last
;
430 Sources
.Table
(Sources
.Last
) :=
431 ALI
.Units
.Table
(J
).Sfile
;
433 for K
in ALI
.Units
.Table
(J
).First_With
..
434 ALI
.Units
.Table
(J
).Last_With
436 if Withs
.Table
(K
).Afile
/= No_File
then
438 ((Format
=> Format_Gnatmake
,
439 File
=> Withs
.Table
(K
).Afile
,
440 Unit
=> No_Unit_Name
,
442 Project
=> No_Project
,
448 -- Look for subunits and put them in the Sources table
450 for J
in ALIs
.Table
(The_ALI
).First_Sdep
..
451 ALIs
.Table
(The_ALI
).Last_Sdep
453 if Sdep
.Table
(J
).Subunit_Name
/= No_Name
then
454 Sources
.Increment_Last
;
455 Sources
.Table
(Sources
.Last
) :=
456 Sdep
.Table
(J
).Sfile
;
462 -- Now delete all existing files corresponding to this ALI file
465 Obj_Dir
: constant String :=
466 Dir_Name
(Get_Name_String
(Full_Lib_File
));
467 Obj
: constant String := Object_File_Name
(Lib_File
);
468 Adt
: constant String := Tree_File_Name
(Lib_File
);
469 Asm
: constant String := Assembly_File_Name
(Lib_File
);
472 Delete
(Obj_Dir
, Get_Name_String
(Lib_File
));
474 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Obj
) then
475 Delete
(Obj_Dir
, Obj
);
478 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Adt
) then
479 Delete
(Obj_Dir
, Adt
);
482 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Asm
) then
483 Delete
(Obj_Dir
, Asm
);
486 -- Delete expanded source files (.dg) and/or repinfo files
489 for J
in 1 .. Sources
.Last
loop
491 Deb
: constant String :=
492 Debug_File_Name
(Sources
.Table
(J
));
493 Rep
: constant String :=
494 Repinfo_File_Name
(Sources
.Table
(J
));
497 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Deb
) then
498 Delete
(Obj_Dir
, Deb
);
501 if Is_Regular_File
(Obj_Dir
& Dir_Separator
& Rep
) then
502 Delete
(Obj_Dir
, Rep
);
510 -- Delete the executable, if it exists, and the binder generated
513 if not Compile_Only
then
515 Source
: constant File_Name_Type
:=
516 Strip_Suffix
(Main_Lib_File
);
517 Executable
: constant String :=
518 Get_Name_String
(Executable_Name
(Source
));
520 if Is_Regular_File
(Executable
) then
521 Delete
("", Executable
);
524 Delete_Binder_Generated_Files
(Get_Current_Dir
, Source
);
528 end Clean_Executables
;
530 ------------------------------------
531 -- Clean_Interface_Copy_Directory --
532 ------------------------------------
534 procedure Clean_Interface_Copy_Directory
(Project
: Project_Id
) is
535 Current
: constant String := Get_Current_Dir
;
539 Name
: String (1 .. 200);
542 Delete_File
: Boolean;
547 and then Project
.Library_Src_Dir
/= No_Path_Information
550 Directory
: constant String :=
551 Get_Name_String
(Project
.Library_Src_Dir
.Display_Name
);
554 Change_Dir
(Directory
);
557 -- For each regular file in the directory, if switch -n has not
558 -- been specified, make it writable and delete the file if it is
559 -- a copy of a source of the project.
562 Read
(Direc
, Name
, Last
);
566 Filename
: constant String := Name
(1 .. Last
);
569 if Is_Regular_File
(Filename
) then
570 Canonical_Case_File_Name
(Name
(1 .. Last
));
571 Delete_File
:= False;
573 Unit
:= Units_Htable
.Get_First
(Project_Tree
.Units_HT
);
575 -- Compare with source file names of the project
577 while Unit
/= No_Unit_Index
loop
578 if Unit
.File_Names
(Impl
) /= null
579 and then Ultimate_Extending_Project_Of
580 (Unit
.File_Names
(Impl
).Project
) = Project
582 Get_Name_String
(Unit
.File_Names
(Impl
).File
) =
589 if Unit
.File_Names
(Spec
) /= null
590 and then Ultimate_Extending_Project_Of
591 (Unit
.File_Names
(Spec
).Project
) = Project
594 (Unit
.File_Names
(Spec
).File
) = Name
(1 .. Last
)
600 Unit
:= Units_Htable
.Get_Next
(Project_Tree
.Units_HT
);
604 if not Do_Nothing
then
605 Set_Writable
(Filename
);
608 Delete
(Directory
, Filename
);
616 -- Restore the initial working directory
618 Change_Dir
(Current
);
621 end Clean_Interface_Copy_Directory
;
623 -----------------------------
624 -- Clean_Library_Directory --
625 -----------------------------
627 Empty_String
: aliased String := "";
629 procedure Clean_Library_Directory
(Project
: Project_Id
) is
630 Current
: constant String := Get_Current_Dir
;
632 Lib_Filename
: constant String := Get_Name_String
(Project
.Library_Name
);
634 DLL_Prefix
& Lib_Filename
& "." & DLL_Ext
;
635 Archive_Name
: String :=
636 "lib" & Lib_Filename
& "." & Archive_Ext
;
639 Name
: String (1 .. 200);
642 Delete_File
: Boolean;
644 Minor
: String_Access
:= Empty_String
'Access;
645 Major
: String_Access
:= Empty_String
'Access;
648 if Project
.Library
then
649 if Project
.Library_Kind
/= Static
650 and then MLib
.Tgt
.Library_Major_Minor_Id_Supported
651 and then Project
.Lib_Internal_Name
/= No_Name
653 Minor
:= new String'(Get_Name_String (Project.Lib_Internal_Name));
654 Major := new String'(MLib
.Major_Id_Name
(DLL_Name
, Minor
.all));
658 Lib_Directory
: constant String :=
659 Get_Name_String
(Project
.Library_Dir
.Display_Name
);
660 Lib_ALI_Directory
: constant String :=
661 Get_Name_String
(Project
.Library_ALI_Dir
.Display_Name
);
664 Canonical_Case_File_Name
(Archive_Name
);
665 Canonical_Case_File_Name
(DLL_Name
);
667 Change_Dir
(Lib_Directory
);
670 -- For each regular file in the directory, if switch -n has not
671 -- been specified, make it writable and delete the file if it is
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
);
713 Change_Dir
(Lib_ALI_Directory
);
716 -- For each regular file in the directory, if switch -n has not
717 -- been specified, make it writable and delete the file if it is
718 -- any ALI file of a source of the project.
721 Read
(Direc
, Name
, Last
);
725 Filename
: constant String := Name
(1 .. Last
);
727 if Is_Regular_File
(Filename
) then
728 Canonical_Case_File_Name
(Name
(1 .. Last
));
729 Delete_File
:= False;
731 if Last
> 4 and then Name
(Last
- 3 .. Last
) = ".ali" then
735 -- Compare with ALI file names of the project
737 Unit
:= Units_Htable
.Get_First
738 (Project_Tree
.Units_HT
);
739 while Unit
/= No_Unit_Index
loop
740 if Unit
.File_Names
(Impl
) /= null
741 and then Unit
.File_Names
(Impl
).Project
/=
744 if Ultimate_Extending_Project_Of
745 (Unit
.File_Names
(Impl
).Project
) =
749 (Unit
.File_Names
(Impl
).File
);
750 Name_Len
:= Name_Len
-
752 (Name
(1 .. Name_Len
))'Length;
753 if Name_Buffer
(1 .. Name_Len
) =
761 elsif Unit
.File_Names
(Spec
) /= null
762 and then Ultimate_Extending_Project_Of
763 (Unit
.File_Names
(Spec
).Project
) =
767 (Unit
.File_Names
(Spec
).File
);
771 (Name
(1 .. Name_Len
))'Length;
773 if Name_Buffer
(1 .. Name_Len
) =
782 Units_Htable
.Get_Next
(Project_Tree
.Units_HT
);
788 if not Do_Nothing
then
789 Set_Writable
(Filename
);
792 Delete
(Lib_ALI_Directory
, Filename
);
800 -- Restore the initial working directory
802 Change_Dir
(Current
);
805 end Clean_Library_Directory
;
811 procedure Clean_Project
(Project
: Project_Id
) is
812 Main_Source_File
: File_Name_Type
;
813 -- Name of executable on the command line without directory info
815 Executable
: File_Name_Type
;
816 -- Name of the executable file
818 Current_Dir
: constant Dir_Name_Str
:= Get_Current_Dir
;
820 File_Name1
: File_Name_Type
;
822 File_Name2
: File_Name_Type
;
824 Lib_File
: File_Name_Type
;
826 Global_Archive
: Boolean := False;
829 -- Check that we don't specify executable on the command line for
830 -- a main library project.
832 if Project
= Main_Project
833 and then Osint
.Number_Of_Files
/= 0
834 and then Project
.Library
837 ("Cannot specify executable(s) for a Library Project File");
840 -- Nothing to clean in an externally built project
842 if Project
.Externally_Built
then
844 Put
("Nothing to do to clean externally built project """);
845 Put
(Get_Name_String
(Project
.Name
));
851 Put
("Cleaning project """);
852 Put
(Get_Name_String
(Project
.Name
));
856 -- Add project to the list of processed projects
858 Processed_Projects
.Increment_Last
;
859 Processed_Projects
.Table
(Processed_Projects
.Last
) := Project
;
861 if Project
.Object_Directory
/= No_Path_Information
then
863 Obj_Dir
: constant String :=
864 Get_Name_String
(Project
.Object_Directory
.Display_Name
);
867 Change_Dir
(Obj_Dir
);
869 -- First, deal with Ada
871 -- Look through the units to find those that are either
872 -- immediate sources or inherited sources of the project.
873 -- Extending projects may have no language specified, if
874 -- Source_Dirs or Source_Files is specified as an empty list,
875 -- so always look for Ada units in extending projects.
877 if Has_Ada_Sources
(Project
)
878 or else Project
.Extends
/= No_Project
880 Unit
:= Units_Htable
.Get_First
(Project_Tree
.Units_HT
);
881 while Unit
/= No_Unit_Index
loop
882 File_Name1
:= No_File
;
883 File_Name2
:= No_File
;
885 -- If either the spec or the body is a source of the
886 -- project, check for the corresponding ALI file in the
889 if (Unit
.File_Names
(Impl
) /= null
892 (Unit
.File_Names
(Impl
).Project
, Project
))
894 (Unit
.File_Names
(Spec
) /= null
895 and then In_Extension_Chain
896 (Unit
.File_Names
(Spec
).Project
, Project
))
898 if Unit
.File_Names
(Impl
) /= null then
899 File_Name1
:= Unit
.File_Names
(Impl
).File
;
900 Index1
:= Unit
.File_Names
(Impl
).Index
;
902 File_Name1
:= No_File
;
906 if Unit
.File_Names
(Spec
) /= null then
907 File_Name2
:= Unit
.File_Names
(Spec
).File
;
908 Index2
:= Unit
.File_Names
(Spec
).Index
;
910 File_Name2
:= No_File
;
914 -- If there is no body file name, then there may be
917 if File_Name1
= No_File
then
918 File_Name1
:= File_Name2
;
920 File_Name2
:= No_File
;
925 -- If there is either a spec or a body, look for files
926 -- in the object directory.
928 if File_Name1
/= No_File
then
929 Lib_File
:= Osint
.Lib_File_Name
(File_Name1
, Index1
);
932 Asm
: constant String :=
933 Assembly_File_Name
(Lib_File
);
934 ALI
: constant String :=
935 ALI_File_Name
(Lib_File
);
936 Obj
: constant String :=
937 Object_File_Name
(Lib_File
);
938 Adt
: constant String :=
939 Tree_File_Name
(Lib_File
);
940 Deb
: constant String :=
941 Debug_File_Name
(File_Name1
);
942 Rep
: constant String :=
943 Repinfo_File_Name
(File_Name1
);
944 Del
: Boolean := True;
947 -- If the ALI file exists and is read-only, no file
950 if Is_Regular_File
(ALI
) then
951 if Is_Writable_File
(ALI
) then
952 Delete
(Obj_Dir
, ALI
);
961 if Obj_Dir
(Obj_Dir
'Last) /=
968 Put_Line
(""" is read-only");
977 if Is_Regular_File
(Obj
) then
978 Delete
(Obj_Dir
, Obj
);
983 if Is_Regular_File
(Asm
) then
984 Delete
(Obj_Dir
, Asm
);
989 if Is_Regular_File
(Adt
) then
990 Delete
(Obj_Dir
, Adt
);
993 -- First expanded source file
995 if Is_Regular_File
(Deb
) then
996 Delete
(Obj_Dir
, Deb
);
1001 if Is_Regular_File
(Rep
) then
1002 Delete
(Obj_Dir
, Rep
);
1005 -- Second expanded source file
1007 if File_Name2
/= No_File
then
1009 Deb
: constant String :=
1010 Debug_File_Name
(File_Name2
);
1011 Rep
: constant String :=
1012 Repinfo_File_Name
(File_Name2
);
1015 if Is_Regular_File
(Deb
) then
1016 Delete
(Obj_Dir
, Deb
);
1019 if Is_Regular_File
(Rep
) then
1020 Delete
(Obj_Dir
, Rep
);
1028 Unit
:= Units_Htable
.Get_Next
(Project_Tree
.Units_HT
);
1032 -- Check if a global archive and it dependency file could have
1033 -- been created and, if they exist, delete them.
1035 if Project
= Main_Project
and then not Project
.Library
then
1036 Global_Archive
:= False;
1039 Proj
: Project_List
;
1042 Proj
:= Project_Tree
.Projects
;
1043 while Proj
/= null loop
1045 -- For gnatmake, when the project specifies more than
1046 -- just Ada as a language (even if course we could not
1047 -- find any source file for the other languages), we
1048 -- will take all the object files found in the object
1049 -- directories. Since we know the project supports at
1050 -- least Ada, we just have to test whether it has at
1051 -- least two languages, and we do not care about the
1054 if Proj
.Project
.Languages
/= null
1055 and then Proj
.Project
.Languages
.Next
/= null
1057 Global_Archive
:= True;
1065 if Global_Archive
then
1066 Clean_Archive
(Project
, Global
=> True);
1073 -- If this is a library project, clean the library directory, the
1074 -- interface copy dir and, for a Stand-Alone Library, the binder
1075 -- generated files of the library.
1077 -- The directories are cleaned only if switch -c is not specified
1079 if Project
.Library
then
1080 if not Compile_Only
then
1081 Clean_Library_Directory
(Project
);
1083 if Project
.Library_Src_Dir
/= No_Path_Information
then
1084 Clean_Interface_Copy_Directory
(Project
);
1088 if Project
.Standalone_Library
/= No
1089 and then Project
.Object_Directory
/= No_Path_Information
1091 Delete_Binder_Generated_Files
1092 (Get_Name_String
(Project
.Object_Directory
.Display_Name
),
1093 File_Name_Type
(Project
.Library_Name
));
1097 if Verbose_Mode
then
1102 -- If switch -r is specified, call Clean_Project recursively for the
1103 -- imported projects and the project being extended.
1105 if All_Projects
then
1107 Imported
: Project_List
;
1111 -- For each imported project, call Clean_Project if the project
1112 -- has not been processed already.
1114 Imported
:= Project
.Imported_Projects
;
1115 while Imported
/= null loop
1119 J
in Processed_Projects
.First
.. Processed_Projects
.Last
1121 if Imported
.Project
= Processed_Projects
.Table
(J
) then
1128 Clean_Project
(Imported
.Project
);
1131 Imported
:= Imported
.Next
;
1134 -- If this project extends another project, call Clean_Project for
1135 -- the project being extended. It is guaranteed that it has not
1136 -- called before, because no other project may import or extend
1139 if Project
.Extends
/= No_Project
then
1140 Clean_Project
(Project
.Extends
);
1145 -- For the main project, delete the executables and the binder
1148 -- The executables are deleted only if switch -c is not specified
1150 if Project
= Main_Project
1151 and then Project
.Exec_Directory
/= No_Path_Information
1154 Exec_Dir
: constant String :=
1155 Get_Name_String
(Project
.Exec_Directory
.Display_Name
);
1158 Change_Dir
(Exec_Dir
);
1160 for N_File
in 1 .. Osint
.Number_Of_Files
loop
1161 Main_Source_File
:= Next_Main_Source
;
1163 if not Compile_Only
then
1167 Project_Tree
.Shared
,
1169 Current_File_Index
);
1172 Exec_File_Name
: constant String :=
1173 Get_Name_String
(Executable
);
1176 if Is_Absolute_Path
(Name
=> Exec_File_Name
) then
1177 if Is_Regular_File
(Exec_File_Name
) then
1178 Delete
("", Exec_File_Name
);
1182 if Is_Regular_File
(Exec_File_Name
) then
1183 Delete
(Exec_Dir
, Exec_File_Name
);
1189 if Project
.Object_Directory
/= No_Path_Information
then
1190 Delete_Binder_Generated_Files
1191 (Get_Name_String
(Project
.Object_Directory
.Display_Name
),
1192 Strip_Suffix
(Main_Source_File
));
1198 -- Change back to previous directory
1200 Change_Dir
(Current_Dir
);
1203 ---------------------
1204 -- Debug_File_Name --
1205 ---------------------
1207 function Debug_File_Name
(Source
: File_Name_Type
) return String is
1209 return Get_Name_String
(Source
) & Debug_Suffix
;
1210 end Debug_File_Name
;
1216 procedure Delete
(In_Directory
: String; File
: String) is
1217 Full_Name
: String (1 .. In_Directory
'Length + File
'Length + 1);
1218 Last
: Natural := 0;
1222 -- Indicate that at least one file is deleted or is to be deleted
1224 File_Deleted
:= True;
1226 -- Build the path name of the file to delete
1228 Last
:= In_Directory
'Length;
1229 Full_Name
(1 .. Last
) := In_Directory
;
1231 if Last
> 0 and then Full_Name
(Last
) /= Directory_Separator
then
1233 Full_Name
(Last
) := Directory_Separator
;
1236 Full_Name
(Last
+ 1 .. Last
+ File
'Length) := File
;
1237 Last
:= Last
+ File
'Length;
1239 -- If switch -n was used, simply output the path name
1242 Put_Line
(Full_Name
(1 .. Last
));
1244 -- Otherwise, delete the file if it is writable
1248 or else Is_Writable_File
(Full_Name
(1 .. Last
))
1249 or else Is_Symbolic_Link
(Full_Name
(1 .. Last
))
1251 -- On VMS, we have to delete all versions of the file
1253 if OpenVMS_On_Target
then
1255 Host_Full_Name
: constant String_Access
:=
1256 To_Host_File_Spec
(Full_Name
(1 .. Last
));
1258 if Host_Full_Name
= null
1259 or else Host_Full_Name
'Length = 0
1263 Delete_File
(Host_Full_Name
.all & ";*", Success
);
1267 -- Otherwise just delete the specified file
1270 Delete_File
(Full_Name
(1 .. Last
), Success
);
1273 -- Here if no deletion required
1279 if Verbose_Mode
or else not Quiet_Output
then
1281 Put
("Warning: """);
1282 Put
(Full_Name
(1 .. Last
));
1283 Put_Line
(""" could not be deleted");
1287 Put
(Full_Name
(1 .. Last
));
1288 Put_Line
(""" has been deleted");
1294 -----------------------------------
1295 -- Delete_Binder_Generated_Files --
1296 -----------------------------------
1298 procedure Delete_Binder_Generated_Files
1300 Source
: File_Name_Type
)
1302 Source_Name
: constant String := Get_Name_String
(Source
);
1303 Current
: constant String := Get_Current_Dir
;
1304 Last
: constant Positive := B_Start
'Length + Source_Name
'Length;
1305 File_Name
: String (1 .. Last
+ 4);
1310 -- Build the file name (before the extension)
1312 File_Name
(1 .. B_Start
'Length) := B_Start
.all;
1313 File_Name
(B_Start
'Length + 1 .. Last
) := Source_Name
;
1317 File_Name
(Last
+ 1 .. Last
+ 4) := ".ads";
1319 if Is_Regular_File
(File_Name
(1 .. Last
+ 4)) then
1320 Delete
(Dir
, File_Name
(1 .. Last
+ 4));
1325 File_Name
(Last
+ 1 .. Last
+ 4) := ".adb";
1327 if Is_Regular_File
(File_Name
(1 .. Last
+ 4)) then
1328 Delete
(Dir
, File_Name
(1 .. Last
+ 4));
1333 File_Name
(Last
+ 1 .. Last
+ 4) := ".ali";
1335 if Is_Regular_File
(File_Name
(1 .. Last
+ 4)) then
1336 Delete
(Dir
, File_Name
(1 .. Last
+ 4));
1341 File_Name
(Last
+ 1 .. Last
+ Object_Suffix
'Length) := Object_Suffix
;
1343 if Is_Regular_File
(File_Name
(1 .. Last
+ Object_Suffix
'Length)) then
1344 Delete
(Dir
, File_Name
(1 .. Last
+ Object_Suffix
'Length));
1347 -- Change back to previous directory
1349 Change_Dir
(Current
);
1350 end Delete_Binder_Generated_Files
;
1352 -----------------------
1353 -- Display_Copyright --
1354 -----------------------
1356 procedure Display_Copyright
is
1358 if not Copyright_Displayed
then
1359 Copyright_Displayed
:= True;
1360 Display_Version
("GNATCLEAN", "2003");
1362 end Display_Copyright
;
1368 procedure Gnatclean
is
1370 -- Do the necessary initializations
1374 -- Parse the command line, getting the switches and the executable names
1378 -- Add the default project search directories now, after the directories
1379 -- that have been specified by switches -aP<dir>.
1381 Prj
.Env
.Initialize_Default_Project_Path
1382 (Root_Environment
.Project_Path
,
1383 Target_Name
=> Sdefault
.Target_Name
.all);
1385 if Verbose_Mode
then
1389 if Project_File_Name
/= null then
1391 -- A project file was specified by a -P switch
1393 if Opt
.Verbose_Mode
then
1395 Put
("Parsing Project File """);
1396 Put
(Project_File_Name
.all);
1401 -- Set the project parsing verbosity to whatever was specified
1402 -- by a possible -vP switch.
1404 Prj
.Pars
.Set_Verbosity
(To
=> Current_Verbosity
);
1406 -- Parse the project file. If there is an error, Main_Project
1407 -- will still be No_Project.
1410 (Project
=> Main_Project
,
1411 In_Tree
=> Project_Tree
,
1412 In_Node_Tree
=> Project_Node_Tree
,
1413 Project_File_Name
=> Project_File_Name
.all,
1414 Env
=> Root_Environment
,
1415 Packages_To_Check
=> Packages_To_Check_By_Gnatmake
);
1417 if Main_Project
= No_Project
then
1418 Fail
("""" & Project_File_Name
.all & """ processing failed");
1421 if Opt
.Verbose_Mode
then
1423 Put
("Parsing of Project File """);
1424 Put
(Project_File_Name
.all);
1425 Put
(""" is finished.");
1429 -- Add source directories and object directories to the search paths
1431 Add_Source_Directories
(Main_Project
, Project_Tree
);
1432 Add_Object_Directories
(Main_Project
, Project_Tree
);
1435 Osint
.Add_Default_Search_Dirs
;
1437 -- If a project file was specified, but no executable name, put all
1438 -- the mains of the project file (if any) as if there were on the
1441 if Main_Project
/= No_Project
and then Osint
.Number_Of_Files
= 0 then
1443 Main
: String_Element
;
1444 Value
: String_List_Id
:= Main_Project
.Mains
;
1446 while Value
/= Prj
.Nil_String
loop
1447 Main
:= Project_Tree
.Shared
.String_Elements
.Table
(Value
);
1449 (File_Name
=> Get_Name_String
(Main
.Value
),
1450 Index
=> Main
.Index
);
1456 -- If neither a project file nor an executable were specified, output
1457 -- the usage and exit.
1459 if Main_Project
= No_Project
and then Osint
.Number_Of_Files
= 0 then
1464 if Verbose_Mode
then
1468 if Main_Project
/= No_Project
then
1470 -- If a project file has been specified, call Clean_Project with the
1471 -- project id of this project file, after resetting the list of
1472 -- processed projects.
1474 Processed_Projects
.Init
;
1475 Clean_Project
(Main_Project
);
1478 -- If no project file has been specified, the work is done in
1479 -- Clean_Executables.
1484 -- In verbose mode, if Delete has not been called, indicate that no file
1485 -- needs to be deleted.
1487 if Verbose_Mode
and (not File_Deleted
) then
1491 Put_Line
("No file needs to be deleted");
1493 Put_Line
("No file has been deleted");
1498 ------------------------
1499 -- In_Extension_Chain --
1500 ------------------------
1502 function In_Extension_Chain
1503 (Of_Project
: Project_Id
;
1504 Prj
: Project_Id
) return Boolean
1509 if Prj
= No_Project
or else Of_Project
= No_Project
then
1513 if Of_Project
= Prj
then
1518 while Proj
.Extends
/= No_Project
loop
1519 if Proj
.Extends
= Prj
then
1523 Proj
:= Proj
.Extends
;
1527 while Proj
.Extends
/= No_Project
loop
1528 if Proj
.Extends
= Of_Project
then
1532 Proj
:= Proj
.Extends
;
1536 end In_Extension_Chain
;
1542 procedure Initialize
is
1544 if not Initialized
then
1545 Initialized
:= True;
1547 -- Get default search directories to locate system.ads when calling
1548 -- Targparm.Get_Target_Parameters.
1550 Osint
.Add_Default_Search_Dirs
;
1552 -- Initialize some packages
1557 Prj
.Tree
.Initialize
(Root_Environment
, Gnatmake_Flags
);
1559 Project_Node_Tree
:= new Project_Node_Tree_Data
;
1560 Prj
.Tree
.Initialize
(Project_Node_Tree
);
1562 Prj
.Initialize
(Project_Tree
);
1564 -- Check if the platform is VMS and, if it is, change some variables
1566 Targparm
.Get_Target_Parameters
;
1568 if OpenVMS_On_Target
then
1569 Debug_Suffix
(Debug_Suffix
'First) := '_';
1570 Repinfo_Suffix
(Repinfo_Suffix
'First) := '_';
1571 B_Start
:= new String'("b__");
1575 -- Reset global variables
1577 Free (Object_Directory_Path);
1578 Do_Nothing := False;
1579 File_Deleted := False;
1580 Copyright_Displayed := False;
1581 Usage_Displayed := False;
1582 Free (Project_File_Name);
1583 Main_Project := Prj.No_Project;
1584 All_Projects := False;
1587 ----------------------
1588 -- Object_File_Name --
1589 ----------------------
1591 function Object_File_Name (Source : File_Name_Type) return String is
1592 Src : constant String := Get_Name_String (Source);
1595 -- If the source name has an extension, then replace it with
1596 -- the Object suffix.
1598 for Index in reverse Src'First + 1 .. Src'Last loop
1599 if Src (Index) = '.' then
1600 return Src (Src'First .. Index - 1) & Object_Suffix;
1604 -- If there is no dot, or if it is the first character, just add the
1607 return Src & Object_Suffix;
1608 end Object_File_Name;
1610 --------------------
1611 -- Parse_Cmd_Line --
1612 --------------------
1614 procedure Parse_Cmd_Line is
1615 Last : constant Natural := Argument_Count;
1616 Source_Index : Int := 0;
1619 procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
1622 -- First, check for --version and --help
1624 Check_Version_And_Help ("GNATCLEAN", "2003");
1627 while Index <= Last loop
1629 Arg : constant String := Argument (Index);
1631 procedure Bad_Argument;
1632 -- Signal bad argument
1638 procedure Bad_Argument is
1640 Fail ("invalid argument """ & Arg & """");
1644 if Arg'Length /= 0 then
1645 if Arg (1) = '-' then
1646 if Arg'Length = 1 then
1652 if Arg'Length > Subdirs_Option'Length and then
1653 Arg (1 .. Subdirs_Option'Length) = Subdirs_Option
1657 (Arg
(Subdirs_Option
'Length + 1 .. Arg
'Last));
1659 elsif Arg
= Makeutl
.Unchecked_Shared_Lib_Imports
then
1660 Opt
.Unchecked_Shared_Lib_Imports
:= True;
1667 if Arg
'Length < 4 then
1671 if Arg
(3) = 'O' then
1672 Add_Lib_Search_Dir
(Arg
(4 .. Arg
'Last));
1674 elsif Arg
(3) = 'P' then
1675 Prj
.Env
.Add_Directories
1676 (Root_Environment
.Project_Path
,
1677 Arg
(4 .. Arg
'Last));
1684 Compile_Only
:= True;
1687 if Object_Directory_Path
/= null then
1688 Fail
("duplicate -D switch");
1690 elsif Project_File_Name
/= null then
1691 Fail
("-P and -D cannot be used simultaneously");
1694 if Arg
'Length > 2 then
1696 Dir
: constant String := Arg
(3 .. Arg
'Last);
1698 if not Is_Directory
(Dir
) then
1699 Fail
(Dir
& " is not a directory");
1701 Add_Lib_Search_Dir
(Dir
);
1706 if Index
= Last
then
1707 Fail
("no directory specified after -D");
1713 Dir
: constant String := Argument
(Index
);
1715 if not Is_Directory
(Dir
) then
1716 Fail
(Dir
& " is not a directory");
1718 Add_Lib_Search_Dir
(Dir
);
1725 Follow_Links_For_Files
:= True;
1726 Follow_Links_For_Dirs
:= True;
1733 Force_Deletions
:= True;
1734 Directories_Must_Exist_In_Projects
:= False;
1737 Full_Path_Name_For_Brief_Errors
:= True;
1743 if Arg
'Length = 2 then
1749 for J
in 3 .. Arg
'Last loop
1750 if Arg
(J
) not in '0' .. '9' then
1755 (20 * Source_Index
) +
1756 (Character'Pos (Arg
(J
)) - Character'Pos ('0'));
1761 Opt
.Look_In_Primary_Dir
:= False;
1764 if Arg
'Length = 2 then
1768 Add_Lib_Search_Dir
(Arg
(3 .. Arg
'Last));
1775 if Project_File_Name
/= null then
1776 Fail
("multiple -P switches");
1778 elsif Object_Directory_Path
/= null then
1779 Fail
("-D and -P cannot be used simultaneously");
1783 if Arg
'Length > 2 then
1785 Prj
: constant String := Arg
(3 .. Arg
'Last);
1787 if Prj
'Length > 1 and then
1788 Prj
(Prj
'First) = '='
1790 Project_File_Name
:=
1792 (Prj (Prj'First + 1 .. Prj'Last));
1794 Project_File_Name := new String'(Prj
);
1799 if Index
= Last
then
1800 Fail
("no project specified after -P");
1804 Project_File_Name
:= new String'(Argument (Index));
1808 Quiet_Output := True;
1811 All_Projects := True;
1815 Verbose_Mode := True;
1817 elsif Arg = "-vP0" then
1818 Current_Verbosity := Prj.Default;
1820 elsif Arg = "-vP1" then
1821 Current_Verbosity := Prj.Medium;
1823 elsif Arg = "-vP2" then
1824 Current_Verbosity := Prj.High;
1831 if Arg'Length = 2 then
1836 Ext_Asgn : constant String := Arg (3 .. Arg'Last);
1837 Start : Positive := Ext_Asgn'First;
1838 Stop : Natural := Ext_Asgn'Last;
1839 OK : Boolean := True;
1842 if Ext_Asgn (Start) = '"' then
1843 if Ext_Asgn (Stop) = '"' then
1854 Prj.Ext.Check (Root_Environment.External,
1855 Ext_Asgn (Start .. Stop))
1858 ("illegal external assignment '"
1869 Add_File (Arg, Source_Index);
1878 -----------------------
1879 -- Repinfo_File_Name --
1880 -----------------------
1882 function Repinfo_File_Name (Source : File_Name_Type) return String is
1884 return Get_Name_String (Source) & Repinfo_Suffix;
1885 end Repinfo_File_Name;
1887 --------------------
1888 -- Tree_File_Name --
1889 --------------------
1891 function Tree_File_Name (Source : File_Name_Type) return String is
1892 Src : constant String := Get_Name_String (Source);
1895 -- If source name has an extension, then replace it with the tree suffix
1897 for Index in reverse Src'First + 1 .. Src'Last loop
1898 if Src (Index) = '.' then
1899 return Src (Src'First .. Index - 1) & Tree_Suffix;
1903 -- If there is no dot, or if it is the first character, just add the
1906 return Src & Tree_Suffix;
1915 if not Usage_Displayed then
1916 Usage_Displayed := True;
1918 Put_Line ("Usage: gnatclean [switches] {[-innn] name}");
1921 Display_Usage_Version_And_Help;
1923 Put_Line (" names is one or more file names from which " &
1924 "the .adb or .ads suffix may be omitted");
1925 Put_Line (" names may be omitted if -P<project> is specified");
1928 Put_Line (" --subdirs=dir real obj/lib/exec dirs are subdirs");
1929 Put_Line (" " & Makeutl.Unchecked_Shared_Lib_Imports);
1930 Put_Line (" Allow shared libraries to import static libraries");
1933 Put_Line (" -c Only delete compiler generated files");
1934 Put_Line (" -D dir Specify dir as the object library");
1935 Put_Line (" -eL Follow symbolic links when processing " &
1937 Put_Line (" -f Force deletions of unwritable files");
1938 Put_Line (" -F Full project path name " &
1939 "in brief error messages");
1940 Put_Line (" -h Display this message");
1941 Put_Line (" -innn Index of unit in source for following names");
1942 Put_Line (" -n Nothing to do: only list files to delete");
1943 Put_Line (" -Pproj Use GNAT Project File proj");
1944 Put_Line (" -q Be quiet/terse");
1945 Put_Line (" -r Clean all projects recursively");
1946 Put_Line (" -v Verbose mode");
1947 Put_Line (" -vPx Specify verbosity when parsing " &
1948 "GNAT Project Files");
1949 Put_Line (" -Xnm=val Specify an external reference " &
1950 "for GNAT Project Files");
1953 Put_Line (" -aPdir Add directory dir to project search path");
1956 Put_Line (" -aOdir Specify ALI/object files search path");
1957 Put_Line (" -Idir Like -aOdir");
1958 Put_Line (" -I- Don't look for source/library files " &
1959 "in the default directory");