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 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);
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 :=
828 (Index => Name_Ada, In_Array => Defaults);
829 Switch : String_List_Id := Nil_String;
831 if not Switches.Default then
832 Switch := Switches.Values;
834 while Switch /= Nil_String loop
837 (String_Elements.Table (Switch).Value));
838 Switch := String_Elements.Table (Switch).Next;
845 -- Get all the ALI files of the project file
852 Interface_ALIs.Reset;
853 Processed_ALIs.Reset;
854 for Source in 1 .. Com.Units.Last loop
855 Unit := Com.Units.Table (Source);
857 if Unit.File_Names (Body_Part).Name /= No_Name
858 and then Unit.File_Names (Body_Part).Path /= Slash
861 Check_Project (Unit.File_Names (Body_Part).Project)
863 if Unit.File_Names (Specification).Name = No_Name then
865 Src_Ind : Source_File_Index;
868 Src_Ind := Sinput.P.Load_Project_File
873 -- Add the ALI file only if it is not a subunit
876 not Sinput.P.Source_File_Is_Subunit (Src_Ind)
879 (Unit.File_Names (Body_Part).Name);
884 Add_ALI_For (Unit.File_Names (Body_Part).Name);
888 elsif Unit.File_Names (Specification).Name /= No_Name
889 and then Unit.File_Names (Specification).Path /= Slash
890 and then Check_Project
891 (Unit.File_Names (Specification).Project)
893 Add_ALI_For (Unit.File_Names (Specification).Name);
901 (Project => For_Project, Including_Libraries => True);
903 -- Display the gnatbind command, if not in quiet output
910 (Gnatbind_Path.all, Arguments (1 .. Argument_Number), Success);
913 Com.Fail ("could not bind standalone library ",
914 Get_Name_String (Data.Library_Name));
919 -- Compile the binder generated file only if Link is true
925 (Project => For_Project, Including_Libraries => True);
927 -- Invoke <gcc> -c b$$<lib>.adb
929 -- Allocate Arguments, if it is the first time we see a standalone
932 if Arguments = No_Argument then
933 Arguments := new String_List (1 .. Initial_Argument_Max);
936 Argument_Number := 1;
937 Arguments (1) := Compile_Switch;
939 if Hostparm.OpenVMS then
940 B_Start (B_Start'Last) := '$';
944 (B_Start & Get_Name_String (Data.Library_Name) & ".adb");
946 -- If necessary, add the PIC option
948 if PIC_Option /= "" then
949 Add_Argument (PIC_Option);
954 (Gcc_Path.all, Arguments (1 .. Argument_Number), Success);
958 ("could not compile binder generated file for library ",
959 Get_Name_String (Data.Library_Name));
962 -- Process binder generated file for pragmas Linker_Options
964 Process_Binder_File (Arguments (2).all & ASCII.NUL);
968 -- Build the library only if Link is True
971 -- If attribute Library_GCC was specified, get the driver name
973 Library_GCC := Value_Of (Name_Library_GCC, Data.Decl.Attributes);
975 if not Library_GCC.Default then
976 Driver_Name := Library_GCC.Value;
979 -- If attribute Library_Options was specified, add these additional
983 Value_Of (Name_Library_Options, Data.Decl.Attributes);
985 if not Library_Options.Default then
987 Current : String_List_Id := Library_Options.Values;
988 Element : String_Element;
991 while Current /= Nil_String loop
992 Element := String_Elements.Table (Current);
993 Get_Name_String (Element.Value);
995 if Name_Len /= 0 then
997 Opts.Table (Opts.Last) :=
998 new String'(Name_Buffer (1 .. Name_Len));
1001 Current := Element.Next;
1006 Lib_Dirpath := new String'(Get_Name_String (Data.Library_Dir));
1007 Lib_Filename := new String'(Get_Name_String (Data.Library_Name));
1009 case Data.Library_Kind is
1011 The_Build_Mode := Static;
1014 The_Build_Mode := Dynamic;
1017 The_Build_Mode := Relocatable;
1019 if PIC_Option /= "" then
1020 Opts.Increment_Last;
1021 Opts.Table (Opts.Last) := new String'(PIC_Option);
1025 -- Get the library version, if any
1027 if Data.Lib_Internal_Name /= No_Name then
1029 new String'(Get_Name_String (Data.Lib_Internal_Name));
1032 -- Add the objects found in the object directory and the object
1033 -- directories of the extended files, if any, except for generated
1034 -- object files (b~.. or B$..) from extended projects.
1035 -- When there are one or more extended files, only add an object file
1036 -- if no object file with the same name have already been added.
1038 In_Main_Object_Directory := True;
1042 Object_Dir_Path : constant String :=
1043 Get_Name_String (Data.Object_Directory);
1044 Object_Dir : Dir_Type;
1045 Filename : String (1 .. 255);
1050 Open (Dir => Object_Dir, Dir_Name => Object_Dir_Path);
1052 -- For all entries in the object directory
1055 Read (Object_Dir, Filename, Last);
1059 -- Check if it is an object file
1061 if Is_Obj (Filename (1 .. Last)) then
1063 Object_Path : String :=
1065 (Object_Dir_Path & Directory_Separator &
1066 Filename (1 .. Last));
1069 Canonical_Case_File_Name (Object_Path);
1070 Canonical_Case_File_Name (Filename (1 .. Last));
1072 -- If in the object directory of an extended project,
1073 -- do not consider generated object files.
1075 if In_Main_Object_Directory or else
1077 Filename (1 .. B_Start'Length) /= B_Start
1080 Name_Buffer (1 .. Name_Len) := Filename (1 .. Last);
1083 if not Objects_Htable.Get (Id) then
1085 -- Record this object file
1087 Objects_Htable.Set (Id, True);
1088 Objects.Increment_Last;
1089 Objects.Table (Objects.Last) :=
1090 new String'(Object_Path);
1093 ALI_File : constant String :=
1094 Ext_To (Object_Path, "ali");
1097 if Is_Regular_File (ALI_File) then
1099 -- Record the ALI file
1101 ALIs.Increment_Last;
1102 ALIs.Table (ALIs.Last) :=
1103 new String'(ALI_File);
1105 -- Find out if for this ALI file,
1106 -- libgnarl or libdecgnat (on OpenVMS)
1109 Check_Libs (ALI_File);
1112 -- The object file is a foreign object
1115 Foreigns.Increment_Last;
1116 Foreigns.Table (Foreigns.Last) :=
1117 new String'(Object_Path);
1126 Close (Dir => Object_Dir);
1129 when Directory_Error =>
1130 Com.Fail ("cannot find object directory """,
1131 Get_Name_String (Data.Object_Directory),
1135 exit when Data.Extends = No_Project;
1137 In_Main_Object_Directory := False;
1138 Data := Projects.Table (Data.Extends);
1141 -- Add the -L and -l switches for the imported Library Project Files,
1142 -- and, if Path Option is supported, the library directory path names
1145 Process_Imported_Libraries;
1147 -- Link with libgnat and possibly libgnarl
1149 Opts.Increment_Last;
1150 Opts.Table (Opts.Last) := new String'("-L" & Lib_Directory);
1152 -- If Path Option is supported, add libgnat directory path name to
1155 if Path_Option /= null then
1156 Add_Rpath (Lib_Directory);
1159 if Libgnarl_Needed then
1160 Opts.Increment_Last;
1161 Opts.Table (Opts.Last) := new String'("-lgnarl");
1164 if Libdecgnat_Needed then
1165 Opts.Increment_Last;
1166 Opts.Table (Opts.Last) :=
1167 new String'("-L" & Lib_Directory & "/../declib");
1168 Opts.Increment_Last;
1169 Opts.Table (Opts.Last) := new String'("-ldecgnat");
1172 Opts.Increment_Last;
1173 Opts.Table (Opts.Last) := new String'("-lgnat");
1175 -- If Path Option is supported, add the necessary switch with the
1176 -- content of Rpath. As Rpath contains at least libgnat directory
1177 -- path name, it is guaranteed that it is not null.
1179 if Path_Option /= null then
1180 Opts.Increment_Last;
1181 Opts.Table (Opts.Last) :=
1182 new String'(Path_Option.all & Rpath (1 .. Rpath_Last));
1189 (Argument_List (Objects.Table (1 .. Objects.Last)));
1192 new Argument_List'(Argument_List
1193 (Foreigns.Table (1 .. Foreigns.Last)));
1196 new Argument_List'(Argument_List (ALIs.Table (1 .. ALIs.Last)));
1199 new Argument_List'(Argument_List (Opts.Table (1 .. Opts.Last)));
1201 -- We fail if there are no object to put in the library
1202 -- (Ada or foreign objects).
1204 if Object_Files'Length = 0 then
1205 Com.Fail ("no object files for library """ &
1206 Lib_Filename.all & '"');
1210 if not Opt.Quiet_Output then
1212 Write_Str ("building ");
1213 Write_Str (Ada.Characters.Handling.To_Lower
1214 (Build_Mode_State'Image (The_Build_Mode)));
1215 Write_Str (" library for project ");
1216 Write_Line (Project_Name);
1220 Write_Line ("object files:");
1222 for Index in Object_Files'Range loop
1224 Write_Line (Object_Files (Index).all);
1229 if Ali_Files'Length = 0 then
1230 Write_Line ("NO ALI files");
1233 Write_Line ("ALI files:");
1235 for Index in Ali_Files'Range loop
1237 Write_Line (Ali_Files (Index).all);
1244 -- We check that all object files are regular files
1248 -- Delete the existing library file, if it exists.
1249 -- Fail if the library file is not writable, or if it is not possible
1250 -- to delete the file.
1253 DLL_Name : aliased String :=
1254 Lib_Dirpath.all & "/lib" &
1255 Lib_Filename.all & "." & DLL_Ext;
1257 Archive_Name : aliased String :=
1258 Lib_Dirpath.all & "/lib" &
1259 Lib_Filename.all & "." & Archive_Ext;
1261 type Str_Ptr is access all String;
1262 -- This type is necessary to meet the accessibility rules of Ada.
1263 -- It is not possible to use String_Access here.
1265 Full_Lib_Name : Str_Ptr;
1266 -- Designates the full library path name. Either DLL_Name or
1267 -- Archive_Name, depending on the library kind.
1269 Success : Boolean := False;
1270 -- Used to call Delete_File
1273 if The_Build_Mode = Static then
1274 Full_Lib_Name := Archive_Name'Access;
1276 Full_Lib_Name := DLL_Name'Access;
1279 if Is_Regular_File (Full_Lib_Name.all) then
1280 if Is_Writable_File (Full_Lib_Name.all) then
1281 Delete_File (Full_Lib_Name.all, Success);
1284 if Is_Regular_File (Full_Lib_Name.all) then
1285 Com.Fail ("could not delete """ & Full_Lib_Name.all & """");
1290 Argument_Number := 0;
1292 -- If we have a standalone library, gather all the interface ALI.
1293 -- They are passed to Build_Dynamic_Library, where they are used by
1294 -- some platforms (VMS, for example) to decide what symbols should be
1295 -- exported. They are also flagged as Interface when we copy them to
1296 -- the library directory (by Copy_ALI_Files, below).
1299 Data := Projects.Table (For_Project);
1302 Interface : String_List_Id := Data.Lib_Interface_ALIs;
1303 ALI : File_Name_Type;
1306 while Interface /= Nil_String loop
1307 ALI := String_Elements.Table (Interface).Value;
1308 Interface_ALIs.Set (ALI, True);
1309 Get_Name_String (String_Elements.Table (Interface).Value);
1310 Add_Argument (Name_Buffer (1 .. Name_Len));
1311 Interface := String_Elements.Table (Interface).Next;
1314 Interface := Data.Lib_Interface_ALIs;
1316 if not Opt.Quiet_Output then
1318 -- Check that the interface set is complete: any unit in the
1319 -- library that is needed by an interface should also be an
1320 -- interface. If it is not the case, output a warning.
1322 while Interface /= Nil_String loop
1323 ALI := String_Elements.Table (Interface).Value;
1325 Interface := String_Elements.Table (Interface).Next;
1331 -- Clean the library directory, if it is also the directory where
1332 -- the ALI files are copied, either because there is no interface
1333 -- copy directory or because the interface copy directory is the
1334 -- same as the library directory.
1336 Copy_Dir := Projects.Table (For_Project).Library_Dir;
1339 -- Call the procedure to build the library, depending on the build
1342 case The_Build_Mode is
1343 when Dynamic | Relocatable =>
1344 Build_Dynamic_Library
1345 (Ofiles => Object_Files.all,
1346 Foreign => Foreign_Objects.all,
1347 Afiles => Ali_Files.all,
1348 Options => Options.all,
1349 Interfaces => Arguments (1 .. Argument_Number),
1350 Lib_Filename => Lib_Filename.all,
1351 Lib_Dir => Lib_Dirpath.all,
1352 Symbol_Data => Data.Symbol_Data,
1353 Driver_Name => Driver_Name,
1354 Lib_Address => DLL_Address.all,
1355 Lib_Version => Lib_Version.all,
1356 Relocatable => The_Build_Mode = Relocatable,
1357 Auto_Init => Data.Lib_Auto_Init);
1370 -- We need to copy the ALI files from the object directory
1371 -- to the library directory, so that the linker find them there,
1372 -- and does not need to look in the object directory where it would
1373 -- also find the object files; and we don't want that: we want the
1374 -- linker to use the library.
1376 -- Copy the ALI files and make the copies read-only. For interfaces,
1377 -- mark the copies as interfaces.
1380 (Files => Ali_Files.all,
1382 Interfaces => Arguments (1 .. Argument_Number));
1384 -- Copy interface sources if Library_Src_Dir specified
1387 and then Projects.Table (For_Project).Library_Src_Dir /= No_Name
1389 -- Clean the interface copy directory, if it is not also the
1390 -- library directory. If it is also the library directory, it has
1391 -- already been cleaned before the generation of the library.
1393 if Projects.Table (For_Project).Library_Src_Dir /= Copy_Dir then
1394 Copy_Dir := Projects.Table (For_Project).Library_Src_Dir;
1398 Copy_Interface_Sources
1399 (For_Project => For_Project,
1400 Interfaces => Arguments (1 .. Argument_Number),
1401 To_Dir => Copy_Dir);
1405 -- Reset the current working directory to its previous value
1407 Change_Dir (Current_Dir);
1414 procedure Check (Filename : String) is
1416 if not Is_Regular_File (Filename) then
1417 Com.Fail (Filename, " not found.");
1425 procedure Check_Context is
1427 -- check that each object file exists
1429 for F in Object_Files'Range loop
1430 Check (Object_Files (F).all);
1438 procedure Check_Library (For_Project : Project_Id) is
1439 Data : constant Project_Data := Projects.Table (For_Project);
1442 if Data.Library and not Data.Flag1 then
1444 Current : constant Dir_Name_Str := Get_Current_Dir;
1445 Lib_Name : constant Name_Id := Library_File_Name_For (For_Project);
1446 Lib_TS : Time_Stamp_Type;
1447 Obj_TS : Time_Stamp_Type;
1449 Object_Dir : Dir_Type;
1452 if Hostparm.OpenVMS then
1453 B_Start (B_Start'Last) := '$';
1456 Change_Dir (Get_Name_String (Data.Library_Dir));
1458 Lib_TS := File_Stamp (Lib_Name);
1460 -- If the library file does not exist, then the time stamp will
1461 -- be Empty_Time_Stamp, earlier than any other time stamp.
1463 Change_Dir (Get_Name_String (Data.Object_Directory));
1464 Open (Dir => Object_Dir, Dir_Name => ".");
1466 -- For all entries in the object directory
1469 Read (Object_Dir, Name_Buffer, Name_Len);
1470 exit when Name_Len = 0;
1472 -- Check if it is an object file, but ignore any binder
1475 if Is_Obj (Name_Buffer (1 .. Name_Len))
1476 and then Name_Buffer (1 .. B_Start'Length) /= B_Start
1479 -- Get the object file time stamp
1481 Obj_TS := File_Stamp (Name_Find);
1483 -- If library file time stamp is earlier, set Flag1 and
1484 -- return. String comparaison is used, otherwise time stamps
1485 -- may be too close and the comparaison would return True,
1486 -- which would trigger an unnecessary rebuild of the
1489 if String (Lib_TS) < String (Obj_TS) then
1491 -- Library must be rebuilt
1493 Projects.Table (For_Project).Flag1 := True;
1499 Change_Dir (Current);
1508 procedure Clean (Directory : Name_Id) is
1509 Current : constant Dir_Name_Str := Get_Current_Dir;
1513 Name : String (1 .. 200);
1516 Disregard : Boolean;
1518 procedure Set_Writable (Name : System.Address);
1519 pragma Import (C, Set_Writable, "__gnat_set_writable");
1522 Get_Name_String (Directory);
1524 -- Change the working directory to the directory to clean
1527 Change_Dir (Name_Buffer (1 .. Name_Len));
1532 ("unable to access directory """,
1533 Name_Buffer (1 .. Name_Len),
1539 -- For each regular file in the directory, make it writable and
1543 Read (Dir, Name, Last);
1546 if Is_Regular_File (Name (1 .. Last)) then
1547 Name (Last + 1) := ASCII.NUL;
1548 Set_Writable (Name (1)'Address);
1549 Delete_File (Name (1 .. Last), Disregard);
1555 -- Restore the initial working directory
1557 Change_Dir (Current);
1560 ----------------------------
1561 -- Copy_Interface_Sources --
1562 ----------------------------
1564 procedure Copy_Interface_Sources
1565 (For_Project : Project_Id;
1566 Interfaces : Argument_List;
1569 Current : constant Dir_Name_Str := Get_Current_Dir;
1570 Target : constant Dir_Name_Str := Get_Name_String (To_Dir);
1572 Text : Text_Buffer_Ptr;
1573 The_ALI : ALI.ALI_Id;
1576 First_Unit : ALI.Unit_Id;
1577 Second_Unit : ALI.Unit_Id;
1581 Copy_Subunits : Boolean := False;
1583 procedure Copy (File_Name : Name_Id);
1584 -- Copy one source of the project to the target directory
1590 procedure Copy (File_Name : Name_Id) is
1591 Success : Boolean := False;
1595 for Index in 1 .. Com.Units.Last loop
1596 Data := Com.Units.Table (Index);
1598 for J in Data.File_Names'Range loop
1599 if Data.File_Names (J).Project = For_Project
1600 and then Data.File_Names (J).Name = File_Name
1603 (Get_Name_String (Data.File_Names (J).Path),
1607 Preserve => Preserve);
1616 -- Start of processing for Copy_Interface_Sources
1619 -- Change the working directory to the object directory
1622 (Get_Name_String (Projects.Table (For_Project).Object_Directory));
1624 for Index in Interfaces'Range loop
1626 -- First, load the ALI file
1629 Add_Str_To_Name_Buffer (Interfaces (Index).all);
1630 Lib_File := Name_Find;
1631 Text := Read_Library_Info (Lib_File);
1632 The_ALI := Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
1635 Second_Unit := No_Unit_Id;
1636 First_Unit := ALI.ALIs.Table (The_ALI).First_Unit;
1637 Copy_Subunits := True;
1639 -- If there is both a spec and a body, check if they are both needed
1641 if ALI.Units.Table (First_Unit).Utype = Is_Body then
1642 Second_Unit := ALI.ALIs.Table (The_ALI).Last_Unit;
1644 -- If the body is not needed, then reset First_Unit
1646 if not ALI.Units.Table (Second_Unit).Body_Needed_For_SAL then
1647 First_Unit := No_Unit_Id;
1648 Copy_Subunits := False;
1651 elsif ALI.Units.Table (First_Unit).Utype = Is_Spec_Only then
1652 Copy_Subunits := False;
1655 -- Copy the file(s) that need to be copied
1657 if First_Unit /= No_Unit_Id then
1658 Copy (File_Name => ALI.Units.Table (First_Unit).Sfile);
1661 if Second_Unit /= No_Unit_Id then
1662 Copy (File_Name => ALI.Units.Table (Second_Unit).Sfile);
1665 -- Copy all the separates, if any
1667 if Copy_Subunits then
1668 for Dep in ALI.ALIs.Table (The_ALI).First_Sdep ..
1669 ALI.ALIs.Table (The_ALI).Last_Sdep
1671 if Sdep.Table (Dep).Subunit_Name /= No_Name then
1672 Copy (File_Name => Sdep.Table (Dep).Sfile);
1678 -- Restore the initial working directory
1680 Change_Dir (Current);
1681 end Copy_Interface_Sources;
1687 procedure Display (Executable : String) is
1689 if not Opt.Quiet_Output then
1690 Write_Str (Executable);
1692 for Index in 1 .. Argument_Number loop
1694 Write_Str (Arguments (Index).all);
1701 -------------------------
1702 -- Process_Binder_File --
1703 -------------------------
1705 procedure Process_Binder_File (Name : String) is
1707 -- Binder file's descriptor
1709 Read_Mode : constant String := "r" & ASCII.Nul;
1712 Status : Interfaces.C_Streams.int;
1715 Begin_Info : String := "-- BEGIN Object file/option list";
1716 End_Info : String := "-- END Object file/option list ";
1718 Next_Line : String (1 .. 1000);
1719 -- Current line value
1720 -- Where does this odd constant 1000 come from, looks suspicious ???
1723 -- End of line slice (the slice does not contain the line terminator)
1725 procedure Get_Next_Line;
1726 -- Read the next line from the binder file without the line terminator
1732 procedure Get_Next_Line is
1736 Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd);
1738 if Fchars = System.Null_Address then
1739 Fail ("Error reading binder output");
1743 while Nlast <= Next_Line'Last
1744 and then Next_Line (Nlast) /= ASCII.LF
1745 and then Next_Line (Nlast) /= ASCII.CR
1753 -- Start of processing for Process_Binder_File
1756 Fd := fopen (Name'Address, Read_Mode'Address);
1758 if Fd = NULL_Stream then
1759 Fail ("Failed to open binder output");
1762 -- Skip up to the Begin Info line
1766 exit when Next_Line (1 .. Nlast) = Begin_Info;
1769 -- Find the first switch
1774 exit when Next_Line (1 .. Nlast) = End_Info;
1776 -- As the binder generated file is in Ada, remove the first eight
1777 -- characters " -- ".
1779 Next_Line (1 .. Nlast - 8) := Next_Line (9 .. Nlast);
1782 -- Stop when the first switch is found
1784 exit when Next_Line (1) = '-';
1787 if Next_Line (1 .. Nlast) /= End_Info then
1789 -- Disregard -static and -shared, as -shared will be used
1792 -- Disregard -lgnat, -lgnarl and -ldecgnat as they will be added
1793 -- later, because they are also needed for non Stand-Alone shared
1796 if Next_Line (1 .. Nlast) /= "-static" and then
1797 Next_Line (1 .. Nlast) /= "-shared" and then
1798 Next_Line (1 .. Nlast) /= "-ldecgnat" and then
1799 Next_Line (1 .. Nlast) /= "-lgnarl" and then
1800 Next_Line (1 .. Nlast) /= "-lgnat"
1802 if Next_Line (1) /= '-' then
1804 -- This is not an option, should we add it?
1806 if Add_Object_Files then
1807 Opts.Increment_Last;
1808 Opts.Table (Opts.Last) :=
1809 new String'(Next_Line (1 .. Nlast));
1813 -- Add all other options
1815 Opts.Increment_Last;
1816 Opts.Table (Opts.Last) :=
1817 new String'(Next_Line (1 .. Nlast));
1821 -- Next option, if any
1824 exit when Next_Line (1 .. Nlast) = End_Info;
1826 -- Remove first eight characters " -- "
1828 Next_Line (1 .. Nlast - 8) := Next_Line (9 .. Nlast);
1833 Status := fclose (Fd);
1834 end Process_Binder_File;
1840 procedure Reset_Tables is
1843 Objects_Htable.Reset;
1847 Processed_Projects.Reset;