1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2005 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, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, 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
.OS_Lib
; use GNAT
.OS_Lib
;
44 with GNAT
.Regexp
; use GNAT
.Regexp
;
46 with System
.Case_Util
; use System
.Case_Util
;
49 package body Prj
.Makr
is
51 function Dup
(Fd
: File_Descriptor
) return File_Descriptor
;
53 procedure Dup2
(Old_Fd
, New_Fd
: File_Descriptor
);
55 Gcc
: constant String := "gcc";
56 Gcc_Path
: String_Access
:= null;
58 Non_Empty_Node
: constant Project_Node_Id
:= 1;
59 -- Used for the With_Clause of the naming project
61 type Matched_Type
is (True, False, Excluded
);
63 Naming_File_Suffix
: constant String := "_naming";
64 Source_List_File_Suffix
: constant String := "_source_list.txt";
66 Output_FD
: File_Descriptor
;
67 -- To save the project file and its naming project file
70 -- Output an empty line
72 procedure Write_A_Char
(C
: Character);
73 -- Write one character to Output_FD
75 procedure Write_A_String
(S
: String);
76 -- Write a String to Output_FD
78 package Processed_Directories
is new Table
.Table
79 (Table_Component_Type
=> String_Access
,
80 Table_Index_Type
=> Natural,
83 Table_Increment
=> 10,
84 Table_Name
=> "Prj.Makr.Processed_Directories");
90 function Dup
(Fd
: File_Descriptor
) return File_Descriptor
is
92 return File_Descriptor
(System
.CRTL
.dup
(Integer (Fd
)));
99 procedure Dup2
(Old_Fd
, New_Fd
: File_Descriptor
) is
101 pragma Warnings
(Off
, Fd
);
103 Fd
:= System
.CRTL
.dup2
(Integer (Old_Fd
), Integer (New_Fd
));
112 Project_File
: Boolean;
113 Directories
: Argument_List
;
114 Name_Patterns
: Argument_List
;
115 Excluded_Patterns
: Argument_List
;
116 Foreign_Patterns
: Argument_List
;
117 Preproc_Switches
: Argument_List
;
118 Very_Verbose
: Boolean)
120 Tree
: constant Project_Node_Tree_Ref
:= new Project_Node_Tree_Data
;
124 Path_Name
: String (1 .. File_Path
'Length +
125 Project_File_Extension
'Length);
126 Path_Last
: Natural := File_Path
'Length;
128 Directory_Last
: Natural := 0;
130 Output_Name
: String (Path_Name
'Range);
131 Output_Name_Last
: Natural;
132 Output_Name_Id
: Name_Id
;
134 Project_Node
: Project_Node_Id
:= Empty_Node
;
135 Project_Declaration
: Project_Node_Id
:= Empty_Node
;
136 Source_Dirs_List
: Project_Node_Id
:= Empty_Node
;
137 Current_Source_Dir
: Project_Node_Id
:= Empty_Node
;
139 Project_Naming_Node
: Project_Node_Id
:= Empty_Node
;
140 Project_Naming_Decl
: Project_Node_Id
:= Empty_Node
;
141 Naming_Package
: Project_Node_Id
:= Empty_Node
;
143 Project_Naming_File_Name
: String (1 .. Output_Name
'Length +
144 Naming_File_Suffix
'Length);
146 Project_Naming_Last
: Natural;
147 Project_Naming_Id
: Name_Id
:= No_Name
;
149 Excluded_Expressions
: array (Excluded_Patterns
'Range) of Regexp
;
150 Regular_Expressions
: array (Name_Patterns
'Range) of Regexp
;
151 Foreign_Expressions
: array (Foreign_Patterns
'Range) of Regexp
;
153 Source_List_Path
: String (1 .. Output_Name
'Length +
154 Source_List_File_Suffix
'Length);
155 Source_List_Last
: Natural;
157 Source_List_FD
: File_Descriptor
;
159 Args
: Argument_List
(1 .. Preproc_Switches
'Length + 6);
161 type SFN_Pragma
is record
168 package SFN_Pragmas
is new Table
.Table
169 (Table_Component_Type
=> SFN_Pragma
,
170 Table_Index_Type
=> Natural,
171 Table_Low_Bound
=> 0,
173 Table_Increment
=> 50,
174 Table_Name
=> "Prj.Makr.SFN_Pragmas");
176 procedure Process_Directory
(Dir_Name
: String; Recursively
: Boolean);
177 -- Look for Ada and foreign sources in a directory, according to the
178 -- patterns. When Recursively is True, after looking for sources in
179 -- Dir_Name, look also in its subdirectories, if any.
181 -----------------------
182 -- Process_Directory --
183 -----------------------
185 procedure Process_Directory
(Dir_Name
: String; Recursively
: Boolean) is
186 Matched
: Matched_Type
:= False;
187 Str
: String (1 .. 2_000
);
188 Canon
: String (1 .. 2_000
);
191 Process
: Boolean := True;
193 Temp_File_Name
: String_Access
:= null;
194 Save_Last_Pragma_Index
: Natural := 0;
195 File_Name_Id
: Name_Id
:= No_Name
;
196 SFN_Prag
: SFN_Pragma
;
199 -- Avoid processing the same directory more than once
201 for Index
in 1 .. Processed_Directories
.Last
loop
202 if Processed_Directories
.Table
(Index
).all = Dir_Name
then
209 if Opt
.Verbose_Mode
then
210 Output
.Write_Str
("Processing directory """);
211 Output
.Write_Str
(Dir_Name
);
212 Output
.Write_Line
("""");
215 Processed_Directories
. Increment_Last
;
216 Processed_Directories
.Table
(Processed_Directories
.Last
) :=
217 new String'(Dir_Name);
219 -- Get the source file names from the directory. Fails if the
220 -- directory does not exist.
223 Open (Dir, Dir_Name);
225 when Directory_Error =>
226 Prj.Com.Fail ("cannot open directory """, Dir_Name, """");
229 -- Process each regular file in the directory
232 Read (Dir, Str, Last);
233 exit File_Loop when Last = 0;
235 -- Copy the file name and put it in canonical case to match
236 -- against the patterns that have themselves already been put
237 -- in canonical case.
239 Canon (1 .. Last) := Str (1 .. Last);
240 Canonical_Case_File_Name (Canon (1 .. Last));
243 (Dir_Name & Directory_Separator & Str (1 .. Last))
248 Name_Buffer (1 .. Name_Len) := Str (1 .. Last);
249 File_Name_Id := Name_Find;
251 -- First, check if the file name matches at least one of
252 -- the excluded expressions;
254 for Index in Excluded_Expressions'Range loop
256 Match (Canon (1 .. Last), Excluded_Expressions (Index))
263 -- If it does not match any of the excluded expressions,
264 -- check if the file name matches at least one of the
265 -- regular expressions.
267 if Matched = True then
270 for Index in Regular_Expressions'Range loop
273 (Canon (1 .. Last), Regular_Expressions (Index))
282 or else (Matched = True and then Opt.Verbose_Mode)
284 Output.Write_Str (" Checking """);
285 Output.Write_Str (Str (1 .. Last));
286 Output.Write_Line (""": ");
289 -- If the file name matches one of the regular expressions,
290 -- parse it to get its unit name.
292 if Matched = True then
294 FD : File_Descriptor;
296 Saved_Output : File_Descriptor;
297 Saved_Error : File_Descriptor;
300 -- If we don't have the path of the compiler yet,
301 -- get it now. The compiler name may have a prefix,
302 -- so we get the potentially prefixed name.
304 if Gcc_Path = null then
306 Prefix_Gcc : String_Access :=
310 Locate_Exec_On_Path (Prefix_Gcc.all);
314 if Gcc_Path = null then
315 Prj.Com.Fail ("could not locate " & Gcc);
319 -- If we don't have yet the file name of the
320 -- temporary file, get it now.
322 if Temp_File_Name = null then
323 Create_Temp_File (FD, Temp_File_Name);
325 if FD = Invalid_FD then
327 ("could not create temporary file");
331 Delete_File (Temp_File_Name.all, Success);
334 Args (Args'Last) := new String'
336 Directory_Separator
&
339 -- Create the temporary file
341 FD
:= Create_Output_Text_File
342 (Name
=> Temp_File_Name
.all);
344 if FD
= Invalid_FD
then
346 ("could not create temporary file");
349 -- Save the standard output and error
351 Saved_Output
:= Dup
(Standout
);
352 Saved_Error
:= Dup
(Standerr
);
354 -- Set standard output and error to the temporary file
359 -- And spawn the compiler
361 Spawn
(Gcc_Path
.all, Args
, Success
);
363 -- Restore the standard output and error
365 Dup2
(Saved_Output
, Standout
);
366 Dup2
(Saved_Error
, Standerr
);
368 -- Close the temporary file
372 -- And close the saved standard output and error to
373 -- avoid too many file descriptors.
375 Close
(Saved_Output
);
378 -- Now that standard output is restored, check if
379 -- the compiler ran correctly.
381 -- Read the lines of the temporary file:
382 -- they should contain the kind and name of the unit.
386 Text_Line
: String (1 .. 1_000
);
390 Open
(File
, Temp_File_Name
.all);
392 if not Is_Valid
(File
) then
394 ("could not read temporary file");
397 Save_Last_Pragma_Index
:= SFN_Pragmas
.Last
;
399 if End_Of_File
(File
) then
400 if Opt
.Verbose_Mode
then
402 Output
.Write_Str
(" (process died) ");
407 Line_Loop
: while not End_Of_File
(File
) loop
408 Get_Line
(File
, Text_Line
, Text_Last
);
410 -- Find the first closing parenthesis
412 Char_Loop
: for J
in 1 .. Text_Last
loop
413 if Text_Line
(J
) = ')' then
415 Text_Line
(1 .. 4) = "Unit"
417 -- Add entry to SFN_Pragmas table
420 Name_Buffer
(1 .. Name_Len
) :=
421 Text_Line
(6 .. J
- 7);
424 File
=> File_Name_Id
,
426 Spec
=> Text_Line
(J
- 5 .. J
) =
429 SFN_Pragmas
.Increment_Last
;
431 (SFN_Pragmas
.Last
) := SFN_Prag
;
439 if Save_Last_Pragma_Index
= SFN_Pragmas
.Last
then
440 if Opt
.Verbose_Mode
then
441 Output
.Write_Line
(" not a unit");
445 if SFN_Pragmas
.Last
>
446 Save_Last_Pragma_Index
+ 1
448 for Index
in Save_Last_Pragma_Index
+ 1 ..
451 SFN_Pragmas
.Table
(Index
).Index
:=
452 Int
(Index
- Save_Last_Pragma_Index
);
456 for Index
in Save_Last_Pragma_Index
+ 1 ..
459 SFN_Prag
:= SFN_Pragmas
.Table
(Index
);
461 if Opt
.Verbose_Mode
then
462 if SFN_Prag
.Spec
then
463 Output
.Write_Str
(" spec of ");
466 Output
.Write_Str
(" body of ");
470 (Get_Name_String
(SFN_Prag
.Unit
));
475 -- Add the corresponding attribute in the
476 -- Naming package of the naming project.
479 Decl_Item
: constant Project_Node_Id
:=
485 Attribute
: constant Project_Node_Id
:=
488 N_Attribute_Declaration
,
491 Expression
: constant Project_Node_Id
:=
493 (Of_Kind
=> N_Expression
,
494 And_Expr_Kind
=> Single
,
497 Term
: constant Project_Node_Id
:=
500 And_Expr_Kind
=> Single
,
503 Value
: constant Project_Node_Id
:=
505 (Of_Kind
=> N_Literal_String
,
506 And_Expr_Kind
=> Single
,
510 Set_Next_Declarative_Item
512 To
=> First_Declarative_Item_Of
513 (Naming_Package
, Tree
),
515 Set_First_Declarative_Item_Of
519 Set_Current_Item_Node
524 -- Is it a spec or a body?
526 if SFN_Prag
.Spec
then
536 -- Get the name of the unit
538 Get_Name_String
(SFN_Prag
.Unit
);
539 To_Lower
(Name_Buffer
(1 .. Name_Len
));
540 Set_Associative_Array_Index_Of
541 (Attribute
, Tree
, To
=> Name_Find
);
544 (Attribute
, Tree
, To
=> Expression
);
546 (Expression
, Tree
, To
=> Term
);
548 (Term
, Tree
, To
=> Value
);
550 -- And set the name of the file
553 (Value
, Tree
, To
=> File_Name_Id
);
555 (Value
, Tree
, To
=> SFN_Prag
.Index
);
561 -- Add source file name to source list
565 Str
(Last
) := ASCII
.LF
;
567 if Write
(Source_List_FD
,
571 Prj
.Com
.Fail
("disk full");
578 Delete_File
(Temp_File_Name
.all, Success
);
582 -- File name matches none of the regular expressions
585 -- If file is not excluded, see if this is foreign source
587 if Matched
/= Excluded
then
588 for Index
in Foreign_Expressions
'Range loop
589 if Match
(Canon
(1 .. Last
),
590 Foreign_Expressions
(Index
))
601 Output
.Write_Line
("no match");
604 Output
.Write_Line
("excluded");
607 Output
.Write_Line
("foreign source");
611 if Project_File
and Matched
= True then
613 -- Add source file name to source list file
616 Str
(Last
) := ASCII
.LF
;
618 if Write
(Source_List_FD
,
622 Prj
.Com
.Fail
("disk full");
632 -- If Recursively is True, call itself for each subdirectory.
633 -- We do that, even when this directory has already been processed,
634 -- because all of its subdirectories may not have been processed.
637 Open
(Dir
, Dir_Name
);
640 Read
(Dir
, Str
, Last
);
643 -- Do not call itself for "." or ".."
646 (Dir_Name
& Directory_Separator
& Str
(1 .. Last
))
647 and then Str
(1 .. Last
) /= "."
648 and then Str
(1 .. Last
) /= ".."
651 (Dir_Name
& Directory_Separator
& Str
(1 .. Last
),
652 Recursively
=> True);
658 end Process_Directory
;
660 -- Start of processing for Make
663 -- Do some needed initializations
668 Prj
.Initialize
(No_Project_Tree
);
669 Prj
.Tree
.Initialize
(Tree
);
671 SFN_Pragmas
.Set_Last
(0);
673 Processed_Directories
.Set_Last
(0);
675 -- Initialize the compiler switches
677 Args
(1) := new String'("-c");
678 Args (2) := new String'("-gnats");
679 Args
(3) := new String'("-gnatu");
680 Args (4 .. 3 + Preproc_Switches'Length) := Preproc_Switches;
681 Args (4 + Preproc_Switches'Length) := new String'("-x");
682 Args
(5 + Preproc_Switches
'Length) := new String'("ada");
684 -- Get the path and file names
686 if File_Names_Case_Sensitive then
687 Path_Name (1 .. Path_Last) := File_Path;
689 Path_Name (1 .. Path_Last) := To_Lower (File_Path);
692 Path_Name (Path_Last + 1 .. Path_Name'Last) :=
693 Project_File_Extension;
695 -- Get the end of directory information, if any
697 for Index in reverse 1 .. Path_Last loop
698 if Path_Name (Index) = Directory_Separator then
699 Directory_Last := Index;
705 if Path_Last < Project_File_Extension'Length + 1
707 (Path_Last - Project_File_Extension'Length + 1 .. Path_Last)
708 /= Project_File_Extension
710 Path_Last := Path_Name'Last;
713 Output_Name (1 .. Path_Last) := To_Lower (Path_Name (1 .. Path_Last));
714 Output_Name_Last := Path_Last - Project_File_Extension'Length;
716 -- If there is already a project file with the specified name, parse
717 -- it to get the components that are not automatically generated.
719 if Is_Regular_File (Output_Name (1 .. Path_Last)) then
720 if Opt.Verbose_Mode then
721 Output.Write_Str ("Parsing already existing project file """);
722 Output.Write_Str (Output_Name (1 .. Output_Name_Last));
723 Output.Write_Line ("""");
728 Project => Project_Node,
729 Project_File_Name => Output_Name (1 .. Output_Name_Last),
730 Always_Errout_Finalize => False);
732 -- Fail if parsing was not successful
734 if Project_Node = Empty_Node then
735 Fail ("parsing of existing project file failed");
738 -- If parsing was successful, remove the components that are
739 -- automatically generated, if any, so that they will be
740 -- unconditionally added later.
742 -- Remove the with clause for the naming project file
745 With_Clause : Project_Node_Id :=
746 First_With_Clause_Of (Project_Node, Tree);
747 Previous : Project_Node_Id := Empty_Node;
750 while With_Clause /= Empty_Node loop
751 if Prj.Tree.Name_Of (With_Clause, Tree) =
754 if Previous = Empty_Node then
755 Set_First_With_Clause_Of
757 To => Next_With_Clause_Of (With_Clause, Tree));
759 Set_Next_With_Clause_Of
761 To => Next_With_Clause_Of (With_Clause, Tree));
767 Previous := With_Clause;
768 With_Clause := Next_With_Clause_Of (With_Clause, Tree);
772 -- Remove attribute declarations of Source_Files,
773 -- Source_List_File, Source_Dirs, and the declaration of
774 -- package Naming, if they exist.
777 Declaration : Project_Node_Id :=
778 First_Declarative_Item_Of
779 (Project_Declaration_Of
780 (Project_Node, Tree),
782 Previous : Project_Node_Id := Empty_Node;
783 Current_Node : Project_Node_Id := Empty_Node;
786 while Declaration /= Empty_Node loop
787 Current_Node := Current_Item_Node (Declaration, Tree);
789 if (Kind_Of (Current_Node, Tree) = N_Attribute_Declaration
791 (Prj.Tree.Name_Of (Current_Node, Tree) =
793 or else Prj.Tree.Name_Of (Current_Node, Tree) =
794 Name_Source_List_File
795 or else Prj.Tree.Name_Of (Current_Node, Tree) =
798 (Kind_Of (Current_Node, Tree) = N_Package_Declaration
799 and then Prj.Tree.Name_Of (Current_Node, Tree) =
802 if Previous = Empty_Node then
803 Set_First_Declarative_Item_Of
804 (Project_Declaration_Of (Project_Node, Tree),
806 To => Next_Declarative_Item (Declaration, Tree));
809 Set_Next_Declarative_Item
811 To => Next_Declarative_Item (Declaration, Tree));
815 Previous := Declaration;
818 Declaration := Next_Declarative_Item (Declaration, Tree);
824 if Directory_Last /= 0 then
825 Output_Name (1 .. Output_Name_Last - Directory_Last) :=
826 Output_Name (Directory_Last + 1 .. Output_Name_Last);
827 Output_Name_Last := Output_Name_Last - Directory_Last;
830 -- Get the project name id
832 Name_Len := Output_Name_Last;
833 Name_Buffer (1 .. Name_Len) := Output_Name (1 .. Name_Len);
834 Output_Name_Id := Name_Find;
836 -- Create the project naming file name
838 Project_Naming_Last := Output_Name_Last;
839 Project_Naming_File_Name (1 .. Project_Naming_Last) :=
840 Output_Name (1 .. Project_Naming_Last);
841 Project_Naming_File_Name
842 (Project_Naming_Last + 1 ..
843 Project_Naming_Last + Naming_File_Suffix'Length) :=
845 Project_Naming_Last :=
846 Project_Naming_Last + Naming_File_Suffix'Length;
848 -- Get the project naming id
850 Name_Len := Project_Naming_Last;
851 Name_Buffer (1 .. Name_Len) :=
852 Project_Naming_File_Name (1 .. Name_Len);
853 Project_Naming_Id := Name_Find;
855 Project_Naming_File_Name
856 (Project_Naming_Last + 1 ..
857 Project_Naming_Last + Project_File_Extension'Length) :=
858 Project_File_Extension;
859 Project_Naming_Last :=
860 Project_Naming_Last + Project_File_Extension'Length;
862 -- Create the source list file name
864 Source_List_Last := Output_Name_Last;
865 Source_List_Path (1 .. Source_List_Last) :=
866 Output_Name (1 .. Source_List_Last);
868 (Source_List_Last + 1 ..
869 Source_List_Last + Source_List_File_Suffix'Length) :=
870 Source_List_File_Suffix;
871 Source_List_Last := Source_List_Last + Source_List_File_Suffix'Length;
873 -- Add the project file extension to the project name
876 (Output_Name_Last + 1 ..
877 Output_Name_Last + Project_File_Extension'Length) :=
878 Project_File_Extension;
879 Output_Name_Last := Output_Name_Last + Project_File_Extension'Length;
882 -- Change the current directory to the directory of the project file,
883 -- if any directory information is specified.
885 if Directory_Last /= 0 then
887 Change_Dir (Path_Name (1 .. Directory_Last));
889 when Directory_Error =>
891 ("unknown directory """,
892 Path_Name (1 .. Directory_Last),
899 -- Delete the source list file, if it already exists
905 (Source_List_Path (1 .. Source_List_Last),
909 -- And create a new source list file.
910 -- Fail if file cannot be created.
912 Source_List_FD := Create_New_File
913 (Name => Source_List_Path (1 .. Source_List_Last),
916 if Source_List_FD = Invalid_FD then
918 ("cannot create file """,
919 Source_List_Path (1 .. Source_List_Last),
924 -- Compile the regular expressions. Fails immediately if any of
925 -- the specified strings is in error.
927 for Index in Excluded_Expressions'Range loop
929 Output.Write_Str ("Excluded pattern: """);
930 Output.Write_Str (Excluded_Patterns (Index).all);
931 Output.Write_Line ("""");
935 Excluded_Expressions (Index) :=
936 Compile (Pattern => Excluded_Patterns (Index).all, Glob => True);
938 when Error_In_Regexp =>
940 ("invalid regular expression """,
941 Excluded_Patterns (Index).all,
946 for Index in Foreign_Expressions'Range loop
948 Output.Write_Str ("Foreign pattern: """);
949 Output.Write_Str (Foreign_Patterns (Index).all);
950 Output.Write_Line ("""");
954 Foreign_Expressions (Index) :=
955 Compile (Pattern => Foreign_Patterns (Index).all, Glob => True);
957 when Error_In_Regexp =>
959 ("invalid regular expression """,
960 Foreign_Patterns (Index).all,
965 for Index in Regular_Expressions'Range loop
967 Output.Write_Str ("Pattern: """);
968 Output.Write_Str (Name_Patterns (Index).all);
969 Output.Write_Line ("""");
973 Regular_Expressions (Index) :=
974 Compile (Pattern => Name_Patterns (Index).all, Glob => True);
977 when Error_In_Regexp =>
979 ("invalid regular expression """,
980 Name_Patterns (Index).all,
986 if Opt.Verbose_Mode then
987 Output.Write_Str ("Naming project file name is """);
989 (Project_Naming_File_Name (1 .. Project_Naming_Last));
990 Output.Write_Line ("""");
993 -- If there were no already existing project file, or if the parsing
994 -- was unsuccessful, create an empty project node with the correct
995 -- name and its project declaration node.
997 if Project_Node = Empty_Node then
999 Default_Project_Node (Of_Kind => N_Project, In_Tree => Tree);
1000 Set_Name_Of (Project_Node, Tree, To => Output_Name_Id);
1001 Set_Project_Declaration_Of
1002 (Project_Node, Tree,
1003 To => Default_Project_Node
1004 (Of_Kind => N_Project_Declaration, In_Tree => Tree));
1008 -- Create the naming project node, and add an attribute declaration
1009 -- for Source_Files as an empty list, to indicate there are no
1010 -- sources in the naming project.
1012 Project_Naming_Node :=
1013 Default_Project_Node (Of_Kind => N_Project, In_Tree => Tree);
1014 Set_Name_Of (Project_Naming_Node, Tree, To => Project_Naming_Id);
1015 Project_Naming_Decl :=
1016 Default_Project_Node
1017 (Of_Kind => N_Project_Declaration, In_Tree => Tree);
1018 Set_Project_Declaration_Of
1019 (Project_Naming_Node, Tree, Project_Naming_Decl);
1021 Default_Project_Node
1022 (Of_Kind => N_Package_Declaration, In_Tree => Tree);
1023 Set_Name_Of (Naming_Package, Tree, To => Name_Naming);
1026 Decl_Item : constant Project_Node_Id :=
1027 Default_Project_Node
1028 (Of_Kind => N_Declarative_Item, In_Tree => Tree);
1030 Attribute : constant Project_Node_Id :=
1031 Default_Project_Node
1032 (Of_Kind => N_Attribute_Declaration,
1034 And_Expr_Kind => List);
1036 Expression : constant Project_Node_Id :=
1037 Default_Project_Node
1038 (Of_Kind => N_Expression,
1040 And_Expr_Kind => List);
1042 Term : constant Project_Node_Id :=
1043 Default_Project_Node
1046 And_Expr_Kind => List);
1048 Empty_List : constant Project_Node_Id :=
1049 Default_Project_Node
1050 (Of_Kind => N_Literal_String_List,
1054 Set_First_Declarative_Item_Of
1055 (Project_Naming_Decl, Tree, To => Decl_Item);
1056 Set_Next_Declarative_Item (Decl_Item, Tree, Naming_Package);
1057 Set_Current_Item_Node (Decl_Item, Tree, To => Attribute);
1058 Set_Name_Of (Attribute, Tree, To => Name_Source_Files);
1059 Set_Expression_Of (Attribute, Tree, To => Expression);
1060 Set_First_Term (Expression, Tree, To => Term);
1061 Set_Current_Term (Term, Tree, To => Empty_List);
1064 -- Add a with clause on the naming project in the main project
1067 With_Clause : constant Project_Node_Id :=
1068 Default_Project_Node
1069 (Of_Kind => N_With_Clause, In_Tree => Tree);
1072 Set_Next_With_Clause_Of
1074 To => First_With_Clause_Of (Project_Node, Tree));
1075 Set_First_With_Clause_Of (Project_Node, Tree, To => With_Clause);
1076 Set_Name_Of (With_Clause, Tree, To => Project_Naming_Id);
1078 -- We set the project node to something different than
1079 -- Empty_Node, so that Prj.PP does not generate a limited
1082 Set_Project_Node_Of (With_Clause, Tree, Non_Empty_Node);
1084 Name_Len := Project_Naming_Last;
1085 Name_Buffer (1 .. Name_Len) :=
1086 Project_Naming_File_Name (1 .. Project_Naming_Last);
1087 Set_String_Value_Of (With_Clause, Tree, To => Name_Find);
1090 Project_Declaration := Project_Declaration_Of (Project_Node, Tree);
1092 -- Add a renaming declaration for package Naming in the main project
1095 Decl_Item : constant Project_Node_Id :=
1096 Default_Project_Node
1097 (Of_Kind => N_Declarative_Item,
1100 Naming : constant Project_Node_Id :=
1101 Default_Project_Node
1102 (Of_Kind => N_Package_Declaration,
1106 Set_Next_Declarative_Item
1108 To => First_Declarative_Item_Of (Project_Declaration, Tree));
1109 Set_First_Declarative_Item_Of
1110 (Project_Declaration, Tree, To => Decl_Item);
1111 Set_Current_Item_Node (Decl_Item, Tree, To => Naming);
1112 Set_Name_Of (Naming, Tree, To => Name_Naming);
1113 Set_Project_Of_Renamed_Package_Of
1114 (Naming, Tree, To => Project_Naming_Node);
1117 -- Add an attribute declaration for Source_Dirs, initialized as an
1118 -- empty list. Directories will be added as they are read from the
1119 -- directory list file.
1122 Decl_Item : constant Project_Node_Id :=
1123 Default_Project_Node
1124 (Of_Kind => N_Declarative_Item,
1127 Attribute : constant Project_Node_Id :=
1128 Default_Project_Node
1129 (Of_Kind => N_Attribute_Declaration,
1131 And_Expr_Kind => List);
1133 Expression : constant Project_Node_Id :=
1134 Default_Project_Node
1135 (Of_Kind => N_Expression,
1137 And_Expr_Kind => List);
1139 Term : constant Project_Node_Id :=
1140 Default_Project_Node
1141 (Of_Kind => N_Term, In_Tree => Tree,
1142 And_Expr_Kind => List);
1145 Set_Next_Declarative_Item
1147 To => First_Declarative_Item_Of (Project_Declaration, Tree));
1148 Set_First_Declarative_Item_Of
1149 (Project_Declaration, Tree, To => Decl_Item);
1150 Set_Current_Item_Node (Decl_Item, Tree, To => Attribute);
1151 Set_Name_Of (Attribute, Tree, To => Name_Source_Dirs);
1152 Set_Expression_Of (Attribute, Tree, To => Expression);
1153 Set_First_Term (Expression, Tree, To => Term);
1155 Default_Project_Node
1156 (Of_Kind => N_Literal_String_List,
1158 And_Expr_Kind => List);
1159 Set_Current_Term (Term, Tree, To => Source_Dirs_List);
1162 -- Add an attribute declaration for Source_List_File with the
1163 -- source list file name that will be created.
1166 Decl_Item : constant Project_Node_Id :=
1167 Default_Project_Node
1168 (Of_Kind => N_Declarative_Item,
1171 Attribute : constant Project_Node_Id :=
1172 Default_Project_Node
1173 (Of_Kind => N_Attribute_Declaration,
1175 And_Expr_Kind => Single);
1177 Expression : constant Project_Node_Id :=
1178 Default_Project_Node
1179 (Of_Kind => N_Expression,
1181 And_Expr_Kind => Single);
1183 Term : constant Project_Node_Id :=
1184 Default_Project_Node
1187 And_Expr_Kind => Single);
1189 Value : constant Project_Node_Id :=
1190 Default_Project_Node
1191 (Of_Kind => N_Literal_String,
1193 And_Expr_Kind => Single);
1196 Set_Next_Declarative_Item
1198 To => First_Declarative_Item_Of (Project_Declaration, Tree));
1199 Set_First_Declarative_Item_Of
1200 (Project_Declaration, Tree, To => Decl_Item);
1201 Set_Current_Item_Node (Decl_Item, Tree, To => Attribute);
1202 Set_Name_Of (Attribute, Tree, To => Name_Source_List_File);
1203 Set_Expression_Of (Attribute, Tree, To => Expression);
1204 Set_First_Term (Expression, Tree, To => Term);
1205 Set_Current_Term (Term, Tree, To => Value);
1206 Name_Len := Source_List_Last;
1207 Name_Buffer (1 .. Name_Len) :=
1208 Source_List_Path (1 .. Source_List_Last);
1209 Set_String_Value_Of (Value, Tree, To => Name_Find);
1213 -- Process each directory
1215 for Index in Directories'Range loop
1218 Dir_Name : constant String := Directories (Index).all;
1219 Last : Natural := Dir_Name'Last;
1220 Recursively : Boolean := False;
1223 if Dir_Name'Length >= 4
1224 and then (Dir_Name (Last - 2 .. Last) = "/**")
1227 Recursively := True;
1230 if Project_File then
1232 -- Add the directory in the list for attribute Source_Dirs
1235 Expression : constant Project_Node_Id :=
1236 Default_Project_Node
1237 (Of_Kind => N_Expression,
1239 And_Expr_Kind => Single);
1241 Term : constant Project_Node_Id :=
1242 Default_Project_Node
1245 And_Expr_Kind => Single);
1247 Value : constant Project_Node_Id :=
1248 Default_Project_Node
1249 (Of_Kind => N_Literal_String,
1251 And_Expr_Kind => Single);
1254 if Current_Source_Dir = Empty_Node then
1255 Set_First_Expression_In_List
1256 (Source_Dirs_List, Tree, To => Expression);
1258 Set_Next_Expression_In_List
1259 (Current_Source_Dir, Tree, To => Expression);
1262 Current_Source_Dir := Expression;
1263 Set_First_Term (Expression, Tree, To => Term);
1264 Set_Current_Term (Term, Tree, To => Value);
1265 Name_Len := Dir_Name'Length;
1266 Name_Buffer (1 .. Name_Len) := Dir_Name;
1267 Set_String_Value_Of (Value, Tree, To => Name_Find);
1271 Process_Directory (Dir_Name (Dir_Name'First .. Last), Recursively);
1276 if Project_File then
1277 Close (Source_List_FD);
1284 -- Delete the file if it already exists
1287 (Path_Name (Directory_Last + 1 .. Path_Last),
1288 Success => Discard);
1292 if Opt.Verbose_Mode then
1293 Output.Write_Str ("Creating new file """);
1294 Output.Write_Str (Path_Name (Directory_Last + 1 .. Path_Last));
1295 Output.Write_Line ("""");
1298 Output_FD := Create_New_File
1299 (Path_Name (Directory_Last + 1 .. Path_Last),
1302 -- Fails if project file cannot be created
1304 if Output_FD = Invalid_FD then
1306 ("cannot create new """, Path_Name (1 .. Path_Last), """");
1309 if Project_File then
1311 -- Output the project file
1314 (Project_Node, Tree,
1315 W_Char => Write_A_Char'Access,
1316 W_Eol => Write_Eol'Access,
1317 W_Str => Write_A_String'Access,
1318 Backward_Compatibility => False);
1321 -- Delete the naming project file if it already exists
1324 (Project_Naming_File_Name (1 .. Project_Naming_Last),
1325 Success => Discard);
1329 if Opt.Verbose_Mode then
1330 Output.Write_Str ("Creating new naming project file """);
1331 Output.Write_Str (Project_Naming_File_Name
1332 (1 .. Project_Naming_Last));
1333 Output.Write_Line ("""");
1336 Output_FD := Create_New_File
1337 (Project_Naming_File_Name (1 .. Project_Naming_Last),
1340 -- Fails if naming project file cannot be created
1342 if Output_FD = Invalid_FD then
1344 ("cannot create new """,
1345 Project_Naming_File_Name (1 .. Project_Naming_Last),
1349 -- Output the naming project file
1352 (Project_Naming_Node, Tree,
1353 W_Char => Write_A_Char'Access,
1354 W_Eol => Write_Eol'Access,
1355 W_Str => Write_A_String'Access,
1356 Backward_Compatibility => False);
1360 -- Write to the output file each entry in the SFN_Pragmas table
1361 -- as an pragma Source_File_Name.
1363 for Index in 1 .. SFN_Pragmas.Last loop
1364 Write_A_String ("pragma Source_File_Name");
1366 Write_A_String (" (");
1368 (Get_Name_String (SFN_Pragmas.Table (Index).Unit));
1369 Write_A_String (",");
1372 if SFN_Pragmas.Table (Index).Spec then
1373 Write_A_String (" Spec_File_Name => """);
1376 Write_A_String (" Body_File_Name => """);
1380 (Get_Name_String (SFN_Pragmas.Table (Index).File));
1382 Write_A_String ("""");
1384 if SFN_Pragmas.Table (Index).Index /= 0 then
1385 Write_A_String (", Index =>");
1386 Write_A_String (SFN_Pragmas.Table (Index).Index'Img);
1389 Write_A_String (");");
1402 procedure Write_A_Char (C : Character) is
1404 Write_A_String ((1 => C));
1411 procedure Write_Eol is
1413 Write_A_String ((1 => ASCII.LF));
1416 --------------------
1417 -- Write_A_String --
1418 --------------------
1420 procedure Write_A_String (S : String) is
1421 Str : String (1 .. S'Length);
1424 if S'Length > 0 then
1427 if Write (Output_FD, Str (1)'Address, Str'Length) /= Str'Length then
1428 Prj.Com.Fail ("disk full");