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 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) return File_Name_Type
114 pragma Assert (Project /= No_Project);
116 The_Packages : constant Package_Id :=
117 In_Tree.Projects.Table (Project).Decl.Packages;
119 Builder_Package : constant Prj.Package_Id :=
121 (Name => Name_Builder,
122 In_Packages => The_Packages,
125 Executable : Variable_Value :=
127 (Name => Name_Id (Main),
129 Attribute_Or_Array_Name => Name_Executable,
130 In_Package => Builder_Package,
133 Executable_Suffix : Variable_Value := Nil_Variable_Value;
135 Executable_Suffix_Name : Name_Id := No_Name;
137 Naming : constant Naming_Data := In_Tree.Projects.Table (Project).Naming;
139 Body_Suffix : constant String :=
140 Body_Suffix_Of (In_Tree, "ada", Naming);
142 Spec_Suffix : constant String :=
143 Spec_Suffix_Of (In_Tree, "ada", Naming);
146 if Builder_Package /= No_Package then
147 if Get_Mode = Multi_Language then
148 Executable_Suffix_Name :=
149 In_Tree.Projects.Table (Project).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 :=
288 In_Tree.Projects.Table (Project).Config.Executable_Suffix;
289 Result := Executable_Name (Name_Find);
290 Executable_Extension_On_Target := Saved_EEOT;
315 if File.Cursor = File.Buffer_Len then
319 A => File.Buffer'Address,
320 N => File.Buffer'Length);
322 if File.Buffer_Len = 0 then
323 File.End_Of_File_Reached := True;
330 File.Cursor := File.Cursor + 1;
334 -- Start of processing for Get_Line
338 Prj.Com.Fail ("Get_Line attempted on an invalid Text_File");
341 Last := Line'First - 1;
343 if not File.End_Of_File_Reached then
345 C := File.Buffer (File.Cursor);
346 exit when C = ASCII.CR or else C = ASCII.LF;
351 if File.End_Of_File_Reached then
355 exit when Last = Line'Last;
358 if C = ASCII.CR or else C = ASCII.LF then
361 if File.End_Of_File_Reached then
367 and then File.Buffer (File.Cursor) = ASCII.LF
378 function Is_Valid (File : Text_File) return Boolean is
387 procedure Open (File : out Text_File; Name : String) is
388 FD : File_Descriptor;
389 File_Name : String (1 .. Name'Length + 1);
392 File_Name (1 .. Name'Length) := Name;
393 File_Name (File_Name'Last) := ASCII.NUL;
394 FD := Open_Read (Name => File_Name'Address,
395 Fmode => GNAT.OS_Lib.Text);
397 if FD = Invalid_FD then
401 File := new Text_File_Data;
405 A => File.Buffer'Address,
406 N => File.Buffer'Length);
408 if File.Buffer_Len = 0 then
409 File.End_Of_File_Reached := True;
421 (Into_List : in out Name_List_Index;
422 From_List : String_List_Id;
423 In_Tree : Project_Tree_Ref)
425 Current_Name : Name_List_Index;
426 List : String_List_Id;
427 Element : String_Element;
428 Last : Name_List_Index :=
429 Name_List_Table.Last (In_Tree.Name_Lists);
432 Current_Name := Into_List;
433 while Current_Name /= No_Name_List and then
434 In_Tree.Name_Lists.Table (Current_Name).Next /= No_Name_List
436 Current_Name := In_Tree.Name_Lists.Table (Current_Name).Next;
440 while List /= Nil_String loop
441 Element := In_Tree.String_Elements.Table (List);
443 Name_List_Table.Append
445 (Name => Element.Value, Next => No_Name_List));
449 if Current_Name = No_Name_List then
453 In_Tree.Name_Lists.Table (Current_Name).Next := Last;
456 Current_Name := Last;
458 List := Element.Next;
467 (Variable : Variable_Value;
468 Default : String) return String
471 if Variable.Kind /= Single
472 or else Variable.Default
473 or else Variable.Value = No_Name
477 return Get_Name_String (Variable.Value);
483 In_Array : Array_Element_Id;
484 In_Tree : Project_Tree_Ref) return Name_Id
486 Current : Array_Element_Id;
487 Element : Array_Element;
488 Real_Index : Name_Id := Index;
493 if Current = No_Array_Element then
497 Element := In_Tree.Array_Elements.Table (Current);
499 if not Element.Index_Case_Sensitive then
500 Get_Name_String (Index);
501 To_Lower (Name_Buffer (1 .. Name_Len));
502 Real_Index := Name_Find;
505 while Current /= No_Array_Element loop
506 Element := In_Tree.Array_Elements.Table (Current);
508 if Real_Index = Element.Index then
509 exit when Element.Value.Kind /= Single;
510 exit when Element.Value.Value = Empty_String;
511 return Element.Value.Value;
513 Current := Element.Next;
522 Src_Index : Int := 0;
523 In_Array : Array_Element_Id;
524 In_Tree : Project_Tree_Ref;
525 Force_Lower_Case_Index : Boolean := False) return Variable_Value
527 Current : Array_Element_Id;
528 Element : Array_Element;
529 Real_Index_1 : Name_Id;
530 Real_Index_2 : Name_Id;
535 if Current = No_Array_Element then
536 return Nil_Variable_Value;
539 Element := In_Tree.Array_Elements.Table (Current);
541 Real_Index_1 := Index;
543 if not Element.Index_Case_Sensitive or Force_Lower_Case_Index then
544 Get_Name_String (Index);
545 To_Lower (Name_Buffer (1 .. Name_Len));
546 Real_Index_1 := Name_Find;
549 while Current /= No_Array_Element loop
550 Element := In_Tree.Array_Elements.Table (Current);
551 Real_Index_2 := Element.Index;
553 if not Element.Index_Case_Sensitive or Force_Lower_Case_Index then
554 Get_Name_String (Element.Index);
555 To_Lower (Name_Buffer (1 .. Name_Len));
556 Real_Index_2 := Name_Find;
559 if Real_Index_1 = Real_Index_2 and then
560 Src_Index = Element.Src_Index
562 return Element.Value;
564 Current := Element.Next;
568 return Nil_Variable_Value;
574 Attribute_Or_Array_Name : Name_Id;
575 In_Package : Package_Id;
576 In_Tree : Project_Tree_Ref;
577 Force_Lower_Case_Index : Boolean := False) return Variable_Value
579 The_Array : Array_Element_Id;
580 The_Attribute : Variable_Value := Nil_Variable_Value;
583 if In_Package /= No_Package then
585 -- First, look if there is an array element that fits
589 (Name => Attribute_Or_Array_Name,
590 In_Arrays => In_Tree.Packages.Table (In_Package).Decl.Arrays,
596 In_Array => The_Array,
598 Force_Lower_Case_Index => Force_Lower_Case_Index);
600 -- If there is no array element, look for a variable
602 if The_Attribute = Nil_Variable_Value then
605 (Variable_Name => Attribute_Or_Array_Name,
606 In_Variables => In_Tree.Packages.Table
607 (In_Package).Decl.Attributes,
612 return The_Attribute;
618 In_Arrays : Array_Id;
619 In_Tree : Project_Tree_Ref) return Name_Id
622 The_Array : Array_Data;
625 Current := In_Arrays;
626 while Current /= No_Array loop
627 The_Array := In_Tree.Arrays.Table (Current);
628 if The_Array.Name = In_Array then
630 (Index, In_Array => The_Array.Value, In_Tree => In_Tree);
632 Current := The_Array.Next;
641 In_Arrays : Array_Id;
642 In_Tree : Project_Tree_Ref) return Array_Element_Id
645 The_Array : Array_Data;
648 Current := In_Arrays;
649 while Current /= No_Array loop
650 The_Array := In_Tree.Arrays.Table (Current);
652 if The_Array.Name = Name then
653 return The_Array.Value;
655 Current := The_Array.Next;
659 return No_Array_Element;
664 In_Packages : Package_Id;
665 In_Tree : Project_Tree_Ref) return Package_Id
667 Current : Package_Id;
668 The_Package : Package_Element;
671 Current := In_Packages;
672 while Current /= No_Package loop
673 The_Package := In_Tree.Packages.Table (Current);
674 exit when The_Package.Name /= No_Name
675 and then The_Package.Name = Name;
676 Current := The_Package.Next;
683 (Variable_Name : Name_Id;
684 In_Variables : Variable_Id;
685 In_Tree : Project_Tree_Ref) return Variable_Value
687 Current : Variable_Id;
688 The_Variable : Variable;
691 Current := In_Variables;
692 while Current /= No_Variable loop
694 In_Tree.Variable_Elements.Table (Current);
696 if Variable_Name = The_Variable.Name then
697 return The_Variable.Value;
699 Current := The_Variable.Next;
703 return Nil_Variable_Value;
712 Max_Length : Positive;
713 Separator : Character)
715 First : Positive := S'First;
716 Last : Natural := S'Last;
719 -- Nothing to do for empty strings
723 -- Start on a new line if current line is already longer than
726 if Positive (Column) >= Max_Length then
730 -- If length of remainder is longer than Max_Length, we need to
731 -- cut the remainder in several lines.
733 while Positive (Column) + S'Last - First > Max_Length loop
735 -- Try the maximum length possible
737 Last := First + Max_Length - Positive (Column);
739 -- Look for last Separator in the line
741 while Last >= First and then S (Last) /= Separator loop
745 -- If we do not find a separator, we output the maximum length
749 Last := First + Max_Length - Positive (Column);
752 Write_Line (S (First .. Last));
754 -- Set the beginning of the new remainder
759 -- What is left goes to the buffer, without EOL
761 Write_Str (S (First .. S'Last));