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
144 package SFN_Pragmas is new Table.Table
145 (Table_Component_Type => SFN_Pragma,
146 Table_Index_Type => Natural,
147 Table_Low_Bound => 0,
149 Table_Increment => 50,
150 Table_Name => "Prj.Makr.SFN_Pragmas");
152 procedure Process_Directory (Dir_Name : String; Recursively : Boolean);
153 -- Look for Ada and foreign sources in a directory, according to the
154 -- patterns. When Recursively is True, after looking for sources in
155 -- Dir_Name, look also in its subdirectories, if any.
157 -----------------------
158 -- Process_Directory --
159 -----------------------
161 procedure Process_Directory (Dir_Name : String; Recursively : Boolean) is
162 Matched : Matched_Type := False;
163 Str : String (1 .. 2_000);
166 Process : Boolean := True;
168 Temp_File_Name : String_Access := null;
170 Save_Last_Pragma_Index : Natural := 0;
172 File_Name_Id : Name_Id := No_Name;
174 SFN_Prag : SFN_Pragma;
177 -- Avoid processing the same directory more than once
179 for Index in 1 .. Processed_Directories.Last loop
180 if Processed_Directories.Table (Index).all = Dir_Name then
187 if Opt.Verbose_Mode then
188 Output.Write_Str ("Processing directory """);
189 Output.Write_Str (Dir_Name);
190 Output.Write_Line ("""");
193 Processed_Directories. Increment_Last;
194 Processed_Directories.Table (Processed_Directories.Last) :=
195 new String'(Dir_Name);
197 -- Get the source file names from the directory.
198 -- Fails if the directory does not exist.
201 Open (Dir, Dir_Name);
204 when Directory_Error =>
205 Prj.Com.Fail ("cannot open directory """, Dir_Name, """");
208 -- Process each regular file in the directory
211 Read (Dir, Str, Last);
212 exit File_Loop when Last = 0;
215 (Dir_Name & Directory_Separator & Str (1 .. Last))
220 Name_Buffer (1 .. Name_Len) := Str (1 .. Last);
221 File_Name_Id := Name_Find;
223 -- First, check if the file name matches at least one of
224 -- the excluded expressions;
226 for Index in Excluded_Expressions'Range loop
228 Match (Str (1 .. Last), Excluded_Expressions (Index))
235 -- If it does not match any of the excluded expressions,
236 -- check if the file name matches at least one of the
237 -- regular expressions.
239 if Matched = True then
242 for Index in Regular_Expressions'Range loop
244 Match (Str (1 .. Last), Regular_Expressions (Index))
253 or else (Matched = True and then Opt.Verbose_Mode)
255 Output.Write_Str (" Checking """);
256 Output.Write_Str (Str (1 .. Last));
257 Output.Write_Str (""": ");
260 -- If the file name matches one of the regular expressions,
261 -- parse it to get its unit name.
263 if Matched = True then
265 FD : File_Descriptor;
267 Saved_Output : File_Descriptor;
268 Saved_Error : File_Descriptor;
271 -- If we don't have the path of the compiler yet,
274 if Gcc_Path = null then
275 Gcc_Path := Locate_Exec_On_Path (Gcc);
277 if Gcc_Path = null then
278 Prj.Com.Fail ("could not locate " & Gcc);
282 -- If we don't have yet the file name of the
283 -- temporary file, get it now.
285 if Temp_File_Name = null then
286 Create_Temp_File (FD, Temp_File_Name);
288 if FD = Invalid_FD then
290 ("could not create temporary file");
294 Delete_File (Temp_File_Name.all, Success);
297 Args (Args'Last) := new String'
299 Directory_Separator &
302 -- Create the temporary file
304 FD := Create_Output_Text_File
305 (Name => Temp_File_Name.all);
307 if FD = Invalid_FD then
309 ("could not create temporary file");
312 -- Save the standard output and error
314 Saved_Output := Dup (Standout);
315 Saved_Error := Dup (Standerr);
317 -- Set standard output and error to the temporary file
322 -- And spawn the compiler
324 Spawn (Gcc_Path.all, Args, Success);
326 -- Restore the standard output and error
328 Dup2 (Saved_Output, Standout);
329 Dup2 (Saved_Error, Standerr);
331 -- Close the temporary file
335 -- And close the saved standard output and error to
336 -- avoid too many file descriptors.
338 Close (Saved_Output);
341 -- Now that standard output is restored, check if
342 -- the compiler ran correctly.
344 -- Read the lines of the temporary file:
345 -- they should contain the kind and name of the unit.
349 Text_Line : String (1 .. 1_000);
353 Open (File, Temp_File_Name.all);
355 if not Is_Valid (File) then
357 ("could not read temporary file");
360 Save_Last_Pragma_Index := SFN_Pragmas.Last;
362 if End_Of_File (File) then
363 if Opt.Verbose_Mode then
365 Output.Write_Str ("(process died) ");
369 Line_Loop : while not End_Of_File (File) loop
370 Get_Line (File, Text_Line, Text_Last);
372 -- Find the first closing parenthesis
374 Char_Loop : for J in 1 .. Text_Last loop
375 if Text_Line (J) = ')' then
377 Text_Line (1 .. 4) = "Unit"
379 -- Add an entry in the SFN_Pragmas
383 Name_Buffer (1 .. Name_Len) :=
384 Text_Line (6 .. J - 7);
387 File => File_Name_Id,
388 Spec => Text_Line (J - 5 .. J) =
391 SFN_Pragmas.Increment_Last;
393 (SFN_Pragmas.Last) := SFN_Prag;
401 if Save_Last_Pragma_Index = SFN_Pragmas.Last then
402 if Opt.Verbose_Mode then
403 Output.Write_Line ("not a unit");
406 elsif SFN_Pragmas.Last >
407 Save_Last_Pragma_Index + 1
409 SFN_Pragmas.Set_Last (Save_Last_Pragma_Index);
411 if Opt.Verbose_Mode then
413 ("file contains multiple units");
417 SFN_Prag := SFN_Pragmas.Table
420 if Opt.Verbose_Mode then
421 if SFN_Prag.Spec then
422 Output.Write_Str ("spec of ");
425 Output.Write_Str ("body of ");
429 (Get_Name_String (SFN_Prag.Unit));
434 -- Add the corresponding attribute in
435 -- the Naming package of the naming
439 Decl_Item : constant Project_Node_Id
440 := Default_Project_Node
444 Attribute : constant Project_Node_Id
445 := Default_Project_Node
447 N_Attribute_Declaration);
449 Expression : constant Project_Node_Id
450 := Default_Project_Node
451 (Of_Kind => N_Expression,
452 And_Expr_Kind => Single);
454 Term : constant Project_Node_Id :=
457 And_Expr_Kind => Single);
459 Value : constant Project_Node_Id :=
467 Set_Next_Declarative_Item
469 To => First_Declarative_Item_Of
471 Set_First_Declarative_Item_Of
472 (Naming_Package, To => Decl_Item);
473 Set_Current_Item_Node
474 (Decl_Item, To => Attribute);
476 -- Is it a spec or a body?
478 if SFN_Prag.Spec then
480 (Attribute, To => Name_Spec);
487 -- Get the name of the unit
489 Get_Name_String (SFN_Prag.Unit);
490 To_Lower (Name_Buffer (1 .. Name_Len));
491 Set_Associative_Array_Index_Of
492 (Attribute, To => Name_Find);
495 (Attribute, To => Expression);
497 (Expression, To => Term);
498 Set_Current_Term (Term, To => Value);
500 -- And set the name of the file
503 (Value, To => File_Name_Id);
506 -- Add source file name to source list
510 Str (Last) := ASCII.LF;
512 if Write (Source_List_FD,
516 Prj.Com.Fail ("disk full");
523 Delete_File (Temp_File_Name.all, Success);
527 -- File name matches none of the regular expressions
530 -- If the file is not excluded, look if this is a foreign
533 if Matched /= Excluded then
534 for Index in Foreign_Expressions'Range loop
535 if Match (Str (1 .. Last),
536 Foreign_Expressions (Index))
547 Output.Write_Line ("no match");
550 Output.Write_Line ("excluded");
553 Output.Write_Line ("foreign source");
557 if Project_File and Matched = True then
559 -- Add source file name to source list file
562 Str (Last) := ASCII.LF;
564 if Write (Source_List_FD,
568 Prj.Com.Fail ("disk full");
578 -- If Recursively is True, call itself for each subdirectory.
579 -- We do that, even when this directory has already been processed,
580 -- because all of its subdirectories may not have been processed.
583 Open (Dir, Dir_Name);
586 Read (Dir, Str, Last);
589 -- Do not call itself for "." or ".."
592 (Dir_Name & Directory_Separator & Str (1 .. Last))
593 and then Str (1 .. Last) /= "."
594 and then Str (1 .. Last) /= ".."
597 (Dir_Name & Directory_Separator & Str (1 .. Last),
598 Recursively => True);
604 end Process_Directory;
606 -- Start of processing for Make
609 -- Do some needed initializations
616 SFN_Pragmas.Set_Last (0);
618 Processed_Directories.Set_Last (0);
620 -- Initialize the compiler switches
622 Args (1) := new String'("-c");
623 Args (2) := new String'("-gnats");
624 Args (3) := new String'("-gnatu");
625 Args (4 .. 3 + Preproc_Switches'Length) := Preproc_Switches;
626 Args (4 + Preproc_Switches'Length) := new String'("-x");
627 Args (5 + Preproc_Switches'Length) := new String'("ada");
629 -- Get the path and file names
631 if File_Names_Case_Sensitive then
632 Path_Name (1 .. Path_Last) := File_Path;
634 Path_Name (1 .. Path_Last) := To_Lower (File_Path);
637 Path_Name (Path_Last + 1 .. Path_Name'Last) :=
638 Project_File_Extension;
640 -- Get the end of directory information, if any
642 for Index in reverse 1 .. Path_Last loop
643 if Path_Name (Index) = Directory_Separator then
644 Directory_Last := Index;
650 if Path_Last < Project_File_Extension'Length + 1
652 (Path_Last - Project_File_Extension'Length + 1 .. Path_Last)
653 /= Project_File_Extension
655 Path_Last := Path_Name'Last;
658 Output_Name (1 .. Path_Last) := To_Lower (Path_Name (1 .. Path_Last));
659 Output_Name_Last := Path_Last - Project_File_Extension'Length;
661 if Directory_Last /= 0 then
662 Output_Name (1 .. Output_Name_Last - Directory_Last) :=
663 Output_Name (Directory_Last + 1 .. Output_Name_Last);
664 Output_Name_Last := Output_Name_Last - Directory_Last;
667 -- Get the project name id
669 Name_Len := Output_Name_Last;
670 Name_Buffer (1 .. Name_Len) := Output_Name (1 .. Name_Len);
671 Output_Name_Id := Name_Find;
673 -- Create the project naming file name
675 Project_Naming_Last := Output_Name_Last;
676 Project_Naming_File_Name (1 .. Project_Naming_Last) :=
677 Output_Name (1 .. Project_Naming_Last);
678 Project_Naming_File_Name
679 (Project_Naming_Last + 1 ..
680 Project_Naming_Last + Naming_File_Suffix'Length) :=
682 Project_Naming_Last :=
683 Project_Naming_Last + Naming_File_Suffix'Length;
685 -- Get the project naming id
687 Name_Len := Project_Naming_Last;
688 Name_Buffer (1 .. Name_Len) :=
689 Project_Naming_File_Name (1 .. Name_Len);
690 Project_Naming_Id := Name_Find;
692 Project_Naming_File_Name
693 (Project_Naming_Last + 1 ..
694 Project_Naming_Last + Project_File_Extension'Length) :=
695 Project_File_Extension;
696 Project_Naming_Last :=
697 Project_Naming_Last + Project_File_Extension'Length;
699 -- Create the source list file name
701 Source_List_Last := Output_Name_Last;
702 Source_List_Path (1 .. Source_List_Last) :=
703 Output_Name (1 .. Source_List_Last);
705 (Source_List_Last + 1 ..
706 Source_List_Last + Source_List_File_Suffix'Length) :=
707 Source_List_File_Suffix;
708 Source_List_Last := Source_List_Last + Source_List_File_Suffix'Length;
710 -- Add the project file extension to the project name
713 (Output_Name_Last + 1 ..
714 Output_Name_Last + Project_File_Extension'Length) :=
715 Project_File_Extension;
716 Output_Name_Last := Output_Name_Last + Project_File_Extension'Length;
719 -- Change the current directory to the directory of the project file,
720 -- if any directory information is specified.
722 if Directory_Last /= 0 then
724 Change_Dir (Path_Name (1 .. Directory_Last));
726 when Directory_Error =>
728 ("unknown directory """,
729 Path_Name (1 .. Directory_Last),
736 -- Delete the source list file, if it already exists
742 (Source_List_Path (1 .. Source_List_Last),
746 -- And create a new source list file.
747 -- Fail if file cannot be created.
749 Source_List_FD := Create_New_File
750 (Name => Source_List_Path (1 .. Source_List_Last),
753 if Source_List_FD = Invalid_FD then
755 ("cannot create file """,
756 Source_List_Path (1 .. Source_List_Last),
761 -- Compile the regular expressions. Fails immediately if any of
762 -- the specified strings is in error.
764 for Index in Excluded_Expressions'Range loop
766 Output.Write_Str ("Excluded pattern: """);
767 Output.Write_Str (Excluded_Patterns (Index).all);
768 Output.Write_Line ("""");
772 Excluded_Expressions (Index) :=
773 Compile (Pattern => Excluded_Patterns (Index).all, Glob => True);
775 when Error_In_Regexp =>
777 ("invalid regular expression """,
778 Excluded_Patterns (Index).all,
783 for Index in Foreign_Expressions'Range loop
785 Output.Write_Str ("Foreign pattern: """);
786 Output.Write_Str (Foreign_Patterns (Index).all);
787 Output.Write_Line ("""");
791 Foreign_Expressions (Index) :=
792 Compile (Pattern => Foreign_Patterns (Index).all, Glob => True);
794 when Error_In_Regexp =>
796 ("invalid regular expression """,
797 Foreign_Patterns (Index).all,
802 for Index in Regular_Expressions'Range loop
804 Output.Write_Str ("Pattern: """);
805 Output.Write_Str (Name_Patterns (Index).all);
806 Output.Write_Line ("""");
810 Regular_Expressions (Index) :=
811 Compile (Pattern => Name_Patterns (Index).all, Glob => True);
814 when Error_In_Regexp =>
816 ("invalid regular expression """,
817 Name_Patterns (Index).all,
823 if Opt.Verbose_Mode then
824 Output.Write_Str ("Naming project file name is """);
826 (Project_Naming_File_Name (1 .. Project_Naming_Last));
827 Output.Write_Line ("""");
830 -- If there is already a project file with the specified name,
831 -- parse it to get the components that are not automatically
834 if Is_Regular_File (Output_Name (1 .. Output_Name_Last)) then
835 if Opt.Verbose_Mode then
836 Output.Write_Str ("Parsing already existing project file """);
837 Output.Write_Str (Output_Name (1 .. Output_Name_Last));
838 Output.Write_Line ("""");
842 (Project => Project_Node,
843 Project_File_Name => Output_Name (1 .. Output_Name_Last),
844 Always_Errout_Finalize => False);
846 -- If parsing was successful, remove the components that are
847 -- automatically generated, if any, so that they will be
848 -- unconditionally added later.
850 if Project_Node /= Empty_Node then
852 -- Remove the with clause for the naming project file
855 With_Clause : Project_Node_Id :=
856 First_With_Clause_Of (Project_Node);
857 Previous : Project_Node_Id := Empty_Node;
860 while With_Clause /= Empty_Node loop
861 if Tree.Name_Of (With_Clause) = Project_Naming_Id then
862 if Previous = Empty_Node then
863 Set_First_With_Clause_Of
865 To => Next_With_Clause_Of (With_Clause));
867 Set_Next_With_Clause_Of
869 To => Next_With_Clause_Of (With_Clause));
875 Previous := With_Clause;
876 With_Clause := Next_With_Clause_Of (With_Clause);
880 -- Remove attribute declarations of Source_Files,
881 -- Source_List_File, Source_Dirs, and the declaration of
882 -- package Naming, if they exist.
885 Declaration : Project_Node_Id :=
886 First_Declarative_Item_Of
887 (Project_Declaration_Of
889 Previous : Project_Node_Id := Empty_Node;
890 Current_Node : Project_Node_Id := Empty_Node;
893 while Declaration /= Empty_Node loop
894 Current_Node := Current_Item_Node (Declaration);
896 if (Kind_Of (Current_Node) = N_Attribute_Declaration
898 (Tree.Name_Of (Current_Node) = Name_Source_Files
899 or else Tree.Name_Of (Current_Node) =
900 Name_Source_List_File
901 or else Tree.Name_Of (Current_Node) =
904 (Kind_Of (Current_Node) = N_Package_Declaration
905 and then Tree.Name_Of (Current_Node) = Name_Naming)
907 if Previous = Empty_Node then
908 Set_First_Declarative_Item_Of
909 (Project_Declaration_Of (Project_Node),
910 To => Next_Declarative_Item (Declaration));
913 Set_Next_Declarative_Item
915 To => Next_Declarative_Item (Declaration));
919 Previous := Declaration;
922 Declaration := Next_Declarative_Item (Declaration);
928 -- If there were no already existing project file, or if the parsing
929 -- was unsuccessful, create an empty project node with the correct
930 -- name and its project declaration node.
932 if Project_Node = Empty_Node then
933 Project_Node := Default_Project_Node (Of_Kind => N_Project);
934 Set_Name_Of (Project_Node, To => Output_Name_Id);
935 Set_Project_Declaration_Of
937 To => Default_Project_Node (Of_Kind => N_Project_Declaration));
941 -- Create the naming project node, and add an attribute declaration
942 -- for Source_Files as an empty list, to indicate there are no
943 -- sources in the naming project.
945 Project_Naming_Node := Default_Project_Node (Of_Kind => N_Project);
946 Set_Name_Of (Project_Naming_Node, To => Project_Naming_Id);
947 Project_Naming_Decl :=
948 Default_Project_Node (Of_Kind => N_Project_Declaration);
949 Set_Project_Declaration_Of (Project_Naming_Node, Project_Naming_Decl);
951 Default_Project_Node (Of_Kind => N_Package_Declaration);
952 Set_Name_Of (Naming_Package, To => Name_Naming);
955 Decl_Item : constant Project_Node_Id :=
956 Default_Project_Node (Of_Kind => N_Declarative_Item);
958 Attribute : constant Project_Node_Id :=
960 (Of_Kind => N_Attribute_Declaration,
961 And_Expr_Kind => List);
963 Expression : constant Project_Node_Id :=
965 (Of_Kind => N_Expression,
966 And_Expr_Kind => List);
968 Term : constant Project_Node_Id :=
971 And_Expr_Kind => List);
973 Empty_List : constant Project_Node_Id :=
975 (Of_Kind => N_Literal_String_List);
978 Set_First_Declarative_Item_Of
979 (Project_Naming_Decl, To => Decl_Item);
980 Set_Next_Declarative_Item (Decl_Item, Naming_Package);
981 Set_Current_Item_Node (Decl_Item, To => Attribute);
982 Set_Name_Of (Attribute, To => Name_Source_Files);
983 Set_Expression_Of (Attribute, To => Expression);
984 Set_First_Term (Expression, To => Term);
985 Set_Current_Term (Term, To => Empty_List);
988 -- Add a with clause on the naming project in the main project
991 With_Clause : constant Project_Node_Id :=
992 Default_Project_Node (Of_Kind => N_With_Clause);
995 Set_Next_With_Clause_Of
996 (With_Clause, To => First_With_Clause_Of (Project_Node));
997 Set_First_With_Clause_Of (Project_Node, To => With_Clause);
998 Set_Name_Of (With_Clause, To => Project_Naming_Id);
1000 -- We set the project node to something different than
1001 -- Empty_Node, so that Prj.PP does not generate a limited
1004 Set_Project_Node_Of (With_Clause, Non_Empty_Node);
1006 Name_Len := Project_Naming_Last;
1007 Name_Buffer (1 .. Name_Len) :=
1008 Project_Naming_File_Name (1 .. Project_Naming_Last);
1009 Set_String_Value_Of (With_Clause, To => Name_Find);
1012 Project_Declaration := Project_Declaration_Of (Project_Node);
1014 -- Add a renaming declaration for package Naming in the main project
1017 Decl_Item : constant Project_Node_Id :=
1018 Default_Project_Node (Of_Kind => N_Declarative_Item);
1020 Naming : constant Project_Node_Id :=
1021 Default_Project_Node (Of_Kind => N_Package_Declaration);
1023 Set_Next_Declarative_Item
1025 To => First_Declarative_Item_Of (Project_Declaration));
1026 Set_First_Declarative_Item_Of
1027 (Project_Declaration, To => Decl_Item);
1028 Set_Current_Item_Node (Decl_Item, To => Naming);
1029 Set_Name_Of (Naming, To => Name_Naming);
1030 Set_Project_Of_Renamed_Package_Of
1031 (Naming, To => Project_Naming_Node);
1034 -- Add an attribute declaration for Source_Dirs, initialized as an
1035 -- empty list. Directories will be added as they are read from the
1036 -- directory list file.
1039 Decl_Item : constant Project_Node_Id :=
1040 Default_Project_Node (Of_Kind => N_Declarative_Item);
1042 Attribute : constant Project_Node_Id :=
1043 Default_Project_Node
1044 (Of_Kind => N_Attribute_Declaration,
1045 And_Expr_Kind => List);
1047 Expression : constant Project_Node_Id :=
1048 Default_Project_Node
1049 (Of_Kind => N_Expression,
1050 And_Expr_Kind => List);
1052 Term : constant Project_Node_Id :=
1053 Default_Project_Node
1054 (Of_Kind => N_Term, And_Expr_Kind => List);
1057 Set_Next_Declarative_Item
1059 To => First_Declarative_Item_Of (Project_Declaration));
1060 Set_First_Declarative_Item_Of
1061 (Project_Declaration, To => Decl_Item);
1062 Set_Current_Item_Node (Decl_Item, To => Attribute);
1063 Set_Name_Of (Attribute, To => Name_Source_Dirs);
1064 Set_Expression_Of (Attribute, To => Expression);
1065 Set_First_Term (Expression, To => Term);
1067 Default_Project_Node (Of_Kind => N_Literal_String_List,
1068 And_Expr_Kind => List);
1069 Set_Current_Term (Term, To => Source_Dirs_List);
1072 -- Add an attribute declaration for Source_List_File with the
1073 -- source list file name that will be created.
1076 Decl_Item : constant Project_Node_Id :=
1077 Default_Project_Node (Of_Kind => N_Declarative_Item);
1079 Attribute : constant Project_Node_Id :=
1080 Default_Project_Node
1081 (Of_Kind => N_Attribute_Declaration,
1082 And_Expr_Kind => Single);
1084 Expression : constant Project_Node_Id :=
1085 Default_Project_Node
1086 (Of_Kind => N_Expression,
1087 And_Expr_Kind => Single);
1089 Term : constant Project_Node_Id :=
1090 Default_Project_Node
1092 And_Expr_Kind => Single);
1094 Value : constant Project_Node_Id :=
1095 Default_Project_Node
1096 (Of_Kind => N_Literal_String,
1097 And_Expr_Kind => Single);
1100 Set_Next_Declarative_Item
1102 To => First_Declarative_Item_Of (Project_Declaration));
1103 Set_First_Declarative_Item_Of
1104 (Project_Declaration, To => Decl_Item);
1105 Set_Current_Item_Node (Decl_Item, To => Attribute);
1106 Set_Name_Of (Attribute, To => Name_Source_List_File);
1107 Set_Expression_Of (Attribute, To => Expression);
1108 Set_First_Term (Expression, To => Term);
1109 Set_Current_Term (Term, To => Value);
1110 Name_Len := Source_List_Last;
1111 Name_Buffer (1 .. Name_Len) :=
1112 Source_List_Path (1 .. Source_List_Last);
1113 Set_String_Value_Of (Value, To => Name_Find);
1117 -- Process each directory
1119 for Index in Directories'Range loop
1122 Dir_Name : constant String := Directories (Index).all;
1123 Last : Natural := Dir_Name'Last;
1124 Recursively : Boolean := False;
1126 if Dir_Name'Length >= 4
1127 and then (Dir_Name (Last - 2 .. Last) = "/**")
1130 Recursively := True;
1133 if Project_File then
1135 -- Add the directory in the list for attribute Source_Dirs
1138 Expression : constant Project_Node_Id :=
1139 Default_Project_Node
1140 (Of_Kind => N_Expression,
1141 And_Expr_Kind => Single);
1143 Term : constant Project_Node_Id :=
1144 Default_Project_Node
1146 And_Expr_Kind => Single);
1148 Value : constant Project_Node_Id :=
1149 Default_Project_Node
1150 (Of_Kind => N_Literal_String,
1151 And_Expr_Kind => Single);
1154 if Current_Source_Dir = Empty_Node then
1155 Set_First_Expression_In_List
1156 (Source_Dirs_List, To => Expression);
1158 Set_Next_Expression_In_List
1159 (Current_Source_Dir, To => Expression);
1162 Current_Source_Dir := Expression;
1163 Set_First_Term (Expression, To => Term);
1164 Set_Current_Term (Term, To => Value);
1165 Name_Len := Dir_Name'Length;
1166 Name_Buffer (1 .. Name_Len) := Dir_Name;
1167 Set_String_Value_Of (Value, To => Name_Find);
1171 Process_Directory (Dir_Name (Dir_Name'First .. Last), Recursively);
1176 if Project_File then
1177 Close (Source_List_FD);
1184 -- Delete the file if it already exists
1187 (Path_Name (Directory_Last + 1 .. Path_Last),
1188 Success => Discard);
1192 if Opt.Verbose_Mode then
1193 Output.Write_Str ("Creating new file """);
1194 Output.Write_Str (Path_Name (Directory_Last + 1 .. Path_Last));
1195 Output.Write_Line ("""");
1198 Output_FD := Create_New_File
1199 (Path_Name (Directory_Last + 1 .. Path_Last),
1202 -- Fails if project file cannot be created
1204 if Output_FD = Invalid_FD then
1206 ("cannot create new """, Path_Name (1 .. Path_Last), """");
1209 if Project_File then
1211 -- Output the project file
1215 W_Char => Write_A_Char'Access,
1216 W_Eol => Write_Eol'Access,
1217 W_Str => Write_A_String'Access,
1218 Backward_Compatibility => False);
1221 -- Delete the naming project file if it already exists
1224 (Project_Naming_File_Name (1 .. Project_Naming_Last),
1225 Success => Discard);
1229 if Opt.Verbose_Mode then
1230 Output.Write_Str ("Creating new naming project file """);
1231 Output.Write_Str (Project_Naming_File_Name
1232 (1 .. Project_Naming_Last));
1233 Output.Write_Line ("""");
1236 Output_FD := Create_New_File
1237 (Project_Naming_File_Name (1 .. Project_Naming_Last),
1240 -- Fails if naming project file cannot be created
1242 if Output_FD = Invalid_FD then
1244 ("cannot create new """,
1245 Project_Naming_File_Name (1 .. Project_Naming_Last),
1249 -- Output the naming project file
1252 (Project_Naming_Node,
1253 W_Char => Write_A_Char'Access,
1254 W_Eol => Write_Eol'Access,
1255 W_Str => Write_A_String'Access,
1256 Backward_Compatibility => False);
1260 -- Write to the output file each entry in the SFN_Pragmas table
1261 -- as an pragma Source_File_Name.
1263 for Index in 1 .. SFN_Pragmas.Last loop
1264 Write_A_String ("pragma Source_File_Name");
1266 Write_A_String (" (");
1268 (Get_Name_String (SFN_Pragmas.Table (Index).Unit));
1269 Write_A_String (",");
1272 if SFN_Pragmas.Table (Index).Spec then
1273 Write_A_String (" Spec_File_Name => """);
1276 Write_A_String (" Body_File_Name => """);
1280 (Get_Name_String (SFN_Pragmas.Table (Index).File));
1281 Write_A_String (""");");
1294 procedure Write_A_Char (C : Character) is
1296 Write_A_String ((1 => C));
1303 procedure Write_Eol is
1305 Write_A_String ((1 => ASCII.LF));
1308 --------------------
1309 -- Write_A_String --
1310 --------------------
1312 procedure Write_A_String (S : String) is
1313 Str : String (1 .. S'Length);
1316 if S'Length > 0 then
1319 if Write (Output_FD, Str (1)'Address, Str'Length) /= Str'Length then
1320 Prj.Com.Fail ("disk full");