1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2003, 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 ------------------------------------------------------------------------------
29 with MLib
.Fil
; use MLib
.Fil
;
30 with MLib
.Tgt
; use MLib
.Tgt
;
31 with MLib
.Utl
; use MLib
.Utl
;
32 with Namet
; use Namet
;
34 with Output
; use Output
;
35 with Prj
.Com
; use Prj
.Com
;
36 with Prj
.Env
; use Prj
.Env
;
37 with Prj
.Util
; use Prj
.Util
;
39 with Snames
; use Snames
;
41 with Types
; use Types
;
43 with Ada
.Characters
.Handling
;
45 with GNAT
.Directory_Operations
; use GNAT
.Directory_Operations
;
47 with GNAT
.OS_Lib
; use GNAT
.OS_Lib
;
48 with Interfaces
.C_Streams
; use Interfaces
.C_Streams
;
49 with System
; use System
;
50 with System
.Case_Util
; use System
.Case_Util
;
52 package body MLib
.Prj
is
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 aq stand-alone
60 -- False for Windows, True for other platforms.
62 ALI_Suffix
: constant String := ".ali";
63 B_Start
: String := "b~";
65 S_Osinte_Ads
: Name_Id
:= No_Name
;
66 -- Name_Id for "s-osinte.ads"
68 S_Dec_Ads
: Name_Id
:= No_Name
;
69 -- Name_Id for "dec.ads"
71 No_Argument_List
: aliased String_List
:= (1 .. 0 => null);
72 No_Argument
: constant String_List_Access
:= No_Argument_List
'Access;
74 Arguments
: String_List_Access
:= No_Argument
;
75 -- Used to accumulate arguments for the invocation of gnatbind and of
76 -- the compiler. Also used to collect the interface ALI when copying
77 -- the ALI files to the library directory.
79 Argument_Number
: Natural := 0;
80 -- Index of the last argument in Arguments
82 Initial_Argument_Max
: constant := 10;
84 No_Main_String
: aliased String := "-n";
85 No_Main
: constant String_Access
:= No_Main_String
'Access;
87 Output_Switch_String
: aliased String := "-o";
88 Output_Switch
: constant String_Access
:= Output_Switch_String
'Access;
90 Compile_Switch_String
: aliased String := "-c";
91 Compile_Switch
: constant String_Access
:= Compile_Switch_String
'Access;
93 -- List of objects to put inside the library
95 Object_Files
: Argument_List_Access
;
97 package Objects
is new Table
.Table
98 (Table_Name
=> "Mlib.Prj.Objects",
99 Table_Component_Type
=> String_Access
,
100 Table_Index_Type
=> Natural,
101 Table_Low_Bound
=> 1,
103 Table_Increment
=> 100);
105 package Objects_Htable
is new GNAT
.HTable
.Simple_HTable
106 (Header_Num
=> Com
.Header_Num
,
113 -- List of non-Ada object files
115 Foreign_Objects
: Argument_List_Access
;
117 package Foreigns
is new Table
.Table
118 (Table_Name
=> "Mlib.Prj.Foreigns",
119 Table_Component_Type
=> String_Access
,
120 Table_Index_Type
=> Natural,
121 Table_Low_Bound
=> 1,
123 Table_Increment
=> 100);
127 Ali_Files
: Argument_List_Access
;
129 package ALIs
is new Table
.Table
130 (Table_Name
=> "Mlib.Prj.Alis",
131 Table_Component_Type
=> String_Access
,
132 Table_Index_Type
=> Natural,
133 Table_Low_Bound
=> 1,
135 Table_Increment
=> 100);
137 -- List of options set in the command line.
139 Options
: Argument_List_Access
;
141 package Opts
is new Table
.Table
142 (Table_Name
=> "Mlib.Prj.Opts",
143 Table_Component_Type
=> String_Access
,
144 Table_Index_Type
=> Natural,
145 Table_Low_Bound
=> 1,
147 Table_Increment
=> 100);
149 -- All the ALI file in the library
151 package Library_ALIs
is new GNAT
.HTable
.Simple_HTable
152 (Header_Num
=> Com
.Header_Num
,
159 -- The ALI files in the interface sets
161 package Interface_ALIs
is new GNAT
.HTable
.Simple_HTable
162 (Header_Num
=> Com
.Header_Num
,
169 -- The ALI files that have been processed to check if the corresponding
170 -- library unit is in the interface set.
172 package Processed_ALIs
is new GNAT
.HTable
.Simple_HTable
173 (Header_Num
=> Com
.Header_Num
,
180 -- The projects imported directly or indirectly.
182 package Processed_Projects
is new GNAT
.HTable
.Simple_HTable
183 (Header_Num
=> Com
.Header_Num
,
190 -- The library projects imported directly or indirectly.
192 package Library_Projs
is new Table
.Table
(
193 Table_Component_Type
=> Project_Id
,
194 Table_Index_Type
=> Integer,
195 Table_Low_Bound
=> 1,
197 Table_Increment
=> 10,
198 Table_Name
=> "Make.Library_Projs");
200 type Build_Mode_State
is (None
, Static
, Dynamic
, Relocatable
);
202 procedure Add_Argument
(S
: String);
203 -- Add one argument to the array Arguments.
204 -- If Arguments is full, double its size.
206 function ALI_File_Name
(Source
: String) return String;
207 -- Return the ALI file name corresponding to a source.
209 procedure Check
(Filename
: String);
210 -- Check if filename is a regular file. Fail if it is not.
212 procedure Check_Context
;
213 -- Check each object files in table Object_Files
214 -- Fail if any of them is not a regular file
216 procedure Clean
(Directory
: Name_Id
);
217 -- Attempt to delete all files in Directory, but not subdirectories
219 procedure Copy_Interface_Sources
220 (For_Project
: Project_Id
;
221 Interfaces
: Argument_List
;
223 -- Copy the interface sources of a SAL to directory To_Dir
225 procedure Display
(Executable
: String);
226 -- Display invocation of gnatbind and of the compiler with the arguments
227 -- in Arguments, except when Quiet_Output is True.
229 procedure Process_Binder_File
(Name
: String);
230 -- For Stand-Alone libraries, get the Linker Options in the binder
233 procedure Reset_Tables
;
234 -- Make sure that all the above tables are empty
235 -- (Objects, Foreign_Objects, Ali_Files, Options).
241 procedure Add_Argument
(S
: String) is
243 if Argument_Number
= Arguments
'Last then
245 New_Args
: constant String_List_Access
:=
246 new String_List
(1 .. 2 * Arguments
'Last);
249 -- Copy the String_Accesses and set them to null in Arguments
250 -- so that they will not be deallocated by the call to
253 New_Args
(Arguments
'Range) := Arguments
.all;
254 Arguments
.all := (others => null);
256 Arguments
:= New_Args
;
260 Argument_Number
:= Argument_Number
+ 1;
261 Arguments
(Argument_Number
) := new String'(S);
268 function ALI_File_Name (Source : String) return String is
270 -- If the source name has an extension, then replace it with
273 for Index in reverse Source'First + 1 .. Source'Last loop
274 if Source (Index) = '.' then
275 return Source (Source'First .. Index - 1) & ALI_Suffix;
279 -- If there is no dot, or if it is the first character, just add the
282 return Source & ALI_Suffix;
289 procedure Build_Library
290 (For_Project : Project_Id;
292 Gnatbind_Path : String_Access;
294 Gcc_Path : String_Access;
295 Bind : Boolean := True;
296 Link : Boolean := True)
298 Warning_For_Library : Boolean := False;
299 -- Set to True for the first warning about a unit missing from the
302 Libgnarl_Needed : Boolean := False;
303 -- Set to True if library needs to be linked with libgnarl
305 Libdecgnat_Needed : Boolean := False;
306 -- On OpenVMS, set to True if library needs to be linked with libdecgnat
308 Data : Project_Data := Projects.Table (For_Project);
310 Object_Directory_Path : constant String :=
311 Get_Name_String (Data.Object_Directory);
313 Standalone : constant Boolean := Data.Standalone_Library;
315 Project_Name : constant String := Get_Name_String (Data.Name);
317 DLL_Address : constant String_Access :=
318 new String'(Default_DLL_Address
);
320 Current_Dir
: constant String := Get_Current_Dir
;
322 Lib_Filename
: String_Access
;
323 Lib_Dirpath
: String_Access
;
324 Lib_Version
: String_Access
:= new String'("");
326 The_Build_Mode : Build_Mode_State := None;
328 Success : Boolean := False;
330 Library_Options : Variable_Value := Nil_Variable_Value;
332 Library_GCC : Variable_Value := Nil_Variable_Value;
334 Driver_Name : Name_Id := No_Name;
336 In_Main_Object_Directory : Boolean := True;
338 Rpath : String_Access := null;
339 -- Allocated only if Path Option is supported
341 Rpath_Last : Natural := 0;
342 -- Index of last valid character of Rpath
344 Initial_Rpath_Length : constant := 200;
345 -- Initial size of Rpath, when first allocated
347 Path_Option : String_Access := Linker_Library_Path_Option;
348 -- If null, Path Option is not supported.
349 -- Not a constant so that it can be deallocated.
352 -- Directory where to copy ALI files and possibly interface sources
354 procedure Add_ALI_For (Source : Name_Id);
355 -- Add the name of the ALI file corresponding to Source to the
358 procedure Add_Rpath (Path : String);
359 -- Add a path name to Rpath
361 function Check_Project (P : Project_Id) return Boolean;
362 -- Returns True if P is For_Project or a project extended by For_Project
364 procedure Check_Libs (ALI_File : String);
365 -- Set Libgnarl_Needed if the ALI_File indicates that there is a need
366 -- to link with -lgnarl (this is the case when there is a dependency
367 -- on s-osinte.ads). On OpenVMS, set Libdecgnat_Needed if the ALI file
368 -- indicates that there is a need to link with -ldecgnat (this is the
369 -- case when there is a dependency on dec.ads).
371 procedure Process (The_ALI : File_Name_Type);
372 -- Check if the closure of a library unit which is or should be in the
373 -- interface set is also in the interface set. Issue a warning for each
374 -- missing library unit.
376 procedure Process_Imported_Libraries;
377 -- Add the -L and -l switches for the imported Library Project Files,
378 -- and, if Path Option is supported, the library directory path names
385 procedure Add_ALI_For (Source : Name_Id) is
386 ALI : constant String := ALI_File_Name (Get_Name_String (Source));
390 -- Add the ALI file name to the library ALIs
393 Add_Str_To_Name_Buffer (S => ALI);
394 Library_ALIs.Set (Name_Find, True);
401 procedure Add_Rpath (Path : String) is
411 New_Rpath : constant String_Access :=
412 new String (1 .. 2 * Rpath'Length);
414 New_Rpath (1 .. Rpath_Last) := Rpath (1 .. Rpath_Last);
419 -- Start of processing for Add_Rpath
422 -- If firt path, allocate initial Rpath
425 Rpath := new String (1 .. Initial_Rpath_Length);
429 -- Otherwise, add a path separator between two path names
431 if Rpath_Last = Rpath'Last then
435 Rpath_Last := Rpath_Last + 1;
436 Rpath (Rpath_Last) := Path_Separator;
439 -- Increase Rpath size until it is large enough
441 while Rpath_Last + Path'Length > Rpath'Last loop
447 Rpath (Rpath_Last + 1 .. Rpath_Last + Path'Length) := Path;
448 Rpath_Last := Rpath_Last + Path'Length;
455 function Check_Project (P : Project_Id) return Boolean is
457 if P = For_Project then
460 elsif P /= No_Project then
462 Data : Project_Data := Projects.Table (For_Project);
465 while Data.Extends /= No_Project loop
466 if P = Data.Extends then
470 Data := Projects.Table (Data.Extends);
482 procedure Check_Libs (ALI_File : String) is
484 Text : Text_Buffer_Ptr;
487 pragma Warnings (Off, Id);
488 -- Comment needed ???
491 if not Libgnarl_Needed or
492 (Hostparm.OpenVMS and then (not Libdecgnat_Needed))
496 Name_Len := ALI_File'Length;
497 Name_Buffer (1 .. Name_Len) := ALI_File;
498 Lib_File := Name_Find;
499 Text := Read_Library_Info (Lib_File, True);
509 -- Look for s-osinte.ads in the dependencies
511 for Index in ALI.ALIs.Table (Id).First_Sdep ..
512 ALI.ALIs.Table (Id).Last_Sdep
514 if ALI.Sdep.Table (Index).Sfile = S_Osinte_Ads then
515 Libgnarl_Needed := True;
517 elsif Hostparm.OpenVMS and then
518 ALI.Sdep.Table (Index).Sfile = S_Dec_Ads
520 Libdecgnat_Needed := True;
530 procedure Process (The_ALI : File_Name_Type) is
531 Text : Text_Buffer_Ptr;
533 First_Unit : ALI.Unit_Id;
534 Last_Unit : ALI.Unit_Id;
535 Unit_Data : Unit_Record;
536 Afile : File_Name_Type;
539 -- Nothing to do if the ALI file has already been processed.
540 -- This happens if an interface imports another interface.
542 if not Processed_ALIs.Get (The_ALI) then
543 Processed_ALIs.Set (The_ALI, True);
544 Text := Read_Library_Info (The_ALI);
555 if Idread /= No_ALI_Id then
556 First_Unit := ALI.ALIs.Table (Idread).First_Unit;
557 Last_Unit := ALI.ALIs.Table (Idread).Last_Unit;
559 -- Process both unit (spec and body) if the body is needed
560 -- by the spec (inline or generic). Otherwise, just process
563 if First_Unit /= Last_Unit and then
564 not ALI.Units.Table (Last_Unit).Body_Needed_For_SAL
566 First_Unit := Last_Unit;
569 for Unit in First_Unit .. Last_Unit loop
570 Unit_Data := ALI.Units.Table (Unit);
572 -- Check if each withed unit which is in the library is
573 -- also in the interface set, if it has not yet been
576 for W in Unit_Data.First_With .. Unit_Data.Last_With loop
577 Afile := Withs.Table (W).Afile;
579 if Afile /= No_Name and then Library_ALIs.Get (Afile)
580 and then not Processed_ALIs.Get (Afile)
582 if not Interface_ALIs.Get (Afile) then
583 if not Warning_For_Library then
584 Write_Str ("Warning: In library project """);
585 Get_Name_String (Data.Name);
586 To_Mixed (Name_Buffer (1 .. Name_Len));
587 Write_Str (Name_Buffer (1 .. Name_Len));
589 Warning_For_Library := True;
592 Write_Str (" Unit """);
593 Get_Name_String (Withs.Table (W).Uname);
594 To_Mixed (Name_Buffer (1 .. Name_Len - 2));
595 Write_Str (Name_Buffer (1 .. Name_Len - 2));
596 Write_Line (""" is not in the interface set");
597 Write_Str (" but it is needed by ");
599 case Unit_Data.Utype is
601 Write_Str ("the spec of ");
604 Write_Str ("the body of ");
611 Get_Name_String (Unit_Data.Uname);
612 To_Mixed (Name_Buffer (1 .. Name_Len - 2));
613 Write_Str (Name_Buffer (1 .. Name_Len - 2));
617 -- Now, process this unit
628 --------------------------------
629 -- Process_Imported_Libraries --
630 --------------------------------
632 procedure Process_Imported_Libraries is
633 Current : Project_Id;
635 procedure Process_Project (Project : Project_Id);
636 -- Process Project and its imported projects recursively.
637 -- Add any library projects to table Library_Projs.
639 ---------------------
640 -- Process_Project --
641 ---------------------
643 procedure Process_Project (Project : Project_Id) is
644 Data : constant Project_Data := Projects.Table (Project);
645 Imported : Project_List := Data.Imported_Projects;
646 Element : Project_Element;
649 -- Nothing to do if process has already been processed.
651 if not Processed_Projects.Get (Data.Name) then
652 Processed_Projects.Set (Data.Name, True);
654 -- If it is a library project, add it to Library_Projs
656 if Project /= For_Project and then Data.Library then
657 Library_Projs.Increment_Last;
658 Library_Projs.Table (Library_Projs.Last) := Project;
661 -- Call Process_Project recursively for any imported project
663 while Imported /= Empty_Project_List loop
664 Element := Project_Lists.Table (Imported);
666 if Element.Project /= No_Project then
667 Process_Project (Element.Project);
670 Imported := Element.Next;
675 -- Start of processing for Process_Imported_Libraries
678 -- Build list of library projects imported directly or indirectly
680 Process_Project (For_Project);
682 -- If there are more that one library project file, make sure
683 -- that if libA depends on libB, libB is first in order.
685 if Library_Projs.Last > 1 then
687 Index : Integer := 1;
690 List : Project_List := Empty_Project_List;
693 Library_Loop : while Index < Library_Projs.Last loop
694 Proj1 := Library_Projs.Table (Index);
695 List := Projects.Table (Proj1).Imported_Projects;
697 List_Loop : while List /= Empty_Project_List loop
698 Proj2 := Project_Lists.Table (List).Project;
700 for J in Index + 1 .. Library_Projs.Last loop
701 if Proj2 = Library_Projs.Table (J) then
702 Library_Projs.Table (J) := Proj1;
703 Library_Projs.Table (Index) := Proj2;
708 List := Project_Lists.Table (List).Next;
711 if List = Empty_Project_List then
714 end loop Library_Loop;
718 -- Now that we have a correct order, add the -L and -l switches and,
719 -- if the Rpath option is supported, add the directory to the Rpath.
721 for Index in 1 .. Library_Projs.Last loop
722 Current := Library_Projs.Table (Index);
725 Opts.Table (Opts.Last) :=
729 (Projects
.Table
(Current
).Library_Dir
));
731 if Path_Option
/= null then
734 (Projects
.Table
(Current
).Library_Dir
));
738 Opts
.Table
(Opts
.Last
) :=
742 (Projects.Table (Current).Library_Name));
744 end Process_Imported_Libraries;
746 -- Start of processing for Build_Library
751 -- Fail if project is not a library project
753 if not Data.Library then
754 Com.Fail ("project """, Project_Name, """ has no library");
757 -- If this is the first time Build_Library is called, get the Name_Id
758 -- of "s-osinte.ads".
760 if S_Osinte_Ads = No_Name then
762 Name_Buffer (1 .. Name_Len) := "s-osinte.ads";
763 S_Osinte_Ads := Name_Find;
766 if S_Dec_Ads = No_Name then
768 Name_Buffer (1 .. Name_Len) := "dec.ads";
769 S_Dec_Ads := Name_Find;
772 -- We work in the object directory
774 Change_Dir (Object_Directory_Path);
777 -- Call gnatbind only if Bind is True
780 if Gnatbind_Path = null then
781 Com.Fail ("unable to locate ", Gnatbind);
784 if Gcc_Path = null then
785 Com.Fail ("unable to locate ", Gcc);
788 -- Allocate Arguments, if it is the first time we see a standalone
791 if Arguments = No_Argument then
792 Arguments := new String_List (1 .. Initial_Argument_Max);
795 -- Add "-n -o b~<lib>.adb (b$<lib>.adb on VMS) -L<lib>"
797 Argument_Number := 2;
798 Arguments (1) := No_Main;
799 Arguments (2) := Output_Switch;
801 if Hostparm.OpenVMS then
802 B_Start (B_Start'Last) := '$
';
806 (B_Start & Get_Name_String (Data.Library_Name) & ".adb");
807 Add_Argument ("-L" & Get_Name_String (Data.Library_Name));
809 -- Check if Binder'Default_Switches ("Ada) is defined. If it is,
810 -- add these switches to call gnatbind.
813 Binder_Package : constant Package_Id :=
815 (Name => Name_Binder,
816 In_Packages => Data.Decl.Packages);
819 if Binder_Package /= No_Package then
821 Defaults : constant Array_Element_Id :=
823 (Name => Name_Default_Switches,
826 (Binder_Package).Decl.Arrays);
827 Switches : Variable_Value := Nil_Variable_Value;
829 Switch : String_List_Id := Nil_String;
832 if Defaults /= No_Array_Element then
835 (Index => Name_Ada, In_Array => Defaults);
837 if not Switches.Default then
838 Switch := Switches.Values;
840 while Switch /= Nil_String loop
843 (String_Elements.Table (Switch).Value));
844 Switch := String_Elements.Table (Switch).Next;
852 -- Get all the ALI files of the project file
859 Interface_ALIs.Reset;
860 Processed_ALIs.Reset;
861 for Source in 1 .. Com.Units.Last loop
862 Unit := Com.Units.Table (Source);
864 if Unit.File_Names (Body_Part).Name /= No_Name
865 and then Unit.File_Names (Body_Part).Path /= Slash
868 Check_Project (Unit.File_Names (Body_Part).Project)
870 if Unit.File_Names (Specification).Name = No_Name then
872 Src_Ind : Source_File_Index;
875 Src_Ind := Sinput.P.Load_Project_File
880 -- Add the ALI file only if it is not a subunit
883 not Sinput.P.Source_File_Is_Subunit (Src_Ind)
886 (Unit.File_Names (Body_Part).Name);
891 Add_ALI_For (Unit.File_Names (Body_Part).Name);
895 elsif Unit.File_Names (Specification).Name /= No_Name
896 and then Unit.File_Names (Specification).Path /= Slash
897 and then Check_Project
898 (Unit.File_Names (Specification).Project)
900 Add_ALI_For (Unit.File_Names (Specification).Name);
908 (Project => For_Project, Including_Libraries => True);
910 -- Display the gnatbind command, if not in quiet output
917 (Gnatbind_Path.all, Arguments (1 .. Argument_Number), Success);
920 Com.Fail ("could not bind standalone library ",
921 Get_Name_String (Data.Library_Name));
926 -- Compile the binder generated file only if Link is true
932 (Project => For_Project, Including_Libraries => True);
934 -- Invoke <gcc> -c b$$<lib>.adb
936 -- Allocate Arguments, if it is the first time we see a standalone
939 if Arguments = No_Argument then
940 Arguments := new String_List (1 .. Initial_Argument_Max);
943 Argument_Number := 1;
944 Arguments (1) := Compile_Switch;
946 if Hostparm.OpenVMS then
947 B_Start (B_Start'Last) := '$
';
951 (B_Start & Get_Name_String (Data.Library_Name) & ".adb");
953 -- If necessary, add the PIC option
955 if PIC_Option /= "" then
956 Add_Argument (PIC_Option);
961 (Gcc_Path.all, Arguments (1 .. Argument_Number), Success);
965 ("could not compile binder generated file for library ",
966 Get_Name_String (Data.Library_Name));
969 -- Process binder generated file for pragmas Linker_Options
971 Process_Binder_File (Arguments (2).all & ASCII.NUL);
975 -- Build the library only if Link is True
978 -- If attribute Library_GCC was specified, get the driver name
980 Library_GCC := Value_Of (Name_Library_GCC, Data.Decl.Attributes);
982 if not Library_GCC.Default then
983 Driver_Name := Library_GCC.Value;
986 -- If attribute Library_Options was specified, add these additional
990 Value_Of (Name_Library_Options, Data.Decl.Attributes);
992 if not Library_Options.Default then
994 Current : String_List_Id := Library_Options.Values;
995 Element : String_Element;
998 while Current /= Nil_String loop
999 Element := String_Elements.Table (Current);
1000 Get_Name_String (Element.Value);
1002 if Name_Len /= 0 then
1003 Opts.Increment_Last;
1004 Opts.Table (Opts.Last) :=
1005 new String'(Name_Buffer
(1 .. Name_Len
));
1008 Current
:= Element
.Next
;
1013 Lib_Dirpath
:= new String'(Get_Name_String (Data.Library_Dir));
1014 Lib_Filename := new String'(Get_Name_String
(Data
.Library_Name
));
1016 case Data
.Library_Kind
is
1018 The_Build_Mode
:= Static
;
1021 The_Build_Mode
:= Dynamic
;
1024 The_Build_Mode
:= Relocatable
;
1026 if PIC_Option
/= "" then
1027 Opts
.Increment_Last
;
1028 Opts
.Table
(Opts
.Last
) := new String'(PIC_Option);
1032 -- Get the library version, if any
1034 if Data.Lib_Internal_Name /= No_Name then
1036 new String'(Get_Name_String
(Data
.Lib_Internal_Name
));
1039 -- Add the objects found in the object directory and the object
1040 -- directories of the extended files, if any, except for generated
1041 -- object files (b~.. or B$..) from extended projects.
1042 -- When there are one or more extended files, only add an object file
1043 -- if no object file with the same name have already been added.
1045 In_Main_Object_Directory
:= True;
1049 Object_Dir_Path
: constant String :=
1050 Get_Name_String
(Data
.Object_Directory
);
1051 Object_Dir
: Dir_Type
;
1052 Filename
: String (1 .. 255);
1057 Open
(Dir
=> Object_Dir
, Dir_Name
=> Object_Dir_Path
);
1059 -- For all entries in the object directory
1062 Read
(Object_Dir
, Filename
, Last
);
1066 -- Check if it is an object file
1068 if Is_Obj
(Filename
(1 .. Last
)) then
1070 Object_Path
: String :=
1072 (Object_Dir_Path
& Directory_Separator
&
1073 Filename
(1 .. Last
));
1076 Canonical_Case_File_Name
(Object_Path
);
1077 Canonical_Case_File_Name
(Filename
(1 .. Last
));
1079 -- If in the object directory of an extended project,
1080 -- do not consider generated object files.
1082 if In_Main_Object_Directory
or else
1084 Filename
(1 .. B_Start
'Length) /= B_Start
1087 Name_Buffer
(1 .. Name_Len
) := Filename
(1 .. Last
);
1090 if not Objects_Htable
.Get
(Id
) then
1092 -- Record this object file
1094 Objects_Htable
.Set
(Id
, True);
1095 Objects
.Increment_Last
;
1096 Objects
.Table
(Objects
.Last
) :=
1097 new String'(Object_Path);
1100 ALI_File : constant String :=
1101 Ext_To (Object_Path, "ali");
1104 if Is_Regular_File (ALI_File) then
1106 -- Record the ALI file
1108 ALIs.Increment_Last;
1109 ALIs.Table (ALIs.Last) :=
1110 new String'(ALI_File
);
1112 -- Find out if for this ALI file,
1113 -- libgnarl or libdecgnat (on OpenVMS)
1116 Check_Libs
(ALI_File
);
1119 -- The object file is a foreign object
1122 Foreigns
.Increment_Last
;
1123 Foreigns
.Table
(Foreigns
.Last
) :=
1124 new String'(Object_Path);
1133 Close (Dir => Object_Dir);
1136 when Directory_Error =>
1137 Com.Fail ("cannot find object directory """,
1138 Get_Name_String (Data.Object_Directory),
1142 exit when Data.Extends = No_Project;
1144 In_Main_Object_Directory := False;
1145 Data := Projects.Table (Data.Extends);
1148 -- Add the -L and -l switches for the imported Library Project Files,
1149 -- and, if Path Option is supported, the library directory path names
1152 Process_Imported_Libraries;
1154 -- Link with libgnat and possibly libgnarl
1156 Opts.Increment_Last;
1157 Opts.Table (Opts.Last) := new String'("-L" & Lib_Directory
);
1159 -- If Path Option is supported, add libgnat directory path name to
1162 if Path_Option
/= null then
1163 Add_Rpath
(Lib_Directory
);
1166 if Libgnarl_Needed
then
1167 Opts
.Increment_Last
;
1168 Opts
.Table
(Opts
.Last
) := new String'("-lgnarl");
1171 if Libdecgnat_Needed then
1172 Opts.Increment_Last;
1173 Opts.Table (Opts.Last) :=
1174 new String'("-L" & Lib_Directory
& "/../declib");
1175 Opts
.Increment_Last
;
1176 Opts
.Table
(Opts
.Last
) := new String'("-ldecgnat");
1179 Opts.Increment_Last;
1180 Opts.Table (Opts.Last) := new String'("-lgnat");
1182 -- If Path Option is supported, add the necessary switch with the
1183 -- content of Rpath. As Rpath contains at least libgnat directory
1184 -- path name, it is guaranteed that it is not null.
1186 if Path_Option
/= null then
1187 Opts
.Increment_Last
;
1188 Opts
.Table
(Opts
.Last
) :=
1189 new String'(Path_Option.all & Rpath (1 .. Rpath_Last));
1196 (Argument_List
(Objects
.Table
(1 .. Objects
.Last
)));
1199 new Argument_List
'(Argument_List
1200 (Foreigns.Table (1 .. Foreigns.Last)));
1203 new Argument_List'(Argument_List
(ALIs
.Table
(1 .. ALIs
.Last
)));
1206 new Argument_List
'(Argument_List (Opts.Table (1 .. Opts.Last)));
1208 -- We fail if there are no object to put in the library
1209 -- (Ada or foreign objects).
1211 if Object_Files'Length = 0 then
1212 Com.Fail ("no object files for library """ &
1213 Lib_Filename.all & '"');
1217 if not Opt.Quiet_Output then
1219 Write_Str ("building
");
1220 Write_Str (Ada.Characters.Handling.To_Lower
1221 (Build_Mode_State'Image (The_Build_Mode)));
1222 Write_Str (" library
for project
");
1223 Write_Line (Project_Name);
1227 Write_Line ("object files
:");
1229 for Index in Object_Files'Range loop
1231 Write_Line (Object_Files (Index).all);
1236 if Ali_Files'Length = 0 then
1237 Write_Line ("NO ALI files
");
1240 Write_Line ("ALI files
:");
1242 for Index in Ali_Files'Range loop
1244 Write_Line (Ali_Files (Index).all);
1251 -- We check that all object files are regular files
1255 -- Delete the existing library file, if it exists.
1256 -- Fail if the library file is not writable, or if it is not possible
1257 -- to delete the file.
1260 DLL_Name : aliased String :=
1261 Lib_Dirpath.all & "/lib
" &
1262 Lib_Filename.all & "." & DLL_Ext;
1264 Archive_Name : aliased String :=
1265 Lib_Dirpath.all & "/lib
" &
1266 Lib_Filename.all & "." & Archive_Ext;
1268 type Str_Ptr is access all String;
1269 -- This type is necessary to meet the accessibility rules of Ada.
1270 -- It is not possible to use String_Access here.
1272 Full_Lib_Name : Str_Ptr;
1273 -- Designates the full library path name. Either DLL_Name or
1274 -- Archive_Name, depending on the library kind.
1276 Success : Boolean := False;
1277 -- Used to call Delete_File
1280 if The_Build_Mode = Static then
1281 Full_Lib_Name := Archive_Name'Access;
1283 Full_Lib_Name := DLL_Name'Access;
1286 if Is_Regular_File (Full_Lib_Name.all) then
1287 if Is_Writable_File (Full_Lib_Name.all) then
1288 Delete_File (Full_Lib_Name.all, Success);
1291 if Is_Regular_File (Full_Lib_Name.all) then
1292 Com.Fail ("could
not delete
""" & Full_Lib_Name.all & """");
1297 Argument_Number := 0;
1299 -- If we have a standalone library, gather all the interface ALI.
1300 -- They are passed to Build_Dynamic_Library, where they are used by
1301 -- some platforms (VMS, for example) to decide what symbols should be
1302 -- exported. They are also flagged as Interface when we copy them to
1303 -- the library directory (by Copy_ALI_Files, below).
1306 Data := Projects.Table (For_Project);
1309 Interface : String_List_Id := Data.Lib_Interface_ALIs;
1310 ALI : File_Name_Type;
1313 while Interface /= Nil_String loop
1314 ALI := String_Elements.Table (Interface).Value;
1315 Interface_ALIs.Set (ALI, True);
1316 Get_Name_String (String_Elements.Table (Interface).Value);
1317 Add_Argument (Name_Buffer (1 .. Name_Len));
1318 Interface := String_Elements.Table (Interface).Next;
1321 Interface := Data.Lib_Interface_ALIs;
1323 if not Opt.Quiet_Output then
1325 -- Check that the interface set is complete: any unit in the
1326 -- library that is needed by an interface should also be an
1327 -- interface. If it is not the case, output a warning.
1329 while Interface /= Nil_String loop
1330 ALI := String_Elements.Table (Interface).Value;
1332 Interface := String_Elements.Table (Interface).Next;
1338 -- Clean the library directory, if it is also the directory where
1339 -- the ALI files are copied, either because there is no interface
1340 -- copy directory or because the interface copy directory is the
1341 -- same as the library directory.
1343 Copy_Dir := Projects.Table (For_Project).Library_Dir;
1346 -- Call the procedure to build the library, depending on the build
1349 case The_Build_Mode is
1350 when Dynamic | Relocatable =>
1351 Build_Dynamic_Library
1352 (Ofiles => Object_Files.all,
1353 Foreign => Foreign_Objects.all,
1354 Afiles => Ali_Files.all,
1355 Options => Options.all,
1356 Interfaces => Arguments (1 .. Argument_Number),
1357 Lib_Filename => Lib_Filename.all,
1358 Lib_Dir => Lib_Dirpath.all,
1359 Symbol_Data => Data.Symbol_Data,
1360 Driver_Name => Driver_Name,
1361 Lib_Address => DLL_Address.all,
1362 Lib_Version => Lib_Version.all,
1363 Relocatable => The_Build_Mode = Relocatable,
1364 Auto_Init => Data.Lib_Auto_Init);
1377 -- We need to copy the ALI files from the object directory
1378 -- to the library directory, so that the linker find them there,
1379 -- and does not need to look in the object directory where it would
1380 -- also find the object files; and we don't want that: we want the
1381 -- linker to use the library.
1383 -- Copy the ALI files and make the copies read-only. For interfaces,
1384 -- mark the copies as interfaces.
1387 (Files => Ali_Files.all,
1389 Interfaces => Arguments (1 .. Argument_Number));
1391 -- Copy interface sources if Library_Src_Dir specified
1394 and then Projects.Table (For_Project).Library_Src_Dir /= No_Name
1396 -- Clean the interface copy directory, if it is not also the
1397 -- library directory. If it is also the library directory, it has
1398 -- already been cleaned before the generation of the library.
1400 if Projects.Table (For_Project).Library_Src_Dir /= Copy_Dir then
1401 Copy_Dir := Projects.Table (For_Project).Library_Src_Dir;
1405 Copy_Interface_Sources
1406 (For_Project => For_Project,
1407 Interfaces => Arguments (1 .. Argument_Number),
1408 To_Dir => Copy_Dir);
1412 -- Reset the current working directory to its previous value
1414 Change_Dir (Current_Dir);
1421 procedure Check (Filename : String) is
1423 if not Is_Regular_File (Filename) then
1424 Com.Fail (Filename, " not found
.");
1432 procedure Check_Context is
1434 -- check that each object file exists
1436 for F in Object_Files'Range loop
1437 Check (Object_Files (F).all);
1445 procedure Check_Library (For_Project : Project_Id) is
1446 Data : constant Project_Data := Projects.Table (For_Project);
1449 if Data.Library and not Data.Flag1 then
1451 Current : constant Dir_Name_Str := Get_Current_Dir;
1452 Lib_Name : constant Name_Id := Library_File_Name_For (For_Project);
1453 Lib_TS : Time_Stamp_Type;
1454 Obj_TS : Time_Stamp_Type;
1456 Object_Dir : Dir_Type;
1459 if Hostparm.OpenVMS then
1460 B_Start (B_Start'Last) := '$';
1463 Change_Dir (Get_Name_String (Data.Library_Dir));
1465 Lib_TS := File_Stamp (Lib_Name);
1467 -- If the library file does not exist, then the time stamp will
1468 -- be Empty_Time_Stamp, earlier than any other time stamp.
1470 Change_Dir (Get_Name_String (Data.Object_Directory));
1471 Open (Dir => Object_Dir, Dir_Name => ".");
1473 -- For all entries in the object directory
1476 Read (Object_Dir, Name_Buffer, Name_Len);
1477 exit when Name_Len = 0;
1479 -- Check if it is an object file, but ignore any binder
1482 if Is_Obj (Name_Buffer (1 .. Name_Len))
1483 and then Name_Buffer (1 .. B_Start'Length) /= B_Start
1486 -- Get the object file time stamp
1488 Obj_TS := File_Stamp (Name_Find);
1490 -- If library file time stamp is earlier, set Flag1 and
1491 -- return. String comparaison is used, otherwise time stamps
1492 -- may be too close and the comparaison would return True,
1493 -- which would trigger an unnecessary rebuild of the
1496 if String (Lib_TS) < String (Obj_TS) then
1498 -- Library must be rebuilt
1500 Projects.Table (For_Project).Flag1 := True;
1506 Change_Dir (Current);
1515 procedure Clean (Directory : Name_Id) is
1516 Current : constant Dir_Name_Str := Get_Current_Dir;
1520 Name : String (1 .. 200);
1523 Disregard : Boolean;
1525 procedure Set_Writable (Name : System.Address);
1526 pragma Import (C, Set_Writable, "__gnat_set_writable
");
1529 Get_Name_String (Directory);
1531 -- Change the working directory to the directory to clean
1534 Change_Dir (Name_Buffer (1 .. Name_Len));
1539 ("unable to
access directory
""",
1540 Name_Buffer (1 .. Name_Len),
1546 -- For each regular file in the directory, make it writable and
1550 Read (Dir, Name, Last);
1553 if Is_Regular_File (Name (1 .. Last)) then
1554 Name (Last + 1) := ASCII.NUL;
1555 Set_Writable (Name (1)'Address);
1556 Delete_File (Name (1 .. Last), Disregard);
1562 -- Restore the initial working directory
1564 Change_Dir (Current);
1567 ----------------------------
1568 -- Copy_Interface_Sources --
1569 ----------------------------
1571 procedure Copy_Interface_Sources
1572 (For_Project : Project_Id;
1573 Interfaces : Argument_List;
1576 Current : constant Dir_Name_Str := Get_Current_Dir;
1577 Target : constant Dir_Name_Str := Get_Name_String (To_Dir);
1579 Text : Text_Buffer_Ptr;
1580 The_ALI : ALI.ALI_Id;
1583 First_Unit : ALI.Unit_Id;
1584 Second_Unit : ALI.Unit_Id;
1588 Copy_Subunits : Boolean := False;
1590 procedure Copy (File_Name : Name_Id);
1591 -- Copy one source of the project to the target directory
1597 procedure Copy (File_Name : Name_Id) is
1598 Success : Boolean := False;
1602 for Index in 1 .. Com.Units.Last loop
1603 Data := Com.Units.Table (Index);
1605 for J in Data.File_Names'Range loop
1606 if Data.File_Names (J).Project = For_Project
1607 and then Data.File_Names (J).Name = File_Name
1610 (Get_Name_String (Data.File_Names (J).Path),
1614 Preserve => Preserve);
1623 -- Start of processing for Copy_Interface_Sources
1626 -- Change the working directory to the object directory
1629 (Get_Name_String (Projects.Table (For_Project).Object_Directory));
1631 for Index in Interfaces'Range loop
1633 -- First, load the ALI file
1636 Add_Str_To_Name_Buffer (Interfaces (Index).all);
1637 Lib_File := Name_Find;
1638 Text := Read_Library_Info (Lib_File);
1639 The_ALI := Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
1642 Second_Unit := No_Unit_Id;
1643 First_Unit := ALI.ALIs.Table (The_ALI).First_Unit;
1644 Copy_Subunits := True;
1646 -- If there is both a spec and a body, check if they are both needed
1648 if ALI.Units.Table (First_Unit).Utype = Is_Body then
1649 Second_Unit := ALI.ALIs.Table (The_ALI).Last_Unit;
1651 -- If the body is not needed, then reset First_Unit
1653 if not ALI.Units.Table (Second_Unit).Body_Needed_For_SAL then
1654 First_Unit := No_Unit_Id;
1655 Copy_Subunits := False;
1658 elsif ALI.Units.Table (First_Unit).Utype = Is_Spec_Only then
1659 Copy_Subunits := False;
1662 -- Copy the file(s) that need to be copied
1664 if First_Unit /= No_Unit_Id then
1665 Copy (File_Name => ALI.Units.Table (First_Unit).Sfile);
1668 if Second_Unit /= No_Unit_Id then
1669 Copy (File_Name => ALI.Units.Table (Second_Unit).Sfile);
1672 -- Copy all the separates, if any
1674 if Copy_Subunits then
1675 for Dep in ALI.ALIs.Table (The_ALI).First_Sdep ..
1676 ALI.ALIs.Table (The_ALI).Last_Sdep
1678 if Sdep.Table (Dep).Subunit_Name /= No_Name then
1679 Copy (File_Name => Sdep.Table (Dep).Sfile);
1685 -- Restore the initial working directory
1687 Change_Dir (Current);
1688 end Copy_Interface_Sources;
1694 procedure Display (Executable : String) is
1696 if not Opt.Quiet_Output then
1697 Write_Str (Executable);
1699 for Index in 1 .. Argument_Number loop
1701 Write_Str (Arguments (Index).all);
1708 -------------------------
1709 -- Process_Binder_File --
1710 -------------------------
1712 procedure Process_Binder_File (Name : String) is
1714 -- Binder file's descriptor
1716 Read_Mode : constant String := "r
" & ASCII.Nul;
1719 Status : Interfaces.C_Streams.int;
1722 Begin_Info : String := "-- BEGIN Object file/option list";
1723 End_Info
: String := "-- END Object file/option list ";
1725 Next_Line
: String (1 .. 1000);
1726 -- Current line value
1727 -- Where does this odd constant 1000 come from, looks suspicious ???
1730 -- End of line slice (the slice does not contain the line terminator)
1732 procedure Get_Next_Line
;
1733 -- Read the next line from the binder file without the line terminator
1739 procedure Get_Next_Line
is
1743 Fchars
:= fgets
(Next_Line
'Address, Next_Line
'Length, Fd
);
1745 if Fchars
= System
.Null_Address
then
1746 Fail
("Error reading binder output");
1750 while Nlast
<= Next_Line
'Last
1751 and then Next_Line
(Nlast
) /= ASCII
.LF
1752 and then Next_Line
(Nlast
) /= ASCII
.CR
1760 -- Start of processing for Process_Binder_File
1763 Fd
:= fopen
(Name
'Address, Read_Mode
'Address);
1765 if Fd
= NULL_Stream
then
1766 Fail
("Failed to open binder output");
1769 -- Skip up to the Begin Info line
1773 exit when Next_Line
(1 .. Nlast
) = Begin_Info
;
1776 -- Find the first switch
1781 exit when Next_Line
(1 .. Nlast
) = End_Info
;
1783 -- As the binder generated file is in Ada, remove the first eight
1784 -- characters " -- ".
1786 Next_Line
(1 .. Nlast
- 8) := Next_Line
(9 .. Nlast
);
1789 -- Stop when the first switch is found
1791 exit when Next_Line
(1) = '-';
1794 if Next_Line
(1 .. Nlast
) /= End_Info
then
1796 -- Disregard -static and -shared, as -shared will be used
1799 -- Disregard -lgnat, -lgnarl and -ldecgnat as they will be added
1800 -- later, because they are also needed for non Stand-Alone shared
1803 if Next_Line
(1 .. Nlast
) /= "-static" and then
1804 Next_Line
(1 .. Nlast
) /= "-shared" and then
1805 Next_Line
(1 .. Nlast
) /= "-ldecgnat" and then
1806 Next_Line
(1 .. Nlast
) /= "-lgnarl" and then
1807 Next_Line
(1 .. Nlast
) /= "-lgnat"
1809 if Next_Line
(1) /= '-' then
1811 -- This is not an option, should we add it?
1813 if Add_Object_Files
then
1814 Opts
.Increment_Last
;
1815 Opts
.Table
(Opts
.Last
) :=
1816 new String'(Next_Line (1 .. Nlast));
1820 -- Add all other options
1822 Opts.Increment_Last;
1823 Opts.Table (Opts.Last) :=
1824 new String'(Next_Line
(1 .. Nlast
));
1828 -- Next option, if any
1831 exit when Next_Line
(1 .. Nlast
) = End_Info
;
1833 -- Remove first eight characters " -- "
1835 Next_Line
(1 .. Nlast
- 8) := Next_Line
(9 .. Nlast
);
1840 Status
:= fclose
(Fd
);
1841 end Process_Binder_File
;
1847 procedure Reset_Tables
is
1850 Objects_Htable
.Reset
;
1854 Processed_Projects
.Reset
;