Implement -mmemcpy-strategy= and -mmemset-strategy= options
[official-gcc.git] / gcc / ada / clean.adb
blobcbaaa61c7d02c91822202b72390c79f5e12bc934
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- C L E A N --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2003-2013, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with ALI; use ALI;
27 with Csets;
28 with Makeutl; use Makeutl;
29 with MLib.Tgt; use MLib.Tgt;
30 with Namet; use Namet;
31 with Opt; use Opt;
32 with Osint; use Osint;
33 with Osint.M; use Osint.M;
34 with Prj; use Prj;
35 with Prj.Env;
36 with Prj.Ext;
37 with Prj.Pars;
38 with Prj.Tree; use Prj.Tree;
39 with Prj.Util; use Prj.Util;
40 with Sdefault;
41 with Snames;
42 with Switch; use Switch;
43 with Table;
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;
53 package body Clean is
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);
77 -- The project tree
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,
127 Table_Initial => 10,
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,
137 Table_Initial => 10,
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
192 (Dir : String;
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.
223 procedure Usage;
224 -- Display the usage. If called several times, the usage is displayed only
225 -- the first time.
227 --------------------
228 -- Add_Object_Dir --
229 --------------------
231 procedure Add_Object_Dir (N : String) is
232 begin
233 Add_Lib_Search_Dir (N);
235 if Opt.Verbose_Mode then
236 Put ("Adding object directory """);
237 Put (N);
238 Put (""".");
239 New_Line;
240 end if;
241 end Add_Object_Dir;
243 --------------------
244 -- Add_Source_Dir --
245 --------------------
247 procedure Add_Source_Dir (N : String) is
248 begin
249 Add_Src_Search_Dir (N);
251 if Opt.Verbose_Mode then
252 Put ("Adding source directory """);
253 Put (N);
254 Put (""".");
255 New_Line;
256 end if;
257 end Add_Source_Dir;
259 -------------------
260 -- ALI_File_Name --
261 -------------------
263 function ALI_File_Name (Source : File_Name_Type) return String is
264 Src : constant String := Get_Name_String (Source);
266 begin
267 -- If the source name has an extension, then replace it with
268 -- the ALI suffix.
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;
273 end if;
274 end loop;
276 -- If there is no dot, or if it is the first character, just add the
277 -- ALI suffix.
279 return Src & ALI_Suffix;
280 end ALI_File_Name;
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);
289 begin
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;
296 end if;
297 end loop;
299 -- If there is no dot, or if it is the first character, just add the
300 -- assembly suffix.
302 return Src & Assembly_Suffix;
303 end Assembly_File_Name;
305 -------------------
306 -- Clean_Archive --
307 -------------------
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);
322 begin
323 Change_Dir (Obj_Dir);
325 -- First, get the lib prefix, the archive file name and the archive
326 -- dependency file name.
328 if Global then
329 Lib_Prefix :=
330 new String'("lib" & Get_Name_String (Project.Display_Name));
331 else
332 Lib_Prefix :=
333 new String'("lib" & Get_Name_String (Project.Library_Name));
334 end if;
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
340 -- exist.
342 if Is_Regular_File (Archive_Name.all) then
343 Delete (Obj_Dir, Archive_Name.all);
344 end if;
346 if Is_Regular_File (Archive_Dep_Name.all) then
347 Delete (Obj_Dir, Archive_Dep_Name.all);
348 end if;
350 Change_Dir (Current_Dir);
351 end Clean_Archive;
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;
365 -- Current ALI file
367 Full_Lib_File : File_Name_Type;
368 -- Full name of the current ALI file
370 Text : Text_Buffer_Ptr;
371 The_ALI : ALI_Id;
372 Found : Boolean;
373 Source : Queue.Source_Info;
375 begin
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;
389 Main_Lib_File :=
390 Osint.Lib_File_Name (Main_Source_File, Current_File_Index);
392 if Main_Lib_File /= No_File then
393 Queue.Insert
394 ((Format => Format_Gnatmake,
395 File => Main_Lib_File,
396 Unit => No_Unit_Name,
397 Index => 0,
398 Project => No_Project,
399 Sid => No_Source));
400 end if;
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)
414 then
415 Text := Read_Library_Info (Lib_File);
417 if Text /= null then
418 The_ALI :=
419 Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
420 Free (Text);
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
428 loop
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
435 loop
436 if Withs.Table (K).Afile /= No_File then
437 Queue.Insert
438 ((Format => Format_Gnatmake,
439 File => Withs.Table (K).Afile,
440 Unit => No_Unit_Name,
441 Index => 0,
442 Project => No_Project,
443 Sid => No_Source));
444 end if;
445 end loop;
446 end loop;
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
452 loop
453 if Sdep.Table (J).Subunit_Name /= No_Name then
454 Sources.Increment_Last;
455 Sources.Table (Sources.Last) :=
456 Sdep.Table (J).Sfile;
457 end if;
458 end loop;
459 end if;
460 end if;
462 -- Now delete all existing files corresponding to this ALI file
464 declare
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);
471 begin
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);
476 end if;
478 if Is_Regular_File (Obj_Dir & Dir_Separator & Adt) then
479 Delete (Obj_Dir, Adt);
480 end if;
482 if Is_Regular_File (Obj_Dir & Dir_Separator & Asm) then
483 Delete (Obj_Dir, Asm);
484 end if;
486 -- Delete expanded source files (.dg) and/or repinfo files
487 -- (.rep) if any
489 for J in 1 .. Sources.Last loop
490 declare
491 Deb : constant String :=
492 Debug_File_Name (Sources.Table (J));
493 Rep : constant String :=
494 Repinfo_File_Name (Sources.Table (J));
496 begin
497 if Is_Regular_File (Obj_Dir & Dir_Separator & Deb) then
498 Delete (Obj_Dir, Deb);
499 end if;
501 if Is_Regular_File (Obj_Dir & Dir_Separator & Rep) then
502 Delete (Obj_Dir, Rep);
503 end if;
504 end;
505 end loop;
506 end;
507 end if;
508 end loop;
510 -- Delete the executable, if it exists, and the binder generated
511 -- files, if any.
513 if not Compile_Only then
514 declare
515 Source : constant File_Name_Type :=
516 Strip_Suffix (Main_Lib_File);
517 Executable : constant String :=
518 Get_Name_String (Executable_Name (Source));
519 begin
520 if Is_Regular_File (Executable) then
521 Delete ("", Executable);
522 end if;
524 Delete_Binder_Generated_Files (Get_Current_Dir, Source);
525 end;
526 end if;
527 end loop;
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;
537 Direc : Dir_Type;
539 Name : String (1 .. 200);
540 Last : Natural;
542 Delete_File : Boolean;
543 Unit : Unit_Index;
545 begin
546 if Project.Library
547 and then Project.Library_Src_Dir /= No_Path_Information
548 then
549 declare
550 Directory : constant String :=
551 Get_Name_String (Project.Library_Src_Dir.Display_Name);
553 begin
554 Change_Dir (Directory);
555 Open (Direc, ".");
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.
561 loop
562 Read (Direc, Name, Last);
563 exit when Last = 0;
565 declare
566 Filename : constant String := Name (1 .. Last);
568 begin
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
581 and then
582 Get_Name_String (Unit.File_Names (Impl).File) =
583 Name (1 .. Last)
584 then
585 Delete_File := True;
586 exit;
587 end if;
589 if Unit.File_Names (Spec) /= null
590 and then Ultimate_Extending_Project_Of
591 (Unit.File_Names (Spec).Project) = Project
592 and then
593 Get_Name_String
594 (Unit.File_Names (Spec).File) = Name (1 .. Last)
595 then
596 Delete_File := True;
597 exit;
598 end if;
600 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
601 end loop;
603 if Delete_File then
604 if not Do_Nothing then
605 Set_Writable (Filename);
606 end if;
608 Delete (Directory, Filename);
609 end if;
610 end if;
611 end;
612 end loop;
614 Close (Direc);
616 -- Restore the initial working directory
618 Change_Dir (Current);
619 end;
620 end if;
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);
633 DLL_Name : String :=
634 DLL_Prefix & Lib_Filename & "." & DLL_Ext;
635 Archive_Name : String :=
636 "lib" & Lib_Filename & "." & Archive_Ext;
637 Direc : Dir_Type;
639 Name : String (1 .. 200);
640 Last : Natural;
642 Delete_File : Boolean;
644 Minor : String_Access := Empty_String'Access;
645 Major : String_Access := Empty_String'Access;
647 begin
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
652 then
653 Minor := new String'(Get_Name_String (Project.Lib_Internal_Name));
654 Major := new String'(MLib.Major_Id_Name (DLL_Name, Minor.all));
655 end if;
657 declare
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);
663 begin
664 Canonical_Case_File_Name (Archive_Name);
665 Canonical_Case_File_Name (DLL_Name);
667 Change_Dir (Lib_Directory);
668 Open (Direc, ".");
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
672 -- the library file.
674 loop
675 Read (Direc, Name, Last);
676 exit when Last = 0;
678 declare
679 Filename : constant String := Name (1 .. Last);
681 begin
682 if Is_Regular_File (Filename)
683 or else Is_Symbolic_Link (Filename)
684 then
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)
690 or else
691 ((Project.Library_Kind = Dynamic
692 or else
693 Project.Library_Kind = Relocatable)
694 and then
695 (Name (1 .. Last) = DLL_Name
696 or else
697 Name (1 .. Last) = Minor.all
698 or else
699 Name (1 .. Last) = Major.all))
700 then
701 if not Do_Nothing then
702 Set_Writable (Filename);
703 end if;
705 Delete (Lib_Directory, Filename);
706 end if;
707 end if;
708 end;
709 end loop;
711 Close (Direc);
713 Change_Dir (Lib_ALI_Directory);
714 Open (Direc, ".");
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.
720 loop
721 Read (Direc, Name, Last);
722 exit when Last = 0;
724 declare
725 Filename : constant String := Name (1 .. Last);
726 begin
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
732 declare
733 Unit : Unit_Index;
734 begin
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 /=
742 No_Project
743 then
744 if Ultimate_Extending_Project_Of
745 (Unit.File_Names (Impl).Project) =
746 Project
747 then
748 Get_Name_String
749 (Unit.File_Names (Impl).File);
750 Name_Len := Name_Len -
751 File_Extension
752 (Name (1 .. Name_Len))'Length;
753 if Name_Buffer (1 .. Name_Len) =
754 Name (1 .. Last - 4)
755 then
756 Delete_File := True;
757 exit;
758 end if;
759 end if;
761 elsif Unit.File_Names (Spec) /= null
762 and then Ultimate_Extending_Project_Of
763 (Unit.File_Names (Spec).Project) =
764 Project
765 then
766 Get_Name_String
767 (Unit.File_Names (Spec).File);
768 Name_Len :=
769 Name_Len -
770 File_Extension
771 (Name (1 .. Name_Len))'Length;
773 if Name_Buffer (1 .. Name_Len) =
774 Name (1 .. Last - 4)
775 then
776 Delete_File := True;
777 exit;
778 end if;
779 end if;
781 Unit :=
782 Units_Htable.Get_Next (Project_Tree.Units_HT);
783 end loop;
784 end;
785 end if;
787 if Delete_File then
788 if not Do_Nothing then
789 Set_Writable (Filename);
790 end if;
792 Delete (Lib_ALI_Directory, Filename);
793 end if;
794 end if;
795 end;
796 end loop;
798 Close (Direc);
800 -- Restore the initial working directory
802 Change_Dir (Current);
803 end;
804 end if;
805 end Clean_Library_Directory;
807 -------------------
808 -- Clean_Project --
809 -------------------
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;
819 Unit : Unit_Index;
820 File_Name1 : File_Name_Type;
821 Index1 : Int;
822 File_Name2 : File_Name_Type;
823 Index2 : Int;
824 Lib_File : File_Name_Type;
826 Global_Archive : Boolean := False;
828 begin
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
835 then
836 Osint.Fail
837 ("Cannot specify executable(s) for a Library Project File");
838 end if;
840 -- Nothing to clean in an externally built project
842 if Project.Externally_Built then
843 if Verbose_Mode then
844 Put ("Nothing to do to clean externally built project """);
845 Put (Get_Name_String (Project.Name));
846 Put_Line ("""");
847 end if;
849 else
850 if Verbose_Mode then
851 Put ("Cleaning project """);
852 Put (Get_Name_String (Project.Name));
853 Put_Line ("""");
854 end if;
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
862 declare
863 Obj_Dir : constant String :=
864 Get_Name_String (Project.Object_Directory.Display_Name);
866 begin
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
879 then
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
887 -- object directory.
889 if (Unit.File_Names (Impl) /= null
890 and then
891 In_Extension_Chain
892 (Unit.File_Names (Impl).Project, Project))
893 or else
894 (Unit.File_Names (Spec) /= null
895 and then In_Extension_Chain
896 (Unit.File_Names (Spec).Project, Project))
897 then
898 if Unit.File_Names (Impl) /= null then
899 File_Name1 := Unit.File_Names (Impl).File;
900 Index1 := Unit.File_Names (Impl).Index;
901 else
902 File_Name1 := No_File;
903 Index1 := 0;
904 end if;
906 if Unit.File_Names (Spec) /= null then
907 File_Name2 := Unit.File_Names (Spec).File;
908 Index2 := Unit.File_Names (Spec).Index;
909 else
910 File_Name2 := No_File;
911 Index2 := 0;
912 end if;
914 -- If there is no body file name, then there may be
915 -- only a spec.
917 if File_Name1 = No_File then
918 File_Name1 := File_Name2;
919 Index1 := Index2;
920 File_Name2 := No_File;
921 Index2 := 0;
922 end if;
923 end if;
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);
931 declare
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;
946 begin
947 -- If the ALI file exists and is read-only, no file
948 -- is deleted.
950 if Is_Regular_File (ALI) then
951 if Is_Writable_File (ALI) then
952 Delete (Obj_Dir, ALI);
954 else
955 Del := False;
957 if Verbose_Mode then
958 Put ('"');
959 Put (Obj_Dir);
961 if Obj_Dir (Obj_Dir'Last) /=
962 Dir_Separator
963 then
964 Put (Dir_Separator);
965 end if;
967 Put (ALI);
968 Put_Line (""" is read-only");
969 end if;
970 end if;
971 end if;
973 if Del then
975 -- Object file
977 if Is_Regular_File (Obj) then
978 Delete (Obj_Dir, Obj);
979 end if;
981 -- Assembly file
983 if Is_Regular_File (Asm) then
984 Delete (Obj_Dir, Asm);
985 end if;
987 -- Tree file
989 if Is_Regular_File (Adt) then
990 Delete (Obj_Dir, Adt);
991 end if;
993 -- First expanded source file
995 if Is_Regular_File (Deb) then
996 Delete (Obj_Dir, Deb);
997 end if;
999 -- Repinfo file
1001 if Is_Regular_File (Rep) then
1002 Delete (Obj_Dir, Rep);
1003 end if;
1005 -- Second expanded source file
1007 if File_Name2 /= No_File then
1008 declare
1009 Deb : constant String :=
1010 Debug_File_Name (File_Name2);
1011 Rep : constant String :=
1012 Repinfo_File_Name (File_Name2);
1014 begin
1015 if Is_Regular_File (Deb) then
1016 Delete (Obj_Dir, Deb);
1017 end if;
1019 if Is_Regular_File (Rep) then
1020 Delete (Obj_Dir, Rep);
1021 end if;
1022 end;
1023 end if;
1024 end if;
1025 end;
1026 end if;
1028 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
1029 end loop;
1030 end if;
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;
1038 declare
1039 Proj : Project_List;
1041 begin
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
1052 -- sources.
1054 if Proj.Project.Languages /= null
1055 and then Proj.Project.Languages.Next /= null
1056 then
1057 Global_Archive := True;
1058 exit;
1059 end if;
1061 Proj := Proj.Next;
1062 end loop;
1063 end;
1065 if Global_Archive then
1066 Clean_Archive (Project, Global => True);
1067 end if;
1068 end if;
1070 end;
1071 end if;
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);
1085 end if;
1086 end if;
1088 if Project.Standalone_Library /= No
1089 and then Project.Object_Directory /= No_Path_Information
1090 then
1091 Delete_Binder_Generated_Files
1092 (Get_Name_String (Project.Object_Directory.Display_Name),
1093 File_Name_Type (Project.Library_Name));
1094 end if;
1095 end if;
1097 if Verbose_Mode then
1098 New_Line;
1099 end if;
1100 end if;
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
1106 declare
1107 Imported : Project_List;
1108 Process : Boolean;
1110 begin
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
1116 Process := True;
1119 J in Processed_Projects.First .. Processed_Projects.Last
1120 loop
1121 if Imported.Project = Processed_Projects.Table (J) then
1122 Process := False;
1123 exit;
1124 end if;
1125 end loop;
1127 if Process then
1128 Clean_Project (Imported.Project);
1129 end if;
1131 Imported := Imported.Next;
1132 end loop;
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
1137 -- this project.
1139 if Project.Extends /= No_Project then
1140 Clean_Project (Project.Extends);
1141 end if;
1142 end;
1143 end if;
1145 -- For the main project, delete the executables and the binder
1146 -- generated files.
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
1152 then
1153 declare
1154 Exec_Dir : constant String :=
1155 Get_Name_String (Project.Exec_Directory.Display_Name);
1157 begin
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
1164 Executable :=
1165 Executable_Of
1166 (Main_Project,
1167 Project_Tree.Shared,
1168 Main_Source_File,
1169 Current_File_Index);
1171 declare
1172 Exec_File_Name : constant String :=
1173 Get_Name_String (Executable);
1175 begin
1176 if Is_Absolute_Path (Name => Exec_File_Name) then
1177 if Is_Regular_File (Exec_File_Name) then
1178 Delete ("", Exec_File_Name);
1179 end if;
1181 else
1182 if Is_Regular_File (Exec_File_Name) then
1183 Delete (Exec_Dir, Exec_File_Name);
1184 end if;
1185 end if;
1186 end;
1187 end if;
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));
1193 end if;
1194 end loop;
1195 end;
1196 end if;
1198 -- Change back to previous directory
1200 Change_Dir (Current_Dir);
1201 end Clean_Project;
1203 ---------------------
1204 -- Debug_File_Name --
1205 ---------------------
1207 function Debug_File_Name (Source : File_Name_Type) return String is
1208 begin
1209 return Get_Name_String (Source) & Debug_Suffix;
1210 end Debug_File_Name;
1212 ------------
1213 -- Delete --
1214 ------------
1216 procedure Delete (In_Directory : String; File : String) is
1217 Full_Name : String (1 .. In_Directory'Length + File'Length + 1);
1218 Last : Natural := 0;
1219 Success : Boolean;
1221 begin
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
1232 Last := Last + 1;
1233 Full_Name (Last) := Directory_Separator;
1234 end if;
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
1241 if Do_Nothing then
1242 Put_Line (Full_Name (1 .. Last));
1244 -- Otherwise, delete the file if it is writable
1246 else
1247 if Force_Deletions
1248 or else Is_Writable_File (Full_Name (1 .. Last))
1249 or else Is_Symbolic_Link (Full_Name (1 .. Last))
1250 then
1251 -- On VMS, we have to delete all versions of the file
1253 if OpenVMS_On_Target then
1254 declare
1255 Host_Full_Name : constant String_Access :=
1256 To_Host_File_Spec (Full_Name (1 .. Last));
1257 begin
1258 if Host_Full_Name = null
1259 or else Host_Full_Name'Length = 0
1260 then
1261 Success := False;
1262 else
1263 Delete_File (Host_Full_Name.all & ";*", Success);
1264 end if;
1265 end;
1267 -- Otherwise just delete the specified file
1269 else
1270 Delete_File (Full_Name (1 .. Last), Success);
1271 end if;
1273 -- Here if no deletion required
1275 else
1276 Success := False;
1277 end if;
1279 if Verbose_Mode or else not Quiet_Output then
1280 if not Success then
1281 Put ("Warning: """);
1282 Put (Full_Name (1 .. Last));
1283 Put_Line (""" could not be deleted");
1285 else
1286 Put ("""");
1287 Put (Full_Name (1 .. Last));
1288 Put_Line (""" has been deleted");
1289 end if;
1290 end if;
1291 end if;
1292 end Delete;
1294 -----------------------------------
1295 -- Delete_Binder_Generated_Files --
1296 -----------------------------------
1298 procedure Delete_Binder_Generated_Files
1299 (Dir : String;
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);
1307 begin
1308 Change_Dir (Dir);
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;
1315 -- Spec
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));
1321 end if;
1323 -- Body
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));
1329 end if;
1331 -- ALI file
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));
1337 end if;
1339 -- Object file
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));
1345 end if;
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
1357 begin
1358 if not Copyright_Displayed then
1359 Copyright_Displayed := True;
1360 Display_Version ("GNATCLEAN", "2003");
1361 end if;
1362 end Display_Copyright;
1364 ---------------
1365 -- Gnatclean --
1366 ---------------
1368 procedure Gnatclean is
1369 begin
1370 -- Do the necessary initializations
1372 Clean.Initialize;
1374 -- Parse the command line, getting the switches and the executable names
1376 Parse_Cmd_Line;
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
1386 Display_Copyright;
1387 end if;
1389 if Project_File_Name /= null then
1391 -- A project file was specified by a -P switch
1393 if Opt.Verbose_Mode then
1394 New_Line;
1395 Put ("Parsing Project File """);
1396 Put (Project_File_Name.all);
1397 Put_Line (""".");
1398 New_Line;
1399 end if;
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.
1409 Prj.Pars.Parse
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");
1419 end if;
1421 if Opt.Verbose_Mode then
1422 New_Line;
1423 Put ("Parsing of Project File """);
1424 Put (Project_File_Name.all);
1425 Put (""" is finished.");
1426 New_Line;
1427 end if;
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);
1433 end if;
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
1439 -- command line.
1441 if Main_Project /= No_Project and then Osint.Number_Of_Files = 0 then
1442 declare
1443 Main : String_Element;
1444 Value : String_List_Id := Main_Project.Mains;
1445 begin
1446 while Value /= Prj.Nil_String loop
1447 Main := Project_Tree.Shared.String_Elements.Table (Value);
1448 Osint.Add_File
1449 (File_Name => Get_Name_String (Main.Value),
1450 Index => Main.Index);
1451 Value := Main.Next;
1452 end loop;
1453 end;
1454 end if;
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
1460 Usage;
1461 return;
1462 end if;
1464 if Verbose_Mode then
1465 New_Line;
1466 end if;
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);
1477 else
1478 -- If no project file has been specified, the work is done in
1479 -- Clean_Executables.
1481 Clean_Executables;
1482 end if;
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
1488 New_Line;
1490 if Do_Nothing then
1491 Put_Line ("No file needs to be deleted");
1492 else
1493 Put_Line ("No file has been deleted");
1494 end if;
1495 end if;
1496 end Gnatclean;
1498 ------------------------
1499 -- In_Extension_Chain --
1500 ------------------------
1502 function In_Extension_Chain
1503 (Of_Project : Project_Id;
1504 Prj : Project_Id) return Boolean
1506 Proj : Project_Id;
1508 begin
1509 if Prj = No_Project or else Of_Project = No_Project then
1510 return False;
1511 end if;
1513 if Of_Project = Prj then
1514 return True;
1515 end if;
1517 Proj := Of_Project;
1518 while Proj.Extends /= No_Project loop
1519 if Proj.Extends = Prj then
1520 return True;
1521 end if;
1523 Proj := Proj.Extends;
1524 end loop;
1526 Proj := Prj;
1527 while Proj.Extends /= No_Project loop
1528 if Proj.Extends = Of_Project then
1529 return True;
1530 end if;
1532 Proj := Proj.Extends;
1533 end loop;
1535 return False;
1536 end In_Extension_Chain;
1538 ----------------
1539 -- Initialize --
1540 ----------------
1542 procedure Initialize is
1543 begin
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
1554 Csets.Initialize;
1555 Snames.Initialize;
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__");
1572 end if;
1573 end if;
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;
1585 end Initialize;
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);
1594 begin
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;
1601 end if;
1602 end loop;
1604 -- If there is no dot, or if it is the first character, just add the
1605 -- ALI suffix.
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;
1617 Index : Positive;
1619 procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
1621 begin
1622 -- First, check for --version and --help
1624 Check_Version_And_Help ("GNATCLEAN", "2003");
1626 Index := 1;
1627 while Index <= Last loop
1628 declare
1629 Arg : constant String := Argument (Index);
1631 procedure Bad_Argument;
1632 -- Signal bad argument
1634 ------------------
1635 -- Bad_Argument --
1636 ------------------
1638 procedure Bad_Argument is
1639 begin
1640 Fail ("invalid argument """ & Arg & """");
1641 end Bad_Argument;
1643 begin
1644 if Arg'Length /= 0 then
1645 if Arg (1) = '-' then
1646 if Arg'Length = 1 then
1647 Bad_Argument;
1648 end if;
1650 case Arg (2) is
1651 when '-' =>
1652 if Arg'Length > Subdirs_Option'Length and then
1653 Arg (1 .. Subdirs_Option'Length) = Subdirs_Option
1654 then
1655 Subdirs :=
1656 new String'
1657 (Arg (Subdirs_Option'Length + 1 .. Arg'Last));
1659 elsif Arg = Makeutl.Unchecked_Shared_Lib_Imports then
1660 Opt.Unchecked_Shared_Lib_Imports := True;
1662 else
1663 Bad_Argument;
1664 end if;
1666 when 'a' =>
1667 if Arg'Length < 4 then
1668 Bad_Argument;
1669 end if;
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));
1679 else
1680 Bad_Argument;
1681 end if;
1683 when 'c' =>
1684 Compile_Only := True;
1686 when 'D' =>
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");
1692 end if;
1694 if Arg'Length > 2 then
1695 declare
1696 Dir : constant String := Arg (3 .. Arg'Last);
1697 begin
1698 if not Is_Directory (Dir) then
1699 Fail (Dir & " is not a directory");
1700 else
1701 Add_Lib_Search_Dir (Dir);
1702 end if;
1703 end;
1705 else
1706 if Index = Last then
1707 Fail ("no directory specified after -D");
1708 end if;
1710 Index := Index + 1;
1712 declare
1713 Dir : constant String := Argument (Index);
1714 begin
1715 if not Is_Directory (Dir) then
1716 Fail (Dir & " is not a directory");
1717 else
1718 Add_Lib_Search_Dir (Dir);
1719 end if;
1720 end;
1721 end if;
1723 when 'e' =>
1724 if Arg = "-eL" then
1725 Follow_Links_For_Files := True;
1726 Follow_Links_For_Dirs := True;
1728 else
1729 Bad_Argument;
1730 end if;
1732 when 'f' =>
1733 Force_Deletions := True;
1734 Directories_Must_Exist_In_Projects := False;
1736 when 'F' =>
1737 Full_Path_Name_For_Brief_Errors := True;
1739 when 'h' =>
1740 Usage;
1742 when 'i' =>
1743 if Arg'Length = 2 then
1744 Bad_Argument;
1745 end if;
1747 Source_Index := 0;
1749 for J in 3 .. Arg'Last loop
1750 if Arg (J) not in '0' .. '9' then
1751 Bad_Argument;
1752 end if;
1754 Source_Index :=
1755 (20 * Source_Index) +
1756 (Character'Pos (Arg (J)) - Character'Pos ('0'));
1757 end loop;
1759 when 'I' =>
1760 if Arg = "-I-" then
1761 Opt.Look_In_Primary_Dir := False;
1763 else
1764 if Arg'Length = 2 then
1765 Bad_Argument;
1766 end if;
1768 Add_Lib_Search_Dir (Arg (3 .. Arg'Last));
1769 end if;
1771 when 'n' =>
1772 Do_Nothing := True;
1774 when 'P' =>
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");
1781 end if;
1783 if Arg'Length > 2 then
1784 declare
1785 Prj : constant String := Arg (3 .. Arg'Last);
1786 begin
1787 if Prj'Length > 1 and then
1788 Prj (Prj'First) = '='
1789 then
1790 Project_File_Name :=
1791 new String'
1792 (Prj (Prj'First + 1 .. Prj'Last));
1793 else
1794 Project_File_Name := new String'(Prj);
1795 end if;
1796 end;
1798 else
1799 if Index = Last then
1800 Fail ("no project specified after -P");
1801 end if;
1803 Index := Index + 1;
1804 Project_File_Name := new String'(Argument (Index));
1805 end if;
1807 when 'q' =>
1808 Quiet_Output := True;
1810 when 'r' =>
1811 All_Projects := True;
1813 when 'v' =>
1814 if Arg = "-v" then
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;
1826 else
1827 Bad_Argument;
1828 end if;
1830 when 'X' =>
1831 if Arg'Length = 2 then
1832 Bad_Argument;
1833 end if;
1835 declare
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;
1841 begin
1842 if Ext_Asgn (Start) = '"' then
1843 if Ext_Asgn (Stop) = '"' then
1844 Start := Start + 1;
1845 Stop := Stop - 1;
1847 else
1848 OK := False;
1849 end if;
1850 end if;
1852 if not OK
1853 or else not
1854 Prj.Ext.Check (Root_Environment.External,
1855 Ext_Asgn (Start .. Stop))
1856 then
1857 Fail
1858 ("illegal external assignment '"
1859 & Ext_Asgn
1860 & "'");
1861 end if;
1862 end;
1864 when others =>
1865 Bad_Argument;
1866 end case;
1868 else
1869 Add_File (Arg, Source_Index);
1870 end if;
1871 end if;
1872 end;
1874 Index := Index + 1;
1875 end loop;
1876 end Parse_Cmd_Line;
1878 -----------------------
1879 -- Repinfo_File_Name --
1880 -----------------------
1882 function Repinfo_File_Name (Source : File_Name_Type) return String is
1883 begin
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);
1894 begin
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;
1900 end if;
1901 end loop;
1903 -- If there is no dot, or if it is the first character, just add the
1904 -- tree suffix.
1906 return Src & Tree_Suffix;
1907 end Tree_File_Name;
1909 -----------
1910 -- Usage --
1911 -----------
1913 procedure Usage is
1914 begin
1915 if not Usage_Displayed then
1916 Usage_Displayed := True;
1917 Display_Copyright;
1918 Put_Line ("Usage: gnatclean [switches] {[-innn] name}");
1919 New_Line;
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");
1926 New_Line;
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");
1931 New_Line;
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 " &
1936 "project files");
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");
1951 New_Line;
1953 Put_Line (" -aPdir Add directory dir to project search path");
1954 New_Line;
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");
1960 New_Line;
1961 end if;
1962 end Usage;
1964 end Clean;