1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
|
------------------------------------------------------------------------------
-- --
-- GNAT COMPILER COMPONENTS --
-- --
-- G N A T M A I N --
-- --
-- B o d y --
-- --
-- $Revision: 1.1 $
-- --
-- Copyright (C) 1992-2001 Free Software Foundation, Inc. --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 2, or (at your option) any later ver- --
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
-- for more details. You should have received a copy of the GNU General --
-- Public License distributed with GNAT; see file COPYING. If not, write --
-- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
-- MA 02111-1307, USA. --
-- --
-- GNAT was originally developed by the GNAT team at New York University. --
-- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
-- --
------------------------------------------------------------------------------
with Csets;
with GNAT.Case_Util;
with GNAT.OS_Lib; use GNAT.OS_Lib;
with Namet; use Namet;
with Opt;
with Osint; use Osint;
with Output; use Output;
with Prj; use Prj;
with Prj.Env;
with Prj.Ext; use Prj.Ext;
with Prj.Pars;
with Prj.Util; use Prj.Util;
with Snames; use Snames;
with Stringt; use Stringt;
with Table;
with Types; use Types;
procedure Gnatmain is
Ada_Include_Path : constant String := "ADA_INCLUDE_PATH";
Ada_Objects_Path : constant String := "ADA_OBJECTS_PATH";
type Tool_Type is (None, List, Xref, Find, Stub, Make, Comp, Bind, Link);
-- The tool that is going to be called
Tool : Tool_Type := None;
-- For each tool, Tool_Package_Names contains the name of the
-- corresponding package in the project file.
Tool_Package_Names : constant array (Tool_Type) of Name_Id :=
(None => No_Name,
List => Name_Gnatls,
Xref => Name_Cross_Reference,
Find => Name_Finder,
Stub => Name_Gnatstub,
Comp => No_Name,
Make => No_Name,
Bind => No_Name,
Link => No_Name);
-- For each tool, Tool_Names contains the name of the executable
-- to be spawned.
Gnatmake : constant String_Access := new String'("gnatmake");
Tool_Names : constant array (Tool_Type) of String_Access :=
(None => null,
List => new String'("gnatls"),
Xref => new String'("gnatxref"),
Find => new String'("gnatfind"),
Stub => new String'("gnatstub"),
Comp => Gnatmake,
Make => Gnatmake,
Bind => Gnatmake,
Link => Gnatmake);
Project_File : String_Access;
Project : Prj.Project_Id;
Current_Verbosity : Prj.Verbosity := Prj.Default;
-- This flag indicates a switch -p (for gnatxref and gnatfind) for
-- an old fashioned project file. -p cannot be used in conjonction
-- with -P.
Old_Project_File_Used : Boolean := False;
Next_Arg : Positive;
-- A table to keep the switches on the command line
package Saved_Switches is new Table.Table (
Table_Component_Type => String_Access,
Table_Index_Type => Integer,
Table_Low_Bound => 1,
Table_Initial => 20,
Table_Increment => 100,
Table_Name => "Gnatmain.Saved_Switches");
-- A table to keep the switches from the project file
package Switches is new Table.Table (
Table_Component_Type => String_Access,
Table_Index_Type => Integer,
Table_Low_Bound => 1,
Table_Initial => 20,
Table_Increment => 100,
Table_Name => "Gnatmain.Switches");
procedure Add_Switch (Argv : String; And_Save : Boolean);
-- Add a switch in one of the tables above
procedure Display (Program : String; Args : Argument_List);
-- Displays Program followed by the arguments in Args
function Index (Char : Character; Str : String) return Natural;
-- Returns the first occurrence of Char in Str.
-- Returns 0 if Char is not in Str.
procedure Scan_Arg (Argv : String; And_Save : Boolean);
-- Scan and process arguments. Argv is a single argument.
procedure Usage;
-- Output usage
----------------
-- Add_Switch --
----------------
procedure Add_Switch (Argv : String; And_Save : Boolean) is
begin
if And_Save then
Saved_Switches.Increment_Last;
Saved_Switches.Table (Saved_Switches.Last) := new String'(Argv);
else
Switches.Increment_Last;
Switches.Table (Switches.Last) := new String'(Argv);
end if;
end Add_Switch;
-------------
-- Display --
-------------
procedure Display (Program : String; Args : Argument_List) is
begin
if not Opt.Quiet_Output then
Write_Str (Program);
for J in Args'Range loop
Write_Str (" ");
Write_Str (Args (J).all);
end loop;
Write_Eol;
end if;
end Display;
-----------
-- Index --
-----------
function Index (Char : Character; Str : String) return Natural is
begin
for Index in Str'Range loop
if Str (Index) = Char then
return Index;
end if;
end loop;
return 0;
end Index;
--------------
-- Scan_Arg --
--------------
procedure Scan_Arg (Argv : String; And_Save : Boolean) is
begin
pragma Assert (Argv'First = 1);
if Argv'Length = 0 then
return;
end if;
if Argv (1) = Switch_Character or else Argv (1) = '-' then
if Argv'Length = 1 then
Fail ("switch character cannot be followed by a blank");
end if;
-- The two style project files (-p and -P) cannot be used together
if (Tool = Find or else Tool = Xref)
and then Argv (2) = 'p'
then
Old_Project_File_Used := True;
if Project_File /= null then
Fail ("-P and -p cannot be used together");
end if;
end if;
-- -q Be quiet: do not output tool command
if Argv (2 .. Argv'Last) = "q" then
Opt.Quiet_Output := True;
-- Only gnatstub and gnatmake have a -q switch
if Tool = Stub or else Tool_Names (Tool) = Gnatmake then
Add_Switch (Argv, And_Save);
end if;
-- gnatmake will take care of the project file related switches
elsif Tool_Names (Tool) = Gnatmake then
Add_Switch (Argv, And_Save);
-- -vPx Specify verbosity while parsing project files
elsif Argv'Length = 4 and then Argv (2 .. 3) = "vP" then
case Argv (4) is
when '0' =>
Current_Verbosity := Prj.Default;
when '1' =>
Current_Verbosity := Prj.Medium;
when '2' =>
Current_Verbosity := Prj.High;
when others =>
null;
end case;
-- -Pproject_file Specify project file to be used
elsif Argv'Length >= 3 and then Argv (2) = 'P' then
-- Only one -P switch can be used
if Project_File /= null then
Fail (Argv & ": second project file forbidden (first is """ &
Project_File.all & """)");
-- The two style project files (-p and -P) cannot be used together
elsif Old_Project_File_Used then
Fail ("-p and -P cannot be used together");
else
Project_File := new String'(Argv (3 .. Argv'Last));
end if;
-- -Xexternal=value Specify an external reference to be used
-- in project files
elsif Argv'Length >= 5 and then Argv (2) = 'X' then
declare
Equal_Pos : constant Natural :=
Index ('=', Argv (3 .. Argv'Last));
begin
if Equal_Pos >= 4 and then
Equal_Pos /= Argv'Last then
Add (External_Name => Argv (3 .. Equal_Pos - 1),
Value => Argv (Equal_Pos + 1 .. Argv'Last));
else
Fail (Argv & " is not a valid external assignment.");
end if;
end;
else
Add_Switch (Argv, And_Save);
end if;
else
Add_Switch (Argv, And_Save);
end if;
end Scan_Arg;
-----------
-- Usage --
-----------
procedure Usage is
begin
Write_Str ("Usage: ");
Write_Eol;
Osint.Write_Program_Name;
Write_Str (" list switches [list of object files]");
Write_Eol;
Osint.Write_Program_Name;
Write_Str (" xref switches file1 file2 ...");
Write_Eol;
Osint.Write_Program_Name;
Write_Str (" find switches pattern[:sourcefile[:line[:column]]] " &
"[file1 file2 ...]");
Write_Eol;
Osint.Write_Program_Name;
Write_Str (" stub switches filename [directory]");
Write_Eol;
Osint.Write_Program_Name;
Write_Str (" comp switches files");
Write_Eol;
Osint.Write_Program_Name;
Write_Str (" make switches [files]");
Write_Eol;
Osint.Write_Program_Name;
Write_Str (" bind switches files");
Write_Eol;
Osint.Write_Program_Name;
Write_Str (" link switches files");
Write_Eol;
Write_Eol;
Write_Str ("switches interpreted by ");
Osint.Write_Program_Name;
Write_Str (" for List Xref and Find:");
Write_Eol;
Write_Str (" -q Be quiet: do not output tool command");
Write_Eol;
Write_Str (" -Pproj Use GNAT Project File proj");
Write_Eol;
Write_Str (" -vPx Specify verbosity when parsing " &
"GNAT Project Files");
Write_Eol;
Write_Str (" -Xnm=val Specify an external reference for " &
"GNAT Project Files");
Write_Eol;
Write_Eol;
Write_Str ("all other arguments are transmited to the tool");
Write_Eol;
Write_Eol;
end Usage;
begin
Osint.Initialize (Unspecified);
Namet.Initialize;
Csets.Initialize;
Snames.Initialize;
Prj.Initialize;
if Arg_Count = 1 then
Usage;
return;
end if;
-- Get the name of the tool
declare
Tool_Name : String (1 .. Len_Arg (1));
begin
Fill_Arg (Tool_Name'Address, 1);
GNAT.Case_Util.To_Lower (Tool_Name);
if Tool_Name = "list" then
Tool := List;
elsif Tool_Name = "xref" then
Tool := Xref;
elsif Tool_Name = "find" then
Tool := Find;
elsif Tool_Name = "stub" then
Tool := Stub;
elsif Tool_Name = "comp" then
Tool := Comp;
elsif Tool_Name = "make" then
Tool := Make;
elsif Tool_Name = "bind" then
Tool := Bind;
elsif Tool_Name = "link" then
Tool := Link;
else
Fail ("first argument needs to be ""list"", ""xref"", ""find""" &
", ""stub"", ""comp"", ""make"", ""bind"" or ""link""");
end if;
end;
Next_Arg := 2;
-- Get the command line switches that follow the name of the tool
Scan_Args : while Next_Arg < Arg_Count loop
declare
Next_Argv : String (1 .. Len_Arg (Next_Arg));
begin
Fill_Arg (Next_Argv'Address, Next_Arg);
Scan_Arg (Next_Argv, And_Save => True);
end;
Next_Arg := Next_Arg + 1;
end loop Scan_Args;
-- If a switch -P was specified, parse the project file.
-- Project_File is always null if we are going to invoke gnatmake,
-- that is when Tool is Comp, Make, Bind or Link.
if Project_File /= null then
Prj.Pars.Set_Verbosity (To => Current_Verbosity);
Prj.Pars.Parse
(Project => Project,
Project_File_Name => Project_File.all);
if Project = Prj.No_Project then
Fail ("""" & Project_File.all & """ processing failed");
end if;
-- Check if a package with the name of the tool is in the project file
-- and if there is one, get the switches, if any, and scan them.
declare
Data : Prj.Project_Data := Prj.Projects.Table (Project);
Pkg : Prj.Package_Id :=
Prj.Util.Value_Of
(Name => Tool_Package_Names (Tool),
In_Packages => Data.Decl.Packages);
Element : Package_Element;
Default_Switches_Array : Array_Element_Id;
Switches : Prj.Variable_Value;
Current : Prj.String_List_Id;
The_String : String_Element;
begin
if Pkg /= No_Package then
Element := Packages.Table (Pkg);
-- Packages Gnatls and Gnatstub have a single attribute Switches,
-- that is not an associative array.
if Tool = List or else Tool = Stub then
Switches :=
Prj.Util.Value_Of
(Variable_Name => Name_Switches,
In_Variables => Element.Decl.Attributes);
-- Packages Cross_Reference (for gnatxref) and Finder
-- (for gnatfind) have an attributed Default_Switches,
-- an associative array, indexed by the name of the
-- programming language.
else
Default_Switches_Array :=
Prj.Util.Value_Of
(Name => Name_Default_Switches,
In_Arrays => Packages.Table (Pkg).Decl.Arrays);
Switches := Prj.Util.Value_Of
(Index => Name_Ada,
In_Array => Default_Switches_Array);
end if;
-- If there are switches specified in the package of the
-- project file corresponding to the tool, scan them.
case Switches.Kind is
when Prj.Undefined =>
null;
when Prj.Single =>
if String_Length (Switches.Value) > 0 then
String_To_Name_Buffer (Switches.Value);
Scan_Arg
(Name_Buffer (1 .. Name_Len),
And_Save => False);
end if;
when Prj.List =>
Current := Switches.Values;
while Current /= Prj.Nil_String loop
The_String := String_Elements.Table (Current);
if String_Length (The_String.Value) > 0 then
String_To_Name_Buffer (The_String.Value);
Scan_Arg
(Name_Buffer (1 .. Name_Len),
And_Save => False);
end if;
Current := The_String.Next;
end loop;
end case;
end if;
end;
-- Set up the environment variables ADA_INCLUDE_PATH and
-- ADA_OBJECTS_PATH.
Setenv
(Name => Ada_Include_Path,
Value => Prj.Env.Ada_Include_Path (Project).all);
Setenv
(Name => Ada_Objects_Path,
Value => Prj.Env.Ada_Objects_Path
(Project, Including_Libraries => False).all);
end if;
-- Gather all the arguments, those from the project file first,
-- locate the tool and call it with the arguments.
declare
Args : Argument_List (1 .. Switches.Last + Saved_Switches.Last + 4);
Arg_Num : Natural := 0;
Tool_Path : String_Access;
Success : Boolean;
procedure Add (Arg : String_Access);
procedure Add (Arg : String_Access) is
begin
Arg_Num := Arg_Num + 1;
Args (Arg_Num) := Arg;
end Add;
begin
case Tool is
when Comp =>
Add (new String'("-u"));
Add (new String'("-f"));
when Bind =>
Add (new String'("-b"));
when Link =>
Add (new String'("-l"));
when others =>
null;
end case;
for Index in 1 .. Switches.Last loop
Arg_Num := Arg_Num + 1;
Args (Arg_Num) := Switches.Table (Index);
end loop;
for Index in 1 .. Saved_Switches.Last loop
Arg_Num := Arg_Num + 1;
Args (Arg_Num) := Saved_Switches.Table (Index);
end loop;
Tool_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Tool_Names (Tool).all);
if Tool_Path = null then
Fail ("error, unable to locate " & Tool_Names (Tool).all);
end if;
Display (Tool_Names (Tool).all, Args (1 .. Arg_Num));
GNAT.OS_Lib.Spawn (Tool_Path.all, Args (1 .. Arg_Num), Success);
end;
end Gnatmain;
|