2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ada / make.adb
blob3ae13fc84aeb4fd31a48a93e67480fbc9a171b6f
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- M A K E --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2008, 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 ------------------------------------------------------------------------------
26 with ALI; use ALI;
27 with ALI.Util; use ALI.Util;
28 with Csets;
29 with Debug;
30 with Fmap;
31 with Fname; use Fname;
32 with Fname.SF; use Fname.SF;
33 with Fname.UF; use Fname.UF;
34 with Gnatvsn; use Gnatvsn;
35 with Hostparm; use Hostparm;
36 with Makeusg;
37 with Makeutl; use Makeutl;
38 with MLib.Prj;
39 with MLib.Tgt; use MLib.Tgt;
40 with MLib.Utl;
41 with Namet; use Namet;
42 with Opt; use Opt;
43 with Osint.M; use Osint.M;
44 with Osint; use Osint;
45 with Output; use Output;
46 with Prj; use Prj;
47 with Prj.Com;
48 with Prj.Env;
49 with Prj.Pars;
50 with Prj.Util;
51 with SFN_Scan;
52 with Sinput.P;
53 with Snames; use Snames;
54 with Switch; use Switch;
55 with Switch.M; use Switch.M;
56 with Targparm; use Targparm;
57 with Table;
58 with Tempdir;
59 with Types; use Types;
61 with Ada.Exceptions; use Ada.Exceptions;
62 with Ada.Command_Line; use Ada.Command_Line;
64 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
65 with GNAT.Case_Util; use GNAT.Case_Util;
66 with GNAT.OS_Lib; use GNAT.OS_Lib;
68 with System.HTable;
70 package body Make is
72 use ASCII;
73 -- Make control characters visible
75 Standard_Library_Package_Body_Name : constant String := "s-stalib.adb";
76 -- Every program depends on this package, that must then be checked,
77 -- especially when -f and -a are used.
79 procedure Kill (Pid : Process_Id; Sig_Num : Integer; Close : Integer);
80 pragma Import (C, Kill, "__gnat_kill");
81 -- Called by Sigint_Intercepted to kill all spawned compilation processes
83 type Sigint_Handler is access procedure;
84 pragma Convention (C, Sigint_Handler);
86 procedure Install_Int_Handler (Handler : Sigint_Handler);
87 pragma Import (C, Install_Int_Handler, "__gnat_install_int_handler");
88 -- Called by Gnatmake to install the SIGINT handler below
90 procedure Sigint_Intercepted;
91 pragma Convention (C, Sigint_Intercepted);
92 -- Called when the program is interrupted by Ctrl-C to delete the
93 -- temporary mapping files and configuration pragmas files.
95 No_Mapping_File : constant Natural := 0;
97 type Compilation_Data is record
98 Pid : Process_Id;
99 Full_Source_File : File_Name_Type;
100 Lib_File : File_Name_Type;
101 Source_Unit : Unit_Name_Type;
102 Mapping_File : Natural := No_Mapping_File;
103 Project : Project_Id := No_Project;
104 Syntax_Only : Boolean := False;
105 Output_Is_Object : Boolean := True;
106 end record;
107 -- Data recorded for each compilation process spawned
109 type Comp_Data_Arr is array (Positive range <>) of Compilation_Data;
110 type Comp_Data_Ptr is access Comp_Data_Arr;
111 Running_Compile : Comp_Data_Ptr;
112 -- Used to save information about outstanding compilations
114 Outstanding_Compiles : Natural := 0;
115 -- Current number of outstanding compiles
117 -------------------------
118 -- Note on terminology --
119 -------------------------
121 -- In this program, we use the phrase "termination" of a file name to refer
122 -- to the suffix that appears after the unit name portion. Very often this
123 -- is simply the extension, but in some cases, the sequence may be more
124 -- complex, for example in main.1.ada, the termination in this name is
125 -- ".1.ada" and in main_.ada the termination is "_.ada".
127 -------------------------------------
128 -- Queue (Q) Manipulation Routines --
129 -------------------------------------
131 -- The Q is used in Compile_Sources below. Its implementation uses the GNAT
132 -- generic package Table (basically an extensible array). Q_Front points to
133 -- the first valid element in the Q, whereas Q.First is the first element
134 -- ever enqueued, while Q.Last - 1 is the last element in the Q.
136 -- +---+--------------+---+---+---+-----------+---+--------
137 -- Q | | ........ | | | | ....... | |
138 -- +---+--------------+---+---+---+-----------+---+--------
139 -- ^ ^ ^
140 -- Q.First Q_Front Q.Last - 1
142 -- The elements comprised between Q.First and Q_Front - 1 are the elements
143 -- that have been enqueued and then dequeued, while the elements between
144 -- Q_Front and Q.Last - 1 are the elements currently in the Q. When the Q
145 -- is initialized Q_Front = Q.First = Q.Last. After Compile_Sources has
146 -- terminated its execution, Q_Front = Q.Last and the elements contained
147 -- between Q.Front and Q.Last-1 are those that were explored and thus
148 -- marked by Compile_Sources. Whenever the Q is reinitialized, the elements
149 -- between Q.First and Q.Last - 1 are unmarked.
151 procedure Init_Q;
152 -- Must be called to (re)initialize the Q
154 procedure Insert_Q
155 (Source_File : File_Name_Type;
156 Source_Unit : Unit_Name_Type := No_Unit_Name;
157 Index : Int := 0);
158 -- Inserts Source_File at the end of Q. Provide Source_Unit when possible
159 -- for external use (gnatdist). Provide index for multi-unit sources.
161 function Empty_Q return Boolean;
162 -- Returns True if Q is empty
164 procedure Extract_From_Q
165 (Source_File : out File_Name_Type;
166 Source_Unit : out Unit_Name_Type;
167 Source_Index : out Int);
168 -- Extracts the first element from the Q
170 procedure Insert_Project_Sources
171 (The_Project : Project_Id;
172 All_Projects : Boolean;
173 Into_Q : Boolean);
174 -- If Into_Q is True, insert all sources of the project file(s) that are
175 -- not already marked into the Q. If Into_Q is False, call Osint.Add_File
176 -- for the first source, then insert all other sources that are not already
177 -- marked into the Q. If All_Projects is True, all sources of all projects
178 -- are concerned; otherwise, only sources of The_Project are concerned,
179 -- including, if The_Project is an extending project, sources inherited
180 -- from projects being extended.
182 First_Q_Initialization : Boolean := True;
183 -- Will be set to false after Init_Q has been called once
185 Q_Front : Natural;
186 -- Points to the first valid element in the Q
188 Unique_Compile : Boolean := False;
189 -- Set to True if -u or -U or a project file with no main is used
191 Unique_Compile_All_Projects : Boolean := False;
192 -- Set to True if -U is used
194 RTS_Specified : String_Access := null;
195 -- Used to detect multiple --RTS= switches
197 type Q_Record is record
198 File : File_Name_Type;
199 Unit : Unit_Name_Type;
200 Index : Int;
201 end record;
202 -- File is the name of the file to compile. Unit is for gnatdist
203 -- use in order to easily get the unit name of a file to compile
204 -- when its name is krunched or declared in gnat.adc. Index, when not 0,
205 -- is the index of the unit in a multi-unit source.
207 package Q is new Table.Table (
208 Table_Component_Type => Q_Record,
209 Table_Index_Type => Natural,
210 Table_Low_Bound => 0,
211 Table_Initial => 4000,
212 Table_Increment => 100,
213 Table_Name => "Make.Q");
214 -- This is the actual Q
216 -- The 3 following packages are used to store gcc, gnatbind and gnatlink
217 -- switches found in the project files.
219 package Gcc_Switches is new Table.Table (
220 Table_Component_Type => String_Access,
221 Table_Index_Type => Integer,
222 Table_Low_Bound => 1,
223 Table_Initial => 20,
224 Table_Increment => 100,
225 Table_Name => "Make.Gcc_Switches");
227 package Binder_Switches is new Table.Table (
228 Table_Component_Type => String_Access,
229 Table_Index_Type => Integer,
230 Table_Low_Bound => 1,
231 Table_Initial => 20,
232 Table_Increment => 100,
233 Table_Name => "Make.Binder_Switches");
235 package Linker_Switches is new Table.Table (
236 Table_Component_Type => String_Access,
237 Table_Index_Type => Integer,
238 Table_Low_Bound => 1,
239 Table_Initial => 20,
240 Table_Increment => 100,
241 Table_Name => "Make.Linker_Switches");
243 -- The following instantiations and variables are necessary to save what
244 -- is found on the command line, in case there is a project file specified.
246 package Saved_Gcc_Switches is new Table.Table (
247 Table_Component_Type => String_Access,
248 Table_Index_Type => Integer,
249 Table_Low_Bound => 1,
250 Table_Initial => 20,
251 Table_Increment => 100,
252 Table_Name => "Make.Saved_Gcc_Switches");
254 package Saved_Binder_Switches is new Table.Table (
255 Table_Component_Type => String_Access,
256 Table_Index_Type => Integer,
257 Table_Low_Bound => 1,
258 Table_Initial => 20,
259 Table_Increment => 100,
260 Table_Name => "Make.Saved_Binder_Switches");
262 package Saved_Linker_Switches is new Table.Table
263 (Table_Component_Type => String_Access,
264 Table_Index_Type => Integer,
265 Table_Low_Bound => 1,
266 Table_Initial => 20,
267 Table_Increment => 100,
268 Table_Name => "Make.Saved_Linker_Switches");
270 package Switches_To_Check is new Table.Table (
271 Table_Component_Type => String_Access,
272 Table_Index_Type => Integer,
273 Table_Low_Bound => 1,
274 Table_Initial => 20,
275 Table_Increment => 100,
276 Table_Name => "Make.Switches_To_Check");
278 package Library_Paths is new Table.Table (
279 Table_Component_Type => String_Access,
280 Table_Index_Type => Integer,
281 Table_Low_Bound => 1,
282 Table_Initial => 20,
283 Table_Increment => 100,
284 Table_Name => "Make.Library_Paths");
286 package Failed_Links is new Table.Table (
287 Table_Component_Type => File_Name_Type,
288 Table_Index_Type => Integer,
289 Table_Low_Bound => 1,
290 Table_Initial => 10,
291 Table_Increment => 100,
292 Table_Name => "Make.Failed_Links");
294 package Successful_Links is new Table.Table (
295 Table_Component_Type => File_Name_Type,
296 Table_Index_Type => Integer,
297 Table_Low_Bound => 1,
298 Table_Initial => 10,
299 Table_Increment => 100,
300 Table_Name => "Make.Successful_Links");
302 package Library_Projs is new Table.Table (
303 Table_Component_Type => Project_Id,
304 Table_Index_Type => Integer,
305 Table_Low_Bound => 1,
306 Table_Initial => 10,
307 Table_Increment => 100,
308 Table_Name => "Make.Library_Projs");
310 -- Two variables to keep the last binder and linker switch index in tables
311 -- Binder_Switches and Linker_Switches, before adding switches from the
312 -- project file (if any) and switches from the command line (if any).
314 Last_Binder_Switch : Integer := 0;
315 Last_Linker_Switch : Integer := 0;
317 Normalized_Switches : Argument_List_Access := new Argument_List (1 .. 10);
318 Last_Norm_Switch : Natural := 0;
320 Saved_Maximum_Processes : Natural := 0;
322 type Arg_List_Ref is access Argument_List;
323 The_Saved_Gcc_Switches : Arg_List_Ref;
325 Project_File_Name : String_Access := null;
326 -- The path name of the main project file, if any
328 Project_File_Name_Present : Boolean := False;
329 -- True when -P is used with a space between -P and the project file name
331 Current_Verbosity : Prj.Verbosity := Prj.Default;
332 -- Verbosity to parse the project files
334 Project_Tree : constant Project_Tree_Ref := new Project_Tree_Data;
336 Main_Project : Prj.Project_Id := No_Project;
337 -- The project id of the main project file, if any
339 Project_Of_Current_Object_Directory : Project_Id := No_Project;
340 -- The object directory of the project for the last compilation. Avoid
341 -- calling Change_Dir if the current working directory is already this
342 -- directory
344 -- Packages of project files where unknown attributes are errors
346 Naming_String : aliased String := "naming";
347 Builder_String : aliased String := "builder";
348 Compiler_String : aliased String := "compiler";
349 Binder_String : aliased String := "binder";
350 Linker_String : aliased String := "linker";
352 Gnatmake_Packages : aliased String_List :=
353 (Naming_String 'Access,
354 Builder_String 'Access,
355 Compiler_String 'Access,
356 Binder_String 'Access,
357 Linker_String 'Access);
359 Packages_To_Check_By_Gnatmake : constant String_List_Access :=
360 Gnatmake_Packages'Access;
362 procedure Add_Library_Search_Dir
363 (Path : String;
364 On_Command_Line : Boolean);
365 -- Call Add_Lib_Search_Dir with an absolute directory path. If Path is
366 -- relative path, when On_Command_Line is True, it is relative to the
367 -- current working directory. When On_Command_Line is False, it is relative
368 -- to the project directory of the main project.
370 procedure Add_Source_Search_Dir
371 (Path : String;
372 On_Command_Line : Boolean);
373 -- Call Add_Src_Search_Dir with an absolute directory path. If Path is a
374 -- relative path, when On_Command_Line is True, it is relative to the
375 -- current working directory. When On_Command_Line is False, it is relative
376 -- to the project directory of the main project.
378 procedure Add_Source_Dir (N : String);
379 -- Call Add_Src_Search_Dir (output one line when in verbose mode)
381 procedure Add_Source_Directories is
382 new Prj.Env.For_All_Source_Dirs (Action => Add_Source_Dir);
384 procedure Add_Object_Dir (N : String);
385 -- Call Add_Lib_Search_Dir (output one line when in verbose mode)
387 procedure Add_Object_Directories is
388 new Prj.Env.For_All_Object_Dirs (Action => Add_Object_Dir);
390 procedure Change_To_Object_Directory (Project : Project_Id);
391 -- Change to the object directory of project Project, if this is not
392 -- already the current working directory.
394 type Bad_Compilation_Info is record
395 File : File_Name_Type;
396 Unit : Unit_Name_Type;
397 Found : Boolean;
398 end record;
399 -- File is the name of the file for which a compilation failed. Unit is for
400 -- gnatdist use in order to easily get the unit name of a file when its
401 -- name is krunched or declared in gnat.adc. Found is False if the
402 -- compilation failed because the file could not be found.
404 package Bad_Compilation is new Table.Table (
405 Table_Component_Type => Bad_Compilation_Info,
406 Table_Index_Type => Natural,
407 Table_Low_Bound => 1,
408 Table_Initial => 20,
409 Table_Increment => 100,
410 Table_Name => "Make.Bad_Compilation");
411 -- Full name of all the source files for which compilation fails
413 Do_Compile_Step : Boolean := True;
414 Do_Bind_Step : Boolean := True;
415 Do_Link_Step : Boolean := True;
416 -- Flags to indicate what step should be executed. Can be set to False
417 -- with the switches -c, -b and -l. These flags are reset to True for
418 -- each invocation of procedure Gnatmake.
420 Shared_String : aliased String := "-shared";
421 Force_Elab_Flags_String : aliased String := "-F";
423 No_Shared_Switch : aliased Argument_List := (1 .. 0 => null);
424 Shared_Switch : aliased Argument_List := (1 => Shared_String'Access);
425 Bind_Shared : Argument_List_Access := No_Shared_Switch'Access;
426 -- Switch to added in front of gnatbind switches. By default no switch is
427 -- added. Switch "-shared" is added if there is a non-static Library
428 -- Project File.
430 Shared_Libgcc : aliased String := "-shared-libgcc";
432 No_Shared_Libgcc_Switch : aliased Argument_List := (1 .. 0 => null);
433 Shared_Libgcc_Switch : aliased Argument_List :=
434 (1 => Shared_Libgcc'Access);
435 Link_With_Shared_Libgcc : Argument_List_Access :=
436 No_Shared_Libgcc_Switch'Access;
438 procedure Make_Failed (S1 : String; S2 : String := ""; S3 : String := "");
439 -- Delete all temp files created by Gnatmake and call Osint.Fail,
440 -- with the parameter S1, S2 and S3 (see osint.ads).
441 -- This is called from the Prj hierarchy and the MLib hierarchy.
443 --------------------------
444 -- Obsolete Executables --
445 --------------------------
447 Executable_Obsolete : Boolean := False;
448 -- Executable_Obsolete is initially set to False for each executable,
449 -- and is set to True whenever one of the source of the executable is
450 -- compiled, or has already been compiled for another executable.
452 Max_Header : constant := 200;
453 -- This needs a proper comment, it used to say "arbitrary"
454 -- that's not an adequate comment ???
456 type Header_Num is range 1 .. Max_Header;
457 -- Header_Num for the hash table Obsoleted below
459 function Hash (F : File_Name_Type) return Header_Num;
460 -- Hash function for the hash table Obsoleted below
462 package Obsoleted is new System.HTable.Simple_HTable
463 (Header_Num => Header_Num,
464 Element => Boolean,
465 No_Element => False,
466 Key => File_Name_Type,
467 Hash => Hash,
468 Equal => "=");
469 -- A hash table to keep all files that have been compiled, to detect
470 -- if an executable is up to date or not.
472 procedure Enter_Into_Obsoleted (F : File_Name_Type);
473 -- Enter a file name, without directory information, into the hash table
474 -- Obsoleted.
476 function Is_In_Obsoleted (F : File_Name_Type) return Boolean;
477 -- Check if a file name, without directory information, has already been
478 -- entered into the hash table Obsoleted.
480 type Dependency is record
481 This : File_Name_Type;
482 Depends_On : File_Name_Type;
483 end record;
484 -- Components of table Dependencies below
486 package Dependencies is new Table.Table (
487 Table_Component_Type => Dependency,
488 Table_Index_Type => Integer,
489 Table_Low_Bound => 1,
490 Table_Initial => 20,
491 Table_Increment => 100,
492 Table_Name => "Make.Dependencies");
493 -- A table to keep dependencies, to be able to decide if an executable
494 -- is obsolete. More explanation needed ???
496 -- procedure Add_Dependency (S : File_Name_Type; On : File_Name_Type);
497 -- -- Add one entry in table Dependencies
499 ----------------------------
500 -- Arguments and Switches --
501 ----------------------------
503 Arguments : Argument_List_Access;
504 -- Used to gather the arguments for invocation of the compiler
506 Last_Argument : Natural := 0;
507 -- Last index of arguments in Arguments above
509 Arguments_Collected : Boolean := False;
510 -- Set to True when the arguments for the next invocation of the compiler
511 -- have been collected.
513 Arguments_Project : Project_Id;
514 -- Project id, if any, of the source to be compiled
516 Arguments_Path_Name : Path_Name_Type;
517 -- Full path of the source to be compiled, when Arguments_Project is not
518 -- No_Project.
520 Dummy_Switch : constant String_Access := new String'("- ");
521 -- Used to initialized Prev_Switch in procedure Check
523 procedure Add_Arguments (Args : Argument_List);
524 -- Add arguments to global variable Arguments, increasing its size
525 -- if necessary and adjusting Last_Argument.
527 function Configuration_Pragmas_Switch
528 (For_Project : Project_Id) return Argument_List;
529 -- Return an argument list of one element, if there is a configuration
530 -- pragmas file to be specified for For_Project,
531 -- otherwise return an empty argument list.
533 -------------------
534 -- Misc Routines --
535 -------------------
537 procedure List_Depend;
538 -- Prints to standard output the list of object dependencies. This list
539 -- can be used directly in a Makefile. A call to Compile_Sources must
540 -- precede the call to List_Depend. Also because this routine uses the
541 -- ALI files that were originally loaded and scanned by Compile_Sources,
542 -- no additional ALI files should be scanned between the two calls (i.e.
543 -- between the call to Compile_Sources and List_Depend.)
545 procedure List_Bad_Compilations;
546 -- Prints out the list of all files for which the compilation failed
548 procedure Verbose_Msg
549 (N1 : Name_Id;
550 S1 : String;
551 N2 : Name_Id := No_Name;
552 S2 : String := "";
553 Prefix : String := " -> ";
554 Minimum_Verbosity : Verbosity_Level_Type := Opt.Low);
555 procedure Verbose_Msg
556 (N1 : File_Name_Type;
557 S1 : String;
558 N2 : File_Name_Type := No_File;
559 S2 : String := "";
560 Prefix : String := " -> ";
561 Minimum_Verbosity : Verbosity_Level_Type := Opt.Low);
562 -- If the verbose flag (Verbose_Mode) is set and the verbosity level is
563 -- at least equal to Minimum_Verbosity, then print Prefix to standard
564 -- output followed by N1 and S1. If N2 /= No_Name then N2 is printed after
565 -- S1. S2 is printed last. Both N1 and N2 are printed in quotation marks.
567 Usage_Needed : Boolean := True;
568 -- Flag used to make sure Makeusg is call at most once
570 procedure Usage;
571 -- Call Makeusg, if Usage_Needed is True.
572 -- Set Usage_Needed to False.
574 procedure Debug_Msg (S : String; N : Name_Id);
575 procedure Debug_Msg (S : String; N : File_Name_Type);
576 procedure Debug_Msg (S : String; N : Unit_Name_Type);
577 -- If Debug.Debug_Flag_W is set outputs string S followed by name N
579 procedure Recursive_Compute_Depth
580 (Project : Project_Id;
581 Depth : Natural);
582 -- Compute depth of Project and of the projects it depends on
584 procedure Compute_All_Imported_Projects (Project : Project_Id);
585 -- Compute, the list of the projects imported directly or indirectly by
586 -- project Project.
588 -----------------------
589 -- Gnatmake Routines --
590 -----------------------
592 subtype Lib_Mark_Type is Byte;
593 -- Used in Mark_Directory
595 Ada_Lib_Dir : constant Lib_Mark_Type := 1;
596 -- Used to mark a directory as a GNAT lib dir
598 -- Note that the notion of GNAT lib dir is no longer used. The code related
599 -- to it has not been removed to give an idea on how to use the directory
600 -- prefix marking mechanism.
602 -- An Ada library directory is a directory containing ali and object files
603 -- but no source files for the bodies (the specs can be in the same or some
604 -- other directory). These directories are specified in the Gnatmake
605 -- command line with the switch "-Adir" (to specify the spec location -Idir
606 -- cab be used). Gnatmake skips the missing sources whose ali are in Ada
607 -- library directories. For an explanation of why Gnatmake behaves that
608 -- way, see the spec of Make.Compile_Sources. The directory lookup penalty
609 -- is incurred every single time this routine is called.
611 procedure Check_Steps;
612 -- Check what steps (Compile, Bind, Link) must be executed.
613 -- Set the step flags accordingly.
615 function In_Ada_Lib_Dir (File : File_Name_Type) return Boolean;
616 -- Get directory prefix of this file and get lib mark stored in name
617 -- table for this directory. Then check if an Ada lib mark has been set.
619 procedure Mark_Directory
620 (Dir : String;
621 Mark : Lib_Mark_Type;
622 On_Command_Line : Boolean);
623 -- Store the absolute path from Dir in name table and set lib mark as name
624 -- info to identify Ada libraries.
626 -- If Dir is a relative path, when On_Command_Line is True, it is relative
627 -- to the current working directory; when On_Command_Line is False, it is
628 -- relative to the project directory of the main project.
630 Output_Is_Object : Boolean := True;
631 -- Set to False when using a switch -S for the compiler
633 procedure Check_For_S_Switch;
634 -- Set Output_Is_Object to False when the -S switch is used for the
635 -- compiler.
637 function Switches_Of
638 (Source_File : File_Name_Type;
639 Source_File_Name : String;
640 Source_Index : Int;
641 Naming : Naming_Data;
642 In_Package : Package_Id;
643 Allow_ALI : Boolean) return Variable_Value;
644 -- Return the switches for the source file in the specified package of a
645 -- project file. If the Source_File ends with a standard GNAT extension
646 -- (".ads" or ".adb"), try first the full name, then the name without the
647 -- extension, then, if Allow_ALI is True, the name with the extension
648 -- ".ali". If there is no switches for either names, try the default
649 -- switches for Ada. If all failed, return No_Variable_Value.
651 function Is_In_Object_Directory
652 (Source_File : File_Name_Type;
653 Full_Lib_File : File_Name_Type) return Boolean;
654 -- Check if, when using a project file, the ALI file is in the project
655 -- directory of the ultimate extending project. If it is not, we ignore
656 -- the fact that this ALI file is read-only.
658 ----------------------------------------------------
659 -- Compiler, Binder & Linker Data and Subprograms --
660 ----------------------------------------------------
662 Gcc : String_Access := Program_Name ("gcc", "gnatmake");
663 Gnatbind : String_Access := Program_Name ("gnatbind", "gnatmake");
664 Gnatlink : String_Access := Program_Name ("gnatlink", "gnatmake");
665 -- Default compiler, binder, linker programs
667 Saved_Gcc : String_Access := null;
668 Saved_Gnatbind : String_Access := null;
669 Saved_Gnatlink : String_Access := null;
670 -- Given by the command line. Will be used, if non null
672 Gcc_Path : String_Access :=
673 GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
674 Gnatbind_Path : String_Access :=
675 GNAT.OS_Lib.Locate_Exec_On_Path (Gnatbind.all);
676 Gnatlink_Path : String_Access :=
677 GNAT.OS_Lib.Locate_Exec_On_Path (Gnatlink.all);
678 -- Path for compiler, binder, linker programs, defaulted now for gnatdist.
679 -- Changed later if overridden on command line.
681 Comp_Flag : constant String_Access := new String'("-c");
682 Output_Flag : constant String_Access := new String'("-o");
683 Ada_Flag_1 : constant String_Access := new String'("-x");
684 Ada_Flag_2 : constant String_Access := new String'("ada");
685 No_gnat_adc : constant String_Access := new String'("-gnatA");
686 GNAT_Flag : constant String_Access := new String'("-gnatpg");
687 Do_Not_Check_Flag : constant String_Access := new String'("-x");
689 Object_Suffix : constant String := Get_Target_Object_Suffix.all;
691 Syntax_Only : Boolean := False;
692 -- Set to True when compiling with -gnats
694 Display_Executed_Programs : Boolean := True;
695 -- Set to True if name of commands should be output on stderr (or on stdout
696 -- if the Commands_To_Stdout flag was set by use of the -eS switch).
698 Output_File_Name_Seen : Boolean := False;
699 -- Set to True after having scanned the file_name for
700 -- switch "-o file_name"
702 Object_Directory_Seen : Boolean := False;
703 -- Set to True after having scanned the object directory for
704 -- switch "-D obj_dir".
706 Object_Directory_Path : String_Access := null;
707 -- The path name of the object directory, set with switch -D
709 type Make_Program_Type is (None, Compiler, Binder, Linker);
711 Program_Args : Make_Program_Type := None;
712 -- Used to indicate if we are scanning gnatmake, gcc, gnatbind, or gnatbind
713 -- options within the gnatmake command line. Used in Scan_Make_Arg only,
714 -- but must be global since value preserved from one call to another.
716 Temporary_Config_File : Boolean := False;
717 -- Set to True when there is a temporary config file used for a project
718 -- file, to avoid displaying the -gnatec switch for a temporary file.
720 procedure Add_Switches
721 (The_Package : Package_Id;
722 File_Name : String;
723 Index : Int;
724 Program : Make_Program_Type);
725 procedure Add_Switch
726 (S : String_Access;
727 Program : Make_Program_Type;
728 Append_Switch : Boolean := True;
729 And_Save : Boolean := True);
730 procedure Add_Switch
731 (S : String;
732 Program : Make_Program_Type;
733 Append_Switch : Boolean := True;
734 And_Save : Boolean := True);
735 -- Make invokes one of three programs (the compiler, the binder or the
736 -- linker). For the sake of convenience, some program specific switches
737 -- can be passed directly on the gnatmake command line. This procedure
738 -- records these switches so that gnatmake can pass them to the right
739 -- program. S is the switch to be added at the end of the command line
740 -- for Program if Append_Switch is True. If Append_Switch is False S is
741 -- added at the beginning of the command line.
743 procedure Check
744 (Source_File : File_Name_Type;
745 Source_Index : Int;
746 Is_Main_Source : Boolean;
747 The_Args : Argument_List;
748 Lib_File : File_Name_Type;
749 Read_Only : Boolean;
750 ALI : out ALI_Id;
751 O_File : out File_Name_Type;
752 O_Stamp : out Time_Stamp_Type);
753 -- Determines whether the library file Lib_File is up-to-date or not. The
754 -- full name (with path information) of the object file corresponding to
755 -- Lib_File is returned in O_File. Its time stamp is saved in O_Stamp.
756 -- ALI is the ALI_Id corresponding to Lib_File. If Lib_File in not
757 -- up-to-date, then the corresponding source file needs to be recompiled.
758 -- In this case ALI = No_ALI_Id.
760 procedure Check_Linker_Options
761 (E_Stamp : Time_Stamp_Type;
762 O_File : out File_Name_Type;
763 O_Stamp : out Time_Stamp_Type);
764 -- Checks all linker options for linker files that are newer
765 -- than E_Stamp. If such objects are found, the youngest object
766 -- is returned in O_File and its stamp in O_Stamp.
768 -- If no obsolete linker files were found, the first missing
769 -- linker file is returned in O_File and O_Stamp is empty.
770 -- Otherwise O_File is No_File.
772 procedure Collect_Arguments
773 (Source_File : File_Name_Type;
774 Source_Index : Int;
775 Is_Main_Source : Boolean;
776 Args : Argument_List);
777 -- Collect all arguments for a source to be compiled, including those
778 -- that come from a project file.
780 procedure Display (Program : String; Args : Argument_List);
781 -- Displays Program followed by the arguments in Args if variable
782 -- Display_Executed_Programs is set. The lower bound of Args must be 1.
784 procedure Report_Compilation_Failed;
785 -- Delete all temporary files and fail graciously
787 -----------------
788 -- Mapping files
789 -----------------
791 type Temp_Path_Names is
792 array (Project_Id range <>, Positive range <>) of Path_Name_Type;
794 type Temp_Path_Ptr is access Temp_Path_Names;
796 type Indices is array (Project_Id range <>) of Natural;
798 type Indices_Ptr is access Indices;
800 type Free_File_Indices is array
801 (Project_Id range <>, Positive range <>) of Positive;
803 type Free_Indices_Ptr is access Free_File_Indices;
805 The_Mapping_File_Names : Temp_Path_Ptr;
806 -- For each project, the name ids of the temporary mapping files used
808 Last_Mapping_File_Names : Indices_Ptr;
809 -- For each project, the index of the last mapping file created
811 The_Free_Mapping_File_Indices : Free_Indices_Ptr;
812 -- For each project, the indices in The_Mapping_File_Names of the mapping
813 -- file names that can be reused for subsequent compilations.
815 Last_Free_Indices : Indices_Ptr;
816 -- For each project, the number of mapping files that can be reused
818 Gnatmake_Mapping_File : String_Access := null;
819 -- The path name of a mapping file specified by switch -C=
821 procedure Delete_Mapping_Files;
822 -- Delete all temporary mapping files. Called only in Delete_All_Temp_Files
823 -- which ensures that Debug_Flag_N is False.
825 procedure Init_Mapping_File
826 (Project : Project_Id;
827 File_Index : in out Natural);
828 -- Create a new temporary mapping file, and fill it with the project file
829 -- mappings, when using project file(s). The out parameter File_Index is
830 -- the index to the name of the file in the array The_Mapping_File_Names.
832 procedure Delete_Temp_Config_Files;
833 -- Delete all temporary config files. Must not be called if Debug_Flag_N
834 -- is False.
836 procedure Delete_All_Temp_Files;
837 -- Delete all temp files (config files, mapping files, path files), unless
838 -- Debug_Flag_N is True (in which case all temp files are left for user
839 -- examination).
841 -------------------------------------------------
842 -- Subprogram declarations moved from the spec --
843 -------------------------------------------------
845 procedure Bind (ALI_File : File_Name_Type; Args : Argument_List);
846 -- Binds ALI_File. Args are the arguments to pass to the binder.
847 -- Args must have a lower bound of 1.
849 procedure Display_Commands (Display : Boolean := True);
850 -- The default behavior of Make commands (Compile_Sources, Bind, Link)
851 -- is to display them on stderr. This behavior can be changed repeatedly
852 -- by invoking this procedure.
854 -- If a compilation, bind or link failed one of the following 3 exceptions
855 -- is raised. These need to be handled by the calling routines.
857 procedure Compile_Sources
858 (Main_Source : File_Name_Type;
859 Args : Argument_List;
860 First_Compiled_File : out File_Name_Type;
861 Most_Recent_Obj_File : out File_Name_Type;
862 Most_Recent_Obj_Stamp : out Time_Stamp_Type;
863 Main_Unit : out Boolean;
864 Compilation_Failures : out Natural;
865 Main_Index : Int := 0;
866 Check_Readonly_Files : Boolean := False;
867 Do_Not_Execute : Boolean := False;
868 Force_Compilations : Boolean := False;
869 Keep_Going : Boolean := False;
870 In_Place_Mode : Boolean := False;
871 Initialize_ALI_Data : Boolean := True;
872 Max_Process : Positive := 1);
873 -- Compile_Sources will recursively compile all the sources needed by
874 -- Main_Source. Before calling this routine make sure Namet has been
875 -- initialized. This routine can be called repeatedly with different
876 -- Main_Source file as long as all the source (-I flags), library
877 -- (-B flags) and ada library (-A flags) search paths between calls are
878 -- *exactly* the same. The default directory must also be the same.
880 -- Args contains the arguments to use during the compilations.
881 -- The lower bound of Args must be 1.
883 -- First_Compiled_File is set to the name of the first file that is
884 -- compiled or that needs to be compiled. This is set to No_Name if no
885 -- compilations were needed.
887 -- Most_Recent_Obj_File is set to the full name of the most recent
888 -- object file found when no compilations are needed, that is when
889 -- First_Compiled_File is set to No_Name. When First_Compiled_File
890 -- is set then Most_Recent_Obj_File is set to No_Name.
892 -- Most_Recent_Obj_Stamp is the time stamp of Most_Recent_Obj_File.
894 -- Main_Unit is set to True if Main_Source can be a main unit.
895 -- If Do_Not_Execute is False and First_Compiled_File /= No_Name
896 -- the value of Main_Unit is always False.
897 -- Is this used any more??? It is certainly not used by gnatmake???
899 -- Compilation_Failures is a count of compilation failures. This count
900 -- is used to extract compilation failure reports with Extract_Failure.
902 -- Main_Index, when not zero, is the index of the main unit in source
903 -- file Main_Source which is a multi-unit source.
904 -- Zero indicates that Main_Source is a single unit source file.
906 -- Check_Readonly_Files set it to True to compile source files
907 -- which library files are read-only. When compiling GNAT predefined
908 -- files the "-gnatg" flag is used.
910 -- Do_Not_Execute set it to True to find out the first source that
911 -- needs to be recompiled, but without recompiling it. This file is
912 -- saved in First_Compiled_File.
914 -- Force_Compilations forces all compilations no matter what but
915 -- recompiles read-only files only if Check_Readonly_Files
916 -- is set.
918 -- Keep_Going when True keep compiling even in the presence of
919 -- compilation errors.
921 -- In_Place_Mode when True save library/object files in their object
922 -- directory if they already exist; otherwise, in the source directory.
924 -- Initialize_ALI_Data set it to True when you want to initialize ALI
925 -- data-structures. This is what you should do most of the time.
926 -- (especially the first time around when you call this routine).
927 -- This parameter is set to False to preserve previously recorded
928 -- ALI file data.
930 -- Max_Process is the maximum number of processes that should be spawned
931 -- to carry out compilations.
933 -- Flags in Package Opt Affecting Compile_Sources
934 -- -----------------------------------------------
936 -- Check_Object_Consistency set it to False to omit all consistency
937 -- checks between an .ali file and its corresponding object file.
938 -- When this flag is set to true, every time an .ali is read,
939 -- package Osint checks that the corresponding object file
940 -- exists and is more recent than the .ali.
942 -- Use of Name Table Info
943 -- ----------------------
945 -- All file names manipulated by Compile_Sources are entered into the
946 -- Names table. The Byte field of a source file is used to mark it.
948 -- Calling Compile_Sources Several Times
949 -- -------------------------------------
951 -- Upon return from Compile_Sources all the ALI data structures are left
952 -- intact for further browsing. HOWEVER upon entry to this routine ALI
953 -- data structures are re-initialized if parameter Initialize_ALI_Data
954 -- above is set to true. Typically this is what you want the first time
955 -- you call Compile_Sources. You should not load an ali file, call this
956 -- routine with flag Initialize_ALI_Data set to True and then expect
957 -- that ALI information to be around after the call. Note that the first
958 -- time you call Compile_Sources you better set Initialize_ALI_Data to
959 -- True unless you have called Initialize_ALI yourself.
961 -- Compile_Sources ALGORITHM : Compile_Sources (Main_Source)
962 -- -------------------------
964 -- 1. Insert Main_Source in a Queue (Q) and mark it.
966 -- 2. Let unit.adb be the file at the head of the Q. If unit.adb is
967 -- missing but its corresponding ali file is in an Ada library directory
968 -- (see below) then, remove unit.adb from the Q and goto step 4.
969 -- Otherwise, look at the files under the D (dependency) section of
970 -- unit.ali. If unit.ali does not exist or some of the time stamps do
971 -- not match, (re)compile unit.adb.
973 -- An Ada library directory is a directory containing Ada specs, ali
974 -- and object files but no source files for the bodies. An Ada library
975 -- directory is communicated to gnatmake by means of some switch so that
976 -- gnatmake can skip the sources whole ali are in that directory.
977 -- There are two reasons for skipping the sources in this case. Firstly,
978 -- Ada libraries typically come without full sources but binding and
979 -- linking against those libraries is still possible. Secondly, it would
980 -- be very wasteful for gnatmake to systematically check the consistency
981 -- of every external Ada library used in a program. The binder is
982 -- already in charge of catching any potential inconsistencies.
984 -- 3. Look into the W section of unit.ali and insert into the Q all
985 -- unmarked source files. Mark all files newly inserted in the Q.
986 -- Specifically, assuming that the W section looks like
988 -- W types%s types.adb types.ali
989 -- W unchecked_deallocation%s
990 -- W xref_tab%s xref_tab.adb xref_tab.ali
992 -- Then xref_tab.adb and types.adb are inserted in the Q if they are not
993 -- already marked.
994 -- Note that there is no file listed under W unchecked_deallocation%s
995 -- so no generic body should ever be explicitly compiled (unless the
996 -- Main_Source at the start was a generic body).
998 -- 4. Repeat steps 2 and 3 above until the Q is empty
1000 -- Note that the above algorithm works because the units withed in
1001 -- subunits are transitively included in the W section (with section) of
1002 -- the main unit. Likewise the withed units in a generic body needed
1003 -- during a compilation are also transitively included in the W section
1004 -- of the originally compiled file.
1006 procedure Initialize;
1007 -- Performs default and package initialization. Therefore,
1008 -- Compile_Sources can be called by an external unit.
1010 procedure Link
1011 (ALI_File : File_Name_Type;
1012 Args : Argument_List;
1013 Success : out Boolean);
1014 -- Links ALI_File. Args are the arguments to pass to the linker.
1015 -- Args must have a lower bound of 1. Success indicates if the link
1016 -- succeeded or not.
1018 procedure Scan_Make_Arg (Argv : String; And_Save : Boolean);
1019 -- Scan make arguments. Argv is a single argument to be processed
1021 -------------------
1022 -- Add_Arguments --
1023 -------------------
1025 procedure Add_Arguments (Args : Argument_List) is
1026 begin
1027 if Arguments = null then
1028 Arguments := new Argument_List (1 .. Args'Length + 10);
1030 else
1031 while Last_Argument + Args'Length > Arguments'Last loop
1032 declare
1033 New_Arguments : constant Argument_List_Access :=
1034 new Argument_List (1 .. Arguments'Last * 2);
1035 begin
1036 New_Arguments (1 .. Last_Argument) :=
1037 Arguments (1 .. Last_Argument);
1038 Arguments := New_Arguments;
1039 end;
1040 end loop;
1041 end if;
1043 Arguments (Last_Argument + 1 .. Last_Argument + Args'Length) := Args;
1044 Last_Argument := Last_Argument + Args'Length;
1045 end Add_Arguments;
1047 -- --------------------
1048 -- -- Add_Dependency --
1049 -- --------------------
1051 -- procedure Add_Dependency (S : File_Name_Type; On : File_Name_Type) is
1052 -- begin
1053 -- Dependencies.Increment_Last;
1054 -- Dependencies.Table (Dependencies.Last) := (S, On);
1055 -- end Add_Dependency;
1057 ----------------------------
1058 -- Add_Library_Search_Dir --
1059 ----------------------------
1061 procedure Add_Library_Search_Dir
1062 (Path : String;
1063 On_Command_Line : Boolean)
1065 begin
1066 if On_Command_Line then
1067 Add_Lib_Search_Dir (Normalize_Pathname (Path));
1069 else
1070 Get_Name_String
1071 (Project_Tree.Projects.Table (Main_Project).Directory.Display_Name);
1072 Add_Lib_Search_Dir
1073 (Normalize_Pathname (Path, Name_Buffer (1 .. Name_Len)));
1074 end if;
1075 end Add_Library_Search_Dir;
1077 --------------------
1078 -- Add_Object_Dir --
1079 --------------------
1081 procedure Add_Object_Dir (N : String) is
1082 begin
1083 Add_Lib_Search_Dir (N);
1085 if Verbose_Mode then
1086 Write_Str ("Adding object directory """);
1087 Write_Str (N);
1088 Write_Str (""".");
1089 Write_Eol;
1090 end if;
1091 end Add_Object_Dir;
1093 --------------------
1094 -- Add_Source_Dir --
1095 --------------------
1097 procedure Add_Source_Dir (N : String) is
1098 begin
1099 Add_Src_Search_Dir (N);
1101 if Verbose_Mode then
1102 Write_Str ("Adding source directory """);
1103 Write_Str (N);
1104 Write_Str (""".");
1105 Write_Eol;
1106 end if;
1107 end Add_Source_Dir;
1109 ---------------------------
1110 -- Add_Source_Search_Dir --
1111 ---------------------------
1113 procedure Add_Source_Search_Dir
1114 (Path : String;
1115 On_Command_Line : Boolean)
1117 begin
1118 if On_Command_Line then
1119 Add_Src_Search_Dir (Normalize_Pathname (Path));
1121 else
1122 Get_Name_String
1123 (Project_Tree.Projects.Table (Main_Project).Directory.Display_Name);
1124 Add_Src_Search_Dir
1125 (Normalize_Pathname (Path, Name_Buffer (1 .. Name_Len)));
1126 end if;
1127 end Add_Source_Search_Dir;
1129 ----------------
1130 -- Add_Switch --
1131 ----------------
1133 procedure Add_Switch
1134 (S : String_Access;
1135 Program : Make_Program_Type;
1136 Append_Switch : Boolean := True;
1137 And_Save : Boolean := True)
1139 generic
1140 with package T is new Table.Table (<>);
1141 procedure Generic_Position (New_Position : out Integer);
1142 -- Generic procedure that chooses a position for S in T at the
1143 -- beginning or the end, depending on the boolean Append_Switch.
1144 -- Calling this procedure may expand the table.
1146 ----------------------
1147 -- Generic_Position --
1148 ----------------------
1150 procedure Generic_Position (New_Position : out Integer) is
1151 begin
1152 T.Increment_Last;
1154 if Append_Switch then
1155 New_Position := Integer (T.Last);
1156 else
1157 for J in reverse T.Table_Index_Type'Succ (T.First) .. T.Last loop
1158 T.Table (J) := T.Table (T.Table_Index_Type'Pred (J));
1159 end loop;
1161 New_Position := Integer (T.First);
1162 end if;
1163 end Generic_Position;
1165 procedure Gcc_Switches_Pos is new Generic_Position (Gcc_Switches);
1166 procedure Binder_Switches_Pos is new Generic_Position (Binder_Switches);
1167 procedure Linker_Switches_Pos is new Generic_Position (Linker_Switches);
1169 procedure Saved_Gcc_Switches_Pos is new
1170 Generic_Position (Saved_Gcc_Switches);
1172 procedure Saved_Binder_Switches_Pos is new
1173 Generic_Position (Saved_Binder_Switches);
1175 procedure Saved_Linker_Switches_Pos is new
1176 Generic_Position (Saved_Linker_Switches);
1178 New_Position : Integer;
1180 -- Start of processing for Add_Switch
1182 begin
1183 if And_Save then
1184 case Program is
1185 when Compiler =>
1186 Saved_Gcc_Switches_Pos (New_Position);
1187 Saved_Gcc_Switches.Table (New_Position) := S;
1189 when Binder =>
1190 Saved_Binder_Switches_Pos (New_Position);
1191 Saved_Binder_Switches.Table (New_Position) := S;
1193 when Linker =>
1194 Saved_Linker_Switches_Pos (New_Position);
1195 Saved_Linker_Switches.Table (New_Position) := S;
1197 when None =>
1198 raise Program_Error;
1199 end case;
1201 else
1202 case Program is
1203 when Compiler =>
1204 Gcc_Switches_Pos (New_Position);
1205 Gcc_Switches.Table (New_Position) := S;
1207 when Binder =>
1208 Binder_Switches_Pos (New_Position);
1209 Binder_Switches.Table (New_Position) := S;
1211 when Linker =>
1212 Linker_Switches_Pos (New_Position);
1213 Linker_Switches.Table (New_Position) := S;
1215 when None =>
1216 raise Program_Error;
1217 end case;
1218 end if;
1219 end Add_Switch;
1221 procedure Add_Switch
1222 (S : String;
1223 Program : Make_Program_Type;
1224 Append_Switch : Boolean := True;
1225 And_Save : Boolean := True)
1227 begin
1228 Add_Switch (S => new String'(S),
1229 Program => Program,
1230 Append_Switch => Append_Switch,
1231 And_Save => And_Save);
1232 end Add_Switch;
1234 ------------------
1235 -- Add_Switches --
1236 ------------------
1238 procedure Add_Switches
1239 (The_Package : Package_Id;
1240 File_Name : String;
1241 Index : Int;
1242 Program : Make_Program_Type)
1244 Switches : Variable_Value;
1245 Switch_List : String_List_Id;
1246 Element : String_Element;
1248 begin
1249 if File_Name'Length > 0 then
1250 Name_Len := File_Name'Length;
1251 Name_Buffer (1 .. Name_Len) := File_Name;
1252 Switches :=
1253 Switches_Of
1254 (Source_File => Name_Find,
1255 Source_File_Name => File_Name,
1256 Source_Index => Index,
1257 Naming => Project_Tree.Projects.Table
1258 (Main_Project).Naming,
1259 In_Package => The_Package,
1260 Allow_ALI =>
1261 Program = Binder or else Program = Linker);
1263 case Switches.Kind is
1264 when Undefined =>
1265 null;
1267 when List =>
1268 Program_Args := Program;
1270 Switch_List := Switches.Values;
1272 while Switch_List /= Nil_String loop
1273 Element := Project_Tree.String_Elements.Table (Switch_List);
1274 Get_Name_String (Element.Value);
1276 if Name_Len > 0 then
1277 declare
1278 Argv : constant String := Name_Buffer (1 .. Name_Len);
1279 -- We need a copy, because Name_Buffer may be modified
1281 begin
1282 if Verbose_Mode then
1283 Write_Str (" Adding ");
1284 Write_Line (Argv);
1285 end if;
1287 Scan_Make_Arg (Argv, And_Save => False);
1288 end;
1289 end if;
1291 Switch_List := Element.Next;
1292 end loop;
1294 when Single =>
1295 Program_Args := Program;
1296 Get_Name_String (Switches.Value);
1298 if Name_Len > 0 then
1299 declare
1300 Argv : constant String := Name_Buffer (1 .. Name_Len);
1301 -- We need a copy, because Name_Buffer may be modified
1303 begin
1304 if Verbose_Mode then
1305 Write_Str (" Adding ");
1306 Write_Line (Argv);
1307 end if;
1309 Scan_Make_Arg (Argv, And_Save => False);
1310 end;
1311 end if;
1312 end case;
1313 end if;
1314 end Add_Switches;
1316 ----------
1317 -- Bind --
1318 ----------
1320 procedure Bind (ALI_File : File_Name_Type; Args : Argument_List) is
1321 Bind_Args : Argument_List (1 .. Args'Last + 2);
1322 Bind_Last : Integer;
1323 Success : Boolean;
1325 begin
1326 pragma Assert (Args'First = 1);
1328 -- Optimize the simple case where the gnatbind command line looks like
1329 -- gnatbind -aO. -I- file.ali --into-> gnatbind file.adb
1331 if Args'Length = 2
1332 and then Args (Args'First).all = "-aO" & Normalized_CWD
1333 and then Args (Args'Last).all = "-I-"
1334 and then ALI_File = Strip_Directory (ALI_File)
1335 then
1336 Bind_Last := Args'First - 1;
1338 else
1339 Bind_Last := Args'Last;
1340 Bind_Args (Args'Range) := Args;
1341 end if;
1343 -- It is completely pointless to re-check source file time stamps. This
1344 -- has been done already by gnatmake
1346 Bind_Last := Bind_Last + 1;
1347 Bind_Args (Bind_Last) := Do_Not_Check_Flag;
1349 Get_Name_String (ALI_File);
1351 Bind_Last := Bind_Last + 1;
1352 Bind_Args (Bind_Last) := new String'(Name_Buffer (1 .. Name_Len));
1354 GNAT.OS_Lib.Normalize_Arguments (Bind_Args (Args'First .. Bind_Last));
1356 Display (Gnatbind.all, Bind_Args (Args'First .. Bind_Last));
1358 if Gnatbind_Path = null then
1359 Make_Failed ("error, unable to locate ", Gnatbind.all);
1360 end if;
1362 GNAT.OS_Lib.Spawn
1363 (Gnatbind_Path.all, Bind_Args (Args'First .. Bind_Last), Success);
1365 if not Success then
1366 Make_Failed ("*** bind failed.");
1367 end if;
1368 end Bind;
1370 --------------------------------
1371 -- Change_To_Object_Directory --
1372 --------------------------------
1374 procedure Change_To_Object_Directory (Project : Project_Id) is
1375 Actual_Project : Project_Id;
1376 Object_Directory : Path_Name_Type;
1378 begin
1379 -- For sources outside of any project, compilation occurs in the object
1380 -- directory of the main project, otherwise we use the project given.
1382 if Project = No_Project then
1383 Actual_Project := Main_Project;
1384 else
1385 Actual_Project := Project;
1386 end if;
1388 -- Nothing to do if the current working directory is already the correct
1389 -- object directory.
1391 if Project_Of_Current_Object_Directory /= Actual_Project then
1392 Project_Of_Current_Object_Directory := Actual_Project;
1393 Object_Directory :=
1394 Project_Tree.Projects.Table (Actual_Project).Object_Directory.Name;
1396 -- Set the working directory to the object directory of the actual
1397 -- project.
1399 if Verbose_Mode then
1400 Write_Str ("Changing to object directory of """);
1401 Write_Name
1402 (Project_Tree.Projects.Table (Actual_Project).Display_Name);
1403 Write_Str (""": """);
1404 Write_Name (Object_Directory);
1405 Write_Line ("""");
1406 end if;
1408 Change_Dir (Get_Name_String (Object_Directory));
1409 end if;
1411 exception
1412 -- Fail if unable to change to the object directory
1414 when Directory_Error =>
1415 Make_Failed ("unable to change to object directory """ &
1416 Path_Or_File_Name
1417 (Project_Tree.Projects.Table
1418 (Actual_Project).Object_Directory.Name) &
1419 """ of project " &
1420 Get_Name_String (Project_Tree.Projects.Table
1421 (Actual_Project).Display_Name));
1422 end Change_To_Object_Directory;
1424 -----------
1425 -- Check --
1426 -----------
1428 procedure Check
1429 (Source_File : File_Name_Type;
1430 Source_Index : Int;
1431 Is_Main_Source : Boolean;
1432 The_Args : Argument_List;
1433 Lib_File : File_Name_Type;
1434 Read_Only : Boolean;
1435 ALI : out ALI_Id;
1436 O_File : out File_Name_Type;
1437 O_Stamp : out Time_Stamp_Type)
1439 function First_New_Spec (A : ALI_Id) return File_Name_Type;
1440 -- Looks in the with table entries of A and returns the spec file name
1441 -- of the first withed unit (subprogram) for which no spec existed when
1442 -- A was generated but for which there exists one now, implying that A
1443 -- is now obsolete. If no such unit is found No_File is returned.
1444 -- Otherwise the spec file name of the unit is returned.
1446 -- **WARNING** in the event of Uname format modifications, one *MUST*
1447 -- make sure this function is also updated.
1449 -- Note: This function should really be in ali.adb and use Uname
1450 -- services, but this causes the whole compiler to be dragged along
1451 -- for gnatbind and gnatmake.
1453 --------------------
1454 -- First_New_Spec --
1455 --------------------
1457 function First_New_Spec (A : ALI_Id) return File_Name_Type is
1458 Spec_File_Name : File_Name_Type := No_File;
1460 function New_Spec (Uname : Unit_Name_Type) return Boolean;
1461 -- Uname is the name of the spec or body of some ada unit. This
1462 -- function returns True if the Uname is the name of a body which has
1463 -- a spec not mentioned in ALI file A. If True is returned
1464 -- Spec_File_Name above is set to the name of this spec file.
1466 --------------
1467 -- New_Spec --
1468 --------------
1470 function New_Spec (Uname : Unit_Name_Type) return Boolean is
1471 Spec_Name : Unit_Name_Type;
1472 File_Name : File_Name_Type;
1474 begin
1475 -- Test whether Uname is the name of a body unit (i.e. ends
1476 -- with %b)
1478 Get_Name_String (Uname);
1479 pragma
1480 Assert (Name_Len > 2 and then Name_Buffer (Name_Len - 1) = '%');
1482 if Name_Buffer (Name_Len) /= 'b' then
1483 return False;
1484 end if;
1486 -- Convert unit name into spec name
1488 -- ??? this code seems dubious in presence of pragma
1489 -- Source_File_Name since there is no more direct relationship
1490 -- between unit name and file name.
1492 -- ??? Further, what about alternative subunit naming
1494 Name_Buffer (Name_Len) := 's';
1495 Spec_Name := Name_Find;
1496 File_Name := Get_File_Name (Spec_Name, Subunit => False);
1498 -- Look if File_Name is mentioned in A's sdep list.
1499 -- If not look if the file exists. If it does return True.
1501 for D in
1502 ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep
1503 loop
1504 if Sdep.Table (D).Sfile = File_Name then
1505 return False;
1506 end if;
1507 end loop;
1509 if Full_Source_Name (File_Name) /= No_File then
1510 Spec_File_Name := File_Name;
1511 return True;
1512 end if;
1514 return False;
1515 end New_Spec;
1517 -- Start of processing for First_New_Spec
1519 begin
1520 U_Chk : for U in
1521 ALIs.Table (A).First_Unit .. ALIs.Table (A).Last_Unit
1522 loop
1523 exit U_Chk when Units.Table (U).Utype = Is_Body_Only
1524 and then New_Spec (Units.Table (U).Uname);
1526 for W in Units.Table (U).First_With
1528 Units.Table (U).Last_With
1529 loop
1530 exit U_Chk when
1531 Withs.Table (W).Afile /= No_File
1532 and then New_Spec (Withs.Table (W).Uname);
1533 end loop;
1534 end loop U_Chk;
1536 return Spec_File_Name;
1537 end First_New_Spec;
1539 ---------------------------------
1540 -- Data declarations for Check --
1541 ---------------------------------
1543 Full_Lib_File : File_Name_Type;
1544 -- Full name of current library file
1546 Full_Obj_File : File_Name_Type;
1547 -- Full name of the object file corresponding to Lib_File
1549 Lib_Stamp : Time_Stamp_Type;
1550 -- Time stamp of the current ada library file
1552 Obj_Stamp : Time_Stamp_Type;
1553 -- Time stamp of the current object file
1555 Modified_Source : File_Name_Type;
1556 -- The first source in Lib_File whose current time stamp differs
1557 -- from that stored in Lib_File.
1559 New_Spec : File_Name_Type;
1560 -- If Lib_File contains in its W (with) section a body (for a
1561 -- subprogram) for which there exists a spec and the spec did not
1562 -- appear in the Sdep section of Lib_File, New_Spec contains the file
1563 -- name of this new spec.
1565 Source_Name : File_Name_Type;
1566 Text : Text_Buffer_Ptr;
1568 Prev_Switch : String_Access;
1569 -- Previous switch processed
1571 Arg : Arg_Id := Arg_Id'First;
1572 -- Current index in Args.Table for a given unit (init to stop warning)
1574 Switch_Found : Boolean;
1575 -- True if a given switch has been found
1577 ALI_Project : Project_Id;
1578 -- If the ALI file is in the object directory of a project, this is
1579 -- the project id.
1581 -- Start of processing for Check
1583 begin
1584 pragma Assert (Lib_File /= No_File);
1586 -- If ALI file is read-only, temporarily set Check_Object_Consistency to
1587 -- False. We don't care if the object file is not there (presumably a
1588 -- library will be used for linking.)
1590 if Read_Only then
1591 declare
1592 Saved_Check_Object_Consistency : constant Boolean :=
1593 Check_Object_Consistency;
1594 begin
1595 Check_Object_Consistency := False;
1596 Text := Read_Library_Info (Lib_File);
1597 Check_Object_Consistency := Saved_Check_Object_Consistency;
1598 end;
1600 else
1601 Text := Read_Library_Info (Lib_File);
1602 end if;
1604 Full_Lib_File := Full_Library_Info_Name;
1605 Full_Obj_File := Full_Object_File_Name;
1606 Lib_Stamp := Current_Library_File_Stamp;
1607 Obj_Stamp := Current_Object_File_Stamp;
1609 if Full_Lib_File = No_File then
1610 Verbose_Msg
1611 (Lib_File,
1612 "being checked ...",
1613 Prefix => " ",
1614 Minimum_Verbosity => Opt.Medium);
1615 else
1616 Verbose_Msg
1617 (Full_Lib_File,
1618 "being checked ...",
1619 Prefix => " ",
1620 Minimum_Verbosity => Opt.Medium);
1621 end if;
1623 ALI := No_ALI_Id;
1624 O_File := Full_Obj_File;
1625 O_Stamp := Obj_Stamp;
1627 if Text = null then
1628 if Full_Lib_File = No_File then
1629 Verbose_Msg (Lib_File, "missing.");
1631 elsif Obj_Stamp (Obj_Stamp'First) = ' ' then
1632 Verbose_Msg (Full_Obj_File, "missing.");
1634 else
1635 Verbose_Msg
1636 (Full_Lib_File, "(" & String (Lib_Stamp) & ") newer than",
1637 Full_Obj_File, "(" & String (Obj_Stamp) & ")");
1638 end if;
1640 else
1641 ALI := Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
1642 Free (Text);
1644 if ALI = No_ALI_Id then
1645 Verbose_Msg (Full_Lib_File, "incorrectly formatted ALI file");
1646 return;
1648 elsif ALIs.Table (ALI).Ver (1 .. ALIs.Table (ALI).Ver_Len) /=
1649 Verbose_Library_Version
1650 then
1651 Verbose_Msg (Full_Lib_File, "compiled with old GNAT version");
1652 ALI := No_ALI_Id;
1653 return;
1654 end if;
1656 -- Don't take Ali file into account if it was generated with
1657 -- errors.
1659 if ALIs.Table (ALI).Compile_Errors then
1660 Verbose_Msg (Full_Lib_File, "had errors, must be recompiled");
1661 ALI := No_ALI_Id;
1662 return;
1663 end if;
1665 -- Don't take Ali file into account if it was generated without
1666 -- object.
1668 if Operating_Mode /= Check_Semantics
1669 and then ALIs.Table (ALI).No_Object
1670 then
1671 Verbose_Msg (Full_Lib_File, "has no corresponding object");
1672 ALI := No_ALI_Id;
1673 return;
1674 end if;
1676 -- Check for matching compiler switches if needed
1678 if Check_Switches then
1680 -- First, collect all the switches
1682 Collect_Arguments
1683 (Source_File, Source_Index, Is_Main_Source, The_Args);
1685 Prev_Switch := Dummy_Switch;
1687 Get_Name_String (ALIs.Table (ALI).Sfile);
1689 Switches_To_Check.Set_Last (0);
1691 for J in 1 .. Last_Argument loop
1693 -- Skip non switches -c, -I and -o switches
1695 if Arguments (J) (1) = '-'
1696 and then Arguments (J) (2) /= 'c'
1697 and then Arguments (J) (2) /= 'o'
1698 and then Arguments (J) (2) /= 'I'
1699 then
1700 Normalize_Compiler_Switches
1701 (Arguments (J).all,
1702 Normalized_Switches,
1703 Last_Norm_Switch);
1705 for K in 1 .. Last_Norm_Switch loop
1706 Switches_To_Check.Increment_Last;
1707 Switches_To_Check.Table (Switches_To_Check.Last) :=
1708 Normalized_Switches (K);
1709 end loop;
1710 end if;
1711 end loop;
1713 for J in 1 .. Switches_To_Check.Last loop
1715 -- Comparing switches is delicate because gcc reorders a number
1716 -- of switches, according to lang-specs.h, but gnatmake doesn't
1717 -- have sufficient knowledge to perform the same reordering.
1718 -- Instead, we ignore orders between different "first letter"
1719 -- switches, but keep orders between same switches, e.g -O -O2
1720 -- is different than -O2 -O, but -g -O is equivalent to -O -g.
1722 if Switches_To_Check.Table (J) (2) /= Prev_Switch (2) or else
1723 (Prev_Switch'Length >= 6 and then
1724 Prev_Switch (2 .. 5) = "gnat" and then
1725 Switches_To_Check.Table (J)'Length >= 6 and then
1726 Switches_To_Check.Table (J) (2 .. 5) = "gnat" and then
1727 Prev_Switch (6) /= Switches_To_Check.Table (J) (6))
1728 then
1729 Prev_Switch := Switches_To_Check.Table (J);
1730 Arg :=
1731 Units.Table (ALIs.Table (ALI).First_Unit).First_Arg;
1732 end if;
1734 Switch_Found := False;
1736 for K in Arg ..
1737 Units.Table (ALIs.Table (ALI).First_Unit).Last_Arg
1738 loop
1740 Switches_To_Check.Table (J).all = Args.Table (K).all
1741 then
1742 Arg := K + 1;
1743 Switch_Found := True;
1744 exit;
1745 end if;
1746 end loop;
1748 if not Switch_Found then
1749 if Verbose_Mode then
1750 Verbose_Msg (ALIs.Table (ALI).Sfile,
1751 "switch mismatch """ &
1752 Switches_To_Check.Table (J).all & '"');
1753 end if;
1755 ALI := No_ALI_Id;
1756 return;
1757 end if;
1758 end loop;
1760 if Switches_To_Check.Last /=
1761 Integer (Units.Table (ALIs.Table (ALI).First_Unit).Last_Arg -
1762 Units.Table (ALIs.Table (ALI).First_Unit).First_Arg + 1)
1763 then
1764 if Verbose_Mode then
1765 Verbose_Msg (ALIs.Table (ALI).Sfile,
1766 "different number of switches");
1768 for K in Units.Table (ALIs.Table (ALI).First_Unit).First_Arg
1769 .. Units.Table (ALIs.Table (ALI).First_Unit).Last_Arg
1770 loop
1771 Write_Str (Args.Table (K).all);
1772 Write_Char (' ');
1773 end loop;
1775 Write_Eol;
1777 for J in 1 .. Switches_To_Check.Last loop
1778 Write_Str (Switches_To_Check.Table (J).all);
1779 Write_Char (' ');
1780 end loop;
1782 Write_Eol;
1783 end if;
1785 ALI := No_ALI_Id;
1786 return;
1787 end if;
1788 end if;
1790 -- Get the source files and their message digests. Note that some
1791 -- sources may be missing if ALI is out-of-date.
1793 Set_Source_Table (ALI);
1795 Modified_Source := Time_Stamp_Mismatch (ALI, Read_Only);
1797 if Modified_Source /= No_File then
1798 ALI := No_ALI_Id;
1800 if Verbose_Mode then
1801 Source_Name := Full_Source_Name (Modified_Source);
1803 if Source_Name /= No_File then
1804 Verbose_Msg (Source_Name, "time stamp mismatch");
1805 else
1806 Verbose_Msg (Modified_Source, "missing");
1807 end if;
1808 end if;
1810 else
1811 New_Spec := First_New_Spec (ALI);
1813 if New_Spec /= No_File then
1814 ALI := No_ALI_Id;
1816 if Verbose_Mode then
1817 Source_Name := Full_Source_Name (New_Spec);
1819 if Source_Name /= No_File then
1820 Verbose_Msg (Source_Name, "new spec");
1821 else
1822 Verbose_Msg (New_Spec, "old spec missing");
1823 end if;
1824 end if;
1826 elsif Main_Project /= No_Project then
1828 -- Check if a file name does not correspond to the mapping of
1829 -- units to file names.
1831 declare
1832 WR : With_Record;
1833 Unit_Name : Name_Id;
1834 UID : Prj.Unit_Index;
1835 U_Data : Unit_Data;
1837 begin
1838 U_Chk :
1839 for U in ALIs.Table (ALI).First_Unit ..
1840 ALIs.Table (ALI).Last_Unit
1841 loop
1842 W_Check :
1843 for W in Units.Table (U).First_With
1845 Units.Table (U).Last_With
1846 loop
1847 WR := Withs.Table (W);
1849 if WR.Sfile /= No_File then
1850 Get_Name_String (WR.Uname);
1851 Name_Len := Name_Len - 2;
1852 Unit_Name := Name_Find;
1854 UID := Units_Htable.Get
1855 (Project_Tree.Units_HT, Unit_Name);
1857 if UID /= Prj.No_Unit_Index then
1858 U_Data := Project_Tree.Units.Table (UID);
1860 if U_Data.File_Names (Body_Part).Name /= WR.Sfile
1861 and then
1862 U_Data.File_Names (Specification).Name /=
1863 WR.Sfile
1864 then
1865 ALI := No_ALI_Id;
1867 Verbose_Msg
1868 (Unit_Name, " sources does not include ",
1869 Name_Id (WR.Sfile));
1871 return;
1872 end if;
1873 end if;
1874 end if;
1875 end loop W_Check;
1876 end loop U_Chk;
1877 end;
1879 -- Check that the ALI file is in the correct object directory.
1880 -- If it is in the object directory of a project that is
1881 -- extended and it depends on a source that is in one of its
1882 -- extending projects, then the ALI file is not in the correct
1883 -- object directory.
1885 -- First, find the project of this ALI file. As there may be
1886 -- several projects with the same object directory, we first
1887 -- need to find the project of the source.
1889 ALI_Project := No_Project;
1891 declare
1892 Udata : Prj.Unit_Data;
1894 begin
1895 for U in 1 .. Unit_Table.Last (Project_Tree.Units) loop
1896 Udata := Project_Tree.Units.Table (U);
1898 if Udata.File_Names (Body_Part).Name = Source_File then
1899 ALI_Project := Udata.File_Names (Body_Part).Project;
1900 exit;
1902 elsif
1903 Udata.File_Names (Specification).Name = Source_File
1904 then
1905 ALI_Project :=
1906 Udata.File_Names (Specification).Project;
1907 exit;
1908 end if;
1909 end loop;
1910 end;
1912 if ALI_Project = No_Project then
1913 return;
1914 end if;
1916 declare
1917 Obj_Dir : Path_Name_Type;
1918 Res_Obj_Dir : constant String :=
1919 Normalize_Pathname
1920 (Dir_Name
1921 (Get_Name_String (Full_Lib_File)),
1922 Resolve_Links => True,
1923 Case_Sensitive => False);
1925 begin
1926 Name_Len := 0;
1927 Add_Str_To_Name_Buffer (Res_Obj_Dir);
1929 if Name_Len > 1 and then
1930 (Name_Buffer (Name_Len) = '/' or else
1931 Name_Buffer (Name_Len) = Directory_Separator)
1932 then
1933 Name_Len := Name_Len - 1;
1934 end if;
1936 Obj_Dir := Name_Find;
1938 while ALI_Project /= No_Project and then
1939 Obj_Dir /=
1940 Project_Tree.Projects.Table
1941 (ALI_Project).Object_Directory.Name
1942 loop
1943 ALI_Project :=
1944 Project_Tree.Projects.Table (ALI_Project).Extended_By;
1945 end loop;
1946 end;
1948 if ALI_Project = No_Project then
1949 ALI := No_ALI_Id;
1951 Verbose_Msg
1952 (Lib_File, " wrong object directory");
1953 return;
1954 end if;
1956 -- If the ALI project is not extended, then it must be in
1957 -- the correct object directory.
1959 if Project_Tree.Projects.Table (ALI_Project).Extended_By =
1960 No_Project
1961 then
1962 return;
1963 end if;
1965 -- Count the extending projects
1967 declare
1968 Num_Ext : Natural;
1969 Proj : Project_Id;
1971 begin
1972 Num_Ext := 0;
1973 Proj := ALI_Project;
1974 loop
1975 Proj := Project_Tree.Projects.Table (Proj).Extended_By;
1976 exit when Proj = No_Project;
1977 Num_Ext := Num_Ext + 1;
1978 end loop;
1980 -- Make a list of the extending projects
1982 declare
1983 Projects : array (1 .. Num_Ext) of Project_Id;
1984 Dep : Sdep_Record;
1985 OK : Boolean := True;
1987 begin
1988 Proj := ALI_Project;
1989 for J in Projects'Range loop
1990 Proj := Project_Tree.Projects.Table (Proj).Extended_By;
1991 Projects (J) := Proj;
1992 end loop;
1994 -- Now check if any of the dependant sources are in
1995 -- any of these extending projects.
1997 D_Chk :
1998 for D in ALIs.Table (ALI).First_Sdep ..
1999 ALIs.Table (ALI).Last_Sdep
2000 loop
2001 Dep := Sdep.Table (D);
2003 Proj := No_Project;
2005 Unit_Loop :
2007 UID in 1 .. Unit_Table.Last (Project_Tree.Units)
2008 loop
2009 if Project_Tree.Units.Table (UID).
2010 File_Names (Body_Part).Name = Dep.Sfile
2011 then
2012 Proj := Project_Tree.Units.Table (UID).
2013 File_Names (Body_Part).Project;
2015 elsif Project_Tree.Units.Table (UID).
2016 File_Names (Specification).Name = Dep.Sfile
2017 then
2018 Proj := Project_Tree.Units.Table (UID).
2019 File_Names (Specification).Project;
2020 end if;
2022 -- If a source is in a project, check if it is one
2023 -- in the list.
2025 if Proj /= No_Project then
2026 for J in Projects'Range loop
2027 if Proj = Projects (J) then
2028 OK := False;
2029 exit D_Chk;
2030 end if;
2031 end loop;
2033 exit Unit_Loop;
2034 end if;
2035 end loop Unit_Loop;
2036 end loop D_Chk;
2038 -- If one of the dependent sources is in one project of
2039 -- the list, then we must recompile.
2041 if not OK then
2042 ALI := No_ALI_Id;
2043 Verbose_Msg (Lib_File, " wrong object directory");
2044 end if;
2045 end;
2046 end;
2047 end if;
2048 end if;
2049 end if;
2050 end Check;
2052 ------------------------
2053 -- Check_For_S_Switch --
2054 ------------------------
2056 procedure Check_For_S_Switch is
2057 begin
2058 -- By default, we generate an object file
2060 Output_Is_Object := True;
2062 for Arg in 1 .. Last_Argument loop
2063 if Arguments (Arg).all = "-S" then
2064 Output_Is_Object := False;
2066 elsif Arguments (Arg).all = "-c" then
2067 Output_Is_Object := True;
2068 end if;
2069 end loop;
2070 end Check_For_S_Switch;
2072 --------------------------
2073 -- Check_Linker_Options --
2074 --------------------------
2076 procedure Check_Linker_Options
2077 (E_Stamp : Time_Stamp_Type;
2078 O_File : out File_Name_Type;
2079 O_Stamp : out Time_Stamp_Type)
2081 procedure Check_File (File : File_Name_Type);
2082 -- Update O_File and O_Stamp if the given file is younger than E_Stamp
2083 -- and O_Stamp, or if O_File is No_File and File does not exist.
2085 function Get_Library_File (Name : String) return File_Name_Type;
2086 -- Return the full file name including path of a library based
2087 -- on the name specified with the -l linker option, using the
2088 -- Ada object path. Return No_File if no such file can be found.
2090 type Char_Array is array (Natural) of Character;
2091 type Char_Array_Access is access constant Char_Array;
2093 Template : Char_Array_Access;
2094 pragma Import (C, Template, "__gnat_library_template");
2096 ----------------
2097 -- Check_File --
2098 ----------------
2100 procedure Check_File (File : File_Name_Type) is
2101 Stamp : Time_Stamp_Type;
2102 Name : File_Name_Type := File;
2104 begin
2105 Get_Name_String (Name);
2107 -- Remove any trailing NUL characters
2109 while Name_Len >= Name_Buffer'First
2110 and then Name_Buffer (Name_Len) = NUL
2111 loop
2112 Name_Len := Name_Len - 1;
2113 end loop;
2115 if Name_Len = 0 then
2116 return;
2118 elsif Name_Buffer (1) = '-' then
2120 -- Do not check if File is a switch other than "-l"
2122 if Name_Buffer (2) /= 'l' then
2123 return;
2124 end if;
2126 -- The argument is a library switch, get actual name. It
2127 -- is necessary to make a copy of the relevant part of
2128 -- Name_Buffer as Get_Library_Name uses Name_Buffer as well.
2130 declare
2131 Base_Name : constant String := Name_Buffer (3 .. Name_Len);
2133 begin
2134 Name := Get_Library_File (Base_Name);
2135 end;
2137 if Name = No_File then
2138 return;
2139 end if;
2140 end if;
2142 Stamp := File_Stamp (Name);
2144 -- Find the youngest object file that is younger than the
2145 -- executable. If no such file exist, record the first object
2146 -- file that is not found.
2148 if (O_Stamp < Stamp and then E_Stamp < Stamp)
2149 or else (O_File = No_File and then Stamp (Stamp'First) = ' ')
2150 then
2151 O_Stamp := Stamp;
2152 O_File := Name;
2154 -- Strip the trailing NUL if present
2156 Get_Name_String (O_File);
2158 if Name_Buffer (Name_Len) = NUL then
2159 Name_Len := Name_Len - 1;
2160 O_File := Name_Find;
2161 end if;
2162 end if;
2163 end Check_File;
2165 ----------------------
2166 -- Get_Library_Name --
2167 ----------------------
2169 -- See comments in a-adaint.c about template syntax
2171 function Get_Library_File (Name : String) return File_Name_Type is
2172 File : File_Name_Type := No_File;
2174 begin
2175 Name_Len := 0;
2177 for Ptr in Template'Range loop
2178 case Template (Ptr) is
2179 when '*' =>
2180 Add_Str_To_Name_Buffer (Name);
2182 when ';' =>
2183 File := Full_Lib_File_Name (Name_Find);
2184 exit when File /= No_File;
2185 Name_Len := 0;
2187 when NUL =>
2188 exit;
2190 when others =>
2191 Add_Char_To_Name_Buffer (Template (Ptr));
2192 end case;
2193 end loop;
2195 -- The for loop exited because the end of the template
2196 -- was reached. File contains the last possible file name
2197 -- for the library.
2199 if File = No_File and then Name_Len > 0 then
2200 File := Full_Lib_File_Name (Name_Find);
2201 end if;
2203 return File;
2204 end Get_Library_File;
2206 -- Start of processing for Check_Linker_Options
2208 begin
2209 O_File := No_File;
2210 O_Stamp := (others => ' ');
2212 -- Process linker options from the ALI files
2214 for Opt in 1 .. Linker_Options.Last loop
2215 Check_File (File_Name_Type (Linker_Options.Table (Opt).Name));
2216 end loop;
2218 -- Process options given on the command line
2220 for Opt in Linker_Switches.First .. Linker_Switches.Last loop
2222 -- Check if the previous Opt has one of the two switches
2223 -- that take an extra parameter. (See GCC manual.)
2225 if Opt = Linker_Switches.First
2226 or else (Linker_Switches.Table (Opt - 1).all /= "-u"
2227 and then
2228 Linker_Switches.Table (Opt - 1).all /= "-Xlinker"
2229 and then
2230 Linker_Switches.Table (Opt - 1).all /= "-L")
2231 then
2232 Name_Len := 0;
2233 Add_Str_To_Name_Buffer (Linker_Switches.Table (Opt).all);
2234 Check_File (Name_Find);
2235 end if;
2236 end loop;
2238 end Check_Linker_Options;
2240 -----------------
2241 -- Check_Steps --
2242 -----------------
2244 procedure Check_Steps is
2245 begin
2246 -- If either -c, -b or -l has been specified, we will not necessarily
2247 -- execute all steps.
2249 if Make_Steps then
2250 Do_Compile_Step := Do_Compile_Step and Compile_Only;
2251 Do_Bind_Step := Do_Bind_Step and Bind_Only;
2252 Do_Link_Step := Do_Link_Step and Link_Only;
2254 -- If -c has been specified, but not -b, ignore any potential -l
2256 if Do_Compile_Step and then not Do_Bind_Step then
2257 Do_Link_Step := False;
2258 end if;
2259 end if;
2260 end Check_Steps;
2262 -----------------------
2263 -- Collect_Arguments --
2264 -----------------------
2266 procedure Collect_Arguments
2267 (Source_File : File_Name_Type;
2268 Source_Index : Int;
2269 Is_Main_Source : Boolean;
2270 Args : Argument_List)
2272 begin
2273 Arguments_Collected := True;
2274 Arguments_Project := No_Project;
2275 Last_Argument := 0;
2276 Add_Arguments (Args);
2278 if Main_Project /= No_Project then
2279 declare
2280 Source_File_Name : constant String :=
2281 Get_Name_String (Source_File);
2282 Compiler_Package : Prj.Package_Id;
2283 Switches : Prj.Variable_Value;
2284 Data : Project_Data;
2286 begin
2287 Prj.Env.
2288 Get_Reference
2289 (Source_File_Name => Source_File_Name,
2290 Project => Arguments_Project,
2291 Path => Arguments_Path_Name,
2292 In_Tree => Project_Tree);
2294 -- If the source is not a source of a project file, add the
2295 -- recorded arguments. Check will be done later if the source
2296 -- need to be compiled that the switch -x has been used.
2298 if Arguments_Project = No_Project then
2299 Add_Arguments (The_Saved_Gcc_Switches.all);
2301 elsif not Project_Tree.Projects.Table
2302 (Arguments_Project).Externally_Built
2303 then
2304 -- We get the project directory for the relative path
2305 -- switches and arguments.
2307 Data := Project_Tree.Projects.Table (Arguments_Project);
2309 -- If the source is in an extended project, we go to
2310 -- the ultimate extending project.
2312 while Data.Extended_By /= No_Project loop
2313 Arguments_Project := Data.Extended_By;
2314 Data := Project_Tree.Projects.Table (Arguments_Project);
2315 end loop;
2317 -- If building a dynamic or relocatable library, compile with
2318 -- PIC option, if it exists.
2320 if Data.Library and then Data.Library_Kind /= Static then
2321 declare
2322 PIC : constant String := MLib.Tgt.PIC_Option;
2324 begin
2325 if PIC /= "" then
2326 Add_Arguments ((1 => new String'(PIC)));
2327 end if;
2328 end;
2329 end if;
2331 if Data.Dir_Path = null then
2332 Data.Dir_Path :=
2333 new String'(Get_Name_String (Data.Directory.Display_Name));
2334 Project_Tree.Projects.Table (Arguments_Project) :=
2335 Data;
2336 end if;
2338 -- We now look for package Compiler and get the switches from
2339 -- this package.
2341 Compiler_Package :=
2342 Prj.Util.Value_Of
2343 (Name => Name_Compiler,
2344 In_Packages => Data.Decl.Packages,
2345 In_Tree => Project_Tree);
2347 if Compiler_Package /= No_Package then
2349 -- If package Gnatmake.Compiler exists, we get the specific
2350 -- switches for the current source, or the global switches,
2351 -- if any.
2353 Switches :=
2354 Switches_Of
2355 (Source_File => Source_File,
2356 Source_File_Name => Source_File_Name,
2357 Source_Index => Source_Index,
2358 Naming => Data.Naming,
2359 In_Package => Compiler_Package,
2360 Allow_ALI => False);
2362 end if;
2364 case Switches.Kind is
2366 -- We have a list of switches. We add these switches,
2367 -- plus the saved gcc switches.
2369 when List =>
2371 declare
2372 Current : String_List_Id := Switches.Values;
2373 Element : String_Element;
2374 Number : Natural := 0;
2376 begin
2377 while Current /= Nil_String loop
2378 Element := Project_Tree.String_Elements.
2379 Table (Current);
2380 Number := Number + 1;
2381 Current := Element.Next;
2382 end loop;
2384 declare
2385 New_Args : Argument_List (1 .. Number);
2386 Last_New : Natural := 0;
2388 begin
2389 Current := Switches.Values;
2391 for Index in New_Args'Range loop
2392 Element := Project_Tree.String_Elements.
2393 Table (Current);
2394 Get_Name_String (Element.Value);
2396 if Name_Len > 0 then
2397 Last_New := Last_New + 1;
2398 New_Args (Last_New) :=
2399 new String'(Name_Buffer (1 .. Name_Len));
2400 Test_If_Relative_Path
2401 (New_Args (Last_New),
2402 Parent => Data.Dir_Path,
2403 Including_Non_Switch => False);
2404 end if;
2406 Current := Element.Next;
2407 end loop;
2409 Add_Arguments
2410 (Configuration_Pragmas_Switch
2411 (Arguments_Project) &
2412 New_Args (1 .. Last_New) &
2413 The_Saved_Gcc_Switches.all);
2414 end;
2415 end;
2417 -- We have a single switch. We add this switch,
2418 -- plus the saved gcc switches.
2420 when Single =>
2421 Get_Name_String (Switches.Value);
2423 declare
2424 New_Args : Argument_List :=
2425 (1 => new String'
2426 (Name_Buffer (1 .. Name_Len)));
2428 begin
2429 Test_If_Relative_Path
2430 (New_Args (1),
2431 Parent => Data.Dir_Path,
2432 Including_Non_Switch => False);
2433 Add_Arguments
2434 (Configuration_Pragmas_Switch (Arguments_Project) &
2435 New_Args & The_Saved_Gcc_Switches.all);
2436 end;
2438 -- We have no switches from Gnatmake.Compiler.
2439 -- We add the saved gcc switches.
2441 when Undefined =>
2442 Add_Arguments
2443 (Configuration_Pragmas_Switch (Arguments_Project) &
2444 The_Saved_Gcc_Switches.all);
2445 end case;
2446 end if;
2447 end;
2448 end if;
2450 -- For VMS, when compiling the main source, add switch
2451 -- -mdebug-main=_ada_ so that the executable can be debugged
2452 -- by the standard VMS debugger.
2454 if not No_Main_Subprogram
2455 and then Targparm.OpenVMS_On_Target
2456 and then Is_Main_Source
2457 then
2458 -- First, check if compilation will be invoked with -g
2460 for J in 1 .. Last_Argument loop
2461 if Arguments (J)'Length >= 2
2462 and then Arguments (J) (1 .. 2) = "-g"
2463 and then (Arguments (J)'Length < 5
2464 or else Arguments (J) (1 .. 5) /= "-gnat")
2465 then
2466 Add_Arguments
2467 ((1 => new String'("-mdebug-main=_ada_")));
2468 exit;
2469 end if;
2470 end loop;
2471 end if;
2473 -- Set Output_Is_Object, depending if there is a -S switch.
2474 -- If the bind step is not performed, and there is a -S switch,
2475 -- then we will not check for a valid object file.
2477 Check_For_S_Switch;
2478 end Collect_Arguments;
2480 ---------------------
2481 -- Compile_Sources --
2482 ---------------------
2484 procedure Compile_Sources
2485 (Main_Source : File_Name_Type;
2486 Args : Argument_List;
2487 First_Compiled_File : out File_Name_Type;
2488 Most_Recent_Obj_File : out File_Name_Type;
2489 Most_Recent_Obj_Stamp : out Time_Stamp_Type;
2490 Main_Unit : out Boolean;
2491 Compilation_Failures : out Natural;
2492 Main_Index : Int := 0;
2493 Check_Readonly_Files : Boolean := False;
2494 Do_Not_Execute : Boolean := False;
2495 Force_Compilations : Boolean := False;
2496 Keep_Going : Boolean := False;
2497 In_Place_Mode : Boolean := False;
2498 Initialize_ALI_Data : Boolean := True;
2499 Max_Process : Positive := 1)
2501 Source_Unit : Unit_Name_Type;
2502 -- Current source unit
2504 Source_File : File_Name_Type;
2505 -- Current source file
2507 Full_Source_File : File_Name_Type;
2508 -- Full name of the current source file
2510 Lib_File : File_Name_Type;
2511 -- Current library file
2513 Full_Lib_File : File_Name_Type;
2514 -- Full name of the current library file
2516 Obj_File : File_Name_Type;
2517 -- Full name of the object file corresponding to Lib_File
2519 Obj_Stamp : Time_Stamp_Type;
2520 -- Time stamp of the current object file
2522 Sfile : File_Name_Type;
2523 -- Contains the source file of the units withed by Source_File
2525 Uname : Unit_Name_Type;
2526 -- Contains the unit name of the units withed by Source_File
2528 ALI : ALI_Id;
2529 -- ALI Id of the current ALI file
2531 -- Comment following declarations ???
2533 Read_Only : Boolean := False;
2535 Compilation_OK : Boolean;
2536 Need_To_Compile : Boolean;
2538 Pid : Process_Id;
2539 Text : Text_Buffer_Ptr;
2541 Mfile : Natural := No_Mapping_File;
2543 Need_To_Check_Standard_Library : Boolean :=
2544 Check_Readonly_Files
2545 and not Unique_Compile;
2547 Mapping_File_Arg : String_Access;
2549 Process_Created : Boolean := False;
2551 procedure Add_Process
2552 (Pid : Process_Id;
2553 Sfile : File_Name_Type;
2554 Afile : File_Name_Type;
2555 Uname : Unit_Name_Type;
2556 Mfile : Natural := No_Mapping_File);
2557 -- Adds process Pid to the current list of outstanding compilation
2558 -- processes and record the full name of the source file Sfile that
2559 -- we are compiling, the name of its library file Afile and the
2560 -- name of its unit Uname. If Mfile is not equal to No_Mapping_File,
2561 -- it is the index of the mapping file used during compilation in the
2562 -- array The_Mapping_File_Names.
2564 procedure Await_Compile
2565 (Sfile : out File_Name_Type;
2566 Afile : out File_Name_Type;
2567 Uname : out Unit_Name_Type;
2568 OK : out Boolean);
2569 -- Awaits that an outstanding compilation process terminates. When
2570 -- it does set Sfile to the name of the source file that was compiled
2571 -- Afile to the name of its library file and Uname to the name of its
2572 -- unit. Note that this time stamp can be used to check whether the
2573 -- compilation did generate an object file. OK is set to True if the
2574 -- compilation succeeded. Note that Sfile, Afile and Uname could be
2575 -- resp. No_File, No_File and No_Name if there were no compilations
2576 -- to wait for.
2578 function Bad_Compilation_Count return Natural;
2579 -- Returns the number of compilation failures
2581 procedure Check_Standard_Library;
2582 -- Check if s-stalib.adb needs to be compiled
2584 procedure Collect_Arguments_And_Compile
2585 (Source_File : File_Name_Type;
2586 Source_Index : Int);
2587 -- Collect arguments from project file (if any) and compile
2589 function Compile
2590 (S : File_Name_Type;
2591 L : File_Name_Type;
2592 Source_Index : Int;
2593 Args : Argument_List) return Process_Id;
2594 -- Compiles S using Args. If S is a GNAT predefined source "-gnatpg" is
2595 -- added to Args. Non blocking call. L corresponds to the expected
2596 -- library file name. Process_Id of the process spawned to execute the
2597 -- compilation.
2599 package Good_ALI is new Table.Table (
2600 Table_Component_Type => ALI_Id,
2601 Table_Index_Type => Natural,
2602 Table_Low_Bound => 1,
2603 Table_Initial => 50,
2604 Table_Increment => 100,
2605 Table_Name => "Make.Good_ALI");
2606 -- Contains the set of valid ALI files that have not yet been scanned
2608 function Good_ALI_Present return Boolean;
2609 -- Returns True if any ALI file was recorded in the previous set
2611 procedure Get_Mapping_File (Project : Project_Id);
2612 -- Get a mapping file name. If there is one to be reused, reuse it.
2613 -- Otherwise, create a new mapping file.
2615 function Get_Next_Good_ALI return ALI_Id;
2616 -- Returns the next good ALI_Id record
2618 procedure Record_Failure
2619 (File : File_Name_Type;
2620 Unit : Unit_Name_Type;
2621 Found : Boolean := True);
2622 -- Records in the previous table that the compilation for File failed.
2623 -- If Found is False then the compilation of File failed because we
2624 -- could not find it. Records also Unit when possible.
2626 procedure Record_Good_ALI (A : ALI_Id);
2627 -- Records in the previous set the Id of an ALI file
2629 -----------------
2630 -- Add_Process --
2631 -----------------
2633 procedure Add_Process
2634 (Pid : Process_Id;
2635 Sfile : File_Name_Type;
2636 Afile : File_Name_Type;
2637 Uname : Unit_Name_Type;
2638 Mfile : Natural := No_Mapping_File)
2640 OC1 : constant Positive := Outstanding_Compiles + 1;
2642 begin
2643 pragma Assert (OC1 <= Max_Process);
2644 pragma Assert (Pid /= Invalid_Pid);
2646 Running_Compile (OC1).Pid := Pid;
2647 Running_Compile (OC1).Full_Source_File := Sfile;
2648 Running_Compile (OC1).Lib_File := Afile;
2649 Running_Compile (OC1).Source_Unit := Uname;
2650 Running_Compile (OC1).Mapping_File := Mfile;
2651 Running_Compile (OC1).Project := Arguments_Project;
2652 Running_Compile (OC1).Syntax_Only := Syntax_Only;
2653 Running_Compile (OC1).Output_Is_Object := Output_Is_Object;
2655 Outstanding_Compiles := OC1;
2656 end Add_Process;
2658 --------------------
2659 -- Await_Compile --
2660 -------------------
2662 procedure Await_Compile
2663 (Sfile : out File_Name_Type;
2664 Afile : out File_Name_Type;
2665 Uname : out Unit_Name_Type;
2666 OK : out Boolean)
2668 Pid : Process_Id;
2669 Project : Project_Id;
2671 begin
2672 pragma Assert (Outstanding_Compiles > 0);
2674 Sfile := No_File;
2675 Afile := No_File;
2676 Uname := No_Unit_Name;
2677 OK := False;
2679 -- The loop here is a work-around for a problem on VMS; in some
2680 -- circumstances (shared library and several executables, for
2681 -- example), there are child processes other than compilation
2682 -- processes that are received. Until this problem is resolved,
2683 -- we will ignore such processes.
2685 loop
2686 Wait_Process (Pid, OK);
2688 if Pid = Invalid_Pid then
2689 return;
2690 end if;
2692 for J in Running_Compile'First .. Outstanding_Compiles loop
2693 if Pid = Running_Compile (J).Pid then
2694 Sfile := Running_Compile (J).Full_Source_File;
2695 Afile := Running_Compile (J).Lib_File;
2696 Uname := Running_Compile (J).Source_Unit;
2697 Syntax_Only := Running_Compile (J).Syntax_Only;
2698 Output_Is_Object := Running_Compile (J).Output_Is_Object;
2699 Project := Running_Compile (J).Project;
2701 -- If a mapping file was used by this compilation,
2702 -- get its file name for reuse by a subsequent compilation
2704 if Running_Compile (J).Mapping_File /= No_Mapping_File then
2705 Last_Free_Indices (Project) :=
2706 Last_Free_Indices (Project) + 1;
2707 The_Free_Mapping_File_Indices
2708 (Project, Last_Free_Indices (Project)) :=
2709 Running_Compile (J).Mapping_File;
2710 end if;
2712 -- To actually remove this Pid and related info from
2713 -- Running_Compile replace its entry with the last valid
2714 -- entry in Running_Compile.
2716 if J = Outstanding_Compiles then
2717 null;
2719 else
2720 Running_Compile (J) :=
2721 Running_Compile (Outstanding_Compiles);
2722 end if;
2724 Outstanding_Compiles := Outstanding_Compiles - 1;
2725 return;
2726 end if;
2727 end loop;
2729 -- This child process was not one of our compilation processes;
2730 -- just ignore it for now.
2732 -- raise Program_Error;
2733 end loop;
2734 end Await_Compile;
2736 ---------------------------
2737 -- Bad_Compilation_Count --
2738 ---------------------------
2740 function Bad_Compilation_Count return Natural is
2741 begin
2742 return Bad_Compilation.Last - Bad_Compilation.First + 1;
2743 end Bad_Compilation_Count;
2745 ----------------------------
2746 -- Check_Standard_Library --
2747 ----------------------------
2749 procedure Check_Standard_Library is
2750 begin
2751 Need_To_Check_Standard_Library := False;
2753 if not Targparm.Suppress_Standard_Library_On_Target then
2754 declare
2755 Sfile : File_Name_Type;
2756 Add_It : Boolean := True;
2758 begin
2759 Name_Len := Standard_Library_Package_Body_Name'Length;
2760 Name_Buffer (1 .. Name_Len) :=
2761 Standard_Library_Package_Body_Name;
2762 Sfile := Name_Enter;
2764 -- If we have a special runtime, we add the standard
2765 -- library only if we can find it.
2767 if RTS_Switch then
2768 Add_It :=
2769 Find_File (Sfile, Osint.Source) /= No_File;
2770 end if;
2772 if Add_It then
2773 if Is_Marked (Sfile) then
2774 if Is_In_Obsoleted (Sfile) then
2775 Executable_Obsolete := True;
2776 end if;
2778 else
2779 Insert_Q (Sfile, Index => 0);
2780 Mark (Sfile, Index => 0);
2781 end if;
2782 end if;
2783 end;
2784 end if;
2785 end Check_Standard_Library;
2787 -----------------------------------
2788 -- Collect_Arguments_And_Compile --
2789 -----------------------------------
2791 procedure Collect_Arguments_And_Compile
2792 (Source_File : File_Name_Type;
2793 Source_Index : Int)
2795 begin
2796 -- Process_Created will be set True if an attempt is made to compile
2797 -- the source, that is if it is not in an externally built project.
2799 Process_Created := False;
2801 -- If arguments not yet collected (in Check), collect them now
2803 if not Arguments_Collected then
2804 Collect_Arguments
2805 (Source_File, Source_Index, Source_File = Main_Source, Args);
2806 end if;
2808 -- If we use mapping file (-P or -C switches), then get one
2810 if Create_Mapping_File then
2811 Get_Mapping_File (Arguments_Project);
2812 end if;
2814 -- If the source is part of a project file, we set the ADA_*_PATHs,
2815 -- check for an eventual library project, and use the full path.
2817 if Arguments_Project /= No_Project then
2818 if not Project_Tree.Projects.Table
2819 (Arguments_Project).Externally_Built
2820 then
2821 Prj.Env.Set_Ada_Paths (Arguments_Project, Project_Tree, True);
2823 if not Unique_Compile
2824 and then MLib.Tgt.Support_For_Libraries /= Prj.None
2825 then
2826 declare
2827 The_Data : Project_Data :=
2828 Project_Tree.Projects.Table
2829 (Arguments_Project);
2831 Prj : Project_Id := Arguments_Project;
2833 begin
2834 while The_Data.Extended_By /= No_Project loop
2835 Prj := The_Data.Extended_By;
2836 The_Data := Project_Tree.Projects.Table (Prj);
2837 end loop;
2839 if The_Data.Library
2840 and then not The_Data.Externally_Built
2841 and then not The_Data.Need_To_Build_Lib
2842 then
2843 -- Add to the Q all sources of the project that
2844 -- have not been marked
2846 Insert_Project_Sources
2847 (The_Project => Prj,
2848 All_Projects => False,
2849 Into_Q => True);
2851 -- Now mark the project as processed
2853 Project_Tree.Projects.Table
2854 (Prj).Need_To_Build_Lib := True;
2855 end if;
2856 end;
2857 end if;
2859 -- Change to the object directory of the project file,
2860 -- if necessary.
2862 Change_To_Object_Directory (Arguments_Project);
2864 Pid :=
2865 Compile
2866 (File_Name_Type (Arguments_Path_Name),
2867 Lib_File,
2868 Source_Index,
2869 Arguments (1 .. Last_Argument));
2870 Process_Created := True;
2871 end if;
2873 else
2874 -- If this is a source outside of any project file, make sure it
2875 -- will be compiled in object directory of the main project file.
2877 if Main_Project /= No_Project then
2878 Change_To_Object_Directory (Arguments_Project);
2879 end if;
2881 Pid := Compile (Full_Source_File, Lib_File, Source_Index,
2882 Arguments (1 .. Last_Argument));
2883 Process_Created := True;
2884 end if;
2885 end Collect_Arguments_And_Compile;
2887 -------------
2888 -- Compile --
2889 -------------
2891 function Compile
2892 (S : File_Name_Type;
2893 L : File_Name_Type;
2894 Source_Index : Int;
2895 Args : Argument_List) return Process_Id
2897 Comp_Args : Argument_List (Args'First .. Args'Last + 9);
2898 Comp_Next : Integer := Args'First;
2899 Comp_Last : Integer;
2900 Arg_Index : Integer;
2902 function Ada_File_Name (Name : File_Name_Type) return Boolean;
2903 -- Returns True if Name is the name of an ada source file
2904 -- (i.e. suffix is .ads or .adb)
2906 -------------------
2907 -- Ada_File_Name --
2908 -------------------
2910 function Ada_File_Name (Name : File_Name_Type) return Boolean is
2911 begin
2912 Get_Name_String (Name);
2913 return
2914 Name_Len > 4
2915 and then Name_Buffer (Name_Len - 3 .. Name_Len - 1) = ".ad"
2916 and then (Name_Buffer (Name_Len) = 'b'
2917 or else
2918 Name_Buffer (Name_Len) = 's');
2919 end Ada_File_Name;
2921 -- Start of processing for Compile
2923 begin
2924 Enter_Into_Obsoleted (S);
2926 -- By default, Syntax_Only is False
2928 Syntax_Only := False;
2930 for J in Args'Range loop
2931 if Args (J).all = "-gnats" then
2933 -- If we compile with -gnats, the bind step and the link step
2934 -- are inhibited. Also, we set Syntax_Only to True, so that
2935 -- we don't fail when we don't find the ALI file, after
2936 -- compilation.
2938 Do_Bind_Step := False;
2939 Do_Link_Step := False;
2940 Syntax_Only := True;
2942 elsif Args (J).all = "-gnatc" then
2944 -- If we compile with -gnatc, the bind step and the link step
2945 -- are inhibited. We set Syntax_Only to False for the case when
2946 -- -gnats was previously specified.
2948 Do_Bind_Step := False;
2949 Do_Link_Step := False;
2950 Syntax_Only := False;
2951 end if;
2952 end loop;
2954 Comp_Args (Comp_Next) := Comp_Flag;
2955 Comp_Next := Comp_Next + 1;
2957 -- Optimize the simple case where the gcc command line looks like
2958 -- gcc -c -I. ... -I- file.adb --into-> gcc -c ... file.adb
2960 if Args (Args'First).all = "-I" & Normalized_CWD
2961 and then Args (Args'Last).all = "-I-"
2962 and then S = Strip_Directory (S)
2963 then
2964 Comp_Last := Comp_Next + Args'Length - 3;
2965 Arg_Index := Args'First + 1;
2967 else
2968 Comp_Last := Comp_Next + Args'Length - 1;
2969 Arg_Index := Args'First;
2970 end if;
2972 -- Make a deep copy of the arguments, because Normalize_Arguments
2973 -- may deallocate some arguments.
2975 for J in Comp_Next .. Comp_Last loop
2976 Comp_Args (J) := new String'(Args (Arg_Index).all);
2977 Arg_Index := Arg_Index + 1;
2978 end loop;
2980 -- Set -gnatpg for predefined files (for this purpose the renamings
2981 -- such as Text_IO do not count as predefined). Note that we strip
2982 -- the directory name from the source file name because the call to
2983 -- Fname.Is_Predefined_File_Name cannot deal with directory prefixes.
2985 declare
2986 Fname : constant File_Name_Type := Strip_Directory (S);
2988 begin
2989 if Is_Predefined_File_Name (Fname, False) then
2990 if Check_Readonly_Files then
2991 Comp_Args (Comp_Args'First + 2 .. Comp_Last + 1) :=
2992 Comp_Args (Comp_Args'First + 1 .. Comp_Last);
2993 Comp_Last := Comp_Last + 1;
2994 Comp_Args (Comp_Args'First + 1) := GNAT_Flag;
2996 else
2997 Make_Failed
2998 ("not allowed to compile """ &
2999 Get_Name_String (Fname) &
3000 """; use -a switch, or compile file with " &
3001 """-gnatg"" switch");
3002 end if;
3003 end if;
3004 end;
3006 -- Now check if the file name has one of the suffixes familiar to
3007 -- the gcc driver. If this is not the case then add the ada flag
3008 -- "-x ada".
3010 if not Ada_File_Name (S) and then not Targparm.AAMP_On_Target then
3011 Comp_Last := Comp_Last + 1;
3012 Comp_Args (Comp_Last) := Ada_Flag_1;
3013 Comp_Last := Comp_Last + 1;
3014 Comp_Args (Comp_Last) := Ada_Flag_2;
3015 end if;
3017 if Source_Index /= 0 then
3018 declare
3019 Num : constant String := Source_Index'Img;
3020 begin
3021 Comp_Last := Comp_Last + 1;
3022 Comp_Args (Comp_Last) :=
3023 new String'("-gnateI" & Num (Num'First + 1 .. Num'Last));
3024 end;
3025 end if;
3027 if Source_Index /= 0
3028 or else L /= Strip_Directory (L)
3029 or else Object_Directory_Path /= null
3030 then
3031 -- Build -o argument
3033 Get_Name_String (L);
3035 for J in reverse 1 .. Name_Len loop
3036 if Name_Buffer (J) = '.' then
3037 Name_Len := J + Object_Suffix'Length - 1;
3038 Name_Buffer (J .. Name_Len) := Object_Suffix;
3039 exit;
3040 end if;
3041 end loop;
3043 Comp_Last := Comp_Last + 1;
3044 Comp_Args (Comp_Last) := Output_Flag;
3045 Comp_Last := Comp_Last + 1;
3047 -- If an object directory was specified, prepend the object file
3048 -- name with this object directory.
3050 if Object_Directory_Path /= null then
3051 Comp_Args (Comp_Last) :=
3052 new String'(Object_Directory_Path.all &
3053 Name_Buffer (1 .. Name_Len));
3055 else
3056 Comp_Args (Comp_Last) :=
3057 new String'(Name_Buffer (1 .. Name_Len));
3058 end if;
3059 end if;
3061 if Create_Mapping_File then
3062 Comp_Last := Comp_Last + 1;
3063 Comp_Args (Comp_Last) := Mapping_File_Arg;
3064 end if;
3066 Get_Name_String (S);
3068 Comp_Last := Comp_Last + 1;
3069 Comp_Args (Comp_Last) := new String'(Name_Buffer (1 .. Name_Len));
3071 GNAT.OS_Lib.Normalize_Arguments (Comp_Args (Args'First .. Comp_Last));
3073 Comp_Last := Comp_Last + 1;
3074 Comp_Args (Comp_Last) := new String'("-gnatez");
3076 Display (Gcc.all, Comp_Args (Args'First .. Comp_Last));
3078 if Gcc_Path = null then
3079 Make_Failed ("error, unable to locate ", Gcc.all);
3080 end if;
3082 return
3083 GNAT.OS_Lib.Non_Blocking_Spawn
3084 (Gcc_Path.all, Comp_Args (Args'First .. Comp_Last));
3085 end Compile;
3087 ----------------------
3088 -- Get_Mapping_File --
3089 ----------------------
3091 procedure Get_Mapping_File (Project : Project_Id) is
3092 begin
3093 -- If there is a mapping file ready to be reused, reuse it
3095 if Last_Free_Indices (Project) > 0 then
3096 Mfile := The_Free_Mapping_File_Indices
3097 (Project, Last_Free_Indices (Project));
3098 Last_Free_Indices (Project) := Last_Free_Indices (Project) - 1;
3100 -- Otherwise, create and initialize a new one
3102 else
3103 Init_Mapping_File (Project => Project, File_Index => Mfile);
3104 end if;
3106 -- Put the name in the mapping file argument for the invocation
3107 -- of the compiler.
3109 Free (Mapping_File_Arg);
3110 Mapping_File_Arg :=
3111 new String'("-gnatem=" &
3112 Get_Name_String
3113 (The_Mapping_File_Names (Project, Mfile)));
3115 end Get_Mapping_File;
3117 -----------------------
3118 -- Get_Next_Good_ALI --
3119 -----------------------
3121 function Get_Next_Good_ALI return ALI_Id is
3122 ALI : ALI_Id;
3124 begin
3125 pragma Assert (Good_ALI_Present);
3126 ALI := Good_ALI.Table (Good_ALI.Last);
3127 Good_ALI.Decrement_Last;
3128 return ALI;
3129 end Get_Next_Good_ALI;
3131 ----------------------
3132 -- Good_ALI_Present --
3133 ----------------------
3135 function Good_ALI_Present return Boolean is
3136 begin
3137 return Good_ALI.First <= Good_ALI.Last;
3138 end Good_ALI_Present;
3140 --------------------
3141 -- Record_Failure --
3142 --------------------
3144 procedure Record_Failure
3145 (File : File_Name_Type;
3146 Unit : Unit_Name_Type;
3147 Found : Boolean := True)
3149 begin
3150 Bad_Compilation.Increment_Last;
3151 Bad_Compilation.Table (Bad_Compilation.Last) := (File, Unit, Found);
3152 end Record_Failure;
3154 ---------------------
3155 -- Record_Good_ALI --
3156 ---------------------
3158 procedure Record_Good_ALI (A : ALI_Id) is
3159 begin
3160 Good_ALI.Increment_Last;
3161 Good_ALI.Table (Good_ALI.Last) := A;
3162 end Record_Good_ALI;
3164 -- Start of processing for Compile_Sources
3166 begin
3167 pragma Assert (Args'First = 1);
3169 Outstanding_Compiles := 0;
3170 Running_Compile := new Comp_Data_Arr (1 .. Max_Process);
3172 -- Package and Queue initializations
3174 Good_ALI.Init;
3176 if First_Q_Initialization then
3177 Init_Q;
3178 end if;
3180 if Initialize_ALI_Data then
3181 Initialize_ALI;
3182 Initialize_ALI_Source;
3183 end if;
3185 -- The following two flags affect the behavior of ALI.Set_Source_Table.
3186 -- We set Check_Source_Files to True to ensure that source file
3187 -- time stamps are checked, and we set All_Sources to False to
3188 -- avoid checking the presence of the source files listed in the
3189 -- source dependency section of an ali file (which would be a mistake
3190 -- since the ali file may be obsolete).
3192 Check_Source_Files := True;
3193 All_Sources := False;
3195 -- Only insert in the Q if it is not already done, to avoid simultaneous
3196 -- compilations if -jnnn is used.
3198 if not Is_Marked (Main_Source, Main_Index) then
3199 Insert_Q (Main_Source, Index => Main_Index);
3200 Mark (Main_Source, Main_Index);
3201 end if;
3203 First_Compiled_File := No_File;
3204 Most_Recent_Obj_File := No_File;
3205 Most_Recent_Obj_Stamp := Empty_Time_Stamp;
3206 Main_Unit := False;
3208 -- Keep looping until there is no more work to do (the Q is empty)
3209 -- and all the outstanding compilations have terminated
3211 Make_Loop : while not Empty_Q or else Outstanding_Compiles > 0 loop
3213 -- If the user does not want to keep going in case of errors then
3214 -- wait for the remaining outstanding compiles and then exit.
3216 if Bad_Compilation_Count > 0 and then not Keep_Going then
3217 while Outstanding_Compiles > 0 loop
3218 Await_Compile
3219 (Full_Source_File, Lib_File, Source_Unit, Compilation_OK);
3221 if not Compilation_OK then
3222 Record_Failure (Full_Source_File, Source_Unit);
3223 end if;
3224 end loop;
3226 exit Make_Loop;
3227 end if;
3229 -- PHASE 1: Check if there is more work that we can do (i.e. the Q
3230 -- is non empty). If there is, do it only if we have not yet used
3231 -- up all the available processes.
3233 if not Empty_Q and then Outstanding_Compiles < Max_Process then
3234 declare
3235 Source_Index : Int;
3236 -- Index of the current unit in the current source file
3238 begin
3239 Extract_From_Q (Source_File, Source_Unit, Source_Index);
3240 Full_Source_File := Osint.Full_Source_Name (Source_File);
3241 Lib_File := Osint.Lib_File_Name
3242 (Source_File, Source_Index);
3243 Full_Lib_File := Osint.Full_Lib_File_Name (Lib_File);
3245 -- If this source has already been compiled, the executable is
3246 -- obsolete.
3248 if Is_In_Obsoleted (Source_File) then
3249 Executable_Obsolete := True;
3250 end if;
3252 -- If the library file is an Ada library skip it
3254 if Full_Lib_File /= No_File
3255 and then In_Ada_Lib_Dir (Full_Lib_File)
3256 then
3257 Verbose_Msg
3258 (Lib_File,
3259 "is in an Ada library",
3260 Prefix => " ",
3261 Minimum_Verbosity => Opt.High);
3263 -- If the library file is a read-only library skip it, but
3264 -- only if, when using project files, this library file is
3265 -- in the right object directory (a read-only ALI file
3266 -- in the object directory of a project being extended
3267 -- should not be skipped).
3269 elsif Full_Lib_File /= No_File
3270 and then not Check_Readonly_Files
3271 and then Is_Readonly_Library (Full_Lib_File)
3272 and then Is_In_Object_Directory (Source_File, Full_Lib_File)
3273 then
3274 Verbose_Msg
3275 (Lib_File,
3276 "is a read-only library",
3277 Prefix => " ",
3278 Minimum_Verbosity => Opt.High);
3280 -- The source file that we are checking cannot be located
3282 elsif Full_Source_File = No_File then
3283 Record_Failure (Source_File, Source_Unit, False);
3285 -- Source and library files can be located but are internal
3286 -- files
3288 elsif not Check_Readonly_Files
3289 and then Full_Lib_File /= No_File
3290 and then Is_Internal_File_Name (Source_File, False)
3291 then
3292 if Force_Compilations then
3293 Fail
3294 ("not allowed to compile """ &
3295 Get_Name_String (Source_File) &
3296 """; use -a switch, or compile file with " &
3297 """-gnatg"" switch");
3298 end if;
3300 Verbose_Msg
3301 (Lib_File,
3302 "is an internal library",
3303 Prefix => " ",
3304 Minimum_Verbosity => Opt.High);
3306 -- The source file that we are checking can be located
3308 else
3309 Arguments_Collected := False;
3311 Collect_Arguments (Source_File, Source_Index,
3312 Source_File = Main_Source, Args);
3314 -- Do nothing if project of source is externally built
3316 if Arguments_Project = No_Project
3317 or else not Project_Tree.Projects.Table
3318 (Arguments_Project).Externally_Built
3319 then
3320 -- Don't waste any time if we have to recompile anyway
3322 Obj_Stamp := Empty_Time_Stamp;
3323 Need_To_Compile := Force_Compilations;
3325 if not Force_Compilations then
3326 Read_Only :=
3327 Full_Lib_File /= No_File
3328 and then not Check_Readonly_Files
3329 and then Is_Readonly_Library (Full_Lib_File);
3330 Check (Source_File, Source_Index,
3331 Source_File = Main_Source, Args, Lib_File,
3332 Read_Only, ALI, Obj_File, Obj_Stamp);
3333 Need_To_Compile := (ALI = No_ALI_Id);
3334 end if;
3336 if not Need_To_Compile then
3337 -- The ALI file is up-to-date. Record its Id
3339 Record_Good_ALI (ALI);
3341 -- Record the time stamp of the most recent object
3342 -- file as long as no (re)compilations are needed.
3344 if First_Compiled_File = No_File
3345 and then (Most_Recent_Obj_File = No_File
3346 or else Obj_Stamp > Most_Recent_Obj_Stamp)
3347 then
3348 Most_Recent_Obj_File := Obj_File;
3349 Most_Recent_Obj_Stamp := Obj_Stamp;
3350 end if;
3352 else
3353 -- Check that switch -x has been used if a source
3354 -- outside of project files need to be compiled.
3356 if Main_Project /= No_Project
3357 and then Arguments_Project = No_Project
3358 and then not External_Unit_Compilation_Allowed
3359 then
3360 Make_Failed ("external source (",
3361 Get_Name_String (Source_File),
3362 ") is not part of any project;"
3363 & " cannot be compiled without" &
3364 " gnatmake switch -x");
3365 end if;
3367 -- Is this the first file we have to compile?
3369 if First_Compiled_File = No_File then
3370 First_Compiled_File := Full_Source_File;
3371 Most_Recent_Obj_File := No_File;
3373 if Do_Not_Execute then
3374 exit Make_Loop;
3375 end if;
3376 end if;
3378 if In_Place_Mode then
3380 -- If the library file was not found, then save
3381 -- the library file near the source file.
3383 if Full_Lib_File = No_File then
3384 Lib_File := Osint.Lib_File_Name
3385 (Full_Source_File, Source_Index);
3387 -- If the library file was found, then save the
3388 -- library file in the same place.
3390 else
3391 Lib_File := Full_Lib_File;
3392 end if;
3394 end if;
3396 -- Start the compilation and record it. We can do
3397 -- this because there is at least one free process.
3399 Collect_Arguments_And_Compile
3400 (Source_File, Source_Index);
3402 -- Make sure we could successfully start
3403 -- the Compilation.
3405 if Process_Created then
3406 if Pid = Invalid_Pid then
3407 Record_Failure (Full_Source_File, Source_Unit);
3408 else
3409 Add_Process
3410 (Pid,
3411 Full_Source_File,
3412 Lib_File,
3413 Source_Unit,
3414 Mfile);
3415 end if;
3416 end if;
3417 end if;
3418 end if;
3419 end if;
3420 end;
3421 end if;
3423 -- PHASE 2: Now check if we should wait for a compilation to
3424 -- finish. This is the case if all the available processes are
3425 -- busy compiling sources or there is nothing else to do
3426 -- (that is the Q is empty and there are no good ALIs to process).
3428 if Outstanding_Compiles = Max_Process
3429 or else (Empty_Q
3430 and then not Good_ALI_Present
3431 and then Outstanding_Compiles > 0)
3432 then
3433 Await_Compile
3434 (Full_Source_File, Lib_File, Source_Unit, Compilation_OK);
3436 if not Compilation_OK then
3437 Record_Failure (Full_Source_File, Source_Unit);
3438 end if;
3440 if Compilation_OK or else Keep_Going then
3442 -- Re-read the updated library file
3444 declare
3445 Saved_Object_Consistency : constant Boolean :=
3446 Check_Object_Consistency;
3448 begin
3449 -- If compilation was not OK, or if output is not an
3450 -- object file and we don't do the bind step, don't check
3451 -- for object consistency.
3453 Check_Object_Consistency :=
3454 Check_Object_Consistency
3455 and Compilation_OK
3456 and (Output_Is_Object or Do_Bind_Step);
3457 Text := Read_Library_Info (Lib_File);
3459 -- Restore Check_Object_Consistency to its initial value
3461 Check_Object_Consistency := Saved_Object_Consistency;
3462 end;
3464 -- If an ALI file was generated by this compilation, scan
3465 -- the ALI file and record it.
3466 -- If the scan fails, a previous ali file is inconsistent with
3467 -- the unit just compiled.
3469 if Text /= null then
3470 ALI :=
3471 Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
3473 if ALI = No_ALI_Id then
3475 -- Record a failure only if not already done
3477 if Compilation_OK then
3478 Inform
3479 (Lib_File,
3480 "incompatible ALI file, please recompile");
3481 Record_Failure (Full_Source_File, Source_Unit);
3482 end if;
3483 else
3484 Free (Text);
3485 Record_Good_ALI (ALI);
3486 end if;
3488 -- If we could not read the ALI file that was just generated
3489 -- then there could be a problem reading either the ALI or the
3490 -- corresponding object file (if Check_Object_Consistency is
3491 -- set Read_Library_Info checks that the time stamp of the
3492 -- object file is more recent than that of the ALI). However,
3493 -- we record a failure only if not already done.
3495 else
3496 if Compilation_OK and not Syntax_Only then
3497 Inform
3498 (Lib_File,
3499 "WARNING: ALI or object file not found after compile");
3500 Record_Failure (Full_Source_File, Source_Unit);
3501 end if;
3502 end if;
3503 end if;
3504 end if;
3506 -- PHASE 3: Check if we recorded good ALI files. If yes process
3507 -- them now in the order in which they have been recorded. There
3508 -- are two occasions in which we record good ali files. The first is
3509 -- in phase 1 when, after scanning an existing ALI file we realize
3510 -- it is up-to-date, the second instance is after a successful
3511 -- compilation.
3513 while Good_ALI_Present loop
3514 ALI := Get_Next_Good_ALI;
3516 declare
3517 Source_Index : Int := Unit_Index_Of (ALIs.Table (ALI).Afile);
3519 begin
3520 -- If we are processing the library file corresponding to the
3521 -- main source file check if this source can be a main unit.
3523 if ALIs.Table (ALI).Sfile = Main_Source and then
3524 Source_Index = Main_Index
3525 then
3526 Main_Unit := ALIs.Table (ALI).Main_Program /= None;
3527 end if;
3529 -- The following adds the standard library (s-stalib) to the
3530 -- list of files to be handled by gnatmake: this file and any
3531 -- files it depends on are always included in every bind,
3532 -- even if they are not in the explicit dependency list.
3533 -- Of course, it is not added if Suppress_Standard_Library
3534 -- is True.
3536 -- However, to avoid annoying output about s-stalib.ali being
3537 -- read only, when "-v" is used, we add the standard library
3538 -- only when "-a" is used.
3540 if Need_To_Check_Standard_Library then
3541 Check_Standard_Library;
3542 end if;
3544 -- Now insert in the Q the unmarked source files (i.e. those
3545 -- which have never been inserted in the Q and hence never
3546 -- considered). Only do that if Unique_Compile is False.
3548 if not Unique_Compile then
3549 for J in
3550 ALIs.Table (ALI).First_Unit .. ALIs.Table (ALI).Last_Unit
3551 loop
3552 for K in
3553 Units.Table (J).First_With .. Units.Table (J).Last_With
3554 loop
3555 Sfile := Withs.Table (K).Sfile;
3556 Uname := Withs.Table (K).Uname;
3558 -- If project files are used, find the proper source
3559 -- to compile, in case Sfile is the spec, but there
3560 -- is a body.
3562 if Main_Project /= No_Project then
3563 declare
3564 Unit_Name : Name_Id;
3565 Uid : Prj.Unit_Index;
3566 Udata : Unit_Data;
3568 begin
3569 Get_Name_String (Uname);
3570 Name_Len := Name_Len - 2;
3571 Unit_Name := Name_Find;
3572 Uid :=
3573 Units_Htable.Get
3574 (Project_Tree.Units_HT, Unit_Name);
3576 if Uid /= Prj.No_Unit_Index then
3577 Udata := Project_Tree.Units.Table (Uid);
3580 Udata.File_Names (Body_Part).Name /=
3581 No_File
3582 and then
3583 Udata.File_Names (Body_Part).Path.Name /=
3584 Slash
3585 then
3586 Sfile := Udata.File_Names (Body_Part).Name;
3587 Source_Index :=
3588 Udata.File_Names (Body_Part).Index;
3590 elsif
3591 Udata.File_Names (Specification).Name /=
3592 No_File
3593 and then
3594 Udata.File_Names
3595 (Specification).Path.Name /= Slash
3596 then
3597 Sfile :=
3598 Udata.File_Names (Specification).Name;
3599 Source_Index :=
3600 Udata.File_Names (Specification).Index;
3601 end if;
3602 end if;
3603 end;
3604 end if;
3606 Dependencies.Append ((ALIs.Table (ALI).Sfile, Sfile));
3608 if Is_In_Obsoleted (Sfile) then
3609 Executable_Obsolete := True;
3610 end if;
3612 if Sfile = No_File then
3613 Debug_Msg
3614 ("Skipping generic:", Withs.Table (K).Uname);
3616 else
3617 Source_Index :=
3618 Unit_Index_Of (Withs.Table (K).Afile);
3620 if Is_Marked (Sfile, Source_Index) then
3621 Debug_Msg ("Skipping marked file:", Sfile);
3623 elsif not Check_Readonly_Files
3624 and then Is_Internal_File_Name (Sfile, False)
3625 then
3626 Debug_Msg ("Skipping internal file:", Sfile);
3628 else
3629 Insert_Q
3630 (Sfile, Withs.Table (K).Uname, Source_Index);
3631 Mark (Sfile, Source_Index);
3632 end if;
3633 end if;
3634 end loop;
3635 end loop;
3636 end if;
3637 end;
3638 end loop;
3640 if Display_Compilation_Progress then
3641 Write_Str ("completed ");
3642 Write_Int (Int (Q_Front));
3643 Write_Str (" out of ");
3644 Write_Int (Int (Q.Last));
3645 Write_Str (" (");
3646 Write_Int (Int ((Q_Front * 100) / (Q.Last - Q.First)));
3647 Write_Str ("%)...");
3648 Write_Eol;
3649 end if;
3650 end loop Make_Loop;
3652 Compilation_Failures := Bad_Compilation_Count;
3654 -- Compilation is finished
3656 -- Delete any temporary configuration pragma file
3658 if not Debug.Debug_Flag_N then
3659 Delete_Temp_Config_Files;
3660 end if;
3661 end Compile_Sources;
3663 -----------------------------------
3664 -- Compute_All_Imported_Projects --
3665 -----------------------------------
3667 procedure Compute_All_Imported_Projects (Project : Project_Id) is
3668 procedure Add_To_List (Prj : Project_Id);
3669 -- Add a project to the list All_Imported_Projects of project Project
3671 procedure Recursive_Add_Imported (Project : Project_Id);
3672 -- Recursively add the projects imported by project Project, but not
3673 -- those that are extended.
3675 -----------------
3676 -- Add_To_List --
3677 -----------------
3679 procedure Add_To_List (Prj : Project_Id) is
3680 Element : constant Project_Element :=
3681 (Prj, Project_Tree.Projects.Table (Project).All_Imported_Projects);
3682 List : Project_List;
3683 begin
3684 Project_List_Table.Increment_Last (Project_Tree.Project_Lists);
3685 List := Project_List_Table.Last (Project_Tree.Project_Lists);
3686 Project_Tree.Project_Lists.Table (List) := Element;
3687 Project_Tree.Projects.Table (Project).All_Imported_Projects := List;
3688 end Add_To_List;
3690 ----------------------------
3691 -- Recursive_Add_Imported --
3692 ----------------------------
3694 procedure Recursive_Add_Imported (Project : Project_Id) is
3695 List : Project_List;
3696 Element : Project_Element;
3697 Prj : Project_Id;
3699 begin
3700 if Project /= No_Project then
3702 -- For all the imported projects
3704 List := Project_Tree.Projects.Table (Project).Imported_Projects;
3705 while List /= Empty_Project_List loop
3706 Element := Project_Tree.Project_Lists.Table (List);
3707 Prj := Element.Project;
3709 -- Get the ultimate extending project
3711 while
3712 Project_Tree.Projects.Table (Prj).Extended_By /= No_Project
3713 loop
3714 Prj := Project_Tree.Projects.Table (Prj).Extended_By;
3715 end loop;
3717 -- If project has not yet been visited, add to list and recurse
3719 if not Project_Tree.Projects.Table (Prj).Seen then
3720 Project_Tree.Projects.Table (Prj).Seen := True;
3721 Add_To_List (Prj);
3722 Recursive_Add_Imported (Prj);
3723 end if;
3725 List := Element.Next;
3726 end loop;
3728 -- Recurse on projects being imported, if any
3730 Recursive_Add_Imported
3731 (Project_Tree.Projects.Table (Project).Extends);
3732 end if;
3733 end Recursive_Add_Imported;
3735 begin
3736 -- Reset the Seen flag for all projects
3738 for Index in 1 .. Project_Table.Last (Project_Tree.Projects) loop
3739 Project_Tree.Projects.Table (Index).Seen := False;
3740 end loop;
3742 -- Make sure the list is empty
3744 Project_Tree.Projects.Table (Project).All_Imported_Projects :=
3745 Empty_Project_List;
3747 -- Add to the list all projects imported directly or indirectly
3749 Recursive_Add_Imported (Project);
3750 end Compute_All_Imported_Projects;
3752 ----------------------------------
3753 -- Configuration_Pragmas_Switch --
3754 ----------------------------------
3756 function Configuration_Pragmas_Switch
3757 (For_Project : Project_Id) return Argument_List
3759 The_Packages : Package_Id;
3760 Gnatmake : Package_Id;
3761 Compiler : Package_Id;
3763 Global_Attribute : Variable_Value := Nil_Variable_Value;
3764 Local_Attribute : Variable_Value := Nil_Variable_Value;
3766 Global_Attribute_Present : Boolean := False;
3767 Local_Attribute_Present : Boolean := False;
3769 Result : Argument_List (1 .. 3);
3770 Last : Natural := 0;
3772 function Absolute_Path
3773 (Path : Path_Name_Type;
3774 Project : Project_Id) return String;
3775 -- Returns an absolute path for a configuration pragmas file
3777 -------------------
3778 -- Absolute_Path --
3779 -------------------
3781 function Absolute_Path
3782 (Path : Path_Name_Type;
3783 Project : Project_Id) return String
3785 begin
3786 Get_Name_String (Path);
3788 declare
3789 Path_Name : constant String := Name_Buffer (1 .. Name_Len);
3791 begin
3792 if Is_Absolute_Path (Path_Name) then
3793 return Path_Name;
3795 else
3796 declare
3797 Parent_Directory : constant String :=
3798 Get_Name_String
3799 (Project_Tree.Projects.Table
3800 (Project).Directory.Display_Name);
3802 begin
3803 if Parent_Directory (Parent_Directory'Last) =
3804 Directory_Separator
3805 then
3806 return Parent_Directory & Path_Name;
3808 else
3809 return Parent_Directory & Directory_Separator & Path_Name;
3810 end if;
3811 end;
3812 end if;
3813 end;
3814 end Absolute_Path;
3816 -- Start of processing for Configuration_Pragmas_Switch
3818 begin
3819 Prj.Env.Create_Config_Pragmas_File
3820 (For_Project, Main_Project, Project_Tree);
3822 if Project_Tree.Projects.Table
3823 (For_Project).Config_File_Name /= No_Path
3824 then
3825 Temporary_Config_File :=
3826 Project_Tree.Projects.Table (For_Project).Config_File_Temp;
3827 Last := 1;
3828 Result (1) :=
3829 new String'
3830 ("-gnatec=" &
3831 Get_Name_String
3832 (Project_Tree.Projects.Table
3833 (For_Project).Config_File_Name));
3835 else
3836 Temporary_Config_File := False;
3837 end if;
3839 -- Check for attribute Builder'Global_Configuration_Pragmas
3841 The_Packages := Project_Tree.Projects.Table
3842 (Main_Project).Decl.Packages;
3843 Gnatmake :=
3844 Prj.Util.Value_Of
3845 (Name => Name_Builder,
3846 In_Packages => The_Packages,
3847 In_Tree => Project_Tree);
3849 if Gnatmake /= No_Package then
3850 Global_Attribute := Prj.Util.Value_Of
3851 (Variable_Name => Name_Global_Configuration_Pragmas,
3852 In_Variables => Project_Tree.Packages.Table
3853 (Gnatmake).Decl.Attributes,
3854 In_Tree => Project_Tree);
3855 Global_Attribute_Present :=
3856 Global_Attribute /= Nil_Variable_Value
3857 and then Get_Name_String (Global_Attribute.Value) /= "";
3859 if Global_Attribute_Present then
3860 declare
3861 Path : constant String :=
3862 Absolute_Path
3863 (Path_Name_Type (Global_Attribute.Value),
3864 Global_Attribute.Project);
3865 begin
3866 if not Is_Regular_File (Path) then
3867 if Debug.Debug_Flag_F then
3868 Make_Failed
3869 ("cannot find configuration pragmas file ",
3870 File_Name (Path));
3871 else
3872 Make_Failed
3873 ("cannot find configuration pragmas file ",
3874 Path);
3875 end if;
3876 end if;
3878 Last := Last + 1;
3879 Result (Last) := new String'("-gnatec=" & Path);
3880 end;
3881 end if;
3882 end if;
3884 -- Check for attribute Compiler'Local_Configuration_Pragmas
3886 The_Packages :=
3887 Project_Tree.Projects.Table (For_Project).Decl.Packages;
3888 Compiler :=
3889 Prj.Util.Value_Of
3890 (Name => Name_Compiler,
3891 In_Packages => The_Packages,
3892 In_Tree => Project_Tree);
3894 if Compiler /= No_Package then
3895 Local_Attribute := Prj.Util.Value_Of
3896 (Variable_Name => Name_Local_Configuration_Pragmas,
3897 In_Variables => Project_Tree.Packages.Table
3898 (Compiler).Decl.Attributes,
3899 In_Tree => Project_Tree);
3900 Local_Attribute_Present :=
3901 Local_Attribute /= Nil_Variable_Value
3902 and then Get_Name_String (Local_Attribute.Value) /= "";
3904 if Local_Attribute_Present then
3905 declare
3906 Path : constant String :=
3907 Absolute_Path
3908 (Path_Name_Type (Local_Attribute.Value),
3909 Local_Attribute.Project);
3910 begin
3911 if not Is_Regular_File (Path) then
3912 if Debug.Debug_Flag_F then
3913 Make_Failed
3914 ("cannot find configuration pragmas file ",
3915 File_Name (Path));
3917 else
3918 Make_Failed
3919 ("cannot find configuration pragmas file ", Path);
3920 end if;
3921 end if;
3923 Last := Last + 1;
3924 Result (Last) := new String'("-gnatec=" & Path);
3925 end;
3926 end if;
3927 end if;
3929 return Result (1 .. Last);
3930 end Configuration_Pragmas_Switch;
3932 ---------------
3933 -- Debug_Msg --
3934 ---------------
3936 procedure Debug_Msg (S : String; N : Name_Id) is
3937 begin
3938 if Debug.Debug_Flag_W then
3939 Write_Str (" ... ");
3940 Write_Str (S);
3941 Write_Str (" ");
3942 Write_Name (N);
3943 Write_Eol;
3944 end if;
3945 end Debug_Msg;
3947 procedure Debug_Msg (S : String; N : File_Name_Type) is
3948 begin
3949 Debug_Msg (S, Name_Id (N));
3950 end Debug_Msg;
3952 procedure Debug_Msg (S : String; N : Unit_Name_Type) is
3953 begin
3954 Debug_Msg (S, Name_Id (N));
3955 end Debug_Msg;
3957 ---------------------------
3958 -- Delete_All_Temp_Files --
3959 ---------------------------
3961 procedure Delete_All_Temp_Files is
3962 begin
3963 if not Debug.Debug_Flag_N then
3964 Delete_Mapping_Files;
3965 Delete_Temp_Config_Files;
3966 Prj.Env.Delete_All_Path_Files (Project_Tree);
3967 end if;
3968 end Delete_All_Temp_Files;
3970 --------------------------
3971 -- Delete_Mapping_Files --
3972 --------------------------
3974 procedure Delete_Mapping_Files is
3975 Success : Boolean;
3976 pragma Warnings (Off, Success);
3978 begin
3979 -- The caller is responsible for ensuring that Debug_Flag_N is False
3981 pragma Assert (not Debug.Debug_Flag_N);
3983 if The_Mapping_File_Names /= null then
3984 for Project in The_Mapping_File_Names'Range (1) loop
3985 for Index in 1 .. Last_Mapping_File_Names (Project) loop
3986 Delete_File
3987 (Name => Get_Name_String
3988 (The_Mapping_File_Names (Project, Index)),
3989 Success => Success);
3990 end loop;
3991 end loop;
3992 end if;
3993 end Delete_Mapping_Files;
3995 ------------------------------
3996 -- Delete_Temp_Config_Files --
3997 ------------------------------
3999 procedure Delete_Temp_Config_Files is
4000 Success : Boolean;
4001 pragma Warnings (Off, Success);
4003 begin
4004 -- The caller is responsible for ensuring that Debug_Flag_N is False
4006 pragma Assert (not Debug.Debug_Flag_N);
4008 if Main_Project /= No_Project then
4009 for Project in Project_Table.First ..
4010 Project_Table.Last (Project_Tree.Projects)
4011 loop
4013 Project_Tree.Projects.Table (Project).Config_File_Temp
4014 then
4015 if Verbose_Mode then
4016 Write_Str ("Deleting temp configuration file """);
4017 Write_Str (Get_Name_String
4018 (Project_Tree.Projects.Table
4019 (Project).Config_File_Name));
4020 Write_Line ("""");
4021 end if;
4023 Delete_File
4024 (Name => Get_Name_String
4025 (Project_Tree.Projects.Table
4026 (Project).Config_File_Name),
4027 Success => Success);
4029 -- Make sure that we don't have a config file for this project,
4030 -- in case there are several mains. In this case, we will
4031 -- recreate another config file: we cannot reuse the one that
4032 -- we just deleted!
4034 Project_Tree.Projects.Table (Project).
4035 Config_Checked := False;
4036 Project_Tree.Projects.Table (Project).
4037 Config_File_Name := No_Path;
4038 Project_Tree.Projects.Table (Project).
4039 Config_File_Temp := False;
4040 end if;
4041 end loop;
4042 end if;
4043 end Delete_Temp_Config_Files;
4045 -------------
4046 -- Display --
4047 -------------
4049 procedure Display (Program : String; Args : Argument_List) is
4050 begin
4051 pragma Assert (Args'First = 1);
4053 if Display_Executed_Programs then
4054 Write_Str (Program);
4056 for J in Args'Range loop
4058 -- Never display -gnatez
4060 if Args (J).all /= "-gnatez" then
4062 -- Do not display the mapping file argument automatically
4063 -- created when using a project file.
4065 if Main_Project = No_Project
4066 or else Debug.Debug_Flag_N
4067 or else Args (J)'Length < 8
4068 or else
4069 Args (J) (Args (J)'First .. Args (J)'First + 6) /= "-gnatem"
4070 then
4071 -- When -dn is not specified, do not display the config
4072 -- pragmas switch (-gnatec) for the temporary file created
4073 -- by the project manager (always the first -gnatec switch).
4074 -- Reset Temporary_Config_File to False so that the eventual
4075 -- other -gnatec switches will be displayed.
4077 if (not Debug.Debug_Flag_N)
4078 and then Temporary_Config_File
4079 and then Args (J)'Length > 7
4080 and then Args (J) (Args (J)'First .. Args (J)'First + 6)
4081 = "-gnatec"
4082 then
4083 Temporary_Config_File := False;
4085 -- Do not display the -F=mapping_file switch for
4086 -- gnatbind, if -dn is not specified.
4088 elsif Debug.Debug_Flag_N
4089 or else Args (J)'Length < 4
4090 or else
4091 Args (J) (Args (J)'First .. Args (J)'First + 2) /= "-F="
4092 then
4093 Write_Str (" ");
4095 -- If -df is used, only display file names, not path
4096 -- names.
4098 if Debug.Debug_Flag_F then
4099 declare
4100 Equal_Pos : Natural;
4101 begin
4102 Equal_Pos := Args (J)'First - 1;
4103 for K in Args (J)'Range loop
4104 if Args (J) (K) = '=' then
4105 Equal_Pos := K;
4106 exit;
4107 end if;
4108 end loop;
4110 if Is_Absolute_Path
4111 (Args (J) (Equal_Pos + 1 .. Args (J)'Last))
4112 then
4113 Write_Str
4114 (Args (J) (Args (J)'First .. Equal_Pos));
4115 Write_Str
4116 (File_Name
4117 (Args (J)
4118 (Equal_Pos + 1 .. Args (J)'Last)));
4120 else
4121 Write_Str (Args (J).all);
4122 end if;
4123 end;
4125 else
4126 Write_Str (Args (J).all);
4127 end if;
4128 end if;
4129 end if;
4130 end if;
4131 end loop;
4133 Write_Eol;
4134 end if;
4135 end Display;
4137 ----------------------
4138 -- Display_Commands --
4139 ----------------------
4141 procedure Display_Commands (Display : Boolean := True) is
4142 begin
4143 Display_Executed_Programs := Display;
4144 end Display_Commands;
4146 -------------
4147 -- Empty_Q --
4148 -------------
4150 function Empty_Q return Boolean is
4151 begin
4152 if Debug.Debug_Flag_P then
4153 Write_Str (" Q := [");
4155 for J in Q_Front .. Q.Last - 1 loop
4156 Write_Str (" ");
4157 Write_Name (Q.Table (J).File);
4158 Write_Eol;
4159 Write_Str (" ");
4160 end loop;
4162 Write_Str ("]");
4163 Write_Eol;
4164 end if;
4166 return Q_Front >= Q.Last;
4167 end Empty_Q;
4169 --------------------------
4170 -- Enter_Into_Obsoleted --
4171 --------------------------
4173 procedure Enter_Into_Obsoleted (F : File_Name_Type) is
4174 Name : constant String := Get_Name_String (F);
4175 First : Natural;
4176 F2 : File_Name_Type;
4178 begin
4179 First := Name'Last;
4180 while First > Name'First
4181 and then Name (First - 1) /= Directory_Separator
4182 and then Name (First - 1) /= '/'
4183 loop
4184 First := First - 1;
4185 end loop;
4187 if First /= Name'First then
4188 Name_Len := 0;
4189 Add_Str_To_Name_Buffer (Name (First .. Name'Last));
4190 F2 := Name_Find;
4192 else
4193 F2 := F;
4194 end if;
4196 Debug_Msg ("New entry in Obsoleted table:", F2);
4197 Obsoleted.Set (F2, True);
4198 end Enter_Into_Obsoleted;
4200 --------------------
4201 -- Extract_From_Q --
4202 --------------------
4204 procedure Extract_From_Q
4205 (Source_File : out File_Name_Type;
4206 Source_Unit : out Unit_Name_Type;
4207 Source_Index : out Int)
4209 File : constant File_Name_Type := Q.Table (Q_Front).File;
4210 Unit : constant Unit_Name_Type := Q.Table (Q_Front).Unit;
4211 Index : constant Int := Q.Table (Q_Front).Index;
4213 begin
4214 if Debug.Debug_Flag_Q then
4215 Write_Str (" Q := Q - [ ");
4216 Write_Name (File);
4218 if Index /= 0 then
4219 Write_Str (", ");
4220 Write_Int (Index);
4221 end if;
4223 Write_Str (" ]");
4224 Write_Eol;
4225 end if;
4227 Q_Front := Q_Front + 1;
4228 Source_File := File;
4229 Source_Unit := Unit;
4230 Source_Index := Index;
4231 end Extract_From_Q;
4233 --------------
4234 -- Gnatmake --
4235 --------------
4237 procedure Gnatmake is
4238 Main_Source_File : File_Name_Type;
4239 -- The source file containing the main compilation unit
4241 Compilation_Failures : Natural;
4243 Total_Compilation_Failures : Natural := 0;
4245 Is_Main_Unit : Boolean;
4246 -- Set to True by Compile_Sources if the Main_Source_File can be a
4247 -- main unit.
4249 Main_ALI_File : File_Name_Type;
4250 -- The ali file corresponding to Main_Source_File
4252 Executable : File_Name_Type := No_File;
4253 -- The file name of an executable
4255 Non_Std_Executable : Boolean := False;
4256 -- Non_Std_Executable is set to True when there is a possibility
4257 -- that the linker will not choose the correct executable file name.
4259 Current_Work_Dir : constant String_Access :=
4260 new String'(Get_Current_Dir);
4261 -- The current working directory, used to modify some relative path
4262 -- switches on the command line when a project file is used.
4264 Current_Main_Index : Int := 0;
4265 -- If not zero, the index of the current main unit in its source file
4267 There_Are_Stand_Alone_Libraries : Boolean := False;
4268 -- Set to True when there are Stand-Alone Libraries, so that gnatbind
4269 -- is invoked with the -F switch to force checking of elaboration flags.
4271 Mapping_Path : Path_Name_Type := No_Path;
4272 -- The path name of the mapping file
4274 Discard : Boolean;
4275 pragma Warnings (Off, Discard);
4277 procedure Check_Mains;
4278 -- Check that the main subprograms do exist and that they all
4279 -- belong to the same project file.
4281 procedure Create_Binder_Mapping_File
4282 (Args : in out Argument_List; Last_Arg : in out Natural);
4283 -- Create a binder mapping file and add the necessary switch
4285 -----------------
4286 -- Check_Mains --
4287 -----------------
4289 procedure Check_Mains is
4290 Real_Main_Project : Project_Id := No_Project;
4291 -- The project of the first main
4293 Proj : Project_Id := No_Project;
4294 -- The project of the current main
4296 Data : Project_Data;
4298 Real_Path : String_Access;
4300 begin
4301 Mains.Reset;
4303 -- Check each main
4305 loop
4306 declare
4307 Main : constant String := Mains.Next_Main;
4308 -- The name specified on the command line may include
4309 -- directory information.
4311 File_Name : constant String := Base_Name (Main);
4312 -- The simple file name of the current main main
4314 begin
4315 exit when Main = "";
4317 -- Get the project of the current main
4319 Proj := Prj.Env.Project_Of
4320 (File_Name, Main_Project, Project_Tree);
4322 -- Fail if the current main is not a source of a
4323 -- project.
4325 if Proj = No_Project then
4326 Make_Failed
4327 ("""" & Main &
4328 """ is not a source of any project");
4330 else
4331 -- If there is directory information, check that
4332 -- the source exists and, if it does, that the path
4333 -- is the actual path of a source of a project.
4335 if Main /= File_Name then
4336 Data :=
4337 Project_Tree.Projects.Table (Main_Project);
4339 Real_Path :=
4340 Locate_Regular_File
4341 (Main &
4342 Body_Suffix_Of (Project_Tree, "ada", Data.Naming),
4343 "");
4344 if Real_Path = null then
4345 Real_Path :=
4346 Locate_Regular_File
4347 (Main &
4348 Spec_Suffix_Of (Project_Tree, "ada", Data.Naming),
4349 "");
4350 end if;
4352 if Real_Path = null then
4353 Real_Path :=
4354 Locate_Regular_File (Main, "");
4355 end if;
4357 -- Fail if the file cannot be found
4359 if Real_Path = null then
4360 Make_Failed
4361 ("file """ & Main & """ does not exist");
4362 end if;
4364 declare
4365 Project_Path : constant String :=
4366 Prj.Env.File_Name_Of_Library_Unit_Body
4367 (Name => File_Name,
4368 Project => Main_Project,
4369 In_Tree => Project_Tree,
4370 Main_Project_Only => False,
4371 Full_Path => True);
4372 Normed_Path : constant String :=
4373 Normalize_Pathname
4374 (Real_Path.all,
4375 Case_Sensitive => False);
4376 Proj_Path : constant String :=
4377 Normalize_Pathname
4378 (Project_Path,
4379 Case_Sensitive => False);
4381 begin
4382 Free (Real_Path);
4384 -- Fail if it is not the correct path
4386 if Normed_Path /= Proj_Path then
4387 if Verbose_Mode then
4388 Set_Standard_Error;
4389 Write_Str (Normed_Path);
4390 Write_Str (" /= ");
4391 Write_Line (Proj_Path);
4392 end if;
4394 Make_Failed
4395 ("""" & Main &
4396 """ is not a source of any project");
4397 end if;
4398 end;
4399 end if;
4401 if not Unique_Compile then
4403 -- Record the project, if it is the first main
4405 if Real_Main_Project = No_Project then
4406 Real_Main_Project := Proj;
4408 elsif Proj /= Real_Main_Project then
4410 -- Fail, as the current main is not a source
4411 -- of the same project as the first main.
4413 Make_Failed
4414 ("""" & Main &
4415 """ is not a source of project " &
4416 Get_Name_String
4417 (Project_Tree.Projects.Table
4418 (Real_Main_Project).Name));
4419 end if;
4420 end if;
4421 end if;
4423 -- If -u and -U are not used, we may have mains that
4424 -- are sources of a project that is not the one
4425 -- specified with switch -P.
4427 if not Unique_Compile then
4428 Main_Project := Real_Main_Project;
4429 end if;
4430 end;
4431 end loop;
4432 end Check_Mains;
4434 --------------------------------
4435 -- Create_Binder_Mapping_File --
4436 --------------------------------
4438 procedure Create_Binder_Mapping_File
4439 (Args : in out Argument_List; Last_Arg : in out Natural)
4441 Mapping_FD : File_Descriptor := Invalid_FD;
4442 -- A File Descriptor for an eventual mapping file
4444 ALI_Unit : Unit_Name_Type := No_Unit_Name;
4445 -- The unit name of an ALI file
4447 ALI_Name : File_Name_Type := No_File;
4448 -- The file name of the ALI file
4450 ALI_Project : Project_Id := No_Project;
4451 -- The project of the ALI file
4453 Bytes : Integer;
4454 OK : Boolean := True;
4456 Status : Boolean;
4457 -- For call to Close
4459 begin
4460 Tempdir.Create_Temp_File (Mapping_FD, Mapping_Path);
4461 Record_Temp_File (Mapping_Path);
4463 if Mapping_FD /= Invalid_FD then
4465 -- Traverse all units
4467 for J in Unit_Table.First ..
4468 Unit_Table.Last (Project_Tree.Units)
4469 loop
4470 declare
4471 Unit : constant Unit_Data := Project_Tree.Units.Table (J);
4472 begin
4473 if Unit.Name /= No_Name then
4475 -- If there is a body, put it in the mapping
4477 if Unit.File_Names (Body_Part).Name /= No_File
4478 and then Unit.File_Names (Body_Part).Project /=
4479 No_Project
4480 then
4481 Get_Name_String (Unit.Name);
4482 Name_Buffer (Name_Len + 1 .. Name_Len + 2) := "%b";
4483 Name_Len := Name_Len + 2;
4484 ALI_Unit := Name_Find;
4485 ALI_Name :=
4486 Lib_File_Name
4487 (Unit.File_Names (Body_Part).Display_Name);
4488 ALI_Project :=
4489 Unit.File_Names (Body_Part).Project;
4491 -- Otherwise, if there is a spec, put it
4492 -- in the mapping.
4494 elsif Unit.File_Names (Specification).Name /= No_File
4495 and then Unit.File_Names (Specification).Project /=
4496 No_Project
4497 then
4498 Get_Name_String (Unit.Name);
4499 Name_Buffer (Name_Len + 1 .. Name_Len + 2) := "%s";
4500 Name_Len := Name_Len + 2;
4501 ALI_Unit := Name_Find;
4502 ALI_Name :=
4503 Lib_File_Name
4504 (Unit.File_Names (Specification).Display_Name);
4505 ALI_Project :=
4506 Unit.File_Names (Specification).Project;
4508 else
4509 ALI_Name := No_File;
4510 end if;
4512 -- If we have something to put in the mapping
4513 -- then we do it now. However, if the project
4514 -- is extended, we don't put anything in the
4515 -- mapping file, because we do not know where
4516 -- the ALI file is: it might be in the ext-
4517 -- ended project obj dir as well as in the
4518 -- extending project obj dir.
4520 if ALI_Name /= No_File
4521 and then
4522 Project_Tree.Projects.Table
4523 (ALI_Project).Extended_By = No_Project
4524 and then
4525 Project_Tree.Projects.Table
4526 (ALI_Project).Extends = No_Project
4527 then
4528 -- First check if the ALI file exists. If it does not,
4529 -- do not put the unit in the mapping file.
4531 declare
4532 ALI : constant String :=
4533 Get_Name_String (ALI_Name);
4534 PD : Project_Data renames
4535 Project_Tree.Projects.Table (ALI_Project);
4537 begin
4538 -- For library projects, use the library directory,
4539 -- for other projects, use the object directory.
4541 if PD.Library then
4542 Get_Name_String (PD.Library_Dir.Name);
4543 else
4544 Get_Name_String (PD.Object_Directory.Name);
4545 end if;
4547 if Name_Buffer (Name_Len) /=
4548 Directory_Separator
4549 then
4550 Name_Len := Name_Len + 1;
4551 Name_Buffer (Name_Len) :=
4552 Directory_Separator;
4553 end if;
4555 Name_Buffer
4556 (Name_Len + 1 ..
4557 Name_Len + ALI'Length) := ALI;
4558 Name_Len :=
4559 Name_Len + ALI'Length + 1;
4560 Name_Buffer (Name_Len) := ASCII.LF;
4562 declare
4563 ALI_Path_Name : constant String :=
4564 Name_Buffer (1 .. Name_Len);
4566 begin
4567 if Is_Regular_File
4568 (ALI_Path_Name (1 .. ALI_Path_Name'Last - 1))
4569 then
4571 -- First line is the unit name
4573 Get_Name_String (ALI_Unit);
4574 Name_Len := Name_Len + 1;
4575 Name_Buffer (Name_Len) := ASCII.LF;
4576 Bytes :=
4577 Write
4578 (Mapping_FD,
4579 Name_Buffer (1)'Address,
4580 Name_Len);
4581 OK := Bytes = Name_Len;
4583 exit when not OK;
4585 -- Second line it the ALI file name
4587 Get_Name_String (ALI_Name);
4588 Name_Len := Name_Len + 1;
4589 Name_Buffer (Name_Len) := ASCII.LF;
4590 Bytes :=
4591 Write
4592 (Mapping_FD,
4593 Name_Buffer (1)'Address,
4594 Name_Len);
4595 OK := Bytes = Name_Len;
4597 exit when not OK;
4599 -- Third line it the ALI path name
4601 Bytes :=
4602 Write
4603 (Mapping_FD,
4604 ALI_Path_Name (1)'Address,
4605 ALI_Path_Name'Length);
4606 OK := Bytes = ALI_Path_Name'Length;
4608 -- If OK is False, it means we were unable
4609 -- to write a line. No point in continuing
4610 -- with the other units.
4612 exit when not OK;
4613 end if;
4614 end;
4615 end;
4616 end if;
4617 end if;
4618 end;
4619 end loop;
4621 Close (Mapping_FD, Status);
4623 OK := OK and Status;
4625 -- If the creation of the mapping file was successful,
4626 -- we add the switch to the arguments of gnatbind.
4628 if OK then
4629 Last_Arg := Last_Arg + 1;
4630 Args (Last_Arg) :=
4631 new String'("-F=" & Get_Name_String (Mapping_Path));
4632 end if;
4633 end if;
4634 end Create_Binder_Mapping_File;
4636 -- Start of processing for Gnatmake
4638 -- This body is very long, should be broken down ???
4640 begin
4641 Install_Int_Handler (Sigint_Intercepted'Access);
4643 Do_Compile_Step := True;
4644 Do_Bind_Step := True;
4645 Do_Link_Step := True;
4647 Obsoleted.Reset;
4649 Make.Initialize;
4651 Bind_Shared := No_Shared_Switch'Access;
4652 Link_With_Shared_Libgcc := No_Shared_Libgcc_Switch'Access;
4654 Failed_Links.Set_Last (0);
4655 Successful_Links.Set_Last (0);
4657 -- Special case when switch -B was specified
4659 if Build_Bind_And_Link_Full_Project then
4661 -- When switch -B is specified, there must be a project file
4663 if Main_Project = No_Project then
4664 Make_Failed ("-B cannot be used without a project file");
4666 -- No main program may be specified on the command line
4668 elsif Osint.Number_Of_Files /= 0 then
4669 Make_Failed ("-B cannot be used with a main specified on " &
4670 "the command line");
4672 -- And the project file cannot be a library project file
4674 elsif Project_Tree.Projects.Table (Main_Project).Library then
4675 Make_Failed ("-B cannot be used for a library project file");
4677 else
4678 No_Main_Subprogram := True;
4679 Insert_Project_Sources
4680 (The_Project => Main_Project,
4681 All_Projects => Unique_Compile_All_Projects,
4682 Into_Q => False);
4684 -- If there are no sources to compile, we fail
4686 if Osint.Number_Of_Files = 0 then
4687 Make_Failed ("no sources to compile");
4688 end if;
4690 -- Specify -n for gnatbind and add the ALI files of all the
4691 -- sources, except the one which is a fake main subprogram:
4692 -- this is the one for the binder generated file and it will be
4693 -- transmitted to gnatlink. These sources are those that are
4694 -- in the queue.
4696 Add_Switch ("-n", Binder, And_Save => True);
4698 for J in Q.First .. Q.Last - 1 loop
4699 Add_Switch
4700 (Get_Name_String
4701 (Lib_File_Name (Q.Table (J).File)),
4702 Binder, And_Save => True);
4703 end loop;
4704 end if;
4706 elsif Main_Index /= 0 and then Osint.Number_Of_Files > 1 then
4707 Make_Failed ("cannot specify several mains with a multi-unit index");
4709 elsif Main_Project /= No_Project then
4711 -- If the main project file is a library project file, main(s)
4712 -- cannot be specified on the command line.
4714 if Osint.Number_Of_Files /= 0 then
4715 if Project_Tree.Projects.Table (Main_Project).Library
4716 and then not Unique_Compile
4717 and then ((not Make_Steps) or else Bind_Only or else Link_Only)
4718 then
4719 Make_Failed ("cannot specify a main program " &
4720 "on the command line for a library project file");
4722 else
4723 -- Check that each main on the command line is a source of a
4724 -- project file and, if there are several mains, each of them
4725 -- is a source of the same project file.
4727 Check_Mains;
4728 end if;
4730 -- If no mains have been specified on the command line,
4731 -- and we are using a project file, we either find the main(s)
4732 -- in the attribute Main of the main project, or we put all
4733 -- the sources of the project file as mains.
4735 else
4736 if Main_Index /= 0 then
4737 Make_Failed ("cannot specify a multi-unit index but no main " &
4738 "on the command line");
4739 end if;
4741 declare
4742 Value : String_List_Id :=
4743 Project_Tree.Projects.Table (Main_Project).Mains;
4745 begin
4746 -- The attribute Main is an empty list or not specified,
4747 -- or else gnatmake was invoked with the switch "-u".
4749 if Value = Prj.Nil_String or else Unique_Compile then
4751 if (not Make_Steps) or else Compile_Only
4752 or else not Project_Tree.Projects.Table
4753 (Main_Project).Library
4754 then
4755 -- First make sure that the binder and the linker
4756 -- will not be invoked.
4758 Do_Bind_Step := False;
4759 Do_Link_Step := False;
4761 -- Put all the sources in the queue
4763 No_Main_Subprogram := True;
4764 Insert_Project_Sources
4765 (The_Project => Main_Project,
4766 All_Projects => Unique_Compile_All_Projects,
4767 Into_Q => False);
4769 -- If no sources to compile, then there is nothing to do
4771 if Osint.Number_Of_Files = 0 then
4772 if not Quiet_Output then
4773 Osint.Write_Program_Name;
4774 Write_Line (": no sources to compile");
4775 end if;
4777 Delete_All_Temp_Files;
4778 Exit_Program (E_Success);
4779 end if;
4780 end if;
4782 else
4783 -- The attribute Main is not an empty list.
4784 -- Put all the main subprograms in the list as if there
4785 -- were specified on the command line. However, if attribute
4786 -- Languages includes a language other than Ada, only
4787 -- include the Ada mains; if there is no Ada main, compile
4788 -- all the sources of the project.
4790 declare
4791 Data : constant Project_Data :=
4792 Project_Tree.Projects.Table (Main_Project);
4794 Languages : constant Variable_Value :=
4795 Prj.Util.Value_Of
4796 (Name_Languages,
4797 Data.Decl.Attributes,
4798 Project_Tree);
4800 Current : String_List_Id;
4801 Element : String_Element;
4803 Foreign_Language : Boolean := False;
4804 At_Least_One_Main : Boolean := False;
4806 begin
4807 -- First, determine if there is a foreign language in
4808 -- attribute Languages.
4810 if not Languages.Default then
4811 Current := Languages.Values;
4813 Look_For_Foreign :
4814 while Current /= Nil_String loop
4815 Element := Project_Tree.String_Elements.
4816 Table (Current);
4817 Get_Name_String (Element.Value);
4818 To_Lower (Name_Buffer (1 .. Name_Len));
4820 if Name_Buffer (1 .. Name_Len) /= "ada" then
4821 Foreign_Language := True;
4822 exit Look_For_Foreign;
4823 end if;
4825 Current := Element.Next;
4826 end loop Look_For_Foreign;
4827 end if;
4829 -- Then, find all mains, or if there is a foreign
4830 -- language, all the Ada mains.
4832 while Value /= Prj.Nil_String loop
4833 Get_Name_String
4834 (Project_Tree.String_Elements.Table
4835 (Value).Value);
4837 -- To know if a main is an Ada main, get its project.
4838 -- It should be the project specified on the command
4839 -- line.
4841 if (not Foreign_Language) or else
4842 Prj.Env.Project_Of
4843 (Name_Buffer (1 .. Name_Len),
4844 Main_Project,
4845 Project_Tree) =
4846 Main_Project
4847 then
4848 At_Least_One_Main := True;
4849 Osint.Add_File
4850 (Get_Name_String
4851 (Project_Tree.String_Elements.Table
4852 (Value).Value),
4853 Index =>
4854 Project_Tree.String_Elements.Table
4855 (Value).Index);
4856 end if;
4858 Value := Project_Tree.String_Elements.Table
4859 (Value).Next;
4860 end loop;
4862 -- If we did not get any main, it means that all mains
4863 -- in attribute Mains are in a foreign language and -B
4864 -- was not specified to gnatmake; so, we fail.
4866 if not At_Least_One_Main then
4867 Make_Failed
4868 ("no Ada mains; use -B to build foreign main");
4869 end if;
4870 end;
4872 end if;
4873 end;
4874 end if;
4875 end if;
4877 if Verbose_Mode then
4878 Write_Eol;
4879 Display_Version ("GNATMAKE ", "1995");
4880 end if;
4882 if Main_Project /= No_Project
4883 and then Project_Tree.Projects.Table
4884 (Main_Project).Externally_Built
4885 then
4886 Make_Failed
4887 ("nothing to do for a main project that is externally built");
4888 end if;
4890 if Osint.Number_Of_Files = 0 then
4891 if Main_Project /= No_Project
4892 and then Project_Tree.Projects.Table (Main_Project).Library
4893 then
4894 if Do_Bind_Step
4895 and then not Project_Tree.Projects.Table
4896 (Main_Project).Standalone_Library
4897 then
4898 Make_Failed ("only stand-alone libraries may be bound");
4899 end if;
4901 -- Add the default search directories to be able to find libgnat
4903 Osint.Add_Default_Search_Dirs;
4905 -- Get the target parameters, so that the correct binder generated
4906 -- files are generated if OpenVMS is the target.
4908 begin
4909 Targparm.Get_Target_Parameters;
4911 exception
4912 when Unrecoverable_Error =>
4913 Make_Failed ("*** make failed.");
4914 end;
4916 -- And bind and or link the library
4918 MLib.Prj.Build_Library
4919 (For_Project => Main_Project,
4920 In_Tree => Project_Tree,
4921 Gnatbind => Gnatbind.all,
4922 Gnatbind_Path => Gnatbind_Path,
4923 Gcc => Gcc.all,
4924 Gcc_Path => Gcc_Path,
4925 Bind => Bind_Only,
4926 Link => Link_Only);
4928 Delete_All_Temp_Files;
4929 Exit_Program (E_Success);
4931 else
4932 -- Output usage information if no files to compile
4934 Usage;
4935 Exit_Program (E_Fatal);
4936 end if;
4937 end if;
4939 -- If -M was specified, behave as if -n was specified
4941 if List_Dependencies then
4942 Do_Not_Execute := True;
4943 end if;
4945 -- Note that Osint.M.Next_Main_Source will always return the (possibly
4946 -- abbreviated file) without any directory information.
4948 Main_Source_File := Next_Main_Source;
4950 if Current_File_Index /= No_Index then
4951 Main_Index := Current_File_Index;
4952 end if;
4954 Add_Switch ("-I-", Compiler, And_Save => True);
4956 if Main_Project = No_Project then
4957 if Look_In_Primary_Dir then
4959 Add_Switch
4960 ("-I" &
4961 Normalize_Directory_Name
4962 (Get_Primary_Src_Search_Directory.all).all,
4963 Compiler, Append_Switch => False,
4964 And_Save => False);
4966 end if;
4968 else
4969 -- If we use a project file, we have already checked that a main
4970 -- specified on the command line with directory information has the
4971 -- path name corresponding to a correct source in the project tree.
4972 -- So, we don't need the directory information to be taken into
4973 -- account by Find_File, and in fact it may lead to take the wrong
4974 -- sources for other compilation units, when there are extending
4975 -- projects.
4977 Look_In_Primary_Dir := False;
4978 Add_Switch ("-I-", Binder, And_Save => True);
4979 end if;
4981 -- If the user wants a program without a main subprogram, add the
4982 -- appropriate switch to the binder.
4984 if No_Main_Subprogram then
4985 Add_Switch ("-z", Binder, And_Save => True);
4986 end if;
4988 if Main_Project /= No_Project then
4990 if Project_Tree.Projects.Table
4991 (Main_Project).Object_Directory /= No_Path_Information
4992 then
4993 -- Change current directory to object directory of main project
4995 Project_Of_Current_Object_Directory := No_Project;
4996 Change_To_Object_Directory (Main_Project);
4997 end if;
4999 -- Source file lookups should be cached for efficiency.
5000 -- Source files are not supposed to change.
5002 Osint.Source_File_Data (Cache => True);
5004 -- Find the file name of the (first) main unit
5006 declare
5007 Main_Source_File_Name : constant String :=
5008 Get_Name_String (Main_Source_File);
5009 Main_Unit_File_Name : constant String :=
5010 Prj.Env.File_Name_Of_Library_Unit_Body
5011 (Name => Main_Source_File_Name,
5012 Project => Main_Project,
5013 In_Tree => Project_Tree,
5014 Main_Project_Only =>
5015 not Unique_Compile);
5017 The_Packages : constant Package_Id :=
5018 Project_Tree.Projects.Table
5019 (Main_Project).Decl.Packages;
5021 Builder_Package : constant Prj.Package_Id :=
5022 Prj.Util.Value_Of
5023 (Name => Name_Builder,
5024 In_Packages => The_Packages,
5025 In_Tree => Project_Tree);
5027 Binder_Package : constant Prj.Package_Id :=
5028 Prj.Util.Value_Of
5029 (Name => Name_Binder,
5030 In_Packages => The_Packages,
5031 In_Tree => Project_Tree);
5033 Linker_Package : constant Prj.Package_Id :=
5034 Prj.Util.Value_Of
5035 (Name => Name_Linker,
5036 In_Packages => The_Packages,
5037 In_Tree => Project_Tree);
5039 begin
5040 -- We fail if we cannot find the main source file
5042 if Main_Unit_File_Name = "" then
5043 Make_Failed ('"' & Main_Source_File_Name,
5044 """ is not a unit of project ",
5045 Project_File_Name.all & ".");
5046 else
5047 -- Remove any directory information from the main
5048 -- source file name.
5050 declare
5051 Pos : Natural := Main_Unit_File_Name'Last;
5053 begin
5054 loop
5055 exit when Pos < Main_Unit_File_Name'First or else
5056 Main_Unit_File_Name (Pos) = Directory_Separator;
5057 Pos := Pos - 1;
5058 end loop;
5060 Name_Len := Main_Unit_File_Name'Last - Pos;
5062 Name_Buffer (1 .. Name_Len) :=
5063 Main_Unit_File_Name
5064 (Pos + 1 .. Main_Unit_File_Name'Last);
5066 Main_Source_File := Name_Find;
5068 -- We only output the main source file if there is only one
5070 if Verbose_Mode and then Osint.Number_Of_Files = 1 then
5071 Write_Str ("Main source file: """);
5072 Write_Str (Main_Unit_File_Name
5073 (Pos + 1 .. Main_Unit_File_Name'Last));
5074 Write_Line (""".");
5075 end if;
5076 end;
5077 end if;
5079 -- If there is a package Builder in the main project file, add
5080 -- the switches from it.
5082 if Builder_Package /= No_Package then
5084 -- If there is only one main, we attempt to get the gnatmake
5085 -- switches for this main (if any). If there are no specific
5086 -- switch for this particular main, get the general gnatmake
5087 -- switches (if any).
5089 if Osint.Number_Of_Files = 1 then
5090 if Verbose_Mode then
5091 Write_Str ("Adding gnatmake switches for """);
5092 Write_Str (Main_Unit_File_Name);
5093 Write_Line (""".");
5094 end if;
5096 Add_Switches
5097 (File_Name => Main_Unit_File_Name,
5098 Index => Main_Index,
5099 The_Package => Builder_Package,
5100 Program => None);
5102 else
5103 -- If there are several mains, we always get the general
5104 -- gnatmake switches (if any).
5106 -- Warn the user, if necessary, so that he is not surprised
5107 -- that specific switches are not taken into account.
5109 declare
5110 Defaults : constant Variable_Value :=
5111 Prj.Util.Value_Of
5112 (Name => Name_Ada,
5113 Index => 0,
5114 Attribute_Or_Array_Name => Name_Default_Switches,
5115 In_Package => Builder_Package,
5116 In_Tree => Project_Tree);
5118 Switches : constant Array_Element_Id :=
5119 Prj.Util.Value_Of
5120 (Name => Name_Switches,
5121 In_Arrays =>
5122 Project_Tree.Packages.Table
5123 (Builder_Package).Decl.Arrays,
5124 In_Tree => Project_Tree);
5126 begin
5127 if Defaults /= Nil_Variable_Value then
5128 if (not Quiet_Output)
5129 and then Switches /= No_Array_Element
5130 then
5131 Write_Line
5132 ("Warning: using Builder'Default_Switches" &
5133 "(""Ada""), as there are several mains");
5134 end if;
5136 -- As there is never a source with name " ", we are
5137 -- guaranteed to always get the general switches.
5139 Add_Switches
5140 (File_Name => " ",
5141 Index => 0,
5142 The_Package => Builder_Package,
5143 Program => None);
5145 elsif (not Quiet_Output)
5146 and then Switches /= No_Array_Element
5147 then
5148 Write_Line
5149 ("Warning: using no switches from package Builder," &
5150 " as there are several mains");
5151 end if;
5152 end;
5153 end if;
5154 end if;
5156 Osint.Add_Default_Search_Dirs;
5158 -- Record the current last switch index for table Binder_Switches
5159 -- and Linker_Switches, so that these tables may be reset before
5160 -- for each main, before adding switches from the project file
5161 -- and from the command line.
5163 Last_Binder_Switch := Binder_Switches.Last;
5164 Last_Linker_Switch := Linker_Switches.Last;
5166 Check_Steps;
5168 -- Add binder switches from the project file for the first main
5170 if Do_Bind_Step and Binder_Package /= No_Package then
5171 if Verbose_Mode then
5172 Write_Str ("Adding binder switches for """);
5173 Write_Str (Main_Unit_File_Name);
5174 Write_Line (""".");
5175 end if;
5177 Add_Switches
5178 (File_Name => Main_Unit_File_Name,
5179 Index => Main_Index,
5180 The_Package => Binder_Package,
5181 Program => Binder);
5182 end if;
5184 -- Add linker switches from the project file for the first main
5186 if Do_Link_Step and Linker_Package /= No_Package then
5187 if Verbose_Mode then
5188 Write_Str ("Adding linker switches for""");
5189 Write_Str (Main_Unit_File_Name);
5190 Write_Line (""".");
5191 end if;
5193 Add_Switches
5194 (File_Name => Main_Unit_File_Name,
5195 Index => Main_Index,
5196 The_Package => Linker_Package,
5197 Program => Linker);
5198 end if;
5199 end;
5200 end if;
5202 -- Get the target parameters, which are only needed for a couple of
5203 -- cases in gnatmake. Protect against an exception, such as the case
5204 -- of system.ads missing from the library, and fail gracefully.
5206 begin
5207 Targparm.Get_Target_Parameters;
5208 exception
5209 when Unrecoverable_Error =>
5210 Make_Failed ("*** make failed.");
5211 end;
5213 -- Special processing for VM targets
5215 if Targparm.VM_Target /= No_VM then
5217 -- Set proper processing commands
5219 case Targparm.VM_Target is
5220 when Targparm.JVM_Target =>
5222 -- Do not check for an object file (".o") when compiling to
5223 -- JVM machine since ".class" files are generated instead.
5225 Check_Object_Consistency := False;
5227 Gcc := new String'("jgnat");
5228 Gnatbind := new String'("jgnatbind");
5229 Gnatlink := new String'("jgnatlink");
5231 when Targparm.CLI_Target =>
5232 Gcc := new String'("dotnet-gnatcompile");
5234 when Targparm.No_VM =>
5235 raise Program_Error;
5236 end case;
5237 end if;
5239 Display_Commands (not Quiet_Output);
5241 Check_Steps;
5243 if Main_Project /= No_Project then
5245 -- For all library project, if the library file does not exist, put
5246 -- all the project sources in the queue, and flag the project so that
5247 -- the library is generated.
5249 if not Unique_Compile
5250 and then MLib.Tgt.Support_For_Libraries /= Prj.None
5251 then
5252 for Proj in Project_Table.First ..
5253 Project_Table.Last (Project_Tree.Projects)
5254 loop
5255 if Project_Tree.Projects.Table (Proj).Library then
5256 Project_Tree.Projects.Table
5257 (Proj).Need_To_Build_Lib :=
5258 (not MLib.Tgt.Library_Exists_For (Proj, Project_Tree))
5259 and then (not Project_Tree.Projects.Table
5260 (Proj).Externally_Built);
5262 if Project_Tree.Projects.Table (Proj).Need_To_Build_Lib then
5264 -- If there is no object directory, then it will be
5265 -- impossible to build the library. So fail immediately.
5267 if Project_Tree.Projects.Table (Proj).Object_Directory =
5268 No_Path_Information
5269 then
5270 Make_Failed
5271 ("no object files to build library for project """,
5272 Get_Name_String
5273 (Project_Tree.Projects.Table (Proj).Name),
5274 """");
5275 Project_Tree.Projects.Table
5276 (Proj).Need_To_Build_Lib := False;
5278 else
5279 if Verbose_Mode then
5280 Write_Str
5281 ("Library file does not exist for project """);
5282 Write_Str
5283 (Get_Name_String
5284 (Project_Tree.Projects.Table
5285 (Proj).Name));
5286 Write_Line ("""");
5287 end if;
5289 Insert_Project_Sources
5290 (The_Project => Proj,
5291 All_Projects => False,
5292 Into_Q => True);
5293 end if;
5294 end if;
5295 end if;
5296 end loop;
5297 end if;
5299 -- If a relative path output file has been specified, we add
5300 -- the exec directory.
5302 for J in reverse 1 .. Saved_Linker_Switches.Last - 1 loop
5303 if Saved_Linker_Switches.Table (J).all = Output_Flag.all then
5304 declare
5305 Exec_File_Name : constant String :=
5306 Saved_Linker_Switches.Table (J + 1).all;
5308 begin
5309 if not Is_Absolute_Path (Exec_File_Name) then
5310 Get_Name_String
5311 (Project_Tree.Projects.Table
5312 (Main_Project).Exec_Directory.Name);
5314 if Name_Buffer (Name_Len) /= Directory_Separator then
5315 Name_Len := Name_Len + 1;
5316 Name_Buffer (Name_Len) := Directory_Separator;
5317 end if;
5319 Name_Buffer (Name_Len + 1 ..
5320 Name_Len + Exec_File_Name'Length) :=
5321 Exec_File_Name;
5322 Name_Len := Name_Len + Exec_File_Name'Length;
5323 Saved_Linker_Switches.Table (J + 1) :=
5324 new String'(Name_Buffer (1 .. Name_Len));
5325 end if;
5326 end;
5328 exit;
5329 end if;
5330 end loop;
5332 -- If we are using a project file, for relative paths we add the
5333 -- current working directory for any relative path on the command
5334 -- line and the project directory, for any relative path in the
5335 -- project file.
5337 declare
5338 Dir_Path : constant String_Access :=
5339 new String'(Get_Name_String
5340 (Project_Tree.Projects.Table
5341 (Main_Project).Directory.Name));
5342 begin
5343 for J in 1 .. Binder_Switches.Last loop
5344 Test_If_Relative_Path
5345 (Binder_Switches.Table (J),
5346 Parent => Dir_Path, Including_L_Switch => False);
5347 end loop;
5349 for J in 1 .. Saved_Binder_Switches.Last loop
5350 Test_If_Relative_Path
5351 (Saved_Binder_Switches.Table (J),
5352 Parent => Current_Work_Dir, Including_L_Switch => False);
5353 end loop;
5355 for J in 1 .. Linker_Switches.Last loop
5356 Test_If_Relative_Path
5357 (Linker_Switches.Table (J), Parent => Dir_Path);
5358 end loop;
5360 for J in 1 .. Saved_Linker_Switches.Last loop
5361 Test_If_Relative_Path
5362 (Saved_Linker_Switches.Table (J), Parent => Current_Work_Dir);
5363 end loop;
5365 for J in 1 .. Gcc_Switches.Last loop
5366 Test_If_Relative_Path
5367 (Gcc_Switches.Table (J),
5368 Parent => Dir_Path,
5369 Including_Non_Switch => False);
5370 end loop;
5372 for J in 1 .. Saved_Gcc_Switches.Last loop
5373 Test_If_Relative_Path
5374 (Saved_Gcc_Switches.Table (J),
5375 Parent => Current_Work_Dir,
5376 Including_Non_Switch => False);
5377 end loop;
5378 end;
5379 end if;
5381 -- We now put in the Binder_Switches and Linker_Switches tables, the
5382 -- binder and linker switches of the command line that have been put in
5383 -- the Saved_ tables. If a project file was used, then the command line
5384 -- switches will follow the project file switches.
5386 for J in 1 .. Saved_Binder_Switches.Last loop
5387 Add_Switch
5388 (Saved_Binder_Switches.Table (J),
5389 Binder,
5390 And_Save => False);
5391 end loop;
5393 for J in 1 .. Saved_Linker_Switches.Last loop
5394 Add_Switch
5395 (Saved_Linker_Switches.Table (J),
5396 Linker,
5397 And_Save => False);
5398 end loop;
5400 -- If no project file is used, we just put the gcc switches
5401 -- from the command line in the Gcc_Switches table.
5403 if Main_Project = No_Project then
5404 for J in 1 .. Saved_Gcc_Switches.Last loop
5405 Add_Switch
5406 (Saved_Gcc_Switches.Table (J),
5407 Compiler,
5408 And_Save => False);
5409 end loop;
5411 else
5412 -- If there is a project, put the command line gcc switches in the
5413 -- variable The_Saved_Gcc_Switches. They are going to be used later
5414 -- in procedure Compile_Sources.
5416 The_Saved_Gcc_Switches :=
5417 new Argument_List (1 .. Saved_Gcc_Switches.Last + 1);
5419 for J in 1 .. Saved_Gcc_Switches.Last loop
5420 The_Saved_Gcc_Switches (J) := Saved_Gcc_Switches.Table (J);
5421 end loop;
5423 -- We never use gnat.adc when a project file is used
5425 The_Saved_Gcc_Switches (The_Saved_Gcc_Switches'Last) :=
5426 No_gnat_adc;
5427 end if;
5429 -- If there was a --GCC, --GNATBIND or --GNATLINK switch on
5430 -- the command line, then we have to use it, even if there was
5431 -- another switch in the project file.
5433 if Saved_Gcc /= null then
5434 Gcc := Saved_Gcc;
5435 end if;
5437 if Saved_Gnatbind /= null then
5438 Gnatbind := Saved_Gnatbind;
5439 end if;
5441 if Saved_Gnatlink /= null then
5442 Gnatlink := Saved_Gnatlink;
5443 end if;
5445 Gcc_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
5446 Gnatbind_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gnatbind.all);
5447 Gnatlink_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gnatlink.all);
5449 -- If we have specified -j switch both from the project file
5450 -- and on the command line, the one from the command line takes
5451 -- precedence.
5453 if Saved_Maximum_Processes = 0 then
5454 Saved_Maximum_Processes := Maximum_Processes;
5455 end if;
5457 -- Allocate as many temporary mapping file names as the maximum
5458 -- number of compilation processed, for each possible project.
5460 The_Mapping_File_Names :=
5461 new Temp_Path_Names
5462 (No_Project .. Project_Table.Last (Project_Tree.Projects),
5463 1 .. Saved_Maximum_Processes);
5464 Last_Mapping_File_Names :=
5465 new Indices'
5466 (No_Project .. Project_Table.Last (Project_Tree.Projects)
5467 => 0);
5469 The_Free_Mapping_File_Indices :=
5470 new Free_File_Indices
5471 (No_Project .. Project_Table.Last (Project_Tree.Projects),
5472 1 .. Saved_Maximum_Processes);
5473 Last_Free_Indices :=
5474 new Indices'(No_Project .. Project_Table.Last
5475 (Project_Tree.Projects) => 0);
5477 Bad_Compilation.Init;
5479 -- If project files are used, create the mapping of all the sources,
5480 -- so that the correct paths will be found. Otherwise, if there is
5481 -- a file which is not a source with the same name in a source directory
5482 -- this file may be incorrectly found.
5484 if Main_Project /= No_Project then
5485 Prj.Env.Create_Mapping (Project_Tree);
5486 end if;
5488 Current_Main_Index := Main_Index;
5490 -- Here is where the make process is started
5492 -- We do the same process for each main
5494 Multiple_Main_Loop : for N_File in 1 .. Osint.Number_Of_Files loop
5496 -- First, find the executable name and path
5498 Executable := No_File;
5499 Executable_Obsolete := False;
5500 Non_Std_Executable :=
5501 Targparm.Executable_Extension_On_Target /= No_Name;
5503 -- Look inside the linker switches to see if the name of the final
5504 -- executable program was specified.
5507 J in reverse Linker_Switches.First .. Linker_Switches.Last
5508 loop
5509 if Linker_Switches.Table (J).all = Output_Flag.all then
5510 pragma Assert (J < Linker_Switches.Last);
5512 -- We cannot specify a single executable for several
5513 -- main subprograms!
5515 if Osint.Number_Of_Files > 1 then
5516 Fail
5517 ("cannot specify a single executable " &
5518 "for several mains");
5519 end if;
5521 Name_Len := Linker_Switches.Table (J + 1)'Length;
5522 Name_Buffer (1 .. Name_Len) :=
5523 Linker_Switches.Table (J + 1).all;
5524 Executable := Name_Enter;
5526 Verbose_Msg (Executable, "final executable");
5527 end if;
5528 end loop;
5530 -- If the name of the final executable program was not specified
5531 -- then construct it from the main input file.
5533 if Executable = No_File then
5534 if Main_Project = No_Project then
5535 Executable :=
5536 Executable_Name (Strip_Suffix (Main_Source_File));
5538 else
5539 -- If we are using a project file, we attempt to remove the
5540 -- body (or spec) termination of the main subprogram. We find
5541 -- it the naming scheme of the project file. This avoids
5542 -- generating an executable "main.2" for a main subprogram
5543 -- "main.2.ada", when the body termination is ".2.ada".
5545 Executable :=
5546 Prj.Util.Executable_Of
5547 (Main_Project, Project_Tree, Main_Source_File, Main_Index);
5548 end if;
5549 end if;
5551 if Main_Project /= No_Project then
5552 declare
5553 Exec_File_Name : constant String :=
5554 Get_Name_String (Executable);
5556 begin
5557 if not Is_Absolute_Path (Exec_File_Name) then
5558 Get_Name_String
5559 (Project_Tree.Projects.Table
5560 (Main_Project).Exec_Directory.Display_Name);
5562 if Name_Buffer (Name_Len) /= Directory_Separator then
5563 Name_Len := Name_Len + 1;
5564 Name_Buffer (Name_Len) := Directory_Separator;
5565 end if;
5567 Name_Buffer (Name_Len + 1 ..
5568 Name_Len + Exec_File_Name'Length) :=
5569 Exec_File_Name;
5571 Name_Len := Name_Len + Exec_File_Name'Length;
5572 Executable := Name_Find;
5573 end if;
5575 Non_Std_Executable := True;
5576 end;
5577 end if;
5579 if Do_Compile_Step then
5580 Recursive_Compilation_Step : declare
5581 Args : Argument_List (1 .. Gcc_Switches.Last);
5583 First_Compiled_File : File_Name_Type;
5584 Youngest_Obj_File : File_Name_Type;
5585 Youngest_Obj_Stamp : Time_Stamp_Type;
5587 Executable_Stamp : Time_Stamp_Type;
5588 -- Executable is the final executable program
5590 Library_Rebuilt : Boolean := False;
5592 begin
5593 for J in 1 .. Gcc_Switches.Last loop
5594 Args (J) := Gcc_Switches.Table (J);
5595 end loop;
5597 -- Now we invoke Compile_Sources for the current main
5599 Compile_Sources
5600 (Main_Source => Main_Source_File,
5601 Args => Args,
5602 First_Compiled_File => First_Compiled_File,
5603 Most_Recent_Obj_File => Youngest_Obj_File,
5604 Most_Recent_Obj_Stamp => Youngest_Obj_Stamp,
5605 Main_Unit => Is_Main_Unit,
5606 Main_Index => Current_Main_Index,
5607 Compilation_Failures => Compilation_Failures,
5608 Check_Readonly_Files => Check_Readonly_Files,
5609 Do_Not_Execute => Do_Not_Execute,
5610 Force_Compilations => Force_Compilations,
5611 In_Place_Mode => In_Place_Mode,
5612 Keep_Going => Keep_Going,
5613 Initialize_ALI_Data => True,
5614 Max_Process => Saved_Maximum_Processes);
5616 if Verbose_Mode then
5617 Write_Str ("End of compilation");
5618 Write_Eol;
5619 end if;
5621 -- Make sure the queue will be reinitialized for the next round
5623 First_Q_Initialization := True;
5625 Total_Compilation_Failures :=
5626 Total_Compilation_Failures + Compilation_Failures;
5628 if Total_Compilation_Failures /= 0 then
5629 if Keep_Going then
5630 goto Next_Main;
5631 else
5632 List_Bad_Compilations;
5633 Report_Compilation_Failed;
5634 end if;
5635 end if;
5637 -- Regenerate libraries, if there are any and if object files
5638 -- have been regenerated.
5640 if Main_Project /= No_Project
5641 and then MLib.Tgt.Support_For_Libraries /= Prj.None
5642 and then (Do_Bind_Step
5643 or Unique_Compile_All_Projects
5644 or not Compile_Only)
5645 and then (Do_Link_Step or N_File = Osint.Number_Of_Files)
5646 then
5647 Library_Projs.Init;
5649 declare
5650 Depth : Natural;
5651 Current : Natural;
5653 procedure Add_To_Library_Projs (Proj : Project_Id);
5654 -- Add project Project to table Library_Projs in
5655 -- decreasing depth order.
5657 --------------------------
5658 -- Add_To_Library_Projs --
5659 --------------------------
5661 procedure Add_To_Library_Projs (Proj : Project_Id) is
5662 Prj : Project_Id;
5664 begin
5665 Library_Projs.Increment_Last;
5666 Depth := Project_Tree.Projects.Table (Proj).Depth;
5668 -- Put the projects in decreasing depth order, so that
5669 -- if libA depends on libB, libB is first in order.
5671 Current := Library_Projs.Last;
5672 while Current > 1 loop
5673 Prj := Library_Projs.Table (Current - 1);
5674 exit when Project_Tree.Projects.Table
5675 (Prj).Depth >= Depth;
5676 Library_Projs.Table (Current) := Prj;
5677 Current := Current - 1;
5678 end loop;
5680 Library_Projs.Table (Current) := Proj;
5681 end Add_To_Library_Projs;
5683 -- Start of processing for ??? (should name declare block
5684 -- or probably better, break this out as a nested proc).
5686 begin
5687 -- Put in Library_Projs table all library project
5688 -- file ids when the library need to be rebuilt.
5690 for Proj1 in Project_Table.First ..
5691 Project_Table.Last (Project_Tree.Projects)
5692 loop
5693 if Project_Tree.Projects.Table
5694 (Proj1).Standalone_Library
5695 then
5696 There_Are_Stand_Alone_Libraries := True;
5697 end if;
5699 if Project_Tree.Projects.Table (Proj1).Library then
5700 MLib.Prj.Check_Library (Proj1, Project_Tree);
5701 end if;
5703 if Project_Tree.Projects.Table
5704 (Proj1).Need_To_Build_Lib
5705 then
5706 Add_To_Library_Projs (Proj1);
5707 end if;
5708 end loop;
5710 -- Check if importing libraries should be regenerated
5711 -- because at least an imported library will be
5712 -- regenerated or is more recent.
5714 for Proj1 in Project_Table.First ..
5715 Project_Table.Last (Project_Tree.Projects)
5716 loop
5717 if Project_Tree.Projects.Table (Proj1).Library
5718 and then
5719 Project_Tree.Projects.Table (Proj1).Library_Kind /=
5720 Static
5721 and then not Project_Tree.Projects.Table
5722 (Proj1).Need_To_Build_Lib
5723 and then not Project_Tree.Projects.Table
5724 (Proj1).Externally_Built
5725 then
5726 declare
5727 List : Project_List;
5728 Element : Project_Element;
5729 Proj2 : Project_Id;
5730 Rebuild : Boolean := False;
5732 Lib_Timestamp1 : constant Time_Stamp_Type :=
5733 Project_Tree.Projects.Table
5734 (Proj1).Library_TS;
5736 begin
5737 List := Project_Tree.Projects.Table (Proj1).
5738 All_Imported_Projects;
5739 while List /= Empty_Project_List loop
5740 Element :=
5741 Project_Tree.Project_Lists.Table (List);
5742 Proj2 := Element.Project;
5745 Project_Tree.Projects.Table (Proj2).Library
5746 then
5747 if Project_Tree.Projects.Table (Proj2).
5748 Need_To_Build_Lib
5749 or else
5750 (Lib_Timestamp1 <
5751 Project_Tree.Projects.Table
5752 (Proj2).Library_TS)
5753 then
5754 Rebuild := True;
5755 exit;
5756 end if;
5757 end if;
5759 List := Element.Next;
5760 end loop;
5762 if Rebuild then
5763 Project_Tree.Projects.Table
5764 (Proj1).Need_To_Build_Lib := True;
5765 Add_To_Library_Projs (Proj1);
5766 end if;
5767 end;
5768 end if;
5769 end loop;
5771 -- Reset the flags Need_To_Build_Lib for the next main,
5772 -- to avoid rebuilding libraries uselessly.
5774 for Proj1 in Project_Table.First ..
5775 Project_Table.Last (Project_Tree.Projects)
5776 loop
5777 Project_Tree.Projects.Table
5778 (Proj1).Need_To_Build_Lib := False;
5779 end loop;
5780 end;
5782 -- Build the libraries, if any need to be built
5784 for J in 1 .. Library_Projs.Last loop
5785 Library_Rebuilt := True;
5787 -- If a library is rebuilt, then executables are obsolete
5789 Executable_Obsolete := True;
5791 MLib.Prj.Build_Library
5792 (For_Project => Library_Projs.Table (J),
5793 In_Tree => Project_Tree,
5794 Gnatbind => Gnatbind.all,
5795 Gnatbind_Path => Gnatbind_Path,
5796 Gcc => Gcc.all,
5797 Gcc_Path => Gcc_Path);
5798 end loop;
5799 end if;
5801 if List_Dependencies then
5802 if First_Compiled_File /= No_File then
5803 Inform
5804 (First_Compiled_File,
5805 "must be recompiled. Can't generate dependence list.");
5806 else
5807 List_Depend;
5808 end if;
5810 elsif First_Compiled_File = No_File
5811 and then not Do_Bind_Step
5812 and then not Quiet_Output
5813 and then not Library_Rebuilt
5814 and then Osint.Number_Of_Files = 1
5815 then
5816 Inform (Msg => "objects up to date.");
5818 elsif Do_Not_Execute
5819 and then First_Compiled_File /= No_File
5820 then
5821 Write_Name (First_Compiled_File);
5822 Write_Eol;
5823 end if;
5825 -- Stop after compile step if any of:
5827 -- 1) -n (Do_Not_Execute) specified
5829 -- 2) -M (List_Dependencies) specified (also sets
5830 -- Do_Not_Execute above, so this is probably superfluous).
5832 -- 3) -c (Compile_Only) specified, but not -b (Bind_Only)
5834 -- 4) Made unit cannot be a main unit
5836 if ((Do_Not_Execute
5837 or List_Dependencies
5838 or not Do_Bind_Step
5839 or not Is_Main_Unit)
5840 and then not No_Main_Subprogram
5841 and then not Build_Bind_And_Link_Full_Project)
5842 or else Unique_Compile
5843 then
5844 if Osint.Number_Of_Files = 1 then
5845 exit Multiple_Main_Loop;
5847 else
5848 goto Next_Main;
5849 end if;
5850 end if;
5852 -- If the objects were up-to-date check if the executable file
5853 -- is also up-to-date. For now always bind and link on the JVM
5854 -- since there is currently no simple way to check the
5855 -- up-to-date status of objects
5857 if Targparm.VM_Target /= JVM_Target
5858 and then First_Compiled_File = No_File
5859 then
5860 Executable_Stamp := File_Stamp (Executable);
5862 if not Executable_Obsolete then
5863 Executable_Obsolete :=
5864 Youngest_Obj_Stamp > Executable_Stamp;
5865 end if;
5867 if not Executable_Obsolete then
5868 for Index in reverse 1 .. Dependencies.Last loop
5869 if Is_In_Obsoleted
5870 (Dependencies.Table (Index).Depends_On)
5871 then
5872 Enter_Into_Obsoleted
5873 (Dependencies.Table (Index).This);
5874 end if;
5875 end loop;
5877 Executable_Obsolete := Is_In_Obsoleted (Main_Source_File);
5878 Dependencies.Init;
5879 end if;
5881 if not Executable_Obsolete then
5883 -- If no Ada object files obsolete the executable, check
5884 -- for younger or missing linker files.
5886 Check_Linker_Options
5887 (Executable_Stamp,
5888 Youngest_Obj_File,
5889 Youngest_Obj_Stamp);
5891 Executable_Obsolete := Youngest_Obj_File /= No_File;
5892 end if;
5894 -- Return if the executable is up to date
5895 -- and otherwise motivate the relink/rebind.
5897 if not Executable_Obsolete then
5898 if not Quiet_Output then
5899 Inform (Executable, "up to date.");
5900 end if;
5902 if Osint.Number_Of_Files = 1 then
5903 exit Multiple_Main_Loop;
5905 else
5906 goto Next_Main;
5907 end if;
5908 end if;
5910 if Executable_Stamp (1) = ' ' then
5911 if not No_Main_Subprogram then
5912 Verbose_Msg (Executable, "missing.", Prefix => " ");
5913 end if;
5915 elsif Youngest_Obj_Stamp (1) = ' ' then
5916 Verbose_Msg
5917 (Youngest_Obj_File, "missing.", Prefix => " ");
5919 elsif Youngest_Obj_Stamp > Executable_Stamp then
5920 Verbose_Msg
5921 (Youngest_Obj_File,
5922 "(" & String (Youngest_Obj_Stamp) & ") newer than",
5923 Executable,
5924 "(" & String (Executable_Stamp) & ")");
5926 else
5927 Verbose_Msg
5928 (Executable, "needs to be rebuilt", Prefix => " ");
5930 end if;
5931 end if;
5932 end Recursive_Compilation_Step;
5933 end if;
5935 -- For binding and linking, we need to be in the object directory of
5936 -- the main project.
5938 if Main_Project /= No_Project then
5939 Change_To_Object_Directory (Main_Project);
5940 end if;
5942 -- If we are here, it means that we need to rebuilt the current
5943 -- main. So we set Executable_Obsolete to True to make sure that
5944 -- the subsequent mains will be rebuilt.
5946 Main_ALI_In_Place_Mode_Step : declare
5947 ALI_File : File_Name_Type;
5948 Src_File : File_Name_Type;
5950 begin
5951 Src_File := Strip_Directory (Main_Source_File);
5952 ALI_File := Lib_File_Name (Src_File, Current_Main_Index);
5953 Main_ALI_File := Full_Lib_File_Name (ALI_File);
5955 -- When In_Place_Mode, the library file can be located in the
5956 -- Main_Source_File directory which may not be present in the
5957 -- library path. If it is not present then use the corresponding
5958 -- library file name.
5960 if Main_ALI_File = No_File and then In_Place_Mode then
5961 Get_Name_String (Get_Directory (Full_Source_Name (Src_File)));
5962 Get_Name_String_And_Append (ALI_File);
5963 Main_ALI_File := Name_Find;
5964 Main_ALI_File := Full_Lib_File_Name (Main_ALI_File);
5965 end if;
5967 if Main_ALI_File = No_File then
5968 Make_Failed ("could not find the main ALI file");
5969 end if;
5970 end Main_ALI_In_Place_Mode_Step;
5972 if Do_Bind_Step then
5973 Bind_Step : declare
5974 Args : Argument_List
5975 (Binder_Switches.First .. Binder_Switches.Last + 2);
5976 -- The arguments for the invocation of gnatbind
5978 Last_Arg : Natural := Binder_Switches.Last;
5979 -- Index of the last argument in Args
5981 Shared_Libs : Boolean := False;
5982 -- Set to True when there are shared library project files or
5983 -- when gnatbind is invoked with -shared.
5985 begin
5986 -- Check if there are shared libraries, so that gnatbind is
5987 -- called with -shared. Check also if gnatbind is called with
5988 -- -shared, so that gnatlink is called with -shared-libgcc
5989 -- ensuring that the shared version of libgcc will be used.
5991 if Main_Project /= No_Project
5992 and then MLib.Tgt.Support_For_Libraries /= Prj.None
5993 then
5994 for Proj in Project_Table.First ..
5995 Project_Table.Last (Project_Tree.Projects)
5996 loop
5997 if Project_Tree.Projects.Table (Proj).Library
5998 and then Project_Tree.Projects.Table
5999 (Proj).Library_Kind /= Static
6000 then
6001 Shared_Libs := True;
6002 Bind_Shared := Shared_Switch'Access;
6003 exit;
6004 end if;
6005 end loop;
6006 end if;
6008 -- Check now for switch -shared
6010 if not Shared_Libs then
6011 for J in Binder_Switches.First .. Last_Arg loop
6012 if Binder_Switches.Table (J).all = "-shared" then
6013 Shared_Libs := True;
6014 exit;
6015 end if;
6016 end loop;
6017 end if;
6019 -- If shared libraries present, invoke gnatlink with
6020 -- -shared-libgcc.
6022 if Shared_Libs then
6023 Link_With_Shared_Libgcc := Shared_Libgcc_Switch'Access;
6024 end if;
6026 -- Get all the binder switches
6028 for J in Binder_Switches.First .. Last_Arg loop
6029 Args (J) := Binder_Switches.Table (J);
6030 end loop;
6032 if There_Are_Stand_Alone_Libraries then
6033 Last_Arg := Last_Arg + 1;
6034 Args (Last_Arg) := Force_Elab_Flags_String'Access;
6035 end if;
6037 if Main_Project /= No_Project then
6039 -- Put all the source directories in ADA_INCLUDE_PATH,
6040 -- and all the object directories in ADA_OBJECTS_PATH.
6042 Prj.Env.Set_Ada_Paths (Main_Project, Project_Tree, False);
6044 -- If switch -C was specified, create a binder mapping file
6046 if Create_Mapping_File then
6047 Create_Binder_Mapping_File (Args, Last_Arg);
6048 end if;
6050 end if;
6052 begin
6053 Bind (Main_ALI_File,
6054 Bind_Shared.all & Args (Args'First .. Last_Arg));
6056 exception
6057 when others =>
6059 -- If -dn was not specified, delete the temporary mapping
6060 -- file, if one was created.
6062 if not Debug.Debug_Flag_N
6063 and then Mapping_Path /= No_Path
6064 then
6065 Delete_File (Get_Name_String (Mapping_Path), Discard);
6066 end if;
6068 -- And reraise the exception
6070 raise;
6071 end;
6073 -- If -dn was not specified, delete the temporary mapping file,
6074 -- if one was created.
6076 if not Debug.Debug_Flag_N and then Mapping_Path /= No_Path then
6077 Delete_File (Get_Name_String (Mapping_Path), Discard);
6078 end if;
6079 end Bind_Step;
6080 end if;
6082 if Do_Link_Step then
6083 Link_Step : declare
6084 Linker_Switches_Last : constant Integer := Linker_Switches.Last;
6085 Path_Option : constant String_Access :=
6086 MLib.Linker_Library_Path_Option;
6087 There_Are_Libraries : Boolean := False;
6088 Current : Natural;
6089 Proj2 : Project_Id;
6090 Depth : Natural;
6092 begin
6093 if not Run_Path_Option then
6094 Linker_Switches.Increment_Last;
6095 Linker_Switches.Table (Linker_Switches.Last) :=
6096 new String'("-R");
6097 end if;
6099 if Main_Project /= No_Project then
6100 Library_Paths.Set_Last (0);
6101 Library_Projs.Init;
6103 if MLib.Tgt.Support_For_Libraries /= Prj.None then
6105 -- Check for library projects
6107 for Proj1 in Project_Table.First ..
6108 Project_Table.Last (Project_Tree.Projects)
6109 loop
6110 if Proj1 /= Main_Project
6111 and then
6112 Project_Tree.Projects.Table (Proj1).Library
6113 then
6114 -- Add this project to table Library_Projs
6116 There_Are_Libraries := True;
6117 Depth := Project_Tree.Projects.Table (Proj1).Depth;
6118 Library_Projs.Increment_Last;
6119 Current := Library_Projs.Last;
6121 -- Any project with a greater depth should be
6122 -- after this project in the list.
6124 while Current > 1 loop
6125 Proj2 := Library_Projs.Table (Current - 1);
6126 exit when Project_Tree.Projects.Table
6127 (Proj2).Depth <= Depth;
6128 Library_Projs.Table (Current) := Proj2;
6129 Current := Current - 1;
6130 end loop;
6132 Library_Projs.Table (Current) := Proj1;
6134 -- If it is not a static library and path option
6135 -- is set, add it to the Library_Paths table.
6137 if Project_Tree.Projects.Table
6138 (Proj1).Library_Kind /= Static
6139 and then Path_Option /= null
6140 then
6141 Library_Paths.Increment_Last;
6142 Library_Paths.Table (Library_Paths.Last) :=
6143 new String'
6144 (Get_Name_String
6145 (Project_Tree.Projects.Table
6146 (Proj1).Library_Dir.Display_Name));
6147 end if;
6148 end if;
6149 end loop;
6151 for Index in 1 .. Library_Projs.Last loop
6153 -- Add the -L switch
6155 Linker_Switches.Increment_Last;
6156 Linker_Switches.Table (Linker_Switches.Last) :=
6157 new String'("-L" &
6158 Get_Name_String
6159 (Project_Tree.Projects.Table
6160 (Library_Projs.Table (Index)).
6161 Library_Dir.Display_Name));
6163 -- Add the -l switch
6165 Linker_Switches.Increment_Last;
6166 Linker_Switches.Table (Linker_Switches.Last) :=
6167 new String'("-l" &
6168 Get_Name_String
6169 (Project_Tree.Projects.Table
6170 (Library_Projs.Table (Index)).
6171 Library_Name));
6172 end loop;
6173 end if;
6175 if There_Are_Libraries then
6177 -- If Path_Option is not null, create the switch
6178 -- ("-Wl,-rpath," or equivalent) with all the non static
6179 -- library dirs plus the standard GNAT library dir.
6180 -- We do that only if Run_Path_Option is True
6181 -- (not disabled by -R switch).
6183 if Run_Path_Option and Path_Option /= null then
6184 declare
6185 Option : String_Access;
6186 Length : Natural := Path_Option'Length;
6187 Current : Natural;
6189 begin
6190 for Index in
6191 Library_Paths.First .. Library_Paths.Last
6192 loop
6193 -- Add the length of the library dir plus one
6194 -- for the directory separator.
6196 Length :=
6197 Length +
6198 Library_Paths.Table (Index)'Length + 1;
6199 end loop;
6201 -- Finally, add the length of the standard GNAT
6202 -- library dir.
6204 Length := Length + MLib.Utl.Lib_Directory'Length;
6205 Option := new String (1 .. Length);
6206 Option (1 .. Path_Option'Length) := Path_Option.all;
6207 Current := Path_Option'Length;
6209 -- Put each library dir followed by a dir separator
6211 for Index in
6212 Library_Paths.First .. Library_Paths.Last
6213 loop
6214 Option
6215 (Current + 1 ..
6216 Current +
6217 Library_Paths.Table (Index)'Length) :=
6218 Library_Paths.Table (Index).all;
6219 Current :=
6220 Current +
6221 Library_Paths.Table (Index)'Length + 1;
6222 Option (Current) := Path_Separator;
6223 end loop;
6225 -- Finally put the standard GNAT library dir
6227 Option
6228 (Current + 1 ..
6229 Current + MLib.Utl.Lib_Directory'Length) :=
6230 MLib.Utl.Lib_Directory;
6232 -- And add the switch to the linker switches
6234 Linker_Switches.Increment_Last;
6235 Linker_Switches.Table (Linker_Switches.Last) :=
6236 Option;
6237 end;
6238 end if;
6240 end if;
6242 -- Put the object directories in ADA_OBJECTS_PATH
6244 Prj.Env.Set_Ada_Paths (Main_Project, Project_Tree, False);
6246 -- Check for attributes Linker'Linker_Options in projects
6247 -- other than the main project
6249 declare
6250 Linker_Options : constant String_List :=
6251 Linker_Options_Switches
6252 (Main_Project, Project_Tree);
6253 begin
6254 for Option in Linker_Options'Range loop
6255 Linker_Switches.Increment_Last;
6256 Linker_Switches.Table (Linker_Switches.Last) :=
6257 Linker_Options (Option);
6258 end loop;
6259 end;
6260 end if;
6262 declare
6263 Args : Argument_List
6264 (Linker_Switches.First .. Linker_Switches.Last + 2);
6266 Last_Arg : Integer := Linker_Switches.First - 1;
6267 Skip : Boolean := False;
6269 begin
6270 -- Get all the linker switches
6272 for J in Linker_Switches.First .. Linker_Switches.Last loop
6273 if Skip then
6274 Skip := False;
6276 elsif Non_Std_Executable
6277 and then Linker_Switches.Table (J).all = "-o"
6278 then
6279 Skip := True;
6281 else
6282 Last_Arg := Last_Arg + 1;
6283 Args (Last_Arg) := Linker_Switches.Table (J);
6284 end if;
6285 end loop;
6287 -- If need be, add the -o switch
6289 if Non_Std_Executable then
6290 Last_Arg := Last_Arg + 1;
6291 Args (Last_Arg) := new String'("-o");
6292 Last_Arg := Last_Arg + 1;
6293 Args (Last_Arg) :=
6294 new String'(Get_Name_String (Executable));
6295 end if;
6297 -- And invoke the linker
6299 declare
6300 Success : Boolean := False;
6301 begin
6302 Link (Main_ALI_File,
6303 Link_With_Shared_Libgcc.all &
6304 Args (Args'First .. Last_Arg),
6305 Success);
6307 if Success then
6308 Successful_Links.Increment_Last;
6309 Successful_Links.Table (Successful_Links.Last) :=
6310 Main_ALI_File;
6312 elsif Osint.Number_Of_Files = 1 or not Keep_Going then
6313 Make_Failed ("*** link failed.");
6315 else
6316 Set_Standard_Error;
6317 Write_Line ("*** link failed");
6319 if Commands_To_Stdout then
6320 Set_Standard_Output;
6321 end if;
6323 Failed_Links.Increment_Last;
6324 Failed_Links.Table (Failed_Links.Last) :=
6325 Main_ALI_File;
6326 end if;
6327 end;
6328 end;
6330 Linker_Switches.Set_Last (Linker_Switches_Last);
6331 end Link_Step;
6332 end if;
6334 -- We go to here when we skip the bind and link steps
6336 <<Next_Main>>
6338 -- We go to the next main, if we did not process the last one
6340 if N_File < Osint.Number_Of_Files then
6341 Main_Source_File := Next_Main_Source;
6343 if Current_File_Index /= No_Index then
6344 Main_Index := Current_File_Index;
6345 end if;
6347 if Main_Project /= No_Project then
6349 -- Find the file name of the main unit
6351 declare
6352 Main_Source_File_Name : constant String :=
6353 Get_Name_String (Main_Source_File);
6355 Main_Unit_File_Name : constant String :=
6356 Prj.Env.
6357 File_Name_Of_Library_Unit_Body
6358 (Name => Main_Source_File_Name,
6359 Project => Main_Project,
6360 In_Tree => Project_Tree,
6361 Main_Project_Only =>
6362 not Unique_Compile);
6364 The_Packages : constant Package_Id :=
6365 Project_Tree.Projects.Table
6366 (Main_Project).Decl.Packages;
6368 Binder_Package : constant Prj.Package_Id :=
6369 Prj.Util.Value_Of
6370 (Name => Name_Binder,
6371 In_Packages => The_Packages,
6372 In_Tree => Project_Tree);
6374 Linker_Package : constant Prj.Package_Id :=
6375 Prj.Util.Value_Of
6376 (Name => Name_Linker,
6377 In_Packages => The_Packages,
6378 In_Tree => Project_Tree);
6380 begin
6381 -- We fail if we cannot find the main source file
6382 -- as an immediate source of the main project file.
6384 if Main_Unit_File_Name = "" then
6385 Make_Failed ('"' & Main_Source_File_Name,
6386 """ is not a unit of project ",
6387 Project_File_Name.all & ".");
6389 else
6390 -- Remove any directory information from the main
6391 -- source file name.
6393 declare
6394 Pos : Natural := Main_Unit_File_Name'Last;
6396 begin
6397 loop
6398 exit when Pos < Main_Unit_File_Name'First
6399 or else
6400 Main_Unit_File_Name (Pos) = Directory_Separator;
6401 Pos := Pos - 1;
6402 end loop;
6404 Name_Len := Main_Unit_File_Name'Last - Pos;
6406 Name_Buffer (1 .. Name_Len) :=
6407 Main_Unit_File_Name
6408 (Pos + 1 .. Main_Unit_File_Name'Last);
6410 Main_Source_File := Name_Find;
6411 end;
6412 end if;
6414 -- We now deal with the binder and linker switches.
6415 -- If no project file is used, there is nothing to do
6416 -- because the binder and linker switches are the same
6417 -- for all mains.
6419 -- Reset the tables Binder_Switches and Linker_Switches
6421 Binder_Switches.Set_Last (Last_Binder_Switch);
6422 Linker_Switches.Set_Last (Last_Linker_Switch);
6424 -- Add binder switches from the project file for this main,
6425 -- if any.
6427 if Do_Bind_Step and Binder_Package /= No_Package then
6428 if Verbose_Mode then
6429 Write_Str ("Adding binder switches for """);
6430 Write_Str (Main_Unit_File_Name);
6431 Write_Line (""".");
6432 end if;
6434 Add_Switches
6435 (File_Name => Main_Unit_File_Name,
6436 Index => Main_Index,
6437 The_Package => Binder_Package,
6438 Program => Binder);
6439 end if;
6441 -- Add linker switches from the project file for this main,
6442 -- if any.
6444 if Do_Link_Step and Linker_Package /= No_Package then
6445 if Verbose_Mode then
6446 Write_Str ("Adding linker switches for""");
6447 Write_Str (Main_Unit_File_Name);
6448 Write_Line (""".");
6449 end if;
6451 Add_Switches
6452 (File_Name => Main_Unit_File_Name,
6453 Index => Main_Index,
6454 The_Package => Linker_Package,
6455 Program => Linker);
6456 end if;
6458 -- As we are using a project file, for relative paths we add
6459 -- the current working directory for any relative path on
6460 -- the command line and the project directory, for any
6461 -- relative path in the project file.
6463 declare
6464 Dir_Path : constant String_Access :=
6465 new String'(Get_Name_String
6466 (Project_Tree.Projects.Table
6467 (Main_Project).Directory.Name));
6468 begin
6470 J in Last_Binder_Switch + 1 .. Binder_Switches.Last
6471 loop
6472 Test_If_Relative_Path
6473 (Binder_Switches.Table (J),
6474 Parent => Dir_Path, Including_L_Switch => False);
6475 end loop;
6478 J in Last_Linker_Switch + 1 .. Linker_Switches.Last
6479 loop
6480 Test_If_Relative_Path
6481 (Linker_Switches.Table (J), Parent => Dir_Path);
6482 end loop;
6483 end;
6485 -- We now put in the Binder_Switches and Linker_Switches
6486 -- tables, the binder and linker switches of the command
6487 -- line that have been put in the Saved_ tables.
6488 -- These switches will follow the project file switches.
6490 for J in 1 .. Saved_Binder_Switches.Last loop
6491 Add_Switch
6492 (Saved_Binder_Switches.Table (J),
6493 Binder,
6494 And_Save => False);
6495 end loop;
6497 for J in 1 .. Saved_Linker_Switches.Last loop
6498 Add_Switch
6499 (Saved_Linker_Switches.Table (J),
6500 Linker,
6501 And_Save => False);
6502 end loop;
6503 end;
6504 end if;
6505 end if;
6507 -- Remove all marks to be sure to check sources for all executables,
6508 -- as the switches may be different and -s may be in use.
6510 Delete_All_Marks;
6511 end loop Multiple_Main_Loop;
6513 if Failed_Links.Last > 0 then
6514 for Index in 1 .. Successful_Links.Last loop
6515 Write_Str ("Linking of """);
6516 Write_Str (Get_Name_String (Successful_Links.Table (Index)));
6517 Write_Line (""" succeeded.");
6518 end loop;
6520 Set_Standard_Error;
6522 for Index in 1 .. Failed_Links.Last loop
6523 Write_Str ("Linking of """);
6524 Write_Str (Get_Name_String (Failed_Links.Table (Index)));
6525 Write_Line (""" failed.");
6526 end loop;
6528 if Commands_To_Stdout then
6529 Set_Standard_Output;
6530 end if;
6532 if Total_Compilation_Failures = 0 then
6533 Report_Compilation_Failed;
6534 end if;
6535 end if;
6537 if Total_Compilation_Failures /= 0 then
6538 List_Bad_Compilations;
6539 Report_Compilation_Failed;
6540 end if;
6542 -- Delete the temporary mapping file that was created if we are
6543 -- using project files.
6545 Delete_All_Temp_Files;
6547 exception
6548 when X : others =>
6549 Set_Standard_Error;
6550 Write_Line (Exception_Information (X));
6551 Make_Failed ("INTERNAL ERROR. Please report.");
6552 end Gnatmake;
6554 ----------
6555 -- Hash --
6556 ----------
6558 function Hash (F : File_Name_Type) return Header_Num is
6559 begin
6560 return Header_Num (1 + F mod Max_Header);
6561 end Hash;
6563 --------------------
6564 -- In_Ada_Lib_Dir --
6565 --------------------
6567 function In_Ada_Lib_Dir (File : File_Name_Type) return Boolean is
6568 D : constant File_Name_Type := Get_Directory (File);
6569 B : constant Byte := Get_Name_Table_Byte (D);
6570 begin
6571 return (B and Ada_Lib_Dir) /= 0;
6572 end In_Ada_Lib_Dir;
6574 -----------------------
6575 -- Init_Mapping_File --
6576 -----------------------
6578 procedure Init_Mapping_File
6579 (Project : Project_Id;
6580 File_Index : in out Natural)
6582 FD : File_Descriptor;
6583 Status : Boolean;
6584 -- For call to Close
6586 begin
6587 -- Increase the index of the last mapping file for this project
6589 Last_Mapping_File_Names (Project) :=
6590 Last_Mapping_File_Names (Project) + 1;
6592 -- If there is a project file, call Create_Mapping_File with
6593 -- the project id.
6595 if Project /= No_Project then
6596 Prj.Env.Create_Mapping_File
6597 (Project, Project_Tree,
6598 The_Mapping_File_Names
6599 (Project, Last_Mapping_File_Names (Project)));
6601 -- Otherwise, just create an empty file
6603 else
6604 Tempdir.Create_Temp_File
6605 (FD,
6606 The_Mapping_File_Names
6607 (No_Project, Last_Mapping_File_Names (No_Project)));
6609 if FD = Invalid_FD then
6610 Make_Failed ("disk full");
6612 else
6613 Record_Temp_File
6614 (The_Mapping_File_Names
6615 (No_Project, Last_Mapping_File_Names (No_Project)));
6616 end if;
6618 Close (FD, Status);
6620 if not Status then
6621 Make_Failed ("disk full");
6622 end if;
6623 end if;
6625 -- And return the index of the newly created file
6627 File_Index := Last_Mapping_File_Names (Project);
6628 end Init_Mapping_File;
6630 ------------
6631 -- Init_Q --
6632 ------------
6634 procedure Init_Q is
6635 begin
6636 First_Q_Initialization := False;
6637 Q_Front := Q.First;
6638 Q.Set_Last (Q.First);
6639 end Init_Q;
6641 ----------------
6642 -- Initialize --
6643 ----------------
6645 procedure Initialize is
6647 procedure Check_Version_And_Help is
6648 new Check_Version_And_Help_G (Makeusg);
6650 -- Start of processing for Initialize
6652 begin
6653 Prj.Set_Mode (Ada_Only);
6655 -- Override default initialization of Check_Object_Consistency since
6656 -- this is normally False for GNATBIND, but is True for GNATMAKE since
6657 -- we do not need to check source consistency again once GNATMAKE has
6658 -- looked at the sources to check.
6660 Check_Object_Consistency := True;
6662 -- Package initializations. The order of calls is important here
6664 Output.Set_Standard_Error;
6666 Gcc_Switches.Init;
6667 Binder_Switches.Init;
6668 Linker_Switches.Init;
6670 Csets.Initialize;
6671 Namet.Initialize;
6673 Snames.Initialize;
6675 Prj.Initialize (Project_Tree);
6677 Dependencies.Init;
6679 RTS_Specified := null;
6681 Mains.Delete;
6683 -- Add the directory where gnatmake is invoked in front of the
6684 -- path, if gnatmake is invoked from a bin directory or with directory
6685 -- information. Only do this if the platform is not VMS, where the
6686 -- notion of path does not really exist.
6688 if not OpenVMS then
6689 declare
6690 Prefix : constant String := Executable_Prefix_Path;
6691 Command : constant String := Command_Name;
6693 begin
6694 if Prefix'Length > 0 then
6695 declare
6696 PATH : constant String :=
6697 Prefix & Directory_Separator & "bin" &
6698 Path_Separator &
6699 Getenv ("PATH").all;
6700 begin
6701 Setenv ("PATH", PATH);
6702 end;
6704 else
6705 for Index in reverse Command'Range loop
6706 if Command (Index) = Directory_Separator then
6707 declare
6708 Absolute_Dir : constant String :=
6709 Normalize_Pathname
6710 (Command (Command'First .. Index));
6711 PATH : constant String :=
6712 Absolute_Dir &
6713 Path_Separator &
6714 Getenv ("PATH").all;
6715 begin
6716 Setenv ("PATH", PATH);
6717 end;
6719 exit;
6720 end if;
6721 end loop;
6722 end if;
6723 end;
6724 end if;
6726 -- Scan the switches and arguments
6728 -- First, scan to detect --version and/or --help
6730 Check_Version_And_Help ("GNATMAKE", "1995");
6732 -- Scan again the switch and arguments, now that we are sure that they
6733 -- do not include --version or --help.
6735 Scan_Args : for Next_Arg in 1 .. Argument_Count loop
6736 Scan_Make_Arg (Argument (Next_Arg), And_Save => True);
6737 end loop Scan_Args;
6739 if Commands_To_Stdout then
6740 Set_Standard_Output;
6741 end if;
6743 if Usage_Requested then
6744 Usage;
6745 end if;
6747 -- Test for trailing -P switch
6749 if Project_File_Name_Present and then Project_File_Name = null then
6750 Make_Failed ("project file name missing after -P");
6752 -- Test for trailing -o switch
6754 elsif Output_File_Name_Present
6755 and then not Output_File_Name_Seen
6756 then
6757 Make_Failed ("output file name missing after -o");
6759 -- Test for trailing -D switch
6761 elsif Object_Directory_Present
6762 and then not Object_Directory_Seen then
6763 Make_Failed ("object directory missing after -D");
6764 end if;
6766 -- Test for simultaneity of -i and -D
6768 if Object_Directory_Path /= null and then In_Place_Mode then
6769 Make_Failed ("-i and -D cannot be used simultaneously");
6770 end if;
6772 -- Deal with -C= switch
6774 if Gnatmake_Mapping_File /= null then
6776 -- First, check compatibility with other switches
6778 if Project_File_Name /= null then
6779 Make_Failed ("-C= switch is not compatible with -P switch");
6781 elsif Saved_Maximum_Processes > 1 then
6782 Make_Failed ("-C= switch is not compatible with -jnnn switch");
6783 end if;
6785 Fmap.Initialize (Gnatmake_Mapping_File.all);
6786 Add_Switch
6787 ("-gnatem=" & Gnatmake_Mapping_File.all,
6788 Compiler,
6789 And_Save => True);
6790 end if;
6792 if Project_File_Name /= null then
6794 -- A project file was specified by a -P switch
6796 if Verbose_Mode then
6797 Write_Eol;
6798 Write_Str ("Parsing project file """);
6799 Write_Str (Project_File_Name.all);
6800 Write_Str (""".");
6801 Write_Eol;
6802 end if;
6804 -- Avoid looking in the current directory for ALI files
6806 -- Look_In_Primary_Dir := False;
6808 -- Set the project parsing verbosity to whatever was specified
6809 -- by a possible -vP switch.
6811 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
6813 -- Parse the project file.
6814 -- If there is an error, Main_Project will still be No_Project.
6816 Prj.Pars.Parse
6817 (Project => Main_Project,
6818 In_Tree => Project_Tree,
6819 Project_File_Name => Project_File_Name.all,
6820 Packages_To_Check => Packages_To_Check_By_Gnatmake);
6822 -- The parsing of project files may have changed the current output
6824 if Commands_To_Stdout then
6825 Set_Standard_Output;
6826 else
6827 Set_Standard_Error;
6828 end if;
6830 if Main_Project = No_Project then
6831 Make_Failed ("""", Project_File_Name.all, """ processing failed");
6832 end if;
6834 Create_Mapping_File := True;
6836 if Verbose_Mode then
6837 Write_Eol;
6838 Write_Str ("Parsing of project file """);
6839 Write_Str (Project_File_Name.all);
6840 Write_Str (""" is finished.");
6841 Write_Eol;
6842 end if;
6844 -- We add the source directories and the object directories
6845 -- to the search paths.
6847 Add_Source_Directories (Main_Project, Project_Tree);
6848 Add_Object_Directories (Main_Project, Project_Tree);
6850 -- Compute depth of each project
6852 for Proj in Project_Table.First ..
6853 Project_Table.Last (Project_Tree.Projects)
6854 loop
6855 Project_Tree.Projects.Table (Proj).Seen := False;
6856 Project_Tree.Projects.Table (Proj).Depth := 0;
6857 end loop;
6859 Recursive_Compute_Depth (Main_Project, Depth => 1);
6861 -- For each project compute the list of the projects it imports
6862 -- directly or indirectly.
6864 for Proj in Project_Table.First ..
6865 Project_Table.Last (Project_Tree.Projects)
6866 loop
6867 Compute_All_Imported_Projects (Proj);
6868 end loop;
6870 else
6872 Osint.Add_Default_Search_Dirs;
6874 -- Source file lookups should be cached for efficiency. Source files
6875 -- are not supposed to change. However, we do that now only if no
6876 -- project file is used; if a project file is used, we do it just
6877 -- after changing the directory to the object directory.
6879 Osint.Source_File_Data (Cache => True);
6881 -- Read gnat.adc file to initialize Fname.UF
6883 Fname.UF.Initialize;
6885 begin
6886 Fname.SF.Read_Source_File_Name_Pragmas;
6888 exception
6889 when Err : SFN_Scan.Syntax_Error_In_GNAT_ADC =>
6890 Make_Failed (Exception_Message (Err));
6891 end;
6892 end if;
6894 -- Make sure no project object directory is recorded
6896 Project_Of_Current_Object_Directory := No_Project;
6898 end Initialize;
6900 ----------------------------
6901 -- Insert_Project_Sources --
6902 ----------------------------
6904 procedure Insert_Project_Sources
6905 (The_Project : Project_Id;
6906 All_Projects : Boolean;
6907 Into_Q : Boolean)
6909 Put_In_Q : Boolean := Into_Q;
6910 Unit : Unit_Data;
6911 Sfile : File_Name_Type;
6912 Index : Int;
6914 Extending : constant Boolean :=
6915 Project_Tree.Projects.Table
6916 (The_Project).Extends /= No_Project;
6918 function Check_Project (P : Project_Id) return Boolean;
6919 -- Returns True if P is The_Project or a project extended by The_Project
6921 -------------------
6922 -- Check_Project --
6923 -------------------
6925 function Check_Project (P : Project_Id) return Boolean is
6926 begin
6927 if All_Projects or P = The_Project then
6928 return True;
6930 elsif Extending then
6931 declare
6932 Data : Project_Data :=
6933 Project_Tree.Projects.Table (The_Project);
6935 begin
6936 loop
6937 if P = Data.Extends then
6938 return True;
6939 end if;
6941 Data := Project_Tree.Projects.Table (Data.Extends);
6942 exit when Data.Extends = No_Project;
6943 end loop;
6944 end;
6945 end if;
6947 return False;
6948 end Check_Project;
6950 -- Start of processing for Insert_Project_Sources
6952 begin
6953 -- For all the sources in the project files,
6955 for Id in Unit_Table.First ..
6956 Unit_Table.Last (Project_Tree.Units)
6957 loop
6958 Unit := Project_Tree.Units.Table (Id);
6959 Sfile := No_File;
6960 Index := 0;
6962 -- If there is a source for the body, and the body has not been
6963 -- locally removed,
6965 if Unit.File_Names (Body_Part).Name /= No_File
6966 and then Unit.File_Names (Body_Part).Path.Name /= Slash
6967 then
6968 -- And it is a source for the specified project
6970 if Check_Project (Unit.File_Names (Body_Part).Project) then
6972 -- If we don't have a spec, we cannot consider the source
6973 -- if it is a subunit
6975 if Unit.File_Names (Specification).Name = No_File then
6976 declare
6977 Src_Ind : Source_File_Index;
6979 -- Here we are cheating a little bit: we don't want to
6980 -- use Sinput.L, because it depends on the GNAT tree
6981 -- (Atree, Sinfo, ...). So, we pretend that it is a
6982 -- project file, and we use Sinput.P.
6984 -- Source_File_Is_Subunit is just scanning through the
6985 -- file until it finds one of the reserved words
6986 -- separate, procedure, function, generic or package.
6987 -- Fortunately, these Ada reserved words are also
6988 -- reserved for project files.
6990 begin
6991 Src_Ind := Sinput.P.Load_Project_File
6992 (Get_Name_String
6993 (Unit.File_Names (Body_Part).Path.Name));
6995 -- If it is a subunit, discard it
6997 if Sinput.P.Source_File_Is_Subunit (Src_Ind) then
6998 Sfile := No_File;
6999 Index := 0;
7000 else
7001 Sfile := Unit.File_Names (Body_Part).Display_Name;
7002 Index := Unit.File_Names (Body_Part).Index;
7003 end if;
7004 end;
7006 else
7007 Sfile := Unit.File_Names (Body_Part).Display_Name;
7008 Index := Unit.File_Names (Body_Part).Index;
7009 end if;
7010 end if;
7012 elsif Unit.File_Names (Specification).Name /= No_File
7013 and then Unit.File_Names (Specification).Path.Name /= Slash
7014 and then Check_Project (Unit.File_Names (Specification).Project)
7015 then
7016 -- If there is no source for the body, but there is a source
7017 -- for the spec which has not been locally removed, then we take
7018 -- this one.
7020 Sfile := Unit.File_Names (Specification).Display_Name;
7021 Index := Unit.File_Names (Specification).Index;
7022 end if;
7024 -- If Put_In_Q is True, we insert into the Q
7026 if Put_In_Q then
7028 -- For the first source inserted into the Q, we need to initialize
7029 -- the Q, but not for the subsequent sources.
7031 if First_Q_Initialization then
7032 Init_Q;
7033 end if;
7035 -- And of course, we only insert in the Q if the source is not
7036 -- marked.
7038 if Sfile /= No_File and then not Is_Marked (Sfile, Index) then
7039 if Verbose_Mode then
7040 Write_Str ("Adding """);
7041 Write_Str (Get_Name_String (Sfile));
7042 Write_Line (""" to the queue");
7043 end if;
7045 Insert_Q (Sfile, Index => Index);
7046 Mark (Sfile, Index);
7047 end if;
7049 elsif Sfile /= No_File then
7051 -- If Put_In_Q is False, we add the source as it it were specified
7052 -- on the command line, and we set Put_In_Q to True, so that the
7053 -- following sources will be put directly in the queue. This will
7054 -- allow parallel compilation processes if -jx switch is used.
7056 if Verbose_Mode then
7057 Write_Str ("Adding """);
7058 Write_Str (Get_Name_String (Sfile));
7059 Write_Line (""" as if on the command line");
7060 end if;
7062 Osint.Add_File (Get_Name_String (Sfile), Index);
7063 Put_In_Q := True;
7065 -- As we may look into the Q later, ensure the Q has been
7066 -- initialized to avoid errors.
7068 if First_Q_Initialization then
7069 Init_Q;
7070 end if;
7071 end if;
7072 end loop;
7073 end Insert_Project_Sources;
7075 --------------
7076 -- Insert_Q --
7077 --------------
7079 procedure Insert_Q
7080 (Source_File : File_Name_Type;
7081 Source_Unit : Unit_Name_Type := No_Unit_Name;
7082 Index : Int := 0)
7084 begin
7085 if Debug.Debug_Flag_Q then
7086 Write_Str (" Q := Q + [ ");
7087 Write_Name (Source_File);
7089 if Index /= 0 then
7090 Write_Str (", ");
7091 Write_Int (Index);
7092 end if;
7094 Write_Str (" ] ");
7095 Write_Eol;
7096 end if;
7098 Q.Table (Q.Last) :=
7099 (File => Source_File,
7100 Unit => Source_Unit,
7101 Index => Index);
7102 Q.Increment_Last;
7103 end Insert_Q;
7105 ---------------------
7106 -- Is_In_Obsoleted --
7107 ---------------------
7109 function Is_In_Obsoleted (F : File_Name_Type) return Boolean is
7110 begin
7111 if F = No_File then
7112 return False;
7114 else
7115 declare
7116 Name : constant String := Get_Name_String (F);
7117 First : Natural;
7118 F2 : File_Name_Type;
7120 begin
7121 First := Name'Last;
7122 while First > Name'First
7123 and then Name (First - 1) /= Directory_Separator
7124 and then Name (First - 1) /= '/'
7125 loop
7126 First := First - 1;
7127 end loop;
7129 if First /= Name'First then
7130 Name_Len := 0;
7131 Add_Str_To_Name_Buffer (Name (First .. Name'Last));
7132 F2 := Name_Find;
7134 else
7135 F2 := F;
7136 end if;
7138 return Obsoleted.Get (F2);
7139 end;
7140 end if;
7141 end Is_In_Obsoleted;
7143 ----------------------------
7144 -- Is_In_Object_Directory --
7145 ----------------------------
7147 function Is_In_Object_Directory
7148 (Source_File : File_Name_Type;
7149 Full_Lib_File : File_Name_Type) return Boolean
7151 begin
7152 -- There is something to check only when using project files.
7153 -- Otherwise, this function returns True (last line of the function).
7155 if Main_Project /= No_Project then
7156 declare
7157 Source_File_Name : constant String :=
7158 Get_Name_String (Source_File);
7159 Saved_Verbosity : constant Verbosity := Current_Verbosity;
7160 Project : Project_Id := No_Project;
7161 Data : Project_Data;
7163 Path_Name : Path_Name_Type := No_Path;
7164 pragma Warnings (Off, Path_Name);
7166 begin
7167 -- Call Get_Reference to know the ultimate extending project of
7168 -- the source. Call it with verbosity default to avoid verbose
7169 -- messages.
7171 Current_Verbosity := Default;
7172 Prj.Env.Get_Reference
7173 (Source_File_Name => Source_File_Name,
7174 Project => Project,
7175 In_Tree => Project_Tree,
7176 Path => Path_Name);
7177 Current_Verbosity := Saved_Verbosity;
7179 -- If this source is in a project, check that the ALI file is
7180 -- in its object directory. If it is not, return False, so that
7181 -- the ALI file will not be skipped.
7183 if Project /= No_Project then
7184 Data := Project_Tree.Projects.Table (Project);
7186 declare
7187 Object_Directory : constant String :=
7188 Normalize_Pathname
7189 (Get_Name_String
7190 (Data.Object_Directory.Display_Name));
7192 Olast : Natural := Object_Directory'Last;
7194 Lib_File_Directory : constant String :=
7195 Normalize_Pathname (Dir_Name
7196 (Get_Name_String (Full_Lib_File)));
7198 Llast : Natural := Lib_File_Directory'Last;
7200 begin
7201 -- For directories, Normalize_Pathname may or may not put
7202 -- a directory separator at the end, depending on its input.
7203 -- Remove any last directory separator before comparison.
7204 -- Returns True only if the two directories are the same.
7206 if Object_Directory (Olast) = Directory_Separator then
7207 Olast := Olast - 1;
7208 end if;
7210 if Lib_File_Directory (Llast) = Directory_Separator then
7211 Llast := Llast - 1;
7212 end if;
7214 return Object_Directory (Object_Directory'First .. Olast) =
7215 Lib_File_Directory (Lib_File_Directory'First .. Llast);
7216 end;
7217 end if;
7218 end;
7219 end if;
7221 -- When the source is not in a project file, always return True
7223 return True;
7224 end Is_In_Object_Directory;
7226 ----------
7227 -- Link --
7228 ----------
7230 procedure Link
7231 (ALI_File : File_Name_Type;
7232 Args : Argument_List;
7233 Success : out Boolean)
7235 Link_Args : Argument_List (1 .. Args'Length + 1);
7237 begin
7238 Get_Name_String (ALI_File);
7239 Link_Args (1) := new String'(Name_Buffer (1 .. Name_Len));
7241 Link_Args (2 .. Args'Length + 1) := Args;
7243 GNAT.OS_Lib.Normalize_Arguments (Link_Args);
7245 Display (Gnatlink.all, Link_Args);
7247 if Gnatlink_Path = null then
7248 Make_Failed ("error, unable to locate ", Gnatlink.all);
7249 end if;
7251 GNAT.OS_Lib.Spawn (Gnatlink_Path.all, Link_Args, Success);
7252 end Link;
7254 ---------------------------
7255 -- List_Bad_Compilations --
7256 ---------------------------
7258 procedure List_Bad_Compilations is
7259 begin
7260 for J in Bad_Compilation.First .. Bad_Compilation.Last loop
7261 if Bad_Compilation.Table (J).File = No_File then
7262 null;
7263 elsif not Bad_Compilation.Table (J).Found then
7264 Inform (Bad_Compilation.Table (J).File, "not found");
7265 else
7266 Inform (Bad_Compilation.Table (J).File, "compilation error");
7267 end if;
7268 end loop;
7269 end List_Bad_Compilations;
7271 -----------------
7272 -- List_Depend --
7273 -----------------
7275 procedure List_Depend is
7276 Lib_Name : File_Name_Type;
7277 Obj_Name : File_Name_Type;
7278 Src_Name : File_Name_Type;
7280 Len : Natural;
7281 Line_Pos : Natural;
7282 Line_Size : constant := 77;
7284 begin
7285 Set_Standard_Output;
7287 for A in ALIs.First .. ALIs.Last loop
7288 Lib_Name := ALIs.Table (A).Afile;
7290 -- We have to provide the full library file name in In_Place_Mode
7292 if In_Place_Mode then
7293 Lib_Name := Full_Lib_File_Name (Lib_Name);
7294 end if;
7296 Obj_Name := Object_File_Name (Lib_Name);
7297 Write_Name (Obj_Name);
7298 Write_Str (" :");
7300 Get_Name_String (Obj_Name);
7301 Len := Name_Len;
7302 Line_Pos := Len + 2;
7304 for D in ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep loop
7305 Src_Name := Sdep.Table (D).Sfile;
7307 if Is_Internal_File_Name (Src_Name)
7308 and then not Check_Readonly_Files
7309 then
7310 null;
7311 else
7312 if not Quiet_Output then
7313 Src_Name := Full_Source_Name (Src_Name);
7314 end if;
7316 Get_Name_String (Src_Name);
7317 Len := Name_Len;
7319 if Line_Pos + Len + 1 > Line_Size then
7320 Write_Str (" \");
7321 Write_Eol;
7322 Line_Pos := 0;
7323 end if;
7325 Line_Pos := Line_Pos + Len + 1;
7327 Write_Str (" ");
7328 Write_Name (Src_Name);
7329 end if;
7330 end loop;
7332 Write_Eol;
7333 end loop;
7335 if not Commands_To_Stdout then
7336 Set_Standard_Error;
7337 end if;
7338 end List_Depend;
7340 -----------------
7341 -- Make_Failed --
7342 -----------------
7344 procedure Make_Failed (S1 : String; S2 : String := ""; S3 : String := "") is
7345 begin
7346 Delete_All_Temp_Files;
7347 Osint.Fail (S1, S2, S3);
7348 end Make_Failed;
7350 --------------------
7351 -- Mark_Directory --
7352 --------------------
7354 procedure Mark_Directory
7355 (Dir : String;
7356 Mark : Lib_Mark_Type;
7357 On_Command_Line : Boolean)
7359 N : Name_Id;
7360 B : Byte;
7362 begin
7363 if On_Command_Line then
7364 declare
7365 Real_Path : constant String := Normalize_Pathname (Dir);
7367 begin
7368 if Real_Path'Length = 0 then
7369 Name_Len := Dir'Length;
7370 Name_Buffer (1 .. Name_Len) := Dir;
7372 else
7373 Name_Len := Real_Path'Length;
7374 Name_Buffer (1 .. Name_Len) := Real_Path;
7375 end if;
7376 end;
7378 else
7379 declare
7380 Real_Path : constant String :=
7381 Normalize_Pathname
7382 (Dir,
7383 Get_Name_String
7384 (Project_Tree.Projects.Table
7385 (Main_Project).Directory.Display_Name));
7387 begin
7388 if Real_Path'Length = 0 then
7389 Name_Len := Dir'Length;
7390 Name_Buffer (1 .. Name_Len) := Dir;
7392 else
7393 Name_Len := Real_Path'Length;
7394 Name_Buffer (1 .. Name_Len) := Real_Path;
7395 end if;
7396 end;
7397 end if;
7399 -- Last character is supposed to be a directory separator
7401 if not Is_Directory_Separator (Name_Buffer (Name_Len)) then
7402 Name_Len := Name_Len + 1;
7403 Name_Buffer (Name_Len) := Directory_Separator;
7404 end if;
7406 -- Add flags to the already existing flags
7408 N := Name_Find;
7409 B := Get_Name_Table_Byte (N);
7410 Set_Name_Table_Byte (N, B or Mark);
7411 end Mark_Directory;
7413 -----------------------------
7414 -- Recursive_Compute_Depth --
7415 -----------------------------
7417 procedure Recursive_Compute_Depth
7418 (Project : Project_Id;
7419 Depth : Natural)
7421 List : Project_List;
7422 Proj : Project_Id;
7424 begin
7425 -- Nothing to do if there is no project or if the project has already
7426 -- been seen or if the depth is large enough.
7428 if Project = No_Project
7429 or else Project_Tree.Projects.Table (Project).Seen
7430 or else Project_Tree.Projects.Table (Project).Depth >= Depth
7431 then
7432 return;
7433 end if;
7435 Project_Tree.Projects.Table (Project).Depth := Depth;
7437 -- Mark project as Seen to avoid endless loop caused by limited withs
7439 Project_Tree.Projects.Table (Project).Seen := True;
7441 List := Project_Tree.Projects.Table (Project).Imported_Projects;
7443 -- Visit each imported project
7445 while List /= Empty_Project_List loop
7446 Proj := Project_Tree.Project_Lists.Table (List).Project;
7447 List := Project_Tree.Project_Lists.Table (List).Next;
7448 Recursive_Compute_Depth
7449 (Project => Proj,
7450 Depth => Depth + 1);
7451 end loop;
7453 -- Visit a project being extended, if any
7455 Recursive_Compute_Depth
7456 (Project => Project_Tree.Projects.Table (Project).Extends,
7457 Depth => Depth + 1);
7459 -- Reset the Seen flag, as we leave this project
7461 Project_Tree.Projects.Table (Project).Seen := False;
7462 end Recursive_Compute_Depth;
7464 -------------------------------
7465 -- Report_Compilation_Failed --
7466 -------------------------------
7468 procedure Report_Compilation_Failed is
7469 begin
7470 Delete_All_Temp_Files;
7471 Exit_Program (E_Fatal);
7472 end Report_Compilation_Failed;
7474 ------------------------
7475 -- Sigint_Intercepted --
7476 ------------------------
7478 procedure Sigint_Intercepted is
7479 SIGINT : constant := 2;
7480 begin
7481 Set_Standard_Error;
7482 Write_Line ("*** Interrupted ***");
7484 -- Send SIGINT to all outstanding compilation processes spawned
7486 for J in 1 .. Outstanding_Compiles loop
7487 Kill (Running_Compile (J).Pid, SIGINT, 1);
7488 end loop;
7490 Delete_All_Temp_Files;
7491 OS_Exit (1);
7492 -- ??? OS_Exit (1) is equivalent to Exit_Program (E_No_Compile),
7493 -- shouldn't that be Exit_Program (E_Abort) instead?
7494 end Sigint_Intercepted;
7496 -------------------
7497 -- Scan_Make_Arg --
7498 -------------------
7500 procedure Scan_Make_Arg (Argv : String; And_Save : Boolean) is
7501 Success : Boolean;
7503 begin
7504 pragma Assert (Argv'First = 1);
7506 if Argv'Length = 0 then
7507 return;
7508 end if;
7510 -- If the previous switch has set the Project_File_Name_Present flag
7511 -- (that is we have seen a -P alone), then the next argument is the name
7512 -- of the project file.
7514 if Project_File_Name_Present and then Project_File_Name = null then
7515 if Argv (1) = '-' then
7516 Make_Failed ("project file name missing after -P");
7518 else
7519 Project_File_Name_Present := False;
7520 Project_File_Name := new String'(Argv);
7521 end if;
7523 -- If the previous switch has set the Output_File_Name_Present flag
7524 -- (that is we have seen a -o), then the next argument is the name of
7525 -- the output executable.
7527 elsif Output_File_Name_Present
7528 and then not Output_File_Name_Seen
7529 then
7530 Output_File_Name_Seen := True;
7532 if Argv (1) = '-' then
7533 Make_Failed ("output file name missing after -o");
7535 else
7536 Add_Switch ("-o", Linker, And_Save => And_Save);
7537 Add_Switch (Executable_Name (Argv), Linker, And_Save => And_Save);
7538 end if;
7540 -- If the previous switch has set the Object_Directory_Present flag
7541 -- (that is we have seen a -D), then the next argument is the path name
7542 -- of the object directory..
7544 elsif Object_Directory_Present
7545 and then not Object_Directory_Seen
7546 then
7547 Object_Directory_Seen := True;
7549 if Argv (1) = '-' then
7550 Make_Failed ("object directory path name missing after -D");
7552 elsif not Is_Directory (Argv) then
7553 Make_Failed ("cannot find object directory """, Argv, """");
7555 else
7556 Add_Lib_Search_Dir (Argv);
7558 -- Specify the object directory to the binder
7560 Add_Switch ("-aO" & Argv, Binder, And_Save => And_Save);
7562 -- Record the object directory. Make sure it ends with a directory
7563 -- separator.
7565 if Argv (Argv'Last) = Directory_Separator then
7566 Object_Directory_Path :=
7567 new String'(Argv);
7568 else
7569 Object_Directory_Path :=
7570 new String'(Argv & Directory_Separator);
7571 end if;
7572 end if;
7574 -- Then check if we are dealing with -cargs/-bargs/-largs/-margs
7576 elsif Argv = "-bargs"
7577 or else
7578 Argv = "-cargs"
7579 or else
7580 Argv = "-largs"
7581 or else
7582 Argv = "-margs"
7583 then
7584 case Argv (2) is
7585 when 'c' => Program_Args := Compiler;
7586 when 'b' => Program_Args := Binder;
7587 when 'l' => Program_Args := Linker;
7588 when 'm' => Program_Args := None;
7590 when others =>
7591 raise Program_Error;
7592 end case;
7594 -- A special test is needed for the -o switch within a -largs
7595 -- since that is another way to specify the name of the final
7596 -- executable.
7598 elsif Program_Args = Linker
7599 and then Argv = "-o"
7600 then
7601 Make_Failed ("switch -o not allowed within a -largs. " &
7602 "Use -o directly.");
7604 -- Check to see if we are reading switches after a -cargs,
7605 -- -bargs or -largs switch. If yes save it.
7607 elsif Program_Args /= None then
7609 -- Check to see if we are reading -I switches in order
7610 -- to take into account in the src & lib search directories.
7612 if Argv'Length > 2 and then Argv (1 .. 2) = "-I" then
7613 if Argv (3 .. Argv'Last) = "-" then
7614 Look_In_Primary_Dir := False;
7616 elsif Program_Args = Compiler then
7617 if Argv (3 .. Argv'Last) /= "-" then
7618 Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7619 end if;
7621 elsif Program_Args = Binder then
7622 Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7623 end if;
7624 end if;
7626 Add_Switch (Argv, Program_Args, And_Save => And_Save);
7628 -- Handle non-default compiler, binder, linker, and handle --RTS switch
7630 elsif Argv'Length > 2 and then Argv (1 .. 2) = "--" then
7631 if Argv'Length > 6
7632 and then Argv (1 .. 6) = "--GCC="
7633 then
7634 declare
7635 Program_Args : constant Argument_List_Access :=
7636 Argument_String_To_List
7637 (Argv (7 .. Argv'Last));
7639 begin
7640 if And_Save then
7641 Saved_Gcc := new String'(Program_Args.all (1).all);
7642 else
7643 Gcc := new String'(Program_Args.all (1).all);
7644 end if;
7646 for J in 2 .. Program_Args.all'Last loop
7647 Add_Switch
7648 (Program_Args.all (J).all,
7649 Compiler,
7650 And_Save => And_Save);
7651 end loop;
7652 end;
7654 elsif Argv'Length > 11
7655 and then Argv (1 .. 11) = "--GNATBIND="
7656 then
7657 declare
7658 Program_Args : constant Argument_List_Access :=
7659 Argument_String_To_List
7660 (Argv (12 .. Argv'Last));
7662 begin
7663 if And_Save then
7664 Saved_Gnatbind := new String'(Program_Args.all (1).all);
7665 else
7666 Gnatbind := new String'(Program_Args.all (1).all);
7667 end if;
7669 for J in 2 .. Program_Args.all'Last loop
7670 Add_Switch
7671 (Program_Args.all (J).all, Binder, And_Save => And_Save);
7672 end loop;
7673 end;
7675 elsif Argv'Length > 11
7676 and then Argv (1 .. 11) = "--GNATLINK="
7677 then
7678 declare
7679 Program_Args : constant Argument_List_Access :=
7680 Argument_String_To_List
7681 (Argv (12 .. Argv'Last));
7682 begin
7683 if And_Save then
7684 Saved_Gnatlink := new String'(Program_Args.all (1).all);
7685 else
7686 Gnatlink := new String'(Program_Args.all (1).all);
7687 end if;
7689 for J in 2 .. Program_Args.all'Last loop
7690 Add_Switch (Program_Args.all (J).all, Linker);
7691 end loop;
7692 end;
7694 elsif Argv'Length >= 5 and then
7695 Argv (1 .. 5) = "--RTS"
7696 then
7697 Add_Switch (Argv, Compiler, And_Save => And_Save);
7698 Add_Switch (Argv, Binder, And_Save => And_Save);
7700 if Argv'Length <= 6 or else Argv (6) /= '=' then
7701 Make_Failed ("missing path for --RTS");
7703 else
7704 -- Check that this is the first time we see this switch or
7705 -- if it is not the first time, the same path is specified.
7707 if RTS_Specified = null then
7708 RTS_Specified := new String'(Argv (7 .. Argv'Last));
7710 elsif RTS_Specified.all /= Argv (7 .. Argv'Last) then
7711 Make_Failed ("--RTS cannot be specified multiple times");
7712 end if;
7714 -- Valid --RTS switch
7716 No_Stdinc := True;
7717 No_Stdlib := True;
7718 RTS_Switch := True;
7720 declare
7721 Src_Path_Name : constant String_Ptr :=
7722 Get_RTS_Search_Dir
7723 (Argv (7 .. Argv'Last), Include);
7725 Lib_Path_Name : constant String_Ptr :=
7726 Get_RTS_Search_Dir
7727 (Argv (7 .. Argv'Last), Objects);
7729 begin
7730 if Src_Path_Name /= null
7731 and then Lib_Path_Name /= null
7732 then
7733 -- Set RTS_*_Path_Name variables, so that correct direct-
7734 -- ories will be set when Osint.Add_Default_Search_Dirs
7735 -- is called later.
7737 RTS_Src_Path_Name := Src_Path_Name;
7738 RTS_Lib_Path_Name := Lib_Path_Name;
7740 elsif Src_Path_Name = null
7741 and Lib_Path_Name = null
7742 then
7743 Make_Failed ("RTS path not valid: missing " &
7744 "adainclude and adalib directories");
7746 elsif Src_Path_Name = null then
7747 Make_Failed ("RTS path not valid: missing adainclude " &
7748 "directory");
7750 elsif Lib_Path_Name = null then
7751 Make_Failed ("RTS path not valid: missing adalib " &
7752 "directory");
7753 end if;
7754 end;
7755 end if;
7757 else
7758 Scan_Make_Switches (Argv, Success);
7759 end if;
7761 -- If we have seen a regular switch process it
7763 elsif Argv (1) = '-' then
7764 if Argv'Length = 1 then
7765 Make_Failed ("switch character cannot be followed by a blank");
7767 -- Incorrect switches that should start with "--"
7769 elsif (Argv'Length > 5 and then Argv (1 .. 5) = "-RTS=")
7770 or else (Argv'Length > 5 and then Argv (1 .. 5) = "-GCC=")
7771 or else (Argv'Length > 10 and then Argv (1 .. 10) = "-GNATLINK=")
7772 or else (Argv'Length > 10 and then Argv (1 .. 10) = "-GNATBIND=")
7773 then
7774 Make_Failed ("option ", Argv, " should start with '--'");
7776 -- -I-
7778 elsif Argv (2 .. Argv'Last) = "I-" then
7779 Look_In_Primary_Dir := False;
7781 -- Forbid -?- or -??- where ? is any character
7783 elsif (Argv'Length = 3 and then Argv (3) = '-')
7784 or else (Argv'Length = 4 and then Argv (4) = '-')
7785 then
7786 Make_Failed ("trailing ""-"" at the end of ", Argv, " forbidden.");
7788 -- -Idir
7790 elsif Argv (2) = 'I' then
7791 Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7792 Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7793 Add_Switch (Argv, Compiler, And_Save => And_Save);
7794 Add_Switch (Argv, Binder, And_Save => And_Save);
7796 -- -aIdir (to gcc this is like a -I switch)
7798 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aI" then
7799 Add_Source_Search_Dir (Argv (4 .. Argv'Last), And_Save);
7800 Add_Switch ("-I" & Argv (4 .. Argv'Last),
7801 Compiler,
7802 And_Save => And_Save);
7803 Add_Switch (Argv, Binder, And_Save => And_Save);
7805 -- -aOdir
7807 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aO" then
7808 Add_Library_Search_Dir (Argv (4 .. Argv'Last), And_Save);
7809 Add_Switch (Argv, Binder, And_Save => And_Save);
7811 -- -aLdir (to gnatbind this is like a -aO switch)
7813 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aL" then
7814 Mark_Directory (Argv (4 .. Argv'Last), Ada_Lib_Dir, And_Save);
7815 Add_Library_Search_Dir (Argv (4 .. Argv'Last), And_Save);
7816 Add_Switch ("-aO" & Argv (4 .. Argv'Last),
7817 Binder,
7818 And_Save => And_Save);
7820 -- -aamp_target=...
7822 elsif Argv'Length >= 13 and then Argv (2 .. 13) = "aamp_target=" then
7823 Add_Switch (Argv, Compiler, And_Save => And_Save);
7825 -- Set the aamp_target environment variable so that the binder and
7826 -- linker will use the proper target library. This is consistent
7827 -- with how things work when -aamp_target is passed on the command
7828 -- line to gnaampmake.
7830 Setenv ("aamp_target", Argv (14 .. Argv'Last));
7832 -- -Adir (to gnatbind this is like a -aO switch, to gcc like a -I)
7834 elsif Argv (2) = 'A' then
7835 Mark_Directory (Argv (3 .. Argv'Last), Ada_Lib_Dir, And_Save);
7836 Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7837 Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7838 Add_Switch ("-I" & Argv (3 .. Argv'Last),
7839 Compiler,
7840 And_Save => And_Save);
7841 Add_Switch ("-aO" & Argv (3 .. Argv'Last),
7842 Binder,
7843 And_Save => And_Save);
7845 -- -Ldir
7847 elsif Argv (2) = 'L' then
7848 Add_Switch (Argv, Linker, And_Save => And_Save);
7850 -- For -gxxxxx, -pg, -mxxx, -fxxx: give the switch to both the
7851 -- compiler and the linker (except for -gnatxxx which is only for
7852 -- the compiler). Some of the -mxxx (for example -m64) and -fxxx
7853 -- (for example -ftest-coverage for gcov) need to be used when
7854 -- compiling the binder generated files, and using all these gcc
7855 -- switches for the binder generated files should not be a problem.
7857 elsif
7858 (Argv (2) = 'g' and then (Argv'Last < 5
7859 or else Argv (2 .. 5) /= "gnat"))
7860 or else Argv (2 .. Argv'Last) = "pg"
7861 or else (Argv (2) = 'm' and then Argv'Last > 2)
7862 or else (Argv (2) = 'f' and then Argv'Last > 2)
7863 then
7864 Add_Switch (Argv, Compiler, And_Save => And_Save);
7865 Add_Switch (Argv, Linker, And_Save => And_Save);
7867 -- -C=<mapping file>
7869 elsif Argv'Last > 2 and then Argv (2) = 'C' then
7870 if And_Save then
7871 if Argv (3) /= '=' or else Argv'Last <= 3 then
7872 Make_Failed ("illegal switch ", Argv);
7873 end if;
7875 Gnatmake_Mapping_File := new String'(Argv (4 .. Argv'Last));
7876 end if;
7878 -- -D
7880 elsif Argv'Last = 2 and then Argv (2) = 'D' then
7881 if Project_File_Name /= null then
7882 Make_Failed ("-D cannot be used in conjunction with a " &
7883 "project file");
7885 else
7886 Scan_Make_Switches (Argv, Success);
7887 end if;
7889 -- -d
7891 elsif Argv (2) = 'd'
7892 and then Argv'Last = 2
7893 then
7894 Display_Compilation_Progress := True;
7896 -- -i
7898 elsif Argv'Last = 2 and then Argv (2) = 'i' then
7899 if Project_File_Name /= null then
7900 Make_Failed ("-i cannot be used in conjunction with a " &
7901 "project file");
7902 else
7903 Scan_Make_Switches (Argv, Success);
7904 end if;
7906 -- -j (need to save the result)
7908 elsif Argv (2) = 'j' then
7909 Scan_Make_Switches (Argv, Success);
7911 if And_Save then
7912 Saved_Maximum_Processes := Maximum_Processes;
7913 end if;
7915 -- -m
7917 elsif Argv (2) = 'm'
7918 and then Argv'Last = 2
7919 then
7920 Minimal_Recompilation := True;
7922 -- -u
7924 elsif Argv (2) = 'u'
7925 and then Argv'Last = 2
7926 then
7927 Unique_Compile := True;
7928 Compile_Only := True;
7929 Do_Bind_Step := False;
7930 Do_Link_Step := False;
7932 -- -U
7934 elsif Argv (2) = 'U'
7935 and then Argv'Last = 2
7936 then
7937 Unique_Compile_All_Projects := True;
7938 Unique_Compile := True;
7939 Compile_Only := True;
7940 Do_Bind_Step := False;
7941 Do_Link_Step := False;
7943 -- -Pprj or -P prj (only once, and only on the command line)
7945 elsif Argv (2) = 'P' then
7946 if Project_File_Name /= null then
7947 Make_Failed ("cannot have several project files specified");
7949 elsif Object_Directory_Path /= null then
7950 Make_Failed ("-D cannot be used in conjunction with a " &
7951 "project file");
7953 elsif In_Place_Mode then
7954 Make_Failed ("-i cannot be used in conjunction with a " &
7955 "project file");
7957 elsif not And_Save then
7959 -- It could be a tool other than gnatmake (i.e, gnatdist)
7960 -- or a -P switch inside a project file.
7962 Fail
7963 ("either the tool is not ""project-aware"" or " &
7964 "a project file is specified inside a project file");
7966 elsif Argv'Last = 2 then
7968 -- -P is used alone: the project file name is the next option
7970 Project_File_Name_Present := True;
7972 else
7973 Project_File_Name := new String'(Argv (3 .. Argv'Last));
7974 end if;
7976 -- -vPx (verbosity of the parsing of the project files)
7978 elsif Argv'Last = 4
7979 and then Argv (2 .. 3) = "vP"
7980 and then Argv (4) in '0' .. '2'
7981 then
7982 if And_Save then
7983 case Argv (4) is
7984 when '0' =>
7985 Current_Verbosity := Prj.Default;
7986 when '1' =>
7987 Current_Verbosity := Prj.Medium;
7988 when '2' =>
7989 Current_Verbosity := Prj.High;
7990 when others =>
7991 null;
7992 end case;
7993 end if;
7995 -- -Xext=val (External assignment)
7997 elsif Argv (2) = 'X'
7998 and then Is_External_Assignment (Argv)
7999 then
8000 -- Is_External_Assignment has side effects
8001 -- when it returns True;
8003 null;
8005 -- If -gnath is present, then generate the usage information
8006 -- right now and do not pass this option on to the compiler calls.
8008 elsif Argv = "-gnath" then
8009 Usage;
8011 -- If -gnatc is specified, make sure the bind step and the link
8012 -- step are not executed.
8014 elsif Argv'Length >= 6 and then Argv (2 .. 6) = "gnatc" then
8016 -- If -gnatc is specified, make sure the bind step and the link
8017 -- step are not executed.
8019 Add_Switch (Argv, Compiler, And_Save => And_Save);
8020 Operating_Mode := Check_Semantics;
8021 Check_Object_Consistency := False;
8022 Compile_Only := True;
8023 Do_Bind_Step := False;
8024 Do_Link_Step := False;
8026 elsif Argv (2 .. Argv'Last) = "nostdlib" then
8028 -- Don't pass -nostdlib to gnatlink, it will disable
8029 -- linking with all standard library files.
8031 No_Stdlib := True;
8033 Add_Switch (Argv, Compiler, And_Save => And_Save);
8034 Add_Switch (Argv, Binder, And_Save => And_Save);
8036 elsif Argv (2 .. Argv'Last) = "nostdinc" then
8038 -- Pass -nostdinc to the Compiler and to gnatbind
8040 No_Stdinc := True;
8041 Add_Switch (Argv, Compiler, And_Save => And_Save);
8042 Add_Switch (Argv, Binder, And_Save => And_Save);
8044 -- All other switches are processed by Scan_Make_Switches.
8045 -- If the call returns with Success = False, then the switch is
8046 -- passed to the compiler.
8048 else
8049 Scan_Make_Switches (Argv, Success);
8051 if not Success then
8052 Add_Switch (Argv, Compiler, And_Save => And_Save);
8053 end if;
8054 end if;
8056 -- If not a switch it must be a file name
8058 else
8059 Add_File (Argv);
8060 Mains.Add_Main (Argv);
8061 end if;
8062 end Scan_Make_Arg;
8064 -----------------
8065 -- Switches_Of --
8066 -----------------
8068 function Switches_Of
8069 (Source_File : File_Name_Type;
8070 Source_File_Name : String;
8071 Source_Index : Int;
8072 Naming : Naming_Data;
8073 In_Package : Package_Id;
8074 Allow_ALI : Boolean) return Variable_Value
8076 Switches : Variable_Value;
8078 Defaults : constant Array_Element_Id :=
8079 Prj.Util.Value_Of
8080 (Name => Name_Default_Switches,
8081 In_Arrays =>
8082 Project_Tree.Packages.Table
8083 (In_Package).Decl.Arrays,
8084 In_Tree => Project_Tree);
8086 Switches_Array : constant Array_Element_Id :=
8087 Prj.Util.Value_Of
8088 (Name => Name_Switches,
8089 In_Arrays =>
8090 Project_Tree.Packages.Table
8091 (In_Package).Decl.Arrays,
8092 In_Tree => Project_Tree);
8094 begin
8095 Switches :=
8096 Prj.Util.Value_Of
8097 (Index => Name_Id (Source_File),
8098 Src_Index => Source_Index,
8099 In_Array => Switches_Array,
8100 In_Tree => Project_Tree);
8102 if Switches = Nil_Variable_Value then
8103 declare
8104 Name : String (1 .. Source_File_Name'Length + 3);
8105 Last : Positive := Source_File_Name'Length;
8106 Spec_Suffix : constant String :=
8107 Spec_Suffix_Of (Project_Tree, "ada", Naming);
8108 Body_Suffix : constant String :=
8109 Body_Suffix_Of (Project_Tree, "ada", Naming);
8110 Truncated : Boolean := False;
8112 begin
8113 Name (1 .. Last) := Source_File_Name;
8115 if Last > Body_Suffix'Length
8116 and then Name (Last - Body_Suffix'Length + 1 .. Last) =
8117 Body_Suffix
8118 then
8119 Truncated := True;
8120 Last := Last - Body_Suffix'Length;
8121 end if;
8123 if not Truncated
8124 and then Last > Spec_Suffix'Length
8125 and then Name (Last - Spec_Suffix'Length + 1 .. Last) =
8126 Spec_Suffix
8127 then
8128 Truncated := True;
8129 Last := Last - Spec_Suffix'Length;
8130 end if;
8132 if Truncated then
8133 Name_Len := Last;
8134 Name_Buffer (1 .. Name_Len) := Name (1 .. Last);
8135 Switches :=
8136 Prj.Util.Value_Of
8137 (Index => Name_Find,
8138 Src_Index => 0,
8139 In_Array => Switches_Array,
8140 In_Tree => Project_Tree);
8142 if Switches = Nil_Variable_Value
8143 and then Allow_ALI
8144 then
8145 Last := Source_File_Name'Length;
8147 while Name (Last) /= '.' loop
8148 Last := Last - 1;
8149 end loop;
8151 Name (Last + 1 .. Last + 3) := "ali";
8152 Name_Len := Last + 3;
8153 Name_Buffer (1 .. Name_Len) := Name (1 .. Name_Len);
8154 Switches :=
8155 Prj.Util.Value_Of
8156 (Index => Name_Find,
8157 Src_Index => 0,
8158 In_Array => Switches_Array,
8159 In_Tree => Project_Tree);
8160 end if;
8161 end if;
8162 end;
8163 end if;
8165 if Switches = Nil_Variable_Value then
8166 Switches :=
8167 Prj.Util.Value_Of
8168 (Index => Name_Ada,
8169 Src_Index => 0,
8170 In_Array => Defaults,
8171 In_Tree => Project_Tree);
8172 end if;
8174 return Switches;
8175 end Switches_Of;
8177 -----------
8178 -- Usage --
8179 -----------
8181 procedure Usage is
8182 begin
8183 if Usage_Needed then
8184 Usage_Needed := False;
8185 Makeusg;
8186 end if;
8187 end Usage;
8189 -----------------
8190 -- Verbose_Msg --
8191 -----------------
8193 procedure Verbose_Msg
8194 (N1 : Name_Id;
8195 S1 : String;
8196 N2 : Name_Id := No_Name;
8197 S2 : String := "";
8198 Prefix : String := " -> ";
8199 Minimum_Verbosity : Verbosity_Level_Type := Opt.Low)
8201 begin
8202 if (not Verbose_Mode) or else (Minimum_Verbosity > Verbosity_Level) then
8203 return;
8204 end if;
8206 Write_Str (Prefix);
8207 Write_Str ("""");
8208 Write_Name (N1);
8209 Write_Str (""" ");
8210 Write_Str (S1);
8212 if N2 /= No_Name then
8213 Write_Str (" """);
8214 Write_Name (N2);
8215 Write_Str (""" ");
8216 end if;
8218 Write_Str (S2);
8219 Write_Eol;
8220 end Verbose_Msg;
8222 procedure Verbose_Msg
8223 (N1 : File_Name_Type;
8224 S1 : String;
8225 N2 : File_Name_Type := No_File;
8226 S2 : String := "";
8227 Prefix : String := " -> ";
8228 Minimum_Verbosity : Verbosity_Level_Type := Opt.Low)
8230 begin
8231 Verbose_Msg
8232 (Name_Id (N1), S1, Name_Id (N2), S2, Prefix, Minimum_Verbosity);
8233 end Verbose_Msg;
8235 begin
8236 -- Make sure that in case of failure, the temp files will be deleted
8238 Prj.Com.Fail := Make_Failed'Access;
8239 MLib.Fail := Make_Failed'Access;
8240 Makeutl.Do_Fail := Make_Failed'Access;
8241 end Make;