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 := Units_Htable.Get_First (In_Tree.Units_HT);
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 = Spec 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
676 while Current_Unit /= No_Unit_Index loop
677 if Current_Unit.File_Names (Spec) /= null
678 and then Current_Unit.File_Names (Spec).Naming_Exception
680 Put (Current_Unit.Name,
681 Current_Unit.File_Names (Spec).File,
683 Current_Unit.File_Names (Spec).Index);
686 if Current_Unit.File_Names (Impl) /= null
687 and then Current_Unit.File_Names (Impl).Naming_Exception
689 Put (Current_Unit.Name,
690 Current_Unit.File_Names (Impl).File,
692 Current_Unit.File_Names (Impl).Index);
695 Current_Unit := Units_Htable.Get_Next (In_Tree.Units_HT);
698 -- If there are no non standard naming scheme, issue the GNAT
699 -- standard naming scheme. This will tell the compiler that
700 -- a project file is used and will forbid any pragma SFN.
702 if File = Invalid_FD then
705 Put_Line (File, "pragma Source_File_Name_Project");
706 Put_Line (File, " (Spec_File_Name => ""*.ads"",");
707 Put_Line (File, " Dot_Replacement => ""-"",");
708 Put_Line (File, " Casing => lowercase);");
710 Put_Line (File, "pragma Source_File_Name_Project");
711 Put_Line (File, " (Body_File_Name => ""*.adb"",");
712 Put_Line (File, " Dot_Replacement => ""-"",");
713 Put_Line (File, " Casing => lowercase);");
716 -- Close the temporary file
718 GNAT.OS_Lib.Close (File, Status);
721 Prj.Com.Fail ("disk full");
724 if Opt.Verbose_Mode then
725 Write_Str ("Closing configuration file """);
726 Write_Str (Get_Name_String (File_Name));
730 For_Project.Config_File_Name := File_Name;
731 For_Project.Config_File_Temp := True;
732 For_Project.Config_Checked := True;
734 end Create_Config_Pragmas_File;
740 procedure Create_Mapping (In_Tree : Project_Tree_Ref) is
747 Unit := Units_Htable.Get_First (In_Tree.Units_HT);
749 while Unit /= No_Unit_Index loop
750 -- Process only if the unit has a valid name
752 if Unit.Name /= No_Name then
753 Data := Unit.File_Names (Spec);
755 -- If there is a spec, put it in the mapping
758 if Data.Path.Name = Slash then
759 Fmap.Add_Forbidden_File_Name (Data.File);
762 (Unit_Name => Unit_Name_Type (Unit.Name),
763 File_Name => Data.File,
764 Path_Name => File_Name_Type (Data.Path.Name));
768 Data := Unit.File_Names (Impl);
770 -- If there is a body (or subunit) put it in the mapping
773 if Data.Path.Name = Slash then
774 Fmap.Add_Forbidden_File_Name (Data.File);
777 (Unit_Name => Unit_Name_Type (Unit.Name),
778 File_Name => Data.File,
779 Path_Name => File_Name_Type (Data.Path.Name));
784 Unit := Units_Htable.Get_Next (In_Tree.Units_HT);
788 -------------------------
789 -- Create_Mapping_File --
790 -------------------------
792 procedure Create_Mapping_File
793 (Project : Project_Id;
794 Language : Name_Id := No_Name;
795 In_Tree : Project_Tree_Ref;
796 Name : out Path_Name_Type)
798 File : File_Descriptor := Invalid_FD;
801 Present : Project_Boolean_Htable.Instance;
802 -- For each project in the closure of Project, the corresponding flag
803 -- will be set to True.
806 Suffix : File_Name_Type;
809 Iter : Source_Iterator;
811 procedure Put_Name_Buffer;
812 -- Put the line contained in the Name_Buffer in the mapping file
814 procedure Put_Data (Spec : Boolean);
815 -- Put the mapping of the spec or body contained in Data in the file
818 procedure Recursive_Flag (Prj : Project_Id);
819 -- Set the flags corresponding to Prj, the projects it imports
820 -- (directly or indirectly) or extends to True. Call itself recursively.
826 procedure Put_Name_Buffer is
830 Name_Len := Name_Len + 1;
831 Name_Buffer (Name_Len) := ASCII.LF;
832 Last := Write (File, Name_Buffer (1)'Address, Name_Len);
834 if Last /= Name_Len then
835 Prj.Com.Fail ("Disk full, cannot write mapping file");
843 procedure Put_Data (Spec : Boolean) is
845 -- Line with the unit name
847 Get_Name_String (Unit.Name);
848 Name_Len := Name_Len + 1;
849 Name_Buffer (Name_Len) := '%';
850 Name_Len := Name_Len + 1;
853 Name_Buffer (Name_Len) := 's';
855 Name_Buffer (Name_Len) := 'b';
860 -- Line with the file name
862 Get_Name_String (Data.File);
865 -- Line with the path name
867 Get_Name_String (Data.Path.Name);
875 procedure Recursive_Flag (Prj : Project_Id) is
876 Imported : Project_List;
879 -- Nothing to do for non existent project or project that has already
883 and then not Project_Boolean_Htable.Get (Present, Prj)
885 Project_Boolean_Htable.Set (Present, Prj, True);
887 Imported := Prj.Imported_Projects;
888 while Imported /= null loop
889 Recursive_Flag (Imported.Project);
890 Imported := Imported.Next;
893 Recursive_Flag (Prj.Extends);
897 -- Start of processing for Create_Mapping_File
900 -- Flag the necessary projects
902 Recursive_Flag (Project);
904 -- Create the temporary file
906 Tempdir.Create_Temp_File (File, Name => Name);
908 if File = Invalid_FD then
909 Prj.Com.Fail ("unable to create temporary mapping file");
912 Record_Temp_File (Name);
914 if Opt.Verbose_Mode then
915 Write_Str ("Creating temp mapping file """);
916 Write_Str (Get_Name_String (Name));
921 if Language = No_Name then
922 if In_Tree.Private_Part.Fill_Mapping_File then
923 Unit := Units_Htable.Get_First (In_Tree.Units_HT);
924 while Unit /= null loop
925 -- Case of unit has a valid name
927 if Unit.Name /= No_Name then
928 Data := Unit.File_Names (Spec);
930 -- If there is a spec, put it mapping in the file if it is
931 -- from a project in the closure of Project.
934 and then Project_Boolean_Htable.Get (Present, Data.Project)
936 Put_Data (Spec => True);
939 Data := Unit.File_Names (Impl);
941 -- If there is a body (or subunit) put its mapping in the
942 -- file if it is from a project in the closure of Project.
945 and then Project_Boolean_Htable.Get (Present, Data.Project)
947 Put_Data (Spec => False);
951 Unit := Units_Htable.Get_Next (In_Tree.Units_HT);
955 -- If language is defined
958 -- For all source of the Language of all projects in the closure
964 P := In_Tree.Projects;
966 if Project_Boolean_Htable.Get (Present, P.Project) then
968 Iter := For_Each_Source (In_Tree, P.Project);
970 Source := Prj.Element (Iter);
971 exit when Source = No_Source;
973 if Source.Language.Name = Language
974 and then not Source.Locally_Removed
975 and then Source.Replaced_By = No_Source
976 and then Source.Path.Name /= No_Path
978 if Source.Unit /= No_Unit_Index then
979 Get_Name_String (Source.Unit.Name);
981 if Source.Kind = Spec then
983 Source.Language.Config.Mapping_Spec_Suffix;
986 Source.Language.Config.Mapping_Body_Suffix;
989 if Suffix /= No_File then
990 Add_Str_To_Name_Buffer
991 (Get_Name_String (Suffix));
997 Get_Name_String (Source.File);
1000 Get_Name_String (Source.Path.Name);
1013 GNAT.OS_Lib.Close (File, Status);
1017 -- We were able to create the temporary file, so there is no problem
1018 -- of protection. However, we are not able to close it, so there must
1019 -- be a capacity problem that we express using "disk full".
1021 Prj.Com.Fail ("disk full, could not write mapping file");
1024 Project_Boolean_Htable.Reset (Present);
1025 end Create_Mapping_File;
1027 --------------------------
1028 -- Create_New_Path_File --
1029 --------------------------
1031 procedure Create_New_Path_File
1032 (In_Tree : Project_Tree_Ref;
1033 Path_FD : out File_Descriptor;
1034 Path_Name : out Path_Name_Type)
1037 Tempdir.Create_Temp_File (Path_FD, Path_Name);
1039 if Path_Name /= No_Path then
1040 Record_Temp_File (Path_Name);
1042 -- Record the name, so that the temp path file will be deleted at the
1043 -- end of the program.
1045 Path_File_Table.Increment_Last (In_Tree.Private_Part.Path_Files);
1046 In_Tree.Private_Part.Path_Files.Table
1047 (Path_File_Table.Last (In_Tree.Private_Part.Path_Files)) :=
1050 end Create_New_Path_File;
1052 ---------------------------
1053 -- Delete_All_Path_Files --
1054 ---------------------------
1056 procedure Delete_All_Path_Files (In_Tree : Project_Tree_Ref) is
1057 Disregard : Boolean := True;
1058 pragma Unreferenced (Disregard);
1061 for Index in Path_File_Table.First ..
1062 Path_File_Table.Last (In_Tree.Private_Part.Path_Files)
1064 if In_Tree.Private_Part.Path_Files.Table (Index) /= No_Path then
1067 (In_Tree.Private_Part.Path_Files.Table (Index)),
1072 -- If any of the environment variables ADA_PRJ_INCLUDE_FILE or
1073 -- ADA_PRJ_OBJECTS_FILE has been set, then reset their value to
1074 -- the empty string. On VMS, this has the effect of deassigning
1075 -- the logical names.
1077 if In_Tree.Private_Part.Ada_Prj_Include_File_Set then
1078 Setenv (Project_Include_Path_File, "");
1079 In_Tree.Private_Part.Ada_Prj_Include_File_Set := False;
1082 if In_Tree.Private_Part.Ada_Prj_Objects_File_Set then
1083 Setenv (Project_Objects_Path_File, "");
1084 In_Tree.Private_Part.Ada_Prj_Objects_File_Set := False;
1086 end Delete_All_Path_Files;
1088 ------------------------------------
1089 -- File_Name_Of_Library_Unit_Body --
1090 ------------------------------------
1092 function File_Name_Of_Library_Unit_Body
1094 Project : Project_Id;
1095 In_Tree : Project_Tree_Ref;
1096 Main_Project_Only : Boolean := True;
1097 Full_Path : Boolean := False) return String
1099 The_Project : Project_Id := Project;
1100 Original_Name : String := Name;
1102 Extended_Spec_Name : String :=
1104 Spec_Suffix_Of (In_Tree, "ada", Project.Naming);
1105 Extended_Body_Name : String :=
1107 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
1152 Unit := Units_Htable.Get_First (In_Tree.Units_HT);
1153 while Unit /= null loop
1156 if not Main_Project_Only
1158 (Unit.File_Names (Impl) /= null
1159 and then Unit.File_Names (Impl).Project = The_Project)
1162 Current_Name : File_Name_Type;
1164 -- Case of a body present
1166 if Unit.File_Names (Impl) /= null then
1167 Current_Name := Unit.File_Names (Impl).File;
1169 if Current_Verbosity = High then
1170 Write_Str (" Comparing with """);
1171 Write_Str (Get_Name_String (Current_Name));
1176 -- If it has the name of the original name, return the
1179 if Unit.Name = The_Original_Name
1181 Current_Name = File_Name_Type (The_Original_Name)
1183 if Current_Verbosity = High then
1188 return Get_Name_String
1189 (Unit.File_Names (Impl).Path.Name);
1192 return Get_Name_String (Current_Name);
1195 -- If it has the name of the extended body name,
1196 -- return the extended body name
1198 elsif Current_Name = File_Name_Type (The_Body_Name) then
1199 if Current_Verbosity = High then
1204 return Get_Name_String
1205 (Unit.File_Names (Impl).Path.Name);
1208 return Extended_Body_Name;
1212 if Current_Verbosity = High then
1213 Write_Line (" not good");
1222 if not Main_Project_Only
1224 (Unit.File_Names (Spec) /= null
1225 and then Unit.File_Names (Spec).Project =
1229 Current_Name : File_Name_Type;
1232 -- Case of spec present
1234 if Unit.File_Names (Spec) /= null then
1235 Current_Name := Unit.File_Names (Spec).File;
1236 if Current_Verbosity = High then
1237 Write_Str (" Comparing with """);
1238 Write_Str (Get_Name_String (Current_Name));
1243 -- If name same as original name, return original name
1245 if Unit.Name = The_Original_Name
1247 Current_Name = File_Name_Type (The_Original_Name)
1249 if Current_Verbosity = High then
1254 return Get_Name_String
1255 (Unit.File_Names (Spec).Path.Name);
1257 return Get_Name_String (Current_Name);
1260 -- If it has the same name as the extended spec name,
1261 -- return the extended spec name.
1263 elsif Current_Name = File_Name_Type (The_Spec_Name) then
1264 if Current_Verbosity = High then
1269 return Get_Name_String
1270 (Unit.File_Names (Spec).Path.Name);
1272 return Extended_Spec_Name;
1276 if Current_Verbosity = High then
1277 Write_Line (" not good");
1284 Unit := Units_Htable.Get_Next (In_Tree.Units_HT);
1287 -- If we are not in an extending project, give up
1289 exit when not Main_Project_Only
1290 or else The_Project.Extends = No_Project;
1292 -- Otherwise, look in the project we are extending
1294 The_Project := The_Project.Extends;
1297 -- We don't know this file name, return an empty string
1300 end File_Name_Of_Library_Unit_Body;
1302 -------------------------
1303 -- For_All_Object_Dirs --
1304 -------------------------
1306 procedure For_All_Object_Dirs (Project : Project_Id) is
1307 procedure For_Project (Prj : Project_Id; Dummy : in out Integer);
1308 -- Get all object directories of Prj
1314 procedure For_Project (Prj : Project_Id; Dummy : in out Integer) is
1315 pragma Unreferenced (Dummy);
1317 -- ??? Set_Ada_Paths has a different behavior for library project
1318 -- files, should we have the same ?
1320 if Prj.Object_Directory /= No_Path_Information then
1321 Get_Name_String (Prj.Object_Directory.Display_Name);
1322 Action (Name_Buffer (1 .. Name_Len));
1326 procedure Get_Object_Dirs is
1327 new For_Every_Project_Imported (Integer, For_Project);
1328 Dummy : Integer := 1;
1330 -- Start of processing for For_All_Object_Dirs
1333 Get_Object_Dirs (Project, Dummy);
1334 end For_All_Object_Dirs;
1336 -------------------------
1337 -- For_All_Source_Dirs --
1338 -------------------------
1340 procedure For_All_Source_Dirs
1341 (Project : Project_Id;
1342 In_Tree : Project_Tree_Ref)
1344 procedure For_Project (Prj : Project_Id; Dummy : in out Integer);
1345 -- Get all object directories of Prj
1351 procedure For_Project (Prj : Project_Id; Dummy : in out Integer) is
1352 pragma Unreferenced (Dummy);
1353 Current : String_List_Id := Prj.Source_Dirs;
1354 The_String : String_Element;
1357 -- If there are Ada sources, call action with the name of every
1358 -- source directory.
1360 if Has_Ada_Sources (Project) then
1361 while Current /= Nil_String loop
1362 The_String := In_Tree.String_Elements.Table (Current);
1363 Action (Get_Name_String (The_String.Display_Value));
1364 Current := The_String.Next;
1369 procedure Get_Source_Dirs is
1370 new For_Every_Project_Imported (Integer, For_Project);
1371 Dummy : Integer := 1;
1373 -- Start of processing for For_All_Source_Dirs
1376 Get_Source_Dirs (Project, Dummy);
1377 end For_All_Source_Dirs;
1383 procedure Get_Reference
1384 (Source_File_Name : String;
1385 In_Tree : Project_Tree_Ref;
1386 Project : out Project_Id;
1387 Path : out Path_Name_Type)
1390 -- Body below could use some comments ???
1392 if Current_Verbosity > Default then
1393 Write_Str ("Getting Reference_Of (""");
1394 Write_Str (Source_File_Name);
1395 Write_Str (""") ... ");
1399 Original_Name : String := Source_File_Name;
1403 Canonical_Case_File_Name (Original_Name);
1404 Unit := Units_Htable.Get_First (In_Tree.Units_HT);
1406 while Unit /= null loop
1407 if Unit.File_Names (Spec) /= null
1408 and then Unit.File_Names (Spec).File /= No_File
1410 (Namet.Get_Name_String
1411 (Unit.File_Names (Spec).File) = Original_Name
1412 or else (Unit.File_Names (Spec).Path /=
1415 Namet.Get_Name_String
1416 (Unit.File_Names (Spec).Path.Name) =
1419 Project := Ultimate_Extension_Of
1420 (Project => Unit.File_Names (Spec).Project);
1421 Path := Unit.File_Names (Spec).Path.Display_Name;
1423 if Current_Verbosity > Default then
1424 Write_Str ("Done: Spec.");
1430 elsif Unit.File_Names (Impl) /= null
1431 and then Unit.File_Names (Impl).File /= No_File
1433 (Namet.Get_Name_String
1434 (Unit.File_Names (Impl).File) = Original_Name
1435 or else (Unit.File_Names (Impl).Path /=
1437 and then Namet.Get_Name_String
1438 (Unit.File_Names (Impl).Path.Name) =
1441 Project := Ultimate_Extension_Of
1442 (Project => Unit.File_Names (Impl).Project);
1443 Path := Unit.File_Names (Impl).Path.Display_Name;
1445 if Current_Verbosity > Default then
1446 Write_Str ("Done: Body.");
1453 Unit := Units_Htable.Get_Next (In_Tree.Units_HT);
1457 Project := No_Project;
1460 if Current_Verbosity > Default then
1461 Write_Str ("Cannot be found.");
1470 procedure Initialize (In_Tree : Project_Tree_Ref) is
1472 In_Tree.Private_Part.Fill_Mapping_File := True;
1473 In_Tree.Private_Part.Current_Source_Path_File := No_Path;
1474 In_Tree.Private_Part.Current_Object_Path_File := No_Path;
1481 -- Could use some comments in this body ???
1483 procedure Print_Sources (In_Tree : Project_Tree_Ref) is
1487 Write_Line ("List of Sources:");
1489 Unit := Units_Htable.Get_First (In_Tree.Units_HT);
1491 while Unit /= No_Unit_Index loop
1493 Write_Line (Namet.Get_Name_String (Unit.Name));
1495 if Unit.File_Names (Spec).File /= No_File then
1496 if Unit.File_Names (Spec).Project = No_Project then
1497 Write_Line (" No project");
1500 Write_Str (" Project: ");
1502 (Unit.File_Names (Spec).Project.Path.Name);
1503 Write_Line (Name_Buffer (1 .. Name_Len));
1506 Write_Str (" spec: ");
1508 (Namet.Get_Name_String
1509 (Unit.File_Names (Spec).File));
1512 if Unit.File_Names (Impl).File /= No_File then
1513 if Unit.File_Names (Impl).Project = No_Project then
1514 Write_Line (" No project");
1517 Write_Str (" Project: ");
1519 (Unit.File_Names (Impl).Project.Path.Name);
1520 Write_Line (Name_Buffer (1 .. Name_Len));
1523 Write_Str (" body: ");
1525 (Namet.Get_Name_String (Unit.File_Names (Impl).File));
1528 Unit := Units_Htable.Get_Next (In_Tree.Units_HT);
1531 Write_Line ("end of List of Sources.");
1540 Main_Project : Project_Id;
1541 In_Tree : Project_Tree_Ref) return Project_Id
1543 Result : Project_Id := No_Project;
1545 Original_Name : String := Name;
1547 Extended_Spec_Name : String :=
1548 Name & Spec_Suffix_Of (In_Tree, "ada", Main_Project.Naming);
1549 Extended_Body_Name : String :=
1550 Name & Body_Suffix_Of (In_Tree, "ada", Main_Project.Naming);
1554 Current_Name : File_Name_Type;
1555 The_Original_Name : File_Name_Type;
1556 The_Spec_Name : File_Name_Type;
1557 The_Body_Name : File_Name_Type;
1560 Canonical_Case_File_Name (Original_Name);
1561 Name_Len := Original_Name'Length;
1562 Name_Buffer (1 .. Name_Len) := Original_Name;
1563 The_Original_Name := Name_Find;
1565 Canonical_Case_File_Name (Extended_Spec_Name);
1566 Name_Len := Extended_Spec_Name'Length;
1567 Name_Buffer (1 .. Name_Len) := Extended_Spec_Name;
1568 The_Spec_Name := Name_Find;
1570 Canonical_Case_File_Name (Extended_Body_Name);
1571 Name_Len := Extended_Body_Name'Length;
1572 Name_Buffer (1 .. Name_Len) := Extended_Body_Name;
1573 The_Body_Name := Name_Find;
1575 Unit := Units_Htable.Get_First (In_Tree.Units_HT);
1577 while Unit /= null loop
1578 -- Case of a body present
1580 if Unit.File_Names (Impl) /= null then
1581 Current_Name := Unit.File_Names (Impl).File;
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 (Impl).Project;
1597 if Unit.File_Names (Spec) /= null then
1598 Current_Name := Unit.File_Names (Spec).File;
1600 -- If name same as the original name, or the spec name, we have
1601 -- found the project.
1603 if Unit.Name = Name_Id (The_Original_Name)
1604 or else Current_Name = The_Original_Name
1605 or else Current_Name = The_Spec_Name
1607 Result := Unit.File_Names (Spec).Project;
1612 Unit := Units_Htable.Get_Next (In_Tree.Units_HT);
1615 -- Get the ultimate extending project
1617 if Result /= No_Project then
1618 while Result.Extended_By /= No_Project loop
1619 Result := Result.Extended_By;
1630 procedure Set_Ada_Paths
1631 (Project : Project_Id;
1632 In_Tree : Project_Tree_Ref;
1633 Including_Libraries : Boolean)
1636 Source_FD : File_Descriptor := Invalid_FD;
1637 Object_FD : File_Descriptor := Invalid_FD;
1639 Process_Source_Dirs : Boolean := False;
1640 Process_Object_Dirs : Boolean := False;
1643 -- For calls to Close
1647 procedure Recursive_Add (Project : Project_Id; Dummy : in out Boolean);
1648 -- Recursive procedure to add the source/object paths of extended/
1649 -- imported projects.
1655 procedure Recursive_Add (Project : Project_Id; Dummy : in out Boolean) is
1656 pragma Unreferenced (Dummy);
1658 Path : Path_Name_Type;
1661 -- ??? This is almost the equivalent of For_All_Source_Dirs
1663 if Process_Source_Dirs then
1665 -- Add to path all source directories of this project if there are
1668 if Has_Ada_Sources (Project) then
1669 Add_To_Source_Path (Project.Source_Dirs, In_Tree);
1673 if Process_Object_Dirs then
1674 Path := Get_Object_Directory
1676 Including_Libraries => Including_Libraries,
1677 Only_If_Ada => True);
1679 if Path /= No_Path then
1680 Add_To_Object_Path (Path, In_Tree);
1685 procedure For_All_Projects is
1686 new For_Every_Project_Imported (Boolean, Recursive_Add);
1687 Dummy : Boolean := False;
1689 -- Start of processing for Set_Ada_Paths
1692 -- If it is the first time we call this procedure for this project,
1693 -- compute the source path and/or the object path.
1695 if Project.Include_Path_File = No_Path then
1696 Process_Source_Dirs := True;
1697 Create_New_Path_File
1698 (In_Tree, Source_FD, Project.Include_Path_File);
1701 -- For the object path, we make a distinction depending on
1702 -- Including_Libraries.
1704 if Including_Libraries then
1705 if Project.Objects_Path_File_With_Libs = No_Path then
1706 Process_Object_Dirs := True;
1707 Create_New_Path_File
1708 (In_Tree, Object_FD, Project.Objects_Path_File_With_Libs);
1712 if Project.Objects_Path_File_Without_Libs = No_Path then
1713 Process_Object_Dirs := True;
1714 Create_New_Path_File
1715 (In_Tree, Object_FD, Project.Objects_Path_File_Without_Libs);
1719 -- If there is something to do, set Seen to False for all projects,
1720 -- then call the recursive procedure Add for Project.
1722 if Process_Source_Dirs or Process_Object_Dirs then
1723 Source_Path_Table.Set_Last (In_Tree.Private_Part.Source_Paths, 0);
1724 Object_Path_Table.Set_Last (In_Tree.Private_Part.Object_Paths, 0);
1725 For_All_Projects (Project, Dummy);
1728 -- Write and close any file that has been created
1730 if Source_FD /= Invalid_FD then
1731 for Index in Source_Path_Table.First ..
1732 Source_Path_Table.Last
1733 (In_Tree.Private_Part.Source_Paths)
1735 Get_Name_String (In_Tree.Private_Part.Source_Paths.Table (Index));
1736 Name_Len := Name_Len + 1;
1737 Name_Buffer (Name_Len) := ASCII.LF;
1738 Len := Write (Source_FD, Name_Buffer (1)'Address, Name_Len);
1740 if Len /= Name_Len then
1741 Prj.Com.Fail ("disk full");
1745 Close (Source_FD, Status);
1748 Prj.Com.Fail ("disk full");
1752 if Object_FD /= Invalid_FD then
1753 for Index in Object_Path_Table.First ..
1754 Object_Path_Table.Last
1755 (In_Tree.Private_Part.Object_Paths)
1757 Get_Name_String (In_Tree.Private_Part.Object_Paths.Table (Index));
1758 Name_Len := Name_Len + 1;
1759 Name_Buffer (Name_Len) := ASCII.LF;
1760 Len := Write (Object_FD, Name_Buffer (1)'Address, Name_Len);
1762 if Len /= Name_Len then
1763 Prj.Com.Fail ("disk full");
1767 Close (Object_FD, Status);
1770 Prj.Com.Fail ("disk full");
1774 -- Set the env vars, if they need to be changed, and set the
1775 -- corresponding flags.
1777 if In_Tree.Private_Part.Current_Source_Path_File /=
1778 Project.Include_Path_File
1780 In_Tree.Private_Part.Current_Source_Path_File :=
1781 Project.Include_Path_File;
1783 (Project_Include_Path_File,
1784 Get_Name_String (In_Tree.Private_Part.Current_Source_Path_File));
1785 In_Tree.Private_Part.Ada_Prj_Include_File_Set := True;
1788 if Including_Libraries then
1789 if In_Tree.Private_Part.Current_Object_Path_File /=
1790 Project.Objects_Path_File_With_Libs
1792 In_Tree.Private_Part.Current_Object_Path_File :=
1793 Project.Objects_Path_File_With_Libs;
1795 (Project_Objects_Path_File,
1797 (In_Tree.Private_Part.Current_Object_Path_File));
1798 In_Tree.Private_Part.Ada_Prj_Objects_File_Set := True;
1802 if In_Tree.Private_Part.Current_Object_Path_File /=
1803 Project.Objects_Path_File_Without_Libs
1805 In_Tree.Private_Part.Current_Object_Path_File :=
1806 Project.Objects_Path_File_Without_Libs;
1808 (Project_Objects_Path_File,
1810 (In_Tree.Private_Part.Current_Object_Path_File));
1811 In_Tree.Private_Part.Ada_Prj_Objects_File_Set := True;
1816 ---------------------------------------------
1817 -- Set_Mapping_File_Initial_State_To_Empty --
1818 ---------------------------------------------
1820 procedure Set_Mapping_File_Initial_State_To_Empty
1821 (In_Tree : Project_Tree_Ref)
1824 In_Tree.Private_Part.Fill_Mapping_File := False;
1825 end Set_Mapping_File_Initial_State_To_Empty;
1827 -----------------------
1828 -- Set_Path_File_Var --
1829 -----------------------
1831 procedure Set_Path_File_Var (Name : String; Value : String) is
1832 Host_Spec : String_Access := To_Host_File_Spec (Value);
1834 if Host_Spec = null then
1836 ("could not convert file name """ & Value & """ to host spec");
1838 Setenv (Name, Host_Spec.all);
1841 end Set_Path_File_Var;
1843 ---------------------------
1844 -- Ultimate_Extension_Of --
1845 ---------------------------
1847 function Ultimate_Extension_Of
1848 (Project : Project_Id) return Project_Id
1850 Result : Project_Id;
1854 while Result.Extended_By /= No_Project loop
1855 Result := Result.Extended_By;
1859 end Ultimate_Extension_Of;