* doc/install.texi (*-*-aix): Update explanation of XLC bootstrap.
[official-gcc.git] / gcc / ada / mlib.adb
blob6c1a4918340de222a085787a32749a01308df3f4
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- M L I B --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1999-2009, AdaCore --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Ada.Characters.Handling; use Ada.Characters.Handling;
27 with Interfaces.C.Strings;
28 with System;
30 with Hostparm;
31 with Opt;
32 with Output; use Output;
34 with MLib.Utl; use MLib.Utl;
36 with Prj.Com;
38 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
40 package body MLib is
42 -------------------
43 -- Build_Library --
44 -------------------
46 procedure Build_Library
47 (Ofiles : Argument_List;
48 Output_File : String;
49 Output_Dir : String)
51 begin
52 if Opt.Verbose_Mode and not Opt.Quiet_Output then
53 Write_Line ("building a library...");
54 Write_Str (" make ");
55 Write_Line (Output_File);
56 end if;
58 Ar (Output_Dir & Directory_Separator &
59 "lib" & Output_File & ".a", Objects => Ofiles);
60 end Build_Library;
62 ------------------------
63 -- Check_Library_Name --
64 ------------------------
66 procedure Check_Library_Name (Name : String) is
67 begin
68 if Name'Length = 0 then
69 Prj.Com.Fail ("library name cannot be empty");
70 end if;
72 if Name'Length > Max_Characters_In_Library_Name then
73 Prj.Com.Fail ("illegal library name """
74 & Name
75 & """: too long");
76 end if;
78 if not Is_Letter (Name (Name'First)) then
79 Prj.Com.Fail ("illegal library name """
80 & Name
81 & """: should start with a letter");
82 end if;
84 for Index in Name'Range loop
85 if not Is_Alphanumeric (Name (Index)) then
86 Prj.Com.Fail ("illegal library name """
87 & Name
88 & """: should include only letters and digits");
89 end if;
90 end loop;
91 end Check_Library_Name;
93 --------------------
94 -- Copy_ALI_Files --
95 --------------------
97 procedure Copy_ALI_Files
98 (Files : Argument_List;
99 To : Path_Name_Type;
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.
110 ------------------
111 -- Verbose_Copy --
112 ------------------
114 procedure Verbose_Copy (Index : Positive) is
115 begin
116 if Opt.Verbose_Mode then
117 Write_Str ("Copying """);
118 Write_Str (Files (Index).all);
119 Write_Str (""" to """);
120 Write_Str (To_Dir);
121 Write_Line ("""");
122 end if;
123 end Verbose_Copy;
125 -- Start of processing for Copy_ALI_Files
127 begin
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);
134 Set_Writable
135 (To_Dir &
136 Directory_Separator &
137 Base_Name (Files (Index).all));
138 Copy_File
139 (Files (Index).all,
140 To_Dir,
141 Success,
142 Mode => Overwrite,
143 Preserve => Preserve);
145 exit when not Success;
146 end loop;
148 else
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
154 declare
155 File_Name : String := Base_Name (Files (Index).all);
157 begin
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;
167 exit;
168 end if;
169 end loop;
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.
175 if Is_Interface then
176 Success := False;
177 Verbose_Copy (Index);
178 Set_Writable
179 (To_Dir &
180 Directory_Separator &
181 Base_Name (Files (Index).all));
183 declare
184 FD : File_Descriptor;
185 Len : Integer;
186 Actual_Len : Integer;
187 S : String_Access;
188 Curr : Natural;
189 P_Line_Found : Boolean;
190 Status : Boolean;
192 begin
193 -- Open the file
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.
210 Curr := 1;
211 Actual_Len := Len;
213 while Actual_Len /= 0 loop
214 Actual_Len := Read (FD, S (Curr)'Address, Len);
215 Curr := Curr + Actual_Len;
216 end loop;
218 -- We are done with the input file, so we close it
219 -- ignoring any bad status.
221 Close (FD, 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)
231 and then
232 S (Index + 1) = 'P'
233 then
234 S (Index + 5 .. Len + 3) := S (Index + 2 .. Len);
235 S (Index + 2 .. Index + 4) := " SL";
236 P_Line_Found := True;
237 exit;
238 end if;
239 end loop;
241 if P_Line_Found then
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;
249 Name_Buffer
250 (Name_Len + 1 .. Name_Len + File_Name'Length) :=
251 File_Name;
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
258 -- created file.
260 if FD /= Invalid_FD then
261 Actual_Len := Write (FD, S (1)'Address, Len + 3);
263 Close (FD, Status);
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;
270 if Success then
272 -- Set_Read_Only is used here, rather than
273 -- Set_Non_Writable, so that gprbuild can
274 -- he compiled with older compilers.
276 Set_Read_Only
277 (Name_Buffer (1 .. Name_Len - 1));
278 end if;
279 end if;
280 end if;
281 end if;
282 end;
284 -- This is not an interface ALI
286 else
287 Success := True;
288 end if;
289 end;
291 if not Success then
292 Prj.Com.Fail ("could not copy ALI files to library dir");
293 end if;
294 end loop;
295 end if;
296 end Copy_ALI_Files;
298 ----------------------
299 -- Create_Sym_Links --
300 ----------------------
302 procedure Create_Sym_Links
303 (Lib_Path : String;
304 Lib_Version : String;
305 Lib_Dir : String;
306 Maj_Version : String)
308 function Symlink
309 (Oldpath : System.Address;
310 Newpath : System.Address) return Integer;
311 pragma Import (C, Symlink, "__gnat_symlink");
313 Version_Path : String_Access;
315 Success : Boolean;
316 Result : Integer;
317 pragma Unreferenced (Success, Result);
319 begin
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
325 declare
326 Newpath : String (1 .. Lib_Path'Length + 1);
327 begin
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);
332 end;
334 else
335 declare
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);
342 begin
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);
359 end;
360 end if;
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);
377 begin
378 if S'Length = 0 then
379 return null;
380 else
381 return new String'(S);
382 end if;
383 end Linker_Library_Path_Option;
385 -------------------
386 -- Major_Id_Name --
387 -------------------
389 function Major_Id_Name
390 (Lib_Filename : String;
391 Lib_Version : String)
392 return String
394 Maj_Version : constant String := Lib_Version;
395 Last_Maj : Positive;
396 Last : Positive;
397 Ok_Maj : Boolean := False;
399 begin
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;
405 else
406 Ok_Maj := Last_Maj /= Maj_Version'Last and then
407 Maj_Version (Last_Maj) = '.';
409 if Ok_Maj then
410 Last_Maj := Last_Maj - 1;
411 end if;
413 exit;
414 end if;
415 end loop;
417 if Ok_Maj then
418 Last := Last_Maj;
419 while Last > Maj_Version'First loop
420 if Maj_Version (Last) in '0' .. '9' then
421 Last := Last - 1;
423 else
424 Ok_Maj := Last /= Last_Maj and then
425 Maj_Version (Last) = '.';
427 if Ok_Maj then
428 Last := Last - 1;
429 Ok_Maj :=
430 Maj_Version (Maj_Version'First .. Last) = Lib_Filename;
431 end if;
433 exit;
434 end if;
435 end loop;
436 end if;
438 if Ok_Maj then
439 return Maj_Version (Maj_Version'First .. Last_Maj);
440 else
441 return "";
442 end if;
443 end Major_Id_Name;
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");
453 begin
454 return Separate_Paths;
455 end Separate_Run_Path_Options;
457 -- Package elaboration
459 begin
460 -- Copy_Attributes always fails on VMS
462 if Hostparm.OpenVMS then
463 Preserve := None;
464 end if;
465 end MLib;