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