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 -- First check for --version or --help
183 Check_Version_And_Help
("GNATNAME", "2001", Usage
'Unrestricted_Access);
185 -- Now scan the other switches
187 Initialize_Option_Scan
;
189 -- Scan options first
192 case Getopt
("c: d: gnatep=! gnatep! gnateD! D: h P: v x: f:") is
198 Fail
("only one -P or -c switch may be specified");
202 File_Path
:= new String'(Parameter);
203 Create_Project := False;
206 Add_Source_Directory (Parameter);
209 Get_Directories (Parameter);
212 Foreign_Patterns.Increment_Last;
213 Foreign_Patterns.Table (Foreign_Patterns.Last) :=
214 new String'(Parameter
);
217 Preprocessor_Switches
.Increment_Last
;
218 Preprocessor_Switches
.Table
(Preprocessor_Switches
.Last
) :=
219 new String'('-' & Full_Switch & Parameter);
222 Usage_Needed := True;
226 Fail ("only one -c or -P switch may be specified");
230 File_Path := new String'(Parameter
);
231 Create_Project
:= True;
234 if Opt
.Verbose_Mode
then
235 Very_Verbose
:= True;
237 Opt
.Verbose_Mode
:= True;
241 Excluded_Patterns
.Increment_Last
;
242 Excluded_Patterns
.Table
(Excluded_Patterns
.Last
) :=
243 new String'(Parameter);
250 -- Now, get the name patterns, if any
254 S : String := Get_Argument (Do_Expansion => False);
258 Canonical_Case_File_Name (S);
259 Patterns.Increment_Last;
260 Patterns.Table (Patterns.Last) := new String'(S
);
265 when Invalid_Switch
=>
266 Fail
("invalid switch " & Full_Switch
);
275 if not Usage_Output
then
276 Usage_Needed
:= False;
277 Usage_Output
:= True;
278 Write_Str
("Usage: ");
279 Osint
.Write_Program_Name
;
280 Write_Line
(" [switches] naming-pattern [naming-patterns]");
282 Write_Line
("switches:");
284 Write_Line
(" -cfile create configuration pragmas file");
285 Write_Line
(" -ddir use dir as one of the source " &
287 Write_Line
(" -Dfile get source directories from file");
288 Write_Line
(" -fpat foreign pattern");
289 Write_Line
(" -gnateDsym=v preprocess with symbol definition");
290 Write_Line
(" -gnatep=data preprocess files with data file");
291 Write_Line
(" -h output this help message");
292 Write_Line
(" -Pproj update or create project file proj");
293 Write_Line
(" -v verbose output");
294 Write_Line
(" -v -v very verbose output");
295 Write_Line
(" -xpat exclude pattern pat");
299 -- Start of processing for Gnatname
302 Prj
.Set_Mode
(Prj
.Ada_Only
);
304 -- Add the directory where gnatname is invoked in front of the
305 -- path, if gnatname is invoked with directory information.
306 -- Only do this if the platform is not VMS, where the notion of path
307 -- does not really exist.
309 if not Hostparm
.OpenVMS
then
311 Command
: constant String := Command_Name
;
314 for Index
in reverse Command
'Range loop
315 if Command
(Index
) = Directory_Separator
then
317 Absolute_Dir
: constant String :=
319 (Command
(Command
'First .. Index
));
321 PATH
: constant String :=
327 Setenv
("PATH", PATH
);
338 Excluded_Patterns
.Set_Last
(0);
339 Foreign_Patterns
.Set_Last
(0);
340 Patterns
.Set_Last
(0);
341 Source_Directories
.Set_Last
(0);
342 Preprocessor_Switches
.Set_Last
(0);
348 if Opt
.Verbose_Mode
then
356 -- If no pattern was specified, print the usage and return
358 if Patterns
.Last
= 0 and Foreign_Patterns
.Last
= 0 then
363 -- If no source directory was specified, use the current directory as the
364 -- unique directory. Note that if a file was specified with directory
365 -- information, the current directory is the directory of the specified
368 if Source_Directories
.Last
= 0 then
369 Source_Directories
.Increment_Last
;
370 Source_Directories
.Table
(Source_Directories
.Last
) := new String'(".");
374 Directories : Argument_List (1 .. Integer (Source_Directories.Last));
375 Name_Patterns : Argument_List (1 .. Integer (Patterns.Last));
376 Excl_Patterns : Argument_List (1 .. Integer (Excluded_Patterns.Last));
377 Frgn_Patterns : Argument_List (1 .. Integer (Foreign_Patterns.Last));
378 Prep_Switches : Argument_List
379 (1 .. Integer (Preprocessor_Switches.Last));
382 -- Build the Directories and Name_Patterns arguments
384 for Index in Directories'Range loop
385 Directories (Index) := Source_Directories.Table (Index);
388 for Index in Name_Patterns'Range loop
389 Name_Patterns (Index) := Patterns.Table (Index);
392 for Index in Excl_Patterns'Range loop
393 Excl_Patterns (Index) := Excluded_Patterns.Table (Index);
396 for Index in Frgn_Patterns'Range loop
397 Frgn_Patterns (Index) := Foreign_Patterns.Table (Index);
400 for Index in Prep_Switches'Range loop
401 Prep_Switches (Index) := Preprocessor_Switches.Table (Index);
404 -- Call Prj.Makr.Make where the real work is done
407 (File_Path => File_Path.all,
408 Project_File => Create_Project,
409 Directories => Directories,
410 Name_Patterns => Name_Patterns,
411 Excluded_Patterns => Excl_Patterns,
412 Foreign_Patterns => Frgn_Patterns,
413 Preproc_Switches => Prep_Switches,
414 Very_Verbose => Very_Verbose);
417 if Opt.Verbose_Mode then