[gcc/]
[official-gcc.git] / gcc / ada / makeutl.adb
blob7f7d060dcbef5a378b543408ebdbd71f6f67dfcf
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 Hostparm;
33 with Osint; use Osint;
34 with Output; use Output;
35 with Opt; use Opt;
36 with Prj.Com;
37 with Prj.Err;
38 with Prj.Ext;
39 with Prj.Util; use Prj.Util;
40 with Sinput.P;
41 with Tempdir;
43 with Ada.Command_Line; use Ada.Command_Line;
44 with Ada.Unchecked_Deallocation;
46 with GNAT.Case_Util; use GNAT.Case_Util;
47 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
48 with GNAT.HTable;
49 with GNAT.Regexp; use GNAT.Regexp;
51 package body Makeutl is
53 type Linker_Options_Data is record
54 Project : Project_Id;
55 Options : String_List_Id;
56 end record;
58 Linker_Option_Initial_Count : constant := 20;
60 Linker_Options_Buffer : String_List_Access :=
61 new String_List (1 .. Linker_Option_Initial_Count);
63 Last_Linker_Option : Natural := 0;
65 package Linker_Opts is new Table.Table (
66 Table_Component_Type => Linker_Options_Data,
67 Table_Index_Type => Integer,
68 Table_Low_Bound => 1,
69 Table_Initial => 10,
70 Table_Increment => 100,
71 Table_Name => "Make.Linker_Opts");
73 procedure Add_Linker_Option (Option : String);
75 ---------
76 -- Add --
77 ---------
79 procedure Add
80 (Option : String_Access;
81 To : in out String_List_Access;
82 Last : in out Natural)
84 begin
85 if Last = To'Last then
86 declare
87 New_Options : constant String_List_Access :=
88 new String_List (1 .. To'Last * 2);
90 begin
91 New_Options (To'Range) := To.all;
93 -- Set all elements of the original options to null to avoid
94 -- deallocation of copies.
96 To.all := (others => null);
98 Free (To);
99 To := New_Options;
100 end;
101 end if;
103 Last := Last + 1;
104 To (Last) := Option;
105 end Add;
107 procedure Add
108 (Option : String;
109 To : in out String_List_Access;
110 Last : in out Natural)
112 begin
113 Add (Option => new String'(Option), To => To, Last => Last);
114 end Add;
116 -----------------------
117 -- Add_Linker_Option --
118 -----------------------
120 procedure Add_Linker_Option (Option : String) is
121 begin
122 if Option'Length > 0 then
123 if Last_Linker_Option = Linker_Options_Buffer'Last then
124 declare
125 New_Buffer : constant String_List_Access :=
126 new String_List
127 (1 .. Linker_Options_Buffer'Last +
128 Linker_Option_Initial_Count);
129 begin
130 New_Buffer (Linker_Options_Buffer'Range) :=
131 Linker_Options_Buffer.all;
132 Linker_Options_Buffer.all := (others => null);
133 Free (Linker_Options_Buffer);
134 Linker_Options_Buffer := New_Buffer;
135 end;
136 end if;
138 Last_Linker_Option := Last_Linker_Option + 1;
139 Linker_Options_Buffer (Last_Linker_Option) := new String'(Option);
140 end if;
141 end Add_Linker_Option;
143 -------------------
144 -- Absolute_Path --
145 -------------------
147 function Absolute_Path
148 (Path : Path_Name_Type;
149 Project : Project_Id) return String
151 begin
152 Get_Name_String (Path);
154 declare
155 Path_Name : constant String := Name_Buffer (1 .. Name_Len);
157 begin
158 if Is_Absolute_Path (Path_Name) then
159 return Path_Name;
161 else
162 declare
163 Parent_Directory : constant String :=
164 Get_Name_String
165 (Project.Directory.Display_Name);
167 begin
168 return Parent_Directory & Path_Name;
169 end;
170 end if;
171 end;
172 end Absolute_Path;
174 ----------------------------
175 -- Aggregate_Libraries_In --
176 ----------------------------
178 function Aggregate_Libraries_In (Tree : Project_Tree_Ref) return Boolean is
179 List : Project_List;
181 begin
182 List := Tree.Projects;
183 while List /= null loop
184 if List.Project.Qualifier = Aggregate_Library then
185 return True;
186 end if;
188 List := List.Next;
189 end loop;
191 return False;
192 end Aggregate_Libraries_In;
194 -------------------------
195 -- Base_Name_Index_For --
196 -------------------------
198 function Base_Name_Index_For
199 (Main : String;
200 Main_Index : Int;
201 Index_Separator : Character) return File_Name_Type
203 Result : File_Name_Type;
205 begin
206 Name_Len := 0;
207 Add_Str_To_Name_Buffer (Base_Name (Main));
209 -- Remove the extension, if any, that is the last part of the base name
210 -- starting with a dot and following some characters.
212 for J in reverse 2 .. Name_Len loop
213 if Name_Buffer (J) = '.' then
214 Name_Len := J - 1;
215 exit;
216 end if;
217 end loop;
219 -- Add the index info, if index is different from 0
221 if Main_Index > 0 then
222 Add_Char_To_Name_Buffer (Index_Separator);
224 declare
225 Img : constant String := Main_Index'Img;
226 begin
227 Add_Str_To_Name_Buffer (Img (2 .. Img'Last));
228 end;
229 end if;
231 Result := Name_Find;
232 return Result;
233 end Base_Name_Index_For;
235 ------------------------------
236 -- Check_Source_Info_In_ALI --
237 ------------------------------
239 function Check_Source_Info_In_ALI
240 (The_ALI : ALI_Id;
241 Tree : Project_Tree_Ref) return Name_Id
243 Result : Name_Id := No_Name;
244 Unit_Name : Name_Id;
246 begin
247 -- Loop through units
249 for U in ALIs.Table (The_ALI).First_Unit ..
250 ALIs.Table (The_ALI).Last_Unit
251 loop
252 -- Check if the file name is one of the source of the unit
254 Get_Name_String (Units.Table (U).Uname);
255 Name_Len := Name_Len - 2;
256 Unit_Name := Name_Find;
258 if File_Not_A_Source_Of (Tree, Unit_Name, Units.Table (U).Sfile) then
259 return No_Name;
260 end if;
262 if Result = No_Name then
263 Result := Unit_Name;
264 end if;
266 -- Loop to do same check for each of the withed units
268 for W in Units.Table (U).First_With .. Units.Table (U).Last_With loop
269 declare
270 WR : ALI.With_Record renames Withs.Table (W);
272 begin
273 if WR.Sfile /= No_File then
274 Get_Name_String (WR.Uname);
275 Name_Len := Name_Len - 2;
276 Unit_Name := Name_Find;
278 if File_Not_A_Source_Of (Tree, Unit_Name, WR.Sfile) then
279 return No_Name;
280 end if;
281 end if;
282 end;
283 end loop;
284 end loop;
286 -- Loop to check subunits and replaced sources
288 for D in ALIs.Table (The_ALI).First_Sdep ..
289 ALIs.Table (The_ALI).Last_Sdep
290 loop
291 declare
292 SD : Sdep_Record renames Sdep.Table (D);
294 begin
295 Unit_Name := SD.Subunit_Name;
297 if Unit_Name = No_Name then
299 -- Check if this source file has been replaced by a source with
300 -- a different file name.
302 if Tree /= null and then Tree.Replaced_Source_Number > 0 then
303 declare
304 Replacement : constant File_Name_Type :=
305 Replaced_Source_HTable.Get
306 (Tree.Replaced_Sources, SD.Sfile);
308 begin
309 if Replacement /= No_File then
310 if Verbose_Mode then
311 Write_Line
312 ("source file"
313 & Get_Name_String (SD.Sfile)
314 & " has been replaced by "
315 & Get_Name_String (Replacement));
316 end if;
318 return No_Name;
319 end if;
320 end;
321 end if;
323 -- Check that a dependent source for a unit that is from a
324 -- project is indeed a source of this unit.
326 Unit_Name := SD.Unit_Name;
328 if Unit_Name /= No_Name
329 and then not Fname.Is_Internal_File_Name (SD.Sfile)
330 and then File_Not_A_Source_Of (Tree, Unit_Name, SD.Sfile)
331 then
332 return No_Name;
333 end if;
335 else
336 -- For separates, the file is no longer associated with the
337 -- unit ("proc-sep.adb" is not associated with unit "proc.sep")
338 -- so we need to check whether the source file still exists in
339 -- the source tree: it will if it matches the naming scheme
340 -- (and then will be for the same unit).
342 if Find_Source
343 (In_Tree => Tree,
344 Project => No_Project,
345 Base_Name => SD.Sfile) = No_Source
346 then
347 -- If this is not a runtime file or if, when gnatmake switch
348 -- -a is used, we are not able to find this subunit in the
349 -- source directories, then recompilation is needed.
351 if not Fname.Is_Internal_File_Name (SD.Sfile)
352 or else
353 (Check_Readonly_Files
354 and then Full_Source_Name (SD.Sfile) = No_File)
355 then
356 if Verbose_Mode then
357 Write_Line
358 ("While parsing ALI file, file "
359 & Get_Name_String (SD.Sfile)
360 & " is indicated as containing subunit "
361 & Get_Name_String (Unit_Name)
362 & " but this does not match what was found while"
363 & " parsing the project. Will recompile");
364 end if;
366 return No_Name;
367 end if;
368 end if;
369 end if;
370 end;
371 end loop;
373 return Result;
374 end Check_Source_Info_In_ALI;
376 --------------------------------
377 -- Create_Binder_Mapping_File --
378 --------------------------------
380 function Create_Binder_Mapping_File
381 (Project_Tree : Project_Tree_Ref) return Path_Name_Type
383 Mapping_Path : Path_Name_Type := No_Path;
385 Mapping_FD : File_Descriptor := Invalid_FD;
386 -- A File Descriptor for an eventual mapping file
388 ALI_Unit : Unit_Name_Type := No_Unit_Name;
389 -- The unit name of an ALI file
391 ALI_Name : File_Name_Type := No_File;
392 -- The file name of the ALI file
394 ALI_Project : Project_Id := No_Project;
395 -- The project of the ALI file
397 Bytes : Integer;
398 OK : Boolean := False;
399 Unit : Unit_Index;
401 Status : Boolean;
402 -- For call to Close
404 Iter : Source_Iterator := For_Each_Source
405 (In_Tree => Project_Tree,
406 Language => Name_Ada,
407 Encapsulated_Libs => False,
408 Locally_Removed => False);
410 Source : Prj.Source_Id;
412 begin
413 Tempdir.Create_Temp_File (Mapping_FD, Mapping_Path);
414 Record_Temp_File (Project_Tree.Shared, Mapping_Path);
416 if Mapping_FD /= Invalid_FD then
417 OK := True;
419 loop
420 Source := Element (Iter);
421 exit when Source = No_Source;
423 Unit := Source.Unit;
425 if Source.Replaced_By /= No_Source
426 or else Unit = No_Unit_Index
427 or else Unit.Name = No_Name
428 then
429 ALI_Name := No_File;
431 -- If this is a body, put it in the mapping
433 elsif Source.Kind = Impl
434 and then Unit.File_Names (Impl) /= No_Source
435 and then Unit.File_Names (Impl).Project /= No_Project
436 then
437 Get_Name_String (Unit.Name);
438 Add_Str_To_Name_Buffer ("%b");
439 ALI_Unit := Name_Find;
440 ALI_Name :=
441 Lib_File_Name (Unit.File_Names (Impl).Display_File);
442 ALI_Project := Unit.File_Names (Impl).Project;
444 -- Otherwise, if this is a spec and there is no body, put it in
445 -- the mapping.
447 elsif Source.Kind = Spec
448 and then Unit.File_Names (Impl) = No_Source
449 and then Unit.File_Names (Spec) /= No_Source
450 and then Unit.File_Names (Spec).Project /= No_Project
451 then
452 Get_Name_String (Unit.Name);
453 Add_Str_To_Name_Buffer ("%s");
454 ALI_Unit := Name_Find;
455 ALI_Name :=
456 Lib_File_Name (Unit.File_Names (Spec).Display_File);
457 ALI_Project := Unit.File_Names (Spec).Project;
459 else
460 ALI_Name := No_File;
461 end if;
463 -- If we have something to put in the mapping then do it now. If
464 -- the project is extended, look for the ALI file in the project,
465 -- then in the extending projects in order, and use the last one
466 -- found.
468 if ALI_Name /= No_File then
470 -- Look in the project and the projects that are extending it
471 -- to find the real ALI file.
473 declare
474 ALI : constant String := Get_Name_String (ALI_Name);
475 ALI_Path : Name_Id := No_Name;
477 begin
478 loop
479 -- For library projects, use the library ALI directory,
480 -- for other projects, use the object directory.
482 if ALI_Project.Library then
483 Get_Name_String
484 (ALI_Project.Library_ALI_Dir.Display_Name);
485 else
486 Get_Name_String
487 (ALI_Project.Object_Directory.Display_Name);
488 end if;
490 Add_Str_To_Name_Buffer (ALI);
492 if Is_Regular_File (Name_Buffer (1 .. Name_Len)) then
493 ALI_Path := Name_Find;
494 end if;
496 ALI_Project := ALI_Project.Extended_By;
497 exit when ALI_Project = No_Project;
498 end loop;
500 if ALI_Path /= No_Name then
502 -- First line is the unit name
504 Get_Name_String (ALI_Unit);
505 Add_Char_To_Name_Buffer (ASCII.LF);
506 Bytes :=
507 Write
508 (Mapping_FD,
509 Name_Buffer (1)'Address,
510 Name_Len);
511 OK := Bytes = Name_Len;
513 exit when not OK;
515 -- Second line is the ALI file name
517 Get_Name_String (ALI_Name);
518 Add_Char_To_Name_Buffer (ASCII.LF);
519 Bytes :=
520 Write
521 (Mapping_FD,
522 Name_Buffer (1)'Address,
523 Name_Len);
524 OK := (Bytes = Name_Len);
526 exit when not OK;
528 -- Third line is the ALI path name
530 Get_Name_String (ALI_Path);
531 Add_Char_To_Name_Buffer (ASCII.LF);
532 Bytes :=
533 Write
534 (Mapping_FD,
535 Name_Buffer (1)'Address,
536 Name_Len);
537 OK := (Bytes = Name_Len);
539 -- If OK is False, it means we were unable to write a
540 -- line. No point in continuing with the other units.
542 exit when not OK;
543 end if;
544 end;
545 end if;
547 Next (Iter);
548 end loop;
550 Close (Mapping_FD, Status);
552 OK := OK and Status;
553 end if;
555 -- If the creation of the mapping file was successful, we add the switch
556 -- to the arguments of gnatbind.
558 if OK then
559 return Mapping_Path;
561 else
562 return No_Path;
563 end if;
564 end Create_Binder_Mapping_File;
566 -----------------
567 -- Create_Name --
568 -----------------
570 function Create_Name (Name : String) return File_Name_Type is
571 begin
572 Name_Len := 0;
573 Add_Str_To_Name_Buffer (Name);
574 return Name_Find;
575 end Create_Name;
577 function Create_Name (Name : String) return Name_Id is
578 begin
579 Name_Len := 0;
580 Add_Str_To_Name_Buffer (Name);
581 return Name_Find;
582 end Create_Name;
584 function Create_Name (Name : String) return Path_Name_Type is
585 begin
586 Name_Len := 0;
587 Add_Str_To_Name_Buffer (Name);
588 return Name_Find;
589 end Create_Name;
591 ---------------------------
592 -- Ensure_Absolute_Path --
593 ---------------------------
595 procedure Ensure_Absolute_Path
596 (Switch : in out String_Access;
597 Parent : String;
598 Do_Fail : Fail_Proc;
599 For_Gnatbind : Boolean := False;
600 Including_Non_Switch : Boolean := True;
601 Including_RTS : Boolean := False)
603 begin
604 if Switch /= null then
605 declare
606 Sw : String (1 .. Switch'Length);
607 Start : Positive;
609 begin
610 Sw := Switch.all;
612 if Sw (1) = '-' then
613 if Sw'Length >= 3
614 and then (Sw (2) = 'I'
615 or else (not For_Gnatbind
616 and then (Sw (2) = 'L'
617 or else
618 Sw (2) = 'A')))
619 then
620 Start := 3;
622 if Sw = "-I-" then
623 return;
624 end if;
626 elsif Sw'Length >= 4
627 and then (Sw (2 .. 3) = "aL"
628 or else
629 Sw (2 .. 3) = "aO"
630 or else
631 Sw (2 .. 3) = "aI"
632 or else
633 (For_Gnatbind and then Sw (2 .. 3) = "A="))
634 then
635 Start := 4;
637 elsif Including_RTS
638 and then Sw'Length >= 7
639 and then Sw (2 .. 6) = "-RTS="
640 then
641 Start := 7;
643 else
644 return;
645 end if;
647 -- Because relative path arguments to --RTS= may be relative to
648 -- the search directory prefix, those relative path arguments
649 -- are converted only when they include directory information.
651 if not Is_Absolute_Path (Sw (Start .. Sw'Last)) then
652 if Parent'Length = 0 then
653 Do_Fail
654 ("relative search path switches ("""
655 & Sw
656 & """) are not allowed");
658 elsif Including_RTS then
659 for J in Start .. Sw'Last loop
660 if Sw (J) = Directory_Separator then
661 Switch :=
662 new String'
663 (Sw (1 .. Start - 1)
664 & Parent
665 & Directory_Separator
666 & Sw (Start .. Sw'Last));
667 return;
668 end if;
669 end loop;
671 else
672 Switch :=
673 new String'
674 (Sw (1 .. Start - 1)
675 & Parent
676 & Directory_Separator
677 & Sw (Start .. Sw'Last));
678 end if;
679 end if;
681 elsif Including_Non_Switch then
682 if not Is_Absolute_Path (Sw) then
683 if Parent'Length = 0 then
684 Do_Fail
685 ("relative paths (""" & Sw & """) are not allowed");
686 else
687 Switch := new String'(Parent & Directory_Separator & Sw);
688 end if;
689 end if;
690 end if;
691 end;
692 end if;
693 end Ensure_Absolute_Path;
695 ----------------------------
696 -- Executable_Prefix_Path --
697 ----------------------------
699 function Executable_Prefix_Path return String is
700 Exec_Name : constant String := Command_Name;
702 function Get_Install_Dir (S : String) return String;
703 -- S is the executable name preceded by the absolute or relative path,
704 -- e.g. "c:\usr\bin\gcc.exe". Returns the absolute directory where "bin"
705 -- lies (in the example "C:\usr"). If the executable is not in a "bin"
706 -- directory, return "".
708 ---------------------
709 -- Get_Install_Dir --
710 ---------------------
712 function Get_Install_Dir (S : String) return String is
713 Exec : String := S;
714 Path_Last : Integer := 0;
716 begin
717 for J in reverse Exec'Range loop
718 if Exec (J) = Directory_Separator then
719 Path_Last := J - 1;
720 exit;
721 end if;
722 end loop;
724 if Path_Last >= Exec'First + 2 then
725 To_Lower (Exec (Path_Last - 2 .. Path_Last));
726 end if;
728 if Path_Last < Exec'First + 2
729 or else Exec (Path_Last - 2 .. Path_Last) /= "bin"
730 or else (Path_Last - 3 >= Exec'First
731 and then Exec (Path_Last - 3) /= Directory_Separator)
732 then
733 return "";
734 end if;
736 return Normalize_Pathname
737 (Exec (Exec'First .. Path_Last - 4),
738 Resolve_Links => Opt.Follow_Links_For_Dirs)
739 & Directory_Separator;
740 end Get_Install_Dir;
742 -- Beginning of Executable_Prefix_Path
744 begin
745 -- For VMS, the path returned is always /gnu/
747 if Hostparm.OpenVMS then
748 return "/gnu/";
749 end if;
751 -- First determine if a path prefix was placed in front of the
752 -- executable name.
754 for J in reverse Exec_Name'Range loop
755 if Exec_Name (J) = Directory_Separator then
756 return Get_Install_Dir (Exec_Name);
757 end if;
758 end loop;
760 -- If we get here, the user has typed the executable name with no
761 -- directory prefix.
763 declare
764 Path : String_Access := Locate_Exec_On_Path (Exec_Name);
765 begin
766 if Path = null then
767 return "";
768 else
769 declare
770 Dir : constant String := Get_Install_Dir (Path.all);
771 begin
772 Free (Path);
773 return Dir;
774 end;
775 end if;
776 end;
777 end Executable_Prefix_Path;
779 ------------------
780 -- Fail_Program --
781 ------------------
783 procedure Fail_Program
784 (Project_Tree : Project_Tree_Ref;
785 S : String;
786 Flush_Messages : Boolean := True)
788 begin
789 if Flush_Messages then
790 if Total_Errors_Detected /= 0 or else Warnings_Detected /= 0 then
791 Errutil.Finalize;
792 end if;
793 end if;
795 Finish_Program (Project_Tree, E_Fatal, S => S);
796 end Fail_Program;
798 --------------------
799 -- Finish_Program --
800 --------------------
802 procedure Finish_Program
803 (Project_Tree : Project_Tree_Ref;
804 Exit_Code : Osint.Exit_Code_Type := Osint.E_Success;
805 S : String := "")
807 begin
808 if not Debug.Debug_Flag_N then
809 Delete_Temp_Config_Files (Project_Tree);
811 if Project_Tree /= null then
812 Delete_All_Temp_Files (Project_Tree.Shared);
813 end if;
814 end if;
816 if S'Length > 0 then
817 if Exit_Code /= E_Success then
818 Osint.Fail (S);
819 else
820 Write_Str (S);
821 end if;
822 end if;
824 -- Output Namet statistics
826 Namet.Finalize;
828 Exit_Program (Exit_Code);
829 end Finish_Program;
831 --------------------------
832 -- File_Not_A_Source_Of --
833 --------------------------
835 function File_Not_A_Source_Of
836 (Project_Tree : Project_Tree_Ref;
837 Uname : Name_Id;
838 Sfile : File_Name_Type) return Boolean
840 Unit : constant Unit_Index :=
841 Units_Htable.Get (Project_Tree.Units_HT, Uname);
843 At_Least_One_File : Boolean := False;
845 begin
846 if Unit /= No_Unit_Index then
847 for F in Unit.File_Names'Range loop
848 if Unit.File_Names (F) /= null then
849 At_Least_One_File := True;
850 if Unit.File_Names (F).File = Sfile then
851 return False;
852 end if;
853 end if;
854 end loop;
856 if not At_Least_One_File then
858 -- The unit was probably created initially for a separate unit
859 -- (which are initially created as IMPL when both suffixes are the
860 -- same). Later on, Override_Kind changed the type of the file,
861 -- and the unit is no longer valid in fact.
863 return False;
864 end if;
866 Verbose_Msg (Uname, "sources do not include ", Name_Id (Sfile));
867 return True;
868 end if;
870 return False;
871 end File_Not_A_Source_Of;
873 ---------------------
874 -- Get_Directories --
875 ---------------------
877 procedure Get_Directories
878 (Project_Tree : Project_Tree_Ref;
879 For_Project : Project_Id;
880 Activity : Activity_Type;
881 Languages : Name_Ids)
884 procedure Recursive_Add
885 (Project : Project_Id;
886 Tree : Project_Tree_Ref;
887 Extended : in out Boolean);
888 -- Add all the source directories of a project to the path only if
889 -- this project has not been visited. Calls itself recursively for
890 -- projects being extended, and imported projects.
892 procedure Add_Dir (Value : Path_Name_Type);
893 -- Add directory Value in table Directories, if it is defined and not
894 -- already there.
896 -------------
897 -- Add_Dir --
898 -------------
900 procedure Add_Dir (Value : Path_Name_Type) is
901 Add_It : Boolean := True;
903 begin
904 if Value /= No_Path then
905 for Index in 1 .. Directories.Last loop
906 if Directories.Table (Index) = Value then
907 Add_It := False;
908 exit;
909 end if;
910 end loop;
912 if Add_It then
913 Directories.Increment_Last;
914 Directories.Table (Directories.Last) := Value;
915 end if;
916 end if;
917 end Add_Dir;
919 -------------------
920 -- Recursive_Add --
921 -------------------
923 procedure Recursive_Add
924 (Project : Project_Id;
925 Tree : Project_Tree_Ref;
926 Extended : in out Boolean)
928 Current : String_List_Id;
929 Dir : String_Element;
930 OK : Boolean := False;
931 Lang_Proc : Language_Ptr := Project.Languages;
933 begin
934 -- Add to path all directories of this project
936 if Activity = Compilation then
937 Lang_Loop :
938 while Lang_Proc /= No_Language_Index loop
939 for J in Languages'Range loop
940 OK := Lang_Proc.Name = Languages (J);
941 exit Lang_Loop when OK;
942 end loop;
944 Lang_Proc := Lang_Proc.Next;
945 end loop Lang_Loop;
947 if OK then
948 Current := Project.Source_Dirs;
950 while Current /= Nil_String loop
951 Dir := Tree.Shared.String_Elements.Table (Current);
952 Add_Dir (Path_Name_Type (Dir.Value));
953 Current := Dir.Next;
954 end loop;
955 end if;
957 elsif Project.Library then
958 if Activity = SAL_Binding and then Extended then
959 Add_Dir (Project.Object_Directory.Display_Name);
961 else
962 Add_Dir (Project.Library_ALI_Dir.Display_Name);
963 end if;
965 else
966 Add_Dir (Project.Object_Directory.Display_Name);
967 end if;
969 if Project.Extends = No_Project then
970 Extended := False;
971 end if;
972 end Recursive_Add;
974 procedure For_All_Projects is
975 new For_Every_Project_Imported (Boolean, Recursive_Add);
977 Extended : Boolean := True;
979 -- Start of processing for Get_Directories
981 begin
982 Directories.Init;
983 For_All_Projects (For_Project, Project_Tree, Extended);
984 end Get_Directories;
986 ------------------
987 -- Get_Switches --
988 ------------------
990 procedure Get_Switches
991 (Source : Prj.Source_Id;
992 Pkg_Name : Name_Id;
993 Project_Tree : Project_Tree_Ref;
994 Value : out Variable_Value;
995 Is_Default : out Boolean)
997 begin
998 Get_Switches
999 (Source_File => Source.File,
1000 Source_Lang => Source.Language.Name,
1001 Source_Prj => Source.Project,
1002 Pkg_Name => Pkg_Name,
1003 Project_Tree => Project_Tree,
1004 Value => Value,
1005 Is_Default => Is_Default);
1006 end Get_Switches;
1008 ------------------
1009 -- Get_Switches --
1010 ------------------
1012 procedure Get_Switches
1013 (Source_File : File_Name_Type;
1014 Source_Lang : Name_Id;
1015 Source_Prj : Project_Id;
1016 Pkg_Name : Name_Id;
1017 Project_Tree : Project_Tree_Ref;
1018 Value : out Variable_Value;
1019 Is_Default : out Boolean;
1020 Test_Without_Suffix : Boolean := False;
1021 Check_ALI_Suffix : Boolean := False)
1023 Project : constant Project_Id :=
1024 Ultimate_Extending_Project_Of (Source_Prj);
1025 Pkg : constant Package_Id :=
1026 Prj.Util.Value_Of
1027 (Name => Pkg_Name,
1028 In_Packages => Project.Decl.Packages,
1029 Shared => Project_Tree.Shared);
1030 Lang : Language_Ptr;
1032 begin
1033 Is_Default := False;
1035 if Source_File /= No_File then
1036 Value := Prj.Util.Value_Of
1037 (Name => Name_Id (Source_File),
1038 Attribute_Or_Array_Name => Name_Switches,
1039 In_Package => Pkg,
1040 Shared => Project_Tree.Shared,
1041 Allow_Wildcards => True);
1042 end if;
1044 if Value = Nil_Variable_Value and then Test_Without_Suffix then
1045 Lang :=
1046 Get_Language_From_Name (Project, Get_Name_String (Source_Lang));
1048 if Lang /= null then
1049 declare
1050 Naming : Lang_Naming_Data renames Lang.Config.Naming_Data;
1051 SF_Name : constant String := Get_Name_String (Source_File);
1052 Last : Positive := SF_Name'Length;
1053 Name : String (1 .. Last + 3);
1054 Spec_Suffix : String := Get_Name_String (Naming.Spec_Suffix);
1055 Body_Suffix : String := Get_Name_String (Naming.Body_Suffix);
1056 Truncated : Boolean := False;
1058 begin
1059 Canonical_Case_File_Name (Spec_Suffix);
1060 Canonical_Case_File_Name (Body_Suffix);
1061 Name (1 .. Last) := SF_Name;
1063 if Last > Body_Suffix'Length
1064 and then
1065 Name (Last - Body_Suffix'Length + 1 .. Last) = Body_Suffix
1066 then
1067 Truncated := True;
1068 Last := Last - Body_Suffix'Length;
1069 end if;
1071 if not Truncated
1072 and then Last > Spec_Suffix'Length
1073 and then
1074 Name (Last - Spec_Suffix'Length + 1 .. Last) = Spec_Suffix
1075 then
1076 Truncated := True;
1077 Last := Last - Spec_Suffix'Length;
1078 end if;
1080 if Truncated then
1081 Name_Len := 0;
1082 Add_Str_To_Name_Buffer (Name (1 .. Last));
1084 Value := Prj.Util.Value_Of
1085 (Name => Name_Find,
1086 Attribute_Or_Array_Name => Name_Switches,
1087 In_Package => Pkg,
1088 Shared => Project_Tree.Shared,
1089 Allow_Wildcards => True);
1090 end if;
1092 if Value = Nil_Variable_Value and then Check_ALI_Suffix then
1093 Last := SF_Name'Length;
1094 while Name (Last) /= '.' loop
1095 Last := Last - 1;
1096 end loop;
1098 Name_Len := 0;
1099 Add_Str_To_Name_Buffer (Name (1 .. Last));
1100 Add_Str_To_Name_Buffer ("ali");
1102 Value := Prj.Util.Value_Of
1103 (Name => Name_Find,
1104 Attribute_Or_Array_Name => Name_Switches,
1105 In_Package => Pkg,
1106 Shared => Project_Tree.Shared,
1107 Allow_Wildcards => True);
1108 end if;
1109 end;
1110 end if;
1111 end if;
1113 if Value = Nil_Variable_Value then
1114 Is_Default := True;
1115 Value :=
1116 Prj.Util.Value_Of
1117 (Name => Source_Lang,
1118 Attribute_Or_Array_Name => Name_Switches,
1119 In_Package => Pkg,
1120 Shared => Project_Tree.Shared,
1121 Force_Lower_Case_Index => True);
1122 end if;
1124 if Value = Nil_Variable_Value then
1125 Value :=
1126 Prj.Util.Value_Of
1127 (Name => All_Other_Names,
1128 Attribute_Or_Array_Name => Name_Switches,
1129 In_Package => Pkg,
1130 Shared => Project_Tree.Shared,
1131 Force_Lower_Case_Index => True);
1132 end if;
1134 if Value = Nil_Variable_Value then
1135 Value :=
1136 Prj.Util.Value_Of
1137 (Name => Source_Lang,
1138 Attribute_Or_Array_Name => Name_Default_Switches,
1139 In_Package => Pkg,
1140 Shared => Project_Tree.Shared);
1141 end if;
1142 end Get_Switches;
1144 ------------
1145 -- Inform --
1146 ------------
1148 procedure Inform (N : File_Name_Type; Msg : String) is
1149 begin
1150 Inform (Name_Id (N), Msg);
1151 end Inform;
1153 procedure Inform (N : Name_Id := No_Name; Msg : String) is
1154 begin
1155 Osint.Write_Program_Name;
1157 Write_Str (": ");
1159 if N /= No_Name then
1160 Write_Str ("""");
1162 declare
1163 Name : constant String := Get_Name_String (N);
1164 begin
1165 if Debug.Debug_Flag_F and then Is_Absolute_Path (Name) then
1166 Write_Str (File_Name (Name));
1167 else
1168 Write_Str (Name);
1169 end if;
1170 end;
1172 Write_Str (""" ");
1173 end if;
1175 Write_Str (Msg);
1176 Write_Eol;
1177 end Inform;
1179 ------------------------------
1180 -- Initialize_Source_Record --
1181 ------------------------------
1183 procedure Initialize_Source_Record (Source : Prj.Source_Id) is
1185 procedure Set_Object_Project
1186 (Obj_Dir : String;
1187 Obj_Proj : Project_Id;
1188 Obj_Path : Path_Name_Type;
1189 Stamp : Time_Stamp_Type);
1190 -- Update information about object file, switches file,...
1192 ------------------------
1193 -- Set_Object_Project --
1194 ------------------------
1196 procedure Set_Object_Project
1197 (Obj_Dir : String;
1198 Obj_Proj : Project_Id;
1199 Obj_Path : Path_Name_Type;
1200 Stamp : Time_Stamp_Type) is
1201 begin
1202 Source.Object_Project := Obj_Proj;
1203 Source.Object_Path := Obj_Path;
1204 Source.Object_TS := Stamp;
1206 if Source.Language.Config.Dependency_Kind /= None then
1207 declare
1208 Dep_Path : constant String :=
1209 Normalize_Pathname
1210 (Name =>
1211 Get_Name_String (Source.Dep_Name),
1212 Resolve_Links => Opt.Follow_Links_For_Files,
1213 Directory => Obj_Dir);
1214 begin
1215 Source.Dep_Path := Create_Name (Dep_Path);
1216 Source.Dep_TS := Osint.Unknown_Attributes;
1217 end;
1218 end if;
1220 -- Get the path of the switches file, even if Opt.Check_Switches is
1221 -- not set, as switch -s may be in the Builder switches that have not
1222 -- been scanned yet.
1224 declare
1225 Switches_Path : constant String :=
1226 Normalize_Pathname
1227 (Name =>
1228 Get_Name_String (Source.Switches),
1229 Resolve_Links => Opt.Follow_Links_For_Files,
1230 Directory => Obj_Dir);
1231 begin
1232 Source.Switches_Path := Create_Name (Switches_Path);
1234 if Stamp /= Empty_Time_Stamp then
1235 Source.Switches_TS := File_Stamp (Source.Switches_Path);
1236 end if;
1237 end;
1238 end Set_Object_Project;
1240 Obj_Proj : Project_Id;
1242 begin
1243 -- Nothing to do if source record has already been fully initialized
1245 if Source.Initialized then
1246 return;
1247 end if;
1249 -- Systematically recompute the time stamp
1251 Source.Source_TS := File_Stamp (Source.Path.Display_Name);
1253 -- Parse the source file to check whether we have a subunit
1255 if Source.Language.Config.Kind = Unit_Based
1256 and then Source.Kind = Impl
1257 and then Is_Subunit (Source)
1258 then
1259 Source.Kind := Sep;
1260 end if;
1262 if Source.Language.Config.Object_Generated
1263 and then Is_Compilable (Source)
1264 then
1265 -- First, get the correct object file name and dependency file name
1266 -- if the source is in a multi-unit file.
1268 if Source.Index /= 0 then
1269 Source.Object :=
1270 Object_Name
1271 (Source_File_Name => Source.File,
1272 Source_Index => Source.Index,
1273 Index_Separator =>
1274 Source.Language.Config.Multi_Unit_Object_Separator,
1275 Object_File_Suffix =>
1276 Source.Language.Config.Object_File_Suffix);
1278 Source.Dep_Name :=
1279 Dependency_Name
1280 (Source.Object, Source.Language.Config.Dependency_Kind);
1281 end if;
1283 -- Find the object file for that source. It could be either in the
1284 -- current project or in an extended project (it might actually not
1285 -- exist yet in the ultimate extending project, but if not found
1286 -- elsewhere that's where we'll expect to find it).
1288 Obj_Proj := Source.Project;
1290 while Obj_Proj /= No_Project loop
1291 if Obj_Proj.Object_Directory /= No_Path_Information then
1292 declare
1293 Dir : constant String :=
1294 Get_Name_String (Obj_Proj.Object_Directory.Display_Name);
1296 Object_Path : constant String :=
1297 Normalize_Pathname
1298 (Name => Get_Name_String (Source.Object),
1299 Resolve_Links => Opt.Follow_Links_For_Files,
1300 Directory => Dir);
1302 Obj_Path : constant Path_Name_Type :=
1303 Create_Name (Object_Path);
1305 Stamp : Time_Stamp_Type := Empty_Time_Stamp;
1307 begin
1308 -- For specs, we do not check object files if there is a
1309 -- body. This saves a system call. On the other hand, we do
1310 -- need to know the object_path, in case the user has passed
1311 -- the .ads on the command line to compile the spec only.
1313 if Source.Kind /= Spec
1314 or else Source.Unit = No_Unit_Index
1315 or else Source.Unit.File_Names (Impl) = No_Source
1316 then
1317 Stamp := File_Stamp (Obj_Path);
1318 end if;
1320 if Stamp /= Empty_Time_Stamp
1321 or else (Obj_Proj.Extended_By = No_Project
1322 and then Source.Object_Project = No_Project)
1323 then
1324 Set_Object_Project (Dir, Obj_Proj, Obj_Path, Stamp);
1325 end if;
1326 end;
1327 end if;
1329 Obj_Proj := Obj_Proj.Extended_By;
1330 end loop;
1332 elsif Source.Language.Config.Dependency_Kind = Makefile then
1333 declare
1334 Object_Dir : constant String :=
1335 Get_Name_String (Source.Project.Object_Directory.Display_Name);
1336 Dep_Path : constant String :=
1337 Normalize_Pathname
1338 (Name => Get_Name_String (Source.Dep_Name),
1339 Resolve_Links => Opt.Follow_Links_For_Files,
1340 Directory => Object_Dir);
1341 begin
1342 Source.Dep_Path := Create_Name (Dep_Path);
1343 Source.Dep_TS := Osint.Unknown_Attributes;
1344 end;
1345 end if;
1347 Source.Initialized := True;
1348 end Initialize_Source_Record;
1350 ----------------------------
1351 -- Is_External_Assignment --
1352 ----------------------------
1354 function Is_External_Assignment
1355 (Env : Prj.Tree.Environment;
1356 Argv : String) return Boolean
1358 Start : Positive := 3;
1359 Finish : Natural := Argv'Last;
1361 pragma Assert (Argv'First = 1);
1362 pragma Assert (Argv (1 .. 2) = "-X");
1364 begin
1365 if Argv'Last < 5 then
1366 return False;
1368 elsif Argv (3) = '"' then
1369 if Argv (Argv'Last) /= '"' or else Argv'Last < 7 then
1370 return False;
1371 else
1372 Start := 4;
1373 Finish := Argv'Last - 1;
1374 end if;
1375 end if;
1377 return Prj.Ext.Check
1378 (Self => Env.External,
1379 Declaration => Argv (Start .. Finish));
1380 end Is_External_Assignment;
1382 ----------------
1383 -- Is_Subunit --
1384 ----------------
1386 function Is_Subunit (Source : Prj.Source_Id) return Boolean is
1387 Src_Ind : Source_File_Index;
1389 begin
1390 if Source.Kind = Sep then
1391 return True;
1393 -- A Spec, a file based language source or a body with a spec cannot be
1394 -- a subunit.
1396 elsif Source.Kind = Spec
1397 or else Source.Unit = No_Unit_Index
1398 or else Other_Part (Source) /= No_Source
1399 then
1400 return False;
1401 end if;
1403 -- Here, we are assuming that the language is Ada, as it is the only
1404 -- unit based language that we know.
1406 Src_Ind :=
1407 Sinput.P.Load_Project_File
1408 (Get_Name_String (Source.Path.Display_Name));
1410 return Sinput.P.Source_File_Is_Subunit (Src_Ind);
1411 end Is_Subunit;
1413 -----------------------------
1414 -- Linker_Options_Switches --
1415 -----------------------------
1417 function Linker_Options_Switches
1418 (Project : Project_Id;
1419 Do_Fail : Fail_Proc;
1420 In_Tree : Project_Tree_Ref) return String_List
1422 procedure Recursive_Add
1423 (Proj : Project_Id;
1424 In_Tree : Project_Tree_Ref;
1425 Dummy : in out Boolean);
1426 -- The recursive routine used to add linker options
1428 -------------------
1429 -- Recursive_Add --
1430 -------------------
1432 procedure Recursive_Add
1433 (Proj : Project_Id;
1434 In_Tree : Project_Tree_Ref;
1435 Dummy : in out Boolean)
1437 pragma Unreferenced (Dummy);
1439 Linker_Package : Package_Id;
1440 Options : Variable_Value;
1442 begin
1443 Linker_Package :=
1444 Prj.Util.Value_Of
1445 (Name => Name_Linker,
1446 In_Packages => Proj.Decl.Packages,
1447 Shared => In_Tree.Shared);
1449 Options :=
1450 Prj.Util.Value_Of
1451 (Name => Name_Ada,
1452 Index => 0,
1453 Attribute_Or_Array_Name => Name_Linker_Options,
1454 In_Package => Linker_Package,
1455 Shared => In_Tree.Shared);
1457 -- If attribute is present, add the project with the attribute to
1458 -- table Linker_Opts.
1460 if Options /= Nil_Variable_Value then
1461 Linker_Opts.Increment_Last;
1462 Linker_Opts.Table (Linker_Opts.Last) :=
1463 (Project => Proj, Options => Options.Values);
1464 end if;
1465 end Recursive_Add;
1467 procedure For_All_Projects is
1468 new For_Every_Project_Imported (Boolean, Recursive_Add);
1470 Dummy : Boolean := False;
1472 -- Start of processing for Linker_Options_Switches
1474 begin
1475 Linker_Opts.Init;
1477 For_All_Projects (Project, In_Tree, Dummy, Imported_First => True);
1479 Last_Linker_Option := 0;
1481 for Index in reverse 1 .. Linker_Opts.Last loop
1482 declare
1483 Options : String_List_Id;
1484 Proj : constant Project_Id :=
1485 Linker_Opts.Table (Index).Project;
1486 Option : Name_Id;
1487 Dir_Path : constant String :=
1488 Get_Name_String (Proj.Directory.Name);
1490 begin
1491 Options := Linker_Opts.Table (Index).Options;
1492 while Options /= Nil_String loop
1493 Option := In_Tree.Shared.String_Elements.Table (Options).Value;
1494 Get_Name_String (Option);
1496 -- Do not consider empty linker options
1498 if Name_Len /= 0 then
1499 Add_Linker_Option (Name_Buffer (1 .. Name_Len));
1501 -- Object files and -L switches specified with relative
1502 -- paths must be converted to absolute paths.
1504 Ensure_Absolute_Path
1505 (Switch =>
1506 Linker_Options_Buffer (Last_Linker_Option),
1507 Parent => Dir_Path,
1508 Do_Fail => Do_Fail,
1509 For_Gnatbind => False);
1510 end if;
1512 Options := In_Tree.Shared.String_Elements.Table (Options).Next;
1513 end loop;
1514 end;
1515 end loop;
1517 return Linker_Options_Buffer (1 .. Last_Linker_Option);
1518 end Linker_Options_Switches;
1520 -----------
1521 -- Mains --
1522 -----------
1524 package body Mains is
1526 package Names is new Table.Table
1527 (Table_Component_Type => Main_Info,
1528 Table_Index_Type => Integer,
1529 Table_Low_Bound => 1,
1530 Table_Initial => 10,
1531 Table_Increment => 100,
1532 Table_Name => "Makeutl.Mains.Names");
1533 -- The table that stores the mains
1535 Current : Natural := 0;
1536 -- The index of the last main retrieved from the table
1538 Count_Of_Mains_With_No_Tree : Natural := 0;
1539 -- Number of main units for which we do not know the project tree
1541 --------------
1542 -- Add_Main --
1543 --------------
1545 procedure Add_Main
1546 (Name : String;
1547 Index : Int := 0;
1548 Location : Source_Ptr := No_Location;
1549 Project : Project_Id := No_Project;
1550 Tree : Project_Tree_Ref := null)
1552 begin
1553 if Current_Verbosity = High then
1554 Debug_Output ("Add_Main """ & Name & """ " & Index'Img
1555 & " with_tree? "
1556 & Boolean'Image (Tree /= null));
1557 end if;
1559 Name_Len := 0;
1560 Add_Str_To_Name_Buffer (Name);
1561 Canonical_Case_File_Name (Name_Buffer (1 .. Name_Len));
1563 Names.Increment_Last;
1564 Names.Table (Names.Last) :=
1565 (Name_Find, Index, Location, No_Source, Project, Tree);
1567 if Tree /= null then
1568 Builder_Data (Tree).Number_Of_Mains :=
1569 Builder_Data (Tree).Number_Of_Mains + 1;
1571 else
1572 Mains.Count_Of_Mains_With_No_Tree :=
1573 Mains.Count_Of_Mains_With_No_Tree + 1;
1574 end if;
1575 end Add_Main;
1577 --------------------
1578 -- Complete_Mains --
1579 --------------------
1581 procedure Complete_Mains
1582 (Flags : Processing_Flags;
1583 Root_Project : Project_Id;
1584 Project_Tree : Project_Tree_Ref)
1586 procedure Do_Complete (Project : Project_Id; Tree : Project_Tree_Ref);
1587 -- Check the mains for this specific project
1589 procedure Complete_All is new For_Project_And_Aggregated
1590 (Do_Complete);
1592 procedure Add_Multi_Unit_Sources
1593 (Tree : Project_Tree_Ref;
1594 Source : Prj.Source_Id);
1595 -- Add all units from the same file as the multi-unit Source
1597 function Find_File_Add_Extension
1598 (Tree : Project_Tree_Ref;
1599 Base_Main : String) return Prj.Source_Id;
1600 -- Search for Main in the project, adding body or spec extensions
1602 ----------------------------
1603 -- Add_Multi_Unit_Sources --
1604 ----------------------------
1606 procedure Add_Multi_Unit_Sources
1607 (Tree : Project_Tree_Ref;
1608 Source : Prj.Source_Id)
1610 Iter : Source_Iterator;
1611 Src : Prj.Source_Id;
1613 begin
1614 Debug_Output
1615 ("found multi-unit source file in project", Source.Project.Name);
1617 Iter := For_Each_Source
1618 (In_Tree => Tree, Project => Source.Project);
1620 while Element (Iter) /= No_Source loop
1621 Src := Element (Iter);
1623 if Src.File = Source.File
1624 and then Src.Index /= Source.Index
1625 then
1626 if Src.File = Source.File then
1627 Debug_Output
1628 ("add main in project, index=" & Src.Index'Img);
1629 end if;
1631 Names.Increment_Last;
1632 Names.Table (Names.Last) :=
1633 (File => Src.File,
1634 Index => Src.Index,
1635 Location => No_Location,
1636 Source => Src,
1637 Project => Src.Project,
1638 Tree => Tree);
1640 Builder_Data (Tree).Number_Of_Mains :=
1641 Builder_Data (Tree).Number_Of_Mains + 1;
1642 end if;
1644 Next (Iter);
1645 end loop;
1646 end Add_Multi_Unit_Sources;
1648 -----------------------------
1649 -- Find_File_Add_Extension --
1650 -----------------------------
1652 function Find_File_Add_Extension
1653 (Tree : Project_Tree_Ref;
1654 Base_Main : String) return Prj.Source_Id
1656 Spec_Source : Prj.Source_Id := No_Source;
1657 Source : Prj.Source_Id;
1658 Iter : Source_Iterator;
1659 Suffix : File_Name_Type;
1661 begin
1662 Source := No_Source;
1663 Iter := For_Each_Source (Tree); -- In all projects
1664 loop
1665 Source := Prj.Element (Iter);
1666 exit when Source = No_Source;
1668 if Source.Kind = Impl then
1669 Get_Name_String (Source.File);
1671 if Name_Len > Base_Main'Length
1672 and then Name_Buffer (1 .. Base_Main'Length) = Base_Main
1673 then
1674 Suffix :=
1675 Source.Language.Config.Naming_Data.Body_Suffix;
1677 if Suffix /= No_File then
1678 declare
1679 Suffix_Str : String := Get_Name_String (Suffix);
1680 begin
1681 Canonical_Case_File_Name (Suffix_Str);
1682 exit when
1683 Name_Buffer (Base_Main'Length + 1 .. Name_Len) =
1684 Suffix_Str;
1685 end;
1686 end if;
1687 end if;
1689 elsif Source.Kind = Spec
1690 and then Source.Language.Config.Kind = Unit_Based
1691 then
1692 -- An Ada spec needs to be taken into account unless there
1693 -- is also a body. So we delay the decision for them.
1695 Get_Name_String (Source.File);
1697 if Name_Len > Base_Main'Length
1698 and then Name_Buffer (1 .. Base_Main'Length) = Base_Main
1699 then
1700 Suffix := Source.Language.Config.Naming_Data.Spec_Suffix;
1702 if Suffix /= No_File then
1703 declare
1704 Suffix_Str : String := Get_Name_String (Suffix);
1706 begin
1707 Canonical_Case_File_Name (Suffix_Str);
1709 if Name_Buffer (Base_Main'Length + 1 .. Name_Len) =
1710 Suffix_Str
1711 then
1712 Spec_Source := Source;
1713 end if;
1714 end;
1715 end if;
1716 end if;
1717 end if;
1719 Next (Iter);
1720 end loop;
1722 if Source = No_Source then
1723 Source := Spec_Source;
1724 end if;
1726 return Source;
1727 end Find_File_Add_Extension;
1729 -----------------
1730 -- Do_Complete --
1731 -----------------
1733 procedure Do_Complete
1734 (Project : Project_Id; Tree : Project_Tree_Ref)
1736 J : Integer;
1738 begin
1739 if Mains.Number_Of_Mains (Tree) > 0
1740 or else Mains.Count_Of_Mains_With_No_Tree > 0
1741 then
1742 -- Traverse in reverse order, since in the case of multi-unit
1743 -- files we will be adding extra files at the end, and there's
1744 -- no need to process them in turn.
1746 J := Names.Last;
1747 Main_Loop : loop
1748 declare
1749 File : Main_Info := Names.Table (J);
1750 Main_Id : File_Name_Type := File.File;
1751 Main : constant String :=
1752 Get_Name_String (Main_Id);
1753 Base : constant String := Base_Name (Main);
1754 Source : Prj.Source_Id := No_Source;
1755 Is_Absolute : Boolean := False;
1757 begin
1758 if Base /= Main then
1759 Is_Absolute := True;
1761 if Is_Absolute_Path (Main) then
1762 Main_Id := Create_Name (Base);
1764 -- Not an absolute path
1766 else
1767 -- Always resolve links here, so that users can be
1768 -- specify any name on the command line. If the
1769 -- project itself uses links, the user will be
1770 -- using -eL anyway, and thus files are also stored
1771 -- with resolved names.
1773 declare
1774 Absolute : constant String :=
1775 Normalize_Pathname
1776 (Name => Main,
1777 Directory => "",
1778 Resolve_Links => True,
1779 Case_Sensitive => False);
1780 begin
1781 File.File := Create_Name (Absolute);
1782 Main_Id := Create_Name (Base);
1783 end;
1784 end if;
1785 end if;
1787 -- If no project or tree was specified for the main, it
1788 -- came from the command line.
1789 -- Note that the assignments below will not modify inside
1790 -- the table itself.
1792 if File.Project = null then
1793 File.Project := Project;
1794 end if;
1796 if File.Tree = null then
1797 File.Tree := Tree;
1798 end if;
1800 if File.Source = null then
1801 if Current_Verbosity = High then
1802 Debug_Output
1803 ("search for main """ & Main
1804 & '"' & File.Index'Img & " in "
1805 & Get_Name_String (Debug_Name (File.Tree))
1806 & ", project", Project.Name);
1807 end if;
1809 -- First, look for the main as specified. We need to
1810 -- search for the base name though, and if needed
1811 -- check later that we found the correct file.
1813 declare
1814 Sources : constant Source_Ids :=
1815 Find_All_Sources
1816 (In_Tree => File.Tree,
1817 Project => File.Project,
1818 Base_Name => Main_Id,
1819 Index => File.Index,
1820 In_Imported_Only => True);
1822 begin
1823 if Is_Absolute then
1824 for J in Sources'Range loop
1825 if File_Name_Type (Sources (J).Path.Name) =
1826 File.File
1827 then
1828 Source := Sources (J);
1829 exit;
1830 end if;
1831 end loop;
1833 elsif Sources'Length > 1 then
1835 -- This is only allowed if the units are from
1836 -- the same multi-unit source file.
1838 Source := Sources (1);
1840 for J in 2 .. Sources'Last loop
1841 if Sources (J).Path /= Source.Path
1842 or else Sources (J).Index = Source.Index
1843 then
1844 Error_Msg_File_1 := Main_Id;
1845 Prj.Err.Error_Msg
1846 (Flags, "several main sources {",
1847 No_Location, File.Project);
1848 exit Main_Loop;
1849 end if;
1850 end loop;
1852 elsif Sources'Length = 1 then
1853 Source := Sources (Sources'First);
1854 end if;
1855 end;
1857 if Source = No_Source then
1858 Source := Find_File_Add_Extension
1859 (File.Tree, Get_Name_String (Main_Id));
1860 end if;
1862 if Is_Absolute
1863 and then Source /= No_Source
1864 and then
1865 File_Name_Type (Source.Path.Name) /= File.File
1866 then
1867 Debug_Output
1868 ("Found a non-matching file",
1869 Name_Id (Source.Path.Display_Name));
1870 Source := No_Source;
1871 end if;
1873 if Source /= No_Source then
1874 if not Is_Allowed_Language
1875 (Source.Language.Name)
1876 then
1877 -- Remove any main that is not in the list of
1878 -- restricted languages.
1880 Names.Table (J .. Names.Last - 1) :=
1881 Names.Table (J + 1 .. Names.Last);
1882 Names.Set_Last (Names.Last - 1);
1884 else
1885 -- If we have found a multi-unit source file but
1886 -- did not specify an index initially, we'll
1887 -- need to compile all the units from the same
1888 -- source file.
1890 if Source.Index /= 0 and then File.Index = 0 then
1891 Add_Multi_Unit_Sources (File.Tree, Source);
1892 end if;
1894 -- Now update the original Main, otherwise it
1895 -- will be reported as not found.
1897 Debug_Output
1898 ("found main in project", Source.Project.Name);
1899 Names.Table (J).File := Source.File;
1900 Names.Table (J).Project := Source.Project;
1902 if Names.Table (J).Tree = null then
1903 Names.Table (J).Tree := File.Tree;
1905 Builder_Data (File.Tree).Number_Of_Mains :=
1906 Builder_Data (File.Tree).Number_Of_Mains
1907 + 1;
1908 Mains.Count_Of_Mains_With_No_Tree :=
1909 Mains.Count_Of_Mains_With_No_Tree - 1;
1910 end if;
1912 Names.Table (J).Source := Source;
1913 Names.Table (J).Index := Source.Index;
1914 end if;
1916 elsif File.Location /= No_Location then
1918 -- If the main is declared in package Builder of
1919 -- the main project, report an error. If the main
1920 -- is on the command line, it may be a main from
1921 -- another project, so do nothing: if the main does
1922 -- not exist in another project, an error will be
1923 -- reported later.
1925 Error_Msg_File_1 := Main_Id;
1926 Error_Msg_Name_1 := File.Project.Name;
1927 Prj.Err.Error_Msg
1928 (Flags, "{ is not a source of project %%",
1929 File.Location, File.Project);
1930 end if;
1931 end if;
1932 end;
1934 J := J - 1;
1935 exit Main_Loop when J < Names.First;
1936 end loop Main_Loop;
1937 end if;
1939 if Total_Errors_Detected > 0 then
1940 Fail_Program (Tree, "problems with main sources");
1941 end if;
1942 end Do_Complete;
1944 -- Start of processing for Complete_Mains
1946 begin
1947 Complete_All (Root_Project, Project_Tree);
1949 if Mains.Count_Of_Mains_With_No_Tree > 0 then
1950 for J in Names.First .. Names.Last loop
1951 if Names.Table (J).Source = No_Source then
1952 Fail_Program
1953 (Project_Tree, '"' & Get_Name_String (Names.Table (J).File)
1954 & """ is not a source of any project");
1955 end if;
1956 end loop;
1957 end if;
1958 end Complete_Mains;
1960 ------------
1961 -- Delete --
1962 ------------
1964 procedure Delete is
1965 begin
1966 Names.Set_Last (0);
1967 Mains.Reset;
1968 end Delete;
1970 -----------------------
1971 -- Fill_From_Project --
1972 -----------------------
1974 procedure Fill_From_Project
1975 (Root_Project : Project_Id;
1976 Project_Tree : Project_Tree_Ref)
1978 procedure Add_Mains_From_Project
1979 (Project : Project_Id;
1980 Tree : Project_Tree_Ref);
1981 -- Add the main units from this project into Mains.
1982 -- This takes into account the aggregated projects
1984 ----------------------------
1985 -- Add_Mains_From_Project --
1986 ----------------------------
1988 procedure Add_Mains_From_Project
1989 (Project : Project_Id;
1990 Tree : Project_Tree_Ref)
1992 List : String_List_Id;
1993 Element : String_Element;
1995 begin
1996 if Number_Of_Mains (Tree) = 0
1997 and then Mains.Count_Of_Mains_With_No_Tree = 0
1998 then
1999 Debug_Output ("Add_Mains_From_Project", Project.Name);
2000 List := Project.Mains;
2002 if List /= Prj.Nil_String then
2004 -- The attribute Main is not an empty list. Get the mains in
2005 -- the list.
2007 while List /= Prj.Nil_String loop
2008 Element := Tree.Shared.String_Elements.Table (List);
2009 Debug_Output ("Add_Main", Element.Value);
2011 if Project.Library then
2012 Fail_Program
2013 (Tree,
2014 "cannot specify a main program "
2015 & "for a library project file");
2016 end if;
2018 Add_Main (Name => Get_Name_String (Element.Value),
2019 Index => Element.Index,
2020 Location => Element.Location,
2021 Project => Project,
2022 Tree => Tree);
2023 List := Element.Next;
2024 end loop;
2025 end if;
2026 end if;
2028 if Total_Errors_Detected > 0 then
2029 Fail_Program (Tree, "problems with main sources");
2030 end if;
2031 end Add_Mains_From_Project;
2033 procedure Fill_All is new For_Project_And_Aggregated
2034 (Add_Mains_From_Project);
2036 -- Start of processing for Fill_From_Project
2038 begin
2039 Fill_All (Root_Project, Project_Tree);
2040 end Fill_From_Project;
2042 ---------------
2043 -- Next_Main --
2044 ---------------
2046 function Next_Main return String is
2047 Info : constant Main_Info := Next_Main;
2048 begin
2049 if Info = No_Main_Info then
2050 return "";
2051 else
2052 return Get_Name_String (Info.File);
2053 end if;
2054 end Next_Main;
2056 function Next_Main return Main_Info is
2057 begin
2058 if Current >= Names.Last then
2059 return No_Main_Info;
2060 else
2061 Current := Current + 1;
2063 -- If not using projects, and in the gnatmake case, the main file
2064 -- may have not have the extension. Try ".adb" first then ".ads"
2066 if Names.Table (Current).Project = No_Project then
2067 declare
2068 Orig_Main : constant File_Name_Type :=
2069 Names.Table (Current).File;
2070 Current_Main : File_Name_Type;
2072 begin
2073 if Strip_Suffix (Orig_Main) = Orig_Main then
2074 Get_Name_String (Orig_Main);
2075 Add_Str_To_Name_Buffer (".adb");
2076 Current_Main := Name_Find;
2078 if Full_Source_Name (Current_Main) = No_File then
2079 Get_Name_String (Orig_Main);
2080 Add_Str_To_Name_Buffer (".ads");
2081 Current_Main := Name_Find;
2083 if Full_Source_Name (Current_Main) /= No_File then
2084 Names.Table (Current).File := Current_Main;
2085 end if;
2087 else
2088 Names.Table (Current).File := Current_Main;
2089 end if;
2090 end if;
2091 end;
2092 end if;
2094 return Names.Table (Current);
2095 end if;
2096 end Next_Main;
2098 ---------------------
2099 -- Number_Of_Mains --
2100 ---------------------
2102 function Number_Of_Mains (Tree : Project_Tree_Ref) return Natural is
2103 begin
2104 if Tree = null then
2105 return Names.Last;
2106 else
2107 return Builder_Data (Tree).Number_Of_Mains;
2108 end if;
2109 end Number_Of_Mains;
2111 -----------
2112 -- Reset --
2113 -----------
2115 procedure Reset is
2116 begin
2117 Current := 0;
2118 end Reset;
2120 --------------------------
2121 -- Set_Multi_Unit_Index --
2122 --------------------------
2124 procedure Set_Multi_Unit_Index
2125 (Project_Tree : Project_Tree_Ref := null;
2126 Index : Int := 0)
2128 begin
2129 if Index /= 0 then
2130 if Names.Last = 0 then
2131 Fail_Program
2132 (Project_Tree,
2133 "cannot specify a multi-unit index but no main "
2134 & "on the command line");
2136 elsif Names.Last > 1 then
2137 Fail_Program
2138 (Project_Tree,
2139 "cannot specify several mains with a multi-unit index");
2141 else
2142 Names.Table (Names.Last).Index := Index;
2143 end if;
2144 end if;
2145 end Set_Multi_Unit_Index;
2147 end Mains;
2149 -----------------------
2150 -- Path_Or_File_Name --
2151 -----------------------
2153 function Path_Or_File_Name (Path : Path_Name_Type) return String is
2154 Path_Name : constant String := Get_Name_String (Path);
2155 begin
2156 if Debug.Debug_Flag_F then
2157 return File_Name (Path_Name);
2158 else
2159 return Path_Name;
2160 end if;
2161 end Path_Or_File_Name;
2163 -------------------
2164 -- Unit_Index_Of --
2165 -------------------
2167 function Unit_Index_Of (ALI_File : File_Name_Type) return Int is
2168 Start : Natural;
2169 Finish : Natural;
2170 Result : Int := 0;
2172 begin
2173 Get_Name_String (ALI_File);
2175 -- First, find the last dot
2177 Finish := Name_Len;
2179 while Finish >= 1 and then Name_Buffer (Finish) /= '.' loop
2180 Finish := Finish - 1;
2181 end loop;
2183 if Finish = 1 then
2184 return 0;
2185 end if;
2187 -- Now check that the dot is preceded by digits
2189 Start := Finish;
2190 Finish := Finish - 1;
2191 while Start >= 1 and then Name_Buffer (Start - 1) in '0' .. '9' loop
2192 Start := Start - 1;
2193 end loop;
2195 -- If there are no digits, or if the digits are not preceded by the
2196 -- character that precedes a unit index, this is not the ALI file of
2197 -- a unit in a multi-unit source.
2199 if Start > Finish
2200 or else Start = 1
2201 or else Name_Buffer (Start - 1) /= Multi_Unit_Index_Character
2202 then
2203 return 0;
2204 end if;
2206 -- Build the index from the digit(s)
2208 while Start <= Finish loop
2209 Result := Result * 10 +
2210 Character'Pos (Name_Buffer (Start)) - Character'Pos ('0');
2211 Start := Start + 1;
2212 end loop;
2214 return Result;
2215 end Unit_Index_Of;
2217 -----------------
2218 -- Verbose_Msg --
2219 -----------------
2221 procedure Verbose_Msg
2222 (N1 : Name_Id;
2223 S1 : String;
2224 N2 : Name_Id := No_Name;
2225 S2 : String := "";
2226 Prefix : String := " -> ";
2227 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
2229 begin
2230 if not Opt.Verbose_Mode
2231 or else Minimum_Verbosity > Opt.Verbosity_Level
2232 then
2233 return;
2234 end if;
2236 Write_Str (Prefix);
2237 Write_Str ("""");
2238 Write_Name (N1);
2239 Write_Str (""" ");
2240 Write_Str (S1);
2242 if N2 /= No_Name then
2243 Write_Str (" """);
2244 Write_Name (N2);
2245 Write_Str (""" ");
2246 end if;
2248 Write_Str (S2);
2249 Write_Eol;
2250 end Verbose_Msg;
2252 procedure Verbose_Msg
2253 (N1 : File_Name_Type;
2254 S1 : String;
2255 N2 : File_Name_Type := No_File;
2256 S2 : String := "";
2257 Prefix : String := " -> ";
2258 Minimum_Verbosity : Opt.Verbosity_Level_Type := Opt.Low)
2260 begin
2261 Verbose_Msg
2262 (Name_Id (N1), S1, Name_Id (N2), S2, Prefix, Minimum_Verbosity);
2263 end Verbose_Msg;
2265 -----------
2266 -- Queue --
2267 -----------
2269 package body Queue is
2271 type Q_Record is record
2272 Info : Source_Info;
2273 Processed : Boolean;
2274 end record;
2276 package Q is new Table.Table
2277 (Table_Component_Type => Q_Record,
2278 Table_Index_Type => Natural,
2279 Table_Low_Bound => 1,
2280 Table_Initial => 1000,
2281 Table_Increment => 100,
2282 Table_Name => "Makeutl.Queue.Q");
2283 -- This is the actual Queue
2285 package Busy_Obj_Dirs is new GNAT.HTable.Simple_HTable
2286 (Header_Num => Prj.Header_Num,
2287 Element => Boolean,
2288 No_Element => False,
2289 Key => Path_Name_Type,
2290 Hash => Hash,
2291 Equal => "=");
2293 type Mark_Key is record
2294 File : File_Name_Type;
2295 Index : Int;
2296 end record;
2297 -- Identify either a mono-unit source (when Index = 0) or a specific
2298 -- unit (index = 1's origin index of unit) in a multi-unit source.
2300 Max_Mask_Num : constant := 2048;
2301 subtype Mark_Num is Union_Id range 0 .. Max_Mask_Num - 1;
2303 function Hash (Key : Mark_Key) return Mark_Num;
2305 package Marks is new GNAT.HTable.Simple_HTable
2306 (Header_Num => Mark_Num,
2307 Element => Boolean,
2308 No_Element => False,
2309 Key => Mark_Key,
2310 Hash => Hash,
2311 Equal => "=");
2312 -- A hash table to keep tracks of the marked units.
2313 -- These are the units that have already been processed, when using the
2314 -- gnatmake format. When using the gprbuild format, we can directly
2315 -- store in the source_id whether the file has already been processed.
2317 procedure Mark (Source_File : File_Name_Type; Index : Int := 0);
2318 -- Mark a unit, identified by its source file and, when Index is not 0,
2319 -- the index of the unit in the source file. Marking is used to signal
2320 -- that the unit has already been inserted in the Q.
2322 function Is_Marked
2323 (Source_File : File_Name_Type;
2324 Index : Int := 0) return Boolean;
2325 -- Returns True if the unit was previously marked
2327 Q_Processed : Natural := 0;
2328 Q_Initialized : Boolean := False;
2330 Q_First : Natural := 1;
2331 -- Points to the first valid element in the queue
2333 One_Queue_Per_Obj_Dir : Boolean := False;
2334 -- See parameter to Initialize
2336 function Available_Obj_Dir (S : Source_Info) return Boolean;
2337 -- Whether the object directory for S is available for a build
2339 procedure Debug_Display (S : Source_Info);
2340 -- A debug display for S
2342 function Was_Processed (S : Source_Info) return Boolean;
2343 -- Whether S has already been processed. This marks the source as
2344 -- processed, if it hasn't already been processed.
2346 function Insert_No_Roots (Source : Source_Info) return Boolean;
2347 -- Insert Source, but do not look for its roots (see doc for Insert)
2349 -------------------
2350 -- Was_Processed --
2351 -------------------
2353 function Was_Processed (S : Source_Info) return Boolean is
2354 begin
2355 case S.Format is
2356 when Format_Gprbuild =>
2357 if S.Id.In_The_Queue then
2358 return True;
2359 end if;
2361 S.Id.In_The_Queue := True;
2363 when Format_Gnatmake =>
2364 if Is_Marked (S.File, S.Index) then
2365 return True;
2366 end if;
2368 Mark (S.File, Index => S.Index);
2369 end case;
2371 return False;
2372 end Was_Processed;
2374 -----------------------
2375 -- Available_Obj_Dir --
2376 -----------------------
2378 function Available_Obj_Dir (S : Source_Info) return Boolean is
2379 begin
2380 case S.Format is
2381 when Format_Gprbuild =>
2382 return not Busy_Obj_Dirs.Get
2383 (S.Id.Project.Object_Directory.Name);
2385 when Format_Gnatmake =>
2386 return S.Project = No_Project
2387 or else
2388 not Busy_Obj_Dirs.Get (S.Project.Object_Directory.Name);
2389 end case;
2390 end Available_Obj_Dir;
2392 -------------------
2393 -- Debug_Display --
2394 -------------------
2396 procedure Debug_Display (S : Source_Info) is
2397 begin
2398 case S.Format is
2399 when Format_Gprbuild =>
2400 Write_Name (S.Id.File);
2402 if S.Id.Index /= 0 then
2403 Write_Str (", ");
2404 Write_Int (S.Id.Index);
2405 end if;
2407 when Format_Gnatmake =>
2408 Write_Name (S.File);
2410 if S.Index /= 0 then
2411 Write_Str (", ");
2412 Write_Int (S.Index);
2413 end if;
2414 end case;
2415 end Debug_Display;
2417 ----------
2418 -- Hash --
2419 ----------
2421 function Hash (Key : Mark_Key) return Mark_Num is
2422 begin
2423 return Union_Id (Key.File) mod Max_Mask_Num;
2424 end Hash;
2426 ---------------
2427 -- Is_Marked --
2428 ---------------
2430 function Is_Marked
2431 (Source_File : File_Name_Type;
2432 Index : Int := 0) return Boolean
2434 begin
2435 return Marks.Get (K => (File => Source_File, Index => Index));
2436 end Is_Marked;
2438 ----------
2439 -- Mark --
2440 ----------
2442 procedure Mark (Source_File : File_Name_Type; Index : Int := 0) is
2443 begin
2444 Marks.Set (K => (File => Source_File, Index => Index), E => True);
2445 end Mark;
2447 -------------
2448 -- Extract --
2449 -------------
2451 procedure Extract
2452 (Found : out Boolean;
2453 Source : out Source_Info)
2455 begin
2456 Found := False;
2458 if One_Queue_Per_Obj_Dir then
2459 for J in Q_First .. Q.Last loop
2460 if not Q.Table (J).Processed
2461 and then Available_Obj_Dir (Q.Table (J).Info)
2462 then
2463 Found := True;
2464 Source := Q.Table (J).Info;
2465 Q.Table (J).Processed := True;
2467 if J = Q_First then
2468 while Q_First <= Q.Last
2469 and then Q.Table (Q_First).Processed
2470 loop
2471 Q_First := Q_First + 1;
2472 end loop;
2473 end if;
2475 exit;
2476 end if;
2477 end loop;
2479 elsif Q_First <= Q.Last then
2480 Source := Q.Table (Q_First).Info;
2481 Q.Table (Q_First).Processed := True;
2482 Q_First := Q_First + 1;
2483 Found := True;
2484 end if;
2486 if Found then
2487 Q_Processed := Q_Processed + 1;
2488 end if;
2490 if Found and then Debug.Debug_Flag_Q then
2491 Write_Str (" Q := Q - [ ");
2492 Debug_Display (Source);
2493 Write_Str (" ]");
2494 Write_Eol;
2496 Write_Str (" Q_First =");
2497 Write_Int (Int (Q_First));
2498 Write_Eol;
2500 Write_Str (" Q.Last =");
2501 Write_Int (Int (Q.Last));
2502 Write_Eol;
2503 end if;
2504 end Extract;
2506 ---------------
2507 -- Processed --
2508 ---------------
2510 function Processed return Natural is
2511 begin
2512 return Q_Processed;
2513 end Processed;
2515 ----------------
2516 -- Initialize --
2517 ----------------
2519 procedure Initialize
2520 (Queue_Per_Obj_Dir : Boolean;
2521 Force : Boolean := False)
2523 begin
2524 if Force or else not Q_Initialized then
2525 Q_Initialized := True;
2527 for J in 1 .. Q.Last loop
2528 case Q.Table (J).Info.Format is
2529 when Format_Gprbuild =>
2530 Q.Table (J).Info.Id.In_The_Queue := False;
2531 when Format_Gnatmake =>
2532 null;
2533 end case;
2534 end loop;
2536 Q.Init;
2537 Q_Processed := 0;
2538 Q_First := 1;
2539 One_Queue_Per_Obj_Dir := Queue_Per_Obj_Dir;
2540 end if;
2541 end Initialize;
2543 ---------------------
2544 -- Insert_No_Roots --
2545 ---------------------
2547 function Insert_No_Roots (Source : Source_Info) return Boolean is
2548 begin
2549 pragma Assert
2550 (Source.Format = Format_Gnatmake or else Source.Id /= No_Source);
2552 -- Only insert in the Q if it is not already done, to avoid
2553 -- simultaneous compilations if -jnnn is used.
2555 if Was_Processed (Source) then
2556 return False;
2557 end if;
2559 -- For gprbuild, check if a source has already been inserted in the
2560 -- queue from the same project in a different project tree.
2562 if Source.Format = Format_Gprbuild then
2563 for J in 1 .. Q.Last loop
2564 if Source.Id.Path.Name = Q.Table (J).Info.Id.Path.Name
2565 and then Source.Id.Index = Q.Table (J).Info.Id.Index
2566 and then Source.Id.Project.Path.Name =
2567 Q.Table (J).Info.Id.Project.Path.Name
2568 then
2569 -- No need to insert this source in the queue, but still
2570 -- return True as we may need to insert its roots.
2572 return True;
2573 end if;
2574 end loop;
2575 end if;
2577 if Current_Verbosity = High then
2578 Write_Str ("Adding """);
2579 Debug_Display (Source);
2580 Write_Line (""" to the queue");
2581 end if;
2583 Q.Append (New_Val => (Info => Source, Processed => False));
2585 if Debug.Debug_Flag_Q then
2586 Write_Str (" Q := Q + [ ");
2587 Debug_Display (Source);
2588 Write_Str (" ] ");
2589 Write_Eol;
2591 Write_Str (" Q_First =");
2592 Write_Int (Int (Q_First));
2593 Write_Eol;
2595 Write_Str (" Q.Last =");
2596 Write_Int (Int (Q.Last));
2597 Write_Eol;
2598 end if;
2600 return True;
2601 end Insert_No_Roots;
2603 ------------
2604 -- Insert --
2605 ------------
2607 function Insert
2608 (Source : Source_Info;
2609 With_Roots : Boolean := False) return Boolean
2611 Root_Arr : Array_Element_Id;
2612 Roots : Variable_Value;
2613 List : String_List_Id;
2614 Elem : String_Element;
2615 Unit_Name : Name_Id;
2616 Pat_Root : Boolean;
2617 Root_Pattern : Regexp;
2618 Root_Found : Boolean;
2619 Roots_Found : Boolean;
2620 Root_Source : Prj.Source_Id;
2621 Iter : Source_Iterator;
2623 Dummy : Boolean;
2624 pragma Unreferenced (Dummy);
2626 begin
2627 if not Insert_No_Roots (Source) then
2629 -- Was already in the queue
2631 return False;
2632 end if;
2634 if With_Roots and then Source.Format = Format_Gprbuild then
2635 Debug_Output ("looking for roots of", Name_Id (Source.Id.File));
2637 Root_Arr :=
2638 Prj.Util.Value_Of
2639 (Name => Name_Roots,
2640 In_Arrays => Source.Id.Project.Decl.Arrays,
2641 Shared => Source.Tree.Shared);
2643 Roots :=
2644 Prj.Util.Value_Of
2645 (Index => Name_Id (Source.Id.File),
2646 Src_Index => 0,
2647 In_Array => Root_Arr,
2648 Shared => Source.Tree.Shared);
2650 -- If there is no roots for the specific main, try the language
2652 if Roots = Nil_Variable_Value then
2653 Roots :=
2654 Prj.Util.Value_Of
2655 (Index => Source.Id.Language.Name,
2656 Src_Index => 0,
2657 In_Array => Root_Arr,
2658 Shared => Source.Tree.Shared,
2659 Force_Lower_Case_Index => True);
2660 end if;
2662 -- Then try "*"
2664 if Roots = Nil_Variable_Value then
2665 Name_Len := 1;
2666 Name_Buffer (1) := '*';
2668 Roots :=
2669 Prj.Util.Value_Of
2670 (Index => Name_Find,
2671 Src_Index => 0,
2672 In_Array => Root_Arr,
2673 Shared => Source.Tree.Shared,
2674 Force_Lower_Case_Index => True);
2675 end if;
2677 if Roots = Nil_Variable_Value then
2678 Debug_Output (" -> no roots declared");
2680 else
2681 List := Roots.Values;
2683 Pattern_Loop :
2684 while List /= Nil_String loop
2685 Elem := Source.Tree.Shared.String_Elements.Table (List);
2686 Get_Name_String (Elem.Value);
2687 To_Lower (Name_Buffer (1 .. Name_Len));
2688 Unit_Name := Name_Find;
2690 -- Check if it is a unit name or a pattern
2692 Pat_Root := False;
2694 for J in 1 .. Name_Len loop
2695 if Name_Buffer (J) not in 'a' .. 'z' and then
2696 Name_Buffer (J) not in '0' .. '9' and then
2697 Name_Buffer (J) /= '_' and then
2698 Name_Buffer (J) /= '.'
2699 then
2700 Pat_Root := True;
2701 exit;
2702 end if;
2703 end loop;
2705 if Pat_Root then
2706 begin
2707 Root_Pattern :=
2708 Compile
2709 (Pattern => Name_Buffer (1 .. Name_Len),
2710 Glob => True);
2712 exception
2713 when Error_In_Regexp =>
2714 Err_Vars.Error_Msg_Name_1 := Unit_Name;
2715 Errutil.Error_Msg
2716 ("invalid pattern %", Roots.Location);
2717 exit Pattern_Loop;
2718 end;
2719 end if;
2721 Roots_Found := False;
2722 Iter := For_Each_Source (Source.Tree);
2724 Source_Loop :
2725 loop
2726 Root_Source := Prj.Element (Iter);
2727 exit Source_Loop when Root_Source = No_Source;
2729 Root_Found := False;
2730 if Pat_Root then
2731 Root_Found := Root_Source.Unit /= No_Unit_Index
2732 and then Match
2733 (Get_Name_String (Root_Source.Unit.Name),
2734 Root_Pattern);
2736 else
2737 Root_Found :=
2738 Root_Source.Unit /= No_Unit_Index
2739 and then Root_Source.Unit.Name = Unit_Name;
2740 end if;
2742 if Root_Found then
2743 case Root_Source.Kind is
2744 when Impl =>
2745 null;
2747 when Spec =>
2748 Root_Found := Other_Part (Root_Source) = No_Source;
2750 when Sep =>
2751 Root_Found := False;
2752 end case;
2753 end if;
2755 if Root_Found then
2756 Roots_Found := True;
2757 Debug_Output
2758 (" -> ", Name_Id (Root_Source.Display_File));
2759 Dummy := Queue.Insert_No_Roots
2760 (Source => (Format => Format_Gprbuild,
2761 Tree => Source.Tree,
2762 Id => Root_Source));
2764 Initialize_Source_Record (Root_Source);
2766 if Other_Part (Root_Source) /= No_Source then
2767 Initialize_Source_Record (Other_Part (Root_Source));
2768 end if;
2770 -- Save the root for the binder
2772 Source.Id.Roots := new Source_Roots'
2773 (Root => Root_Source,
2774 Next => Source.Id.Roots);
2776 exit Source_Loop when not Pat_Root;
2777 end if;
2779 Next (Iter);
2780 end loop Source_Loop;
2782 if not Roots_Found then
2783 if Pat_Root then
2784 if not Quiet_Output then
2785 Error_Msg_Name_1 := Unit_Name;
2786 Errutil.Error_Msg
2787 ("?no unit matches pattern %", Roots.Location);
2788 end if;
2790 else
2791 Errutil.Error_Msg
2792 ("Unit " & Get_Name_String (Unit_Name)
2793 & " does not exist", Roots.Location);
2794 end if;
2795 end if;
2797 List := Elem.Next;
2798 end loop Pattern_Loop;
2799 end if;
2800 end if;
2802 return True;
2803 end Insert;
2805 ------------
2806 -- Insert --
2807 ------------
2809 procedure Insert
2810 (Source : Source_Info;
2811 With_Roots : Boolean := False)
2813 Discard : Boolean;
2814 begin
2815 Discard := Insert (Source, With_Roots);
2816 end Insert;
2818 --------------
2819 -- Is_Empty --
2820 --------------
2822 function Is_Empty return Boolean is
2823 begin
2824 return Q_Processed >= Q.Last;
2825 end Is_Empty;
2827 ------------------------
2828 -- Is_Virtually_Empty --
2829 ------------------------
2831 function Is_Virtually_Empty return Boolean is
2832 begin
2833 if One_Queue_Per_Obj_Dir then
2834 for J in Q_First .. Q.Last loop
2835 if not Q.Table (J).Processed
2836 and then Available_Obj_Dir (Q.Table (J).Info)
2837 then
2838 return False;
2839 end if;
2840 end loop;
2842 return True;
2844 else
2845 return Is_Empty;
2846 end if;
2847 end Is_Virtually_Empty;
2849 ----------------------
2850 -- Set_Obj_Dir_Busy --
2851 ----------------------
2853 procedure Set_Obj_Dir_Busy (Obj_Dir : Path_Name_Type) is
2854 begin
2855 if One_Queue_Per_Obj_Dir then
2856 Busy_Obj_Dirs.Set (Obj_Dir, True);
2857 end if;
2858 end Set_Obj_Dir_Busy;
2860 ----------------------
2861 -- Set_Obj_Dir_Free --
2862 ----------------------
2864 procedure Set_Obj_Dir_Free (Obj_Dir : Path_Name_Type) is
2865 begin
2866 if One_Queue_Per_Obj_Dir then
2867 Busy_Obj_Dirs.Set (Obj_Dir, False);
2868 end if;
2869 end Set_Obj_Dir_Free;
2871 ----------
2872 -- Size --
2873 ----------
2875 function Size return Natural is
2876 begin
2877 return Q.Last;
2878 end Size;
2880 -------------
2881 -- Element --
2882 -------------
2884 function Element (Rank : Positive) return File_Name_Type is
2885 begin
2886 if Rank <= Q.Last then
2887 case Q.Table (Rank).Info.Format is
2888 when Format_Gprbuild =>
2889 return Q.Table (Rank).Info.Id.File;
2890 when Format_Gnatmake =>
2891 return Q.Table (Rank).Info.File;
2892 end case;
2893 else
2894 return No_File;
2895 end if;
2896 end Element;
2898 ------------------
2899 -- Remove_Marks --
2900 ------------------
2902 procedure Remove_Marks is
2903 begin
2904 Marks.Reset;
2905 end Remove_Marks;
2907 ----------------------------
2908 -- Insert_Project_Sources --
2909 ----------------------------
2911 procedure Insert_Project_Sources
2912 (Project : Project_Id;
2913 Project_Tree : Project_Tree_Ref;
2914 All_Projects : Boolean;
2915 Unique_Compile : Boolean)
2917 procedure Do_Insert (Project : Project_Id; Tree : Project_Tree_Ref);
2919 ---------------
2920 -- Do_Insert --
2921 ---------------
2923 procedure Do_Insert (Project : Project_Id; Tree : Project_Tree_Ref) is
2924 Unit_Based : constant Boolean :=
2925 Unique_Compile
2926 or else not Builder_Data (Tree).Closure_Needed;
2927 -- When Unit_Based is True, put in the queue all compilable
2928 -- sources including the unit based (Ada) one. When Unit_Based is
2929 -- False, put the Ada sources only when they are in a library
2930 -- project.
2932 Iter : Source_Iterator;
2933 Source : Prj.Source_Id;
2935 begin
2936 -- Nothing to do when "-u" was specified and some files were
2937 -- specified on the command line
2939 if Unique_Compile
2940 and then Mains.Number_Of_Mains (Tree) > 0
2941 then
2942 return;
2943 end if;
2945 Iter := For_Each_Source (Tree);
2946 loop
2947 Source := Prj.Element (Iter);
2948 exit when Source = No_Source;
2950 if Is_Allowed_Language (Source.Language.Name)
2951 and then Is_Compilable (Source)
2952 and then
2953 (All_Projects
2954 or else Is_Extending (Project, Source.Project))
2955 and then not Source.Locally_Removed
2956 and then Source.Replaced_By = No_Source
2957 and then
2958 (not Source.Project.Externally_Built
2959 or else
2960 (Is_Extending (Project, Source.Project)
2961 and then not Project.Externally_Built))
2962 and then Source.Kind /= Sep
2963 and then Source.Path /= No_Path_Information
2964 then
2965 if Source.Kind = Impl
2966 or else (Source.Unit /= No_Unit_Index
2967 and then Source.Kind = Spec
2968 and then (Other_Part (Source) = No_Source
2969 or else
2970 Other_Part (Source).Locally_Removed))
2971 then
2972 if (Unit_Based
2973 or else Source.Unit = No_Unit_Index
2974 or else Source.Project.Library)
2975 and then not Is_Subunit (Source)
2976 then
2977 Queue.Insert
2978 (Source => (Format => Format_Gprbuild,
2979 Tree => Tree,
2980 Id => Source));
2981 end if;
2982 end if;
2983 end if;
2985 Next (Iter);
2986 end loop;
2987 end Do_Insert;
2989 procedure Insert_All is new For_Project_And_Aggregated (Do_Insert);
2991 begin
2992 Insert_All (Project, Project_Tree);
2993 end Insert_Project_Sources;
2995 -------------------------------
2996 -- Insert_Withed_Sources_For --
2997 -------------------------------
2999 procedure Insert_Withed_Sources_For
3000 (The_ALI : ALI.ALI_Id;
3001 Project_Tree : Project_Tree_Ref;
3002 Excluding_Shared_SALs : Boolean := False)
3004 Sfile : File_Name_Type;
3005 Afile : File_Name_Type;
3006 Src_Id : Prj.Source_Id;
3008 begin
3009 -- Insert in the queue the unmarked source files (i.e. those which
3010 -- have never been inserted in the queue and hence never considered).
3012 for J in ALI.ALIs.Table (The_ALI).First_Unit ..
3013 ALI.ALIs.Table (The_ALI).Last_Unit
3014 loop
3015 for K in ALI.Units.Table (J).First_With ..
3016 ALI.Units.Table (J).Last_With
3017 loop
3018 Sfile := ALI.Withs.Table (K).Sfile;
3020 -- Skip generics
3022 if Sfile /= No_File then
3023 Afile := ALI.Withs.Table (K).Afile;
3025 Src_Id := Source_Files_Htable.Get
3026 (Project_Tree.Source_Files_HT, Sfile);
3027 while Src_Id /= No_Source loop
3028 Initialize_Source_Record (Src_Id);
3030 if Is_Compilable (Src_Id)
3031 and then Src_Id.Dep_Name = Afile
3032 then
3033 case Src_Id.Kind is
3034 when Spec =>
3035 declare
3036 Bdy : constant Prj.Source_Id :=
3037 Other_Part (Src_Id);
3038 begin
3039 if Bdy /= No_Source
3040 and then not Bdy.Locally_Removed
3041 then
3042 Src_Id := Other_Part (Src_Id);
3043 end if;
3044 end;
3046 when Impl =>
3047 if Is_Subunit (Src_Id) then
3048 Src_Id := No_Source;
3049 end if;
3051 when Sep =>
3052 Src_Id := No_Source;
3053 end case;
3055 exit;
3056 end if;
3058 Src_Id := Src_Id.Next_With_File_Name;
3059 end loop;
3061 -- If Excluding_Shared_SALs is True, do not insert in the
3062 -- queue the sources of a shared Stand-Alone Library.
3064 if Src_Id /= No_Source
3065 and then (not Excluding_Shared_SALs
3066 or else Src_Id.Project.Standalone_Library = No
3067 or else Src_Id.Project.Library_Kind = Static)
3068 then
3069 Queue.Insert
3070 (Source => (Format => Format_Gprbuild,
3071 Tree => Project_Tree,
3072 Id => Src_Id));
3073 end if;
3074 end if;
3075 end loop;
3076 end loop;
3077 end Insert_Withed_Sources_For;
3079 end Queue;
3081 ----------
3082 -- Free --
3083 ----------
3085 procedure Free (Data : in out Builder_Project_Tree_Data) is
3086 procedure Unchecked_Free is new Ada.Unchecked_Deallocation
3087 (Binding_Data_Record, Binding_Data);
3089 TmpB, Binding : Binding_Data := Data.Binding;
3091 begin
3092 while Binding /= null loop
3093 TmpB := Binding.Next;
3094 Unchecked_Free (Binding);
3095 Binding := TmpB;
3096 end loop;
3097 end Free;
3099 ------------------
3100 -- Builder_Data --
3101 ------------------
3103 function Builder_Data
3104 (Tree : Project_Tree_Ref) return Builder_Data_Access
3106 begin
3107 if Tree.Appdata = null then
3108 Tree.Appdata := new Builder_Project_Tree_Data;
3109 end if;
3111 return Builder_Data_Access (Tree.Appdata);
3112 end Builder_Data;
3114 --------------------------------
3115 -- Compute_Compilation_Phases --
3116 --------------------------------
3118 procedure Compute_Compilation_Phases
3119 (Tree : Project_Tree_Ref;
3120 Root_Project : Project_Id;
3121 Option_Unique_Compile : Boolean := False; -- Was "-u" specified ?
3122 Option_Compile_Only : Boolean := False; -- Was "-c" specified ?
3123 Option_Bind_Only : Boolean := False;
3124 Option_Link_Only : Boolean := False)
3126 procedure Do_Compute (Project : Project_Id; Tree : Project_Tree_Ref);
3128 ----------------
3129 -- Do_Compute --
3130 ----------------
3132 procedure Do_Compute (Project : Project_Id; Tree : Project_Tree_Ref) is
3133 Data : constant Builder_Data_Access := Builder_Data (Tree);
3134 All_Phases : constant Boolean :=
3135 not Option_Compile_Only
3136 and then not Option_Bind_Only
3137 and then not Option_Link_Only;
3138 -- Whether the command line asked for all three phases. Depending on
3139 -- the project settings, we might still disable some of the phases.
3141 Has_Mains : constant Boolean := Data.Number_Of_Mains > 0;
3142 -- Whether there are some main units defined for this project tree
3143 -- (either from one of the projects, or from the command line)
3145 begin
3146 if Option_Unique_Compile then
3148 -- If -u or -U is specified on the command line, disregard any -c,
3149 -- -b or -l switch: only perform compilation.
3151 Data.Closure_Needed := False;
3152 Data.Need_Compilation := True;
3153 Data.Need_Binding := False;
3154 Data.Need_Linking := False;
3156 else
3157 Data.Closure_Needed := Has_Mains;
3158 Data.Need_Compilation := All_Phases or Option_Compile_Only;
3159 Data.Need_Binding := All_Phases or Option_Bind_Only;
3160 Data.Need_Linking := (All_Phases or Option_Link_Only)
3161 and Has_Mains;
3162 end if;
3164 if Current_Verbosity = High then
3165 Debug_Output ("compilation phases: "
3166 & " compile=" & Data.Need_Compilation'Img
3167 & " bind=" & Data.Need_Binding'Img
3168 & " link=" & Data.Need_Linking'Img
3169 & " closure=" & Data.Closure_Needed'Img
3170 & " mains=" & Data.Number_Of_Mains'Img,
3171 Project.Name);
3172 end if;
3173 end Do_Compute;
3175 procedure Compute_All is new For_Project_And_Aggregated (Do_Compute);
3177 begin
3178 Compute_All (Root_Project, Tree);
3179 end Compute_Compilation_Phases;
3181 ------------------------------
3182 -- Compute_Builder_Switches --
3183 ------------------------------
3185 procedure Compute_Builder_Switches
3186 (Project_Tree : Project_Tree_Ref;
3187 Env : in out Prj.Tree.Environment;
3188 Main_Project : Project_Id;
3189 Only_For_Lang : Name_Id := No_Name)
3191 Builder_Package : constant Package_Id :=
3192 Value_Of (Name_Builder, Main_Project.Decl.Packages,
3193 Project_Tree.Shared);
3195 Global_Compilation_Array : Array_Element_Id;
3196 Global_Compilation_Elem : Array_Element;
3197 Global_Compilation_Switches : Variable_Value;
3199 Default_Switches_Array : Array_Id;
3201 Builder_Switches_Lang : Name_Id := No_Name;
3203 List : String_List_Id;
3204 Element : String_Element;
3206 Index : Name_Id;
3207 Source : Prj.Source_Id;
3209 Lang : Name_Id := No_Name; -- language index for Switches
3210 Switches_For_Lang : Variable_Value := Nil_Variable_Value;
3211 -- Value of Builder'Default_Switches(lang)
3213 Name : Name_Id := No_Name; -- main file index for Switches
3214 Switches_For_Main : Variable_Value := Nil_Variable_Value;
3215 -- Switches for a specific main. When there are several mains, Name is
3216 -- set to No_Name, and Switches_For_Main might be left with an actual
3217 -- value (so that we can display a warning that it was ignored).
3219 Other_Switches : Variable_Value := Nil_Variable_Value;
3220 -- Value of Builder'Switches(others)
3222 Defaults : Variable_Value := Nil_Variable_Value;
3224 Switches : Variable_Value := Nil_Variable_Value;
3225 -- The computed builder switches
3227 Success : Boolean := False;
3228 begin
3229 if Builder_Package /= No_Package then
3230 Mains.Reset;
3232 -- If there is no main, and there is only one compilable language,
3233 -- use this language as the switches index.
3235 if Mains.Number_Of_Mains (Project_Tree) = 0 then
3236 if Only_For_Lang = No_Name then
3237 declare
3238 Language : Language_Ptr := Main_Project.Languages;
3240 begin
3241 while Language /= No_Language_Index loop
3242 if Language.Config.Compiler_Driver /= No_File
3243 and then Language.Config.Compiler_Driver /= Empty_File
3244 then
3245 if Lang /= No_Name then
3246 Lang := No_Name;
3247 exit;
3248 else
3249 Lang := Language.Name;
3250 end if;
3251 end if;
3252 Language := Language.Next;
3253 end loop;
3254 end;
3255 else
3256 Lang := Only_For_Lang;
3257 end if;
3259 else
3260 for Index in 1 .. Mains.Number_Of_Mains (Project_Tree) loop
3261 Source := Mains.Next_Main.Source;
3263 if Source /= No_Source then
3264 if Switches_For_Main = Nil_Variable_Value then
3265 Switches_For_Main := Value_Of
3266 (Name => Name_Id (Source.File),
3267 Attribute_Or_Array_Name => Name_Switches,
3268 In_Package => Builder_Package,
3269 Shared => Project_Tree.Shared,
3270 Force_Lower_Case_Index => False,
3271 Allow_Wildcards => True);
3273 -- If not found, try without extension.
3274 -- That's because gnatmake accepts truncated file names
3275 -- in Builder'Switches
3277 if Switches_For_Main = Nil_Variable_Value
3278 and then Source.Unit /= null
3279 then
3280 Switches_For_Main := Value_Of
3281 (Name => Source.Unit.Name,
3282 Attribute_Or_Array_Name => Name_Switches,
3283 In_Package => Builder_Package,
3284 Shared => Project_Tree.Shared,
3285 Force_Lower_Case_Index => False,
3286 Allow_Wildcards => True);
3287 end if;
3288 end if;
3290 if Index = 1 then
3291 Lang := Source.Language.Name;
3292 Name := Name_Id (Source.File);
3293 else
3294 Name := No_Name; -- Can't use main specific switches
3296 if Lang /= Source.Language.Name then
3297 Lang := No_Name;
3298 end if;
3299 end if;
3300 end if;
3301 end loop;
3302 end if;
3304 Global_Compilation_Array := Value_Of
3305 (Name => Name_Global_Compilation_Switches,
3306 In_Arrays => Project_Tree.Shared.Packages.Table
3307 (Builder_Package).Decl.Arrays,
3308 Shared => Project_Tree.Shared);
3310 Default_Switches_Array :=
3311 Project_Tree.Shared.Packages.Table (Builder_Package).Decl.Arrays;
3313 while Default_Switches_Array /= No_Array
3314 and then
3315 Project_Tree.Shared.Arrays.Table (Default_Switches_Array).Name /=
3316 Name_Default_Switches
3317 loop
3318 Default_Switches_Array :=
3319 Project_Tree.Shared.Arrays.Table (Default_Switches_Array).Next;
3320 end loop;
3322 if Global_Compilation_Array /= No_Array_Element
3323 and then Default_Switches_Array /= No_Array
3324 then
3325 Prj.Err.Error_Msg
3326 (Env.Flags,
3327 "Default_Switches forbidden in presence of "
3328 & "Global_Compilation_Switches. Use Switches instead.",
3329 Project_Tree.Shared.Arrays.Table
3330 (Default_Switches_Array).Location);
3331 Fail_Program
3332 (Project_Tree, "*** illegal combination of Builder attributes");
3333 end if;
3335 if Lang /= No_Name then
3336 Switches_For_Lang := Prj.Util.Value_Of
3337 (Name => Lang,
3338 Index => 0,
3339 Attribute_Or_Array_Name => Name_Switches,
3340 In_Package => Builder_Package,
3341 Shared => Project_Tree.Shared,
3342 Force_Lower_Case_Index => True);
3344 Defaults := Prj.Util.Value_Of
3345 (Name => Lang,
3346 Index => 0,
3347 Attribute_Or_Array_Name => Name_Default_Switches,
3348 In_Package => Builder_Package,
3349 Shared => Project_Tree.Shared,
3350 Force_Lower_Case_Index => True);
3351 end if;
3353 Other_Switches := Prj.Util.Value_Of
3354 (Name => All_Other_Names,
3355 Index => 0,
3356 Attribute_Or_Array_Name => Name_Switches,
3357 In_Package => Builder_Package,
3358 Shared => Project_Tree.Shared);
3360 if not Quiet_Output
3361 and then Mains.Number_Of_Mains (Project_Tree) > 1
3362 and then Switches_For_Main /= Nil_Variable_Value
3363 then
3364 -- More than one main, but we had main-specific switches that
3365 -- are ignored.
3367 if Switches_For_Lang /= Nil_Variable_Value then
3368 Write_Line
3369 ("Warning: using Builder'Switches("""
3370 & Get_Name_String (Lang)
3371 & """), as there are several mains");
3373 elsif Other_Switches /= Nil_Variable_Value then
3374 Write_Line
3375 ("Warning: using Builder'Switches(others), "
3376 & "as there are several mains");
3378 elsif Defaults /= Nil_Variable_Value then
3379 Write_Line
3380 ("Warning: using Builder'Default_Switches("""
3381 & Get_Name_String (Lang)
3382 & """), as there are several mains");
3383 else
3384 Write_Line
3385 ("Warning: using no switches from package "
3386 & "Builder, as there are several mains");
3387 end if;
3388 end if;
3390 Builder_Switches_Lang := Lang;
3392 if Name /= No_Name then
3393 -- Get the switches for the single main
3394 Switches := Switches_For_Main;
3395 end if;
3397 if Switches = Nil_Variable_Value or else Switches.Default then
3398 -- Get the switches for the common language of the mains
3399 Switches := Switches_For_Lang;
3400 end if;
3402 if Switches = Nil_Variable_Value or else Switches.Default then
3403 Switches := Other_Switches;
3404 end if;
3406 -- For backward compatibility with gnatmake, if no Switches
3407 -- are declared, check for Default_Switches (<language>).
3409 if Switches = Nil_Variable_Value or else Switches.Default then
3410 Switches := Defaults;
3411 end if;
3413 -- If switches have been found, scan them
3415 if Switches /= Nil_Variable_Value and then not Switches.Default then
3416 List := Switches.Values;
3418 while List /= Nil_String loop
3419 Element := Project_Tree.Shared.String_Elements.Table (List);
3420 Get_Name_String (Element.Value);
3422 if Name_Len /= 0 then
3423 declare
3424 -- Add_Switch might itself be using the name_buffer, so
3425 -- we make a temporary here.
3426 Switch : constant String := Name_Buffer (1 .. Name_Len);
3427 begin
3428 Success := Add_Switch
3429 (Switch => Switch,
3430 For_Lang => Builder_Switches_Lang,
3431 For_Builder => True,
3432 Has_Global_Compilation_Switches =>
3433 Global_Compilation_Array /= No_Array_Element);
3434 end;
3436 if not Success then
3437 for J in reverse 1 .. Name_Len loop
3438 Name_Buffer (J + J) := Name_Buffer (J);
3439 Name_Buffer (J + J - 1) := ''';
3440 end loop;
3442 Name_Len := Name_Len + Name_Len;
3444 Prj.Err.Error_Msg
3445 (Env.Flags,
3446 '"' & Name_Buffer (1 .. Name_Len)
3447 & """ is not a builder switch. Consider moving "
3448 & "it to Global_Compilation_Switches.",
3449 Element.Location);
3450 Fail_Program
3451 (Project_Tree,
3452 "*** illegal switch """
3453 & Get_Name_String (Element.Value) & '"');
3454 end if;
3455 end if;
3457 List := Element.Next;
3458 end loop;
3459 end if;
3461 -- Reset the Builder Switches language
3463 Builder_Switches_Lang := No_Name;
3465 -- Take into account attributes Global_Compilation_Switches
3467 while Global_Compilation_Array /= No_Array_Element loop
3468 Global_Compilation_Elem :=
3469 Project_Tree.Shared.Array_Elements.Table
3470 (Global_Compilation_Array);
3472 Get_Name_String (Global_Compilation_Elem.Index);
3473 To_Lower (Name_Buffer (1 .. Name_Len));
3474 Index := Name_Find;
3476 if Only_For_Lang = No_Name or else Index = Only_For_Lang then
3477 Global_Compilation_Switches := Global_Compilation_Elem.Value;
3479 if Global_Compilation_Switches /= Nil_Variable_Value
3480 and then not Global_Compilation_Switches.Default
3481 then
3482 -- We have found an attribute
3483 -- Global_Compilation_Switches for a language: put the
3484 -- switches in the appropriate table.
3486 List := Global_Compilation_Switches.Values;
3487 while List /= Nil_String loop
3488 Element :=
3489 Project_Tree.Shared.String_Elements.Table (List);
3491 if Element.Value /= No_Name then
3492 Success := Add_Switch
3493 (Switch => Get_Name_String (Element.Value),
3494 For_Lang => Index,
3495 For_Builder => False,
3496 Has_Global_Compilation_Switches =>
3497 Global_Compilation_Array /= No_Array_Element);
3498 end if;
3500 List := Element.Next;
3501 end loop;
3502 end if;
3503 end if;
3505 Global_Compilation_Array := Global_Compilation_Elem.Next;
3506 end loop;
3507 end if;
3508 end Compute_Builder_Switches;
3510 ---------------------
3511 -- Write_Path_File --
3512 ---------------------
3514 procedure Write_Path_File (FD : File_Descriptor) is
3515 Last : Natural;
3516 Status : Boolean;
3518 begin
3519 Name_Len := 0;
3521 for Index in Directories.First .. Directories.Last loop
3522 Add_Str_To_Name_Buffer (Get_Name_String (Directories.Table (Index)));
3523 Add_Char_To_Name_Buffer (ASCII.LF);
3524 end loop;
3526 Last := Write (FD, Name_Buffer (1)'Address, Name_Len);
3528 if Last = Name_Len then
3529 Close (FD, Status);
3530 else
3531 Status := False;
3532 end if;
3534 if not Status then
3535 Prj.Com.Fail ("could not write temporary file");
3536 end if;
3537 end Write_Path_File;
3539 end Makeutl;