Debian Codegraph @

gcc/ada/gnatcmd.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T C M D --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1996-2025, 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 Gnatvsn;
27 with Namet; use Namet;
28 with Opt; use Opt;
29 with Osint; use Osint;
30 with Output; use Output;
31 with Switch; use Switch;
32 with Table;
33 with Usage;
34
35 with Ada.Characters.Handling; use Ada.Characters.Handling;
36 with Ada.Command_Line; use Ada.Command_Line;
37 with Ada.Text_IO; use Ada.Text_IO;
38
39 with GNAT.OS_Lib; use GNAT.OS_Lib;
40
41 procedure GNATCmd is
42 Gprbuild : constant String := "gprbuild";
43 Gprclean : constant String := "gprclean";
44 Gprname : constant String := "gprname";
45 Gprls : constant String := "gprls";
46
47 Ada_Help_Switch : constant String := "--help-ada";
48 -- Flag to display available build switches
49
50 Error_Exit : exception;
51 -- Raise this exception if error detected
52
53 type Command_Type is
54 (Bind,
55 Chop,
56 Clean,
57 Compile,
58 Check,
59 Elim,
60 Krunch,
61 Link,
62 List,
63 Make,
64 Metric,
65 Name,
66 Preprocess,
67 Pretty,
68 Stack,
69 Stub,
70 Test,
71 Undefined);
72
73 subtype Real_Command_Type is Command_Type range Bind .. Test;
74 -- All real command types (excludes only Undefined).
75
76 type Alternate_Command is (Comp, Ls, Kr, Pp, Prep);
77 -- Alternate command label
78
79 Corresponding_To : constant array (Alternate_Command) of Command_Type :=
80 (Comp => Compile,
81 Ls => List,
82 Kr => Krunch,
83 Prep => Preprocess,
84 Pp => Pretty);
85 -- Mapping of alternate commands to commands
86
87 package First_Switches is new Table.Table
88 (Table_Component_Type => String_Access,
89 Table_Index_Type => Integer,
90 Table_Low_Bound => 1,
91 Table_Initial => 20,
92 Table_Increment => 100,
93 Table_Name => "Gnatcmd.First_Switches");
94 -- A table to keep the switches from the project file
95
96 package Last_Switches is new Table.Table
97 (Table_Component_Type => String_Access,
98 Table_Index_Type => Integer,
99 Table_Low_Bound => 1,
100 Table_Initial => 20,
101 Table_Increment => 100,
102 Table_Name => "Gnatcmd.Last_Switches");
103
104 ----------------------------------
105 -- Declarations for GNATCMD use --
106 ----------------------------------
107
108 The_Command : Command_Type;
109 -- The command specified in the invocation of the GNAT driver
110
111 Command_Arg : Positive := 1;
112 -- The index of the command in the arguments of the GNAT driver
113
114 My_Exit_Status : Exit_Status := Success;
115 -- The exit status of the spawned tool
116
117 type Command_Entry is record
118 Cname : String_Access;
119 -- Command name for GNAT xxx command
120
121 Unixcmd : String_Access;
122 -- Corresponding Unix command
123
124 Unixsws : Argument_List_Access;
125 -- List of switches to be used with the Unix command
126 end record;
127
128 Command_List : constant array (Real_Command_Type) of Command_Entry :=
129 (Bind =>
130 (Cname => new String'("BIND"),
131 Unixcmd => new String'("gnatbind"),
132 Unixsws => null),
133
134 Chop =>
135 (Cname => new String'("CHOP"),
136 Unixcmd => new String'("gnatchop"),
137 Unixsws => null),
138
139 Clean =>
140 (Cname => new String'("CLEAN"),
141 Unixcmd => new String'("gnatclean"),
142 Unixsws => null),
143
144 Compile =>
145 (Cname => new String'("COMPILE"),
146 Unixcmd => new String'("gnatmake"),
147 Unixsws => new Argument_List'(1 => new String'("-f"),
148 2 => new String'("-u"),
149 3 => new String'("-c"))),
150
151 Check =>
152 (Cname => new String'("CHECK"),
153 Unixcmd => new String'("gnatcheck"),
154 Unixsws => null),
155
156 Elim =>
157 (Cname => new String'("ELIM"),
158 Unixcmd => new String'("gnatelim"),
159 Unixsws => null),
160
161 Krunch =>
162 (Cname => new String'("KRUNCH"),
163 Unixcmd => new String'("gnatkr"),
164 Unixsws => null),
165
166 Link =>
167 (Cname => new String'("LINK"),
168 Unixcmd => new String'("gnatlink"),
169 Unixsws => null),
170
171 List =>
172 (Cname => new String'("LIST"),
173 Unixcmd => new String'("gnatls"),
174 Unixsws => null),
175
176 Make =>
177 (Cname => new String'("MAKE"),
178 Unixcmd => new String'("gnatmake"),
179 Unixsws => null),
180
181 Metric =>
182 (Cname => new String'("METRIC"),
183 Unixcmd => new String'("gnatmetric"),
184 Unixsws => null),
185
186 Name =>
187 (Cname => new String'("NAME"),
188 Unixcmd => new String'("gnatname"),
189 Unixsws => null),
190
191 Preprocess =>
192 (Cname => new String'("PREPROCESS"),
193 Unixcmd => new String'("gnatprep"),
194 Unixsws => null),
195
196 Pretty =>
197 (Cname => new String'("PRETTY"),
198 Unixcmd => new String'("gnatpp"),
199 Unixsws => null),
200
201 Stack =>
202 (Cname => new String'("STACK"),
203 Unixcmd => new String'("gnatstack"),
204 Unixsws => null),
205
206 Stub =>
207 (Cname => new String'("STUB"),
208 Unixcmd => new String'("gnatstub"),
209 Unixsws => null),
210
211 Test =>
212 (Cname => new String'("TEST"),
213 Unixcmd => new String'("gnattest"),
214 Unixsws => null)
215 );
216
217 -----------------------
218 -- Local Subprograms --
219 -----------------------
220
221 procedure Output_Version;
222 -- Output the version of this program
223
224 procedure GNATCmd_Usage;
225 -- Display usage
226
227 --------------------
228 -- Output_Version --
229 --------------------
230
231 procedure Output_Version is
232 begin
233 Put ("GNAT ");
234 Put_Line (Gnatvsn.Gnat_Version_String);
235 Put_Line ("Copyright 1996-" & Gnatvsn.Current_Year
236 & ", Free Software Foundation, Inc.");
237 end Output_Version;
238
239 -------------------
240 -- GNATCmd_Usage --
241 -------------------
242
243 procedure GNATCmd_Usage is
244 begin
245 Output_Version;
246 New_Line;
247 Put_Line ("To list Ada build switches use " & Ada_Help_Switch);
248 New_Line;
249 Put_Line ("List of available commands");
250 New_Line;
251
252 for C in Command_List'Range loop
253 Put ("gnat ");
254 Put (To_Lower (Command_List (C).Cname.all));
255 Set_Col (25);
256 Put (Program_Name (Command_List (C).Unixcmd.all, "gnat").all);
257
258 declare
259 Sws : Argument_List_Access renames Command_List (C).Unixsws;
260 begin
261 if Sws /= null then
262 for J in Sws'Range loop
263 Put (' ');
264 Put (Sws (J).all);
265 end loop;
266 end if;
267 end;
268
269 New_Line;
270 end loop;
271
272 New_Line;
273 end GNATCmd_Usage;
274
275 procedure Check_Version_And_Help
276 is new Check_Version_And_Help_G (GNATCmd_Usage);
277
278 -- Start of processing for GNATCmd
279
280 begin
281 -- Almost all output from GNATCmd is debugging or error output: send to
282 -- stderr.
283
284 Set_Standard_Error;
285
286 -- Initializations
287
288 Last_Switches.Init;
289 Last_Switches.Set_Last (0);
290
291 First_Switches.Init;
292 First_Switches.Set_Last (0);
293
294 -- Put the command line in environment variable GNAT_DRIVER_COMMAND_LINE,
295 -- so that the spawned tool may know the way the GNAT driver was invoked.
296
297 Name_Len := 0;
298 Add_Str_To_Name_Buffer (Command_Name);
299
300 for J in 1 .. Argument_Count loop
301 Add_Char_To_Name_Buffer (' ');
302 Add_Str_To_Name_Buffer (Argument (J));
303 end loop;
304
305 Setenv ("GNAT_DRIVER_COMMAND_LINE", Name_Buffer (1 .. Name_Len));
306
307 -- Add the directory where the GNAT driver is invoked in front of the path,
308 -- if the GNAT driver is invoked with directory information.
309
310 declare
311 Command : constant String := Command_Name;
312
313 begin
314 for Index in reverse Command'Range loop
315 if Command (Index) = Directory_Separator then
316 declare
317 Absolute_Dir : constant String :=
318 Normalize_Pathname (Command (Command'First .. Index));
319 PATH : constant String :=
320 Absolute_Dir & Path_Separator & Getenv ("PATH").all;
321 begin
322 Setenv ("PATH", PATH);
323 end;
324
325 exit;
326 end if;
327 end loop;
328 end;
329
330 -- Scan the command line
331
332 -- First, scan to detect --version and/or --help
333
334 Check_Version_And_Help ("GNAT", "1996");
335
336 begin
337 loop
338 if Command_Arg <= Argument_Count
339 and then Argument (Command_Arg) = "-v"
340 then
341 Verbose_Mode := True;
342 Command_Arg := Command_Arg + 1;
343
344 elsif Command_Arg <= Argument_Count
345 and then Argument (Command_Arg) = "-dn"
346 then
347 Keep_Temporary_Files := True;
348 Command_Arg := Command_Arg + 1;
349
350 elsif Command_Arg <= Argument_Count
351 and then Argument (Command_Arg) = Ada_Help_Switch
352 then
353 Set_Standard_Output;
354 Usage;
355 Exit_Program (E_Success);
356
357 else
358 exit;
359 end if;
360 end loop;
361
362 -- If there is no command, just output the usage
363
364 if Command_Arg > Argument_Count then
365 GNATCmd_Usage;
366
367 -- Add the following so that output is consistent with or without the
368 -- --help flag.
369 Set_Standard_Output;
370 Write_Eol;
371 Write_Line ("Report bugs to [email protected]");
372 return;
373 end if;
374
375 The_Command := Real_Command_Type'Value (Argument (Command_Arg));
376
377 exception
378 when Constraint_Error =>
379
380 -- Check if it is an alternate command
381
382 declare
383 Alternate : Alternate_Command;
384
385 begin
386 Alternate := Alternate_Command'Value (Argument (Command_Arg));
387 The_Command := Corresponding_To (Alternate);
388
389 exception
390 when Constraint_Error =>
391 GNATCmd_Usage;
392 Fail ("unknown command: " & Argument (Command_Arg));
393 end;
394 end;
395
396 -- Get the arguments from the command line and from the eventual
397 -- argument file(s) specified on the command line.
398
399 for Arg in Command_Arg + 1 .. Argument_Count loop
400 declare
401 The_Arg : constant String := Argument (Arg);
402
403 begin
404 -- Check if an argument file is specified
405
406 if The_Arg'Length > 0 and then The_Arg (The_Arg'First) = '@' then
407 declare
408 Arg_File : Ada.Text_IO.File_Type;
409 Line : String (1 .. 256);
410 Last : Natural;
411
412 begin
413 -- Open the file and fail if the file cannot be found
414
415 begin
416 Open (Arg_File, In_File,
417 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
418
419 exception
420 when others =>
421 Put (Standard_Error, "Cannot open argument file """);
422 Put (Standard_Error,
423 The_Arg (The_Arg'First + 1 .. The_Arg'Last));
424 Put_Line (Standard_Error, """");
425 raise Error_Exit;
426 end;
427
428 -- Read line by line and put the content of each non-
429 -- empty line in the Last_Switches table.
430
431 while not End_Of_File (Arg_File) loop
432 Get_Line (Arg_File, Line, Last);
433
434 if Last /= 0 then
435 Last_Switches.Increment_Last;
436 Last_Switches.Table (Last_Switches.Last) :=
437 new String'(Line (1 .. Last));
438 end if;
439 end loop;
440
441 Close (Arg_File);
442 end;
443
444 elsif The_Arg'Length > 0 then
445 -- It is not an argument file; just put the argument in
446 -- the Last_Switches table.
447
448 Last_Switches.Increment_Last;
449 Last_Switches.Table (Last_Switches.Last) := new String'(The_Arg);
450 end if;
451 end;
452 end loop;
453
454 declare
455 Program : String_Access;
456 Exec_Path : String_Access;
457 Get_Target : Boolean := False;
458
459 begin
460 if The_Command = Stack then
461
462 -- Never call gnatstack with a prefix
463
464 Program := new String'(Command_List (The_Command).Unixcmd.all);
465
466 elsif The_Command in Check | Test then
467 Program := new String'(Command_List (The_Command).Unixcmd.all);
468 Find_Program_Name;
469
470 if Name_Len > 5 then
471 First_Switches.Append
472 (new String'
473 ("--target=" & Name_Buffer (1 .. Name_Len - 5)));
474 end if;
475 else
476 Program :=
477 Program_Name (Command_List (The_Command).Unixcmd.all, "gnat");
478
479 -- If we want to invoke gnatmake/gnatclean with -P, then check if
480 -- gprbuild/gprclean is available; if it is, use gprbuild/gprclean
481 -- instead of gnatmake/gnatclean.
482 -- Ditto for gnatname -> gprname and gnatls -> gprls.
483
484 if The_Command in Make | Compile | Bind | Link | Clean | Name | List
485 then
486 declare
487 Switch : String_Access;
488 Call_GPR_Tool : Boolean := False;
489 begin
490 for J in 1 .. Last_Switches.Last loop
491 Switch := Last_Switches.Table (J);
492
493 if Switch'Length >= 2
494 and then Switch (Switch'First .. Switch'First + 1) = "-P"
495 then
496 Call_GPR_Tool := True;
497 exit;
498 end if;
499 end loop;
500
501 if Call_GPR_Tool then
502 case The_Command is
503 when Bind
504 | Compile
505 | Link
506 | Make
507 =>
508 if Locate_Exec_On_Path (Gprbuild) /= null then
509 Program := new String'(Gprbuild);
510 Get_Target := True;
511
512 if The_Command = Bind then
513 First_Switches.Append (new String'("-b"));
514 elsif The_Command = Link then
515 First_Switches.Append (new String'("-l"));
516 end if;
517
518 elsif The_Command = Bind then
519 Fail
520 ("'gnat bind -P' is no longer supported;" &
521 " use 'gprbuild -b' instead.");
522
523 elsif The_Command = Link then
524 Fail
525 ("'gnat Link -P' is no longer supported;" &
526 " use 'gprbuild -l' instead.");
527 end if;
528
529 when Clean =>
530 if Locate_Exec_On_Path (Gprclean) /= null then
531 Program := new String'(Gprclean);
532 Get_Target := True;
533 end if;
534
535 when Name =>
536 if Locate_Exec_On_Path (Gprname) /= null then
537 Program := new String'(Gprname);
538 Get_Target := True;
539 end if;
540
541 when List =>
542 if Locate_Exec_On_Path (Gprls) /= null then
543 Program := new String'(Gprls);
544 Get_Target := True;
545 end if;
546
547 when others =>
548 null;
549 end case;
550
551 if Get_Target then
552 Find_Program_Name;
553
554 if Name_Len > 5 then
555 First_Switches.Append
556 (new String'
557 ("--target=" & Name_Buffer (1 .. Name_Len - 5)));
558 end if;
559 end if;
560 end if;
561 end;
562 end if;
563 end if;
564
565 -- Locate the executable for the command
566
567 Exec_Path := Locate_Exec_On_Path (Program.all);
568
569 if Exec_Path = null then
570 Put_Line (Standard_Error, "could not locate " & Program.all);
571 raise Error_Exit;
572 end if;
573
574 -- If there are switches for the executable, put them as first switches
575
576 if Command_List (The_Command).Unixsws /= null then
577 for J in Command_List (The_Command).Unixsws'Range loop
578 First_Switches.Increment_Last;
579 First_Switches.Table (First_Switches.Last) :=
580 Command_List (The_Command).Unixsws (J);
581 end loop;
582 end if;
583
584 -- Gather all the arguments and invoke the executable
585
586 declare
587 The_Args : Argument_List
588 (1 .. First_Switches.Last + Last_Switches.Last);
589 Arg_Num : Natural := 0;
590
591 begin
592 for J in 1 .. First_Switches.Last loop
593 Arg_Num := Arg_Num + 1;
594 The_Args (Arg_Num) := First_Switches.Table (J);
595 end loop;
596
597 for J in 1 .. Last_Switches.Last loop
598 Arg_Num := Arg_Num + 1;
599 The_Args (Arg_Num) := Last_Switches.Table (J);
600 end loop;
601
602 if Verbose_Mode then
603 Put (Exec_Path.all);
604
605 for Arg in The_Args'Range loop
606 Put (" " & The_Args (Arg).all);
607 end loop;
608
609 New_Line;
610 end if;
611
612 My_Exit_Status := Exit_Status (Spawn (Exec_Path.all, The_Args));
613
614 Set_Exit_Status (My_Exit_Status);
615 end;
616 end;
617
618 exception
619 when Error_Exit =>
620 Set_Exit_Status (Failure);
621 end GNATCmd;