PR c++/33620
[official-gcc.git] / gcc / ada / prj-makr.adb
blobaef87437b882f1a7ff4ed05f8eda49f284983e01
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- P R J . M A K R --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2001-2007, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Csets;
27 with Opt;
28 with Output;
29 with Osint; use Osint;
30 with Prj; use Prj;
31 with Prj.Com;
32 with Prj.Part;
33 with Prj.PP;
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;
43 with System.CRTL;
44 with System.Regexp; use System.Regexp;
46 package body Prj.Makr is
48 -- Packages of project files where unknown attributes are errors
50 -- All the following need comments ??? All global variables and
51 -- subprograms must be fully commented.
53 Naming_String : aliased String := "naming";
55 Gnatname_Packages : aliased String_List := (1 => Naming_String'Access);
57 Packages_To_Check_By_Gnatname : constant String_List_Access :=
58 Gnatname_Packages'Access;
60 function Dup (Fd : File_Descriptor) return File_Descriptor;
62 procedure Dup2 (Old_Fd, New_Fd : File_Descriptor);
64 Gcc : constant String := "gcc";
65 Gcc_Path : String_Access := null;
67 Non_Empty_Node : constant Project_Node_Id := 1;
68 -- Used for the With_Clause of the naming project
70 type Matched_Type is (True, False, Excluded);
72 Naming_File_Suffix : constant String := "_naming";
73 Source_List_File_Suffix : constant String := "_source_list.txt";
75 Output_FD : File_Descriptor;
76 -- To save the project file and its naming project file
78 procedure Write_Eol;
79 -- Output an empty line
81 procedure Write_A_Char (C : Character);
82 -- Write one character to Output_FD
84 procedure Write_A_String (S : String);
85 -- Write a String to Output_FD
87 package Processed_Directories is new Table.Table
88 (Table_Component_Type => String_Access,
89 Table_Index_Type => Natural,
90 Table_Low_Bound => 0,
91 Table_Initial => 10,
92 Table_Increment => 100,
93 Table_Name => "Prj.Makr.Processed_Directories");
95 ---------
96 -- Dup --
97 ---------
99 function Dup (Fd : File_Descriptor) return File_Descriptor is
100 begin
101 return File_Descriptor (System.CRTL.dup (Integer (Fd)));
102 end Dup;
104 ----------
105 -- Dup2 --
106 ----------
108 procedure Dup2 (Old_Fd, New_Fd : File_Descriptor) is
109 Fd : Integer;
110 pragma Warnings (Off, Fd);
111 begin
112 Fd := System.CRTL.dup2 (Integer (Old_Fd), Integer (New_Fd));
113 end Dup2;
115 ----------
116 -- Make --
117 ----------
119 procedure Make
120 (File_Path : String;
121 Project_File : Boolean;
122 Directories : Argument_List;
123 Name_Patterns : Argument_List;
124 Excluded_Patterns : Argument_List;
125 Foreign_Patterns : Argument_List;
126 Preproc_Switches : Argument_List;
127 Very_Verbose : Boolean)
129 Tree : constant Project_Node_Tree_Ref := new Project_Node_Tree_Data;
131 Path_Name : String (1 .. File_Path'Length +
132 Project_File_Extension'Length);
133 Path_Last : Natural := File_Path'Length;
135 Directory_Last : Natural := 0;
137 Output_Name : String (Path_Name'Range);
138 Output_Name_Last : Natural;
139 Output_Name_Id : Name_Id;
141 Project_Node : Project_Node_Id := Empty_Node;
142 Project_Declaration : Project_Node_Id := Empty_Node;
143 Source_Dirs_List : Project_Node_Id := Empty_Node;
144 Current_Source_Dir : Project_Node_Id := Empty_Node;
146 Project_Naming_Node : Project_Node_Id := Empty_Node;
147 Project_Naming_Decl : Project_Node_Id := Empty_Node;
148 Naming_Package : Project_Node_Id := Empty_Node;
149 Naming_Package_Comments : Project_Node_Id := Empty_Node;
151 Source_Files_Comments : Project_Node_Id := Empty_Node;
152 Source_Dirs_Comments : Project_Node_Id := Empty_Node;
153 Source_List_File_Comments : Project_Node_Id := Empty_Node;
155 Project_Naming_File_Name : String (1 .. Output_Name'Length +
156 Naming_File_Suffix'Length);
158 Project_Naming_Last : Natural;
159 Project_Naming_Id : Name_Id := No_Name;
161 Excluded_Expressions : array (Excluded_Patterns'Range) of Regexp;
162 Regular_Expressions : array (Name_Patterns'Range) of Regexp;
163 Foreign_Expressions : array (Foreign_Patterns'Range) of Regexp;
165 Source_List_Path : String (1 .. Output_Name'Length +
166 Source_List_File_Suffix'Length);
167 Source_List_Last : Natural;
169 Source_List_FD : File_Descriptor;
171 Args : Argument_List (1 .. Preproc_Switches'Length + 6);
173 type SFN_Pragma is record
174 Unit : Name_Id;
175 File : Name_Id;
176 Index : Int := 0;
177 Spec : Boolean;
178 end record;
180 package SFN_Pragmas is new Table.Table
181 (Table_Component_Type => SFN_Pragma,
182 Table_Index_Type => Natural,
183 Table_Low_Bound => 0,
184 Table_Initial => 50,
185 Table_Increment => 100,
186 Table_Name => "Prj.Makr.SFN_Pragmas");
188 procedure Process_Directory (Dir_Name : String; Recursively : Boolean);
189 -- Look for Ada and foreign sources in a directory, according to the
190 -- patterns. When Recursively is True, after looking for sources in
191 -- Dir_Name, look also in its subdirectories, if any.
193 -----------------------
194 -- Process_Directory --
195 -----------------------
197 procedure Process_Directory (Dir_Name : String; Recursively : Boolean) is
198 Matched : Matched_Type := False;
199 Str : String (1 .. 2_000);
200 Canon : String (1 .. 2_000);
201 Last : Natural;
202 Dir : Dir_Type;
203 Process : Boolean := True;
205 Temp_File_Name : String_Access := null;
206 Save_Last_Pragma_Index : Natural := 0;
207 File_Name_Id : Name_Id := No_Name;
208 SFN_Prag : SFN_Pragma;
210 begin
211 -- Avoid processing the same directory more than once
213 for Index in 1 .. Processed_Directories.Last loop
214 if Processed_Directories.Table (Index).all = Dir_Name then
215 Process := False;
216 exit;
217 end if;
218 end loop;
220 if Process then
221 if Opt.Verbose_Mode then
222 Output.Write_Str ("Processing directory """);
223 Output.Write_Str (Dir_Name);
224 Output.Write_Line ("""");
225 end if;
227 Processed_Directories. Increment_Last;
228 Processed_Directories.Table (Processed_Directories.Last) :=
229 new String'(Dir_Name);
231 -- Get the source file names from the directory. Fails if the
232 -- directory does not exist.
234 begin
235 Open (Dir, Dir_Name);
236 exception
237 when Directory_Error =>
238 Prj.Com.Fail ("cannot open directory """, Dir_Name, """");
239 end;
241 -- Process each regular file in the directory
243 File_Loop : loop
244 Read (Dir, Str, Last);
245 exit File_Loop when Last = 0;
247 -- Copy the file name and put it in canonical case to match
248 -- against the patterns that have themselves already been put
249 -- in canonical case.
251 Canon (1 .. Last) := Str (1 .. Last);
252 Canonical_Case_File_Name (Canon (1 .. Last));
254 if Is_Regular_File
255 (Dir_Name & Directory_Separator & Str (1 .. Last))
256 then
257 Matched := True;
259 Name_Len := Last;
260 Name_Buffer (1 .. Name_Len) := Str (1 .. Last);
261 File_Name_Id := Name_Find;
263 -- First, check if the file name matches at least one of
264 -- the excluded expressions;
266 for Index in Excluded_Expressions'Range loop
268 Match (Canon (1 .. Last), Excluded_Expressions (Index))
269 then
270 Matched := Excluded;
271 exit;
272 end if;
273 end loop;
275 -- If it does not match any of the excluded expressions,
276 -- check if the file name matches at least one of the
277 -- regular expressions.
279 if Matched = True then
280 Matched := False;
282 for Index in Regular_Expressions'Range loop
284 Match
285 (Canon (1 .. Last), Regular_Expressions (Index))
286 then
287 Matched := True;
288 exit;
289 end if;
290 end loop;
291 end if;
293 if Very_Verbose
294 or else (Matched = True and then Opt.Verbose_Mode)
295 then
296 Output.Write_Str (" Checking """);
297 Output.Write_Str (Str (1 .. Last));
298 Output.Write_Line (""": ");
299 end if;
301 -- If the file name matches one of the regular expressions,
302 -- parse it to get its unit name.
304 if Matched = True then
305 declare
306 FD : File_Descriptor;
307 Success : Boolean;
308 Saved_Output : File_Descriptor;
309 Saved_Error : File_Descriptor;
311 begin
312 -- If we don't have the path of the compiler yet,
313 -- get it now. The compiler name may have a prefix,
314 -- so we get the potentially prefixed name.
316 if Gcc_Path = null then
317 declare
318 Prefix_Gcc : String_Access :=
319 Program_Name (Gcc);
320 begin
321 Gcc_Path :=
322 Locate_Exec_On_Path (Prefix_Gcc.all);
323 Free (Prefix_Gcc);
324 end;
326 if Gcc_Path = null then
327 Prj.Com.Fail ("could not locate " & Gcc);
328 end if;
329 end if;
331 -- If we don't have yet the file name of the
332 -- temporary file, get it now.
334 if Temp_File_Name = null then
335 Create_Temp_File (FD, Temp_File_Name);
337 if FD = Invalid_FD then
338 Prj.Com.Fail
339 ("could not create temporary file");
340 end if;
342 Close (FD);
343 Delete_File (Temp_File_Name.all, Success);
344 end if;
346 Args (Args'Last) := new String'
347 (Dir_Name &
348 Directory_Separator &
349 Str (1 .. Last));
351 -- Create the temporary file
353 FD := Create_Output_Text_File
354 (Name => Temp_File_Name.all);
356 if FD = Invalid_FD then
357 Prj.Com.Fail
358 ("could not create temporary file");
359 end if;
361 -- Save the standard output and error
363 Saved_Output := Dup (Standout);
364 Saved_Error := Dup (Standerr);
366 -- Set standard output and error to the temporary file
368 Dup2 (FD, Standout);
369 Dup2 (FD, Standerr);
371 -- And spawn the compiler
373 Spawn (Gcc_Path.all, Args, Success);
375 -- Restore the standard output and error
377 Dup2 (Saved_Output, Standout);
378 Dup2 (Saved_Error, Standerr);
380 -- Close the temporary file
382 Close (FD);
384 -- And close the saved standard output and error to
385 -- avoid too many file descriptors.
387 Close (Saved_Output);
388 Close (Saved_Error);
390 -- Now that standard output is restored, check if
391 -- the compiler ran correctly.
393 -- Read the lines of the temporary file:
394 -- they should contain the kind and name of the unit.
396 declare
397 File : Text_File;
398 Text_Line : String (1 .. 1_000);
399 Text_Last : Natural;
401 begin
402 Open (File, Temp_File_Name.all);
404 if not Is_Valid (File) then
405 Prj.Com.Fail
406 ("could not read temporary file");
407 end if;
409 Save_Last_Pragma_Index := SFN_Pragmas.Last;
411 if End_Of_File (File) then
412 if Opt.Verbose_Mode then
413 if not Success then
414 Output.Write_Str (" (process died) ");
415 end if;
416 end if;
418 else
419 Line_Loop : while not End_Of_File (File) loop
420 Get_Line (File, Text_Line, Text_Last);
422 -- Find the first closing parenthesis
424 Char_Loop : for J in 1 .. Text_Last loop
425 if Text_Line (J) = ')' then
426 if J >= 13 and then
427 Text_Line (1 .. 4) = "Unit"
428 then
429 -- Add entry to SFN_Pragmas table
431 Name_Len := J - 12;
432 Name_Buffer (1 .. Name_Len) :=
433 Text_Line (6 .. J - 7);
434 SFN_Prag :=
435 (Unit => Name_Find,
436 File => File_Name_Id,
437 Index => 0,
438 Spec => Text_Line (J - 5 .. J) =
439 "(spec)");
441 SFN_Pragmas.Increment_Last;
442 SFN_Pragmas.Table
443 (SFN_Pragmas.Last) := SFN_Prag;
444 end if;
445 exit Char_Loop;
446 end if;
447 end loop Char_Loop;
448 end loop Line_Loop;
449 end if;
451 if Save_Last_Pragma_Index = SFN_Pragmas.Last then
452 if Opt.Verbose_Mode then
453 Output.Write_Line (" not a unit");
454 end if;
456 else
457 if SFN_Pragmas.Last >
458 Save_Last_Pragma_Index + 1
459 then
460 for Index in Save_Last_Pragma_Index + 1 ..
461 SFN_Pragmas.Last
462 loop
463 SFN_Pragmas.Table (Index).Index :=
464 Int (Index - Save_Last_Pragma_Index);
465 end loop;
466 end if;
468 for Index in Save_Last_Pragma_Index + 1 ..
469 SFN_Pragmas.Last
470 loop
471 SFN_Prag := SFN_Pragmas.Table (Index);
473 if Opt.Verbose_Mode then
474 if SFN_Prag.Spec then
475 Output.Write_Str (" spec of ");
477 else
478 Output.Write_Str (" body of ");
479 end if;
481 Output.Write_Line
482 (Get_Name_String (SFN_Prag.Unit));
483 end if;
485 if Project_File then
487 -- Add the corresponding attribute in the
488 -- Naming package of the naming project.
490 declare
491 Decl_Item : constant Project_Node_Id :=
492 Default_Project_Node
493 (Of_Kind =>
494 N_Declarative_Item,
495 In_Tree => Tree);
497 Attribute : constant Project_Node_Id :=
498 Default_Project_Node
499 (Of_Kind =>
500 N_Attribute_Declaration,
501 In_Tree => Tree);
503 Expression : constant Project_Node_Id :=
504 Default_Project_Node
505 (Of_Kind => N_Expression,
506 And_Expr_Kind => Single,
507 In_Tree => Tree);
509 Term : constant Project_Node_Id :=
510 Default_Project_Node
511 (Of_Kind => N_Term,
512 And_Expr_Kind => Single,
513 In_Tree => Tree);
515 Value : constant Project_Node_Id :=
516 Default_Project_Node
517 (Of_Kind => N_Literal_String,
518 And_Expr_Kind => Single,
519 In_Tree => Tree);
521 begin
522 Set_Next_Declarative_Item
523 (Decl_Item,
524 To => First_Declarative_Item_Of
525 (Naming_Package, Tree),
526 In_Tree => Tree);
527 Set_First_Declarative_Item_Of
528 (Naming_Package,
529 To => Decl_Item,
530 In_Tree => Tree);
531 Set_Current_Item_Node
532 (Decl_Item,
533 To => Attribute,
534 In_Tree => Tree);
536 -- Is it a spec or a body?
538 if SFN_Prag.Spec then
539 Set_Name_Of
540 (Attribute, Tree,
541 To => Name_Spec);
542 else
543 Set_Name_Of
544 (Attribute, Tree,
545 To => Name_Body);
546 end if;
548 -- Get the name of the unit
550 Get_Name_String (SFN_Prag.Unit);
551 To_Lower (Name_Buffer (1 .. Name_Len));
552 Set_Associative_Array_Index_Of
553 (Attribute, Tree, To => Name_Find);
555 Set_Expression_Of
556 (Attribute, Tree, To => Expression);
557 Set_First_Term
558 (Expression, Tree, To => Term);
559 Set_Current_Term
560 (Term, Tree, To => Value);
562 -- And set the name of the file
564 Set_String_Value_Of
565 (Value, Tree, To => File_Name_Id);
566 Set_Source_Index_Of
567 (Value, Tree, To => SFN_Prag.Index);
568 end;
569 end if;
570 end loop;
572 if Project_File then
573 -- Add source file name to source list
574 -- file.
576 Last := Last + 1;
577 Str (Last) := ASCII.LF;
579 if Write (Source_List_FD,
580 Str (1)'Address,
581 Last) /= Last
582 then
583 Prj.Com.Fail ("disk full");
584 end if;
585 end if;
586 end if;
588 Close (File);
590 Delete_File (Temp_File_Name.all, Success);
591 end;
592 end;
594 -- File name matches none of the regular expressions
596 else
597 -- If file is not excluded, see if this is foreign source
599 if Matched /= Excluded then
600 for Index in Foreign_Expressions'Range loop
601 if Match (Canon (1 .. Last),
602 Foreign_Expressions (Index))
603 then
604 Matched := True;
605 exit;
606 end if;
607 end loop;
608 end if;
610 if Very_Verbose then
611 case Matched is
612 when False =>
613 Output.Write_Line ("no match");
615 when Excluded =>
616 Output.Write_Line ("excluded");
618 when True =>
619 Output.Write_Line ("foreign source");
620 end case;
621 end if;
623 if Project_File and Matched = True then
625 -- Add source file name to source list file
627 Last := Last + 1;
628 Str (Last) := ASCII.LF;
630 if Write (Source_List_FD,
631 Str (1)'Address,
632 Last) /= Last
633 then
634 Prj.Com.Fail ("disk full");
635 end if;
636 end if;
637 end if;
638 end if;
639 end loop File_Loop;
641 Close (Dir);
642 end if;
644 -- If Recursively is True, call itself for each subdirectory.
645 -- We do that, even when this directory has already been processed,
646 -- because all of its subdirectories may not have been processed.
648 if Recursively then
649 Open (Dir, Dir_Name);
651 loop
652 Read (Dir, Str, Last);
653 exit when Last = 0;
655 -- Do not call itself for "." or ".."
657 if Is_Directory
658 (Dir_Name & Directory_Separator & Str (1 .. Last))
659 and then Str (1 .. Last) /= "."
660 and then Str (1 .. Last) /= ".."
661 then
662 Process_Directory
663 (Dir_Name & Directory_Separator & Str (1 .. Last),
664 Recursively => True);
665 end if;
666 end loop;
668 Close (Dir);
669 end if;
670 end Process_Directory;
672 -- Start of processing for Make
674 begin
675 -- Do some needed initializations
677 Csets.Initialize;
678 Namet.Initialize;
679 Snames.Initialize;
680 Prj.Initialize (No_Project_Tree);
681 Prj.Tree.Initialize (Tree);
683 SFN_Pragmas.Set_Last (0);
685 Processed_Directories.Set_Last (0);
687 -- Initialize the compiler switches
689 Args (1) := new String'("-c");
690 Args (2) := new String'("-gnats");
691 Args (3) := new String'("-gnatu");
692 Args (4 .. 3 + Preproc_Switches'Length) := Preproc_Switches;
693 Args (4 + Preproc_Switches'Length) := new String'("-x");
694 Args (5 + Preproc_Switches'Length) := new String'("ada");
696 -- Get the path and file names
698 if File_Names_Case_Sensitive then
699 Path_Name (1 .. Path_Last) := File_Path;
700 else
701 Path_Name (1 .. Path_Last) := To_Lower (File_Path);
702 end if;
704 Path_Name (Path_Last + 1 .. Path_Name'Last) :=
705 Project_File_Extension;
707 -- Get the end of directory information, if any
709 for Index in reverse 1 .. Path_Last loop
710 if Path_Name (Index) = Directory_Separator then
711 Directory_Last := Index;
712 exit;
713 end if;
714 end loop;
716 if Project_File then
717 if Path_Last < Project_File_Extension'Length + 1
718 or else Path_Name
719 (Path_Last - Project_File_Extension'Length + 1 .. Path_Last)
720 /= Project_File_Extension
721 then
722 Path_Last := Path_Name'Last;
723 end if;
725 Output_Name (1 .. Path_Last) := To_Lower (Path_Name (1 .. Path_Last));
726 Output_Name_Last := Path_Last - Project_File_Extension'Length;
728 -- If there is already a project file with the specified name, parse
729 -- it to get the components that are not automatically generated.
731 if Is_Regular_File (Output_Name (1 .. Path_Last)) then
732 if Opt.Verbose_Mode then
733 Output.Write_Str ("Parsing already existing project file """);
734 Output.Write_Str (Output_Name (1 .. Output_Name_Last));
735 Output.Write_Line ("""");
736 end if;
738 Part.Parse
739 (In_Tree => Tree,
740 Project => Project_Node,
741 Project_File_Name => Output_Name (1 .. Output_Name_Last),
742 Always_Errout_Finalize => False,
743 Store_Comments => True,
744 Packages_To_Check => Packages_To_Check_By_Gnatname);
746 -- Fail if parsing was not successful
748 if Project_Node = Empty_Node then
749 Fail ("parsing of existing project file failed");
751 else
752 -- If parsing was successful, remove the components that are
753 -- automatically generated, if any, so that they will be
754 -- unconditionally added later.
756 -- Remove the with clause for the naming project file
758 declare
759 With_Clause : Project_Node_Id :=
760 First_With_Clause_Of (Project_Node, Tree);
761 Previous : Project_Node_Id := Empty_Node;
763 begin
764 while With_Clause /= Empty_Node loop
765 if Prj.Tree.Name_Of (With_Clause, Tree) =
766 Project_Naming_Id
767 then
768 if Previous = Empty_Node then
769 Set_First_With_Clause_Of
770 (Project_Node, Tree,
771 To => Next_With_Clause_Of (With_Clause, Tree));
772 else
773 Set_Next_With_Clause_Of
774 (Previous, Tree,
775 To => Next_With_Clause_Of (With_Clause, Tree));
776 end if;
778 exit;
779 end if;
781 Previous := With_Clause;
782 With_Clause := Next_With_Clause_Of (With_Clause, Tree);
783 end loop;
784 end;
786 -- Remove attribute declarations of Source_Files,
787 -- Source_List_File, Source_Dirs, and the declaration of
788 -- package Naming, if they exist, but preserve the comments
789 -- attached to these nodes.
791 declare
792 Declaration : Project_Node_Id :=
793 First_Declarative_Item_Of
794 (Project_Declaration_Of
795 (Project_Node, Tree),
796 Tree);
797 Previous : Project_Node_Id := Empty_Node;
798 Current_Node : Project_Node_Id := Empty_Node;
800 Name : Name_Id;
801 Kind_Of_Node : Project_Node_Kind;
802 Comments : Project_Node_Id;
804 begin
805 while Declaration /= Empty_Node loop
806 Current_Node := Current_Item_Node (Declaration, Tree);
808 Kind_Of_Node := Kind_Of (Current_Node, Tree);
810 if Kind_Of_Node = N_Attribute_Declaration or else
811 Kind_Of_Node = N_Package_Declaration
812 then
813 Name := Prj.Tree.Name_Of (Current_Node, Tree);
815 if Name = Name_Source_Files or else
816 Name = Name_Source_List_File or else
817 Name = Name_Source_Dirs or else
818 Name = Name_Naming
819 then
820 Comments :=
821 Tree.Project_Nodes.Table (Current_Node).Comments;
823 if Name = Name_Source_Files then
824 Source_Files_Comments := Comments;
826 elsif Name = Name_Source_List_File then
827 Source_List_File_Comments := Comments;
829 elsif Name = Name_Source_Dirs then
830 Source_Dirs_Comments := Comments;
832 elsif Name = Name_Naming then
833 Naming_Package_Comments := Comments;
834 end if;
836 if Previous = Empty_Node then
837 Set_First_Declarative_Item_Of
838 (Project_Declaration_Of (Project_Node, Tree),
839 Tree,
840 To => Next_Declarative_Item
841 (Declaration, Tree));
843 else
844 Set_Next_Declarative_Item
845 (Previous, Tree,
846 To => Next_Declarative_Item
847 (Declaration, Tree));
848 end if;
850 else
851 Previous := Declaration;
852 end if;
853 end if;
855 Declaration := Next_Declarative_Item (Declaration, Tree);
856 end loop;
857 end;
858 end if;
859 end if;
861 if Directory_Last /= 0 then
862 Output_Name (1 .. Output_Name_Last - Directory_Last) :=
863 Output_Name (Directory_Last + 1 .. Output_Name_Last);
864 Output_Name_Last := Output_Name_Last - Directory_Last;
865 end if;
867 -- Get the project name id
869 Name_Len := Output_Name_Last;
870 Name_Buffer (1 .. Name_Len) := Output_Name (1 .. Name_Len);
871 Output_Name_Id := Name_Find;
873 -- Create the project naming file name
875 Project_Naming_Last := Output_Name_Last;
876 Project_Naming_File_Name (1 .. Project_Naming_Last) :=
877 Output_Name (1 .. Project_Naming_Last);
878 Project_Naming_File_Name
879 (Project_Naming_Last + 1 ..
880 Project_Naming_Last + Naming_File_Suffix'Length) :=
881 Naming_File_Suffix;
882 Project_Naming_Last :=
883 Project_Naming_Last + Naming_File_Suffix'Length;
885 -- Get the project naming id
887 Name_Len := Project_Naming_Last;
888 Name_Buffer (1 .. Name_Len) :=
889 Project_Naming_File_Name (1 .. Name_Len);
890 Project_Naming_Id := Name_Find;
892 Project_Naming_File_Name
893 (Project_Naming_Last + 1 ..
894 Project_Naming_Last + Project_File_Extension'Length) :=
895 Project_File_Extension;
896 Project_Naming_Last :=
897 Project_Naming_Last + Project_File_Extension'Length;
899 -- Create the source list file name
901 Source_List_Last := Output_Name_Last;
902 Source_List_Path (1 .. Source_List_Last) :=
903 Output_Name (1 .. Source_List_Last);
904 Source_List_Path
905 (Source_List_Last + 1 ..
906 Source_List_Last + Source_List_File_Suffix'Length) :=
907 Source_List_File_Suffix;
908 Source_List_Last := Source_List_Last + Source_List_File_Suffix'Length;
910 -- Add the project file extension to the project name
912 Output_Name
913 (Output_Name_Last + 1 ..
914 Output_Name_Last + Project_File_Extension'Length) :=
915 Project_File_Extension;
916 Output_Name_Last := Output_Name_Last + Project_File_Extension'Length;
917 end if;
919 -- Change the current directory to the directory of the project file,
920 -- if any directory information is specified.
922 if Directory_Last /= 0 then
923 begin
924 Change_Dir (Path_Name (1 .. Directory_Last));
925 exception
926 when Directory_Error =>
927 Prj.Com.Fail
928 ("unknown directory """,
929 Path_Name (1 .. Directory_Last),
930 """");
931 end;
932 end if;
934 if Project_File then
936 -- Delete the source list file, if it already exists
938 declare
939 Discard : Boolean;
940 pragma Warnings (Off, Discard);
941 begin
942 Delete_File
943 (Source_List_Path (1 .. Source_List_Last),
944 Success => Discard);
945 end;
947 -- And create a new source list file.
948 -- Fail if file cannot be created.
950 Source_List_FD := Create_New_File
951 (Name => Source_List_Path (1 .. Source_List_Last),
952 Fmode => Text);
954 if Source_List_FD = Invalid_FD then
955 Prj.Com.Fail
956 ("cannot create file """,
957 Source_List_Path (1 .. Source_List_Last),
958 """");
959 end if;
960 end if;
962 -- Compile the regular expressions. Fails immediately if any of
963 -- the specified strings is in error.
965 for Index in Excluded_Expressions'Range loop
966 if Very_Verbose then
967 Output.Write_Str ("Excluded pattern: """);
968 Output.Write_Str (Excluded_Patterns (Index).all);
969 Output.Write_Line ("""");
970 end if;
972 begin
973 Excluded_Expressions (Index) :=
974 Compile (Pattern => Excluded_Patterns (Index).all, Glob => True);
975 exception
976 when Error_In_Regexp =>
977 Prj.Com.Fail
978 ("invalid regular expression """,
979 Excluded_Patterns (Index).all,
980 """");
981 end;
982 end loop;
984 for Index in Foreign_Expressions'Range loop
985 if Very_Verbose then
986 Output.Write_Str ("Foreign pattern: """);
987 Output.Write_Str (Foreign_Patterns (Index).all);
988 Output.Write_Line ("""");
989 end if;
991 begin
992 Foreign_Expressions (Index) :=
993 Compile (Pattern => Foreign_Patterns (Index).all, Glob => True);
994 exception
995 when Error_In_Regexp =>
996 Prj.Com.Fail
997 ("invalid regular expression """,
998 Foreign_Patterns (Index).all,
999 """");
1000 end;
1001 end loop;
1003 for Index in Regular_Expressions'Range loop
1004 if Very_Verbose then
1005 Output.Write_Str ("Pattern: """);
1006 Output.Write_Str (Name_Patterns (Index).all);
1007 Output.Write_Line ("""");
1008 end if;
1010 begin
1011 Regular_Expressions (Index) :=
1012 Compile (Pattern => Name_Patterns (Index).all, Glob => True);
1014 exception
1015 when Error_In_Regexp =>
1016 Prj.Com.Fail
1017 ("invalid regular expression """,
1018 Name_Patterns (Index).all,
1019 """");
1020 end;
1021 end loop;
1023 if Project_File then
1024 if Opt.Verbose_Mode then
1025 Output.Write_Str ("Naming project file name is """);
1026 Output.Write_Str
1027 (Project_Naming_File_Name (1 .. Project_Naming_Last));
1028 Output.Write_Line ("""");
1029 end if;
1031 -- If there were no already existing project file, or if the parsing
1032 -- was unsuccessful, create an empty project node with the correct
1033 -- name and its project declaration node.
1035 if Project_Node = Empty_Node then
1036 Project_Node :=
1037 Default_Project_Node (Of_Kind => N_Project, In_Tree => Tree);
1038 Set_Name_Of (Project_Node, Tree, To => Output_Name_Id);
1039 Set_Project_Declaration_Of
1040 (Project_Node, Tree,
1041 To => Default_Project_Node
1042 (Of_Kind => N_Project_Declaration, In_Tree => Tree));
1044 end if;
1046 -- Create the naming project node, and add an attribute declaration
1047 -- for Source_Files as an empty list, to indicate there are no
1048 -- sources in the naming project.
1050 Project_Naming_Node :=
1051 Default_Project_Node (Of_Kind => N_Project, In_Tree => Tree);
1052 Set_Name_Of (Project_Naming_Node, Tree, To => Project_Naming_Id);
1053 Project_Naming_Decl :=
1054 Default_Project_Node
1055 (Of_Kind => N_Project_Declaration, In_Tree => Tree);
1056 Set_Project_Declaration_Of
1057 (Project_Naming_Node, Tree, Project_Naming_Decl);
1058 Naming_Package :=
1059 Default_Project_Node
1060 (Of_Kind => N_Package_Declaration, In_Tree => Tree);
1061 Set_Name_Of (Naming_Package, Tree, To => Name_Naming);
1063 declare
1064 Decl_Item : constant Project_Node_Id :=
1065 Default_Project_Node
1066 (Of_Kind => N_Declarative_Item, In_Tree => Tree);
1068 Attribute : constant Project_Node_Id :=
1069 Default_Project_Node
1070 (Of_Kind => N_Attribute_Declaration,
1071 In_Tree => Tree,
1072 And_Expr_Kind => List);
1074 Expression : constant Project_Node_Id :=
1075 Default_Project_Node
1076 (Of_Kind => N_Expression,
1077 In_Tree => Tree,
1078 And_Expr_Kind => List);
1080 Term : constant Project_Node_Id :=
1081 Default_Project_Node
1082 (Of_Kind => N_Term,
1083 In_Tree => Tree,
1084 And_Expr_Kind => List);
1086 Empty_List : constant Project_Node_Id :=
1087 Default_Project_Node
1088 (Of_Kind => N_Literal_String_List,
1089 In_Tree => Tree);
1091 begin
1092 Set_First_Declarative_Item_Of
1093 (Project_Naming_Decl, Tree, To => Decl_Item);
1094 Set_Next_Declarative_Item (Decl_Item, Tree, Naming_Package);
1095 Set_Current_Item_Node (Decl_Item, Tree, To => Attribute);
1096 Set_Name_Of (Attribute, Tree, To => Name_Source_Files);
1097 Set_Expression_Of (Attribute, Tree, To => Expression);
1098 Set_First_Term (Expression, Tree, To => Term);
1099 Set_Current_Term (Term, Tree, To => Empty_List);
1100 end;
1102 -- Add a with clause on the naming project in the main project, if
1103 -- there is not already one.
1105 declare
1106 With_Clause : Project_Node_Id :=
1107 First_With_Clause_Of (Project_Node, Tree);
1109 begin
1110 while With_Clause /= Empty_Node loop
1111 exit when
1112 Prj.Tree.Name_Of (With_Clause, Tree) = Project_Naming_Id;
1113 With_Clause := Next_With_Clause_Of (With_Clause, Tree);
1114 end loop;
1116 if With_Clause = Empty_Node then
1117 With_Clause := Default_Project_Node
1118 (Of_Kind => N_With_Clause, In_Tree => Tree);
1119 Set_Next_With_Clause_Of
1120 (With_Clause, Tree,
1121 To => First_With_Clause_Of (Project_Node, Tree));
1122 Set_First_With_Clause_Of
1123 (Project_Node, Tree, To => With_Clause);
1124 Set_Name_Of (With_Clause, Tree, To => Project_Naming_Id);
1126 -- We set the project node to something different than
1127 -- Empty_Node, so that Prj.PP does not generate a limited
1128 -- with clause.
1130 Set_Project_Node_Of (With_Clause, Tree, Non_Empty_Node);
1132 Name_Len := Project_Naming_Last;
1133 Name_Buffer (1 .. Name_Len) :=
1134 Project_Naming_File_Name (1 .. Project_Naming_Last);
1135 Set_String_Value_Of (With_Clause, Tree, To => Name_Find);
1136 end if;
1137 end;
1139 Project_Declaration := Project_Declaration_Of (Project_Node, Tree);
1141 -- Add a renaming declaration for package Naming in the main project
1143 declare
1144 Decl_Item : constant Project_Node_Id :=
1145 Default_Project_Node
1146 (Of_Kind => N_Declarative_Item,
1147 In_Tree => Tree);
1149 Naming : constant Project_Node_Id :=
1150 Default_Project_Node
1151 (Of_Kind => N_Package_Declaration,
1152 In_Tree => Tree);
1154 begin
1155 Set_Next_Declarative_Item
1156 (Decl_Item, Tree,
1157 To => First_Declarative_Item_Of (Project_Declaration, Tree));
1158 Set_First_Declarative_Item_Of
1159 (Project_Declaration, Tree, To => Decl_Item);
1160 Set_Current_Item_Node (Decl_Item, Tree, To => Naming);
1161 Set_Name_Of (Naming, Tree, To => Name_Naming);
1162 Set_Project_Of_Renamed_Package_Of
1163 (Naming, Tree, To => Project_Naming_Node);
1165 -- Attach the comments, if any, that were saved for package
1166 -- Naming.
1168 Tree.Project_Nodes.Table (Naming).Comments :=
1169 Naming_Package_Comments;
1170 end;
1172 -- Add an attribute declaration for Source_Dirs, initialized as an
1173 -- empty list. Directories will be added as they are read from the
1174 -- directory list file.
1176 declare
1177 Decl_Item : constant Project_Node_Id :=
1178 Default_Project_Node
1179 (Of_Kind => N_Declarative_Item,
1180 In_Tree => Tree);
1182 Attribute : constant Project_Node_Id :=
1183 Default_Project_Node
1184 (Of_Kind => N_Attribute_Declaration,
1185 In_Tree => Tree,
1186 And_Expr_Kind => List);
1188 Expression : constant Project_Node_Id :=
1189 Default_Project_Node
1190 (Of_Kind => N_Expression,
1191 In_Tree => Tree,
1192 And_Expr_Kind => List);
1194 Term : constant Project_Node_Id :=
1195 Default_Project_Node
1196 (Of_Kind => N_Term, In_Tree => Tree,
1197 And_Expr_Kind => List);
1199 begin
1200 Set_Next_Declarative_Item
1201 (Decl_Item, Tree,
1202 To => First_Declarative_Item_Of (Project_Declaration, Tree));
1203 Set_First_Declarative_Item_Of
1204 (Project_Declaration, Tree, To => Decl_Item);
1205 Set_Current_Item_Node (Decl_Item, Tree, To => Attribute);
1206 Set_Name_Of (Attribute, Tree, To => Name_Source_Dirs);
1207 Set_Expression_Of (Attribute, Tree, To => Expression);
1208 Set_First_Term (Expression, Tree, To => Term);
1209 Source_Dirs_List :=
1210 Default_Project_Node
1211 (Of_Kind => N_Literal_String_List,
1212 In_Tree => Tree,
1213 And_Expr_Kind => List);
1214 Set_Current_Term (Term, Tree, To => Source_Dirs_List);
1216 -- Attach the comments, if any, that were saved for attribute
1217 -- Source_Dirs.
1219 Tree.Project_Nodes.Table (Attribute).Comments :=
1220 Source_Dirs_Comments;
1221 end;
1223 -- Add an attribute declaration for Source_List_File with the
1224 -- source list file name that will be created.
1226 declare
1227 Decl_Item : constant Project_Node_Id :=
1228 Default_Project_Node
1229 (Of_Kind => N_Declarative_Item,
1230 In_Tree => Tree);
1232 Attribute : constant Project_Node_Id :=
1233 Default_Project_Node
1234 (Of_Kind => N_Attribute_Declaration,
1235 In_Tree => Tree,
1236 And_Expr_Kind => Single);
1238 Expression : constant Project_Node_Id :=
1239 Default_Project_Node
1240 (Of_Kind => N_Expression,
1241 In_Tree => Tree,
1242 And_Expr_Kind => Single);
1244 Term : constant Project_Node_Id :=
1245 Default_Project_Node
1246 (Of_Kind => N_Term,
1247 In_Tree => Tree,
1248 And_Expr_Kind => Single);
1250 Value : constant Project_Node_Id :=
1251 Default_Project_Node
1252 (Of_Kind => N_Literal_String,
1253 In_Tree => Tree,
1254 And_Expr_Kind => Single);
1256 begin
1257 Set_Next_Declarative_Item
1258 (Decl_Item, Tree,
1259 To => First_Declarative_Item_Of (Project_Declaration, Tree));
1260 Set_First_Declarative_Item_Of
1261 (Project_Declaration, Tree, To => Decl_Item);
1262 Set_Current_Item_Node (Decl_Item, Tree, To => Attribute);
1263 Set_Name_Of (Attribute, Tree, To => Name_Source_List_File);
1264 Set_Expression_Of (Attribute, Tree, To => Expression);
1265 Set_First_Term (Expression, Tree, To => Term);
1266 Set_Current_Term (Term, Tree, To => Value);
1267 Name_Len := Source_List_Last;
1268 Name_Buffer (1 .. Name_Len) :=
1269 Source_List_Path (1 .. Source_List_Last);
1270 Set_String_Value_Of (Value, Tree, To => Name_Find);
1272 -- If there was no comments for attribute Source_List_File, put
1273 -- those for Source_Files, if they exist.
1275 if Source_List_File_Comments /= Empty_Node then
1276 Tree.Project_Nodes.Table (Attribute).Comments :=
1277 Source_List_File_Comments;
1278 else
1279 Tree.Project_Nodes.Table (Attribute).Comments :=
1280 Source_Files_Comments;
1281 end if;
1282 end;
1283 end if;
1285 -- Process each directory
1287 for Index in Directories'Range loop
1289 declare
1290 Dir_Name : constant String := Directories (Index).all;
1291 Last : Natural := Dir_Name'Last;
1292 Recursively : Boolean := False;
1294 begin
1295 if Dir_Name'Length >= 4
1296 and then (Dir_Name (Last - 2 .. Last) = "/**")
1297 then
1298 Last := Last - 3;
1299 Recursively := True;
1300 end if;
1302 if Project_File then
1304 -- Add the directory in the list for attribute Source_Dirs
1306 declare
1307 Expression : constant Project_Node_Id :=
1308 Default_Project_Node
1309 (Of_Kind => N_Expression,
1310 In_Tree => Tree,
1311 And_Expr_Kind => Single);
1313 Term : constant Project_Node_Id :=
1314 Default_Project_Node
1315 (Of_Kind => N_Term,
1316 In_Tree => Tree,
1317 And_Expr_Kind => Single);
1319 Value : constant Project_Node_Id :=
1320 Default_Project_Node
1321 (Of_Kind => N_Literal_String,
1322 In_Tree => Tree,
1323 And_Expr_Kind => Single);
1325 begin
1326 if Current_Source_Dir = Empty_Node then
1327 Set_First_Expression_In_List
1328 (Source_Dirs_List, Tree, To => Expression);
1329 else
1330 Set_Next_Expression_In_List
1331 (Current_Source_Dir, Tree, To => Expression);
1332 end if;
1334 Current_Source_Dir := Expression;
1335 Set_First_Term (Expression, Tree, To => Term);
1336 Set_Current_Term (Term, Tree, To => Value);
1337 Name_Len := Dir_Name'Length;
1338 Name_Buffer (1 .. Name_Len) := Dir_Name;
1339 Set_String_Value_Of (Value, Tree, To => Name_Find);
1340 end;
1341 end if;
1343 Process_Directory (Dir_Name (Dir_Name'First .. Last), Recursively);
1344 end;
1346 end loop;
1348 if Project_File then
1349 Close (Source_List_FD);
1350 end if;
1352 declare
1353 Discard : Boolean;
1354 pragma Warnings (Off, Discard);
1356 begin
1357 -- Delete the file if it already exists
1359 Delete_File
1360 (Path_Name (Directory_Last + 1 .. Path_Last),
1361 Success => Discard);
1363 -- Create a new one
1365 if Opt.Verbose_Mode then
1366 Output.Write_Str ("Creating new file """);
1367 Output.Write_Str (Path_Name (Directory_Last + 1 .. Path_Last));
1368 Output.Write_Line ("""");
1369 end if;
1371 Output_FD := Create_New_File
1372 (Path_Name (Directory_Last + 1 .. Path_Last),
1373 Fmode => Text);
1375 -- Fails if project file cannot be created
1377 if Output_FD = Invalid_FD then
1378 Prj.Com.Fail
1379 ("cannot create new """, Path_Name (1 .. Path_Last), """");
1380 end if;
1382 if Project_File then
1384 -- Output the project file
1386 Prj.PP.Pretty_Print
1387 (Project_Node, Tree,
1388 W_Char => Write_A_Char'Access,
1389 W_Eol => Write_Eol'Access,
1390 W_Str => Write_A_String'Access,
1391 Backward_Compatibility => False);
1392 Close (Output_FD);
1394 -- Delete the naming project file if it already exists
1396 Delete_File
1397 (Project_Naming_File_Name (1 .. Project_Naming_Last),
1398 Success => Discard);
1400 -- Create a new one
1402 if Opt.Verbose_Mode then
1403 Output.Write_Str ("Creating new naming project file """);
1404 Output.Write_Str (Project_Naming_File_Name
1405 (1 .. Project_Naming_Last));
1406 Output.Write_Line ("""");
1407 end if;
1409 Output_FD := Create_New_File
1410 (Project_Naming_File_Name (1 .. Project_Naming_Last),
1411 Fmode => Text);
1413 -- Fails if naming project file cannot be created
1415 if Output_FD = Invalid_FD then
1416 Prj.Com.Fail
1417 ("cannot create new """,
1418 Project_Naming_File_Name (1 .. Project_Naming_Last),
1419 """");
1420 end if;
1422 -- Output the naming project file
1424 Prj.PP.Pretty_Print
1425 (Project_Naming_Node, Tree,
1426 W_Char => Write_A_Char'Access,
1427 W_Eol => Write_Eol'Access,
1428 W_Str => Write_A_String'Access,
1429 Backward_Compatibility => False);
1430 Close (Output_FD);
1432 else
1433 -- Write to the output file each entry in the SFN_Pragmas table
1434 -- as an pragma Source_File_Name.
1436 for Index in 1 .. SFN_Pragmas.Last loop
1437 Write_A_String ("pragma Source_File_Name");
1438 Write_Eol;
1439 Write_A_String (" (");
1440 Write_A_String
1441 (Get_Name_String (SFN_Pragmas.Table (Index).Unit));
1442 Write_A_String (",");
1443 Write_Eol;
1445 if SFN_Pragmas.Table (Index).Spec then
1446 Write_A_String (" Spec_File_Name => """);
1448 else
1449 Write_A_String (" Body_File_Name => """);
1450 end if;
1452 Write_A_String
1453 (Get_Name_String (SFN_Pragmas.Table (Index).File));
1455 Write_A_String ("""");
1457 if SFN_Pragmas.Table (Index).Index /= 0 then
1458 Write_A_String (", Index =>");
1459 Write_A_String (SFN_Pragmas.Table (Index).Index'Img);
1460 end if;
1462 Write_A_String (");");
1463 Write_Eol;
1464 end loop;
1466 Close (Output_FD);
1467 end if;
1468 end;
1470 end Make;
1472 ----------------
1473 -- Write_Char --
1474 ----------------
1475 procedure Write_A_Char (C : Character) is
1476 begin
1477 Write_A_String ((1 => C));
1478 end Write_A_Char;
1480 ---------------
1481 -- Write_Eol --
1482 ---------------
1484 procedure Write_Eol is
1485 begin
1486 Write_A_String ((1 => ASCII.LF));
1487 end Write_Eol;
1489 --------------------
1490 -- Write_A_String --
1491 --------------------
1493 procedure Write_A_String (S : String) is
1494 Str : String (1 .. S'Length);
1496 begin
1497 if S'Length > 0 then
1498 Str := S;
1500 if Write (Output_FD, Str (1)'Address, Str'Length) /= Str'Length then
1501 Prj.Com.Fail ("disk full");
1502 end if;
1503 end if;
1504 end Write_A_String;
1506 end Prj.Makr;