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 ------------------------------------------------------------------------------
29 with Osint
; use Osint
;
34 with Prj
.Tree
; use Prj
.Tree
;
35 with Prj
.Util
; use Prj
.Util
;
36 with Snames
; use Snames
;
37 with Table
; use Table
;
39 with Ada
.Characters
.Handling
; use Ada
.Characters
.Handling
;
40 with GNAT
.Directory_Operations
; use GNAT
.Directory_Operations
;
42 with System
.Case_Util
; use System
.Case_Util
;
44 with System
.Regexp
; use System
.Regexp
;
46 package body Prj
.Makr
is
48 -- Packages of project files where unknown attributes are errors
50 -- All the following need comments ??? All global variables and
51 -- subprograms must be fully commented.
53 Naming_String
: aliased String := "naming";
55 Gnatname_Packages
: aliased String_List
:= (1 => Naming_String
'Access);
57 Packages_To_Check_By_Gnatname
: constant String_List_Access
:=
58 Gnatname_Packages
'Access;
60 function Dup
(Fd
: File_Descriptor
) return File_Descriptor
;
62 procedure Dup2
(Old_Fd
, New_Fd
: File_Descriptor
);
64 Gcc
: constant String := "gcc";
65 Gcc_Path
: String_Access
:= null;
67 Non_Empty_Node
: constant Project_Node_Id
:= 1;
68 -- Used for the With_Clause of the naming project
70 type Matched_Type
is (True, False, Excluded
);
72 Naming_File_Suffix
: constant String := "_naming";
73 Source_List_File_Suffix
: constant String := "_source_list.txt";
75 Output_FD
: File_Descriptor
;
76 -- To save the project file and its naming project file
79 -- Output an empty line
81 procedure Write_A_Char
(C
: Character);
82 -- Write one character to Output_FD
84 procedure Write_A_String
(S
: String);
85 -- Write a String to Output_FD
87 package Processed_Directories
is new Table
.Table
88 (Table_Component_Type
=> String_Access
,
89 Table_Index_Type
=> Natural,
92 Table_Increment
=> 100,
93 Table_Name
=> "Prj.Makr.Processed_Directories");
99 function Dup
(Fd
: File_Descriptor
) return File_Descriptor
is
101 return File_Descriptor
(System
.CRTL
.dup
(Integer (Fd
)));
108 procedure Dup2
(Old_Fd
, New_Fd
: File_Descriptor
) is
110 pragma Warnings
(Off
, Fd
);
112 Fd
:= System
.CRTL
.dup2
(Integer (Old_Fd
), Integer (New_Fd
));
121 Project_File
: Boolean;
122 Directories
: Argument_List
;
123 Name_Patterns
: Argument_List
;
124 Excluded_Patterns
: Argument_List
;
125 Foreign_Patterns
: Argument_List
;
126 Preproc_Switches
: Argument_List
;
127 Very_Verbose
: Boolean)
129 Tree
: constant Project_Node_Tree_Ref
:= new Project_Node_Tree_Data
;
131 Path_Name
: String (1 .. File_Path
'Length +
132 Project_File_Extension
'Length);
133 Path_Last
: Natural := File_Path
'Length;
135 Directory_Last
: Natural := 0;
137 Output_Name
: String (Path_Name
'Range);
138 Output_Name_Last
: Natural;
139 Output_Name_Id
: Name_Id
;
141 Project_Node
: Project_Node_Id
:= Empty_Node
;
142 Project_Declaration
: Project_Node_Id
:= Empty_Node
;
143 Source_Dirs_List
: Project_Node_Id
:= Empty_Node
;
144 Current_Source_Dir
: Project_Node_Id
:= Empty_Node
;
146 Project_Naming_Node
: Project_Node_Id
:= Empty_Node
;
147 Project_Naming_Decl
: Project_Node_Id
:= Empty_Node
;
148 Naming_Package
: Project_Node_Id
:= Empty_Node
;
149 Naming_Package_Comments
: Project_Node_Id
:= Empty_Node
;
151 Source_Files_Comments
: Project_Node_Id
:= Empty_Node
;
152 Source_Dirs_Comments
: Project_Node_Id
:= Empty_Node
;
153 Source_List_File_Comments
: Project_Node_Id
:= Empty_Node
;
155 Project_Naming_File_Name
: String (1 .. Output_Name
'Length +
156 Naming_File_Suffix
'Length);
158 Project_Naming_Last
: Natural;
159 Project_Naming_Id
: Name_Id
:= No_Name
;
161 Excluded_Expressions
: array (Excluded_Patterns
'Range) of Regexp
;
162 Regular_Expressions
: array (Name_Patterns
'Range) of Regexp
;
163 Foreign_Expressions
: array (Foreign_Patterns
'Range) of Regexp
;
165 Source_List_Path
: String (1 .. Output_Name
'Length +
166 Source_List_File_Suffix
'Length);
167 Source_List_Last
: Natural;
169 Source_List_FD
: File_Descriptor
;
171 Args
: Argument_List
(1 .. Preproc_Switches
'Length + 6);
173 type SFN_Pragma
is record
180 package SFN_Pragmas
is new Table
.Table
181 (Table_Component_Type
=> SFN_Pragma
,
182 Table_Index_Type
=> Natural,
183 Table_Low_Bound
=> 0,
185 Table_Increment
=> 100,
186 Table_Name
=> "Prj.Makr.SFN_Pragmas");
188 procedure Process_Directory
(Dir_Name
: String; Recursively
: Boolean);
189 -- Look for Ada and foreign sources in a directory, according to the
190 -- patterns. When Recursively is True, after looking for sources in
191 -- Dir_Name, look also in its subdirectories, if any.
193 -----------------------
194 -- Process_Directory --
195 -----------------------
197 procedure Process_Directory
(Dir_Name
: String; Recursively
: Boolean) is
198 Matched
: Matched_Type
:= False;
199 Str
: String (1 .. 2_000
);
200 Canon
: String (1 .. 2_000
);
203 Process
: Boolean := True;
205 Temp_File_Name
: String_Access
:= null;
206 Save_Last_Pragma_Index
: Natural := 0;
207 File_Name_Id
: Name_Id
:= No_Name
;
208 SFN_Prag
: SFN_Pragma
;
211 -- Avoid processing the same directory more than once
213 for Index
in 1 .. Processed_Directories
.Last
loop
214 if Processed_Directories
.Table
(Index
).all = Dir_Name
then
221 if Opt
.Verbose_Mode
then
222 Output
.Write_Str
("Processing directory """);
223 Output
.Write_Str
(Dir_Name
);
224 Output
.Write_Line
("""");
227 Processed_Directories
. Increment_Last
;
228 Processed_Directories
.Table
(Processed_Directories
.Last
) :=
229 new String'(Dir_Name);
231 -- Get the source file names from the directory. Fails if the
232 -- directory does not exist.
235 Open (Dir, Dir_Name);
237 when Directory_Error =>
238 Prj.Com.Fail ("cannot open directory """, Dir_Name, """");
241 -- Process each regular file in the directory
244 Read (Dir, Str, Last);
245 exit File_Loop when Last = 0;
247 -- Copy the file name and put it in canonical case to match
248 -- against the patterns that have themselves already been put
249 -- in canonical case.
251 Canon (1 .. Last) := Str (1 .. Last);
252 Canonical_Case_File_Name (Canon (1 .. Last));
255 (Dir_Name & Directory_Separator & Str (1 .. Last))
260 Name_Buffer (1 .. Name_Len) := Str (1 .. Last);
261 File_Name_Id := Name_Find;
263 -- First, check if the file name matches at least one of
264 -- the excluded expressions;
266 for Index in Excluded_Expressions'Range loop
268 Match (Canon (1 .. Last), Excluded_Expressions (Index))
275 -- If it does not match any of the excluded expressions,
276 -- check if the file name matches at least one of the
277 -- regular expressions.
279 if Matched = True then
282 for Index in Regular_Expressions'Range loop
285 (Canon (1 .. Last), Regular_Expressions (Index))
294 or else (Matched = True and then Opt.Verbose_Mode)
296 Output.Write_Str (" Checking """);
297 Output.Write_Str (Str (1 .. Last));
298 Output.Write_Line (""": ");
301 -- If the file name matches one of the regular expressions,
302 -- parse it to get its unit name.
304 if Matched = True then
306 FD : File_Descriptor;
308 Saved_Output : File_Descriptor;
309 Saved_Error : File_Descriptor;
312 -- If we don't have the path of the compiler yet,
313 -- get it now. The compiler name may have a prefix,
314 -- so we get the potentially prefixed name.
316 if Gcc_Path = null then
318 Prefix_Gcc : String_Access :=
322 Locate_Exec_On_Path (Prefix_Gcc.all);
326 if Gcc_Path = null then
327 Prj.Com.Fail ("could not locate " & Gcc);
331 -- If we don't have yet the file name of the
332 -- temporary file, get it now.
334 if Temp_File_Name = null then
335 Create_Temp_File (FD, Temp_File_Name);
337 if FD = Invalid_FD then
339 ("could not create temporary file");
343 Delete_File (Temp_File_Name.all, Success);
346 Args (Args'Last) := new String'
348 Directory_Separator
&
351 -- Create the temporary file
353 FD
:= Create_Output_Text_File
354 (Name
=> Temp_File_Name
.all);
356 if FD
= Invalid_FD
then
358 ("could not create temporary file");
361 -- Save the standard output and error
363 Saved_Output
:= Dup
(Standout
);
364 Saved_Error
:= Dup
(Standerr
);
366 -- Set standard output and error to the temporary file
371 -- And spawn the compiler
373 Spawn
(Gcc_Path
.all, Args
, Success
);
375 -- Restore the standard output and error
377 Dup2
(Saved_Output
, Standout
);
378 Dup2
(Saved_Error
, Standerr
);
380 -- Close the temporary file
384 -- And close the saved standard output and error to
385 -- avoid too many file descriptors.
387 Close
(Saved_Output
);
390 -- Now that standard output is restored, check if
391 -- the compiler ran correctly.
393 -- Read the lines of the temporary file:
394 -- they should contain the kind and name of the unit.
398 Text_Line
: String (1 .. 1_000
);
402 Open
(File
, Temp_File_Name
.all);
404 if not Is_Valid
(File
) then
406 ("could not read temporary file");
409 Save_Last_Pragma_Index
:= SFN_Pragmas
.Last
;
411 if End_Of_File
(File
) then
412 if Opt
.Verbose_Mode
then
414 Output
.Write_Str
(" (process died) ");
419 Line_Loop
: while not End_Of_File
(File
) loop
420 Get_Line
(File
, Text_Line
, Text_Last
);
422 -- Find the first closing parenthesis
424 Char_Loop
: for J
in 1 .. Text_Last
loop
425 if Text_Line
(J
) = ')' then
427 Text_Line
(1 .. 4) = "Unit"
429 -- Add entry to SFN_Pragmas table
432 Name_Buffer
(1 .. Name_Len
) :=
433 Text_Line
(6 .. J
- 7);
436 File
=> File_Name_Id
,
438 Spec
=> Text_Line
(J
- 5 .. J
) =
441 SFN_Pragmas
.Increment_Last
;
443 (SFN_Pragmas
.Last
) := SFN_Prag
;
451 if Save_Last_Pragma_Index
= SFN_Pragmas
.Last
then
452 if Opt
.Verbose_Mode
then
453 Output
.Write_Line
(" not a unit");
457 if SFN_Pragmas
.Last
>
458 Save_Last_Pragma_Index
+ 1
460 for Index
in Save_Last_Pragma_Index
+ 1 ..
463 SFN_Pragmas
.Table
(Index
).Index
:=
464 Int
(Index
- Save_Last_Pragma_Index
);
468 for Index
in Save_Last_Pragma_Index
+ 1 ..
471 SFN_Prag
:= SFN_Pragmas
.Table
(Index
);
473 if Opt
.Verbose_Mode
then
474 if SFN_Prag
.Spec
then
475 Output
.Write_Str
(" spec of ");
478 Output
.Write_Str
(" body of ");
482 (Get_Name_String
(SFN_Prag
.Unit
));
487 -- Add the corresponding attribute in the
488 -- Naming package of the naming project.
491 Decl_Item
: constant Project_Node_Id
:=
497 Attribute
: constant Project_Node_Id
:=
500 N_Attribute_Declaration
,
503 Expression
: constant Project_Node_Id
:=
505 (Of_Kind
=> N_Expression
,
506 And_Expr_Kind
=> Single
,
509 Term
: constant Project_Node_Id
:=
512 And_Expr_Kind
=> Single
,
515 Value
: constant Project_Node_Id
:=
517 (Of_Kind
=> N_Literal_String
,
518 And_Expr_Kind
=> Single
,
522 Set_Next_Declarative_Item
524 To
=> First_Declarative_Item_Of
525 (Naming_Package
, Tree
),
527 Set_First_Declarative_Item_Of
531 Set_Current_Item_Node
536 -- Is it a spec or a body?
538 if SFN_Prag
.Spec
then
548 -- Get the name of the unit
550 Get_Name_String
(SFN_Prag
.Unit
);
551 To_Lower
(Name_Buffer
(1 .. Name_Len
));
552 Set_Associative_Array_Index_Of
553 (Attribute
, Tree
, To
=> Name_Find
);
556 (Attribute
, Tree
, To
=> Expression
);
558 (Expression
, Tree
, To
=> Term
);
560 (Term
, Tree
, To
=> Value
);
562 -- And set the name of the file
565 (Value
, Tree
, To
=> File_Name_Id
);
567 (Value
, Tree
, To
=> SFN_Prag
.Index
);
573 -- Add source file name to source list
577 Str
(Last
) := ASCII
.LF
;
579 if Write
(Source_List_FD
,
583 Prj
.Com
.Fail
("disk full");
590 Delete_File
(Temp_File_Name
.all, Success
);
594 -- File name matches none of the regular expressions
597 -- If file is not excluded, see if this is foreign source
599 if Matched
/= Excluded
then
600 for Index
in Foreign_Expressions
'Range loop
601 if Match
(Canon
(1 .. Last
),
602 Foreign_Expressions
(Index
))
613 Output
.Write_Line
("no match");
616 Output
.Write_Line
("excluded");
619 Output
.Write_Line
("foreign source");
623 if Project_File
and Matched
= True then
625 -- Add source file name to source list file
628 Str
(Last
) := ASCII
.LF
;
630 if Write
(Source_List_FD
,
634 Prj
.Com
.Fail
("disk full");
644 -- If Recursively is True, call itself for each subdirectory.
645 -- We do that, even when this directory has already been processed,
646 -- because all of its subdirectories may not have been processed.
649 Open
(Dir
, Dir_Name
);
652 Read
(Dir
, Str
, Last
);
655 -- Do not call itself for "." or ".."
658 (Dir_Name
& Directory_Separator
& Str
(1 .. Last
))
659 and then Str
(1 .. Last
) /= "."
660 and then Str
(1 .. Last
) /= ".."
663 (Dir_Name
& Directory_Separator
& Str
(1 .. Last
),
664 Recursively
=> True);
670 end Process_Directory
;
672 -- Start of processing for Make
675 -- Do some needed initializations
680 Prj
.Initialize
(No_Project_Tree
);
681 Prj
.Tree
.Initialize
(Tree
);
683 SFN_Pragmas
.Set_Last
(0);
685 Processed_Directories
.Set_Last
(0);
687 -- Initialize the compiler switches
689 Args
(1) := new String'("-c");
690 Args (2) := new String'("-gnats");
691 Args
(3) := new String'("-gnatu");
692 Args (4 .. 3 + Preproc_Switches'Length) := Preproc_Switches;
693 Args (4 + Preproc_Switches'Length) := new String'("-x");
694 Args
(5 + Preproc_Switches
'Length) := new String'("ada");
696 -- Get the path and file names
698 if File_Names_Case_Sensitive then
699 Path_Name (1 .. Path_Last) := File_Path;
701 Path_Name (1 .. Path_Last) := To_Lower (File_Path);
704 Path_Name (Path_Last + 1 .. Path_Name'Last) :=
705 Project_File_Extension;
707 -- Get the end of directory information, if any
709 for Index in reverse 1 .. Path_Last loop
710 if Path_Name (Index) = Directory_Separator then
711 Directory_Last := Index;
717 if Path_Last < Project_File_Extension'Length + 1
719 (Path_Last - Project_File_Extension'Length + 1 .. Path_Last)
720 /= Project_File_Extension
722 Path_Last := Path_Name'Last;
725 Output_Name (1 .. Path_Last) := To_Lower (Path_Name (1 .. Path_Last));
726 Output_Name_Last := Path_Last - Project_File_Extension'Length;
728 -- If there is already a project file with the specified name, parse
729 -- it to get the components that are not automatically generated.
731 if Is_Regular_File (Output_Name (1 .. Path_Last)) then
732 if Opt.Verbose_Mode then
733 Output.Write_Str ("Parsing already existing project file """);
734 Output.Write_Str (Output_Name (1 .. Output_Name_Last));
735 Output.Write_Line ("""");
740 Project => Project_Node,
741 Project_File_Name => Output_Name (1 .. Output_Name_Last),
742 Always_Errout_Finalize => False,
743 Store_Comments => True,
744 Current_Directory => Get_Current_Dir,
745 Packages_To_Check => Packages_To_Check_By_Gnatname);
747 -- Fail if parsing was not successful
749 if Project_Node = Empty_Node then
750 Fail ("parsing of existing project file failed");
753 -- If parsing was successful, remove the components that are
754 -- automatically generated, if any, so that they will be
755 -- unconditionally added later.
757 -- Remove the with clause for the naming project file
760 With_Clause : Project_Node_Id :=
761 First_With_Clause_Of (Project_Node, Tree);
762 Previous : Project_Node_Id := Empty_Node;
765 while With_Clause /= Empty_Node loop
766 if Prj.Tree.Name_Of (With_Clause, Tree) =
769 if Previous = Empty_Node then
770 Set_First_With_Clause_Of
772 To => Next_With_Clause_Of (With_Clause, Tree));
774 Set_Next_With_Clause_Of
776 To => Next_With_Clause_Of (With_Clause, Tree));
782 Previous := With_Clause;
783 With_Clause := Next_With_Clause_Of (With_Clause, Tree);
787 -- Remove attribute declarations of Source_Files,
788 -- Source_List_File, Source_Dirs, and the declaration of
789 -- package Naming, if they exist, but preserve the comments
790 -- attached to these nodes.
793 Declaration : Project_Node_Id :=
794 First_Declarative_Item_Of
795 (Project_Declaration_Of
796 (Project_Node, Tree),
798 Previous : Project_Node_Id := Empty_Node;
799 Current_Node : Project_Node_Id := Empty_Node;
802 Kind_Of_Node : Project_Node_Kind;
803 Comments : Project_Node_Id;
806 while Declaration /= Empty_Node loop
807 Current_Node := Current_Item_Node (Declaration, Tree);
809 Kind_Of_Node := Kind_Of (Current_Node, Tree);
811 if Kind_Of_Node = N_Attribute_Declaration or else
812 Kind_Of_Node = N_Package_Declaration
814 Name := Prj.Tree.Name_Of (Current_Node, Tree);
816 if Name = Name_Source_Files or else
817 Name = Name_Source_List_File or else
818 Name = Name_Source_Dirs or else
822 Tree.Project_Nodes.Table (Current_Node).Comments;
824 if Name = Name_Source_Files then
825 Source_Files_Comments := Comments;
827 elsif Name = Name_Source_List_File then
828 Source_List_File_Comments := Comments;
830 elsif Name = Name_Source_Dirs then
831 Source_Dirs_Comments := Comments;
833 elsif Name = Name_Naming then
834 Naming_Package_Comments := Comments;
837 if Previous = Empty_Node then
838 Set_First_Declarative_Item_Of
839 (Project_Declaration_Of (Project_Node, Tree),
841 To => Next_Declarative_Item
842 (Declaration, Tree));
845 Set_Next_Declarative_Item
847 To => Next_Declarative_Item
848 (Declaration, Tree));
852 Previous := Declaration;
856 Declaration := Next_Declarative_Item (Declaration, Tree);
862 if Directory_Last /= 0 then
863 Output_Name (1 .. Output_Name_Last - Directory_Last) :=
864 Output_Name (Directory_Last + 1 .. Output_Name_Last);
865 Output_Name_Last := Output_Name_Last - Directory_Last;
868 -- Get the project name id
870 Name_Len := Output_Name_Last;
871 Name_Buffer (1 .. Name_Len) := Output_Name (1 .. Name_Len);
872 Output_Name_Id := Name_Find;
874 -- Create the project naming file name
876 Project_Naming_Last := Output_Name_Last;
877 Project_Naming_File_Name (1 .. Project_Naming_Last) :=
878 Output_Name (1 .. Project_Naming_Last);
879 Project_Naming_File_Name
880 (Project_Naming_Last + 1 ..
881 Project_Naming_Last + Naming_File_Suffix'Length) :=
883 Project_Naming_Last :=
884 Project_Naming_Last + Naming_File_Suffix'Length;
886 -- Get the project naming id
888 Name_Len := Project_Naming_Last;
889 Name_Buffer (1 .. Name_Len) :=
890 Project_Naming_File_Name (1 .. Name_Len);
891 Project_Naming_Id := Name_Find;
893 Project_Naming_File_Name
894 (Project_Naming_Last + 1 ..
895 Project_Naming_Last + Project_File_Extension'Length) :=
896 Project_File_Extension;
897 Project_Naming_Last :=
898 Project_Naming_Last + Project_File_Extension'Length;
900 -- Create the source list file name
902 Source_List_Last := Output_Name_Last;
903 Source_List_Path (1 .. Source_List_Last) :=
904 Output_Name (1 .. Source_List_Last);
906 (Source_List_Last + 1 ..
907 Source_List_Last + Source_List_File_Suffix'Length) :=
908 Source_List_File_Suffix;
909 Source_List_Last := Source_List_Last + Source_List_File_Suffix'Length;
911 -- Add the project file extension to the project name
914 (Output_Name_Last + 1 ..
915 Output_Name_Last + Project_File_Extension'Length) :=
916 Project_File_Extension;
917 Output_Name_Last := Output_Name_Last + Project_File_Extension'Length;
920 -- Change the current directory to the directory of the project file,
921 -- if any directory information is specified.
923 if Directory_Last /= 0 then
925 Change_Dir (Path_Name (1 .. Directory_Last));
927 when Directory_Error =>
929 ("unknown directory """,
930 Path_Name (1 .. Directory_Last),
937 -- Delete the source list file, if it already exists
941 pragma Warnings (Off, Discard);
944 (Source_List_Path (1 .. Source_List_Last),
948 -- And create a new source list file.
949 -- Fail if file cannot be created.
951 Source_List_FD := Create_New_File
952 (Name => Source_List_Path (1 .. Source_List_Last),
955 if Source_List_FD = Invalid_FD then
957 ("cannot create file """,
958 Source_List_Path (1 .. Source_List_Last),
963 -- Compile the regular expressions. Fails immediately if any of
964 -- the specified strings is in error.
966 for Index in Excluded_Expressions'Range loop
968 Output.Write_Str ("Excluded pattern: """);
969 Output.Write_Str (Excluded_Patterns (Index).all);
970 Output.Write_Line ("""");
974 Excluded_Expressions (Index) :=
975 Compile (Pattern => Excluded_Patterns (Index).all, Glob => True);
977 when Error_In_Regexp =>
979 ("invalid regular expression """,
980 Excluded_Patterns (Index).all,
985 for Index in Foreign_Expressions'Range loop
987 Output.Write_Str ("Foreign pattern: """);
988 Output.Write_Str (Foreign_Patterns (Index).all);
989 Output.Write_Line ("""");
993 Foreign_Expressions (Index) :=
994 Compile (Pattern => Foreign_Patterns (Index).all, Glob => True);
996 when Error_In_Regexp =>
998 ("invalid regular expression """,
999 Foreign_Patterns (Index).all,
1004 for Index in Regular_Expressions'Range loop
1005 if Very_Verbose then
1006 Output.Write_Str ("Pattern: """);
1007 Output.Write_Str (Name_Patterns (Index).all);
1008 Output.Write_Line ("""");
1012 Regular_Expressions (Index) :=
1013 Compile (Pattern => Name_Patterns (Index).all, Glob => True);
1016 when Error_In_Regexp =>
1018 ("invalid regular expression """,
1019 Name_Patterns (Index).all,
1024 if Project_File then
1025 if Opt.Verbose_Mode then
1026 Output.Write_Str ("Naming project file name is """);
1028 (Project_Naming_File_Name (1 .. Project_Naming_Last));
1029 Output.Write_Line ("""");
1032 -- If there were no already existing project file, or if the parsing
1033 -- was unsuccessful, create an empty project node with the correct
1034 -- name and its project declaration node.
1036 if Project_Node = Empty_Node then
1038 Default_Project_Node (Of_Kind => N_Project, In_Tree => Tree);
1039 Set_Name_Of (Project_Node, Tree, To => Output_Name_Id);
1040 Set_Project_Declaration_Of
1041 (Project_Node, Tree,
1042 To => Default_Project_Node
1043 (Of_Kind => N_Project_Declaration, In_Tree => Tree));
1047 -- Create the naming project node, and add an attribute declaration
1048 -- for Source_Files as an empty list, to indicate there are no
1049 -- sources in the naming project.
1051 Project_Naming_Node :=
1052 Default_Project_Node (Of_Kind => N_Project, In_Tree => Tree);
1053 Set_Name_Of (Project_Naming_Node, Tree, To => Project_Naming_Id);
1054 Project_Naming_Decl :=
1055 Default_Project_Node
1056 (Of_Kind => N_Project_Declaration, In_Tree => Tree);
1057 Set_Project_Declaration_Of
1058 (Project_Naming_Node, Tree, Project_Naming_Decl);
1060 Default_Project_Node
1061 (Of_Kind => N_Package_Declaration, In_Tree => Tree);
1062 Set_Name_Of (Naming_Package, Tree, To => Name_Naming);
1065 Decl_Item : constant Project_Node_Id :=
1066 Default_Project_Node
1067 (Of_Kind => N_Declarative_Item, In_Tree => Tree);
1069 Attribute : constant Project_Node_Id :=
1070 Default_Project_Node
1071 (Of_Kind => N_Attribute_Declaration,
1073 And_Expr_Kind => List);
1075 Expression : constant Project_Node_Id :=
1076 Default_Project_Node
1077 (Of_Kind => N_Expression,
1079 And_Expr_Kind => List);
1081 Term : constant Project_Node_Id :=
1082 Default_Project_Node
1085 And_Expr_Kind => List);
1087 Empty_List : constant Project_Node_Id :=
1088 Default_Project_Node
1089 (Of_Kind => N_Literal_String_List,
1093 Set_First_Declarative_Item_Of
1094 (Project_Naming_Decl, Tree, To => Decl_Item);
1095 Set_Next_Declarative_Item (Decl_Item, Tree, Naming_Package);
1096 Set_Current_Item_Node (Decl_Item, Tree, To => Attribute);
1097 Set_Name_Of (Attribute, Tree, To => Name_Source_Files);
1098 Set_Expression_Of (Attribute, Tree, To => Expression);
1099 Set_First_Term (Expression, Tree, To => Term);
1100 Set_Current_Term (Term, Tree, To => Empty_List);
1103 -- Add a with clause on the naming project in the main project, if
1104 -- there is not already one.
1107 With_Clause : Project_Node_Id :=
1108 First_With_Clause_Of (Project_Node, Tree);
1111 while With_Clause /= Empty_Node loop
1113 Prj.Tree.Name_Of (With_Clause, Tree) = Project_Naming_Id;
1114 With_Clause := Next_With_Clause_Of (With_Clause, Tree);
1117 if With_Clause = Empty_Node then
1118 With_Clause := Default_Project_Node
1119 (Of_Kind => N_With_Clause, In_Tree => Tree);
1120 Set_Next_With_Clause_Of
1122 To => First_With_Clause_Of (Project_Node, Tree));
1123 Set_First_With_Clause_Of
1124 (Project_Node, Tree, To => With_Clause);
1125 Set_Name_Of (With_Clause, Tree, To => Project_Naming_Id);
1127 -- We set the project node to something different than
1128 -- Empty_Node, so that Prj.PP does not generate a limited
1131 Set_Project_Node_Of (With_Clause, Tree, Non_Empty_Node);
1133 Name_Len := Project_Naming_Last;
1134 Name_Buffer (1 .. Name_Len) :=
1135 Project_Naming_File_Name (1 .. Project_Naming_Last);
1136 Set_String_Value_Of (With_Clause, Tree, To => Name_Find);
1140 Project_Declaration := Project_Declaration_Of (Project_Node, Tree);
1142 -- Add a renaming declaration for package Naming in the main project
1145 Decl_Item : constant Project_Node_Id :=
1146 Default_Project_Node
1147 (Of_Kind => N_Declarative_Item,
1150 Naming : constant Project_Node_Id :=
1151 Default_Project_Node
1152 (Of_Kind => N_Package_Declaration,
1156 Set_Next_Declarative_Item
1158 To => First_Declarative_Item_Of (Project_Declaration, Tree));
1159 Set_First_Declarative_Item_Of
1160 (Project_Declaration, Tree, To => Decl_Item);
1161 Set_Current_Item_Node (Decl_Item, Tree, To => Naming);
1162 Set_Name_Of (Naming, Tree, To => Name_Naming);
1163 Set_Project_Of_Renamed_Package_Of
1164 (Naming, Tree, To => Project_Naming_Node);
1166 -- Attach the comments, if any, that were saved for package
1169 Tree.Project_Nodes.Table (Naming).Comments :=
1170 Naming_Package_Comments;
1173 -- Add an attribute declaration for Source_Dirs, initialized as an
1174 -- empty list. Directories will be added as they are read from the
1175 -- directory list file.
1178 Decl_Item : constant Project_Node_Id :=
1179 Default_Project_Node
1180 (Of_Kind => N_Declarative_Item,
1183 Attribute : constant Project_Node_Id :=
1184 Default_Project_Node
1185 (Of_Kind => N_Attribute_Declaration,
1187 And_Expr_Kind => List);
1189 Expression : constant Project_Node_Id :=
1190 Default_Project_Node
1191 (Of_Kind => N_Expression,
1193 And_Expr_Kind => List);
1195 Term : constant Project_Node_Id :=
1196 Default_Project_Node
1197 (Of_Kind => N_Term, In_Tree => Tree,
1198 And_Expr_Kind => List);
1201 Set_Next_Declarative_Item
1203 To => First_Declarative_Item_Of (Project_Declaration, Tree));
1204 Set_First_Declarative_Item_Of
1205 (Project_Declaration, Tree, To => Decl_Item);
1206 Set_Current_Item_Node (Decl_Item, Tree, To => Attribute);
1207 Set_Name_Of (Attribute, Tree, To => Name_Source_Dirs);
1208 Set_Expression_Of (Attribute, Tree, To => Expression);
1209 Set_First_Term (Expression, Tree, To => Term);
1211 Default_Project_Node
1212 (Of_Kind => N_Literal_String_List,
1214 And_Expr_Kind => List);
1215 Set_Current_Term (Term, Tree, To => Source_Dirs_List);
1217 -- Attach the comments, if any, that were saved for attribute
1220 Tree.Project_Nodes.Table (Attribute).Comments :=
1221 Source_Dirs_Comments;
1224 -- Add an attribute declaration for Source_List_File with the
1225 -- source list file name that will be created.
1228 Decl_Item : constant Project_Node_Id :=
1229 Default_Project_Node
1230 (Of_Kind => N_Declarative_Item,
1233 Attribute : constant Project_Node_Id :=
1234 Default_Project_Node
1235 (Of_Kind => N_Attribute_Declaration,
1237 And_Expr_Kind => Single);
1239 Expression : constant Project_Node_Id :=
1240 Default_Project_Node
1241 (Of_Kind => N_Expression,
1243 And_Expr_Kind => Single);
1245 Term : constant Project_Node_Id :=
1246 Default_Project_Node
1249 And_Expr_Kind => Single);
1251 Value : constant Project_Node_Id :=
1252 Default_Project_Node
1253 (Of_Kind => N_Literal_String,
1255 And_Expr_Kind => Single);
1258 Set_Next_Declarative_Item
1260 To => First_Declarative_Item_Of (Project_Declaration, Tree));
1261 Set_First_Declarative_Item_Of
1262 (Project_Declaration, Tree, To => Decl_Item);
1263 Set_Current_Item_Node (Decl_Item, Tree, To => Attribute);
1264 Set_Name_Of (Attribute, Tree, To => Name_Source_List_File);
1265 Set_Expression_Of (Attribute, Tree, To => Expression);
1266 Set_First_Term (Expression, Tree, To => Term);
1267 Set_Current_Term (Term, Tree, To => Value);
1268 Name_Len := Source_List_Last;
1269 Name_Buffer (1 .. Name_Len) :=
1270 Source_List_Path (1 .. Source_List_Last);
1271 Set_String_Value_Of (Value, Tree, To => Name_Find);
1273 -- If there was no comments for attribute Source_List_File, put
1274 -- those for Source_Files, if they exist.
1276 if Source_List_File_Comments /= Empty_Node then
1277 Tree.Project_Nodes.Table (Attribute).Comments :=
1278 Source_List_File_Comments;
1280 Tree.Project_Nodes.Table (Attribute).Comments :=
1281 Source_Files_Comments;
1286 -- Process each directory
1288 for Index in Directories'Range loop
1291 Dir_Name : constant String := Directories (Index).all;
1292 Last : Natural := Dir_Name'Last;
1293 Recursively : Boolean := False;
1296 if Dir_Name'Length >= 4
1297 and then (Dir_Name (Last - 2 .. Last) = "/**")
1300 Recursively := True;
1303 if Project_File then
1305 -- Add the directory in the list for attribute Source_Dirs
1308 Expression : constant Project_Node_Id :=
1309 Default_Project_Node
1310 (Of_Kind => N_Expression,
1312 And_Expr_Kind => Single);
1314 Term : constant Project_Node_Id :=
1315 Default_Project_Node
1318 And_Expr_Kind => Single);
1320 Value : constant Project_Node_Id :=
1321 Default_Project_Node
1322 (Of_Kind => N_Literal_String,
1324 And_Expr_Kind => Single);
1327 if Current_Source_Dir = Empty_Node then
1328 Set_First_Expression_In_List
1329 (Source_Dirs_List, Tree, To => Expression);
1331 Set_Next_Expression_In_List
1332 (Current_Source_Dir, Tree, To => Expression);
1335 Current_Source_Dir := Expression;
1336 Set_First_Term (Expression, Tree, To => Term);
1337 Set_Current_Term (Term, Tree, To => Value);
1338 Name_Len := Dir_Name'Length;
1339 Name_Buffer (1 .. Name_Len) := Dir_Name;
1340 Set_String_Value_Of (Value, Tree, To => Name_Find);
1344 Process_Directory (Dir_Name (Dir_Name'First .. Last), Recursively);
1349 if Project_File then
1350 Close (Source_List_FD);
1355 pragma Warnings (Off, Discard);
1358 -- Delete the file if it already exists
1361 (Path_Name (Directory_Last + 1 .. Path_Last),
1362 Success => Discard);
1366 if Opt.Verbose_Mode then
1367 Output.Write_Str ("Creating new file """);
1368 Output.Write_Str (Path_Name (Directory_Last + 1 .. Path_Last));
1369 Output.Write_Line ("""");
1372 Output_FD := Create_New_File
1373 (Path_Name (Directory_Last + 1 .. Path_Last),
1376 -- Fails if project file cannot be created
1378 if Output_FD = Invalid_FD then
1380 ("cannot create new """, Path_Name (1 .. Path_Last), """");
1383 if Project_File then
1385 -- Output the project file
1388 (Project_Node, Tree,
1389 W_Char => Write_A_Char'Access,
1390 W_Eol => Write_Eol'Access,
1391 W_Str => Write_A_String'Access,
1392 Backward_Compatibility => False);
1395 -- Delete the naming project file if it already exists
1398 (Project_Naming_File_Name (1 .. Project_Naming_Last),
1399 Success => Discard);
1403 if Opt.Verbose_Mode then
1404 Output.Write_Str ("Creating new naming project file """);
1405 Output.Write_Str (Project_Naming_File_Name
1406 (1 .. Project_Naming_Last));
1407 Output.Write_Line ("""");
1410 Output_FD := Create_New_File
1411 (Project_Naming_File_Name (1 .. Project_Naming_Last),
1414 -- Fails if naming project file cannot be created
1416 if Output_FD = Invalid_FD then
1418 ("cannot create new """,
1419 Project_Naming_File_Name (1 .. Project_Naming_Last),
1423 -- Output the naming project file
1426 (Project_Naming_Node, Tree,
1427 W_Char => Write_A_Char'Access,
1428 W_Eol => Write_Eol'Access,
1429 W_Str => Write_A_String'Access,
1430 Backward_Compatibility => False);
1434 -- Write to the output file each entry in the SFN_Pragmas table
1435 -- as an pragma Source_File_Name.
1437 for Index in 1 .. SFN_Pragmas.Last loop
1438 Write_A_String ("pragma Source_File_Name");
1440 Write_A_String (" (");
1442 (Get_Name_String (SFN_Pragmas.Table (Index).Unit));
1443 Write_A_String (",");
1446 if SFN_Pragmas.Table (Index).Spec then
1447 Write_A_String (" Spec_File_Name => """);
1450 Write_A_String (" Body_File_Name => """);
1454 (Get_Name_String (SFN_Pragmas.Table (Index).File));
1456 Write_A_String ("""");
1458 if SFN_Pragmas.Table (Index).Index /= 0 then
1459 Write_A_String (", Index =>");
1460 Write_A_String (SFN_Pragmas.Table (Index).Index'Img);
1463 Write_A_String (");");
1476 procedure Write_A_Char (C : Character) is
1478 Write_A_String ((1 => C));
1485 procedure Write_Eol is
1487 Write_A_String ((1 => ASCII.LF));
1490 --------------------
1491 -- Write_A_String --
1492 --------------------
1494 procedure Write_A_String (S : String) is
1495 Str : String (1 .. S'Length);
1498 if S'Length > 0 then
1501 if Write (Output_FD, Str (1)'Address, Str'Length) /= Str'Length then
1502 Prj.Com.Fail ("disk full");