Rebase.
[official-gcc.git] / gcc / ada / make.adb
blob07f960bddeb889b1ff8490a02b30689c63c911eb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- M A K E --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2014, 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.Command_Line; use GNAT.Command_Line;
77 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
78 with GNAT.Dynamic_HTables; use GNAT.Dynamic_HTables;
79 with GNAT.OS_Lib; use GNAT.OS_Lib;
81 package body Make is
83 use ASCII;
84 -- Make control characters visible
86 Standard_Library_Package_Body_Name : constant String := "s-stalib.adb";
87 -- Every program depends on this package, that must then be checked,
88 -- especially when -f and -a are used.
90 procedure Kill (Pid : Process_Id; Sig_Num : Integer; Close : Integer);
91 pragma Import (C, Kill, "__gnat_kill");
92 -- Called by Sigint_Intercepted to kill all spawned compilation processes
94 type Sigint_Handler is access procedure;
95 pragma Convention (C, Sigint_Handler);
97 procedure Install_Int_Handler (Handler : Sigint_Handler);
98 pragma Import (C, Install_Int_Handler, "__gnat_install_int_handler");
99 -- Called by Gnatmake to install the SIGINT handler below
101 procedure Sigint_Intercepted;
102 pragma Convention (C, Sigint_Intercepted);
103 -- Called when the program is interrupted by Ctrl-C to delete the
104 -- temporary mapping files and configuration pragmas files.
106 No_Mapping_File : constant Natural := 0;
108 type Compilation_Data is record
109 Pid : Process_Id;
110 Full_Source_File : File_Name_Type;
111 Lib_File : File_Name_Type;
112 Source_Unit : Unit_Name_Type;
113 Full_Lib_File : File_Name_Type;
114 Lib_File_Attr : aliased File_Attributes;
115 Mapping_File : Natural := No_Mapping_File;
116 Project : Project_Id := No_Project;
117 end record;
118 -- Data recorded for each compilation process spawned
120 No_Compilation_Data : constant Compilation_Data :=
121 (Invalid_Pid, No_File, No_File, No_Unit_Name, No_File, Unknown_Attributes,
122 No_Mapping_File, No_Project);
124 type Comp_Data_Arr is array (Positive range <>) of Compilation_Data;
125 type Comp_Data_Ptr is access Comp_Data_Arr;
126 Running_Compile : Comp_Data_Ptr;
127 -- Used to save information about outstanding compilations
129 Outstanding_Compiles : Natural := 0;
130 -- Current number of outstanding compiles
132 -------------------------
133 -- Note on terminology --
134 -------------------------
136 -- In this program, we use the phrase "termination" of a file name to refer
137 -- to the suffix that appears after the unit name portion. Very often this
138 -- is simply the extension, but in some cases, the sequence may be more
139 -- complex, for example in main.1.ada, the termination in this name is
140 -- ".1.ada" and in main_.ada the termination is "_.ada".
142 procedure Insert_Project_Sources
143 (The_Project : Project_Id;
144 All_Projects : Boolean;
145 Into_Q : Boolean);
146 -- If Into_Q is True, insert all sources of the project file(s) that are
147 -- not already marked into the Q. If Into_Q is False, call Osint.Add_File
148 -- for the first source, then insert all other sources that are not already
149 -- marked into the Q. If All_Projects is True, all sources of all projects
150 -- are concerned; otherwise, only sources of The_Project are concerned,
151 -- including, if The_Project is an extending project, sources inherited
152 -- from projects being extended.
154 Unique_Compile : Boolean := False;
155 -- Set to True if -u or -U or a project file with no main is used
157 Unique_Compile_All_Projects : Boolean := False;
158 -- Set to True if -U is used
160 Must_Compile : Boolean := False;
161 -- True if gnatmake is invoked with -f -u and one or several mains on the
162 -- command line.
164 Project_Tree : constant Project_Tree_Ref :=
165 new Project_Tree_Data (Is_Root_Tree => True);
166 -- The project tree
168 Main_On_Command_Line : Boolean := False;
169 -- True if gnatmake is invoked with one or several mains on the command
170 -- line.
172 RTS_Specified : String_Access := null;
173 -- Used to detect multiple --RTS= switches
175 N_M_Switch : Natural := 0;
176 -- Used to count -mxxx switches that can affect multilib
178 -- The 3 following packages are used to store gcc, gnatbind and gnatlink
179 -- switches found in the project files.
181 package Gcc_Switches is new Table.Table (
182 Table_Component_Type => String_Access,
183 Table_Index_Type => Integer,
184 Table_Low_Bound => 1,
185 Table_Initial => 20,
186 Table_Increment => 100,
187 Table_Name => "Make.Gcc_Switches");
189 package Binder_Switches is new Table.Table (
190 Table_Component_Type => String_Access,
191 Table_Index_Type => Integer,
192 Table_Low_Bound => 1,
193 Table_Initial => 20,
194 Table_Increment => 100,
195 Table_Name => "Make.Binder_Switches");
197 package Linker_Switches is new Table.Table (
198 Table_Component_Type => String_Access,
199 Table_Index_Type => Integer,
200 Table_Low_Bound => 1,
201 Table_Initial => 20,
202 Table_Increment => 100,
203 Table_Name => "Make.Linker_Switches");
205 -- The following instantiations and variables are necessary to save what
206 -- is found on the command line, in case there is a project file specified.
208 package Saved_Gcc_Switches is new Table.Table (
209 Table_Component_Type => String_Access,
210 Table_Index_Type => Integer,
211 Table_Low_Bound => 1,
212 Table_Initial => 20,
213 Table_Increment => 100,
214 Table_Name => "Make.Saved_Gcc_Switches");
216 package Saved_Binder_Switches is new Table.Table (
217 Table_Component_Type => String_Access,
218 Table_Index_Type => Integer,
219 Table_Low_Bound => 1,
220 Table_Initial => 20,
221 Table_Increment => 100,
222 Table_Name => "Make.Saved_Binder_Switches");
224 package Saved_Linker_Switches is new Table.Table
225 (Table_Component_Type => String_Access,
226 Table_Index_Type => Integer,
227 Table_Low_Bound => 1,
228 Table_Initial => 20,
229 Table_Increment => 100,
230 Table_Name => "Make.Saved_Linker_Switches");
232 package Switches_To_Check is new Table.Table (
233 Table_Component_Type => String_Access,
234 Table_Index_Type => Integer,
235 Table_Low_Bound => 1,
236 Table_Initial => 20,
237 Table_Increment => 100,
238 Table_Name => "Make.Switches_To_Check");
240 package Library_Paths is new Table.Table (
241 Table_Component_Type => String_Access,
242 Table_Index_Type => Integer,
243 Table_Low_Bound => 1,
244 Table_Initial => 20,
245 Table_Increment => 100,
246 Table_Name => "Make.Library_Paths");
248 package Failed_Links is new Table.Table (
249 Table_Component_Type => File_Name_Type,
250 Table_Index_Type => Integer,
251 Table_Low_Bound => 1,
252 Table_Initial => 10,
253 Table_Increment => 100,
254 Table_Name => "Make.Failed_Links");
256 package Successful_Links is new Table.Table (
257 Table_Component_Type => File_Name_Type,
258 Table_Index_Type => Integer,
259 Table_Low_Bound => 1,
260 Table_Initial => 10,
261 Table_Increment => 100,
262 Table_Name => "Make.Successful_Links");
264 package Library_Projs is new Table.Table (
265 Table_Component_Type => Project_Id,
266 Table_Index_Type => Integer,
267 Table_Low_Bound => 1,
268 Table_Initial => 10,
269 Table_Increment => 100,
270 Table_Name => "Make.Library_Projs");
272 -- Two variables to keep the last binder and linker switch index in tables
273 -- Binder_Switches and Linker_Switches, before adding switches from the
274 -- project file (if any) and switches from the command line (if any).
276 Last_Binder_Switch : Integer := 0;
277 Last_Linker_Switch : Integer := 0;
279 Normalized_Switches : Argument_List_Access := new Argument_List (1 .. 10);
280 Last_Norm_Switch : Natural := 0;
282 Saved_Maximum_Processes : Natural := 0;
284 Gnatmake_Switch_Found : Boolean;
285 -- Set by Scan_Make_Arg. True when the switch is a gnatmake switch.
286 -- Tested by Add_Switches when switches in package Builder must all be
287 -- gnatmake switches.
289 Switch_May_Be_Passed_To_The_Compiler : Boolean;
290 -- Set by Add_Switches and Switches_Of. True when unrecognized switches
291 -- are passed to the Ada compiler.
293 type Arg_List_Ref is access Argument_List;
294 The_Saved_Gcc_Switches : Arg_List_Ref;
296 Project_File_Name : String_Access := null;
297 -- The path name of the main project file, if any
299 Project_File_Name_Present : Boolean := False;
300 -- True when -P is used with a space between -P and the project file name
302 Current_Verbosity : Prj.Verbosity := Prj.Default;
303 -- Verbosity to parse the project files
305 Main_Project : Prj.Project_Id := No_Project;
306 -- The project id of the main project file, if any
308 Project_Of_Current_Object_Directory : Project_Id := No_Project;
309 -- The object directory of the project for the last compilation. Avoid
310 -- calling Change_Dir if the current working directory is already this
311 -- directory.
313 Map_File : String_Access := null;
314 -- Value of switch --create-map-file
316 -- Packages of project files where unknown attributes are errors
318 Naming_String : aliased String := "naming";
319 Builder_String : aliased String := "builder";
320 Compiler_String : aliased String := "compiler";
321 Binder_String : aliased String := "binder";
322 Linker_String : aliased String := "linker";
324 Gnatmake_Packages : aliased String_List :=
325 (Naming_String 'Access,
326 Builder_String 'Access,
327 Compiler_String 'Access,
328 Binder_String 'Access,
329 Linker_String 'Access);
331 Packages_To_Check_By_Gnatmake : constant String_List_Access :=
332 Gnatmake_Packages'Access;
334 procedure Add_Library_Search_Dir
335 (Path : String;
336 On_Command_Line : Boolean);
337 -- Call Add_Lib_Search_Dir with an absolute directory path. If Path is
338 -- relative path, when On_Command_Line is True, it is relative to the
339 -- current working directory. When On_Command_Line is False, it is relative
340 -- to the project directory of the main project.
342 procedure Add_Source_Search_Dir
343 (Path : String;
344 On_Command_Line : Boolean);
345 -- Call Add_Src_Search_Dir with an absolute directory path. If Path is a
346 -- relative path, when On_Command_Line is True, it is relative to the
347 -- current working directory. When On_Command_Line is False, it is relative
348 -- to the project directory of the main project.
350 procedure Add_Source_Dir (N : String);
351 -- Call Add_Src_Search_Dir (output one line when in verbose mode)
353 procedure Add_Source_Directories is
354 new Prj.Env.For_All_Source_Dirs (Action => Add_Source_Dir);
356 procedure Add_Object_Dir (N : String);
357 -- Call Add_Lib_Search_Dir (output one line when in verbose mode)
359 procedure Add_Object_Directories is
360 new Prj.Env.For_All_Object_Dirs (Action => Add_Object_Dir);
362 procedure Change_To_Object_Directory (Project : Project_Id);
363 -- Change to the object directory of project Project, if this is not
364 -- already the current working directory.
366 type Bad_Compilation_Info is record
367 File : File_Name_Type;
368 Unit : Unit_Name_Type;
369 Found : Boolean;
370 end record;
371 -- File is the name of the file for which a compilation failed. Unit is for
372 -- gnatdist use in order to easily get the unit name of a file when its
373 -- name is krunched or declared in gnat.adc. Found is False if the
374 -- compilation failed because the file could not be found.
376 package Bad_Compilation is new Table.Table (
377 Table_Component_Type => Bad_Compilation_Info,
378 Table_Index_Type => Natural,
379 Table_Low_Bound => 1,
380 Table_Initial => 20,
381 Table_Increment => 100,
382 Table_Name => "Make.Bad_Compilation");
383 -- Full name of all the source files for which compilation fails
385 Do_Compile_Step : Boolean := True;
386 Do_Bind_Step : Boolean := True;
387 Do_Link_Step : Boolean := True;
388 -- Flags to indicate what step should be executed. Can be set to False
389 -- with the switches -c, -b and -l. These flags are reset to True for
390 -- each invocation of procedure Gnatmake.
392 Shared_String : aliased String := "-shared";
393 Force_Elab_Flags_String : aliased String := "-F";
394 CodePeer_Mode_String : aliased String := "-P";
396 No_Shared_Switch : aliased Argument_List := (1 .. 0 => null);
397 Shared_Switch : aliased Argument_List := (1 => Shared_String'Access);
398 Bind_Shared : Argument_List_Access := No_Shared_Switch'Access;
399 -- Switch to added in front of gnatbind switches. By default no switch is
400 -- added. Switch "-shared" is added if there is a non-static Library
401 -- Project File.
403 Shared_Libgcc : aliased String := "-shared-libgcc";
405 No_Shared_Libgcc_Switch : aliased Argument_List := (1 .. 0 => null);
406 Shared_Libgcc_Switch : aliased Argument_List :=
407 (1 => Shared_Libgcc'Access);
408 Link_With_Shared_Libgcc : Argument_List_Access :=
409 No_Shared_Libgcc_Switch'Access;
411 procedure Make_Failed (S : String);
412 -- Delete all temp files created by Gnatmake and call Osint.Fail, with the
413 -- parameter S (see osint.ads). This is called from the Prj hierarchy and
414 -- the MLib hierarchy. This subprogram also prints current error messages
415 -- (i.e. finalizes Errutil).
417 --------------------------
418 -- Obsolete Executables --
419 --------------------------
421 Executable_Obsolete : Boolean := False;
422 -- Executable_Obsolete is initially set to False for each executable,
423 -- and is set to True whenever one of the source of the executable is
424 -- compiled, or has already been compiled for another executable.
426 Max_Header : constant := 200;
427 -- This needs a proper comment, it used to say "arbitrary" that's not an
428 -- adequate comment ???
430 type Header_Num is range 1 .. Max_Header;
431 -- Header_Num for the hash table Obsoleted below
433 function Hash (F : File_Name_Type) return Header_Num;
434 -- Hash function for the hash table Obsoleted below
436 package Obsoleted is new System.HTable.Simple_HTable
437 (Header_Num => Header_Num,
438 Element => Boolean,
439 No_Element => False,
440 Key => File_Name_Type,
441 Hash => Hash,
442 Equal => "=");
443 -- A hash table to keep all files that have been compiled, to detect
444 -- if an executable is up to date or not.
446 procedure Enter_Into_Obsoleted (F : File_Name_Type);
447 -- Enter a file name, without directory information, into the hash table
448 -- Obsoleted.
450 function Is_In_Obsoleted (F : File_Name_Type) return Boolean;
451 -- Check if a file name, without directory information, has already been
452 -- entered into the hash table Obsoleted.
454 type Dependency is record
455 This : File_Name_Type;
456 Depends_On : File_Name_Type;
457 end record;
458 -- Components of table Dependencies below
460 package Dependencies is new Table.Table (
461 Table_Component_Type => Dependency,
462 Table_Index_Type => Integer,
463 Table_Low_Bound => 1,
464 Table_Initial => 20,
465 Table_Increment => 100,
466 Table_Name => "Make.Dependencies");
467 -- A table to keep dependencies, to be able to decide if an executable
468 -- is obsolete. More explanation needed ???
470 ----------------------------
471 -- Arguments and Switches --
472 ----------------------------
474 Arguments : Argument_List_Access;
475 -- Used to gather the arguments for invocation of the compiler
477 Last_Argument : Natural := 0;
478 -- Last index of arguments in Arguments above
480 Arguments_Project : Project_Id;
481 -- Project id, if any, of the source to be compiled
483 Arguments_Path_Name : Path_Name_Type;
484 -- Full path of the source to be compiled, when Arguments_Project is not
485 -- No_Project.
487 Dummy_Switch : constant String_Access := new String'("- ");
488 -- Used to initialized Prev_Switch in procedure Check
490 procedure Add_Arguments (Args : Argument_List);
491 -- Add arguments to global variable Arguments, increasing its size
492 -- if necessary and adjusting Last_Argument.
494 function Configuration_Pragmas_Switch
495 (For_Project : Project_Id) return Argument_List;
496 -- Return an argument list of one element, if there is a configuration
497 -- pragmas file to be specified for For_Project,
498 -- otherwise return an empty argument list.
500 -------------------
501 -- Misc Routines --
502 -------------------
504 procedure List_Depend;
505 -- Prints to standard output the list of object dependencies. This list
506 -- can be used directly in a Makefile. A call to Compile_Sources must
507 -- precede the call to List_Depend. Also because this routine uses the
508 -- ALI files that were originally loaded and scanned by Compile_Sources,
509 -- no additional ALI files should be scanned between the two calls (i.e.
510 -- between the call to Compile_Sources and List_Depend.)
512 procedure List_Bad_Compilations;
513 -- Prints out the list of all files for which the compilation failed
515 Usage_Needed : Boolean := True;
516 -- Flag used to make sure Makeusg is call at most once
518 procedure Usage;
519 -- Call Makeusg, if Usage_Needed is True.
520 -- Set Usage_Needed to False.
522 procedure Debug_Msg (S : String; N : Name_Id);
523 procedure Debug_Msg (S : String; N : File_Name_Type);
524 procedure Debug_Msg (S : String; N : Unit_Name_Type);
525 -- If Debug.Debug_Flag_W is set outputs string S followed by name N
527 procedure Recursive_Compute_Depth (Project : Project_Id);
528 -- Compute depth of Project and of the projects it depends on
530 -----------------------
531 -- Gnatmake Routines --
532 -----------------------
534 subtype Lib_Mark_Type is Byte;
535 -- Used in Mark_Directory
537 Ada_Lib_Dir : constant Lib_Mark_Type := 1;
538 -- Used to mark a directory as a GNAT lib dir
540 -- Note that the notion of GNAT lib dir is no longer used. The code related
541 -- to it has not been removed to give an idea on how to use the directory
542 -- prefix marking mechanism.
544 -- An Ada library directory is a directory containing ali and object files
545 -- but no source files for the bodies (the specs can be in the same or some
546 -- other directory). These directories are specified in the Gnatmake
547 -- command line with the switch "-Adir" (to specify the spec location -Idir
548 -- cab be used). Gnatmake skips the missing sources whose ali are in Ada
549 -- library directories. For an explanation of why Gnatmake behaves that
550 -- way, see the spec of Make.Compile_Sources. The directory lookup penalty
551 -- is incurred every single time this routine is called.
553 procedure Check_Steps;
554 -- Check what steps (Compile, Bind, Link) must be executed.
555 -- Set the step flags accordingly.
557 function In_Ada_Lib_Dir (File : File_Name_Type) return Boolean;
558 -- Get directory prefix of this file and get lib mark stored in name
559 -- table for this directory. Then check if an Ada lib mark has been set.
561 procedure Mark_Directory
562 (Dir : String;
563 Mark : Lib_Mark_Type;
564 On_Command_Line : Boolean);
565 -- Store the absolute path from Dir in name table and set lib mark as name
566 -- info to identify Ada libraries.
568 -- If Dir is a relative path, when On_Command_Line is True, it is relative
569 -- to the current working directory; when On_Command_Line is False, it is
570 -- relative to the project directory of the main project.
572 Output_Is_Object : Boolean := True;
573 -- Set to False when using a switch -S for the compiler
575 procedure Check_For_S_Switch;
576 -- Set Output_Is_Object to False when the -S switch is used for the
577 -- compiler.
579 function Switches_Of
580 (Source_File : File_Name_Type;
581 Project : Project_Id;
582 In_Package : Package_Id;
583 Allow_ALI : Boolean) return Variable_Value;
584 -- Return the switches for the source file in the specified package of a
585 -- project file. If the Source_File ends with a standard GNAT extension
586 -- (".ads" or ".adb"), try first the full name, then the name without the
587 -- extension, then, if Allow_ALI is True, the name with the extension
588 -- ".ali". If there is no switches for either names, try first Switches
589 -- (others) then the default switches for Ada. If all failed, return
590 -- No_Variable_Value.
592 function Is_In_Object_Directory
593 (Source_File : File_Name_Type;
594 Full_Lib_File : File_Name_Type) return Boolean;
595 -- Check if, when using a project file, the ALI file is in the project
596 -- directory of the ultimate extending project. If it is not, we ignore
597 -- the fact that this ALI file is read-only.
599 procedure Process_Multilib (Env : in out Prj.Tree.Environment);
600 -- Add appropriate --RTS argument to handle multilib
602 procedure Resolve_Relative_Names_In_Switches (Current_Work_Dir : String);
603 -- Resolve all relative paths found in the linker and binder switches,
604 -- when using project files.
606 procedure Queue_Library_Project_Sources;
607 -- For all library project, if the library file does not exist, put all the
608 -- project sources in the queue, and flag the project so that the library
609 -- is generated.
611 procedure Compute_Switches_For_Main
612 (Main_Source_File : in out File_Name_Type;
613 Root_Environment : in out Prj.Tree.Environment;
614 Compute_Builder : Boolean;
615 Current_Work_Dir : String);
616 -- Find compiler, binder and linker switches to use for the given main
618 procedure Compute_Executable
619 (Main_Source_File : File_Name_Type;
620 Executable : out File_Name_Type;
621 Non_Std_Executable : out Boolean);
622 -- Parse the linker switches and project file to compute the name of the
623 -- executable to generate.
624 -- ??? What is the meaning of Non_Std_Executable
626 procedure Compilation_Phase
627 (Main_Source_File : File_Name_Type;
628 Current_Main_Index : Int := 0;
629 Total_Compilation_Failures : in out Natural;
630 Stand_Alone_Libraries : in out Boolean;
631 Executable : File_Name_Type := No_File;
632 Is_Last_Main : Boolean;
633 Stop_Compile : out Boolean);
634 -- Build all source files for a given main file
636 -- Current_Main_Index, if not zero, is the index of the current main unit
637 -- in its source file.
639 -- Stand_Alone_Libraries is set to True when there are Stand-Alone
640 -- Libraries, so that gnatbind is invoked with the -F switch to force
641 -- checking of elaboration flags.
643 -- Stop_Compile is set to true if we should not try to compile any more
644 -- of the main units
646 procedure Binding_Phase
647 (Stand_Alone_Libraries : Boolean := False;
648 Main_ALI_File : File_Name_Type);
649 -- Stand_Alone_Libraries should be set to True when there are Stand-Alone
650 -- Libraries, so that gnatbind is invoked with the -F switch to force
651 -- checking of elaboration flags.
653 procedure Library_Phase
654 (Stand_Alone_Libraries : in out Boolean;
655 Library_Rebuilt : in out Boolean);
656 -- Build libraries.
657 -- Stand_Alone_Libraries is set to True when there are Stand-Alone
658 -- Libraries, so that gnatbind is invoked with the -F switch to force
659 -- checking of elaboration flags.
661 procedure Linking_Phase
662 (Non_Std_Executable : Boolean := False;
663 Executable : File_Name_Type := No_File;
664 Main_ALI_File : File_Name_Type);
665 -- Perform the link of a single executable. The ali file corresponds
666 -- to Main_ALI_File. Executable is the file name of an executable.
667 -- Non_Std_Executable is set to True when there is a possibility that
668 -- the linker will not choose the correct executable file name.
670 ----------------------------------------------------
671 -- Compiler, Binder & Linker Data and Subprograms --
672 ----------------------------------------------------
674 Gcc : String_Access := Program_Name ("gcc", "gnatmake");
675 Original_Gcc : constant String_Access := Gcc;
676 -- Original_Gcc is used to check if Gcc has been modified by a switch
677 -- --GCC=, so that for VM platforms, it is not modified again, as it can
678 -- result in incorrect error messages if the compiler cannot be found.
680 Gnatbind : String_Access := Program_Name ("gnatbind", "gnatmake");
681 Gnatlink : String_Access := Program_Name ("gnatlink", "gnatmake");
682 -- Default compiler, binder, linker programs
684 Globalizer : constant String := "codepeer_globalizer";
685 -- CodePeer globalizer executable name
687 Saved_Gcc : String_Access := null;
688 Saved_Gnatbind : String_Access := null;
689 Saved_Gnatlink : String_Access := null;
690 -- Given by the command line. Will be used, if non null
692 Gcc_Path : String_Access :=
693 GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
694 Gnatbind_Path : String_Access :=
695 GNAT.OS_Lib.Locate_Exec_On_Path (Gnatbind.all);
696 Gnatlink_Path : String_Access :=
697 GNAT.OS_Lib.Locate_Exec_On_Path (Gnatlink.all);
698 -- Path for compiler, binder, linker programs, defaulted now for gnatdist.
699 -- Changed later if overridden on command line.
701 Globalizer_Path : constant String_Access :=
702 GNAT.OS_Lib.Locate_Exec_On_Path (Globalizer);
703 -- Path for CodePeer globalizer
705 Comp_Flag : constant String_Access := new String'("-c");
706 Output_Flag : constant String_Access := new String'("-o");
707 Ada_Flag_1 : constant String_Access := new String'("-x");
708 Ada_Flag_2 : constant String_Access := new String'("ada");
709 AdaSCIL_Flag : constant String_Access := new String'("adascil");
710 No_gnat_adc : constant String_Access := new String'("-gnatA");
711 GNAT_Flag : constant String_Access := new String'("-gnatpg");
712 Do_Not_Check_Flag : constant String_Access := new String'("-x");
714 Object_Suffix : constant String := Get_Target_Object_Suffix.all;
716 Syntax_Only : Boolean := False;
717 -- Set to True when compiling with -gnats
719 Display_Executed_Programs : Boolean := True;
720 -- Set to True if name of commands should be output on stderr (or on stdout
721 -- if the Commands_To_Stdout flag was set by use of the -eS switch).
723 Output_File_Name_Seen : Boolean := False;
724 -- Set to True after having scanned the file_name for
725 -- switch "-o file_name"
727 Object_Directory_Seen : Boolean := False;
728 -- Set to True after having scanned the object directory for
729 -- switch "-D obj_dir".
731 Object_Directory_Path : String_Access := null;
732 -- The path name of the object directory, set with switch -D
734 type Make_Program_Type is (None, Compiler, Binder, Linker);
736 Program_Args : Make_Program_Type := None;
737 -- Used to indicate if we are scanning gnatmake, gcc, gnatbind, or gnatbind
738 -- options within the gnatmake command line. Used in Scan_Make_Arg only,
739 -- but must be global since value preserved from one call to another.
741 Temporary_Config_File : Boolean := False;
742 -- Set to True when there is a temporary config file used for a project
743 -- file, to avoid displaying the -gnatec switch for a temporary file.
745 procedure Add_Switches
746 (The_Package : Package_Id;
747 File_Name : String;
748 Program : Make_Program_Type;
749 Unknown_Switches_To_The_Compiler : Boolean := True;
750 Env : in out Prj.Tree.Environment);
751 procedure Add_Switch
752 (S : String_Access;
753 Program : Make_Program_Type;
754 Append_Switch : Boolean := True;
755 And_Save : Boolean := True);
756 procedure Add_Switch
757 (S : String;
758 Program : Make_Program_Type;
759 Append_Switch : Boolean := True;
760 And_Save : Boolean := True);
761 -- Make invokes one of three programs (the compiler, the binder or the
762 -- linker). For the sake of convenience, some program specific switches
763 -- can be passed directly on the gnatmake command line. This procedure
764 -- records these switches so that gnatmake can pass them to the right
765 -- program. S is the switch to be added at the end of the command line
766 -- for Program if Append_Switch is True. If Append_Switch is False S is
767 -- added at the beginning of the command line.
769 procedure Check
770 (Source_File : File_Name_Type;
771 Is_Main_Source : Boolean;
772 The_Args : Argument_List;
773 Lib_File : File_Name_Type;
774 Full_Lib_File : File_Name_Type;
775 Lib_File_Attr : access File_Attributes;
776 Read_Only : Boolean;
777 ALI : out ALI_Id;
778 O_File : out File_Name_Type;
779 O_Stamp : out Time_Stamp_Type);
780 -- Determines whether the library file Lib_File is up-to-date or not. The
781 -- full name (with path information) of the object file corresponding to
782 -- Lib_File is returned in O_File. Its time stamp is saved in O_Stamp.
783 -- ALI is the ALI_Id corresponding to Lib_File. If Lib_File in not
784 -- up-to-date, then the corresponding source file needs to be recompiled.
785 -- In this case ALI = No_ALI_Id.
786 -- Full_Lib_File must be the result of calling Osint.Full_Lib_File_Name on
787 -- Lib_File. Precomputing it saves system calls. Lib_File_Attr is the
788 -- initialized attributes of that file, which is also used to save on
789 -- system calls (it can safely be initialized to Unknown_Attributes).
791 procedure Check_Linker_Options
792 (E_Stamp : Time_Stamp_Type;
793 O_File : out File_Name_Type;
794 O_Stamp : out Time_Stamp_Type);
795 -- Checks all linker options for linker files that are newer
796 -- than E_Stamp. If such objects are found, the youngest object
797 -- is returned in O_File and its stamp in O_Stamp.
799 -- If no obsolete linker files were found, the first missing
800 -- linker file is returned in O_File and O_Stamp is empty.
801 -- Otherwise O_File is No_File.
803 procedure Collect_Arguments
804 (Source_File : File_Name_Type;
805 Is_Main_Source : Boolean;
806 Args : Argument_List);
807 -- Collect all arguments for a source to be compiled, including those
808 -- that come from a project file.
810 procedure Display (Program : String; Args : Argument_List);
811 -- Displays Program followed by the arguments in Args if variable
812 -- Display_Executed_Programs is set. The lower bound of Args must be 1.
814 procedure Report_Compilation_Failed;
815 -- Delete all temporary files and fail graciously
817 -----------------
818 -- Mapping files
819 -----------------
821 type Temp_Path_Names is array (Positive range <>) of Path_Name_Type;
822 type Temp_Path_Ptr is access Temp_Path_Names;
824 type Free_File_Indexes is array (Positive range <>) of Positive;
825 type Free_Indexes_Ptr is access Free_File_Indexes;
827 type Project_Compilation_Data is record
828 Mapping_File_Names : Temp_Path_Ptr;
829 -- The name ids of the temporary mapping files used. This is indexed
830 -- on the maximum number of compilation processes we will be spawning
831 -- (-j parameter)
833 Last_Mapping_File_Names : Natural;
834 -- Index of the last mapping file created for this project
836 Free_Mapping_File_Indexes : Free_Indexes_Ptr;
837 -- Indexes in Mapping_File_Names of the mapping file names that can be
838 -- reused for subsequent compilations.
840 Last_Free_Indexes : Natural;
841 -- Number of mapping files that can be reused
842 end record;
843 -- Information necessary when compiling a project
845 type Project_Compilation_Access is access Project_Compilation_Data;
847 package Project_Compilation_Htable is new Simple_HTable
848 (Header_Num => Prj.Header_Num,
849 Element => Project_Compilation_Access,
850 No_Element => null,
851 Key => Project_Id,
852 Hash => Prj.Hash,
853 Equal => "=");
855 Project_Compilation : Project_Compilation_Htable.Instance;
857 Gnatmake_Mapping_File : String_Access := null;
858 -- The path name of a mapping file specified by switch -C=
860 procedure Init_Mapping_File
861 (Project : Project_Id;
862 Data : in out Project_Compilation_Data;
863 File_Index : in out Natural);
864 -- Create a new temporary mapping file, and fill it with the project file
865 -- mappings, when using project file(s). The out parameter File_Index is
866 -- the index to the name of the file in the array The_Mapping_File_Names.
868 -------------------------------------------------
869 -- Subprogram declarations moved from the spec --
870 -------------------------------------------------
872 procedure Bind (ALI_File : File_Name_Type; Args : Argument_List);
873 -- Binds ALI_File. Args are the arguments to pass to the binder.
874 -- Args must have a lower bound of 1.
876 procedure Display_Commands (Display : Boolean := True);
877 -- The default behavior of Make commands (Compile_Sources, Bind, Link)
878 -- is to display them on stderr. This behavior can be changed repeatedly
879 -- by invoking this procedure.
881 -- If a compilation, bind or link failed one of the following 3 exceptions
882 -- is raised. These need to be handled by the calling routines.
884 procedure Compile_Sources
885 (Main_Source : File_Name_Type;
886 Args : Argument_List;
887 First_Compiled_File : out File_Name_Type;
888 Most_Recent_Obj_File : out File_Name_Type;
889 Most_Recent_Obj_Stamp : out Time_Stamp_Type;
890 Main_Unit : out Boolean;
891 Compilation_Failures : out Natural;
892 Main_Index : Int := 0;
893 Check_Readonly_Files : Boolean := False;
894 Do_Not_Execute : Boolean := False;
895 Force_Compilations : Boolean := False;
896 Keep_Going : Boolean := False;
897 In_Place_Mode : Boolean := False;
898 Initialize_ALI_Data : Boolean := True;
899 Max_Process : Positive := 1);
900 -- Compile_Sources will recursively compile all the sources needed by
901 -- Main_Source. Before calling this routine make sure Namet has been
902 -- initialized. This routine can be called repeatedly with different
903 -- Main_Source file as long as all the source (-I flags), library
904 -- (-B flags) and ada library (-A flags) search paths between calls are
905 -- *exactly* the same. The default directory must also be the same.
907 -- Args contains the arguments to use during the compilations.
908 -- The lower bound of Args must be 1.
910 -- First_Compiled_File is set to the name of the first file that is
911 -- compiled or that needs to be compiled. This is set to No_Name if no
912 -- compilations were needed.
914 -- Most_Recent_Obj_File is set to the full name of the most recent
915 -- object file found when no compilations are needed, that is when
916 -- First_Compiled_File is set to No_Name. When First_Compiled_File
917 -- is set then Most_Recent_Obj_File is set to No_Name.
919 -- Most_Recent_Obj_Stamp is the time stamp of Most_Recent_Obj_File.
921 -- Main_Unit is set to True if Main_Source can be a main unit.
922 -- If Do_Not_Execute is False and First_Compiled_File /= No_Name
923 -- the value of Main_Unit is always False.
924 -- Is this used any more??? It is certainly not used by gnatmake???
926 -- Compilation_Failures is a count of compilation failures. This count
927 -- is used to extract compilation failure reports with Extract_Failure.
929 -- Main_Index, when not zero, is the index of the main unit in source
930 -- file Main_Source which is a multi-unit source.
931 -- Zero indicates that Main_Source is a single unit source file.
933 -- Check_Readonly_Files set it to True to compile source files
934 -- which library files are read-only. When compiling GNAT predefined
935 -- files the "-gnatg" flag is used.
937 -- Do_Not_Execute set it to True to find out the first source that
938 -- needs to be recompiled, but without recompiling it. This file is
939 -- saved in First_Compiled_File.
941 -- Force_Compilations forces all compilations no matter what but
942 -- recompiles read-only files only if Check_Readonly_Files
943 -- is set.
945 -- Keep_Going when True keep compiling even in the presence of
946 -- compilation errors.
948 -- In_Place_Mode when True save library/object files in their object
949 -- directory if they already exist; otherwise, in the source directory.
951 -- Initialize_ALI_Data set it to True when you want to initialize ALI
952 -- data-structures. This is what you should do most of the time.
953 -- (especially the first time around when you call this routine).
954 -- This parameter is set to False to preserve previously recorded
955 -- ALI file data.
957 -- Max_Process is the maximum number of processes that should be spawned
958 -- to carry out compilations.
960 -- Flags in Package Opt Affecting Compile_Sources
961 -- -----------------------------------------------
963 -- Check_Object_Consistency set it to False to omit all consistency
964 -- checks between an .ali file and its corresponding object file.
965 -- When this flag is set to true, every time an .ali is read,
966 -- package Osint checks that the corresponding object file
967 -- exists and is more recent than the .ali.
969 -- Use of Name Table Info
970 -- ----------------------
972 -- All file names manipulated by Compile_Sources are entered into the
973 -- Names table. The Byte field of a source file is used to mark it.
975 -- Calling Compile_Sources Several Times
976 -- -------------------------------------
978 -- Upon return from Compile_Sources all the ALI data structures are left
979 -- intact for further browsing. HOWEVER upon entry to this routine ALI
980 -- data structures are re-initialized if parameter Initialize_ALI_Data
981 -- above is set to true. Typically this is what you want the first time
982 -- you call Compile_Sources. You should not load an ali file, call this
983 -- routine with flag Initialize_ALI_Data set to True and then expect
984 -- that ALI information to be around after the call. Note that the first
985 -- time you call Compile_Sources you better set Initialize_ALI_Data to
986 -- True unless you have called Initialize_ALI yourself.
988 -- Compile_Sources ALGORITHM : Compile_Sources (Main_Source)
989 -- -------------------------
991 -- 1. Insert Main_Source in a Queue (Q) and mark it.
993 -- 2. Let unit.adb be the file at the head of the Q. If unit.adb is
994 -- missing but its corresponding ali file is in an Ada library directory
995 -- (see below) then, remove unit.adb from the Q and goto step 4.
996 -- Otherwise, look at the files under the D (dependency) section of
997 -- unit.ali. If unit.ali does not exist or some of the time stamps do
998 -- not match, (re)compile unit.adb.
1000 -- An Ada library directory is a directory containing Ada specs, ali
1001 -- and object files but no source files for the bodies. An Ada library
1002 -- directory is communicated to gnatmake by means of some switch so that
1003 -- gnatmake can skip the sources whole ali are in that directory.
1004 -- There are two reasons for skipping the sources in this case. Firstly,
1005 -- Ada libraries typically come without full sources but binding and
1006 -- linking against those libraries is still possible. Secondly, it would
1007 -- be very wasteful for gnatmake to systematically check the consistency
1008 -- of every external Ada library used in a program. The binder is
1009 -- already in charge of catching any potential inconsistencies.
1011 -- 3. Look into the W section of unit.ali and insert into the Q all
1012 -- unmarked source files. Mark all files newly inserted in the Q.
1013 -- Specifically, assuming that the W section looks like
1015 -- W types%s types.adb types.ali
1016 -- W unchecked_deallocation%s
1017 -- W xref_tab%s xref_tab.adb xref_tab.ali
1019 -- Then xref_tab.adb and types.adb are inserted in the Q if they are not
1020 -- already marked.
1021 -- Note that there is no file listed under W unchecked_deallocation%s
1022 -- so no generic body should ever be explicitly compiled (unless the
1023 -- Main_Source at the start was a generic body).
1025 -- 4. Repeat steps 2 and 3 above until the Q is empty
1027 -- Note that the above algorithm works because the units withed in
1028 -- subunits are transitively included in the W section (with section) of
1029 -- the main unit. Likewise the withed units in a generic body needed
1030 -- during a compilation are also transitively included in the W section
1031 -- of the originally compiled file.
1033 procedure Globalize (Success : out Boolean);
1034 -- Call the CodePeer globalizer on all the project's object directories,
1035 -- or on the current directory if no projects.
1037 procedure Initialize
1038 (Project_Node_Tree : out Project_Node_Tree_Ref;
1039 Env : out Prj.Tree.Environment);
1040 -- Performs default and package initialization. Therefore,
1041 -- Compile_Sources can be called by an external unit.
1043 procedure Link
1044 (ALI_File : File_Name_Type;
1045 Args : Argument_List;
1046 Success : out Boolean);
1047 -- Links ALI_File. Args are the arguments to pass to the linker.
1048 -- Args must have a lower bound of 1. Success indicates if the link
1049 -- succeeded or not.
1051 procedure Scan_Make_Arg
1052 (Env : in out Prj.Tree.Environment;
1053 Argv : String;
1054 And_Save : Boolean);
1055 -- Scan make arguments. Argv is a single argument to be processed.
1056 -- Project_Node_Tree will be used to initialize external references. It
1057 -- must have been initialized.
1059 -------------------
1060 -- Add_Arguments --
1061 -------------------
1063 procedure Add_Arguments (Args : Argument_List) is
1064 begin
1065 if Arguments = null then
1066 Arguments := new Argument_List (1 .. Args'Length + 10);
1068 else
1069 while Last_Argument + Args'Length > Arguments'Last loop
1070 declare
1071 New_Arguments : constant Argument_List_Access :=
1072 new Argument_List (1 .. Arguments'Last * 2);
1073 begin
1074 New_Arguments (1 .. Last_Argument) :=
1075 Arguments (1 .. Last_Argument);
1076 Arguments := New_Arguments;
1077 end;
1078 end loop;
1079 end if;
1081 Arguments (Last_Argument + 1 .. Last_Argument + Args'Length) := Args;
1082 Last_Argument := Last_Argument + Args'Length;
1083 end Add_Arguments;
1085 -- --------------------
1086 -- -- Add_Dependency --
1087 -- --------------------
1089 -- procedure Add_Dependency (S : File_Name_Type; On : File_Name_Type) is
1090 -- begin
1091 -- Dependencies.Increment_Last;
1092 -- Dependencies.Table (Dependencies.Last) := (S, On);
1093 -- end Add_Dependency;
1095 ----------------------------
1096 -- Add_Library_Search_Dir --
1097 ----------------------------
1099 procedure Add_Library_Search_Dir
1100 (Path : String;
1101 On_Command_Line : Boolean)
1103 begin
1104 if On_Command_Line then
1105 Add_Lib_Search_Dir (Normalize_Pathname (Path));
1107 else
1108 Get_Name_String (Main_Project.Directory.Display_Name);
1109 Add_Lib_Search_Dir
1110 (Normalize_Pathname (Path, Name_Buffer (1 .. Name_Len)));
1111 end if;
1112 end Add_Library_Search_Dir;
1114 --------------------
1115 -- Add_Object_Dir --
1116 --------------------
1118 procedure Add_Object_Dir (N : String) is
1119 begin
1120 Add_Lib_Search_Dir (N);
1122 if Verbose_Mode then
1123 Write_Str ("Adding object directory """);
1124 Write_Str (N);
1125 Write_Str (""".");
1126 Write_Eol;
1127 end if;
1128 end Add_Object_Dir;
1130 --------------------
1131 -- Add_Source_Dir --
1132 --------------------
1134 procedure Add_Source_Dir (N : String) is
1135 begin
1136 Add_Src_Search_Dir (N);
1138 if Verbose_Mode then
1139 Write_Str ("Adding source directory """);
1140 Write_Str (N);
1141 Write_Str (""".");
1142 Write_Eol;
1143 end if;
1144 end Add_Source_Dir;
1146 ---------------------------
1147 -- Add_Source_Search_Dir --
1148 ---------------------------
1150 procedure Add_Source_Search_Dir
1151 (Path : String;
1152 On_Command_Line : Boolean)
1154 begin
1155 if On_Command_Line then
1156 Add_Src_Search_Dir (Normalize_Pathname (Path));
1158 else
1159 Get_Name_String (Main_Project.Directory.Display_Name);
1160 Add_Src_Search_Dir
1161 (Normalize_Pathname (Path, Name_Buffer (1 .. Name_Len)));
1162 end if;
1163 end Add_Source_Search_Dir;
1165 ----------------
1166 -- Add_Switch --
1167 ----------------
1169 procedure Add_Switch
1170 (S : String_Access;
1171 Program : Make_Program_Type;
1172 Append_Switch : Boolean := True;
1173 And_Save : Boolean := True)
1175 generic
1176 with package T is new Table.Table (<>);
1177 procedure Generic_Position (New_Position : out Integer);
1178 -- Generic procedure that chooses a position for S in T at the
1179 -- beginning or the end, depending on the boolean Append_Switch.
1180 -- Calling this procedure may expand the table.
1182 ----------------------
1183 -- Generic_Position --
1184 ----------------------
1186 procedure Generic_Position (New_Position : out Integer) is
1187 begin
1188 T.Increment_Last;
1190 if Append_Switch then
1191 New_Position := Integer (T.Last);
1192 else
1193 for J in reverse T.Table_Index_Type'Succ (T.First) .. T.Last loop
1194 T.Table (J) := T.Table (T.Table_Index_Type'Pred (J));
1195 end loop;
1197 New_Position := Integer (T.First);
1198 end if;
1199 end Generic_Position;
1201 procedure Gcc_Switches_Pos is new Generic_Position (Gcc_Switches);
1202 procedure Binder_Switches_Pos is new Generic_Position (Binder_Switches);
1203 procedure Linker_Switches_Pos is new Generic_Position (Linker_Switches);
1205 procedure Saved_Gcc_Switches_Pos is new
1206 Generic_Position (Saved_Gcc_Switches);
1208 procedure Saved_Binder_Switches_Pos is new
1209 Generic_Position (Saved_Binder_Switches);
1211 procedure Saved_Linker_Switches_Pos is new
1212 Generic_Position (Saved_Linker_Switches);
1214 New_Position : Integer;
1216 -- Start of processing for Add_Switch
1218 begin
1219 if And_Save then
1220 case Program is
1221 when Compiler =>
1222 Saved_Gcc_Switches_Pos (New_Position);
1223 Saved_Gcc_Switches.Table (New_Position) := S;
1225 when Binder =>
1226 Saved_Binder_Switches_Pos (New_Position);
1227 Saved_Binder_Switches.Table (New_Position) := S;
1229 when Linker =>
1230 Saved_Linker_Switches_Pos (New_Position);
1231 Saved_Linker_Switches.Table (New_Position) := S;
1233 when None =>
1234 raise Program_Error;
1235 end case;
1237 else
1238 case Program is
1239 when Compiler =>
1240 Gcc_Switches_Pos (New_Position);
1241 Gcc_Switches.Table (New_Position) := S;
1243 when Binder =>
1244 Binder_Switches_Pos (New_Position);
1245 Binder_Switches.Table (New_Position) := S;
1247 when Linker =>
1248 Linker_Switches_Pos (New_Position);
1249 Linker_Switches.Table (New_Position) := S;
1251 when None =>
1252 raise Program_Error;
1253 end case;
1254 end if;
1255 end Add_Switch;
1257 procedure Add_Switch
1258 (S : String;
1259 Program : Make_Program_Type;
1260 Append_Switch : Boolean := True;
1261 And_Save : Boolean := True)
1263 begin
1264 Add_Switch (S => new String'(S),
1265 Program => Program,
1266 Append_Switch => Append_Switch,
1267 And_Save => And_Save);
1268 end Add_Switch;
1270 ------------------
1271 -- Add_Switches --
1272 ------------------
1274 procedure Add_Switches
1275 (The_Package : Package_Id;
1276 File_Name : String;
1277 Program : Make_Program_Type;
1278 Unknown_Switches_To_The_Compiler : Boolean := True;
1279 Env : in out Prj.Tree.Environment)
1281 Switches : Variable_Value;
1282 Switch_List : String_List_Id;
1283 Element : String_Element;
1285 begin
1286 Switch_May_Be_Passed_To_The_Compiler :=
1287 Unknown_Switches_To_The_Compiler;
1289 if File_Name'Length > 0 then
1290 Name_Len := 0;
1291 Add_Str_To_Name_Buffer (File_Name);
1292 Switches :=
1293 Switches_Of
1294 (Source_File => Name_Find,
1295 Project => Main_Project,
1296 In_Package => The_Package,
1297 Allow_ALI => Program = Binder or else Program = Linker);
1299 if Switches.Kind = List then
1300 Program_Args := Program;
1302 Switch_List := Switches.Values;
1303 while Switch_List /= Nil_String loop
1304 Element :=
1305 Project_Tree.Shared.String_Elements.Table (Switch_List);
1306 Get_Name_String (Element.Value);
1308 if Name_Len > 0 then
1309 declare
1310 Argv : constant String := Name_Buffer (1 .. Name_Len);
1311 -- We need a copy, because Name_Buffer may be modified
1313 begin
1314 if Verbose_Mode then
1315 Write_Str (" Adding ");
1316 Write_Line (Argv);
1317 end if;
1319 Scan_Make_Arg (Env, Argv, And_Save => False);
1321 if not Gnatmake_Switch_Found
1322 and then not Switch_May_Be_Passed_To_The_Compiler
1323 then
1324 Errutil.Error_Msg
1325 ('"' & Argv &
1326 """ is not a gnatmake switch. Consider moving "
1327 & "it to Global_Compilation_Switches.",
1328 Element.Location);
1329 Make_Failed ("*** illegal switch """ & Argv & """");
1330 end if;
1331 end;
1332 end if;
1334 Switch_List := Element.Next;
1335 end loop;
1336 end if;
1337 end if;
1338 end Add_Switches;
1340 ----------
1341 -- Bind --
1342 ----------
1344 procedure Bind (ALI_File : File_Name_Type; Args : Argument_List) is
1345 Bind_Args : Argument_List (1 .. Args'Last + 2);
1346 Bind_Last : Integer;
1347 Success : Boolean;
1349 begin
1350 pragma Assert (Args'First = 1);
1352 -- Optimize the simple case where the gnatbind command line looks like
1353 -- gnatbind -aO. -I- file.ali
1354 -- into
1355 -- gnatbind file.adb
1357 if Args'Length = 2
1358 and then Args (Args'First).all = "-aO" & Normalized_CWD
1359 and then Args (Args'Last).all = "-I-"
1360 and then ALI_File = Strip_Directory (ALI_File)
1361 then
1362 Bind_Last := Args'First - 1;
1364 else
1365 Bind_Last := Args'Last;
1366 Bind_Args (Args'Range) := Args;
1367 end if;
1369 -- It is completely pointless to re-check source file time stamps. This
1370 -- has been done already by gnatmake
1372 Bind_Last := Bind_Last + 1;
1373 Bind_Args (Bind_Last) := Do_Not_Check_Flag;
1375 Get_Name_String (ALI_File);
1377 Bind_Last := Bind_Last + 1;
1378 Bind_Args (Bind_Last) := new String'(Name_Buffer (1 .. Name_Len));
1380 GNAT.OS_Lib.Normalize_Arguments (Bind_Args (Args'First .. Bind_Last));
1382 Display (Gnatbind.all, Bind_Args (Args'First .. Bind_Last));
1384 if Gnatbind_Path = null then
1385 Make_Failed ("error, unable to locate " & Gnatbind.all);
1386 end if;
1388 GNAT.OS_Lib.Spawn
1389 (Gnatbind_Path.all, Bind_Args (Args'First .. Bind_Last), Success);
1391 if not Success then
1392 Make_Failed ("*** bind failed.");
1393 end if;
1394 end Bind;
1396 --------------------------------
1397 -- Change_To_Object_Directory --
1398 --------------------------------
1400 procedure Change_To_Object_Directory (Project : Project_Id) is
1401 Object_Directory : Path_Name_Type;
1403 begin
1404 pragma Assert (Project /= No_Project);
1406 -- Nothing to do if the current working directory is already the correct
1407 -- object directory.
1409 if Project_Of_Current_Object_Directory /= Project then
1410 Project_Of_Current_Object_Directory := Project;
1411 Object_Directory := Project.Object_Directory.Display_Name;
1413 -- Set the working directory to the object directory of the actual
1414 -- project.
1416 if Verbose_Mode then
1417 Write_Str ("Changing to object directory of """);
1418 Write_Name (Project.Display_Name);
1419 Write_Str (""": """);
1420 Write_Name (Object_Directory);
1421 Write_Line ("""");
1422 end if;
1424 Change_Dir (Get_Name_String (Object_Directory));
1425 end if;
1427 exception
1428 -- Fail if unable to change to the object directory
1430 when Directory_Error =>
1431 Make_Failed ("unable to change to object directory """ &
1432 Path_Or_File_Name
1433 (Project.Object_Directory.Display_Name) &
1434 """ of project " &
1435 Get_Name_String (Project.Display_Name));
1436 end Change_To_Object_Directory;
1438 -----------
1439 -- Check --
1440 -----------
1442 procedure Check
1443 (Source_File : File_Name_Type;
1444 Is_Main_Source : Boolean;
1445 The_Args : Argument_List;
1446 Lib_File : File_Name_Type;
1447 Full_Lib_File : File_Name_Type;
1448 Lib_File_Attr : access File_Attributes;
1449 Read_Only : Boolean;
1450 ALI : out ALI_Id;
1451 O_File : out File_Name_Type;
1452 O_Stamp : out Time_Stamp_Type)
1454 function First_New_Spec (A : ALI_Id) return File_Name_Type;
1455 -- Looks in the with table entries of A and returns the spec file name
1456 -- of the first withed unit (subprogram) for which no spec existed when
1457 -- A was generated but for which there exists one now, implying that A
1458 -- is now obsolete. If no such unit is found No_File is returned.
1459 -- Otherwise the spec file name of the unit is returned.
1461 -- **WARNING** in the event of Uname format modifications, one *MUST*
1462 -- make sure this function is also updated.
1464 -- Note: This function should really be in ali.adb and use Uname
1465 -- services, but this causes the whole compiler to be dragged along
1466 -- for gnatbind and gnatmake.
1468 --------------------
1469 -- First_New_Spec --
1470 --------------------
1472 function First_New_Spec (A : ALI_Id) return File_Name_Type is
1473 Spec_File_Name : File_Name_Type := No_File;
1475 function New_Spec (Uname : Unit_Name_Type) return Boolean;
1476 -- Uname is the name of the spec or body of some ada unit. This
1477 -- function returns True if the Uname is the name of a body which has
1478 -- a spec not mentioned in ALI file A. If True is returned
1479 -- Spec_File_Name above is set to the name of this spec file.
1481 --------------
1482 -- New_Spec --
1483 --------------
1485 function New_Spec (Uname : Unit_Name_Type) return Boolean is
1486 Spec_Name : Unit_Name_Type;
1487 File_Name : File_Name_Type;
1489 begin
1490 -- Test whether Uname is the name of a body unit (i.e. ends
1491 -- with %b).
1493 Get_Name_String (Uname);
1494 pragma
1495 Assert (Name_Len > 2 and then Name_Buffer (Name_Len - 1) = '%');
1497 if Name_Buffer (Name_Len) /= 'b' then
1498 return False;
1499 end if;
1501 -- Convert unit name into spec name
1503 -- ??? this code seems dubious in presence of pragma
1504 -- Source_File_Name since there is no more direct relationship
1505 -- between unit name and file name.
1507 -- ??? Further, what about alternative subunit naming
1509 Name_Buffer (Name_Len) := 's';
1510 Spec_Name := Name_Find;
1511 File_Name := Get_File_Name (Spec_Name, Subunit => False);
1513 -- Look if File_Name is mentioned in A's sdep list.
1514 -- If not look if the file exists. If it does return True.
1516 for D in
1517 ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep
1518 loop
1519 if Sdep.Table (D).Sfile = File_Name then
1520 return False;
1521 end if;
1522 end loop;
1524 if Full_Source_Name (File_Name) /= No_File then
1525 Spec_File_Name := File_Name;
1526 return True;
1527 end if;
1529 return False;
1530 end New_Spec;
1532 -- Start of processing for First_New_Spec
1534 begin
1535 U_Chk : for U in
1536 ALIs.Table (A).First_Unit .. ALIs.Table (A).Last_Unit
1537 loop
1538 exit U_Chk when Units.Table (U).Utype = Is_Body_Only
1539 and then New_Spec (Units.Table (U).Uname);
1541 for W in Units.Table (U).First_With
1543 Units.Table (U).Last_With
1544 loop
1545 exit U_Chk when
1546 Withs.Table (W).Afile /= No_File
1547 and then New_Spec (Withs.Table (W).Uname);
1548 end loop;
1549 end loop U_Chk;
1551 return Spec_File_Name;
1552 end First_New_Spec;
1554 ---------------------------------
1555 -- Data declarations for Check --
1556 ---------------------------------
1558 Full_Obj_File : File_Name_Type;
1559 -- Full name of the object file corresponding to Lib_File
1561 Lib_Stamp : Time_Stamp_Type;
1562 -- Time stamp of the current ada library file
1564 Obj_Stamp : Time_Stamp_Type;
1565 -- Time stamp of the current object file
1567 Modified_Source : File_Name_Type;
1568 -- The first source in Lib_File whose current time stamp differs from
1569 -- that stored in Lib_File.
1571 New_Spec : File_Name_Type;
1572 -- If Lib_File contains in its W (with) section a body (for a
1573 -- subprogram) for which there exists a spec, and the spec did not
1574 -- appear in the Sdep section of Lib_File, New_Spec contains the file
1575 -- name of this new spec.
1577 Source_Name : File_Name_Type;
1578 Text : Text_Buffer_Ptr;
1580 Prev_Switch : String_Access;
1581 -- Previous switch processed
1583 Arg : Arg_Id := Arg_Id'First;
1584 -- Current index in Args.Table for a given unit (init to stop warning)
1586 Switch_Found : Boolean;
1587 -- True if a given switch has been found
1589 ALI_Project : Project_Id;
1590 -- If the ALI file is in the object directory of a project, this is
1591 -- the project id.
1593 -- Start of processing for Check
1595 begin
1596 pragma Assert (Lib_File /= No_File);
1598 -- If ALI file is read-only, temporarily set Check_Object_Consistency to
1599 -- False. We don't care if the object file is not there (presumably a
1600 -- library will be used for linking.)
1602 if Read_Only then
1603 declare
1604 Saved_Check_Object_Consistency : constant Boolean :=
1605 Check_Object_Consistency;
1606 begin
1607 Check_Object_Consistency := False;
1608 Text := Read_Library_Info_From_Full (Full_Lib_File, Lib_File_Attr);
1609 Check_Object_Consistency := Saved_Check_Object_Consistency;
1610 end;
1612 else
1613 Text := Read_Library_Info_From_Full (Full_Lib_File, Lib_File_Attr);
1614 end if;
1616 Full_Obj_File := Full_Object_File_Name;
1617 Lib_Stamp := Current_Library_File_Stamp;
1618 Obj_Stamp := Current_Object_File_Stamp;
1620 if Full_Lib_File = No_File then
1621 Verbose_Msg
1622 (Lib_File,
1623 "being checked ...",
1624 Prefix => " ",
1625 Minimum_Verbosity => Opt.Medium);
1626 else
1627 Verbose_Msg
1628 (Full_Lib_File,
1629 "being checked ...",
1630 Prefix => " ",
1631 Minimum_Verbosity => Opt.Medium);
1632 end if;
1634 ALI := No_ALI_Id;
1635 O_File := Full_Obj_File;
1636 O_Stamp := Obj_Stamp;
1638 if Text = null then
1639 if Full_Lib_File = No_File then
1640 Verbose_Msg (Lib_File, "missing.");
1642 elsif Obj_Stamp (Obj_Stamp'First) = ' ' then
1643 Verbose_Msg (Full_Obj_File, "missing.");
1645 else
1646 Verbose_Msg
1647 (Full_Lib_File, "(" & String (Lib_Stamp) & ") newer than",
1648 Full_Obj_File, "(" & String (Obj_Stamp) & ")");
1649 end if;
1651 else
1652 ALI := Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
1653 Free (Text);
1655 if ALI = No_ALI_Id then
1656 Verbose_Msg (Full_Lib_File, "incorrectly formatted ALI file");
1657 return;
1659 elsif ALIs.Table (ALI).Ver (1 .. ALIs.Table (ALI).Ver_Len) /=
1660 Verbose_Library_Version
1661 then
1662 Verbose_Msg (Full_Lib_File, "compiled with old GNAT version");
1663 ALI := No_ALI_Id;
1664 return;
1665 end if;
1667 -- Don't take ALI file into account if it was generated with errors
1669 if ALIs.Table (ALI).Compile_Errors then
1670 Verbose_Msg (Full_Lib_File, "had errors, must be recompiled");
1671 ALI := No_ALI_Id;
1672 return;
1673 end if;
1675 -- Don't take ALI file into account if no object was generated
1677 if Operating_Mode /= Check_Semantics
1678 and then ALIs.Table (ALI).No_Object
1679 then
1680 Verbose_Msg (Full_Lib_File, "has no corresponding object");
1681 ALI := No_ALI_Id;
1682 return;
1683 end if;
1685 -- When compiling with -gnatc, don't take ALI file into account if
1686 -- it has not been generated for the current source, for example if
1687 -- it has been generated for the spec, but we are compiling the body.
1689 if Operating_Mode = Check_Semantics then
1690 declare
1691 File_Name : String := Get_Name_String (Source_File);
1692 OK : Boolean := False;
1694 begin
1695 -- In the ALI file, the source file names are in canonical case
1697 Canonical_Case_File_Name (File_Name);
1699 for U in ALIs.Table (ALI).First_Unit ..
1700 ALIs.Table (ALI).Last_Unit
1701 loop
1702 OK := Get_Name_String (Units.Table (U).Sfile) = File_Name;
1703 exit when OK;
1704 end loop;
1706 if not OK then
1707 Verbose_Msg
1708 (Full_Lib_File, "not generated for the same source");
1709 ALI := No_ALI_Id;
1710 return;
1711 end if;
1712 end;
1713 end if;
1715 -- Check for matching compiler switches if needed
1717 if Check_Switches then
1719 -- First, collect all the switches
1721 Collect_Arguments (Source_File, Is_Main_Source, The_Args);
1722 Prev_Switch := Dummy_Switch;
1723 Get_Name_String (ALIs.Table (ALI).Sfile);
1724 Switches_To_Check.Set_Last (0);
1726 for J in 1 .. Last_Argument loop
1728 -- Skip non switches -c, -I and -o switches
1730 if Arguments (J) (1) = '-'
1731 and then Arguments (J) (2) /= 'c'
1732 and then Arguments (J) (2) /= 'o'
1733 and then Arguments (J) (2) /= 'I'
1734 then
1735 Normalize_Compiler_Switches
1736 (Arguments (J).all,
1737 Normalized_Switches,
1738 Last_Norm_Switch);
1740 for K in 1 .. Last_Norm_Switch loop
1741 Switches_To_Check.Increment_Last;
1742 Switches_To_Check.Table (Switches_To_Check.Last) :=
1743 Normalized_Switches (K);
1744 end loop;
1745 end if;
1746 end loop;
1748 for J in 1 .. Switches_To_Check.Last loop
1750 -- Comparing switches is delicate because gcc reorders a number
1751 -- of switches, according to lang-specs.h, but gnatmake doesn't
1752 -- have sufficient knowledge to perform the same reordering.
1753 -- Instead, we ignore orders between different "first letter"
1754 -- switches, but keep orders between same switches, e.g -O -O2
1755 -- is different than -O2 -O, but -g -O is equivalent to -O -g.
1757 if Switches_To_Check.Table (J) (2) /= Prev_Switch (2) or else
1758 (Prev_Switch'Length >= 6 and then
1759 Prev_Switch (2 .. 5) = "gnat" and then
1760 Switches_To_Check.Table (J)'Length >= 6 and then
1761 Switches_To_Check.Table (J) (2 .. 5) = "gnat" and then
1762 Prev_Switch (6) /= Switches_To_Check.Table (J) (6))
1763 then
1764 Prev_Switch := Switches_To_Check.Table (J);
1765 Arg :=
1766 Units.Table (ALIs.Table (ALI).First_Unit).First_Arg;
1767 end if;
1769 Switch_Found := False;
1771 for K in Arg ..
1772 Units.Table (ALIs.Table (ALI).First_Unit).Last_Arg
1773 loop
1775 Switches_To_Check.Table (J).all = Args.Table (K).all
1776 then
1777 Arg := K + 1;
1778 Switch_Found := True;
1779 exit;
1780 end if;
1781 end loop;
1783 if not Switch_Found then
1784 if Verbose_Mode then
1785 Verbose_Msg (ALIs.Table (ALI).Sfile,
1786 "switch mismatch """ &
1787 Switches_To_Check.Table (J).all & '"');
1788 end if;
1790 ALI := No_ALI_Id;
1791 return;
1792 end if;
1793 end loop;
1795 if Switches_To_Check.Last /=
1796 Integer (Units.Table (ALIs.Table (ALI).First_Unit).Last_Arg -
1797 Units.Table (ALIs.Table (ALI).First_Unit).First_Arg + 1)
1798 then
1799 if Verbose_Mode then
1800 Verbose_Msg (ALIs.Table (ALI).Sfile,
1801 "different number of switches");
1803 for K in Units.Table (ALIs.Table (ALI).First_Unit).First_Arg
1804 .. Units.Table (ALIs.Table (ALI).First_Unit).Last_Arg
1805 loop
1806 Write_Str (Args.Table (K).all);
1807 Write_Char (' ');
1808 end loop;
1810 Write_Eol;
1812 for J in 1 .. Switches_To_Check.Last loop
1813 Write_Str (Switches_To_Check.Table (J).all);
1814 Write_Char (' ');
1815 end loop;
1817 Write_Eol;
1818 end if;
1820 ALI := No_ALI_Id;
1821 return;
1822 end if;
1823 end if;
1825 -- Get the source files and their message digests. Note that some
1826 -- sources may be missing if ALI is out-of-date.
1828 Set_Source_Table (ALI);
1830 Modified_Source := Time_Stamp_Mismatch (ALI, Read_Only);
1832 -- To avoid using too much memory when switch -m is used, free the
1833 -- memory allocated for the source file when computing the checksum.
1835 if Minimal_Recompilation then
1836 Sinput.P.Clear_Source_File_Table;
1837 end if;
1839 if Modified_Source /= No_File then
1840 ALI := No_ALI_Id;
1842 if Verbose_Mode then
1843 Source_Name := Full_Source_Name (Modified_Source);
1845 if Source_Name /= No_File then
1846 Verbose_Msg (Source_Name, "time stamp mismatch");
1847 else
1848 Verbose_Msg (Modified_Source, "missing");
1849 end if;
1850 end if;
1852 else
1853 New_Spec := First_New_Spec (ALI);
1855 if New_Spec /= No_File then
1856 ALI := No_ALI_Id;
1858 if Verbose_Mode then
1859 Source_Name := Full_Source_Name (New_Spec);
1861 if Source_Name /= No_File then
1862 Verbose_Msg (Source_Name, "new spec");
1863 else
1864 Verbose_Msg (New_Spec, "old spec missing");
1865 end if;
1866 end if;
1868 elsif not Read_Only and then Main_Project /= No_Project then
1869 declare
1870 Uname : constant Name_Id :=
1871 Check_Source_Info_In_ALI (ALI, Project_Tree);
1873 Udata : Prj.Unit_Index;
1875 begin
1876 if Uname = No_Name then
1877 ALI := No_ALI_Id;
1878 return;
1879 end if;
1881 -- Check that ALI file is in the correct object directory.
1882 -- If it is in the object directory of a project that is
1883 -- extended and it depends on a source that is in one of
1884 -- its extending projects, then the ALI file is not in the
1885 -- correct object directory.
1887 -- First, find the project of this ALI file. As there may be
1888 -- several projects with the same object directory, we first
1889 -- need to find the project of the source.
1891 ALI_Project := No_Project;
1893 Udata := Units_Htable.Get (Project_Tree.Units_HT, Uname);
1895 if Udata /= No_Unit_Index then
1896 if Udata.File_Names (Impl) /= null
1897 and then Udata.File_Names (Impl).File = Source_File
1898 then
1899 ALI_Project := Udata.File_Names (Impl).Project;
1901 elsif Udata.File_Names (Spec) /= null
1902 and then Udata.File_Names (Spec).File = Source_File
1903 then
1904 ALI_Project := Udata.File_Names (Spec).Project;
1905 end if;
1906 end if;
1907 end;
1909 if ALI_Project = No_Project then
1910 return;
1911 end if;
1913 declare
1914 Obj_Dir : Path_Name_Type;
1915 Res_Obj_Dir : constant String :=
1916 Normalize_Pathname
1917 (Dir_Name
1918 (Get_Name_String (Full_Lib_File)),
1919 Resolve_Links =>
1920 Opt.Follow_Links_For_Dirs,
1921 Case_Sensitive => False);
1923 begin
1924 Name_Len := 0;
1925 Add_Str_To_Name_Buffer (Res_Obj_Dir);
1927 if not Is_Directory_Separator (Name_Buffer (Name_Len)) then
1928 Add_Char_To_Name_Buffer (Directory_Separator);
1929 end if;
1931 Obj_Dir := Name_Find;
1933 while ALI_Project /= No_Project
1934 and then Obj_Dir /= ALI_Project.Object_Directory.Name
1935 loop
1936 ALI_Project := ALI_Project.Extended_By;
1937 end loop;
1938 end;
1940 if ALI_Project = No_Project then
1941 ALI := No_ALI_Id;
1943 Verbose_Msg (Lib_File, " wrong object directory");
1944 return;
1945 end if;
1947 -- If the ALI project is not extended, then it must be in
1948 -- the correct object directory.
1950 if ALI_Project.Extended_By = No_Project then
1951 return;
1952 end if;
1954 -- Count the extending projects
1956 declare
1957 Num_Ext : Natural;
1958 Proj : Project_Id;
1960 begin
1961 Num_Ext := 0;
1962 Proj := ALI_Project;
1963 loop
1964 Proj := Proj.Extended_By;
1965 exit when Proj = No_Project;
1966 Num_Ext := Num_Ext + 1;
1967 end loop;
1969 -- Make a list of the extending projects
1971 declare
1972 Projects : array (1 .. Num_Ext) of Project_Id;
1973 Dep : Sdep_Record;
1974 OK : Boolean := True;
1975 UID : Unit_Index;
1977 begin
1978 Proj := ALI_Project;
1979 for J in Projects'Range loop
1980 Proj := Proj.Extended_By;
1981 Projects (J) := Proj;
1982 end loop;
1984 -- Now check if any of the dependant sources are in any
1985 -- of these extending projects.
1987 D_Chk :
1988 for D in ALIs.Table (ALI).First_Sdep ..
1989 ALIs.Table (ALI).Last_Sdep
1990 loop
1991 Dep := Sdep.Table (D);
1992 UID := Units_Htable.Get_First (Project_Tree.Units_HT);
1993 Proj := No_Project;
1995 Unit_Loop :
1996 while UID /= null loop
1997 if UID.File_Names (Impl) /= null
1998 and then UID.File_Names (Impl).File = Dep.Sfile
1999 then
2000 Proj := UID.File_Names (Impl).Project;
2002 elsif UID.File_Names (Spec) /= null
2003 and then UID.File_Names (Spec).File = Dep.Sfile
2004 then
2005 Proj := UID.File_Names (Spec).Project;
2006 end if;
2008 -- If a source is in a project, check if it is one
2009 -- in the list.
2011 if Proj /= No_Project then
2012 for J in Projects'Range loop
2013 if Proj = Projects (J) then
2014 OK := False;
2015 exit D_Chk;
2016 end if;
2017 end loop;
2019 exit Unit_Loop;
2020 end if;
2022 UID :=
2023 Units_Htable.Get_Next (Project_Tree.Units_HT);
2024 end loop Unit_Loop;
2025 end loop D_Chk;
2027 -- If one of the dependent sources is in one project of
2028 -- the list, then we must recompile.
2030 if not OK then
2031 ALI := No_ALI_Id;
2032 Verbose_Msg (Lib_File, " wrong object directory");
2033 end if;
2034 end;
2035 end;
2036 end if;
2037 end if;
2038 end if;
2039 end Check;
2041 ------------------------
2042 -- Check_For_S_Switch --
2043 ------------------------
2045 procedure Check_For_S_Switch is
2046 begin
2047 -- By default, we generate an object file
2049 Output_Is_Object := True;
2051 for Arg in 1 .. Last_Argument loop
2052 if Arguments (Arg).all = "-S" then
2053 Output_Is_Object := False;
2055 elsif Arguments (Arg).all = "-c" then
2056 Output_Is_Object := True;
2057 end if;
2058 end loop;
2059 end Check_For_S_Switch;
2061 --------------------------
2062 -- Check_Linker_Options --
2063 --------------------------
2065 procedure Check_Linker_Options
2066 (E_Stamp : Time_Stamp_Type;
2067 O_File : out File_Name_Type;
2068 O_Stamp : out Time_Stamp_Type)
2070 procedure Check_File (File : File_Name_Type);
2071 -- Update O_File and O_Stamp if the given file is younger than E_Stamp
2072 -- and O_Stamp, or if O_File is No_File and File does not exist.
2074 function Get_Library_File (Name : String) return File_Name_Type;
2075 -- Return the full file name including path of a library based
2076 -- on the name specified with the -l linker option, using the
2077 -- Ada object path. Return No_File if no such file can be found.
2079 type Char_Array is array (Natural) of Character;
2080 type Char_Array_Access is access constant Char_Array;
2082 Template : Char_Array_Access;
2083 pragma Import (C, Template, "__gnat_library_template");
2085 ----------------
2086 -- Check_File --
2087 ----------------
2089 procedure Check_File (File : File_Name_Type) is
2090 Stamp : Time_Stamp_Type;
2091 Name : File_Name_Type := File;
2093 begin
2094 Get_Name_String (Name);
2096 -- Remove any trailing NUL characters
2098 while Name_Len >= Name_Buffer'First
2099 and then Name_Buffer (Name_Len) = NUL
2100 loop
2101 Name_Len := Name_Len - 1;
2102 end loop;
2104 if Name_Len = 0 then
2105 return;
2107 elsif Name_Buffer (1) = '-' then
2109 -- Do not check if File is a switch other than "-l"
2111 if Name_Buffer (2) /= 'l' then
2112 return;
2113 end if;
2115 -- The argument is a library switch, get actual name. It
2116 -- is necessary to make a copy of the relevant part of
2117 -- Name_Buffer as Get_Library_Name uses Name_Buffer as well.
2119 declare
2120 Base_Name : constant String := Name_Buffer (3 .. Name_Len);
2122 begin
2123 Name := Get_Library_File (Base_Name);
2124 end;
2126 if Name = No_File then
2127 return;
2128 end if;
2129 end if;
2131 Stamp := File_Stamp (Name);
2133 -- Find the youngest object file that is younger than the
2134 -- executable. If no such file exist, record the first object
2135 -- file that is not found.
2137 if (O_Stamp < Stamp and then E_Stamp < Stamp)
2138 or else (O_File = No_File and then Stamp (Stamp'First) = ' ')
2139 then
2140 O_Stamp := Stamp;
2141 O_File := Name;
2143 -- Strip the trailing NUL if present
2145 Get_Name_String (O_File);
2147 if Name_Buffer (Name_Len) = NUL then
2148 Name_Len := Name_Len - 1;
2149 O_File := Name_Find;
2150 end if;
2151 end if;
2152 end Check_File;
2154 ----------------------
2155 -- Get_Library_Name --
2156 ----------------------
2158 -- See comments in a-adaint.c about template syntax
2160 function Get_Library_File (Name : String) return File_Name_Type is
2161 File : File_Name_Type := No_File;
2163 begin
2164 Name_Len := 0;
2166 for Ptr in Template'Range loop
2167 case Template (Ptr) is
2168 when '*' =>
2169 Add_Str_To_Name_Buffer (Name);
2171 when ';' =>
2172 File := Full_Lib_File_Name (Name_Find);
2173 exit when File /= No_File;
2174 Name_Len := 0;
2176 when NUL =>
2177 exit;
2179 when others =>
2180 Add_Char_To_Name_Buffer (Template (Ptr));
2181 end case;
2182 end loop;
2184 -- The for loop exited because the end of the template
2185 -- was reached. File contains the last possible file name
2186 -- for the library.
2188 if File = No_File and then Name_Len > 0 then
2189 File := Full_Lib_File_Name (Name_Find);
2190 end if;
2192 return File;
2193 end Get_Library_File;
2195 -- Start of processing for Check_Linker_Options
2197 begin
2198 O_File := No_File;
2199 O_Stamp := (others => ' ');
2201 -- Process linker options from the ALI files
2203 for Opt in 1 .. Linker_Options.Last loop
2204 Check_File (File_Name_Type (Linker_Options.Table (Opt).Name));
2205 end loop;
2207 -- Process options given on the command line
2209 for Opt in Linker_Switches.First .. Linker_Switches.Last loop
2211 -- Check if the previous Opt has one of the two switches
2212 -- that take an extra parameter. (See GCC manual.)
2214 if Opt = Linker_Switches.First
2215 or else (Linker_Switches.Table (Opt - 1).all /= "-u"
2216 and then
2217 Linker_Switches.Table (Opt - 1).all /= "-Xlinker"
2218 and then
2219 Linker_Switches.Table (Opt - 1).all /= "-L")
2220 then
2221 Name_Len := 0;
2222 Add_Str_To_Name_Buffer (Linker_Switches.Table (Opt).all);
2223 Check_File (Name_Find);
2224 end if;
2225 end loop;
2226 end Check_Linker_Options;
2228 -----------------
2229 -- Check_Steps --
2230 -----------------
2232 procedure Check_Steps is
2233 begin
2234 -- If either -c, -b or -l has been specified, we will not necessarily
2235 -- execute all steps.
2237 if Make_Steps then
2238 Do_Compile_Step := Do_Compile_Step and Compile_Only;
2239 Do_Bind_Step := Do_Bind_Step and Bind_Only;
2240 Do_Link_Step := Do_Link_Step and Link_Only;
2242 -- If -c has been specified, but not -b, ignore any potential -l
2244 if Do_Compile_Step and then not Do_Bind_Step then
2245 Do_Link_Step := False;
2246 end if;
2247 end if;
2248 end Check_Steps;
2250 -----------------------
2251 -- Collect_Arguments --
2252 -----------------------
2254 procedure Collect_Arguments
2255 (Source_File : File_Name_Type;
2256 Is_Main_Source : Boolean;
2257 Args : Argument_List)
2259 pragma Unreferenced (Is_Main_Source);
2261 begin
2262 Arguments_Project := No_Project;
2263 Last_Argument := 0;
2264 Add_Arguments (Args);
2266 if Main_Project /= No_Project then
2267 declare
2268 Source_File_Name : constant String :=
2269 Get_Name_String (Source_File);
2270 Compiler_Package : Prj.Package_Id;
2271 Switches : Prj.Variable_Value;
2273 begin
2274 Prj.Env.
2275 Get_Reference
2276 (Source_File_Name => Source_File_Name,
2277 Project => Arguments_Project,
2278 Path => Arguments_Path_Name,
2279 In_Tree => Project_Tree);
2281 -- If the source is not a source of a project file, add the
2282 -- recorded arguments. Check will be done later if the source
2283 -- need to be compiled that the switch -x has been used.
2285 if Arguments_Project = No_Project then
2286 Add_Arguments (The_Saved_Gcc_Switches.all);
2288 elsif not Arguments_Project.Externally_Built or else Must_Compile
2289 then
2290 -- We get the project directory for the relative path
2291 -- switches and arguments.
2293 Arguments_Project :=
2294 Ultimate_Extending_Project_Of (Arguments_Project);
2296 -- If building a dynamic or relocatable library, compile with
2297 -- PIC option, if it exists.
2299 if Arguments_Project.Library
2300 and then Arguments_Project.Library_Kind /= Static
2301 then
2302 declare
2303 PIC : constant String := MLib.Tgt.PIC_Option;
2304 begin
2305 if PIC /= "" then
2306 Add_Arguments ((1 => new String'(PIC)));
2307 end if;
2308 end;
2309 end if;
2311 -- We now look for package Compiler and get the switches from
2312 -- this package.
2314 Compiler_Package :=
2315 Prj.Util.Value_Of
2316 (Name => Name_Compiler,
2317 In_Packages => Arguments_Project.Decl.Packages,
2318 Shared => Project_Tree.Shared);
2320 if Compiler_Package /= No_Package then
2322 -- If package Gnatmake.Compiler exists, we get the specific
2323 -- switches for the current source, or the global switches,
2324 -- if any.
2326 Switches :=
2327 Switches_Of
2328 (Source_File => Source_File,
2329 Project => Arguments_Project,
2330 In_Package => Compiler_Package,
2331 Allow_ALI => False);
2333 end if;
2335 case Switches.Kind is
2337 -- We have a list of switches. We add these switches,
2338 -- plus the saved gcc switches.
2340 when List =>
2341 declare
2342 Current : String_List_Id := Switches.Values;
2343 Element : String_Element;
2344 Number : Natural := 0;
2346 begin
2347 while Current /= Nil_String loop
2348 Element := Project_Tree.Shared.String_Elements.
2349 Table (Current);
2350 Number := Number + 1;
2351 Current := Element.Next;
2352 end loop;
2354 declare
2355 New_Args : Argument_List (1 .. Number);
2356 Last_New : Natural := 0;
2357 Dir_Path : constant String := Get_Name_String
2358 (Arguments_Project.Directory.Display_Name);
2360 begin
2361 Current := Switches.Values;
2363 for Index in New_Args'Range loop
2364 Element := Project_Tree.Shared.String_Elements.
2365 Table (Current);
2366 Get_Name_String (Element.Value);
2368 if Name_Len > 0 then
2369 Last_New := Last_New + 1;
2370 New_Args (Last_New) :=
2371 new String'(Name_Buffer (1 .. Name_Len));
2372 Ensure_Absolute_Path
2373 (New_Args (Last_New),
2374 Do_Fail => Make_Failed'Access,
2375 Parent => Dir_Path,
2376 Including_Non_Switch => False);
2377 end if;
2379 Current := Element.Next;
2380 end loop;
2382 Add_Arguments
2383 (Configuration_Pragmas_Switch (Arguments_Project)
2384 & New_Args (1 .. Last_New)
2385 & The_Saved_Gcc_Switches.all);
2386 end;
2387 end;
2389 -- We have a single switch. We add this switch,
2390 -- plus the saved gcc switches.
2392 when Single =>
2393 Get_Name_String (Switches.Value);
2395 declare
2396 New_Args : Argument_List :=
2397 (1 => new String'
2398 (Name_Buffer (1 .. Name_Len)));
2399 Dir_Path : constant String :=
2400 Get_Name_String
2401 (Arguments_Project.
2402 Directory.Display_Name);
2404 begin
2405 Ensure_Absolute_Path
2406 (New_Args (1),
2407 Do_Fail => Make_Failed'Access,
2408 Parent => Dir_Path,
2409 Including_Non_Switch => False);
2410 Add_Arguments
2411 (Configuration_Pragmas_Switch (Arguments_Project) &
2412 New_Args & The_Saved_Gcc_Switches.all);
2413 end;
2415 -- We have no switches from Gnatmake.Compiler.
2416 -- We add the saved gcc switches.
2418 when Undefined =>
2419 Add_Arguments
2420 (Configuration_Pragmas_Switch (Arguments_Project) &
2421 The_Saved_Gcc_Switches.all);
2422 end case;
2423 end if;
2424 end;
2425 end if;
2427 -- Set Output_Is_Object, depending if there is a -S switch.
2428 -- If the bind step is not performed, and there is a -S switch,
2429 -- then we will not check for a valid object file.
2431 Check_For_S_Switch;
2432 end Collect_Arguments;
2434 ---------------------
2435 -- Compile_Sources --
2436 ---------------------
2438 procedure Compile_Sources
2439 (Main_Source : File_Name_Type;
2440 Args : Argument_List;
2441 First_Compiled_File : out File_Name_Type;
2442 Most_Recent_Obj_File : out File_Name_Type;
2443 Most_Recent_Obj_Stamp : out Time_Stamp_Type;
2444 Main_Unit : out Boolean;
2445 Compilation_Failures : out Natural;
2446 Main_Index : Int := 0;
2447 Check_Readonly_Files : Boolean := False;
2448 Do_Not_Execute : Boolean := False;
2449 Force_Compilations : Boolean := False;
2450 Keep_Going : Boolean := False;
2451 In_Place_Mode : Boolean := False;
2452 Initialize_ALI_Data : Boolean := True;
2453 Max_Process : Positive := 1)
2455 Mfile : Natural := No_Mapping_File;
2456 Mapping_File_Arg : String_Access;
2457 -- Info on the mapping file
2459 Need_To_Check_Standard_Library : Boolean :=
2460 (Check_Readonly_Files or Must_Compile)
2461 and not Unique_Compile;
2463 procedure Add_Process
2464 (Pid : Process_Id;
2465 Sfile : File_Name_Type;
2466 Afile : File_Name_Type;
2467 Uname : Unit_Name_Type;
2468 Full_Lib_File : File_Name_Type;
2469 Lib_File_Attr : File_Attributes;
2470 Mfile : Natural := No_Mapping_File);
2471 -- Adds process Pid to the current list of outstanding compilation
2472 -- processes and record the full name of the source file Sfile that
2473 -- we are compiling, the name of its library file Afile and the
2474 -- name of its unit Uname. If Mfile is not equal to No_Mapping_File,
2475 -- it is the index of the mapping file used during compilation in the
2476 -- array The_Mapping_File_Names.
2478 procedure Await_Compile
2479 (Data : out Compilation_Data;
2480 OK : out Boolean);
2481 -- Awaits that an outstanding compilation process terminates. When it
2482 -- does set Data to the information registered for the corresponding
2483 -- call to Add_Process. Note that this time stamp can be used to check
2484 -- whether the compilation did generate an object file. OK is set to
2485 -- True if the compilation succeeded. Data could be No_Compilation_Data
2486 -- if there was no compilation to wait for.
2488 function Bad_Compilation_Count return Natural;
2489 -- Returns the number of compilation failures
2491 procedure Check_Standard_Library;
2492 -- Check if s-stalib.adb needs to be compiled
2494 procedure Collect_Arguments_And_Compile
2495 (Full_Source_File : File_Name_Type;
2496 Lib_File : File_Name_Type;
2497 Source_Index : Int;
2498 Pid : out Process_Id;
2499 Process_Created : out Boolean);
2500 -- Collect arguments from project file (if any) and compile. If no
2501 -- compilation was attempted, Processed_Created is set to False, and the
2502 -- value of Pid is unknown.
2504 function Compile
2505 (Project : Project_Id;
2506 S : File_Name_Type;
2507 L : File_Name_Type;
2508 Source_Index : Int;
2509 Args : Argument_List) return Process_Id;
2510 -- Compiles S using Args. If S is a GNAT predefined source "-gnatpg" is
2511 -- added to Args. Non blocking call. L corresponds to the expected
2512 -- library file name. Process_Id of the process spawned to execute the
2513 -- compilation.
2515 type ALI_Project is record
2516 ALI : ALI_Id;
2517 Project : Project_Id;
2518 end record;
2520 package Good_ALI is new Table.Table (
2521 Table_Component_Type => ALI_Project,
2522 Table_Index_Type => Natural,
2523 Table_Low_Bound => 1,
2524 Table_Initial => 50,
2525 Table_Increment => 100,
2526 Table_Name => "Make.Good_ALI");
2527 -- Contains the set of valid ALI files that have not yet been scanned
2529 function Good_ALI_Present return Boolean;
2530 -- Returns True if any ALI file was recorded in the previous set
2532 procedure Get_Mapping_File (Project : Project_Id);
2533 -- Get a mapping file name. If there is one to be reused, reuse it.
2534 -- Otherwise, create a new mapping file.
2536 function Get_Next_Good_ALI return ALI_Project;
2537 -- Returns the next good ALI_Id record
2539 procedure Record_Failure
2540 (File : File_Name_Type;
2541 Unit : Unit_Name_Type;
2542 Found : Boolean := True);
2543 -- Records in the previous table that the compilation for File failed.
2544 -- If Found is False then the compilation of File failed because we
2545 -- could not find it. Records also Unit when possible.
2547 procedure Record_Good_ALI (A : ALI_Id; Project : Project_Id);
2548 -- Records in the previous set the Id of an ALI file
2550 function Must_Exit_Because_Of_Error return Boolean;
2551 -- Return True if there were errors and the user decided to exit in such
2552 -- a case. This waits for any outstanding compilation.
2554 function Start_Compile_If_Possible (Args : Argument_List) return Boolean;
2555 -- Check if there is more work that we can do (i.e. the Queue is non
2556 -- empty). If there is, do it only if we have not yet used up all the
2557 -- available processes.
2558 -- Returns True if we should exit the main loop
2560 procedure Wait_For_Available_Slot;
2561 -- Check if we should wait for a compilation to finish. This is the case
2562 -- if all the available processes are busy compiling sources or there is
2563 -- nothing else to do (that is the Q is empty and there are no good ALIs
2564 -- to process).
2566 procedure Fill_Queue_From_ALI_Files;
2567 -- Check if we recorded good ALI files. If yes process them now in the
2568 -- order in which they have been recorded. There are two occasions in
2569 -- which we record good ali files. The first is in phase 1 when, after
2570 -- scanning an existing ALI file we realize it is up-to-date, the second
2571 -- instance is after a successful compilation.
2573 -----------------
2574 -- Add_Process --
2575 -----------------
2577 procedure Add_Process
2578 (Pid : Process_Id;
2579 Sfile : File_Name_Type;
2580 Afile : File_Name_Type;
2581 Uname : Unit_Name_Type;
2582 Full_Lib_File : File_Name_Type;
2583 Lib_File_Attr : File_Attributes;
2584 Mfile : Natural := No_Mapping_File)
2586 OC1 : constant Positive := Outstanding_Compiles + 1;
2588 begin
2589 pragma Assert (OC1 <= Max_Process);
2590 pragma Assert (Pid /= Invalid_Pid);
2592 Running_Compile (OC1) :=
2593 (Pid => Pid,
2594 Full_Source_File => Sfile,
2595 Lib_File => Afile,
2596 Full_Lib_File => Full_Lib_File,
2597 Lib_File_Attr => Lib_File_Attr,
2598 Source_Unit => Uname,
2599 Mapping_File => Mfile,
2600 Project => Arguments_Project);
2602 Outstanding_Compiles := OC1;
2604 if Arguments_Project /= No_Project then
2605 Queue.Set_Obj_Dir_Busy (Arguments_Project.Object_Directory.Name);
2606 end if;
2607 end Add_Process;
2609 --------------------
2610 -- Await_Compile --
2611 -------------------
2613 procedure Await_Compile
2614 (Data : out Compilation_Data;
2615 OK : out Boolean)
2617 Pid : Process_Id;
2618 Project : Project_Id;
2619 Comp_Data : Project_Compilation_Access;
2621 begin
2622 pragma Assert (Outstanding_Compiles > 0);
2624 Data := No_Compilation_Data;
2625 OK := False;
2627 Wait_Process (Pid, OK);
2629 if Pid = Invalid_Pid then
2630 return;
2631 end if;
2633 -- Look into the running compilation processes for this PID
2635 for J in Running_Compile'First .. Outstanding_Compiles loop
2636 if Pid = Running_Compile (J).Pid then
2637 Data := Running_Compile (J);
2638 Project := Running_Compile (J).Project;
2640 if Project /= No_Project then
2641 Queue.Set_Obj_Dir_Free (Project.Object_Directory.Name);
2642 end if;
2644 -- If a mapping file was used by this compilation, get its file
2645 -- name for reuse by a subsequent compilation.
2647 if Running_Compile (J).Mapping_File /= No_Mapping_File then
2648 Comp_Data :=
2649 Project_Compilation_Htable.Get
2650 (Project_Compilation, Project);
2651 Comp_Data.Last_Free_Indexes :=
2652 Comp_Data.Last_Free_Indexes + 1;
2653 Comp_Data.Free_Mapping_File_Indexes
2654 (Comp_Data.Last_Free_Indexes) :=
2655 Running_Compile (J).Mapping_File;
2656 end if;
2658 -- To actually remove this Pid and related info from
2659 -- Running_Compile replace its entry with the last valid
2660 -- entry in Running_Compile.
2662 if J = Outstanding_Compiles then
2663 null;
2664 else
2665 Running_Compile (J) :=
2666 Running_Compile (Outstanding_Compiles);
2667 end if;
2669 Outstanding_Compiles := Outstanding_Compiles - 1;
2670 exit;
2671 end if;
2672 end loop;
2674 -- If the PID was not found, return with OK set to False
2676 if Data = No_Compilation_Data then
2677 OK := False;
2678 end if;
2679 end Await_Compile;
2681 ---------------------------
2682 -- Bad_Compilation_Count --
2683 ---------------------------
2685 function Bad_Compilation_Count return Natural is
2686 begin
2687 return Bad_Compilation.Last - Bad_Compilation.First + 1;
2688 end Bad_Compilation_Count;
2690 ----------------------------
2691 -- Check_Standard_Library --
2692 ----------------------------
2694 procedure Check_Standard_Library is
2695 begin
2696 Need_To_Check_Standard_Library := False;
2698 if not Targparm.Suppress_Standard_Library_On_Target then
2699 declare
2700 Sfile : File_Name_Type;
2701 Add_It : Boolean := True;
2703 begin
2704 Name_Len := 0;
2705 Add_Str_To_Name_Buffer (Standard_Library_Package_Body_Name);
2706 Sfile := Name_Enter;
2708 -- If we have a special runtime, we add the standard
2709 -- library only if we can find it.
2711 if RTS_Switch then
2712 Add_It := Full_Source_Name (Sfile) /= No_File;
2713 end if;
2715 if Add_It then
2716 if not Queue.Insert
2717 ((Format => Format_Gnatmake,
2718 File => Sfile,
2719 Unit => No_Unit_Name,
2720 Project => No_Project,
2721 Index => 0,
2722 Sid => No_Source))
2723 then
2724 if Is_In_Obsoleted (Sfile) then
2725 Executable_Obsolete := True;
2726 end if;
2727 end if;
2728 end if;
2729 end;
2730 end if;
2731 end Check_Standard_Library;
2733 -----------------------------------
2734 -- Collect_Arguments_And_Compile --
2735 -----------------------------------
2737 procedure Collect_Arguments_And_Compile
2738 (Full_Source_File : File_Name_Type;
2739 Lib_File : File_Name_Type;
2740 Source_Index : Int;
2741 Pid : out Process_Id;
2742 Process_Created : out Boolean) is
2743 begin
2744 Process_Created := False;
2746 -- If we use mapping file (-P or -C switches), then get one
2748 if Create_Mapping_File then
2749 Get_Mapping_File (Arguments_Project);
2750 end if;
2752 -- If the source is part of a project file, we set the ADA_*_PATHs,
2753 -- check for an eventual library project, and use the full path.
2755 if Arguments_Project /= No_Project then
2756 if not Arguments_Project.Externally_Built
2757 or else Must_Compile
2758 then
2759 Prj.Env.Set_Ada_Paths
2760 (Arguments_Project,
2761 Project_Tree,
2762 Including_Libraries => True,
2763 Include_Path => Use_Include_Path_File);
2765 if not Unique_Compile
2766 and then MLib.Tgt.Support_For_Libraries /= Prj.None
2767 then
2768 declare
2769 Prj : constant Project_Id :=
2770 Ultimate_Extending_Project_Of (Arguments_Project);
2772 begin
2773 if Prj.Library
2774 and then (not Prj.Externally_Built or else Must_Compile)
2775 and then not Prj.Need_To_Build_Lib
2776 then
2777 -- Add to the Q all sources of the project that have
2778 -- not been marked.
2780 Insert_Project_Sources
2781 (The_Project => Prj,
2782 All_Projects => False,
2783 Into_Q => True);
2785 -- Now mark the project as processed
2787 Prj.Need_To_Build_Lib := True;
2788 end if;
2789 end;
2790 end if;
2792 Pid :=
2793 Compile
2794 (Project => Arguments_Project,
2795 S => File_Name_Type (Arguments_Path_Name),
2796 L => Lib_File,
2797 Source_Index => Source_Index,
2798 Args => Arguments (1 .. Last_Argument));
2799 Process_Created := True;
2800 end if;
2802 else
2803 -- If this is a source outside of any project file, make sure it
2804 -- will be compiled in object directory of the main project file.
2806 Pid :=
2807 Compile
2808 (Project => Main_Project,
2809 S => Full_Source_File,
2810 L => Lib_File,
2811 Source_Index => Source_Index,
2812 Args => Arguments (1 .. Last_Argument));
2813 Process_Created := True;
2814 end if;
2815 end Collect_Arguments_And_Compile;
2817 -------------
2818 -- Compile --
2819 -------------
2821 function Compile
2822 (Project : Project_Id;
2823 S : File_Name_Type;
2824 L : File_Name_Type;
2825 Source_Index : Int;
2826 Args : Argument_List) return Process_Id
2828 Comp_Args : Argument_List (Args'First .. Args'Last + 10);
2829 Comp_Next : Integer := Args'First;
2830 Comp_Last : Integer;
2831 Arg_Index : Integer;
2833 function Ada_File_Name (Name : File_Name_Type) return Boolean;
2834 -- Returns True if Name is the name of an ada source file
2835 -- (i.e. suffix is .ads or .adb)
2837 -------------------
2838 -- Ada_File_Name --
2839 -------------------
2841 function Ada_File_Name (Name : File_Name_Type) return Boolean is
2842 begin
2843 Get_Name_String (Name);
2844 return
2845 Name_Len > 4
2846 and then Name_Buffer (Name_Len - 3 .. Name_Len - 1) = ".ad"
2847 and then (Name_Buffer (Name_Len) = 'b'
2848 or else
2849 Name_Buffer (Name_Len) = 's');
2850 end Ada_File_Name;
2852 -- Start of processing for Compile
2854 begin
2855 Enter_Into_Obsoleted (S);
2857 -- By default, Syntax_Only is False
2859 Syntax_Only := False;
2861 for J in Args'Range loop
2862 if Args (J).all = "-gnats" then
2864 -- If we compile with -gnats, the bind step and the link step
2865 -- are inhibited. Also, we set Syntax_Only to True, so that
2866 -- we don't fail when we don't find the ALI file, after
2867 -- compilation.
2869 Do_Bind_Step := False;
2870 Do_Link_Step := False;
2871 Syntax_Only := True;
2873 elsif Args (J).all = "-gnatc" then
2875 -- If we compile with -gnatc, the bind step and the link step
2876 -- are inhibited. We set Syntax_Only to False for the case when
2877 -- -gnats was previously specified.
2879 Do_Bind_Step := False;
2880 Do_Link_Step := False;
2881 Syntax_Only := False;
2882 end if;
2883 end loop;
2885 Comp_Args (Comp_Next) := new String'("-gnatea");
2886 Comp_Next := Comp_Next + 1;
2888 Comp_Args (Comp_Next) := Comp_Flag;
2889 Comp_Next := Comp_Next + 1;
2891 -- Optimize the simple case where the gcc command line looks like
2892 -- gcc -c -I. ... -I- file.adb
2893 -- into
2894 -- gcc -c ... file.adb
2896 if Args (Args'First).all = "-I" & Normalized_CWD
2897 and then Args (Args'Last).all = "-I-"
2898 and then S = Strip_Directory (S)
2899 then
2900 Comp_Last := Comp_Next + Args'Length - 3;
2901 Arg_Index := Args'First + 1;
2903 else
2904 Comp_Last := Comp_Next + Args'Length - 1;
2905 Arg_Index := Args'First;
2906 end if;
2908 -- Make a deep copy of the arguments, because Normalize_Arguments
2909 -- may deallocate some arguments. Also strip target specific -mxxx
2910 -- switches in CodePeer mode.
2912 declare
2913 Index : Natural;
2914 Last : constant Natural := Comp_Last;
2916 begin
2917 Index := Comp_Next;
2918 for J in Comp_Next .. Last loop
2919 declare
2920 Str : String renames Args (Arg_Index).all;
2921 begin
2922 if CodePeer_Mode
2923 and then Str'Length > 2
2924 and then Str (Str'First .. Str'First + 1) = "-m"
2925 then
2926 Comp_Last := Comp_Last - 1;
2927 else
2928 Comp_Args (Index) := new String'(Str);
2929 Index := Index + 1;
2930 end if;
2931 end;
2933 Arg_Index := Arg_Index + 1;
2934 end loop;
2935 end;
2937 -- Set -gnatpg for predefined files (for this purpose the renamings
2938 -- such as Text_IO do not count as predefined). Note that we strip
2939 -- the directory name from the source file name because the call to
2940 -- Fname.Is_Predefined_File_Name cannot deal with directory prefixes.
2942 declare
2943 Fname : constant File_Name_Type := Strip_Directory (S);
2945 begin
2946 if Is_Predefined_File_Name (Fname, False) then
2947 if Check_Readonly_Files or else Must_Compile then
2948 Comp_Args (Comp_Args'First + 2 .. Comp_Last + 1) :=
2949 Comp_Args (Comp_Args'First + 1 .. Comp_Last);
2950 Comp_Last := Comp_Last + 1;
2951 Comp_Args (Comp_Args'First + 1) := GNAT_Flag;
2953 else
2954 Make_Failed
2955 ("not allowed to compile """ &
2956 Get_Name_String (Fname) &
2957 """; use -a switch, or use the compiler directly with "
2958 & "the ""-gnatg"" switch");
2959 end if;
2960 end if;
2961 end;
2963 -- Now check if the file name has one of the suffixes familiar to
2964 -- the gcc driver. If this is not the case then add the ada flag
2965 -- "-x ada".
2966 -- Append systematically "-x adascil" in CodePeer mode instead, to
2967 -- force the use of gnat1scil instead of gnat1.
2969 if CodePeer_Mode then
2970 Comp_Last := Comp_Last + 1;
2971 Comp_Args (Comp_Last) := Ada_Flag_1;
2972 Comp_Last := Comp_Last + 1;
2973 Comp_Args (Comp_Last) := AdaSCIL_Flag;
2975 elsif not Ada_File_Name (S) and then not Targparm.AAMP_On_Target then
2976 Comp_Last := Comp_Last + 1;
2977 Comp_Args (Comp_Last) := Ada_Flag_1;
2978 Comp_Last := Comp_Last + 1;
2979 Comp_Args (Comp_Last) := Ada_Flag_2;
2980 end if;
2982 if Source_Index /= 0 then
2983 declare
2984 Num : constant String := Source_Index'Img;
2985 begin
2986 Comp_Last := Comp_Last + 1;
2987 Comp_Args (Comp_Last) :=
2988 new String'("-gnateI" & Num (Num'First + 1 .. Num'Last));
2989 end;
2990 end if;
2992 if Source_Index /= 0
2993 or else L /= Strip_Directory (L)
2994 or else Object_Directory_Path /= null
2995 then
2996 -- Build -o argument
2998 Get_Name_String (L);
3000 for J in reverse 1 .. Name_Len loop
3001 if Name_Buffer (J) = '.' then
3002 Name_Len := J + Object_Suffix'Length - 1;
3003 Name_Buffer (J .. Name_Len) := Object_Suffix;
3004 exit;
3005 end if;
3006 end loop;
3008 Comp_Last := Comp_Last + 1;
3009 Comp_Args (Comp_Last) := Output_Flag;
3010 Comp_Last := Comp_Last + 1;
3012 -- If an object directory was specified, prepend the object file
3013 -- name with this object directory.
3015 if Object_Directory_Path /= null then
3016 Comp_Args (Comp_Last) :=
3017 new String'(Object_Directory_Path.all &
3018 Name_Buffer (1 .. Name_Len));
3020 else
3021 Comp_Args (Comp_Last) :=
3022 new String'(Name_Buffer (1 .. Name_Len));
3023 end if;
3024 end if;
3026 if Create_Mapping_File and then Mapping_File_Arg /= null then
3027 Comp_Last := Comp_Last + 1;
3028 Comp_Args (Comp_Last) := new String'(Mapping_File_Arg.all);
3029 end if;
3031 Get_Name_String (S);
3033 Comp_Last := Comp_Last + 1;
3034 Comp_Args (Comp_Last) := new String'(Name_Buffer (1 .. Name_Len));
3036 -- Change to object directory of the project file, if necessary
3038 if Project /= No_Project then
3039 Change_To_Object_Directory (Project);
3040 end if;
3042 GNAT.OS_Lib.Normalize_Arguments (Comp_Args (Args'First .. Comp_Last));
3044 Comp_Last := Comp_Last + 1;
3045 Comp_Args (Comp_Last) := new String'("-gnatez");
3047 Display (Gcc.all, Comp_Args (Args'First .. Comp_Last));
3049 if Gcc_Path = null then
3050 Make_Failed ("error, unable to locate " & Gcc.all);
3051 end if;
3053 return
3054 GNAT.OS_Lib.Non_Blocking_Spawn
3055 (Gcc_Path.all, Comp_Args (Args'First .. Comp_Last));
3056 end Compile;
3058 -------------------------------
3059 -- Fill_Queue_From_ALI_Files --
3060 -------------------------------
3062 procedure Fill_Queue_From_ALI_Files is
3063 ALI_P : ALI_Project;
3064 ALI : ALI_Id;
3065 Source_Index : Int;
3066 Sfile : File_Name_Type;
3067 Sid : Prj.Source_Id;
3068 Uname : Unit_Name_Type;
3069 Unit_Name : Name_Id;
3070 Uid : Prj.Unit_Index;
3072 begin
3073 while Good_ALI_Present loop
3074 ALI_P := Get_Next_Good_ALI;
3075 ALI := ALI_P.ALI;
3076 Source_Index := Unit_Index_Of (ALIs.Table (ALI_P.ALI).Afile);
3078 -- If we are processing the library file corresponding to the
3079 -- main source file check if this source can be a main unit.
3081 if ALIs.Table (ALI).Sfile = Main_Source
3082 and then Source_Index = Main_Index
3083 then
3084 Main_Unit := ALIs.Table (ALI).Main_Program /= None;
3085 end if;
3087 -- The following adds the standard library (s-stalib) to the list
3088 -- of files to be handled by gnatmake: this file and any files it
3089 -- depends on are always included in every bind, even if they are
3090 -- not in the explicit dependency list. Of course, it is not added
3091 -- if Suppress_Standard_Library is True.
3093 -- However, to avoid annoying output about s-stalib.ali being read
3094 -- only, when "-v" is used, we add the standard library only when
3095 -- "-a" is used.
3097 if Need_To_Check_Standard_Library then
3098 Check_Standard_Library;
3099 end if;
3101 -- Now insert in the Q the unmarked source files (i.e. those which
3102 -- have never been inserted in the Q and hence never considered).
3103 -- Only do that if Unique_Compile is False.
3105 if not Unique_Compile then
3106 for J in
3107 ALIs.Table (ALI).First_Unit .. ALIs.Table (ALI).Last_Unit
3108 loop
3109 for K in
3110 Units.Table (J).First_With .. Units.Table (J).Last_With
3111 loop
3112 Sfile := Withs.Table (K).Sfile;
3113 Uname := Withs.Table (K).Uname;
3114 Sid := No_Source;
3116 -- If project files are used, find the proper source to
3117 -- compile in case Sfile is the spec but there is a body.
3119 if Main_Project /= No_Project then
3120 Get_Name_String (Uname);
3121 Name_Len := Name_Len - 2;
3122 Unit_Name := Name_Find;
3123 Uid :=
3124 Units_Htable.Get (Project_Tree.Units_HT, Unit_Name);
3126 if Uid /= Prj.No_Unit_Index then
3127 if Uid.File_Names (Impl) /= null
3128 and then not Uid.File_Names (Impl).Locally_Removed
3129 then
3130 Sfile := Uid.File_Names (Impl).File;
3131 Source_Index := Uid.File_Names (Impl).Index;
3132 Sid := Uid.File_Names (Impl);
3134 elsif Uid.File_Names (Spec) /= null
3135 and then not Uid.File_Names (Spec).Locally_Removed
3136 then
3137 Sfile := Uid.File_Names (Spec).File;
3138 Source_Index := Uid.File_Names (Spec).Index;
3139 Sid := Uid.File_Names (Spec);
3140 end if;
3141 end if;
3142 end if;
3144 Dependencies.Append ((ALIs.Table (ALI).Sfile, Sfile));
3146 if Is_In_Obsoleted (Sfile) then
3147 Executable_Obsolete := True;
3148 end if;
3150 if Sfile = No_File then
3151 Debug_Msg ("Skipping generic:", Withs.Table (K).Uname);
3153 else
3154 Source_Index := Unit_Index_Of (Withs.Table (K).Afile);
3156 if not (Check_Readonly_Files or Must_Compile)
3157 and then Is_Internal_File_Name (Sfile, False)
3158 then
3159 Debug_Msg ("Skipping internal file:", Sfile);
3161 else
3162 Queue.Insert
3163 ((Format => Format_Gnatmake,
3164 File => Sfile,
3165 Project => ALI_P.Project,
3166 Unit => Withs.Table (K).Uname,
3167 Index => Source_Index,
3168 Sid => Sid));
3169 end if;
3170 end if;
3171 end loop;
3172 end loop;
3173 end if;
3174 end loop;
3175 end Fill_Queue_From_ALI_Files;
3177 ----------------------
3178 -- Get_Mapping_File --
3179 ----------------------
3181 procedure Get_Mapping_File (Project : Project_Id) is
3182 Data : Project_Compilation_Access;
3184 begin
3185 Data := Project_Compilation_Htable.Get (Project_Compilation, Project);
3187 -- If there is a mapping file ready to be reused, reuse it
3189 if Data.Last_Free_Indexes > 0 then
3190 Mfile := Data.Free_Mapping_File_Indexes (Data.Last_Free_Indexes);
3191 Data.Last_Free_Indexes := Data.Last_Free_Indexes - 1;
3193 -- Otherwise, create and initialize a new one
3195 else
3196 Init_Mapping_File
3197 (Project => Project, Data => Data.all, File_Index => Mfile);
3198 end if;
3200 -- Put the name in the mapping file argument for the invocation
3201 -- of the compiler.
3203 Free (Mapping_File_Arg);
3204 Mapping_File_Arg :=
3205 new String'("-gnatem=" &
3206 Get_Name_String (Data.Mapping_File_Names (Mfile)));
3207 end Get_Mapping_File;
3209 -----------------------
3210 -- Get_Next_Good_ALI --
3211 -----------------------
3213 function Get_Next_Good_ALI return ALI_Project is
3214 ALIP : ALI_Project;
3216 begin
3217 pragma Assert (Good_ALI_Present);
3218 ALIP := Good_ALI.Table (Good_ALI.Last);
3219 Good_ALI.Decrement_Last;
3220 return ALIP;
3221 end Get_Next_Good_ALI;
3223 ----------------------
3224 -- Good_ALI_Present --
3225 ----------------------
3227 function Good_ALI_Present return Boolean is
3228 begin
3229 return Good_ALI.First <= Good_ALI.Last;
3230 end Good_ALI_Present;
3232 --------------------------------
3233 -- Must_Exit_Because_Of_Error --
3234 --------------------------------
3236 function Must_Exit_Because_Of_Error return Boolean is
3237 Data : Compilation_Data;
3238 Success : Boolean;
3240 begin
3241 if Bad_Compilation_Count > 0 and then not Keep_Going then
3242 while Outstanding_Compiles > 0 loop
3243 Await_Compile (Data, Success);
3245 if not Success then
3246 Record_Failure (Data.Full_Source_File, Data.Source_Unit);
3247 end if;
3248 end loop;
3250 return True;
3251 end if;
3253 return False;
3254 end Must_Exit_Because_Of_Error;
3256 --------------------
3257 -- Record_Failure --
3258 --------------------
3260 procedure Record_Failure
3261 (File : File_Name_Type;
3262 Unit : Unit_Name_Type;
3263 Found : Boolean := True)
3265 begin
3266 Bad_Compilation.Increment_Last;
3267 Bad_Compilation.Table (Bad_Compilation.Last) := (File, Unit, Found);
3268 end Record_Failure;
3270 ---------------------
3271 -- Record_Good_ALI --
3272 ---------------------
3274 procedure Record_Good_ALI (A : ALI_Id; Project : Project_Id) is
3275 begin
3276 Good_ALI.Increment_Last;
3277 Good_ALI.Table (Good_ALI.Last) := (A, Project);
3278 end Record_Good_ALI;
3280 -------------------------------
3281 -- Start_Compile_If_Possible --
3282 -------------------------------
3284 function Start_Compile_If_Possible
3285 (Args : Argument_List) return Boolean
3287 In_Lib_Dir : Boolean;
3288 Need_To_Compile : Boolean;
3289 Pid : Process_Id := Invalid_Pid;
3290 Process_Created : Boolean;
3292 Source : Queue.Source_Info;
3293 Full_Source_File : File_Name_Type := No_File;
3294 Source_File_Attr : aliased File_Attributes;
3295 -- The full name of the source file and its attributes (size, ...)
3297 Lib_File : File_Name_Type;
3298 Full_Lib_File : File_Name_Type := No_File;
3299 Lib_File_Attr : aliased File_Attributes;
3300 Read_Only : Boolean := False;
3301 ALI : ALI_Id;
3302 -- The ALI file and its attributes (size, stamp, ...)
3304 Obj_File : File_Name_Type;
3305 Obj_Stamp : Time_Stamp_Type;
3306 -- The object file
3308 Found : Boolean;
3310 begin
3311 if not Queue.Is_Virtually_Empty and then
3312 Outstanding_Compiles < Max_Process
3313 then
3314 Queue.Extract (Found, Source);
3316 -- If it is a source in a project, first look for the ALI file
3317 -- in the object directory. When the project is extending another
3318 -- the ALI file may not be found, but the source does not
3319 -- necessarily need to be compiled, as it may already be up to
3320 -- date in the project being extended. In this case, look for an
3321 -- ALI file in all the object directories, as is done when
3322 -- gnatmake is not invoked with a project file.
3324 if Source.Sid /= No_Source then
3325 Initialize_Source_Record (Source.Sid);
3326 Full_Source_File :=
3327 File_Name_Type (Source.Sid.Path.Display_Name);
3328 Lib_File := Source.Sid.Dep_Name;
3329 Full_Lib_File := File_Name_Type (Source.Sid.Dep_Path);
3330 Lib_File_Attr := Unknown_Attributes;
3332 if Full_Lib_File /= No_File then
3333 declare
3334 FLF : constant String :=
3335 Get_Name_String (Full_Lib_File) & ASCII.NUL;
3336 begin
3337 if not Is_Regular_File
3338 (FLF'Address, Lib_File_Attr'Access)
3339 then
3340 Full_Lib_File := No_File;
3341 end if;
3342 end;
3343 end if;
3344 end if;
3346 if Full_Lib_File = No_File then
3347 Osint.Full_Source_Name
3348 (Source.File,
3349 Full_File => Full_Source_File,
3350 Attr => Source_File_Attr'Access);
3352 Lib_File := Osint.Lib_File_Name (Source.File, Source.Index);
3354 Osint.Full_Lib_File_Name
3355 (Lib_File,
3356 Lib_File => Full_Lib_File,
3357 Attr => Lib_File_Attr);
3358 end if;
3360 -- If source has already been compiled, executable is obsolete
3362 if Is_In_Obsoleted (Source.File) then
3363 Executable_Obsolete := True;
3364 end if;
3366 In_Lib_Dir := Full_Lib_File /= No_File
3367 and then In_Ada_Lib_Dir (Full_Lib_File);
3369 -- Since the following requires a system call, we precompute it
3370 -- when needed.
3372 if not In_Lib_Dir then
3373 if Full_Lib_File /= No_File
3374 and then not (Check_Readonly_Files or else Must_Compile)
3375 then
3376 Get_Name_String (Full_Lib_File);
3377 Name_Buffer (Name_Len + 1) := ASCII.NUL;
3378 Read_Only := not Is_Writable_File
3379 (Name_Buffer'Address, Lib_File_Attr'Access);
3380 else
3381 Read_Only := False;
3382 end if;
3383 end if;
3385 -- If the library file is an Ada library skip it
3387 if In_Lib_Dir then
3388 Verbose_Msg
3389 (Lib_File,
3390 "is in an Ada library",
3391 Prefix => " ",
3392 Minimum_Verbosity => Opt.High);
3394 -- If the library file is a read-only library skip it, but only
3395 -- if, when using project files, this library file is in the
3396 -- right object directory (a read-only ALI file in the object
3397 -- directory of a project being extended must not be skipped).
3399 elsif Read_Only
3400 and then Is_In_Object_Directory (Source.File, Full_Lib_File)
3401 then
3402 Verbose_Msg
3403 (Lib_File,
3404 "is a read-only library",
3405 Prefix => " ",
3406 Minimum_Verbosity => Opt.High);
3408 -- The source file that we are checking cannot be located
3410 elsif Full_Source_File = No_File then
3411 Record_Failure (Source.File, Source.Unit, False);
3413 -- Source and library files can be located but are internal
3414 -- files.
3416 elsif not (Check_Readonly_Files or else Must_Compile)
3417 and then Full_Lib_File /= No_File
3418 and then Is_Internal_File_Name (Source.File, False)
3419 then
3420 if Force_Compilations then
3421 Fail
3422 ("not allowed to compile """ &
3423 Get_Name_String (Source.File) &
3424 """; use -a switch, or use the compiler directly with "
3425 & "the ""-gnatg"" switch");
3426 end if;
3428 Verbose_Msg
3429 (Lib_File,
3430 "is an internal library",
3431 Prefix => " ",
3432 Minimum_Verbosity => Opt.High);
3434 -- The source file that we are checking can be located
3436 else
3437 Collect_Arguments
3438 (Source.File, Source.File = Main_Source, Args);
3440 -- Do nothing if project of source is externally built
3442 if Arguments_Project = No_Project
3443 or else not Arguments_Project.Externally_Built
3444 or else Must_Compile
3445 then
3446 -- Don't waste any time if we have to recompile anyway
3448 Obj_Stamp := Empty_Time_Stamp;
3449 Need_To_Compile := Force_Compilations;
3451 if not Force_Compilations then
3452 Check (Source_File => Source.File,
3453 Is_Main_Source => Source.File = Main_Source,
3454 The_Args => Args,
3455 Lib_File => Lib_File,
3456 Full_Lib_File => Full_Lib_File,
3457 Lib_File_Attr => Lib_File_Attr'Access,
3458 Read_Only => Read_Only,
3459 ALI => ALI,
3460 O_File => Obj_File,
3461 O_Stamp => Obj_Stamp);
3462 Need_To_Compile := (ALI = No_ALI_Id);
3463 end if;
3465 if not Need_To_Compile then
3467 -- The ALI file is up-to-date; record its Id
3469 Record_Good_ALI (ALI, Arguments_Project);
3471 -- Record the time stamp of the most recent object
3472 -- file as long as no (re)compilations are needed.
3474 if First_Compiled_File = No_File
3475 and then (Most_Recent_Obj_File = No_File
3476 or else Obj_Stamp > Most_Recent_Obj_Stamp)
3477 then
3478 Most_Recent_Obj_File := Obj_File;
3479 Most_Recent_Obj_Stamp := Obj_Stamp;
3480 end if;
3482 else
3483 -- Check that switch -x has been used if a source outside
3484 -- of project files need to be compiled.
3486 if Main_Project /= No_Project
3487 and then Arguments_Project = No_Project
3488 and then not External_Unit_Compilation_Allowed
3489 then
3490 Make_Failed ("external source ("
3491 & Get_Name_String (Source.File)
3492 & ") is not part of any project;"
3493 & " cannot be compiled without"
3494 & " gnatmake switch -x");
3495 end if;
3497 -- Is this the first file we have to compile?
3499 if First_Compiled_File = No_File then
3500 First_Compiled_File := Full_Source_File;
3501 Most_Recent_Obj_File := No_File;
3503 if Do_Not_Execute then
3505 -- Exit the main loop
3507 return True;
3508 end if;
3509 end if;
3511 -- Compute where the ALI file must be generated in
3512 -- In_Place_Mode (this does not require to know the
3513 -- location of the object directory).
3515 if In_Place_Mode then
3516 if Full_Lib_File = No_File then
3518 -- If the library file was not found, then save
3519 -- the library file near the source file.
3521 Lib_File :=
3522 Osint.Lib_File_Name
3523 (Full_Source_File, Source.Index);
3524 Full_Lib_File := Lib_File;
3526 else
3527 -- If the library file was found, then save the
3528 -- library file in the same place.
3530 Lib_File := Full_Lib_File;
3531 end if;
3532 end if;
3534 -- Start the compilation and record it. We can do this
3535 -- because there is at least one free process. This might
3536 -- change the current directory.
3538 Collect_Arguments_And_Compile
3539 (Full_Source_File => Full_Source_File,
3540 Lib_File => Lib_File,
3541 Source_Index => Source.Index,
3542 Pid => Pid,
3543 Process_Created => Process_Created);
3545 -- Compute where the ALI file will be generated (for
3546 -- cases that might require to know the current
3547 -- directory). The current directory might be changed
3548 -- when compiling other files so we cannot rely on it
3549 -- being the same to find the resulting ALI file.
3551 if not In_Place_Mode then
3553 -- Compute the expected location of the ALI file. This
3554 -- can be from several places:
3555 -- -i => in place mode. In such a case,
3556 -- Full_Lib_File has already been set above
3557 -- -D => if specified
3558 -- or defaults in current dir
3559 -- We could simply use a call similar to
3560 -- Osint.Full_Lib_File_Name (Lib_File)
3561 -- but that involves system calls and is thus slower
3563 if Object_Directory_Path /= null then
3564 Name_Len := 0;
3565 Add_Str_To_Name_Buffer (Object_Directory_Path.all);
3566 Add_Str_To_Name_Buffer (Get_Name_String (Lib_File));
3567 Full_Lib_File := Name_Find;
3569 else
3570 if Project_Of_Current_Object_Directory /=
3571 No_Project
3572 then
3573 Get_Name_String
3574 (Project_Of_Current_Object_Directory
3575 .Object_Directory.Display_Name);
3576 Add_Str_To_Name_Buffer
3577 (Get_Name_String (Lib_File));
3578 Full_Lib_File := Name_Find;
3580 else
3581 Full_Lib_File := Lib_File;
3582 end if;
3583 end if;
3585 end if;
3587 Lib_File_Attr := Unknown_Attributes;
3589 -- Make sure we could successfully start the compilation
3591 if Process_Created then
3592 if Pid = Invalid_Pid then
3593 Record_Failure (Full_Source_File, Source.Unit);
3594 else
3595 Add_Process
3596 (Pid => Pid,
3597 Sfile => Full_Source_File,
3598 Afile => Lib_File,
3599 Uname => Source.Unit,
3600 Mfile => Mfile,
3601 Full_Lib_File => Full_Lib_File,
3602 Lib_File_Attr => Lib_File_Attr);
3603 end if;
3604 end if;
3605 end if;
3606 end if;
3607 end if;
3608 end if;
3609 return False;
3610 end Start_Compile_If_Possible;
3612 -----------------------------
3613 -- Wait_For_Available_Slot --
3614 -----------------------------
3616 procedure Wait_For_Available_Slot is
3617 Compilation_OK : Boolean;
3618 Text : Text_Buffer_Ptr;
3619 ALI : ALI_Id;
3620 Data : Compilation_Data;
3622 begin
3623 if Outstanding_Compiles = Max_Process
3624 or else (Queue.Is_Virtually_Empty
3625 and then not Good_ALI_Present
3626 and then Outstanding_Compiles > 0)
3627 then
3628 Await_Compile (Data, Compilation_OK);
3630 if not Compilation_OK then
3631 Record_Failure (Data.Full_Source_File, Data.Source_Unit);
3632 end if;
3634 if Compilation_OK or else Keep_Going then
3636 -- Re-read the updated library file
3638 declare
3639 Saved_Object_Consistency : constant Boolean :=
3640 Check_Object_Consistency;
3642 begin
3643 -- If compilation was not OK, or if output is not an object
3644 -- file and we don't do the bind step, don't check for
3645 -- object consistency.
3647 Check_Object_Consistency :=
3648 Check_Object_Consistency
3649 and Compilation_OK
3650 and (Output_Is_Object or Do_Bind_Step);
3652 Text :=
3653 Read_Library_Info_From_Full
3654 (Data.Full_Lib_File, Data.Lib_File_Attr'Access);
3656 -- Restore Check_Object_Consistency to its initial value
3658 Check_Object_Consistency := Saved_Object_Consistency;
3659 end;
3661 -- If an ALI file was generated by this compilation, scan the
3662 -- ALI file and record it.
3664 -- If the scan fails, a previous ali file is inconsistent with
3665 -- the unit just compiled.
3667 if Text /= null then
3668 ALI :=
3669 Scan_ALI
3670 (Data.Lib_File, Text, Ignore_ED => False, Err => True);
3672 if ALI = No_ALI_Id then
3674 -- Record a failure only if not already done
3676 if Compilation_OK then
3677 Inform
3678 (Data.Lib_File,
3679 "incompatible ALI file, please recompile");
3680 Record_Failure
3681 (Data.Full_Source_File, Data.Source_Unit);
3682 end if;
3684 else
3685 Record_Good_ALI (ALI, Data.Project);
3686 end if;
3688 Free (Text);
3690 -- If we could not read the ALI file that was just generated
3691 -- then there could be a problem reading either the ALI or the
3692 -- corresponding object file (if Check_Object_Consistency is
3693 -- set Read_Library_Info checks that the time stamp of the
3694 -- object file is more recent than that of the ALI). However,
3695 -- we record a failure only if not already done.
3697 else
3698 if Compilation_OK and not Syntax_Only then
3699 Inform
3700 (Data.Lib_File,
3701 "WARNING: ALI or object file not found after compile");
3703 if not Is_Regular_File
3704 (Get_Name_String (Name_Id (Data.Full_Lib_File)))
3705 then
3706 Inform (Data.Full_Lib_File, "not found");
3707 end if;
3709 Record_Failure (Data.Full_Source_File, Data.Source_Unit);
3710 end if;
3711 end if;
3712 end if;
3713 end if;
3714 end Wait_For_Available_Slot;
3716 -- Start of processing for Compile_Sources
3718 begin
3719 pragma Assert (Args'First = 1);
3721 Outstanding_Compiles := 0;
3722 Running_Compile := new Comp_Data_Arr (1 .. Max_Process);
3724 -- Package and Queue initializations
3726 Good_ALI.Init;
3728 if Initialize_ALI_Data then
3729 Initialize_ALI;
3730 Initialize_ALI_Source;
3731 end if;
3733 -- The following two flags affect the behavior of ALI.Set_Source_Table.
3734 -- We set Check_Source_Files to True to ensure that source file time
3735 -- stamps are checked, and we set All_Sources to False to avoid checking
3736 -- the presence of the source files listed in the source dependency
3737 -- section of an ali file (which would be a mistake since the ali file
3738 -- may be obsolete).
3740 Check_Source_Files := True;
3741 All_Sources := False;
3743 Queue.Insert
3744 ((Format => Format_Gnatmake,
3745 File => Main_Source,
3746 Project => Main_Project,
3747 Unit => No_Unit_Name,
3748 Index => Main_Index,
3749 Sid => No_Source));
3751 First_Compiled_File := No_File;
3752 Most_Recent_Obj_File := No_File;
3753 Most_Recent_Obj_Stamp := Empty_Time_Stamp;
3754 Main_Unit := False;
3756 -- Keep looping until there is no more work to do (the Q is empty)
3757 -- and all the outstanding compilations have terminated.
3759 Make_Loop :
3760 while not Queue.Is_Empty or else Outstanding_Compiles > 0 loop
3761 exit Make_Loop when Must_Exit_Because_Of_Error;
3762 exit Make_Loop when Start_Compile_If_Possible (Args);
3764 Wait_For_Available_Slot;
3766 -- ??? Should be done as soon as we add a Good_ALI, wouldn't it avoid
3767 -- the need for a list of good ALI?
3769 Fill_Queue_From_ALI_Files;
3771 if Display_Compilation_Progress then
3772 Write_Str ("completed ");
3773 Write_Int (Int (Queue.Processed));
3774 Write_Str (" out of ");
3775 Write_Int (Int (Queue.Size));
3776 Write_Str (" (");
3777 Write_Int (Int ((Queue.Processed * 100) / Queue.Size));
3778 Write_Str ("%)...");
3779 Write_Eol;
3780 end if;
3781 end loop Make_Loop;
3783 Compilation_Failures := Bad_Compilation_Count;
3785 -- Compilation is finished
3787 -- Delete any temporary configuration pragma file
3789 if not Keep_Temporary_Files then
3790 Delete_Temp_Config_Files (Project_Tree);
3791 end if;
3792 end Compile_Sources;
3794 ----------------------------------
3795 -- Configuration_Pragmas_Switch --
3796 ----------------------------------
3798 function Configuration_Pragmas_Switch
3799 (For_Project : Project_Id) return Argument_List
3801 The_Packages : Package_Id;
3802 Gnatmake : Package_Id;
3803 Compiler : Package_Id;
3805 Global_Attribute : Variable_Value := Nil_Variable_Value;
3806 Local_Attribute : Variable_Value := Nil_Variable_Value;
3808 Global_Attribute_Present : Boolean := False;
3809 Local_Attribute_Present : Boolean := False;
3811 Result : Argument_List (1 .. 3);
3812 Last : Natural := 0;
3814 begin
3815 Prj.Env.Create_Config_Pragmas_File
3816 (For_Project, Project_Tree);
3818 if For_Project.Config_File_Name /= No_Path then
3819 Temporary_Config_File := For_Project.Config_File_Temp;
3820 Last := 1;
3821 Result (1) :=
3822 new String'
3823 ("-gnatec=" & Get_Name_String (For_Project.Config_File_Name));
3825 else
3826 Temporary_Config_File := False;
3827 end if;
3829 -- Check for attribute Builder'Global_Configuration_Pragmas
3831 The_Packages := Main_Project.Decl.Packages;
3832 Gnatmake :=
3833 Prj.Util.Value_Of
3834 (Name => Name_Builder,
3835 In_Packages => The_Packages,
3836 Shared => Project_Tree.Shared);
3838 if Gnatmake /= No_Package then
3839 Global_Attribute := Prj.Util.Value_Of
3840 (Variable_Name => Name_Global_Configuration_Pragmas,
3841 In_Variables => Project_Tree.Shared.Packages.Table
3842 (Gnatmake).Decl.Attributes,
3843 Shared => Project_Tree.Shared);
3844 Global_Attribute_Present :=
3845 Global_Attribute /= Nil_Variable_Value
3846 and then Get_Name_String (Global_Attribute.Value) /= "";
3848 if Global_Attribute_Present then
3849 declare
3850 Path : constant String :=
3851 Absolute_Path
3852 (Path_Name_Type (Global_Attribute.Value),
3853 Global_Attribute.Project);
3854 begin
3855 if not Is_Regular_File (Path) then
3856 if Debug.Debug_Flag_F then
3857 Make_Failed
3858 ("cannot find configuration pragmas file "
3859 & File_Name (Path));
3860 else
3861 Make_Failed
3862 ("cannot find configuration pragmas file " & Path);
3863 end if;
3864 end if;
3866 Last := Last + 1;
3867 Result (Last) := new String'("-gnatec=" & Path);
3868 end;
3869 end if;
3870 end if;
3872 -- Check for attribute Compiler'Local_Configuration_Pragmas
3874 The_Packages := For_Project.Decl.Packages;
3875 Compiler :=
3876 Prj.Util.Value_Of
3877 (Name => Name_Compiler,
3878 In_Packages => The_Packages,
3879 Shared => Project_Tree.Shared);
3881 if Compiler /= No_Package then
3882 Local_Attribute := Prj.Util.Value_Of
3883 (Variable_Name => Name_Local_Configuration_Pragmas,
3884 In_Variables => Project_Tree.Shared.Packages.Table
3885 (Compiler).Decl.Attributes,
3886 Shared => Project_Tree.Shared);
3887 Local_Attribute_Present :=
3888 Local_Attribute /= Nil_Variable_Value
3889 and then Get_Name_String (Local_Attribute.Value) /= "";
3891 if Local_Attribute_Present then
3892 declare
3893 Path : constant String :=
3894 Absolute_Path
3895 (Path_Name_Type (Local_Attribute.Value),
3896 Local_Attribute.Project);
3897 begin
3898 if not Is_Regular_File (Path) then
3899 if Debug.Debug_Flag_F then
3900 Make_Failed
3901 ("cannot find configuration pragmas file "
3902 & File_Name (Path));
3904 else
3905 Make_Failed
3906 ("cannot find configuration pragmas file " & Path);
3907 end if;
3908 end if;
3910 Last := Last + 1;
3911 Result (Last) := new String'("-gnatec=" & Path);
3912 end;
3913 end if;
3914 end if;
3916 return Result (1 .. Last);
3917 end Configuration_Pragmas_Switch;
3919 ---------------
3920 -- Debug_Msg --
3921 ---------------
3923 procedure Debug_Msg (S : String; N : Name_Id) is
3924 begin
3925 if Debug.Debug_Flag_W then
3926 Write_Str (" ... ");
3927 Write_Str (S);
3928 Write_Str (" ");
3929 Write_Name (N);
3930 Write_Eol;
3931 end if;
3932 end Debug_Msg;
3934 procedure Debug_Msg (S : String; N : File_Name_Type) is
3935 begin
3936 Debug_Msg (S, Name_Id (N));
3937 end Debug_Msg;
3939 procedure Debug_Msg (S : String; N : Unit_Name_Type) is
3940 begin
3941 Debug_Msg (S, Name_Id (N));
3942 end Debug_Msg;
3944 -------------
3945 -- Display --
3946 -------------
3948 procedure Display (Program : String; Args : Argument_List) is
3949 begin
3950 pragma Assert (Args'First = 1);
3952 if Display_Executed_Programs then
3953 Write_Str (Program);
3955 for J in Args'Range loop
3957 -- Never display -gnatea nor -gnatez
3959 if Args (J).all /= "-gnatea"
3960 and then
3961 Args (J).all /= "-gnatez"
3962 then
3963 -- Do not display the mapping file argument automatically
3964 -- created when using a project file.
3966 if Main_Project = No_Project
3967 or else Opt.Keep_Temporary_Files
3968 or else Args (J)'Length < 8
3969 or else
3970 Args (J) (Args (J)'First .. Args (J)'First + 6) /= "-gnatem"
3971 then
3972 -- When -dn is not specified, do not display the config
3973 -- pragmas switch (-gnatec) for the temporary file created
3974 -- by the project manager (always the first -gnatec switch).
3975 -- Reset Temporary_Config_File to False so that the eventual
3976 -- other -gnatec switches will be displayed.
3978 if not Opt.Keep_Temporary_Files
3979 and then Temporary_Config_File
3980 and then Args (J)'Length > 7
3981 and then Args (J) (Args (J)'First .. Args (J)'First + 6) =
3982 "-gnatec"
3983 then
3984 Temporary_Config_File := False;
3986 -- Do not display the -F=mapping_file switch for gnatbind
3987 -- if -dn is not specified.
3989 elsif Opt.Keep_Temporary_Files
3990 or else Args (J)'Length < 4
3991 or else
3992 Args (J) (Args (J)'First .. Args (J)'First + 2) /= "-F="
3993 then
3994 Write_Str (" ");
3996 -- If -df is used, only display file names, not path
3997 -- names.
3999 if Debug.Debug_Flag_F then
4000 declare
4001 Equal_Pos : Natural;
4003 begin
4004 Equal_Pos := Args (J)'First - 1;
4005 for K in Args (J)'Range loop
4006 if Args (J) (K) = '=' then
4007 Equal_Pos := K;
4008 exit;
4009 end if;
4010 end loop;
4012 if Is_Absolute_Path
4013 (Args (J) (Equal_Pos + 1 .. Args (J)'Last))
4014 then
4015 Write_Str
4016 (Args (J) (Args (J)'First .. Equal_Pos));
4017 Write_Str
4018 (File_Name
4019 (Args (J)
4020 (Equal_Pos + 1 .. Args (J)'Last)));
4022 else
4023 Write_Str (Args (J).all);
4024 end if;
4025 end;
4027 else
4028 Write_Str (Args (J).all);
4029 end if;
4030 end if;
4031 end if;
4032 end if;
4033 end loop;
4035 Write_Eol;
4036 end if;
4037 end Display;
4039 ----------------------
4040 -- Display_Commands --
4041 ----------------------
4043 procedure Display_Commands (Display : Boolean := True) is
4044 begin
4045 Display_Executed_Programs := Display;
4046 end Display_Commands;
4048 --------------------------
4049 -- Enter_Into_Obsoleted --
4050 --------------------------
4052 procedure Enter_Into_Obsoleted (F : File_Name_Type) is
4053 Name : constant String := Get_Name_String (F);
4054 First : Natural;
4055 F2 : File_Name_Type;
4057 begin
4058 First := Name'Last;
4059 while First > Name'First
4060 and then Name (First - 1) /= Directory_Separator
4061 and then Name (First - 1) /= '/'
4062 loop
4063 First := First - 1;
4064 end loop;
4066 if First /= Name'First then
4067 Name_Len := 0;
4068 Add_Str_To_Name_Buffer (Name (First .. Name'Last));
4069 F2 := Name_Find;
4071 else
4072 F2 := F;
4073 end if;
4075 Debug_Msg ("New entry in Obsoleted table:", F2);
4076 Obsoleted.Set (F2, True);
4077 end Enter_Into_Obsoleted;
4079 ---------------
4080 -- Globalize --
4081 ---------------
4083 procedure Globalize (Success : out Boolean) is
4084 Quiet_Str : aliased String := "-quiet";
4085 Globalizer_Args : constant Argument_List :=
4086 (1 => Quiet_Str'Unchecked_Access);
4087 Previous_Dir : String_Access;
4089 procedure Globalize_Dir (Dir : String);
4090 -- Call CodePeer globalizer on Dir
4092 -------------------
4093 -- Globalize_Dir --
4094 -------------------
4096 procedure Globalize_Dir (Dir : String) is
4097 Result : Boolean;
4098 begin
4099 if Previous_Dir = null or else Dir /= Previous_Dir.all then
4100 Free (Previous_Dir);
4101 Previous_Dir := new String'(Dir);
4102 Change_Dir (Dir);
4103 GNAT.OS_Lib.Spawn (Globalizer_Path.all, Globalizer_Args, Result);
4104 Success := Success and Result;
4105 end if;
4106 end Globalize_Dir;
4108 procedure Globalize_Dirs is new
4109 Prj.Env.For_All_Object_Dirs (Globalize_Dir);
4111 -- Start of procedure Globalize
4113 begin
4114 Success := True;
4115 Display (Globalizer, Globalizer_Args);
4117 if Globalizer_Path = null then
4118 Make_Failed ("error, unable to locate " & Globalizer);
4119 end if;
4121 if Main_Project = No_Project then
4122 GNAT.OS_Lib.Spawn (Globalizer_Path.all, Globalizer_Args, Success);
4123 else
4124 Globalize_Dirs (Main_Project, Project_Tree);
4125 end if;
4126 end Globalize;
4128 -------------------
4129 -- Linking_Phase --
4130 -------------------
4132 procedure Linking_Phase
4133 (Non_Std_Executable : Boolean := False;
4134 Executable : File_Name_Type := No_File;
4135 Main_ALI_File : File_Name_Type)
4137 Linker_Switches_Last : constant Integer := Linker_Switches.Last;
4138 Path_Option : constant String_Access :=
4139 MLib.Linker_Library_Path_Option;
4140 Libraries_Present : Boolean := False;
4141 Current : Natural;
4142 Proj2 : Project_Id;
4143 Depth : Natural;
4144 Proj1 : Project_List;
4146 begin
4147 if not Run_Path_Option then
4148 Linker_Switches.Increment_Last;
4149 Linker_Switches.Table (Linker_Switches.Last) :=
4150 new String'("-R");
4151 end if;
4153 if Main_Project /= No_Project then
4154 Library_Paths.Set_Last (0);
4155 Library_Projs.Init;
4157 if MLib.Tgt.Support_For_Libraries /= Prj.None then
4159 -- Check for library projects
4161 Proj1 := Project_Tree.Projects;
4162 while Proj1 /= null loop
4163 if Proj1.Project /= Main_Project
4164 and then Proj1.Project.Library
4165 then
4166 -- Add this project to table Library_Projs
4168 Libraries_Present := True;
4169 Depth := Proj1.Project.Depth;
4170 Library_Projs.Increment_Last;
4171 Current := Library_Projs.Last;
4173 -- Any project with a greater depth should be after this
4174 -- project in the list.
4176 while Current > 1 loop
4177 Proj2 := Library_Projs.Table (Current - 1);
4178 exit when Proj2.Depth <= Depth;
4179 Library_Projs.Table (Current) := Proj2;
4180 Current := Current - 1;
4181 end loop;
4183 Library_Projs.Table (Current) := Proj1.Project;
4185 -- If it is not a static library and path option is set, add
4186 -- it to the Library_Paths table.
4188 if Proj1.Project.Library_Kind /= Static
4189 and then Proj1.Project.Extended_By = No_Project
4190 and then Path_Option /= null
4191 then
4192 Library_Paths.Increment_Last;
4193 Library_Paths.Table (Library_Paths.Last) :=
4194 new String'
4195 (Get_Name_String
4196 (Proj1.Project.Library_Dir.Display_Name));
4197 end if;
4198 end if;
4200 Proj1 := Proj1.Next;
4201 end loop;
4203 for Index in 1 .. Library_Projs.Last loop
4204 if Library_Projs.Table (Index).Extended_By = No_Project then
4205 if Library_Projs.Table (Index).Library_Kind = Static then
4206 Linker_Switches.Increment_Last;
4207 Linker_Switches.Table (Linker_Switches.Last) :=
4208 new String'
4209 (Get_Name_String
4210 (Library_Projs.Table
4211 (Index).Library_Dir.Display_Name) &
4212 "lib" &
4213 Get_Name_String
4214 (Library_Projs.Table (Index).Library_Name) &
4215 "." &
4216 MLib.Tgt.Archive_Ext);
4218 else
4219 -- Add the -L switch
4221 Linker_Switches.Increment_Last;
4222 Linker_Switches.Table (Linker_Switches.Last) :=
4223 new String'("-L" &
4224 Get_Name_String (Library_Projs.Table (Index).
4225 Library_Dir.Display_Name));
4227 -- Add the -l switch
4229 Linker_Switches.Increment_Last;
4230 Linker_Switches.Table (Linker_Switches.Last) :=
4231 new String'("-l" &
4232 Get_Name_String
4233 (Library_Projs.Table (Index).Library_Name));
4234 end if;
4235 end if;
4236 end loop;
4237 end if;
4239 if Libraries_Present then
4241 -- If Path_Option is not null, create the switch ("-Wl,-rpath,"
4242 -- or equivalent) with all the non-static library dirs plus the
4243 -- standard GNAT library dir. We do that only if Run_Path_Option
4244 -- is True (not disabled by -R switch).
4246 if Run_Path_Option and then Path_Option /= null then
4247 declare
4248 Option : String_Access;
4249 Length : Natural := Path_Option'Length;
4250 Current : Natural;
4252 begin
4253 if MLib.Separate_Run_Path_Options then
4255 -- We are going to create one switch of the form
4256 -- "-Wl,-rpath,dir_N" for each directory to
4257 -- consider.
4259 -- One switch for each library directory
4261 for Index in
4262 Library_Paths.First .. Library_Paths.Last
4263 loop
4264 Linker_Switches.Increment_Last;
4265 Linker_Switches.Table (Linker_Switches.Last) :=
4266 new String'
4267 (Path_Option.all &
4268 Library_Paths.Table (Index).all);
4269 end loop;
4271 -- One switch for the standard GNAT library dir
4273 Linker_Switches.Increment_Last;
4274 Linker_Switches.Table (Linker_Switches.Last) :=
4275 new String'(Path_Option.all & MLib.Utl.Lib_Directory);
4277 else
4278 -- We are going to create one switch of the form
4279 -- "-Wl,-rpath,dir_1:dir_2:dir_3"
4281 for Index in Library_Paths.First .. Library_Paths.Last
4282 loop
4283 -- Add the length of the library dir plus one for the
4284 -- directory separator.
4286 Length :=
4287 Length + Library_Paths.Table (Index)'Length + 1;
4288 end loop;
4290 -- Finally, add the length of the standard GNAT
4291 -- library dir.
4293 Length := Length + MLib.Utl.Lib_Directory'Length;
4294 Option := new String (1 .. Length);
4295 Option (1 .. Path_Option'Length) := Path_Option.all;
4296 Current := Path_Option'Length;
4298 -- Put each library dir followed by a dir
4299 -- separator.
4301 for Index in Library_Paths.First .. Library_Paths.Last
4302 loop
4303 Option
4304 (Current + 1 ..
4305 Current + Library_Paths.Table (Index)'Length) :=
4306 Library_Paths.Table (Index).all;
4307 Current :=
4308 Current + Library_Paths.Table (Index)'Length + 1;
4309 Option (Current) := Path_Separator;
4310 end loop;
4312 -- Finally put the standard GNAT library dir
4314 Option
4315 (Current + 1 ..
4316 Current + MLib.Utl.Lib_Directory'Length) :=
4317 MLib.Utl.Lib_Directory;
4319 -- And add the switch to the linker switches
4321 Linker_Switches.Increment_Last;
4322 Linker_Switches.Table (Linker_Switches.Last) := Option;
4323 end if;
4324 end;
4325 end if;
4326 end if;
4328 -- Put the object directories in ADA_OBJECTS_PATH
4330 Prj.Env.Set_Ada_Paths
4331 (Main_Project,
4332 Project_Tree,
4333 Including_Libraries => False,
4334 Include_Path => False);
4336 -- Check for attributes Linker'Linker_Options in projects other than
4337 -- the main project
4339 declare
4340 Linker_Options : constant String_List :=
4341 Linker_Options_Switches
4342 (Main_Project,
4343 Do_Fail => Make_Failed'Access,
4344 In_Tree => Project_Tree);
4345 begin
4346 for Option in Linker_Options'Range loop
4347 Linker_Switches.Increment_Last;
4348 Linker_Switches.Table (Linker_Switches.Last) :=
4349 Linker_Options (Option);
4350 end loop;
4351 end;
4352 end if;
4354 if CodePeer_Mode then
4355 Linker_Switches.Increment_Last;
4356 Linker_Switches.Table (Linker_Switches.Last) :=
4357 new String'(CodePeer_Mode_String);
4358 end if;
4360 -- Add switch -M to gnatlink if builder switch --create-map-file
4361 -- has been specified.
4363 if Map_File /= null then
4364 Linker_Switches.Increment_Last;
4365 Linker_Switches.Table (Linker_Switches.Last) :=
4366 new String'("-M" & Map_File.all);
4367 end if;
4369 declare
4370 Args : Argument_List
4371 (Linker_Switches.First .. Linker_Switches.Last + 2);
4373 Last_Arg : Integer := Linker_Switches.First - 1;
4374 Skip : Boolean := False;
4376 begin
4377 -- Get all the linker switches
4379 for J in Linker_Switches.First .. Linker_Switches.Last loop
4380 if Skip then
4381 Skip := False;
4383 elsif Non_Std_Executable
4384 and then Linker_Switches.Table (J).all = "-o"
4385 then
4386 Skip := True;
4388 -- Here we capture and duplicate the linker argument. We
4389 -- need to do the duplication since the arguments will get
4390 -- normalized. Not doing so will result in calling normalized
4391 -- two times for the same set of arguments if gnatmake is
4392 -- passed multiple mains. This can result in the wrong
4393 -- argument being passed to the linker.
4395 else
4396 Last_Arg := Last_Arg + 1;
4397 Args (Last_Arg) := new String'(Linker_Switches.Table (J).all);
4398 end if;
4399 end loop;
4401 -- If need be, add the -o switch
4403 if Non_Std_Executable then
4404 Last_Arg := Last_Arg + 1;
4405 Args (Last_Arg) := new String'("-o");
4406 Last_Arg := Last_Arg + 1;
4407 Args (Last_Arg) := new String'(Get_Name_String (Executable));
4408 end if;
4410 -- And invoke the linker
4412 declare
4413 Success : Boolean := False;
4415 begin
4416 -- If gnatmake was invoked with --subdirs and no project file,
4417 -- put the executable in the subdirectory specified.
4419 if Prj.Subdirs /= null and then Main_Project = No_Project then
4420 Change_Dir (Object_Directory_Path.all);
4421 end if;
4423 Link (Main_ALI_File,
4424 Link_With_Shared_Libgcc.all &
4425 Args (Args'First .. Last_Arg),
4426 Success);
4428 if Success then
4429 Successful_Links.Increment_Last;
4430 Successful_Links.Table (Successful_Links.Last) := Main_ALI_File;
4432 elsif Osint.Number_Of_Files = 1 or else not Keep_Going then
4433 Make_Failed ("*** link failed.");
4435 else
4436 Set_Standard_Error;
4437 Write_Line ("*** link failed");
4439 if Commands_To_Stdout then
4440 Set_Standard_Output;
4441 end if;
4443 Failed_Links.Increment_Last;
4444 Failed_Links.Table (Failed_Links.Last) := Main_ALI_File;
4445 end if;
4446 end;
4447 end;
4449 Linker_Switches.Set_Last (Linker_Switches_Last);
4450 end Linking_Phase;
4452 -------------------
4453 -- Binding_Phase --
4454 -------------------
4456 procedure Binding_Phase
4457 (Stand_Alone_Libraries : Boolean := False;
4458 Main_ALI_File : File_Name_Type)
4460 Args : Argument_List (Binder_Switches.First .. Binder_Switches.Last + 2);
4461 -- The arguments for the invocation of gnatbind
4463 Last_Arg : Natural := Binder_Switches.Last;
4464 -- Index of the last argument in Args
4466 Shared_Libs : Boolean := False;
4467 -- Set to True when there are shared library project files or
4468 -- when gnatbind is invoked with -shared.
4470 Proj : Project_List;
4472 Mapping_Path : Path_Name_Type := No_Path;
4473 -- The path name of the mapping file
4475 begin
4476 -- Check if there are shared libraries, so that gnatbind is called with
4477 -- -shared. Check also if gnatbind is called with -shared, so that
4478 -- gnatlink is called with -shared-libgcc ensuring that the shared
4479 -- version of libgcc will be used.
4481 if Main_Project /= No_Project
4482 and then MLib.Tgt.Support_For_Libraries /= Prj.None
4483 then
4484 Proj := Project_Tree.Projects;
4485 while Proj /= null loop
4486 if Proj.Project.Library
4487 and then Proj.Project.Library_Kind /= Static
4488 then
4489 Shared_Libs := True;
4490 Bind_Shared := Shared_Switch'Access;
4491 exit;
4492 end if;
4494 Proj := Proj.Next;
4495 end loop;
4496 end if;
4498 -- Check now for switch -shared
4500 if not Shared_Libs then
4501 for J in Binder_Switches.First .. Last_Arg loop
4502 if Binder_Switches.Table (J).all = "-shared" then
4503 Shared_Libs := True;
4504 exit;
4505 end if;
4506 end loop;
4507 end if;
4509 -- If shared libraries present, invoke gnatlink with
4510 -- -shared-libgcc.
4512 if Shared_Libs then
4513 Link_With_Shared_Libgcc := Shared_Libgcc_Switch'Access;
4514 end if;
4516 -- Get all the binder switches
4518 for J in Binder_Switches.First .. Last_Arg loop
4519 Args (J) := Binder_Switches.Table (J);
4520 end loop;
4522 if Stand_Alone_Libraries then
4523 Last_Arg := Last_Arg + 1;
4524 Args (Last_Arg) := Force_Elab_Flags_String'Access;
4525 end if;
4527 if CodePeer_Mode then
4528 Last_Arg := Last_Arg + 1;
4529 Args (Last_Arg) := CodePeer_Mode_String'Access;
4530 end if;
4532 if Main_Project /= No_Project then
4534 -- Put all the source directories in ADA_INCLUDE_PATH, and all the
4535 -- object directories in ADA_OBJECTS_PATH.
4537 Prj.Env.Set_Ada_Paths
4538 (Project => Main_Project,
4539 In_Tree => Project_Tree,
4540 Including_Libraries => True,
4541 Include_Path => Use_Include_Path_File);
4543 -- If switch -C was specified, create a binder mapping file
4545 if Create_Mapping_File then
4546 Mapping_Path := Create_Binder_Mapping_File (Project_Tree);
4548 if Mapping_Path /= No_Path then
4549 Last_Arg := Last_Arg + 1;
4550 Args (Last_Arg) :=
4551 new String'("-F=" & Get_Name_String (Mapping_Path));
4552 end if;
4553 end if;
4554 end if;
4556 -- If gnatmake was invoked with --subdirs and no project file, put the
4557 -- binder generated files in the subdirectory specified.
4559 if Main_Project = No_Project and then Prj.Subdirs /= null then
4560 Change_Dir (Object_Directory_Path.all);
4561 end if;
4563 begin
4564 Bind (Main_ALI_File,
4565 Bind_Shared.all & Args (Args'First .. Last_Arg));
4567 exception
4568 when others =>
4570 -- Delete the temporary mapping file if one was created
4572 if Mapping_Path /= No_Path then
4573 Delete_Temporary_File (Project_Tree.Shared, Mapping_Path);
4574 end if;
4576 -- And reraise the exception
4578 raise;
4579 end;
4581 -- If -dn was not specified, delete the temporary mapping file
4582 -- if one was created.
4584 if Mapping_Path /= No_Path then
4585 Delete_Temporary_File (Project_Tree.Shared, Mapping_Path);
4586 end if;
4587 end Binding_Phase;
4589 -------------------
4590 -- Library_Phase --
4591 -------------------
4593 procedure Library_Phase
4594 (Stand_Alone_Libraries : in out Boolean;
4595 Library_Rebuilt : in out Boolean)
4597 Depth : Natural;
4598 Current : Natural;
4599 Proj1 : Project_List;
4601 procedure Add_To_Library_Projs (Proj : Project_Id);
4602 -- Add project Project to table Library_Projs in decreasing depth order
4604 --------------------------
4605 -- Add_To_Library_Projs --
4606 --------------------------
4608 procedure Add_To_Library_Projs (Proj : Project_Id) is
4609 Prj : Project_Id;
4611 begin
4612 Library_Projs.Increment_Last;
4613 Depth := Proj.Depth;
4615 -- Put the projects in decreasing depth order, so that
4616 -- if libA depends on libB, libB is first in order.
4618 Current := Library_Projs.Last;
4619 while Current > 1 loop
4620 Prj := Library_Projs.Table (Current - 1);
4621 exit when Prj.Depth >= Depth;
4622 Library_Projs.Table (Current) := Prj;
4623 Current := Current - 1;
4624 end loop;
4626 Library_Projs.Table (Current) := Proj;
4627 end Add_To_Library_Projs;
4629 -- Start of processing for Library_Phase
4631 begin
4632 Library_Projs.Init;
4634 -- Put in Library_Projs table all library project file ids when the
4635 -- library need to be rebuilt.
4637 Proj1 := Project_Tree.Projects;
4638 while Proj1 /= null loop
4639 if Proj1.Project.Extended_By = No_Project then
4640 if Proj1.Project.Standalone_Library /= No then
4641 Stand_Alone_Libraries := True;
4642 end if;
4644 if Proj1.Project.Library then
4645 MLib.Prj.Check_Library
4646 (Proj1.Project, Project_Tree);
4647 end if;
4649 if Proj1.Project.Need_To_Build_Lib then
4650 Add_To_Library_Projs (Proj1.Project);
4651 end if;
4652 end if;
4654 Proj1 := Proj1.Next;
4655 end loop;
4657 -- Check if importing libraries should be regenerated
4658 -- because at least an imported library will be
4659 -- regenerated or is more recent.
4661 Proj1 := Project_Tree.Projects;
4662 while Proj1 /= null loop
4663 if Proj1.Project.Library
4664 and then Proj1.Project.Extended_By = No_Project
4665 and then Proj1.Project.Library_Kind /= Static
4666 and then not Proj1.Project.Need_To_Build_Lib
4667 and then not Proj1.Project.Externally_Built
4668 then
4669 declare
4670 List : Project_List;
4671 Proj2 : Project_Id;
4672 Rebuild : Boolean := False;
4674 Lib_Timestamp1 : constant Time_Stamp_Type :=
4675 Proj1.Project.Library_TS;
4677 begin
4678 List := Proj1.Project.All_Imported_Projects;
4679 while List /= null loop
4680 Proj2 := List.Project;
4682 if Proj2.Library then
4683 if Proj2.Need_To_Build_Lib
4684 or else
4685 (Lib_Timestamp1 < Proj2.Library_TS)
4686 then
4687 Rebuild := True;
4688 exit;
4689 end if;
4690 end if;
4692 List := List.Next;
4693 end loop;
4695 if Rebuild then
4696 Proj1.Project.Need_To_Build_Lib := True;
4697 Add_To_Library_Projs (Proj1.Project);
4698 end if;
4699 end;
4700 end if;
4702 Proj1 := Proj1.Next;
4703 end loop;
4705 -- Reset the flags Need_To_Build_Lib for the next main, to avoid
4706 -- rebuilding libraries uselessly.
4708 Proj1 := Project_Tree.Projects;
4709 while Proj1 /= null loop
4710 Proj1.Project.Need_To_Build_Lib := False;
4711 Proj1 := Proj1.Next;
4712 end loop;
4714 -- Build the libraries, if any need to be built
4716 for J in 1 .. Library_Projs.Last loop
4717 Library_Rebuilt := True;
4719 -- If a library is rebuilt, then executables are obsolete
4721 Executable_Obsolete := True;
4723 MLib.Prj.Build_Library
4724 (For_Project => Library_Projs.Table (J),
4725 In_Tree => Project_Tree,
4726 Gnatbind => Gnatbind.all,
4727 Gnatbind_Path => Gnatbind_Path,
4728 Gcc => Gcc.all,
4729 Gcc_Path => Gcc_Path);
4730 end loop;
4731 end Library_Phase;
4733 -----------------------
4734 -- Compilation_Phase --
4735 -----------------------
4737 procedure Compilation_Phase
4738 (Main_Source_File : File_Name_Type;
4739 Current_Main_Index : Int := 0;
4740 Total_Compilation_Failures : in out Natural;
4741 Stand_Alone_Libraries : in out Boolean;
4742 Executable : File_Name_Type := No_File;
4743 Is_Last_Main : Boolean;
4744 Stop_Compile : out Boolean)
4746 Args : Argument_List (1 .. Gcc_Switches.Last);
4747 First_Compiled_File : File_Name_Type;
4748 Youngest_Obj_File : File_Name_Type;
4749 Youngest_Obj_Stamp : Time_Stamp_Type;
4751 Is_Main_Unit : Boolean;
4752 -- Set True by Compile_Sources if Main_Source_File can be a main unit
4754 Compilation_Failures : Natural;
4756 Executable_Stamp : Time_Stamp_Type;
4758 Library_Rebuilt : Boolean := False;
4760 begin
4761 Stop_Compile := False;
4763 for J in 1 .. Gcc_Switches.Last loop
4764 Args (J) := Gcc_Switches.Table (J);
4765 end loop;
4767 -- Now we invoke Compile_Sources for the current main
4769 Compile_Sources
4770 (Main_Source => Main_Source_File,
4771 Args => Args,
4772 First_Compiled_File => First_Compiled_File,
4773 Most_Recent_Obj_File => Youngest_Obj_File,
4774 Most_Recent_Obj_Stamp => Youngest_Obj_Stamp,
4775 Main_Unit => Is_Main_Unit,
4776 Main_Index => Current_Main_Index,
4777 Compilation_Failures => Compilation_Failures,
4778 Check_Readonly_Files => Check_Readonly_Files,
4779 Do_Not_Execute => Do_Not_Execute,
4780 Force_Compilations => Force_Compilations,
4781 In_Place_Mode => In_Place_Mode,
4782 Keep_Going => Keep_Going,
4783 Initialize_ALI_Data => True,
4784 Max_Process => Saved_Maximum_Processes);
4786 if Verbose_Mode then
4787 Write_Str ("End of compilation");
4788 Write_Eol;
4789 end if;
4791 Total_Compilation_Failures :=
4792 Total_Compilation_Failures + Compilation_Failures;
4794 if Total_Compilation_Failures /= 0 then
4795 Stop_Compile := True;
4796 return;
4797 end if;
4799 -- Regenerate libraries, if there are any and if object files have been
4800 -- regenerated. Note that we skip this in CodePeer mode because we don't
4801 -- need libraries in this case, and more importantly, the object files
4802 -- may not be present.
4804 if Main_Project /= No_Project
4805 and then not CodePeer_Mode
4806 and then MLib.Tgt.Support_For_Libraries /= Prj.None
4807 and then (Do_Bind_Step
4808 or Unique_Compile_All_Projects
4809 or not Compile_Only)
4810 and then (Do_Link_Step or Is_Last_Main)
4811 then
4812 Library_Phase
4813 (Stand_Alone_Libraries => Stand_Alone_Libraries,
4814 Library_Rebuilt => Library_Rebuilt);
4815 end if;
4817 if List_Dependencies then
4818 if First_Compiled_File /= No_File then
4819 Inform
4820 (First_Compiled_File,
4821 "must be recompiled. Can't generate dependence list.");
4822 else
4823 List_Depend;
4824 end if;
4826 elsif First_Compiled_File = No_File
4827 and then not Do_Bind_Step
4828 and then not Quiet_Output
4829 and then not Library_Rebuilt
4830 and then Osint.Number_Of_Files = 1
4831 then
4832 Inform (Msg => "objects up to date.");
4833 Stop_Compile := True;
4834 return;
4836 elsif Do_Not_Execute and then First_Compiled_File /= No_File then
4837 Write_Name (First_Compiled_File);
4838 Write_Eol;
4839 end if;
4841 -- Stop after compile step if any of:
4843 -- 1) -n (Do_Not_Execute) specified
4845 -- 2) -M (List_Dependencies) specified (also sets
4846 -- Do_Not_Execute above, so this is probably superfluous).
4848 -- 3) -c (Compile_Only) specified, but not -b (Bind_Only)
4850 -- 4) Made unit cannot be a main unit
4852 if ((Do_Not_Execute
4853 or List_Dependencies
4854 or not Do_Bind_Step
4855 or not Is_Main_Unit)
4856 and not No_Main_Subprogram
4857 and not Build_Bind_And_Link_Full_Project)
4858 or Unique_Compile
4859 then
4860 Stop_Compile := True;
4861 return;
4862 end if;
4864 -- If the objects were up-to-date check if the executable file is also
4865 -- up-to-date. For now always bind and link on the JVM since there is
4866 -- currently no simple way to check whether objects are up to date wrt
4867 -- the executable. Same in CodePeer mode where there is no executable.
4869 if Targparm.VM_Target /= JVM_Target
4870 and then not CodePeer_Mode
4871 and then First_Compiled_File = No_File
4872 then
4873 Executable_Stamp := File_Stamp (Executable);
4875 if not Executable_Obsolete then
4876 Executable_Obsolete := Youngest_Obj_Stamp > Executable_Stamp;
4877 end if;
4879 if not Executable_Obsolete then
4880 for Index in reverse 1 .. Dependencies.Last loop
4881 if Is_In_Obsoleted (Dependencies.Table (Index).Depends_On) then
4882 Enter_Into_Obsoleted (Dependencies.Table (Index).This);
4883 end if;
4884 end loop;
4886 Executable_Obsolete := Is_In_Obsoleted (Main_Source_File);
4887 Dependencies.Init;
4888 end if;
4890 if not Executable_Obsolete then
4892 -- If no Ada object files obsolete the executable, check
4893 -- for younger or missing linker files.
4895 Check_Linker_Options
4896 (Executable_Stamp,
4897 Youngest_Obj_File,
4898 Youngest_Obj_Stamp);
4900 Executable_Obsolete := Youngest_Obj_File /= No_File;
4901 end if;
4903 -- Check if any library file is more recent than the
4904 -- executable: there may be an externally built library
4905 -- file that has been modified.
4907 if not Executable_Obsolete and then Main_Project /= No_Project then
4908 declare
4909 Proj1 : Project_List;
4911 begin
4912 Proj1 := Project_Tree.Projects;
4913 while Proj1 /= null loop
4914 if Proj1.Project.Library
4915 and then Proj1.Project.Library_TS > Executable_Stamp
4916 then
4917 Executable_Obsolete := True;
4918 Youngest_Obj_Stamp := Proj1.Project.Library_TS;
4919 Name_Len := 0;
4920 Add_Str_To_Name_Buffer ("library ");
4921 Add_Str_To_Name_Buffer
4922 (Get_Name_String (Proj1.Project.Library_Name));
4923 Youngest_Obj_File := Name_Find;
4924 exit;
4925 end if;
4927 Proj1 := Proj1.Next;
4928 end loop;
4929 end;
4930 end if;
4932 -- Return if the executable is up to date and otherwise
4933 -- motivate the relink/rebind.
4935 if not Executable_Obsolete then
4936 if not Quiet_Output then
4937 Inform (Executable, "up to date.");
4938 end if;
4940 Stop_Compile := True;
4941 return;
4942 end if;
4944 if Executable_Stamp (1) = ' ' then
4945 if not No_Main_Subprogram then
4946 Verbose_Msg (Executable, "missing.", Prefix => " ");
4947 end if;
4949 elsif Youngest_Obj_Stamp (1) = ' ' then
4950 Verbose_Msg
4951 (Youngest_Obj_File, "missing.", Prefix => " ");
4953 elsif Youngest_Obj_Stamp > Executable_Stamp then
4954 Verbose_Msg
4955 (Youngest_Obj_File,
4956 "(" & String (Youngest_Obj_Stamp) & ") newer than",
4957 Executable,
4958 "(" & String (Executable_Stamp) & ")");
4960 else
4961 Verbose_Msg
4962 (Executable, "needs to be rebuilt", Prefix => " ");
4964 end if;
4965 end if;
4966 end Compilation_Phase;
4968 ----------------------------------------
4969 -- Resolve_Relative_Names_In_Switches --
4970 ----------------------------------------
4972 procedure Resolve_Relative_Names_In_Switches (Current_Work_Dir : String) is
4973 begin
4974 -- If a relative path output file has been specified, we add the
4975 -- exec directory.
4977 for J in reverse 1 .. Saved_Linker_Switches.Last - 1 loop
4978 if Saved_Linker_Switches.Table (J).all = Output_Flag.all then
4979 declare
4980 Exec_File_Name : constant String :=
4981 Saved_Linker_Switches.Table (J + 1).all;
4983 begin
4984 if not Is_Absolute_Path (Exec_File_Name) then
4985 Get_Name_String (Main_Project.Exec_Directory.Display_Name);
4986 Add_Str_To_Name_Buffer (Exec_File_Name);
4987 Saved_Linker_Switches.Table (J + 1) :=
4988 new String'(Name_Buffer (1 .. Name_Len));
4989 end if;
4990 end;
4992 exit;
4993 end if;
4994 end loop;
4996 -- If we are using a project file, for relative paths we add the
4997 -- current working directory for any relative path on the command
4998 -- line and the project directory, for any relative path in the
4999 -- project file.
5001 declare
5002 Dir_Path : constant String :=
5003 Get_Name_String (Main_Project.Directory.Display_Name);
5004 begin
5005 for J in 1 .. Binder_Switches.Last loop
5006 Ensure_Absolute_Path
5007 (Binder_Switches.Table (J),
5008 Do_Fail => Make_Failed'Access,
5009 Parent => Dir_Path, For_Gnatbind => True);
5010 end loop;
5012 for J in 1 .. Saved_Binder_Switches.Last loop
5013 Ensure_Absolute_Path
5014 (Saved_Binder_Switches.Table (J),
5015 Do_Fail => Make_Failed'Access,
5016 Parent => Current_Work_Dir,
5017 For_Gnatbind => True);
5018 end loop;
5020 for J in 1 .. Linker_Switches.Last loop
5021 Ensure_Absolute_Path
5022 (Linker_Switches.Table (J),
5023 Parent => Dir_Path,
5024 Do_Fail => Make_Failed'Access);
5025 end loop;
5027 for J in 1 .. Saved_Linker_Switches.Last loop
5028 Ensure_Absolute_Path
5029 (Saved_Linker_Switches.Table (J),
5030 Do_Fail => Make_Failed'Access,
5031 Parent => Current_Work_Dir);
5032 end loop;
5034 for J in 1 .. Gcc_Switches.Last loop
5035 Ensure_Absolute_Path
5036 (Gcc_Switches.Table (J),
5037 Do_Fail => Make_Failed'Access,
5038 Parent => Dir_Path,
5039 Including_Non_Switch => False);
5040 end loop;
5042 for J in 1 .. Saved_Gcc_Switches.Last loop
5043 Ensure_Absolute_Path
5044 (Saved_Gcc_Switches.Table (J),
5045 Parent => Current_Work_Dir,
5046 Do_Fail => Make_Failed'Access,
5047 Including_Non_Switch => False);
5048 end loop;
5049 end;
5050 end Resolve_Relative_Names_In_Switches;
5052 -----------------------------------
5053 -- Queue_Library_Project_Sources --
5054 -----------------------------------
5056 procedure Queue_Library_Project_Sources is
5057 begin
5058 if not Unique_Compile
5059 and then MLib.Tgt.Support_For_Libraries /= Prj.None
5060 then
5061 declare
5062 Proj : Project_List;
5064 begin
5065 Proj := Project_Tree.Projects;
5066 while Proj /= null loop
5067 if Proj.Project.Library then
5068 Proj.Project.Need_To_Build_Lib :=
5069 not MLib.Tgt.Library_Exists_For
5070 (Proj.Project, Project_Tree)
5071 and then not Proj.Project.Externally_Built;
5073 if Proj.Project.Need_To_Build_Lib then
5075 -- If there is no object directory, then it will be
5076 -- impossible to build the library, so fail immediately.
5078 if Proj.Project.Object_Directory = No_Path_Information
5079 then
5080 Make_Failed
5081 ("no object files to build library for"
5082 & " project """
5083 & Get_Name_String (Proj.Project.Name)
5084 & """");
5085 Proj.Project.Need_To_Build_Lib := False;
5087 else
5088 if Verbose_Mode then
5089 Write_Str
5090 ("Library file does not exist for "
5091 & "project """);
5092 Write_Str
5093 (Get_Name_String (Proj.Project.Name));
5094 Write_Line ("""");
5095 end if;
5097 Insert_Project_Sources
5098 (The_Project => Proj.Project,
5099 All_Projects => False,
5100 Into_Q => True);
5101 end if;
5102 end if;
5103 end if;
5105 Proj := Proj.Next;
5106 end loop;
5107 end;
5108 end if;
5109 end Queue_Library_Project_Sources;
5111 ------------------------
5112 -- Compute_Executable --
5113 ------------------------
5115 procedure Compute_Executable
5116 (Main_Source_File : File_Name_Type;
5117 Executable : out File_Name_Type;
5118 Non_Std_Executable : out Boolean)
5120 begin
5121 Executable := No_File;
5122 Non_Std_Executable :=
5123 Targparm.Executable_Extension_On_Target /= No_Name;
5125 -- Look inside the linker switches to see if the name of the final
5126 -- executable program was specified.
5128 for J in reverse Linker_Switches.First .. Linker_Switches.Last loop
5129 if Linker_Switches.Table (J).all = Output_Flag.all then
5130 pragma Assert (J < Linker_Switches.Last);
5132 -- We cannot specify a single executable for several main
5133 -- subprograms
5135 if Osint.Number_Of_Files > 1 then
5136 Fail ("cannot specify a single executable for several mains");
5137 end if;
5139 Name_Len := 0;
5140 Add_Str_To_Name_Buffer (Linker_Switches.Table (J + 1).all);
5141 Executable := Name_Enter;
5143 Verbose_Msg (Executable, "final executable");
5144 end if;
5145 end loop;
5147 -- If the name of the final executable program was not specified then
5148 -- construct it from the main input file.
5150 if Executable = No_File then
5151 if Main_Project = No_Project then
5152 Executable := Executable_Name (Strip_Suffix (Main_Source_File));
5154 else
5155 -- If we are using a project file, we attempt to remove the body
5156 -- (or spec) termination of the main subprogram. We find it the
5157 -- naming scheme of the project file. This avoids generating an
5158 -- executable "main.2" for a main subprogram "main.2.ada", when
5159 -- the body termination is ".2.ada".
5161 Executable :=
5162 Prj.Util.Executable_Of
5163 (Main_Project, Project_Tree.Shared,
5164 Main_Source_File, Main_Index);
5165 end if;
5166 end if;
5168 if Main_Project /= No_Project
5169 and then Main_Project.Exec_Directory /= No_Path_Information
5170 then
5171 declare
5172 Exec_File_Name : constant String := Get_Name_String (Executable);
5173 begin
5174 if not Is_Absolute_Path (Exec_File_Name) then
5175 Get_Name_String (Main_Project.Exec_Directory.Display_Name);
5176 Add_Str_To_Name_Buffer (Exec_File_Name);
5177 Executable := Name_Find;
5178 end if;
5180 Non_Std_Executable := True;
5181 end;
5182 end if;
5183 end Compute_Executable;
5185 -------------------------------
5186 -- Compute_Switches_For_Main --
5187 -------------------------------
5189 procedure Compute_Switches_For_Main
5190 (Main_Source_File : in out File_Name_Type;
5191 Root_Environment : in out Prj.Tree.Environment;
5192 Compute_Builder : Boolean;
5193 Current_Work_Dir : String)
5195 function Add_Global_Switches
5196 (Switch : String;
5197 For_Lang : Name_Id;
5198 For_Builder : Boolean;
5199 Has_Global_Compilation_Switches : Boolean) return Boolean;
5200 -- Handles builder and global compilation switches, as read from the
5201 -- project file.
5203 -------------------------
5204 -- Add_Global_Switches --
5205 -------------------------
5207 function Add_Global_Switches
5208 (Switch : String;
5209 For_Lang : Name_Id;
5210 For_Builder : Boolean;
5211 Has_Global_Compilation_Switches : Boolean) return Boolean
5213 pragma Unreferenced (For_Lang);
5215 begin
5216 if For_Builder then
5217 Program_Args := None;
5218 Switch_May_Be_Passed_To_The_Compiler :=
5219 not Has_Global_Compilation_Switches;
5220 Scan_Make_Arg (Root_Environment, Switch, And_Save => False);
5222 return Gnatmake_Switch_Found
5223 or else Switch_May_Be_Passed_To_The_Compiler;
5224 else
5225 Add_Switch (Switch, Compiler, And_Save => False);
5226 return True;
5227 end if;
5228 end Add_Global_Switches;
5230 procedure Do_Compute_Builder_Switches
5231 is new Makeutl.Compute_Builder_Switches (Add_Global_Switches);
5233 -- Start of processing for Compute_Switches_For_Main
5235 begin
5236 if Main_Project /= No_Project then
5237 declare
5238 Main_Source_File_Name : constant String :=
5239 Get_Name_String (Main_Source_File);
5241 Main_Unit_File_Name : constant String :=
5242 Prj.Env.File_Name_Of_Library_Unit_Body
5243 (Name => Main_Source_File_Name,
5244 Project => Main_Project,
5245 In_Tree => Project_Tree,
5246 Main_Project_Only => not Unique_Compile);
5248 The_Packages : constant Package_Id := Main_Project.Decl.Packages;
5250 Binder_Package : constant Prj.Package_Id :=
5251 Prj.Util.Value_Of
5252 (Name => Name_Binder,
5253 In_Packages => The_Packages,
5254 Shared => Project_Tree.Shared);
5256 Linker_Package : constant Prj.Package_Id :=
5257 Prj.Util.Value_Of
5258 (Name => Name_Linker,
5259 In_Packages => The_Packages,
5260 Shared => Project_Tree.Shared);
5262 begin
5263 -- We fail if we cannot find the main source file
5265 if Main_Unit_File_Name = "" then
5266 Make_Failed ('"' & Main_Source_File_Name
5267 & """ is not a unit of project "
5268 & Project_File_Name.all & ".");
5269 end if;
5271 -- Remove any directory information from the main source file
5272 -- file name.
5274 declare
5275 Pos : Natural := Main_Unit_File_Name'Last;
5277 begin
5278 loop
5279 exit when Pos < Main_Unit_File_Name'First
5280 or else Main_Unit_File_Name (Pos) = Directory_Separator;
5281 Pos := Pos - 1;
5282 end loop;
5284 Name_Len := Main_Unit_File_Name'Last - Pos;
5286 Name_Buffer (1 .. Name_Len) :=
5287 Main_Unit_File_Name (Pos + 1 .. Main_Unit_File_Name'Last);
5289 Main_Source_File := Name_Find;
5291 -- We only output the main source file if there is only one
5293 if Verbose_Mode and then Osint.Number_Of_Files = 1 then
5294 Write_Str ("Main source file: """);
5295 Write_Str (Main_Unit_File_Name
5296 (Pos + 1 .. Main_Unit_File_Name'Last));
5297 Write_Line (""".");
5298 end if;
5299 end;
5301 if Compute_Builder then
5302 Do_Compute_Builder_Switches
5303 (Project_Tree => Project_Tree,
5304 Env => Root_Environment,
5305 Main_Project => Main_Project,
5306 Only_For_Lang => Name_Ada);
5308 Resolve_Relative_Names_In_Switches
5309 (Current_Work_Dir => Current_Work_Dir);
5311 -- Record current last switch index for tables Binder_Switches
5312 -- and Linker_Switches, so that these tables may be reset
5313 -- before each main, before adding switches from the project
5314 -- file and from the command line.
5316 Last_Binder_Switch := Binder_Switches.Last;
5317 Last_Linker_Switch := Linker_Switches.Last;
5319 else
5320 -- Reset the tables Binder_Switches and Linker_Switches
5322 Binder_Switches.Set_Last (Last_Binder_Switch);
5323 Linker_Switches.Set_Last (Last_Linker_Switch);
5324 end if;
5326 -- We now deal with the binder and linker switches. If no project
5327 -- file is used, there is nothing to do because the binder and
5328 -- linker switches are the same for all mains.
5330 -- Add binder switches from the project file for the first main
5332 if Do_Bind_Step and then Binder_Package /= No_Package then
5333 if Verbose_Mode then
5334 Write_Str ("Adding binder switches for """);
5335 Write_Str (Main_Unit_File_Name);
5336 Write_Line (""".");
5337 end if;
5339 Add_Switches
5340 (Env => Root_Environment,
5341 File_Name => Main_Unit_File_Name,
5342 The_Package => Binder_Package,
5343 Program => Binder);
5344 end if;
5346 -- Add linker switches from the project file for the first main
5348 if Do_Link_Step and then Linker_Package /= No_Package then
5349 if Verbose_Mode then
5350 Write_Str ("Adding linker switches for""");
5351 Write_Str (Main_Unit_File_Name);
5352 Write_Line (""".");
5353 end if;
5355 Add_Switches
5356 (Env => Root_Environment,
5357 File_Name => Main_Unit_File_Name,
5358 The_Package => Linker_Package,
5359 Program => Linker);
5360 end if;
5362 -- As we are using a project file, for relative paths we add the
5363 -- current working directory for any relative path on the command
5364 -- line and the project directory, for any relative path in the
5365 -- project file.
5367 declare
5368 Dir_Path : constant String :=
5369 Get_Name_String (Main_Project.Directory.Display_Name);
5371 begin
5372 for J in Last_Binder_Switch + 1 .. Binder_Switches.Last loop
5373 Ensure_Absolute_Path
5374 (Binder_Switches.Table (J),
5375 Do_Fail => Make_Failed'Access,
5376 Parent => Dir_Path, For_Gnatbind => True);
5377 end loop;
5379 for J in Last_Linker_Switch + 1 .. Linker_Switches.Last loop
5380 Ensure_Absolute_Path
5381 (Linker_Switches.Table (J),
5382 Parent => Dir_Path,
5383 Do_Fail => Make_Failed'Access);
5384 end loop;
5385 end;
5386 end;
5388 else
5389 if not Compute_Builder then
5391 -- Reset the tables Binder_Switches and Linker_Switches
5393 Binder_Switches.Set_Last (Last_Binder_Switch);
5394 Linker_Switches.Set_Last (Last_Linker_Switch);
5395 end if;
5396 end if;
5398 Check_Steps;
5400 if Compute_Builder then
5401 Display_Commands (not Quiet_Output);
5402 end if;
5404 -- We now put in the Binder_Switches and Linker_Switches tables, the
5405 -- binder and linker switches of the command line that have been put in
5406 -- the Saved_ tables. If a project file was used, then the command line
5407 -- switches will follow the project file switches.
5409 for J in 1 .. Saved_Binder_Switches.Last loop
5410 Add_Switch
5411 (Saved_Binder_Switches.Table (J),
5412 Binder,
5413 And_Save => False);
5414 end loop;
5416 for J in 1 .. Saved_Linker_Switches.Last loop
5417 Add_Switch
5418 (Saved_Linker_Switches.Table (J),
5419 Linker,
5420 And_Save => False);
5421 end loop;
5422 end Compute_Switches_For_Main;
5424 --------------
5425 -- Gnatmake --
5426 --------------
5428 procedure Gnatmake is
5429 Main_Source_File : File_Name_Type;
5430 -- The source file containing the main compilation unit
5432 Total_Compilation_Failures : Natural := 0;
5434 Main_ALI_File : File_Name_Type;
5435 -- The ali file corresponding to Main_Source_File
5437 Executable : File_Name_Type := No_File;
5438 -- The file name of an executable
5440 Non_Std_Executable : Boolean := False;
5441 -- Non_Std_Executable is set to True when there is a possibility that
5442 -- the linker will not choose the correct executable file name.
5444 Current_Work_Dir : constant String_Access :=
5445 new String'(Get_Current_Dir);
5446 -- The current working directory, used to modify some relative path
5447 -- switches on the command line when a project file is used.
5449 Current_Main_Index : Int := 0;
5450 -- If not zero, the index of the current main unit in its source file
5452 Is_First_Main : Boolean;
5453 -- Whether we are processing the first main
5455 Stand_Alone_Libraries : Boolean := False;
5456 -- Set to True when there are Stand-Alone Libraries, so that gnatbind
5457 -- is invoked with the -F switch to force checking of elaboration flags.
5459 Project_Node_Tree : Project_Node_Tree_Ref;
5461 Stop_Compile : Boolean;
5463 Discard : Boolean;
5464 pragma Warnings (Off, Discard);
5466 procedure Check_Mains;
5467 -- Check that the main subprograms do exist and that they all
5468 -- belong to the same project file.
5470 -----------------
5471 -- Check_Mains --
5472 -----------------
5474 procedure Check_Mains is
5475 Real_Main_Project : Project_Id := No_Project;
5476 Info : Main_Info;
5477 Proj : Project_Id;
5479 begin
5480 if Mains.Number_Of_Mains (Project_Tree) = 0
5481 and then not Unique_Compile
5482 then
5483 Mains.Fill_From_Project (Main_Project, Project_Tree);
5484 end if;
5486 Mains.Complete_Mains
5487 (Root_Environment.Flags, Main_Project, Project_Tree);
5489 -- If we have multiple mains on the command line, they need not
5490 -- belong to the root project, but they must all belong to the same
5491 -- project.
5493 if not Unique_Compile then
5494 Mains.Reset;
5495 loop
5496 Info := Mains.Next_Main;
5497 exit when Info = No_Main_Info;
5499 Proj := Ultimate_Extending_Project_Of (Info.Project);
5501 if Real_Main_Project = No_Project then
5502 Real_Main_Project := Proj;
5503 elsif Real_Main_Project /= Proj then
5504 Make_Failed
5505 ("""" & Get_Name_String (Info.File) &
5506 """ is not a source of project " &
5507 Get_Name_String (Real_Main_Project.Name));
5508 end if;
5509 end loop;
5511 if Real_Main_Project /= No_Project then
5512 Main_Project := Real_Main_Project;
5513 end if;
5515 Debug_Output ("After checking mains, main project is",
5516 Main_Project.Name);
5518 else
5519 -- For all mains on the command line, make sure they were in
5520 -- osint. In particular, if the user has specified a multi-unit
5521 -- source file, the call to Complete_Mains will have expanded
5522 -- the list of mains to all its units, and we must now put them
5523 -- back on the command line.
5524 -- ??? This will not be necessary when gnatmake shares the same
5525 -- queue as gprbuild and processes the file directly on the queue.
5527 Mains.Reset;
5528 loop
5529 Info := Mains.Next_Main;
5530 exit when Info = No_Main_Info;
5532 if Info.Index /= 0 then
5533 Debug_Output ("Add to command line index="
5534 & Info.Index'Img, Name_Id (Info.File));
5535 Osint.Add_File (Get_Name_String (Info.File), Info.Index);
5536 end if;
5537 end loop;
5538 end if;
5539 end Check_Mains;
5541 -- Start of processing for Gnatmake
5543 -- This body is very long, should be broken down???
5545 begin
5546 Install_Int_Handler (Sigint_Intercepted'Access);
5548 Do_Compile_Step := True;
5549 Do_Bind_Step := True;
5550 Do_Link_Step := True;
5552 Obsoleted.Reset;
5554 Make.Initialize (Project_Node_Tree, Root_Environment);
5556 Bind_Shared := No_Shared_Switch'Access;
5557 Link_With_Shared_Libgcc := No_Shared_Libgcc_Switch'Access;
5559 Failed_Links.Set_Last (0);
5560 Successful_Links.Set_Last (0);
5562 -- Special case when switch -B was specified
5564 if Build_Bind_And_Link_Full_Project then
5566 -- When switch -B is specified, there must be a project file
5568 if Main_Project = No_Project then
5569 Make_Failed ("-B cannot be used without a project file");
5571 -- No main program may be specified on the command line
5573 elsif Osint.Number_Of_Files /= 0 then
5574 Make_Failed
5575 ("-B cannot be used with a main specified on the command line");
5577 -- And the project file cannot be a library project file
5579 elsif Main_Project.Library then
5580 Make_Failed ("-B cannot be used for a library project file");
5582 else
5583 No_Main_Subprogram := True;
5584 Insert_Project_Sources
5585 (The_Project => Main_Project,
5586 All_Projects => Unique_Compile_All_Projects,
5587 Into_Q => False);
5589 -- If there are no sources to compile, we fail
5591 if Osint.Number_Of_Files = 0 then
5592 Make_Failed ("no sources to compile");
5593 end if;
5595 -- Specify -n for gnatbind and add the ALI files of all the
5596 -- sources, except the one which is a fake main subprogram: this
5597 -- is the one for the binder generated file and it will be
5598 -- transmitted to gnatlink. These sources are those that are in
5599 -- the queue.
5601 Add_Switch ("-n", Binder, And_Save => True);
5603 for J in 1 .. Queue.Size loop
5604 Add_Switch
5605 (Get_Name_String (Lib_File_Name (Queue.Element (J))),
5606 Binder, And_Save => True);
5607 end loop;
5608 end if;
5610 elsif Main_Index /= 0 and then Osint.Number_Of_Files > 1 then
5611 Make_Failed ("cannot specify several mains with a multi-unit index");
5613 elsif Main_Project /= No_Project then
5615 -- If the main project file is a library project file, main(s) cannot
5616 -- be specified on the command line.
5618 if Osint.Number_Of_Files /= 0 then
5619 if Main_Project.Library
5620 and then not Unique_Compile
5621 and then ((not Make_Steps) or else Bind_Only or else Link_Only)
5622 then
5623 Make_Failed
5624 ("cannot specify a main program "
5625 & "on the command line for a library project file");
5626 end if;
5628 -- If no mains have been specified on the command line, and we are
5629 -- using a project file, we either find the main(s) in attribute Main
5630 -- of the main project, or we put all the sources of the project file
5631 -- as mains.
5633 else
5634 if Main_Index /= 0 then
5635 Make_Failed ("cannot specify a multi-unit index but no main "
5636 & "on the command line");
5637 end if;
5639 declare
5640 Value : String_List_Id := Main_Project.Mains;
5642 begin
5643 -- The attribute Main is an empty list or not specified, or
5644 -- else gnatmake was invoked with the switch "-u".
5646 if Value = Prj.Nil_String or else Unique_Compile then
5647 if not Make_Steps
5648 or Compile_Only
5649 or not Main_Project.Library
5650 then
5651 -- First make sure that the binder and the linker will
5652 -- not be invoked.
5654 Do_Bind_Step := False;
5655 Do_Link_Step := False;
5657 -- Put all the sources in the queue
5659 No_Main_Subprogram := True;
5660 Insert_Project_Sources
5661 (The_Project => Main_Project,
5662 All_Projects => Unique_Compile_All_Projects,
5663 Into_Q => False);
5665 -- If no sources to compile, then there is nothing to do
5667 if Osint.Number_Of_Files = 0 then
5668 if not Quiet_Output then
5669 Osint.Write_Program_Name;
5670 Write_Line (": no sources to compile");
5671 end if;
5673 Finish_Program (Project_Tree, E_Success);
5674 end if;
5675 end if;
5677 else
5678 -- The attribute Main is not an empty list. Put all the main
5679 -- subprograms in the list as if they were specified on the
5680 -- command line. However, if attribute Languages includes a
5681 -- language other than Ada, only include the Ada mains; if
5682 -- there is no Ada main, compile all sources of the project.
5684 declare
5685 Languages : constant Variable_Value :=
5686 Prj.Util.Value_Of
5687 (Name_Languages,
5688 Main_Project.Decl.Attributes,
5689 Project_Tree.Shared);
5691 Current : String_List_Id;
5692 Element : String_Element;
5694 Foreign_Language : Boolean := False;
5695 At_Least_One_Main : Boolean := False;
5697 begin
5698 -- First, determine if there is a foreign language in
5699 -- attribute Languages.
5701 if not Languages.Default then
5702 Current := Languages.Values;
5703 Look_For_Foreign :
5704 while Current /= Nil_String loop
5705 Element := Project_Tree.Shared.String_Elements.
5706 Table (Current);
5707 Get_Name_String (Element.Value);
5708 To_Lower (Name_Buffer (1 .. Name_Len));
5710 if Name_Buffer (1 .. Name_Len) /= "ada" then
5711 Foreign_Language := True;
5712 exit Look_For_Foreign;
5713 end if;
5715 Current := Element.Next;
5716 end loop Look_For_Foreign;
5717 end if;
5719 -- Then, find all mains, or if there is a foreign
5720 -- language, all the Ada mains.
5722 while Value /= Prj.Nil_String loop
5723 -- To know if a main is an Ada main, get its project.
5724 -- It should be the project specified on the command
5725 -- line.
5727 Get_Name_String
5728 (Project_Tree.Shared.String_Elements.Table
5729 (Value).Value);
5731 declare
5732 Main_Name : constant String :=
5733 Get_Name_String
5734 (Project_Tree.Shared.
5735 String_Elements.
5736 Table (Value).Value);
5738 Proj : constant Project_Id :=
5739 Prj.Env.Project_Of
5740 (Main_Name, Main_Project, Project_Tree);
5742 begin
5743 if Proj = Main_Project then
5744 At_Least_One_Main := True;
5745 Osint.Add_File
5746 (Get_Name_String
5747 (Project_Tree.Shared.String_Elements.Table
5748 (Value).Value),
5749 Index =>
5750 Project_Tree.Shared.String_Elements.Table
5751 (Value).Index);
5753 elsif not Foreign_Language then
5754 Make_Failed
5755 ("""" & Main_Name &
5756 """ is not a source of project " &
5757 Get_Name_String (Main_Project.Display_Name));
5758 end if;
5759 end;
5761 Value := Project_Tree.Shared.String_Elements.Table
5762 (Value).Next;
5763 end loop;
5765 -- If we did not get any main, it means that all mains
5766 -- in attribute Mains are in a foreign language and -B
5767 -- was not specified to gnatmake; so, we fail.
5769 if not At_Least_One_Main then
5770 Make_Failed
5771 ("no Ada mains, use -B to build foreign main");
5772 end if;
5773 end;
5775 end if;
5776 end;
5777 end if;
5779 -- Check that each main on the command line is a source of a
5780 -- project file and, if there are several mains, each of them
5781 -- is a source of the same project file.
5783 Check_Mains;
5784 end if;
5786 if Verbose_Mode then
5787 Write_Eol;
5788 Display_Version ("GNATMAKE", "1992");
5789 end if;
5791 if Osint.Number_Of_Files = 0 then
5792 if Main_Project /= No_Project and then Main_Project.Library then
5793 if Do_Bind_Step and then Main_Project.Standalone_Library = No then
5794 Make_Failed ("only stand-alone libraries may be bound");
5795 end if;
5797 -- Add the default search directories to be able to find libgnat
5799 Osint.Add_Default_Search_Dirs;
5801 -- And bind and or link the library
5803 MLib.Prj.Build_Library
5804 (For_Project => Main_Project,
5805 In_Tree => Project_Tree,
5806 Gnatbind => Gnatbind.all,
5807 Gnatbind_Path => Gnatbind_Path,
5808 Gcc => Gcc.all,
5809 Gcc_Path => Gcc_Path,
5810 Bind => Bind_Only,
5811 Link => Link_Only);
5813 Finish_Program (Project_Tree, E_Success);
5815 else
5816 -- Call Get_Target_Parameters to ensure that VM_Target and
5817 -- AAMP_On_Target get set before calling Usage.
5819 Targparm.Get_Target_Parameters;
5821 -- Output usage information if no argument on the command line
5823 if Argument_Count = 0 then
5824 Usage;
5825 else
5826 Try_Help;
5827 end if;
5829 Finish_Program (Project_Tree, E_Success);
5830 end if;
5831 end if;
5833 -- Get the first executable.
5834 -- ??? This needs to be done early, because Osint.Next_Main_File also
5835 -- initializes the primary search directory, used below to initialize
5836 -- the "-I" parameter
5838 Main_Source_File := Next_Main_Source; -- No directory information
5840 -- If -M was specified, behave as if -n was specified
5842 if List_Dependencies then
5843 Do_Not_Execute := True;
5844 end if;
5846 Add_Switch ("-I-", Compiler, And_Save => True);
5848 if Main_Project = No_Project then
5849 if Look_In_Primary_Dir then
5850 Add_Switch
5851 ("-I" &
5852 Normalize_Directory_Name
5853 (Get_Primary_Src_Search_Directory.all).all,
5854 Compiler,
5855 Append_Switch => False,
5856 And_Save => False);
5858 end if;
5860 else
5861 -- If we use a project file, we have already checked that a main
5862 -- specified on the command line with directory information has the
5863 -- path name corresponding to a correct source in the project tree.
5864 -- So, we don't need the directory information to be taken into
5865 -- account by Find_File, and in fact it may lead to take the wrong
5866 -- sources for other compilation units, when there are extending
5867 -- projects.
5869 Look_In_Primary_Dir := False;
5870 end if;
5872 -- If the user wants a program without a main subprogram, add the
5873 -- appropriate switch to the binder.
5875 if No_Main_Subprogram then
5876 Add_Switch ("-z", Binder, And_Save => True);
5877 end if;
5879 if Main_Project /= No_Project then
5881 if Main_Project.Object_Directory /= No_Path_Information then
5883 -- Change current directory to object directory of main project
5885 Project_Of_Current_Object_Directory := No_Project;
5886 Change_To_Object_Directory (Main_Project);
5887 end if;
5889 -- Source file lookups should be cached for efficiency. Source files
5890 -- are not supposed to change.
5892 Osint.Source_File_Data (Cache => True);
5894 Queue_Library_Project_Sources;
5895 end if;
5897 -- The combination of -f -u and one or several mains on the command line
5898 -- implies -a.
5900 if Force_Compilations
5901 and then Unique_Compile
5902 and then not Unique_Compile_All_Projects
5903 and then Main_On_Command_Line
5904 then
5905 Must_Compile := True;
5906 end if;
5908 if Main_Project /= No_Project
5909 and then not Must_Compile
5910 and then Main_Project.Externally_Built
5911 then
5912 Make_Failed
5913 ("nothing to do for a main project that is externally built");
5914 end if;
5916 -- If no project file is used, we just put the gcc switches
5917 -- from the command line in the Gcc_Switches table.
5919 if Main_Project = No_Project then
5920 for J in 1 .. Saved_Gcc_Switches.Last loop
5921 Add_Switch
5922 (Saved_Gcc_Switches.Table (J), Compiler, And_Save => False);
5923 end loop;
5925 else
5926 -- If there is a project, put the command line gcc switches in the
5927 -- variable The_Saved_Gcc_Switches. They are going to be used later
5928 -- in procedure Compile_Sources.
5930 The_Saved_Gcc_Switches :=
5931 new Argument_List (1 .. Saved_Gcc_Switches.Last + 1);
5933 for J in 1 .. Saved_Gcc_Switches.Last loop
5934 The_Saved_Gcc_Switches (J) := Saved_Gcc_Switches.Table (J);
5935 end loop;
5937 -- We never use gnat.adc when a project file is used
5939 The_Saved_Gcc_Switches (The_Saved_Gcc_Switches'Last) := No_gnat_adc;
5940 end if;
5942 -- If there was a --GCC, --GNATBIND or --GNATLINK switch on the command
5943 -- line, then we have to use it, even if there was another switch in
5944 -- the project file.
5946 if Saved_Gcc /= null then
5947 Gcc := Saved_Gcc;
5948 end if;
5950 if Saved_Gnatbind /= null then
5951 Gnatbind := Saved_Gnatbind;
5952 end if;
5954 if Saved_Gnatlink /= null then
5955 Gnatlink := Saved_Gnatlink;
5956 end if;
5958 Bad_Compilation.Init;
5960 -- If project files are used, create the mapping of all the sources, so
5961 -- that the correct paths will be found. Otherwise, if there is a file
5962 -- which is not a source with the same name in a source directory this
5963 -- file may be incorrectly found.
5965 if Main_Project /= No_Project then
5966 Prj.Env.Create_Mapping (Project_Tree);
5967 end if;
5969 -- Here is where the make process is started
5971 Queue.Initialize
5972 (Main_Project /= No_Project and then One_Compilation_Per_Obj_Dir);
5974 Is_First_Main := True;
5976 Multiple_Main_Loop : for N_File in 1 .. Osint.Number_Of_Files loop
5977 if Current_File_Index /= No_Index then
5978 Main_Index := Current_File_Index;
5979 end if;
5981 Current_Main_Index := Main_Index;
5983 if Current_Main_Index = 0
5984 and then Unique_Compile
5985 and then Main_Project /= No_Project
5986 then
5987 -- If this is a multi-unit source, do not compile it as is (ie
5988 -- without specifying which unit to compile)
5989 -- Insert_Project_Sources has added each of the unit separately.
5991 declare
5992 Source : constant Prj.Source_Id := Find_Source
5993 (In_Tree => Project_Tree,
5994 Project => Main_Project,
5995 Base_Name => Main_Source_File,
5996 Index => Current_Main_Index,
5997 In_Imported_Only => True);
5998 begin
5999 if Source /= No_Source and then Source.Index /= 0 then
6000 goto Next_Main;
6001 end if;
6002 end;
6003 end if;
6005 Compute_Switches_For_Main
6006 (Main_Source_File,
6007 Root_Environment,
6008 Compute_Builder => Is_First_Main,
6009 Current_Work_Dir => Current_Work_Dir.all);
6011 if Is_First_Main then
6013 -- Put the default source dirs in the source path only now, so
6014 -- that we take the correct ones in the case where --RTS= is
6015 -- specified in the Builder switches.
6017 Osint.Add_Default_Search_Dirs;
6019 -- Get the target parameters, which are only needed for a couple
6020 -- of cases in gnatmake. Protect against an exception, such as the
6021 -- case of system.ads missing from the library, and fail
6022 -- gracefully.
6024 begin
6025 Targparm.Get_Target_Parameters;
6026 exception
6027 when Unrecoverable_Error =>
6028 Make_Failed ("*** make failed.");
6029 end;
6031 -- Special processing for VM targets
6033 if Targparm.VM_Target /= No_VM then
6035 -- Set proper processing commands
6037 case Targparm.VM_Target is
6038 when Targparm.JVM_Target =>
6040 -- Do not check for an object file (".o") when compiling
6041 -- to JVM machine since ".class" files are generated
6042 -- instead.
6044 Check_Object_Consistency := False;
6046 -- Do not modify Gcc is --GCC= was specified
6048 if Gcc = Original_Gcc then
6049 Gcc := new String'("jvm-gnatcompile");
6050 end if;
6052 when Targparm.CLI_Target =>
6053 -- Do not modify Gcc is --GCC= was specified
6055 if Gcc = Original_Gcc then
6056 Gcc := new String'("dotnet-gnatcompile");
6057 end if;
6059 when Targparm.No_VM =>
6060 raise Program_Error;
6061 end case;
6062 end if;
6064 Gcc_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
6065 Gnatbind_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gnatbind.all);
6066 Gnatlink_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gnatlink.all);
6068 -- If we have specified -j switch both from the project file
6069 -- and on the command line, the one from the command line takes
6070 -- precedence.
6072 if Saved_Maximum_Processes = 0 then
6073 Saved_Maximum_Processes := Maximum_Processes;
6074 end if;
6076 if Debug.Debug_Flag_M then
6077 Write_Line ("Maximum number of simultaneous compilations =" &
6078 Saved_Maximum_Processes'Img);
6079 end if;
6081 -- Allocate as many temporary mapping file names as the maximum
6082 -- number of compilations processed, for each possible project.
6084 declare
6085 Data : Project_Compilation_Access;
6086 Proj : Project_List;
6088 begin
6089 Proj := Project_Tree.Projects;
6090 while Proj /= null loop
6091 Data :=
6092 new Project_Compilation_Data'
6093 (Mapping_File_Names =>
6094 new Temp_Path_Names (1 .. Saved_Maximum_Processes),
6095 Last_Mapping_File_Names => 0,
6096 Free_Mapping_File_Indexes =>
6097 new Free_File_Indexes (1 .. Saved_Maximum_Processes),
6098 Last_Free_Indexes => 0);
6100 Project_Compilation_Htable.Set
6101 (Project_Compilation, Proj.Project, Data);
6102 Proj := Proj.Next;
6103 end loop;
6105 Data :=
6106 new Project_Compilation_Data'
6107 (Mapping_File_Names =>
6108 new Temp_Path_Names (1 .. Saved_Maximum_Processes),
6109 Last_Mapping_File_Names => 0,
6110 Free_Mapping_File_Indexes =>
6111 new Free_File_Indexes (1 .. Saved_Maximum_Processes),
6112 Last_Free_Indexes => 0);
6114 Project_Compilation_Htable.Set
6115 (Project_Compilation, No_Project, Data);
6116 end;
6118 Is_First_Main := False;
6119 end if;
6121 Executable_Obsolete := False;
6123 Compute_Executable
6124 (Main_Source_File => Main_Source_File,
6125 Executable => Executable,
6126 Non_Std_Executable => Non_Std_Executable);
6128 if Do_Compile_Step then
6129 Compilation_Phase
6130 (Main_Source_File => Main_Source_File,
6131 Current_Main_Index => Current_Main_Index,
6132 Total_Compilation_Failures => Total_Compilation_Failures,
6133 Stand_Alone_Libraries => Stand_Alone_Libraries,
6134 Executable => Executable,
6135 Is_Last_Main => N_File = Osint.Number_Of_Files,
6136 Stop_Compile => Stop_Compile);
6138 if Stop_Compile then
6139 if Total_Compilation_Failures /= 0 then
6140 if Keep_Going then
6141 goto Next_Main;
6143 else
6144 List_Bad_Compilations;
6145 Report_Compilation_Failed;
6146 end if;
6148 elsif Osint.Number_Of_Files = 1 then
6149 exit Multiple_Main_Loop;
6150 else
6151 goto Next_Main;
6152 end if;
6153 end if;
6154 end if;
6156 -- For binding and linking, we need to be in the object directory of
6157 -- the main project.
6159 if Main_Project /= No_Project then
6160 Change_To_Object_Directory (Main_Project);
6161 end if;
6163 -- If we are here, it means that we need to rebuilt the current main,
6164 -- so we set Executable_Obsolete to True to make sure that subsequent
6165 -- mains will be rebuilt.
6167 Main_ALI_In_Place_Mode_Step : declare
6168 ALI_File : File_Name_Type;
6169 Src_File : File_Name_Type;
6171 begin
6172 Src_File := Strip_Directory (Main_Source_File);
6173 ALI_File := Lib_File_Name (Src_File, Current_Main_Index);
6174 Main_ALI_File := Full_Lib_File_Name (ALI_File);
6176 -- When In_Place_Mode, the library file can be located in the
6177 -- Main_Source_File directory which may not be present in the
6178 -- library path. If it is not present then use the corresponding
6179 -- library file name.
6181 if Main_ALI_File = No_File and then In_Place_Mode then
6182 Get_Name_String (Get_Directory (Full_Source_Name (Src_File)));
6183 Get_Name_String_And_Append (ALI_File);
6184 Main_ALI_File := Name_Find;
6185 Main_ALI_File := Full_Lib_File_Name (Main_ALI_File);
6186 end if;
6188 if Main_ALI_File = No_File then
6189 Make_Failed ("could not find the main ALI file");
6190 end if;
6191 end Main_ALI_In_Place_Mode_Step;
6193 if Do_Bind_Step then
6194 Binding_Phase
6195 (Stand_Alone_Libraries => Stand_Alone_Libraries,
6196 Main_ALI_File => Main_ALI_File);
6197 end if;
6199 if Do_Link_Step then
6200 Linking_Phase
6201 (Non_Std_Executable => Non_Std_Executable,
6202 Executable => Executable,
6203 Main_ALI_File => Main_ALI_File);
6204 end if;
6206 -- We go to here when we skip the bind and link steps
6208 <<Next_Main>>
6210 Queue.Remove_Marks;
6212 if N_File < Osint.Number_Of_Files then
6213 Main_Source_File := Next_Main_Source; -- No directory information
6214 end if;
6215 end loop Multiple_Main_Loop;
6217 if CodePeer_Mode then
6218 declare
6219 Success : Boolean := False;
6220 begin
6221 Globalize (Success);
6223 if not Success then
6224 Set_Standard_Error;
6225 Write_Str ("*** globalize failed.");
6227 if Commands_To_Stdout then
6228 Set_Standard_Output;
6229 end if;
6230 end if;
6231 end;
6232 end if;
6234 if Failed_Links.Last > 0 then
6235 for Index in 1 .. Successful_Links.Last loop
6236 Write_Str ("Linking of """);
6237 Write_Str (Get_Name_String (Successful_Links.Table (Index)));
6238 Write_Line (""" succeeded.");
6239 end loop;
6241 Set_Standard_Error;
6243 for Index in 1 .. Failed_Links.Last loop
6244 Write_Str ("Linking of """);
6245 Write_Str (Get_Name_String (Failed_Links.Table (Index)));
6246 Write_Line (""" failed.");
6247 end loop;
6249 if Commands_To_Stdout then
6250 Set_Standard_Output;
6251 end if;
6253 if Total_Compilation_Failures = 0 then
6254 Report_Compilation_Failed;
6255 end if;
6256 end if;
6258 if Total_Compilation_Failures /= 0 then
6259 List_Bad_Compilations;
6260 Report_Compilation_Failed;
6261 end if;
6263 Finish_Program (Project_Tree, E_Success);
6265 exception
6266 when X : others =>
6267 Set_Standard_Error;
6268 Write_Line (Exception_Information (X));
6269 Make_Failed ("INTERNAL ERROR. Please report.");
6270 end Gnatmake;
6272 ----------
6273 -- Hash --
6274 ----------
6276 function Hash (F : File_Name_Type) return Header_Num is
6277 begin
6278 return Header_Num (1 + F mod Max_Header);
6279 end Hash;
6281 --------------------
6282 -- In_Ada_Lib_Dir --
6283 --------------------
6285 function In_Ada_Lib_Dir (File : File_Name_Type) return Boolean is
6286 D : constant File_Name_Type := Get_Directory (File);
6287 B : constant Byte := Get_Name_Table_Byte (D);
6288 begin
6289 return (B and Ada_Lib_Dir) /= 0;
6290 end In_Ada_Lib_Dir;
6292 -----------------------
6293 -- Init_Mapping_File --
6294 -----------------------
6296 procedure Init_Mapping_File
6297 (Project : Project_Id;
6298 Data : in out Project_Compilation_Data;
6299 File_Index : in out Natural)
6301 FD : File_Descriptor;
6302 Status : Boolean;
6303 -- For call to Close
6305 begin
6306 -- Increase the index of the last mapping file for this project
6308 Data.Last_Mapping_File_Names := Data.Last_Mapping_File_Names + 1;
6310 -- If there is a project file, call Create_Mapping_File with
6311 -- the project id.
6313 if Project /= No_Project then
6314 Prj.Env.Create_Mapping_File
6315 (Project,
6316 In_Tree => Project_Tree,
6317 Language => Name_Ada,
6318 Name => Data.Mapping_File_Names
6319 (Data.Last_Mapping_File_Names));
6321 -- Otherwise, just create an empty file
6323 else
6324 Tempdir.Create_Temp_File
6325 (FD, Data.Mapping_File_Names (Data.Last_Mapping_File_Names));
6327 if FD = Invalid_FD then
6328 Make_Failed ("disk full");
6329 else
6330 Record_Temp_File
6331 (Project_Tree.Shared,
6332 Data.Mapping_File_Names (Data.Last_Mapping_File_Names));
6333 end if;
6335 Close (FD, Status);
6337 if not Status then
6338 Make_Failed ("disk full");
6339 end if;
6340 end if;
6342 -- And return the index of the newly created file
6344 File_Index := Data.Last_Mapping_File_Names;
6345 end Init_Mapping_File;
6347 ----------------
6348 -- Initialize --
6349 ----------------
6351 procedure Initialize
6352 (Project_Node_Tree : out Project_Node_Tree_Ref;
6353 Env : out Prj.Tree.Environment)
6355 procedure Check_Version_And_Help is
6356 new Check_Version_And_Help_G (Makeusg);
6358 -- Start of processing for Initialize
6360 begin
6361 -- Prepare the project's tree, since this is used to hold external
6362 -- references, project path and other attributes that can be impacted by
6363 -- the command line switches
6365 Prj.Tree.Initialize (Env, Gnatmake_Flags);
6367 Project_Node_Tree := new Project_Node_Tree_Data;
6368 Prj.Tree.Initialize (Project_Node_Tree);
6370 -- Override default initialization of Check_Object_Consistency since
6371 -- this is normally False for GNATBIND, but is True for GNATMAKE since
6372 -- we do not need to check source consistency again once GNATMAKE has
6373 -- looked at the sources to check.
6375 Check_Object_Consistency := True;
6377 -- Package initializations (the order of calls is important here)
6379 Output.Set_Standard_Error;
6381 Gcc_Switches.Init;
6382 Binder_Switches.Init;
6383 Linker_Switches.Init;
6385 Csets.Initialize;
6386 Snames.Initialize;
6387 Stringt.Initialize;
6389 Prj.Initialize (Project_Tree);
6391 Dependencies.Init;
6393 RTS_Specified := null;
6394 N_M_Switch := 0;
6396 Mains.Delete;
6398 -- Add the directory where gnatmake is invoked in front of the path,
6399 -- if gnatmake is invoked from a bin directory or with directory
6400 -- information.
6402 declare
6403 Prefix : constant String := Executable_Prefix_Path;
6404 Command : constant String := Command_Name;
6406 begin
6407 if Prefix'Length > 0 then
6408 declare
6409 PATH : constant String :=
6410 Prefix & Directory_Separator & "bin" & Path_Separator
6411 & Getenv ("PATH").all;
6412 begin
6413 Setenv ("PATH", PATH);
6414 end;
6416 else
6417 for Index in reverse Command'Range loop
6418 if Command (Index) = Directory_Separator then
6419 declare
6420 Absolute_Dir : constant String :=
6421 Normalize_Pathname
6422 (Command (Command'First .. Index));
6423 PATH : constant String :=
6424 Absolute_Dir &
6425 Path_Separator &
6426 Getenv ("PATH").all;
6427 begin
6428 Setenv ("PATH", PATH);
6429 end;
6431 exit;
6432 end if;
6433 end loop;
6434 end if;
6435 end;
6437 -- Scan the switches and arguments
6439 -- First, scan to detect --version and/or --help
6441 Check_Version_And_Help ("GNATMAKE", "1995");
6443 -- Scan again the switch and arguments, now that we are sure that they
6444 -- do not include --version or --help.
6446 Scan_Args : for Next_Arg in 1 .. Argument_Count loop
6447 Scan_Make_Arg (Env, Argument (Next_Arg), And_Save => True);
6448 end loop Scan_Args;
6450 if N_M_Switch > 0 and RTS_Specified = null then
6451 Process_Multilib (Env);
6452 end if;
6454 if Commands_To_Stdout then
6455 Set_Standard_Output;
6456 end if;
6458 if Usage_Requested then
6459 Usage;
6460 end if;
6462 -- Add the default project search directories now, after the directories
6463 -- that have been specified by switches -aP<dir>.
6465 Prj.Env.Initialize_Default_Project_Path
6466 (Env.Project_Path, Target_Name => Sdefault.Target_Name.all);
6468 -- Test for trailing -P switch
6470 if Project_File_Name_Present and then Project_File_Name = null then
6471 Make_Failed ("project file name missing after -P");
6473 -- Test for trailing -o switch
6475 elsif Output_File_Name_Present and then not Output_File_Name_Seen then
6476 Make_Failed ("output file name missing after -o");
6478 -- Test for trailing -D switch
6480 elsif Object_Directory_Present and then not Object_Directory_Seen then
6481 Make_Failed ("object directory missing after -D");
6482 end if;
6484 -- Test for simultaneity of -i and -D
6486 if Object_Directory_Path /= null and then In_Place_Mode then
6487 Make_Failed ("-i and -D cannot be used simultaneously");
6488 end if;
6490 -- If --subdirs= is specified, but not -P, this is equivalent to -D,
6491 -- except that the directory is created if it does not exist.
6493 if Prj.Subdirs /= null and then Project_File_Name = null then
6494 if Object_Directory_Path /= null then
6495 Make_Failed ("--subdirs and -D cannot be used simultaneously");
6497 elsif In_Place_Mode then
6498 Make_Failed ("--subdirs and -i cannot be used simultaneously");
6500 else
6501 if not Is_Directory (Prj.Subdirs.all) then
6502 begin
6503 Ada.Directories.Create_Path (Prj.Subdirs.all);
6504 exception
6505 when others =>
6506 Make_Failed ("unable to create object directory " &
6507 Prj.Subdirs.all);
6508 end;
6509 end if;
6511 Object_Directory_Present := True;
6513 declare
6514 Argv : constant String (1 .. Prj.Subdirs'Length) :=
6515 Prj.Subdirs.all;
6516 begin
6517 Scan_Make_Arg (Env, Argv, And_Save => False);
6518 end;
6519 end if;
6520 end if;
6522 -- Deal with -C= switch
6524 if Gnatmake_Mapping_File /= null then
6526 -- First, check compatibility with other switches
6528 if Project_File_Name /= null then
6529 Make_Failed ("-C= switch is not compatible with -P switch");
6531 elsif Saved_Maximum_Processes > 1 then
6532 Make_Failed ("-C= switch is not compatible with -jnnn switch");
6533 end if;
6535 Fmap.Initialize (Gnatmake_Mapping_File.all);
6536 Add_Switch
6537 ("-gnatem=" & Gnatmake_Mapping_File.all,
6538 Compiler,
6539 And_Save => True);
6540 end if;
6542 if Project_File_Name /= null then
6544 -- A project file was specified by a -P switch
6546 if Verbose_Mode then
6547 Write_Eol;
6548 Write_Str ("Parsing project file """);
6549 Write_Str (Project_File_Name.all);
6550 Write_Str (""".");
6551 Write_Eol;
6552 end if;
6554 -- Avoid looking in the current directory for ALI files
6556 -- Look_In_Primary_Dir := False;
6558 -- Set the project parsing verbosity to whatever was specified
6559 -- by a possible -vP switch.
6561 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
6563 -- Parse the project file.
6564 -- If there is an error, Main_Project will still be No_Project.
6566 Prj.Pars.Parse
6567 (Project => Main_Project,
6568 In_Tree => Project_Tree,
6569 Project_File_Name => Project_File_Name.all,
6570 Packages_To_Check => Packages_To_Check_By_Gnatmake,
6571 Env => Env,
6572 In_Node_Tree => Project_Node_Tree);
6574 -- The parsing of project files may have changed the current output
6576 if Commands_To_Stdout then
6577 Set_Standard_Output;
6578 else
6579 Set_Standard_Error;
6580 end if;
6582 if Main_Project = No_Project then
6583 Make_Failed
6584 ("""" & Project_File_Name.all & """ processing failed");
6585 end if;
6587 if Main_Project.Qualifier = Aggregate then
6588 Make_Failed ("aggregate projects are not supported");
6590 elsif Aggregate_Libraries_In (Project_Tree) then
6591 Make_Failed ("aggregate library projects are not supported");
6592 end if;
6594 Create_Mapping_File := True;
6596 if Verbose_Mode then
6597 Write_Eol;
6598 Write_Str ("Parsing of project file """);
6599 Write_Str (Project_File_Name.all);
6600 Write_Str (""" is finished.");
6601 Write_Eol;
6602 end if;
6604 -- We add the source directories and the object directories to the
6605 -- search paths.
6607 -- ??? Why do we need these search directories, we already know the
6608 -- locations from parsing the project, except for the runtime which
6609 -- has its own directories anyway
6611 Add_Source_Directories (Main_Project, Project_Tree);
6612 Add_Object_Directories (Main_Project, Project_Tree);
6614 Recursive_Compute_Depth (Main_Project);
6615 Compute_All_Imported_Projects (Main_Project, Project_Tree);
6617 else
6619 Osint.Add_Default_Search_Dirs;
6621 -- Source file lookups should be cached for efficiency. Source files
6622 -- are not supposed to change. However, we do that now only if no
6623 -- project file is used; if a project file is used, we do it just
6624 -- after changing the directory to the object directory.
6626 Osint.Source_File_Data (Cache => True);
6628 -- Read gnat.adc file to initialize Fname.UF
6630 Fname.UF.Initialize;
6632 if Config_File then
6633 begin
6634 Fname.SF.Read_Source_File_Name_Pragmas;
6636 exception
6637 when Err : SFN_Scan.Syntax_Error_In_GNAT_ADC =>
6638 Make_Failed (Exception_Message (Err));
6639 end;
6640 end if;
6641 end if;
6643 -- Make sure no project object directory is recorded
6645 Project_Of_Current_Object_Directory := No_Project;
6647 if Debug.Debug_Flag_N then
6648 Opt.Keep_Temporary_Files := True;
6649 end if;
6650 end Initialize;
6652 ----------------------------
6653 -- Insert_Project_Sources --
6654 ----------------------------
6656 procedure Insert_Project_Sources
6657 (The_Project : Project_Id;
6658 All_Projects : Boolean;
6659 Into_Q : Boolean)
6661 Put_In_Q : Boolean := Into_Q;
6662 Unit : Unit_Index;
6663 Sfile : File_Name_Type;
6664 Sid : Prj.Source_Id;
6665 Index : Int;
6666 Project : Project_Id;
6668 begin
6669 -- Loop through all the sources in the project files
6671 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
6672 while Unit /= null loop
6673 Sfile := No_File;
6674 Sid := No_Source;
6675 Index := 0;
6676 Project := No_Project;
6678 -- If there is a source for the body, and the body has not been
6679 -- locally removed.
6681 if Unit.File_Names (Impl) /= null
6682 and then not Unit.File_Names (Impl).Locally_Removed
6683 then
6684 -- And it is a source for the specified project
6686 if All_Projects
6687 or else
6688 Is_Extending (The_Project, Unit.File_Names (Impl).Project)
6689 then
6690 Project := Unit.File_Names (Impl).Project;
6692 -- If we don't have a spec, we cannot consider the source
6693 -- if it is a subunit.
6695 if Unit.File_Names (Spec) = null then
6696 declare
6697 Src_Ind : Source_File_Index;
6699 -- Here we are cheating a little bit: we don't want to
6700 -- use Sinput.L, because it depends on the GNAT tree
6701 -- (Atree, Sinfo, ...). So, we pretend that it is a
6702 -- project file, and we use Sinput.P.
6704 -- Source_File_Is_Subunit is just scanning through the
6705 -- file until it finds one of the reserved words
6706 -- separate, procedure, function, generic or package.
6707 -- Fortunately, these Ada reserved words are also
6708 -- reserved for project files.
6710 begin
6711 Src_Ind := Sinput.P.Load_Project_File
6712 (Get_Name_String
6713 (Unit.File_Names (Impl).Path.Display_Name));
6715 -- If it is a subunit, discard it
6717 if Sinput.P.Source_File_Is_Subunit (Src_Ind) then
6718 Sfile := No_File;
6719 Index := 0;
6720 Sid := No_Source;
6721 else
6722 Sfile := Unit.File_Names (Impl).Display_File;
6723 Index := Unit.File_Names (Impl).Index;
6724 Sid := Unit.File_Names (Impl);
6725 end if;
6726 end;
6728 else
6729 Sfile := Unit.File_Names (Impl).Display_File;
6730 Index := Unit.File_Names (Impl).Index;
6731 Sid := Unit.File_Names (Impl);
6732 end if;
6733 end if;
6735 elsif Unit.File_Names (Spec) /= null
6736 and then not Unit.File_Names (Spec).Locally_Removed
6737 and then
6738 (All_Projects
6739 or else
6740 Is_Extending (The_Project, Unit.File_Names (Spec).Project))
6741 then
6742 -- If there is no source for the body, but there is one for the
6743 -- spec which has not been locally removed, then we take this one.
6745 Sfile := Unit.File_Names (Spec).Display_File;
6746 Index := Unit.File_Names (Spec).Index;
6747 Sid := Unit.File_Names (Spec);
6748 Project := Unit.File_Names (Spec).Project;
6749 end if;
6751 -- For the first source inserted into the Q, we need to initialize
6752 -- the Q, but not for the subsequent sources.
6754 Queue.Initialize
6755 (Main_Project /= No_Project and then
6756 One_Compilation_Per_Obj_Dir);
6758 if Sfile /= No_File then
6759 Queue.Insert
6760 ((Format => Format_Gnatmake,
6761 File => Sfile,
6762 Project => Project,
6763 Unit => No_Unit_Name,
6764 Index => Index,
6765 Sid => Sid));
6766 end if;
6768 if not Put_In_Q and then Sfile /= No_File then
6770 -- If Put_In_Q is False, we add the source as if it were specified
6771 -- on the command line, and we set Put_In_Q to True, so that the
6772 -- following sources will only be put in the queue. The source is
6773 -- already in the Q, but we need at least one fake main to call
6774 -- Compile_Sources.
6776 if Verbose_Mode then
6777 Write_Str ("Adding """);
6778 Write_Str (Get_Name_String (Sfile));
6779 Write_Line (""" as if on the command line");
6780 end if;
6782 Osint.Add_File (Get_Name_String (Sfile), Index);
6783 Put_In_Q := True;
6784 end if;
6786 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
6787 end loop;
6788 end Insert_Project_Sources;
6790 ---------------------
6791 -- Is_In_Obsoleted --
6792 ---------------------
6794 function Is_In_Obsoleted (F : File_Name_Type) return Boolean is
6795 begin
6796 if F = No_File then
6797 return False;
6799 else
6800 declare
6801 Name : constant String := Get_Name_String (F);
6802 First : Natural;
6803 F2 : File_Name_Type;
6805 begin
6806 First := Name'Last;
6807 while First > Name'First
6808 and then Name (First - 1) /= Directory_Separator
6809 and then Name (First - 1) /= '/'
6810 loop
6811 First := First - 1;
6812 end loop;
6814 if First /= Name'First then
6815 Name_Len := 0;
6816 Add_Str_To_Name_Buffer (Name (First .. Name'Last));
6817 F2 := Name_Find;
6819 else
6820 F2 := F;
6821 end if;
6823 return Obsoleted.Get (F2);
6824 end;
6825 end if;
6826 end Is_In_Obsoleted;
6828 ----------------------------
6829 -- Is_In_Object_Directory --
6830 ----------------------------
6832 function Is_In_Object_Directory
6833 (Source_File : File_Name_Type;
6834 Full_Lib_File : File_Name_Type) return Boolean
6836 begin
6837 -- There is something to check only when using project files. Otherwise,
6838 -- this function returns True (last line of the function).
6840 if Main_Project /= No_Project then
6841 declare
6842 Source_File_Name : constant String :=
6843 Get_Name_String (Source_File);
6844 Saved_Verbosity : constant Verbosity := Current_Verbosity;
6845 Project : Project_Id := No_Project;
6847 Path_Name : Path_Name_Type := No_Path;
6848 pragma Warnings (Off, Path_Name);
6850 begin
6851 -- Call Get_Reference to know the ultimate extending project of
6852 -- the source. Call it with verbosity default to avoid verbose
6853 -- messages.
6855 Current_Verbosity := Default;
6856 Prj.Env.Get_Reference
6857 (Source_File_Name => Source_File_Name,
6858 Project => Project,
6859 In_Tree => Project_Tree,
6860 Path => Path_Name);
6861 Current_Verbosity := Saved_Verbosity;
6863 -- If this source is in a project, check that the ALI file is in
6864 -- its object directory. If it is not, return False, so that the
6865 -- ALI file will not be skipped.
6867 if Project /= No_Project then
6868 declare
6869 Object_Directory : constant String :=
6870 Normalize_Pathname
6871 (Get_Name_String
6872 (Project.
6873 Object_Directory.Display_Name));
6875 Olast : Natural := Object_Directory'Last;
6877 Lib_File_Directory : constant String :=
6878 Normalize_Pathname (Dir_Name
6879 (Get_Name_String (Full_Lib_File)));
6881 Llast : Natural := Lib_File_Directory'Last;
6883 begin
6884 -- For directories, Normalize_Pathname may or may not put
6885 -- a directory separator at the end, depending on its input.
6886 -- Remove any last directory separator before comparison.
6887 -- Returns True only if the two directories are the same.
6889 if Object_Directory (Olast) = Directory_Separator then
6890 Olast := Olast - 1;
6891 end if;
6893 if Lib_File_Directory (Llast) = Directory_Separator then
6894 Llast := Llast - 1;
6895 end if;
6897 return Object_Directory (Object_Directory'First .. Olast) =
6898 Lib_File_Directory (Lib_File_Directory'First .. Llast);
6899 end;
6900 end if;
6901 end;
6902 end if;
6904 -- When the source is not in a project file, always return True
6906 return True;
6907 end Is_In_Object_Directory;
6909 ----------
6910 -- Link --
6911 ----------
6913 procedure Link
6914 (ALI_File : File_Name_Type;
6915 Args : Argument_List;
6916 Success : out Boolean)
6918 Link_Args : Argument_List (1 .. Args'Length + 1);
6920 begin
6921 Get_Name_String (ALI_File);
6922 Link_Args (1) := new String'(Name_Buffer (1 .. Name_Len));
6924 Link_Args (2 .. Args'Length + 1) := Args;
6926 GNAT.OS_Lib.Normalize_Arguments (Link_Args);
6928 Display (Gnatlink.all, Link_Args);
6930 if Gnatlink_Path = null then
6931 Make_Failed ("error, unable to locate " & Gnatlink.all);
6932 end if;
6934 GNAT.OS_Lib.Spawn (Gnatlink_Path.all, Link_Args, Success);
6935 end Link;
6937 ---------------------------
6938 -- List_Bad_Compilations --
6939 ---------------------------
6941 procedure List_Bad_Compilations is
6942 begin
6943 if not No_Exit_Message then
6944 for J in Bad_Compilation.First .. Bad_Compilation.Last loop
6945 if Bad_Compilation.Table (J).File = No_File then
6946 null;
6947 elsif not Bad_Compilation.Table (J).Found then
6948 Inform (Bad_Compilation.Table (J).File, "not found");
6949 else
6950 Inform (Bad_Compilation.Table (J).File, "compilation error");
6951 end if;
6952 end loop;
6953 end if;
6954 end List_Bad_Compilations;
6956 -----------------
6957 -- List_Depend --
6958 -----------------
6960 procedure List_Depend is
6961 Lib_Name : File_Name_Type;
6962 Obj_Name : File_Name_Type;
6963 Src_Name : File_Name_Type;
6965 Len : Natural;
6966 Line_Pos : Natural;
6967 Line_Size : constant := 77;
6969 begin
6970 Set_Standard_Output;
6972 for A in ALIs.First .. ALIs.Last loop
6973 Lib_Name := ALIs.Table (A).Afile;
6975 -- We have to provide the full library file name in In_Place_Mode
6977 if In_Place_Mode then
6978 Lib_Name := Full_Lib_File_Name (Lib_Name);
6979 end if;
6981 Obj_Name := Object_File_Name (Lib_Name);
6982 Write_Name (Obj_Name);
6983 Write_Str (" :");
6985 Get_Name_String (Obj_Name);
6986 Len := Name_Len;
6987 Line_Pos := Len + 2;
6989 for D in ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep loop
6990 Src_Name := Sdep.Table (D).Sfile;
6992 if Is_Internal_File_Name (Src_Name)
6993 and then not Check_Readonly_Files
6994 then
6995 null;
6996 else
6997 if not Quiet_Output then
6998 Src_Name := Full_Source_Name (Src_Name);
6999 end if;
7001 Get_Name_String (Src_Name);
7002 Len := Name_Len;
7004 if Line_Pos + Len + 1 > Line_Size then
7005 Write_Str (" \");
7006 Write_Eol;
7007 Line_Pos := 0;
7008 end if;
7010 Line_Pos := Line_Pos + Len + 1;
7012 Write_Str (" ");
7013 Write_Name (Src_Name);
7014 end if;
7015 end loop;
7017 Write_Eol;
7018 end loop;
7020 if not Commands_To_Stdout then
7021 Set_Standard_Error;
7022 end if;
7023 end List_Depend;
7025 -----------------
7026 -- Make_Failed --
7027 -----------------
7029 procedure Make_Failed (S : String) is
7030 begin
7031 Fail_Program (Project_Tree, S);
7032 end Make_Failed;
7034 --------------------
7035 -- Mark_Directory --
7036 --------------------
7038 procedure Mark_Directory
7039 (Dir : String;
7040 Mark : Lib_Mark_Type;
7041 On_Command_Line : Boolean)
7043 N : Name_Id;
7044 B : Byte;
7046 function Base_Directory return String;
7047 -- If Dir comes from the command line, empty string (relative paths are
7048 -- resolved with respect to the current directory), else return the main
7049 -- project's directory.
7051 --------------------
7052 -- Base_Directory --
7053 --------------------
7055 function Base_Directory return String is
7056 begin
7057 if On_Command_Line then
7058 return "";
7059 else
7060 return Get_Name_String (Main_Project.Directory.Display_Name);
7061 end if;
7062 end Base_Directory;
7064 Real_Path : constant String := Normalize_Pathname (Dir, Base_Directory);
7066 -- Start of processing for Mark_Directory
7068 begin
7069 Name_Len := 0;
7071 if Real_Path'Length = 0 then
7072 Add_Str_To_Name_Buffer (Dir);
7074 else
7075 Add_Str_To_Name_Buffer (Real_Path);
7076 end if;
7078 -- Last character is supposed to be a directory separator
7080 if not Is_Directory_Separator (Name_Buffer (Name_Len)) then
7081 Add_Char_To_Name_Buffer (Directory_Separator);
7082 end if;
7084 -- Add flags to the already existing flags
7086 N := Name_Find;
7087 B := Get_Name_Table_Byte (N);
7088 Set_Name_Table_Byte (N, B or Mark);
7089 end Mark_Directory;
7091 ----------------------
7092 -- Process_Multilib --
7093 ----------------------
7095 procedure Process_Multilib (Env : in out Prj.Tree.Environment) is
7096 Output_FD : File_Descriptor;
7097 Output_Name : String_Access;
7098 Arg_Index : Natural := 0;
7099 Success : Boolean := False;
7100 Return_Code : Integer := 0;
7101 Multilib_Gcc_Path : String_Access;
7102 Multilib_Gcc : String_Access;
7103 N_Read : Integer := 0;
7104 Line : String (1 .. 1000);
7105 Args : Argument_List (1 .. N_M_Switch + 1);
7107 begin
7108 pragma Assert (N_M_Switch > 0 and RTS_Specified = null);
7110 -- In case we detected a multilib switch and the user has not
7111 -- manually specified a specific RTS we emulate the following command:
7112 -- gnatmake $FLAGS --RTS=$(gcc -print-multi-directory $FLAGS)
7114 -- First select the flags which might have an impact on multilib
7115 -- processing. Note that this is an heuristic selection and it
7116 -- will need to be maintained over time. The condition has to
7117 -- be kept synchronized with N_M_Switch counting in Scan_Make_Arg.
7119 for Next_Arg in 1 .. Argument_Count loop
7120 declare
7121 Argv : constant String := Argument (Next_Arg);
7123 begin
7124 if Argv'Length > 2
7125 and then Argv (1) = '-'
7126 and then Argv (2) = 'm'
7127 and then Argv /= "-margs"
7129 -- Ignore -mieee to avoid spawning an extra gcc in this case
7131 and then Argv /= "-mieee"
7132 then
7133 Arg_Index := Arg_Index + 1;
7134 Args (Arg_Index) := new String'(Argv);
7135 end if;
7136 end;
7137 end loop;
7139 pragma Assert (Arg_Index = N_M_Switch);
7141 Args (Args'Last) := new String'("-print-multi-directory");
7143 -- Call the GCC driver with the collected flags and save its
7144 -- output. Alternate design would be to link in gnatmake the
7145 -- relevant part of the GCC driver.
7147 if Saved_Gcc /= null then
7148 Multilib_Gcc := Saved_Gcc;
7149 else
7150 Multilib_Gcc := Gcc;
7151 end if;
7153 Multilib_Gcc_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Multilib_Gcc.all);
7155 Create_Temp_Output_File (Output_FD, Output_Name);
7157 if Output_FD = Invalid_FD then
7158 return;
7159 end if;
7161 GNAT.OS_Lib.Spawn
7162 (Multilib_Gcc_Path.all, Args, Output_FD, Return_Code, False);
7163 Close (Output_FD);
7165 if Return_Code /= 0 then
7166 return;
7167 end if;
7169 -- Parse the GCC driver output which is a single line, removing CR/LF
7171 Output_FD := Open_Read (Output_Name.all, Binary);
7173 if Output_FD = Invalid_FD then
7174 return;
7175 end if;
7177 N_Read := Read (Output_FD, Line (1)'Address, Line'Length);
7178 Close (Output_FD);
7179 Delete_File (Output_Name.all, Success);
7181 for J in reverse 1 .. N_Read loop
7182 if Line (J) = ASCII.CR or else Line (J) = ASCII.LF then
7183 N_Read := N_Read - 1;
7184 else
7185 exit;
7186 end if;
7187 end loop;
7189 -- In case the standard RTS is selected do nothing
7191 if N_Read = 0 or else Line (1 .. N_Read) = "." then
7192 return;
7193 end if;
7195 -- Otherwise add -margs --RTS=output
7197 Scan_Make_Arg (Env, "-margs", And_Save => True);
7198 Scan_Make_Arg (Env, "--RTS=" & Line (1 .. N_Read), And_Save => True);
7199 end Process_Multilib;
7201 -----------------------------
7202 -- Recursive_Compute_Depth --
7203 -----------------------------
7205 procedure Recursive_Compute_Depth (Project : Project_Id) is
7206 use Project_Boolean_Htable;
7207 Seen : Project_Boolean_Htable.Instance := Project_Boolean_Htable.Nil;
7209 procedure Recurse (Prj : Project_Id; Depth : Natural);
7210 -- Recursive procedure that does the work, keeping track of the depth
7212 -------------
7213 -- Recurse --
7214 -------------
7216 procedure Recurse (Prj : Project_Id; Depth : Natural) is
7217 List : Project_List;
7218 Proj : Project_Id;
7220 begin
7221 if Prj.Depth >= Depth or else Get (Seen, Prj) then
7222 return;
7223 end if;
7225 -- We need a test to avoid infinite recursions with limited withs:
7226 -- If we have A -> B -> A, then when set level of A to n, we try and
7227 -- set level of B to n+1, and then level of A to n + 2, ...
7229 Set (Seen, Prj, True);
7231 Prj.Depth := Depth;
7233 -- Visit each imported project
7235 List := Prj.Imported_Projects;
7236 while List /= null loop
7237 Proj := List.Project;
7238 List := List.Next;
7239 Recurse (Prj => Proj, Depth => Depth + 1);
7240 end loop;
7242 -- We again allow changing the depth of this project later on if it
7243 -- is in fact imported by a lower-level project.
7245 Set (Seen, Prj, False);
7246 end Recurse;
7248 Proj : Project_List;
7250 -- Start of processing for Recursive_Compute_Depth
7252 begin
7253 Proj := Project_Tree.Projects;
7254 while Proj /= null loop
7255 Proj.Project.Depth := 0;
7256 Proj := Proj.Next;
7257 end loop;
7259 Recurse (Project, Depth => 1);
7260 Reset (Seen);
7261 end Recursive_Compute_Depth;
7263 -------------------------------
7264 -- Report_Compilation_Failed --
7265 -------------------------------
7267 procedure Report_Compilation_Failed is
7268 begin
7269 Fail_Program (Project_Tree, "");
7270 end Report_Compilation_Failed;
7272 ------------------------
7273 -- Sigint_Intercepted --
7274 ------------------------
7276 procedure Sigint_Intercepted is
7277 SIGINT : constant := 2;
7279 begin
7280 Set_Standard_Error;
7281 Write_Line ("*** Interrupted ***");
7283 -- Send SIGINT to all outstanding compilation processes spawned
7285 for J in 1 .. Outstanding_Compiles loop
7286 Kill (Running_Compile (J).Pid, SIGINT, 1);
7287 end loop;
7289 Finish_Program (Project_Tree, E_No_Compile);
7290 end Sigint_Intercepted;
7292 -------------------
7293 -- Scan_Make_Arg --
7294 -------------------
7296 procedure Scan_Make_Arg
7297 (Env : in out Prj.Tree.Environment;
7298 Argv : String;
7299 And_Save : Boolean)
7301 Success : Boolean;
7303 begin
7304 Gnatmake_Switch_Found := True;
7306 pragma Assert (Argv'First = 1);
7308 if Argv'Length = 0 then
7309 return;
7310 end if;
7312 -- If the previous switch has set the Project_File_Name_Present flag
7313 -- (that is we have seen a -P alone), then the next argument is the name
7314 -- of the project file.
7316 if Project_File_Name_Present and then Project_File_Name = null then
7317 if Argv (1) = '-' then
7318 Make_Failed ("project file name missing after -P");
7320 else
7321 Project_File_Name_Present := False;
7322 Project_File_Name := new String'(Argv);
7323 end if;
7325 -- If the previous switch has set the Output_File_Name_Present flag
7326 -- (that is we have seen a -o), then the next argument is the name of
7327 -- the output executable.
7329 elsif Output_File_Name_Present
7330 and then not Output_File_Name_Seen
7331 then
7332 Output_File_Name_Seen := True;
7334 if Argv (1) = '-' then
7335 Make_Failed ("output file name missing after -o");
7337 else
7338 Add_Switch ("-o", Linker, And_Save => And_Save);
7339 Add_Switch (Executable_Name (Argv), Linker, And_Save => And_Save);
7340 end if;
7342 -- If the previous switch has set the Object_Directory_Present flag
7343 -- (that is we have seen a -D), then the next argument is the path name
7344 -- of the object directory.
7346 elsif Object_Directory_Present
7347 and then not Object_Directory_Seen
7348 then
7349 Object_Directory_Seen := True;
7351 if Argv (1) = '-' then
7352 Make_Failed ("object directory path name missing after -D");
7354 elsif not Is_Directory (Argv) then
7355 Make_Failed ("cannot find object directory """ & Argv & """");
7357 else
7358 -- Record the object directory. Make sure it ends with a directory
7359 -- separator.
7361 declare
7362 Norm : constant String := Normalize_Pathname (Argv);
7364 begin
7365 if Norm (Norm'Last) = Directory_Separator then
7366 Object_Directory_Path := new String'(Norm);
7367 else
7368 Object_Directory_Path :=
7369 new String'(Norm & Directory_Separator);
7370 end if;
7372 Add_Lib_Search_Dir (Norm);
7374 -- Specify the object directory to the binder
7376 Add_Switch ("-aO" & Norm, Binder, And_Save => And_Save);
7377 end;
7379 end if;
7381 -- Then check if we are dealing with -cargs/-bargs/-largs/-margs. These
7382 -- options are taken as is when found in package Compiler, Binder or
7383 -- Linker of the main project file.
7385 elsif (And_Save or else Program_Args = None)
7386 and then (Argv = "-bargs" or else
7387 Argv = "-cargs" or else
7388 Argv = "-largs" or else
7389 Argv = "-margs")
7390 then
7391 case Argv (2) is
7392 when 'c' => Program_Args := Compiler;
7393 when 'b' => Program_Args := Binder;
7394 when 'l' => Program_Args := Linker;
7395 when 'm' => Program_Args := None;
7397 when others =>
7398 raise Program_Error;
7399 end case;
7401 -- A special test is needed for the -o switch within a -largs since that
7402 -- is another way to specify the name of the final executable.
7404 elsif Program_Args = Linker and then Argv = "-o" then
7405 Make_Failed
7406 ("switch -o not allowed within a -largs. Use -o directly.");
7408 -- Check to see if we are reading switches after a -cargs, -bargs or
7409 -- -largs switch. If so, save it.
7411 elsif Program_Args /= None then
7413 -- Check to see if we are reading -I switches in order to take into
7414 -- account in the src & lib search directories.
7416 if Argv'Length > 2 and then Argv (1 .. 2) = "-I" then
7417 if Argv (3 .. Argv'Last) = "-" then
7418 Look_In_Primary_Dir := False;
7420 elsif Program_Args = Compiler then
7421 if Argv (3 .. Argv'Last) /= "-" then
7422 Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7423 end if;
7425 elsif Program_Args = Binder then
7426 Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7427 end if;
7428 end if;
7430 Add_Switch (Argv, Program_Args, And_Save => And_Save);
7432 -- Make sure that all significant switches -m on the command line
7433 -- are counted.
7435 if Argv'Length > 2
7436 and then Argv (1 .. 2) = "-m"
7437 and then Argv /= "-mieee"
7438 then
7439 N_M_Switch := N_M_Switch + 1;
7440 end if;
7442 -- Handle non-default compiler, binder, linker, and handle --RTS switch
7444 elsif Argv'Length > 2 and then Argv (1 .. 2) = "--" then
7445 if Argv'Length > 6
7446 and then Argv (1 .. 6) = "--GCC="
7447 then
7448 declare
7449 Program_Args : constant Argument_List_Access :=
7450 Argument_String_To_List
7451 (Argv (7 .. Argv'Last));
7453 begin
7454 if And_Save then
7455 Saved_Gcc := new String'(Program_Args.all (1).all);
7456 else
7457 Gcc := new String'(Program_Args.all (1).all);
7458 end if;
7460 for J in 2 .. Program_Args.all'Last loop
7461 Add_Switch
7462 (Program_Args.all (J).all, Compiler, And_Save => And_Save);
7463 end loop;
7464 end;
7466 elsif Argv'Length > 11
7467 and then Argv (1 .. 11) = "--GNATBIND="
7468 then
7469 declare
7470 Program_Args : constant Argument_List_Access :=
7471 Argument_String_To_List
7472 (Argv (12 .. Argv'Last));
7474 begin
7475 if And_Save then
7476 Saved_Gnatbind := new String'(Program_Args.all (1).all);
7477 else
7478 Gnatbind := new String'(Program_Args.all (1).all);
7479 end if;
7481 for J in 2 .. Program_Args.all'Last loop
7482 Add_Switch
7483 (Program_Args.all (J).all, Binder, And_Save => And_Save);
7484 end loop;
7485 end;
7487 elsif Argv'Length > 11
7488 and then Argv (1 .. 11) = "--GNATLINK="
7489 then
7490 declare
7491 Program_Args : constant Argument_List_Access :=
7492 Argument_String_To_List
7493 (Argv (12 .. Argv'Last));
7494 begin
7495 if And_Save then
7496 Saved_Gnatlink := new String'(Program_Args.all (1).all);
7497 else
7498 Gnatlink := new String'(Program_Args.all (1).all);
7499 end if;
7501 for J in 2 .. Program_Args.all'Last loop
7502 Add_Switch (Program_Args.all (J).all, Linker);
7503 end loop;
7504 end;
7506 elsif Argv'Length >= 5 and then
7507 Argv (1 .. 5) = "--RTS"
7508 then
7509 Add_Switch (Argv, Compiler, And_Save => And_Save);
7510 Add_Switch (Argv, Binder, And_Save => And_Save);
7512 if Argv'Length <= 6 or else Argv (6) /= '=' then
7513 Make_Failed ("missing path for --RTS");
7515 else
7516 -- Check that this is the first time we see this switch or
7517 -- if it is not the first time, the same path is specified.
7519 if RTS_Specified = null then
7520 RTS_Specified := new String'(Argv (7 .. Argv'Last));
7522 elsif RTS_Specified.all /= Argv (7 .. Argv'Last) then
7523 Make_Failed ("--RTS cannot be specified multiple times");
7524 end if;
7526 -- Valid --RTS switch
7528 No_Stdinc := True;
7529 No_Stdlib := True;
7530 RTS_Switch := True;
7532 declare
7533 Src_Path_Name : constant String_Ptr :=
7534 Get_RTS_Search_Dir
7535 (Argv (7 .. Argv'Last), Include);
7537 Lib_Path_Name : constant String_Ptr :=
7538 Get_RTS_Search_Dir
7539 (Argv (7 .. Argv'Last), Objects);
7541 begin
7542 if Src_Path_Name /= null
7543 and then Lib_Path_Name /= null
7544 then
7545 -- Set RTS_*_Path_Name variables, so that correct direct-
7546 -- ories will be set when Osint.Add_Default_Search_Dirs
7547 -- is called later.
7549 RTS_Src_Path_Name := Src_Path_Name;
7550 RTS_Lib_Path_Name := Lib_Path_Name;
7552 elsif Src_Path_Name = null
7553 and then Lib_Path_Name = null
7554 then
7555 Make_Failed ("RTS path not valid: missing "
7556 & "adainclude and adalib directories");
7558 elsif Src_Path_Name = null then
7559 Make_Failed ("RTS path not valid: missing adainclude "
7560 & "directory");
7562 elsif Lib_Path_Name = null then
7563 Make_Failed ("RTS path not valid: missing adalib "
7564 & "directory");
7565 end if;
7566 end;
7567 end if;
7569 elsif Argv'Length > Source_Info_Option'Length and then
7570 Argv (1 .. Source_Info_Option'Length) = Source_Info_Option
7571 then
7572 Project_Tree.Source_Info_File_Name :=
7573 new String'(Argv (Source_Info_Option'Length + 1 .. Argv'Last));
7575 elsif Argv'Length >= 8 and then
7576 Argv (1 .. 8) = "--param="
7577 then
7578 Add_Switch (Argv, Compiler, And_Save => And_Save);
7579 Add_Switch (Argv, Linker, And_Save => And_Save);
7581 elsif Argv = Create_Map_File_Switch then
7582 Map_File := new String'("");
7584 elsif Argv'Length > Create_Map_File_Switch'Length + 1
7585 and then
7586 Argv (1 .. Create_Map_File_Switch'Length) = Create_Map_File_Switch
7587 and then
7588 Argv (Create_Map_File_Switch'Length + 1) = '='
7589 then
7590 Map_File :=
7591 new String'
7592 (Argv (Create_Map_File_Switch'Length + 2 .. Argv'Last));
7594 else
7595 Scan_Make_Switches (Env, Argv, Success);
7596 end if;
7598 -- If we have seen a regular switch process it
7600 elsif Argv (1) = '-' then
7601 if Argv'Length = 1 then
7602 Make_Failed ("switch character cannot be followed by a blank");
7604 -- Incorrect switches that should start with "--"
7606 elsif (Argv'Length > 5 and then Argv (1 .. 5) = "-RTS=")
7607 or else (Argv'Length > 5 and then Argv (1 .. 5) = "-GCC=")
7608 or else (Argv'Length > 8 and then Argv (1 .. 7) = "-param=")
7609 or else (Argv'Length > 10 and then Argv (1 .. 10) = "-GNATLINK=")
7610 or else (Argv'Length > 10 and then Argv (1 .. 10) = "-GNATBIND=")
7611 then
7612 Make_Failed ("option " & Argv & " should start with '--'");
7614 -- -I-
7616 elsif Argv (2 .. Argv'Last) = "I-" then
7617 Look_In_Primary_Dir := False;
7619 -- Forbid -?- or -??- where ? is any character
7621 elsif (Argv'Length = 3 and then Argv (3) = '-')
7622 or else (Argv'Length = 4 and then Argv (4) = '-')
7623 then
7624 Make_Failed
7625 ("trailing ""-"" at the end of " & Argv & " forbidden.");
7627 -- -Idir
7629 elsif Argv (2) = 'I' then
7630 Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7631 Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7632 Add_Switch (Argv, Compiler, And_Save => And_Save);
7633 Add_Switch (Argv, Binder, And_Save => And_Save);
7635 -- -aIdir (to gcc this is like a -I switch)
7637 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aI" then
7638 Add_Source_Search_Dir (Argv (4 .. Argv'Last), And_Save);
7639 Add_Switch
7640 ("-I" & Argv (4 .. Argv'Last), Compiler, And_Save => And_Save);
7641 Add_Switch (Argv, Binder, And_Save => And_Save);
7643 -- -aOdir
7645 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aO" then
7646 Add_Library_Search_Dir (Argv (4 .. Argv'Last), And_Save);
7647 Add_Switch (Argv, Binder, And_Save => And_Save);
7649 -- -aLdir (to gnatbind this is like a -aO switch)
7651 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aL" then
7652 Mark_Directory (Argv (4 .. Argv'Last), Ada_Lib_Dir, And_Save);
7653 Add_Library_Search_Dir (Argv (4 .. Argv'Last), And_Save);
7654 Add_Switch
7655 ("-aO" & Argv (4 .. Argv'Last), Binder, And_Save => And_Save);
7657 -- -aamp_target=...
7659 elsif Argv'Length >= 13 and then Argv (2 .. 13) = "aamp_target=" then
7660 Add_Switch (Argv, Compiler, And_Save => And_Save);
7662 -- Set the aamp_target environment variable so that the binder and
7663 -- linker will use the proper target library. This is consistent
7664 -- with how things work when -aamp_target is passed on the command
7665 -- line to gnaampmake.
7667 Setenv ("aamp_target", Argv (14 .. Argv'Last));
7669 -- -Adir (to gnatbind this is like a -aO switch, to gcc like a -I)
7671 elsif Argv (2) = 'A' then
7672 Mark_Directory (Argv (3 .. Argv'Last), Ada_Lib_Dir, And_Save);
7673 Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7674 Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7675 Add_Switch
7676 ("-I" & Argv (3 .. Argv'Last), Compiler, And_Save => And_Save);
7677 Add_Switch
7678 ("-aO" & Argv (3 .. Argv'Last), Binder, And_Save => And_Save);
7680 -- -Ldir
7682 elsif Argv (2) = 'L' then
7683 Add_Switch (Argv, Linker, And_Save => And_Save);
7685 -- For -gxxx, -pg, -mxxx, -fxxx, -Oxxx, pass the switch to both the
7686 -- compiler and the linker (except for -gnatxxx which is only for the
7687 -- compiler). Some of the -mxxx (for example -m64) and -fxxx (for
7688 -- example -ftest-coverage for gcov) need to be used when compiling
7689 -- the binder generated files, and using all these gcc switches for
7690 -- them should not be a problem. Pass -Oxxx to the linker for LTO.
7692 elsif
7693 (Argv (2) = 'g' and then (Argv'Last < 5
7694 or else Argv (2 .. 5) /= "gnat"))
7695 or else Argv (2 .. Argv'Last) = "pg"
7696 or else (Argv (2) = 'm' and then Argv'Last > 2)
7697 or else (Argv (2) = 'f' and then Argv'Last > 2)
7698 or else Argv (2) = 'O'
7699 then
7700 Add_Switch (Argv, Compiler, And_Save => And_Save);
7701 Add_Switch (Argv, Linker, And_Save => And_Save);
7703 -- The following condition has to be kept synchronized with
7704 -- the Process_Multilib one.
7706 if Argv (2) = 'm'
7707 and then Argv /= "-mieee"
7708 then
7709 N_M_Switch := N_M_Switch + 1;
7710 end if;
7712 -- -C=<mapping file>
7714 elsif Argv'Last > 2 and then Argv (2) = 'C' then
7715 if And_Save then
7716 if Argv (3) /= '=' or else Argv'Last <= 3 then
7717 Make_Failed ("illegal switch " & Argv);
7718 end if;
7720 Gnatmake_Mapping_File := new String'(Argv (4 .. Argv'Last));
7721 end if;
7723 -- -D
7725 elsif Argv'Last = 2 and then Argv (2) = 'D' then
7726 if Project_File_Name /= null then
7727 Make_Failed
7728 ("-D cannot be used in conjunction with a project file");
7730 else
7731 Scan_Make_Switches (Env, Argv, Success);
7732 end if;
7734 -- -d
7736 elsif Argv (2) = 'd' and then Argv'Last = 2 then
7737 Display_Compilation_Progress := True;
7739 -- -i
7741 elsif Argv'Last = 2 and then Argv (2) = 'i' then
7742 if Project_File_Name /= null then
7743 Make_Failed
7744 ("-i cannot be used in conjunction with a project file");
7745 else
7746 Scan_Make_Switches (Env, Argv, Success);
7747 end if;
7749 -- -j (need to save the result)
7751 elsif Argv (2) = 'j' then
7752 Scan_Make_Switches (Env, Argv, Success);
7754 if And_Save then
7755 Saved_Maximum_Processes := Maximum_Processes;
7756 end if;
7758 -- -m
7760 elsif Argv (2) = 'm' and then Argv'Last = 2 then
7761 Minimal_Recompilation := True;
7763 -- -u
7765 elsif Argv (2) = 'u' and then Argv'Last = 2 then
7766 Unique_Compile := True;
7767 Compile_Only := True;
7768 Do_Bind_Step := False;
7769 Do_Link_Step := False;
7771 -- -U
7773 elsif Argv (2) = 'U'
7774 and then Argv'Last = 2
7775 then
7776 Unique_Compile_All_Projects := True;
7777 Unique_Compile := True;
7778 Compile_Only := True;
7779 Do_Bind_Step := False;
7780 Do_Link_Step := False;
7782 -- -Pprj or -P prj (only once, and only on the command line)
7784 elsif Argv (2) = 'P' then
7785 if Project_File_Name /= null then
7786 Make_Failed ("cannot have several project files specified");
7788 elsif Object_Directory_Path /= null then
7789 Make_Failed
7790 ("-D cannot be used in conjunction with a project file");
7792 elsif In_Place_Mode then
7793 Make_Failed
7794 ("-i cannot be used in conjunction with a project file");
7796 elsif not And_Save then
7798 -- It could be a tool other than gnatmake (e.g. gnatdist)
7799 -- or a -P switch inside a project file.
7801 Fail
7802 ("either the tool is not ""project-aware"" or "
7803 & "a project file is specified inside a project file");
7805 elsif Argv'Last = 2 then
7807 -- -P is used alone: the project file name is the next option
7809 Project_File_Name_Present := True;
7811 else
7812 Project_File_Name := new String'(Argv (3 .. Argv'Last));
7813 end if;
7815 -- -vPx (verbosity of the parsing of the project files)
7817 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "vP" then
7818 if Argv'Last /= 4 or else Argv (4) not in '0' .. '2' then
7819 Make_Failed
7820 ("invalid verbosity level " & Argv (4 .. Argv'Last));
7822 elsif And_Save then
7823 case Argv (4) is
7824 when '0' =>
7825 Current_Verbosity := Prj.Default;
7826 when '1' =>
7827 Current_Verbosity := Prj.Medium;
7828 when '2' =>
7829 Current_Verbosity := Prj.High;
7830 when others =>
7831 null;
7832 end case;
7833 end if;
7835 -- -Xext=val (External assignment)
7837 elsif Argv (2) = 'X'
7838 and then Is_External_Assignment (Env, Argv)
7839 then
7840 -- Is_External_Assignment has side effects when it returns True
7842 null;
7844 -- If -gnath is present, then generate the usage information right
7845 -- now and do not pass this option on to the compiler calls.
7847 elsif Argv = "-gnath" then
7848 Usage;
7850 -- If -gnatc is specified, make sure the bind and link steps are not
7851 -- executed.
7853 elsif Argv'Length >= 6 and then Argv (2 .. 6) = "gnatc" then
7855 -- If -gnatc is specified, make sure the bind and link steps are
7856 -- not executed.
7858 Add_Switch (Argv, Compiler, And_Save => And_Save);
7859 Operating_Mode := Check_Semantics;
7860 Check_Object_Consistency := False;
7862 -- Except in CodePeer mode (set by -gnatcC), where we do want to
7863 -- call bind/link in CodePeer mode (-P switch).
7865 if Argv'Last >= 7 and then Argv (7) = 'C' then
7866 CodePeer_Mode := True;
7867 else
7868 Compile_Only := True;
7869 Do_Bind_Step := False;
7870 Do_Link_Step := False;
7871 end if;
7873 -- If -gnatA is specified, make sure that gnat.adc is never read
7875 elsif Argv'Length >= 6 and then Argv (2 .. 6) = "gnatA" then
7876 Add_Switch (Argv, Compiler, And_Save => And_Save);
7877 Opt.Config_File := False;
7879 elsif Argv (2 .. Argv'Last) = "nostdlib" then
7881 -- Pass -nstdlib to gnatbind and gnatlink
7883 No_Stdlib := True;
7884 Add_Switch (Argv, Binder, And_Save => And_Save);
7885 Add_Switch (Argv, Linker, And_Save => And_Save);
7887 elsif Argv (2 .. Argv'Last) = "nostdinc" then
7889 -- Pass -nostdinc to the Compiler and to gnatbind
7891 No_Stdinc := True;
7892 Add_Switch (Argv, Compiler, And_Save => And_Save);
7893 Add_Switch (Argv, Binder, And_Save => And_Save);
7895 -- All other switches are processed by Scan_Make_Switches. If the
7896 -- call returns with Gnatmake_Switch_Found = False, then the switch
7897 -- is passed to the compiler.
7899 else
7900 Scan_Make_Switches (Env, Argv, Gnatmake_Switch_Found);
7902 if not Gnatmake_Switch_Found then
7903 Add_Switch (Argv, Compiler, And_Save => And_Save);
7904 end if;
7905 end if;
7907 -- If not a switch it must be a file name
7909 else
7910 if And_Save then
7911 Main_On_Command_Line := True;
7912 end if;
7914 Add_File (Argv);
7915 Mains.Add_Main (Argv);
7916 end if;
7917 end Scan_Make_Arg;
7919 -----------------
7920 -- Switches_Of --
7921 -----------------
7923 function Switches_Of
7924 (Source_File : File_Name_Type;
7925 Project : Project_Id;
7926 In_Package : Package_Id;
7927 Allow_ALI : Boolean) return Variable_Value
7929 Switches : Variable_Value;
7930 Is_Default : Boolean;
7932 begin
7933 Makeutl.Get_Switches
7934 (Source_File => Source_File,
7935 Source_Lang => Name_Ada,
7936 Source_Prj => Project,
7937 Pkg_Name => Project_Tree.Shared.Packages.Table (In_Package).Name,
7938 Project_Tree => Project_Tree,
7939 Value => Switches,
7940 Is_Default => Is_Default,
7941 Test_Without_Suffix => True,
7942 Check_ALI_Suffix => Allow_ALI);
7943 return Switches;
7944 end Switches_Of;
7946 -----------
7947 -- Usage --
7948 -----------
7950 procedure Usage is
7951 begin
7952 if Usage_Needed then
7953 Usage_Needed := False;
7954 Makeusg;
7955 end if;
7956 end Usage;
7958 begin
7959 -- Make sure that in case of failure, the temp files will be deleted
7961 Prj.Com.Fail := Make_Failed'Access;
7962 MLib.Fail := Make_Failed'Access;
7963 end Make;