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;
28 with Osint; use Osint;
29 with Output; use Output;
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);
91 (Option : String_Access;
92 To : in out String_List_Access;
93 Last : in out Natural)
96 if Last = To'Last then
98 New_Options : constant String_List_Access :=
99 new String_List (1 .. To'Last * 2);
101 New_Options (To'Range) := To.all;
103 -- Set all elements of the original options to null to avoid
104 -- deallocation of copies.
106 To.all := (others => null);
119 To : in out String_List_Access;
120 Last : in out Natural)
123 Add (Option => new String'(Option), To => To, Last => Last);
126 -----------------------
127 -- Add_Linker_Option --
128 -----------------------
130 procedure Add_Linker_Option (Option : String) is
132 if Option'Length > 0 then
133 if Last_Linker_Option = Linker_Options_Buffer'Last then
135 New_Buffer : constant String_List_Access :=
137 (1 .. Linker_Options_Buffer'Last +
138 Linker_Option_Initial_Count);
140 New_Buffer (Linker_Options_Buffer'Range) :=
141 Linker_Options_Buffer.all;
142 Linker_Options_Buffer.all := (others => null);
143 Free (Linker_Options_Buffer);
144 Linker_Options_Buffer := New_Buffer;
148 Last_Linker_Option := Last_Linker_Option + 1;
149 Linker_Options_Buffer (Last_Linker_Option) := new String'(Option);
151 end Add_Linker_Option;
157 function Create_Name (Name : String) return File_Name_Type is
160 Add_Str_To_Name_Buffer (Name);
164 function Create_Name (Name : String) return Name_Id is
167 Add_Str_To_Name_Buffer (Name);
171 function Create_Name (Name : String) return Path_Name_Type is
174 Add_Str_To_Name_Buffer (Name);
178 ----------------------
179 -- Delete_All_Marks --
180 ----------------------
182 procedure Delete_All_Marks is
185 end Delete_All_Marks;
187 ----------------------------
188 -- Executable_Prefix_Path --
189 ----------------------------
191 function Executable_Prefix_Path return String is
192 Exec_Name : constant String := Command_Name;
194 function Get_Install_Dir (S : String) return String;
195 -- S is the executable name preceeded by the absolute or relative
196 -- path, e.g. "c:\usr\bin\gcc.exe". Returns the absolute directory
197 -- where "bin" lies (in the example "C:\usr").
198 -- If the executable is not in a "bin" directory, return "".
200 ---------------------
201 -- Get_Install_Dir --
202 ---------------------
204 function Get_Install_Dir (S : String) return String is
206 Path_Last : Integer := 0;
209 for J in reverse Exec'Range loop
210 if Exec (J) = Directory_Separator then
216 if Path_Last >= Exec'First + 2 then
217 To_Lower (Exec (Path_Last - 2 .. Path_Last));
220 if Path_Last < Exec'First + 2
221 or else Exec (Path_Last - 2 .. Path_Last) /= "bin"
222 or else (Path_Last - 3 >= Exec'First
223 and then Exec (Path_Last - 3) /= Directory_Separator)
228 return Normalize_Pathname (Exec (Exec'First .. Path_Last - 4));
231 -- Beginning of Executable_Prefix_Path
234 -- First determine if a path prefix was placed in front of the
237 for J in reverse Exec_Name'Range loop
238 if Exec_Name (J) = Directory_Separator then
239 return Get_Install_Dir (Exec_Name);
243 -- If we get here, the user has typed the executable name with no
246 return Get_Install_Dir (Locate_Exec_On_Path (Exec_Name).all);
247 end Executable_Prefix_Path;
253 function Hash (Key : Mark_Key) return Mark_Num is
255 return Union_Id (Key.File) mod Max_Mask_Num;
262 procedure Inform (N : File_Name_Type; Msg : String) is
264 Inform (Name_Id (N), Msg);
267 procedure Inform (N : Name_Id := No_Name; Msg : String) is
269 Osint.Write_Program_Name;
283 ----------------------------
284 -- Is_External_Assignment --
285 ----------------------------
287 function Is_External_Assignment (Argv : String) return Boolean is
288 Start : Positive := 3;
289 Finish : Natural := Argv'Last;
292 pragma Assert (Argv'First = 1);
293 pragma Assert (Argv (1 .. 2) = "-X");
296 if Argv'Last < 5 then
299 elsif Argv (3) = '"' then
300 if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
304 Finish := Argv'Last - 1;
310 while Equal_Pos <= Finish and then Argv (Equal_Pos) /= '=' loop
311 Equal_Pos := Equal_Pos + 1;
315 or else Equal_Pos > Finish
320 (External_Name => Argv (Start .. Equal_Pos - 1),
321 Value => Argv (Equal_Pos + 1 .. Finish));
324 end Is_External_Assignment;
331 (Source_File : File_Name_Type;
332 Index : Int := 0) return Boolean
335 return Marks.Get (K => (File => Source_File, Index => Index));
338 -----------------------------
339 -- Linker_Options_Switches --
340 -----------------------------
342 function Linker_Options_Switches
343 (Project : Project_Id;
344 In_Tree : Project_Tree_Ref) return String_List
346 procedure Recursive_Add_Linker_Options (Proj : Project_Id);
347 -- The recursive routine used to add linker options
349 ----------------------------------
350 -- Recursive_Add_Linker_Options --
351 ----------------------------------
353 procedure Recursive_Add_Linker_Options (Proj : Project_Id) is
355 Linker_Package : Package_Id;
356 Options : Variable_Value;
357 Imported : Project_List;
360 if Proj /= No_Project then
361 Data := In_Tree.Projects.Table (Proj);
363 if not Data.Seen then
364 In_Tree.Projects.Table (Proj).Seen := True;
365 Imported := Data.Imported_Projects;
367 while Imported /= Empty_Project_List loop
368 Recursive_Add_Linker_Options
369 (In_Tree.Project_Lists.Table
371 Imported := In_Tree.Project_Lists.Table
375 if Proj /= Project then
378 (Name => Name_Linker,
379 In_Packages => Data.Decl.Packages,
385 Attribute_Or_Array_Name => Name_Linker_Options,
386 In_Package => Linker_Package,
389 -- If attribute is present, add the project with
390 -- the attribute to table Linker_Opts.
392 if Options /= Nil_Variable_Value then
393 Linker_Opts.Increment_Last;
394 Linker_Opts.Table (Linker_Opts.Last) :=
395 (Project => Proj, Options => Options.Values);
400 end Recursive_Add_Linker_Options;
402 -- Start of processing for Linker_Options_Switches
407 for Index in Project_Table.First ..
408 Project_Table.Last (In_Tree.Projects)
410 In_Tree.Projects.Table (Index).Seen := False;
413 Recursive_Add_Linker_Options (Project);
415 Last_Linker_Option := 0;
417 for Index in reverse 1 .. Linker_Opts.Last loop
419 Options : String_List_Id := Linker_Opts.Table (Index).Options;
420 Proj : constant Project_Id :=
421 Linker_Opts.Table (Index).Project;
425 -- If Dir_Path has not been computed for this project, do it now
427 if In_Tree.Projects.Table (Proj).Dir_Path = null then
428 In_Tree.Projects.Table (Proj).Dir_Path :=
431 (In_Tree.Projects.Table
435 while Options /= Nil_String loop
437 In_Tree.String_Elements.Table (Options).Value;
438 Get_Name_String (Option);
440 -- Do not consider empty linker options
442 if Name_Len /= 0 then
443 Add_Linker_Option (Name_Buffer (1 .. Name_Len));
445 -- Object files and -L switches specified with relative
446 -- paths must be converted to absolute paths.
448 Test_If_Relative_Path
450 Linker_Options_Buffer (Last_Linker_Option),
452 In_Tree.Projects.Table (Proj).Dir_Path,
453 Including_L_Switch => True);
457 In_Tree.String_Elements.Table (Options).Next;
462 return Linker_Options_Buffer (1 .. Last_Linker_Option);
463 end Linker_Options_Switches;
469 package body Mains is
471 package Names is new Table.Table
472 (Table_Component_Type => File_Name_Type,
473 Table_Index_Type => Integer,
474 Table_Low_Bound => 1,
476 Table_Increment => 100,
477 Table_Name => "Makeutl.Mains.Names");
478 -- The table that stores the mains
480 Current : Natural := 0;
481 -- The index of the last main retrieved from the table
487 procedure Add_Main (Name : String) is
490 Add_Str_To_Name_Buffer (Name);
491 Names.Increment_Last;
492 Names.Table (Names.Last) := Name_Find;
509 function Next_Main return String is
511 if Current >= Names.Last then
515 Current := Current + 1;
516 return Get_Name_String (Names.Table (Current));
520 ---------------------
521 -- Number_Of_Mains --
522 ---------------------
524 function Number_Of_Mains return Natural is
544 procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
546 Marks.Set (K => (File => Source_File, Index => Index), E => True);
549 ---------------------------
550 -- Test_If_Relative_Path --
551 ---------------------------
553 procedure Test_If_Relative_Path
554 (Switch : in out String_Access;
555 Parent : String_Access;
556 Including_L_Switch : Boolean := True)
559 if Switch /= null then
561 Sw : String (1 .. Switch'Length);
569 and then (Sw (2) = 'A'
571 or else (Including_L_Switch and then Sw (2) = 'L'))
580 and then (Sw (2 .. 3) = "aL"
581 or else Sw (2 .. 3) = "aO"
582 or else Sw (2 .. 3) = "aI")
590 -- Because relative path arguments to --RTS= may be relative
591 -- to the search directory prefix, those relative path
592 -- arguments are not converted.
594 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
595 if Parent = null or else Parent'Length = 0 then
597 ("relative search path switches (""",
599 """) are not allowed");
604 (Sw (1 .. Start - 1) &
606 Directory_Separator &
607 Sw (Start .. Sw'Last));
612 if not Is_Absolute_Path (Sw) then
613 if Parent = null or else Parent'Length = 0 then
615 ("relative paths (""", Sw, """) are not allowed");
619 new String'(Parent.all & Directory_Separator & Sw);
625 end Test_If_Relative_Path;
631 function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
637 Get_Name_String (ALI_File);
639 -- First, find the last dot
643 while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
644 Finish := Finish - 1;
651 -- Now check that the dot is preceded by digits
654 Finish := Finish - 1;
656 while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
660 -- If there is no difits, or if the digits are not preceded by
661 -- the character that precedes a unit index, this is not the ALI file
662 -- of a unit in a multi-unit source.
666 or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
671 -- Build the index from the digit(s)
673 while Start <= Finish loop
674 Result := Result * 10 +
675 Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');