1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2004-2009, 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 ------------------------------------------------------------------------------
27 with Osint; use Osint;
28 with Output; use Output;
31 with Snames; use Snames;
34 with Ada.Command_Line; use Ada.Command_Line;
36 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
38 with System.Case_Util; use System.Case_Util;
41 package body Makeutl is
43 type Mark_Key is record
44 File : File_Name_Type;
47 -- Identify either a mono-unit source (when Index = 0) or a specific unit
48 -- (index = 1's origin index of unit) in a multi-unit source.
50 -- There follow many global undocumented declarations, comments needed ???
52 Max_Mask_Num : constant := 2048;
54 subtype Mark_Num is Union_Id range 0 .. Max_Mask_Num - 1;
56 function Hash (Key : Mark_Key) return Mark_Num;
58 package Marks is new System.HTable.Simple_HTable
59 (Header_Num => Mark_Num,
65 -- A hash table to keep tracks of the marked units
67 type Linker_Options_Data is record
69 Options : String_List_Id;
72 Linker_Option_Initial_Count : constant := 20;
74 Linker_Options_Buffer : String_List_Access :=
75 new String_List (1 .. Linker_Option_Initial_Count);
77 Last_Linker_Option : Natural := 0;
79 package Linker_Opts is new Table.Table (
80 Table_Component_Type => Linker_Options_Data,
81 Table_Index_Type => Integer,
84 Table_Increment => 100,
85 Table_Name => "Make.Linker_Opts");
87 procedure Add_Linker_Option (Option : String);
94 (Option : String_Access;
95 To : in out String_List_Access;
96 Last : in out Natural)
99 if Last = To'Last then
101 New_Options : constant String_List_Access :=
102 new String_List (1 .. To'Last * 2);
105 New_Options (To'Range) := To.all;
107 -- Set all elements of the original options to null to avoid
108 -- deallocation of copies.
110 To.all := (others => null);
123 To : in out String_List_Access;
124 Last : in out Natural)
127 Add (Option => new String'(Option), To => To, Last => Last);
130 -----------------------
131 -- Add_Linker_Option --
132 -----------------------
134 procedure Add_Linker_Option (Option : String) is
136 if Option'Length > 0 then
137 if Last_Linker_Option = Linker_Options_Buffer'Last then
139 New_Buffer : constant String_List_Access :=
141 (1 .. Linker_Options_Buffer'Last +
142 Linker_Option_Initial_Count);
144 New_Buffer (Linker_Options_Buffer'Range) :=
145 Linker_Options_Buffer.all;
146 Linker_Options_Buffer.all := (others => null);
147 Free (Linker_Options_Buffer);
148 Linker_Options_Buffer := New_Buffer;
152 Last_Linker_Option := Last_Linker_Option + 1;
153 Linker_Options_Buffer (Last_Linker_Option) := new String'(Option);
155 end Add_Linker_Option;
161 function Create_Name (Name : String) return File_Name_Type is
164 Add_Str_To_Name_Buffer (Name);
168 function Create_Name (Name : String) return Name_Id is
171 Add_Str_To_Name_Buffer (Name);
175 function Create_Name (Name : String) return Path_Name_Type is
178 Add_Str_To_Name_Buffer (Name);
182 ----------------------
183 -- Delete_All_Marks --
184 ----------------------
186 procedure Delete_All_Marks is
189 end Delete_All_Marks;
191 ----------------------------
192 -- Executable_Prefix_Path --
193 ----------------------------
195 function Executable_Prefix_Path return String is
196 Exec_Name : constant String := Command_Name;
198 function Get_Install_Dir (S : String) return String;
199 -- S is the executable name preceded by the absolute or relative path,
200 -- e.g. "c:\usr\bin\gcc.exe". Returns the absolute directory where "bin"
201 -- lies (in the example "C:\usr"). If the executable is not in a "bin"
202 -- directory, return "".
204 ---------------------
205 -- Get_Install_Dir --
206 ---------------------
208 function Get_Install_Dir (S : String) return String is
210 Path_Last : Integer := 0;
213 for J in reverse Exec'Range loop
214 if Exec (J) = Directory_Separator then
220 if Path_Last >= Exec'First + 2 then
221 To_Lower (Exec (Path_Last - 2 .. Path_Last));
224 if Path_Last < Exec'First + 2
225 or else Exec (Path_Last - 2 .. Path_Last) /= "bin"
226 or else (Path_Last - 3 >= Exec'First
227 and then Exec (Path_Last - 3) /= Directory_Separator)
232 return Normalize_Pathname (Exec (Exec'First .. Path_Last - 4));
235 -- Beginning of Executable_Prefix_Path
238 -- First determine if a path prefix was placed in front of the
241 for J in reverse Exec_Name'Range loop
242 if Exec_Name (J) = Directory_Separator then
243 return Get_Install_Dir (Exec_Name);
247 -- If we get here, the user has typed the executable name with no
251 Path : constant String_Access := Locate_Exec_On_Path (Exec_Name);
256 return Get_Install_Dir (Path.all);
259 end Executable_Prefix_Path;
265 function Hash (Key : Mark_Key) return Mark_Num is
267 return Union_Id (Key.File) mod Max_Mask_Num;
274 procedure Inform (N : File_Name_Type; Msg : String) is
276 Inform (Name_Id (N), Msg);
279 procedure Inform (N : Name_Id := No_Name; Msg : String) is
281 Osint.Write_Program_Name;
289 Name : constant String := Get_Name_String (N);
291 if Debug.Debug_Flag_F and then Is_Absolute_Path (Name) then
292 Write_Str (File_Name (Name));
305 ----------------------------
306 -- Is_External_Assignment --
307 ----------------------------
309 function Is_External_Assignment (Argv : String) return Boolean is
310 Start : Positive := 3;
311 Finish : Natural := Argv'Last;
314 pragma Assert (Argv'First = 1);
315 pragma Assert (Argv (1 .. 2) = "-X");
318 if Argv'Last < 5 then
321 elsif Argv (3) = '"' then
322 if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
326 Finish := Argv'Last - 1;
332 while Equal_Pos <= Finish and then Argv (Equal_Pos) /= '=' loop
333 Equal_Pos := Equal_Pos + 1;
336 if Equal_Pos = Start or else Equal_Pos > Finish then
340 (External_Name => Argv (Start .. Equal_Pos - 1),
341 Value => Argv (Equal_Pos + 1 .. Finish));
344 end Is_External_Assignment;
351 (Source_File : File_Name_Type;
352 Index : Int := 0) return Boolean
355 return Marks.Get (K => (File => Source_File, Index => Index));
358 -----------------------------
359 -- Linker_Options_Switches --
360 -----------------------------
362 function Linker_Options_Switches
363 (Project : Project_Id;
364 In_Tree : Project_Tree_Ref) return String_List
366 procedure Recursive_Add (Proj : Project_Id; Dummy : in out Boolean);
367 -- The recursive routine used to add linker options
373 procedure Recursive_Add (Proj : Project_Id; Dummy : in out Boolean) is
374 pragma Unreferenced (Dummy);
376 Linker_Package : Package_Id;
377 Options : Variable_Value;
382 (Name => Name_Linker,
383 In_Packages => Proj.Decl.Packages,
390 Attribute_Or_Array_Name => Name_Linker_Options,
391 In_Package => Linker_Package,
394 -- If attribute is present, add the project with
395 -- the attribute to table Linker_Opts.
397 if Options /= Nil_Variable_Value then
398 Linker_Opts.Increment_Last;
399 Linker_Opts.Table (Linker_Opts.Last) :=
400 (Project => Proj, Options => Options.Values);
404 procedure For_All_Projects is
405 new For_Every_Project_Imported (Boolean, Recursive_Add);
407 Dummy : Boolean := False;
409 -- Start of processing for Linker_Options_Switches
414 For_All_Projects (Project, Dummy, Imported_First => True);
416 Last_Linker_Option := 0;
418 for Index in reverse 1 .. Linker_Opts.Last loop
420 Options : String_List_Id;
421 Proj : constant Project_Id :=
422 Linker_Opts.Table (Index).Project;
424 Dir_Path : constant String :=
425 Get_Name_String (Proj.Directory.Name);
428 Options := Linker_Opts.Table (Index).Options;
429 while Options /= Nil_String loop
430 Option := In_Tree.String_Elements.Table (Options).Value;
431 Get_Name_String (Option);
433 -- Do not consider empty linker options
435 if Name_Len /= 0 then
436 Add_Linker_Option (Name_Buffer (1 .. Name_Len));
438 -- Object files and -L switches specified with relative
439 -- paths must be converted to absolute paths.
441 Test_If_Relative_Path
442 (Switch => Linker_Options_Buffer (Last_Linker_Option),
444 Including_L_Switch => True);
447 Options := In_Tree.String_Elements.Table (Options).Next;
452 return Linker_Options_Buffer (1 .. Last_Linker_Option);
453 end Linker_Options_Switches;
459 package body Mains is
461 type File_And_Loc is record
462 File_Name : File_Name_Type;
463 Location : Source_Ptr := No_Location;
466 package Names is new Table.Table
467 (Table_Component_Type => File_And_Loc,
468 Table_Index_Type => Integer,
469 Table_Low_Bound => 1,
471 Table_Increment => 100,
472 Table_Name => "Makeutl.Mains.Names");
473 -- The table that stores the mains
475 Current : Natural := 0;
476 -- The index of the last main retrieved from the table
482 procedure Add_Main (Name : String) is
485 Add_Str_To_Name_Buffer (Name);
486 Names.Increment_Last;
487 Names.Table (Names.Last) := (Name_Find, No_Location);
504 function Get_Location return Source_Ptr is
506 if Current in Names.First .. Names.Last then
507 return Names.Table (Current).Location;
517 function Next_Main return String is
519 if Current >= Names.Last then
522 Current := Current + 1;
523 return Get_Name_String (Names.Table (Current).File_Name);
527 ---------------------
528 -- Number_Of_Mains --
529 ---------------------
531 function Number_Of_Mains return Natural is
549 procedure Set_Location (Location : Source_Ptr) is
551 if Names.Last > 0 then
552 Names.Table (Names.Last).Location := Location;
560 procedure Update_Main (Name : String) is
562 if Current in Names.First .. Names.Last then
564 Add_Str_To_Name_Buffer (Name);
565 Names.Table (Current).File_Name := Name_Find;
574 procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
576 Marks.Set (K => (File => Source_File, Index => Index), E => True);
579 -----------------------
580 -- Path_Or_File_Name --
581 -----------------------
583 function Path_Or_File_Name (Path : Path_Name_Type) return String is
584 Path_Name : constant String := Get_Name_String (Path);
586 if Debug.Debug_Flag_F then
587 return File_Name (Path_Name);
591 end Path_Or_File_Name;
593 ---------------------------
594 -- Test_If_Relative_Path --
595 ---------------------------
597 procedure Test_If_Relative_Path
598 (Switch : in out String_Access;
600 Including_L_Switch : Boolean := True;
601 Including_Non_Switch : Boolean := True;
602 Including_RTS : Boolean := False)
605 if Switch /= null then
607 Sw : String (1 .. Switch'Length);
615 and then (Sw (2) = 'A'
617 or else (Including_L_Switch and then Sw (2) = 'L'))
626 and then (Sw (2 .. 3) = "aL"
627 or else Sw (2 .. 3) = "aO"
628 or else Sw (2 .. 3) = "aI")
633 and then Sw'Length >= 7
634 and then Sw (2 .. 6) = "-RTS="
642 -- Because relative path arguments to --RTS= may be relative
643 -- to the search directory prefix, those relative path
644 -- arguments are converted only when they include directory
647 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
648 if Parent'Length = 0 then
650 ("relative search path switches ("""
652 & """) are not allowed");
654 elsif Including_RTS then
655 for J in Start .. Sw'Last loop
656 if Sw (J) = Directory_Separator then
659 (Sw (1 .. Start - 1) &
661 Directory_Separator &
662 Sw (Start .. Sw'Last));
670 (Sw (1 .. Start - 1) &
672 Directory_Separator &
673 Sw (Start .. Sw'Last));
677 elsif Including_Non_Switch then
678 if not Is_Absolute_Path (Sw) then
679 if Parent'Length = 0 then
681 ("relative paths (""" & Sw & """) are not allowed");
683 Switch := new String'(Parent & Directory_Separator & Sw);
689 end Test_If_Relative_Path;
695 function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
701 Get_Name_String (ALI_File);
703 -- First, find the last dot
707 while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
708 Finish := Finish - 1;
715 -- Now check that the dot is preceded by digits
718 Finish := Finish - 1;
720 while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
724 -- If there are no digits, or if the digits are not preceded by the
725 -- character that precedes a unit index, this is not the ALI file of
726 -- a unit in a multi-unit source.
730 or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
735 -- Build the index from the digit(s)
737 while Start <= Finish loop
738 Result := Result * 10 +
739 Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');