2014-03-25 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / ada / make.adb
blobe8acb4e604a69212dacdea879776ec3bbd74ad67
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- M A K E --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2013, 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 Sdefault;
55 with SFN_Scan;
56 with Sinput.P;
57 with Snames; use Snames;
58 with Stringt;
60 pragma Warnings (Off);
61 with System.HTable;
62 pragma Warnings (On);
64 with Switch; use Switch;
65 with Switch.M; use Switch.M;
66 with Table;
67 with Targparm; use Targparm;
68 with Tempdir;
69 with Types; use Types;
71 with Ada.Command_Line; use Ada.Command_Line;
72 with Ada.Directories;
73 with Ada.Exceptions; use Ada.Exceptions;
75 with GNAT.Case_Util; use GNAT.Case_Util;
76 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
77 with GNAT.Dynamic_HTables; use GNAT.Dynamic_HTables;
78 with GNAT.OS_Lib; use GNAT.OS_Lib;
80 package body Make is
82 use ASCII;
83 -- Make control characters visible
85 Standard_Library_Package_Body_Name : constant String := "s-stalib.adb";
86 -- Every program depends on this package, that must then be checked,
87 -- especially when -f and -a are used.
89 procedure Kill (Pid : Process_Id; Sig_Num : Integer; Close : Integer);
90 pragma Import (C, Kill, "__gnat_kill");
91 -- Called by Sigint_Intercepted to kill all spawned compilation processes
93 type Sigint_Handler is access procedure;
94 pragma Convention (C, Sigint_Handler);
96 procedure Install_Int_Handler (Handler : Sigint_Handler);
97 pragma Import (C, Install_Int_Handler, "__gnat_install_int_handler");
98 -- Called by Gnatmake to install the SIGINT handler below
100 procedure Sigint_Intercepted;
101 pragma Convention (C, Sigint_Intercepted);
102 -- Called when the program is interrupted by Ctrl-C to delete the
103 -- temporary mapping files and configuration pragmas files.
105 No_Mapping_File : constant Natural := 0;
107 type Compilation_Data is record
108 Pid : Process_Id;
109 Full_Source_File : File_Name_Type;
110 Lib_File : File_Name_Type;
111 Source_Unit : Unit_Name_Type;
112 Full_Lib_File : File_Name_Type;
113 Lib_File_Attr : aliased File_Attributes;
114 Mapping_File : Natural := No_Mapping_File;
115 Project : Project_Id := No_Project;
116 end record;
117 -- Data recorded for each compilation process spawned
119 No_Compilation_Data : constant Compilation_Data :=
120 (Invalid_Pid, No_File, No_File, No_Unit_Name, No_File, Unknown_Attributes,
121 No_Mapping_File, No_Project);
123 type Comp_Data_Arr is array (Positive range <>) of Compilation_Data;
124 type Comp_Data_Ptr is access Comp_Data_Arr;
125 Running_Compile : Comp_Data_Ptr;
126 -- Used to save information about outstanding compilations
128 Outstanding_Compiles : Natural := 0;
129 -- Current number of outstanding compiles
131 -------------------------
132 -- Note on terminology --
133 -------------------------
135 -- In this program, we use the phrase "termination" of a file name to refer
136 -- to the suffix that appears after the unit name portion. Very often this
137 -- is simply the extension, but in some cases, the sequence may be more
138 -- complex, for example in main.1.ada, the termination in this name is
139 -- ".1.ada" and in main_.ada the termination is "_.ada".
141 procedure Insert_Project_Sources
142 (The_Project : Project_Id;
143 All_Projects : Boolean;
144 Into_Q : Boolean);
145 -- If Into_Q is True, insert all sources of the project file(s) that are
146 -- not already marked into the Q. If Into_Q is False, call Osint.Add_File
147 -- for the first source, then insert all other sources that are not already
148 -- marked into the Q. If All_Projects is True, all sources of all projects
149 -- are concerned; otherwise, only sources of The_Project are concerned,
150 -- including, if The_Project is an extending project, sources inherited
151 -- from projects being extended.
153 Unique_Compile : Boolean := False;
154 -- Set to True if -u or -U or a project file with no main is used
156 Unique_Compile_All_Projects : Boolean := False;
157 -- Set to True if -U is used
159 Must_Compile : Boolean := False;
160 -- True if gnatmake is invoked with -f -u and one or several mains on the
161 -- command line.
163 Project_Tree : constant Project_Tree_Ref :=
164 new Project_Tree_Data (Is_Root_Tree => True);
165 -- The project tree
167 Main_On_Command_Line : Boolean := False;
168 -- True if gnatmake is invoked with one or several mains on the command
169 -- line.
171 RTS_Specified : String_Access := null;
172 -- Used to detect multiple --RTS= switches
174 N_M_Switch : Natural := 0;
175 -- Used to count -mxxx switches that can affect multilib
177 -- The 3 following packages are used to store gcc, gnatbind and gnatlink
178 -- switches found in the project files.
180 package Gcc_Switches is new Table.Table (
181 Table_Component_Type => String_Access,
182 Table_Index_Type => Integer,
183 Table_Low_Bound => 1,
184 Table_Initial => 20,
185 Table_Increment => 100,
186 Table_Name => "Make.Gcc_Switches");
188 package Binder_Switches is new Table.Table (
189 Table_Component_Type => String_Access,
190 Table_Index_Type => Integer,
191 Table_Low_Bound => 1,
192 Table_Initial => 20,
193 Table_Increment => 100,
194 Table_Name => "Make.Binder_Switches");
196 package Linker_Switches is new Table.Table (
197 Table_Component_Type => String_Access,
198 Table_Index_Type => Integer,
199 Table_Low_Bound => 1,
200 Table_Initial => 20,
201 Table_Increment => 100,
202 Table_Name => "Make.Linker_Switches");
204 -- The following instantiations and variables are necessary to save what
205 -- is found on the command line, in case there is a project file specified.
207 package Saved_Gcc_Switches is new Table.Table (
208 Table_Component_Type => String_Access,
209 Table_Index_Type => Integer,
210 Table_Low_Bound => 1,
211 Table_Initial => 20,
212 Table_Increment => 100,
213 Table_Name => "Make.Saved_Gcc_Switches");
215 package Saved_Binder_Switches is new Table.Table (
216 Table_Component_Type => String_Access,
217 Table_Index_Type => Integer,
218 Table_Low_Bound => 1,
219 Table_Initial => 20,
220 Table_Increment => 100,
221 Table_Name => "Make.Saved_Binder_Switches");
223 package Saved_Linker_Switches is new Table.Table
224 (Table_Component_Type => String_Access,
225 Table_Index_Type => Integer,
226 Table_Low_Bound => 1,
227 Table_Initial => 20,
228 Table_Increment => 100,
229 Table_Name => "Make.Saved_Linker_Switches");
231 package Switches_To_Check is new Table.Table (
232 Table_Component_Type => String_Access,
233 Table_Index_Type => Integer,
234 Table_Low_Bound => 1,
235 Table_Initial => 20,
236 Table_Increment => 100,
237 Table_Name => "Make.Switches_To_Check");
239 package Library_Paths is new Table.Table (
240 Table_Component_Type => String_Access,
241 Table_Index_Type => Integer,
242 Table_Low_Bound => 1,
243 Table_Initial => 20,
244 Table_Increment => 100,
245 Table_Name => "Make.Library_Paths");
247 package Failed_Links is new Table.Table (
248 Table_Component_Type => File_Name_Type,
249 Table_Index_Type => Integer,
250 Table_Low_Bound => 1,
251 Table_Initial => 10,
252 Table_Increment => 100,
253 Table_Name => "Make.Failed_Links");
255 package Successful_Links is new Table.Table (
256 Table_Component_Type => File_Name_Type,
257 Table_Index_Type => Integer,
258 Table_Low_Bound => 1,
259 Table_Initial => 10,
260 Table_Increment => 100,
261 Table_Name => "Make.Successful_Links");
263 package Library_Projs is new Table.Table (
264 Table_Component_Type => Project_Id,
265 Table_Index_Type => Integer,
266 Table_Low_Bound => 1,
267 Table_Initial => 10,
268 Table_Increment => 100,
269 Table_Name => "Make.Library_Projs");
271 -- Two variables to keep the last binder and linker switch index in tables
272 -- Binder_Switches and Linker_Switches, before adding switches from the
273 -- project file (if any) and switches from the command line (if any).
275 Last_Binder_Switch : Integer := 0;
276 Last_Linker_Switch : Integer := 0;
278 Normalized_Switches : Argument_List_Access := new Argument_List (1 .. 10);
279 Last_Norm_Switch : Natural := 0;
281 Saved_Maximum_Processes : Natural := 0;
283 Gnatmake_Switch_Found : Boolean;
284 -- Set by Scan_Make_Arg. True when the switch is a gnatmake switch.
285 -- Tested by Add_Switches when switches in package Builder must all be
286 -- gnatmake switches.
288 Switch_May_Be_Passed_To_The_Compiler : Boolean;
289 -- Set by Add_Switches and Switches_Of. True when unrecognized switches
290 -- are passed to the Ada compiler.
292 type Arg_List_Ref is access Argument_List;
293 The_Saved_Gcc_Switches : Arg_List_Ref;
295 Project_File_Name : String_Access := null;
296 -- The path name of the main project file, if any
298 Project_File_Name_Present : Boolean := False;
299 -- True when -P is used with a space between -P and the project file name
301 Current_Verbosity : Prj.Verbosity := Prj.Default;
302 -- Verbosity to parse the project files
304 Main_Project : Prj.Project_Id := No_Project;
305 -- The project id of the main project file, if any
307 Project_Of_Current_Object_Directory : Project_Id := No_Project;
308 -- The object directory of the project for the last compilation. Avoid
309 -- calling Change_Dir if the current working directory is already this
310 -- directory.
312 Map_File : String_Access := null;
313 -- Value of switch --create-map-file
315 -- Packages of project files where unknown attributes are errors
317 Naming_String : aliased String := "naming";
318 Builder_String : aliased String := "builder";
319 Compiler_String : aliased String := "compiler";
320 Binder_String : aliased String := "binder";
321 Linker_String : aliased String := "linker";
323 Gnatmake_Packages : aliased String_List :=
324 (Naming_String 'Access,
325 Builder_String 'Access,
326 Compiler_String 'Access,
327 Binder_String 'Access,
328 Linker_String 'Access);
330 Packages_To_Check_By_Gnatmake : constant String_List_Access :=
331 Gnatmake_Packages'Access;
333 procedure Add_Library_Search_Dir
334 (Path : String;
335 On_Command_Line : Boolean);
336 -- Call Add_Lib_Search_Dir with an absolute directory path. If Path is
337 -- relative path, when On_Command_Line is True, it is relative to the
338 -- current working directory. When On_Command_Line is False, it is relative
339 -- to the project directory of the main project.
341 procedure Add_Source_Search_Dir
342 (Path : String;
343 On_Command_Line : Boolean);
344 -- Call Add_Src_Search_Dir with an absolute directory path. If Path is a
345 -- relative path, when On_Command_Line is True, it is relative to the
346 -- current working directory. When On_Command_Line is False, it is relative
347 -- to the project directory of the main project.
349 procedure Add_Source_Dir (N : String);
350 -- Call Add_Src_Search_Dir (output one line when in verbose mode)
352 procedure Add_Source_Directories is
353 new Prj.Env.For_All_Source_Dirs (Action => Add_Source_Dir);
355 procedure Add_Object_Dir (N : String);
356 -- Call Add_Lib_Search_Dir (output one line when in verbose mode)
358 procedure Add_Object_Directories is
359 new Prj.Env.For_All_Object_Dirs (Action => Add_Object_Dir);
361 procedure Change_To_Object_Directory (Project : Project_Id);
362 -- Change to the object directory of project Project, if this is not
363 -- already the current working directory.
365 type Bad_Compilation_Info is record
366 File : File_Name_Type;
367 Unit : Unit_Name_Type;
368 Found : Boolean;
369 end record;
370 -- File is the name of the file for which a compilation failed. Unit is for
371 -- gnatdist use in order to easily get the unit name of a file when its
372 -- name is krunched or declared in gnat.adc. Found is False if the
373 -- compilation failed because the file could not be found.
375 package Bad_Compilation is new Table.Table (
376 Table_Component_Type => Bad_Compilation_Info,
377 Table_Index_Type => Natural,
378 Table_Low_Bound => 1,
379 Table_Initial => 20,
380 Table_Increment => 100,
381 Table_Name => "Make.Bad_Compilation");
382 -- Full name of all the source files for which compilation fails
384 Do_Compile_Step : Boolean := True;
385 Do_Bind_Step : Boolean := True;
386 Do_Link_Step : Boolean := True;
387 -- Flags to indicate what step should be executed. Can be set to False
388 -- with the switches -c, -b and -l. These flags are reset to True for
389 -- each invocation of procedure Gnatmake.
391 Shared_String : aliased String := "-shared";
392 Force_Elab_Flags_String : aliased String := "-F";
393 CodePeer_Mode_String : aliased String := "-P";
395 No_Shared_Switch : aliased Argument_List := (1 .. 0 => null);
396 Shared_Switch : aliased Argument_List := (1 => Shared_String'Access);
397 Bind_Shared : Argument_List_Access := No_Shared_Switch'Access;
398 -- Switch to added in front of gnatbind switches. By default no switch is
399 -- added. Switch "-shared" is added if there is a non-static Library
400 -- Project File.
402 Shared_Libgcc : aliased String := "-shared-libgcc";
404 No_Shared_Libgcc_Switch : aliased Argument_List := (1 .. 0 => null);
405 Shared_Libgcc_Switch : aliased Argument_List :=
406 (1 => Shared_Libgcc'Access);
407 Link_With_Shared_Libgcc : Argument_List_Access :=
408 No_Shared_Libgcc_Switch'Access;
410 procedure Make_Failed (S : String);
411 -- Delete all temp files created by Gnatmake and call Osint.Fail, with the
412 -- parameter S (see osint.ads). This is called from the Prj hierarchy and
413 -- the MLib hierarchy. This subprogram also prints current error messages
414 -- (i.e. finalizes Errutil).
416 --------------------------
417 -- Obsolete Executables --
418 --------------------------
420 Executable_Obsolete : Boolean := False;
421 -- Executable_Obsolete is initially set to False for each executable,
422 -- and is set to True whenever one of the source of the executable is
423 -- compiled, or has already been compiled for another executable.
425 Max_Header : constant := 200;
426 -- This needs a proper comment, it used to say "arbitrary" that's not an
427 -- adequate comment ???
429 type Header_Num is range 1 .. Max_Header;
430 -- Header_Num for the hash table Obsoleted below
432 function Hash (F : File_Name_Type) return Header_Num;
433 -- Hash function for the hash table Obsoleted below
435 package Obsoleted is new System.HTable.Simple_HTable
436 (Header_Num => Header_Num,
437 Element => Boolean,
438 No_Element => False,
439 Key => File_Name_Type,
440 Hash => Hash,
441 Equal => "=");
442 -- A hash table to keep all files that have been compiled, to detect
443 -- if an executable is up to date or not.
445 procedure Enter_Into_Obsoleted (F : File_Name_Type);
446 -- Enter a file name, without directory information, into the hash table
447 -- Obsoleted.
449 function Is_In_Obsoleted (F : File_Name_Type) return Boolean;
450 -- Check if a file name, without directory information, has already been
451 -- entered into the hash table Obsoleted.
453 type Dependency is record
454 This : File_Name_Type;
455 Depends_On : File_Name_Type;
456 end record;
457 -- Components of table Dependencies below
459 package Dependencies is new Table.Table (
460 Table_Component_Type => Dependency,
461 Table_Index_Type => Integer,
462 Table_Low_Bound => 1,
463 Table_Initial => 20,
464 Table_Increment => 100,
465 Table_Name => "Make.Dependencies");
466 -- A table to keep dependencies, to be able to decide if an executable
467 -- is obsolete. More explanation needed ???
469 ----------------------------
470 -- Arguments and Switches --
471 ----------------------------
473 Arguments : Argument_List_Access;
474 -- Used to gather the arguments for invocation of the compiler
476 Last_Argument : Natural := 0;
477 -- Last index of arguments in Arguments above
479 Arguments_Project : Project_Id;
480 -- Project id, if any, of the source to be compiled
482 Arguments_Path_Name : Path_Name_Type;
483 -- Full path of the source to be compiled, when Arguments_Project is not
484 -- No_Project.
486 Dummy_Switch : constant String_Access := new String'("- ");
487 -- Used to initialized Prev_Switch in procedure Check
489 procedure Add_Arguments (Args : Argument_List);
490 -- Add arguments to global variable Arguments, increasing its size
491 -- if necessary and adjusting Last_Argument.
493 function Configuration_Pragmas_Switch
494 (For_Project : Project_Id) return Argument_List;
495 -- Return an argument list of one element, if there is a configuration
496 -- pragmas file to be specified for For_Project,
497 -- otherwise return an empty argument list.
499 -------------------
500 -- Misc Routines --
501 -------------------
503 procedure List_Depend;
504 -- Prints to standard output the list of object dependencies. This list
505 -- can be used directly in a Makefile. A call to Compile_Sources must
506 -- precede the call to List_Depend. Also because this routine uses the
507 -- ALI files that were originally loaded and scanned by Compile_Sources,
508 -- no additional ALI files should be scanned between the two calls (i.e.
509 -- between the call to Compile_Sources and List_Depend.)
511 procedure List_Bad_Compilations;
512 -- Prints out the list of all files for which the compilation failed
514 Usage_Needed : Boolean := True;
515 -- Flag used to make sure Makeusg is call at most once
517 procedure Usage;
518 -- Call Makeusg, if Usage_Needed is True.
519 -- Set Usage_Needed to False.
521 procedure Debug_Msg (S : String; N : Name_Id);
522 procedure Debug_Msg (S : String; N : File_Name_Type);
523 procedure Debug_Msg (S : String; N : Unit_Name_Type);
524 -- If Debug.Debug_Flag_W is set outputs string S followed by name N
526 procedure Recursive_Compute_Depth (Project : Project_Id);
527 -- Compute depth of Project and of the projects it depends on
529 -----------------------
530 -- Gnatmake Routines --
531 -----------------------
533 subtype Lib_Mark_Type is Byte;
534 -- Used in Mark_Directory
536 Ada_Lib_Dir : constant Lib_Mark_Type := 1;
537 -- Used to mark a directory as a GNAT lib dir
539 -- Note that the notion of GNAT lib dir is no longer used. The code related
540 -- to it has not been removed to give an idea on how to use the directory
541 -- prefix marking mechanism.
543 -- An Ada library directory is a directory containing ali and object files
544 -- but no source files for the bodies (the specs can be in the same or some
545 -- other directory). These directories are specified in the Gnatmake
546 -- command line with the switch "-Adir" (to specify the spec location -Idir
547 -- cab be used). Gnatmake skips the missing sources whose ali are in Ada
548 -- library directories. For an explanation of why Gnatmake behaves that
549 -- way, see the spec of Make.Compile_Sources. The directory lookup penalty
550 -- is incurred every single time this routine is called.
552 procedure Check_Steps;
553 -- Check what steps (Compile, Bind, Link) must be executed.
554 -- Set the step flags accordingly.
556 function In_Ada_Lib_Dir (File : File_Name_Type) return Boolean;
557 -- Get directory prefix of this file and get lib mark stored in name
558 -- table for this directory. Then check if an Ada lib mark has been set.
560 procedure Mark_Directory
561 (Dir : String;
562 Mark : Lib_Mark_Type;
563 On_Command_Line : Boolean);
564 -- Store the absolute path from Dir in name table and set lib mark as name
565 -- info to identify Ada libraries.
567 -- If Dir is a relative path, when On_Command_Line is True, it is relative
568 -- to the current working directory; when On_Command_Line is False, it is
569 -- relative to the project directory of the main project.
571 Output_Is_Object : Boolean := True;
572 -- Set to False when using a switch -S for the compiler
574 procedure Check_For_S_Switch;
575 -- Set Output_Is_Object to False when the -S switch is used for the
576 -- compiler.
578 function Switches_Of
579 (Source_File : File_Name_Type;
580 Project : Project_Id;
581 In_Package : Package_Id;
582 Allow_ALI : Boolean) return Variable_Value;
583 -- Return the switches for the source file in the specified package of a
584 -- project file. If the Source_File ends with a standard GNAT extension
585 -- (".ads" or ".adb"), try first the full name, then the name without the
586 -- extension, then, if Allow_ALI is True, the name with the extension
587 -- ".ali". If there is no switches for either names, try first Switches
588 -- (others) then the default switches for Ada. If all failed, return
589 -- No_Variable_Value.
591 function Is_In_Object_Directory
592 (Source_File : File_Name_Type;
593 Full_Lib_File : File_Name_Type) return Boolean;
594 -- Check if, when using a project file, the ALI file is in the project
595 -- directory of the ultimate extending project. If it is not, we ignore
596 -- the fact that this ALI file is read-only.
598 procedure Process_Multilib (Env : in out Prj.Tree.Environment);
599 -- Add appropriate --RTS argument to handle multilib
601 procedure Resolve_Relative_Names_In_Switches (Current_Work_Dir : String);
602 -- Resolve all relative paths found in the linker and binder switches,
603 -- when using project files.
605 procedure Queue_Library_Project_Sources;
606 -- For all library project, if the library file does not exist, put all the
607 -- project sources in the queue, and flag the project so that the library
608 -- is generated.
610 procedure Compute_Switches_For_Main
611 (Main_Source_File : in out File_Name_Type;
612 Root_Environment : in out Prj.Tree.Environment;
613 Compute_Builder : Boolean;
614 Current_Work_Dir : String);
615 -- Find compiler, binder and linker switches to use for the given main
617 procedure Compute_Executable
618 (Main_Source_File : File_Name_Type;
619 Executable : out File_Name_Type;
620 Non_Std_Executable : out Boolean);
621 -- Parse the linker switches and project file to compute the name of the
622 -- executable to generate.
623 -- ??? What is the meaning of Non_Std_Executable
625 procedure Compilation_Phase
626 (Main_Source_File : File_Name_Type;
627 Current_Main_Index : Int := 0;
628 Total_Compilation_Failures : in out Natural;
629 Stand_Alone_Libraries : in out Boolean;
630 Executable : File_Name_Type := No_File;
631 Is_Last_Main : Boolean;
632 Stop_Compile : out Boolean);
633 -- Build all source files for a given main file
635 -- Current_Main_Index, if not zero, is the index of the current main unit
636 -- in its source file.
638 -- Stand_Alone_Libraries is set to True when there are Stand-Alone
639 -- Libraries, so that gnatbind is invoked with the -F switch to force
640 -- checking of elaboration flags.
642 -- Stop_Compile is set to true if we should not try to compile any more
643 -- of the main units
645 procedure Binding_Phase
646 (Stand_Alone_Libraries : Boolean := False;
647 Main_ALI_File : File_Name_Type);
648 -- Stand_Alone_Libraries should be set to True when there are Stand-Alone
649 -- Libraries, so that gnatbind is invoked with the -F switch to force
650 -- checking of elaboration flags.
652 procedure Library_Phase
653 (Stand_Alone_Libraries : in out Boolean;
654 Library_Rebuilt : in out Boolean);
655 -- Build libraries.
656 -- Stand_Alone_Libraries is set to True when there are Stand-Alone
657 -- Libraries, so that gnatbind is invoked with the -F switch to force
658 -- checking of elaboration flags.
660 procedure Linking_Phase
661 (Non_Std_Executable : Boolean := False;
662 Executable : File_Name_Type := No_File;
663 Main_ALI_File : File_Name_Type);
664 -- Perform the link of a single executable. The ali file corresponds
665 -- to Main_ALI_File. Executable is the file name of an executable.
666 -- Non_Std_Executable is set to True when there is a possibility that
667 -- the linker will not choose the correct executable file name.
669 ----------------------------------------------------
670 -- Compiler, Binder & Linker Data and Subprograms --
671 ----------------------------------------------------
673 Gcc : String_Access := Program_Name ("gcc", "gnatmake");
674 Original_Gcc : constant String_Access := Gcc;
675 -- Original_Gcc is used to check if Gcc has been modified by a switch
676 -- --GCC=, so that for VM platforms, it is not modified again, as it can
677 -- result in incorrect error messages if the compiler cannot be found.
679 Gnatbind : String_Access := Program_Name ("gnatbind", "gnatmake");
680 Gnatlink : String_Access := Program_Name ("gnatlink", "gnatmake");
681 -- Default compiler, binder, linker programs
683 Globalizer : constant String := "codepeer_globalizer";
684 -- CodePeer globalizer executable name
686 Saved_Gcc : String_Access := null;
687 Saved_Gnatbind : String_Access := null;
688 Saved_Gnatlink : String_Access := null;
689 -- Given by the command line. Will be used, if non null
691 Gcc_Path : String_Access :=
692 GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
693 Gnatbind_Path : String_Access :=
694 GNAT.OS_Lib.Locate_Exec_On_Path (Gnatbind.all);
695 Gnatlink_Path : String_Access :=
696 GNAT.OS_Lib.Locate_Exec_On_Path (Gnatlink.all);
697 -- Path for compiler, binder, linker programs, defaulted now for gnatdist.
698 -- Changed later if overridden on command line.
700 Globalizer_Path : constant String_Access :=
701 GNAT.OS_Lib.Locate_Exec_On_Path (Globalizer);
702 -- Path for CodePeer globalizer
704 Comp_Flag : constant String_Access := new String'("-c");
705 Output_Flag : constant String_Access := new String'("-o");
706 Ada_Flag_1 : constant String_Access := new String'("-x");
707 Ada_Flag_2 : constant String_Access := new String'("ada");
708 AdaSCIL_Flag : constant String_Access := new String'("adascil");
709 No_gnat_adc : constant String_Access := new String'("-gnatA");
710 GNAT_Flag : constant String_Access := new String'("-gnatpg");
711 Do_Not_Check_Flag : constant String_Access := new String'("-x");
713 Object_Suffix : constant String := Get_Target_Object_Suffix.all;
715 Syntax_Only : Boolean := False;
716 -- Set to True when compiling with -gnats
718 Display_Executed_Programs : Boolean := True;
719 -- Set to True if name of commands should be output on stderr (or on stdout
720 -- if the Commands_To_Stdout flag was set by use of the -eS switch).
722 Output_File_Name_Seen : Boolean := False;
723 -- Set to True after having scanned the file_name for
724 -- switch "-o file_name"
726 Object_Directory_Seen : Boolean := False;
727 -- Set to True after having scanned the object directory for
728 -- switch "-D obj_dir".
730 Object_Directory_Path : String_Access := null;
731 -- The path name of the object directory, set with switch -D
733 type Make_Program_Type is (None, Compiler, Binder, Linker);
735 Program_Args : Make_Program_Type := None;
736 -- Used to indicate if we are scanning gnatmake, gcc, gnatbind, or gnatbind
737 -- options within the gnatmake command line. Used in Scan_Make_Arg only,
738 -- but must be global since value preserved from one call to another.
740 Temporary_Config_File : Boolean := False;
741 -- Set to True when there is a temporary config file used for a project
742 -- file, to avoid displaying the -gnatec switch for a temporary file.
744 procedure Add_Switches
745 (The_Package : Package_Id;
746 File_Name : String;
747 Program : Make_Program_Type;
748 Unknown_Switches_To_The_Compiler : Boolean := True;
749 Env : in out Prj.Tree.Environment);
750 procedure Add_Switch
751 (S : String_Access;
752 Program : Make_Program_Type;
753 Append_Switch : Boolean := True;
754 And_Save : Boolean := True);
755 procedure Add_Switch
756 (S : String;
757 Program : Make_Program_Type;
758 Append_Switch : Boolean := True;
759 And_Save : Boolean := True);
760 -- Make invokes one of three programs (the compiler, the binder or the
761 -- linker). For the sake of convenience, some program specific switches
762 -- can be passed directly on the gnatmake command line. This procedure
763 -- records these switches so that gnatmake can pass them to the right
764 -- program. S is the switch to be added at the end of the command line
765 -- for Program if Append_Switch is True. If Append_Switch is False S is
766 -- added at the beginning of the command line.
768 procedure Check
769 (Source_File : File_Name_Type;
770 Is_Main_Source : Boolean;
771 The_Args : Argument_List;
772 Lib_File : File_Name_Type;
773 Full_Lib_File : File_Name_Type;
774 Lib_File_Attr : access File_Attributes;
775 Read_Only : Boolean;
776 ALI : out ALI_Id;
777 O_File : out File_Name_Type;
778 O_Stamp : out Time_Stamp_Type);
779 -- Determines whether the library file Lib_File is up-to-date or not. The
780 -- full name (with path information) of the object file corresponding to
781 -- Lib_File is returned in O_File. Its time stamp is saved in O_Stamp.
782 -- ALI is the ALI_Id corresponding to Lib_File. If Lib_File in not
783 -- up-to-date, then the corresponding source file needs to be recompiled.
784 -- In this case ALI = No_ALI_Id.
785 -- Full_Lib_File must be the result of calling Osint.Full_Lib_File_Name on
786 -- Lib_File. Precomputing it saves system calls. Lib_File_Attr is the
787 -- initialized attributes of that file, which is also used to save on
788 -- system calls (it can safely be initialized to Unknown_Attributes).
790 procedure Check_Linker_Options
791 (E_Stamp : Time_Stamp_Type;
792 O_File : out File_Name_Type;
793 O_Stamp : out Time_Stamp_Type);
794 -- Checks all linker options for linker files that are newer
795 -- than E_Stamp. If such objects are found, the youngest object
796 -- is returned in O_File and its stamp in O_Stamp.
798 -- If no obsolete linker files were found, the first missing
799 -- linker file is returned in O_File and O_Stamp is empty.
800 -- Otherwise O_File is No_File.
802 procedure Collect_Arguments
803 (Source_File : File_Name_Type;
804 Is_Main_Source : Boolean;
805 Args : Argument_List);
806 -- Collect all arguments for a source to be compiled, including those
807 -- that come from a project file.
809 procedure Display (Program : String; Args : Argument_List);
810 -- Displays Program followed by the arguments in Args if variable
811 -- Display_Executed_Programs is set. The lower bound of Args must be 1.
813 procedure Report_Compilation_Failed;
814 -- Delete all temporary files and fail graciously
816 -----------------
817 -- Mapping files
818 -----------------
820 type Temp_Path_Names is array (Positive range <>) of Path_Name_Type;
821 type Temp_Path_Ptr is access Temp_Path_Names;
823 type Free_File_Indexes is array (Positive range <>) of Positive;
824 type Free_Indexes_Ptr is access Free_File_Indexes;
826 type Project_Compilation_Data is record
827 Mapping_File_Names : Temp_Path_Ptr;
828 -- The name ids of the temporary mapping files used. This is indexed
829 -- on the maximum number of compilation processes we will be spawning
830 -- (-j parameter)
832 Last_Mapping_File_Names : Natural;
833 -- Index of the last mapping file created for this project
835 Free_Mapping_File_Indexes : Free_Indexes_Ptr;
836 -- Indexes in Mapping_File_Names of the mapping file names that can be
837 -- reused for subsequent compilations.
839 Last_Free_Indexes : Natural;
840 -- Number of mapping files that can be reused
841 end record;
842 -- Information necessary when compiling a project
844 type Project_Compilation_Access is access Project_Compilation_Data;
846 package Project_Compilation_Htable is new Simple_HTable
847 (Header_Num => Prj.Header_Num,
848 Element => Project_Compilation_Access,
849 No_Element => null,
850 Key => Project_Id,
851 Hash => Prj.Hash,
852 Equal => "=");
854 Project_Compilation : Project_Compilation_Htable.Instance;
856 Gnatmake_Mapping_File : String_Access := null;
857 -- The path name of a mapping file specified by switch -C=
859 procedure Init_Mapping_File
860 (Project : Project_Id;
861 Data : in out Project_Compilation_Data;
862 File_Index : in out Natural);
863 -- Create a new temporary mapping file, and fill it with the project file
864 -- mappings, when using project file(s). The out parameter File_Index is
865 -- the index to the name of the file in the array The_Mapping_File_Names.
867 -------------------------------------------------
868 -- Subprogram declarations moved from the spec --
869 -------------------------------------------------
871 procedure Bind (ALI_File : File_Name_Type; Args : Argument_List);
872 -- Binds ALI_File. Args are the arguments to pass to the binder.
873 -- Args must have a lower bound of 1.
875 procedure Display_Commands (Display : Boolean := True);
876 -- The default behavior of Make commands (Compile_Sources, Bind, Link)
877 -- is to display them on stderr. This behavior can be changed repeatedly
878 -- by invoking this procedure.
880 -- If a compilation, bind or link failed one of the following 3 exceptions
881 -- is raised. These need to be handled by the calling routines.
883 procedure Compile_Sources
884 (Main_Source : File_Name_Type;
885 Args : Argument_List;
886 First_Compiled_File : out File_Name_Type;
887 Most_Recent_Obj_File : out File_Name_Type;
888 Most_Recent_Obj_Stamp : out Time_Stamp_Type;
889 Main_Unit : out Boolean;
890 Compilation_Failures : out Natural;
891 Main_Index : Int := 0;
892 Check_Readonly_Files : Boolean := False;
893 Do_Not_Execute : Boolean := False;
894 Force_Compilations : Boolean := False;
895 Keep_Going : Boolean := False;
896 In_Place_Mode : Boolean := False;
897 Initialize_ALI_Data : Boolean := True;
898 Max_Process : Positive := 1);
899 -- Compile_Sources will recursively compile all the sources needed by
900 -- Main_Source. Before calling this routine make sure Namet has been
901 -- initialized. This routine can be called repeatedly with different
902 -- Main_Source file as long as all the source (-I flags), library
903 -- (-B flags) and ada library (-A flags) search paths between calls are
904 -- *exactly* the same. The default directory must also be the same.
906 -- Args contains the arguments to use during the compilations.
907 -- The lower bound of Args must be 1.
909 -- First_Compiled_File is set to the name of the first file that is
910 -- compiled or that needs to be compiled. This is set to No_Name if no
911 -- compilations were needed.
913 -- Most_Recent_Obj_File is set to the full name of the most recent
914 -- object file found when no compilations are needed, that is when
915 -- First_Compiled_File is set to No_Name. When First_Compiled_File
916 -- is set then Most_Recent_Obj_File is set to No_Name.
918 -- Most_Recent_Obj_Stamp is the time stamp of Most_Recent_Obj_File.
920 -- Main_Unit is set to True if Main_Source can be a main unit.
921 -- If Do_Not_Execute is False and First_Compiled_File /= No_Name
922 -- the value of Main_Unit is always False.
923 -- Is this used any more??? It is certainly not used by gnatmake???
925 -- Compilation_Failures is a count of compilation failures. This count
926 -- is used to extract compilation failure reports with Extract_Failure.
928 -- Main_Index, when not zero, is the index of the main unit in source
929 -- file Main_Source which is a multi-unit source.
930 -- Zero indicates that Main_Source is a single unit source file.
932 -- Check_Readonly_Files set it to True to compile source files
933 -- which library files are read-only. When compiling GNAT predefined
934 -- files the "-gnatg" flag is used.
936 -- Do_Not_Execute set it to True to find out the first source that
937 -- needs to be recompiled, but without recompiling it. This file is
938 -- saved in First_Compiled_File.
940 -- Force_Compilations forces all compilations no matter what but
941 -- recompiles read-only files only if Check_Readonly_Files
942 -- is set.
944 -- Keep_Going when True keep compiling even in the presence of
945 -- compilation errors.
947 -- In_Place_Mode when True save library/object files in their object
948 -- directory if they already exist; otherwise, in the source directory.
950 -- Initialize_ALI_Data set it to True when you want to initialize ALI
951 -- data-structures. This is what you should do most of the time.
952 -- (especially the first time around when you call this routine).
953 -- This parameter is set to False to preserve previously recorded
954 -- ALI file data.
956 -- Max_Process is the maximum number of processes that should be spawned
957 -- to carry out compilations.
959 -- Flags in Package Opt Affecting Compile_Sources
960 -- -----------------------------------------------
962 -- Check_Object_Consistency set it to False to omit all consistency
963 -- checks between an .ali file and its corresponding object file.
964 -- When this flag is set to true, every time an .ali is read,
965 -- package Osint checks that the corresponding object file
966 -- exists and is more recent than the .ali.
968 -- Use of Name Table Info
969 -- ----------------------
971 -- All file names manipulated by Compile_Sources are entered into the
972 -- Names table. The Byte field of a source file is used to mark it.
974 -- Calling Compile_Sources Several Times
975 -- -------------------------------------
977 -- Upon return from Compile_Sources all the ALI data structures are left
978 -- intact for further browsing. HOWEVER upon entry to this routine ALI
979 -- data structures are re-initialized if parameter Initialize_ALI_Data
980 -- above is set to true. Typically this is what you want the first time
981 -- you call Compile_Sources. You should not load an ali file, call this
982 -- routine with flag Initialize_ALI_Data set to True and then expect
983 -- that ALI information to be around after the call. Note that the first
984 -- time you call Compile_Sources you better set Initialize_ALI_Data to
985 -- True unless you have called Initialize_ALI yourself.
987 -- Compile_Sources ALGORITHM : Compile_Sources (Main_Source)
988 -- -------------------------
990 -- 1. Insert Main_Source in a Queue (Q) and mark it.
992 -- 2. Let unit.adb be the file at the head of the Q. If unit.adb is
993 -- missing but its corresponding ali file is in an Ada library directory
994 -- (see below) then, remove unit.adb from the Q and goto step 4.
995 -- Otherwise, look at the files under the D (dependency) section of
996 -- unit.ali. If unit.ali does not exist or some of the time stamps do
997 -- not match, (re)compile unit.adb.
999 -- An Ada library directory is a directory containing Ada specs, ali
1000 -- and object files but no source files for the bodies. An Ada library
1001 -- directory is communicated to gnatmake by means of some switch so that
1002 -- gnatmake can skip the sources whole ali are in that directory.
1003 -- There are two reasons for skipping the sources in this case. Firstly,
1004 -- Ada libraries typically come without full sources but binding and
1005 -- linking against those libraries is still possible. Secondly, it would
1006 -- be very wasteful for gnatmake to systematically check the consistency
1007 -- of every external Ada library used in a program. The binder is
1008 -- already in charge of catching any potential inconsistencies.
1010 -- 3. Look into the W section of unit.ali and insert into the Q all
1011 -- unmarked source files. Mark all files newly inserted in the Q.
1012 -- Specifically, assuming that the W section looks like
1014 -- W types%s types.adb types.ali
1015 -- W unchecked_deallocation%s
1016 -- W xref_tab%s xref_tab.adb xref_tab.ali
1018 -- Then xref_tab.adb and types.adb are inserted in the Q if they are not
1019 -- already marked.
1020 -- Note that there is no file listed under W unchecked_deallocation%s
1021 -- so no generic body should ever be explicitly compiled (unless the
1022 -- Main_Source at the start was a generic body).
1024 -- 4. Repeat steps 2 and 3 above until the Q is empty
1026 -- Note that the above algorithm works because the units withed in
1027 -- subunits are transitively included in the W section (with section) of
1028 -- the main unit. Likewise the withed units in a generic body needed
1029 -- during a compilation are also transitively included in the W section
1030 -- of the originally compiled file.
1032 procedure Globalize (Success : out Boolean);
1033 -- Call the CodePeer globalizer on all the project's object directories,
1034 -- or on the current directory if no projects.
1036 procedure Initialize
1037 (Project_Node_Tree : out Project_Node_Tree_Ref;
1038 Env : out Prj.Tree.Environment);
1039 -- Performs default and package initialization. Therefore,
1040 -- Compile_Sources can be called by an external unit.
1042 procedure Link
1043 (ALI_File : File_Name_Type;
1044 Args : Argument_List;
1045 Success : out Boolean);
1046 -- Links ALI_File. Args are the arguments to pass to the linker.
1047 -- Args must have a lower bound of 1. Success indicates if the link
1048 -- succeeded or not.
1050 procedure Scan_Make_Arg
1051 (Env : in out Prj.Tree.Environment;
1052 Argv : String;
1053 And_Save : Boolean);
1054 -- Scan make arguments. Argv is a single argument to be processed.
1055 -- Project_Node_Tree will be used to initialize external references. It
1056 -- must have been initialized.
1058 -------------------
1059 -- Add_Arguments --
1060 -------------------
1062 procedure Add_Arguments (Args : Argument_List) is
1063 begin
1064 if Arguments = null then
1065 Arguments := new Argument_List (1 .. Args'Length + 10);
1067 else
1068 while Last_Argument + Args'Length > Arguments'Last loop
1069 declare
1070 New_Arguments : constant Argument_List_Access :=
1071 new Argument_List (1 .. Arguments'Last * 2);
1072 begin
1073 New_Arguments (1 .. Last_Argument) :=
1074 Arguments (1 .. Last_Argument);
1075 Arguments := New_Arguments;
1076 end;
1077 end loop;
1078 end if;
1080 Arguments (Last_Argument + 1 .. Last_Argument + Args'Length) := Args;
1081 Last_Argument := Last_Argument + Args'Length;
1082 end Add_Arguments;
1084 -- --------------------
1085 -- -- Add_Dependency --
1086 -- --------------------
1088 -- procedure Add_Dependency (S : File_Name_Type; On : File_Name_Type) is
1089 -- begin
1090 -- Dependencies.Increment_Last;
1091 -- Dependencies.Table (Dependencies.Last) := (S, On);
1092 -- end Add_Dependency;
1094 ----------------------------
1095 -- Add_Library_Search_Dir --
1096 ----------------------------
1098 procedure Add_Library_Search_Dir
1099 (Path : String;
1100 On_Command_Line : Boolean)
1102 begin
1103 if On_Command_Line then
1104 Add_Lib_Search_Dir (Normalize_Pathname (Path));
1106 else
1107 Get_Name_String (Main_Project.Directory.Display_Name);
1108 Add_Lib_Search_Dir
1109 (Normalize_Pathname (Path, Name_Buffer (1 .. Name_Len)));
1110 end if;
1111 end Add_Library_Search_Dir;
1113 --------------------
1114 -- Add_Object_Dir --
1115 --------------------
1117 procedure Add_Object_Dir (N : String) is
1118 begin
1119 Add_Lib_Search_Dir (N);
1121 if Verbose_Mode then
1122 Write_Str ("Adding object directory """);
1123 Write_Str (N);
1124 Write_Str (""".");
1125 Write_Eol;
1126 end if;
1127 end Add_Object_Dir;
1129 --------------------
1130 -- Add_Source_Dir --
1131 --------------------
1133 procedure Add_Source_Dir (N : String) is
1134 begin
1135 Add_Src_Search_Dir (N);
1137 if Verbose_Mode then
1138 Write_Str ("Adding source directory """);
1139 Write_Str (N);
1140 Write_Str (""".");
1141 Write_Eol;
1142 end if;
1143 end Add_Source_Dir;
1145 ---------------------------
1146 -- Add_Source_Search_Dir --
1147 ---------------------------
1149 procedure Add_Source_Search_Dir
1150 (Path : String;
1151 On_Command_Line : Boolean)
1153 begin
1154 if On_Command_Line then
1155 Add_Src_Search_Dir (Normalize_Pathname (Path));
1157 else
1158 Get_Name_String (Main_Project.Directory.Display_Name);
1159 Add_Src_Search_Dir
1160 (Normalize_Pathname (Path, Name_Buffer (1 .. Name_Len)));
1161 end if;
1162 end Add_Source_Search_Dir;
1164 ----------------
1165 -- Add_Switch --
1166 ----------------
1168 procedure Add_Switch
1169 (S : String_Access;
1170 Program : Make_Program_Type;
1171 Append_Switch : Boolean := True;
1172 And_Save : Boolean := True)
1174 generic
1175 with package T is new Table.Table (<>);
1176 procedure Generic_Position (New_Position : out Integer);
1177 -- Generic procedure that chooses a position for S in T at the
1178 -- beginning or the end, depending on the boolean Append_Switch.
1179 -- Calling this procedure may expand the table.
1181 ----------------------
1182 -- Generic_Position --
1183 ----------------------
1185 procedure Generic_Position (New_Position : out Integer) is
1186 begin
1187 T.Increment_Last;
1189 if Append_Switch then
1190 New_Position := Integer (T.Last);
1191 else
1192 for J in reverse T.Table_Index_Type'Succ (T.First) .. T.Last loop
1193 T.Table (J) := T.Table (T.Table_Index_Type'Pred (J));
1194 end loop;
1196 New_Position := Integer (T.First);
1197 end if;
1198 end Generic_Position;
1200 procedure Gcc_Switches_Pos is new Generic_Position (Gcc_Switches);
1201 procedure Binder_Switches_Pos is new Generic_Position (Binder_Switches);
1202 procedure Linker_Switches_Pos is new Generic_Position (Linker_Switches);
1204 procedure Saved_Gcc_Switches_Pos is new
1205 Generic_Position (Saved_Gcc_Switches);
1207 procedure Saved_Binder_Switches_Pos is new
1208 Generic_Position (Saved_Binder_Switches);
1210 procedure Saved_Linker_Switches_Pos is new
1211 Generic_Position (Saved_Linker_Switches);
1213 New_Position : Integer;
1215 -- Start of processing for Add_Switch
1217 begin
1218 if And_Save then
1219 case Program is
1220 when Compiler =>
1221 Saved_Gcc_Switches_Pos (New_Position);
1222 Saved_Gcc_Switches.Table (New_Position) := S;
1224 when Binder =>
1225 Saved_Binder_Switches_Pos (New_Position);
1226 Saved_Binder_Switches.Table (New_Position) := S;
1228 when Linker =>
1229 Saved_Linker_Switches_Pos (New_Position);
1230 Saved_Linker_Switches.Table (New_Position) := S;
1232 when None =>
1233 raise Program_Error;
1234 end case;
1236 else
1237 case Program is
1238 when Compiler =>
1239 Gcc_Switches_Pos (New_Position);
1240 Gcc_Switches.Table (New_Position) := S;
1242 when Binder =>
1243 Binder_Switches_Pos (New_Position);
1244 Binder_Switches.Table (New_Position) := S;
1246 when Linker =>
1247 Linker_Switches_Pos (New_Position);
1248 Linker_Switches.Table (New_Position) := S;
1250 when None =>
1251 raise Program_Error;
1252 end case;
1253 end if;
1254 end Add_Switch;
1256 procedure Add_Switch
1257 (S : String;
1258 Program : Make_Program_Type;
1259 Append_Switch : Boolean := True;
1260 And_Save : Boolean := True)
1262 begin
1263 Add_Switch (S => new String'(S),
1264 Program => Program,
1265 Append_Switch => Append_Switch,
1266 And_Save => And_Save);
1267 end Add_Switch;
1269 ------------------
1270 -- Add_Switches --
1271 ------------------
1273 procedure Add_Switches
1274 (The_Package : Package_Id;
1275 File_Name : String;
1276 Program : Make_Program_Type;
1277 Unknown_Switches_To_The_Compiler : Boolean := True;
1278 Env : in out Prj.Tree.Environment)
1280 Switches : Variable_Value;
1281 Switch_List : String_List_Id;
1282 Element : String_Element;
1284 begin
1285 Switch_May_Be_Passed_To_The_Compiler :=
1286 Unknown_Switches_To_The_Compiler;
1288 if File_Name'Length > 0 then
1289 Name_Len := 0;
1290 Add_Str_To_Name_Buffer (File_Name);
1291 Switches :=
1292 Switches_Of
1293 (Source_File => Name_Find,
1294 Project => Main_Project,
1295 In_Package => The_Package,
1296 Allow_ALI => Program = Binder or else Program = Linker);
1298 if Switches.Kind = List then
1299 Program_Args := Program;
1301 Switch_List := Switches.Values;
1302 while Switch_List /= Nil_String loop
1303 Element :=
1304 Project_Tree.Shared.String_Elements.Table (Switch_List);
1305 Get_Name_String (Element.Value);
1307 if Name_Len > 0 then
1308 declare
1309 Argv : constant String := Name_Buffer (1 .. Name_Len);
1310 -- We need a copy, because Name_Buffer may be modified
1312 begin
1313 if Verbose_Mode then
1314 Write_Str (" Adding ");
1315 Write_Line (Argv);
1316 end if;
1318 Scan_Make_Arg (Env, Argv, And_Save => False);
1320 if not Gnatmake_Switch_Found
1321 and then not Switch_May_Be_Passed_To_The_Compiler
1322 then
1323 Errutil.Error_Msg
1324 ('"' & Argv &
1325 """ is not a gnatmake switch. Consider moving "
1326 & "it to Global_Compilation_Switches.",
1327 Element.Location);
1328 Make_Failed ("*** illegal switch """ & Argv & """");
1329 end if;
1330 end;
1331 end if;
1333 Switch_List := Element.Next;
1334 end loop;
1335 end if;
1336 end if;
1337 end Add_Switches;
1339 ----------
1340 -- Bind --
1341 ----------
1343 procedure Bind (ALI_File : File_Name_Type; Args : Argument_List) is
1344 Bind_Args : Argument_List (1 .. Args'Last + 2);
1345 Bind_Last : Integer;
1346 Success : Boolean;
1348 begin
1349 pragma Assert (Args'First = 1);
1351 -- Optimize the simple case where the gnatbind command line looks like
1352 -- gnatbind -aO. -I- file.ali
1353 -- into
1354 -- gnatbind file.adb
1356 if Args'Length = 2
1357 and then Args (Args'First).all = "-aO" & Normalized_CWD
1358 and then Args (Args'Last).all = "-I-"
1359 and then ALI_File = Strip_Directory (ALI_File)
1360 then
1361 Bind_Last := Args'First - 1;
1363 else
1364 Bind_Last := Args'Last;
1365 Bind_Args (Args'Range) := Args;
1366 end if;
1368 -- It is completely pointless to re-check source file time stamps. This
1369 -- has been done already by gnatmake
1371 Bind_Last := Bind_Last + 1;
1372 Bind_Args (Bind_Last) := Do_Not_Check_Flag;
1374 Get_Name_String (ALI_File);
1376 Bind_Last := Bind_Last + 1;
1377 Bind_Args (Bind_Last) := new String'(Name_Buffer (1 .. Name_Len));
1379 GNAT.OS_Lib.Normalize_Arguments (Bind_Args (Args'First .. Bind_Last));
1381 Display (Gnatbind.all, Bind_Args (Args'First .. Bind_Last));
1383 if Gnatbind_Path = null then
1384 Make_Failed ("error, unable to locate " & Gnatbind.all);
1385 end if;
1387 GNAT.OS_Lib.Spawn
1388 (Gnatbind_Path.all, Bind_Args (Args'First .. Bind_Last), Success);
1390 if not Success then
1391 Make_Failed ("*** bind failed.");
1392 end if;
1393 end Bind;
1395 --------------------------------
1396 -- Change_To_Object_Directory --
1397 --------------------------------
1399 procedure Change_To_Object_Directory (Project : Project_Id) is
1400 Object_Directory : Path_Name_Type;
1402 begin
1403 pragma Assert (Project /= No_Project);
1405 -- Nothing to do if the current working directory is already the correct
1406 -- object directory.
1408 if Project_Of_Current_Object_Directory /= Project then
1409 Project_Of_Current_Object_Directory := Project;
1410 Object_Directory := Project.Object_Directory.Display_Name;
1412 -- Set the working directory to the object directory of the actual
1413 -- project.
1415 if Verbose_Mode then
1416 Write_Str ("Changing to object directory of """);
1417 Write_Name (Project.Display_Name);
1418 Write_Str (""": """);
1419 Write_Name (Object_Directory);
1420 Write_Line ("""");
1421 end if;
1423 Change_Dir (Get_Name_String (Object_Directory));
1424 end if;
1426 exception
1427 -- Fail if unable to change to the object directory
1429 when Directory_Error =>
1430 Make_Failed ("unable to change to object directory """ &
1431 Path_Or_File_Name
1432 (Project.Object_Directory.Display_Name) &
1433 """ of project " &
1434 Get_Name_String (Project.Display_Name));
1435 end Change_To_Object_Directory;
1437 -----------
1438 -- Check --
1439 -----------
1441 procedure Check
1442 (Source_File : File_Name_Type;
1443 Is_Main_Source : Boolean;
1444 The_Args : Argument_List;
1445 Lib_File : File_Name_Type;
1446 Full_Lib_File : File_Name_Type;
1447 Lib_File_Attr : access File_Attributes;
1448 Read_Only : Boolean;
1449 ALI : out ALI_Id;
1450 O_File : out File_Name_Type;
1451 O_Stamp : out Time_Stamp_Type)
1453 function First_New_Spec (A : ALI_Id) return File_Name_Type;
1454 -- Looks in the with table entries of A and returns the spec file name
1455 -- of the first withed unit (subprogram) for which no spec existed when
1456 -- A was generated but for which there exists one now, implying that A
1457 -- is now obsolete. If no such unit is found No_File is returned.
1458 -- Otherwise the spec file name of the unit is returned.
1460 -- **WARNING** in the event of Uname format modifications, one *MUST*
1461 -- make sure this function is also updated.
1463 -- Note: This function should really be in ali.adb and use Uname
1464 -- services, but this causes the whole compiler to be dragged along
1465 -- for gnatbind and gnatmake.
1467 --------------------
1468 -- First_New_Spec --
1469 --------------------
1471 function First_New_Spec (A : ALI_Id) return File_Name_Type is
1472 Spec_File_Name : File_Name_Type := No_File;
1474 function New_Spec (Uname : Unit_Name_Type) return Boolean;
1475 -- Uname is the name of the spec or body of some ada unit. This
1476 -- function returns True if the Uname is the name of a body which has
1477 -- a spec not mentioned in ALI file A. If True is returned
1478 -- Spec_File_Name above is set to the name of this spec file.
1480 --------------
1481 -- New_Spec --
1482 --------------
1484 function New_Spec (Uname : Unit_Name_Type) return Boolean is
1485 Spec_Name : Unit_Name_Type;
1486 File_Name : File_Name_Type;
1488 begin
1489 -- Test whether Uname is the name of a body unit (i.e. ends
1490 -- with %b).
1492 Get_Name_String (Uname);
1493 pragma
1494 Assert (Name_Len > 2 and then Name_Buffer (Name_Len - 1) = '%');
1496 if Name_Buffer (Name_Len) /= 'b' then
1497 return False;
1498 end if;
1500 -- Convert unit name into spec name
1502 -- ??? this code seems dubious in presence of pragma
1503 -- Source_File_Name since there is no more direct relationship
1504 -- between unit name and file name.
1506 -- ??? Further, what about alternative subunit naming
1508 Name_Buffer (Name_Len) := 's';
1509 Spec_Name := Name_Find;
1510 File_Name := Get_File_Name (Spec_Name, Subunit => False);
1512 -- Look if File_Name is mentioned in A's sdep list.
1513 -- If not look if the file exists. If it does return True.
1515 for D in
1516 ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep
1517 loop
1518 if Sdep.Table (D).Sfile = File_Name then
1519 return False;
1520 end if;
1521 end loop;
1523 if Full_Source_Name (File_Name) /= No_File then
1524 Spec_File_Name := File_Name;
1525 return True;
1526 end if;
1528 return False;
1529 end New_Spec;
1531 -- Start of processing for First_New_Spec
1533 begin
1534 U_Chk : for U in
1535 ALIs.Table (A).First_Unit .. ALIs.Table (A).Last_Unit
1536 loop
1537 exit U_Chk when Units.Table (U).Utype = Is_Body_Only
1538 and then New_Spec (Units.Table (U).Uname);
1540 for W in Units.Table (U).First_With
1542 Units.Table (U).Last_With
1543 loop
1544 exit U_Chk when
1545 Withs.Table (W).Afile /= No_File
1546 and then New_Spec (Withs.Table (W).Uname);
1547 end loop;
1548 end loop U_Chk;
1550 return Spec_File_Name;
1551 end First_New_Spec;
1553 ---------------------------------
1554 -- Data declarations for Check --
1555 ---------------------------------
1557 Full_Obj_File : File_Name_Type;
1558 -- Full name of the object file corresponding to Lib_File
1560 Lib_Stamp : Time_Stamp_Type;
1561 -- Time stamp of the current ada library file
1563 Obj_Stamp : Time_Stamp_Type;
1564 -- Time stamp of the current object file
1566 Modified_Source : File_Name_Type;
1567 -- The first source in Lib_File whose current time stamp differs from
1568 -- that stored in Lib_File.
1570 New_Spec : File_Name_Type;
1571 -- If Lib_File contains in its W (with) section a body (for a
1572 -- subprogram) for which there exists a spec, and the spec did not
1573 -- appear in the Sdep section of Lib_File, New_Spec contains the file
1574 -- name of this new spec.
1576 Source_Name : File_Name_Type;
1577 Text : Text_Buffer_Ptr;
1579 Prev_Switch : String_Access;
1580 -- Previous switch processed
1582 Arg : Arg_Id := Arg_Id'First;
1583 -- Current index in Args.Table for a given unit (init to stop warning)
1585 Switch_Found : Boolean;
1586 -- True if a given switch has been found
1588 ALI_Project : Project_Id;
1589 -- If the ALI file is in the object directory of a project, this is
1590 -- the project id.
1592 -- Start of processing for Check
1594 begin
1595 pragma Assert (Lib_File /= No_File);
1597 -- If ALI file is read-only, temporarily set Check_Object_Consistency to
1598 -- False. We don't care if the object file is not there (presumably a
1599 -- library will be used for linking.)
1601 if Read_Only then
1602 declare
1603 Saved_Check_Object_Consistency : constant Boolean :=
1604 Check_Object_Consistency;
1605 begin
1606 Check_Object_Consistency := False;
1607 Text := Read_Library_Info_From_Full (Full_Lib_File, Lib_File_Attr);
1608 Check_Object_Consistency := Saved_Check_Object_Consistency;
1609 end;
1611 else
1612 Text := Read_Library_Info_From_Full (Full_Lib_File, Lib_File_Attr);
1613 end if;
1615 Full_Obj_File := Full_Object_File_Name;
1616 Lib_Stamp := Current_Library_File_Stamp;
1617 Obj_Stamp := Current_Object_File_Stamp;
1619 if Full_Lib_File = No_File then
1620 Verbose_Msg
1621 (Lib_File,
1622 "being checked ...",
1623 Prefix => " ",
1624 Minimum_Verbosity => Opt.Medium);
1625 else
1626 Verbose_Msg
1627 (Full_Lib_File,
1628 "being checked ...",
1629 Prefix => " ",
1630 Minimum_Verbosity => Opt.Medium);
1631 end if;
1633 ALI := No_ALI_Id;
1634 O_File := Full_Obj_File;
1635 O_Stamp := Obj_Stamp;
1637 if Text = null then
1638 if Full_Lib_File = No_File then
1639 Verbose_Msg (Lib_File, "missing.");
1641 elsif Obj_Stamp (Obj_Stamp'First) = ' ' then
1642 Verbose_Msg (Full_Obj_File, "missing.");
1644 else
1645 Verbose_Msg
1646 (Full_Lib_File, "(" & String (Lib_Stamp) & ") newer than",
1647 Full_Obj_File, "(" & String (Obj_Stamp) & ")");
1648 end if;
1650 else
1651 ALI := Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
1652 Free (Text);
1654 if ALI = No_ALI_Id then
1655 Verbose_Msg (Full_Lib_File, "incorrectly formatted ALI file");
1656 return;
1658 elsif ALIs.Table (ALI).Ver (1 .. ALIs.Table (ALI).Ver_Len) /=
1659 Verbose_Library_Version
1660 then
1661 Verbose_Msg (Full_Lib_File, "compiled with old GNAT version");
1662 ALI := No_ALI_Id;
1663 return;
1664 end if;
1666 -- Don't take ALI file into account if it was generated with errors
1668 if ALIs.Table (ALI).Compile_Errors then
1669 Verbose_Msg (Full_Lib_File, "had errors, must be recompiled");
1670 ALI := No_ALI_Id;
1671 return;
1672 end if;
1674 -- Don't take ALI file into account if no object was generated
1676 if Operating_Mode /= Check_Semantics
1677 and then ALIs.Table (ALI).No_Object
1678 then
1679 Verbose_Msg (Full_Lib_File, "has no corresponding object");
1680 ALI := No_ALI_Id;
1681 return;
1682 end if;
1684 -- When compiling with -gnatc, don't take ALI file into account if
1685 -- it has not been generated for the current source, for example if
1686 -- it has been generated for the spec, but we are compiling the body.
1688 if Operating_Mode = Check_Semantics then
1689 declare
1690 File_Name : String := Get_Name_String (Source_File);
1691 OK : Boolean := False;
1693 begin
1694 -- In the ALI file, the source file names are in canonical case
1696 Canonical_Case_File_Name (File_Name);
1698 for U in ALIs.Table (ALI).First_Unit ..
1699 ALIs.Table (ALI).Last_Unit
1700 loop
1701 OK := Get_Name_String (Units.Table (U).Sfile) = File_Name;
1702 exit when OK;
1703 end loop;
1705 if not OK then
1706 Verbose_Msg
1707 (Full_Lib_File, "not generated for the same source");
1708 ALI := No_ALI_Id;
1709 return;
1710 end if;
1711 end;
1712 end if;
1714 -- Check for matching compiler switches if needed
1716 if Check_Switches then
1718 -- First, collect all the switches
1720 Collect_Arguments (Source_File, Is_Main_Source, The_Args);
1721 Prev_Switch := Dummy_Switch;
1722 Get_Name_String (ALIs.Table (ALI).Sfile);
1723 Switches_To_Check.Set_Last (0);
1725 for J in 1 .. Last_Argument loop
1727 -- Skip non switches -c, -I and -o switches
1729 if Arguments (J) (1) = '-'
1730 and then Arguments (J) (2) /= 'c'
1731 and then Arguments (J) (2) /= 'o'
1732 and then Arguments (J) (2) /= 'I'
1733 then
1734 Normalize_Compiler_Switches
1735 (Arguments (J).all,
1736 Normalized_Switches,
1737 Last_Norm_Switch);
1739 for K in 1 .. Last_Norm_Switch loop
1740 Switches_To_Check.Increment_Last;
1741 Switches_To_Check.Table (Switches_To_Check.Last) :=
1742 Normalized_Switches (K);
1743 end loop;
1744 end if;
1745 end loop;
1747 for J in 1 .. Switches_To_Check.Last loop
1749 -- Comparing switches is delicate because gcc reorders a number
1750 -- of switches, according to lang-specs.h, but gnatmake doesn't
1751 -- have sufficient knowledge to perform the same reordering.
1752 -- Instead, we ignore orders between different "first letter"
1753 -- switches, but keep orders between same switches, e.g -O -O2
1754 -- is different than -O2 -O, but -g -O is equivalent to -O -g.
1756 if Switches_To_Check.Table (J) (2) /= Prev_Switch (2) or else
1757 (Prev_Switch'Length >= 6 and then
1758 Prev_Switch (2 .. 5) = "gnat" and then
1759 Switches_To_Check.Table (J)'Length >= 6 and then
1760 Switches_To_Check.Table (J) (2 .. 5) = "gnat" and then
1761 Prev_Switch (6) /= Switches_To_Check.Table (J) (6))
1762 then
1763 Prev_Switch := Switches_To_Check.Table (J);
1764 Arg :=
1765 Units.Table (ALIs.Table (ALI).First_Unit).First_Arg;
1766 end if;
1768 Switch_Found := False;
1770 for K in Arg ..
1771 Units.Table (ALIs.Table (ALI).First_Unit).Last_Arg
1772 loop
1774 Switches_To_Check.Table (J).all = Args.Table (K).all
1775 then
1776 Arg := K + 1;
1777 Switch_Found := True;
1778 exit;
1779 end if;
1780 end loop;
1782 if not Switch_Found then
1783 if Verbose_Mode then
1784 Verbose_Msg (ALIs.Table (ALI).Sfile,
1785 "switch mismatch """ &
1786 Switches_To_Check.Table (J).all & '"');
1787 end if;
1789 ALI := No_ALI_Id;
1790 return;
1791 end if;
1792 end loop;
1794 if Switches_To_Check.Last /=
1795 Integer (Units.Table (ALIs.Table (ALI).First_Unit).Last_Arg -
1796 Units.Table (ALIs.Table (ALI).First_Unit).First_Arg + 1)
1797 then
1798 if Verbose_Mode then
1799 Verbose_Msg (ALIs.Table (ALI).Sfile,
1800 "different number of switches");
1802 for K in Units.Table (ALIs.Table (ALI).First_Unit).First_Arg
1803 .. Units.Table (ALIs.Table (ALI).First_Unit).Last_Arg
1804 loop
1805 Write_Str (Args.Table (K).all);
1806 Write_Char (' ');
1807 end loop;
1809 Write_Eol;
1811 for J in 1 .. Switches_To_Check.Last loop
1812 Write_Str (Switches_To_Check.Table (J).all);
1813 Write_Char (' ');
1814 end loop;
1816 Write_Eol;
1817 end if;
1819 ALI := No_ALI_Id;
1820 return;
1821 end if;
1822 end if;
1824 -- Get the source files and their message digests. Note that some
1825 -- sources may be missing if ALI is out-of-date.
1827 Set_Source_Table (ALI);
1829 Modified_Source := Time_Stamp_Mismatch (ALI, Read_Only);
1831 -- To avoid using too much memory when switch -m is used, free the
1832 -- memory allocated for the source file when computing the checksum.
1834 if Minimal_Recompilation then
1835 Sinput.P.Clear_Source_File_Table;
1836 end if;
1838 if Modified_Source /= No_File then
1839 ALI := No_ALI_Id;
1841 if Verbose_Mode then
1842 Source_Name := Full_Source_Name (Modified_Source);
1844 if Source_Name /= No_File then
1845 Verbose_Msg (Source_Name, "time stamp mismatch");
1846 else
1847 Verbose_Msg (Modified_Source, "missing");
1848 end if;
1849 end if;
1851 else
1852 New_Spec := First_New_Spec (ALI);
1854 if New_Spec /= No_File then
1855 ALI := No_ALI_Id;
1857 if Verbose_Mode then
1858 Source_Name := Full_Source_Name (New_Spec);
1860 if Source_Name /= No_File then
1861 Verbose_Msg (Source_Name, "new spec");
1862 else
1863 Verbose_Msg (New_Spec, "old spec missing");
1864 end if;
1865 end if;
1867 elsif not Read_Only and then Main_Project /= No_Project then
1868 declare
1869 Uname : constant Name_Id :=
1870 Check_Source_Info_In_ALI (ALI, Project_Tree);
1872 Udata : Prj.Unit_Index;
1874 begin
1875 if Uname = No_Name then
1876 ALI := No_ALI_Id;
1877 return;
1878 end if;
1880 -- Check that ALI file is in the correct object directory.
1881 -- If it is in the object directory of a project that is
1882 -- extended and it depends on a source that is in one of
1883 -- its extending projects, then the ALI file is not in the
1884 -- correct object directory.
1886 -- First, find the project of this ALI file. As there may be
1887 -- several projects with the same object directory, we first
1888 -- need to find the project of the source.
1890 ALI_Project := No_Project;
1892 Udata := Units_Htable.Get (Project_Tree.Units_HT, Uname);
1894 if Udata /= No_Unit_Index then
1895 if Udata.File_Names (Impl) /= null
1896 and then Udata.File_Names (Impl).File = Source_File
1897 then
1898 ALI_Project := Udata.File_Names (Impl).Project;
1900 elsif Udata.File_Names (Spec) /= null
1901 and then Udata.File_Names (Spec).File = Source_File
1902 then
1903 ALI_Project := Udata.File_Names (Spec).Project;
1904 end if;
1905 end if;
1906 end;
1908 if ALI_Project = No_Project then
1909 return;
1910 end if;
1912 declare
1913 Obj_Dir : Path_Name_Type;
1914 Res_Obj_Dir : constant String :=
1915 Normalize_Pathname
1916 (Dir_Name
1917 (Get_Name_String (Full_Lib_File)),
1918 Resolve_Links =>
1919 Opt.Follow_Links_For_Dirs,
1920 Case_Sensitive => False);
1922 begin
1923 Name_Len := 0;
1924 Add_Str_To_Name_Buffer (Res_Obj_Dir);
1926 if not Is_Directory_Separator (Name_Buffer (Name_Len)) then
1927 Add_Char_To_Name_Buffer (Directory_Separator);
1928 end if;
1930 Obj_Dir := Name_Find;
1932 while ALI_Project /= No_Project
1933 and then Obj_Dir /= ALI_Project.Object_Directory.Name
1934 loop
1935 ALI_Project := ALI_Project.Extended_By;
1936 end loop;
1937 end;
1939 if ALI_Project = No_Project then
1940 ALI := No_ALI_Id;
1942 Verbose_Msg (Lib_File, " wrong object directory");
1943 return;
1944 end if;
1946 -- If the ALI project is not extended, then it must be in
1947 -- the correct object directory.
1949 if ALI_Project.Extended_By = No_Project then
1950 return;
1951 end if;
1953 -- Count the extending projects
1955 declare
1956 Num_Ext : Natural;
1957 Proj : Project_Id;
1959 begin
1960 Num_Ext := 0;
1961 Proj := ALI_Project;
1962 loop
1963 Proj := Proj.Extended_By;
1964 exit when Proj = No_Project;
1965 Num_Ext := Num_Ext + 1;
1966 end loop;
1968 -- Make a list of the extending projects
1970 declare
1971 Projects : array (1 .. Num_Ext) of Project_Id;
1972 Dep : Sdep_Record;
1973 OK : Boolean := True;
1974 UID : Unit_Index;
1976 begin
1977 Proj := ALI_Project;
1978 for J in Projects'Range loop
1979 Proj := Proj.Extended_By;
1980 Projects (J) := Proj;
1981 end loop;
1983 -- Now check if any of the dependant sources are in any
1984 -- of these extending projects.
1986 D_Chk :
1987 for D in ALIs.Table (ALI).First_Sdep ..
1988 ALIs.Table (ALI).Last_Sdep
1989 loop
1990 Dep := Sdep.Table (D);
1991 UID := Units_Htable.Get_First (Project_Tree.Units_HT);
1992 Proj := No_Project;
1994 Unit_Loop :
1995 while UID /= null loop
1996 if UID.File_Names (Impl) /= null
1997 and then UID.File_Names (Impl).File = Dep.Sfile
1998 then
1999 Proj := UID.File_Names (Impl).Project;
2001 elsif UID.File_Names (Spec) /= null
2002 and then UID.File_Names (Spec).File = Dep.Sfile
2003 then
2004 Proj := UID.File_Names (Spec).Project;
2005 end if;
2007 -- If a source is in a project, check if it is one
2008 -- in the list.
2010 if Proj /= No_Project then
2011 for J in Projects'Range loop
2012 if Proj = Projects (J) then
2013 OK := False;
2014 exit D_Chk;
2015 end if;
2016 end loop;
2018 exit Unit_Loop;
2019 end if;
2021 UID :=
2022 Units_Htable.Get_Next (Project_Tree.Units_HT);
2023 end loop Unit_Loop;
2024 end loop D_Chk;
2026 -- If one of the dependent sources is in one project of
2027 -- the list, then we must recompile.
2029 if not OK then
2030 ALI := No_ALI_Id;
2031 Verbose_Msg (Lib_File, " wrong object directory");
2032 end if;
2033 end;
2034 end;
2035 end if;
2036 end if;
2037 end if;
2038 end Check;
2040 ------------------------
2041 -- Check_For_S_Switch --
2042 ------------------------
2044 procedure Check_For_S_Switch is
2045 begin
2046 -- By default, we generate an object file
2048 Output_Is_Object := True;
2050 for Arg in 1 .. Last_Argument loop
2051 if Arguments (Arg).all = "-S" then
2052 Output_Is_Object := False;
2054 elsif Arguments (Arg).all = "-c" then
2055 Output_Is_Object := True;
2056 end if;
2057 end loop;
2058 end Check_For_S_Switch;
2060 --------------------------
2061 -- Check_Linker_Options --
2062 --------------------------
2064 procedure Check_Linker_Options
2065 (E_Stamp : Time_Stamp_Type;
2066 O_File : out File_Name_Type;
2067 O_Stamp : out Time_Stamp_Type)
2069 procedure Check_File (File : File_Name_Type);
2070 -- Update O_File and O_Stamp if the given file is younger than E_Stamp
2071 -- and O_Stamp, or if O_File is No_File and File does not exist.
2073 function Get_Library_File (Name : String) return File_Name_Type;
2074 -- Return the full file name including path of a library based
2075 -- on the name specified with the -l linker option, using the
2076 -- Ada object path. Return No_File if no such file can be found.
2078 type Char_Array is array (Natural) of Character;
2079 type Char_Array_Access is access constant Char_Array;
2081 Template : Char_Array_Access;
2082 pragma Import (C, Template, "__gnat_library_template");
2084 ----------------
2085 -- Check_File --
2086 ----------------
2088 procedure Check_File (File : File_Name_Type) is
2089 Stamp : Time_Stamp_Type;
2090 Name : File_Name_Type := File;
2092 begin
2093 Get_Name_String (Name);
2095 -- Remove any trailing NUL characters
2097 while Name_Len >= Name_Buffer'First
2098 and then Name_Buffer (Name_Len) = NUL
2099 loop
2100 Name_Len := Name_Len - 1;
2101 end loop;
2103 if Name_Len = 0 then
2104 return;
2106 elsif Name_Buffer (1) = '-' then
2108 -- Do not check if File is a switch other than "-l"
2110 if Name_Buffer (2) /= 'l' then
2111 return;
2112 end if;
2114 -- The argument is a library switch, get actual name. It
2115 -- is necessary to make a copy of the relevant part of
2116 -- Name_Buffer as Get_Library_Name uses Name_Buffer as well.
2118 declare
2119 Base_Name : constant String := Name_Buffer (3 .. Name_Len);
2121 begin
2122 Name := Get_Library_File (Base_Name);
2123 end;
2125 if Name = No_File then
2126 return;
2127 end if;
2128 end if;
2130 Stamp := File_Stamp (Name);
2132 -- Find the youngest object file that is younger than the
2133 -- executable. If no such file exist, record the first object
2134 -- file that is not found.
2136 if (O_Stamp < Stamp and then E_Stamp < Stamp)
2137 or else (O_File = No_File and then Stamp (Stamp'First) = ' ')
2138 then
2139 O_Stamp := Stamp;
2140 O_File := Name;
2142 -- Strip the trailing NUL if present
2144 Get_Name_String (O_File);
2146 if Name_Buffer (Name_Len) = NUL then
2147 Name_Len := Name_Len - 1;
2148 O_File := Name_Find;
2149 end if;
2150 end if;
2151 end Check_File;
2153 ----------------------
2154 -- Get_Library_Name --
2155 ----------------------
2157 -- See comments in a-adaint.c about template syntax
2159 function Get_Library_File (Name : String) return File_Name_Type is
2160 File : File_Name_Type := No_File;
2162 begin
2163 Name_Len := 0;
2165 for Ptr in Template'Range loop
2166 case Template (Ptr) is
2167 when '*' =>
2168 Add_Str_To_Name_Buffer (Name);
2170 when ';' =>
2171 File := Full_Lib_File_Name (Name_Find);
2172 exit when File /= No_File;
2173 Name_Len := 0;
2175 when NUL =>
2176 exit;
2178 when others =>
2179 Add_Char_To_Name_Buffer (Template (Ptr));
2180 end case;
2181 end loop;
2183 -- The for loop exited because the end of the template
2184 -- was reached. File contains the last possible file name
2185 -- for the library.
2187 if File = No_File and then Name_Len > 0 then
2188 File := Full_Lib_File_Name (Name_Find);
2189 end if;
2191 return File;
2192 end Get_Library_File;
2194 -- Start of processing for Check_Linker_Options
2196 begin
2197 O_File := No_File;
2198 O_Stamp := (others => ' ');
2200 -- Process linker options from the ALI files
2202 for Opt in 1 .. Linker_Options.Last loop
2203 Check_File (File_Name_Type (Linker_Options.Table (Opt).Name));
2204 end loop;
2206 -- Process options given on the command line
2208 for Opt in Linker_Switches.First .. Linker_Switches.Last loop
2210 -- Check if the previous Opt has one of the two switches
2211 -- that take an extra parameter. (See GCC manual.)
2213 if Opt = Linker_Switches.First
2214 or else (Linker_Switches.Table (Opt - 1).all /= "-u"
2215 and then
2216 Linker_Switches.Table (Opt - 1).all /= "-Xlinker"
2217 and then
2218 Linker_Switches.Table (Opt - 1).all /= "-L")
2219 then
2220 Name_Len := 0;
2221 Add_Str_To_Name_Buffer (Linker_Switches.Table (Opt).all);
2222 Check_File (Name_Find);
2223 end if;
2224 end loop;
2225 end Check_Linker_Options;
2227 -----------------
2228 -- Check_Steps --
2229 -----------------
2231 procedure Check_Steps is
2232 begin
2233 -- If either -c, -b or -l has been specified, we will not necessarily
2234 -- execute all steps.
2236 if Make_Steps then
2237 Do_Compile_Step := Do_Compile_Step and Compile_Only;
2238 Do_Bind_Step := Do_Bind_Step and Bind_Only;
2239 Do_Link_Step := Do_Link_Step and Link_Only;
2241 -- If -c has been specified, but not -b, ignore any potential -l
2243 if Do_Compile_Step and then not Do_Bind_Step then
2244 Do_Link_Step := False;
2245 end if;
2246 end if;
2247 end Check_Steps;
2249 -----------------------
2250 -- Collect_Arguments --
2251 -----------------------
2253 procedure Collect_Arguments
2254 (Source_File : File_Name_Type;
2255 Is_Main_Source : Boolean;
2256 Args : Argument_List)
2258 begin
2259 Arguments_Project := No_Project;
2260 Last_Argument := 0;
2261 Add_Arguments (Args);
2263 if Main_Project /= No_Project then
2264 declare
2265 Source_File_Name : constant String :=
2266 Get_Name_String (Source_File);
2267 Compiler_Package : Prj.Package_Id;
2268 Switches : Prj.Variable_Value;
2270 begin
2271 Prj.Env.
2272 Get_Reference
2273 (Source_File_Name => Source_File_Name,
2274 Project => Arguments_Project,
2275 Path => Arguments_Path_Name,
2276 In_Tree => Project_Tree);
2278 -- If the source is not a source of a project file, add the
2279 -- recorded arguments. Check will be done later if the source
2280 -- need to be compiled that the switch -x has been used.
2282 if Arguments_Project = No_Project then
2283 Add_Arguments (The_Saved_Gcc_Switches.all);
2285 elsif not Arguments_Project.Externally_Built
2286 or else Must_Compile
2287 then
2288 -- We get the project directory for the relative path
2289 -- switches and arguments.
2291 Arguments_Project :=
2292 Ultimate_Extending_Project_Of (Arguments_Project);
2294 -- If building a dynamic or relocatable library, compile with
2295 -- PIC option, if it exists.
2297 if Arguments_Project.Library
2298 and then Arguments_Project.Library_Kind /= Static
2299 then
2300 declare
2301 PIC : constant String := MLib.Tgt.PIC_Option;
2302 begin
2303 if PIC /= "" then
2304 Add_Arguments ((1 => new String'(PIC)));
2305 end if;
2306 end;
2307 end if;
2309 -- We now look for package Compiler and get the switches from
2310 -- this package.
2312 Compiler_Package :=
2313 Prj.Util.Value_Of
2314 (Name => Name_Compiler,
2315 In_Packages => Arguments_Project.Decl.Packages,
2316 Shared => Project_Tree.Shared);
2318 if Compiler_Package /= No_Package then
2320 -- If package Gnatmake.Compiler exists, we get the specific
2321 -- switches for the current source, or the global switches,
2322 -- if any.
2324 Switches :=
2325 Switches_Of
2326 (Source_File => Source_File,
2327 Project => Arguments_Project,
2328 In_Package => Compiler_Package,
2329 Allow_ALI => False);
2331 end if;
2333 case Switches.Kind is
2335 -- We have a list of switches. We add these switches,
2336 -- plus the saved gcc switches.
2338 when List =>
2340 declare
2341 Current : String_List_Id := Switches.Values;
2342 Element : String_Element;
2343 Number : Natural := 0;
2345 begin
2346 while Current /= Nil_String loop
2347 Element := Project_Tree.Shared.String_Elements.
2348 Table (Current);
2349 Number := Number + 1;
2350 Current := Element.Next;
2351 end loop;
2353 declare
2354 New_Args : Argument_List (1 .. Number);
2355 Last_New : Natural := 0;
2356 Dir_Path : constant String := Get_Name_String
2357 (Arguments_Project.Directory.Display_Name);
2359 begin
2360 Current := Switches.Values;
2362 for Index in New_Args'Range loop
2363 Element := Project_Tree.Shared.String_Elements.
2364 Table (Current);
2365 Get_Name_String (Element.Value);
2367 if Name_Len > 0 then
2368 Last_New := Last_New + 1;
2369 New_Args (Last_New) :=
2370 new String'(Name_Buffer (1 .. Name_Len));
2371 Ensure_Absolute_Path
2372 (New_Args (Last_New),
2373 Do_Fail => Make_Failed'Access,
2374 Parent => Dir_Path,
2375 Including_Non_Switch => False);
2376 end if;
2378 Current := Element.Next;
2379 end loop;
2381 Add_Arguments
2382 (Configuration_Pragmas_Switch (Arguments_Project)
2383 & New_Args (1 .. Last_New)
2384 & The_Saved_Gcc_Switches.all);
2385 end;
2386 end;
2388 -- We have a single switch. We add this switch,
2389 -- plus the saved gcc switches.
2391 when Single =>
2392 Get_Name_String (Switches.Value);
2394 declare
2395 New_Args : Argument_List :=
2396 (1 => new String'
2397 (Name_Buffer (1 .. Name_Len)));
2398 Dir_Path : constant String :=
2399 Get_Name_String
2400 (Arguments_Project.
2401 Directory.Display_Name);
2403 begin
2404 Ensure_Absolute_Path
2405 (New_Args (1),
2406 Do_Fail => Make_Failed'Access,
2407 Parent => Dir_Path,
2408 Including_Non_Switch => False);
2409 Add_Arguments
2410 (Configuration_Pragmas_Switch (Arguments_Project) &
2411 New_Args & The_Saved_Gcc_Switches.all);
2412 end;
2414 -- We have no switches from Gnatmake.Compiler.
2415 -- We add the saved gcc switches.
2417 when Undefined =>
2418 Add_Arguments
2419 (Configuration_Pragmas_Switch (Arguments_Project) &
2420 The_Saved_Gcc_Switches.all);
2421 end case;
2422 end if;
2423 end;
2424 end if;
2426 -- For VMS, when compiling the main source, add switch
2427 -- -mdebug-main=_ada_ so that the executable can be debugged
2428 -- by the standard VMS debugger.
2430 if not No_Main_Subprogram
2431 and then Targparm.OpenVMS_On_Target
2432 and then Is_Main_Source
2433 then
2434 -- First, check if compilation will be invoked with -g
2436 for J in 1 .. Last_Argument loop
2437 if Arguments (J)'Length >= 2
2438 and then Arguments (J) (1 .. 2) = "-g"
2439 and then (Arguments (J)'Length < 5
2440 or else Arguments (J) (1 .. 5) /= "-gnat")
2441 then
2442 Add_Arguments
2443 ((1 => new String'("-mdebug-main=_ada_")));
2444 exit;
2445 end if;
2446 end loop;
2447 end if;
2449 -- Set Output_Is_Object, depending if there is a -S switch.
2450 -- If the bind step is not performed, and there is a -S switch,
2451 -- then we will not check for a valid object file.
2453 Check_For_S_Switch;
2454 end Collect_Arguments;
2456 ---------------------
2457 -- Compile_Sources --
2458 ---------------------
2460 procedure Compile_Sources
2461 (Main_Source : File_Name_Type;
2462 Args : Argument_List;
2463 First_Compiled_File : out File_Name_Type;
2464 Most_Recent_Obj_File : out File_Name_Type;
2465 Most_Recent_Obj_Stamp : out Time_Stamp_Type;
2466 Main_Unit : out Boolean;
2467 Compilation_Failures : out Natural;
2468 Main_Index : Int := 0;
2469 Check_Readonly_Files : Boolean := False;
2470 Do_Not_Execute : Boolean := False;
2471 Force_Compilations : Boolean := False;
2472 Keep_Going : Boolean := False;
2473 In_Place_Mode : Boolean := False;
2474 Initialize_ALI_Data : Boolean := True;
2475 Max_Process : Positive := 1)
2477 Mfile : Natural := No_Mapping_File;
2478 Mapping_File_Arg : String_Access;
2479 -- Info on the mapping file
2481 Need_To_Check_Standard_Library : Boolean :=
2482 (Check_Readonly_Files or Must_Compile)
2483 and not Unique_Compile;
2485 procedure Add_Process
2486 (Pid : Process_Id;
2487 Sfile : File_Name_Type;
2488 Afile : File_Name_Type;
2489 Uname : Unit_Name_Type;
2490 Full_Lib_File : File_Name_Type;
2491 Lib_File_Attr : File_Attributes;
2492 Mfile : Natural := No_Mapping_File);
2493 -- Adds process Pid to the current list of outstanding compilation
2494 -- processes and record the full name of the source file Sfile that
2495 -- we are compiling, the name of its library file Afile and the
2496 -- name of its unit Uname. If Mfile is not equal to No_Mapping_File,
2497 -- it is the index of the mapping file used during compilation in the
2498 -- array The_Mapping_File_Names.
2500 procedure Await_Compile
2501 (Data : out Compilation_Data;
2502 OK : out Boolean);
2503 -- Awaits that an outstanding compilation process terminates. When it
2504 -- does set Data to the information registered for the corresponding
2505 -- call to Add_Process. Note that this time stamp can be used to check
2506 -- whether the compilation did generate an object file. OK is set to
2507 -- True if the compilation succeeded. Data could be No_Compilation_Data
2508 -- if there was no compilation to wait for.
2510 function Bad_Compilation_Count return Natural;
2511 -- Returns the number of compilation failures
2513 procedure Check_Standard_Library;
2514 -- Check if s-stalib.adb needs to be compiled
2516 procedure Collect_Arguments_And_Compile
2517 (Full_Source_File : File_Name_Type;
2518 Lib_File : File_Name_Type;
2519 Source_Index : Int;
2520 Pid : out Process_Id;
2521 Process_Created : out Boolean);
2522 -- Collect arguments from project file (if any) and compile. If no
2523 -- compilation was attempted, Processed_Created is set to False, and the
2524 -- value of Pid is unknown.
2526 function Compile
2527 (Project : Project_Id;
2528 S : File_Name_Type;
2529 L : File_Name_Type;
2530 Source_Index : Int;
2531 Args : Argument_List) return Process_Id;
2532 -- Compiles S using Args. If S is a GNAT predefined source "-gnatpg" is
2533 -- added to Args. Non blocking call. L corresponds to the expected
2534 -- library file name. Process_Id of the process spawned to execute the
2535 -- compilation.
2537 type ALI_Project is record
2538 ALI : ALI_Id;
2539 Project : Project_Id;
2540 end record;
2542 package Good_ALI is new Table.Table (
2543 Table_Component_Type => ALI_Project,
2544 Table_Index_Type => Natural,
2545 Table_Low_Bound => 1,
2546 Table_Initial => 50,
2547 Table_Increment => 100,
2548 Table_Name => "Make.Good_ALI");
2549 -- Contains the set of valid ALI files that have not yet been scanned
2551 function Good_ALI_Present return Boolean;
2552 -- Returns True if any ALI file was recorded in the previous set
2554 procedure Get_Mapping_File (Project : Project_Id);
2555 -- Get a mapping file name. If there is one to be reused, reuse it.
2556 -- Otherwise, create a new mapping file.
2558 function Get_Next_Good_ALI return ALI_Project;
2559 -- Returns the next good ALI_Id record
2561 procedure Record_Failure
2562 (File : File_Name_Type;
2563 Unit : Unit_Name_Type;
2564 Found : Boolean := True);
2565 -- Records in the previous table that the compilation for File failed.
2566 -- If Found is False then the compilation of File failed because we
2567 -- could not find it. Records also Unit when possible.
2569 procedure Record_Good_ALI (A : ALI_Id; Project : Project_Id);
2570 -- Records in the previous set the Id of an ALI file
2572 function Must_Exit_Because_Of_Error return Boolean;
2573 -- Return True if there were errors and the user decided to exit in such
2574 -- a case. This waits for any outstanding compilation.
2576 function Start_Compile_If_Possible (Args : Argument_List) return Boolean;
2577 -- Check if there is more work that we can do (i.e. the Queue is non
2578 -- empty). If there is, do it only if we have not yet used up all the
2579 -- available processes.
2580 -- Returns True if we should exit the main loop
2582 procedure Wait_For_Available_Slot;
2583 -- Check if we should wait for a compilation to finish. This is the case
2584 -- if all the available processes are busy compiling sources or there is
2585 -- nothing else to do (that is the Q is empty and there are no good ALIs
2586 -- to process).
2588 procedure Fill_Queue_From_ALI_Files;
2589 -- Check if we recorded good ALI files. If yes process them now in the
2590 -- order in which they have been recorded. There are two occasions in
2591 -- which we record good ali files. The first is in phase 1 when, after
2592 -- scanning an existing ALI file we realize it is up-to-date, the second
2593 -- instance is after a successful compilation.
2595 -----------------
2596 -- Add_Process --
2597 -----------------
2599 procedure Add_Process
2600 (Pid : Process_Id;
2601 Sfile : File_Name_Type;
2602 Afile : File_Name_Type;
2603 Uname : Unit_Name_Type;
2604 Full_Lib_File : File_Name_Type;
2605 Lib_File_Attr : File_Attributes;
2606 Mfile : Natural := No_Mapping_File)
2608 OC1 : constant Positive := Outstanding_Compiles + 1;
2610 begin
2611 pragma Assert (OC1 <= Max_Process);
2612 pragma Assert (Pid /= Invalid_Pid);
2614 Running_Compile (OC1) :=
2615 (Pid => Pid,
2616 Full_Source_File => Sfile,
2617 Lib_File => Afile,
2618 Full_Lib_File => Full_Lib_File,
2619 Lib_File_Attr => Lib_File_Attr,
2620 Source_Unit => Uname,
2621 Mapping_File => Mfile,
2622 Project => Arguments_Project);
2624 Outstanding_Compiles := OC1;
2626 if Arguments_Project /= No_Project then
2627 Queue.Set_Obj_Dir_Busy (Arguments_Project.Object_Directory.Name);
2628 end if;
2629 end Add_Process;
2631 --------------------
2632 -- Await_Compile --
2633 -------------------
2635 procedure Await_Compile
2636 (Data : out Compilation_Data;
2637 OK : out Boolean)
2639 Pid : Process_Id;
2640 Project : Project_Id;
2641 Comp_Data : Project_Compilation_Access;
2643 begin
2644 pragma Assert (Outstanding_Compiles > 0);
2646 Data := No_Compilation_Data;
2647 OK := False;
2649 -- The loop here is a work-around for a problem on VMS; in some
2650 -- circumstances (shared library and several executables, for
2651 -- example), there are child processes other than compilation
2652 -- processes that are received. Until this problem is resolved,
2653 -- we will ignore such processes.
2655 loop
2656 Wait_Process (Pid, OK);
2658 if Pid = Invalid_Pid then
2659 return;
2660 end if;
2662 for J in Running_Compile'First .. Outstanding_Compiles loop
2663 if Pid = Running_Compile (J).Pid then
2664 Data := Running_Compile (J);
2665 Project := Running_Compile (J).Project;
2667 if Project /= No_Project then
2668 Queue.Set_Obj_Dir_Free (Project.Object_Directory.Name);
2669 end if;
2671 -- If a mapping file was used by this compilation, get its
2672 -- file name for reuse by a subsequent compilation.
2674 if Running_Compile (J).Mapping_File /= No_Mapping_File then
2675 Comp_Data :=
2676 Project_Compilation_Htable.Get
2677 (Project_Compilation, Project);
2678 Comp_Data.Last_Free_Indexes :=
2679 Comp_Data.Last_Free_Indexes + 1;
2680 Comp_Data.Free_Mapping_File_Indexes
2681 (Comp_Data.Last_Free_Indexes) :=
2682 Running_Compile (J).Mapping_File;
2683 end if;
2685 -- To actually remove this Pid and related info from
2686 -- Running_Compile replace its entry with the last valid
2687 -- entry in Running_Compile.
2689 if J = Outstanding_Compiles then
2690 null;
2691 else
2692 Running_Compile (J) :=
2693 Running_Compile (Outstanding_Compiles);
2694 end if;
2696 Outstanding_Compiles := Outstanding_Compiles - 1;
2697 return;
2698 end if;
2699 end loop;
2701 -- This child process was not one of our compilation processes;
2702 -- just ignore it for now.
2704 -- Why is this commented out code sitting here???
2706 -- raise Program_Error;
2707 end loop;
2708 end Await_Compile;
2710 ---------------------------
2711 -- Bad_Compilation_Count --
2712 ---------------------------
2714 function Bad_Compilation_Count return Natural is
2715 begin
2716 return Bad_Compilation.Last - Bad_Compilation.First + 1;
2717 end Bad_Compilation_Count;
2719 ----------------------------
2720 -- Check_Standard_Library --
2721 ----------------------------
2723 procedure Check_Standard_Library is
2724 begin
2725 Need_To_Check_Standard_Library := False;
2727 if not Targparm.Suppress_Standard_Library_On_Target then
2728 declare
2729 Sfile : File_Name_Type;
2730 Add_It : Boolean := True;
2732 begin
2733 Name_Len := 0;
2734 Add_Str_To_Name_Buffer (Standard_Library_Package_Body_Name);
2735 Sfile := Name_Enter;
2737 -- If we have a special runtime, we add the standard
2738 -- library only if we can find it.
2740 if RTS_Switch then
2741 Add_It := Full_Source_Name (Sfile) /= No_File;
2742 end if;
2744 if Add_It then
2745 if not Queue.Insert
2746 ((Format => Format_Gnatmake,
2747 File => Sfile,
2748 Unit => No_Unit_Name,
2749 Project => No_Project,
2750 Index => 0,
2751 Sid => No_Source))
2752 then
2753 if Is_In_Obsoleted (Sfile) then
2754 Executable_Obsolete := True;
2755 end if;
2756 end if;
2757 end if;
2758 end;
2759 end if;
2760 end Check_Standard_Library;
2762 -----------------------------------
2763 -- Collect_Arguments_And_Compile --
2764 -----------------------------------
2766 procedure Collect_Arguments_And_Compile
2767 (Full_Source_File : File_Name_Type;
2768 Lib_File : File_Name_Type;
2769 Source_Index : Int;
2770 Pid : out Process_Id;
2771 Process_Created : out Boolean) is
2772 begin
2773 Process_Created := False;
2775 -- If we use mapping file (-P or -C switches), then get one
2777 if Create_Mapping_File then
2778 Get_Mapping_File (Arguments_Project);
2779 end if;
2781 -- If the source is part of a project file, we set the ADA_*_PATHs,
2782 -- check for an eventual library project, and use the full path.
2784 if Arguments_Project /= No_Project then
2785 if not Arguments_Project.Externally_Built
2786 or else Must_Compile
2787 then
2788 Prj.Env.Set_Ada_Paths
2789 (Arguments_Project,
2790 Project_Tree,
2791 Including_Libraries => True,
2792 Include_Path => Use_Include_Path_File);
2794 if not Unique_Compile
2795 and then MLib.Tgt.Support_For_Libraries /= Prj.None
2796 then
2797 declare
2798 Prj : constant Project_Id :=
2799 Ultimate_Extending_Project_Of (Arguments_Project);
2801 begin
2802 if Prj.Library
2803 and then (not Prj.Externally_Built or else Must_Compile)
2804 and then not Prj.Need_To_Build_Lib
2805 then
2806 -- Add to the Q all sources of the project that have
2807 -- not been marked.
2809 Insert_Project_Sources
2810 (The_Project => Prj,
2811 All_Projects => False,
2812 Into_Q => True);
2814 -- Now mark the project as processed
2816 Prj.Need_To_Build_Lib := True;
2817 end if;
2818 end;
2819 end if;
2821 Pid :=
2822 Compile
2823 (Project => Arguments_Project,
2824 S => File_Name_Type (Arguments_Path_Name),
2825 L => Lib_File,
2826 Source_Index => Source_Index,
2827 Args => Arguments (1 .. Last_Argument));
2828 Process_Created := True;
2829 end if;
2831 else
2832 -- If this is a source outside of any project file, make sure it
2833 -- will be compiled in object directory of the main project file.
2835 Pid :=
2836 Compile
2837 (Project => Main_Project,
2838 S => Full_Source_File,
2839 L => Lib_File,
2840 Source_Index => Source_Index,
2841 Args => Arguments (1 .. Last_Argument));
2842 Process_Created := True;
2843 end if;
2844 end Collect_Arguments_And_Compile;
2846 -------------
2847 -- Compile --
2848 -------------
2850 function Compile
2851 (Project : Project_Id;
2852 S : File_Name_Type;
2853 L : File_Name_Type;
2854 Source_Index : Int;
2855 Args : Argument_List) return Process_Id
2857 Comp_Args : Argument_List (Args'First .. Args'Last + 10);
2858 Comp_Next : Integer := Args'First;
2859 Comp_Last : Integer;
2860 Arg_Index : Integer;
2862 function Ada_File_Name (Name : File_Name_Type) return Boolean;
2863 -- Returns True if Name is the name of an ada source file
2864 -- (i.e. suffix is .ads or .adb)
2866 -------------------
2867 -- Ada_File_Name --
2868 -------------------
2870 function Ada_File_Name (Name : File_Name_Type) return Boolean is
2871 begin
2872 Get_Name_String (Name);
2873 return
2874 Name_Len > 4
2875 and then Name_Buffer (Name_Len - 3 .. Name_Len - 1) = ".ad"
2876 and then (Name_Buffer (Name_Len) = 'b'
2877 or else
2878 Name_Buffer (Name_Len) = 's');
2879 end Ada_File_Name;
2881 -- Start of processing for Compile
2883 begin
2884 Enter_Into_Obsoleted (S);
2886 -- By default, Syntax_Only is False
2888 Syntax_Only := False;
2890 for J in Args'Range loop
2891 if Args (J).all = "-gnats" then
2893 -- If we compile with -gnats, the bind step and the link step
2894 -- are inhibited. Also, we set Syntax_Only to True, so that
2895 -- we don't fail when we don't find the ALI file, after
2896 -- compilation.
2898 Do_Bind_Step := False;
2899 Do_Link_Step := False;
2900 Syntax_Only := True;
2902 elsif Args (J).all = "-gnatc" then
2904 -- If we compile with -gnatc, the bind step and the link step
2905 -- are inhibited. We set Syntax_Only to False for the case when
2906 -- -gnats was previously specified.
2908 Do_Bind_Step := False;
2909 Do_Link_Step := False;
2910 Syntax_Only := False;
2911 end if;
2912 end loop;
2914 Comp_Args (Comp_Next) := new String'("-gnatea");
2915 Comp_Next := Comp_Next + 1;
2917 Comp_Args (Comp_Next) := Comp_Flag;
2918 Comp_Next := Comp_Next + 1;
2920 -- Optimize the simple case where the gcc command line looks like
2921 -- gcc -c -I. ... -I- file.adb
2922 -- into
2923 -- gcc -c ... file.adb
2925 if Args (Args'First).all = "-I" & Normalized_CWD
2926 and then Args (Args'Last).all = "-I-"
2927 and then S = Strip_Directory (S)
2928 then
2929 Comp_Last := Comp_Next + Args'Length - 3;
2930 Arg_Index := Args'First + 1;
2932 else
2933 Comp_Last := Comp_Next + Args'Length - 1;
2934 Arg_Index := Args'First;
2935 end if;
2937 -- Make a deep copy of the arguments, because Normalize_Arguments
2938 -- may deallocate some arguments. Also strip target specific -mxxx
2939 -- switches in CodePeer mode.
2941 declare
2942 Index : Natural;
2943 Last : constant Natural := Comp_Last;
2945 begin
2946 Index := Comp_Next;
2947 for J in Comp_Next .. Last loop
2948 declare
2949 Str : String renames Args (Arg_Index).all;
2950 begin
2951 if CodePeer_Mode
2952 and then Str'Length > 2
2953 and then Str (Str'First .. Str'First + 1) = "-m"
2954 then
2955 Comp_Last := Comp_Last - 1;
2956 else
2957 Comp_Args (Index) := new String'(Str);
2958 Index := Index + 1;
2959 end if;
2960 end;
2962 Arg_Index := Arg_Index + 1;
2963 end loop;
2964 end;
2966 -- Set -gnatpg for predefined files (for this purpose the renamings
2967 -- such as Text_IO do not count as predefined). Note that we strip
2968 -- the directory name from the source file name because the call to
2969 -- Fname.Is_Predefined_File_Name cannot deal with directory prefixes.
2971 declare
2972 Fname : constant File_Name_Type := Strip_Directory (S);
2974 begin
2975 if Is_Predefined_File_Name (Fname, False) then
2976 if Check_Readonly_Files or else Must_Compile then
2977 Comp_Args (Comp_Args'First + 2 .. Comp_Last + 1) :=
2978 Comp_Args (Comp_Args'First + 1 .. Comp_Last);
2979 Comp_Last := Comp_Last + 1;
2980 Comp_Args (Comp_Args'First + 1) := GNAT_Flag;
2982 else
2983 Make_Failed
2984 ("not allowed to compile """ &
2985 Get_Name_String (Fname) &
2986 """; use -a switch, or use the compiler directly with "
2987 & "the ""-gnatg"" switch");
2988 end if;
2989 end if;
2990 end;
2992 -- Now check if the file name has one of the suffixes familiar to
2993 -- the gcc driver. If this is not the case then add the ada flag
2994 -- "-x ada".
2995 -- Append systematically "-x adascil" in CodePeer mode instead, to
2996 -- force the use of gnat1scil instead of gnat1.
2998 if CodePeer_Mode then
2999 Comp_Last := Comp_Last + 1;
3000 Comp_Args (Comp_Last) := Ada_Flag_1;
3001 Comp_Last := Comp_Last + 1;
3002 Comp_Args (Comp_Last) := AdaSCIL_Flag;
3004 elsif not Ada_File_Name (S) and then not Targparm.AAMP_On_Target then
3005 Comp_Last := Comp_Last + 1;
3006 Comp_Args (Comp_Last) := Ada_Flag_1;
3007 Comp_Last := Comp_Last + 1;
3008 Comp_Args (Comp_Last) := Ada_Flag_2;
3009 end if;
3011 if Source_Index /= 0 then
3012 declare
3013 Num : constant String := Source_Index'Img;
3014 begin
3015 Comp_Last := Comp_Last + 1;
3016 Comp_Args (Comp_Last) :=
3017 new String'("-gnateI" & Num (Num'First + 1 .. Num'Last));
3018 end;
3019 end if;
3021 if Source_Index /= 0
3022 or else L /= Strip_Directory (L)
3023 or else Object_Directory_Path /= null
3024 then
3025 -- Build -o argument
3027 Get_Name_String (L);
3029 for J in reverse 1 .. Name_Len loop
3030 if Name_Buffer (J) = '.' then
3031 Name_Len := J + Object_Suffix'Length - 1;
3032 Name_Buffer (J .. Name_Len) := Object_Suffix;
3033 exit;
3034 end if;
3035 end loop;
3037 Comp_Last := Comp_Last + 1;
3038 Comp_Args (Comp_Last) := Output_Flag;
3039 Comp_Last := Comp_Last + 1;
3041 -- If an object directory was specified, prepend the object file
3042 -- name with this object directory.
3044 if Object_Directory_Path /= null then
3045 Comp_Args (Comp_Last) :=
3046 new String'(Object_Directory_Path.all &
3047 Name_Buffer (1 .. Name_Len));
3049 else
3050 Comp_Args (Comp_Last) :=
3051 new String'(Name_Buffer (1 .. Name_Len));
3052 end if;
3053 end if;
3055 if Create_Mapping_File and then Mapping_File_Arg /= null then
3056 Comp_Last := Comp_Last + 1;
3057 Comp_Args (Comp_Last) := new String'(Mapping_File_Arg.all);
3058 end if;
3060 Get_Name_String (S);
3062 Comp_Last := Comp_Last + 1;
3063 Comp_Args (Comp_Last) := new String'(Name_Buffer (1 .. Name_Len));
3065 -- Change to object directory of the project file, if necessary
3067 if Project /= No_Project then
3068 Change_To_Object_Directory (Project);
3069 end if;
3071 GNAT.OS_Lib.Normalize_Arguments (Comp_Args (Args'First .. Comp_Last));
3073 Comp_Last := Comp_Last + 1;
3074 Comp_Args (Comp_Last) := new String'("-gnatez");
3076 Display (Gcc.all, Comp_Args (Args'First .. Comp_Last));
3078 if Gcc_Path = null then
3079 Make_Failed ("error, unable to locate " & Gcc.all);
3080 end if;
3082 return
3083 GNAT.OS_Lib.Non_Blocking_Spawn
3084 (Gcc_Path.all, Comp_Args (Args'First .. Comp_Last));
3085 end Compile;
3087 -------------------------------
3088 -- Fill_Queue_From_ALI_Files --
3089 -------------------------------
3091 procedure Fill_Queue_From_ALI_Files is
3092 ALI_P : ALI_Project;
3093 ALI : ALI_Id;
3094 Source_Index : Int;
3095 Sfile : File_Name_Type;
3096 Sid : Prj.Source_Id;
3097 Uname : Unit_Name_Type;
3098 Unit_Name : Name_Id;
3099 Uid : Prj.Unit_Index;
3101 begin
3102 while Good_ALI_Present loop
3103 ALI_P := Get_Next_Good_ALI;
3104 ALI := ALI_P.ALI;
3105 Source_Index := Unit_Index_Of (ALIs.Table (ALI_P.ALI).Afile);
3107 -- If we are processing the library file corresponding to the
3108 -- main source file check if this source can be a main unit.
3110 if ALIs.Table (ALI).Sfile = Main_Source
3111 and then Source_Index = Main_Index
3112 then
3113 Main_Unit := ALIs.Table (ALI).Main_Program /= None;
3114 end if;
3116 -- The following adds the standard library (s-stalib) to the list
3117 -- of files to be handled by gnatmake: this file and any files it
3118 -- depends on are always included in every bind, even if they are
3119 -- not in the explicit dependency list. Of course, it is not added
3120 -- if Suppress_Standard_Library is True.
3122 -- However, to avoid annoying output about s-stalib.ali being read
3123 -- only, when "-v" is used, we add the standard library only when
3124 -- "-a" is used.
3126 if Need_To_Check_Standard_Library then
3127 Check_Standard_Library;
3128 end if;
3130 -- Now insert in the Q the unmarked source files (i.e. those which
3131 -- have never been inserted in the Q and hence never considered).
3132 -- Only do that if Unique_Compile is False.
3134 if not Unique_Compile then
3135 for J in
3136 ALIs.Table (ALI).First_Unit .. ALIs.Table (ALI).Last_Unit
3137 loop
3138 for K in
3139 Units.Table (J).First_With .. Units.Table (J).Last_With
3140 loop
3141 Sfile := Withs.Table (K).Sfile;
3142 Uname := Withs.Table (K).Uname;
3143 Sid := No_Source;
3145 -- If project files are used, find the proper source to
3146 -- compile in case Sfile is the spec but there is a body.
3148 if Main_Project /= No_Project then
3149 Get_Name_String (Uname);
3150 Name_Len := Name_Len - 2;
3151 Unit_Name := Name_Find;
3152 Uid :=
3153 Units_Htable.Get (Project_Tree.Units_HT, Unit_Name);
3155 if Uid /= Prj.No_Unit_Index then
3156 if Uid.File_Names (Impl) /= null
3157 and then not Uid.File_Names (Impl).Locally_Removed
3158 then
3159 Sfile := Uid.File_Names (Impl).File;
3160 Source_Index := Uid.File_Names (Impl).Index;
3161 Sid := Uid.File_Names (Impl);
3163 elsif Uid.File_Names (Spec) /= null
3164 and then not Uid.File_Names (Spec).Locally_Removed
3165 then
3166 Sfile := Uid.File_Names (Spec).File;
3167 Source_Index := Uid.File_Names (Spec).Index;
3168 Sid := Uid.File_Names (Spec);
3169 end if;
3170 end if;
3171 end if;
3173 Dependencies.Append ((ALIs.Table (ALI).Sfile, Sfile));
3175 if Is_In_Obsoleted (Sfile) then
3176 Executable_Obsolete := True;
3177 end if;
3179 if Sfile = No_File then
3180 Debug_Msg ("Skipping generic:", Withs.Table (K).Uname);
3182 else
3183 Source_Index := Unit_Index_Of (Withs.Table (K).Afile);
3185 if not (Check_Readonly_Files or Must_Compile)
3186 and then Is_Internal_File_Name (Sfile, False)
3187 then
3188 Debug_Msg ("Skipping internal file:", Sfile);
3190 else
3191 Queue.Insert
3192 ((Format => Format_Gnatmake,
3193 File => Sfile,
3194 Project => ALI_P.Project,
3195 Unit => Withs.Table (K).Uname,
3196 Index => Source_Index,
3197 Sid => Sid));
3198 end if;
3199 end if;
3200 end loop;
3201 end loop;
3202 end if;
3203 end loop;
3204 end Fill_Queue_From_ALI_Files;
3206 ----------------------
3207 -- Get_Mapping_File --
3208 ----------------------
3210 procedure Get_Mapping_File (Project : Project_Id) is
3211 Data : Project_Compilation_Access;
3213 begin
3214 Data := Project_Compilation_Htable.Get (Project_Compilation, Project);
3216 -- If there is a mapping file ready to be reused, reuse it
3218 if Data.Last_Free_Indexes > 0 then
3219 Mfile := Data.Free_Mapping_File_Indexes (Data.Last_Free_Indexes);
3220 Data.Last_Free_Indexes := Data.Last_Free_Indexes - 1;
3222 -- Otherwise, create and initialize a new one
3224 else
3225 Init_Mapping_File
3226 (Project => Project, Data => Data.all, File_Index => Mfile);
3227 end if;
3229 -- Put the name in the mapping file argument for the invocation
3230 -- of the compiler.
3232 Free (Mapping_File_Arg);
3233 Mapping_File_Arg :=
3234 new String'("-gnatem=" &
3235 Get_Name_String (Data.Mapping_File_Names (Mfile)));
3236 end Get_Mapping_File;
3238 -----------------------
3239 -- Get_Next_Good_ALI --
3240 -----------------------
3242 function Get_Next_Good_ALI return ALI_Project is
3243 ALIP : ALI_Project;
3245 begin
3246 pragma Assert (Good_ALI_Present);
3247 ALIP := Good_ALI.Table (Good_ALI.Last);
3248 Good_ALI.Decrement_Last;
3249 return ALIP;
3250 end Get_Next_Good_ALI;
3252 ----------------------
3253 -- Good_ALI_Present --
3254 ----------------------
3256 function Good_ALI_Present return Boolean is
3257 begin
3258 return Good_ALI.First <= Good_ALI.Last;
3259 end Good_ALI_Present;
3261 --------------------------------
3262 -- Must_Exit_Because_Of_Error --
3263 --------------------------------
3265 function Must_Exit_Because_Of_Error return Boolean is
3266 Data : Compilation_Data;
3267 Success : Boolean;
3269 begin
3270 if Bad_Compilation_Count > 0 and then not Keep_Going then
3271 while Outstanding_Compiles > 0 loop
3272 Await_Compile (Data, Success);
3274 if not Success then
3275 Record_Failure (Data.Full_Source_File, Data.Source_Unit);
3276 end if;
3277 end loop;
3279 return True;
3280 end if;
3282 return False;
3283 end Must_Exit_Because_Of_Error;
3285 --------------------
3286 -- Record_Failure --
3287 --------------------
3289 procedure Record_Failure
3290 (File : File_Name_Type;
3291 Unit : Unit_Name_Type;
3292 Found : Boolean := True)
3294 begin
3295 Bad_Compilation.Increment_Last;
3296 Bad_Compilation.Table (Bad_Compilation.Last) := (File, Unit, Found);
3297 end Record_Failure;
3299 ---------------------
3300 -- Record_Good_ALI --
3301 ---------------------
3303 procedure Record_Good_ALI (A : ALI_Id; Project : Project_Id) is
3304 begin
3305 Good_ALI.Increment_Last;
3306 Good_ALI.Table (Good_ALI.Last) := (A, Project);
3307 end Record_Good_ALI;
3309 -------------------------------
3310 -- Start_Compile_If_Possible --
3311 -------------------------------
3313 function Start_Compile_If_Possible
3314 (Args : Argument_List) return Boolean
3316 In_Lib_Dir : Boolean;
3317 Need_To_Compile : Boolean;
3318 Pid : Process_Id := Invalid_Pid;
3319 Process_Created : Boolean;
3321 Source : Queue.Source_Info;
3322 Full_Source_File : File_Name_Type := No_File;
3323 Source_File_Attr : aliased File_Attributes;
3324 -- The full name of the source file and its attributes (size, ...)
3326 Lib_File : File_Name_Type;
3327 Full_Lib_File : File_Name_Type := No_File;
3328 Lib_File_Attr : aliased File_Attributes;
3329 Read_Only : Boolean := False;
3330 ALI : ALI_Id;
3331 -- The ALI file and its attributes (size, stamp, ...)
3333 Obj_File : File_Name_Type;
3334 Obj_Stamp : Time_Stamp_Type;
3335 -- The object file
3337 Found : Boolean;
3339 begin
3340 if not Queue.Is_Virtually_Empty and then
3341 Outstanding_Compiles < Max_Process
3342 then
3343 Queue.Extract (Found, Source);
3345 -- If it is a source in a project, first look for the ALI file
3346 -- in the object directory. When the project is extending another
3347 -- the ALI file may not be found, but the source does not
3348 -- necessarily need to be compiled, as it may already be up to
3349 -- date in the project being extended. In this case, look for an
3350 -- ALI file in all the object directories, as is done when
3351 -- gnatmake is not invoked with a project file.
3353 if Source.Sid /= No_Source then
3354 Initialize_Source_Record (Source.Sid);
3355 Full_Source_File :=
3356 File_Name_Type (Source.Sid.Path.Display_Name);
3357 Lib_File := Source.Sid.Dep_Name;
3358 Full_Lib_File := File_Name_Type (Source.Sid.Dep_Path);
3359 Lib_File_Attr := Unknown_Attributes;
3361 if Full_Lib_File /= No_File then
3362 declare
3363 FLF : constant String :=
3364 Get_Name_String (Full_Lib_File) & ASCII.NUL;
3365 begin
3366 if not Is_Regular_File
3367 (FLF'Address, Lib_File_Attr'Access)
3368 then
3369 Full_Lib_File := No_File;
3370 end if;
3371 end;
3372 end if;
3373 end if;
3375 if Full_Lib_File = No_File then
3376 Osint.Full_Source_Name
3377 (Source.File,
3378 Full_File => Full_Source_File,
3379 Attr => Source_File_Attr'Access);
3381 Lib_File := Osint.Lib_File_Name (Source.File, Source.Index);
3383 Osint.Full_Lib_File_Name
3384 (Lib_File,
3385 Lib_File => Full_Lib_File,
3386 Attr => Lib_File_Attr);
3387 end if;
3389 -- If source has already been compiled, executable is obsolete
3391 if Is_In_Obsoleted (Source.File) then
3392 Executable_Obsolete := True;
3393 end if;
3395 In_Lib_Dir := Full_Lib_File /= No_File
3396 and then In_Ada_Lib_Dir (Full_Lib_File);
3398 -- Since the following requires a system call, we precompute it
3399 -- when needed.
3401 if not In_Lib_Dir then
3402 if Full_Lib_File /= No_File
3403 and then not (Check_Readonly_Files or else Must_Compile)
3404 then
3405 Get_Name_String (Full_Lib_File);
3406 Name_Buffer (Name_Len + 1) := ASCII.NUL;
3407 Read_Only := not Is_Writable_File
3408 (Name_Buffer'Address, Lib_File_Attr'Access);
3409 else
3410 Read_Only := False;
3411 end if;
3412 end if;
3414 -- If the library file is an Ada library skip it
3416 if In_Lib_Dir then
3417 Verbose_Msg
3418 (Lib_File,
3419 "is in an Ada library",
3420 Prefix => " ",
3421 Minimum_Verbosity => Opt.High);
3423 -- If the library file is a read-only library skip it, but only
3424 -- if, when using project files, this library file is in the
3425 -- right object directory (a read-only ALI file in the object
3426 -- directory of a project being extended must not be skipped).
3428 elsif Read_Only
3429 and then Is_In_Object_Directory (Source.File, Full_Lib_File)
3430 then
3431 Verbose_Msg
3432 (Lib_File,
3433 "is a read-only library",
3434 Prefix => " ",
3435 Minimum_Verbosity => Opt.High);
3437 -- The source file that we are checking cannot be located
3439 elsif Full_Source_File = No_File then
3440 Record_Failure (Source.File, Source.Unit, False);
3442 -- Source and library files can be located but are internal
3443 -- files.
3445 elsif not (Check_Readonly_Files or else Must_Compile)
3446 and then Full_Lib_File /= No_File
3447 and then Is_Internal_File_Name (Source.File, False)
3448 then
3449 if Force_Compilations then
3450 Fail
3451 ("not allowed to compile """ &
3452 Get_Name_String (Source.File) &
3453 """; use -a switch, or use the compiler directly with "
3454 & "the ""-gnatg"" switch");
3455 end if;
3457 Verbose_Msg
3458 (Lib_File,
3459 "is an internal library",
3460 Prefix => " ",
3461 Minimum_Verbosity => Opt.High);
3463 -- The source file that we are checking can be located
3465 else
3466 Collect_Arguments
3467 (Source.File, Source.File = Main_Source, Args);
3469 -- Do nothing if project of source is externally built
3471 if Arguments_Project = No_Project
3472 or else not Arguments_Project.Externally_Built
3473 or else Must_Compile
3474 then
3475 -- Don't waste any time if we have to recompile anyway
3477 Obj_Stamp := Empty_Time_Stamp;
3478 Need_To_Compile := Force_Compilations;
3480 if not Force_Compilations then
3481 Check (Source_File => Source.File,
3482 Is_Main_Source => Source.File = Main_Source,
3483 The_Args => Args,
3484 Lib_File => Lib_File,
3485 Full_Lib_File => Full_Lib_File,
3486 Lib_File_Attr => Lib_File_Attr'Access,
3487 Read_Only => Read_Only,
3488 ALI => ALI,
3489 O_File => Obj_File,
3490 O_Stamp => Obj_Stamp);
3491 Need_To_Compile := (ALI = No_ALI_Id);
3492 end if;
3494 if not Need_To_Compile then
3496 -- The ALI file is up-to-date; record its Id
3498 Record_Good_ALI (ALI, Arguments_Project);
3500 -- Record the time stamp of the most recent object
3501 -- file as long as no (re)compilations are needed.
3503 if First_Compiled_File = No_File
3504 and then (Most_Recent_Obj_File = No_File
3505 or else Obj_Stamp > Most_Recent_Obj_Stamp)
3506 then
3507 Most_Recent_Obj_File := Obj_File;
3508 Most_Recent_Obj_Stamp := Obj_Stamp;
3509 end if;
3511 else
3512 -- Check that switch -x has been used if a source outside
3513 -- of project files need to be compiled.
3515 if Main_Project /= No_Project
3516 and then Arguments_Project = No_Project
3517 and then not External_Unit_Compilation_Allowed
3518 then
3519 Make_Failed ("external source ("
3520 & Get_Name_String (Source.File)
3521 & ") is not part of any project;"
3522 & " cannot be compiled without"
3523 & " gnatmake switch -x");
3524 end if;
3526 -- Is this the first file we have to compile?
3528 if First_Compiled_File = No_File then
3529 First_Compiled_File := Full_Source_File;
3530 Most_Recent_Obj_File := No_File;
3532 if Do_Not_Execute then
3534 -- Exit the main loop
3536 return True;
3537 end if;
3538 end if;
3540 -- Compute where the ALI file must be generated in
3541 -- In_Place_Mode (this does not require to know the
3542 -- location of the object directory).
3544 if In_Place_Mode then
3545 if Full_Lib_File = No_File then
3547 -- If the library file was not found, then save
3548 -- the library file near the source file.
3550 Lib_File :=
3551 Osint.Lib_File_Name
3552 (Full_Source_File, Source.Index);
3553 Full_Lib_File := Lib_File;
3555 else
3556 -- If the library file was found, then save the
3557 -- library file in the same place.
3559 Lib_File := Full_Lib_File;
3560 end if;
3561 end if;
3563 -- Start the compilation and record it. We can do this
3564 -- because there is at least one free process. This might
3565 -- change the current directory.
3567 Collect_Arguments_And_Compile
3568 (Full_Source_File => Full_Source_File,
3569 Lib_File => Lib_File,
3570 Source_Index => Source.Index,
3571 Pid => Pid,
3572 Process_Created => Process_Created);
3574 -- Compute where the ALI file will be generated (for
3575 -- cases that might require to know the current
3576 -- directory). The current directory might be changed
3577 -- when compiling other files so we cannot rely on it
3578 -- being the same to find the resulting ALI file.
3580 if not In_Place_Mode then
3582 -- Compute the expected location of the ALI file. This
3583 -- can be from several places:
3584 -- -i => in place mode. In such a case,
3585 -- Full_Lib_File has already been set above
3586 -- -D => if specified
3587 -- or defaults in current dir
3588 -- We could simply use a call similar to
3589 -- Osint.Full_Lib_File_Name (Lib_File)
3590 -- but that involves system calls and is thus slower
3592 if Object_Directory_Path /= null then
3593 Name_Len := 0;
3594 Add_Str_To_Name_Buffer (Object_Directory_Path.all);
3595 Add_Str_To_Name_Buffer (Get_Name_String (Lib_File));
3596 Full_Lib_File := Name_Find;
3598 else
3599 if Project_Of_Current_Object_Directory /=
3600 No_Project
3601 then
3602 Get_Name_String
3603 (Project_Of_Current_Object_Directory
3604 .Object_Directory.Display_Name);
3605 Add_Str_To_Name_Buffer
3606 (Get_Name_String (Lib_File));
3607 Full_Lib_File := Name_Find;
3609 else
3610 Full_Lib_File := Lib_File;
3611 end if;
3612 end if;
3614 end if;
3616 Lib_File_Attr := Unknown_Attributes;
3618 -- Make sure we could successfully start the compilation
3620 if Process_Created then
3621 if Pid = Invalid_Pid then
3622 Record_Failure (Full_Source_File, Source.Unit);
3623 else
3624 Add_Process
3625 (Pid => Pid,
3626 Sfile => Full_Source_File,
3627 Afile => Lib_File,
3628 Uname => Source.Unit,
3629 Mfile => Mfile,
3630 Full_Lib_File => Full_Lib_File,
3631 Lib_File_Attr => Lib_File_Attr);
3632 end if;
3633 end if;
3634 end if;
3635 end if;
3636 end if;
3637 end if;
3638 return False;
3639 end Start_Compile_If_Possible;
3641 -----------------------------
3642 -- Wait_For_Available_Slot --
3643 -----------------------------
3645 procedure Wait_For_Available_Slot is
3646 Compilation_OK : Boolean;
3647 Text : Text_Buffer_Ptr;
3648 ALI : ALI_Id;
3649 Data : Compilation_Data;
3651 begin
3652 if Outstanding_Compiles = Max_Process
3653 or else (Queue.Is_Virtually_Empty
3654 and then not Good_ALI_Present
3655 and then Outstanding_Compiles > 0)
3656 then
3657 Await_Compile (Data, Compilation_OK);
3659 if not Compilation_OK then
3660 Record_Failure (Data.Full_Source_File, Data.Source_Unit);
3661 end if;
3663 if Compilation_OK or else Keep_Going then
3665 -- Re-read the updated library file
3667 declare
3668 Saved_Object_Consistency : constant Boolean :=
3669 Check_Object_Consistency;
3671 begin
3672 -- If compilation was not OK, or if output is not an object
3673 -- file and we don't do the bind step, don't check for
3674 -- object consistency.
3676 Check_Object_Consistency :=
3677 Check_Object_Consistency
3678 and Compilation_OK
3679 and (Output_Is_Object or Do_Bind_Step);
3681 Text :=
3682 Read_Library_Info_From_Full
3683 (Data.Full_Lib_File, Data.Lib_File_Attr'Access);
3685 -- Restore Check_Object_Consistency to its initial value
3687 Check_Object_Consistency := Saved_Object_Consistency;
3688 end;
3690 -- If an ALI file was generated by this compilation, scan the
3691 -- ALI file and record it.
3693 -- If the scan fails, a previous ali file is inconsistent with
3694 -- the unit just compiled.
3696 if Text /= null then
3697 ALI :=
3698 Scan_ALI
3699 (Data.Lib_File, Text, Ignore_ED => False, Err => True);
3701 if ALI = No_ALI_Id then
3703 -- Record a failure only if not already done
3705 if Compilation_OK then
3706 Inform
3707 (Data.Lib_File,
3708 "incompatible ALI file, please recompile");
3709 Record_Failure
3710 (Data.Full_Source_File, Data.Source_Unit);
3711 end if;
3713 else
3714 Record_Good_ALI (ALI, Data.Project);
3715 end if;
3717 Free (Text);
3719 -- If we could not read the ALI file that was just generated
3720 -- then there could be a problem reading either the ALI or the
3721 -- corresponding object file (if Check_Object_Consistency is
3722 -- set Read_Library_Info checks that the time stamp of the
3723 -- object file is more recent than that of the ALI). However,
3724 -- we record a failure only if not already done.
3726 else
3727 if Compilation_OK and not Syntax_Only then
3728 Inform
3729 (Data.Lib_File,
3730 "WARNING: ALI or object file not found after compile");
3731 Record_Failure (Data.Full_Source_File, Data.Source_Unit);
3732 end if;
3733 end if;
3734 end if;
3735 end if;
3736 end Wait_For_Available_Slot;
3738 -- Start of processing for Compile_Sources
3740 begin
3741 pragma Assert (Args'First = 1);
3743 Outstanding_Compiles := 0;
3744 Running_Compile := new Comp_Data_Arr (1 .. Max_Process);
3746 -- Package and Queue initializations
3748 Good_ALI.Init;
3750 if Initialize_ALI_Data then
3751 Initialize_ALI;
3752 Initialize_ALI_Source;
3753 end if;
3755 -- The following two flags affect the behavior of ALI.Set_Source_Table.
3756 -- We set Check_Source_Files to True to ensure that source file time
3757 -- stamps are checked, and we set All_Sources to False to avoid checking
3758 -- the presence of the source files listed in the source dependency
3759 -- section of an ali file (which would be a mistake since the ali file
3760 -- may be obsolete).
3762 Check_Source_Files := True;
3763 All_Sources := False;
3765 Queue.Insert
3766 ((Format => Format_Gnatmake,
3767 File => Main_Source,
3768 Project => Main_Project,
3769 Unit => No_Unit_Name,
3770 Index => Main_Index,
3771 Sid => No_Source));
3773 First_Compiled_File := No_File;
3774 Most_Recent_Obj_File := No_File;
3775 Most_Recent_Obj_Stamp := Empty_Time_Stamp;
3776 Main_Unit := False;
3778 -- Keep looping until there is no more work to do (the Q is empty)
3779 -- and all the outstanding compilations have terminated.
3781 Make_Loop :
3782 while not Queue.Is_Empty or else Outstanding_Compiles > 0 loop
3783 exit Make_Loop when Must_Exit_Because_Of_Error;
3784 exit Make_Loop when Start_Compile_If_Possible (Args);
3786 Wait_For_Available_Slot;
3788 -- ??? Should be done as soon as we add a Good_ALI, wouldn't it avoid
3789 -- the need for a list of good ALI?
3791 Fill_Queue_From_ALI_Files;
3793 if Display_Compilation_Progress then
3794 Write_Str ("completed ");
3795 Write_Int (Int (Queue.Processed));
3796 Write_Str (" out of ");
3797 Write_Int (Int (Queue.Size));
3798 Write_Str (" (");
3799 Write_Int (Int ((Queue.Processed * 100) / Queue.Size));
3800 Write_Str ("%)...");
3801 Write_Eol;
3802 end if;
3803 end loop Make_Loop;
3805 Compilation_Failures := Bad_Compilation_Count;
3807 -- Compilation is finished
3809 -- Delete any temporary configuration pragma file
3811 if not Debug.Debug_Flag_N then
3812 Delete_Temp_Config_Files (Project_Tree);
3813 end if;
3814 end Compile_Sources;
3816 ----------------------------------
3817 -- Configuration_Pragmas_Switch --
3818 ----------------------------------
3820 function Configuration_Pragmas_Switch
3821 (For_Project : Project_Id) return Argument_List
3823 The_Packages : Package_Id;
3824 Gnatmake : Package_Id;
3825 Compiler : Package_Id;
3827 Global_Attribute : Variable_Value := Nil_Variable_Value;
3828 Local_Attribute : Variable_Value := Nil_Variable_Value;
3830 Global_Attribute_Present : Boolean := False;
3831 Local_Attribute_Present : Boolean := False;
3833 Result : Argument_List (1 .. 3);
3834 Last : Natural := 0;
3836 begin
3837 Prj.Env.Create_Config_Pragmas_File
3838 (For_Project, Project_Tree);
3840 if For_Project.Config_File_Name /= No_Path then
3841 Temporary_Config_File := For_Project.Config_File_Temp;
3842 Last := 1;
3843 Result (1) :=
3844 new String'
3845 ("-gnatec=" & Get_Name_String (For_Project.Config_File_Name));
3847 else
3848 Temporary_Config_File := False;
3849 end if;
3851 -- Check for attribute Builder'Global_Configuration_Pragmas
3853 The_Packages := Main_Project.Decl.Packages;
3854 Gnatmake :=
3855 Prj.Util.Value_Of
3856 (Name => Name_Builder,
3857 In_Packages => The_Packages,
3858 Shared => Project_Tree.Shared);
3860 if Gnatmake /= No_Package then
3861 Global_Attribute := Prj.Util.Value_Of
3862 (Variable_Name => Name_Global_Configuration_Pragmas,
3863 In_Variables => Project_Tree.Shared.Packages.Table
3864 (Gnatmake).Decl.Attributes,
3865 Shared => Project_Tree.Shared);
3866 Global_Attribute_Present :=
3867 Global_Attribute /= Nil_Variable_Value
3868 and then Get_Name_String (Global_Attribute.Value) /= "";
3870 if Global_Attribute_Present then
3871 declare
3872 Path : constant String :=
3873 Absolute_Path
3874 (Path_Name_Type (Global_Attribute.Value),
3875 Global_Attribute.Project);
3876 begin
3877 if not Is_Regular_File (Path) then
3878 if Debug.Debug_Flag_F then
3879 Make_Failed
3880 ("cannot find configuration pragmas file "
3881 & File_Name (Path));
3882 else
3883 Make_Failed
3884 ("cannot find configuration pragmas file " & Path);
3885 end if;
3886 end if;
3888 Last := Last + 1;
3889 Result (Last) := new String'("-gnatec=" & Path);
3890 end;
3891 end if;
3892 end if;
3894 -- Check for attribute Compiler'Local_Configuration_Pragmas
3896 The_Packages := For_Project.Decl.Packages;
3897 Compiler :=
3898 Prj.Util.Value_Of
3899 (Name => Name_Compiler,
3900 In_Packages => The_Packages,
3901 Shared => Project_Tree.Shared);
3903 if Compiler /= No_Package then
3904 Local_Attribute := Prj.Util.Value_Of
3905 (Variable_Name => Name_Local_Configuration_Pragmas,
3906 In_Variables => Project_Tree.Shared.Packages.Table
3907 (Compiler).Decl.Attributes,
3908 Shared => Project_Tree.Shared);
3909 Local_Attribute_Present :=
3910 Local_Attribute /= Nil_Variable_Value
3911 and then Get_Name_String (Local_Attribute.Value) /= "";
3913 if Local_Attribute_Present then
3914 declare
3915 Path : constant String :=
3916 Absolute_Path
3917 (Path_Name_Type (Local_Attribute.Value),
3918 Local_Attribute.Project);
3919 begin
3920 if not Is_Regular_File (Path) then
3921 if Debug.Debug_Flag_F then
3922 Make_Failed
3923 ("cannot find configuration pragmas file "
3924 & File_Name (Path));
3926 else
3927 Make_Failed
3928 ("cannot find configuration pragmas file " & Path);
3929 end if;
3930 end if;
3932 Last := Last + 1;
3933 Result (Last) := new String'("-gnatec=" & Path);
3934 end;
3935 end if;
3936 end if;
3938 return Result (1 .. Last);
3939 end Configuration_Pragmas_Switch;
3941 ---------------
3942 -- Debug_Msg --
3943 ---------------
3945 procedure Debug_Msg (S : String; N : Name_Id) is
3946 begin
3947 if Debug.Debug_Flag_W then
3948 Write_Str (" ... ");
3949 Write_Str (S);
3950 Write_Str (" ");
3951 Write_Name (N);
3952 Write_Eol;
3953 end if;
3954 end Debug_Msg;
3956 procedure Debug_Msg (S : String; N : File_Name_Type) is
3957 begin
3958 Debug_Msg (S, Name_Id (N));
3959 end Debug_Msg;
3961 procedure Debug_Msg (S : String; N : Unit_Name_Type) is
3962 begin
3963 Debug_Msg (S, Name_Id (N));
3964 end Debug_Msg;
3966 -------------
3967 -- Display --
3968 -------------
3970 procedure Display (Program : String; Args : Argument_List) is
3971 begin
3972 pragma Assert (Args'First = 1);
3974 if Display_Executed_Programs then
3975 Write_Str (Program);
3977 for J in Args'Range loop
3979 -- Never display -gnatea nor -gnatez
3981 if Args (J).all /= "-gnatea"
3982 and then
3983 Args (J).all /= "-gnatez"
3984 then
3985 -- Do not display the mapping file argument automatically
3986 -- created when using a project file.
3988 if Main_Project = No_Project
3989 or else Debug.Debug_Flag_N
3990 or else Args (J)'Length < 8
3991 or else
3992 Args (J) (Args (J)'First .. Args (J)'First + 6) /= "-gnatem"
3993 then
3994 -- When -dn is not specified, do not display the config
3995 -- pragmas switch (-gnatec) for the temporary file created
3996 -- by the project manager (always the first -gnatec switch).
3997 -- Reset Temporary_Config_File to False so that the eventual
3998 -- other -gnatec switches will be displayed.
4000 if (not Debug.Debug_Flag_N)
4001 and then Temporary_Config_File
4002 and then Args (J)'Length > 7
4003 and then Args (J) (Args (J)'First .. Args (J)'First + 6)
4004 = "-gnatec"
4005 then
4006 Temporary_Config_File := False;
4008 -- Do not display the -F=mapping_file switch for gnatbind
4009 -- if -dn is not specified.
4011 elsif Debug.Debug_Flag_N
4012 or else Args (J)'Length < 4
4013 or else
4014 Args (J) (Args (J)'First .. Args (J)'First + 2) /= "-F="
4015 then
4016 Write_Str (" ");
4018 -- If -df is used, only display file names, not path
4019 -- names.
4021 if Debug.Debug_Flag_F then
4022 declare
4023 Equal_Pos : Natural;
4024 begin
4025 Equal_Pos := Args (J)'First - 1;
4026 for K in Args (J)'Range loop
4027 if Args (J) (K) = '=' then
4028 Equal_Pos := K;
4029 exit;
4030 end if;
4031 end loop;
4033 if Is_Absolute_Path
4034 (Args (J) (Equal_Pos + 1 .. Args (J)'Last))
4035 then
4036 Write_Str
4037 (Args (J) (Args (J)'First .. Equal_Pos));
4038 Write_Str
4039 (File_Name
4040 (Args (J)
4041 (Equal_Pos + 1 .. Args (J)'Last)));
4043 else
4044 Write_Str (Args (J).all);
4045 end if;
4046 end;
4048 else
4049 Write_Str (Args (J).all);
4050 end if;
4051 end if;
4052 end if;
4053 end if;
4054 end loop;
4056 Write_Eol;
4057 end if;
4058 end Display;
4060 ----------------------
4061 -- Display_Commands --
4062 ----------------------
4064 procedure Display_Commands (Display : Boolean := True) is
4065 begin
4066 Display_Executed_Programs := Display;
4067 end Display_Commands;
4069 --------------------------
4070 -- Enter_Into_Obsoleted --
4071 --------------------------
4073 procedure Enter_Into_Obsoleted (F : File_Name_Type) is
4074 Name : constant String := Get_Name_String (F);
4075 First : Natural;
4076 F2 : File_Name_Type;
4078 begin
4079 First := Name'Last;
4080 while First > Name'First
4081 and then Name (First - 1) /= Directory_Separator
4082 and then Name (First - 1) /= '/'
4083 loop
4084 First := First - 1;
4085 end loop;
4087 if First /= Name'First then
4088 Name_Len := 0;
4089 Add_Str_To_Name_Buffer (Name (First .. Name'Last));
4090 F2 := Name_Find;
4092 else
4093 F2 := F;
4094 end if;
4096 Debug_Msg ("New entry in Obsoleted table:", F2);
4097 Obsoleted.Set (F2, True);
4098 end Enter_Into_Obsoleted;
4100 ---------------
4101 -- Globalize --
4102 ---------------
4104 procedure Globalize (Success : out Boolean) is
4105 Quiet_Str : aliased String := "-quiet";
4106 Globalizer_Args : constant Argument_List :=
4107 (1 => Quiet_Str'Unchecked_Access);
4108 Previous_Dir : String_Access;
4110 procedure Globalize_Dir (Dir : String);
4111 -- Call CodePeer globalizer on Dir
4113 -------------------
4114 -- Globalize_Dir --
4115 -------------------
4117 procedure Globalize_Dir (Dir : String) is
4118 Result : Boolean;
4119 begin
4120 if Previous_Dir = null or else Dir /= Previous_Dir.all then
4121 Free (Previous_Dir);
4122 Previous_Dir := new String'(Dir);
4123 Change_Dir (Dir);
4124 GNAT.OS_Lib.Spawn (Globalizer_Path.all, Globalizer_Args, Result);
4125 Success := Success and Result;
4126 end if;
4127 end Globalize_Dir;
4129 procedure Globalize_Dirs is new
4130 Prj.Env.For_All_Object_Dirs (Globalize_Dir);
4132 begin
4133 Success := True;
4134 Display (Globalizer, Globalizer_Args);
4136 if Globalizer_Path = null then
4137 Make_Failed ("error, unable to locate " & Globalizer);
4138 end if;
4140 if Main_Project = No_Project then
4141 GNAT.OS_Lib.Spawn (Globalizer_Path.all, Globalizer_Args, Success);
4142 else
4143 Globalize_Dirs (Main_Project, Project_Tree);
4144 end if;
4145 end Globalize;
4147 -------------------
4148 -- Linking_Phase --
4149 -------------------
4151 procedure Linking_Phase
4152 (Non_Std_Executable : Boolean := False;
4153 Executable : File_Name_Type := No_File;
4154 Main_ALI_File : File_Name_Type)
4156 Linker_Switches_Last : constant Integer := Linker_Switches.Last;
4157 Path_Option : constant String_Access :=
4158 MLib.Linker_Library_Path_Option;
4159 Libraries_Present : Boolean := False;
4160 Current : Natural;
4161 Proj2 : Project_Id;
4162 Depth : Natural;
4163 Proj1 : Project_List;
4165 begin
4166 if not Run_Path_Option then
4167 Linker_Switches.Increment_Last;
4168 Linker_Switches.Table (Linker_Switches.Last) :=
4169 new String'("-R");
4170 end if;
4172 if Main_Project /= No_Project then
4173 Library_Paths.Set_Last (0);
4174 Library_Projs.Init;
4176 if MLib.Tgt.Support_For_Libraries /= Prj.None then
4178 -- Check for library projects
4180 Proj1 := Project_Tree.Projects;
4181 while Proj1 /= null loop
4182 if Proj1.Project /= Main_Project
4183 and then Proj1.Project.Library
4184 then
4185 -- Add this project to table Library_Projs
4187 Libraries_Present := True;
4188 Depth := Proj1.Project.Depth;
4189 Library_Projs.Increment_Last;
4190 Current := Library_Projs.Last;
4192 -- Any project with a greater depth should be after this
4193 -- project in the list.
4195 while Current > 1 loop
4196 Proj2 := Library_Projs.Table (Current - 1);
4197 exit when Proj2.Depth <= Depth;
4198 Library_Projs.Table (Current) := Proj2;
4199 Current := Current - 1;
4200 end loop;
4202 Library_Projs.Table (Current) := Proj1.Project;
4204 -- If it is not a static library and path option is set, add
4205 -- it to the Library_Paths table.
4207 if Proj1.Project.Library_Kind /= Static
4208 and then Proj1.Project.Extended_By = No_Project
4209 and then Path_Option /= null
4210 then
4211 Library_Paths.Increment_Last;
4212 Library_Paths.Table (Library_Paths.Last) :=
4213 new String'
4214 (Get_Name_String
4215 (Proj1.Project.Library_Dir.Display_Name));
4216 end if;
4217 end if;
4219 Proj1 := Proj1.Next;
4220 end loop;
4222 for Index in 1 .. Library_Projs.Last loop
4224 Library_Projs.Table (Index).Extended_By = No_Project
4225 then
4226 if Library_Projs.Table (Index).Library_Kind = Static
4227 and then not Targparm.OpenVMS_On_Target
4228 then
4229 Linker_Switches.Increment_Last;
4230 Linker_Switches.Table (Linker_Switches.Last) :=
4231 new String'
4232 (Get_Name_String
4233 (Library_Projs.Table
4234 (Index).Library_Dir.Display_Name) &
4235 "lib" &
4236 Get_Name_String
4237 (Library_Projs.Table (Index).Library_Name) &
4238 "." &
4239 MLib.Tgt.Archive_Ext);
4241 else
4242 -- Add the -L switch
4244 Linker_Switches.Increment_Last;
4245 Linker_Switches.Table (Linker_Switches.Last) :=
4246 new String'("-L" &
4247 Get_Name_String (Library_Projs.Table (Index).
4248 Library_Dir.Display_Name));
4250 -- Add the -l switch
4252 Linker_Switches.Increment_Last;
4253 Linker_Switches.Table (Linker_Switches.Last) :=
4254 new String'("-l" &
4255 Get_Name_String
4256 (Library_Projs.Table (Index).Library_Name));
4257 end if;
4258 end if;
4259 end loop;
4260 end if;
4262 if Libraries_Present then
4264 -- If Path_Option is not null, create the switch ("-Wl,-rpath,"
4265 -- or equivalent) with all the non-static library dirs plus the
4266 -- standard GNAT library dir. We do that only if Run_Path_Option
4267 -- is True (not disabled by -R switch).
4269 if Run_Path_Option and then Path_Option /= null then
4270 declare
4271 Option : String_Access;
4272 Length : Natural := Path_Option'Length;
4273 Current : Natural;
4275 begin
4276 if MLib.Separate_Run_Path_Options then
4278 -- We are going to create one switch of the form
4279 -- "-Wl,-rpath,dir_N" for each directory to
4280 -- consider.
4282 -- One switch for each library directory
4284 for Index in
4285 Library_Paths.First .. Library_Paths.Last
4286 loop
4287 Linker_Switches.Increment_Last;
4288 Linker_Switches.Table (Linker_Switches.Last) :=
4289 new String'
4290 (Path_Option.all &
4291 Library_Paths.Table (Index).all);
4292 end loop;
4294 -- One switch for the standard GNAT library dir
4296 Linker_Switches.Increment_Last;
4297 Linker_Switches.Table (Linker_Switches.Last) :=
4298 new String'(Path_Option.all & MLib.Utl.Lib_Directory);
4300 else
4301 -- We are going to create one switch of the form
4302 -- "-Wl,-rpath,dir_1:dir_2:dir_3"
4304 for Index in
4305 Library_Paths.First .. Library_Paths.Last
4306 loop
4307 -- Add the length of the library dir plus one for the
4308 -- directory separator.
4310 Length :=
4311 Length + Library_Paths.Table (Index)'Length + 1;
4312 end loop;
4314 -- Finally, add the length of the standard GNAT
4315 -- library dir.
4317 Length := Length + MLib.Utl.Lib_Directory'Length;
4318 Option := new String (1 .. Length);
4319 Option (1 .. Path_Option'Length) := Path_Option.all;
4320 Current := Path_Option'Length;
4322 -- Put each library dir followed by a dir
4323 -- separator.
4325 for Index in
4326 Library_Paths.First .. Library_Paths.Last
4327 loop
4328 Option
4329 (Current + 1 ..
4330 Current + Library_Paths.Table (Index)'Length) :=
4331 Library_Paths.Table (Index).all;
4332 Current :=
4333 Current + Library_Paths.Table (Index)'Length + 1;
4334 Option (Current) := Path_Separator;
4335 end loop;
4337 -- Finally put the standard GNAT library dir
4339 Option
4340 (Current + 1 ..
4341 Current + MLib.Utl.Lib_Directory'Length) :=
4342 MLib.Utl.Lib_Directory;
4344 -- And add the switch to the linker switches
4346 Linker_Switches.Increment_Last;
4347 Linker_Switches.Table (Linker_Switches.Last) := Option;
4348 end if;
4349 end;
4350 end if;
4351 end if;
4353 -- Put the object directories in ADA_OBJECTS_PATH
4355 Prj.Env.Set_Ada_Paths
4356 (Main_Project,
4357 Project_Tree,
4358 Including_Libraries => False,
4359 Include_Path => False);
4361 -- Check for attributes Linker'Linker_Options in projects other than
4362 -- the main project
4364 declare
4365 Linker_Options : constant String_List :=
4366 Linker_Options_Switches
4367 (Main_Project,
4368 Do_Fail => Make_Failed'Access,
4369 In_Tree => Project_Tree);
4370 begin
4371 for Option in Linker_Options'Range loop
4372 Linker_Switches.Increment_Last;
4373 Linker_Switches.Table (Linker_Switches.Last) :=
4374 Linker_Options (Option);
4375 end loop;
4376 end;
4377 end if;
4379 if CodePeer_Mode then
4380 Linker_Switches.Increment_Last;
4381 Linker_Switches.Table (Linker_Switches.Last) :=
4382 new String'(CodePeer_Mode_String);
4383 end if;
4385 -- Add switch -M to gnatlink if builder switch --create-map-file
4386 -- has been specified.
4388 if Map_File /= null then
4389 Linker_Switches.Increment_Last;
4390 Linker_Switches.Table (Linker_Switches.Last) :=
4391 new String'("-M" & Map_File.all);
4392 end if;
4394 declare
4395 Args : Argument_List
4396 (Linker_Switches.First .. Linker_Switches.Last + 2);
4398 Last_Arg : Integer := Linker_Switches.First - 1;
4399 Skip : Boolean := False;
4401 begin
4402 -- Get all the linker switches
4404 for J in Linker_Switches.First .. Linker_Switches.Last loop
4405 if Skip then
4406 Skip := False;
4408 elsif Non_Std_Executable
4409 and then Linker_Switches.Table (J).all = "-o"
4410 then
4411 Skip := True;
4413 -- Here we capture and duplicate the linker argument. We
4414 -- need to do the duplication since the arguments will get
4415 -- normalized. Not doing so will result in calling normalized
4416 -- two times for the same set of arguments if gnatmake is
4417 -- passed multiple mains. This can result in the wrong argument
4418 -- being passed to the linker.
4420 else
4421 Last_Arg := Last_Arg + 1;
4422 Args (Last_Arg) := new String'(Linker_Switches.Table (J).all);
4423 end if;
4424 end loop;
4426 -- If need be, add the -o switch
4428 if Non_Std_Executable then
4429 Last_Arg := Last_Arg + 1;
4430 Args (Last_Arg) := new String'("-o");
4431 Last_Arg := Last_Arg + 1;
4432 Args (Last_Arg) := new String'(Get_Name_String (Executable));
4433 end if;
4435 -- And invoke the linker
4437 declare
4438 Success : Boolean := False;
4439 begin
4440 -- If gnatmake was invoked with --subdirs and no project file,
4441 -- put the executable in the subdirectory specified.
4443 if Prj.Subdirs /= null and then Main_Project = No_Project then
4444 Change_Dir (Object_Directory_Path.all);
4445 end if;
4447 Link (Main_ALI_File,
4448 Link_With_Shared_Libgcc.all &
4449 Args (Args'First .. Last_Arg),
4450 Success);
4452 if Success then
4453 Successful_Links.Increment_Last;
4454 Successful_Links.Table (Successful_Links.Last) := Main_ALI_File;
4456 elsif Osint.Number_Of_Files = 1
4457 or else not Keep_Going
4458 then
4459 Make_Failed ("*** link failed.");
4461 else
4462 Set_Standard_Error;
4463 Write_Line ("*** link failed");
4465 if Commands_To_Stdout then
4466 Set_Standard_Output;
4467 end if;
4469 Failed_Links.Increment_Last;
4470 Failed_Links.Table (Failed_Links.Last) := Main_ALI_File;
4471 end if;
4472 end;
4473 end;
4475 Linker_Switches.Set_Last (Linker_Switches_Last);
4476 end Linking_Phase;
4478 -------------------
4479 -- Binding_Phase --
4480 -------------------
4482 procedure Binding_Phase
4483 (Stand_Alone_Libraries : Boolean := False;
4484 Main_ALI_File : File_Name_Type)
4486 Args : Argument_List (Binder_Switches.First .. Binder_Switches.Last + 2);
4487 -- The arguments for the invocation of gnatbind
4489 Last_Arg : Natural := Binder_Switches.Last;
4490 -- Index of the last argument in Args
4492 Shared_Libs : Boolean := False;
4493 -- Set to True when there are shared library project files or
4494 -- when gnatbind is invoked with -shared.
4496 Proj : Project_List;
4498 Mapping_Path : Path_Name_Type := No_Path;
4499 -- The path name of the mapping file
4501 begin
4502 -- Check if there are shared libraries, so that gnatbind is called with
4503 -- -shared. Check also if gnatbind is called with -shared, so that
4504 -- gnatlink is called with -shared-libgcc ensuring that the shared
4505 -- version of libgcc will be used.
4507 if Main_Project /= No_Project
4508 and then MLib.Tgt.Support_For_Libraries /= Prj.None
4509 then
4510 Proj := Project_Tree.Projects;
4511 while Proj /= null loop
4512 if Proj.Project.Library
4513 and then Proj.Project.Library_Kind /= Static
4514 then
4515 Shared_Libs := True;
4516 Bind_Shared := Shared_Switch'Access;
4517 exit;
4518 end if;
4520 Proj := Proj.Next;
4521 end loop;
4522 end if;
4524 -- Check now for switch -shared
4526 if not Shared_Libs then
4527 for J in Binder_Switches.First .. Last_Arg loop
4528 if Binder_Switches.Table (J).all = "-shared" then
4529 Shared_Libs := True;
4530 exit;
4531 end if;
4532 end loop;
4533 end if;
4535 -- If shared libraries present, invoke gnatlink with
4536 -- -shared-libgcc.
4538 if Shared_Libs then
4539 Link_With_Shared_Libgcc := Shared_Libgcc_Switch'Access;
4540 end if;
4542 -- Get all the binder switches
4544 for J in Binder_Switches.First .. Last_Arg loop
4545 Args (J) := Binder_Switches.Table (J);
4546 end loop;
4548 if Stand_Alone_Libraries then
4549 Last_Arg := Last_Arg + 1;
4550 Args (Last_Arg) := Force_Elab_Flags_String'Access;
4551 end if;
4553 if CodePeer_Mode then
4554 Last_Arg := Last_Arg + 1;
4555 Args (Last_Arg) := CodePeer_Mode_String'Access;
4556 end if;
4558 if Main_Project /= No_Project then
4560 -- Put all the source directories in ADA_INCLUDE_PATH, and all the
4561 -- object directories in ADA_OBJECTS_PATH.
4563 Prj.Env.Set_Ada_Paths
4564 (Project => Main_Project,
4565 In_Tree => Project_Tree,
4566 Including_Libraries => True,
4567 Include_Path => Use_Include_Path_File);
4569 -- If switch -C was specified, create a binder mapping file
4571 if Create_Mapping_File then
4572 Mapping_Path := Create_Binder_Mapping_File (Project_Tree);
4574 if Mapping_Path /= No_Path then
4575 Last_Arg := Last_Arg + 1;
4576 Args (Last_Arg) :=
4577 new String'("-F=" & Get_Name_String (Mapping_Path));
4578 end if;
4579 end if;
4580 end if;
4582 -- If gnatmake was invoked with --subdirs and no project file, put the
4583 -- binder generated files in the subdirectory specified.
4585 if Main_Project = No_Project and then Prj.Subdirs /= null then
4586 Change_Dir (Object_Directory_Path.all);
4587 end if;
4589 begin
4590 Bind (Main_ALI_File,
4591 Bind_Shared.all & Args (Args'First .. Last_Arg));
4593 exception
4594 when others =>
4596 -- Delete the temporary mapping file if one was created
4598 if Mapping_Path /= No_Path then
4599 Delete_Temporary_File (Project_Tree.Shared, Mapping_Path);
4600 end if;
4602 -- And reraise the exception
4604 raise;
4605 end;
4607 -- If -dn was not specified, delete the temporary mapping file
4608 -- if one was created.
4610 if Mapping_Path /= No_Path then
4611 Delete_Temporary_File (Project_Tree.Shared, Mapping_Path);
4612 end if;
4613 end Binding_Phase;
4615 -------------------
4616 -- Library_Phase --
4617 -------------------
4619 procedure Library_Phase
4620 (Stand_Alone_Libraries : in out Boolean;
4621 Library_Rebuilt : in out Boolean)
4623 Depth : Natural;
4624 Current : Natural;
4625 Proj1 : Project_List;
4627 procedure Add_To_Library_Projs (Proj : Project_Id);
4628 -- Add project Project to table Library_Projs in
4629 -- decreasing depth order.
4631 --------------------------
4632 -- Add_To_Library_Projs --
4633 --------------------------
4635 procedure Add_To_Library_Projs (Proj : Project_Id) is
4636 Prj : Project_Id;
4638 begin
4639 Library_Projs.Increment_Last;
4640 Depth := Proj.Depth;
4642 -- Put the projects in decreasing depth order, so that
4643 -- if libA depends on libB, libB is first in order.
4645 Current := Library_Projs.Last;
4646 while Current > 1 loop
4647 Prj := Library_Projs.Table (Current - 1);
4648 exit when Prj.Depth >= Depth;
4649 Library_Projs.Table (Current) := Prj;
4650 Current := Current - 1;
4651 end loop;
4653 Library_Projs.Table (Current) := Proj;
4654 end Add_To_Library_Projs;
4656 begin
4657 Library_Projs.Init;
4659 -- Put in Library_Projs table all library project file
4660 -- ids when the library need to be rebuilt.
4662 Proj1 := Project_Tree.Projects;
4663 while Proj1 /= null loop
4664 if Proj1.Project.Extended_By = No_Project then
4665 if Proj1.Project.Standalone_Library /= No then
4666 Stand_Alone_Libraries := True;
4667 end if;
4669 if Proj1.Project.Library then
4670 MLib.Prj.Check_Library
4671 (Proj1.Project, Project_Tree);
4672 end if;
4674 if Proj1.Project.Need_To_Build_Lib then
4675 Add_To_Library_Projs (Proj1.Project);
4676 end if;
4677 end if;
4679 Proj1 := Proj1.Next;
4680 end loop;
4682 -- Check if importing libraries should be regenerated
4683 -- because at least an imported library will be
4684 -- regenerated or is more recent.
4686 Proj1 := Project_Tree.Projects;
4687 while Proj1 /= null loop
4688 if Proj1.Project.Library
4689 and then Proj1.Project.Extended_By = No_Project
4690 and then Proj1.Project.Library_Kind /= Static
4691 and then not Proj1.Project.Need_To_Build_Lib
4692 and then not Proj1.Project.Externally_Built
4693 then
4694 declare
4695 List : Project_List;
4696 Proj2 : Project_Id;
4697 Rebuild : Boolean := False;
4699 Lib_Timestamp1 : constant Time_Stamp_Type :=
4700 Proj1.Project.Library_TS;
4702 begin
4703 List := Proj1.Project.All_Imported_Projects;
4704 while List /= null loop
4705 Proj2 := List.Project;
4707 if Proj2.Library then
4708 if Proj2.Need_To_Build_Lib
4709 or else
4710 (Lib_Timestamp1 < Proj2.Library_TS)
4711 then
4712 Rebuild := True;
4713 exit;
4714 end if;
4715 end if;
4717 List := List.Next;
4718 end loop;
4720 if Rebuild then
4721 Proj1.Project.Need_To_Build_Lib := True;
4722 Add_To_Library_Projs (Proj1.Project);
4723 end if;
4724 end;
4725 end if;
4727 Proj1 := Proj1.Next;
4728 end loop;
4730 -- Reset the flags Need_To_Build_Lib for the next main, to avoid
4731 -- rebuilding libraries uselessly.
4733 Proj1 := Project_Tree.Projects;
4734 while Proj1 /= null loop
4735 Proj1.Project.Need_To_Build_Lib := False;
4736 Proj1 := Proj1.Next;
4737 end loop;
4739 -- Build the libraries, if any need to be built
4741 for J in 1 .. Library_Projs.Last loop
4742 Library_Rebuilt := True;
4744 -- If a library is rebuilt, then executables are obsolete
4746 Executable_Obsolete := True;
4748 MLib.Prj.Build_Library
4749 (For_Project => Library_Projs.Table (J),
4750 In_Tree => Project_Tree,
4751 Gnatbind => Gnatbind.all,
4752 Gnatbind_Path => Gnatbind_Path,
4753 Gcc => Gcc.all,
4754 Gcc_Path => Gcc_Path);
4755 end loop;
4756 end Library_Phase;
4758 -----------------------
4759 -- Compilation_Phase --
4760 -----------------------
4762 procedure Compilation_Phase
4763 (Main_Source_File : File_Name_Type;
4764 Current_Main_Index : Int := 0;
4765 Total_Compilation_Failures : in out Natural;
4766 Stand_Alone_Libraries : in out Boolean;
4767 Executable : File_Name_Type := No_File;
4768 Is_Last_Main : Boolean;
4769 Stop_Compile : out Boolean)
4771 Args : Argument_List (1 .. Gcc_Switches.Last);
4773 First_Compiled_File : File_Name_Type;
4774 Youngest_Obj_File : File_Name_Type;
4775 Youngest_Obj_Stamp : Time_Stamp_Type;
4777 Is_Main_Unit : Boolean;
4778 -- Set True by Compile_Sources if Main_Source_File can be a main unit
4780 Compilation_Failures : Natural;
4782 Executable_Stamp : Time_Stamp_Type;
4784 Library_Rebuilt : Boolean := False;
4786 begin
4787 Stop_Compile := False;
4789 for J in 1 .. Gcc_Switches.Last loop
4790 Args (J) := Gcc_Switches.Table (J);
4791 end loop;
4793 -- Now we invoke Compile_Sources for the current main
4795 Compile_Sources
4796 (Main_Source => Main_Source_File,
4797 Args => Args,
4798 First_Compiled_File => First_Compiled_File,
4799 Most_Recent_Obj_File => Youngest_Obj_File,
4800 Most_Recent_Obj_Stamp => Youngest_Obj_Stamp,
4801 Main_Unit => Is_Main_Unit,
4802 Main_Index => Current_Main_Index,
4803 Compilation_Failures => Compilation_Failures,
4804 Check_Readonly_Files => Check_Readonly_Files,
4805 Do_Not_Execute => Do_Not_Execute,
4806 Force_Compilations => Force_Compilations,
4807 In_Place_Mode => In_Place_Mode,
4808 Keep_Going => Keep_Going,
4809 Initialize_ALI_Data => True,
4810 Max_Process => Saved_Maximum_Processes);
4812 if Verbose_Mode then
4813 Write_Str ("End of compilation");
4814 Write_Eol;
4815 end if;
4817 Total_Compilation_Failures :=
4818 Total_Compilation_Failures + Compilation_Failures;
4820 if Total_Compilation_Failures /= 0 then
4821 Stop_Compile := True;
4822 return;
4823 end if;
4825 -- Regenerate libraries, if there are any and if object files have been
4826 -- regenerated. Note that we skip this in CodePeer mode because we don't
4827 -- need libraries in this case, and more importantly, the object files
4828 -- may not be present.
4830 if Main_Project /= No_Project
4831 and then not CodePeer_Mode
4832 and then MLib.Tgt.Support_For_Libraries /= Prj.None
4833 and then (Do_Bind_Step
4834 or Unique_Compile_All_Projects
4835 or not Compile_Only)
4836 and then (Do_Link_Step or Is_Last_Main)
4837 then
4838 Library_Phase
4839 (Stand_Alone_Libraries => Stand_Alone_Libraries,
4840 Library_Rebuilt => Library_Rebuilt);
4841 end if;
4843 if List_Dependencies then
4844 if First_Compiled_File /= No_File then
4845 Inform
4846 (First_Compiled_File,
4847 "must be recompiled. Can't generate dependence list.");
4848 else
4849 List_Depend;
4850 end if;
4852 elsif First_Compiled_File = No_File
4853 and then not Do_Bind_Step
4854 and then not Quiet_Output
4855 and then not Library_Rebuilt
4856 and then Osint.Number_Of_Files = 1
4857 then
4858 Inform (Msg => "objects up to date.");
4859 Stop_Compile := True;
4860 return;
4862 elsif Do_Not_Execute and then First_Compiled_File /= No_File then
4863 Write_Name (First_Compiled_File);
4864 Write_Eol;
4865 end if;
4867 -- Stop after compile step if any of:
4869 -- 1) -n (Do_Not_Execute) specified
4871 -- 2) -M (List_Dependencies) specified (also sets
4872 -- Do_Not_Execute above, so this is probably superfluous).
4874 -- 3) -c (Compile_Only) specified, but not -b (Bind_Only)
4876 -- 4) Made unit cannot be a main unit
4878 if ((Do_Not_Execute
4879 or List_Dependencies
4880 or not Do_Bind_Step
4881 or not Is_Main_Unit)
4882 and not No_Main_Subprogram
4883 and not Build_Bind_And_Link_Full_Project)
4884 or Unique_Compile
4885 then
4886 Stop_Compile := True;
4887 return;
4888 end if;
4890 -- If the objects were up-to-date check if the executable file is also
4891 -- up-to-date. For now always bind and link on the JVM since there is
4892 -- currently no simple way to check whether objects are up to date wrt
4893 -- the executable. Same in CodePeer mode where there is no executable.
4895 if Targparm.VM_Target /= JVM_Target
4896 and then not CodePeer_Mode
4897 and then First_Compiled_File = No_File
4898 then
4899 Executable_Stamp := File_Stamp (Executable);
4901 if not Executable_Obsolete then
4902 Executable_Obsolete := Youngest_Obj_Stamp > Executable_Stamp;
4903 end if;
4905 if not Executable_Obsolete then
4906 for Index in reverse 1 .. Dependencies.Last loop
4907 if Is_In_Obsoleted (Dependencies.Table (Index).Depends_On) then
4908 Enter_Into_Obsoleted (Dependencies.Table (Index).This);
4909 end if;
4910 end loop;
4912 Executable_Obsolete := Is_In_Obsoleted (Main_Source_File);
4913 Dependencies.Init;
4914 end if;
4916 if not Executable_Obsolete then
4918 -- If no Ada object files obsolete the executable, check
4919 -- for younger or missing linker files.
4921 Check_Linker_Options
4922 (Executable_Stamp,
4923 Youngest_Obj_File,
4924 Youngest_Obj_Stamp);
4926 Executable_Obsolete := Youngest_Obj_File /= No_File;
4927 end if;
4929 -- Check if any library file is more recent than the
4930 -- executable: there may be an externally built library
4931 -- file that has been modified.
4933 if not Executable_Obsolete and then Main_Project /= No_Project then
4934 declare
4935 Proj1 : Project_List;
4937 begin
4938 Proj1 := Project_Tree.Projects;
4939 while Proj1 /= null loop
4940 if Proj1.Project.Library
4941 and then Proj1.Project.Library_TS > Executable_Stamp
4942 then
4943 Executable_Obsolete := True;
4944 Youngest_Obj_Stamp := Proj1.Project.Library_TS;
4945 Name_Len := 0;
4946 Add_Str_To_Name_Buffer ("library ");
4947 Add_Str_To_Name_Buffer
4948 (Get_Name_String (Proj1.Project.Library_Name));
4949 Youngest_Obj_File := Name_Find;
4950 exit;
4951 end if;
4953 Proj1 := Proj1.Next;
4954 end loop;
4955 end;
4956 end if;
4958 -- Return if the executable is up to date and otherwise
4959 -- motivate the relink/rebind.
4961 if not Executable_Obsolete then
4962 if not Quiet_Output then
4963 Inform (Executable, "up to date.");
4964 end if;
4966 Stop_Compile := True;
4967 return;
4968 end if;
4970 if Executable_Stamp (1) = ' ' then
4971 if not No_Main_Subprogram then
4972 Verbose_Msg (Executable, "missing.", Prefix => " ");
4973 end if;
4975 elsif Youngest_Obj_Stamp (1) = ' ' then
4976 Verbose_Msg
4977 (Youngest_Obj_File, "missing.", Prefix => " ");
4979 elsif Youngest_Obj_Stamp > Executable_Stamp then
4980 Verbose_Msg
4981 (Youngest_Obj_File,
4982 "(" & String (Youngest_Obj_Stamp) & ") newer than",
4983 Executable,
4984 "(" & String (Executable_Stamp) & ")");
4986 else
4987 Verbose_Msg
4988 (Executable, "needs to be rebuilt", Prefix => " ");
4990 end if;
4991 end if;
4992 end Compilation_Phase;
4994 ----------------------------------------
4995 -- Resolve_Relative_Names_In_Switches --
4996 ----------------------------------------
4998 procedure Resolve_Relative_Names_In_Switches (Current_Work_Dir : String) is
4999 begin
5000 -- If a relative path output file has been specified, we add the
5001 -- exec directory.
5003 for J in reverse 1 .. Saved_Linker_Switches.Last - 1 loop
5004 if Saved_Linker_Switches.Table (J).all = Output_Flag.all then
5005 declare
5006 Exec_File_Name : constant String :=
5007 Saved_Linker_Switches.Table (J + 1).all;
5009 begin
5010 if not Is_Absolute_Path (Exec_File_Name) then
5011 Get_Name_String (Main_Project.Exec_Directory.Display_Name);
5012 Add_Str_To_Name_Buffer (Exec_File_Name);
5013 Saved_Linker_Switches.Table (J + 1) :=
5014 new String'(Name_Buffer (1 .. Name_Len));
5015 end if;
5016 end;
5018 exit;
5019 end if;
5020 end loop;
5022 -- If we are using a project file, for relative paths we add the
5023 -- current working directory for any relative path on the command
5024 -- line and the project directory, for any relative path in the
5025 -- project file.
5027 declare
5028 Dir_Path : constant String :=
5029 Get_Name_String (Main_Project.Directory.Display_Name);
5030 begin
5031 for J in 1 .. Binder_Switches.Last loop
5032 Ensure_Absolute_Path
5033 (Binder_Switches.Table (J),
5034 Do_Fail => Make_Failed'Access,
5035 Parent => Dir_Path, For_Gnatbind => True);
5036 end loop;
5038 for J in 1 .. Saved_Binder_Switches.Last loop
5039 Ensure_Absolute_Path
5040 (Saved_Binder_Switches.Table (J),
5041 Do_Fail => Make_Failed'Access,
5042 Parent => Current_Work_Dir,
5043 For_Gnatbind => True);
5044 end loop;
5046 for J in 1 .. Linker_Switches.Last loop
5047 Ensure_Absolute_Path
5048 (Linker_Switches.Table (J),
5049 Parent => Dir_Path,
5050 Do_Fail => Make_Failed'Access);
5051 end loop;
5053 for J in 1 .. Saved_Linker_Switches.Last loop
5054 Ensure_Absolute_Path
5055 (Saved_Linker_Switches.Table (J),
5056 Do_Fail => Make_Failed'Access,
5057 Parent => Current_Work_Dir);
5058 end loop;
5060 for J in 1 .. Gcc_Switches.Last loop
5061 Ensure_Absolute_Path
5062 (Gcc_Switches.Table (J),
5063 Do_Fail => Make_Failed'Access,
5064 Parent => Dir_Path,
5065 Including_Non_Switch => False);
5066 end loop;
5068 for J in 1 .. Saved_Gcc_Switches.Last loop
5069 Ensure_Absolute_Path
5070 (Saved_Gcc_Switches.Table (J),
5071 Parent => Current_Work_Dir,
5072 Do_Fail => Make_Failed'Access,
5073 Including_Non_Switch => False);
5074 end loop;
5075 end;
5076 end Resolve_Relative_Names_In_Switches;
5078 -----------------------------------
5079 -- Queue_Library_Project_Sources --
5080 -----------------------------------
5082 procedure Queue_Library_Project_Sources is
5083 begin
5084 if not Unique_Compile
5085 and then MLib.Tgt.Support_For_Libraries /= Prj.None
5086 then
5087 declare
5088 Proj : Project_List;
5090 begin
5091 Proj := Project_Tree.Projects;
5092 while Proj /= null loop
5093 if Proj.Project.Library then
5094 Proj.Project.Need_To_Build_Lib :=
5095 not MLib.Tgt.Library_Exists_For
5096 (Proj.Project, Project_Tree)
5097 and then not Proj.Project.Externally_Built;
5099 if Proj.Project.Need_To_Build_Lib then
5101 -- If there is no object directory, then it will be
5102 -- impossible to build the library, so fail immediately.
5104 if Proj.Project.Object_Directory =
5105 No_Path_Information
5106 then
5107 Make_Failed
5108 ("no object files to build library for"
5109 & " project """
5110 & Get_Name_String (Proj.Project.Name)
5111 & """");
5112 Proj.Project.Need_To_Build_Lib := False;
5114 else
5115 if Verbose_Mode then
5116 Write_Str
5117 ("Library file does not exist for "
5118 & "project """);
5119 Write_Str
5120 (Get_Name_String (Proj.Project.Name));
5121 Write_Line ("""");
5122 end if;
5124 Insert_Project_Sources
5125 (The_Project => Proj.Project,
5126 All_Projects => False,
5127 Into_Q => True);
5128 end if;
5129 end if;
5130 end if;
5132 Proj := Proj.Next;
5133 end loop;
5134 end;
5135 end if;
5136 end Queue_Library_Project_Sources;
5138 ------------------------
5139 -- Compute_Executable --
5140 ------------------------
5142 procedure Compute_Executable
5143 (Main_Source_File : File_Name_Type;
5144 Executable : out File_Name_Type;
5145 Non_Std_Executable : out Boolean)
5147 begin
5148 Executable := No_File;
5149 Non_Std_Executable :=
5150 Targparm.Executable_Extension_On_Target /= No_Name;
5152 -- Look inside the linker switches to see if the name of the final
5153 -- executable program was specified.
5155 for J in reverse Linker_Switches.First .. Linker_Switches.Last loop
5156 if Linker_Switches.Table (J).all = Output_Flag.all then
5157 pragma Assert (J < Linker_Switches.Last);
5159 -- We cannot specify a single executable for several main
5160 -- subprograms
5162 if Osint.Number_Of_Files > 1 then
5163 Fail ("cannot specify a single executable for several mains");
5164 end if;
5166 Name_Len := 0;
5167 Add_Str_To_Name_Buffer (Linker_Switches.Table (J + 1).all);
5168 Executable := Name_Enter;
5170 Verbose_Msg (Executable, "final executable");
5171 end if;
5172 end loop;
5174 -- If the name of the final executable program was not specified then
5175 -- construct it from the main input file.
5177 if Executable = No_File then
5178 if Main_Project = No_Project then
5179 Executable := Executable_Name (Strip_Suffix (Main_Source_File));
5181 else
5182 -- If we are using a project file, we attempt to remove the body
5183 -- (or spec) termination of the main subprogram. We find it the
5184 -- naming scheme of the project file. This avoids generating an
5185 -- executable "main.2" for a main subprogram "main.2.ada", when
5186 -- the body termination is ".2.ada".
5188 Executable :=
5189 Prj.Util.Executable_Of
5190 (Main_Project, Project_Tree.Shared,
5191 Main_Source_File, Main_Index);
5192 end if;
5193 end if;
5195 if Main_Project /= No_Project
5196 and then Main_Project.Exec_Directory /= No_Path_Information
5197 then
5198 declare
5199 Exec_File_Name : constant String := Get_Name_String (Executable);
5200 begin
5201 if not Is_Absolute_Path (Exec_File_Name) then
5202 Get_Name_String (Main_Project.Exec_Directory.Display_Name);
5203 Add_Str_To_Name_Buffer (Exec_File_Name);
5204 Executable := Name_Find;
5205 end if;
5207 Non_Std_Executable := True;
5208 end;
5209 end if;
5210 end Compute_Executable;
5212 -------------------------------
5213 -- Compute_Switches_For_Main --
5214 -------------------------------
5216 procedure Compute_Switches_For_Main
5217 (Main_Source_File : in out File_Name_Type;
5218 Root_Environment : in out Prj.Tree.Environment;
5219 Compute_Builder : Boolean;
5220 Current_Work_Dir : String)
5222 function Add_Global_Switches
5223 (Switch : String;
5224 For_Lang : Name_Id;
5225 For_Builder : Boolean;
5226 Has_Global_Compilation_Switches : Boolean) return Boolean;
5227 -- Handles builder and global compilation switches, as read from the
5228 -- project file.
5230 function Add_Global_Switches
5231 (Switch : String;
5232 For_Lang : Name_Id;
5233 For_Builder : Boolean;
5234 Has_Global_Compilation_Switches : Boolean) return Boolean
5236 pragma Unreferenced (For_Lang);
5237 begin
5238 if For_Builder then
5239 Program_Args := None;
5240 Switch_May_Be_Passed_To_The_Compiler :=
5241 not Has_Global_Compilation_Switches;
5242 Scan_Make_Arg (Root_Environment, Switch, And_Save => False);
5244 return Gnatmake_Switch_Found
5245 or else Switch_May_Be_Passed_To_The_Compiler;
5246 else
5247 Add_Switch (Switch, Compiler, And_Save => False);
5248 return True;
5249 end if;
5250 end Add_Global_Switches;
5252 procedure Do_Compute_Builder_Switches
5253 is new Makeutl.Compute_Builder_Switches (Add_Global_Switches);
5254 begin
5255 if Main_Project /= No_Project then
5256 declare
5257 Main_Source_File_Name : constant String :=
5258 Get_Name_String (Main_Source_File);
5260 Main_Unit_File_Name : constant String :=
5261 Prj.Env.File_Name_Of_Library_Unit_Body
5262 (Name => Main_Source_File_Name,
5263 Project => Main_Project,
5264 In_Tree => Project_Tree,
5265 Main_Project_Only => not Unique_Compile);
5267 The_Packages : constant Package_Id := Main_Project.Decl.Packages;
5269 Binder_Package : constant Prj.Package_Id :=
5270 Prj.Util.Value_Of
5271 (Name => Name_Binder,
5272 In_Packages => The_Packages,
5273 Shared => Project_Tree.Shared);
5275 Linker_Package : constant Prj.Package_Id :=
5276 Prj.Util.Value_Of
5277 (Name => Name_Linker,
5278 In_Packages => The_Packages,
5279 Shared => Project_Tree.Shared);
5281 begin
5282 -- We fail if we cannot find the main source file
5284 if Main_Unit_File_Name = "" then
5285 Make_Failed ('"' & Main_Source_File_Name
5286 & """ is not a unit of project "
5287 & Project_File_Name.all & ".");
5288 end if;
5290 -- Remove any directory information from the main source file
5291 -- file name.
5293 declare
5294 Pos : Natural := Main_Unit_File_Name'Last;
5296 begin
5297 loop
5298 exit when Pos < Main_Unit_File_Name'First
5299 or else Main_Unit_File_Name (Pos) = Directory_Separator;
5300 Pos := Pos - 1;
5301 end loop;
5303 Name_Len := Main_Unit_File_Name'Last - Pos;
5305 Name_Buffer (1 .. Name_Len) :=
5306 Main_Unit_File_Name (Pos + 1 .. Main_Unit_File_Name'Last);
5308 Main_Source_File := Name_Find;
5310 -- We only output the main source file if there is only one
5312 if Verbose_Mode and then Osint.Number_Of_Files = 1 then
5313 Write_Str ("Main source file: """);
5314 Write_Str (Main_Unit_File_Name
5315 (Pos + 1 .. Main_Unit_File_Name'Last));
5316 Write_Line (""".");
5317 end if;
5318 end;
5320 if Compute_Builder then
5321 Do_Compute_Builder_Switches
5322 (Project_Tree => Project_Tree,
5323 Root_Environment => Root_Environment,
5324 Main_Project => Main_Project,
5325 Only_For_Lang => Name_Ada);
5327 Resolve_Relative_Names_In_Switches
5328 (Current_Work_Dir => Current_Work_Dir);
5330 -- Record current last switch index for tables Binder_Switches
5331 -- and Linker_Switches, so that these tables may be reset
5332 -- before each main, before adding switches from the project
5333 -- file and from the command line.
5335 Last_Binder_Switch := Binder_Switches.Last;
5336 Last_Linker_Switch := Linker_Switches.Last;
5338 else
5339 -- Reset the tables Binder_Switches and Linker_Switches
5341 Binder_Switches.Set_Last (Last_Binder_Switch);
5342 Linker_Switches.Set_Last (Last_Linker_Switch);
5343 end if;
5345 -- We now deal with the binder and linker switches. If no project
5346 -- file is used, there is nothing to do because the binder and
5347 -- linker switches are the same for all mains.
5349 -- Add binder switches from the project file for the first main
5351 if Do_Bind_Step and then Binder_Package /= No_Package then
5352 if Verbose_Mode then
5353 Write_Str ("Adding binder switches for """);
5354 Write_Str (Main_Unit_File_Name);
5355 Write_Line (""".");
5356 end if;
5358 Add_Switches
5359 (Env => Root_Environment,
5360 File_Name => Main_Unit_File_Name,
5361 The_Package => Binder_Package,
5362 Program => Binder);
5363 end if;
5365 -- Add linker switches from the project file for the first main
5367 if Do_Link_Step and then Linker_Package /= No_Package then
5368 if Verbose_Mode then
5369 Write_Str ("Adding linker switches for""");
5370 Write_Str (Main_Unit_File_Name);
5371 Write_Line (""".");
5372 end if;
5374 Add_Switches
5375 (Env => Root_Environment,
5376 File_Name => Main_Unit_File_Name,
5377 The_Package => Linker_Package,
5378 Program => Linker);
5379 end if;
5381 -- As we are using a project file, for relative paths we add the
5382 -- current working directory for any relative path on the command
5383 -- line and the project directory, for any relative path in the
5384 -- project file.
5386 declare
5387 Dir_Path : constant String :=
5388 Get_Name_String (Main_Project.Directory.Display_Name);
5389 begin
5390 for J in Last_Binder_Switch + 1 .. Binder_Switches.Last loop
5391 Ensure_Absolute_Path
5392 (Binder_Switches.Table (J),
5393 Do_Fail => Make_Failed'Access,
5394 Parent => Dir_Path, For_Gnatbind => True);
5395 end loop;
5397 for J in Last_Linker_Switch + 1 .. Linker_Switches.Last loop
5398 Ensure_Absolute_Path
5399 (Linker_Switches.Table (J),
5400 Parent => Dir_Path,
5401 Do_Fail => Make_Failed'Access);
5402 end loop;
5403 end;
5404 end;
5406 else
5407 if not Compute_Builder then
5409 -- Reset the tables Binder_Switches and Linker_Switches
5411 Binder_Switches.Set_Last (Last_Binder_Switch);
5412 Linker_Switches.Set_Last (Last_Linker_Switch);
5413 end if;
5414 end if;
5416 Check_Steps;
5418 if Compute_Builder then
5419 Display_Commands (not Quiet_Output);
5420 end if;
5422 -- We now put in the Binder_Switches and Linker_Switches tables, the
5423 -- binder and linker switches of the command line that have been put in
5424 -- the Saved_ tables. If a project file was used, then the command line
5425 -- switches will follow the project file switches.
5427 for J in 1 .. Saved_Binder_Switches.Last loop
5428 Add_Switch
5429 (Saved_Binder_Switches.Table (J),
5430 Binder,
5431 And_Save => False);
5432 end loop;
5434 for J in 1 .. Saved_Linker_Switches.Last loop
5435 Add_Switch
5436 (Saved_Linker_Switches.Table (J),
5437 Linker,
5438 And_Save => False);
5439 end loop;
5440 end Compute_Switches_For_Main;
5442 --------------
5443 -- Gnatmake --
5444 --------------
5446 procedure Gnatmake is
5447 Main_Source_File : File_Name_Type;
5448 -- The source file containing the main compilation unit
5450 Total_Compilation_Failures : Natural := 0;
5452 Main_ALI_File : File_Name_Type;
5453 -- The ali file corresponding to Main_Source_File
5455 Executable : File_Name_Type := No_File;
5456 -- The file name of an executable
5458 Non_Std_Executable : Boolean := False;
5459 -- Non_Std_Executable is set to True when there is a possibility that
5460 -- the linker will not choose the correct executable file name.
5462 Current_Work_Dir : constant String_Access :=
5463 new String'(Get_Current_Dir);
5464 -- The current working directory, used to modify some relative path
5465 -- switches on the command line when a project file is used.
5467 Current_Main_Index : Int := 0;
5468 -- If not zero, the index of the current main unit in its source file
5470 Is_First_Main : Boolean;
5471 -- Whether we are processing the first main
5473 Stand_Alone_Libraries : Boolean := False;
5474 -- Set to True when there are Stand-Alone Libraries, so that gnatbind
5475 -- is invoked with the -F switch to force checking of elaboration flags.
5477 Project_Node_Tree : Project_Node_Tree_Ref;
5479 Stop_Compile : Boolean;
5481 Discard : Boolean;
5482 pragma Warnings (Off, Discard);
5484 procedure Check_Mains;
5485 -- Check that the main subprograms do exist and that they all
5486 -- belong to the same project file.
5488 -----------------
5489 -- Check_Mains --
5490 -----------------
5492 procedure Check_Mains is
5493 Real_Main_Project : Project_Id := No_Project;
5494 Info : Main_Info;
5495 Proj : Project_Id;
5496 begin
5497 if Mains.Number_Of_Mains (Project_Tree) = 0
5498 and then not Unique_Compile
5499 then
5500 Mains.Fill_From_Project (Main_Project, Project_Tree);
5501 end if;
5503 Mains.Complete_Mains
5504 (Root_Environment.Flags, Main_Project, Project_Tree);
5506 -- If we have multiple mains on the command line, they need not
5507 -- belong to the root project, but they must all belong to the same
5508 -- project.
5510 if not Unique_Compile then
5511 Mains.Reset;
5512 loop
5513 Info := Mains.Next_Main;
5514 exit when Info = No_Main_Info;
5516 Proj := Ultimate_Extending_Project_Of (Info.Project);
5518 if Real_Main_Project = No_Project then
5519 Real_Main_Project := Proj;
5520 elsif Real_Main_Project /= Proj then
5521 Make_Failed
5522 ("""" & Get_Name_String (Info.File) &
5523 """ is not a source of project " &
5524 Get_Name_String (Real_Main_Project.Name));
5525 end if;
5526 end loop;
5528 if Real_Main_Project /= No_Project then
5529 Main_Project := Real_Main_Project;
5530 end if;
5532 Debug_Output ("After checking mains, main project is",
5533 Main_Project.Name);
5535 else
5536 -- For all mains on the command line, make sure they were in
5537 -- osint. In particular, if the user has specified a multi-unit
5538 -- source file, the call to Complete_Mains will have expanded
5539 -- the list of mains to all its units, and we must now put them
5540 -- back on the command line.
5541 -- ??? This will not be necessary when gnatmake shares the same
5542 -- queue as gprbuild and processes the file directly on the queue.
5544 Mains.Reset;
5545 loop
5546 Info := Mains.Next_Main;
5547 exit when Info = No_Main_Info;
5549 if Info.Index /= 0 then
5550 Debug_Output ("Add to command line index="
5551 & Info.Index'Img, Name_Id (Info.File));
5552 Osint.Add_File (Get_Name_String (Info.File), Info.Index);
5553 end if;
5554 end loop;
5555 end if;
5556 end Check_Mains;
5558 -- Start of processing for Gnatmake
5560 -- This body is very long, should be broken down???
5562 begin
5563 Install_Int_Handler (Sigint_Intercepted'Access);
5565 Do_Compile_Step := True;
5566 Do_Bind_Step := True;
5567 Do_Link_Step := True;
5569 Obsoleted.Reset;
5571 Make.Initialize (Project_Node_Tree, Root_Environment);
5573 Bind_Shared := No_Shared_Switch'Access;
5574 Link_With_Shared_Libgcc := No_Shared_Libgcc_Switch'Access;
5576 Failed_Links.Set_Last (0);
5577 Successful_Links.Set_Last (0);
5579 -- Special case when switch -B was specified
5581 if Build_Bind_And_Link_Full_Project then
5583 -- When switch -B is specified, there must be a project file
5585 if Main_Project = No_Project then
5586 Make_Failed ("-B cannot be used without a project file");
5588 -- No main program may be specified on the command line
5590 elsif Osint.Number_Of_Files /= 0 then
5591 Make_Failed
5592 ("-B cannot be used with a main specified on the command line");
5594 -- And the project file cannot be a library project file
5596 elsif Main_Project.Library then
5597 Make_Failed ("-B cannot be used for a library project file");
5599 else
5600 No_Main_Subprogram := True;
5601 Insert_Project_Sources
5602 (The_Project => Main_Project,
5603 All_Projects => Unique_Compile_All_Projects,
5604 Into_Q => False);
5606 -- If there are no sources to compile, we fail
5608 if Osint.Number_Of_Files = 0 then
5609 Make_Failed ("no sources to compile");
5610 end if;
5612 -- Specify -n for gnatbind and add the ALI files of all the
5613 -- sources, except the one which is a fake main subprogram: this
5614 -- is the one for the binder generated file and it will be
5615 -- transmitted to gnatlink. These sources are those that are in
5616 -- the queue.
5618 Add_Switch ("-n", Binder, And_Save => True);
5620 for J in 1 .. Queue.Size loop
5621 Add_Switch
5622 (Get_Name_String (Lib_File_Name (Queue.Element (J))),
5623 Binder, And_Save => True);
5624 end loop;
5625 end if;
5627 elsif Main_Index /= 0 and then Osint.Number_Of_Files > 1 then
5628 Make_Failed ("cannot specify several mains with a multi-unit index");
5630 elsif Main_Project /= No_Project then
5632 -- If the main project file is a library project file, main(s) cannot
5633 -- be specified on the command line.
5635 if Osint.Number_Of_Files /= 0 then
5636 if Main_Project.Library
5637 and then not Unique_Compile
5638 and then ((not Make_Steps) or else Bind_Only or else Link_Only)
5639 then
5640 Make_Failed
5641 ("cannot specify a main program "
5642 & "on the command line for a library project file");
5643 end if;
5645 -- If no mains have been specified on the command line, and we are
5646 -- using a project file, we either find the main(s) in attribute Main
5647 -- of the main project, or we put all the sources of the project file
5648 -- as mains.
5650 else
5651 if Main_Index /= 0 then
5652 Make_Failed ("cannot specify a multi-unit index but no main "
5653 & "on the command line");
5654 end if;
5656 declare
5657 Value : String_List_Id := Main_Project.Mains;
5659 begin
5660 -- The attribute Main is an empty list or not specified, or
5661 -- else gnatmake was invoked with the switch "-u".
5663 if Value = Prj.Nil_String or else Unique_Compile then
5665 if not Make_Steps
5666 or Compile_Only
5667 or not Main_Project.Library
5668 then
5669 -- First make sure that the binder and the linker will
5670 -- not be invoked.
5672 Do_Bind_Step := False;
5673 Do_Link_Step := False;
5675 -- Put all the sources in the queue
5677 No_Main_Subprogram := True;
5678 Insert_Project_Sources
5679 (The_Project => Main_Project,
5680 All_Projects => Unique_Compile_All_Projects,
5681 Into_Q => False);
5683 -- If no sources to compile, then there is nothing to do
5685 if Osint.Number_Of_Files = 0 then
5686 if not Quiet_Output then
5687 Osint.Write_Program_Name;
5688 Write_Line (": no sources to compile");
5689 end if;
5691 Finish_Program (Project_Tree, E_Success);
5692 end if;
5693 end if;
5695 else
5696 -- The attribute Main is not an empty list. Put all the main
5697 -- subprograms in the list as if they were specified on the
5698 -- command line. However, if attribute Languages includes a
5699 -- language other than Ada, only include the Ada mains; if
5700 -- there is no Ada main, compile all sources of the project.
5702 declare
5703 Languages : constant Variable_Value :=
5704 Prj.Util.Value_Of
5705 (Name_Languages,
5706 Main_Project.Decl.Attributes,
5707 Project_Tree.Shared);
5709 Current : String_List_Id;
5710 Element : String_Element;
5712 Foreign_Language : Boolean := False;
5713 At_Least_One_Main : Boolean := False;
5715 begin
5716 -- First, determine if there is a foreign language in
5717 -- attribute Languages.
5719 if not Languages.Default then
5720 Current := Languages.Values;
5721 Look_For_Foreign :
5722 while Current /= Nil_String loop
5723 Element := Project_Tree.Shared.String_Elements.
5724 Table (Current);
5725 Get_Name_String (Element.Value);
5726 To_Lower (Name_Buffer (1 .. Name_Len));
5728 if Name_Buffer (1 .. Name_Len) /= "ada" then
5729 Foreign_Language := True;
5730 exit Look_For_Foreign;
5731 end if;
5733 Current := Element.Next;
5734 end loop Look_For_Foreign;
5735 end if;
5737 -- Then, find all mains, or if there is a foreign
5738 -- language, all the Ada mains.
5740 while Value /= Prj.Nil_String loop
5741 -- To know if a main is an Ada main, get its project.
5742 -- It should be the project specified on the command
5743 -- line.
5745 Get_Name_String
5746 (Project_Tree.Shared.String_Elements.Table
5747 (Value).Value);
5749 declare
5750 Main_Name : constant String :=
5751 Get_Name_String
5752 (Project_Tree.Shared.
5753 String_Elements.
5754 Table (Value).Value);
5756 Proj : constant Project_Id :=
5757 Prj.Env.Project_Of
5758 (Main_Name, Main_Project, Project_Tree);
5760 begin
5761 if Proj = Main_Project then
5762 At_Least_One_Main := True;
5763 Osint.Add_File
5764 (Get_Name_String
5765 (Project_Tree.Shared.String_Elements.Table
5766 (Value).Value),
5767 Index =>
5768 Project_Tree.Shared.String_Elements.Table
5769 (Value).Index);
5771 elsif not Foreign_Language then
5772 Make_Failed
5773 ("""" & Main_Name &
5774 """ is not a source of project " &
5775 Get_Name_String (Main_Project.Display_Name));
5776 end if;
5777 end;
5779 Value := Project_Tree.Shared.String_Elements.Table
5780 (Value).Next;
5781 end loop;
5783 -- If we did not get any main, it means that all mains
5784 -- in attribute Mains are in a foreign language and -B
5785 -- was not specified to gnatmake; so, we fail.
5787 if not At_Least_One_Main then
5788 Make_Failed
5789 ("no Ada mains, use -B to build foreign main");
5790 end if;
5791 end;
5793 end if;
5794 end;
5795 end if;
5797 -- Check that each main on the command line is a source of a
5798 -- project file and, if there are several mains, each of them
5799 -- is a source of the same project file.
5801 Check_Mains;
5802 end if;
5804 if Verbose_Mode then
5805 Write_Eol;
5806 Display_Version ("GNATMAKE", "1992");
5807 end if;
5809 if Osint.Number_Of_Files = 0 then
5810 if Main_Project /= No_Project and then Main_Project.Library then
5811 if Do_Bind_Step
5812 and then Main_Project.Standalone_Library = No
5813 then
5814 Make_Failed ("only stand-alone libraries may be bound");
5815 end if;
5817 -- Add the default search directories to be able to find libgnat
5819 Osint.Add_Default_Search_Dirs;
5821 -- Get the target parameters, so that the correct binder generated
5822 -- files are generated if OpenVMS is the target.
5824 begin
5825 Targparm.Get_Target_Parameters;
5827 exception
5828 when Unrecoverable_Error =>
5829 Make_Failed ("*** make failed.");
5830 end;
5832 -- And bind and or link the library
5834 MLib.Prj.Build_Library
5835 (For_Project => Main_Project,
5836 In_Tree => Project_Tree,
5837 Gnatbind => Gnatbind.all,
5838 Gnatbind_Path => Gnatbind_Path,
5839 Gcc => Gcc.all,
5840 Gcc_Path => Gcc_Path,
5841 Bind => Bind_Only,
5842 Link => Link_Only);
5844 Finish_Program (Project_Tree, E_Success);
5846 else
5847 -- Call Get_Target_Parameters to ensure that VM_Target and
5848 -- AAMP_On_Target get set before calling Usage.
5850 Targparm.Get_Target_Parameters;
5852 -- Output usage information if no files to compile
5854 Usage;
5855 Finish_Program (Project_Tree, E_Success);
5856 end if;
5857 end if;
5859 -- Get the first executable.
5860 -- ??? This needs to be done early, because Osint.Next_Main_File also
5861 -- initializes the primary search directory, used below to initialize
5862 -- the "-I" parameter
5864 Main_Source_File := Next_Main_Source; -- No directory information
5866 -- If -M was specified, behave as if -n was specified
5868 if List_Dependencies then
5869 Do_Not_Execute := True;
5870 end if;
5872 Add_Switch ("-I-", Compiler, And_Save => True);
5874 if Main_Project = No_Project then
5875 if Look_In_Primary_Dir then
5876 Add_Switch
5877 ("-I" &
5878 Normalize_Directory_Name
5879 (Get_Primary_Src_Search_Directory.all).all,
5880 Compiler,
5881 Append_Switch => False,
5882 And_Save => False);
5884 end if;
5886 else
5887 -- If we use a project file, we have already checked that a main
5888 -- specified on the command line with directory information has the
5889 -- path name corresponding to a correct source in the project tree.
5890 -- So, we don't need the directory information to be taken into
5891 -- account by Find_File, and in fact it may lead to take the wrong
5892 -- sources for other compilation units, when there are extending
5893 -- projects.
5895 Look_In_Primary_Dir := False;
5896 end if;
5898 -- If the user wants a program without a main subprogram, add the
5899 -- appropriate switch to the binder.
5901 if No_Main_Subprogram then
5902 Add_Switch ("-z", Binder, And_Save => True);
5903 end if;
5905 if Main_Project /= No_Project then
5907 if Main_Project.Object_Directory /= No_Path_Information then
5909 -- Change current directory to object directory of main project
5911 Project_Of_Current_Object_Directory := No_Project;
5912 Change_To_Object_Directory (Main_Project);
5913 end if;
5915 -- Source file lookups should be cached for efficiency. Source files
5916 -- are not supposed to change.
5918 Osint.Source_File_Data (Cache => True);
5920 Queue_Library_Project_Sources;
5921 end if;
5923 -- The combination of -f -u and one or several mains on the command line
5924 -- implies -a.
5926 if Force_Compilations
5927 and then Unique_Compile
5928 and then not Unique_Compile_All_Projects
5929 and then Main_On_Command_Line
5930 then
5931 Must_Compile := True;
5932 end if;
5934 if Main_Project /= No_Project
5935 and then not Must_Compile
5936 and then Main_Project.Externally_Built
5937 then
5938 Make_Failed
5939 ("nothing to do for a main project that is externally built");
5940 end if;
5942 -- If no project file is used, we just put the gcc switches
5943 -- from the command line in the Gcc_Switches table.
5945 if Main_Project = No_Project then
5946 for J in 1 .. Saved_Gcc_Switches.Last loop
5947 Add_Switch
5948 (Saved_Gcc_Switches.Table (J), Compiler, And_Save => False);
5949 end loop;
5951 else
5952 -- If there is a project, put the command line gcc switches in the
5953 -- variable The_Saved_Gcc_Switches. They are going to be used later
5954 -- in procedure Compile_Sources.
5956 The_Saved_Gcc_Switches :=
5957 new Argument_List (1 .. Saved_Gcc_Switches.Last + 1);
5959 for J in 1 .. Saved_Gcc_Switches.Last loop
5960 The_Saved_Gcc_Switches (J) := Saved_Gcc_Switches.Table (J);
5961 end loop;
5963 -- We never use gnat.adc when a project file is used
5965 The_Saved_Gcc_Switches (The_Saved_Gcc_Switches'Last) := No_gnat_adc;
5966 end if;
5968 -- If there was a --GCC, --GNATBIND or --GNATLINK switch on the command
5969 -- line, then we have to use it, even if there was another switch in
5970 -- the project file.
5972 if Saved_Gcc /= null then
5973 Gcc := Saved_Gcc;
5974 end if;
5976 if Saved_Gnatbind /= null then
5977 Gnatbind := Saved_Gnatbind;
5978 end if;
5980 if Saved_Gnatlink /= null then
5981 Gnatlink := Saved_Gnatlink;
5982 end if;
5984 Bad_Compilation.Init;
5986 -- If project files are used, create the mapping of all the sources, so
5987 -- that the correct paths will be found. Otherwise, if there is a file
5988 -- which is not a source with the same name in a source directory this
5989 -- file may be incorrectly found.
5991 if Main_Project /= No_Project then
5992 Prj.Env.Create_Mapping (Project_Tree);
5993 end if;
5995 -- Here is where the make process is started
5997 Queue.Initialize
5998 (Main_Project /= No_Project and then One_Compilation_Per_Obj_Dir);
6000 Is_First_Main := True;
6002 Multiple_Main_Loop : for N_File in 1 .. Osint.Number_Of_Files loop
6003 if Current_File_Index /= No_Index then
6004 Main_Index := Current_File_Index;
6005 end if;
6007 Current_Main_Index := Main_Index;
6009 if Current_Main_Index = 0
6010 and then Unique_Compile
6011 and then Main_Project /= No_Project
6012 then
6013 -- If this is a multi-unit source, do not compile it as is (ie
6014 -- without specifying which unit to compile)
6015 -- Insert_Project_Sources has added each of the unit separately.
6017 declare
6018 Source : constant Prj.Source_Id := Find_Source
6019 (In_Tree => Project_Tree,
6020 Project => Main_Project,
6021 Base_Name => Main_Source_File,
6022 Index => Current_Main_Index,
6023 In_Imported_Only => True);
6024 begin
6025 if Source /= No_Source
6026 and then Source.Index /= 0
6027 then
6028 goto Next_Main;
6029 end if;
6030 end;
6031 end if;
6033 Compute_Switches_For_Main
6034 (Main_Source_File,
6035 Root_Environment,
6036 Compute_Builder => Is_First_Main,
6037 Current_Work_Dir => Current_Work_Dir.all);
6039 if Is_First_Main then
6041 -- Put the default source dirs in the source path only now, so
6042 -- that we take the correct ones in the case where --RTS= is
6043 -- specified in the Builder switches.
6045 Osint.Add_Default_Search_Dirs;
6047 -- Get the target parameters, which are only needed for a couple
6048 -- of cases in gnatmake. Protect against an exception, such as the
6049 -- case of system.ads missing from the library, and fail
6050 -- gracefully.
6052 begin
6053 Targparm.Get_Target_Parameters;
6054 exception
6055 when Unrecoverable_Error =>
6056 Make_Failed ("*** make failed.");
6057 end;
6059 -- Special processing for VM targets
6061 if Targparm.VM_Target /= No_VM then
6063 -- Set proper processing commands
6065 case Targparm.VM_Target is
6066 when Targparm.JVM_Target =>
6068 -- Do not check for an object file (".o") when compiling
6069 -- to JVM machine since ".class" files are generated
6070 -- instead.
6072 Check_Object_Consistency := False;
6074 -- Do not modify Gcc is --GCC= was specified
6076 if Gcc = Original_Gcc then
6077 Gcc := new String'("jvm-gnatcompile");
6078 end if;
6080 when Targparm.CLI_Target =>
6081 -- Do not modify Gcc is --GCC= was specified
6083 if Gcc = Original_Gcc then
6084 Gcc := new String'("dotnet-gnatcompile");
6085 end if;
6087 when Targparm.No_VM =>
6088 raise Program_Error;
6089 end case;
6090 end if;
6092 Gcc_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
6093 Gnatbind_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gnatbind.all);
6094 Gnatlink_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gnatlink.all);
6096 -- If we have specified -j switch both from the project file
6097 -- and on the command line, the one from the command line takes
6098 -- precedence.
6100 if Saved_Maximum_Processes = 0 then
6101 Saved_Maximum_Processes := Maximum_Processes;
6102 end if;
6104 if Debug.Debug_Flag_M then
6105 Write_Line ("Maximum number of simultaneous compilations =" &
6106 Saved_Maximum_Processes'Img);
6107 end if;
6109 -- Allocate as many temporary mapping file names as the maximum
6110 -- number of compilations processed, for each possible project.
6112 declare
6113 Data : Project_Compilation_Access;
6114 Proj : Project_List;
6116 begin
6117 Proj := Project_Tree.Projects;
6118 while Proj /= null loop
6119 Data := new Project_Compilation_Data'
6120 (Mapping_File_Names => new Temp_Path_Names
6121 (1 .. Saved_Maximum_Processes),
6122 Last_Mapping_File_Names => 0,
6123 Free_Mapping_File_Indexes => new Free_File_Indexes
6124 (1 .. Saved_Maximum_Processes),
6125 Last_Free_Indexes => 0);
6127 Project_Compilation_Htable.Set
6128 (Project_Compilation, Proj.Project, Data);
6129 Proj := Proj.Next;
6130 end loop;
6132 Data := new Project_Compilation_Data'
6133 (Mapping_File_Names => new Temp_Path_Names
6134 (1 .. Saved_Maximum_Processes),
6135 Last_Mapping_File_Names => 0,
6136 Free_Mapping_File_Indexes => new Free_File_Indexes
6137 (1 .. Saved_Maximum_Processes),
6138 Last_Free_Indexes => 0);
6140 Project_Compilation_Htable.Set
6141 (Project_Compilation, No_Project, Data);
6142 end;
6144 Is_First_Main := False;
6145 end if;
6147 Executable_Obsolete := False;
6149 Compute_Executable
6150 (Main_Source_File => Main_Source_File,
6151 Executable => Executable,
6152 Non_Std_Executable => Non_Std_Executable);
6154 if Do_Compile_Step then
6155 Compilation_Phase
6156 (Main_Source_File => Main_Source_File,
6157 Current_Main_Index => Current_Main_Index,
6158 Total_Compilation_Failures => Total_Compilation_Failures,
6159 Stand_Alone_Libraries => Stand_Alone_Libraries,
6160 Executable => Executable,
6161 Is_Last_Main => N_File = Osint.Number_Of_Files,
6162 Stop_Compile => Stop_Compile);
6164 if Stop_Compile then
6165 if Total_Compilation_Failures /= 0 then
6166 if Keep_Going then
6167 goto Next_Main;
6169 else
6170 List_Bad_Compilations;
6171 Report_Compilation_Failed;
6172 end if;
6174 elsif Osint.Number_Of_Files = 1 then
6175 exit Multiple_Main_Loop;
6176 else
6177 goto Next_Main;
6178 end if;
6179 end if;
6180 end if;
6182 -- For binding and linking, we need to be in the object directory of
6183 -- the main project.
6185 if Main_Project /= No_Project then
6186 Change_To_Object_Directory (Main_Project);
6187 end if;
6189 -- If we are here, it means that we need to rebuilt the current main,
6190 -- so we set Executable_Obsolete to True to make sure that subsequent
6191 -- mains will be rebuilt.
6193 Main_ALI_In_Place_Mode_Step : declare
6194 ALI_File : File_Name_Type;
6195 Src_File : File_Name_Type;
6197 begin
6198 Src_File := Strip_Directory (Main_Source_File);
6199 ALI_File := Lib_File_Name (Src_File, Current_Main_Index);
6200 Main_ALI_File := Full_Lib_File_Name (ALI_File);
6202 -- When In_Place_Mode, the library file can be located in the
6203 -- Main_Source_File directory which may not be present in the
6204 -- library path. If it is not present then use the corresponding
6205 -- library file name.
6207 if Main_ALI_File = No_File and then In_Place_Mode then
6208 Get_Name_String (Get_Directory (Full_Source_Name (Src_File)));
6209 Get_Name_String_And_Append (ALI_File);
6210 Main_ALI_File := Name_Find;
6211 Main_ALI_File := Full_Lib_File_Name (Main_ALI_File);
6212 end if;
6214 if Main_ALI_File = No_File then
6215 Make_Failed ("could not find the main ALI file");
6216 end if;
6217 end Main_ALI_In_Place_Mode_Step;
6219 if Do_Bind_Step then
6220 Binding_Phase
6221 (Stand_Alone_Libraries => Stand_Alone_Libraries,
6222 Main_ALI_File => Main_ALI_File);
6223 end if;
6225 if Do_Link_Step then
6226 Linking_Phase
6227 (Non_Std_Executable => Non_Std_Executable,
6228 Executable => Executable,
6229 Main_ALI_File => Main_ALI_File);
6230 end if;
6232 -- We go to here when we skip the bind and link steps
6234 <<Next_Main>>
6236 Queue.Remove_Marks;
6238 if N_File < Osint.Number_Of_Files then
6239 Main_Source_File := Next_Main_Source; -- No directory information
6240 end if;
6241 end loop Multiple_Main_Loop;
6243 if CodePeer_Mode then
6244 declare
6245 Success : Boolean := False;
6246 begin
6247 Globalize (Success);
6249 if not Success then
6250 Set_Standard_Error;
6251 Write_Str ("*** globalize failed.");
6253 if Commands_To_Stdout then
6254 Set_Standard_Output;
6255 end if;
6256 end if;
6257 end;
6258 end if;
6260 if Failed_Links.Last > 0 then
6261 for Index in 1 .. Successful_Links.Last loop
6262 Write_Str ("Linking of """);
6263 Write_Str (Get_Name_String (Successful_Links.Table (Index)));
6264 Write_Line (""" succeeded.");
6265 end loop;
6267 Set_Standard_Error;
6269 for Index in 1 .. Failed_Links.Last loop
6270 Write_Str ("Linking of """);
6271 Write_Str (Get_Name_String (Failed_Links.Table (Index)));
6272 Write_Line (""" failed.");
6273 end loop;
6275 if Commands_To_Stdout then
6276 Set_Standard_Output;
6277 end if;
6279 if Total_Compilation_Failures = 0 then
6280 Report_Compilation_Failed;
6281 end if;
6282 end if;
6284 if Total_Compilation_Failures /= 0 then
6285 List_Bad_Compilations;
6286 Report_Compilation_Failed;
6287 end if;
6289 Finish_Program (Project_Tree, E_Success);
6291 exception
6292 when X : others =>
6293 Set_Standard_Error;
6294 Write_Line (Exception_Information (X));
6295 Make_Failed ("INTERNAL ERROR. Please report.");
6296 end Gnatmake;
6298 ----------
6299 -- Hash --
6300 ----------
6302 function Hash (F : File_Name_Type) return Header_Num is
6303 begin
6304 return Header_Num (1 + F mod Max_Header);
6305 end Hash;
6307 --------------------
6308 -- In_Ada_Lib_Dir --
6309 --------------------
6311 function In_Ada_Lib_Dir (File : File_Name_Type) return Boolean is
6312 D : constant File_Name_Type := Get_Directory (File);
6313 B : constant Byte := Get_Name_Table_Byte (D);
6314 begin
6315 return (B and Ada_Lib_Dir) /= 0;
6316 end In_Ada_Lib_Dir;
6318 -----------------------
6319 -- Init_Mapping_File --
6320 -----------------------
6322 procedure Init_Mapping_File
6323 (Project : Project_Id;
6324 Data : in out Project_Compilation_Data;
6325 File_Index : in out Natural)
6327 FD : File_Descriptor;
6328 Status : Boolean;
6329 -- For call to Close
6331 begin
6332 -- Increase the index of the last mapping file for this project
6334 Data.Last_Mapping_File_Names := Data.Last_Mapping_File_Names + 1;
6336 -- If there is a project file, call Create_Mapping_File with
6337 -- the project id.
6339 if Project /= No_Project then
6340 Prj.Env.Create_Mapping_File
6341 (Project,
6342 In_Tree => Project_Tree,
6343 Language => Name_Ada,
6344 Name => Data.Mapping_File_Names
6345 (Data.Last_Mapping_File_Names));
6347 -- Otherwise, just create an empty file
6349 else
6350 Tempdir.Create_Temp_File
6351 (FD,
6352 Data.Mapping_File_Names (Data.Last_Mapping_File_Names));
6354 if FD = Invalid_FD then
6355 Make_Failed ("disk full");
6357 else
6358 Record_Temp_File
6359 (Project_Tree.Shared,
6360 Data.Mapping_File_Names (Data.Last_Mapping_File_Names));
6361 end if;
6363 Close (FD, Status);
6365 if not Status then
6366 Make_Failed ("disk full");
6367 end if;
6368 end if;
6370 -- And return the index of the newly created file
6372 File_Index := Data.Last_Mapping_File_Names;
6373 end Init_Mapping_File;
6375 ----------------
6376 -- Initialize --
6377 ----------------
6379 procedure Initialize
6380 (Project_Node_Tree : out Project_Node_Tree_Ref;
6381 Env : out Prj.Tree.Environment)
6383 procedure Check_Version_And_Help is
6384 new Check_Version_And_Help_G (Makeusg);
6386 -- Start of processing for Initialize
6388 begin
6389 -- Prepare the project's tree, since this is used to hold external
6390 -- references, project path and other attributes that can be impacted by
6391 -- the command line switches
6393 Prj.Tree.Initialize (Env, Gnatmake_Flags);
6395 Project_Node_Tree := new Project_Node_Tree_Data;
6396 Prj.Tree.Initialize (Project_Node_Tree);
6398 -- Override default initialization of Check_Object_Consistency since
6399 -- this is normally False for GNATBIND, but is True for GNATMAKE since
6400 -- we do not need to check source consistency again once GNATMAKE has
6401 -- looked at the sources to check.
6403 Check_Object_Consistency := True;
6405 -- Package initializations (the order of calls is important here)
6407 Output.Set_Standard_Error;
6409 Gcc_Switches.Init;
6410 Binder_Switches.Init;
6411 Linker_Switches.Init;
6413 Csets.Initialize;
6414 Snames.Initialize;
6415 Stringt.Initialize;
6417 Prj.Initialize (Project_Tree);
6419 Dependencies.Init;
6421 RTS_Specified := null;
6422 N_M_Switch := 0;
6424 Mains.Delete;
6426 -- Add the directory where gnatmake is invoked in front of the path,
6427 -- if gnatmake is invoked from a bin directory or with directory
6428 -- information. Only do this if the platform is not VMS, where the
6429 -- notion of path does not really exist.
6431 if not OpenVMS then
6432 declare
6433 Prefix : constant String := Executable_Prefix_Path;
6434 Command : constant String := Command_Name;
6436 begin
6437 if Prefix'Length > 0 then
6438 declare
6439 PATH : constant String :=
6440 Prefix & Directory_Separator & "bin" & Path_Separator &
6441 Getenv ("PATH").all;
6442 begin
6443 Setenv ("PATH", PATH);
6444 end;
6446 else
6447 for Index in reverse Command'Range loop
6448 if Command (Index) = Directory_Separator then
6449 declare
6450 Absolute_Dir : constant String :=
6451 Normalize_Pathname
6452 (Command (Command'First .. Index));
6453 PATH : constant String :=
6454 Absolute_Dir &
6455 Path_Separator &
6456 Getenv ("PATH").all;
6457 begin
6458 Setenv ("PATH", PATH);
6459 end;
6461 exit;
6462 end if;
6463 end loop;
6464 end if;
6465 end;
6466 end if;
6468 -- Scan the switches and arguments
6470 -- First, scan to detect --version and/or --help
6472 Check_Version_And_Help ("GNATMAKE", "1995");
6474 -- Scan again the switch and arguments, now that we are sure that they
6475 -- do not include --version or --help.
6477 Scan_Args : for Next_Arg in 1 .. Argument_Count loop
6478 Scan_Make_Arg (Env, Argument (Next_Arg), And_Save => True);
6479 end loop Scan_Args;
6481 if N_M_Switch > 0 and RTS_Specified = null then
6482 Process_Multilib (Env);
6483 end if;
6485 if Commands_To_Stdout then
6486 Set_Standard_Output;
6487 end if;
6489 if Usage_Requested then
6490 Usage;
6491 end if;
6493 -- Add the default project search directories now, after the directories
6494 -- that have been specified by switches -aP<dir>.
6496 Prj.Env.Initialize_Default_Project_Path
6497 (Env.Project_Path, Target_Name => Sdefault.Target_Name.all);
6499 -- Test for trailing -P switch
6501 if Project_File_Name_Present and then Project_File_Name = null then
6502 Make_Failed ("project file name missing after -P");
6504 -- Test for trailing -o switch
6506 elsif Output_File_Name_Present
6507 and then not Output_File_Name_Seen
6508 then
6509 Make_Failed ("output file name missing after -o");
6511 -- Test for trailing -D switch
6513 elsif Object_Directory_Present
6514 and then not Object_Directory_Seen
6515 then
6516 Make_Failed ("object directory missing after -D");
6517 end if;
6519 -- Test for simultaneity of -i and -D
6521 if Object_Directory_Path /= null and then In_Place_Mode then
6522 Make_Failed ("-i and -D cannot be used simultaneously");
6523 end if;
6525 -- If --subdirs= is specified, but not -P, this is equivalent to -D,
6526 -- except that the directory is created if it does not exist.
6528 if Prj.Subdirs /= null and then Project_File_Name = null then
6529 if Object_Directory_Path /= null then
6530 Make_Failed ("--subdirs and -D cannot be used simultaneously");
6532 elsif In_Place_Mode then
6533 Make_Failed ("--subdirs and -i cannot be used simultaneously");
6535 else
6536 if not Is_Directory (Prj.Subdirs.all) then
6537 begin
6538 Ada.Directories.Create_Path (Prj.Subdirs.all);
6539 exception
6540 when others =>
6541 Make_Failed ("unable to create object directory " &
6542 Prj.Subdirs.all);
6543 end;
6544 end if;
6546 Object_Directory_Present := True;
6548 declare
6549 Argv : constant String (1 .. Prj.Subdirs'Length) :=
6550 Prj.Subdirs.all;
6551 begin
6552 Scan_Make_Arg (Env, Argv, And_Save => False);
6553 end;
6554 end if;
6555 end if;
6557 -- Deal with -C= switch
6559 if Gnatmake_Mapping_File /= null then
6561 -- First, check compatibility with other switches
6563 if Project_File_Name /= null then
6564 Make_Failed ("-C= switch is not compatible with -P switch");
6566 elsif Saved_Maximum_Processes > 1 then
6567 Make_Failed ("-C= switch is not compatible with -jnnn switch");
6568 end if;
6570 Fmap.Initialize (Gnatmake_Mapping_File.all);
6571 Add_Switch
6572 ("-gnatem=" & Gnatmake_Mapping_File.all,
6573 Compiler,
6574 And_Save => True);
6575 end if;
6577 if Project_File_Name /= null then
6579 -- A project file was specified by a -P switch
6581 if Verbose_Mode then
6582 Write_Eol;
6583 Write_Str ("Parsing project file """);
6584 Write_Str (Project_File_Name.all);
6585 Write_Str (""".");
6586 Write_Eol;
6587 end if;
6589 -- Avoid looking in the current directory for ALI files
6591 -- Look_In_Primary_Dir := False;
6593 -- Set the project parsing verbosity to whatever was specified
6594 -- by a possible -vP switch.
6596 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
6598 -- Parse the project file.
6599 -- If there is an error, Main_Project will still be No_Project.
6601 Prj.Pars.Parse
6602 (Project => Main_Project,
6603 In_Tree => Project_Tree,
6604 Project_File_Name => Project_File_Name.all,
6605 Packages_To_Check => Packages_To_Check_By_Gnatmake,
6606 Env => Env,
6607 In_Node_Tree => Project_Node_Tree);
6609 -- The parsing of project files may have changed the current output
6611 if Commands_To_Stdout then
6612 Set_Standard_Output;
6613 else
6614 Set_Standard_Error;
6615 end if;
6617 if Main_Project = No_Project then
6618 Make_Failed
6619 ("""" & Project_File_Name.all & """ processing failed");
6620 end if;
6622 if Main_Project.Qualifier = Aggregate then
6623 Make_Failed ("aggregate projects are not supported");
6625 elsif Aggregate_Libraries_In (Project_Tree) then
6626 Make_Failed ("aggregate library projects are not supported");
6627 end if;
6629 Create_Mapping_File := True;
6631 if Verbose_Mode then
6632 Write_Eol;
6633 Write_Str ("Parsing of project file """);
6634 Write_Str (Project_File_Name.all);
6635 Write_Str (""" is finished.");
6636 Write_Eol;
6637 end if;
6639 -- We add the source directories and the object directories to the
6640 -- search paths.
6642 -- ??? Why do we need these search directories, we already know the
6643 -- locations from parsing the project, except for the runtime which
6644 -- has its own directories anyway
6646 Add_Source_Directories (Main_Project, Project_Tree);
6647 Add_Object_Directories (Main_Project, Project_Tree);
6649 Recursive_Compute_Depth (Main_Project);
6650 Compute_All_Imported_Projects (Main_Project, Project_Tree);
6652 else
6654 Osint.Add_Default_Search_Dirs;
6656 -- Source file lookups should be cached for efficiency. Source files
6657 -- are not supposed to change. However, we do that now only if no
6658 -- project file is used; if a project file is used, we do it just
6659 -- after changing the directory to the object directory.
6661 Osint.Source_File_Data (Cache => True);
6663 -- Read gnat.adc file to initialize Fname.UF
6665 Fname.UF.Initialize;
6667 begin
6668 Fname.SF.Read_Source_File_Name_Pragmas;
6670 exception
6671 when Err : SFN_Scan.Syntax_Error_In_GNAT_ADC =>
6672 Make_Failed (Exception_Message (Err));
6673 end;
6674 end if;
6676 -- Make sure no project object directory is recorded
6678 Project_Of_Current_Object_Directory := No_Project;
6680 end Initialize;
6682 ----------------------------
6683 -- Insert_Project_Sources --
6684 ----------------------------
6686 procedure Insert_Project_Sources
6687 (The_Project : Project_Id;
6688 All_Projects : Boolean;
6689 Into_Q : Boolean)
6691 Put_In_Q : Boolean := Into_Q;
6692 Unit : Unit_Index;
6693 Sfile : File_Name_Type;
6694 Sid : Prj.Source_Id;
6695 Index : Int;
6696 Project : Project_Id;
6698 begin
6699 -- Loop through all the sources in the project files
6701 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
6702 while Unit /= null loop
6703 Sfile := No_File;
6704 Sid := No_Source;
6705 Index := 0;
6706 Project := No_Project;
6708 -- If there is a source for the body, and the body has not been
6709 -- locally removed.
6711 if Unit.File_Names (Impl) /= null
6712 and then not Unit.File_Names (Impl).Locally_Removed
6713 then
6714 -- And it is a source for the specified project
6716 if All_Projects
6717 or else
6718 Is_Extending (The_Project, Unit.File_Names (Impl).Project)
6719 then
6720 Project := Unit.File_Names (Impl).Project;
6722 -- If we don't have a spec, we cannot consider the source
6723 -- if it is a subunit.
6725 if Unit.File_Names (Spec) = null then
6726 declare
6727 Src_Ind : Source_File_Index;
6729 -- Here we are cheating a little bit: we don't want to
6730 -- use Sinput.L, because it depends on the GNAT tree
6731 -- (Atree, Sinfo, ...). So, we pretend that it is a
6732 -- project file, and we use Sinput.P.
6734 -- Source_File_Is_Subunit is just scanning through the
6735 -- file until it finds one of the reserved words
6736 -- separate, procedure, function, generic or package.
6737 -- Fortunately, these Ada reserved words are also
6738 -- reserved for project files.
6740 begin
6741 Src_Ind := Sinput.P.Load_Project_File
6742 (Get_Name_String
6743 (Unit.File_Names (Impl).Path.Display_Name));
6745 -- If it is a subunit, discard it
6747 if Sinput.P.Source_File_Is_Subunit (Src_Ind) then
6748 Sfile := No_File;
6749 Index := 0;
6750 Sid := No_Source;
6751 else
6752 Sfile := Unit.File_Names (Impl).Display_File;
6753 Index := Unit.File_Names (Impl).Index;
6754 Sid := Unit.File_Names (Impl);
6755 end if;
6756 end;
6758 else
6759 Sfile := Unit.File_Names (Impl).Display_File;
6760 Index := Unit.File_Names (Impl).Index;
6761 Sid := Unit.File_Names (Impl);
6762 end if;
6763 end if;
6765 elsif Unit.File_Names (Spec) /= null
6766 and then not Unit.File_Names (Spec).Locally_Removed
6767 and then
6768 (All_Projects
6769 or else
6770 Is_Extending (The_Project, Unit.File_Names (Spec).Project))
6771 then
6772 -- If there is no source for the body, but there is one for the
6773 -- spec which has not been locally removed, then we take this one.
6775 Sfile := Unit.File_Names (Spec).Display_File;
6776 Index := Unit.File_Names (Spec).Index;
6777 Sid := Unit.File_Names (Spec);
6778 Project := Unit.File_Names (Spec).Project;
6779 end if;
6781 -- For the first source inserted into the Q, we need to initialize
6782 -- the Q, but not for the subsequent sources.
6784 Queue.Initialize
6785 (Main_Project /= No_Project and then
6786 One_Compilation_Per_Obj_Dir);
6788 if Sfile /= No_File then
6789 Queue.Insert
6790 ((Format => Format_Gnatmake,
6791 File => Sfile,
6792 Project => Project,
6793 Unit => No_Unit_Name,
6794 Index => Index,
6795 Sid => Sid));
6796 end if;
6798 if not Put_In_Q and then Sfile /= No_File then
6800 -- If Put_In_Q is False, we add the source as if it were specified
6801 -- on the command line, and we set Put_In_Q to True, so that the
6802 -- following sources will only be put in the queue. The source is
6803 -- already in the Q, but we need at least one fake main to call
6804 -- Compile_Sources.
6806 if Verbose_Mode then
6807 Write_Str ("Adding """);
6808 Write_Str (Get_Name_String (Sfile));
6809 Write_Line (""" as if on the command line");
6810 end if;
6812 Osint.Add_File (Get_Name_String (Sfile), Index);
6813 Put_In_Q := True;
6814 end if;
6816 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
6817 end loop;
6818 end Insert_Project_Sources;
6820 ---------------------
6821 -- Is_In_Obsoleted --
6822 ---------------------
6824 function Is_In_Obsoleted (F : File_Name_Type) return Boolean is
6825 begin
6826 if F = No_File then
6827 return False;
6829 else
6830 declare
6831 Name : constant String := Get_Name_String (F);
6832 First : Natural;
6833 F2 : File_Name_Type;
6835 begin
6836 First := Name'Last;
6837 while First > Name'First
6838 and then Name (First - 1) /= Directory_Separator
6839 and then Name (First - 1) /= '/'
6840 loop
6841 First := First - 1;
6842 end loop;
6844 if First /= Name'First then
6845 Name_Len := 0;
6846 Add_Str_To_Name_Buffer (Name (First .. Name'Last));
6847 F2 := Name_Find;
6849 else
6850 F2 := F;
6851 end if;
6853 return Obsoleted.Get (F2);
6854 end;
6855 end if;
6856 end Is_In_Obsoleted;
6858 ----------------------------
6859 -- Is_In_Object_Directory --
6860 ----------------------------
6862 function Is_In_Object_Directory
6863 (Source_File : File_Name_Type;
6864 Full_Lib_File : File_Name_Type) return Boolean
6866 begin
6867 -- There is something to check only when using project files. Otherwise,
6868 -- this function returns True (last line of the function).
6870 if Main_Project /= No_Project then
6871 declare
6872 Source_File_Name : constant String :=
6873 Get_Name_String (Source_File);
6874 Saved_Verbosity : constant Verbosity := Current_Verbosity;
6875 Project : Project_Id := No_Project;
6877 Path_Name : Path_Name_Type := No_Path;
6878 pragma Warnings (Off, Path_Name);
6880 begin
6881 -- Call Get_Reference to know the ultimate extending project of
6882 -- the source. Call it with verbosity default to avoid verbose
6883 -- messages.
6885 Current_Verbosity := Default;
6886 Prj.Env.Get_Reference
6887 (Source_File_Name => Source_File_Name,
6888 Project => Project,
6889 In_Tree => Project_Tree,
6890 Path => Path_Name);
6891 Current_Verbosity := Saved_Verbosity;
6893 -- If this source is in a project, check that the ALI file is in
6894 -- its object directory. If it is not, return False, so that the
6895 -- ALI file will not be skipped.
6897 if Project /= No_Project then
6898 declare
6899 Object_Directory : constant String :=
6900 Normalize_Pathname
6901 (Get_Name_String
6902 (Project.
6903 Object_Directory.Display_Name));
6905 Olast : Natural := Object_Directory'Last;
6907 Lib_File_Directory : constant String :=
6908 Normalize_Pathname (Dir_Name
6909 (Get_Name_String (Full_Lib_File)));
6911 Llast : Natural := Lib_File_Directory'Last;
6913 begin
6914 -- For directories, Normalize_Pathname may or may not put
6915 -- a directory separator at the end, depending on its input.
6916 -- Remove any last directory separator before comparison.
6917 -- Returns True only if the two directories are the same.
6919 if Object_Directory (Olast) = Directory_Separator then
6920 Olast := Olast - 1;
6921 end if;
6923 if Lib_File_Directory (Llast) = Directory_Separator then
6924 Llast := Llast - 1;
6925 end if;
6927 return Object_Directory (Object_Directory'First .. Olast) =
6928 Lib_File_Directory (Lib_File_Directory'First .. Llast);
6929 end;
6930 end if;
6931 end;
6932 end if;
6934 -- When the source is not in a project file, always return True
6936 return True;
6937 end Is_In_Object_Directory;
6939 ----------
6940 -- Link --
6941 ----------
6943 procedure Link
6944 (ALI_File : File_Name_Type;
6945 Args : Argument_List;
6946 Success : out Boolean)
6948 Link_Args : Argument_List (1 .. Args'Length + 1);
6950 begin
6951 Get_Name_String (ALI_File);
6952 Link_Args (1) := new String'(Name_Buffer (1 .. Name_Len));
6954 Link_Args (2 .. Args'Length + 1) := Args;
6956 GNAT.OS_Lib.Normalize_Arguments (Link_Args);
6958 Display (Gnatlink.all, Link_Args);
6960 if Gnatlink_Path = null then
6961 Make_Failed ("error, unable to locate " & Gnatlink.all);
6962 end if;
6964 GNAT.OS_Lib.Spawn (Gnatlink_Path.all, Link_Args, Success);
6965 end Link;
6967 ---------------------------
6968 -- List_Bad_Compilations --
6969 ---------------------------
6971 procedure List_Bad_Compilations is
6972 begin
6973 for J in Bad_Compilation.First .. Bad_Compilation.Last loop
6974 if Bad_Compilation.Table (J).File = No_File then
6975 null;
6976 elsif not Bad_Compilation.Table (J).Found then
6977 Inform (Bad_Compilation.Table (J).File, "not found");
6978 else
6979 Inform (Bad_Compilation.Table (J).File, "compilation error");
6980 end if;
6981 end loop;
6982 end List_Bad_Compilations;
6984 -----------------
6985 -- List_Depend --
6986 -----------------
6988 procedure List_Depend is
6989 Lib_Name : File_Name_Type;
6990 Obj_Name : File_Name_Type;
6991 Src_Name : File_Name_Type;
6993 Len : Natural;
6994 Line_Pos : Natural;
6995 Line_Size : constant := 77;
6997 begin
6998 Set_Standard_Output;
7000 for A in ALIs.First .. ALIs.Last loop
7001 Lib_Name := ALIs.Table (A).Afile;
7003 -- We have to provide the full library file name in In_Place_Mode
7005 if In_Place_Mode then
7006 Lib_Name := Full_Lib_File_Name (Lib_Name);
7007 end if;
7009 Obj_Name := Object_File_Name (Lib_Name);
7010 Write_Name (Obj_Name);
7011 Write_Str (" :");
7013 Get_Name_String (Obj_Name);
7014 Len := Name_Len;
7015 Line_Pos := Len + 2;
7017 for D in ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep loop
7018 Src_Name := Sdep.Table (D).Sfile;
7020 if Is_Internal_File_Name (Src_Name)
7021 and then not Check_Readonly_Files
7022 then
7023 null;
7024 else
7025 if not Quiet_Output then
7026 Src_Name := Full_Source_Name (Src_Name);
7027 end if;
7029 Get_Name_String (Src_Name);
7030 Len := Name_Len;
7032 if Line_Pos + Len + 1 > Line_Size then
7033 Write_Str (" \");
7034 Write_Eol;
7035 Line_Pos := 0;
7036 end if;
7038 Line_Pos := Line_Pos + Len + 1;
7040 Write_Str (" ");
7041 Write_Name (Src_Name);
7042 end if;
7043 end loop;
7045 Write_Eol;
7046 end loop;
7048 if not Commands_To_Stdout then
7049 Set_Standard_Error;
7050 end if;
7051 end List_Depend;
7053 -----------------
7054 -- Make_Failed --
7055 -----------------
7057 procedure Make_Failed (S : String) is
7058 begin
7059 Fail_Program (Project_Tree, S);
7060 end Make_Failed;
7062 --------------------
7063 -- Mark_Directory --
7064 --------------------
7066 procedure Mark_Directory
7067 (Dir : String;
7068 Mark : Lib_Mark_Type;
7069 On_Command_Line : Boolean)
7071 N : Name_Id;
7072 B : Byte;
7074 function Base_Directory return String;
7075 -- If Dir comes from the command line, empty string (relative paths are
7076 -- resolved with respect to the current directory), else return the main
7077 -- project's directory.
7079 --------------------
7080 -- Base_Directory --
7081 --------------------
7083 function Base_Directory return String is
7084 begin
7085 if On_Command_Line then
7086 return "";
7087 else
7088 return Get_Name_String (Main_Project.Directory.Display_Name);
7089 end if;
7090 end Base_Directory;
7092 Real_Path : constant String := Normalize_Pathname (Dir, Base_Directory);
7094 -- Start of processing for Mark_Directory
7096 begin
7097 Name_Len := 0;
7099 if Real_Path'Length = 0 then
7100 Add_Str_To_Name_Buffer (Dir);
7102 else
7103 Add_Str_To_Name_Buffer (Real_Path);
7104 end if;
7106 -- Last character is supposed to be a directory separator
7108 if not Is_Directory_Separator (Name_Buffer (Name_Len)) then
7109 Add_Char_To_Name_Buffer (Directory_Separator);
7110 end if;
7112 -- Add flags to the already existing flags
7114 N := Name_Find;
7115 B := Get_Name_Table_Byte (N);
7116 Set_Name_Table_Byte (N, B or Mark);
7117 end Mark_Directory;
7119 ----------------------
7120 -- Process_Multilib --
7121 ----------------------
7123 procedure Process_Multilib (Env : in out Prj.Tree.Environment) is
7124 Output_FD : File_Descriptor;
7125 Output_Name : String_Access;
7126 Arg_Index : Natural := 0;
7127 Success : Boolean := False;
7128 Return_Code : Integer := 0;
7129 Multilib_Gcc_Path : String_Access;
7130 Multilib_Gcc : String_Access;
7131 N_Read : Integer := 0;
7132 Line : String (1 .. 1000);
7133 Args : Argument_List (1 .. N_M_Switch + 1);
7135 begin
7136 pragma Assert (N_M_Switch > 0 and RTS_Specified = null);
7138 -- In case we detected a multilib switch and the user has not
7139 -- manually specified a specific RTS we emulate the following command:
7140 -- gnatmake $FLAGS --RTS=$(gcc -print-multi-directory $FLAGS)
7142 -- First select the flags which might have an impact on multilib
7143 -- processing. Note that this is an heuristic selection and it
7144 -- will need to be maintained over time. The condition has to
7145 -- be kept synchronized with N_M_Switch counting in Scan_Make_Arg.
7147 for Next_Arg in 1 .. Argument_Count loop
7148 declare
7149 Argv : constant String := Argument (Next_Arg);
7151 begin
7152 if Argv'Length > 2
7153 and then Argv (1) = '-'
7154 and then Argv (2) = 'm'
7155 and then Argv /= "-margs"
7157 -- Ignore -mieee to avoid spawning an extra gcc in this case
7159 and then Argv /= "-mieee"
7160 then
7161 Arg_Index := Arg_Index + 1;
7162 Args (Arg_Index) := new String'(Argv);
7163 end if;
7164 end;
7165 end loop;
7167 pragma Assert (Arg_Index = N_M_Switch);
7169 Args (Args'Last) := new String'("-print-multi-directory");
7171 -- Call the GCC driver with the collected flags and save its
7172 -- output. Alternate design would be to link in gnatmake the
7173 -- relevant part of the GCC driver.
7175 if Saved_Gcc /= null then
7176 Multilib_Gcc := Saved_Gcc;
7177 else
7178 Multilib_Gcc := Gcc;
7179 end if;
7181 Multilib_Gcc_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Multilib_Gcc.all);
7183 Create_Temp_Output_File (Output_FD, Output_Name);
7185 if Output_FD = Invalid_FD then
7186 return;
7187 end if;
7189 GNAT.OS_Lib.Spawn
7190 (Multilib_Gcc_Path.all, Args, Output_FD, Return_Code, False);
7191 Close (Output_FD);
7193 if Return_Code /= 0 then
7194 return;
7195 end if;
7197 -- Parse the GCC driver output which is a single line, removing CR/LF
7199 Output_FD := Open_Read (Output_Name.all, Binary);
7201 if Output_FD = Invalid_FD then
7202 return;
7203 end if;
7205 N_Read := Read (Output_FD, Line (1)'Address, Line'Length);
7206 Close (Output_FD);
7207 Delete_File (Output_Name.all, Success);
7209 for J in reverse 1 .. N_Read loop
7210 if Line (J) = ASCII.CR or else Line (J) = ASCII.LF then
7211 N_Read := N_Read - 1;
7212 else
7213 exit;
7214 end if;
7215 end loop;
7217 -- In case the standard RTS is selected do nothing
7219 if N_Read = 0 or else Line (1 .. N_Read) = "." then
7220 return;
7221 end if;
7223 -- Otherwise add -margs --RTS=output
7225 Scan_Make_Arg (Env, "-margs", And_Save => True);
7226 Scan_Make_Arg (Env, "--RTS=" & Line (1 .. N_Read), And_Save => True);
7227 end Process_Multilib;
7229 -----------------------------
7230 -- Recursive_Compute_Depth --
7231 -----------------------------
7233 procedure Recursive_Compute_Depth (Project : Project_Id) is
7234 use Project_Boolean_Htable;
7235 Seen : Project_Boolean_Htable.Instance := Project_Boolean_Htable.Nil;
7237 procedure Recurse (Prj : Project_Id; Depth : Natural);
7238 -- Recursive procedure that does the work, keeping track of the depth
7240 -------------
7241 -- Recurse --
7242 -------------
7244 procedure Recurse (Prj : Project_Id; Depth : Natural) is
7245 List : Project_List;
7246 Proj : Project_Id;
7248 begin
7249 if Prj.Depth >= Depth or else Get (Seen, Prj) then
7250 return;
7251 end if;
7253 -- We need a test to avoid infinite recursions with limited withs:
7254 -- If we have A -> B -> A, then when set level of A to n, we try and
7255 -- set level of B to n+1, and then level of A to n + 2, ...
7257 Set (Seen, Prj, True);
7259 Prj.Depth := Depth;
7261 -- Visit each imported project
7263 List := Prj.Imported_Projects;
7264 while List /= null loop
7265 Proj := List.Project;
7266 List := List.Next;
7267 Recurse (Prj => Proj, Depth => Depth + 1);
7268 end loop;
7270 -- We again allow changing the depth of this project later on if it
7271 -- is in fact imported by a lower-level project.
7273 Set (Seen, Prj, False);
7274 end Recurse;
7276 Proj : Project_List;
7278 -- Start of processing for Recursive_Compute_Depth
7280 begin
7281 Proj := Project_Tree.Projects;
7282 while Proj /= null loop
7283 Proj.Project.Depth := 0;
7284 Proj := Proj.Next;
7285 end loop;
7287 Recurse (Project, Depth => 1);
7288 Reset (Seen);
7289 end Recursive_Compute_Depth;
7291 -------------------------------
7292 -- Report_Compilation_Failed --
7293 -------------------------------
7295 procedure Report_Compilation_Failed is
7296 begin
7297 Fail_Program (Project_Tree, "");
7298 end Report_Compilation_Failed;
7300 ------------------------
7301 -- Sigint_Intercepted --
7302 ------------------------
7304 procedure Sigint_Intercepted is
7305 SIGINT : constant := 2;
7307 begin
7308 Set_Standard_Error;
7309 Write_Line ("*** Interrupted ***");
7311 -- Send SIGINT to all outstanding compilation processes spawned
7313 for J in 1 .. Outstanding_Compiles loop
7314 Kill (Running_Compile (J).Pid, SIGINT, 1);
7315 end loop;
7317 Finish_Program (Project_Tree, E_No_Compile);
7318 end Sigint_Intercepted;
7320 -------------------
7321 -- Scan_Make_Arg --
7322 -------------------
7324 procedure Scan_Make_Arg
7325 (Env : in out Prj.Tree.Environment;
7326 Argv : String;
7327 And_Save : Boolean)
7329 Success : Boolean;
7331 begin
7332 Gnatmake_Switch_Found := True;
7334 pragma Assert (Argv'First = 1);
7336 if Argv'Length = 0 then
7337 return;
7338 end if;
7340 -- If the previous switch has set the Project_File_Name_Present flag
7341 -- (that is we have seen a -P alone), then the next argument is the name
7342 -- of the project file.
7344 if Project_File_Name_Present and then Project_File_Name = null then
7345 if Argv (1) = '-' then
7346 Make_Failed ("project file name missing after -P");
7348 else
7349 Project_File_Name_Present := False;
7350 Project_File_Name := new String'(Argv);
7351 end if;
7353 -- If the previous switch has set the Output_File_Name_Present flag
7354 -- (that is we have seen a -o), then the next argument is the name of
7355 -- the output executable.
7357 elsif Output_File_Name_Present
7358 and then not Output_File_Name_Seen
7359 then
7360 Output_File_Name_Seen := True;
7362 if Argv (1) = '-' then
7363 Make_Failed ("output file name missing after -o");
7365 else
7366 Add_Switch ("-o", Linker, And_Save => And_Save);
7367 Add_Switch (Executable_Name (Argv), Linker, And_Save => And_Save);
7368 end if;
7370 -- If the previous switch has set the Object_Directory_Present flag
7371 -- (that is we have seen a -D), then the next argument is the path name
7372 -- of the object directory.
7374 elsif Object_Directory_Present
7375 and then not Object_Directory_Seen
7376 then
7377 Object_Directory_Seen := True;
7379 if Argv (1) = '-' then
7380 Make_Failed ("object directory path name missing after -D");
7382 elsif not Is_Directory (Argv) then
7383 Make_Failed ("cannot find object directory """ & Argv & """");
7385 else
7386 -- Record the object directory. Make sure it ends with a directory
7387 -- separator.
7389 declare
7390 Norm : constant String := Normalize_Pathname (Argv);
7392 begin
7393 if Norm (Norm'Last) = Directory_Separator then
7394 Object_Directory_Path := new String'(Norm);
7395 else
7396 Object_Directory_Path :=
7397 new String'(Norm & Directory_Separator);
7398 end if;
7400 Add_Lib_Search_Dir (Norm);
7402 -- Specify the object directory to the binder
7404 Add_Switch ("-aO" & Norm, Binder, And_Save => And_Save);
7405 end;
7407 end if;
7409 -- Then check if we are dealing with -cargs/-bargs/-largs/-margs. These
7410 -- options are taken as is when found in package Compiler, Binder or
7411 -- Linker of the main project file.
7413 elsif (And_Save or else Program_Args = None)
7414 and then (Argv = "-bargs" or else
7415 Argv = "-cargs" or else
7416 Argv = "-largs" or else
7417 Argv = "-margs")
7418 then
7419 case Argv (2) is
7420 when 'c' => Program_Args := Compiler;
7421 when 'b' => Program_Args := Binder;
7422 when 'l' => Program_Args := Linker;
7423 when 'm' => Program_Args := None;
7425 when others =>
7426 raise Program_Error;
7427 end case;
7429 -- A special test is needed for the -o switch within a -largs since that
7430 -- is another way to specify the name of the final executable.
7432 elsif Program_Args = Linker
7433 and then Argv = "-o"
7434 then
7435 Make_Failed
7436 ("switch -o not allowed within a -largs. Use -o directly.");
7438 -- Check to see if we are reading switches after a -cargs, -bargs or
7439 -- -largs switch. If so, save it.
7441 elsif Program_Args /= None then
7443 -- Check to see if we are reading -I switches in order to take into
7444 -- account in the src & lib search directories.
7446 if Argv'Length > 2 and then Argv (1 .. 2) = "-I" then
7447 if Argv (3 .. Argv'Last) = "-" then
7448 Look_In_Primary_Dir := False;
7450 elsif Program_Args = Compiler then
7451 if Argv (3 .. Argv'Last) /= "-" then
7452 Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7453 end if;
7455 elsif Program_Args = Binder then
7456 Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7457 end if;
7458 end if;
7460 Add_Switch (Argv, Program_Args, And_Save => And_Save);
7462 -- Make sure that all significant switches -m on the command line
7463 -- are counted.
7465 if Argv'Length > 2
7466 and then Argv (1 .. 2) = "-m"
7467 and then Argv /= "-mieee"
7468 then
7469 N_M_Switch := N_M_Switch + 1;
7470 end if;
7472 -- Handle non-default compiler, binder, linker, and handle --RTS switch
7474 elsif Argv'Length > 2 and then Argv (1 .. 2) = "--" then
7475 if Argv'Length > 6
7476 and then Argv (1 .. 6) = "--GCC="
7477 then
7478 declare
7479 Program_Args : constant Argument_List_Access :=
7480 Argument_String_To_List
7481 (Argv (7 .. Argv'Last));
7483 begin
7484 if And_Save then
7485 Saved_Gcc := new String'(Program_Args.all (1).all);
7486 else
7487 Gcc := new String'(Program_Args.all (1).all);
7488 end if;
7490 for J in 2 .. Program_Args.all'Last loop
7491 Add_Switch
7492 (Program_Args.all (J).all, Compiler, And_Save => And_Save);
7493 end loop;
7494 end;
7496 elsif Argv'Length > 11
7497 and then Argv (1 .. 11) = "--GNATBIND="
7498 then
7499 declare
7500 Program_Args : constant Argument_List_Access :=
7501 Argument_String_To_List
7502 (Argv (12 .. Argv'Last));
7504 begin
7505 if And_Save then
7506 Saved_Gnatbind := new String'(Program_Args.all (1).all);
7507 else
7508 Gnatbind := new String'(Program_Args.all (1).all);
7509 end if;
7511 for J in 2 .. Program_Args.all'Last loop
7512 Add_Switch
7513 (Program_Args.all (J).all, Binder, And_Save => And_Save);
7514 end loop;
7515 end;
7517 elsif Argv'Length > 11
7518 and then Argv (1 .. 11) = "--GNATLINK="
7519 then
7520 declare
7521 Program_Args : constant Argument_List_Access :=
7522 Argument_String_To_List
7523 (Argv (12 .. Argv'Last));
7524 begin
7525 if And_Save then
7526 Saved_Gnatlink := new String'(Program_Args.all (1).all);
7527 else
7528 Gnatlink := new String'(Program_Args.all (1).all);
7529 end if;
7531 for J in 2 .. Program_Args.all'Last loop
7532 Add_Switch (Program_Args.all (J).all, Linker);
7533 end loop;
7534 end;
7536 elsif Argv'Length >= 5 and then
7537 Argv (1 .. 5) = "--RTS"
7538 then
7539 Add_Switch (Argv, Compiler, And_Save => And_Save);
7540 Add_Switch (Argv, Binder, And_Save => And_Save);
7542 if Argv'Length <= 6 or else Argv (6) /= '=' then
7543 Make_Failed ("missing path for --RTS");
7545 else
7546 -- Check that this is the first time we see this switch or
7547 -- if it is not the first time, the same path is specified.
7549 if RTS_Specified = null then
7550 RTS_Specified := new String'(Argv (7 .. Argv'Last));
7552 elsif RTS_Specified.all /= Argv (7 .. Argv'Last) then
7553 Make_Failed ("--RTS cannot be specified multiple times");
7554 end if;
7556 -- Valid --RTS switch
7558 No_Stdinc := True;
7559 No_Stdlib := True;
7560 RTS_Switch := True;
7562 declare
7563 Src_Path_Name : constant String_Ptr :=
7564 Get_RTS_Search_Dir
7565 (Argv (7 .. Argv'Last), Include);
7567 Lib_Path_Name : constant String_Ptr :=
7568 Get_RTS_Search_Dir
7569 (Argv (7 .. Argv'Last), Objects);
7571 begin
7572 if Src_Path_Name /= null
7573 and then Lib_Path_Name /= null
7574 then
7575 -- Set RTS_*_Path_Name variables, so that correct direct-
7576 -- ories will be set when Osint.Add_Default_Search_Dirs
7577 -- is called later.
7579 RTS_Src_Path_Name := Src_Path_Name;
7580 RTS_Lib_Path_Name := Lib_Path_Name;
7582 elsif Src_Path_Name = null
7583 and then Lib_Path_Name = null
7584 then
7585 Make_Failed ("RTS path not valid: missing "
7586 & "adainclude and adalib directories");
7588 elsif Src_Path_Name = null then
7589 Make_Failed ("RTS path not valid: missing adainclude "
7590 & "directory");
7592 elsif Lib_Path_Name = null then
7593 Make_Failed ("RTS path not valid: missing adalib "
7594 & "directory");
7595 end if;
7596 end;
7597 end if;
7599 elsif Argv'Length > Source_Info_Option'Length and then
7600 Argv (1 .. Source_Info_Option'Length) = Source_Info_Option
7601 then
7602 Project_Tree.Source_Info_File_Name :=
7603 new String'(Argv (Source_Info_Option'Length + 1 .. Argv'Last));
7605 elsif Argv'Length >= 8 and then
7606 Argv (1 .. 8) = "--param="
7607 then
7608 Add_Switch (Argv, Compiler, And_Save => And_Save);
7609 Add_Switch (Argv, Linker, And_Save => And_Save);
7611 elsif Argv = Create_Map_File_Switch then
7612 Map_File := new String'("");
7614 elsif Argv'Length > Create_Map_File_Switch'Length + 1
7615 and then
7616 Argv (1 .. Create_Map_File_Switch'Length) = Create_Map_File_Switch
7617 and then
7618 Argv (Create_Map_File_Switch'Length + 1) = '='
7619 then
7620 Map_File :=
7621 new String'
7622 (Argv (Create_Map_File_Switch'Length + 2 .. Argv'Last));
7624 else
7625 Scan_Make_Switches (Env, Argv, Success);
7626 end if;
7628 -- If we have seen a regular switch process it
7630 elsif Argv (1) = '-' then
7631 if Argv'Length = 1 then
7632 Make_Failed ("switch character cannot be followed by a blank");
7634 -- Incorrect switches that should start with "--"
7636 elsif (Argv'Length > 5 and then Argv (1 .. 5) = "-RTS=")
7637 or else (Argv'Length > 5 and then Argv (1 .. 5) = "-GCC=")
7638 or else (Argv'Length > 8 and then Argv (1 .. 7) = "-param=")
7639 or else (Argv'Length > 10 and then Argv (1 .. 10) = "-GNATLINK=")
7640 or else (Argv'Length > 10 and then Argv (1 .. 10) = "-GNATBIND=")
7641 then
7642 Make_Failed ("option " & Argv & " should start with '--'");
7644 -- -I-
7646 elsif Argv (2 .. Argv'Last) = "I-" then
7647 Look_In_Primary_Dir := False;
7649 -- Forbid -?- or -??- where ? is any character
7651 elsif (Argv'Length = 3 and then Argv (3) = '-')
7652 or else (Argv'Length = 4 and then Argv (4) = '-')
7653 then
7654 Make_Failed
7655 ("trailing ""-"" at the end of " & Argv & " forbidden.");
7657 -- -Idir
7659 elsif Argv (2) = 'I' then
7660 Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7661 Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7662 Add_Switch (Argv, Compiler, And_Save => And_Save);
7663 Add_Switch (Argv, Binder, And_Save => And_Save);
7665 -- -aIdir (to gcc this is like a -I switch)
7667 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aI" then
7668 Add_Source_Search_Dir (Argv (4 .. Argv'Last), And_Save);
7669 Add_Switch
7670 ("-I" & Argv (4 .. Argv'Last), Compiler, And_Save => And_Save);
7671 Add_Switch (Argv, Binder, And_Save => And_Save);
7673 -- -aOdir
7675 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aO" then
7676 Add_Library_Search_Dir (Argv (4 .. Argv'Last), And_Save);
7677 Add_Switch (Argv, Binder, And_Save => And_Save);
7679 -- -aLdir (to gnatbind this is like a -aO switch)
7681 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aL" then
7682 Mark_Directory (Argv (4 .. Argv'Last), Ada_Lib_Dir, And_Save);
7683 Add_Library_Search_Dir (Argv (4 .. Argv'Last), And_Save);
7684 Add_Switch
7685 ("-aO" & Argv (4 .. Argv'Last), Binder, And_Save => And_Save);
7687 -- -aamp_target=...
7689 elsif Argv'Length >= 13 and then Argv (2 .. 13) = "aamp_target=" then
7690 Add_Switch (Argv, Compiler, And_Save => And_Save);
7692 -- Set the aamp_target environment variable so that the binder and
7693 -- linker will use the proper target library. This is consistent
7694 -- with how things work when -aamp_target is passed on the command
7695 -- line to gnaampmake.
7697 Setenv ("aamp_target", Argv (14 .. Argv'Last));
7699 -- -Adir (to gnatbind this is like a -aO switch, to gcc like a -I)
7701 elsif Argv (2) = 'A' then
7702 Mark_Directory (Argv (3 .. Argv'Last), Ada_Lib_Dir, And_Save);
7703 Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7704 Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7705 Add_Switch
7706 ("-I" & Argv (3 .. Argv'Last), Compiler, And_Save => And_Save);
7707 Add_Switch
7708 ("-aO" & Argv (3 .. Argv'Last), Binder, And_Save => And_Save);
7710 -- -Ldir
7712 elsif Argv (2) = 'L' then
7713 Add_Switch (Argv, Linker, And_Save => And_Save);
7715 -- For -gxxx, -pg, -mxxx, -fxxx, -Oxxx, pass the switch to both the
7716 -- compiler and the linker (except for -gnatxxx which is only for the
7717 -- compiler). Some of the -mxxx (for example -m64) and -fxxx (for
7718 -- example -ftest-coverage for gcov) need to be used when compiling
7719 -- the binder generated files, and using all these gcc switches for
7720 -- them should not be a problem. Pass -Oxxx to the linker for LTO.
7722 elsif
7723 (Argv (2) = 'g' and then (Argv'Last < 5
7724 or else Argv (2 .. 5) /= "gnat"))
7725 or else Argv (2 .. Argv'Last) = "pg"
7726 or else (Argv (2) = 'm' and then Argv'Last > 2)
7727 or else (Argv (2) = 'f' and then Argv'Last > 2)
7728 or else Argv (2) = 'O'
7729 then
7730 Add_Switch (Argv, Compiler, And_Save => And_Save);
7731 Add_Switch (Argv, Linker, And_Save => And_Save);
7733 -- The following condition has to be kept synchronized with
7734 -- the Process_Multilib one.
7736 if Argv (2) = 'm'
7737 and then Argv /= "-mieee"
7738 then
7739 N_M_Switch := N_M_Switch + 1;
7740 end if;
7742 -- -C=<mapping file>
7744 elsif Argv'Last > 2 and then Argv (2) = 'C' then
7745 if And_Save then
7746 if Argv (3) /= '=' or else Argv'Last <= 3 then
7747 Make_Failed ("illegal switch " & Argv);
7748 end if;
7750 Gnatmake_Mapping_File := new String'(Argv (4 .. Argv'Last));
7751 end if;
7753 -- -D
7755 elsif Argv'Last = 2 and then Argv (2) = 'D' then
7756 if Project_File_Name /= null then
7757 Make_Failed
7758 ("-D cannot be used in conjunction with a project file");
7760 else
7761 Scan_Make_Switches (Env, Argv, Success);
7762 end if;
7764 -- -d
7766 elsif Argv (2) = 'd' and then Argv'Last = 2 then
7767 Display_Compilation_Progress := True;
7769 -- -i
7771 elsif Argv'Last = 2 and then Argv (2) = 'i' then
7772 if Project_File_Name /= null then
7773 Make_Failed
7774 ("-i cannot be used in conjunction with a project file");
7775 else
7776 Scan_Make_Switches (Env, Argv, Success);
7777 end if;
7779 -- -j (need to save the result)
7781 elsif Argv (2) = 'j' then
7782 Scan_Make_Switches (Env, Argv, Success);
7784 if And_Save then
7785 Saved_Maximum_Processes := Maximum_Processes;
7786 end if;
7788 -- -m
7790 elsif Argv (2) = 'm' and then Argv'Last = 2 then
7791 Minimal_Recompilation := True;
7793 -- -u
7795 elsif Argv (2) = 'u' and then Argv'Last = 2 then
7796 Unique_Compile := True;
7797 Compile_Only := True;
7798 Do_Bind_Step := False;
7799 Do_Link_Step := False;
7801 -- -U
7803 elsif Argv (2) = 'U'
7804 and then Argv'Last = 2
7805 then
7806 Unique_Compile_All_Projects := True;
7807 Unique_Compile := True;
7808 Compile_Only := True;
7809 Do_Bind_Step := False;
7810 Do_Link_Step := False;
7812 -- -Pprj or -P prj (only once, and only on the command line)
7814 elsif Argv (2) = 'P' then
7815 if Project_File_Name /= null then
7816 Make_Failed ("cannot have several project files specified");
7818 elsif Object_Directory_Path /= null then
7819 Make_Failed
7820 ("-D cannot be used in conjunction with a project file");
7822 elsif In_Place_Mode then
7823 Make_Failed
7824 ("-i cannot be used in conjunction with a project file");
7826 elsif not And_Save then
7828 -- It could be a tool other than gnatmake (e.g. gnatdist)
7829 -- or a -P switch inside a project file.
7831 Fail
7832 ("either the tool is not ""project-aware"" or "
7833 & "a project file is specified inside a project file");
7835 elsif Argv'Last = 2 then
7837 -- -P is used alone: the project file name is the next option
7839 Project_File_Name_Present := True;
7841 else
7842 Project_File_Name := new String'(Argv (3 .. Argv'Last));
7843 end if;
7845 -- -vPx (verbosity of the parsing of the project files)
7847 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "vP" then
7848 if Argv'Last /= 4 or else Argv (4) not in '0' .. '2' then
7849 Make_Failed
7850 ("invalid verbosity level " & Argv (4 .. Argv'Last));
7852 elsif And_Save then
7853 case Argv (4) is
7854 when '0' =>
7855 Current_Verbosity := Prj.Default;
7856 when '1' =>
7857 Current_Verbosity := Prj.Medium;
7858 when '2' =>
7859 Current_Verbosity := Prj.High;
7860 when others =>
7861 null;
7862 end case;
7863 end if;
7865 -- -Xext=val (External assignment)
7867 elsif Argv (2) = 'X'
7868 and then Is_External_Assignment (Env, Argv)
7869 then
7870 -- Is_External_Assignment has side effects when it returns True
7872 null;
7874 -- If -gnath is present, then generate the usage information right
7875 -- now and do not pass this option on to the compiler calls.
7877 elsif Argv = "-gnath" then
7878 Usage;
7880 -- If -gnatc is specified, make sure the bind and link steps are not
7881 -- executed.
7883 elsif Argv'Length >= 6 and then Argv (2 .. 6) = "gnatc" then
7885 -- If -gnatc is specified, make sure the bind and link steps are
7886 -- not executed.
7888 Add_Switch (Argv, Compiler, And_Save => And_Save);
7889 Operating_Mode := Check_Semantics;
7890 Check_Object_Consistency := False;
7892 -- Except in CodePeer mode (set by -gnatcC), where we do want to
7893 -- call bind/link in CodePeer mode (-P switch).
7895 if Argv'Last >= 7 and then Argv (7) = 'C' then
7896 CodePeer_Mode := True;
7897 else
7898 Compile_Only := True;
7899 Do_Bind_Step := False;
7900 Do_Link_Step := False;
7901 end if;
7903 elsif Argv (2 .. Argv'Last) = "nostdlib" then
7905 -- Pass -nstdlib to gnatbind and gnatlink
7907 No_Stdlib := True;
7908 Add_Switch (Argv, Binder, And_Save => And_Save);
7909 Add_Switch (Argv, Linker, And_Save => And_Save);
7911 elsif Argv (2 .. Argv'Last) = "nostdinc" then
7913 -- Pass -nostdinc to the Compiler and to gnatbind
7915 No_Stdinc := True;
7916 Add_Switch (Argv, Compiler, And_Save => And_Save);
7917 Add_Switch (Argv, Binder, And_Save => And_Save);
7919 -- All other switches are processed by Scan_Make_Switches. If the
7920 -- call returns with Gnatmake_Switch_Found = False, then the switch
7921 -- is passed to the compiler.
7923 else
7924 Scan_Make_Switches (Env, Argv, Gnatmake_Switch_Found);
7926 if not Gnatmake_Switch_Found then
7927 Add_Switch (Argv, Compiler, And_Save => And_Save);
7928 end if;
7929 end if;
7931 -- If not a switch it must be a file name
7933 else
7934 if And_Save then
7935 Main_On_Command_Line := True;
7936 end if;
7938 Add_File (Argv);
7939 Mains.Add_Main (Argv);
7940 end if;
7941 end Scan_Make_Arg;
7943 -----------------
7944 -- Switches_Of --
7945 -----------------
7947 function Switches_Of
7948 (Source_File : File_Name_Type;
7949 Project : Project_Id;
7950 In_Package : Package_Id;
7951 Allow_ALI : Boolean) return Variable_Value
7953 Switches : Variable_Value;
7954 Is_Default : Boolean;
7956 begin
7957 Makeutl.Get_Switches
7958 (Source_File => Source_File,
7959 Source_Lang => Name_Ada,
7960 Source_Prj => Project,
7961 Pkg_Name => Project_Tree.Shared.Packages.Table (In_Package).Name,
7962 Project_Tree => Project_Tree,
7963 Value => Switches,
7964 Is_Default => Is_Default,
7965 Test_Without_Suffix => True,
7966 Check_ALI_Suffix => Allow_ALI);
7967 return Switches;
7968 end Switches_Of;
7970 -----------
7971 -- Usage --
7972 -----------
7974 procedure Usage is
7975 begin
7976 if Usage_Needed then
7977 Usage_Needed := False;
7978 Makeusg;
7979 end if;
7980 end Usage;
7982 begin
7983 -- Make sure that in case of failure, the temp files will be deleted
7985 Prj.Com.Fail := Make_Failed'Access;
7986 MLib.Fail := Make_Failed'Access;
7987 end Make;