OSDN Git Service

2008-04-08 Hristian Kirtchev <kirtchev@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / mlib-prj.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                            M L I B . P R J                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                     Copyright (C) 2001-2007, AdaCore                     --
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 Gnatvsn;  use Gnatvsn;
28 with MLib.Fil; use MLib.Fil;
29 with MLib.Tgt; use MLib.Tgt;
30 with MLib.Utl; use MLib.Utl;
31 with Opt;
32 with Output;   use Output;
33 with Prj.Com;  use Prj.Com;
34 with Prj.Env;  use Prj.Env;
35 with Prj.Util; use Prj.Util;
36 with Sinput.P;
37 with Snames;   use Snames;
38 with Switch;   use Switch;
39 with Table;
40 with Targparm; use Targparm;
41 with Tempdir;
42 with Types;    use Types;
43
44 with Ada.Characters.Handling;
45
46 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
47 with GNAT.HTable;
48 with Interfaces.C_Streams;      use Interfaces.C_Streams;
49 with System;                    use System;
50 with System.Case_Util;          use System.Case_Util;
51
52 package body MLib.Prj is
53
54    Prj_Add_Obj_Files : Types.Int;
55    pragma Import (C, Prj_Add_Obj_Files, "__gnat_prj_add_obj_files");
56    Add_Object_Files : constant Boolean := Prj_Add_Obj_Files /= 0;
57    --  Indicates if object files in pragmas Linker_Options (found in the
58    --  binder generated file) should be taken when linking a stand-alone
59    --  library. False for Windows, True for other platforms.
60
61    ALI_Suffix : constant String := ".ali";
62
63    B_Start : String_Ptr := new String'("b~");
64    --  Prefix of bind file, changed to b__ for VMS
65
66    S_Osinte_Ads : File_Name_Type := No_File;
67    --  Name_Id for "s-osinte.ads"
68
69    S_Dec_Ads : File_Name_Type := No_File;
70    --  Name_Id for "dec.ads"
71
72    G_Trasym_Ads : File_Name_Type := No_File;
73    --  Name_Id for "g-trasym.ads"
74
75    Arguments : String_List_Access := No_Argument;
76    --  Used to accumulate arguments for the invocation of gnatbind and of
77    --  the compiler. Also used to collect the interface ALI when copying
78    --  the ALI files to the library directory.
79
80    Argument_Number : Natural := 0;
81    --  Index of the last argument in Arguments
82
83    Initial_Argument_Max : constant := 10;
84
85    No_Main_String : aliased String := "-n";
86    No_Main : constant String_Access := No_Main_String'Access;
87
88    Output_Switch_String : aliased String := "-o";
89    Output_Switch : constant String_Access := Output_Switch_String'Access;
90
91    Compile_Switch_String : aliased String := "-c";
92    Compile_Switch : constant String_Access := Compile_Switch_String'Access;
93
94    Auto_Initialize : constant String := "-a";
95
96    --  List of objects to put inside the library
97
98    Object_Files : Argument_List_Access;
99
100    package Objects is new Table.Table
101      (Table_Name           => "Mlib.Prj.Objects",
102       Table_Component_Type => String_Access,
103       Table_Index_Type     => Natural,
104       Table_Low_Bound      => 1,
105       Table_Initial        => 50,
106       Table_Increment      => 100);
107
108    package Objects_Htable is new GNAT.HTable.Simple_HTable
109      (Header_Num => Header_Num,
110       Element    => Boolean,
111       No_Element => False,
112       Key        => Name_Id,
113       Hash       => Hash,
114       Equal      => "=");
115
116    --  List of ALI files
117
118    Ali_Files : Argument_List_Access;
119
120    package ALIs is new Table.Table
121      (Table_Name           => "Mlib.Prj.Alis",
122       Table_Component_Type => String_Access,
123       Table_Index_Type     => Natural,
124       Table_Low_Bound      => 1,
125       Table_Initial        => 50,
126       Table_Increment      => 100);
127
128    --  List of options set in the command line
129
130    Options : Argument_List_Access;
131
132    package Opts is new Table.Table
133      (Table_Name           => "Mlib.Prj.Opts",
134       Table_Component_Type => String_Access,
135       Table_Index_Type     => Natural,
136       Table_Low_Bound      => 1,
137       Table_Initial        => 5,
138       Table_Increment      => 100);
139
140    --  All the ALI file in the library
141
142    package Library_ALIs is new GNAT.HTable.Simple_HTable
143      (Header_Num => Header_Num,
144       Element    => Boolean,
145       No_Element => False,
146       Key        => File_Name_Type,
147       Hash       => Hash,
148       Equal      => "=");
149
150    --  The ALI files in the interface sets
151
152    package Interface_ALIs is new GNAT.HTable.Simple_HTable
153      (Header_Num => Header_Num,
154       Element    => Boolean,
155       No_Element => False,
156       Key        => File_Name_Type,
157       Hash       => Hash,
158       Equal      => "=");
159
160    --  The ALI files that have been processed to check if the corresponding
161    --  library unit is in the interface set.
162
163    package Processed_ALIs is new GNAT.HTable.Simple_HTable
164      (Header_Num => Header_Num,
165       Element    => Boolean,
166       No_Element => False,
167       Key        => File_Name_Type,
168       Hash       => Hash,
169       Equal      => "=");
170
171    --  The projects imported directly or indirectly
172
173    package Processed_Projects is new GNAT.HTable.Simple_HTable
174      (Header_Num => Header_Num,
175       Element    => Boolean,
176       No_Element => False,
177       Key        => Name_Id,
178       Hash       => Hash,
179       Equal      => "=");
180
181    --  The library projects imported directly or indirectly
182
183    package Library_Projs is new Table.Table (
184      Table_Component_Type => Project_Id,
185      Table_Index_Type     => Integer,
186      Table_Low_Bound      => 1,
187      Table_Initial        => 10,
188      Table_Increment      => 10,
189      Table_Name           => "Make.Library_Projs");
190
191    type Build_Mode_State is (None, Static, Dynamic, Relocatable);
192
193    procedure Add_Argument (S : String);
194    --  Add one argument to Arguments array, if array is full, double its size
195
196    function ALI_File_Name (Source : String) return String;
197    --  Return the ALI file name corresponding to a source
198
199    procedure Check (Filename : String);
200    --  Check if filename is a regular file. Fail if it is not
201
202    procedure Check_Context;
203    --  Check each object files in table Object_Files
204    --  Fail if any of them is not a regular file
205
206    procedure Copy_Interface_Sources
207      (For_Project : Project_Id;
208       In_Tree     : Project_Tree_Ref;
209       Interfaces  : Argument_List;
210       To_Dir      : Path_Name_Type);
211    --  Copy the interface sources of a SAL to directory To_Dir
212
213    procedure Display (Executable : String);
214    --  Display invocation of gnatbind and of the compiler with the arguments
215    --  in Arguments, except when Quiet_Output is True.
216
217    function Index (S, Pattern : String) return Natural;
218    --  Return the last occurrence of Pattern in S, or 0 if none
219
220    procedure Process_Binder_File (Name : String);
221    --  For Stand-Alone libraries, get the Linker Options in the binder
222    --  generated file.
223
224    procedure Reset_Tables;
225    --  Make sure that all the above tables are empty
226    --  (Objects, Ali_Files, Options).
227
228    function SALs_Use_Constructors return Boolean;
229    --  Indicate if Stand-Alone Libraries are automatically initialized using
230    --  the constructor mechanism.
231
232    function Ultimate_Extension_Of
233      (Project : Project_Id;
234       In_Tree : Project_Tree_Ref) return Project_Id;
235    --  Returns the Project_Id of project Project. Returns No_Project
236    --  if Project is No_Project.
237
238    ------------------
239    -- Add_Argument --
240    ------------------
241
242    procedure Add_Argument (S : String) is
243    begin
244       if Argument_Number = Arguments'Last then
245          declare
246             New_Args : constant String_List_Access :=
247               new String_List (1 .. 2 * Arguments'Last);
248
249          begin
250             --  Copy the String_Accesses and set them to null in Arguments
251             --  so that they will not be deallocated by the call to
252             --  Free (Arguments).
253
254             New_Args (Arguments'Range) := Arguments.all;
255             Arguments.all := (others => null);
256             Free (Arguments);
257             Arguments := New_Args;
258          end;
259       end if;
260
261       Argument_Number := Argument_Number + 1;
262       Arguments (Argument_Number) := new String'(S);
263    end Add_Argument;
264
265    -------------------
266    -- ALI_File_Name --
267    -------------------
268
269    function ALI_File_Name (Source : String) return String is
270    begin
271       --  If the source name has an extension, then replace it with
272       --  the ALI suffix.
273
274       for Index in reverse Source'First + 1 .. Source'Last loop
275          if Source (Index) = '.' then
276             return Source (Source'First .. Index - 1) & ALI_Suffix;
277          end if;
278       end loop;
279
280       --  If there is no dot, or if it is the first character, just add the
281       --  ALI suffix.
282
283       return Source & ALI_Suffix;
284    end ALI_File_Name;
285
286    -------------------
287    -- Build_Library --
288    -------------------
289
290    procedure Build_Library
291      (For_Project   : Project_Id;
292       In_Tree       : Project_Tree_Ref;
293       Gnatbind      : String;
294       Gnatbind_Path : String_Access;
295       Gcc           : String;
296       Gcc_Path      : String_Access;
297       Bind          : Boolean := True;
298       Link          : Boolean := True)
299    is
300       Maximum_Size : Integer;
301       pragma Import (C, Maximum_Size, "__gnat_link_max");
302       --  Maximum number of bytes to put in an invocation of the
303       --  gnatbind.
304
305       Size : Integer;
306       --  The number of bytes for the invocation of the gnatbind
307
308       Warning_For_Library : Boolean := False;
309       --  Set to True for the first warning about a unit missing from the
310       --  interface set.
311
312       Data : Project_Data := In_Tree.Projects.Table (For_Project);
313
314       Libgnarl_Needed   : Yes_No_Unknown := Data.Libgnarl_Needed;
315       --  Set to True if library needs to be linked with libgnarl
316
317       Libdecgnat_Needed : Boolean := False;
318       --  On OpenVMS, set to True if library needs to be linked with libdecgnat
319
320       Gtrasymobj_Needed : Boolean := False;
321       --  On OpenVMS, set to True if library needs to be linked with
322       --  g-trasym.obj.
323
324       Object_Directory_Path : constant String :=
325                           Get_Name_String (Data.Display_Object_Dir);
326
327       Standalone   : constant Boolean := Data.Standalone_Library;
328
329       Project_Name : constant String := Get_Name_String (Data.Name);
330
331       Current_Dir  : constant String := Get_Current_Dir;
332
333       Lib_Filename : String_Access;
334       Lib_Dirpath  : String_Access;
335       Lib_Version  : String_Access := new String'("");
336
337       The_Build_Mode : Build_Mode_State := None;
338
339       Success : Boolean := False;
340
341       Library_Options : Variable_Value := Nil_Variable_Value;
342       Library_GCC     : Variable_Value := Nil_Variable_Value;
343
344       Driver_Name : Name_Id := No_Name;
345
346       In_Main_Object_Directory : Boolean := True;
347
348       There_Are_Foreign_Sources : Boolean;
349
350       Rpath : String_Access := null;
351       --  Allocated only if Path Option is supported
352
353       Rpath_Last : Natural := 0;
354       --  Index of last valid character of Rpath
355
356       Initial_Rpath_Length : constant := 200;
357       --  Initial size of Rpath, when first allocated
358
359       Path_Option : String_Access := Linker_Library_Path_Option;
360       --  If null, Path Option is not supported.
361       --  Not a constant so that it can be deallocated.
362
363       First_ALI : File_Name_Type := No_File;
364       --  Store the ALI file name of a source of the library (the first found)
365
366       procedure Add_ALI_For (Source : File_Name_Type);
367       --  Add the name of the ALI file corresponding to Source to the
368       --  Arguments.
369
370       procedure Add_Rpath (Path : String);
371       --  Add a path name to Rpath
372
373       function Check_Project (P : Project_Id) return Boolean;
374       --  Returns True if P is For_Project or a project extended by For_Project
375
376       procedure Check_Libs (ALI_File : String; Main_Project : Boolean);
377       --  Set Libgnarl_Needed if the ALI_File indicates that there is a need
378       --  to link with -lgnarl (this is the case when there is a dependency
379       --  on s-osinte.ads). On OpenVMS, set Libdecgnat_Needed if the ALI file
380       --  indicates that there is a need to link with -ldecgnat (this is the
381       --  case when there is a dependency on dec.ads), and set
382       --  Gtrasymobj_Needed if there is a dependency on g-trasym.ads.
383
384       procedure Process (The_ALI : File_Name_Type);
385       --  Check if the closure of a library unit which is or should be in the
386       --  interface set is also in the interface set. Issue a warning for each
387       --  missing library unit.
388
389       procedure Process_Imported_Libraries;
390       --  Add the -L and -l switches for the imported Library Project Files,
391       --  and, if Path Option is supported, the library directory path names
392       --  to Rpath.
393
394       -----------------
395       -- Add_ALI_For --
396       -----------------
397
398       procedure Add_ALI_For (Source : File_Name_Type) is
399          ALI    : constant String := ALI_File_Name (Get_Name_String (Source));
400          ALI_Id : File_Name_Type;
401
402       begin
403          if Bind then
404             Add_Argument (ALI);
405          end if;
406
407          Name_Len := 0;
408          Add_Str_To_Name_Buffer (S => ALI);
409          ALI_Id := Name_Find;
410
411          --  Add the ALI file name to the library ALIs
412
413          if Bind then
414             Library_ALIs.Set (ALI_Id, True);
415          end if;
416
417          --  Set First_ALI, if not already done
418
419          if First_ALI = No_File then
420             First_ALI := ALI_Id;
421          end if;
422       end Add_ALI_For;
423
424       ---------------
425       -- Add_Rpath --
426       ---------------
427
428       procedure Add_Rpath (Path : String) is
429
430          procedure Double;
431          --  Double Rpath size
432
433          ------------
434          -- Double --
435          ------------
436
437          procedure Double is
438             New_Rpath : constant String_Access :=
439                           new String (1 .. 2 * Rpath'Length);
440          begin
441             New_Rpath (1 .. Rpath_Last) := Rpath (1 .. Rpath_Last);
442             Free (Rpath);
443             Rpath := New_Rpath;
444          end Double;
445
446       --  Start of processing for Add_Rpath
447
448       begin
449          --  If first path, allocate initial Rpath
450
451          if Rpath = null then
452             Rpath := new String (1 .. Initial_Rpath_Length);
453             Rpath_Last := 0;
454
455          else
456             --  Otherwise, add a path separator between two path names
457
458             if Rpath_Last = Rpath'Last then
459                Double;
460             end if;
461
462             Rpath_Last := Rpath_Last + 1;
463             Rpath (Rpath_Last) := Path_Separator;
464          end if;
465
466          --  Increase Rpath size until it is large enough
467
468          while Rpath_Last + Path'Length > Rpath'Last loop
469             Double;
470          end loop;
471
472          --  Add the path name
473
474          Rpath (Rpath_Last + 1 .. Rpath_Last + Path'Length) := Path;
475          Rpath_Last := Rpath_Last + Path'Length;
476       end Add_Rpath;
477
478       -------------------
479       -- Check_Project --
480       -------------------
481
482       function Check_Project (P : Project_Id) return Boolean is
483       begin
484          if P = For_Project then
485             return True;
486
487          elsif P /= No_Project then
488             declare
489                Data : Project_Data :=
490                         In_Tree.Projects.Table (For_Project);
491             begin
492                while Data.Extends /= No_Project loop
493                   if P = Data.Extends then
494                      return True;
495                   end if;
496
497                   Data := In_Tree.Projects.Table (Data.Extends);
498                end loop;
499             end;
500          end if;
501
502          return False;
503       end Check_Project;
504
505       ----------------
506       -- Check_Libs --
507       ----------------
508
509       procedure Check_Libs (ALI_File : String; Main_Project : Boolean) is
510          Lib_File : File_Name_Type;
511          Text     : Text_Buffer_Ptr;
512          Id       : ALI.ALI_Id;
513
514       begin
515          if Libgnarl_Needed /= Yes
516            or else
517             (Main_Project
518               and then OpenVMS_On_Target
519               and then ((not Libdecgnat_Needed) or (not Gtrasymobj_Needed)))
520          then
521             --  Scan the ALI file
522
523             Name_Len := ALI_File'Length;
524             Name_Buffer (1 .. Name_Len) := ALI_File;
525             Lib_File := Name_Find;
526             Text := Read_Library_Info (Lib_File, True);
527
528             Id  := ALI.Scan_ALI
529                          (F          => Lib_File,
530                           T          => Text,
531                           Ignore_ED  => False,
532                           Err        => True,
533                           Read_Lines => "D");
534             Free (Text);
535
536             --  Look for s-osinte.ads in the dependencies
537
538             for Index in ALI.ALIs.Table (Id).First_Sdep ..
539                          ALI.ALIs.Table (Id).Last_Sdep
540             loop
541                if ALI.Sdep.Table (Index).Sfile = S_Osinte_Ads then
542                   Libgnarl_Needed := Yes;
543
544                   if Main_Project then
545                      In_Tree.Projects.Table (For_Project).Libgnarl_Needed :=
546                        Yes;
547                   else
548                      exit;
549                   end if;
550
551                elsif OpenVMS_On_Target then
552                   if ALI.Sdep.Table (Index).Sfile = S_Dec_Ads then
553                      Libdecgnat_Needed := True;
554
555                   elsif ALI.Sdep.Table (Index).Sfile = G_Trasym_Ads then
556                      Gtrasymobj_Needed := True;
557                   end if;
558                end if;
559             end loop;
560          end if;
561       end Check_Libs;
562
563       -------------
564       -- Process --
565       -------------
566
567       procedure Process (The_ALI : File_Name_Type) is
568          Text       : Text_Buffer_Ptr;
569          Idread     : ALI_Id;
570          First_Unit : ALI.Unit_Id;
571          Last_Unit  : ALI.Unit_Id;
572          Unit_Data  : Unit_Record;
573          Afile      : File_Name_Type;
574
575       begin
576          --  Nothing to do if the ALI file has already been processed.
577          --  This happens if an interface imports another interface.
578
579          if not Processed_ALIs.Get (The_ALI) then
580             Processed_ALIs.Set (The_ALI, True);
581             Text := Read_Library_Info (The_ALI);
582
583             if Text /= null then
584                Idread :=
585                  Scan_ALI
586                    (F         => The_ALI,
587                     T         => Text,
588                     Ignore_ED => False,
589                     Err       => True);
590                Free (Text);
591
592                if Idread /= No_ALI_Id then
593                   First_Unit := ALI.ALIs.Table (Idread).First_Unit;
594                   Last_Unit  := ALI.ALIs.Table (Idread).Last_Unit;
595
596                   --  Process both unit (spec and body) if the body is needed
597                   --  by the spec (inline or generic). Otherwise, just process
598                   --  the spec.
599
600                   if First_Unit /= Last_Unit and then
601                     not ALI.Units.Table (Last_Unit).Body_Needed_For_SAL
602                   then
603                      First_Unit := Last_Unit;
604                   end if;
605
606                   for Unit in First_Unit .. Last_Unit loop
607                      Unit_Data := ALI.Units.Table (Unit);
608
609                      --  Check if each withed unit which is in the library is
610                      --  also in the interface set, if it has not yet been
611                      --  processed.
612
613                      for W in Unit_Data.First_With .. Unit_Data.Last_With loop
614                         Afile := Withs.Table (W).Afile;
615
616                         if Afile /= No_File and then Library_ALIs.Get (Afile)
617                           and then not Processed_ALIs.Get (Afile)
618                         then
619                            if not Interface_ALIs.Get (Afile) then
620                               if not Warning_For_Library then
621                                  Write_Str ("Warning: In library project """);
622                                  Get_Name_String (Data.Name);
623                                  To_Mixed (Name_Buffer (1 .. Name_Len));
624                                  Write_Str (Name_Buffer (1 .. Name_Len));
625                                  Write_Line ("""");
626                                  Warning_For_Library := True;
627                               end if;
628
629                               Write_Str ("         Unit """);
630                               Get_Name_String (Withs.Table (W).Uname);
631                               To_Mixed (Name_Buffer (1 .. Name_Len - 2));
632                               Write_Str (Name_Buffer (1 .. Name_Len - 2));
633                               Write_Line (""" is not in the interface set");
634                               Write_Str ("         but it is needed by ");
635
636                               case Unit_Data.Utype is
637                                  when Is_Spec =>
638                                     Write_Str ("the spec of ");
639
640                                  when Is_Body =>
641                                     Write_Str ("the body of ");
642
643                                  when others =>
644                                     null;
645                               end case;
646
647                               Write_Str ("""");
648                               Get_Name_String (Unit_Data.Uname);
649                               To_Mixed (Name_Buffer (1 .. Name_Len - 2));
650                               Write_Str (Name_Buffer (1 .. Name_Len - 2));
651                               Write_Line ("""");
652                            end if;
653
654                            --  Now, process this unit
655
656                            Process (Afile);
657                         end if;
658                      end loop;
659                   end loop;
660                end if;
661             end if;
662          end if;
663       end Process;
664
665       --------------------------------
666       -- Process_Imported_Libraries --
667       --------------------------------
668
669       procedure Process_Imported_Libraries is
670          Current : Project_Id;
671
672          procedure Process_Project (Project : Project_Id);
673          --  Process Project and its imported projects recursively.
674          --  Add any library projects to table Library_Projs.
675
676          ---------------------
677          -- Process_Project --
678          ---------------------
679
680          procedure Process_Project (Project : Project_Id) is
681             Data     : Project_Data := In_Tree.Projects.Table (Project);
682             Imported : Project_List := Data.Imported_Projects;
683             Element  : Project_Element;
684
685          begin
686             --  Nothing to do if process has already been processed
687
688             if not Processed_Projects.Get (Data.Name) then
689                Processed_Projects.Set (Data.Name, True);
690
691                --  Call Process_Project recursively for any imported project.
692                --  We first process the imported projects to guarantee that
693                --  we have a proper reverse order for the libraries.
694
695                while Imported /= Empty_Project_List loop
696                   Element :=
697                     In_Tree.Project_Lists.Table (Imported);
698
699                   if Element.Project /= No_Project then
700                      Process_Project (Element.Project);
701                   end if;
702
703                   Imported := Element.Next;
704                end loop;
705
706                --  If it is a library project, add it to Library_Projs
707
708                if Project /= For_Project and then Data.Library then
709                   Library_Projs.Increment_Last;
710                   Library_Projs.Table (Library_Projs.Last) := Project;
711
712                   --  Check if because of this library we need to use libgnarl
713
714                   if Libgnarl_Needed = Unknown then
715                      if Data.Libgnarl_Needed = Unknown
716                        and then Data.Object_Directory /= No_Path
717                      then
718                         --  Check if libgnarl is needed for this library
719
720                         declare
721                            Object_Dir_Path : constant String :=
722                                                Get_Name_String
723                                                  (Data.Display_Object_Dir);
724                            Object_Dir      : Dir_Type;
725                            Filename        : String (1 .. 255);
726                            Last            : Natural;
727
728                         begin
729                            Open (Object_Dir, Object_Dir_Path);
730
731                            --  For all entries in the object directory
732
733                            loop
734                               Read (Object_Dir, Filename, Last);
735                               exit when Last = 0;
736
737                               --  Check if it is an object file
738
739                               if Is_Obj (Filename (1 .. Last)) then
740                                  declare
741                                     Object_Path : constant String :=
742                                                     Normalize_Pathname
743                                                       (Object_Dir_Path &
744                                                        Directory_Separator &
745                                                        Filename (1 .. Last));
746                                     ALI_File    : constant String :=
747                                                     Ext_To
748                                                       (Object_Path, "ali");
749
750                                  begin
751                                     if Is_Regular_File (ALI_File) then
752
753                                        --  Find out if for this ALI file,
754                                        --  libgnarl is necessary.
755
756                                        Check_Libs
757                                          (ALI_File, Main_Project => False);
758
759                                        if Libgnarl_Needed = Yes then
760                                           Data.Libgnarl_Needed := Yes;
761                                           In_Tree.Projects.Table
762                                             (For_Project).Libgnarl_Needed :=
763                                             Yes;
764                                           exit;
765                                        end if;
766                                     end if;
767                                  end;
768                               end if;
769                            end loop;
770
771                            Close (Object_Dir);
772                         end;
773                      end if;
774
775                      if Data.Libgnarl_Needed = Yes then
776                         Libgnarl_Needed := Yes;
777                         In_Tree.Projects.Table (For_Project).Libgnarl_Needed :=
778                           Yes;
779                      end if;
780                   end if;
781                end if;
782
783             end if;
784          end Process_Project;
785
786       --  Start of processing for Process_Imported_Libraries
787
788       begin
789          --  Build list of library projects imported directly or indirectly,
790          --  in the reverse order.
791
792          Process_Project (For_Project);
793
794          --  Add the -L and -l switches and, if the Rpath option is supported,
795          --  add the directory to the Rpath. As the library projects are in the
796          --  wrong order, process from the last to the first.
797
798          for Index in reverse 1 .. Library_Projs.Last loop
799             Current := Library_Projs.Table (Index);
800
801             Get_Name_String
802               (In_Tree.Projects.Table (Current).Display_Library_Dir);
803             Opts.Increment_Last;
804             Opts.Table (Opts.Last) :=
805               new String'("-L" & Name_Buffer (1 .. Name_Len));
806
807             if Path_Option /= null then
808                Add_Rpath (Name_Buffer (1 .. Name_Len));
809             end if;
810
811             Opts.Increment_Last;
812             Opts.Table (Opts.Last) :=
813               new String'
814                 ("-l" &
815                  Get_Name_String
816                    (In_Tree.Projects.Table
817                       (Current).Library_Name));
818          end loop;
819       end Process_Imported_Libraries;
820
821    --  Start of processing for Build_Library
822
823    begin
824       Reset_Tables;
825
826       --  Fail if project is not a library project
827
828       if not Data.Library then
829          Com.Fail ("project """, Project_Name, """ has no library");
830       end if;
831
832       --  If this is the first time Build_Library is called, get the Name_Id
833       --  of "s-osinte.ads".
834
835       if S_Osinte_Ads = No_File then
836          Name_Len := 0;
837          Add_Str_To_Name_Buffer ("s-osinte.ads");
838          S_Osinte_Ads := Name_Find;
839       end if;
840
841       if S_Dec_Ads = No_File then
842          Name_Len := 0;
843          Add_Str_To_Name_Buffer ("dec.ads");
844          S_Dec_Ads := Name_Find;
845       end if;
846
847       if G_Trasym_Ads = No_File then
848          Name_Len := 0;
849          Add_Str_To_Name_Buffer ("g-trasym.ads");
850          G_Trasym_Ads := Name_Find;
851       end if;
852
853       --  We work in the object directory
854
855       Change_Dir (Object_Directory_Path);
856
857       if Standalone then
858
859          --  Call gnatbind only if Bind is True
860
861          if Bind then
862             if Gnatbind_Path = null then
863                Com.Fail ("unable to locate ", Gnatbind);
864             end if;
865
866             if Gcc_Path = null then
867                Com.Fail ("unable to locate ", Gcc);
868             end if;
869
870             --  Allocate Arguments, if it is the first time we see a standalone
871             --  library.
872
873             if Arguments = No_Argument then
874                Arguments := new String_List (1 .. Initial_Argument_Max);
875             end if;
876
877             --  Add "-n -o b~<lib>.adb (b__<lib>.adb on VMS) -L<lib>"
878
879             Argument_Number := 2;
880             Arguments (1) := No_Main;
881             Arguments (2) := Output_Switch;
882
883             if OpenVMS_On_Target then
884                B_Start := new String'("b__");
885             end if;
886
887             Add_Argument
888               (B_Start.all & Get_Name_String (Data.Library_Name) & ".adb");
889             Add_Argument ("-L" & Get_Name_String (Data.Library_Name));
890
891             if Data.Lib_Auto_Init and then SALs_Use_Constructors then
892                Add_Argument (Auto_Initialize);
893             end if;
894
895             --  Check if Binder'Default_Switches ("Ada") is defined. If it is,
896             --  add these switches to call gnatbind.
897
898             declare
899                Binder_Package : constant Package_Id :=
900                                   Value_Of
901                                     (Name        => Name_Binder,
902                                      In_Packages => Data.Decl.Packages,
903                                      In_Tree     => In_Tree);
904
905             begin
906                if Binder_Package /= No_Package then
907                   declare
908                      Defaults : constant Array_Element_Id :=
909                                   Value_Of
910                                     (Name      => Name_Default_Switches,
911                                      In_Arrays =>
912                                        In_Tree.Packages.Table
913                                          (Binder_Package).Decl.Arrays,
914                                      In_Tree   => In_Tree);
915                      Switches : Variable_Value := Nil_Variable_Value;
916
917                      Switch : String_List_Id := Nil_String;
918
919                   begin
920                      if Defaults /= No_Array_Element then
921                         Switches :=
922                           Value_Of
923                             (Index     => Name_Ada,
924                              Src_Index => 0,
925                              In_Array  => Defaults,
926                              In_Tree   => In_Tree);
927
928                         if not Switches.Default then
929                            Switch := Switches.Values;
930
931                            while Switch /= Nil_String loop
932                               Add_Argument
933                                 (Get_Name_String
934                                    (In_Tree.String_Elements.Table
935                                       (Switch).Value));
936                               Switch := In_Tree.String_Elements.
937                                           Table (Switch).Next;
938                            end loop;
939                         end if;
940                      end if;
941                   end;
942                end if;
943             end;
944          end if;
945
946          --  Get all the ALI files of the project file. We do that even if
947          --  Bind is False, so that First_ALI is set.
948
949          declare
950             Unit : Unit_Data;
951
952          begin
953             Library_ALIs.Reset;
954             Interface_ALIs.Reset;
955             Processed_ALIs.Reset;
956
957             for Source in Unit_Table.First ..
958                           Unit_Table.Last (In_Tree.Units)
959             loop
960                Unit := In_Tree.Units.Table (Source);
961
962                if Unit.File_Names (Body_Part).Name /= No_File
963                  and then Unit.File_Names (Body_Part).Path /= Slash
964                then
965                   if
966                     Check_Project (Unit.File_Names (Body_Part).Project)
967                   then
968                      if Unit.File_Names (Specification).Name = No_File then
969                         declare
970                            Src_Ind : Source_File_Index;
971
972                         begin
973                            Src_Ind := Sinput.P.Load_Project_File
974                              (Get_Name_String
975                                 (Unit.File_Names
976                                    (Body_Part).Path));
977
978                            --  Add the ALI file only if it is not a subunit
979
980                            if not
981                              Sinput.P.Source_File_Is_Subunit (Src_Ind)
982                            then
983                               Add_ALI_For
984                                 (Unit.File_Names (Body_Part).Name);
985                               exit when not Bind;
986                            end if;
987                         end;
988
989                      else
990                         Add_ALI_For (Unit.File_Names (Body_Part).Name);
991                         exit when not Bind;
992                      end if;
993                   end if;
994
995                elsif Unit.File_Names (Specification).Name /= No_File
996                  and then Unit.File_Names (Specification).Path /= Slash
997                  and then Check_Project
998                    (Unit.File_Names (Specification).Project)
999                then
1000                   Add_ALI_For (Unit.File_Names (Specification).Name);
1001                   exit when not Bind;
1002                end if;
1003             end loop;
1004          end;
1005
1006          --  Continue setup and call gnatbind if Bind is True
1007
1008          if Bind then
1009
1010             --  Get an eventual --RTS from the ALI file
1011
1012             if First_ALI /= No_File then
1013                declare
1014                   T : Text_Buffer_Ptr;
1015                   A : ALI_Id;
1016
1017                begin
1018                   --  Load the ALI file
1019
1020                   T := Read_Library_Info (First_ALI, True);
1021
1022                   --  Read it
1023
1024                   A := Scan_ALI
1025                          (First_ALI, T, Ignore_ED => False, Err => False);
1026
1027                   if A /= No_ALI_Id then
1028                      for Index in
1029                        ALI.Units.Table
1030                          (ALI.ALIs.Table (A).First_Unit).First_Arg ..
1031                        ALI.Units.Table
1032                          (ALI.ALIs.Table (A).First_Unit).Last_Arg
1033                      loop
1034                         --  Look for --RTS. If found, add the switch to call
1035                         --  gnatbind.
1036
1037                         declare
1038                            Arg : String_Ptr renames Args.Table (Index);
1039                         begin
1040                            if Arg'Length >= 6 and then
1041                               Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1042                            then
1043                               Add_Argument (Arg.all);
1044                               exit;
1045                            end if;
1046                         end;
1047                      end loop;
1048                   end if;
1049                end;
1050             end if;
1051
1052             --  Set the paths
1053
1054             Set_Ada_Paths
1055               (Project             => For_Project,
1056                In_Tree             => In_Tree,
1057                Including_Libraries => True);
1058
1059             --  Display the gnatbind command, if not in quiet output
1060
1061             Display (Gnatbind);
1062
1063             Size := 0;
1064             for J in 1 .. Argument_Number loop
1065                Size := Size + Arguments (J)'Length + 1;
1066             end loop;
1067
1068             --  Invoke gnatbind with the arguments if the size is not too large
1069
1070             if Size <= Maximum_Size then
1071                Spawn
1072                  (Gnatbind_Path.all,
1073                   Arguments (1 .. Argument_Number),
1074                   Success);
1075
1076             else
1077                --  Otherwise create a temporary response file
1078
1079                declare
1080                   FD            : File_Descriptor;
1081                   Path          : Path_Name_Type;
1082                   Args          : Argument_List (1 .. 1);
1083                   EOL           : constant String (1 .. 1) := (1 => ASCII.LF);
1084                   Status        : Integer;
1085                   Succ          : Boolean;
1086                   Quotes_Needed : Boolean;
1087                   Last_Char     : Natural;
1088                   Ch            : Character;
1089
1090                begin
1091                   Tempdir.Create_Temp_File (FD, Path);
1092                   Args (1) := new String'("@" & Get_Name_String (Path));
1093
1094                   for J in 1 .. Argument_Number loop
1095
1096                      --  Check if the argument should be quoted
1097
1098                      Quotes_Needed := False;
1099                      Last_Char     := Arguments (J)'Length;
1100
1101                      for K in Arguments (J)'Range loop
1102                         Ch := Arguments (J) (K);
1103
1104                         if Ch = ' ' or else Ch = ASCII.HT or else Ch = '"' then
1105                            Quotes_Needed := True;
1106                            exit;
1107                         end if;
1108                      end loop;
1109
1110                      if Quotes_Needed then
1111
1112                         --  Quote the argument, doubling '"'
1113
1114                         declare
1115                            Arg : String (1 .. Arguments (J)'Length * 2 + 2);
1116
1117                         begin
1118                            Arg (1) := '"';
1119                            Last_Char := 1;
1120
1121                            for K in Arguments (J)'Range loop
1122                               Ch := Arguments (J) (K);
1123                               Last_Char := Last_Char + 1;
1124                               Arg (Last_Char) := Ch;
1125
1126                               if Ch = '"' then
1127                                  Last_Char := Last_Char + 1;
1128                                  Arg (Last_Char) := '"';
1129                               end if;
1130                            end loop;
1131
1132                            Last_Char := Last_Char + 1;
1133                            Arg (Last_Char) := '"';
1134
1135                            Status := Write (FD, Arg'Address, Last_Char);
1136                         end;
1137
1138                      else
1139                         Status := Write
1140                           (FD,
1141                            Arguments (J) (Arguments (J)'First)'Address,
1142                            Last_Char);
1143                      end if;
1144
1145                      if Status /= Last_Char then
1146                         Fail ("disk full");
1147                      end if;
1148
1149                      Status := Write (FD, EOL (1)'Address, 1);
1150
1151                      if Status /= 1 then
1152                         Fail ("disk full");
1153                      end if;
1154                   end loop;
1155
1156                   Close (FD);
1157
1158                   --  And invoke gnatbind with this this response file
1159
1160                   Spawn (Gnatbind_Path.all, Args, Success);
1161
1162                   Delete_File (Get_Name_String (Path), Succ);
1163
1164                   if not Succ then
1165                      null;
1166                   end if;
1167                end;
1168             end if;
1169
1170             if not Success then
1171                Com.Fail ("could not bind standalone library ",
1172                          Get_Name_String (Data.Library_Name));
1173             end if;
1174          end if;
1175
1176          --  Compile the binder generated file only if Link is true
1177
1178          if Link then
1179
1180             --  Set the paths
1181
1182             Set_Ada_Paths
1183               (Project             => For_Project,
1184                In_Tree             => In_Tree,
1185                Including_Libraries => True);
1186
1187             --  Invoke <gcc> -c b__<lib>.adb
1188
1189             --  Allocate Arguments, if it is the first time we see a standalone
1190             --  library.
1191
1192             if Arguments = No_Argument then
1193                Arguments := new String_List (1 .. Initial_Argument_Max);
1194             end if;
1195
1196             Argument_Number := 1;
1197             Arguments (1) := Compile_Switch;
1198
1199             if OpenVMS_On_Target then
1200                B_Start := new String'("b__");
1201             end if;
1202
1203             Add_Argument
1204               (B_Start.all & Get_Name_String (Data.Library_Name) & ".adb");
1205
1206             --  If necessary, add the PIC option
1207
1208             if PIC_Option /= "" then
1209                Add_Argument (PIC_Option);
1210             end if;
1211
1212             --  Get the back-end switches and --RTS from the ALI file
1213
1214             if First_ALI /= No_File then
1215                declare
1216                   T : Text_Buffer_Ptr;
1217                   A : ALI_Id;
1218
1219                begin
1220                   --  Load the ALI file
1221
1222                   T := Read_Library_Info (First_ALI, True);
1223
1224                   --  Read it
1225
1226                   A :=
1227                     Scan_ALI (First_ALI, T, Ignore_ED => False, Err => False);
1228
1229                   if A /= No_ALI_Id then
1230                      for Index in
1231                        ALI.Units.Table
1232                          (ALI.ALIs.Table (A).First_Unit).First_Arg ..
1233                        ALI.Units.Table
1234                          (ALI.ALIs.Table (A).First_Unit).Last_Arg
1235                      loop
1236                         --  Do not compile with the front end switches except
1237                         --  for --RTS.
1238
1239                         declare
1240                            Arg : String_Ptr renames Args.Table (Index);
1241                         begin
1242                            if not Is_Front_End_Switch (Arg.all)
1243                              or else
1244                                Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1245                            then
1246                               Add_Argument (Arg.all);
1247                            end if;
1248                         end;
1249                      end loop;
1250                   end if;
1251                end;
1252             end if;
1253
1254             --  Now that all the arguments are set, compile the binder
1255             --  generated file.
1256
1257             Display (Gcc);
1258             Spawn
1259               (Gcc_Path.all, Arguments (1 .. Argument_Number), Success);
1260
1261             if not Success then
1262                Com.Fail
1263                  ("could not compile binder generated file for library ",
1264                   Get_Name_String (Data.Library_Name));
1265             end if;
1266
1267             --  Process binder generated file for pragmas Linker_Options
1268
1269             Process_Binder_File (Arguments (2).all & ASCII.NUL);
1270          end if;
1271       end if;
1272
1273       --  Build the library only if Link is True
1274
1275       if Link then
1276
1277          --  If attribute Library_GCC was specified, get the driver name
1278
1279          Library_GCC :=
1280            Value_Of (Name_Library_GCC, Data.Decl.Attributes, In_Tree);
1281
1282          if not Library_GCC.Default then
1283             Driver_Name := Library_GCC.Value;
1284          end if;
1285
1286          --  If attribute Library_Options was specified, add these additional
1287          --  options.
1288
1289          Library_Options :=
1290            Value_Of (Name_Library_Options, Data.Decl.Attributes, In_Tree);
1291
1292          if not Library_Options.Default then
1293             declare
1294                Current : String_List_Id;
1295                Element : String_Element;
1296
1297             begin
1298                Current := Library_Options.Values;
1299                while Current /= Nil_String loop
1300                   Element := In_Tree.String_Elements.Table (Current);
1301                   Get_Name_String (Element.Value);
1302
1303                   if Name_Len /= 0 then
1304                      Opts.Increment_Last;
1305                      Opts.Table (Opts.Last) :=
1306                        new String'(Name_Buffer (1 .. Name_Len));
1307                   end if;
1308
1309                   Current := Element.Next;
1310                end loop;
1311             end;
1312          end if;
1313
1314          Lib_Dirpath  :=
1315            new String'(Get_Name_String (Data.Display_Library_Dir));
1316          Lib_Filename := new String'(Get_Name_String (Data.Library_Name));
1317
1318          case Data.Library_Kind is
1319             when Static =>
1320                The_Build_Mode := Static;
1321
1322             when Dynamic =>
1323                The_Build_Mode := Dynamic;
1324
1325             when Relocatable =>
1326                The_Build_Mode := Relocatable;
1327
1328                if PIC_Option /= "" then
1329                   Opts.Increment_Last;
1330                   Opts.Table (Opts.Last) := new String'(PIC_Option);
1331                end if;
1332          end case;
1333
1334          --  Get the library version, if any
1335
1336          if Data.Lib_Internal_Name /= No_Name then
1337             Lib_Version :=
1338               new String'(Get_Name_String (Data.Lib_Internal_Name));
1339          end if;
1340
1341          --  Add the objects found in the object directory and the object
1342          --  directories of the extended files, if any, except for generated
1343          --  object files (b~.. or B__..) from extended projects.
1344          --  When there are one or more extended files, only add an object file
1345          --  if no object file with the same name have already been added.
1346
1347          In_Main_Object_Directory := True;
1348
1349          There_Are_Foreign_Sources := Data.Other_Sources_Present;
1350
1351          loop
1352             declare
1353                Object_Dir_Path : constant String :=
1354                                    Get_Name_String (Data.Display_Object_Dir);
1355                Object_Dir      : Dir_Type;
1356                Filename        : String (1 .. 255);
1357                Last            : Natural;
1358                Id              : Name_Id;
1359
1360             begin
1361                Open (Dir => Object_Dir, Dir_Name => Object_Dir_Path);
1362
1363                --  For all entries in the object directory
1364
1365                loop
1366                   Read (Object_Dir, Filename, Last);
1367
1368                   exit when Last = 0;
1369
1370                   --  Check if it is an object file
1371
1372                   if Is_Obj (Filename (1 .. Last)) then
1373                      declare
1374                         Object_Path : constant String :=
1375                           Normalize_Pathname
1376                             (Object_Dir_Path & Directory_Separator &
1377                              Filename (1 .. Last));
1378                         C_Object_Path : String := Object_Path;
1379                         C_Filename    : String := Filename (1 .. Last);
1380
1381                      begin
1382                         Canonical_Case_File_Name (C_Object_Path);
1383                         Canonical_Case_File_Name (C_Filename);
1384
1385                         --  If in the object directory of an extended project,
1386                         --  do not consider generated object files.
1387
1388                         if In_Main_Object_Directory
1389                           or else Last < 5
1390                           or else C_Filename (1 .. B_Start'Length) /=
1391                                     B_Start.all
1392                         then
1393                            Name_Len := Last;
1394                            Name_Buffer (1 .. Name_Len) :=
1395                              C_Filename (1 .. Last);
1396                            Id := Name_Find;
1397
1398                            if not Objects_Htable.Get (Id) then
1399                               declare
1400                                  ALI_File : constant String :=
1401                                               Ext_To
1402                                                 (Filename (1 .. Last), "ali");
1403                                  ALI_Path : constant String :=
1404                                               Ext_To (Object_Path, "ali");
1405                                  Add_It   : Boolean :=
1406                                               There_Are_Foreign_Sources
1407                                               or else
1408                                                 (Last > 5
1409                                                  and then
1410                                                  C_Filename
1411                                                    (1 .. B_Start'Length) =
1412                                                  B_Start.all);
1413                                  Fname    : File_Name_Type;
1414                                  Proj     : Project_Id;
1415
1416                               begin
1417                                  if Is_Regular_File (ALI_Path) then
1418
1419                                     --  If there is an ALI file, check if the
1420                                     --  object file should be added to the
1421                                     --  library. If there are foreign sources
1422                                     --  we put all object files in the library.
1423
1424                                     if not Add_It then
1425                                        for Index in
1426                                          1 .. Unit_Table.Last (In_Tree.Units)
1427                                        loop
1428                                           if In_Tree.Units.Table
1429                                             (Index).File_Names
1430                                               (Body_Part).Name /= No_File
1431                                           then
1432                                              Proj :=
1433                                                In_Tree.Units.Table (Index).
1434                                                  File_Names
1435                                                    (Body_Part).Project;
1436                                              Fname :=
1437                                                In_Tree.Units.Table (Index).
1438                                                  File_Names (Body_Part).Name;
1439
1440                                           elsif
1441                                             In_Tree.Units.Table
1442                                               (Index).File_Names
1443                                                 (Specification).Name /= No_File
1444                                           then
1445                                              Proj :=
1446                                                In_Tree.Units.Table
1447                                                  (Index).File_Names
1448                                                    (Specification).Project;
1449                                              Fname :=
1450                                                In_Tree.Units.Table
1451                                                  (Index).File_Names
1452                                                    (Specification).Name;
1453
1454                                           else
1455                                              Proj := No_Project;
1456                                           end if;
1457
1458                                           Add_It := Proj /= No_Project;
1459
1460                                           --  If the source is in the project
1461                                           --  or a project it extends, we may
1462                                           --  put it in the library.
1463
1464                                           if Add_It then
1465                                              Add_It := Check_Project (Proj);
1466                                           end if;
1467
1468                                           --  But we don't, if the ALI file
1469                                           --  does not correspond to the unit.
1470
1471                                           if Add_It then
1472                                              declare
1473                                                 F : constant String :=
1474                                                       Ext_To
1475                                                         (Get_Name_String
1476                                                            (Fname), "ali");
1477                                              begin
1478                                                 Add_It := F = ALI_File;
1479                                              end;
1480                                           end if;
1481
1482                                           exit when Add_It;
1483                                        end loop;
1484                                     end if;
1485
1486                                     if Add_It then
1487                                        Objects_Htable.Set (Id, True);
1488                                        Objects.Append
1489                                          (new String'(Object_Path));
1490
1491                                        --  Record the ALI file
1492
1493                                        ALIs.Append (new String'(ALI_Path));
1494
1495                                        --  Find out if for this ALI file,
1496                                        --  libgnarl or libdecgnat or
1497                                        --  g-trasym.obj (on OpenVMS) is
1498                                        --  necessary.
1499
1500                                        Check_Libs (ALI_Path, True);
1501                                     end if;
1502
1503                                  elsif There_Are_Foreign_Sources then
1504                                     Objects.Append (new String'(Object_Path));
1505                                  end if;
1506                               end;
1507                            end if;
1508                         end if;
1509                      end;
1510                   end if;
1511                end loop;
1512
1513                Close (Dir => Object_Dir);
1514
1515             exception
1516                when Directory_Error =>
1517                   Com.Fail ("cannot find object directory """,
1518                             Get_Name_String (Data.Object_Directory),
1519                             """");
1520             end;
1521
1522             exit when Data.Extends = No_Project;
1523
1524             In_Main_Object_Directory  := False;
1525             Data := In_Tree.Projects.Table (Data.Extends);
1526          end loop;
1527
1528          --  Add the -L and -l switches for the imported Library Project Files,
1529          --  and, if Path Option is supported, the library directory path names
1530          --  to Rpath.
1531
1532          Process_Imported_Libraries;
1533
1534          --  Link with libgnat and possibly libgnarl
1535
1536          Opts.Increment_Last;
1537          Opts.Table (Opts.Last) := new String'("-L" & Lib_Directory);
1538
1539          --  If Path Option is supported, add libgnat directory path name to
1540          --  Rpath.
1541
1542          if Path_Option /= null then
1543             declare
1544                Libdir    : constant String := Lib_Directory;
1545                GCC_Index : Natural := 0;
1546
1547             begin
1548                Add_Rpath (Libdir);
1549
1550                --  For shared libraries, add to the Path Option the directory
1551                --  of the shared version of libgcc.
1552
1553                if The_Build_Mode /= Static then
1554                   GCC_Index := Index (Libdir, "/lib/");
1555
1556                   if GCC_Index = 0 then
1557                      GCC_Index :=
1558                        Index
1559                          (Libdir,
1560                           Directory_Separator & "lib" & Directory_Separator);
1561                   end if;
1562
1563                   if GCC_Index /= 0 then
1564                      Add_Rpath (Libdir (Libdir'First .. GCC_Index + 3));
1565                   end if;
1566                end if;
1567             end;
1568          end if;
1569
1570          if Libgnarl_Needed = Yes then
1571             Opts.Increment_Last;
1572
1573             if The_Build_Mode = Static then
1574                Opts.Table (Opts.Last) := new String'("-lgnarl");
1575             else
1576                Opts.Table (Opts.Last) := new String'(Shared_Lib ("gnarl"));
1577             end if;
1578          end if;
1579
1580          if Gtrasymobj_Needed then
1581             Opts.Increment_Last;
1582             Opts.Table (Opts.Last) :=
1583               new String'(Lib_Directory & "/g-trasym.obj");
1584          end if;
1585
1586          if Libdecgnat_Needed then
1587             Opts.Increment_Last;
1588
1589             Opts.Table (Opts.Last) :=
1590               new String'("-L" & Lib_Directory & "/../declib");
1591
1592             Opts.Increment_Last;
1593
1594             if The_Build_Mode = Static then
1595                Opts.Table (Opts.Last) := new String'("-ldecgnat");
1596             else
1597                Opts.Table (Opts.Last) := new String'(Shared_Lib ("decgnat"));
1598             end if;
1599          end if;
1600
1601          Opts.Increment_Last;
1602
1603          if The_Build_Mode = Static then
1604             Opts.Table (Opts.Last) := new String'("-lgnat");
1605          else
1606             Opts.Table (Opts.Last) := new String'(Shared_Lib ("gnat"));
1607          end if;
1608
1609          --  If Path Option is supported, add the necessary switch with the
1610          --  content of Rpath. As Rpath contains at least libgnat directory
1611          --  path name, it is guaranteed that it is not null.
1612
1613          if Path_Option /= null then
1614             Opts.Increment_Last;
1615             Opts.Table (Opts.Last) :=
1616               new String'(Path_Option.all & Rpath (1 .. Rpath_Last));
1617             Free (Path_Option);
1618             Free (Rpath);
1619          end if;
1620
1621          Object_Files :=
1622            new Argument_List'
1623              (Argument_List (Objects.Table (1 .. Objects.Last)));
1624
1625          Ali_Files :=
1626            new Argument_List'(Argument_List (ALIs.Table (1 .. ALIs.Last)));
1627
1628          Options :=
1629            new Argument_List'(Argument_List (Opts.Table (1 .. Opts.Last)));
1630
1631          --  We fail if there are no object to put in the library
1632          --  (Ada or foreign objects).
1633
1634          if Object_Files'Length = 0 then
1635             Com.Fail ("no object files for library """ &
1636                       Lib_Filename.all & '"');
1637          end if;
1638
1639          if not Opt.Quiet_Output then
1640             Write_Eol;
1641             Write_Str  ("building ");
1642             Write_Str (Ada.Characters.Handling.To_Lower
1643                          (Build_Mode_State'Image (The_Build_Mode)));
1644             Write_Str  (" library for project ");
1645             Write_Line (Project_Name);
1646
1647             --  Only output list of object files and ALI files in verbose mode
1648
1649             if Opt.Verbose_Mode then
1650                Write_Eol;
1651
1652                Write_Line ("object files:");
1653
1654                for Index in Object_Files'Range loop
1655                   Write_Str  ("   ");
1656                   Write_Line (Object_Files (Index).all);
1657                end loop;
1658
1659                Write_Eol;
1660
1661                if Ali_Files'Length = 0 then
1662                   Write_Line ("NO ALI files");
1663
1664                else
1665                   Write_Line ("ALI files:");
1666
1667                   for Index in Ali_Files'Range loop
1668                      Write_Str  ("   ");
1669                      Write_Line (Ali_Files (Index).all);
1670                   end loop;
1671                end if;
1672
1673                Write_Eol;
1674             end if;
1675          end if;
1676
1677          --  We check that all object files are regular files
1678
1679          Check_Context;
1680
1681          --  Delete the existing library file, if it exists. Fail if the
1682          --  library file is not writable, or if it is not possible to delete
1683          --  the file.
1684
1685          declare
1686             DLL_Name : aliased String :=
1687                          Lib_Dirpath.all & Directory_Separator & DLL_Prefix &
1688                            Lib_Filename.all & "." & DLL_Ext;
1689
1690             Archive_Name : aliased String :=
1691                              Lib_Dirpath.all & Directory_Separator & "lib" &
1692                                Lib_Filename.all & "." & Archive_Ext;
1693
1694             type Str_Ptr is access all String;
1695             --  This type is necessary to meet the accessibility rules of Ada.
1696             --  It is not possible to use String_Access here.
1697
1698             Full_Lib_Name : Str_Ptr;
1699             --  Designates the full library path name. Either DLL_Name or
1700             --  Archive_Name, depending on the library kind.
1701
1702             Success : Boolean;
1703             pragma Warnings (Off, Success);
1704             --  Used to call Delete_File
1705
1706          begin
1707             if The_Build_Mode = Static then
1708                Full_Lib_Name := Archive_Name'Access;
1709             else
1710                Full_Lib_Name := DLL_Name'Access;
1711             end if;
1712
1713             if Is_Regular_File (Full_Lib_Name.all) then
1714                if Is_Writable_File (Full_Lib_Name.all) then
1715                   Delete_File (Full_Lib_Name.all, Success);
1716                end if;
1717
1718                if Is_Regular_File (Full_Lib_Name.all) then
1719                   Com.Fail ("could not delete """ & Full_Lib_Name.all & """");
1720                end if;
1721             end if;
1722          end;
1723
1724          Argument_Number := 0;
1725
1726          --  If we have a standalone library, gather all the interface ALI.
1727          --  They are passed to Build_Dynamic_Library, where they are used by
1728          --  some platforms (VMS, for example) to decide what symbols should be
1729          --  exported. They are also flagged as Interface when we copy them to
1730          --  the library directory (by Copy_ALI_Files, below).
1731
1732          if Standalone then
1733             Data := In_Tree.Projects.Table (For_Project);
1734
1735             declare
1736                Iface : String_List_Id := Data.Lib_Interface_ALIs;
1737                ALI   : File_Name_Type;
1738
1739             begin
1740                while Iface /= Nil_String loop
1741                   ALI :=
1742                     File_Name_Type
1743                       (In_Tree.String_Elements.Table (Iface).Value);
1744                   Interface_ALIs.Set (ALI, True);
1745                   Get_Name_String
1746                     (In_Tree.String_Elements.Table (Iface).Value);
1747                   Add_Argument (Name_Buffer (1 .. Name_Len));
1748                   Iface := In_Tree.String_Elements.Table (Iface).Next;
1749                end loop;
1750
1751                Iface := Data.Lib_Interface_ALIs;
1752
1753                if not Opt.Quiet_Output then
1754
1755                   --  Check that the interface set is complete: any unit in the
1756                   --  library that is needed by an interface should also be an
1757                   --  interface. If it is not the case, output a warning.
1758
1759                   while Iface /= Nil_String loop
1760                      ALI :=
1761                        File_Name_Type
1762                          (In_Tree.String_Elements.Table (Iface).Value);
1763                      Process (ALI);
1764                      Iface := In_Tree.String_Elements.Table (Iface).Next;
1765                   end loop;
1766                end if;
1767             end;
1768          end if;
1769
1770          declare
1771             Current_Dir  : constant String := Get_Current_Dir;
1772             Dir          : Dir_Type;
1773
1774             Name : String (1 .. 200);
1775             Last : Natural;
1776
1777             Disregard : Boolean;
1778             pragma Warnings (Off, Disregard);
1779
1780             DLL_Name : aliased constant String :=
1781                          Lib_Filename.all & "." & DLL_Ext;
1782
1783             Archive_Name : aliased constant String :=
1784                              Lib_Filename.all & "." & Archive_Ext;
1785
1786             Delete : Boolean := False;
1787
1788          begin
1789             --  Clean the library directory: remove any file with the name of
1790             --  the library file and any ALI file of a source of the project.
1791
1792             begin
1793                Get_Name_String
1794                  (In_Tree.Projects.Table (For_Project).Library_Dir);
1795                Change_Dir (Name_Buffer (1 .. Name_Len));
1796
1797             exception
1798                when others =>
1799                   Com.Fail
1800                     ("unable to access library directory """,
1801                      Name_Buffer (1 .. Name_Len),
1802                      """");
1803             end;
1804
1805             Open (Dir, ".");
1806
1807             loop
1808                Read (Dir, Name, Last);
1809                exit when Last = 0;
1810
1811                declare
1812                   Filename : constant String := Name (1 .. Last);
1813
1814                begin
1815                   if Is_Regular_File (Filename) then
1816                      Canonical_Case_File_Name (Name (1 .. Last));
1817                      Delete := False;
1818
1819                      if (The_Build_Mode = Static and then
1820                            Name (1 .. Last) =  Archive_Name)
1821                        or else
1822                          ((The_Build_Mode = Dynamic or else
1823                              The_Build_Mode = Relocatable)
1824                           and then
1825                             Name (1 .. Last) = DLL_Name)
1826                      then
1827                         Delete := True;
1828
1829                      elsif Last > 4
1830                        and then Name (Last - 3 .. Last) = ".ali"
1831                      then
1832                         declare
1833                            Unit : Unit_Data;
1834
1835                         begin
1836                            --  Compare with ALI file names of the project
1837
1838                            for Index in
1839                              1 .. Unit_Table.Last (In_Tree.Units)
1840                            loop
1841                               Unit := In_Tree.Units.Table (Index);
1842
1843                               if Unit.File_Names (Body_Part).Project /=
1844                                 No_Project
1845                               then
1846                                  if  Ultimate_Extension_Of
1847                                    (Unit.File_Names (Body_Part).Project,
1848                                     In_Tree) = For_Project
1849                                  then
1850                                     Get_Name_String
1851                                       (Unit.File_Names (Body_Part).Name);
1852                                     Name_Len := Name_Len -
1853                                       File_Extension
1854                                         (Name (1 .. Name_Len))'Length;
1855                                     if Name_Buffer (1 .. Name_Len) =
1856                                       Name (1 .. Last - 4)
1857                                     then
1858                                        Delete := True;
1859                                        exit;
1860                                     end if;
1861                                  end if;
1862
1863                               elsif Ultimate_Extension_Of
1864                                 (Unit.File_Names (Specification).Project,
1865                                  In_Tree) = For_Project
1866                               then
1867                                  Get_Name_String
1868                                    (Unit.File_Names (Specification).Name);
1869                                  Name_Len :=
1870                                    Name_Len -
1871                                    File_Extension
1872                                      (Name (1 .. Name_Len))'Length;
1873
1874                                  if Name_Buffer (1 .. Name_Len) =
1875                                    Name (1 .. Last - 4)
1876                                  then
1877                                     Delete := True;
1878                                     exit;
1879                                  end if;
1880                               end if;
1881                            end loop;
1882                         end;
1883                      end if;
1884
1885                      if Delete then
1886                         Set_Writable (Filename);
1887                         Delete_File (Filename, Disregard);
1888                      end if;
1889                   end if;
1890                end;
1891             end loop;
1892
1893             Close (Dir);
1894
1895             Change_Dir (Current_Dir);
1896          end;
1897
1898          --  Call procedure to build the library, depending on the build mode
1899
1900          case The_Build_Mode is
1901             when Dynamic | Relocatable =>
1902                Build_Dynamic_Library
1903                  (Ofiles        => Object_Files.all,
1904                   Options       => Options.all,
1905                   Interfaces    => Arguments (1 .. Argument_Number),
1906                   Lib_Filename  => Lib_Filename.all,
1907                   Lib_Dir       => Lib_Dirpath.all,
1908                   Symbol_Data   => Data.Symbol_Data,
1909                   Driver_Name   => Driver_Name,
1910                   Lib_Version   => Lib_Version.all,
1911                   Auto_Init     => Data.Lib_Auto_Init);
1912
1913             when Static =>
1914                MLib.Build_Library
1915                  (Object_Files.all,
1916                   Lib_Filename.all,
1917                   Lib_Dirpath.all);
1918
1919             when None =>
1920                null;
1921          end case;
1922
1923          --  We need to copy the ALI files from the object directory to the
1924          --  library ALI directory, so that the linker find them there, and
1925          --  does not need to look in the object directory where it would also
1926          --  find the object files; and we don't want that: we want the linker
1927          --  to use the library.
1928
1929          --  Copy the ALI files and make the copies read-only. For interfaces,
1930          --  mark the copies as interfaces.
1931
1932          Copy_ALI_Files
1933            (Files      => Ali_Files.all,
1934             To         => In_Tree.Projects.Table (For_Project).Library_ALI_Dir,
1935             Interfaces => Arguments (1 .. Argument_Number));
1936
1937          --  Copy interface sources if Library_Src_Dir specified
1938
1939          if Standalone
1940            and then In_Tree.Projects.Table
1941                       (For_Project).Library_Src_Dir /= No_Path
1942          then
1943             --  Clean the interface copy directory: remove any source that
1944             --  could be a source of the project.
1945
1946             begin
1947                Get_Name_String
1948                  (In_Tree.Projects.Table (For_Project).Library_Src_Dir);
1949                Change_Dir (Name_Buffer (1 .. Name_Len));
1950
1951             exception
1952                when others =>
1953                   Com.Fail
1954                     ("unable to access library source copy directory """,
1955                      Name_Buffer (1 .. Name_Len),
1956                      """");
1957             end;
1958
1959             declare
1960                Dir    : Dir_Type;
1961                Delete : Boolean := False;
1962                Unit   : Unit_Data;
1963
1964                Name : String (1 .. 200);
1965                Last : Natural;
1966
1967                Disregard : Boolean;
1968                pragma Warnings (Off, Disregard);
1969
1970             begin
1971                Open (Dir, ".");
1972
1973                loop
1974                   Read (Dir, Name, Last);
1975                   exit when Last = 0;
1976
1977                   if Is_Regular_File (Name (1 .. Last)) then
1978                      Canonical_Case_File_Name (Name (1 .. Last));
1979                      Delete := False;
1980
1981                      --  Compare with source file names of the project
1982
1983                      for Index in 1 .. Unit_Table.Last (In_Tree.Units) loop
1984                         Unit := In_Tree.Units.Table (Index);
1985
1986                         if Ultimate_Extension_Of
1987                             (Unit.File_Names (Body_Part).Project, In_Tree) =
1988                             For_Project
1989                           and then
1990                             Get_Name_String
1991                               (Unit.File_Names (Body_Part).Name) =
1992                             Name (1 .. Last)
1993                         then
1994                            Delete := True;
1995                            exit;
1996                         end if;
1997
1998                         if Ultimate_Extension_Of
1999                            (Unit.File_Names (Specification).Project, In_Tree) =
2000                            For_Project
2001                           and then
2002                            Get_Name_String
2003                              (Unit.File_Names (Specification).Name) =
2004                            Name (1 .. Last)
2005                         then
2006                            Delete := True;
2007                            exit;
2008                         end if;
2009                      end loop;
2010                   end if;
2011
2012                   if Delete then
2013                      Set_Writable (Name (1 .. Last));
2014                      Delete_File (Name (1 .. Last), Disregard);
2015                   end if;
2016                end loop;
2017
2018                Close (Dir);
2019             end;
2020
2021             Copy_Interface_Sources
2022               (For_Project => For_Project,
2023                In_Tree     => In_Tree,
2024                Interfaces  => Arguments (1 .. Argument_Number),
2025                To_Dir      => In_Tree.Projects.Table
2026                                 (For_Project).Display_Library_Src_Dir);
2027          end if;
2028       end if;
2029
2030       --  Reset the current working directory to its previous value
2031
2032       Change_Dir (Current_Dir);
2033    end Build_Library;
2034
2035    -----------
2036    -- Check --
2037    -----------
2038
2039    procedure Check (Filename : String) is
2040    begin
2041       if not Is_Regular_File (Filename) then
2042          Com.Fail (Filename, " not found.");
2043       end if;
2044    end Check;
2045
2046    -------------------
2047    -- Check_Context --
2048    -------------------
2049
2050    procedure Check_Context is
2051    begin
2052       --  Check that each object file exists
2053
2054       for F in Object_Files'Range loop
2055          Check (Object_Files (F).all);
2056       end loop;
2057    end Check_Context;
2058
2059    -------------------
2060    -- Check_Library --
2061    -------------------
2062
2063    procedure Check_Library
2064      (For_Project : Project_Id; In_Tree : Project_Tree_Ref)
2065    is
2066       Data    : constant Project_Data :=
2067                   In_Tree.Projects.Table (For_Project);
2068       Lib_TS  : Time_Stamp_Type;
2069       Current : constant Dir_Name_Str := Get_Current_Dir;
2070
2071    begin
2072       --  No need to build the library if there is no object directory,
2073       --  hence no object files to build the library.
2074
2075       if Data.Library then
2076          declare
2077             Lib_Name : constant File_Name_Type :=
2078               Library_File_Name_For (For_Project, In_Tree);
2079          begin
2080             Change_Dir (Get_Name_String (Data.Library_Dir));
2081             Lib_TS := File_Stamp (Lib_Name);
2082             In_Tree.Projects.Table (For_Project).Library_TS := Lib_TS;
2083          end;
2084
2085          if not Data.Externally_Built
2086            and then not Data.Need_To_Build_Lib
2087            and then Data.Object_Directory /= No_Path
2088          then
2089             declare
2090                Obj_TS     : Time_Stamp_Type;
2091                Object_Dir : Dir_Type;
2092
2093             begin
2094                if OpenVMS_On_Target then
2095                   B_Start := new String'("b__");
2096                end if;
2097
2098                --  If the library file does not exist, then the time stamp will
2099                --  be Empty_Time_Stamp, earlier than any other time stamp.
2100
2101                Change_Dir (Get_Name_String (Data.Object_Directory));
2102                Open (Dir => Object_Dir, Dir_Name => ".");
2103
2104                --  For all entries in the object directory
2105
2106                loop
2107                   Read (Object_Dir, Name_Buffer, Name_Len);
2108                   exit when Name_Len = 0;
2109
2110                   --  Check if it is an object file, but ignore any binder
2111                   --  generated file.
2112
2113                   if Is_Obj (Name_Buffer (1 .. Name_Len))
2114                     and then Name_Buffer (1 .. B_Start'Length) /= B_Start.all
2115                   then
2116                      --  Get the object file time stamp
2117
2118                      Obj_TS := File_Stamp (File_Name_Type'(Name_Find));
2119
2120                      --  If library file time stamp is earlier, set
2121                      --  Need_To_Build_Lib and return. String comparison is
2122                      --  used, otherwise time stamps may be too close and the
2123                      --  comparison would return True, which would trigger
2124                      --  an unnecessary rebuild of the library.
2125
2126                      if String (Lib_TS) < String (Obj_TS) then
2127
2128                         --  Library must be rebuilt
2129
2130                         In_Tree.Projects.Table
2131                           (For_Project).Need_To_Build_Lib := True;
2132                         exit;
2133                      end if;
2134                   end if;
2135                end loop;
2136
2137                Close (Object_Dir);
2138             end;
2139          end if;
2140
2141          Change_Dir (Current);
2142       end if;
2143    end Check_Library;
2144
2145    ----------------------------
2146    -- Copy_Interface_Sources --
2147    ----------------------------
2148
2149    procedure Copy_Interface_Sources
2150      (For_Project : Project_Id;
2151       In_Tree     : Project_Tree_Ref;
2152       Interfaces  : Argument_List;
2153       To_Dir      : Path_Name_Type)
2154    is
2155       Current : constant Dir_Name_Str := Get_Current_Dir;
2156       --  The current directory, where to return to at the end
2157
2158       Target : constant Dir_Name_Str := Get_Name_String (To_Dir);
2159       --  The directory where to copy sources
2160
2161       Text     : Text_Buffer_Ptr;
2162       The_ALI  : ALI.ALI_Id;
2163       Lib_File : File_Name_Type;
2164
2165       First_Unit  : ALI.Unit_Id;
2166       Second_Unit : ALI.Unit_Id;
2167
2168       Data : Unit_Data;
2169
2170       Copy_Subunits : Boolean := False;
2171       --  When True, indicates that subunits, if any, need to be copied too
2172
2173       procedure Copy (File_Name : File_Name_Type);
2174       --  Copy one source of the project to the target directory
2175
2176       function Is_Same_Or_Extension
2177         (Extending : Project_Id;
2178          Extended  : Project_Id) return Boolean;
2179       --  Return True if project Extending is equal to or extends project
2180       --  Extended.
2181
2182       ----------
2183       -- Copy --
2184       ----------
2185
2186       procedure Copy (File_Name : File_Name_Type) is
2187          Success : Boolean;
2188          pragma Warnings (Off, Success);
2189
2190       begin
2191          Unit_Loop :
2192          for Index in Unit_Table.First ..
2193                       Unit_Table.Last (In_Tree.Units)
2194          loop
2195             Data := In_Tree.Units.Table (Index);
2196
2197             --  Find and copy the immediate or inherited source
2198
2199             for J in Data.File_Names'Range loop
2200                if Is_Same_Or_Extension
2201                     (For_Project, Data.File_Names (J).Project)
2202                  and then Data.File_Names (J).Name = File_Name
2203                then
2204                   Copy_File
2205                     (Get_Name_String (Data.File_Names (J).Path),
2206                      Target,
2207                      Success,
2208                      Mode => Overwrite,
2209                      Preserve => Preserve);
2210                   exit Unit_Loop;
2211                end if;
2212             end loop;
2213          end loop Unit_Loop;
2214       end Copy;
2215
2216       --------------------------
2217       -- Is_Same_Or_Extension --
2218       --------------------------
2219
2220       function Is_Same_Or_Extension
2221         (Extending : Project_Id;
2222          Extended  : Project_Id) return Boolean
2223       is
2224          Ext : Project_Id;
2225
2226       begin
2227          Ext := Extending;
2228          while Ext /= No_Project loop
2229             if Ext = Extended then
2230                return True;
2231             end if;
2232
2233             Ext := In_Tree.Projects.Table (Ext).Extends;
2234          end loop;
2235
2236          return False;
2237       end Is_Same_Or_Extension;
2238
2239    --  Start of processing for Copy_Interface_Sources
2240
2241    begin
2242       --  Change the working directory to the object directory
2243
2244       Change_Dir
2245         (Get_Name_String
2246            (In_Tree.Projects.Table
2247               (For_Project).Object_Directory));
2248
2249       for Index in Interfaces'Range loop
2250
2251          --  First, load the ALI file
2252
2253          Name_Len := 0;
2254          Add_Str_To_Name_Buffer (Interfaces (Index).all);
2255          Lib_File := Name_Find;
2256          Text := Read_Library_Info (Lib_File);
2257          The_ALI := Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
2258          Free (Text);
2259
2260          Second_Unit := No_Unit_Id;
2261          First_Unit := ALI.ALIs.Table (The_ALI).First_Unit;
2262          Copy_Subunits := True;
2263
2264          --  If there is both a spec and a body, check if they are both needed
2265
2266          if ALI.Units.Table (First_Unit).Utype = Is_Body then
2267             Second_Unit := ALI.ALIs.Table (The_ALI).Last_Unit;
2268
2269             --  If the body is not needed, then reset First_Unit
2270
2271             if not ALI.Units.Table (Second_Unit).Body_Needed_For_SAL then
2272                First_Unit := No_Unit_Id;
2273                Copy_Subunits := False;
2274             end if;
2275
2276          elsif ALI.Units.Table (First_Unit).Utype = Is_Spec_Only then
2277             Copy_Subunits := False;
2278          end if;
2279
2280          --  Copy the file(s) that need to be copied
2281
2282          if First_Unit /= No_Unit_Id then
2283             Copy (File_Name => ALI.Units.Table (First_Unit).Sfile);
2284          end if;
2285
2286          if Second_Unit /= No_Unit_Id then
2287             Copy (File_Name => ALI.Units.Table (Second_Unit).Sfile);
2288          end if;
2289
2290          --  Copy all the separates, if any
2291
2292          if Copy_Subunits then
2293             for Dep in ALI.ALIs.Table (The_ALI).First_Sdep ..
2294               ALI.ALIs.Table (The_ALI).Last_Sdep
2295             loop
2296                if Sdep.Table (Dep).Subunit_Name /= No_Name then
2297                   Copy (File_Name => Sdep.Table (Dep).Sfile);
2298                end if;
2299             end loop;
2300          end if;
2301       end loop;
2302
2303       --  Restore the initial working directory
2304
2305       Change_Dir (Current);
2306    end Copy_Interface_Sources;
2307
2308    -------------
2309    -- Display --
2310    -------------
2311
2312    procedure Display (Executable : String) is
2313    begin
2314       if not Opt.Quiet_Output then
2315          Write_Str (Executable);
2316
2317          for Index in 1 .. Argument_Number loop
2318             Write_Char (' ');
2319             Write_Str (Arguments (Index).all);
2320          end loop;
2321
2322          Write_Eol;
2323       end if;
2324    end Display;
2325
2326    -----------
2327    -- Index --
2328    -----------
2329
2330    function Index (S, Pattern : String) return Natural is
2331       Len : constant Natural := Pattern'Length;
2332
2333    begin
2334       for J in reverse S'First .. S'Last - Len + 1 loop
2335          if Pattern = S (J .. J + Len - 1) then
2336             return J;
2337          end if;
2338       end loop;
2339
2340       return 0;
2341    end Index;
2342
2343    -------------------------
2344    -- Process_Binder_File --
2345    -------------------------
2346
2347    procedure Process_Binder_File (Name : String) is
2348       Fd : FILEs;
2349       --  Binder file's descriptor
2350
2351       Read_Mode : constant String := "r" & ASCII.Nul;
2352       --  For fopen
2353
2354       Status : Interfaces.C_Streams.int;
2355       pragma Unreferenced (Status);
2356       --  For fclose
2357
2358       Begin_Info : constant String := "--  BEGIN Object file/option list";
2359       End_Info   : constant String := "--  END Object file/option list   ";
2360
2361       Next_Line : String (1 .. 1000);
2362       --  Current line value
2363       --  Where does this odd constant 1000 come from, looks suspicious ???
2364
2365       Nlast : Integer;
2366       --  End of line slice (the slice does not contain the line terminator)
2367
2368       procedure Get_Next_Line;
2369       --  Read the next line from the binder file without the line terminator
2370
2371       -------------------
2372       -- Get_Next_Line --
2373       -------------------
2374
2375       procedure Get_Next_Line is
2376          Fchars : chars;
2377
2378       begin
2379          Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd);
2380
2381          if Fchars = System.Null_Address then
2382             Fail ("Error reading binder output");
2383          end if;
2384
2385          Nlast := 1;
2386          while Nlast <= Next_Line'Last
2387            and then Next_Line (Nlast) /= ASCII.LF
2388            and then Next_Line (Nlast) /= ASCII.CR
2389          loop
2390             Nlast := Nlast + 1;
2391          end loop;
2392
2393          Nlast := Nlast - 1;
2394       end Get_Next_Line;
2395
2396    --  Start of processing for Process_Binder_File
2397
2398    begin
2399       Fd := fopen (Name'Address, Read_Mode'Address);
2400
2401       if Fd = NULL_Stream then
2402          Fail ("Failed to open binder output");
2403       end if;
2404
2405       --  Skip up to the Begin Info line
2406
2407       loop
2408          Get_Next_Line;
2409          exit when Next_Line (1 .. Nlast) = Begin_Info;
2410       end loop;
2411
2412       --  Find the first switch
2413
2414       loop
2415          Get_Next_Line;
2416
2417          exit when Next_Line (1 .. Nlast) = End_Info;
2418
2419          --  As the binder generated file is in Ada, remove the first eight
2420          --  characters "   --   ".
2421
2422          Next_Line (1 .. Nlast - 8) := Next_Line (9 .. Nlast);
2423          Nlast := Nlast - 8;
2424
2425          --  Stop when the first switch is found
2426
2427          exit when Next_Line (1) = '-';
2428       end loop;
2429
2430       if Next_Line (1 .. Nlast) /= End_Info then
2431          loop
2432             --  Ignore -static and -shared, since -shared will be used
2433             --  in any case.
2434
2435             --  Ignore -lgnat, -lgnarl and -ldecgnat as they will be added
2436             --  later, because they are also needed for non Stand-Alone shared
2437             --  libraries.
2438
2439             --  Also ignore the shared libraries which are :
2440
2441             --  UNIX / Windows    VMS
2442             --  -lgnat-<version>  -lgnat_<version>  (7 + version'length chars)
2443             --  -lgnarl-<version> -lgnarl_<version> (8 + version'length chars)
2444
2445             if Next_Line (1 .. Nlast) /= "-static" and then
2446                Next_Line (1 .. Nlast) /= "-shared" and then
2447                Next_Line (1 .. Nlast) /= "-ldecgnat" and then
2448                Next_Line (1 .. Nlast) /= "-lgnarl" and then
2449                Next_Line (1 .. Nlast) /= "-lgnat" and then
2450                Next_Line
2451                  (1 .. Natural'Min (Nlast, 10 + Library_Version'Length)) /=
2452                    Shared_Lib ("decgnat") and then
2453                Next_Line
2454                  (1 .. Natural'Min (Nlast, 8 + Library_Version'Length)) /=
2455                    Shared_Lib ("gnarl") and then
2456                Next_Line
2457                  (1 .. Natural'Min (Nlast, 7 + Library_Version'Length)) /=
2458                    Shared_Lib ("gnat")
2459             then
2460                if Next_Line (1) /= '-' then
2461
2462                   --  This is not an option, should we add it?
2463
2464                   if Add_Object_Files then
2465                      Opts.Increment_Last;
2466                      Opts.Table (Opts.Last) :=
2467                        new String'(Next_Line (1 .. Nlast));
2468                   end if;
2469
2470                else
2471                   --  Add all other options
2472
2473                   Opts.Increment_Last;
2474                   Opts.Table (Opts.Last) :=
2475                     new String'(Next_Line (1 .. Nlast));
2476                end if;
2477             end if;
2478
2479             --  Next option, if any
2480
2481             Get_Next_Line;
2482             exit when Next_Line (1 .. Nlast) = End_Info;
2483
2484             --  Remove first eight characters "   --   "
2485
2486             Next_Line (1 .. Nlast - 8) := Next_Line (9 .. Nlast);
2487             Nlast := Nlast - 8;
2488          end loop;
2489       end if;
2490
2491       Status := fclose (Fd);
2492
2493       --  Is it really right to ignore any close error ???
2494
2495    end Process_Binder_File;
2496
2497    ------------------
2498    -- Reset_Tables --
2499    ------------------
2500
2501    procedure Reset_Tables is
2502    begin
2503       Objects.Init;
2504       Objects_Htable.Reset;
2505       ALIs.Init;
2506       Opts.Init;
2507       Processed_Projects.Reset;
2508       Library_Projs.Init;
2509    end Reset_Tables;
2510
2511    ---------------------------
2512    -- SALs_Use_Constructors --
2513    ---------------------------
2514
2515    function SALs_Use_Constructors return Boolean is
2516       function C_SALs_Init_Using_Constructors return Integer;
2517       pragma Import (C, C_SALs_Init_Using_Constructors,
2518                      "__gnat_sals_init_using_constructors");
2519    begin
2520       return C_SALs_Init_Using_Constructors /= 0;
2521    end SALs_Use_Constructors;
2522
2523    ---------------------------
2524    -- Ultimate_Extension_Of --
2525    ---------------------------
2526
2527    function Ultimate_Extension_Of
2528      (Project : Project_Id;
2529       In_Tree : Project_Tree_Ref) return Project_Id
2530    is
2531       Result : Project_Id := Project;
2532       Data   : Project_Data;
2533
2534    begin
2535       if Project /= No_Project then
2536          loop
2537             Data := In_Tree.Projects.Table (Result);
2538             exit when Data.Extended_By = No_Project;
2539             Result := Data.Extended_By;
2540          end loop;
2541       end if;
2542
2543       return Result;
2544    end Ultimate_Extension_Of;
2545
2546 end MLib.Prj;