Update ChangeLog and version files for release
[official-gcc.git] / gcc / ada / make.adb
blob4fd741c1be600f33f78e7ef3cb010963d7a2622f
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- M A K E --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2016, 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 System_Package_Spec_Name : constant String := "system.ads";
88 -- Every program depends on one of these packages: usually the first one,
89 -- or if Supress_Standard_Library is true on the second one. The dependency
90 -- is not always explicit and considering it is important when -f and -a
91 -- are used.
93 type Sigint_Handler is access procedure;
94 pragma Convention (C, Sigint_Handler);
96 procedure Install_Int_Handler (Handler : Sigint_Handler);
97 pragma Import (C, Install_Int_Handler, "__gnat_install_int_handler");
98 -- Called by Gnatmake to install the SIGINT handler below
100 procedure Sigint_Intercepted;
101 pragma Convention (C, Sigint_Intercepted);
102 -- Called when the program is interrupted by Ctrl-C to delete the
103 -- temporary mapping files and configuration pragmas files.
105 No_Mapping_File : constant Natural := 0;
107 type Compilation_Data is record
108 Pid : Process_Id;
109 Full_Source_File : File_Name_Type;
110 Lib_File : File_Name_Type;
111 Source_Unit : Unit_Name_Type;
112 Full_Lib_File : File_Name_Type;
113 Lib_File_Attr : aliased File_Attributes;
114 Mapping_File : Natural := No_Mapping_File;
115 Project : Project_Id := No_Project;
116 end record;
117 -- Data recorded for each compilation process spawned
119 No_Compilation_Data : constant Compilation_Data :=
120 (Invalid_Pid, No_File, No_File, No_Unit_Name, No_File, Unknown_Attributes,
121 No_Mapping_File, No_Project);
123 type Comp_Data_Arr is array (Positive range <>) of Compilation_Data;
124 type Comp_Data_Ptr is access Comp_Data_Arr;
125 Running_Compile : Comp_Data_Ptr;
126 -- Used to save information about outstanding compilations
128 Outstanding_Compiles : Natural := 0;
129 -- Current number of outstanding compiles
131 -------------------------
132 -- Note on terminology --
133 -------------------------
135 -- In this program, we use the phrase "termination" of a file name to refer
136 -- to the suffix that appears after the unit name portion. Very often this
137 -- is simply the extension, but in some cases, the sequence may be more
138 -- complex, for example in main.1.ada, the termination in this name is
139 -- ".1.ada" and in main_.ada the termination is "_.ada".
141 procedure Insert_Project_Sources
142 (The_Project : Project_Id;
143 All_Projects : Boolean;
144 Into_Q : Boolean);
145 -- If Into_Q is True, insert all sources of the project file(s) that are
146 -- not already marked into the Q. If Into_Q is False, call Osint.Add_File
147 -- for the first source, then insert all other sources that are not already
148 -- marked into the Q. If All_Projects is True, all sources of all projects
149 -- are concerned; otherwise, only sources of The_Project are concerned,
150 -- including, if The_Project is an extending project, sources inherited
151 -- from projects being extended.
153 Unique_Compile : Boolean := False;
154 -- Set to True if -u or -U or a project file with no main is used
156 Unique_Compile_All_Projects : Boolean := False;
157 -- Set to True if -U is used
159 Must_Compile : Boolean := False;
160 -- True if gnatmake is invoked with -f -u and one or several mains on the
161 -- command line.
163 Project_Tree : constant Project_Tree_Ref :=
164 new Project_Tree_Data (Is_Root_Tree => True);
165 -- The project tree
167 Main_On_Command_Line : Boolean := False;
168 -- True if gnatmake is invoked with one or several mains on the command
169 -- line.
171 RTS_Specified : String_Access := null;
172 -- Used to detect multiple --RTS= switches
174 N_M_Switch : Natural := 0;
175 -- Used to count -mxxx switches that can affect multilib
177 -- The 3 following packages are used to store gcc, gnatbind and gnatlink
178 -- switches found in the project files.
180 package Gcc_Switches is new Table.Table (
181 Table_Component_Type => String_Access,
182 Table_Index_Type => Integer,
183 Table_Low_Bound => 1,
184 Table_Initial => 20,
185 Table_Increment => 100,
186 Table_Name => "Make.Gcc_Switches");
188 package Binder_Switches is new Table.Table (
189 Table_Component_Type => String_Access,
190 Table_Index_Type => Integer,
191 Table_Low_Bound => 1,
192 Table_Initial => 20,
193 Table_Increment => 100,
194 Table_Name => "Make.Binder_Switches");
196 package Linker_Switches is new Table.Table (
197 Table_Component_Type => String_Access,
198 Table_Index_Type => Integer,
199 Table_Low_Bound => 1,
200 Table_Initial => 20,
201 Table_Increment => 100,
202 Table_Name => "Make.Linker_Switches");
204 -- The following instantiations and variables are necessary to save what
205 -- is found on the command line, in case there is a project file specified.
207 package Saved_Gcc_Switches is new Table.Table (
208 Table_Component_Type => String_Access,
209 Table_Index_Type => Integer,
210 Table_Low_Bound => 1,
211 Table_Initial => 20,
212 Table_Increment => 100,
213 Table_Name => "Make.Saved_Gcc_Switches");
215 package Saved_Binder_Switches is new Table.Table (
216 Table_Component_Type => String_Access,
217 Table_Index_Type => Integer,
218 Table_Low_Bound => 1,
219 Table_Initial => 20,
220 Table_Increment => 100,
221 Table_Name => "Make.Saved_Binder_Switches");
223 package Saved_Linker_Switches is new Table.Table
224 (Table_Component_Type => String_Access,
225 Table_Index_Type => Integer,
226 Table_Low_Bound => 1,
227 Table_Initial => 20,
228 Table_Increment => 100,
229 Table_Name => "Make.Saved_Linker_Switches");
231 package Switches_To_Check is new Table.Table (
232 Table_Component_Type => String_Access,
233 Table_Index_Type => Integer,
234 Table_Low_Bound => 1,
235 Table_Initial => 20,
236 Table_Increment => 100,
237 Table_Name => "Make.Switches_To_Check");
239 package Library_Paths is new Table.Table (
240 Table_Component_Type => String_Access,
241 Table_Index_Type => Integer,
242 Table_Low_Bound => 1,
243 Table_Initial => 20,
244 Table_Increment => 100,
245 Table_Name => "Make.Library_Paths");
247 package Failed_Links is new Table.Table (
248 Table_Component_Type => File_Name_Type,
249 Table_Index_Type => Integer,
250 Table_Low_Bound => 1,
251 Table_Initial => 10,
252 Table_Increment => 100,
253 Table_Name => "Make.Failed_Links");
255 package Successful_Links is new Table.Table (
256 Table_Component_Type => File_Name_Type,
257 Table_Index_Type => Integer,
258 Table_Low_Bound => 1,
259 Table_Initial => 10,
260 Table_Increment => 100,
261 Table_Name => "Make.Successful_Links");
263 package Library_Projs is new Table.Table (
264 Table_Component_Type => Project_Id,
265 Table_Index_Type => Integer,
266 Table_Low_Bound => 1,
267 Table_Initial => 10,
268 Table_Increment => 100,
269 Table_Name => "Make.Library_Projs");
271 -- Two variables to keep the last binder and linker switch index in tables
272 -- Binder_Switches and Linker_Switches, before adding switches from the
273 -- project file (if any) and switches from the command line (if any).
275 Last_Binder_Switch : Integer := 0;
276 Last_Linker_Switch : Integer := 0;
278 Normalized_Switches : Argument_List_Access := new Argument_List (1 .. 10);
279 Last_Norm_Switch : Natural := 0;
281 Saved_Maximum_Processes : Natural := 0;
283 Gnatmake_Switch_Found : Boolean;
284 -- Set by Scan_Make_Arg. True when the switch is a gnatmake switch.
285 -- Tested by Add_Switches when switches in package Builder must all be
286 -- gnatmake switches.
288 Switch_May_Be_Passed_To_The_Compiler : Boolean;
289 -- Set by Add_Switches and Switches_Of. True when unrecognized switches
290 -- are passed to the Ada compiler.
292 type Arg_List_Ref is access Argument_List;
293 The_Saved_Gcc_Switches : Arg_List_Ref;
295 Project_File_Name : String_Access := null;
296 -- The path name of the main project file, if any
298 Project_File_Name_Present : Boolean := False;
299 -- True when -P is used with a space between -P and the project file name
301 Current_Verbosity : Prj.Verbosity := Prj.Default;
302 -- Verbosity to parse the project files
304 Main_Project : Prj.Project_Id := No_Project;
305 -- The project id of the main project file, if any
307 Project_Of_Current_Object_Directory : Project_Id := No_Project;
308 -- The object directory of the project for the last compilation. Avoid
309 -- calling Change_Dir if the current working directory is already this
310 -- directory.
312 Map_File : String_Access := null;
313 -- Value of switch --create-map-file
315 -- Packages of project files where unknown attributes are errors
317 Naming_String : aliased String := "naming";
318 Builder_String : aliased String := "builder";
319 Compiler_String : aliased String := "compiler";
320 Binder_String : aliased String := "binder";
321 Linker_String : aliased String := "linker";
323 Gnatmake_Packages : aliased String_List :=
324 (Naming_String 'Access,
325 Builder_String 'Access,
326 Compiler_String 'Access,
327 Binder_String 'Access,
328 Linker_String 'Access);
330 Packages_To_Check_By_Gnatmake : constant String_List_Access :=
331 Gnatmake_Packages'Access;
333 procedure Add_Library_Search_Dir
334 (Path : String;
335 On_Command_Line : Boolean);
336 -- Call Add_Lib_Search_Dir with an absolute directory path. If Path is
337 -- relative path, when On_Command_Line is True, it is relative to the
338 -- current working directory. When On_Command_Line is False, it is relative
339 -- to the project directory of the main project.
341 procedure Add_Source_Search_Dir
342 (Path : String;
343 On_Command_Line : Boolean);
344 -- Call Add_Src_Search_Dir with an absolute directory path. If Path is a
345 -- relative path, when On_Command_Line is True, it is relative to the
346 -- current working directory. When On_Command_Line is False, it is relative
347 -- to the project directory of the main project.
349 procedure Add_Source_Dir (N : String);
350 -- Call Add_Src_Search_Dir (output one line when in verbose mode)
352 procedure Add_Source_Directories is
353 new Prj.Env.For_All_Source_Dirs (Action => Add_Source_Dir);
355 procedure Add_Object_Dir (N : String);
356 -- Call Add_Lib_Search_Dir (output one line when in verbose mode)
358 procedure Add_Object_Directories is
359 new Prj.Env.For_All_Object_Dirs (Action => Add_Object_Dir);
361 procedure Change_To_Object_Directory (Project : Project_Id);
362 -- Change to the object directory of project Project, if this is not
363 -- already the current working directory.
365 type Bad_Compilation_Info is record
366 File : File_Name_Type;
367 Unit : Unit_Name_Type;
368 Found : Boolean;
369 end record;
370 -- File is the name of the file for which a compilation failed. Unit is for
371 -- gnatdist use in order to easily get the unit name of a file when its
372 -- name is krunched or declared in gnat.adc. Found is False if the
373 -- compilation failed because the file could not be found.
375 package Bad_Compilation is new Table.Table (
376 Table_Component_Type => Bad_Compilation_Info,
377 Table_Index_Type => Natural,
378 Table_Low_Bound => 1,
379 Table_Initial => 20,
380 Table_Increment => 100,
381 Table_Name => "Make.Bad_Compilation");
382 -- Full name of all the source files for which compilation fails
384 Do_Compile_Step : Boolean := True;
385 Do_Bind_Step : Boolean := True;
386 Do_Link_Step : Boolean := True;
387 -- Flags to indicate what step should be executed. Can be set to False
388 -- with the switches -c, -b and -l. These flags are reset to True for
389 -- each invocation of procedure Gnatmake.
391 Shared_String : aliased String := "-shared";
392 Force_Elab_Flags_String : aliased String := "-F";
393 CodePeer_Mode_String : aliased String := "-P";
395 No_Shared_Switch : aliased Argument_List := (1 .. 0 => null);
396 Shared_Switch : aliased Argument_List := (1 => Shared_String'Access);
397 Bind_Shared : Argument_List_Access := No_Shared_Switch'Access;
398 -- Switch to added in front of gnatbind switches. By default no switch is
399 -- added. Switch "-shared" is added if there is a non-static Library
400 -- Project File.
402 Shared_Libgcc : aliased String := "-shared-libgcc";
404 No_Shared_Libgcc_Switch : aliased Argument_List := (1 .. 0 => null);
405 Shared_Libgcc_Switch : aliased Argument_List :=
406 (1 => Shared_Libgcc'Access);
407 Link_With_Shared_Libgcc : Argument_List_Access :=
408 No_Shared_Libgcc_Switch'Access;
410 procedure Make_Failed (S : String);
411 -- Delete all temp files created by Gnatmake and call Osint.Fail, with the
412 -- parameter S (see osint.ads). This is called from the Prj hierarchy and
413 -- the MLib hierarchy. This subprogram also prints current error messages
414 -- (i.e. finalizes Errutil).
416 --------------------------
417 -- Obsolete Executables --
418 --------------------------
420 Executable_Obsolete : Boolean := False;
421 -- Executable_Obsolete is initially set to False for each executable,
422 -- and is set to True whenever one of the source of the executable is
423 -- compiled, or has already been compiled for another executable.
425 Max_Header : constant := 200;
426 -- This needs a proper comment, it used to say "arbitrary" that's not an
427 -- adequate comment ???
429 type Header_Num is range 1 .. Max_Header;
430 -- Header_Num for the hash table Obsoleted below
432 function Hash (F : File_Name_Type) return Header_Num;
433 -- Hash function for the hash table Obsoleted below
435 package Obsoleted is new System.HTable.Simple_HTable
436 (Header_Num => Header_Num,
437 Element => Boolean,
438 No_Element => False,
439 Key => File_Name_Type,
440 Hash => Hash,
441 Equal => "=");
442 -- A hash table to keep all files that have been compiled, to detect
443 -- if an executable is up to date or not.
445 procedure Enter_Into_Obsoleted (F : File_Name_Type);
446 -- Enter a file name, without directory information, into the hash table
447 -- Obsoleted.
449 function Is_In_Obsoleted (F : File_Name_Type) return Boolean;
450 -- Check if a file name, without directory information, has already been
451 -- entered into the hash table Obsoleted.
453 type Dependency is record
454 This : File_Name_Type;
455 Depends_On : File_Name_Type;
456 end record;
457 -- Components of table Dependencies below
459 package Dependencies is new Table.Table (
460 Table_Component_Type => Dependency,
461 Table_Index_Type => Integer,
462 Table_Low_Bound => 1,
463 Table_Initial => 20,
464 Table_Increment => 100,
465 Table_Name => "Make.Dependencies");
466 -- A table to keep dependencies, to be able to decide if an executable
467 -- is obsolete. More explanation needed ???
469 ----------------------------
470 -- Arguments and Switches --
471 ----------------------------
473 Arguments : Argument_List_Access;
474 -- Used to gather the arguments for invocation of the compiler
476 Last_Argument : Natural := 0;
477 -- Last index of arguments in Arguments above
479 Arguments_Project : Project_Id;
480 -- Project id, if any, of the source to be compiled
482 Arguments_Path_Name : Path_Name_Type;
483 -- Full path of the source to be compiled, when Arguments_Project is not
484 -- No_Project.
486 Dummy_Switch : constant String_Access := new String'("- ");
487 -- Used to initialized Prev_Switch in procedure Check
489 procedure Add_Arguments (Args : Argument_List);
490 -- Add arguments to global variable Arguments, increasing its size
491 -- if necessary and adjusting Last_Argument.
493 function Configuration_Pragmas_Switch
494 (For_Project : Project_Id) return Argument_List;
495 -- Return an argument list of one element, if there is a configuration
496 -- pragmas file to be specified for For_Project,
497 -- otherwise return an empty argument list.
499 -------------------
500 -- Misc Routines --
501 -------------------
503 procedure List_Depend;
504 -- Prints to standard output the list of object dependencies. This list
505 -- can be used directly in a Makefile. A call to Compile_Sources must
506 -- precede the call to List_Depend. Also because this routine uses the
507 -- ALI files that were originally loaded and scanned by Compile_Sources,
508 -- no additional ALI files should be scanned between the two calls (i.e.
509 -- between the call to Compile_Sources and List_Depend.)
511 procedure List_Bad_Compilations;
512 -- Prints out the list of all files for which the compilation failed
514 Usage_Needed : Boolean := True;
515 -- Flag used to make sure Makeusg is call at most once
517 procedure Usage;
518 -- Call Makeusg, if Usage_Needed is True.
519 -- Set Usage_Needed to False.
521 procedure Debug_Msg (S : String; N : Name_Id);
522 procedure Debug_Msg (S : String; N : File_Name_Type);
523 procedure Debug_Msg (S : String; N : Unit_Name_Type);
524 -- If Debug.Debug_Flag_W is set outputs string S followed by name N
526 procedure Recursive_Compute_Depth (Project : Project_Id);
527 -- Compute depth of Project and of the projects it depends on
529 -----------------------
530 -- Gnatmake Routines --
531 -----------------------
533 subtype Lib_Mark_Type is Byte;
534 -- Used in Mark_Directory
536 Ada_Lib_Dir : constant Lib_Mark_Type := 1;
537 -- Used to mark a directory as a GNAT lib dir
539 -- Note that the notion of GNAT lib dir is no longer used. The code related
540 -- to it has not been removed to give an idea on how to use the directory
541 -- prefix marking mechanism.
543 -- An Ada library directory is a directory containing ali and object files
544 -- but no source files for the bodies (the specs can be in the same or some
545 -- other directory). These directories are specified in the Gnatmake
546 -- command line with the switch "-Adir" (to specify the spec location -Idir
547 -- cab be used). Gnatmake skips the missing sources whose ali are in Ada
548 -- library directories. For an explanation of why Gnatmake behaves that
549 -- way, see the spec of Make.Compile_Sources. The directory lookup penalty
550 -- is incurred every single time this routine is called.
552 procedure Check_Steps;
553 -- Check what steps (Compile, Bind, Link) must be executed.
554 -- Set the step flags accordingly.
556 function In_Ada_Lib_Dir (File : File_Name_Type) return Boolean;
557 -- Get directory prefix of this file and get lib mark stored in name
558 -- table for this directory. Then check if an Ada lib mark has been set.
560 procedure Mark_Directory
561 (Dir : String;
562 Mark : Lib_Mark_Type;
563 On_Command_Line : Boolean);
564 -- Store the absolute path from Dir in name table and set lib mark as name
565 -- info to identify Ada libraries.
567 -- If Dir is a relative path, when On_Command_Line is True, it is relative
568 -- to the current working directory; when On_Command_Line is False, it is
569 -- relative to the project directory of the main project.
571 Output_Is_Object : Boolean := True;
572 -- Set to False when using a switch -S for the compiler
574 procedure Check_For_S_Switch;
575 -- Set Output_Is_Object to False when the -S switch is used for the
576 -- compiler.
578 function Switches_Of
579 (Source_File : File_Name_Type;
580 Project : Project_Id;
581 In_Package : Package_Id;
582 Allow_ALI : Boolean) return Variable_Value;
583 -- Return the switches for the source file in the specified package of a
584 -- project file. If the Source_File ends with a standard GNAT extension
585 -- (".ads" or ".adb"), try first the full name, then the name without the
586 -- extension, then, if Allow_ALI is True, the name with the extension
587 -- ".ali". If there is no switches for either names, try first Switches
588 -- (others) then the default switches for Ada. If all failed, return
589 -- No_Variable_Value.
591 function Is_In_Object_Directory
592 (Source_File : File_Name_Type;
593 Full_Lib_File : File_Name_Type) return Boolean;
594 -- Check if, when using a project file, the ALI file is in the project
595 -- directory of the ultimate extending project. If it is not, we ignore
596 -- the fact that this ALI file is read-only.
598 procedure Process_Multilib (Env : in out Prj.Tree.Environment);
599 -- Add appropriate --RTS argument to handle multilib
601 procedure Resolve_Relative_Names_In_Switches (Current_Work_Dir : String);
602 -- Resolve all relative paths found in the linker and binder switches,
603 -- when using project files.
605 procedure Queue_Library_Project_Sources;
606 -- For all library project, if the library file does not exist, put all the
607 -- project sources in the queue, and flag the project so that the library
608 -- is generated.
610 procedure Compute_Switches_For_Main
611 (Main_Source_File : in out File_Name_Type;
612 Root_Environment : in out Prj.Tree.Environment;
613 Compute_Builder : Boolean;
614 Current_Work_Dir : String);
615 -- Find compiler, binder and linker switches to use for the given main
617 procedure Compute_Executable
618 (Main_Source_File : File_Name_Type;
619 Executable : out File_Name_Type;
620 Non_Std_Executable : out Boolean);
621 -- Parse the linker switches and project file to compute the name of the
622 -- executable to generate.
623 -- ??? What is the meaning of Non_Std_Executable
625 procedure Compilation_Phase
626 (Main_Source_File : File_Name_Type;
627 Current_Main_Index : Int := 0;
628 Total_Compilation_Failures : in out Natural;
629 Stand_Alone_Libraries : in out Boolean;
630 Executable : File_Name_Type := No_File;
631 Is_Last_Main : Boolean;
632 Stop_Compile : out Boolean);
633 -- Build all source files for a given main file
635 -- Current_Main_Index, if not zero, is the index of the current main unit
636 -- in its source file.
638 -- Stand_Alone_Libraries is set to True when there are Stand-Alone
639 -- Libraries, so that gnatbind is invoked with the -F switch to force
640 -- checking of elaboration flags.
642 -- Stop_Compile is set to true if we should not try to compile any more
643 -- of the main units
645 procedure Binding_Phase
646 (Stand_Alone_Libraries : Boolean := False;
647 Main_ALI_File : File_Name_Type);
648 -- Stand_Alone_Libraries should be set to True when there are Stand-Alone
649 -- Libraries, so that gnatbind is invoked with the -F switch to force
650 -- checking of elaboration flags.
652 procedure Library_Phase
653 (Stand_Alone_Libraries : in out Boolean;
654 Library_Rebuilt : in out Boolean);
655 -- Build libraries.
656 -- Stand_Alone_Libraries is set to True when there are Stand-Alone
657 -- Libraries, so that gnatbind is invoked with the -F switch to force
658 -- checking of elaboration flags.
660 procedure Linking_Phase
661 (Non_Std_Executable : Boolean := False;
662 Executable : File_Name_Type := No_File;
663 Main_ALI_File : File_Name_Type);
664 -- Perform the link of a single executable. The ali file corresponds
665 -- to Main_ALI_File. Executable is the file name of an executable.
666 -- Non_Std_Executable is set to True when there is a possibility that
667 -- the linker will not choose the correct executable file name.
669 ----------------------------------------------------
670 -- Compiler, Binder & Linker Data and Subprograms --
671 ----------------------------------------------------
673 Gcc : String_Access := Program_Name ("gcc", "gnatmake");
674 Gnatbind : String_Access := Program_Name ("gnatbind", "gnatmake");
675 Gnatlink : String_Access := Program_Name ("gnatlink", "gnatmake");
676 -- Default compiler, binder, linker programs
678 Saved_Gcc : String_Access := null;
679 Saved_Gnatbind : String_Access := null;
680 Saved_Gnatlink : String_Access := null;
681 -- Given by the command line. Will be used, if non null
683 Gcc_Path : String_Access :=
684 GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
685 Gnatbind_Path : String_Access :=
686 GNAT.OS_Lib.Locate_Exec_On_Path (Gnatbind.all);
687 Gnatlink_Path : String_Access :=
688 GNAT.OS_Lib.Locate_Exec_On_Path (Gnatlink.all);
689 -- Path for compiler, binder, linker programs, defaulted now for gnatdist.
690 -- Changed later if overridden on command line.
692 Comp_Flag : constant String_Access := new String'("-c");
693 Output_Flag : constant String_Access := new String'("-o");
694 Ada_Flag_1 : constant String_Access := new String'("-x");
695 Ada_Flag_2 : constant String_Access := new String'("ada");
696 AdaSCIL_Flag : constant String_Access := new String'("adascil");
697 No_gnat_adc : constant String_Access := new String'("-gnatA");
698 GNAT_Flag : constant String_Access := new String'("-gnatpg");
699 Do_Not_Check_Flag : constant String_Access := new String'("-x");
701 Object_Suffix : constant String := Get_Target_Object_Suffix.all;
703 Syntax_Only : Boolean := False;
704 -- Set to True when compiling with -gnats
706 Display_Executed_Programs : Boolean := True;
707 -- Set to True if name of commands should be output on stderr (or on stdout
708 -- if the Commands_To_Stdout flag was set by use of the -eS switch).
710 Output_File_Name_Seen : Boolean := False;
711 -- Set to True after having scanned the file_name for
712 -- switch "-o file_name"
714 Object_Directory_Seen : Boolean := False;
715 -- Set to True after having scanned the object directory for
716 -- switch "-D obj_dir".
718 Object_Directory_Path : String_Access := null;
719 -- The path name of the object directory, set with switch -D
721 type Make_Program_Type is (None, Compiler, Binder, Linker);
723 Program_Args : Make_Program_Type := None;
724 -- Used to indicate if we are scanning gnatmake, gcc, gnatbind, or gnatbind
725 -- options within the gnatmake command line. Used in Scan_Make_Arg only,
726 -- but must be global since value preserved from one call to another.
728 Temporary_Config_File : Boolean := False;
729 -- Set to True when there is a temporary config file used for a project
730 -- file, to avoid displaying the -gnatec switch for a temporary file.
732 procedure Add_Switches
733 (The_Package : Package_Id;
734 File_Name : String;
735 Program : Make_Program_Type;
736 Unknown_Switches_To_The_Compiler : Boolean := True;
737 Env : in out Prj.Tree.Environment);
738 procedure Add_Switch
739 (S : String_Access;
740 Program : Make_Program_Type;
741 Append_Switch : Boolean := True;
742 And_Save : Boolean := True);
743 procedure Add_Switch
744 (S : String;
745 Program : Make_Program_Type;
746 Append_Switch : Boolean := True;
747 And_Save : Boolean := True);
748 -- Make invokes one of three programs (the compiler, the binder or the
749 -- linker). For the sake of convenience, some program specific switches
750 -- can be passed directly on the gnatmake command line. This procedure
751 -- records these switches so that gnatmake can pass them to the right
752 -- program. S is the switch to be added at the end of the command line
753 -- for Program if Append_Switch is True. If Append_Switch is False S is
754 -- added at the beginning of the command line.
756 procedure Check
757 (Source_File : File_Name_Type;
758 Is_Main_Source : Boolean;
759 The_Args : Argument_List;
760 Lib_File : File_Name_Type;
761 Full_Lib_File : File_Name_Type;
762 Lib_File_Attr : access File_Attributes;
763 Read_Only : Boolean;
764 ALI : out ALI_Id;
765 O_File : out File_Name_Type;
766 O_Stamp : out Time_Stamp_Type);
767 -- Determines whether the library file Lib_File is up-to-date or not. The
768 -- full name (with path information) of the object file corresponding to
769 -- Lib_File is returned in O_File. Its time stamp is saved in O_Stamp.
770 -- ALI is the ALI_Id corresponding to Lib_File. If Lib_File in not
771 -- up-to-date, then the corresponding source file needs to be recompiled.
772 -- In this case ALI = No_ALI_Id.
773 -- Full_Lib_File must be the result of calling Osint.Full_Lib_File_Name on
774 -- Lib_File. Precomputing it saves system calls. Lib_File_Attr is the
775 -- initialized attributes of that file, which is also used to save on
776 -- system calls (it can safely be initialized to Unknown_Attributes).
778 procedure Check_Linker_Options
779 (E_Stamp : Time_Stamp_Type;
780 O_File : out File_Name_Type;
781 O_Stamp : out Time_Stamp_Type);
782 -- Checks all linker options for linker files that are newer
783 -- than E_Stamp. If such objects are found, the youngest object
784 -- is returned in O_File and its stamp in O_Stamp.
786 -- If no obsolete linker files were found, the first missing
787 -- linker file is returned in O_File and O_Stamp is empty.
788 -- Otherwise O_File is No_File.
790 procedure Collect_Arguments
791 (Source_File : File_Name_Type;
792 Is_Main_Source : Boolean;
793 Args : Argument_List);
794 -- Collect all arguments for a source to be compiled, including those
795 -- that come from a project file.
797 procedure Display (Program : String; Args : Argument_List);
798 -- Displays Program followed by the arguments in Args if variable
799 -- Display_Executed_Programs is set. The lower bound of Args must be 1.
801 procedure Report_Compilation_Failed;
802 -- Delete all temporary files and fail graciously
804 -----------------
805 -- Mapping files
806 -----------------
808 type Temp_Path_Names is array (Positive range <>) of Path_Name_Type;
809 type Temp_Path_Ptr is access Temp_Path_Names;
811 type Free_File_Indexes is array (Positive range <>) of Positive;
812 type Free_Indexes_Ptr is access Free_File_Indexes;
814 type Project_Compilation_Data is record
815 Mapping_File_Names : Temp_Path_Ptr;
816 -- The name ids of the temporary mapping files used. This is indexed
817 -- on the maximum number of compilation processes we will be spawning
818 -- (-j parameter)
820 Last_Mapping_File_Names : Natural;
821 -- Index of the last mapping file created for this project
823 Free_Mapping_File_Indexes : Free_Indexes_Ptr;
824 -- Indexes in Mapping_File_Names of the mapping file names that can be
825 -- reused for subsequent compilations.
827 Last_Free_Indexes : Natural;
828 -- Number of mapping files that can be reused
829 end record;
830 -- Information necessary when compiling a project
832 type Project_Compilation_Access is access Project_Compilation_Data;
834 package Project_Compilation_Htable is new Simple_HTable
835 (Header_Num => Prj.Header_Num,
836 Element => Project_Compilation_Access,
837 No_Element => null,
838 Key => Project_Id,
839 Hash => Prj.Hash,
840 Equal => "=");
842 Project_Compilation : Project_Compilation_Htable.Instance;
844 Gnatmake_Mapping_File : String_Access := null;
845 -- The path name of a mapping file specified by switch -C=
847 procedure Init_Mapping_File
848 (Project : Project_Id;
849 Data : in out Project_Compilation_Data;
850 File_Index : in out Natural);
851 -- Create a new temporary mapping file, and fill it with the project file
852 -- mappings, when using project file(s). The out parameter File_Index is
853 -- the index to the name of the file in the array The_Mapping_File_Names.
855 -------------------------------------------------
856 -- Subprogram declarations moved from the spec --
857 -------------------------------------------------
859 procedure Bind (ALI_File : File_Name_Type; Args : Argument_List);
860 -- Binds ALI_File. Args are the arguments to pass to the binder.
861 -- Args must have a lower bound of 1.
863 procedure Display_Commands (Display : Boolean := True);
864 -- The default behavior of Make commands (Compile_Sources, Bind, Link)
865 -- is to display them on stderr. This behavior can be changed repeatedly
866 -- by invoking this procedure.
868 -- If a compilation, bind or link failed one of the following 3 exceptions
869 -- is raised. These need to be handled by the calling routines.
871 procedure Compile_Sources
872 (Main_Source : File_Name_Type;
873 Args : Argument_List;
874 First_Compiled_File : out File_Name_Type;
875 Most_Recent_Obj_File : out File_Name_Type;
876 Most_Recent_Obj_Stamp : out Time_Stamp_Type;
877 Main_Unit : out Boolean;
878 Compilation_Failures : out Natural;
879 Main_Index : Int := 0;
880 Check_Readonly_Files : Boolean := False;
881 Do_Not_Execute : Boolean := False;
882 Force_Compilations : Boolean := False;
883 Keep_Going : Boolean := False;
884 In_Place_Mode : Boolean := False;
885 Initialize_ALI_Data : Boolean := True;
886 Max_Process : Positive := 1);
887 -- Compile_Sources will recursively compile all the sources needed by
888 -- Main_Source. Before calling this routine make sure Namet has been
889 -- initialized. This routine can be called repeatedly with different
890 -- Main_Source file as long as all the source (-I flags), library
891 -- (-B flags) and ada library (-A flags) search paths between calls are
892 -- *exactly* the same. The default directory must also be the same.
894 -- Args contains the arguments to use during the compilations.
895 -- The lower bound of Args must be 1.
897 -- First_Compiled_File is set to the name of the first file that is
898 -- compiled or that needs to be compiled. This is set to No_Name if no
899 -- compilations were needed.
901 -- Most_Recent_Obj_File is set to the full name of the most recent
902 -- object file found when no compilations are needed, that is when
903 -- First_Compiled_File is set to No_Name. When First_Compiled_File
904 -- is set then Most_Recent_Obj_File is set to No_Name.
906 -- Most_Recent_Obj_Stamp is the time stamp of Most_Recent_Obj_File.
908 -- Main_Unit is set to True if Main_Source can be a main unit.
909 -- If Do_Not_Execute is False and First_Compiled_File /= No_Name
910 -- the value of Main_Unit is always False.
911 -- Is this used any more??? It is certainly not used by gnatmake???
913 -- Compilation_Failures is a count of compilation failures. This count
914 -- is used to extract compilation failure reports with Extract_Failure.
916 -- Main_Index, when not zero, is the index of the main unit in source
917 -- file Main_Source which is a multi-unit source.
918 -- Zero indicates that Main_Source is a single unit source file.
920 -- Check_Readonly_Files set it to True to compile source files
921 -- which library files are read-only. When compiling GNAT predefined
922 -- files the "-gnatg" flag is used.
924 -- Do_Not_Execute set it to True to find out the first source that
925 -- needs to be recompiled, but without recompiling it. This file is
926 -- saved in First_Compiled_File.
928 -- Force_Compilations forces all compilations no matter what but
929 -- recompiles read-only files only if Check_Readonly_Files
930 -- is set.
932 -- Keep_Going when True keep compiling even in the presence of
933 -- compilation errors.
935 -- In_Place_Mode when True save library/object files in their object
936 -- directory if they already exist; otherwise, in the source directory.
938 -- Initialize_ALI_Data set it to True when you want to initialize ALI
939 -- data-structures. This is what you should do most of the time.
940 -- (especially the first time around when you call this routine).
941 -- This parameter is set to False to preserve previously recorded
942 -- ALI file data.
944 -- Max_Process is the maximum number of processes that should be spawned
945 -- to carry out compilations.
947 -- Flags in Package Opt Affecting Compile_Sources
948 -- -----------------------------------------------
950 -- Check_Object_Consistency set it to False to omit all consistency
951 -- checks between an .ali file and its corresponding object file.
952 -- When this flag is set to true, every time an .ali is read,
953 -- package Osint checks that the corresponding object file
954 -- exists and is more recent than the .ali.
956 -- Use of Name Table Info
957 -- ----------------------
959 -- All file names manipulated by Compile_Sources are entered into the
960 -- Names table. The Byte field of a source file is used to mark it.
962 -- Calling Compile_Sources Several Times
963 -- -------------------------------------
965 -- Upon return from Compile_Sources all the ALI data structures are left
966 -- intact for further browsing. HOWEVER upon entry to this routine ALI
967 -- data structures are re-initialized if parameter Initialize_ALI_Data
968 -- above is set to true. Typically this is what you want the first time
969 -- you call Compile_Sources. You should not load an ali file, call this
970 -- routine with flag Initialize_ALI_Data set to True and then expect
971 -- that ALI information to be around after the call. Note that the first
972 -- time you call Compile_Sources you better set Initialize_ALI_Data to
973 -- True unless you have called Initialize_ALI yourself.
975 -- Compile_Sources ALGORITHM : Compile_Sources (Main_Source)
976 -- -------------------------
978 -- 1. Insert Main_Source in a Queue (Q) and mark it.
980 -- 2. Let unit.adb be the file at the head of the Q. If unit.adb is
981 -- missing but its corresponding ali file is in an Ada library directory
982 -- (see below) then, remove unit.adb from the Q and goto step 4.
983 -- Otherwise, look at the files under the D (dependency) section of
984 -- unit.ali. If unit.ali does not exist or some of the time stamps do
985 -- not match, (re)compile unit.adb.
987 -- An Ada library directory is a directory containing Ada specs, ali
988 -- and object files but no source files for the bodies. An Ada library
989 -- directory is communicated to gnatmake by means of some switch so that
990 -- gnatmake can skip the sources whole ali are in that directory.
991 -- There are two reasons for skipping the sources in this case. Firstly,
992 -- Ada libraries typically come without full sources but binding and
993 -- linking against those libraries is still possible. Secondly, it would
994 -- be very wasteful for gnatmake to systematically check the consistency
995 -- of every external Ada library used in a program. The binder is
996 -- already in charge of catching any potential inconsistencies.
998 -- 3. Look into the W section of unit.ali and insert into the Q all
999 -- unmarked source files. Mark all files newly inserted in the Q.
1000 -- Specifically, assuming that the W section looks like
1002 -- W types%s types.adb types.ali
1003 -- W unchecked_deallocation%s
1004 -- W xref_tab%s xref_tab.adb xref_tab.ali
1006 -- Then xref_tab.adb and types.adb are inserted in the Q if they are not
1007 -- already marked.
1008 -- Note that there is no file listed under W unchecked_deallocation%s
1009 -- so no generic body should ever be explicitly compiled (unless the
1010 -- Main_Source at the start was a generic body).
1012 -- 4. Repeat steps 2 and 3 above until the Q is empty
1014 -- Note that the above algorithm works because the units withed in
1015 -- subunits are transitively included in the W section (with section) of
1016 -- the main unit. Likewise the withed units in a generic body needed
1017 -- during a compilation are also transitively included in the W section
1018 -- of the originally compiled file.
1020 procedure Initialize
1021 (Project_Node_Tree : out Project_Node_Tree_Ref;
1022 Env : out Prj.Tree.Environment);
1023 -- Performs default and package initialization. Therefore,
1024 -- Compile_Sources can be called by an external unit.
1026 procedure Link
1027 (ALI_File : File_Name_Type;
1028 Args : Argument_List;
1029 Success : out Boolean);
1030 -- Links ALI_File. Args are the arguments to pass to the linker.
1031 -- Args must have a lower bound of 1. Success indicates if the link
1032 -- succeeded or not.
1034 procedure Scan_Make_Arg
1035 (Env : in out Prj.Tree.Environment;
1036 Argv : String;
1037 And_Save : Boolean);
1038 -- Scan make arguments. Argv is a single argument to be processed.
1039 -- Project_Node_Tree will be used to initialize external references. It
1040 -- must have been initialized.
1042 -------------------
1043 -- Add_Arguments --
1044 -------------------
1046 procedure Add_Arguments (Args : Argument_List) is
1047 begin
1048 if Arguments = null then
1049 Arguments := new Argument_List (1 .. Args'Length + 10);
1051 else
1052 while Last_Argument + Args'Length > Arguments'Last loop
1053 declare
1054 New_Arguments : constant Argument_List_Access :=
1055 new Argument_List (1 .. Arguments'Last * 2);
1056 begin
1057 New_Arguments (1 .. Last_Argument) :=
1058 Arguments (1 .. Last_Argument);
1059 Arguments := New_Arguments;
1060 end;
1061 end loop;
1062 end if;
1064 Arguments (Last_Argument + 1 .. Last_Argument + Args'Length) := Args;
1065 Last_Argument := Last_Argument + Args'Length;
1066 end Add_Arguments;
1068 -- --------------------
1069 -- -- Add_Dependency --
1070 -- --------------------
1072 -- procedure Add_Dependency (S : File_Name_Type; On : File_Name_Type) is
1073 -- begin
1074 -- Dependencies.Increment_Last;
1075 -- Dependencies.Table (Dependencies.Last) := (S, On);
1076 -- end Add_Dependency;
1078 ----------------------------
1079 -- Add_Library_Search_Dir --
1080 ----------------------------
1082 procedure Add_Library_Search_Dir
1083 (Path : String;
1084 On_Command_Line : Boolean)
1086 begin
1087 if On_Command_Line then
1088 Add_Lib_Search_Dir (Normalize_Pathname (Path));
1090 else
1091 Get_Name_String (Main_Project.Directory.Display_Name);
1092 Add_Lib_Search_Dir
1093 (Normalize_Pathname (Path, Name_Buffer (1 .. Name_Len)));
1094 end if;
1095 end Add_Library_Search_Dir;
1097 --------------------
1098 -- Add_Object_Dir --
1099 --------------------
1101 procedure Add_Object_Dir (N : String) is
1102 begin
1103 Add_Lib_Search_Dir (N);
1105 if Verbose_Mode then
1106 Write_Str ("Adding object directory """);
1107 Write_Str (N);
1108 Write_Str (""".");
1109 Write_Eol;
1110 end if;
1111 end Add_Object_Dir;
1113 --------------------
1114 -- Add_Source_Dir --
1115 --------------------
1117 procedure Add_Source_Dir (N : String) is
1118 begin
1119 Add_Src_Search_Dir (N);
1121 if Verbose_Mode then
1122 Write_Str ("Adding source directory """);
1123 Write_Str (N);
1124 Write_Str (""".");
1125 Write_Eol;
1126 end if;
1127 end Add_Source_Dir;
1129 ---------------------------
1130 -- Add_Source_Search_Dir --
1131 ---------------------------
1133 procedure Add_Source_Search_Dir
1134 (Path : String;
1135 On_Command_Line : Boolean)
1137 begin
1138 if On_Command_Line then
1139 Add_Src_Search_Dir (Normalize_Pathname (Path));
1141 else
1142 Get_Name_String (Main_Project.Directory.Display_Name);
1143 Add_Src_Search_Dir
1144 (Normalize_Pathname (Path, Name_Buffer (1 .. Name_Len)));
1145 end if;
1146 end Add_Source_Search_Dir;
1148 ----------------
1149 -- Add_Switch --
1150 ----------------
1152 procedure Add_Switch
1153 (S : String_Access;
1154 Program : Make_Program_Type;
1155 Append_Switch : Boolean := True;
1156 And_Save : Boolean := True)
1158 generic
1159 with package T is new Table.Table (<>);
1160 procedure Generic_Position (New_Position : out Integer);
1161 -- Generic procedure that chooses a position for S in T at the
1162 -- beginning or the end, depending on the boolean Append_Switch.
1163 -- Calling this procedure may expand the table.
1165 ----------------------
1166 -- Generic_Position --
1167 ----------------------
1169 procedure Generic_Position (New_Position : out Integer) is
1170 begin
1171 T.Increment_Last;
1173 if Append_Switch then
1174 New_Position := Integer (T.Last);
1175 else
1176 for J in reverse T.Table_Index_Type'Succ (T.First) .. T.Last loop
1177 T.Table (J) := T.Table (T.Table_Index_Type'Pred (J));
1178 end loop;
1180 New_Position := Integer (T.First);
1181 end if;
1182 end Generic_Position;
1184 procedure Gcc_Switches_Pos is new Generic_Position (Gcc_Switches);
1185 procedure Binder_Switches_Pos is new Generic_Position (Binder_Switches);
1186 procedure Linker_Switches_Pos is new Generic_Position (Linker_Switches);
1188 procedure Saved_Gcc_Switches_Pos is new
1189 Generic_Position (Saved_Gcc_Switches);
1191 procedure Saved_Binder_Switches_Pos is new
1192 Generic_Position (Saved_Binder_Switches);
1194 procedure Saved_Linker_Switches_Pos is new
1195 Generic_Position (Saved_Linker_Switches);
1197 New_Position : Integer;
1199 -- Start of processing for Add_Switch
1201 begin
1202 if And_Save then
1203 case Program is
1204 when Compiler =>
1205 Saved_Gcc_Switches_Pos (New_Position);
1206 Saved_Gcc_Switches.Table (New_Position) := S;
1208 when Binder =>
1209 Saved_Binder_Switches_Pos (New_Position);
1210 Saved_Binder_Switches.Table (New_Position) := S;
1212 when Linker =>
1213 Saved_Linker_Switches_Pos (New_Position);
1214 Saved_Linker_Switches.Table (New_Position) := S;
1216 when None =>
1217 raise Program_Error;
1218 end case;
1220 else
1221 case Program is
1222 when Compiler =>
1223 Gcc_Switches_Pos (New_Position);
1224 Gcc_Switches.Table (New_Position) := S;
1226 when Binder =>
1227 Binder_Switches_Pos (New_Position);
1228 Binder_Switches.Table (New_Position) := S;
1230 when Linker =>
1231 Linker_Switches_Pos (New_Position);
1232 Linker_Switches.Table (New_Position) := S;
1234 when None =>
1235 raise Program_Error;
1236 end case;
1237 end if;
1238 end Add_Switch;
1240 procedure Add_Switch
1241 (S : String;
1242 Program : Make_Program_Type;
1243 Append_Switch : Boolean := True;
1244 And_Save : Boolean := True)
1246 begin
1247 Add_Switch (S => new String'(S),
1248 Program => Program,
1249 Append_Switch => Append_Switch,
1250 And_Save => And_Save);
1251 end Add_Switch;
1253 ------------------
1254 -- Add_Switches --
1255 ------------------
1257 procedure Add_Switches
1258 (The_Package : Package_Id;
1259 File_Name : String;
1260 Program : Make_Program_Type;
1261 Unknown_Switches_To_The_Compiler : Boolean := True;
1262 Env : in out Prj.Tree.Environment)
1264 Switches : Variable_Value;
1265 Switch_List : String_List_Id;
1266 Element : String_Element;
1268 begin
1269 Switch_May_Be_Passed_To_The_Compiler :=
1270 Unknown_Switches_To_The_Compiler;
1272 if File_Name'Length > 0 then
1273 Name_Len := 0;
1274 Add_Str_To_Name_Buffer (File_Name);
1275 Switches :=
1276 Switches_Of
1277 (Source_File => Name_Find,
1278 Project => Main_Project,
1279 In_Package => The_Package,
1280 Allow_ALI => Program = Binder or else Program = Linker);
1282 if Switches.Kind = List then
1283 Program_Args := Program;
1285 Switch_List := Switches.Values;
1286 while Switch_List /= Nil_String loop
1287 Element :=
1288 Project_Tree.Shared.String_Elements.Table (Switch_List);
1289 Get_Name_String (Element.Value);
1291 if Name_Len > 0 then
1292 declare
1293 Argv : constant String := Name_Buffer (1 .. Name_Len);
1294 -- We need a copy, because Name_Buffer may be modified
1296 begin
1297 if Verbose_Mode then
1298 Write_Str (" Adding ");
1299 Write_Line (Argv);
1300 end if;
1302 Scan_Make_Arg (Env, Argv, And_Save => False);
1304 if not Gnatmake_Switch_Found
1305 and then not Switch_May_Be_Passed_To_The_Compiler
1306 then
1307 Errutil.Error_Msg
1308 ('"' & Argv &
1309 """ is not a gnatmake switch. Consider moving "
1310 & "it to Global_Compilation_Switches.",
1311 Element.Location);
1312 Make_Failed ("*** illegal switch """ & Argv & """");
1313 end if;
1314 end;
1315 end if;
1317 Switch_List := Element.Next;
1318 end loop;
1319 end if;
1320 end if;
1321 end Add_Switches;
1323 ----------
1324 -- Bind --
1325 ----------
1327 procedure Bind (ALI_File : File_Name_Type; Args : Argument_List) is
1328 Bind_Args : Argument_List (1 .. Args'Last + 2);
1329 Bind_Last : Integer;
1330 Success : Boolean;
1332 begin
1333 pragma Assert (Args'First = 1);
1335 -- Optimize the simple case where the gnatbind command line looks like
1336 -- gnatbind -aO. -I- file.ali
1337 -- into
1338 -- gnatbind file.adb
1340 if Args'Length = 2
1341 and then Args (Args'First).all = "-aO" & Normalized_CWD
1342 and then Args (Args'Last).all = "-I-"
1343 and then ALI_File = Strip_Directory (ALI_File)
1344 then
1345 Bind_Last := Args'First - 1;
1347 else
1348 Bind_Last := Args'Last;
1349 Bind_Args (Args'Range) := Args;
1350 end if;
1352 -- It is completely pointless to re-check source file time stamps. This
1353 -- has been done already by gnatmake
1355 Bind_Last := Bind_Last + 1;
1356 Bind_Args (Bind_Last) := Do_Not_Check_Flag;
1358 Get_Name_String (ALI_File);
1360 Bind_Last := Bind_Last + 1;
1361 Bind_Args (Bind_Last) := new String'(Name_Buffer (1 .. Name_Len));
1363 GNAT.OS_Lib.Normalize_Arguments (Bind_Args (Args'First .. Bind_Last));
1365 Display (Gnatbind.all, Bind_Args (Args'First .. Bind_Last));
1367 if Gnatbind_Path = null then
1368 Make_Failed ("error, unable to locate " & Gnatbind.all);
1369 end if;
1371 GNAT.OS_Lib.Spawn
1372 (Gnatbind_Path.all, Bind_Args (Args'First .. Bind_Last), Success);
1374 if not Success then
1375 Make_Failed ("*** bind failed.");
1376 end if;
1377 end Bind;
1379 --------------------------------
1380 -- Change_To_Object_Directory --
1381 --------------------------------
1383 procedure Change_To_Object_Directory (Project : Project_Id) is
1384 Object_Directory : Path_Name_Type;
1386 begin
1387 pragma Assert (Project /= No_Project);
1389 -- Nothing to do if the current working directory is already the correct
1390 -- object directory.
1392 if Project_Of_Current_Object_Directory /= Project then
1393 Project_Of_Current_Object_Directory := Project;
1394 Object_Directory := Project.Object_Directory.Display_Name;
1396 -- Set the working directory to the object directory of the actual
1397 -- project.
1399 if Verbose_Mode then
1400 Write_Str ("Changing to object directory of """);
1401 Write_Name (Project.Display_Name);
1402 Write_Str (""": """);
1403 Write_Name (Object_Directory);
1404 Write_Line ("""");
1405 end if;
1407 Change_Dir (Get_Name_String (Object_Directory));
1408 end if;
1410 exception
1411 -- Fail if unable to change to the object directory
1413 when Directory_Error =>
1414 Make_Failed ("unable to change to object directory """ &
1415 Path_Or_File_Name
1416 (Project.Object_Directory.Display_Name) &
1417 """ of project " &
1418 Get_Name_String (Project.Display_Name));
1419 end Change_To_Object_Directory;
1421 -----------
1422 -- Check --
1423 -----------
1425 procedure Check
1426 (Source_File : File_Name_Type;
1427 Is_Main_Source : Boolean;
1428 The_Args : Argument_List;
1429 Lib_File : File_Name_Type;
1430 Full_Lib_File : File_Name_Type;
1431 Lib_File_Attr : access File_Attributes;
1432 Read_Only : Boolean;
1433 ALI : out ALI_Id;
1434 O_File : out File_Name_Type;
1435 O_Stamp : out Time_Stamp_Type)
1437 function First_New_Spec (A : ALI_Id) return File_Name_Type;
1438 -- Looks in the with table entries of A and returns the spec file name
1439 -- of the first withed unit (subprogram) for which no spec existed when
1440 -- A was generated but for which there exists one now, implying that A
1441 -- is now obsolete. If no such unit is found No_File is returned.
1442 -- Otherwise the spec file name of the unit is returned.
1444 -- **WARNING** in the event of Uname format modifications, one *MUST*
1445 -- make sure this function is also updated.
1447 -- Note: This function should really be in ali.adb and use Uname
1448 -- services, but this causes the whole compiler to be dragged along
1449 -- for gnatbind and gnatmake.
1451 --------------------
1452 -- First_New_Spec --
1453 --------------------
1455 function First_New_Spec (A : ALI_Id) return File_Name_Type is
1456 Spec_File_Name : File_Name_Type := No_File;
1458 function New_Spec (Uname : Unit_Name_Type) return Boolean;
1459 -- Uname is the name of the spec or body of some ada unit. This
1460 -- function returns True if the Uname is the name of a body which has
1461 -- a spec not mentioned in ALI file A. If True is returned
1462 -- Spec_File_Name above is set to the name of this spec file.
1464 --------------
1465 -- New_Spec --
1466 --------------
1468 function New_Spec (Uname : Unit_Name_Type) return Boolean is
1469 Spec_Name : Unit_Name_Type;
1470 File_Name : File_Name_Type;
1472 begin
1473 -- Test whether Uname is the name of a body unit (i.e. ends
1474 -- with %b).
1476 Get_Name_String (Uname);
1477 pragma
1478 Assert (Name_Len > 2 and then Name_Buffer (Name_Len - 1) = '%');
1480 if Name_Buffer (Name_Len) /= 'b' then
1481 return False;
1482 end if;
1484 -- Convert unit name into spec name
1486 -- ??? this code seems dubious in presence of pragma
1487 -- Source_File_Name since there is no more direct relationship
1488 -- between unit name and file name.
1490 -- ??? Further, what about alternative subunit naming
1492 Name_Buffer (Name_Len) := 's';
1493 Spec_Name := Name_Find;
1494 File_Name := Get_File_Name (Spec_Name, Subunit => False);
1496 -- Look if File_Name is mentioned in A's sdep list.
1497 -- If not look if the file exists. If it does return True.
1499 for D in
1500 ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep
1501 loop
1502 if Sdep.Table (D).Sfile = File_Name then
1503 return False;
1504 end if;
1505 end loop;
1507 if Full_Source_Name (File_Name) /= No_File then
1508 Spec_File_Name := File_Name;
1509 return True;
1510 end if;
1512 return False;
1513 end New_Spec;
1515 -- Start of processing for First_New_Spec
1517 begin
1518 U_Chk : for U in
1519 ALIs.Table (A).First_Unit .. ALIs.Table (A).Last_Unit
1520 loop
1521 exit U_Chk when Units.Table (U).Utype = Is_Body_Only
1522 and then New_Spec (Units.Table (U).Uname);
1524 for W in Units.Table (U).First_With
1526 Units.Table (U).Last_With
1527 loop
1528 exit U_Chk when
1529 Withs.Table (W).Afile /= No_File
1530 and then New_Spec (Withs.Table (W).Uname);
1531 end loop;
1532 end loop U_Chk;
1534 return Spec_File_Name;
1535 end First_New_Spec;
1537 ---------------------------------
1538 -- Data declarations for Check --
1539 ---------------------------------
1541 Full_Obj_File : File_Name_Type;
1542 -- Full name of the object file corresponding to Lib_File
1544 Lib_Stamp : Time_Stamp_Type;
1545 -- Time stamp of the current ada library file
1547 Obj_Stamp : Time_Stamp_Type;
1548 -- Time stamp of the current object file
1550 Modified_Source : File_Name_Type;
1551 -- The first source in Lib_File whose current time stamp differs from
1552 -- that stored in Lib_File.
1554 New_Spec : File_Name_Type;
1555 -- If Lib_File contains in its W (with) section a body (for a
1556 -- subprogram) for which there exists a spec, and the spec did not
1557 -- appear in the Sdep section of Lib_File, New_Spec contains the file
1558 -- name of this new spec.
1560 Source_Name : File_Name_Type;
1561 Text : Text_Buffer_Ptr;
1563 First_Arg : Arg_Id;
1564 -- Index of the first argument in Args.Table for a given unit
1566 Last_Arg : Arg_Id;
1567 -- Index of the last argument in Args.Table for a given unit
1569 Arg : Arg_Id := Arg_Id'First;
1570 -- Current index in Args.Table for a given unit (init to stop warning)
1572 Number_Of_Switches : Natural;
1573 -- Number of switches recorded for a given unit
1575 Prev_Switch : String_Access;
1576 -- Previous switch processed
1578 Switch_Found : Boolean;
1579 -- True if a given switch has been found
1581 ALI_Project : Project_Id;
1582 -- If the ALI file is in the object directory of a project, this is
1583 -- the project id.
1585 -- Start of processing for Check
1587 begin
1588 pragma Assert (Lib_File /= No_File);
1590 -- If ALI file is read-only, temporarily set Check_Object_Consistency to
1591 -- False. We don't care if the object file is not there (presumably a
1592 -- library will be used for linking.)
1594 if Read_Only then
1595 declare
1596 Saved_Check_Object_Consistency : constant Boolean :=
1597 Check_Object_Consistency;
1598 begin
1599 Check_Object_Consistency := False;
1600 Text := Read_Library_Info_From_Full (Full_Lib_File, Lib_File_Attr);
1601 Check_Object_Consistency := Saved_Check_Object_Consistency;
1602 end;
1604 else
1605 Text := Read_Library_Info_From_Full (Full_Lib_File, Lib_File_Attr);
1606 end if;
1608 Full_Obj_File := Full_Object_File_Name;
1609 Lib_Stamp := Current_Library_File_Stamp;
1610 Obj_Stamp := Current_Object_File_Stamp;
1612 if Full_Lib_File = No_File then
1613 Verbose_Msg
1614 (Lib_File,
1615 "being checked ...",
1616 Prefix => " ",
1617 Minimum_Verbosity => Opt.Medium);
1618 else
1619 Verbose_Msg
1620 (Full_Lib_File,
1621 "being checked ...",
1622 Prefix => " ",
1623 Minimum_Verbosity => Opt.Medium);
1624 end if;
1626 ALI := No_ALI_Id;
1627 O_File := Full_Obj_File;
1628 O_Stamp := Obj_Stamp;
1630 if Text = null then
1631 if Full_Lib_File = No_File then
1632 Verbose_Msg (Lib_File, "missing.");
1634 elsif Obj_Stamp (Obj_Stamp'First) = ' ' then
1635 Verbose_Msg (Full_Obj_File, "missing.");
1637 else
1638 Verbose_Msg
1639 (Full_Lib_File, "(" & String (Lib_Stamp) & ") newer than",
1640 Full_Obj_File, "(" & String (Obj_Stamp) & ")");
1641 end if;
1643 else
1644 ALI := Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
1645 Free (Text);
1647 if ALI = No_ALI_Id then
1648 Verbose_Msg (Full_Lib_File, "incorrectly formatted ALI file");
1649 return;
1651 elsif ALIs.Table (ALI).Ver (1 .. ALIs.Table (ALI).Ver_Len) /=
1652 Verbose_Library_Version
1653 then
1654 Verbose_Msg (Full_Lib_File, "compiled with old GNAT version");
1655 ALI := No_ALI_Id;
1656 return;
1657 end if;
1659 -- Don't take ALI file into account if it was generated with errors
1661 if ALIs.Table (ALI).Compile_Errors then
1662 Verbose_Msg (Full_Lib_File, "had errors, must be recompiled");
1663 ALI := No_ALI_Id;
1664 return;
1665 end if;
1667 -- Don't take ALI file into account if no object was generated
1669 if Operating_Mode /= Check_Semantics
1670 and then ALIs.Table (ALI).No_Object
1671 then
1672 Verbose_Msg (Full_Lib_File, "has no corresponding object");
1673 ALI := No_ALI_Id;
1674 return;
1675 end if;
1677 -- When compiling with -gnatc, don't take ALI file into account if
1678 -- it has not been generated for the current source, for example if
1679 -- it has been generated for the spec, but we are compiling the body.
1681 if Operating_Mode = Check_Semantics then
1682 declare
1683 File_Name : String := Get_Name_String (Source_File);
1684 OK : Boolean := False;
1686 begin
1687 -- In the ALI file, the source file names are in canonical case
1689 Canonical_Case_File_Name (File_Name);
1691 for U in ALIs.Table (ALI).First_Unit ..
1692 ALIs.Table (ALI).Last_Unit
1693 loop
1694 OK := Get_Name_String (Units.Table (U).Sfile) = File_Name;
1695 exit when OK;
1696 end loop;
1698 if not OK then
1699 Verbose_Msg
1700 (Full_Lib_File, "not generated for the same source");
1701 ALI := No_ALI_Id;
1702 return;
1703 end if;
1704 end;
1705 end if;
1707 -- Check for matching compiler switches if needed
1709 if Check_Switches then
1711 -- First, collect all the switches
1713 Collect_Arguments (Source_File, Is_Main_Source, The_Args);
1714 Prev_Switch := Dummy_Switch;
1715 Get_Name_String (ALIs.Table (ALI).Sfile);
1716 Switches_To_Check.Set_Last (0);
1718 for J in 1 .. Last_Argument loop
1720 -- Skip -c, -I and -o switches
1722 if Arguments (J) (1) = '-'
1723 and then Arguments (J) (2) /= 'c'
1724 and then Arguments (J) (2) /= 'o'
1725 and then Arguments (J) (2) /= 'I'
1726 then
1727 Normalize_Compiler_Switches
1728 (Arguments (J).all,
1729 Normalized_Switches,
1730 Last_Norm_Switch);
1732 for K in 1 .. Last_Norm_Switch loop
1733 Switches_To_Check.Increment_Last;
1734 Switches_To_Check.Table (Switches_To_Check.Last) :=
1735 Normalized_Switches (K);
1736 end loop;
1737 end if;
1738 end loop;
1740 First_Arg := Units.Table (ALIs.Table (ALI).First_Unit).First_Arg;
1741 Last_Arg := Units.Table (ALIs.Table (ALI).First_Unit).Last_Arg;
1743 for J in 1 .. Switches_To_Check.Last loop
1745 -- Comparing switches is delicate because gcc reorders a number
1746 -- of switches, according to lang-specs.h, but gnatmake doesn't
1747 -- have sufficient knowledge to perform the same reordering.
1748 -- Instead, we ignore orders between different "first letter"
1749 -- switches, but keep orders between same switches, e.g -O -O2
1750 -- is different than -O2 -O, but -g -O is equivalent to -O -g.
1752 if Switches_To_Check.Table (J) (2) /= Prev_Switch (2) or else
1753 (Prev_Switch'Length >= 6 and then
1754 Prev_Switch (2 .. 5) = "gnat" and then
1755 Switches_To_Check.Table (J)'Length >= 6 and then
1756 Switches_To_Check.Table (J) (2 .. 5) = "gnat" and then
1757 Prev_Switch (6) /= Switches_To_Check.Table (J) (6))
1758 then
1759 Prev_Switch := Switches_To_Check.Table (J);
1760 Arg := First_Arg;
1761 end if;
1763 Switch_Found := False;
1765 for K in Arg .. Last_Arg loop
1767 Switches_To_Check.Table (J).all = Args.Table (K).all
1768 then
1769 Arg := K + 1;
1770 Switch_Found := True;
1771 exit;
1772 end if;
1773 end loop;
1775 if not Switch_Found then
1776 if Verbose_Mode then
1777 Verbose_Msg (ALIs.Table (ALI).Sfile,
1778 "switch mismatch """ &
1779 Switches_To_Check.Table (J).all & '"');
1780 end if;
1782 ALI := No_ALI_Id;
1783 return;
1784 end if;
1785 end loop;
1787 Number_Of_Switches := Natural (Last_Arg - First_Arg + 1);
1789 -- Do not count the multilib switches reinstated by the compiler
1790 -- according to the lang-specs.h.settings.
1792 for K in First_Arg .. Last_Arg loop
1793 if Args.Table (K).all = "-mrtp" then
1794 Number_Of_Switches := Number_Of_Switches - 1;
1795 end if;
1796 end loop;
1798 if Switches_To_Check.Last /= Number_Of_Switches then
1799 if Verbose_Mode then
1800 Verbose_Msg (ALIs.Table (ALI).Sfile,
1801 "different number of switches");
1803 for K in First_Arg .. Last_Arg loop
1804 Write_Str (Args.Table (K).all);
1805 Write_Char (' ');
1806 end loop;
1808 Write_Eol;
1810 for J in 1 .. Switches_To_Check.Last loop
1811 Write_Str (Switches_To_Check.Table (J).all);
1812 Write_Char (' ');
1813 end loop;
1815 Write_Eol;
1816 end if;
1818 ALI := No_ALI_Id;
1819 return;
1820 end if;
1821 end if;
1823 -- Get the source files and their message digests. Note that some
1824 -- sources may be missing if ALI is out-of-date.
1826 Set_Source_Table (ALI);
1828 Modified_Source := Time_Stamp_Mismatch (ALI, Read_Only);
1830 -- To avoid using too much memory when switch -m is used, free the
1831 -- memory allocated for the source file when computing the checksum.
1833 if Minimal_Recompilation then
1834 Sinput.P.Clear_Source_File_Table;
1835 end if;
1837 if Modified_Source /= No_File then
1838 ALI := No_ALI_Id;
1840 if Verbose_Mode then
1841 Source_Name := Full_Source_Name (Modified_Source);
1843 if Source_Name /= No_File then
1844 Verbose_Msg (Source_Name, "time stamp mismatch");
1845 else
1846 Verbose_Msg (Modified_Source, "missing");
1847 end if;
1848 end if;
1850 else
1851 New_Spec := First_New_Spec (ALI);
1853 if New_Spec /= No_File then
1854 ALI := No_ALI_Id;
1856 if Verbose_Mode then
1857 Source_Name := Full_Source_Name (New_Spec);
1859 if Source_Name /= No_File then
1860 Verbose_Msg (Source_Name, "new spec");
1861 else
1862 Verbose_Msg (New_Spec, "old spec missing");
1863 end if;
1864 end if;
1866 elsif not Read_Only and then Main_Project /= No_Project then
1867 declare
1868 Uname : constant Name_Id :=
1869 Check_Source_Info_In_ALI (ALI, Project_Tree);
1871 Udata : Prj.Unit_Index;
1873 begin
1874 if Uname = No_Name then
1875 ALI := No_ALI_Id;
1876 return;
1877 end if;
1879 -- Check that ALI file is in the correct object directory.
1880 -- If it is in the object directory of a project that is
1881 -- extended and it depends on a source that is in one of
1882 -- its extending projects, then the ALI file is not in the
1883 -- correct object directory.
1885 -- First, find the project of this ALI file. As there may be
1886 -- several projects with the same object directory, we first
1887 -- need to find the project of the source.
1889 ALI_Project := No_Project;
1891 Udata := Units_Htable.Get (Project_Tree.Units_HT, Uname);
1893 if Udata /= No_Unit_Index then
1894 if Udata.File_Names (Impl) /= null
1895 and then Udata.File_Names (Impl).File = Source_File
1896 then
1897 ALI_Project := Udata.File_Names (Impl).Project;
1899 elsif Udata.File_Names (Spec) /= null
1900 and then Udata.File_Names (Spec).File = Source_File
1901 then
1902 ALI_Project := Udata.File_Names (Spec).Project;
1903 end if;
1904 end if;
1905 end;
1907 if ALI_Project = No_Project then
1908 return;
1909 end if;
1911 declare
1912 Obj_Dir : Path_Name_Type;
1913 Res_Obj_Dir : constant String :=
1914 Normalize_Pathname
1915 (Dir_Name
1916 (Get_Name_String (Full_Lib_File)),
1917 Resolve_Links =>
1918 Opt.Follow_Links_For_Dirs,
1919 Case_Sensitive => False);
1921 begin
1922 Name_Len := 0;
1923 Add_Str_To_Name_Buffer (Res_Obj_Dir);
1925 if not Is_Directory_Separator (Name_Buffer (Name_Len)) then
1926 Add_Char_To_Name_Buffer (Directory_Separator);
1927 end if;
1929 Obj_Dir := Name_Find;
1931 while ALI_Project /= No_Project
1932 and then Obj_Dir /= ALI_Project.Object_Directory.Name
1933 loop
1934 ALI_Project := ALI_Project.Extended_By;
1935 end loop;
1936 end;
1938 if ALI_Project = No_Project then
1939 ALI := No_ALI_Id;
1941 Verbose_Msg (Lib_File, " wrong object directory");
1942 return;
1943 end if;
1945 -- If the ALI project is not extended, then it must be in
1946 -- the correct object directory.
1948 if ALI_Project.Extended_By = No_Project then
1949 return;
1950 end if;
1952 -- Count the extending projects
1954 declare
1955 Num_Ext : Natural;
1956 Proj : Project_Id;
1958 begin
1959 Num_Ext := 0;
1960 Proj := ALI_Project;
1961 loop
1962 Proj := Proj.Extended_By;
1963 exit when Proj = No_Project;
1964 Num_Ext := Num_Ext + 1;
1965 end loop;
1967 -- Make a list of the extending projects
1969 declare
1970 Projects : array (1 .. Num_Ext) of Project_Id;
1971 Dep : Sdep_Record;
1972 OK : Boolean := True;
1973 UID : Unit_Index;
1975 begin
1976 Proj := ALI_Project;
1977 for J in Projects'Range loop
1978 Proj := Proj.Extended_By;
1979 Projects (J) := Proj;
1980 end loop;
1982 -- Now check if any of the dependant sources are in any
1983 -- of these extending projects.
1985 D_Chk :
1986 for D in ALIs.Table (ALI).First_Sdep ..
1987 ALIs.Table (ALI).Last_Sdep
1988 loop
1989 Dep := Sdep.Table (D);
1990 UID := Units_Htable.Get_First (Project_Tree.Units_HT);
1991 Proj := No_Project;
1993 Unit_Loop :
1994 while UID /= null loop
1995 if UID.File_Names (Impl) /= null
1996 and then UID.File_Names (Impl).File = Dep.Sfile
1997 then
1998 Proj := UID.File_Names (Impl).Project;
2000 elsif UID.File_Names (Spec) /= null
2001 and then UID.File_Names (Spec).File = Dep.Sfile
2002 then
2003 Proj := UID.File_Names (Spec).Project;
2004 end if;
2006 -- If a source is in a project, check if it is one
2007 -- in the list.
2009 if Proj /= No_Project then
2010 for J in Projects'Range loop
2011 if Proj = Projects (J) then
2012 OK := False;
2013 exit D_Chk;
2014 end if;
2015 end loop;
2017 exit Unit_Loop;
2018 end if;
2020 UID :=
2021 Units_Htable.Get_Next (Project_Tree.Units_HT);
2022 end loop Unit_Loop;
2023 end loop D_Chk;
2025 -- If one of the dependent sources is in one project of
2026 -- the list, then we must recompile.
2028 if not OK then
2029 ALI := No_ALI_Id;
2030 Verbose_Msg (Lib_File, " wrong object directory");
2031 end if;
2032 end;
2033 end;
2034 end if;
2035 end if;
2036 end if;
2037 end Check;
2039 ------------------------
2040 -- Check_For_S_Switch --
2041 ------------------------
2043 procedure Check_For_S_Switch is
2044 begin
2045 -- By default, we generate an object file
2047 Output_Is_Object := True;
2049 for Arg in 1 .. Last_Argument loop
2050 if Arguments (Arg).all = "-S" then
2051 Output_Is_Object := False;
2053 elsif Arguments (Arg).all = "-c" then
2054 Output_Is_Object := True;
2055 end if;
2056 end loop;
2057 end Check_For_S_Switch;
2059 --------------------------
2060 -- Check_Linker_Options --
2061 --------------------------
2063 procedure Check_Linker_Options
2064 (E_Stamp : Time_Stamp_Type;
2065 O_File : out File_Name_Type;
2066 O_Stamp : out Time_Stamp_Type)
2068 procedure Check_File (File : File_Name_Type);
2069 -- Update O_File and O_Stamp if the given file is younger than E_Stamp
2070 -- and O_Stamp, or if O_File is No_File and File does not exist.
2072 function Get_Library_File (Name : String) return File_Name_Type;
2073 -- Return the full file name including path of a library based
2074 -- on the name specified with the -l linker option, using the
2075 -- Ada object path. Return No_File if no such file can be found.
2077 type Char_Array is array (Natural) of Character;
2078 type Char_Array_Access is access constant Char_Array;
2080 Template : Char_Array_Access;
2081 pragma Import (C, Template, "__gnat_library_template");
2083 ----------------
2084 -- Check_File --
2085 ----------------
2087 procedure Check_File (File : File_Name_Type) is
2088 Stamp : Time_Stamp_Type;
2089 Name : File_Name_Type := File;
2091 begin
2092 Get_Name_String (Name);
2094 -- Remove any trailing NUL characters
2096 while Name_Len >= Name_Buffer'First
2097 and then Name_Buffer (Name_Len) = NUL
2098 loop
2099 Name_Len := Name_Len - 1;
2100 end loop;
2102 if Name_Len = 0 then
2103 return;
2105 elsif Name_Buffer (1) = '-' then
2107 -- Do not check if File is a switch other than "-l"
2109 if Name_Buffer (2) /= 'l' then
2110 return;
2111 end if;
2113 -- The argument is a library switch, get actual name. It
2114 -- is necessary to make a copy of the relevant part of
2115 -- Name_Buffer as Get_Library_Name uses Name_Buffer as well.
2117 declare
2118 Base_Name : constant String := Name_Buffer (3 .. Name_Len);
2120 begin
2121 Name := Get_Library_File (Base_Name);
2122 end;
2124 if Name = No_File then
2125 return;
2126 end if;
2127 end if;
2129 Stamp := File_Stamp (Name);
2131 -- Find the youngest object file that is younger than the
2132 -- executable. If no such file exist, record the first object
2133 -- file that is not found.
2135 if (O_Stamp < Stamp and then E_Stamp < Stamp)
2136 or else (O_File = No_File and then Stamp (Stamp'First) = ' ')
2137 then
2138 O_Stamp := Stamp;
2139 O_File := Name;
2141 -- Strip the trailing NUL if present
2143 Get_Name_String (O_File);
2145 if Name_Buffer (Name_Len) = NUL then
2146 Name_Len := Name_Len - 1;
2147 O_File := Name_Find;
2148 end if;
2149 end if;
2150 end Check_File;
2152 ----------------------
2153 -- Get_Library_Name --
2154 ----------------------
2156 -- See comments in a-adaint.c about template syntax
2158 function Get_Library_File (Name : String) return File_Name_Type is
2159 File : File_Name_Type := No_File;
2161 begin
2162 Name_Len := 0;
2164 for Ptr in Template'Range loop
2165 case Template (Ptr) is
2166 when '*' =>
2167 Add_Str_To_Name_Buffer (Name);
2169 when ';' =>
2170 File := Full_Lib_File_Name (Name_Find);
2171 exit when File /= No_File;
2172 Name_Len := 0;
2174 when NUL =>
2175 exit;
2177 when others =>
2178 Add_Char_To_Name_Buffer (Template (Ptr));
2179 end case;
2180 end loop;
2182 -- The for loop exited because the end of the template
2183 -- was reached. File contains the last possible file name
2184 -- for the library.
2186 if File = No_File and then Name_Len > 0 then
2187 File := Full_Lib_File_Name (Name_Find);
2188 end if;
2190 return File;
2191 end Get_Library_File;
2193 -- Start of processing for Check_Linker_Options
2195 begin
2196 O_File := No_File;
2197 O_Stamp := (others => ' ');
2199 -- Process linker options from the ALI files
2201 for Opt in 1 .. Linker_Options.Last loop
2202 Check_File (File_Name_Type (Linker_Options.Table (Opt).Name));
2203 end loop;
2205 -- Process options given on the command line
2207 for Opt in Linker_Switches.First .. Linker_Switches.Last loop
2209 -- Check if the previous Opt has one of the two switches
2210 -- that take an extra parameter. (See GCC manual.)
2212 if Opt = Linker_Switches.First
2213 or else (Linker_Switches.Table (Opt - 1).all /= "-u"
2214 and then
2215 Linker_Switches.Table (Opt - 1).all /= "-Xlinker"
2216 and then
2217 Linker_Switches.Table (Opt - 1).all /= "-L")
2218 then
2219 Name_Len := 0;
2220 Add_Str_To_Name_Buffer (Linker_Switches.Table (Opt).all);
2221 Check_File (Name_Find);
2222 end if;
2223 end loop;
2224 end Check_Linker_Options;
2226 -----------------
2227 -- Check_Steps --
2228 -----------------
2230 procedure Check_Steps is
2231 begin
2232 -- If either -c, -b or -l has been specified, we will not necessarily
2233 -- execute all steps.
2235 if Make_Steps then
2236 Do_Compile_Step := Do_Compile_Step and Compile_Only;
2237 Do_Bind_Step := Do_Bind_Step and Bind_Only;
2238 Do_Link_Step := Do_Link_Step and Link_Only;
2240 -- If -c has been specified, but not -b, ignore any potential -l
2242 if Do_Compile_Step and then not Do_Bind_Step then
2243 Do_Link_Step := False;
2244 end if;
2245 end if;
2246 end Check_Steps;
2248 -----------------------
2249 -- Collect_Arguments --
2250 -----------------------
2252 procedure Collect_Arguments
2253 (Source_File : File_Name_Type;
2254 Is_Main_Source : Boolean;
2255 Args : Argument_List)
2257 pragma Unreferenced (Is_Main_Source);
2259 begin
2260 Arguments_Project := No_Project;
2261 Last_Argument := 0;
2262 Add_Arguments (Args);
2264 if Main_Project /= No_Project then
2265 declare
2266 Source_File_Name : constant String :=
2267 Get_Name_String (Source_File);
2268 Compiler_Package : Prj.Package_Id;
2269 Switches : Prj.Variable_Value;
2271 begin
2272 Prj.Env.
2273 Get_Reference
2274 (Source_File_Name => Source_File_Name,
2275 Project => Arguments_Project,
2276 Path => Arguments_Path_Name,
2277 In_Tree => Project_Tree);
2279 -- If the source is not a source of a project file, add the
2280 -- recorded arguments. Check will be done later if the source
2281 -- need to be compiled that the switch -x has been used.
2283 if Arguments_Project = No_Project then
2284 Add_Arguments (The_Saved_Gcc_Switches.all);
2286 elsif not Arguments_Project.Externally_Built or else Must_Compile
2287 then
2288 -- We get the project directory for the relative path
2289 -- switches and arguments.
2291 Arguments_Project :=
2292 Ultimate_Extending_Project_Of (Arguments_Project);
2294 -- If building a dynamic or relocatable library, compile with
2295 -- PIC option, if it exists.
2297 if Arguments_Project.Library
2298 and then Arguments_Project.Library_Kind /= Static
2299 then
2300 declare
2301 PIC : constant String := MLib.Tgt.PIC_Option;
2302 begin
2303 if PIC /= "" then
2304 Add_Arguments ((1 => new String'(PIC)));
2305 end if;
2306 end;
2307 end if;
2309 -- We now look for package Compiler and get the switches from
2310 -- this package.
2312 Compiler_Package :=
2313 Prj.Util.Value_Of
2314 (Name => Name_Compiler,
2315 In_Packages => Arguments_Project.Decl.Packages,
2316 Shared => Project_Tree.Shared);
2318 if Compiler_Package /= No_Package then
2320 -- If package Gnatmake.Compiler exists, we get the specific
2321 -- switches for the current source, or the global switches,
2322 -- if any.
2324 Switches :=
2325 Switches_Of
2326 (Source_File => Source_File,
2327 Project => Arguments_Project,
2328 In_Package => Compiler_Package,
2329 Allow_ALI => False);
2331 end if;
2333 case Switches.Kind is
2335 -- We have a list of switches. We add these switches,
2336 -- plus the saved gcc switches.
2338 when List =>
2339 declare
2340 Current : String_List_Id := Switches.Values;
2341 Element : String_Element;
2342 Number : Natural := 0;
2344 begin
2345 while Current /= Nil_String loop
2346 Element := Project_Tree.Shared.String_Elements.
2347 Table (Current);
2348 Number := Number + 1;
2349 Current := Element.Next;
2350 end loop;
2352 declare
2353 New_Args : Argument_List (1 .. Number);
2354 Last_New : Natural := 0;
2355 Dir_Path : constant String := Get_Name_String
2356 (Arguments_Project.Directory.Display_Name);
2358 begin
2359 Current := Switches.Values;
2361 for Index in New_Args'Range loop
2362 Element := Project_Tree.Shared.String_Elements.
2363 Table (Current);
2364 Get_Name_String (Element.Value);
2366 if Name_Len > 0 then
2367 Last_New := Last_New + 1;
2368 New_Args (Last_New) :=
2369 new String'(Name_Buffer (1 .. Name_Len));
2370 Ensure_Absolute_Path
2371 (New_Args (Last_New),
2372 Do_Fail => Make_Failed'Access,
2373 Parent => Dir_Path,
2374 Including_Non_Switch => False);
2375 end if;
2377 Current := Element.Next;
2378 end loop;
2380 Add_Arguments
2381 (Configuration_Pragmas_Switch (Arguments_Project)
2382 & New_Args (1 .. Last_New)
2383 & The_Saved_Gcc_Switches.all);
2384 end;
2385 end;
2387 -- We have a single switch. We add this switch,
2388 -- plus the saved gcc switches.
2390 when Single =>
2391 Get_Name_String (Switches.Value);
2393 declare
2394 New_Args : Argument_List :=
2395 (1 => new String'
2396 (Name_Buffer (1 .. Name_Len)));
2397 Dir_Path : constant String :=
2398 Get_Name_String
2399 (Arguments_Project.
2400 Directory.Display_Name);
2402 begin
2403 Ensure_Absolute_Path
2404 (New_Args (1),
2405 Do_Fail => Make_Failed'Access,
2406 Parent => Dir_Path,
2407 Including_Non_Switch => False);
2408 Add_Arguments
2409 (Configuration_Pragmas_Switch (Arguments_Project) &
2410 New_Args & The_Saved_Gcc_Switches.all);
2411 end;
2413 -- We have no switches from Gnatmake.Compiler.
2414 -- We add the saved gcc switches.
2416 when Undefined =>
2417 Add_Arguments
2418 (Configuration_Pragmas_Switch (Arguments_Project) &
2419 The_Saved_Gcc_Switches.all);
2420 end case;
2421 end if;
2422 end;
2423 end if;
2425 -- Set Output_Is_Object, depending if there is a -S switch.
2426 -- If the bind step is not performed, and there is a -S switch,
2427 -- then we will not check for a valid object file.
2429 Check_For_S_Switch;
2430 end Collect_Arguments;
2432 ---------------------
2433 -- Compile_Sources --
2434 ---------------------
2436 procedure Compile_Sources
2437 (Main_Source : File_Name_Type;
2438 Args : Argument_List;
2439 First_Compiled_File : out File_Name_Type;
2440 Most_Recent_Obj_File : out File_Name_Type;
2441 Most_Recent_Obj_Stamp : out Time_Stamp_Type;
2442 Main_Unit : out Boolean;
2443 Compilation_Failures : out Natural;
2444 Main_Index : Int := 0;
2445 Check_Readonly_Files : Boolean := False;
2446 Do_Not_Execute : Boolean := False;
2447 Force_Compilations : Boolean := False;
2448 Keep_Going : Boolean := False;
2449 In_Place_Mode : Boolean := False;
2450 Initialize_ALI_Data : Boolean := True;
2451 Max_Process : Positive := 1)
2453 Mfile : Natural := No_Mapping_File;
2454 Mapping_File_Arg : String_Access;
2455 -- Info on the mapping file
2457 Need_To_Check_Standard_Library : Boolean :=
2458 (Check_Readonly_Files or Must_Compile)
2459 and not Unique_Compile;
2461 procedure Add_Process
2462 (Pid : Process_Id;
2463 Sfile : File_Name_Type;
2464 Afile : File_Name_Type;
2465 Uname : Unit_Name_Type;
2466 Full_Lib_File : File_Name_Type;
2467 Lib_File_Attr : File_Attributes;
2468 Mfile : Natural := No_Mapping_File);
2469 -- Adds process Pid to the current list of outstanding compilation
2470 -- processes and record the full name of the source file Sfile that
2471 -- we are compiling, the name of its library file Afile and the
2472 -- name of its unit Uname. If Mfile is not equal to No_Mapping_File,
2473 -- it is the index of the mapping file used during compilation in the
2474 -- array The_Mapping_File_Names.
2476 procedure Await_Compile
2477 (Data : out Compilation_Data;
2478 OK : out Boolean);
2479 -- Awaits that an outstanding compilation process terminates. When it
2480 -- does set Data to the information registered for the corresponding
2481 -- call to Add_Process. Note that this time stamp can be used to check
2482 -- whether the compilation did generate an object file. OK is set to
2483 -- True if the compilation succeeded. Data could be No_Compilation_Data
2484 -- if there was no compilation to wait for.
2486 function Bad_Compilation_Count return Natural;
2487 -- Returns the number of compilation failures
2489 procedure Check_Standard_Library;
2490 -- Check if s-stalib.adb needs to be compiled
2492 procedure Collect_Arguments_And_Compile
2493 (Full_Source_File : File_Name_Type;
2494 Lib_File : File_Name_Type;
2495 Source_Index : Int;
2496 Pid : out Process_Id;
2497 Process_Created : out Boolean);
2498 -- Collect arguments from project file (if any) and compile. If no
2499 -- compilation was attempted, Processed_Created is set to False, and the
2500 -- value of Pid is unknown.
2502 function Compile
2503 (Project : Project_Id;
2504 S : File_Name_Type;
2505 L : File_Name_Type;
2506 Source_Index : Int;
2507 Args : Argument_List) return Process_Id;
2508 -- Compiles S using Args. If S is a GNAT predefined source "-gnatpg" is
2509 -- added to Args. Non blocking call. L corresponds to the expected
2510 -- library file name. Process_Id of the process spawned to execute the
2511 -- compilation.
2513 type ALI_Project is record
2514 ALI : ALI_Id;
2515 Project : Project_Id;
2516 end record;
2518 package Good_ALI is new Table.Table (
2519 Table_Component_Type => ALI_Project,
2520 Table_Index_Type => Natural,
2521 Table_Low_Bound => 1,
2522 Table_Initial => 50,
2523 Table_Increment => 100,
2524 Table_Name => "Make.Good_ALI");
2525 -- Contains the set of valid ALI files that have not yet been scanned
2527 function Good_ALI_Present return Boolean;
2528 -- Returns True if any ALI file was recorded in the previous set
2530 procedure Get_Mapping_File (Project : Project_Id);
2531 -- Get a mapping file name. If there is one to be reused, reuse it.
2532 -- Otherwise, create a new mapping file.
2534 function Get_Next_Good_ALI return ALI_Project;
2535 -- Returns the next good ALI_Id record
2537 procedure Record_Failure
2538 (File : File_Name_Type;
2539 Unit : Unit_Name_Type;
2540 Found : Boolean := True);
2541 -- Records in the previous table that the compilation for File failed.
2542 -- If Found is False then the compilation of File failed because we
2543 -- could not find it. Records also Unit when possible.
2545 procedure Record_Good_ALI (A : ALI_Id; Project : Project_Id);
2546 -- Records in the previous set the Id of an ALI file
2548 function Must_Exit_Because_Of_Error return Boolean;
2549 -- Return True if there were errors and the user decided to exit in such
2550 -- a case. This waits for any outstanding compilation.
2552 function Start_Compile_If_Possible (Args : Argument_List) return Boolean;
2553 -- Check if there is more work that we can do (i.e. the Queue is non
2554 -- empty). If there is, do it only if we have not yet used up all the
2555 -- available processes.
2556 -- Returns True if we should exit the main loop
2558 procedure Wait_For_Available_Slot;
2559 -- Check if we should wait for a compilation to finish. This is the case
2560 -- if all the available processes are busy compiling sources or there is
2561 -- nothing else to do (that is the Q is empty and there are no good ALIs
2562 -- to process).
2564 procedure Fill_Queue_From_ALI_Files;
2565 -- Check if we recorded good ALI files. If yes process them now in the
2566 -- order in which they have been recorded. There are two occasions in
2567 -- which we record good ali files. The first is in phase 1 when, after
2568 -- scanning an existing ALI file we realize it is up-to-date, the second
2569 -- instance is after a successful compilation.
2571 -----------------
2572 -- Add_Process --
2573 -----------------
2575 procedure Add_Process
2576 (Pid : Process_Id;
2577 Sfile : File_Name_Type;
2578 Afile : File_Name_Type;
2579 Uname : Unit_Name_Type;
2580 Full_Lib_File : File_Name_Type;
2581 Lib_File_Attr : File_Attributes;
2582 Mfile : Natural := No_Mapping_File)
2584 OC1 : constant Positive := Outstanding_Compiles + 1;
2586 begin
2587 pragma Assert (OC1 <= Max_Process);
2588 pragma Assert (Pid /= Invalid_Pid);
2590 Running_Compile (OC1) :=
2591 (Pid => Pid,
2592 Full_Source_File => Sfile,
2593 Lib_File => Afile,
2594 Full_Lib_File => Full_Lib_File,
2595 Lib_File_Attr => Lib_File_Attr,
2596 Source_Unit => Uname,
2597 Mapping_File => Mfile,
2598 Project => Arguments_Project);
2600 Outstanding_Compiles := OC1;
2602 if Arguments_Project /= No_Project then
2603 Queue.Set_Obj_Dir_Busy (Arguments_Project.Object_Directory.Name);
2604 end if;
2605 end Add_Process;
2607 --------------------
2608 -- Await_Compile --
2609 -------------------
2611 procedure Await_Compile
2612 (Data : out Compilation_Data;
2613 OK : out Boolean)
2615 Pid : Process_Id;
2616 Project : Project_Id;
2617 Comp_Data : Project_Compilation_Access;
2619 begin
2620 pragma Assert (Outstanding_Compiles > 0);
2622 Data := No_Compilation_Data;
2623 OK := False;
2625 Wait_Process (Pid, OK);
2627 if Pid = Invalid_Pid then
2628 return;
2629 end if;
2631 -- Look into the running compilation processes for this PID
2633 for J in Running_Compile'First .. Outstanding_Compiles loop
2634 if Pid = Running_Compile (J).Pid then
2635 Data := Running_Compile (J);
2636 Project := Running_Compile (J).Project;
2638 if Project /= No_Project then
2639 Queue.Set_Obj_Dir_Free (Project.Object_Directory.Name);
2640 end if;
2642 -- If a mapping file was used by this compilation, get its file
2643 -- name for reuse by a subsequent compilation.
2645 if Running_Compile (J).Mapping_File /= No_Mapping_File then
2646 Comp_Data :=
2647 Project_Compilation_Htable.Get
2648 (Project_Compilation, Project);
2649 Comp_Data.Last_Free_Indexes :=
2650 Comp_Data.Last_Free_Indexes + 1;
2651 Comp_Data.Free_Mapping_File_Indexes
2652 (Comp_Data.Last_Free_Indexes) :=
2653 Running_Compile (J).Mapping_File;
2654 end if;
2656 -- To actually remove this Pid and related info from
2657 -- Running_Compile replace its entry with the last valid
2658 -- entry in Running_Compile.
2660 if J = Outstanding_Compiles then
2661 null;
2662 else
2663 Running_Compile (J) :=
2664 Running_Compile (Outstanding_Compiles);
2665 end if;
2667 Outstanding_Compiles := Outstanding_Compiles - 1;
2668 exit;
2669 end if;
2670 end loop;
2672 -- If the PID was not found, return with OK set to False
2674 if Data = No_Compilation_Data then
2675 OK := False;
2676 end if;
2677 end Await_Compile;
2679 ---------------------------
2680 -- Bad_Compilation_Count --
2681 ---------------------------
2683 function Bad_Compilation_Count return Natural is
2684 begin
2685 return Bad_Compilation.Last - Bad_Compilation.First + 1;
2686 end Bad_Compilation_Count;
2688 ----------------------------
2689 -- Check_Standard_Library --
2690 ----------------------------
2692 procedure Check_Standard_Library is
2693 begin
2694 Need_To_Check_Standard_Library := False;
2695 Name_Len := 0;
2697 if not Targparm.Suppress_Standard_Library_On_Target then
2698 Add_Str_To_Name_Buffer (Standard_Library_Package_Body_Name);
2699 else
2700 Add_Str_To_Name_Buffer (System_Package_Spec_Name);
2701 end if;
2703 declare
2704 Add_It : Boolean := True;
2705 Sfile : File_Name_Type;
2707 begin
2708 Sfile := Name_Enter;
2710 -- If we have a special runtime, we add the standard library only
2711 -- if we can find it.
2713 if RTS_Switch then
2714 Add_It := Full_Source_Name (Sfile) /= No_File;
2715 end if;
2717 if Add_It then
2718 if not Queue.Insert
2719 ((Format => Format_Gnatmake,
2720 File => Sfile,
2721 Unit => No_Unit_Name,
2722 Project => No_Project,
2723 Index => 0,
2724 Sid => No_Source))
2725 then
2726 if Is_In_Obsoleted (Sfile) then
2727 Executable_Obsolete := True;
2728 end if;
2729 end if;
2730 end if;
2731 end;
2732 end Check_Standard_Library;
2734 -----------------------------------
2735 -- Collect_Arguments_And_Compile --
2736 -----------------------------------
2738 procedure Collect_Arguments_And_Compile
2739 (Full_Source_File : File_Name_Type;
2740 Lib_File : File_Name_Type;
2741 Source_Index : Int;
2742 Pid : out Process_Id;
2743 Process_Created : out Boolean) is
2744 begin
2745 Process_Created := False;
2747 -- If we use mapping file (-P or -C switches), then get one
2749 if Create_Mapping_File then
2750 Get_Mapping_File (Arguments_Project);
2751 end if;
2753 -- If the source is part of a project file, we set the ADA_*_PATHs,
2754 -- check for an eventual library project, and use the full path.
2756 if Arguments_Project /= No_Project then
2757 if not Arguments_Project.Externally_Built
2758 or else Must_Compile
2759 then
2760 Prj.Env.Set_Ada_Paths
2761 (Arguments_Project,
2762 Project_Tree,
2763 Including_Libraries => True,
2764 Include_Path => Use_Include_Path_File);
2766 if not Unique_Compile
2767 and then MLib.Tgt.Support_For_Libraries /= Prj.None
2768 then
2769 declare
2770 Prj : constant Project_Id :=
2771 Ultimate_Extending_Project_Of (Arguments_Project);
2773 begin
2774 if Prj.Library
2775 and then (not Prj.Externally_Built or else Must_Compile)
2776 and then not Prj.Need_To_Build_Lib
2777 then
2778 -- Add to the Q all sources of the project that have
2779 -- not been marked.
2781 Insert_Project_Sources
2782 (The_Project => Prj,
2783 All_Projects => False,
2784 Into_Q => True);
2786 -- Now mark the project as processed
2788 Prj.Need_To_Build_Lib := True;
2789 end if;
2790 end;
2791 end if;
2793 Pid :=
2794 Compile
2795 (Project => Arguments_Project,
2796 S => File_Name_Type (Arguments_Path_Name),
2797 L => Lib_File,
2798 Source_Index => Source_Index,
2799 Args => Arguments (1 .. Last_Argument));
2800 Process_Created := True;
2801 end if;
2803 else
2804 -- If this is a source outside of any project file, make sure it
2805 -- will be compiled in object directory of the main project file.
2807 Pid :=
2808 Compile
2809 (Project => Main_Project,
2810 S => Full_Source_File,
2811 L => Lib_File,
2812 Source_Index => Source_Index,
2813 Args => Arguments (1 .. Last_Argument));
2814 Process_Created := True;
2815 end if;
2816 end Collect_Arguments_And_Compile;
2818 -------------
2819 -- Compile --
2820 -------------
2822 function Compile
2823 (Project : Project_Id;
2824 S : File_Name_Type;
2825 L : File_Name_Type;
2826 Source_Index : Int;
2827 Args : Argument_List) return Process_Id
2829 Comp_Args : Argument_List (Args'First .. Args'Last + 10);
2830 Comp_Next : Integer := Args'First;
2831 Comp_Last : Integer;
2832 Arg_Index : Integer;
2834 function Ada_File_Name (Name : File_Name_Type) return Boolean;
2835 -- Returns True if Name is the name of an ada source file
2836 -- (i.e. suffix is .ads or .adb)
2838 -------------------
2839 -- Ada_File_Name --
2840 -------------------
2842 function Ada_File_Name (Name : File_Name_Type) return Boolean is
2843 begin
2844 Get_Name_String (Name);
2845 return
2846 Name_Len > 4
2847 and then Name_Buffer (Name_Len - 3 .. Name_Len - 1) = ".ad"
2848 and then (Name_Buffer (Name_Len) = 'b'
2849 or else
2850 Name_Buffer (Name_Len) = 's');
2851 end Ada_File_Name;
2853 -- Start of processing for Compile
2855 begin
2856 Enter_Into_Obsoleted (S);
2858 -- By default, Syntax_Only is False
2860 Syntax_Only := False;
2862 for J in Args'Range loop
2863 if Args (J).all = "-gnats" then
2865 -- If we compile with -gnats, the bind step and the link step
2866 -- are inhibited. Also, we set Syntax_Only to True, so that
2867 -- we don't fail when we don't find the ALI file, after
2868 -- compilation.
2870 Do_Bind_Step := False;
2871 Do_Link_Step := False;
2872 Syntax_Only := True;
2874 elsif Args (J).all = "-gnatc" then
2876 -- If we compile with -gnatc, the bind step and the link step
2877 -- are inhibited. We set Syntax_Only to False for the case when
2878 -- -gnats was previously specified.
2880 Do_Bind_Step := False;
2881 Do_Link_Step := False;
2882 Syntax_Only := False;
2883 end if;
2884 end loop;
2886 Comp_Args (Comp_Next) := new String'("-gnatea");
2887 Comp_Next := Comp_Next + 1;
2889 Comp_Args (Comp_Next) := Comp_Flag;
2890 Comp_Next := Comp_Next + 1;
2892 -- Optimize the simple case where the gcc command line looks like
2893 -- gcc -c -I. ... -I- file.adb
2894 -- into
2895 -- gcc -c ... file.adb
2897 if Args (Args'First).all = "-I" & Normalized_CWD
2898 and then Args (Args'Last).all = "-I-"
2899 and then S = Strip_Directory (S)
2900 then
2901 Comp_Last := Comp_Next + Args'Length - 3;
2902 Arg_Index := Args'First + 1;
2904 else
2905 Comp_Last := Comp_Next + Args'Length - 1;
2906 Arg_Index := Args'First;
2907 end if;
2909 -- Make a deep copy of the arguments, because Normalize_Arguments
2910 -- may deallocate some arguments. Also strip target specific -mxxx
2911 -- switches in CodePeer mode.
2913 declare
2914 Index : Natural;
2915 Last : constant Natural := Comp_Last;
2917 begin
2918 Index := Comp_Next;
2919 for J in Comp_Next .. Last loop
2920 declare
2921 Str : String renames Args (Arg_Index).all;
2922 begin
2923 if CodePeer_Mode
2924 and then Str'Length > 2
2925 and then Str (Str'First .. Str'First + 1) = "-m"
2926 then
2927 Comp_Last := Comp_Last - 1;
2928 else
2929 Comp_Args (Index) := new String'(Str);
2930 Index := Index + 1;
2931 end if;
2932 end;
2934 Arg_Index := Arg_Index + 1;
2935 end loop;
2936 end;
2938 -- Set -gnatpg for predefined files (for this purpose the renamings
2939 -- such as Text_IO do not count as predefined). Note that we strip
2940 -- the directory name from the source file name because the call to
2941 -- Fname.Is_Predefined_File_Name cannot deal with directory prefixes.
2943 declare
2944 Fname : constant File_Name_Type := Strip_Directory (S);
2946 begin
2947 if Is_Predefined_File_Name (Fname, False) then
2948 if Check_Readonly_Files or else Must_Compile then
2949 Comp_Args (Comp_Args'First + 2 .. Comp_Last + 1) :=
2950 Comp_Args (Comp_Args'First + 1 .. Comp_Last);
2951 Comp_Last := Comp_Last + 1;
2952 Comp_Args (Comp_Args'First + 1) := GNAT_Flag;
2954 else
2955 Make_Failed
2956 ("not allowed to compile """ &
2957 Get_Name_String (Fname) &
2958 """; use -a switch, or use the compiler directly with "
2959 & "the ""-gnatg"" switch");
2960 end if;
2961 end if;
2962 end;
2964 -- Now check if the file name has one of the suffixes familiar to
2965 -- the gcc driver. If this is not the case then add the ada flag
2966 -- "-x ada".
2967 -- Append systematically "-x adascil" in CodePeer mode instead, to
2968 -- force the use of gnat1scil instead of gnat1.
2970 if CodePeer_Mode then
2971 Comp_Last := Comp_Last + 1;
2972 Comp_Args (Comp_Last) := Ada_Flag_1;
2973 Comp_Last := Comp_Last + 1;
2974 Comp_Args (Comp_Last) := AdaSCIL_Flag;
2976 elsif not Ada_File_Name (S) then
2977 Comp_Last := Comp_Last + 1;
2978 Comp_Args (Comp_Last) := Ada_Flag_1;
2979 Comp_Last := Comp_Last + 1;
2980 Comp_Args (Comp_Last) := Ada_Flag_2;
2981 end if;
2983 if Source_Index /= 0 then
2984 declare
2985 Num : constant String := Source_Index'Img;
2986 begin
2987 Comp_Last := Comp_Last + 1;
2988 Comp_Args (Comp_Last) :=
2989 new String'("-gnateI" & Num (Num'First + 1 .. Num'Last));
2990 end;
2991 end if;
2993 if Source_Index /= 0
2994 or else L /= Strip_Directory (L)
2995 or else Object_Directory_Path /= null
2996 then
2997 -- Build -o argument
2999 Get_Name_String (L);
3001 for J in reverse 1 .. Name_Len loop
3002 if Name_Buffer (J) = '.' then
3003 Name_Len := J + Object_Suffix'Length - 1;
3004 Name_Buffer (J .. Name_Len) := Object_Suffix;
3005 exit;
3006 end if;
3007 end loop;
3009 Comp_Last := Comp_Last + 1;
3010 Comp_Args (Comp_Last) := Output_Flag;
3011 Comp_Last := Comp_Last + 1;
3013 -- If an object directory was specified, prepend the object file
3014 -- name with this object directory.
3016 if Object_Directory_Path /= null then
3017 Comp_Args (Comp_Last) :=
3018 new String'(Object_Directory_Path.all &
3019 Name_Buffer (1 .. Name_Len));
3021 else
3022 Comp_Args (Comp_Last) :=
3023 new String'(Name_Buffer (1 .. Name_Len));
3024 end if;
3025 end if;
3027 if Create_Mapping_File and then Mapping_File_Arg /= null then
3028 Comp_Last := Comp_Last + 1;
3029 Comp_Args (Comp_Last) := new String'(Mapping_File_Arg.all);
3030 end if;
3032 Get_Name_String (S);
3034 Comp_Last := Comp_Last + 1;
3035 Comp_Args (Comp_Last) := new String'(Name_Buffer (1 .. Name_Len));
3037 -- Change to object directory of the project file, if necessary
3039 if Project /= No_Project then
3040 Change_To_Object_Directory (Project);
3041 end if;
3043 GNAT.OS_Lib.Normalize_Arguments (Comp_Args (Args'First .. Comp_Last));
3045 Comp_Last := Comp_Last + 1;
3046 Comp_Args (Comp_Last) := new String'("-gnatez");
3048 Display (Gcc.all, Comp_Args (Args'First .. Comp_Last));
3050 if Gcc_Path = null then
3051 Make_Failed ("error, unable to locate " & Gcc.all);
3052 end if;
3054 return
3055 GNAT.OS_Lib.Non_Blocking_Spawn
3056 (Gcc_Path.all, Comp_Args (Args'First .. Comp_Last));
3057 end Compile;
3059 -------------------------------
3060 -- Fill_Queue_From_ALI_Files --
3061 -------------------------------
3063 procedure Fill_Queue_From_ALI_Files is
3064 ALI_P : ALI_Project;
3065 ALI : ALI_Id;
3066 Source_Index : Int;
3067 Sfile : File_Name_Type;
3068 Sid : Prj.Source_Id;
3069 Uname : Unit_Name_Type;
3070 Unit_Name : Name_Id;
3071 Uid : Prj.Unit_Index;
3073 begin
3074 while Good_ALI_Present loop
3075 ALI_P := Get_Next_Good_ALI;
3076 ALI := ALI_P.ALI;
3077 Source_Index := Unit_Index_Of (ALIs.Table (ALI_P.ALI).Afile);
3079 -- If we are processing the library file corresponding to the
3080 -- main source file check if this source can be a main unit.
3082 if ALIs.Table (ALI).Sfile = Main_Source
3083 and then Source_Index = Main_Index
3084 then
3085 Main_Unit := ALIs.Table (ALI).Main_Program /= None;
3086 end if;
3088 -- The following adds the standard library (s-stalib) to the list
3089 -- of files to be handled by gnatmake: this file and any files it
3090 -- depends on are always included in every bind, even if they are
3091 -- not in the explicit dependency list. Of course, it is not added
3092 -- if Suppress_Standard_Library is True.
3094 -- However, to avoid annoying output about s-stalib.ali being read
3095 -- only, when "-v" is used, we add the standard library only when
3096 -- "-a" is used.
3098 if Need_To_Check_Standard_Library then
3099 Check_Standard_Library;
3100 end if;
3102 -- Now insert in the Q the unmarked source files (i.e. those which
3103 -- have never been inserted in the Q and hence never considered).
3104 -- Only do that if Unique_Compile is False.
3106 if not Unique_Compile then
3107 for J in
3108 ALIs.Table (ALI).First_Unit .. ALIs.Table (ALI).Last_Unit
3109 loop
3110 for K in
3111 Units.Table (J).First_With .. Units.Table (J).Last_With
3112 loop
3113 Sfile := Withs.Table (K).Sfile;
3114 Uname := Withs.Table (K).Uname;
3115 Sid := No_Source;
3117 -- If project files are used, find the proper source to
3118 -- compile in case Sfile is the spec but there is a body.
3120 if Main_Project /= No_Project then
3121 Get_Name_String (Uname);
3122 Name_Len := Name_Len - 2;
3123 Unit_Name := Name_Find;
3124 Uid :=
3125 Units_Htable.Get (Project_Tree.Units_HT, Unit_Name);
3127 if Uid /= Prj.No_Unit_Index then
3128 if Uid.File_Names (Impl) /= null
3129 and then not Uid.File_Names (Impl).Locally_Removed
3130 then
3131 Sfile := Uid.File_Names (Impl).File;
3132 Source_Index := Uid.File_Names (Impl).Index;
3133 Sid := Uid.File_Names (Impl);
3135 elsif Uid.File_Names (Spec) /= null
3136 and then not Uid.File_Names (Spec).Locally_Removed
3137 then
3138 Sfile := Uid.File_Names (Spec).File;
3139 Source_Index := Uid.File_Names (Spec).Index;
3140 Sid := Uid.File_Names (Spec);
3141 end if;
3142 end if;
3143 end if;
3145 Dependencies.Append ((ALIs.Table (ALI).Sfile, Sfile));
3147 if Is_In_Obsoleted (Sfile) then
3148 Executable_Obsolete := True;
3149 end if;
3151 if Sfile = No_File then
3152 Debug_Msg ("Skipping generic:", Withs.Table (K).Uname);
3154 else
3155 Source_Index := Unit_Index_Of (Withs.Table (K).Afile);
3157 if not (Check_Readonly_Files or Must_Compile)
3158 and then Is_Internal_File_Name (Sfile, False)
3159 then
3160 Debug_Msg ("Skipping internal file:", Sfile);
3162 else
3163 Queue.Insert
3164 ((Format => Format_Gnatmake,
3165 File => Sfile,
3166 Project => ALI_P.Project,
3167 Unit => Withs.Table (K).Uname,
3168 Index => Source_Index,
3169 Sid => Sid));
3170 end if;
3171 end if;
3172 end loop;
3173 end loop;
3174 end if;
3175 end loop;
3176 end Fill_Queue_From_ALI_Files;
3178 ----------------------
3179 -- Get_Mapping_File --
3180 ----------------------
3182 procedure Get_Mapping_File (Project : Project_Id) is
3183 Data : Project_Compilation_Access;
3185 begin
3186 Data := Project_Compilation_Htable.Get (Project_Compilation, Project);
3188 -- If there is a mapping file ready to be reused, reuse it
3190 if Data.Last_Free_Indexes > 0 then
3191 Mfile := Data.Free_Mapping_File_Indexes (Data.Last_Free_Indexes);
3192 Data.Last_Free_Indexes := Data.Last_Free_Indexes - 1;
3194 -- Otherwise, create and initialize a new one
3196 else
3197 Init_Mapping_File
3198 (Project => Project, Data => Data.all, File_Index => Mfile);
3199 end if;
3201 -- Put the name in the mapping file argument for the invocation
3202 -- of the compiler.
3204 Free (Mapping_File_Arg);
3205 Mapping_File_Arg :=
3206 new String'("-gnatem=" &
3207 Get_Name_String (Data.Mapping_File_Names (Mfile)));
3208 end Get_Mapping_File;
3210 -----------------------
3211 -- Get_Next_Good_ALI --
3212 -----------------------
3214 function Get_Next_Good_ALI return ALI_Project is
3215 ALIP : ALI_Project;
3217 begin
3218 pragma Assert (Good_ALI_Present);
3219 ALIP := Good_ALI.Table (Good_ALI.Last);
3220 Good_ALI.Decrement_Last;
3221 return ALIP;
3222 end Get_Next_Good_ALI;
3224 ----------------------
3225 -- Good_ALI_Present --
3226 ----------------------
3228 function Good_ALI_Present return Boolean is
3229 begin
3230 return Good_ALI.First <= Good_ALI.Last;
3231 end Good_ALI_Present;
3233 --------------------------------
3234 -- Must_Exit_Because_Of_Error --
3235 --------------------------------
3237 function Must_Exit_Because_Of_Error return Boolean is
3238 Data : Compilation_Data;
3239 Success : Boolean;
3241 begin
3242 if Bad_Compilation_Count > 0 and then not Keep_Going then
3243 while Outstanding_Compiles > 0 loop
3244 Await_Compile (Data, Success);
3246 if not Success then
3247 Record_Failure (Data.Full_Source_File, Data.Source_Unit);
3248 end if;
3249 end loop;
3251 return True;
3252 end if;
3254 return False;
3255 end Must_Exit_Because_Of_Error;
3257 --------------------
3258 -- Record_Failure --
3259 --------------------
3261 procedure Record_Failure
3262 (File : File_Name_Type;
3263 Unit : Unit_Name_Type;
3264 Found : Boolean := True)
3266 begin
3267 Bad_Compilation.Increment_Last;
3268 Bad_Compilation.Table (Bad_Compilation.Last) := (File, Unit, Found);
3269 end Record_Failure;
3271 ---------------------
3272 -- Record_Good_ALI --
3273 ---------------------
3275 procedure Record_Good_ALI (A : ALI_Id; Project : Project_Id) is
3276 begin
3277 Good_ALI.Increment_Last;
3278 Good_ALI.Table (Good_ALI.Last) := (A, Project);
3279 end Record_Good_ALI;
3281 -------------------------------
3282 -- Start_Compile_If_Possible --
3283 -------------------------------
3285 function Start_Compile_If_Possible
3286 (Args : Argument_List) return Boolean
3288 In_Lib_Dir : Boolean;
3289 Need_To_Compile : Boolean;
3290 Pid : Process_Id := Invalid_Pid;
3291 Process_Created : Boolean;
3293 Source : Queue.Source_Info;
3294 Full_Source_File : File_Name_Type := No_File;
3295 Source_File_Attr : aliased File_Attributes;
3296 -- The full name of the source file and its attributes (size, ...)
3298 Lib_File : File_Name_Type;
3299 Full_Lib_File : File_Name_Type := No_File;
3300 Lib_File_Attr : aliased File_Attributes;
3301 Read_Only : Boolean := False;
3302 ALI : ALI_Id;
3303 -- The ALI file and its attributes (size, stamp, ...)
3305 Obj_File : File_Name_Type;
3306 Obj_Stamp : Time_Stamp_Type;
3307 -- The object file
3309 Found : Boolean;
3311 begin
3312 if not Queue.Is_Virtually_Empty and then
3313 Outstanding_Compiles < Max_Process
3314 then
3315 Queue.Extract (Found, Source);
3317 -- If it is a source in a project, first look for the ALI file
3318 -- in the object directory. When the project is extending another
3319 -- the ALI file may not be found, but the source does not
3320 -- necessarily need to be compiled, as it may already be up to
3321 -- date in the project being extended. In this case, look for an
3322 -- ALI file in all the object directories, as is done when
3323 -- gnatmake is not invoked with a project file.
3325 if Source.Sid /= No_Source then
3326 Initialize_Source_Record (Source.Sid);
3327 Full_Source_File :=
3328 File_Name_Type (Source.Sid.Path.Display_Name);
3329 Lib_File := Source.Sid.Dep_Name;
3330 Full_Lib_File := File_Name_Type (Source.Sid.Dep_Path);
3331 Lib_File_Attr := Unknown_Attributes;
3333 if Full_Lib_File /= No_File then
3334 declare
3335 FLF : constant String :=
3336 Get_Name_String (Full_Lib_File) & ASCII.NUL;
3337 begin
3338 if not Is_Regular_File
3339 (FLF'Address, Lib_File_Attr'Access)
3340 then
3341 Full_Lib_File := No_File;
3342 end if;
3343 end;
3344 end if;
3345 end if;
3347 if Full_Lib_File = No_File then
3348 Osint.Full_Source_Name
3349 (Source.File,
3350 Full_File => Full_Source_File,
3351 Attr => Source_File_Attr'Access);
3353 Lib_File := Osint.Lib_File_Name (Source.File, Source.Index);
3355 Osint.Full_Lib_File_Name
3356 (Lib_File,
3357 Lib_File => Full_Lib_File,
3358 Attr => Lib_File_Attr);
3359 end if;
3361 -- If source has already been compiled, executable is obsolete
3363 if Is_In_Obsoleted (Source.File) then
3364 Executable_Obsolete := True;
3365 end if;
3367 In_Lib_Dir := Full_Lib_File /= No_File
3368 and then In_Ada_Lib_Dir (Full_Lib_File);
3370 -- Since the following requires a system call, we precompute it
3371 -- when needed.
3373 if not In_Lib_Dir then
3374 if Full_Lib_File /= No_File
3375 and then not (Check_Readonly_Files or else Must_Compile)
3376 then
3377 Get_Name_String (Full_Lib_File);
3378 Name_Buffer (Name_Len + 1) := ASCII.NUL;
3379 Read_Only := not Is_Writable_File
3380 (Name_Buffer'Address, Lib_File_Attr'Access);
3381 else
3382 Read_Only := False;
3383 end if;
3384 end if;
3386 -- If the library file is an Ada library skip it
3388 if In_Lib_Dir then
3389 Verbose_Msg
3390 (Lib_File,
3391 "is in an Ada library",
3392 Prefix => " ",
3393 Minimum_Verbosity => Opt.High);
3395 -- If the library file is a read-only library skip it, but only
3396 -- if, when using project files, this library file is in the
3397 -- right object directory (a read-only ALI file in the object
3398 -- directory of a project being extended must not be skipped).
3400 elsif Read_Only
3401 and then Is_In_Object_Directory (Source.File, Full_Lib_File)
3402 then
3403 Verbose_Msg
3404 (Lib_File,
3405 "is a read-only library",
3406 Prefix => " ",
3407 Minimum_Verbosity => Opt.High);
3409 -- The source file that we are checking cannot be located
3411 elsif Full_Source_File = No_File then
3412 Record_Failure (Source.File, Source.Unit, False);
3414 -- Source and library files can be located but are internal
3415 -- files.
3417 elsif not (Check_Readonly_Files or else Must_Compile)
3418 and then Full_Lib_File /= No_File
3419 and then Is_Internal_File_Name (Source.File, False)
3420 then
3421 if Force_Compilations then
3422 Fail
3423 ("not allowed to compile """ &
3424 Get_Name_String (Source.File) &
3425 """; use -a switch, or use the compiler directly with "
3426 & "the ""-gnatg"" switch");
3427 end if;
3429 Verbose_Msg
3430 (Lib_File,
3431 "is an internal library",
3432 Prefix => " ",
3433 Minimum_Verbosity => Opt.High);
3435 -- The source file that we are checking can be located
3437 else
3438 Collect_Arguments
3439 (Source.File, Source.File = Main_Source, Args);
3441 -- Do nothing if project of source is externally built
3443 if Arguments_Project = No_Project
3444 or else not Arguments_Project.Externally_Built
3445 or else Must_Compile
3446 then
3447 -- Don't waste any time if we have to recompile anyway
3449 Obj_Stamp := Empty_Time_Stamp;
3450 Need_To_Compile := Force_Compilations;
3452 if not Force_Compilations then
3453 Check (Source_File => Source.File,
3454 Is_Main_Source => Source.File = Main_Source,
3455 The_Args => Args,
3456 Lib_File => Lib_File,
3457 Full_Lib_File => Full_Lib_File,
3458 Lib_File_Attr => Lib_File_Attr'Access,
3459 Read_Only => Read_Only,
3460 ALI => ALI,
3461 O_File => Obj_File,
3462 O_Stamp => Obj_Stamp);
3463 Need_To_Compile := (ALI = No_ALI_Id);
3464 end if;
3466 if not Need_To_Compile then
3468 -- The ALI file is up-to-date; record its Id
3470 Record_Good_ALI (ALI, Arguments_Project);
3472 -- Record the time stamp of the most recent object
3473 -- file as long as no (re)compilations are needed.
3475 if First_Compiled_File = No_File
3476 and then (Most_Recent_Obj_File = No_File
3477 or else Obj_Stamp > Most_Recent_Obj_Stamp)
3478 then
3479 Most_Recent_Obj_File := Obj_File;
3480 Most_Recent_Obj_Stamp := Obj_Stamp;
3481 end if;
3483 else
3484 -- Check that switch -x has been used if a source outside
3485 -- of project files need to be compiled.
3487 if Main_Project /= No_Project
3488 and then Arguments_Project = No_Project
3489 and then not External_Unit_Compilation_Allowed
3490 then
3491 Make_Failed ("external source ("
3492 & Get_Name_String (Source.File)
3493 & ") is not part of any project;"
3494 & " cannot be compiled without"
3495 & " gnatmake switch -x");
3496 end if;
3498 -- Is this the first file we have to compile?
3500 if First_Compiled_File = No_File then
3501 First_Compiled_File := Full_Source_File;
3502 Most_Recent_Obj_File := No_File;
3504 if Do_Not_Execute then
3506 -- Exit the main loop
3508 return True;
3509 end if;
3510 end if;
3512 -- Compute where the ALI file must be generated in
3513 -- In_Place_Mode (this does not require to know the
3514 -- location of the object directory).
3516 if In_Place_Mode then
3517 if Full_Lib_File = No_File then
3519 -- If the library file was not found, then save
3520 -- the library file near the source file.
3522 Lib_File :=
3523 Osint.Lib_File_Name
3524 (Full_Source_File, Source.Index);
3525 Full_Lib_File := Lib_File;
3527 else
3528 -- If the library file was found, then save the
3529 -- library file in the same place.
3531 Lib_File := Full_Lib_File;
3532 end if;
3533 end if;
3535 -- Start the compilation and record it. We can do this
3536 -- because there is at least one free process. This might
3537 -- change the current directory.
3539 Collect_Arguments_And_Compile
3540 (Full_Source_File => Full_Source_File,
3541 Lib_File => Lib_File,
3542 Source_Index => Source.Index,
3543 Pid => Pid,
3544 Process_Created => Process_Created);
3546 -- Compute where the ALI file will be generated (for
3547 -- cases that might require to know the current
3548 -- directory). The current directory might be changed
3549 -- when compiling other files so we cannot rely on it
3550 -- being the same to find the resulting ALI file.
3552 if not In_Place_Mode then
3554 -- Compute the expected location of the ALI file. This
3555 -- can be from several places:
3556 -- -i => in place mode. In such a case,
3557 -- Full_Lib_File has already been set above
3558 -- -D => if specified
3559 -- or defaults in current dir
3560 -- We could simply use a call similar to
3561 -- Osint.Full_Lib_File_Name (Lib_File)
3562 -- but that involves system calls and is thus slower
3564 if Object_Directory_Path /= null then
3565 Name_Len := 0;
3566 Add_Str_To_Name_Buffer (Object_Directory_Path.all);
3567 Add_Str_To_Name_Buffer (Get_Name_String (Lib_File));
3568 Full_Lib_File := Name_Find;
3570 else
3571 if Project_Of_Current_Object_Directory /=
3572 No_Project
3573 then
3574 Get_Name_String
3575 (Project_Of_Current_Object_Directory
3576 .Object_Directory.Display_Name);
3577 Add_Str_To_Name_Buffer
3578 (Get_Name_String (Lib_File));
3579 Full_Lib_File := Name_Find;
3581 else
3582 Full_Lib_File := Lib_File;
3583 end if;
3584 end if;
3586 end if;
3588 Lib_File_Attr := Unknown_Attributes;
3590 -- Make sure we could successfully start the compilation
3592 if Process_Created then
3593 if Pid = Invalid_Pid then
3594 Record_Failure (Full_Source_File, Source.Unit);
3595 else
3596 Add_Process
3597 (Pid => Pid,
3598 Sfile => Full_Source_File,
3599 Afile => Lib_File,
3600 Uname => Source.Unit,
3601 Mfile => Mfile,
3602 Full_Lib_File => Full_Lib_File,
3603 Lib_File_Attr => Lib_File_Attr);
3604 end if;
3605 end if;
3606 end if;
3607 end if;
3608 end if;
3609 end if;
3610 return False;
3611 end Start_Compile_If_Possible;
3613 -----------------------------
3614 -- Wait_For_Available_Slot --
3615 -----------------------------
3617 procedure Wait_For_Available_Slot is
3618 Compilation_OK : Boolean;
3619 Text : Text_Buffer_Ptr;
3620 ALI : ALI_Id;
3621 Data : Compilation_Data;
3623 begin
3624 if Outstanding_Compiles = Max_Process
3625 or else (Queue.Is_Virtually_Empty
3626 and then not Good_ALI_Present
3627 and then Outstanding_Compiles > 0)
3628 then
3629 Await_Compile (Data, Compilation_OK);
3631 if not Compilation_OK then
3632 Record_Failure (Data.Full_Source_File, Data.Source_Unit);
3633 end if;
3635 if Compilation_OK or else Keep_Going then
3637 -- Re-read the updated library file
3639 declare
3640 Saved_Object_Consistency : constant Boolean :=
3641 Check_Object_Consistency;
3643 begin
3644 -- If compilation was not OK, or if output is not an object
3645 -- file and we don't do the bind step, don't check for
3646 -- object consistency.
3648 Check_Object_Consistency :=
3649 Check_Object_Consistency
3650 and Compilation_OK
3651 and (Output_Is_Object or Do_Bind_Step);
3653 Text :=
3654 Read_Library_Info_From_Full
3655 (Data.Full_Lib_File, Data.Lib_File_Attr'Access);
3657 -- Restore Check_Object_Consistency to its initial value
3659 Check_Object_Consistency := Saved_Object_Consistency;
3660 end;
3662 -- If an ALI file was generated by this compilation, scan the
3663 -- ALI file and record it.
3665 -- If the scan fails, a previous ali file is inconsistent with
3666 -- the unit just compiled.
3668 if Text /= null then
3669 ALI :=
3670 Scan_ALI
3671 (Data.Lib_File, Text, Ignore_ED => False, Err => True);
3673 if ALI = No_ALI_Id then
3675 -- Record a failure only if not already done
3677 if Compilation_OK then
3678 Inform
3679 (Data.Lib_File,
3680 "incompatible ALI file, please recompile");
3681 Record_Failure
3682 (Data.Full_Source_File, Data.Source_Unit);
3683 end if;
3685 else
3686 Record_Good_ALI (ALI, Data.Project);
3687 end if;
3689 Free (Text);
3691 -- If we could not read the ALI file that was just generated
3692 -- then there could be a problem reading either the ALI or the
3693 -- corresponding object file (if Check_Object_Consistency is
3694 -- set Read_Library_Info checks that the time stamp of the
3695 -- object file is more recent than that of the ALI). However,
3696 -- we record a failure only if not already done.
3698 else
3699 if Compilation_OK and not Syntax_Only then
3700 Inform
3701 (Data.Lib_File,
3702 "WARNING: ALI or object file not found after compile");
3704 if not Is_Regular_File
3705 (Get_Name_String (Name_Id (Data.Full_Lib_File)))
3706 then
3707 Inform (Data.Full_Lib_File, "not found");
3708 end if;
3710 Record_Failure (Data.Full_Source_File, Data.Source_Unit);
3711 end if;
3712 end if;
3713 end if;
3714 end if;
3715 end Wait_For_Available_Slot;
3717 -- Start of processing for Compile_Sources
3719 begin
3720 pragma Assert (Args'First = 1);
3722 Outstanding_Compiles := 0;
3723 Running_Compile := new Comp_Data_Arr (1 .. Max_Process);
3725 -- Package and Queue initializations
3727 Good_ALI.Init;
3729 if Initialize_ALI_Data then
3730 Initialize_ALI;
3731 Initialize_ALI_Source;
3732 end if;
3734 -- The following two flags affect the behavior of ALI.Set_Source_Table.
3735 -- We set Check_Source_Files to True to ensure that source file time
3736 -- stamps are checked, and we set All_Sources to False to avoid checking
3737 -- the presence of the source files listed in the source dependency
3738 -- section of an ali file (which would be a mistake since the ali file
3739 -- may be obsolete).
3741 Check_Source_Files := True;
3742 All_Sources := False;
3744 Queue.Insert
3745 ((Format => Format_Gnatmake,
3746 File => Main_Source,
3747 Project => Main_Project,
3748 Unit => No_Unit_Name,
3749 Index => Main_Index,
3750 Sid => No_Source));
3752 First_Compiled_File := No_File;
3753 Most_Recent_Obj_File := No_File;
3754 Most_Recent_Obj_Stamp := Empty_Time_Stamp;
3755 Main_Unit := False;
3757 -- Keep looping until there is no more work to do (the Q is empty)
3758 -- and all the outstanding compilations have terminated.
3760 Make_Loop :
3761 while not Queue.Is_Empty or else Outstanding_Compiles > 0 loop
3762 exit Make_Loop when Must_Exit_Because_Of_Error;
3763 exit Make_Loop when Start_Compile_If_Possible (Args);
3765 Wait_For_Available_Slot;
3767 -- ??? Should be done as soon as we add a Good_ALI, wouldn't it avoid
3768 -- the need for a list of good ALI?
3770 Fill_Queue_From_ALI_Files;
3772 if Display_Compilation_Progress then
3773 Write_Str ("completed ");
3774 Write_Int (Int (Queue.Processed));
3775 Write_Str (" out of ");
3776 Write_Int (Int (Queue.Size));
3777 Write_Str (" (");
3778 Write_Int (Int ((Queue.Processed * 100) / Queue.Size));
3779 Write_Str ("%)...");
3780 Write_Eol;
3781 end if;
3782 end loop Make_Loop;
3784 Compilation_Failures := Bad_Compilation_Count;
3786 -- Compilation is finished
3788 -- Delete any temporary configuration pragma file
3790 if not Keep_Temporary_Files then
3791 Delete_Temp_Config_Files (Project_Tree);
3792 end if;
3793 end Compile_Sources;
3795 ----------------------------------
3796 -- Configuration_Pragmas_Switch --
3797 ----------------------------------
3799 function Configuration_Pragmas_Switch
3800 (For_Project : Project_Id) return Argument_List
3802 The_Packages : Package_Id;
3803 Gnatmake : Package_Id;
3804 Compiler : Package_Id;
3806 Global_Attribute : Variable_Value := Nil_Variable_Value;
3807 Local_Attribute : Variable_Value := Nil_Variable_Value;
3809 Global_Attribute_Present : Boolean := False;
3810 Local_Attribute_Present : Boolean := False;
3812 Result : Argument_List (1 .. 3);
3813 Last : Natural := 0;
3815 begin
3816 Prj.Env.Create_Config_Pragmas_File
3817 (For_Project, Project_Tree);
3819 if For_Project.Config_File_Name /= No_Path then
3820 Temporary_Config_File := For_Project.Config_File_Temp;
3821 Last := 1;
3822 Result (1) :=
3823 new String'
3824 ("-gnatec=" & Get_Name_String (For_Project.Config_File_Name));
3826 else
3827 Temporary_Config_File := False;
3828 end if;
3830 -- Check for attribute Builder'Global_Configuration_Pragmas
3832 The_Packages := Main_Project.Decl.Packages;
3833 Gnatmake :=
3834 Prj.Util.Value_Of
3835 (Name => Name_Builder,
3836 In_Packages => The_Packages,
3837 Shared => Project_Tree.Shared);
3839 if Gnatmake /= No_Package then
3840 Global_Attribute := Prj.Util.Value_Of
3841 (Variable_Name => Name_Global_Configuration_Pragmas,
3842 In_Variables => Project_Tree.Shared.Packages.Table
3843 (Gnatmake).Decl.Attributes,
3844 Shared => Project_Tree.Shared);
3845 Global_Attribute_Present :=
3846 Global_Attribute /= Nil_Variable_Value
3847 and then Get_Name_String (Global_Attribute.Value) /= "";
3849 if Global_Attribute_Present then
3850 declare
3851 Path : constant String :=
3852 Absolute_Path
3853 (Path_Name_Type (Global_Attribute.Value),
3854 Global_Attribute.Project);
3855 begin
3856 if not Is_Regular_File (Path) then
3857 if Debug.Debug_Flag_F then
3858 Make_Failed
3859 ("cannot find configuration pragmas file "
3860 & File_Name (Path));
3861 else
3862 Make_Failed
3863 ("cannot find configuration pragmas file " & Path);
3864 end if;
3865 end if;
3867 Last := Last + 1;
3868 Result (Last) := new String'("-gnatec=" & Path);
3869 end;
3870 end if;
3871 end if;
3873 -- Check for attribute Compiler'Local_Configuration_Pragmas
3875 The_Packages := For_Project.Decl.Packages;
3876 Compiler :=
3877 Prj.Util.Value_Of
3878 (Name => Name_Compiler,
3879 In_Packages => The_Packages,
3880 Shared => Project_Tree.Shared);
3882 if Compiler /= No_Package then
3883 Local_Attribute := Prj.Util.Value_Of
3884 (Variable_Name => Name_Local_Configuration_Pragmas,
3885 In_Variables => Project_Tree.Shared.Packages.Table
3886 (Compiler).Decl.Attributes,
3887 Shared => Project_Tree.Shared);
3888 Local_Attribute_Present :=
3889 Local_Attribute /= Nil_Variable_Value
3890 and then Get_Name_String (Local_Attribute.Value) /= "";
3892 if Local_Attribute_Present then
3893 declare
3894 Path : constant String :=
3895 Absolute_Path
3896 (Path_Name_Type (Local_Attribute.Value),
3897 Local_Attribute.Project);
3898 begin
3899 if not Is_Regular_File (Path) then
3900 if Debug.Debug_Flag_F then
3901 Make_Failed
3902 ("cannot find configuration pragmas file "
3903 & File_Name (Path));
3905 else
3906 Make_Failed
3907 ("cannot find configuration pragmas file " & Path);
3908 end if;
3909 end if;
3911 Last := Last + 1;
3912 Result (Last) := new String'("-gnatec=" & Path);
3913 end;
3914 end if;
3915 end if;
3917 return Result (1 .. Last);
3918 end Configuration_Pragmas_Switch;
3920 ---------------
3921 -- Debug_Msg --
3922 ---------------
3924 procedure Debug_Msg (S : String; N : Name_Id) is
3925 begin
3926 if Debug.Debug_Flag_W then
3927 Write_Str (" ... ");
3928 Write_Str (S);
3929 Write_Str (" ");
3930 Write_Name (N);
3931 Write_Eol;
3932 end if;
3933 end Debug_Msg;
3935 procedure Debug_Msg (S : String; N : File_Name_Type) is
3936 begin
3937 Debug_Msg (S, Name_Id (N));
3938 end Debug_Msg;
3940 procedure Debug_Msg (S : String; N : Unit_Name_Type) is
3941 begin
3942 Debug_Msg (S, Name_Id (N));
3943 end Debug_Msg;
3945 -------------
3946 -- Display --
3947 -------------
3949 procedure Display (Program : String; Args : Argument_List) is
3950 begin
3951 pragma Assert (Args'First = 1);
3953 if Display_Executed_Programs then
3954 Write_Str (Program);
3956 for J in Args'Range loop
3958 -- Never display -gnatea nor -gnatez
3960 if Args (J).all /= "-gnatea"
3961 and then
3962 Args (J).all /= "-gnatez"
3963 then
3964 -- Do not display the mapping file argument automatically
3965 -- created when using a project file.
3967 if Main_Project = No_Project
3968 or else Opt.Keep_Temporary_Files
3969 or else Args (J)'Length < 8
3970 or else
3971 Args (J) (Args (J)'First .. Args (J)'First + 6) /= "-gnatem"
3972 then
3973 -- When -dn is not specified, do not display the config
3974 -- pragmas switch (-gnatec) for the temporary file created
3975 -- by the project manager (always the first -gnatec switch).
3976 -- Reset Temporary_Config_File to False so that the eventual
3977 -- other -gnatec switches will be displayed.
3979 if not Opt.Keep_Temporary_Files
3980 and then Temporary_Config_File
3981 and then Args (J)'Length > 7
3982 and then Args (J) (Args (J)'First .. Args (J)'First + 6) =
3983 "-gnatec"
3984 then
3985 Temporary_Config_File := False;
3987 -- Do not display the -F=mapping_file switch for gnatbind
3988 -- if -dn is not specified.
3990 elsif Opt.Keep_Temporary_Files
3991 or else Args (J)'Length < 4
3992 or else
3993 Args (J) (Args (J)'First .. Args (J)'First + 2) /= "-F="
3994 then
3995 Write_Str (" ");
3997 -- If -df is used, only display file names, not path
3998 -- names.
4000 if Debug.Debug_Flag_F then
4001 declare
4002 Equal_Pos : Natural;
4004 begin
4005 Equal_Pos := Args (J)'First - 1;
4006 for K in Args (J)'Range loop
4007 if Args (J) (K) = '=' then
4008 Equal_Pos := K;
4009 exit;
4010 end if;
4011 end loop;
4013 if Is_Absolute_Path
4014 (Args (J) (Equal_Pos + 1 .. Args (J)'Last))
4015 then
4016 Write_Str
4017 (Args (J) (Args (J)'First .. Equal_Pos));
4018 Write_Str
4019 (File_Name
4020 (Args (J)
4021 (Equal_Pos + 1 .. Args (J)'Last)));
4023 else
4024 Write_Str (Args (J).all);
4025 end if;
4026 end;
4028 else
4029 Write_Str (Args (J).all);
4030 end if;
4031 end if;
4032 end if;
4033 end if;
4034 end loop;
4036 Write_Eol;
4037 end if;
4038 end Display;
4040 ----------------------
4041 -- Display_Commands --
4042 ----------------------
4044 procedure Display_Commands (Display : Boolean := True) is
4045 begin
4046 Display_Executed_Programs := Display;
4047 end Display_Commands;
4049 --------------------------
4050 -- Enter_Into_Obsoleted --
4051 --------------------------
4053 procedure Enter_Into_Obsoleted (F : File_Name_Type) is
4054 Name : constant String := Get_Name_String (F);
4055 First : Natural;
4056 F2 : File_Name_Type;
4058 begin
4059 First := Name'Last;
4060 while First > Name'First
4061 and then not Is_Directory_Separator (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 -- Linking_Phase --
4081 -------------------
4083 procedure Linking_Phase
4084 (Non_Std_Executable : Boolean := False;
4085 Executable : File_Name_Type := No_File;
4086 Main_ALI_File : File_Name_Type)
4088 Linker_Switches_Last : constant Integer := Linker_Switches.Last;
4089 Path_Option : constant String_Access :=
4090 MLib.Linker_Library_Path_Option;
4091 Libraries_Present : Boolean := False;
4092 Current : Natural;
4093 Proj2 : Project_Id;
4094 Depth : Natural;
4095 Proj1 : Project_List;
4097 begin
4098 if not Run_Path_Option then
4099 Linker_Switches.Increment_Last;
4100 Linker_Switches.Table (Linker_Switches.Last) :=
4101 new String'("-R");
4102 end if;
4104 if Main_Project /= No_Project then
4105 Library_Paths.Set_Last (0);
4106 Library_Projs.Init;
4108 if MLib.Tgt.Support_For_Libraries /= Prj.None then
4110 -- Check for library projects
4112 Proj1 := Project_Tree.Projects;
4113 while Proj1 /= null loop
4114 if Proj1.Project /= Main_Project
4115 and then Proj1.Project.Library
4116 then
4117 -- Add this project to table Library_Projs
4119 Libraries_Present := True;
4120 Depth := Proj1.Project.Depth;
4121 Library_Projs.Increment_Last;
4122 Current := Library_Projs.Last;
4124 -- Any project with a greater depth should be after this
4125 -- project in the list.
4127 while Current > 1 loop
4128 Proj2 := Library_Projs.Table (Current - 1);
4129 exit when Proj2.Depth <= Depth;
4130 Library_Projs.Table (Current) := Proj2;
4131 Current := Current - 1;
4132 end loop;
4134 Library_Projs.Table (Current) := Proj1.Project;
4136 -- If it is not a static library and path option is set, add
4137 -- it to the Library_Paths table.
4139 if Proj1.Project.Library_Kind /= Static
4140 and then Proj1.Project.Extended_By = No_Project
4141 and then Path_Option /= null
4142 then
4143 Library_Paths.Increment_Last;
4144 Library_Paths.Table (Library_Paths.Last) :=
4145 new String'
4146 (Get_Name_String
4147 (Proj1.Project.Library_Dir.Display_Name));
4148 end if;
4149 end if;
4151 Proj1 := Proj1.Next;
4152 end loop;
4154 for Index in 1 .. Library_Projs.Last loop
4155 if Library_Projs.Table (Index).Extended_By = No_Project then
4156 if Library_Projs.Table (Index).Library_Kind = Static then
4157 Linker_Switches.Increment_Last;
4158 Linker_Switches.Table (Linker_Switches.Last) :=
4159 new String'
4160 (Get_Name_String
4161 (Library_Projs.Table
4162 (Index).Library_Dir.Display_Name) &
4163 "lib" &
4164 Get_Name_String
4165 (Library_Projs.Table (Index).Library_Name) &
4166 "." &
4167 MLib.Tgt.Archive_Ext);
4169 else
4170 -- Add the -L switch
4172 Linker_Switches.Increment_Last;
4173 Linker_Switches.Table (Linker_Switches.Last) :=
4174 new String'("-L" &
4175 Get_Name_String (Library_Projs.Table (Index).
4176 Library_Dir.Display_Name));
4178 -- Add the -l switch
4180 Linker_Switches.Increment_Last;
4181 Linker_Switches.Table (Linker_Switches.Last) :=
4182 new String'("-l" &
4183 Get_Name_String
4184 (Library_Projs.Table (Index).Library_Name));
4185 end if;
4186 end if;
4187 end loop;
4188 end if;
4190 if Libraries_Present then
4192 -- If Path_Option is not null, create the switch ("-Wl,-rpath,"
4193 -- or equivalent) with all the non-static library dirs plus the
4194 -- standard GNAT library dir. We do that only if Run_Path_Option
4195 -- is True (not disabled by -R switch).
4197 if Run_Path_Option and then Path_Option /= null then
4198 declare
4199 Option : String_Access;
4200 Length : Natural := Path_Option'Length;
4201 Current : Natural;
4203 begin
4204 if MLib.Separate_Run_Path_Options then
4206 -- We are going to create one switch of the form
4207 -- "-Wl,-rpath,dir_N" for each directory to
4208 -- consider.
4210 -- One switch for each library directory
4212 for Index in
4213 Library_Paths.First .. Library_Paths.Last
4214 loop
4215 Linker_Switches.Increment_Last;
4216 Linker_Switches.Table (Linker_Switches.Last) :=
4217 new String'
4218 (Path_Option.all &
4219 Library_Paths.Table (Index).all);
4220 end loop;
4222 -- One switch for the standard GNAT library dir
4224 Linker_Switches.Increment_Last;
4225 Linker_Switches.Table (Linker_Switches.Last) :=
4226 new String'(Path_Option.all & MLib.Utl.Lib_Directory);
4228 else
4229 -- We are going to create one switch of the form
4230 -- "-Wl,-rpath,dir_1:dir_2:dir_3"
4232 for Index in Library_Paths.First .. Library_Paths.Last
4233 loop
4234 -- Add the length of the library dir plus one for the
4235 -- directory separator.
4237 Length :=
4238 Length + Library_Paths.Table (Index)'Length + 1;
4239 end loop;
4241 -- Finally, add the length of the standard GNAT
4242 -- library dir.
4244 Length := Length + MLib.Utl.Lib_Directory'Length;
4245 Option := new String (1 .. Length);
4246 Option (1 .. Path_Option'Length) := Path_Option.all;
4247 Current := Path_Option'Length;
4249 -- Put each library dir followed by a dir
4250 -- separator.
4252 for Index in Library_Paths.First .. Library_Paths.Last
4253 loop
4254 Option
4255 (Current + 1 ..
4256 Current + Library_Paths.Table (Index)'Length) :=
4257 Library_Paths.Table (Index).all;
4258 Current :=
4259 Current + Library_Paths.Table (Index)'Length + 1;
4260 Option (Current) := Path_Separator;
4261 end loop;
4263 -- Finally put the standard GNAT library dir
4265 Option
4266 (Current + 1 ..
4267 Current + MLib.Utl.Lib_Directory'Length) :=
4268 MLib.Utl.Lib_Directory;
4270 -- And add the switch to the linker switches
4272 Linker_Switches.Increment_Last;
4273 Linker_Switches.Table (Linker_Switches.Last) := Option;
4274 end if;
4275 end;
4276 end if;
4277 end if;
4279 -- Put the object directories in ADA_OBJECTS_PATH
4281 Prj.Env.Set_Ada_Paths
4282 (Main_Project,
4283 Project_Tree,
4284 Including_Libraries => False,
4285 Include_Path => False);
4287 -- Check for attributes Linker'Linker_Options in projects other than
4288 -- the main project
4290 declare
4291 Linker_Options : constant String_List :=
4292 Linker_Options_Switches
4293 (Main_Project,
4294 Do_Fail => Make_Failed'Access,
4295 In_Tree => Project_Tree);
4296 begin
4297 for Option in Linker_Options'Range loop
4298 Linker_Switches.Increment_Last;
4299 Linker_Switches.Table (Linker_Switches.Last) :=
4300 Linker_Options (Option);
4301 end loop;
4302 end;
4303 end if;
4305 if CodePeer_Mode then
4306 Linker_Switches.Increment_Last;
4307 Linker_Switches.Table (Linker_Switches.Last) :=
4308 new String'(CodePeer_Mode_String);
4309 end if;
4311 -- Add switch -M to gnatlink if builder switch --create-map-file
4312 -- has been specified.
4314 if Map_File /= null then
4315 Linker_Switches.Increment_Last;
4316 Linker_Switches.Table (Linker_Switches.Last) :=
4317 new String'("-M" & Map_File.all);
4318 end if;
4320 declare
4321 Args : Argument_List
4322 (Linker_Switches.First .. Linker_Switches.Last + 2);
4324 Last_Arg : Integer := Linker_Switches.First - 1;
4325 Skip : Boolean := False;
4327 begin
4328 -- Get all the linker switches
4330 for J in Linker_Switches.First .. Linker_Switches.Last loop
4331 if Skip then
4332 Skip := False;
4334 elsif Non_Std_Executable
4335 and then Linker_Switches.Table (J).all = "-o"
4336 then
4337 Skip := True;
4339 -- Here we capture and duplicate the linker argument. We
4340 -- need to do the duplication since the arguments will get
4341 -- normalized. Not doing so will result in calling normalized
4342 -- two times for the same set of arguments if gnatmake is
4343 -- passed multiple mains. This can result in the wrong
4344 -- argument being passed to the linker.
4346 else
4347 Last_Arg := Last_Arg + 1;
4348 Args (Last_Arg) := new String'(Linker_Switches.Table (J).all);
4349 end if;
4350 end loop;
4352 -- If need be, add the -o switch
4354 if Non_Std_Executable then
4355 Last_Arg := Last_Arg + 1;
4356 Args (Last_Arg) := new String'("-o");
4357 Last_Arg := Last_Arg + 1;
4358 Args (Last_Arg) := new String'(Get_Name_String (Executable));
4359 end if;
4361 -- And invoke the linker
4363 declare
4364 Success : Boolean := False;
4366 begin
4367 -- If gnatmake was invoked with --subdirs and no project file,
4368 -- put the executable in the subdirectory specified.
4370 if Prj.Subdirs /= null and then Main_Project = No_Project then
4371 Change_Dir (Object_Directory_Path.all);
4372 end if;
4374 Link (Main_ALI_File,
4375 Link_With_Shared_Libgcc.all &
4376 Args (Args'First .. Last_Arg),
4377 Success);
4379 if Success then
4380 Successful_Links.Increment_Last;
4381 Successful_Links.Table (Successful_Links.Last) := Main_ALI_File;
4383 elsif Osint.Number_Of_Files = 1 or else not Keep_Going then
4384 Make_Failed ("*** link failed.");
4386 else
4387 Set_Standard_Error;
4388 Write_Line ("*** link failed");
4390 if Commands_To_Stdout then
4391 Set_Standard_Output;
4392 end if;
4394 Failed_Links.Increment_Last;
4395 Failed_Links.Table (Failed_Links.Last) := Main_ALI_File;
4396 end if;
4397 end;
4398 end;
4400 Linker_Switches.Set_Last (Linker_Switches_Last);
4401 end Linking_Phase;
4403 -------------------
4404 -- Binding_Phase --
4405 -------------------
4407 procedure Binding_Phase
4408 (Stand_Alone_Libraries : Boolean := False;
4409 Main_ALI_File : File_Name_Type)
4411 Args : Argument_List (Binder_Switches.First .. Binder_Switches.Last + 2);
4412 -- The arguments for the invocation of gnatbind
4414 Last_Arg : Natural := Binder_Switches.Last;
4415 -- Index of the last argument in Args
4417 Shared_Libs : Boolean := False;
4418 -- Set to True when there are shared library project files or
4419 -- when gnatbind is invoked with -shared.
4421 Proj : Project_List;
4423 Mapping_Path : Path_Name_Type := No_Path;
4424 -- The path name of the mapping file
4426 begin
4427 -- Check if there are shared libraries, so that gnatbind is called with
4428 -- -shared. Check also if gnatbind is called with -shared, so that
4429 -- gnatlink is called with -shared-libgcc ensuring that the shared
4430 -- version of libgcc will be used.
4432 if Main_Project /= No_Project
4433 and then MLib.Tgt.Support_For_Libraries /= Prj.None
4434 then
4435 Proj := Project_Tree.Projects;
4436 while Proj /= null loop
4437 if Proj.Project.Library
4438 and then Proj.Project.Library_Kind /= Static
4439 then
4440 Shared_Libs := True;
4441 Bind_Shared := Shared_Switch'Access;
4442 exit;
4443 end if;
4445 Proj := Proj.Next;
4446 end loop;
4447 end if;
4449 -- Check now for switch -shared
4451 if not Shared_Libs then
4452 for J in Binder_Switches.First .. Last_Arg loop
4453 if Binder_Switches.Table (J).all = "-shared" then
4454 Shared_Libs := True;
4455 exit;
4456 end if;
4457 end loop;
4458 end if;
4460 -- If shared libraries present, invoke gnatlink with
4461 -- -shared-libgcc.
4463 if Shared_Libs then
4464 Link_With_Shared_Libgcc := Shared_Libgcc_Switch'Access;
4465 end if;
4467 -- Get all the binder switches
4469 for J in Binder_Switches.First .. Last_Arg loop
4470 Args (J) := Binder_Switches.Table (J);
4471 end loop;
4473 if Stand_Alone_Libraries then
4474 Last_Arg := Last_Arg + 1;
4475 Args (Last_Arg) := Force_Elab_Flags_String'Access;
4476 end if;
4478 if CodePeer_Mode then
4479 Last_Arg := Last_Arg + 1;
4480 Args (Last_Arg) := CodePeer_Mode_String'Access;
4481 end if;
4483 if Main_Project /= No_Project then
4485 -- Put all the source directories in ADA_INCLUDE_PATH, and all the
4486 -- object directories in ADA_OBJECTS_PATH.
4488 Prj.Env.Set_Ada_Paths
4489 (Project => Main_Project,
4490 In_Tree => Project_Tree,
4491 Including_Libraries => True,
4492 Include_Path => Use_Include_Path_File);
4494 -- If switch -C was specified, create a binder mapping file
4496 if Create_Mapping_File then
4497 Mapping_Path := Create_Binder_Mapping_File (Project_Tree);
4499 if Mapping_Path /= No_Path then
4500 Last_Arg := Last_Arg + 1;
4501 Args (Last_Arg) :=
4502 new String'("-F=" & Get_Name_String (Mapping_Path));
4503 end if;
4504 end if;
4505 end if;
4507 -- If gnatmake was invoked with --subdirs and no project file, put the
4508 -- binder generated files in the subdirectory specified.
4510 if Main_Project = No_Project and then Prj.Subdirs /= null then
4511 Change_Dir (Object_Directory_Path.all);
4512 end if;
4514 begin
4515 Bind (Main_ALI_File,
4516 Bind_Shared.all & Args (Args'First .. Last_Arg));
4518 exception
4519 when others =>
4521 -- Delete the temporary mapping file if one was created
4523 if Mapping_Path /= No_Path then
4524 Delete_Temporary_File (Project_Tree.Shared, Mapping_Path);
4525 end if;
4527 -- And reraise the exception
4529 raise;
4530 end;
4532 -- If -dn was not specified, delete the temporary mapping file
4533 -- if one was created.
4535 if Mapping_Path /= No_Path then
4536 Delete_Temporary_File (Project_Tree.Shared, Mapping_Path);
4537 end if;
4538 end Binding_Phase;
4540 -------------------
4541 -- Library_Phase --
4542 -------------------
4544 procedure Library_Phase
4545 (Stand_Alone_Libraries : in out Boolean;
4546 Library_Rebuilt : in out Boolean)
4548 Depth : Natural;
4549 Current : Natural;
4550 Proj1 : Project_List;
4552 procedure Add_To_Library_Projs (Proj : Project_Id);
4553 -- Add project Project to table Library_Projs in decreasing depth order
4555 --------------------------
4556 -- Add_To_Library_Projs --
4557 --------------------------
4559 procedure Add_To_Library_Projs (Proj : Project_Id) is
4560 Prj : Project_Id;
4562 begin
4563 Library_Projs.Increment_Last;
4564 Depth := Proj.Depth;
4566 -- Put the projects in decreasing depth order, so that
4567 -- if libA depends on libB, libB is first in order.
4569 Current := Library_Projs.Last;
4570 while Current > 1 loop
4571 Prj := Library_Projs.Table (Current - 1);
4572 exit when Prj.Depth >= Depth;
4573 Library_Projs.Table (Current) := Prj;
4574 Current := Current - 1;
4575 end loop;
4577 Library_Projs.Table (Current) := Proj;
4578 end Add_To_Library_Projs;
4580 -- Start of processing for Library_Phase
4582 begin
4583 Library_Projs.Init;
4585 -- Put in Library_Projs table all library project file ids when the
4586 -- library need to be rebuilt.
4588 Proj1 := Project_Tree.Projects;
4589 while Proj1 /= null loop
4590 if Proj1.Project.Extended_By = No_Project then
4591 if Proj1.Project.Standalone_Library /= No then
4592 Stand_Alone_Libraries := True;
4593 end if;
4595 if Proj1.Project.Library then
4596 MLib.Prj.Check_Library
4597 (Proj1.Project, Project_Tree);
4598 end if;
4600 if Proj1.Project.Need_To_Build_Lib then
4601 Add_To_Library_Projs (Proj1.Project);
4602 end if;
4603 end if;
4605 Proj1 := Proj1.Next;
4606 end loop;
4608 -- Check if importing libraries should be regenerated
4609 -- because at least an imported library will be
4610 -- regenerated or is more recent.
4612 Proj1 := Project_Tree.Projects;
4613 while Proj1 /= null loop
4614 if Proj1.Project.Library
4615 and then Proj1.Project.Extended_By = No_Project
4616 and then Proj1.Project.Library_Kind /= Static
4617 and then not Proj1.Project.Need_To_Build_Lib
4618 and then not Proj1.Project.Externally_Built
4619 then
4620 declare
4621 List : Project_List;
4622 Proj2 : Project_Id;
4623 Rebuild : Boolean := False;
4625 Lib_Timestamp1 : constant Time_Stamp_Type :=
4626 Proj1.Project.Library_TS;
4628 begin
4629 List := Proj1.Project.All_Imported_Projects;
4630 while List /= null loop
4631 Proj2 := List.Project;
4633 if Proj2.Library then
4634 if Proj2.Need_To_Build_Lib
4635 or else
4636 (Lib_Timestamp1 < Proj2.Library_TS)
4637 then
4638 Rebuild := True;
4639 exit;
4640 end if;
4641 end if;
4643 List := List.Next;
4644 end loop;
4646 if Rebuild then
4647 Proj1.Project.Need_To_Build_Lib := True;
4648 Add_To_Library_Projs (Proj1.Project);
4649 end if;
4650 end;
4651 end if;
4653 Proj1 := Proj1.Next;
4654 end loop;
4656 -- Reset the flags Need_To_Build_Lib for the next main, to avoid
4657 -- rebuilding libraries uselessly.
4659 Proj1 := Project_Tree.Projects;
4660 while Proj1 /= null loop
4661 Proj1.Project.Need_To_Build_Lib := False;
4662 Proj1 := Proj1.Next;
4663 end loop;
4665 -- Build the libraries, if any need to be built
4667 for J in 1 .. Library_Projs.Last loop
4668 Library_Rebuilt := True;
4670 -- If a library is rebuilt, then executables are obsolete
4672 Executable_Obsolete := True;
4674 MLib.Prj.Build_Library
4675 (For_Project => Library_Projs.Table (J),
4676 In_Tree => Project_Tree,
4677 Gnatbind => Gnatbind.all,
4678 Gnatbind_Path => Gnatbind_Path,
4679 Gcc => Gcc.all,
4680 Gcc_Path => Gcc_Path);
4681 end loop;
4682 end Library_Phase;
4684 -----------------------
4685 -- Compilation_Phase --
4686 -----------------------
4688 procedure Compilation_Phase
4689 (Main_Source_File : File_Name_Type;
4690 Current_Main_Index : Int := 0;
4691 Total_Compilation_Failures : in out Natural;
4692 Stand_Alone_Libraries : in out Boolean;
4693 Executable : File_Name_Type := No_File;
4694 Is_Last_Main : Boolean;
4695 Stop_Compile : out Boolean)
4697 Args : Argument_List (1 .. Gcc_Switches.Last);
4698 First_Compiled_File : File_Name_Type;
4699 Youngest_Obj_File : File_Name_Type;
4700 Youngest_Obj_Stamp : Time_Stamp_Type;
4702 Is_Main_Unit : Boolean;
4703 -- Set True by Compile_Sources if Main_Source_File can be a main unit
4705 Compilation_Failures : Natural;
4707 Executable_Stamp : Time_Stamp_Type;
4709 Library_Rebuilt : Boolean := False;
4711 begin
4712 Stop_Compile := False;
4714 for J in 1 .. Gcc_Switches.Last loop
4715 Args (J) := Gcc_Switches.Table (J);
4716 end loop;
4718 -- Now we invoke Compile_Sources for the current main
4720 Compile_Sources
4721 (Main_Source => Main_Source_File,
4722 Args => Args,
4723 First_Compiled_File => First_Compiled_File,
4724 Most_Recent_Obj_File => Youngest_Obj_File,
4725 Most_Recent_Obj_Stamp => Youngest_Obj_Stamp,
4726 Main_Unit => Is_Main_Unit,
4727 Main_Index => Current_Main_Index,
4728 Compilation_Failures => Compilation_Failures,
4729 Check_Readonly_Files => Check_Readonly_Files,
4730 Do_Not_Execute => Do_Not_Execute,
4731 Force_Compilations => Force_Compilations,
4732 In_Place_Mode => In_Place_Mode,
4733 Keep_Going => Keep_Going,
4734 Initialize_ALI_Data => True,
4735 Max_Process => Saved_Maximum_Processes);
4737 if Verbose_Mode then
4738 Write_Str ("End of compilation");
4739 Write_Eol;
4740 end if;
4742 Total_Compilation_Failures :=
4743 Total_Compilation_Failures + Compilation_Failures;
4745 if Total_Compilation_Failures /= 0 then
4746 Stop_Compile := True;
4747 return;
4748 end if;
4750 -- Regenerate libraries, if there are any and if object files have been
4751 -- regenerated. Note that we skip this in CodePeer mode because we don't
4752 -- need libraries in this case, and more importantly, the object files
4753 -- may not be present.
4755 if Main_Project /= No_Project
4756 and then not CodePeer_Mode
4757 and then MLib.Tgt.Support_For_Libraries /= Prj.None
4758 and then (Do_Bind_Step
4759 or Unique_Compile_All_Projects
4760 or not Compile_Only)
4761 and then (Do_Link_Step or Is_Last_Main)
4762 then
4763 Library_Phase
4764 (Stand_Alone_Libraries => Stand_Alone_Libraries,
4765 Library_Rebuilt => Library_Rebuilt);
4766 end if;
4768 if List_Dependencies then
4769 if First_Compiled_File /= No_File then
4770 Inform
4771 (First_Compiled_File,
4772 "must be recompiled. Can't generate dependence list.");
4773 else
4774 List_Depend;
4775 end if;
4777 elsif First_Compiled_File = No_File
4778 and then not Do_Bind_Step
4779 and then not Quiet_Output
4780 and then not Library_Rebuilt
4781 and then Osint.Number_Of_Files = 1
4782 then
4783 Inform (Msg => "objects up to date.");
4784 Stop_Compile := True;
4785 return;
4787 elsif Do_Not_Execute and then First_Compiled_File /= No_File then
4788 Write_Name (First_Compiled_File);
4789 Write_Eol;
4790 end if;
4792 -- Stop after compile step if any of:
4794 -- 1) -n (Do_Not_Execute) specified
4796 -- 2) -M (List_Dependencies) specified (also sets
4797 -- Do_Not_Execute above, so this is probably superfluous).
4799 -- 3) -c (Compile_Only) specified, but not -b (Bind_Only)
4801 -- 4) Made unit cannot be a main unit
4803 if ((Do_Not_Execute
4804 or List_Dependencies
4805 or not Do_Bind_Step
4806 or not Is_Main_Unit)
4807 and not No_Main_Subprogram
4808 and not Build_Bind_And_Link_Full_Project)
4809 or Unique_Compile
4810 then
4811 Stop_Compile := True;
4812 return;
4813 end if;
4815 -- If the objects were up-to-date check if the executable file is also
4816 -- up-to-date. For now always bind and link in CodePeer mode where there
4817 -- is no executable.
4819 if not CodePeer_Mode
4820 and then First_Compiled_File = No_File
4821 then
4822 Executable_Stamp := File_Stamp (Executable);
4824 if not Executable_Obsolete then
4825 Executable_Obsolete := Youngest_Obj_Stamp > Executable_Stamp;
4826 end if;
4828 if not Executable_Obsolete then
4829 for Index in reverse 1 .. Dependencies.Last loop
4830 if Is_In_Obsoleted (Dependencies.Table (Index).Depends_On) then
4831 Enter_Into_Obsoleted (Dependencies.Table (Index).This);
4832 end if;
4833 end loop;
4835 Executable_Obsolete := Is_In_Obsoleted (Main_Source_File);
4836 Dependencies.Init;
4837 end if;
4839 if not Executable_Obsolete then
4841 -- If no Ada object files obsolete the executable, check
4842 -- for younger or missing linker files.
4844 Check_Linker_Options
4845 (Executable_Stamp,
4846 Youngest_Obj_File,
4847 Youngest_Obj_Stamp);
4849 Executable_Obsolete := Youngest_Obj_File /= No_File;
4850 end if;
4852 -- Check if any library file is more recent than the
4853 -- executable: there may be an externally built library
4854 -- file that has been modified.
4856 if not Executable_Obsolete and then Main_Project /= No_Project then
4857 declare
4858 Proj1 : Project_List;
4860 begin
4861 Proj1 := Project_Tree.Projects;
4862 while Proj1 /= null loop
4863 if Proj1.Project.Library
4864 and then Proj1.Project.Library_TS > Executable_Stamp
4865 then
4866 Executable_Obsolete := True;
4867 Youngest_Obj_Stamp := Proj1.Project.Library_TS;
4868 Name_Len := 0;
4869 Add_Str_To_Name_Buffer ("library ");
4870 Add_Str_To_Name_Buffer
4871 (Get_Name_String (Proj1.Project.Library_Name));
4872 Youngest_Obj_File := Name_Find;
4873 exit;
4874 end if;
4876 Proj1 := Proj1.Next;
4877 end loop;
4878 end;
4879 end if;
4881 -- Return if the executable is up to date and otherwise
4882 -- motivate the relink/rebind.
4884 if not Executable_Obsolete then
4885 if not Quiet_Output then
4886 Inform (Executable, "up to date.");
4887 end if;
4889 Stop_Compile := True;
4890 return;
4891 end if;
4893 if Executable_Stamp (1) = ' ' then
4894 if not No_Main_Subprogram then
4895 Verbose_Msg (Executable, "missing.", Prefix => " ");
4896 end if;
4898 elsif Youngest_Obj_Stamp (1) = ' ' then
4899 Verbose_Msg
4900 (Youngest_Obj_File, "missing.", Prefix => " ");
4902 elsif Youngest_Obj_Stamp > Executable_Stamp then
4903 Verbose_Msg
4904 (Youngest_Obj_File,
4905 "(" & String (Youngest_Obj_Stamp) & ") newer than",
4906 Executable,
4907 "(" & String (Executable_Stamp) & ")");
4909 else
4910 Verbose_Msg
4911 (Executable, "needs to be rebuilt", Prefix => " ");
4913 end if;
4914 end if;
4915 end Compilation_Phase;
4917 ----------------------------------------
4918 -- Resolve_Relative_Names_In_Switches --
4919 ----------------------------------------
4921 procedure Resolve_Relative_Names_In_Switches (Current_Work_Dir : String) is
4922 begin
4923 -- If a relative path output file has been specified, we add the
4924 -- exec directory.
4926 for J in reverse 1 .. Saved_Linker_Switches.Last - 1 loop
4927 if Saved_Linker_Switches.Table (J).all = Output_Flag.all then
4928 declare
4929 Exec_File_Name : constant String :=
4930 Saved_Linker_Switches.Table (J + 1).all;
4932 begin
4933 if not Is_Absolute_Path (Exec_File_Name) then
4934 Get_Name_String (Main_Project.Exec_Directory.Display_Name);
4935 Add_Str_To_Name_Buffer (Exec_File_Name);
4936 Saved_Linker_Switches.Table (J + 1) :=
4937 new String'(Name_Buffer (1 .. Name_Len));
4938 end if;
4939 end;
4941 exit;
4942 end if;
4943 end loop;
4945 -- If we are using a project file, for relative paths we add the
4946 -- current working directory for any relative path on the command
4947 -- line and the project directory, for any relative path in the
4948 -- project file.
4950 declare
4951 Dir_Path : constant String :=
4952 Get_Name_String (Main_Project.Directory.Display_Name);
4953 begin
4954 for J in 1 .. Binder_Switches.Last loop
4955 Ensure_Absolute_Path
4956 (Binder_Switches.Table (J),
4957 Do_Fail => Make_Failed'Access,
4958 Parent => Dir_Path, For_Gnatbind => True);
4959 end loop;
4961 for J in 1 .. Saved_Binder_Switches.Last loop
4962 Ensure_Absolute_Path
4963 (Saved_Binder_Switches.Table (J),
4964 Do_Fail => Make_Failed'Access,
4965 Parent => Current_Work_Dir,
4966 For_Gnatbind => True);
4967 end loop;
4969 for J in 1 .. Linker_Switches.Last loop
4970 Ensure_Absolute_Path
4971 (Linker_Switches.Table (J),
4972 Parent => Dir_Path,
4973 Do_Fail => Make_Failed'Access);
4974 end loop;
4976 for J in 1 .. Saved_Linker_Switches.Last loop
4977 Ensure_Absolute_Path
4978 (Saved_Linker_Switches.Table (J),
4979 Do_Fail => Make_Failed'Access,
4980 Parent => Current_Work_Dir);
4981 end loop;
4983 for J in 1 .. Gcc_Switches.Last loop
4984 Ensure_Absolute_Path
4985 (Gcc_Switches.Table (J),
4986 Do_Fail => Make_Failed'Access,
4987 Parent => Dir_Path,
4988 Including_Non_Switch => False);
4989 end loop;
4991 for J in 1 .. Saved_Gcc_Switches.Last loop
4992 Ensure_Absolute_Path
4993 (Saved_Gcc_Switches.Table (J),
4994 Parent => Current_Work_Dir,
4995 Do_Fail => Make_Failed'Access,
4996 Including_Non_Switch => False);
4997 end loop;
4998 end;
4999 end Resolve_Relative_Names_In_Switches;
5001 -----------------------------------
5002 -- Queue_Library_Project_Sources --
5003 -----------------------------------
5005 procedure Queue_Library_Project_Sources is
5006 begin
5007 if not Unique_Compile
5008 and then MLib.Tgt.Support_For_Libraries /= Prj.None
5009 then
5010 declare
5011 Proj : Project_List;
5013 begin
5014 Proj := Project_Tree.Projects;
5015 while Proj /= null loop
5016 if Proj.Project.Library then
5017 Proj.Project.Need_To_Build_Lib :=
5018 not MLib.Tgt.Library_Exists_For
5019 (Proj.Project, Project_Tree)
5020 and then not Proj.Project.Externally_Built;
5022 if Proj.Project.Need_To_Build_Lib then
5024 -- If there is no object directory, then it will be
5025 -- impossible to build the library, so fail immediately.
5027 if Proj.Project.Object_Directory = No_Path_Information
5028 then
5029 Make_Failed
5030 ("no object files to build library for"
5031 & " project """
5032 & Get_Name_String (Proj.Project.Name)
5033 & """");
5034 Proj.Project.Need_To_Build_Lib := False;
5036 else
5037 if Verbose_Mode then
5038 Write_Str
5039 ("Library file does not exist for "
5040 & "project """);
5041 Write_Str
5042 (Get_Name_String (Proj.Project.Name));
5043 Write_Line ("""");
5044 end if;
5046 Insert_Project_Sources
5047 (The_Project => Proj.Project,
5048 All_Projects => False,
5049 Into_Q => True);
5050 end if;
5051 end if;
5052 end if;
5054 Proj := Proj.Next;
5055 end loop;
5056 end;
5057 end if;
5058 end Queue_Library_Project_Sources;
5060 ------------------------
5061 -- Compute_Executable --
5062 ------------------------
5064 procedure Compute_Executable
5065 (Main_Source_File : File_Name_Type;
5066 Executable : out File_Name_Type;
5067 Non_Std_Executable : out Boolean)
5069 begin
5070 Executable := No_File;
5071 Non_Std_Executable :=
5072 Targparm.Executable_Extension_On_Target /= No_Name;
5074 -- Look inside the linker switches to see if the name of the final
5075 -- executable program was specified.
5077 for J in reverse Linker_Switches.First .. Linker_Switches.Last loop
5078 if Linker_Switches.Table (J).all = Output_Flag.all then
5079 pragma Assert (J < Linker_Switches.Last);
5081 -- We cannot specify a single executable for several main
5082 -- subprograms
5084 if Osint.Number_Of_Files > 1 then
5085 Fail ("cannot specify a single executable for several mains");
5086 end if;
5088 Name_Len := 0;
5089 Add_Str_To_Name_Buffer (Linker_Switches.Table (J + 1).all);
5090 Executable := Name_Enter;
5092 Verbose_Msg (Executable, "final executable");
5093 end if;
5094 end loop;
5096 -- If the name of the final executable program was not specified then
5097 -- construct it from the main input file.
5099 if Executable = No_File then
5100 if Main_Project = No_Project then
5101 Executable := Executable_Name (Strip_Suffix (Main_Source_File));
5103 else
5104 -- If we are using a project file, we attempt to remove the body
5105 -- (or spec) termination of the main subprogram. We find it the
5106 -- naming scheme of the project file. This avoids generating an
5107 -- executable "main.2" for a main subprogram "main.2.ada", when
5108 -- the body termination is ".2.ada".
5110 Executable :=
5111 Prj.Util.Executable_Of
5112 (Main_Project, Project_Tree.Shared,
5113 Main_Source_File, Main_Index);
5114 end if;
5115 end if;
5117 if Main_Project /= No_Project
5118 and then Main_Project.Exec_Directory /= No_Path_Information
5119 then
5120 declare
5121 Exec_File_Name : constant String := Get_Name_String (Executable);
5122 begin
5123 if not Is_Absolute_Path (Exec_File_Name) then
5124 Get_Name_String (Main_Project.Exec_Directory.Display_Name);
5125 Add_Str_To_Name_Buffer (Exec_File_Name);
5126 Executable := Name_Find;
5127 end if;
5129 Non_Std_Executable := True;
5130 end;
5131 end if;
5132 end Compute_Executable;
5134 -------------------------------
5135 -- Compute_Switches_For_Main --
5136 -------------------------------
5138 procedure Compute_Switches_For_Main
5139 (Main_Source_File : in out File_Name_Type;
5140 Root_Environment : in out Prj.Tree.Environment;
5141 Compute_Builder : Boolean;
5142 Current_Work_Dir : String)
5144 function Add_Global_Switches
5145 (Switch : String;
5146 For_Lang : Name_Id;
5147 For_Builder : Boolean;
5148 Has_Global_Compilation_Switches : Boolean) return Boolean;
5149 -- Handles builder and global compilation switches, as read from the
5150 -- project file.
5152 -------------------------
5153 -- Add_Global_Switches --
5154 -------------------------
5156 function Add_Global_Switches
5157 (Switch : String;
5158 For_Lang : Name_Id;
5159 For_Builder : Boolean;
5160 Has_Global_Compilation_Switches : Boolean) return Boolean
5162 pragma Unreferenced (For_Lang);
5164 begin
5165 if For_Builder then
5166 Program_Args := None;
5167 Switch_May_Be_Passed_To_The_Compiler :=
5168 not Has_Global_Compilation_Switches;
5169 Scan_Make_Arg (Root_Environment, Switch, And_Save => False);
5171 return Gnatmake_Switch_Found
5172 or else Switch_May_Be_Passed_To_The_Compiler;
5173 else
5174 Add_Switch (Switch, Compiler, And_Save => False);
5175 return True;
5176 end if;
5177 end Add_Global_Switches;
5179 procedure Do_Compute_Builder_Switches
5180 is new Makeutl.Compute_Builder_Switches (Add_Global_Switches);
5182 -- Start of processing for Compute_Switches_For_Main
5184 begin
5185 if Main_Project /= No_Project then
5186 declare
5187 Main_Source_File_Name : constant String :=
5188 Get_Name_String (Main_Source_File);
5190 Main_Unit_File_Name : constant String :=
5191 Prj.Env.File_Name_Of_Library_Unit_Body
5192 (Name => Main_Source_File_Name,
5193 Project => Main_Project,
5194 In_Tree => Project_Tree,
5195 Main_Project_Only => not Unique_Compile);
5197 The_Packages : constant Package_Id := Main_Project.Decl.Packages;
5199 Binder_Package : constant Prj.Package_Id :=
5200 Prj.Util.Value_Of
5201 (Name => Name_Binder,
5202 In_Packages => The_Packages,
5203 Shared => Project_Tree.Shared);
5205 Linker_Package : constant Prj.Package_Id :=
5206 Prj.Util.Value_Of
5207 (Name => Name_Linker,
5208 In_Packages => The_Packages,
5209 Shared => Project_Tree.Shared);
5211 begin
5212 -- We fail if we cannot find the main source file
5214 if Main_Unit_File_Name = "" then
5215 Make_Failed ('"' & Main_Source_File_Name
5216 & """ is not a unit of project "
5217 & Project_File_Name.all & ".");
5218 end if;
5220 -- Remove any directory information from the main source file
5221 -- file name.
5223 declare
5224 Pos : Natural := Main_Unit_File_Name'Last;
5226 begin
5227 loop
5228 exit when Pos < Main_Unit_File_Name'First
5229 or else Main_Unit_File_Name (Pos) = Directory_Separator;
5230 Pos := Pos - 1;
5231 end loop;
5233 Name_Len := Main_Unit_File_Name'Last - Pos;
5235 Name_Buffer (1 .. Name_Len) :=
5236 Main_Unit_File_Name (Pos + 1 .. Main_Unit_File_Name'Last);
5238 Main_Source_File := Name_Find;
5240 -- We only output the main source file if there is only one
5242 if Verbose_Mode and then Osint.Number_Of_Files = 1 then
5243 Write_Str ("Main source file: """);
5244 Write_Str (Main_Unit_File_Name
5245 (Pos + 1 .. Main_Unit_File_Name'Last));
5246 Write_Line (""".");
5247 end if;
5248 end;
5250 if Compute_Builder then
5251 Do_Compute_Builder_Switches
5252 (Project_Tree => Project_Tree,
5253 Env => Root_Environment,
5254 Main_Project => Main_Project,
5255 Only_For_Lang => Name_Ada);
5257 Resolve_Relative_Names_In_Switches
5258 (Current_Work_Dir => Current_Work_Dir);
5260 -- Record current last switch index for tables Binder_Switches
5261 -- and Linker_Switches, so that these tables may be reset
5262 -- before each main, before adding switches from the project
5263 -- file and from the command line.
5265 Last_Binder_Switch := Binder_Switches.Last;
5266 Last_Linker_Switch := Linker_Switches.Last;
5268 else
5269 -- Reset the tables Binder_Switches and Linker_Switches
5271 Binder_Switches.Set_Last (Last_Binder_Switch);
5272 Linker_Switches.Set_Last (Last_Linker_Switch);
5273 end if;
5275 -- We now deal with the binder and linker switches. If no project
5276 -- file is used, there is nothing to do because the binder and
5277 -- linker switches are the same for all mains.
5279 -- Add binder switches from the project file for the first main
5281 if Do_Bind_Step and then Binder_Package /= No_Package then
5282 if Verbose_Mode then
5283 Write_Str ("Adding binder switches for """);
5284 Write_Str (Main_Unit_File_Name);
5285 Write_Line (""".");
5286 end if;
5288 Add_Switches
5289 (Env => Root_Environment,
5290 File_Name => Main_Unit_File_Name,
5291 The_Package => Binder_Package,
5292 Program => Binder);
5293 end if;
5295 -- Add linker switches from the project file for the first main
5297 if Do_Link_Step and then Linker_Package /= No_Package then
5298 if Verbose_Mode then
5299 Write_Str ("Adding linker switches for""");
5300 Write_Str (Main_Unit_File_Name);
5301 Write_Line (""".");
5302 end if;
5304 Add_Switches
5305 (Env => Root_Environment,
5306 File_Name => Main_Unit_File_Name,
5307 The_Package => Linker_Package,
5308 Program => Linker);
5309 end if;
5311 -- As we are using a project file, for relative paths we add the
5312 -- current working directory for any relative path on the command
5313 -- line and the project directory, for any relative path in the
5314 -- project file.
5316 declare
5317 Dir_Path : constant String :=
5318 Get_Name_String (Main_Project.Directory.Display_Name);
5320 begin
5321 for J in Last_Binder_Switch + 1 .. Binder_Switches.Last loop
5322 Ensure_Absolute_Path
5323 (Binder_Switches.Table (J),
5324 Do_Fail => Make_Failed'Access,
5325 Parent => Dir_Path, For_Gnatbind => True);
5326 end loop;
5328 for J in Last_Linker_Switch + 1 .. Linker_Switches.Last loop
5329 Ensure_Absolute_Path
5330 (Linker_Switches.Table (J),
5331 Parent => Dir_Path,
5332 Do_Fail => Make_Failed'Access);
5333 end loop;
5334 end;
5335 end;
5337 else
5338 if not Compute_Builder then
5340 -- Reset the tables Binder_Switches and Linker_Switches
5342 Binder_Switches.Set_Last (Last_Binder_Switch);
5343 Linker_Switches.Set_Last (Last_Linker_Switch);
5344 end if;
5345 end if;
5347 Check_Steps;
5349 if Compute_Builder then
5350 Display_Commands (not Quiet_Output);
5351 end if;
5353 -- We now put in the Binder_Switches and Linker_Switches tables, the
5354 -- binder and linker switches of the command line that have been put in
5355 -- the Saved_ tables. If a project file was used, then the command line
5356 -- switches will follow the project file switches.
5358 for J in 1 .. Saved_Binder_Switches.Last loop
5359 Add_Switch
5360 (Saved_Binder_Switches.Table (J),
5361 Binder,
5362 And_Save => False);
5363 end loop;
5365 for J in 1 .. Saved_Linker_Switches.Last loop
5366 Add_Switch
5367 (Saved_Linker_Switches.Table (J),
5368 Linker,
5369 And_Save => False);
5370 end loop;
5371 end Compute_Switches_For_Main;
5373 --------------
5374 -- Gnatmake --
5375 --------------
5377 procedure Gnatmake is
5378 Main_Source_File : File_Name_Type;
5379 -- The source file containing the main compilation unit
5381 Total_Compilation_Failures : Natural := 0;
5383 Main_ALI_File : File_Name_Type;
5384 -- The ali file corresponding to Main_Source_File
5386 Executable : File_Name_Type := No_File;
5387 -- The file name of an executable
5389 Non_Std_Executable : Boolean := False;
5390 -- Non_Std_Executable is set to True when there is a possibility that
5391 -- the linker will not choose the correct executable file name.
5393 Current_Work_Dir : constant String_Access :=
5394 new String'(Get_Current_Dir);
5395 -- The current working directory, used to modify some relative path
5396 -- switches on the command line when a project file is used.
5398 Current_Main_Index : Int := 0;
5399 -- If not zero, the index of the current main unit in its source file
5401 Is_First_Main : Boolean;
5402 -- Whether we are processing the first main
5404 Stand_Alone_Libraries : Boolean := False;
5405 -- Set to True when there are Stand-Alone Libraries, so that gnatbind
5406 -- is invoked with the -F switch to force checking of elaboration flags.
5408 Project_Node_Tree : Project_Node_Tree_Ref;
5410 Stop_Compile : Boolean;
5412 Discard : Boolean;
5413 pragma Warnings (Off, Discard);
5415 procedure Check_Mains;
5416 -- Check that the main subprograms do exist and that they all
5417 -- belong to the same project file.
5419 -----------------
5420 -- Check_Mains --
5421 -----------------
5423 procedure Check_Mains is
5424 Real_Main_Project : Project_Id := No_Project;
5425 Info : Main_Info;
5426 Proj : Project_Id;
5428 begin
5429 if Mains.Number_Of_Mains (Project_Tree) = 0
5430 and then not Unique_Compile
5431 then
5432 Mains.Fill_From_Project (Main_Project, Project_Tree);
5433 end if;
5435 Mains.Complete_Mains
5436 (Root_Environment.Flags, Main_Project, Project_Tree);
5438 -- If we have multiple mains on the command line, they need not
5439 -- belong to the root project, but they must all belong to the same
5440 -- project.
5442 if not Unique_Compile then
5443 Mains.Reset;
5444 loop
5445 Info := Mains.Next_Main;
5446 exit when Info = No_Main_Info;
5448 Proj := Ultimate_Extending_Project_Of (Info.Project);
5450 if Real_Main_Project = No_Project then
5451 Real_Main_Project := Proj;
5452 elsif Real_Main_Project /= Proj then
5453 Make_Failed
5454 ("""" & Get_Name_String (Info.File) &
5455 """ is not a source of project " &
5456 Get_Name_String (Real_Main_Project.Name));
5457 end if;
5458 end loop;
5460 if Real_Main_Project /= No_Project then
5461 Main_Project := Real_Main_Project;
5462 end if;
5464 Debug_Output ("After checking mains, main project is",
5465 Main_Project.Name);
5467 else
5468 -- For all mains on the command line, make sure they were in
5469 -- osint. In particular, if the user has specified a multi-unit
5470 -- source file, the call to Complete_Mains will have expanded
5471 -- the list of mains to all its units, and we must now put them
5472 -- back on the command line.
5473 -- ??? This will not be necessary when gnatmake shares the same
5474 -- queue as gprbuild and processes the file directly on the queue.
5476 Mains.Reset;
5477 loop
5478 Info := Mains.Next_Main;
5479 exit when Info = No_Main_Info;
5481 if Info.Index /= 0 then
5482 Debug_Output ("Add to command line index="
5483 & Info.Index'Img, Name_Id (Info.File));
5484 Osint.Add_File (Get_Name_String (Info.File), Info.Index);
5485 end if;
5486 end loop;
5487 end if;
5488 end Check_Mains;
5490 -- Start of processing for Gnatmake
5492 -- This body is very long, should be broken down???
5494 begin
5495 Install_Int_Handler (Sigint_Intercepted'Access);
5497 Do_Compile_Step := True;
5498 Do_Bind_Step := True;
5499 Do_Link_Step := True;
5501 Obsoleted.Reset;
5503 Make.Initialize (Project_Node_Tree, Root_Environment);
5505 Bind_Shared := No_Shared_Switch'Access;
5506 Link_With_Shared_Libgcc := No_Shared_Libgcc_Switch'Access;
5508 Failed_Links.Set_Last (0);
5509 Successful_Links.Set_Last (0);
5511 -- Special case when switch -B was specified
5513 if Build_Bind_And_Link_Full_Project then
5515 -- When switch -B is specified, there must be a project file
5517 if Main_Project = No_Project then
5518 Make_Failed ("-B cannot be used without a project file");
5520 -- No main program may be specified on the command line
5522 elsif Osint.Number_Of_Files /= 0 then
5523 Make_Failed
5524 ("-B cannot be used with a main specified on the command line");
5526 -- And the project file cannot be a library project file
5528 elsif Main_Project.Library then
5529 Make_Failed ("-B cannot be used for a library project file");
5531 else
5532 No_Main_Subprogram := True;
5533 Insert_Project_Sources
5534 (The_Project => Main_Project,
5535 All_Projects => Unique_Compile_All_Projects,
5536 Into_Q => False);
5538 -- If there are no sources to compile, we fail
5540 if Osint.Number_Of_Files = 0 then
5541 Make_Failed ("no sources to compile");
5542 end if;
5544 -- Specify -n for gnatbind and add the ALI files of all the
5545 -- sources, except the one which is a fake main subprogram: this
5546 -- is the one for the binder generated file and it will be
5547 -- transmitted to gnatlink. These sources are those that are in
5548 -- the queue.
5550 Add_Switch ("-n", Binder, And_Save => True);
5552 for J in 1 .. Queue.Size loop
5553 Add_Switch
5554 (Get_Name_String (Lib_File_Name (Queue.Element (J))),
5555 Binder, And_Save => True);
5556 end loop;
5557 end if;
5559 elsif Main_Index /= 0 and then Osint.Number_Of_Files > 1 then
5560 Make_Failed ("cannot specify several mains with a multi-unit index");
5562 elsif Main_Project /= No_Project then
5564 -- If the main project file is a library project file, main(s) cannot
5565 -- be specified on the command line.
5567 if Osint.Number_Of_Files /= 0 then
5568 if Main_Project.Library
5569 and then not Unique_Compile
5570 and then ((not Make_Steps) or else Bind_Only or else Link_Only)
5571 then
5572 Make_Failed
5573 ("cannot specify a main program "
5574 & "on the command line for a library project file");
5575 end if;
5577 -- If no mains have been specified on the command line, and we are
5578 -- using a project file, we either find the main(s) in attribute Main
5579 -- of the main project, or we put all the sources of the project file
5580 -- as mains.
5582 else
5583 if Main_Index /= 0 then
5584 Make_Failed ("cannot specify a multi-unit index but no main "
5585 & "on the command line");
5586 end if;
5588 declare
5589 Value : String_List_Id := Main_Project.Mains;
5591 begin
5592 -- The attribute Main is an empty list or not specified, or
5593 -- else gnatmake was invoked with the switch "-u".
5595 if Value = Prj.Nil_String or else Unique_Compile then
5596 if not Make_Steps
5597 or Compile_Only
5598 or not Main_Project.Library
5599 then
5600 -- First make sure that the binder and the linker will
5601 -- not be invoked.
5603 Do_Bind_Step := False;
5604 Do_Link_Step := False;
5606 -- Put all the sources in the queue
5608 No_Main_Subprogram := True;
5609 Insert_Project_Sources
5610 (The_Project => Main_Project,
5611 All_Projects => Unique_Compile_All_Projects,
5612 Into_Q => False);
5614 -- If no sources to compile, then there is nothing to do
5616 if Osint.Number_Of_Files = 0 then
5617 if not Quiet_Output then
5618 Osint.Write_Program_Name;
5619 Write_Line (": no sources to compile");
5620 end if;
5622 Finish_Program (Project_Tree, E_Success);
5623 end if;
5624 end if;
5626 else
5627 -- The attribute Main is not an empty list. Put all the main
5628 -- subprograms in the list as if they were specified on the
5629 -- command line. However, if attribute Languages includes a
5630 -- language other than Ada, only include the Ada mains; if
5631 -- there is no Ada main, compile all sources of the project.
5633 declare
5634 Languages : constant Variable_Value :=
5635 Prj.Util.Value_Of
5636 (Name_Languages,
5637 Main_Project.Decl.Attributes,
5638 Project_Tree.Shared);
5640 Current : String_List_Id;
5641 Element : String_Element;
5643 Foreign_Language : Boolean := False;
5644 At_Least_One_Main : Boolean := False;
5646 begin
5647 -- First, determine if there is a foreign language in
5648 -- attribute Languages.
5650 if not Languages.Default then
5651 Current := Languages.Values;
5652 Look_For_Foreign :
5653 while Current /= Nil_String loop
5654 Element := Project_Tree.Shared.String_Elements.
5655 Table (Current);
5656 Get_Name_String (Element.Value);
5657 To_Lower (Name_Buffer (1 .. Name_Len));
5659 if Name_Buffer (1 .. Name_Len) /= "ada" then
5660 Foreign_Language := True;
5661 exit Look_For_Foreign;
5662 end if;
5664 Current := Element.Next;
5665 end loop Look_For_Foreign;
5666 end if;
5668 -- Then, find all mains, or if there is a foreign
5669 -- language, all the Ada mains.
5671 while Value /= Prj.Nil_String loop
5672 -- To know if a main is an Ada main, get its project.
5673 -- It should be the project specified on the command
5674 -- line.
5676 Get_Name_String
5677 (Project_Tree.Shared.String_Elements.Table
5678 (Value).Value);
5680 declare
5681 Main_Name : constant String :=
5682 Get_Name_String
5683 (Project_Tree.Shared.
5684 String_Elements.
5685 Table (Value).Value);
5687 Proj : constant Project_Id :=
5688 Prj.Env.Project_Of
5689 (Main_Name, Main_Project, Project_Tree);
5691 begin
5692 if Proj = Main_Project then
5693 At_Least_One_Main := True;
5694 Osint.Add_File
5695 (Get_Name_String
5696 (Project_Tree.Shared.String_Elements.Table
5697 (Value).Value),
5698 Index =>
5699 Project_Tree.Shared.String_Elements.Table
5700 (Value).Index);
5702 elsif not Foreign_Language then
5703 Make_Failed
5704 ("""" & Main_Name &
5705 """ is not a source of project " &
5706 Get_Name_String (Main_Project.Display_Name));
5707 end if;
5708 end;
5710 Value := Project_Tree.Shared.String_Elements.Table
5711 (Value).Next;
5712 end loop;
5714 -- If we did not get any main, it means that all mains
5715 -- in attribute Mains are in a foreign language and -B
5716 -- was not specified to gnatmake; so, we fail.
5718 if not At_Least_One_Main then
5719 Make_Failed
5720 ("no Ada mains, use -B to build foreign main");
5721 end if;
5722 end;
5724 end if;
5725 end;
5726 end if;
5728 -- Check that each main on the command line is a source of a
5729 -- project file and, if there are several mains, each of them
5730 -- is a source of the same project file.
5732 Check_Mains;
5733 end if;
5735 if Verbose_Mode then
5736 Write_Eol;
5737 Display_Version ("GNATMAKE", "1992");
5738 end if;
5740 if Osint.Number_Of_Files = 0 then
5741 if Main_Project /= No_Project and then Main_Project.Library then
5742 if Do_Bind_Step and then Main_Project.Standalone_Library = No then
5743 Make_Failed ("only stand-alone libraries may be bound");
5744 end if;
5746 -- Add the default search directories to be able to find libgnat
5748 Osint.Add_Default_Search_Dirs;
5750 -- And bind and or link the library
5752 MLib.Prj.Build_Library
5753 (For_Project => Main_Project,
5754 In_Tree => Project_Tree,
5755 Gnatbind => Gnatbind.all,
5756 Gnatbind_Path => Gnatbind_Path,
5757 Gcc => Gcc.all,
5758 Gcc_Path => Gcc_Path,
5759 Bind => Bind_Only,
5760 Link => Link_Only);
5762 Finish_Program (Project_Tree, E_Success);
5764 else
5765 -- Call Get_Target_Parameters to ensure that flags are properly
5766 -- set before calling Usage.
5768 Targparm.Get_Target_Parameters;
5770 -- Output usage information if no argument on the command line
5772 if Argument_Count = 0 then
5773 Usage;
5774 else
5775 Try_Help;
5776 end if;
5778 Finish_Program (Project_Tree, E_Success);
5779 end if;
5780 end if;
5782 -- Get the first executable.
5783 -- ??? This needs to be done early, because Osint.Next_Main_File also
5784 -- initializes the primary search directory, used below to initialize
5785 -- the "-I" parameter
5787 Main_Source_File := Next_Main_Source; -- No directory information
5789 -- If -M was specified, behave as if -n was specified
5791 if List_Dependencies then
5792 Do_Not_Execute := True;
5793 end if;
5795 Add_Switch ("-I-", Compiler, And_Save => True);
5797 if Main_Project = No_Project then
5798 if Look_In_Primary_Dir then
5799 Add_Switch
5800 ("-I" &
5801 Normalize_Directory_Name
5802 (Get_Primary_Src_Search_Directory.all).all,
5803 Compiler,
5804 Append_Switch => False,
5805 And_Save => False);
5807 end if;
5809 else
5810 -- If we use a project file, we have already checked that a main
5811 -- specified on the command line with directory information has the
5812 -- path name corresponding to a correct source in the project tree.
5813 -- So, we don't need the directory information to be taken into
5814 -- account by Find_File, and in fact it may lead to take the wrong
5815 -- sources for other compilation units, when there are extending
5816 -- projects.
5818 Look_In_Primary_Dir := False;
5819 end if;
5821 -- If the user wants a program without a main subprogram, add the
5822 -- appropriate switch to the binder.
5824 if No_Main_Subprogram then
5825 Add_Switch ("-z", Binder, And_Save => True);
5826 end if;
5828 if Main_Project /= No_Project then
5830 if Main_Project.Object_Directory /= No_Path_Information then
5832 -- Change current directory to object directory of main project
5834 Project_Of_Current_Object_Directory := No_Project;
5835 Change_To_Object_Directory (Main_Project);
5836 end if;
5838 -- Source file lookups should be cached for efficiency. Source files
5839 -- are not supposed to change.
5841 Osint.Source_File_Data (Cache => True);
5843 Queue_Library_Project_Sources;
5844 end if;
5846 -- The combination of -f -u and one or several mains on the command line
5847 -- implies -a.
5849 if Force_Compilations
5850 and then Unique_Compile
5851 and then not Unique_Compile_All_Projects
5852 and then Main_On_Command_Line
5853 then
5854 Must_Compile := True;
5855 end if;
5857 if Main_Project /= No_Project
5858 and then not Must_Compile
5859 and then Main_Project.Externally_Built
5860 then
5861 Make_Failed
5862 ("nothing to do for a main project that is externally built");
5863 end if;
5865 -- If no project file is used, we just put the gcc switches
5866 -- from the command line in the Gcc_Switches table.
5868 if Main_Project = No_Project then
5869 for J in 1 .. Saved_Gcc_Switches.Last loop
5870 Add_Switch
5871 (Saved_Gcc_Switches.Table (J), Compiler, And_Save => False);
5872 end loop;
5874 else
5875 -- If there is a project, put the command line gcc switches in the
5876 -- variable The_Saved_Gcc_Switches. They are going to be used later
5877 -- in procedure Compile_Sources.
5879 The_Saved_Gcc_Switches :=
5880 new Argument_List (1 .. Saved_Gcc_Switches.Last + 1);
5882 for J in 1 .. Saved_Gcc_Switches.Last loop
5883 The_Saved_Gcc_Switches (J) := Saved_Gcc_Switches.Table (J);
5884 end loop;
5886 -- We never use gnat.adc when a project file is used
5888 The_Saved_Gcc_Switches (The_Saved_Gcc_Switches'Last) := No_gnat_adc;
5889 end if;
5891 -- If there was a --GCC, --GNATBIND or --GNATLINK switch on the command
5892 -- line, then we have to use it, even if there was another switch in
5893 -- the project file.
5895 if Saved_Gcc /= null then
5896 Gcc := Saved_Gcc;
5897 end if;
5899 if Saved_Gnatbind /= null then
5900 Gnatbind := Saved_Gnatbind;
5901 end if;
5903 if Saved_Gnatlink /= null then
5904 Gnatlink := Saved_Gnatlink;
5905 end if;
5907 Bad_Compilation.Init;
5909 -- If project files are used, create the mapping of all the sources, so
5910 -- that the correct paths will be found. Otherwise, if there is a file
5911 -- which is not a source with the same name in a source directory this
5912 -- file may be incorrectly found.
5914 if Main_Project /= No_Project then
5915 Prj.Env.Create_Mapping (Project_Tree);
5916 end if;
5918 -- Here is where the make process is started
5920 Queue.Initialize
5921 (Main_Project /= No_Project and then One_Compilation_Per_Obj_Dir);
5923 Is_First_Main := True;
5925 Multiple_Main_Loop : for N_File in 1 .. Osint.Number_Of_Files loop
5926 if Current_File_Index /= No_Index then
5927 Main_Index := Current_File_Index;
5928 end if;
5930 Current_Main_Index := Main_Index;
5932 if Current_Main_Index = 0
5933 and then Unique_Compile
5934 and then Main_Project /= No_Project
5935 then
5936 -- If this is a multi-unit source, do not compile it as is (ie
5937 -- without specifying which unit to compile)
5938 -- Insert_Project_Sources has added each of the unit separately.
5940 declare
5941 Source : constant Prj.Source_Id := Find_Source
5942 (In_Tree => Project_Tree,
5943 Project => Main_Project,
5944 Base_Name => Main_Source_File,
5945 Index => Current_Main_Index,
5946 In_Imported_Only => True);
5947 begin
5948 if Source /= No_Source and then Source.Index /= 0 then
5949 goto Next_Main;
5950 end if;
5951 end;
5952 end if;
5954 Compute_Switches_For_Main
5955 (Main_Source_File,
5956 Root_Environment,
5957 Compute_Builder => Is_First_Main,
5958 Current_Work_Dir => Current_Work_Dir.all);
5960 if Is_First_Main then
5962 -- Put the default source dirs in the source path only now, so
5963 -- that we take the correct ones in the case where --RTS= is
5964 -- specified in the Builder switches.
5966 Osint.Add_Default_Search_Dirs;
5968 -- Get the target parameters, which are only needed for a couple
5969 -- of cases in gnatmake. Protect against an exception, such as the
5970 -- case of system.ads missing from the library, and fail
5971 -- gracefully.
5973 begin
5974 Targparm.Get_Target_Parameters;
5975 exception
5976 when Unrecoverable_Error =>
5977 Make_Failed ("*** make failed.");
5978 end;
5980 Gcc_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
5981 Gnatbind_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gnatbind.all);
5982 Gnatlink_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gnatlink.all);
5984 -- If we have specified -j switch both from the project file
5985 -- and on the command line, the one from the command line takes
5986 -- precedence.
5988 if Saved_Maximum_Processes = 0 then
5989 Saved_Maximum_Processes := Maximum_Processes;
5990 end if;
5992 if Debug.Debug_Flag_M then
5993 Write_Line ("Maximum number of simultaneous compilations =" &
5994 Saved_Maximum_Processes'Img);
5995 end if;
5997 -- Allocate as many temporary mapping file names as the maximum
5998 -- number of compilations processed, for each possible project.
6000 declare
6001 Data : Project_Compilation_Access;
6002 Proj : Project_List;
6004 begin
6005 Proj := Project_Tree.Projects;
6006 while Proj /= null loop
6007 Data :=
6008 new Project_Compilation_Data'
6009 (Mapping_File_Names =>
6010 new Temp_Path_Names (1 .. Saved_Maximum_Processes),
6011 Last_Mapping_File_Names => 0,
6012 Free_Mapping_File_Indexes =>
6013 new Free_File_Indexes (1 .. Saved_Maximum_Processes),
6014 Last_Free_Indexes => 0);
6016 Project_Compilation_Htable.Set
6017 (Project_Compilation, Proj.Project, Data);
6018 Proj := Proj.Next;
6019 end loop;
6021 Data :=
6022 new Project_Compilation_Data'
6023 (Mapping_File_Names =>
6024 new Temp_Path_Names (1 .. Saved_Maximum_Processes),
6025 Last_Mapping_File_Names => 0,
6026 Free_Mapping_File_Indexes =>
6027 new Free_File_Indexes (1 .. Saved_Maximum_Processes),
6028 Last_Free_Indexes => 0);
6030 Project_Compilation_Htable.Set
6031 (Project_Compilation, No_Project, Data);
6032 end;
6034 Is_First_Main := False;
6035 end if;
6037 Executable_Obsolete := False;
6039 Compute_Executable
6040 (Main_Source_File => Main_Source_File,
6041 Executable => Executable,
6042 Non_Std_Executable => Non_Std_Executable);
6044 if Do_Compile_Step then
6045 Compilation_Phase
6046 (Main_Source_File => Main_Source_File,
6047 Current_Main_Index => Current_Main_Index,
6048 Total_Compilation_Failures => Total_Compilation_Failures,
6049 Stand_Alone_Libraries => Stand_Alone_Libraries,
6050 Executable => Executable,
6051 Is_Last_Main => N_File = Osint.Number_Of_Files,
6052 Stop_Compile => Stop_Compile);
6054 if Stop_Compile then
6055 if Total_Compilation_Failures /= 0 then
6056 if Keep_Going then
6057 goto Next_Main;
6059 else
6060 List_Bad_Compilations;
6061 Report_Compilation_Failed;
6062 end if;
6064 elsif Osint.Number_Of_Files = 1 then
6065 exit Multiple_Main_Loop;
6066 else
6067 goto Next_Main;
6068 end if;
6069 end if;
6070 end if;
6072 -- For binding and linking, we need to be in the object directory of
6073 -- the main project.
6075 if Main_Project /= No_Project then
6076 Change_To_Object_Directory (Main_Project);
6077 end if;
6079 -- If we are here, it means that we need to rebuilt the current main,
6080 -- so we set Executable_Obsolete to True to make sure that subsequent
6081 -- mains will be rebuilt.
6083 Main_ALI_In_Place_Mode_Step : declare
6084 ALI_File : File_Name_Type;
6085 Src_File : File_Name_Type;
6087 begin
6088 Src_File := Strip_Directory (Main_Source_File);
6089 ALI_File := Lib_File_Name (Src_File, Current_Main_Index);
6090 Main_ALI_File := Full_Lib_File_Name (ALI_File);
6092 -- When In_Place_Mode, the library file can be located in the
6093 -- Main_Source_File directory which may not be present in the
6094 -- library path. If it is not present then use the corresponding
6095 -- library file name.
6097 if Main_ALI_File = No_File and then In_Place_Mode then
6098 Get_Name_String (Get_Directory (Full_Source_Name (Src_File)));
6099 Get_Name_String_And_Append (ALI_File);
6100 Main_ALI_File := Name_Find;
6101 Main_ALI_File := Full_Lib_File_Name (Main_ALI_File);
6102 end if;
6104 if Main_ALI_File = No_File then
6105 Make_Failed ("could not find the main ALI file");
6106 end if;
6107 end Main_ALI_In_Place_Mode_Step;
6109 if Do_Bind_Step then
6110 Binding_Phase
6111 (Stand_Alone_Libraries => Stand_Alone_Libraries,
6112 Main_ALI_File => Main_ALI_File);
6113 end if;
6115 if Do_Link_Step then
6116 Linking_Phase
6117 (Non_Std_Executable => Non_Std_Executable,
6118 Executable => Executable,
6119 Main_ALI_File => Main_ALI_File);
6120 end if;
6122 -- We go to here when we skip the bind and link steps
6124 <<Next_Main>>
6126 Queue.Remove_Marks;
6128 if N_File < Osint.Number_Of_Files then
6129 Main_Source_File := Next_Main_Source; -- No directory information
6130 end if;
6131 end loop Multiple_Main_Loop;
6133 if Failed_Links.Last > 0 then
6134 for Index in 1 .. Successful_Links.Last loop
6135 Write_Str ("Linking of """);
6136 Write_Str (Get_Name_String (Successful_Links.Table (Index)));
6137 Write_Line (""" succeeded.");
6138 end loop;
6140 Set_Standard_Error;
6142 for Index in 1 .. Failed_Links.Last loop
6143 Write_Str ("Linking of """);
6144 Write_Str (Get_Name_String (Failed_Links.Table (Index)));
6145 Write_Line (""" failed.");
6146 end loop;
6148 if Commands_To_Stdout then
6149 Set_Standard_Output;
6150 end if;
6152 if Total_Compilation_Failures = 0 then
6153 Report_Compilation_Failed;
6154 end if;
6155 end if;
6157 if Total_Compilation_Failures /= 0 then
6158 List_Bad_Compilations;
6159 Report_Compilation_Failed;
6160 end if;
6162 Finish_Program (Project_Tree, E_Success);
6164 exception
6165 when X : others =>
6166 Set_Standard_Error;
6167 Write_Line (Exception_Information (X));
6168 Make_Failed ("INTERNAL ERROR. Please report.");
6169 end Gnatmake;
6171 ----------
6172 -- Hash --
6173 ----------
6175 function Hash (F : File_Name_Type) return Header_Num is
6176 begin
6177 return Header_Num (1 + F mod Max_Header);
6178 end Hash;
6180 --------------------
6181 -- In_Ada_Lib_Dir --
6182 --------------------
6184 function In_Ada_Lib_Dir (File : File_Name_Type) return Boolean is
6185 D : constant File_Name_Type := Get_Directory (File);
6186 B : constant Byte := Get_Name_Table_Byte (D);
6187 begin
6188 return (B and Ada_Lib_Dir) /= 0;
6189 end In_Ada_Lib_Dir;
6191 -----------------------
6192 -- Init_Mapping_File --
6193 -----------------------
6195 procedure Init_Mapping_File
6196 (Project : Project_Id;
6197 Data : in out Project_Compilation_Data;
6198 File_Index : in out Natural)
6200 FD : File_Descriptor;
6201 Status : Boolean;
6202 -- For call to Close
6204 begin
6205 -- Increase the index of the last mapping file for this project
6207 Data.Last_Mapping_File_Names := Data.Last_Mapping_File_Names + 1;
6209 -- If there is a project file, call Create_Mapping_File with
6210 -- the project id.
6212 if Project /= No_Project then
6213 Prj.Env.Create_Mapping_File
6214 (Project,
6215 In_Tree => Project_Tree,
6216 Language => Name_Ada,
6217 Name => Data.Mapping_File_Names
6218 (Data.Last_Mapping_File_Names));
6220 -- Otherwise, just create an empty file
6222 else
6223 Tempdir.Create_Temp_File
6224 (FD, Data.Mapping_File_Names (Data.Last_Mapping_File_Names));
6226 if FD = Invalid_FD then
6227 Make_Failed ("disk full");
6228 else
6229 Record_Temp_File
6230 (Project_Tree.Shared,
6231 Data.Mapping_File_Names (Data.Last_Mapping_File_Names));
6232 end if;
6234 Close (FD, Status);
6236 if not Status then
6237 Make_Failed ("disk full");
6238 end if;
6239 end if;
6241 -- And return the index of the newly created file
6243 File_Index := Data.Last_Mapping_File_Names;
6244 end Init_Mapping_File;
6246 ----------------
6247 -- Initialize --
6248 ----------------
6250 procedure Initialize
6251 (Project_Node_Tree : out Project_Node_Tree_Ref;
6252 Env : out Prj.Tree.Environment)
6254 procedure Check_Version_And_Help is
6255 new Check_Version_And_Help_G (Makeusg);
6257 -- Start of processing for Initialize
6259 begin
6260 -- Prepare the project's tree, since this is used to hold external
6261 -- references, project path and other attributes that can be impacted by
6262 -- the command line switches
6264 Prj.Tree.Initialize (Env, Gnatmake_Flags);
6266 Project_Node_Tree := new Project_Node_Tree_Data;
6267 Prj.Tree.Initialize (Project_Node_Tree);
6269 -- Override default initialization of Check_Object_Consistency since
6270 -- this is normally False for GNATBIND, but is True for GNATMAKE since
6271 -- we do not need to check source consistency again once GNATMAKE has
6272 -- looked at the sources to check.
6274 Check_Object_Consistency := True;
6276 -- Package initializations (the order of calls is important here)
6278 Output.Set_Standard_Error;
6280 Gcc_Switches.Init;
6281 Binder_Switches.Init;
6282 Linker_Switches.Init;
6284 Csets.Initialize;
6285 Snames.Initialize;
6286 Stringt.Initialize;
6288 Prj.Initialize (Project_Tree);
6290 Dependencies.Init;
6292 RTS_Specified := null;
6293 N_M_Switch := 0;
6295 Mains.Delete;
6297 -- Add the directory where gnatmake is invoked in front of the path,
6298 -- if gnatmake is invoked from a bin directory or with directory
6299 -- information.
6301 declare
6302 Prefix : constant String := Executable_Prefix_Path;
6303 Command : constant String := Command_Name;
6305 begin
6306 if Prefix'Length > 0 then
6307 declare
6308 PATH : constant String :=
6309 Prefix & Directory_Separator & "bin" & Path_Separator
6310 & Getenv ("PATH").all;
6311 begin
6312 Setenv ("PATH", PATH);
6313 end;
6315 else
6316 for Index in reverse Command'Range loop
6317 if Command (Index) = Directory_Separator then
6318 declare
6319 Absolute_Dir : constant String :=
6320 Normalize_Pathname
6321 (Command (Command'First .. Index));
6322 PATH : constant String :=
6323 Absolute_Dir &
6324 Path_Separator &
6325 Getenv ("PATH").all;
6326 begin
6327 Setenv ("PATH", PATH);
6328 end;
6330 exit;
6331 end if;
6332 end loop;
6333 end if;
6334 end;
6336 -- Scan the switches and arguments
6338 -- First, scan to detect --version and/or --help
6340 Check_Version_And_Help ("GNATMAKE", "1995");
6342 -- Scan again the switch and arguments, now that we are sure that they
6343 -- do not include --version or --help.
6345 -- First, check for switch -P and, if found and gprbuild is available,
6346 -- silently invoke gprbuild, with switch --target if not on a native
6347 -- platform.
6349 declare
6350 Arg_Len : Natural := Argument_Count;
6351 Call_Gprbuild : Boolean := False;
6352 Gprbuild : String_Access := null;
6353 Pos : Natural := 0;
6354 Success : Boolean;
6355 Target : String_Access := null;
6357 begin
6358 Find_Program_Name;
6360 if Name_Len >= 8
6361 and then Name_Buffer (Name_Len - 7 .. Name_Len) = "gnatmake"
6362 then
6363 if Name_Len > 8 then
6364 Target := new String'(Name_Buffer (1 .. Name_Len - 9));
6365 Arg_Len := Arg_Len + 1;
6366 end if;
6368 for J in 1 .. Argument_Count loop
6369 declare
6370 Arg : constant String := Argument (J);
6371 begin
6372 if Arg'Length >= 2
6373 and then Arg (Arg'First .. Arg'First + 1) = "-P"
6374 then
6375 Call_Gprbuild := True;
6376 exit;
6377 end if;
6378 end;
6379 end loop;
6381 if Call_Gprbuild then
6382 Gprbuild := Locate_Exec_On_Path (Exec_Name => "gprbuild");
6384 if Gprbuild /= null then
6385 declare
6386 Args : Argument_List (1 .. Arg_Len);
6387 begin
6388 if Target /= null then
6389 Args (1) := new String'("--target=" & Target.all);
6390 Pos := 1;
6391 end if;
6393 for J in 1 .. Argument_Count loop
6394 Pos := Pos + 1;
6395 Args (Pos) := new String'(Argument (J));
6396 end loop;
6398 Spawn (Gprbuild.all, Args, Success);
6400 Free (Gprbuild);
6402 if Success then
6403 Exit_Program (E_Success);
6404 end if;
6405 end;
6406 end if;
6407 end if;
6408 end if;
6409 end;
6411 Scan_Args : for Next_Arg in 1 .. Argument_Count loop
6412 Scan_Make_Arg (Env, Argument (Next_Arg), And_Save => True);
6413 end loop Scan_Args;
6415 if N_M_Switch > 0 and RTS_Specified = null then
6416 Process_Multilib (Env);
6417 end if;
6419 if Commands_To_Stdout then
6420 Set_Standard_Output;
6421 end if;
6423 if Usage_Requested then
6424 Usage;
6425 end if;
6427 -- Add the default project search directories now, after the directories
6428 -- that have been specified by switches -aP<dir>.
6430 Prj.Env.Initialize_Default_Project_Path
6431 (Env.Project_Path, Target_Name => Sdefault.Target_Name.all);
6433 -- Test for trailing -P switch
6435 if Project_File_Name_Present and then Project_File_Name = null then
6436 Make_Failed ("project file name missing after -P");
6438 -- Test for trailing -o switch
6440 elsif Output_File_Name_Present and then not Output_File_Name_Seen then
6441 Make_Failed ("output file name missing after -o");
6443 -- Test for trailing -D switch
6445 elsif Object_Directory_Present and then not Object_Directory_Seen then
6446 Make_Failed ("object directory missing after -D");
6447 end if;
6449 -- Test for simultaneity of -i and -D
6451 if Object_Directory_Path /= null and then In_Place_Mode then
6452 Make_Failed ("-i and -D cannot be used simultaneously");
6453 end if;
6455 -- If --subdirs= is specified, but not -P, this is equivalent to -D,
6456 -- except that the directory is created if it does not exist.
6458 if Prj.Subdirs /= null and then Project_File_Name = null then
6459 if Object_Directory_Path /= null then
6460 Make_Failed ("--subdirs and -D cannot be used simultaneously");
6462 elsif In_Place_Mode then
6463 Make_Failed ("--subdirs and -i cannot be used simultaneously");
6465 else
6466 if not Is_Directory (Prj.Subdirs.all) then
6467 begin
6468 Ada.Directories.Create_Path (Prj.Subdirs.all);
6469 exception
6470 when others =>
6471 Make_Failed ("unable to create object directory " &
6472 Prj.Subdirs.all);
6473 end;
6474 end if;
6476 Object_Directory_Present := True;
6478 declare
6479 Argv : constant String (1 .. Prj.Subdirs'Length) :=
6480 Prj.Subdirs.all;
6481 begin
6482 Scan_Make_Arg (Env, Argv, And_Save => False);
6483 end;
6484 end if;
6485 end if;
6487 -- Deal with -C= switch
6489 if Gnatmake_Mapping_File /= null then
6491 -- First, check compatibility with other switches
6493 if Project_File_Name /= null then
6494 Make_Failed ("-C= switch is not compatible with -P switch");
6496 elsif Saved_Maximum_Processes > 1 then
6497 Make_Failed ("-C= switch is not compatible with -jnnn switch");
6498 end if;
6500 Fmap.Initialize (Gnatmake_Mapping_File.all);
6501 Add_Switch
6502 ("-gnatem=" & Gnatmake_Mapping_File.all,
6503 Compiler,
6504 And_Save => True);
6505 end if;
6507 if Project_File_Name /= null then
6509 -- A project file was specified by a -P switch
6511 if Verbose_Mode then
6512 Write_Eol;
6513 Write_Str ("Parsing project file """);
6514 Write_Str (Project_File_Name.all);
6515 Write_Str (""".");
6516 Write_Eol;
6517 end if;
6519 -- Avoid looking in the current directory for ALI files
6521 -- Look_In_Primary_Dir := False;
6523 -- Set the project parsing verbosity to whatever was specified
6524 -- by a possible -vP switch.
6526 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
6528 -- Parse the project file.
6529 -- If there is an error, Main_Project will still be No_Project.
6531 Prj.Pars.Parse
6532 (Project => Main_Project,
6533 In_Tree => Project_Tree,
6534 Project_File_Name => Project_File_Name.all,
6535 Packages_To_Check => Packages_To_Check_By_Gnatmake,
6536 Env => Env,
6537 In_Node_Tree => Project_Node_Tree);
6539 -- The parsing of project files may have changed the current output
6541 if Commands_To_Stdout then
6542 Set_Standard_Output;
6543 else
6544 Set_Standard_Error;
6545 end if;
6547 if Main_Project = No_Project then
6548 Make_Failed
6549 ("""" & Project_File_Name.all & """ processing failed");
6550 end if;
6552 if Main_Project.Qualifier = Aggregate then
6553 Make_Failed ("aggregate projects are not supported");
6555 elsif Aggregate_Libraries_In (Project_Tree) then
6556 Make_Failed ("aggregate library projects are not supported");
6557 end if;
6559 Create_Mapping_File := True;
6561 if Verbose_Mode then
6562 Write_Eol;
6563 Write_Str ("Parsing of project file """);
6564 Write_Str (Project_File_Name.all);
6565 Write_Str (""" is finished.");
6566 Write_Eol;
6567 end if;
6569 -- We add the source directories and the object directories to the
6570 -- search paths.
6572 -- ??? Why do we need these search directories, we already know the
6573 -- locations from parsing the project, except for the runtime which
6574 -- has its own directories anyway
6576 Add_Source_Directories (Main_Project, Project_Tree);
6577 Add_Object_Directories (Main_Project, Project_Tree);
6579 Recursive_Compute_Depth (Main_Project);
6580 Compute_All_Imported_Projects (Main_Project, Project_Tree);
6582 else
6584 Osint.Add_Default_Search_Dirs;
6586 -- Source file lookups should be cached for efficiency. Source files
6587 -- are not supposed to change. However, we do that now only if no
6588 -- project file is used; if a project file is used, we do it just
6589 -- after changing the directory to the object directory.
6591 Osint.Source_File_Data (Cache => True);
6593 -- Read gnat.adc file to initialize Fname.UF
6595 Fname.UF.Initialize;
6597 if Config_File then
6598 begin
6599 Fname.SF.Read_Source_File_Name_Pragmas;
6601 exception
6602 when Err : SFN_Scan.Syntax_Error_In_GNAT_ADC =>
6603 Make_Failed (Exception_Message (Err));
6604 end;
6605 end if;
6606 end if;
6608 -- Make sure no project object directory is recorded
6610 Project_Of_Current_Object_Directory := No_Project;
6612 if Debug.Debug_Flag_N then
6613 Opt.Keep_Temporary_Files := True;
6614 end if;
6615 end Initialize;
6617 ----------------------------
6618 -- Insert_Project_Sources --
6619 ----------------------------
6621 procedure Insert_Project_Sources
6622 (The_Project : Project_Id;
6623 All_Projects : Boolean;
6624 Into_Q : Boolean)
6626 Put_In_Q : Boolean := Into_Q;
6627 Unit : Unit_Index;
6628 Sfile : File_Name_Type;
6629 Sid : Prj.Source_Id;
6630 Index : Int;
6631 Project : Project_Id;
6633 begin
6634 -- Loop through all the sources in the project files
6636 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
6637 while Unit /= null loop
6638 Sfile := No_File;
6639 Sid := No_Source;
6640 Index := 0;
6641 Project := No_Project;
6643 -- If there is a source for the body, and the body has not been
6644 -- locally removed.
6646 if Unit.File_Names (Impl) /= null
6647 and then not Unit.File_Names (Impl).Locally_Removed
6648 then
6649 -- And it is a source for the specified project
6651 if All_Projects
6652 or else
6653 Is_Extending (The_Project, Unit.File_Names (Impl).Project)
6654 then
6655 Project := Unit.File_Names (Impl).Project;
6657 -- If we don't have a spec, we cannot consider the source
6658 -- if it is a subunit.
6660 if Unit.File_Names (Spec) = null then
6661 declare
6662 Src_Ind : Source_File_Index;
6664 -- Here we are cheating a little bit: we don't want to
6665 -- use Sinput.L, because it depends on the GNAT tree
6666 -- (Atree, Sinfo, ...). So, we pretend that it is a
6667 -- project file, and we use Sinput.P.
6669 -- Source_File_Is_Subunit is just scanning through the
6670 -- file until it finds one of the reserved words
6671 -- separate, procedure, function, generic or package.
6672 -- Fortunately, these Ada reserved words are also
6673 -- reserved for project files.
6675 begin
6676 Src_Ind := Sinput.P.Load_Project_File
6677 (Get_Name_String
6678 (Unit.File_Names (Impl).Path.Display_Name));
6680 -- If it is a subunit, discard it
6682 if Sinput.P.Source_File_Is_Subunit (Src_Ind) then
6683 Sfile := No_File;
6684 Index := 0;
6685 Sid := No_Source;
6686 else
6687 Sfile := Unit.File_Names (Impl).Display_File;
6688 Index := Unit.File_Names (Impl).Index;
6689 Sid := Unit.File_Names (Impl);
6690 end if;
6691 end;
6693 else
6694 Sfile := Unit.File_Names (Impl).Display_File;
6695 Index := Unit.File_Names (Impl).Index;
6696 Sid := Unit.File_Names (Impl);
6697 end if;
6698 end if;
6700 elsif Unit.File_Names (Spec) /= null
6701 and then not Unit.File_Names (Spec).Locally_Removed
6702 and then
6703 (All_Projects
6704 or else
6705 Is_Extending (The_Project, Unit.File_Names (Spec).Project))
6706 then
6707 -- If there is no source for the body, but there is one for the
6708 -- spec which has not been locally removed, then we take this one.
6710 Sfile := Unit.File_Names (Spec).Display_File;
6711 Index := Unit.File_Names (Spec).Index;
6712 Sid := Unit.File_Names (Spec);
6713 Project := Unit.File_Names (Spec).Project;
6714 end if;
6716 -- For the first source inserted into the Q, we need to initialize
6717 -- the Q, but not for the subsequent sources.
6719 Queue.Initialize
6720 (Main_Project /= No_Project and then
6721 One_Compilation_Per_Obj_Dir);
6723 if Sfile /= No_File then
6724 Queue.Insert
6725 ((Format => Format_Gnatmake,
6726 File => Sfile,
6727 Project => Project,
6728 Unit => No_Unit_Name,
6729 Index => Index,
6730 Sid => Sid));
6731 end if;
6733 if not Put_In_Q and then Sfile /= No_File then
6735 -- If Put_In_Q is False, we add the source as if it were specified
6736 -- on the command line, and we set Put_In_Q to True, so that the
6737 -- following sources will only be put in the queue. The source is
6738 -- already in the Q, but we need at least one fake main to call
6739 -- Compile_Sources.
6741 if Verbose_Mode then
6742 Write_Str ("Adding """);
6743 Write_Str (Get_Name_String (Sfile));
6744 Write_Line (""" as if on the command line");
6745 end if;
6747 Osint.Add_File (Get_Name_String (Sfile), Index);
6748 Put_In_Q := True;
6749 end if;
6751 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
6752 end loop;
6753 end Insert_Project_Sources;
6755 ---------------------
6756 -- Is_In_Obsoleted --
6757 ---------------------
6759 function Is_In_Obsoleted (F : File_Name_Type) return Boolean is
6760 begin
6761 if F = No_File then
6762 return False;
6764 else
6765 declare
6766 Name : constant String := Get_Name_String (F);
6767 First : Natural;
6768 F2 : File_Name_Type;
6770 begin
6771 First := Name'Last;
6772 while First > Name'First
6773 and then not Is_Directory_Separator (Name (First - 1))
6774 loop
6775 First := First - 1;
6776 end loop;
6778 if First /= Name'First then
6779 Name_Len := 0;
6780 Add_Str_To_Name_Buffer (Name (First .. Name'Last));
6781 F2 := Name_Find;
6783 else
6784 F2 := F;
6785 end if;
6787 return Obsoleted.Get (F2);
6788 end;
6789 end if;
6790 end Is_In_Obsoleted;
6792 ----------------------------
6793 -- Is_In_Object_Directory --
6794 ----------------------------
6796 function Is_In_Object_Directory
6797 (Source_File : File_Name_Type;
6798 Full_Lib_File : File_Name_Type) return Boolean
6800 begin
6801 -- There is something to check only when using project files. Otherwise,
6802 -- this function returns True (last line of the function).
6804 if Main_Project /= No_Project then
6805 declare
6806 Source_File_Name : constant String :=
6807 Get_Name_String (Source_File);
6808 Saved_Verbosity : constant Verbosity := Current_Verbosity;
6809 Project : Project_Id := No_Project;
6811 Path_Name : Path_Name_Type := No_Path;
6812 pragma Warnings (Off, Path_Name);
6814 begin
6815 -- Call Get_Reference to know the ultimate extending project of
6816 -- the source. Call it with verbosity default to avoid verbose
6817 -- messages.
6819 Current_Verbosity := Default;
6820 Prj.Env.Get_Reference
6821 (Source_File_Name => Source_File_Name,
6822 Project => Project,
6823 In_Tree => Project_Tree,
6824 Path => Path_Name);
6825 Current_Verbosity := Saved_Verbosity;
6827 -- If this source is in a project, check that the ALI file is in
6828 -- its object directory. If it is not, return False, so that the
6829 -- ALI file will not be skipped.
6831 if Project /= No_Project then
6832 declare
6833 Object_Directory : constant String :=
6834 Normalize_Pathname
6835 (Get_Name_String
6836 (Project.
6837 Object_Directory.Display_Name));
6839 Olast : Natural := Object_Directory'Last;
6841 Lib_File_Directory : constant String :=
6842 Normalize_Pathname (Dir_Name
6843 (Get_Name_String (Full_Lib_File)));
6845 Llast : Natural := Lib_File_Directory'Last;
6847 begin
6848 -- For directories, Normalize_Pathname may or may not put
6849 -- a directory separator at the end, depending on its input.
6850 -- Remove any last directory separator before comparison.
6851 -- Returns True only if the two directories are the same.
6853 if Object_Directory (Olast) = Directory_Separator then
6854 Olast := Olast - 1;
6855 end if;
6857 if Lib_File_Directory (Llast) = Directory_Separator then
6858 Llast := Llast - 1;
6859 end if;
6861 return Object_Directory (Object_Directory'First .. Olast) =
6862 Lib_File_Directory (Lib_File_Directory'First .. Llast);
6863 end;
6864 end if;
6865 end;
6866 end if;
6868 -- When the source is not in a project file, always return True
6870 return True;
6871 end Is_In_Object_Directory;
6873 ----------
6874 -- Link --
6875 ----------
6877 procedure Link
6878 (ALI_File : File_Name_Type;
6879 Args : Argument_List;
6880 Success : out Boolean)
6882 Link_Args : Argument_List (1 .. Args'Length + 1);
6884 begin
6885 Get_Name_String (ALI_File);
6886 Link_Args (1) := new String'(Name_Buffer (1 .. Name_Len));
6888 Link_Args (2 .. Args'Length + 1) := Args;
6890 GNAT.OS_Lib.Normalize_Arguments (Link_Args);
6892 Display (Gnatlink.all, Link_Args);
6894 if Gnatlink_Path = null then
6895 Make_Failed ("error, unable to locate " & Gnatlink.all);
6896 end if;
6898 GNAT.OS_Lib.Spawn (Gnatlink_Path.all, Link_Args, Success);
6899 end Link;
6901 ---------------------------
6902 -- List_Bad_Compilations --
6903 ---------------------------
6905 procedure List_Bad_Compilations is
6906 begin
6907 if not No_Exit_Message then
6908 for J in Bad_Compilation.First .. Bad_Compilation.Last loop
6909 if Bad_Compilation.Table (J).File = No_File then
6910 null;
6911 elsif not Bad_Compilation.Table (J).Found then
6912 Inform (Bad_Compilation.Table (J).File, "not found");
6913 else
6914 Inform (Bad_Compilation.Table (J).File, "compilation error");
6915 end if;
6916 end loop;
6917 end if;
6918 end List_Bad_Compilations;
6920 -----------------
6921 -- List_Depend --
6922 -----------------
6924 procedure List_Depend is
6925 Lib_Name : File_Name_Type;
6926 Obj_Name : File_Name_Type;
6927 Src_Name : File_Name_Type;
6929 Len : Natural;
6930 Line_Pos : Natural;
6931 Line_Size : constant := 77;
6933 begin
6934 Set_Standard_Output;
6936 for A in ALIs.First .. ALIs.Last loop
6937 Lib_Name := ALIs.Table (A).Afile;
6939 -- We have to provide the full library file name in In_Place_Mode
6941 if In_Place_Mode then
6942 Lib_Name := Full_Lib_File_Name (Lib_Name);
6943 end if;
6945 Obj_Name := Object_File_Name (Lib_Name);
6946 Write_Name (Obj_Name);
6947 Write_Str (" :");
6949 Get_Name_String (Obj_Name);
6950 Len := Name_Len;
6951 Line_Pos := Len + 2;
6953 for D in ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep loop
6954 Src_Name := Sdep.Table (D).Sfile;
6956 if Is_Internal_File_Name (Src_Name)
6957 and then not Check_Readonly_Files
6958 then
6959 null;
6960 else
6961 if not Quiet_Output then
6962 Src_Name := Full_Source_Name (Src_Name);
6963 end if;
6965 Get_Name_String (Src_Name);
6966 Len := Name_Len;
6968 if Line_Pos + Len + 1 > Line_Size then
6969 Write_Str (" \");
6970 Write_Eol;
6971 Line_Pos := 0;
6972 end if;
6974 Line_Pos := Line_Pos + Len + 1;
6976 Write_Str (" ");
6977 Write_Name (Src_Name);
6978 end if;
6979 end loop;
6981 Write_Eol;
6982 end loop;
6984 if not Commands_To_Stdout then
6985 Set_Standard_Error;
6986 end if;
6987 end List_Depend;
6989 -----------------
6990 -- Make_Failed --
6991 -----------------
6993 procedure Make_Failed (S : String) is
6994 begin
6995 Fail_Program (Project_Tree, S);
6996 end Make_Failed;
6998 --------------------
6999 -- Mark_Directory --
7000 --------------------
7002 procedure Mark_Directory
7003 (Dir : String;
7004 Mark : Lib_Mark_Type;
7005 On_Command_Line : Boolean)
7007 N : Name_Id;
7008 B : Byte;
7010 function Base_Directory return String;
7011 -- If Dir comes from the command line, empty string (relative paths are
7012 -- resolved with respect to the current directory), else return the main
7013 -- project's directory.
7015 --------------------
7016 -- Base_Directory --
7017 --------------------
7019 function Base_Directory return String is
7020 begin
7021 if On_Command_Line then
7022 return "";
7023 else
7024 return Get_Name_String (Main_Project.Directory.Display_Name);
7025 end if;
7026 end Base_Directory;
7028 Real_Path : constant String := Normalize_Pathname (Dir, Base_Directory);
7030 -- Start of processing for Mark_Directory
7032 begin
7033 Name_Len := 0;
7035 if Real_Path'Length = 0 then
7036 Add_Str_To_Name_Buffer (Dir);
7038 else
7039 Add_Str_To_Name_Buffer (Real_Path);
7040 end if;
7042 -- Last character is supposed to be a directory separator
7044 if not Is_Directory_Separator (Name_Buffer (Name_Len)) then
7045 Add_Char_To_Name_Buffer (Directory_Separator);
7046 end if;
7048 -- Add flags to the already existing flags
7050 N := Name_Find;
7051 B := Get_Name_Table_Byte (N);
7052 Set_Name_Table_Byte (N, B or Mark);
7053 end Mark_Directory;
7055 ----------------------
7056 -- Process_Multilib --
7057 ----------------------
7059 procedure Process_Multilib (Env : in out Prj.Tree.Environment) is
7060 Output_FD : File_Descriptor;
7061 Output_Name : String_Access;
7062 Arg_Index : Natural := 0;
7063 Success : Boolean := False;
7064 Return_Code : Integer := 0;
7065 Multilib_Gcc_Path : String_Access;
7066 Multilib_Gcc : String_Access;
7067 N_Read : Integer := 0;
7068 Line : String (1 .. 1000);
7069 Args : Argument_List (1 .. N_M_Switch + 1);
7071 begin
7072 pragma Assert (N_M_Switch > 0 and RTS_Specified = null);
7074 -- In case we detected a multilib switch and the user has not
7075 -- manually specified a specific RTS we emulate the following command:
7076 -- gnatmake $FLAGS --RTS=$(gcc -print-multi-directory $FLAGS)
7078 -- First select the flags which might have an impact on multilib
7079 -- processing. Note that this is an heuristic selection and it
7080 -- will need to be maintained over time. The condition has to
7081 -- be kept synchronized with N_M_Switch counting in Scan_Make_Arg.
7083 for Next_Arg in 1 .. Argument_Count loop
7084 declare
7085 Argv : constant String := Argument (Next_Arg);
7087 begin
7088 if Argv'Length > 2
7089 and then Argv (1) = '-'
7090 and then Argv (2) = 'm'
7091 and then Argv /= "-margs"
7093 -- Ignore -mieee to avoid spawning an extra gcc in this case
7095 and then Argv /= "-mieee"
7096 then
7097 Arg_Index := Arg_Index + 1;
7098 Args (Arg_Index) := new String'(Argv);
7099 end if;
7100 end;
7101 end loop;
7103 pragma Assert (Arg_Index = N_M_Switch);
7105 Args (Args'Last) := new String'("-print-multi-directory");
7107 -- Call the GCC driver with the collected flags and save its
7108 -- output. Alternate design would be to link in gnatmake the
7109 -- relevant part of the GCC driver.
7111 if Saved_Gcc /= null then
7112 Multilib_Gcc := Saved_Gcc;
7113 else
7114 Multilib_Gcc := Gcc;
7115 end if;
7117 Multilib_Gcc_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Multilib_Gcc.all);
7119 Create_Temp_Output_File (Output_FD, Output_Name);
7121 if Output_FD = Invalid_FD then
7122 return;
7123 end if;
7125 GNAT.OS_Lib.Spawn
7126 (Multilib_Gcc_Path.all, Args, Output_FD, Return_Code, False);
7127 Close (Output_FD);
7129 if Return_Code /= 0 then
7130 return;
7131 end if;
7133 -- Parse the GCC driver output which is a single line, removing CR/LF
7135 Output_FD := Open_Read (Output_Name.all, Binary);
7137 if Output_FD = Invalid_FD then
7138 return;
7139 end if;
7141 N_Read := Read (Output_FD, Line (1)'Address, Line'Length);
7142 Close (Output_FD);
7143 Delete_File (Output_Name.all, Success);
7145 for J in reverse 1 .. N_Read loop
7146 if Line (J) = ASCII.CR or else Line (J) = ASCII.LF then
7147 N_Read := N_Read - 1;
7148 else
7149 exit;
7150 end if;
7151 end loop;
7153 -- In case the standard RTS is selected do nothing
7155 if N_Read = 0 or else Line (1 .. N_Read) = "." then
7156 return;
7157 end if;
7159 -- Otherwise add -margs --RTS=output
7161 Scan_Make_Arg (Env, "-margs", And_Save => True);
7162 Scan_Make_Arg (Env, "--RTS=" & Line (1 .. N_Read), And_Save => True);
7163 end Process_Multilib;
7165 -----------------------------
7166 -- Recursive_Compute_Depth --
7167 -----------------------------
7169 procedure Recursive_Compute_Depth (Project : Project_Id) is
7170 use Project_Boolean_Htable;
7171 Seen : Project_Boolean_Htable.Instance := Project_Boolean_Htable.Nil;
7173 procedure Recurse (Prj : Project_Id; Depth : Natural);
7174 -- Recursive procedure that does the work, keeping track of the depth
7176 -------------
7177 -- Recurse --
7178 -------------
7180 procedure Recurse (Prj : Project_Id; Depth : Natural) is
7181 List : Project_List;
7182 Proj : Project_Id;
7184 begin
7185 if Prj.Depth >= Depth or else Get (Seen, Prj) then
7186 return;
7187 end if;
7189 -- We need a test to avoid infinite recursions with limited withs:
7190 -- If we have A -> B -> A, then when set level of A to n, we try and
7191 -- set level of B to n+1, and then level of A to n + 2, ...
7193 Set (Seen, Prj, True);
7195 Prj.Depth := Depth;
7197 -- Visit each imported project
7199 List := Prj.Imported_Projects;
7200 while List /= null loop
7201 Proj := List.Project;
7202 List := List.Next;
7203 Recurse (Prj => Proj, Depth => Depth + 1);
7204 end loop;
7206 -- We again allow changing the depth of this project later on if it
7207 -- is in fact imported by a lower-level project.
7209 Set (Seen, Prj, False);
7210 end Recurse;
7212 Proj : Project_List;
7214 -- Start of processing for Recursive_Compute_Depth
7216 begin
7217 Proj := Project_Tree.Projects;
7218 while Proj /= null loop
7219 Proj.Project.Depth := 0;
7220 Proj := Proj.Next;
7221 end loop;
7223 Recurse (Project, Depth => 1);
7224 Reset (Seen);
7225 end Recursive_Compute_Depth;
7227 -------------------------------
7228 -- Report_Compilation_Failed --
7229 -------------------------------
7231 procedure Report_Compilation_Failed is
7232 begin
7233 Fail_Program (Project_Tree, "");
7234 end Report_Compilation_Failed;
7236 ------------------------
7237 -- Sigint_Intercepted --
7238 ------------------------
7240 procedure Sigint_Intercepted is
7241 begin
7242 Set_Standard_Error;
7243 Write_Line ("*** Interrupted ***");
7245 -- Send SIGINT to all outstanding compilation processes spawned
7247 for J in 1 .. Outstanding_Compiles loop
7248 Kill (Running_Compile (J).Pid, Hard_Kill => False);
7249 end loop;
7251 Finish_Program (Project_Tree, E_No_Compile);
7252 end Sigint_Intercepted;
7254 -------------------
7255 -- Scan_Make_Arg --
7256 -------------------
7258 procedure Scan_Make_Arg
7259 (Env : in out Prj.Tree.Environment;
7260 Argv : String;
7261 And_Save : Boolean)
7263 Success : Boolean;
7265 begin
7266 Gnatmake_Switch_Found := True;
7268 pragma Assert (Argv'First = 1);
7270 if Argv'Length = 0 then
7271 return;
7272 end if;
7274 -- If the previous switch has set the Project_File_Name_Present flag
7275 -- (that is we have seen a -P alone), then the next argument is the name
7276 -- of the project file.
7278 if Project_File_Name_Present and then Project_File_Name = null then
7279 if Argv (1) = '-' then
7280 Make_Failed ("project file name missing after -P");
7282 else
7283 Project_File_Name_Present := False;
7284 Project_File_Name := new String'(Argv);
7285 end if;
7287 -- If the previous switch has set the Output_File_Name_Present flag
7288 -- (that is we have seen a -o), then the next argument is the name of
7289 -- the output executable.
7291 elsif Output_File_Name_Present
7292 and then not Output_File_Name_Seen
7293 then
7294 Output_File_Name_Seen := True;
7296 if Argv (1) = '-' then
7297 Make_Failed ("output file name missing after -o");
7299 else
7300 Add_Switch ("-o", Linker, And_Save => And_Save);
7301 Add_Switch (Executable_Name (Argv), Linker, And_Save => And_Save);
7302 end if;
7304 -- If the previous switch has set the Object_Directory_Present flag
7305 -- (that is we have seen a -D), then the next argument is the path name
7306 -- of the object directory.
7308 elsif Object_Directory_Present
7309 and then not Object_Directory_Seen
7310 then
7311 Object_Directory_Seen := True;
7313 if Argv (1) = '-' then
7314 Make_Failed ("object directory path name missing after -D");
7316 elsif not Is_Directory (Argv) then
7317 Make_Failed ("cannot find object directory """ & Argv & """");
7319 else
7320 -- Record the object directory. Make sure it ends with a directory
7321 -- separator.
7323 declare
7324 Norm : constant String := Normalize_Pathname (Argv);
7326 begin
7327 if Norm (Norm'Last) = Directory_Separator then
7328 Object_Directory_Path := new String'(Norm);
7329 else
7330 Object_Directory_Path :=
7331 new String'(Norm & Directory_Separator);
7332 end if;
7334 Add_Lib_Search_Dir (Norm);
7336 -- Specify the object directory to the binder
7338 Add_Switch ("-aO" & Norm, Binder, And_Save => And_Save);
7339 end;
7341 end if;
7343 -- Then check if we are dealing with -cargs/-bargs/-largs/-margs. These
7344 -- options are taken as is when found in package Compiler, Binder or
7345 -- Linker of the main project file.
7347 elsif (And_Save or else Program_Args = None)
7348 and then (Argv = "-bargs" or else
7349 Argv = "-cargs" or else
7350 Argv = "-largs" or else
7351 Argv = "-margs")
7352 then
7353 case Argv (2) is
7354 when 'c' => Program_Args := Compiler;
7355 when 'b' => Program_Args := Binder;
7356 when 'l' => Program_Args := Linker;
7357 when 'm' => Program_Args := None;
7359 when others =>
7360 raise Program_Error;
7361 end case;
7363 -- A special test is needed for the -o switch within a -largs since that
7364 -- is another way to specify the name of the final executable.
7366 elsif Program_Args = Linker and then Argv = "-o" then
7367 Make_Failed
7368 ("switch -o not allowed within a -largs. Use -o directly.");
7370 -- Check to see if we are reading switches after a -cargs, -bargs or
7371 -- -largs switch. If so, save it.
7373 elsif Program_Args /= None then
7375 -- Check to see if we are reading -I switches in order to take into
7376 -- account in the src & lib search directories.
7378 if Argv'Length > 2 and then Argv (1 .. 2) = "-I" then
7379 if Argv (3 .. Argv'Last) = "-" then
7380 Look_In_Primary_Dir := False;
7382 elsif Program_Args = Compiler then
7383 if Argv (3 .. Argv'Last) /= "-" then
7384 Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7385 end if;
7387 elsif Program_Args = Binder then
7388 Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7389 end if;
7390 end if;
7392 Add_Switch (Argv, Program_Args, And_Save => And_Save);
7394 -- Make sure that all significant switches -m on the command line
7395 -- are counted.
7397 if Argv'Length > 2
7398 and then Argv (1 .. 2) = "-m"
7399 and then Argv /= "-mieee"
7400 then
7401 N_M_Switch := N_M_Switch + 1;
7402 end if;
7404 -- Handle non-default compiler, binder, linker, and handle --RTS switch
7406 elsif Argv'Length > 2 and then Argv (1 .. 2) = "--" then
7407 if Argv'Length > 6
7408 and then Argv (1 .. 6) = "--GCC="
7409 then
7410 declare
7411 Program_Args : constant Argument_List_Access :=
7412 Argument_String_To_List
7413 (Argv (7 .. Argv'Last));
7415 begin
7416 if And_Save then
7417 Saved_Gcc := new String'(Program_Args.all (1).all);
7418 else
7419 Gcc := new String'(Program_Args.all (1).all);
7420 end if;
7422 for J in 2 .. Program_Args.all'Last loop
7423 Add_Switch
7424 (Program_Args.all (J).all, Compiler, And_Save => And_Save);
7425 end loop;
7426 end;
7428 elsif Argv'Length > 11
7429 and then Argv (1 .. 11) = "--GNATBIND="
7430 then
7431 declare
7432 Program_Args : constant Argument_List_Access :=
7433 Argument_String_To_List
7434 (Argv (12 .. Argv'Last));
7436 begin
7437 if And_Save then
7438 Saved_Gnatbind := new String'(Program_Args.all (1).all);
7439 else
7440 Gnatbind := new String'(Program_Args.all (1).all);
7441 end if;
7443 for J in 2 .. Program_Args.all'Last loop
7444 Add_Switch
7445 (Program_Args.all (J).all, Binder, And_Save => And_Save);
7446 end loop;
7447 end;
7449 elsif Argv'Length > 11
7450 and then Argv (1 .. 11) = "--GNATLINK="
7451 then
7452 declare
7453 Program_Args : constant Argument_List_Access :=
7454 Argument_String_To_List
7455 (Argv (12 .. Argv'Last));
7456 begin
7457 if And_Save then
7458 Saved_Gnatlink := new String'(Program_Args.all (1).all);
7459 else
7460 Gnatlink := new String'(Program_Args.all (1).all);
7461 end if;
7463 for J in 2 .. Program_Args.all'Last loop
7464 Add_Switch (Program_Args.all (J).all, Linker);
7465 end loop;
7466 end;
7468 elsif Argv'Length >= 5 and then
7469 Argv (1 .. 5) = "--RTS"
7470 then
7471 Add_Switch (Argv, Compiler, And_Save => And_Save);
7472 Add_Switch (Argv, Binder, And_Save => And_Save);
7474 if Argv'Length <= 6 or else Argv (6) /= '=' then
7475 Make_Failed ("missing path for --RTS");
7477 else
7478 -- Check that this is the first time we see this switch or
7479 -- if it is not the first time, the same path is specified.
7481 if RTS_Specified = null then
7482 RTS_Specified := new String'(Argv (7 .. Argv'Last));
7484 elsif RTS_Specified.all /= Argv (7 .. Argv'Last) then
7485 Make_Failed ("--RTS cannot be specified multiple times");
7486 end if;
7488 -- Valid --RTS switch
7490 No_Stdinc := True;
7491 No_Stdlib := True;
7492 RTS_Switch := True;
7494 declare
7495 Src_Path_Name : constant String_Ptr :=
7496 Get_RTS_Search_Dir
7497 (Argv (7 .. Argv'Last), Include);
7499 Lib_Path_Name : constant String_Ptr :=
7500 Get_RTS_Search_Dir
7501 (Argv (7 .. Argv'Last), Objects);
7503 begin
7504 if Src_Path_Name /= null
7505 and then Lib_Path_Name /= null
7506 then
7507 -- Set RTS_*_Path_Name variables, so that correct direct-
7508 -- ories will be set when Osint.Add_Default_Search_Dirs
7509 -- is called later.
7511 RTS_Src_Path_Name := Src_Path_Name;
7512 RTS_Lib_Path_Name := Lib_Path_Name;
7514 elsif Src_Path_Name = null
7515 and then Lib_Path_Name = null
7516 then
7517 Make_Failed
7518 ("RTS path not valid: missing adainclude and adalib "
7519 & "directories");
7521 elsif Src_Path_Name = null then
7522 Make_Failed
7523 ("RTS path not valid: missing adainclude directory");
7525 elsif Lib_Path_Name = null then
7526 Make_Failed
7527 ("RTS path not valid: missing adalib directory");
7528 end if;
7529 end;
7530 end if;
7532 elsif Argv'Length > Source_Info_Option'Length
7533 and then Argv (1 .. Source_Info_Option'Length) = Source_Info_Option
7534 then
7535 Project_Tree.Source_Info_File_Name :=
7536 new String'(Argv (Source_Info_Option'Length + 1 .. Argv'Last));
7538 elsif Argv'Length >= 8 and then Argv (1 .. 8) = "--param=" then
7539 Add_Switch (Argv, Compiler, And_Save => And_Save);
7540 Add_Switch (Argv, Linker, And_Save => And_Save);
7542 elsif Argv = Create_Map_File_Switch then
7543 Map_File := new String'("");
7545 elsif Argv'Length > Create_Map_File_Switch'Length + 1
7546 and then
7547 Argv (1 .. Create_Map_File_Switch'Length) = Create_Map_File_Switch
7548 and then
7549 Argv (Create_Map_File_Switch'Length + 1) = '='
7550 then
7551 Map_File :=
7552 new String'
7553 (Argv (Create_Map_File_Switch'Length + 2 .. Argv'Last));
7555 else
7556 Scan_Make_Switches (Env, Argv, Success);
7557 end if;
7559 -- If we have seen a regular switch process it
7561 elsif Argv (1) = '-' then
7562 if Argv'Length = 1 then
7563 Make_Failed ("switch character cannot be followed by a blank");
7565 -- Incorrect switches that should start with "--"
7567 elsif (Argv'Length > 5 and then Argv (1 .. 5) = "-RTS=")
7568 or else (Argv'Length > 5 and then Argv (1 .. 5) = "-GCC=")
7569 or else (Argv'Length > 8 and then Argv (1 .. 7) = "-param=")
7570 or else (Argv'Length > 10 and then Argv (1 .. 10) = "-GNATLINK=")
7571 or else (Argv'Length > 10 and then Argv (1 .. 10) = "-GNATBIND=")
7572 then
7573 Make_Failed ("option " & Argv & " should start with '--'");
7575 -- -I-
7577 elsif Argv (2 .. Argv'Last) = "I-" then
7578 Look_In_Primary_Dir := False;
7580 -- Forbid -?- or -??- where ? is any character
7582 elsif (Argv'Length = 3 and then Argv (3) = '-')
7583 or else (Argv'Length = 4 and then Argv (4) = '-')
7584 then
7585 Make_Failed
7586 ("trailing ""-"" at the end of " & Argv & " forbidden.");
7588 -- -Idir
7590 elsif Argv (2) = 'I' then
7591 Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7592 Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7593 Add_Switch (Argv, Compiler, And_Save => And_Save);
7594 Add_Switch (Argv, Binder, And_Save => And_Save);
7596 -- -aIdir (to gcc this is like a -I switch)
7598 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aI" then
7599 Add_Source_Search_Dir (Argv (4 .. Argv'Last), And_Save);
7600 Add_Switch
7601 ("-I" & Argv (4 .. Argv'Last), Compiler, And_Save => And_Save);
7602 Add_Switch (Argv, Binder, And_Save => And_Save);
7604 -- -aOdir
7606 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aO" then
7607 Add_Library_Search_Dir (Argv (4 .. Argv'Last), And_Save);
7608 Add_Switch (Argv, Binder, And_Save => And_Save);
7610 -- -aLdir (to gnatbind this is like a -aO switch)
7612 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aL" then
7613 Mark_Directory (Argv (4 .. Argv'Last), Ada_Lib_Dir, And_Save);
7614 Add_Library_Search_Dir (Argv (4 .. Argv'Last), And_Save);
7615 Add_Switch
7616 ("-aO" & Argv (4 .. Argv'Last), Binder, And_Save => And_Save);
7618 -- -aamp_target=...
7620 elsif Argv'Length >= 13 and then Argv (2 .. 13) = "aamp_target=" then
7621 Add_Switch (Argv, Compiler, And_Save => And_Save);
7623 -- Set the aamp_target environment variable so that the binder and
7624 -- linker will use the proper target library. This is consistent
7625 -- with how things work when -aamp_target is passed on the command
7626 -- line to gnaampmake.
7628 Setenv ("aamp_target", Argv (14 .. Argv'Last));
7630 -- -Adir (to gnatbind this is like a -aO switch, to gcc like a -I)
7632 elsif Argv (2) = 'A' then
7633 Mark_Directory (Argv (3 .. Argv'Last), Ada_Lib_Dir, And_Save);
7634 Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7635 Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7636 Add_Switch
7637 ("-I" & Argv (3 .. Argv'Last), Compiler, And_Save => And_Save);
7638 Add_Switch
7639 ("-aO" & Argv (3 .. Argv'Last), Binder, And_Save => And_Save);
7641 -- -Ldir
7643 elsif Argv (2) = 'L' then
7644 Add_Switch (Argv, Linker, And_Save => And_Save);
7646 -- For -gxxx, -pg, -mxxx, -fxxx, -Oxxx, pass the switch to both the
7647 -- compiler and the linker (except for -gnatxxx which is only for the
7648 -- compiler). Some of the -mxxx (for example -m64) and -fxxx (for
7649 -- example -ftest-coverage for gcov) need to be used when compiling
7650 -- the binder generated files, and using all these gcc switches for
7651 -- them should not be a problem. Pass -Oxxx to the linker for LTO.
7653 elsif
7654 (Argv (2) = 'g' and then (Argv'Last < 5
7655 or else Argv (2 .. 5) /= "gnat"))
7656 or else Argv (2 .. Argv'Last) = "pg"
7657 or else (Argv (2) = 'm' and then Argv'Last > 2)
7658 or else (Argv (2) = 'f' and then Argv'Last > 2)
7659 or else Argv (2) = 'O'
7660 then
7661 Add_Switch (Argv, Compiler, And_Save => And_Save);
7662 Add_Switch (Argv, Linker, And_Save => And_Save);
7664 -- The following condition has to be kept synchronized with
7665 -- the Process_Multilib one.
7667 if Argv (2) = 'm'
7668 and then Argv /= "-mieee"
7669 then
7670 N_M_Switch := N_M_Switch + 1;
7671 end if;
7673 -- -C=<mapping file>
7675 elsif Argv'Last > 2 and then Argv (2) = 'C' then
7676 if And_Save then
7677 if Argv (3) /= '=' or else Argv'Last <= 3 then
7678 Make_Failed ("illegal switch " & Argv);
7679 end if;
7681 Gnatmake_Mapping_File := new String'(Argv (4 .. Argv'Last));
7682 end if;
7684 -- -D
7686 elsif Argv'Last = 2 and then Argv (2) = 'D' then
7687 if Project_File_Name /= null then
7688 Make_Failed
7689 ("-D cannot be used in conjunction with a project file");
7691 else
7692 Scan_Make_Switches (Env, Argv, Success);
7693 end if;
7695 -- -d
7697 elsif Argv (2) = 'd' and then Argv'Last = 2 then
7698 Display_Compilation_Progress := True;
7700 -- -i
7702 elsif Argv'Last = 2 and then Argv (2) = 'i' then
7703 if Project_File_Name /= null then
7704 Make_Failed
7705 ("-i cannot be used in conjunction with a project file");
7706 else
7707 Scan_Make_Switches (Env, Argv, Success);
7708 end if;
7710 -- -j (need to save the result)
7712 elsif Argv (2) = 'j' then
7713 Scan_Make_Switches (Env, Argv, Success);
7715 if And_Save then
7716 Saved_Maximum_Processes := Maximum_Processes;
7717 end if;
7719 -- -m
7721 elsif Argv (2) = 'm' and then Argv'Last = 2 then
7722 Minimal_Recompilation := True;
7724 -- -u
7726 elsif Argv (2) = 'u' and then Argv'Last = 2 then
7727 Unique_Compile := True;
7728 Compile_Only := True;
7729 Do_Bind_Step := False;
7730 Do_Link_Step := False;
7732 -- -U
7734 elsif Argv (2) = 'U'
7735 and then Argv'Last = 2
7736 then
7737 Unique_Compile_All_Projects := True;
7738 Unique_Compile := True;
7739 Compile_Only := True;
7740 Do_Bind_Step := False;
7741 Do_Link_Step := False;
7743 -- -Pprj or -P prj (only once, and only on the command line)
7745 elsif Argv (2) = 'P' then
7746 if Project_File_Name /= null then
7747 Make_Failed ("cannot have several project files specified");
7749 elsif Object_Directory_Path /= null then
7750 Make_Failed
7751 ("-D cannot be used in conjunction with a project file");
7753 elsif In_Place_Mode then
7754 Make_Failed
7755 ("-i cannot be used in conjunction with a project file");
7757 elsif not And_Save then
7759 -- It could be a tool other than gnatmake (e.g. gnatdist)
7760 -- or a -P switch inside a project file.
7762 Fail
7763 ("either the tool is not ""project-aware"" or "
7764 & "a project file is specified inside a project file");
7766 elsif Argv'Last = 2 then
7768 -- -P is used alone: the project file name is the next option
7770 Project_File_Name_Present := True;
7772 else
7773 Project_File_Name := new String'(Argv (3 .. Argv'Last));
7774 end if;
7776 -- -vPx (verbosity of the parsing of the project files)
7778 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "vP" then
7779 if Argv'Last /= 4 or else Argv (4) not in '0' .. '2' then
7780 Make_Failed
7781 ("invalid verbosity level " & Argv (4 .. Argv'Last));
7783 elsif And_Save then
7784 case Argv (4) is
7785 when '0' =>
7786 Current_Verbosity := Prj.Default;
7787 when '1' =>
7788 Current_Verbosity := Prj.Medium;
7789 when '2' =>
7790 Current_Verbosity := Prj.High;
7791 when others =>
7792 null;
7793 end case;
7794 end if;
7796 -- -Xext=val (External assignment)
7798 elsif Argv (2) = 'X'
7799 and then Is_External_Assignment (Env, Argv)
7800 then
7801 -- Is_External_Assignment has side effects when it returns True
7803 null;
7805 -- If -gnath is present, then generate the usage information right
7806 -- now and do not pass this option on to the compiler calls.
7808 elsif Argv = "-gnath" then
7809 Usage;
7811 -- If -gnatc is specified, make sure the bind and link steps are not
7812 -- executed.
7814 elsif Argv'Length >= 6 and then Argv (2 .. 6) = "gnatc" then
7816 -- If -gnatc is specified, make sure the bind and link steps are
7817 -- not executed.
7819 Add_Switch (Argv, Compiler, And_Save => And_Save);
7820 Operating_Mode := Check_Semantics;
7821 Check_Object_Consistency := False;
7823 -- Except in CodePeer mode (set by -gnatcC), where we do want to
7824 -- call bind/link in CodePeer mode (-P switch).
7826 if Argv'Last >= 7 and then Argv (7) = 'C' then
7827 CodePeer_Mode := True;
7828 else
7829 Compile_Only := True;
7830 Do_Bind_Step := False;
7831 Do_Link_Step := False;
7832 end if;
7834 -- If -gnatA is specified, make sure that gnat.adc is never read
7836 elsif Argv'Length >= 6 and then Argv (2 .. 6) = "gnatA" then
7837 Add_Switch (Argv, Compiler, And_Save => And_Save);
7838 Opt.Config_File := False;
7840 elsif Argv (2 .. Argv'Last) = "nostdlib" then
7842 -- Pass -nstdlib to gnatbind and gnatlink
7844 No_Stdlib := True;
7845 Add_Switch (Argv, Binder, And_Save => And_Save);
7846 Add_Switch (Argv, Linker, And_Save => And_Save);
7848 elsif Argv (2 .. Argv'Last) = "nostdinc" then
7850 -- Pass -nostdinc to the Compiler and to gnatbind
7852 No_Stdinc := True;
7853 Add_Switch (Argv, Compiler, And_Save => And_Save);
7854 Add_Switch (Argv, Binder, And_Save => And_Save);
7856 -- All other switches are processed by Scan_Make_Switches. If the
7857 -- call returns with Gnatmake_Switch_Found = False, then the switch
7858 -- is passed to the compiler.
7860 else
7861 Scan_Make_Switches (Env, Argv, Gnatmake_Switch_Found);
7863 if not Gnatmake_Switch_Found then
7864 Add_Switch (Argv, Compiler, And_Save => And_Save);
7865 end if;
7866 end if;
7868 -- If not a switch it must be a file name
7870 else
7871 if And_Save then
7872 Main_On_Command_Line := True;
7873 end if;
7875 Add_File (Argv);
7876 Mains.Add_Main (Argv);
7877 end if;
7878 end Scan_Make_Arg;
7880 -----------------
7881 -- Switches_Of --
7882 -----------------
7884 function Switches_Of
7885 (Source_File : File_Name_Type;
7886 Project : Project_Id;
7887 In_Package : Package_Id;
7888 Allow_ALI : Boolean) return Variable_Value
7890 Switches : Variable_Value;
7891 Is_Default : Boolean;
7893 begin
7894 Makeutl.Get_Switches
7895 (Source_File => Source_File,
7896 Source_Lang => Name_Ada,
7897 Source_Prj => Project,
7898 Pkg_Name => Project_Tree.Shared.Packages.Table (In_Package).Name,
7899 Project_Tree => Project_Tree,
7900 Value => Switches,
7901 Is_Default => Is_Default,
7902 Test_Without_Suffix => True,
7903 Check_ALI_Suffix => Allow_ALI);
7904 return Switches;
7905 end Switches_Of;
7907 -----------
7908 -- Usage --
7909 -----------
7911 procedure Usage is
7912 begin
7913 if Usage_Needed then
7914 Usage_Needed := False;
7915 Makeusg;
7916 end if;
7917 end Usage;
7919 begin
7920 -- Make sure that in case of failure, the temp files will be deleted
7922 Prj.Com.Fail := Make_Failed'Access;
7923 MLib.Fail := Make_Failed'Access;
7924 end Make;