1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2007, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
28 with Osint
; use Osint
;
29 with Output
; use Output
;
31 with Switch
; use Switch
;
34 with Ada
.Command_Line
; use Ada
.Command_Line
;
35 with Ada
.Text_IO
; use Ada
.Text_IO
;
36 with GNAT
.Command_Line
; use GNAT
.Command_Line
;
37 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
41 Usage_Output
: Boolean := False;
42 -- Set to True when usage is output, to avoid multiple output
44 Usage_Needed
: Boolean := False;
45 -- Set to True by -h switch
47 Version_Output
: Boolean := False;
48 -- Set to True when version is output, to avoid multiple output
50 Very_Verbose
: Boolean := False;
51 -- Set to True with -v -v
53 Create_Project
: Boolean := False;
54 -- Set to True with a -P switch
56 File_Path
: String_Access
:= new String'("gnat.adc");
57 -- Path name of the file specified by -c or -P switch
59 File_Set : Boolean := False;
60 -- Set to True by -c or -P switch.
61 -- Used to detect multiple -c/-P switches.
63 package Excluded_Patterns is new Table.Table
64 (Table_Component_Type => String_Access,
65 Table_Index_Type => Natural,
68 Table_Increment => 100,
69 Table_Name => "Gnatname.Excluded_Patterns");
70 -- Table to accumulate the negative patterns
72 package Foreign_Patterns is new Table.Table
73 (Table_Component_Type => String_Access,
74 Table_Index_Type => Natural,
77 Table_Increment => 100,
78 Table_Name => "Gnatname.Foreign_Patterns");
79 -- Table to accumulate the foreign patterns
81 package Patterns is new Table.Table
82 (Table_Component_Type => String_Access,
83 Table_Index_Type => Natural,
86 Table_Increment => 100,
87 Table_Name => "Gnatname.Patterns");
88 -- Table to accumulate the name patterns
90 package Source_Directories is new Table.Table
91 (Table_Component_Type => String_Access,
92 Table_Index_Type => Natural,
95 Table_Increment => 100,
96 Table_Name => "Gnatname.Source_Directories");
97 -- Table to accumulate the source directories specified directly with -d
98 -- or indirectly with -D.
100 package Preprocessor_Switches is new Table.Table
101 (Table_Component_Type => String_Access,
102 Table_Index_Type => Natural,
103 Table_Low_Bound => 0,
105 Table_Increment => 100,
106 Table_Name => "Gnatname.Preprocessor_Switches");
107 -- Table to store the preprocessor switches to be used in the call
110 procedure Output_Version;
111 -- Print name and version
117 -- Scan the command line arguments
119 procedure Add_Source_Directory (S : String);
120 -- Add S in the Source_Directories table
122 procedure Get_Directories (From_File : String);
123 -- Read a source directory text file
125 --------------------------
126 -- Add_Source_Directory --
127 --------------------------
129 procedure Add_Source_Directory (S : String) is
131 Source_Directories.Increment_Last;
132 Source_Directories.Table (Source_Directories.Last) := new String'(S
);
133 end Add_Source_Directory
;
135 ---------------------
136 -- Get_Directories --
137 ---------------------
139 procedure Get_Directories
(From_File
: String) is
140 File
: Ada
.Text_IO
.File_Type
;
141 Line
: String (1 .. 2_000
);
145 Open
(File
, In_File
, From_File
);
147 while not End_Of_File
(File
) loop
148 Get_Line
(File
, Line
, Last
);
151 Add_Source_Directory
(Line
(1 .. Last
));
159 Fail
("cannot open source directory """ & From_File
& '"');
166 procedure Output_Version
is
168 if not Version_Output
then
169 Version_Output
:= True;
171 Display_Version
("GNATNAME", "2001");
179 procedure Scan_Args
is
181 procedure Check_Version_And_Help
is new Check_Version_And_Help_G
(Usage
);
183 -- Start of processing for Scan_Args
186 -- First check for --version or --help
188 Check_Version_And_Help
("GNATNAME", "2001");
190 -- Now scan the other switches
192 Initialize_Option_Scan
;
194 -- Scan options first
197 case Getopt
("c: d: gnatep=! gnatep! gnateD! D: h P: v x: f:") is
203 Fail
("only one -P or -c switch may be specified");
207 File_Path
:= new String'(Parameter);
208 Create_Project := False;
211 Add_Source_Directory (Parameter);
214 Get_Directories (Parameter);
217 Foreign_Patterns.Increment_Last;
218 Foreign_Patterns.Table (Foreign_Patterns.Last) :=
219 new String'(Parameter
);
222 Preprocessor_Switches
.Increment_Last
;
223 Preprocessor_Switches
.Table
(Preprocessor_Switches
.Last
) :=
224 new String'('-' & Full_Switch & Parameter);
227 Usage_Needed := True;
231 Fail ("only one -c or -P switch may be specified");
235 File_Path := new String'(Parameter
);
236 Create_Project
:= True;
239 if Opt
.Verbose_Mode
then
240 Very_Verbose
:= True;
242 Opt
.Verbose_Mode
:= True;
246 Excluded_Patterns
.Increment_Last
;
247 Excluded_Patterns
.Table
(Excluded_Patterns
.Last
) :=
248 new String'(Parameter);
255 -- Now, get the name patterns, if any
259 S : String := Get_Argument (Do_Expansion => False);
263 Canonical_Case_File_Name (S);
264 Patterns.Increment_Last;
265 Patterns.Table (Patterns.Last) := new String'(S
);
270 when Invalid_Switch
=>
271 Fail
("invalid switch " & Full_Switch
);
280 if not Usage_Output
then
281 Usage_Needed
:= False;
282 Usage_Output
:= True;
283 Write_Str
("Usage: ");
284 Osint
.Write_Program_Name
;
285 Write_Line
(" [switches] naming-pattern [naming-patterns]");
287 Write_Line
("switches:");
289 Write_Line
(" -cfile create configuration pragmas file");
290 Write_Line
(" -ddir use dir as one of the source " &
292 Write_Line
(" -Dfile get source directories from file");
293 Write_Line
(" -fpat foreign pattern");
294 Write_Line
(" -gnateDsym=v preprocess with symbol definition");
295 Write_Line
(" -gnatep=data preprocess files with data file");
296 Write_Line
(" -h output this help message");
297 Write_Line
(" -Pproj update or create project file proj");
298 Write_Line
(" -v verbose output");
299 Write_Line
(" -v -v very verbose output");
300 Write_Line
(" -xpat exclude pattern pat");
304 -- Start of processing for Gnatname
307 Prj
.Set_Mode
(Prj
.Ada_Only
);
309 -- Add the directory where gnatname is invoked in front of the
310 -- path, if gnatname is invoked with directory information.
311 -- Only do this if the platform is not VMS, where the notion of path
312 -- does not really exist.
314 if not Hostparm
.OpenVMS
then
316 Command
: constant String := Command_Name
;
319 for Index
in reverse Command
'Range loop
320 if Command
(Index
) = Directory_Separator
then
322 Absolute_Dir
: constant String :=
324 (Command
(Command
'First .. Index
));
326 PATH
: constant String :=
332 Setenv
("PATH", PATH
);
343 Excluded_Patterns
.Set_Last
(0);
344 Foreign_Patterns
.Set_Last
(0);
345 Patterns
.Set_Last
(0);
346 Source_Directories
.Set_Last
(0);
347 Preprocessor_Switches
.Set_Last
(0);
353 if Opt
.Verbose_Mode
then
361 -- If no pattern was specified, print the usage and return
363 if Patterns
.Last
= 0 and Foreign_Patterns
.Last
= 0 then
368 -- If no source directory was specified, use the current directory as the
369 -- unique directory. Note that if a file was specified with directory
370 -- information, the current directory is the directory of the specified
373 if Source_Directories
.Last
= 0 then
374 Source_Directories
.Increment_Last
;
375 Source_Directories
.Table
(Source_Directories
.Last
) := new String'(".");
379 Directories : Argument_List (1 .. Integer (Source_Directories.Last));
380 Name_Patterns : Argument_List (1 .. Integer (Patterns.Last));
381 Excl_Patterns : Argument_List (1 .. Integer (Excluded_Patterns.Last));
382 Frgn_Patterns : Argument_List (1 .. Integer (Foreign_Patterns.Last));
383 Prep_Switches : Argument_List
384 (1 .. Integer (Preprocessor_Switches.Last));
387 -- Build the Directories and Name_Patterns arguments
389 for Index in Directories'Range loop
390 Directories (Index) := Source_Directories.Table (Index);
393 for Index in Name_Patterns'Range loop
394 Name_Patterns (Index) := Patterns.Table (Index);
397 for Index in Excl_Patterns'Range loop
398 Excl_Patterns (Index) := Excluded_Patterns.Table (Index);
401 for Index in Frgn_Patterns'Range loop
402 Frgn_Patterns (Index) := Foreign_Patterns.Table (Index);
405 for Index in Prep_Switches'Range loop
406 Prep_Switches (Index) := Preprocessor_Switches.Table (Index);
409 -- Call Prj.Makr.Make where the real work is done
412 (File_Path => File_Path.all,
413 Project_File => Create_Project,
414 Directories => Directories,
415 Name_Patterns => Name_Patterns,
416 Excluded_Patterns => Excl_Patterns,
417 Foreign_Patterns => Frgn_Patterns,
418 Preproc_Switches => Prep_Switches,
419 Very_Verbose => Very_Verbose);
422 if Opt.Verbose_Mode then