OSDN Git Service

2007-08-31 Hristian Kirtchev <kirtchev@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / mlib.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                                 M L I B                                  --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                     Copyright (C) 1999-2007, 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 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,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Ada.Characters.Handling; use Ada.Characters.Handling;
28 with Interfaces.C.Strings;
29 with System;
30
31 with Hostparm;
32 with Opt;
33 with Output; use Output;
34
35 with MLib.Utl; use MLib.Utl;
36
37 with Prj.Com;
38
39 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
40
41 package body MLib is
42
43    -------------------
44    -- Build_Library --
45    -------------------
46
47    procedure Build_Library
48      (Ofiles      : Argument_List;
49       Output_File : String;
50       Output_Dir  : String)
51    is
52    begin
53       if Opt.Verbose_Mode and not Opt.Quiet_Output then
54          Write_Line ("building a library...");
55          Write_Str  ("   make ");
56          Write_Line (Output_File);
57       end if;
58
59       Ar (Output_Dir & Directory_Separator &
60           "lib" & Output_File & ".a", Objects => Ofiles);
61    end Build_Library;
62
63    ------------------------
64    -- Check_Library_Name --
65    ------------------------
66
67    procedure Check_Library_Name (Name : String) is
68    begin
69       if Name'Length = 0 then
70          Prj.Com.Fail ("library name cannot be empty");
71       end if;
72
73       if Name'Length > Max_Characters_In_Library_Name then
74          Prj.Com.Fail ("illegal library name """, Name, """: too long");
75       end if;
76
77       if not Is_Letter (Name (Name'First)) then
78          Prj.Com.Fail ("illegal library name """,
79                        Name,
80                        """: should start with a letter");
81       end if;
82
83       for Index in Name'Range loop
84          if not Is_Alphanumeric (Name (Index)) then
85             Prj.Com.Fail ("illegal library name """,
86                           Name,
87                           """: should include only letters and digits");
88          end if;
89       end loop;
90    end Check_Library_Name;
91
92    --------------------
93    -- Copy_ALI_Files --
94    --------------------
95
96    procedure Copy_ALI_Files
97      (Files      : Argument_List;
98       To         : Path_Name_Type;
99       Interfaces : String_List)
100    is
101       Success      : Boolean := False;
102       To_Dir       : constant String := Get_Name_String (To);
103       Is_Interface : Boolean := False;
104
105       procedure Verbose_Copy (Index : Positive);
106       --  In verbose mode, output a message that the indexed file is copied
107       --  to the destination directory.
108
109       ------------------
110       -- Verbose_Copy --
111       ------------------
112
113       procedure Verbose_Copy (Index : Positive) is
114       begin
115          if Opt.Verbose_Mode then
116             Write_Str ("Copying """);
117             Write_Str (Files (Index).all);
118             Write_Str (""" to """);
119             Write_Str (To_Dir);
120             Write_Line ("""");
121          end if;
122       end Verbose_Copy;
123
124    --  Start of processing for Copy_ALI_Files
125
126    begin
127       if Interfaces'Length = 0 then
128
129          --  If there are no Interfaces, copy all the ALI files as is
130
131          for Index in Files'Range loop
132             Verbose_Copy (Index);
133             Set_Writable
134               (To_Dir &
135                Directory_Separator &
136                Base_Name (Files (Index).all));
137             Copy_File
138               (Files (Index).all,
139                To_Dir,
140                Success,
141                Mode => Overwrite,
142                Preserve => Preserve);
143
144             exit when not Success;
145          end loop;
146
147       else
148          --  Copy only the interface ALI file, and put the special indicator
149          --  "SL" on the P line.
150
151          for Index in Files'Range loop
152
153             declare
154                File_Name : String := Base_Name (Files (Index).all);
155
156             begin
157                Canonical_Case_File_Name (File_Name);
158
159                --  Check if this is one of the interface ALIs
160
161                Is_Interface := False;
162
163                for Index in Interfaces'Range loop
164                   if File_Name = Interfaces (Index).all then
165                      Is_Interface := True;
166                      exit;
167                   end if;
168                end loop;
169
170                --  If it is an interface ALI, copy line by line. Insert
171                --  the interface indication at the end of the P line.
172                --  Do not copy ALI files that are not Interfaces.
173
174                if Is_Interface then
175                   Success := False;
176                   Verbose_Copy (Index);
177                   Set_Writable
178                     (To_Dir &
179                      Directory_Separator &
180                      Base_Name (Files (Index).all));
181
182                   declare
183                      FD           : File_Descriptor;
184                      Len          : Integer;
185                      Actual_Len   : Integer;
186                      S            : String_Access;
187                      Curr         : Natural;
188                      P_Line_Found : Boolean;
189                      Status       : Boolean;
190
191                   begin
192                      --  Open the file
193
194                      Name_Len := Files (Index)'Length;
195                      Name_Buffer (1 .. Name_Len) := Files (Index).all;
196                      Name_Len := Name_Len + 1;
197                      Name_Buffer (Name_Len) := ASCII.NUL;
198
199                      FD := Open_Read (Name_Buffer'Address, Binary);
200
201                      if FD /= Invalid_FD then
202                         Len := Integer (File_Length (FD));
203
204                         S := new String (1 .. Len + 3);
205
206                         --  Read the file. Note that the loop is not necessary
207                         --  since the whole file is read at once except on VMS.
208
209                         Curr := 1;
210                         Actual_Len := Len;
211
212                         while Actual_Len /= 0 loop
213                            Actual_Len := Read (FD, S (Curr)'Address, Len);
214                            Curr := Curr + Actual_Len;
215                         end loop;
216
217                         --  We are done with the input file, so we close it
218                         --  ignoring any bad status.
219
220                         Close (FD, Status);
221
222                         P_Line_Found := False;
223
224                         --  Look for the P line. When found, add marker SL
225                         --  at the beginning of the P line.
226
227                         for Index in 1 .. Len - 3 loop
228                            if (S (Index) = ASCII.LF or else
229                                  S (Index) = ASCII.CR)
230                              and then
231                                S (Index + 1) = 'P'
232                            then
233                               S (Index + 5 .. Len + 3) := S (Index + 2 .. Len);
234                               S (Index + 2 .. Index + 4) := " SL";
235                               P_Line_Found := True;
236                               exit;
237                            end if;
238                         end loop;
239
240                         if P_Line_Found then
241
242                            --  Create new modified ALI file
243
244                            Name_Len := To_Dir'Length;
245                            Name_Buffer (1 .. Name_Len) := To_Dir;
246                            Name_Len := Name_Len + 1;
247                            Name_Buffer (Name_Len) := Directory_Separator;
248                            Name_Buffer
249                              (Name_Len + 1 .. Name_Len + File_Name'Length) :=
250                                 File_Name;
251                            Name_Len := Name_Len + File_Name'Length + 1;
252                            Name_Buffer (Name_Len) := ASCII.NUL;
253
254                            FD := Create_File (Name_Buffer'Address, Binary);
255
256                            --  Write the modified text and close the newly
257                            --  created file.
258
259                            if FD /= Invalid_FD then
260                               Actual_Len := Write (FD, S (1)'Address, Len + 3);
261
262                               Close (FD, Status);
263
264                               --  Set Success to True only if the newly
265                               --  created file has been correctly written.
266
267                               Success := Status and Actual_Len = Len + 3;
268
269                               if Success then
270                                  Set_Read_Only (
271                                    Name_Buffer (1 .. Name_Len - 1));
272                               end if;
273                            end if;
274                         end if;
275                      end if;
276                   end;
277
278                --  This is not an interface ALI
279
280                else
281                   Success := True;
282                end if;
283             end;
284
285             if not Success then
286                Prj.Com.Fail ("could not copy ALI files to library dir");
287             end if;
288          end loop;
289       end if;
290    end Copy_ALI_Files;
291
292    ----------------------
293    -- Create_Sym_Links --
294    ----------------------
295
296    procedure Create_Sym_Links
297      (Lib_Path    : String;
298       Lib_Version : String;
299       Lib_Dir     : String;
300       Maj_Version : String)
301    is
302       function Symlink
303         (Oldpath : System.Address;
304          Newpath : System.Address) return Integer;
305       pragma Import (C, Symlink, "__gnat_symlink");
306
307       Success      : Boolean;
308       Version_Path : String_Access;
309
310       Result : Integer;
311       pragma Unreferenced (Result);
312
313    begin
314       if Is_Absolute_Path (Lib_Version) then
315          Version_Path := new String (1 .. Lib_Version'Length + 1);
316          Version_Path (1 .. Lib_Version'Length) := Lib_Version;
317
318       else
319          Version_Path :=
320            new String (1 .. Lib_Dir'Length + 1 + Lib_Version'Length + 1);
321          Version_Path (1 .. Version_Path'Last - 1) :=
322            Lib_Dir & Directory_Separator & Lib_Version;
323       end if;
324
325       Version_Path (Version_Path'Last) := ASCII.NUL;
326
327       if Maj_Version'Length = 0 then
328          declare
329             Newpath : String (1 .. Lib_Path'Length + 1);
330          begin
331             Newpath (1 .. Lib_Path'Length) := Lib_Path;
332             Newpath (Newpath'Last)         := ASCII.NUL;
333             Delete_File (Lib_Path, Success);
334             Result := Symlink (Version_Path (1)'Address, Newpath'Address);
335          end;
336
337       else
338          declare
339             Newpath1 : String (1 .. Lib_Path'Length + 1);
340             Maj_Path : constant String :=
341                          Lib_Dir & Directory_Separator & Maj_Version;
342             Newpath2 : String (1 .. Maj_Path'Length + 1);
343
344          begin
345             Newpath1 (1 .. Lib_Path'Length) := Lib_Path;
346             Newpath1 (Newpath1'Last)        := ASCII.NUL;
347
348             Newpath2 (1 .. Maj_Path'Length) := Maj_Path;
349             Newpath2 (Newpath2'Last)        := ASCII.NUL;
350
351             Delete_File (Maj_Path, Success);
352
353             Result := Symlink (Version_Path (1)'Address, Newpath2'Address);
354
355             Delete_File (Lib_Path, Success);
356
357             Result := Symlink (Newpath2'Address, Newpath1'Address);
358          end;
359       end if;
360    end Create_Sym_Links;
361
362    --------------------------------
363    -- Linker_Library_Path_Option --
364    --------------------------------
365
366    function Linker_Library_Path_Option return String_Access is
367
368       Run_Path_Option_Ptr : Interfaces.C.Strings.chars_ptr;
369       pragma Import (C, Run_Path_Option_Ptr, "__gnat_run_path_option");
370       --  Pointer to string representing the native linker option which
371       --  specifies the path where the dynamic loader should find shared
372       --  libraries. Equal to null string if this system doesn't support it.
373
374       S : constant String := Interfaces.C.Strings.Value (Run_Path_Option_Ptr);
375
376    begin
377       if S'Length = 0 then
378          return null;
379       else
380          return new String'(S);
381       end if;
382    end Linker_Library_Path_Option;
383
384    -------------------
385    -- Major_Id_Name --
386    -------------------
387
388    function Major_Id_Name
389      (Lib_Filename : String;
390       Lib_Version  : String)
391       return String
392    is
393       Maj_Version : constant String := Lib_Version;
394       Last_Maj    : Positive;
395       Last        : Positive;
396       Ok_Maj      : Boolean := False;
397
398    begin
399       Last_Maj := Maj_Version'Last;
400       while Last_Maj > Maj_Version'First loop
401          if Maj_Version (Last_Maj) in '0' .. '9' then
402             Last_Maj := Last_Maj - 1;
403
404          else
405             Ok_Maj := Last_Maj /= Maj_Version'Last and then
406             Maj_Version (Last_Maj) = '.';
407
408             if Ok_Maj then
409                Last_Maj := Last_Maj - 1;
410             end if;
411
412             exit;
413          end if;
414       end loop;
415
416       if Ok_Maj then
417          Last := Last_Maj;
418          while Last > Maj_Version'First loop
419             if Maj_Version (Last) in '0' .. '9' then
420                Last := Last - 1;
421
422             else
423                Ok_Maj := Last /= Last_Maj and then
424                Maj_Version (Last) = '.';
425
426                if Ok_Maj then
427                   Last := Last - 1;
428                   Ok_Maj :=
429                     Maj_Version (Maj_Version'First .. Last) = Lib_Filename;
430                end if;
431
432                exit;
433             end if;
434          end loop;
435       end if;
436
437       if Ok_Maj then
438          return Maj_Version (Maj_Version'First .. Last_Maj);
439       else
440          return "";
441       end if;
442    end Major_Id_Name;
443
444 --  Package elaboration
445
446 begin
447    --  Copy_Attributes always fails on VMS
448
449    if Hostparm.OpenVMS then
450       Preserve := None;
451    end if;
452 end MLib;