8251052258b646615e34ad86a76cc24bb2e8ce3e
[gcc.git] / gcc / ada / make.adb
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- M A K E --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2010, 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 ------------------------------------------------------------------------------
25
26 with ALI; use ALI;
27 with ALI.Util; use ALI.Util;
28 with Csets;
29 with Debug;
30 with Errutil;
31 with Fmap;
32 with Fname; use Fname;
33 with Fname.SF; use Fname.SF;
34 with Fname.UF; use Fname.UF;
35 with Gnatvsn; use Gnatvsn;
36 with Hostparm; use Hostparm;
37 with Makeusg;
38 with Makeutl; use Makeutl;
39 with MLib;
40 with MLib.Prj;
41 with MLib.Tgt; use MLib.Tgt;
42 with MLib.Utl;
43 with Namet; use Namet;
44 with Opt; use Opt;
45 with Osint.M; use Osint.M;
46 with Osint; use Osint;
47 with Output; use Output;
48 with Prj; use Prj;
49 with Prj.Com;
50 with Prj.Env;
51 with Prj.Pars;
52 with Prj.Tree; use Prj.Tree;
53 with Prj.Util;
54 with SFN_Scan;
55 with Sinput.P;
56 with Snames; use Snames;
57
58 pragma Warnings (Off);
59 with System.HTable;
60 pragma Warnings (On);
61
62 with Switch; use Switch;
63 with Switch.M; use Switch.M;
64 with Targparm; use Targparm;
65 with Table;
66 with Tempdir;
67 with Types; use Types;
68
69 with Ada.Exceptions; use Ada.Exceptions;
70 with Ada.Command_Line; use Ada.Command_Line;
71
72 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
73 with GNAT.Dynamic_HTables; use GNAT.Dynamic_HTables;
74 with GNAT.Case_Util; use GNAT.Case_Util;
75 with GNAT.OS_Lib; use GNAT.OS_Lib;
76
77 package body Make is
78
79 use ASCII;
80 -- Make control characters visible
81
82 Standard_Library_Package_Body_Name : constant String := "s-stalib.adb";
83 -- Every program depends on this package, that must then be checked,
84 -- especially when -f and -a are used.
85
86 procedure Kill (Pid : Process_Id; Sig_Num : Integer; Close : Integer);
87 pragma Import (C, Kill, "__gnat_kill");
88 -- Called by Sigint_Intercepted to kill all spawned compilation processes
89
90 type Sigint_Handler is access procedure;
91 pragma Convention (C, Sigint_Handler);
92
93 procedure Install_Int_Handler (Handler : Sigint_Handler);
94 pragma Import (C, Install_Int_Handler, "__gnat_install_int_handler");
95 -- Called by Gnatmake to install the SIGINT handler below
96
97 procedure Sigint_Intercepted;
98 pragma Convention (C, Sigint_Intercepted);
99 -- Called when the program is interrupted by Ctrl-C to delete the
100 -- temporary mapping files and configuration pragmas files.
101
102 No_Mapping_File : constant Natural := 0;
103
104 type Compilation_Data is record
105 Pid : Process_Id;
106 Full_Source_File : File_Name_Type;
107 Lib_File : File_Name_Type;
108 Source_Unit : Unit_Name_Type;
109 Full_Lib_File : File_Name_Type;
110 Lib_File_Attr : aliased File_Attributes;
111 Mapping_File : Natural := No_Mapping_File;
112 Project : Project_Id := No_Project;
113 end record;
114 -- Data recorded for each compilation process spawned
115
116 No_Compilation_Data : constant Compilation_Data :=
117 (Invalid_Pid, No_File, No_File, No_Unit_Name, No_File, Unknown_Attributes,
118 No_Mapping_File, No_Project);
119
120 type Comp_Data_Arr is array (Positive range <>) of Compilation_Data;
121 type Comp_Data_Ptr is access Comp_Data_Arr;
122 Running_Compile : Comp_Data_Ptr;
123 -- Used to save information about outstanding compilations
124
125 Outstanding_Compiles : Natural := 0;
126 -- Current number of outstanding compiles
127
128 -------------------------
129 -- Note on terminology --
130 -------------------------
131
132 -- In this program, we use the phrase "termination" of a file name to refer
133 -- to the suffix that appears after the unit name portion. Very often this
134 -- is simply the extension, but in some cases, the sequence may be more
135 -- complex, for example in main.1.ada, the termination in this name is
136 -- ".1.ada" and in main_.ada the termination is "_.ada".
137
138 -------------------------------------
139 -- Queue (Q) Manipulation Routines --
140 -------------------------------------
141
142 -- The Q is used in Compile_Sources below. Its implementation uses the GNAT
143 -- generic package Table (basically an extensible array). Q_Front points to
144 -- the first valid element in the Q, whereas Q.First is the first element
145 -- ever enqueued, while Q.Last - 1 is the last element in the Q.
146 --
147 -- +---+--------------+---+---+---+-----------+---+--------
148 -- Q | | ........ | | | | ....... | |
149 -- +---+--------------+---+---+---+-----------+---+--------
150 -- ^ ^ ^
151 -- Q.First Q_Front Q.Last-1
152 --
153 -- The elements comprised between Q.First and Q_Front-1 are the elements
154 -- that have been enqueued and then dequeued, while the elements between
155 -- Q_Front and Q.Last-1 are the elements currently in the Q. When the Q
156 -- is initialized Q_Front = Q.First = Q.Last. After Compile_Sources has
157 -- terminated its execution, Q_Front = Q.Last and the elements contained
158 -- between Q.First and Q.Last-1 are those that were explored and thus
159 -- marked by Compile_Sources. Whenever the Q is reinitialized, the elements
160 -- between Q.First and Q.Last-1 are unmarked.
161
162 procedure Init_Q;
163 -- Must be called to (re)initialize the Q
164
165 procedure Insert_Q
166 (Source_File : File_Name_Type;
167 Source_Unit : Unit_Name_Type := No_Unit_Name;
168 Index : Int := 0);
169 -- Inserts Source_File at the end of Q. Provide Source_Unit when possible
170 -- for external use (gnatdist). Provide index for multi-unit sources.
171
172 function Empty_Q return Boolean;
173 -- Returns True if Q is empty
174
175 procedure Extract_From_Q
176 (Source_File : out File_Name_Type;
177 Source_Unit : out Unit_Name_Type;
178 Source_Index : out Int);
179 -- Extracts the first element from the Q
180
181 procedure Insert_Project_Sources
182 (The_Project : Project_Id;
183 All_Projects : Boolean;
184 Into_Q : Boolean);
185 -- If Into_Q is True, insert all sources of the project file(s) that are
186 -- not already marked into the Q. If Into_Q is False, call Osint.Add_File
187 -- for the first source, then insert all other sources that are not already
188 -- marked into the Q. If All_Projects is True, all sources of all projects
189 -- are concerned; otherwise, only sources of The_Project are concerned,
190 -- including, if The_Project is an extending project, sources inherited
191 -- from projects being extended.
192
193 First_Q_Initialization : Boolean := True;
194 -- Will be set to false after Init_Q has been called once
195
196 Q_Front : Natural;
197 -- Points to the first valid element in the Q
198
199 Unique_Compile : Boolean := False;
200 -- Set to True if -u or -U or a project file with no main is used
201
202 Unique_Compile_All_Projects : Boolean := False;
203 -- Set to True if -U is used
204
205 Must_Compile : Boolean := False;
206 -- True if gnatmake is invoked with -f -u and one or several mains on the
207 -- command line.
208
209 Main_On_Command_Line : Boolean := False;
210 -- True if gnatmake is invoked with one or several mains on the command
211 -- line.
212
213 RTS_Specified : String_Access := null;
214 -- Used to detect multiple --RTS= switches
215
216 N_M_Switch : Natural := 0;
217 -- Used to count -mxxx switches that can affect multilib
218
219 type Q_Record is record
220 File : File_Name_Type;
221 Unit : Unit_Name_Type;
222 Index : Int;
223 end record;
224 -- File is the name of the file to compile. Unit is for gnatdist
225 -- use in order to easily get the unit name of a file to compile
226 -- when its name is krunched or declared in gnat.adc. Index, when not 0,
227 -- is the index of the unit in a multi-unit source.
228
229 package Q is new Table.Table (
230 Table_Component_Type => Q_Record,
231 Table_Index_Type => Natural,
232 Table_Low_Bound => 0,
233 Table_Initial => 4000,
234 Table_Increment => 100,
235 Table_Name => "Make.Q");
236 -- This is the actual Q
237
238 -- The 3 following packages are used to store gcc, gnatbind and gnatlink
239 -- switches found in the project files.
240
241 package Gcc_Switches is new Table.Table (
242 Table_Component_Type => String_Access,
243 Table_Index_Type => Integer,
244 Table_Low_Bound => 1,
245 Table_Initial => 20,
246 Table_Increment => 100,
247 Table_Name => "Make.Gcc_Switches");
248
249 package Binder_Switches is new Table.Table (
250 Table_Component_Type => String_Access,
251 Table_Index_Type => Integer,
252 Table_Low_Bound => 1,
253 Table_Initial => 20,
254 Table_Increment => 100,
255 Table_Name => "Make.Binder_Switches");
256
257 package Linker_Switches is new Table.Table (
258 Table_Component_Type => String_Access,
259 Table_Index_Type => Integer,
260 Table_Low_Bound => 1,
261 Table_Initial => 20,
262 Table_Increment => 100,
263 Table_Name => "Make.Linker_Switches");
264
265 -- The following instantiations and variables are necessary to save what
266 -- is found on the command line, in case there is a project file specified.
267
268 package Saved_Gcc_Switches is new Table.Table (
269 Table_Component_Type => String_Access,
270 Table_Index_Type => Integer,
271 Table_Low_Bound => 1,
272 Table_Initial => 20,
273 Table_Increment => 100,
274 Table_Name => "Make.Saved_Gcc_Switches");
275
276 package Saved_Binder_Switches is new Table.Table (
277 Table_Component_Type => String_Access,
278 Table_Index_Type => Integer,
279 Table_Low_Bound => 1,
280 Table_Initial => 20,
281 Table_Increment => 100,
282 Table_Name => "Make.Saved_Binder_Switches");
283
284 package Saved_Linker_Switches is new Table.Table
285 (Table_Component_Type => String_Access,
286 Table_Index_Type => Integer,
287 Table_Low_Bound => 1,
288 Table_Initial => 20,
289 Table_Increment => 100,
290 Table_Name => "Make.Saved_Linker_Switches");
291
292 package Switches_To_Check is new Table.Table (
293 Table_Component_Type => String_Access,
294 Table_Index_Type => Integer,
295 Table_Low_Bound => 1,
296 Table_Initial => 20,
297 Table_Increment => 100,
298 Table_Name => "Make.Switches_To_Check");
299
300 package Library_Paths is new Table.Table (
301 Table_Component_Type => String_Access,
302 Table_Index_Type => Integer,
303 Table_Low_Bound => 1,
304 Table_Initial => 20,
305 Table_Increment => 100,
306 Table_Name => "Make.Library_Paths");
307
308 package Failed_Links is new Table.Table (
309 Table_Component_Type => File_Name_Type,
310 Table_Index_Type => Integer,
311 Table_Low_Bound => 1,
312 Table_Initial => 10,
313 Table_Increment => 100,
314 Table_Name => "Make.Failed_Links");
315
316 package Successful_Links is new Table.Table (
317 Table_Component_Type => File_Name_Type,
318 Table_Index_Type => Integer,
319 Table_Low_Bound => 1,
320 Table_Initial => 10,
321 Table_Increment => 100,
322 Table_Name => "Make.Successful_Links");
323
324 package Library_Projs is new Table.Table (
325 Table_Component_Type => Project_Id,
326 Table_Index_Type => Integer,
327 Table_Low_Bound => 1,
328 Table_Initial => 10,
329 Table_Increment => 100,
330 Table_Name => "Make.Library_Projs");
331
332 -- Two variables to keep the last binder and linker switch index in tables
333 -- Binder_Switches and Linker_Switches, before adding switches from the
334 -- project file (if any) and switches from the command line (if any).
335
336 Last_Binder_Switch : Integer := 0;
337 Last_Linker_Switch : Integer := 0;
338
339 Normalized_Switches : Argument_List_Access := new Argument_List (1 .. 10);
340 Last_Norm_Switch : Natural := 0;
341
342 Saved_Maximum_Processes : Natural := 0;
343
344 Gnatmake_Switch_Found : Boolean;
345 -- Set by Scan_Make_Arg. True when the switch is a gnatmake switch.
346 -- Tested by Add_Switches when switches in package Builder must all be
347 -- gnatmake switches.
348
349 Switch_May_Be_Passed_To_The_Compiler : Boolean;
350 -- Set by Add_Switches and Switches_Of. True when unrecognized switches
351 -- are passed to the Ada compiler.
352
353 type Arg_List_Ref is access Argument_List;
354 The_Saved_Gcc_Switches : Arg_List_Ref;
355
356 Project_File_Name : String_Access := null;
357 -- The path name of the main project file, if any
358
359 Project_File_Name_Present : Boolean := False;
360 -- True when -P is used with a space between -P and the project file name
361
362 Current_Verbosity : Prj.Verbosity := Prj.Default;
363 -- Verbosity to parse the project files
364
365 Main_Project : Prj.Project_Id := No_Project;
366 -- The project id of the main project file, if any
367
368 Project_Of_Current_Object_Directory : Project_Id := No_Project;
369 -- The object directory of the project for the last compilation. Avoid
370 -- calling Change_Dir if the current working directory is already this
371 -- directory.
372
373 -- Packages of project files where unknown attributes are errors
374
375 Naming_String : aliased String := "naming";
376 Builder_String : aliased String := "builder";
377 Compiler_String : aliased String := "compiler";
378 Binder_String : aliased String := "binder";
379 Linker_String : aliased String := "linker";
380
381 Gnatmake_Packages : aliased String_List :=
382 (Naming_String 'Access,
383 Builder_String 'Access,
384 Compiler_String 'Access,
385 Binder_String 'Access,
386 Linker_String 'Access);
387
388 Packages_To_Check_By_Gnatmake : constant String_List_Access :=
389 Gnatmake_Packages'Access;
390
391 procedure Add_Library_Search_Dir
392 (Path : String;
393 On_Command_Line : Boolean);
394 -- Call Add_Lib_Search_Dir with an absolute directory path. If Path is
395 -- relative path, when On_Command_Line is True, it is relative to the
396 -- current working directory. When On_Command_Line is False, it is relative
397 -- to the project directory of the main project.
398
399 procedure Add_Source_Search_Dir
400 (Path : String;
401 On_Command_Line : Boolean);
402 -- Call Add_Src_Search_Dir with an absolute directory path. If Path is a
403 -- relative path, when On_Command_Line is True, it is relative to the
404 -- current working directory. When On_Command_Line is False, it is relative
405 -- to the project directory of the main project.
406
407 procedure Add_Source_Dir (N : String);
408 -- Call Add_Src_Search_Dir (output one line when in verbose mode)
409
410 procedure Add_Source_Directories is
411 new Prj.Env.For_All_Source_Dirs (Action => Add_Source_Dir);
412
413 procedure Add_Object_Dir (N : String);
414 -- Call Add_Lib_Search_Dir (output one line when in verbose mode)
415
416 procedure Add_Object_Directories is
417 new Prj.Env.For_All_Object_Dirs (Action => Add_Object_Dir);
418
419 procedure Change_To_Object_Directory (Project : Project_Id);
420 -- Change to the object directory of project Project, if this is not
421 -- already the current working directory.
422
423 type Bad_Compilation_Info is record
424 File : File_Name_Type;
425 Unit : Unit_Name_Type;
426 Found : Boolean;
427 end record;
428 -- File is the name of the file for which a compilation failed. Unit is for
429 -- gnatdist use in order to easily get the unit name of a file when its
430 -- name is krunched or declared in gnat.adc. Found is False if the
431 -- compilation failed because the file could not be found.
432
433 package Bad_Compilation is new Table.Table (
434 Table_Component_Type => Bad_Compilation_Info,
435 Table_Index_Type => Natural,
436 Table_Low_Bound => 1,
437 Table_Initial => 20,
438 Table_Increment => 100,
439 Table_Name => "Make.Bad_Compilation");
440 -- Full name of all the source files for which compilation fails
441
442 Do_Compile_Step : Boolean := True;
443 Do_Bind_Step : Boolean := True;
444 Do_Link_Step : Boolean := True;
445 -- Flags to indicate what step should be executed. Can be set to False
446 -- with the switches -c, -b and -l. These flags are reset to True for
447 -- each invocation of procedure Gnatmake.
448
449 Shared_String : aliased String := "-shared";
450 Force_Elab_Flags_String : aliased String := "-F";
451
452 No_Shared_Switch : aliased Argument_List := (1 .. 0 => null);
453 Shared_Switch : aliased Argument_List := (1 => Shared_String'Access);
454 Bind_Shared : Argument_List_Access := No_Shared_Switch'Access;
455 -- Switch to added in front of gnatbind switches. By default no switch is
456 -- added. Switch "-shared" is added if there is a non-static Library
457 -- Project File.
458
459 Shared_Libgcc : aliased String := "-shared-libgcc";
460
461 No_Shared_Libgcc_Switch : aliased Argument_List := (1 .. 0 => null);
462 Shared_Libgcc_Switch : aliased Argument_List :=
463 (1 => Shared_Libgcc'Access);
464 Link_With_Shared_Libgcc : Argument_List_Access :=
465 No_Shared_Libgcc_Switch'Access;
466
467 procedure Make_Failed (S : String);
468 -- Delete all temp files created by Gnatmake and call Osint.Fail, with the
469 -- parameter S (see osint.ads). This is called from the Prj hierarchy and
470 -- the MLib hierarchy.
471
472 --------------------------
473 -- Obsolete Executables --
474 --------------------------
475
476 Executable_Obsolete : Boolean := False;
477 -- Executable_Obsolete is initially set to False for each executable,
478 -- and is set to True whenever one of the source of the executable is
479 -- compiled, or has already been compiled for another executable.
480
481 Max_Header : constant := 200;
482 -- This needs a proper comment, it used to say "arbitrary"
483 -- that's not an adequate comment ???
484
485 type Header_Num is range 1 .. Max_Header;
486 -- Header_Num for the hash table Obsoleted below
487
488 function Hash (F : File_Name_Type) return Header_Num;
489 -- Hash function for the hash table Obsoleted below
490
491 package Obsoleted is new System.HTable.Simple_HTable
492 (Header_Num => Header_Num,
493 Element => Boolean,
494 No_Element => False,
495 Key => File_Name_Type,
496 Hash => Hash,
497 Equal => "=");
498 -- A hash table to keep all files that have been compiled, to detect
499 -- if an executable is up to date or not.
500
501 procedure Enter_Into_Obsoleted (F : File_Name_Type);
502 -- Enter a file name, without directory information, into the hash table
503 -- Obsoleted.
504
505 function Is_In_Obsoleted (F : File_Name_Type) return Boolean;
506 -- Check if a file name, without directory information, has already been
507 -- entered into the hash table Obsoleted.
508
509 type Dependency is record
510 This : File_Name_Type;
511 Depends_On : File_Name_Type;
512 end record;
513 -- Components of table Dependencies below
514
515 package Dependencies is new Table.Table (
516 Table_Component_Type => Dependency,
517 Table_Index_Type => Integer,
518 Table_Low_Bound => 1,
519 Table_Initial => 20,
520 Table_Increment => 100,
521 Table_Name => "Make.Dependencies");
522 -- A table to keep dependencies, to be able to decide if an executable
523 -- is obsolete. More explanation needed ???
524
525 -- procedure Add_Dependency (S : File_Name_Type; On : File_Name_Type);
526 -- -- Add one entry in table Dependencies
527
528 ----------------------------
529 -- Arguments and Switches --
530 ----------------------------
531
532 Arguments : Argument_List_Access;
533 -- Used to gather the arguments for invocation of the compiler
534
535 Last_Argument : Natural := 0;
536 -- Last index of arguments in Arguments above
537
538 Arguments_Project : Project_Id;
539 -- Project id, if any, of the source to be compiled
540
541 Arguments_Path_Name : Path_Name_Type;
542 -- Full path of the source to be compiled, when Arguments_Project is not
543 -- No_Project.
544
545 Dummy_Switch : constant String_Access := new String'("- ");
546 -- Used to initialized Prev_Switch in procedure Check
547
548 procedure Add_Arguments (Args : Argument_List);
549 -- Add arguments to global variable Arguments, increasing its size
550 -- if necessary and adjusting Last_Argument.
551
552 function Configuration_Pragmas_Switch
553 (For_Project : Project_Id) return Argument_List;
554 -- Return an argument list of one element, if there is a configuration
555 -- pragmas file to be specified for For_Project,
556 -- otherwise return an empty argument list.
557
558 -------------------
559 -- Misc Routines --
560 -------------------
561
562 procedure List_Depend;
563 -- Prints to standard output the list of object dependencies. This list
564 -- can be used directly in a Makefile. A call to Compile_Sources must
565 -- precede the call to List_Depend. Also because this routine uses the
566 -- ALI files that were originally loaded and scanned by Compile_Sources,
567 -- no additional ALI files should be scanned between the two calls (i.e.
568 -- between the call to Compile_Sources and List_Depend.)
569
570 procedure List_Bad_Compilations;
571 -- Prints out the list of all files for which the compilation failed
572
573 Usage_Needed : Boolean := True;
574 -- Flag used to make sure Makeusg is call at most once
575
576 procedure Usage;
577 -- Call Makeusg, if Usage_Needed is True.
578 -- Set Usage_Needed to False.
579
580 procedure Debug_Msg (S : String; N : Name_Id);
581 procedure Debug_Msg (S : String; N : File_Name_Type);
582 procedure Debug_Msg (S : String; N : Unit_Name_Type);
583 -- If Debug.Debug_Flag_W is set outputs string S followed by name N
584
585 procedure Recursive_Compute_Depth (Project : Project_Id);
586 -- Compute depth of Project and of the projects it depends on
587
588 -----------------------
589 -- Gnatmake Routines --
590 -----------------------
591
592 subtype Lib_Mark_Type is Byte;
593 -- Used in Mark_Directory
594
595 Ada_Lib_Dir : constant Lib_Mark_Type := 1;
596 -- Used to mark a directory as a GNAT lib dir
597
598 -- Note that the notion of GNAT lib dir is no longer used. The code related
599 -- to it has not been removed to give an idea on how to use the directory
600 -- prefix marking mechanism.
601
602 -- An Ada library directory is a directory containing ali and object files
603 -- but no source files for the bodies (the specs can be in the same or some
604 -- other directory). These directories are specified in the Gnatmake
605 -- command line with the switch "-Adir" (to specify the spec location -Idir
606 -- cab be used). Gnatmake skips the missing sources whose ali are in Ada
607 -- library directories. For an explanation of why Gnatmake behaves that
608 -- way, see the spec of Make.Compile_Sources. The directory lookup penalty
609 -- is incurred every single time this routine is called.
610
611 procedure Check_Steps;
612 -- Check what steps (Compile, Bind, Link) must be executed.
613 -- Set the step flags accordingly.
614
615 function In_Ada_Lib_Dir (File : File_Name_Type) return Boolean;
616 -- Get directory prefix of this file and get lib mark stored in name
617 -- table for this directory. Then check if an Ada lib mark has been set.
618
619 procedure Mark_Directory
620 (Dir : String;
621 Mark : Lib_Mark_Type;
622 On_Command_Line : Boolean);
623 -- Store the absolute path from Dir in name table and set lib mark as name
624 -- info to identify Ada libraries.
625 --
626 -- If Dir is a relative path, when On_Command_Line is True, it is relative
627 -- to the current working directory; when On_Command_Line is False, it is
628 -- relative to the project directory of the main project.
629
630 Output_Is_Object : Boolean := True;
631 -- Set to False when using a switch -S for the compiler
632
633 procedure Check_For_S_Switch;
634 -- Set Output_Is_Object to False when the -S switch is used for the
635 -- compiler.
636
637 function Switches_Of
638 (Source_File : File_Name_Type;
639 Source_File_Name : String;
640 Source_Index : Int;
641 Project : Project_Id;
642 In_Package : Package_Id;
643 Allow_ALI : Boolean) return Variable_Value;
644 -- Return the switches for the source file in the specified package of a
645 -- project file. If the Source_File ends with a standard GNAT extension
646 -- (".ads" or ".adb"), try first the full name, then the name without the
647 -- extension, then, if Allow_ALI is True, the name with the extension
648 -- ".ali". If there is no switches for either names, try first Switches
649 -- (others) then the default switches for Ada. If all failed, return
650 -- No_Variable_Value.
651
652 function Is_In_Object_Directory
653 (Source_File : File_Name_Type;
654 Full_Lib_File : File_Name_Type) return Boolean;
655 -- Check if, when using a project file, the ALI file is in the project
656 -- directory of the ultimate extending project. If it is not, we ignore
657 -- the fact that this ALI file is read-only.
658
659 procedure Process_Multilib (Project_Node_Tree : Project_Node_Tree_Ref);
660 -- Add appropriate --RTS argument to handle multilib
661
662 ----------------------------------------------------
663 -- Compiler, Binder & Linker Data and Subprograms --
664 ----------------------------------------------------
665
666 Gcc : String_Access := Program_Name ("gcc", "gnatmake");
667 Gnatbind : String_Access := Program_Name ("gnatbind", "gnatmake");
668 Gnatlink : String_Access := Program_Name ("gnatlink", "gnatmake");
669 -- Default compiler, binder, linker programs
670
671 Saved_Gcc : String_Access := null;
672 Saved_Gnatbind : String_Access := null;
673 Saved_Gnatlink : String_Access := null;
674 -- Given by the command line. Will be used, if non null
675
676 Gcc_Path : String_Access :=
677 GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
678 Gnatbind_Path : String_Access :=
679 GNAT.OS_Lib.Locate_Exec_On_Path (Gnatbind.all);
680 Gnatlink_Path : String_Access :=
681 GNAT.OS_Lib.Locate_Exec_On_Path (Gnatlink.all);
682 -- Path for compiler, binder, linker programs, defaulted now for gnatdist.
683 -- Changed later if overridden on command line.
684
685 Comp_Flag : constant String_Access := new String'("-c");
686 Output_Flag : constant String_Access := new String'("-o");
687 Ada_Flag_1 : constant String_Access := new String'("-x");
688 Ada_Flag_2 : constant String_Access := new String'("ada");
689 No_gnat_adc : constant String_Access := new String'("-gnatA");
690 GNAT_Flag : constant String_Access := new String'("-gnatpg");
691 Do_Not_Check_Flag : constant String_Access := new String'("-x");
692
693 Object_Suffix : constant String := Get_Target_Object_Suffix.all;
694
695 Syntax_Only : Boolean := False;
696 -- Set to True when compiling with -gnats
697
698 Display_Executed_Programs : Boolean := True;
699 -- Set to True if name of commands should be output on stderr (or on stdout
700 -- if the Commands_To_Stdout flag was set by use of the -eS switch).
701
702 Output_File_Name_Seen : Boolean := False;
703 -- Set to True after having scanned the file_name for
704 -- switch "-o file_name"
705
706 Object_Directory_Seen : Boolean := False;
707 -- Set to True after having scanned the object directory for
708 -- switch "-D obj_dir".
709
710 Object_Directory_Path : String_Access := null;
711 -- The path name of the object directory, set with switch -D
712
713 type Make_Program_Type is (None, Compiler, Binder, Linker);
714
715 Program_Args : Make_Program_Type := None;
716 -- Used to indicate if we are scanning gnatmake, gcc, gnatbind, or gnatbind
717 -- options within the gnatmake command line. Used in Scan_Make_Arg only,
718 -- but must be global since value preserved from one call to another.
719
720 Temporary_Config_File : Boolean := False;
721 -- Set to True when there is a temporary config file used for a project
722 -- file, to avoid displaying the -gnatec switch for a temporary file.
723
724 procedure Add_Switches
725 (The_Package : Package_Id;
726 File_Name : String;
727 Index : Int;
728 Program : Make_Program_Type;
729 Unknown_Switches_To_The_Compiler : Boolean := True;
730 Project_Node_Tree : Project_Node_Tree_Ref);
731 procedure Add_Switch
732 (S : String_Access;
733 Program : Make_Program_Type;
734 Append_Switch : Boolean := True;
735 And_Save : Boolean := True);
736 procedure Add_Switch
737 (S : String;
738 Program : Make_Program_Type;
739 Append_Switch : Boolean := True;
740 And_Save : Boolean := True);
741 -- Make invokes one of three programs (the compiler, the binder or the
742 -- linker). For the sake of convenience, some program specific switches
743 -- can be passed directly on the gnatmake command line. This procedure
744 -- records these switches so that gnatmake can pass them to the right
745 -- program. S is the switch to be added at the end of the command line
746 -- for Program if Append_Switch is True. If Append_Switch is False S is
747 -- added at the beginning of the command line.
748
749 procedure Check
750 (Source_File : File_Name_Type;
751 Source_Index : Int;
752 Is_Main_Source : Boolean;
753 The_Args : Argument_List;
754 Lib_File : File_Name_Type;
755 Full_Lib_File : File_Name_Type;
756 Lib_File_Attr : access File_Attributes;
757 Read_Only : Boolean;
758 ALI : out ALI_Id;
759 O_File : out File_Name_Type;
760 O_Stamp : out Time_Stamp_Type);
761 -- Determines whether the library file Lib_File is up-to-date or not. The
762 -- full name (with path information) of the object file corresponding to
763 -- Lib_File is returned in O_File. Its time stamp is saved in O_Stamp.
764 -- ALI is the ALI_Id corresponding to Lib_File. If Lib_File in not
765 -- up-to-date, then the corresponding source file needs to be recompiled.
766 -- In this case ALI = No_ALI_Id.
767 -- Full_Lib_File must be the result of calling Osint.Full_Lib_File_Name on
768 -- Lib_File. Precomputing it saves system calls. Lib_File_Attr is the
769 -- initialized attributes of that file, which is also used to save on
770 -- system calls (it can safely be initialized to Unknown_Attributes).
771
772 procedure Check_Linker_Options
773 (E_Stamp : Time_Stamp_Type;
774 O_File : out File_Name_Type;
775 O_Stamp : out Time_Stamp_Type);
776 -- Checks all linker options for linker files that are newer
777 -- than E_Stamp. If such objects are found, the youngest object
778 -- is returned in O_File and its stamp in O_Stamp.
779 --
780 -- If no obsolete linker files were found, the first missing
781 -- linker file is returned in O_File and O_Stamp is empty.
782 -- Otherwise O_File is No_File.
783
784 procedure Collect_Arguments
785 (Source_File : File_Name_Type;
786 Source_Index : Int;
787 Is_Main_Source : Boolean;
788 Args : Argument_List);
789 -- Collect all arguments for a source to be compiled, including those
790 -- that come from a project file.
791
792 procedure Display (Program : String; Args : Argument_List);
793 -- Displays Program followed by the arguments in Args if variable
794 -- Display_Executed_Programs is set. The lower bound of Args must be 1.
795
796 procedure Report_Compilation_Failed;
797 -- Delete all temporary files and fail graciously
798
799 -----------------
800 -- Mapping files
801 -----------------
802
803 type Temp_Path_Names is array (Positive range <>) of Path_Name_Type;
804 type Temp_Path_Ptr is access Temp_Path_Names;
805
806 type Free_File_Indices is array (Positive range <>) of Positive;
807 type Free_Indices_Ptr is access Free_File_Indices;
808
809 type Project_Compilation_Data is record
810 Mapping_File_Names : Temp_Path_Ptr;
811 -- The name ids of the temporary mapping files used. This is indexed
812 -- on the maximum number of compilation processes we will be spawning
813 -- (-j parameter)
814
815 Last_Mapping_File_Names : Natural;
816 -- Index of the last mapping file created for this project
817
818 Free_Mapping_File_Indices : Free_Indices_Ptr;
819 -- Indices in Mapping_File_Names of the mapping file names that can be
820 -- reused for subsequent compilations.
821
822 Last_Free_Indices : Natural;
823 -- Number of mapping files that can be reused
824 end record;
825 -- Information necessary when compiling a project
826
827 type Project_Compilation_Access is access Project_Compilation_Data;
828
829 package Project_Compilation_Htable is new Simple_HTable
830 (Header_Num => Prj.Header_Num,
831 Element => Project_Compilation_Access,
832 No_Element => null,
833 Key => Project_Id,
834 Hash => Prj.Hash,
835 Equal => "=");
836
837 Project_Compilation : Project_Compilation_Htable.Instance;
838
839 Gnatmake_Mapping_File : String_Access := null;
840 -- The path name of a mapping file specified by switch -C=
841
842 procedure Init_Mapping_File
843 (Project : Project_Id;
844 Data : in out Project_Compilation_Data;
845 File_Index : in out Natural);
846 -- Create a new temporary mapping file, and fill it with the project file
847 -- mappings, when using project file(s). The out parameter File_Index is
848 -- the index to the name of the file in the array The_Mapping_File_Names.
849
850 procedure Delete_Temp_Config_Files;
851 -- Delete all temporary config files. Must not be called if Debug_Flag_N
852 -- is False.
853
854 procedure Delete_All_Temp_Files;
855 -- Delete all temp files (config files, mapping files, path files), unless
856 -- Debug_Flag_N is True (in which case all temp files are left for user
857 -- examination).
858
859 -------------------------------------------------
860 -- Subprogram declarations moved from the spec --
861 -------------------------------------------------
862
863 procedure Bind (ALI_File : File_Name_Type; Args : Argument_List);
864 -- Binds ALI_File. Args are the arguments to pass to the binder.
865 -- Args must have a lower bound of 1.
866
867 procedure Display_Commands (Display : Boolean := True);
868 -- The default behavior of Make commands (Compile_Sources, Bind, Link)
869 -- is to display them on stderr. This behavior can be changed repeatedly
870 -- by invoking this procedure.
871
872 -- If a compilation, bind or link failed one of the following 3 exceptions
873 -- is raised. These need to be handled by the calling routines.
874
875 procedure Compile_Sources
876 (Main_Source : File_Name_Type;
877 Args : Argument_List;
878 First_Compiled_File : out File_Name_Type;
879 Most_Recent_Obj_File : out File_Name_Type;
880 Most_Recent_Obj_Stamp : out Time_Stamp_Type;
881 Main_Unit : out Boolean;
882 Compilation_Failures : out Natural;
883 Main_Index : Int := 0;
884 Check_Readonly_Files : Boolean := False;
885 Do_Not_Execute : Boolean := False;
886 Force_Compilations : Boolean := False;
887 Keep_Going : Boolean := False;
888 In_Place_Mode : Boolean := False;
889 Initialize_ALI_Data : Boolean := True;
890 Max_Process : Positive := 1);
891 -- Compile_Sources will recursively compile all the sources needed by
892 -- Main_Source. Before calling this routine make sure Namet has been
893 -- initialized. This routine can be called repeatedly with different
894 -- Main_Source file as long as all the source (-I flags), library
895 -- (-B flags) and ada library (-A flags) search paths between calls are
896 -- *exactly* the same. The default directory must also be the same.
897 --
898 -- Args contains the arguments to use during the compilations.
899 -- The lower bound of Args must be 1.
900 --
901 -- First_Compiled_File is set to the name of the first file that is
902 -- compiled or that needs to be compiled. This is set to No_Name if no
903 -- compilations were needed.
904 --
905 -- Most_Recent_Obj_File is set to the full name of the most recent
906 -- object file found when no compilations are needed, that is when
907 -- First_Compiled_File is set to No_Name. When First_Compiled_File
908 -- is set then Most_Recent_Obj_File is set to No_Name.
909 --
910 -- Most_Recent_Obj_Stamp is the time stamp of Most_Recent_Obj_File.
911 --
912 -- Main_Unit is set to True if Main_Source can be a main unit.
913 -- If Do_Not_Execute is False and First_Compiled_File /= No_Name
914 -- the value of Main_Unit is always False.
915 -- Is this used any more??? It is certainly not used by gnatmake???
916 --
917 -- Compilation_Failures is a count of compilation failures. This count
918 -- is used to extract compilation failure reports with Extract_Failure.
919 --
920 -- Main_Index, when not zero, is the index of the main unit in source
921 -- file Main_Source which is a multi-unit source.
922 -- Zero indicates that Main_Source is a single unit source file.
923 --
924 -- Check_Readonly_Files set it to True to compile source files
925 -- which library files are read-only. When compiling GNAT predefined
926 -- files the "-gnatg" flag is used.
927 --
928 -- Do_Not_Execute set it to True to find out the first source that
929 -- needs to be recompiled, but without recompiling it. This file is
930 -- saved in First_Compiled_File.
931 --
932 -- Force_Compilations forces all compilations no matter what but
933 -- recompiles read-only files only if Check_Readonly_Files
934 -- is set.
935 --
936 -- Keep_Going when True keep compiling even in the presence of
937 -- compilation errors.
938 --
939 -- In_Place_Mode when True save library/object files in their object
940 -- directory if they already exist; otherwise, in the source directory.
941 --
942 -- Initialize_ALI_Data set it to True when you want to initialize ALI
943 -- data-structures. This is what you should do most of the time.
944 -- (especially the first time around when you call this routine).
945 -- This parameter is set to False to preserve previously recorded
946 -- ALI file data.
947 --
948 -- Max_Process is the maximum number of processes that should be spawned
949 -- to carry out compilations.
950 --
951 -- Flags in Package Opt Affecting Compile_Sources
952 -- -----------------------------------------------
953 --
954 -- Check_Object_Consistency set it to False to omit all consistency
955 -- checks between an .ali file and its corresponding object file.
956 -- When this flag is set to true, every time an .ali is read,
957 -- package Osint checks that the corresponding object file
958 -- exists and is more recent than the .ali.
959 --
960 -- Use of Name Table Info
961 -- ----------------------
962 --
963 -- All file names manipulated by Compile_Sources are entered into the
964 -- Names table. The Byte field of a source file is used to mark it.
965 --
966 -- Calling Compile_Sources Several Times
967 -- -------------------------------------
968 --
969 -- Upon return from Compile_Sources all the ALI data structures are left
970 -- intact for further browsing. HOWEVER upon entry to this routine ALI
971 -- data structures are re-initialized if parameter Initialize_ALI_Data
972 -- above is set to true. Typically this is what you want the first time
973 -- you call Compile_Sources. You should not load an ali file, call this
974 -- routine with flag Initialize_ALI_Data set to True and then expect
975 -- that ALI information to be around after the call. Note that the first
976 -- time you call Compile_Sources you better set Initialize_ALI_Data to
977 -- True unless you have called Initialize_ALI yourself.
978 --
979 -- Compile_Sources ALGORITHM : Compile_Sources (Main_Source)
980 -- -------------------------
981 --
982 -- 1. Insert Main_Source in a Queue (Q) and mark it.
983 --
984 -- 2. Let unit.adb be the file at the head of the Q. If unit.adb is
985 -- missing but its corresponding ali file is in an Ada library directory
986 -- (see below) then, remove unit.adb from the Q and goto step 4.
987 -- Otherwise, look at the files under the D (dependency) section of
988 -- unit.ali. If unit.ali does not exist or some of the time stamps do
989 -- not match, (re)compile unit.adb.
990 --
991 -- An Ada library directory is a directory containing Ada specs, ali
992 -- and object files but no source files for the bodies. An Ada library
993 -- directory is communicated to gnatmake by means of some switch so that
994 -- gnatmake can skip the sources whole ali are in that directory.
995 -- There are two reasons for skipping the sources in this case. Firstly,
996 -- Ada libraries typically come without full sources but binding and
997 -- linking against those libraries is still possible. Secondly, it would
998 -- be very wasteful for gnatmake to systematically check the consistency
999 -- of every external Ada library used in a program. The binder is
1000 -- already in charge of catching any potential inconsistencies.
1001 --
1002 -- 3. Look into the W section of unit.ali and insert into the Q all
1003 -- unmarked source files. Mark all files newly inserted in the Q.
1004 -- Specifically, assuming that the W section looks like
1005 --
1006 -- W types%s types.adb types.ali
1007 -- W unchecked_deallocation%s
1008 -- W xref_tab%s xref_tab.adb xref_tab.ali
1009 --
1010 -- Then xref_tab.adb and types.adb are inserted in the Q if they are not
1011 -- already marked.
1012 -- Note that there is no file listed under W unchecked_deallocation%s
1013 -- so no generic body should ever be explicitly compiled (unless the
1014 -- Main_Source at the start was a generic body).
1015 --
1016 -- 4. Repeat steps 2 and 3 above until the Q is empty
1017 --
1018 -- Note that the above algorithm works because the units withed in
1019 -- subunits are transitively included in the W section (with section) of
1020 -- the main unit. Likewise the withed units in a generic body needed
1021 -- during a compilation are also transitively included in the W section
1022 -- of the originally compiled file.
1023
1024 procedure Initialize (Project_Node_Tree : out Project_Node_Tree_Ref);
1025 -- Performs default and package initialization. Therefore,
1026 -- Compile_Sources can be called by an external unit.
1027
1028 procedure Link
1029 (ALI_File : File_Name_Type;
1030 Args : Argument_List;
1031 Success : out Boolean);
1032 -- Links ALI_File. Args are the arguments to pass to the linker.
1033 -- Args must have a lower bound of 1. Success indicates if the link
1034 -- succeeded or not.
1035
1036 procedure Scan_Make_Arg
1037 (Project_Node_Tree : Project_Node_Tree_Ref;
1038 Argv : String;
1039 And_Save : Boolean);
1040 -- Scan make arguments. Argv is a single argument to be processed.
1041 -- Project_Node_Tree will be used to initialize external references. It
1042 -- must have been initialized.
1043
1044 -------------------
1045 -- Add_Arguments --
1046 -------------------
1047
1048 procedure Add_Arguments (Args : Argument_List) is
1049 begin
1050 if Arguments = null then
1051 Arguments := new Argument_List (1 .. Args'Length + 10);
1052
1053 else
1054 while Last_Argument + Args'Length > Arguments'Last loop
1055 declare
1056 New_Arguments : constant Argument_List_Access :=
1057 new Argument_List (1 .. Arguments'Last * 2);
1058 begin
1059 New_Arguments (1 .. Last_Argument) :=
1060 Arguments (1 .. Last_Argument);
1061 Arguments := New_Arguments;
1062 end;
1063 end loop;
1064 end if;
1065
1066 Arguments (Last_Argument + 1 .. Last_Argument + Args'Length) := Args;
1067 Last_Argument := Last_Argument + Args'Length;
1068 end Add_Arguments;
1069
1070 -- --------------------
1071 -- -- Add_Dependency --
1072 -- --------------------
1073 --
1074 -- procedure Add_Dependency (S : File_Name_Type; On : File_Name_Type) is
1075 -- begin
1076 -- Dependencies.Increment_Last;
1077 -- Dependencies.Table (Dependencies.Last) := (S, On);
1078 -- end Add_Dependency;
1079
1080 ----------------------------
1081 -- Add_Library_Search_Dir --
1082 ----------------------------
1083
1084 procedure Add_Library_Search_Dir
1085 (Path : String;
1086 On_Command_Line : Boolean)
1087 is
1088 begin
1089 if On_Command_Line then
1090 Add_Lib_Search_Dir (Normalize_Pathname (Path));
1091
1092 else
1093 Get_Name_String (Main_Project.Directory.Display_Name);
1094 Add_Lib_Search_Dir
1095 (Normalize_Pathname (Path, Name_Buffer (1 .. Name_Len)));
1096 end if;
1097 end Add_Library_Search_Dir;
1098
1099 --------------------
1100 -- Add_Object_Dir --
1101 --------------------
1102
1103 procedure Add_Object_Dir (N : String) is
1104 begin
1105 Add_Lib_Search_Dir (N);
1106
1107 if Verbose_Mode then
1108 Write_Str ("Adding object directory """);
1109 Write_Str (N);
1110 Write_Str (""".");
1111 Write_Eol;
1112 end if;
1113 end Add_Object_Dir;
1114
1115 --------------------
1116 -- Add_Source_Dir --
1117 --------------------
1118
1119 procedure Add_Source_Dir (N : String) is
1120 begin
1121 Add_Src_Search_Dir (N);
1122
1123 if Verbose_Mode then
1124 Write_Str ("Adding source directory """);
1125 Write_Str (N);
1126 Write_Str (""".");
1127 Write_Eol;
1128 end if;
1129 end Add_Source_Dir;
1130
1131 ---------------------------
1132 -- Add_Source_Search_Dir --
1133 ---------------------------
1134
1135 procedure Add_Source_Search_Dir
1136 (Path : String;
1137 On_Command_Line : Boolean)
1138 is
1139 begin
1140 if On_Command_Line then
1141 Add_Src_Search_Dir (Normalize_Pathname (Path));
1142
1143 else
1144 Get_Name_String (Main_Project.Directory.Display_Name);
1145 Add_Src_Search_Dir
1146 (Normalize_Pathname (Path, Name_Buffer (1 .. Name_Len)));
1147 end if;
1148 end Add_Source_Search_Dir;
1149
1150 ----------------
1151 -- Add_Switch --
1152 ----------------
1153
1154 procedure Add_Switch
1155 (S : String_Access;
1156 Program : Make_Program_Type;
1157 Append_Switch : Boolean := True;
1158 And_Save : Boolean := True)
1159 is
1160 generic
1161 with package T is new Table.Table (<>);
1162 procedure Generic_Position (New_Position : out Integer);
1163 -- Generic procedure that chooses a position for S in T at the
1164 -- beginning or the end, depending on the boolean Append_Switch.
1165 -- Calling this procedure may expand the table.
1166
1167 ----------------------
1168 -- Generic_Position --
1169 ----------------------
1170
1171 procedure Generic_Position (New_Position : out Integer) is
1172 begin
1173 T.Increment_Last;
1174
1175 if Append_Switch then
1176 New_Position := Integer (T.Last);
1177 else
1178 for J in reverse T.Table_Index_Type'Succ (T.First) .. T.Last loop
1179 T.Table (J) := T.Table (T.Table_Index_Type'Pred (J));
1180 end loop;
1181
1182 New_Position := Integer (T.First);
1183 end if;
1184 end Generic_Position;
1185
1186 procedure Gcc_Switches_Pos is new Generic_Position (Gcc_Switches);
1187 procedure Binder_Switches_Pos is new Generic_Position (Binder_Switches);
1188 procedure Linker_Switches_Pos is new Generic_Position (Linker_Switches);
1189
1190 procedure Saved_Gcc_Switches_Pos is new
1191 Generic_Position (Saved_Gcc_Switches);
1192
1193 procedure Saved_Binder_Switches_Pos is new
1194 Generic_Position (Saved_Binder_Switches);
1195
1196 procedure Saved_Linker_Switches_Pos is new
1197 Generic_Position (Saved_Linker_Switches);
1198
1199 New_Position : Integer;
1200
1201 -- Start of processing for Add_Switch
1202
1203 begin
1204 if And_Save then
1205 case Program is
1206 when Compiler =>
1207 Saved_Gcc_Switches_Pos (New_Position);
1208 Saved_Gcc_Switches.Table (New_Position) := S;
1209
1210 when Binder =>
1211 Saved_Binder_Switches_Pos (New_Position);
1212 Saved_Binder_Switches.Table (New_Position) := S;
1213
1214 when Linker =>
1215 Saved_Linker_Switches_Pos (New_Position);
1216 Saved_Linker_Switches.Table (New_Position) := S;
1217
1218 when None =>
1219 raise Program_Error;
1220 end case;
1221
1222 else
1223 case Program is
1224 when Compiler =>
1225 Gcc_Switches_Pos (New_Position);
1226 Gcc_Switches.Table (New_Position) := S;
1227
1228 when Binder =>
1229 Binder_Switches_Pos (New_Position);
1230 Binder_Switches.Table (New_Position) := S;
1231
1232 when Linker =>
1233 Linker_Switches_Pos (New_Position);
1234 Linker_Switches.Table (New_Position) := S;
1235
1236 when None =>
1237 raise Program_Error;
1238 end case;
1239 end if;
1240 end Add_Switch;
1241
1242 procedure Add_Switch
1243 (S : String;
1244 Program : Make_Program_Type;
1245 Append_Switch : Boolean := True;
1246 And_Save : Boolean := True)
1247 is
1248 begin
1249 Add_Switch (S => new String'(S),
1250 Program => Program,
1251 Append_Switch => Append_Switch,
1252 And_Save => And_Save);
1253 end Add_Switch;
1254
1255 ------------------
1256 -- Add_Switches --
1257 ------------------
1258
1259 procedure Add_Switches
1260 (The_Package : Package_Id;
1261 File_Name : String;
1262 Index : Int;
1263 Program : Make_Program_Type;
1264 Unknown_Switches_To_The_Compiler : Boolean := True;
1265 Project_Node_Tree : Project_Node_Tree_Ref)
1266 is
1267 Switches : Variable_Value;
1268 Switch_List : String_List_Id;
1269 Element : String_Element;
1270
1271 begin
1272 Switch_May_Be_Passed_To_The_Compiler :=
1273 Unknown_Switches_To_The_Compiler;
1274
1275 if File_Name'Length > 0 then
1276 Name_Len := 0;
1277 Add_Str_To_Name_Buffer (File_Name);
1278 Switches :=
1279 Switches_Of
1280 (Source_File => Name_Find,
1281 Source_File_Name => File_Name,
1282 Source_Index => Index,
1283 Project => Main_Project,
1284 In_Package => The_Package,
1285 Allow_ALI => Program = Binder or else Program = Linker);
1286
1287 if Switches.Kind = List then
1288 Program_Args := Program;
1289
1290 Switch_List := Switches.Values;
1291 while Switch_List /= Nil_String loop
1292 Element := Project_Tree.String_Elements.Table (Switch_List);
1293 Get_Name_String (Element.Value);
1294
1295 if Name_Len > 0 then
1296 declare
1297 Argv : constant String := Name_Buffer (1 .. Name_Len);
1298 -- We need a copy, because Name_Buffer may be modified
1299
1300 begin
1301 if Verbose_Mode then
1302 Write_Str (" Adding ");
1303 Write_Line (Argv);
1304 end if;
1305
1306 Scan_Make_Arg
1307 (Project_Node_Tree, Argv, And_Save => False);
1308
1309 if not Gnatmake_Switch_Found
1310 and then not Switch_May_Be_Passed_To_The_Compiler
1311 then
1312 Errutil.Error_Msg
1313 ('"' & Argv &
1314 """ is not a gnatmake switch. Consider moving " &
1315 "it to Global_Compilation_Switches.",
1316 Element.Location);
1317 Errutil.Finalize;
1318 Make_Failed ("*** illegal switch """ & Argv & """");
1319 end if;
1320 end;
1321 end if;
1322
1323 Switch_List := Element.Next;
1324 end loop;
1325 end if;
1326 end if;
1327 end Add_Switches;
1328
1329 ----------
1330 -- Bind --
1331 ----------
1332
1333 procedure Bind (ALI_File : File_Name_Type; Args : Argument_List) is
1334 Bind_Args : Argument_List (1 .. Args'Last + 2);
1335 Bind_Last : Integer;
1336 Success : Boolean;
1337
1338 begin
1339 pragma Assert (Args'First = 1);
1340
1341 -- Optimize the simple case where the gnatbind command line looks like
1342 -- gnatbind -aO. -I- file.ali --into-> gnatbind file.adb
1343
1344 if Args'Length = 2
1345 and then Args (Args'First).all = "-aO" & Normalized_CWD
1346 and then Args (Args'Last).all = "-I-"
1347 and then ALI_File = Strip_Directory (ALI_File)
1348 then
1349 Bind_Last := Args'First - 1;
1350
1351 else
1352 Bind_Last := Args'Last;
1353 Bind_Args (Args'Range) := Args;
1354 end if;
1355
1356 -- It is completely pointless to re-check source file time stamps. This
1357 -- has been done already by gnatmake
1358
1359 Bind_Last := Bind_Last + 1;
1360 Bind_Args (Bind_Last) := Do_Not_Check_Flag;
1361
1362 Get_Name_String (ALI_File);
1363
1364 Bind_Last := Bind_Last + 1;
1365 Bind_Args (Bind_Last) := new String'(Name_Buffer (1 .. Name_Len));
1366
1367 GNAT.OS_Lib.Normalize_Arguments (Bind_Args (Args'First .. Bind_Last));
1368
1369 Display (Gnatbind.all, Bind_Args (Args'First .. Bind_Last));
1370
1371 if Gnatbind_Path = null then
1372 Make_Failed ("error, unable to locate " & Gnatbind.all);
1373 end if;
1374
1375 GNAT.OS_Lib.Spawn
1376 (Gnatbind_Path.all, Bind_Args (Args'First .. Bind_Last), Success);
1377
1378 if not Success then
1379 Make_Failed ("*** bind failed.");
1380 end if;
1381 end Bind;
1382
1383 --------------------------------
1384 -- Change_To_Object_Directory --
1385 --------------------------------
1386
1387 procedure Change_To_Object_Directory (Project : Project_Id) is
1388 Object_Directory : Path_Name_Type;
1389
1390 begin
1391 pragma Assert (Project /= No_Project);
1392
1393 -- Nothing to do if the current working directory is already the correct
1394 -- object directory.
1395
1396 if Project_Of_Current_Object_Directory /= Project then
1397 Project_Of_Current_Object_Directory := Project;
1398 Object_Directory := Project.Object_Directory.Display_Name;
1399
1400 -- Set the working directory to the object directory of the actual
1401 -- project.
1402
1403 if Verbose_Mode then
1404 Write_Str ("Changing to object directory of """);
1405 Write_Name (Project.Display_Name);
1406 Write_Str (""": """);
1407 Write_Name (Object_Directory);
1408 Write_Line ("""");
1409 end if;
1410
1411 Change_Dir (Get_Name_String (Object_Directory));
1412 end if;
1413
1414 exception
1415 -- Fail if unable to change to the object directory
1416
1417 when Directory_Error =>
1418 Make_Failed ("unable to change to object directory """ &
1419 Path_Or_File_Name
1420 (Project.Object_Directory.Name) &
1421 """ of project " &
1422 Get_Name_String (Project.Display_Name));
1423 end Change_To_Object_Directory;
1424
1425 -----------
1426 -- Check --
1427 -----------
1428
1429 procedure Check
1430 (Source_File : File_Name_Type;
1431 Source_Index : Int;
1432 Is_Main_Source : Boolean;
1433 The_Args : Argument_List;
1434 Lib_File : File_Name_Type;
1435 Full_Lib_File : File_Name_Type;
1436 Lib_File_Attr : access File_Attributes;
1437 Read_Only : Boolean;
1438 ALI : out ALI_Id;
1439 O_File : out File_Name_Type;
1440 O_Stamp : out Time_Stamp_Type)
1441 is
1442 function First_New_Spec (A : ALI_Id) return File_Name_Type;
1443 -- Looks in the with table entries of A and returns the spec file name
1444 -- of the first withed unit (subprogram) for which no spec existed when
1445 -- A was generated but for which there exists one now, implying that A
1446 -- is now obsolete. If no such unit is found No_File is returned.
1447 -- Otherwise the spec file name of the unit is returned.
1448 --
1449 -- **WARNING** in the event of Uname format modifications, one *MUST*
1450 -- make sure this function is also updated.
1451 --
1452 -- Note: This function should really be in ali.adb and use Uname
1453 -- services, but this causes the whole compiler to be dragged along
1454 -- for gnatbind and gnatmake.
1455
1456 --------------------
1457 -- First_New_Spec --
1458 --------------------
1459
1460 function First_New_Spec (A : ALI_Id) return File_Name_Type is
1461 Spec_File_Name : File_Name_Type := No_File;
1462
1463 function New_Spec (Uname : Unit_Name_Type) return Boolean;
1464 -- Uname is the name of the spec or body of some ada unit. This
1465 -- function returns True if the Uname is the name of a body which has
1466 -- a spec not mentioned in ALI file A. If True is returned
1467 -- Spec_File_Name above is set to the name of this spec file.
1468
1469 --------------
1470 -- New_Spec --
1471 --------------
1472
1473 function New_Spec (Uname : Unit_Name_Type) return Boolean is
1474 Spec_Name : Unit_Name_Type;
1475 File_Name : File_Name_Type;
1476
1477 begin
1478 -- Test whether Uname is the name of a body unit (i.e. ends
1479 -- with %b)
1480
1481 Get_Name_String (Uname);
1482 pragma
1483 Assert (Name_Len > 2 and then Name_Buffer (Name_Len - 1) = '%');
1484
1485 if Name_Buffer (Name_Len) /= 'b' then
1486 return False;
1487 end if;
1488
1489 -- Convert unit name into spec name
1490
1491 -- ??? this code seems dubious in presence of pragma
1492 -- Source_File_Name since there is no more direct relationship
1493 -- between unit name and file name.
1494
1495 -- ??? Further, what about alternative subunit naming
1496
1497 Name_Buffer (Name_Len) := 's';
1498 Spec_Name := Name_Find;
1499 File_Name := Get_File_Name (Spec_Name, Subunit => False);
1500
1501 -- Look if File_Name is mentioned in A's sdep list.
1502 -- If not look if the file exists. If it does return True.
1503
1504 for D in
1505 ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep
1506 loop
1507 if Sdep.Table (D).Sfile = File_Name then
1508 return False;
1509 end if;
1510 end loop;
1511
1512 if Full_Source_Name (File_Name) /= No_File then
1513 Spec_File_Name := File_Name;
1514 return True;
1515 end if;
1516
1517 return False;
1518 end New_Spec;
1519
1520 -- Start of processing for First_New_Spec
1521
1522 begin
1523 U_Chk : for U in
1524 ALIs.Table (A).First_Unit .. ALIs.Table (A).Last_Unit
1525 loop
1526 exit U_Chk when Units.Table (U).Utype = Is_Body_Only
1527 and then New_Spec (Units.Table (U).Uname);
1528
1529 for W in Units.Table (U).First_With
1530 ..
1531 Units.Table (U).Last_With
1532 loop
1533 exit U_Chk when
1534 Withs.Table (W).Afile /= No_File
1535 and then New_Spec (Withs.Table (W).Uname);
1536 end loop;
1537 end loop U_Chk;
1538
1539 return Spec_File_Name;
1540 end First_New_Spec;
1541
1542 ---------------------------------
1543 -- Data declarations for Check --
1544 ---------------------------------
1545
1546 Full_Obj_File : File_Name_Type;
1547 -- Full name of the object file corresponding to Lib_File
1548
1549 Lib_Stamp : Time_Stamp_Type;
1550 -- Time stamp of the current ada library file
1551
1552 Obj_Stamp : Time_Stamp_Type;
1553 -- Time stamp of the current object file
1554
1555 Modified_Source : File_Name_Type;
1556 -- The first source in Lib_File whose current time stamp differs
1557 -- from that stored in Lib_File.
1558
1559 New_Spec : File_Name_Type;
1560 -- If Lib_File contains in its W (with) section a body (for a
1561 -- subprogram) for which there exists a spec and the spec did not
1562 -- appear in the Sdep section of Lib_File, New_Spec contains the file
1563 -- name of this new spec.
1564
1565 Source_Name : File_Name_Type;
1566 Text : Text_Buffer_Ptr;
1567
1568 Prev_Switch : String_Access;
1569 -- Previous switch processed
1570
1571 Arg : Arg_Id := Arg_Id'First;
1572 -- Current index in Args.Table for a given unit (init to stop warning)
1573
1574 Switch_Found : Boolean;
1575 -- True if a given switch has been found
1576
1577 ALI_Project : Project_Id;
1578 -- If the ALI file is in the object directory of a project, this is
1579 -- the project id.
1580
1581 -- Start of processing for Check
1582
1583 begin
1584 pragma Assert (Lib_File /= No_File);
1585
1586 -- If ALI file is read-only, temporarily set Check_Object_Consistency to
1587 -- False. We don't care if the object file is not there (presumably a
1588 -- library will be used for linking.)
1589
1590 if Read_Only then
1591 declare
1592 Saved_Check_Object_Consistency : constant Boolean :=
1593 Check_Object_Consistency;
1594 begin
1595 Check_Object_Consistency := False;
1596 Text := Read_Library_Info_From_Full (Full_Lib_File, Lib_File_Attr);
1597 Check_Object_Consistency := Saved_Check_Object_Consistency;
1598 end;
1599
1600 else
1601 Text := Read_Library_Info_From_Full (Full_Lib_File, Lib_File_Attr);
1602 end if;
1603
1604 Full_Obj_File := Full_Object_File_Name;
1605 Lib_Stamp := Current_Library_File_Stamp;
1606 Obj_Stamp := Current_Object_File_Stamp;
1607
1608 if Full_Lib_File = No_File then
1609 Verbose_Msg
1610 (Lib_File,
1611 "being checked ...",
1612 Prefix => " ",
1613 Minimum_Verbosity => Opt.Medium);
1614 else
1615 Verbose_Msg
1616 (Full_Lib_File,
1617 "being checked ...",
1618 Prefix => " ",
1619 Minimum_Verbosity => Opt.Medium);
1620 end if;
1621
1622 ALI := No_ALI_Id;
1623 O_File := Full_Obj_File;
1624 O_Stamp := Obj_Stamp;
1625
1626 if Text = null then
1627 if Full_Lib_File = No_File then
1628 Verbose_Msg (Lib_File, "missing.");
1629
1630 elsif Obj_Stamp (Obj_Stamp'First) = ' ' then
1631 Verbose_Msg (Full_Obj_File, "missing.");
1632
1633 else
1634 Verbose_Msg
1635 (Full_Lib_File, "(" & String (Lib_Stamp) & ") newer than",
1636 Full_Obj_File, "(" & String (Obj_Stamp) & ")");
1637 end if;
1638
1639 else
1640 ALI := Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
1641 Free (Text);
1642
1643 if ALI = No_ALI_Id then
1644 Verbose_Msg (Full_Lib_File, "incorrectly formatted ALI file");
1645 return;
1646
1647 elsif ALIs.Table (ALI).Ver (1 .. ALIs.Table (ALI).Ver_Len) /=
1648 Verbose_Library_Version
1649 then
1650 Verbose_Msg (Full_Lib_File, "compiled with old GNAT version");
1651 ALI := No_ALI_Id;
1652 return;
1653 end if;
1654
1655 -- Don't take Ali file into account if it was generated with
1656 -- errors.
1657
1658 if ALIs.Table (ALI).Compile_Errors then
1659 Verbose_Msg (Full_Lib_File, "had errors, must be recompiled");
1660 ALI := No_ALI_Id;
1661 return;
1662 end if;
1663
1664 -- Don't take Ali file into account if it was generated without
1665 -- object.
1666
1667 if Operating_Mode /= Check_Semantics
1668 and then ALIs.Table (ALI).No_Object
1669 then
1670 Verbose_Msg (Full_Lib_File, "has no corresponding object");
1671 ALI := No_ALI_Id;
1672 return;
1673 end if;
1674
1675 -- Check for matching compiler switches if needed
1676
1677 if Check_Switches then
1678
1679 -- First, collect all the switches
1680
1681 Collect_Arguments
1682 (Source_File, Source_Index, Is_Main_Source, The_Args);
1683
1684 Prev_Switch := Dummy_Switch;
1685
1686 Get_Name_String (ALIs.Table (ALI).Sfile);
1687
1688 Switches_To_Check.Set_Last (0);
1689
1690 for J in 1 .. Last_Argument loop
1691
1692 -- Skip non switches -c, -I and -o switches
1693
1694 if Arguments (J) (1) = '-'
1695 and then Arguments (J) (2) /= 'c'
1696 and then Arguments (J) (2) /= 'o'
1697 and then Arguments (J) (2) /= 'I'
1698 then
1699 Normalize_Compiler_Switches
1700 (Arguments (J).all,
1701 Normalized_Switches,
1702 Last_Norm_Switch);
1703
1704 for K in 1 .. Last_Norm_Switch loop
1705 Switches_To_Check.Increment_Last;
1706 Switches_To_Check.Table (Switches_To_Check.Last) :=
1707 Normalized_Switches (K);
1708 end loop;
1709 end if;
1710 end loop;
1711
1712 for J in 1 .. Switches_To_Check.Last loop
1713
1714 -- Comparing switches is delicate because gcc reorders a number
1715 -- of switches, according to lang-specs.h, but gnatmake doesn't
1716 -- have sufficient knowledge to perform the same reordering.
1717 -- Instead, we ignore orders between different "first letter"
1718 -- switches, but keep orders between same switches, e.g -O -O2
1719 -- is different than -O2 -O, but -g -O is equivalent to -O -g.
1720
1721 if Switches_To_Check.Table (J) (2) /= Prev_Switch (2) or else
1722 (Prev_Switch'Length >= 6 and then
1723 Prev_Switch (2 .. 5) = "gnat" and then
1724 Switches_To_Check.Table (J)'Length >= 6 and then
1725 Switches_To_Check.Table (J) (2 .. 5) = "gnat" and then
1726 Prev_Switch (6) /= Switches_To_Check.Table (J) (6))
1727 then
1728 Prev_Switch := Switches_To_Check.Table (J);
1729 Arg :=
1730 Units.Table (ALIs.Table (ALI).First_Unit).First_Arg;
1731 end if;
1732
1733 Switch_Found := False;
1734
1735 for K in Arg ..
1736 Units.Table (ALIs.Table (ALI).First_Unit).Last_Arg
1737 loop
1738 if
1739 Switches_To_Check.Table (J).all = Args.Table (K).all
1740 then
1741 Arg := K + 1;
1742 Switch_Found := True;
1743 exit;
1744 end if;
1745 end loop;
1746
1747 if not Switch_Found then
1748 if Verbose_Mode then
1749 Verbose_Msg (ALIs.Table (ALI).Sfile,
1750 "switch mismatch """ &
1751 Switches_To_Check.Table (J).all & '"');
1752 end if;
1753
1754 ALI := No_ALI_Id;
1755 return;
1756 end if;
1757 end loop;
1758
1759 if Switches_To_Check.Last /=
1760 Integer (Units.Table (ALIs.Table (ALI).First_Unit).Last_Arg -
1761 Units.Table (ALIs.Table (ALI).First_Unit).First_Arg + 1)
1762 then
1763 if Verbose_Mode then
1764 Verbose_Msg (ALIs.Table (ALI).Sfile,
1765 "different number of switches");
1766
1767 for K in Units.Table (ALIs.Table (ALI).First_Unit).First_Arg
1768 .. Units.Table (ALIs.Table (ALI).First_Unit).Last_Arg
1769 loop
1770 Write_Str (Args.Table (K).all);
1771 Write_Char (' ');
1772 end loop;
1773
1774 Write_Eol;
1775
1776 for J in 1 .. Switches_To_Check.Last loop
1777 Write_Str (Switches_To_Check.Table (J).all);
1778 Write_Char (' ');
1779 end loop;
1780
1781 Write_Eol;
1782 end if;
1783
1784 ALI := No_ALI_Id;
1785 return;
1786 end if;
1787 end if;
1788
1789 -- Get the source files and their message digests. Note that some
1790 -- sources may be missing if ALI is out-of-date.
1791
1792 Set_Source_Table (ALI);
1793
1794 Modified_Source := Time_Stamp_Mismatch (ALI, Read_Only);
1795
1796 -- To avoid using too much memory when switch -m is used, free the
1797 -- memory allocated for the source file when computing the checksum.
1798
1799 if Minimal_Recompilation then
1800 Sinput.P.Clear_Source_File_Table;
1801 end if;
1802
1803 if Modified_Source /= No_File then
1804 ALI := No_ALI_Id;
1805
1806 if Verbose_Mode then
1807 Source_Name := Full_Source_Name (Modified_Source);
1808
1809 if Source_Name /= No_File then
1810 Verbose_Msg (Source_Name, "time stamp mismatch");
1811 else
1812 Verbose_Msg (Modified_Source, "missing");
1813 end if;
1814 end if;
1815
1816 else
1817 New_Spec := First_New_Spec (ALI);
1818
1819 if New_Spec /= No_File then
1820 ALI := No_ALI_Id;
1821
1822 if Verbose_Mode then
1823 Source_Name := Full_Source_Name (New_Spec);
1824
1825 if Source_Name /= No_File then
1826 Verbose_Msg (Source_Name, "new spec");
1827 else
1828 Verbose_Msg (New_Spec, "old spec missing");
1829 end if;
1830 end if;
1831
1832 elsif not Read_Only and then Main_Project /= No_Project then
1833
1834 if not Check_Source_Info_In_ALI (ALI) then
1835 ALI := No_ALI_Id;
1836 return;
1837 end if;
1838
1839 -- Check that the ALI file is in the correct object directory.
1840 -- If it is in the object directory of a project that is
1841 -- extended and it depends on a source that is in one of its
1842 -- extending projects, then the ALI file is not in the correct
1843 -- object directory.
1844
1845 -- First, find the project of this ALI file. As there may be
1846 -- several projects with the same object directory, we first
1847 -- need to find the project of the source.
1848
1849 ALI_Project := No_Project;
1850
1851 declare
1852 Udata : Prj.Unit_Index;
1853
1854 begin
1855 Udata := Units_Htable.Get_First (Project_Tree.Units_HT);
1856 while Udata /= No_Unit_Index loop
1857 if Udata.File_Names (Impl) /= null
1858 and then Udata.File_Names (Impl).File = Source_File
1859 then
1860 ALI_Project := Udata.File_Names (Impl).Project;
1861 exit;
1862
1863 elsif Udata.File_Names (Spec) /= null
1864 and then Udata.File_Names (Spec).File = Source_File
1865 then
1866 ALI_Project := Udata.File_Names (Spec).Project;
1867 exit;
1868 end if;
1869
1870 Udata := Units_Htable.Get_Next (Project_Tree.Units_HT);
1871 end loop;
1872 end;
1873
1874 if ALI_Project = No_Project then
1875 return;
1876 end if;
1877
1878 declare
1879 Obj_Dir : Path_Name_Type;
1880 Res_Obj_Dir : constant String :=
1881 Normalize_Pathname
1882 (Dir_Name
1883 (Get_Name_String (Full_Lib_File)),
1884 Resolve_Links =>
1885 Opt.Follow_Links_For_Dirs,
1886 Case_Sensitive => False);
1887
1888 begin
1889 Name_Len := 0;
1890 Add_Str_To_Name_Buffer (Res_Obj_Dir);
1891
1892 if not Is_Directory_Separator (Name_Buffer (Name_Len)) then
1893 Add_Char_To_Name_Buffer (Directory_Separator);
1894 end if;
1895
1896 Obj_Dir := Name_Find;
1897
1898 while ALI_Project /= No_Project
1899 and then Obj_Dir /= ALI_Project.Object_Directory.Name
1900 loop
1901 ALI_Project := ALI_Project.Extended_By;
1902 end loop;
1903 end;
1904
1905 if ALI_Project = No_Project then
1906 ALI := No_ALI_Id;
1907
1908 Verbose_Msg
1909 (Lib_File, " wrong object directory");
1910 return;
1911 end if;
1912
1913 -- If the ALI project is not extended, then it must be in
1914 -- the correct object directory.
1915
1916 if ALI_Project.Extended_By = No_Project then
1917 return;
1918 end if;
1919
1920 -- Count the extending projects
1921
1922 declare
1923 Num_Ext : Natural;
1924 Proj : Project_Id;
1925
1926 begin
1927 Num_Ext := 0;
1928 Proj := ALI_Project;
1929 loop
1930 Proj := Proj.Extended_By;
1931 exit when Proj = No_Project;
1932 Num_Ext := Num_Ext + 1;
1933 end loop;
1934
1935 -- Make a list of the extending projects
1936
1937 declare
1938 Projects : array (1 .. Num_Ext) of Project_Id;
1939 Dep : Sdep_Record;
1940 OK : Boolean := True;
1941 UID : Unit_Index;
1942
1943 begin
1944 Proj := ALI_Project;
1945 for J in Projects'Range loop
1946 Proj := Proj.Extended_By;
1947 Projects (J) := Proj;
1948 end loop;
1949
1950 -- Now check if any of the dependant sources are in
1951 -- any of these extending projects.
1952
1953 D_Chk :
1954 for D in ALIs.Table (ALI).First_Sdep ..
1955 ALIs.Table (ALI).Last_Sdep
1956 loop
1957 Dep := Sdep.Table (D);
1958 UID := Units_Htable.Get_First (Project_Tree.Units_HT);
1959 Proj := No_Project;
1960
1961 Unit_Loop :
1962 while UID /= null loop
1963 if UID.File_Names (Impl) /= null
1964 and then UID.File_Names (Impl).File = Dep.Sfile
1965 then
1966 Proj := UID.File_Names (Impl).Project;
1967
1968 elsif UID.File_Names (Spec) /= null
1969 and then UID.File_Names (Spec).File = Dep.Sfile
1970 then
1971 Proj := UID.File_Names (Spec).Project;
1972 end if;
1973
1974 -- If a source is in a project, check if it is one
1975 -- in the list.
1976
1977 if Proj /= No_Project then
1978 for J in Projects'Range loop
1979 if Proj = Projects (J) then
1980 OK := False;
1981 exit D_Chk;
1982 end if;
1983 end loop;
1984
1985 exit Unit_Loop;
1986 end if;
1987
1988 UID :=
1989 Units_Htable.Get_Next (Project_Tree.Units_HT);
1990 end loop Unit_Loop;
1991 end loop D_Chk;
1992
1993 -- If one of the dependent sources is in one project of
1994 -- the list, then we must recompile.
1995
1996 if not OK then
1997 ALI := No_ALI_Id;
1998 Verbose_Msg (Lib_File, " wrong object directory");
1999 end if;
2000 end;
2001 end;
2002 end if;
2003 end if;
2004 end if;
2005 end Check;
2006
2007 ------------------------
2008 -- Check_For_S_Switch --
2009 ------------------------
2010
2011 procedure Check_For_S_Switch is
2012 begin
2013 -- By default, we generate an object file
2014
2015 Output_Is_Object := True;
2016
2017 for Arg in 1 .. Last_Argument loop
2018 if Arguments (Arg).all = "-S" then
2019 Output_Is_Object := False;
2020
2021 elsif Arguments (Arg).all = "-c" then
2022 Output_Is_Object := True;
2023 end if;
2024 end loop;
2025 end Check_For_S_Switch;
2026
2027 --------------------------
2028 -- Check_Linker_Options --
2029 --------------------------
2030
2031 procedure Check_Linker_Options
2032 (E_Stamp : Time_Stamp_Type;
2033 O_File : out File_Name_Type;
2034 O_Stamp : out Time_Stamp_Type)
2035 is
2036 procedure Check_File (File : File_Name_Type);
2037 -- Update O_File and O_Stamp if the given file is younger than E_Stamp
2038 -- and O_Stamp, or if O_File is No_File and File does not exist.
2039
2040 function Get_Library_File (Name : String) return File_Name_Type;
2041 -- Return the full file name including path of a library based
2042 -- on the name specified with the -l linker option, using the
2043 -- Ada object path. Return No_File if no such file can be found.
2044
2045 type Char_Array is array (Natural) of Character;
2046 type Char_Array_Access is access constant Char_Array;
2047
2048 Template : Char_Array_Access;
2049 pragma Import (C, Template, "__gnat_library_template");
2050
2051 ----------------
2052 -- Check_File --
2053 ----------------
2054
2055 procedure Check_File (File : File_Name_Type) is
2056 Stamp : Time_Stamp_Type;
2057 Name : File_Name_Type := File;
2058
2059 begin
2060 Get_Name_String (Name);
2061
2062 -- Remove any trailing NUL characters
2063
2064 while Name_Len >= Name_Buffer'First
2065 and then Name_Buffer (Name_Len) = NUL
2066 loop
2067 Name_Len := Name_Len - 1;
2068 end loop;
2069
2070 if Name_Len = 0 then
2071 return;
2072
2073 elsif Name_Buffer (1) = '-' then
2074
2075 -- Do not check if File is a switch other than "-l"
2076
2077 if Name_Buffer (2) /= 'l' then
2078 return;
2079 end if;
2080
2081 -- The argument is a library switch, get actual name. It
2082 -- is necessary to make a copy of the relevant part of
2083 -- Name_Buffer as Get_Library_Name uses Name_Buffer as well.
2084
2085 declare
2086 Base_Name : constant String := Name_Buffer (3 .. Name_Len);
2087
2088 begin
2089 Name := Get_Library_File (Base_Name);
2090 end;
2091
2092 if Name = No_File then
2093 return;
2094 end if;
2095 end if;
2096
2097 Stamp := File_Stamp (Name);
2098
2099 -- Find the youngest object file that is younger than the
2100 -- executable. If no such file exist, record the first object
2101 -- file that is not found.
2102
2103 if (O_Stamp < Stamp and then E_Stamp < Stamp)
2104 or else (O_File = No_File and then Stamp (Stamp'First) = ' ')
2105 then
2106 O_Stamp := Stamp;
2107 O_File := Name;
2108
2109 -- Strip the trailing NUL if present
2110
2111 Get_Name_String (O_File);
2112
2113 if Name_Buffer (Name_Len) = NUL then
2114 Name_Len := Name_Len - 1;
2115 O_File := Name_Find;
2116 end if;
2117 end if;
2118 end Check_File;
2119
2120 ----------------------
2121 -- Get_Library_Name --
2122 ----------------------
2123
2124 -- See comments in a-adaint.c about template syntax
2125
2126 function Get_Library_File (Name : String) return File_Name_Type is
2127 File : File_Name_Type := No_File;
2128
2129 begin
2130 Name_Len := 0;
2131
2132 for Ptr in Template'Range loop
2133 case Template (Ptr) is
2134 when '*' =>
2135 Add_Str_To_Name_Buffer (Name);
2136
2137 when ';' =>
2138 File := Full_Lib_File_Name (Name_Find);
2139 exit when File /= No_File;
2140 Name_Len := 0;
2141
2142 when NUL =>
2143 exit;
2144
2145 when others =>
2146 Add_Char_To_Name_Buffer (Template (Ptr));
2147 end case;
2148 end loop;
2149
2150 -- The for loop exited because the end of the template
2151 -- was reached. File contains the last possible file name
2152 -- for the library.
2153
2154 if File = No_File and then Name_Len > 0 then
2155 File := Full_Lib_File_Name (Name_Find);
2156 end if;
2157
2158 return File;
2159 end Get_Library_File;
2160
2161 -- Start of processing for Check_Linker_Options
2162
2163 begin
2164 O_File := No_File;
2165 O_Stamp := (others => ' ');
2166
2167 -- Process linker options from the ALI files
2168
2169 for Opt in 1 .. Linker_Options.Last loop
2170 Check_File (File_Name_Type (Linker_Options.Table (Opt).Name));
2171 end loop;
2172
2173 -- Process options given on the command line
2174
2175 for Opt in Linker_Switches.First .. Linker_Switches.Last loop
2176
2177 -- Check if the previous Opt has one of the two switches
2178 -- that take an extra parameter. (See GCC manual.)
2179
2180 if Opt = Linker_Switches.First
2181 or else (Linker_Switches.Table (Opt - 1).all /= "-u"
2182 and then
2183 Linker_Switches.Table (Opt - 1).all /= "-Xlinker"
2184 and then
2185 Linker_Switches.Table (Opt - 1).all /= "-L")
2186 then
2187 Name_Len := 0;
2188 Add_Str_To_Name_Buffer (Linker_Switches.Table (Opt).all);
2189 Check_File (Name_Find);
2190 end if;
2191 end loop;
2192
2193 end Check_Linker_Options;
2194
2195 -----------------
2196 -- Check_Steps --
2197 -----------------
2198
2199 procedure Check_Steps is
2200 begin
2201 -- If either -c, -b or -l has been specified, we will not necessarily
2202 -- execute all steps.
2203
2204 if Make_Steps then
2205 Do_Compile_Step := Do_Compile_Step and Compile_Only;
2206 Do_Bind_Step := Do_Bind_Step and Bind_Only;
2207 Do_Link_Step := Do_Link_Step and Link_Only;
2208
2209 -- If -c has been specified, but not -b, ignore any potential -l
2210
2211 if Do_Compile_Step and then not Do_Bind_Step then
2212 Do_Link_Step := False;
2213 end if;
2214 end if;
2215 end Check_Steps;
2216
2217 -----------------------
2218 -- Collect_Arguments --
2219 -----------------------
2220
2221 procedure Collect_Arguments
2222 (Source_File : File_Name_Type;
2223 Source_Index : Int;
2224 Is_Main_Source : Boolean;
2225 Args : Argument_List)
2226 is
2227 begin
2228 Arguments_Project := No_Project;
2229 Last_Argument := 0;
2230 Add_Arguments (Args);
2231
2232 if Main_Project /= No_Project then
2233 declare
2234 Source_File_Name : constant String :=
2235 Get_Name_String (Source_File);
2236 Compiler_Package : Prj.Package_Id;
2237 Switches : Prj.Variable_Value;
2238
2239 begin
2240 Prj.Env.
2241 Get_Reference
2242 (Source_File_Name => Source_File_Name,
2243 Project => Arguments_Project,
2244 Path => Arguments_Path_Name,
2245 In_Tree => Project_Tree);
2246
2247 -- If the source is not a source of a project file, add the
2248 -- recorded arguments. Check will be done later if the source
2249 -- need to be compiled that the switch -x has been used.
2250
2251 if Arguments_Project = No_Project then
2252 Add_Arguments (The_Saved_Gcc_Switches.all);
2253
2254 elsif not Arguments_Project.Externally_Built
2255 or else Must_Compile
2256 then
2257 -- We get the project directory for the relative path
2258 -- switches and arguments.
2259
2260 Arguments_Project :=
2261 Ultimate_Extending_Project_Of (Arguments_Project);
2262
2263 -- If building a dynamic or relocatable library, compile with
2264 -- PIC option, if it exists.
2265
2266 if Arguments_Project.Library
2267 and then Arguments_Project.Library_Kind /= Static
2268 then
2269 declare
2270 PIC : constant String := MLib.Tgt.PIC_Option;
2271 begin
2272 if PIC /= "" then
2273 Add_Arguments ((1 => new String'(PIC)));
2274 end if;
2275 end;
2276 end if;
2277
2278 -- We now look for package Compiler and get the switches from
2279 -- this package.
2280
2281 Compiler_Package :=
2282 Prj.Util.Value_Of
2283 (Name => Name_Compiler,
2284 In_Packages => Arguments_Project.Decl.Packages,
2285 In_Tree => Project_Tree);
2286
2287 if Compiler_Package /= No_Package then
2288
2289 -- If package Gnatmake.Compiler exists, we get the specific
2290 -- switches for the current source, or the global switches,
2291 -- if any.
2292
2293 Switches :=
2294 Switches_Of
2295 (Source_File => Source_File,
2296 Source_File_Name => Source_File_Name,
2297 Source_Index => Source_Index,
2298 Project => Arguments_Project,
2299 In_Package => Compiler_Package,
2300 Allow_ALI => False);
2301
2302 end if;
2303
2304 case Switches.Kind is
2305
2306 -- We have a list of switches. We add these switches,
2307 -- plus the saved gcc switches.
2308
2309 when List =>
2310
2311 declare
2312 Current : String_List_Id := Switches.Values;
2313 Element : String_Element;
2314 Number : Natural := 0;
2315
2316 begin
2317 while Current /= Nil_String loop
2318 Element := Project_Tree.String_Elements.
2319 Table (Current);
2320 Number := Number + 1;
2321 Current := Element.Next;
2322 end loop;
2323
2324 declare
2325 New_Args : Argument_List (1 .. Number);
2326 Last_New : Natural := 0;
2327 Dir_Path : constant String := Get_Name_String
2328 (Arguments_Project.Directory.Name);
2329
2330 begin
2331 Current := Switches.Values;
2332
2333 for Index in New_Args'Range loop
2334 Element := Project_Tree.String_Elements.
2335 Table (Current);
2336 Get_Name_String (Element.Value);
2337
2338 if Name_Len > 0 then
2339 Last_New := Last_New + 1;
2340 New_Args (Last_New) :=
2341 new String'(Name_Buffer (1 .. Name_Len));
2342 Test_If_Relative_Path
2343 (New_Args (Last_New),
2344 Parent => Dir_Path,
2345 Including_Non_Switch => False);
2346 end if;
2347
2348 Current := Element.Next;
2349 end loop;
2350
2351 Add_Arguments
2352 (Configuration_Pragmas_Switch
2353 (Arguments_Project) &
2354 New_Args (1 .. Last_New) &
2355 The_Saved_Gcc_Switches.all);
2356 end;
2357 end;
2358
2359 -- We have a single switch. We add this switch,
2360 -- plus the saved gcc switches.
2361
2362 when Single =>
2363 Get_Name_String (Switches.Value);
2364
2365 declare
2366 New_Args : Argument_List :=
2367 (1 => new String'
2368 (Name_Buffer (1 .. Name_Len)));
2369 Dir_Path : constant String :=
2370 Get_Name_String
2371 (Arguments_Project.Directory.Name);
2372
2373 begin
2374 Test_If_Relative_Path
2375 (New_Args (1),
2376 Parent => Dir_Path,
2377 Including_Non_Switch => False);
2378 Add_Arguments
2379 (Configuration_Pragmas_Switch (Arguments_Project) &
2380 New_Args & The_Saved_Gcc_Switches.all);
2381 end;
2382
2383 -- We have no switches from Gnatmake.Compiler.
2384 -- We add the saved gcc switches.
2385
2386 when Undefined =>
2387 Add_Arguments
2388 (Configuration_Pragmas_Switch (Arguments_Project) &
2389 The_Saved_Gcc_Switches.all);
2390 end case;
2391 end if;
2392 end;
2393 end if;
2394
2395 -- For VMS, when compiling the main source, add switch
2396 -- -mdebug-main=_ada_ so that the executable can be debugged
2397 -- by the standard VMS debugger.
2398
2399 if not No_Main_Subprogram
2400 and then Targparm.OpenVMS_On_Target
2401 and then Is_Main_Source
2402 then
2403 -- First, check if compilation will be invoked with -g
2404
2405 for J in 1 .. Last_Argument loop
2406 if Arguments (J)'Length >= 2
2407 and then Arguments (J) (1 .. 2) = "-g"
2408 and then (Arguments (J)'Length < 5
2409 or else Arguments (J) (1 .. 5) /= "-gnat")
2410 then
2411 Add_Arguments
2412 ((1 => new String'("-mdebug-main=_ada_")));
2413 exit;
2414 end if;
2415 end loop;
2416 end if;
2417
2418 -- Set Output_Is_Object, depending if there is a -S switch.
2419 -- If the bind step is not performed, and there is a -S switch,
2420 -- then we will not check for a valid object file.
2421
2422 Check_For_S_Switch;
2423 end Collect_Arguments;
2424
2425 ---------------------
2426 -- Compile_Sources --
2427 ---------------------
2428
2429 procedure Compile_Sources
2430 (Main_Source : File_Name_Type;
2431 Args : Argument_List;
2432 First_Compiled_File : out File_Name_Type;
2433 Most_Recent_Obj_File : out File_Name_Type;
2434 Most_Recent_Obj_Stamp : out Time_Stamp_Type;
2435 Main_Unit : out Boolean;
2436 Compilation_Failures : out Natural;
2437 Main_Index : Int := 0;
2438 Check_Readonly_Files : Boolean := False;
2439 Do_Not_Execute : Boolean := False;
2440 Force_Compilations : Boolean := False;
2441 Keep_Going : Boolean := False;
2442 In_Place_Mode : Boolean := False;
2443 Initialize_ALI_Data : Boolean := True;
2444 Max_Process : Positive := 1)
2445 is
2446 Mfile : Natural := No_Mapping_File;
2447 Mapping_File_Arg : String_Access;
2448 -- Info on the mapping file
2449
2450 Need_To_Check_Standard_Library : Boolean :=
2451 Check_Readonly_Files
2452 and not Unique_Compile;
2453
2454 procedure Add_Process
2455 (Pid : Process_Id;
2456 Sfile : File_Name_Type;
2457 Afile : File_Name_Type;
2458 Uname : Unit_Name_Type;
2459 Full_Lib_File : File_Name_Type;
2460 Lib_File_Attr : File_Attributes;
2461 Mfile : Natural := No_Mapping_File);
2462 -- Adds process Pid to the current list of outstanding compilation
2463 -- processes and record the full name of the source file Sfile that
2464 -- we are compiling, the name of its library file Afile and the
2465 -- name of its unit Uname. If Mfile is not equal to No_Mapping_File,
2466 -- it is the index of the mapping file used during compilation in the
2467 -- array The_Mapping_File_Names.
2468
2469 procedure Await_Compile
2470 (Data : out Compilation_Data;
2471 OK : out Boolean);
2472 -- Awaits that an outstanding compilation process terminates. When it
2473 -- does set Data to the information registered for the corresponding
2474 -- call to Add_Process. Note that this time stamp can be used to check
2475 -- whether the compilation did generate an object file. OK is set to
2476 -- True if the compilation succeeded. Data could be No_Compilation_Data
2477 -- if there was no compilation to wait for.
2478
2479 function Bad_Compilation_Count return Natural;
2480 -- Returns the number of compilation failures
2481
2482 procedure Check_Standard_Library;
2483 -- Check if s-stalib.adb needs to be compiled
2484
2485 procedure Collect_Arguments_And_Compile
2486 (Full_Source_File : File_Name_Type;
2487 Lib_File : File_Name_Type;
2488 Source_Index : Int;
2489 Pid : out Process_Id;
2490 Process_Created : out Boolean);
2491 -- Collect arguments from project file (if any) and compile. If no
2492 -- compilation was attempted, Processed_Created is set to False, and the
2493 -- value of Pid is unknown.
2494
2495 function Compile
2496 (Project : Project_Id;
2497 S : File_Name_Type;
2498 L : File_Name_Type;
2499 Source_Index : Int;
2500 Args : Argument_List) return Process_Id;
2501 -- Compiles S using Args. If S is a GNAT predefined source "-gnatpg" is
2502 -- added to Args. Non blocking call. L corresponds to the expected
2503 -- library file name. Process_Id of the process spawned to execute the
2504 -- compilation.
2505
2506 package Good_ALI is new Table.Table (
2507 Table_Component_Type => ALI_Id,
2508 Table_Index_Type => Natural,
2509 Table_Low_Bound => 1,
2510 Table_Initial => 50,
2511 Table_Increment => 100,
2512 Table_Name => "Make.Good_ALI");
2513 -- Contains the set of valid ALI files that have not yet been scanned
2514
2515 function Good_ALI_Present return Boolean;
2516 -- Returns True if any ALI file was recorded in the previous set
2517
2518 procedure Get_Mapping_File (Project : Project_Id);
2519 -- Get a mapping file name. If there is one to be reused, reuse it.
2520 -- Otherwise, create a new mapping file.
2521
2522 function Get_Next_Good_ALI return ALI_Id;
2523 -- Returns the next good ALI_Id record
2524
2525 procedure Record_Failure
2526 (File : File_Name_Type;
2527 Unit : Unit_Name_Type;
2528 Found : Boolean := True);
2529 -- Records in the previous table that the compilation for File failed.
2530 -- If Found is False then the compilation of File failed because we
2531 -- could not find it. Records also Unit when possible.
2532
2533 procedure Record_Good_ALI (A : ALI_Id);
2534 -- Records in the previous set the Id of an ALI file
2535
2536 function Must_Exit_Because_Of_Error return Boolean;
2537 -- Return True if there were errors and the user decided to exit in such
2538 -- a case. This waits for any outstanding compilation.
2539
2540 function Start_Compile_If_Possible (Args : Argument_List) return Boolean;
2541 -- Check if there is more work that we can do (i.e. the Queue is non
2542 -- empty). If there is, do it only if we have not yet used up all the
2543 -- available processes.
2544 -- Returns True if we should exit the main loop
2545
2546 procedure Wait_For_Available_Slot;
2547 -- Check if we should wait for a compilation to finish. This is the case
2548 -- if all the available processes are busy compiling sources or there is
2549 -- nothing else to do (that is the Q is empty and there are no good ALIs
2550 -- to process).
2551
2552 procedure Fill_Queue_From_ALI_Files;
2553 -- Check if we recorded good ALI files. If yes process them now in the
2554 -- order in which they have been recorded. There are two occasions in
2555 -- which we record good ali files. The first is in phase 1 when, after
2556 -- scanning an existing ALI file we realize it is up-to-date, the second
2557 -- instance is after a successful compilation.
2558
2559 -----------------
2560 -- Add_Process --
2561 -----------------
2562
2563 procedure Add_Process
2564 (Pid : Process_Id;
2565 Sfile : File_Name_Type;
2566 Afile : File_Name_Type;
2567 Uname : Unit_Name_Type;
2568 Full_Lib_File : File_Name_Type;
2569 Lib_File_Attr : File_Attributes;
2570 Mfile : Natural := No_Mapping_File)
2571 is
2572 OC1 : constant Positive := Outstanding_Compiles + 1;
2573
2574 begin
2575 pragma Assert (OC1 <= Max_Process);
2576 pragma Assert (Pid /= Invalid_Pid);
2577
2578 Running_Compile (OC1) :=
2579 (Pid => Pid,
2580 Full_Source_File => Sfile,
2581 Lib_File => Afile,
2582 Full_Lib_File => Full_Lib_File,
2583 Lib_File_Attr => Lib_File_Attr,
2584 Source_Unit => Uname,
2585 Mapping_File => Mfile,
2586 Project => Arguments_Project);
2587
2588 Outstanding_Compiles := OC1;
2589 end Add_Process;
2590
2591 --------------------
2592 -- Await_Compile --
2593 -------------------
2594
2595 procedure Await_Compile
2596 (Data : out Compilation_Data;
2597 OK : out Boolean)
2598 is
2599 Pid : Process_Id;
2600 Project : Project_Id;
2601 Comp_Data : Project_Compilation_Access;
2602
2603 begin
2604 pragma Assert (Outstanding_Compiles > 0);
2605
2606 Data := No_Compilation_Data;
2607 OK := False;
2608
2609 -- The loop here is a work-around for a problem on VMS; in some
2610 -- circumstances (shared library and several executables, for
2611 -- example), there are child processes other than compilation
2612 -- processes that are received. Until this problem is resolved,
2613 -- we will ignore such processes.
2614
2615 loop
2616 Wait_Process (Pid, OK);
2617
2618 if Pid = Invalid_Pid then
2619 return;
2620 end if;
2621
2622 for J in Running_Compile'First .. Outstanding_Compiles loop
2623 if Pid = Running_Compile (J).Pid then
2624 Data := Running_Compile (J);
2625 Project := Running_Compile (J).Project;
2626
2627 -- If a mapping file was used by this compilation, get its
2628 -- file name for reuse by a subsequent compilation.
2629
2630 if Running_Compile (J).Mapping_File /= No_Mapping_File then
2631 Comp_Data :=
2632 Project_Compilation_Htable.Get
2633 (Project_Compilation, Project);
2634 Comp_Data.Last_Free_Indices :=
2635 Comp_Data.Last_Free_Indices + 1;
2636 Comp_Data.Free_Mapping_File_Indices
2637 (Comp_Data.Last_Free_Indices) :=
2638 Running_Compile (J).Mapping_File;
2639 end if;
2640
2641 -- To actually remove this Pid and related info from
2642 -- Running_Compile replace its entry with the last valid
2643 -- entry in Running_Compile.
2644
2645 if J = Outstanding_Compiles then
2646 null;
2647 else
2648 Running_Compile (J) :=
2649 Running_Compile (Outstanding_Compiles);
2650 end if;
2651
2652 Outstanding_Compiles := Outstanding_Compiles - 1;
2653 return;
2654 end if;
2655 end loop;
2656
2657 -- This child process was not one of our compilation processes;
2658 -- just ignore it for now.
2659
2660 -- Why is this commented out code sitting here???
2661
2662 -- raise Program_Error;
2663 end loop;
2664 end Await_Compile;
2665
2666 ---------------------------
2667 -- Bad_Compilation_Count --
2668 ---------------------------
2669
2670 function Bad_Compilation_Count return Natural is
2671 begin
2672 return Bad_Compilation.Last - Bad_Compilation.First + 1;
2673 end Bad_Compilation_Count;
2674
2675 ----------------------------
2676 -- Check_Standard_Library --
2677 ----------------------------
2678
2679 procedure Check_Standard_Library is
2680 begin
2681 Need_To_Check_Standard_Library := False;
2682
2683 if not Targparm.Suppress_Standard_Library_On_Target then
2684 declare
2685 Sfile : File_Name_Type;
2686 Add_It : Boolean := True;
2687
2688 begin
2689 Name_Len := 0;
2690 Add_Str_To_Name_Buffer (Standard_Library_Package_Body_Name);
2691 Sfile := Name_Enter;
2692
2693 -- If we have a special runtime, we add the standard
2694 -- library only if we can find it.
2695
2696 if RTS_Switch then
2697 Add_It := Full_Source_Name (Sfile) /= No_File;
2698 end if;
2699
2700 if Add_It then
2701 if Is_Marked (Sfile) then
2702 if Is_In_Obsoleted (Sfile) then
2703 Executable_Obsolete := True;
2704 end if;
2705
2706 else
2707 Insert_Q (Sfile, Index => 0);
2708 Mark (Sfile, Index => 0);
2709 end if;
2710 end if;
2711 end;
2712 end if;
2713 end Check_Standard_Library;
2714
2715 -----------------------------------
2716 -- Collect_Arguments_And_Compile --
2717 -----------------------------------
2718
2719 procedure Collect_Arguments_And_Compile
2720 (Full_Source_File : File_Name_Type;
2721 Lib_File : File_Name_Type;
2722 Source_Index : Int;
2723 Pid : out Process_Id;
2724 Process_Created : out Boolean) is
2725 begin
2726 Process_Created := False;
2727
2728 -- If we use mapping file (-P or -C switches), then get one
2729
2730 if Create_Mapping_File then
2731 Get_Mapping_File (Arguments_Project);
2732 end if;
2733
2734 -- If the source is part of a project file, we set the ADA_*_PATHs,
2735 -- check for an eventual library project, and use the full path.
2736
2737 if Arguments_Project /= No_Project then
2738 if not Arguments_Project.Externally_Built
2739 or else Must_Compile
2740 then
2741 Prj.Env.Set_Ada_Paths
2742 (Arguments_Project,
2743 Project_Tree,
2744 Including_Libraries => True,
2745 Include_Path => Use_Include_Path_File);
2746
2747 if not Unique_Compile
2748 and then MLib.Tgt.Support_For_Libraries /= Prj.None
2749 then
2750 declare
2751 Prj : constant Project_Id :=
2752 Ultimate_Extending_Project_Of (Arguments_Project);
2753
2754 begin
2755 if Prj.Library
2756 and then (not Prj.Externally_Built or else Must_Compile)
2757 and then not Prj.Need_To_Build_Lib
2758 then
2759 -- Add to the Q all sources of the project that have
2760 -- not been marked.
2761
2762 Insert_Project_Sources
2763 (The_Project => Prj,
2764 All_Projects => False,
2765 Into_Q => True);
2766
2767 -- Now mark the project as processed
2768
2769 Prj.Need_To_Build_Lib := True;
2770 end if;
2771 end;
2772 end if;
2773
2774 Pid :=
2775 Compile
2776 (Project => Arguments_Project,
2777 S => File_Name_Type (Arguments_Path_Name),
2778 L => Lib_File,
2779 Source_Index => Source_Index,
2780 Args => Arguments (1 .. Last_Argument));
2781 Process_Created := True;
2782 end if;
2783
2784 else
2785 -- If this is a source outside of any project file, make sure it
2786 -- will be compiled in object directory of the main project file.
2787
2788 Pid :=
2789 Compile
2790 (Project => Main_Project,
2791 S => Full_Source_File,
2792 L => Lib_File,
2793 Source_Index => Source_Index,
2794 Args => Arguments (1 .. Last_Argument));
2795 Process_Created := True;
2796 end if;
2797 end Collect_Arguments_And_Compile;
2798
2799 -------------
2800 -- Compile --
2801 -------------
2802
2803 function Compile
2804 (Project : Project_Id;
2805 S : File_Name_Type;
2806 L : File_Name_Type;
2807 Source_Index : Int;
2808 Args : Argument_List) return Process_Id
2809 is
2810 Comp_Args : Argument_List (Args'First .. Args'Last + 10);
2811 Comp_Next : Integer := Args'First;
2812 Comp_Last : Integer;
2813 Arg_Index : Integer;
2814
2815 function Ada_File_Name (Name : File_Name_Type) return Boolean;
2816 -- Returns True if Name is the name of an ada source file
2817 -- (i.e. suffix is .ads or .adb)
2818
2819 -------------------
2820 -- Ada_File_Name --
2821 -------------------
2822
2823 function Ada_File_Name (Name : File_Name_Type) return Boolean is
2824 begin
2825 Get_Name_String (Name);
2826 return
2827 Name_Len > 4
2828 and then Name_Buffer (Name_Len - 3 .. Name_Len - 1) = ".ad"
2829 and then (Name_Buffer (Name_Len) = 'b'
2830 or else
2831 Name_Buffer (Name_Len) = 's');
2832 end Ada_File_Name;
2833
2834 -- Start of processing for Compile
2835
2836 begin
2837 Enter_Into_Obsoleted (S);
2838
2839 -- By default, Syntax_Only is False
2840
2841 Syntax_Only := False;
2842
2843 for J in Args'Range loop
2844 if Args (J).all = "-gnats" then
2845
2846 -- If we compile with -gnats, the bind step and the link step
2847 -- are inhibited. Also, we set Syntax_Only to True, so that
2848 -- we don't fail when we don't find the ALI file, after
2849 -- compilation.
2850
2851 Do_Bind_Step := False;
2852 Do_Link_Step := False;
2853 Syntax_Only := True;
2854
2855 elsif Args (J).all = "-gnatc" then
2856
2857 -- If we compile with -gnatc, the bind step and the link step
2858 -- are inhibited. We set Syntax_Only to False for the case when
2859 -- -gnats was previously specified.
2860
2861 Do_Bind_Step := False;
2862 Do_Link_Step := False;
2863 Syntax_Only := False;
2864 end if;
2865 end loop;
2866
2867 Comp_Args (Comp_Next) := new String'("-gnatea");
2868 Comp_Next := Comp_Next + 1;
2869
2870 Comp_Args (Comp_Next) := Comp_Flag;
2871 Comp_Next := Comp_Next + 1;
2872
2873 -- Optimize the simple case where the gcc command line looks like
2874 -- gcc -c -I. ... -I- file.adb
2875 -- into
2876 -- gcc -c ... file.adb
2877
2878 if Args (Args'First).all = "-I" & Normalized_CWD
2879 and then Args (Args'Last).all = "-I-"
2880 and then S = Strip_Directory (S)
2881 then
2882 Comp_Last := Comp_Next + Args'Length - 3;
2883 Arg_Index := Args'First + 1;
2884
2885 else
2886 Comp_Last := Comp_Next + Args'Length - 1;
2887 Arg_Index := Args'First;
2888 end if;
2889
2890 -- Make a deep copy of the arguments, because Normalize_Arguments
2891 -- may deallocate some arguments.
2892
2893 for J in Comp_Next .. Comp_Last loop
2894 Comp_Args (J) := new String'(Args (Arg_Index).all);
2895 Arg_Index := Arg_Index + 1;
2896 end loop;
2897
2898 -- Set -gnatpg for predefined files (for this purpose the renamings
2899 -- such as Text_IO do not count as predefined). Note that we strip
2900 -- the directory name from the source file name because the call to
2901 -- Fname.Is_Predefined_File_Name cannot deal with directory prefixes.
2902
2903 declare
2904 Fname : constant File_Name_Type := Strip_Directory (S);
2905
2906 begin
2907 if Is_Predefined_File_Name (Fname, False) then
2908 if Check_Readonly_Files then
2909 Comp_Args (Comp_Args'First + 2 .. Comp_Last + 1) :=
2910 Comp_Args (Comp_Args'First + 1 .. Comp_Last);
2911 Comp_Last := Comp_Last + 1;
2912 Comp_Args (Comp_Args'First + 1) := GNAT_Flag;
2913
2914 else
2915 Make_Failed
2916 ("not allowed to compile """ &
2917 Get_Name_String (Fname) &
2918 """; use -a switch, or compile file with " &
2919 """-gnatg"" switch");
2920 end if;
2921 end if;
2922 end;
2923
2924 -- Now check if the file name has one of the suffixes familiar to
2925 -- the gcc driver. If this is not the case then add the ada flag
2926 -- "-x ada".
2927
2928 if not Ada_File_Name (S) and then not Targparm.AAMP_On_Target then
2929 Comp_Last := Comp_Last + 1;
2930 Comp_Args (Comp_Last) := Ada_Flag_1;
2931 Comp_Last := Comp_Last + 1;
2932 Comp_Args (Comp_Last) := Ada_Flag_2;
2933 end if;
2934
2935 if Source_Index /= 0 then
2936 declare
2937 Num : constant String := Source_Index'Img;
2938 begin
2939 Comp_Last := Comp_Last + 1;
2940 Comp_Args (Comp_Last) :=
2941 new String'("-gnateI" & Num (Num'First + 1 .. Num'Last));
2942 end;
2943 end if;
2944
2945 if Source_Index /= 0
2946 or else L /= Strip_Directory (L)
2947 or else Object_Directory_Path /= null
2948 then
2949 -- Build -o argument
2950
2951 Get_Name_String (L);
2952
2953 for J in reverse 1 .. Name_Len loop
2954 if Name_Buffer (J) = '.' then
2955 Name_Len := J + Object_Suffix'Length - 1;
2956 Name_Buffer (J .. Name_Len) := Object_Suffix;
2957 exit;
2958 end if;
2959 end loop;
2960
2961 Comp_Last := Comp_Last + 1;
2962 Comp_Args (Comp_Last) := Output_Flag;
2963 Comp_Last := Comp_Last + 1;
2964
2965 -- If an object directory was specified, prepend the object file
2966 -- name with this object directory.
2967
2968 if Object_Directory_Path /= null then
2969 Comp_Args (Comp_Last) :=
2970 new String'(Object_Directory_Path.all &
2971 Name_Buffer (1 .. Name_Len));
2972
2973 else
2974 Comp_Args (Comp_Last) :=
2975 new String'(Name_Buffer (1 .. Name_Len));
2976 end if;
2977 end if;
2978
2979 if Create_Mapping_File and then Mapping_File_Arg /= null then
2980 Comp_Last := Comp_Last + 1;
2981 Comp_Args (Comp_Last) := new String'(Mapping_File_Arg.all);
2982 end if;
2983
2984 Get_Name_String (S);
2985
2986 Comp_Last := Comp_Last + 1;
2987 Comp_Args (Comp_Last) := new String'(Name_Buffer (1 .. Name_Len));
2988
2989 -- Change to object directory of the project file, if necessary
2990
2991 if Project /= No_Project then
2992 Change_To_Object_Directory (Project);
2993 end if;
2994
2995 GNAT.OS_Lib.Normalize_Arguments (Comp_Args (Args'First .. Comp_Last));
2996
2997 Comp_Last := Comp_Last + 1;
2998 Comp_Args (Comp_Last) := new String'("-gnatez");
2999
3000 Display (Gcc.all, Comp_Args (Args'First .. Comp_Last));
3001
3002 if Gcc_Path = null then
3003 Make_Failed ("error, unable to locate " & Gcc.all);
3004 end if;
3005
3006 return
3007 GNAT.OS_Lib.Non_Blocking_Spawn
3008 (Gcc_Path.all, Comp_Args (Args'First .. Comp_Last));
3009 end Compile;
3010
3011 -------------------------------
3012 -- Fill_Queue_From_ALI_Files --
3013 -------------------------------
3014
3015 procedure Fill_Queue_From_ALI_Files is
3016 ALI : ALI_Id;
3017 Source_Index : Int;
3018 Sfile : File_Name_Type;
3019 Uname : Unit_Name_Type;
3020 Unit_Name : Name_Id;
3021 Uid : Prj.Unit_Index;
3022
3023 begin
3024 while Good_ALI_Present loop
3025 ALI := Get_Next_Good_ALI;
3026 Source_Index := Unit_Index_Of (ALIs.Table (ALI).Afile);
3027
3028 -- If we are processing the library file corresponding to the
3029 -- main source file check if this source can be a main unit.
3030
3031 if ALIs.Table (ALI).Sfile = Main_Source
3032 and then Source_Index = Main_Index
3033 then
3034 Main_Unit := ALIs.Table (ALI).Main_Program /= None;
3035 end if;
3036
3037 -- The following adds the standard library (s-stalib) to the list
3038 -- of files to be handled by gnatmake: this file and any files it
3039 -- depends on are always included in every bind, even if they are
3040 -- not in the explicit dependency list. Of course, it is not added
3041 -- if Suppress_Standard_Library is True.
3042
3043 -- However, to avoid annoying output about s-stalib.ali being read
3044 -- only, when "-v" is used, we add the standard library only when
3045 -- "-a" is used.
3046
3047 if Need_To_Check_Standard_Library then
3048 Check_Standard_Library;
3049 end if;
3050
3051 -- Now insert in the Q the unmarked source files (i.e. those which
3052 -- have never been inserted in the Q and hence never considered).
3053 -- Only do that if Unique_Compile is False.
3054
3055 if not Unique_Compile then
3056 for J in
3057 ALIs.Table (ALI).First_Unit .. ALIs.Table (ALI).Last_Unit
3058 loop
3059 for K in
3060 Units.Table (J).First_With .. Units.Table (J).Last_With
3061 loop
3062 Sfile := Withs.Table (K).Sfile;
3063 Uname := Withs.Table (K).Uname;
3064
3065 -- If project files are used, find the proper source to
3066 -- compile in case Sfile is the spec but there is a body.
3067
3068 if Main_Project /= No_Project then
3069 Get_Name_String (Uname);
3070 Name_Len := Name_Len - 2;
3071 Unit_Name := Name_Find;
3072 Uid :=
3073 Units_Htable.Get (Project_Tree.Units_HT, Unit_Name);
3074
3075 if Uid /= Prj.No_Unit_Index then
3076 if Uid.File_Names (Impl) /= null
3077 and then not Uid.File_Names (Impl).Locally_Removed
3078 then
3079 Sfile := Uid.File_Names (Impl).File;
3080 Source_Index := Uid.File_Names (Impl).Index;
3081
3082 elsif Uid.File_Names (Spec) /= null
3083 and then not Uid.File_Names (Spec).Locally_Removed
3084 then
3085 Sfile := Uid.File_Names (Spec).File;
3086 Source_Index := Uid.File_Names (Spec).Index;
3087 end if;
3088 end if;
3089 end if;
3090
3091 Dependencies.Append ((ALIs.Table (ALI).Sfile, Sfile));
3092
3093 if Is_In_Obsoleted (Sfile) then
3094 Executable_Obsolete := True;
3095 end if;
3096
3097 if Sfile = No_File then
3098 Debug_Msg ("Skipping generic:", Withs.Table (K).Uname);
3099
3100 else
3101 Source_Index := Unit_Index_Of (Withs.Table (K).Afile);
3102
3103 if Is_Marked (Sfile, Source_Index) then
3104 Debug_Msg ("Skipping marked file:", Sfile);
3105
3106 elsif not Check_Readonly_Files
3107 and then Is_Internal_File_Name (Sfile, False)
3108 then
3109 Debug_Msg ("Skipping internal file:", Sfile);
3110
3111 else
3112 Insert_Q
3113 (Sfile, Withs.Table (K).Uname, Source_Index);
3114 Mark (Sfile, Source_Index);
3115 end if;
3116 end if;
3117 end loop;
3118 end loop;
3119 end if;
3120 end loop;
3121 end Fill_Queue_From_ALI_Files;
3122
3123 ----------------------
3124 -- Get_Mapping_File --
3125 ----------------------
3126
3127 procedure Get_Mapping_File (Project : Project_Id) is
3128 Data : Project_Compilation_Access;
3129
3130 begin
3131 Data := Project_Compilation_Htable.Get (Project_Compilation, Project);
3132
3133 -- If there is a mapping file ready to be reused, reuse it
3134
3135 if Data.Last_Free_Indices > 0 then
3136 Mfile := Data.Free_Mapping_File_Indices (Data.Last_Free_Indices);
3137 Data.Last_Free_Indices := Data.Last_Free_Indices - 1;
3138
3139 -- Otherwise, create and initialize a new one
3140
3141 else
3142 Init_Mapping_File
3143 (Project => Project, Data => Data.all, File_Index => Mfile);
3144 end if;
3145
3146 -- Put the name in the mapping file argument for the invocation
3147 -- of the compiler.
3148
3149 Free (Mapping_File_Arg);
3150 Mapping_File_Arg :=
3151 new String'("-gnatem=" &
3152 Get_Name_String (Data.Mapping_File_Names (Mfile)));
3153 end Get_Mapping_File;
3154
3155 -----------------------
3156 -- Get_Next_Good_ALI --
3157 -----------------------
3158
3159 function Get_Next_Good_ALI return ALI_Id is
3160 ALI : ALI_Id;
3161
3162 begin
3163 pragma Assert (Good_ALI_Present);
3164 ALI := Good_ALI.Table (Good_ALI.Last);
3165 Good_ALI.Decrement_Last;
3166 return ALI;
3167 end Get_Next_Good_ALI;
3168
3169 ----------------------
3170 -- Good_ALI_Present --
3171 ----------------------
3172
3173 function Good_ALI_Present return Boolean is
3174 begin
3175 return Good_ALI.First <= Good_ALI.Last;
3176 end Good_ALI_Present;
3177
3178 --------------------------------
3179 -- Must_Exit_Because_Of_Error --
3180 --------------------------------
3181
3182 function Must_Exit_Because_Of_Error return Boolean is
3183 Data : Compilation_Data;
3184 Success : Boolean;
3185
3186 begin
3187 if Bad_Compilation_Count > 0 and then not Keep_Going then
3188 while Outstanding_Compiles > 0 loop
3189 Await_Compile (Data, Success);
3190
3191 if not Success then
3192 Record_Failure (Data.Full_Source_File, Data.Source_Unit);
3193 end if;
3194 end loop;
3195
3196 return True;
3197 end if;
3198
3199 return False;
3200 end Must_Exit_Because_Of_Error;
3201
3202 --------------------
3203 -- Record_Failure --
3204 --------------------
3205
3206 procedure Record_Failure
3207 (File : File_Name_Type;
3208 Unit : Unit_Name_Type;
3209 Found : Boolean := True)
3210 is
3211 begin
3212 Bad_Compilation.Increment_Last;
3213 Bad_Compilation.Table (Bad_Compilation.Last) := (File, Unit, Found);
3214 end Record_Failure;
3215
3216 ---------------------
3217 -- Record_Good_ALI --
3218 ---------------------
3219
3220 procedure Record_Good_ALI (A : ALI_Id) is
3221 begin
3222 Good_ALI.Increment_Last;
3223 Good_ALI.Table (Good_ALI.Last) := A;
3224 end Record_Good_ALI;
3225
3226 -------------------------------
3227 -- Start_Compile_If_Possible --
3228 -------------------------------
3229
3230 function Start_Compile_If_Possible
3231 (Args : Argument_List) return Boolean
3232 is
3233 In_Lib_Dir : Boolean;
3234 Need_To_Compile : Boolean;
3235 Pid : Process_Id;
3236 Process_Created : Boolean;
3237
3238 Source_File : File_Name_Type;
3239 Full_Source_File : File_Name_Type;
3240 Source_File_Attr : aliased File_Attributes;
3241 -- The full name of the source file and its attributes (size, ...)
3242
3243 Source_Unit : Unit_Name_Type;
3244 Source_Index : Int;
3245 -- Index of the current unit in the current source file
3246
3247 Lib_File : File_Name_Type;
3248 Full_Lib_File : File_Name_Type;
3249 Lib_File_Attr : aliased File_Attributes;
3250 Read_Only : Boolean := False;
3251 ALI : ALI_Id;
3252 -- The ALI file and its attributes (size, stamp, ...)
3253
3254 Obj_File : File_Name_Type;
3255 Obj_Stamp : Time_Stamp_Type;
3256 -- The object file
3257
3258 begin
3259 if not Empty_Q and then Outstanding_Compiles < Max_Process then
3260 Extract_From_Q (Source_File, Source_Unit, Source_Index);
3261
3262 Osint.Full_Source_Name
3263 (Source_File,
3264 Full_File => Full_Source_File,
3265 Attr => Source_File_Attr'Access);
3266
3267 Lib_File := Osint.Lib_File_Name (Source_File, Source_Index);
3268
3269 -- ??? This call could be avoided when using projects, since we
3270 -- know where the ALI file is supposed to be. That would avoid
3271 -- searches in the object directories, including in the runtime
3272 -- dir. However, that would require getting access to the
3273 -- Source_Id.
3274
3275 Osint.Full_Lib_File_Name
3276 (Lib_File,
3277 Lib_File => Full_Lib_File,
3278 Attr => Lib_File_Attr);
3279
3280 -- If source has already been compiled, executable is obsolete
3281
3282 if Is_In_Obsoleted (Source_File) then
3283 Executable_Obsolete := True;
3284 end if;
3285
3286 In_Lib_Dir := not Check_Readonly_Files
3287 and then Full_Lib_File /= No_File
3288 and then In_Ada_Lib_Dir (Full_Lib_File);
3289
3290 -- Since the following requires a system call, we precompute it
3291 -- when needed.
3292
3293 if not In_Lib_Dir then
3294 if Full_Lib_File /= No_File
3295 and then not Check_Readonly_Files
3296 then
3297 Get_Name_String (Full_Lib_File);
3298 Name_Buffer (Name_Len + 1) := ASCII.NUL;
3299 Read_Only := not Is_Writable_File
3300 (Name_Buffer'Address, Lib_File_Attr'Access);
3301 else
3302 Read_Only := False;
3303 end if;
3304 end if;
3305
3306 -- If the library file is an Ada library skip it
3307
3308 if In_Lib_Dir then
3309 Verbose_Msg
3310 (Lib_File,
3311 "is in an Ada library",
3312 Prefix => " ",
3313 Minimum_Verbosity => Opt.High);
3314
3315 -- If the library file is a read-only library skip it, but only
3316 -- if, when using project files, this library file is in the
3317 -- right object directory (a read-only ALI file in the object
3318 -- directory of a project being extended must not be skipped).
3319
3320 elsif Read_Only
3321 and then Is_In_Object_Directory (Source_File, Full_Lib_File)
3322 then
3323 Verbose_Msg
3324 (Lib_File,
3325 "is a read-only library",
3326 Prefix => " ",
3327 Minimum_Verbosity => Opt.High);
3328
3329 -- The source file that we are checking cannot be located
3330
3331 elsif Full_Source_File = No_File then
3332 Record_Failure (Source_File, Source_Unit, False);
3333
3334 -- Source and library files can be located but are internal
3335 -- files.
3336
3337 elsif not Check_Readonly_Files
3338 and then Full_Lib_File /= No_File
3339 and then Is_Internal_File_Name (Source_File, False)
3340 then
3341 if Force_Compilations then
3342 Fail
3343 ("not allowed to compile """ &
3344 Get_Name_String (Source_File) &
3345 """; use -a switch, or compile file with " &
3346 """-gnatg"" switch");
3347 end if;
3348
3349 Verbose_Msg
3350 (Lib_File,
3351 "is an internal library",
3352 Prefix => " ",
3353 Minimum_Verbosity => Opt.High);
3354
3355 -- The source file that we are checking can be located
3356
3357 else
3358 Collect_Arguments (Source_File, Source_Index,
3359 Source_File = Main_Source, Args);
3360
3361 -- Do nothing if project of source is externally built
3362
3363 if Arguments_Project = No_Project
3364 or else not Arguments_Project.Externally_Built
3365 or else Must_Compile
3366 then
3367 -- Don't waste any time if we have to recompile anyway
3368
3369 Obj_Stamp := Empty_Time_Stamp;
3370 Need_To_Compile := Force_Compilations;
3371
3372 if not Force_Compilations then
3373 Check (Source_File => Source_File,
3374 Source_Index => Source_Index,
3375 Is_Main_Source => Source_File = Main_Source,
3376 The_Args => Args,
3377 Lib_File => Lib_File,
3378 Full_Lib_File => Full_Lib_File,
3379 Lib_File_Attr => Lib_File_Attr'Access,
3380 Read_Only => Read_Only,
3381 ALI => ALI,
3382 O_File => Obj_File,
3383 O_Stamp => Obj_Stamp);
3384 Need_To_Compile := (ALI = No_ALI_Id);
3385 end if;
3386
3387 if not Need_To_Compile then
3388
3389 -- The ALI file is up-to-date; record its Id
3390
3391 Record_Good_ALI (ALI);
3392
3393 -- Record the time stamp of the most recent object
3394 -- file as long as no (re)compilations are needed.
3395
3396 if First_Compiled_File = No_File
3397 and then (Most_Recent_Obj_File = No_File
3398 or else Obj_Stamp > Most_Recent_Obj_Stamp)
3399 then
3400 Most_Recent_Obj_File := Obj_File;
3401 Most_Recent_Obj_Stamp := Obj_Stamp;
3402 end if;
3403
3404 else
3405 -- Check that switch -x has been used if a source outside
3406 -- of project files need to be compiled.
3407
3408 if Main_Project /= No_Project
3409 and then Arguments_Project = No_Project
3410 and then not External_Unit_Compilation_Allowed
3411 then
3412 Make_Failed ("external source ("
3413 & Get_Name_String (Source_File)
3414 & ") is not part of any project;"
3415 & " cannot be compiled without"
3416 & " gnatmake switch -x");
3417 end if;
3418
3419 -- Is this the first file we have to compile?
3420
3421 if First_Compiled_File = No_File then
3422 First_Compiled_File := Full_Source_File;
3423 Most_Recent_Obj_File := No_File;
3424
3425 if Do_Not_Execute then
3426
3427 -- Exit the main loop
3428
3429 return True;
3430 end if;
3431 end if;
3432
3433 -- Compute where the ALI file must be generated in
3434 -- In_Place_Mode (this does not require to know the
3435 -- location of the object directory).
3436
3437 if In_Place_Mode then
3438 if Full_Lib_File = No_File then
3439
3440 -- If the library file was not found, then save
3441 -- the library file near the source file.
3442
3443 Lib_File :=
3444 Osint.Lib_File_Name
3445 (Full_Source_File, Source_Index);
3446 Full_Lib_File := Lib_File;
3447
3448 else
3449 -- If the library file was found, then save the
3450 -- library file in the same place.
3451
3452 Lib_File := Full_Lib_File;
3453 end if;
3454 end if;
3455
3456 -- Start the compilation and record it. We can do this
3457 -- because there is at least one free process. This might
3458 -- change the current directory.
3459
3460 Collect_Arguments_And_Compile
3461 (Full_Source_File => Full_Source_File,
3462 Lib_File => Lib_File,
3463 Source_Index => Source_Index,
3464 Pid => Pid,
3465 Process_Created => Process_Created);
3466
3467 -- Compute where the ALI file will be generated (for
3468 -- cases that might require to know the current
3469 -- directory). The current directory might be changed
3470 -- when compiling other files so we cannot rely on it
3471 -- being the same to find the resulting ALI file.
3472
3473 if not In_Place_Mode then
3474
3475 -- Compute the expected location of the ALI file. This
3476 -- can be from several places:
3477 -- -i => in place mode. In such a case,
3478 -- Full_Lib_File has already been set above
3479 -- -D => if specified
3480 -- or defaults in current dir
3481 -- We could simply use a call similar to
3482 -- Osint.Full_Lib_File_Name (Lib_File)
3483 -- but that involves system calls and is thus slower
3484
3485 if Object_Directory_Path /= null then
3486 Name_Len := 0;
3487 Add_Str_To_Name_Buffer (Object_Directory_Path.all);
3488 Add_Str_To_Name_Buffer (Get_Name_String (Lib_File));
3489 Full_Lib_File := Name_Find;
3490
3491 else
3492 if Project_Of_Current_Object_Directory /=
3493 No_Project
3494 then
3495 Get_Name_String
3496 (Project_Of_Current_Object_Directory
3497 .Object_Directory.Name);
3498 Add_Str_To_Name_Buffer
3499 (Get_Name_String (Lib_File));
3500 Full_Lib_File := Name_Find;
3501
3502 else
3503 Full_Lib_File := Lib_File;
3504 end if;
3505 end if;
3506
3507 end if;
3508
3509 Lib_File_Attr := Unknown_Attributes;
3510
3511 -- Make sure we could successfully start the compilation
3512
3513 if Process_Created then
3514 if Pid = Invalid_Pid then
3515 Record_Failure (Full_Source_File, Source_Unit);
3516 else
3517 Add_Process
3518 (Pid => Pid,
3519 Sfile => Full_Source_File,
3520 Afile => Lib_File,
3521 Uname => Source_Unit,
3522 Mfile => Mfile,
3523 Full_Lib_File => Full_Lib_File,
3524 Lib_File_Attr => Lib_File_Attr);
3525 end if;
3526 end if;
3527 end if;
3528 end if;
3529 end if;
3530 end if;
3531 return False;
3532 end Start_Compile_If_Possible;
3533
3534 -----------------------------
3535 -- Wait_For_Available_Slot --
3536 -----------------------------
3537
3538 procedure Wait_For_Available_Slot is
3539 Compilation_OK : Boolean;
3540 Text : Text_Buffer_Ptr;
3541 ALI : ALI_Id;
3542 Data : Compilation_Data;
3543
3544 begin
3545 if Outstanding_Compiles = Max_Process
3546 or else (Empty_Q
3547 and then not Good_ALI_Present
3548 and then Outstanding_Compiles > 0)
3549 then
3550 Await_Compile (Data, Compilation_OK);
3551
3552 if not Compilation_OK then
3553 Record_Failure (Data.Full_Source_File, Data.Source_Unit);
3554 end if;
3555
3556 if Compilation_OK or else Keep_Going then
3557
3558 -- Re-read the updated library file
3559
3560 declare
3561 Saved_Object_Consistency : constant Boolean :=
3562 Check_Object_Consistency;
3563
3564 begin
3565 -- If compilation was not OK, or if output is not an object
3566 -- file and we don't do the bind step, don't check for
3567 -- object consistency.
3568
3569 Check_Object_Consistency :=
3570 Check_Object_Consistency
3571 and Compilation_OK
3572 and (Output_Is_Object or Do_Bind_Step);
3573
3574 Text :=
3575 Read_Library_Info_From_Full
3576 (Data.Full_Lib_File, Data.Lib_File_Attr'Access);
3577
3578 -- Restore Check_Object_Consistency to its initial value
3579
3580 Check_Object_Consistency := Saved_Object_Consistency;
3581 end;
3582
3583 -- If an ALI file was generated by this compilation, scan the
3584 -- ALI file and record it.
3585
3586 -- If the scan fails, a previous ali file is inconsistent with
3587 -- the unit just compiled.
3588
3589 if Text /= null then
3590 ALI :=
3591 Scan_ALI
3592 (Data.Lib_File, Text, Ignore_ED => False, Err => True);
3593
3594 if ALI = No_ALI_Id then
3595
3596 -- Record a failure only if not already done
3597
3598 if Compilation_OK then
3599 Inform
3600 (Data.Lib_File,
3601 "incompatible ALI file, please recompile");
3602 Record_Failure
3603 (Data.Full_Source_File, Data.Source_Unit);
3604 end if;
3605
3606 else
3607 Record_Good_ALI (ALI);
3608 end if;
3609
3610 Free (Text);
3611
3612 -- If we could not read the ALI file that was just generated
3613 -- then there could be a problem reading either the ALI or the
3614 -- corresponding object file (if Check_Object_Consistency is
3615 -- set Read_Library_Info checks that the time stamp of the
3616 -- object file is more recent than that of the ALI). However,
3617 -- we record a failure only if not already done.
3618
3619 else
3620 if Compilation_OK and not Syntax_Only then
3621 Inform
3622 (Data.Lib_File,
3623 "WARNING: ALI or object file not found after compile");
3624 Record_Failure (Data.Full_Source_File, Data.Source_Unit);
3625 end if;
3626 end if;
3627 end if;
3628 end if;
3629 end Wait_For_Available_Slot;
3630
3631 -- Start of processing for Compile_Sources
3632
3633 begin
3634 pragma Assert (Args'First = 1);
3635
3636 Outstanding_Compiles := 0;
3637 Running_Compile := new Comp_Data_Arr (1 .. Max_Process);
3638
3639 -- Package and Queue initializations
3640
3641 Good_ALI.Init;
3642
3643 if First_Q_Initialization then
3644 Init_Q;
3645 end if;
3646
3647 if Initialize_ALI_Data then
3648 Initialize_ALI;
3649 Initialize_ALI_Source;
3650 end if;
3651
3652 -- The following two flags affect the behavior of ALI.Set_Source_Table.
3653 -- We set Check_Source_Files to True to ensure that source file time
3654 -- stamps are checked, and we set All_Sources to False to avoid checking
3655 -- the presence of the source files listed in the source dependency
3656 -- section of an ali file (which would be a mistake since the ali file
3657 -- may be obsolete).
3658
3659 Check_Source_Files := True;
3660 All_Sources := False;
3661
3662 -- Only insert in the Q if it is not already done, to avoid simultaneous
3663 -- compilations if -jnnn is used.
3664
3665 if not Is_Marked (Main_Source, Main_Index) then
3666 Insert_Q (Main_Source, Index => Main_Index);
3667 Mark (Main_Source, Main_Index);
3668 end if;
3669
3670 First_Compiled_File := No_File;
3671 Most_Recent_Obj_File := No_File;
3672 Most_Recent_Obj_Stamp := Empty_Time_Stamp;
3673 Main_Unit := False;
3674
3675 -- Keep looping until there is no more work to do (the Q is empty)
3676 -- and all the outstanding compilations have terminated.
3677
3678 Make_Loop : while not Empty_Q or else Outstanding_Compiles > 0 loop
3679 exit Make_Loop when Must_Exit_Because_Of_Error;
3680 exit Make_Loop when Start_Compile_If_Possible (Args);
3681
3682 Wait_For_Available_Slot;
3683
3684 -- ??? Should be done as soon as we add a Good_ALI, wouldn't it avoid
3685 -- the need for a list of good ALI?
3686
3687 Fill_Queue_From_ALI_Files;
3688
3689 if Display_Compilation_Progress then
3690 Write_Str ("completed ");
3691 Write_Int (Int (Q_Front));
3692 Write_Str (" out of ");
3693 Write_Int (Int (Q.Last));
3694 Write_Str (" (");
3695 Write_Int (Int ((Q_Front * 100) / (Q.Last - Q.First)));
3696 Write_Str ("%)...");
3697 Write_Eol;
3698 end if;
3699 end loop Make_Loop;
3700
3701 Compilation_Failures := Bad_Compilation_Count;
3702
3703 -- Compilation is finished
3704
3705 -- Delete any temporary configuration pragma file
3706
3707 if not Debug.Debug_Flag_N then
3708 Delete_Temp_Config_Files;
3709 end if;
3710 end Compile_Sources;
3711
3712 ----------------------------------
3713 -- Configuration_Pragmas_Switch --
3714 ----------------------------------
3715
3716 function Configuration_Pragmas_Switch
3717 (For_Project : Project_Id) return Argument_List
3718 is
3719 The_Packages : Package_Id;
3720 Gnatmake : Package_Id;
3721 Compiler : Package_Id;
3722
3723 Global_Attribute : Variable_Value := Nil_Variable_Value;
3724 Local_Attribute : Variable_Value := Nil_Variable_Value;
3725
3726 Global_Attribute_Present : Boolean := False;
3727 Local_Attribute_Present : Boolean := False;
3728
3729 Result : Argument_List (1 .. 3);
3730 Last : Natural := 0;
3731
3732 function Absolute_Path
3733 (Path : Path_Name_Type;
3734 Project : Project_Id) return String;
3735 -- Returns an absolute path for a configuration pragmas file
3736
3737 -------------------
3738 -- Absolute_Path --
3739 -------------------
3740
3741 function Absolute_Path
3742 (Path : Path_Name_Type;
3743 Project : Project_Id) return String
3744 is
3745 begin
3746 Get_Name_String (Path);
3747
3748 declare
3749 Path_Name : constant String := Name_Buffer (1 .. Name_Len);
3750
3751 begin
3752 if Is_Absolute_Path (Path_Name) then
3753 return Path_Name;
3754
3755 else
3756 declare
3757 Parent_Directory : constant String :=
3758 Get_Name_String (Project.Directory.Display_Name);
3759
3760 begin
3761 if Parent_Directory (Parent_Directory'Last) =
3762 Directory_Separator
3763 then
3764 return Parent_Directory & Path_Name;
3765
3766 else
3767 return Parent_Directory & Directory_Separator & Path_Name;
3768 end if;
3769 end;
3770 end if;
3771 end;
3772 end Absolute_Path;
3773
3774 -- Start of processing for Configuration_Pragmas_Switch
3775
3776 begin
3777 Prj.Env.Create_Config_Pragmas_File
3778 (For_Project, Project_Tree);
3779
3780 if For_Project.Config_File_Name /= No_Path then
3781 Temporary_Config_File := For_Project.Config_File_Temp;
3782 Last := 1;
3783 Result (1) :=
3784 new String'
3785 ("-gnatec=" & Get_Name_String (For_Project.Config_File_Name));
3786
3787 else
3788 Temporary_Config_File := False;
3789 end if;
3790
3791 -- Check for attribute Builder'Global_Configuration_Pragmas
3792
3793 The_Packages := Main_Project.Decl.Packages;
3794 Gnatmake :=
3795 Prj.Util.Value_Of
3796 (Name => Name_Builder,
3797 In_Packages => The_Packages,
3798 In_Tree => Project_Tree);
3799
3800 if Gnatmake /= No_Package then
3801 Global_Attribute := Prj.Util.Value_Of
3802 (Variable_Name => Name_Global_Configuration_Pragmas,
3803 In_Variables => Project_Tree.Packages.Table
3804 (Gnatmake).Decl.Attributes,
3805 In_Tree => Project_Tree);
3806 Global_Attribute_Present :=
3807 Global_Attribute /= Nil_Variable_Value
3808 and then Get_Name_String (Global_Attribute.Value) /= "";
3809
3810 if Global_Attribute_Present then
3811 declare
3812 Path : constant String :=
3813 Absolute_Path
3814 (Path_Name_Type (Global_Attribute.Value),
3815 Global_Attribute.Project);
3816 begin
3817 if not Is_Regular_File (Path) then
3818 if Debug.Debug_Flag_F then
3819 Make_Failed
3820 ("cannot find configuration pragmas file "
3821 & File_Name (Path));
3822 else
3823 Make_Failed
3824 ("cannot find configuration pragmas file " & Path);
3825 end if;
3826 end if;
3827
3828 Last := Last + 1;
3829 Result (Last) := new String'("-gnatec=" & Path);
3830 end;
3831 end if;
3832 end if;
3833
3834 -- Check for attribute Compiler'Local_Configuration_Pragmas
3835
3836 The_Packages := For_Project.Decl.Packages;
3837 Compiler :=
3838 Prj.Util.Value_Of
3839 (Name => Name_Compiler,
3840 In_Packages => The_Packages,
3841 In_Tree => Project_Tree);
3842
3843 if Compiler /= No_Package then
3844 Local_Attribute := Prj.Util.Value_Of
3845 (Variable_Name => Name_Local_Configuration_Pragmas,
3846 In_Variables => Project_Tree.Packages.Table
3847 (Compiler).Decl.Attributes,
3848 In_Tree => Project_Tree);
3849 Local_Attribute_Present :=
3850 Local_Attribute /= Nil_Variable_Value
3851 and then Get_Name_String (Local_Attribute.Value) /= "";
3852
3853 if Local_Attribute_Present then
3854 declare
3855 Path : constant String :=
3856 Absolute_Path
3857 (Path_Name_Type (Local_Attribute.Value),
3858 Local_Attribute.Project);
3859 begin
3860 if not Is_Regular_File (Path) then
3861 if Debug.Debug_Flag_F then
3862 Make_Failed
3863 ("cannot find configuration pragmas file "
3864 & File_Name (Path));
3865
3866 else
3867 Make_Failed
3868 ("cannot find configuration pragmas file " & Path);
3869 end if;
3870 end if;
3871
3872 Last := Last + 1;
3873 Result (Last) := new String'("-gnatec=" & Path);
3874 end;
3875 end if;
3876 end if;
3877
3878 return Result (1 .. Last);
3879 end Configuration_Pragmas_Switch;
3880
3881 ---------------
3882 -- Debug_Msg --
3883 ---------------
3884
3885 procedure Debug_Msg (S : String; N : Name_Id) is
3886 begin
3887 if Debug.Debug_Flag_W then
3888 Write_Str (" ... ");
3889 Write_Str (S);
3890 Write_Str (" ");
3891 Write_Name (N);
3892 Write_Eol;
3893 end if;
3894 end Debug_Msg;
3895
3896 procedure Debug_Msg (S : String; N : File_Name_Type) is
3897 begin
3898 Debug_Msg (S, Name_Id (N));
3899 end Debug_Msg;
3900
3901 procedure Debug_Msg (S : String; N : Unit_Name_Type) is
3902 begin
3903 Debug_Msg (S, Name_Id (N));
3904 end Debug_Msg;
3905
3906 ---------------------------
3907 -- Delete_All_Temp_Files --
3908 ---------------------------
3909
3910 procedure Delete_All_Temp_Files is
3911 begin
3912 if not Debug.Debug_Flag_N then
3913 Delete_Temp_Config_Files;
3914 Prj.Delete_All_Temp_Files (Project_Tree);
3915 end if;
3916 end Delete_All_Temp_Files;
3917
3918 ------------------------------
3919 -- Delete_Temp_Config_Files --
3920 ------------------------------
3921
3922 procedure Delete_Temp_Config_Files is
3923 Success : Boolean;
3924 Proj : Project_List;
3925 pragma Warnings (Off, Success);
3926
3927 begin
3928 -- The caller is responsible for ensuring that Debug_Flag_N is False
3929
3930 pragma Assert (not Debug.Debug_Flag_N);
3931
3932 if Main_Project /= No_Project then
3933 Proj := Project_Tree.Projects;
3934 while Proj /= null loop
3935 if Proj.Project.Config_File_Temp then
3936 Delete_Temporary_File
3937 (Project_Tree, Proj.Project.Config_File_Name);
3938
3939 -- Make sure that we don't have a config file for this project,
3940 -- in case there are several mains. In this case, we will
3941 -- recreate another config file: we cannot reuse the one that
3942 -- we just deleted!
3943
3944 Proj.Project.Config_Checked := False;
3945 Proj.Project.Config_File_Name := No_Path;
3946 Proj.Project.Config_File_Temp := False;
3947 end if;
3948 Proj := Proj.Next;
3949 end loop;
3950 end if;
3951 end Delete_Temp_Config_Files;
3952
3953 -------------
3954 -- Display --
3955 -------------
3956
3957 procedure Display (Program : String; Args : Argument_List) is
3958 begin
3959 pragma Assert (Args'First = 1);
3960
3961 if Display_Executed_Programs then
3962 Write_Str (Program);
3963
3964 for J in Args'Range loop
3965
3966 -- Never display -gnatea nor -gnatez
3967
3968 if Args (J).all /= "-gnatea"
3969 and then
3970 Args (J).all /= "-gnatez"
3971 then
3972 -- Do not display the mapping file argument automatically
3973 -- created when using a project file.
3974
3975 if Main_Project = No_Project
3976 or else Debug.Debug_Flag_N
3977 or else Args (J)'Length < 8
3978 or else
3979 Args (J) (Args (J)'First .. Args (J)'First + 6) /= "-gnatem"
3980 then
3981 -- When -dn is not specified, do not display the config
3982 -- pragmas switch (-gnatec) for the temporary file created
3983 -- by the project manager (always the first -gnatec switch).
3984 -- Reset Temporary_Config_File to False so that the eventual
3985 -- other -gnatec switches will be displayed.
3986
3987 if (not Debug.Debug_Flag_N)
3988 and then Temporary_Config_File
3989 and then Args (J)'Length > 7
3990 and then Args (J) (Args (J)'First .. Args (J)'First + 6)
3991 = "-gnatec"
3992 then
3993 Temporary_Config_File := False;
3994
3995 -- Do not display the -F=mapping_file switch for gnatbind
3996 -- if -dn is not specified.
3997
3998 elsif Debug.Debug_Flag_N
3999 or else Args (J)'Length < 4
4000 or else
4001 Args (J) (Args (J)'First .. Args (J)'First + 2) /= "-F="
4002 then
4003 Write_Str (" ");
4004
4005 -- If -df is used, only display file names, not path
4006 -- names.
4007
4008 if Debug.Debug_Flag_F then
4009 declare
4010 Equal_Pos : Natural;
4011 begin
4012 Equal_Pos := Args (J)'First - 1;
4013 for K in Args (J)'Range loop
4014 if Args (J) (K) = '=' then
4015 Equal_Pos := K;
4016 exit;
4017 end if;
4018 end loop;
4019
4020 if Is_Absolute_Path
4021 (Args (J) (Equal_Pos + 1 .. Args (J)'Last))
4022 then
4023 Write_Str
4024 (Args (J) (Args (J)'First .. Equal_Pos));
4025 Write_Str
4026 (File_Name
4027 (Args (J)
4028 (Equal_Pos + 1 .. Args (J)'Last)));
4029
4030 else
4031 Write_Str (Args (J).all);
4032 end if;
4033 end;
4034
4035 else
4036 Write_Str (Args (J).all);
4037 end if;
4038 end if;
4039 end if;
4040 end if;
4041 end loop;
4042
4043 Write_Eol;
4044 end if;
4045 end Display;
4046
4047 ----------------------
4048 -- Display_Commands --
4049 ----------------------
4050
4051 procedure Display_Commands (Display : Boolean := True) is
4052 begin
4053 Display_Executed_Programs := Display;
4054 end Display_Commands;
4055
4056 -------------
4057 -- Empty_Q --
4058 -------------
4059
4060 function Empty_Q return Boolean is
4061 begin
4062 if Debug.Debug_Flag_P then
4063 Write_Str (" Q := [");
4064
4065 for J in Q_Front .. Q.Last - 1 loop
4066 Write_Str (" ");
4067 Write_Name (Q.Table (J).File);
4068 Write_Eol;
4069 Write_Str (" ");
4070 end loop;
4071
4072 Write_Str ("]");
4073 Write_Eol;
4074 end if;
4075
4076 return Q_Front >= Q.Last;
4077 end Empty_Q;
4078
4079 --------------------------
4080 -- Enter_Into_Obsoleted --
4081 --------------------------
4082
4083 procedure Enter_Into_Obsoleted (F : File_Name_Type) is
4084 Name : constant String := Get_Name_String (F);
4085 First : Natural;
4086 F2 : File_Name_Type;
4087
4088 begin
4089 First := Name'Last;
4090 while First > Name'First
4091 and then Name (First - 1) /= Directory_Separator
4092 and then Name (First - 1) /= '/'
4093 loop
4094 First := First - 1;
4095 end loop;
4096
4097 if First /= Name'First then
4098 Name_Len := 0;
4099 Add_Str_To_Name_Buffer (Name (First .. Name'Last));
4100 F2 := Name_Find;
4101
4102 else
4103 F2 := F;
4104 end if;
4105
4106 Debug_Msg ("New entry in Obsoleted table:", F2);
4107 Obsoleted.Set (F2, True);
4108 end Enter_Into_Obsoleted;
4109
4110 --------------------
4111 -- Extract_From_Q --
4112 --------------------
4113
4114 procedure Extract_From_Q
4115 (Source_File : out File_Name_Type;
4116 Source_Unit : out Unit_Name_Type;
4117 Source_Index : out Int)
4118 is
4119 File : constant File_Name_Type := Q.Table (Q_Front).File;
4120 Unit : constant Unit_Name_Type := Q.Table (Q_Front).Unit;
4121 Index : constant Int := Q.Table (Q_Front).Index;
4122
4123 begin
4124 if Debug.Debug_Flag_Q then
4125 Write_Str (" Q := Q - [ ");
4126 Write_Name (File);
4127
4128 if Index /= 0 then
4129 Write_Str (", ");
4130 Write_Int (Index);
4131 end if;
4132
4133 Write_Str (" ]");
4134 Write_Eol;
4135 end if;
4136
4137 Q_Front := Q_Front + 1;
4138 Source_File := File;
4139 Source_Unit := Unit;
4140 Source_Index := Index;
4141 end Extract_From_Q;
4142
4143 --------------
4144 -- Gnatmake --
4145 --------------
4146
4147 procedure Gnatmake is
4148 Main_Source_File : File_Name_Type;
4149 -- The source file containing the main compilation unit
4150
4151 Compilation_Failures : Natural;
4152
4153 Total_Compilation_Failures : Natural := 0;
4154
4155 Is_Main_Unit : Boolean;
4156 -- Set True by Compile_Sources if Main_Source_File can be a main unit
4157
4158 Main_ALI_File : File_Name_Type;
4159 -- The ali file corresponding to Main_Source_File
4160
4161 Executable : File_Name_Type := No_File;
4162 -- The file name of an executable
4163
4164 Non_Std_Executable : Boolean := False;
4165 -- Non_Std_Executable is set to True when there is a possibility that
4166 -- the linker will not choose the correct executable file name.
4167
4168 Current_Work_Dir : constant String_Access :=
4169 new String'(Get_Current_Dir);
4170 -- The current working directory, used to modify some relative path
4171 -- switches on the command line when a project file is used.
4172
4173 Current_Main_Index : Int := 0;
4174 -- If not zero, the index of the current main unit in its source file
4175
4176 Stand_Alone_Libraries : Boolean := False;
4177 -- Set to True when there are Stand-Alone Libraries, so that gnatbind
4178 -- is invoked with the -F switch to force checking of elaboration flags.
4179
4180 Mapping_Path : Path_Name_Type := No_Path;
4181 -- The path name of the mapping file
4182
4183 Project_Node_Tree : Project_Node_Tree_Ref;
4184
4185 Discard : Boolean;
4186 pragma Warnings (Off, Discard);
4187
4188 procedure Check_Mains;
4189 -- Check that the main subprograms do exist and that they all
4190 -- belong to the same project file.
4191
4192 procedure Create_Binder_Mapping_File
4193 (Args : in out Argument_List; Last_Arg : in out Natural);
4194 -- Create a binder mapping file and add the necessary switch
4195
4196 -----------------
4197 -- Check_Mains --
4198 -----------------
4199
4200 procedure Check_Mains is
4201 Real_Main_Project : Project_Id := No_Project;
4202 -- The project of the first main
4203
4204 Proj : Project_Id := No_Project;
4205 -- The project of the current main
4206
4207 Real_Path : String_Access;
4208
4209 begin
4210 Mains.Reset;
4211
4212 -- Check each main
4213
4214 loop
4215 declare
4216 Main : constant String := Mains.Next_Main;
4217 -- The name specified on the command line may include directory
4218 -- information.
4219
4220 File_Name : constant String := Base_Name (Main);
4221 -- The simple file name of the current main
4222
4223 Lang : Language_Ptr;
4224
4225 begin
4226 exit when Main = "";
4227
4228 -- Get the project of the current main
4229
4230 Proj := Prj.Env.Project_Of
4231 (File_Name, Main_Project, Project_Tree);
4232
4233 -- Fail if the current main is not a source of a project
4234
4235 if Proj = No_Project then
4236 Make_Failed
4237 ("""" & Main & """ is not a source of any project");
4238
4239 else
4240 -- If there is directory information, check that the source
4241 -- exists and, if it does, that the path is the actual path
4242 -- of a source of a project.
4243
4244 if Main /= File_Name then
4245 Lang := Get_Language_From_Name (Main_Project, "ada");
4246
4247 Real_Path :=
4248 Locate_Regular_File
4249 (Main & Get_Name_String
4250 (Lang.Config.Naming_Data.Body_Suffix),
4251 "");
4252 if Real_Path = null then
4253 Real_Path :=
4254 Locate_Regular_File
4255 (Main & Get_Name_String
4256 (Lang.Config.Naming_Data.Spec_Suffix),
4257 "");
4258 end if;
4259
4260 if Real_Path = null then
4261 Real_Path := Locate_Regular_File (Main, "");
4262 end if;
4263
4264 -- Fail if the file cannot be found
4265
4266 if Real_Path = null then
4267 Make_Failed ("file """ & Main & """ does not exist");
4268 end if;
4269
4270 declare
4271 Project_Path : constant String :=
4272 Prj.Env.File_Name_Of_Library_Unit_Body
4273 (Name => File_Name,
4274 Project => Main_Project,
4275 In_Tree => Project_Tree,
4276 Main_Project_Only => False,
4277 Full_Path => True);
4278 Normed_Path : constant String :=
4279 Normalize_Pathname
4280 (Real_Path.all,
4281 Case_Sensitive => False);
4282 Proj_Path : constant String :=
4283 Normalize_Pathname
4284 (Project_Path,
4285 Case_Sensitive => False);
4286
4287 begin
4288 Free (Real_Path);
4289
4290 -- Fail if it is not the correct path
4291
4292 if Normed_Path /= Proj_Path then
4293 if Verbose_Mode then
4294 Set_Standard_Error;
4295 Write_Str (Normed_Path);
4296 Write_Str (" /= ");
4297 Write_Line (Proj_Path);
4298 end if;
4299
4300 Make_Failed
4301 ("""" & Main &
4302 """ is not a source of any project");
4303 end if;
4304 end;
4305 end if;
4306
4307 if not Unique_Compile then
4308
4309 -- Record the project, if it is the first main
4310
4311 if Real_Main_Project = No_Project then
4312 Real_Main_Project := Proj;
4313
4314 elsif Proj /= Real_Main_Project then
4315
4316 -- Fail, as the current main is not a source of the
4317 -- same project as the first main.
4318
4319 Make_Failed
4320 ("""" & Main &
4321 """ is not a source of project " &
4322 Get_Name_String (Real_Main_Project.Name));
4323 end if;
4324 end if;
4325 end if;
4326
4327 -- If -u and -U are not used, we may have mains that are
4328 -- sources of a project that is not the one specified with
4329 -- switch -P.
4330
4331 if not Unique_Compile then
4332 Main_Project := Real_Main_Project;
4333 end if;
4334 end;
4335 end loop;
4336 end Check_Mains;
4337
4338 --------------------------------
4339 -- Create_Binder_Mapping_File --
4340 --------------------------------
4341
4342 procedure Create_Binder_Mapping_File
4343 (Args : in out Argument_List; Last_Arg : in out Natural)
4344 is
4345 Mapping_FD : File_Descriptor := Invalid_FD;
4346 -- A File Descriptor for an eventual mapping file
4347
4348 ALI_Unit : Unit_Name_Type := No_Unit_Name;
4349 -- The unit name of an ALI file
4350
4351 ALI_Name : File_Name_Type := No_File;
4352 -- The file name of the ALI file
4353
4354 ALI_Project : Project_Id := No_Project;
4355 -- The project of the ALI file
4356
4357 Bytes : Integer;
4358 OK : Boolean := True;
4359 Unit : Unit_Index;
4360
4361 Status : Boolean;
4362 -- For call to Close
4363
4364 begin
4365 Tempdir.Create_Temp_File (Mapping_FD, Mapping_Path);
4366 Record_Temp_File (Project_Tree, Mapping_Path);
4367
4368 if Mapping_FD /= Invalid_FD then
4369
4370 -- Traverse all units
4371
4372 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
4373
4374 while Unit /= No_Unit_Index loop
4375 if Unit.Name /= No_Name then
4376
4377 -- If there is a body, put it in the mapping
4378
4379 if Unit.File_Names (Impl) /= No_Source
4380 and then Unit.File_Names (Impl).Project /=
4381 No_Project
4382 then
4383 Get_Name_String (Unit.Name);
4384 Add_Str_To_Name_Buffer ("%b");
4385 ALI_Unit := Name_Find;
4386 ALI_Name :=
4387 Lib_File_Name
4388 (Unit.File_Names (Impl).Display_File);
4389 ALI_Project := Unit.File_Names (Impl).Project;
4390
4391 -- Otherwise, if there is a spec, put it in the mapping
4392
4393 elsif Unit.File_Names (Spec) /= No_Source
4394 and then Unit.File_Names (Spec).Project /= No_Project
4395 then
4396 Get_Name_String (Unit.Name);
4397 Add_Str_To_Name_Buffer ("%s");
4398 ALI_Unit := Name_Find;
4399 ALI_Name :=
4400 Lib_File_Name
4401 (Unit.File_Names (Spec).Display_File);
4402 ALI_Project := Unit.File_Names (Spec).Project;
4403
4404 else
4405 ALI_Name := No_File;
4406 end if;
4407
4408 -- If we have something to put in the mapping then do it
4409 -- now. However, if the project is extended, we don't put
4410 -- anything in the mapping file, because we don't know where
4411 -- the ALI file is: it might be in the extended project
4412 -- object directory as well as in the extending project
4413 -- object directory.
4414
4415 if ALI_Name /= No_File
4416 and then ALI_Project.Extended_By = No_Project
4417 and then ALI_Project.Extends = No_Project
4418 then
4419 -- First check if the ALI file exists. If it does not,
4420 -- do not put the unit in the mapping file.
4421
4422 declare
4423 ALI : constant String := Get_Name_String (ALI_Name);
4424
4425 begin
4426 -- For library projects, use the library directory,
4427 -- for other projects, use the object directory.
4428
4429 if ALI_Project.Library then
4430 Get_Name_String (ALI_Project.Library_Dir.Name);
4431 else
4432 Get_Name_String
4433 (ALI_Project.Object_Directory.Name);
4434 end if;
4435
4436 if not
4437 Is_Directory_Separator (Name_Buffer (Name_Len))
4438 then
4439 Add_Char_To_Name_Buffer (Directory_Separator);
4440 end if;
4441
4442 Add_Str_To_Name_Buffer (ALI);
4443 Add_Char_To_Name_Buffer (ASCII.LF);
4444
4445 declare
4446 ALI_Path_Name : constant String :=
4447 Name_Buffer (1 .. Name_Len);
4448
4449 begin
4450 if Is_Regular_File
4451 (ALI_Path_Name (1 .. ALI_Path_Name'Last - 1))
4452 then
4453 -- First line is the unit name
4454
4455 Get_Name_String (ALI_Unit);
4456 Add_Char_To_Name_Buffer (ASCII.LF);
4457 Bytes :=
4458 Write
4459 (Mapping_FD,
4460 Name_Buffer (1)'Address,
4461 Name_Len);
4462 OK := Bytes = Name_Len;
4463
4464 exit when not OK;
4465
4466 -- Second line it the ALI file name
4467
4468 Get_Name_String (ALI_Name);
4469 Add_Char_To_Name_Buffer (ASCII.LF);
4470 Bytes :=
4471 Write
4472 (Mapping_FD,
4473 Name_Buffer (1)'Address,
4474 Name_Len);
4475 OK := (Bytes = Name_Len);
4476
4477 exit when not OK;
4478
4479 -- Third line it the ALI path name
4480
4481 Bytes :=
4482 Write
4483 (Mapping_FD,
4484 ALI_Path_Name (1)'Address,
4485 ALI_Path_Name'Length);
4486 OK := (Bytes = ALI_Path_Name'Length);
4487
4488 -- If OK is False, it means we were unable to
4489 -- write a line. No point in continuing with the
4490 -- other units.
4491
4492 exit when not OK;
4493 end if;
4494 end;
4495 end;
4496 end if;
4497 end if;
4498
4499 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
4500 end loop;
4501
4502 Close (Mapping_FD, Status);
4503
4504 OK := OK and Status;
4505
4506 -- If the creation of the mapping file was successful, we add the
4507 -- switch to the arguments of gnatbind.
4508
4509 if OK then
4510 Last_Arg := Last_Arg + 1;
4511 Args (Last_Arg) :=
4512 new String'("-F=" & Get_Name_String (Mapping_Path));
4513 end if;
4514 end if;
4515 end Create_Binder_Mapping_File;
4516
4517 -- Start of processing for Gnatmake
4518
4519 -- This body is very long, should be broken down???
4520
4521 begin
4522 Install_Int_Handler (Sigint_Intercepted'Access);
4523
4524 Do_Compile_Step := True;
4525 Do_Bind_Step := True;
4526 Do_Link_Step := True;
4527
4528 Obsoleted.Reset;
4529
4530 Make.Initialize (Project_Node_Tree);
4531
4532 Bind_Shared := No_Shared_Switch'Access;
4533 Link_With_Shared_Libgcc := No_Shared_Libgcc_Switch'Access;
4534
4535 Failed_Links.Set_Last (0);
4536 Successful_Links.Set_Last (0);
4537
4538 -- Special case when switch -B was specified
4539
4540 if Build_Bind_And_Link_Full_Project then
4541
4542 -- When switch -B is specified, there must be a project file
4543
4544 if Main_Project = No_Project then
4545 Make_Failed ("-B cannot be used without a project file");
4546
4547 -- No main program may be specified on the command line
4548
4549 elsif Osint.Number_Of_Files /= 0 then
4550 Make_Failed ("-B cannot be used with a main specified on " &
4551 "the command line");
4552
4553 -- And the project file cannot be a library project file
4554
4555 elsif Main_Project.Library then
4556 Make_Failed ("-B cannot be used for a library project file");
4557
4558 else
4559 No_Main_Subprogram := True;
4560 Insert_Project_Sources
4561 (The_Project => Main_Project,
4562 All_Projects => Unique_Compile_All_Projects,
4563 Into_Q => False);
4564
4565 -- If there are no sources to compile, we fail
4566
4567 if Osint.Number_Of_Files = 0 then
4568 Make_Failed ("no sources to compile");
4569 end if;
4570
4571 -- Specify -n for gnatbind and add the ALI files of all the
4572 -- sources, except the one which is a fake main subprogram: this
4573 -- is the one for the binder generated file and it will be
4574 -- transmitted to gnatlink. These sources are those that are in
4575 -- the queue.
4576
4577 Add_Switch ("-n", Binder, And_Save => True);
4578
4579 for J in Q.First .. Q.Last - 1 loop
4580 Add_Switch
4581 (Get_Name_String
4582 (Lib_File_Name (Q.Table (J).File)),
4583 Binder, And_Save => True);
4584 end loop;
4585 end if;
4586
4587 elsif Main_Index /= 0 and then Osint.Number_Of_Files > 1 then
4588 Make_Failed ("cannot specify several mains with a multi-unit index");
4589
4590 elsif Main_Project /= No_Project then
4591
4592 -- If the main project file is a library project file, main(s) cannot
4593 -- be specified on the command line.
4594
4595 if Osint.Number_Of_Files /= 0 then
4596 if Main_Project.Library
4597 and then not Unique_Compile
4598 and then ((not Make_Steps) or else Bind_Only or else Link_Only)
4599 then
4600 Make_Failed ("cannot specify a main program " &
4601 "on the command line for a library project file");
4602
4603 else
4604 -- Check that each main on the command line is a source of a
4605 -- project file and, if there are several mains, each of them
4606 -- is a source of the same project file.
4607
4608 Check_Mains;
4609 end if;
4610
4611 -- If no mains have been specified on the command line, and we are
4612 -- using a project file, we either find the main(s) in attribute Main
4613 -- of the main project, or we put all the sources of the project file
4614 -- as mains.
4615
4616 else
4617 if Main_Index /= 0 then
4618 Make_Failed ("cannot specify a multi-unit index but no main " &
4619 "on the command line");
4620 end if;
4621
4622 declare
4623 Value : String_List_Id := Main_Project.Mains;
4624
4625 begin
4626 -- The attribute Main is an empty list or not specified, or
4627 -- else gnatmake was invoked with the switch "-u".
4628
4629 if Value = Prj.Nil_String or else Unique_Compile then
4630
4631 if (not Make_Steps) or else Compile_Only
4632 or else not Main_Project.Library
4633 then
4634 -- First make sure that the binder and the linker will
4635 -- not be invoked.
4636
4637 Do_Bind_Step := False;
4638 Do_Link_Step := False;
4639
4640 -- Put all the sources in the queue
4641
4642 No_Main_Subprogram := True;
4643 Insert_Project_Sources
4644 (The_Project => Main_Project,
4645 All_Projects => Unique_Compile_All_Projects,
4646 Into_Q => False);
4647
4648 -- If no sources to compile, then there is nothing to do
4649
4650 if Osint.Number_Of_Files = 0 then
4651 if not Quiet_Output then
4652 Osint.Write_Program_Name;
4653 Write_Line (": no sources to compile");
4654 end if;
4655
4656 Delete_All_Temp_Files;
4657 Exit_Program (E_Success);
4658 end if;
4659 end if;
4660
4661 else
4662 -- The attribute Main is not an empty list. Put all the main
4663 -- subprograms in the list as if they were specified on the
4664 -- command line. However, if attribute Languages includes a
4665 -- language other than Ada, only include the Ada mains; if
4666 -- there is no Ada main, compile all sources of the project.
4667
4668 declare
4669 Languages : constant Variable_Value :=
4670 Prj.Util.Value_Of
4671 (Name_Languages,
4672 Main_Project.Decl.Attributes,
4673 Project_Tree);
4674
4675 Current : String_List_Id;
4676 Element : String_Element;
4677
4678 Foreign_Language : Boolean := False;
4679 At_Least_One_Main : Boolean := False;
4680
4681 begin
4682 -- First, determine if there is a foreign language in
4683 -- attribute Languages.
4684
4685 if not Languages.Default then
4686 Current := Languages.Values;
4687 Look_For_Foreign :
4688 while Current /= Nil_String loop
4689 Element := Project_Tree.String_Elements.
4690 Table (Current);
4691 Get_Name_String (Element.Value);
4692 To_Lower (Name_Buffer (1 .. Name_Len));
4693
4694 if Name_Buffer (1 .. Name_Len) /= "ada" then
4695 Foreign_Language := True;
4696 exit Look_For_Foreign;
4697 end if;
4698
4699 Current := Element.Next;
4700 end loop Look_For_Foreign;
4701 end if;
4702
4703 -- Then, find all mains, or if there is a foreign
4704 -- language, all the Ada mains.
4705
4706 while Value /= Prj.Nil_String loop
4707 Get_Name_String
4708 (Project_Tree.String_Elements.Table (Value).Value);
4709
4710 -- To know if a main is an Ada main, get its project.
4711 -- It should be the project specified on the command
4712 -- line.
4713
4714 if (not Foreign_Language) or else
4715 Prj.Env.Project_Of
4716 (Name_Buffer (1 .. Name_Len),
4717 Main_Project,
4718 Project_Tree) =
4719 Main_Project
4720 then
4721 At_Least_One_Main := True;
4722 Osint.Add_File
4723 (Get_Name_String
4724 (Project_Tree.String_Elements.Table
4725 (Value).Value),
4726 Index =>
4727 Project_Tree.String_Elements.Table
4728 (Value).Index);
4729 end if;
4730
4731 Value := Project_Tree.String_Elements.Table
4732 (Value).Next;
4733 end loop;
4734
4735 -- If we did not get any main, it means that all mains
4736 -- in attribute Mains are in a foreign language and -B
4737 -- was not specified to gnatmake; so, we fail.
4738
4739 if not At_Least_One_Main then
4740 Make_Failed
4741 ("no Ada mains, use -B to build foreign main");
4742 end if;
4743 end;
4744
4745 end if;
4746 end;
4747 end if;
4748 end if;
4749
4750 if Verbose_Mode then
4751 Write_Eol;
4752 Display_Version ("GNATMAKE", "1995");
4753 end if;
4754
4755 if Osint.Number_Of_Files = 0 then
4756 if Main_Project /= No_Project
4757 and then Main_Project.Library
4758 then
4759 if Do_Bind_Step
4760 and then not Main_Project.Standalone_Library
4761 then
4762 Make_Failed ("only stand-alone libraries may be bound");
4763 end if;
4764
4765 -- Add the default search directories to be able to find libgnat
4766
4767 Osint.Add_Default_Search_Dirs;
4768
4769 -- Get the target parameters, so that the correct binder generated
4770 -- files are generated if OpenVMS is the target.
4771
4772 begin
4773 Targparm.Get_Target_Parameters;
4774
4775 exception
4776 when Unrecoverable_Error =>
4777 Make_Failed ("*** make failed.");
4778 end;
4779
4780 -- And bind and or link the library
4781
4782 MLib.Prj.Build_Library
4783 (For_Project => Main_Project,
4784 In_Tree => Project_Tree,
4785 Gnatbind => Gnatbind.all,
4786 Gnatbind_Path => Gnatbind_Path,
4787 Gcc => Gcc.all,
4788 Gcc_Path => Gcc_Path,
4789 Bind => Bind_Only,
4790 Link => Link_Only);
4791
4792 Delete_All_Temp_Files;
4793 Exit_Program (E_Success);
4794
4795 else
4796 -- Call Get_Target_Parameters to ensure that VM_Target and
4797 -- AAMP_On_Target get set before calling Usage.
4798
4799 Targparm.Get_Target_Parameters;
4800
4801 -- Output usage information if no files to compile
4802
4803 Usage;
4804 Exit_Program (E_Fatal);
4805 end if;
4806 end if;
4807
4808 -- If -M was specified, behave as if -n was specified
4809
4810 if List_Dependencies then
4811 Do_Not_Execute := True;
4812 end if;
4813
4814 -- Note that Osint.M.Next_Main_Source will always return the (possibly
4815 -- abbreviated file) without any directory information.
4816
4817 Main_Source_File := Next_Main_Source;
4818
4819 if Current_File_Index /= No_Index then
4820 Main_Index := Current_File_Index;
4821 end if;
4822
4823 Add_Switch ("-I-", Compiler, And_Save => True);
4824
4825 if Main_Project = No_Project then
4826 if Look_In_Primary_Dir then
4827
4828 Add_Switch
4829 ("-I" &
4830 Normalize_Directory_Name
4831 (Get_Primary_Src_Search_Directory.all).all,
4832 Compiler, Append_Switch => False,
4833 And_Save => False);
4834
4835 end if;
4836
4837 else
4838 -- If we use a project file, we have already checked that a main
4839 -- specified on the command line with directory information has the
4840 -- path name corresponding to a correct source in the project tree.
4841 -- So, we don't need the directory information to be taken into
4842 -- account by Find_File, and in fact it may lead to take the wrong
4843 -- sources for other compilation units, when there are extending
4844 -- projects.
4845
4846 Look_In_Primary_Dir := False;
4847 Add_Switch ("-I-", Binder, And_Save => True);
4848 end if;
4849
4850 -- If the user wants a program without a main subprogram, add the
4851 -- appropriate switch to the binder.
4852
4853 if No_Main_Subprogram then
4854 Add_Switch ("-z", Binder, And_Save => True);
4855 end if;
4856
4857 if Main_Project /= No_Project then
4858
4859 if Main_Project.Object_Directory /= No_Path_Information then
4860 -- Change current directory to object directory of main project
4861
4862 Project_Of_Current_Object_Directory := No_Project;
4863 Change_To_Object_Directory (Main_Project);
4864 end if;
4865
4866 -- Source file lookups should be cached for efficiency.
4867 -- Source files are not supposed to change.
4868
4869 Osint.Source_File_Data (Cache => True);
4870
4871 -- Find the file name of the (first) main unit
4872
4873 declare
4874 Main_Source_File_Name : constant String :=
4875 Get_Name_String (Main_Source_File);
4876 Main_Unit_File_Name : constant String :=
4877 Prj.Env.File_Name_Of_Library_Unit_Body
4878 (Name => Main_Source_File_Name,
4879 Project => Main_Project,
4880 In_Tree => Project_Tree,
4881 Main_Project_Only =>
4882 not Unique_Compile);
4883
4884 The_Packages : constant Package_Id :=
4885 Main_Project.Decl.Packages;
4886
4887 Builder_Package : constant Prj.Package_Id :=
4888 Prj.Util.Value_Of
4889 (Name => Name_Builder,
4890 In_Packages => The_Packages,
4891 In_Tree => Project_Tree);
4892
4893 Binder_Package : constant Prj.Package_Id :=
4894 Prj.Util.Value_Of
4895 (Name => Name_Binder,
4896 In_Packages => The_Packages,
4897 In_Tree => Project_Tree);
4898
4899 Linker_Package : constant Prj.Package_Id :=
4900 Prj.Util.Value_Of
4901 (Name => Name_Linker,
4902 In_Packages => The_Packages,
4903 In_Tree => Project_Tree);
4904
4905 Default_Switches_Array : Array_Id;
4906
4907 Global_Compilation_Array : Array_Element_Id;
4908 Global_Compilation_Elem : Array_Element;
4909 Global_Compilation_Switches : Variable_Value;
4910
4911 begin
4912 -- We fail if we cannot find the main source file
4913
4914 if Main_Unit_File_Name = "" then
4915 Make_Failed ('"' & Main_Source_File_Name
4916 & """ is not a unit of project "
4917 & Project_File_Name.all & ".");
4918 else
4919 -- Remove any directory information from the main source file
4920 -- file name.
4921
4922 declare
4923 Pos : Natural := Main_Unit_File_Name'Last;
4924
4925 begin
4926 loop
4927 exit when Pos < Main_Unit_File_Name'First or else
4928 Main_Unit_File_Name (Pos) = Directory_Separator;
4929 Pos := Pos - 1;
4930 end loop;
4931
4932 Name_Len := Main_Unit_File_Name'Last - Pos;
4933
4934 Name_Buffer (1 .. Name_Len) :=
4935 Main_Unit_File_Name
4936 (Pos + 1 .. Main_Unit_File_Name'Last);
4937
4938 Main_Source_File := Name_Find;
4939
4940 -- We only output the main source file if there is only one
4941
4942 if Verbose_Mode and then Osint.Number_Of_Files = 1 then
4943 Write_Str ("Main source file: """);
4944 Write_Str (Main_Unit_File_Name
4945 (Pos + 1 .. Main_Unit_File_Name'Last));
4946 Write_Line (""".");
4947 end if;
4948 end;
4949 end if;
4950
4951 -- If there is a package Builder in the main project file, add
4952 -- the switches from it.
4953
4954 if Builder_Package /= No_Package then
4955
4956 Global_Compilation_Array := Prj.Util.Value_Of
4957 (Name => Name_Global_Compilation_Switches,
4958 In_Arrays => Project_Tree.Packages.Table
4959 (Builder_Package).Decl.Arrays,
4960 In_Tree => Project_Tree);
4961
4962 Default_Switches_Array :=
4963 Project_Tree.Packages.Table
4964 (Builder_Package).Decl.Arrays;
4965
4966 while Default_Switches_Array /= No_Array and then
4967 Project_Tree.Arrays.Table (Default_Switches_Array).Name /=
4968 Name_Default_Switches
4969 loop
4970 Default_Switches_Array :=
4971 Project_Tree.Arrays.Table (Default_Switches_Array).Next;
4972 end loop;
4973
4974 if Global_Compilation_Array /= No_Array_Element and then
4975 Default_Switches_Array /= No_Array
4976 then
4977 Errutil.Error_Msg
4978 ("Default_Switches forbidden in presence of " &
4979 "Global_Compilation_Switches. Use Switches instead.",
4980 Project_Tree.Arrays.Table
4981 (Default_Switches_Array).Location);
4982 Errutil.Finalize;
4983 Make_Failed
4984 ("*** illegal combination of Builder attributes");
4985 end if;
4986
4987 -- If there is only one main, we attempt to get the gnatmake
4988 -- switches for this main (if any). If there are no specific
4989 -- switch for this particular main, get the general gnatmake
4990 -- switches (if any).
4991
4992 if Osint.Number_Of_Files = 1 then
4993 if Verbose_Mode then
4994 Write_Str ("Adding gnatmake switches for """);
4995 Write_Str (Main_Unit_File_Name);
4996 Write_Line (""".");
4997 end if;
4998
4999 Add_Switches
5000 (Project_Node_Tree => Project_Node_Tree,
5001 File_Name => Main_Unit_File_Name,
5002 Index => Main_Index,
5003 The_Package => Builder_Package,
5004 Program => None,
5005 Unknown_Switches_To_The_Compiler =>
5006 Global_Compilation_Array = No_Array_Element);
5007
5008 else
5009 -- If there are several mains, we always get the general
5010 -- gnatmake switches (if any).
5011
5012 -- Warn the user, if necessary, so that he is not surprised
5013 -- that specific switches are not taken into account.
5014
5015 declare
5016 Defaults : constant Variable_Value :=
5017 Prj.Util.Value_Of
5018 (Name => Name_Ada,
5019 Index => 0,
5020 Attribute_Or_Array_Name =>
5021 Name_Default_Switches,
5022 In_Package =>
5023 Builder_Package,
5024 In_Tree => Project_Tree);
5025
5026 Switches : constant Array_Element_Id :=
5027 Prj.Util.Value_Of
5028 (Name => Name_Switches,
5029 In_Arrays =>
5030 Project_Tree.Packages.Table
5031 (Builder_Package).Decl.Arrays,
5032 In_Tree => Project_Tree);
5033
5034 Other_Switches : constant Variable_Value :=
5035 Prj.Util.Value_Of
5036 (Name => All_Other_Names,
5037 Index => 0,
5038 Attribute_Or_Array_Name
5039 => Name_Switches,
5040 In_Package => Builder_Package,
5041 In_Tree => Project_Tree);
5042
5043 begin
5044 if Other_Switches /= Nil_Variable_Value then
5045 if not Quiet_Output
5046 and then Switches /= No_Array_Element
5047 and then Project_Tree.Array_Elements.Table
5048 (Switches).Next /= No_Array_Element
5049 then
5050 Write_Line
5051 ("Warning: using Builder'Switches(others), "
5052 & "as there are several mains");
5053 end if;
5054
5055 Add_Switches
5056 (Project_Node_Tree => Project_Node_Tree,
5057 File_Name => " ",
5058 Index => 0,
5059 The_Package => Builder_Package,
5060 Program => None,
5061 Unknown_Switches_To_The_Compiler => False);
5062
5063 elsif Defaults /= Nil_Variable_Value then
5064 if not Quiet_Output
5065 and then Switches /= No_Array_Element
5066 then
5067 Write_Line
5068 ("Warning: using Builder'Default_Switches"
5069 & "(""Ada""), as there are several mains");
5070 end if;
5071
5072 Add_Switches
5073 (Project_Node_Tree => Project_Node_Tree,
5074 File_Name => " ",
5075 Index => 0,
5076 The_Package => Builder_Package,
5077 Program => None);
5078
5079 elsif not Quiet_Output
5080 and then Switches /= No_Array_Element
5081 then
5082 Write_Line
5083 ("Warning: using no switches from package "
5084 & "Builder, as there are several mains");
5085 end if;
5086 end;
5087 end if;
5088
5089 -- Take into account attribute Global_Compilation_Switches
5090 -- ("Ada").
5091
5092 declare
5093 Index : Name_Id;
5094 List : String_List_Id;
5095 Elem : String_Element;
5096
5097 begin
5098 while Global_Compilation_Array /= No_Array_Element loop
5099 Global_Compilation_Elem :=
5100 Project_Tree.Array_Elements.Table
5101 (Global_Compilation_Array);
5102
5103 Get_Name_String (Global_Compilation_Elem.Index);
5104 To_Lower (Name_Buffer (1 .. Name_Len));
5105 Index := Name_Find;
5106
5107 if Index = Name_Ada then
5108 Global_Compilation_Switches :=
5109 Global_Compilation_Elem.Value;
5110
5111 if Global_Compilation_Switches /= Nil_Variable_Value
5112 and then not Global_Compilation_Switches.Default
5113 then
5114 -- We have found attribute
5115 -- Global_Compilation_Switches ("Ada"): put the
5116 -- switches in the appropriate table.
5117
5118 List := Global_Compilation_Switches.Values;
5119
5120 while List /= Nil_String loop
5121 Elem :=
5122 Project_Tree.String_Elements.Table (List);
5123
5124 if Elem.Value /= No_Name then
5125 Add_Switch
5126 (Get_Name_String (Elem.Value),
5127 Compiler,
5128 And_Save => False);
5129 end if;
5130
5131 List := Elem.Next;
5132 end loop;
5133
5134 exit;
5135 end if;
5136 end if;
5137
5138 Global_Compilation_Array := Global_Compilation_Elem.Next;
5139 end loop;
5140 end;
5141 end if;
5142
5143 Osint.Add_Default_Search_Dirs;
5144
5145 -- Record the current last switch index for table Binder_Switches
5146 -- and Linker_Switches, so that these tables may be reset before
5147 -- for each main, before adding switches from the project file
5148 -- and from the command line.
5149
5150 Last_Binder_Switch := Binder_Switches.Last;
5151 Last_Linker_Switch := Linker_Switches.Last;
5152
5153 Check_Steps;
5154
5155 -- Add binder switches from the project file for the first main
5156
5157 if Do_Bind_Step and then Binder_Package /= No_Package then
5158 if Verbose_Mode then
5159 Write_Str ("Adding binder switches for """);
5160 Write_Str (Main_Unit_File_Name);
5161 Write_Line (""".");
5162 end if;
5163
5164 Add_Switches
5165 (Project_Node_Tree => Project_Node_Tree,
5166 File_Name => Main_Unit_File_Name,
5167 Index => Main_Index,
5168 The_Package => Binder_Package,
5169 Program => Binder);
5170 end if;
5171
5172 -- Add linker switches from the project file for the first main
5173
5174 if Do_Link_Step and then Linker_Package /= No_Package then
5175 if Verbose_Mode then
5176 Write_Str ("Adding linker switches for""");
5177 Write_Str (Main_Unit_File_Name);
5178 Write_Line (""".");
5179 end if;
5180
5181 Add_Switches
5182 (Project_Node_Tree => Project_Node_Tree,
5183 File_Name => Main_Unit_File_Name,
5184 Index => Main_Index,
5185 The_Package => Linker_Package,
5186 Program => Linker);
5187 end if;
5188 end;
5189 end if;
5190
5191 -- The combination of -f -u and one or several mains on the command line
5192 -- implies -a.
5193
5194 if Force_Compilations
5195 and then Unique_Compile
5196 and then not Unique_Compile_All_Projects
5197 and then Main_On_Command_Line
5198 then
5199 Check_Readonly_Files := True;
5200 Must_Compile := True;
5201 end if;
5202
5203 if Main_Project /= No_Project
5204 and then not Must_Compile
5205 and then Main_Project.Externally_Built
5206 then
5207 Make_Failed
5208 ("nothing to do for a main project that is externally built");
5209 end if;
5210
5211 -- Get the target parameters, which are only needed for a couple of
5212 -- cases in gnatmake. Protect against an exception, such as the case of
5213 -- system.ads missing from the library, and fail gracefully.
5214
5215 begin
5216 Targparm.Get_Target_Parameters;
5217 exception
5218 when Unrecoverable_Error =>
5219 Make_Failed ("*** make failed.");
5220 end;
5221
5222 -- Special processing for VM targets
5223
5224 if Targparm.VM_Target /= No_VM then
5225
5226 -- Set proper processing commands
5227
5228 case Targparm.VM_Target is
5229 when Targparm.JVM_Target =>
5230
5231 -- Do not check for an object file (".o") when compiling to
5232 -- JVM machine since ".class" files are generated instead.
5233
5234 Check_Object_Consistency := False;
5235 Gcc := new String'("jvm-gnatcompile");
5236
5237 when Targparm.CLI_Target =>
5238 Gcc := new String'("dotnet-gnatcompile");
5239
5240 when Targparm.No_VM =>
5241 raise Program_Error;
5242 end case;
5243 end if;
5244
5245 Display_Commands (not Quiet_Output);
5246
5247 Check_Steps;
5248
5249 if Main_Project /= No_Project then
5250
5251 -- For all library project, if the library file does not exist, put
5252 -- all the project sources in the queue, and flag the project so that
5253 -- the library is generated.
5254
5255 if not Unique_Compile
5256 and then MLib.Tgt.Support_For_Libraries /= Prj.None
5257 then
5258 declare
5259 Proj : Project_List;
5260
5261 begin
5262 Proj := Project_Tree.Projects;
5263 while Proj /= null loop
5264 if Proj.Project.Library then
5265 Proj.Project.Need_To_Build_Lib :=
5266 not MLib.Tgt.Library_Exists_For
5267 (Proj.Project, Project_Tree)
5268 and then not Proj.Project.Externally_Built;
5269
5270 if Proj.Project.Need_To_Build_Lib then
5271
5272 -- If there is no object directory, then it will be
5273 -- impossible to build the library. So fail
5274 -- immediately.
5275
5276 if
5277 Proj.Project.Object_Directory = No_Path_Information
5278 then
5279 Make_Failed
5280 ("no object files to build library for project """
5281 & Get_Name_String (Proj.Project.Name)
5282 & """");
5283 Proj.Project.Need_To_Build_Lib := False;
5284
5285 else
5286 if Verbose_Mode then
5287 Write_Str
5288 ("Library file does not exist for project """);
5289 Write_Str (Get_Name_String (Proj.Project.Name));
5290 Write_Line ("""");
5291 end if;
5292
5293 Insert_Project_Sources
5294 (The_Project => Proj.Project,
5295 All_Projects => False,
5296 Into_Q => True);
5297 end if;
5298 end if;
5299 end if;
5300
5301 Proj := Proj.Next;
5302 end loop;
5303 end;
5304 end if;
5305
5306 -- If a relative path output file has been specified, we add the
5307 -- exec directory.
5308
5309 for J in reverse 1 .. Saved_Linker_Switches.Last - 1 loop
5310 if Saved_Linker_Switches.Table (J).all = Output_Flag.all then
5311 declare
5312 Exec_File_Name : constant String :=
5313 Saved_Linker_Switches.Table (J + 1).all;
5314
5315 begin
5316 if not Is_Absolute_Path (Exec_File_Name) then
5317 Get_Name_String (Main_Project.Exec_Directory.Name);
5318
5319 if not
5320 Is_Directory_Separator (Name_Buffer (Name_Len))
5321 then
5322 Add_Char_To_Name_Buffer (Directory_Separator);
5323 end if;
5324
5325 Add_Str_To_Name_Buffer (Exec_File_Name);
5326 Saved_Linker_Switches.Table (J + 1) :=
5327 new String'(Name_Buffer (1 .. Name_Len));
5328 end if;
5329 end;
5330
5331 exit;
5332 end if;
5333 end loop;
5334
5335 -- If we are using a project file, for relative paths we add the
5336 -- current working directory for any relative path on the command
5337 -- line and the project directory, for any relative path in the
5338 -- project file.
5339
5340 declare
5341 Dir_Path : constant String :=
5342 Get_Name_String (Main_Project.Directory.Name);
5343 begin
5344 for J in 1 .. Binder_Switches.Last loop
5345 Test_If_Relative_Path
5346 (Binder_Switches.Table (J),
5347 Parent => Dir_Path, Including_L_Switch => False);
5348 end loop;
5349
5350 for J in 1 .. Saved_Binder_Switches.Last loop
5351 Test_If_Relative_Path
5352 (Saved_Binder_Switches.Table (J),
5353 Parent => Current_Work_Dir.all, Including_L_Switch => False);
5354 end loop;
5355
5356 for J in 1 .. Linker_Switches.Last loop
5357 Test_If_Relative_Path
5358 (Linker_Switches.Table (J), Parent => Dir_Path);
5359 end loop;
5360
5361 for J in 1 .. Saved_Linker_Switches.Last loop
5362 Test_If_Relative_Path
5363 (Saved_Linker_Switches.Table (J),
5364 Parent => Current_Work_Dir.all);
5365 end loop;
5366
5367 for J in 1 .. Gcc_Switches.Last loop
5368 Test_If_Relative_Path
5369 (Gcc_Switches.Table (J),
5370 Parent => Dir_Path,
5371 Including_Non_Switch => False);
5372 end loop;
5373
5374 for J in 1 .. Saved_Gcc_Switches.Last loop
5375 Test_If_Relative_Path
5376 (Saved_Gcc_Switches.Table (J),
5377 Parent => Current_Work_Dir.all,
5378 Including_Non_Switch => False);
5379 end loop;
5380 end;
5381 end if;
5382
5383 -- We now put in the Binder_Switches and Linker_Switches tables, the
5384 -- binder and linker switches of the command line that have been put in
5385 -- the Saved_ tables. If a project file was used, then the command line
5386 -- switches will follow the project file switches.
5387
5388 for J in 1 .. Saved_Binder_Switches.Last loop
5389 Add_Switch
5390 (Saved_Binder_Switches.Table (J),
5391 Binder,
5392 And_Save => False);
5393 end loop;
5394
5395 for J in 1 .. Saved_Linker_Switches.Last loop
5396 Add_Switch
5397 (Saved_Linker_Switches.Table (J),
5398 Linker,
5399 And_Save => False);
5400 end loop;
5401
5402 -- If no project file is used, we just put the gcc switches
5403 -- from the command line in the Gcc_Switches table.
5404
5405 if Main_Project = No_Project then
5406 for J in 1 .. Saved_Gcc_Switches.Last loop
5407 Add_Switch
5408 (Saved_Gcc_Switches.Table (J), Compiler, And_Save => False);
5409 end loop;
5410
5411 else
5412 -- If there is a project, put the command line gcc switches in the
5413 -- variable The_Saved_Gcc_Switches. They are going to be used later
5414 -- in procedure Compile_Sources.
5415
5416 The_Saved_Gcc_Switches :=
5417 new Argument_List (1 .. Saved_Gcc_Switches.Last + 1);
5418
5419 for J in 1 .. Saved_Gcc_Switches.Last loop
5420 The_Saved_Gcc_Switches (J) := Saved_Gcc_Switches.Table (J);
5421 end loop;
5422
5423 -- We never use gnat.adc when a project file is used
5424
5425 The_Saved_Gcc_Switches (The_Saved_Gcc_Switches'Last) := No_gnat_adc;
5426 end if;
5427
5428 -- If there was a --GCC, --GNATBIND or --GNATLINK switch on the command
5429 -- line, then we have to use it, even if there was another switch in
5430 -- the project file.
5431
5432 if Saved_Gcc /= null then
5433 Gcc := Saved_Gcc;
5434 end if;
5435
5436 if Saved_Gnatbind /= null then
5437 Gnatbind := Saved_Gnatbind;
5438 end if;
5439
5440 if Saved_Gnatlink /= null then
5441 Gnatlink := Saved_Gnatlink;
5442 end if;
5443
5444 Gcc_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gcc.all);
5445 Gnatbind_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gnatbind.all);
5446 Gnatlink_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Gnatlink.all);
5447
5448 -- If we have specified -j switch both from the project file
5449 -- and on the command line, the one from the command line takes
5450 -- precedence.
5451
5452 if Saved_Maximum_Processes = 0 then
5453 Saved_Maximum_Processes := Maximum_Processes;
5454 end if;
5455
5456 -- Allocate as many temporary mapping file names as the maximum number
5457 -- of compilations processed, for each possible project.
5458
5459 declare
5460 Data : Project_Compilation_Access;
5461 Proj : Project_List := Project_Tree.Projects;
5462 begin
5463 while Proj /= null loop
5464 Data := new Project_Compilation_Data'
5465 (Mapping_File_Names => new Temp_Path_Names
5466 (1 .. Saved_Maximum_Processes),
5467 Last_Mapping_File_Names => 0,
5468 Free_Mapping_File_Indices => new Free_File_Indices
5469 (1 .. Saved_Maximum_Processes),
5470 Last_Free_Indices => 0);
5471
5472 Project_Compilation_Htable.Set
5473 (Project_Compilation, Proj.Project, Data);
5474 Proj := Proj.Next;
5475 end loop;
5476
5477 Data := new Project_Compilation_Data'
5478 (Mapping_File_Names => new Temp_Path_Names
5479 (1 .. Saved_Maximum_Processes),
5480 Last_Mapping_File_Names => 0,
5481 Free_Mapping_File_Indices => new Free_File_Indices
5482 (1 .. Saved_Maximum_Processes),
5483 Last_Free_Indices => 0);
5484
5485 Project_Compilation_Htable.Set
5486 (Project_Compilation, No_Project, Data);
5487 end;
5488
5489 Bad_Compilation.Init;
5490
5491 -- If project files are used, create the mapping of all the sources, so
5492 -- that the correct paths will be found. Otherwise, if there is a file
5493 -- which is not a source with the same name in a source directory this
5494 -- file may be incorrectly found.
5495
5496 if Main_Project /= No_Project then
5497 Prj.Env.Create_Mapping (Project_Tree);
5498 end if;
5499
5500 Current_Main_Index := Main_Index;
5501
5502 -- Here is where the make process is started
5503
5504 -- We do the same process for each main
5505
5506 Multiple_Main_Loop : for N_File in 1 .. Osint.Number_Of_Files loop
5507
5508 -- First, find the executable name and path
5509
5510 Executable := No_File;
5511 Executable_Obsolete := False;
5512 Non_Std_Executable :=
5513 Targparm.Executable_Extension_On_Target /= No_Name;
5514
5515 -- Look inside the linker switches to see if the name of the final
5516 -- executable program was specified.
5517
5518 for J in reverse Linker_Switches.First .. Linker_Switches.Last loop
5519 if Linker_Switches.Table (J).all = Output_Flag.all then
5520 pragma Assert (J < Linker_Switches.Last);
5521
5522 -- We cannot specify a single executable for several main
5523 -- subprograms
5524
5525 if Osint.Number_Of_Files > 1 then
5526 Fail
5527 ("cannot specify a single executable for several mains");
5528 end if;
5529
5530 Name_Len := 0;
5531 Add_Str_To_Name_Buffer (Linker_Switches.Table (J + 1).all);
5532 Executable := Name_Enter;
5533
5534 Verbose_Msg (Executable, "final executable");
5535 end if;
5536 end loop;
5537
5538 -- If the name of the final executable program was not specified then
5539 -- construct it from the main input file.
5540
5541 if Executable = No_File then
5542 if Main_Project = No_Project then
5543 Executable := Executable_Name (Strip_Suffix (Main_Source_File));
5544
5545 else
5546 -- If we are using a project file, we attempt to remove the
5547 -- body (or spec) termination of the main subprogram. We find
5548 -- it the naming scheme of the project file. This avoids
5549 -- generating an executable "main.2" for a main subprogram
5550 -- "main.2.ada", when the body termination is ".2.ada".
5551
5552 Executable :=
5553 Prj.Util.Executable_Of
5554 (Main_Project, Project_Tree, Main_Source_File, Main_Index);
5555 end if;
5556 end if;
5557
5558 if Main_Project /= No_Project
5559 and then Main_Project.Exec_Directory /= No_Path_Information
5560 then
5561 declare
5562 Exec_File_Name : constant String :=
5563 Get_Name_String (Executable);
5564
5565 begin
5566 if not Is_Absolute_Path (Exec_File_Name) then
5567 Get_Name_String (Main_Project.Exec_Directory.Display_Name);
5568
5569 if Name_Buffer (Name_Len) /= Directory_Separator then
5570 Add_Char_To_Name_Buffer (Directory_Separator);
5571 end if;
5572
5573 Add_Str_To_Name_Buffer (Exec_File_Name);
5574 Executable := Name_Find;
5575 end if;
5576
5577 Non_Std_Executable := True;
5578 end;
5579 end if;
5580
5581 if Do_Compile_Step then
5582 Recursive_Compilation_Step : declare
5583 Args : Argument_List (1 .. Gcc_Switches.Last);
5584
5585 First_Compiled_File : File_Name_Type;
5586 Youngest_Obj_File : File_Name_Type;
5587 Youngest_Obj_Stamp : Time_Stamp_Type;
5588
5589 Executable_Stamp : Time_Stamp_Type;
5590 -- Executable is the final executable program
5591 -- ??? comment seems unrelated to declaration
5592
5593 Library_Rebuilt : Boolean := False;
5594
5595 begin
5596 for J in 1 .. Gcc_Switches.Last loop
5597 Args (J) := Gcc_Switches.Table (J);
5598 end loop;
5599
5600 -- Now we invoke Compile_Sources for the current main
5601
5602 Compile_Sources
5603 (Main_Source => Main_Source_File,
5604 Args => Args,
5605 First_Compiled_File => First_Compiled_File,
5606 Most_Recent_Obj_File => Youngest_Obj_File,
5607 Most_Recent_Obj_Stamp => Youngest_Obj_Stamp,
5608 Main_Unit => Is_Main_Unit,
5609 Main_Index => Current_Main_Index,
5610 Compilation_Failures => Compilation_Failures,
5611 Check_Readonly_Files => Check_Readonly_Files,
5612 Do_Not_Execute => Do_Not_Execute,
5613 Force_Compilations => Force_Compilations,
5614 In_Place_Mode => In_Place_Mode,
5615 Keep_Going => Keep_Going,
5616 Initialize_ALI_Data => True,
5617 Max_Process => Saved_Maximum_Processes);
5618
5619 if Verbose_Mode then
5620 Write_Str ("End of compilation");
5621 Write_Eol;
5622 end if;
5623
5624 -- Make sure the queue will be reinitialized for the next round
5625
5626 First_Q_Initialization := True;
5627
5628 Total_Compilation_Failures :=
5629 Total_Compilation_Failures + Compilation_Failures;
5630
5631 if Total_Compilation_Failures /= 0 then
5632 if Keep_Going then
5633 goto Next_Main;
5634
5635 else
5636 List_Bad_Compilations;
5637 Report_Compilation_Failed;
5638 end if;
5639 end if;
5640
5641 -- Regenerate libraries, if there are any and if object files
5642 -- have been regenerated.
5643
5644 if Main_Project /= No_Project
5645 and then MLib.Tgt.Support_For_Libraries /= Prj.None
5646 and then (Do_Bind_Step
5647 or Unique_Compile_All_Projects
5648 or not Compile_Only)
5649 and then (Do_Link_Step or else N_File = Osint.Number_Of_Files)
5650 then
5651 Library_Projs.Init;
5652
5653 declare
5654 Depth : Natural;
5655 Current : Natural;
5656 Proj1 : Project_List;
5657
5658 procedure Add_To_Library_Projs (Proj : Project_Id);
5659 -- Add project Project to table Library_Projs in
5660 -- decreasing depth order.
5661
5662 --------------------------
5663 -- Add_To_Library_Projs --
5664 --------------------------
5665
5666 procedure Add_To_Library_Projs (Proj : Project_Id) is
5667 Prj : Project_Id;
5668
5669 begin
5670 Library_Projs.Increment_Last;
5671 Depth := Proj.Depth;
5672
5673 -- Put the projects in decreasing depth order, so that
5674 -- if libA depends on libB, libB is first in order.
5675
5676 Current := Library_Projs.Last;
5677 while Current > 1 loop
5678 Prj := Library_Projs.Table (Current - 1);
5679 exit when Prj.Depth >= Depth;
5680 Library_Projs.Table (Current) := Prj;
5681 Current := Current - 1;
5682 end loop;
5683
5684 Library_Projs.Table (Current) := Proj;
5685 end Add_To_Library_Projs;
5686
5687 -- Start of processing for ??? (should name declare block
5688 -- or probably better, break this out as a nested proc).
5689
5690 begin
5691 -- Put in Library_Projs table all library project file
5692 -- ids when the library need to be rebuilt.
5693
5694 Proj1 := Project_Tree.Projects;
5695 while Proj1 /= null loop
5696 if Proj1.Project.Standalone_Library then
5697 Stand_Alone_Libraries := True;
5698 end if;
5699
5700 if Proj1.Project.Library then
5701 MLib.Prj.Check_Library
5702 (Proj1.Project, Project_Tree);
5703 end if;
5704
5705 if Proj1.Project.Need_To_Build_Lib then
5706 Add_To_Library_Projs (Proj1.Project);
5707 end if;
5708
5709 Proj1 := Proj1.Next;
5710 end loop;
5711
5712 -- Check if importing libraries should be regenerated
5713 -- because at least an imported library will be
5714 -- regenerated or is more recent.
5715
5716 Proj1 := Project_Tree.Projects;
5717 while Proj1 /= null loop
5718 if Proj1.Project.Library
5719 and then Proj1.Project.Library_Kind /= Static
5720 and then not Proj1.Project.Need_To_Build_Lib
5721 and then not Proj1.Project.Externally_Built
5722 then
5723 declare
5724 List : Project_List;
5725 Proj2 : Project_Id;
5726 Rebuild : Boolean := False;
5727
5728 Lib_Timestamp1 : constant Time_Stamp_Type :=
5729 Proj1.Project.Library_TS;
5730
5731 begin
5732 List := Proj1.Project.All_Imported_Projects;
5733 while List /= null loop
5734 Proj2 := List.Project;
5735
5736 if Proj2.Library then
5737 if Proj2.Need_To_Build_Lib
5738 or else
5739 (Lib_Timestamp1 < Proj2.Library_TS)
5740 then
5741 Rebuild := True;
5742 exit;
5743 end if;
5744 end if;
5745
5746 List := List.Next;
5747 end loop;
5748
5749 if Rebuild then
5750 Proj1.Project.Need_To_Build_Lib := True;
5751 Add_To_Library_Projs (Proj1.Project);
5752 end if;
5753 end;
5754 end if;
5755
5756 Proj1 := Proj1.Next;
5757 end loop;
5758
5759 -- Reset the flags Need_To_Build_Lib for the next main,
5760 -- to avoid rebuilding libraries uselessly.
5761
5762 Proj1 := Project_Tree.Projects;
5763 while Proj1 /= null loop
5764 Proj1.Project.Need_To_Build_Lib := False;
5765 Proj1 := Proj1.Next;
5766 end loop;
5767 end;
5768
5769 -- Build the libraries, if any need to be built
5770
5771 for J in 1 .. Library_Projs.Last loop
5772 Library_Rebuilt := True;
5773
5774 -- If a library is rebuilt, then executables are obsolete
5775
5776 Executable_Obsolete := True;
5777
5778 MLib.Prj.Build_Library
5779 (For_Project => Library_Projs.Table (J),
5780 In_Tree => Project_Tree,
5781 Gnatbind => Gnatbind.all,
5782 Gnatbind_Path => Gnatbind_Path,
5783 Gcc => Gcc.all,
5784 Gcc_Path => Gcc_Path);
5785 end loop;
5786 end if;
5787
5788 if List_Dependencies then
5789 if First_Compiled_File /= No_File then
5790 Inform
5791 (First_Compiled_File,
5792 "must be recompiled. Can't generate dependence list.");
5793 else
5794 List_Depend;
5795 end if;
5796
5797 elsif First_Compiled_File = No_File
5798 and then not Do_Bind_Step
5799 and then not Quiet_Output
5800 and then not Library_Rebuilt
5801 and then Osint.Number_Of_Files = 1
5802 then
5803 Inform (Msg => "objects up to date.");
5804
5805 elsif Do_Not_Execute
5806 and then First_Compiled_File /= No_File
5807 then
5808 Write_Name (First_Compiled_File);
5809 Write_Eol;
5810 end if;
5811
5812 -- Stop after compile step if any of:
5813
5814 -- 1) -n (Do_Not_Execute) specified
5815
5816 -- 2) -M (List_Dependencies) specified (also sets
5817 -- Do_Not_Execute above, so this is probably superfluous).
5818
5819 -- 3) -c (Compile_Only) specified, but not -b (Bind_Only)
5820
5821 -- 4) Made unit cannot be a main unit
5822
5823 if ((Do_Not_Execute
5824 or List_Dependencies
5825 or not Do_Bind_Step
5826 or not Is_Main_Unit)
5827 and then not No_Main_Subprogram
5828 and then not Build_Bind_And_Link_Full_Project)
5829 or else Unique_Compile
5830 then
5831 if Osint.Number_Of_Files = 1 then
5832 exit Multiple_Main_Loop;
5833
5834 else
5835 goto Next_Main;
5836 end if;
5837 end if;
5838
5839 -- If the objects were up-to-date check if the executable file
5840 -- is also up-to-date. For now always bind and link on the JVM
5841 -- since there is currently no simple way to check whether
5842 -- objects are up-to-date.
5843
5844 if Targparm.VM_Target /= JVM_Target
5845 and then First_Compiled_File = No_File
5846 then
5847 Executable_Stamp := File_Stamp (Executable);
5848
5849 if not Executable_Obsolete then
5850 Executable_Obsolete :=
5851 Youngest_Obj_Stamp > Executable_Stamp;
5852 end if;
5853
5854 if not Executable_Obsolete then
5855 for Index in reverse 1 .. Dependencies.Last loop
5856 if Is_In_Obsoleted
5857 (Dependencies.Table (Index).Depends_On)
5858 then
5859 Enter_Into_Obsoleted
5860 (Dependencies.Table (Index).This);
5861 end if;
5862 end loop;
5863
5864 Executable_Obsolete := Is_In_Obsoleted (Main_Source_File);
5865 Dependencies.Init;
5866 end if;
5867
5868 if not Executable_Obsolete then
5869
5870 -- If no Ada object files obsolete the executable, check
5871 -- for younger or missing linker files.
5872
5873 Check_Linker_Options
5874 (Executable_Stamp,
5875 Youngest_Obj_File,
5876 Youngest_Obj_Stamp);
5877
5878 Executable_Obsolete := Youngest_Obj_File /= No_File;
5879 end if;
5880
5881 -- Check if any library file is more recent than the
5882 -- executable: there may be an externally built library
5883 -- file that has been modified.
5884
5885 if not Executable_Obsolete
5886 and then Main_Project /= No_Project
5887 then
5888 declare
5889 Proj1 : Project_List;
5890
5891 begin
5892 Proj1 := Project_Tree.Projects;
5893 while Proj1 /= null loop
5894 if Proj1.Project.Library
5895 and then
5896 Proj1.Project.Library_TS > Executable_Stamp
5897 then
5898 Executable_Obsolete := True;
5899 Youngest_Obj_Stamp := Proj1.Project.Library_TS;
5900 Name_Len := 0;
5901 Add_Str_To_Name_Buffer ("library ");
5902 Add_Str_To_Name_Buffer
5903 (Get_Name_String (Proj1.Project.Library_Name));
5904 Youngest_Obj_File := Name_Find;
5905 exit;
5906 end if;
5907
5908 Proj1 := Proj1.Next;
5909 end loop;
5910 end;
5911 end if;
5912
5913 -- Return if the executable is up to date and otherwise
5914 -- motivate the relink/rebind.
5915
5916 if not Executable_Obsolete then
5917 if not Quiet_Output then
5918 Inform (Executable, "up to date.");
5919 end if;
5920
5921 if Osint.Number_Of_Files = 1 then
5922 exit Multiple_Main_Loop;
5923
5924 else
5925 goto Next_Main;
5926 end if;
5927 end if;
5928
5929 if Executable_Stamp (1) = ' ' then
5930 if not No_Main_Subprogram then
5931 Verbose_Msg (Executable, "missing.", Prefix => " ");
5932 end if;
5933
5934 elsif Youngest_Obj_Stamp (1) = ' ' then
5935 Verbose_Msg
5936 (Youngest_Obj_File, "missing.", Prefix => " ");
5937
5938 elsif Youngest_Obj_Stamp > Executable_Stamp then
5939 Verbose_Msg
5940 (Youngest_Obj_File,
5941 "(" & String (Youngest_Obj_Stamp) & ") newer than",
5942 Executable,
5943 "(" & String (Executable_Stamp) & ")");
5944
5945 else
5946 Verbose_Msg
5947 (Executable, "needs to be rebuilt", Prefix => " ");
5948
5949 end if;
5950 end if;
5951 end Recursive_Compilation_Step;
5952 end if;
5953
5954 -- For binding and linking, we need to be in the object directory of
5955 -- the main project.
5956
5957 if Main_Project /= No_Project then
5958 Change_To_Object_Directory (Main_Project);
5959 end if;
5960
5961 -- If we are here, it means that we need to rebuilt the current main,
5962 -- so we set Executable_Obsolete to True to make sure that subsequent
5963 -- mains will be rebuilt.
5964
5965 Main_ALI_In_Place_Mode_Step : declare
5966 ALI_File : File_Name_Type;
5967 Src_File : File_Name_Type;
5968
5969 begin
5970 Src_File := Strip_Directory (Main_Source_File);
5971 ALI_File := Lib_File_Name (Src_File, Current_Main_Index);
5972 Main_ALI_File := Full_Lib_File_Name (ALI_File);
5973
5974 -- When In_Place_Mode, the library file can be located in the
5975 -- Main_Source_File directory which may not be present in the
5976 -- library path. If it is not present then use the corresponding
5977 -- library file name.
5978
5979 if Main_ALI_File = No_File and then In_Place_Mode then
5980 Get_Name_String (Get_Directory (Full_Source_Name (Src_File)));
5981 Get_Name_String_And_Append (ALI_File);
5982 Main_ALI_File := Name_Find;
5983 Main_ALI_File := Full_Lib_File_Name (Main_ALI_File);
5984 end if;
5985
5986 if Main_ALI_File = No_File then
5987 Make_Failed ("could not find the main ALI file");
5988 end if;
5989 end Main_ALI_In_Place_Mode_Step;
5990
5991 if Do_Bind_Step then
5992 Bind_Step : declare
5993 Args : Argument_List
5994 (Binder_Switches.First .. Binder_Switches.Last + 2);
5995 -- The arguments for the invocation of gnatbind
5996
5997 Last_Arg : Natural := Binder_Switches.Last;
5998 -- Index of the last argument in Args
5999
6000 Shared_Libs : Boolean := False;
6001 -- Set to True when there are shared library project files or
6002 -- when gnatbind is invoked with -shared.
6003
6004 Proj : Project_List;
6005
6006 begin
6007 -- Check if there are shared libraries, so that gnatbind is
6008 -- called with -shared. Check also if gnatbind is called with
6009 -- -shared, so that gnatlink is called with -shared-libgcc
6010 -- ensuring that the shared version of libgcc will be used.
6011
6012 if Main_Project /= No_Project
6013 and then MLib.Tgt.Support_For_Libraries /= Prj.None
6014 then
6015 Proj := Project_Tree.Projects;
6016 while Proj /= null loop
6017 if Proj.Project.Library
6018 and then Proj.Project.Library_Kind /= Static
6019 then
6020 Shared_Libs := True;
6021 Bind_Shared := Shared_Switch'Access;
6022 exit;
6023 end if;
6024 Proj := Proj.Next;
6025 end loop;
6026 end if;
6027
6028 -- Check now for switch -shared
6029
6030 if not Shared_Libs then
6031 for J in Binder_Switches.First .. Last_Arg loop
6032 if Binder_Switches.Table (J).all = "-shared" then
6033 Shared_Libs := True;
6034 exit;
6035 end if;
6036 end loop;
6037 end if;
6038
6039 -- If shared libraries present, invoke gnatlink with
6040 -- -shared-libgcc.
6041
6042 if Shared_Libs then
6043 Link_With_Shared_Libgcc := Shared_Libgcc_Switch'Access;
6044 end if;
6045
6046 -- Get all the binder switches
6047
6048 for J in Binder_Switches.First .. Last_Arg loop
6049 Args (J) := Binder_Switches.Table (J);
6050 end loop;
6051
6052 if Stand_Alone_Libraries then
6053 Last_Arg := Last_Arg + 1;
6054 Args (Last_Arg) := Force_Elab_Flags_String'Access;
6055 end if;
6056
6057 if Main_Project /= No_Project then
6058
6059 -- Put all the source directories in ADA_INCLUDE_PATH,
6060 -- and all the object directories in ADA_OBJECTS_PATH,
6061 -- except those of library projects.
6062
6063 Prj.Env.Set_Ada_Paths
6064 (Main_Project, Project_Tree, Use_Include_Path_File);
6065
6066 -- If switch -C was specified, create a binder mapping file
6067
6068 if Create_Mapping_File then
6069 Create_Binder_Mapping_File (Args, Last_Arg);
6070 end if;
6071
6072 end if;
6073
6074 begin
6075 Bind (Main_ALI_File,
6076 Bind_Shared.all & Args (Args'First .. Last_Arg));
6077
6078 exception
6079 when others =>
6080
6081 -- Delete the temporary mapping file if one was created
6082
6083 if Mapping_Path /= No_Path then
6084 Delete_Temporary_File (Project_Tree, Mapping_Path);
6085 end if;
6086
6087 -- And reraise the exception
6088
6089 raise;
6090 end;
6091
6092 -- If -dn was not specified, delete the temporary mapping file
6093 -- if one was created.
6094
6095 if Mapping_Path /= No_Path then
6096 Delete_Temporary_File (Project_Tree, Mapping_Path);
6097 end if;
6098 end Bind_Step;
6099 end if;
6100
6101 if Do_Link_Step then
6102 Link_Step : declare
6103 Linker_Switches_Last : constant Integer := Linker_Switches.Last;
6104 Path_Option : constant String_Access :=
6105 MLib.Linker_Library_Path_Option;
6106 Libraries_Present : Boolean := False;
6107 Current : Natural;
6108 Proj2 : Project_Id;
6109 Depth : Natural;
6110 Proj1 : Project_List;
6111
6112 begin
6113 if not Run_Path_Option then
6114 Linker_Switches.Increment_Last;
6115 Linker_Switches.Table (Linker_Switches.Last) :=
6116 new String'("-R");
6117 end if;
6118
6119 if Main_Project /= No_Project then
6120 Library_Paths.Set_Last (0);
6121 Library_Projs.Init;
6122
6123 if MLib.Tgt.Support_For_Libraries /= Prj.None then
6124
6125 -- Check for library projects
6126
6127 Proj1 := Project_Tree.Projects;
6128 while Proj1 /= null loop
6129 if Proj1.Project /= Main_Project
6130 and then Proj1.Project.Library
6131 then
6132 -- Add this project to table Library_Projs
6133
6134 Libraries_Present := True;
6135 Depth := Proj1.Project.Depth;
6136 Library_Projs.Increment_Last;
6137 Current := Library_Projs.Last;
6138
6139 -- Any project with a greater depth should be
6140 -- after this project in the list.
6141
6142 while Current > 1 loop
6143 Proj2 := Library_Projs.Table (Current - 1);
6144 exit when Proj2.Depth <= Depth;
6145 Library_Projs.Table (Current) := Proj2;
6146 Current := Current - 1;
6147 end loop;
6148
6149 Library_Projs.Table (Current) := Proj1.Project;
6150
6151 -- If it is not a static library and path option
6152 -- is set, add it to the Library_Paths table.
6153
6154 if Proj1.Project.Library_Kind /= Static
6155 and then Path_Option /= null
6156 then
6157 Library_Paths.Increment_Last;
6158 Library_Paths.Table (Library_Paths.Last) :=
6159 new String'
6160 (Get_Name_String
6161 (Proj1.Project.Library_Dir.Display_Name));
6162 end if;
6163 end if;
6164
6165 Proj1 := Proj1.Next;
6166 end loop;
6167
6168 for Index in 1 .. Library_Projs.Last loop
6169
6170 -- Add the -L switch
6171
6172 Linker_Switches.Increment_Last;
6173 Linker_Switches.Table (Linker_Switches.Last) :=
6174 new String'("-L" &
6175 Get_Name_String
6176 (Library_Projs.Table (Index).
6177 Library_Dir.Display_Name));
6178
6179 -- Add the -l switch
6180
6181 Linker_Switches.Increment_Last;
6182 Linker_Switches.Table (Linker_Switches.Last) :=
6183 new String'("-l" &
6184 Get_Name_String
6185 (Library_Projs.Table (Index).
6186 Library_Name));
6187 end loop;
6188 end if;
6189
6190 if Libraries_Present then
6191
6192 -- If Path_Option is not null, create the switch
6193 -- ("-Wl,-rpath," or equivalent) with all the non static
6194 -- library dirs plus the standard GNAT library dir.
6195 -- We do that only if Run_Path_Option is True
6196 -- (not disabled by -R switch).
6197
6198 if Run_Path_Option and then Path_Option /= null then
6199 declare
6200 Option : String_Access;
6201 Length : Natural := Path_Option'Length;
6202 Current : Natural;
6203
6204 begin
6205 if MLib.Separate_Run_Path_Options then
6206
6207 -- We are going to create one switch of the form
6208 -- "-Wl,-rpath,dir_N" for each directory to
6209 -- consider.
6210
6211 -- One switch for each library directory
6212
6213 for Index in
6214 Library_Paths.First .. Library_Paths.Last
6215 loop
6216 Linker_Switches.Increment_Last;
6217 Linker_Switches.Table
6218 (Linker_Switches.Last) := new String'
6219 (Path_Option.all &
6220 Library_Paths.Table (Index).all);
6221 end loop;
6222
6223 -- One switch for the standard GNAT library dir
6224
6225 Linker_Switches.Increment_Last;
6226 Linker_Switches.Table
6227 (Linker_Switches.Last) := new String'
6228 (Path_Option.all & MLib.Utl.Lib_Directory);
6229
6230 else
6231 -- We are going to create one switch of the form
6232 -- "-Wl,-rpath,dir_1:dir_2:dir_3"
6233
6234 for Index in
6235 Library_Paths.First .. Library_Paths.Last
6236 loop
6237 -- Add the length of the library dir plus one
6238 -- for the directory separator.
6239
6240 Length :=
6241 Length +
6242 Library_Paths.Table (Index)'Length + 1;
6243 end loop;
6244
6245 -- Finally, add the length of the standard GNAT
6246 -- library dir.
6247
6248 Length := Length + MLib.Utl.Lib_Directory'Length;
6249 Option := new String (1 .. Length);
6250 Option (1 .. Path_Option'Length) :=
6251 Path_Option.all;
6252 Current := Path_Option'Length;
6253
6254 -- Put each library dir followed by a dir
6255 -- separator.
6256
6257 for Index in
6258 Library_Paths.First .. Library_Paths.Last
6259 loop
6260 Option
6261 (Current + 1 ..
6262 Current +
6263 Library_Paths.Table (Index)'Length) :=
6264 Library_Paths.Table (Index).all;
6265 Current :=
6266 Current +
6267 Library_Paths.Table (Index)'Length + 1;
6268 Option (Current) := Path_Separator;
6269 end loop;
6270
6271 -- Finally put the standard GNAT library dir
6272
6273 Option
6274 (Current + 1 ..
6275 Current + MLib.Utl.Lib_Directory'Length) :=
6276 MLib.Utl.Lib_Directory;
6277
6278 -- And add the switch to the linker switches
6279
6280 Linker_Switches.Increment_Last;
6281 Linker_Switches.Table (Linker_Switches.Last) :=
6282 Option;
6283 end if;
6284 end;
6285 end if;
6286
6287 end if;
6288
6289 -- Put the object directories in ADA_OBJECTS_PATH
6290
6291 Prj.Env.Set_Ada_Paths
6292 (Main_Project,
6293 Project_Tree,
6294 Including_Libraries => False,
6295 Include_Path => False);
6296
6297 -- Check for attributes Linker'Linker_Options in projects
6298 -- other than the main project
6299
6300 declare
6301 Linker_Options : constant String_List :=
6302 Linker_Options_Switches
6303 (Main_Project, Project_Tree);
6304 begin
6305 for Option in Linker_Options'Range loop
6306 Linker_Switches.Increment_Last;
6307 Linker_Switches.Table (Linker_Switches.Last) :=
6308 Linker_Options (Option);
6309 end loop;
6310 end;
6311 end if;
6312
6313 declare
6314 Args : Argument_List
6315 (Linker_Switches.First .. Linker_Switches.Last + 2);
6316
6317 Last_Arg : Integer := Linker_Switches.First - 1;
6318 Skip : Boolean := False;
6319
6320 begin
6321 -- Get all the linker switches
6322
6323 for J in Linker_Switches.First .. Linker_Switches.Last loop
6324 if Skip then
6325 Skip := False;
6326
6327 elsif Non_Std_Executable
6328 and then Linker_Switches.Table (J).all = "-o"
6329 then
6330 Skip := True;
6331
6332 -- Here we capture and duplicate the linker argument. We
6333 -- need to do the duplication since the arguments will
6334 -- get normalized. Not doing so will result in calling
6335 -- normalized two times for the same set of arguments if
6336 -- gnatmake is passed multiple mains. This can result in
6337 -- the wrong argument being passed to the linker.
6338
6339 else
6340 Last_Arg := Last_Arg + 1;
6341 Args (Last_Arg) :=
6342 new String'(Linker_Switches.Table (J).all);
6343 end if;
6344 end loop;
6345
6346 -- If need be, add the -o switch
6347
6348 if Non_Std_Executable then
6349 Last_Arg := Last_Arg + 1;
6350 Args (Last_Arg) := new String'("-o");
6351 Last_Arg := Last_Arg + 1;
6352 Args (Last_Arg) :=
6353 new String'(Get_Name_String (Executable));
6354 end if;
6355
6356 -- And invoke the linker
6357
6358 declare
6359 Success : Boolean := False;
6360 begin
6361 Link (Main_ALI_File,
6362 Link_With_Shared_Libgcc.all &
6363 Args (Args'First .. Last_Arg),
6364 Success);
6365
6366 if Success then
6367 Successful_Links.Increment_Last;
6368 Successful_Links.Table (Successful_Links.Last) :=
6369 Main_ALI_File;
6370
6371 elsif Osint.Number_Of_Files = 1
6372 or else not Keep_Going
6373 then
6374 Make_Failed ("*** link failed.");
6375
6376 else
6377 Set_Standard_Error;
6378 Write_Line ("*** link failed");
6379
6380 if Commands_To_Stdout then
6381 Set_Standard_Output;
6382 end if;
6383
6384 Failed_Links.Increment_Last;
6385 Failed_Links.Table (Failed_Links.Last) :=
6386 Main_ALI_File;
6387 end if;
6388 end;
6389 end;
6390
6391 Linker_Switches.Set_Last (Linker_Switches_Last);
6392 end Link_Step;
6393 end if;
6394
6395 -- We go to here when we skip the bind and link steps
6396
6397 <<Next_Main>>
6398
6399 -- We go to the next main, if we did not process the last one
6400
6401 if N_File < Osint.Number_Of_Files then
6402 Main_Source_File := Next_Main_Source;
6403
6404 if Current_File_Index /= No_Index then
6405 Main_Index := Current_File_Index;
6406 end if;
6407
6408 if Main_Project /= No_Project then
6409
6410 -- Find the file name of the main unit
6411
6412 declare
6413 Main_Source_File_Name : constant String :=
6414 Get_Name_String (Main_Source_File);
6415
6416 Main_Unit_File_Name : constant String :=
6417 Prj.Env.
6418 File_Name_Of_Library_Unit_Body
6419 (Name => Main_Source_File_Name,
6420 Project => Main_Project,
6421 In_Tree => Project_Tree,
6422 Main_Project_Only =>
6423 not Unique_Compile);
6424
6425 The_Packages : constant Package_Id :=
6426 Main_Project.Decl.Packages;
6427
6428 Binder_Package : constant Prj.Package_Id :=
6429 Prj.Util.Value_Of
6430 (Name => Name_Binder,
6431 In_Packages => The_Packages,
6432 In_Tree => Project_Tree);
6433
6434 Linker_Package : constant Prj.Package_Id :=
6435 Prj.Util.Value_Of
6436 (Name => Name_Linker,
6437 In_Packages => The_Packages,
6438 In_Tree => Project_Tree);
6439
6440 begin
6441 -- We fail if we cannot find the main source file
6442 -- as an immediate source of the main project file.
6443
6444 if Main_Unit_File_Name = "" then
6445 Make_Failed ('"' & Main_Source_File_Name
6446 & """ is not a unit of project "
6447 & Project_File_Name.all & ".");
6448
6449 else
6450 -- Remove any directory information from the main
6451 -- source file name.
6452
6453 declare
6454 Pos : Natural := Main_Unit_File_Name'Last;
6455
6456 begin
6457 loop
6458 exit when Pos < Main_Unit_File_Name'First
6459 or else
6460 Main_Unit_File_Name (Pos) = Directory_Separator;
6461 Pos := Pos - 1;
6462 end loop;
6463
6464 Name_Len := Main_Unit_File_Name'Last - Pos;
6465
6466 Name_Buffer (1 .. Name_Len) :=
6467 Main_Unit_File_Name
6468 (Pos + 1 .. Main_Unit_File_Name'Last);
6469
6470 Main_Source_File := Name_Find;
6471 end;
6472 end if;
6473
6474 -- We now deal with the binder and linker switches.
6475 -- If no project file is used, there is nothing to do
6476 -- because the binder and linker switches are the same
6477 -- for all mains.
6478
6479 -- Reset the tables Binder_Switches and Linker_Switches
6480
6481 Binder_Switches.Set_Last (Last_Binder_Switch);
6482 Linker_Switches.Set_Last (Last_Linker_Switch);
6483
6484 -- Add binder switches from the project file for this main,
6485 -- if any.
6486
6487 if Do_Bind_Step and then Binder_Package /= No_Package then
6488 if Verbose_Mode then
6489 Write_Str ("Adding binder switches for """);
6490 Write_Str (Main_Unit_File_Name);
6491 Write_Line (""".");
6492 end if;
6493
6494 Add_Switches
6495 (Project_Node_Tree => Project_Node_Tree,
6496 File_Name => Main_Unit_File_Name,
6497 Index => Main_Index,
6498 The_Package => Binder_Package,
6499 Program => Binder);
6500 end if;
6501
6502 -- Add linker switches from the project file for this main,
6503 -- if any.
6504
6505 if Do_Link_Step and then Linker_Package /= No_Package then
6506 if Verbose_Mode then
6507 Write_Str ("Adding linker switches for""");
6508 Write_Str (Main_Unit_File_Name);
6509 Write_Line (""".");
6510 end if;
6511
6512 Add_Switches
6513 (Project_Node_Tree => Project_Node_Tree,
6514 File_Name => Main_Unit_File_Name,
6515 Index => Main_Index,
6516 The_Package => Linker_Package,
6517 Program => Linker);
6518 end if;
6519
6520 -- As we are using a project file, for relative paths we add
6521 -- the current working directory for any relative path on
6522 -- the command line and the project directory, for any
6523 -- relative path in the project file.
6524
6525 declare
6526 Dir_Path : constant String :=
6527 Get_Name_String
6528 (Main_Project.Directory.Name);
6529
6530 begin
6531 for
6532 J in Last_Binder_Switch + 1 .. Binder_Switches.Last
6533 loop
6534 Test_If_Relative_Path
6535 (Binder_Switches.Table (J),
6536 Parent => Dir_Path, Including_L_Switch => False);
6537 end loop;
6538
6539 for
6540 J in Last_Linker_Switch + 1 .. Linker_Switches.Last
6541 loop
6542 Test_If_Relative_Path
6543 (Linker_Switches.Table (J), Parent => Dir_Path);
6544 end loop;
6545 end;
6546
6547 -- We now put in the Binder_Switches and Linker_Switches
6548 -- tables, the binder and linker switches of the command
6549 -- line that have been put in the Saved_ tables.
6550 -- These switches will follow the project file switches.
6551
6552 for J in 1 .. Saved_Binder_Switches.Last loop
6553 Add_Switch
6554 (Saved_Binder_Switches.Table (J),
6555 Binder,
6556 And_Save => False);
6557 end loop;
6558
6559 for J in 1 .. Saved_Linker_Switches.Last loop
6560 Add_Switch
6561 (Saved_Linker_Switches.Table (J),
6562 Linker,
6563 And_Save => False);
6564 end loop;
6565 end;
6566 end if;
6567 end if;
6568
6569 -- Remove all marks to be sure to check sources for all executables,
6570 -- as the switches may be different and -s may be in use.
6571
6572 Delete_All_Marks;
6573 end loop Multiple_Main_Loop;
6574
6575 if Failed_Links.Last > 0 then
6576 for Index in 1 .. Successful_Links.Last loop
6577 Write_Str ("Linking of """);
6578 Write_Str (Get_Name_String (Successful_Links.Table (Index)));
6579 Write_Line (""" succeeded.");
6580 end loop;
6581
6582 Set_Standard_Error;
6583
6584 for Index in 1 .. Failed_Links.Last loop
6585 Write_Str ("Linking of """);
6586 Write_Str (Get_Name_String (Failed_Links.Table (Index)));
6587 Write_Line (""" failed.");
6588 end loop;
6589
6590 if Commands_To_Stdout then
6591 Set_Standard_Output;
6592 end if;
6593
6594 if Total_Compilation_Failures = 0 then
6595 Report_Compilation_Failed;
6596 end if;
6597 end if;
6598
6599 if Total_Compilation_Failures /= 0 then
6600 List_Bad_Compilations;
6601 Report_Compilation_Failed;
6602 end if;
6603
6604 -- Delete the temporary mapping file that was created if we are
6605 -- using project files.
6606
6607 Delete_All_Temp_Files;
6608
6609 exception
6610 when X : others =>
6611 Set_Standard_Error;
6612 Write_Line (Exception_Information (X));
6613 Make_Failed ("INTERNAL ERROR. Please report.");
6614 end Gnatmake;
6615
6616 ----------
6617 -- Hash --
6618 ----------
6619
6620 function Hash (F : File_Name_Type) return Header_Num is
6621 begin
6622 return Header_Num (1 + F mod Max_Header);
6623 end Hash;
6624
6625 --------------------
6626 -- In_Ada_Lib_Dir --
6627 --------------------
6628
6629 function In_Ada_Lib_Dir (File : File_Name_Type) return Boolean is
6630 D : constant File_Name_Type := Get_Directory (File);
6631 B : constant Byte := Get_Name_Table_Byte (D);
6632 begin
6633 return (B and Ada_Lib_Dir) /= 0;
6634 end In_Ada_Lib_Dir;
6635
6636 -----------------------
6637 -- Init_Mapping_File --
6638 -----------------------
6639
6640 procedure Init_Mapping_File
6641 (Project : Project_Id;
6642 Data : in out Project_Compilation_Data;
6643 File_Index : in out Natural)
6644 is
6645 FD : File_Descriptor;
6646 Status : Boolean;
6647 -- For call to Close
6648
6649 begin
6650 -- Increase the index of the last mapping file for this project
6651
6652 Data.Last_Mapping_File_Names := Data.Last_Mapping_File_Names + 1;
6653
6654 -- If there is a project file, call Create_Mapping_File with
6655 -- the project id.
6656
6657 if Project /= No_Project then
6658 Prj.Env.Create_Mapping_File
6659 (Project,
6660 In_Tree => Project_Tree,
6661 Language => Name_Ada,
6662 Name => Data.Mapping_File_Names
6663 (Data.Last_Mapping_File_Names));
6664
6665 -- Otherwise, just create an empty file
6666
6667 else
6668 Tempdir.Create_Temp_File
6669 (FD,
6670 Data.Mapping_File_Names (Data.Last_Mapping_File_Names));
6671
6672 if FD = Invalid_FD then
6673 Make_Failed ("disk full");
6674
6675 else
6676 Record_Temp_File
6677 (Project_Tree,
6678 Data.Mapping_File_Names (Data.Last_Mapping_File_Names));
6679 end if;
6680
6681 Close (FD, Status);
6682
6683 if not Status then
6684 Make_Failed ("disk full");
6685 end if;
6686 end if;
6687
6688 -- And return the index of the newly created file
6689
6690 File_Index := Data.Last_Mapping_File_Names;
6691 end Init_Mapping_File;
6692
6693 ------------
6694 -- Init_Q --
6695 ------------
6696
6697 procedure Init_Q is
6698 begin
6699 First_Q_Initialization := False;
6700 Q_Front := Q.First;
6701 Q.Set_Last (Q.First);
6702 end Init_Q;
6703
6704 ----------------
6705 -- Initialize --
6706 ----------------
6707
6708 procedure Initialize (Project_Node_Tree : out Project_Node_Tree_Ref) is
6709
6710 procedure Check_Version_And_Help is
6711 new Check_Version_And_Help_G (Makeusg);
6712
6713 -- Start of processing for Initialize
6714
6715 begin
6716 -- Prepare the project's tree, since this is used to hold external
6717 -- references, project path and other attributes that can be impacted by
6718 -- the command line switches
6719
6720 Project_Node_Tree := new Project_Node_Tree_Data;
6721 Prj.Tree.Initialize (Project_Node_Tree);
6722
6723 -- Override default initialization of Check_Object_Consistency since
6724 -- this is normally False for GNATBIND, but is True for GNATMAKE since
6725 -- we do not need to check source consistency again once GNATMAKE has
6726 -- looked at the sources to check.
6727
6728 Check_Object_Consistency := True;
6729
6730 -- Package initializations. The order of calls is important here
6731
6732 Output.Set_Standard_Error;
6733
6734 Gcc_Switches.Init;
6735 Binder_Switches.Init;
6736 Linker_Switches.Init;
6737
6738 Csets.Initialize;
6739 Namet.Initialize;
6740
6741 Snames.Initialize;
6742
6743 Prj.Initialize (Project_Tree);
6744
6745 Dependencies.Init;
6746
6747 RTS_Specified := null;
6748 N_M_Switch := 0;
6749
6750 Mains.Delete;
6751
6752 -- Add the directory where gnatmake is invoked in front of the path,
6753 -- if gnatmake is invoked from a bin directory or with directory
6754 -- information. Only do this if the platform is not VMS, where the
6755 -- notion of path does not really exist.
6756
6757 if not OpenVMS then
6758 declare
6759 Prefix : constant String := Executable_Prefix_Path;
6760 Command : constant String := Command_Name;
6761
6762 begin
6763 if Prefix'Length > 0 then
6764 declare
6765 PATH : constant String :=
6766 Prefix & Directory_Separator & "bin" &
6767 Path_Separator &
6768 Getenv ("PATH").all;
6769 begin
6770 Setenv ("PATH", PATH);
6771 end;
6772
6773 else
6774 for Index in reverse Command'Range loop
6775 if Command (Index) = Directory_Separator then
6776 declare
6777 Absolute_Dir : constant String :=
6778 Normalize_Pathname
6779 (Command (Command'First .. Index));
6780 PATH : constant String :=
6781 Absolute_Dir &
6782 Path_Separator &
6783 Getenv ("PATH").all;
6784 begin
6785 Setenv ("PATH", PATH);
6786 end;
6787
6788 exit;
6789 end if;
6790 end loop;
6791 end if;
6792 end;
6793 end if;
6794
6795 -- Scan the switches and arguments
6796
6797 -- First, scan to detect --version and/or --help
6798
6799 Check_Version_And_Help ("GNATMAKE", "1995");
6800
6801 -- Scan again the switch and arguments, now that we are sure that they
6802 -- do not include --version or --help.
6803
6804 Scan_Args : for Next_Arg in 1 .. Argument_Count loop
6805 Scan_Make_Arg
6806 (Project_Node_Tree, Argument (Next_Arg), And_Save => True);
6807 end loop Scan_Args;
6808
6809 if N_M_Switch > 0 and RTS_Specified = null then
6810 Process_Multilib (Project_Node_Tree);
6811 end if;
6812
6813 if Commands_To_Stdout then
6814 Set_Standard_Output;
6815 end if;
6816
6817 if Usage_Requested then
6818 Usage;
6819 end if;
6820
6821 -- Test for trailing -P switch
6822
6823 if Project_File_Name_Present and then Project_File_Name = null then
6824 Make_Failed ("project file name missing after -P");
6825
6826 -- Test for trailing -o switch
6827
6828 elsif Output_File_Name_Present
6829 and then not Output_File_Name_Seen
6830 then
6831 Make_Failed ("output file name missing after -o");
6832
6833 -- Test for trailing -D switch
6834
6835 elsif Object_Directory_Present
6836 and then not Object_Directory_Seen then
6837 Make_Failed ("object directory missing after -D");
6838 end if;
6839
6840 -- Test for simultaneity of -i and -D
6841
6842 if Object_Directory_Path /= null and then In_Place_Mode then
6843 Make_Failed ("-i and -D cannot be used simultaneously");
6844 end if;
6845
6846 -- Deal with -C= switch
6847
6848 if Gnatmake_Mapping_File /= null then
6849
6850 -- First, check compatibility with other switches
6851
6852 if Project_File_Name /= null then
6853 Make_Failed ("-C= switch is not compatible with -P switch");
6854
6855 elsif Saved_Maximum_Processes > 1 then
6856 Make_Failed ("-C= switch is not compatible with -jnnn switch");
6857 end if;
6858
6859 Fmap.Initialize (Gnatmake_Mapping_File.all);
6860 Add_Switch
6861 ("-gnatem=" & Gnatmake_Mapping_File.all,
6862 Compiler,
6863 And_Save => True);
6864 end if;
6865
6866 if Project_File_Name /= null then
6867
6868 -- A project file was specified by a -P switch
6869
6870 if Verbose_Mode then
6871 Write_Eol;
6872 Write_Str ("Parsing project file """);
6873 Write_Str (Project_File_Name.all);
6874 Write_Str (""".");
6875 Write_Eol;
6876 end if;
6877
6878 -- Avoid looking in the current directory for ALI files
6879
6880 -- Look_In_Primary_Dir := False;
6881
6882 -- Set the project parsing verbosity to whatever was specified
6883 -- by a possible -vP switch.
6884
6885 Prj.Pars.Set_Verbosity (To => Current_Verbosity);
6886
6887 -- Parse the project file.
6888 -- If there is an error, Main_Project will still be No_Project.
6889
6890 Prj.Pars.Parse
6891 (Project => Main_Project,
6892 In_Tree => Project_Tree,
6893 Project_File_Name => Project_File_Name.all,
6894 Packages_To_Check => Packages_To_Check_By_Gnatmake,
6895 Flags => Gnatmake_Flags,
6896 In_Node_Tree => Project_Node_Tree);
6897
6898 -- The parsing of project files may have changed the current output
6899
6900 if Commands_To_Stdout then
6901 Set_Standard_Output;
6902 else
6903 Set_Standard_Error;
6904 end if;
6905
6906 if Main_Project = No_Project then
6907 Make_Failed
6908 ("""" & Project_File_Name.all & """ processing failed");
6909 end if;
6910
6911 Create_Mapping_File := True;
6912
6913 if Verbose_Mode then
6914 Write_Eol;
6915 Write_Str ("Parsing of project file """);
6916 Write_Str (Project_File_Name.all);
6917 Write_Str (""" is finished.");
6918 Write_Eol;
6919 end if;
6920
6921 -- We add the source directories and the object directories to the
6922 -- search paths.
6923 -- ??? Why do we need these search directories, we already know the
6924 -- locations from parsing the project, except for the runtime which
6925 -- has its own directories anyway
6926
6927 Add_Source_Directories (Main_Project, Project_Tree);
6928 Add_Object_Directories (Main_Project);
6929
6930 Recursive_Compute_Depth (Main_Project);
6931 Compute_All_Imported_Projects (Project_Tree);
6932
6933 else
6934
6935 Osint.Add_Default_Search_Dirs;
6936
6937 -- Source file lookups should be cached for efficiency. Source files
6938 -- are not supposed to change. However, we do that now only if no
6939 -- project file is used; if a project file is used, we do it just
6940 -- after changing the directory to the object directory.
6941
6942 Osint.Source_File_Data (Cache => True);
6943
6944 -- Read gnat.adc file to initialize Fname.UF
6945
6946 Fname.UF.Initialize;
6947
6948 begin
6949 Fname.SF.Read_Source_File_Name_Pragmas;
6950
6951 exception
6952 when Err : SFN_Scan.Syntax_Error_In_GNAT_ADC =>
6953 Make_Failed (Exception_Message (Err));
6954 end;
6955 end if;
6956
6957 -- Make sure no project object directory is recorded
6958
6959 Project_Of_Current_Object_Directory := No_Project;
6960
6961 end Initialize;
6962
6963 ----------------------------
6964 -- Insert_Project_Sources --
6965 ----------------------------
6966
6967 procedure Insert_Project_Sources
6968 (The_Project : Project_Id;
6969 All_Projects : Boolean;
6970 Into_Q : Boolean)
6971 is
6972 Put_In_Q : Boolean := Into_Q;
6973 Unit : Unit_Index;
6974 Sfile : File_Name_Type;
6975 Index : Int;
6976
6977 Extending : constant Boolean := The_Project.Extends /= No_Project;
6978
6979 function Check_Project (P : Project_Id) return Boolean;
6980 -- Returns True if P is The_Project or a project extended by The_Project
6981
6982 -------------------
6983 -- Check_Project --
6984 -------------------
6985
6986 function Check_Project (P : Project_Id) return Boolean is
6987 begin
6988 if All_Projects or else P = The_Project then
6989 return True;
6990
6991 elsif Extending then
6992 declare
6993 Proj : Project_Id;
6994
6995 begin
6996 Proj := The_Project;
6997 while Proj /= null loop
6998 if P = Proj.Extends then
6999 return True;
7000 end if;
7001
7002 Proj := Proj.Extends;
7003 end loop;
7004 end;
7005 end if;
7006
7007 return False;
7008 end Check_Project;
7009
7010 -- Start of processing for Insert_Project_Sources
7011
7012 begin
7013 -- For all the sources in the project files,
7014
7015 Unit := Units_Htable.Get_First (Project_Tree.Units_HT);
7016 while Unit /= null loop
7017 Sfile := No_File;
7018 Index := 0;
7019
7020 -- If there is a source for the body, and the body has not been
7021 -- locally removed.
7022
7023 if Unit.File_Names (Impl) /= null
7024 and then not Unit.File_Names (Impl).Locally_Removed
7025 then
7026 -- And it is a source for the specified project
7027
7028 if Check_Project (Unit.File_Names (Impl).Project) then
7029
7030 -- If we don't have a spec, we cannot consider the source
7031 -- if it is a subunit.
7032
7033 if Unit.File_Names (Spec) = null then
7034 declare
7035 Src_Ind : Source_File_Index;
7036
7037 -- Here we are cheating a little bit: we don't want to
7038 -- use Sinput.L, because it depends on the GNAT tree
7039 -- (Atree, Sinfo, ...). So, we pretend that it is a
7040 -- project file, and we use Sinput.P.
7041
7042 -- Source_File_Is_Subunit is just scanning through the
7043 -- file until it finds one of the reserved words
7044 -- separate, procedure, function, generic or package.
7045 -- Fortunately, these Ada reserved words are also
7046 -- reserved for project files.
7047
7048 begin
7049 Src_Ind := Sinput.P.Load_Project_File
7050 (Get_Name_String
7051 (Unit.File_Names (Impl).Path.Name));
7052
7053 -- If it is a subunit, discard it
7054
7055 if Sinput.P.Source_File_Is_Subunit (Src_Ind) then
7056 Sfile := No_File;
7057 Index := 0;
7058 else
7059 Sfile := Unit.File_Names (Impl).Display_File;
7060 Index := Unit.File_Names (Impl).Index;
7061 end if;
7062 end;
7063
7064 else
7065 Sfile := Unit.File_Names (Impl).Display_File;
7066 Index := Unit.File_Names (Impl).Index;
7067 end if;
7068 end if;
7069
7070 elsif Unit.File_Names (Spec) /= null
7071 and then not Unit.File_Names (Spec).Locally_Removed
7072 and then Check_Project (Unit.File_Names (Spec).Project)
7073 then
7074 -- If there is no source for the body, but there is one for the
7075 -- spec which has not been locally removed, then we take this one.
7076
7077 Sfile := Unit.File_Names (Spec).Display_File;
7078 Index := Unit.File_Names (Spec).Index;
7079 end if;
7080
7081 -- If Put_In_Q is True, we insert into the Q
7082
7083 if Put_In_Q then
7084
7085 -- For the first source inserted into the Q, we need to initialize
7086 -- the Q, but not for the subsequent sources.
7087
7088 if First_Q_Initialization then
7089 Init_Q;
7090 end if;
7091
7092 -- And of course, only insert in the Q if the source is not marked
7093
7094 if Sfile /= No_File and then not Is_Marked (Sfile, Index) then
7095 if Verbose_Mode then
7096 Write_Str ("Adding """);
7097 Write_Str (Get_Name_String (Sfile));
7098 Write_Line (""" to the queue");
7099 end if;
7100
7101 Insert_Q (Sfile, Index => Index);
7102 Mark (Sfile, Index);
7103 end if;
7104
7105 elsif Sfile /= No_File then
7106
7107 -- If Put_In_Q is False, we add the source as if it were specified
7108 -- on the command line, and we set Put_In_Q to True, so that the
7109 -- following sources will be put directly in the queue. This will
7110 -- allow parallel compilation processes if -jx switch is used.
7111
7112 if Verbose_Mode then
7113 Write_Str ("Adding """);
7114 Write_Str (Get_Name_String (Sfile));
7115 Write_Line (""" as if on the command line");
7116 end if;
7117
7118 Osint.Add_File (Get_Name_String (Sfile), Index);
7119 Put_In_Q := True;
7120
7121 -- As we may look into the Q later, ensure the Q has been
7122 -- initialized to avoid errors.
7123
7124 if First_Q_Initialization then
7125 Init_Q;
7126 end if;
7127 end if;
7128
7129 Unit := Units_Htable.Get_Next (Project_Tree.Units_HT);
7130 end loop;
7131 end Insert_Project_Sources;
7132
7133 --------------
7134 -- Insert_Q --
7135 --------------
7136
7137 procedure Insert_Q
7138 (Source_File : File_Name_Type;
7139 Source_Unit : Unit_Name_Type := No_Unit_Name;
7140 Index : Int := 0)
7141 is
7142 begin
7143 if Debug.Debug_Flag_Q then
7144 Write_Str (" Q := Q + [ ");
7145 Write_Name (Source_File);
7146
7147 if Index /= 0 then
7148 Write_Str (", ");
7149 Write_Int (Index);
7150 end if;
7151
7152 Write_Str (" ] ");
7153 Write_Eol;
7154 end if;
7155
7156 Q.Table (Q.Last) :=
7157 (File => Source_File,
7158 Unit => Source_Unit,
7159 Index => Index);
7160 Q.Increment_Last;
7161 end Insert_Q;
7162
7163 ---------------------
7164 -- Is_In_Obsoleted --
7165 ---------------------
7166
7167 function Is_In_Obsoleted (F : File_Name_Type) return Boolean is
7168 begin
7169 if F = No_File then
7170 return False;
7171
7172 else
7173 declare
7174 Name : constant String := Get_Name_String (F);
7175 First : Natural;
7176 F2 : File_Name_Type;
7177
7178 begin
7179 First := Name'Last;
7180 while First > Name'First
7181 and then Name (First - 1) /= Directory_Separator
7182 and then Name (First - 1) /= '/'
7183 loop
7184 First := First - 1;
7185 end loop;
7186
7187 if First /= Name'First then
7188 Name_Len := 0;
7189 Add_Str_To_Name_Buffer (Name (First .. Name'Last));
7190 F2 := Name_Find;
7191
7192 else
7193 F2 := F;
7194 end if;
7195
7196 return Obsoleted.Get (F2);
7197 end;
7198 end if;
7199 end Is_In_Obsoleted;
7200
7201 ----------------------------
7202 -- Is_In_Object_Directory --
7203 ----------------------------
7204
7205 function Is_In_Object_Directory
7206 (Source_File : File_Name_Type;
7207 Full_Lib_File : File_Name_Type) return Boolean
7208 is
7209 begin
7210 -- There is something to check only when using project files. Otherwise,
7211 -- this function returns True (last line of the function).
7212
7213 if Main_Project /= No_Project then
7214 declare
7215 Source_File_Name : constant String :=
7216 Get_Name_String (Source_File);
7217 Saved_Verbosity : constant Verbosity := Current_Verbosity;
7218 Project : Project_Id := No_Project;
7219
7220 Path_Name : Path_Name_Type := No_Path;
7221 pragma Warnings (Off, Path_Name);
7222
7223 begin
7224 -- Call Get_Reference to know the ultimate extending project of
7225 -- the source. Call it with verbosity default to avoid verbose
7226 -- messages.
7227
7228 Current_Verbosity := Default;
7229 Prj.Env.Get_Reference
7230 (Source_File_Name => Source_File_Name,
7231 Project => Project,
7232 In_Tree => Project_Tree,
7233 Path => Path_Name);
7234 Current_Verbosity := Saved_Verbosity;
7235
7236 -- If this source is in a project, check that the ALI file is in
7237 -- its object directory. If it is not, return False, so that the
7238 -- ALI file will not be skipped.
7239
7240 if Project /= No_Project then
7241 declare
7242 Object_Directory : constant String :=
7243 Normalize_Pathname
7244 (Get_Name_String
7245 (Project.
7246 Object_Directory.Display_Name));
7247
7248 Olast : Natural := Object_Directory'Last;
7249
7250 Lib_File_Directory : constant String :=
7251 Normalize_Pathname (Dir_Name
7252 (Get_Name_String (Full_Lib_File)));
7253
7254 Llast : Natural := Lib_File_Directory'Last;
7255
7256 begin
7257 -- For directories, Normalize_Pathname may or may not put
7258 -- a directory separator at the end, depending on its input.
7259 -- Remove any last directory separator before comparison.
7260 -- Returns True only if the two directories are the same.
7261
7262 if Object_Directory (Olast) = Directory_Separator then
7263 Olast := Olast - 1;
7264 end if;
7265
7266 if Lib_File_Directory (Llast) = Directory_Separator then
7267 Llast := Llast - 1;
7268 end if;
7269
7270 return Object_Directory (Object_Directory'First .. Olast) =
7271 Lib_File_Directory (Lib_File_Directory'First .. Llast);
7272 end;
7273 end if;
7274 end;
7275 end if;
7276
7277 -- When the source is not in a project file, always return True
7278
7279 return True;
7280 end Is_In_Object_Directory;
7281
7282 ----------
7283 -- Link --
7284 ----------
7285
7286 procedure Link
7287 (ALI_File : File_Name_Type;
7288 Args : Argument_List;
7289 Success : out Boolean)
7290 is
7291 Link_Args : Argument_List (1 .. Args'Length + 1);
7292
7293 begin
7294 Get_Name_String (ALI_File);
7295 Link_Args (1) := new String'(Name_Buffer (1 .. Name_Len));
7296
7297 Link_Args (2 .. Args'Length + 1) := Args;
7298
7299 GNAT.OS_Lib.Normalize_Arguments (Link_Args);
7300
7301 Display (Gnatlink.all, Link_Args);
7302
7303 if Gnatlink_Path = null then
7304 Make_Failed ("error, unable to locate " & Gnatlink.all);
7305 end if;
7306
7307 GNAT.OS_Lib.Spawn (Gnatlink_Path.all, Link_Args, Success);
7308 end Link;
7309
7310 ---------------------------
7311 -- List_Bad_Compilations --
7312 ---------------------------
7313
7314 procedure List_Bad_Compilations is
7315 begin
7316 for J in Bad_Compilation.First .. Bad_Compilation.Last loop
7317 if Bad_Compilation.Table (J).File = No_File then
7318 null;
7319 elsif not Bad_Compilation.Table (J).Found then
7320 Inform (Bad_Compilation.Table (J).File, "not found");
7321 else
7322 Inform (Bad_Compilation.Table (J).File, "compilation error");
7323 end if;
7324 end loop;
7325 end List_Bad_Compilations;
7326
7327 -----------------
7328 -- List_Depend --
7329 -----------------
7330
7331 procedure List_Depend is
7332 Lib_Name : File_Name_Type;
7333 Obj_Name : File_Name_Type;
7334 Src_Name : File_Name_Type;
7335
7336 Len : Natural;
7337 Line_Pos : Natural;
7338 Line_Size : constant := 77;
7339
7340 begin
7341 Set_Standard_Output;
7342
7343 for A in ALIs.First .. ALIs.Last loop
7344 Lib_Name := ALIs.Table (A).Afile;
7345
7346 -- We have to provide the full library file name in In_Place_Mode
7347
7348 if In_Place_Mode then
7349 Lib_Name := Full_Lib_File_Name (Lib_Name);
7350 end if;
7351
7352 Obj_Name := Object_File_Name (Lib_Name);
7353 Write_Name (Obj_Name);
7354 Write_Str (" :");
7355
7356 Get_Name_String (Obj_Name);
7357 Len := Name_Len;
7358 Line_Pos := Len + 2;
7359
7360 for D in ALIs.Table (A).First_Sdep .. ALIs.Table (A).Last_Sdep loop
7361 Src_Name := Sdep.Table (D).Sfile;
7362
7363 if Is_Internal_File_Name (Src_Name)
7364 and then not Check_Readonly_Files
7365 then
7366 null;
7367 else
7368 if not Quiet_Output then
7369 Src_Name := Full_Source_Name (Src_Name);
7370 end if;
7371
7372 Get_Name_String (Src_Name);
7373 Len := Name_Len;
7374
7375 if Line_Pos + Len + 1 > Line_Size then
7376 Write_Str (" \");
7377 Write_Eol;
7378 Line_Pos := 0;
7379 end if;
7380
7381 Line_Pos := Line_Pos + Len + 1;
7382
7383 Write_Str (" ");
7384 Write_Name (Src_Name);
7385 end if;
7386 end loop;
7387
7388 Write_Eol;
7389 end loop;
7390
7391 if not Commands_To_Stdout then
7392 Set_Standard_Error;
7393 end if;
7394 end List_Depend;
7395
7396 -----------------
7397 -- Make_Failed --
7398 -----------------
7399
7400 procedure Make_Failed (S : String) is
7401 begin
7402 Delete_All_Temp_Files;
7403 Osint.Fail (S);
7404 end Make_Failed;
7405
7406 --------------------
7407 -- Mark_Directory --
7408 --------------------
7409
7410 procedure Mark_Directory
7411 (Dir : String;
7412 Mark : Lib_Mark_Type;
7413 On_Command_Line : Boolean)
7414 is
7415 N : Name_Id;
7416 B : Byte;
7417
7418 function Base_Directory return String;
7419 -- If Dir comes from the command line, empty string (relative paths are
7420 -- resolved with respect to the current directory), else return the main
7421 -- project's directory.
7422
7423 --------------------
7424 -- Base_Directory --
7425 --------------------
7426
7427 function Base_Directory return String is
7428 begin
7429 if On_Command_Line then
7430 return "";
7431 else
7432 return Get_Name_String (Main_Project.Directory.Display_Name);
7433 end if;
7434 end Base_Directory;
7435
7436 Real_Path : constant String := Normalize_Pathname (Dir, Base_Directory);
7437
7438 -- Start of processing for Mark_Directory
7439
7440 begin
7441 Name_Len := 0;
7442
7443 if Real_Path'Length = 0 then
7444 Add_Str_To_Name_Buffer (Dir);
7445
7446 else
7447 Add_Str_To_Name_Buffer (Real_Path);
7448 end if;
7449
7450 -- Last character is supposed to be a directory separator
7451
7452 if not Is_Directory_Separator (Name_Buffer (Name_Len)) then
7453 Add_Char_To_Name_Buffer (Directory_Separator);
7454 end if;
7455
7456 -- Add flags to the already existing flags
7457
7458 N := Name_Find;
7459 B := Get_Name_Table_Byte (N);
7460 Set_Name_Table_Byte (N, B or Mark);
7461 end Mark_Directory;
7462
7463 ----------------------
7464 -- Process_Multilib --
7465 ----------------------
7466
7467 procedure Process_Multilib
7468 (Project_Node_Tree : Project_Node_Tree_Ref)
7469 is
7470 Output_FD : File_Descriptor;
7471 Output_Name : String_Access;
7472 Arg_Index : Natural := 0;
7473 Success : Boolean := False;
7474 Return_Code : Integer := 0;
7475 Multilib_Gcc_Path : String_Access;
7476 Multilib_Gcc : String_Access;
7477 N_Read : Integer := 0;
7478 Line : String (1 .. 1000);
7479 Args : Argument_List (1 .. N_M_Switch + 1);
7480
7481 begin
7482 pragma Assert (N_M_Switch > 0 and RTS_Specified = null);
7483
7484 -- In case we detected a multilib switch and the user has not
7485 -- manually specified a specific RTS we emulate the following command:
7486 -- gnatmake $FLAGS --RTS=$(gcc -print-multi-directory $FLAGS)
7487
7488 -- First select the flags which might have an impact on multilib
7489 -- processing. Note that this is an heuristic selection and it
7490 -- will need to be maintained over time. The condition has to
7491 -- be kept synchronized with N_M_Switch counting in Scan_Make_Arg.
7492
7493 for Next_Arg in 1 .. Argument_Count loop
7494 declare
7495 Argv : constant String := Argument (Next_Arg);
7496 begin
7497 if Argv'Length > 2
7498 and then Argv (1) = '-'
7499 and then Argv (2) = 'm'
7500 and then Argv /= "-margs"
7501
7502 -- Ignore -mieee to avoid spawning an extra gcc in this case
7503
7504 and then Argv /= "-mieee"
7505 then
7506 Arg_Index := Arg_Index + 1;
7507 Args (Arg_Index) := new String'(Argv);
7508 end if;
7509 end;
7510 end loop;
7511
7512 pragma Assert (Arg_Index = N_M_Switch);
7513
7514 Args (Args'Last) := new String'("-print-multi-directory");
7515
7516 -- Call the GCC driver with the collected flags and save its
7517 -- output. Alternate design would be to link in gnatmake the
7518 -- relevant part of the GCC driver.
7519
7520 if Saved_Gcc /= null then
7521 Multilib_Gcc := Saved_Gcc;
7522 else
7523 Multilib_Gcc := Gcc;
7524 end if;
7525
7526 Multilib_Gcc_Path := GNAT.OS_Lib.Locate_Exec_On_Path (Multilib_Gcc.all);
7527
7528 Create_Temp_Output_File (Output_FD, Output_Name);
7529
7530 if Output_FD = Invalid_FD then
7531 return;
7532 end if;
7533
7534 GNAT.OS_Lib.Spawn
7535 (Multilib_Gcc_Path.all, Args, Output_FD, Return_Code, False);
7536 Close (Output_FD);
7537
7538 if Return_Code /= 0 then
7539 return;
7540 end if;
7541
7542 -- Parse the GCC driver output which is a single line, removing CR/LF
7543
7544 Output_FD := Open_Read (Output_Name.all, Binary);
7545
7546 if Output_FD = Invalid_FD then
7547 return;
7548 end if;
7549
7550 N_Read := Read (Output_FD, Line (1)'Address, Line'Length);
7551 Close (Output_FD);
7552 Delete_File (Output_Name.all, Success);
7553
7554 for J in reverse 1 .. N_Read loop
7555 if Line (J) = ASCII.CR or else Line (J) = ASCII.LF then
7556 N_Read := N_Read - 1;
7557 else
7558 exit;
7559 end if;
7560 end loop;
7561
7562 -- In case the standard RTS is selected do nothing
7563
7564 if N_Read = 0 or else Line (1 .. N_Read) = "." then
7565 return;
7566 end if;
7567
7568 -- Otherwise add -margs --RTS=output
7569
7570 Scan_Make_Arg (Project_Node_Tree, "-margs", And_Save => True);
7571 Scan_Make_Arg
7572 (Project_Node_Tree, "--RTS=" & Line (1 .. N_Read), And_Save => True);
7573 end Process_Multilib;
7574
7575 -----------------------------
7576 -- Recursive_Compute_Depth --
7577 -----------------------------
7578
7579 procedure Recursive_Compute_Depth (Project : Project_Id) is
7580 use Project_Boolean_Htable;
7581 Seen : Project_Boolean_Htable.Instance := Project_Boolean_Htable.Nil;
7582
7583 procedure Recurse (Prj : Project_Id; Depth : Natural);
7584 -- Recursive procedure that does the work, keeping track of the depth
7585
7586 -------------
7587 -- Recurse --
7588 -------------
7589
7590 procedure Recurse (Prj : Project_Id; Depth : Natural) is
7591 List : Project_List;
7592 Proj : Project_Id;
7593
7594 begin
7595 if Prj.Depth >= Depth or else Get (Seen, Prj) then
7596 return;
7597 end if;
7598
7599 -- We need a test to avoid infinite recursions with limited withs:
7600 -- If we have A -> B -> A, then when set level of A to n, we try and
7601 -- set level of B to n+1, and then level of A to n + 2, ...
7602
7603 Set (Seen, Prj, True);
7604
7605 Prj.Depth := Depth;
7606
7607 -- Visit each imported project
7608
7609 List := Prj.Imported_Projects;
7610 while List /= null loop
7611 Proj := List.Project;
7612 List := List.Next;
7613 Recurse (Prj => Proj, Depth => Depth + 1);
7614 end loop;
7615
7616 -- We again allow changing the depth of this project later on if it
7617 -- is in fact imported by a lower-level project.
7618
7619 Set (Seen, Prj, False);
7620 end Recurse;
7621
7622 Proj : Project_List;
7623
7624 -- Start of processing for Recursive_Compute_Depth
7625
7626 begin
7627 Proj := Project_Tree.Projects;
7628 while Proj /= null loop
7629 Proj.Project.Depth := 0;
7630 Proj := Proj.Next;
7631 end loop;
7632
7633 Recurse (Project, Depth => 1);
7634 Reset (Seen);
7635 end Recursive_Compute_Depth;
7636
7637 -------------------------------
7638 -- Report_Compilation_Failed --
7639 -------------------------------
7640
7641 procedure Report_Compilation_Failed is
7642 begin
7643 Delete_All_Temp_Files;
7644 Exit_Program (E_Fatal);
7645 end Report_Compilation_Failed;
7646
7647 ------------------------
7648 -- Sigint_Intercepted --
7649 ------------------------
7650
7651 procedure Sigint_Intercepted is
7652 SIGINT : constant := 2;
7653
7654 begin
7655 Set_Standard_Error;
7656 Write_Line ("*** Interrupted ***");
7657
7658 -- Send SIGINT to all outstanding compilation processes spawned
7659
7660 for J in 1 .. Outstanding_Compiles loop
7661 Kill (Running_Compile (J).Pid, SIGINT, 1);
7662 end loop;
7663
7664 Delete_All_Temp_Files;
7665 OS_Exit (1);
7666 -- ??? OS_Exit (1) is equivalent to Exit_Program (E_No_Compile),
7667 -- shouldn't that be Exit_Program (E_Abort) instead?
7668 end Sigint_Intercepted;
7669
7670 -------------------
7671 -- Scan_Make_Arg --
7672 -------------------
7673
7674 procedure Scan_Make_Arg
7675 (Project_Node_Tree : Project_Node_Tree_Ref;
7676 Argv : String;
7677 And_Save : Boolean)
7678 is
7679 Success : Boolean;
7680
7681 begin
7682 Gnatmake_Switch_Found := True;
7683
7684 pragma Assert (Argv'First = 1);
7685
7686 if Argv'Length = 0 then
7687 return;
7688 end if;
7689
7690 -- If the previous switch has set the Project_File_Name_Present flag
7691 -- (that is we have seen a -P alone), then the next argument is the name
7692 -- of the project file.
7693
7694 if Project_File_Name_Present and then Project_File_Name = null then
7695 if Argv (1) = '-' then
7696 Make_Failed ("project file name missing after -P");
7697
7698 else
7699 Project_File_Name_Present := False;
7700 Project_File_Name := new String'(Argv);
7701 end if;
7702
7703 -- If the previous switch has set the Output_File_Name_Present flag
7704 -- (that is we have seen a -o), then the next argument is the name of
7705 -- the output executable.
7706
7707 elsif Output_File_Name_Present
7708 and then not Output_File_Name_Seen
7709 then
7710 Output_File_Name_Seen := True;
7711
7712 if Argv (1) = '-' then
7713 Make_Failed ("output file name missing after -o");
7714
7715 else
7716 Add_Switch ("-o", Linker, And_Save => And_Save);
7717 Add_Switch (Executable_Name (Argv), Linker, And_Save => And_Save);
7718 end if;
7719
7720 -- If the previous switch has set the Object_Directory_Present flag
7721 -- (that is we have seen a -D), then the next argument is the path name
7722 -- of the object directory.
7723
7724 elsif Object_Directory_Present
7725 and then not Object_Directory_Seen
7726 then
7727 Object_Directory_Seen := True;
7728
7729 if Argv (1) = '-' then
7730 Make_Failed ("object directory path name missing after -D");
7731
7732 elsif not Is_Directory (Argv) then
7733 Make_Failed ("cannot find object directory """ & Argv & """");
7734
7735 else
7736 -- Record the object directory. Make sure it ends with a directory
7737 -- separator.
7738
7739 declare
7740 Norm : constant String := Normalize_Pathname (Argv);
7741
7742 begin
7743 if Norm (Norm'Last) = Directory_Separator then
7744 Object_Directory_Path := new String'(Norm);
7745 else
7746 Object_Directory_Path :=
7747 new String'(Norm & Directory_Separator);
7748 end if;
7749
7750 Add_Lib_Search_Dir (Norm);
7751
7752 -- Specify the object directory to the binder
7753
7754 Add_Switch ("-aO" & Norm, Binder, And_Save => And_Save);
7755 end;
7756
7757 end if;
7758
7759 -- Then check if we are dealing with -cargs/-bargs/-largs/-margs
7760
7761 elsif Argv = "-bargs"
7762 or else
7763 Argv = "-cargs"
7764 or else
7765 Argv = "-largs"
7766 or else
7767 Argv = "-margs"
7768 then
7769 case Argv (2) is
7770 when 'c' => Program_Args := Compiler;
7771 when 'b' => Program_Args := Binder;
7772 when 'l' => Program_Args := Linker;
7773 when 'm' => Program_Args := None;
7774
7775 when others =>
7776 raise Program_Error;
7777 end case;
7778
7779 -- A special test is needed for the -o switch within a -largs since that
7780 -- is another way to specify the name of the final executable.
7781
7782 elsif Program_Args = Linker
7783 and then Argv = "-o"
7784 then
7785 Make_Failed ("switch -o not allowed within a -largs. " &
7786 "Use -o directly.");
7787
7788 -- Check to see if we are reading switches after a -cargs, -bargs or
7789 -- -largs switch. If so, save it.
7790
7791 elsif Program_Args /= None then
7792
7793 -- Check to see if we are reading -I switches in order
7794 -- to take into account in the src & lib search directories.
7795
7796 if Argv'Length > 2 and then Argv (1 .. 2) = "-I" then
7797 if Argv (3 .. Argv'Last) = "-" then
7798 Look_In_Primary_Dir := False;
7799
7800 elsif Program_Args = Compiler then
7801 if Argv (3 .. Argv'Last) /= "-" then
7802 Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7803 end if;
7804
7805 elsif Program_Args = Binder then
7806 Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7807 end if;
7808 end if;
7809
7810 Add_Switch (Argv, Program_Args, And_Save => And_Save);
7811
7812 -- Handle non-default compiler, binder, linker, and handle --RTS switch
7813
7814 elsif Argv'Length > 2 and then Argv (1 .. 2) = "--" then
7815 if Argv'Length > 6
7816 and then Argv (1 .. 6) = "--GCC="
7817 then
7818 declare
7819 Program_Args : constant Argument_List_Access :=
7820 Argument_String_To_List
7821 (Argv (7 .. Argv'Last));
7822
7823 begin
7824 if And_Save then
7825 Saved_Gcc := new String'(Program_Args.all (1).all);
7826 else
7827 Gcc := new String'(Program_Args.all (1).all);
7828 end if;
7829
7830 for J in 2 .. Program_Args.all'Last loop
7831 Add_Switch
7832 (Program_Args.all (J).all, Compiler, And_Save => And_Save);
7833 end loop;
7834 end;
7835
7836 elsif Argv'Length > 11
7837 and then Argv (1 .. 11) = "--GNATBIND="
7838 then
7839 declare
7840 Program_Args : constant Argument_List_Access :=
7841 Argument_String_To_List
7842 (Argv (12 .. Argv'Last));
7843
7844 begin
7845 if And_Save then
7846 Saved_Gnatbind := new String'(Program_Args.all (1).all);
7847 else
7848 Gnatbind := new String'(Program_Args.all (1).all);
7849 end if;
7850
7851 for J in 2 .. Program_Args.all'Last loop
7852 Add_Switch
7853 (Program_Args.all (J).all, Binder, And_Save => And_Save);
7854 end loop;
7855 end;
7856
7857 elsif Argv'Length > 11
7858 and then Argv (1 .. 11) = "--GNATLINK="
7859 then
7860 declare
7861 Program_Args : constant Argument_List_Access :=
7862 Argument_String_To_List
7863 (Argv (12 .. Argv'Last));
7864 begin
7865 if And_Save then
7866 Saved_Gnatlink := new String'(Program_Args.all (1).all);
7867 else
7868 Gnatlink := new String'(Program_Args.all (1).all);
7869 end if;
7870
7871 for J in 2 .. Program_Args.all'Last loop
7872 Add_Switch (Program_Args.all (J).all, Linker);
7873 end loop;
7874 end;
7875
7876 elsif Argv'Length >= 5 and then
7877 Argv (1 .. 5) = "--RTS"
7878 then
7879 Add_Switch (Argv, Compiler, And_Save => And_Save);
7880 Add_Switch (Argv, Binder, And_Save => And_Save);
7881
7882 if Argv'Length <= 6 or else Argv (6) /= '=' then
7883 Make_Failed ("missing path for --RTS");
7884
7885 else
7886 -- Check that this is the first time we see this switch or
7887 -- if it is not the first time, the same path is specified.
7888
7889 if RTS_Specified = null then
7890 RTS_Specified := new String'(Argv (7 .. Argv'Last));
7891
7892 elsif RTS_Specified.all /= Argv (7 .. Argv'Last) then
7893 Make_Failed ("--RTS cannot be specified multiple times");
7894 end if;
7895
7896 -- Valid --RTS switch
7897
7898 No_Stdinc := True;
7899 No_Stdlib := True;
7900 RTS_Switch := True;
7901
7902 declare
7903 Src_Path_Name : constant String_Ptr :=
7904 Get_RTS_Search_Dir
7905 (Argv (7 .. Argv'Last), Include);
7906
7907 Lib_Path_Name : constant String_Ptr :=
7908 Get_RTS_Search_Dir
7909 (Argv (7 .. Argv'Last), Objects);
7910
7911 begin
7912 if Src_Path_Name /= null
7913 and then Lib_Path_Name /= null
7914 then
7915 -- Set RTS_*_Path_Name variables, so that correct direct-
7916 -- ories will be set when Osint.Add_Default_Search_Dirs
7917 -- is called later.
7918
7919 RTS_Src_Path_Name := Src_Path_Name;
7920 RTS_Lib_Path_Name := Lib_Path_Name;
7921
7922 elsif Src_Path_Name = null
7923 and then Lib_Path_Name = null
7924 then
7925 Make_Failed ("RTS path not valid: missing " &
7926 "adainclude and adalib directories");
7927
7928 elsif Src_Path_Name = null then
7929 Make_Failed ("RTS path not valid: missing adainclude " &
7930 "directory");
7931
7932 elsif Lib_Path_Name = null then
7933 Make_Failed ("RTS path not valid: missing adalib " &
7934 "directory");
7935 end if;
7936 end;
7937 end if;
7938
7939 elsif Argv'Length >= 8 and then
7940 Argv (1 .. 8) = "--param="
7941 then
7942 Add_Switch (Argv, Compiler, And_Save => And_Save);
7943 Add_Switch (Argv, Linker, And_Save => And_Save);
7944
7945 else
7946 Scan_Make_Switches (Project_Node_Tree, Argv, Success);
7947 end if;
7948
7949 -- If we have seen a regular switch process it
7950
7951 elsif Argv (1) = '-' then
7952 if Argv'Length = 1 then
7953 Make_Failed ("switch character cannot be followed by a blank");
7954
7955 -- Incorrect switches that should start with "--"
7956
7957 elsif (Argv'Length > 5 and then Argv (1 .. 5) = "-RTS=")
7958 or else (Argv'Length > 5 and then Argv (1 .. 5) = "-GCC=")
7959 or else (Argv'Length > 8 and then Argv (1 .. 7) = "-param=")
7960 or else (Argv'Length > 10 and then Argv (1 .. 10) = "-GNATLINK=")
7961 or else (Argv'Length > 10 and then Argv (1 .. 10) = "-GNATBIND=")
7962 then
7963 Make_Failed ("option " & Argv & " should start with '--'");
7964
7965 -- -I-
7966
7967 elsif Argv (2 .. Argv'Last) = "I-" then
7968 Look_In_Primary_Dir := False;
7969
7970 -- Forbid -?- or -??- where ? is any character
7971
7972 elsif (Argv'Length = 3 and then Argv (3) = '-')
7973 or else (Argv'Length = 4 and then Argv (4) = '-')
7974 then
7975 Make_Failed
7976 ("trailing ""-"" at the end of " & Argv & " forbidden.");
7977
7978 -- -Idir
7979
7980 elsif Argv (2) = 'I' then
7981 Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7982 Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
7983 Add_Switch (Argv, Compiler, And_Save => And_Save);
7984 Add_Switch (Argv, Binder, And_Save => And_Save);
7985
7986 -- -aIdir (to gcc this is like a -I switch)
7987
7988 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aI" then
7989 Add_Source_Search_Dir (Argv (4 .. Argv'Last), And_Save);
7990 Add_Switch
7991 ("-I" & Argv (4 .. Argv'Last), Compiler, And_Save => And_Save);
7992 Add_Switch (Argv, Binder, And_Save => And_Save);
7993
7994 -- -aOdir
7995
7996 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aO" then
7997 Add_Library_Search_Dir (Argv (4 .. Argv'Last), And_Save);
7998 Add_Switch (Argv, Binder, And_Save => And_Save);
7999
8000 -- -aLdir (to gnatbind this is like a -aO switch)
8001
8002 elsif Argv'Length >= 3 and then Argv (2 .. 3) = "aL" then
8003 Mark_Directory (Argv (4 .. Argv'Last), Ada_Lib_Dir, And_Save);
8004 Add_Library_Search_Dir (Argv (4 .. Argv'Last), And_Save);
8005 Add_Switch
8006 ("-aO" & Argv (4 .. Argv'Last), Binder, And_Save => And_Save);
8007
8008 -- -aamp_target=...
8009
8010 elsif Argv'Length >= 13 and then Argv (2 .. 13) = "aamp_target=" then
8011 Add_Switch (Argv, Compiler, And_Save => And_Save);
8012
8013 -- Set the aamp_target environment variable so that the binder and
8014 -- linker will use the proper target library. This is consistent
8015 -- with how things work when -aamp_target is passed on the command
8016 -- line to gnaampmake.
8017
8018 Setenv ("aamp_target", Argv (14 .. Argv'Last));
8019
8020 -- -Adir (to gnatbind this is like a -aO switch, to gcc like a -I)
8021
8022 elsif Argv (2) = 'A' then
8023 Mark_Directory (Argv (3 .. Argv'Last), Ada_Lib_Dir, And_Save);
8024 Add_Source_Search_Dir (Argv (3 .. Argv'Last), And_Save);
8025 Add_Library_Search_Dir (Argv (3 .. Argv'Last), And_Save);
8026 Add_Switch
8027 ("-I" & Argv (3 .. Argv'Last), Compiler, And_Save => And_Save);
8028 Add_Switch
8029 ("-aO" & Argv (3 .. Argv'Last), Binder, And_Save => And_Save);
8030
8031 -- -Ldir
8032
8033 elsif Argv (2) = 'L' then
8034 Add_Switch (Argv, Linker, And_Save => And_Save);
8035
8036 -- For -gxxxxx, -pg, -mxxx, -fxxx: give the switch to both the
8037 -- compiler and the linker (except for -gnatxxx which is only for the
8038 -- compiler). Some of the -mxxx (for example -m64) and -fxxx (for
8039 -- example -ftest-coverage for gcov) need to be used when compiling
8040 -- the binder generated files, and using all these gcc switches for
8041 -- the binder generated files should not be a problem.
8042
8043 elsif
8044 (Argv (2) = 'g' and then (Argv'Last < 5
8045 or else Argv (2 .. 5) /= "gnat"))
8046 or else Argv (2 .. Argv'Last) = "pg"
8047 or else (Argv (2) = 'm' and then Argv'Last > 2)
8048 or else (Argv (2) = 'f' and then Argv'Last > 2)
8049 then
8050 Add_Switch (Argv, Compiler, And_Save => And_Save);
8051 Add_Switch (Argv, Linker, And_Save => And_Save);
8052
8053 -- The following condition has to be kept synchronized with
8054 -- the Process_Multilib one.
8055
8056 if Argv (2) = 'm'
8057 and then Argv /= "-mieee"
8058 then
8059 N_M_Switch := N_M_Switch + 1;
8060 end if;
8061
8062 -- -C=<mapping file>
8063
8064 elsif Argv'Last > 2 and then Argv (2) = 'C' then
8065 if And_Save then
8066 if Argv (3) /= '=' or else Argv'Last <= 3 then
8067 Make_Failed ("illegal switch " & Argv);
8068 end if;
8069
8070 Gnatmake_Mapping_File := new String'(Argv (4 .. Argv'Last));
8071 end if;
8072
8073 -- -D
8074
8075 elsif Argv'Last = 2 and then Argv (2) = 'D' then
8076 if Project_File_Name /= null then
8077 Make_Failed
8078 ("-D cannot be used in conjunction with a project file");
8079
8080 else
8081 Scan_Make_Switches (Project_Node_Tree, Argv, Success);
8082 end if;
8083
8084 -- -d
8085
8086 elsif Argv (2) = 'd' and then Argv'Last = 2 then
8087 Display_Compilation_Progress := True;
8088
8089 -- -i
8090
8091 elsif Argv'Last = 2 and then Argv (2) = 'i' then
8092 if Project_File_Name /= null then
8093 Make_Failed
8094 ("-i cannot be used in conjunction with a project file");
8095 else
8096 Scan_Make_Switches (Project_Node_Tree, Argv, Success);
8097 end if;
8098
8099 -- -j (need to save the result)
8100
8101 elsif Argv (2) = 'j' then
8102 Scan_Make_Switches (Project_Node_Tree, Argv, Success);
8103
8104 if And_Save then
8105 Saved_Maximum_Processes := Maximum_Processes;
8106 end if;
8107
8108 -- -m
8109
8110 elsif Argv (2) = 'm' and then Argv'Last = 2 then
8111 Minimal_Recompilation := True;
8112
8113 -- -u
8114
8115 elsif Argv (2) = 'u' and then Argv'Last = 2 then
8116 Unique_Compile := True;
8117 Compile_Only := True;
8118 Do_Bind_Step := False;
8119 Do_Link_Step := False;
8120
8121 -- -U
8122
8123 elsif Argv (2) = 'U'
8124 and then Argv'Last = 2
8125 then
8126 Unique_Compile_All_Projects := True;
8127 Unique_Compile := True;
8128 Compile_Only := True;
8129 Do_Bind_Step := False;
8130 Do_Link_Step := False;
8131
8132 -- -Pprj or -P prj (only once, and only on the command line)
8133
8134 elsif Argv (2) = 'P' then
8135 if Project_File_Name /= null then
8136 Make_Failed ("cannot have several project files specified");
8137
8138 elsif Object_Directory_Path /= null then
8139 Make_Failed
8140 ("-D cannot be used in conjunction with a project file");
8141
8142 elsif In_Place_Mode then
8143 Make_Failed
8144 ("-i cannot be used in conjunction with a project file");
8145
8146 elsif not And_Save then
8147
8148 -- It could be a tool other than gnatmake (e.g. gnatdist)
8149 -- or a -P switch inside a project file.
8150
8151 Fail
8152 ("either the tool is not ""project-aware"" or " &
8153 "a project file is specified inside a project file");
8154
8155 elsif Argv'Last = 2 then
8156
8157 -- -P is used alone: the project file name is the next option
8158
8159 Project_File_Name_Present := True;
8160
8161 else
8162 Project_File_Name := new String'(Argv (3 .. Argv'Last));
8163 end if;
8164
8165 -- -vPx (verbosity of the parsing of the project files)
8166
8167 elsif Argv'Last = 4
8168 and then Argv (2 .. 3) = "vP"
8169 and then Argv (4) in '0' .. '2'
8170 then
8171 if And_Save then
8172 case Argv (4) is
8173 when '0' =>
8174 Current_Verbosity := Prj.Default;
8175 when '1' =>
8176 Current_Verbosity := Prj.Medium;
8177 when '2' =>
8178 Current_Verbosity := Prj.High;
8179 when others =>
8180 null;
8181 end case;
8182 end if;
8183
8184 -- -Xext=val (External assignment)
8185
8186 elsif Argv (2) = 'X'
8187 and then Is_External_Assignment (Project_Node_Tree, Argv)
8188 then
8189 -- Is_External_Assignment has side effects when it returns True
8190
8191 null;
8192
8193 -- If -gnath is present, then generate the usage information right
8194 -- now and do not pass this option on to the compiler calls.
8195
8196 elsif Argv = "-gnath" then
8197 Usage;
8198
8199 -- If -gnatc is specified, make sure the bind and link steps are not
8200 -- executed.
8201
8202 elsif Argv'Length >= 6 and then Argv (2 .. 6) = "gnatc" then
8203
8204 -- If -gnatc is specified, make sure the bind and link steps are
8205 -- not executed.
8206
8207 Add_Switch (Argv, Compiler, And_Save => And_Save);
8208 Operating_Mode := Check_Semantics;
8209 Check_Object_Consistency := False;
8210 Compile_Only := True;
8211 Do_Bind_Step := False;
8212 Do_Link_Step := False;
8213
8214 elsif Argv (2 .. Argv'Last) = "nostdlib" then
8215
8216 -- Pass -nstdlib to gnatbind and gnatlink
8217
8218 No_Stdlib := True;
8219 Add_Switch (Argv, Binder, And_Save => And_Save);
8220 Add_Switch (Argv, Linker, And_Save => And_Save);
8221
8222 elsif Argv (2 .. Argv'Last) = "nostdinc" then
8223
8224 -- Pass -nostdinc to the Compiler and to gnatbind
8225
8226 No_Stdinc := True;
8227 Add_Switch (Argv, Compiler, And_Save => And_Save);
8228 Add_Switch (Argv, Binder, And_Save => And_Save);
8229
8230 -- All other switches are processed by Scan_Make_Switches. If the
8231 -- call returns with Gnatmake_Switch_Found = False, then the switch
8232 -- is passed to the compiler.
8233
8234 else
8235 Scan_Make_Switches
8236 (Project_Node_Tree, Argv, Gnatmake_Switch_Found);
8237
8238 if not Gnatmake_Switch_Found then
8239 Add_Switch (Argv, Compiler, And_Save => And_Save);
8240 end if;
8241 end if;
8242
8243 -- If not a switch it must be a file name
8244
8245 else
8246 if And_Save then
8247 Main_On_Command_Line := True;
8248 end if;
8249
8250 Add_File (Argv);
8251 Mains.Add_Main (Argv);
8252 end if;
8253 end Scan_Make_Arg;
8254
8255 -----------------
8256 -- Switches_Of --
8257 -----------------
8258
8259 function Switches_Of
8260 (Source_File : File_Name_Type;
8261 Source_File_Name : String;
8262 Source_Index : Int;
8263 Project : Project_Id;
8264 In_Package : Package_Id;
8265 Allow_ALI : Boolean) return Variable_Value
8266 is
8267 Lang : constant Language_Ptr := Get_Language_From_Name (Project, "ada");
8268
8269 Switches : Variable_Value;
8270
8271 Defaults : constant Array_Element_Id :=
8272 Prj.Util.Value_Of
8273 (Name => Name_Default_Switches,
8274 In_Arrays =>
8275 Project_Tree.Packages.Table
8276 (In_Package).Decl.Arrays,
8277 In_Tree => Project_Tree);
8278
8279 Switches_Array : constant Array_Element_Id :=
8280 Prj.Util.Value_Of
8281 (Name => Name_Switches,
8282 In_Arrays =>
8283 Project_Tree.Packages.Table
8284 (In_Package).Decl.Arrays,
8285 In_Tree => Project_Tree);
8286
8287 begin
8288 -- First, try Switches (<file name>)
8289
8290 Switches :=
8291 Prj.Util.Value_Of
8292 (Index => Name_Id (Source_File),
8293 Src_Index => Source_Index,
8294 In_Array => Switches_Array,
8295 In_Tree => Project_Tree);
8296
8297 -- Check also without the suffix
8298
8299 if Switches = Nil_Variable_Value
8300 and then Lang /= null
8301 then
8302 declare
8303 Naming : Lang_Naming_Data renames Lang.Config.Naming_Data;
8304 Name : String (1 .. Source_File_Name'Length + 3);
8305 Last : Positive := Source_File_Name'Length;
8306 Spec_Suffix : constant String :=
8307 Get_Name_String (Naming.Spec_Suffix);
8308 Body_Suffix : constant String :=
8309 Get_Name_String (Naming.Body_Suffix);
8310 Truncated : Boolean := False;
8311
8312 begin
8313 Name (1 .. Last) := Source_File_Name;
8314
8315 if Last > Body_Suffix'Length
8316 and then Name (Last - Body_Suffix'Length + 1 .. Last) =
8317 Body_Suffix
8318 then
8319 Truncated := True;
8320 Last := Last - Body_Suffix'Length;
8321 end if;
8322
8323 if not Truncated
8324 and then Last > Spec_Suffix'Length
8325 and then Name (Last - Spec_Suffix'Length + 1 .. Last) =
8326 Spec_Suffix
8327 then
8328 Truncated := True;
8329 Last := Last - Spec_Suffix'Length;
8330 end if;
8331
8332 if Truncated then
8333 Name_Len := 0;
8334 Add_Str_To_Name_Buffer (Name (1 .. Last));
8335 Switches :=
8336 Prj.Util.Value_Of
8337 (Index => Name_Find,
8338 Src_Index => 0,
8339 In_Array => Switches_Array,
8340 In_Tree => Project_Tree);
8341
8342 if Switches = Nil_Variable_Value and then Allow_ALI then
8343 Last := Source_File_Name'Length;
8344
8345 while Name (Last) /= '.' loop
8346 Last := Last - 1;
8347 end loop;
8348
8349 Name_Len := 0;
8350 Add_Str_To_Name_Buffer (Name (1 .. Last));
8351 Add_Str_To_Name_Buffer ("ali");
8352
8353 Switches :=
8354 Prj.Util.Value_Of
8355 (Index => Name_Find,
8356 Src_Index => 0,
8357 In_Array => Switches_Array,
8358 In_Tree => Project_Tree);
8359 end if;
8360 end if;
8361 end;
8362 end if;
8363
8364 -- Next, try Switches ("Ada")
8365
8366 if Switches = Nil_Variable_Value then
8367 Switches :=
8368 Prj.Util.Value_Of
8369 (Index => Name_Ada,
8370 Src_Index => 0,
8371 In_Array => Switches_Array,
8372 In_Tree => Project_Tree,
8373 Force_Lower_Case_Index => True);
8374
8375 if Switches /= Nil_Variable_Value then
8376 Switch_May_Be_Passed_To_The_Compiler := False;
8377 end if;
8378 end if;
8379
8380 -- Next, try Switches (others)
8381
8382 if Switches = Nil_Variable_Value then
8383 Switches :=
8384 Prj.Util.Value_Of
8385 (Index => All_Other_Names,
8386 Src_Index => 0,
8387 In_Array => Switches_Array,
8388 In_Tree => Project_Tree);
8389
8390 if Switches /= Nil_Variable_Value then
8391 Switch_May_Be_Passed_To_The_Compiler := False;
8392 end if;
8393 end if;
8394
8395 -- And finally, Default_Switches ("Ada")
8396
8397 if Switches = Nil_Variable_Value then
8398 Switches :=
8399 Prj.Util.Value_Of
8400 (Index => Name_Ada,
8401 Src_Index => 0,
8402 In_Array => Defaults,
8403 In_Tree => Project_Tree);
8404 end if;
8405
8406 return Switches;
8407 end Switches_Of;
8408
8409 -----------
8410 -- Usage --
8411 -----------
8412
8413 procedure Usage is
8414 begin
8415 if Usage_Needed then
8416 Usage_Needed := False;
8417 Makeusg;
8418 end if;
8419 end Usage;
8420
8421 begin
8422 -- Make sure that in case of failure, the temp files will be deleted
8423
8424 Prj.Com.Fail := Make_Failed'Access;
8425 MLib.Fail := Make_Failed'Access;
8426 Makeutl.Do_Fail := Make_Failed'Access;
8427 end Make;