1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2001-2004, Ada Core Technologies, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 2, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING. If not, write --
19 -- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
20 -- MA 02111-1307, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
28 with Gnatvsn; use Gnatvsn;
30 with MLib.Fil; use MLib.Fil;
31 with MLib.Tgt; use MLib.Tgt;
32 with MLib.Utl; use MLib.Utl;
33 with Namet; use Namet;
35 with Osint; use Osint;
36 with Output; use Output;
37 with Prj.Com; use Prj.Com;
38 with Prj.Env; use Prj.Env;
39 with Prj.Util; use Prj.Util;
41 with Snames; use Snames;
42 with Switch; use Switch;
44 with Types; use Types;
46 with Ada.Characters.Handling;
48 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
50 with GNAT.OS_Lib; use GNAT.OS_Lib;
51 with Interfaces.C_Streams; use Interfaces.C_Streams;
52 with System; use System;
53 with System.Case_Util; use System.Case_Util;
55 package body MLib.Prj is
57 Prj_Add_Obj_Files : Types.Int;
58 pragma Import (C, Prj_Add_Obj_Files, "__gnat_prj_add_obj_files");
59 Add_Object_Files : constant Boolean := Prj_Add_Obj_Files /= 0;
60 -- Indicates if object files in pragmas Linker_Options (found in the
61 -- binder generated file) should be taken when linking aq stand-alone
63 -- False for Windows, True for other platforms.
65 ALI_Suffix : constant String := ".ali";
66 B_Start : String := "b~";
68 S_Osinte_Ads : Name_Id := No_Name;
69 -- Name_Id for "s-osinte.ads"
71 S_Dec_Ads : Name_Id := No_Name;
72 -- Name_Id for "dec.ads"
74 No_Argument_List : aliased String_List := (1 .. 0 => null);
75 No_Argument : constant String_List_Access := No_Argument_List'Access;
77 Arguments : String_List_Access := No_Argument;
78 -- Used to accumulate arguments for the invocation of gnatbind and of
79 -- the compiler. Also used to collect the interface ALI when copying
80 -- the ALI files to the library directory.
82 Argument_Number : Natural := 0;
83 -- Index of the last argument in Arguments
85 Initial_Argument_Max : constant := 10;
87 No_Main_String : aliased String := "-n";
88 No_Main : constant String_Access := No_Main_String'Access;
90 Output_Switch_String : aliased String := "-o";
91 Output_Switch : constant String_Access := Output_Switch_String'Access;
93 Compile_Switch_String : aliased String := "-c";
94 Compile_Switch : constant String_Access := Compile_Switch_String'Access;
96 -- List of objects to put inside the library
98 Object_Files : Argument_List_Access;
100 package Objects is new Table.Table
101 (Table_Name => "Mlib.Prj.Objects",
102 Table_Component_Type => String_Access,
103 Table_Index_Type => Natural,
104 Table_Low_Bound => 1,
106 Table_Increment => 100);
108 package Objects_Htable is new GNAT.HTable.Simple_HTable
109 (Header_Num => Com.Header_Num,
116 -- List of non-Ada object files
118 Foreign_Objects : Argument_List_Access;
120 package Foreigns is new Table.Table
121 (Table_Name => "Mlib.Prj.Foreigns",
122 Table_Component_Type => String_Access,
123 Table_Index_Type => Natural,
124 Table_Low_Bound => 1,
126 Table_Increment => 100);
130 Ali_Files : Argument_List_Access;
132 package ALIs is new Table.Table
133 (Table_Name => "Mlib.Prj.Alis",
134 Table_Component_Type => String_Access,
135 Table_Index_Type => Natural,
136 Table_Low_Bound => 1,
138 Table_Increment => 100);
140 -- List of options set in the command line.
142 Options : Argument_List_Access;
144 package Opts is new Table.Table
145 (Table_Name => "Mlib.Prj.Opts",
146 Table_Component_Type => String_Access,
147 Table_Index_Type => Natural,
148 Table_Low_Bound => 1,
150 Table_Increment => 100);
152 -- All the ALI file in the library
154 package Library_ALIs is new GNAT.HTable.Simple_HTable
155 (Header_Num => Com.Header_Num,
162 -- The ALI files in the interface sets
164 package Interface_ALIs is new GNAT.HTable.Simple_HTable
165 (Header_Num => Com.Header_Num,
172 -- The ALI files that have been processed to check if the corresponding
173 -- library unit is in the interface set.
175 package Processed_ALIs is new GNAT.HTable.Simple_HTable
176 (Header_Num => Com.Header_Num,
183 -- The projects imported directly or indirectly.
185 package Processed_Projects is new GNAT.HTable.Simple_HTable
186 (Header_Num => Com.Header_Num,
193 -- The library projects imported directly or indirectly.
195 package Library_Projs is new Table.Table (
196 Table_Component_Type => Project_Id,
197 Table_Index_Type => Integer,
198 Table_Low_Bound => 1,
200 Table_Increment => 10,
201 Table_Name => "Make.Library_Projs");
203 type Build_Mode_State is (None, Static, Dynamic, Relocatable);
205 procedure Add_Argument (S : String);
206 -- Add one argument to the array Arguments.
207 -- If Arguments is full, double its size.
209 function ALI_File_Name (Source : String) return String;
210 -- Return the ALI file name corresponding to a source.
212 procedure Check (Filename : String);
213 -- Check if filename is a regular file. Fail if it is not.
215 procedure Check_Context;
216 -- Check each object files in table Object_Files
217 -- Fail if any of them is not a regular file
219 procedure Clean (Directory : Name_Id);
220 -- Attempt to delete all files in Directory, but not subdirectories
222 procedure Copy_Interface_Sources
223 (For_Project : Project_Id;
224 Interfaces : Argument_List;
226 -- Copy the interface sources of a SAL to directory To_Dir
228 procedure Display (Executable : String);
229 -- Display invocation of gnatbind and of the compiler with the arguments
230 -- in Arguments, except when Quiet_Output is True.
232 procedure Process_Binder_File (Name : String);
233 -- For Stand-Alone libraries, get the Linker Options in the binder
236 procedure Reset_Tables;
237 -- Make sure that all the above tables are empty
238 -- (Objects, Foreign_Objects, Ali_Files, Options).
244 procedure Add_Argument (S : String) is
246 if Argument_Number = Arguments'Last then
248 New_Args : constant String_List_Access :=
249 new String_List (1 .. 2 * Arguments'Last);
252 -- Copy the String_Accesses and set them to null in Arguments
253 -- so that they will not be deallocated by the call to
256 New_Args (Arguments'Range) := Arguments.all;
257 Arguments.all := (others => null);
259 Arguments := New_Args;
263 Argument_Number := Argument_Number + 1;
264 Arguments (Argument_Number) := new String'(S);
271 function ALI_File_Name (Source : String) return String is
273 -- If the source name has an extension, then replace it with
276 for Index in reverse Source'First + 1 .. Source'Last loop
277 if Source (Index) = '.' then
278 return Source (Source'First .. Index - 1) & ALI_Suffix;
282 -- If there is no dot, or if it is the first character, just add the
285 return Source & ALI_Suffix;
292 procedure Build_Library
293 (For_Project : Project_Id;
295 Gnatbind_Path : String_Access;
297 Gcc_Path : String_Access;
298 Bind : Boolean := True;
299 Link : Boolean := True)
301 Warning_For_Library : Boolean := False;
302 -- Set to True for the first warning about a unit missing from the
305 Libgnarl_Needed : Boolean := False;
306 -- Set to True if library needs to be linked with libgnarl
308 Libdecgnat_Needed : Boolean := False;
309 -- On OpenVMS, set to True if library needs to be linked with libdecgnat
311 Data : Project_Data := Projects.Table (For_Project);
313 Object_Directory_Path : constant String :=
314 Get_Name_String (Data.Object_Directory);
316 Standalone : constant Boolean := Data.Standalone_Library;
318 Project_Name : constant String := Get_Name_String (Data.Name);
320 DLL_Address : constant String_Access :=
321 new String'(Default_DLL_Address);
323 Current_Dir : constant String := Get_Current_Dir;
325 Lib_Filename : String_Access;
326 Lib_Dirpath : String_Access;
327 Lib_Version : String_Access := new String'("");
329 The_Build_Mode : Build_Mode_State := None;
331 Success : Boolean := False;
333 Library_Options : Variable_Value := Nil_Variable_Value;
335 Library_GCC : Variable_Value := Nil_Variable_Value;
337 Driver_Name : Name_Id := No_Name;
339 In_Main_Object_Directory : Boolean := True;
341 Rpath : String_Access := null;
342 -- Allocated only if Path Option is supported
344 Rpath_Last : Natural := 0;
345 -- Index of last valid character of Rpath
347 Initial_Rpath_Length : constant := 200;
348 -- Initial size of Rpath, when first allocated
350 Path_Option : String_Access := Linker_Library_Path_Option;
351 -- If null, Path Option is not supported.
352 -- Not a constant so that it can be deallocated.
355 -- Directory where to copy ALI files and possibly interface sources
357 First_ALI : Name_Id := No_Name;
358 -- Store the ALI file name of a source of the library (the first found)
360 procedure Add_ALI_For (Source : Name_Id);
361 -- Add the name of the ALI file corresponding to Source to the
364 procedure Add_Rpath (Path : String);
365 -- Add a path name to Rpath
367 function Check_Project (P : Project_Id) return Boolean;
368 -- Returns True if P is For_Project or a project extended by For_Project
370 procedure Check_Libs (ALI_File : String);
371 -- Set Libgnarl_Needed if the ALI_File indicates that there is a need
372 -- to link with -lgnarl (this is the case when there is a dependency
373 -- on s-osinte.ads). On OpenVMS, set Libdecgnat_Needed if the ALI file
374 -- indicates that there is a need to link with -ldecgnat (this is the
375 -- case when there is a dependency on dec.ads).
377 procedure Process (The_ALI : File_Name_Type);
378 -- Check if the closure of a library unit which is or should be in the
379 -- interface set is also in the interface set. Issue a warning for each
380 -- missing library unit.
382 procedure Process_Imported_Libraries;
383 -- Add the -L and -l switches for the imported Library Project Files,
384 -- and, if Path Option is supported, the library directory path names
391 procedure Add_ALI_For (Source : Name_Id) is
392 ALI : constant String := ALI_File_Name (Get_Name_String (Source));
401 Add_Str_To_Name_Buffer (S => ALI);
404 -- Add the ALI file name to the library ALIs
407 Library_ALIs.Set (ALI_Id, True);
410 -- Set First_ALI, if not already done
412 if First_ALI = No_Name then
421 procedure Add_Rpath (Path : String) is
431 New_Rpath : constant String_Access :=
432 new String (1 .. 2 * Rpath'Length);
434 New_Rpath (1 .. Rpath_Last) := Rpath (1 .. Rpath_Last);
439 -- Start of processing for Add_Rpath
442 -- If firt path, allocate initial Rpath
445 Rpath := new String (1 .. Initial_Rpath_Length);
449 -- Otherwise, add a path separator between two path names
451 if Rpath_Last = Rpath'Last then
455 Rpath_Last := Rpath_Last + 1;
456 Rpath (Rpath_Last) := Path_Separator;
459 -- Increase Rpath size until it is large enough
461 while Rpath_Last + Path'Length > Rpath'Last loop
467 Rpath (Rpath_Last + 1 .. Rpath_Last + Path'Length) := Path;
468 Rpath_Last := Rpath_Last + Path'Length;
475 function Check_Project (P : Project_Id) return Boolean is
477 if P = For_Project then
480 elsif P /= No_Project then
482 Data : Project_Data := Projects.Table (For_Project);
485 while Data.Extends /= No_Project loop
486 if P = Data.Extends then
490 Data := Projects.Table (Data.Extends);
502 procedure Check_Libs (ALI_File : String) is
504 Text : Text_Buffer_Ptr;
508 if not Libgnarl_Needed or
509 (Hostparm.OpenVMS and then (not Libdecgnat_Needed))
513 Name_Len := ALI_File'Length;
514 Name_Buffer (1 .. Name_Len) := ALI_File;
515 Lib_File := Name_Find;
516 Text := Read_Library_Info (Lib_File, True);
526 -- Look for s-osinte.ads in the dependencies
528 for Index in ALI.ALIs.Table (Id).First_Sdep ..
529 ALI.ALIs.Table (Id).Last_Sdep
531 if ALI.Sdep.Table (Index).Sfile = S_Osinte_Ads then
532 Libgnarl_Needed := True;
534 elsif Hostparm.OpenVMS and then
535 ALI.Sdep.Table (Index).Sfile = S_Dec_Ads
537 Libdecgnat_Needed := True;
547 procedure Process (The_ALI : File_Name_Type) is
548 Text : Text_Buffer_Ptr;
550 First_Unit : ALI.Unit_Id;
551 Last_Unit : ALI.Unit_Id;
552 Unit_Data : Unit_Record;
553 Afile : File_Name_Type;
556 -- Nothing to do if the ALI file has already been processed.
557 -- This happens if an interface imports another interface.
559 if not Processed_ALIs.Get (The_ALI) then
560 Processed_ALIs.Set (The_ALI, True);
561 Text := Read_Library_Info (The_ALI);
572 if Idread /= No_ALI_Id then
573 First_Unit := ALI.ALIs.Table (Idread).First_Unit;
574 Last_Unit := ALI.ALIs.Table (Idread).Last_Unit;
576 -- Process both unit (spec and body) if the body is needed
577 -- by the spec (inline or generic). Otherwise, just process
580 if First_Unit /= Last_Unit and then
581 not ALI.Units.Table (Last_Unit).Body_Needed_For_SAL
583 First_Unit := Last_Unit;
586 for Unit in First_Unit .. Last_Unit loop
587 Unit_Data := ALI.Units.Table (Unit);
589 -- Check if each withed unit which is in the library is
590 -- also in the interface set, if it has not yet been
593 for W in Unit_Data.First_With .. Unit_Data.Last_With loop
594 Afile := Withs.Table (W).Afile;
596 if Afile /= No_Name and then Library_ALIs.Get (Afile)
597 and then not Processed_ALIs.Get (Afile)
599 if not Interface_ALIs.Get (Afile) then
600 if not Warning_For_Library then
601 Write_Str ("Warning: In library project """);
602 Get_Name_String (Data.Name);
603 To_Mixed (Name_Buffer (1 .. Name_Len));
604 Write_Str (Name_Buffer (1 .. Name_Len));
606 Warning_For_Library := True;
609 Write_Str (" Unit """);
610 Get_Name_String (Withs.Table (W).Uname);
611 To_Mixed (Name_Buffer (1 .. Name_Len - 2));
612 Write_Str (Name_Buffer (1 .. Name_Len - 2));
613 Write_Line (""" is not in the interface set");
614 Write_Str (" but it is needed by ");
616 case Unit_Data.Utype is
618 Write_Str ("the spec of ");
621 Write_Str ("the body of ");
628 Get_Name_String (Unit_Data.Uname);
629 To_Mixed (Name_Buffer (1 .. Name_Len - 2));
630 Write_Str (Name_Buffer (1 .. Name_Len - 2));
634 -- Now, process this unit
645 --------------------------------
646 -- Process_Imported_Libraries --
647 --------------------------------
649 procedure Process_Imported_Libraries is
650 Current : Project_Id;
652 procedure Process_Project (Project : Project_Id);
653 -- Process Project and its imported projects recursively.
654 -- Add any library projects to table Library_Projs.
656 ---------------------
657 -- Process_Project --
658 ---------------------
660 procedure Process_Project (Project : Project_Id) is
661 Data : constant Project_Data := Projects.Table (Project);
662 Imported : Project_List := Data.Imported_Projects;
663 Element : Project_Element;
666 -- Nothing to do if process has already been processed
668 if not Processed_Projects.Get (Data.Name) then
669 Processed_Projects.Set (Data.Name, True);
671 -- Call Process_Project recursively for any imported project.
672 -- We first process the imported projects to guarantee that
673 -- we have a proper reverse order for the libraries.
675 while Imported /= Empty_Project_List loop
676 Element := Project_Lists.Table (Imported);
678 if Element.Project /= No_Project then
679 Process_Project (Element.Project);
682 Imported := Element.Next;
685 -- If it is a library project, add it to Library_Projs
687 if Project /= For_Project and then Data.Library then
688 Library_Projs.Increment_Last;
689 Library_Projs.Table (Library_Projs.Last) := Project;
695 -- Start of processing for Process_Imported_Libraries
698 -- Build list of library projects imported directly or indirectly,
699 -- in the reverse order.
701 Process_Project (For_Project);
703 -- Add the -L and -l switches and, if the Rpath option is supported,
704 -- add the directory to the Rpath.
705 -- As the library projects are in the wrong order, process from the
706 -- last to the first.
708 for Index in reverse 1 .. Library_Projs.Last loop
709 Current := Library_Projs.Table (Index);
711 Get_Name_String (Projects.Table (Current).Library_Dir);
713 Opts.Table (Opts.Last) :=
714 new String'("-L" & Name_Buffer (1 .. Name_Len));
716 if Path_Option /= null then
717 Add_Rpath (Name_Buffer (1 .. Name_Len));
721 Opts.Table (Opts.Last) :=
725 (Projects.Table (Current).Library_Name));
727 end Process_Imported_Libraries;
729 -- Start of processing for Build_Library
734 -- Fail if project is not a library project
736 if not Data.Library then
737 Com.Fail ("project """, Project_Name, """ has no library");
740 -- If this is the first time Build_Library is called, get the Name_Id
741 -- of "s-osinte.ads".
743 if S_Osinte_Ads = No_Name then
745 Name_Buffer (1 .. Name_Len) := "s-osinte.ads";
746 S_Osinte_Ads := Name_Find;
749 if S_Dec_Ads = No_Name then
751 Name_Buffer (1 .. Name_Len) := "dec.ads";
752 S_Dec_Ads := Name_Find;
755 -- We work in the object directory
757 Change_Dir (Object_Directory_Path);
760 -- Call gnatbind only if Bind is True
763 if Gnatbind_Path = null then
764 Com.Fail ("unable to locate ", Gnatbind);
767 if Gcc_Path = null then
768 Com.Fail ("unable to locate ", Gcc);
771 -- Allocate Arguments, if it is the first time we see a standalone
774 if Arguments = No_Argument then
775 Arguments := new String_List (1 .. Initial_Argument_Max);
778 -- Add "-n -o b~<lib>.adb (b$<lib>.adb on VMS) -L<lib>"
780 Argument_Number := 2;
781 Arguments (1) := No_Main;
782 Arguments (2) := Output_Switch;
784 if Hostparm.OpenVMS then
785 B_Start (B_Start'Last) := '$';
789 (B_Start & Get_Name_String (Data.Library_Name) & ".adb");
790 Add_Argument ("-L" & Get_Name_String (Data.Library_Name));
792 -- Check if Binder'Default_Switches ("Ada) is defined. If it is,
793 -- add these switches to call gnatbind.
796 Binder_Package : constant Package_Id :=
798 (Name => Name_Binder,
799 In_Packages => Data.Decl.Packages);
802 if Binder_Package /= No_Package then
804 Defaults : constant Array_Element_Id :=
806 (Name => Name_Default_Switches,
809 (Binder_Package).Decl.Arrays);
810 Switches : Variable_Value := Nil_Variable_Value;
812 Switch : String_List_Id := Nil_String;
815 if Defaults /= No_Array_Element then
818 (Index => Name_Ada, In_Array => Defaults);
820 if not Switches.Default then
821 Switch := Switches.Values;
823 while Switch /= Nil_String loop
826 (String_Elements.Table (Switch).Value));
827 Switch := String_Elements.Table (Switch).Next;
836 -- Get all the ALI files of the project file. We do that even if
837 -- Bind is False, so that First_ALI is set.
844 Interface_ALIs.Reset;
845 Processed_ALIs.Reset;
847 for Source in 1 .. Com.Units.Last loop
848 Unit := Com.Units.Table (Source);
850 if Unit.File_Names (Body_Part).Name /= No_Name
851 and then Unit.File_Names (Body_Part).Path /= Slash
854 Check_Project (Unit.File_Names (Body_Part).Project)
856 if Unit.File_Names (Specification).Name = No_Name then
858 Src_Ind : Source_File_Index;
861 Src_Ind := Sinput.P.Load_Project_File
866 -- Add the ALI file only if it is not a subunit
869 not Sinput.P.Source_File_Is_Subunit (Src_Ind)
872 (Unit.File_Names (Body_Part).Name);
878 Add_ALI_For (Unit.File_Names (Body_Part).Name);
883 elsif Unit.File_Names (Specification).Name /= No_Name
884 and then Unit.File_Names (Specification).Path /= Slash
885 and then Check_Project
886 (Unit.File_Names (Specification).Project)
888 Add_ALI_For (Unit.File_Names (Specification).Name);
894 -- Continue setup and call gnatbind if Bind is True
898 -- Get an eventual --RTS from the ALI file
900 if First_ALI /= No_Name then
909 T := Read_Library_Info (First_ALI, True);
914 (First_ALI, T, Ignore_ED => False, Err => False);
916 if A /= No_ALI_Id then
919 (ALI.ALIs.Table (A).First_Unit).First_Arg ..
921 (ALI.ALIs.Table (A).First_Unit).Last_Arg
923 -- Look for --RTS. If found, add the switch to call
927 Arg : String_Ptr renames Args.Table (Index);
930 Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
932 Add_Argument (Arg.all);
944 (Project => For_Project, Including_Libraries => True);
946 -- Display the gnatbind command, if not in quiet output
953 (Gnatbind_Path.all, Arguments (1 .. Argument_Number), Success);
956 Com.Fail ("could not bind standalone library ",
957 Get_Name_String (Data.Library_Name));
961 -- Compile the binder generated file only if Link is true
967 (Project => For_Project, Including_Libraries => True);
969 -- Invoke <gcc> -c b$$<lib>.adb
971 -- Allocate Arguments, if it is the first time we see a standalone
974 if Arguments = No_Argument then
975 Arguments := new String_List (1 .. Initial_Argument_Max);
978 Argument_Number := 1;
979 Arguments (1) := Compile_Switch;
981 if Hostparm.OpenVMS then
982 B_Start (B_Start'Last) := '$';
986 (B_Start & Get_Name_String (Data.Library_Name) & ".adb");
988 -- If necessary, add the PIC option
990 if PIC_Option /= "" then
991 Add_Argument (PIC_Option);
994 -- Get the back-end switches and --RTS from the ALI file
996 if First_ALI /= No_Name then
1003 -- Load the ALI file
1005 T := Read_Library_Info (First_ALI, True);
1010 (First_ALI, T, Ignore_ED => False, Err => False);
1012 if A /= No_ALI_Id then
1015 (ALI.ALIs.Table (A).First_Unit).First_Arg ..
1017 (ALI.ALIs.Table (A).First_Unit).Last_Arg
1019 -- Do not compile with the front end switches except
1023 Arg : String_Ptr renames Args.Table (Index);
1025 if not Is_Front_End_Switch (Arg.all)
1027 Arg (Arg'First + 2 .. Arg'First + 5) = "RTS="
1029 Add_Argument (Arg.all);
1037 -- Now that all the arguments are set, compile the binder
1042 (Gcc_Path.all, Arguments (1 .. Argument_Number), Success);
1046 ("could not compile binder generated file for library ",
1047 Get_Name_String (Data.Library_Name));
1050 -- Process binder generated file for pragmas Linker_Options
1052 Process_Binder_File (Arguments (2).all & ASCII.NUL);
1056 -- Build the library only if Link is True
1059 -- If attribute Library_GCC was specified, get the driver name
1061 Library_GCC := Value_Of (Name_Library_GCC, Data.Decl.Attributes);
1063 if not Library_GCC.Default then
1064 Driver_Name := Library_GCC.Value;
1067 -- If attribute Library_Options was specified, add these additional
1071 Value_Of (Name_Library_Options, Data.Decl.Attributes);
1073 if not Library_Options.Default then
1075 Current : String_List_Id := Library_Options.Values;
1076 Element : String_Element;
1079 while Current /= Nil_String loop
1080 Element := String_Elements.Table (Current);
1081 Get_Name_String (Element.Value);
1083 if Name_Len /= 0 then
1084 Opts.Increment_Last;
1085 Opts.Table (Opts.Last) :=
1086 new String'(Name_Buffer (1 .. Name_Len));
1089 Current := Element.Next;
1094 Lib_Dirpath := new String'(Get_Name_String (Data.Library_Dir));
1095 Lib_Filename := new String'(Get_Name_String (Data.Library_Name));
1097 case Data.Library_Kind is
1099 The_Build_Mode := Static;
1102 The_Build_Mode := Dynamic;
1105 The_Build_Mode := Relocatable;
1107 if PIC_Option /= "" then
1108 Opts.Increment_Last;
1109 Opts.Table (Opts.Last) := new String'(PIC_Option);
1113 -- Get the library version, if any
1115 if Data.Lib_Internal_Name /= No_Name then
1117 new String'(Get_Name_String (Data.Lib_Internal_Name));
1120 -- Add the objects found in the object directory and the object
1121 -- directories of the extended files, if any, except for generated
1122 -- object files (b~.. or B$..) from extended projects.
1123 -- When there are one or more extended files, only add an object file
1124 -- if no object file with the same name have already been added.
1126 In_Main_Object_Directory := True;
1130 Object_Dir_Path : constant String :=
1131 Get_Name_String (Data.Object_Directory);
1132 Object_Dir : Dir_Type;
1133 Filename : String (1 .. 255);
1138 Open (Dir => Object_Dir, Dir_Name => Object_Dir_Path);
1140 -- For all entries in the object directory
1143 Read (Object_Dir, Filename, Last);
1147 -- Check if it is an object file
1149 if Is_Obj (Filename (1 .. Last)) then
1151 Object_Path : String :=
1153 (Object_Dir_Path & Directory_Separator &
1154 Filename (1 .. Last));
1157 Canonical_Case_File_Name (Object_Path);
1158 Canonical_Case_File_Name (Filename (1 .. Last));
1160 -- If in the object directory of an extended project,
1161 -- do not consider generated object files.
1163 if In_Main_Object_Directory
1165 or else Filename (1 .. B_Start'Length) /= B_Start
1168 Name_Buffer (1 .. Name_Len) := Filename (1 .. Last);
1171 if not Objects_Htable.Get (Id) then
1173 -- Record this object file
1175 Objects_Htable.Set (Id, True);
1176 Objects.Increment_Last;
1177 Objects.Table (Objects.Last) :=
1178 new String'(Object_Path);
1181 ALI_File : constant String :=
1182 Ext_To (Object_Path, "ali");
1185 if Is_Regular_File (ALI_File) then
1187 -- Record the ALI file
1189 ALIs.Increment_Last;
1190 ALIs.Table (ALIs.Last) :=
1191 new String'(ALI_File);
1193 -- Find out if for this ALI file,
1194 -- libgnarl or libdecgnat (on OpenVMS)
1197 Check_Libs (ALI_File);
1200 -- Object file is a foreign object file
1202 Foreigns.Increment_Last;
1203 Foreigns.Table (Foreigns.Last) :=
1204 new String'(Object_Path);
1213 Close (Dir => Object_Dir);
1216 when Directory_Error =>
1217 Com.Fail ("cannot find object directory """,
1218 Get_Name_String (Data.Object_Directory),
1222 exit when Data.Extends = No_Project;
1224 In_Main_Object_Directory := False;
1225 Data := Projects.Table (Data.Extends);
1228 -- Add the -L and -l switches for the imported Library Project Files,
1229 -- and, if Path Option is supported, the library directory path names
1232 Process_Imported_Libraries;
1234 -- Link with libgnat and possibly libgnarl
1236 Opts.Increment_Last;
1237 Opts.Table (Opts.Last) := new String'("-L" & Lib_Directory);
1239 -- If Path Option is supported, add libgnat directory path name to
1242 if Path_Option /= null then
1243 Add_Rpath (Lib_Directory);
1246 if Libgnarl_Needed then
1247 Opts.Increment_Last;
1249 if The_Build_Mode = Static then
1250 Opts.Table (Opts.Last) := new String'("-lgnarl");
1252 Opts.Table (Opts.Last) := new String'(Shared_Lib ("gnarl"));
1256 if Libdecgnat_Needed then
1257 Opts.Increment_Last;
1258 Opts.Table (Opts.Last) :=
1259 new String'("-L" & Lib_Directory & "/../declib");
1260 Opts.Increment_Last;
1261 Opts.Table (Opts.Last) := new String'("-ldecgnat");
1264 Opts.Increment_Last;
1266 if The_Build_Mode = Static then
1267 Opts.Table (Opts.Last) := new String'("-lgnat");
1269 Opts.Table (Opts.Last) := new String'(Shared_Lib ("gnat"));
1272 -- If Path Option is supported, add the necessary switch with the
1273 -- content of Rpath. As Rpath contains at least libgnat directory
1274 -- path name, it is guaranteed that it is not null.
1276 if Path_Option /= null then
1277 Opts.Increment_Last;
1278 Opts.Table (Opts.Last) :=
1279 new String'(Path_Option.all & Rpath (1 .. Rpath_Last));
1286 (Argument_List (Objects.Table (1 .. Objects.Last)));
1289 new Argument_List'(Argument_List
1290 (Foreigns.Table (1 .. Foreigns.Last)));
1293 new Argument_List'(Argument_List (ALIs.Table (1 .. ALIs.Last)));
1296 new Argument_List'(Argument_List (Opts.Table (1 .. Opts.Last)));
1298 -- We fail if there are no object to put in the library
1299 -- (Ada or foreign objects).
1301 if Object_Files'Length = 0 then
1302 Com.Fail ("no object files for library """ &
1303 Lib_Filename.all & '"');
1306 if not Opt.Quiet_Output then
1308 Write_Str ("building ");
1309 Write_Str (Ada.Characters.Handling.To_Lower
1310 (Build_Mode_State'Image (The_Build_Mode)));
1311 Write_Str (" library for project ");
1312 Write_Line (Project_Name);
1316 Write_Line ("object files:");
1318 for Index in Object_Files'Range loop
1320 Write_Line (Object_Files (Index).all);
1325 if Ali_Files'Length = 0 then
1326 Write_Line ("NO ALI files");
1329 Write_Line ("ALI files:");
1331 for Index in Ali_Files'Range loop
1333 Write_Line (Ali_Files (Index).all);
1340 -- We check that all object files are regular files
1344 -- Delete the existing library file, if it exists.
1345 -- Fail if the library file is not writable, or if it is not possible
1346 -- to delete the file.
1349 DLL_Name : aliased String :=
1350 Lib_Dirpath.all & "/lib" &
1351 Lib_Filename.all & "." & DLL_Ext;
1353 Archive_Name : aliased String :=
1354 Lib_Dirpath.all & "/lib" &
1355 Lib_Filename.all & "." & Archive_Ext;
1357 type Str_Ptr is access all String;
1358 -- This type is necessary to meet the accessibility rules of Ada.
1359 -- It is not possible to use String_Access here.
1361 Full_Lib_Name : Str_Ptr;
1362 -- Designates the full library path name. Either DLL_Name or
1363 -- Archive_Name, depending on the library kind.
1365 Success : Boolean := False;
1366 -- Used to call Delete_File
1369 if The_Build_Mode = Static then
1370 Full_Lib_Name := Archive_Name'Access;
1372 Full_Lib_Name := DLL_Name'Access;
1375 if Is_Regular_File (Full_Lib_Name.all) then
1376 if Is_Writable_File (Full_Lib_Name.all) then
1377 Delete_File (Full_Lib_Name.all, Success);
1380 if Is_Regular_File (Full_Lib_Name.all) then
1381 Com.Fail ("could not delete """ & Full_Lib_Name.all & """");
1386 Argument_Number := 0;
1388 -- If we have a standalone library, gather all the interface ALI.
1389 -- They are passed to Build_Dynamic_Library, where they are used by
1390 -- some platforms (VMS, for example) to decide what symbols should be
1391 -- exported. They are also flagged as Interface when we copy them to
1392 -- the library directory (by Copy_ALI_Files, below).
1395 Data := Projects.Table (For_Project);
1398 Interface : String_List_Id := Data.Lib_Interface_ALIs;
1399 ALI : File_Name_Type;
1402 while Interface /= Nil_String loop
1403 ALI := String_Elements.Table (Interface).Value;
1404 Interface_ALIs.Set (ALI, True);
1405 Get_Name_String (String_Elements.Table (Interface).Value);
1406 Add_Argument (Name_Buffer (1 .. Name_Len));
1407 Interface := String_Elements.Table (Interface).Next;
1410 Interface := Data.Lib_Interface_ALIs;
1412 if not Opt.Quiet_Output then
1414 -- Check that the interface set is complete: any unit in the
1415 -- library that is needed by an interface should also be an
1416 -- interface. If it is not the case, output a warning.
1418 while Interface /= Nil_String loop
1419 ALI := String_Elements.Table (Interface).Value;
1421 Interface := String_Elements.Table (Interface).Next;
1427 -- Clean the library directory, if it is also the directory where
1428 -- the ALI files are copied, either because there is no interface
1429 -- copy directory or because the interface copy directory is the
1430 -- same as the library directory.
1432 Copy_Dir := Projects.Table (For_Project).Library_Dir;
1435 -- Call procedure to build the library, depending on the build mode
1437 case The_Build_Mode is
1438 when Dynamic | Relocatable =>
1439 Build_Dynamic_Library
1440 (Ofiles => Object_Files.all,
1441 Foreign => Foreign_Objects.all,
1442 Afiles => Ali_Files.all,
1443 Options => Options.all,
1444 Interfaces => Arguments (1 .. Argument_Number),
1445 Lib_Filename => Lib_Filename.all,
1446 Lib_Dir => Lib_Dirpath.all,
1447 Symbol_Data => Data.Symbol_Data,
1448 Driver_Name => Driver_Name,
1449 Lib_Address => DLL_Address.all,
1450 Lib_Version => Lib_Version.all,
1451 Relocatable => The_Build_Mode = Relocatable,
1452 Auto_Init => Data.Lib_Auto_Init);
1465 -- We need to copy the ALI files from the object directory to
1466 -- the library directory, so that the linker find them there,
1467 -- and does not need to look in the object directory where it
1468 -- would also find the object files; and we don't want that:
1469 -- we want the linker to use the library.
1471 -- Copy the ALI files and make the copies read-only. For interfaces,
1472 -- mark the copies as interfaces.
1475 (Files => Ali_Files.all,
1477 Interfaces => Arguments (1 .. Argument_Number));
1479 -- Copy interface sources if Library_Src_Dir specified
1482 and then Projects.Table (For_Project).Library_Src_Dir /= No_Name
1484 -- Clean the interface copy directory, if it is not also the
1485 -- library directory. If it is also the library directory, it
1486 -- has already been cleaned before generation of the library.
1488 if Projects.Table (For_Project).Library_Src_Dir /= Copy_Dir then
1489 Copy_Dir := Projects.Table (For_Project).Library_Src_Dir;
1493 Copy_Interface_Sources
1494 (For_Project => For_Project,
1495 Interfaces => Arguments (1 .. Argument_Number),
1496 To_Dir => Copy_Dir);
1500 -- Reset the current working directory to its previous value
1502 Change_Dir (Current_Dir);
1509 procedure Check (Filename : String) is
1511 if not Is_Regular_File (Filename) then
1512 Com.Fail (Filename, " not found.");
1520 procedure Check_Context is
1522 -- Check that each object file exists
1524 for F in Object_Files'Range loop
1525 Check (Object_Files (F).all);
1533 procedure Check_Library (For_Project : Project_Id) is
1534 Data : constant Project_Data := Projects.Table (For_Project);
1537 if Data.Library and not Data.Flag1 then
1539 Current : constant Dir_Name_Str := Get_Current_Dir;
1540 Lib_Name : constant Name_Id := Library_File_Name_For (For_Project);
1541 Lib_TS : Time_Stamp_Type;
1542 Obj_TS : Time_Stamp_Type;
1544 Object_Dir : Dir_Type;
1547 if Hostparm.OpenVMS then
1548 B_Start (B_Start'Last) := '$';
1551 Change_Dir (Get_Name_String (Data.Library_Dir));
1553 Lib_TS := File_Stamp (Lib_Name);
1555 -- If the library file does not exist, then the time stamp will
1556 -- be Empty_Time_Stamp, earlier than any other time stamp.
1558 Change_Dir (Get_Name_String (Data.Object_Directory));
1559 Open (Dir => Object_Dir, Dir_Name => ".");
1561 -- For all entries in the object directory
1564 Read (Object_Dir, Name_Buffer, Name_Len);
1565 exit when Name_Len = 0;
1567 -- Check if it is an object file, but ignore any binder
1570 if Is_Obj (Name_Buffer (1 .. Name_Len))
1571 and then Name_Buffer (1 .. B_Start'Length) /= B_Start
1573 -- Get the object file time stamp
1575 Obj_TS := File_Stamp (Name_Find);
1577 -- If library file time stamp is earlier, set Flag1 and
1578 -- return. String comparaison is used, otherwise time stamps
1579 -- may be too close and the comparaison would return True,
1580 -- which would trigger an unnecessary rebuild of the
1583 if String (Lib_TS) < String (Obj_TS) then
1585 -- Library must be rebuilt
1587 Projects.Table (For_Project).Flag1 := True;
1593 Change_Dir (Current);
1602 procedure Clean (Directory : Name_Id) is
1603 Current : constant Dir_Name_Str := Get_Current_Dir;
1607 Name : String (1 .. 200);
1610 Disregard : Boolean;
1612 procedure Set_Writable (Name : System.Address);
1613 pragma Import (C, Set_Writable, "__gnat_set_writable");
1616 Get_Name_String (Directory);
1618 -- Change the working directory to the directory to clean
1621 Change_Dir (Name_Buffer (1 .. Name_Len));
1626 ("unable to access directory """,
1627 Name_Buffer (1 .. Name_Len),
1633 -- For each regular file in the directory, make it writable and
1637 Read (Dir, Name, Last);
1640 if Is_Regular_File (Name (1 .. Last)) then
1641 Name (Last + 1) := ASCII.NUL;
1642 Set_Writable (Name (1)'Address);
1643 Delete_File (Name (1 .. Last), Disregard);
1649 -- Restore the initial working directory
1651 Change_Dir (Current);
1654 ----------------------------
1655 -- Copy_Interface_Sources --
1656 ----------------------------
1658 procedure Copy_Interface_Sources
1659 (For_Project : Project_Id;
1660 Interfaces : Argument_List;
1663 Current : constant Dir_Name_Str := Get_Current_Dir;
1664 Target : constant Dir_Name_Str := Get_Name_String (To_Dir);
1666 Text : Text_Buffer_Ptr;
1667 The_ALI : ALI.ALI_Id;
1670 First_Unit : ALI.Unit_Id;
1671 Second_Unit : ALI.Unit_Id;
1675 Copy_Subunits : Boolean := False;
1677 procedure Copy (File_Name : Name_Id);
1678 -- Copy one source of the project to the target directory
1684 procedure Copy (File_Name : Name_Id) is
1685 Success : Boolean := False;
1689 for Index in 1 .. Com.Units.Last loop
1690 Data := Com.Units.Table (Index);
1692 for J in Data.File_Names'Range loop
1693 if Data.File_Names (J).Project = For_Project
1694 and then Data.File_Names (J).Name = File_Name
1697 (Get_Name_String (Data.File_Names (J).Path),
1701 Preserve => Preserve);
1710 -- Start of processing for Copy_Interface_Sources
1713 -- Change the working directory to the object directory
1716 (Get_Name_String (Projects.Table (For_Project).Object_Directory));
1718 for Index in Interfaces'Range loop
1720 -- First, load the ALI file
1723 Add_Str_To_Name_Buffer (Interfaces (Index).all);
1724 Lib_File := Name_Find;
1725 Text := Read_Library_Info (Lib_File);
1726 The_ALI := Scan_ALI (Lib_File, Text, Ignore_ED => False, Err => True);
1729 Second_Unit := No_Unit_Id;
1730 First_Unit := ALI.ALIs.Table (The_ALI).First_Unit;
1731 Copy_Subunits := True;
1733 -- If there is both a spec and a body, check if they are both needed
1735 if ALI.Units.Table (First_Unit).Utype = Is_Body then
1736 Second_Unit := ALI.ALIs.Table (The_ALI).Last_Unit;
1738 -- If the body is not needed, then reset First_Unit
1740 if not ALI.Units.Table (Second_Unit).Body_Needed_For_SAL then
1741 First_Unit := No_Unit_Id;
1742 Copy_Subunits := False;
1745 elsif ALI.Units.Table (First_Unit).Utype = Is_Spec_Only then
1746 Copy_Subunits := False;
1749 -- Copy the file(s) that need to be copied
1751 if First_Unit /= No_Unit_Id then
1752 Copy (File_Name => ALI.Units.Table (First_Unit).Sfile);
1755 if Second_Unit /= No_Unit_Id then
1756 Copy (File_Name => ALI.Units.Table (Second_Unit).Sfile);
1759 -- Copy all the separates, if any
1761 if Copy_Subunits then
1762 for Dep in ALI.ALIs.Table (The_ALI).First_Sdep ..
1763 ALI.ALIs.Table (The_ALI).Last_Sdep
1765 if Sdep.Table (Dep).Subunit_Name /= No_Name then
1766 Copy (File_Name => Sdep.Table (Dep).Sfile);
1772 -- Restore the initial working directory
1774 Change_Dir (Current);
1775 end Copy_Interface_Sources;
1781 procedure Display (Executable : String) is
1783 if not Opt.Quiet_Output then
1784 Write_Str (Executable);
1786 for Index in 1 .. Argument_Number loop
1788 Write_Str (Arguments (Index).all);
1795 -------------------------
1796 -- Process_Binder_File --
1797 -------------------------
1799 procedure Process_Binder_File (Name : String) is
1801 -- Binder file's descriptor
1803 Read_Mode : constant String := "r" & ASCII.Nul;
1806 Status : Interfaces.C_Streams.int;
1807 pragma Unreferenced (Status);
1810 Begin_Info : constant String := "-- BEGIN Object file/option list";
1811 End_Info : constant String := "-- END Object file/option list ";
1813 Next_Line : String (1 .. 1000);
1814 -- Current line value
1815 -- Where does this odd constant 1000 come from, looks suspicious ???
1818 -- End of line slice (the slice does not contain the line terminator)
1820 procedure Get_Next_Line;
1821 -- Read the next line from the binder file without the line terminator
1827 procedure Get_Next_Line is
1831 Fchars := fgets (Next_Line'Address, Next_Line'Length, Fd);
1833 if Fchars = System.Null_Address then
1834 Fail ("Error reading binder output");
1838 while Nlast <= Next_Line'Last
1839 and then Next_Line (Nlast) /= ASCII.LF
1840 and then Next_Line (Nlast) /= ASCII.CR
1848 -- Start of processing for Process_Binder_File
1851 Fd := fopen (Name'Address, Read_Mode'Address);
1853 if Fd = NULL_Stream then
1854 Fail ("Failed to open binder output");
1857 -- Skip up to the Begin Info line
1861 exit when Next_Line (1 .. Nlast) = Begin_Info;
1864 -- Find the first switch
1869 exit when Next_Line (1 .. Nlast) = End_Info;
1871 -- As the binder generated file is in Ada, remove the first eight
1872 -- characters " -- ".
1874 Next_Line (1 .. Nlast - 8) := Next_Line (9 .. Nlast);
1877 -- Stop when the first switch is found
1879 exit when Next_Line (1) = '-';
1882 if Next_Line (1 .. Nlast) /= End_Info then
1884 -- Ignore -static and -shared, since -shared will be used
1887 -- Ignore -lgnat, -lgnarl and -ldecgnat as they will be added
1888 -- later, because they are also needed for non Stand-Alone shared
1891 -- Also ignore the shared libraries which are :
1893 -- UNIX / Windows VMS
1894 -- -lgnat-<version> -lgnat_<version> (7 + version'length chars)
1895 -- -lgnarl-<version> -lgnarl_<version> (8 + version'length chars)
1897 if Next_Line (1 .. Nlast) /= "-static" and then
1898 Next_Line (1 .. Nlast) /= "-shared" and then
1899 Next_Line (1 .. Nlast) /= "-ldecgnat" and then
1900 Next_Line (1 .. Nlast) /= "-lgnarl" and then
1901 Next_Line (1 .. Nlast) /= "-lgnat" and then
1903 (1 .. Natural'Min (Nlast, 8 + Library_Version'Length)) /=
1904 Shared_Lib ("gnarl") and then
1906 (1 .. Natural'Min (Nlast, 7 + Library_Version'Length)) /=
1909 if Next_Line (1) /= '-' then
1911 -- This is not an option, should we add it?
1913 if Add_Object_Files then
1914 Opts.Increment_Last;
1915 Opts.Table (Opts.Last) :=
1916 new String'(Next_Line (1 .. Nlast));
1920 -- Add all other options
1922 Opts.Increment_Last;
1923 Opts.Table (Opts.Last) :=
1924 new String'(Next_Line (1 .. Nlast));
1928 -- Next option, if any
1931 exit when Next_Line (1 .. Nlast) = End_Info;
1933 -- Remove first eight characters " -- "
1935 Next_Line (1 .. Nlast - 8) := Next_Line (9 .. Nlast);
1940 Status := fclose (Fd);
1941 -- Is it really right to ignore any close error ???
1942 end Process_Binder_File;
1948 procedure Reset_Tables is
1951 Objects_Htable.Reset;
1955 Processed_Projects.Reset;