51793b07596eabcd9ee121b4a67ecd114ba249a0
[gcc.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-2017, 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 Make_Util; use Make_Util;
29 with Namet; use Namet;
30 with Opt; use Opt;
31 with Osint; use Osint;
32 with Osint.M; use Osint.M;
33 -- with Sdefault;
34 with Snames;
35 with Stringt;
36 with Switch; use Switch;
37 with Table;
38 with Targparm; use Targparm;
39 with Types; use Types;
40
41 with Ada.Command_Line; use Ada.Command_Line;
42
43 with GNAT.Command_Line; use GNAT.Command_Line;
44 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
45 with GNAT.IO; use GNAT.IO;
46 with GNAT.OS_Lib; use GNAT.OS_Lib;
47
48 package body Clean is
49
50 Initialized : Boolean := False;
51 -- Set to True by the first call to Initialize to avoid reinitialization
52 -- of some packages.
53
54 -- Suffixes of various files
55
56 Assembly_Suffix : constant String := ".s";
57 Tree_Suffix : constant String := ".adt";
58 Object_Suffix : constant String := Get_Target_Object_Suffix.all;
59 Debug_Suffix : constant String := ".dg";
60 Repinfo_Suffix : constant String := ".rep";
61 -- Suffix of representation info files
62
63 B_Start : constant String := "b~";
64 -- Prefix of binder generated file, and number of actual characters used
65
66 Object_Directory_Path : String_Access := null;
67 -- The path name of the object directory, set with switch -D
68
69 Force_Deletions : Boolean := False;
70 -- Set to True by switch -f. When True, attempts to delete non writable
71 -- files will be done.
72
73 Do_Nothing : Boolean := False;
74 -- Set to True when switch -n is specified. When True, no file is deleted.
75 -- gnatclean only lists the files that would have been deleted if the
76 -- switch -n had not been specified.
77
78 File_Deleted : Boolean := False;
79 -- Set to True if at least one file has been deleted
80
81 Copyright_Displayed : Boolean := False;
82 Usage_Displayed : Boolean := False;
83
84 Project_File_Name : String_Access := null;
85
86 package Sources is new Table.Table
87 (Table_Component_Type => File_Name_Type,
88 Table_Index_Type => Natural,
89 Table_Low_Bound => 0,
90 Table_Initial => 10,
91 Table_Increment => 100,
92 Table_Name => "Clean.Processed_Projects");
93 -- Table to store all the source files of a library unit: spec, body and
94 -- subunits, to detect .dg files and delete them.
95
96 -----------------------------
97 -- Other local subprograms --
98 -----------------------------
99
100 function Assembly_File_Name (Source : File_Name_Type) return String;
101 -- Returns the assembly file name corresponding to Source
102
103 procedure Clean_Executables;
104 -- Do the cleaning work when no project file is specified
105
106 function Debug_File_Name (Source : File_Name_Type) return String;
107 -- Name of the expanded source file corresponding to Source
108
109 procedure Delete (In_Directory : String; File : String);
110 -- Delete one file, or list the file name if switch -n is specified
111
112 procedure Delete_Binder_Generated_Files
113 (Dir : String;
114 Source : File_Name_Type);
115 -- Delete the binder generated file in directory Dir for Source, if they
116 -- exist: for Unix these are b~<source>.ads, b~<source>.adb,
117 -- b~<source>.ali and b~<source>.o.
118
119 procedure Display_Copyright;
120 -- Display the Copyright notice. If called several times, display the
121 -- Copyright notice only the first time.
122
123 procedure Initialize;
124 -- Call the necessary package initializations
125
126 function Object_File_Name (Source : File_Name_Type) return String;
127 -- Returns the object file name corresponding to Source
128
129 procedure Parse_Cmd_Line;
130 -- Parse the command line
131
132 function Repinfo_File_Name (Source : File_Name_Type) return String;
133 -- Returns the repinfo file name corresponding to Source
134
135 function Tree_File_Name (Source : File_Name_Type) return String;
136 -- Returns the tree file name corresponding to Source
137
138 procedure Usage;
139 -- Display the usage. If called several times, the usage is displayed only
140 -- the first time.
141
142 ------------------------
143 -- Assembly_File_Name --
144 ------------------------
145
146 function Assembly_File_Name (Source : File_Name_Type) return String is
147 Src : constant String := Get_Name_String (Source);
148
149 begin
150 -- If the source name has an extension, then replace it with
151 -- the assembly suffix.
152
153 for Index in reverse Src'First + 1 .. Src'Last loop
154 if Src (Index) = '.' then
155 return Src (Src'First .. Index - 1) & Assembly_Suffix;
156 end if;
157 end loop;
158
159 -- If there is no dot, or if it is the first character, just add the
160 -- assembly suffix.
161
162 return Src & Assembly_Suffix;
163 end Assembly_File_Name;
164
165 -----------------------
166 -- Clean_Executables --
167 -----------------------
168
169 procedure Clean_Executables is
170 Main_Source_File : File_Name_Type;
171 -- Current main source
172
173 Main_Lib_File : File_Name_Type;
174 -- ALI file of the current main
175
176 Lib_File : File_Name_Type;
177 -- Current ALI file
178
179 Full_Lib_File : File_Name_Type;
180 -- Full name of the current ALI file
181
182 Text : Text_Buffer_Ptr;
183 The_ALI : ALI_Id;
184 Found : Boolean;
185 Source : Queue.Source_Info;
186
187 begin
188 Queue.Initialize;
189
190 -- It does not really matter if there is or not an object file
191 -- corresponding to an ALI file: if there is one, it will be deleted.
192
193 Opt.Check_Object_Consistency := False;
194
195 -- Proceed each executable one by one. Each source is marked as it is
196 -- processed, so common sources between executables will not be
197 -- processed several times.
198
199 for N_File in 1 .. Osint.Number_Of_Files loop
200 Main_Source_File := Next_Main_Source;
201 Main_Lib_File :=
202 Osint.Lib_File_Name (Main_Source_File, Current_File_Index);
203
204 if Main_Lib_File /= No_File then
205 Queue.Insert
206 ((File => Main_Lib_File,
207 Unit => No_Unit_Name,
208 Index => 0));
209 end if;
210
211 while not Queue.Is_Empty loop
212 Sources.Set_Last (0);
213 Queue.Extract (Found, Source);
214 pragma Assert (Found);
215 pragma Assert (Source.File /= No_File);
216 Lib_File := Source.File;
217 Full_Lib_File := Osint.Full_Lib_File_Name (Lib_File);
218
219 -- If we have existing ALI file that is not read-only, process it
220
221 if Full_Lib_File /= No_File
222 and then not Is_Readonly_Library (Full_Lib_File)
223 then
224 Text := Read_Library_Info (Lib_File);
225
226 if Text /= null then
227 The_ALI :=
228 Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
229 Free (Text);
230
231 -- If no error was produced while loading this ALI file,
232 -- insert into the queue all the unmarked withed sources.
233
234 if The_ALI /= No_ALI_Id then
235 for J in ALIs.Table (The_ALI).First_Unit ..
236 ALIs.Table (The_ALI).Last_Unit
237 loop
238 Sources.Increment_Last;
239 Sources.Table (Sources.Last) :=
240 ALI.Units.Table (J).Sfile;
241
242 for K in ALI.Units.Table (J).First_With ..
243 ALI.Units.Table (J).Last_With
244 loop
245 if Withs.Table (K).Afile /= No_File then
246 Queue.Insert
247 ((File => Withs.Table (K).Afile,
248 Unit => No_Unit_Name,
249 Index => 0));
250 end if;
251 end loop;
252 end loop;
253
254 -- Look for subunits and put them in the Sources table
255
256 for J in ALIs.Table (The_ALI).First_Sdep ..
257 ALIs.Table (The_ALI).Last_Sdep
258 loop
259 if Sdep.Table (J).Subunit_Name /= No_Name then
260 Sources.Increment_Last;
261 Sources.Table (Sources.Last) :=
262 Sdep.Table (J).Sfile;
263 end if;
264 end loop;
265 end if;
266 end if;
267
268 -- Now delete all existing files corresponding to this ALI file
269
270 declare
271 Obj_Dir : constant String :=
272 Dir_Name (Get_Name_String (Full_Lib_File));
273 Obj : constant String := Object_File_Name (Lib_File);
274 Adt : constant String := Tree_File_Name (Lib_File);
275 Asm : constant String := Assembly_File_Name (Lib_File);
276
277 begin
278 Delete (Obj_Dir, Get_Name_String (Lib_File));
279
280 if Is_Regular_File (Obj_Dir & Dir_Separator & Obj) then
281 Delete (Obj_Dir, Obj);
282 end if;
283
284 if Is_Regular_File (Obj_Dir & Dir_Separator & Adt) then
285 Delete (Obj_Dir, Adt);
286 end if;
287
288 if Is_Regular_File (Obj_Dir & Dir_Separator & Asm) then
289 Delete (Obj_Dir, Asm);
290 end if;
291
292 -- Delete expanded source files (.dg) and/or repinfo files
293 -- (.rep) if any
294
295 for J in 1 .. Sources.Last loop
296 declare
297 Deb : constant String :=
298 Debug_File_Name (Sources.Table (J));
299 Rep : constant String :=
300 Repinfo_File_Name (Sources.Table (J));
301
302 begin
303 if Is_Regular_File (Obj_Dir & Dir_Separator & Deb) then
304 Delete (Obj_Dir, Deb);
305 end if;
306
307 if Is_Regular_File (Obj_Dir & Dir_Separator & Rep) then
308 Delete (Obj_Dir, Rep);
309 end if;
310 end;
311 end loop;
312 end;
313 end if;
314 end loop;
315
316 -- Delete the executable, if it exists, and the binder generated
317 -- files, if any.
318
319 if not Compile_Only then
320 declare
321 Source : constant File_Name_Type :=
322 Strip_Suffix (Main_Lib_File);
323 Executable : constant String :=
324 Get_Name_String (Executable_Name (Source));
325 begin
326 if Is_Regular_File (Executable) then
327 Delete ("", Executable);
328 end if;
329
330 Delete_Binder_Generated_Files (Get_Current_Dir, Source);
331 end;
332 end if;
333 end loop;
334 end Clean_Executables;
335
336 ---------------------
337 -- Debug_File_Name --
338 ---------------------
339
340 function Debug_File_Name (Source : File_Name_Type) return String is
341 begin
342 return Get_Name_String (Source) & Debug_Suffix;
343 end Debug_File_Name;
344
345 ------------
346 -- Delete --
347 ------------
348
349 procedure Delete (In_Directory : String; File : String) is
350 Full_Name : String (1 .. In_Directory'Length + File'Length + 1);
351 Last : Natural := 0;
352 Success : Boolean;
353
354 begin
355 -- Indicate that at least one file is deleted or is to be deleted
356
357 File_Deleted := True;
358
359 -- Build the path name of the file to delete
360
361 Last := In_Directory'Length;
362 Full_Name (1 .. Last) := In_Directory;
363
364 if Last > 0 and then Full_Name (Last) /= Directory_Separator then
365 Last := Last + 1;
366 Full_Name (Last) := Directory_Separator;
367 end if;
368
369 Full_Name (Last + 1 .. Last + File'Length) := File;
370 Last := Last + File'Length;
371
372 -- If switch -n was used, simply output the path name
373
374 if Do_Nothing then
375 Put_Line (Full_Name (1 .. Last));
376
377 -- Otherwise, delete the file if it is writable
378
379 else
380 if Force_Deletions
381 or else Is_Writable_File (Full_Name (1 .. Last))
382 or else Is_Symbolic_Link (Full_Name (1 .. Last))
383 then
384 Delete_File (Full_Name (1 .. Last), Success);
385
386 -- Here if no deletion required
387
388 else
389 Success := False;
390 end if;
391
392 if Verbose_Mode or else not Quiet_Output then
393 if not Success then
394 Put ("Warning: """);
395 Put (Full_Name (1 .. Last));
396 Put_Line (""" could not be deleted");
397
398 else
399 Put ("""");
400 Put (Full_Name (1 .. Last));
401 Put_Line (""" has been deleted");
402 end if;
403 end if;
404 end if;
405 end Delete;
406
407 -----------------------------------
408 -- Delete_Binder_Generated_Files --
409 -----------------------------------
410
411 procedure Delete_Binder_Generated_Files
412 (Dir : String;
413 Source : File_Name_Type)
414 is
415 Source_Name : constant String := Get_Name_String (Source);
416 Current : constant String := Get_Current_Dir;
417 Last : constant Positive := B_Start'Length + Source_Name'Length;
418 File_Name : String (1 .. Last + 4);
419
420 begin
421 Change_Dir (Dir);
422
423 -- Build the file name (before the extension)
424
425 File_Name (1 .. B_Start'Length) := B_Start;
426 File_Name (B_Start'Length + 1 .. Last) := Source_Name;
427
428 -- Spec
429
430 File_Name (Last + 1 .. Last + 4) := ".ads";
431
432 if Is_Regular_File (File_Name (1 .. Last + 4)) then
433 Delete (Dir, File_Name (1 .. Last + 4));
434 end if;
435
436 -- Body
437
438 File_Name (Last + 1 .. Last + 4) := ".adb";
439
440 if Is_Regular_File (File_Name (1 .. Last + 4)) then
441 Delete (Dir, File_Name (1 .. Last + 4));
442 end if;
443
444 -- ALI file
445
446 File_Name (Last + 1 .. Last + 4) := ".ali";
447
448 if Is_Regular_File (File_Name (1 .. Last + 4)) then
449 Delete (Dir, File_Name (1 .. Last + 4));
450 end if;
451
452 -- Object file
453
454 File_Name (Last + 1 .. Last + Object_Suffix'Length) := Object_Suffix;
455
456 if Is_Regular_File (File_Name (1 .. Last + Object_Suffix'Length)) then
457 Delete (Dir, File_Name (1 .. Last + Object_Suffix'Length));
458 end if;
459
460 -- Change back to previous directory
461
462 Change_Dir (Current);
463 end Delete_Binder_Generated_Files;
464
465 -----------------------
466 -- Display_Copyright --
467 -----------------------
468
469 procedure Display_Copyright is
470 begin
471 if not Copyright_Displayed then
472 Copyright_Displayed := True;
473 Display_Version ("GNATCLEAN", "2003");
474 end if;
475 end Display_Copyright;
476
477 ---------------
478 -- Gnatclean --
479 ---------------
480
481 procedure Gnatclean is
482 begin
483 -- Do the necessary initializations
484
485 Clean.Initialize;
486
487 -- Parse the command line, getting the switches and the executable names
488
489 Parse_Cmd_Line;
490
491 if Verbose_Mode then
492 Display_Copyright;
493 end if;
494
495 Osint.Add_Default_Search_Dirs;
496
497 if Osint.Number_Of_Files = 0 then
498 if Argument_Count = 0 then
499 Usage;
500 else
501 Try_Help;
502 end if;
503
504 return;
505 end if;
506
507 if Verbose_Mode then
508 New_Line;
509 end if;
510
511 if Project_File_Name /= null then
512 declare
513 Gprclean_Path : constant String_Access :=
514 Locate_Exec_On_Path ("gprclean");
515 Arg_Len : Natural := Argument_Count;
516 Pos : Natural := 0;
517 Target : String_Access := null;
518 Success : Boolean := False;
519 begin
520 if Gprclean_Path = null then
521 Fail_Program
522 ("project files are no longer supported by gnatclean;" &
523 " use gprclean instead");
524 end if;
525
526 Find_Program_Name;
527
528 if Name_Len > 10
529 and then Name_Buffer (Name_Len - 7 .. Name_Len) = "gnatclean"
530 then
531 Target := new String'(Name_Buffer (1 .. Name_Len - 9));
532 Arg_Len := Arg_Len + 1;
533 end if;
534
535 declare
536 Args : Argument_List (1 .. Arg_Len);
537 begin
538 if Target /= null then
539 Args (1) := new String'("--target=" & Target.all);
540 Pos := 1;
541 end if;
542
543 for J in 1 .. Argument_Count loop
544 Pos := Pos + 1;
545 Args (Pos) := new String'(Argument (J));
546 end loop;
547
548 Spawn (Gprclean_Path.all, Args, Success);
549
550 if Success then
551 Exit_Program (E_Success);
552 else
553 Exit_Program (E_Errors);
554 end if;
555 end;
556 end;
557 end if;
558
559 Clean_Executables;
560
561 -- In verbose mode, if Delete has not been called, indicate that no file
562 -- needs to be deleted.
563
564 if Verbose_Mode and (not File_Deleted) then
565 New_Line;
566
567 if Do_Nothing then
568 Put_Line ("No file needs to be deleted");
569 else
570 Put_Line ("No file has been deleted");
571 end if;
572 end if;
573 end Gnatclean;
574
575 ----------------
576 -- Initialize --
577 ----------------
578
579 procedure Initialize is
580 begin
581 if not Initialized then
582 Initialized := True;
583
584 -- Get default search directories to locate system.ads when calling
585 -- Targparm.Get_Target_Parameters.
586
587 Osint.Add_Default_Search_Dirs;
588
589 -- Initialize some packages
590
591 Csets.Initialize;
592 Snames.Initialize;
593 Stringt.Initialize;
594
595 Targparm.Get_Target_Parameters;
596 end if;
597
598 -- Reset global variables
599
600 Free (Object_Directory_Path);
601 Do_Nothing := False;
602 File_Deleted := False;
603 Copyright_Displayed := False;
604 Usage_Displayed := False;
605 end Initialize;
606
607 ----------------------
608 -- Object_File_Name --
609 ----------------------
610
611 function Object_File_Name (Source : File_Name_Type) return String is
612 Src : constant String := Get_Name_String (Source);
613
614 begin
615 -- If the source name has an extension, then replace it with
616 -- the Object suffix.
617
618 for Index in reverse Src'First + 1 .. Src'Last loop
619 if Src (Index) = '.' then
620 return Src (Src'First .. Index - 1) & Object_Suffix;
621 end if;
622 end loop;
623
624 -- If there is no dot, or if it is the first character, just add the
625 -- ALI suffix.
626
627 return Src & Object_Suffix;
628 end Object_File_Name;
629
630 --------------------
631 -- Parse_Cmd_Line --
632 --------------------
633
634 procedure Parse_Cmd_Line is
635 Last : constant Natural := Argument_Count;
636 Index : Positive;
637 Source_Index : Int := 0;
638
639 procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
640
641 begin
642 -- First, check for --version and --help
643
644 Check_Version_And_Help ("GNATCLEAN", "2003");
645
646 -- First, check for switch -P and, if found and gprclean is available,
647 -- silently invoke gprclean, with switch --target if not on a native
648 -- platform.
649
650 declare
651 Arg_Len : Positive := Argument_Count;
652 Call_Gprclean : Boolean := False;
653 Gprclean : String_Access := null;
654 Pos : Natural := 0;
655 Success : Boolean;
656 Target : String_Access := null;
657
658 begin
659 Find_Program_Name;
660
661 if Name_Len >= 9
662 and then Name_Buffer (Name_Len - 8 .. Name_Len) = "gnatclean"
663 then
664 if Name_Len > 9 then
665 Target := new String'(Name_Buffer (1 .. Name_Len - 10));
666 Arg_Len := Arg_Len + 1;
667 end if;
668
669 for J in 1 .. Argument_Count loop
670 declare
671 Arg : constant String := Argument (J);
672 begin
673 if Arg'Length >= 2
674 and then Arg (Arg'First .. Arg'First + 1) = "-P"
675 then
676 Call_Gprclean := True;
677 exit;
678 end if;
679 end;
680 end loop;
681
682 if Call_Gprclean then
683 Gprclean := Locate_Exec_On_Path (Exec_Name => "gprclean");
684
685 if Gprclean /= null then
686 declare
687 Args : Argument_List (1 .. Arg_Len);
688 begin
689 if Target /= null then
690 Args (1) := new String'("--target=" & Target.all);
691 Pos := 1;
692 end if;
693
694 for J in 1 .. Argument_Count loop
695 Pos := Pos + 1;
696 Args (Pos) := new String'(Argument (J));
697 end loop;
698
699 Spawn (Gprclean.all, Args, Success);
700
701 Free (Gprclean);
702
703 if Success then
704 Exit_Program (E_Success);
705 end if;
706 end;
707 end if;
708 end if;
709 end if;
710 end;
711
712 Index := 1;
713 while Index <= Last loop
714 declare
715 Arg : constant String := Argument (Index);
716
717 procedure Bad_Argument;
718 -- Signal bad argument
719
720 ------------------
721 -- Bad_Argument --
722 ------------------
723
724 procedure Bad_Argument is
725 begin
726 Fail ("invalid argument """ & Arg & """");
727 end Bad_Argument;
728
729 begin
730 if Arg'Length /= 0 then
731 if Arg (1) = '-' then
732 if Arg'Length = 1 then
733 Bad_Argument;
734 end if;
735
736 case Arg (2) is
737 when '-' =>
738 if Arg'Length > Subdirs_Option'Length
739 and then
740 Arg (1 .. Subdirs_Option'Length) = Subdirs_Option
741 then
742 null;
743 -- Subdirs are only used in gprclean
744
745 elsif Arg = Make_Util.Unchecked_Shared_Lib_Imports then
746 Opt.Unchecked_Shared_Lib_Imports := True;
747
748 else
749 Bad_Argument;
750 end if;
751
752 when 'a' =>
753 if Arg'Length < 4 then
754 Bad_Argument;
755 end if;
756
757 if Arg (3) = 'O' then
758 Add_Lib_Search_Dir (Arg (4 .. Arg'Last));
759
760 elsif Arg (3) = 'P' then
761 null;
762 -- This is only for gprclean
763
764 else
765 Bad_Argument;
766 end if;
767
768 when 'c' =>
769 Compile_Only := True;
770
771 when 'D' =>
772 if Object_Directory_Path /= null then
773 Fail ("duplicate -D switch");
774
775 elsif Project_File_Name /= null then
776 Fail ("-P and -D cannot be used simultaneously");
777 end if;
778
779 if Arg'Length > 2 then
780 declare
781 Dir : constant String := Arg (3 .. Arg'Last);
782 begin
783 if not Is_Directory (Dir) then
784 Fail (Dir & " is not a directory");
785 else
786 Add_Lib_Search_Dir (Dir);
787 end if;
788 end;
789
790 else
791 if Index = Last then
792 Fail ("no directory specified after -D");
793 end if;
794
795 Index := Index + 1;
796
797 declare
798 Dir : constant String := Argument (Index);
799 begin
800 if not Is_Directory (Dir) then
801 Fail (Dir & " is not a directory");
802 else
803 Add_Lib_Search_Dir (Dir);
804 end if;
805 end;
806 end if;
807
808 when 'e' =>
809 if Arg = "-eL" then
810 Follow_Links_For_Files := True;
811 Follow_Links_For_Dirs := True;
812
813 else
814 Bad_Argument;
815 end if;
816
817 when 'f' =>
818 Force_Deletions := True;
819 Directories_Must_Exist_In_Projects := False;
820
821 when 'F' =>
822 Full_Path_Name_For_Brief_Errors := True;
823
824 when 'h' =>
825 Usage;
826
827 when 'i' =>
828 if Arg'Length = 2 then
829 Bad_Argument;
830 end if;
831
832 Source_Index := 0;
833
834 for J in 3 .. Arg'Last loop
835 if Arg (J) not in '0' .. '9' then
836 Bad_Argument;
837 end if;
838
839 Source_Index :=
840 (20 * Source_Index) +
841 (Character'Pos (Arg (J)) - Character'Pos ('0'));
842 end loop;
843
844 when 'I' =>
845 if Arg = "-I-" then
846 Opt.Look_In_Primary_Dir := False;
847
848 else
849 if Arg'Length = 2 then
850 Bad_Argument;
851 end if;
852
853 Add_Lib_Search_Dir (Arg (3 .. Arg'Last));
854 end if;
855
856 when 'n' =>
857 Do_Nothing := True;
858
859 when 'P' =>
860 if Project_File_Name /= null then
861 Fail ("multiple -P switches");
862
863 elsif Object_Directory_Path /= null then
864 Fail ("-D and -P cannot be used simultaneously");
865
866 end if;
867
868 if Arg'Length > 2 then
869 declare
870 Prj : constant String := Arg (3 .. Arg'Last);
871 begin
872 if Prj'Length > 1
873 and then Prj (Prj'First) = '='
874 then
875 Project_File_Name :=
876 new String'
877 (Prj (Prj'First + 1 .. Prj'Last));
878 else
879 Project_File_Name := new String'(Prj);
880 end if;
881 end;
882
883 else
884 if Index = Last then
885 Fail ("no project specified after -P");
886 end if;
887
888 Index := Index + 1;
889 Project_File_Name := new String'(Argument (Index));
890 end if;
891
892 when 'q' =>
893 Quiet_Output := True;
894
895 when 'r' =>
896 null;
897 -- This is only for gprclean
898
899 when 'v' =>
900 if Arg = "-v" then
901 Verbose_Mode := True;
902
903 elsif Arg = "-vP0"
904 or else Arg = "-vP1"
905 or else Arg = "-vP2"
906 then
907 null;
908 -- This is only for gprclean
909
910 else
911 Bad_Argument;
912 end if;
913
914 when 'X' =>
915 if Arg'Length = 2 then
916 Bad_Argument;
917 end if;
918
919 when others =>
920 Bad_Argument;
921 end case;
922
923 else
924 Add_File (Arg, Source_Index);
925 end if;
926 end if;
927 end;
928
929 Index := Index + 1;
930 end loop;
931 end Parse_Cmd_Line;
932
933 -----------------------
934 -- Repinfo_File_Name --
935 -----------------------
936
937 function Repinfo_File_Name (Source : File_Name_Type) return String is
938 begin
939 return Get_Name_String (Source) & Repinfo_Suffix;
940 end Repinfo_File_Name;
941
942 --------------------
943 -- Tree_File_Name --
944 --------------------
945
946 function Tree_File_Name (Source : File_Name_Type) return String is
947 Src : constant String := Get_Name_String (Source);
948
949 begin
950 -- If source name has an extension, then replace it with the tree suffix
951
952 for Index in reverse Src'First + 1 .. Src'Last loop
953 if Src (Index) = '.' then
954 return Src (Src'First .. Index - 1) & Tree_Suffix;
955 end if;
956 end loop;
957
958 -- If there is no dot, or if it is the first character, just add the
959 -- tree suffix.
960
961 return Src & Tree_Suffix;
962 end Tree_File_Name;
963
964 -----------
965 -- Usage --
966 -----------
967
968 procedure Usage is
969 begin
970 if not Usage_Displayed then
971 Usage_Displayed := True;
972 Display_Copyright;
973 Put_Line ("Usage: gnatclean [switches] {[-innn] name}");
974 New_Line;
975
976 Display_Usage_Version_And_Help;
977
978 Put_Line (" names is one or more file names from which " &
979 "the .adb or .ads suffix may be omitted");
980 Put_Line (" names may be omitted if -P<project> is specified");
981 New_Line;
982
983 Put_Line (" --subdirs=dir real obj/lib/exec dirs are subdirs");
984 Put_Line (" " & Make_Util.Unchecked_Shared_Lib_Imports);
985 Put_Line (" Allow shared libraries to import static libraries");
986 New_Line;
987
988 Put_Line (" -c Only delete compiler generated files");
989 Put_Line (" -D dir Specify dir as the object library");
990 Put_Line (" -eL Follow symbolic links when processing " &
991 "project files");
992 Put_Line (" -f Force deletions of unwritable files");
993 Put_Line (" -F Full project path name " &
994 "in brief error messages");
995 Put_Line (" -h Display this message");
996 Put_Line (" -innn Index of unit in source for following names");
997 Put_Line (" -n Nothing to do: only list files to delete");
998 Put_Line (" -Pproj Use GNAT Project File proj");
999 Put_Line (" -q Be quiet/terse");
1000 Put_Line (" -r Clean all projects recursively");
1001 Put_Line (" -v Verbose mode");
1002 Put_Line (" -vPx Specify verbosity when parsing " &
1003 "GNAT Project Files");
1004 Put_Line (" -Xnm=val Specify an external reference " &
1005 "for GNAT Project Files");
1006 New_Line;
1007
1008 Put_Line (" -aPdir Add directory dir to project search path");
1009 New_Line;
1010
1011 Put_Line (" -aOdir Specify ALI/object files search path");
1012 Put_Line (" -Idir Like -aOdir");
1013 Put_Line (" -I- Don't look for source/library files " &
1014 "in the default directory");
1015 New_Line;
1016 end if;
1017 end Usage;
1018
1019 end Clean;