2009-10-01 Tobias Burnus <burnus@net-b.de>
[official-gcc/alias-decl.git] / gcc / ada / make.adb
blobdacf290c2732f7fe28b163fdbaf54f7ed1196e59
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- M A K E --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2009, 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 Errutil;
31 with Fmap;
32 with Fname; use Fname;
33 with Fname.SF; use Fname.SF;
34 with Fname.UF; use Fname.UF;
35 with Gnatvsn; use Gnatvsn;
36 with Hostparm; use Hostparm;
37 with Makeusg;
38 with Makeutl; use Makeutl;
39 with MLib;
40 with MLib.Prj;
41 with MLib.Tgt; use MLib.Tgt;
42 with MLib.Utl;
43 with Namet; use Namet;
44 with Opt; use Opt;
45 with Osint.M; use Osint.M;
46 with Osint; use Osint;
47 with Output; use Output;
48 with Prj; use Prj;
49 with Prj.Com;
50 with Prj.Env;
51 with Prj.Pars;
52 with Prj.Tree; use Prj.Tree;
53 with Prj.Util;
54 with SFN_Scan;
55 with Sinput.P;
56 with Snames; use Snames;
58 pragma Warnings (Off);
59 with System.HTable;
60 pragma Warnings (On);
62 with Switch; use Switch;
63 with Switch.M; use Switch.M;
64 with Targparm; use Targparm;
65 with Table;
66 with Tempdir;
67 with Types; use Types;
69 with Ada.Exceptions; use Ada.Exceptions;
70 with Ada.Command_Line; use Ada.Command_Line;
72 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
73 with GNAT.Dynamic_HTables; use GNAT.Dynamic_HTables;
74 with GNAT.Case_Util; use GNAT.Case_Util;
75 with GNAT.OS_Lib; use GNAT.OS_Lib;
77 package body Make is
79 use ASCII;
80 -- Make control characters visible
82 Standard_Library_Package_Body_Name : constant String := "s-stalib.adb";
83 -- Every program depends on this package, that must then be checked,
84 -- especially when -f and -a are used.
86 procedure Kill (Pid : Process_Id; Sig_Num : Integer; Close : Integer);
87 pragma Import (C, Kill, "__gnat_kill");
88 -- Called by Sigint_Intercepted to kill all spawned compilation processes
90 type Sigint_Handler is access procedure;
91 pragma Convention (C, Sigint_Handler);
93 procedure Install_Int_Handler (Handler : Sigint_Handler);
94 pragma Import (C, Install_Int_Handler, "__gnat_install_int_handler");
95 -- Called by Gnatmake to install the SIGINT handler below
97 procedure Sigint_Intercepted;
98 pragma Convention (C, Sigint_Intercepted);
99 -- Called when the program is interrupted by Ctrl-C to delete the
100 -- temporary mapping files and configuration pragmas files.
102 No_Mapping_File : constant Natural := 0;
104 type Compilation_Data is record
105 Pid : Process_Id;
106 Full_Source_File : File_Name_Type;
107 Lib_File : File_Name_Type;
108 Source_Unit : Unit_Name_Type;
109 Mapping_File : Natural := No_Mapping_File;
110 Project : Project_Id := No_Project;
111 Syntax_Only : Boolean := False;
112 Output_Is_Object : Boolean := True;
113 end record;
114 -- Data recorded for each compilation process spawned
116 type Comp_Data_Arr is array (Positive range <>) of Compilation_Data;
117 type Comp_Data_Ptr is access Comp_Data_Arr;
118 Running_Compile : Comp_Data_Ptr;
119 -- Used to save information about outstanding compilations
121 Outstanding_Compiles : Natural := 0;
122 -- Current number of outstanding compiles
124 -------------------------
125 -- Note on terminology --
126 -------------------------
128 -- In this program, we use the phrase "termination" of a file name to refer
129 -- to the suffix that appears after the unit name portion. Very often this
130 -- is simply the extension, but in some cases, the sequence may be more
131 -- complex, for example in main.1.ada, the termination in this name is
132 -- ".1.ada" and in main_.ada the termination is "_.ada".
134 -------------------------------------
135 -- Queue (Q) Manipulation Routines --
136 -------------------------------------
138 -- The Q is used in Compile_Sources below. Its implementation uses the GNAT
139 -- generic package Table (basically an extensible array). Q_Front points to
140 -- the first valid element in the Q, whereas Q.First is the first element
141 -- ever enqueued, while Q.Last - 1 is the last element in the Q.
143 -- +---+--------------+---+---+---+-----------+---+--------
144 -- Q | | ........ | | | | ....... | |
145 -- +---+--------------+---+---+---+-----------+---+--------
146 -- ^ ^ ^
147 -- Q.First Q_Front Q.Last-1
149 -- The elements comprised between Q.First and Q_Front-1 are the elements
150 -- that have been enqueued and then dequeued, while the elements between
151 -- Q_Front and Q.Last-1 are the elements currently in the Q. When the Q
152 -- is initialized Q_Front = Q.First = Q.Last. After Compile_Sources has
153 -- terminated its execution, Q_Front = Q.Last and the elements contained
154 -- between Q.First and Q.Last-1 are those that were explored and thus
155 -- marked by Compile_Sources. Whenever the Q is reinitialized, the elements
156 -- between Q.First and Q.Last-1 are unmarked.
158 procedure Init_Q;
159 -- Must be called to (re)initialize the Q
161 procedure Insert_Q
162 (Source_File : File_Name_Type;
163 Source_Unit : Unit_Name_Type := No_Unit_Name;
164 Index : Int := 0);
165 -- Inserts Source_File at the end of Q. Provide Source_Unit when possible
166 -- for external use (gnatdist). Provide index for multi-unit sources.
168 function Empty_Q return Boolean;
169 -- Returns True if Q is empty
171 procedure Extract_From_Q
172 (Source_File : out File_Name_Type;
173 Source_Unit : out Unit_Name_Type;
174 Source_Index : out Int);
175 -- Extracts the first element from the Q
177 procedure Insert_Project_Sources
178 (The_Project : Project_Id;
179 All_Projects : Boolean;
180 Into_Q : Boolean);
181 -- If Into_Q is True, insert all sources of the project file(s) that are
182 -- not already marked into the Q. If Into_Q is False, call Osint.Add_File
183 -- for the first source, then insert all other sources that are not already
184 -- marked into the Q. If All_Projects is True, all sources of all projects
185 -- are concerned; otherwise, only sources of The_Project are concerned,
186 -- including, if The_Project is an extending project, sources inherited
187 -- from projects being extended.
189 First_Q_Initialization : Boolean := True;
190 -- Will be set to false after Init_Q has been called once
192 Q_Front : Natural;
193 -- Points to the first valid element in the Q
195 Unique_Compile : Boolean := False;
196 -- Set to True if -u or -U or a project file with no main is used
198 Unique_Compile_All_Projects : Boolean := False;
199 -- Set to True if -U is used
201 RTS_Specified : String_Access := null;
202 -- Used to detect multiple --RTS= switches
204 N_M_Switch : Natural := 0;
205 -- Used to count -mxxx switches that can affect multilib
207 type Q_Record is record
208 File : File_Name_Type;
209 Unit : Unit_Name_Type;
210 Index : Int;
211 end record;
212 -- File is the name of the file to compile. Unit is for gnatdist
213 -- use in order to easily get the unit name of a file to compile
214 -- when its name is krunched or declared in gnat.adc. Index, when not 0,
215 -- is the index of the unit in a multi-unit source.
217 package Q is new Table.Table (
218 Table_Component_Type => Q_Record,
219 Table_Index_Type => Natural,
220 Table_Low_Bound => 0,
221 Table_Initial => 4000,
222 Table_Increment => 100,
223 Table_Name => "Make.Q");
224 -- This is the actual Q
226 -- The 3 following packages are used to store gcc, gnatbind and gnatlink
227 -- switches found in the project files.
229 package Gcc_Switches is new Table.Table (
230 Table_Component_Type => String_Access,
231 Table_Index_Type => Integer,
232 Table_Low_Bound => 1,
233 Table_Initial => 20,
234 Table_Increment => 100,
235 Table_Name => "Make.Gcc_Switches");
237 package Binder_Switches is new Table.Table (
238 Table_Component_Type => String_Access,
239 Table_Index_Type => Integer,
240 Table_Low_Bound => 1,
241 Table_Initial => 20,
242 Table_Increment => 100,
243 Table_Name => "Make.Binder_Switches");
245 package Linker_Switches is new Table.Table (
246 Table_Component_Type => String_Access,
247 Table_Index_Type => Integer,
248 Table_Low_Bound => 1,
249 Table_Initial => 20,
250 Table_Increment => 100,
251 Table_Name => "Make.Linker_Switches");
253 -- The following instantiations and variables are necessary to save what
254 -- is found on the command line, in case there is a project file specified.
256 package Saved_Gcc_Switches is new Table.Table (
257 Table_Component_Type => String_Access,
258 Table_Index_Type => Integer,
259 Table_Low_Bound => 1,
260 Table_Initial => 20,
261 Table_Increment => 100,
262 Table_Name => "Make.Saved_Gcc_Switches");
264 package Saved_Binder_Switches is new Table.Table (
265 Table_Component_Type => String_Access,
266 Table_Index_Type => Integer,
267 Table_Low_Bound => 1,
268 Table_Initial => 20,
269 Table_Increment => 100,
270 Table_Name => "Make.Saved_Binder_Switches");
272 package Saved_Linker_Switches is new Table.Table
273 (Table_Component_Type => String_Access,
274 Table_Index_Type => Integer,
275 Table_Low_Bound => 1,
276 Table_Initial => 20,
277 Table_Increment => 100,
278 Table_Name => "Make.Saved_Linker_Switches");
280 package Switches_To_Check is new Table.Table (
281 Table_Component_Type => String_Access,
282 Table_Index_Type => Integer,
283 Table_Low_Bound => 1,
284 Table_Initial => 20,
285 Table_Increment => 100,
286 Table_Name => "Make.Switches_To_Check");
288 package Library_Paths is new Table.Table (
289 Table_Component_Type => String_Access,
290 Table_Index_Type => Integer,
291 Table_Low_Bound => 1,
292 Table_Initial => 20,
293 Table_Increment => 100,
294 Table_Name => "Make.Library_Paths");
296 package Failed_Links is new Table.Table (
297 Table_Component_Type => File_Name_Type,
298 Table_Index_Type => Integer,
299 Table_Low_Bound => 1,
300 Table_Initial => 10,
301 Table_Increment => 100,
302 Table_Name => "Make.Failed_Links");
304 package Successful_Links is new Table.Table (
305 Table_Component_Type => File_Name_Type,
306 Table_Index_Type => Integer,
307 Table_Low_Bound => 1,
308 Table_Initial => 10,
309 Table_Increment => 100,
310 Table_Name => "Make.Successful_Links");
312 package Library_Projs is new Table.Table (
313 Table_Component_Type => Project_Id,
314 Table_Index_Type => Integer,
315 Table_Low_Bound => 1,
316 Table_Initial => 10,
317 Table_Increment => 100,
318 Table_Name => "Make.Library_Projs");
320 -- Two variables to keep the last binder and linker switch index in tables
321 -- Binder_Switches and Linker_Switches, before adding switches from the
322 -- project file (if any) and switches from the command line (if any).
324 Last_Binder_Switch : Integer := 0;
325 Last_Linker_Switch : Integer := 0;
327 Normalized_Switches : Argument_List_Access := new Argument_List (1 .. 10);
328 Last_Norm_Switch : Natural := 0;
330 Saved_Maximum_Processes : Natural := 0;
332 Gnatmake_Switch_Found : Boolean;
333 -- Set by Scan_Make_Arg. True when the switch is a gnatmake switch.
334 -- Tested by Add_Switches when switches in package Builder must all be
335 -- gnatmake switches.
337 Switch_May_Be_Passed_To_The_Compiler : Boolean;
338 -- Set by Add_Switches and Switches_Of. True when unrecognized switches
339 -- are passed to the Ada compiler.
341 type Arg_List_Ref is access Argument_List;
342 The_Saved_Gcc_Switches : Arg_List_Ref;
344 Project_File_Name : String_Access := null;
345 -- The path name of the main project file, if any
347 Project_File_Name_Present : Boolean := False;
348 -- True when -P is used with a space between -P and the project file name
350 Current_Verbosity : Prj.Verbosity := Prj.Default;
351 -- Verbosity to parse the project files
353 Main_Project : Prj.Project_Id := No_Project;
354 -- The project id of the main project file, if any
356 Project_Of_Current_Object_Directory : Project_Id := No_Project;
357 -- The object directory of the project for the last compilation. Avoid
358 -- calling Change_Dir if the current working directory is already this
359 -- directory
361 -- Packages of project files where unknown attributes are errors
363 Naming_String : aliased String := "naming";
364 Builder_String : aliased String := "builder";
365 Compiler_String : aliased String := "compiler";
366 Binder_String : aliased String := "binder";
367 Linker_String : aliased String := "linker";
369 Gnatmake_Packages : aliased String_List :=
370 (Naming_String 'Access,
371 Builder_String 'Access,
372 Compiler_String 'Access,
373 Binder_String 'Access,
374 Linker_String 'Access);
376 Packages_To_Check_By_Gnatmake : constant String_List_Access :=
377 Gnatmake_Packages'Access;
379 procedure Add_Library_Search_Dir
380 (Path : String;
381 On_Command_Line : Boolean);
382 -- Call Add_Lib_Search_Dir with an absolute directory path. If Path is
383 -- relative path, when On_Command_Line is True, it is relative to the
384 -- current working directory. When On_Command_Line is False, it is relative
385 -- to the project directory of the main project.
387 procedure Add_Source_Search_Dir
388 (Path : String;
389 On_Command_Line : Boolean);
390 -- Call Add_Src_Search_Dir with an absolute directory path. If Path is a
391 -- relative path, when On_Command_Line is True, it is relative to the
392 -- current working directory. When On_Command_Line is False, it is relative
393 -- to the project directory of the main project.
395 procedure Add_Source_Dir (N : String);
396 -- Call Add_Src_Search_Dir (output one line when in verbose mode)
398 procedure Add_Source_Directories is
399 new Prj.Env.For_All_Source_Dirs (Action => Add_Source_Dir);
401 procedure Add_Object_Dir (N : String);
402 -- Call Add_Lib_Search_Dir (output one line when in verbose mode)
404 procedure Add_Object_Directories is
405 new Prj.Env.For_All_Object_Dirs (Action => Add_Object_Dir);
407 procedure Change_To_Object_Directory (Project : Project_Id);
408 -- Change to the object directory of project Project, if this is not
409 -- already the current working directory.
411 type Bad_Compilation_Info is record
412 File : File_Name_Type;
413 Unit : Unit_Name_Type;
414 Found : Boolean;
415 end record;
416 -- File is the name of the file for which a compilation failed. Unit is for
417 -- gnatdist use in order to easily get the unit name of a file when its
418 -- name is krunched or declared in gnat.adc. Found is False if the
419 -- compilation failed because the file could not be found.
421 package Bad_Compilation is new Table.Table (
422 Table_Component_Type => Bad_Compilation_Info,
423 Table_Index_Type => Natural,
424 Table_Low_Bound => 1,
425 Table_Initial => 20,
426 Table_Increment => 100,
427 Table_Name => "Make.Bad_Compilation");
428 -- Full name of all the source files for which compilation fails
430 Do_Compile_Step : Boolean := True;
431 Do_Bind_Step : Boolean := True;
432 Do_Link_Step : Boolean := True;
433 -- Flags to indicate what step should be executed. Can be set to False
434 -- with the switches -c, -b and -l. These flags are reset to True for
435 -- each invocation of procedure Gnatmake.
437 Shared_String : aliased String := "-shared";
438 Force_Elab_Flags_String : aliased String := "-F";
440 No_Shared_Switch : aliased Argument_List := (1 .. 0 => null);
441 Shared_Switch : aliased Argument_List := (1 => Shared_String'Access);
442 Bind_Shared : Argument_List_Access := No_Shared_Switch'Access;
443 -- Switch to added in front of gnatbind switches. By default no switch is
444 -- added. Switch "-shared" is added if there is a non-static Library
445 -- Project File.
447 Shared_Libgcc : aliased String := "-shared-libgcc";
449 No_Shared_Libgcc_Switch : aliased Argument_List := (1 .. 0 => null);
450 Shared_Libgcc_Switch : aliased Argument_List :=
451 (1 => Shared_Libgcc'Access);
452 Link_With_Shared_Libgcc : Argument_List_Access :=
453 No_Shared_Libgcc_Switch'Access;
455 procedure Make_Failed (S : String);
456 -- Delete all temp files created by Gnatmake and call Osint.Fail, with the
457 -- parameter S (see osint.ads). This is called from the Prj hierarchy and
458 -- the MLib hierarchy.
460 --------------------------
461 -- Obsolete Executables --
462 --------------------------
464 Executable_Obsolete : Boolean := False;
465 -- Executable_Obsolete is initially set to False for each executable,
466 -- and is set to True whenever one of the source of the executable is
467 -- compiled, or has already been compiled for another executable.
469 Max_Header : constant := 200;
470 -- This needs a proper comment, it used to say "arbitrary"
471 -- that's not an adequate comment ???
473 type Header_Num is range 1 .. Max_Header;
474 -- Header_Num for the hash table Obsoleted below
476 function Hash (F : File_Name_Type) return Header_Num;
477 -- Hash function for the hash table Obsoleted below
479 package Obsoleted is new System.HTable.Simple_HTable
480 (Header_Num => Header_Num,
481 Element => Boolean,
482 No_Element => False,
483 Key => File_Name_Type,
484 Hash => Hash,
485 Equal => "=");
486 -- A hash table to keep all files that have been compiled, to detect
487 -- if an executable is up to date or not.
489 procedure Enter_Into_Obsoleted (F : File_Name_Type);
490 -- Enter a file name, without directory information, into the hash table
491 -- Obsoleted.
493 function Is_In_Obsoleted (F : File_Name_Type) return Boolean;
494 -- Check if a file name, without directory information, has already been
495 -- entered into the hash table Obsoleted.
497 type Dependency is record
498 This : File_Name_Type;
499 Depends_On : File_Name_Type;
500 end record;
501 -- Components of table Dependencies below
503 package Dependencies is new Table.Table (
504 Table_Component_Type => Dependency,
505 Table_Index_Type => Integer,
506 Table_Low_Bound => 1,
507 Table_Initial => 20,
508 Table_Increment => 100,
509 Table_Name => "Make.Dependencies");
510 -- A table to keep dependencies, to be able to decide if an executable
511 -- is obsolete. More explanation needed ???
513 -- procedure Add_Dependency (S : File_Name_Type; On : File_Name_Type);
514 -- -- Add one entry in table Dependencies
516 ----------------------------
517 -- Arguments and Switches --
518 ----------------------------
520 Arguments : Argument_List_Access;
521 -- Used to gather the arguments for invocation of the compiler
523 Last_Argument : Natural := 0;
524 -- Last index of arguments in Arguments above
526 Arguments_Project : Project_Id;
527 -- Project id, if any, of the source to be compiled
529 Arguments_Path_Name : Path_Name_Type;
530 -- Full path of the source to be compiled, when Arguments_Project is not
531 -- No_Project.
533 Dummy_Switch : constant String_Access := new String'("- ");
534 -- Used to initialized Prev_Switch in procedure Check
536 procedure Add_Arguments (Args : Argument_List);
537 -- Add arguments to global variable Arguments, increasing its size
538 -- if necessary and adjusting Last_Argument.
540 function Configuration_Pragmas_Switch
541 (For_Project : Project_Id) return Argument_List;
542 -- Return an argument list of one element, if there is a configuration
543 -- pragmas file to be specified for For_Project,
544 -- otherwise return an empty argument list.
546 -------------------
547 -- Misc Routines --
548 -------------------
550 procedure List_Depend;
551 -- Prints to standard output the list of object dependencies. This list
552 -- can be used directly in a Makefile. A call to Compile_Sources must
553 -- precede the call to List_Depend. Also because this routine uses the
554 -- ALI files that were originally loaded and scanned by Compile_Sources,
555 -- no additional ALI files should be scanned between the two calls (i.e.
556 -- between the call to Compile_Sources and List_Depend.)
558 procedure List_Bad_Compilations;
559 -- Prints out the list of all files for which the compilation failed
561 Usage_Needed : Boolean := True;
562 -- Flag used to make sure Makeusg is call at most once
564 procedure Usage;
565 -- Call Makeusg, if Usage_Needed is True.
566 -- Set Usage_Needed to False.
568 procedure Debug_Msg (S : String; N : Name_Id);
569 procedure Debug_Msg (S : String; N : File_Name_Type);
570 procedure Debug_Msg (S : String; N : Unit_Name_Type);
571 -- If Debug.Debug_Flag_W is set outputs string S followed by name N
573 procedure Recursive_Compute_Depth (Project : Project_Id);
574 -- Compute depth of Project and of the projects it depends on
576 -----------------------
577 -- Gnatmake Routines --
578 -----------------------
580 subtype Lib_Mark_Type is Byte;
581 -- Used in Mark_Directory
583 Ada_Lib_Dir : constant Lib_Mark_Type := 1;
584 -- Used to mark a directory as a GNAT lib dir
586 -- Note that the notion of GNAT lib dir is no longer used. The code related
587 -- to it has not been removed to give an idea on how to use the directory
588 -- prefix marking mechanism.
590 -- An Ada library directory is a directory containing ali and object files
591 -- but no source files for the bodies (the specs can be in the same or some
592 -- other directory). These directories are specified in the Gnatmake
593 -- command line with the switch "-Adir" (to specify the spec location -Idir
594 -- cab be used). Gnatmake skips the missing sources whose ali are in Ada
595 -- library directories. For an explanation of why Gnatmake behaves that
596 -- way, see the spec of Make.Compile_Sources. The directory lookup penalty
597 -- is incurred every single time this routine is called.
599 procedure Check_Steps;
600 -- Check what steps (Compile, Bind, Link) must be executed.
601 -- Set the step flags accordingly.
603 function In_Ada_Lib_Dir (File : File_Name_Type) return Boolean;
604 -- Get directory prefix of this file and get lib mark stored in name
605 -- table for this directory. Then check if an Ada lib mark has been set.
607 procedure Mark_Directory
608 (Dir : String;
609 Mark : Lib_Mark_Type;
610 On_Command_Line : Boolean);
611 -- Store the absolute path from Dir in name table and set lib mark as name
612 -- info to identify Ada libraries.
614 -- If Dir is a relative path, when On_Command_Line is True, it is relative
615 -- to the current working directory; when On_Command_Line is False, it is
616 -- relative to the project directory of the main project.
618 Output_Is_Object : Boolean := True;
619 -- Set to False when using a switch -S for the compiler
621 procedure Check_For_S_Switch;
622 -- Set Output_Is_Object to False when the -S switch is used for the
623 -- compiler.
625 function Switches_Of
626 (Source_File : File_Name_Type;
627 Source_File_Name : String;
628 Source_Index : Int;
629 Project : Project_Id;
630 In_Package : Package_Id;
631 Allow_ALI : Boolean) return Variable_Value;
632 -- Return the switches for the source file in the specified package of a
633 -- project file. If the Source_File ends with a standard GNAT extension
634 -- (".ads" or ".adb"), try first the full name, then the name without the
635 -- extension, then, if Allow_ALI is True, the name with the extension
636 -- ".ali". If there is no switches for either names, try first Switches
637 -- (others) then the default switches for Ada. If all failed, return
638 -- No_Variable_Value.
640 function Is_In_Object_Directory
641 (Source_File : File_Name_Type;
642 Full_Lib_File : File_Name_Type) return Boolean;
643 -- Check if, when using a project file, the ALI file is in the project
644 -- directory of the ultimate extending project. If it is not, we ignore
645 -- the fact that this ALI file is read-only.
647 procedure Process_Multilib (Project_Node_Tree : Project_Node_Tree_Ref);
648 -- Add appropriate --RTS argument to handle multilib
650 ----------------------------------------------------
651 -- Compiler, Binder & Linker Data and Subprograms --
652 ----------------------------------------------------
654 Gcc : String_Access := Program_Name ("gcc", "gnatmake");
655 Gnatbind : String_Access := Program_Name ("gnatbind", "gnatmake");
656 Gnatlink : String_Access := Program_Name ("gnatlink", "gnatmake");
657 -- Default compiler, binder, linker programs
659 Saved_Gcc : String_Access := null;
660 Saved_Gnatbind : String_Access := null;
661 Saved_Gnatlink : String_Access := null;
662 -- Given by the command line. Will be used, if non null
664 Gcc_Path : String_Access :=
665 GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
666 Gnatbind_Path : String_Access :=
667 GNAT.OS_Lib.Locate_Exec_On_Path (Gnatbind.all);
668 Gnatlink_Path : String_Access :=
669 GNAT.OS_Lib.Locate_Exec_On_Path (Gnatlink.all);
670 -- Path for compiler, binder, linker programs, defaulted now for gnatdist.
671 -- Changed later if overridden on command line.
673 Comp_Flag : constant String_Access := new String'("-c");
674 Output_Flag : constant String_Access := new String'("-o");
675 Ada_Flag_1 : constant String_Access := new String'("-x");
676 Ada_Flag_2 : constant String_Access := new String'("ada");
677 No_gnat_adc : constant String_Access := new String'("-gnatA");
678 GNAT_Flag : constant String_Access := new String'("-gnatpg");
679 Do_Not_Check_Flag : constant String_Access := new String'("-x");
681 Object_Suffix : constant String := Get_Target_Object_Suffix.all;
683 Syntax_Only : Boolean := False;
684 -- Set to True when compiling with -gnats
686 Display_Executed_Programs : Boolean := True;
687 -- Set to True if name of commands should be output on stderr (or on stdout
688 -- if the Commands_To_Stdout flag was set by use of the -eS switch).
690 Output_File_Name_Seen : Boolean := False;
691 -- Set to True after having scanned the file_name for
692 -- switch "-o file_name"
694 Object_Directory_Seen : Boolean := False;
695 -- Set to True after having scanned the object directory for
696 -- switch "-D obj_dir".
698 Object_Directory_Path : String_Access := null;
699 -- The path name of the object directory, set with switch -D
701 type Make_Program_Type is (None, Compiler, Binder, Linker);
703 Program_Args : Make_Program_Type := None;
704 -- Used to indicate if we are scanning gnatmake, gcc, gnatbind, or gnatbind
705 -- options within the gnatmake command line. Used in Scan_Make_Arg only,
706 -- but must be global since value preserved from one call to another.
708 Temporary_Config_File : Boolean := False;
709 -- Set to True when there is a temporary config file used for a project
710 -- file, to avoid displaying the -gnatec switch for a temporary file.
712 procedure Add_Switches
713 (The_Package : Package_Id;
714 File_Name : String;
715 Index : Int;
716 Program : Make_Program_Type;
717 Unknown_Switches_To_The_Compiler : Boolean := True;
718 Project_Node_Tree : Project_Node_Tree_Ref);
719 procedure Add_Switch
720 (S : String_Access;
721 Program : Make_Program_Type;
722 Append_Switch : Boolean := True;
723 And_Save : Boolean := True);
724 procedure Add_Switch
725 (S : String;
726 Program : Make_Program_Type;
727 Append_Switch : Boolean := True;
728 And_Save : Boolean := True);
729 -- Make invokes one of three programs (the compiler, the binder or the
730 -- linker). For the sake of convenience, some program specific switches
731 -- can be passed directly on the gnatmake command line. This procedure
732 -- records these switches so that gnatmake can pass them to the right
733 -- program. S is the switch to be added at the end of the command line
734 -- for Program if Append_Switch is True. If Append_Switch is False S is
735 -- added at the beginning of the command line.
737 procedure Check
738 (Source_File : File_Name_Type;
739 Source_Index : Int;
740 Is_Main_Source : Boolean;
741 The_Args : Argument_List;
742 Lib_File : File_Name_Type;
743 Read_Only : Boolean;
744 ALI : out ALI_Id;
745 O_File : out File_Name_Type;
746 O_Stamp : out Time_Stamp_Type);
747 -- Determines whether the library file Lib_File is up-to-date or not. The
748 -- full name (with path information) of the object file corresponding to
749 -- Lib_File is returned in O_File. Its time stamp is saved in O_Stamp.
750 -- ALI is the ALI_Id corresponding to Lib_File. If Lib_File in not
751 -- up-to-date, then the corresponding source file needs to be recompiled.
752 -- In this case ALI = No_ALI_Id.
754 procedure Check_Linker_Options
755 (E_Stamp : Time_Stamp_Type;
756 O_File : out File_Name_Type;
757 O_Stamp : out Time_Stamp_Type);
758 -- Checks all linker options for linker files that are newer
759 -- than E_Stamp. If such objects are found, the youngest object
760 -- is returned in O_File and its stamp in O_Stamp.
762 -- If no obsolete linker files were found, the first missing
763 -- linker file is returned in O_File and O_Stamp is empty.
764 -- Otherwise O_File is No_File.
766 procedure Collect_Arguments
767 (Source_File : File_Name_Type;
768 Source_Index : Int;
769 Is_Main_Source : Boolean;
770 Args : Argument_List);
771 -- Collect all arguments for a source to be compiled, including those
772 -- that come from a project file.
774 procedure Display (Program : String; Args : Argument_List);
775 -- Displays Program followed by the arguments in Args if variable
776 -- Display_Executed_Programs is set. The lower bound of Args must be 1.
778 procedure Report_Compilation_Failed;
779 -- Delete all temporary files and fail graciously
781 -----------------
782 -- Mapping files
783 -----------------
785 type Temp_Path_Names is array (Positive range <>) of Path_Name_Type;
786 type Temp_Path_Ptr is access Temp_Path_Names;
788 type Free_File_Indices is array (Positive range <>) of Positive;
789 type Free_Indices_Ptr is access Free_File_Indices;
791 type Project_Compilation_Data is record
792 Mapping_File_Names : Temp_Path_Ptr;
793 -- The name ids of the temporary mapping files used. This is indexed
794 -- on the maximum number of compilation processes we will be spawning
795 -- (-j parameter)
797 Last_Mapping_File_Names : Natural;
798 -- Index of the last mapping file created for this project
800 Free_Mapping_File_Indices : Free_Indices_Ptr;
801 -- Indices in Mapping_File_Names of the mapping file names that can be
802 -- reused for subsequent compilations.
804 Last_Free_Indices : Natural;
805 -- Number of mapping files that can be reused
806 end record;
807 -- Information necessary when compiling a project
809 type Project_Compilation_Access is access Project_Compilation_Data;
811 package Project_Compilation_Htable is new Simple_HTable
812 (Header_Num => Prj.Header_Num,
813 Element => Project_Compilation_Access,
814 No_Element => null,
815 Key => Project_Id,
816 Hash => Prj.Hash,
817 Equal => "=");
819 Project_Compilation : Project_Compilation_Htable.Instance;
821 Gnatmake_Mapping_File : String_Access := null;
822 -- The path name of a mapping file specified by switch -C=
824 procedure Init_Mapping_File
825 (Project : Project_Id;
826 Data : in out Project_Compilation_Data;
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 (Project_Node_Tree : out Project_Node_Tree_Ref);
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
1019 (Project_Node_Tree : Project_Node_Tree_Ref;
1020 Argv : String;
1021 And_Save : Boolean);
1022 -- Scan make arguments. Argv is a single argument to be processed.
1023 -- Project_Node_Tree will be used to initialize external references. It
1024 -- must have been initialized.
1026 -------------------
1027 -- Add_Arguments --
1028 -------------------
1030 procedure Add_Arguments (Args : Argument_List) is
1031 begin
1032 if Arguments = null then
1033 Arguments := new Argument_List (1 .. Args'Length + 10);
1035 else
1036 while Last_Argument + Args'Length > Arguments'Last loop
1037 declare
1038 New_Arguments : constant Argument_List_Access :=
1039 new Argument_List (1 .. Arguments'Last * 2);
1040 begin
1041 New_Arguments (1 .. Last_Argument) :=
1042 Arguments (1 .. Last_Argument);
1043 Arguments := New_Arguments;
1044 end;
1045 end loop;
1046 end if;
1048 Arguments (Last_Argument + 1 .. Last_Argument + Args'Length) := Args;
1049 Last_Argument := Last_Argument + Args'Length;
1050 end Add_Arguments;
1052 -- --------------------
1053 -- -- Add_Dependency --
1054 -- --------------------
1056 -- procedure Add_Dependency (S : File_Name_Type; On : File_Name_Type) is
1057 -- begin
1058 -- Dependencies.Increment_Last;
1059 -- Dependencies.Table (Dependencies.Last) := (S, On);
1060 -- end Add_Dependency;
1062 ----------------------------
1063 -- Add_Library_Search_Dir --
1064 ----------------------------
1066 procedure Add_Library_Search_Dir
1067 (Path : String;
1068 On_Command_Line : Boolean)
1070 begin
1071 if On_Command_Line then
1072 Add_Lib_Search_Dir (Normalize_Pathname (Path));
1074 else
1075 Get_Name_String (Main_Project.Directory.Display_Name);
1076 Add_Lib_Search_Dir
1077 (Normalize_Pathname (Path, Name_Buffer (1 .. Name_Len)));
1078 end if;
1079 end Add_Library_Search_Dir;
1081 --------------------
1082 -- Add_Object_Dir --
1083 --------------------
1085 procedure Add_Object_Dir (N : String) is
1086 begin
1087 Add_Lib_Search_Dir (N);
1089 if Verbose_Mode then
1090 Write_Str ("Adding object directory """);
1091 Write_Str (N);
1092 Write_Str (""".");
1093 Write_Eol;
1094 end if;
1095 end Add_Object_Dir;
1097 --------------------
1098 -- Add_Source_Dir --
1099 --------------------
1101 procedure Add_Source_Dir (N : String) is
1102 begin
1103 Add_Src_Search_Dir (N);
1105 if Verbose_Mode then
1106 Write_Str ("Adding source directory """);
1107 Write_Str (N);
1108 Write_Str (""".");
1109 Write_Eol;
1110 end if;
1111 end Add_Source_Dir;
1113 ---------------------------
1114 -- Add_Source_Search_Dir --
1115 ---------------------------
1117 procedure Add_Source_Search_Dir
1118 (Path : String;
1119 On_Command_Line : Boolean)
1121 begin
1122 if On_Command_Line then
1123 Add_Src_Search_Dir (Normalize_Pathname (Path));
1125 else
1126 Get_Name_String (Main_Project.Directory.Display_Name);
1127 Add_Src_Search_Dir
1128 (Normalize_Pathname (Path, Name_Buffer (1 .. Name_Len)));
1129 end if;
1130 end Add_Source_Search_Dir;
1132 ----------------
1133 -- Add_Switch --
1134 ----------------
1136 procedure Add_Switch
1137 (S : String_Access;
1138 Program : Make_Program_Type;
1139 Append_Switch : Boolean := True;
1140 And_Save : Boolean := True)
1142 generic
1143 with package T is new Table.Table (<>);
1144 procedure Generic_Position (New_Position : out Integer);
1145 -- Generic procedure that chooses a position for S in T at the
1146 -- beginning or the end, depending on the boolean Append_Switch.
1147 -- Calling this procedure may expand the table.
1149 ----------------------
1150 -- Generic_Position --
1151 ----------------------
1153 procedure Generic_Position (New_Position : out Integer) is
1154 begin
1155 T.Increment_Last;
1157 if Append_Switch then
1158 New_Position := Integer (T.Last);
1159 else
1160 for J in reverse T.Table_Index_Type'Succ (T.First) .. T.Last loop
1161 T.Table (J) := T.Table (T.Table_Index_Type'Pred (J));
1162 end loop;
1164 New_Position := Integer (T.First);
1165 end if;
1166 end Generic_Position;
1168 procedure Gcc_Switches_Pos is new Generic_Position (Gcc_Switches);
1169 procedure Binder_Switches_Pos is new Generic_Position (Binder_Switches);
1170 procedure Linker_Switches_Pos is new Generic_Position (Linker_Switches);
1172 procedure Saved_Gcc_Switches_Pos is new
1173 Generic_Position (Saved_Gcc_Switches);
1175 procedure Saved_Binder_Switches_Pos is new
1176 Generic_Position (Saved_Binder_Switches);
1178 procedure Saved_Linker_Switches_Pos is new
1179 Generic_Position (Saved_Linker_Switches);
1181 New_Position : Integer;
1183 -- Start of processing for Add_Switch
1185 begin
1186 if And_Save then
1187 case Program is
1188 when Compiler =>
1189 Saved_Gcc_Switches_Pos (New_Position);
1190 Saved_Gcc_Switches.Table (New_Position) := S;
1192 when Binder =>
1193 Saved_Binder_Switches_Pos (New_Position);
1194 Saved_Binder_Switches.Table (New_Position) := S;
1196 when Linker =>
1197 Saved_Linker_Switches_Pos (New_Position);
1198 Saved_Linker_Switches.Table (New_Position) := S;
1200 when None =>
1201 raise Program_Error;
1202 end case;
1204 else
1205 case Program is
1206 when Compiler =>
1207 Gcc_Switches_Pos (New_Position);
1208 Gcc_Switches.Table (New_Position) := S;
1210 when Binder =>
1211 Binder_Switches_Pos (New_Position);
1212 Binder_Switches.Table (New_Position) := S;
1214 when Linker =>
1215 Linker_Switches_Pos (New_Position);
1216 Linker_Switches.Table (New_Position) := S;
1218 when None =>
1219 raise Program_Error;
1220 end case;
1221 end if;
1222 end Add_Switch;
1224 procedure Add_Switch
1225 (S : String;
1226 Program : Make_Program_Type;
1227 Append_Switch : Boolean := True;
1228 And_Save : Boolean := True)
1230 begin
1231 Add_Switch (S => new String'(S),
1232 Program => Program,
1233 Append_Switch => Append_Switch,
1234 And_Save => And_Save);
1235 end Add_Switch;
1237 ------------------
1238 -- Add_Switches --
1239 ------------------
1241 procedure Add_Switches
1242 (The_Package : Package_Id;
1243 File_Name : String;
1244 Index : Int;
1245 Program : Make_Program_Type;
1246 Unknown_Switches_To_The_Compiler : Boolean := True;
1247 Project_Node_Tree : Project_Node_Tree_Ref)
1249 Switches : Variable_Value;
1250 Switch_List : String_List_Id;
1251 Element : String_Element;
1253 begin
1254 Switch_May_Be_Passed_To_The_Compiler :=
1255 Unknown_Switches_To_The_Compiler;
1257 if File_Name'Length > 0 then
1258 Name_Len := 0;
1259 Add_Str_To_Name_Buffer (File_Name);
1260 Switches :=
1261 Switches_Of
1262 (Source_File => Name_Find,
1263 Source_File_Name => File_Name,
1264 Source_Index => Index,
1265 Project => Main_Project,
1266 In_Package => The_Package,
1267 Allow_ALI => Program = Binder or else Program = Linker);
1269 if Switches.Kind = List then
1270 Program_Args := Program;
1272 Switch_List := Switches.Values;
1273 while Switch_List /= Nil_String loop
1274 Element := Project_Tree.String_Elements.Table (Switch_List);
1275 Get_Name_String (Element.Value);
1277 if Name_Len > 0 then
1278 declare
1279 Argv : constant String := Name_Buffer (1 .. Name_Len);
1280 -- We need a copy, because Name_Buffer may be modified
1282 begin
1283 if Verbose_Mode then
1284 Write_Str (" Adding ");
1285 Write_Line (Argv);
1286 end if;
1288 Scan_Make_Arg
1289 (Project_Node_Tree, Argv, And_Save => False);
1291 if not Gnatmake_Switch_Found
1292 and then not Switch_May_Be_Passed_To_The_Compiler
1293 then
1294 Errutil.Error_Msg
1295 ('"' & Argv &
1296 """ is not a gnatmake switch. Consider moving " &
1297 "it to Global_Compilation_Switches.",
1298 Element.Location);
1299 Errutil.Finalize;
1300 Make_Failed ("*** illegal switch """ & Argv & """");
1301 end if;
1302 end;
1303 end if;
1305 Switch_List := Element.Next;
1306 end loop;
1307 end if;
1308 end if;
1309 end Add_Switches;
1311 ----------
1312 -- Bind --
1313 ----------
1315 procedure Bind (ALI_File : File_Name_Type; Args : Argument_List) is
1316 Bind_Args : Argument_List (1 .. Args'Last + 2);
1317 Bind_Last : Integer;
1318 Success : Boolean;
1320 begin
1321 pragma Assert (Args'First = 1);
1323 -- Optimize the simple case where the gnatbind command line looks like
1324 -- gnatbind -aO. -I- file.ali --into-> gnatbind file.adb
1326 if Args'Length = 2
1327 and then Args (Args'First).all = "-aO" & Normalized_CWD
1328 and then Args (Args'Last).all = "-I-"
1329 and then ALI_File = Strip_Directory (ALI_File)
1330 then
1331 Bind_Last := Args'First - 1;
1333 else
1334 Bind_Last := Args'Last;
1335 Bind_Args (Args'Range) := Args;
1336 end if;
1338 -- It is completely pointless to re-check source file time stamps. This
1339 -- has been done already by gnatmake
1341 Bind_Last := Bind_Last + 1;
1342 Bind_Args (Bind_Last) := Do_Not_Check_Flag;
1344 Get_Name_String (ALI_File);
1346 Bind_Last := Bind_Last + 1;
1347 Bind_Args (Bind_Last) := new String'(Name_Buffer (1 .. Name_Len));
1349 GNAT.OS_Lib.Normalize_Arguments (Bind_Args (Args'First .. Bind_Last));
1351 Display (Gnatbind.all, Bind_Args (Args'First .. Bind_Last));
1353 if Gnatbind_Path = null then
1354 Make_Failed ("error, unable to locate " & Gnatbind.all);
1355 end if;
1357 GNAT.OS_Lib.Spawn
1358 (Gnatbind_Path.all, Bind_Args (Args'First .. Bind_Last), Success);
1360 if not Success then
1361 Make_Failed ("*** bind failed.");
1362 end if;
1363 end Bind;
1365 --------------------------------
1366 -- Change_To_Object_Directory --
1367 --------------------------------
1369 procedure Change_To_Object_Directory (Project : Project_Id) is
1370 Object_Directory : Path_Name_Type;
1372 begin
1373 pragma Assert (Project /= No_Project);
1375 -- Nothing to do if the current working directory is already the correct
1376 -- object directory.
1378 if Project_Of_Current_Object_Directory /= Project then
1379 Project_Of_Current_Object_Directory := Project;
1380 Object_Directory := Project.Object_Directory.Name;
1382 -- Set the working directory to the object directory of the actual
1383 -- project.
1385 if Verbose_Mode then
1386 Write_Str ("Changing to object directory of """);
1387 Write_Name (Project.Display_Name);
1388 Write_Str (""": """);
1389 Write_Name (Object_Directory);
1390 Write_Line ("""");
1391 end if;
1393 Change_Dir (Get_Name_String (Object_Directory));
1394 end if;
1396 exception
1397 -- Fail if unable to change to the object directory
1399 when Directory_Error =>
1400 Make_Failed ("unable to change to object directory """ &
1401 Path_Or_File_Name
1402 (Project.Object_Directory.Name) &
1403 """ of project " &
1404 Get_Name_String (Project.Display_Name));
1405 end Change_To_Object_Directory;
1407 -----------
1408 -- Check --
1409 -----------
1411 procedure Check
1412 (Source_File : File_Name_Type;
1413 Source_Index : Int;
1414 Is_Main_Source : Boolean;
1415 The_Args : Argument_List;
1416 Lib_File : File_Name_Type;
1417 Read_Only : Boolean;
1418 ALI : out ALI_Id;
1419 O_File : out File_Name_Type;
1420 O_Stamp : out Time_Stamp_Type)
1422 function First_New_Spec (A : ALI_Id) return File_Name_Type;
1423 -- Looks in the with table entries of A and returns the spec file name
1424 -- of the first withed unit (subprogram) for which no spec existed when
1425 -- A was generated but for which there exists one now, implying that A
1426 -- is now obsolete. If no such unit is found No_File is returned.
1427 -- Otherwise the spec file name of the unit is returned.
1429 -- **WARNING** in the event of Uname format modifications, one *MUST*
1430 -- make sure this function is also updated.
1432 -- Note: This function should really be in ali.adb and use Uname
1433 -- services, but this causes the whole compiler to be dragged along
1434 -- for gnatbind and gnatmake.
1436 --------------------
1437 -- First_New_Spec --
1438 --------------------
1440 function First_New_Spec (A : ALI_Id) return File_Name_Type is
1441 Spec_File_Name : File_Name_Type := No_File;
1443 function New_Spec (Uname : Unit_Name_Type) return Boolean;
1444 -- Uname is the name of the spec or body of some ada unit. This
1445 -- function returns True if the Uname is the name of a body which has
1446 -- a spec not mentioned in ALI file A. If True is returned
1447 -- Spec_File_Name above is set to the name of this spec file.
1449 --------------
1450 -- New_Spec --
1451 --------------
1453 function New_Spec (Uname : Unit_Name_Type) return Boolean is
1454 Spec_Name : Unit_Name_Type;
1455 File_Name : File_Name_Type;
1457 begin
1458 -- Test whether Uname is the name of a body unit (i.e. ends
1459 -- with %b)
1461 Get_Name_String (Uname);
1462 pragma
1463 Assert (Name_Len > 2 and then Name_Buffer (Name_Len - 1) = '%');
1465 if Name_Buffer (Name_Len) /= 'b' then
1466 return False;
1467 end if;
1469 -- Convert unit name into spec name
1471 -- ??? this code seems dubious in presence of pragma
1472 -- Source_File_Name since there is no more direct relationship
1473 -- between unit name and file name.
1475 -- ??? Further, what about alternative subunit naming
1477 Name_Buffer (Name_Len) := 's';
1478 Spec_Name := Name_Find;
1479 File_Name := Get_File_Name (Spec_Name, Subunit => False);
1481 -- Look if File_Name is mentioned in A's sdep list.
1482 -- If not look if the file exists. If it does return True.
1484 for D in
1485 ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep
1486 loop
1487 if Sdep.Table (D).Sfile = File_Name then
1488 return False;
1489 end if;
1490 end loop;
1492 if Full_Source_Name (File_Name) /= No_File then
1493 Spec_File_Name := File_Name;
1494 return True;
1495 end if;
1497 return False;
1498 end New_Spec;
1500 -- Start of processing for First_New_Spec
1502 begin
1503 U_Chk : for U in
1504 ALIs.Table (A).First_Unit .. ALIs.Table (A).Last_Unit
1505 loop
1506 exit U_Chk when Units.Table (U).Utype = Is_Body_Only
1507 and then New_Spec (Units.Table (U).Uname);
1509 for W in Units.Table (U).First_With
1511 Units.Table (U).Last_With
1512 loop
1513 exit U_Chk when
1514 Withs.Table (W).Afile /= No_File
1515 and then New_Spec (Withs.Table (W).Uname);
1516 end loop;
1517 end loop U_Chk;
1519 return Spec_File_Name;
1520 end First_New_Spec;
1522 ---------------------------------
1523 -- Data declarations for Check --
1524 ---------------------------------
1526 Full_Lib_File : File_Name_Type;
1527 -- Full name of current library file
1529 Full_Obj_File : File_Name_Type;
1530 -- Full name of the object file corresponding to Lib_File
1532 Lib_Stamp : Time_Stamp_Type;
1533 -- Time stamp of the current ada library file
1535 Obj_Stamp : Time_Stamp_Type;
1536 -- Time stamp of the current object file
1538 Modified_Source : File_Name_Type;
1539 -- The first source in Lib_File whose current time stamp differs
1540 -- from that stored in Lib_File.
1542 New_Spec : File_Name_Type;
1543 -- If Lib_File contains in its W (with) section a body (for a
1544 -- subprogram) for which there exists a spec and the spec did not
1545 -- appear in the Sdep section of Lib_File, New_Spec contains the file
1546 -- name of this new spec.
1548 Source_Name : File_Name_Type;
1549 Text : Text_Buffer_Ptr;
1551 Prev_Switch : String_Access;
1552 -- Previous switch processed
1554 Arg : Arg_Id := Arg_Id'First;
1555 -- Current index in Args.Table for a given unit (init to stop warning)
1557 Switch_Found : Boolean;
1558 -- True if a given switch has been found
1560 ALI_Project : Project_Id;
1561 -- If the ALI file is in the object directory of a project, this is
1562 -- the project id.
1564 -- Start of processing for Check
1566 begin
1567 pragma Assert (Lib_File /= No_File);
1569 -- If ALI file is read-only, temporarily set Check_Object_Consistency to
1570 -- False. We don't care if the object file is not there (presumably a
1571 -- library will be used for linking.)
1573 if Read_Only then
1574 declare
1575 Saved_Check_Object_Consistency : constant Boolean :=
1576 Check_Object_Consistency;
1577 begin
1578 Check_Object_Consistency := False;
1579 Text := Read_Library_Info (Lib_File);
1580 Check_Object_Consistency := Saved_Check_Object_Consistency;
1581 end;
1583 else
1584 Text := Read_Library_Info (Lib_File);
1585 end if;
1587 Full_Lib_File := Full_Library_Info_Name;
1588 Full_Obj_File := Full_Object_File_Name;
1589 Lib_Stamp := Current_Library_File_Stamp;
1590 Obj_Stamp := Current_Object_File_Stamp;
1592 if Full_Lib_File = No_File then
1593 Verbose_Msg
1594 (Lib_File,
1595 "being checked ...",
1596 Prefix => " ",
1597 Minimum_Verbosity => Opt.Medium);
1598 else
1599 Verbose_Msg
1600 (Full_Lib_File,
1601 "being checked ...",
1602 Prefix => " ",
1603 Minimum_Verbosity => Opt.Medium);
1604 end if;
1606 ALI := No_ALI_Id;
1607 O_File := Full_Obj_File;
1608 O_Stamp := Obj_Stamp;
1610 if Text = null then
1611 if Full_Lib_File = No_File then
1612 Verbose_Msg (Lib_File, "missing.");
1614 elsif Obj_Stamp (Obj_Stamp'First) = ' ' then
1615 Verbose_Msg (Full_Obj_File, "missing.");
1617 else
1618 Verbose_Msg
1619 (Full_Lib_File, "(" & String (Lib_Stamp) & ") newer than",
1620 Full_Obj_File, "(" & String (Obj_Stamp) & ")");
1621 end if;
1623 else
1624 ALI := Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
1625 Free (Text);
1627 if ALI = No_ALI_Id then
1628 Verbose_Msg (Full_Lib_File, "incorrectly formatted ALI file");
1629 return;
1631 elsif ALIs.Table (ALI).Ver (1 .. ALIs.Table (ALI).Ver_Len) /=
1632 Verbose_Library_Version
1633 then
1634 Verbose_Msg (Full_Lib_File, "compiled with old GNAT version");
1635 ALI := No_ALI_Id;
1636 return;
1637 end if;
1639 -- Don't take Ali file into account if it was generated with
1640 -- errors.
1642 if ALIs.Table (ALI).Compile_Errors then
1643 Verbose_Msg (Full_Lib_File, "had errors, must be recompiled");
1644 ALI := No_ALI_Id;
1645 return;
1646 end if;
1648 -- Don't take Ali file into account if it was generated without
1649 -- object.
1651 if Operating_Mode /= Check_Semantics
1652 and then ALIs.Table (ALI).No_Object
1653 then
1654 Verbose_Msg (Full_Lib_File, "has no corresponding object");
1655 ALI := No_ALI_Id;
1656 return;
1657 end if;
1659 -- Check for matching compiler switches if needed
1661 if Check_Switches then
1663 -- First, collect all the switches
1665 Collect_Arguments
1666 (Source_File, Source_Index, Is_Main_Source, The_Args);
1668 Prev_Switch := Dummy_Switch;
1670 Get_Name_String (ALIs.Table (ALI).Sfile);
1672 Switches_To_Check.Set_Last (0);
1674 for J in 1 .. Last_Argument loop
1676 -- Skip non switches -c, -I and -o switches
1678 if Arguments (J) (1) = '-'
1679 and then Arguments (J) (2) /= 'c'
1680 and then Arguments (J) (2) /= 'o'
1681 and then Arguments (J) (2) /= 'I'
1682 then
1683 Normalize_Compiler_Switches
1684 (Arguments (J).all,
1685 Normalized_Switches,
1686 Last_Norm_Switch);
1688 for K in 1 .. Last_Norm_Switch loop
1689 Switches_To_Check.Increment_Last;
1690 Switches_To_Check.Table (Switches_To_Check.Last) :=
1691 Normalized_Switches (K);
1692 end loop;
1693 end if;
1694 end loop;
1696 for J in 1 .. Switches_To_Check.Last loop
1698 -- Comparing switches is delicate because gcc reorders a number
1699 -- of switches, according to lang-specs.h, but gnatmake doesn't
1700 -- have sufficient knowledge to perform the same reordering.
1701 -- Instead, we ignore orders between different "first letter"
1702 -- switches, but keep orders between same switches, e.g -O -O2
1703 -- is different than -O2 -O, but -g -O is equivalent to -O -g.
1705 if Switches_To_Check.Table (J) (2) /= Prev_Switch (2) or else
1706 (Prev_Switch'Length >= 6 and then
1707 Prev_Switch (2 .. 5) = "gnat" and then
1708 Switches_To_Check.Table (J)'Length >= 6 and then
1709 Switches_To_Check.Table (J) (2 .. 5) = "gnat" and then
1710 Prev_Switch (6) /= Switches_To_Check.Table (J) (6))
1711 then
1712 Prev_Switch := Switches_To_Check.Table (J);
1713 Arg :=
1714 Units.Table (ALIs.Table (ALI).First_Unit).First_Arg;
1715 end if;
1717 Switch_Found := False;
1719 for K in Arg ..
1720 Units.Table (ALIs.Table (ALI).First_Unit).Last_Arg
1721 loop
1723 Switches_To_Check.Table (J).all = Args.Table (K).all
1724 then
1725 Arg := K + 1;
1726 Switch_Found := True;
1727 exit;
1728 end if;
1729 end loop;
1731 if not Switch_Found then
1732 if Verbose_Mode then
1733 Verbose_Msg (ALIs.Table (ALI).Sfile,
1734 "switch mismatch """ &
1735 Switches_To_Check.Table (J).all & '"');
1736 end if;
1738 ALI := No_ALI_Id;
1739 return;
1740 end if;
1741 end loop;
1743 if Switches_To_Check.Last /=
1744 Integer (Units.Table (ALIs.Table (ALI).First_Unit).Last_Arg -
1745 Units.Table (ALIs.Table (ALI).First_Unit).First_Arg + 1)
1746 then
1747 if Verbose_Mode then
1748 Verbose_Msg (ALIs.Table (ALI).Sfile,
1749 "different number of switches");
1751 for K in Units.Table (ALIs.Table (ALI).First_Unit).First_Arg
1752 .. Units.Table (ALIs.Table (ALI).First_Unit).Last_Arg
1753 loop
1754 Write_Str (Args.Table (K).all);
1755 Write_Char (' ');
1756 end loop;
1758 Write_Eol;
1760 for J in 1 .. Switches_To_Check.Last loop
1761 Write_Str (Switches_To_Check.Table (J).all);
1762 Write_Char (' ');
1763 end loop;
1765 Write_Eol;
1766 end if;
1768 ALI := No_ALI_Id;
1769 return;
1770 end if;
1771 end if;
1773 -- Get the source files and their message digests. Note that some
1774 -- sources may be missing if ALI is out-of-date.
1776 Set_Source_Table (ALI);
1778 Modified_Source := Time_Stamp_Mismatch (ALI, Read_Only);
1780 if Modified_Source /= No_File then
1781 ALI := No_ALI_Id;
1783 if Verbose_Mode then
1784 Source_Name := Full_Source_Name (Modified_Source);
1786 if Source_Name /= No_File then
1787 Verbose_Msg (Source_Name, "time stamp mismatch");
1788 else
1789 Verbose_Msg (Modified_Source, "missing");
1790 end if;
1791 end if;
1793 else
1794 New_Spec := First_New_Spec (ALI);
1796 if New_Spec /= No_File then
1797 ALI := No_ALI_Id;
1799 if Verbose_Mode then
1800 Source_Name := Full_Source_Name (New_Spec);
1802 if Source_Name /= No_File then
1803 Verbose_Msg (Source_Name, "new spec");
1804 else
1805 Verbose_Msg (New_Spec, "old spec missing");
1806 end if;
1807 end if;
1809 elsif not Read_Only and then Main_Project /= No_Project then
1811 if not Check_Source_Info_In_ALI (ALI) then
1812 ALI := No_ALI_Id;
1813 return;
1814 end if;
1816 -- Check that the ALI file is in the correct object directory.
1817 -- If it is in the object directory of a project that is
1818 -- extended and it depends on a source that is in one of its
1819 -- extending projects, then the ALI file is not in the correct
1820 -- object directory.
1822 -- First, find the project of this ALI file. As there may be
1823 -- several projects with the same object directory, we first
1824 -- need to find the project of the source.
1826 ALI_Project := No_Project;
1828 declare
1829 Udata : Prj.Unit_Index;
1831 begin
1832 Udata := Units_Htable.Get_First (Project_Tree.Units_HT);
1833 while Udata /= No_Unit_Index loop
1834 if Udata.File_Names (Impl) /= null
1835 and then Udata.File_Names (Impl).File = Source_File
1836 then
1837 ALI_Project := Udata.File_Names (Impl).Project;
1838 exit;
1840 elsif Udata.File_Names (Spec) /= null
1841 and then Udata.File_Names (Spec).File = Source_File
1842 then
1843 ALI_Project := Udata.File_Names (Spec).Project;
1844 exit;
1845 end if;
1847 Udata := Units_Htable.Get_Next (Project_Tree.Units_HT);
1848 end loop;
1849 end;
1851 if ALI_Project = No_Project then
1852 return;
1853 end if;
1855 declare
1856 Obj_Dir : Path_Name_Type;
1857 Res_Obj_Dir : constant String :=
1858 Normalize_Pathname
1859 (Dir_Name
1860 (Get_Name_String (Full_Lib_File)),
1861 Resolve_Links => True,
1862 Case_Sensitive => False);
1864 begin
1865 Name_Len := 0;
1866 Add_Str_To_Name_Buffer (Res_Obj_Dir);
1868 if not Is_Directory_Separator (Name_Buffer (Name_Len)) then
1869 Add_Char_To_Name_Buffer (Directory_Separator);
1870 end if;
1872 Obj_Dir := Name_Find;
1874 while ALI_Project /= No_Project
1875 and then Obj_Dir /= ALI_Project.Object_Directory.Name
1876 loop
1877 ALI_Project := ALI_Project.Extended_By;
1878 end loop;
1879 end;
1881 if ALI_Project = No_Project then
1882 ALI := No_ALI_Id;
1884 Verbose_Msg
1885 (Lib_File, " wrong object directory");
1886 return;
1887 end if;
1889 -- If the ALI project is not extended, then it must be in
1890 -- the correct object directory.
1892 if ALI_Project.Extended_By = No_Project then
1893 return;
1894 end if;
1896 -- Count the extending projects
1898 declare
1899 Num_Ext : Natural;
1900 Proj : Project_Id;
1902 begin
1903 Num_Ext := 0;
1904 Proj := ALI_Project;
1905 loop
1906 Proj := Proj.Extended_By;
1907 exit when Proj = No_Project;
1908 Num_Ext := Num_Ext + 1;
1909 end loop;
1911 -- Make a list of the extending projects
1913 declare
1914 Projects : array (1 .. Num_Ext) of Project_Id;
1915 Dep : Sdep_Record;
1916 OK : Boolean := True;
1917 UID : Unit_Index;
1919 begin
1920 Proj := ALI_Project;
1921 for J in Projects'Range loop
1922 Proj := Proj.Extended_By;
1923 Projects (J) := Proj;
1924 end loop;
1926 -- Now check if any of the dependant sources are in
1927 -- any of these extending projects.
1929 D_Chk :
1930 for D in ALIs.Table (ALI).First_Sdep ..
1931 ALIs.Table (ALI).Last_Sdep
1932 loop
1933 Dep := Sdep.Table (D);
1934 UID := Units_Htable.Get_First (Project_Tree.Units_HT);
1935 Proj := No_Project;
1937 Unit_Loop :
1938 while UID /= null loop
1939 if UID.File_Names (Impl) /= null
1940 and then UID.File_Names (Impl).File = Dep.Sfile
1941 then
1942 Proj := UID.File_Names (Impl).Project;
1944 elsif UID.File_Names (Spec) /= null
1945 and then UID.File_Names (Spec).File = Dep.Sfile
1946 then
1947 Proj := UID.File_Names (Spec).Project;
1948 end if;
1950 -- If a source is in a project, check if it is one
1951 -- in the list.
1953 if Proj /= No_Project then
1954 for J in Projects'Range loop
1955 if Proj = Projects (J) then
1956 OK := False;
1957 exit D_Chk;
1958 end if;
1959 end loop;
1961 exit Unit_Loop;
1962 end if;
1964 UID :=
1965 Units_Htable.Get_Next (Project_Tree.Units_HT);
1966 end loop Unit_Loop;
1967 end loop D_Chk;
1969 -- If one of the dependent sources is in one project of
1970 -- the list, then we must recompile.
1972 if not OK then
1973 ALI := No_ALI_Id;
1974 Verbose_Msg (Lib_File, " wrong object directory");
1975 end if;
1976 end;
1977 end;
1978 end if;
1979 end if;
1980 end if;
1981 end Check;
1983 ------------------------
1984 -- Check_For_S_Switch --
1985 ------------------------
1987 procedure Check_For_S_Switch is
1988 begin
1989 -- By default, we generate an object file
1991 Output_Is_Object := True;
1993 for Arg in 1 .. Last_Argument loop
1994 if Arguments (Arg).all = "-S" then
1995 Output_Is_Object := False;
1997 elsif Arguments (Arg).all = "-c" then
1998 Output_Is_Object := True;
1999 end if;
2000 end loop;
2001 end Check_For_S_Switch;
2003 --------------------------
2004 -- Check_Linker_Options --
2005 --------------------------
2007 procedure Check_Linker_Options
2008 (E_Stamp : Time_Stamp_Type;
2009 O_File : out File_Name_Type;
2010 O_Stamp : out Time_Stamp_Type)
2012 procedure Check_File (File : File_Name_Type);
2013 -- Update O_File and O_Stamp if the given file is younger than E_Stamp
2014 -- and O_Stamp, or if O_File is No_File and File does not exist.
2016 function Get_Library_File (Name : String) return File_Name_Type;
2017 -- Return the full file name including path of a library based
2018 -- on the name specified with the -l linker option, using the
2019 -- Ada object path. Return No_File if no such file can be found.
2021 type Char_Array is array (Natural) of Character;
2022 type Char_Array_Access is access constant Char_Array;
2024 Template : Char_Array_Access;
2025 pragma Import (C, Template, "__gnat_library_template");
2027 ----------------
2028 -- Check_File --
2029 ----------------
2031 procedure Check_File (File : File_Name_Type) is
2032 Stamp : Time_Stamp_Type;
2033 Name : File_Name_Type := File;
2035 begin
2036 Get_Name_String (Name);
2038 -- Remove any trailing NUL characters
2040 while Name_Len >= Name_Buffer'First
2041 and then Name_Buffer (Name_Len) = NUL
2042 loop
2043 Name_Len := Name_Len - 1;
2044 end loop;
2046 if Name_Len = 0 then
2047 return;
2049 elsif Name_Buffer (1) = '-' then
2051 -- Do not check if File is a switch other than "-l"
2053 if Name_Buffer (2) /= 'l' then
2054 return;
2055 end if;
2057 -- The argument is a library switch, get actual name. It
2058 -- is necessary to make a copy of the relevant part of
2059 -- Name_Buffer as Get_Library_Name uses Name_Buffer as well.
2061 declare
2062 Base_Name : constant String := Name_Buffer (3 .. Name_Len);
2064 begin
2065 Name := Get_Library_File (Base_Name);
2066 end;
2068 if Name = No_File then
2069 return;
2070 end if;
2071 end if;
2073 Stamp := File_Stamp (Name);
2075 -- Find the youngest object file that is younger than the
2076 -- executable. If no such file exist, record the first object
2077 -- file that is not found.
2079 if (O_Stamp < Stamp and then E_Stamp < Stamp)
2080 or else (O_File = No_File and then Stamp (Stamp'First) = ' ')
2081 then
2082 O_Stamp := Stamp;
2083 O_File := Name;
2085 -- Strip the trailing NUL if present
2087 Get_Name_String (O_File);
2089 if Name_Buffer (Name_Len) = NUL then
2090 Name_Len := Name_Len - 1;
2091 O_File := Name_Find;
2092 end if;
2093 end if;
2094 end Check_File;
2096 ----------------------
2097 -- Get_Library_Name --
2098 ----------------------
2100 -- See comments in a-adaint.c about template syntax
2102 function Get_Library_File (Name : String) return File_Name_Type is
2103 File : File_Name_Type := No_File;
2105 begin
2106 Name_Len := 0;
2108 for Ptr in Template'Range loop
2109 case Template (Ptr) is
2110 when '*' =>
2111 Add_Str_To_Name_Buffer (Name);
2113 when ';' =>
2114 File := Full_Lib_File_Name (Name_Find);
2115 exit when File /= No_File;
2116 Name_Len := 0;
2118 when NUL =>
2119 exit;
2121 when others =>
2122 Add_Char_To_Name_Buffer (Template (Ptr));
2123 end case;
2124 end loop;
2126 -- The for loop exited because the end of the template
2127 -- was reached. File contains the last possible file name
2128 -- for the library.
2130 if File = No_File and then Name_Len > 0 then
2131 File := Full_Lib_File_Name (Name_Find);
2132 end if;
2134 return File;
2135 end Get_Library_File;
2137 -- Start of processing for Check_Linker_Options
2139 begin
2140 O_File := No_File;
2141 O_Stamp := (others => ' ');
2143 -- Process linker options from the ALI files
2145 for Opt in 1 .. Linker_Options.Last loop
2146 Check_File (File_Name_Type (Linker_Options.Table (Opt).Name));
2147 end loop;
2149 -- Process options given on the command line
2151 for Opt in Linker_Switches.First .. Linker_Switches.Last loop
2153 -- Check if the previous Opt has one of the two switches
2154 -- that take an extra parameter. (See GCC manual.)
2156 if Opt = Linker_Switches.First
2157 or else (Linker_Switches.Table (Opt - 1).all /= "-u"
2158 and then
2159 Linker_Switches.Table (Opt - 1).all /= "-Xlinker"
2160 and then
2161 Linker_Switches.Table (Opt - 1).all /= "-L")
2162 then
2163 Name_Len := 0;
2164 Add_Str_To_Name_Buffer (Linker_Switches.Table (Opt).all);
2165 Check_File (Name_Find);
2166 end if;
2167 end loop;
2169 end Check_Linker_Options;
2171 -----------------
2172 -- Check_Steps --
2173 -----------------
2175 procedure Check_Steps is
2176 begin
2177 -- If either -c, -b or -l has been specified, we will not necessarily
2178 -- execute all steps.
2180 if Make_Steps then
2181 Do_Compile_Step := Do_Compile_Step and Compile_Only;
2182 Do_Bind_Step := Do_Bind_Step and Bind_Only;
2183 Do_Link_Step := Do_Link_Step and Link_Only;
2185 -- If -c has been specified, but not -b, ignore any potential -l
2187 if Do_Compile_Step and then not Do_Bind_Step then
2188 Do_Link_Step := False;
2189 end if;
2190 end if;
2191 end Check_Steps;
2193 -----------------------
2194 -- Collect_Arguments --
2195 -----------------------
2197 procedure Collect_Arguments
2198 (Source_File : File_Name_Type;
2199 Source_Index : Int;
2200 Is_Main_Source : Boolean;
2201 Args : Argument_List)
2203 begin
2204 Arguments_Project := No_Project;
2205 Last_Argument := 0;
2206 Add_Arguments (Args);
2208 if Main_Project /= No_Project then
2209 declare
2210 Source_File_Name : constant String :=
2211 Get_Name_String (Source_File);
2212 Compiler_Package : Prj.Package_Id;
2213 Switches : Prj.Variable_Value;
2215 begin
2216 Prj.Env.
2217 Get_Reference
2218 (Source_File_Name => Source_File_Name,
2219 Project => Arguments_Project,
2220 Path => Arguments_Path_Name,
2221 In_Tree => Project_Tree);
2223 -- If the source is not a source of a project file, add the
2224 -- recorded arguments. Check will be done later if the source
2225 -- need to be compiled that the switch -x has been used.
2227 if Arguments_Project = No_Project then
2228 Add_Arguments (The_Saved_Gcc_Switches.all);
2230 elsif not Arguments_Project.Externally_Built then
2231 -- We get the project directory for the relative path
2232 -- switches and arguments.
2234 Arguments_Project := Ultimate_Extending_Project_Of
2235 (Arguments_Project);
2237 -- If building a dynamic or relocatable library, compile with
2238 -- PIC option, if it exists.
2240 if Arguments_Project.Library
2241 and then Arguments_Project.Library_Kind /= Static
2242 then
2243 declare
2244 PIC : constant String := MLib.Tgt.PIC_Option;
2246 begin
2247 if PIC /= "" then
2248 Add_Arguments ((1 => new String'(PIC)));
2249 end if;
2250 end;
2251 end if;
2253 -- We now look for package Compiler and get the switches from
2254 -- this package.
2256 Compiler_Package :=
2257 Prj.Util.Value_Of
2258 (Name => Name_Compiler,
2259 In_Packages => Arguments_Project.Decl.Packages,
2260 In_Tree => Project_Tree);
2262 if Compiler_Package /= No_Package then
2264 -- If package Gnatmake.Compiler exists, we get the specific
2265 -- switches for the current source, or the global switches,
2266 -- if any.
2268 Switches :=
2269 Switches_Of
2270 (Source_File => Source_File,
2271 Source_File_Name => Source_File_Name,
2272 Source_Index => Source_Index,
2273 Project => Arguments_Project,
2274 In_Package => Compiler_Package,
2275 Allow_ALI => False);
2277 end if;
2279 case Switches.Kind is
2281 -- We have a list of switches. We add these switches,
2282 -- plus the saved gcc switches.
2284 when List =>
2286 declare
2287 Current : String_List_Id := Switches.Values;
2288 Element : String_Element;
2289 Number : Natural := 0;
2291 begin
2292 while Current /= Nil_String loop
2293 Element := Project_Tree.String_Elements.
2294 Table (Current);
2295 Number := Number + 1;
2296 Current := Element.Next;
2297 end loop;
2299 declare
2300 New_Args : Argument_List (1 .. Number);
2301 Last_New : Natural := 0;
2302 Dir_Path : constant String := Get_Name_String
2303 (Arguments_Project.Directory.Name);
2305 begin
2306 Current := Switches.Values;
2308 for Index in New_Args'Range loop
2309 Element := Project_Tree.String_Elements.
2310 Table (Current);
2311 Get_Name_String (Element.Value);
2313 if Name_Len > 0 then
2314 Last_New := Last_New + 1;
2315 New_Args (Last_New) :=
2316 new String'(Name_Buffer (1 .. Name_Len));
2317 Test_If_Relative_Path
2318 (New_Args (Last_New),
2319 Parent => Dir_Path,
2320 Including_Non_Switch => False);
2321 end if;
2323 Current := Element.Next;
2324 end loop;
2326 Add_Arguments
2327 (Configuration_Pragmas_Switch
2328 (Arguments_Project) &
2329 New_Args (1 .. Last_New) &
2330 The_Saved_Gcc_Switches.all);
2331 end;
2332 end;
2334 -- We have a single switch. We add this switch,
2335 -- plus the saved gcc switches.
2337 when Single =>
2338 Get_Name_String (Switches.Value);
2340 declare
2341 New_Args : Argument_List :=
2342 (1 => new String'
2343 (Name_Buffer (1 .. Name_Len)));
2344 Dir_Path : constant String :=
2345 Get_Name_String
2346 (Arguments_Project.Directory.Name);
2348 begin
2349 Test_If_Relative_Path
2350 (New_Args (1),
2351 Parent => Dir_Path,
2352 Including_Non_Switch => False);
2353 Add_Arguments
2354 (Configuration_Pragmas_Switch (Arguments_Project) &
2355 New_Args & The_Saved_Gcc_Switches.all);
2356 end;
2358 -- We have no switches from Gnatmake.Compiler.
2359 -- We add the saved gcc switches.
2361 when Undefined =>
2362 Add_Arguments
2363 (Configuration_Pragmas_Switch (Arguments_Project) &
2364 The_Saved_Gcc_Switches.all);
2365 end case;
2366 end if;
2367 end;
2368 end if;
2370 -- For VMS, when compiling the main source, add switch
2371 -- -mdebug-main=_ada_ so that the executable can be debugged
2372 -- by the standard VMS debugger.
2374 if not No_Main_Subprogram
2375 and then Targparm.OpenVMS_On_Target
2376 and then Is_Main_Source
2377 then
2378 -- First, check if compilation will be invoked with -g
2380 for J in 1 .. Last_Argument loop
2381 if Arguments (J)'Length >= 2
2382 and then Arguments (J) (1 .. 2) = "-g"
2383 and then (Arguments (J)'Length < 5
2384 or else Arguments (J) (1 .. 5) /= "-gnat")
2385 then
2386 Add_Arguments
2387 ((1 => new String'("-mdebug-main=_ada_")));
2388 exit;
2389 end if;
2390 end loop;
2391 end if;
2393 -- Set Output_Is_Object, depending if there is a -S switch.
2394 -- If the bind step is not performed, and there is a -S switch,
2395 -- then we will not check for a valid object file.
2397 Check_For_S_Switch;
2398 end Collect_Arguments;
2400 ---------------------
2401 -- Compile_Sources --
2402 ---------------------
2404 procedure Compile_Sources
2405 (Main_Source : File_Name_Type;
2406 Args : Argument_List;
2407 First_Compiled_File : out File_Name_Type;
2408 Most_Recent_Obj_File : out File_Name_Type;
2409 Most_Recent_Obj_Stamp : out Time_Stamp_Type;
2410 Main_Unit : out Boolean;
2411 Compilation_Failures : out Natural;
2412 Main_Index : Int := 0;
2413 Check_Readonly_Files : Boolean := False;
2414 Do_Not_Execute : Boolean := False;
2415 Force_Compilations : Boolean := False;
2416 Keep_Going : Boolean := False;
2417 In_Place_Mode : Boolean := False;
2418 Initialize_ALI_Data : Boolean := True;
2419 Max_Process : Positive := 1)
2421 Source_Unit : Unit_Name_Type;
2422 -- Current source unit
2424 Source_File : File_Name_Type;
2425 -- Current source file
2427 Full_Source_File : File_Name_Type;
2428 -- Full name of the current source file
2430 Lib_File : File_Name_Type;
2431 -- Current library file
2433 Full_Lib_File : File_Name_Type;
2434 -- Full name of the current library file
2436 Obj_File : File_Name_Type;
2437 -- Full name of the object file corresponding to Lib_File
2439 Obj_Stamp : Time_Stamp_Type;
2440 -- Time stamp of the current object file
2442 Sfile : File_Name_Type;
2443 -- Contains the source file of the units withed by Source_File
2445 Uname : Unit_Name_Type;
2446 -- Contains the unit name of the units withed by Source_File
2448 ALI : ALI_Id;
2449 -- ALI Id of the current ALI file
2451 -- Comment following declarations ???
2453 Read_Only : Boolean := False;
2455 Compilation_OK : Boolean;
2456 Need_To_Compile : Boolean;
2458 Pid : Process_Id;
2459 Text : Text_Buffer_Ptr;
2461 Mfile : Natural := No_Mapping_File;
2463 Need_To_Check_Standard_Library : Boolean :=
2464 Check_Readonly_Files
2465 and not Unique_Compile;
2467 Mapping_File_Arg : String_Access;
2469 Process_Created : Boolean := False;
2471 procedure Add_Process
2472 (Pid : Process_Id;
2473 Sfile : File_Name_Type;
2474 Afile : File_Name_Type;
2475 Uname : Unit_Name_Type;
2476 Mfile : Natural := No_Mapping_File);
2477 -- Adds process Pid to the current list of outstanding compilation
2478 -- processes and record the full name of the source file Sfile that
2479 -- we are compiling, the name of its library file Afile and the
2480 -- name of its unit Uname. If Mfile is not equal to No_Mapping_File,
2481 -- it is the index of the mapping file used during compilation in the
2482 -- array The_Mapping_File_Names.
2484 procedure Await_Compile
2485 (Sfile : out File_Name_Type;
2486 Afile : out File_Name_Type;
2487 Uname : out Unit_Name_Type;
2488 OK : out Boolean);
2489 -- Awaits that an outstanding compilation process terminates. When
2490 -- it does set Sfile to the name of the source file that was compiled
2491 -- Afile to the name of its library file and Uname to the name of its
2492 -- unit. Note that this time stamp can be used to check whether the
2493 -- compilation did generate an object file. OK is set to True if the
2494 -- compilation succeeded. Note that Sfile, Afile and Uname could be
2495 -- resp. No_File, No_File and No_Name if there were no compilations
2496 -- to wait for.
2498 function Bad_Compilation_Count return Natural;
2499 -- Returns the number of compilation failures
2501 procedure Check_Standard_Library;
2502 -- Check if s-stalib.adb needs to be compiled
2504 procedure Collect_Arguments_And_Compile (Source_Index : Int);
2505 -- Collect arguments from project file (if any) and compile
2507 function Compile
2508 (Project : Project_Id;
2509 S : File_Name_Type;
2510 L : File_Name_Type;
2511 Source_Index : Int;
2512 Args : Argument_List) return Process_Id;
2513 -- Compiles S using Args. If S is a GNAT predefined source "-gnatpg" is
2514 -- added to Args. Non blocking call. L corresponds to the expected
2515 -- library file name. Process_Id of the process spawned to execute the
2516 -- compilation.
2518 package Good_ALI is new Table.Table (
2519 Table_Component_Type => ALI_Id,
2520 Table_Index_Type => Natural,
2521 Table_Low_Bound => 1,
2522 Table_Initial => 50,
2523 Table_Increment => 100,
2524 Table_Name => "Make.Good_ALI");
2525 -- Contains the set of valid ALI files that have not yet been scanned
2527 function Good_ALI_Present return Boolean;
2528 -- Returns True if any ALI file was recorded in the previous set
2530 procedure Get_Mapping_File (Project : Project_Id);
2531 -- Get a mapping file name. If there is one to be reused, reuse it.
2532 -- Otherwise, create a new mapping file.
2534 function Get_Next_Good_ALI return ALI_Id;
2535 -- Returns the next good ALI_Id record
2537 procedure Record_Failure
2538 (File : File_Name_Type;
2539 Unit : Unit_Name_Type;
2540 Found : Boolean := True);
2541 -- Records in the previous table that the compilation for File failed.
2542 -- If Found is False then the compilation of File failed because we
2543 -- could not find it. Records also Unit when possible.
2545 procedure Record_Good_ALI (A : ALI_Id);
2546 -- Records in the previous set the Id of an ALI file
2548 -----------------
2549 -- Add_Process --
2550 -----------------
2552 procedure Add_Process
2553 (Pid : Process_Id;
2554 Sfile : File_Name_Type;
2555 Afile : File_Name_Type;
2556 Uname : Unit_Name_Type;
2557 Mfile : Natural := No_Mapping_File)
2559 OC1 : constant Positive := Outstanding_Compiles + 1;
2561 begin
2562 pragma Assert (OC1 <= Max_Process);
2563 pragma Assert (Pid /= Invalid_Pid);
2565 Running_Compile (OC1).Pid := Pid;
2566 Running_Compile (OC1).Full_Source_File := Sfile;
2567 Running_Compile (OC1).Lib_File := Afile;
2568 Running_Compile (OC1).Source_Unit := Uname;
2569 Running_Compile (OC1).Mapping_File := Mfile;
2570 Running_Compile (OC1).Project := Arguments_Project;
2571 Running_Compile (OC1).Syntax_Only := Syntax_Only;
2572 Running_Compile (OC1).Output_Is_Object := Output_Is_Object;
2574 Outstanding_Compiles := OC1;
2575 end Add_Process;
2577 --------------------
2578 -- Await_Compile --
2579 -------------------
2581 procedure Await_Compile
2582 (Sfile : out File_Name_Type;
2583 Afile : out File_Name_Type;
2584 Uname : out Unit_Name_Type;
2585 OK : out Boolean)
2587 Pid : Process_Id;
2588 Project : Project_Id;
2589 Data : Project_Compilation_Access;
2591 begin
2592 pragma Assert (Outstanding_Compiles > 0);
2594 Sfile := No_File;
2595 Afile := No_File;
2596 Uname := No_Unit_Name;
2597 OK := False;
2599 -- The loop here is a work-around for a problem on VMS; in some
2600 -- circumstances (shared library and several executables, for
2601 -- example), there are child processes other than compilation
2602 -- processes that are received. Until this problem is resolved,
2603 -- we will ignore such processes.
2605 loop
2606 Wait_Process (Pid, OK);
2608 if Pid = Invalid_Pid then
2609 return;
2610 end if;
2612 for J in Running_Compile'First .. Outstanding_Compiles loop
2613 if Pid = Running_Compile (J).Pid then
2614 Sfile := Running_Compile (J).Full_Source_File;
2615 Afile := Running_Compile (J).Lib_File;
2616 Uname := Running_Compile (J).Source_Unit;
2617 Syntax_Only := Running_Compile (J).Syntax_Only;
2618 Output_Is_Object := Running_Compile (J).Output_Is_Object;
2619 Project := Running_Compile (J).Project;
2621 -- If a mapping file was used by this compilation,
2622 -- get its file name for reuse by a subsequent compilation
2624 if Running_Compile (J).Mapping_File /= No_Mapping_File then
2625 Data := Project_Compilation_Htable.Get
2626 (Project_Compilation, Project);
2627 Data.Last_Free_Indices := Data.Last_Free_Indices + 1;
2628 Data.Free_Mapping_File_Indices (Data.Last_Free_Indices) :=
2629 Running_Compile (J).Mapping_File;
2630 end if;
2632 -- To actually remove this Pid and related info from
2633 -- Running_Compile replace its entry with the last valid
2634 -- entry in Running_Compile.
2636 if J = Outstanding_Compiles then
2637 null;
2639 else
2640 Running_Compile (J) :=
2641 Running_Compile (Outstanding_Compiles);
2642 end if;
2644 Outstanding_Compiles := Outstanding_Compiles - 1;
2645 return;
2646 end if;
2647 end loop;
2649 -- This child process was not one of our compilation processes;
2650 -- just ignore it for now.
2652 -- raise Program_Error;
2653 end loop;
2654 end Await_Compile;
2656 ---------------------------
2657 -- Bad_Compilation_Count --
2658 ---------------------------
2660 function Bad_Compilation_Count return Natural is
2661 begin
2662 return Bad_Compilation.Last - Bad_Compilation.First + 1;
2663 end Bad_Compilation_Count;
2665 ----------------------------
2666 -- Check_Standard_Library --
2667 ----------------------------
2669 procedure Check_Standard_Library is
2670 begin
2671 Need_To_Check_Standard_Library := False;
2673 if not Targparm.Suppress_Standard_Library_On_Target then
2674 declare
2675 Sfile : File_Name_Type;
2676 Add_It : Boolean := True;
2678 begin
2679 Name_Len := 0;
2680 Add_Str_To_Name_Buffer (Standard_Library_Package_Body_Name);
2681 Sfile := Name_Enter;
2683 -- If we have a special runtime, we add the standard
2684 -- library only if we can find it.
2686 if RTS_Switch then
2687 Add_It :=
2688 Find_File (Sfile, Osint.Source) /= No_File;
2689 end if;
2691 if Add_It then
2692 if Is_Marked (Sfile) then
2693 if Is_In_Obsoleted (Sfile) then
2694 Executable_Obsolete := True;
2695 end if;
2697 else
2698 Insert_Q (Sfile, Index => 0);
2699 Mark (Sfile, Index => 0);
2700 end if;
2701 end if;
2702 end;
2703 end if;
2704 end Check_Standard_Library;
2706 -----------------------------------
2707 -- Collect_Arguments_And_Compile --
2708 -----------------------------------
2710 procedure Collect_Arguments_And_Compile (Source_Index : Int) is
2711 begin
2712 -- Process_Created will be set True if an attempt is made to compile
2713 -- the source, that is if it is not in an externally built project.
2715 Process_Created := False;
2717 -- If we use mapping file (-P or -C switches), then get one
2719 if Create_Mapping_File then
2720 Get_Mapping_File (Arguments_Project);
2721 end if;
2723 -- If the source is part of a project file, we set the ADA_*_PATHs,
2724 -- check for an eventual library project, and use the full path.
2726 if Arguments_Project /= No_Project then
2727 if not Arguments_Project.Externally_Built then
2728 Prj.Env.Set_Ada_Paths
2729 (Arguments_Project,
2730 Project_Tree,
2731 Including_Libraries => True);
2733 if not Unique_Compile
2734 and then MLib.Tgt.Support_For_Libraries /= Prj.None
2735 then
2736 declare
2737 Prj : constant Project_Id :=
2738 Ultimate_Extending_Project_Of (Arguments_Project);
2740 begin
2741 if Prj.Library
2742 and then not Prj.Externally_Built
2743 and then not Prj.Need_To_Build_Lib
2744 then
2745 -- Add to the Q all sources of the project that have
2746 -- not been marked.
2748 Insert_Project_Sources
2749 (The_Project => Prj,
2750 All_Projects => False,
2751 Into_Q => True);
2753 -- Now mark the project as processed
2755 Prj.Need_To_Build_Lib := True;
2756 end if;
2757 end;
2758 end if;
2760 Pid :=
2761 Compile
2762 (Arguments_Project,
2763 File_Name_Type (Arguments_Path_Name),
2764 Lib_File,
2765 Source_Index,
2766 Arguments (1 .. Last_Argument));
2767 Process_Created := True;
2768 end if;
2770 else
2771 -- If this is a source outside of any project file, make sure it
2772 -- will be compiled in object directory of the main project file.
2774 Pid :=
2775 Compile
2776 (Main_Project,
2777 Full_Source_File,
2778 Lib_File,
2779 Source_Index,
2780 Arguments (1 .. Last_Argument));
2781 Process_Created := True;
2782 end if;
2783 end Collect_Arguments_And_Compile;
2785 -------------
2786 -- Compile --
2787 -------------
2789 function Compile
2790 (Project : Project_Id;
2791 S : File_Name_Type;
2792 L : File_Name_Type;
2793 Source_Index : Int;
2794 Args : Argument_List) return Process_Id
2796 Comp_Args : Argument_List (Args'First .. Args'Last + 10);
2797 Comp_Next : Integer := Args'First;
2798 Comp_Last : Integer;
2799 Arg_Index : Integer;
2801 function Ada_File_Name (Name : File_Name_Type) return Boolean;
2802 -- Returns True if Name is the name of an ada source file
2803 -- (i.e. suffix is .ads or .adb)
2805 -------------------
2806 -- Ada_File_Name --
2807 -------------------
2809 function Ada_File_Name (Name : File_Name_Type) return Boolean is
2810 begin
2811 Get_Name_String (Name);
2812 return
2813 Name_Len > 4
2814 and then Name_Buffer (Name_Len - 3 .. Name_Len - 1) = ".ad"
2815 and then (Name_Buffer (Name_Len) = 'b'
2816 or else
2817 Name_Buffer (Name_Len) = 's');
2818 end Ada_File_Name;
2820 -- Start of processing for Compile
2822 begin
2823 Enter_Into_Obsoleted (S);
2825 -- By default, Syntax_Only is False
2827 Syntax_Only := False;
2829 for J in Args'Range loop
2830 if Args (J).all = "-gnats" then
2832 -- If we compile with -gnats, the bind step and the link step
2833 -- are inhibited. Also, we set Syntax_Only to True, so that
2834 -- we don't fail when we don't find the ALI file, after
2835 -- compilation.
2837 Do_Bind_Step := False;
2838 Do_Link_Step := False;
2839 Syntax_Only := True;
2841 elsif Args (J).all = "-gnatc" then
2843 -- If we compile with -gnatc, the bind step and the link step
2844 -- are inhibited. We set Syntax_Only to False for the case when
2845 -- -gnats was previously specified.
2847 Do_Bind_Step := False;
2848 Do_Link_Step := False;
2849 Syntax_Only := False;
2850 end if;
2851 end loop;
2853 Comp_Args (Comp_Next) := new String'("-gnatea");
2854 Comp_Next := Comp_Next + 1;
2856 Comp_Args (Comp_Next) := Comp_Flag;
2857 Comp_Next := Comp_Next + 1;
2859 -- Optimize the simple case where the gcc command line looks like
2860 -- gcc -c -I. ... -I- file.adb
2861 -- into
2862 -- gcc -c ... file.adb
2864 if Args (Args'First).all = "-I" & Normalized_CWD
2865 and then Args (Args'Last).all = "-I-"
2866 and then S = Strip_Directory (S)
2867 then
2868 Comp_Last := Comp_Next + Args'Length - 3;
2869 Arg_Index := Args'First + 1;
2871 else
2872 Comp_Last := Comp_Next + Args'Length - 1;
2873 Arg_Index := Args'First;
2874 end if;
2876 -- Make a deep copy of the arguments, because Normalize_Arguments
2877 -- may deallocate some arguments.
2879 for J in Comp_Next .. Comp_Last loop
2880 Comp_Args (J) := new String'(Args (Arg_Index).all);
2881 Arg_Index := Arg_Index + 1;
2882 end loop;
2884 -- Set -gnatpg for predefined files (for this purpose the renamings
2885 -- such as Text_IO do not count as predefined). Note that we strip
2886 -- the directory name from the source file name because the call to
2887 -- Fname.Is_Predefined_File_Name cannot deal with directory prefixes.
2889 declare
2890 Fname : constant File_Name_Type := Strip_Directory (S);
2892 begin
2893 if Is_Predefined_File_Name (Fname, False) then
2894 if Check_Readonly_Files then
2895 Comp_Args (Comp_Args'First + 2 .. Comp_Last + 1) :=
2896 Comp_Args (Comp_Args'First + 1 .. Comp_Last);
2897 Comp_Last := Comp_Last + 1;
2898 Comp_Args (Comp_Args'First + 1) := GNAT_Flag;
2900 else
2901 Make_Failed
2902 ("not allowed to compile """ &
2903 Get_Name_String (Fname) &
2904 """; use -a switch, or compile file with " &
2905 """-gnatg"" switch");
2906 end if;
2907 end if;
2908 end;
2910 -- Now check if the file name has one of the suffixes familiar to
2911 -- the gcc driver. If this is not the case then add the ada flag
2912 -- "-x ada".
2914 if not Ada_File_Name (S) and then not Targparm.AAMP_On_Target then
2915 Comp_Last := Comp_Last + 1;
2916 Comp_Args (Comp_Last) := Ada_Flag_1;
2917 Comp_Last := Comp_Last + 1;
2918 Comp_Args (Comp_Last) := Ada_Flag_2;
2919 end if;
2921 if Source_Index /= 0 then
2922 declare
2923 Num : constant String := Source_Index'Img;
2924 begin
2925 Comp_Last := Comp_Last + 1;
2926 Comp_Args (Comp_Last) :=
2927 new String'("-gnateI" & Num (Num'First + 1 .. Num'Last));
2928 end;
2929 end if;
2931 if Source_Index /= 0
2932 or else L /= Strip_Directory (L)
2933 or else Object_Directory_Path /= null
2934 then
2935 -- Build -o argument
2937 Get_Name_String (L);
2939 for J in reverse 1 .. Name_Len loop
2940 if Name_Buffer (J) = '.' then
2941 Name_Len := J + Object_Suffix'Length - 1;
2942 Name_Buffer (J .. Name_Len) := Object_Suffix;
2943 exit;
2944 end if;
2945 end loop;
2947 Comp_Last := Comp_Last + 1;
2948 Comp_Args (Comp_Last) := Output_Flag;
2949 Comp_Last := Comp_Last + 1;
2951 -- If an object directory was specified, prepend the object file
2952 -- name with this object directory.
2954 if Object_Directory_Path /= null then
2955 Comp_Args (Comp_Last) :=
2956 new String'(Object_Directory_Path.all &
2957 Name_Buffer (1 .. Name_Len));
2959 else
2960 Comp_Args (Comp_Last) :=
2961 new String'(Name_Buffer (1 .. Name_Len));
2962 end if;
2963 end if;
2965 if Create_Mapping_File and then Mapping_File_Arg /= null then
2966 Comp_Last := Comp_Last + 1;
2967 Comp_Args (Comp_Last) := new String'(Mapping_File_Arg.all);
2968 end if;
2970 Get_Name_String (S);
2972 Comp_Last := Comp_Last + 1;
2973 Comp_Args (Comp_Last) := new String'(Name_Buffer (1 .. Name_Len));
2975 -- Change to object directory of the project file, if necessary
2977 if Project /= No_Project then
2978 Change_To_Object_Directory (Project);
2979 end if;
2981 GNAT.OS_Lib.Normalize_Arguments (Comp_Args (Args'First .. Comp_Last));
2983 Comp_Last := Comp_Last + 1;
2984 Comp_Args (Comp_Last) := new String'("-gnatez");
2986 Display (Gcc.all, Comp_Args (Args'First .. Comp_Last));
2988 if Gcc_Path = null then
2989 Make_Failed ("error, unable to locate " & Gcc.all);
2990 end if;
2992 return
2993 GNAT.OS_Lib.Non_Blocking_Spawn
2994 (Gcc_Path.all, Comp_Args (Args'First .. Comp_Last));
2995 end Compile;
2997 ----------------------
2998 -- Get_Mapping_File --
2999 ----------------------
3001 procedure Get_Mapping_File (Project : Project_Id) is
3002 Data : Project_Compilation_Access;
3004 begin
3005 Data := Project_Compilation_Htable.Get (Project_Compilation, Project);
3007 -- If there is a mapping file ready to be reused, reuse it
3009 if Data.Last_Free_Indices > 0 then
3010 Mfile := Data.Free_Mapping_File_Indices (Data.Last_Free_Indices);
3011 Data.Last_Free_Indices := Data.Last_Free_Indices - 1;
3013 -- Otherwise, create and initialize a new one
3015 else
3016 Init_Mapping_File
3017 (Project => Project, Data => Data.all, File_Index => Mfile);
3018 end if;
3020 -- Put the name in the mapping file argument for the invocation
3021 -- of the compiler.
3023 Free (Mapping_File_Arg);
3024 Mapping_File_Arg :=
3025 new String'("-gnatem=" &
3026 Get_Name_String (Data.Mapping_File_Names (Mfile)));
3027 end Get_Mapping_File;
3029 -----------------------
3030 -- Get_Next_Good_ALI --
3031 -----------------------
3033 function Get_Next_Good_ALI return ALI_Id is
3034 ALI : ALI_Id;
3036 begin
3037 pragma Assert (Good_ALI_Present);
3038 ALI := Good_ALI.Table (Good_ALI.Last);
3039 Good_ALI.Decrement_Last;
3040 return ALI;
3041 end Get_Next_Good_ALI;
3043 ----------------------
3044 -- Good_ALI_Present --
3045 ----------------------
3047 function Good_ALI_Present return Boolean is
3048 begin
3049 return Good_ALI.First <= Good_ALI.Last;
3050 end Good_ALI_Present;
3052 --------------------
3053 -- Record_Failure --
3054 --------------------
3056 procedure Record_Failure
3057 (File : File_Name_Type;
3058 Unit : Unit_Name_Type;
3059 Found : Boolean := True)
3061 begin
3062 Bad_Compilation.Increment_Last;
3063 Bad_Compilation.Table (Bad_Compilation.Last) := (File, Unit, Found);
3064 end Record_Failure;
3066 ---------------------
3067 -- Record_Good_ALI --
3068 ---------------------
3070 procedure Record_Good_ALI (A : ALI_Id) is
3071 begin
3072 Good_ALI.Increment_Last;
3073 Good_ALI.Table (Good_ALI.Last) := A;
3074 end Record_Good_ALI;
3076 -- Start of processing for Compile_Sources
3078 begin
3079 pragma Assert (Args'First = 1);
3081 Outstanding_Compiles := 0;
3082 Running_Compile := new Comp_Data_Arr (1 .. Max_Process);
3084 -- Package and Queue initializations
3086 Good_ALI.Init;
3088 if First_Q_Initialization then
3089 Init_Q;
3090 end if;
3092 if Initialize_ALI_Data then
3093 Initialize_ALI;
3094 Initialize_ALI_Source;
3095 end if;
3097 -- The following two flags affect the behavior of ALI.Set_Source_Table.
3098 -- We set Check_Source_Files to True to ensure that source file
3099 -- time stamps are checked, and we set All_Sources to False to
3100 -- avoid checking the presence of the source files listed in the
3101 -- source dependency section of an ali file (which would be a mistake
3102 -- since the ali file may be obsolete).
3104 Check_Source_Files := True;
3105 All_Sources := False;
3107 -- Only insert in the Q if it is not already done, to avoid simultaneous
3108 -- compilations if -jnnn is used.
3110 if not Is_Marked (Main_Source, Main_Index) then
3111 Insert_Q (Main_Source, Index => Main_Index);
3112 Mark (Main_Source, Main_Index);
3113 end if;
3115 First_Compiled_File := No_File;
3116 Most_Recent_Obj_File := No_File;
3117 Most_Recent_Obj_Stamp := Empty_Time_Stamp;
3118 Main_Unit := False;
3120 -- Keep looping until there is no more work to do (the Q is empty)
3121 -- and all the outstanding compilations have terminated
3123 Make_Loop : while not Empty_Q or else Outstanding_Compiles > 0 loop
3125 -- If the user does not want to keep going in case of errors then
3126 -- wait for the remaining outstanding compiles and then exit.
3128 if Bad_Compilation_Count > 0 and then not Keep_Going then
3129 while Outstanding_Compiles > 0 loop
3130 Await_Compile
3131 (Full_Source_File, Lib_File, Source_Unit, Compilation_OK);
3133 if not Compilation_OK then
3134 Record_Failure (Full_Source_File, Source_Unit);
3135 end if;
3136 end loop;
3138 exit Make_Loop;
3139 end if;
3141 -- PHASE 1: Check if there is more work that we can do (i.e. the Q
3142 -- is non empty). If there is, do it only if we have not yet used
3143 -- up all the available processes.
3145 if not Empty_Q and then Outstanding_Compiles < Max_Process then
3146 declare
3147 Source_Index : Int;
3148 -- Index of the current unit in the current source file
3150 begin
3151 Extract_From_Q (Source_File, Source_Unit, Source_Index);
3152 Full_Source_File := Osint.Full_Source_Name (Source_File);
3153 Lib_File := Osint.Lib_File_Name
3154 (Source_File, Source_Index);
3155 Full_Lib_File := Osint.Full_Lib_File_Name (Lib_File);
3157 -- If this source has already been compiled, the executable is
3158 -- obsolete.
3160 if Is_In_Obsoleted (Source_File) then
3161 Executable_Obsolete := True;
3162 end if;
3164 -- If the library file is an Ada library skip it
3166 if Full_Lib_File /= No_File
3167 and then In_Ada_Lib_Dir (Full_Lib_File)
3168 then
3169 Verbose_Msg
3170 (Lib_File,
3171 "is in an Ada library",
3172 Prefix => " ",
3173 Minimum_Verbosity => Opt.High);
3175 -- If the library file is a read-only library skip it, but
3176 -- only if, when using project files, this library file is
3177 -- in the right object directory (a read-only ALI file
3178 -- in the object directory of a project being extended
3179 -- should not be skipped).
3181 elsif Full_Lib_File /= No_File
3182 and then not Check_Readonly_Files
3183 and then Is_Readonly_Library (Full_Lib_File)
3184 and then Is_In_Object_Directory (Source_File, Full_Lib_File)
3185 then
3186 Verbose_Msg
3187 (Lib_File,
3188 "is a read-only library",
3189 Prefix => " ",
3190 Minimum_Verbosity => Opt.High);
3192 -- The source file that we are checking cannot be located
3194 elsif Full_Source_File = No_File then
3195 Record_Failure (Source_File, Source_Unit, False);
3197 -- Source and library files can be located but are internal
3198 -- files
3200 elsif not Check_Readonly_Files
3201 and then Full_Lib_File /= No_File
3202 and then Is_Internal_File_Name (Source_File, False)
3203 then
3204 if Force_Compilations then
3205 Fail
3206 ("not allowed to compile """ &
3207 Get_Name_String (Source_File) &
3208 """; use -a switch, or compile file with " &
3209 """-gnatg"" switch");
3210 end if;
3212 Verbose_Msg
3213 (Lib_File,
3214 "is an internal library",
3215 Prefix => " ",
3216 Minimum_Verbosity => Opt.High);
3218 -- The source file that we are checking can be located
3220 else
3221 Collect_Arguments (Source_File, Source_Index,
3222 Source_File = Main_Source, Args);
3224 -- Do nothing if project of source is externally built
3226 if Arguments_Project = No_Project
3227 or else not Arguments_Project.Externally_Built
3228 then
3229 -- Don't waste any time if we have to recompile anyway
3231 Obj_Stamp := Empty_Time_Stamp;
3232 Need_To_Compile := Force_Compilations;
3234 if not Force_Compilations then
3235 Read_Only :=
3236 Full_Lib_File /= No_File
3237 and then not Check_Readonly_Files
3238 and then Is_Readonly_Library (Full_Lib_File);
3239 Check (Source_File, Source_Index,
3240 Source_File = Main_Source, Args, Lib_File,
3241 Read_Only, ALI, Obj_File, Obj_Stamp);
3242 Need_To_Compile := (ALI = No_ALI_Id);
3243 end if;
3245 if not Need_To_Compile then
3246 -- The ALI file is up-to-date. Record its Id
3248 Record_Good_ALI (ALI);
3250 -- Record the time stamp of the most recent object
3251 -- file as long as no (re)compilations are needed.
3253 if First_Compiled_File = No_File
3254 and then (Most_Recent_Obj_File = No_File
3255 or else Obj_Stamp > Most_Recent_Obj_Stamp)
3256 then
3257 Most_Recent_Obj_File := Obj_File;
3258 Most_Recent_Obj_Stamp := Obj_Stamp;
3259 end if;
3261 else
3262 -- Check that switch -x has been used if a source
3263 -- outside of project files need to be compiled.
3265 if Main_Project /= No_Project
3266 and then Arguments_Project = No_Project
3267 and then not External_Unit_Compilation_Allowed
3268 then
3269 Make_Failed ("external source ("
3270 & Get_Name_String (Source_File)
3271 & ") is not part of any project;"
3272 & " cannot be compiled without"
3273 & " gnatmake switch -x");
3274 end if;
3276 -- Is this the first file we have to compile?
3278 if First_Compiled_File = No_File then
3279 First_Compiled_File := Full_Source_File;
3280 Most_Recent_Obj_File := No_File;
3282 if Do_Not_Execute then
3283 exit Make_Loop;
3284 end if;
3285 end if;
3287 if In_Place_Mode then
3289 -- If the library file was not found, then save
3290 -- the library file near the source file.
3292 if Full_Lib_File = No_File then
3293 Lib_File := Osint.Lib_File_Name
3294 (Full_Source_File, Source_Index);
3296 -- If the library file was found, then save the
3297 -- library file in the same place.
3299 else
3300 Lib_File := Full_Lib_File;
3301 end if;
3303 end if;
3305 -- Start the compilation and record it. We can do
3306 -- this because there is at least one free process.
3308 Collect_Arguments_And_Compile (Source_Index);
3310 -- Make sure we could successfully start
3311 -- the Compilation.
3313 if Process_Created then
3314 if Pid = Invalid_Pid then
3315 Record_Failure (Full_Source_File, Source_Unit);
3316 else
3317 Add_Process
3318 (Pid,
3319 Full_Source_File,
3320 Lib_File,
3321 Source_Unit,
3322 Mfile);
3323 end if;
3324 end if;
3325 end if;
3326 end if;
3327 end if;
3328 end;
3329 end if;
3331 -- PHASE 2: Now check if we should wait for a compilation to
3332 -- finish. This is the case if all the available processes are
3333 -- busy compiling sources or there is nothing else to do
3334 -- (that is the Q is empty and there are no good ALIs to process).
3336 if Outstanding_Compiles = Max_Process
3337 or else (Empty_Q
3338 and then not Good_ALI_Present
3339 and then Outstanding_Compiles > 0)
3340 then
3341 Await_Compile
3342 (Full_Source_File, Lib_File, Source_Unit, Compilation_OK);
3344 if not Compilation_OK then
3345 Record_Failure (Full_Source_File, Source_Unit);
3346 end if;
3348 if Compilation_OK or else Keep_Going then
3350 -- Re-read the updated library file
3352 declare
3353 Saved_Object_Consistency : constant Boolean :=
3354 Check_Object_Consistency;
3356 begin
3357 -- If compilation was not OK, or if output is not an
3358 -- object file and we don't do the bind step, don't check
3359 -- for object consistency.
3361 Check_Object_Consistency :=
3362 Check_Object_Consistency
3363 and Compilation_OK
3364 and (Output_Is_Object or Do_Bind_Step);
3365 Text := Read_Library_Info (Lib_File);
3367 -- Restore Check_Object_Consistency to its initial value
3369 Check_Object_Consistency := Saved_Object_Consistency;
3370 end;
3372 -- If an ALI file was generated by this compilation, scan
3373 -- the ALI file and record it.
3375 -- If the scan fails, a previous ali file is inconsistent with
3376 -- the unit just compiled.
3378 if Text /= null then
3379 ALI :=
3380 Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
3382 if ALI = No_ALI_Id then
3384 -- Record a failure only if not already done
3386 if Compilation_OK then
3387 Inform
3388 (Lib_File,
3389 "incompatible ALI file, please recompile");
3390 Record_Failure (Full_Source_File, Source_Unit);
3391 end if;
3392 else
3393 Free (Text);
3394 Record_Good_ALI (ALI);
3395 end if;
3397 -- If we could not read the ALI file that was just generated
3398 -- then there could be a problem reading either the ALI or the
3399 -- corresponding object file (if Check_Object_Consistency is
3400 -- set Read_Library_Info checks that the time stamp of the
3401 -- object file is more recent than that of the ALI). However,
3402 -- we record a failure only if not already done.
3404 else
3405 if Compilation_OK and not Syntax_Only then
3406 Inform
3407 (Lib_File,
3408 "WARNING: ALI or object file not found after compile");
3409 Record_Failure (Full_Source_File, Source_Unit);
3410 end if;
3411 end if;
3412 end if;
3413 end if;
3415 -- PHASE 3: Check if we recorded good ALI files. If yes process
3416 -- them now in the order in which they have been recorded. There
3417 -- are two occasions in which we record good ali files. The first is
3418 -- in phase 1 when, after scanning an existing ALI file we realize
3419 -- it is up-to-date, the second instance is after a successful
3420 -- compilation.
3422 while Good_ALI_Present loop
3423 ALI := Get_Next_Good_ALI;
3425 declare
3426 Source_Index : Int := Unit_Index_Of (ALIs.Table (ALI).Afile);
3428 begin
3429 -- If we are processing the library file corresponding to the
3430 -- main source file check if this source can be a main unit.
3432 if ALIs.Table (ALI).Sfile = Main_Source and then
3433 Source_Index = Main_Index
3434 then
3435 Main_Unit := ALIs.Table (ALI).Main_Program /= None;
3436 end if;
3438 -- The following adds the standard library (s-stalib) to the
3439 -- list of files to be handled by gnatmake: this file and any
3440 -- files it depends on are always included in every bind,
3441 -- even if they are not in the explicit dependency list.
3442 -- Of course, it is not added if Suppress_Standard_Library
3443 -- is True.
3445 -- However, to avoid annoying output about s-stalib.ali being
3446 -- read only, when "-v" is used, we add the standard library
3447 -- only when "-a" is used.
3449 if Need_To_Check_Standard_Library then
3450 Check_Standard_Library;
3451 end if;
3453 -- Now insert in the Q the unmarked source files (i.e. those
3454 -- which have never been inserted in the Q and hence never
3455 -- considered). Only do that if Unique_Compile is False.
3457 if not Unique_Compile then
3458 for J in
3459 ALIs.Table (ALI).First_Unit .. ALIs.Table (ALI).Last_Unit
3460 loop
3461 for K in
3462 Units.Table (J).First_With .. Units.Table (J).Last_With
3463 loop
3464 Sfile := Withs.Table (K).Sfile;
3465 Uname := Withs.Table (K).Uname;
3467 -- If project files are used, find the proper source
3468 -- to compile, in case Sfile is the spec, but there
3469 -- is a body.
3471 if Main_Project /= No_Project then
3472 declare
3473 Unit_Name : Name_Id;
3474 Uid : Prj.Unit_Index;
3476 begin
3477 Get_Name_String (Uname);
3478 Name_Len := Name_Len - 2;
3479 Unit_Name := Name_Find;
3480 Uid :=
3481 Units_Htable.Get
3482 (Project_Tree.Units_HT, Unit_Name);
3484 if Uid /= Prj.No_Unit_Index then
3485 if Uid.File_Names (Impl) /= null
3486 and then
3487 not Uid.File_Names (Impl).Locally_Removed
3488 then
3489 Sfile := Uid.File_Names (Impl).File;
3490 Source_Index :=
3491 Uid.File_Names (Impl).Index;
3493 elsif Uid.File_Names (Spec) /= null
3494 and then
3495 not Uid.File_Names (Spec).Locally_Removed
3496 then
3497 Sfile := Uid.File_Names (Spec).File;
3498 Source_Index :=
3499 Uid.File_Names (Spec).Index;
3500 end if;
3501 end if;
3502 end;
3503 end if;
3505 Dependencies.Append ((ALIs.Table (ALI).Sfile, Sfile));
3507 if Is_In_Obsoleted (Sfile) then
3508 Executable_Obsolete := True;
3509 end if;
3511 if Sfile = No_File then
3512 Debug_Msg
3513 ("Skipping generic:", Withs.Table (K).Uname);
3515 else
3516 Source_Index :=
3517 Unit_Index_Of (Withs.Table (K).Afile);
3519 if Is_Marked (Sfile, Source_Index) then
3520 Debug_Msg ("Skipping marked file:", Sfile);
3522 elsif not Check_Readonly_Files
3523 and then Is_Internal_File_Name (Sfile, False)
3524 then
3525 Debug_Msg ("Skipping internal file:", Sfile);
3527 else
3528 Insert_Q
3529 (Sfile, Withs.Table (K).Uname, Source_Index);
3530 Mark (Sfile, Source_Index);
3531 end if;
3532 end if;
3533 end loop;
3534 end loop;
3535 end if;
3536 end;
3537 end loop;
3539 if Display_Compilation_Progress then
3540 Write_Str ("completed ");
3541 Write_Int (Int (Q_Front));
3542 Write_Str (" out of ");
3543 Write_Int (Int (Q.Last));
3544 Write_Str (" (");
3545 Write_Int (Int ((Q_Front * 100) / (Q.Last - Q.First)));
3546 Write_Str ("%)...");
3547 Write_Eol;
3548 end if;
3549 end loop Make_Loop;
3551 Compilation_Failures := Bad_Compilation_Count;
3553 -- Compilation is finished
3555 -- Delete any temporary configuration pragma file
3557 if not Debug.Debug_Flag_N then
3558 Delete_Temp_Config_Files;
3559 end if;
3560 end Compile_Sources;
3562 ----------------------------------
3563 -- Configuration_Pragmas_Switch --
3564 ----------------------------------
3566 function Configuration_Pragmas_Switch
3567 (For_Project : Project_Id) return Argument_List
3569 The_Packages : Package_Id;
3570 Gnatmake : Package_Id;
3571 Compiler : Package_Id;
3573 Global_Attribute : Variable_Value := Nil_Variable_Value;
3574 Local_Attribute : Variable_Value := Nil_Variable_Value;
3576 Global_Attribute_Present : Boolean := False;
3577 Local_Attribute_Present : Boolean := False;
3579 Result : Argument_List (1 .. 3);
3580 Last : Natural := 0;
3582 function Absolute_Path
3583 (Path : Path_Name_Type;
3584 Project : Project_Id) return String;
3585 -- Returns an absolute path for a configuration pragmas file
3587 -------------------
3588 -- Absolute_Path --
3589 -------------------
3591 function Absolute_Path
3592 (Path : Path_Name_Type;
3593 Project : Project_Id) return String
3595 begin
3596 Get_Name_String (Path);
3598 declare
3599 Path_Name : constant String := Name_Buffer (1 .. Name_Len);
3601 begin
3602 if Is_Absolute_Path (Path_Name) then
3603 return Path_Name;
3605 else
3606 declare
3607 Parent_Directory : constant String :=
3608 Get_Name_String (Project.Directory.Display_Name);
3610 begin
3611 if Parent_Directory (Parent_Directory'Last) =
3612 Directory_Separator
3613 then
3614 return Parent_Directory & Path_Name;
3616 else
3617 return Parent_Directory & Directory_Separator & Path_Name;
3618 end if;
3619 end;
3620 end if;
3621 end;
3622 end Absolute_Path;
3624 -- Start of processing for Configuration_Pragmas_Switch
3626 begin
3627 Prj.Env.Create_Config_Pragmas_File
3628 (For_Project, Project_Tree);
3630 if For_Project.Config_File_Name /= No_Path then
3631 Temporary_Config_File := For_Project.Config_File_Temp;
3632 Last := 1;
3633 Result (1) :=
3634 new String'
3635 ("-gnatec=" & Get_Name_String (For_Project.Config_File_Name));
3637 else
3638 Temporary_Config_File := False;
3639 end if;
3641 -- Check for attribute Builder'Global_Configuration_Pragmas
3643 The_Packages := Main_Project.Decl.Packages;
3644 Gnatmake :=
3645 Prj.Util.Value_Of
3646 (Name => Name_Builder,
3647 In_Packages => The_Packages,
3648 In_Tree => Project_Tree);
3650 if Gnatmake /= No_Package then
3651 Global_Attribute := Prj.Util.Value_Of
3652 (Variable_Name => Name_Global_Configuration_Pragmas,
3653 In_Variables => Project_Tree.Packages.Table
3654 (Gnatmake).Decl.Attributes,
3655 In_Tree => Project_Tree);
3656 Global_Attribute_Present :=
3657 Global_Attribute /= Nil_Variable_Value
3658 and then Get_Name_String (Global_Attribute.Value) /= "";
3660 if Global_Attribute_Present then
3661 declare
3662 Path : constant String :=
3663 Absolute_Path
3664 (Path_Name_Type (Global_Attribute.Value),
3665 Global_Attribute.Project);
3666 begin
3667 if not Is_Regular_File (Path) then
3668 if Debug.Debug_Flag_F then
3669 Make_Failed
3670 ("cannot find configuration pragmas file "
3671 & File_Name (Path));
3672 else
3673 Make_Failed
3674 ("cannot find configuration pragmas file " & Path);
3675 end if;
3676 end if;
3678 Last := Last + 1;
3679 Result (Last) := new String'("-gnatec=" & Path);
3680 end;
3681 end if;
3682 end if;
3684 -- Check for attribute Compiler'Local_Configuration_Pragmas
3686 The_Packages := For_Project.Decl.Packages;
3687 Compiler :=
3688 Prj.Util.Value_Of
3689 (Name => Name_Compiler,
3690 In_Packages => The_Packages,
3691 In_Tree => Project_Tree);
3693 if Compiler /= No_Package then
3694 Local_Attribute := Prj.Util.Value_Of
3695 (Variable_Name => Name_Local_Configuration_Pragmas,
3696 In_Variables => Project_Tree.Packages.Table
3697 (Compiler).Decl.Attributes,
3698 In_Tree => Project_Tree);
3699 Local_Attribute_Present :=
3700 Local_Attribute /= Nil_Variable_Value
3701 and then Get_Name_String (Local_Attribute.Value) /= "";
3703 if Local_Attribute_Present then
3704 declare
3705 Path : constant String :=
3706 Absolute_Path
3707 (Path_Name_Type (Local_Attribute.Value),
3708 Local_Attribute.Project);
3709 begin
3710 if not Is_Regular_File (Path) then
3711 if Debug.Debug_Flag_F then
3712 Make_Failed
3713 ("cannot find configuration pragmas file "
3714 & File_Name (Path));
3716 else
3717 Make_Failed
3718 ("cannot find configuration pragmas file " & Path);
3719 end if;
3720 end if;
3722 Last := Last + 1;
3723 Result (Last) := new String'("-gnatec=" & Path);
3724 end;
3725 end if;
3726 end if;
3728 return Result (1 .. Last);
3729 end Configuration_Pragmas_Switch;
3731 ---------------
3732 -- Debug_Msg --
3733 ---------------
3735 procedure Debug_Msg (S : String; N : Name_Id) is
3736 begin
3737 if Debug.Debug_Flag_W then
3738 Write_Str (" ... ");
3739 Write_Str (S);
3740 Write_Str (" ");
3741 Write_Name (N);
3742 Write_Eol;
3743 end if;
3744 end Debug_Msg;
3746 procedure Debug_Msg (S : String; N : File_Name_Type) is
3747 begin
3748 Debug_Msg (S, Name_Id (N));
3749 end Debug_Msg;
3751 procedure Debug_Msg (S : String; N : Unit_Name_Type) is
3752 begin
3753 Debug_Msg (S, Name_Id (N));
3754 end Debug_Msg;
3756 ---------------------------
3757 -- Delete_All_Temp_Files --
3758 ---------------------------
3760 procedure Delete_All_Temp_Files is
3761 begin
3762 if not Debug.Debug_Flag_N then
3763 Delete_Temp_Config_Files;
3764 Prj.Delete_All_Temp_Files (Project_Tree);
3765 end if;
3766 end Delete_All_Temp_Files;
3768 ------------------------------
3769 -- Delete_Temp_Config_Files --
3770 ------------------------------
3772 procedure Delete_Temp_Config_Files is
3773 Success : Boolean;
3774 Proj : Project_List;
3775 pragma Warnings (Off, Success);
3777 begin
3778 -- The caller is responsible for ensuring that Debug_Flag_N is False
3780 pragma Assert (not Debug.Debug_Flag_N);
3782 if Main_Project /= No_Project then
3783 Proj := Project_Tree.Projects;
3784 while Proj /= null loop
3785 if Proj.Project.Config_File_Temp then
3786 Delete_Temporary_File
3787 (Project_Tree, Proj.Project.Config_File_Name);
3789 -- Make sure that we don't have a config file for this project,
3790 -- in case there are several mains. In this case, we will
3791 -- recreate another config file: we cannot reuse the one that
3792 -- we just deleted!
3794 Proj.Project.Config_Checked := False;
3795 Proj.Project.Config_File_Name := No_Path;
3796 Proj.Project.Config_File_Temp := False;
3797 end if;
3798 Proj := Proj.Next;
3799 end loop;
3800 end if;
3801 end Delete_Temp_Config_Files;
3803 -------------
3804 -- Display --
3805 -------------
3807 procedure Display (Program : String; Args : Argument_List) is
3808 begin
3809 pragma Assert (Args'First = 1);
3811 if Display_Executed_Programs then
3812 Write_Str (Program);
3814 for J in Args'Range loop
3816 -- Never display -gnatea nor -gnatez
3818 if Args (J).all /= "-gnatea"
3819 and then
3820 Args (J).all /= "-gnatez"
3821 then
3822 -- Do not display the mapping file argument automatically
3823 -- created when using a project file.
3825 if Main_Project = No_Project
3826 or else Debug.Debug_Flag_N
3827 or else Args (J)'Length < 8
3828 or else
3829 Args (J) (Args (J)'First .. Args (J)'First + 6) /= "-gnatem"
3830 then
3831 -- When -dn is not specified, do not display the config
3832 -- pragmas switch (-gnatec) for the temporary file created
3833 -- by the project manager (always the first -gnatec switch).
3834 -- Reset Temporary_Config_File to False so that the eventual
3835 -- other -gnatec switches will be displayed.
3837 if (not Debug.Debug_Flag_N)
3838 and then Temporary_Config_File
3839 and then Args (J)'Length > 7
3840 and then Args (J) (Args (J)'First .. Args (J)'First + 6)
3841 = "-gnatec"
3842 then
3843 Temporary_Config_File := False;
3845 -- Do not display the -F=mapping_file switch for
3846 -- gnatbind, if -dn is not specified.
3848 elsif Debug.Debug_Flag_N
3849 or else Args (J)'Length < 4
3850 or else
3851 Args (J) (Args (J)'First .. Args (J)'First + 2) /= "-F="
3852 then
3853 Write_Str (" ");
3855 -- If -df is used, only display file names, not path
3856 -- names.
3858 if Debug.Debug_Flag_F then
3859 declare
3860 Equal_Pos : Natural;
3861 begin
3862 Equal_Pos := Args (J)'First - 1;
3863 for K in Args (J)'Range loop
3864 if Args (J) (K) = '=' then
3865 Equal_Pos := K;
3866 exit;
3867 end if;
3868 end loop;
3870 if Is_Absolute_Path
3871 (Args (J) (Equal_Pos + 1 .. Args (J)'Last))
3872 then
3873 Write_Str
3874 (Args (J) (Args (J)'First .. Equal_Pos));
3875 Write_Str
3876 (File_Name
3877 (Args (J)
3878 (Equal_Pos + 1 .. Args (J)'Last)));
3880 else
3881 Write_Str (Args (J).all);
3882 end if;
3883 end;
3885 else
3886 Write_Str (Args (J).all);
3887 end if;
3888 end if;
3889 end if;
3890 end if;
3891 end loop;
3893 Write_Eol;
3894 end if;
3895 end Display;
3897 ----------------------
3898 -- Display_Commands --
3899 ----------------------
3901 procedure Display_Commands (Display : Boolean := True) is
3902 begin
3903 Display_Executed_Programs := Display;
3904 end Display_Commands;
3906 -------------
3907 -- Empty_Q --
3908 -------------
3910 function Empty_Q return Boolean is
3911 begin
3912 if Debug.Debug_Flag_P then
3913 Write_Str (" Q := [");
3915 for J in Q_Front .. Q.Last - 1 loop
3916 Write_Str (" ");
3917 Write_Name (Q.Table (J).File);
3918 Write_Eol;
3919 Write_Str (" ");
3920 end loop;
3922 Write_Str ("]");
3923 Write_Eol;
3924 end if;
3926 return Q_Front >= Q.Last;
3927 end Empty_Q;
3929 --------------------------
3930 -- Enter_Into_Obsoleted --
3931 --------------------------
3933 procedure Enter_Into_Obsoleted (F : File_Name_Type) is
3934 Name : constant String := Get_Name_String (F);
3935 First : Natural;
3936 F2 : File_Name_Type;
3938 begin
3939 First := Name'Last;
3940 while First > Name'First
3941 and then Name (First - 1) /= Directory_Separator
3942 and then Name (First - 1) /= '/'
3943 loop
3944 First := First - 1;
3945 end loop;
3947 if First /= Name'First then
3948 Name_Len := 0;
3949 Add_Str_To_Name_Buffer (Name (First .. Name'Last));
3950 F2 := Name_Find;
3952 else
3953 F2 := F;
3954 end if;
3956 Debug_Msg ("New entry in Obsoleted table:", F2);
3957 Obsoleted.Set (F2, True);
3958 end Enter_Into_Obsoleted;
3960 --------------------
3961 -- Extract_From_Q --
3962 --------------------
3964 procedure Extract_From_Q
3965 (Source_File : out File_Name_Type;
3966 Source_Unit : out Unit_Name_Type;
3967 Source_Index : out Int)
3969 File : constant File_Name_Type := Q.Table (Q_Front).File;
3970 Unit : constant Unit_Name_Type := Q.Table (Q_Front).Unit;
3971 Index : constant Int := Q.Table (Q_Front).Index;
3973 begin
3974 if Debug.Debug_Flag_Q then
3975 Write_Str (" Q := Q - [ ");
3976 Write_Name (File);
3978 if Index /= 0 then
3979 Write_Str (", ");
3980 Write_Int (Index);
3981 end if;
3983 Write_Str (" ]");
3984 Write_Eol;
3985 end if;
3987 Q_Front := Q_Front + 1;
3988 Source_File := File;
3989 Source_Unit := Unit;
3990 Source_Index := Index;
3991 end Extract_From_Q;
3993 --------------
3994 -- Gnatmake --
3995 --------------
3997 procedure Gnatmake is
3998 Main_Source_File : File_Name_Type;
3999 -- The source file containing the main compilation unit
4001 Compilation_Failures : Natural;
4003 Total_Compilation_Failures : Natural := 0;
4005 Is_Main_Unit : Boolean;
4006 -- Set to True by Compile_Sources if the Main_Source_File can be a
4007 -- main unit.
4009 Main_ALI_File : File_Name_Type;
4010 -- The ali file corresponding to Main_Source_File
4012 Executable : File_Name_Type := No_File;
4013 -- The file name of an executable
4015 Non_Std_Executable : Boolean := False;
4016 -- Non_Std_Executable is set to True when there is a possibility
4017 -- that the linker will not choose the correct executable file name.
4019 Current_Work_Dir : constant String_Access :=
4020 new String'(Get_Current_Dir);
4021 -- The current working directory, used to modify some relative path
4022 -- switches on the command line when a project file is used.
4024 Current_Main_Index : Int := 0;
4025 -- If not zero, the index of the current main unit in its source file
4027 Stand_Alone_Libraries : Boolean := False;
4028 -- Set to True when there are Stand-Alone Libraries, so that gnatbind
4029 -- is invoked with the -F switch to force checking of elaboration flags.
4031 Mapping_Path : Path_Name_Type := No_Path;
4032 -- The path name of the mapping file
4034 Project_Node_Tree : Project_Node_Tree_Ref;
4036 Discard : Boolean;
4037 pragma Warnings (Off, Discard);
4039 procedure Check_Mains;
4040 -- Check that the main subprograms do exist and that they all
4041 -- belong to the same project file.
4043 procedure Create_Binder_Mapping_File
4044 (Args : in out Argument_List; Last_Arg : in out Natural);
4045 -- Create a binder mapping file and add the necessary switch
4047 -----------------
4048 -- Check_Mains --
4049 -----------------
4051 procedure Check_Mains is
4052 Real_Main_Project : Project_Id := No_Project;
4053 -- The project of the first main
4055 Proj : Project_Id := No_Project;
4056 -- The project of the current main
4058 Real_Path : String_Access;
4060 begin
4061 Mains.Reset;
4063 -- Check each main
4065 loop
4066 declare
4067 Main : constant String := Mains.Next_Main;
4068 -- The name specified on the command line may include
4069 -- directory information.
4071 File_Name : constant String := Base_Name (Main);
4072 -- The simple file name of the current main
4074 Lang : Language_Ptr;
4076 begin
4077 exit when Main = "";
4079 -- Get the project of the current main
4081 Proj := Prj.Env.Project_Of
4082 (File_Name, Main_Project, Project_Tree);
4084 -- Fail if the current main is not a source of a
4085 -- project.
4087 if Proj = No_Project then
4088 Make_Failed
4089 ("""" & Main & """ is not a source of any project");
4091 else
4092 -- If there is directory information, check that
4093 -- the source exists and, if it does, that the path
4094 -- is the actual path of a source of a project.
4096 if Main /= File_Name then
4097 Lang := Get_Language_From_Name (Main_Project, "ada");
4099 Real_Path :=
4100 Locate_Regular_File
4101 (Main & Get_Name_String
4102 (Lang.Config.Naming_Data.Body_Suffix),
4103 "");
4104 if Real_Path = null then
4105 Real_Path :=
4106 Locate_Regular_File
4107 (Main & Get_Name_String
4108 (Lang.Config.Naming_Data.Spec_Suffix),
4109 "");
4110 end if;
4112 if Real_Path = null then
4113 Real_Path := Locate_Regular_File (Main, "");
4114 end if;
4116 -- Fail if the file cannot be found
4118 if Real_Path = null then
4119 Make_Failed ("file """ & Main & """ does not exist");
4120 end if;
4122 declare
4123 Project_Path : constant String :=
4124 Prj.Env.File_Name_Of_Library_Unit_Body
4125 (Name => File_Name,
4126 Project => Main_Project,
4127 In_Tree => Project_Tree,
4128 Main_Project_Only => False,
4129 Full_Path => True);
4130 Normed_Path : constant String :=
4131 Normalize_Pathname
4132 (Real_Path.all,
4133 Case_Sensitive => False);
4134 Proj_Path : constant String :=
4135 Normalize_Pathname
4136 (Project_Path,
4137 Case_Sensitive => False);
4139 begin
4140 Free (Real_Path);
4142 -- Fail if it is not the correct path
4144 if Normed_Path /= Proj_Path then
4145 if Verbose_Mode then
4146 Set_Standard_Error;
4147 Write_Str (Normed_Path);
4148 Write_Str (" /= ");
4149 Write_Line (Proj_Path);
4150 end if;
4152 Make_Failed
4153 ("""" & Main &
4154 """ is not a source of any project");
4155 end if;
4156 end;
4157 end if;
4159 if not Unique_Compile then
4161 -- Record the project, if it is the first main
4163 if Real_Main_Project = No_Project then
4164 Real_Main_Project := Proj;
4166 elsif Proj /= Real_Main_Project then
4168 -- Fail, as the current main is not a source
4169 -- of the same project as the first main.
4171 Make_Failed
4172 ("""" & Main &
4173 """ is not a source of project " &
4174 Get_Name_String (Real_Main_Project.Name));
4175 end if;
4176 end if;
4177 end if;
4179 -- If -u and -U are not used, we may have mains that
4180 -- are sources of a project that is not the one
4181 -- specified with switch -P.
4183 if not Unique_Compile then
4184 Main_Project := Real_Main_Project;
4185 end if;
4186 end;
4187 end loop;
4188 end Check_Mains;
4190 --------------------------------
4191 -- Create_Binder_Mapping_File --
4192 --------------------------------
4194 procedure Create_Binder_Mapping_File
4195 (Args : in out Argument_List; Last_Arg : in out Natural)
4197 Mapping_FD : File_Descriptor := Invalid_FD;
4198 -- A File Descriptor for an eventual mapping file
4200 ALI_Unit : Unit_Name_Type := No_Unit_Name;
4201 -- The unit name of an ALI file
4203 ALI_Name : File_Name_Type := No_File;
4204 -- The file name of the ALI file
4206 ALI_Project : Project_Id := No_Project;
4207 -- The project of the ALI file
4209 Bytes : Integer;
4210 OK : Boolean := True;
4211 Unit : Unit_Index;
4213 Status : Boolean;
4214 -- For call to Close
4216 begin
4217 Tempdir.Create_Temp_File (Mapping_FD, Mapping_Path);
4218 Record_Temp_File (Project_Tree, Mapping_Path);
4220 if Mapping_FD /= Invalid_FD then
4222 -- Traverse all units
4224 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
4226 while Unit /= No_Unit_Index loop
4227 if Unit.Name /= No_Name then
4229 -- If there is a body, put it in the mapping
4231 if Unit.File_Names (Impl) /= No_Source
4232 and then Unit.File_Names (Impl).Project /=
4233 No_Project
4234 then
4235 Get_Name_String (Unit.Name);
4236 Add_Str_To_Name_Buffer ("%b");
4237 ALI_Unit := Name_Find;
4238 ALI_Name :=
4239 Lib_File_Name
4240 (Unit.File_Names (Impl).Display_File);
4241 ALI_Project := Unit.File_Names (Impl).Project;
4243 -- Otherwise, if there is a spec, put it in the
4244 -- mapping.
4246 elsif Unit.File_Names (Spec) /= No_Source
4247 and then Unit.File_Names (Spec).Project /=
4248 No_Project
4249 then
4250 Get_Name_String (Unit.Name);
4251 Add_Str_To_Name_Buffer ("%s");
4252 ALI_Unit := Name_Find;
4253 ALI_Name :=
4254 Lib_File_Name
4255 (Unit.File_Names (Spec).Display_File);
4256 ALI_Project := Unit.File_Names (Spec).Project;
4258 else
4259 ALI_Name := No_File;
4260 end if;
4262 -- If we have something to put in the mapping then do it
4263 -- now. However, if the project is extended, we don't put
4264 -- anything in the mapping file, because we don't know where
4265 -- the ALI file is: it might be in the extended project obj
4266 -- dir as well as in the extending project obj dir.
4268 if ALI_Name /= No_File
4269 and then ALI_Project.Extended_By = No_Project
4270 and then ALI_Project.Extends = No_Project
4271 then
4272 -- First check if the ALI file exists. If it does not,
4273 -- do not put the unit in the mapping file.
4275 declare
4276 ALI : constant String := Get_Name_String (ALI_Name);
4278 begin
4279 -- For library projects, use the library directory,
4280 -- for other projects, use the object directory.
4282 if ALI_Project.Library then
4283 Get_Name_String (ALI_Project.Library_Dir.Name);
4284 else
4285 Get_Name_String
4286 (ALI_Project.Object_Directory.Name);
4287 end if;
4289 if not
4290 Is_Directory_Separator (Name_Buffer (Name_Len))
4291 then
4292 Add_Char_To_Name_Buffer (Directory_Separator);
4293 end if;
4295 Add_Str_To_Name_Buffer (ALI);
4296 Add_Char_To_Name_Buffer (ASCII.LF);
4298 declare
4299 ALI_Path_Name : constant String :=
4300 Name_Buffer (1 .. Name_Len);
4302 begin
4303 if Is_Regular_File
4304 (ALI_Path_Name (1 .. ALI_Path_Name'Last - 1))
4305 then
4306 -- First line is the unit name
4308 Get_Name_String (ALI_Unit);
4309 Add_Char_To_Name_Buffer (ASCII.LF);
4310 Bytes :=
4311 Write
4312 (Mapping_FD,
4313 Name_Buffer (1)'Address,
4314 Name_Len);
4315 OK := Bytes = Name_Len;
4317 exit when not OK;
4319 -- Second line it the ALI file name
4321 Get_Name_String (ALI_Name);
4322 Add_Char_To_Name_Buffer (ASCII.LF);
4323 Bytes :=
4324 Write
4325 (Mapping_FD,
4326 Name_Buffer (1)'Address,
4327 Name_Len);
4328 OK := (Bytes = Name_Len);
4330 exit when not OK;
4332 -- Third line it the ALI path name
4334 Bytes :=
4335 Write
4336 (Mapping_FD,
4337 ALI_Path_Name (1)'Address,
4338 ALI_Path_Name'Length);
4339 OK := (Bytes = ALI_Path_Name'Length);
4341 -- If OK is False, it means we were unable to
4342 -- write a line. No point in continuing with the
4343 -- other units.
4345 exit when not OK;
4346 end if;
4347 end;
4348 end;
4349 end if;
4350 end if;
4352 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
4353 end loop;
4355 Close (Mapping_FD, Status);
4357 OK := OK and Status;
4359 -- If the creation of the mapping file was successful,
4360 -- we add the switch to the arguments of gnatbind.
4362 if OK then
4363 Last_Arg := Last_Arg + 1;
4364 Args (Last_Arg) :=
4365 new String'("-F=" & Get_Name_String (Mapping_Path));
4366 end if;
4367 end if;
4368 end Create_Binder_Mapping_File;
4370 -- Start of processing for Gnatmake
4372 -- This body is very long, should be broken down ???
4374 begin
4375 Install_Int_Handler (Sigint_Intercepted'Access);
4377 Do_Compile_Step := True;
4378 Do_Bind_Step := True;
4379 Do_Link_Step := True;
4381 Obsoleted.Reset;
4383 Make.Initialize (Project_Node_Tree);
4385 Bind_Shared := No_Shared_Switch'Access;
4386 Link_With_Shared_Libgcc := No_Shared_Libgcc_Switch'Access;
4388 Failed_Links.Set_Last (0);
4389 Successful_Links.Set_Last (0);
4391 -- Special case when switch -B was specified
4393 if Build_Bind_And_Link_Full_Project then
4395 -- When switch -B is specified, there must be a project file
4397 if Main_Project = No_Project then
4398 Make_Failed ("-B cannot be used without a project file");
4400 -- No main program may be specified on the command line
4402 elsif Osint.Number_Of_Files /= 0 then
4403 Make_Failed ("-B cannot be used with a main specified on " &
4404 "the command line");
4406 -- And the project file cannot be a library project file
4408 elsif Main_Project.Library then
4409 Make_Failed ("-B cannot be used for a library project file");
4411 else
4412 No_Main_Subprogram := True;
4413 Insert_Project_Sources
4414 (The_Project => Main_Project,
4415 All_Projects => Unique_Compile_All_Projects,
4416 Into_Q => False);
4418 -- If there are no sources to compile, we fail
4420 if Osint.Number_Of_Files = 0 then
4421 Make_Failed ("no sources to compile");
4422 end if;
4424 -- Specify -n for gnatbind and add the ALI files of all the
4425 -- sources, except the one which is a fake main subprogram:
4426 -- this is the one for the binder generated file and it will be
4427 -- transmitted to gnatlink. These sources are those that are
4428 -- in the queue.
4430 Add_Switch ("-n", Binder, And_Save => True);
4432 for J in Q.First .. Q.Last - 1 loop
4433 Add_Switch
4434 (Get_Name_String
4435 (Lib_File_Name (Q.Table (J).File)),
4436 Binder, And_Save => True);
4437 end loop;
4438 end if;
4440 elsif Main_Index /= 0 and then Osint.Number_Of_Files > 1 then
4441 Make_Failed ("cannot specify several mains with a multi-unit index");
4443 elsif Main_Project /= No_Project then
4445 -- If the main project file is a library project file, main(s)
4446 -- cannot be specified on the command line.
4448 if Osint.Number_Of_Files /= 0 then
4449 if Main_Project.Library
4450 and then not Unique_Compile
4451 and then ((not Make_Steps) or else Bind_Only or else Link_Only)
4452 then
4453 Make_Failed ("cannot specify a main program " &
4454 "on the command line for a library project file");
4456 else
4457 -- Check that each main on the command line is a source of a
4458 -- project file and, if there are several mains, each of them
4459 -- is a source of the same project file.
4461 Check_Mains;
4462 end if;
4464 -- If no mains have been specified on the command line,
4465 -- and we are using a project file, we either find the main(s)
4466 -- in the attribute Main of the main project, or we put all
4467 -- the sources of the project file as mains.
4469 else
4470 if Main_Index /= 0 then
4471 Make_Failed ("cannot specify a multi-unit index but no main " &
4472 "on the command line");
4473 end if;
4475 declare
4476 Value : String_List_Id := Main_Project.Mains;
4478 begin
4479 -- The attribute Main is an empty list or not specified,
4480 -- or else gnatmake was invoked with the switch "-u".
4482 if Value = Prj.Nil_String or else Unique_Compile then
4484 if (not Make_Steps) or else Compile_Only
4485 or else not Main_Project.Library
4486 then
4487 -- First make sure that the binder and the linker
4488 -- will not be invoked.
4490 Do_Bind_Step := False;
4491 Do_Link_Step := False;
4493 -- Put all the sources in the queue
4495 No_Main_Subprogram := True;
4496 Insert_Project_Sources
4497 (The_Project => Main_Project,
4498 All_Projects => Unique_Compile_All_Projects,
4499 Into_Q => False);
4501 -- If no sources to compile, then there is nothing to do
4503 if Osint.Number_Of_Files = 0 then
4504 if not Quiet_Output then
4505 Osint.Write_Program_Name;
4506 Write_Line (": no sources to compile");
4507 end if;
4509 Delete_All_Temp_Files;
4510 Exit_Program (E_Success);
4511 end if;
4512 end if;
4514 else
4515 -- The attribute Main is not an empty list.
4516 -- Put all the main subprograms in the list as if there
4517 -- were specified on the command line. However, if attribute
4518 -- Languages includes a language other than Ada, only
4519 -- include the Ada mains; if there is no Ada main, compile
4520 -- all the sources of the project.
4522 declare
4523 Languages : constant Variable_Value :=
4524 Prj.Util.Value_Of
4525 (Name_Languages,
4526 Main_Project.Decl.Attributes,
4527 Project_Tree);
4529 Current : String_List_Id;
4530 Element : String_Element;
4532 Foreign_Language : Boolean := False;
4533 At_Least_One_Main : Boolean := False;
4535 begin
4536 -- First, determine if there is a foreign language in
4537 -- attribute Languages.
4539 if not Languages.Default then
4540 Current := Languages.Values;
4542 Look_For_Foreign :
4543 while Current /= Nil_String loop
4544 Element := Project_Tree.String_Elements.
4545 Table (Current);
4546 Get_Name_String (Element.Value);
4547 To_Lower (Name_Buffer (1 .. Name_Len));
4549 if Name_Buffer (1 .. Name_Len) /= "ada" then
4550 Foreign_Language := True;
4551 exit Look_For_Foreign;
4552 end if;
4554 Current := Element.Next;
4555 end loop Look_For_Foreign;
4556 end if;
4558 -- Then, find all mains, or if there is a foreign
4559 -- language, all the Ada mains.
4561 while Value /= Prj.Nil_String loop
4562 Get_Name_String
4563 (Project_Tree.String_Elements.Table (Value).Value);
4565 -- To know if a main is an Ada main, get its project.
4566 -- It should be the project specified on the command
4567 -- line.
4569 if (not Foreign_Language) or else
4570 Prj.Env.Project_Of
4571 (Name_Buffer (1 .. Name_Len),
4572 Main_Project,
4573 Project_Tree) =
4574 Main_Project
4575 then
4576 At_Least_One_Main := True;
4577 Osint.Add_File
4578 (Get_Name_String
4579 (Project_Tree.String_Elements.Table
4580 (Value).Value),
4581 Index =>
4582 Project_Tree.String_Elements.Table
4583 (Value).Index);
4584 end if;
4586 Value := Project_Tree.String_Elements.Table
4587 (Value).Next;
4588 end loop;
4590 -- If we did not get any main, it means that all mains
4591 -- in attribute Mains are in a foreign language and -B
4592 -- was not specified to gnatmake; so, we fail.
4594 if not At_Least_One_Main then
4595 Make_Failed
4596 ("no Ada mains, use -B to build foreign main");
4597 end if;
4598 end;
4600 end if;
4601 end;
4602 end if;
4603 end if;
4605 if Verbose_Mode then
4606 Write_Eol;
4607 Display_Version ("GNATMAKE", "1995");
4608 end if;
4610 if Main_Project /= No_Project
4611 and then Main_Project.Externally_Built
4612 then
4613 Make_Failed
4614 ("nothing to do for a main project that is externally built");
4615 end if;
4617 if Osint.Number_Of_Files = 0 then
4618 if Main_Project /= No_Project
4619 and then Main_Project.Library
4620 then
4621 if Do_Bind_Step
4622 and then not Main_Project.Standalone_Library
4623 then
4624 Make_Failed ("only stand-alone libraries may be bound");
4625 end if;
4627 -- Add the default search directories to be able to find libgnat
4629 Osint.Add_Default_Search_Dirs;
4631 -- Get the target parameters, so that the correct binder generated
4632 -- files are generated if OpenVMS is the target.
4634 begin
4635 Targparm.Get_Target_Parameters;
4637 exception
4638 when Unrecoverable_Error =>
4639 Make_Failed ("*** make failed.");
4640 end;
4642 -- And bind and or link the library
4644 MLib.Prj.Build_Library
4645 (For_Project => Main_Project,
4646 In_Tree => Project_Tree,
4647 Gnatbind => Gnatbind.all,
4648 Gnatbind_Path => Gnatbind_Path,
4649 Gcc => Gcc.all,
4650 Gcc_Path => Gcc_Path,
4651 Bind => Bind_Only,
4652 Link => Link_Only);
4654 Delete_All_Temp_Files;
4655 Exit_Program (E_Success);
4657 else
4658 -- Call Get_Target_Parameters to ensure that VM_Target and
4659 -- AAMP_On_Target get set before calling Usage.
4661 Targparm.Get_Target_Parameters;
4663 -- Output usage information if no files to compile
4665 Usage;
4666 Exit_Program (E_Fatal);
4667 end if;
4668 end if;
4670 -- If -M was specified, behave as if -n was specified
4672 if List_Dependencies then
4673 Do_Not_Execute := True;
4674 end if;
4676 -- Note that Osint.M.Next_Main_Source will always return the (possibly
4677 -- abbreviated file) without any directory information.
4679 Main_Source_File := Next_Main_Source;
4681 if Current_File_Index /= No_Index then
4682 Main_Index := Current_File_Index;
4683 end if;
4685 Add_Switch ("-I-", Compiler, And_Save => True);
4687 if Main_Project = No_Project then
4688 if Look_In_Primary_Dir then
4690 Add_Switch
4691 ("-I" &
4692 Normalize_Directory_Name
4693 (Get_Primary_Src_Search_Directory.all).all,
4694 Compiler, Append_Switch => False,
4695 And_Save => False);
4697 end if;
4699 else
4700 -- If we use a project file, we have already checked that a main
4701 -- specified on the command line with directory information has the
4702 -- path name corresponding to a correct source in the project tree.
4703 -- So, we don't need the directory information to be taken into
4704 -- account by Find_File, and in fact it may lead to take the wrong
4705 -- sources for other compilation units, when there are extending
4706 -- projects.
4708 Look_In_Primary_Dir := False;
4709 Add_Switch ("-I-", Binder, And_Save => True);
4710 end if;
4712 -- If the user wants a program without a main subprogram, add the
4713 -- appropriate switch to the binder.
4715 if No_Main_Subprogram then
4716 Add_Switch ("-z", Binder, And_Save => True);
4717 end if;
4719 if Main_Project /= No_Project then
4721 if Main_Project.Object_Directory /= No_Path_Information then
4722 -- Change current directory to object directory of main project
4724 Project_Of_Current_Object_Directory := No_Project;
4725 Change_To_Object_Directory (Main_Project);
4726 end if;
4728 -- Source file lookups should be cached for efficiency.
4729 -- Source files are not supposed to change.
4731 Osint.Source_File_Data (Cache => True);
4733 -- Find the file name of the (first) main unit
4735 declare
4736 Main_Source_File_Name : constant String :=
4737 Get_Name_String (Main_Source_File);
4738 Main_Unit_File_Name : constant String :=
4739 Prj.Env.File_Name_Of_Library_Unit_Body
4740 (Name => Main_Source_File_Name,
4741 Project => Main_Project,
4742 In_Tree => Project_Tree,
4743 Main_Project_Only =>
4744 not Unique_Compile);
4746 The_Packages : constant Package_Id :=
4747 Main_Project.Decl.Packages;
4749 Builder_Package : constant Prj.Package_Id :=
4750 Prj.Util.Value_Of
4751 (Name => Name_Builder,
4752 In_Packages => The_Packages,
4753 In_Tree => Project_Tree);
4755 Binder_Package : constant Prj.Package_Id :=
4756 Prj.Util.Value_Of
4757 (Name => Name_Binder,
4758 In_Packages => The_Packages,
4759 In_Tree => Project_Tree);
4761 Linker_Package : constant Prj.Package_Id :=
4762 Prj.Util.Value_Of
4763 (Name => Name_Linker,
4764 In_Packages => The_Packages,
4765 In_Tree => Project_Tree);
4767 Default_Switches_Array : Array_Id;
4769 Global_Compilation_Array : Array_Element_Id;
4770 Global_Compilation_Elem : Array_Element;
4771 Global_Compilation_Switches : Variable_Value;
4773 begin
4774 -- We fail if we cannot find the main source file
4776 if Main_Unit_File_Name = "" then
4777 Make_Failed ('"' & Main_Source_File_Name
4778 & """ is not a unit of project "
4779 & Project_File_Name.all & ".");
4780 else
4781 -- Remove any directory information from the main
4782 -- source file name.
4784 declare
4785 Pos : Natural := Main_Unit_File_Name'Last;
4787 begin
4788 loop
4789 exit when Pos < Main_Unit_File_Name'First or else
4790 Main_Unit_File_Name (Pos) = Directory_Separator;
4791 Pos := Pos - 1;
4792 end loop;
4794 Name_Len := Main_Unit_File_Name'Last - Pos;
4796 Name_Buffer (1 .. Name_Len) :=
4797 Main_Unit_File_Name
4798 (Pos + 1 .. Main_Unit_File_Name'Last);
4800 Main_Source_File := Name_Find;
4802 -- We only output the main source file if there is only one
4804 if Verbose_Mode and then Osint.Number_Of_Files = 1 then
4805 Write_Str ("Main source file: """);
4806 Write_Str (Main_Unit_File_Name
4807 (Pos + 1 .. Main_Unit_File_Name'Last));
4808 Write_Line (""".");
4809 end if;
4810 end;
4811 end if;
4813 -- If there is a package Builder in the main project file, add
4814 -- the switches from it.
4816 if Builder_Package /= No_Package then
4818 Global_Compilation_Array := Prj.Util.Value_Of
4819 (Name => Name_Global_Compilation_Switches,
4820 In_Arrays => Project_Tree.Packages.Table
4821 (Builder_Package).Decl.Arrays,
4822 In_Tree => Project_Tree);
4824 Default_Switches_Array :=
4825 Project_Tree.Packages.Table
4826 (Builder_Package).Decl.Arrays;
4828 while Default_Switches_Array /= No_Array and then
4829 Project_Tree.Arrays.Table (Default_Switches_Array).Name /=
4830 Name_Default_Switches
4831 loop
4832 Default_Switches_Array :=
4833 Project_Tree.Arrays.Table (Default_Switches_Array).Next;
4834 end loop;
4836 if Global_Compilation_Array /= No_Array_Element and then
4837 Default_Switches_Array /= No_Array
4838 then
4839 Errutil.Error_Msg
4840 ("Default_Switches forbidden in presence of " &
4841 "Global_Compilation_Switches. Use Switches instead.",
4842 Project_Tree.Arrays.Table
4843 (Default_Switches_Array).Location);
4844 Errutil.Finalize;
4845 Make_Failed
4846 ("*** illegal combination of Builder attributes");
4847 end if;
4849 -- If there is only one main, we attempt to get the gnatmake
4850 -- switches for this main (if any). If there are no specific
4851 -- switch for this particular main, get the general gnatmake
4852 -- switches (if any).
4854 if Osint.Number_Of_Files = 1 then
4855 if Verbose_Mode then
4856 Write_Str ("Adding gnatmake switches for """);
4857 Write_Str (Main_Unit_File_Name);
4858 Write_Line (""".");
4859 end if;
4861 Add_Switches
4862 (Project_Node_Tree => Project_Node_Tree,
4863 File_Name => Main_Unit_File_Name,
4864 Index => Main_Index,
4865 The_Package => Builder_Package,
4866 Program => None,
4867 Unknown_Switches_To_The_Compiler =>
4868 Global_Compilation_Array = No_Array_Element);
4870 else
4871 -- If there are several mains, we always get the general
4872 -- gnatmake switches (if any).
4874 -- Warn the user, if necessary, so that he is not surprised
4875 -- that specific switches are not taken into account.
4877 declare
4878 Defaults : constant Variable_Value :=
4879 Prj.Util.Value_Of
4880 (Name => Name_Ada,
4881 Index => 0,
4882 Attribute_Or_Array_Name =>
4883 Name_Default_Switches,
4884 In_Package =>
4885 Builder_Package,
4886 In_Tree => Project_Tree);
4888 Switches : constant Array_Element_Id :=
4889 Prj.Util.Value_Of
4890 (Name => Name_Switches,
4891 In_Arrays =>
4892 Project_Tree.Packages.Table
4893 (Builder_Package).Decl.Arrays,
4894 In_Tree => Project_Tree);
4896 Other_Switches : constant Variable_Value :=
4897 Prj.Util.Value_Of
4898 (Name => All_Other_Names,
4899 Index => 0,
4900 Attribute_Or_Array_Name
4901 => Name_Switches,
4902 In_Package => Builder_Package,
4903 In_Tree => Project_Tree);
4905 begin
4906 if Other_Switches /= Nil_Variable_Value then
4907 if not Quiet_Output
4908 and then Switches /= No_Array_Element
4909 and then Project_Tree.Array_Elements.Table
4910 (Switches).Next /= No_Array_Element
4911 then
4912 Write_Line
4913 ("Warning: using Builder'Switches(others), "
4914 & "as there are several mains");
4915 end if;
4917 Add_Switches
4918 (Project_Node_Tree => Project_Node_Tree,
4919 File_Name => " ",
4920 Index => 0,
4921 The_Package => Builder_Package,
4922 Program => None,
4923 Unknown_Switches_To_The_Compiler => False);
4925 elsif Defaults /= Nil_Variable_Value then
4926 if not Quiet_Output
4927 and then Switches /= No_Array_Element
4928 then
4929 Write_Line
4930 ("Warning: using Builder'Default_Switches"
4931 & "(""Ada""), as there are several mains");
4932 end if;
4934 Add_Switches
4935 (Project_Node_Tree => Project_Node_Tree,
4936 File_Name => " ",
4937 Index => 0,
4938 The_Package => Builder_Package,
4939 Program => None);
4941 elsif not Quiet_Output
4942 and then Switches /= No_Array_Element
4943 then
4944 Write_Line
4945 ("Warning: using no switches from package "
4946 & "Builder, as there are several mains");
4947 end if;
4948 end;
4949 end if;
4951 -- Take into account attribute Global_Compilation_Switches
4952 -- ("Ada").
4954 declare
4955 Index : Name_Id;
4956 List : String_List_Id;
4957 Elem : String_Element;
4959 begin
4960 while Global_Compilation_Array /= No_Array_Element loop
4961 Global_Compilation_Elem :=
4962 Project_Tree.Array_Elements.Table
4963 (Global_Compilation_Array);
4965 Get_Name_String (Global_Compilation_Elem.Index);
4966 To_Lower (Name_Buffer (1 .. Name_Len));
4967 Index := Name_Find;
4969 if Index = Name_Ada then
4970 Global_Compilation_Switches :=
4971 Global_Compilation_Elem.Value;
4973 if Global_Compilation_Switches /= Nil_Variable_Value
4974 and then not Global_Compilation_Switches.Default
4975 then
4976 -- We have found attribute
4977 -- Global_Compilation_Switches ("Ada"): put the
4978 -- switches in the appropriate table.
4980 List := Global_Compilation_Switches.Values;
4982 while List /= Nil_String loop
4983 Elem :=
4984 Project_Tree.String_Elements.Table (List);
4986 if Elem.Value /= No_Name then
4987 Add_Switch
4988 (Get_Name_String (Elem.Value),
4989 Compiler,
4990 And_Save => False);
4991 end if;
4993 List := Elem.Next;
4994 end loop;
4996 exit;
4997 end if;
4998 end if;
5000 Global_Compilation_Array := Global_Compilation_Elem.Next;
5001 end loop;
5002 end;
5003 end if;
5005 Osint.Add_Default_Search_Dirs;
5007 -- Record the current last switch index for table Binder_Switches
5008 -- and Linker_Switches, so that these tables may be reset before
5009 -- for each main, before adding switches from the project file
5010 -- and from the command line.
5012 Last_Binder_Switch := Binder_Switches.Last;
5013 Last_Linker_Switch := Linker_Switches.Last;
5015 Check_Steps;
5017 -- Add binder switches from the project file for the first main
5019 if Do_Bind_Step and then Binder_Package /= No_Package then
5020 if Verbose_Mode then
5021 Write_Str ("Adding binder switches for """);
5022 Write_Str (Main_Unit_File_Name);
5023 Write_Line (""".");
5024 end if;
5026 Add_Switches
5027 (Project_Node_Tree => Project_Node_Tree,
5028 File_Name => Main_Unit_File_Name,
5029 Index => Main_Index,
5030 The_Package => Binder_Package,
5031 Program => Binder);
5032 end if;
5034 -- Add linker switches from the project file for the first main
5036 if Do_Link_Step and then Linker_Package /= No_Package then
5037 if Verbose_Mode then
5038 Write_Str ("Adding linker switches for""");
5039 Write_Str (Main_Unit_File_Name);
5040 Write_Line (""".");
5041 end if;
5043 Add_Switches
5044 (Project_Node_Tree => Project_Node_Tree,
5045 File_Name => Main_Unit_File_Name,
5046 Index => Main_Index,
5047 The_Package => Linker_Package,
5048 Program => Linker);
5049 end if;
5050 end;
5051 end if;
5053 -- Get the target parameters, which are only needed for a couple of
5054 -- cases in gnatmake. Protect against an exception, such as the case
5055 -- of system.ads missing from the library, and fail gracefully.
5057 begin
5058 Targparm.Get_Target_Parameters;
5059 exception
5060 when Unrecoverable_Error =>
5061 Make_Failed ("*** make failed.");
5062 end;
5064 -- Special processing for VM targets
5066 if Targparm.VM_Target /= No_VM then
5068 -- Set proper processing commands
5070 case Targparm.VM_Target is
5071 when Targparm.JVM_Target =>
5073 -- Do not check for an object file (".o") when compiling to
5074 -- JVM machine since ".class" files are generated instead.
5076 Check_Object_Consistency := False;
5077 Gcc := new String'("jvm-gnatcompile");
5079 when Targparm.CLI_Target =>
5080 Gcc := new String'("dotnet-gnatcompile");
5082 when Targparm.No_VM =>
5083 raise Program_Error;
5084 end case;
5085 end if;
5087 Display_Commands (not Quiet_Output);
5089 Check_Steps;
5091 if Main_Project /= No_Project then
5093 -- For all library project, if the library file does not exist, put
5094 -- all the project sources in the queue, and flag the project so that
5095 -- the library is generated.
5097 if not Unique_Compile
5098 and then MLib.Tgt.Support_For_Libraries /= Prj.None
5099 then
5100 declare
5101 Proj : Project_List;
5103 begin
5104 Proj := Project_Tree.Projects;
5105 while Proj /= null loop
5106 if Proj.Project.Library then
5107 Proj.Project.Need_To_Build_Lib :=
5108 not MLib.Tgt.Library_Exists_For
5109 (Proj.Project, Project_Tree)
5110 and then not Proj.Project.Externally_Built;
5112 if Proj.Project.Need_To_Build_Lib then
5114 -- If there is no object directory, then it will be
5115 -- impossible to build the library. So fail
5116 -- immediately.
5119 Proj.Project.Object_Directory = No_Path_Information
5120 then
5121 Make_Failed
5122 ("no object files to build library for project """
5123 & Get_Name_String (Proj.Project.Name)
5124 & """");
5125 Proj.Project.Need_To_Build_Lib := False;
5127 else
5128 if Verbose_Mode then
5129 Write_Str
5130 ("Library file does not exist for project """);
5131 Write_Str (Get_Name_String (Proj.Project.Name));
5132 Write_Line ("""");
5133 end if;
5135 Insert_Project_Sources
5136 (The_Project => Proj.Project,
5137 All_Projects => False,
5138 Into_Q => True);
5139 end if;
5140 end if;
5141 end if;
5143 Proj := Proj.Next;
5144 end loop;
5145 end;
5146 end if;
5148 -- If a relative path output file has been specified, we add
5149 -- the exec directory.
5151 for J in reverse 1 .. Saved_Linker_Switches.Last - 1 loop
5152 if Saved_Linker_Switches.Table (J).all = Output_Flag.all then
5153 declare
5154 Exec_File_Name : constant String :=
5155 Saved_Linker_Switches.Table (J + 1).all;
5157 begin
5158 if not Is_Absolute_Path (Exec_File_Name) then
5159 Get_Name_String (Main_Project.Exec_Directory.Name);
5161 if not
5162 Is_Directory_Separator (Name_Buffer (Name_Len))
5163 then
5164 Add_Char_To_Name_Buffer (Directory_Separator);
5165 end if;
5167 Add_Str_To_Name_Buffer (Exec_File_Name);
5168 Saved_Linker_Switches.Table (J + 1) :=
5169 new String'(Name_Buffer (1 .. Name_Len));
5170 end if;
5171 end;
5173 exit;
5174 end if;
5175 end loop;
5177 -- If we are using a project file, for relative paths we add the
5178 -- current working directory for any relative path on the command
5179 -- line and the project directory, for any relative path in the
5180 -- project file.
5182 declare
5183 Dir_Path : constant String :=
5184 Get_Name_String (Main_Project.Directory.Name);
5185 begin
5186 for J in 1 .. Binder_Switches.Last loop
5187 Test_If_Relative_Path
5188 (Binder_Switches.Table (J),
5189 Parent => Dir_Path, Including_L_Switch => False);
5190 end loop;
5192 for J in 1 .. Saved_Binder_Switches.Last loop
5193 Test_If_Relative_Path
5194 (Saved_Binder_Switches.Table (J),
5195 Parent => Current_Work_Dir.all, Including_L_Switch => False);
5196 end loop;
5198 for J in 1 .. Linker_Switches.Last loop
5199 Test_If_Relative_Path
5200 (Linker_Switches.Table (J), Parent => Dir_Path);
5201 end loop;
5203 for J in 1 .. Saved_Linker_Switches.Last loop
5204 Test_If_Relative_Path
5205 (Saved_Linker_Switches.Table (J),
5206 Parent => Current_Work_Dir.all);
5207 end loop;
5209 for J in 1 .. Gcc_Switches.Last loop
5210 Test_If_Relative_Path
5211 (Gcc_Switches.Table (J),
5212 Parent => Dir_Path,
5213 Including_Non_Switch => False);
5214 end loop;
5216 for J in 1 .. Saved_Gcc_Switches.Last loop
5217 Test_If_Relative_Path
5218 (Saved_Gcc_Switches.Table (J),
5219 Parent => Current_Work_Dir.all,
5220 Including_Non_Switch => False);
5221 end loop;
5222 end;
5223 end if;
5225 -- We now put in the Binder_Switches and Linker_Switches tables, the
5226 -- binder and linker switches of the command line that have been put in
5227 -- the Saved_ tables. If a project file was used, then the command line
5228 -- switches will follow the project file switches.
5230 for J in 1 .. Saved_Binder_Switches.Last loop
5231 Add_Switch
5232 (Saved_Binder_Switches.Table (J),
5233 Binder,
5234 And_Save => False);
5235 end loop;
5237 for J in 1 .. Saved_Linker_Switches.Last loop
5238 Add_Switch
5239 (Saved_Linker_Switches.Table (J),
5240 Linker,
5241 And_Save => False);
5242 end loop;
5244 -- If no project file is used, we just put the gcc switches
5245 -- from the command line in the Gcc_Switches table.
5247 if Main_Project = No_Project then
5248 for J in 1 .. Saved_Gcc_Switches.Last loop
5249 Add_Switch
5250 (Saved_Gcc_Switches.Table (J), Compiler, And_Save => False);
5251 end loop;
5253 else
5254 -- If there is a project, put the command line gcc switches in the
5255 -- variable The_Saved_Gcc_Switches. They are going to be used later
5256 -- in procedure Compile_Sources.
5258 The_Saved_Gcc_Switches :=
5259 new Argument_List (1 .. Saved_Gcc_Switches.Last + 1);
5261 for J in 1 .. Saved_Gcc_Switches.Last loop
5262 The_Saved_Gcc_Switches (J) := Saved_Gcc_Switches.Table (J);
5263 end loop;
5265 -- We never use gnat.adc when a project file is used
5267 The_Saved_Gcc_Switches (The_Saved_Gcc_Switches'Last) := No_gnat_adc;
5268 end if;
5270 -- If there was a --GCC, --GNATBIND or --GNATLINK switch on
5271 -- the command line, then we have to use it, even if there was
5272 -- another switch in the project file.
5274 if Saved_Gcc /= null then
5275 Gcc := Saved_Gcc;
5276 end if;
5278 if Saved_Gnatbind /= null then
5279 Gnatbind := Saved_Gnatbind;
5280 end if;
5282 if Saved_Gnatlink /= null then
5283 Gnatlink := Saved_Gnatlink;
5284 end if;
5286 Gcc_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
5287 Gnatbind_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gnatbind.all);
5288 Gnatlink_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gnatlink.all);
5290 -- If we have specified -j switch both from the project file
5291 -- and on the command line, the one from the command line takes
5292 -- precedence.
5294 if Saved_Maximum_Processes = 0 then
5295 Saved_Maximum_Processes := Maximum_Processes;
5296 end if;
5298 -- Allocate as many temporary mapping file names as the maximum number
5299 -- of compilations processed, for each possible project.
5301 declare
5302 Data : Project_Compilation_Access;
5303 Proj : Project_List := Project_Tree.Projects;
5304 begin
5305 while Proj /= null loop
5306 Data := new Project_Compilation_Data'
5307 (Mapping_File_Names => new Temp_Path_Names
5308 (1 .. Saved_Maximum_Processes),
5309 Last_Mapping_File_Names => 0,
5310 Free_Mapping_File_Indices => new Free_File_Indices
5311 (1 .. Saved_Maximum_Processes),
5312 Last_Free_Indices => 0);
5314 Project_Compilation_Htable.Set
5315 (Project_Compilation, Proj.Project, Data);
5316 Proj := Proj.Next;
5317 end loop;
5319 Data := new Project_Compilation_Data'
5320 (Mapping_File_Names => new Temp_Path_Names
5321 (1 .. Saved_Maximum_Processes),
5322 Last_Mapping_File_Names => 0,
5323 Free_Mapping_File_Indices => new Free_File_Indices
5324 (1 .. Saved_Maximum_Processes),
5325 Last_Free_Indices => 0);
5327 Project_Compilation_Htable.Set
5328 (Project_Compilation, No_Project, Data);
5329 end;
5331 Bad_Compilation.Init;
5333 -- If project files are used, create the mapping of all the sources, so
5334 -- that the correct paths will be found. Otherwise, if there is a file
5335 -- which is not a source with the same name in a source directory this
5336 -- file may be incorrectly found.
5338 if Main_Project /= No_Project then
5339 Prj.Env.Create_Mapping (Project_Tree);
5340 end if;
5342 Current_Main_Index := Main_Index;
5344 -- Here is where the make process is started
5346 -- We do the same process for each main
5348 Multiple_Main_Loop : for N_File in 1 .. Osint.Number_Of_Files loop
5350 -- First, find the executable name and path
5352 Executable := No_File;
5353 Executable_Obsolete := False;
5354 Non_Std_Executable :=
5355 Targparm.Executable_Extension_On_Target /= No_Name;
5357 -- Look inside the linker switches to see if the name of the final
5358 -- executable program was specified.
5360 for J in reverse Linker_Switches.First .. Linker_Switches.Last loop
5361 if Linker_Switches.Table (J).all = Output_Flag.all then
5362 pragma Assert (J < Linker_Switches.Last);
5364 -- We cannot specify a single executable for several main
5365 -- subprograms
5367 if Osint.Number_Of_Files > 1 then
5368 Fail
5369 ("cannot specify a single executable for several mains");
5370 end if;
5372 Name_Len := 0;
5373 Add_Str_To_Name_Buffer (Linker_Switches.Table (J + 1).all);
5374 Executable := Name_Enter;
5376 Verbose_Msg (Executable, "final executable");
5377 end if;
5378 end loop;
5380 -- If the name of the final executable program was not specified then
5381 -- construct it from the main input file.
5383 if Executable = No_File then
5384 if Main_Project = No_Project then
5385 Executable := Executable_Name (Strip_Suffix (Main_Source_File));
5387 else
5388 -- If we are using a project file, we attempt to remove the
5389 -- body (or spec) termination of the main subprogram. We find
5390 -- it the naming scheme of the project file. This avoids
5391 -- generating an executable "main.2" for a main subprogram
5392 -- "main.2.ada", when the body termination is ".2.ada".
5394 Executable :=
5395 Prj.Util.Executable_Of
5396 (Main_Project, Project_Tree, Main_Source_File, Main_Index);
5397 end if;
5398 end if;
5400 if Main_Project /= No_Project
5401 and then Main_Project.Exec_Directory /= No_Path_Information
5402 then
5403 declare
5404 Exec_File_Name : constant String :=
5405 Get_Name_String (Executable);
5407 begin
5408 if not Is_Absolute_Path (Exec_File_Name) then
5409 Get_Name_String (Main_Project.Exec_Directory.Display_Name);
5411 if Name_Buffer (Name_Len) /= Directory_Separator then
5412 Add_Char_To_Name_Buffer (Directory_Separator);
5413 end if;
5415 Add_Str_To_Name_Buffer (Exec_File_Name);
5416 Executable := Name_Find;
5417 end if;
5419 Non_Std_Executable := True;
5420 end;
5421 end if;
5423 if Do_Compile_Step then
5424 Recursive_Compilation_Step : declare
5425 Args : Argument_List (1 .. Gcc_Switches.Last);
5427 First_Compiled_File : File_Name_Type;
5428 Youngest_Obj_File : File_Name_Type;
5429 Youngest_Obj_Stamp : Time_Stamp_Type;
5431 Executable_Stamp : Time_Stamp_Type;
5432 -- Executable is the final executable program
5433 -- ??? comment seems unrelated to declaration
5435 Library_Rebuilt : Boolean := False;
5437 begin
5438 for J in 1 .. Gcc_Switches.Last loop
5439 Args (J) := Gcc_Switches.Table (J);
5440 end loop;
5442 -- Now we invoke Compile_Sources for the current main
5444 Compile_Sources
5445 (Main_Source => Main_Source_File,
5446 Args => Args,
5447 First_Compiled_File => First_Compiled_File,
5448 Most_Recent_Obj_File => Youngest_Obj_File,
5449 Most_Recent_Obj_Stamp => Youngest_Obj_Stamp,
5450 Main_Unit => Is_Main_Unit,
5451 Main_Index => Current_Main_Index,
5452 Compilation_Failures => Compilation_Failures,
5453 Check_Readonly_Files => Check_Readonly_Files,
5454 Do_Not_Execute => Do_Not_Execute,
5455 Force_Compilations => Force_Compilations,
5456 In_Place_Mode => In_Place_Mode,
5457 Keep_Going => Keep_Going,
5458 Initialize_ALI_Data => True,
5459 Max_Process => Saved_Maximum_Processes);
5461 if Verbose_Mode then
5462 Write_Str ("End of compilation");
5463 Write_Eol;
5464 end if;
5466 -- Make sure the queue will be reinitialized for the next round
5468 First_Q_Initialization := True;
5470 Total_Compilation_Failures :=
5471 Total_Compilation_Failures + Compilation_Failures;
5473 if Total_Compilation_Failures /= 0 then
5474 if Keep_Going then
5475 goto Next_Main;
5477 else
5478 List_Bad_Compilations;
5479 Report_Compilation_Failed;
5480 end if;
5481 end if;
5483 -- Regenerate libraries, if there are any and if object files
5484 -- have been regenerated.
5486 if Main_Project /= No_Project
5487 and then MLib.Tgt.Support_For_Libraries /= Prj.None
5488 and then (Do_Bind_Step
5489 or Unique_Compile_All_Projects
5490 or not Compile_Only)
5491 and then (Do_Link_Step or else N_File = Osint.Number_Of_Files)
5492 then
5493 Library_Projs.Init;
5495 declare
5496 Depth : Natural;
5497 Current : Natural;
5498 Proj1 : Project_List;
5500 procedure Add_To_Library_Projs (Proj : Project_Id);
5501 -- Add project Project to table Library_Projs in
5502 -- decreasing depth order.
5504 --------------------------
5505 -- Add_To_Library_Projs --
5506 --------------------------
5508 procedure Add_To_Library_Projs (Proj : Project_Id) is
5509 Prj : Project_Id;
5511 begin
5512 Library_Projs.Increment_Last;
5513 Depth := Proj.Depth;
5515 -- Put the projects in decreasing depth order, so that
5516 -- if libA depends on libB, libB is first in order.
5518 Current := Library_Projs.Last;
5519 while Current > 1 loop
5520 Prj := Library_Projs.Table (Current - 1);
5521 exit when Prj.Depth >= Depth;
5522 Library_Projs.Table (Current) := Prj;
5523 Current := Current - 1;
5524 end loop;
5526 Library_Projs.Table (Current) := Proj;
5527 end Add_To_Library_Projs;
5529 -- Start of processing for ??? (should name declare block
5530 -- or probably better, break this out as a nested proc).
5532 begin
5533 -- Put in Library_Projs table all library project file
5534 -- ids when the library need to be rebuilt.
5536 Proj1 := Project_Tree.Projects;
5537 while Proj1 /= null loop
5538 if Proj1.Project.Standalone_Library then
5539 Stand_Alone_Libraries := True;
5540 end if;
5542 if Proj1.Project.Library then
5543 MLib.Prj.Check_Library
5544 (Proj1.Project, Project_Tree);
5545 end if;
5547 if Proj1.Project.Need_To_Build_Lib then
5548 Add_To_Library_Projs (Proj1.Project);
5549 end if;
5551 Proj1 := Proj1.Next;
5552 end loop;
5554 -- Check if importing libraries should be regenerated
5555 -- because at least an imported library will be
5556 -- regenerated or is more recent.
5558 Proj1 := Project_Tree.Projects;
5559 while Proj1 /= null loop
5560 if Proj1.Project.Library
5561 and then Proj1.Project.Library_Kind /= Static
5562 and then not Proj1.Project.Need_To_Build_Lib
5563 and then not Proj1.Project.Externally_Built
5564 then
5565 declare
5566 List : Project_List;
5567 Proj2 : Project_Id;
5568 Rebuild : Boolean := False;
5570 Lib_Timestamp1 : constant Time_Stamp_Type :=
5571 Proj1.Project.Library_TS;
5573 begin
5574 List := Proj1.Project.All_Imported_Projects;
5575 while List /= null loop
5576 Proj2 := List.Project;
5578 if Proj2.Library then
5579 if Proj2.Need_To_Build_Lib
5580 or else
5581 (Lib_Timestamp1 < Proj2.Library_TS)
5582 then
5583 Rebuild := True;
5584 exit;
5585 end if;
5586 end if;
5588 List := List.Next;
5589 end loop;
5591 if Rebuild then
5592 Proj1.Project.Need_To_Build_Lib := True;
5593 Add_To_Library_Projs (Proj1.Project);
5594 end if;
5595 end;
5596 end if;
5598 Proj1 := Proj1.Next;
5599 end loop;
5601 -- Reset the flags Need_To_Build_Lib for the next main,
5602 -- to avoid rebuilding libraries uselessly.
5604 Proj1 := Project_Tree.Projects;
5605 while Proj1 /= null loop
5606 Proj1.Project.Need_To_Build_Lib := False;
5607 Proj1 := Proj1.Next;
5608 end loop;
5609 end;
5611 -- Build the libraries, if any need to be built
5613 for J in 1 .. Library_Projs.Last loop
5614 Library_Rebuilt := True;
5616 -- If a library is rebuilt, then executables are obsolete
5618 Executable_Obsolete := True;
5620 MLib.Prj.Build_Library
5621 (For_Project => Library_Projs.Table (J),
5622 In_Tree => Project_Tree,
5623 Gnatbind => Gnatbind.all,
5624 Gnatbind_Path => Gnatbind_Path,
5625 Gcc => Gcc.all,
5626 Gcc_Path => Gcc_Path);
5627 end loop;
5628 end if;
5630 if List_Dependencies then
5631 if First_Compiled_File /= No_File then
5632 Inform
5633 (First_Compiled_File,
5634 "must be recompiled. Can't generate dependence list.");
5635 else
5636 List_Depend;
5637 end if;
5639 elsif First_Compiled_File = No_File
5640 and then not Do_Bind_Step
5641 and then not Quiet_Output
5642 and then not Library_Rebuilt
5643 and then Osint.Number_Of_Files = 1
5644 then
5645 Inform (Msg => "objects up to date.");
5647 elsif Do_Not_Execute
5648 and then First_Compiled_File /= No_File
5649 then
5650 Write_Name (First_Compiled_File);
5651 Write_Eol;
5652 end if;
5654 -- Stop after compile step if any of:
5656 -- 1) -n (Do_Not_Execute) specified
5658 -- 2) -M (List_Dependencies) specified (also sets
5659 -- Do_Not_Execute above, so this is probably superfluous).
5661 -- 3) -c (Compile_Only) specified, but not -b (Bind_Only)
5663 -- 4) Made unit cannot be a main unit
5665 if ((Do_Not_Execute
5666 or List_Dependencies
5667 or not Do_Bind_Step
5668 or not Is_Main_Unit)
5669 and then not No_Main_Subprogram
5670 and then not Build_Bind_And_Link_Full_Project)
5671 or else Unique_Compile
5672 then
5673 if Osint.Number_Of_Files = 1 then
5674 exit Multiple_Main_Loop;
5676 else
5677 goto Next_Main;
5678 end if;
5679 end if;
5681 -- If the objects were up-to-date check if the executable file
5682 -- is also up-to-date. For now always bind and link on the JVM
5683 -- since there is currently no simple way to check whether
5684 -- objects are up-to-date.
5686 if Targparm.VM_Target /= JVM_Target
5687 and then First_Compiled_File = No_File
5688 then
5689 Executable_Stamp := File_Stamp (Executable);
5691 if not Executable_Obsolete then
5692 Executable_Obsolete :=
5693 Youngest_Obj_Stamp > Executable_Stamp;
5694 end if;
5696 if not Executable_Obsolete then
5697 for Index in reverse 1 .. Dependencies.Last loop
5698 if Is_In_Obsoleted
5699 (Dependencies.Table (Index).Depends_On)
5700 then
5701 Enter_Into_Obsoleted
5702 (Dependencies.Table (Index).This);
5703 end if;
5704 end loop;
5706 Executable_Obsolete := Is_In_Obsoleted (Main_Source_File);
5707 Dependencies.Init;
5708 end if;
5710 if not Executable_Obsolete then
5712 -- If no Ada object files obsolete the executable, check
5713 -- for younger or missing linker files.
5715 Check_Linker_Options
5716 (Executable_Stamp,
5717 Youngest_Obj_File,
5718 Youngest_Obj_Stamp);
5720 Executable_Obsolete := Youngest_Obj_File /= No_File;
5721 end if;
5723 -- Check if any library file is more recent than the
5724 -- executable: there may be an externally built library
5725 -- file that has been modified.
5727 if not Executable_Obsolete
5728 and then Main_Project /= No_Project
5729 then
5730 declare
5731 Proj1 : Project_List;
5733 begin
5734 Proj1 := Project_Tree.Projects;
5735 while Proj1 /= null loop
5736 if Proj1.Project.Library
5737 and then
5738 Proj1.Project.Library_TS > Executable_Stamp
5739 then
5740 Executable_Obsolete := True;
5741 Youngest_Obj_Stamp := Proj1.Project.Library_TS;
5742 Name_Len := 0;
5743 Add_Str_To_Name_Buffer ("library ");
5744 Add_Str_To_Name_Buffer
5745 (Get_Name_String (Proj1.Project.Library_Name));
5746 Youngest_Obj_File := Name_Find;
5747 exit;
5748 end if;
5750 Proj1 := Proj1.Next;
5751 end loop;
5752 end;
5753 end if;
5755 -- Return if the executable is up to date and otherwise
5756 -- motivate the relink/rebind.
5758 if not Executable_Obsolete then
5759 if not Quiet_Output then
5760 Inform (Executable, "up to date.");
5761 end if;
5763 if Osint.Number_Of_Files = 1 then
5764 exit Multiple_Main_Loop;
5766 else
5767 goto Next_Main;
5768 end if;
5769 end if;
5771 if Executable_Stamp (1) = ' ' then
5772 if not No_Main_Subprogram then
5773 Verbose_Msg (Executable, "missing.", Prefix => " ");
5774 end if;
5776 elsif Youngest_Obj_Stamp (1) = ' ' then
5777 Verbose_Msg
5778 (Youngest_Obj_File, "missing.", Prefix => " ");
5780 elsif Youngest_Obj_Stamp > Executable_Stamp then
5781 Verbose_Msg
5782 (Youngest_Obj_File,
5783 "(" & String (Youngest_Obj_Stamp) & ") newer than",
5784 Executable,
5785 "(" & String (Executable_Stamp) & ")");
5787 else
5788 Verbose_Msg
5789 (Executable, "needs to be rebuilt", Prefix => " ");
5791 end if;
5792 end if;
5793 end Recursive_Compilation_Step;
5794 end if;
5796 -- For binding and linking, we need to be in the object directory of
5797 -- the main project.
5799 if Main_Project /= No_Project then
5800 Change_To_Object_Directory (Main_Project);
5801 end if;
5803 -- If we are here, it means that we need to rebuilt the current main,
5804 -- so we set Executable_Obsolete to True to make sure that subsequent
5805 -- mains will be rebuilt.
5807 Main_ALI_In_Place_Mode_Step : declare
5808 ALI_File : File_Name_Type;
5809 Src_File : File_Name_Type;
5811 begin
5812 Src_File := Strip_Directory (Main_Source_File);
5813 ALI_File := Lib_File_Name (Src_File, Current_Main_Index);
5814 Main_ALI_File := Full_Lib_File_Name (ALI_File);
5816 -- When In_Place_Mode, the library file can be located in the
5817 -- Main_Source_File directory which may not be present in the
5818 -- library path. If it is not present then use the corresponding
5819 -- library file name.
5821 if Main_ALI_File = No_File and then In_Place_Mode then
5822 Get_Name_String (Get_Directory (Full_Source_Name (Src_File)));
5823 Get_Name_String_And_Append (ALI_File);
5824 Main_ALI_File := Name_Find;
5825 Main_ALI_File := Full_Lib_File_Name (Main_ALI_File);
5826 end if;
5828 if Main_ALI_File = No_File then
5829 Make_Failed ("could not find the main ALI file");
5830 end if;
5831 end Main_ALI_In_Place_Mode_Step;
5833 if Do_Bind_Step then
5834 Bind_Step : declare
5835 Args : Argument_List
5836 (Binder_Switches.First .. Binder_Switches.Last + 2);
5837 -- The arguments for the invocation of gnatbind
5839 Last_Arg : Natural := Binder_Switches.Last;
5840 -- Index of the last argument in Args
5842 Shared_Libs : Boolean := False;
5843 -- Set to True when there are shared library project files or
5844 -- when gnatbind is invoked with -shared.
5846 Proj : Project_List;
5848 begin
5849 -- Check if there are shared libraries, so that gnatbind is
5850 -- called with -shared. Check also if gnatbind is called with
5851 -- -shared, so that gnatlink is called with -shared-libgcc
5852 -- ensuring that the shared version of libgcc will be used.
5854 if Main_Project /= No_Project
5855 and then MLib.Tgt.Support_For_Libraries /= Prj.None
5856 then
5857 Proj := Project_Tree.Projects;
5858 while Proj /= null loop
5859 if Proj.Project.Library
5860 and then Proj.Project.Library_Kind /= Static
5861 then
5862 Shared_Libs := True;
5863 Bind_Shared := Shared_Switch'Access;
5864 exit;
5865 end if;
5866 Proj := Proj.Next;
5867 end loop;
5868 end if;
5870 -- Check now for switch -shared
5872 if not Shared_Libs then
5873 for J in Binder_Switches.First .. Last_Arg loop
5874 if Binder_Switches.Table (J).all = "-shared" then
5875 Shared_Libs := True;
5876 exit;
5877 end if;
5878 end loop;
5879 end if;
5881 -- If shared libraries present, invoke gnatlink with
5882 -- -shared-libgcc.
5884 if Shared_Libs then
5885 Link_With_Shared_Libgcc := Shared_Libgcc_Switch'Access;
5886 end if;
5888 -- Get all the binder switches
5890 for J in Binder_Switches.First .. Last_Arg loop
5891 Args (J) := Binder_Switches.Table (J);
5892 end loop;
5894 if Stand_Alone_Libraries then
5895 Last_Arg := Last_Arg + 1;
5896 Args (Last_Arg) := Force_Elab_Flags_String'Access;
5897 end if;
5899 if Main_Project /= No_Project then
5901 -- Put all the source directories in ADA_INCLUDE_PATH,
5902 -- and all the object directories in ADA_OBJECTS_PATH,
5903 -- except those of library projects.
5905 Prj.Env.Set_Ada_Paths (Main_Project, Project_Tree, False);
5907 -- If switch -C was specified, create a binder mapping file
5909 if Create_Mapping_File then
5910 Create_Binder_Mapping_File (Args, Last_Arg);
5911 end if;
5913 end if;
5915 begin
5916 Bind (Main_ALI_File,
5917 Bind_Shared.all & Args (Args'First .. Last_Arg));
5919 exception
5920 when others =>
5922 -- Delete the temporary mapping file, if one was created.
5924 if Mapping_Path /= No_Path then
5925 Delete_Temporary_File (Project_Tree, Mapping_Path);
5926 end if;
5928 -- And reraise the exception
5930 raise;
5931 end;
5933 -- If -dn was not specified, delete the temporary mapping file,
5934 -- if one was created.
5936 if Mapping_Path /= No_Path then
5937 Delete_Temporary_File (Project_Tree, Mapping_Path);
5938 end if;
5939 end Bind_Step;
5940 end if;
5942 if Do_Link_Step then
5943 Link_Step : declare
5944 Linker_Switches_Last : constant Integer := Linker_Switches.Last;
5945 Path_Option : constant String_Access :=
5946 MLib.Linker_Library_Path_Option;
5947 Libraries_Present : Boolean := False;
5948 Current : Natural;
5949 Proj2 : Project_Id;
5950 Depth : Natural;
5951 Proj1 : Project_List;
5953 begin
5954 if not Run_Path_Option then
5955 Linker_Switches.Increment_Last;
5956 Linker_Switches.Table (Linker_Switches.Last) :=
5957 new String'("-R");
5958 end if;
5960 if Main_Project /= No_Project then
5961 Library_Paths.Set_Last (0);
5962 Library_Projs.Init;
5964 if MLib.Tgt.Support_For_Libraries /= Prj.None then
5966 -- Check for library projects
5968 Proj1 := Project_Tree.Projects;
5969 while Proj1 /= null loop
5970 if Proj1.Project /= Main_Project
5971 and then Proj1.Project.Library
5972 then
5973 -- Add this project to table Library_Projs
5975 Libraries_Present := True;
5976 Depth := Proj1.Project.Depth;
5977 Library_Projs.Increment_Last;
5978 Current := Library_Projs.Last;
5980 -- Any project with a greater depth should be
5981 -- after this project in the list.
5983 while Current > 1 loop
5984 Proj2 := Library_Projs.Table (Current - 1);
5985 exit when Proj2.Depth <= Depth;
5986 Library_Projs.Table (Current) := Proj2;
5987 Current := Current - 1;
5988 end loop;
5990 Library_Projs.Table (Current) := Proj1.Project;
5992 -- If it is not a static library and path option
5993 -- is set, add it to the Library_Paths table.
5995 if Proj1.Project.Library_Kind /= Static
5996 and then Path_Option /= null
5997 then
5998 Library_Paths.Increment_Last;
5999 Library_Paths.Table (Library_Paths.Last) :=
6000 new String'
6001 (Get_Name_String
6002 (Proj1.Project.Library_Dir.Display_Name));
6003 end if;
6004 end if;
6006 Proj1 := Proj1.Next;
6007 end loop;
6009 for Index in 1 .. Library_Projs.Last loop
6011 -- Add the -L switch
6013 Linker_Switches.Increment_Last;
6014 Linker_Switches.Table (Linker_Switches.Last) :=
6015 new String'("-L" &
6016 Get_Name_String
6017 (Library_Projs.Table (Index).
6018 Library_Dir.Display_Name));
6020 -- Add the -l switch
6022 Linker_Switches.Increment_Last;
6023 Linker_Switches.Table (Linker_Switches.Last) :=
6024 new String'("-l" &
6025 Get_Name_String
6026 (Library_Projs.Table (Index).
6027 Library_Name));
6028 end loop;
6029 end if;
6031 if Libraries_Present then
6033 -- If Path_Option is not null, create the switch
6034 -- ("-Wl,-rpath," or equivalent) with all the non static
6035 -- library dirs plus the standard GNAT library dir.
6036 -- We do that only if Run_Path_Option is True
6037 -- (not disabled by -R switch).
6039 if Run_Path_Option and then Path_Option /= null then
6040 declare
6041 Option : String_Access;
6042 Length : Natural := Path_Option'Length;
6043 Current : Natural;
6045 begin
6046 if MLib.Separate_Run_Path_Options then
6048 -- We are going to create one switch of the form
6049 -- "-Wl,-rpath,dir_N" for each directory to
6050 -- consider.
6052 -- One switch for each library directory
6054 for Index in
6055 Library_Paths.First .. Library_Paths.Last
6056 loop
6057 Linker_Switches.Increment_Last;
6058 Linker_Switches.Table
6059 (Linker_Switches.Last) := new String'
6060 (Path_Option.all &
6061 Library_Paths.Table (Index).all);
6062 end loop;
6064 -- One switch for the standard GNAT library dir
6066 Linker_Switches.Increment_Last;
6067 Linker_Switches.Table
6068 (Linker_Switches.Last) := new String'
6069 (Path_Option.all & MLib.Utl.Lib_Directory);
6071 else
6072 -- We are going to create one switch of the form
6073 -- "-Wl,-rpath,dir_1:dir_2:dir_3"
6075 for Index in
6076 Library_Paths.First .. Library_Paths.Last
6077 loop
6078 -- Add the length of the library dir plus one
6079 -- for the directory separator.
6081 Length :=
6082 Length +
6083 Library_Paths.Table (Index)'Length + 1;
6084 end loop;
6086 -- Finally, add the length of the standard GNAT
6087 -- library dir.
6089 Length := Length + MLib.Utl.Lib_Directory'Length;
6090 Option := new String (1 .. Length);
6091 Option (1 .. Path_Option'Length) :=
6092 Path_Option.all;
6093 Current := Path_Option'Length;
6095 -- Put each library dir followed by a dir
6096 -- separator.
6098 for Index in
6099 Library_Paths.First .. Library_Paths.Last
6100 loop
6101 Option
6102 (Current + 1 ..
6103 Current +
6104 Library_Paths.Table (Index)'Length) :=
6105 Library_Paths.Table (Index).all;
6106 Current :=
6107 Current +
6108 Library_Paths.Table (Index)'Length + 1;
6109 Option (Current) := Path_Separator;
6110 end loop;
6112 -- Finally put the standard GNAT library dir
6114 Option
6115 (Current + 1 ..
6116 Current + MLib.Utl.Lib_Directory'Length) :=
6117 MLib.Utl.Lib_Directory;
6119 -- And add the switch to the linker switches
6121 Linker_Switches.Increment_Last;
6122 Linker_Switches.Table (Linker_Switches.Last) :=
6123 Option;
6124 end if;
6125 end;
6126 end if;
6128 end if;
6130 -- Put the object directories in ADA_OBJECTS_PATH
6132 Prj.Env.Set_Ada_Paths (Main_Project, Project_Tree, False);
6134 -- Check for attributes Linker'Linker_Options in projects
6135 -- other than the main project
6137 declare
6138 Linker_Options : constant String_List :=
6139 Linker_Options_Switches
6140 (Main_Project, Project_Tree);
6141 begin
6142 for Option in Linker_Options'Range loop
6143 Linker_Switches.Increment_Last;
6144 Linker_Switches.Table (Linker_Switches.Last) :=
6145 Linker_Options (Option);
6146 end loop;
6147 end;
6148 end if;
6150 declare
6151 Args : Argument_List
6152 (Linker_Switches.First .. Linker_Switches.Last + 2);
6154 Last_Arg : Integer := Linker_Switches.First - 1;
6155 Skip : Boolean := False;
6157 begin
6158 -- Get all the linker switches
6160 for J in Linker_Switches.First .. Linker_Switches.Last loop
6161 if Skip then
6162 Skip := False;
6164 elsif Non_Std_Executable
6165 and then Linker_Switches.Table (J).all = "-o"
6166 then
6167 Skip := True;
6169 -- Here we capture and duplicate the linker argument. We
6170 -- need to do the duplication since the arguments will
6171 -- get normalized. Not doing so will result in calling
6172 -- normalized two times for the same set of arguments if
6173 -- gnatmake is passed multiple mains. This can result in
6174 -- the wrong argument being passed to the linker.
6176 else
6177 Last_Arg := Last_Arg + 1;
6178 Args (Last_Arg) :=
6179 new String'(Linker_Switches.Table (J).all);
6180 end if;
6181 end loop;
6183 -- If need be, add the -o switch
6185 if Non_Std_Executable then
6186 Last_Arg := Last_Arg + 1;
6187 Args (Last_Arg) := new String'("-o");
6188 Last_Arg := Last_Arg + 1;
6189 Args (Last_Arg) :=
6190 new String'(Get_Name_String (Executable));
6191 end if;
6193 -- And invoke the linker
6195 declare
6196 Success : Boolean := False;
6197 begin
6198 Link (Main_ALI_File,
6199 Link_With_Shared_Libgcc.all &
6200 Args (Args'First .. Last_Arg),
6201 Success);
6203 if Success then
6204 Successful_Links.Increment_Last;
6205 Successful_Links.Table (Successful_Links.Last) :=
6206 Main_ALI_File;
6208 elsif Osint.Number_Of_Files = 1
6209 or else not Keep_Going
6210 then
6211 Make_Failed ("*** link failed.");
6213 else
6214 Set_Standard_Error;
6215 Write_Line ("*** link failed");
6217 if Commands_To_Stdout then
6218 Set_Standard_Output;
6219 end if;
6221 Failed_Links.Increment_Last;
6222 Failed_Links.Table (Failed_Links.Last) :=
6223 Main_ALI_File;
6224 end if;
6225 end;
6226 end;
6228 Linker_Switches.Set_Last (Linker_Switches_Last);
6229 end Link_Step;
6230 end if;
6232 -- We go to here when we skip the bind and link steps
6234 <<Next_Main>>
6236 -- We go to the next main, if we did not process the last one
6238 if N_File < Osint.Number_Of_Files then
6239 Main_Source_File := Next_Main_Source;
6241 if Current_File_Index /= No_Index then
6242 Main_Index := Current_File_Index;
6243 end if;
6245 if Main_Project /= No_Project then
6247 -- Find the file name of the main unit
6249 declare
6250 Main_Source_File_Name : constant String :=
6251 Get_Name_String (Main_Source_File);
6253 Main_Unit_File_Name : constant String :=
6254 Prj.Env.
6255 File_Name_Of_Library_Unit_Body
6256 (Name => Main_Source_File_Name,
6257 Project => Main_Project,
6258 In_Tree => Project_Tree,
6259 Main_Project_Only =>
6260 not Unique_Compile);
6262 The_Packages : constant Package_Id :=
6263 Main_Project.Decl.Packages;
6265 Binder_Package : constant Prj.Package_Id :=
6266 Prj.Util.Value_Of
6267 (Name => Name_Binder,
6268 In_Packages => The_Packages,
6269 In_Tree => Project_Tree);
6271 Linker_Package : constant Prj.Package_Id :=
6272 Prj.Util.Value_Of
6273 (Name => Name_Linker,
6274 In_Packages => The_Packages,
6275 In_Tree => Project_Tree);
6277 begin
6278 -- We fail if we cannot find the main source file
6279 -- as an immediate source of the main project file.
6281 if Main_Unit_File_Name = "" then
6282 Make_Failed ('"' & Main_Source_File_Name
6283 & """ is not a unit of project "
6284 & Project_File_Name.all & ".");
6286 else
6287 -- Remove any directory information from the main
6288 -- source file name.
6290 declare
6291 Pos : Natural := Main_Unit_File_Name'Last;
6293 begin
6294 loop
6295 exit when Pos < Main_Unit_File_Name'First
6296 or else
6297 Main_Unit_File_Name (Pos) = Directory_Separator;
6298 Pos := Pos - 1;
6299 end loop;
6301 Name_Len := Main_Unit_File_Name'Last - Pos;
6303 Name_Buffer (1 .. Name_Len) :=
6304 Main_Unit_File_Name
6305 (Pos + 1 .. Main_Unit_File_Name'Last);
6307 Main_Source_File := Name_Find;
6308 end;
6309 end if;
6311 -- We now deal with the binder and linker switches.
6312 -- If no project file is used, there is nothing to do
6313 -- because the binder and linker switches are the same
6314 -- for all mains.
6316 -- Reset the tables Binder_Switches and Linker_Switches
6318 Binder_Switches.Set_Last (Last_Binder_Switch);
6319 Linker_Switches.Set_Last (Last_Linker_Switch);
6321 -- Add binder switches from the project file for this main,
6322 -- if any.
6324 if Do_Bind_Step and then Binder_Package /= No_Package then
6325 if Verbose_Mode then
6326 Write_Str ("Adding binder switches for """);
6327 Write_Str (Main_Unit_File_Name);
6328 Write_Line (""".");
6329 end if;
6331 Add_Switches
6332 (Project_Node_Tree => Project_Node_Tree,
6333 File_Name => Main_Unit_File_Name,
6334 Index => Main_Index,
6335 The_Package => Binder_Package,
6336 Program => Binder);
6337 end if;
6339 -- Add linker switches from the project file for this main,
6340 -- if any.
6342 if Do_Link_Step and then Linker_Package /= No_Package then
6343 if Verbose_Mode then
6344 Write_Str ("Adding linker switches for""");
6345 Write_Str (Main_Unit_File_Name);
6346 Write_Line (""".");
6347 end if;
6349 Add_Switches
6350 (Project_Node_Tree => Project_Node_Tree,
6351 File_Name => Main_Unit_File_Name,
6352 Index => Main_Index,
6353 The_Package => Linker_Package,
6354 Program => Linker);
6355 end if;
6357 -- As we are using a project file, for relative paths we add
6358 -- the current working directory for any relative path on
6359 -- the command line and the project directory, for any
6360 -- relative path in the project file.
6362 declare
6363 Dir_Path : constant String :=
6364 Get_Name_String
6365 (Main_Project.Directory.Name);
6367 begin
6369 J in Last_Binder_Switch + 1 .. Binder_Switches.Last
6370 loop
6371 Test_If_Relative_Path
6372 (Binder_Switches.Table (J),
6373 Parent => Dir_Path, Including_L_Switch => False);
6374 end loop;
6377 J in Last_Linker_Switch + 1 .. Linker_Switches.Last
6378 loop
6379 Test_If_Relative_Path
6380 (Linker_Switches.Table (J), Parent => Dir_Path);
6381 end loop;
6382 end;
6384 -- We now put in the Binder_Switches and Linker_Switches
6385 -- tables, the binder and linker switches of the command
6386 -- line that have been put in the Saved_ tables.
6387 -- These switches will follow the project file switches.
6389 for J in 1 .. Saved_Binder_Switches.Last loop
6390 Add_Switch
6391 (Saved_Binder_Switches.Table (J),
6392 Binder,
6393 And_Save => False);
6394 end loop;
6396 for J in 1 .. Saved_Linker_Switches.Last loop
6397 Add_Switch
6398 (Saved_Linker_Switches.Table (J),
6399 Linker,
6400 And_Save => False);
6401 end loop;
6402 end;
6403 end if;
6404 end if;
6406 -- Remove all marks to be sure to check sources for all executables,
6407 -- as the switches may be different and -s may be in use.
6409 Delete_All_Marks;
6410 end loop Multiple_Main_Loop;
6412 if Failed_Links.Last > 0 then
6413 for Index in 1 .. Successful_Links.Last loop
6414 Write_Str ("Linking of """);
6415 Write_Str (Get_Name_String (Successful_Links.Table (Index)));
6416 Write_Line (""" succeeded.");
6417 end loop;
6419 Set_Standard_Error;
6421 for Index in 1 .. Failed_Links.Last loop
6422 Write_Str ("Linking of """);
6423 Write_Str (Get_Name_String (Failed_Links.Table (Index)));
6424 Write_Line (""" failed.");
6425 end loop;
6427 if Commands_To_Stdout then
6428 Set_Standard_Output;
6429 end if;
6431 if Total_Compilation_Failures = 0 then
6432 Report_Compilation_Failed;
6433 end if;
6434 end if;
6436 if Total_Compilation_Failures /= 0 then
6437 List_Bad_Compilations;
6438 Report_Compilation_Failed;
6439 end if;
6441 -- Delete the temporary mapping file that was created if we are
6442 -- using project files.
6444 Delete_All_Temp_Files;
6446 exception
6447 when X : others =>
6448 Set_Standard_Error;
6449 Write_Line (Exception_Information (X));
6450 Make_Failed ("INTERNAL ERROR. Please report.");
6451 end Gnatmake;
6453 ----------
6454 -- Hash --
6455 ----------
6457 function Hash (F : File_Name_Type) return Header_Num is
6458 begin
6459 return Header_Num (1 + F mod Max_Header);
6460 end Hash;
6462 --------------------
6463 -- In_Ada_Lib_Dir --
6464 --------------------
6466 function In_Ada_Lib_Dir (File : File_Name_Type) return Boolean is
6467 D : constant File_Name_Type := Get_Directory (File);
6468 B : constant Byte := Get_Name_Table_Byte (D);
6469 begin
6470 return (B and Ada_Lib_Dir) /= 0;
6471 end In_Ada_Lib_Dir;
6473 -----------------------
6474 -- Init_Mapping_File --
6475 -----------------------
6477 procedure Init_Mapping_File
6478 (Project : Project_Id;
6479 Data : in out Project_Compilation_Data;
6480 File_Index : in out Natural)
6482 FD : File_Descriptor;
6483 Status : Boolean;
6484 -- For call to Close
6486 begin
6487 -- Increase the index of the last mapping file for this project
6489 Data.Last_Mapping_File_Names := Data.Last_Mapping_File_Names + 1;
6491 -- If there is a project file, call Create_Mapping_File with
6492 -- the project id.
6494 if Project /= No_Project then
6495 Prj.Env.Create_Mapping_File
6496 (Project,
6497 In_Tree => Project_Tree,
6498 Language => Name_Ada,
6499 Name => Data.Mapping_File_Names
6500 (Data.Last_Mapping_File_Names));
6502 -- Otherwise, just create an empty file
6504 else
6505 Tempdir.Create_Temp_File
6506 (FD,
6507 Data.Mapping_File_Names (Data.Last_Mapping_File_Names));
6509 if FD = Invalid_FD then
6510 Make_Failed ("disk full");
6512 else
6513 Record_Temp_File
6514 (Project_Tree,
6515 Data.Mapping_File_Names (Data.Last_Mapping_File_Names));
6516 end if;
6518 Close (FD, Status);
6520 if not Status then
6521 Make_Failed ("disk full");
6522 end if;
6523 end if;
6525 -- And return the index of the newly created file
6527 File_Index := Data.Last_Mapping_File_Names;
6528 end Init_Mapping_File;
6530 ------------
6531 -- Init_Q --
6532 ------------
6534 procedure Init_Q is
6535 begin
6536 First_Q_Initialization := False;
6537 Q_Front := Q.First;
6538 Q.Set_Last (Q.First);
6539 end Init_Q;
6541 ----------------
6542 -- Initialize --
6543 ----------------
6545 procedure Initialize (Project_Node_Tree : out Project_Node_Tree_Ref) is
6547 procedure Check_Version_And_Help is
6548 new Check_Version_And_Help_G (Makeusg);
6550 -- Start of processing for Initialize
6552 begin
6553 -- Prepare the project's tree, since this is used to hold external
6554 -- references, project path and other attributes that can be impacted by
6555 -- the command line switches
6557 Project_Node_Tree := new Project_Node_Tree_Data;
6558 Prj.Tree.Initialize (Project_Node_Tree);
6560 -- Override default initialization of Check_Object_Consistency since
6561 -- this is normally False for GNATBIND, but is True for GNATMAKE since
6562 -- we do not need to check source consistency again once GNATMAKE has
6563 -- looked at the sources to check.
6565 Check_Object_Consistency := True;
6567 -- Package initializations. The order of calls is important here
6569 Output.Set_Standard_Error;
6571 Gcc_Switches.Init;
6572 Binder_Switches.Init;
6573 Linker_Switches.Init;
6575 Csets.Initialize;
6576 Namet.Initialize;
6578 Snames.Initialize;
6580 Prj.Initialize (Project_Tree);
6582 Dependencies.Init;
6584 RTS_Specified := null;
6585 N_M_Switch := 0;
6587 Mains.Delete;
6589 -- Add the directory where gnatmake is invoked in front of the
6590 -- path, if gnatmake is invoked from a bin directory or with directory
6591 -- information. Only do this if the platform is not VMS, where the
6592 -- notion of path does not really exist.
6594 if not OpenVMS then
6595 declare
6596 Prefix : constant String := Executable_Prefix_Path;
6597 Command : constant String := Command_Name;
6599 begin
6600 if Prefix'Length > 0 then
6601 declare
6602 PATH : constant String :=
6603 Prefix & Directory_Separator & "bin" &
6604 Path_Separator &
6605 Getenv ("PATH").all;
6606 begin
6607 Setenv ("PATH", PATH);
6608 end;
6610 else
6611 for Index in reverse Command'Range loop
6612 if Command (Index) = Directory_Separator then
6613 declare
6614 Absolute_Dir : constant String :=
6615 Normalize_Pathname
6616 (Command (Command'First .. Index));
6617 PATH : constant String :=
6618 Absolute_Dir &
6619 Path_Separator &
6620 Getenv ("PATH").all;
6621 begin
6622 Setenv ("PATH", PATH);
6623 end;
6625 exit;
6626 end if;
6627 end loop;
6628 end if;
6629 end;
6630 end if;
6632 -- Scan the switches and arguments
6634 -- First, scan to detect --version and/or --help
6636 Check_Version_And_Help ("GNATMAKE", "1995");
6638 -- Scan again the switch and arguments, now that we are sure that they
6639 -- do not include --version or --help.
6641 Scan_Args : for Next_Arg in 1 .. Argument_Count loop
6642 Scan_Make_Arg
6643 (Project_Node_Tree, Argument (Next_Arg), And_Save => True);
6644 end loop Scan_Args;
6646 if N_M_Switch > 0 and RTS_Specified = null then
6647 Process_Multilib (Project_Node_Tree);
6648 end if;
6650 if Commands_To_Stdout then
6651 Set_Standard_Output;
6652 end if;
6654 if Usage_Requested then
6655 Usage;
6656 end if;
6658 -- Test for trailing -P switch
6660 if Project_File_Name_Present and then Project_File_Name = null then
6661 Make_Failed ("project file name missing after -P");
6663 -- Test for trailing -o switch
6665 elsif Output_File_Name_Present
6666 and then not Output_File_Name_Seen
6667 then
6668 Make_Failed ("output file name missing after -o");
6670 -- Test for trailing -D switch
6672 elsif Object_Directory_Present
6673 and then not Object_Directory_Seen then
6674 Make_Failed ("object directory missing after -D");
6675 end if;
6677 -- Test for simultaneity of -i and -D
6679 if Object_Directory_Path /= null and then In_Place_Mode then
6680 Make_Failed ("-i and -D cannot be used simultaneously");
6681 end if;
6683 -- Deal with -C= switch
6685 if Gnatmake_Mapping_File /= null then
6687 -- First, check compatibility with other switches
6689 if Project_File_Name /= null then
6690 Make_Failed ("-C= switch is not compatible with -P switch");
6692 elsif Saved_Maximum_Processes > 1 then
6693 Make_Failed ("-C= switch is not compatible with -jnnn switch");
6694 end if;
6696 Fmap.Initialize (Gnatmake_Mapping_File.all);
6697 Add_Switch
6698 ("-gnatem=" & Gnatmake_Mapping_File.all,
6699 Compiler,
6700 And_Save => True);
6701 end if;
6703 if Project_File_Name /= null then
6705 -- A project file was specified by a -P switch
6707 if Verbose_Mode then
6708 Write_Eol;
6709 Write_Str ("Parsing project file """);
6710 Write_Str (Project_File_Name.all);
6711 Write_Str (""".");
6712 Write_Eol;
6713 end if;
6715 -- Avoid looking in the current directory for ALI files
6717 -- Look_In_Primary_Dir := False;
6719 -- Set the project parsing verbosity to whatever was specified
6720 -- by a possible -vP switch.
6722 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
6724 -- Parse the project file.
6725 -- If there is an error, Main_Project will still be No_Project.
6727 Prj.Pars.Parse
6728 (Project => Main_Project,
6729 In_Tree => Project_Tree,
6730 Project_File_Name => Project_File_Name.all,
6731 Packages_To_Check => Packages_To_Check_By_Gnatmake,
6732 Flags => Gnatmake_Flags,
6733 In_Node_Tree => Project_Node_Tree);
6735 -- The parsing of project files may have changed the current output
6737 if Commands_To_Stdout then
6738 Set_Standard_Output;
6739 else
6740 Set_Standard_Error;
6741 end if;
6743 if Main_Project = No_Project then
6744 Make_Failed
6745 ("""" & Project_File_Name.all & """ processing failed");
6746 end if;
6748 Create_Mapping_File := True;
6750 if Verbose_Mode then
6751 Write_Eol;
6752 Write_Str ("Parsing of project file """);
6753 Write_Str (Project_File_Name.all);
6754 Write_Str (""" is finished.");
6755 Write_Eol;
6756 end if;
6758 -- We add the source directories and the object directories
6759 -- to the search paths.
6761 Add_Source_Directories (Main_Project, Project_Tree);
6762 Add_Object_Directories (Main_Project);
6764 Recursive_Compute_Depth (Main_Project);
6766 -- For each project compute the list of the projects it imports
6767 -- directly or indirectly.
6769 declare
6770 Proj : Project_List;
6771 begin
6772 Proj := Project_Tree.Projects;
6773 while Proj /= null loop
6774 Compute_All_Imported_Projects (Proj.Project);
6775 Proj := Proj.Next;
6776 end loop;
6777 end;
6779 else
6781 Osint.Add_Default_Search_Dirs;
6783 -- Source file lookups should be cached for efficiency. Source files
6784 -- are not supposed to change. However, we do that now only if no
6785 -- project file is used; if a project file is used, we do it just
6786 -- after changing the directory to the object directory.
6788 Osint.Source_File_Data (Cache => True);
6790 -- Read gnat.adc file to initialize Fname.UF
6792 Fname.UF.Initialize;
6794 begin
6795 Fname.SF.Read_Source_File_Name_Pragmas;
6797 exception
6798 when Err : SFN_Scan.Syntax_Error_In_GNAT_ADC =>
6799 Make_Failed (Exception_Message (Err));
6800 end;
6801 end if;
6803 -- Make sure no project object directory is recorded
6805 Project_Of_Current_Object_Directory := No_Project;
6807 end Initialize;
6809 ----------------------------
6810 -- Insert_Project_Sources --
6811 ----------------------------
6813 procedure Insert_Project_Sources
6814 (The_Project : Project_Id;
6815 All_Projects : Boolean;
6816 Into_Q : Boolean)
6818 Put_In_Q : Boolean := Into_Q;
6819 Unit : Unit_Index;
6820 Sfile : File_Name_Type;
6821 Index : Int;
6823 Extending : constant Boolean := The_Project.Extends /= No_Project;
6825 function Check_Project (P : Project_Id) return Boolean;
6826 -- Returns True if P is The_Project or a project extended by The_Project
6828 -------------------
6829 -- Check_Project --
6830 -------------------
6832 function Check_Project (P : Project_Id) return Boolean is
6833 begin
6834 if All_Projects or else P = The_Project then
6835 return True;
6837 elsif Extending then
6838 declare
6839 Proj : Project_Id;
6841 begin
6842 Proj := The_Project;
6843 while Proj /= null loop
6844 if P = Proj.Extends then
6845 return True;
6846 end if;
6848 Proj := Proj.Extends;
6849 end loop;
6850 end;
6851 end if;
6853 return False;
6854 end Check_Project;
6856 -- Start of processing for Insert_Project_Sources
6858 begin
6859 -- For all the sources in the project files,
6861 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
6862 while Unit /= null loop
6863 Sfile := No_File;
6864 Index := 0;
6866 -- If there is a source for the body, and the body has not been
6867 -- locally removed.
6869 if Unit.File_Names (Impl) /= null
6870 and then not Unit.File_Names (Impl).Locally_Removed
6871 then
6872 -- And it is a source for the specified project
6874 if Check_Project (Unit.File_Names (Impl).Project) then
6876 -- If we don't have a spec, we cannot consider the source
6877 -- if it is a subunit.
6879 if Unit.File_Names (Spec) = null then
6880 declare
6881 Src_Ind : Source_File_Index;
6883 -- Here we are cheating a little bit: we don't want to
6884 -- use Sinput.L, because it depends on the GNAT tree
6885 -- (Atree, Sinfo, ...). So, we pretend that it is a
6886 -- project file, and we use Sinput.P.
6888 -- Source_File_Is_Subunit is just scanning through the
6889 -- file until it finds one of the reserved words
6890 -- separate, procedure, function, generic or package.
6891 -- Fortunately, these Ada reserved words are also
6892 -- reserved for project files.
6894 begin
6895 Src_Ind := Sinput.P.Load_Project_File
6896 (Get_Name_String
6897 (Unit.File_Names (Impl).Path.Name));
6899 -- If it is a subunit, discard it
6901 if Sinput.P.Source_File_Is_Subunit (Src_Ind) then
6902 Sfile := No_File;
6903 Index := 0;
6904 else
6905 Sfile := Unit.File_Names (Impl).Display_File;
6906 Index := Unit.File_Names (Impl).Index;
6907 end if;
6908 end;
6910 else
6911 Sfile := Unit.File_Names (Impl).Display_File;
6912 Index := Unit.File_Names (Impl).Index;
6913 end if;
6914 end if;
6916 elsif Unit.File_Names (Spec) /= null
6917 and then not Unit.File_Names (Spec).Locally_Removed
6918 and then Check_Project (Unit.File_Names (Spec).Project)
6919 then
6920 -- If there is no source for the body, but there is a source
6921 -- for the spec which has not been locally removed, then we take
6922 -- this one.
6924 Sfile := Unit.File_Names (Spec).Display_File;
6925 Index := Unit.File_Names (Spec).Index;
6926 end if;
6928 -- If Put_In_Q is True, we insert into the Q
6930 if Put_In_Q then
6932 -- For the first source inserted into the Q, we need to initialize
6933 -- the Q, but not for the subsequent sources.
6935 if First_Q_Initialization then
6936 Init_Q;
6937 end if;
6939 -- And of course, only insert in the Q if the source is not marked
6941 if Sfile /= No_File and then not Is_Marked (Sfile, Index) then
6942 if Verbose_Mode then
6943 Write_Str ("Adding """);
6944 Write_Str (Get_Name_String (Sfile));
6945 Write_Line (""" to the queue");
6946 end if;
6948 Insert_Q (Sfile, Index => Index);
6949 Mark (Sfile, Index);
6950 end if;
6952 elsif Sfile /= No_File then
6954 -- If Put_In_Q is False, we add the source as if it were specified
6955 -- on the command line, and we set Put_In_Q to True, so that the
6956 -- following sources will be put directly in the queue. This will
6957 -- allow parallel compilation processes if -jx switch is used.
6959 if Verbose_Mode then
6960 Write_Str ("Adding """);
6961 Write_Str (Get_Name_String (Sfile));
6962 Write_Line (""" as if on the command line");
6963 end if;
6965 Osint.Add_File (Get_Name_String (Sfile), Index);
6966 Put_In_Q := True;
6968 -- As we may look into the Q later, ensure the Q has been
6969 -- initialized to avoid errors.
6971 if First_Q_Initialization then
6972 Init_Q;
6973 end if;
6974 end if;
6976 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
6977 end loop;
6978 end Insert_Project_Sources;
6980 --------------
6981 -- Insert_Q --
6982 --------------
6984 procedure Insert_Q
6985 (Source_File : File_Name_Type;
6986 Source_Unit : Unit_Name_Type := No_Unit_Name;
6987 Index : Int := 0)
6989 begin
6990 if Debug.Debug_Flag_Q then
6991 Write_Str (" Q := Q + [ ");
6992 Write_Name (Source_File);
6994 if Index /= 0 then
6995 Write_Str (", ");
6996 Write_Int (Index);
6997 end if;
6999 Write_Str (" ] ");
7000 Write_Eol;
7001 end if;
7003 Q.Table (Q.Last) :=
7004 (File => Source_File,
7005 Unit => Source_Unit,
7006 Index => Index);
7007 Q.Increment_Last;
7008 end Insert_Q;
7010 ---------------------
7011 -- Is_In_Obsoleted --
7012 ---------------------
7014 function Is_In_Obsoleted (F : File_Name_Type) return Boolean is
7015 begin
7016 if F = No_File then
7017 return False;
7019 else
7020 declare
7021 Name : constant String := Get_Name_String (F);
7022 First : Natural;
7023 F2 : File_Name_Type;
7025 begin
7026 First := Name'Last;
7027 while First > Name'First
7028 and then Name (First - 1) /= Directory_Separator
7029 and then Name (First - 1) /= '/'
7030 loop
7031 First := First - 1;
7032 end loop;
7034 if First /= Name'First then
7035 Name_Len := 0;
7036 Add_Str_To_Name_Buffer (Name (First .. Name'Last));
7037 F2 := Name_Find;
7039 else
7040 F2 := F;
7041 end if;
7043 return Obsoleted.Get (F2);
7044 end;
7045 end if;
7046 end Is_In_Obsoleted;
7048 ----------------------------
7049 -- Is_In_Object_Directory --
7050 ----------------------------
7052 function Is_In_Object_Directory
7053 (Source_File : File_Name_Type;
7054 Full_Lib_File : File_Name_Type) return Boolean
7056 begin
7057 -- There is something to check only when using project files. Otherwise,
7058 -- this function returns True (last line of the function).
7060 if Main_Project /= No_Project then
7061 declare
7062 Source_File_Name : constant String :=
7063 Get_Name_String (Source_File);
7064 Saved_Verbosity : constant Verbosity := Current_Verbosity;
7065 Project : Project_Id := No_Project;
7067 Path_Name : Path_Name_Type := No_Path;
7068 pragma Warnings (Off, Path_Name);
7070 begin
7071 -- Call Get_Reference to know the ultimate extending project of
7072 -- the source. Call it with verbosity default to avoid verbose
7073 -- messages.
7075 Current_Verbosity := Default;
7076 Prj.Env.Get_Reference
7077 (Source_File_Name => Source_File_Name,
7078 Project => Project,
7079 In_Tree => Project_Tree,
7080 Path => Path_Name);
7081 Current_Verbosity := Saved_Verbosity;
7083 -- If this source is in a project, check that the ALI file is in
7084 -- its object directory. If it is not, return False, so that the
7085 -- ALI file will not be skipped.
7087 if Project /= No_Project then
7088 declare
7089 Object_Directory : constant String :=
7090 Normalize_Pathname
7091 (Get_Name_String
7092 (Project.
7093 Object_Directory.Display_Name));
7095 Olast : Natural := Object_Directory'Last;
7097 Lib_File_Directory : constant String :=
7098 Normalize_Pathname (Dir_Name
7099 (Get_Name_String (Full_Lib_File)));
7101 Llast : Natural := Lib_File_Directory'Last;
7103 begin
7104 -- For directories, Normalize_Pathname may or may not put
7105 -- a directory separator at the end, depending on its input.
7106 -- Remove any last directory separator before comparison.
7107 -- Returns True only if the two directories are the same.
7109 if Object_Directory (Olast) = Directory_Separator then
7110 Olast := Olast - 1;
7111 end if;
7113 if Lib_File_Directory (Llast) = Directory_Separator then
7114 Llast := Llast - 1;
7115 end if;
7117 return Object_Directory (Object_Directory'First .. Olast) =
7118 Lib_File_Directory (Lib_File_Directory'First .. Llast);
7119 end;
7120 end if;
7121 end;
7122 end if;
7124 -- When the source is not in a project file, always return True
7126 return True;
7127 end Is_In_Object_Directory;
7129 ----------
7130 -- Link --
7131 ----------
7133 procedure Link
7134 (ALI_File : File_Name_Type;
7135 Args : Argument_List;
7136 Success : out Boolean)
7138 Link_Args : Argument_List (1 .. Args'Length + 1);
7140 begin
7141 Get_Name_String (ALI_File);
7142 Link_Args (1) := new String'(Name_Buffer (1 .. Name_Len));
7144 Link_Args (2 .. Args'Length + 1) := Args;
7146 GNAT.OS_Lib.Normalize_Arguments (Link_Args);
7148 Display (Gnatlink.all, Link_Args);
7150 if Gnatlink_Path = null then
7151 Make_Failed ("error, unable to locate " & Gnatlink.all);
7152 end if;
7154 GNAT.OS_Lib.Spawn (Gnatlink_Path.all, Link_Args, Success);
7155 end Link;
7157 ---------------------------
7158 -- List_Bad_Compilations --
7159 ---------------------------
7161 procedure List_Bad_Compilations is
7162 begin
7163 for J in Bad_Compilation.First .. Bad_Compilation.Last loop
7164 if Bad_Compilation.Table (J).File = No_File then
7165 null;
7166 elsif not Bad_Compilation.Table (J).Found then
7167 Inform (Bad_Compilation.Table (J).File, "not found");
7168 else
7169 Inform (Bad_Compilation.Table (J).File, "compilation error");
7170 end if;
7171 end loop;
7172 end List_Bad_Compilations;
7174 -----------------
7175 -- List_Depend --
7176 -----------------
7178 procedure List_Depend is
7179 Lib_Name : File_Name_Type;
7180 Obj_Name : File_Name_Type;
7181 Src_Name : File_Name_Type;
7183 Len : Natural;
7184 Line_Pos : Natural;
7185 Line_Size : constant := 77;
7187 begin
7188 Set_Standard_Output;
7190 for A in ALIs.First .. ALIs.Last loop
7191 Lib_Name := ALIs.Table (A).Afile;
7193 -- We have to provide the full library file name in In_Place_Mode
7195 if In_Place_Mode then
7196 Lib_Name := Full_Lib_File_Name (Lib_Name);
7197 end if;
7199 Obj_Name := Object_File_Name (Lib_Name);
7200 Write_Name (Obj_Name);
7201 Write_Str (" :");
7203 Get_Name_String (Obj_Name);
7204 Len := Name_Len;
7205 Line_Pos := Len + 2;
7207 for D in ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep loop
7208 Src_Name := Sdep.Table (D).Sfile;
7210 if Is_Internal_File_Name (Src_Name)
7211 and then not Check_Readonly_Files
7212 then
7213 null;
7214 else
7215 if not Quiet_Output then
7216 Src_Name := Full_Source_Name (Src_Name);
7217 end if;
7219 Get_Name_String (Src_Name);
7220 Len := Name_Len;
7222 if Line_Pos + Len + 1 > Line_Size then
7223 Write_Str (" \");
7224 Write_Eol;
7225 Line_Pos := 0;
7226 end if;
7228 Line_Pos := Line_Pos + Len + 1;
7230 Write_Str (" ");
7231 Write_Name (Src_Name);
7232 end if;
7233 end loop;
7235 Write_Eol;
7236 end loop;
7238 if not Commands_To_Stdout then
7239 Set_Standard_Error;
7240 end if;
7241 end List_Depend;
7243 -----------------
7244 -- Make_Failed --
7245 -----------------
7247 procedure Make_Failed (S : String) is
7248 begin
7249 Delete_All_Temp_Files;
7250 Osint.Fail (S);
7251 end Make_Failed;
7253 --------------------
7254 -- Mark_Directory --
7255 --------------------
7257 procedure Mark_Directory
7258 (Dir : String;
7259 Mark : Lib_Mark_Type;
7260 On_Command_Line : Boolean)
7262 N : Name_Id;
7263 B : Byte;
7265 function Base_Directory return String;
7266 -- If Dir comes from the command line, empty string (relative paths
7267 -- are resolved with respect to the current directory), else return
7268 -- the main project's directory.
7270 --------------------
7271 -- Base_Directory --
7272 --------------------
7274 function Base_Directory return String is
7275 begin
7276 if On_Command_Line then
7277 return "";
7278 else
7279 return Get_Name_String (Main_Project.Directory.Display_Name);
7280 end if;
7281 end Base_Directory;
7283 Real_Path : constant String := Normalize_Pathname (Dir, Base_Directory);
7285 -- Start of processing for Mark_Directory
7287 begin
7288 Name_Len := 0;
7290 if Real_Path'Length = 0 then
7291 Add_Str_To_Name_Buffer (Dir);
7293 else
7294 Add_Str_To_Name_Buffer (Real_Path);
7295 end if;
7297 -- Last character is supposed to be a directory separator
7299 if not Is_Directory_Separator (Name_Buffer (Name_Len)) then
7300 Add_Char_To_Name_Buffer (Directory_Separator);
7301 end if;
7303 -- Add flags to the already existing flags
7305 N := Name_Find;
7306 B := Get_Name_Table_Byte (N);
7307 Set_Name_Table_Byte (N, B or Mark);
7308 end Mark_Directory;
7310 ----------------------
7311 -- Process_Multilib --
7312 ----------------------
7314 procedure Process_Multilib
7315 (Project_Node_Tree : Project_Node_Tree_Ref)
7317 Output_FD : File_Descriptor;
7318 Output_Name : String_Access;
7319 Arg_Index : Natural := 0;
7320 Success : Boolean := False;
7321 Return_Code : Integer := 0;
7322 Multilib_Gcc_Path : String_Access;
7323 Multilib_Gcc : String_Access;
7324 N_Read : Integer := 0;
7325 Line : String (1 .. 1000);
7326 Args : Argument_List (1 .. N_M_Switch + 1);
7328 begin
7329 pragma Assert (N_M_Switch > 0 and RTS_Specified = null);
7331 -- In case we detected a multilib switch and the user has not
7332 -- manually specified a specific RTS we emulate the following command:
7333 -- gnatmake $FLAGS --RTS=$(gcc -print-multi-directory $FLAGS)
7335 -- First select the flags which might have an impact on multilib
7336 -- processing. Note that this is an heuristic selection and it
7337 -- will need to be maintained over time. The condition has to
7338 -- be kept synchronized with N_M_Switch counting in Scan_Make_Arg.
7340 for Next_Arg in 1 .. Argument_Count loop
7341 declare
7342 Argv : constant String := Argument (Next_Arg);
7343 begin
7344 if Argv'Length > 2
7345 and then Argv (1) = '-'
7346 and then Argv (2) = 'm'
7347 and then Argv /= "-margs"
7349 -- Ignore -mieee to avoid spawning an extra gcc in this case
7351 and then Argv /= "-mieee"
7352 then
7353 Arg_Index := Arg_Index + 1;
7354 Args (Arg_Index) := new String'(Argv);
7355 end if;
7356 end;
7357 end loop;
7359 pragma Assert (Arg_Index = N_M_Switch);
7361 Args (Args'Last) := new String'("-print-multi-directory");
7363 -- Call the GCC driver with the collected flags and save its
7364 -- output. Alternate design would be to link in gnatmake the
7365 -- relevant part of the GCC driver.
7367 if Saved_Gcc /= null then
7368 Multilib_Gcc := Saved_Gcc;
7369 else
7370 Multilib_Gcc := Gcc;
7371 end if;
7373 Multilib_Gcc_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Multilib_Gcc.all);
7375 Create_Temp_File (Output_FD, Output_Name);
7377 if Output_FD = Invalid_FD then
7378 return;
7379 end if;
7381 GNAT.OS_Lib.Spawn
7382 (Multilib_Gcc_Path.all, Args, Output_FD, Return_Code, False);
7383 Close (Output_FD);
7385 if Return_Code /= 0 then
7386 return;
7387 end if;
7389 -- Parse the GCC driver output which is a single line, removing CR/LF
7391 Output_FD := Open_Read (Output_Name.all, Binary);
7393 if Output_FD = Invalid_FD then
7394 return;
7395 end if;
7397 N_Read := Read (Output_FD, Line (1)'Address, Line'Length);
7398 Close (Output_FD);
7399 Delete_File (Output_Name.all, Success);
7401 for J in reverse 1 .. N_Read loop
7402 if Line (J) = ASCII.CR or else Line (J) = ASCII.LF then
7403 N_Read := N_Read - 1;
7404 else
7405 exit;
7406 end if;
7407 end loop;
7409 -- In case the standard RTS is selected do nothing
7411 if N_Read = 0 or else Line (1 .. N_Read) = "." then
7412 return;
7413 end if;
7415 -- Otherwise add -margs --RTS=output
7417 Scan_Make_Arg (Project_Node_Tree, "-margs", And_Save => True);
7418 Scan_Make_Arg
7419 (Project_Node_Tree, "--RTS=" & Line (1 .. N_Read), And_Save => True);
7420 end Process_Multilib;
7422 -----------------------------
7423 -- Recursive_Compute_Depth --
7424 -----------------------------
7426 procedure Recursive_Compute_Depth (Project : Project_Id) is
7427 use Project_Boolean_Htable;
7428 Seen : Project_Boolean_Htable.Instance := Project_Boolean_Htable.Nil;
7430 procedure Recurse (Prj : Project_Id; Depth : Natural);
7431 -- Recursive procedure that does the work, keeping track of the depth
7433 -------------
7434 -- Recurse --
7435 -------------
7437 procedure Recurse (Prj : Project_Id; Depth : Natural) is
7438 List : Project_List;
7439 Proj : Project_Id;
7441 begin
7442 if Prj.Depth >= Depth or else Get (Seen, Prj) then
7443 return;
7444 end if;
7446 -- We need a test to avoid infinite recursions with limited withs:
7447 -- If we have A -> B -> A, then when set level of A to n, we try and
7448 -- set level of B to n+1, and then level of A to n + 2, ...
7450 Set (Seen, Prj, True);
7452 Prj.Depth := Depth;
7454 -- Visit each imported project
7456 List := Prj.Imported_Projects;
7457 while List /= null loop
7458 Proj := List.Project;
7459 List := List.Next;
7460 Recurse (Prj => Proj, Depth => Depth + 1);
7461 end loop;
7463 -- We again allow changing the depth of this project later on if it
7464 -- is in fact imported by a lower-level project.
7466 Set (Seen, Prj, False);
7467 end Recurse;
7469 Proj : Project_List;
7471 -- Start of processing for Recursive_Compute_Depth
7473 begin
7474 Proj := Project_Tree.Projects;
7475 while Proj /= null loop
7476 Proj.Project.Depth := 0;
7477 Proj := Proj.Next;
7478 end loop;
7480 Recurse (Project, Depth => 1);
7481 Reset (Seen);
7482 end Recursive_Compute_Depth;
7484 -------------------------------
7485 -- Report_Compilation_Failed --
7486 -------------------------------
7488 procedure Report_Compilation_Failed is
7489 begin
7490 Delete_All_Temp_Files;
7491 Exit_Program (E_Fatal);
7492 end Report_Compilation_Failed;
7494 ------------------------
7495 -- Sigint_Intercepted --
7496 ------------------------
7498 procedure Sigint_Intercepted is
7499 SIGINT : constant := 2;
7501 begin
7502 Set_Standard_Error;
7503 Write_Line ("*** Interrupted ***");
7505 -- Send SIGINT to all outstanding compilation processes spawned
7507 for J in 1 .. Outstanding_Compiles loop
7508 Kill (Running_Compile (J).Pid, SIGINT, 1);
7509 end loop;
7511 Delete_All_Temp_Files;
7512 OS_Exit (1);
7513 -- ??? OS_Exit (1) is equivalent to Exit_Program (E_No_Compile),
7514 -- shouldn't that be Exit_Program (E_Abort) instead?
7515 end Sigint_Intercepted;
7517 -------------------
7518 -- Scan_Make_Arg --
7519 -------------------
7521 procedure Scan_Make_Arg
7522 (Project_Node_Tree : Project_Node_Tree_Ref;
7523 Argv : String;
7524 And_Save : Boolean)
7526 Success : Boolean;
7528 begin
7529 Gnatmake_Switch_Found := True;
7531 pragma Assert (Argv'First = 1);
7533 if Argv'Length = 0 then
7534 return;
7535 end if;
7537 -- If the previous switch has set the Project_File_Name_Present flag
7538 -- (that is we have seen a -P alone), then the next argument is the name
7539 -- of the project file.
7541 if Project_File_Name_Present and then Project_File_Name = null then
7542 if Argv (1) = '-' then
7543 Make_Failed ("project file name missing after -P");
7545 else
7546 Project_File_Name_Present := False;
7547 Project_File_Name := new String'(Argv);
7548 end if;
7550 -- If the previous switch has set the Output_File_Name_Present flag
7551 -- (that is we have seen a -o), then the next argument is the name of
7552 -- the output executable.
7554 elsif Output_File_Name_Present
7555 and then not Output_File_Name_Seen
7556 then
7557 Output_File_Name_Seen := True;
7559 if Argv (1) = '-' then
7560 Make_Failed ("output file name missing after -o");
7562 else
7563 Add_Switch ("-o", Linker, And_Save => And_Save);
7564 Add_Switch (Executable_Name (Argv), Linker, And_Save => And_Save);
7565 end if;
7567 -- If the previous switch has set the Object_Directory_Present flag
7568 -- (that is we have seen a -D), then the next argument is the path name
7569 -- of the object directory..
7571 elsif Object_Directory_Present
7572 and then not Object_Directory_Seen
7573 then
7574 Object_Directory_Seen := True;
7576 if Argv (1) = '-' then
7577 Make_Failed ("object directory path name missing after -D");
7579 elsif not Is_Directory (Argv) then
7580 Make_Failed ("cannot find object directory """ & Argv & """");
7582 else
7583 Add_Lib_Search_Dir (Argv);
7585 -- Specify the object directory to the binder
7587 Add_Switch ("-aO" & Argv, Binder, And_Save => And_Save);
7589 -- Record the object directory. Make sure it ends with a directory
7590 -- separator.
7592 if Argv (Argv'Last) = Directory_Separator then
7593 Object_Directory_Path := new String'(Argv);
7594 else
7595 Object_Directory_Path :=
7596 new String'(Argv & Directory_Separator);
7597 end if;
7598 end if;
7600 -- Then check if we are dealing with -cargs/-bargs/-largs/-margs
7602 elsif Argv = "-bargs"
7603 or else
7604 Argv = "-cargs"
7605 or else
7606 Argv = "-largs"
7607 or else
7608 Argv = "-margs"
7609 then
7610 case Argv (2) is
7611 when 'c' => Program_Args := Compiler;
7612 when 'b' => Program_Args := Binder;
7613 when 'l' => Program_Args := Linker;
7614 when 'm' => Program_Args := None;
7616 when others =>
7617 raise Program_Error;
7618 end case;
7620 -- A special test is needed for the -o switch within a -largs
7621 -- since that is another way to specify the name of the final
7622 -- executable.
7624 elsif Program_Args = Linker
7625 and then Argv = "-o"
7626 then
7627 Make_Failed ("switch -o not allowed within a -largs. " &
7628 "Use -o directly.");
7630 -- Check to see if we are reading switches after a -cargs,
7631 -- -bargs or -largs switch. If yes save it.
7633 elsif Program_Args /= None then
7635 -- Check to see if we are reading -I switches in order
7636 -- to take into account in the src & lib search directories.
7638 if Argv'Length > 2 and then Argv (1 .. 2) = "-I" then
7639 if Argv (3 .. Argv'Last) = "-" then
7640 Look_In_Primary_Dir := False;
7642 elsif Program_Args = Compiler then
7643 if Argv (3 .. Argv'Last) /= "-" then
7644 Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7645 end if;
7647 elsif Program_Args = Binder then
7648 Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7649 end if;
7650 end if;
7652 Add_Switch (Argv, Program_Args, And_Save => And_Save);
7654 -- Handle non-default compiler, binder, linker, and handle --RTS switch
7656 elsif Argv'Length > 2 and then Argv (1 .. 2) = "--" then
7657 if Argv'Length > 6
7658 and then Argv (1 .. 6) = "--GCC="
7659 then
7660 declare
7661 Program_Args : constant Argument_List_Access :=
7662 Argument_String_To_List
7663 (Argv (7 .. Argv'Last));
7665 begin
7666 if And_Save then
7667 Saved_Gcc := new String'(Program_Args.all (1).all);
7668 else
7669 Gcc := new String'(Program_Args.all (1).all);
7670 end if;
7672 for J in 2 .. Program_Args.all'Last loop
7673 Add_Switch
7674 (Program_Args.all (J).all,
7675 Compiler,
7676 And_Save => And_Save);
7677 end loop;
7678 end;
7680 elsif Argv'Length > 11
7681 and then Argv (1 .. 11) = "--GNATBIND="
7682 then
7683 declare
7684 Program_Args : constant Argument_List_Access :=
7685 Argument_String_To_List
7686 (Argv (12 .. Argv'Last));
7688 begin
7689 if And_Save then
7690 Saved_Gnatbind := new String'(Program_Args.all (1).all);
7691 else
7692 Gnatbind := new String'(Program_Args.all (1).all);
7693 end if;
7695 for J in 2 .. Program_Args.all'Last loop
7696 Add_Switch
7697 (Program_Args.all (J).all, Binder, And_Save => And_Save);
7698 end loop;
7699 end;
7701 elsif Argv'Length > 11
7702 and then Argv (1 .. 11) = "--GNATLINK="
7703 then
7704 declare
7705 Program_Args : constant Argument_List_Access :=
7706 Argument_String_To_List
7707 (Argv (12 .. Argv'Last));
7708 begin
7709 if And_Save then
7710 Saved_Gnatlink := new String'(Program_Args.all (1).all);
7711 else
7712 Gnatlink := new String'(Program_Args.all (1).all);
7713 end if;
7715 for J in 2 .. Program_Args.all'Last loop
7716 Add_Switch (Program_Args.all (J).all, Linker);
7717 end loop;
7718 end;
7720 elsif Argv'Length >= 5 and then
7721 Argv (1 .. 5) = "--RTS"
7722 then
7723 Add_Switch (Argv, Compiler, And_Save => And_Save);
7724 Add_Switch (Argv, Binder, And_Save => And_Save);
7726 if Argv'Length <= 6 or else Argv (6) /= '=' then
7727 Make_Failed ("missing path for --RTS");
7729 else
7730 -- Check that this is the first time we see this switch or
7731 -- if it is not the first time, the same path is specified.
7733 if RTS_Specified = null then
7734 RTS_Specified := new String'(Argv (7 .. Argv'Last));
7736 elsif RTS_Specified.all /= Argv (7 .. Argv'Last) then
7737 Make_Failed ("--RTS cannot be specified multiple times");
7738 end if;
7740 -- Valid --RTS switch
7742 No_Stdinc := True;
7743 No_Stdlib := True;
7744 RTS_Switch := True;
7746 declare
7747 Src_Path_Name : constant String_Ptr :=
7748 Get_RTS_Search_Dir
7749 (Argv (7 .. Argv'Last), Include);
7751 Lib_Path_Name : constant String_Ptr :=
7752 Get_RTS_Search_Dir
7753 (Argv (7 .. Argv'Last), Objects);
7755 begin
7756 if Src_Path_Name /= null
7757 and then Lib_Path_Name /= null
7758 then
7759 -- Set RTS_*_Path_Name variables, so that correct direct-
7760 -- ories will be set when Osint.Add_Default_Search_Dirs
7761 -- is called later.
7763 RTS_Src_Path_Name := Src_Path_Name;
7764 RTS_Lib_Path_Name := Lib_Path_Name;
7766 elsif Src_Path_Name = null
7767 and then Lib_Path_Name = null
7768 then
7769 Make_Failed ("RTS path not valid: missing " &
7770 "adainclude and adalib directories");
7772 elsif Src_Path_Name = null then
7773 Make_Failed ("RTS path not valid: missing adainclude " &
7774 "directory");
7776 elsif Lib_Path_Name = null then
7777 Make_Failed ("RTS path not valid: missing adalib " &
7778 "directory");
7779 end if;
7780 end;
7781 end if;
7783 elsif Argv'Length >= 8 and then
7784 Argv (1 .. 8) = "--param="
7785 then
7786 Add_Switch (Argv, Compiler, And_Save => And_Save);
7787 Add_Switch (Argv, Linker, And_Save => And_Save);
7789 else
7790 Scan_Make_Switches (Project_Node_Tree, Argv, Success);
7791 end if;
7793 -- If we have seen a regular switch process it
7795 elsif Argv (1) = '-' then
7796 if Argv'Length = 1 then
7797 Make_Failed ("switch character cannot be followed by a blank");
7799 -- Incorrect switches that should start with "--"
7801 elsif (Argv'Length > 5 and then Argv (1 .. 5) = "-RTS=")
7802 or else (Argv'Length > 5 and then Argv (1 .. 5) = "-GCC=")
7803 or else (Argv'Length > 8 and then Argv (1 .. 7) = "-param=")
7804 or else (Argv'Length > 10 and then Argv (1 .. 10) = "-GNATLINK=")
7805 or else (Argv'Length > 10 and then Argv (1 .. 10) = "-GNATBIND=")
7806 then
7807 Make_Failed ("option " & Argv & " should start with '--'");
7809 -- -I-
7811 elsif Argv (2 .. Argv'Last) = "I-" then
7812 Look_In_Primary_Dir := False;
7814 -- Forbid -?- or -??- where ? is any character
7816 elsif (Argv'Length = 3 and then Argv (3) = '-')
7817 or else (Argv'Length = 4 and then Argv (4) = '-')
7818 then
7819 Make_Failed
7820 ("trailing ""-"" at the end of " & Argv & " forbidden.");
7822 -- -Idir
7824 elsif Argv (2) = 'I' then
7825 Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7826 Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7827 Add_Switch (Argv, Compiler, And_Save => And_Save);
7828 Add_Switch (Argv, Binder, And_Save => And_Save);
7830 -- -aIdir (to gcc this is like a -I switch)
7832 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aI" then
7833 Add_Source_Search_Dir (Argv (4 .. Argv'Last), And_Save);
7834 Add_Switch ("-I" & Argv (4 .. Argv'Last),
7835 Compiler,
7836 And_Save => And_Save);
7837 Add_Switch (Argv, Binder, And_Save => And_Save);
7839 -- -aOdir
7841 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aO" then
7842 Add_Library_Search_Dir (Argv (4 .. Argv'Last), And_Save);
7843 Add_Switch (Argv, Binder, And_Save => And_Save);
7845 -- -aLdir (to gnatbind this is like a -aO switch)
7847 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aL" then
7848 Mark_Directory (Argv (4 .. Argv'Last), Ada_Lib_Dir, And_Save);
7849 Add_Library_Search_Dir (Argv (4 .. Argv'Last), And_Save);
7850 Add_Switch ("-aO" & Argv (4 .. Argv'Last),
7851 Binder,
7852 And_Save => And_Save);
7854 -- -aamp_target=...
7856 elsif Argv'Length >= 13 and then Argv (2 .. 13) = "aamp_target=" then
7857 Add_Switch (Argv, Compiler, And_Save => And_Save);
7859 -- Set the aamp_target environment variable so that the binder and
7860 -- linker will use the proper target library. This is consistent
7861 -- with how things work when -aamp_target is passed on the command
7862 -- line to gnaampmake.
7864 Setenv ("aamp_target", Argv (14 .. Argv'Last));
7866 -- -Adir (to gnatbind this is like a -aO switch, to gcc like a -I)
7868 elsif Argv (2) = 'A' then
7869 Mark_Directory (Argv (3 .. Argv'Last), Ada_Lib_Dir, And_Save);
7870 Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7871 Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7872 Add_Switch ("-I" & Argv (3 .. Argv'Last),
7873 Compiler,
7874 And_Save => And_Save);
7875 Add_Switch ("-aO" & Argv (3 .. Argv'Last),
7876 Binder,
7877 And_Save => And_Save);
7879 -- -Ldir
7881 elsif Argv (2) = 'L' then
7882 Add_Switch (Argv, Linker, And_Save => And_Save);
7884 -- For -gxxxxx, -pg, -mxxx, -fxxx: give the switch to both the
7885 -- compiler and the linker (except for -gnatxxx which is only for
7886 -- the compiler). Some of the -mxxx (for example -m64) and -fxxx
7887 -- (for example -ftest-coverage for gcov) need to be used when
7888 -- compiling the binder generated files, and using all these gcc
7889 -- switches for the binder generated files should not be a problem.
7891 elsif
7892 (Argv (2) = 'g' and then (Argv'Last < 5
7893 or else Argv (2 .. 5) /= "gnat"))
7894 or else Argv (2 .. Argv'Last) = "pg"
7895 or else (Argv (2) = 'm' and then Argv'Last > 2)
7896 or else (Argv (2) = 'f' and then Argv'Last > 2)
7897 then
7898 Add_Switch (Argv, Compiler, And_Save => And_Save);
7899 Add_Switch (Argv, Linker, And_Save => And_Save);
7901 -- The following condition has to be kept synchronized with
7902 -- the Process_Multilib one.
7904 if Argv (2) = 'm'
7905 and then Argv /= "-mieee"
7906 then
7907 N_M_Switch := N_M_Switch + 1;
7908 end if;
7910 -- -C=<mapping file>
7912 elsif Argv'Last > 2 and then Argv (2) = 'C' then
7913 if And_Save then
7914 if Argv (3) /= '=' or else Argv'Last <= 3 then
7915 Make_Failed ("illegal switch " & Argv);
7916 end if;
7918 Gnatmake_Mapping_File := new String'(Argv (4 .. Argv'Last));
7919 end if;
7921 -- -D
7923 elsif Argv'Last = 2 and then Argv (2) = 'D' then
7924 if Project_File_Name /= null then
7925 Make_Failed ("-D cannot be used in conjunction with a " &
7926 "project file");
7928 else
7929 Scan_Make_Switches (Project_Node_Tree, Argv, Success);
7930 end if;
7932 -- -d
7934 elsif Argv (2) = 'd'
7935 and then Argv'Last = 2
7936 then
7937 Display_Compilation_Progress := True;
7939 -- -i
7941 elsif Argv'Last = 2 and then Argv (2) = 'i' then
7942 if Project_File_Name /= null then
7943 Make_Failed ("-i cannot be used in conjunction with a " &
7944 "project file");
7945 else
7946 Scan_Make_Switches (Project_Node_Tree, Argv, Success);
7947 end if;
7949 -- -j (need to save the result)
7951 elsif Argv (2) = 'j' then
7952 Scan_Make_Switches (Project_Node_Tree, Argv, Success);
7954 if And_Save then
7955 Saved_Maximum_Processes := Maximum_Processes;
7956 end if;
7958 -- -m
7960 elsif Argv (2) = 'm'
7961 and then Argv'Last = 2
7962 then
7963 Minimal_Recompilation := True;
7965 -- -u
7967 elsif Argv (2) = 'u'
7968 and then Argv'Last = 2
7969 then
7970 Unique_Compile := True;
7971 Compile_Only := True;
7972 Do_Bind_Step := False;
7973 Do_Link_Step := False;
7975 -- -U
7977 elsif Argv (2) = 'U'
7978 and then Argv'Last = 2
7979 then
7980 Unique_Compile_All_Projects := True;
7981 Unique_Compile := True;
7982 Compile_Only := True;
7983 Do_Bind_Step := False;
7984 Do_Link_Step := False;
7986 -- -Pprj or -P prj (only once, and only on the command line)
7988 elsif Argv (2) = 'P' then
7989 if Project_File_Name /= null then
7990 Make_Failed ("cannot have several project files specified");
7992 elsif Object_Directory_Path /= null then
7993 Make_Failed ("-D cannot be used in conjunction with a " &
7994 "project file");
7996 elsif In_Place_Mode then
7997 Make_Failed ("-i cannot be used in conjunction with a " &
7998 "project file");
8000 elsif not And_Save then
8002 -- It could be a tool other than gnatmake (i.e, gnatdist)
8003 -- or a -P switch inside a project file.
8005 Fail
8006 ("either the tool is not ""project-aware"" or " &
8007 "a project file is specified inside a project file");
8009 elsif Argv'Last = 2 then
8011 -- -P is used alone: the project file name is the next option
8013 Project_File_Name_Present := True;
8015 else
8016 Project_File_Name := new String'(Argv (3 .. Argv'Last));
8017 end if;
8019 -- -vPx (verbosity of the parsing of the project files)
8021 elsif Argv'Last = 4
8022 and then Argv (2 .. 3) = "vP"
8023 and then Argv (4) in '0' .. '2'
8024 then
8025 if And_Save then
8026 case Argv (4) is
8027 when '0' =>
8028 Current_Verbosity := Prj.Default;
8029 when '1' =>
8030 Current_Verbosity := Prj.Medium;
8031 when '2' =>
8032 Current_Verbosity := Prj.High;
8033 when others =>
8034 null;
8035 end case;
8036 end if;
8038 -- -Xext=val (External assignment)
8040 elsif Argv (2) = 'X'
8041 and then Is_External_Assignment (Project_Node_Tree, Argv)
8042 then
8043 -- Is_External_Assignment has side effects
8044 -- when it returns True;
8046 null;
8048 -- If -gnath is present, then generate the usage information
8049 -- right now and do not pass this option on to the compiler calls.
8051 elsif Argv = "-gnath" then
8052 Usage;
8054 -- If -gnatc is specified, make sure the bind step and the link
8055 -- step are not executed.
8057 elsif Argv'Length >= 6 and then Argv (2 .. 6) = "gnatc" then
8059 -- If -gnatc is specified, make sure the bind step and the link
8060 -- step are not executed.
8062 Add_Switch (Argv, Compiler, And_Save => And_Save);
8063 Operating_Mode := Check_Semantics;
8064 Check_Object_Consistency := False;
8065 Compile_Only := True;
8066 Do_Bind_Step := False;
8067 Do_Link_Step := False;
8069 elsif Argv (2 .. Argv'Last) = "nostdlib" then
8071 -- Don't pass -nostdlib to gnatlink, it will disable
8072 -- linking with all standard library files.
8074 No_Stdlib := True;
8076 Add_Switch (Argv, Compiler, And_Save => And_Save);
8077 Add_Switch (Argv, Binder, And_Save => And_Save);
8079 elsif Argv (2 .. Argv'Last) = "nostdinc" then
8081 -- Pass -nostdinc to the Compiler and to gnatbind
8083 No_Stdinc := True;
8084 Add_Switch (Argv, Compiler, And_Save => And_Save);
8085 Add_Switch (Argv, Binder, And_Save => And_Save);
8087 -- All other switches are processed by Scan_Make_Switches. If the
8088 -- call returns with Gnatmake_Switch_Found = False, then the switch
8089 -- is passed to the compiler.
8091 else
8092 Scan_Make_Switches
8093 (Project_Node_Tree, Argv, Gnatmake_Switch_Found);
8095 if not Gnatmake_Switch_Found then
8096 Add_Switch (Argv, Compiler, And_Save => And_Save);
8097 end if;
8098 end if;
8100 -- If not a switch it must be a file name
8102 else
8103 Add_File (Argv);
8104 Mains.Add_Main (Argv);
8105 end if;
8106 end Scan_Make_Arg;
8108 -----------------
8109 -- Switches_Of --
8110 -----------------
8112 function Switches_Of
8113 (Source_File : File_Name_Type;
8114 Source_File_Name : String;
8115 Source_Index : Int;
8116 Project : Project_Id;
8117 In_Package : Package_Id;
8118 Allow_ALI : Boolean) return Variable_Value
8120 Lang : constant Language_Ptr := Get_Language_From_Name (Project, "ada");
8122 Switches : Variable_Value;
8124 Defaults : constant Array_Element_Id :=
8125 Prj.Util.Value_Of
8126 (Name => Name_Default_Switches,
8127 In_Arrays =>
8128 Project_Tree.Packages.Table
8129 (In_Package).Decl.Arrays,
8130 In_Tree => Project_Tree);
8132 Switches_Array : constant Array_Element_Id :=
8133 Prj.Util.Value_Of
8134 (Name => Name_Switches,
8135 In_Arrays =>
8136 Project_Tree.Packages.Table
8137 (In_Package).Decl.Arrays,
8138 In_Tree => Project_Tree);
8140 begin
8141 -- First, try Switches (<file name>)
8143 Switches :=
8144 Prj.Util.Value_Of
8145 (Index => Name_Id (Source_File),
8146 Src_Index => Source_Index,
8147 In_Array => Switches_Array,
8148 In_Tree => Project_Tree);
8150 -- Check also without the suffix
8152 if Switches = Nil_Variable_Value
8153 and then Lang /= null
8154 then
8155 declare
8156 Naming : Lang_Naming_Data renames Lang.Config.Naming_Data;
8157 Name : String (1 .. Source_File_Name'Length + 3);
8158 Last : Positive := Source_File_Name'Length;
8159 Spec_Suffix : constant String :=
8160 Get_Name_String (Naming.Spec_Suffix);
8161 Body_Suffix : constant String :=
8162 Get_Name_String (Naming.Body_Suffix);
8163 Truncated : Boolean := False;
8165 begin
8166 Name (1 .. Last) := Source_File_Name;
8168 if Last > Body_Suffix'Length
8169 and then Name (Last - Body_Suffix'Length + 1 .. Last) =
8170 Body_Suffix
8171 then
8172 Truncated := True;
8173 Last := Last - Body_Suffix'Length;
8174 end if;
8176 if not Truncated
8177 and then Last > Spec_Suffix'Length
8178 and then Name (Last - Spec_Suffix'Length + 1 .. Last) =
8179 Spec_Suffix
8180 then
8181 Truncated := True;
8182 Last := Last - Spec_Suffix'Length;
8183 end if;
8185 if Truncated then
8186 Name_Len := 0;
8187 Add_Str_To_Name_Buffer (Name (1 .. Last));
8188 Switches :=
8189 Prj.Util.Value_Of
8190 (Index => Name_Find,
8191 Src_Index => 0,
8192 In_Array => Switches_Array,
8193 In_Tree => Project_Tree);
8195 if Switches = Nil_Variable_Value and then Allow_ALI then
8196 Last := Source_File_Name'Length;
8198 while Name (Last) /= '.' loop
8199 Last := Last - 1;
8200 end loop;
8202 Name_Len := 0;
8203 Add_Str_To_Name_Buffer (Name (1 .. Last));
8204 Add_Str_To_Name_Buffer ("ali");
8206 Switches :=
8207 Prj.Util.Value_Of
8208 (Index => Name_Find,
8209 Src_Index => 0,
8210 In_Array => Switches_Array,
8211 In_Tree => Project_Tree);
8212 end if;
8213 end if;
8214 end;
8215 end if;
8217 -- Next, try Switches ("Ada")
8219 if Switches = Nil_Variable_Value then
8220 Switches :=
8221 Prj.Util.Value_Of
8222 (Index => Name_Ada,
8223 Src_Index => 0,
8224 In_Array => Switches_Array,
8225 In_Tree => Project_Tree,
8226 Force_Lower_Case_Index => True);
8228 if Switches /= Nil_Variable_Value then
8229 Switch_May_Be_Passed_To_The_Compiler := False;
8230 end if;
8231 end if;
8233 -- Next, try Switches (others)
8235 if Switches = Nil_Variable_Value then
8236 Switches :=
8237 Prj.Util.Value_Of
8238 (Index => All_Other_Names,
8239 Src_Index => 0,
8240 In_Array => Switches_Array,
8241 In_Tree => Project_Tree);
8243 if Switches /= Nil_Variable_Value then
8244 Switch_May_Be_Passed_To_The_Compiler := False;
8245 end if;
8246 end if;
8248 -- And finally, Default_Switches ("Ada")
8250 if Switches = Nil_Variable_Value then
8251 Switches :=
8252 Prj.Util.Value_Of
8253 (Index => Name_Ada,
8254 Src_Index => 0,
8255 In_Array => Defaults,
8256 In_Tree => Project_Tree);
8257 end if;
8259 return Switches;
8260 end Switches_Of;
8262 -----------
8263 -- Usage --
8264 -----------
8266 procedure Usage is
8267 begin
8268 if Usage_Needed then
8269 Usage_Needed := False;
8270 Makeusg;
8271 end if;
8272 end Usage;
8274 begin
8275 -- Make sure that in case of failure, the temp files will be deleted
8277 Prj.Com.Fail := Make_Failed'Access;
8278 MLib.Fail := Make_Failed'Access;
8279 Makeutl.Do_Fail := Make_Failed'Access;
8280 end Make;