1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2001 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 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 -- The following package implements the facilities to recursively
28 -- compile (a la make), bind and/or link a set of sources. This package
29 -- gives the individual routines for performing such tasks as well as
30 -- the routine gnatmake below that puts it all together.
32 with GNAT.OS_Lib; use GNAT.OS_Lib; -- defines Argument_List
34 with Types; use Types;
38 -- The 3 following packages are used to store gcc, gnatbind and gnatbl
39 -- switches passed on the gnatmake or gnatdist command line.
40 -- Note that the lower bounds definitely need to be 1 to match the
41 -- requirement that the argument array prepared for Spawn must have
42 -- a lower bound of 1.
44 package Gcc_Switches is new Table.Table (
45 Table_Component_Type => String_Access,
46 Table_Index_Type => Integer,
49 Table_Increment => 100,
50 Table_Name => "Make.Gcc_Switches");
52 package Binder_Switches is new Table.Table (
53 Table_Component_Type => String_Access,
54 Table_Index_Type => Integer,
57 Table_Increment => 100,
58 Table_Name => "Make.Binder_Switches");
60 package Linker_Switches is new Table.Table (
61 Table_Component_Type => String_Access,
62 Table_Index_Type => Integer,
65 Table_Increment => 100,
66 Table_Name => "Make.Linker_Switches");
68 procedure Display_Commands (Display : Boolean := True);
69 -- The default behavior of Make commands (Compile_Sources, Bind, Link)
70 -- is to display them on stderr. This behavior can be changed repeatedly
71 -- by invoking this procedure.
73 -- If a compilation, bind or link failed one of the following 3 exceptions
74 -- is raised. These need to be handled by the calling routines.
76 Compilation_Failed : exception;
77 -- Raised by Compile_Sources if a compilation failed.
79 Bind_Failed : exception;
80 -- Raised by Bind below if the bind failed.
82 Link_Failed : exception;
83 -- Raised by Link below if the link failed.
85 procedure Bind (ALI_File : File_Name_Type; Args : Argument_List);
86 -- Binds ALI_File. Args are the arguments to pass to the binder.
87 -- Args must have a lower bound of 1.
89 procedure Link (ALI_File : File_Name_Type; Args : Argument_List);
90 -- Links ALI_File. Args are the arguments to pass to the linker.
91 -- Args must have a lower bound of 1.
94 -- Performs default and package initialization. Therefore,
95 -- Compile_Sources can be called by an external unit.
97 procedure Scan_Make_Arg (Argv : String; And_Save : Boolean);
98 -- Scan make arguments. Argv is a single argument to be processed.
100 procedure Extract_Failure
101 (File : out File_Name_Type;
102 Unit : out Unit_Name_Type;
103 Found : out Boolean);
104 -- Extracts the first failure report from Bad_Compilation table.
106 procedure Compile_Sources
107 (Main_Source : File_Name_Type;
108 Args : Argument_List;
109 First_Compiled_File : out Name_Id;
110 Most_Recent_Obj_File : out Name_Id;
111 Most_Recent_Obj_Stamp : out Time_Stamp_Type;
112 Main_Unit : out Boolean;
113 Compilation_Failures : out Natural;
114 Check_Readonly_Files : Boolean := False;
115 Do_Not_Execute : Boolean := False;
116 Force_Compilations : Boolean := False;
117 Keep_Going : Boolean := False;
118 In_Place_Mode : Boolean := False;
119 Initialize_ALI_Data : Boolean := True;
120 Max_Process : Positive := 1);
121 -- Compile_Sources will recursively compile all the sources needed by
122 -- Main_Source. Before calling this routine make sure Namet has been
123 -- initialized. This routine can be called repeatedly with different
124 -- Main_Source file as long as all the source (-I flags), library
125 -- (-B flags) and ada library (-A flags) search paths between calls are
126 -- *exactly* the same. The default directory must also be the same.
128 -- Args contains the arguments to use during the compilations.
129 -- The lower bound of Args must be 1.
131 -- First_Compiled_File is set to the name of the first file that is
132 -- compiled or that needs to be compiled. This is set to No_Name if no
133 -- compilations were needed.
135 -- Most_Recent_Obj_File is set to the full name of the most recent
136 -- object file found when no compilations are needed, that is when
137 -- First_Compiled_File is set to No_Name. When First_Compiled_File
138 -- is set then Most_Recent_Obj_File is set to No_Name.
140 -- Most_Recent_Obj_Stamp is the time stamp of Most_Recent_Obj_File.
142 -- Main_Unit is set to True if Main_Source can be a main unit.
143 -- If Do_Not_Execute is False and First_Compiled_File /= No_Name
144 -- the value of Main_Unit is always False.
145 -- Is this used any more??? It is certainly not used by gnatmake???
147 -- Compilation_Failures is a count of compilation failures. This count
148 -- is used to extract compilation failure reports with Extract_Failure.
150 -- Check_Readonly_Files set it to True to compile source files
151 -- which library files are read-only. When compiling GNAT predefined
152 -- files the "-gnatg" flag is used.
154 -- Do_Not_Execute set it to True to find out the first source that
155 -- needs to be recompiled, but without recompiling it. This file is
156 -- saved in First_Compiled_File.
158 -- Force_Compilations forces all compilations no matter what but
159 -- recompiles read-only files only if Check_Readonly_Files
162 -- Keep_Going when True keep compiling even in the presence of
163 -- compilation errors.
165 -- In_Place_Mode when True save library/object files in their object
166 -- directory if they already exist; otherwise, in the source directory.
168 -- Initialize_ALI_Data set it to True when you want to initialize ALI
169 -- data-structures. This is what you should do most of the time.
170 -- (especially the first time around when you call this routine).
171 -- This parameter is set to False to preserve previously recorded
174 -- Max_Process is the maximum number of processes that should be spawned
175 -- to carry out compilations.
177 -- Flags in Package Opt Affecting Compile_Sources
178 -- -----------------------------------------------
180 -- Check_Object_Consistency set it to False to omit all consistency
181 -- checks between an .ali file and its corresponding object file.
182 -- When this flag is set to true, every time an .ali is read,
183 -- package Osint checks that the corresponding object file
184 -- exists and is more recent than the .ali.
186 -- Use of Name Table Info
187 -- ----------------------
189 -- All file names manipulated by Compile_Sources are entered into the
190 -- Names table. The Byte field of a source file is used to mark it.
192 -- Calling Compile_Sources Several Times
193 -- -------------------------------------
195 -- Upon return from Compile_Sources all the ALI data structures are left
196 -- intact for further browsing. HOWEVER upon entry to this routine ALI
197 -- data structures are re-initialized if parameter Initialize_ALI_Data
198 -- above is set to true. Typically this is what you want the first time
199 -- you call Compile_Sources. You should not load an ali file, call this
200 -- routine with flag Initialize_ALI_Data set to True and then expect
201 -- that ALI information to be around after the call. Note that the first
202 -- time you call Compile_Sources you better set Initialize_ALI_Data to
203 -- True unless you have called Initialize_ALI yourself.
205 -- Compile_Sources ALGORITHM : Compile_Sources (Main_Source)
206 -- -------------------------
208 -- 1. Insert Main_Source in a Queue (Q) and mark it.
210 -- 2. Let unit.adb be the file at the head of the Q. If unit.adb is
211 -- missing but its corresponding ali file is in an Ada library directory
212 -- (see below) then, remove unit.adb from the Q and goto step 4.
213 -- Otherwise, look at the files under the D (dependency) section of
214 -- unit.ali. If unit.ali does not exist or some of the time stamps do
215 -- not match, (re)compile unit.adb.
217 -- An Ada library directory is a directory containing Ada specs, ali
218 -- and object files but no source files for the bodies. An Ada library
219 -- directory is communicated to gnatmake by means of some switch so that
220 -- gnatmake can skip the sources whole ali are in that directory.
221 -- There are two reasons for skipping the sources in this case. Firstly,
222 -- Ada libraries typically come without full sources but binding and
223 -- linking against those libraries is still possible. Secondly, it would
224 -- be very wasteful for gnatmake to systematically check the consistency
225 -- of every external Ada library used in a program. The binder is
226 -- already in charge of catching any potential inconsistencies.
228 -- 3. Look into the W section of unit.ali and insert into the Q all
229 -- unmarked source files. Mark all files newly inserted in the Q.
230 -- Specifically, assuming that the W section looks like
232 -- W types%s types.adb types.ali
233 -- W unchecked_deallocation%s
234 -- W xref_tab%s xref_tab.adb xref_tab.ali
236 -- Then xref_tab.adb and types.adb are inserted in the Q if they are not
238 -- Note that there is no file listed under W unchecked_deallocation%s
239 -- so no generic body should ever be explicitly compiled (unless the
240 -- Main_Source at the start was a generic body).
242 -- 4. Repeat steps 2 and 3 above until the Q is empty
244 -- Note that the above algorithm works because the units withed in
245 -- subunits are transitively included in the W section (with section) of
246 -- the main unit. Likewise the withed units in a generic body needed
247 -- during a compilation are also transitively included in the W section
248 -- of the originally compiled file.
251 -- The driver of gnatmake. This routine puts it all together.
252 -- This utility can be used to automatically (re)compile (using
253 -- Compile_Sources), bind (using Bind) and link (using Link) a set of
254 -- ada sources. For more information on gnatmake and its precise usage
255 -- please refer to the gnat documentation.
257 -- Flags in Package Opt Affecting Gnatmake
258 -- ---------------------------------------
260 -- Check_Readonly_Files: True when -a present in command line
261 -- Check_Object_Consistency: Set to True by Gnatmake
262 -- Compile_Only: True when -c present in command line
263 -- Force_Compilations: True when -f present in command line
264 -- Maximum_Processes: Number of processes given by -jnum
265 -- Keep_Going: True when -k present in command line
266 -- List_Dependencies: True when -l present in command line
267 -- Do_Not_Execute True when -n present in command line
268 -- Quiet_Output: True when -q present in command line
269 -- Minimal_Recompilation: True when -m present in command line
270 -- Verbose_Mode: True when -v present in command line