1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1999-2005, 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 ------------------------------------------------------------------------------
27 with Ada.Characters.Handling; use Ada.Characters.Handling;
28 with Interfaces.C.Strings;
32 with Output; use Output;
33 with Namet; use Namet;
35 with MLib.Utl; use MLib.Utl;
37 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
38 with GNAT.OS_Lib; use GNAT.OS_Lib;
46 procedure Build_Library
47 (Ofiles : Argument_List;
48 Afiles : Argument_List;
52 pragma Warnings (Off, Afiles);
57 if not Opt.Quiet_Output then
58 Write_Line ("building a library...");
60 Write_Line (Output_File);
63 Ar (Output_Dir & "/lib" & Output_File & ".a", Objects => Ofiles);
66 ------------------------
67 -- Check_Library_Name --
68 ------------------------
70 procedure Check_Library_Name (Name : String) is
72 if Name'Length = 0 then
73 Fail ("library name cannot be empty");
76 if Name'Length > Max_Characters_In_Library_Name then
77 Fail ("illegal library name """, Name, """: too long");
80 if not Is_Letter (Name (Name'First)) then
81 Fail ("illegal library name """,
83 """: should start with a letter");
86 for Index in Name'Range loop
87 if not Is_Alphanumeric (Name (Index)) then
88 Fail ("illegal library name """,
90 """: should include only letters and digits");
93 end Check_Library_Name;
99 procedure Copy_ALI_Files
100 (Files : Argument_List;
102 Interfaces : String_List)
104 Success : Boolean := False;
105 To_Dir : constant String := Get_Name_String (To);
106 Is_Interface : Boolean := False;
108 procedure Verbose_Copy (Index : Positive);
109 -- In verbose mode, output a message that the indexed file is copied
110 -- to the destination directory.
116 procedure Verbose_Copy (Index : Positive) is
118 if Opt.Verbose_Mode then
119 Write_Str ("Copying """);
120 Write_Str (Files (Index).all);
121 Write_Str (""" to """);
128 if Interfaces'Length = 0 then
130 -- If there are no Interfaces, copy all the ALI files as is
132 for Index in Files'Range loop
133 Verbose_Copy (Index);
139 Preserve => Preserve);
141 exit when not Success;
145 -- Copy only the interface ALI file, and put the special indicator
146 -- "SL" on the P line.
148 for Index in Files'Range loop
151 File_Name : String := Base_Name (Files (Index).all);
153 Canonical_Case_File_Name (File_Name);
155 -- Check if this is one of the interface ALIs
157 Is_Interface := False;
159 for Index in Interfaces'Range loop
160 if File_Name = Interfaces (Index).all then
161 Is_Interface := True;
166 -- If it is an interface ALI, copy line by line. Insert
167 -- the interface indication at the end of the P line.
168 -- Do not copy ALI files that are not Interfaces.
172 Verbose_Copy (Index);
175 FD : File_Descriptor;
177 Actual_Len : Integer;
180 P_Line_Found : Boolean;
186 Name_Len := Files (Index)'Length;
187 Name_Buffer (1 .. Name_Len) := Files (Index).all;
188 Name_Len := Name_Len + 1;
189 Name_Buffer (Name_Len) := ASCII.NUL;
191 FD := Open_Read (Name_Buffer'Address, Binary);
193 if FD /= Invalid_FD then
194 Len := Integer (File_Length (FD));
196 S := new String (1 .. Len + 3);
198 -- Read the file. Note that the loop is not necessary
199 -- since the whole file is read at once except on VMS.
204 while Actual_Len /= 0 loop
205 Actual_Len := Read (FD, S (Curr)'Address, Len);
206 Curr := Curr + Actual_Len;
209 -- We are done with the input file, so we close it
212 -- We simply ignore any bad status
214 P_Line_Found := False;
216 -- Look for the P line. When found, add marker SL
217 -- at the beginning of the P line.
219 for Index in 1 .. Len - 3 loop
220 if (S (Index) = ASCII.LF or else
221 S (Index) = ASCII.CR)
225 S (Index + 5 .. Len + 3) := S (Index + 2 .. Len);
226 S (Index + 2 .. Index + 4) := " SL";
227 P_Line_Found := True;
234 -- Create new modified ALI file
236 Name_Len := To_Dir'Length;
237 Name_Buffer (1 .. Name_Len) := To_Dir;
238 Name_Len := Name_Len + 1;
239 Name_Buffer (Name_Len) := Directory_Separator;
241 (Name_Len + 1 .. Name_Len + File_Name'Length) :=
243 Name_Len := Name_Len + File_Name'Length + 1;
244 Name_Buffer (Name_Len) := ASCII.NUL;
246 FD := Create_File (Name_Buffer'Address, Binary);
248 -- Write the modified text and close the newly
251 if FD /= Invalid_FD then
252 Actual_Len := Write (FD, S (1)'Address, Len + 3);
256 -- Set Success to True only if the newly
257 -- created file has been correctly written.
259 Success := Status and Actual_Len = Len + 3;
263 Name_Buffer (1 .. Name_Len - 1));
271 -- This is not an interface ALI
279 Fail ("could not copy ALI files to library dir");
285 --------------------------------
286 -- Linker_Library_Path_Option --
287 --------------------------------
289 function Linker_Library_Path_Option return String_Access is
291 Run_Path_Option_Ptr : Interfaces.C.Strings.chars_ptr;
292 pragma Import (C, Run_Path_Option_Ptr, "__gnat_run_path_option");
293 -- Pointer to string representing the native linker option which
294 -- specifies the path where the dynamic loader should find shared
295 -- libraries. Equal to null string if this system doesn't support it.
297 S : constant String := Interfaces.C.Strings.Value (Run_Path_Option_Ptr);
303 return new String'(S);
305 end Linker_Library_Path_Option;
307 -- Package elaboration
310 -- Copy_Attributes always fails on VMS
312 if Hostparm.OpenVMS then