2014-09-15 Andreas Krebbel <Andreas.Krebbel@de.ibm.com>
[official-gcc.git] / gcc / ada / makeutl.adb
blobcbfd01e49d3c7262e30eae8f6137bade97c4a16e
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- M A K E U T L --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2004-2014, 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 ALI; use ALI;
27 with Atree; use Atree;
28 with Debug;
29 with Err_Vars; use Err_Vars;
30 with Errutil;
31 with Fname;
32 with Osint; use Osint;
33 with Output; use Output;
34 with Opt; use Opt;
35 with Prj.Com;
36 with Prj.Err;
37 with Prj.Ext;
38 with Prj.Util; use Prj.Util;
39 with Sinput.P;
40 with Tempdir;
42 with Ada.Command_Line; use Ada.Command_Line;
43 with Ada.Unchecked_Deallocation;
45 with GNAT.Case_Util; use GNAT.Case_Util;
46 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
47 with GNAT.HTable;
48 with GNAT.Regexp; use GNAT.Regexp;
50 package body Makeutl is
52 type Linker_Options_Data is record
53 Project : Project_Id;
54 Options : String_List_Id;
55 end record;
57 Linker_Option_Initial_Count : constant := 20;
59 Linker_Options_Buffer : String_List_Access :=
60 new String_List (1 .. Linker_Option_Initial_Count);
62 Last_Linker_Option : Natural := 0;
64 package Linker_Opts is new Table.Table (
65 Table_Component_Type => Linker_Options_Data,
66 Table_Index_Type => Integer,
67 Table_Low_Bound => 1,
68 Table_Initial => 10,
69 Table_Increment => 100,
70 Table_Name => "Make.Linker_Opts");
72 procedure Add_Linker_Option (Option : String);
74 ---------
75 -- Add --
76 ---------
78 procedure Add
79 (Option : String_Access;
80 To : in out String_List_Access;
81 Last : in out Natural)
83 begin
84 if Last = To'Last then
85 declare
86 New_Options : constant String_List_Access :=
87 new String_List (1 .. To'Last * 2);
89 begin
90 New_Options (To'Range) := To.all;
92 -- Set all elements of the original options to null to avoid
93 -- deallocation of copies.
95 To.all := (others => null);
97 Free (To);
98 To := New_Options;
99 end;
100 end if;
102 Last := Last + 1;
103 To (Last) := Option;
104 end Add;
106 procedure Add
107 (Option : String;
108 To : in out String_List_Access;
109 Last : in out Natural)
111 begin
112 Add (Option => new String'(Option), To => To, Last => Last);
113 end Add;
115 -----------------------
116 -- Add_Linker_Option --
117 -----------------------
119 procedure Add_Linker_Option (Option : String) is
120 begin
121 if Option'Length > 0 then
122 if Last_Linker_Option = Linker_Options_Buffer'Last then
123 declare
124 New_Buffer : constant String_List_Access :=
125 new String_List
126 (1 .. Linker_Options_Buffer'Last +
127 Linker_Option_Initial_Count);
128 begin
129 New_Buffer (Linker_Options_Buffer'Range) :=
130 Linker_Options_Buffer.all;
131 Linker_Options_Buffer.all := (others => null);
132 Free (Linker_Options_Buffer);
133 Linker_Options_Buffer := New_Buffer;
134 end;
135 end if;
137 Last_Linker_Option := Last_Linker_Option + 1;
138 Linker_Options_Buffer (Last_Linker_Option) := new String'(Option);
139 end if;
140 end Add_Linker_Option;
142 -------------------
143 -- Absolute_Path --
144 -------------------
146 function Absolute_Path
147 (Path : Path_Name_Type;
148 Project : Project_Id) return String
150 begin
151 Get_Name_String (Path);
153 declare
154 Path_Name : constant String := Name_Buffer (1 .. Name_Len);
156 begin
157 if Is_Absolute_Path (Path_Name) then
158 return Path_Name;
160 else
161 declare
162 Parent_Directory : constant String :=
163 Get_Name_String
164 (Project.Directory.Display_Name);
166 begin
167 return Parent_Directory & Path_Name;
168 end;
169 end if;
170 end;
171 end Absolute_Path;
173 ----------------------------
174 -- Aggregate_Libraries_In --
175 ----------------------------
177 function Aggregate_Libraries_In (Tree : Project_Tree_Ref) return Boolean is
178 List : Project_List;
180 begin
181 List := Tree.Projects;
182 while List /= null loop
183 if List.Project.Qualifier = Aggregate_Library then
184 return True;
185 end if;
187 List := List.Next;
188 end loop;
190 return False;
191 end Aggregate_Libraries_In;
193 -------------------------
194 -- Base_Name_Index_For --
195 -------------------------
197 function Base_Name_Index_For
198 (Main : String;
199 Main_Index : Int;
200 Index_Separator : Character) return File_Name_Type
202 Result : File_Name_Type;
204 begin
205 Name_Len := 0;
206 Add_Str_To_Name_Buffer (Base_Name (Main));
208 -- Remove the extension, if any, that is the last part of the base name
209 -- starting with a dot and following some characters.
211 for J in reverse 2 .. Name_Len loop
212 if Name_Buffer (J) = '.' then
213 Name_Len := J - 1;
214 exit;
215 end if;
216 end loop;
218 -- Add the index info, if index is different from 0
220 if Main_Index > 0 then
221 Add_Char_To_Name_Buffer (Index_Separator);
223 declare
224 Img : constant String := Main_Index'Img;
225 begin
226 Add_Str_To_Name_Buffer (Img (2 .. Img'Last));
227 end;
228 end if;
230 Result := Name_Find;
231 return Result;
232 end Base_Name_Index_For;
234 ------------------------------
235 -- Check_Source_Info_In_ALI --
236 ------------------------------
238 function Check_Source_Info_In_ALI
239 (The_ALI : ALI_Id;
240 Tree : Project_Tree_Ref) return Name_Id
242 Result : Name_Id := No_Name;
243 Unit_Name : Name_Id;
245 begin
246 -- Loop through units
248 for U in ALIs.Table (The_ALI).First_Unit ..
249 ALIs.Table (The_ALI).Last_Unit
250 loop
251 -- Check if the file name is one of the source of the unit
253 Get_Name_String (Units.Table (U).Uname);
254 Name_Len := Name_Len - 2;
255 Unit_Name := Name_Find;
257 if File_Not_A_Source_Of (Tree, Unit_Name, Units.Table (U).Sfile) then
258 return No_Name;
259 end if;
261 if Result = No_Name then
262 Result := Unit_Name;
263 end if;
265 -- Loop to do same check for each of the withed units
267 for W in Units.Table (U).First_With .. Units.Table (U).Last_With loop
268 declare
269 WR : ALI.With_Record renames Withs.Table (W);
271 begin
272 if WR.Sfile /= No_File then
273 Get_Name_String (WR.Uname);
274 Name_Len := Name_Len - 2;
275 Unit_Name := Name_Find;
277 if File_Not_A_Source_Of (Tree, Unit_Name, WR.Sfile) then
278 return No_Name;
279 end if;
280 end if;
281 end;
282 end loop;
283 end loop;
285 -- Loop to check subunits and replaced sources
287 for D in ALIs.Table (The_ALI).First_Sdep ..
288 ALIs.Table (The_ALI).Last_Sdep
289 loop
290 declare
291 SD : Sdep_Record renames Sdep.Table (D);
293 begin
294 Unit_Name := SD.Subunit_Name;
296 if Unit_Name = No_Name then
298 -- Check if this source file has been replaced by a source with
299 -- a different file name.
301 if Tree /= null and then Tree.Replaced_Source_Number > 0 then
302 declare
303 Replacement : constant File_Name_Type :=
304 Replaced_Source_HTable.Get
305 (Tree.Replaced_Sources, SD.Sfile);
307 begin
308 if Replacement /= No_File then
309 if Verbose_Mode then
310 Write_Line
311 ("source file"
312 & Get_Name_String (SD.Sfile)
313 & " has been replaced by "
314 & Get_Name_String (Replacement));
315 end if;
317 return No_Name;
318 end if;
319 end;
320 end if;
322 -- Check that a dependent source for a unit that is from a
323 -- project is indeed a source of this unit.
325 Unit_Name := SD.Unit_Name;
327 if Unit_Name /= No_Name
328 and then not Fname.Is_Internal_File_Name (SD.Sfile)
329 and then File_Not_A_Source_Of (Tree, Unit_Name, SD.Sfile)
330 then
331 return No_Name;
332 end if;
334 else
335 -- For separates, the file is no longer associated with the
336 -- unit ("proc-sep.adb" is not associated with unit "proc.sep")
337 -- so we need to check whether the source file still exists in
338 -- the source tree: it will if it matches the naming scheme
339 -- (and then will be for the same unit).
341 if Find_Source
342 (In_Tree => Tree,
343 Project => No_Project,
344 Base_Name => SD.Sfile) = No_Source
345 then
346 -- If this is not a runtime file or if, when gnatmake switch
347 -- -a is used, we are not able to find this subunit in the
348 -- source directories, then recompilation is needed.
350 if not Fname.Is_Internal_File_Name (SD.Sfile)
351 or else
352 (Check_Readonly_Files
353 and then Full_Source_Name (SD.Sfile) = No_File)
354 then
355 if Verbose_Mode then
356 Write_Line
357 ("While parsing ALI file, file "
358 & Get_Name_String (SD.Sfile)
359 & " is indicated as containing subunit "
360 & Get_Name_String (Unit_Name)
361 & " but this does not match what was found while"
362 & " parsing the project. Will recompile");
363 end if;
365 return No_Name;
366 end if;
367 end if;
368 end if;
369 end;
370 end loop;
372 return Result;
373 end Check_Source_Info_In_ALI;
375 --------------------------------
376 -- Create_Binder_Mapping_File --
377 --------------------------------
379 function Create_Binder_Mapping_File
380 (Project_Tree : Project_Tree_Ref) return Path_Name_Type
382 Mapping_Path : Path_Name_Type := No_Path;
384 Mapping_FD : File_Descriptor := Invalid_FD;
385 -- A File Descriptor for an eventual mapping file
387 ALI_Unit : Unit_Name_Type := No_Unit_Name;
388 -- The unit name of an ALI file
390 ALI_Name : File_Name_Type := No_File;
391 -- The file name of the ALI file
393 ALI_Project : Project_Id := No_Project;
394 -- The project of the ALI file
396 Bytes : Integer;
397 OK : Boolean := False;
398 Unit : Unit_Index;
400 Status : Boolean;
401 -- For call to Close
403 Iter : Source_Iterator := For_Each_Source
404 (In_Tree => Project_Tree,
405 Language => Name_Ada,
406 Encapsulated_Libs => False,
407 Locally_Removed => False);
409 Source : Prj.Source_Id;
411 begin
412 Tempdir.Create_Temp_File (Mapping_FD, Mapping_Path);
413 Record_Temp_File (Project_Tree.Shared, Mapping_Path);
415 if Mapping_FD /= Invalid_FD then
416 OK := True;
418 loop
419 Source := Element (Iter);
420 exit when Source = No_Source;
422 Unit := Source.Unit;
424 if Source.Replaced_By /= No_Source
425 or else Unit = No_Unit_Index
426 or else Unit.Name = No_Name
427 then
428 ALI_Name := No_File;
430 -- If this is a body, put it in the mapping
432 elsif Source.Kind = Impl
433 and then Unit.File_Names (Impl) /= No_Source
434 and then Unit.File_Names (Impl).Project /= No_Project
435 then
436 Get_Name_String (Unit.Name);
437 Add_Str_To_Name_Buffer ("%b");
438 ALI_Unit := Name_Find;
439 ALI_Name :=
440 Lib_File_Name (Unit.File_Names (Impl).Display_File);
441 ALI_Project := Unit.File_Names (Impl).Project;
443 -- Otherwise, if this is a spec and there is no body, put it in
444 -- the mapping.
446 elsif Source.Kind = Spec
447 and then Unit.File_Names (Impl) = No_Source
448 and then Unit.File_Names (Spec) /= No_Source
449 and then Unit.File_Names (Spec).Project /= No_Project
450 then
451 Get_Name_String (Unit.Name);
452 Add_Str_To_Name_Buffer ("%s");
453 ALI_Unit := Name_Find;
454 ALI_Name :=
455 Lib_File_Name (Unit.File_Names (Spec).Display_File);
456 ALI_Project := Unit.File_Names (Spec).Project;
458 else
459 ALI_Name := No_File;
460 end if;
462 -- If we have something to put in the mapping then do it now. If
463 -- the project is extended, look for the ALI file in the project,
464 -- then in the extending projects in order, and use the last one
465 -- found.
467 if ALI_Name /= No_File then
469 -- Look in the project and the projects that are extending it
470 -- to find the real ALI file.
472 declare
473 ALI : constant String := Get_Name_String (ALI_Name);
474 ALI_Path : Name_Id := No_Name;
476 begin
477 loop
478 -- For library projects, use the library ALI directory,
479 -- for other projects, use the object directory.
481 if ALI_Project.Library then
482 Get_Name_String
483 (ALI_Project.Library_ALI_Dir.Display_Name);
484 else
485 Get_Name_String
486 (ALI_Project.Object_Directory.Display_Name);
487 end if;
489 Add_Str_To_Name_Buffer (ALI);
491 if Is_Regular_File (Name_Buffer (1 .. Name_Len)) then
492 ALI_Path := Name_Find;
493 end if;
495 ALI_Project := ALI_Project.Extended_By;
496 exit when ALI_Project = No_Project;
497 end loop;
499 if ALI_Path /= No_Name then
501 -- First line is the unit name
503 Get_Name_String (ALI_Unit);
504 Add_Char_To_Name_Buffer (ASCII.LF);
505 Bytes :=
506 Write
507 (Mapping_FD,
508 Name_Buffer (1)'Address,
509 Name_Len);
510 OK := Bytes = Name_Len;
512 exit when not OK;
514 -- Second line is the ALI file name
516 Get_Name_String (ALI_Name);
517 Add_Char_To_Name_Buffer (ASCII.LF);
518 Bytes :=
519 Write
520 (Mapping_FD,
521 Name_Buffer (1)'Address,
522 Name_Len);
523 OK := (Bytes = Name_Len);
525 exit when not OK;
527 -- Third line is the ALI path name
529 Get_Name_String (ALI_Path);
530 Add_Char_To_Name_Buffer (ASCII.LF);
531 Bytes :=
532 Write
533 (Mapping_FD,
534 Name_Buffer (1)'Address,
535 Name_Len);
536 OK := (Bytes = Name_Len);
538 -- If OK is False, it means we were unable to write a
539 -- line. No point in continuing with the other units.
541 exit when not OK;
542 end if;
543 end;
544 end if;
546 Next (Iter);
547 end loop;
549 Close (Mapping_FD, Status);
551 OK := OK and Status;
552 end if;
554 -- If the creation of the mapping file was successful, we add the switch
555 -- to the arguments of gnatbind.
557 if OK then
558 return Mapping_Path;
560 else
561 return No_Path;
562 end if;
563 end Create_Binder_Mapping_File;
565 -----------------
566 -- Create_Name --
567 -----------------
569 function Create_Name (Name : String) return File_Name_Type is
570 begin
571 Name_Len := 0;
572 Add_Str_To_Name_Buffer (Name);
573 return Name_Find;
574 end Create_Name;
576 function Create_Name (Name : String) return Name_Id is
577 begin
578 Name_Len := 0;
579 Add_Str_To_Name_Buffer (Name);
580 return Name_Find;
581 end Create_Name;
583 function Create_Name (Name : String) return Path_Name_Type is
584 begin
585 Name_Len := 0;
586 Add_Str_To_Name_Buffer (Name);
587 return Name_Find;
588 end Create_Name;
590 ---------------------------
591 -- Ensure_Absolute_Path --
592 ---------------------------
594 procedure Ensure_Absolute_Path
595 (Switch : in out String_Access;
596 Parent : String;
597 Do_Fail : Fail_Proc;
598 For_Gnatbind : Boolean := False;
599 Including_Non_Switch : Boolean := True;
600 Including_RTS : Boolean := False)
602 begin
603 if Switch /= null then
604 declare
605 Sw : String (1 .. Switch'Length);
606 Start : Positive;
608 begin
609 Sw := Switch.all;
611 if Sw (1) = '-' then
612 if Sw'Length >= 3
613 and then (Sw (2) = 'I'
614 or else (not For_Gnatbind
615 and then (Sw (2) = 'L'
616 or else
617 Sw (2) = 'A')))
618 then
619 Start := 3;
621 if Sw = "-I-" then
622 return;
623 end if;
625 elsif Sw'Length >= 4
626 and then
627 (Sw (2 .. 3) = "aL" or else
628 Sw (2 .. 3) = "aO" or else
629 Sw (2 .. 3) = "aI"
630 or else (For_Gnatbind and then Sw (2 .. 3) = "A="))
631 then
632 Start := 4;
634 elsif Including_RTS
635 and then Sw'Length >= 7
636 and then Sw (2 .. 6) = "-RTS="
637 then
638 Start := 7;
640 else
641 return;
642 end if;
644 -- Because relative path arguments to --RTS= may be relative to
645 -- the search directory prefix, those relative path arguments
646 -- are converted only when they include directory information.
648 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
649 if Parent'Length = 0 then
650 Do_Fail
651 ("relative search path switches ("""
652 & Sw
653 & """) are not allowed");
655 elsif Including_RTS then
656 for J in Start .. Sw'Last loop
657 if Sw (J) = Directory_Separator then
658 Switch :=
659 new String'
660 (Sw (1 .. Start - 1)
661 & Parent
662 & Directory_Separator
663 & Sw (Start .. Sw'Last));
664 return;
665 end if;
666 end loop;
668 else
669 Switch :=
670 new String'
671 (Sw (1 .. Start - 1)
672 & Parent
673 & Directory_Separator
674 & Sw (Start .. Sw'Last));
675 end if;
676 end if;
678 elsif Including_Non_Switch then
679 if not Is_Absolute_Path (Sw) then
680 if Parent'Length = 0 then
681 Do_Fail
682 ("relative paths (""" & Sw & """) are not allowed");
683 else
684 Switch := new String'(Parent & Directory_Separator & Sw);
685 end if;
686 end if;
687 end if;
688 end;
689 end if;
690 end Ensure_Absolute_Path;
692 ----------------------------
693 -- Executable_Prefix_Path --
694 ----------------------------
696 function Executable_Prefix_Path return String is
697 Exec_Name : constant String := Command_Name;
699 function Get_Install_Dir (S : String) return String;
700 -- S is the executable name preceded by the absolute or relative path,
701 -- e.g. "c:\usr\bin\gcc.exe". Returns the absolute directory where "bin"
702 -- lies (in the example "C:\usr"). If the executable is not in a "bin"
703 -- directory, return "".
705 ---------------------
706 -- Get_Install_Dir --
707 ---------------------
709 function Get_Install_Dir (S : String) return String is
710 Exec : String := S;
711 Path_Last : Integer := 0;
713 begin
714 for J in reverse Exec'Range loop
715 if Exec (J) = Directory_Separator then
716 Path_Last := J - 1;
717 exit;
718 end if;
719 end loop;
721 if Path_Last >= Exec'First + 2 then
722 To_Lower (Exec (Path_Last - 2 .. Path_Last));
723 end if;
725 if Path_Last < Exec'First + 2
726 or else Exec (Path_Last - 2 .. Path_Last) /= "bin"
727 or else (Path_Last - 3 >= Exec'First
728 and then Exec (Path_Last - 3) /= Directory_Separator)
729 then
730 return "";
731 end if;
733 return Normalize_Pathname
734 (Exec (Exec'First .. Path_Last - 4),
735 Resolve_Links => Opt.Follow_Links_For_Dirs)
736 & Directory_Separator;
737 end Get_Install_Dir;
739 -- Beginning of Executable_Prefix_Path
741 begin
742 -- First determine if a path prefix was placed in front of the
743 -- executable name.
745 for J in reverse Exec_Name'Range loop
746 if Exec_Name (J) = Directory_Separator then
747 return Get_Install_Dir (Exec_Name);
748 end if;
749 end loop;
751 -- If we get here, the user has typed the executable name with no
752 -- directory prefix.
754 declare
755 Path : String_Access := Locate_Exec_On_Path (Exec_Name);
756 begin
757 if Path = null then
758 return "";
759 else
760 declare
761 Dir : constant String := Get_Install_Dir (Path.all);
762 begin
763 Free (Path);
764 return Dir;
765 end;
766 end if;
767 end;
768 end Executable_Prefix_Path;
770 ------------------
771 -- Fail_Program --
772 ------------------
774 procedure Fail_Program
775 (Project_Tree : Project_Tree_Ref;
776 S : String;
777 Flush_Messages : Boolean := True)
779 begin
780 if Flush_Messages and not No_Exit_Message then
781 if Total_Errors_Detected /= 0 or else Warnings_Detected /= 0 then
782 Errutil.Finalize;
783 end if;
784 end if;
786 Finish_Program (Project_Tree, E_Fatal, S => S);
787 end Fail_Program;
789 --------------------
790 -- Finish_Program --
791 --------------------
793 procedure Finish_Program
794 (Project_Tree : Project_Tree_Ref;
795 Exit_Code : Osint.Exit_Code_Type := Osint.E_Success;
796 S : String := "")
798 begin
799 if not Debug.Debug_Flag_N then
800 Delete_Temp_Config_Files (Project_Tree);
802 if Project_Tree /= null then
803 Delete_All_Temp_Files (Project_Tree.Shared);
804 end if;
805 end if;
807 if S'Length > 0 then
808 if Exit_Code /= E_Success then
809 if No_Exit_Message then
810 Osint.Exit_Program (E_Fatal);
811 else
812 Osint.Fail (S);
813 end if;
815 elsif not No_Exit_Message then
816 Write_Str (S);
817 end if;
818 end if;
820 -- Output Namet statistics
822 Namet.Finalize;
824 Exit_Program (Exit_Code);
825 end Finish_Program;
827 --------------------------
828 -- File_Not_A_Source_Of --
829 --------------------------
831 function File_Not_A_Source_Of
832 (Project_Tree : Project_Tree_Ref;
833 Uname : Name_Id;
834 Sfile : File_Name_Type) return Boolean
836 Unit : constant Unit_Index :=
837 Units_Htable.Get (Project_Tree.Units_HT, Uname);
839 At_Least_One_File : Boolean := False;
841 begin
842 if Unit /= No_Unit_Index then
843 for F in Unit.File_Names'Range loop
844 if Unit.File_Names (F) /= null then
845 At_Least_One_File := True;
846 if Unit.File_Names (F).File = Sfile then
847 return False;
848 end if;
849 end if;
850 end loop;
852 if not At_Least_One_File then
854 -- The unit was probably created initially for a separate unit
855 -- (which are initially created as IMPL when both suffixes are the
856 -- same). Later on, Override_Kind changed the type of the file,
857 -- and the unit is no longer valid in fact.
859 return False;
860 end if;
862 Verbose_Msg (Uname, "sources do not include ", Name_Id (Sfile));
863 return True;
864 end if;
866 return False;
867 end File_Not_A_Source_Of;
869 ---------------------
870 -- Get_Directories --
871 ---------------------
873 procedure Get_Directories
874 (Project_Tree : Project_Tree_Ref;
875 For_Project : Project_Id;
876 Activity : Activity_Type;
877 Languages : Name_Ids)
880 procedure Recursive_Add
881 (Project : Project_Id;
882 Tree : Project_Tree_Ref;
883 Extended : in out Boolean);
884 -- Add all the source directories of a project to the path only if
885 -- this project has not been visited. Calls itself recursively for
886 -- projects being extended, and imported projects.
888 procedure Add_Dir (Value : Path_Name_Type);
889 -- Add directory Value in table Directories, if it is defined and not
890 -- already there.
892 -------------
893 -- Add_Dir --
894 -------------
896 procedure Add_Dir (Value : Path_Name_Type) is
897 Add_It : Boolean := True;
899 begin
900 if Value /= No_Path then
901 for Index in 1 .. Directories.Last loop
902 if Directories.Table (Index) = Value then
903 Add_It := False;
904 exit;
905 end if;
906 end loop;
908 if Add_It then
909 Directories.Increment_Last;
910 Directories.Table (Directories.Last) := Value;
911 end if;
912 end if;
913 end Add_Dir;
915 -------------------
916 -- Recursive_Add --
917 -------------------
919 procedure Recursive_Add
920 (Project : Project_Id;
921 Tree : Project_Tree_Ref;
922 Extended : in out Boolean)
924 Current : String_List_Id;
925 Dir : String_Element;
926 OK : Boolean := False;
927 Lang_Proc : Language_Ptr := Project.Languages;
929 begin
930 -- Add to path all directories of this project
932 if Activity = Compilation then
933 Lang_Loop :
934 while Lang_Proc /= No_Language_Index loop
935 for J in Languages'Range loop
936 OK := Lang_Proc.Name = Languages (J);
937 exit Lang_Loop when OK;
938 end loop;
940 Lang_Proc := Lang_Proc.Next;
941 end loop Lang_Loop;
943 if OK then
944 Current := Project.Source_Dirs;
946 while Current /= Nil_String loop
947 Dir := Tree.Shared.String_Elements.Table (Current);
948 Add_Dir (Path_Name_Type (Dir.Value));
949 Current := Dir.Next;
950 end loop;
951 end if;
953 elsif Project.Library then
954 if Activity = SAL_Binding and then Extended then
955 Add_Dir (Project.Object_Directory.Display_Name);
957 else
958 Add_Dir (Project.Library_ALI_Dir.Display_Name);
959 end if;
961 else
962 Add_Dir (Project.Object_Directory.Display_Name);
963 end if;
965 if Project.Extends = No_Project then
966 Extended := False;
967 end if;
968 end Recursive_Add;
970 procedure For_All_Projects is
971 new For_Every_Project_Imported (Boolean, Recursive_Add);
973 Extended : Boolean := True;
975 -- Start of processing for Get_Directories
977 begin
978 Directories.Init;
979 For_All_Projects (For_Project, Project_Tree, Extended);
980 end Get_Directories;
982 ------------------
983 -- Get_Switches --
984 ------------------
986 procedure Get_Switches
987 (Source : Prj.Source_Id;
988 Pkg_Name : Name_Id;
989 Project_Tree : Project_Tree_Ref;
990 Value : out Variable_Value;
991 Is_Default : out Boolean)
993 begin
994 Get_Switches
995 (Source_File => Source.File,
996 Source_Lang => Source.Language.Name,
997 Source_Prj => Source.Project,
998 Pkg_Name => Pkg_Name,
999 Project_Tree => Project_Tree,
1000 Value => Value,
1001 Is_Default => Is_Default);
1002 end Get_Switches;
1004 ------------------
1005 -- Get_Switches --
1006 ------------------
1008 procedure Get_Switches
1009 (Source_File : File_Name_Type;
1010 Source_Lang : Name_Id;
1011 Source_Prj : Project_Id;
1012 Pkg_Name : Name_Id;
1013 Project_Tree : Project_Tree_Ref;
1014 Value : out Variable_Value;
1015 Is_Default : out Boolean;
1016 Test_Without_Suffix : Boolean := False;
1017 Check_ALI_Suffix : Boolean := False)
1019 Project : constant Project_Id :=
1020 Ultimate_Extending_Project_Of (Source_Prj);
1021 Pkg : constant Package_Id :=
1022 Prj.Util.Value_Of
1023 (Name => Pkg_Name,
1024 In_Packages => Project.Decl.Packages,
1025 Shared => Project_Tree.Shared);
1026 Lang : Language_Ptr;
1028 begin
1029 Is_Default := False;
1031 if Source_File /= No_File then
1032 Value := Prj.Util.Value_Of
1033 (Name => Name_Id (Source_File),
1034 Attribute_Or_Array_Name => Name_Switches,
1035 In_Package => Pkg,
1036 Shared => Project_Tree.Shared,
1037 Allow_Wildcards => True);
1038 end if;
1040 if Value = Nil_Variable_Value and then Test_Without_Suffix then
1041 Lang :=
1042 Get_Language_From_Name (Project, Get_Name_String (Source_Lang));
1044 if Lang /= null then
1045 declare
1046 Naming : Lang_Naming_Data renames Lang.Config.Naming_Data;
1047 SF_Name : constant String := Get_Name_String (Source_File);
1048 Last : Positive := SF_Name'Length;
1049 Name : String (1 .. Last + 3);
1050 Spec_Suffix : String := Get_Name_String (Naming.Spec_Suffix);
1051 Body_Suffix : String := Get_Name_String (Naming.Body_Suffix);
1052 Truncated : Boolean := False;
1054 begin
1055 Canonical_Case_File_Name (Spec_Suffix);
1056 Canonical_Case_File_Name (Body_Suffix);
1057 Name (1 .. Last) := SF_Name;
1059 if Last > Body_Suffix'Length
1060 and then
1061 Name (Last - Body_Suffix'Length + 1 .. Last) = Body_Suffix
1062 then
1063 Truncated := True;
1064 Last := Last - Body_Suffix'Length;
1065 end if;
1067 if not Truncated
1068 and then Last > Spec_Suffix'Length
1069 and then
1070 Name (Last - Spec_Suffix'Length + 1 .. Last) = Spec_Suffix
1071 then
1072 Truncated := True;
1073 Last := Last - Spec_Suffix'Length;
1074 end if;
1076 if Truncated then
1077 Name_Len := 0;
1078 Add_Str_To_Name_Buffer (Name (1 .. Last));
1080 Value := Prj.Util.Value_Of
1081 (Name => Name_Find,
1082 Attribute_Or_Array_Name => Name_Switches,
1083 In_Package => Pkg,
1084 Shared => Project_Tree.Shared,
1085 Allow_Wildcards => True);
1086 end if;
1088 if Value = Nil_Variable_Value and then Check_ALI_Suffix then
1089 Last := SF_Name'Length;
1090 while Name (Last) /= '.' loop
1091 Last := Last - 1;
1092 end loop;
1094 Name_Len := 0;
1095 Add_Str_To_Name_Buffer (Name (1 .. Last));
1096 Add_Str_To_Name_Buffer ("ali");
1098 Value := Prj.Util.Value_Of
1099 (Name => Name_Find,
1100 Attribute_Or_Array_Name => Name_Switches,
1101 In_Package => Pkg,
1102 Shared => Project_Tree.Shared,
1103 Allow_Wildcards => True);
1104 end if;
1105 end;
1106 end if;
1107 end if;
1109 if Value = Nil_Variable_Value then
1110 Is_Default := True;
1111 Value :=
1112 Prj.Util.Value_Of
1113 (Name => Source_Lang,
1114 Attribute_Or_Array_Name => Name_Switches,
1115 In_Package => Pkg,
1116 Shared => Project_Tree.Shared,
1117 Force_Lower_Case_Index => True);
1118 end if;
1120 if Value = Nil_Variable_Value then
1121 Value :=
1122 Prj.Util.Value_Of
1123 (Name => All_Other_Names,
1124 Attribute_Or_Array_Name => Name_Switches,
1125 In_Package => Pkg,
1126 Shared => Project_Tree.Shared,
1127 Force_Lower_Case_Index => True);
1128 end if;
1130 if Value = Nil_Variable_Value then
1131 Value :=
1132 Prj.Util.Value_Of
1133 (Name => Source_Lang,
1134 Attribute_Or_Array_Name => Name_Default_Switches,
1135 In_Package => Pkg,
1136 Shared => Project_Tree.Shared);
1137 end if;
1138 end Get_Switches;
1140 ------------
1141 -- Inform --
1142 ------------
1144 procedure Inform (N : File_Name_Type; Msg : String) is
1145 begin
1146 Inform (Name_Id (N), Msg);
1147 end Inform;
1149 procedure Inform (N : Name_Id := No_Name; Msg : String) is
1150 begin
1151 Osint.Write_Program_Name;
1153 Write_Str (": ");
1155 if N /= No_Name then
1156 Write_Str ("""");
1158 declare
1159 Name : constant String := Get_Name_String (N);
1160 begin
1161 if Debug.Debug_Flag_F and then Is_Absolute_Path (Name) then
1162 Write_Str (File_Name (Name));
1163 else
1164 Write_Str (Name);
1165 end if;
1166 end;
1168 Write_Str (""" ");
1169 end if;
1171 Write_Str (Msg);
1172 Write_Eol;
1173 end Inform;
1175 ------------------------------
1176 -- Initialize_Source_Record --
1177 ------------------------------
1179 procedure Initialize_Source_Record (Source : Prj.Source_Id) is
1181 procedure Set_Object_Project
1182 (Obj_Dir : String;
1183 Obj_Proj : Project_Id;
1184 Obj_Path : Path_Name_Type;
1185 Stamp : Time_Stamp_Type);
1186 -- Update information about object file, switches file,...
1188 ------------------------
1189 -- Set_Object_Project --
1190 ------------------------
1192 procedure Set_Object_Project
1193 (Obj_Dir : String;
1194 Obj_Proj : Project_Id;
1195 Obj_Path : Path_Name_Type;
1196 Stamp : Time_Stamp_Type) is
1197 begin
1198 Source.Object_Project := Obj_Proj;
1199 Source.Object_Path := Obj_Path;
1200 Source.Object_TS := Stamp;
1202 if Source.Language.Config.Dependency_Kind /= None then
1203 declare
1204 Dep_Path : constant String :=
1205 Normalize_Pathname
1206 (Name =>
1207 Get_Name_String (Source.Dep_Name),
1208 Resolve_Links => Opt.Follow_Links_For_Files,
1209 Directory => Obj_Dir);
1210 begin
1211 Source.Dep_Path := Create_Name (Dep_Path);
1212 Source.Dep_TS := Osint.Unknown_Attributes;
1213 end;
1214 end if;
1216 -- Get the path of the switches file, even if Opt.Check_Switches is
1217 -- not set, as switch -s may be in the Builder switches that have not
1218 -- been scanned yet.
1220 declare
1221 Switches_Path : constant String :=
1222 Normalize_Pathname
1223 (Name =>
1224 Get_Name_String (Source.Switches),
1225 Resolve_Links => Opt.Follow_Links_For_Files,
1226 Directory => Obj_Dir);
1227 begin
1228 Source.Switches_Path := Create_Name (Switches_Path);
1230 if Stamp /= Empty_Time_Stamp then
1231 Source.Switches_TS := File_Stamp (Source.Switches_Path);
1232 end if;
1233 end;
1234 end Set_Object_Project;
1236 Obj_Proj : Project_Id;
1238 begin
1239 -- Nothing to do if source record has already been fully initialized
1241 if Source.Initialized then
1242 return;
1243 end if;
1245 -- Systematically recompute the time stamp
1247 Source.Source_TS := File_Stamp (Source.Path.Display_Name);
1249 -- Parse the source file to check whether we have a subunit
1251 if Source.Language.Config.Kind = Unit_Based
1252 and then Source.Kind = Impl
1253 and then Is_Subunit (Source)
1254 then
1255 Source.Kind := Sep;
1256 end if;
1258 if Source.Language.Config.Object_Generated
1259 and then Is_Compilable (Source)
1260 then
1261 -- First, get the correct object file name and dependency file name
1262 -- if the source is in a multi-unit file.
1264 if Source.Index /= 0 then
1265 Source.Object :=
1266 Object_Name
1267 (Source_File_Name => Source.File,
1268 Source_Index => Source.Index,
1269 Index_Separator =>
1270 Source.Language.Config.Multi_Unit_Object_Separator,
1271 Object_File_Suffix =>
1272 Source.Language.Config.Object_File_Suffix);
1274 Source.Dep_Name :=
1275 Dependency_Name
1276 (Source.Object, Source.Language.Config.Dependency_Kind);
1277 end if;
1279 -- Find the object file for that source. It could be either in the
1280 -- current project or in an extended project (it might actually not
1281 -- exist yet in the ultimate extending project, but if not found
1282 -- elsewhere that's where we'll expect to find it).
1284 Obj_Proj := Source.Project;
1286 while Obj_Proj /= No_Project loop
1287 if Obj_Proj.Object_Directory /= No_Path_Information then
1288 declare
1289 Dir : constant String :=
1290 Get_Name_String (Obj_Proj.Object_Directory.Display_Name);
1292 Object_Path : constant String :=
1293 Normalize_Pathname
1294 (Name => Get_Name_String (Source.Object),
1295 Resolve_Links => Opt.Follow_Links_For_Files,
1296 Directory => Dir);
1298 Obj_Path : constant Path_Name_Type :=
1299 Create_Name (Object_Path);
1301 Stamp : Time_Stamp_Type := Empty_Time_Stamp;
1303 begin
1304 -- For specs, we do not check object files if there is a
1305 -- body. This saves a system call. On the other hand, we do
1306 -- need to know the object_path, in case the user has passed
1307 -- the .ads on the command line to compile the spec only.
1309 if Source.Kind /= Spec
1310 or else Source.Unit = No_Unit_Index
1311 or else Source.Unit.File_Names (Impl) = No_Source
1312 then
1313 Stamp := File_Stamp (Obj_Path);
1314 end if;
1316 if Stamp /= Empty_Time_Stamp
1317 or else (Obj_Proj.Extended_By = No_Project
1318 and then Source.Object_Project = No_Project)
1319 then
1320 Set_Object_Project (Dir, Obj_Proj, Obj_Path, Stamp);
1321 end if;
1322 end;
1323 end if;
1325 Obj_Proj := Obj_Proj.Extended_By;
1326 end loop;
1328 elsif Source.Language.Config.Dependency_Kind = Makefile then
1329 declare
1330 Object_Dir : constant String :=
1331 Get_Name_String (Source.Project.Object_Directory.Display_Name);
1332 Dep_Path : constant String :=
1333 Normalize_Pathname
1334 (Name => Get_Name_String (Source.Dep_Name),
1335 Resolve_Links => Opt.Follow_Links_For_Files,
1336 Directory => Object_Dir);
1337 begin
1338 Source.Dep_Path := Create_Name (Dep_Path);
1339 Source.Dep_TS := Osint.Unknown_Attributes;
1340 end;
1341 end if;
1343 Source.Initialized := True;
1344 end Initialize_Source_Record;
1346 ----------------------------
1347 -- Is_External_Assignment --
1348 ----------------------------
1350 function Is_External_Assignment
1351 (Env : Prj.Tree.Environment;
1352 Argv : String) return Boolean
1354 Start : Positive := 3;
1355 Finish : Natural := Argv'Last;
1357 pragma Assert (Argv'First = 1);
1358 pragma Assert (Argv (1 .. 2) = "-X");
1360 begin
1361 if Argv'Last < 5 then
1362 return False;
1364 elsif Argv (3) = '"' then
1365 if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
1366 return False;
1367 else
1368 Start := 4;
1369 Finish := Argv'Last - 1;
1370 end if;
1371 end if;
1373 return Prj.Ext.Check
1374 (Self => Env.External,
1375 Declaration => Argv (Start .. Finish));
1376 end Is_External_Assignment;
1378 ----------------
1379 -- Is_Subunit --
1380 ----------------
1382 function Is_Subunit (Source : Prj.Source_Id) return Boolean is
1383 Src_Ind : Source_File_Index;
1385 begin
1386 if Source.Kind = Sep then
1387 return True;
1389 -- A Spec, a file based language source or a body with a spec cannot be
1390 -- a subunit.
1392 elsif Source.Kind = Spec
1393 or else Source.Unit = No_Unit_Index
1394 or else Other_Part (Source) /= No_Source
1395 then
1396 return False;
1397 end if;
1399 -- Here, we are assuming that the language is Ada, as it is the only
1400 -- unit based language that we know.
1402 Src_Ind :=
1403 Sinput.P.Load_Project_File
1404 (Get_Name_String (Source.Path.Display_Name));
1406 return Sinput.P.Source_File_Is_Subunit (Src_Ind);
1407 end Is_Subunit;
1409 -----------------------------
1410 -- Linker_Options_Switches --
1411 -----------------------------
1413 function Linker_Options_Switches
1414 (Project : Project_Id;
1415 Do_Fail : Fail_Proc;
1416 In_Tree : Project_Tree_Ref) return String_List
1418 procedure Recursive_Add
1419 (Proj : Project_Id;
1420 In_Tree : Project_Tree_Ref;
1421 Dummy : in out Boolean);
1422 -- The recursive routine used to add linker options
1424 -------------------
1425 -- Recursive_Add --
1426 -------------------
1428 procedure Recursive_Add
1429 (Proj : Project_Id;
1430 In_Tree : Project_Tree_Ref;
1431 Dummy : in out Boolean)
1433 Linker_Package : Package_Id;
1434 Options : Variable_Value;
1436 begin
1437 Linker_Package :=
1438 Prj.Util.Value_Of
1439 (Name => Name_Linker,
1440 In_Packages => Proj.Decl.Packages,
1441 Shared => In_Tree.Shared);
1443 Options :=
1444 Prj.Util.Value_Of
1445 (Name => Name_Ada,
1446 Index => 0,
1447 Attribute_Or_Array_Name => Name_Linker_Options,
1448 In_Package => Linker_Package,
1449 Shared => In_Tree.Shared);
1451 -- If attribute is present, add the project with the attribute to
1452 -- table Linker_Opts.
1454 if Options /= Nil_Variable_Value then
1455 Linker_Opts.Increment_Last;
1456 Linker_Opts.Table (Linker_Opts.Last) :=
1457 (Project => Proj, Options => Options.Values);
1458 end if;
1459 end Recursive_Add;
1461 procedure For_All_Projects is
1462 new For_Every_Project_Imported (Boolean, Recursive_Add);
1464 Dummy : Boolean := False;
1466 -- Start of processing for Linker_Options_Switches
1468 begin
1469 Linker_Opts.Init;
1471 For_All_Projects (Project, In_Tree, Dummy, Imported_First => True);
1473 Last_Linker_Option := 0;
1475 for Index in reverse 1 .. Linker_Opts.Last loop
1476 declare
1477 Options : String_List_Id;
1478 Proj : constant Project_Id :=
1479 Linker_Opts.Table (Index).Project;
1480 Option : Name_Id;
1481 Dir_Path : constant String :=
1482 Get_Name_String (Proj.Directory.Name);
1484 begin
1485 Options := Linker_Opts.Table (Index).Options;
1486 while Options /= Nil_String loop
1487 Option := In_Tree.Shared.String_Elements.Table (Options).Value;
1488 Get_Name_String (Option);
1490 -- Do not consider empty linker options
1492 if Name_Len /= 0 then
1493 Add_Linker_Option (Name_Buffer (1 .. Name_Len));
1495 -- Object files and -L switches specified with relative
1496 -- paths must be converted to absolute paths.
1498 Ensure_Absolute_Path
1499 (Switch =>
1500 Linker_Options_Buffer (Last_Linker_Option),
1501 Parent => Dir_Path,
1502 Do_Fail => Do_Fail,
1503 For_Gnatbind => False);
1504 end if;
1506 Options := In_Tree.Shared.String_Elements.Table (Options).Next;
1507 end loop;
1508 end;
1509 end loop;
1511 return Linker_Options_Buffer (1 .. Last_Linker_Option);
1512 end Linker_Options_Switches;
1514 -----------
1515 -- Mains --
1516 -----------
1518 package body Mains is
1520 package Names is new Table.Table
1521 (Table_Component_Type => Main_Info,
1522 Table_Index_Type => Integer,
1523 Table_Low_Bound => 1,
1524 Table_Initial => 10,
1525 Table_Increment => 100,
1526 Table_Name => "Makeutl.Mains.Names");
1527 -- The table that stores the mains
1529 Current : Natural := 0;
1530 -- The index of the last main retrieved from the table
1532 Count_Of_Mains_With_No_Tree : Natural := 0;
1533 -- Number of main units for which we do not know the project tree
1535 --------------
1536 -- Add_Main --
1537 --------------
1539 procedure Add_Main
1540 (Name : String;
1541 Index : Int := 0;
1542 Location : Source_Ptr := No_Location;
1543 Project : Project_Id := No_Project;
1544 Tree : Project_Tree_Ref := null)
1546 begin
1547 if Current_Verbosity = High then
1548 Debug_Output ("Add_Main """ & Name & """ " & Index'Img
1549 & " with_tree? "
1550 & Boolean'Image (Tree /= null));
1551 end if;
1553 Name_Len := 0;
1554 Add_Str_To_Name_Buffer (Name);
1555 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
1557 Names.Increment_Last;
1558 Names.Table (Names.Last) :=
1559 (Name_Find, Index, Location, No_Source, Project, Tree);
1561 if Tree /= null then
1562 Builder_Data (Tree).Number_Of_Mains :=
1563 Builder_Data (Tree).Number_Of_Mains + 1;
1565 else
1566 Mains.Count_Of_Mains_With_No_Tree :=
1567 Mains.Count_Of_Mains_With_No_Tree + 1;
1568 end if;
1569 end Add_Main;
1571 --------------------
1572 -- Complete_Mains --
1573 --------------------
1575 procedure Complete_Mains
1576 (Flags : Processing_Flags;
1577 Root_Project : Project_Id;
1578 Project_Tree : Project_Tree_Ref)
1580 procedure Do_Complete (Project : Project_Id; Tree : Project_Tree_Ref);
1581 -- Check the mains for this specific project
1583 procedure Complete_All is new For_Project_And_Aggregated
1584 (Do_Complete);
1586 procedure Add_Multi_Unit_Sources
1587 (Tree : Project_Tree_Ref;
1588 Source : Prj.Source_Id);
1589 -- Add all units from the same file as the multi-unit Source
1591 function Find_File_Add_Extension
1592 (Tree : Project_Tree_Ref;
1593 Base_Main : String) return Prj.Source_Id;
1594 -- Search for Main in the project, adding body or spec extensions
1596 ----------------------------
1597 -- Add_Multi_Unit_Sources --
1598 ----------------------------
1600 procedure Add_Multi_Unit_Sources
1601 (Tree : Project_Tree_Ref;
1602 Source : Prj.Source_Id)
1604 Iter : Source_Iterator;
1605 Src : Prj.Source_Id;
1607 begin
1608 Debug_Output
1609 ("found multi-unit source file in project", Source.Project.Name);
1611 Iter := For_Each_Source
1612 (In_Tree => Tree, Project => Source.Project);
1614 while Element (Iter) /= No_Source loop
1615 Src := Element (Iter);
1617 if Src.File = Source.File
1618 and then Src.Index /= Source.Index
1619 then
1620 if Src.File = Source.File then
1621 Debug_Output
1622 ("add main in project, index=" & Src.Index'Img);
1623 end if;
1625 Names.Increment_Last;
1626 Names.Table (Names.Last) :=
1627 (File => Src.File,
1628 Index => Src.Index,
1629 Location => No_Location,
1630 Source => Src,
1631 Project => Src.Project,
1632 Tree => Tree);
1634 Builder_Data (Tree).Number_Of_Mains :=
1635 Builder_Data (Tree).Number_Of_Mains + 1;
1636 end if;
1638 Next (Iter);
1639 end loop;
1640 end Add_Multi_Unit_Sources;
1642 -----------------------------
1643 -- Find_File_Add_Extension --
1644 -----------------------------
1646 function Find_File_Add_Extension
1647 (Tree : Project_Tree_Ref;
1648 Base_Main : String) return Prj.Source_Id
1650 Spec_Source : Prj.Source_Id := No_Source;
1651 Source : Prj.Source_Id;
1652 Iter : Source_Iterator;
1653 Suffix : File_Name_Type;
1655 begin
1656 Source := No_Source;
1657 Iter := For_Each_Source (Tree); -- In all projects
1658 loop
1659 Source := Prj.Element (Iter);
1660 exit when Source = No_Source;
1662 if Source.Kind = Impl then
1663 Get_Name_String (Source.File);
1665 if Name_Len > Base_Main'Length
1666 and then Name_Buffer (1 .. Base_Main'Length) = Base_Main
1667 then
1668 Suffix :=
1669 Source.Language.Config.Naming_Data.Body_Suffix;
1671 if Suffix /= No_File then
1672 declare
1673 Suffix_Str : String := Get_Name_String (Suffix);
1674 begin
1675 Canonical_Case_File_Name (Suffix_Str);
1676 exit when
1677 Name_Buffer (Base_Main'Length + 1 .. Name_Len) =
1678 Suffix_Str;
1679 end;
1680 end if;
1681 end if;
1683 elsif Source.Kind = Spec
1684 and then Source.Language.Config.Kind = Unit_Based
1685 then
1686 -- An Ada spec needs to be taken into account unless there
1687 -- is also a body. So we delay the decision for them.
1689 Get_Name_String (Source.File);
1691 if Name_Len > Base_Main'Length
1692 and then Name_Buffer (1 .. Base_Main'Length) = Base_Main
1693 then
1694 Suffix := Source.Language.Config.Naming_Data.Spec_Suffix;
1696 if Suffix /= No_File then
1697 declare
1698 Suffix_Str : String := Get_Name_String (Suffix);
1700 begin
1701 Canonical_Case_File_Name (Suffix_Str);
1703 if Name_Buffer (Base_Main'Length + 1 .. Name_Len) =
1704 Suffix_Str
1705 then
1706 Spec_Source := Source;
1707 end if;
1708 end;
1709 end if;
1710 end if;
1711 end if;
1713 Next (Iter);
1714 end loop;
1716 if Source = No_Source then
1717 Source := Spec_Source;
1718 end if;
1720 return Source;
1721 end Find_File_Add_Extension;
1723 -----------------
1724 -- Do_Complete --
1725 -----------------
1727 procedure Do_Complete
1728 (Project : Project_Id; Tree : Project_Tree_Ref)
1730 J : Integer;
1732 begin
1733 if Mains.Number_Of_Mains (Tree) > 0
1734 or else Mains.Count_Of_Mains_With_No_Tree > 0
1735 then
1736 -- Traverse in reverse order, since in the case of multi-unit
1737 -- files we will be adding extra files at the end, and there's
1738 -- no need to process them in turn.
1740 J := Names.Last;
1741 Main_Loop : loop
1742 declare
1743 File : Main_Info := Names.Table (J);
1744 Main_Id : File_Name_Type := File.File;
1745 Main : constant String :=
1746 Get_Name_String (Main_Id);
1747 Base : constant String := Base_Name (Main);
1748 Source : Prj.Source_Id := No_Source;
1749 Is_Absolute : Boolean := False;
1751 begin
1752 if Base /= Main then
1753 Is_Absolute := True;
1755 if Is_Absolute_Path (Main) then
1756 Main_Id := Create_Name (Base);
1758 -- Not an absolute path
1760 else
1761 -- Always resolve links here, so that users can be
1762 -- specify any name on the command line. If the
1763 -- project itself uses links, the user will be
1764 -- using -eL anyway, and thus files are also stored
1765 -- with resolved names.
1767 declare
1768 Absolute : constant String :=
1769 Normalize_Pathname
1770 (Name => Main,
1771 Directory => "",
1772 Resolve_Links => True,
1773 Case_Sensitive => False);
1774 begin
1775 File.File := Create_Name (Absolute);
1776 Main_Id := Create_Name (Base);
1777 end;
1778 end if;
1779 end if;
1781 -- If no project or tree was specified for the main, it
1782 -- came from the command line.
1783 -- Note that the assignments below will not modify inside
1784 -- the table itself.
1786 if File.Project = null then
1787 File.Project := Project;
1788 end if;
1790 if File.Tree = null then
1791 File.Tree := Tree;
1792 end if;
1794 if File.Source = null then
1795 if Current_Verbosity = High then
1796 Debug_Output
1797 ("search for main """ & Main
1798 & '"' & File.Index'Img & " in "
1799 & Get_Name_String (Debug_Name (File.Tree))
1800 & ", project", Project.Name);
1801 end if;
1803 -- First, look for the main as specified. We need to
1804 -- search for the base name though, and if needed
1805 -- check later that we found the correct file.
1807 declare
1808 Sources : constant Source_Ids :=
1809 Find_All_Sources
1810 (In_Tree => File.Tree,
1811 Project => File.Project,
1812 Base_Name => Main_Id,
1813 Index => File.Index,
1814 In_Imported_Only => True);
1816 begin
1817 if Is_Absolute then
1818 for J in Sources'Range loop
1819 if File_Name_Type (Sources (J).Path.Name) =
1820 File.File
1821 then
1822 Source := Sources (J);
1823 exit;
1824 end if;
1825 end loop;
1827 elsif Sources'Length > 1 then
1829 -- This is only allowed if the units are from
1830 -- the same multi-unit source file.
1832 Source := Sources (1);
1834 for J in 2 .. Sources'Last loop
1835 if Sources (J).Path /= Source.Path
1836 or else Sources (J).Index = Source.Index
1837 then
1838 Error_Msg_File_1 := Main_Id;
1839 Prj.Err.Error_Msg
1840 (Flags, "several main sources {",
1841 No_Location, File.Project);
1842 exit Main_Loop;
1843 end if;
1844 end loop;
1846 elsif Sources'Length = 1 then
1847 Source := Sources (Sources'First);
1848 end if;
1849 end;
1851 if Source = No_Source then
1852 Source := Find_File_Add_Extension
1853 (File.Tree, Get_Name_String (Main_Id));
1854 end if;
1856 if Is_Absolute
1857 and then Source /= No_Source
1858 and then
1859 File_Name_Type (Source.Path.Name) /= File.File
1860 then
1861 Debug_Output
1862 ("Found a non-matching file",
1863 Name_Id (Source.Path.Display_Name));
1864 Source := No_Source;
1865 end if;
1867 if Source /= No_Source then
1868 if not Is_Allowed_Language
1869 (Source.Language.Name)
1870 then
1871 -- Remove any main that is not in the list of
1872 -- restricted languages.
1874 Names.Table (J .. Names.Last - 1) :=
1875 Names.Table (J + 1 .. Names.Last);
1876 Names.Set_Last (Names.Last - 1);
1878 else
1879 -- If we have found a multi-unit source file but
1880 -- did not specify an index initially, we'll
1881 -- need to compile all the units from the same
1882 -- source file.
1884 if Source.Index /= 0 and then File.Index = 0 then
1885 Add_Multi_Unit_Sources (File.Tree, Source);
1886 end if;
1888 -- Now update the original Main, otherwise it
1889 -- will be reported as not found.
1891 Debug_Output
1892 ("found main in project", Source.Project.Name);
1893 Names.Table (J).File := Source.File;
1894 Names.Table (J).Project := Source.Project;
1896 if Names.Table (J).Tree = null then
1897 Names.Table (J).Tree := File.Tree;
1899 Builder_Data (File.Tree).Number_Of_Mains :=
1900 Builder_Data (File.Tree).Number_Of_Mains
1901 + 1;
1902 Mains.Count_Of_Mains_With_No_Tree :=
1903 Mains.Count_Of_Mains_With_No_Tree - 1;
1904 end if;
1906 Names.Table (J).Source := Source;
1907 Names.Table (J).Index := Source.Index;
1908 end if;
1910 elsif File.Location /= No_Location then
1912 -- If the main is declared in package Builder of
1913 -- the main project, report an error. If the main
1914 -- is on the command line, it may be a main from
1915 -- another project, so do nothing: if the main does
1916 -- not exist in another project, an error will be
1917 -- reported later.
1919 Error_Msg_File_1 := Main_Id;
1920 Error_Msg_Name_1 := File.Project.Name;
1921 Prj.Err.Error_Msg
1922 (Flags, "{ is not a source of project %%",
1923 File.Location, File.Project);
1924 end if;
1925 end if;
1926 end;
1928 J := J - 1;
1929 exit Main_Loop when J < Names.First;
1930 end loop Main_Loop;
1931 end if;
1933 if Total_Errors_Detected > 0 then
1934 Fail_Program (Tree, "problems with main sources");
1935 end if;
1936 end Do_Complete;
1938 -- Start of processing for Complete_Mains
1940 begin
1941 Complete_All (Root_Project, Project_Tree);
1943 if Mains.Count_Of_Mains_With_No_Tree > 0 then
1944 for J in Names.First .. Names.Last loop
1945 if Names.Table (J).Source = No_Source then
1946 Fail_Program
1947 (Project_Tree, '"' & Get_Name_String (Names.Table (J).File)
1948 & """ is not a source of any project");
1949 end if;
1950 end loop;
1951 end if;
1952 end Complete_Mains;
1954 ------------
1955 -- Delete --
1956 ------------
1958 procedure Delete is
1959 begin
1960 Names.Set_Last (0);
1961 Mains.Reset;
1962 end Delete;
1964 -----------------------
1965 -- Fill_From_Project --
1966 -----------------------
1968 procedure Fill_From_Project
1969 (Root_Project : Project_Id;
1970 Project_Tree : Project_Tree_Ref)
1972 procedure Add_Mains_From_Project
1973 (Project : Project_Id;
1974 Tree : Project_Tree_Ref);
1975 -- Add the main units from this project into Mains.
1976 -- This takes into account the aggregated projects
1978 ----------------------------
1979 -- Add_Mains_From_Project --
1980 ----------------------------
1982 procedure Add_Mains_From_Project
1983 (Project : Project_Id;
1984 Tree : Project_Tree_Ref)
1986 List : String_List_Id;
1987 Element : String_Element;
1989 begin
1990 if Number_Of_Mains (Tree) = 0
1991 and then Mains.Count_Of_Mains_With_No_Tree = 0
1992 then
1993 Debug_Output ("Add_Mains_From_Project", Project.Name);
1994 List := Project.Mains;
1996 if List /= Prj.Nil_String then
1998 -- The attribute Main is not an empty list. Get the mains in
1999 -- the list.
2001 while List /= Prj.Nil_String loop
2002 Element := Tree.Shared.String_Elements.Table (List);
2003 Debug_Output ("Add_Main", Element.Value);
2005 if Project.Library then
2006 Fail_Program
2007 (Tree,
2008 "cannot specify a main program "
2009 & "for a library project file");
2010 end if;
2012 Add_Main (Name => Get_Name_String (Element.Value),
2013 Index => Element.Index,
2014 Location => Element.Location,
2015 Project => Project,
2016 Tree => Tree);
2017 List := Element.Next;
2018 end loop;
2019 end if;
2020 end if;
2022 if Total_Errors_Detected > 0 then
2023 Fail_Program (Tree, "problems with main sources");
2024 end if;
2025 end Add_Mains_From_Project;
2027 procedure Fill_All is new For_Project_And_Aggregated
2028 (Add_Mains_From_Project);
2030 -- Start of processing for Fill_From_Project
2032 begin
2033 Fill_All (Root_Project, Project_Tree);
2034 end Fill_From_Project;
2036 ---------------
2037 -- Next_Main --
2038 ---------------
2040 function Next_Main return String is
2041 Info : constant Main_Info := Next_Main;
2042 begin
2043 if Info = No_Main_Info then
2044 return "";
2045 else
2046 return Get_Name_String (Info.File);
2047 end if;
2048 end Next_Main;
2050 function Next_Main return Main_Info is
2051 begin
2052 if Current >= Names.Last then
2053 return No_Main_Info;
2054 else
2055 Current := Current + 1;
2057 -- If not using projects, and in the gnatmake case, the main file
2058 -- may have not have the extension. Try ".adb" first then ".ads"
2060 if Names.Table (Current).Project = No_Project then
2061 declare
2062 Orig_Main : constant File_Name_Type :=
2063 Names.Table (Current).File;
2064 Current_Main : File_Name_Type;
2066 begin
2067 if Strip_Suffix (Orig_Main) = Orig_Main then
2068 Get_Name_String (Orig_Main);
2069 Add_Str_To_Name_Buffer (".adb");
2070 Current_Main := Name_Find;
2072 if Full_Source_Name (Current_Main) = No_File then
2073 Get_Name_String (Orig_Main);
2074 Add_Str_To_Name_Buffer (".ads");
2075 Current_Main := Name_Find;
2077 if Full_Source_Name (Current_Main) /= No_File then
2078 Names.Table (Current).File := Current_Main;
2079 end if;
2081 else
2082 Names.Table (Current).File := Current_Main;
2083 end if;
2084 end if;
2085 end;
2086 end if;
2088 return Names.Table (Current);
2089 end if;
2090 end Next_Main;
2092 ---------------------
2093 -- Number_Of_Mains --
2094 ---------------------
2096 function Number_Of_Mains (Tree : Project_Tree_Ref) return Natural is
2097 begin
2098 if Tree = null then
2099 return Names.Last;
2100 else
2101 return Builder_Data (Tree).Number_Of_Mains;
2102 end if;
2103 end Number_Of_Mains;
2105 -----------
2106 -- Reset --
2107 -----------
2109 procedure Reset is
2110 begin
2111 Current := 0;
2112 end Reset;
2114 --------------------------
2115 -- Set_Multi_Unit_Index --
2116 --------------------------
2118 procedure Set_Multi_Unit_Index
2119 (Project_Tree : Project_Tree_Ref := null;
2120 Index : Int := 0)
2122 begin
2123 if Index /= 0 then
2124 if Names.Last = 0 then
2125 Fail_Program
2126 (Project_Tree,
2127 "cannot specify a multi-unit index but no main "
2128 & "on the command line");
2130 elsif Names.Last > 1 then
2131 Fail_Program
2132 (Project_Tree,
2133 "cannot specify several mains with a multi-unit index");
2135 else
2136 Names.Table (Names.Last).Index := Index;
2137 end if;
2138 end if;
2139 end Set_Multi_Unit_Index;
2141 end Mains;
2143 -----------------------
2144 -- Path_Or_File_Name --
2145 -----------------------
2147 function Path_Or_File_Name (Path : Path_Name_Type) return String is
2148 Path_Name : constant String := Get_Name_String (Path);
2149 begin
2150 if Debug.Debug_Flag_F then
2151 return File_Name (Path_Name);
2152 else
2153 return Path_Name;
2154 end if;
2155 end Path_Or_File_Name;
2157 -------------------
2158 -- Unit_Index_Of --
2159 -------------------
2161 function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
2162 Start : Natural;
2163 Finish : Natural;
2164 Result : Int := 0;
2166 begin
2167 Get_Name_String (ALI_File);
2169 -- First, find the last dot
2171 Finish := Name_Len;
2173 while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
2174 Finish := Finish - 1;
2175 end loop;
2177 if Finish = 1 then
2178 return 0;
2179 end if;
2181 -- Now check that the dot is preceded by digits
2183 Start := Finish;
2184 Finish := Finish - 1;
2185 while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
2186 Start := Start - 1;
2187 end loop;
2189 -- If there are no digits, or if the digits are not preceded by the
2190 -- character that precedes a unit index, this is not the ALI file of
2191 -- a unit in a multi-unit source.
2193 if Start > Finish
2194 or else Start = 1
2195 or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
2196 then
2197 return 0;
2198 end if;
2200 -- Build the index from the digit(s)
2202 while Start <= Finish loop
2203 Result := Result * 10 +
2204 Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');
2205 Start := Start + 1;
2206 end loop;
2208 return Result;
2209 end Unit_Index_Of;
2211 -----------------
2212 -- Verbose_Msg --
2213 -----------------
2215 procedure Verbose_Msg
2216 (N1 : Name_Id;
2217 S1 : String;
2218 N2 : Name_Id := No_Name;
2219 S2 : String := "";
2220 Prefix : String := " -> ";
2221 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
2223 begin
2224 if not Opt.Verbose_Mode
2225 or else Minimum_Verbosity > Opt.Verbosity_Level
2226 then
2227 return;
2228 end if;
2230 Write_Str (Prefix);
2231 Write_Str ("""");
2232 Write_Name (N1);
2233 Write_Str (""" ");
2234 Write_Str (S1);
2236 if N2 /= No_Name then
2237 Write_Str (" """);
2238 Write_Name (N2);
2239 Write_Str (""" ");
2240 end if;
2242 Write_Str (S2);
2243 Write_Eol;
2244 end Verbose_Msg;
2246 procedure Verbose_Msg
2247 (N1 : File_Name_Type;
2248 S1 : String;
2249 N2 : File_Name_Type := No_File;
2250 S2 : String := "";
2251 Prefix : String := " -> ";
2252 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
2254 begin
2255 Verbose_Msg
2256 (Name_Id (N1), S1, Name_Id (N2), S2, Prefix, Minimum_Verbosity);
2257 end Verbose_Msg;
2259 -----------
2260 -- Queue --
2261 -----------
2263 package body Queue is
2265 type Q_Record is record
2266 Info : Source_Info;
2267 Processed : Boolean;
2268 end record;
2270 package Q is new Table.Table
2271 (Table_Component_Type => Q_Record,
2272 Table_Index_Type => Natural,
2273 Table_Low_Bound => 1,
2274 Table_Initial => 1000,
2275 Table_Increment => 100,
2276 Table_Name => "Makeutl.Queue.Q");
2277 -- This is the actual Queue
2279 package Busy_Obj_Dirs is new GNAT.HTable.Simple_HTable
2280 (Header_Num => Prj.Header_Num,
2281 Element => Boolean,
2282 No_Element => False,
2283 Key => Path_Name_Type,
2284 Hash => Hash,
2285 Equal => "=");
2287 type Mark_Key is record
2288 File : File_Name_Type;
2289 Index : Int;
2290 end record;
2291 -- Identify either a mono-unit source (when Index = 0) or a specific
2292 -- unit (index = 1's origin index of unit) in a multi-unit source.
2294 Max_Mask_Num : constant := 2048;
2295 subtype Mark_Num is Union_Id range 0 .. Max_Mask_Num - 1;
2297 function Hash (Key : Mark_Key) return Mark_Num;
2299 package Marks is new GNAT.HTable.Simple_HTable
2300 (Header_Num => Mark_Num,
2301 Element => Boolean,
2302 No_Element => False,
2303 Key => Mark_Key,
2304 Hash => Hash,
2305 Equal => "=");
2306 -- A hash table to keep tracks of the marked units.
2307 -- These are the units that have already been processed, when using the
2308 -- gnatmake format. When using the gprbuild format, we can directly
2309 -- store in the source_id whether the file has already been processed.
2311 procedure Mark (Source_File : File_Name_Type; Index : Int := 0);
2312 -- Mark a unit, identified by its source file and, when Index is not 0,
2313 -- the index of the unit in the source file. Marking is used to signal
2314 -- that the unit has already been inserted in the Q.
2316 function Is_Marked
2317 (Source_File : File_Name_Type;
2318 Index : Int := 0) return Boolean;
2319 -- Returns True if the unit was previously marked
2321 Q_Processed : Natural := 0;
2322 Q_Initialized : Boolean := False;
2324 Q_First : Natural := 1;
2325 -- Points to the first valid element in the queue
2327 One_Queue_Per_Obj_Dir : Boolean := False;
2328 -- See parameter to Initialize
2330 function Available_Obj_Dir (S : Source_Info) return Boolean;
2331 -- Whether the object directory for S is available for a build
2333 procedure Debug_Display (S : Source_Info);
2334 -- A debug display for S
2336 function Was_Processed (S : Source_Info) return Boolean;
2337 -- Whether S has already been processed. This marks the source as
2338 -- processed, if it hasn't already been processed.
2340 function Insert_No_Roots (Source : Source_Info) return Boolean;
2341 -- Insert Source, but do not look for its roots (see doc for Insert)
2343 -------------------
2344 -- Was_Processed --
2345 -------------------
2347 function Was_Processed (S : Source_Info) return Boolean is
2348 begin
2349 case S.Format is
2350 when Format_Gprbuild =>
2351 if S.Id.In_The_Queue then
2352 return True;
2353 end if;
2355 S.Id.In_The_Queue := True;
2357 when Format_Gnatmake =>
2358 if Is_Marked (S.File, S.Index) then
2359 return True;
2360 end if;
2362 Mark (S.File, Index => S.Index);
2363 end case;
2365 return False;
2366 end Was_Processed;
2368 -----------------------
2369 -- Available_Obj_Dir --
2370 -----------------------
2372 function Available_Obj_Dir (S : Source_Info) return Boolean is
2373 begin
2374 case S.Format is
2375 when Format_Gprbuild =>
2376 return not Busy_Obj_Dirs.Get
2377 (S.Id.Project.Object_Directory.Name);
2379 when Format_Gnatmake =>
2380 return S.Project = No_Project
2381 or else
2382 not Busy_Obj_Dirs.Get (S.Project.Object_Directory.Name);
2383 end case;
2384 end Available_Obj_Dir;
2386 -------------------
2387 -- Debug_Display --
2388 -------------------
2390 procedure Debug_Display (S : Source_Info) is
2391 begin
2392 case S.Format is
2393 when Format_Gprbuild =>
2394 Write_Name (S.Id.File);
2396 if S.Id.Index /= 0 then
2397 Write_Str (", ");
2398 Write_Int (S.Id.Index);
2399 end if;
2401 when Format_Gnatmake =>
2402 Write_Name (S.File);
2404 if S.Index /= 0 then
2405 Write_Str (", ");
2406 Write_Int (S.Index);
2407 end if;
2408 end case;
2409 end Debug_Display;
2411 ----------
2412 -- Hash --
2413 ----------
2415 function Hash (Key : Mark_Key) return Mark_Num is
2416 begin
2417 return Union_Id (Key.File) mod Max_Mask_Num;
2418 end Hash;
2420 ---------------
2421 -- Is_Marked --
2422 ---------------
2424 function Is_Marked
2425 (Source_File : File_Name_Type;
2426 Index : Int := 0) return Boolean
2428 begin
2429 return Marks.Get (K => (File => Source_File, Index => Index));
2430 end Is_Marked;
2432 ----------
2433 -- Mark --
2434 ----------
2436 procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
2437 begin
2438 Marks.Set (K => (File => Source_File, Index => Index), E => True);
2439 end Mark;
2441 -------------
2442 -- Extract --
2443 -------------
2445 procedure Extract
2446 (Found : out Boolean;
2447 Source : out Source_Info)
2449 begin
2450 Found := False;
2452 if One_Queue_Per_Obj_Dir then
2453 for J in Q_First .. Q.Last loop
2454 if not Q.Table (J).Processed
2455 and then Available_Obj_Dir (Q.Table (J).Info)
2456 then
2457 Found := True;
2458 Source := Q.Table (J).Info;
2459 Q.Table (J).Processed := True;
2461 if J = Q_First then
2462 while Q_First <= Q.Last
2463 and then Q.Table (Q_First).Processed
2464 loop
2465 Q_First := Q_First + 1;
2466 end loop;
2467 end if;
2469 exit;
2470 end if;
2471 end loop;
2473 elsif Q_First <= Q.Last then
2474 Source := Q.Table (Q_First).Info;
2475 Q.Table (Q_First).Processed := True;
2476 Q_First := Q_First + 1;
2477 Found := True;
2478 end if;
2480 if Found then
2481 Q_Processed := Q_Processed + 1;
2482 end if;
2484 if Found and then Debug.Debug_Flag_Q then
2485 Write_Str (" Q := Q - [ ");
2486 Debug_Display (Source);
2487 Write_Str (" ]");
2488 Write_Eol;
2490 Write_Str (" Q_First =");
2491 Write_Int (Int (Q_First));
2492 Write_Eol;
2494 Write_Str (" Q.Last =");
2495 Write_Int (Int (Q.Last));
2496 Write_Eol;
2497 end if;
2498 end Extract;
2500 ---------------
2501 -- Processed --
2502 ---------------
2504 function Processed return Natural is
2505 begin
2506 return Q_Processed;
2507 end Processed;
2509 ----------------
2510 -- Initialize --
2511 ----------------
2513 procedure Initialize
2514 (Queue_Per_Obj_Dir : Boolean;
2515 Force : Boolean := False)
2517 begin
2518 if Force or else not Q_Initialized then
2519 Q_Initialized := True;
2521 for J in 1 .. Q.Last loop
2522 case Q.Table (J).Info.Format is
2523 when Format_Gprbuild =>
2524 Q.Table (J).Info.Id.In_The_Queue := False;
2525 when Format_Gnatmake =>
2526 null;
2527 end case;
2528 end loop;
2530 Q.Init;
2531 Q_Processed := 0;
2532 Q_First := 1;
2533 One_Queue_Per_Obj_Dir := Queue_Per_Obj_Dir;
2534 end if;
2535 end Initialize;
2537 ---------------------
2538 -- Insert_No_Roots --
2539 ---------------------
2541 function Insert_No_Roots (Source : Source_Info) return Boolean is
2542 begin
2543 pragma Assert
2544 (Source.Format = Format_Gnatmake or else Source.Id /= No_Source);
2546 -- Only insert in the Q if it is not already done, to avoid
2547 -- simultaneous compilations if -jnnn is used.
2549 if Was_Processed (Source) then
2550 return False;
2551 end if;
2553 -- For gprbuild, check if a source has already been inserted in the
2554 -- queue from the same project in a different project tree.
2556 if Source.Format = Format_Gprbuild then
2557 for J in 1 .. Q.Last loop
2558 if Source.Id.Path.Name = Q.Table (J).Info.Id.Path.Name
2559 and then Source.Id.Index = Q.Table (J).Info.Id.Index
2560 and then Source.Id.Project.Path.Name =
2561 Q.Table (J).Info.Id.Project.Path.Name
2562 then
2563 -- No need to insert this source in the queue, but still
2564 -- return True as we may need to insert its roots.
2566 return True;
2567 end if;
2568 end loop;
2569 end if;
2571 if Current_Verbosity = High then
2572 Write_Str ("Adding """);
2573 Debug_Display (Source);
2574 Write_Line (""" to the queue");
2575 end if;
2577 Q.Append (New_Val => (Info => Source, Processed => False));
2579 if Debug.Debug_Flag_Q then
2580 Write_Str (" Q := Q + [ ");
2581 Debug_Display (Source);
2582 Write_Str (" ] ");
2583 Write_Eol;
2585 Write_Str (" Q_First =");
2586 Write_Int (Int (Q_First));
2587 Write_Eol;
2589 Write_Str (" Q.Last =");
2590 Write_Int (Int (Q.Last));
2591 Write_Eol;
2592 end if;
2594 return True;
2595 end Insert_No_Roots;
2597 ------------
2598 -- Insert --
2599 ------------
2601 function Insert
2602 (Source : Source_Info;
2603 With_Roots : Boolean := False) return Boolean
2605 Root_Arr : Array_Element_Id;
2606 Roots : Variable_Value;
2607 List : String_List_Id;
2608 Elem : String_Element;
2609 Unit_Name : Name_Id;
2610 Pat_Root : Boolean;
2611 Root_Pattern : Regexp;
2612 Root_Found : Boolean;
2613 Roots_Found : Boolean;
2614 Root_Source : Prj.Source_Id;
2615 Iter : Source_Iterator;
2617 Dummy : Boolean;
2619 begin
2620 if not Insert_No_Roots (Source) then
2622 -- Was already in the queue
2624 return False;
2625 end if;
2627 if With_Roots and then Source.Format = Format_Gprbuild then
2628 Debug_Output ("looking for roots of", Name_Id (Source.Id.File));
2630 Root_Arr :=
2631 Prj.Util.Value_Of
2632 (Name => Name_Roots,
2633 In_Arrays => Source.Id.Project.Decl.Arrays,
2634 Shared => Source.Tree.Shared);
2636 Roots :=
2637 Prj.Util.Value_Of
2638 (Index => Name_Id (Source.Id.File),
2639 Src_Index => 0,
2640 In_Array => Root_Arr,
2641 Shared => Source.Tree.Shared);
2643 -- If there is no roots for the specific main, try the language
2645 if Roots = Nil_Variable_Value then
2646 Roots :=
2647 Prj.Util.Value_Of
2648 (Index => Source.Id.Language.Name,
2649 Src_Index => 0,
2650 In_Array => Root_Arr,
2651 Shared => Source.Tree.Shared,
2652 Force_Lower_Case_Index => True);
2653 end if;
2655 -- Then try "*"
2657 if Roots = Nil_Variable_Value then
2658 Name_Len := 1;
2659 Name_Buffer (1) := '*';
2661 Roots :=
2662 Prj.Util.Value_Of
2663 (Index => Name_Find,
2664 Src_Index => 0,
2665 In_Array => Root_Arr,
2666 Shared => Source.Tree.Shared,
2667 Force_Lower_Case_Index => True);
2668 end if;
2670 if Roots = Nil_Variable_Value then
2671 Debug_Output (" -> no roots declared");
2673 else
2674 List := Roots.Values;
2676 Pattern_Loop :
2677 while List /= Nil_String loop
2678 Elem := Source.Tree.Shared.String_Elements.Table (List);
2679 Get_Name_String (Elem.Value);
2680 To_Lower (Name_Buffer (1 .. Name_Len));
2681 Unit_Name := Name_Find;
2683 -- Check if it is a unit name or a pattern
2685 Pat_Root := False;
2687 for J in 1 .. Name_Len loop
2688 if Name_Buffer (J) not in 'a' .. 'z' and then
2689 Name_Buffer (J) not in '0' .. '9' and then
2690 Name_Buffer (J) /= '_' and then
2691 Name_Buffer (J) /= '.'
2692 then
2693 Pat_Root := True;
2694 exit;
2695 end if;
2696 end loop;
2698 if Pat_Root then
2699 begin
2700 Root_Pattern :=
2701 Compile
2702 (Pattern => Name_Buffer (1 .. Name_Len),
2703 Glob => True);
2705 exception
2706 when Error_In_Regexp =>
2707 Err_Vars.Error_Msg_Name_1 := Unit_Name;
2708 Errutil.Error_Msg
2709 ("invalid pattern %", Roots.Location);
2710 exit Pattern_Loop;
2711 end;
2712 end if;
2714 Roots_Found := False;
2715 Iter := For_Each_Source (Source.Tree);
2717 Source_Loop :
2718 loop
2719 Root_Source := Prj.Element (Iter);
2720 exit Source_Loop when Root_Source = No_Source;
2722 Root_Found := False;
2723 if Pat_Root then
2724 Root_Found := Root_Source.Unit /= No_Unit_Index
2725 and then Match
2726 (Get_Name_String (Root_Source.Unit.Name),
2727 Root_Pattern);
2729 else
2730 Root_Found :=
2731 Root_Source.Unit /= No_Unit_Index
2732 and then Root_Source.Unit.Name = Unit_Name;
2733 end if;
2735 if Root_Found then
2736 case Root_Source.Kind is
2737 when Impl =>
2738 null;
2740 when Spec =>
2741 Root_Found := Other_Part (Root_Source) = No_Source;
2743 when Sep =>
2744 Root_Found := False;
2745 end case;
2746 end if;
2748 if Root_Found then
2749 Roots_Found := True;
2750 Debug_Output
2751 (" -> ", Name_Id (Root_Source.Display_File));
2752 Dummy := Queue.Insert_No_Roots
2753 (Source => (Format => Format_Gprbuild,
2754 Tree => Source.Tree,
2755 Id => Root_Source,
2756 Closure => False));
2758 Initialize_Source_Record (Root_Source);
2760 if Other_Part (Root_Source) /= No_Source then
2761 Initialize_Source_Record (Other_Part (Root_Source));
2762 end if;
2764 -- Save the root for the binder
2766 Source.Id.Roots := new Source_Roots'
2767 (Root => Root_Source,
2768 Next => Source.Id.Roots);
2770 exit Source_Loop when not Pat_Root;
2771 end if;
2773 Next (Iter);
2774 end loop Source_Loop;
2776 if not Roots_Found then
2777 if Pat_Root then
2778 if not Quiet_Output then
2779 Error_Msg_Name_1 := Unit_Name;
2780 Errutil.Error_Msg
2781 ("?no unit matches pattern %", Roots.Location);
2782 end if;
2784 else
2785 Errutil.Error_Msg
2786 ("Unit " & Get_Name_String (Unit_Name)
2787 & " does not exist", Roots.Location);
2788 end if;
2789 end if;
2791 List := Elem.Next;
2792 end loop Pattern_Loop;
2793 end if;
2794 end if;
2796 return True;
2797 end Insert;
2799 ------------
2800 -- Insert --
2801 ------------
2803 procedure Insert
2804 (Source : Source_Info;
2805 With_Roots : Boolean := False)
2807 Discard : Boolean;
2808 begin
2809 Discard := Insert (Source, With_Roots);
2810 end Insert;
2812 --------------
2813 -- Is_Empty --
2814 --------------
2816 function Is_Empty return Boolean is
2817 begin
2818 return Q_Processed >= Q.Last;
2819 end Is_Empty;
2821 ------------------------
2822 -- Is_Virtually_Empty --
2823 ------------------------
2825 function Is_Virtually_Empty return Boolean is
2826 begin
2827 if One_Queue_Per_Obj_Dir then
2828 for J in Q_First .. Q.Last loop
2829 if not Q.Table (J).Processed
2830 and then Available_Obj_Dir (Q.Table (J).Info)
2831 then
2832 return False;
2833 end if;
2834 end loop;
2836 return True;
2838 else
2839 return Is_Empty;
2840 end if;
2841 end Is_Virtually_Empty;
2843 ----------------------
2844 -- Set_Obj_Dir_Busy --
2845 ----------------------
2847 procedure Set_Obj_Dir_Busy (Obj_Dir : Path_Name_Type) is
2848 begin
2849 if One_Queue_Per_Obj_Dir then
2850 Busy_Obj_Dirs.Set (Obj_Dir, True);
2851 end if;
2852 end Set_Obj_Dir_Busy;
2854 ----------------------
2855 -- Set_Obj_Dir_Free --
2856 ----------------------
2858 procedure Set_Obj_Dir_Free (Obj_Dir : Path_Name_Type) is
2859 begin
2860 if One_Queue_Per_Obj_Dir then
2861 Busy_Obj_Dirs.Set (Obj_Dir, False);
2862 end if;
2863 end Set_Obj_Dir_Free;
2865 ----------
2866 -- Size --
2867 ----------
2869 function Size return Natural is
2870 begin
2871 return Q.Last;
2872 end Size;
2874 -------------
2875 -- Element --
2876 -------------
2878 function Element (Rank : Positive) return File_Name_Type is
2879 begin
2880 if Rank <= Q.Last then
2881 case Q.Table (Rank).Info.Format is
2882 when Format_Gprbuild =>
2883 return Q.Table (Rank).Info.Id.File;
2884 when Format_Gnatmake =>
2885 return Q.Table (Rank).Info.File;
2886 end case;
2887 else
2888 return No_File;
2889 end if;
2890 end Element;
2892 ------------------
2893 -- Remove_Marks --
2894 ------------------
2896 procedure Remove_Marks is
2897 begin
2898 Marks.Reset;
2899 end Remove_Marks;
2901 ----------------------------
2902 -- Insert_Project_Sources --
2903 ----------------------------
2905 procedure Insert_Project_Sources
2906 (Project : Project_Id;
2907 Project_Tree : Project_Tree_Ref;
2908 All_Projects : Boolean;
2909 Unique_Compile : Boolean)
2912 procedure Do_Insert
2913 (Project : Project_Id;
2914 Tree : Project_Tree_Ref;
2915 Context : Project_Context);
2916 -- Local procedures must be commented ???
2918 ---------------
2919 -- Do_Insert --
2920 ---------------
2922 procedure Do_Insert
2923 (Project : Project_Id;
2924 Tree : Project_Tree_Ref;
2925 Context : Project_Context)
2927 Unit_Based : constant Boolean :=
2928 Unique_Compile
2929 or else not Builder_Data (Tree).Closure_Needed;
2930 -- When Unit_Based is True, we enqueue all compilable sources
2931 -- including the unit based (Ada) one. When Unit_Based is False,
2932 -- put the Ada sources only when they are in a library project.
2934 Iter : Source_Iterator;
2935 Source : Prj.Source_Id;
2936 OK : Boolean;
2937 Closure : Boolean;
2939 begin
2940 -- Nothing to do when "-u" was specified and some files were
2941 -- specified on the command line
2943 if Unique_Compile and then Mains.Number_Of_Mains (Tree) > 0 then
2944 return;
2945 end if;
2947 Iter := For_Each_Source (Tree);
2948 loop
2949 Source := Prj.Element (Iter);
2950 exit when Source = No_Source;
2952 if Is_Allowed_Language (Source.Language.Name)
2953 and then Is_Compilable (Source)
2954 and then (All_Projects
2955 or else Is_Extending (Project, Source.Project))
2956 and then not Source.Locally_Removed
2957 and then Source.Replaced_By = No_Source
2958 and then (not Source.Project.Externally_Built
2959 or else (Is_Extending (Project, Source.Project)
2960 and then not Project.Externally_Built))
2961 and then Source.Kind /= Sep
2962 and then Source.Path /= No_Path_Information
2963 then
2964 if Source.Kind = Impl
2965 or else (Source.Unit /= No_Unit_Index
2966 and then Source.Kind = Spec
2967 and then (Other_Part (Source) = No_Source
2968 or else
2969 Other_Part (Source).Locally_Removed))
2970 then
2971 if (Unit_Based
2972 or else Source.Unit = No_Unit_Index
2973 or else Source.Project.Library
2974 or else Context.In_Aggregate_Lib
2975 or else Project.Qualifier = Aggregate_Library)
2976 and then not Is_Subunit (Source)
2977 then
2978 OK := True;
2979 Closure := False;
2981 if Source.Unit /= No_Unit_Index
2982 and then
2983 (Source.Project.Library
2984 or else Project.Qualifier = Aggregate_Library
2985 or else Context.In_Aggregate_Lib)
2986 and then Source.Project.Standalone_Library /= No
2987 then
2988 -- Check if the unit is in the interface
2990 OK := False;
2992 declare
2993 List : String_List_Id;
2994 Element : String_Element;
2996 begin
2997 List := Source.Project.Lib_Interface_ALIs;
2998 while List /= Nil_String loop
2999 Element :=
3000 Project_Tree.Shared.String_Elements.Table
3001 (List);
3003 if Element.Value = Name_Id (Source.Dep_Name)
3004 then
3005 OK := True;
3006 Closure := True;
3007 exit;
3008 end if;
3010 List := Element.Next;
3011 end loop;
3012 end;
3013 end if;
3015 if OK then
3016 Queue.Insert
3017 (Source => (Format => Format_Gprbuild,
3018 Tree => Tree,
3019 Id => Source,
3020 Closure => Closure));
3021 end if;
3022 end if;
3023 end if;
3024 end if;
3026 Next (Iter);
3027 end loop;
3028 end Do_Insert;
3030 procedure Insert_All is
3031 new For_Project_And_Aggregated_Context (Do_Insert);
3033 begin
3034 Insert_All (Project, Project_Tree);
3035 end Insert_Project_Sources;
3037 -------------------------------
3038 -- Insert_Withed_Sources_For --
3039 -------------------------------
3041 procedure Insert_Withed_Sources_For
3042 (The_ALI : ALI.ALI_Id;
3043 Project_Tree : Project_Tree_Ref;
3044 Excluding_Shared_SALs : Boolean := False)
3046 Sfile : File_Name_Type;
3047 Afile : File_Name_Type;
3048 Src_Id : Prj.Source_Id;
3050 begin
3051 -- Insert in the queue the unmarked source files (i.e. those which
3052 -- have never been inserted in the queue and hence never considered).
3054 for J in ALI.ALIs.Table (The_ALI).First_Unit ..
3055 ALI.ALIs.Table (The_ALI).Last_Unit
3056 loop
3057 for K in ALI.Units.Table (J).First_With ..
3058 ALI.Units.Table (J).Last_With
3059 loop
3060 Sfile := ALI.Withs.Table (K).Sfile;
3062 -- Skip generics
3064 if Sfile /= No_File then
3065 Afile := ALI.Withs.Table (K).Afile;
3067 Src_Id := Source_Files_Htable.Get
3068 (Project_Tree.Source_Files_HT, Sfile);
3069 while Src_Id /= No_Source loop
3070 Initialize_Source_Record (Src_Id);
3072 if Is_Compilable (Src_Id)
3073 and then Src_Id.Dep_Name = Afile
3074 then
3075 case Src_Id.Kind is
3076 when Spec =>
3077 declare
3078 Bdy : constant Prj.Source_Id :=
3079 Other_Part (Src_Id);
3080 begin
3081 if Bdy /= No_Source
3082 and then not Bdy.Locally_Removed
3083 then
3084 Src_Id := Other_Part (Src_Id);
3085 end if;
3086 end;
3088 when Impl =>
3089 if Is_Subunit (Src_Id) then
3090 Src_Id := No_Source;
3091 end if;
3093 when Sep =>
3094 Src_Id := No_Source;
3095 end case;
3097 exit;
3098 end if;
3100 Src_Id := Src_Id.Next_With_File_Name;
3101 end loop;
3103 -- If Excluding_Shared_SALs is True, do not insert in the
3104 -- queue the sources of a shared Stand-Alone Library.
3106 if Src_Id /= No_Source
3107 and then (not Excluding_Shared_SALs
3108 or else Src_Id.Project.Standalone_Library = No
3109 or else Src_Id.Project.Library_Kind = Static)
3110 then
3111 Queue.Insert
3112 (Source => (Format => Format_Gprbuild,
3113 Tree => Project_Tree,
3114 Id => Src_Id,
3115 Closure => True));
3116 end if;
3117 end if;
3118 end loop;
3119 end loop;
3120 end Insert_Withed_Sources_For;
3122 end Queue;
3124 ----------
3125 -- Free --
3126 ----------
3128 procedure Free (Data : in out Builder_Project_Tree_Data) is
3129 procedure Unchecked_Free is new Ada.Unchecked_Deallocation
3130 (Binding_Data_Record, Binding_Data);
3132 TmpB, Binding : Binding_Data := Data.Binding;
3134 begin
3135 while Binding /= null loop
3136 TmpB := Binding.Next;
3137 Unchecked_Free (Binding);
3138 Binding := TmpB;
3139 end loop;
3140 end Free;
3142 ------------------
3143 -- Builder_Data --
3144 ------------------
3146 function Builder_Data
3147 (Tree : Project_Tree_Ref) return Builder_Data_Access
3149 begin
3150 if Tree.Appdata = null then
3151 Tree.Appdata := new Builder_Project_Tree_Data;
3152 end if;
3154 return Builder_Data_Access (Tree.Appdata);
3155 end Builder_Data;
3157 --------------------------------
3158 -- Compute_Compilation_Phases --
3159 --------------------------------
3161 procedure Compute_Compilation_Phases
3162 (Tree : Project_Tree_Ref;
3163 Root_Project : Project_Id;
3164 Option_Unique_Compile : Boolean := False; -- Was "-u" specified ?
3165 Option_Compile_Only : Boolean := False; -- Was "-c" specified ?
3166 Option_Bind_Only : Boolean := False;
3167 Option_Link_Only : Boolean := False)
3169 procedure Do_Compute (Project : Project_Id; Tree : Project_Tree_Ref);
3171 ----------------
3172 -- Do_Compute --
3173 ----------------
3175 procedure Do_Compute (Project : Project_Id; Tree : Project_Tree_Ref) is
3176 Data : constant Builder_Data_Access := Builder_Data (Tree);
3177 All_Phases : constant Boolean :=
3178 not Option_Compile_Only
3179 and then not Option_Bind_Only
3180 and then not Option_Link_Only;
3181 -- Whether the command line asked for all three phases. Depending on
3182 -- the project settings, we might still disable some of the phases.
3184 Has_Mains : constant Boolean := Data.Number_Of_Mains > 0;
3185 -- Whether there are some main units defined for this project tree
3186 -- (either from one of the projects, or from the command line)
3188 begin
3189 if Option_Unique_Compile then
3191 -- If -u or -U is specified on the command line, disregard any -c,
3192 -- -b or -l switch: only perform compilation.
3194 Data.Closure_Needed := False;
3195 Data.Need_Compilation := True;
3196 Data.Need_Binding := False;
3197 Data.Need_Linking := False;
3199 else
3200 Data.Closure_Needed :=
3201 Has_Mains
3202 or else (Root_Project.Library
3203 and then Root_Project.Standalone_Library /= No);
3204 Data.Need_Compilation := All_Phases or Option_Compile_Only;
3205 Data.Need_Binding := All_Phases or Option_Bind_Only;
3206 Data.Need_Linking := (All_Phases or Option_Link_Only)
3207 and Has_Mains;
3208 end if;
3210 if Current_Verbosity = High then
3211 Debug_Output ("compilation phases: "
3212 & " compile=" & Data.Need_Compilation'Img
3213 & " bind=" & Data.Need_Binding'Img
3214 & " link=" & Data.Need_Linking'Img
3215 & " closure=" & Data.Closure_Needed'Img
3216 & " mains=" & Data.Number_Of_Mains'Img,
3217 Project.Name);
3218 end if;
3219 end Do_Compute;
3221 procedure Compute_All is new For_Project_And_Aggregated (Do_Compute);
3223 begin
3224 Compute_All (Root_Project, Tree);
3225 end Compute_Compilation_Phases;
3227 ------------------------------
3228 -- Compute_Builder_Switches --
3229 ------------------------------
3231 procedure Compute_Builder_Switches
3232 (Project_Tree : Project_Tree_Ref;
3233 Env : in out Prj.Tree.Environment;
3234 Main_Project : Project_Id;
3235 Only_For_Lang : Name_Id := No_Name)
3237 Builder_Package : constant Package_Id :=
3238 Value_Of (Name_Builder, Main_Project.Decl.Packages,
3239 Project_Tree.Shared);
3241 Global_Compilation_Array : Array_Element_Id;
3242 Global_Compilation_Elem : Array_Element;
3243 Global_Compilation_Switches : Variable_Value;
3245 Default_Switches_Array : Array_Id;
3247 Builder_Switches_Lang : Name_Id := No_Name;
3249 List : String_List_Id;
3250 Element : String_Element;
3252 Index : Name_Id;
3253 Source : Prj.Source_Id;
3255 Lang : Name_Id := No_Name; -- language index for Switches
3256 Switches_For_Lang : Variable_Value := Nil_Variable_Value;
3257 -- Value of Builder'Default_Switches(lang)
3259 Name : Name_Id := No_Name; -- main file index for Switches
3260 Switches_For_Main : Variable_Value := Nil_Variable_Value;
3261 -- Switches for a specific main. When there are several mains, Name is
3262 -- set to No_Name, and Switches_For_Main might be left with an actual
3263 -- value (so that we can display a warning that it was ignored).
3265 Other_Switches : Variable_Value := Nil_Variable_Value;
3266 -- Value of Builder'Switches(others)
3268 Defaults : Variable_Value := Nil_Variable_Value;
3270 Switches : Variable_Value := Nil_Variable_Value;
3271 -- The computed builder switches
3273 Success : Boolean := False;
3274 begin
3275 if Builder_Package /= No_Package then
3276 Mains.Reset;
3278 -- If there is no main, and there is only one compilable language,
3279 -- use this language as the switches index.
3281 if Mains.Number_Of_Mains (Project_Tree) = 0 then
3282 if Only_For_Lang = No_Name then
3283 declare
3284 Language : Language_Ptr := Main_Project.Languages;
3286 begin
3287 while Language /= No_Language_Index loop
3288 if Language.Config.Compiler_Driver /= No_File
3289 and then Language.Config.Compiler_Driver /= Empty_File
3290 then
3291 if Lang /= No_Name then
3292 Lang := No_Name;
3293 exit;
3294 else
3295 Lang := Language.Name;
3296 end if;
3297 end if;
3298 Language := Language.Next;
3299 end loop;
3300 end;
3301 else
3302 Lang := Only_For_Lang;
3303 end if;
3305 else
3306 for Index in 1 .. Mains.Number_Of_Mains (Project_Tree) loop
3307 Source := Mains.Next_Main.Source;
3309 if Source /= No_Source then
3310 if Switches_For_Main = Nil_Variable_Value then
3311 Switches_For_Main := Value_Of
3312 (Name => Name_Id (Source.File),
3313 Attribute_Or_Array_Name => Name_Switches,
3314 In_Package => Builder_Package,
3315 Shared => Project_Tree.Shared,
3316 Force_Lower_Case_Index => False,
3317 Allow_Wildcards => True);
3319 -- If not found, try without extension.
3320 -- That's because gnatmake accepts truncated file names
3321 -- in Builder'Switches
3323 if Switches_For_Main = Nil_Variable_Value
3324 and then Source.Unit /= null
3325 then
3326 Switches_For_Main := Value_Of
3327 (Name => Source.Unit.Name,
3328 Attribute_Or_Array_Name => Name_Switches,
3329 In_Package => Builder_Package,
3330 Shared => Project_Tree.Shared,
3331 Force_Lower_Case_Index => False,
3332 Allow_Wildcards => True);
3333 end if;
3334 end if;
3336 if Index = 1 then
3337 Lang := Source.Language.Name;
3338 Name := Name_Id (Source.File);
3339 else
3340 Name := No_Name; -- Can't use main specific switches
3342 if Lang /= Source.Language.Name then
3343 Lang := No_Name;
3344 end if;
3345 end if;
3346 end if;
3347 end loop;
3348 end if;
3350 Global_Compilation_Array := Value_Of
3351 (Name => Name_Global_Compilation_Switches,
3352 In_Arrays => Project_Tree.Shared.Packages.Table
3353 (Builder_Package).Decl.Arrays,
3354 Shared => Project_Tree.Shared);
3356 Default_Switches_Array :=
3357 Project_Tree.Shared.Packages.Table (Builder_Package).Decl.Arrays;
3359 while Default_Switches_Array /= No_Array
3360 and then
3361 Project_Tree.Shared.Arrays.Table (Default_Switches_Array).Name /=
3362 Name_Default_Switches
3363 loop
3364 Default_Switches_Array :=
3365 Project_Tree.Shared.Arrays.Table (Default_Switches_Array).Next;
3366 end loop;
3368 if Global_Compilation_Array /= No_Array_Element
3369 and then Default_Switches_Array /= No_Array
3370 then
3371 Prj.Err.Error_Msg
3372 (Env.Flags,
3373 "Default_Switches forbidden in presence of "
3374 & "Global_Compilation_Switches. Use Switches instead.",
3375 Project_Tree.Shared.Arrays.Table
3376 (Default_Switches_Array).Location);
3377 Fail_Program
3378 (Project_Tree, "*** illegal combination of Builder attributes");
3379 end if;
3381 if Lang /= No_Name then
3382 Switches_For_Lang := Prj.Util.Value_Of
3383 (Name => Lang,
3384 Index => 0,
3385 Attribute_Or_Array_Name => Name_Switches,
3386 In_Package => Builder_Package,
3387 Shared => Project_Tree.Shared,
3388 Force_Lower_Case_Index => True);
3390 Defaults := Prj.Util.Value_Of
3391 (Name => Lang,
3392 Index => 0,
3393 Attribute_Or_Array_Name => Name_Default_Switches,
3394 In_Package => Builder_Package,
3395 Shared => Project_Tree.Shared,
3396 Force_Lower_Case_Index => True);
3397 end if;
3399 Other_Switches := Prj.Util.Value_Of
3400 (Name => All_Other_Names,
3401 Index => 0,
3402 Attribute_Or_Array_Name => Name_Switches,
3403 In_Package => Builder_Package,
3404 Shared => Project_Tree.Shared);
3406 if not Quiet_Output
3407 and then Mains.Number_Of_Mains (Project_Tree) > 1
3408 and then Switches_For_Main /= Nil_Variable_Value
3409 then
3410 -- More than one main, but we had main-specific switches that
3411 -- are ignored.
3413 if Switches_For_Lang /= Nil_Variable_Value then
3414 Write_Line
3415 ("Warning: using Builder'Switches("""
3416 & Get_Name_String (Lang)
3417 & """), as there are several mains");
3419 elsif Other_Switches /= Nil_Variable_Value then
3420 Write_Line
3421 ("Warning: using Builder'Switches(others), "
3422 & "as there are several mains");
3424 elsif Defaults /= Nil_Variable_Value then
3425 Write_Line
3426 ("Warning: using Builder'Default_Switches("""
3427 & Get_Name_String (Lang)
3428 & """), as there are several mains");
3429 else
3430 Write_Line
3431 ("Warning: using no switches from package "
3432 & "Builder, as there are several mains");
3433 end if;
3434 end if;
3436 Builder_Switches_Lang := Lang;
3438 if Name /= No_Name then
3439 -- Get the switches for the single main
3440 Switches := Switches_For_Main;
3441 end if;
3443 if Switches = Nil_Variable_Value or else Switches.Default then
3444 -- Get the switches for the common language of the mains
3445 Switches := Switches_For_Lang;
3446 end if;
3448 if Switches = Nil_Variable_Value or else Switches.Default then
3449 Switches := Other_Switches;
3450 end if;
3452 -- For backward compatibility with gnatmake, if no Switches
3453 -- are declared, check for Default_Switches (<language>).
3455 if Switches = Nil_Variable_Value or else Switches.Default then
3456 Switches := Defaults;
3457 end if;
3459 -- If switches have been found, scan them
3461 if Switches /= Nil_Variable_Value and then not Switches.Default then
3462 List := Switches.Values;
3464 while List /= Nil_String loop
3465 Element := Project_Tree.Shared.String_Elements.Table (List);
3466 Get_Name_String (Element.Value);
3468 if Name_Len /= 0 then
3469 declare
3470 -- Add_Switch might itself be using the name_buffer, so
3471 -- we make a temporary here.
3472 Switch : constant String := Name_Buffer (1 .. Name_Len);
3473 begin
3474 Success := Add_Switch
3475 (Switch => Switch,
3476 For_Lang => Builder_Switches_Lang,
3477 For_Builder => True,
3478 Has_Global_Compilation_Switches =>
3479 Global_Compilation_Array /= No_Array_Element);
3480 end;
3482 if not Success then
3483 for J in reverse 1 .. Name_Len loop
3484 Name_Buffer (J + J) := Name_Buffer (J);
3485 Name_Buffer (J + J - 1) := ''';
3486 end loop;
3488 Name_Len := Name_Len + Name_Len;
3490 Prj.Err.Error_Msg
3491 (Env.Flags,
3492 '"' & Name_Buffer (1 .. Name_Len)
3493 & """ is not a builder switch. Consider moving "
3494 & "it to Global_Compilation_Switches.",
3495 Element.Location);
3496 Fail_Program
3497 (Project_Tree,
3498 "*** illegal switch """
3499 & Get_Name_String (Element.Value) & '"');
3500 end if;
3501 end if;
3503 List := Element.Next;
3504 end loop;
3505 end if;
3507 -- Reset the Builder Switches language
3509 Builder_Switches_Lang := No_Name;
3511 -- Take into account attributes Global_Compilation_Switches
3513 while Global_Compilation_Array /= No_Array_Element loop
3514 Global_Compilation_Elem :=
3515 Project_Tree.Shared.Array_Elements.Table
3516 (Global_Compilation_Array);
3518 Get_Name_String (Global_Compilation_Elem.Index);
3519 To_Lower (Name_Buffer (1 .. Name_Len));
3520 Index := Name_Find;
3522 if Only_For_Lang = No_Name or else Index = Only_For_Lang then
3523 Global_Compilation_Switches := Global_Compilation_Elem.Value;
3525 if Global_Compilation_Switches /= Nil_Variable_Value
3526 and then not Global_Compilation_Switches.Default
3527 then
3528 -- We have found an attribute
3529 -- Global_Compilation_Switches for a language: put the
3530 -- switches in the appropriate table.
3532 List := Global_Compilation_Switches.Values;
3533 while List /= Nil_String loop
3534 Element :=
3535 Project_Tree.Shared.String_Elements.Table (List);
3537 if Element.Value /= No_Name then
3538 Success := Add_Switch
3539 (Switch => Get_Name_String (Element.Value),
3540 For_Lang => Index,
3541 For_Builder => False,
3542 Has_Global_Compilation_Switches =>
3543 Global_Compilation_Array /= No_Array_Element);
3544 end if;
3546 List := Element.Next;
3547 end loop;
3548 end if;
3549 end if;
3551 Global_Compilation_Array := Global_Compilation_Elem.Next;
3552 end loop;
3553 end if;
3554 end Compute_Builder_Switches;
3556 ---------------------
3557 -- Write_Path_File --
3558 ---------------------
3560 procedure Write_Path_File (FD : File_Descriptor) is
3561 Last : Natural;
3562 Status : Boolean;
3564 begin
3565 Name_Len := 0;
3567 for Index in Directories.First .. Directories.Last loop
3568 Add_Str_To_Name_Buffer (Get_Name_String (Directories.Table (Index)));
3569 Add_Char_To_Name_Buffer (ASCII.LF);
3570 end loop;
3572 Last := Write (FD, Name_Buffer (1)'Address, Name_Len);
3574 if Last = Name_Len then
3575 Close (FD, Status);
3576 else
3577 Status := False;
3578 end if;
3580 if not Status then
3581 Prj.Com.Fail ("could not write temporary file");
3582 end if;
3583 end Write_Path_File;
3585 end Makeutl;