1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1999-2009, AdaCore --
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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Ada.Characters.Handling; use Ada.Characters.Handling;
27 with Interfaces.C.Strings;
32 with Output; use Output;
34 with MLib.Utl; use MLib.Utl;
38 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
46 procedure Build_Library
47 (Ofiles : Argument_List;
52 if Opt.Verbose_Mode and not Opt.Quiet_Output then
53 Write_Line ("building a library...");
55 Write_Line (Output_File);
58 Ar (Output_Dir & Directory_Separator &
59 "lib" & Output_File & ".a", Objects => Ofiles);
62 ------------------------
63 -- Check_Library_Name --
64 ------------------------
66 procedure Check_Library_Name (Name : String) is
68 if Name'Length = 0 then
69 Prj.Com.Fail ("library name cannot be empty");
72 if Name'Length > Max_Characters_In_Library_Name then
73 Prj.Com.Fail ("illegal library name """
78 if not Is_Letter (Name (Name'First)) then
79 Prj.Com.Fail ("illegal library name """
81 & """: should start with a letter");
84 for Index in Name'Range loop
85 if not Is_Alphanumeric (Name (Index)) then
86 Prj.Com.Fail ("illegal library name """
88 & """: should include only letters and digits");
91 end Check_Library_Name;
97 procedure Copy_ALI_Files
98 (Files : Argument_List;
100 Interfaces : String_List)
102 Success : Boolean := False;
103 To_Dir : constant String := Get_Name_String (To);
104 Is_Interface : Boolean := False;
106 procedure Verbose_Copy (Index : Positive);
107 -- In verbose mode, output a message that the indexed file is copied
108 -- to the destination directory.
114 procedure Verbose_Copy (Index : Positive) is
116 if Opt.Verbose_Mode then
117 Write_Str ("Copying """);
118 Write_Str (Files (Index).all);
119 Write_Str (""" to """);
125 -- Start of processing for Copy_ALI_Files
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);
136 Directory_Separator &
137 Base_Name (Files (Index).all));
143 Preserve => Preserve);
145 exit when not Success;
149 -- Copy only the interface ALI file, and put the special indicator
150 -- "SL" on the P line.
152 for Index in Files'Range loop
155 File_Name : String := Base_Name (Files (Index).all);
158 Canonical_Case_File_Name (File_Name);
160 -- Check if this is one of the interface ALIs
162 Is_Interface := False;
164 for Index in Interfaces'Range loop
165 if File_Name = Interfaces (Index).all then
166 Is_Interface := True;
171 -- If it is an interface ALI, copy line by line. Insert
172 -- the interface indication at the end of the P line.
173 -- Do not copy ALI files that are not Interfaces.
177 Verbose_Copy (Index);
180 Directory_Separator &
181 Base_Name (Files (Index).all));
184 FD : File_Descriptor;
186 Actual_Len : Integer;
189 P_Line_Found : Boolean;
195 Name_Len := Files (Index)'Length;
196 Name_Buffer (1 .. Name_Len) := Files (Index).all;
197 Name_Len := Name_Len + 1;
198 Name_Buffer (Name_Len) := ASCII.NUL;
200 FD := Open_Read (Name_Buffer'Address, Binary);
202 if FD /= Invalid_FD then
203 Len := Integer (File_Length (FD));
205 S := new String (1 .. Len + 3);
207 -- Read the file. Note that the loop is not necessary
208 -- since the whole file is read at once except on VMS.
213 while Actual_Len /= 0 loop
214 Actual_Len := Read (FD, S (Curr)'Address, Len);
215 Curr := Curr + Actual_Len;
218 -- We are done with the input file, so we close it
219 -- ignoring any bad status.
223 P_Line_Found := False;
225 -- Look for the P line. When found, add marker SL
226 -- at the beginning of the P line.
228 for Index in 1 .. Len - 3 loop
229 if (S (Index) = ASCII.LF or else
230 S (Index) = ASCII.CR)
234 S (Index + 5 .. Len + 3) := S (Index + 2 .. Len);
235 S (Index + 2 .. Index + 4) := " SL";
236 P_Line_Found := True;
243 -- Create new modified ALI file
245 Name_Len := To_Dir'Length;
246 Name_Buffer (1 .. Name_Len) := To_Dir;
247 Name_Len := Name_Len + 1;
248 Name_Buffer (Name_Len) := Directory_Separator;
250 (Name_Len + 1 .. Name_Len + File_Name'Length) :=
252 Name_Len := Name_Len + File_Name'Length + 1;
253 Name_Buffer (Name_Len) := ASCII.NUL;
255 FD := Create_File (Name_Buffer'Address, Binary);
257 -- Write the modified text and close the newly
260 if FD /= Invalid_FD then
261 Actual_Len := Write (FD, S (1)'Address, Len + 3);
265 -- Set Success to True only if the newly
266 -- created file has been correctly written.
268 Success := Status and then Actual_Len = Len + 3;
272 -- Set_Read_Only is used here, rather than
273 -- Set_Non_Writable, so that gprbuild can
274 -- he compiled with older compilers.
277 (Name_Buffer (1 .. Name_Len - 1));
284 -- This is not an interface ALI
292 Prj.Com.Fail ("could not copy ALI files to library dir");
298 ----------------------
299 -- Create_Sym_Links --
300 ----------------------
302 procedure Create_Sym_Links
304 Lib_Version : String;
306 Maj_Version : String)
309 (Oldpath : System.Address;
310 Newpath : System.Address) return Integer;
311 pragma Import (C, Symlink, "__gnat_symlink");
313 Version_Path : String_Access;
317 pragma Unreferenced (Success, Result);
320 Version_Path := new String (1 .. Lib_Version'Length + 1);
321 Version_Path (1 .. Lib_Version'Length) := Lib_Version;
322 Version_Path (Version_Path'Last) := ASCII.NUL;
324 if Maj_Version'Length = 0 then
326 Newpath : String (1 .. Lib_Path'Length + 1);
328 Newpath (1 .. Lib_Path'Length) := Lib_Path;
329 Newpath (Newpath'Last) := ASCII.NUL;
330 Delete_File (Lib_Path, Success);
331 Result := Symlink (Version_Path (1)'Address, Newpath'Address);
336 Newpath1 : String (1 .. Lib_Path'Length + 1);
337 Maj_Path : constant String :=
338 Lib_Dir & Directory_Separator & Maj_Version;
339 Newpath2 : String (1 .. Maj_Path'Length + 1);
340 Maj_Ver : String (1 .. Maj_Version'Length + 1);
343 Newpath1 (1 .. Lib_Path'Length) := Lib_Path;
344 Newpath1 (Newpath1'Last) := ASCII.NUL;
346 Newpath2 (1 .. Maj_Path'Length) := Maj_Path;
347 Newpath2 (Newpath2'Last) := ASCII.NUL;
349 Maj_Ver (1 .. Maj_Version'Length) := Maj_Version;
350 Maj_Ver (Maj_Ver'Last) := ASCII.NUL;
352 Delete_File (Maj_Path, Success);
354 Result := Symlink (Version_Path (1)'Address, Newpath2'Address);
356 Delete_File (Lib_Path, Success);
358 Result := Symlink (Maj_Ver'Address, Newpath1'Address);
361 end Create_Sym_Links;
363 --------------------------------
364 -- Linker_Library_Path_Option --
365 --------------------------------
367 function Linker_Library_Path_Option return String_Access is
369 Run_Path_Option_Ptr : Interfaces.C.Strings.chars_ptr;
370 pragma Import (C, Run_Path_Option_Ptr, "__gnat_run_path_option");
371 -- Pointer to string representing the native linker option which
372 -- specifies the path where the dynamic loader should find shared
373 -- libraries. Equal to null string if this system doesn't support it.
375 S : constant String := Interfaces.C.Strings.Value (Run_Path_Option_Ptr);
381 return new String'(S);
383 end Linker_Library_Path_Option;
389 function Major_Id_Name
390 (Lib_Filename : String;
391 Lib_Version : String)
394 Maj_Version : constant String := Lib_Version;
397 Ok_Maj : Boolean := False;
400 Last_Maj := Maj_Version'Last;
401 while Last_Maj > Maj_Version'First loop
402 if Maj_Version (Last_Maj) in '0' .. '9' then
403 Last_Maj := Last_Maj - 1;
406 Ok_Maj := Last_Maj /= Maj_Version'Last and then
407 Maj_Version (Last_Maj) = '.';
410 Last_Maj := Last_Maj - 1;
419 while Last > Maj_Version'First loop
420 if Maj_Version (Last) in '0' .. '9' then
424 Ok_Maj := Last /= Last_Maj and then
425 Maj_Version (Last) = '.';
430 Maj_Version (Maj_Version'First .. Last) = Lib_Filename;
439 return Maj_Version (Maj_Version'First .. Last_Maj);
445 -------------------------------
446 -- Separate_Run_Path_Options --
447 -------------------------------
449 function Separate_Run_Path_Options return Boolean is
450 Separate_Paths : Boolean;
451 for Separate_Paths'Size use Character'Size;
452 pragma Import (C, Separate_Paths, "__gnat_separate_run_path_options");
454 return Separate_Paths;
455 end Separate_Run_Path_Options;
457 -- Package elaboration
460 -- Copy_Attributes always fails on VMS
462 if Hostparm.OpenVMS then