1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2003 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 Snames
; use Snames
;
38 with Table
; use Table
;
40 with Ada
.Characters
.Handling
; use Ada
.Characters
.Handling
;
41 with GNAT
.Directory_Operations
; use GNAT
.Directory_Operations
;
42 with GNAT
.Expect
; use GNAT
.Expect
;
43 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
44 with GNAT
.Regexp
; use GNAT
.Regexp
;
45 with GNAT
.Regpat
; use GNAT
.Regpat
;
47 package body Prj
.Makr
is
49 Non_Empty_Node
: constant Project_Node_Id
:= 1;
50 -- Used for the With_Clause of the naming project
52 type Matched_Type
is (True, False, Excluded
);
54 Naming_File_Suffix
: constant String := "_naming";
55 Source_List_File_Suffix
: constant String := "_source_list.txt";
57 Output_FD
: File_Descriptor
;
58 -- To save the project file and its naming project file.
61 -- Output an empty line.
63 procedure Write_A_Char
(C
: Character);
64 -- Write one character to Output_FD
66 procedure Write_A_String
(S
: String);
67 -- Write a String to Output_FD
69 package Processed_Directories
is new Table
.Table
70 (Table_Component_Type
=> String_Access
,
71 Table_Index_Type
=> Natural,
74 Table_Increment
=> 10,
75 Table_Name
=> "Prj.Makr.Processed_Directories");
83 Project_File
: Boolean;
84 Directories
: Argument_List
;
85 Name_Patterns
: Argument_List
;
86 Excluded_Patterns
: Argument_List
;
87 Foreign_Patterns
: Argument_List
;
88 Preproc_Switches
: Argument_List
;
89 Very_Verbose
: Boolean)
91 Path_Name
: String (1 .. File_Path
'Length +
92 Project_File_Extension
'Length);
93 Path_Last
: Natural := File_Path
'Length;
95 Directory_Last
: Natural := 0;
97 Output_Name
: String (Path_Name
'Range);
98 Output_Name_Last
: Natural;
99 Output_Name_Id
: Name_Id
;
101 Project_Node
: Project_Node_Id
:= Empty_Node
;
102 Project_Declaration
: Project_Node_Id
:= Empty_Node
;
103 Source_Dirs_List
: Project_Node_Id
:= Empty_Node
;
104 Current_Source_Dir
: Project_Node_Id
:= Empty_Node
;
106 Project_Naming_Node
: Project_Node_Id
:= Empty_Node
;
107 Project_Naming_Decl
: Project_Node_Id
:= Empty_Node
;
108 Naming_Package
: Project_Node_Id
:= Empty_Node
;
110 Project_Naming_File_Name
: String (1 .. Output_Name
'Length +
111 Naming_File_Suffix
'Length);
113 Project_Naming_Last
: Natural;
114 Project_Naming_Id
: Name_Id
:= No_Name
;
116 Excluded_Expressions
: array (Excluded_Patterns
'Range) of Regexp
;
117 Regular_Expressions
: array (Name_Patterns
'Range) of Regexp
;
118 Foreign_Expressions
: array (Foreign_Patterns
'Range) of Regexp
;
120 Source_List_Path
: String (1 .. Output_Name
'Length +
121 Source_List_File_Suffix
'Length);
122 Source_List_Last
: Natural;
124 Source_List_FD
: File_Descriptor
;
126 Matcher
: constant Pattern_Matcher
:=
127 Compile
(Expression
=> "expected|Unit.*\)|No such");
129 Args
: Argument_List
(1 .. Preproc_Switches
'Length + 6);
130 -- (1 => new String'("-c"),
131 -- 2 => new String'("-gnats"),
132 -- 3 => new String'("-gnatu"),
133 -- 4 => new String'("-x"),
134 -- 5 => new String'("ada"),
137 type SFN_Pragma
is record
138 Unit
: String_Access
;
139 File
: String_Access
;
143 package SFN_Pragmas
is new Table
.Table
144 (Table_Component_Type
=> SFN_Pragma
,
145 Table_Index_Type
=> Natural,
146 Table_Low_Bound
=> 0,
148 Table_Increment
=> 50,
149 Table_Name
=> "Prj.Makr.SFN_Pragmas");
151 procedure Process_Directory
(Dir_Name
: String; Recursively
: Boolean);
152 -- Look for Ada and foreign sources in a directory, according to the
153 -- patterns. When Recursively is True, after looking for sources in
154 -- Dir_Name, look also in its subdirectories, if any.
156 -----------------------
157 -- Process_Directory --
158 -----------------------
160 procedure Process_Directory
(Dir_Name
: String; Recursively
: Boolean) is
161 Matched
: Matched_Type
:= False;
162 Str
: String (1 .. 2_000
);
165 Process
: Boolean := True;
168 if Opt
.Verbose_Mode
then
169 Output
.Write_Str
("Processing directory """);
170 Output
.Write_Str
(Dir_Name
);
171 Output
.Write_Line
("""");
174 -- Avoid processing several times the same directory.
176 for Index
in 1 .. Processed_Directories
.Last
loop
177 if Processed_Directories
.Table
(Index
).all = Dir_Name
then
184 Processed_Directories
. Increment_Last
;
185 Processed_Directories
.Table
(Processed_Directories
.Last
) :=
186 new String'(Dir_Name);
187 -- Get the source file names from the directory.
188 -- Fails if the directory does not exist.
191 Open (Dir, Dir_Name);
194 when Directory_Error =>
195 Prj.Com.Fail ("cannot open directory """, Dir_Name, """");
198 -- Process each regular file in the directory
201 Read (Dir, Str, Last);
205 (Dir_Name & Directory_Separator & Str (1 .. Last))
209 -- First, check if the file name matches at least one of
210 -- the excluded expressions;
212 for Index in Excluded_Expressions'Range loop
214 Match (Str (1 .. Last), Excluded_Expressions (Index))
221 -- If it does not match any of the excluded expressions,
222 -- check if the file name matches at least one of the
223 -- regular expressions.
225 if Matched = True then
228 for Index in Regular_Expressions'Range loop
230 Match (Str (1 .. Last), Regular_Expressions (Index))
239 or else (Matched = True and then Opt.Verbose_Mode)
241 Output.Write_Str (" Checking """);
242 Output.Write_Str (Str (1 .. Last));
243 Output.Write_Str (""": ");
246 -- If the file name matches one of the regular expressions,
247 -- parse it to get its unit name.
249 if Matched = True then
251 PD : Process_Descriptor;
252 Result : Expect_Match;
255 Args (Args'Last) := new String'
257 Directory_Separator
&
262 (PD
, "gcc", Args
, Err_To_Out
=> True);
263 Expect
(PD
, Result
, Matcher
);
267 if Opt
.Verbose_Mode
then
268 Output
.Write_Str
("(process died) ");
271 Result
:= Expect_Timeout
;
274 if Result
/= Expect_Timeout
then
276 -- If we got a unit name, this is a valid source
280 S
: constant String := Expect_Out_Match
(PD
);
284 and then S
(S
'First .. S
'First + 3) = "Unit"
286 if Opt
.Verbose_Mode
then
288 (S
(S
'Last - 4 .. S
'Last - 1));
289 Output
.Write_Str
(" of ");
291 (S
(S
'First + 5 .. S
'Last - 7));
296 -- Add the corresponding attribute in the
297 -- Naming package of the naming project.
300 Decl_Item
: constant Project_Node_Id
:=
305 Attribute
: constant Project_Node_Id
:=
308 N_Attribute_Declaration
);
310 Expression
: constant Project_Node_Id
:=
312 (Of_Kind
=> N_Expression
,
313 And_Expr_Kind
=> Single
);
315 Term
: constant Project_Node_Id
:=
318 And_Expr_Kind
=> Single
);
320 Value
: constant Project_Node_Id
:=
322 (Of_Kind
=> N_Literal_String
,
323 And_Expr_Kind
=> Single
);
326 Set_Next_Declarative_Item
328 To
=> First_Declarative_Item_Of
330 Set_First_Declarative_Item_Of
331 (Naming_Package
, To
=> Decl_Item
);
332 Set_Current_Item_Node
333 (Decl_Item
, To
=> Attribute
);
336 S
(S
'Last - 5 .. S
'Last) = "(spec)"
339 (Attribute
, To
=> Name_Spec
);
346 Name_Len
:= S
'Last - S
'First - 11;
347 Name_Buffer
(1 .. Name_Len
) :=
349 (S
(S
'First + 5 .. S
'Last - 7)));
350 Set_Associative_Array_Index_Of
351 (Attribute
, To
=> Name_Find
);
354 (Attribute
, To
=> Expression
);
355 Set_First_Term
(Expression
, To
=> Term
);
356 Set_Current_Term
(Term
, To
=> Value
);
359 Name_Buffer
(1 .. Name_Len
) :=
362 (Value
, To
=> Name_Find
);
365 -- Add source file name to source list
369 Str
(Last
) := ASCII
.LF
;
371 if Write
(Source_List_FD
,
375 Prj
.Com
.Fail
("disk full");
378 -- Add an entry in the SFN_Pragmas table
380 SFN_Pragmas
.Increment_Last
;
381 SFN_Pragmas
.Table
(SFN_Pragmas
.Last
) :=
383 (S (S'First + 5 .. S'Last - 7)),
384 File => new String'(Str
(1 .. Last
)),
385 Spec
=> S
(S
'Last - 5 .. S
'Last)
390 if Opt
.Verbose_Mode
then
391 Output
.Write_Line
("not a unit");
397 if Opt
.Verbose_Mode
then
398 Output
.Write_Line
("not a unit");
406 if Matched
= False then
407 -- Look if this is a foreign source
409 for Index
in Foreign_Expressions
'Range loop
410 if Match
(Str
(1 .. Last
),
411 Foreign_Expressions
(Index
))
422 Output
.Write_Line
("no match");
425 Output
.Write_Line
("excluded");
428 Output
.Write_Line
("foreign source");
432 if Project_File
and Matched
= True then
434 -- Add source file name to source list file
437 Str
(Last
) := ASCII
.LF
;
439 if Write
(Source_List_FD
,
443 Prj
.Com
.Fail
("disk full");
453 -- If Recursively is True, call itself for each subdirectory.
454 -- We do that, even when this directory has already been processed,
455 -- because all of its subdirectories may not have been processed.
458 Open
(Dir
, Dir_Name
);
461 Read
(Dir
, Str
, Last
);
464 -- Do not call itself for "." or ".."
467 (Dir_Name
& Directory_Separator
& Str
(1 .. Last
))
468 and then Str
(1 .. Last
) /= "."
469 and then Str
(1 .. Last
) /= ".."
472 (Dir_Name
& Directory_Separator
& Str
(1 .. Last
),
473 Recursively
=> True);
479 end Process_Directory
;
481 -- Start of processing for Make
484 -- Do some needed initializations
491 SFN_Pragmas
.Set_Last
(0);
493 Processed_Directories
.Set_Last
(0);
495 -- Initialize the compiler switches
497 Args
(1) := new String'("-c");
498 Args (2) := new String'("-gnats");
499 Args
(3) := new String'("-gnatu");
500 Args (4 .. 3 + Preproc_Switches'Length) := Preproc_Switches;
501 Args (4 + Preproc_Switches'Length) := new String'("-x");
502 Args
(5 + Preproc_Switches
'Length) := new String'("ada");
504 -- Get the path and file names
506 if File_Names_Case_Sensitive then
507 Path_Name (1 .. Path_Last) := File_Path;
509 Path_Name (1 .. Path_Last) := To_Lower (File_Path);
512 Path_Name (Path_Last + 1 .. Path_Name'Last) :=
513 Project_File_Extension;
515 -- Get the end of directory information, if any
517 for Index in reverse 1 .. Path_Last loop
518 if Path_Name (Index) = Directory_Separator then
519 Directory_Last := Index;
525 if Path_Last < Project_File_Extension'Length + 1
527 (Path_Last - Project_File_Extension'Length + 1 .. Path_Last)
528 /= Project_File_Extension
530 Path_Last := Path_Name'Last;
533 Output_Name (1 .. Path_Last) := To_Lower (Path_Name (1 .. Path_Last));
534 Output_Name_Last := Path_Last - Project_File_Extension'Length;
536 if Directory_Last /= 0 then
537 Output_Name (1 .. Output_Name_Last - Directory_Last) :=
538 Output_Name (Directory_Last + 1 .. Output_Name_Last);
539 Output_Name_Last := Output_Name_Last - Directory_Last;
542 -- Get the project name id
544 Name_Len := Output_Name_Last;
545 Name_Buffer (1 .. Name_Len) := Output_Name (1 .. Name_Len);
546 Output_Name_Id := Name_Find;
548 -- Create the project naming file name
550 Project_Naming_Last := Output_Name_Last;
551 Project_Naming_File_Name (1 .. Project_Naming_Last) :=
552 Output_Name (1 .. Project_Naming_Last);
553 Project_Naming_File_Name
554 (Project_Naming_Last + 1 ..
555 Project_Naming_Last + Naming_File_Suffix'Length) :=
557 Project_Naming_Last :=
558 Project_Naming_Last + Naming_File_Suffix'Length;
560 -- Get the project naming id
562 Name_Len := Project_Naming_Last;
563 Name_Buffer (1 .. Name_Len) :=
564 Project_Naming_File_Name (1 .. Name_Len);
565 Project_Naming_Id := Name_Find;
567 Project_Naming_File_Name
568 (Project_Naming_Last + 1 ..
569 Project_Naming_Last + Project_File_Extension'Length) :=
570 Project_File_Extension;
571 Project_Naming_Last :=
572 Project_Naming_Last + Project_File_Extension'Length;
574 -- Create the source list file name
576 Source_List_Last := Output_Name_Last;
577 Source_List_Path (1 .. Source_List_Last) :=
578 Output_Name (1 .. Source_List_Last);
580 (Source_List_Last + 1 ..
581 Source_List_Last + Source_List_File_Suffix'Length) :=
582 Source_List_File_Suffix;
583 Source_List_Last := Source_List_Last + Source_List_File_Suffix'Length;
585 -- Add the project file extension to the project name
588 (Output_Name_Last + 1 ..
589 Output_Name_Last + Project_File_Extension'Length) :=
590 Project_File_Extension;
591 Output_Name_Last := Output_Name_Last + Project_File_Extension'Length;
594 -- Change the current directory to the directory of the project file,
595 -- if any directory information is specified.
597 if Directory_Last /= 0 then
599 Change_Dir (Path_Name (1 .. Directory_Last));
601 when Directory_Error =>
603 ("unknown directory """,
604 Path_Name (1 .. Directory_Last),
611 -- Delete the source list file, if it already exists
618 (Source_List_Path (1 .. Source_List_Last),
622 -- And create a new source list file.
623 -- Fail if file cannot be created.
625 Source_List_FD := Create_New_File
626 (Name => Source_List_Path (1 .. Source_List_Last),
629 if Source_List_FD = Invalid_FD then
631 ("cannot create file """,
632 Source_List_Path (1 .. Source_List_Last),
637 -- Compile the regular expressions. Fails immediately if any of
638 -- the specified strings is in error.
640 for Index in Excluded_Expressions'Range loop
642 Output.Write_Str ("Excluded pattern: """);
643 Output.Write_Str (Excluded_Patterns (Index).all);
644 Output.Write_Line ("""");
648 Excluded_Expressions (Index) :=
649 Compile (Pattern => Excluded_Patterns (Index).all, Glob => True);
652 when Error_In_Regexp =>
654 ("invalid regular expression """,
655 Excluded_Patterns (Index).all,
660 for Index in Foreign_Expressions'Range loop
662 Output.Write_Str ("Foreign pattern: """);
663 Output.Write_Str (Foreign_Patterns (Index).all);
664 Output.Write_Line ("""");
668 Foreign_Expressions (Index) :=
669 Compile (Pattern => Foreign_Patterns (Index).all, Glob => True);
672 when Error_In_Regexp =>
674 ("invalid regular expression """,
675 Foreign_Patterns (Index).all,
680 for Index in Regular_Expressions'Range loop
682 Output.Write_Str ("Pattern: """);
683 Output.Write_Str (Name_Patterns (Index).all);
684 Output.Write_Line ("""");
688 Regular_Expressions (Index) :=
689 Compile (Pattern => Name_Patterns (Index).all, Glob => True);
692 when Error_In_Regexp =>
694 ("invalid regular expression """,
695 Name_Patterns (Index).all,
701 if Opt.Verbose_Mode then
702 Output.Write_Str ("Naming project file name is """);
704 (Project_Naming_File_Name (1 .. Project_Naming_Last));
705 Output.Write_Line ("""");
708 -- If there is already a project file with the specified name,
709 -- parse it to get the components that are not automatically
712 if Is_Regular_File (Output_Name (1 .. Output_Name_Last)) then
713 if Opt.Verbose_Mode then
714 Output.Write_Str ("Parsing already existing project file """);
715 Output.Write_Str (Output_Name (1 .. Output_Name_Last));
716 Output.Write_Line ("""");
720 (Project => Project_Node,
721 Project_File_Name => Output_Name (1 .. Output_Name_Last),
722 Always_Errout_Finalize => False);
724 -- If parsing was successful, remove the components that are
725 -- automatically generated, if any, so that they will be
726 -- unconditionally added later.
728 if Project_Node /= Empty_Node then
730 -- Remove the with clause for the naming project file
733 With_Clause : Project_Node_Id :=
734 First_With_Clause_Of (Project_Node);
735 Previous : Project_Node_Id := Empty_Node;
738 while With_Clause /= Empty_Node loop
739 if Tree.Name_Of (With_Clause) = Project_Naming_Id then
740 if Previous = Empty_Node then
741 Set_First_With_Clause_Of
743 To => Next_With_Clause_Of (With_Clause));
745 Set_Next_With_Clause_Of
747 To => Next_With_Clause_Of (With_Clause));
753 Previous := With_Clause;
754 With_Clause := Next_With_Clause_Of (With_Clause);
758 -- Remove attribute declarations of Source_Files,
759 -- Source_List_File, Source_Dirs, and the declaration of
760 -- package Naming, if they exist.
763 Declaration : Project_Node_Id :=
764 First_Declarative_Item_Of
765 (Project_Declaration_Of
767 Previous : Project_Node_Id := Empty_Node;
768 Current_Node : Project_Node_Id := Empty_Node;
771 while Declaration /= Empty_Node loop
772 Current_Node := Current_Item_Node (Declaration);
774 if (Kind_Of (Current_Node) = N_Attribute_Declaration
776 (Tree.Name_Of (Current_Node) = Name_Source_Files
777 or else Tree.Name_Of (Current_Node) =
778 Name_Source_List_File
779 or else Tree.Name_Of (Current_Node) =
782 (Kind_Of (Current_Node) = N_Package_Declaration
783 and then Tree.Name_Of (Current_Node) = Name_Naming)
785 if Previous = Empty_Node then
786 Set_First_Declarative_Item_Of
787 (Project_Declaration_Of (Project_Node),
788 To => Next_Declarative_Item (Declaration));
791 Set_Next_Declarative_Item
793 To => Next_Declarative_Item (Declaration));
797 Previous := Declaration;
800 Declaration := Next_Declarative_Item (Declaration);
806 -- If there were no already existing project file, or if the parsing
807 -- was unsuccessful, create an empty project node with the correct
808 -- name and its project declaration node.
810 if Project_Node = Empty_Node then
811 Project_Node := Default_Project_Node (Of_Kind => N_Project);
812 Set_Name_Of (Project_Node, To => Output_Name_Id);
813 Set_Project_Declaration_Of
815 To => Default_Project_Node (Of_Kind => N_Project_Declaration));
819 -- Create the naming project node, and add an attribute declaration
820 -- for Source_Files as an empty list, to indicate there are no
821 -- sources in the naming project.
823 Project_Naming_Node := Default_Project_Node (Of_Kind => N_Project);
824 Set_Name_Of (Project_Naming_Node, To => Project_Naming_Id);
825 Project_Naming_Decl :=
826 Default_Project_Node (Of_Kind => N_Project_Declaration);
827 Set_Project_Declaration_Of (Project_Naming_Node, Project_Naming_Decl);
829 Default_Project_Node (Of_Kind => N_Package_Declaration);
830 Set_Name_Of (Naming_Package, To => Name_Naming);
833 Decl_Item : constant Project_Node_Id :=
834 Default_Project_Node (Of_Kind => N_Declarative_Item);
836 Attribute : constant Project_Node_Id :=
838 (Of_Kind => N_Attribute_Declaration,
839 And_Expr_Kind => List);
841 Expression : constant Project_Node_Id :=
843 (Of_Kind => N_Expression,
844 And_Expr_Kind => List);
846 Term : constant Project_Node_Id :=
849 And_Expr_Kind => List);
851 Empty_List : constant Project_Node_Id :=
853 (Of_Kind => N_Literal_String_List);
856 Set_First_Declarative_Item_Of
857 (Project_Naming_Decl, To => Decl_Item);
858 Set_Next_Declarative_Item (Decl_Item, Naming_Package);
859 Set_Current_Item_Node (Decl_Item, To => Attribute);
860 Set_Name_Of (Attribute, To => Name_Source_Files);
861 Set_Expression_Of (Attribute, To => Expression);
862 Set_First_Term (Expression, To => Term);
863 Set_Current_Term (Term, To => Empty_List);
866 -- Add a with clause on the naming project in the main project
869 With_Clause : constant Project_Node_Id :=
870 Default_Project_Node (Of_Kind => N_With_Clause);
873 Set_Next_With_Clause_Of
874 (With_Clause, To => First_With_Clause_Of (Project_Node));
875 Set_First_With_Clause_Of (Project_Node, To => With_Clause);
876 Set_Name_Of (With_Clause, To => Project_Naming_Id);
878 -- We set the project node to something different than
879 -- Empty_Node, so that Prj.PP does not generate a limited
882 Set_Project_Node_Of (With_Clause, Non_Empty_Node);
884 Name_Len := Project_Naming_Last;
885 Name_Buffer (1 .. Name_Len) :=
886 Project_Naming_File_Name (1 .. Project_Naming_Last);
887 Set_String_Value_Of (With_Clause, To => Name_Find);
890 Project_Declaration := Project_Declaration_Of (Project_Node);
892 -- Add a renaming declaration for package Naming in the main project
895 Decl_Item : constant Project_Node_Id :=
896 Default_Project_Node (Of_Kind => N_Declarative_Item);
898 Naming : constant Project_Node_Id :=
899 Default_Project_Node (Of_Kind => N_Package_Declaration);
901 Set_Next_Declarative_Item
903 To => First_Declarative_Item_Of (Project_Declaration));
904 Set_First_Declarative_Item_Of
905 (Project_Declaration, To => Decl_Item);
906 Set_Current_Item_Node (Decl_Item, To => Naming);
907 Set_Name_Of (Naming, To => Name_Naming);
908 Set_Project_Of_Renamed_Package_Of
909 (Naming, To => Project_Naming_Node);
912 -- Add an attribute declaration for Source_Dirs, initialized as an
913 -- empty list. Directories will be added as they are read from the
914 -- directory list file.
917 Decl_Item : constant Project_Node_Id :=
918 Default_Project_Node (Of_Kind => N_Declarative_Item);
920 Attribute : constant Project_Node_Id :=
922 (Of_Kind => N_Attribute_Declaration,
923 And_Expr_Kind => List);
925 Expression : constant Project_Node_Id :=
927 (Of_Kind => N_Expression,
928 And_Expr_Kind => List);
930 Term : constant Project_Node_Id :=
932 (Of_Kind => N_Term, And_Expr_Kind => List);
935 Set_Next_Declarative_Item
937 To => First_Declarative_Item_Of (Project_Declaration));
938 Set_First_Declarative_Item_Of
939 (Project_Declaration, To => Decl_Item);
940 Set_Current_Item_Node (Decl_Item, To => Attribute);
941 Set_Name_Of (Attribute, To => Name_Source_Dirs);
942 Set_Expression_Of (Attribute, To => Expression);
943 Set_First_Term (Expression, To => Term);
945 Default_Project_Node (Of_Kind => N_Literal_String_List,
946 And_Expr_Kind => List);
947 Set_Current_Term (Term, To => Source_Dirs_List);
950 -- Add an attribute declaration for Source_List_File with the
951 -- source list file name that will be created.
954 Decl_Item : constant Project_Node_Id :=
955 Default_Project_Node (Of_Kind => N_Declarative_Item);
957 Attribute : constant Project_Node_Id :=
959 (Of_Kind => N_Attribute_Declaration,
960 And_Expr_Kind => Single);
962 Expression : constant Project_Node_Id :=
964 (Of_Kind => N_Expression,
965 And_Expr_Kind => Single);
967 Term : constant Project_Node_Id :=
970 And_Expr_Kind => Single);
972 Value : constant Project_Node_Id :=
974 (Of_Kind => N_Literal_String,
975 And_Expr_Kind => Single);
978 Set_Next_Declarative_Item
980 To => First_Declarative_Item_Of (Project_Declaration));
981 Set_First_Declarative_Item_Of
982 (Project_Declaration, To => Decl_Item);
983 Set_Current_Item_Node (Decl_Item, To => Attribute);
984 Set_Name_Of (Attribute, To => Name_Source_List_File);
985 Set_Expression_Of (Attribute, To => Expression);
986 Set_First_Term (Expression, To => Term);
987 Set_Current_Term (Term, To => Value);
988 Name_Len := Source_List_Last;
989 Name_Buffer (1 .. Name_Len) :=
990 Source_List_Path (1 .. Source_List_Last);
991 Set_String_Value_Of (Value, To => Name_Find);
995 -- Process each directory
997 for Index in Directories'Range loop
1000 Dir_Name : constant String := Directories (Index).all;
1001 Last : Natural := Dir_Name'Last;
1002 Recursively : Boolean := False;
1004 if Dir_Name'Length >= 4
1005 and then (Dir_Name (Last - 2 .. Last) = "/**")
1008 Recursively := True;
1011 if Project_File then
1013 -- Add the directory in the list for attribute Source_Dirs
1016 Expression : constant Project_Node_Id :=
1017 Default_Project_Node
1018 (Of_Kind => N_Expression,
1019 And_Expr_Kind => Single);
1021 Term : constant Project_Node_Id :=
1022 Default_Project_Node
1024 And_Expr_Kind => Single);
1026 Value : constant Project_Node_Id :=
1027 Default_Project_Node
1028 (Of_Kind => N_Literal_String,
1029 And_Expr_Kind => Single);
1032 if Current_Source_Dir = Empty_Node then
1033 Set_First_Expression_In_List
1034 (Source_Dirs_List, To => Expression);
1036 Set_Next_Expression_In_List
1037 (Current_Source_Dir, To => Expression);
1040 Current_Source_Dir := Expression;
1041 Set_First_Term (Expression, To => Term);
1042 Set_Current_Term (Term, To => Value);
1043 Name_Len := Dir_Name'Length;
1044 Name_Buffer (1 .. Name_Len) := Dir_Name;
1045 Set_String_Value_Of (Value, To => Name_Find);
1049 Process_Directory (Dir_Name (Dir_Name'First .. Last), Recursively);
1054 if Project_File then
1055 Close (Source_List_FD);
1062 -- Delete the file if it already exists
1065 (Path_Name (Directory_Last + 1 .. Path_Last),
1066 Success => Discard);
1070 if Opt.Verbose_Mode then
1071 Output.Write_Str ("Creating new file """);
1072 Output.Write_Str (Path_Name (Directory_Last + 1 .. Path_Last));
1073 Output.Write_Line ("""");
1076 Output_FD := Create_New_File
1077 (Path_Name (Directory_Last + 1 .. Path_Last),
1080 -- Fails if project file cannot be created
1082 if Output_FD = Invalid_FD then
1084 ("cannot create new """, Path_Name (1 .. Path_Last), """");
1087 if Project_File then
1089 -- Output the project file
1093 W_Char => Write_A_Char'Access,
1094 W_Eol => Write_Eol'Access,
1095 W_Str => Write_A_String'Access,
1096 Backward_Compatibility => False);
1099 -- Delete the naming project file if it already exists
1102 (Project_Naming_File_Name (1 .. Project_Naming_Last),
1103 Success => Discard);
1107 if Opt.Verbose_Mode then
1108 Output.Write_Str ("Creating new naming project file """);
1109 Output.Write_Str (Project_Naming_File_Name
1110 (1 .. Project_Naming_Last));
1111 Output.Write_Line ("""");
1114 Output_FD := Create_New_File
1115 (Project_Naming_File_Name (1 .. Project_Naming_Last),
1118 -- Fails if naming project file cannot be created
1120 if Output_FD = Invalid_FD then
1122 ("cannot create new """,
1123 Project_Naming_File_Name (1 .. Project_Naming_Last),
1127 -- Output the naming project file
1130 (Project_Naming_Node,
1131 W_Char => Write_A_Char'Access,
1132 W_Eol => Write_Eol'Access,
1133 W_Str => Write_A_String'Access,
1134 Backward_Compatibility => False);
1138 -- Write to the output file each entry in the SFN_Pragmas table
1139 -- as an pragma Source_File_Name.
1141 for Index in 1 .. SFN_Pragmas.Last loop
1142 Write_A_String ("pragma Source_File_Name");
1144 Write_A_String (" (");
1145 Write_A_String (SFN_Pragmas.Table (Index).Unit.all);
1146 Write_A_String (",");
1149 if SFN_Pragmas.Table (Index).Spec then
1150 Write_A_String (" Spec_File_Name => """);
1153 Write_A_String (" Body_File_Name => """);
1156 Write_A_String (SFN_Pragmas.Table (Index).File.all);
1157 Write_A_String (""");");
1170 procedure Write_A_Char (C : Character) is
1172 Write_A_String ((1 => C));
1179 procedure Write_Eol is
1181 Write_A_String ((1 => ASCII.LF));
1184 --------------------
1185 -- Write_A_String --
1186 --------------------
1188 procedure Write_A_String (S : String) is
1189 Str : String (1 .. S'Length);
1192 if S'Length > 0 then
1195 if Write (Output_FD, Str (1)'Address, Str'Length) /= Str'Length then
1196 Prj.Com.Fail ("disk full");