OSDN Git Service

* gcc.dg/vect/O3-vect-pr34223.c: Check vect_int_mult.
[pf3gnuchains/gcc-fork.git] / gcc / ada / clean.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                                C L E A N                                 --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 2003-2008, 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 ------------------------------------------------------------------------------
25
26 with ALI;      use ALI;
27 with Csets;
28 with 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.Util; use Prj.Util;
39 with Snames;
40 with Switch;   use Switch;
41 with Table;
42 with Targparm; use Targparm;
43 with Types;    use Types;
44
45 with Ada.Command_Line;          use Ada.Command_Line;
46
47 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
48 with GNAT.IO;                   use GNAT.IO;
49 with GNAT.OS_Lib;               use GNAT.OS_Lib;
50
51 package body Clean is
52
53    Initialized : Boolean := False;
54    --  Set to True by the first call to Initialize.
55    --  To avoid reinitialization of some packages.
56
57    --  Suffixes of various files
58
59    Assembly_Suffix : constant String := ".s";
60    ALI_Suffix      : constant String := ".ali";
61    Tree_Suffix     : constant String := ".adt";
62    Object_Suffix   : constant String := Get_Target_Object_Suffix.all;
63    Debug_Suffix    : String          := ".dg";
64    --  Changed to "_dg" for VMS in the body of the package
65
66    Repinfo_Suffix  : String := ".rep";
67    --  Changed to "_rep" for VMS in the body of the package
68
69    B_Start : String_Ptr := new String'("b~");
70    --  Prefix of binder generated file, and number of actual characters used.
71    --  Changed to "b__" for VMS in the body of the package.
72
73    Object_Directory_Path : String_Access := null;
74    --  The path name of the object directory, set with switch -D
75
76    Force_Deletions : Boolean := False;
77    --  Set to True by switch -f. When True, attempts to delete non writable
78    --  files will be done.
79
80    Do_Nothing : Boolean := False;
81    --  Set to True when switch -n is specified. When True, no file is deleted.
82    --  gnatclean only lists the files that would have been deleted if the
83    --  switch -n had not been specified.
84
85    File_Deleted : Boolean := False;
86    --  Set to True if at least one file has been deleted
87
88    Copyright_Displayed : Boolean := False;
89    Usage_Displayed     : Boolean := False;
90
91    Project_File_Name : String_Access := null;
92
93    Project_Tree : constant Prj.Project_Tree_Ref := new Prj.Project_Tree_Data;
94
95    Main_Project : Prj.Project_Id := Prj.No_Project;
96
97    All_Projects : Boolean := False;
98
99    --  Packages of project files where unknown attributes are errors
100
101    Naming_String   : aliased String := "naming";
102    Builder_String  : aliased String := "builder";
103    Compiler_String : aliased String := "compiler";
104    Binder_String   : aliased String := "binder";
105    Linker_String   : aliased String := "linker";
106
107    Gnatmake_Packages : aliased String_List :=
108      (Naming_String   'Access,
109       Builder_String  'Access,
110       Compiler_String 'Access,
111       Binder_String   'Access,
112       Linker_String   'Access);
113
114    Packages_To_Check_By_Gnatmake : constant String_List_Access :=
115      Gnatmake_Packages'Access;
116
117    package Processed_Projects is new Table.Table
118      (Table_Component_Type => Project_Id,
119       Table_Index_Type     => Natural,
120       Table_Low_Bound      => 0,
121       Table_Initial        => 10,
122       Table_Increment      => 100,
123       Table_Name           => "Clean.Processed_Projects");
124    --  Table to keep track of what project files have been processed, when
125    --  switch -r is specified.
126
127    package Sources is new Table.Table
128      (Table_Component_Type => File_Name_Type,
129       Table_Index_Type     => Natural,
130       Table_Low_Bound      => 0,
131       Table_Initial        => 10,
132       Table_Increment      => 100,
133       Table_Name           => "Clean.Processed_Projects");
134    --  Table to store all the source files of a library unit: spec, body and
135    --  subunits, to detect .dg files and delete them.
136
137    ----------------------------
138    -- Queue (Q) manipulation --
139    ----------------------------
140
141    procedure Init_Q;
142    --  Must be called to initialize the Q
143
144    procedure Insert_Q (Lib_File  : File_Name_Type);
145    --  If Lib_File is not marked, inserts it at the end of Q and mark it
146
147    function Empty_Q return Boolean;
148    --  Returns True if Q is empty
149
150    procedure Extract_From_Q (Lib_File : out File_Name_Type);
151    --  Extracts the first element from the Q
152
153    Q_Front : Natural;
154    --  Points to the first valid element in the Q
155
156    package Q is new Table.Table (
157      Table_Component_Type => File_Name_Type,
158      Table_Index_Type     => Natural,
159      Table_Low_Bound      => 0,
160      Table_Initial        => 4000,
161      Table_Increment      => 100,
162      Table_Name           => "Clean.Q");
163    --  This is the actual queue
164
165    -----------------------------
166    -- Other local subprograms --
167    -----------------------------
168
169    procedure Add_Source_Dir (N : String);
170    --  Call Add_Src_Search_Dir and output one line when in verbose mode
171
172    procedure Add_Source_Directories is
173      new Prj.Env.For_All_Source_Dirs (Action => Add_Source_Dir);
174
175    procedure Add_Object_Dir (N : String);
176    --  Call Add_Lib_Search_Dir and output one line when in verbose mode
177
178    procedure Add_Object_Directories is
179      new Prj.Env.For_All_Object_Dirs (Action => Add_Object_Dir);
180
181    function ALI_File_Name (Source : File_Name_Type) return String;
182    --  Returns the name of the ALI file corresponding to Source
183
184    function Assembly_File_Name (Source : File_Name_Type) return String;
185    --  Returns the assembly file name corresponding to Source
186
187    procedure Clean_Archive (Project : Project_Id; Global : Boolean);
188    --  Delete a global archive or library project archive and the dependency
189    --  file, if they exist.
190
191    procedure Clean_Executables;
192    --  Do the cleaning work when no project file is specified
193
194    procedure Clean_Interface_Copy_Directory (Project : Project_Id);
195    --  Delete files in an interface copy directory: any file that is a copy of
196    --  a source of the project.
197
198    procedure Clean_Library_Directory (Project : Project_Id);
199    --  Delete the library file in a library directory and any ALI file of a
200    --  source of the project in a library ALI directory.
201
202    procedure Clean_Project (Project : Project_Id);
203    --  Do the cleaning work when a project file is specified. This procedure
204    --  calls itself recursively when there are several project files in the
205    --  tree rooted at the main project file and switch -r has been specified.
206
207    function Debug_File_Name (Source : File_Name_Type) return String;
208    --  Name of the expanded source file corresponding to Source
209
210    procedure Delete (In_Directory : String; File : String);
211    --  Delete one file, or list the file name if switch -n is specified
212
213    procedure Delete_Binder_Generated_Files
214      (Dir    : String;
215       Source : File_Name_Type);
216    --  Delete the binder generated file in directory Dir for Source, if they
217    --  exist: for Unix these are b~<source>.ads, b~<source>.adb,
218    --  b~<source>.ali and b~<source>.o.
219
220    procedure Display_Copyright;
221    --  Display the Copyright notice. If called several times, display the
222    --  Copyright notice only the first time.
223
224    procedure Initialize;
225    --  Call the necessary package initializations
226
227    function Object_File_Name (Source : File_Name_Type) return String;
228    --  Returns the object file name corresponding to Source
229
230    procedure Parse_Cmd_Line;
231    --  Parse the command line
232
233    function Repinfo_File_Name (Source : File_Name_Type) return String;
234    --  Returns the repinfo file name corresponding to Source
235
236    function Tree_File_Name (Source : File_Name_Type) return String;
237    --  Returns the tree file name corresponding to Source
238
239    function In_Extension_Chain
240      (Of_Project : Project_Id;
241       Prj        : Project_Id) return Boolean;
242    --  Returns True iff Prj is an extension of Of_Project or if Of_Project is
243    --  an extension of Prj.
244
245    function Ultimate_Extension_Of (Project : Project_Id) return Project_Id;
246    --  Returns either Project, if it is not extended by another project, or
247    --  the project that extends Project, directly or indirectly, and that is
248    --  not itself extended. Returns No_Project if Project is No_Project.
249
250    procedure Usage;
251    --  Display the usage. If called several times, the usage is displayed only
252    --  the first time.
253
254    --------------------
255    -- Add_Object_Dir --
256    --------------------
257
258    procedure Add_Object_Dir (N : String) is
259    begin
260       Add_Lib_Search_Dir (N);
261
262       if Opt.Verbose_Mode then
263          Put ("Adding object directory """);
264          Put (N);
265          Put (""".");
266          New_Line;
267       end if;
268    end Add_Object_Dir;
269
270    --------------------
271    -- Add_Source_Dir --
272    --------------------
273
274    procedure Add_Source_Dir (N : String) is
275    begin
276       Add_Src_Search_Dir (N);
277
278       if Opt.Verbose_Mode then
279          Put ("Adding source directory """);
280          Put (N);
281          Put (""".");
282          New_Line;
283       end if;
284    end Add_Source_Dir;
285
286    -------------------
287    -- ALI_File_Name --
288    -------------------
289
290    function ALI_File_Name (Source : File_Name_Type) return String is
291       Src : constant String := Get_Name_String (Source);
292
293    begin
294       --  If the source name has an extension, then replace it with
295       --  the ALI suffix.
296
297       for Index in reverse Src'First + 1 .. Src'Last loop
298          if Src (Index) = '.' then
299             return Src (Src'First .. Index - 1) & ALI_Suffix;
300          end if;
301       end loop;
302
303       --  If there is no dot, or if it is the first character, just add the
304       --  ALI suffix.
305
306       return Src & ALI_Suffix;
307    end ALI_File_Name;
308
309    ------------------------
310    -- Assembly_File_Name --
311    ------------------------
312
313    function Assembly_File_Name (Source : File_Name_Type) return String is
314       Src : constant String := Get_Name_String (Source);
315
316    begin
317       --  If the source name has an extension, then replace it with
318       --  the assembly suffix.
319
320       for Index in reverse Src'First + 1 .. Src'Last loop
321          if Src (Index) = '.' then
322             return Src (Src'First .. Index - 1) & Assembly_Suffix;
323          end if;
324       end loop;
325
326       --  If there is no dot, or if it is the first character, just add the
327       --  assembly suffix.
328
329       return Src & Assembly_Suffix;
330    end Assembly_File_Name;
331
332    -------------------
333    -- Clean_Archive --
334    -------------------
335
336    procedure Clean_Archive (Project : Project_Id; Global : Boolean) is
337       Current_Dir : constant Dir_Name_Str := Get_Current_Dir;
338
339       Data : constant Project_Data := Project_Tree.Projects.Table (Project);
340
341       Lib_Prefix : String_Access;
342       Archive_Name : String_Access;
343       --  The name of the archive file for this project
344
345       Archive_Dep_Name : String_Access;
346       --  The name of the archive dependency file for this project
347
348       Obj_Dir : constant String :=
349                   Get_Name_String (Data.Object_Directory.Display_Name);
350
351    begin
352       Change_Dir (Obj_Dir);
353
354       --  First, get the lib prefix, the archive file name and the archive
355       --  dependency file name.
356
357       if Global then
358          Lib_Prefix :=
359            new String'("lib" & Get_Name_String (Data.Display_Name));
360       else
361          Lib_Prefix :=
362            new String'("lib" & Get_Name_String (Data.Library_Name));
363       end if;
364
365       Archive_Name := new String'(Lib_Prefix.all & '.' & Archive_Ext);
366       Archive_Dep_Name := new String'(Lib_Prefix.all & ".deps");
367
368       --  Delete the archive file and the archive dependency file, if they
369       --  exist.
370
371       if Is_Regular_File (Archive_Name.all) then
372          Delete (Obj_Dir, Archive_Name.all);
373       end if;
374
375       if Is_Regular_File (Archive_Dep_Name.all) then
376          Delete (Obj_Dir, Archive_Dep_Name.all);
377       end if;
378
379       Change_Dir (Current_Dir);
380    end Clean_Archive;
381
382    -----------------------
383    -- Clean_Executables --
384    -----------------------
385
386    procedure Clean_Executables is
387       Main_Source_File : File_Name_Type;
388       --  Current main source
389
390       Main_Lib_File : File_Name_Type;
391       --  ALI file of the current main
392
393       Lib_File : File_Name_Type;
394       --  Current ALI file
395
396       Full_Lib_File : File_Name_Type;
397       --  Full name of the current ALI file
398
399       Text    : Text_Buffer_Ptr;
400       The_ALI : ALI_Id;
401
402    begin
403       Init_Q;
404
405       --  It does not really matter if there is or not an object file
406       --  corresponding to an ALI file: if there is one, it will be deleted.
407
408       Opt.Check_Object_Consistency := False;
409
410       --  Proceed each executable one by one. Each source is marked as it is
411       --  processed, so common sources between executables will not be
412       --  processed several times.
413
414       for N_File in 1 .. Osint.Number_Of_Files loop
415          Main_Source_File := Next_Main_Source;
416          Main_Lib_File := Osint.Lib_File_Name
417                              (Main_Source_File, Current_File_Index);
418          Insert_Q (Main_Lib_File);
419
420          while not Empty_Q loop
421             Sources.Set_Last (0);
422             Extract_From_Q (Lib_File);
423             Full_Lib_File := Osint.Full_Lib_File_Name (Lib_File);
424
425             --  If we have existing ALI file that is not read-only, process it
426
427             if Full_Lib_File /= No_File
428               and then not Is_Readonly_Library (Full_Lib_File)
429             then
430                Text := Read_Library_Info (Lib_File);
431
432                if Text /= null then
433                   The_ALI :=
434                     Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
435                   Free (Text);
436
437                   --  If no error was produced while loading this ALI file,
438                   --  insert into the queue all the unmarked withed sources.
439
440                   if The_ALI /= No_ALI_Id then
441                      for J in ALIs.Table (The_ALI).First_Unit ..
442                        ALIs.Table (The_ALI).Last_Unit
443                      loop
444                         Sources.Increment_Last;
445                         Sources.Table (Sources.Last) :=
446                           ALI.Units.Table (J).Sfile;
447
448                         for K in ALI.Units.Table (J).First_With ..
449                           ALI.Units.Table (J).Last_With
450                         loop
451                            Insert_Q (Withs.Table (K).Afile);
452                         end loop;
453                      end loop;
454
455                      --  Look for subunits and put them in the Sources table
456
457                      for J in ALIs.Table (The_ALI).First_Sdep ..
458                        ALIs.Table (The_ALI).Last_Sdep
459                      loop
460                         if Sdep.Table (J).Subunit_Name /= No_Name then
461                            Sources.Increment_Last;
462                            Sources.Table (Sources.Last) :=
463                              Sdep.Table (J).Sfile;
464                         end if;
465                      end loop;
466                   end if;
467                end if;
468
469                --  Now delete all existing files corresponding to this ALI file
470
471                declare
472                   Obj_Dir : constant String :=
473                               Dir_Name (Get_Name_String (Full_Lib_File));
474                   Obj     : constant String := Object_File_Name (Lib_File);
475                   Adt     : constant String := Tree_File_Name   (Lib_File);
476                   Asm     : constant String := Assembly_File_Name (Lib_File);
477
478                begin
479                   Delete (Obj_Dir, Get_Name_String (Lib_File));
480
481                   if Is_Regular_File (Obj_Dir & Dir_Separator & Obj) then
482                      Delete (Obj_Dir, Obj);
483                   end if;
484
485                   if Is_Regular_File (Obj_Dir & Dir_Separator & Adt) then
486                      Delete (Obj_Dir, Adt);
487                   end if;
488
489                   if Is_Regular_File (Obj_Dir & Dir_Separator & Asm) then
490                      Delete (Obj_Dir, Asm);
491                   end if;
492
493                   --  Delete expanded source files (.dg) and/or repinfo files
494                   --  (.rep) if any
495
496                   for J in 1 .. Sources.Last loop
497                      declare
498                         Deb : constant String :=
499                                 Debug_File_Name (Sources.Table (J));
500                         Rep : constant String :=
501                                 Repinfo_File_Name (Sources.Table (J));
502
503                      begin
504                         if Is_Regular_File (Obj_Dir & Dir_Separator & Deb) then
505                            Delete (Obj_Dir, Deb);
506                         end if;
507
508                         if Is_Regular_File (Obj_Dir & Dir_Separator & Rep) then
509                            Delete (Obj_Dir, Rep);
510                         end if;
511                      end;
512                   end loop;
513                end;
514             end if;
515          end loop;
516
517          --  Delete the executable, if it exists, and the binder generated
518          --  files, if any.
519
520          if not Compile_Only then
521             declare
522                Source     : constant File_Name_Type :=
523                               Strip_Suffix (Main_Lib_File);
524                Executable : constant String :=
525                               Get_Name_String (Executable_Name (Source));
526             begin
527                if Is_Regular_File (Executable) then
528                   Delete ("", Executable);
529                end if;
530
531                Delete_Binder_Generated_Files (Get_Current_Dir, Source);
532             end;
533          end if;
534       end loop;
535    end Clean_Executables;
536
537    ------------------------------------
538    -- Clean_Interface_Copy_Directory --
539    ------------------------------------
540
541    procedure Clean_Interface_Copy_Directory (Project : Project_Id) is
542       Current : constant String := Get_Current_Dir;
543       Data    : constant Project_Data := Project_Tree.Projects.Table (Project);
544
545       Direc : Dir_Type;
546
547       Name : String (1 .. 200);
548       Last : Natural;
549
550       Delete_File : Boolean;
551       Unit        : Unit_Data;
552
553    begin
554       if Data.Library and then Data.Library_Src_Dir /= No_Path_Information then
555          declare
556             Directory : constant String :=
557                           Get_Name_String (Data.Library_Src_Dir.Display_Name);
558
559          begin
560             Change_Dir (Directory);
561             Open (Direc, ".");
562
563             --  For each regular file in the directory, if switch -n has not
564             --  been specified, make it writable and delete the file if it is
565             --  a copy of a source of the project.
566
567             loop
568                Read (Direc, Name, Last);
569                exit when Last = 0;
570
571                declare
572                   Filename : constant String := Name (1 .. Last);
573
574                begin
575                   if Is_Regular_File (Filename) then
576                      Canonical_Case_File_Name (Name (1 .. Last));
577                      Delete_File := False;
578
579                      --  Compare with source file names of the project
580
581                      for Index in
582                        1 .. Unit_Table.Last (Project_Tree.Units)
583                      loop
584                         Unit := Project_Tree.Units.Table (Index);
585
586                         if Ultimate_Extension_Of
587                           (Unit.File_Names (Body_Part).Project) = Project
588                           and then
589                             Get_Name_String
590                               (Unit.File_Names (Body_Part).Name) =
591                           Name (1 .. Last)
592                         then
593                            Delete_File := True;
594                            exit;
595                         end if;
596
597                         if Ultimate_Extension_Of
598                           (Unit.File_Names (Specification).Project) = Project
599                           and then
600                             Get_Name_String
601                               (Unit.File_Names (Specification).Name) =
602                           Name (1 .. Last)
603                         then
604                            Delete_File := True;
605                            exit;
606                         end if;
607                      end loop;
608
609                      if Delete_File then
610                         if not Do_Nothing then
611                            Set_Writable (Filename);
612                         end if;
613
614                         Delete (Directory, Filename);
615                      end if;
616                   end if;
617                end;
618             end loop;
619
620             Close (Direc);
621
622             --  Restore the initial working directory
623
624             Change_Dir (Current);
625          end;
626       end if;
627    end Clean_Interface_Copy_Directory;
628
629    -----------------------------
630    -- Clean_Library_Directory --
631    -----------------------------
632
633    Empty_String : aliased String := "";
634
635    procedure Clean_Library_Directory (Project : Project_Id) is
636       Current : constant String := Get_Current_Dir;
637       Data    : constant Project_Data := Project_Tree.Projects.Table (Project);
638
639       Lib_Filename : constant String := Get_Name_String (Data.Library_Name);
640       DLL_Name     : String :=
641                        DLL_Prefix & Lib_Filename & "." & DLL_Ext;
642       Archive_Name : String :=
643                        "lib" & Lib_Filename & "." & Archive_Ext;
644       Direc        : Dir_Type;
645
646       Name : String (1 .. 200);
647       Last : Natural;
648
649       Delete_File : Boolean;
650
651       Minor : String_Access := Empty_String'Access;
652       Major : String_Access := Empty_String'Access;
653
654    begin
655       if Data.Library then
656          if Data.Library_Kind /= Static
657            and then MLib.Tgt.Library_Major_Minor_Id_Supported
658            and then Data.Lib_Internal_Name /= No_Name
659          then
660             Minor := new String'(Get_Name_String (Data.Lib_Internal_Name));
661             Major := new String'(MLib.Major_Id_Name (DLL_Name, Minor.all));
662          end if;
663
664          declare
665             Lib_Directory     : constant String :=
666                                   Get_Name_String
667                                     (Data.Library_Dir.Display_Name);
668             Lib_ALI_Directory : constant String :=
669                                   Get_Name_String
670                                     (Data.Library_ALI_Dir.Display_Name);
671
672          begin
673             Canonical_Case_File_Name (Archive_Name);
674             Canonical_Case_File_Name (DLL_Name);
675
676             Change_Dir (Lib_Directory);
677             Open (Direc, ".");
678
679             --  For each regular file in the directory, if switch -n has not
680             --  been specified, make it writable and delete the file if it is
681             --  the library file.
682
683             loop
684                Read (Direc, Name, Last);
685                exit when Last = 0;
686
687                declare
688                   Filename : constant String := Name (1 .. Last);
689                begin
690                   if Is_Regular_File (Filename)
691                     or else Is_Symbolic_Link (Filename)
692                   then
693                      Canonical_Case_File_Name (Name (1 .. Last));
694                      Delete_File := False;
695
696                      if (Data.Library_Kind = Static
697                          and then Name (1 .. Last) =  Archive_Name)
698                        or else
699                          ((Data.Library_Kind = Dynamic or else
700                              Data.Library_Kind = Relocatable)
701                           and then
702                             (Name (1 .. Last) = DLL_Name
703                              or else Name (1 .. Last) = Minor.all
704                              or else Name (1 .. Last) = Major.all))
705                      then
706                         if not Do_Nothing then
707                            Set_Writable (Filename);
708                         end if;
709
710                         Delete (Lib_Directory, Filename);
711                      end if;
712                   end if;
713                end;
714             end loop;
715
716             Close (Direc);
717
718             Change_Dir (Lib_ALI_Directory);
719             Open (Direc, ".");
720
721             --  For each regular file in the directory, if switch -n has not
722             --  been specified, make it writable and delete the file if it is
723             --  any ALI file of a source of the project.
724
725             loop
726                Read (Direc, Name, Last);
727                exit when Last = 0;
728
729                declare
730                   Filename : constant String := Name (1 .. Last);
731                begin
732                   if Is_Regular_File (Filename) then
733                      Canonical_Case_File_Name (Name (1 .. Last));
734                      Delete_File := False;
735
736                      if Last > 4 and then Name (Last - 3 .. Last) = ".ali" then
737                         declare
738                            Unit : Unit_Data;
739                         begin
740                            --  Compare with ALI file names of the project
741
742                            for
743                              Index in 1 .. Unit_Table.Last (Project_Tree.Units)
744                            loop
745                               Unit := Project_Tree.Units.Table (Index);
746
747                               if Unit.File_Names (Body_Part).Project /=
748                                 No_Project
749                               then
750                                  if  Ultimate_Extension_Of
751                                    (Unit.File_Names (Body_Part).Project) =
752                                    Project
753                                  then
754                                     Get_Name_String
755                                       (Unit.File_Names (Body_Part).Name);
756                                     Name_Len := Name_Len -
757                                       File_Extension
758                                         (Name (1 .. Name_Len))'Length;
759                                     if Name_Buffer (1 .. Name_Len) =
760                                       Name (1 .. Last - 4)
761                                     then
762                                        Delete_File := True;
763                                        exit;
764                                     end if;
765                                  end if;
766
767                               elsif Ultimate_Extension_Of
768                                 (Unit.File_Names (Specification).Project) =
769                                 Project
770                               then
771                                  Get_Name_String
772                                    (Unit.File_Names (Specification).Name);
773                                  Name_Len := Name_Len -
774                                    File_Extension
775                                      (Name (1 .. Name_Len))'Length;
776
777                                  if Name_Buffer (1 .. Name_Len) =
778                                    Name (1 .. Last - 4)
779                                  then
780                                     Delete_File := True;
781                                     exit;
782                                  end if;
783                               end if;
784                            end loop;
785                         end;
786                      end if;
787
788                      if Delete_File then
789                         if not Do_Nothing then
790                            Set_Writable (Filename);
791                         end if;
792
793                         Delete (Lib_ALI_Directory, Filename);
794                      end if;
795                   end if;
796                end;
797             end loop;
798
799             Close (Direc);
800
801             --  Restore the initial working directory
802
803             Change_Dir (Current);
804          end;
805       end if;
806    end Clean_Library_Directory;
807
808    -------------------
809    -- Clean_Project --
810    -------------------
811
812    procedure Clean_Project (Project : Project_Id) is
813       Main_Source_File : File_Name_Type;
814       --  Name of executable on the command line without directory info
815
816       Executable : File_Name_Type;
817       --  Name of the executable file
818
819       Current_Dir : constant Dir_Name_Str := Get_Current_Dir;
820       Data        : constant Project_Data :=
821                       Project_Tree.Projects.Table (Project);
822       U_Data      : Unit_Data;
823       File_Name1  : File_Name_Type;
824       Index1      : Int;
825       File_Name2  : File_Name_Type;
826       Index2      : Int;
827       Lib_File    : File_Name_Type;
828
829       Global_Archive : Boolean := False;
830
831    begin
832       --  Check that we don't specify executable on the command line for
833       --  a main library project.
834
835       if Project = Main_Project
836         and then Osint.Number_Of_Files /= 0
837         and then Data.Library
838       then
839          Osint.Fail
840            ("Cannot specify executable(s) for a Library Project File");
841       end if;
842
843       --  Nothing to clean in an externally built project
844
845       if Data.Externally_Built then
846          if Verbose_Mode then
847             Put ("Nothing to do to clean externally built project """);
848             Put (Get_Name_String (Data.Name));
849             Put_Line ("""");
850          end if;
851
852       else
853          if Verbose_Mode then
854             Put ("Cleaning project """);
855             Put (Get_Name_String (Data.Name));
856             Put_Line ("""");
857          end if;
858
859          --  Add project to the list of processed projects
860
861          Processed_Projects.Increment_Last;
862          Processed_Projects.Table (Processed_Projects.Last) := Project;
863
864          if Data.Object_Directory /= No_Path_Information then
865             declare
866                Obj_Dir : constant String :=
867                            Get_Name_String
868                              (Data.Object_Directory.Display_Name);
869
870             begin
871                Change_Dir (Obj_Dir);
872
873                --  First, deal with Ada
874
875                --  Look through the units to find those that are either
876                --  immediate sources or inherited sources of the project.
877                --  Extending projects may have no language specified, if
878                --  Source_Dirs or Source_Files is specified as an empty list,
879                --  so always look for Ada units in extending projects.
880
881                if Data.Ada_Sources_Present
882                  or else Data.Extends /= No_Project
883                then
884                   for Unit in Unit_Table.First ..
885                     Unit_Table.Last (Project_Tree.Units)
886                   loop
887                      U_Data := Project_Tree.Units.Table (Unit);
888                      File_Name1 := No_File;
889                      File_Name2 := No_File;
890
891                      --  If either the spec or the body is a source of the
892                      --  project, check for the corresponding ALI file in the
893                      --  object directory.
894
895                      if In_Extension_Chain
896                        (U_Data.File_Names (Body_Part).Project, Project)
897                        or else
898                          In_Extension_Chain
899                            (U_Data.File_Names (Specification).Project, Project)
900                      then
901                         File_Name1 := U_Data.File_Names (Body_Part).Name;
902                         Index1     := U_Data.File_Names (Body_Part).Index;
903                         File_Name2 := U_Data.File_Names (Specification).Name;
904                         Index2     := U_Data.File_Names (Specification).Index;
905
906                         --  If there is no body file name, then there may be
907                         --  only a spec.
908
909                         if File_Name1 = No_File then
910                            File_Name1 := File_Name2;
911                            Index1     := Index2;
912                            File_Name2 := No_File;
913                            Index2     := 0;
914                         end if;
915                      end if;
916
917                      --  If there is either a spec or a body, look for files
918                      --  in the object directory.
919
920                      if File_Name1 /= No_File then
921                         Lib_File := Osint.Lib_File_Name (File_Name1, Index1);
922
923                         declare
924                            Asm : constant String :=
925                                    Assembly_File_Name (Lib_File);
926                            ALI : constant String :=
927                                    ALI_File_Name      (Lib_File);
928                            Obj : constant String :=
929                                    Object_File_Name   (Lib_File);
930                            Adt : constant String :=
931                                    Tree_File_Name     (Lib_File);
932                            Deb : constant String :=
933                                    Debug_File_Name    (File_Name1);
934                            Rep : constant String :=
935                                    Repinfo_File_Name  (File_Name1);
936                            Del : Boolean := True;
937
938                         begin
939                            --  If the ALI file exists and is read-only, no file
940                            --  is deleted.
941
942                            if Is_Regular_File (ALI) then
943                               if Is_Writable_File (ALI) then
944                                  Delete (Obj_Dir, ALI);
945
946                               else
947                                  Del := False;
948
949                                  if Verbose_Mode then
950                                     Put ('"');
951                                     Put (Obj_Dir);
952
953                                     if Obj_Dir (Obj_Dir'Last) /=
954                                       Dir_Separator
955                                     then
956                                        Put (Dir_Separator);
957                                     end if;
958
959                                     Put (ALI);
960                                     Put_Line (""" is read-only");
961                                  end if;
962                               end if;
963                            end if;
964
965                            if Del then
966
967                               --  Object file
968
969                               if Is_Regular_File (Obj) then
970                                  Delete (Obj_Dir, Obj);
971                               end if;
972
973                               --  Assembly file
974
975                               if Is_Regular_File (Asm) then
976                                  Delete (Obj_Dir, Asm);
977                               end if;
978
979                               --  Tree file
980
981                               if Is_Regular_File (Adt) then
982                                  Delete (Obj_Dir, Adt);
983                               end if;
984
985                               --  First expanded source file
986
987                               if Is_Regular_File (Deb) then
988                                  Delete (Obj_Dir, Deb);
989                               end if;
990
991                               --  Repinfo file
992
993                               if Is_Regular_File (Rep) then
994                                  Delete (Obj_Dir, Rep);
995                               end if;
996
997                               --  Second expanded source file
998
999                               if File_Name2 /= No_File then
1000                                  declare
1001                                     Deb : constant String :=
1002                                             Debug_File_Name (File_Name2);
1003                                     Rep : constant String :=
1004                                             Repinfo_File_Name (File_Name2);
1005
1006                                  begin
1007                                     if Is_Regular_File (Deb) then
1008                                        Delete (Obj_Dir, Deb);
1009                                     end if;
1010
1011                                     if Is_Regular_File (Rep) then
1012                                        Delete (Obj_Dir, Rep);
1013                                     end if;
1014                                  end;
1015                               end if;
1016                            end if;
1017                         end;
1018                      end if;
1019                   end loop;
1020                end if;
1021
1022                --  Check if a global archive and it dependency file could have
1023                --  been created and, if they exist, delete them.
1024
1025                if Project = Main_Project and then not Data.Library then
1026                   Global_Archive := False;
1027
1028                   for Proj in Project_Table.First ..
1029                     Project_Table.Last (Project_Tree.Projects)
1030                   loop
1031                      if Project_Tree.Projects.Table
1032                        (Proj).Other_Sources_Present
1033                      then
1034                         Global_Archive := True;
1035                         exit;
1036                      end if;
1037                   end loop;
1038
1039                   if Global_Archive then
1040                      Clean_Archive (Project, Global => True);
1041                   end if;
1042                end if;
1043
1044             end;
1045          end if;
1046
1047          --  If this is a library project, clean the library directory, the
1048          --  interface copy dir and, for a Stand-Alone Library, the binder
1049          --  generated files of the library.
1050
1051          --  The directories are cleaned only if switch -c is not specified
1052
1053          if Data.Library then
1054             if not Compile_Only then
1055                Clean_Library_Directory (Project);
1056
1057                if Data.Library_Src_Dir /= No_Path_Information then
1058                   Clean_Interface_Copy_Directory (Project);
1059                end if;
1060             end if;
1061
1062             if Data.Standalone_Library and then
1063               Data.Object_Directory /= No_Path_Information
1064             then
1065                Delete_Binder_Generated_Files
1066                  (Get_Name_String (Data.Object_Directory.Display_Name),
1067                   File_Name_Type (Data.Library_Name));
1068             end if;
1069          end if;
1070
1071          if Verbose_Mode then
1072             New_Line;
1073          end if;
1074       end if;
1075
1076       --  If switch -r is specified, call Clean_Project recursively for the
1077       --  imported projects and the project being extended.
1078
1079       if All_Projects then
1080          declare
1081             Imported : Project_List := Data.Imported_Projects;
1082             Element  : Project_Element;
1083             Process  : Boolean;
1084
1085          begin
1086             --  For each imported project, call Clean_Project if the project
1087             --  has not been processed already.
1088
1089             while Imported /= Empty_Project_List loop
1090                Element := Project_Tree.Project_Lists.Table (Imported);
1091                Imported := Element.Next;
1092                Process := True;
1093
1094                for
1095                  J in Processed_Projects.First .. Processed_Projects.Last
1096                loop
1097                   if Element.Project = Processed_Projects.Table (J) then
1098                      Process := False;
1099                      exit;
1100                   end if;
1101                end loop;
1102
1103                if Process then
1104                   Clean_Project (Element.Project);
1105                end if;
1106             end loop;
1107
1108             --  If this project extends another project, call Clean_Project for
1109             --  the project being extended. It is guaranteed that it has not
1110             --  called before, because no other project may import or extend
1111             --  this project.
1112
1113             if Data.Extends /= No_Project then
1114                Clean_Project (Data.Extends);
1115             end if;
1116          end;
1117       end if;
1118
1119          --  For the main project, delete the executables and the binder
1120          --  generated files.
1121
1122          --  The executables are deleted only if switch -c is not specified
1123
1124       if Project = Main_Project
1125         and then Data.Exec_Directory /= No_Path_Information
1126       then
1127          declare
1128             Exec_Dir : constant String :=
1129                          Get_Name_String (Data.Exec_Directory.Display_Name);
1130
1131          begin
1132             Change_Dir (Exec_Dir);
1133
1134             for N_File in 1 .. Osint.Number_Of_Files loop
1135                Main_Source_File := Next_Main_Source;
1136
1137                if not Compile_Only then
1138                   Executable :=
1139                     Executable_Of
1140                       (Main_Project,
1141                        Project_Tree,
1142                        Main_Source_File,
1143                        Current_File_Index);
1144
1145                   declare
1146                      Exec_File_Name : constant String :=
1147                                         Get_Name_String (Executable);
1148
1149                   begin
1150                      if Is_Absolute_Path (Name => Exec_File_Name) then
1151                         if Is_Regular_File (Exec_File_Name) then
1152                            Delete ("", Exec_File_Name);
1153                         end if;
1154
1155                      else
1156                         if Is_Regular_File (Exec_File_Name) then
1157                            Delete (Exec_Dir, Exec_File_Name);
1158                         end if;
1159                      end if;
1160                   end;
1161                end if;
1162
1163                if Data.Object_Directory /= No_Path_Information then
1164                   Delete_Binder_Generated_Files
1165                     (Get_Name_String (Data.Object_Directory.Display_Name),
1166                      Strip_Suffix (Main_Source_File));
1167                end if;
1168             end loop;
1169          end;
1170       end if;
1171
1172       --  Change back to previous directory
1173
1174       Change_Dir (Current_Dir);
1175    end Clean_Project;
1176
1177    ---------------------
1178    -- Debug_File_Name --
1179    ---------------------
1180
1181    function Debug_File_Name (Source : File_Name_Type) return String is
1182    begin
1183       return Get_Name_String (Source) & Debug_Suffix;
1184    end Debug_File_Name;
1185
1186    ------------
1187    -- Delete --
1188    ------------
1189
1190    procedure Delete (In_Directory : String; File : String) is
1191       Full_Name : String (1 .. In_Directory'Length + File'Length + 1);
1192       Last      : Natural := 0;
1193       Success   : Boolean;
1194
1195    begin
1196       --  Indicate that at least one file is deleted or is to be deleted
1197
1198       File_Deleted := True;
1199
1200       --  Build the path name of the file to delete
1201
1202       Last := In_Directory'Length;
1203       Full_Name (1 .. Last) := In_Directory;
1204
1205       if Last > 0 and then Full_Name (Last) /= Directory_Separator then
1206          Last := Last + 1;
1207          Full_Name (Last) := Directory_Separator;
1208       end if;
1209
1210       Full_Name (Last + 1 .. Last + File'Length) := File;
1211       Last := Last + File'Length;
1212
1213       --  If switch -n was used, simply output the path name
1214
1215       if Do_Nothing then
1216          Put_Line (Full_Name (1 .. Last));
1217
1218       --  Otherwise, delete the file if it is writable
1219
1220       else
1221          if Force_Deletions
1222            or else Is_Writable_File (Full_Name (1 .. Last))
1223            or else Is_Symbolic_Link (Full_Name (1 .. Last))
1224          then
1225             Delete_File (Full_Name (1 .. Last), Success);
1226          else
1227             Success := False;
1228          end if;
1229
1230          if Verbose_Mode or else not Quiet_Output then
1231             if not Success then
1232                Put ("Warning: """);
1233                Put (Full_Name (1 .. Last));
1234                Put_Line (""" could not be deleted");
1235
1236             else
1237                Put ("""");
1238                Put (Full_Name (1 .. Last));
1239                Put_Line (""" has been deleted");
1240             end if;
1241          end if;
1242       end if;
1243    end Delete;
1244
1245    -----------------------------------
1246    -- Delete_Binder_Generated_Files --
1247    -----------------------------------
1248
1249    procedure Delete_Binder_Generated_Files
1250      (Dir    : String;
1251       Source : File_Name_Type)
1252    is
1253       Source_Name : constant String := Get_Name_String (Source);
1254       Current     : constant String := Get_Current_Dir;
1255       Last        : constant Positive := B_Start'Length + Source_Name'Length;
1256       File_Name   : String (1 .. Last + 4);
1257
1258    begin
1259       Change_Dir (Dir);
1260
1261       --  Build the file name (before the extension)
1262
1263       File_Name (1 .. B_Start'Length) := B_Start.all;
1264       File_Name (B_Start'Length + 1 .. Last) := Source_Name;
1265
1266       --  Spec
1267
1268       File_Name (Last + 1 .. Last + 4) := ".ads";
1269
1270       if Is_Regular_File (File_Name (1 .. Last + 4)) then
1271          Delete (Dir, File_Name (1 .. Last + 4));
1272       end if;
1273
1274       --  Body
1275
1276       File_Name (Last + 1 .. Last + 4) := ".adb";
1277
1278       if Is_Regular_File (File_Name (1 .. Last + 4)) then
1279          Delete (Dir, File_Name (1 .. Last + 4));
1280       end if;
1281
1282       --  ALI file
1283
1284       File_Name (Last + 1 .. Last + 4) := ".ali";
1285
1286       if Is_Regular_File (File_Name (1 .. Last + 4)) then
1287          Delete (Dir, File_Name (1 .. Last + 4));
1288       end if;
1289
1290       --  Object file
1291
1292       File_Name (Last + 1 .. Last + Object_Suffix'Length) := Object_Suffix;
1293
1294       if Is_Regular_File (File_Name (1 .. Last + Object_Suffix'Length)) then
1295          Delete (Dir, File_Name (1 .. Last + Object_Suffix'Length));
1296       end if;
1297
1298       --  Change back to previous directory
1299
1300       Change_Dir (Current);
1301    end Delete_Binder_Generated_Files;
1302
1303    -----------------------
1304    -- Display_Copyright --
1305    -----------------------
1306
1307    procedure Display_Copyright is
1308    begin
1309       if not Copyright_Displayed then
1310          Copyright_Displayed := True;
1311          Display_Version ("GNATCLEAN", "2003");
1312       end if;
1313    end Display_Copyright;
1314
1315    -------------
1316    -- Empty_Q --
1317    -------------
1318
1319    function Empty_Q return Boolean is
1320    begin
1321       return Q_Front >= Q.Last;
1322    end Empty_Q;
1323
1324    --------------------
1325    -- Extract_From_Q --
1326    --------------------
1327
1328    procedure Extract_From_Q (Lib_File : out File_Name_Type) is
1329       Lib : constant File_Name_Type := Q.Table (Q_Front);
1330    begin
1331       Q_Front  := Q_Front + 1;
1332       Lib_File := Lib;
1333    end Extract_From_Q;
1334
1335    ---------------
1336    -- Gnatclean --
1337    ---------------
1338
1339    procedure Gnatclean is
1340    begin
1341       --  Do the necessary initializations
1342
1343       Clean.Initialize;
1344
1345       --  Parse the command line, getting the switches and the executable names
1346
1347       Parse_Cmd_Line;
1348
1349       if Verbose_Mode then
1350          Display_Copyright;
1351       end if;
1352
1353       if Project_File_Name /= null then
1354
1355          --  A project file was specified by a -P switch
1356
1357          if Opt.Verbose_Mode then
1358             New_Line;
1359             Put ("Parsing Project File """);
1360             Put (Project_File_Name.all);
1361             Put_Line (""".");
1362             New_Line;
1363          end if;
1364
1365          --  Set the project parsing verbosity to whatever was specified
1366          --  by a possible -vP switch.
1367
1368          Prj.Pars.Set_Verbosity (To => Current_Verbosity);
1369
1370          --  Parse the project file. If there is an error, Main_Project
1371          --  will still be No_Project.
1372
1373          Prj.Pars.Parse
1374            (Project           => Main_Project,
1375             In_Tree           => Project_Tree,
1376             Project_File_Name => Project_File_Name.all,
1377             Packages_To_Check => Packages_To_Check_By_Gnatmake);
1378
1379          if Main_Project = No_Project then
1380             Fail ("""" & Project_File_Name.all & """ processing failed");
1381          end if;
1382
1383          if Opt.Verbose_Mode then
1384             New_Line;
1385             Put ("Parsing of Project File """);
1386             Put (Project_File_Name.all);
1387             Put (""" is finished.");
1388             New_Line;
1389          end if;
1390
1391          --  Add source directories and object directories to the search paths
1392
1393          Add_Source_Directories (Main_Project, Project_Tree);
1394          Add_Object_Directories (Main_Project, Project_Tree);
1395       end if;
1396
1397       Osint.Add_Default_Search_Dirs;
1398
1399       --  If a project file was specified, but no executable name, put all
1400       --  the mains of the project file (if any) as if there were on the
1401       --  command line.
1402
1403       if Main_Project /= No_Project and then Osint.Number_Of_Files = 0 then
1404          declare
1405             Value : String_List_Id :=
1406                       Project_Tree.Projects.Table (Main_Project).Mains;
1407             Main  : String_Element;
1408          begin
1409             while Value /= Prj.Nil_String loop
1410                Main := Project_Tree.String_Elements.Table (Value);
1411                Osint.Add_File
1412                  (File_Name => Get_Name_String (Main.Value),
1413                   Index     => Main.Index);
1414                Value := Main.Next;
1415             end loop;
1416          end;
1417       end if;
1418
1419       --  If neither a project file nor an executable were specified,
1420       --  output the usage and exit.
1421
1422       if Main_Project = No_Project and then Osint.Number_Of_Files = 0 then
1423          Usage;
1424          return;
1425       end if;
1426
1427       if Verbose_Mode then
1428          New_Line;
1429       end if;
1430
1431       if Main_Project /= No_Project then
1432
1433          --  If a project file has been specified, call Clean_Project with the
1434          --  project id of this project file, after resetting the list of
1435          --  processed projects.
1436
1437          Processed_Projects.Init;
1438          Clean_Project (Main_Project);
1439
1440       else
1441          --  If no project file has been specified, the work is done in
1442          --  Clean_Executables.
1443
1444          Clean_Executables;
1445       end if;
1446
1447       --  In verbose mode, if Delete has not been called, indicate that
1448       --  no file needs to be deleted.
1449
1450       if Verbose_Mode and (not File_Deleted) then
1451          New_Line;
1452
1453          if Do_Nothing then
1454             Put_Line ("No file needs to be deleted");
1455          else
1456             Put_Line ("No file has been deleted");
1457          end if;
1458       end if;
1459    end Gnatclean;
1460
1461    ------------------------
1462    -- In_Extension_Chain --
1463    ------------------------
1464
1465    function In_Extension_Chain
1466      (Of_Project : Project_Id;
1467       Prj        : Project_Id) return Boolean
1468    is
1469       Data : Project_Data;
1470
1471    begin
1472       if Prj = No_Project or else Of_Project = No_Project then
1473          return False;
1474       end if;
1475
1476       if Of_Project = Prj then
1477          return True;
1478       end if;
1479
1480       Data := Project_Tree.Projects.Table (Of_Project);
1481
1482       while Data.Extends /= No_Project loop
1483          if Data.Extends = Prj then
1484             return True;
1485          end if;
1486
1487          Data := Project_Tree.Projects.Table (Data.Extends);
1488       end loop;
1489
1490       Data := Project_Tree.Projects.Table (Prj);
1491
1492       while Data.Extends /= No_Project loop
1493          if Data.Extends = Of_Project then
1494             return True;
1495          end if;
1496
1497          Data := Project_Tree.Projects.Table (Data.Extends);
1498       end loop;
1499
1500       return False;
1501    end In_Extension_Chain;
1502
1503    ------------
1504    -- Init_Q --
1505    ------------
1506
1507    procedure Init_Q is
1508    begin
1509       Q_Front := Q.First;
1510       Q.Set_Last (Q.First);
1511    end Init_Q;
1512
1513    ----------------
1514    -- Initialize --
1515    ----------------
1516
1517    procedure Initialize is
1518    begin
1519       if not Initialized then
1520          Initialized := True;
1521
1522          --  Get default search directories to locate system.ads when calling
1523          --  Targparm.Get_Target_Parameters.
1524
1525          Osint.Add_Default_Search_Dirs;
1526
1527          --  Initialize some packages
1528
1529          Csets.Initialize;
1530          Namet.Initialize;
1531          Snames.Initialize;
1532          Prj.Initialize (Project_Tree);
1533
1534          --  Check if the platform is VMS and, if it is, change some variables
1535
1536          Targparm.Get_Target_Parameters;
1537
1538          if OpenVMS_On_Target then
1539             Debug_Suffix (Debug_Suffix'First) := '_';
1540             Repinfo_Suffix (Repinfo_Suffix'First) := '_';
1541             B_Start := new String'("b__");
1542          end if;
1543       end if;
1544
1545       --  Reset global variables
1546
1547       Free (Object_Directory_Path);
1548       Do_Nothing := False;
1549       File_Deleted := False;
1550       Copyright_Displayed := False;
1551       Usage_Displayed := False;
1552       Free (Project_File_Name);
1553       Main_Project := Prj.No_Project;
1554       All_Projects := False;
1555    end Initialize;
1556
1557    --------------
1558    -- Insert_Q --
1559    --------------
1560
1561    procedure Insert_Q (Lib_File : File_Name_Type) is
1562    begin
1563       --  Do not insert an empty name or an already marked source
1564
1565       if Lib_File /= No_File and then not Makeutl.Is_Marked (Lib_File) then
1566          Q.Table (Q.Last) := Lib_File;
1567          Q.Increment_Last;
1568
1569          --  Mark the source that has been just added to the Q
1570
1571          Makeutl.Mark (Lib_File);
1572       end if;
1573    end Insert_Q;
1574
1575    ----------------------
1576    -- Object_File_Name --
1577    ----------------------
1578
1579    function Object_File_Name (Source : File_Name_Type) return String is
1580       Src : constant String := Get_Name_String (Source);
1581
1582    begin
1583       --  If the source name has an extension, then replace it with
1584       --  the Object suffix.
1585
1586       for Index in reverse Src'First + 1 .. Src'Last loop
1587          if Src (Index) = '.' then
1588             return Src (Src'First .. Index - 1) & Object_Suffix;
1589          end if;
1590       end loop;
1591
1592       --  If there is no dot, or if it is the first character, just add the
1593       --  ALI suffix.
1594
1595       return Src & Object_Suffix;
1596    end Object_File_Name;
1597
1598    --------------------
1599    -- Parse_Cmd_Line --
1600    --------------------
1601
1602    procedure Parse_Cmd_Line is
1603       Last         : constant Natural := Argument_Count;
1604       Source_Index : Int := 0;
1605       Index        : Positive;
1606
1607       procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
1608
1609    begin
1610       --  First, check for --version and --help
1611
1612       Check_Version_And_Help ("GNATCLEAN", "2003");
1613
1614       Index := 1;
1615       while Index <= Last loop
1616          declare
1617             Arg : constant String := Argument (Index);
1618
1619             procedure Bad_Argument;
1620             --  Signal bad argument
1621
1622             ------------------
1623             -- Bad_Argument --
1624             ------------------
1625
1626             procedure Bad_Argument is
1627             begin
1628                Fail ("invalid argument """, Arg, """");
1629             end Bad_Argument;
1630
1631          begin
1632             if Arg'Length /= 0 then
1633                if Arg (1) = '-' then
1634                   if Arg'Length = 1 then
1635                      Bad_Argument;
1636                   end if;
1637
1638                   case Arg (2) is
1639                      when '-' =>
1640                         if Arg'Length > Subdirs_Option'Length and then
1641                           Arg (1 .. Subdirs_Option'Length) = Subdirs_Option
1642                         then
1643                            Subdirs :=
1644                              new String'
1645                                (Arg (Subdirs_Option'Length + 1 .. Arg'Last));
1646
1647                         else
1648                            Bad_Argument;
1649                         end if;
1650
1651                      when 'a' =>
1652                         if Arg'Length < 4 then
1653                            Bad_Argument;
1654                         end if;
1655
1656                         if Arg (3) = 'O' then
1657                            Add_Lib_Search_Dir (Arg (4 .. Arg'Last));
1658
1659                         elsif Arg (3) = 'P' then
1660                            Prj.Ext.Add_Search_Project_Directory
1661                              (Arg (4 .. Arg'Last));
1662
1663                         else
1664                            Bad_Argument;
1665                         end if;
1666
1667                      when 'c'    =>
1668                         Compile_Only := True;
1669
1670                      when 'D'    =>
1671                         if Object_Directory_Path /= null then
1672                            Fail ("duplicate -D switch");
1673
1674                         elsif Project_File_Name /= null then
1675                            Fail ("-P and -D cannot be used simultaneously");
1676                         end if;
1677
1678                         if Arg'Length > 2 then
1679                            declare
1680                               Dir : constant String := Arg (3 .. Arg'Last);
1681                            begin
1682                               if not Is_Directory (Dir) then
1683                                  Fail (Dir, " is not a directory");
1684                               else
1685                                  Add_Lib_Search_Dir (Dir);
1686                               end if;
1687                            end;
1688
1689                         else
1690                            if Index = Last then
1691                               Fail ("no directory specified after -D");
1692                            end if;
1693
1694                            Index := Index + 1;
1695
1696                            declare
1697                               Dir : constant String := Argument (Index);
1698                            begin
1699                               if not Is_Directory (Dir) then
1700                                  Fail (Dir, " is not a directory");
1701                               else
1702                                  Add_Lib_Search_Dir (Dir);
1703                               end if;
1704                            end;
1705                         end if;
1706
1707                      when 'e' =>
1708                         if Arg = "-eL" then
1709                            Follow_Links_For_Files := True;
1710
1711                         else
1712                            Bad_Argument;
1713                         end if;
1714
1715                      when 'f' =>
1716                         Force_Deletions := True;
1717
1718                      when 'F' =>
1719                         Full_Path_Name_For_Brief_Errors := True;
1720
1721                      when 'h' =>
1722                         Usage;
1723
1724                      when 'i' =>
1725                         if Arg'Length = 2 then
1726                            Bad_Argument;
1727                         end if;
1728
1729                         Source_Index := 0;
1730
1731                         for J in 3 .. Arg'Last loop
1732                            if Arg (J) not in '0' .. '9' then
1733                               Bad_Argument;
1734                            end if;
1735
1736                            Source_Index :=
1737                              (20 * Source_Index) +
1738                              (Character'Pos (Arg (J)) - Character'Pos ('0'));
1739                         end loop;
1740
1741                      when 'I' =>
1742                         if Arg = "-I-" then
1743                            Opt.Look_In_Primary_Dir := False;
1744
1745                         else
1746                            if Arg'Length = 2 then
1747                               Bad_Argument;
1748                            end if;
1749
1750                            Add_Lib_Search_Dir (Arg (3 .. Arg'Last));
1751                         end if;
1752
1753                      when 'n' =>
1754                         Do_Nothing := True;
1755
1756                      when 'P' =>
1757                         if Project_File_Name /= null then
1758                            Fail ("multiple -P switches");
1759
1760                         elsif Object_Directory_Path /= null then
1761                            Fail ("-D and -P cannot be used simultaneously");
1762
1763                         end if;
1764
1765                         if Arg'Length > 2 then
1766                            declare
1767                               Prj : constant String := Arg (3 .. Arg'Last);
1768                            begin
1769                               if Prj'Length > 1 and then
1770                                 Prj (Prj'First) = '='
1771                               then
1772                                  Project_File_Name :=
1773                                    new String'
1774                                      (Prj (Prj'First + 1 ..  Prj'Last));
1775                               else
1776                                  Project_File_Name := new String'(Prj);
1777                               end if;
1778                            end;
1779
1780                         else
1781                            if Index = Last then
1782                               Fail ("no project specified after -P");
1783                            end if;
1784
1785                            Index := Index + 1;
1786                            Project_File_Name := new String'(Argument (Index));
1787                         end if;
1788
1789                      when 'q' =>
1790                         Quiet_Output := True;
1791
1792                      when 'r' =>
1793                         All_Projects := True;
1794
1795                      when 'v' =>
1796                         if Arg = "-v" then
1797                            Verbose_Mode := True;
1798
1799                         elsif Arg = "-vP0" then
1800                            Current_Verbosity := Prj.Default;
1801
1802                         elsif Arg = "-vP1" then
1803                            Current_Verbosity := Prj.Medium;
1804
1805                         elsif Arg = "-vP2" then
1806                            Current_Verbosity := Prj.High;
1807
1808                         else
1809                            Bad_Argument;
1810                         end if;
1811
1812                      when 'X' =>
1813                         if Arg'Length = 2 then
1814                            Bad_Argument;
1815                         end if;
1816
1817                         declare
1818                            Ext_Asgn  : constant String := Arg (3 .. Arg'Last);
1819                            Start     : Positive := Ext_Asgn'First;
1820                            Stop      : Natural  := Ext_Asgn'Last;
1821                            Equal_Pos : Natural;
1822                            OK        : Boolean  := True;
1823
1824                         begin
1825                            if Ext_Asgn (Start) = '"' then
1826                               if Ext_Asgn (Stop) = '"' then
1827                                  Start := Start + 1;
1828                                  Stop  := Stop - 1;
1829
1830                               else
1831                                  OK := False;
1832                               end if;
1833                            end if;
1834
1835                            Equal_Pos := Start;
1836
1837                            while Equal_Pos <= Stop
1838                              and then Ext_Asgn (Equal_Pos) /= '='
1839                            loop
1840                               Equal_Pos := Equal_Pos + 1;
1841                            end loop;
1842
1843                            if Equal_Pos = Start or else Equal_Pos > Stop then
1844                               OK := False;
1845                            end if;
1846
1847                            if OK then
1848                               Prj.Ext.Add
1849                                 (External_Name =>
1850                                    Ext_Asgn (Start .. Equal_Pos - 1),
1851                                  Value         =>
1852                                    Ext_Asgn (Equal_Pos + 1 .. Stop));
1853
1854                            else
1855                               Fail
1856                                 ("illegal external assignment '",
1857                                  Ext_Asgn, "'");
1858                            end if;
1859                         end;
1860
1861                      when others =>
1862                         Bad_Argument;
1863                   end case;
1864
1865                else
1866                   Add_File (Arg, Source_Index);
1867                end if;
1868             end if;
1869          end;
1870
1871          Index := Index + 1;
1872       end loop;
1873    end Parse_Cmd_Line;
1874
1875    -----------------------
1876    -- Repinfo_File_Name --
1877    -----------------------
1878
1879    function Repinfo_File_Name (Source : File_Name_Type) return String is
1880    begin
1881       return Get_Name_String (Source) & Repinfo_Suffix;
1882    end Repinfo_File_Name;
1883
1884    --------------------
1885    -- Tree_File_Name --
1886    --------------------
1887
1888    function Tree_File_Name (Source : File_Name_Type) return String is
1889       Src : constant String := Get_Name_String (Source);
1890
1891    begin
1892       --  If the source name has an extension, then replace it with
1893       --  the tree suffix.
1894
1895       for Index in reverse Src'First + 1 .. Src'Last loop
1896          if Src (Index) = '.' then
1897             return Src (Src'First .. Index - 1) & Tree_Suffix;
1898          end if;
1899       end loop;
1900
1901       --  If there is no dot, or if it is the first character, just add the
1902       --  tree suffix.
1903
1904       return Src & Tree_Suffix;
1905    end Tree_File_Name;
1906
1907    ---------------------------
1908    -- Ultimate_Extension_Of --
1909    ---------------------------
1910
1911    function Ultimate_Extension_Of (Project : Project_Id) return Project_Id is
1912       Result : Project_Id := Project;
1913       Data   : Project_Data;
1914
1915    begin
1916       if Project /= No_Project then
1917          loop
1918             Data := Project_Tree.Projects.Table (Result);
1919             exit when Data.Extended_By = No_Project;
1920             Result := Data.Extended_By;
1921          end loop;
1922       end if;
1923
1924       return Result;
1925    end Ultimate_Extension_Of;
1926
1927    -----------
1928    -- Usage --
1929    -----------
1930
1931    procedure Usage is
1932    begin
1933       if not Usage_Displayed then
1934          Usage_Displayed := True;
1935          Display_Copyright;
1936          Put_Line ("Usage: gnatclean [switches] {[-innn] name}");
1937          New_Line;
1938
1939          Put_Line ("  names is one or more file names from which " &
1940                    "the .adb or .ads suffix may be omitted");
1941          Put_Line ("  names may be omitted if -P<project> is specified");
1942          New_Line;
1943
1944          Put_Line ("  --subdirs=dir real obj/lib/exec dirs are subdirs");
1945          New_Line;
1946
1947          Put_Line ("  -c       Only delete compiler generated files");
1948          Put_Line ("  -D dir   Specify dir as the object library");
1949          Put_Line ("  -eL      Follow symbolic links when processing " &
1950                    "project files");
1951          Put_Line ("  -f       Force deletions of unwritable files");
1952          Put_Line ("  -F       Full project path name " &
1953                    "in brief error messages");
1954          Put_Line ("  -h       Display this message");
1955          Put_Line ("  -innn    Index of unit in source for following names");
1956          Put_Line ("  -n       Nothing to do: only list files to delete");
1957          Put_Line ("  -Pproj   Use GNAT Project File proj");
1958          Put_Line ("  -q       Be quiet/terse");
1959          Put_Line ("  -r       Clean all projects recursively");
1960          Put_Line ("  -v       Verbose mode");
1961          Put_Line ("  -vPx     Specify verbosity when parsing " &
1962                    "GNAT Project Files");
1963          Put_Line ("  -Xnm=val Specify an external reference " &
1964                    "for GNAT Project Files");
1965          New_Line;
1966
1967          Put_Line ("  -aPdir   Add directory dir to project search path");
1968          New_Line;
1969
1970          Put_Line ("  -aOdir   Specify ALI/object files search path");
1971          Put_Line ("  -Idir    Like -aOdir");
1972          Put_Line ("  -I-      Don't look for source/library files " &
1973                    "in the default directory");
1974          New_Line;
1975       end if;
1976    end Usage;
1977
1978 end Clean;