1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2014, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Ada.Command_Line; use Ada.Command_Line;
27 with Ada.Text_IO; use Ada.Text_IO;
29 with GNAT.Command_Line; use GNAT.Command_Line;
30 with GNAT.Dynamic_Tables;
31 with GNAT.OS_Lib; use GNAT.OS_Lib;
35 with Osint; use Osint;
36 with Output; use Output;
39 with Switch; use Switch;
42 with System.Regexp; use System.Regexp;
46 Subdirs_Switch : constant String := "--subdirs=";
48 Usage_Output : Boolean := False;
49 -- Set to True when usage is output, to avoid multiple output
51 Usage_Needed : Boolean := False;
52 -- Set to True by -h switch
54 Version_Output : Boolean := False;
55 -- Set to True when version is output, to avoid multiple output
57 Very_Verbose : Boolean := False;
58 -- Set to True with -v -v
60 Create_Project : Boolean := False;
61 -- Set to True with a -P switch
63 File_Path : String_Access := new String'("gnat.adc");
64 -- Path name of the file specified by -c or -P switch
66 File_Set : Boolean := False;
67 -- Set to True by -c or -P switch.
68 -- Used to detect multiple -c/-P switches.
70 package Patterns is new GNAT.Dynamic_Tables
71 (Table_Component_Type => String_Access,
72 Table_Index_Type => Natural,
75 Table_Increment => 100);
76 -- Table to accumulate the patterns
78 type Argument_Data is record
79 Directories : Patterns.Instance;
80 Name_Patterns : Patterns.Instance;
81 Excluded_Patterns : Patterns.Instance;
82 Foreign_Patterns : Patterns.Instance;
85 package Arguments is new Table.Table
86 (Table_Component_Type => Argument_Data,
87 Table_Index_Type => Natural,
90 Table_Increment => 100,
91 Table_Name => "Gnatname.Arguments");
92 -- Table to accumulate directories and patterns
94 package Preprocessor_Switches is new Table.Table
95 (Table_Component_Type => String_Access,
96 Table_Index_Type => Natural,
99 Table_Increment => 100,
100 Table_Name => "Gnatname.Preprocessor_Switches");
101 -- Table to store the preprocessor switches to be used in the call
104 procedure Output_Version;
105 -- Print name and version
111 -- Scan the command line arguments
113 procedure Add_Source_Directory (S : String);
114 -- Add S in the Source_Directories table
116 procedure Get_Directories (From_File : String);
117 -- Read a source directory text file
119 --------------------------
120 -- Add_Source_Directory --
121 --------------------------
123 procedure Add_Source_Directory (S : String) is
126 (Arguments.Table (Arguments.Last).Directories, new String'(S));
127 end Add_Source_Directory;
129 ---------------------
130 -- Get_Directories --
131 ---------------------
133 procedure Get_Directories (From_File : String) is
134 File : Ada.Text_IO.File_Type;
135 Line : String (1 .. 2_000);
139 Open (File, In_File, From_File);
141 while not End_Of_File (File) loop
142 Get_Line (File, Line, Last);
145 Add_Source_Directory (Line (1 .. Last));
153 Fail ("cannot open source directory file """ & From_File & '"');
160 procedure Output_Version is
162 if not Version_Output then
163 Version_Output := True;
165 Display_Version ("GNATNAME", "2001");
173 procedure Scan_Args is
175 procedure Check_Version_And_Help is new Check_Version_And_Help_G (Usage);
177 Project_File_Name_Expected : Boolean;
179 Pragmas_File_Expected : Boolean;
181 Directory_Expected : Boolean;
183 Dir_File_Name_Expected : Boolean;
185 Foreign_Pattern_Expected : Boolean;
187 Excluded_Pattern_Expected : Boolean;
189 procedure Check_Regular_Expression (S : String);
190 -- Compile string S into a Regexp, fail if any error
192 -----------------------------
193 -- Check_Regular_Expression--
194 -----------------------------
196 procedure Check_Regular_Expression (S : String) is
198 pragma Warnings (Off, Dummy);
200 Dummy := Compile (S, Glob => True);
202 when Error_In_Regexp =>
203 Fail ("invalid regular expression """ & S & """");
204 end Check_Regular_Expression;
206 -- Start of processing for Scan_Args
209 -- First check for --version or --help
211 Check_Version_And_Help ("GNATNAME", "2001");
213 -- Now scan the other switches
215 Project_File_Name_Expected := False;
216 Pragmas_File_Expected := False;
217 Directory_Expected := False;
218 Dir_File_Name_Expected := False;
219 Foreign_Pattern_Expected := False;
220 Excluded_Pattern_Expected := False;
222 for Next_Arg in 1 .. Argument_Count loop
224 Next_Argv : constant String := Argument (Next_Arg);
225 Arg : String (1 .. Next_Argv'Length) := Next_Argv;
228 if Arg'Length > 0 then
232 if Project_File_Name_Expected then
233 if Arg (1) = '-' then
234 Fail ("project file name missing");
238 File_Path := new String'(Arg);
239 Project_File_Name_Expected := False;
244 elsif Pragmas_File_Expected then
246 File_Path := new String'(Arg);
247 Create_Project := False;
248 Pragmas_File_Expected := False;
252 elsif Directory_Expected then
253 Add_Source_Directory (Arg);
254 Directory_Expected := False;
258 elsif Dir_File_Name_Expected then
259 Get_Directories (Arg);
260 Dir_File_Name_Expected := False;
264 elsif Foreign_Pattern_Expected then
266 (Arguments.Table (Arguments.Last).Foreign_Patterns,
268 Check_Regular_Expression (Arg);
269 Foreign_Pattern_Expected := False;
273 elsif Excluded_Pattern_Expected then
275 (Arguments.Table (Arguments.Last).Excluded_Patterns,
277 Check_Regular_Expression (Arg);
278 Excluded_Pattern_Expected := False;
280 -- There must be at least one Ada pattern or one foreign
281 -- pattern for the previous section.
285 elsif Arg = "--and" then
288 (Arguments.Table (Arguments.Last).Name_Patterns) = 0
291 (Arguments.Table (Arguments.Last).Foreign_Patterns) = 0
297 -- If no directory were specified for the previous section,
298 -- then the directory is the project directory.
301 (Arguments.Table (Arguments.Last).Directories) = 0
304 (Arguments.Table (Arguments.Last).Directories,
308 -- Add and initialize another component to Arguments table
311 New_Arguments : Argument_Data;
312 pragma Warnings (Off, New_Arguments);
313 -- Declaring this defaulted initialized object ensures
314 -- that the new allocated component of table Arguments
315 -- is correctly initialized.
317 -- This is VERY ugly, Table should never be used with
318 -- data requiring default initialization. We should
319 -- find a way to avoid violating this rule ???
322 Arguments.Append (New_Arguments);
326 (Arguments.Table (Arguments.Last).Directories);
328 (Arguments.Table (Arguments.Last).Directories, 0);
330 (Arguments.Table (Arguments.Last).Name_Patterns);
332 (Arguments.Table (Arguments.Last).Name_Patterns, 0);
334 (Arguments.Table (Arguments.Last).Excluded_Patterns);
336 (Arguments.Table (Arguments.Last).Excluded_Patterns, 0);
338 (Arguments.Table (Arguments.Last).Foreign_Patterns);
340 (Arguments.Table (Arguments.Last).Foreign_Patterns, 0);
342 -- Subdirectory switch
344 elsif Arg'Length > Subdirs_Switch'Length
345 and then Arg (1 .. Subdirs_Switch'Length) = Subdirs_Switch
348 new String'(Arg (Subdirs_Switch'Length + 1 .. Arg'Last));
352 elsif Arg = "--no-backup" then
353 Opt.No_Backup := True;
357 elsif Arg'Length >= 2 and then Arg (1 .. 2) = "-c" then
359 Fail ("only one -P or -c switch may be specified");
362 if Arg'Length = 2 then
363 Pragmas_File_Expected := True;
365 if Next_Arg = Argument_Count then
366 Fail ("configuration pragmas file name missing");
371 File_Path := new String'(Arg (3 .. Arg'Last));
372 Create_Project := False;
377 elsif Arg'Length >= 2 and then Arg (1 .. 2) = "-d" then
378 if Arg'Length = 2 then
379 Directory_Expected := True;
381 if Next_Arg = Argument_Count then
382 Fail ("directory name missing");
386 Add_Source_Directory (Arg (3 .. Arg'Last));
391 elsif Arg'Length >= 2 and then Arg (1 .. 2) = "-D" then
392 if Arg'Length = 2 then
393 Dir_File_Name_Expected := True;
395 if Next_Arg = Argument_Count then
396 Fail ("directory list file name missing");
400 Get_Directories (Arg (3 .. Arg'Last));
405 elsif Arg = "-eL" then
406 Opt.Follow_Links_For_Files := True;
407 Opt.Follow_Links_For_Dirs := True;
411 elsif Arg'Length >= 2 and then Arg (1 .. 2) = "-f" then
412 if Arg'Length = 2 then
413 Foreign_Pattern_Expected := True;
415 if Next_Arg = Argument_Count then
416 Fail ("foreign pattern missing");
421 (Arguments.Table (Arguments.Last).Foreign_Patterns,
422 new String'(Arg (3 .. Arg'Last)));
423 Check_Regular_Expression (Arg (3 .. Arg'Last));
426 -- -gnatep or -gnateD
428 elsif Arg'Length > 7 and then
429 (Arg (1 .. 7) = "-gnatep" or else Arg (1 .. 7) = "-gnateD")
431 Preprocessor_Switches.Append (new String'(Arg));
435 elsif Arg = "-h" then
436 Usage_Needed := True;
440 elsif Arg'Length >= 2 and then Arg (1 .. 2) = "-P" then
442 Fail ("only one -c or -P switch may be specified");
445 if Arg'Length = 2 then
446 if Next_Arg = Argument_Count then
447 Fail ("project file name missing");
450 Project_File_Name_Expected := True;
455 File_Path := new String'(Arg (3 .. Arg'Last));
458 Create_Project := True;
462 elsif Arg = "-v" then
463 if Opt.Verbose_Mode then
464 Very_Verbose := True;
466 Opt.Verbose_Mode := True;
471 elsif Arg'Length >= 2 and then Arg (1 .. 2) = "-x" then
472 if Arg'Length = 2 then
473 Excluded_Pattern_Expected := True;
475 if Next_Arg = Argument_Count then
476 Fail ("excluded pattern missing");
481 (Arguments.Table (Arguments.Last).Excluded_Patterns,
482 new String'(Arg (3 .. Arg'Last)));
483 Check_Regular_Expression (Arg (3 .. Arg'Last));
486 -- Junk switch starting with minus
488 elsif Arg (1) = '-' then
489 Fail ("wrong switch: " & Arg);
491 -- Not a recognized switch, assume file name
494 Canonical_Case_File_Name (Arg);
496 (Arguments.Table (Arguments.Last).Name_Patterns,
498 Check_Regular_Expression (Arg);
511 if not Usage_Output then
512 Usage_Needed := False;
513 Usage_Output := True;
514 Write_Str ("Usage: ");
515 Osint.Write_Program_Name;
516 Write_Line (" [switches] naming-pattern [naming-patterns]");
517 Write_Line (" {--and [switches] naming-pattern [naming-patterns]}");
519 Write_Line ("switches:");
521 Display_Usage_Version_And_Help;
523 Write_Line (" --subdirs=dir real obj/lib/exec dirs are subdirs");
524 Write_Line (" --no-backup do not create backup of project file");
527 Write_Line (" --and use different patterns");
530 Write_Line (" -cfile create configuration pragmas file");
531 Write_Line (" -ddir use dir as one of the source " &
533 Write_Line (" -Dfile get source directories from file");
534 Write_Line (" -eL follow symbolic links when processing " &
536 Write_Line (" -fpat foreign pattern");
537 Write_Line (" -gnateDsym=v preprocess with symbol definition");
538 Write_Line (" -gnatep=data preprocess files with data file");
539 Write_Line (" -h output this help message");
540 Write_Line (" -Pproj update or create project file proj");
541 Write_Line (" -v verbose output");
542 Write_Line (" -v -v very verbose output");
543 Write_Line (" -xpat exclude pattern pat");
547 -- Start of processing for Gnatname
550 -- Add the directory where gnatname is invoked in front of the
551 -- path, if gnatname is invoked with directory information.
552 -- Only do this if the platform is not VMS, where the notion of path
553 -- does not really exist.
555 if not Hostparm.OpenVMS then
557 Command : constant String := Command_Name;
560 for Index in reverse Command'Range loop
561 if Command (Index) = Directory_Separator then
563 Absolute_Dir : constant String :=
565 (Command (Command'First .. Index));
567 PATH : constant String :=
573 Setenv ("PATH", PATH);
584 Arguments.Set_Last (0);
586 New_Arguments : Argument_Data;
587 pragma Warnings (Off, New_Arguments);
588 -- Declaring this defaulted initialized object ensures
589 -- that the new allocated component of table Arguments
590 -- is correctly initialized.
592 Arguments.Append (New_Arguments);
594 Patterns.Init (Arguments.Table (1).Directories);
595 Patterns.Set_Last (Arguments.Table (1).Directories, 0);
596 Patterns.Init (Arguments.Table (1).Name_Patterns);
597 Patterns.Set_Last (Arguments.Table (1).Name_Patterns, 0);
598 Patterns.Init (Arguments.Table (1).Excluded_Patterns);
599 Patterns.Set_Last (Arguments.Table (1).Excluded_Patterns, 0);
600 Patterns.Init (Arguments.Table (1).Foreign_Patterns);
601 Patterns.Set_Last (Arguments.Table (1).Foreign_Patterns, 0);
603 Preprocessor_Switches.Set_Last (0);
609 if Opt.Verbose_Mode then
617 -- If no Ada or foreign pattern was specified, print the usage and return
619 if Patterns.Last (Arguments.Table (Arguments.Last).Name_Patterns) = 0
621 Patterns.Last (Arguments.Table (Arguments.Last).Foreign_Patterns) = 0
623 if Argument_Count = 0 then
625 elsif not Usage_Output then
632 -- If no source directory was specified, use the current directory as the
633 -- unique directory. Note that if a file was specified with directory
634 -- information, the current directory is the directory of the specified
638 (Arguments.Table (Arguments.Last).Directories) = 0
641 (Arguments.Table (Arguments.Last).Directories, new String'("."));
647 Prep_Switches : Argument_List
648 (1 .. Integer (Preprocessor_Switches.Last));
651 for Index in Prep_Switches'Range loop
652 Prep_Switches (Index) := Preprocessor_Switches.Table (Index);
656 (File_Path => File_Path.all,
657 Project_File => Create_Project,
658 Preproc_Switches => Prep_Switches,
659 Very_Verbose => Very_Verbose,
660 Flags => Gnatmake_Flags);
663 -- Process each section successively
665 for J in 1 .. Arguments.Last loop
667 Directories : Argument_List
669 (Patterns.Last (Arguments.Table (J).Directories)));
670 Name_Patterns : Prj.Makr.Regexp_List
672 (Patterns.Last (Arguments.Table (J).Name_Patterns)));
673 Excl_Patterns : Prj.Makr.Regexp_List
675 (Patterns.Last (Arguments.Table (J).Excluded_Patterns)));
676 Frgn_Patterns : Prj.Makr.Regexp_List
678 (Patterns.Last (Arguments.Table (J).Foreign_Patterns)));
681 -- Build the Directories and Patterns arguments
683 for Index in Directories'Range loop
684 Directories (Index) :=
685 Arguments.Table (J).Directories.Table (Index);
688 for Index in Name_Patterns'Range loop
689 Name_Patterns (Index) :=
691 (Arguments.Table (J).Name_Patterns.Table (Index).all,
695 for Index in Excl_Patterns'Range loop
696 Excl_Patterns (Index) :=
698 (Arguments.Table (J).Excluded_Patterns.Table (Index).all,
702 for Index in Frgn_Patterns'Range loop
703 Frgn_Patterns (Index) :=
705 (Arguments.Table (J).Foreign_Patterns.Table (Index).all,
709 -- Call Prj.Makr.Process where the real work is done
712 (Directories => Directories,
713 Name_Patterns => Name_Patterns,
714 Excluded_Patterns => Excl_Patterns,
715 Foreign_Patterns => Frgn_Patterns);
723 if Opt.Verbose_Mode then