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 ------------------------------------------------------------------------------
26 with Ada.Command_Line; use Ada.Command_Line;
27 with Osint; use Osint;
28 with Output; use Output;
31 with Snames; use Snames;
34 with System.Case_Util; use System.Case_Util;
37 package body Makeutl is
39 type Mark_Key is record
40 File : File_Name_Type;
43 -- Identify either a mono-unit source (when Index = 0) or a specific unit
44 -- in a multi-unit source.
46 -- There follow many global undocumented declarations, comments needed ???
48 Max_Mask_Num : constant := 2048;
50 subtype Mark_Num is Union_Id range 0 .. Max_Mask_Num - 1;
52 function Hash (Key : Mark_Key) return Mark_Num;
54 package Marks is new System.HTable.Simple_HTable
55 (Header_Num => Mark_Num,
61 -- A hash table to keep tracks of the marked units
63 type Linker_Options_Data is record
65 Options : String_List_Id;
68 Linker_Option_Initial_Count : constant := 20;
70 Linker_Options_Buffer : String_List_Access :=
71 new String_List (1 .. Linker_Option_Initial_Count);
73 Last_Linker_Option : Natural := 0;
75 package Linker_Opts is new Table.Table (
76 Table_Component_Type => Linker_Options_Data,
77 Table_Index_Type => Integer,
80 Table_Increment => 100,
81 Table_Name => "Make.Linker_Opts");
83 procedure Add_Linker_Option (Option : String);
90 (Option : String_Access;
91 To : in out String_List_Access;
92 Last : in out Natural)
95 if Last = To'Last then
97 New_Options : constant String_List_Access :=
98 new String_List (1 .. To'Last * 2);
100 New_Options (To'Range) := To.all;
102 -- Set all elements of the original options to null to avoid
103 -- deallocation of copies.
105 To.all := (others => null);
118 To : in out String_List_Access;
119 Last : in out Natural)
122 Add (Option => new String'(Option), To => To, Last => Last);
125 -----------------------
126 -- Add_Linker_Option --
127 -----------------------
129 procedure Add_Linker_Option (Option : String) is
131 if Option'Length > 0 then
132 if Last_Linker_Option = Linker_Options_Buffer'Last then
134 New_Buffer : constant String_List_Access :=
136 (1 .. Linker_Options_Buffer'Last +
137 Linker_Option_Initial_Count);
139 New_Buffer (Linker_Options_Buffer'Range) :=
140 Linker_Options_Buffer.all;
141 Linker_Options_Buffer.all := (others => null);
142 Free (Linker_Options_Buffer);
143 Linker_Options_Buffer := New_Buffer;
147 Last_Linker_Option := Last_Linker_Option + 1;
148 Linker_Options_Buffer (Last_Linker_Option) := new String'(Option);
150 end Add_Linker_Option;
156 function Create_Name (Name : String) return File_Name_Type is
159 Add_Str_To_Name_Buffer (Name);
163 function Create_Name (Name : String) return Name_Id is
166 Add_Str_To_Name_Buffer (Name);
170 function Create_Name (Name : String) return Path_Name_Type is
173 Add_Str_To_Name_Buffer (Name);
177 ----------------------
178 -- Delete_All_Marks --
179 ----------------------
181 procedure Delete_All_Marks is
184 end Delete_All_Marks;
186 ----------------------------
187 -- Executable_Prefix_Path --
188 ----------------------------
190 function Executable_Prefix_Path return String is
191 Exec_Name : constant String := Command_Name;
193 function Get_Install_Dir (S : String) return String;
194 -- S is the executable name preceeded by the absolute or relative
195 -- path, e.g. "c:\usr\bin\gcc.exe". Returns the absolute directory
196 -- where "bin" lies (in the example "C:\usr").
197 -- If the executable is not in a "bin" directory, return "".
199 ---------------------
200 -- Get_Install_Dir --
201 ---------------------
203 function Get_Install_Dir (S : String) return String is
205 Path_Last : Integer := 0;
208 for J in reverse Exec'Range loop
209 if Exec (J) = Directory_Separator then
215 if Path_Last >= Exec'First + 2 then
216 To_Lower (Exec (Path_Last - 2 .. Path_Last));
219 if Path_Last < Exec'First + 2
220 or else Exec (Path_Last - 2 .. Path_Last) /= "bin"
221 or else (Path_Last - 3 >= Exec'First
222 and then Exec (Path_Last - 3) /= Directory_Separator)
227 return Normalize_Pathname (Exec (Exec'First .. Path_Last - 4));
230 -- Beginning of Executable_Prefix_Path
233 -- First determine if a path prefix was placed in front of the
236 for J in reverse Exec_Name'Range loop
237 if Exec_Name (J) = Directory_Separator then
238 return Get_Install_Dir (Exec_Name);
242 -- If we get here, the user has typed the executable name with no
245 return Get_Install_Dir (Locate_Exec_On_Path (Exec_Name).all);
246 end Executable_Prefix_Path;
252 function Hash (Key : Mark_Key) return Mark_Num is
254 return Union_Id (Key.File) mod Max_Mask_Num;
261 procedure Inform (N : File_Name_Type; Msg : String) is
263 Inform (Name_Id (N), Msg);
266 procedure Inform (N : Name_Id := No_Name; Msg : String) is
268 Osint.Write_Program_Name;
282 ----------------------------
283 -- Is_External_Assignment --
284 ----------------------------
286 function Is_External_Assignment (Argv : String) return Boolean is
287 Start : Positive := 3;
288 Finish : Natural := Argv'Last;
291 pragma Assert (Argv'First = 1);
292 pragma Assert (Argv (1 .. 2) = "-X");
295 if Argv'Last < 5 then
298 elsif Argv (3) = '"' then
299 if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
303 Finish := Argv'Last - 1;
309 while Equal_Pos <= Finish and then Argv (Equal_Pos) /= '=' loop
310 Equal_Pos := Equal_Pos + 1;
314 or else Equal_Pos > Finish
319 (External_Name => Argv (Start .. Equal_Pos - 1),
320 Value => Argv (Equal_Pos + 1 .. Finish));
323 end Is_External_Assignment;
330 (Source_File : File_Name_Type;
331 Index : Int := 0) return Boolean
334 return Marks.Get (K => (File => Source_File, Index => Index));
337 -----------------------------
338 -- Linker_Options_Switches --
339 -----------------------------
341 function Linker_Options_Switches
342 (Project : Project_Id;
343 In_Tree : Project_Tree_Ref) return String_List
345 procedure Recursive_Add_Linker_Options (Proj : Project_Id);
346 -- The recursive routine used to add linker options
348 ----------------------------------
349 -- Recursive_Add_Linker_Options --
350 ----------------------------------
352 procedure Recursive_Add_Linker_Options (Proj : Project_Id) is
354 Linker_Package : Package_Id;
355 Options : Variable_Value;
356 Imported : Project_List;
359 if Proj /= No_Project then
360 Data := In_Tree.Projects.Table (Proj);
362 if not Data.Seen then
363 In_Tree.Projects.Table (Proj).Seen := True;
364 Imported := Data.Imported_Projects;
366 while Imported /= Empty_Project_List loop
367 Recursive_Add_Linker_Options
368 (In_Tree.Project_Lists.Table
370 Imported := In_Tree.Project_Lists.Table
374 if Proj /= Project then
377 (Name => Name_Linker,
378 In_Packages => Data.Decl.Packages,
384 Attribute_Or_Array_Name => Name_Linker_Options,
385 In_Package => Linker_Package,
388 -- If attribute is present, add the project with
389 -- the attribute to table Linker_Opts.
391 if Options /= Nil_Variable_Value then
392 Linker_Opts.Increment_Last;
393 Linker_Opts.Table (Linker_Opts.Last) :=
394 (Project => Proj, Options => Options.Values);
399 end Recursive_Add_Linker_Options;
401 -- Start of processing for Linker_Options_Switches
406 for Index in Project_Table.First ..
407 Project_Table.Last (In_Tree.Projects)
409 In_Tree.Projects.Table (Index).Seen := False;
412 Recursive_Add_Linker_Options (Project);
414 Last_Linker_Option := 0;
416 for Index in reverse 1 .. Linker_Opts.Last loop
418 Options : String_List_Id := Linker_Opts.Table (Index).Options;
419 Proj : constant Project_Id :=
420 Linker_Opts.Table (Index).Project;
424 -- If Dir_Path has not been computed for this project, do it now
426 if In_Tree.Projects.Table (Proj).Dir_Path = null then
427 In_Tree.Projects.Table (Proj).Dir_Path :=
430 (In_Tree.Projects.Table
434 while Options /= Nil_String loop
436 In_Tree.String_Elements.Table (Options).Value;
437 Get_Name_String (Option);
439 -- Do not consider empty linker options
441 if Name_Len /= 0 then
442 Add_Linker_Option (Name_Buffer (1 .. Name_Len));
444 -- Object files and -L switches specified with relative
445 -- paths must be converted to absolute paths.
447 Test_If_Relative_Path
449 Linker_Options_Buffer (Last_Linker_Option),
451 In_Tree.Projects.Table (Proj).Dir_Path,
452 Including_L_Switch => True);
456 In_Tree.String_Elements.Table (Options).Next;
461 return Linker_Options_Buffer (1 .. Last_Linker_Option);
462 end Linker_Options_Switches;
468 package body Mains is
470 package Names is new Table.Table
471 (Table_Component_Type => File_Name_Type,
472 Table_Index_Type => Integer,
473 Table_Low_Bound => 1,
475 Table_Increment => 100,
476 Table_Name => "Makeutl.Mains.Names");
477 -- The table that stores the mains
479 Current : Natural := 0;
480 -- The index of the last main retrieved from the table
486 procedure Add_Main (Name : String) is
489 Add_Str_To_Name_Buffer (Name);
490 Names.Increment_Last;
491 Names.Table (Names.Last) := Name_Find;
508 function Next_Main return String is
510 if Current >= Names.Last then
514 Current := Current + 1;
515 return Get_Name_String (Names.Table (Current));
519 ---------------------
520 -- Number_Of_Mains --
521 ---------------------
523 function Number_Of_Mains return Natural is
543 procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
545 Marks.Set (K => (File => Source_File, Index => Index), E => True);
548 ---------------------------
549 -- Test_If_Relative_Path --
550 ---------------------------
552 procedure Test_If_Relative_Path
553 (Switch : in out String_Access;
554 Parent : String_Access;
555 Including_L_Switch : Boolean := True;
556 Including_Non_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));
611 elsif Including_Non_Switch then
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');