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 -- Get all the ALI files of the project file
816 Interface_ALIs.Reset;
817 Processed_ALIs.Reset;
818 for Source in 1 .. Com.Units.Last loop
819 Unit := Com.Units.Table (Source);
821 if Unit.File_Names (Body_Part).Name /= No_Name
822 and then Unit.File_Names (Body_Part).Path /= Slash
825 Check_Project (Unit.File_Names (Body_Part).Project)
827 if Unit.File_Names (Specification).Name = No_Name then
829 Src_Ind : Source_File_Index;
832 Src_Ind := Sinput.P.Load_Project_File
837 -- Add the ALI file only if it is not a subunit
840 not Sinput.P.Source_File_Is_Subunit (Src_Ind)
843 (Unit.File_Names (Body_Part).Name);
848 Add_ALI_For (Unit.File_Names (Body_Part).Name);
852 elsif Unit.File_Names (Specification).Name /= No_Name
853 and then Unit.File_Names (Specification).Path /= Slash
854 and then Check_Project
855 (Unit.File_Names (Specification).Project)
857 Add_ALI_For (Unit.File_Names (Specification).Name);
865 (Project => For_Project, Including_Libraries => True);
867 -- Display the gnatbind command, if not in quiet output
874 (Gnatbind_Path.all, Arguments (1 .. Argument_Number), Success);
877 Com.Fail ("could not bind standalone library ",
878 Get_Name_String (Data.Library_Name));
883 -- Compile the binder generated file only if Link is true
889 (Project => For_Project, Including_Libraries => True);
891 -- Invoke <gcc> -c b$$<lib>.adb
893 -- Allocate Arguments, if it is the first time we see a standalone
896 if Arguments = No_Argument then
897 Arguments := new String_List (1 .. Initial_Argument_Max);
900 Argument_Number := 1;
901 Arguments (1) := Compile_Switch;
903 if Hostparm.OpenVMS then
904 B_Start (B_Start'Last) := '$';
908 (B_Start & Get_Name_String (Data.Library_Name) & ".adb");
910 -- If necessary, add the PIC option
912 if PIC_Option /= "" then
913 Add_Argument (PIC_Option);
918 (Gcc_Path.all, Arguments (1 .. Argument_Number), Success);
922 ("could not compile binder generated file for library ",
923 Get_Name_String (Data.Library_Name));
926 -- Process binder generated file for pragmas Linker_Options
928 Process_Binder_File (Arguments (2).all & ASCII.NUL);
932 -- Build the library only if Link is True
935 -- If attribute Library_GCC was specified, get the driver name
937 Library_GCC := Value_Of (Name_Library_GCC, Data.Decl.Attributes);
939 if not Library_GCC.Default then
940 Driver_Name := Library_GCC.Value;
943 -- If attribute Library_Options was specified, add these additional
947 Value_Of (Name_Library_Options, Data.Decl.Attributes);
949 if not Library_Options.Default then
951 Current : String_List_Id := Library_Options.Values;
952 Element : String_Element;
955 while Current /= Nil_String loop
956 Element := String_Elements.Table (Current);
957 Get_Name_String (Element.Value);
959 if Name_Len /= 0 then
961 Opts.Table (Opts.Last) :=
962 new String'(Name_Buffer (1 .. Name_Len));
965 Current := Element.Next;
970 Lib_Dirpath := new String'(Get_Name_String (Data.Library_Dir));
971 Lib_Filename := new String'(Get_Name_String (Data.Library_Name));
973 case Data.Library_Kind is
975 The_Build_Mode := Static;
978 The_Build_Mode := Dynamic;
981 The_Build_Mode := Relocatable;
983 if PIC_Option /= "" then
985 Opts.Table (Opts.Last) := new String'(PIC_Option);
989 -- Get the library version, if any
991 if Data.Lib_Internal_Name /= No_Name then
993 new String'(Get_Name_String (Data.Lib_Internal_Name));
996 -- Add the objects found in the object directory and the object
997 -- directories of the extended files, if any, except for generated
998 -- object files (b~.. or B$..) from extended projects.
999 -- When there are one or more extended files, only add an object file
1000 -- if no object file with the same name have already been added.
1002 In_Main_Object_Directory := True;
1006 Object_Dir_Path : constant String :=
1007 Get_Name_String (Data.Object_Directory);
1008 Object_Dir : Dir_Type;
1009 Filename : String (1 .. 255);
1014 Open (Dir => Object_Dir, Dir_Name => Object_Dir_Path);
1016 -- For all entries in the object directory
1019 Read (Object_Dir, Filename, Last);
1023 -- Check if it is an object file
1025 if Is_Obj (Filename (1 .. Last)) then
1027 Object_Path : String :=
1029 (Object_Dir_Path & Directory_Separator &
1030 Filename (1 .. Last));
1033 Canonical_Case_File_Name (Object_Path);
1034 Canonical_Case_File_Name (Filename (1 .. Last));
1036 -- If in the object directory of an extended project,
1037 -- do not consider generated object files.
1039 if In_Main_Object_Directory or else
1041 Filename (1 .. B_Start'Length) /= B_Start
1044 Name_Buffer (1 .. Name_Len) := Filename (1 .. Last);
1047 if not Objects_Htable.Get (Id) then
1049 -- Record this object file
1051 Objects_Htable.Set (Id, True);
1052 Objects.Increment_Last;
1053 Objects.Table (Objects.Last) :=
1054 new String'(Object_Path);
1057 ALI_File : constant String :=
1058 Ext_To (Object_Path, "ali");
1061 if Is_Regular_File (ALI_File) then
1063 -- Record the ALI file
1065 ALIs.Increment_Last;
1066 ALIs.Table (ALIs.Last) :=
1067 new String'(ALI_File);
1069 -- Find out if for this ALI file,
1070 -- libgnarl or libdecgnat (on OpenVMS)
1073 Check_Libs (ALI_File);
1076 -- The object file is a foreign object
1079 Foreigns.Increment_Last;
1080 Foreigns.Table (Foreigns.Last) :=
1081 new String'(Object_Path);
1090 Close (Dir => Object_Dir);
1093 when Directory_Error =>
1094 Com.Fail ("cannot find object directory """,
1095 Get_Name_String (Data.Object_Directory),
1099 exit when Data.Extends = No_Project;
1101 In_Main_Object_Directory := False;
1102 Data := Projects.Table (Data.Extends);
1105 -- Add the -L and -l switches for the imported Library Project Files,
1106 -- and, if Path Option is supported, the library directory path names
1109 Process_Imported_Libraries;
1111 -- Link with libgnat and possibly libgnarl
1113 Opts.Increment_Last;
1114 Opts.Table (Opts.Last) := new String'("-L" & Lib_Directory);
1116 -- If Path Option is supported, add libgnat directory path name to
1119 if Path_Option /= null then
1120 Add_Rpath (Lib_Directory);
1123 if Libgnarl_Needed then
1124 Opts.Increment_Last;
1125 Opts.Table (Opts.Last) := new String'("-lgnarl");
1128 if Libdecgnat_Needed then
1129 Opts.Increment_Last;
1130 Opts.Table (Opts.Last) :=
1131 new String'("-L" & Lib_Directory & "/../declib");
1132 Opts.Increment_Last;
1133 Opts.Table (Opts.Last) := new String'("-ldecgnat");
1136 Opts.Increment_Last;
1137 Opts.Table (Opts.Last) := new String'("-lgnat");
1139 -- If Path Option is supported, add the necessary switch with the
1140 -- content of Rpath. As Rpath contains at least libgnat directory
1141 -- path name, it is guaranteed that it is not null.
1143 if Path_Option /= null then
1144 Opts.Increment_Last;
1145 Opts.Table (Opts.Last) :=
1146 new String'(Path_Option.all & Rpath (1 .. Rpath_Last));
1153 (Argument_List (Objects.Table (1 .. Objects.Last)));
1156 new Argument_List'(Argument_List
1157 (Foreigns.Table (1 .. Foreigns.Last)));
1160 new Argument_List'(Argument_List (ALIs.Table (1 .. ALIs.Last)));
1163 new Argument_List'(Argument_List (Opts.Table (1 .. Opts.Last)));
1165 -- We fail if there are no object to put in the library
1166 -- (Ada or foreign objects).
1168 if Object_Files'Length = 0 then
1169 Com.Fail ("no object files for library """ &
1170 Lib_Filename.all & '"');
1174 if not Opt.Quiet_Output then
1176 Write_Str ("building ");
1177 Write_Str (Ada.Characters.Handling.To_Lower
1178 (Build_Mode_State'Image (The_Build_Mode)));
1179 Write_Str (" library for project ");
1180 Write_Line (Project_Name);
1184 Write_Line ("object files:");
1186 for Index in Object_Files'Range loop
1188 Write_Line (Object_Files (Index).all);
1193 if Ali_Files'Length = 0 then
1194 Write_Line ("NO ALI files");
1197 Write_Line ("ALI files:");
1199 for Index in Ali_Files'Range loop
1201 Write_Line (Ali_Files (Index).all);
1208 -- We check that all object files are regular files
1212 -- Delete the existing library file, if it exists.
1213 -- Fail if the library file is not writable, or if it is not possible
1214 -- to delete the file.
1217 DLL_Name : aliased String :=
1218 Lib_Dirpath.all & "/lib" &
1219 Lib_Filename.all & "." & DLL_Ext;
1221 Archive_Name : aliased String :=
1222 Lib_Dirpath.all & "/lib" &
1223 Lib_Filename.all & "." & Archive_Ext;
1225 type Str_Ptr is access all String;
1226 -- This type is necessary to meet the accessibility rules of Ada.
1227 -- It is not possible to use String_Access here.
1229 Full_Lib_Name : Str_Ptr;
1230 -- Designates the full library path name. Either DLL_Name or
1231 -- Archive_Name, depending on the library kind.
1233 Success : Boolean := False;
1234 -- Used to call Delete_File
1237 if The_Build_Mode = Static then
1238 Full_Lib_Name := Archive_Name'Access;
1240 Full_Lib_Name := DLL_Name'Access;
1243 if Is_Regular_File (Full_Lib_Name.all) then
1244 if Is_Writable_File (Full_Lib_Name.all) then
1245 Delete_File (Full_Lib_Name.all, Success);
1248 if Is_Regular_File (Full_Lib_Name.all) then
1249 Com.Fail ("could not delete """ & Full_Lib_Name.all & """");
1254 Argument_Number := 0;
1256 -- If we have a standalone library, gather all the interface ALI.
1257 -- They are passed to Build_Dynamic_Library, where they are used by
1258 -- some platforms (VMS, for example) to decide what symbols should be
1259 -- exported. They are also flagged as Interface when we copy them to
1260 -- the library directory (by Copy_ALI_Files, below).
1263 Data := Projects.Table (For_Project);
1266 Interface : String_List_Id := Data.Lib_Interface_ALIs;
1267 ALI : File_Name_Type;
1270 while Interface /= Nil_String loop
1271 ALI := String_Elements.Table (Interface).Value;
1272 Interface_ALIs.Set (ALI, True);
1273 Get_Name_String (String_Elements.Table (Interface).Value);
1274 Add_Argument (Name_Buffer (1 .. Name_Len));
1275 Interface := String_Elements.Table (Interface).Next;
1278 Interface := Data.Lib_Interface_ALIs;
1280 if not Opt.Quiet_Output then
1282 -- Check that the interface set is complete: any unit in the
1283 -- library that is needed by an interface should also be an
1284 -- interface. If it is not the case, output a warning.
1286 while Interface /= Nil_String loop
1287 ALI := String_Elements.Table (Interface).Value;
1289 Interface := String_Elements.Table (Interface).Next;
1295 -- Clean the library directory, if it is also the directory where
1296 -- the ALI files are copied, either because there is no interface
1297 -- copy directory or because the interface copy directory is the
1298 -- same as the library directory.
1300 Copy_Dir := Projects.Table (For_Project).Library_Dir;
1303 -- Call the procedure to build the library, depending on the build
1306 case The_Build_Mode is
1307 when Dynamic | Relocatable =>
1308 Build_Dynamic_Library
1309 (Ofiles => Object_Files.all,
1310 Foreign => Foreign_Objects.all,
1311 Afiles => Ali_Files.all,
1312 Options => Options.all,
1313 Interfaces => Arguments (1 .. Argument_Number),
1314 Lib_Filename => Lib_Filename.all,
1315 Lib_Dir => Lib_Dirpath.all,
1316 Driver_Name => Driver_Name,
1317 Lib_Address => DLL_Address.all,
1318 Lib_Version => Lib_Version.all,
1319 Relocatable => The_Build_Mode = Relocatable,
1320 Auto_Init => Data.Lib_Auto_Init);
1333 -- We need to copy the ALI files from the object directory
1334 -- to the library directory, so that the linker find them there,
1335 -- and does not need to look in the object directory where it would
1336 -- also find the object files; and we don't want that: we want the
1337 -- linker to use the library.
1339 -- Copy the ALI files and make the copies read-only. For interfaces,
1340 -- mark the copies as interfaces.
1343 (Files => Ali_Files.all,
1345 Interfaces => Arguments (1 .. Argument_Number));
1347 -- Copy interface sources if Library_Src_Dir specified
1350 and then Projects.Table (For_Project).Library_Src_Dir /= No_Name
1352 -- Clean the interface copy directory, if it is not also the
1353 -- library directory. If it is also the library directory, it has
1354 -- already been cleaned before the generation of the library.
1356 if Projects.Table (For_Project).Library_Src_Dir /= Copy_Dir then
1357 Copy_Dir := Projects.Table (For_Project).Library_Src_Dir;
1361 Copy_Interface_Sources
1362 (For_Project => For_Project,
1363 Interfaces => Arguments (1 .. Argument_Number),
1364 To_Dir => Copy_Dir);
1368 -- Reset the current working directory to its previous value
1370 Change_Dir (Current_Dir);
1377 procedure Check (Filename : String) is
1379 if not Is_Regular_File (Filename) then
1380 Com.Fail (Filename, " not found.");
1388 procedure Check_Context is
1390 -- check that each object file exists
1392 for F in Object_Files'Range loop
1393 Check (Object_Files (F).all);
1401 procedure Check_Library (For_Project : Project_Id) is
1402 Data : constant Project_Data := Projects.Table (For_Project);
1405 if Data.Library and not Data.Flag1 then
1407 Current : constant Dir_Name_Str := Get_Current_Dir;
1408 Lib_Name : constant Name_Id := Library_File_Name_For (For_Project);
1409 Lib_TS : Time_Stamp_Type;
1410 Obj_TS : Time_Stamp_Type;
1412 Object_Dir : Dir_Type;
1415 if Hostparm.OpenVMS then
1416 B_Start (B_Start'Last) := '$';
1419 Change_Dir (Get_Name_String (Data.Library_Dir));
1421 Lib_TS := File_Stamp (Lib_Name);
1423 -- If the library file does not exist, then the time stamp will
1424 -- be Empty_Time_Stamp, earlier than any other time stamp.
1426 Change_Dir (Get_Name_String (Data.Object_Directory));
1427 Open (Dir => Object_Dir, Dir_Name => ".");
1429 -- For all entries in the object directory
1432 Read (Object_Dir, Name_Buffer, Name_Len);
1433 exit when Name_Len = 0;
1435 -- Check if it is an object file, but ignore any binder
1438 if Is_Obj (Name_Buffer (1 .. Name_Len))
1439 and then Name_Buffer (1 .. B_Start'Length) /= B_Start
1442 -- Get the object file time stamp
1444 Obj_TS := File_Stamp (Name_Find);
1446 -- If library file time stamp is earlier, set Flag1 and
1447 -- return. String comparaison is used, otherwise time stamps
1448 -- may be too close and the comparaison would return True,
1449 -- which would trigger an unnecessary rebuild of the
1452 if String (Lib_TS) < String (Obj_TS) then
1454 -- Library must be rebuilt
1456 Projects.Table (For_Project).Flag1 := True;
1462 Change_Dir (Current);
1471 procedure Clean (Directory : Name_Id) is
1472 Current : constant Dir_Name_Str := Get_Current_Dir;
1476 Name : String (1 .. 200);
1479 Disregard : Boolean;
1481 procedure Set_Writable (Name : System.Address);
1482 pragma Import (C, Set_Writable, "__gnat_set_writable");
1485 Get_Name_String (Directory);
1487 -- Change the working directory to the directory to clean
1490 Change_Dir (Name_Buffer (1 .. Name_Len));
1495 ("unable to access directory """,
1496 Name_Buffer (1 .. Name_Len),
1502 -- For each regular file in the directory, make it writable and
1506 Read (Dir, Name, Last);
1509 if Is_Regular_File (Name (1 .. Last)) then
1510 Name (Last + 1) := ASCII.NUL;
1511 Set_Writable (Name (1)'Address);
1512 Delete_File (Name (1 .. Last), Disregard);
1518 -- Restore the initial working directory
1520 Change_Dir (Current);
1523 ----------------------------
1524 -- Copy_Interface_Sources --
1525 ----------------------------
1527 procedure Copy_Interface_Sources
1528 (For_Project : Project_Id;
1529 Interfaces : Argument_List;
1532 Current : constant Dir_Name_Str := Get_Current_Dir;
1533 Target : constant Dir_Name_Str := Get_Name_String (To_Dir);
1535 Text : Text_Buffer_Ptr;
1536 The_ALI : ALI.ALI_Id;
1539 First_Unit : ALI.Unit_Id;
1540 Second_Unit : ALI.Unit_Id;
1544 Copy_Subunits : Boolean := False;
1546 procedure Copy (File_Name : Name_Id);
1547 -- Copy one source of the project to the target directory
1553 procedure Copy (File_Name : Name_Id) is
1554 Success : Boolean := False;
1558 for Index in 1 .. Com.Units.Last loop
1559 Data := Com.Units.Table (Index);
1561 for J in Data.File_Names'Range loop
1562 if Data.File_Names (J).Project = For_Project
1563 and then Data.File_Names (J).Name = File_Name
1566 (Get_Name_String (Data.File_Names (J).Path),
1570 Preserve => Preserve);
1579 -- Start of processing for Copy_Interface_Sources
1582 -- Change the working directory to the object directory
1585 (Get_Name_String (Projects.Table (For_Project).Object_Directory));
1587 for Index in Interfaces'Range loop
1589 -- First, load the ALI file
1592 Add_Str_To_Name_Buffer (Interfaces (Index).all);
1593 Lib_File := Name_Find;
1594 Text := Read_Library_Info (Lib_File);
1595 The_ALI := Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
1598 Second_Unit := No_Unit_Id;
1599 First_Unit := ALI.ALIs.Table (The_ALI).First_Unit;
1600 Copy_Subunits := True;
1602 -- If there is both a spec and a body, check if they are both needed
1604 if ALI.Units.Table (First_Unit).Utype = Is_Body then
1605 Second_Unit := ALI.ALIs.Table (The_ALI).Last_Unit;
1607 -- If the body is not needed, then reset First_Unit
1609 if not ALI.Units.Table (Second_Unit).Body_Needed_For_SAL then
1610 First_Unit := No_Unit_Id;
1611 Copy_Subunits := False;
1614 elsif ALI.Units.Table (First_Unit).Utype = Is_Spec_Only then
1615 Copy_Subunits := False;
1618 -- Copy the file(s) that need to be copied
1620 if First_Unit /= No_Unit_Id then
1621 Copy (File_Name => ALI.Units.Table (First_Unit).Sfile);
1624 if Second_Unit /= No_Unit_Id then
1625 Copy (File_Name => ALI.Units.Table (Second_Unit).Sfile);
1628 -- Copy all the separates, if any
1630 if Copy_Subunits then
1631 for Dep in ALI.ALIs.Table (The_ALI).First_Sdep ..
1632 ALI.ALIs.Table (The_ALI).Last_Sdep
1634 if Sdep.Table (Dep).Subunit_Name /= No_Name then
1635 Copy (File_Name => Sdep.Table (Dep).Sfile);
1641 -- Restore the initial working directory
1643 Change_Dir (Current);
1644 end Copy_Interface_Sources;
1650 procedure Display (Executable : String) is
1652 if not Opt.Quiet_Output then
1653 Write_Str (Executable);
1655 for Index in 1 .. Argument_Number loop
1657 Write_Str (Arguments (Index).all);
1664 -------------------------
1665 -- Process_Binder_File --
1666 -------------------------
1668 procedure Process_Binder_File (Name : String) is
1670 -- Binder file's descriptor
1672 Read_Mode : constant String := "r" & ASCII.Nul;
1675 Status : Interfaces.C_Streams.int;
1678 Begin_Info : String := "-- BEGIN Object file/option list";
1679 End_Info : String := "-- END Object file/option list ";
1681 Next_Line : String (1 .. 1000);
1682 -- Current line value
1683 -- Where does this odd constant 1000 come from, looks suspicious ???
1686 -- End of line slice (the slice does not contain the line terminator)
1688 procedure Get_Next_Line;
1689 -- Read the next line from the binder file without the line terminator
1695 procedure Get_Next_Line is
1699 Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd);
1701 if Fchars = System.Null_Address then
1702 Fail ("Error reading binder output");
1706 while Nlast <= Next_Line'Last
1707 and then Next_Line (Nlast) /= ASCII.LF
1708 and then Next_Line (Nlast) /= ASCII.CR
1716 -- Start of processing for Process_Binder_File
1719 Fd := fopen (Name'Address, Read_Mode'Address);
1721 if Fd = NULL_Stream then
1722 Fail ("Failed to open binder output");
1725 -- Skip up to the Begin Info line
1729 exit when Next_Line (1 .. Nlast) = Begin_Info;
1732 -- Find the first switch
1737 exit when Next_Line (1 .. Nlast) = End_Info;
1739 -- As the binder generated file is in Ada, remove the first eight
1740 -- characters " -- ".
1742 Next_Line (1 .. Nlast - 8) := Next_Line (9 .. Nlast);
1745 -- Stop when the first switch is found
1747 exit when Next_Line (1) = '-';
1750 if Next_Line (1 .. Nlast) /= End_Info then
1752 -- Disregard -static and -shared, as -shared will be used
1755 -- Disregard -lgnat, -lgnarl and -ldecgnat as they will be added
1756 -- later, because they are also needed for non Stand-Alone shared
1759 if Next_Line (1 .. Nlast) /= "-static" and then
1760 Next_Line (1 .. Nlast) /= "-shared" and then
1761 Next_Line (1 .. Nlast) /= "-ldecgnat" and then
1762 Next_Line (1 .. Nlast) /= "-lgnarl" and then
1763 Next_Line (1 .. Nlast) /= "-lgnat"
1765 if Next_Line (1) /= '-' then
1767 -- This is not an option, should we add it?
1769 if Add_Object_Files then
1770 Opts.Increment_Last;
1771 Opts.Table (Opts.Last) :=
1772 new String'(Next_Line (1 .. Nlast));
1776 -- Add all other options
1778 Opts.Increment_Last;
1779 Opts.Table (Opts.Last) :=
1780 new String'(Next_Line (1 .. Nlast));
1784 -- Next option, if any
1787 exit when Next_Line (1 .. Nlast) = End_Info;
1789 -- Remove first eight characters " -- "
1791 Next_Line (1 .. Nlast - 8) := Next_Line (9 .. Nlast);
1796 Status := fclose (Fd);
1797 end Process_Binder_File;
1803 procedure Reset_Tables is
1806 Objects_Htable.Reset;
1810 Processed_Projects.Reset;