1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-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.Unchecked_Deallocation;
29 with GNAT.Case_Util; use GNAT.Case_Util;
31 with Osint; use Osint;
32 with Output; use Output;
34 with Snames; use Snames;
35 with Targparm; use Targparm;
37 package body Prj.Util is
39 procedure Free is new Ada.Unchecked_Deallocation
40 (Text_File_Data, Text_File);
46 procedure Close (File : in out Text_File) is
49 Prj.Com.Fail ("Close attempted on an invalid Text_File");
52 -- Close file, no need to test status, since this is a file that we
53 -- read, and the file was read successfully before we closed it.
64 (This : in out Name_List_Index;
65 In_Tree : Project_Tree_Ref)
67 Old_Current : Name_List_Index;
68 New_Current : Name_List_Index;
71 if This /= No_Name_List then
73 Name_List_Table.Increment_Last (In_Tree.Name_Lists);
74 New_Current := Name_List_Table.Last (In_Tree.Name_Lists);
76 In_Tree.Name_Lists.Table (New_Current) :=
77 (In_Tree.Name_Lists.Table (Old_Current).Name, No_Name_List);
80 Old_Current := In_Tree.Name_Lists.Table (Old_Current).Next;
81 exit when Old_Current = No_Name_List;
82 In_Tree.Name_Lists.Table (New_Current).Next := New_Current + 1;
83 Name_List_Table.Increment_Last (In_Tree.Name_Lists);
84 New_Current := New_Current + 1;
85 In_Tree.Name_Lists.Table (New_Current) :=
86 (In_Tree.Name_Lists.Table (Old_Current).Name, No_Name_List);
95 function End_Of_File (File : Text_File) return Boolean is
98 Prj.Com.Fail ("End_Of_File attempted on an invalid Text_File");
101 return File.End_Of_File_Reached;
108 function Executable_Of
109 (Project : Project_Id;
110 In_Tree : Project_Tree_Ref;
111 Main : File_Name_Type;
113 Ada_Main : Boolean := True) return File_Name_Type
115 pragma Assert (Project /= No_Project);
117 The_Packages : constant Package_Id :=
118 In_Tree.Projects.Table (Project).Decl.Packages;
120 Builder_Package : constant Prj.Package_Id :=
122 (Name => Name_Builder,
123 In_Packages => The_Packages,
126 Executable : Variable_Value :=
128 (Name => Name_Id (Main),
130 Attribute_Or_Array_Name => Name_Executable,
131 In_Package => Builder_Package,
134 Executable_Suffix : Variable_Value := Nil_Variable_Value;
136 Executable_Suffix_Name : Name_Id := No_Name;
138 Naming : constant Naming_Data := In_Tree.Projects.Table (Project).Naming;
140 Body_Suffix : constant String :=
141 Body_Suffix_Of (In_Tree, "ada", Naming);
143 Spec_Suffix : constant String :=
144 Spec_Suffix_Of (In_Tree, "ada", Naming);
147 if Builder_Package /= No_Package then
148 if Get_Mode = Multi_Language then
149 Executable_Suffix_Name := In_Tree.Config.Executable_Suffix;
152 Executable_Suffix := Prj.Util.Value_Of
153 (Variable_Name => Name_Executable_Suffix,
154 In_Variables => In_Tree.Packages.Table
155 (Builder_Package).Decl.Attributes,
158 if Executable_Suffix /= Nil_Variable_Value
159 and then not Executable_Suffix.Default
161 Executable_Suffix_Name := Executable_Suffix.Value;
165 if Executable = Nil_Variable_Value and Ada_Main then
166 Get_Name_String (Main);
168 -- Try as index the name minus the implementation suffix or minus
169 -- the specification suffix.
172 Name : constant String (1 .. Name_Len) :=
173 Name_Buffer (1 .. Name_Len);
174 Last : Positive := Name_Len;
176 Truncated : Boolean := False;
179 if Last > Body_Suffix'Length
180 and then Name (Last - Body_Suffix'Length + 1 .. Last) =
184 Last := Last - Body_Suffix'Length;
188 and then Last > Spec_Suffix'Length
189 and then Name (Last - Spec_Suffix'Length + 1 .. Last) =
193 Last := Last - Spec_Suffix'Length;
198 Name_Buffer (1 .. Name_Len) := Name (1 .. Last);
203 Attribute_Or_Array_Name => Name_Executable,
204 In_Package => Builder_Package,
210 -- If we have found an Executable attribute, return its value,
211 -- possibly suffixed by the executable suffix.
213 if Executable /= Nil_Variable_Value
214 and then Executable.Value /= Empty_Name
216 -- Get the executable name. If Executable_Suffix is defined,
217 -- make sure that it will be the extension of the executable.
220 Saved_EEOT : constant Name_Id := Executable_Extension_On_Target;
221 Result : File_Name_Type;
224 if Executable_Suffix_Name /= No_Name then
225 Executable_Extension_On_Target := Executable_Suffix_Name;
228 Result := Executable_Name (File_Name_Type (Executable.Value));
229 Executable_Extension_On_Target := Saved_EEOT;
235 Get_Name_String (Main);
237 -- If there is a body suffix or a spec suffix, remove this suffix,
238 -- otherwise remove any suffix ('.' followed by other characters), if
241 if Ada_Main and then Name_Len > Body_Suffix'Length
242 and then Name_Buffer (Name_Len - Body_Suffix'Length + 1 .. Name_Len) =
245 -- Found the body termination, remove it
247 Name_Len := Name_Len - Body_Suffix'Length;
249 elsif Ada_Main and then Name_Len > Spec_Suffix'Length
250 and then Name_Buffer (Name_Len - Spec_Suffix'Length + 1 .. Name_Len) =
253 -- Found the spec termination, remove it
255 Name_Len := Name_Len - Spec_Suffix'Length;
258 -- Remove any suffix, if there is one
260 Get_Name_String (Strip_Suffix (Main));
263 if Executable_Suffix /= Nil_Variable_Value
264 and then not Executable_Suffix.Default
266 -- If attribute Executable_Suffix is specified, add this suffix
269 Suffix : constant String :=
270 Get_Name_String (Executable_Suffix.Value);
272 Name_Buffer (Name_Len + 1 .. Name_Len + Suffix'Length) := Suffix;
273 Name_Len := Name_Len + Suffix'Length;
278 -- Get the executable name. If Executable_Suffix is defined in the
279 -- configuration, make sure that it will be the extension of the
283 Saved_EEOT : constant Name_Id := Executable_Extension_On_Target;
284 Result : File_Name_Type;
287 Executable_Extension_On_Target := In_Tree.Config.Executable_Suffix;
288 Result := Executable_Name (Name_Find);
289 Executable_Extension_On_Target := Saved_EEOT;
314 if File.Cursor = File.Buffer_Len then
318 A => File.Buffer'Address,
319 N => File.Buffer'Length);
321 if File.Buffer_Len = 0 then
322 File.End_Of_File_Reached := True;
329 File.Cursor := File.Cursor + 1;
333 -- Start of processing for Get_Line
337 Prj.Com.Fail ("Get_Line attempted on an invalid Text_File");
340 Last := Line'First - 1;
342 if not File.End_Of_File_Reached then
344 C := File.Buffer (File.Cursor);
345 exit when C = ASCII.CR or else C = ASCII.LF;
350 if File.End_Of_File_Reached then
354 exit when Last = Line'Last;
357 if C = ASCII.CR or else C = ASCII.LF then
360 if File.End_Of_File_Reached then
366 and then File.Buffer (File.Cursor) = ASCII.LF
377 function Is_Valid (File : Text_File) return Boolean is
386 procedure Open (File : out Text_File; Name : String) is
387 FD : File_Descriptor;
388 File_Name : String (1 .. Name'Length + 1);
391 File_Name (1 .. Name'Length) := Name;
392 File_Name (File_Name'Last) := ASCII.NUL;
393 FD := Open_Read (Name => File_Name'Address,
394 Fmode => GNAT.OS_Lib.Text);
396 if FD = Invalid_FD then
400 File := new Text_File_Data;
404 A => File.Buffer'Address,
405 N => File.Buffer'Length);
407 if File.Buffer_Len = 0 then
408 File.End_Of_File_Reached := True;
420 (Into_List : in out Name_List_Index;
421 From_List : String_List_Id;
422 In_Tree : Project_Tree_Ref)
424 Current_Name : Name_List_Index;
425 List : String_List_Id;
426 Element : String_Element;
427 Last : Name_List_Index :=
428 Name_List_Table.Last (In_Tree.Name_Lists);
431 Current_Name := Into_List;
432 while Current_Name /= No_Name_List and then
433 In_Tree.Name_Lists.Table (Current_Name).Next /= No_Name_List
435 Current_Name := In_Tree.Name_Lists.Table (Current_Name).Next;
439 while List /= Nil_String loop
440 Element := In_Tree.String_Elements.Table (List);
442 Name_List_Table.Append
444 (Name => Element.Value, Next => No_Name_List));
448 if Current_Name = No_Name_List then
452 In_Tree.Name_Lists.Table (Current_Name).Next := Last;
455 Current_Name := Last;
457 List := Element.Next;
466 (Variable : Variable_Value;
467 Default : String) return String
470 if Variable.Kind /= Single
471 or else Variable.Default
472 or else Variable.Value = No_Name
476 return Get_Name_String (Variable.Value);
482 In_Array : Array_Element_Id;
483 In_Tree : Project_Tree_Ref) return Name_Id
485 Current : Array_Element_Id;
486 Element : Array_Element;
487 Real_Index : Name_Id := Index;
492 if Current = No_Array_Element then
496 Element := In_Tree.Array_Elements.Table (Current);
498 if not Element.Index_Case_Sensitive then
499 Get_Name_String (Index);
500 To_Lower (Name_Buffer (1 .. Name_Len));
501 Real_Index := Name_Find;
504 while Current /= No_Array_Element loop
505 Element := In_Tree.Array_Elements.Table (Current);
507 if Real_Index = Element.Index then
508 exit when Element.Value.Kind /= Single;
509 exit when Element.Value.Value = Empty_String;
510 return Element.Value.Value;
512 Current := Element.Next;
521 Src_Index : Int := 0;
522 In_Array : Array_Element_Id;
523 In_Tree : Project_Tree_Ref;
524 Force_Lower_Case_Index : Boolean := False) return Variable_Value
526 Current : Array_Element_Id;
527 Element : Array_Element;
528 Real_Index : Name_Id;
533 if Current = No_Array_Element then
534 return Nil_Variable_Value;
537 Element := In_Tree.Array_Elements.Table (Current);
541 if not Element.Index_Case_Sensitive or Force_Lower_Case_Index then
542 Get_Name_String (Index);
543 To_Lower (Name_Buffer (1 .. Name_Len));
544 Real_Index := Name_Find;
547 while Current /= No_Array_Element loop
548 Element := In_Tree.Array_Elements.Table (Current);
550 if Real_Index = Element.Index and then
551 Src_Index = Element.Src_Index
553 return Element.Value;
555 Current := Element.Next;
559 return Nil_Variable_Value;
565 Attribute_Or_Array_Name : Name_Id;
566 In_Package : Package_Id;
567 In_Tree : Project_Tree_Ref;
568 Force_Lower_Case_Index : Boolean := False) return Variable_Value
570 The_Array : Array_Element_Id;
571 The_Attribute : Variable_Value := Nil_Variable_Value;
574 if In_Package /= No_Package then
576 -- First, look if there is an array element that fits
580 (Name => Attribute_Or_Array_Name,
581 In_Arrays => In_Tree.Packages.Table (In_Package).Decl.Arrays,
587 In_Array => The_Array,
589 Force_Lower_Case_Index => Force_Lower_Case_Index);
591 -- If there is no array element, look for a variable
593 if The_Attribute = Nil_Variable_Value then
596 (Variable_Name => Attribute_Or_Array_Name,
597 In_Variables => In_Tree.Packages.Table
598 (In_Package).Decl.Attributes,
603 return The_Attribute;
609 In_Arrays : Array_Id;
610 In_Tree : Project_Tree_Ref) return Name_Id
613 The_Array : Array_Data;
616 Current := In_Arrays;
617 while Current /= No_Array loop
618 The_Array := In_Tree.Arrays.Table (Current);
619 if The_Array.Name = In_Array then
621 (Index, In_Array => The_Array.Value, In_Tree => In_Tree);
623 Current := The_Array.Next;
632 In_Arrays : Array_Id;
633 In_Tree : Project_Tree_Ref) return Array_Element_Id
636 The_Array : Array_Data;
639 Current := In_Arrays;
640 while Current /= No_Array loop
641 The_Array := In_Tree.Arrays.Table (Current);
643 if The_Array.Name = Name then
644 return The_Array.Value;
646 Current := The_Array.Next;
650 return No_Array_Element;
655 In_Packages : Package_Id;
656 In_Tree : Project_Tree_Ref) return Package_Id
658 Current : Package_Id;
659 The_Package : Package_Element;
662 Current := In_Packages;
663 while Current /= No_Package loop
664 The_Package := In_Tree.Packages.Table (Current);
665 exit when The_Package.Name /= No_Name
666 and then The_Package.Name = Name;
667 Current := The_Package.Next;
674 (Variable_Name : Name_Id;
675 In_Variables : Variable_Id;
676 In_Tree : Project_Tree_Ref) return Variable_Value
678 Current : Variable_Id;
679 The_Variable : Variable;
682 Current := In_Variables;
683 while Current /= No_Variable loop
685 In_Tree.Variable_Elements.Table (Current);
687 if Variable_Name = The_Variable.Name then
688 return The_Variable.Value;
690 Current := The_Variable.Next;
694 return Nil_Variable_Value;
703 Max_Length : Positive;
704 Separator : Character)
706 First : Positive := S'First;
707 Last : Natural := S'Last;
710 -- Nothing to do for empty strings
714 -- Start on a new line if current line is already longer than
717 if Positive (Column) >= Max_Length then
721 -- If length of remainder is longer than Max_Length, we need to
722 -- cut the remainder in several lines.
724 while Positive (Column) + S'Last - First > Max_Length loop
726 -- Try the maximum length possible
728 Last := First + Max_Length - Positive (Column);
730 -- Look for last Separator in the line
732 while Last >= First and then S (Last) /= Separator loop
736 -- If we do not find a separator, we output the maximum length
740 Last := First + Max_Length - Positive (Column);
743 Write_Line (S (First .. Last));
745 -- Set the beginning of the new remainder
750 -- What is left goes to the buffer, without EOL
752 Write_Str (S (First .. S'Last));