1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2003-2004, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 with Ada.Command_Line; use Ada.Command_Line;
33 with Makeutl; use Makeutl;
34 with Namet; use Namet;
36 with Osint; use Osint;
37 with Osint.M; use Osint.M;
43 with Prj.Util; use Prj.Util;
47 with Types; use Types;
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_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 := "b~";
72 -- Prefix of binder generated file.
73 -- Changed to "b$" for VMS in the body of the package.
75 Object_Directory_Path : String_Access := null;
76 -- The path name of the object directory, set with switch -D
78 Do_Nothing : Boolean := False;
79 -- Set to True when switch -n is specified.
80 -- When True, no file is deleted. gnatclean only lists the files that
81 -- would have been deleted if the switch -n had not been specified.
83 File_Deleted : Boolean := False;
84 -- Set to True if at least one file has been deleted
86 Copyright_Displayed : Boolean := False;
87 Usage_Displayed : Boolean := False;
89 Project_File_Name : String_Access := null;
91 Main_Project : Prj.Project_Id := Prj.No_Project;
93 All_Projects : Boolean := False;
95 -- Packages of project files where unknown attributes are errors.
97 Naming_String : aliased String := "naming";
98 Builder_String : aliased String := "builder";
99 Compiler_String : aliased String := "compiler";
100 Binder_String : aliased String := "binder";
101 Linker_String : aliased String := "linker";
103 Gnatmake_Packages : aliased String_List :=
104 (Naming_String 'Access,
105 Builder_String 'Access,
106 Compiler_String 'Access,
107 Binder_String 'Access,
108 Linker_String 'Access);
110 Packages_To_Check_By_Gnatmake : constant String_List_Access :=
111 Gnatmake_Packages'Access;
113 package Processed_Projects is new Table.Table
114 (Table_Component_Type => Project_Id,
115 Table_Index_Type => Natural,
116 Table_Low_Bound => 0,
118 Table_Increment => 10,
119 Table_Name => "Clean.Processed_Projects");
120 -- Table to keep track of what project files have been processed, when
121 -- switch -r is specified.
123 package Sources is new Table.Table
124 (Table_Component_Type => File_Name_Type,
125 Table_Index_Type => Natural,
126 Table_Low_Bound => 0,
128 Table_Increment => 10,
129 Table_Name => "Clean.Processed_Projects");
130 -- Table to store all the source files of a library unit: spec, body and
131 -- subunits, to detect .dg files and delete them.
133 ----------------------------
134 -- Queue (Q) manipulation --
135 ----------------------------
138 -- Must be called to initialize the Q
140 procedure Insert_Q (Lib_File : File_Name_Type);
141 -- If Lib_File is not marked, inserts it at the end of Q and mark it
143 function Empty_Q return Boolean;
144 -- Returns True if Q is empty.
146 procedure Extract_From_Q (Lib_File : out File_Name_Type);
147 -- Extracts the first element from the Q.
150 -- Points to the first valid element in the Q.
152 package Q is new Table.Table (
153 Table_Component_Type => File_Name_Type,
154 Table_Index_Type => Natural,
155 Table_Low_Bound => 0,
156 Table_Initial => 4000,
157 Table_Increment => 100,
158 Table_Name => "Clean.Q");
159 -- This is the actual queue
161 -----------------------------
162 -- Other local subprograms --
163 -----------------------------
165 procedure Add_Source_Dir (N : String);
166 -- Call Add_Src_Search_Dir.
167 -- Output one line when in verbose mode.
169 procedure Add_Source_Directories is
170 new Prj.Env.For_All_Source_Dirs (Action => Add_Source_Dir);
172 procedure Add_Object_Dir (N : String);
173 -- Call Add_Lib_Search_Dir.
174 -- Output one line when in verbose mode.
176 procedure Add_Object_Directories is
177 new Prj.Env.For_All_Object_Dirs (Action => Add_Object_Dir);
179 function ALI_File_Name (Source : Name_Id) return String;
180 -- Returns the name of the ALI file corresponding to Source
182 function Assembly_File_Name (Source : Name_Id) return String;
183 -- Returns the assembly file name corresponding to Source
185 procedure Clean_Directory (Dir : Name_Id);
186 -- Delete all regular files in a library directory or in a library
189 procedure Clean_Executables;
190 -- Do the cleaning work when no project file is specified
192 procedure Clean_Project (Project : Project_Id);
193 -- Do the cleaning work when a project file is specified.
194 -- This procedure calls itself recursively when there are several
195 -- project files in the tree rooted at the main project file and switch -r
196 -- has been specified.
198 function Debug_File_Name (Source : Name_Id) return String;
199 -- Name of the expanded source file corresponding to Source
201 procedure Delete (In_Directory : String; File : String);
202 -- Delete one file, or list the file name if switch -n is specified
204 procedure Delete_Binder_Generated_Files (Dir : String; Source : Name_Id);
205 -- Delete the binder generated file in directory Dir for Source, if they
206 -- exist: for Unix these are b~<source>.ads, b~<source>.adb,
207 -- b~<source>.ali and b~<source>.o.
209 procedure Display_Copyright;
210 -- Display the Copyright notice.
211 -- If called several times, display the Copyright notice only the first
214 procedure Initialize;
215 -- Call the necessary package initializations
217 function Object_File_Name (Source : Name_Id) return String;
218 -- Returns the object file name corresponding to Source
220 procedure Parse_Cmd_Line;
221 -- Parse the command line
223 function Repinfo_File_Name (Source : Name_Id) return String;
224 -- Returns the repinfo file name corresponding to Source
226 function Tree_File_Name (Source : Name_Id) return String;
227 -- Returns the tree file name corresponding to Source
229 function In_Extension_Chain
230 (Of_Project : Project_Id;
231 Prj : Project_Id) return Boolean;
232 -- Returns True iff Prj is an extension of Of_Project or if Of_Project is
233 -- an extension of Prj.
236 -- Display the usage.
237 -- If called several times, the usage is displayed only the first time.
243 procedure Add_Object_Dir (N : String) is
245 Add_Lib_Search_Dir (N);
247 if Opt.Verbose_Mode then
248 Put ("Adding object directory """);
259 procedure Add_Source_Dir (N : String) is
261 Add_Src_Search_Dir (N);
263 if Opt.Verbose_Mode then
264 Put ("Adding source directory """);
275 function ALI_File_Name (Source : Name_Id) return String is
276 Src : constant String := Get_Name_String (Source);
279 -- If the source name has an extension, then replace it with
282 for Index in reverse Src'First + 1 .. Src'Last loop
283 if Src (Index) = '.' then
284 return Src (Src'First .. Index - 1) & ALI_Suffix;
288 -- If there is no dot, or if it is the first character, just add the
291 return Src & ALI_Suffix;
294 ------------------------
295 -- Assembly_File_Name --
296 ------------------------
298 function Assembly_File_Name (Source : Name_Id) return String is
299 Src : constant String := Get_Name_String (Source);
302 -- If the source name has an extension, then replace it with
303 -- the assembly suffix.
305 for Index in reverse Src'First + 1 .. Src'Last loop
306 if Src (Index) = '.' then
307 return Src (Src'First .. Index - 1) & Assembly_Suffix;
311 -- If there is no dot, or if it is the first character, just add the
314 return Src & Assembly_Suffix;
315 end Assembly_File_Name;
317 ---------------------
318 -- Clean_Directory --
319 ---------------------
321 procedure Clean_Directory (Dir : Name_Id) is
322 Directory : constant String := Get_Name_String (Dir);
323 Current : constant Dir_Name_Str := Get_Current_Dir;
327 Name : String (1 .. 200);
330 procedure Set_Writable (Name : System.Address);
331 pragma Import (C, Set_Writable, "__gnat_set_writable");
334 Change_Dir (Directory);
337 -- For each regular file in the directory, if switch -n has not been
338 -- specified, make it writable and delete the file.
341 Read (Direc, Name, Last);
344 if Is_Regular_File (Name (1 .. Last)) then
345 if not Do_Nothing then
346 Name (Last + 1) := ASCII.NUL;
347 Set_Writable (Name (1)'Address);
350 Delete (Directory, Name (1 .. Last));
356 -- Restore the initial working directory
358 Change_Dir (Current);
361 -----------------------
362 -- Clean_Executables --
363 -----------------------
365 procedure Clean_Executables is
366 Main_Source_File : File_Name_Type;
367 -- Current main source
369 Main_Lib_File : File_Name_Type;
370 -- ALI file of the current main
372 Lib_File : File_Name_Type;
375 Full_Lib_File : File_Name_Type;
376 -- Full name of the current ALI file
378 Text : Text_Buffer_Ptr;
384 -- It does not really matter if there is or not an object file
385 -- corresponding to an ALI file: if there is one, it will be deleted.
387 Opt.Check_Object_Consistency := False;
389 -- Proceed each executable one by one. Each source is marked as it is
390 -- processed, so common sources between executables will not be
391 -- processed several times.
393 for N_File in 1 .. Osint.Number_Of_Files loop
394 Main_Source_File := Next_Main_Source;
395 Main_Lib_File := Osint.Lib_File_Name
396 (Main_Source_File, Current_File_Index);
397 Insert_Q (Main_Lib_File);
399 while not Empty_Q loop
400 Sources.Set_Last (0);
401 Extract_From_Q (Lib_File);
402 Full_Lib_File := Osint.Full_Lib_File_Name (Lib_File);
404 -- If we have an existing ALI file that is not read-only,
407 if Full_Lib_File /= No_File
408 and then not Is_Readonly_Library (Full_Lib_File)
410 Text := Read_Library_Info (Lib_File);
414 Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
417 -- If no error was produced while loading this ALI file,
418 -- insert into the queue all the unmarked withed sources.
420 if The_ALI /= No_ALI_Id then
421 for J in ALIs.Table (The_ALI).First_Unit ..
422 ALIs.Table (The_ALI).Last_Unit
424 Sources.Increment_Last;
425 Sources.Table (Sources.Last) :=
426 ALI.Units.Table (J).Sfile;
428 for K in ALI.Units.Table (J).First_With ..
429 ALI.Units.Table (J).Last_With
431 Insert_Q (Withs.Table (K).Afile);
435 -- Look for subunits and put them in the Sources table
437 for J in ALIs.Table (The_ALI).First_Sdep ..
438 ALIs.Table (The_ALI).Last_Sdep
440 if Sdep.Table (J).Subunit_Name /= No_Name then
441 Sources.Increment_Last;
442 Sources.Table (Sources.Last) :=
443 Sdep.Table (J).Sfile;
449 -- Now, delete all the existing files corresponding to this
453 Obj_Dir : constant String :=
454 Dir_Name (Get_Name_String (Full_Lib_File));
455 Obj : constant String := Object_File_Name (Lib_File);
456 Adt : constant String := Tree_File_Name (Lib_File);
457 Asm : constant String := Assembly_File_Name (Lib_File);
460 Delete (Obj_Dir, Get_Name_String (Lib_File));
462 if Is_Regular_File (Obj_Dir & Dir_Separator & Obj) then
463 Delete (Obj_Dir, Obj);
466 if Is_Regular_File (Obj_Dir & Dir_Separator & Adt) then
467 Delete (Obj_Dir, Adt);
470 if Is_Regular_File (Obj_Dir & Dir_Separator & Asm) then
471 Delete (Obj_Dir, Asm);
474 -- Delete expanded source files (.dg) and/or repinfo files
477 for J in 1 .. Sources.Last loop
479 Deb : constant String :=
480 Debug_File_Name (Sources.Table (J));
481 Rep : constant String :=
482 Repinfo_File_Name (Sources.Table (J));
484 if Is_Regular_File (Obj_Dir & Dir_Separator & Deb) then
485 Delete (Obj_Dir, Deb);
488 if Is_Regular_File (Obj_Dir & Dir_Separator & Rep) then
489 Delete (Obj_Dir, Rep);
497 -- Delete the executable, if it exists, and the binder generated
500 if not Compile_Only then
502 Source : constant Name_Id := Strip_Suffix (Main_Lib_File);
503 Executable : constant String := Get_Name_String
504 (Executable_Name (Source));
506 if Is_Regular_File (Executable) then
507 Delete ("", Executable);
510 Delete_Binder_Generated_Files (Get_Current_Dir, Source);
514 end Clean_Executables;
520 procedure Clean_Project (Project : Project_Id) is
521 Main_Source_File : File_Name_Type;
522 -- Name of the executable on the command line, without directory
525 Executable : Name_Id;
526 -- Name of the executable file
528 Current_Dir : constant Dir_Name_Str := Get_Current_Dir;
529 Data : constant Project_Data := Projects.Table (Project);
530 U_Data : Prj.Com.Unit_Data;
531 File_Name1 : Name_Id;
533 File_Name2 : Name_Id;
535 Lib_File : File_Name_Type;
540 -- Check that we don't specify executable on the command line for
541 -- a main library project.
543 if Project = Main_Project
544 and then Osint.Number_Of_Files /= 0
545 and then Data.Library
548 ("Cannot specify executable(s) for a Library Project File");
552 Put ("Cleaning project """);
553 Put (Get_Name_String (Data.Name));
557 -- Add project to the list of proceesed projects
559 Processed_Projects.Increment_Last;
560 Processed_Projects.Table (Processed_Projects.Last) := Project;
562 if Data.Object_Directory /= No_Name then
564 Obj_Dir : constant String :=
565 Get_Name_String (Data.Object_Directory);
568 Change_Dir (Obj_Dir);
570 -- Look through the units to find those that are either immediate
571 -- sources or inherited sources of the project.
573 for Unit in 1 .. Prj.Com.Units.Last loop
574 U_Data := Prj.Com.Units.Table (Unit);
575 File_Name1 := No_Name;
576 File_Name2 := No_Name;
578 -- If either the spec or the body is a source of the project,
579 -- check for the corresponding ALI file in the object
582 if In_Extension_Chain
583 (U_Data.File_Names (Body_Part).Project, Project)
586 (U_Data.File_Names (Specification).Project, Project)
588 File_Name1 := U_Data.File_Names (Body_Part).Name;
589 Index1 := U_Data.File_Names (Body_Part).Index;
590 File_Name2 := U_Data.File_Names (Specification).Name;
591 Index2 := U_Data.File_Names (Specification).Index;
593 -- If there is no body file name, then there may be only a
596 if File_Name1 = No_Name then
597 File_Name1 := File_Name2;
599 File_Name2 := No_Name;
604 -- If there is either a spec or a body, look for files in the
607 if File_Name1 /= No_Name then
608 Lib_File := Osint.Lib_File_Name (File_Name1, Index1);
611 Asm : constant String := Assembly_File_Name (Lib_File);
612 ALI : constant String := ALI_File_Name (Lib_File);
613 Obj : constant String := Object_File_Name (Lib_File);
614 Adt : constant String := Tree_File_Name (Lib_File);
615 Deb : constant String := Debug_File_Name (File_Name1);
616 Rep : constant String := Repinfo_File_Name (File_Name1);
617 Del : Boolean := True;
620 -- If the ALI file exists and is read-only, no file is
623 if Is_Regular_File (ALI) then
624 if Is_Writable_File (ALI) then
625 Delete (Obj_Dir, ALI);
634 if Obj_Dir (Obj_Dir'Last) /= Dir_Separator then
639 Put_Line (""" is read-only");
648 if Is_Regular_File (Obj) then
649 Delete (Obj_Dir, Obj);
654 if Is_Regular_File (Asm) then
655 Delete (Obj_Dir, Asm);
660 if Is_Regular_File (Adt) then
661 Delete (Obj_Dir, Adt);
664 -- First expanded source file
666 if Is_Regular_File (Deb) then
667 Delete (Obj_Dir, Deb);
672 if Is_Regular_File (Rep) then
673 Delete (Obj_Dir, Rep);
676 -- Second expanded source file
678 if File_Name2 /= No_Name then
680 Deb : constant String :=
681 Debug_File_Name (File_Name2);
682 Rep : constant String :=
683 Repinfo_File_Name (File_Name2);
685 if Is_Regular_File (Deb) then
686 Delete (Obj_Dir, Deb);
689 if Is_Regular_File (Rep) then
690 Delete (Obj_Dir, Rep);
705 -- If switch -r is specified, call Clean_Project recursively for the
706 -- imported projects and the project being extended.
710 Imported : Project_List := Data.Imported_Projects;
711 Element : Project_Element;
715 -- For each imported project, call Clean_Project if the project
716 -- has not been processed already.
718 while Imported /= Empty_Project_List loop
719 Element := Project_Lists.Table (Imported);
720 Imported := Element.Next;
724 J in Processed_Projects.First .. Processed_Projects.Last
726 if Element.Project = Processed_Projects.Table (J) then
733 Clean_Project (Element.Project);
737 -- If this project extends another project, call Clean_Project for
738 -- the project being extended. It is guaranteed that it has not
739 -- called before, because no other project may import or extend
742 if Data.Extends /= No_Project then
743 Clean_Project (Data.Extends);
748 -- If this is a library project, clean the library directory, the
749 -- interface copy dir and, for a Stand-Alone Library, the binder
750 -- generated files of the library.
752 -- The directories are cleaned only if switch -c is not specified.
755 if not Compile_Only then
756 Clean_Directory (Data.Library_Dir);
758 if Data.Library_Src_Dir /= No_Name
759 and then Data.Library_Src_Dir /= Data.Library_Dir
761 Clean_Directory (Data.Library_Src_Dir);
765 if Data.Standalone_Library and then
766 Data.Object_Directory /= No_Name
768 Delete_Binder_Generated_Files
769 (Get_Name_String (Data.Object_Directory), Data.Library_Name);
772 -- Otherwise, for the main project, delete the executables and the
773 -- binder generated files.
775 -- The executables are deleted only if switch -c is not specified.
777 elsif Project = Main_Project and then Data.Exec_Directory /= No_Name then
779 Exec_Dir : constant String :=
780 Get_Name_String (Data.Exec_Directory);
782 Change_Dir (Exec_Dir);
784 for N_File in 1 .. Osint.Number_Of_Files loop
785 Main_Source_File := Next_Main_Source;
787 if not Compile_Only then
794 if Is_Regular_File (Get_Name_String (Executable)) then
795 Delete (Exec_Dir, Get_Name_String (Executable));
799 if Data.Object_Directory /= No_Name then
800 Delete_Binder_Generated_Files
802 (Data.Object_Directory),
803 Strip_Suffix (Main_Source_File));
809 -- Change back to previous directory
811 Change_Dir (Current_Dir);
814 ---------------------
815 -- Debug_File_Name --
816 ---------------------
818 function Debug_File_Name (Source : Name_Id) return String is
820 return Get_Name_String (Source) & Debug_Suffix;
827 procedure Delete (In_Directory : String; File : String) is
828 Full_Name : String (1 .. In_Directory'Length + File'Length + 1);
833 -- Indicate that at least one file is deleted or is to be deleted
835 File_Deleted := True;
837 -- Build the path name of the file to delete
839 Last := In_Directory'Length;
840 Full_Name (1 .. Last) := In_Directory;
842 if Last > 0 and then Full_Name (Last) /= Directory_Separator then
844 Full_Name (Last) := Directory_Separator;
847 Full_Name (Last + 1 .. Last + File'Length) := File;
848 Last := Last + File'Length;
850 -- If switch -n was used, simply output the path name
853 Put_Line (Full_Name (1 .. Last));
855 -- Otherwise, delete the file
858 Delete_File (Full_Name (1 .. Last), Success);
862 Put (Full_Name (1 .. Last));
863 Put_Line (""" could not be deleted");
865 elsif Verbose_Mode or else not Quiet_Output then
867 Put (Full_Name (1 .. Last));
868 Put_Line (""" has been deleted");
873 -----------------------------------
874 -- Delete_Binder_Generated_Files --
875 -----------------------------------
877 procedure Delete_Binder_Generated_Files (Dir : String; Source : Name_Id) is
878 Source_Name : constant String := Get_Name_String (Source);
879 Current : constant String := Get_Current_Dir;
880 Last : constant Positive := B_Start'Length + Source_Name'Length;
881 File_Name : String (1 .. Last + 4);
886 -- Build the file name (before the extension)
888 File_Name (1 .. B_Start'Length) := B_Start;
889 File_Name (B_Start'Length + 1 .. Last) := Source_Name;
893 File_Name (Last + 1 .. Last + 4) := ".ads";
895 if Is_Regular_File (File_Name (1 .. Last + 4)) then
896 Delete (Dir, File_Name (1 .. Last + 4));
901 File_Name (Last + 1 .. Last + 4) := ".adb";
903 if Is_Regular_File (File_Name (1 .. Last + 4)) then
904 Delete (Dir, File_Name (1 .. Last + 4));
909 File_Name (Last + 1 .. Last + 4) := ".ali";
911 if Is_Regular_File (File_Name (1 .. Last + 4)) then
912 Delete (Dir, File_Name (1 .. Last + 4));
917 File_Name (Last + 1 .. Last + Object_Suffix'Length) := Object_Suffix;
919 if Is_Regular_File (File_Name (1 .. Last + Object_Suffix'Length)) then
920 Delete (Dir, File_Name (1 .. Last + Object_Suffix'Length));
923 -- Change back to previous directory
925 Change_Dir (Current);
926 end Delete_Binder_Generated_Files;
928 -----------------------
929 -- Display_Copyright --
930 -----------------------
932 procedure Display_Copyright is
934 if not Copyright_Displayed then
935 Copyright_Displayed := True;
936 Put_Line ("GNATCLEAN " & Gnatvsn.Gnat_Version_String
937 & " Copyright 2003-2004 Free Software Foundation, Inc.");
939 end Display_Copyright;
945 function Empty_Q return Boolean is
947 return Q_Front >= Q.Last;
954 procedure Extract_From_Q (Lib_File : out File_Name_Type) is
955 Lib : constant File_Name_Type := Q.Table (Q_Front);
958 Q_Front := Q_Front + 1;
966 procedure Gnatclean is
968 -- Do the necessary initializations
972 -- Parse the command line, getting the switches and the executable names
980 if Project_File_Name /= null then
982 -- A project file was specified by a -P switch
984 if Opt.Verbose_Mode then
986 Put ("Parsing Project File """);
987 Put (Project_File_Name.all);
992 -- Set the project parsing verbosity to whatever was specified
993 -- by a possible -vP switch.
995 Prj.Pars.Set_Verbosity (To => Prj.Com.Current_Verbosity);
997 -- Parse the project file.
998 -- If there is an error, Main_Project will still be No_Project.
1001 (Project => Main_Project,
1002 Project_File_Name => Project_File_Name.all,
1003 Packages_To_Check => Packages_To_Check_By_Gnatmake);
1005 if Main_Project = No_Project then
1006 Fail ("""" & Project_File_Name.all &
1007 """ processing failed");
1010 if Opt.Verbose_Mode then
1012 Put ("Parsing of Project File """);
1013 Put (Project_File_Name.all);
1014 Put (""" is finished.");
1018 -- We add the source directories and the object directories
1019 -- to the search paths.
1021 Add_Source_Directories (Main_Project);
1022 Add_Object_Directories (Main_Project);
1026 Osint.Add_Default_Search_Dirs;
1028 -- If a project file was specified, but no executable name, put all
1029 -- the mains of the project file (if any) as if there were on the
1032 if Main_Project /= No_Project and then Osint.Number_Of_Files = 0 then
1034 Value : String_List_Id := Projects.Table (Main_Project).Mains;
1035 Main : String_Element;
1037 while Value /= Prj.Nil_String loop
1038 Main := String_Elements.Table (Value);
1040 (File_Name => Get_Name_String (Main.Value),
1041 Index => Main.Index);
1047 -- If neither a project file nor an executable were specified,
1048 -- output the usage and exit.
1050 if Main_Project = No_Project and then Osint.Number_Of_Files = 0 then
1055 if Verbose_Mode then
1059 if Main_Project /= No_Project then
1061 -- If a project file has been specified, call Clean_Project with the
1062 -- project id of this project file, after resetting the list of
1063 -- processed projects.
1065 Processed_Projects.Init;
1066 Clean_Project (Main_Project);
1069 -- If no project file has been specified, the work is done in
1070 -- Clean_Executables.
1075 -- In verbose mode, if Delete has not been called, indicate that
1076 -- no file needs to be deleted.
1078 if Verbose_Mode and (not File_Deleted) then
1082 Put_Line ("No file needs to be deleted");
1084 Put_Line ("No file has been deleted");
1089 ------------------------
1090 -- In_Extension_Chain --
1091 ------------------------
1093 function In_Extension_Chain
1094 (Of_Project : Project_Id;
1095 Prj : Project_Id) return Boolean
1097 Data : Project_Data;
1100 if Of_Project = Prj then
1104 Data := Projects.Table (Of_Project);
1106 while Data.Extends /= No_Project loop
1107 if Data.Extends = Prj then
1111 Data := Projects.Table (Data.Extends);
1114 Data := Projects.Table (Prj);
1116 while Data.Extends /= No_Project loop
1117 if Data.Extends = Of_Project then
1121 Data := Projects.Table (Data.Extends);
1125 end In_Extension_Chain;
1134 Q.Set_Last (Q.First);
1141 procedure Initialize is
1143 if not Initialized then
1144 Initialized := True;
1146 -- Initialize some packages
1154 -- Reset global variables
1156 Free (Object_Directory_Path);
1157 Do_Nothing := False;
1158 File_Deleted := False;
1159 Copyright_Displayed := False;
1160 Usage_Displayed := False;
1161 Free (Project_File_Name);
1162 Main_Project := Prj.No_Project;
1163 All_Projects := False;
1170 procedure Insert_Q (Lib_File : File_Name_Type) is
1172 -- Do not insert an empty name or an already marked source
1174 if Lib_File /= No_Name and then not Is_Marked (Lib_File) then
1175 Q.Table (Q.Last) := Lib_File;
1178 -- Mark the source that has been just added to the Q
1184 ----------------------
1185 -- Object_File_Name --
1186 ----------------------
1188 function Object_File_Name (Source : Name_Id) return String is
1189 Src : constant String := Get_Name_String (Source);
1192 -- If the source name has an extension, then replace it with
1193 -- the Object suffix.
1195 for Index in reverse Src'First + 1 .. Src'Last loop
1196 if Src (Index) = '.' then
1197 return Src (Src'First .. Index - 1) & Object_Suffix;
1201 -- If there is no dot, or if it is the first character, just add the
1204 return Src & Object_Suffix;
1205 end Object_File_Name;
1207 --------------------
1208 -- Parse_Cmd_Line --
1209 --------------------
1211 procedure Parse_Cmd_Line is
1212 Source_Index : Int := 0;
1213 Index : Positive := 1;
1214 Last : constant Natural := Argument_Count;
1216 while Index <= Last loop
1218 Arg : constant String := Argument (Index);
1220 procedure Bad_Argument;
1221 -- Signal bad argument
1227 procedure Bad_Argument is
1229 Fail ("invalid argument """, Arg, """");
1233 if Arg'Length /= 0 then
1234 if Arg (1) = '-' then
1235 if Arg'Length = 1 then
1241 if Arg'Length < 4 or else Arg (3) /= 'O' then
1245 Add_Lib_Search_Dir (Arg (3 .. Arg'Last));
1248 Compile_Only := True;
1251 if Object_Directory_Path /= null then
1252 Fail ("duplicate -D switch");
1254 elsif Project_File_Name /= null then
1255 Fail ("-P and -D cannot be used simultaneously");
1258 if Arg'Length > 2 then
1260 Dir : constant String := Arg (3 .. Arg'Last);
1262 if not Is_Directory (Dir) then
1263 Fail (Dir, " is not a directory");
1265 Add_Lib_Search_Dir (Dir);
1270 if Index = Last then
1271 Fail ("no directory specified after -D");
1277 Dir : constant String := Argument (Index);
1279 if not Is_Directory (Dir) then
1280 Fail (Dir, " is not a directory");
1282 Add_Lib_Search_Dir (Dir);
1288 Full_Path_Name_For_Brief_Errors := True;
1294 if Arg'Length = 2 then
1300 for J in 3 .. Arg'Last loop
1301 if Arg (J) not in '0' .. '9' then
1306 (20 * Source_Index) +
1307 (Character'Pos (Arg (J)) - Character'Pos ('0'));
1312 Opt.Look_In_Primary_Dir := False;
1315 if Arg'Length = 2 then
1319 Add_Lib_Search_Dir (Arg (3 .. Arg'Last));
1326 if Project_File_Name /= null then
1327 Fail ("multiple -P switches");
1329 elsif Object_Directory_Path /= null then
1330 Fail ("-D and -P cannot be used simultaneously");
1334 if Arg'Length > 2 then
1336 Prj : constant String := Arg (3 .. Arg'Last);
1338 if Prj'Length > 1 and then
1339 Prj (Prj'First) = '='
1341 Project_File_Name :=
1343 (Prj (Prj'First + 1 .. Prj'Last));
1345 Project_File_Name := new String'(Prj);
1350 if Index = Last then
1351 Fail ("no project specified after -P");
1355 Project_File_Name := new String'(Argument (Index));
1359 Quiet_Output := True;
1362 All_Projects := True;
1366 Verbose_Mode := True;
1368 elsif Arg = "-vP0" then
1369 Prj.Com.Current_Verbosity := Prj.Default;
1371 elsif Arg = "-vP1" then
1372 Prj.Com.Current_Verbosity := Prj.Medium;
1374 elsif Arg = "-vP2" then
1375 Prj.Com.Current_Verbosity := Prj.High;
1382 if Arg'Length = 2 then
1387 Ext_Asgn : constant String := Arg (3 .. Arg'Last);
1388 Start : Positive := Ext_Asgn'First;
1389 Stop : Natural := Ext_Asgn'Last;
1390 Equal_Pos : Natural;
1391 OK : Boolean := True;
1394 if Ext_Asgn (Start) = '"' then
1395 if Ext_Asgn (Stop) = '"' then
1406 while Equal_Pos <= Stop
1407 and then Ext_Asgn (Equal_Pos) /= '='
1409 Equal_Pos := Equal_Pos + 1;
1412 if Equal_Pos = Start or else Equal_Pos > Stop then
1419 Ext_Asgn (Start .. Equal_Pos - 1),
1421 Ext_Asgn (Equal_Pos + 1 .. Stop));
1425 ("illegal external assignment '",
1435 Add_File (Arg, Source_Index);
1444 -----------------------
1445 -- Repinfo_File_Name --
1446 -----------------------
1448 function Repinfo_File_Name (Source : Name_Id) return String is
1450 return Get_Name_String (Source) & Repinfo_Suffix;
1451 end Repinfo_File_Name;
1453 --------------------
1454 -- Tree_File_Name --
1455 --------------------
1457 function Tree_File_Name (Source : Name_Id) return String is
1458 Src : constant String := Get_Name_String (Source);
1461 -- If the source name has an extension, then replace it with
1464 for Index in reverse Src'First + 1 .. Src'Last loop
1465 if Src (Index) = '.' then
1466 return Src (Src'First .. Index - 1) & Tree_Suffix;
1470 -- If there is no dot, or if it is the first character, just add the
1473 return Src & Tree_Suffix;
1482 if not Usage_Displayed then
1483 Usage_Displayed := True;
1485 Put_Line ("Usage: gnatclean [switches] {[-innn] name}");
1488 Put_Line (" names is one or more file names from which " &
1489 "the .adb or .ads suffix may be omitted");
1490 Put_Line (" names may be omitted if -P<project> is specified");
1493 Put_Line (" -c Only delete compiler generated files");
1494 Put_Line (" -D dir Specify dir as the object library");
1495 Put_Line (" -F Full project path name " &
1496 "in brief error messages");
1497 Put_Line (" -h Display this message");
1498 Put_Line (" -innn Index of unit in source for following names");
1499 Put_Line (" -n Nothing to do: only list files to delete");
1500 Put_Line (" -Pproj Use GNAT Project File proj");
1501 Put_Line (" -q Be quiet/terse");
1502 Put_Line (" -r Clean all projects recursively");
1503 Put_Line (" -v Verbose mode");
1504 Put_Line (" -vPx Specify verbosity when parsing " &
1505 "GNAT Project Files");
1506 Put_Line (" -Xnm=val Specify an external reference " &
1507 "for GNAT Project Files");
1510 Put_Line (" -aOdir Specify ALI/object files search path");
1511 Put_Line (" -Idir Like -aOdir");
1512 Put_Line (" -I- Don't look for source/library files " &
1513 "in the default directory");
1519 if Hostparm.OpenVMS then
1520 Debug_Suffix (Debug_Suffix'First) := '_';
1521 Repinfo_Suffix (Repinfo_Suffix'First) := '_';
1522 B_Start (B_Start'Last) := '$';