1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2006, 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 ------------------------------------------------------------------------------
28 with Namet
; use Namet
;
31 with Osint
; use Osint
;
36 with Prj
.Tree
; use Prj
.Tree
;
37 with Prj
.Util
; use Prj
.Util
;
38 with Snames
; use Snames
;
39 with Table
; use Table
;
41 with Ada
.Characters
.Handling
; use Ada
.Characters
.Handling
;
42 with GNAT
.Directory_Operations
; use GNAT
.Directory_Operations
;
43 with GNAT
.Regexp
; use GNAT
.Regexp
;
45 with System
.Case_Util
; use System
.Case_Util
;
48 package body Prj
.Makr
is
50 function Dup
(Fd
: File_Descriptor
) return File_Descriptor
;
52 procedure Dup2
(Old_Fd
, New_Fd
: File_Descriptor
);
54 Gcc
: constant String := "gcc";
55 Gcc_Path
: String_Access
:= null;
57 Non_Empty_Node
: constant Project_Node_Id
:= 1;
58 -- Used for the With_Clause of the naming project
60 type Matched_Type
is (True, False, Excluded
);
62 Naming_File_Suffix
: constant String := "_naming";
63 Source_List_File_Suffix
: constant String := "_source_list.txt";
65 Output_FD
: File_Descriptor
;
66 -- To save the project file and its naming project file
69 -- Output an empty line
71 procedure Write_A_Char
(C
: Character);
72 -- Write one character to Output_FD
74 procedure Write_A_String
(S
: String);
75 -- Write a String to Output_FD
77 package Processed_Directories
is new Table
.Table
78 (Table_Component_Type
=> String_Access
,
79 Table_Index_Type
=> Natural,
82 Table_Increment
=> 10,
83 Table_Name
=> "Prj.Makr.Processed_Directories");
89 function Dup
(Fd
: File_Descriptor
) return File_Descriptor
is
91 return File_Descriptor
(System
.CRTL
.dup
(Integer (Fd
)));
98 procedure Dup2
(Old_Fd
, New_Fd
: File_Descriptor
) is
100 pragma Warnings
(Off
, Fd
);
102 Fd
:= System
.CRTL
.dup2
(Integer (Old_Fd
), Integer (New_Fd
));
111 Project_File
: Boolean;
112 Directories
: Argument_List
;
113 Name_Patterns
: Argument_List
;
114 Excluded_Patterns
: Argument_List
;
115 Foreign_Patterns
: Argument_List
;
116 Preproc_Switches
: Argument_List
;
117 Very_Verbose
: Boolean)
119 Tree
: constant Project_Node_Tree_Ref
:= new Project_Node_Tree_Data
;
121 Path_Name
: String (1 .. File_Path
'Length +
122 Project_File_Extension
'Length);
123 Path_Last
: Natural := File_Path
'Length;
125 Directory_Last
: Natural := 0;
127 Output_Name
: String (Path_Name
'Range);
128 Output_Name_Last
: Natural;
129 Output_Name_Id
: Name_Id
;
131 Project_Node
: Project_Node_Id
:= Empty_Node
;
132 Project_Declaration
: Project_Node_Id
:= Empty_Node
;
133 Source_Dirs_List
: Project_Node_Id
:= Empty_Node
;
134 Current_Source_Dir
: Project_Node_Id
:= Empty_Node
;
136 Project_Naming_Node
: Project_Node_Id
:= Empty_Node
;
137 Project_Naming_Decl
: Project_Node_Id
:= Empty_Node
;
138 Naming_Package
: Project_Node_Id
:= Empty_Node
;
139 Naming_Package_Comments
: Project_Node_Id
:= Empty_Node
;
141 Source_Files_Comments
: Project_Node_Id
:= Empty_Node
;
142 Source_Dirs_Comments
: Project_Node_Id
:= Empty_Node
;
143 Source_List_File_Comments
: Project_Node_Id
:= Empty_Node
;
145 Project_Naming_File_Name
: String (1 .. Output_Name
'Length +
146 Naming_File_Suffix
'Length);
148 Project_Naming_Last
: Natural;
149 Project_Naming_Id
: Name_Id
:= No_Name
;
151 Excluded_Expressions
: array (Excluded_Patterns
'Range) of Regexp
;
152 Regular_Expressions
: array (Name_Patterns
'Range) of Regexp
;
153 Foreign_Expressions
: array (Foreign_Patterns
'Range) of Regexp
;
155 Source_List_Path
: String (1 .. Output_Name
'Length +
156 Source_List_File_Suffix
'Length);
157 Source_List_Last
: Natural;
159 Source_List_FD
: File_Descriptor
;
161 Args
: Argument_List
(1 .. Preproc_Switches
'Length + 6);
163 type SFN_Pragma
is record
170 package SFN_Pragmas
is new Table
.Table
171 (Table_Component_Type
=> SFN_Pragma
,
172 Table_Index_Type
=> Natural,
173 Table_Low_Bound
=> 0,
175 Table_Increment
=> 50,
176 Table_Name
=> "Prj.Makr.SFN_Pragmas");
178 procedure Process_Directory
(Dir_Name
: String; Recursively
: Boolean);
179 -- Look for Ada and foreign sources in a directory, according to the
180 -- patterns. When Recursively is True, after looking for sources in
181 -- Dir_Name, look also in its subdirectories, if any.
183 -----------------------
184 -- Process_Directory --
185 -----------------------
187 procedure Process_Directory
(Dir_Name
: String; Recursively
: Boolean) is
188 Matched
: Matched_Type
:= False;
189 Str
: String (1 .. 2_000
);
190 Canon
: String (1 .. 2_000
);
193 Process
: Boolean := True;
195 Temp_File_Name
: String_Access
:= null;
196 Save_Last_Pragma_Index
: Natural := 0;
197 File_Name_Id
: Name_Id
:= No_Name
;
198 SFN_Prag
: SFN_Pragma
;
201 -- Avoid processing the same directory more than once
203 for Index
in 1 .. Processed_Directories
.Last
loop
204 if Processed_Directories
.Table
(Index
).all = Dir_Name
then
211 if Opt
.Verbose_Mode
then
212 Output
.Write_Str
("Processing directory """);
213 Output
.Write_Str
(Dir_Name
);
214 Output
.Write_Line
("""");
217 Processed_Directories
. Increment_Last
;
218 Processed_Directories
.Table
(Processed_Directories
.Last
) :=
219 new String'(Dir_Name);
221 -- Get the source file names from the directory. Fails if the
222 -- directory does not exist.
225 Open (Dir, Dir_Name);
227 when Directory_Error =>
228 Prj.Com.Fail ("cannot open directory """, Dir_Name, """");
231 -- Process each regular file in the directory
234 Read (Dir, Str, Last);
235 exit File_Loop when Last = 0;
237 -- Copy the file name and put it in canonical case to match
238 -- against the patterns that have themselves already been put
239 -- in canonical case.
241 Canon (1 .. Last) := Str (1 .. Last);
242 Canonical_Case_File_Name (Canon (1 .. Last));
245 (Dir_Name & Directory_Separator & Str (1 .. Last))
250 Name_Buffer (1 .. Name_Len) := Str (1 .. Last);
251 File_Name_Id := Name_Find;
253 -- First, check if the file name matches at least one of
254 -- the excluded expressions;
256 for Index in Excluded_Expressions'Range loop
258 Match (Canon (1 .. Last), Excluded_Expressions (Index))
265 -- If it does not match any of the excluded expressions,
266 -- check if the file name matches at least one of the
267 -- regular expressions.
269 if Matched = True then
272 for Index in Regular_Expressions'Range loop
275 (Canon (1 .. Last), Regular_Expressions (Index))
284 or else (Matched = True and then Opt.Verbose_Mode)
286 Output.Write_Str (" Checking """);
287 Output.Write_Str (Str (1 .. Last));
288 Output.Write_Line (""": ");
291 -- If the file name matches one of the regular expressions,
292 -- parse it to get its unit name.
294 if Matched = True then
296 FD : File_Descriptor;
298 Saved_Output : File_Descriptor;
299 Saved_Error : File_Descriptor;
302 -- If we don't have the path of the compiler yet,
303 -- get it now. The compiler name may have a prefix,
304 -- so we get the potentially prefixed name.
306 if Gcc_Path = null then
308 Prefix_Gcc : String_Access :=
312 Locate_Exec_On_Path (Prefix_Gcc.all);
316 if Gcc_Path = null then
317 Prj.Com.Fail ("could not locate " & Gcc);
321 -- If we don't have yet the file name of the
322 -- temporary file, get it now.
324 if Temp_File_Name = null then
325 Create_Temp_File (FD, Temp_File_Name);
327 if FD = Invalid_FD then
329 ("could not create temporary file");
333 Delete_File (Temp_File_Name.all, Success);
336 Args (Args'Last) := new String'
338 Directory_Separator
&
341 -- Create the temporary file
343 FD
:= Create_Output_Text_File
344 (Name
=> Temp_File_Name
.all);
346 if FD
= Invalid_FD
then
348 ("could not create temporary file");
351 -- Save the standard output and error
353 Saved_Output
:= Dup
(Standout
);
354 Saved_Error
:= Dup
(Standerr
);
356 -- Set standard output and error to the temporary file
361 -- And spawn the compiler
363 Spawn
(Gcc_Path
.all, Args
, Success
);
365 -- Restore the standard output and error
367 Dup2
(Saved_Output
, Standout
);
368 Dup2
(Saved_Error
, Standerr
);
370 -- Close the temporary file
374 -- And close the saved standard output and error to
375 -- avoid too many file descriptors.
377 Close
(Saved_Output
);
380 -- Now that standard output is restored, check if
381 -- the compiler ran correctly.
383 -- Read the lines of the temporary file:
384 -- they should contain the kind and name of the unit.
388 Text_Line
: String (1 .. 1_000
);
392 Open
(File
, Temp_File_Name
.all);
394 if not Is_Valid
(File
) then
396 ("could not read temporary file");
399 Save_Last_Pragma_Index
:= SFN_Pragmas
.Last
;
401 if End_Of_File
(File
) then
402 if Opt
.Verbose_Mode
then
404 Output
.Write_Str
(" (process died) ");
409 Line_Loop
: while not End_Of_File
(File
) loop
410 Get_Line
(File
, Text_Line
, Text_Last
);
412 -- Find the first closing parenthesis
414 Char_Loop
: for J
in 1 .. Text_Last
loop
415 if Text_Line
(J
) = ')' then
417 Text_Line
(1 .. 4) = "Unit"
419 -- Add entry to SFN_Pragmas table
422 Name_Buffer
(1 .. Name_Len
) :=
423 Text_Line
(6 .. J
- 7);
426 File
=> File_Name_Id
,
428 Spec
=> Text_Line
(J
- 5 .. J
) =
431 SFN_Pragmas
.Increment_Last
;
433 (SFN_Pragmas
.Last
) := SFN_Prag
;
441 if Save_Last_Pragma_Index
= SFN_Pragmas
.Last
then
442 if Opt
.Verbose_Mode
then
443 Output
.Write_Line
(" not a unit");
447 if SFN_Pragmas
.Last
>
448 Save_Last_Pragma_Index
+ 1
450 for Index
in Save_Last_Pragma_Index
+ 1 ..
453 SFN_Pragmas
.Table
(Index
).Index
:=
454 Int
(Index
- Save_Last_Pragma_Index
);
458 for Index
in Save_Last_Pragma_Index
+ 1 ..
461 SFN_Prag
:= SFN_Pragmas
.Table
(Index
);
463 if Opt
.Verbose_Mode
then
464 if SFN_Prag
.Spec
then
465 Output
.Write_Str
(" spec of ");
468 Output
.Write_Str
(" body of ");
472 (Get_Name_String
(SFN_Prag
.Unit
));
477 -- Add the corresponding attribute in the
478 -- Naming package of the naming project.
481 Decl_Item
: constant Project_Node_Id
:=
487 Attribute
: constant Project_Node_Id
:=
490 N_Attribute_Declaration
,
493 Expression
: constant Project_Node_Id
:=
495 (Of_Kind
=> N_Expression
,
496 And_Expr_Kind
=> Single
,
499 Term
: constant Project_Node_Id
:=
502 And_Expr_Kind
=> Single
,
505 Value
: constant Project_Node_Id
:=
507 (Of_Kind
=> N_Literal_String
,
508 And_Expr_Kind
=> Single
,
512 Set_Next_Declarative_Item
514 To
=> First_Declarative_Item_Of
515 (Naming_Package
, Tree
),
517 Set_First_Declarative_Item_Of
521 Set_Current_Item_Node
526 -- Is it a spec or a body?
528 if SFN_Prag
.Spec
then
538 -- Get the name of the unit
540 Get_Name_String
(SFN_Prag
.Unit
);
541 To_Lower
(Name_Buffer
(1 .. Name_Len
));
542 Set_Associative_Array_Index_Of
543 (Attribute
, Tree
, To
=> Name_Find
);
546 (Attribute
, Tree
, To
=> Expression
);
548 (Expression
, Tree
, To
=> Term
);
550 (Term
, Tree
, To
=> Value
);
552 -- And set the name of the file
555 (Value
, Tree
, To
=> File_Name_Id
);
557 (Value
, Tree
, To
=> SFN_Prag
.Index
);
563 -- Add source file name to source list
567 Str
(Last
) := ASCII
.LF
;
569 if Write
(Source_List_FD
,
573 Prj
.Com
.Fail
("disk full");
580 Delete_File
(Temp_File_Name
.all, Success
);
584 -- File name matches none of the regular expressions
587 -- If file is not excluded, see if this is foreign source
589 if Matched
/= Excluded
then
590 for Index
in Foreign_Expressions
'Range loop
591 if Match
(Canon
(1 .. Last
),
592 Foreign_Expressions
(Index
))
603 Output
.Write_Line
("no match");
606 Output
.Write_Line
("excluded");
609 Output
.Write_Line
("foreign source");
613 if Project_File
and Matched
= True then
615 -- Add source file name to source list file
618 Str
(Last
) := ASCII
.LF
;
620 if Write
(Source_List_FD
,
624 Prj
.Com
.Fail
("disk full");
634 -- If Recursively is True, call itself for each subdirectory.
635 -- We do that, even when this directory has already been processed,
636 -- because all of its subdirectories may not have been processed.
639 Open
(Dir
, Dir_Name
);
642 Read
(Dir
, Str
, Last
);
645 -- Do not call itself for "." or ".."
648 (Dir_Name
& Directory_Separator
& Str
(1 .. Last
))
649 and then Str
(1 .. Last
) /= "."
650 and then Str
(1 .. Last
) /= ".."
653 (Dir_Name
& Directory_Separator
& Str
(1 .. Last
),
654 Recursively
=> True);
660 end Process_Directory
;
662 -- Start of processing for Make
665 -- Do some needed initializations
670 Prj
.Initialize
(No_Project_Tree
);
671 Prj
.Tree
.Initialize
(Tree
);
673 SFN_Pragmas
.Set_Last
(0);
675 Processed_Directories
.Set_Last
(0);
677 -- Initialize the compiler switches
679 Args
(1) := new String'("-c");
680 Args (2) := new String'("-gnats");
681 Args
(3) := new String'("-gnatu");
682 Args (4 .. 3 + Preproc_Switches'Length) := Preproc_Switches;
683 Args (4 + Preproc_Switches'Length) := new String'("-x");
684 Args
(5 + Preproc_Switches
'Length) := new String'("ada");
686 -- Get the path and file names
688 if File_Names_Case_Sensitive then
689 Path_Name (1 .. Path_Last) := File_Path;
691 Path_Name (1 .. Path_Last) := To_Lower (File_Path);
694 Path_Name (Path_Last + 1 .. Path_Name'Last) :=
695 Project_File_Extension;
697 -- Get the end of directory information, if any
699 for Index in reverse 1 .. Path_Last loop
700 if Path_Name (Index) = Directory_Separator then
701 Directory_Last := Index;
707 if Path_Last < Project_File_Extension'Length + 1
709 (Path_Last - Project_File_Extension'Length + 1 .. Path_Last)
710 /= Project_File_Extension
712 Path_Last := Path_Name'Last;
715 Output_Name (1 .. Path_Last) := To_Lower (Path_Name (1 .. Path_Last));
716 Output_Name_Last := Path_Last - Project_File_Extension'Length;
718 -- If there is already a project file with the specified name, parse
719 -- it to get the components that are not automatically generated.
721 if Is_Regular_File (Output_Name (1 .. Path_Last)) then
722 if Opt.Verbose_Mode then
723 Output.Write_Str ("Parsing already existing project file """);
724 Output.Write_Str (Output_Name (1 .. Output_Name_Last));
725 Output.Write_Line ("""");
730 Project => Project_Node,
731 Project_File_Name => Output_Name (1 .. Output_Name_Last),
732 Always_Errout_Finalize => False,
733 Store_Comments => True);
735 -- Fail if parsing was not successful
737 if Project_Node = Empty_Node then
738 Fail ("parsing of existing project file failed");
741 -- If parsing was successful, remove the components that are
742 -- automatically generated, if any, so that they will be
743 -- unconditionally added later.
745 -- Remove the with clause for the naming project file
748 With_Clause : Project_Node_Id :=
749 First_With_Clause_Of (Project_Node, Tree);
750 Previous : Project_Node_Id := Empty_Node;
753 while With_Clause /= Empty_Node loop
754 if Prj.Tree.Name_Of (With_Clause, Tree) =
757 if Previous = Empty_Node then
758 Set_First_With_Clause_Of
760 To => Next_With_Clause_Of (With_Clause, Tree));
762 Set_Next_With_Clause_Of
764 To => Next_With_Clause_Of (With_Clause, Tree));
770 Previous := With_Clause;
771 With_Clause := Next_With_Clause_Of (With_Clause, Tree);
775 -- Remove attribute declarations of Source_Files,
776 -- Source_List_File, Source_Dirs, and the declaration of
777 -- package Naming, if they exist, but preserve the comments
778 -- attached to these nodes.
781 Declaration : Project_Node_Id :=
782 First_Declarative_Item_Of
783 (Project_Declaration_Of
784 (Project_Node, Tree),
786 Previous : Project_Node_Id := Empty_Node;
787 Current_Node : Project_Node_Id := Empty_Node;
790 Kind_Of_Node : Project_Node_Kind;
791 Comments : Project_Node_Id;
794 while Declaration /= Empty_Node loop
795 Current_Node := Current_Item_Node (Declaration, Tree);
797 Kind_Of_Node := Kind_Of (Current_Node, Tree);
799 if Kind_Of_Node = N_Attribute_Declaration or else
800 Kind_Of_Node = N_Package_Declaration
802 Name := Prj.Tree.Name_Of (Current_Node, Tree);
804 if Name = Name_Source_Files or else
805 Name = Name_Source_List_File or else
806 Name = Name_Source_Dirs or else
810 Tree.Project_Nodes.Table (Current_Node).Comments;
812 if Name = Name_Source_Files then
813 Source_Files_Comments := Comments;
815 elsif Name = Name_Source_List_File then
816 Source_List_File_Comments := Comments;
818 elsif Name = Name_Source_Dirs then
819 Source_Dirs_Comments := Comments;
821 elsif Name = Name_Naming then
822 Naming_Package_Comments := Comments;
825 if Previous = Empty_Node then
826 Set_First_Declarative_Item_Of
827 (Project_Declaration_Of (Project_Node, Tree),
829 To => Next_Declarative_Item
830 (Declaration, Tree));
833 Set_Next_Declarative_Item
835 To => Next_Declarative_Item
836 (Declaration, Tree));
840 Previous := Declaration;
844 Declaration := Next_Declarative_Item (Declaration, Tree);
850 if Directory_Last /= 0 then
851 Output_Name (1 .. Output_Name_Last - Directory_Last) :=
852 Output_Name (Directory_Last + 1 .. Output_Name_Last);
853 Output_Name_Last := Output_Name_Last - Directory_Last;
856 -- Get the project name id
858 Name_Len := Output_Name_Last;
859 Name_Buffer (1 .. Name_Len) := Output_Name (1 .. Name_Len);
860 Output_Name_Id := Name_Find;
862 -- Create the project naming file name
864 Project_Naming_Last := Output_Name_Last;
865 Project_Naming_File_Name (1 .. Project_Naming_Last) :=
866 Output_Name (1 .. Project_Naming_Last);
867 Project_Naming_File_Name
868 (Project_Naming_Last + 1 ..
869 Project_Naming_Last + Naming_File_Suffix'Length) :=
871 Project_Naming_Last :=
872 Project_Naming_Last + Naming_File_Suffix'Length;
874 -- Get the project naming id
876 Name_Len := Project_Naming_Last;
877 Name_Buffer (1 .. Name_Len) :=
878 Project_Naming_File_Name (1 .. Name_Len);
879 Project_Naming_Id := Name_Find;
881 Project_Naming_File_Name
882 (Project_Naming_Last + 1 ..
883 Project_Naming_Last + Project_File_Extension'Length) :=
884 Project_File_Extension;
885 Project_Naming_Last :=
886 Project_Naming_Last + Project_File_Extension'Length;
888 -- Create the source list file name
890 Source_List_Last := Output_Name_Last;
891 Source_List_Path (1 .. Source_List_Last) :=
892 Output_Name (1 .. Source_List_Last);
894 (Source_List_Last + 1 ..
895 Source_List_Last + Source_List_File_Suffix'Length) :=
896 Source_List_File_Suffix;
897 Source_List_Last := Source_List_Last + Source_List_File_Suffix'Length;
899 -- Add the project file extension to the project name
902 (Output_Name_Last + 1 ..
903 Output_Name_Last + Project_File_Extension'Length) :=
904 Project_File_Extension;
905 Output_Name_Last := Output_Name_Last + Project_File_Extension'Length;
908 -- Change the current directory to the directory of the project file,
909 -- if any directory information is specified.
911 if Directory_Last /= 0 then
913 Change_Dir (Path_Name (1 .. Directory_Last));
915 when Directory_Error =>
917 ("unknown directory """,
918 Path_Name (1 .. Directory_Last),
925 -- Delete the source list file, if it already exists
931 (Source_List_Path (1 .. Source_List_Last),
935 -- And create a new source list file.
936 -- Fail if file cannot be created.
938 Source_List_FD := Create_New_File
939 (Name => Source_List_Path (1 .. Source_List_Last),
942 if Source_List_FD = Invalid_FD then
944 ("cannot create file """,
945 Source_List_Path (1 .. Source_List_Last),
950 -- Compile the regular expressions. Fails immediately if any of
951 -- the specified strings is in error.
953 for Index in Excluded_Expressions'Range loop
955 Output.Write_Str ("Excluded pattern: """);
956 Output.Write_Str (Excluded_Patterns (Index).all);
957 Output.Write_Line ("""");
961 Excluded_Expressions (Index) :=
962 Compile (Pattern => Excluded_Patterns (Index).all, Glob => True);
964 when Error_In_Regexp =>
966 ("invalid regular expression """,
967 Excluded_Patterns (Index).all,
972 for Index in Foreign_Expressions'Range loop
974 Output.Write_Str ("Foreign pattern: """);
975 Output.Write_Str (Foreign_Patterns (Index).all);
976 Output.Write_Line ("""");
980 Foreign_Expressions (Index) :=
981 Compile (Pattern => Foreign_Patterns (Index).all, Glob => True);
983 when Error_In_Regexp =>
985 ("invalid regular expression """,
986 Foreign_Patterns (Index).all,
991 for Index in Regular_Expressions'Range loop
993 Output.Write_Str ("Pattern: """);
994 Output.Write_Str (Name_Patterns (Index).all);
995 Output.Write_Line ("""");
999 Regular_Expressions (Index) :=
1000 Compile (Pattern => Name_Patterns (Index).all, Glob => True);
1003 when Error_In_Regexp =>
1005 ("invalid regular expression """,
1006 Name_Patterns (Index).all,
1011 if Project_File then
1012 if Opt.Verbose_Mode then
1013 Output.Write_Str ("Naming project file name is """);
1015 (Project_Naming_File_Name (1 .. Project_Naming_Last));
1016 Output.Write_Line ("""");
1019 -- If there were no already existing project file, or if the parsing
1020 -- was unsuccessful, create an empty project node with the correct
1021 -- name and its project declaration node.
1023 if Project_Node = Empty_Node then
1025 Default_Project_Node (Of_Kind => N_Project, In_Tree => Tree);
1026 Set_Name_Of (Project_Node, Tree, To => Output_Name_Id);
1027 Set_Project_Declaration_Of
1028 (Project_Node, Tree,
1029 To => Default_Project_Node
1030 (Of_Kind => N_Project_Declaration, In_Tree => Tree));
1034 -- Create the naming project node, and add an attribute declaration
1035 -- for Source_Files as an empty list, to indicate there are no
1036 -- sources in the naming project.
1038 Project_Naming_Node :=
1039 Default_Project_Node (Of_Kind => N_Project, In_Tree => Tree);
1040 Set_Name_Of (Project_Naming_Node, Tree, To => Project_Naming_Id);
1041 Project_Naming_Decl :=
1042 Default_Project_Node
1043 (Of_Kind => N_Project_Declaration, In_Tree => Tree);
1044 Set_Project_Declaration_Of
1045 (Project_Naming_Node, Tree, Project_Naming_Decl);
1047 Default_Project_Node
1048 (Of_Kind => N_Package_Declaration, In_Tree => Tree);
1049 Set_Name_Of (Naming_Package, Tree, To => Name_Naming);
1052 Decl_Item : constant Project_Node_Id :=
1053 Default_Project_Node
1054 (Of_Kind => N_Declarative_Item, In_Tree => Tree);
1056 Attribute : constant Project_Node_Id :=
1057 Default_Project_Node
1058 (Of_Kind => N_Attribute_Declaration,
1060 And_Expr_Kind => List);
1062 Expression : constant Project_Node_Id :=
1063 Default_Project_Node
1064 (Of_Kind => N_Expression,
1066 And_Expr_Kind => List);
1068 Term : constant Project_Node_Id :=
1069 Default_Project_Node
1072 And_Expr_Kind => List);
1074 Empty_List : constant Project_Node_Id :=
1075 Default_Project_Node
1076 (Of_Kind => N_Literal_String_List,
1080 Set_First_Declarative_Item_Of
1081 (Project_Naming_Decl, Tree, To => Decl_Item);
1082 Set_Next_Declarative_Item (Decl_Item, Tree, Naming_Package);
1083 Set_Current_Item_Node (Decl_Item, Tree, To => Attribute);
1084 Set_Name_Of (Attribute, Tree, To => Name_Source_Files);
1085 Set_Expression_Of (Attribute, Tree, To => Expression);
1086 Set_First_Term (Expression, Tree, To => Term);
1087 Set_Current_Term (Term, Tree, To => Empty_List);
1090 -- Add a with clause on the naming project in the main project, if
1091 -- there is not already one.
1094 With_Clause : Project_Node_Id :=
1095 First_With_Clause_Of (Project_Node, Tree);
1098 while With_Clause /= Empty_Node loop
1100 Prj.Tree.Name_Of (With_Clause, Tree) = Project_Naming_Id;
1101 With_Clause := Next_With_Clause_Of (With_Clause, Tree);
1104 if With_Clause = Empty_Node then
1105 With_Clause := Default_Project_Node
1106 (Of_Kind => N_With_Clause, In_Tree => Tree);
1107 Set_Next_With_Clause_Of
1109 To => First_With_Clause_Of (Project_Node, Tree));
1110 Set_First_With_Clause_Of
1111 (Project_Node, Tree, To => With_Clause);
1112 Set_Name_Of (With_Clause, Tree, To => Project_Naming_Id);
1114 -- We set the project node to something different than
1115 -- Empty_Node, so that Prj.PP does not generate a limited
1118 Set_Project_Node_Of (With_Clause, Tree, Non_Empty_Node);
1120 Name_Len := Project_Naming_Last;
1121 Name_Buffer (1 .. Name_Len) :=
1122 Project_Naming_File_Name (1 .. Project_Naming_Last);
1123 Set_String_Value_Of (With_Clause, Tree, To => Name_Find);
1127 Project_Declaration := Project_Declaration_Of (Project_Node, Tree);
1129 -- Add a renaming declaration for package Naming in the main project
1132 Decl_Item : constant Project_Node_Id :=
1133 Default_Project_Node
1134 (Of_Kind => N_Declarative_Item,
1137 Naming : constant Project_Node_Id :=
1138 Default_Project_Node
1139 (Of_Kind => N_Package_Declaration,
1143 Set_Next_Declarative_Item
1145 To => First_Declarative_Item_Of (Project_Declaration, Tree));
1146 Set_First_Declarative_Item_Of
1147 (Project_Declaration, Tree, To => Decl_Item);
1148 Set_Current_Item_Node (Decl_Item, Tree, To => Naming);
1149 Set_Name_Of (Naming, Tree, To => Name_Naming);
1150 Set_Project_Of_Renamed_Package_Of
1151 (Naming, Tree, To => Project_Naming_Node);
1153 -- Attach the comments, if any, that were saved for package
1156 Tree.Project_Nodes.Table (Naming).Comments :=
1157 Naming_Package_Comments;
1160 -- Add an attribute declaration for Source_Dirs, initialized as an
1161 -- empty list. Directories will be added as they are read from the
1162 -- directory list file.
1165 Decl_Item : constant Project_Node_Id :=
1166 Default_Project_Node
1167 (Of_Kind => N_Declarative_Item,
1170 Attribute : constant Project_Node_Id :=
1171 Default_Project_Node
1172 (Of_Kind => N_Attribute_Declaration,
1174 And_Expr_Kind => List);
1176 Expression : constant Project_Node_Id :=
1177 Default_Project_Node
1178 (Of_Kind => N_Expression,
1180 And_Expr_Kind => List);
1182 Term : constant Project_Node_Id :=
1183 Default_Project_Node
1184 (Of_Kind => N_Term, In_Tree => Tree,
1185 And_Expr_Kind => List);
1188 Set_Next_Declarative_Item
1190 To => First_Declarative_Item_Of (Project_Declaration, Tree));
1191 Set_First_Declarative_Item_Of
1192 (Project_Declaration, Tree, To => Decl_Item);
1193 Set_Current_Item_Node (Decl_Item, Tree, To => Attribute);
1194 Set_Name_Of (Attribute, Tree, To => Name_Source_Dirs);
1195 Set_Expression_Of (Attribute, Tree, To => Expression);
1196 Set_First_Term (Expression, Tree, To => Term);
1198 Default_Project_Node
1199 (Of_Kind => N_Literal_String_List,
1201 And_Expr_Kind => List);
1202 Set_Current_Term (Term, Tree, To => Source_Dirs_List);
1204 -- Attach the comments, if any, that were saved for attribute
1207 Tree.Project_Nodes.Table (Attribute).Comments :=
1208 Source_Dirs_Comments;
1211 -- Add an attribute declaration for Source_List_File with the
1212 -- source list file name that will be created.
1215 Decl_Item : constant Project_Node_Id :=
1216 Default_Project_Node
1217 (Of_Kind => N_Declarative_Item,
1220 Attribute : constant Project_Node_Id :=
1221 Default_Project_Node
1222 (Of_Kind => N_Attribute_Declaration,
1224 And_Expr_Kind => Single);
1226 Expression : constant Project_Node_Id :=
1227 Default_Project_Node
1228 (Of_Kind => N_Expression,
1230 And_Expr_Kind => Single);
1232 Term : constant Project_Node_Id :=
1233 Default_Project_Node
1236 And_Expr_Kind => Single);
1238 Value : constant Project_Node_Id :=
1239 Default_Project_Node
1240 (Of_Kind => N_Literal_String,
1242 And_Expr_Kind => Single);
1245 Set_Next_Declarative_Item
1247 To => First_Declarative_Item_Of (Project_Declaration, Tree));
1248 Set_First_Declarative_Item_Of
1249 (Project_Declaration, Tree, To => Decl_Item);
1250 Set_Current_Item_Node (Decl_Item, Tree, To => Attribute);
1251 Set_Name_Of (Attribute, Tree, To => Name_Source_List_File);
1252 Set_Expression_Of (Attribute, Tree, To => Expression);
1253 Set_First_Term (Expression, Tree, To => Term);
1254 Set_Current_Term (Term, Tree, To => Value);
1255 Name_Len := Source_List_Last;
1256 Name_Buffer (1 .. Name_Len) :=
1257 Source_List_Path (1 .. Source_List_Last);
1258 Set_String_Value_Of (Value, Tree, To => Name_Find);
1260 -- If there was no comments for attribute Source_List_File, put
1261 -- those for Source_Files, if they exist.
1263 if Source_List_File_Comments /= Empty_Node then
1264 Tree.Project_Nodes.Table (Attribute).Comments :=
1265 Source_List_File_Comments;
1267 Tree.Project_Nodes.Table (Attribute).Comments :=
1268 Source_Files_Comments;
1273 -- Process each directory
1275 for Index in Directories'Range loop
1278 Dir_Name : constant String := Directories (Index).all;
1279 Last : Natural := Dir_Name'Last;
1280 Recursively : Boolean := False;
1283 if Dir_Name'Length >= 4
1284 and then (Dir_Name (Last - 2 .. Last) = "/**")
1287 Recursively := True;
1290 if Project_File then
1292 -- Add the directory in the list for attribute Source_Dirs
1295 Expression : constant Project_Node_Id :=
1296 Default_Project_Node
1297 (Of_Kind => N_Expression,
1299 And_Expr_Kind => Single);
1301 Term : constant Project_Node_Id :=
1302 Default_Project_Node
1305 And_Expr_Kind => Single);
1307 Value : constant Project_Node_Id :=
1308 Default_Project_Node
1309 (Of_Kind => N_Literal_String,
1311 And_Expr_Kind => Single);
1314 if Current_Source_Dir = Empty_Node then
1315 Set_First_Expression_In_List
1316 (Source_Dirs_List, Tree, To => Expression);
1318 Set_Next_Expression_In_List
1319 (Current_Source_Dir, Tree, To => Expression);
1322 Current_Source_Dir := Expression;
1323 Set_First_Term (Expression, Tree, To => Term);
1324 Set_Current_Term (Term, Tree, To => Value);
1325 Name_Len := Dir_Name'Length;
1326 Name_Buffer (1 .. Name_Len) := Dir_Name;
1327 Set_String_Value_Of (Value, Tree, To => Name_Find);
1331 Process_Directory (Dir_Name (Dir_Name'First .. Last), Recursively);
1336 if Project_File then
1337 Close (Source_List_FD);
1344 -- Delete the file if it already exists
1347 (Path_Name (Directory_Last + 1 .. Path_Last),
1348 Success => Discard);
1352 if Opt.Verbose_Mode then
1353 Output.Write_Str ("Creating new file """);
1354 Output.Write_Str (Path_Name (Directory_Last + 1 .. Path_Last));
1355 Output.Write_Line ("""");
1358 Output_FD := Create_New_File
1359 (Path_Name (Directory_Last + 1 .. Path_Last),
1362 -- Fails if project file cannot be created
1364 if Output_FD = Invalid_FD then
1366 ("cannot create new """, Path_Name (1 .. Path_Last), """");
1369 if Project_File then
1371 -- Output the project file
1374 (Project_Node, Tree,
1375 W_Char => Write_A_Char'Access,
1376 W_Eol => Write_Eol'Access,
1377 W_Str => Write_A_String'Access,
1378 Backward_Compatibility => False);
1381 -- Delete the naming project file if it already exists
1384 (Project_Naming_File_Name (1 .. Project_Naming_Last),
1385 Success => Discard);
1389 if Opt.Verbose_Mode then
1390 Output.Write_Str ("Creating new naming project file """);
1391 Output.Write_Str (Project_Naming_File_Name
1392 (1 .. Project_Naming_Last));
1393 Output.Write_Line ("""");
1396 Output_FD := Create_New_File
1397 (Project_Naming_File_Name (1 .. Project_Naming_Last),
1400 -- Fails if naming project file cannot be created
1402 if Output_FD = Invalid_FD then
1404 ("cannot create new """,
1405 Project_Naming_File_Name (1 .. Project_Naming_Last),
1409 -- Output the naming project file
1412 (Project_Naming_Node, Tree,
1413 W_Char => Write_A_Char'Access,
1414 W_Eol => Write_Eol'Access,
1415 W_Str => Write_A_String'Access,
1416 Backward_Compatibility => False);
1420 -- Write to the output file each entry in the SFN_Pragmas table
1421 -- as an pragma Source_File_Name.
1423 for Index in 1 .. SFN_Pragmas.Last loop
1424 Write_A_String ("pragma Source_File_Name");
1426 Write_A_String (" (");
1428 (Get_Name_String (SFN_Pragmas.Table (Index).Unit));
1429 Write_A_String (",");
1432 if SFN_Pragmas.Table (Index).Spec then
1433 Write_A_String (" Spec_File_Name => """);
1436 Write_A_String (" Body_File_Name => """);
1440 (Get_Name_String (SFN_Pragmas.Table (Index).File));
1442 Write_A_String ("""");
1444 if SFN_Pragmas.Table (Index).Index /= 0 then
1445 Write_A_String (", Index =>");
1446 Write_A_String (SFN_Pragmas.Table (Index).Index'Img);
1449 Write_A_String (");");
1462 procedure Write_A_Char (C : Character) is
1464 Write_A_String ((1 => C));
1471 procedure Write_Eol is
1473 Write_A_String ((1 => ASCII.LF));
1476 --------------------
1477 -- Write_A_String --
1478 --------------------
1480 procedure Write_A_String (S : String) is
1481 Str : String (1 .. S'Length);
1484 if S'Length > 0 then
1487 if Write (Output_FD, Str (1)'Address, Str'Length) /= Str'Length then
1488 Prj.Com.Fail ("disk full");