1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2004-2007, Free Software Foundation, Inc. --
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. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
28 with Hostparm; use Hostparm;
29 with Makeutl; use Makeutl;
30 with MLib.Tgt; use MLib.Tgt;
31 with Namet; use Namet;
32 with Output; use Output;
34 with Osint; use Osint;
36 with Prj.Ext; use Prj.Ext;
38 with Prj.Util; use Prj.Util;
39 with Snames; use Snames;
41 with Types; use Types;
43 with Ada.Command_Line; use Ada.Command_Line;
44 with Ada.Strings.Fixed; use Ada.Strings.Fixed;
45 with Ada.Text_IO; use Ada.Text_IO;
46 with Ada.Unchecked_Deallocation;
48 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
49 with GNAT.Dynamic_Tables;
50 with GNAT.Expect; use GNAT.Expect;
52 with GNAT.OS_Lib; use GNAT.OS_Lib;
53 with GNAT.Regpat; use GNAT.Regpat;
56 with System.Case_Util; use System.Case_Util;
58 package body Makegpr is
60 On_Windows : constant Boolean := Directory_Separator = '\';
61 -- True when on Windows. Used in Check_Compilation_Needed when processing
62 -- C/C++ dependency files for backslash handling.
64 Max_In_Archives : constant := 50;
65 -- The maximum number of arguments for a single invocation of the
66 -- Archive Indexer (ar).
68 No_Argument : aliased Argument_List := (1 .. 0 => null);
69 -- Null argument list representing case of no arguments
71 FD : Process_Descriptor;
72 -- The process descriptor used when invoking a non GNU compiler with -M
73 -- and getting the output with GNAT.Expect.
75 Line_Matcher : constant Pattern_Matcher := Compile ("^.*?\n", Single_Line);
76 -- Pattern for GNAT.Expect for the invocation of a non GNU compiler with -M
79 Name_Compiler_Command : Name_Id;
80 -- Names of package IDE and its attribute Compiler_Command.
81 -- Set up by Initialize.
83 Unique_Compile : Boolean := False;
84 -- True when switch -u is used on the command line
86 type Source_Index_Rec is record
89 Found : Boolean := False;
91 -- Used as Source_Indexes component to check if archive needs to be rebuilt
93 type Source_Index_Array is array (Positive range <>) of Source_Index_Rec;
94 type Source_Indexes_Ref is access Source_Index_Array;
96 procedure Free is new Ada.Unchecked_Deallocation
97 (Source_Index_Array, Source_Indexes_Ref);
99 Initial_Source_Index_Count : constant Positive := 20;
100 Source_Indexes : Source_Indexes_Ref :=
101 new Source_Index_Array (1 .. Initial_Source_Index_Count);
102 -- A list of the Other_Source_Ids of a project file, with an indication
103 -- that they have been found in the archive dependency file.
105 Last_Source : Natural := 0;
106 -- The index of the last valid component of Source_Indexes
108 Compiler_Names : array (First_Language_Indexes) of String_Access;
109 -- The names of the compilers to be used. Set up by Get_Compiler.
110 -- Used to display the commands spawned.
112 Gnatmake_String : constant String_Access := new String'("gnatmake");
113 GCC_String : constant String_Access := new String'("gcc");
114 G_Plus_Plus_String : constant String_Access := new String'("g++");
116 Default_Compiler_Names : constant array
117 (First_Language_Indexes range
118 Ada_Language_Index .. C_Plus_Plus_Language_Index)
120 (Ada_Language_Index => Gnatmake_String,
121 C_Language_Index => GCC_String,
122 C_Plus_Plus_Language_Index => G_Plus_Plus_String);
124 Compiler_Paths : array (First_Language_Indexes) of String_Access;
125 -- The path names of the compiler to be used. Set up by Get_Compiler.
126 -- Used to spawn compiling/linking processes.
128 Compiler_Is_Gcc : array (First_Language_Indexes) of Boolean;
129 -- An indication that a compiler is a GCC compiler, to be able to use
130 -- specific GCC switches.
132 Archive_Builder_Path : String_Access := null;
133 -- The path name of the archive builder (ar). To be used when spawning
136 Archive_Indexer_Path : String_Access := null;
137 -- The path name of the archive indexer (ranlib), if it exists
139 Copyright_Output : Boolean := False;
140 Usage_Output : Boolean := False;
141 -- Flags to avoid multiple displays of Copyright notice and of Usage
143 Output_File_Name : String_Access := null;
144 -- The name given after a switch -o
146 Output_File_Name_Expected : Boolean := False;
147 -- True when last switch was -o
149 Project_File_Name : String_Access := null;
150 -- The name of the project file specified with switch -P
152 Project_File_Name_Expected : Boolean := False;
153 -- True when last switch was -P
155 Naming_String : aliased String := "naming";
156 Builder_String : aliased String := "builder";
157 Compiler_String : aliased String := "compiler";
158 Binder_String : aliased String := "binder";
159 Linker_String : aliased String := "linker";
160 -- Name of packages to be checked when parsing/processing project files
162 List_Of_Packages : aliased String_List :=
163 (Naming_String 'Access,
164 Builder_String 'Access,
165 Compiler_String 'Access,
166 Binder_String 'Access,
167 Linker_String 'Access);
168 Packages_To_Check : constant String_List_Access := List_Of_Packages'Access;
169 -- List of the packages to be checked when parsing/processing project files
171 Project_Tree : constant Project_Tree_Ref := new Project_Tree_Data;
173 Main_Project : Project_Id;
174 -- The project id of the main project
176 type Processor is (None, Linker, Compiler);
177 Current_Processor : Processor := None;
178 -- This variable changes when switches -*args are used
180 Current_Language : Language_Index := Ada_Language_Index;
181 -- The compiler language to consider when Processor is Compiler
183 package Comp_Opts is new GNAT.Dynamic_Tables
184 (Table_Component_Type => String_Access,
185 Table_Index_Type => Integer,
186 Table_Low_Bound => 1,
188 Table_Increment => 100);
189 Options : array (First_Language_Indexes) of Comp_Opts.Instance;
190 -- Tables to store compiling options for the different compilers
192 package Linker_Options is new Table.Table
193 (Table_Component_Type => String_Access,
194 Table_Index_Type => Integer,
195 Table_Low_Bound => 1,
197 Table_Increment => 100,
198 Table_Name => "Makegpr.Linker_Options");
199 -- Table to store the linking options
201 package Library_Opts is new Table.Table
202 (Table_Component_Type => String_Access,
203 Table_Index_Type => Integer,
204 Table_Low_Bound => 1,
206 Table_Increment => 100,
207 Table_Name => "Makegpr.Library_Opts");
208 -- Table to store the linking options
210 package Ada_Mains is new Table.Table
211 (Table_Component_Type => String_Access,
212 Table_Index_Type => Integer,
213 Table_Low_Bound => 1,
215 Table_Increment => 100,
216 Table_Name => "Makegpr.Ada_Mains");
217 -- Table to store the Ada mains, either specified on the command line
218 -- or found in attribute Main of the main project file.
220 package Other_Mains is new Table.Table
221 (Table_Component_Type => Other_Source,
222 Table_Index_Type => Integer,
223 Table_Low_Bound => 1,
225 Table_Increment => 100,
226 Table_Name => "Makegpr.Other_Mains");
227 -- Table to store the mains of languages other than Ada, either specified
228 -- on the command line or found in attribute Main of the main project file.
230 package Sources_Compiled is new GNAT.HTable.Simple_HTable
231 (Header_Num => Header_Num,
234 Key => File_Name_Type,
238 package Saved_Switches is new Table.Table
239 (Table_Component_Type => String_Access,
240 Table_Index_Type => Integer,
241 Table_Low_Bound => 1,
243 Table_Increment => 100,
244 Table_Name => "Makegpr.Saved_Switches");
245 -- Table to store the switches to be passed to gnatmake
247 Initial_Argument_Count : constant Positive := 20;
248 type Boolean_Array is array (Positive range <>) of Boolean;
249 type Booleans is access Boolean_Array;
251 procedure Free is new Ada.Unchecked_Deallocation (Boolean_Array, Booleans);
253 Arguments : Argument_List_Access :=
254 new Argument_List (1 .. Initial_Argument_Count);
255 -- Used to store lists of arguments to be used when spawning a process
257 Arguments_Displayed : Booleans :=
258 new Boolean_Array (1 .. Initial_Argument_Count);
259 -- For each argument in Arguments, indicate if the argument should be
260 -- displayed when procedure Display_Command is called.
262 Last_Argument : Natural := 0;
263 -- Index of the last valid argument in Arguments
265 package Cache_Args is new Table.Table
266 (Table_Component_Type => String_Access,
267 Table_Index_Type => Integer,
268 Table_Low_Bound => 1,
269 Table_Initial => 200,
270 Table_Increment => 100,
271 Table_Name => "Makegpr.Cache_Args");
272 -- A table to cache arguments, to avoid multiple allocation of the same
273 -- strings. It is not possible to use a hash table, because String is
274 -- an unconstrained type.
276 -- Various switches used when spawning processes:
278 Dash_B_String : aliased String := "-B";
279 Dash_B : constant String_Access := Dash_B_String'Access;
280 Dash_c_String : aliased String := "-c";
281 Dash_c : constant String_Access := Dash_c_String'Access;
282 Dash_cargs_String : aliased String := "-cargs";
283 Dash_cargs : constant String_Access := Dash_cargs_String'Access;
284 Dash_d_String : aliased String := "-d";
285 Dash_d : constant String_Access := Dash_d_String'Access;
286 Dash_f_String : aliased String := "-f";
287 Dash_f : constant String_Access := Dash_f_String'Access;
288 Dash_k_String : aliased String := "-k";
289 Dash_k : constant String_Access := Dash_k_String'Access;
290 Dash_largs_String : aliased String := "-largs";
291 Dash_largs : constant String_Access := Dash_largs_String'Access;
292 Dash_M_String : aliased String := "-M";
293 Dash_M : constant String_Access := Dash_M_String'Access;
294 Dash_margs_String : aliased String := "-margs";
295 Dash_margs : constant String_Access := Dash_margs_String'Access;
296 Dash_o_String : aliased String := "-o";
297 Dash_o : constant String_Access := Dash_o_String'Access;
298 Dash_P_String : aliased String := "-P";
299 Dash_P : constant String_Access := Dash_P_String'Access;
300 Dash_q_String : aliased String := "-q";
301 Dash_q : constant String_Access := Dash_q_String'Access;
302 Dash_u_String : aliased String := "-u";
303 Dash_u : constant String_Access := Dash_u_String'Access;
304 Dash_v_String : aliased String := "-v";
305 Dash_v : constant String_Access := Dash_v_String'Access;
306 Dash_vP1_String : aliased String := "-vP1";
307 Dash_vP1 : constant String_Access := Dash_vP1_String'Access;
308 Dash_vP2_String : aliased String := "-vP2";
309 Dash_vP2 : constant String_Access := Dash_vP2_String'Access;
310 Dash_x_String : aliased String := "-x";
311 Dash_x : constant String_Access := Dash_x_String'Access;
312 r_String : aliased String := "r";
313 r : constant String_Access := r_String'Access;
315 CPATH : constant String := "CPATH";
316 -- The environment variable to set when compiler is a GCC compiler
317 -- to indicate the include directory path.
319 Current_Include_Paths : array (First_Language_Indexes) of String_Access;
320 -- A cache for the paths of included directories, to avoid setting
321 -- env var CPATH unnecessarily.
323 C_Plus_Plus_Is_Used : Boolean := False;
324 -- True when there are sources in C++
326 Link_Options_Switches : Argument_List_Access := null;
327 -- The link options coming from the attributes Linker'Linker_Options in
328 -- project files imported, directly or indirectly, by the main project.
330 Total_Number_Of_Errors : Natural := 0;
331 -- Used when Keep_Going is True (switch -k) to keep the total number
332 -- of compilation/linking errors, to report at the end of execution.
334 Need_To_Rebuild_Global_Archive : Boolean := False;
336 Error_Header : constant String := "*** ERROR: ";
337 -- The beginning of error message, when Keep_Going is True
339 Need_To_Relink : Boolean := False;
340 -- True when an executable of a language other than Ada need to be linked
342 Global_Archive_Exists : Boolean := False;
343 -- True if there is a non empty global archive, to prevent creation
346 Path_Option : String_Access;
347 -- The path option switch, when supported
349 Project_Of_Current_Object_Directory : Project_Id := No_Project;
350 -- The object directory of the project for the last compilation. Avoid
351 -- calling Change_Dir if the current working directory is already this
354 package Lib_Path is new Table.Table
355 (Table_Component_Type => Character,
356 Table_Index_Type => Integer,
357 Table_Low_Bound => 1,
358 Table_Initial => 200,
359 Table_Increment => 100,
360 Table_Name => "Makegpr.Lib_Path");
361 -- A table to compute the path to put in the path option switch, when it
364 procedure Add_Archives (For_Gnatmake : Boolean);
365 -- Add to Arguments the list of archives for linking an executable
367 procedure Add_Argument (Arg : String_Access; Display : Boolean);
368 procedure Add_Argument (Arg : String; Display : Boolean);
369 -- Add an argument to Arguments. Reallocate if necessary
371 procedure Add_Arguments (Args : Argument_List; Display : Boolean);
372 -- Add a list of arguments to Arguments. Reallocate if necessary
374 procedure Add_Option (Arg : String);
375 -- Add a switch for the Ada, C or C++ compiler, or for the linker.
376 -- The table where this option is stored depends on the values of
377 -- Current_Processor and Current_Language.
379 procedure Add_Search_Directories
380 (Data : Project_Data;
381 Language : First_Language_Indexes);
382 -- Either add to the Arguments the necessary -I switches needed to
383 -- compile, or, when compiler is gcc/g++, set up the C*INCLUDE_PATH
384 -- environment variable, if necessary.
386 procedure Add_Source_Id (Project : Project_Id; Id : Other_Source_Id);
387 -- Add a source id to Source_Indexes, with Found set to False
389 procedure Add_Switches
390 (Data : Project_Data;
392 Language : Language_Index;
393 File_Name : File_Name_Type);
394 -- Add to Arguments the switches, if any, for a source (attribute Switches)
395 -- or language (attribute Default_Switches), coming from package Compiler
396 -- or Linker (depending on Proc) of a specified project file.
398 procedure Build_Global_Archive;
399 -- Build the archive for the main project
401 procedure Build_Library (Project : Project_Id; Unconditionally : Boolean);
402 -- Build the library for a library project. If Unconditionally is
403 -- False, first check if the library is up to date, and build it only
406 procedure Check (Option : String);
407 -- Check that a switch coming from a project file is not the concatenation
408 -- of several valid switch, for example "-g -v". If it is, issue a warning.
410 procedure Check_Archive_Builder;
411 -- Check if the archive builder (ar) is there
413 procedure Check_Compilation_Needed
414 (Source : Other_Source;
415 Need_To_Compile : out Boolean);
416 -- Check if a source of a language other than Ada needs to be compiled or
419 procedure Check_For_C_Plus_Plus;
420 -- Check if C++ is used in at least one project
423 (Source_Id : Other_Source_Id;
425 Local_Errors : in out Boolean);
426 -- Compile one non-Ada source
428 procedure Compile_Individual_Sources;
429 -- Compile the sources specified on the command line, when in
430 -- Unique_Compile mode.
432 procedure Compile_Link_With_Gnatmake (Mains_Specified : Boolean);
433 -- Compile/Link with gnatmake when there are Ada sources in the main
434 -- project. Arguments may already contain options to be used by
435 -- gnatmake. Used for both Ada mains and mains of other languages.
436 -- When Compile_Only is True, do not use the linking options
438 procedure Compile_Sources;
439 -- Compile the sources of languages other than Ada, if necessary
442 -- Output the Copyright notice
444 procedure Create_Archive_Dependency_File
446 First_Source : Other_Source_Id);
447 -- Create the archive dependency file for a library project
449 procedure Create_Global_Archive_Dependency_File (Name : String);
450 -- Create the archive depenency file for the main project
452 procedure Display_Command
454 Path : String_Access;
455 CPATH : String_Access := null;
456 Ellipse : Boolean := False);
457 -- Display the command for a spawned process, if in Verbose_Mode or not in
458 -- Quiet_Output. In non verbose mode, when Ellipse is True, display "..."
459 -- in place of the first argument that has Display set to False.
461 procedure Get_Compiler (For_Language : First_Language_Indexes);
462 -- Find the compiler name and path name for a specified programming
463 -- language, if not already done. Results are in the corresponding elements
464 -- of arrays Compiler_Names and Compiler_Paths. Name of compiler is found
465 -- in package IDE of the main project, or defaulted. Fail if compiler
466 -- cannot be found on the path. For the Ada language, gnatmake, rather than
467 -- the Ada compiler is returned.
469 procedure Get_Imported_Directories
470 (Project : Project_Id;
471 Data : in out Project_Data);
472 -- Find the necessary switches -I to be used when compiling sources of
473 -- languages other than Ada, in a specified project file. Cache the result
474 -- in component Imported_Directories_Switches of the project data. For
475 -- gcc/g++ compilers, get the value of the C*_INCLUDE_PATH, instead.
477 procedure Initialize;
478 -- Do the necessary package initialization and process the command line
481 function Is_Included_In_Global_Archive
482 (Object_Name : File_Name_Type;
483 Project : Project_Id) return Boolean;
484 -- Return True if the object Object_Name is not overridden by a source
485 -- in a project extending project Project.
487 procedure Link_Executables;
490 procedure Report_Error (S1 : String; S2 : String := ""; S3 : String := "");
491 -- Report an error. If Keep_Going is False, just call Osint.Fail. If
492 -- Keep_Going is True, display the error and increase the total number of
495 procedure Report_Total_Errors (Kind : String);
496 -- If Total_Number_Of_Errors is not zero, report it, and fail
498 procedure Scan_Arg (Arg : String);
499 -- Process one command line argument
501 function Strip_CR_LF (Text : String) return String;
502 -- Remove characters ASCII.CR and ASCII.LF from a String
511 procedure Add_Archives (For_Gnatmake : Boolean) is
512 Last_Arg : constant Natural := Last_Argument;
513 -- The position of the last argument before adding the archives. Used to
514 -- reverse the order of the arguments added when processing the
517 procedure Recursive_Add_Archives (Project : Project_Id);
518 -- Recursive procedure to add the archive of a project file, if any,
519 -- then call itself for the project imported.
521 ----------------------------
522 -- Recursive_Add_Archives --
523 ----------------------------
525 procedure Recursive_Add_Archives (Project : Project_Id) is
527 Imported : Project_List;
530 procedure Add_Archive_Path;
531 -- For a library project or the main project, add the archive
532 -- path to the arguments.
534 ----------------------
535 -- Add_Archive_Path --
536 ----------------------
538 procedure Add_Archive_Path is
539 Increment : Positive;
540 Prev_Last : Positive;
545 -- If it is a library project file, nothing to do if gnatmake
546 -- will be invoked, because gnatmake will take care of it, even
547 -- if the library is not an Ada library.
549 if not For_Gnatmake then
550 if Data.Library_Kind = Static then
552 (Get_Name_String (Data.Display_Library_Dir) &
553 Directory_Separator &
554 "lib" & Get_Name_String (Data.Library_Name) &
559 -- As we first insert in the reverse order,
560 -- -L<dir> is put after -l<lib>
563 ("-l" & Get_Name_String (Data.Library_Name),
566 Get_Name_String (Data.Display_Library_Dir);
569 ("-L" & Name_Buffer (1 .. Name_Len),
572 -- If there is a run path option, prepend this directory
573 -- to the library path. It is probable that the order of
574 -- the directories in the path option is not important,
575 -- but just in case put the directories in the same order
578 if Path_Option /= null then
580 -- If it is not the first directory, make room at the
581 -- beginning of the table, including for a path
584 if Lib_Path.Last > 0 then
585 Increment := Name_Len + 1;
586 Prev_Last := Lib_Path.Last;
587 Lib_Path.Set_Last (Prev_Last + Increment);
589 for Index in reverse 1 .. Prev_Last loop
590 Lib_Path.Table (Index + Increment) :=
591 Lib_Path.Table (Index);
594 Lib_Path.Table (Increment) := Path_Separator;
597 -- If it is the first directory, just set
598 -- Last to the length of the directory.
600 Lib_Path.Set_Last (Name_Len);
603 -- Put the directory at the beginning of the
606 for Index in 1 .. Name_Len loop
607 Lib_Path.Table (Index) := Name_Buffer (Index);
613 -- For a non-library project, the only archive needed is the one
614 -- for the main project, if there is one.
616 elsif Project = Main_Project and then Global_Archive_Exists then
618 (Get_Name_String (Data.Display_Object_Dir) &
619 Directory_Separator &
620 "lib" & Get_Name_String (Data.Display_Name)
624 end Add_Archive_Path;
627 -- Nothing to do when there is no project specified
629 if Project /= No_Project then
630 Data := Project_Tree.Projects.Table (Project);
632 -- Nothing to do if the project has already been processed
634 if not Data.Seen then
636 -- Mark the project as processed, to avoid processing it again
638 Project_Tree.Projects.Table (Project).Seen := True;
640 Recursive_Add_Archives (Data.Extends);
642 Imported := Data.Imported_Projects;
644 -- Call itself recursively for all imported projects
646 while Imported /= Empty_Project_List loop
647 Prj := Project_Tree.Project_Lists.Table
650 if Prj /= No_Project then
651 while Project_Tree.Projects.Table
652 (Prj).Extended_By /= No_Project
654 Prj := Project_Tree.Projects.Table
658 Recursive_Add_Archives (Prj);
661 Imported := Project_Tree.Project_Lists.Table
665 -- If there is sources of language other than Ada in this
666 -- project, add the path of the archive to Arguments.
668 if Project = Main_Project
669 or else Data.Other_Sources_Present
675 end Recursive_Add_Archives;
677 -- Start of processing for Add_Archives
680 -- First, mark all projects as not processed
682 for Project in Project_Table.First ..
683 Project_Table.Last (Project_Tree.Projects)
685 Project_Tree.Projects.Table (Project).Seen := False;
688 -- Take care of the run path option
690 if Path_Option = null then
691 Path_Option := MLib.Linker_Library_Path_Option;
694 Lib_Path.Set_Last (0);
696 -- Add archives in the reverse order
698 Recursive_Add_Archives (Main_Project);
700 -- And reverse the order
705 Temp : String_Access;
708 First := Last_Arg + 1;
709 Last := Last_Argument;
710 while First < Last loop
711 Temp := Arguments (First);
712 Arguments (First) := Arguments (Last);
713 Arguments (Last) := Temp;
724 procedure Add_Argument (Arg : String_Access; Display : Boolean) is
726 -- Nothing to do if no argument is specified or if argument is empty
728 if Arg /= null or else Arg'Length = 0 then
730 -- Reallocate arrays if necessary
732 if Last_Argument = Arguments'Last then
734 New_Arguments : constant Argument_List_Access :=
736 (1 .. Last_Argument +
737 Initial_Argument_Count);
739 New_Arguments_Displayed : constant Booleans :=
741 (1 .. Last_Argument +
742 Initial_Argument_Count);
745 New_Arguments (Arguments'Range) := Arguments.all;
747 -- To avoid deallocating the strings, nullify all components
748 -- of Arguments before calling Free.
750 Arguments.all := (others => null);
753 Arguments := New_Arguments;
755 New_Arguments_Displayed (Arguments_Displayed'Range) :=
756 Arguments_Displayed.all;
757 Free (Arguments_Displayed);
758 Arguments_Displayed := New_Arguments_Displayed;
762 -- Add the argument and its display indication
764 Last_Argument := Last_Argument + 1;
765 Arguments (Last_Argument) := Arg;
766 Arguments_Displayed (Last_Argument) := Display;
770 procedure Add_Argument (Arg : String; Display : Boolean) is
771 Argument : String_Access := null;
774 -- Nothing to do if argument is empty
776 if Arg'Length > 0 then
778 -- Check if the argument is already in the Cache_Args table.
779 -- If it is already there, reuse the allocated value.
781 for Index in 1 .. Cache_Args.Last loop
782 if Cache_Args.Table (Index).all = Arg then
783 Argument := Cache_Args.Table (Index);
788 -- If the argument is not in the cache, create a new entry in the
791 if Argument = null then
792 Argument := new String'(Arg);
793 Cache_Args.Increment_Last;
794 Cache_Args.Table (Cache_Args.Last) := Argument;
797 -- And add the argument
799 Add_Argument (Argument, Display);
807 procedure Add_Arguments (Args : Argument_List; Display : Boolean) is
809 -- Reallocate the arrays, if necessary
811 if Last_Argument + Args'Length > Arguments'Last then
813 New_Arguments : constant Argument_List_Access :=
815 (1 .. Last_Argument + Args'Length +
816 Initial_Argument_Count);
818 New_Arguments_Displayed : constant Booleans :=
820 (1 .. Last_Argument +
822 Initial_Argument_Count);
825 New_Arguments (1 .. Last_Argument) :=
826 Arguments (1 .. Last_Argument);
828 -- To avoid deallocating the strings, nullify all components
829 -- of Arguments before calling Free.
831 Arguments.all := (others => null);
834 Arguments := New_Arguments;
835 New_Arguments_Displayed (1 .. Last_Argument) :=
836 Arguments_Displayed (1 .. Last_Argument);
837 Free (Arguments_Displayed);
838 Arguments_Displayed := New_Arguments_Displayed;
842 -- Add the new arguments and the display indications
844 Arguments (Last_Argument + 1 .. Last_Argument + Args'Length) := Args;
845 Arguments_Displayed (Last_Argument + 1 .. Last_Argument + Args'Length) :=
847 Last_Argument := Last_Argument + Args'Length;
854 procedure Add_Option (Arg : String) is
855 Option : constant String_Access := new String'(Arg);
858 case Current_Processor is
864 -- Add option to the linker table
866 Linker_Options.Increment_Last;
867 Linker_Options.Table (Linker_Options.Last) := Option;
871 -- Add option to the compiler option table, depending on the
872 -- value of Current_Language.
874 Comp_Opts.Increment_Last (Options (Current_Language));
875 Options (Current_Language).Table
876 (Comp_Opts.Last (Options (Current_Language))) := Option;
885 procedure Add_Source_Id (Project : Project_Id; Id : Other_Source_Id) is
887 -- Reallocate the array, if necessary
889 if Last_Source = Source_Indexes'Last then
891 New_Indexes : constant Source_Indexes_Ref :=
892 new Source_Index_Array
893 (1 .. Source_Indexes'Last +
894 Initial_Source_Index_Count);
896 New_Indexes (Source_Indexes'Range) := Source_Indexes.all;
897 Free (Source_Indexes);
898 Source_Indexes := New_Indexes;
902 Last_Source := Last_Source + 1;
903 Source_Indexes (Last_Source) := (Project, Id, False);
906 ----------------------------
907 -- Add_Search_Directories --
908 ----------------------------
910 procedure Add_Search_Directories
911 (Data : Project_Data;
912 Language : First_Language_Indexes)
915 -- If a GNU compiler is used, set the CPATH environment variable,
916 -- if it does not already has the correct value.
918 if Compiler_Is_Gcc (Language) then
919 if Current_Include_Paths (Language) /= Data.Include_Path then
920 Current_Include_Paths (Language) := Data.Include_Path;
921 Setenv (CPATH, Data.Include_Path.all);
925 Add_Arguments (Data.Imported_Directories_Switches.all, Verbose_Mode);
927 end Add_Search_Directories;
933 procedure Add_Switches
934 (Data : Project_Data;
936 Language : Language_Index;
937 File_Name : File_Name_Type)
939 Switches : Variable_Value;
940 -- The switches, if any, for the file/language
943 -- The id of the package where to look for the switches
945 Defaults : Array_Element_Id;
946 -- The Default_Switches associative array
948 Switches_Array : Array_Element_Id;
949 -- The Switches associative array
951 Element_Id : String_List_Id;
952 Element : String_Element;
955 -- First, choose the proper package
962 Pkg := Value_Of (Name_Linker, Data.Decl.Packages, Project_Tree);
965 Pkg := Value_Of (Name_Compiler, Data.Decl.Packages, Project_Tree);
968 if Pkg /= No_Package then
970 -- Get the Switches ("file name"), if they exist
972 Switches_Array := Prj.Util.Value_Of
973 (Name => Name_Switches,
974 In_Arrays => Project_Tree.Packages.Table
976 In_Tree => Project_Tree);
980 (Index => Name_Id (File_Name),
982 In_Array => Switches_Array,
983 In_Tree => Project_Tree);
985 -- Otherwise, get the Default_Switches ("language"), if they exist
987 if Switches = Nil_Variable_Value then
988 Defaults := Prj.Util.Value_Of
989 (Name => Name_Default_Switches,
990 In_Arrays => Project_Tree.Packages.Table
992 In_Tree => Project_Tree);
993 Switches := Prj.Util.Value_Of
994 (Index => Language_Names.Table (Language),
996 In_Array => Defaults,
997 In_Tree => Project_Tree);
1000 -- If there are switches, add them to Arguments
1002 if Switches /= Nil_Variable_Value then
1003 Element_Id := Switches.Values;
1004 while Element_Id /= Nil_String loop
1005 Element := Project_Tree.String_Elements.Table
1008 if Element.Value /= No_Name then
1009 Get_Name_String (Element.Value);
1011 if not Quiet_Output then
1013 -- When not in quiet output (no -q), check that the
1014 -- switch is not the concatenation of several valid
1015 -- switches, such as "-g -v". If it is, issue a warning.
1017 Check (Option => Name_Buffer (1 .. Name_Len));
1020 Add_Argument (Name_Buffer (1 .. Name_Len), True);
1023 Element_Id := Element.Next;
1029 --------------------------
1030 -- Build_Global_Archive --
1031 --------------------------
1033 procedure Build_Global_Archive is
1034 Data : Project_Data := Project_Tree.Projects.Table (Main_Project);
1035 Source_Id : Other_Source_Id;
1036 S_Id : Other_Source_Id;
1037 Source : Other_Source;
1040 Archive_Name : constant String :=
1042 & Get_Name_String (Data.Display_Name)
1045 -- The name of the archive file for this project
1047 Archive_Dep_Name : constant String :=
1049 & Get_Name_String (Data.Display_Name)
1051 -- The name of the archive dependency file for this project
1053 Need_To_Rebuild : Boolean := Need_To_Rebuild_Global_Archive;
1054 -- When True, archive will be rebuilt
1056 File : Prj.Util.Text_File;
1057 Object_Path : Path_Name_Type;
1058 Time_Stamp : Time_Stamp_Type;
1059 Saved_Last_Argument : Natural;
1060 First_Object : Natural;
1064 Check_Archive_Builder;
1066 if Project_Of_Current_Object_Directory /= Main_Project then
1067 Project_Of_Current_Object_Directory := Main_Project;
1068 Change_Dir (Get_Name_String (Data.Object_Directory));
1070 if Verbose_Mode then
1071 Write_Str ("Changing to object directory of """);
1072 Write_Name (Data.Display_Name);
1073 Write_Str (""": """);
1074 Write_Name (Data.Display_Object_Dir);
1079 if not Need_To_Rebuild then
1080 if Verbose_Mode then
1081 Write_Str (" Checking ");
1082 Write_Line (Archive_Name);
1085 -- If the archive does not exist, of course it needs to be built
1087 if not Is_Regular_File (Archive_Name) then
1088 Need_To_Rebuild := True;
1090 if Verbose_Mode then
1091 Write_Line (" -> archive does not exist");
1094 -- Archive does exist
1097 -- Check the archive dependency file
1099 Open (File, Archive_Dep_Name);
1101 -- If the archive dependency file does not exist, we need to
1102 -- rebuild the archive and to create its dependency file.
1104 if not Is_Valid (File) then
1105 Need_To_Rebuild := True;
1107 if Verbose_Mode then
1108 Write_Str (" -> archive dependency file ");
1109 Write_Str (Archive_Dep_Name);
1110 Write_Line (" does not exist");
1114 -- Put all sources of language other than Ada in Source_Indexes
1117 Local_Data : Project_Data;
1122 for Proj in Project_Table.First ..
1123 Project_Table.Last (Project_Tree.Projects)
1125 Local_Data := Project_Tree.Projects.Table (Proj);
1127 if not Local_Data.Library then
1128 Source_Id := Local_Data.First_Other_Source;
1129 while Source_Id /= No_Other_Source loop
1130 Add_Source_Id (Proj, Source_Id);
1131 Source_Id := Project_Tree.Other_Sources.Table
1138 -- Read the dependency file, line by line
1140 while not End_Of_File (File) loop
1141 Get_Line (File, Name_Buffer, Name_Len);
1143 -- First line is the path of the object file
1145 Object_Path := Name_Find;
1146 Source_Id := No_Other_Source;
1148 -- Check if this object file is for a source of this project
1150 for S in 1 .. Last_Source loop
1151 S_Id := Source_Indexes (S).Id;
1152 Source := Project_Tree.Other_Sources.Table (S_Id);
1154 if (not Source_Indexes (S).Found)
1155 and then Source.Object_Path = Object_Path
1157 -- We have found the object file: get the source data,
1158 -- and mark it as found.
1161 Source_Indexes (S).Found := True;
1166 -- If it is not for a source of this project, then the
1167 -- archive needs to be rebuilt.
1169 if Source_Id = No_Other_Source then
1170 Need_To_Rebuild := True;
1171 if Verbose_Mode then
1173 Write_Str (Get_Name_String (Object_Path));
1174 Write_Line (" is not an object of any project");
1180 -- The second line is the time stamp of the object file. If
1181 -- there is no next line, then the dependency file is
1182 -- truncated, and the archive need to be rebuilt.
1184 if End_Of_File (File) then
1185 Need_To_Rebuild := True;
1187 if Verbose_Mode then
1188 Write_Str (" -> archive dependency file ");
1189 Write_Line (" is truncated");
1195 Get_Line (File, Name_Buffer, Name_Len);
1197 -- If the line has the wrong number of characters, then
1198 -- the dependency file is incorrectly formatted, and the
1199 -- archive needs to be rebuilt.
1201 if Name_Len /= Time_Stamp_Length then
1202 Need_To_Rebuild := True;
1204 if Verbose_Mode then
1205 Write_Str (" -> archive dependency file ");
1206 Write_Line (" is incorrectly formatted (time stamp)");
1212 Time_Stamp := Time_Stamp_Type (Name_Buffer (1 .. Name_Len));
1214 -- If the time stamp in the dependency file is different
1215 -- from the time stamp of the object file, then the archive
1216 -- needs to be rebuilt.
1218 if Time_Stamp /= Source.Object_TS then
1219 Need_To_Rebuild := True;
1221 if Verbose_Mode then
1222 Write_Str (" -> time stamp of ");
1223 Write_Str (Get_Name_String (Object_Path));
1224 Write_Str (" is incorrect in the archive");
1225 Write_Line (" dependency file");
1237 if not Need_To_Rebuild then
1238 if Verbose_Mode then
1239 Write_Line (" -> up to date");
1242 -- No need to create a global archive, if there is no object
1243 -- file to put into.
1245 Global_Archive_Exists := Last_Source /= 0;
1247 -- Archive needs to be rebuilt
1250 -- If archive already exists, first delete it
1252 -- Comment needed on why we discard result???
1254 if Is_Regular_File (Archive_Name) then
1255 Delete_File (Archive_Name, Discard);
1260 -- Start with the options found in MLib.Tgt (usually just "rc")
1262 Add_Arguments (Archive_Builder_Options.all, True);
1264 -- Followed by the archive name
1266 Add_Argument (Archive_Name, True);
1268 First_Object := Last_Argument;
1270 -- Followed by all the object files of the non library projects
1272 for Proj in Project_Table.First ..
1273 Project_Table.Last (Project_Tree.Projects)
1275 Data := Project_Tree.Projects.Table (Proj);
1277 if not Data.Library then
1278 Source_Id := Data.First_Other_Source;
1279 while Source_Id /= No_Other_Source loop
1281 Project_Tree.Other_Sources.Table (Source_Id);
1283 -- Only include object file name that have not been
1284 -- overriden in extending projects.
1286 if Is_Included_In_Global_Archive
1287 (Source.Object_Name, Proj)
1290 (Get_Name_String (Source.Object_Path),
1291 Verbose_Mode or (First_Object = Last_Argument));
1294 Source_Id := Source.Next;
1299 -- No need to create a global archive, if there is no object
1300 -- file to put into.
1302 Global_Archive_Exists := Last_Argument > First_Object;
1304 if Global_Archive_Exists then
1306 -- If the archive is built, then linking will need to occur
1309 Need_To_Relink := True;
1311 -- Spawn the archive builder (ar)
1313 Saved_Last_Argument := Last_Argument;
1314 Last_Argument := First_Object + Max_In_Archives;
1316 if Last_Argument > Saved_Last_Argument then
1317 Last_Argument := Saved_Last_Argument;
1322 Archive_Builder_Path,
1326 (Archive_Builder_Path.all,
1327 Arguments (1 .. Last_Argument),
1330 exit when not Success
1331 or else Last_Argument = Saved_Last_Argument;
1334 Arguments (3 .. Saved_Last_Argument - Last_Argument + 2) :=
1335 Arguments (Last_Argument + 1 .. Saved_Last_Argument);
1336 Saved_Last_Argument := Saved_Last_Argument - Last_Argument + 2;
1339 -- If the archive was built, run the archive indexer (ranlib)
1344 if Archive_Indexer_Path /= null then
1346 Add_Argument (Archive_Name, True);
1348 Display_Command (Archive_Indexer, Archive_Indexer_Path);
1351 (Archive_Indexer_Path.all, Arguments (1 .. 1), Success);
1355 -- Running ranlib failed, delete the dependency file,
1358 if Is_Regular_File (Archive_Dep_Name) then
1359 Delete_File (Archive_Dep_Name, Success);
1362 -- And report the error
1365 ("running" & Archive_Indexer & " for project """,
1366 Get_Name_String (Data.Display_Name),
1372 -- The archive was correctly built, create its dependency file
1374 Create_Global_Archive_Dependency_File (Archive_Dep_Name);
1376 -- Building the archive failed, delete dependency file if one
1380 if Is_Regular_File (Archive_Dep_Name) then
1381 Delete_File (Archive_Dep_Name, Success);
1384 -- And report the error
1387 ("building archive for project """,
1388 Get_Name_String (Data.Display_Name),
1393 end Build_Global_Archive;
1399 procedure Build_Library (Project : Project_Id; Unconditionally : Boolean) is
1400 Data : constant Project_Data :=
1401 Project_Tree.Projects.Table (Project);
1402 Source_Id : Other_Source_Id;
1403 Source : Other_Source;
1405 Archive_Name : constant String :=
1406 "lib" & Get_Name_String (Data.Library_Name)
1407 & '.' & Archive_Ext;
1408 -- The name of the archive file for this project
1410 Archive_Dep_Name : constant String :=
1411 "lib" & Get_Name_String (Data.Library_Name)
1413 -- The name of the archive dependency file for this project
1415 Need_To_Rebuild : Boolean := Unconditionally;
1416 -- When True, archive will be rebuilt
1418 File : Prj.Util.Text_File;
1420 Object_Name : File_Name_Type;
1421 Time_Stamp : Time_Stamp_Type;
1422 Driver_Name : Name_Id := No_Name;
1424 Lib_Opts : Argument_List_Access := No_Argument'Access;
1427 -- Nothing to do if the project is externally built
1429 if Data.Externally_Built then
1433 Check_Archive_Builder;
1435 -- If Unconditionally is False, check if the archive need to be built
1437 if not Need_To_Rebuild then
1438 if Verbose_Mode then
1439 Write_Str (" Checking ");
1440 Write_Line (Archive_Name);
1443 -- If the archive does not exist, of course it needs to be built
1445 if not Is_Regular_File (Archive_Name) then
1446 Need_To_Rebuild := True;
1448 if Verbose_Mode then
1449 Write_Line (" -> archive does not exist");
1452 -- Archive does exist
1455 -- Check the archive dependency file
1457 Open (File, Archive_Dep_Name);
1459 -- If the archive dependency file does not exist, we need to
1460 -- rebuild the archive and to create its dependency file.
1462 if not Is_Valid (File) then
1463 Need_To_Rebuild := True;
1465 if Verbose_Mode then
1466 Write_Str (" -> archive dependency file ");
1467 Write_Str (Archive_Dep_Name);
1468 Write_Line (" does not exist");
1472 -- Put all sources of language other than Ada in Source_Indexes
1476 Source_Id := Data.First_Other_Source;
1477 while Source_Id /= No_Other_Source loop
1478 Add_Source_Id (Project, Source_Id);
1480 Project_Tree.Other_Sources.Table (Source_Id).Next;
1483 -- Read the dependency file, line by line
1485 while not End_Of_File (File) loop
1486 Get_Line (File, Name_Buffer, Name_Len);
1488 -- First line is the name of an object file
1490 Object_Name := Name_Find;
1491 Source_Id := No_Other_Source;
1493 -- Check if this object file is for a source of this project
1495 for S in 1 .. Last_Source loop
1496 if (not Source_Indexes (S).Found)
1498 Project_Tree.Other_Sources.Table
1499 (Source_Indexes (S).Id).Object_Name = Object_Name
1501 -- We have found the object file: get the source
1502 -- data, and mark it as found.
1504 Source_Id := Source_Indexes (S).Id;
1505 Source := Project_Tree.Other_Sources.Table
1507 Source_Indexes (S).Found := True;
1512 -- If it is not for a source of this project, then the
1513 -- archive needs to be rebuilt.
1515 if Source_Id = No_Other_Source then
1516 Need_To_Rebuild := True;
1518 if Verbose_Mode then
1520 Write_Str (Get_Name_String (Object_Name));
1521 Write_Line (" is not an object of the project");
1527 -- The second line is the time stamp of the object file.
1528 -- If there is no next line, then the dependency file is
1529 -- truncated, and the archive need to be rebuilt.
1531 if End_Of_File (File) then
1532 Need_To_Rebuild := True;
1534 if Verbose_Mode then
1535 Write_Str (" -> archive dependency file ");
1536 Write_Line (" is truncated");
1542 Get_Line (File, Name_Buffer, Name_Len);
1544 -- If the line has the wrong number of character, then
1545 -- the dependency file is incorrectly formatted, and the
1546 -- archive needs to be rebuilt.
1548 if Name_Len /= Time_Stamp_Length then
1549 Need_To_Rebuild := True;
1551 if Verbose_Mode then
1552 Write_Str (" -> archive dependency file ");
1553 Write_Line (" is incorrectly formatted (time stamp)");
1559 Time_Stamp := Time_Stamp_Type (Name_Buffer (1 .. Name_Len));
1561 -- If the time stamp in the dependency file is different
1562 -- from the time stamp of the object file, then the archive
1563 -- needs to be rebuilt.
1565 if Time_Stamp /= Source.Object_TS then
1566 Need_To_Rebuild := True;
1568 if Verbose_Mode then
1569 Write_Str (" -> time stamp of ");
1570 Write_Str (Get_Name_String (Object_Name));
1571 Write_Str (" is incorrect in the archive");
1572 Write_Line (" dependency file");
1581 if not Need_To_Rebuild then
1583 -- Now, check if all object files of the project have been
1584 -- accounted for. If any of them is not in the dependency
1585 -- file, the archive needs to be rebuilt.
1587 for Index in 1 .. Last_Source loop
1588 if not Source_Indexes (Index).Found then
1589 Need_To_Rebuild := True;
1591 if Verbose_Mode then
1592 Source_Id := Source_Indexes (Index).Id;
1593 Source := Project_Tree.Other_Sources.Table
1596 Write_Str (Get_Name_String (Source.Object_Name));
1597 Write_Str (" is not in the archive ");
1598 Write_Line ("dependency file");
1606 if (not Need_To_Rebuild) and Verbose_Mode then
1607 Write_Line (" -> up to date");
1613 -- Build the library if necessary
1615 if Need_To_Rebuild then
1617 -- If a library is built, then linking will need to occur
1620 Need_To_Relink := True;
1624 -- If there are sources in Ada, then gnatmake will build the library,
1625 -- so nothing to do.
1627 if not Data.Langs (Ada_Language_Index) then
1629 -- Get all the object files of the project
1631 Source_Id := Data.First_Other_Source;
1632 while Source_Id /= No_Other_Source loop
1633 Source := Project_Tree.Other_Sources.Table (Source_Id);
1635 (Get_Name_String (Source.Object_Name), Verbose_Mode);
1636 Source_Id := Source.Next;
1639 -- If it is a library, it need to be built it the same way Ada
1640 -- libraries are built.
1642 if Data.Library_Kind = Static then
1644 (Ofiles => Arguments (1 .. Last_Argument),
1645 Output_File => Get_Name_String (Data.Library_Name),
1646 Output_Dir => Get_Name_String (Data.Display_Library_Dir));
1649 -- Link with g++ if C++ is one of the languages, otherwise
1650 -- building the library may fail with unresolved symbols.
1652 if C_Plus_Plus_Is_Used then
1653 if Compiler_Names (C_Plus_Plus_Language_Index) = null then
1654 Get_Compiler (C_Plus_Plus_Language_Index);
1657 if Compiler_Is_Gcc (C_Plus_Plus_Language_Index) then
1659 Add_Str_To_Name_Buffer
1660 (Compiler_Names (C_Plus_Plus_Language_Index).all);
1661 Driver_Name := Name_Find;
1665 -- If Library_Options is specified, add these options
1668 Library_Options : constant Variable_Value :=
1670 (Name_Library_Options,
1671 Data.Decl.Attributes,
1675 if not Library_Options.Default then
1677 Current : String_List_Id;
1678 Element : String_Element;
1681 Current := Library_Options.Values;
1682 while Current /= Nil_String loop
1684 Project_Tree.String_Elements.Table (Current);
1685 Get_Name_String (Element.Value);
1687 if Name_Len /= 0 then
1688 Library_Opts.Increment_Last;
1689 Library_Opts.Table (Library_Opts.Last) :=
1690 new String'(Name_Buffer (1 .. Name_Len));
1693 Current := Element.Next;
1699 new Argument_List'(Argument_List
1700 (Library_Opts.Table (1 .. Library_Opts.Last)));
1703 MLib.Tgt.Build_Dynamic_Library
1704 (Ofiles => Arguments (1 .. Last_Argument),
1705 Options => Lib_Opts.all,
1706 Interfaces => No_Argument,
1707 Lib_Filename => Get_Name_String (Data.Library_Name),
1708 Lib_Dir => Get_Name_String (Data.Library_Dir),
1709 Symbol_Data => No_Symbols,
1710 Driver_Name => Driver_Name,
1712 Auto_Init => False);
1716 -- Create fake empty archive, so we can check its time stamp later
1719 Archive : Ada.Text_IO.File_Type;
1721 Create (Archive, Out_File, Archive_Name);
1725 Create_Archive_Dependency_File
1726 (Archive_Dep_Name, Data.First_Other_Source);
1734 procedure Check (Option : String) is
1735 First : Positive := Option'First;
1739 for Index in Option'First + 1 .. Option'Last - 1 loop
1740 if Option (Index) = ' ' and then Option (Index + 1) = '-' then
1741 Write_Str ("warning: switch """);
1743 Write_Str (""" is suspicious; consider using ");
1746 while Last <= Option'Last loop
1747 if Option (Last) = ' ' then
1748 if First /= Option'First then
1753 Write_Str (Option (First .. Last - 1));
1756 while Last <= Option'Last and then Option (Last) = ' ' loop
1763 if Last = Option'Last then
1764 if First /= Option'First then
1769 Write_Str (Option (First .. Last));
1777 Write_Line (" instead");
1783 ---------------------------
1784 -- Check_Archive_Builder --
1785 ---------------------------
1787 procedure Check_Archive_Builder is
1789 -- First, make sure that the archive builder (ar) is on the path
1791 if Archive_Builder_Path = null then
1792 Archive_Builder_Path := Locate_Exec_On_Path (Archive_Builder);
1794 if Archive_Builder_Path = null then
1796 ("unable to locate archive builder """,
1801 -- If there is an archive indexer (ranlib), try to locate it on the
1802 -- path. Don't fail if it is not found.
1804 if Archive_Indexer /= "" then
1805 Archive_Indexer_Path := Locate_Exec_On_Path (Archive_Indexer);
1808 end Check_Archive_Builder;
1810 ------------------------------
1811 -- Check_Compilation_Needed --
1812 ------------------------------
1814 procedure Check_Compilation_Needed
1815 (Source : Other_Source;
1816 Need_To_Compile : out Boolean)
1818 Source_Name : constant String := Get_Name_String (Source.File_Name);
1819 Source_Path : constant String := Get_Name_String (Source.Path_Name);
1820 Object_Name : constant String := Get_Name_String (Source.Object_Name);
1821 C_Object_Name : String := Object_Name;
1822 Dep_Name : constant String := Get_Name_String (Source.Dep_Name);
1823 C_Source_Path : String := Source_Path;
1825 Source_In_Dependencies : Boolean := False;
1826 -- Set True if source was found in dependency file of its object file
1828 Dep_File : Prj.Util.Text_File;
1832 Looping : Boolean := False;
1833 -- Set to True at the end of the first Big_Loop
1836 Canonical_Case_File_Name (C_Source_Path);
1837 Canonical_Case_File_Name (C_Object_Name);
1839 -- Assume the worst, so that statement "return;" may be used if there
1842 Need_To_Compile := True;
1844 if Verbose_Mode then
1845 Write_Str (" Checking ");
1846 Write_Str (Source_Name);
1847 Write_Line (" ... ");
1850 -- If object file does not exist, of course source need to be compiled
1852 if Source.Object_TS = Empty_Time_Stamp then
1853 if Verbose_Mode then
1854 Write_Str (" -> object file ");
1855 Write_Str (Object_Name);
1856 Write_Line (" does not exist");
1862 -- If the object file has been created before the last modification
1863 -- of the source, the source need to be recompiled.
1865 if Source.Object_TS < Source.Source_TS then
1866 if Verbose_Mode then
1867 Write_Str (" -> object file ");
1868 Write_Str (Object_Name);
1869 Write_Line (" has time stamp earlier than source");
1875 -- If there is no dependency file, then the source needs to be
1876 -- recompiled and the dependency file need to be created.
1878 if Source.Dep_TS = Empty_Time_Stamp then
1879 if Verbose_Mode then
1880 Write_Str (" -> dependency file ");
1881 Write_Str (Dep_Name);
1882 Write_Line (" does not exist");
1888 -- The source needs to be recompiled if the source has been modified
1889 -- after the dependency file has been created.
1891 if Source.Dep_TS < Source.Source_TS then
1892 if Verbose_Mode then
1893 Write_Str (" -> dependency file ");
1894 Write_Str (Dep_Name);
1895 Write_Line (" has time stamp earlier than source");
1901 -- Look for all dependencies
1903 Open (Dep_File, Dep_Name);
1905 -- If dependency file cannot be open, we need to recompile the source
1907 if not Is_Valid (Dep_File) then
1908 if Verbose_Mode then
1909 Write_Str (" -> could not open dependency file ");
1910 Write_Line (Dep_Name);
1916 -- Loop Big_Loop is executed several times only when the dependency file
1917 -- contains several times
1918 -- <object file>: <source1> ...
1919 -- When there is only one of such occurence, Big_Loop is exited
1920 -- successfully at the beginning of the second loop.
1925 End_Of_File_Reached : Boolean := False;
1929 if End_Of_File (Dep_File) then
1930 End_Of_File_Reached := True;
1934 Get_Line (Dep_File, Name_Buffer, Name_Len);
1936 exit when Name_Len > 0 and then Name_Buffer (1) /= '#';
1939 -- If dependency file contains only empty lines or comments, then
1940 -- dependencies are unknown, and the source needs to be
1943 if End_Of_File_Reached then
1944 -- If we have reached the end of file after the first loop,
1945 -- there is nothing else to do.
1947 exit Big_Loop when Looping;
1949 if Verbose_Mode then
1950 Write_Str (" -> dependency file ");
1951 Write_Str (Dep_Name);
1952 Write_Line (" is empty");
1961 Finish := Index (Name_Buffer (1 .. Name_Len), ": ");
1964 Canonical_Case_File_Name (Name_Buffer (1 .. Finish - 1));
1967 -- First line must start with name of object file, followed by colon
1969 if Finish = 0 or else
1970 Name_Buffer (1 .. Finish - 1) /= C_Object_Name
1972 if Verbose_Mode then
1973 Write_Str (" -> dependency file ");
1974 Write_Str (Dep_Name);
1975 Write_Line (" has wrong format");
1982 Start := Finish + 2;
1984 -- Process each line
1988 Line : String := Name_Buffer (1 .. Name_Len);
1989 Last : Natural := Name_Len;
1994 -- Find the beginning of the next source path name
1996 while Start < Last and then Line (Start) = ' ' loop
2000 -- Go to next line when there is a continuation character
2001 -- \ at the end of the line.
2003 exit Name_Loop when Start = Last
2004 and then Line (Start) = '\';
2006 -- We should not be at the end of the line, without
2007 -- a continuation character \.
2009 if Start = Last then
2010 if Verbose_Mode then
2011 Write_Str (" -> dependency file ");
2012 Write_Str (Dep_Name);
2013 Write_Line (" has wrong format");
2020 -- Look for the end of the source path name
2023 while Finish < Last loop
2024 if Line (Finish) = '\' then
2026 -- On Windows, a '\' is part of the path name,
2027 -- except when it is followed by another '\' or by
2028 -- a space. On other platforms, when we are getting
2029 -- a '\' that is not the last character of the
2030 -- line, the next character is part of the path
2031 -- name, even if it is a space.
2034 and then Line (Finish + 1) /= '\'
2035 and then Line (Finish + 1) /= ' '
2037 Finish := Finish + 1;
2040 Line (Finish .. Last - 1) :=
2041 Line (Finish + 1 .. Last);
2046 -- A space that is not preceded by '\' indicates
2047 -- the end of the path name.
2049 exit when Line (Finish + 1) = ' ';
2051 Finish := Finish + 1;
2055 -- Check this source
2058 Src_Name : constant String :=
2061 Line (Start .. Finish),
2062 Resolve_Links => False,
2063 Case_Sensitive => False);
2064 Src_TS : Time_Stamp_Type;
2067 -- If it is original source, set
2068 -- Source_In_Dependencies.
2070 if Src_Name = C_Source_Path then
2071 Source_In_Dependencies := True;
2075 Add_Str_To_Name_Buffer (Src_Name);
2076 Src_TS := File_Stamp (File_Name_Type'(Name_Find));
2078 -- If the source does not exist, we need to recompile
2080 if Src_TS = Empty_Time_Stamp then
2081 if Verbose_Mode then
2082 Write_Str (" -> source ");
2083 Write_Str (Src_Name);
2084 Write_Line (" does not exist");
2090 -- If the source has been modified after the object
2091 -- file, we need to recompile.
2093 elsif Src_TS > Source.Object_TS then
2094 if Verbose_Mode then
2095 Write_Str (" -> source ");
2096 Write_Str (Src_Name);
2098 (" has time stamp later than object file");
2106 -- If the source path name ends the line, we are done
2108 exit Line_Loop when Finish = Last;
2110 -- Go get the next source on the line
2112 Start := Finish + 1;
2116 -- If we are here, we had a continuation character \ at the end
2117 -- of the line, so we continue with the next line.
2119 Get_Line (Dep_File, Name_Buffer, Name_Len);
2124 -- Set Looping at the end of the first loop
2130 -- If the original sources were not in the dependency file, then we
2131 -- need to recompile. It may mean that we are using a different source
2132 -- (different variant) for this object file.
2134 if not Source_In_Dependencies then
2135 if Verbose_Mode then
2136 Write_Str (" -> source ");
2137 Write_Str (Source_Path);
2138 Write_Line (" is not in the dependencies");
2144 -- If we are here, then everything is OK, no need to recompile
2146 if Verbose_Mode then
2147 Write_Line (" -> up to date");
2150 Need_To_Compile := False;
2151 end Check_Compilation_Needed;
2153 ---------------------------
2154 -- Check_For_C_Plus_Plus --
2155 ---------------------------
2157 procedure Check_For_C_Plus_Plus is
2159 C_Plus_Plus_Is_Used := False;
2161 for Project in Project_Table.First ..
2162 Project_Table.Last (Project_Tree.Projects)
2165 Project_Tree.Projects.Table (Project).Langs
2166 (C_Plus_Plus_Language_Index)
2168 C_Plus_Plus_Is_Used := True;
2172 end Check_For_C_Plus_Plus;
2179 (Source_Id : Other_Source_Id;
2180 Data : Project_Data;
2181 Local_Errors : in out Boolean)
2183 Source : Other_Source :=
2184 Project_Tree.Other_Sources.Table (Source_Id);
2186 CPATH : String_Access := null;
2189 -- If the compiler is not known yet, get its path name
2191 if Compiler_Names (Source.Language) = null then
2192 Get_Compiler (Source.Language);
2195 -- For non GCC compilers, get the dependency file, first calling the
2196 -- compiler with the switch -M.
2198 if not Compiler_Is_Gcc (Source.Language) then
2201 -- Add the source name, preceded by -M
2203 Add_Argument (Dash_M, True);
2204 Add_Argument (Get_Name_String (Source.Path_Name), True);
2206 -- Add the compiling switches for this source found in
2207 -- package Compiler of the project file, if they exist.
2210 (Data, Compiler, Source.Language, Source.File_Name);
2212 -- Add the compiling switches for the language specified
2213 -- on the command line, if any.
2216 J in 1 .. Comp_Opts.Last (Options (Source.Language))
2218 Add_Argument (Options (Source.Language).Table (J), True);
2221 -- Finally, add imported directory switches for this project file
2223 Add_Search_Directories (Data, Source.Language);
2225 -- And invoke the compiler using GNAT.Expect
2228 (Compiler_Names (Source.Language).all,
2229 Compiler_Paths (Source.Language));
2234 Compiler_Paths (Source.Language).all,
2235 Arguments (1 .. Last_Argument),
2237 Err_To_Out => True);
2240 Dep_File : Ada.Text_IO.File_Type;
2241 Result : Expect_Match;
2245 -- Create the dependency file
2247 Create (Dep_File, Out_File, Get_Name_String (Source.Dep_Name));
2250 Expect (FD, Result, Line_Matcher);
2252 exit when Result = Expect_Timeout;
2255 S : constant String := Strip_CR_LF (Expect_Out (FD));
2258 -- Each line of the output is put in the dependency
2259 -- file, including errors. If there are errors, the
2260 -- syntax of the dependency file will be incorrect and
2261 -- recompilation will occur automatically the next time
2262 -- the dependencies are checked.
2264 Put_Line (Dep_File, S);
2268 -- If we are here, it means we had a timeout, so the
2269 -- dependency file may be incomplete. It is safer to
2270 -- delete it, otherwise the dependencies may be wrong.
2274 Delete_File (Get_Name_String (Source.Dep_Name), Success);
2277 when Process_Died =>
2279 -- This is the normal outcome. Just close the file
2286 -- Something wrong happened. It is safer to delete the
2287 -- dependency file, otherwise the dependencies may be wrong.
2291 if Is_Open (Dep_File) then
2295 Delete_File (Get_Name_String (Source.Dep_Name), Success);
2299 -- If we cannot spawn the compiler, then the dependencies are
2300 -- not updated. It is safer then to delete the dependency file,
2301 -- otherwise the dependencies may be wrong.
2303 when Invalid_Process =>
2304 Delete_File (Get_Name_String (Source.Dep_Name), Success);
2310 -- For GCC compilers, make sure the language is always specified to
2311 -- to the GCC driver, in case the extension is not recognized by the
2312 -- GCC driver as a source of the language.
2314 if Compiler_Is_Gcc (Source.Language) then
2315 Add_Argument (Dash_x, Verbose_Mode);
2317 (Get_Name_String (Language_Names.Table (Source.Language)),
2321 Add_Argument (Dash_c, True);
2323 -- Add the compiling switches for this source found in package Compiler
2324 -- of the project file, if they exist.
2327 (Data, Compiler, Source.Language, Source.File_Name);
2329 -- Specify the source to be compiled
2331 Add_Argument (Get_Name_String (Source.Path_Name), True);
2333 -- If non static library project, compile with the PIC option if there
2334 -- is one (when there is no PIC option, MLib.Tgt.PIC_Option returns an
2335 -- empty string, and Add_Argument with an empty string has no effect).
2337 if Data.Library and then Data.Library_Kind /= Static then
2338 Add_Argument (PIC_Option, True);
2341 -- Indicate the name of the object
2343 Add_Argument (Dash_o, True);
2344 Add_Argument (Get_Name_String (Source.Object_Name), True);
2346 -- When compiler is GCC, use the magic switch that creates the
2347 -- dependency file in the correct format.
2349 if Compiler_Is_Gcc (Source.Language) then
2351 ("-Wp,-MD," & Get_Name_String (Source.Dep_Name),
2355 -- Add the compiling switches for the language specified on the command
2358 for J in 1 .. Comp_Opts.Last (Options (Source.Language)) loop
2359 Add_Argument (Options (Source.Language).Table (J), True);
2362 -- Finally, add the imported directory switches for this project file
2363 -- (or, for gcc compilers, set up the CPATH env var if needed).
2365 Add_Search_Directories (Data, Source.Language);
2367 -- Set CPATH, if compiler is GCC
2369 if Compiler_Is_Gcc (Source.Language) then
2370 CPATH := Current_Include_Paths (Source.Language);
2373 -- And invoke the compiler
2376 (Name => Compiler_Names (Source.Language).all,
2377 Path => Compiler_Paths (Source.Language),
2381 (Compiler_Paths (Source.Language).all,
2382 Arguments (1 .. Last_Argument),
2385 -- Case of successful compilation
2389 -- Update the time stamp of the object file
2391 Source.Object_TS := File_Stamp (Source.Object_Name);
2393 -- Do some sanity checks
2395 if Source.Object_TS = Empty_Time_Stamp then
2396 Local_Errors := True;
2399 Get_Name_String (Source.Object_Name),
2400 " has not been created");
2402 elsif Source.Object_TS < Source.Source_TS then
2403 Local_Errors := True;
2406 Get_Name_String (Source.Object_Name),
2407 " has not been modified");
2410 -- Everything looks fine, update the Other_Sources table
2412 Project_Tree.Other_Sources.Table (Source_Id) := Source;
2415 -- Compilation failed
2418 Local_Errors := True;
2421 Get_Name_String (Source.Path_Name),
2426 --------------------------------
2427 -- Compile_Individual_Sources --
2428 --------------------------------
2430 procedure Compile_Individual_Sources is
2431 Data : Project_Data :=
2432 Project_Tree.Projects.Table (Main_Project);
2433 Source_Id : Other_Source_Id;
2434 Source : Other_Source;
2435 Source_Name : File_Name_Type;
2436 Project_Name : String := Get_Name_String (Data.Name);
2437 Dummy : Boolean := False;
2439 Ada_Is_A_Language : constant Boolean :=
2440 Data.Langs (Ada_Language_Index);
2444 To_Mixed (Project_Name);
2445 Compile_Only := True;
2447 Get_Imported_Directories (Main_Project, Data);
2448 Project_Tree.Projects.Table (Main_Project) := Data;
2450 -- Compilation will occur in the object directory
2452 if Project_Of_Current_Object_Directory /= Main_Project then
2453 Project_Of_Current_Object_Directory := Main_Project;
2454 Change_Dir (Get_Name_String (Data.Object_Directory));
2456 if Verbose_Mode then
2457 Write_Str ("Changing to object directory of """);
2458 Write_Name (Data.Name);
2459 Write_Str (""": """);
2460 Write_Name (Data.Display_Object_Dir);
2465 if not Data.Other_Sources_Present then
2466 if Ada_Is_A_Language then
2471 Main : constant String := Mains.Next_Main;
2473 exit when Main'Length = 0;
2474 Ada_Mains.Increment_Last;
2475 Ada_Mains.Table (Ada_Mains.Last) := new String'(Main);
2480 Osint.Fail ("project ", Project_Name, " contains no source");
2488 Main : constant String := Mains.Next_Main;
2490 Name_Len := Main'Length;
2491 exit when Name_Len = 0;
2492 Name_Buffer (1 .. Name_Len) := Main;
2493 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
2494 Source_Name := Name_Find;
2496 if not Sources_Compiled.Get (Source_Name) then
2497 Sources_Compiled.Set (Source_Name, True);
2499 Source_Id := Data.First_Other_Source;
2500 while Source_Id /= No_Other_Source loop
2501 Source := Project_Tree.Other_Sources.Table (Source_Id);
2502 exit when Source.File_Name = Source_Name;
2503 Source_Id := Source.Next;
2506 if Source_Id = No_Other_Source then
2507 if Ada_Is_A_Language then
2508 Ada_Mains.Increment_Last;
2509 Ada_Mains.Table (Ada_Mains.Last) := new String'(Main);
2514 " is not a valid source of project ",
2519 Compile (Source_Id, Data, Dummy);
2526 if Ada_Mains.Last > 0 then
2528 -- Invoke gnatmake for all Ada sources
2531 Add_Argument (Dash_u, True);
2533 for Index in 1 .. Ada_Mains.Last loop
2534 Add_Argument (Ada_Mains.Table (Index), True);
2537 Compile_Link_With_Gnatmake (Mains_Specified => False);
2539 end Compile_Individual_Sources;
2541 --------------------------------
2542 -- Compile_Link_With_Gnatmake --
2543 --------------------------------
2545 procedure Compile_Link_With_Gnatmake (Mains_Specified : Boolean) is
2546 Data : constant Project_Data :=
2547 Project_Tree.Projects.Table (Main_Project);
2551 -- Array Arguments may already contain some arguments, so we don't
2552 -- set Last_Argument to 0.
2554 -- Get the gnatmake to invoke
2556 Get_Compiler (Ada_Language_Index);
2558 -- Specify the project file
2560 Add_Argument (Dash_P, True);
2561 Add_Argument (Get_Name_String (Data.Display_Path_Name), True);
2563 -- Add the saved switches, if any
2565 for Index in 1 .. Saved_Switches.Last loop
2566 Add_Argument (Saved_Switches.Table (Index), True);
2569 -- If Mains_Specified is True, find the mains in package Mains
2571 if Mains_Specified then
2576 Main : constant String := Mains.Next_Main;
2578 exit when Main'Length = 0;
2579 Add_Argument (Main, True);
2584 -- Specify output file name, if any was specified on the command line
2586 if Output_File_Name /= null then
2587 Add_Argument (Dash_o, True);
2588 Add_Argument (Output_File_Name, True);
2591 -- Transmit some switches to gnatmake
2595 if Compile_Only then
2596 Add_Argument (Dash_c, True);
2601 if Display_Compilation_Progress then
2602 Add_Argument (Dash_d, True);
2608 Add_Argument (Dash_k, True);
2613 if Force_Compilations then
2614 Add_Argument (Dash_f, True);
2619 if Verbose_Mode then
2620 Add_Argument (Dash_v, True);
2625 if Quiet_Output then
2626 Add_Argument (Dash_q, True);
2631 case Current_Verbosity is
2636 Add_Argument (Dash_vP1, True);
2639 Add_Argument (Dash_vP2, True);
2642 -- If there are compiling options for Ada, transmit them to gnatmake
2644 if Comp_Opts.Last (Options (Ada_Language_Index)) /= 0 then
2645 Add_Argument (Dash_cargs, True);
2647 for Arg in 1 .. Comp_Opts.Last (Options (Ada_Language_Index)) loop
2648 Add_Argument (Options (Ada_Language_Index).Table (Arg), True);
2652 if not Compile_Only then
2656 if Linker_Options.Last /= 0 then
2657 Add_Argument (Dash_largs, True);
2659 Add_Argument (Dash_largs, Verbose_Mode);
2664 Add_Archives (For_Gnatmake => True);
2666 -- If there are linking options from the command line,
2667 -- transmit them to gnatmake.
2669 for Arg in 1 .. Linker_Options.Last loop
2670 Add_Argument (Linker_Options.Table (Arg), True);
2674 -- And invoke gnatmake
2677 (Compiler_Names (Ada_Language_Index).all,
2678 Compiler_Paths (Ada_Language_Index));
2681 (Compiler_Paths (Ada_Language_Index).all,
2682 Arguments (1 .. Last_Argument),
2685 -- Report an error if call to gnatmake failed
2690 Compiler_Names (Ada_Language_Index).all,
2693 end Compile_Link_With_Gnatmake;
2695 ---------------------
2696 -- Compile_Sources --
2697 ---------------------
2699 procedure Compile_Sources is
2700 Data : Project_Data;
2701 Source_Id : Other_Source_Id;
2702 Source : Other_Source;
2704 Local_Errors : Boolean := False;
2705 -- Set to True when there is a compilation error. Used only when
2706 -- Keep_Going is True, to inhibit the building of the archive.
2708 Need_To_Compile : Boolean;
2709 -- Set to True when a source needs to be compiled/recompiled
2711 Need_To_Rebuild_Archive : Boolean := Force_Compilations;
2712 -- True when the archive needs to be built/rebuilt unconditionally
2714 Total_Number_Of_Sources : Int := 0;
2716 Current_Source_Number : Int := 0;
2719 -- First, get the number of sources
2721 for Project in Project_Table.First ..
2722 Project_Table.Last (Project_Tree.Projects)
2724 Data := Project_Tree.Projects.Table (Project);
2726 if not Data.Virtual and then Data.Other_Sources_Present then
2727 Source_Id := Data.First_Other_Source;
2728 while Source_Id /= No_Other_Source loop
2729 Source := Project_Tree.Other_Sources.Table (Source_Id);
2730 Total_Number_Of_Sources := Total_Number_Of_Sources + 1;
2731 Source_Id := Source.Next;
2736 -- Loop through project files
2738 for Project in Project_Table.First ..
2739 Project_Table.Last (Project_Tree.Projects)
2741 Local_Errors := False;
2742 Data := Project_Tree.Projects.Table (Project);
2744 -- Nothing to do when no sources of language other than Ada
2746 if (not Data.Virtual) and then Data.Other_Sources_Present then
2748 -- If the imported directory switches are unknown, compute them
2750 if not Data.Include_Data_Set then
2751 Get_Imported_Directories (Project, Data);
2752 Data.Include_Data_Set := True;
2753 Project_Tree.Projects.Table (Project) := Data;
2756 Need_To_Rebuild_Archive := Force_Compilations;
2758 -- Compilation will occur in the object directory
2760 if Project_Of_Current_Object_Directory /= Project then
2761 Project_Of_Current_Object_Directory := Project;
2762 Change_Dir (Get_Name_String (Data.Object_Directory));
2764 if Verbose_Mode then
2765 Write_Str ("Changing to object directory of """);
2766 Write_Name (Data.Display_Name);
2767 Write_Str (""": """);
2768 Write_Name (Data.Display_Object_Dir);
2773 -- Process each source one by one
2775 Source_Id := Data.First_Other_Source;
2776 while Source_Id /= No_Other_Source loop
2777 Source := Project_Tree.Other_Sources.Table (Source_Id);
2778 Current_Source_Number := Current_Source_Number + 1;
2779 Need_To_Compile := Force_Compilations;
2781 -- Check if compilation is needed
2783 if not Need_To_Compile then
2784 Check_Compilation_Needed (Source, Need_To_Compile);
2787 -- Proceed, if compilation is needed
2789 if Need_To_Compile then
2791 -- If a source is compiled/recompiled, of course the
2792 -- archive will need to be built/rebuilt.
2794 Need_To_Rebuild_Archive := True;
2795 Compile (Source_Id, Data, Local_Errors);
2798 if Display_Compilation_Progress then
2799 Write_Str ("completed ");
2800 Write_Int (Current_Source_Number);
2801 Write_Str (" out of ");
2802 Write_Int (Total_Number_Of_Sources);
2805 ((Current_Source_Number * 100) / Total_Number_Of_Sources);
2806 Write_Str ("%)...");
2810 -- Next source, if any
2812 Source_Id := Source.Next;
2815 if Need_To_Rebuild_Archive and then (not Data.Library) then
2816 Need_To_Rebuild_Global_Archive := True;
2819 -- If there was no compilation error and -c was not used,
2820 -- build / rebuild the archive if necessary.
2823 and then Data.Library
2824 and then not Data.Langs (Ada_Language_Index)
2825 and then not Compile_Only
2827 Build_Library (Project, Need_To_Rebuild_Archive);
2831 end Compile_Sources;
2837 procedure Copyright is
2839 -- Only output the Copyright notice once
2841 if not Copyright_Output then
2842 Copyright_Output := True;
2844 Write_Str ("GPRMAKE ");
2845 Write_Str (Gnatvsn.Gnat_Version_String);
2846 Write_Str (" Copyright 2004-");
2847 Write_Str (Gnatvsn.Current_Year);
2848 Write_Str (" Free Software Foundation, Inc.");
2853 ------------------------------------
2854 -- Create_Archive_Dependency_File --
2855 ------------------------------------
2857 procedure Create_Archive_Dependency_File
2859 First_Source : Other_Source_Id)
2861 Source_Id : Other_Source_Id;
2862 Source : Other_Source;
2863 Dep_File : Ada.Text_IO.File_Type;
2866 -- Create the file in Append mode, to avoid automatic insertion of
2867 -- an end of line if file is empty.
2869 Create (Dep_File, Append_File, Name);
2871 Source_Id := First_Source;
2872 while Source_Id /= No_Other_Source loop
2873 Source := Project_Tree.Other_Sources.Table (Source_Id);
2874 Put_Line (Dep_File, Get_Name_String (Source.Object_Name));
2875 Put_Line (Dep_File, String (Source.Object_TS));
2876 Source_Id := Source.Next;
2883 if Is_Open (Dep_File) then
2886 end Create_Archive_Dependency_File;
2888 -------------------------------------------
2889 -- Create_Global_Archive_Dependency_File --
2890 -------------------------------------------
2892 procedure Create_Global_Archive_Dependency_File (Name : String) is
2893 Source_Id : Other_Source_Id;
2894 Source : Other_Source;
2895 Dep_File : Ada.Text_IO.File_Type;
2898 -- Create the file in Append mode, to avoid automatic insertion of
2899 -- an end of line if file is empty.
2901 Create (Dep_File, Append_File, Name);
2903 -- Get all the object files of non-Ada sources in non-library projects
2905 for Project in Project_Table.First ..
2906 Project_Table.Last (Project_Tree.Projects)
2908 if not Project_Tree.Projects.Table (Project).Library then
2910 Project_Tree.Projects.Table (Project).First_Other_Source;
2911 while Source_Id /= No_Other_Source loop
2912 Source := Project_Tree.Other_Sources.Table (Source_Id);
2914 -- Put only those object files that are in the global archive
2916 if Is_Included_In_Global_Archive
2917 (Source.Object_Name, Project)
2919 Put_Line (Dep_File, Get_Name_String (Source.Object_Path));
2920 Put_Line (Dep_File, String (Source.Object_TS));
2923 Source_Id := Source.Next;
2932 if Is_Open (Dep_File) then
2935 end Create_Global_Archive_Dependency_File;
2937 ---------------------
2938 -- Display_Command --
2939 ---------------------
2941 procedure Display_Command
2943 Path : String_Access;
2944 CPATH : String_Access := null;
2945 Ellipse : Boolean := False)
2947 Display_Ellipse : Boolean := Ellipse;
2950 -- Only display the command in Verbose Mode (-v) or when
2951 -- not in Quiet Output (no -q).
2953 if Verbose_Mode or (not Quiet_Output) then
2955 -- In Verbose Mode output the full path of the spawned process
2957 if Verbose_Mode then
2958 if CPATH /= null then
2959 Write_Str ("CPATH = ");
2960 Write_Line (CPATH.all);
2963 Write_Str (Path.all);
2969 -- Display only the arguments for which the display flag is set
2970 -- (in Verbose Mode, the display flag is set for all arguments)
2972 for Arg in 1 .. Last_Argument loop
2973 if Arguments_Displayed (Arg) then
2975 Write_Str (Arguments (Arg).all);
2977 elsif Display_Ellipse then
2979 Display_Ellipse := False;
2985 end Display_Command;
2991 procedure Get_Compiler (For_Language : First_Language_Indexes) is
2992 Data : constant Project_Data :=
2993 Project_Tree.Projects.Table (Main_Project);
2995 Ide : constant Package_Id :=
2998 In_Packages => Data.Decl.Packages,
2999 In_Tree => Project_Tree);
3000 -- The id of the package IDE in the project file
3002 Compiler : constant Variable_Value :=
3004 (Name => Language_Names.Table (For_Language),
3006 Attribute_Or_Array_Name => Name_Compiler_Command,
3008 In_Tree => Project_Tree);
3009 -- The value of Compiler_Command ("language") in package IDE, if defined
3012 -- No need to do it again if the compiler is known for this language
3014 if Compiler_Names (For_Language) = null then
3016 -- If compiler command is not defined for this language in package
3017 -- IDE, use the default compiler for this language.
3019 if Compiler = Nil_Variable_Value then
3020 if For_Language in Default_Compiler_Names'Range then
3021 Compiler_Names (For_Language) :=
3022 Default_Compiler_Names (For_Language);
3026 ("unknow compiler name for language """,
3027 Get_Name_String (Language_Names.Table (For_Language)),
3032 Compiler_Names (For_Language) :=
3033 new String'(Get_Name_String (Compiler.Value));
3036 -- Check we have a GCC compiler (name ends with "gcc" or "g++")
3039 Comp_Name : constant String := Compiler_Names (For_Language).all;
3040 Last3 : String (1 .. 3);
3042 if Comp_Name'Length >= 3 then
3043 Last3 := Comp_Name (Comp_Name'Last - 2 .. Comp_Name'Last);
3044 Compiler_Is_Gcc (For_Language) :=
3045 (Last3 = "gcc") or (Last3 = "g++");
3047 Compiler_Is_Gcc (For_Language) := False;
3051 -- Locate the compiler on the path
3053 Compiler_Paths (For_Language) :=
3054 Locate_Exec_On_Path (Compiler_Names (For_Language).all);
3056 -- Fail if compiler cannot be found
3058 if Compiler_Paths (For_Language) = null then
3059 if For_Language = Ada_Language_Index then
3061 ("unable to locate """,
3062 Compiler_Names (For_Language).all,
3067 ("unable to locate " &
3068 Get_Name_String (Language_Names.Table (For_Language)),
3069 " compiler """, Compiler_Names (For_Language).all & '"');
3075 ------------------------------
3076 -- Get_Imported_Directories --
3077 ------------------------------
3079 procedure Get_Imported_Directories
3080 (Project : Project_Id;
3081 Data : in out Project_Data)
3083 Imported_Projects : Project_List := Data.Imported_Projects;
3085 Path_Length : Natural := 0;
3086 Position : Natural := 0;
3088 procedure Add (Source_Dirs : String_List_Id);
3089 -- Add a list of source directories
3091 procedure Recursive_Get_Dirs (Prj : Project_Id);
3092 -- Recursive procedure to get the source directories of this project
3093 -- file and of the project files it imports, in the correct order.
3099 procedure Add (Source_Dirs : String_List_Id) is
3100 Element_Id : String_List_Id;
3101 Element : String_Element;
3102 Add_Arg : Boolean := True;
3105 -- Add each source directory path name, preceded by "-I" to Arguments
3107 Element_Id := Source_Dirs;
3108 while Element_Id /= Nil_String loop
3109 Element := Project_Tree.String_Elements.Table (Element_Id);
3111 if Element.Value /= No_Name then
3112 Get_Name_String (Element.Display_Value);
3114 if Name_Len > 0 then
3116 -- Remove a trailing directory separator: this may cause
3117 -- problems on Windows.
3120 and then Name_Buffer (Name_Len) = Directory_Separator
3122 Name_Len := Name_Len - 1;
3126 Arg : constant String :=
3127 "-I" & Name_Buffer (1 .. Name_Len);
3129 -- Check if directory is already in the list. If it is,
3130 -- no need to put it there again.
3134 for Index in 1 .. Last_Argument loop
3135 if Arguments (Index).all = Arg then
3142 if Path_Length /= 0 then
3143 Path_Length := Path_Length + 1;
3146 Path_Length := Path_Length + Name_Len;
3148 Add_Argument (Arg, True);
3154 Element_Id := Element.Next;
3158 ------------------------
3159 -- Recursive_Get_Dirs --
3160 ------------------------
3162 procedure Recursive_Get_Dirs (Prj : Project_Id) is
3163 Data : Project_Data;
3164 Imported : Project_List;
3167 -- Nothing to do if project is undefined
3169 if Prj /= No_Project then
3170 Data := Project_Tree.Projects.Table (Prj);
3172 -- Nothing to do if project has already been processed
3174 if not Data.Seen then
3176 -- Mark the project as processed, to avoid multiple processing
3177 -- of the same project.
3179 Project_Tree.Projects.Table (Prj).Seen := True;
3181 -- Add the source directories of this project
3183 if not Data.Virtual then
3184 Add (Data.Source_Dirs);
3187 Recursive_Get_Dirs (Data.Extends);
3189 -- Call itself for all imported projects, if any
3191 Imported := Data.Imported_Projects;
3192 while Imported /= Empty_Project_List loop
3194 (Project_Tree.Project_Lists.Table (Imported).Project);
3196 Project_Tree.Project_Lists.Table (Imported).Next;
3200 end Recursive_Get_Dirs;
3202 -- Start of processing for Get_Imported_Directories
3205 -- First, mark all project as not processed
3207 for J in Project_Table.First ..
3208 Project_Table.Last (Project_Tree.Projects)
3210 Project_Tree.Projects.Table (J).Seen := False;
3217 -- Process this project individually, project data are already known
3219 Project_Tree.Projects.Table (Project).Seen := True;
3221 Add (Data.Source_Dirs);
3223 Recursive_Get_Dirs (Data.Extends);
3225 while Imported_Projects /= Empty_Project_List loop
3227 (Project_Tree.Project_Lists.Table
3228 (Imported_Projects).Project);
3229 Imported_Projects := Project_Tree.Project_Lists.Table
3230 (Imported_Projects).Next;
3233 Data.Imported_Directories_Switches :=
3234 new Argument_List'(Arguments (1 .. Last_Argument));
3236 -- Create the Include_Path, from the Arguments
3238 Data.Include_Path := new String (1 .. Path_Length);
3239 Data.Include_Path (1 .. Arguments (1)'Length - 2) :=
3240 Arguments (1)(Arguments (1)'First + 2 .. Arguments (1)'Last);
3241 Position := Arguments (1)'Length - 2;
3243 for Arg in 2 .. Last_Argument loop
3244 Position := Position + 1;
3245 Data.Include_Path (Position) := Path_Separator;
3247 (Position + 1 .. Position + Arguments (Arg)'Length - 2) :=
3248 Arguments (Arg)(Arguments (Arg)'First + 2 .. Arguments (Arg)'Last);
3249 Position := Position + Arguments (Arg)'Length - 2;
3253 end Get_Imported_Directories;
3259 procedure Gprmake is
3263 if Verbose_Mode then
3265 Write_Str ("Parsing project file """);
3266 Write_Str (Project_File_Name.all);
3271 -- Parse and process project files for other languages (not for Ada)
3274 (Project => Main_Project,
3275 In_Tree => Project_Tree,
3276 Project_File_Name => Project_File_Name.all,
3277 Packages_To_Check => Packages_To_Check);
3279 -- Fail if parsing/processing was unsuccessful
3281 if Main_Project = No_Project then
3282 Osint.Fail ("""", Project_File_Name.all, """ processing failed");
3285 if Verbose_Mode then
3287 Write_Str ("Parsing of project file """);
3288 Write_Str (Project_File_Name.all);
3289 Write_Str (""" is finished.");
3293 -- If -f was specified, we will certainly need to link (except when
3294 -- -u or -c were specified, of course).
3296 Need_To_Relink := Force_Compilations;
3298 if Unique_Compile then
3299 if Mains.Number_Of_Mains = 0 then
3301 ("No source specified to compile in 'unique compile' mode");
3303 Compile_Individual_Sources;
3304 Report_Total_Errors ("compilation");
3309 Data : constant Prj.Project_Data :=
3310 Project_Tree.Projects.Table (Main_Project);
3312 if Data.Library and then Mains.Number_Of_Mains /= 0 then
3314 ("Cannot specify mains on the command line " &
3315 "for a Library Project");
3318 -- First check for C++, to link libraries with g++,
3321 Check_For_C_Plus_Plus;
3323 -- Compile sources and build archives for library project,
3328 -- When Keep_Going is True, if we had some errors, fail now,
3329 -- reporting the number of compilation errors.
3330 -- Do not attempt to link.
3332 Report_Total_Errors ("compilation");
3334 -- If -c was not specified, link the executables,
3335 -- if there are any.
3338 and then not Data.Library
3339 and then Data.Object_Directory /= No_Path
3341 Build_Global_Archive;
3345 -- When Keep_Going is True, if we had some errors, fail, reporting
3346 -- the number of linking errors.
3348 Report_Total_Errors ("linking");
3357 procedure Initialize is
3359 Set_Mode (Ada_Only);
3361 -- Do some necessary package initializations
3366 Prj.Initialize (Project_Tree);
3369 -- Add the directory where gprmake is invoked in front of the path,
3370 -- if gprmake is invoked from a bin directory or with directory
3371 -- information. information. Only do this if the platform is not VMS,
3372 -- where the notion of path does not really exist.
3374 -- Below code shares nasty code duplication with make.adb code???
3378 Prefix : constant String := Executable_Prefix_Path;
3379 Command : constant String := Command_Name;
3382 if Prefix'Length > 0 then
3384 PATH : constant String :=
3385 Prefix & Directory_Separator & "bin" &
3387 Getenv ("PATH").all;
3389 Setenv ("PATH", PATH);
3393 for Index in reverse Command'Range loop
3394 if Command (Index) = Directory_Separator then
3396 Absolute_Dir : constant String :=
3398 (Command (Command'First .. Index));
3399 PATH : constant String :=
3402 Getenv ("PATH").all;
3404 Setenv ("PATH", PATH);
3414 -- Set Name_Ide and Name_Compiler_Command
3417 Add_Str_To_Name_Buffer ("ide");
3418 Name_Ide := Name_Find;
3421 Add_Str_To_Name_Buffer ("compiler_command");
3422 Name_Compiler_Command := Name_Find;
3424 -- Make sure the Saved_Switches table is empty
3426 Saved_Switches.Set_Last (0);
3428 -- Get the command line arguments
3430 Scan_Args : for Next_Arg in 1 .. Argument_Count loop
3431 Scan_Arg (Argument (Next_Arg));
3434 -- Fail if command line ended with "-P"
3436 if Project_File_Name_Expected then
3437 Osint.Fail ("project file name missing after -P");
3439 -- Or if it ended with "-o"
3441 elsif Output_File_Name_Expected then
3442 Osint.Fail ("output file name missing after -o");
3445 -- If no project file was specified, display the usage and fail
3447 if Project_File_Name = null then
3449 Exit_Program (E_Success);
3452 -- To be able of finding libgnat.a in MLib.Tgt, we need to have the
3453 -- default search dirs established in Osint.
3455 Osint.Add_Default_Search_Dirs;
3458 -----------------------------------
3459 -- Is_Included_In_Global_Archive --
3460 -----------------------------------
3462 function Is_Included_In_Global_Archive
3463 (Object_Name : File_Name_Type;
3464 Project : Project_Id) return Boolean
3466 Data : Project_Data := Project_Tree.Projects.Table (Project);
3467 Source : Other_Source_Id;
3470 while Data.Extended_By /= No_Project loop
3471 Data := Project_Tree.Projects.Table (Data.Extended_By);
3473 Source := Data.First_Other_Source;
3474 while Source /= No_Other_Source loop
3475 if Project_Tree.Other_Sources.Table (Source).Object_Name =
3481 Project_Tree.Other_Sources.Table (Source).Next;
3487 end Is_Included_In_Global_Archive;
3489 ----------------------
3490 -- Link_Executables --
3491 ----------------------
3493 procedure Link_Executables is
3494 Data : constant Project_Data :=
3495 Project_Tree.Projects.Table (Main_Project);
3497 Mains_Specified : constant Boolean := Mains.Number_Of_Mains /= 0;
3498 -- True if main sources were specified on the command line
3500 Object_Dir : constant String :=
3501 Get_Name_String (Data.Display_Object_Dir);
3502 -- Path of the object directory of the main project
3504 Source_Id : Other_Source_Id;
3505 Source : Other_Source;
3508 Linker_Name : String_Access;
3509 Linker_Path : String_Access;
3510 -- The linker name and path, when linking is not done by gnatlink
3512 Link_Done : Boolean := False;
3513 -- Set to True when the linker is invoked directly (not through
3514 -- gnatmake) to be able to report if mains were up to date at the end
3517 procedure Add_C_Plus_Plus_Link_For_Gnatmake;
3518 -- Add the --LINK= switch for gnatlink, depending on the C++ compiler
3520 procedure Check_Time_Stamps (Exec_Time_Stamp : Time_Stamp_Type);
3521 -- Check if there is an archive that is more recent than the executable
3522 -- to decide if we need to relink.
3524 procedure Choose_C_Plus_Plus_Link_Process;
3525 -- If the C++ compiler is not g++, create the correct script to link
3527 procedure Link_Foreign
3529 Main_Id : File_Name_Type;
3530 Source : Other_Source);
3531 -- Link a non-Ada main, when there is no Ada code
3533 ---------------------------------------
3534 -- Add_C_Plus_Plus_Link_For_Gnatmake --
3535 ---------------------------------------
3537 procedure Add_C_Plus_Plus_Link_For_Gnatmake is
3540 ("--LINK=" & Compiler_Names (C_Plus_Plus_Language_Index).all,
3542 end Add_C_Plus_Plus_Link_For_Gnatmake;
3544 -----------------------
3545 -- Check_Time_Stamps --
3546 -----------------------
3548 procedure Check_Time_Stamps (Exec_Time_Stamp : Time_Stamp_Type) is
3549 Prj_Data : Project_Data;
3552 for Prj in Project_Table.First ..
3553 Project_Table.Last (Project_Tree.Projects)
3555 Prj_Data := Project_Tree.Projects.Table (Prj);
3557 -- There is an archive only in project
3558 -- files with sources other than Ada
3561 if Data.Other_Sources_Present then
3563 Archive_Path : constant String := Get_Name_String
3564 (Prj_Data.Display_Object_Dir) & Directory_Separator
3565 & "lib" & Get_Name_String (Prj_Data.Display_Name)
3566 & '.' & Archive_Ext;
3567 Archive_TS : Time_Stamp_Type;
3570 Add_Str_To_Name_Buffer (Archive_Path);
3571 Archive_TS := File_Stamp (File_Name_Type'(Name_Find));
3573 -- If the archive is later than the
3574 -- executable, we need to relink.
3576 if Archive_TS /= Empty_Time_Stamp
3578 Exec_Time_Stamp < Archive_TS
3580 Need_To_Relink := True;
3582 if Verbose_Mode then
3584 Write_Str (Archive_Path);
3585 Write_Str (" has time stamp ");
3586 Write_Str ("later than ");
3587 Write_Line ("executable");
3595 end Check_Time_Stamps;
3597 -------------------------------------
3598 -- Choose_C_Plus_Plus_Link_Process --
3599 -------------------------------------
3601 procedure Choose_C_Plus_Plus_Link_Process is
3603 if Compiler_Names (C_Plus_Plus_Language_Index) = null then
3604 Get_Compiler (C_Plus_Plus_Language_Index);
3606 end Choose_C_Plus_Plus_Link_Process;
3612 procedure Link_Foreign
3614 Main_Id : File_Name_Type;
3615 Source : Other_Source)
3617 Executable_Name : constant String :=
3620 (Project => Main_Project,
3621 In_Tree => Project_Tree,
3624 Ada_Main => False));
3625 -- File name of the executable
3627 Executable_Path : constant String :=
3629 (Data.Display_Exec_Dir) &
3630 Directory_Separator & Executable_Name;
3631 -- Path name of the executable
3633 Exec_Time_Stamp : Time_Stamp_Type;
3636 -- Now, check if the executable is up to date. It is considered
3637 -- up to date if its time stamp is not earlier that the time stamp
3638 -- of any archive. Only do that if we don't know if we need to link.
3640 if not Need_To_Relink then
3642 -- Get the time stamp of the executable
3645 Add_Str_To_Name_Buffer (Executable_Path);
3646 Exec_Time_Stamp := File_Stamp (File_Name_Type'(Name_Find));
3648 if Verbose_Mode then
3649 Write_Str (" Checking executable ");
3650 Write_Line (Executable_Name);
3653 -- If executable does not exist, we need to link
3655 if Exec_Time_Stamp = Empty_Time_Stamp then
3656 Need_To_Relink := True;
3658 if Verbose_Mode then
3659 Write_Line (" -> not found");
3662 -- Otherwise, get the time stamps of each archive. If one of
3663 -- them is found later than the executable, we need to relink.
3666 Check_Time_Stamps (Exec_Time_Stamp);
3669 -- If Need_To_Relink is False, we are done
3671 if Verbose_Mode and (not Need_To_Relink) then
3672 Write_Line (" -> up to date");
3678 if Need_To_Relink then
3683 -- Specify the executable path name
3685 Add_Argument (Dash_o, True);
3687 (Get_Name_String (Data.Display_Exec_Dir) &
3688 Directory_Separator &
3691 (Project => Main_Project,
3692 In_Tree => Project_Tree,
3695 Ada_Main => False)),
3698 -- Specify the object file of the main source
3701 (Object_Dir & Directory_Separator &
3702 Get_Name_String (Source.Object_Name),
3705 -- Add all the archives, in a correct order
3707 Add_Archives (For_Gnatmake => False);
3709 -- Add the switches specified in package Linker of
3710 -- the main project.
3715 Language => Source.Language,
3716 File_Name => Main_Id);
3718 -- Add the switches specified in attribute
3719 -- Linker_Options of packages Linker.
3721 if Link_Options_Switches = null then
3722 Link_Options_Switches :=
3724 (Linker_Options_Switches (Main_Project, Project_Tree));
3727 Add_Arguments (Link_Options_Switches.all, True);
3729 -- Add the linking options specified on the
3732 for Arg in 1 .. Linker_Options.Last loop
3733 Add_Argument (Linker_Options.Table (Arg), True);
3736 -- If there are shared libraries and the run path
3737 -- option is supported, add the run path switch.
3739 if Lib_Path.Last > 0 then
3742 String (Lib_Path.Table (1 .. Lib_Path.Last)),
3746 -- And invoke the linker
3748 Display_Command (Linker_Name.all, Linker_Path);
3751 Arguments (1 .. Last_Argument),
3755 Report_Error ("could not link ", Main);
3760 -- Start of processing of Link_Executables
3763 -- If no mains specified, get mains from attribute Main, if it exists
3765 if not Mains_Specified then
3767 Element_Id : String_List_Id;
3768 Element : String_Element;
3771 Element_Id := Data.Mains;
3772 while Element_Id /= Nil_String loop
3773 Element := Project_Tree.String_Elements.Table (Element_Id);
3775 if Element.Value /= No_Name then
3776 Mains.Add_Main (Get_Name_String (Element.Value));
3779 Element_Id := Element.Next;
3784 if Mains.Number_Of_Mains = 0 then
3786 -- If the attribute Main is an empty list or not specified,
3787 -- there is nothing to do.
3789 if Verbose_Mode then
3790 Write_Line ("No main to link");
3795 -- Check if -o was used for several mains
3797 if Output_File_Name /= null and then Mains.Number_Of_Mains > 1 then
3798 Osint.Fail ("cannot specify an executable name for several mains");
3801 -- Check how we are going to do the link
3803 if not Data.Other_Sources_Present then
3805 -- Only Ada sources in the main project, and even maybe not
3807 if not Data.Langs (Ada_Language_Index) then
3809 -- Fail if the main project has no source of any language
3813 Get_Name_String (Data.Name),
3814 """ has no sources, so no main can be linked");
3817 -- Only Ada sources in the main project, call gnatmake directly
3821 -- Choose correct linker if there is C++ code in other projects
3823 if C_Plus_Plus_Is_Used then
3824 Choose_C_Plus_Plus_Link_Process;
3825 Add_Argument (Dash_largs, Verbose_Mode);
3826 Add_C_Plus_Plus_Link_For_Gnatmake;
3827 Add_Argument (Dash_margs, Verbose_Mode);
3830 Compile_Link_With_Gnatmake (Mains_Specified);
3834 -- There are other language sources. First check if there are also
3837 if Data.Langs (Ada_Language_Index) then
3839 -- There is a mix of Ada and other language sources in the main
3840 -- project. Any main that is not a source of the other languages
3841 -- will be deemed to be an Ada main.
3843 -- Find the mains of the other languages and the Ada mains
3846 Ada_Mains.Set_Last (0);
3847 Other_Mains.Set_Last (0);
3853 Main : constant String := Mains.Next_Main;
3854 Main_Id : File_Name_Type;
3857 exit when Main'Length = 0;
3859 -- Get the main file name
3862 Add_Str_To_Name_Buffer (Main);
3863 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
3864 Main_Id := Name_Find;
3866 -- Check if it is a source of a language other than Ada
3868 Source_Id := Data.First_Other_Source;
3869 while Source_Id /= No_Other_Source loop
3871 Project_Tree.Other_Sources.Table (Source_Id);
3872 exit when Source.File_Name = Main_Id;
3873 Source_Id := Source.Next;
3876 -- If it is not, put it in the list of Ada mains
3878 if Source_Id = No_Other_Source then
3879 Ada_Mains.Increment_Last;
3880 Ada_Mains.Table (Ada_Mains.Last) := new String'(Main);
3882 -- Otherwise, put it in the list of other mains
3885 Other_Mains.Increment_Last;
3886 Other_Mains.Table (Other_Mains.Last) := Source;
3891 -- If C++ is one of the other language, create the shell script
3894 if C_Plus_Plus_Is_Used then
3895 Choose_C_Plus_Plus_Link_Process;
3898 -- Call gnatmake with the necessary switches for each non-Ada
3899 -- main, if there are some.
3901 for Main in 1 .. Other_Mains.Last loop
3903 Source : constant Other_Source := Other_Mains.Table (Main);
3908 -- Add -o if -o was specified
3910 if Output_File_Name = null then
3911 Add_Argument (Dash_o, True);
3915 (Project => Main_Project,
3916 In_Tree => Project_Tree,
3917 Main => Other_Mains.Table (Main).File_Name,
3919 Ada_Main => False)),
3923 -- Call gnatmake with the -B switch
3925 Add_Argument (Dash_B, True);
3927 -- Add to the linking options the object file of the source
3929 Add_Argument (Dash_largs, Verbose_Mode);
3931 (Get_Name_String (Source.Object_Name), Verbose_Mode);
3933 -- If C++ is one of the language, add the --LINK switch
3934 -- to the linking switches.
3936 if C_Plus_Plus_Is_Used then
3937 Add_C_Plus_Plus_Link_For_Gnatmake;
3940 -- Add -margs so that the following switches are for
3943 Add_Argument (Dash_margs, Verbose_Mode);
3945 -- And link with gnatmake
3947 Compile_Link_With_Gnatmake (Mains_Specified => False);
3951 -- If there are also Ada mains, call gnatmake for all these mains
3953 if Ada_Mains.Last /= 0 then
3956 -- Put all the Ada mains as the first arguments
3958 for Main in 1 .. Ada_Mains.Last loop
3959 Add_Argument (Ada_Mains.Table (Main).all, True);
3962 -- If C++ is one of the languages, add the --LINK switch to
3963 -- the linking switches.
3965 if Data.Langs (C_Plus_Plus_Language_Index) then
3966 Add_Argument (Dash_largs, Verbose_Mode);
3967 Add_C_Plus_Plus_Link_For_Gnatmake;
3968 Add_Argument (Dash_margs, Verbose_Mode);
3971 -- And link with gnatmake
3973 Compile_Link_With_Gnatmake (Mains_Specified => False);
3977 -- No Ada source in main project
3979 -- First, get the linker to invoke
3981 if Data.Langs (C_Plus_Plus_Language_Index) then
3982 Get_Compiler (C_Plus_Plus_Language_Index);
3983 Linker_Name := Compiler_Names (C_Plus_Plus_Language_Index);
3984 Linker_Path := Compiler_Paths (C_Plus_Plus_Language_Index);
3987 Get_Compiler (C_Language_Index);
3988 Linker_Name := Compiler_Names (C_Language_Index);
3989 Linker_Path := Compiler_Paths (C_Language_Index);
3996 -- Get each main, check if it is a source of the main project,
3997 -- and if it is, invoke the linker.
4001 Main : constant String := Mains.Next_Main;
4002 Main_Id : File_Name_Type;
4005 exit when Main'Length = 0;
4007 -- Get the file name of the main
4010 Add_Str_To_Name_Buffer (Main);
4011 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
4012 Main_Id := Name_Find;
4014 -- Check if it is a source of the main project file
4016 Source_Id := Data.First_Other_Source;
4017 while Source_Id /= No_Other_Source loop
4019 Project_Tree.Other_Sources.Table (Source_Id);
4020 exit when Source.File_Name = Main_Id;
4021 Source_Id := Source.Next;
4024 -- Report an error if it is not
4026 if Source_Id = No_Other_Source then
4028 (Main, "is not a source of project ",
4029 Get_Name_String (Data.Name));
4032 Link_Foreign (Main, Main_Id, Source);
4037 -- If no linking was done, report it, except in Quiet Output
4039 if (Verbose_Mode or (not Quiet_Output)) and (not Link_Done) then
4040 Osint.Write_Program_Name;
4042 if Mains.Number_Of_Mains = 1 then
4044 -- If there is only one executable, report its name too
4050 Main : constant String := Mains.Next_Main;
4051 Main_Id : File_Name_Type;
4054 Add_Str_To_Name_Buffer (Main);
4055 Main_Id := Name_Find;
4059 (Project => Main_Project,
4060 In_Tree => Project_Tree,
4063 Ada_Main => False)));
4064 Write_Line (""" up to date");
4068 Write_Line (": all executables up to date");
4073 end Link_Executables;
4079 procedure Report_Error
4085 -- If Keep_Going is True, output error message preceded by error header
4088 Total_Number_Of_Errors := Total_Number_Of_Errors + 1;
4089 Write_Str (Error_Header);
4095 -- Otherwise just fail
4098 Osint.Fail (S1, S2, S3);
4102 -------------------------
4103 -- Report_Total_Errors --
4104 -------------------------
4106 procedure Report_Total_Errors (Kind : String) is
4108 if Total_Number_Of_Errors /= 0 then
4109 if Total_Number_Of_Errors = 1 then
4111 ("One ", Kind, " error");
4115 ("Total of" & Total_Number_Of_Errors'Img,
4116 ' ' & Kind & " errors");
4119 end Report_Total_Errors;
4125 procedure Scan_Arg (Arg : String) is
4127 pragma Assert (Arg'First = 1);
4129 if Arg'Length = 0 then
4133 -- If preceding switch was -P, a project file name need to be
4134 -- specified, not a switch.
4136 if Project_File_Name_Expected then
4137 if Arg (1) = '-' then
4138 Osint.Fail ("project file name missing after -P");
4140 Project_File_Name_Expected := False;
4141 Project_File_Name := new String'(Arg);
4144 -- If preceding switch was -o, an executable name need to be
4145 -- specified, not a switch.
4147 elsif Output_File_Name_Expected then
4148 if Arg (1) = '-' then
4149 Osint.Fail ("output file name missing after -o");
4151 Output_File_Name_Expected := False;
4152 Output_File_Name := new String'(Arg);
4155 -- Set the processor/language for the following switches
4157 -- -cargs: Ada compiler arguments
4159 elsif Arg = "-cargs" then
4160 Current_Language := Ada_Language_Index;
4161 Current_Processor := Compiler;
4163 elsif Arg'Length > 7 and then Arg (1 .. 7) = "-cargs:" then
4165 Add_Str_To_Name_Buffer (Arg (8 .. Arg'Last));
4166 To_Lower (Name_Buffer (1 .. Name_Len));
4169 Lang : constant Name_Id := Name_Find;
4171 Current_Language := Language_Indexes.Get (Lang);
4173 if Current_Language = No_Language_Index then
4174 Add_Language_Name (Lang);
4175 Current_Language := Last_Language_Index;
4178 Current_Processor := Compiler;
4181 elsif Arg = "-largs" then
4182 Current_Processor := Linker;
4186 elsif Arg = "-gargs" then
4187 Current_Processor := None;
4189 -- A special test is needed for the -o switch within a -largs since
4190 -- that is another way to specify the name of the final executable.
4192 elsif Current_Processor = Linker and then Arg = "-o" then
4194 ("switch -o not allowed within a -largs. Use -o directly.");
4196 -- If current processor is not gprmake directly, store the option in
4197 -- the appropriate table.
4199 elsif Current_Processor /= None then
4202 -- Switches start with '-'
4204 elsif Arg (1) = '-' then
4205 if Arg'Length > 3 and then Arg (1 .. 3) = "-aP" then
4206 Add_Search_Project_Directory (Arg (4 .. Arg'Last));
4208 -- Record the switch, so that it is passed to gnatmake, if
4209 -- gnatmake is called.
4211 Saved_Switches.Append (new String'(Arg));
4213 elsif Arg = "-c" then
4214 Compile_Only := True;
4216 -- Make sure that when a main is specified and switch -c is used,
4217 -- only the main(s) is/are compiled.
4219 if Mains.Number_Of_Mains > 0 then
4220 Unique_Compile := True;
4223 elsif Arg = "-d" then
4224 Display_Compilation_Progress := True;
4226 elsif Arg = "-f" then
4227 Force_Compilations := True;
4229 elsif Arg = "-h" then
4232 elsif Arg = "-k" then
4235 elsif Arg = "-o" then
4236 if Output_File_Name /= null then
4237 Osint.Fail ("cannot specify several -o switches");
4240 Output_File_Name_Expected := True;
4243 elsif Arg'Length >= 2 and then Arg (2) = 'P' then
4244 if Project_File_Name /= null then
4245 Osint.Fail ("cannot have several project files specified");
4247 elsif Arg'Length = 2 then
4248 Project_File_Name_Expected := True;
4251 Project_File_Name := new String'(Arg (3 .. Arg'Last));
4254 elsif Arg = "-p" or else Arg = "--create-missing-dirs" then
4255 Setup_Projects := True;
4257 elsif Arg = "-q" then
4258 Quiet_Output := True;
4260 elsif Arg = "-u" then
4261 Unique_Compile := True;
4262 Compile_Only := True;
4264 elsif Arg = "-v" then
4265 Verbose_Mode := True;
4268 elsif Arg'Length = 4 and then Arg (1 .. 3) = "-vP"
4269 and then Arg (4) in '0' .. '2'
4273 Current_Verbosity := Prj.Default;
4275 Current_Verbosity := Prj.Medium;
4277 Current_Verbosity := Prj.High;
4282 elsif Arg'Length >= 3 and then Arg (2) = 'X'
4283 and then Is_External_Assignment (Arg)
4285 -- Is_External_Assignment has side effects when it returns True
4287 -- Record the -X switch, so that it will be passed to gnatmake,
4288 -- if gnatmake is called.
4290 Saved_Switches.Append (new String'(Arg));
4293 Osint.Fail ("illegal option """, Arg, """");
4297 -- Not a switch: must be a main
4299 Mains.Add_Main (Arg);
4301 -- Make sure that when a main is specified and switch -c is used,
4302 -- only the main(s) is/are compiled.
4304 if Compile_Only then
4305 Unique_Compile := True;
4314 function Strip_CR_LF (Text : String) return String is
4315 To : String (1 .. Text'Length);
4316 Index_To : Natural := 0;
4319 for Index in Text'Range loop
4320 if (Text (Index) /= ASCII.CR) and then (Text (Index) /= ASCII.LF) then
4321 Index_To := Index_To + 1;
4322 To (Index_To) := Text (Index);
4326 return To (1 .. Index_To);
4335 if not Usage_Output then
4336 Usage_Output := True;
4339 Write_Str ("Usage: ");
4340 Osint.Write_Program_Name;
4341 Write_Str (" -P<project file> [opts] [name] {");
4342 Write_Str ("[-cargs:lang opts] ");
4343 Write_Str ("[-largs opts] [-gargs opts]}");
4346 Write_Str (" name is zero or more file names");
4352 Write_Str ("gprmake switches:");
4357 Write_Str (" -aPdir Add directory dir to project search path");
4362 Write_Str (" -c Compile only");
4367 Write_Str (" -f Force recompilations");
4372 Write_Str (" -k Keep going after compilation errors");
4377 Write_Str (" -o name Choose an alternate executable name");
4382 Write_Str (" -p Create missing obj, lib and exec dirs");
4387 Write_Str (" -Pproj Use GNAT Project File proj");
4392 Write_Str (" -q Be quiet/terse");
4398 (" -u Unique compilation. Only compile the given files");
4403 Write_Str (" -v Verbose output");
4408 Write_Str (" -vPx Specify verbosity when parsing Project Files");
4413 Write_Str (" -Xnm=val Specify an external reference for " &
4420 Write_Line (" -cargs opts opts are passed to the Ada compiler");
4422 -- Line for -cargs:lang
4424 Write_Line (" -cargs:<lang> opts");
4425 Write_Line (" opts are passed to the compiler " &
4426 "for language < lang > ");
4430 Write_Str (" -largs opts opts are passed to the linker");
4435 Write_Str (" -gargs opts opts directly interpreted by gprmake");
4443 Makeutl.Do_Fail := Report_Error'Access;