1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2004-2006, 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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, 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 with Ada.Command_Line; use Ada.Command_Line;
29 with Namet; use Namet;
30 with Osint; use Osint;
33 with Snames; use Snames;
36 with System.Case_Util; use System.Case_Util;
39 package body Makeutl is
41 type Mark_Key is record
42 File : File_Name_Type;
45 -- Identify either a mono-unit source (when Index = 0) or a specific unit
46 -- in a multi-unit source.
48 -- There follow many global undocumented declarations, comments needed ???
50 Max_Mask_Num : constant := 2048;
52 subtype Mark_Num is Union_Id range 0 .. Max_Mask_Num - 1;
54 function Hash (Key : Mark_Key) return Mark_Num;
56 package Marks is new System.HTable.Simple_HTable
57 (Header_Num => Mark_Num,
63 -- A hash table to keep tracks of the marked units
65 type Linker_Options_Data is record
67 Options : String_List_Id;
70 Linker_Option_Initial_Count : constant := 20;
72 Linker_Options_Buffer : String_List_Access :=
73 new String_List (1 .. Linker_Option_Initial_Count);
75 Last_Linker_Option : Natural := 0;
77 package Linker_Opts is new Table.Table (
78 Table_Component_Type => Linker_Options_Data,
79 Table_Index_Type => Integer,
82 Table_Increment => 100,
83 Table_Name => "Make.Linker_Opts");
85 procedure Add_Linker_Option (Option : String);
87 -----------------------
88 -- Add_Linker_Option --
89 -----------------------
91 procedure Add_Linker_Option (Option : String) is
93 if Option'Length > 0 then
94 if Last_Linker_Option = Linker_Options_Buffer'Last then
96 New_Buffer : constant String_List_Access :=
98 (1 .. Linker_Options_Buffer'Last +
99 Linker_Option_Initial_Count);
101 New_Buffer (Linker_Options_Buffer'Range) :=
102 Linker_Options_Buffer.all;
103 Linker_Options_Buffer.all := (others => null);
104 Free (Linker_Options_Buffer);
105 Linker_Options_Buffer := New_Buffer;
109 Last_Linker_Option := Last_Linker_Option + 1;
110 Linker_Options_Buffer (Last_Linker_Option) := new String'(Option);
112 end Add_Linker_Option;
114 ----------------------
115 -- Delete_All_Marks --
116 ----------------------
118 procedure Delete_All_Marks is
121 end Delete_All_Marks;
123 ----------------------------
124 -- Executable_Prefix_Path --
125 ----------------------------
127 function Executable_Prefix_Path return String is
128 Exec_Name : constant String := Command_Name;
130 function Get_Install_Dir (S : String) return String;
131 -- S is the executable name preceeded by the absolute or relative
132 -- path, e.g. "c:\usr\bin\gcc.exe". Returns the absolute directory
133 -- where "bin" lies (in the example "C:\usr").
134 -- If the executable is not in a "bin" directory, return "".
136 ---------------------
137 -- Get_Install_Dir --
138 ---------------------
140 function Get_Install_Dir (S : String) return String is
142 Path_Last : Integer := 0;
145 for J in reverse Exec'Range loop
146 if Exec (J) = Directory_Separator then
152 if Path_Last >= Exec'First + 2 then
153 To_Lower (Exec (Path_Last - 2 .. Path_Last));
156 if Path_Last < Exec'First + 2
157 or else Exec (Path_Last - 2 .. Path_Last) /= "bin"
158 or else (Path_Last - 3 >= Exec'First
159 and then Exec (Path_Last - 3) /= Directory_Separator)
164 return Normalize_Pathname (Exec (Exec'First .. Path_Last - 4));
167 -- Beginning of Executable_Prefix_Path
170 -- First determine if a path prefix was placed in front of the
173 for J in reverse Exec_Name'Range loop
174 if Exec_Name (J) = Directory_Separator then
175 return Get_Install_Dir (Exec_Name);
179 -- If we get here, the user has typed the executable name with no
182 return Get_Install_Dir (Locate_Exec_On_Path (Exec_Name).all);
183 end Executable_Prefix_Path;
189 function Hash (Key : Mark_Key) return Mark_Num is
191 return Union_Id (Key.File) mod Max_Mask_Num;
194 ----------------------------
195 -- Is_External_Assignment --
196 ----------------------------
198 function Is_External_Assignment (Argv : String) return Boolean is
199 Start : Positive := 3;
200 Finish : Natural := Argv'Last;
203 pragma Assert (Argv'First = 1);
204 pragma Assert (Argv (1 .. 2) = "-X");
207 if Argv'Last < 5 then
210 elsif Argv (3) = '"' then
211 if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
215 Finish := Argv'Last - 1;
221 while Equal_Pos <= Finish and then Argv (Equal_Pos) /= '=' loop
222 Equal_Pos := Equal_Pos + 1;
226 or else Equal_Pos >= Finish
231 (External_Name => Argv (Start .. Equal_Pos - 1),
232 Value => Argv (Equal_Pos + 1 .. Finish));
235 end Is_External_Assignment;
242 (Source_File : File_Name_Type;
243 Index : Int := 0) return Boolean
246 return Marks.Get (K => (File => Source_File, Index => Index));
249 -----------------------------
250 -- Linker_Options_Switches --
251 -----------------------------
253 function Linker_Options_Switches
254 (Project : Project_Id;
255 In_Tree : Project_Tree_Ref) return String_List
257 procedure Recursive_Add_Linker_Options (Proj : Project_Id);
258 -- The recursive routine used to add linker options
260 ----------------------------------
261 -- Recursive_Add_Linker_Options --
262 ----------------------------------
264 procedure Recursive_Add_Linker_Options (Proj : Project_Id) is
266 Linker_Package : Package_Id;
267 Options : Variable_Value;
268 Imported : Project_List;
271 if Proj /= No_Project then
272 Data := In_Tree.Projects.Table (Proj);
274 if not Data.Seen then
275 In_Tree.Projects.Table (Proj).Seen := True;
276 Imported := Data.Imported_Projects;
278 while Imported /= Empty_Project_List loop
279 Recursive_Add_Linker_Options
280 (In_Tree.Project_Lists.Table
282 Imported := In_Tree.Project_Lists.Table
286 if Proj /= Project then
289 (Name => Name_Linker,
290 In_Packages => Data.Decl.Packages,
296 Attribute_Or_Array_Name => Name_Linker_Options,
297 In_Package => Linker_Package,
300 -- If attribute is present, add the project with
301 -- the attribute to table Linker_Opts.
303 if Options /= Nil_Variable_Value then
304 Linker_Opts.Increment_Last;
305 Linker_Opts.Table (Linker_Opts.Last) :=
306 (Project => Proj, Options => Options.Values);
311 end Recursive_Add_Linker_Options;
313 -- Start of processing for Linker_Options_Switches
318 for Index in Project_Table.First ..
319 Project_Table.Last (In_Tree.Projects)
321 In_Tree.Projects.Table (Index).Seen := False;
324 Recursive_Add_Linker_Options (Project);
326 Last_Linker_Option := 0;
328 for Index in reverse 1 .. Linker_Opts.Last loop
330 Options : String_List_Id := Linker_Opts.Table (Index).Options;
331 Proj : constant Project_Id :=
332 Linker_Opts.Table (Index).Project;
336 -- If Dir_Path has not been computed for this project, do it now
338 if In_Tree.Projects.Table (Proj).Dir_Path = null then
339 In_Tree.Projects.Table (Proj).Dir_Path :=
342 (In_Tree.Projects.Table
346 while Options /= Nil_String loop
348 In_Tree.String_Elements.Table (Options).Value;
349 Get_Name_String (Option);
351 -- Do not consider empty linker options
353 if Name_Len /= 0 then
354 Add_Linker_Option (Name_Buffer (1 .. Name_Len));
356 -- Object files and -L switches specified with relative
357 -- paths must be converted to absolute paths.
359 Test_If_Relative_Path
361 Linker_Options_Buffer (Last_Linker_Option),
363 In_Tree.Projects.Table (Proj).Dir_Path,
364 Including_L_Switch => True);
368 In_Tree.String_Elements.Table (Options).Next;
373 return Linker_Options_Buffer (1 .. Last_Linker_Option);
374 end Linker_Options_Switches;
380 package body Mains is
382 package Names is new Table.Table
383 (Table_Component_Type => File_Name_Type,
384 Table_Index_Type => Integer,
385 Table_Low_Bound => 1,
387 Table_Increment => 100,
388 Table_Name => "Makeutl.Mains.Names");
389 -- The table that stores the mains
391 Current : Natural := 0;
392 -- The index of the last main retrieved from the table
398 procedure Add_Main (Name : String) is
401 Add_Str_To_Name_Buffer (Name);
402 Names.Increment_Last;
403 Names.Table (Names.Last) := Name_Find;
420 function Next_Main return String is
422 if Current >= Names.Last then
426 Current := Current + 1;
427 return Get_Name_String (Names.Table (Current));
431 ---------------------
432 -- Number_Of_Mains --
433 ---------------------
435 function Number_Of_Mains return Natural is
455 procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
457 Marks.Set (K => (File => Source_File, Index => Index), E => True);
460 ---------------------------
461 -- Test_If_Relative_Path --
462 ---------------------------
464 procedure Test_If_Relative_Path
465 (Switch : in out String_Access;
466 Parent : String_Access;
467 Including_L_Switch : Boolean := True)
470 if Switch /= null then
472 Sw : String (1 .. Switch'Length);
480 and then (Sw (2) = 'A'
482 or else (Including_L_Switch and then Sw (2) = 'L'))
491 and then (Sw (2 .. 3) = "aL"
492 or else Sw (2 .. 3) = "aO"
493 or else Sw (2 .. 3) = "aI")
501 -- Because relative path arguments to --RTS= may be relative
502 -- to the search directory prefix, those relative path
503 -- arguments are not converted.
505 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
506 if Parent = null or else Parent'Length = 0 then
508 ("relative search path switches (""",
510 """) are not allowed");
515 (Sw (1 .. Start - 1) &
517 Directory_Separator &
518 Sw (Start .. Sw'Last));
523 if not Is_Absolute_Path (Sw) then
524 if Parent = null or else Parent'Length = 0 then
526 ("relative paths (""", Sw, """) are not allowed");
530 new String'(Parent.all & Directory_Separator & Sw);
536 end Test_If_Relative_Path;
542 function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
548 Get_Name_String (ALI_File);
550 -- First, find the last dot
554 while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
555 Finish := Finish - 1;
562 -- Now check that the dot is preceded by digits
565 Finish := Finish - 1;
567 while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
571 -- If there is no difits, or if the digits are not preceded by
572 -- the character that precedes a unit index, this is not the ALI file
573 -- of a unit in a multi-unit source.
577 or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
582 -- Build the index from the digit(s)
584 while Start <= Finish loop
585 Result := Result * 10 +
586 Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');