1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2009, 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 ------------------------------------------------------------------------------
28 with Osint; use Osint;
29 with Output; use Output;
30 with Prj.Com; use Prj.Com;
33 package body Prj.Env is
35 Default_Naming : constant Naming_Id := Naming_Table.First;
37 -----------------------
38 -- Local Subprograms --
39 -----------------------
42 (Source_Dirs : String_List_Id;
43 In_Tree : Project_Tree_Ref);
44 -- Add to Ada_Path_Buffer all the source directories in string list
45 -- Source_Dirs, if any. Increment Ada_Path_Length.
47 procedure Add_To_Path (Dir : String; In_Tree : Project_Tree_Ref);
48 -- If Dir is not already in the global variable Ada_Path_Buffer, add it.
49 -- Increment Ada_Path_Length.
50 -- If Ada_Path_Length /= 0, prepend a Path_Separator character to
53 procedure Add_To_Source_Path
54 (Source_Dirs : String_List_Id; In_Tree : Project_Tree_Ref);
55 -- Add to Ada_Path_B all the source directories in string list
56 -- Source_Dirs, if any. Increment Ada_Path_Length.
58 procedure Add_To_Object_Path
59 (Object_Dir : Path_Name_Type;
60 In_Tree : Project_Tree_Ref);
61 -- Add Object_Dir to object path table. Make sure it is not duplicate
62 -- and it is the last one in the current table.
64 procedure Set_Path_File_Var (Name : String; Value : String);
65 -- Call Setenv, after calling To_Host_File_Spec
67 function Ultimate_Extension_Of
68 (Project : Project_Id) return Project_Id;
69 -- Return a project that is either Project or an extended ancestor of
70 -- Project that itself is not extended.
72 ----------------------
73 -- Ada_Include_Path --
74 ----------------------
76 function Ada_Include_Path
77 (Project : Project_Id;
78 In_Tree : Project_Tree_Ref) return String_Access
80 procedure Add (Project : Project_Id; Dummy : in out Boolean);
81 -- Add source dirs of Project to the path
87 procedure Add (Project : Project_Id; Dummy : in out Boolean) is
88 pragma Unreferenced (Dummy);
90 Add_To_Path (Project.Source_Dirs, In_Tree);
93 procedure For_All_Projects is
94 new For_Every_Project_Imported (Boolean, Add);
95 Dummy : Boolean := False;
97 -- Start of processing for Ada_Include_Path
100 -- If it is the first time we call this function for
101 -- this project, compute the source path
103 if Project.Ada_Include_Path = null then
104 In_Tree.Private_Part.Ada_Path_Length := 0;
105 For_All_Projects (Project, Dummy);
107 Project.Ada_Include_Path :=
109 (In_Tree.Private_Part.Ada_Path_Buffer
110 (1 .. In_Tree.Private_Part.Ada_Path_Length));
113 return Project.Ada_Include_Path;
114 end Ada_Include_Path;
116 ----------------------
117 -- Ada_Include_Path --
118 ----------------------
120 function Ada_Include_Path
121 (Project : Project_Id;
122 In_Tree : Project_Tree_Ref;
123 Recursive : Boolean) return String
127 return Ada_Include_Path (Project, In_Tree).all;
129 In_Tree.Private_Part.Ada_Path_Length := 0;
130 Add_To_Path (Project.Source_Dirs, In_Tree);
132 In_Tree.Private_Part.Ada_Path_Buffer
133 (1 .. In_Tree.Private_Part.Ada_Path_Length);
135 end Ada_Include_Path;
137 ----------------------
138 -- Ada_Objects_Path --
139 ----------------------
141 function Ada_Objects_Path
142 (Project : Project_Id;
143 In_Tree : Project_Tree_Ref;
144 Including_Libraries : Boolean := True) return String_Access
146 procedure Add (Project : Project_Id; Dummy : in out Boolean);
147 -- Add all the object directories of a project to the path
153 procedure Add (Project : Project_Id; Dummy : in out Boolean) is
154 pragma Unreferenced (Dummy);
155 Path : constant Path_Name_Type :=
158 Including_Libraries => Including_Libraries,
159 Only_If_Ada => False);
161 if Path /= No_Path then
162 Add_To_Path (Get_Name_String (Path), In_Tree);
166 procedure For_All_Projects is
167 new For_Every_Project_Imported (Boolean, Add);
168 Dummy : Boolean := False;
170 -- Start of processing for Ada_Objects_Path
173 -- If it is the first time we call this function for
174 -- this project, compute the objects path
176 if Project.Ada_Objects_Path = null then
177 In_Tree.Private_Part.Ada_Path_Length := 0;
178 For_All_Projects (Project, Dummy);
180 Project.Ada_Objects_Path :=
182 (In_Tree.Private_Part.Ada_Path_Buffer
183 (1 .. In_Tree.Private_Part.Ada_Path_Length));
186 return Project.Ada_Objects_Path;
187 end Ada_Objects_Path;
189 ------------------------
190 -- Add_To_Object_Path --
191 ------------------------
193 procedure Add_To_Object_Path
194 (Object_Dir : Path_Name_Type; In_Tree : Project_Tree_Ref)
197 -- Check if the directory is already in the table
199 for Index in Object_Path_Table.First ..
200 Object_Path_Table.Last (In_Tree.Private_Part.Object_Paths)
203 -- If it is, remove it, and add it as the last one
205 if In_Tree.Private_Part.Object_Paths.Table (Index) = Object_Dir then
206 for Index2 in Index + 1 ..
207 Object_Path_Table.Last
208 (In_Tree.Private_Part.Object_Paths)
210 In_Tree.Private_Part.Object_Paths.Table (Index2 - 1) :=
211 In_Tree.Private_Part.Object_Paths.Table (Index2);
214 In_Tree.Private_Part.Object_Paths.Table
215 (Object_Path_Table.Last (In_Tree.Private_Part.Object_Paths)) :=
221 -- The directory is not already in the table, add it
223 Object_Path_Table.Increment_Last (In_Tree.Private_Part.Object_Paths);
224 In_Tree.Private_Part.Object_Paths.Table
225 (Object_Path_Table.Last (In_Tree.Private_Part.Object_Paths)) :=
227 end Add_To_Object_Path;
233 procedure Add_To_Path
234 (Source_Dirs : String_List_Id;
235 In_Tree : Project_Tree_Ref)
237 Current : String_List_Id := Source_Dirs;
238 Source_Dir : String_Element;
240 while Current /= Nil_String loop
241 Source_Dir := In_Tree.String_Elements.Table (Current);
242 Add_To_Path (Get_Name_String (Source_Dir.Display_Value), In_Tree);
243 Current := Source_Dir.Next;
247 procedure Add_To_Path (Dir : String; In_Tree : Project_Tree_Ref) is
249 New_Buffer : String_Access;
252 function Is_Present (Path : String; Dir : String) return Boolean;
253 -- Return True if Dir is part of Path
259 function Is_Present (Path : String; Dir : String) return Boolean is
260 Last : constant Integer := Path'Last - Dir'Length + 1;
263 for J in Path'First .. Last loop
265 -- Note: the order of the conditions below is important, since
266 -- it ensures a minimal number of string comparisons.
269 or else Path (J - 1) = Path_Separator)
271 (J + Dir'Length > Path'Last
272 or else Path (J + Dir'Length) = Path_Separator)
273 and then Dir = Path (J .. J + Dir'Length - 1)
282 -- Start of processing for Add_To_Path
285 if Is_Present (In_Tree.Private_Part.Ada_Path_Buffer
286 (1 .. In_Tree.Private_Part.Ada_Path_Length),
290 -- Dir is already in the path, nothing to do
295 Min_Len := In_Tree.Private_Part.Ada_Path_Length + Dir'Length;
297 if In_Tree.Private_Part.Ada_Path_Length > 0 then
299 -- Add 1 for the Path_Separator character
301 Min_Len := Min_Len + 1;
304 -- If Ada_Path_Buffer is too small, increase it
306 Len := In_Tree.Private_Part.Ada_Path_Buffer'Last;
308 if Len < Min_Len then
311 exit when Len >= Min_Len;
314 New_Buffer := new String (1 .. Len);
315 New_Buffer (1 .. In_Tree.Private_Part.Ada_Path_Length) :=
316 In_Tree.Private_Part.Ada_Path_Buffer
317 (1 .. In_Tree.Private_Part.Ada_Path_Length);
318 Free (In_Tree.Private_Part.Ada_Path_Buffer);
319 In_Tree.Private_Part.Ada_Path_Buffer := New_Buffer;
322 if In_Tree.Private_Part.Ada_Path_Length > 0 then
323 In_Tree.Private_Part.Ada_Path_Length :=
324 In_Tree.Private_Part.Ada_Path_Length + 1;
325 In_Tree.Private_Part.Ada_Path_Buffer
326 (In_Tree.Private_Part.Ada_Path_Length) := Path_Separator;
329 In_Tree.Private_Part.Ada_Path_Buffer
330 (In_Tree.Private_Part.Ada_Path_Length + 1 ..
331 In_Tree.Private_Part.Ada_Path_Length + Dir'Length) := Dir;
332 In_Tree.Private_Part.Ada_Path_Length :=
333 In_Tree.Private_Part.Ada_Path_Length + Dir'Length;
336 ------------------------
337 -- Add_To_Source_Path --
338 ------------------------
340 procedure Add_To_Source_Path
341 (Source_Dirs : String_List_Id; In_Tree : Project_Tree_Ref)
343 Current : String_List_Id := Source_Dirs;
344 Source_Dir : String_Element;
348 -- Add each source directory
350 while Current /= Nil_String loop
351 Source_Dir := In_Tree.String_Elements.Table (Current);
354 -- Check if the source directory is already in the table
356 for Index in Source_Path_Table.First ..
357 Source_Path_Table.Last
358 (In_Tree.Private_Part.Source_Paths)
360 -- If it is already, no need to add it
362 if In_Tree.Private_Part.Source_Paths.Table (Index) =
371 Source_Path_Table.Increment_Last
372 (In_Tree.Private_Part.Source_Paths);
373 In_Tree.Private_Part.Source_Paths.Table
374 (Source_Path_Table.Last (In_Tree.Private_Part.Source_Paths)) :=
378 -- Next source directory
380 Current := Source_Dir.Next;
382 end Add_To_Source_Path;
384 --------------------------------
385 -- Create_Config_Pragmas_File --
386 --------------------------------
388 procedure Create_Config_Pragmas_File
389 (For_Project : Project_Id;
390 Main_Project : Project_Id;
391 In_Tree : Project_Tree_Ref;
392 Include_Config_Files : Boolean := True)
394 pragma Unreferenced (Main_Project);
395 pragma Unreferenced (Include_Config_Files);
397 File_Name : Path_Name_Type := No_Path;
398 File : File_Descriptor := Invalid_FD;
400 Current_Unit : Unit_Index := Unit_Table.First;
402 First_Project : Project_List;
404 Current_Project : Project_List;
405 Current_Naming : Naming_Id;
410 procedure Check (Project : Project_Id);
411 -- Recursive procedure that put in the config pragmas file any non
412 -- standard naming schemes, if it is not already in the file, then call
413 -- itself for any imported project.
415 procedure Check_Temp_File;
416 -- Check that a temporary file has been opened.
417 -- If not, create one, and put its name in the project data,
418 -- with the indication that it is a temporary file.
421 (Unit_Name : Name_Id;
422 File_Name : File_Name_Type;
423 Unit_Kind : Spec_Or_Body;
425 -- Put an SFN pragma in the temporary file
427 procedure Put (File : File_Descriptor; S : String);
428 procedure Put_Line (File : File_Descriptor; S : String);
429 -- Output procedures, analogous to normal Text_IO procs of same name
435 procedure Check (Project : Project_Id) is
437 if Current_Verbosity = High then
438 Write_Str ("Checking project file """);
439 Write_Str (Namet.Get_Name_String (Project.Name));
444 -- Is this project in the list of the visited project?
446 Current_Project := First_Project;
447 while Current_Project /= null
448 and then Current_Project.Project /= Project
450 Current_Project := Current_Project.Next;
453 -- If it is not, put it in the list, and visit it
455 if Current_Project = null then
456 First_Project := new Project_List_Element'
458 Next => First_Project);
460 -- Is the naming scheme of this project one that we know?
462 Current_Naming := Default_Naming;
463 while Current_Naming <=
464 Naming_Table.Last (In_Tree.Private_Part.Namings)
465 and then not Same_Naming_Scheme
466 (Left => In_Tree.Private_Part.Namings.Table (Current_Naming),
467 Right => Project.Naming) loop
468 Current_Naming := Current_Naming + 1;
471 -- If we don't know it, add it
474 Naming_Table.Last (In_Tree.Private_Part.Namings)
476 Naming_Table.Increment_Last (In_Tree.Private_Part.Namings);
477 In_Tree.Private_Part.Namings.Table
478 (Naming_Table.Last (In_Tree.Private_Part.Namings)) :=
481 -- We need a temporary file to be created
485 -- Put the SFN pragmas for the naming scheme
490 (File, "pragma Source_File_Name_Project");
492 (File, " (Spec_File_Name => ""*" &
493 Spec_Suffix_Of (In_Tree, "ada", Project.Naming) &
496 (File, " Casing => " &
497 Image (Project.Naming.Casing) & ",");
499 (File, " Dot_Replacement => """ &
500 Namet.Get_Name_String (Project.Naming.Dot_Replacement) &
506 (File, "pragma Source_File_Name_Project");
508 (File, " (Body_File_Name => ""*" &
509 Body_Suffix_Of (In_Tree, "ada", Project.Naming) &
512 (File, " Casing => " &
513 Image (Project.Naming.Casing) & ",");
515 (File, " Dot_Replacement => """ &
516 Namet.Get_Name_String (Project.Naming.Dot_Replacement) &
519 -- and maybe separate
521 if Body_Suffix_Of (In_Tree, "ada", Project.Naming) /=
522 Get_Name_String (Project.Naming.Separate_Suffix)
525 (File, "pragma Source_File_Name_Project");
527 (File, " (Subunit_File_Name => ""*" &
528 Namet.Get_Name_String (Project.Naming.Separate_Suffix) &
531 (File, " Casing => " &
532 Image (Project.Naming.Casing) &
535 (File, " Dot_Replacement => """ &
536 Namet.Get_Name_String (Project.Naming.Dot_Replacement) &
541 if Project.Extends /= No_Project then
542 Check (Project.Extends);
546 Current : Project_List := Project.Imported_Projects;
548 while Current /= null loop
549 Check (Current.Project);
550 Current := Current.Next;
556 ---------------------
557 -- Check_Temp_File --
558 ---------------------
560 procedure Check_Temp_File is
562 if File = Invalid_FD then
563 Tempdir.Create_Temp_File (File, Name => File_Name);
565 if File = Invalid_FD then
567 ("unable to create temporary configuration pragmas file");
570 Record_Temp_File (File_Name);
572 if Opt.Verbose_Mode then
573 Write_Str ("Creating temp file """);
574 Write_Str (Get_Name_String (File_Name));
586 (Unit_Name : Name_Id;
587 File_Name : File_Name_Type;
588 Unit_Kind : Spec_Or_Body;
592 -- A temporary file needs to be open
596 -- Put the pragma SFN for the unit kind (spec or body)
598 Put (File, "pragma Source_File_Name_Project (");
599 Put (File, Namet.Get_Name_String (Unit_Name));
601 if Unit_Kind = Specification then
602 Put (File, ", Spec_File_Name => """);
604 Put (File, ", Body_File_Name => """);
607 Put (File, Namet.Get_Name_String (File_Name));
611 Put (File, ", Index =>");
612 Put (File, Index'Img);
615 Put_Line (File, ");");
618 procedure Put (File : File_Descriptor; S : String) is
622 Last := Write (File, S (S'First)'Address, S'Length);
624 if Last /= S'Length then
625 Prj.Com.Fail ("Disk full");
628 if Current_Verbosity = High then
637 procedure Put_Line (File : File_Descriptor; S : String) is
638 S0 : String (1 .. S'Length + 1);
642 -- Add an ASCII.LF to the string. As this config file is supposed to
643 -- be used only by the compiler, we don't care about the characters
644 -- for the end of line. In fact we could have put a space, but
645 -- it is more convenient to be able to read gnat.adc during
646 -- development, for which the ASCII.LF is fine.
648 S0 (1 .. S'Length) := S;
649 S0 (S0'Last) := ASCII.LF;
650 Last := Write (File, S0'Address, S0'Length);
652 if Last /= S'Length + 1 then
653 Prj.Com.Fail ("Disk full");
656 if Current_Verbosity = High then
661 -- Start of processing for Create_Config_Pragmas_File
664 if not For_Project.Config_Checked then
666 -- Remove any memory of processed naming schemes, if any
668 Naming_Table.Set_Last (In_Tree.Private_Part.Namings, Default_Naming);
670 -- Check the naming schemes
674 -- Visit all the units and process those that need an SFN pragma
677 Current_Unit <= Unit_Table.Last (In_Tree.Units)
680 Unit : constant Unit_Data :=
681 In_Tree.Units.Table (Current_Unit);
684 if Unit.File_Names (Specification).Needs_Pragma then
686 Unit.File_Names (Specification).Name,
688 Unit.File_Names (Specification).Index);
691 if Unit.File_Names (Body_Part).Needs_Pragma then
693 Unit.File_Names (Body_Part).Name,
695 Unit.File_Names (Body_Part).Index);
698 Current_Unit := Current_Unit + 1;
702 -- If there are no non standard naming scheme, issue the GNAT
703 -- standard naming scheme. This will tell the compiler that
704 -- a project file is used and will forbid any pragma SFN.
706 if File = Invalid_FD then
709 Put_Line (File, "pragma Source_File_Name_Project");
710 Put_Line (File, " (Spec_File_Name => ""*.ads"",");
711 Put_Line (File, " Dot_Replacement => ""-"",");
712 Put_Line (File, " Casing => lowercase);");
714 Put_Line (File, "pragma Source_File_Name_Project");
715 Put_Line (File, " (Body_File_Name => ""*.adb"",");
716 Put_Line (File, " Dot_Replacement => ""-"",");
717 Put_Line (File, " Casing => lowercase);");
720 -- Close the temporary file
722 GNAT.OS_Lib.Close (File, Status);
725 Prj.Com.Fail ("disk full");
728 if Opt.Verbose_Mode then
729 Write_Str ("Closing configuration file """);
730 Write_Str (Get_Name_String (File_Name));
734 For_Project.Config_File_Name := File_Name;
735 For_Project.Config_File_Temp := True;
736 For_Project.Config_Checked := True;
738 end Create_Config_Pragmas_File;
744 procedure Create_Mapping (In_Tree : Project_Tree_Ref) is
745 The_Unit_Data : Unit_Data;
746 Data : File_Name_Data;
751 for Unit in 1 .. Unit_Table.Last (In_Tree.Units) loop
752 The_Unit_Data := In_Tree.Units.Table (Unit);
754 -- Process only if the unit has a valid name
756 if The_Unit_Data.Name /= No_Name then
757 Data := The_Unit_Data.File_Names (Specification);
759 -- If there is a spec, put it in the mapping
761 if Data.Name /= No_File then
762 if Data.Path.Name = Slash then
763 Fmap.Add_Forbidden_File_Name (Data.Name);
766 (Unit_Name => Unit_Name_Type (The_Unit_Data.Name),
767 File_Name => Data.Name,
768 Path_Name => File_Name_Type (Data.Path.Name));
772 Data := The_Unit_Data.File_Names (Body_Part);
774 -- If there is a body (or subunit) put it in the mapping
776 if Data.Name /= No_File then
777 if Data.Path.Name = Slash then
778 Fmap.Add_Forbidden_File_Name (Data.Name);
781 (Unit_Name => Unit_Name_Type (The_Unit_Data.Name),
782 File_Name => Data.Name,
783 Path_Name => File_Name_Type (Data.Path.Name));
790 -------------------------
791 -- Create_Mapping_File --
792 -------------------------
794 procedure Create_Mapping_File
795 (Project : Project_Id;
796 Language : Name_Id := No_Name;
797 In_Tree : Project_Tree_Ref;
798 Name : out Path_Name_Type)
800 File : File_Descriptor := Invalid_FD;
803 Present : Project_Boolean_Htable.Instance;
804 -- For each project in the closure of Project, the corresponding flag
805 -- will be set to True.
808 Suffix : File_Name_Type;
809 The_Unit_Data : Unit_Data;
810 Data : File_Name_Data;
811 Iter : Source_Iterator;
813 procedure Put_Name_Buffer;
814 -- Put the line contained in the Name_Buffer in the mapping file
816 procedure Put_Data (Spec : Boolean);
817 -- Put the mapping of the spec or body contained in Data in the file
820 procedure Recursive_Flag (Prj : Project_Id);
821 -- Set the flags corresponding to Prj, the projects it imports
822 -- (directly or indirectly) or extends to True. Call itself recursively.
828 procedure Put_Name_Buffer is
832 Name_Len := Name_Len + 1;
833 Name_Buffer (Name_Len) := ASCII.LF;
834 Last := Write (File, Name_Buffer (1)'Address, Name_Len);
836 if Last /= Name_Len then
837 Prj.Com.Fail ("Disk full, cannot write mapping file");
845 procedure Put_Data (Spec : Boolean) is
847 -- Line with the unit name
849 Get_Name_String (The_Unit_Data.Name);
850 Name_Len := Name_Len + 1;
851 Name_Buffer (Name_Len) := '%';
852 Name_Len := Name_Len + 1;
855 Name_Buffer (Name_Len) := 's';
857 Name_Buffer (Name_Len) := 'b';
862 -- Line with the file name
864 Get_Name_String (Data.Name);
867 -- Line with the path name
869 Get_Name_String (Data.Path.Name);
877 procedure Recursive_Flag (Prj : Project_Id) is
878 Imported : Project_List;
881 -- Nothing to do for non existent project or project that has already
885 and then not Project_Boolean_Htable.Get (Present, Prj)
887 Project_Boolean_Htable.Set (Present, Prj, True);
889 Imported := Prj.Imported_Projects;
890 while Imported /= null loop
891 Recursive_Flag (Imported.Project);
892 Imported := Imported.Next;
895 Recursive_Flag (Prj.Extends);
899 -- Start of processing for Create_Mapping_File
902 -- Flag the necessary projects
904 Recursive_Flag (Project);
906 -- Create the temporary file
908 Tempdir.Create_Temp_File (File, Name => Name);
910 if File = Invalid_FD then
911 Prj.Com.Fail ("unable to create temporary mapping file");
914 Record_Temp_File (Name);
916 if Opt.Verbose_Mode then
917 Write_Str ("Creating temp mapping file """);
918 Write_Str (Get_Name_String (Name));
923 if Language = No_Name then
924 if In_Tree.Private_Part.Fill_Mapping_File then
925 for Unit in 1 .. Unit_Table.Last (In_Tree.Units) loop
926 The_Unit_Data := In_Tree.Units.Table (Unit);
928 -- Case of unit has a valid name
930 if The_Unit_Data.Name /= No_Name then
931 Data := The_Unit_Data.File_Names (Specification);
933 -- If there is a spec, put it mapping in the file if it is
934 -- from a project in the closure of Project.
936 if Data.Name /= No_File
937 and then Project_Boolean_Htable.Get (Present, Data.Project)
939 Put_Data (Spec => True);
942 Data := The_Unit_Data.File_Names (Body_Part);
944 -- If there is a body (or subunit) put its mapping in the
945 -- file if it is from a project in the closure of Project.
947 if Data.Name /= No_File
948 and then Project_Boolean_Htable.Get (Present, Data.Project)
950 Put_Data (Spec => False);
956 -- If language is defined
958 -- For all source of the Language of all projects in the closure
963 P := In_Tree.Projects;
965 if Project_Boolean_Htable.Get (Present, P.Project) then
967 Iter := For_Each_Source (In_Tree, P.Project);
969 Source := Prj.Element (Iter);
970 exit when Source = No_Source;
972 if Source.Language.Name = Language
973 and then not Source.Locally_Removed
974 and then Source.Replaced_By = No_Source
975 and then Source.Path.Name /= No_Path
977 if Source.Unit /= No_Name then
978 Get_Name_String (Source.Unit);
980 if Source.Kind = Spec then
982 Source.Language.Config.Mapping_Spec_Suffix;
985 Source.Language.Config.Mapping_Body_Suffix;
988 if Suffix /= No_File then
989 Add_Str_To_Name_Buffer
990 (Get_Name_String (Suffix));
996 Get_Name_String (Source.File);
999 Get_Name_String (Source.Path.Name);
1012 GNAT.OS_Lib.Close (File, Status);
1016 -- We were able to create the temporary file, so there is no problem
1017 -- of protection. However, we are not able to close it, so there must
1018 -- be a capacity problem that we express using "disk full".
1020 Prj.Com.Fail ("disk full, could not write mapping file");
1023 Project_Boolean_Htable.Reset (Present);
1024 end Create_Mapping_File;
1026 --------------------------
1027 -- Create_New_Path_File --
1028 --------------------------
1030 procedure Create_New_Path_File
1031 (In_Tree : Project_Tree_Ref;
1032 Path_FD : out File_Descriptor;
1033 Path_Name : out Path_Name_Type)
1036 Tempdir.Create_Temp_File (Path_FD, Path_Name);
1038 if Path_Name /= No_Path then
1039 Record_Temp_File (Path_Name);
1041 -- Record the name, so that the temp path file will be deleted at the
1042 -- end of the program.
1044 Path_File_Table.Increment_Last (In_Tree.Private_Part.Path_Files);
1045 In_Tree.Private_Part.Path_Files.Table
1046 (Path_File_Table.Last (In_Tree.Private_Part.Path_Files)) :=
1049 end Create_New_Path_File;
1051 ---------------------------
1052 -- Delete_All_Path_Files --
1053 ---------------------------
1055 procedure Delete_All_Path_Files (In_Tree : Project_Tree_Ref) is
1056 Disregard : Boolean := True;
1057 pragma Warnings (Off, Disregard);
1060 for Index in Path_File_Table.First ..
1061 Path_File_Table.Last (In_Tree.Private_Part.Path_Files)
1063 if In_Tree.Private_Part.Path_Files.Table (Index) /= No_Path then
1066 (In_Tree.Private_Part.Path_Files.Table (Index)),
1071 -- If any of the environment variables ADA_PRJ_INCLUDE_FILE or
1072 -- ADA_PRJ_OBJECTS_FILE has been set, then reset their value to
1073 -- the empty string. On VMS, this has the effect of deassigning
1074 -- the logical names.
1076 if In_Tree.Private_Part.Ada_Prj_Include_File_Set then
1077 Setenv (Project_Include_Path_File, "");
1078 In_Tree.Private_Part.Ada_Prj_Include_File_Set := False;
1081 if In_Tree.Private_Part.Ada_Prj_Objects_File_Set then
1082 Setenv (Project_Objects_Path_File, "");
1083 In_Tree.Private_Part.Ada_Prj_Objects_File_Set := False;
1085 end Delete_All_Path_Files;
1087 ------------------------------------
1088 -- File_Name_Of_Library_Unit_Body --
1089 ------------------------------------
1091 function File_Name_Of_Library_Unit_Body
1093 Project : Project_Id;
1094 In_Tree : Project_Tree_Ref;
1095 Main_Project_Only : Boolean := True;
1096 Full_Path : Boolean := False) return String
1098 The_Project : Project_Id := Project;
1099 Original_Name : String := Name;
1101 Extended_Spec_Name : String :=
1103 Spec_Suffix_Of (In_Tree, "ada", Project.Naming);
1104 Extended_Body_Name : String :=
1106 Body_Suffix_Of (In_Tree, "ada", Project.Naming);
1110 The_Original_Name : Name_Id;
1111 The_Spec_Name : Name_Id;
1112 The_Body_Name : Name_Id;
1115 Canonical_Case_File_Name (Original_Name);
1116 Name_Len := Original_Name'Length;
1117 Name_Buffer (1 .. Name_Len) := Original_Name;
1118 The_Original_Name := Name_Find;
1120 Canonical_Case_File_Name (Extended_Spec_Name);
1121 Name_Len := Extended_Spec_Name'Length;
1122 Name_Buffer (1 .. Name_Len) := Extended_Spec_Name;
1123 The_Spec_Name := Name_Find;
1125 Canonical_Case_File_Name (Extended_Body_Name);
1126 Name_Len := Extended_Body_Name'Length;
1127 Name_Buffer (1 .. Name_Len) := Extended_Body_Name;
1128 The_Body_Name := Name_Find;
1130 if Current_Verbosity = High then
1131 Write_Str ("Looking for file name of """);
1135 Write_Str (" Extended Spec Name = """);
1136 Write_Str (Extended_Spec_Name);
1139 Write_Str (" Extended Body Name = """);
1140 Write_Str (Extended_Body_Name);
1145 -- For extending project, search in the extended project if the source
1146 -- is not found. For non extending projects, this loop will be run only
1150 -- Loop through units
1151 -- Should have comment explaining reverse ???
1153 for Current in reverse Unit_Table.First ..
1154 Unit_Table.Last (In_Tree.Units)
1156 Unit := In_Tree.Units.Table (Current);
1160 if not Main_Project_Only
1161 or else Unit.File_Names (Body_Part).Project = The_Project
1164 Current_Name : constant File_Name_Type :=
1165 Unit.File_Names (Body_Part).Name;
1168 -- Case of a body present
1170 if Current_Name /= No_File then
1171 if Current_Verbosity = High then
1172 Write_Str (" Comparing with """);
1173 Write_Str (Get_Name_String (Current_Name));
1178 -- If it has the name of the original name, return the
1181 if Unit.Name = The_Original_Name
1183 Current_Name = File_Name_Type (The_Original_Name)
1185 if Current_Verbosity = High then
1190 return Get_Name_String
1191 (Unit.File_Names (Body_Part).Path.Name);
1194 return Get_Name_String (Current_Name);
1197 -- If it has the name of the extended body name,
1198 -- return the extended body name
1200 elsif Current_Name = File_Name_Type (The_Body_Name) then
1201 if Current_Verbosity = High then
1206 return Get_Name_String
1207 (Unit.File_Names (Body_Part).Path.Name);
1210 return Extended_Body_Name;
1214 if Current_Verbosity = High then
1215 Write_Line (" not good");
1224 if not Main_Project_Only
1225 or else Unit.File_Names (Specification).Project = The_Project
1228 Current_Name : constant File_Name_Type :=
1229 Unit.File_Names (Specification).Name;
1232 -- Case of spec present
1234 if Current_Name /= No_File then
1235 if Current_Verbosity = High then
1236 Write_Str (" Comparing with """);
1237 Write_Str (Get_Name_String (Current_Name));
1242 -- If name same as original name, return original name
1244 if Unit.Name = The_Original_Name
1246 Current_Name = File_Name_Type (The_Original_Name)
1248 if Current_Verbosity = High then
1253 return Get_Name_String
1254 (Unit.File_Names (Specification).Path.Name);
1256 return Get_Name_String (Current_Name);
1259 -- If it has the same name as the extended spec name,
1260 -- return the extended spec name.
1262 elsif Current_Name = File_Name_Type (The_Spec_Name) then
1263 if Current_Verbosity = High then
1268 return Get_Name_String
1269 (Unit.File_Names (Specification).Path.Name);
1271 return Extended_Spec_Name;
1275 if Current_Verbosity = High then
1276 Write_Line (" not good");
1284 -- If we are not in an extending project, give up
1286 exit when not Main_Project_Only
1287 or else The_Project.Extends = No_Project;
1289 -- Otherwise, look in the project we are extending
1291 The_Project := The_Project.Extends;
1294 -- We don't know this file name, return an empty string
1297 end File_Name_Of_Library_Unit_Body;
1299 -------------------------
1300 -- For_All_Object_Dirs --
1301 -------------------------
1303 procedure For_All_Object_Dirs (Project : Project_Id) is
1304 procedure For_Project (Prj : Project_Id; Dummy : in out Integer);
1305 -- Get all object directories of Prj
1311 procedure For_Project (Prj : Project_Id; Dummy : in out Integer) is
1312 pragma Unreferenced (Dummy);
1314 -- ??? Set_Ada_Paths has a different behavior for library project
1315 -- files, should we have the same ?
1317 if Prj.Object_Directory /= No_Path_Information then
1318 Get_Name_String (Prj.Object_Directory.Display_Name);
1319 Action (Name_Buffer (1 .. Name_Len));
1323 procedure Get_Object_Dirs is
1324 new For_Every_Project_Imported (Integer, For_Project);
1325 Dummy : Integer := 1;
1327 -- Start of processing for For_All_Object_Dirs
1330 Get_Object_Dirs (Project, Dummy);
1331 end For_All_Object_Dirs;
1333 -------------------------
1334 -- For_All_Source_Dirs --
1335 -------------------------
1337 procedure For_All_Source_Dirs
1338 (Project : Project_Id;
1339 In_Tree : Project_Tree_Ref)
1341 procedure For_Project (Prj : Project_Id; Dummy : in out Integer);
1342 -- Get all object directories of Prj
1348 procedure For_Project (Prj : Project_Id; Dummy : in out Integer) is
1349 pragma Unreferenced (Dummy);
1350 Current : String_List_Id := Prj.Source_Dirs;
1351 The_String : String_Element;
1354 -- If there are Ada sources, call action with the name of every
1355 -- source directory.
1357 if Has_Ada_Sources (Project) then
1358 while Current /= Nil_String loop
1359 The_String := In_Tree.String_Elements.Table (Current);
1360 Action (Get_Name_String (The_String.Display_Value));
1361 Current := The_String.Next;
1366 procedure Get_Source_Dirs is
1367 new For_Every_Project_Imported (Integer, For_Project);
1368 Dummy : Integer := 1;
1370 -- Start of processing for For_All_Source_Dirs
1373 Get_Source_Dirs (Project, Dummy);
1374 end For_All_Source_Dirs;
1380 procedure Get_Reference
1381 (Source_File_Name : String;
1382 In_Tree : Project_Tree_Ref;
1383 Project : out Project_Id;
1384 Path : out Path_Name_Type)
1387 -- Body below could use some comments ???
1389 if Current_Verbosity > Default then
1390 Write_Str ("Getting Reference_Of (""");
1391 Write_Str (Source_File_Name);
1392 Write_Str (""") ... ");
1396 Original_Name : String := Source_File_Name;
1400 Canonical_Case_File_Name (Original_Name);
1402 for Id in Unit_Table.First ..
1403 Unit_Table.Last (In_Tree.Units)
1405 Unit := In_Tree.Units.Table (Id);
1407 if (Unit.File_Names (Specification).Name /= No_File
1409 Namet.Get_Name_String
1410 (Unit.File_Names (Specification).Name) = Original_Name)
1411 or else (Unit.File_Names (Specification).Path /=
1414 Namet.Get_Name_String
1415 (Unit.File_Names (Specification).Path.Name) =
1418 Project := Ultimate_Extension_Of
1419 (Project => Unit.File_Names (Specification).Project);
1420 Path := Unit.File_Names (Specification).Path.Display_Name;
1422 if Current_Verbosity > Default then
1423 Write_Str ("Done: Specification.");
1429 elsif (Unit.File_Names (Body_Part).Name /= No_File
1431 Namet.Get_Name_String
1432 (Unit.File_Names (Body_Part).Name) = Original_Name)
1433 or else (Unit.File_Names (Body_Part).Path /= No_Path_Information
1434 and then Namet.Get_Name_String
1435 (Unit.File_Names (Body_Part).Path.Name) =
1438 Project := Ultimate_Extension_Of
1439 (Project => Unit.File_Names (Body_Part).Project);
1440 Path := Unit.File_Names (Body_Part).Path.Display_Name;
1442 if Current_Verbosity > Default then
1443 Write_Str ("Done: Body.");
1452 Project := No_Project;
1455 if Current_Verbosity > Default then
1456 Write_Str ("Cannot be found.");
1465 procedure Initialize (In_Tree : Project_Tree_Ref) is
1467 In_Tree.Private_Part.Fill_Mapping_File := True;
1468 In_Tree.Private_Part.Current_Source_Path_File := No_Path;
1469 In_Tree.Private_Part.Current_Object_Path_File := No_Path;
1476 -- Could use some comments in this body ???
1478 procedure Print_Sources (In_Tree : Project_Tree_Ref) is
1482 Write_Line ("List of Sources:");
1484 for Id in Unit_Table.First ..
1485 Unit_Table.Last (In_Tree.Units)
1487 Unit := In_Tree.Units.Table (Id);
1489 Write_Line (Namet.Get_Name_String (Unit.Name));
1491 if Unit.File_Names (Specification).Name /= No_File then
1492 if Unit.File_Names (Specification).Project = No_Project then
1493 Write_Line (" No project");
1496 Write_Str (" Project: ");
1498 (Unit.File_Names (Specification).Project.Path.Name);
1499 Write_Line (Name_Buffer (1 .. Name_Len));
1502 Write_Str (" spec: ");
1504 (Namet.Get_Name_String
1505 (Unit.File_Names (Specification).Name));
1508 if Unit.File_Names (Body_Part).Name /= No_File then
1509 if Unit.File_Names (Body_Part).Project = No_Project then
1510 Write_Line (" No project");
1513 Write_Str (" Project: ");
1515 (Unit.File_Names (Body_Part).Project.Path.Name);
1516 Write_Line (Name_Buffer (1 .. Name_Len));
1519 Write_Str (" body: ");
1521 (Namet.Get_Name_String
1522 (Unit.File_Names (Body_Part).Name));
1526 Write_Line ("end of List of Sources.");
1535 Main_Project : Project_Id;
1536 In_Tree : Project_Tree_Ref) return Project_Id
1538 Result : Project_Id := No_Project;
1540 Original_Name : String := Name;
1542 Extended_Spec_Name : String :=
1543 Name & Spec_Suffix_Of (In_Tree, "ada", Main_Project.Naming);
1544 Extended_Body_Name : String :=
1545 Name & Body_Suffix_Of (In_Tree, "ada", Main_Project.Naming);
1549 Current_Name : File_Name_Type;
1550 The_Original_Name : File_Name_Type;
1551 The_Spec_Name : File_Name_Type;
1552 The_Body_Name : File_Name_Type;
1555 Canonical_Case_File_Name (Original_Name);
1556 Name_Len := Original_Name'Length;
1557 Name_Buffer (1 .. Name_Len) := Original_Name;
1558 The_Original_Name := Name_Find;
1560 Canonical_Case_File_Name (Extended_Spec_Name);
1561 Name_Len := Extended_Spec_Name'Length;
1562 Name_Buffer (1 .. Name_Len) := Extended_Spec_Name;
1563 The_Spec_Name := Name_Find;
1565 Canonical_Case_File_Name (Extended_Body_Name);
1566 Name_Len := Extended_Body_Name'Length;
1567 Name_Buffer (1 .. Name_Len) := Extended_Body_Name;
1568 The_Body_Name := Name_Find;
1570 for Current in reverse Unit_Table.First ..
1571 Unit_Table.Last (In_Tree.Units)
1573 Unit := In_Tree.Units.Table (Current);
1577 Current_Name := Unit.File_Names (Body_Part).Name;
1579 -- Case of a body present
1581 if Current_Name /= No_File then
1583 -- If it has the name of the original name or the body name,
1584 -- we have found the project.
1586 if Unit.Name = Name_Id (The_Original_Name)
1587 or else Current_Name = The_Original_Name
1588 or else Current_Name = The_Body_Name
1590 Result := Unit.File_Names (Body_Part).Project;
1597 Current_Name := Unit.File_Names (Specification).Name;
1599 if Current_Name /= No_File then
1601 -- If name same as the original name, or the spec name, we have
1602 -- found the project.
1604 if Unit.Name = Name_Id (The_Original_Name)
1605 or else Current_Name = The_Original_Name
1606 or else Current_Name = The_Spec_Name
1608 Result := Unit.File_Names (Specification).Project;
1614 -- Get the ultimate extending project
1616 if Result /= No_Project then
1617 while Result.Extended_By /= No_Project loop
1618 Result := Result.Extended_By;
1629 procedure Set_Ada_Paths
1630 (Project : Project_Id;
1631 In_Tree : Project_Tree_Ref;
1632 Including_Libraries : Boolean)
1635 Source_FD : File_Descriptor := Invalid_FD;
1636 Object_FD : File_Descriptor := Invalid_FD;
1638 Process_Source_Dirs : Boolean := False;
1639 Process_Object_Dirs : Boolean := False;
1642 -- For calls to Close
1646 procedure Recursive_Add (Project : Project_Id; Dummy : in out Boolean);
1647 -- Recursive procedure to add the source/object paths of extended/
1648 -- imported projects.
1654 procedure Recursive_Add (Project : Project_Id; Dummy : in out Boolean) is
1655 pragma Unreferenced (Dummy);
1657 Path : Path_Name_Type;
1660 -- ??? This is almost the equivalent of For_All_Source_Dirs
1662 if Process_Source_Dirs then
1664 -- Add to path all source directories of this project if there are
1667 if Has_Ada_Sources (Project) then
1668 Add_To_Source_Path (Project.Source_Dirs, In_Tree);
1672 if Process_Object_Dirs then
1673 Path := Get_Object_Directory
1675 Including_Libraries => Including_Libraries,
1676 Only_If_Ada => True);
1678 if Path /= No_Path then
1679 Add_To_Object_Path (Path, In_Tree);
1684 procedure For_All_Projects is
1685 new For_Every_Project_Imported (Boolean, Recursive_Add);
1686 Dummy : Boolean := False;
1688 -- Start of processing for Set_Ada_Paths
1691 -- If it is the first time we call this procedure for this project,
1692 -- compute the source path and/or the object path.
1694 if Project.Include_Path_File = No_Path then
1695 Process_Source_Dirs := True;
1696 Create_New_Path_File
1697 (In_Tree, Source_FD, Project.Include_Path_File);
1700 -- For the object path, we make a distinction depending on
1701 -- Including_Libraries.
1703 if Including_Libraries then
1704 if Project.Objects_Path_File_With_Libs = No_Path then
1705 Process_Object_Dirs := True;
1706 Create_New_Path_File
1707 (In_Tree, Object_FD, Project.Objects_Path_File_With_Libs);
1711 if Project.Objects_Path_File_Without_Libs = No_Path then
1712 Process_Object_Dirs := True;
1713 Create_New_Path_File
1714 (In_Tree, Object_FD, Project.Objects_Path_File_Without_Libs);
1718 -- If there is something to do, set Seen to False for all projects,
1719 -- then call the recursive procedure Add for Project.
1721 if Process_Source_Dirs or Process_Object_Dirs then
1722 Source_Path_Table.Set_Last (In_Tree.Private_Part.Source_Paths, 0);
1723 Object_Path_Table.Set_Last (In_Tree.Private_Part.Object_Paths, 0);
1724 For_All_Projects (Project, Dummy);
1727 -- Write and close any file that has been created
1729 if Source_FD /= Invalid_FD then
1730 for Index in Source_Path_Table.First ..
1731 Source_Path_Table.Last
1732 (In_Tree.Private_Part.Source_Paths)
1734 Get_Name_String (In_Tree.Private_Part.Source_Paths.Table (Index));
1735 Name_Len := Name_Len + 1;
1736 Name_Buffer (Name_Len) := ASCII.LF;
1737 Len := Write (Source_FD, Name_Buffer (1)'Address, Name_Len);
1739 if Len /= Name_Len then
1740 Prj.Com.Fail ("disk full");
1744 Close (Source_FD, Status);
1747 Prj.Com.Fail ("disk full");
1751 if Object_FD /= Invalid_FD then
1752 for Index in Object_Path_Table.First ..
1753 Object_Path_Table.Last
1754 (In_Tree.Private_Part.Object_Paths)
1756 Get_Name_String (In_Tree.Private_Part.Object_Paths.Table (Index));
1757 Name_Len := Name_Len + 1;
1758 Name_Buffer (Name_Len) := ASCII.LF;
1759 Len := Write (Object_FD, Name_Buffer (1)'Address, Name_Len);
1761 if Len /= Name_Len then
1762 Prj.Com.Fail ("disk full");
1766 Close (Object_FD, Status);
1769 Prj.Com.Fail ("disk full");
1773 -- Set the env vars, if they need to be changed, and set the
1774 -- corresponding flags.
1776 if In_Tree.Private_Part.Current_Source_Path_File /=
1777 Project.Include_Path_File
1779 In_Tree.Private_Part.Current_Source_Path_File :=
1780 Project.Include_Path_File;
1782 (Project_Include_Path_File,
1783 Get_Name_String (In_Tree.Private_Part.Current_Source_Path_File));
1784 In_Tree.Private_Part.Ada_Prj_Include_File_Set := True;
1787 if Including_Libraries then
1788 if In_Tree.Private_Part.Current_Object_Path_File /=
1789 Project.Objects_Path_File_With_Libs
1791 In_Tree.Private_Part.Current_Object_Path_File :=
1792 Project.Objects_Path_File_With_Libs;
1794 (Project_Objects_Path_File,
1796 (In_Tree.Private_Part.Current_Object_Path_File));
1797 In_Tree.Private_Part.Ada_Prj_Objects_File_Set := True;
1801 if In_Tree.Private_Part.Current_Object_Path_File /=
1802 Project.Objects_Path_File_Without_Libs
1804 In_Tree.Private_Part.Current_Object_Path_File :=
1805 Project.Objects_Path_File_Without_Libs;
1807 (Project_Objects_Path_File,
1809 (In_Tree.Private_Part.Current_Object_Path_File));
1810 In_Tree.Private_Part.Ada_Prj_Objects_File_Set := True;
1815 ---------------------------------------------
1816 -- Set_Mapping_File_Initial_State_To_Empty --
1817 ---------------------------------------------
1819 procedure Set_Mapping_File_Initial_State_To_Empty
1820 (In_Tree : Project_Tree_Ref)
1823 In_Tree.Private_Part.Fill_Mapping_File := False;
1824 end Set_Mapping_File_Initial_State_To_Empty;
1826 -----------------------
1827 -- Set_Path_File_Var --
1828 -----------------------
1830 procedure Set_Path_File_Var (Name : String; Value : String) is
1831 Host_Spec : String_Access := To_Host_File_Spec (Value);
1833 if Host_Spec = null then
1835 ("could not convert file name """ & Value & """ to host spec");
1837 Setenv (Name, Host_Spec.all);
1840 end Set_Path_File_Var;
1842 ---------------------------
1843 -- Ultimate_Extension_Of --
1844 ---------------------------
1846 function Ultimate_Extension_Of
1847 (Project : Project_Id) return Project_Id
1849 Result : Project_Id := Project;
1852 while Result.Extended_By /= No_Project loop
1853 Result := Result.Extended_By;
1857 end Ultimate_Extension_Of;