1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2010, 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.Unchecked_Deallocation;
28 with GNAT.Case_Util; use GNAT.Case_Util;
30 with Osint; use Osint;
31 with Output; use Output;
33 with Snames; use Snames;
34 with Targparm; use Targparm;
36 package body Prj.Util is
38 procedure Free is new Ada.Unchecked_Deallocation
39 (Text_File_Data, Text_File);
45 procedure Close (File : in out Text_File) is
48 Prj.Com.Fail ("Close attempted on an invalid Text_File");
51 -- Close file, no need to test status, since this is a file that we
52 -- read, and the file was read successfully before we closed it.
63 (This : in out Name_List_Index;
64 In_Tree : Project_Tree_Ref)
66 Old_Current : Name_List_Index;
67 New_Current : Name_List_Index;
70 if This /= No_Name_List then
72 Name_List_Table.Increment_Last (In_Tree.Name_Lists);
73 New_Current := Name_List_Table.Last (In_Tree.Name_Lists);
75 In_Tree.Name_Lists.Table (New_Current) :=
76 (In_Tree.Name_Lists.Table (Old_Current).Name, No_Name_List);
79 Old_Current := In_Tree.Name_Lists.Table (Old_Current).Next;
80 exit when Old_Current = No_Name_List;
81 In_Tree.Name_Lists.Table (New_Current).Next := New_Current + 1;
82 Name_List_Table.Increment_Last (In_Tree.Name_Lists);
83 New_Current := New_Current + 1;
84 In_Tree.Name_Lists.Table (New_Current) :=
85 (In_Tree.Name_Lists.Table (Old_Current).Name, No_Name_List);
94 function End_Of_File (File : Text_File) return Boolean is
97 Prj.Com.Fail ("End_Of_File attempted on an invalid Text_File");
100 return File.End_Of_File_Reached;
107 function Executable_Of
108 (Project : Project_Id;
109 In_Tree : Project_Tree_Ref;
110 Main : File_Name_Type;
112 Ada_Main : Boolean := True;
113 Language : String := "";
114 Include_Suffix : Boolean := True) return File_Name_Type
116 pragma Assert (Project /= No_Project);
118 The_Packages : constant Package_Id := 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,
136 Spec_Suffix : Name_Id := No_Name;
137 Body_Suffix : Name_Id := No_Name;
139 Spec_Suffix_Length : Natural := 0;
140 Body_Suffix_Length : Natural := 0;
142 procedure Get_Suffixes
143 (B_Suffix : File_Name_Type;
144 S_Suffix : File_Name_Type);
145 -- Get the non empty suffixes in variables Spec_Suffix and Body_Suffix
147 function Add_Suffix (File : File_Name_Type) return File_Name_Type;
148 -- Return the name of the executable, based on File, and adding the
149 -- executable suffix if needed
155 procedure Get_Suffixes
156 (B_Suffix : File_Name_Type;
157 S_Suffix : File_Name_Type)
160 if B_Suffix /= No_File then
161 Body_Suffix := Name_Id (B_Suffix);
162 Body_Suffix_Length := Natural (Length_Of_Name (Body_Suffix));
165 if S_Suffix /= No_File then
166 Spec_Suffix := Name_Id (S_Suffix);
167 Spec_Suffix_Length := Natural (Length_Of_Name (Spec_Suffix));
175 function Add_Suffix (File : File_Name_Type) return File_Name_Type is
176 Saved_EEOT : constant Name_Id := Executable_Extension_On_Target;
177 Result : File_Name_Type;
178 Suffix_From_Project : Variable_Value;
180 if Include_Suffix then
181 if Project.Config.Executable_Suffix /= No_Name then
182 Executable_Extension_On_Target :=
183 Project.Config.Executable_Suffix;
186 Result := Executable_Name (File);
187 Executable_Extension_On_Target := Saved_EEOT;
191 -- We still want to take into account cases where the suffix is
192 -- specified in the project itself, as opposed to the config file.
193 -- Unfortunately, when the project was processed, they are both
194 -- stored in Project.Config, so we need to get it from the project
197 Suffix_From_Project :=
199 (Variable_Name => Name_Executable_Suffix,
201 In_Tree.Packages.Table (Builder_Package).Decl.Attributes,
204 if Suffix_From_Project /= Nil_Variable_Value
205 and then Suffix_From_Project.Value /= No_Name
207 Executable_Extension_On_Target := Suffix_From_Project.Value;
208 Result := Executable_Name (File);
209 Executable_Extension_On_Target := Saved_EEOT;
218 -- Start of processing for Executable_Of
222 Lang := Get_Language_From_Name (Project, "ada");
223 elsif Language /= "" then
224 Lang := Get_Language_From_Name (Project, Language);
229 (B_Suffix => Lang.Config.Naming_Data.Body_Suffix,
230 S_Suffix => Lang.Config.Naming_Data.Spec_Suffix);
233 if Builder_Package /= No_Package then
234 if Executable = Nil_Variable_Value and then Ada_Main then
235 Get_Name_String (Main);
237 -- Try as index the name minus the implementation suffix or minus
238 -- the specification suffix.
241 Name : constant String (1 .. Name_Len) :=
242 Name_Buffer (1 .. Name_Len);
243 Last : Positive := Name_Len;
245 Truncated : Boolean := False;
248 if Body_Suffix /= No_Name
249 and then Last > Natural (Length_Of_Name (Body_Suffix))
250 and then Name (Last - Body_Suffix_Length + 1 .. Last) =
251 Get_Name_String (Body_Suffix)
254 Last := Last - Body_Suffix_Length;
257 if Spec_Suffix /= No_Name
258 and then not Truncated
259 and then Last > Spec_Suffix_Length
260 and then Name (Last - Spec_Suffix_Length + 1 .. Last) =
261 Get_Name_String (Spec_Suffix)
264 Last := Last - Spec_Suffix_Length;
269 Name_Buffer (1 .. Name_Len) := Name (1 .. Last);
274 Attribute_Or_Array_Name => Name_Executable,
275 In_Package => Builder_Package,
281 -- If we have found an Executable attribute, return its value,
282 -- possibly suffixed by the executable suffix.
284 if Executable /= Nil_Variable_Value
285 and then Executable.Value /= No_Name
286 and then Length_Of_Name (Executable.Value) /= 0
288 return Add_Suffix (File_Name_Type (Executable.Value));
292 Get_Name_String (Main);
294 -- If there is a body suffix or a spec suffix, remove this suffix,
295 -- otherwise remove any suffix ('.' followed by other characters), if
298 if Body_Suffix /= No_Name
299 and then Name_Len > Body_Suffix_Length
300 and then Name_Buffer (Name_Len - Body_Suffix_Length + 1 .. Name_Len) =
301 Get_Name_String (Body_Suffix)
303 -- Found the body termination, remove it
305 Name_Len := Name_Len - Body_Suffix_Length;
307 elsif Spec_Suffix /= No_Name
308 and then Name_Len > Spec_Suffix_Length
310 Name_Buffer (Name_Len - Spec_Suffix_Length + 1 .. Name_Len) =
311 Get_Name_String (Spec_Suffix)
313 -- Found the spec termination, remove it
315 Name_Len := Name_Len - Spec_Suffix_Length;
318 -- Remove any suffix, if there is one
320 Get_Name_String (Strip_Suffix (Main));
323 return Add_Suffix (Name_Find);
345 if File.Cursor = File.Buffer_Len then
349 A => File.Buffer'Address,
350 N => File.Buffer'Length);
352 if File.Buffer_Len = 0 then
353 File.End_Of_File_Reached := True;
360 File.Cursor := File.Cursor + 1;
364 -- Start of processing for Get_Line
368 Prj.Com.Fail ("Get_Line attempted on an invalid Text_File");
371 Last := Line'First - 1;
373 if not File.End_Of_File_Reached then
375 C := File.Buffer (File.Cursor);
376 exit when C = ASCII.CR or else C = ASCII.LF;
381 if File.End_Of_File_Reached then
385 exit when Last = Line'Last;
388 if C = ASCII.CR or else C = ASCII.LF then
391 if File.End_Of_File_Reached then
397 and then File.Buffer (File.Cursor) = ASCII.LF
408 function Is_Valid (File : Text_File) return Boolean is
417 procedure Open (File : out Text_File; Name : String) is
418 FD : File_Descriptor;
419 File_Name : String (1 .. Name'Length + 1);
422 File_Name (1 .. Name'Length) := Name;
423 File_Name (File_Name'Last) := ASCII.NUL;
424 FD := Open_Read (Name => File_Name'Address,
425 Fmode => GNAT.OS_Lib.Text);
427 if FD = Invalid_FD then
431 File := new Text_File_Data;
435 A => File.Buffer'Address,
436 N => File.Buffer'Length);
438 if File.Buffer_Len = 0 then
439 File.End_Of_File_Reached := True;
451 (Into_List : in out Name_List_Index;
452 From_List : String_List_Id;
453 In_Tree : Project_Tree_Ref;
454 Lower_Case : Boolean := False)
456 Current_Name : Name_List_Index;
457 List : String_List_Id;
458 Element : String_Element;
459 Last : Name_List_Index :=
460 Name_List_Table.Last (In_Tree.Name_Lists);
464 Current_Name := Into_List;
465 while Current_Name /= No_Name_List
466 and then In_Tree.Name_Lists.Table (Current_Name).Next /= No_Name_List
468 Current_Name := In_Tree.Name_Lists.Table (Current_Name).Next;
472 while List /= Nil_String loop
473 Element := In_Tree.String_Elements.Table (List);
474 Value := Element.Value;
477 Get_Name_String (Value);
478 To_Lower (Name_Buffer (1 .. Name_Len));
482 Name_List_Table.Append
483 (In_Tree.Name_Lists, (Name => Value, Next => No_Name_List));
487 if Current_Name = No_Name_List then
491 In_Tree.Name_Lists.Table (Current_Name).Next := Last;
494 Current_Name := Last;
496 List := Element.Next;
505 (Variable : Variable_Value;
506 Default : String) return String
509 if Variable.Kind /= Single
510 or else Variable.Default
511 or else Variable.Value = No_Name
515 return Get_Name_String (Variable.Value);
521 In_Array : Array_Element_Id;
522 In_Tree : Project_Tree_Ref) return Name_Id
524 Current : Array_Element_Id;
525 Element : Array_Element;
526 Real_Index : Name_Id := Index;
531 if Current = No_Array_Element then
535 Element := In_Tree.Array_Elements.Table (Current);
537 if not Element.Index_Case_Sensitive then
538 Get_Name_String (Index);
539 To_Lower (Name_Buffer (1 .. Name_Len));
540 Real_Index := Name_Find;
543 while Current /= No_Array_Element loop
544 Element := In_Tree.Array_Elements.Table (Current);
546 if Real_Index = Element.Index then
547 exit when Element.Value.Kind /= Single;
548 exit when Element.Value.Value = Empty_String;
549 return Element.Value.Value;
551 Current := Element.Next;
560 Src_Index : Int := 0;
561 In_Array : Array_Element_Id;
562 In_Tree : Project_Tree_Ref;
563 Force_Lower_Case_Index : Boolean := False) return Variable_Value
565 Current : Array_Element_Id;
566 Element : Array_Element;
567 Real_Index_1 : Name_Id;
568 Real_Index_2 : Name_Id;
573 if Current = No_Array_Element then
574 return Nil_Variable_Value;
577 Element := In_Tree.Array_Elements.Table (Current);
579 Real_Index_1 := Index;
581 if not Element.Index_Case_Sensitive or else Force_Lower_Case_Index then
582 if Index /= All_Other_Names then
583 Get_Name_String (Index);
584 To_Lower (Name_Buffer (1 .. Name_Len));
585 Real_Index_1 := Name_Find;
589 while Current /= No_Array_Element loop
590 Element := In_Tree.Array_Elements.Table (Current);
591 Real_Index_2 := Element.Index;
593 if not Element.Index_Case_Sensitive
594 or else Force_Lower_Case_Index
596 if Element.Index /= All_Other_Names then
597 Get_Name_String (Element.Index);
598 To_Lower (Name_Buffer (1 .. Name_Len));
599 Real_Index_2 := Name_Find;
603 if Real_Index_1 = Real_Index_2 and then
604 Src_Index = Element.Src_Index
606 return Element.Value;
608 Current := Element.Next;
612 return Nil_Variable_Value;
618 Attribute_Or_Array_Name : Name_Id;
619 In_Package : Package_Id;
620 In_Tree : Project_Tree_Ref;
621 Force_Lower_Case_Index : Boolean := False) return Variable_Value
623 The_Array : Array_Element_Id;
624 The_Attribute : Variable_Value := Nil_Variable_Value;
627 if In_Package /= No_Package then
629 -- First, look if there is an array element that fits
633 (Name => Attribute_Or_Array_Name,
634 In_Arrays => In_Tree.Packages.Table (In_Package).Decl.Arrays,
640 In_Array => The_Array,
642 Force_Lower_Case_Index => Force_Lower_Case_Index);
644 -- If there is no array element, look for a variable
646 if The_Attribute = Nil_Variable_Value then
649 (Variable_Name => Attribute_Or_Array_Name,
650 In_Variables => In_Tree.Packages.Table
651 (In_Package).Decl.Attributes,
656 return The_Attribute;
662 In_Arrays : Array_Id;
663 In_Tree : Project_Tree_Ref) return Name_Id
666 The_Array : Array_Data;
669 Current := In_Arrays;
670 while Current /= No_Array loop
671 The_Array := In_Tree.Arrays.Table (Current);
672 if The_Array.Name = In_Array then
674 (Index, In_Array => The_Array.Value, In_Tree => In_Tree);
676 Current := The_Array.Next;
685 In_Arrays : Array_Id;
686 In_Tree : Project_Tree_Ref) return Array_Element_Id
689 The_Array : Array_Data;
692 Current := In_Arrays;
693 while Current /= No_Array loop
694 The_Array := In_Tree.Arrays.Table (Current);
696 if The_Array.Name = Name then
697 return The_Array.Value;
699 Current := The_Array.Next;
703 return No_Array_Element;
708 In_Packages : Package_Id;
709 In_Tree : Project_Tree_Ref) return Package_Id
711 Current : Package_Id;
712 The_Package : Package_Element;
715 Current := In_Packages;
716 while Current /= No_Package loop
717 The_Package := In_Tree.Packages.Table (Current);
718 exit when The_Package.Name /= No_Name
719 and then The_Package.Name = Name;
720 Current := The_Package.Next;
727 (Variable_Name : Name_Id;
728 In_Variables : Variable_Id;
729 In_Tree : Project_Tree_Ref) return Variable_Value
731 Current : Variable_Id;
732 The_Variable : Variable;
735 Current := In_Variables;
736 while Current /= No_Variable loop
738 In_Tree.Variable_Elements.Table (Current);
740 if Variable_Name = The_Variable.Name then
741 return The_Variable.Value;
743 Current := The_Variable.Next;
747 return Nil_Variable_Value;
756 Max_Length : Positive;
757 Separator : Character)
759 First : Positive := S'First;
760 Last : Natural := S'Last;
763 -- Nothing to do for empty strings
767 -- Start on a new line if current line is already longer than
770 if Positive (Column) >= Max_Length then
774 -- If length of remainder is longer than Max_Length, we need to
775 -- cut the remainder in several lines.
777 while Positive (Column) + S'Last - First > Max_Length loop
779 -- Try the maximum length possible
781 Last := First + Max_Length - Positive (Column);
783 -- Look for last Separator in the line
785 while Last >= First and then S (Last) /= Separator loop
789 -- If we do not find a separator, we output the maximum length
793 Last := First + Max_Length - Positive (Column);
796 Write_Line (S (First .. Last));
798 -- Set the beginning of the new remainder
803 -- What is left goes to the buffer, without EOL
805 Write_Str (S (First .. S'Last));