1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2003-2017, 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 Make_Util; use Make_Util;
29 with Namet; use Namet;
31 with Osint; use Osint;
32 with Osint.M; use Osint.M;
36 with Switch; use Switch;
38 with Targparm; use Targparm;
39 with Types; use Types;
41 with Ada.Command_Line; use Ada.Command_Line;
43 with GNAT.Command_Line; use GNAT.Command_Line;
44 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
45 with GNAT.IO; use GNAT.IO;
46 with GNAT.OS_Lib; use GNAT.OS_Lib;
50 Initialized : Boolean := False;
51 -- Set to True by the first call to Initialize to avoid reinitialization
54 -- Suffixes of various files
56 Assembly_Suffix : constant String := ".s";
57 Tree_Suffix : constant String := ".adt";
58 Object_Suffix : constant String := Get_Target_Object_Suffix.all;
59 Debug_Suffix : constant String := ".dg";
60 Repinfo_Suffix : constant String := ".rep";
61 -- Suffix of representation info files
63 B_Start : constant String := "b~";
64 -- Prefix of binder generated file, and number of actual characters used
66 Object_Directory_Path : String_Access := null;
67 -- The path name of the object directory, set with switch -D
69 Force_Deletions : Boolean := False;
70 -- Set to True by switch -f. When True, attempts to delete non writable
71 -- files will be done.
73 Do_Nothing : Boolean := False;
74 -- Set to True when switch -n is specified. When True, no file is deleted.
75 -- gnatclean only lists the files that would have been deleted if the
76 -- switch -n had not been specified.
78 File_Deleted : Boolean := False;
79 -- Set to True if at least one file has been deleted
81 Copyright_Displayed : Boolean := False;
82 Usage_Displayed : Boolean := False;
84 Project_File_Name : String_Access := null;
86 package Sources is new Table.Table
87 (Table_Component_Type => File_Name_Type,
88 Table_Index_Type => Natural,
91 Table_Increment => 100,
92 Table_Name => "Clean.Processed_Projects");
93 -- Table to store all the source files of a library unit: spec, body and
94 -- subunits, to detect .dg files and delete them.
96 -----------------------------
97 -- Other local subprograms --
98 -----------------------------
100 function Assembly_File_Name (Source : File_Name_Type) return String;
101 -- Returns the assembly file name corresponding to Source
103 procedure Clean_Executables;
104 -- Do the cleaning work when no project file is specified
106 function Debug_File_Name (Source : File_Name_Type) return String;
107 -- Name of the expanded source file corresponding to Source
109 procedure Delete (In_Directory : String; File : String);
110 -- Delete one file, or list the file name if switch -n is specified
112 procedure Delete_Binder_Generated_Files
114 Source : File_Name_Type);
115 -- Delete the binder generated file in directory Dir for Source, if they
116 -- exist: for Unix these are b~<source>.ads, b~<source>.adb,
117 -- b~<source>.ali and b~<source>.o.
119 procedure Display_Copyright;
120 -- Display the Copyright notice. If called several times, display the
121 -- Copyright notice only the first time.
123 procedure Initialize;
124 -- Call the necessary package initializations
126 function Object_File_Name (Source : File_Name_Type) return String;
127 -- Returns the object file name corresponding to Source
129 procedure Parse_Cmd_Line;
130 -- Parse the command line
132 function Repinfo_File_Name (Source : File_Name_Type) return String;
133 -- Returns the repinfo file name corresponding to Source
135 function Tree_File_Name (Source : File_Name_Type) return String;
136 -- Returns the tree file name corresponding to Source
139 -- Display the usage. If called several times, the usage is displayed only
142 ------------------------
143 -- Assembly_File_Name --
144 ------------------------
146 function Assembly_File_Name (Source : File_Name_Type) return String is
147 Src : constant String := Get_Name_String (Source);
150 -- If the source name has an extension, then replace it with
151 -- the assembly suffix.
153 for Index in reverse Src'First + 1 .. Src'Last loop
154 if Src (Index) = '.' then
155 return Src (Src'First .. Index - 1) & Assembly_Suffix;
159 -- If there is no dot, or if it is the first character, just add the
162 return Src & Assembly_Suffix;
163 end Assembly_File_Name;
165 -----------------------
166 -- Clean_Executables --
167 -----------------------
169 procedure Clean_Executables is
170 Main_Source_File : File_Name_Type;
171 -- Current main source
173 Main_Lib_File : File_Name_Type;
174 -- ALI file of the current main
176 Lib_File : File_Name_Type;
179 Full_Lib_File : File_Name_Type;
180 -- Full name of the current ALI file
182 Text : Text_Buffer_Ptr;
185 Source : Queue.Source_Info;
190 -- It does not really matter if there is or not an object file
191 -- corresponding to an ALI file: if there is one, it will be deleted.
193 Opt.Check_Object_Consistency := False;
195 -- Proceed each executable one by one. Each source is marked as it is
196 -- processed, so common sources between executables will not be
197 -- processed several times.
199 for N_File in 1 .. Osint.Number_Of_Files loop
200 Main_Source_File := Next_Main_Source;
202 Osint.Lib_File_Name (Main_Source_File, Current_File_Index);
204 if Main_Lib_File /= No_File then
206 ((File => Main_Lib_File,
207 Unit => No_Unit_Name,
211 while not Queue.Is_Empty loop
212 Sources.Set_Last (0);
213 Queue.Extract (Found, Source);
214 pragma Assert (Found);
215 pragma Assert (Source.File /= No_File);
216 Lib_File := Source.File;
217 Full_Lib_File := Osint.Full_Lib_File_Name (Lib_File);
219 -- If we have existing ALI file that is not read-only, process it
221 if Full_Lib_File /= No_File
222 and then not Is_Readonly_Library (Full_Lib_File)
224 Text := Read_Library_Info (Lib_File);
228 Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
231 -- If no error was produced while loading this ALI file,
232 -- insert into the queue all the unmarked withed sources.
234 if The_ALI /= No_ALI_Id then
235 for J in ALIs.Table (The_ALI).First_Unit ..
236 ALIs.Table (The_ALI).Last_Unit
238 Sources.Increment_Last;
239 Sources.Table (Sources.Last) :=
240 ALI.Units.Table (J).Sfile;
242 for K in ALI.Units.Table (J).First_With ..
243 ALI.Units.Table (J).Last_With
245 if Withs.Table (K).Afile /= No_File then
247 ((File => Withs.Table (K).Afile,
248 Unit => No_Unit_Name,
254 -- Look for subunits and put them in the Sources table
256 for J in ALIs.Table (The_ALI).First_Sdep ..
257 ALIs.Table (The_ALI).Last_Sdep
259 if Sdep.Table (J).Subunit_Name /= No_Name then
260 Sources.Increment_Last;
261 Sources.Table (Sources.Last) :=
262 Sdep.Table (J).Sfile;
268 -- Now delete all existing files corresponding to this ALI file
271 Obj_Dir : constant String :=
272 Dir_Name (Get_Name_String (Full_Lib_File));
273 Obj : constant String := Object_File_Name (Lib_File);
274 Adt : constant String := Tree_File_Name (Lib_File);
275 Asm : constant String := Assembly_File_Name (Lib_File);
278 Delete (Obj_Dir, Get_Name_String (Lib_File));
280 if Is_Regular_File (Obj_Dir & Dir_Separator & Obj) then
281 Delete (Obj_Dir, Obj);
284 if Is_Regular_File (Obj_Dir & Dir_Separator & Adt) then
285 Delete (Obj_Dir, Adt);
288 if Is_Regular_File (Obj_Dir & Dir_Separator & Asm) then
289 Delete (Obj_Dir, Asm);
292 -- Delete expanded source files (.dg) and/or repinfo files
295 for J in 1 .. Sources.Last loop
297 Deb : constant String :=
298 Debug_File_Name (Sources.Table (J));
299 Rep : constant String :=
300 Repinfo_File_Name (Sources.Table (J));
303 if Is_Regular_File (Obj_Dir & Dir_Separator & Deb) then
304 Delete (Obj_Dir, Deb);
307 if Is_Regular_File (Obj_Dir & Dir_Separator & Rep) then
308 Delete (Obj_Dir, Rep);
316 -- Delete the executable, if it exists, and the binder generated
319 if not Compile_Only then
321 Source : constant File_Name_Type :=
322 Strip_Suffix (Main_Lib_File);
323 Executable : constant String :=
324 Get_Name_String (Executable_Name (Source));
326 if Is_Regular_File (Executable) then
327 Delete ("", Executable);
330 Delete_Binder_Generated_Files (Get_Current_Dir, Source);
334 end Clean_Executables;
336 ---------------------
337 -- Debug_File_Name --
338 ---------------------
340 function Debug_File_Name (Source : File_Name_Type) return String is
342 return Get_Name_String (Source) & Debug_Suffix;
349 procedure Delete (In_Directory : String; File : String) is
350 Full_Name : String (1 .. In_Directory'Length + File'Length + 1);
355 -- Indicate that at least one file is deleted or is to be deleted
357 File_Deleted := True;
359 -- Build the path name of the file to delete
361 Last := In_Directory'Length;
362 Full_Name (1 .. Last) := In_Directory;
364 if Last > 0 and then Full_Name (Last) /= Directory_Separator then
366 Full_Name (Last) := Directory_Separator;
369 Full_Name (Last + 1 .. Last + File'Length) := File;
370 Last := Last + File'Length;
372 -- If switch -n was used, simply output the path name
375 Put_Line (Full_Name (1 .. Last));
377 -- Otherwise, delete the file if it is writable
381 or else Is_Writable_File (Full_Name (1 .. Last))
382 or else Is_Symbolic_Link (Full_Name (1 .. Last))
384 Delete_File (Full_Name (1 .. Last), Success);
386 -- Here if no deletion required
392 if Verbose_Mode or else not Quiet_Output then
395 Put (Full_Name (1 .. Last));
396 Put_Line (""" could not be deleted");
400 Put (Full_Name (1 .. Last));
401 Put_Line (""" has been deleted");
407 -----------------------------------
408 -- Delete_Binder_Generated_Files --
409 -----------------------------------
411 procedure Delete_Binder_Generated_Files
413 Source : File_Name_Type)
415 Source_Name : constant String := Get_Name_String (Source);
416 Current : constant String := Get_Current_Dir;
417 Last : constant Positive := B_Start'Length + Source_Name'Length;
418 File_Name : String (1 .. Last + 4);
423 -- Build the file name (before the extension)
425 File_Name (1 .. B_Start'Length) := B_Start;
426 File_Name (B_Start'Length + 1 .. Last) := Source_Name;
430 File_Name (Last + 1 .. Last + 4) := ".ads";
432 if Is_Regular_File (File_Name (1 .. Last + 4)) then
433 Delete (Dir, File_Name (1 .. Last + 4));
438 File_Name (Last + 1 .. Last + 4) := ".adb";
440 if Is_Regular_File (File_Name (1 .. Last + 4)) then
441 Delete (Dir, File_Name (1 .. Last + 4));
446 File_Name (Last + 1 .. Last + 4) := ".ali";
448 if Is_Regular_File (File_Name (1 .. Last + 4)) then
449 Delete (Dir, File_Name (1 .. Last + 4));
454 File_Name (Last + 1 .. Last + Object_Suffix'Length) := Object_Suffix;
456 if Is_Regular_File (File_Name (1 .. Last + Object_Suffix'Length)) then
457 Delete (Dir, File_Name (1 .. Last + Object_Suffix'Length));
460 -- Change back to previous directory
462 Change_Dir (Current);
463 end Delete_Binder_Generated_Files;
465 -----------------------
466 -- Display_Copyright --
467 -----------------------
469 procedure Display_Copyright is
471 if not Copyright_Displayed then
472 Copyright_Displayed := True;
473 Display_Version ("GNATCLEAN", "2003");
475 end Display_Copyright;
481 procedure Gnatclean is
483 -- Do the necessary initializations
487 -- Parse the command line, getting the switches and the executable names
495 Osint.Add_Default_Search_Dirs;
497 if Osint.Number_Of_Files = 0 then
498 if Argument_Count = 0 then
511 if Project_File_Name /= null then
513 Gprclean_Path : constant String_Access :=
514 Locate_Exec_On_Path ("gprclean");
515 Arg_Len : Natural := Argument_Count;
517 Target : String_Access := null;
518 Success : Boolean := False;
520 if Gprclean_Path = null then
522 ("project files are no longer supported by gnatclean;" &
523 " use gprclean instead");
529 and then Name_Buffer (Name_Len - 7 .. Name_Len) = "gnatclean"
531 Target := new String'(Name_Buffer (1 .. Name_Len - 9));
532 Arg_Len := Arg_Len + 1;
536 Args : Argument_List (1 .. Arg_Len);
538 if Target /= null then
539 Args (1) := new String'("--target=" & Target.all);
543 for J in 1 .. Argument_Count loop
545 Args (Pos) := new String'(Argument (J));
548 Spawn (Gprclean_Path.all, Args, Success);
551 Exit_Program (E_Success);
553 Exit_Program (E_Errors);
561 -- In verbose mode, if Delete has not been called, indicate that no file
562 -- needs to be deleted.
564 if Verbose_Mode and (not File_Deleted) then
568 Put_Line ("No file needs to be deleted");
570 Put_Line ("No file has been deleted");
579 procedure Initialize is
581 if not Initialized then
584 -- Get default search directories to locate system.ads when calling
585 -- Targparm.Get_Target_Parameters.
587 Osint.Add_Default_Search_Dirs;
589 -- Initialize some packages
595 Targparm.Get_Target_Parameters;
598 -- Reset global variables
600 Free (Object_Directory_Path);
602 File_Deleted := False;
603 Copyright_Displayed := False;
604 Usage_Displayed := False;
607 ----------------------
608 -- Object_File_Name --
609 ----------------------
611 function Object_File_Name (Source : File_Name_Type) return String is
612 Src : constant String := Get_Name_String (Source);
615 -- If the source name has an extension, then replace it with
616 -- the Object suffix.
618 for Index in reverse Src'First + 1 .. Src'Last loop
619 if Src (Index) = '.' then
620 return Src (Src'First .. Index - 1) & Object_Suffix;
624 -- If there is no dot, or if it is the first character, just add the
627 return Src & Object_Suffix;
628 end Object_File_Name;
634 procedure Parse_Cmd_Line is
635 Last : constant Natural := Argument_Count;
637 Source_Index : Int := 0;
639 procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
642 -- First, check for --version and --help
644 Check_Version_And_Help ("GNATCLEAN", "2003");
646 -- First, check for switch -P and, if found and gprclean is available,
647 -- silently invoke gprclean, with switch --target if not on a native
651 Arg_Len : Positive := Argument_Count;
652 Call_Gprclean : Boolean := False;
653 Gprclean : String_Access := null;
656 Target : String_Access := null;
662 and then Name_Buffer (Name_Len - 8 .. Name_Len) = "gnatclean"
665 Target := new String'(Name_Buffer (1 .. Name_Len - 10));
666 Arg_Len := Arg_Len + 1;
669 for J in 1 .. Argument_Count loop
671 Arg : constant String := Argument (J);
674 and then Arg (Arg'First .. Arg'First + 1) = "-P"
676 Call_Gprclean := True;
682 if Call_Gprclean then
683 Gprclean := Locate_Exec_On_Path (Exec_Name => "gprclean");
685 if Gprclean /= null then
687 Args : Argument_List (1 .. Arg_Len);
689 if Target /= null then
690 Args (1) := new String'("--target=" & Target.all);
694 for J in 1 .. Argument_Count loop
696 Args (Pos) := new String'(Argument (J));
699 Spawn (Gprclean.all, Args, Success);
704 Exit_Program (E_Success);
713 while Index <= Last loop
715 Arg : constant String := Argument (Index);
717 procedure Bad_Argument;
718 -- Signal bad argument
724 procedure Bad_Argument is
726 Fail ("invalid argument """ & Arg & """");
730 if Arg'Length /= 0 then
731 if Arg (1) = '-' then
732 if Arg'Length = 1 then
738 if Arg'Length > Subdirs_Option'Length
740 Arg (1 .. Subdirs_Option'Length) = Subdirs_Option
743 -- Subdirs are only used in gprclean
745 elsif Arg = Make_Util.Unchecked_Shared_Lib_Imports then
746 Opt.Unchecked_Shared_Lib_Imports := True;
753 if Arg'Length < 4 then
757 if Arg (3) = 'O' then
758 Add_Lib_Search_Dir (Arg (4 .. Arg'Last));
760 elsif Arg (3) = 'P' then
762 -- This is only for gprclean
769 Compile_Only := True;
772 if Object_Directory_Path /= null then
773 Fail ("duplicate -D switch");
775 elsif Project_File_Name /= null then
776 Fail ("-P and -D cannot be used simultaneously");
779 if Arg'Length > 2 then
781 Dir : constant String := Arg (3 .. Arg'Last);
783 if not Is_Directory (Dir) then
784 Fail (Dir & " is not a directory");
786 Add_Lib_Search_Dir (Dir);
792 Fail ("no directory specified after -D");
798 Dir : constant String := Argument (Index);
800 if not Is_Directory (Dir) then
801 Fail (Dir & " is not a directory");
803 Add_Lib_Search_Dir (Dir);
810 Follow_Links_For_Files := True;
811 Follow_Links_For_Dirs := True;
818 Force_Deletions := True;
819 Directories_Must_Exist_In_Projects := False;
822 Full_Path_Name_For_Brief_Errors := True;
828 if Arg'Length = 2 then
834 for J in 3 .. Arg'Last loop
835 if Arg (J) not in '0' .. '9' then
840 (20 * Source_Index) +
841 (Character'Pos (Arg (J)) - Character'Pos ('0'));
846 Opt.Look_In_Primary_Dir := False;
849 if Arg'Length = 2 then
853 Add_Lib_Search_Dir (Arg (3 .. Arg'Last));
860 if Project_File_Name /= null then
861 Fail ("multiple -P switches");
863 elsif Object_Directory_Path /= null then
864 Fail ("-D and -P cannot be used simultaneously");
868 if Arg'Length > 2 then
870 Prj : constant String := Arg (3 .. Arg'Last);
873 and then Prj (Prj'First) = '='
877 (Prj (Prj'First + 1 .. Prj'Last));
879 Project_File_Name := new String'(Prj);
885 Fail ("no project specified after -P");
889 Project_File_Name := new String'(Argument (Index));
893 Quiet_Output := True;
897 -- This is only for gprclean
901 Verbose_Mode := True;
908 -- This is only for gprclean
915 if Arg'Length = 2 then
924 Add_File (Arg, Source_Index);
933 -----------------------
934 -- Repinfo_File_Name --
935 -----------------------
937 function Repinfo_File_Name (Source : File_Name_Type) return String is
939 return Get_Name_String (Source) & Repinfo_Suffix;
940 end Repinfo_File_Name;
946 function Tree_File_Name (Source : File_Name_Type) return String is
947 Src : constant String := Get_Name_String (Source);
950 -- If source name has an extension, then replace it with the tree suffix
952 for Index in reverse Src'First + 1 .. Src'Last loop
953 if Src (Index) = '.' then
954 return Src (Src'First .. Index - 1) & Tree_Suffix;
958 -- If there is no dot, or if it is the first character, just add the
961 return Src & Tree_Suffix;
970 if not Usage_Displayed then
971 Usage_Displayed := True;
973 Put_Line ("Usage: gnatclean [switches] {[-innn] name}");
976 Display_Usage_Version_And_Help;
978 Put_Line (" names is one or more file names from which " &
979 "the .adb or .ads suffix may be omitted");
980 Put_Line (" names may be omitted if -P<project> is specified");
983 Put_Line (" --subdirs=dir real obj/lib/exec dirs are subdirs");
984 Put_Line (" " & Make_Util.Unchecked_Shared_Lib_Imports);
985 Put_Line (" Allow shared libraries to import static libraries");
988 Put_Line (" -c Only delete compiler generated files");
989 Put_Line (" -D dir Specify dir as the object library");
990 Put_Line (" -eL Follow symbolic links when processing " &
992 Put_Line (" -f Force deletions of unwritable files");
993 Put_Line (" -F Full project path name " &
994 "in brief error messages");
995 Put_Line (" -h Display this message");
996 Put_Line (" -innn Index of unit in source for following names");
997 Put_Line (" -n Nothing to do: only list files to delete");
998 Put_Line (" -Pproj Use GNAT Project File proj");
999 Put_Line (" -q Be quiet/terse");
1000 Put_Line (" -r Clean all projects recursively");
1001 Put_Line (" -v Verbose mode");
1002 Put_Line (" -vPx Specify verbosity when parsing " &
1003 "GNAT Project Files");
1004 Put_Line (" -Xnm=val Specify an external reference " &
1005 "for GNAT Project Files");
1008 Put_Line (" -aPdir Add directory dir to project search path");
1011 Put_Line (" -aOdir Specify ALI/object files search path");
1012 Put_Line (" -Idir Like -aOdir");
1013 Put_Line (" -I- Don't look for source/library files " &
1014 "in the default directory");