1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2004 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
;
48 package body Prj
.Makr
is
50 function Dup
(Fd
: File_Descriptor
) return File_Descriptor
;
51 pragma Import
(C
, Dup
);
53 procedure Dup2
(Old_Fd
, New_Fd
: File_Descriptor
);
54 pragma Import
(C
, Dup2
);
56 Gcc
: constant String := "gcc";
57 Gcc_Path
: String_Access
:= null;
59 Non_Empty_Node
: constant Project_Node_Id
:= 1;
60 -- Used for the With_Clause of the naming project
62 type Matched_Type
is (True, False, Excluded
);
64 Naming_File_Suffix
: constant String := "_naming";
65 Source_List_File_Suffix
: constant String := "_source_list.txt";
67 Output_FD
: File_Descriptor
;
68 -- To save the project file and its naming project file.
71 -- Output an empty line.
73 procedure Write_A_Char
(C
: Character);
74 -- Write one character to Output_FD
76 procedure Write_A_String
(S
: String);
77 -- Write a String to Output_FD
79 package Processed_Directories
is new Table
.Table
80 (Table_Component_Type
=> String_Access
,
81 Table_Index_Type
=> Natural,
84 Table_Increment
=> 10,
85 Table_Name
=> "Prj.Makr.Processed_Directories");
93 Project_File
: Boolean;
94 Directories
: Argument_List
;
95 Name_Patterns
: Argument_List
;
96 Excluded_Patterns
: Argument_List
;
97 Foreign_Patterns
: Argument_List
;
98 Preproc_Switches
: Argument_List
;
99 Very_Verbose
: Boolean)
101 Path_Name
: String (1 .. File_Path
'Length +
102 Project_File_Extension
'Length);
103 Path_Last
: Natural := File_Path
'Length;
105 Directory_Last
: Natural := 0;
107 Output_Name
: String (Path_Name
'Range);
108 Output_Name_Last
: Natural;
109 Output_Name_Id
: Name_Id
;
111 Project_Node
: Project_Node_Id
:= Empty_Node
;
112 Project_Declaration
: Project_Node_Id
:= Empty_Node
;
113 Source_Dirs_List
: Project_Node_Id
:= Empty_Node
;
114 Current_Source_Dir
: Project_Node_Id
:= Empty_Node
;
116 Project_Naming_Node
: Project_Node_Id
:= Empty_Node
;
117 Project_Naming_Decl
: Project_Node_Id
:= Empty_Node
;
118 Naming_Package
: Project_Node_Id
:= Empty_Node
;
120 Project_Naming_File_Name
: String (1 .. Output_Name
'Length +
121 Naming_File_Suffix
'Length);
123 Project_Naming_Last
: Natural;
124 Project_Naming_Id
: Name_Id
:= No_Name
;
126 Excluded_Expressions
: array (Excluded_Patterns
'Range) of Regexp
;
127 Regular_Expressions
: array (Name_Patterns
'Range) of Regexp
;
128 Foreign_Expressions
: array (Foreign_Patterns
'Range) of Regexp
;
130 Source_List_Path
: String (1 .. Output_Name
'Length +
131 Source_List_File_Suffix
'Length);
132 Source_List_Last
: Natural;
134 Source_List_FD
: File_Descriptor
;
136 Args
: Argument_List
(1 .. Preproc_Switches
'Length + 6);
138 type SFN_Pragma
is record
145 package SFN_Pragmas
is new Table
.Table
146 (Table_Component_Type
=> SFN_Pragma
,
147 Table_Index_Type
=> Natural,
148 Table_Low_Bound
=> 0,
150 Table_Increment
=> 50,
151 Table_Name
=> "Prj.Makr.SFN_Pragmas");
153 procedure Process_Directory
(Dir_Name
: String; Recursively
: Boolean);
154 -- Look for Ada and foreign sources in a directory, according to the
155 -- patterns. When Recursively is True, after looking for sources in
156 -- Dir_Name, look also in its subdirectories, if any.
158 -----------------------
159 -- Process_Directory --
160 -----------------------
162 procedure Process_Directory
(Dir_Name
: String; Recursively
: Boolean) is
163 Matched
: Matched_Type
:= False;
164 Str
: String (1 .. 2_000
);
167 Process
: Boolean := True;
169 Temp_File_Name
: String_Access
:= null;
171 Save_Last_Pragma_Index
: Natural := 0;
173 File_Name_Id
: Name_Id
:= No_Name
;
175 SFN_Prag
: SFN_Pragma
;
178 -- Avoid processing the same directory more than once
180 for Index
in 1 .. Processed_Directories
.Last
loop
181 if Processed_Directories
.Table
(Index
).all = Dir_Name
then
188 if Opt
.Verbose_Mode
then
189 Output
.Write_Str
("Processing directory """);
190 Output
.Write_Str
(Dir_Name
);
191 Output
.Write_Line
("""");
194 Processed_Directories
. Increment_Last
;
195 Processed_Directories
.Table
(Processed_Directories
.Last
) :=
196 new String'(Dir_Name);
198 -- Get the source file names from the directory.
199 -- Fails if the directory does not exist.
202 Open (Dir, Dir_Name);
205 when Directory_Error =>
206 Prj.Com.Fail ("cannot open directory """, Dir_Name, """");
209 -- Process each regular file in the directory
212 Read (Dir, Str, Last);
213 exit File_Loop when Last = 0;
216 (Dir_Name & Directory_Separator & Str (1 .. Last))
221 Name_Buffer (1 .. Name_Len) := Str (1 .. Last);
222 File_Name_Id := Name_Find;
224 -- First, check if the file name matches at least one of
225 -- the excluded expressions;
227 for Index in Excluded_Expressions'Range loop
229 Match (Str (1 .. Last), Excluded_Expressions (Index))
236 -- If it does not match any of the excluded expressions,
237 -- check if the file name matches at least one of the
238 -- regular expressions.
240 if Matched = True then
243 for Index in Regular_Expressions'Range loop
245 Match (Str (1 .. Last), Regular_Expressions (Index))
254 or else (Matched = True and then Opt.Verbose_Mode)
256 Output.Write_Str (" Checking """);
257 Output.Write_Str (Str (1 .. Last));
258 Output.Write_Line (""": ");
261 -- If the file name matches one of the regular expressions,
262 -- parse it to get its unit name.
264 if Matched = True then
266 FD : File_Descriptor;
268 Saved_Output : File_Descriptor;
269 Saved_Error : File_Descriptor;
272 -- If we don't have the path of the compiler yet,
275 if Gcc_Path = null then
276 Gcc_Path := Locate_Exec_On_Path (Gcc);
278 if Gcc_Path = null then
279 Prj.Com.Fail ("could not locate " & Gcc);
283 -- If we don't have yet the file name of the
284 -- temporary file, get it now.
286 if Temp_File_Name = null then
287 Create_Temp_File (FD, Temp_File_Name);
289 if FD = Invalid_FD then
291 ("could not create temporary file");
295 Delete_File (Temp_File_Name.all, Success);
298 Args (Args'Last) := new String'
300 Directory_Separator
&
303 -- Create the temporary file
305 FD
:= Create_Output_Text_File
306 (Name
=> Temp_File_Name
.all);
308 if FD
= Invalid_FD
then
310 ("could not create temporary file");
313 -- Save the standard output and error
315 Saved_Output
:= Dup
(Standout
);
316 Saved_Error
:= Dup
(Standerr
);
318 -- Set standard output and error to the temporary file
323 -- And spawn the compiler
325 Spawn
(Gcc_Path
.all, Args
, Success
);
327 -- Restore the standard output and error
329 Dup2
(Saved_Output
, Standout
);
330 Dup2
(Saved_Error
, Standerr
);
332 -- Close the temporary file
336 -- And close the saved standard output and error to
337 -- avoid too many file descriptors.
339 Close
(Saved_Output
);
342 -- Now that standard output is restored, check if
343 -- the compiler ran correctly.
345 -- Read the lines of the temporary file:
346 -- they should contain the kind and name of the unit.
350 Text_Line
: String (1 .. 1_000
);
354 Open
(File
, Temp_File_Name
.all);
356 if not Is_Valid
(File
) then
358 ("could not read temporary file");
361 Save_Last_Pragma_Index
:= SFN_Pragmas
.Last
;
363 if End_Of_File
(File
) then
364 if Opt
.Verbose_Mode
then
366 Output
.Write_Str
(" (process died) ");
371 Line_Loop
: while not End_Of_File
(File
) loop
372 Get_Line
(File
, Text_Line
, Text_Last
);
374 -- Find the first closing parenthesis
376 Char_Loop
: for J
in 1 .. Text_Last
loop
377 if Text_Line
(J
) = ')' then
379 Text_Line
(1 .. 4) = "Unit"
381 -- Add entry to SFN_Pragmas table
384 Name_Buffer
(1 .. Name_Len
) :=
385 Text_Line
(6 .. J
- 7);
388 File
=> File_Name_Id
,
390 Spec
=> Text_Line
(J
- 5 .. J
) =
393 SFN_Pragmas
.Increment_Last
;
395 (SFN_Pragmas
.Last
) := SFN_Prag
;
403 if Save_Last_Pragma_Index
= SFN_Pragmas
.Last
then
404 if Opt
.Verbose_Mode
then
405 Output
.Write_Line
(" not a unit");
409 if SFN_Pragmas
.Last
>
410 Save_Last_Pragma_Index
+ 1
412 for Index
in Save_Last_Pragma_Index
+ 1 ..
415 SFN_Pragmas
.Table
(Index
).Index
:=
416 Int
(Index
- Save_Last_Pragma_Index
);
420 for Index
in Save_Last_Pragma_Index
+ 1 ..
423 SFN_Prag
:= SFN_Pragmas
.Table
(Index
);
425 if Opt
.Verbose_Mode
then
426 if SFN_Prag
.Spec
then
427 Output
.Write_Str
(" spec of ");
430 Output
.Write_Str
(" body of ");
434 (Get_Name_String
(SFN_Prag
.Unit
));
439 -- Add the corresponding attribute in the
440 -- Naming package of the naming project.
443 Decl_Item
: constant Project_Node_Id
:=
448 Attribute
: constant Project_Node_Id
:=
451 N_Attribute_Declaration
);
453 Expression
: constant Project_Node_Id
:=
455 (Of_Kind
=> N_Expression
,
456 And_Expr_Kind
=> Single
);
458 Term
: constant Project_Node_Id
:=
461 And_Expr_Kind
=> Single
);
463 Value
: constant Project_Node_Id
:=
465 (Of_Kind
=> N_Literal_String
,
466 And_Expr_Kind
=> Single
);
469 Set_Next_Declarative_Item
471 To
=> First_Declarative_Item_Of
473 Set_First_Declarative_Item_Of
474 (Naming_Package
, To
=> Decl_Item
);
475 Set_Current_Item_Node
476 (Decl_Item
, To
=> Attribute
);
478 -- Is it a spec or a body?
480 if SFN_Prag
.Spec
then
482 (Attribute
, To
=> Name_Spec
);
489 -- Get the name of the unit
491 Get_Name_String
(SFN_Prag
.Unit
);
492 To_Lower
(Name_Buffer
(1 .. Name_Len
));
493 Set_Associative_Array_Index_Of
494 (Attribute
, To
=> Name_Find
);
497 (Attribute
, To
=> Expression
);
499 (Expression
, To
=> Term
);
500 Set_Current_Term
(Term
, To
=> Value
);
502 -- And set the name of the file
505 (Value
, To
=> File_Name_Id
);
507 (Value
, To
=> SFN_Prag
.Index
);
513 -- Add source file name to source list
517 Str
(Last
) := ASCII
.LF
;
519 if Write
(Source_List_FD
,
523 Prj
.Com
.Fail
("disk full");
530 Delete_File
(Temp_File_Name
.all, Success
);
534 -- File name matches none of the regular expressions
537 -- If file is not excluded, see if this is foreign source
539 if Matched
/= Excluded
then
540 for Index
in Foreign_Expressions
'Range loop
541 if Match
(Str
(1 .. Last
),
542 Foreign_Expressions
(Index
))
553 Output
.Write_Line
("no match");
556 Output
.Write_Line
("excluded");
559 Output
.Write_Line
("foreign source");
563 if Project_File
and Matched
= True then
565 -- Add source file name to source list file
568 Str
(Last
) := ASCII
.LF
;
570 if Write
(Source_List_FD
,
574 Prj
.Com
.Fail
("disk full");
584 -- If Recursively is True, call itself for each subdirectory.
585 -- We do that, even when this directory has already been processed,
586 -- because all of its subdirectories may not have been processed.
589 Open
(Dir
, Dir_Name
);
592 Read
(Dir
, Str
, Last
);
595 -- Do not call itself for "." or ".."
598 (Dir_Name
& Directory_Separator
& Str
(1 .. Last
))
599 and then Str
(1 .. Last
) /= "."
600 and then Str
(1 .. Last
) /= ".."
603 (Dir_Name
& Directory_Separator
& Str
(1 .. Last
),
604 Recursively
=> True);
610 end Process_Directory
;
612 -- Start of processing for Make
615 -- Do some needed initializations
622 SFN_Pragmas
.Set_Last
(0);
624 Processed_Directories
.Set_Last
(0);
626 -- Initialize the compiler switches
628 Args
(1) := new String'("-c");
629 Args (2) := new String'("-gnats");
630 Args
(3) := new String'("-gnatu");
631 Args (4 .. 3 + Preproc_Switches'Length) := Preproc_Switches;
632 Args (4 + Preproc_Switches'Length) := new String'("-x");
633 Args
(5 + Preproc_Switches
'Length) := new String'("ada");
635 -- Get the path and file names
637 if File_Names_Case_Sensitive then
638 Path_Name (1 .. Path_Last) := File_Path;
640 Path_Name (1 .. Path_Last) := To_Lower (File_Path);
643 Path_Name (Path_Last + 1 .. Path_Name'Last) :=
644 Project_File_Extension;
646 -- Get the end of directory information, if any
648 for Index in reverse 1 .. Path_Last loop
649 if Path_Name (Index) = Directory_Separator then
650 Directory_Last := Index;
656 if Path_Last < Project_File_Extension'Length + 1
658 (Path_Last - Project_File_Extension'Length + 1 .. Path_Last)
659 /= Project_File_Extension
661 Path_Last := Path_Name'Last;
664 Output_Name (1 .. Path_Last) := To_Lower (Path_Name (1 .. Path_Last));
665 Output_Name_Last := Path_Last - Project_File_Extension'Length;
667 -- If there is already a project file with the specified name, parse
668 -- it to get the components that are not automatically generated.
670 if Is_Regular_File (Output_Name (1 .. Path_Last)) then
671 if Opt.Verbose_Mode then
672 Output.Write_Str ("Parsing already existing project file """);
673 Output.Write_Str (Output_Name (1 .. Output_Name_Last));
674 Output.Write_Line ("""");
678 (Project => Project_Node,
679 Project_File_Name => Output_Name (1 .. Output_Name_Last),
680 Always_Errout_Finalize => False);
682 -- Fail if parsing was not successful
684 if Project_Node = Empty_Node then
685 Fail ("parsing of existing project file failed");
688 -- If parsing was successful, remove the components that are
689 -- automatically generated, if any, so that they will be
690 -- unconditionally added later.
692 -- Remove the with clause for the naming project file
695 With_Clause : Project_Node_Id :=
696 First_With_Clause_Of (Project_Node);
697 Previous : Project_Node_Id := Empty_Node;
700 while With_Clause /= Empty_Node loop
701 if Tree.Name_Of (With_Clause) = Project_Naming_Id then
702 if Previous = Empty_Node then
703 Set_First_With_Clause_Of
705 To => Next_With_Clause_Of (With_Clause));
707 Set_Next_With_Clause_Of
709 To => Next_With_Clause_Of (With_Clause));
715 Previous := With_Clause;
716 With_Clause := Next_With_Clause_Of (With_Clause);
720 -- Remove attribute declarations of Source_Files,
721 -- Source_List_File, Source_Dirs, and the declaration of
722 -- package Naming, if they exist.
725 Declaration : Project_Node_Id :=
726 First_Declarative_Item_Of
727 (Project_Declaration_Of
729 Previous : Project_Node_Id := Empty_Node;
730 Current_Node : Project_Node_Id := Empty_Node;
733 while Declaration /= Empty_Node loop
734 Current_Node := Current_Item_Node (Declaration);
736 if (Kind_Of (Current_Node) = N_Attribute_Declaration
738 (Tree.Name_Of (Current_Node) = Name_Source_Files
739 or else Tree.Name_Of (Current_Node) =
740 Name_Source_List_File
741 or else Tree.Name_Of (Current_Node) =
744 (Kind_Of (Current_Node) = N_Package_Declaration
745 and then Tree.Name_Of (Current_Node) = Name_Naming)
747 if Previous = Empty_Node then
748 Set_First_Declarative_Item_Of
749 (Project_Declaration_Of (Project_Node),
750 To => Next_Declarative_Item (Declaration));
753 Set_Next_Declarative_Item
755 To => Next_Declarative_Item (Declaration));
759 Previous := Declaration;
762 Declaration := Next_Declarative_Item (Declaration);
768 if Directory_Last /= 0 then
769 Output_Name (1 .. Output_Name_Last - Directory_Last) :=
770 Output_Name (Directory_Last + 1 .. Output_Name_Last);
771 Output_Name_Last := Output_Name_Last - Directory_Last;
774 -- Get the project name id
776 Name_Len := Output_Name_Last;
777 Name_Buffer (1 .. Name_Len) := Output_Name (1 .. Name_Len);
778 Output_Name_Id := Name_Find;
780 -- Create the project naming file name
782 Project_Naming_Last := Output_Name_Last;
783 Project_Naming_File_Name (1 .. Project_Naming_Last) :=
784 Output_Name (1 .. Project_Naming_Last);
785 Project_Naming_File_Name
786 (Project_Naming_Last + 1 ..
787 Project_Naming_Last + Naming_File_Suffix'Length) :=
789 Project_Naming_Last :=
790 Project_Naming_Last + Naming_File_Suffix'Length;
792 -- Get the project naming id
794 Name_Len := Project_Naming_Last;
795 Name_Buffer (1 .. Name_Len) :=
796 Project_Naming_File_Name (1 .. Name_Len);
797 Project_Naming_Id := Name_Find;
799 Project_Naming_File_Name
800 (Project_Naming_Last + 1 ..
801 Project_Naming_Last + Project_File_Extension'Length) :=
802 Project_File_Extension;
803 Project_Naming_Last :=
804 Project_Naming_Last + Project_File_Extension'Length;
806 -- Create the source list file name
808 Source_List_Last := Output_Name_Last;
809 Source_List_Path (1 .. Source_List_Last) :=
810 Output_Name (1 .. Source_List_Last);
812 (Source_List_Last + 1 ..
813 Source_List_Last + Source_List_File_Suffix'Length) :=
814 Source_List_File_Suffix;
815 Source_List_Last := Source_List_Last + Source_List_File_Suffix'Length;
817 -- Add the project file extension to the project name
820 (Output_Name_Last + 1 ..
821 Output_Name_Last + Project_File_Extension'Length) :=
822 Project_File_Extension;
823 Output_Name_Last := Output_Name_Last + Project_File_Extension'Length;
826 -- Change the current directory to the directory of the project file,
827 -- if any directory information is specified.
829 if Directory_Last /= 0 then
831 Change_Dir (Path_Name (1 .. Directory_Last));
833 when Directory_Error =>
835 ("unknown directory """,
836 Path_Name (1 .. Directory_Last),
843 -- Delete the source list file, if it already exists
849 (Source_List_Path (1 .. Source_List_Last),
853 -- And create a new source list file.
854 -- Fail if file cannot be created.
856 Source_List_FD := Create_New_File
857 (Name => Source_List_Path (1 .. Source_List_Last),
860 if Source_List_FD = Invalid_FD then
862 ("cannot create file """,
863 Source_List_Path (1 .. Source_List_Last),
868 -- Compile the regular expressions. Fails immediately if any of
869 -- the specified strings is in error.
871 for Index in Excluded_Expressions'Range loop
873 Output.Write_Str ("Excluded pattern: """);
874 Output.Write_Str (Excluded_Patterns (Index).all);
875 Output.Write_Line ("""");
879 Excluded_Expressions (Index) :=
880 Compile (Pattern => Excluded_Patterns (Index).all, Glob => True);
882 when Error_In_Regexp =>
884 ("invalid regular expression """,
885 Excluded_Patterns (Index).all,
890 for Index in Foreign_Expressions'Range loop
892 Output.Write_Str ("Foreign pattern: """);
893 Output.Write_Str (Foreign_Patterns (Index).all);
894 Output.Write_Line ("""");
898 Foreign_Expressions (Index) :=
899 Compile (Pattern => Foreign_Patterns (Index).all, Glob => True);
901 when Error_In_Regexp =>
903 ("invalid regular expression """,
904 Foreign_Patterns (Index).all,
909 for Index in Regular_Expressions'Range loop
911 Output.Write_Str ("Pattern: """);
912 Output.Write_Str (Name_Patterns (Index).all);
913 Output.Write_Line ("""");
917 Regular_Expressions (Index) :=
918 Compile (Pattern => Name_Patterns (Index).all, Glob => True);
921 when Error_In_Regexp =>
923 ("invalid regular expression """,
924 Name_Patterns (Index).all,
930 if Opt.Verbose_Mode then
931 Output.Write_Str ("Naming project file name is """);
933 (Project_Naming_File_Name (1 .. Project_Naming_Last));
934 Output.Write_Line ("""");
937 -- If there were no already existing project file, or if the parsing
938 -- was unsuccessful, create an empty project node with the correct
939 -- name and its project declaration node.
941 if Project_Node = Empty_Node then
942 Project_Node := Default_Project_Node (Of_Kind => N_Project);
943 Set_Name_Of (Project_Node, To => Output_Name_Id);
944 Set_Project_Declaration_Of
946 To => Default_Project_Node (Of_Kind => N_Project_Declaration));
950 -- Create the naming project node, and add an attribute declaration
951 -- for Source_Files as an empty list, to indicate there are no
952 -- sources in the naming project.
954 Project_Naming_Node := Default_Project_Node (Of_Kind => N_Project);
955 Set_Name_Of (Project_Naming_Node, To => Project_Naming_Id);
956 Project_Naming_Decl :=
957 Default_Project_Node (Of_Kind => N_Project_Declaration);
958 Set_Project_Declaration_Of (Project_Naming_Node, Project_Naming_Decl);
960 Default_Project_Node (Of_Kind => N_Package_Declaration);
961 Set_Name_Of (Naming_Package, To => Name_Naming);
964 Decl_Item : constant Project_Node_Id :=
965 Default_Project_Node (Of_Kind => N_Declarative_Item);
967 Attribute : constant Project_Node_Id :=
969 (Of_Kind => N_Attribute_Declaration,
970 And_Expr_Kind => List);
972 Expression : constant Project_Node_Id :=
974 (Of_Kind => N_Expression,
975 And_Expr_Kind => List);
977 Term : constant Project_Node_Id :=
980 And_Expr_Kind => List);
982 Empty_List : constant Project_Node_Id :=
984 (Of_Kind => N_Literal_String_List);
987 Set_First_Declarative_Item_Of
988 (Project_Naming_Decl, To => Decl_Item);
989 Set_Next_Declarative_Item (Decl_Item, Naming_Package);
990 Set_Current_Item_Node (Decl_Item, To => Attribute);
991 Set_Name_Of (Attribute, To => Name_Source_Files);
992 Set_Expression_Of (Attribute, To => Expression);
993 Set_First_Term (Expression, To => Term);
994 Set_Current_Term (Term, To => Empty_List);
997 -- Add a with clause on the naming project in the main project
1000 With_Clause : constant Project_Node_Id :=
1001 Default_Project_Node (Of_Kind => N_With_Clause);
1004 Set_Next_With_Clause_Of
1005 (With_Clause, To => First_With_Clause_Of (Project_Node));
1006 Set_First_With_Clause_Of (Project_Node, To => With_Clause);
1007 Set_Name_Of (With_Clause, To => Project_Naming_Id);
1009 -- We set the project node to something different than
1010 -- Empty_Node, so that Prj.PP does not generate a limited
1013 Set_Project_Node_Of (With_Clause, Non_Empty_Node);
1015 Name_Len := Project_Naming_Last;
1016 Name_Buffer (1 .. Name_Len) :=
1017 Project_Naming_File_Name (1 .. Project_Naming_Last);
1018 Set_String_Value_Of (With_Clause, To => Name_Find);
1021 Project_Declaration := Project_Declaration_Of (Project_Node);
1023 -- Add a renaming declaration for package Naming in the main project
1026 Decl_Item : constant Project_Node_Id :=
1027 Default_Project_Node (Of_Kind => N_Declarative_Item);
1029 Naming : constant Project_Node_Id :=
1030 Default_Project_Node (Of_Kind => N_Package_Declaration);
1032 Set_Next_Declarative_Item
1034 To => First_Declarative_Item_Of (Project_Declaration));
1035 Set_First_Declarative_Item_Of
1036 (Project_Declaration, To => Decl_Item);
1037 Set_Current_Item_Node (Decl_Item, To => Naming);
1038 Set_Name_Of (Naming, To => Name_Naming);
1039 Set_Project_Of_Renamed_Package_Of
1040 (Naming, To => Project_Naming_Node);
1043 -- Add an attribute declaration for Source_Dirs, initialized as an
1044 -- empty list. Directories will be added as they are read from the
1045 -- directory list file.
1048 Decl_Item : constant Project_Node_Id :=
1049 Default_Project_Node (Of_Kind => N_Declarative_Item);
1051 Attribute : constant Project_Node_Id :=
1052 Default_Project_Node
1053 (Of_Kind => N_Attribute_Declaration,
1054 And_Expr_Kind => List);
1056 Expression : constant Project_Node_Id :=
1057 Default_Project_Node
1058 (Of_Kind => N_Expression,
1059 And_Expr_Kind => List);
1061 Term : constant Project_Node_Id :=
1062 Default_Project_Node
1063 (Of_Kind => N_Term, And_Expr_Kind => List);
1066 Set_Next_Declarative_Item
1068 To => First_Declarative_Item_Of (Project_Declaration));
1069 Set_First_Declarative_Item_Of
1070 (Project_Declaration, To => Decl_Item);
1071 Set_Current_Item_Node (Decl_Item, To => Attribute);
1072 Set_Name_Of (Attribute, To => Name_Source_Dirs);
1073 Set_Expression_Of (Attribute, To => Expression);
1074 Set_First_Term (Expression, To => Term);
1076 Default_Project_Node (Of_Kind => N_Literal_String_List,
1077 And_Expr_Kind => List);
1078 Set_Current_Term (Term, To => Source_Dirs_List);
1081 -- Add an attribute declaration for Source_List_File with the
1082 -- source list file name that will be created.
1085 Decl_Item : constant Project_Node_Id :=
1086 Default_Project_Node (Of_Kind => N_Declarative_Item);
1088 Attribute : constant Project_Node_Id :=
1089 Default_Project_Node
1090 (Of_Kind => N_Attribute_Declaration,
1091 And_Expr_Kind => Single);
1093 Expression : constant Project_Node_Id :=
1094 Default_Project_Node
1095 (Of_Kind => N_Expression,
1096 And_Expr_Kind => Single);
1098 Term : constant Project_Node_Id :=
1099 Default_Project_Node
1101 And_Expr_Kind => Single);
1103 Value : constant Project_Node_Id :=
1104 Default_Project_Node
1105 (Of_Kind => N_Literal_String,
1106 And_Expr_Kind => Single);
1109 Set_Next_Declarative_Item
1111 To => First_Declarative_Item_Of (Project_Declaration));
1112 Set_First_Declarative_Item_Of
1113 (Project_Declaration, To => Decl_Item);
1114 Set_Current_Item_Node (Decl_Item, To => Attribute);
1115 Set_Name_Of (Attribute, To => Name_Source_List_File);
1116 Set_Expression_Of (Attribute, To => Expression);
1117 Set_First_Term (Expression, To => Term);
1118 Set_Current_Term (Term, To => Value);
1119 Name_Len := Source_List_Last;
1120 Name_Buffer (1 .. Name_Len) :=
1121 Source_List_Path (1 .. Source_List_Last);
1122 Set_String_Value_Of (Value, To => Name_Find);
1126 -- Process each directory
1128 for Index in Directories'Range loop
1131 Dir_Name : constant String := Directories (Index).all;
1132 Last : Natural := Dir_Name'Last;
1133 Recursively : Boolean := False;
1135 if Dir_Name'Length >= 4
1136 and then (Dir_Name (Last - 2 .. Last) = "/**")
1139 Recursively := True;
1142 if Project_File then
1144 -- Add the directory in the list for attribute Source_Dirs
1147 Expression : constant Project_Node_Id :=
1148 Default_Project_Node
1149 (Of_Kind => N_Expression,
1150 And_Expr_Kind => Single);
1152 Term : constant Project_Node_Id :=
1153 Default_Project_Node
1155 And_Expr_Kind => Single);
1157 Value : constant Project_Node_Id :=
1158 Default_Project_Node
1159 (Of_Kind => N_Literal_String,
1160 And_Expr_Kind => Single);
1163 if Current_Source_Dir = Empty_Node then
1164 Set_First_Expression_In_List
1165 (Source_Dirs_List, To => Expression);
1167 Set_Next_Expression_In_List
1168 (Current_Source_Dir, To => Expression);
1171 Current_Source_Dir := Expression;
1172 Set_First_Term (Expression, To => Term);
1173 Set_Current_Term (Term, To => Value);
1174 Name_Len := Dir_Name'Length;
1175 Name_Buffer (1 .. Name_Len) := Dir_Name;
1176 Set_String_Value_Of (Value, To => Name_Find);
1180 Process_Directory (Dir_Name (Dir_Name'First .. Last), Recursively);
1185 if Project_File then
1186 Close (Source_List_FD);
1193 -- Delete the file if it already exists
1196 (Path_Name (Directory_Last + 1 .. Path_Last),
1197 Success => Discard);
1201 if Opt.Verbose_Mode then
1202 Output.Write_Str ("Creating new file """);
1203 Output.Write_Str (Path_Name (Directory_Last + 1 .. Path_Last));
1204 Output.Write_Line ("""");
1207 Output_FD := Create_New_File
1208 (Path_Name (Directory_Last + 1 .. Path_Last),
1211 -- Fails if project file cannot be created
1213 if Output_FD = Invalid_FD then
1215 ("cannot create new """, Path_Name (1 .. Path_Last), """");
1218 if Project_File then
1220 -- Output the project file
1224 W_Char => Write_A_Char'Access,
1225 W_Eol => Write_Eol'Access,
1226 W_Str => Write_A_String'Access,
1227 Backward_Compatibility => False);
1230 -- Delete the naming project file if it already exists
1233 (Project_Naming_File_Name (1 .. Project_Naming_Last),
1234 Success => Discard);
1238 if Opt.Verbose_Mode then
1239 Output.Write_Str ("Creating new naming project file """);
1240 Output.Write_Str (Project_Naming_File_Name
1241 (1 .. Project_Naming_Last));
1242 Output.Write_Line ("""");
1245 Output_FD := Create_New_File
1246 (Project_Naming_File_Name (1 .. Project_Naming_Last),
1249 -- Fails if naming project file cannot be created
1251 if Output_FD = Invalid_FD then
1253 ("cannot create new """,
1254 Project_Naming_File_Name (1 .. Project_Naming_Last),
1258 -- Output the naming project file
1261 (Project_Naming_Node,
1262 W_Char => Write_A_Char'Access,
1263 W_Eol => Write_Eol'Access,
1264 W_Str => Write_A_String'Access,
1265 Backward_Compatibility => False);
1269 -- Write to the output file each entry in the SFN_Pragmas table
1270 -- as an pragma Source_File_Name.
1272 for Index in 1 .. SFN_Pragmas.Last loop
1273 Write_A_String ("pragma Source_File_Name");
1275 Write_A_String (" (");
1277 (Get_Name_String (SFN_Pragmas.Table (Index).Unit));
1278 Write_A_String (",");
1281 if SFN_Pragmas.Table (Index).Spec then
1282 Write_A_String (" Spec_File_Name => """);
1285 Write_A_String (" Body_File_Name => """);
1289 (Get_Name_String (SFN_Pragmas.Table (Index).File));
1291 Write_A_String ("""");
1293 if SFN_Pragmas.Table (Index).Index /= 0 then
1294 Write_A_String (", Index =>");
1295 Write_A_String (SFN_Pragmas.Table (Index).Index'Img);
1298 Write_A_String (");");
1311 procedure Write_A_Char (C : Character) is
1313 Write_A_String ((1 => C));
1320 procedure Write_Eol is
1322 Write_A_String ((1 => ASCII.LF));
1325 --------------------
1326 -- Write_A_String --
1327 --------------------
1329 procedure Write_A_String (S : String) is
1330 Str : String (1 .. S'Length);
1333 if S'Length > 0 then
1336 if Write (Output_FD, Str (1)'Address, Str'Length) /= Str'Length then
1337 Prj.Com.Fail ("disk full");