1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2010, 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;
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 Very_Verbose : Boolean := False;
54 -- Set in call to Initialize to indicate very verbose output
56 Project_File : Boolean := False;
57 -- True when gnatname is creating/modifying a project file. False when
58 -- gnatname is creating a configuration pragmas file.
60 Tree : constant Project_Node_Tree_Ref := new Project_Node_Tree_Data;
61 -- The project tree where the project file is parsed
63 Args : Argument_List_Access;
64 -- The list of arguments for calls to the compiler to get the unit names
65 -- and kinds (spec or body) in the Ada sources.
67 Path_Name : String_Access;
71 Directory_Last : Natural := 0;
73 Output_Name : String_Access;
74 Output_Name_Last : Natural;
75 Output_Name_Id : Name_Id;
77 Project_Naming_File_Name : String_Access;
78 -- String (1 .. Output_Name'Length + Naming_File_Suffix'Length);
80 Project_Naming_Last : Natural;
81 Project_Naming_Id : Name_Id := No_Name;
83 Source_List_Path : String_Access;
84 -- (1 .. Output_Name'Length + Source_List_File_Suffix'Length);
85 Source_List_Last : Natural;
87 Source_List_FD : File_Descriptor;
89 Project_Node : Project_Node_Id := Empty_Node;
90 Project_Declaration : Project_Node_Id := Empty_Node;
91 Source_Dirs_List : Project_Node_Id := Empty_Node;
93 Project_Naming_Node : Project_Node_Id := Empty_Node;
94 Project_Naming_Decl : Project_Node_Id := Empty_Node;
95 Naming_Package : Project_Node_Id := Empty_Node;
96 Naming_Package_Comments : Project_Node_Id := Empty_Node;
98 Source_Files_Comments : Project_Node_Id := Empty_Node;
99 Source_Dirs_Comments : Project_Node_Id := Empty_Node;
100 Source_List_File_Comments : Project_Node_Id := Empty_Node;
102 Naming_String : aliased String := "naming";
104 Gnatname_Packages : aliased String_List := (1 => Naming_String'Access);
106 Packages_To_Check_By_Gnatname : constant String_List_Access :=
107 Gnatname_Packages'Access;
109 function Dup (Fd : File_Descriptor) return File_Descriptor;
111 procedure Dup2 (Old_Fd, New_Fd : File_Descriptor);
113 Gcc : constant String := "gcc";
114 Gcc_Path : String_Access := null;
116 Non_Empty_Node : constant Project_Node_Id := 1;
117 -- Used for the With_Clause of the naming project
119 type Matched_Type is (True, False, Excluded);
121 Naming_File_Suffix : constant String := "_naming";
122 Source_List_File_Suffix : constant String := "_source_list.txt";
124 Output_FD : File_Descriptor;
125 -- To save the project file and its naming project file
128 -- Output an empty line
130 procedure Write_A_Char (C : Character);
131 -- Write one character to Output_FD
133 procedure Write_A_String (S : String);
134 -- Write a String to Output_FD
136 package Processed_Directories is new Table.Table
137 (Table_Component_Type => String_Access,
138 Table_Index_Type => Natural,
139 Table_Low_Bound => 0,
141 Table_Increment => 100,
142 Table_Name => "Prj.Makr.Processed_Directories");
143 -- The list of already processed directories for each section, to avoid
144 -- processing several times the same directory in the same section.
146 package Source_Directories is new Table.Table
147 (Table_Component_Type => String_Access,
148 Table_Index_Type => Natural,
149 Table_Low_Bound => 0,
151 Table_Increment => 100,
152 Table_Name => "Prj.Makr.Source_Directories");
153 -- The complete list of directories to be put in attribute Source_Dirs in
156 type Source is record
163 package Sources is new Table.Table
164 (Table_Component_Type => Source,
165 Table_Index_Type => Natural,
166 Table_Low_Bound => 0,
168 Table_Increment => 100,
169 Table_Name => "Prj.Makr.Sources");
170 -- The list of Ada sources found, with their unit name and kind, to be put
171 -- in the source attribute and package Naming of the project file, or in
172 -- the pragmas Source_File_Name in the configuration pragmas file.
174 package Source_Files is new System.HTable.Simple_HTable
175 (Header_Num => Prj.Header_Num,
181 -- Hash table to keep track of source file names, to avoid putting several
182 -- times the same file name in case of multi-unit files.
188 function Dup (Fd : File_Descriptor) return File_Descriptor is
190 return File_Descriptor (System.CRTL.dup (Integer (Fd)));
197 procedure Dup2 (Old_Fd, New_Fd : File_Descriptor) is
199 pragma Warnings (Off, Fd);
201 Fd := System.CRTL.dup2 (Integer (Old_Fd), Integer (New_Fd));
208 procedure Finalize is
210 pragma Warnings (Off, Discard);
212 Current_Source_Dir : Project_Node_Id := Empty_Node;
216 -- If there were no already existing project file, or if the parsing
217 -- was unsuccessful, create an empty project node with the correct
218 -- name and its project declaration node.
220 if No (Project_Node) then
222 Default_Project_Node (Of_Kind => N_Project, In_Tree => Tree);
223 Set_Name_Of (Project_Node, Tree, To => Output_Name_Id);
224 Set_Project_Declaration_Of
226 To => Default_Project_Node
227 (Of_Kind => N_Project_Declaration, In_Tree => Tree));
233 -- Delete the file if it already exists
236 (Path_Name (Directory_Last + 1 .. Path_Last),
241 if Opt.Verbose_Mode then
242 Output.Write_Str ("Creating new file """);
243 Output.Write_Str (Path_Name (Directory_Last + 1 .. Path_Last));
244 Output.Write_Line ("""");
247 Output_FD := Create_New_File
248 (Path_Name (Directory_Last + 1 .. Path_Last),
251 -- Fails if project file cannot be created
253 if Output_FD = Invalid_FD then
255 ("cannot create new """ & Path_Name (1 .. Path_Last) & """");
260 -- Delete the source list file, if it already exists
264 pragma Warnings (Off, Discard);
267 (Source_List_Path (1 .. Source_List_Last),
271 -- And create a new source list file, fail if file cannot be created
273 Source_List_FD := Create_New_File
274 (Name => Source_List_Path (1 .. Source_List_Last),
277 if Source_List_FD = Invalid_FD then
279 ("cannot create file """
280 & Source_List_Path (1 .. Source_List_Last)
284 if Opt.Verbose_Mode then
285 Output.Write_Str ("Naming project file name is """);
287 (Project_Naming_File_Name (1 .. Project_Naming_Last));
288 Output.Write_Line ("""");
291 -- Create the naming project node
293 Project_Naming_Node :=
294 Default_Project_Node (Of_Kind => N_Project, In_Tree => Tree);
295 Set_Name_Of (Project_Naming_Node, Tree, To => Project_Naming_Id);
296 Project_Naming_Decl :=
298 (Of_Kind => N_Project_Declaration, In_Tree => Tree);
299 Set_Project_Declaration_Of
300 (Project_Naming_Node, Tree, Project_Naming_Decl);
303 (Of_Kind => N_Package_Declaration, In_Tree => Tree);
304 Set_Name_Of (Naming_Package, Tree, To => Name_Naming);
306 -- Add an attribute declaration for Source_Files as an empty list (to
307 -- indicate there are no sources in the naming project) and a package
308 -- Naming (that will be filled later).
311 Decl_Item : constant Project_Node_Id :=
313 (Of_Kind => N_Declarative_Item, In_Tree => Tree);
315 Attribute : constant Project_Node_Id :=
317 (Of_Kind => N_Attribute_Declaration,
319 And_Expr_Kind => List);
321 Expression : constant Project_Node_Id :=
323 (Of_Kind => N_Expression,
325 And_Expr_Kind => List);
327 Term : constant Project_Node_Id :=
331 And_Expr_Kind => List);
333 Empty_List : constant Project_Node_Id :=
335 (Of_Kind => N_Literal_String_List,
339 Set_First_Declarative_Item_Of
340 (Project_Naming_Decl, Tree, To => Decl_Item);
341 Set_Next_Declarative_Item (Decl_Item, Tree, Naming_Package);
342 Set_Current_Item_Node (Decl_Item, Tree, To => Attribute);
343 Set_Name_Of (Attribute, Tree, To => Name_Source_Files);
344 Set_Expression_Of (Attribute, Tree, To => Expression);
345 Set_First_Term (Expression, Tree, To => Term);
346 Set_Current_Term (Term, Tree, To => Empty_List);
349 -- Add a with clause on the naming project in the main project, if
350 -- there is not already one.
353 With_Clause : Project_Node_Id :=
354 First_With_Clause_Of (Project_Node, Tree);
357 while Present (With_Clause) loop
359 Prj.Tree.Name_Of (With_Clause, Tree) = Project_Naming_Id;
360 With_Clause := Next_With_Clause_Of (With_Clause, Tree);
363 if No (With_Clause) then
364 With_Clause := Default_Project_Node
365 (Of_Kind => N_With_Clause, In_Tree => Tree);
366 Set_Next_With_Clause_Of
368 To => First_With_Clause_Of (Project_Node, Tree));
369 Set_First_With_Clause_Of
370 (Project_Node, Tree, To => With_Clause);
371 Set_Name_Of (With_Clause, Tree, To => Project_Naming_Id);
373 -- We set the project node to something different than
374 -- Empty_Node, so that Prj.PP does not generate a limited
377 Set_Project_Node_Of (With_Clause, Tree, Non_Empty_Node);
379 Name_Len := Project_Naming_Last;
380 Name_Buffer (1 .. Name_Len) :=
381 Project_Naming_File_Name (1 .. Project_Naming_Last);
382 Set_String_Value_Of (With_Clause, Tree, To => Name_Find);
386 Project_Declaration := Project_Declaration_Of (Project_Node, Tree);
388 -- Add a package Naming in the main project, that is a renaming of
389 -- package Naming in the naming project.
392 Decl_Item : constant Project_Node_Id :=
394 (Of_Kind => N_Declarative_Item,
397 Naming : constant Project_Node_Id :=
399 (Of_Kind => N_Package_Declaration,
403 Set_Next_Declarative_Item
405 To => First_Declarative_Item_Of (Project_Declaration, Tree));
406 Set_First_Declarative_Item_Of
407 (Project_Declaration, Tree, To => Decl_Item);
408 Set_Current_Item_Node (Decl_Item, Tree, To => Naming);
409 Set_Name_Of (Naming, Tree, To => Name_Naming);
410 Set_Project_Of_Renamed_Package_Of
411 (Naming, Tree, To => Project_Naming_Node);
413 -- Attach the comments, if any, that were saved for package
416 Tree.Project_Nodes.Table (Naming).Comments :=
417 Naming_Package_Comments;
420 -- Add an attribute declaration for Source_Dirs, initialized as an
424 Decl_Item : constant Project_Node_Id :=
426 (Of_Kind => N_Declarative_Item,
429 Attribute : constant Project_Node_Id :=
431 (Of_Kind => N_Attribute_Declaration,
433 And_Expr_Kind => List);
435 Expression : constant Project_Node_Id :=
437 (Of_Kind => N_Expression,
439 And_Expr_Kind => List);
441 Term : constant Project_Node_Id :=
443 (Of_Kind => N_Term, In_Tree => Tree,
444 And_Expr_Kind => List);
447 Set_Next_Declarative_Item
449 To => First_Declarative_Item_Of (Project_Declaration, Tree));
450 Set_First_Declarative_Item_Of
451 (Project_Declaration, Tree, To => Decl_Item);
452 Set_Current_Item_Node (Decl_Item, Tree, To => Attribute);
453 Set_Name_Of (Attribute, Tree, To => Name_Source_Dirs);
454 Set_Expression_Of (Attribute, Tree, To => Expression);
455 Set_First_Term (Expression, Tree, To => Term);
458 (Of_Kind => N_Literal_String_List,
460 And_Expr_Kind => List);
461 Set_Current_Term (Term, Tree, To => Source_Dirs_List);
463 -- Attach the comments, if any, that were saved for attribute
466 Tree.Project_Nodes.Table (Attribute).Comments :=
467 Source_Dirs_Comments;
470 -- Put the source directories in attribute Source_Dirs
472 for Source_Dir_Index in 1 .. Source_Directories.Last loop
474 Expression : constant Project_Node_Id :=
476 (Of_Kind => N_Expression,
478 And_Expr_Kind => Single);
480 Term : constant Project_Node_Id :=
484 And_Expr_Kind => Single);
486 Value : constant Project_Node_Id :=
488 (Of_Kind => N_Literal_String,
490 And_Expr_Kind => Single);
493 if No (Current_Source_Dir) then
494 Set_First_Expression_In_List
495 (Source_Dirs_List, Tree, To => Expression);
497 Set_Next_Expression_In_List
498 (Current_Source_Dir, Tree, To => Expression);
501 Current_Source_Dir := Expression;
502 Set_First_Term (Expression, Tree, To => Term);
503 Set_Current_Term (Term, Tree, To => Value);
505 Add_Str_To_Name_Buffer
506 (Source_Directories.Table (Source_Dir_Index).all);
507 Set_String_Value_Of (Value, Tree, To => Name_Find);
511 -- Add an attribute declaration for Source_Files or Source_List_File
512 -- with the source list file name that will be created.
515 Decl_Item : constant Project_Node_Id :=
517 (Of_Kind => N_Declarative_Item,
520 Attribute : constant Project_Node_Id :=
522 (Of_Kind => N_Attribute_Declaration,
524 And_Expr_Kind => Single);
526 Expression : constant Project_Node_Id :=
528 (Of_Kind => N_Expression,
530 And_Expr_Kind => Single);
532 Term : constant Project_Node_Id :=
536 And_Expr_Kind => Single);
538 Value : constant Project_Node_Id :=
540 (Of_Kind => N_Literal_String,
542 And_Expr_Kind => Single);
545 Set_Next_Declarative_Item
547 To => First_Declarative_Item_Of (Project_Declaration, Tree));
548 Set_First_Declarative_Item_Of
549 (Project_Declaration, Tree, To => Decl_Item);
550 Set_Current_Item_Node (Decl_Item, Tree, To => Attribute);
552 Set_Name_Of (Attribute, Tree, To => Name_Source_List_File);
553 Set_Expression_Of (Attribute, Tree, To => Expression);
554 Set_First_Term (Expression, Tree, To => Term);
555 Set_Current_Term (Term, Tree, To => Value);
556 Name_Len := Source_List_Last;
557 Name_Buffer (1 .. Name_Len) :=
558 Source_List_Path (1 .. Source_List_Last);
559 Set_String_Value_Of (Value, Tree, To => Name_Find);
561 -- If there was no comments for attribute Source_List_File, put
562 -- those for Source_Files, if they exist.
564 if Present (Source_List_File_Comments) then
565 Tree.Project_Nodes.Table (Attribute).Comments :=
566 Source_List_File_Comments;
568 Tree.Project_Nodes.Table (Attribute).Comments :=
569 Source_Files_Comments;
573 -- Put the sources in the source list files and in the naming
576 for Source_Index in 1 .. Sources.Last loop
578 -- Add the corresponding attribute in the
579 -- Naming package of the naming project.
582 Current_Source : constant Source :=
583 Sources.Table (Source_Index);
585 Decl_Item : constant Project_Node_Id :=
591 Attribute : constant Project_Node_Id :=
594 N_Attribute_Declaration,
597 Expression : constant Project_Node_Id :=
599 (Of_Kind => N_Expression,
600 And_Expr_Kind => Single,
603 Term : constant Project_Node_Id :=
606 And_Expr_Kind => Single,
609 Value : constant Project_Node_Id :=
611 (Of_Kind => N_Literal_String,
612 And_Expr_Kind => Single,
616 -- Add source file name to the source list file if it is not
619 if not Source_Files.Get (Current_Source.File_Name) then
620 Source_Files.Set (Current_Source.File_Name, True);
621 Get_Name_String (Current_Source.File_Name);
622 Add_Char_To_Name_Buffer (ASCII.LF);
624 if Write (Source_List_FD,
625 Name_Buffer (1)'Address,
626 Name_Len) /= Name_Len
628 Prj.Com.Fail ("disk full");
632 -- For an Ada source, add entry in package Naming
634 if Current_Source.Unit_Name /= No_Name then
635 Set_Next_Declarative_Item
637 To => First_Declarative_Item_Of
638 (Naming_Package, Tree),
640 Set_First_Declarative_Item_Of
644 Set_Current_Item_Node
649 -- Is it a spec or a body?
651 if Current_Source.Spec then
661 -- Get the name of the unit
663 Get_Name_String (Current_Source.Unit_Name);
664 To_Lower (Name_Buffer (1 .. Name_Len));
665 Set_Associative_Array_Index_Of
666 (Attribute, Tree, To => Name_Find);
669 (Attribute, Tree, To => Expression);
671 (Expression, Tree, To => Term);
673 (Term, Tree, To => Value);
675 -- And set the name of the file
678 (Value, Tree, To => Current_Source.File_Name);
680 (Value, Tree, To => Current_Source.Index);
685 -- Close the source list file
687 Close (Source_List_FD);
689 -- Output the project file
693 W_Char => Write_A_Char'Access,
694 W_Eol => Write_Eol'Access,
695 W_Str => Write_A_String'Access,
696 Backward_Compatibility => False,
697 Max_Line_Length => 79);
700 -- Delete the naming project file if it already exists
703 (Project_Naming_File_Name (1 .. Project_Naming_Last),
708 if Opt.Verbose_Mode then
709 Output.Write_Str ("Creating new naming project file """);
710 Output.Write_Str (Project_Naming_File_Name
711 (1 .. Project_Naming_Last));
712 Output.Write_Line ("""");
715 Output_FD := Create_New_File
716 (Project_Naming_File_Name (1 .. Project_Naming_Last),
719 -- Fails if naming project file cannot be created
721 if Output_FD = Invalid_FD then
723 ("cannot create new """
724 & Project_Naming_File_Name (1 .. Project_Naming_Last)
728 -- Output the naming project file
731 (Project_Naming_Node, Tree,
732 W_Char => Write_A_Char'Access,
733 W_Eol => Write_Eol'Access,
734 W_Str => Write_A_String'Access,
735 Backward_Compatibility => False);
739 -- For each Ada source, write a pragma Source_File_Name to the
740 -- configuration pragmas file.
742 for Index in 1 .. Sources.Last loop
743 if Sources.Table (Index).Unit_Name /= No_Name then
744 Write_A_String ("pragma Source_File_Name");
746 Write_A_String (" (");
748 (Get_Name_String (Sources.Table (Index).Unit_Name));
749 Write_A_String (",");
752 if Sources.Table (Index).Spec then
753 Write_A_String (" Spec_File_Name => """);
756 Write_A_String (" Body_File_Name => """);
760 (Get_Name_String (Sources.Table (Index).File_Name));
762 Write_A_String ("""");
764 if Sources.Table (Index).Index /= 0 then
765 Write_A_String (", Index =>");
766 Write_A_String (Sources.Table (Index).Index'Img);
769 Write_A_String (");");
784 Project_File : Boolean;
785 Preproc_Switches : Argument_List;
786 Very_Verbose : Boolean;
787 Flags : Processing_Flags)
790 Makr.Very_Verbose := Initialize.Very_Verbose;
791 Makr.Project_File := Initialize.Project_File;
793 -- Do some needed initializations
797 Prj.Initialize (No_Project_Tree);
798 Prj.Tree.Initialize (Tree);
800 Sources.Set_Last (0);
801 Source_Directories.Set_Last (0);
803 -- Initialize the compiler switches
805 Args := new Argument_List (1 .. Preproc_Switches'Length + 6);
806 Args (1) := new String'("-c");
807 Args (2) := new String'("-gnats");
808 Args (3) := new String'("-gnatu");
809 Args (4 .. 3 + Preproc_Switches'Length) := Preproc_Switches;
810 Args (4 + Preproc_Switches'Length) := new String'("-x");
811 Args (5 + Preproc_Switches'Length) := new String'("ada");
813 -- Get the path and file names
816 String (1 .. File_Path'Length + Project_File_Extension'Length);
817 Path_Last := File_Path'Length;
819 if File_Names_Case_Sensitive then
820 Path_Name (1 .. Path_Last) := File_Path;
822 Path_Name (1 .. Path_Last) := To_Lower (File_Path);
825 Path_Name (Path_Last + 1 .. Path_Name'Last) :=
826 Project_File_Extension;
828 -- Get the end of directory information, if any
830 for Index in reverse 1 .. Path_Last loop
831 if Path_Name (Index) = Directory_Separator then
832 Directory_Last := Index;
838 if Path_Last < Project_File_Extension'Length + 1
840 (Path_Last - Project_File_Extension'Length + 1 .. Path_Last)
841 /= Project_File_Extension
843 Path_Last := Path_Name'Last;
846 Output_Name := new String'(To_Lower (Path_Name (1 .. Path_Last)));
847 Output_Name_Last := Output_Name'Last - 4;
849 -- If there is already a project file with the specified name, parse
850 -- it to get the components that are not automatically generated.
852 if Is_Regular_File (Output_Name (1 .. Path_Last)) then
853 if Opt.Verbose_Mode then
854 Output.Write_Str ("Parsing already existing project file """);
855 Output.Write_Str (Output_Name.all);
856 Output.Write_Line ("""");
861 Project => Project_Node,
862 Project_File_Name => Output_Name.all,
863 Always_Errout_Finalize => False,
864 Store_Comments => True,
865 Is_Config_File => False,
867 Current_Directory => Get_Current_Dir,
868 Packages_To_Check => Packages_To_Check_By_Gnatname);
870 -- Fail if parsing was not successful
872 if No (Project_Node) then
873 Prj.Com.Fail ("parsing of existing project file failed");
876 -- If parsing was successful, remove the components that are
877 -- automatically generated, if any, so that they will be
878 -- unconditionally added later.
880 -- Remove the with clause for the naming project file
883 With_Clause : Project_Node_Id :=
884 First_With_Clause_Of (Project_Node, Tree);
885 Previous : Project_Node_Id := Empty_Node;
888 while Present (With_Clause) loop
889 if Prj.Tree.Name_Of (With_Clause, Tree) =
892 if No (Previous) then
893 Set_First_With_Clause_Of
895 To => Next_With_Clause_Of (With_Clause, Tree));
897 Set_Next_With_Clause_Of
899 To => Next_With_Clause_Of (With_Clause, Tree));
905 Previous := With_Clause;
906 With_Clause := Next_With_Clause_Of (With_Clause, Tree);
910 -- Remove attribute declarations of Source_Files,
911 -- Source_List_File, Source_Dirs, and the declaration of
912 -- package Naming, if they exist, but preserve the comments
913 -- attached to these nodes.
916 Declaration : Project_Node_Id :=
917 First_Declarative_Item_Of
918 (Project_Declaration_Of
919 (Project_Node, Tree),
921 Previous : Project_Node_Id := Empty_Node;
922 Current_Node : Project_Node_Id := Empty_Node;
925 Kind_Of_Node : Project_Node_Kind;
926 Comments : Project_Node_Id;
929 while Present (Declaration) loop
930 Current_Node := Current_Item_Node (Declaration, Tree);
932 Kind_Of_Node := Kind_Of (Current_Node, Tree);
934 if Kind_Of_Node = N_Attribute_Declaration or else
935 Kind_Of_Node = N_Package_Declaration
937 Name := Prj.Tree.Name_Of (Current_Node, Tree);
939 if Name = Name_Source_Files or else
940 Name = Name_Source_List_File or else
941 Name = Name_Source_Dirs or else
945 Tree.Project_Nodes.Table (Current_Node).Comments;
947 if Name = Name_Source_Files then
948 Source_Files_Comments := Comments;
950 elsif Name = Name_Source_List_File then
951 Source_List_File_Comments := Comments;
953 elsif Name = Name_Source_Dirs then
954 Source_Dirs_Comments := Comments;
956 elsif Name = Name_Naming then
957 Naming_Package_Comments := Comments;
960 if No (Previous) then
961 Set_First_Declarative_Item_Of
962 (Project_Declaration_Of (Project_Node, Tree),
964 To => Next_Declarative_Item
965 (Declaration, Tree));
968 Set_Next_Declarative_Item
970 To => Next_Declarative_Item
971 (Declaration, Tree));
975 Previous := Declaration;
979 Declaration := Next_Declarative_Item (Declaration, Tree);
985 if Directory_Last /= 0 then
986 Output_Name (1 .. Output_Name_Last - Directory_Last) :=
987 Output_Name (Directory_Last + 1 .. Output_Name_Last);
988 Output_Name_Last := Output_Name_Last - Directory_Last;
991 -- Get the project name id
993 Name_Len := Output_Name_Last;
994 Name_Buffer (1 .. Name_Len) := Output_Name (1 .. Name_Len);
995 Output_Name_Id := Name_Find;
997 -- Create the project naming file name
999 Project_Naming_Last := Output_Name_Last;
1000 Project_Naming_File_Name :=
1001 new String'(Output_Name (1 .. Output_Name_Last) &
1002 Naming_File_Suffix &
1003 Project_File_Extension);
1004 Project_Naming_Last :=
1005 Project_Naming_Last + Naming_File_Suffix'Length;
1007 -- Get the project naming id
1009 Name_Len := Project_Naming_Last;
1010 Name_Buffer (1 .. Name_Len) :=
1011 Project_Naming_File_Name (1 .. Name_Len);
1012 Project_Naming_Id := Name_Find;
1014 Project_Naming_Last :=
1015 Project_Naming_Last + Project_File_Extension'Length;
1017 -- Create the source list file name
1019 Source_List_Last := Output_Name_Last;
1021 new String'(Output_Name (1 .. Output_Name_Last) &
1022 Source_List_File_Suffix);
1024 Output_Name_Last + Source_List_File_Suffix'Length;
1026 -- Add the project file extension to the project name
1029 (Output_Name_Last + 1 ..
1030 Output_Name_Last + Project_File_Extension'Length) :=
1031 Project_File_Extension;
1032 Output_Name_Last := Output_Name_Last + Project_File_Extension'Length;
1036 -- Change the current directory to the directory of the project file,
1037 -- if any directory information is specified.
1039 if Directory_Last /= 0 then
1041 Change_Dir (Path_Name (1 .. Directory_Last));
1043 when Directory_Error =>
1045 ("unknown directory """
1046 & Path_Name (1 .. Directory_Last)
1057 (Directories : Argument_List;
1058 Name_Patterns : Regexp_List;
1059 Excluded_Patterns : Regexp_List;
1060 Foreign_Patterns : Regexp_List)
1062 procedure Process_Directory (Dir_Name : String; Recursively : Boolean);
1063 -- Look for Ada and foreign sources in a directory, according to the
1064 -- patterns. When Recursively is True, after looking for sources in
1065 -- Dir_Name, look also in its subdirectories, if any.
1067 -----------------------
1068 -- Process_Directory --
1069 -----------------------
1071 procedure Process_Directory (Dir_Name : String; Recursively : Boolean) is
1072 Matched : Matched_Type := False;
1073 Str : String (1 .. 2_000);
1074 Canon : String (1 .. 2_000);
1077 Do_Process : Boolean := True;
1079 Temp_File_Name : String_Access := null;
1080 Save_Last_Source_Index : Natural := 0;
1081 File_Name_Id : Name_Id := No_Name;
1083 Current_Source : Source;
1086 -- Avoid processing the same directory more than once
1088 for Index in 1 .. Processed_Directories.Last loop
1089 if Processed_Directories.Table (Index).all = Dir_Name then
1090 Do_Process := False;
1096 if Opt.Verbose_Mode then
1097 Output.Write_Str ("Processing directory """);
1098 Output.Write_Str (Dir_Name);
1099 Output.Write_Line ("""");
1102 Processed_Directories. Increment_Last;
1103 Processed_Directories.Table (Processed_Directories.Last) :=
1104 new String'(Dir_Name);
1106 -- Get the source file names from the directory. Fails if the
1107 -- directory does not exist.
1110 Open (Dir, Dir_Name);
1112 when Directory_Error =>
1113 Prj.Com.Fail ("cannot open directory """ & Dir_Name & """");
1116 -- Process each regular file in the directory
1119 Read (Dir, Str, Last);
1120 exit File_Loop when Last = 0;
1122 -- Copy the file name and put it in canonical case to match
1123 -- against the patterns that have themselves already been put
1124 -- in canonical case.
1126 Canon (1 .. Last) := Str (1 .. Last);
1127 Canonical_Case_File_Name (Canon (1 .. Last));
1130 (Dir_Name & Directory_Separator & Str (1 .. Last))
1135 Name_Buffer (1 .. Name_Len) := Str (1 .. Last);
1136 File_Name_Id := Name_Find;
1138 -- First, check if the file name matches at least one of
1139 -- the excluded expressions;
1141 for Index in Excluded_Patterns'Range loop
1143 Match (Canon (1 .. Last), Excluded_Patterns (Index))
1145 Matched := Excluded;
1150 -- If it does not match any of the excluded expressions,
1151 -- check if the file name matches at least one of the
1152 -- regular expressions.
1154 if Matched = True then
1157 for Index in Name_Patterns'Range loop
1160 (Canon (1 .. Last), Name_Patterns (Index))
1169 or else (Matched = True and then Opt.Verbose_Mode)
1171 Output.Write_Str (" Checking """);
1172 Output.Write_Str (Str (1 .. Last));
1173 Output.Write_Line (""": ");
1176 -- If the file name matches one of the regular expressions,
1177 -- parse it to get its unit name.
1179 if Matched = True then
1181 FD : File_Descriptor;
1183 Saved_Output : File_Descriptor;
1184 Saved_Error : File_Descriptor;
1187 -- If we don't have the path of the compiler yet,
1188 -- get it now. The compiler name may have a prefix,
1189 -- so we get the potentially prefixed name.
1191 if Gcc_Path = null then
1193 Prefix_Gcc : String_Access :=
1194 Program_Name (Gcc, "gnatname");
1197 Locate_Exec_On_Path (Prefix_Gcc.all);
1201 if Gcc_Path = null then
1202 Prj.Com.Fail ("could not locate " & Gcc);
1206 -- If we don't have yet the file name of the
1207 -- temporary file, get it now.
1209 if Temp_File_Name = null then
1210 Create_Temp_File (FD, Temp_File_Name);
1212 if FD = Invalid_FD then
1214 ("could not create temporary file");
1218 Delete_File (Temp_File_Name.all, Success);
1221 Args (Args'Last) := new String'
1223 Directory_Separator &
1226 -- Create the temporary file
1228 FD := Create_Output_Text_File
1229 (Name => Temp_File_Name.all);
1231 if FD = Invalid_FD then
1233 ("could not create temporary file");
1236 -- Save the standard output and error
1238 Saved_Output := Dup (Standout);
1239 Saved_Error := Dup (Standerr);
1241 -- Set standard output and error to the temporary file
1243 Dup2 (FD, Standout);
1244 Dup2 (FD, Standerr);
1246 -- And spawn the compiler
1248 Spawn (Gcc_Path.all, Args.all, Success);
1250 -- Restore the standard output and error
1252 Dup2 (Saved_Output, Standout);
1253 Dup2 (Saved_Error, Standerr);
1255 -- Close the temporary file
1259 -- And close the saved standard output and error to
1260 -- avoid too many file descriptors.
1262 Close (Saved_Output);
1263 Close (Saved_Error);
1265 -- Now that standard output is restored, check if
1266 -- the compiler ran correctly.
1268 -- Read the lines of the temporary file:
1269 -- they should contain the kind and name of the unit.
1273 Text_Line : String (1 .. 1_000);
1274 Text_Last : Natural;
1277 Open (File, Temp_File_Name.all);
1279 if not Is_Valid (File) then
1281 ("could not read temporary file");
1284 Save_Last_Source_Index := Sources.Last;
1286 if End_Of_File (File) then
1287 if Opt.Verbose_Mode then
1289 Output.Write_Str (" (process died) ");
1294 Line_Loop : while not End_Of_File (File) loop
1295 Get_Line (File, Text_Line, Text_Last);
1297 -- Find the first closing parenthesis
1299 Char_Loop : for J in 1 .. Text_Last loop
1300 if Text_Line (J) = ')' then
1302 Text_Line (1 .. 4) = "Unit"
1304 -- Add entry to Sources table
1307 Name_Buffer (1 .. Name_Len) :=
1308 Text_Line (6 .. J - 7);
1310 (Unit_Name => Name_Find,
1311 File_Name => File_Name_Id,
1313 Spec => Text_Line (J - 5 .. J) =
1316 Sources.Append (Current_Source);
1325 if Save_Last_Source_Index = Sources.Last then
1326 if Opt.Verbose_Mode then
1327 Output.Write_Line (" not a unit");
1332 Save_Last_Source_Index + 1
1334 for Index in Save_Last_Source_Index + 1 ..
1337 Sources.Table (Index).Index :=
1338 Int (Index - Save_Last_Source_Index);
1342 for Index in Save_Last_Source_Index + 1 ..
1345 Current_Source := Sources.Table (Index);
1347 if Opt.Verbose_Mode then
1348 if Current_Source.Spec then
1349 Output.Write_Str (" spec of ");
1352 Output.Write_Str (" body of ");
1357 (Current_Source.Unit_Name));
1364 Delete_File (Temp_File_Name.all, Success);
1368 -- File name matches none of the regular expressions
1371 -- If file is not excluded, see if this is foreign source
1373 if Matched /= Excluded then
1374 for Index in Foreign_Patterns'Range loop
1375 if Match (Canon (1 .. Last),
1376 Foreign_Patterns (Index))
1384 if Very_Verbose then
1387 Output.Write_Line ("no match");
1390 Output.Write_Line ("excluded");
1393 Output.Write_Line ("foreign source");
1397 if Matched = True then
1399 -- Add source file name without unit name
1402 Add_Str_To_Name_Buffer (Canon (1 .. Last));
1404 ((File_Name => Name_Find,
1405 Unit_Name => No_Name,
1416 -- If Recursively is True, call itself for each subdirectory.
1417 -- We do that, even when this directory has already been processed,
1418 -- because all of its subdirectories may not have been processed.
1421 Open (Dir, Dir_Name);
1424 Read (Dir, Str, Last);
1427 -- Do not call itself for "." or ".."
1430 (Dir_Name & Directory_Separator & Str (1 .. Last))
1431 and then Str (1 .. Last) /= "."
1432 and then Str (1 .. Last) /= ".."
1435 (Dir_Name & Directory_Separator & Str (1 .. Last),
1436 Recursively => True);
1442 end Process_Directory;
1444 -- Start of processing for Process
1447 Processed_Directories.Set_Last (0);
1449 -- Process each directory
1451 for Index in Directories'Range loop
1454 Dir_Name : constant String := Directories (Index).all;
1455 Last : Natural := Dir_Name'Last;
1456 Recursively : Boolean := False;
1458 Canonical : String (1 .. Dir_Name'Length) := Dir_Name;
1461 Canonical_Case_File_Name (Canonical);
1464 for J in 1 .. Source_Directories.Last loop
1465 if Source_Directories.Table (J).all = Canonical then
1472 Source_Directories.Append (new String'(Canonical));
1475 if Dir_Name'Length >= 4
1476 and then (Dir_Name (Last - 2 .. Last) = "/**")
1479 Recursively := True;
1482 Process_Directory (Dir_Name (Dir_Name'First .. Last), Recursively);
1491 procedure Write_A_Char (C : Character) is
1493 Write_A_String ((1 => C));
1500 procedure Write_Eol is
1502 Write_A_String ((1 => ASCII.LF));
1505 --------------------
1506 -- Write_A_String --
1507 --------------------
1509 procedure Write_A_String (S : String) is
1510 Str : String (1 .. S'Length);
1513 if S'Length > 0 then
1516 if Write (Output_FD, Str (1)'Address, Str'Length) /= Str'Length then
1517 Prj.Com.Fail ("disk full");