1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2004, Ada Core Technologies, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, 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 COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
28 with Gnatvsn; use Gnatvsn;
30 with MLib.Fil; use MLib.Fil;
31 with MLib.Tgt; use MLib.Tgt;
32 with MLib.Utl; use MLib.Utl;
33 with Namet; use Namet;
35 with Osint; use Osint;
36 with Output; use Output;
37 with Prj.Com; use Prj.Com;
38 with Prj.Env; use Prj.Env;
39 with Prj.Util; use Prj.Util;
41 with Snames; use Snames;
42 with Switch; use Switch;
44 with Types; use Types;
46 with Ada.Characters.Handling;
48 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
50 with GNAT.OS_Lib; use GNAT.OS_Lib;
51 with Interfaces.C_Streams; use Interfaces.C_Streams;
52 with System; use System;
53 with System.Case_Util; use System.Case_Util;
55 package body MLib.Prj is
57 Prj_Add_Obj_Files : Types.Int;
58 pragma Import (C, Prj_Add_Obj_Files, "__gnat_prj_add_obj_files");
59 Add_Object_Files : constant Boolean := Prj_Add_Obj_Files /= 0;
60 -- Indicates if object files in pragmas Linker_Options (found in the
61 -- binder generated file) should be taken when linking aq stand-alone
63 -- False for Windows, True for other platforms.
65 ALI_Suffix : constant String := ".ali";
66 B_Start : String := "b~";
68 S_Osinte_Ads : Name_Id := No_Name;
69 -- Name_Id for "s-osinte.ads"
71 S_Dec_Ads : Name_Id := No_Name;
72 -- Name_Id for "dec.ads"
74 G_Trasym_Ads : Name_Id := No_Name;
75 -- Name_Id for "g-trasym.ads"
77 No_Argument_List : aliased String_List := (1 .. 0 => null);
78 No_Argument : constant String_List_Access := No_Argument_List'Access;
80 Arguments : String_List_Access := No_Argument;
81 -- Used to accumulate arguments for the invocation of gnatbind and of
82 -- the compiler. Also used to collect the interface ALI when copying
83 -- the ALI files to the library directory.
85 Argument_Number : Natural := 0;
86 -- Index of the last argument in Arguments
88 Initial_Argument_Max : constant := 10;
90 No_Main_String : aliased String := "-n";
91 No_Main : constant String_Access := No_Main_String'Access;
93 Output_Switch_String : aliased String := "-o";
94 Output_Switch : constant String_Access := Output_Switch_String'Access;
96 Compile_Switch_String : aliased String := "-c";
97 Compile_Switch : constant String_Access := Compile_Switch_String'Access;
99 -- List of objects to put inside the library
101 Object_Files : Argument_List_Access;
103 package Objects is new Table.Table
104 (Table_Name => "Mlib.Prj.Objects",
105 Table_Component_Type => String_Access,
106 Table_Index_Type => Natural,
107 Table_Low_Bound => 1,
109 Table_Increment => 100);
111 package Objects_Htable is new GNAT.HTable.Simple_HTable
112 (Header_Num => Com.Header_Num,
119 -- List of non-Ada object files
121 Foreign_Objects : Argument_List_Access;
123 package Foreigns is new Table.Table
124 (Table_Name => "Mlib.Prj.Foreigns",
125 Table_Component_Type => String_Access,
126 Table_Index_Type => Natural,
127 Table_Low_Bound => 1,
129 Table_Increment => 100);
133 Ali_Files : Argument_List_Access;
135 package ALIs is new Table.Table
136 (Table_Name => "Mlib.Prj.Alis",
137 Table_Component_Type => String_Access,
138 Table_Index_Type => Natural,
139 Table_Low_Bound => 1,
141 Table_Increment => 100);
143 -- List of options set in the command line.
145 Options : Argument_List_Access;
147 package Opts is new Table.Table
148 (Table_Name => "Mlib.Prj.Opts",
149 Table_Component_Type => String_Access,
150 Table_Index_Type => Natural,
151 Table_Low_Bound => 1,
153 Table_Increment => 100);
155 -- All the ALI file in the library
157 package Library_ALIs is new GNAT.HTable.Simple_HTable
158 (Header_Num => Com.Header_Num,
165 -- The ALI files in the interface sets
167 package Interface_ALIs is new GNAT.HTable.Simple_HTable
168 (Header_Num => Com.Header_Num,
175 -- The ALI files that have been processed to check if the corresponding
176 -- library unit is in the interface set.
178 package Processed_ALIs is new GNAT.HTable.Simple_HTable
179 (Header_Num => Com.Header_Num,
186 -- The projects imported directly or indirectly.
188 package Processed_Projects is new GNAT.HTable.Simple_HTable
189 (Header_Num => Com.Header_Num,
196 -- The library projects imported directly or indirectly.
198 package Library_Projs is new Table.Table (
199 Table_Component_Type => Project_Id,
200 Table_Index_Type => Integer,
201 Table_Low_Bound => 1,
203 Table_Increment => 10,
204 Table_Name => "Make.Library_Projs");
206 type Build_Mode_State is (None, Static, Dynamic, Relocatable);
208 procedure Add_Argument (S : String);
209 -- Add one argument to the array Arguments.
210 -- If Arguments is full, double its size.
212 function ALI_File_Name (Source : String) return String;
213 -- Return the ALI file name corresponding to a source.
215 procedure Check (Filename : String);
216 -- Check if filename is a regular file. Fail if it is not.
218 procedure Check_Context;
219 -- Check each object files in table Object_Files
220 -- Fail if any of them is not a regular file
222 procedure Clean (Directory : Name_Id);
223 -- Attempt to delete all files in Directory, but not subdirectories
225 procedure Copy_Interface_Sources
226 (For_Project : Project_Id;
227 Interfaces : Argument_List;
229 -- Copy the interface sources of a SAL to directory To_Dir
231 procedure Display (Executable : String);
232 -- Display invocation of gnatbind and of the compiler with the arguments
233 -- in Arguments, except when Quiet_Output is True.
235 procedure Process_Binder_File (Name : String);
236 -- For Stand-Alone libraries, get the Linker Options in the binder
239 procedure Reset_Tables;
240 -- Make sure that all the above tables are empty
241 -- (Objects, Foreign_Objects, Ali_Files, Options).
247 procedure Add_Argument (S : String) is
249 if Argument_Number = Arguments'Last then
251 New_Args : constant String_List_Access :=
252 new String_List (1 .. 2 * Arguments'Last);
255 -- Copy the String_Accesses and set them to null in Arguments
256 -- so that they will not be deallocated by the call to
259 New_Args (Arguments'Range) := Arguments.all;
260 Arguments.all := (others => null);
262 Arguments := New_Args;
266 Argument_Number := Argument_Number + 1;
267 Arguments (Argument_Number) := new String'(S);
274 function ALI_File_Name (Source : String) return String is
276 -- If the source name has an extension, then replace it with
279 for Index in reverse Source'First + 1 .. Source'Last loop
280 if Source (Index) = '.' then
281 return Source (Source'First .. Index - 1) & ALI_Suffix;
285 -- If there is no dot, or if it is the first character, just add the
288 return Source & ALI_Suffix;
295 procedure Build_Library
296 (For_Project : Project_Id;
298 Gnatbind_Path : String_Access;
300 Gcc_Path : String_Access;
301 Bind : Boolean := True;
302 Link : Boolean := True)
304 Warning_For_Library : Boolean := False;
305 -- Set to True for the first warning about a unit missing from the
308 Libgnarl_Needed : Boolean := False;
309 -- Set to True if library needs to be linked with libgnarl
311 Libdecgnat_Needed : Boolean := False;
312 -- On OpenVMS, set to True if library needs to be linked with libdecgnat
314 Gtrasymobj_Needed : Boolean := False;
315 -- On OpenVMS, set to True if library needs to be linked with
318 Data : Project_Data := Projects.Table (For_Project);
320 Object_Directory_Path : constant String :=
321 Get_Name_String (Data.Object_Directory);
323 Standalone : constant Boolean := Data.Standalone_Library;
325 Project_Name : constant String := Get_Name_String (Data.Name);
327 Current_Dir : constant String := Get_Current_Dir;
329 Lib_Filename : String_Access;
330 Lib_Dirpath : String_Access;
331 Lib_Version : String_Access := new String'("");
333 The_Build_Mode : Build_Mode_State := None;
335 Success : Boolean := False;
337 Library_Options : Variable_Value := Nil_Variable_Value;
339 Library_GCC : Variable_Value := Nil_Variable_Value;
341 Driver_Name : Name_Id := No_Name;
343 In_Main_Object_Directory : Boolean := True;
345 Rpath : String_Access := null;
346 -- Allocated only if Path Option is supported
348 Rpath_Last : Natural := 0;
349 -- Index of last valid character of Rpath
351 Initial_Rpath_Length : constant := 200;
352 -- Initial size of Rpath, when first allocated
354 Path_Option : String_Access := Linker_Library_Path_Option;
355 -- If null, Path Option is not supported.
356 -- Not a constant so that it can be deallocated.
359 -- Directory where to copy ALI files and possibly interface sources
361 First_ALI : Name_Id := No_Name;
362 -- Store the ALI file name of a source of the library (the first found)
364 procedure Add_ALI_For (Source : Name_Id);
365 -- Add the name of the ALI file corresponding to Source to the
368 procedure Add_Rpath (Path : String);
369 -- Add a path name to Rpath
371 function Check_Project (P : Project_Id) return Boolean;
372 -- Returns True if P is For_Project or a project extended by For_Project
374 procedure Check_Libs (ALI_File : String);
375 -- Set Libgnarl_Needed if the ALI_File indicates that there is a need
376 -- to link with -lgnarl (this is the case when there is a dependency
377 -- on s-osinte.ads). On OpenVMS, set Libdecgnat_Needed if the ALI file
378 -- indicates that there is a need to link with -ldecgnat (this is the
379 -- case when there is a dependency on dec.ads), and set
380 -- Gtrasymobj_Needed if there is a dependency on g-trasym.ads.
382 procedure Process (The_ALI : File_Name_Type);
383 -- Check if the closure of a library unit which is or should be in the
384 -- interface set is also in the interface set. Issue a warning for each
385 -- missing library unit.
387 procedure Process_Imported_Libraries;
388 -- Add the -L and -l switches for the imported Library Project Files,
389 -- and, if Path Option is supported, the library directory path names
396 procedure Add_ALI_For (Source : Name_Id) is
397 ALI : constant String := ALI_File_Name (Get_Name_String (Source));
406 Add_Str_To_Name_Buffer (S => ALI);
409 -- Add the ALI file name to the library ALIs
412 Library_ALIs.Set (ALI_Id, True);
415 -- Set First_ALI, if not already done
417 if First_ALI = No_Name then
426 procedure Add_Rpath (Path : String) is
436 New_Rpath : constant String_Access :=
437 new String (1 .. 2 * Rpath'Length);
439 New_Rpath (1 .. Rpath_Last) := Rpath (1 .. Rpath_Last);
444 -- Start of processing for Add_Rpath
447 -- If firt path, allocate initial Rpath
450 Rpath := new String (1 .. Initial_Rpath_Length);
454 -- Otherwise, add a path separator between two path names
456 if Rpath_Last = Rpath'Last then
460 Rpath_Last := Rpath_Last + 1;
461 Rpath (Rpath_Last) := Path_Separator;
464 -- Increase Rpath size until it is large enough
466 while Rpath_Last + Path'Length > Rpath'Last loop
472 Rpath (Rpath_Last + 1 .. Rpath_Last + Path'Length) := Path;
473 Rpath_Last := Rpath_Last + Path'Length;
480 function Check_Project (P : Project_Id) return Boolean is
482 if P = For_Project then
485 elsif P /= No_Project then
487 Data : Project_Data := Projects.Table (For_Project);
490 while Data.Extends /= No_Project loop
491 if P = Data.Extends then
495 Data := Projects.Table (Data.Extends);
507 procedure Check_Libs (ALI_File : String) is
509 Text : Text_Buffer_Ptr;
513 if not Libgnarl_Needed or
514 (Hostparm.OpenVMS and then
515 ((not Libdecgnat_Needed) or
516 (not Gtrasymobj_Needed)))
520 Name_Len := ALI_File'Length;
521 Name_Buffer (1 .. Name_Len) := ALI_File;
522 Lib_File := Name_Find;
523 Text := Read_Library_Info (Lib_File, True);
533 -- Look for s-osinte.ads in the dependencies
535 for Index in ALI.ALIs.Table (Id).First_Sdep ..
536 ALI.ALIs.Table (Id).Last_Sdep
538 if ALI.Sdep.Table (Index).Sfile = S_Osinte_Ads then
539 Libgnarl_Needed := True;
541 elsif Hostparm.OpenVMS then
542 if ALI.Sdep.Table (Index).Sfile = S_Dec_Ads then
543 Libdecgnat_Needed := True;
545 elsif ALI.Sdep.Table (Index).Sfile = G_Trasym_Ads then
546 Gtrasymobj_Needed := True;
557 procedure Process (The_ALI : File_Name_Type) is
558 Text : Text_Buffer_Ptr;
560 First_Unit : ALI.Unit_Id;
561 Last_Unit : ALI.Unit_Id;
562 Unit_Data : Unit_Record;
563 Afile : File_Name_Type;
566 -- Nothing to do if the ALI file has already been processed.
567 -- This happens if an interface imports another interface.
569 if not Processed_ALIs.Get (The_ALI) then
570 Processed_ALIs.Set (The_ALI, True);
571 Text := Read_Library_Info (The_ALI);
582 if Idread /= No_ALI_Id then
583 First_Unit := ALI.ALIs.Table (Idread).First_Unit;
584 Last_Unit := ALI.ALIs.Table (Idread).Last_Unit;
586 -- Process both unit (spec and body) if the body is needed
587 -- by the spec (inline or generic). Otherwise, just process
590 if First_Unit /= Last_Unit and then
591 not ALI.Units.Table (Last_Unit).Body_Needed_For_SAL
593 First_Unit := Last_Unit;
596 for Unit in First_Unit .. Last_Unit loop
597 Unit_Data := ALI.Units.Table (Unit);
599 -- Check if each withed unit which is in the library is
600 -- also in the interface set, if it has not yet been
603 for W in Unit_Data.First_With .. Unit_Data.Last_With loop
604 Afile := Withs.Table (W).Afile;
606 if Afile /= No_Name and then Library_ALIs.Get (Afile)
607 and then not Processed_ALIs.Get (Afile)
609 if not Interface_ALIs.Get (Afile) then
610 if not Warning_For_Library then
611 Write_Str ("Warning: In library project """);
612 Get_Name_String (Data.Name);
613 To_Mixed (Name_Buffer (1 .. Name_Len));
614 Write_Str (Name_Buffer (1 .. Name_Len));
616 Warning_For_Library := True;
619 Write_Str (" Unit """);
620 Get_Name_String (Withs.Table (W).Uname);
621 To_Mixed (Name_Buffer (1 .. Name_Len - 2));
622 Write_Str (Name_Buffer (1 .. Name_Len - 2));
623 Write_Line (""" is not in the interface set");
624 Write_Str (" but it is needed by ");
626 case Unit_Data.Utype is
628 Write_Str ("the spec of ");
631 Write_Str ("the body of ");
638 Get_Name_String (Unit_Data.Uname);
639 To_Mixed (Name_Buffer (1 .. Name_Len - 2));
640 Write_Str (Name_Buffer (1 .. Name_Len - 2));
644 -- Now, process this unit
655 --------------------------------
656 -- Process_Imported_Libraries --
657 --------------------------------
659 procedure Process_Imported_Libraries is
660 Current : Project_Id;
662 procedure Process_Project (Project : Project_Id);
663 -- Process Project and its imported projects recursively.
664 -- Add any library projects to table Library_Projs.
666 ---------------------
667 -- Process_Project --
668 ---------------------
670 procedure Process_Project (Project : Project_Id) is
671 Data : constant Project_Data := Projects.Table (Project);
672 Imported : Project_List := Data.Imported_Projects;
673 Element : Project_Element;
676 -- Nothing to do if process has already been processed
678 if not Processed_Projects.Get (Data.Name) then
679 Processed_Projects.Set (Data.Name, True);
681 -- Call Process_Project recursively for any imported project.
682 -- We first process the imported projects to guarantee that
683 -- we have a proper reverse order for the libraries.
685 while Imported /= Empty_Project_List loop
686 Element := Project_Lists.Table (Imported);
688 if Element.Project /= No_Project then
689 Process_Project (Element.Project);
692 Imported := Element.Next;
695 -- If it is a library project, add it to Library_Projs
697 if Project /= For_Project and then Data.Library then
698 Library_Projs.Increment_Last;
699 Library_Projs.Table (Library_Projs.Last) := Project;
705 -- Start of processing for Process_Imported_Libraries
708 -- Build list of library projects imported directly or indirectly,
709 -- in the reverse order.
711 Process_Project (For_Project);
713 -- Add the -L and -l switches and, if the Rpath option is supported,
714 -- add the directory to the Rpath.
715 -- As the library projects are in the wrong order, process from the
716 -- last to the first.
718 for Index in reverse 1 .. Library_Projs.Last loop
719 Current := Library_Projs.Table (Index);
721 Get_Name_String (Projects.Table (Current).Library_Dir);
723 Opts.Table (Opts.Last) :=
724 new String'("-L" & Name_Buffer (1 .. Name_Len));
726 if Path_Option /= null then
727 Add_Rpath (Name_Buffer (1 .. Name_Len));
731 Opts.Table (Opts.Last) :=
735 (Projects.Table (Current).Library_Name));
737 end Process_Imported_Libraries;
739 -- Start of processing for Build_Library
744 -- Fail if project is not a library project
746 if not Data.Library then
747 Com.Fail ("project """, Project_Name, """ has no library");
750 -- If this is the first time Build_Library is called, get the Name_Id
751 -- of "s-osinte.ads".
753 if S_Osinte_Ads = No_Name then
755 Add_Str_To_Name_Buffer ("s-osinte.ads");
756 S_Osinte_Ads := Name_Find;
759 if S_Dec_Ads = No_Name then
761 Add_Str_To_Name_Buffer ("dec.ads");
762 S_Dec_Ads := Name_Find;
765 if G_Trasym_Ads = No_Name then
767 Add_Str_To_Name_Buffer ("g-trasym.ads");
768 G_Trasym_Ads := Name_Find;
771 -- We work in the object directory
773 Change_Dir (Object_Directory_Path);
776 -- Call gnatbind only if Bind is True
779 if Gnatbind_Path = null then
780 Com.Fail ("unable to locate ", Gnatbind);
783 if Gcc_Path = null then
784 Com.Fail ("unable to locate ", Gcc);
787 -- Allocate Arguments, if it is the first time we see a standalone
790 if Arguments = No_Argument then
791 Arguments := new String_List (1 .. Initial_Argument_Max);
794 -- Add "-n -o b~<lib>.adb (b$<lib>.adb on VMS) -L<lib>"
796 Argument_Number := 2;
797 Arguments (1) := No_Main;
798 Arguments (2) := Output_Switch;
800 if Hostparm.OpenVMS then
801 B_Start (B_Start'Last) := '$';
805 (B_Start & Get_Name_String (Data.Library_Name) & ".adb");
806 Add_Argument ("-L" & Get_Name_String (Data.Library_Name));
808 -- Check if Binder'Default_Switches ("Ada) is defined. If it is,
809 -- add these switches to call gnatbind.
812 Binder_Package : constant Package_Id :=
814 (Name => Name_Binder,
815 In_Packages => Data.Decl.Packages);
818 if Binder_Package /= No_Package then
820 Defaults : constant Array_Element_Id :=
822 (Name => Name_Default_Switches,
825 (Binder_Package).Decl.Arrays);
826 Switches : Variable_Value := Nil_Variable_Value;
828 Switch : String_List_Id := Nil_String;
831 if Defaults /= No_Array_Element then
836 In_Array => Defaults);
838 if not Switches.Default then
839 Switch := Switches.Values;
841 while Switch /= Nil_String loop
844 (String_Elements.Table (Switch).Value));
845 Switch := String_Elements.Table (Switch).Next;
854 -- Get all the ALI files of the project file. We do that even if
855 -- Bind is False, so that First_ALI is set.
862 Interface_ALIs.Reset;
863 Processed_ALIs.Reset;
865 for Source in 1 .. Com.Units.Last loop
866 Unit := Com.Units.Table (Source);
868 if Unit.File_Names (Body_Part).Name /= No_Name
869 and then Unit.File_Names (Body_Part).Path /= Slash
872 Check_Project (Unit.File_Names (Body_Part).Project)
874 if Unit.File_Names (Specification).Name = No_Name then
876 Src_Ind : Source_File_Index;
879 Src_Ind := Sinput.P.Load_Project_File
884 -- Add the ALI file only if it is not a subunit
887 not Sinput.P.Source_File_Is_Subunit (Src_Ind)
890 (Unit.File_Names (Body_Part).Name);
896 Add_ALI_For (Unit.File_Names (Body_Part).Name);
901 elsif Unit.File_Names (Specification).Name /= No_Name
902 and then Unit.File_Names (Specification).Path /= Slash
903 and then Check_Project
904 (Unit.File_Names (Specification).Project)
906 Add_ALI_For (Unit.File_Names (Specification).Name);
912 -- Continue setup and call gnatbind if Bind is True
916 -- Get an eventual --RTS from the ALI file
918 if First_ALI /= No_Name then
927 T := Read_Library_Info (First_ALI, True);
932 (First_ALI, T, Ignore_ED => False, Err => False);
934 if A /= No_ALI_Id then
937 (ALI.ALIs.Table (A).First_Unit).First_Arg ..
939 (ALI.ALIs.Table (A).First_Unit).Last_Arg
941 -- Look for --RTS. If found, add the switch to call
945 Arg : String_Ptr renames Args.Table (Index);
948 Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
950 Add_Argument (Arg.all);
962 (Project => For_Project, Including_Libraries => True);
964 -- Display the gnatbind command, if not in quiet output
971 (Gnatbind_Path.all, Arguments (1 .. Argument_Number), Success);
974 Com.Fail ("could not bind standalone library ",
975 Get_Name_String (Data.Library_Name));
979 -- Compile the binder generated file only if Link is true
985 (Project => For_Project, Including_Libraries => True);
987 -- Invoke <gcc> -c b$$<lib>.adb
989 -- Allocate Arguments, if it is the first time we see a standalone
992 if Arguments = No_Argument then
993 Arguments := new String_List (1 .. Initial_Argument_Max);
996 Argument_Number := 1;
997 Arguments (1) := Compile_Switch;
999 if Hostparm.OpenVMS then
1000 B_Start (B_Start'Last) := '$';
1004 (B_Start & Get_Name_String (Data.Library_Name) & ".adb");
1006 -- If necessary, add the PIC option
1008 if PIC_Option /= "" then
1009 Add_Argument (PIC_Option);
1012 -- Get the back-end switches and --RTS from the ALI file
1014 if First_ALI /= No_Name then
1017 T : Text_Buffer_Ptr;
1021 -- Load the ALI file
1023 T := Read_Library_Info (First_ALI, True);
1028 (First_ALI, T, Ignore_ED => False, Err => False);
1030 if A /= No_ALI_Id then
1033 (ALI.ALIs.Table (A).First_Unit).First_Arg ..
1035 (ALI.ALIs.Table (A).First_Unit).Last_Arg
1037 -- Do not compile with the front end switches except
1041 Arg : String_Ptr renames Args.Table (Index);
1043 if not Is_Front_End_Switch (Arg.all)
1045 Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1047 Add_Argument (Arg.all);
1055 -- Now that all the arguments are set, compile the binder
1060 (Gcc_Path.all, Arguments (1 .. Argument_Number), Success);
1064 ("could not compile binder generated file for library ",
1065 Get_Name_String (Data.Library_Name));
1068 -- Process binder generated file for pragmas Linker_Options
1070 Process_Binder_File (Arguments (2).all & ASCII.NUL);
1074 -- Build the library only if Link is True
1077 -- If attribute Library_GCC was specified, get the driver name
1079 Library_GCC := Value_Of (Name_Library_GCC, Data.Decl.Attributes);
1081 if not Library_GCC.Default then
1082 Driver_Name := Library_GCC.Value;
1085 -- If attribute Library_Options was specified, add these additional
1089 Value_Of (Name_Library_Options, Data.Decl.Attributes);
1091 if not Library_Options.Default then
1093 Current : String_List_Id := Library_Options.Values;
1094 Element : String_Element;
1097 while Current /= Nil_String loop
1098 Element := String_Elements.Table (Current);
1099 Get_Name_String (Element.Value);
1101 if Name_Len /= 0 then
1102 Opts.Increment_Last;
1103 Opts.Table (Opts.Last) :=
1104 new String'(Name_Buffer (1 .. Name_Len));
1107 Current := Element.Next;
1112 Lib_Dirpath := new String'(Get_Name_String (Data.Library_Dir));
1113 Lib_Filename := new String'(Get_Name_String (Data.Library_Name));
1115 case Data.Library_Kind is
1117 The_Build_Mode := Static;
1120 The_Build_Mode := Dynamic;
1123 The_Build_Mode := Relocatable;
1125 if PIC_Option /= "" then
1126 Opts.Increment_Last;
1127 Opts.Table (Opts.Last) := new String'(PIC_Option);
1131 -- Get the library version, if any
1133 if Data.Lib_Internal_Name /= No_Name then
1135 new String'(Get_Name_String (Data.Lib_Internal_Name));
1138 -- Add the objects found in the object directory and the object
1139 -- directories of the extended files, if any, except for generated
1140 -- object files (b~.. or B$..) from extended projects.
1141 -- When there are one or more extended files, only add an object file
1142 -- if no object file with the same name have already been added.
1144 In_Main_Object_Directory := True;
1148 Object_Dir_Path : constant String :=
1149 Get_Name_String (Data.Object_Directory);
1150 Object_Dir : Dir_Type;
1151 Filename : String (1 .. 255);
1156 Open (Dir => Object_Dir, Dir_Name => Object_Dir_Path);
1158 -- For all entries in the object directory
1161 Read (Object_Dir, Filename, Last);
1165 -- Check if it is an object file
1167 if Is_Obj (Filename (1 .. Last)) then
1169 Object_Path : String :=
1171 (Object_Dir_Path & Directory_Separator &
1172 Filename (1 .. Last));
1175 Canonical_Case_File_Name (Object_Path);
1176 Canonical_Case_File_Name (Filename (1 .. Last));
1178 -- If in the object directory of an extended project,
1179 -- do not consider generated object files.
1181 if In_Main_Object_Directory
1183 or else Filename (1 .. B_Start'Length) /= B_Start
1186 Name_Buffer (1 .. Name_Len) := Filename (1 .. Last);
1189 if not Objects_Htable.Get (Id) then
1191 -- Record this object file
1193 Objects_Htable.Set (Id, True);
1194 Objects.Increment_Last;
1195 Objects.Table (Objects.Last) :=
1196 new String'(Object_Path);
1199 ALI_File : constant String :=
1200 Ext_To (Object_Path, "ali");
1203 if Is_Regular_File (ALI_File) then
1205 -- Record the ALI file
1207 ALIs.Increment_Last;
1208 ALIs.Table (ALIs.Last) :=
1209 new String'(ALI_File);
1211 -- Find out if for this ALI file,
1212 -- libgnarl or libdecgnat or g-trasym.obj
1213 -- (on OpenVMS) is necessary.
1215 Check_Libs (ALI_File);
1218 -- Object file is a foreign object file
1220 Foreigns.Increment_Last;
1221 Foreigns.Table (Foreigns.Last) :=
1222 new String'(Object_Path);
1231 Close (Dir => Object_Dir);
1234 when Directory_Error =>
1235 Com.Fail ("cannot find object directory """,
1236 Get_Name_String (Data.Object_Directory),
1240 exit when Data.Extends = No_Project;
1242 In_Main_Object_Directory := False;
1243 Data := Projects.Table (Data.Extends);
1246 -- Add the -L and -l switches for the imported Library Project Files,
1247 -- and, if Path Option is supported, the library directory path names
1250 Process_Imported_Libraries;
1252 -- Link with libgnat and possibly libgnarl
1254 Opts.Increment_Last;
1255 Opts.Table (Opts.Last) := new String'("-L" & Lib_Directory);
1257 -- If Path Option is supported, add libgnat directory path name to
1260 if Path_Option /= null then
1261 Add_Rpath (Lib_Directory);
1264 if Libgnarl_Needed then
1265 Opts.Increment_Last;
1267 if The_Build_Mode = Static then
1268 Opts.Table (Opts.Last) := new String'("-lgnarl");
1270 Opts.Table (Opts.Last) := new String'(Shared_Lib ("gnarl"));
1274 if Gtrasymobj_Needed then
1275 Opts.Increment_Last;
1276 Opts.Table (Opts.Last) :=
1277 new String'(Lib_Directory & "/g-trasym.obj");
1280 if Libdecgnat_Needed then
1281 Opts.Increment_Last;
1282 Opts.Table (Opts.Last) :=
1283 new String'("-L" & Lib_Directory & "/../declib");
1284 Opts.Increment_Last;
1285 Opts.Table (Opts.Last) := new String'("-ldecgnat");
1288 Opts.Increment_Last;
1290 if The_Build_Mode = Static then
1291 Opts.Table (Opts.Last) := new String'("-lgnat");
1293 Opts.Table (Opts.Last) := new String'(Shared_Lib ("gnat"));
1296 -- If Path Option is supported, add the necessary switch with the
1297 -- content of Rpath. As Rpath contains at least libgnat directory
1298 -- path name, it is guaranteed that it is not null.
1300 if Path_Option /= null then
1301 Opts.Increment_Last;
1302 Opts.Table (Opts.Last) :=
1303 new String'(Path_Option.all & Rpath (1 .. Rpath_Last));
1310 (Argument_List (Objects.Table (1 .. Objects.Last)));
1313 new Argument_List'(Argument_List
1314 (Foreigns.Table (1 .. Foreigns.Last)));
1317 new Argument_List'(Argument_List (ALIs.Table (1 .. ALIs.Last)));
1320 new Argument_List'(Argument_List (Opts.Table (1 .. Opts.Last)));
1322 -- We fail if there are no object to put in the library
1323 -- (Ada or foreign objects).
1325 if Object_Files'Length = 0 then
1326 Com.Fail ("no object files for library """ &
1327 Lib_Filename.all & '"');
1330 if not Opt.Quiet_Output then
1332 Write_Str ("building ");
1333 Write_Str (Ada.Characters.Handling.To_Lower
1334 (Build_Mode_State'Image (The_Build_Mode)));
1335 Write_Str (" library for project ");
1336 Write_Line (Project_Name);
1340 Write_Line ("object files:");
1342 for Index in Object_Files'Range loop
1344 Write_Line (Object_Files (Index).all);
1349 if Ali_Files'Length = 0 then
1350 Write_Line ("NO ALI files");
1353 Write_Line ("ALI files:");
1355 for Index in Ali_Files'Range loop
1357 Write_Line (Ali_Files (Index).all);
1364 -- We check that all object files are regular files
1368 -- Delete the existing library file, if it exists.
1369 -- Fail if the library file is not writable, or if it is not possible
1370 -- to delete the file.
1373 DLL_Name : aliased String :=
1374 Lib_Dirpath.all & "/lib" &
1375 Lib_Filename.all & "." & DLL_Ext;
1377 Archive_Name : aliased String :=
1378 Lib_Dirpath.all & "/lib" &
1379 Lib_Filename.all & "." & Archive_Ext;
1381 type Str_Ptr is access all String;
1382 -- This type is necessary to meet the accessibility rules of Ada.
1383 -- It is not possible to use String_Access here.
1385 Full_Lib_Name : Str_Ptr;
1386 -- Designates the full library path name. Either DLL_Name or
1387 -- Archive_Name, depending on the library kind.
1389 Success : Boolean := False;
1390 -- Used to call Delete_File
1393 if The_Build_Mode = Static then
1394 Full_Lib_Name := Archive_Name'Access;
1396 Full_Lib_Name := DLL_Name'Access;
1399 if Is_Regular_File (Full_Lib_Name.all) then
1400 if Is_Writable_File (Full_Lib_Name.all) then
1401 Delete_File (Full_Lib_Name.all, Success);
1404 if Is_Regular_File (Full_Lib_Name.all) then
1405 Com.Fail ("could not delete """ & Full_Lib_Name.all & """");
1410 Argument_Number := 0;
1412 -- If we have a standalone library, gather all the interface ALI.
1413 -- They are passed to Build_Dynamic_Library, where they are used by
1414 -- some platforms (VMS, for example) to decide what symbols should be
1415 -- exported. They are also flagged as Interface when we copy them to
1416 -- the library directory (by Copy_ALI_Files, below).
1419 Data := Projects.Table (For_Project);
1422 Interface : String_List_Id := Data.Lib_Interface_ALIs;
1423 ALI : File_Name_Type;
1426 while Interface /= Nil_String loop
1427 ALI := String_Elements.Table (Interface).Value;
1428 Interface_ALIs.Set (ALI, True);
1429 Get_Name_String (String_Elements.Table (Interface).Value);
1430 Add_Argument (Name_Buffer (1 .. Name_Len));
1431 Interface := String_Elements.Table (Interface).Next;
1434 Interface := Data.Lib_Interface_ALIs;
1436 if not Opt.Quiet_Output then
1438 -- Check that the interface set is complete: any unit in the
1439 -- library that is needed by an interface should also be an
1440 -- interface. If it is not the case, output a warning.
1442 while Interface /= Nil_String loop
1443 ALI := String_Elements.Table (Interface).Value;
1445 Interface := String_Elements.Table (Interface).Next;
1451 -- Clean the library directory, if it is also the directory where
1452 -- the ALI files are copied, either because there is no interface
1453 -- copy directory or because the interface copy directory is the
1454 -- same as the library directory.
1456 Copy_Dir := Projects.Table (For_Project).Library_Dir;
1459 -- Call procedure to build the library, depending on the build mode
1461 case The_Build_Mode is
1462 when Dynamic | Relocatable =>
1463 Build_Dynamic_Library
1464 (Ofiles => Object_Files.all,
1465 Foreign => Foreign_Objects.all,
1466 Afiles => Ali_Files.all,
1467 Options => Options.all,
1468 Interfaces => Arguments (1 .. Argument_Number),
1469 Lib_Filename => Lib_Filename.all,
1470 Lib_Dir => Lib_Dirpath.all,
1471 Symbol_Data => Data.Symbol_Data,
1472 Driver_Name => Driver_Name,
1473 Lib_Version => Lib_Version.all,
1474 Auto_Init => Data.Lib_Auto_Init);
1487 -- We need to copy the ALI files from the object directory to
1488 -- the library directory, so that the linker find them there,
1489 -- and does not need to look in the object directory where it
1490 -- would also find the object files; and we don't want that:
1491 -- we want the linker to use the library.
1493 -- Copy the ALI files and make the copies read-only. For interfaces,
1494 -- mark the copies as interfaces.
1497 (Files => Ali_Files.all,
1499 Interfaces => Arguments (1 .. Argument_Number));
1501 -- Copy interface sources if Library_Src_Dir specified
1504 and then Projects.Table (For_Project).Library_Src_Dir /= No_Name
1506 -- Clean the interface copy directory, if it is not also the
1507 -- library directory. If it is also the library directory, it
1508 -- has already been cleaned before generation of the library.
1510 if Projects.Table (For_Project).Library_Src_Dir /= Copy_Dir then
1511 Copy_Dir := Projects.Table (For_Project).Library_Src_Dir;
1515 Copy_Interface_Sources
1516 (For_Project => For_Project,
1517 Interfaces => Arguments (1 .. Argument_Number),
1518 To_Dir => Copy_Dir);
1522 -- Reset the current working directory to its previous value
1524 Change_Dir (Current_Dir);
1531 procedure Check (Filename : String) is
1533 if not Is_Regular_File (Filename) then
1534 Com.Fail (Filename, " not found.");
1542 procedure Check_Context is
1544 -- Check that each object file exists
1546 for F in Object_Files'Range loop
1547 Check (Object_Files (F).all);
1555 procedure Check_Library (For_Project : Project_Id) is
1556 Data : constant Project_Data := Projects.Table (For_Project);
1559 if Data.Library and not Data.Need_To_Build_Lib then
1561 Current : constant Dir_Name_Str := Get_Current_Dir;
1562 Lib_Name : constant Name_Id := Library_File_Name_For (For_Project);
1563 Lib_TS : Time_Stamp_Type;
1564 Obj_TS : Time_Stamp_Type;
1566 Object_Dir : Dir_Type;
1569 if Hostparm.OpenVMS then
1570 B_Start (B_Start'Last) := '$';
1573 Change_Dir (Get_Name_String (Data.Library_Dir));
1575 Lib_TS := File_Stamp (Lib_Name);
1577 -- If the library file does not exist, then the time stamp will
1578 -- be Empty_Time_Stamp, earlier than any other time stamp.
1580 Change_Dir (Get_Name_String (Data.Object_Directory));
1581 Open (Dir => Object_Dir, Dir_Name => ".");
1583 -- For all entries in the object directory
1586 Read (Object_Dir, Name_Buffer, Name_Len);
1587 exit when Name_Len = 0;
1589 -- Check if it is an object file, but ignore any binder
1592 if Is_Obj (Name_Buffer (1 .. Name_Len))
1593 and then Name_Buffer (1 .. B_Start'Length) /= B_Start
1595 -- Get the object file time stamp
1597 Obj_TS := File_Stamp (Name_Find);
1599 -- If library file time stamp is earlier, set
1600 -- Need_To_Build_Lib and return. String comparaison is used,
1601 -- otherwise time stamps may be too close and the
1602 -- comparaison would return True, which would trigger
1603 -- an unnecessary rebuild of the library.
1605 if String (Lib_TS) < String (Obj_TS) then
1607 -- Library must be rebuilt
1609 Projects.Table (For_Project).Need_To_Build_Lib := True;
1615 Change_Dir (Current);
1624 procedure Clean (Directory : Name_Id) is
1625 Current : constant Dir_Name_Str := Get_Current_Dir;
1629 Name : String (1 .. 200);
1632 Disregard : Boolean;
1634 procedure Set_Writable (Name : System.Address);
1635 pragma Import (C, Set_Writable, "__gnat_set_writable");
1638 Get_Name_String (Directory);
1640 -- Change the working directory to the directory to clean
1643 Change_Dir (Name_Buffer (1 .. Name_Len));
1648 ("unable to access directory """,
1649 Name_Buffer (1 .. Name_Len),
1655 -- For each regular file in the directory, make it writable and
1659 Read (Dir, Name, Last);
1662 if Is_Regular_File (Name (1 .. Last)) then
1663 Name (Last + 1) := ASCII.NUL;
1664 Set_Writable (Name (1)'Address);
1665 Delete_File (Name (1 .. Last), Disregard);
1671 -- Restore the initial working directory
1673 Change_Dir (Current);
1676 ----------------------------
1677 -- Copy_Interface_Sources --
1678 ----------------------------
1680 procedure Copy_Interface_Sources
1681 (For_Project : Project_Id;
1682 Interfaces : Argument_List;
1685 Current : constant Dir_Name_Str := Get_Current_Dir;
1686 Target : constant Dir_Name_Str := Get_Name_String (To_Dir);
1688 Text : Text_Buffer_Ptr;
1689 The_ALI : ALI.ALI_Id;
1692 First_Unit : ALI.Unit_Id;
1693 Second_Unit : ALI.Unit_Id;
1697 Copy_Subunits : Boolean := False;
1699 procedure Copy (File_Name : Name_Id);
1700 -- Copy one source of the project to the target directory
1706 procedure Copy (File_Name : Name_Id) is
1707 Success : Boolean := False;
1711 for Index in 1 .. Com.Units.Last loop
1712 Data := Com.Units.Table (Index);
1714 for J in Data.File_Names'Range loop
1715 if Data.File_Names (J).Project = For_Project
1716 and then Data.File_Names (J).Name = File_Name
1719 (Get_Name_String (Data.File_Names (J).Path),
1723 Preserve => Preserve);
1732 -- Start of processing for Copy_Interface_Sources
1735 -- Change the working directory to the object directory
1738 (Get_Name_String (Projects.Table (For_Project).Object_Directory));
1740 for Index in Interfaces'Range loop
1742 -- First, load the ALI file
1745 Add_Str_To_Name_Buffer (Interfaces (Index).all);
1746 Lib_File := Name_Find;
1747 Text := Read_Library_Info (Lib_File);
1748 The_ALI := Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
1751 Second_Unit := No_Unit_Id;
1752 First_Unit := ALI.ALIs.Table (The_ALI).First_Unit;
1753 Copy_Subunits := True;
1755 -- If there is both a spec and a body, check if they are both needed
1757 if ALI.Units.Table (First_Unit).Utype = Is_Body then
1758 Second_Unit := ALI.ALIs.Table (The_ALI).Last_Unit;
1760 -- If the body is not needed, then reset First_Unit
1762 if not ALI.Units.Table (Second_Unit).Body_Needed_For_SAL then
1763 First_Unit := No_Unit_Id;
1764 Copy_Subunits := False;
1767 elsif ALI.Units.Table (First_Unit).Utype = Is_Spec_Only then
1768 Copy_Subunits := False;
1771 -- Copy the file(s) that need to be copied
1773 if First_Unit /= No_Unit_Id then
1774 Copy (File_Name => ALI.Units.Table (First_Unit).Sfile);
1777 if Second_Unit /= No_Unit_Id then
1778 Copy (File_Name => ALI.Units.Table (Second_Unit).Sfile);
1781 -- Copy all the separates, if any
1783 if Copy_Subunits then
1784 for Dep in ALI.ALIs.Table (The_ALI).First_Sdep ..
1785 ALI.ALIs.Table (The_ALI).Last_Sdep
1787 if Sdep.Table (Dep).Subunit_Name /= No_Name then
1788 Copy (File_Name => Sdep.Table (Dep).Sfile);
1794 -- Restore the initial working directory
1796 Change_Dir (Current);
1797 end Copy_Interface_Sources;
1803 procedure Display (Executable : String) is
1805 if not Opt.Quiet_Output then
1806 Write_Str (Executable);
1808 for Index in 1 .. Argument_Number loop
1810 Write_Str (Arguments (Index).all);
1817 -------------------------
1818 -- Process_Binder_File --
1819 -------------------------
1821 procedure Process_Binder_File (Name : String) is
1823 -- Binder file's descriptor
1825 Read_Mode : constant String := "r" & ASCII.Nul;
1828 Status : Interfaces.C_Streams.int;
1829 pragma Unreferenced (Status);
1832 Begin_Info : constant String := "-- BEGIN Object file/option list";
1833 End_Info : constant String := "-- END Object file/option list ";
1835 Next_Line : String (1 .. 1000);
1836 -- Current line value
1837 -- Where does this odd constant 1000 come from, looks suspicious ???
1840 -- End of line slice (the slice does not contain the line terminator)
1842 procedure Get_Next_Line;
1843 -- Read the next line from the binder file without the line terminator
1849 procedure Get_Next_Line is
1853 Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd);
1855 if Fchars = System.Null_Address then
1856 Fail ("Error reading binder output");
1860 while Nlast <= Next_Line'Last
1861 and then Next_Line (Nlast) /= ASCII.LF
1862 and then Next_Line (Nlast) /= ASCII.CR
1870 -- Start of processing for Process_Binder_File
1873 Fd := fopen (Name'Address, Read_Mode'Address);
1875 if Fd = NULL_Stream then
1876 Fail ("Failed to open binder output");
1879 -- Skip up to the Begin Info line
1883 exit when Next_Line (1 .. Nlast) = Begin_Info;
1886 -- Find the first switch
1891 exit when Next_Line (1 .. Nlast) = End_Info;
1893 -- As the binder generated file is in Ada, remove the first eight
1894 -- characters " -- ".
1896 Next_Line (1 .. Nlast - 8) := Next_Line (9 .. Nlast);
1899 -- Stop when the first switch is found
1901 exit when Next_Line (1) = '-';
1904 if Next_Line (1 .. Nlast) /= End_Info then
1906 -- Ignore -static and -shared, since -shared will be used
1909 -- Ignore -lgnat, -lgnarl and -ldecgnat as they will be added
1910 -- later, because they are also needed for non Stand-Alone shared
1913 -- Also ignore the shared libraries which are :
1915 -- UNIX / Windows VMS
1916 -- -lgnat-<version> -lgnat_<version> (7 + version'length chars)
1917 -- -lgnarl-<version> -lgnarl_<version> (8 + version'length chars)
1919 if Next_Line (1 .. Nlast) /= "-static" and then
1920 Next_Line (1 .. Nlast) /= "-shared" and then
1921 Next_Line (1 .. Nlast) /= "-ldecgnat" and then
1922 Next_Line (1 .. Nlast) /= "-lgnarl" and then
1923 Next_Line (1 .. Nlast) /= "-lgnat" and then
1925 (1 .. Natural'Min (Nlast, 8 + Library_Version'Length)) /=
1926 Shared_Lib ("gnarl") and then
1928 (1 .. Natural'Min (Nlast, 7 + Library_Version'Length)) /=
1931 if Next_Line (1) /= '-' then
1933 -- This is not an option, should we add it?
1935 if Add_Object_Files then
1936 Opts.Increment_Last;
1937 Opts.Table (Opts.Last) :=
1938 new String'(Next_Line (1 .. Nlast));
1942 -- Add all other options
1944 Opts.Increment_Last;
1945 Opts.Table (Opts.Last) :=
1946 new String'(Next_Line (1 .. Nlast));
1950 -- Next option, if any
1953 exit when Next_Line (1 .. Nlast) = End_Info;
1955 -- Remove first eight characters " -- "
1957 Next_Line (1 .. Nlast - 8) := Next_Line (9 .. Nlast);
1962 Status := fclose (Fd);
1963 -- Is it really right to ignore any close error ???
1964 end Process_Binder_File;
1970 procedure Reset_Tables is
1973 Objects_Htable.Reset;
1977 Processed_Projects.Reset;