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 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 Osint; use Osint;
32 with Snames; use Snames;
35 with System.Case_Util; use System.Case_Util;
38 package body Makeutl is
40 type Mark_Key is record
41 File : File_Name_Type;
44 -- Identify either a mono-unit source (when Index = 0) or a specific unit
45 -- in a multi-unit source.
47 -- There follow many global undocumented declarations, comments needed ???
49 Max_Mask_Num : constant := 2048;
51 subtype Mark_Num is Union_Id range 0 .. Max_Mask_Num - 1;
53 function Hash (Key : Mark_Key) return Mark_Num;
55 package Marks is new System.HTable.Simple_HTable
56 (Header_Num => Mark_Num,
62 -- A hash table to keep tracks of the marked units
64 type Linker_Options_Data is record
66 Options : String_List_Id;
69 Linker_Option_Initial_Count : constant := 20;
71 Linker_Options_Buffer : String_List_Access :=
72 new String_List (1 .. Linker_Option_Initial_Count);
74 Last_Linker_Option : Natural := 0;
76 package Linker_Opts is new Table.Table (
77 Table_Component_Type => Linker_Options_Data,
78 Table_Index_Type => Integer,
81 Table_Increment => 100,
82 Table_Name => "Make.Linker_Opts");
84 procedure Add_Linker_Option (Option : String);
86 -----------------------
87 -- Add_Linker_Option --
88 -----------------------
90 procedure Add_Linker_Option (Option : String) is
92 if Option'Length > 0 then
93 if Last_Linker_Option = Linker_Options_Buffer'Last then
95 New_Buffer : constant String_List_Access :=
97 (1 .. Linker_Options_Buffer'Last +
98 Linker_Option_Initial_Count);
100 New_Buffer (Linker_Options_Buffer'Range) :=
101 Linker_Options_Buffer.all;
102 Linker_Options_Buffer.all := (others => null);
103 Free (Linker_Options_Buffer);
104 Linker_Options_Buffer := New_Buffer;
108 Last_Linker_Option := Last_Linker_Option + 1;
109 Linker_Options_Buffer (Last_Linker_Option) := new String'(Option);
111 end Add_Linker_Option;
113 ----------------------
114 -- Delete_All_Marks --
115 ----------------------
117 procedure Delete_All_Marks is
120 end Delete_All_Marks;
122 ----------------------------
123 -- Executable_Prefix_Path --
124 ----------------------------
126 function Executable_Prefix_Path return String is
127 Exec_Name : constant String := Command_Name;
129 function Get_Install_Dir (S : String) return String;
130 -- S is the executable name preceeded by the absolute or relative
131 -- path, e.g. "c:\usr\bin\gcc.exe". Returns the absolute directory
132 -- where "bin" lies (in the example "C:\usr").
133 -- If the executable is not in a "bin" directory, return "".
135 ---------------------
136 -- Get_Install_Dir --
137 ---------------------
139 function Get_Install_Dir (S : String) return String is
141 Path_Last : Integer := 0;
144 for J in reverse Exec'Range loop
145 if Exec (J) = Directory_Separator then
151 if Path_Last >= Exec'First + 2 then
152 To_Lower (Exec (Path_Last - 2 .. Path_Last));
155 if Path_Last < Exec'First + 2
156 or else Exec (Path_Last - 2 .. Path_Last) /= "bin"
157 or else (Path_Last - 3 >= Exec'First
158 and then Exec (Path_Last - 3) /= Directory_Separator)
163 return Normalize_Pathname (Exec (Exec'First .. Path_Last - 4));
166 -- Beginning of Executable_Prefix_Path
169 -- First determine if a path prefix was placed in front of the
172 for J in reverse Exec_Name'Range loop
173 if Exec_Name (J) = Directory_Separator then
174 return Get_Install_Dir (Exec_Name);
178 -- If we get here, the user has typed the executable name with no
181 return Get_Install_Dir (Locate_Exec_On_Path (Exec_Name).all);
182 end Executable_Prefix_Path;
188 function Hash (Key : Mark_Key) return Mark_Num is
190 return Union_Id (Key.File) mod Max_Mask_Num;
193 ----------------------------
194 -- Is_External_Assignment --
195 ----------------------------
197 function Is_External_Assignment (Argv : String) return Boolean is
198 Start : Positive := 3;
199 Finish : Natural := Argv'Last;
202 pragma Assert (Argv'First = 1);
203 pragma Assert (Argv (1 .. 2) = "-X");
206 if Argv'Last < 5 then
209 elsif Argv (3) = '"' then
210 if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
214 Finish := Argv'Last - 1;
220 while Equal_Pos <= Finish and then Argv (Equal_Pos) /= '=' loop
221 Equal_Pos := Equal_Pos + 1;
225 or else Equal_Pos > Finish
230 (External_Name => Argv (Start .. Equal_Pos - 1),
231 Value => Argv (Equal_Pos + 1 .. Finish));
234 end Is_External_Assignment;
241 (Source_File : File_Name_Type;
242 Index : Int := 0) return Boolean
245 return Marks.Get (K => (File => Source_File, Index => Index));
248 -----------------------------
249 -- Linker_Options_Switches --
250 -----------------------------
252 function Linker_Options_Switches
253 (Project : Project_Id;
254 In_Tree : Project_Tree_Ref) return String_List
256 procedure Recursive_Add_Linker_Options (Proj : Project_Id);
257 -- The recursive routine used to add linker options
259 ----------------------------------
260 -- Recursive_Add_Linker_Options --
261 ----------------------------------
263 procedure Recursive_Add_Linker_Options (Proj : Project_Id) is
265 Linker_Package : Package_Id;
266 Options : Variable_Value;
267 Imported : Project_List;
270 if Proj /= No_Project then
271 Data := In_Tree.Projects.Table (Proj);
273 if not Data.Seen then
274 In_Tree.Projects.Table (Proj).Seen := True;
275 Imported := Data.Imported_Projects;
277 while Imported /= Empty_Project_List loop
278 Recursive_Add_Linker_Options
279 (In_Tree.Project_Lists.Table
281 Imported := In_Tree.Project_Lists.Table
285 if Proj /= Project then
288 (Name => Name_Linker,
289 In_Packages => Data.Decl.Packages,
295 Attribute_Or_Array_Name => Name_Linker_Options,
296 In_Package => Linker_Package,
299 -- If attribute is present, add the project with
300 -- the attribute to table Linker_Opts.
302 if Options /= Nil_Variable_Value then
303 Linker_Opts.Increment_Last;
304 Linker_Opts.Table (Linker_Opts.Last) :=
305 (Project => Proj, Options => Options.Values);
310 end Recursive_Add_Linker_Options;
312 -- Start of processing for Linker_Options_Switches
317 for Index in Project_Table.First ..
318 Project_Table.Last (In_Tree.Projects)
320 In_Tree.Projects.Table (Index).Seen := False;
323 Recursive_Add_Linker_Options (Project);
325 Last_Linker_Option := 0;
327 for Index in reverse 1 .. Linker_Opts.Last loop
329 Options : String_List_Id := Linker_Opts.Table (Index).Options;
330 Proj : constant Project_Id :=
331 Linker_Opts.Table (Index).Project;
335 -- If Dir_Path has not been computed for this project, do it now
337 if In_Tree.Projects.Table (Proj).Dir_Path = null then
338 In_Tree.Projects.Table (Proj).Dir_Path :=
341 (In_Tree.Projects.Table
345 while Options /= Nil_String loop
347 In_Tree.String_Elements.Table (Options).Value;
348 Get_Name_String (Option);
350 -- Do not consider empty linker options
352 if Name_Len /= 0 then
353 Add_Linker_Option (Name_Buffer (1 .. Name_Len));
355 -- Object files and -L switches specified with relative
356 -- paths must be converted to absolute paths.
358 Test_If_Relative_Path
360 Linker_Options_Buffer (Last_Linker_Option),
362 In_Tree.Projects.Table (Proj).Dir_Path,
363 Including_L_Switch => True);
367 In_Tree.String_Elements.Table (Options).Next;
372 return Linker_Options_Buffer (1 .. Last_Linker_Option);
373 end Linker_Options_Switches;
379 package body Mains is
381 package Names is new Table.Table
382 (Table_Component_Type => File_Name_Type,
383 Table_Index_Type => Integer,
384 Table_Low_Bound => 1,
386 Table_Increment => 100,
387 Table_Name => "Makeutl.Mains.Names");
388 -- The table that stores the mains
390 Current : Natural := 0;
391 -- The index of the last main retrieved from the table
397 procedure Add_Main (Name : String) is
400 Add_Str_To_Name_Buffer (Name);
401 Names.Increment_Last;
402 Names.Table (Names.Last) := Name_Find;
419 function Next_Main return String is
421 if Current >= Names.Last then
425 Current := Current + 1;
426 return Get_Name_String (Names.Table (Current));
430 ---------------------
431 -- Number_Of_Mains --
432 ---------------------
434 function Number_Of_Mains return Natural is
454 procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
456 Marks.Set (K => (File => Source_File, Index => Index), E => True);
459 ---------------------------
460 -- Test_If_Relative_Path --
461 ---------------------------
463 procedure Test_If_Relative_Path
464 (Switch : in out String_Access;
465 Parent : String_Access;
466 Including_L_Switch : Boolean := True)
469 if Switch /= null then
471 Sw : String (1 .. Switch'Length);
479 and then (Sw (2) = 'A'
481 or else (Including_L_Switch and then Sw (2) = 'L'))
490 and then (Sw (2 .. 3) = "aL"
491 or else Sw (2 .. 3) = "aO"
492 or else Sw (2 .. 3) = "aI")
500 -- Because relative path arguments to --RTS= may be relative
501 -- to the search directory prefix, those relative path
502 -- arguments are not converted.
504 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
505 if Parent = null or else Parent'Length = 0 then
507 ("relative search path switches (""",
509 """) are not allowed");
514 (Sw (1 .. Start - 1) &
516 Directory_Separator &
517 Sw (Start .. Sw'Last));
522 if not Is_Absolute_Path (Sw) then
523 if Parent = null or else Parent'Length = 0 then
525 ("relative paths (""", Sw, """) are not allowed");
529 new String'(Parent.all & Directory_Separator & Sw);
535 end Test_If_Relative_Path;
541 function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
547 Get_Name_String (ALI_File);
549 -- First, find the last dot
553 while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
554 Finish := Finish - 1;
561 -- Now check that the dot is preceded by digits
564 Finish := Finish - 1;
566 while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
570 -- If there is no difits, or if the digits are not preceded by
571 -- the character that precedes a unit index, this is not the ALI file
572 -- of a unit in a multi-unit source.
576 or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
581 -- Build the index from the digit(s)
583 while Start <= Finish loop
584 Result := Result * 10 +
585 Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');