OSDN Git Service

2007-04-20 Vincent Celier <celier@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
30 with Hostparm;
31 with Opt;
32 with Output; use Output;
33
34 with MLib.Utl; use MLib.Utl;
35
36 with Prj.Com;
37
38 with GNAT.Directory_Operations; use GNAT.Directory_Operations;
39
40 package body MLib is
41
42    -------------------
43    -- Build_Library --
44    -------------------
45
46    procedure Build_Library
47      (Ofiles      : Argument_List;
48       Afiles      : Argument_List;
49       Output_File : String;
50       Output_Dir  : String)
51    is
52       pragma Warnings (Off, Afiles);
53
54    begin
55       if Opt.Verbose_Mode and not Opt.Quiet_Output then
56          Write_Line ("building a library...");
57          Write_Str  ("   make ");
58          Write_Line (Output_File);
59       end if;
60
61       Ar (Output_Dir & Directory_Separator &
62           "lib" & Output_File & ".a", Objects => Ofiles);
63    end Build_Library;
64
65    ------------------------
66    -- Check_Library_Name --
67    ------------------------
68
69    procedure Check_Library_Name (Name : String) is
70    begin
71       if Name'Length = 0 then
72          Prj.Com.Fail ("library name cannot be empty");
73       end if;
74
75       if Name'Length > Max_Characters_In_Library_Name then
76          Prj.Com.Fail ("illegal library name """, Name, """: too long");
77       end if;
78
79       if not Is_Letter (Name (Name'First)) then
80          Prj.Com.Fail ("illegal library name """,
81                        Name,
82                        """: should start with a letter");
83       end if;
84
85       for Index in Name'Range loop
86          if not Is_Alphanumeric (Name (Index)) then
87             Prj.Com.Fail ("illegal library name """,
88                           Name,
89                           """: should include only letters and digits");
90          end if;
91       end loop;
92    end Check_Library_Name;
93
94    --------------------
95    -- Copy_ALI_Files --
96    --------------------
97
98    procedure Copy_ALI_Files
99      (Files      : Argument_List;
100       To         : Path_Name_Type;
101       Interfaces : String_List)
102    is
103       Success      : Boolean := False;
104       To_Dir       : constant String := Get_Name_String (To);
105       Is_Interface : Boolean := False;
106
107       procedure Verbose_Copy (Index : Positive);
108       --  In verbose mode, output a message that the indexed file is copied
109       --  to the destination directory.
110
111       ------------------
112       -- Verbose_Copy --
113       ------------------
114
115       procedure Verbose_Copy (Index : Positive) is
116       begin
117          if Opt.Verbose_Mode then
118             Write_Str ("Copying """);
119             Write_Str (Files (Index).all);
120             Write_Str (""" to """);
121             Write_Str (To_Dir);
122             Write_Line ("""");
123          end if;
124       end Verbose_Copy;
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             begin
156                Canonical_Case_File_Name (File_Name);
157
158                --  Check if this is one of the interface ALIs
159
160                Is_Interface := False;
161
162                for Index in Interfaces'Range loop
163                   if File_Name = Interfaces (Index).all then
164                      Is_Interface := True;
165                      exit;
166                   end if;
167                end loop;
168
169                --  If it is an interface ALI, copy line by line. Insert
170                --  the interface indication at the end of the P line.
171                --  Do not copy ALI files that are not Interfaces.
172
173                if Is_Interface then
174                   Success := False;
175                   Verbose_Copy (Index);
176                   Set_Writable
177                     (To_Dir &
178                      Directory_Separator &
179                      Base_Name (Files (Index).all));
180
181                   declare
182                      FD           : File_Descriptor;
183                      Len          : Integer;
184                      Actual_Len   : Integer;
185                      S            : String_Access;
186                      Curr         : Natural;
187                      P_Line_Found : Boolean;
188                      Status       : Boolean;
189
190                   begin
191                      --  Open the file
192
193                      Name_Len := Files (Index)'Length;
194                      Name_Buffer (1 .. Name_Len) := Files (Index).all;
195                      Name_Len := Name_Len + 1;
196                      Name_Buffer (Name_Len) := ASCII.NUL;
197
198                      FD := Open_Read (Name_Buffer'Address, Binary);
199
200                      if FD /= Invalid_FD then
201                         Len := Integer (File_Length (FD));
202
203                         S := new String (1 .. Len + 3);
204
205                         --  Read the file. Note that the loop is not necessary
206                         --  since the whole file is read at once except on VMS.
207
208                         Curr := 1;
209                         Actual_Len := Len;
210
211                         while Actual_Len /= 0 loop
212                            Actual_Len := Read (FD, S (Curr)'Address, Len);
213                            Curr := Curr + Actual_Len;
214                         end loop;
215
216                         --  We are done with the input file, so we close it
217
218                         Close (FD, Status);
219                         --  We simply ignore any bad status
220
221                         P_Line_Found := False;
222
223                         --  Look for the P line. When found, add marker SL
224                         --  at the beginning of the P line.
225
226                         for Index in 1 .. Len - 3 loop
227                            if (S (Index) = ASCII.LF or else
228                                  S (Index) = ASCII.CR)
229                              and then
230                                S (Index + 1) = 'P'
231                            then
232                               S (Index + 5 .. Len + 3) := S (Index + 2 .. Len);
233                               S (Index + 2 .. Index + 4) := " SL";
234                               P_Line_Found := True;
235                               exit;
236                            end if;
237                         end loop;
238
239                         if P_Line_Found then
240
241                            --  Create new modified ALI file
242
243                            Name_Len := To_Dir'Length;
244                            Name_Buffer (1 .. Name_Len) := To_Dir;
245                            Name_Len := Name_Len + 1;
246                            Name_Buffer (Name_Len) := Directory_Separator;
247                            Name_Buffer
248                              (Name_Len + 1 .. Name_Len + File_Name'Length) :=
249                                 File_Name;
250                            Name_Len := Name_Len + File_Name'Length + 1;
251                            Name_Buffer (Name_Len) := ASCII.NUL;
252
253                            FD := Create_File (Name_Buffer'Address, Binary);
254
255                            --  Write the modified text and close the newly
256                            --  created file.
257
258                            if FD /= Invalid_FD then
259                               Actual_Len := Write (FD, S (1)'Address, Len + 3);
260
261                               Close (FD, Status);
262
263                               --  Set Success to True only if the newly
264                               --  created file has been correctly written.
265
266                               Success := Status and Actual_Len = Len + 3;
267
268                               if Success then
269                                  Set_Read_Only (
270                                    Name_Buffer (1 .. Name_Len - 1));
271                               end if;
272                            end if;
273                         end if;
274                      end if;
275                   end;
276
277                else
278                   --  This is not an interface ALI
279
280                   Success := True;
281
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    -- Linker_Library_Path_Option --
294    --------------------------------
295
296    function Linker_Library_Path_Option return String_Access is
297
298       Run_Path_Option_Ptr : Interfaces.C.Strings.chars_ptr;
299       pragma Import (C, Run_Path_Option_Ptr, "__gnat_run_path_option");
300       --  Pointer to string representing the native linker option which
301       --  specifies the path where the dynamic loader should find shared
302       --  libraries. Equal to null string if this system doesn't support it.
303
304       S : constant String := Interfaces.C.Strings.Value (Run_Path_Option_Ptr);
305
306    begin
307       if S'Length = 0 then
308          return null;
309       else
310          return new String'(S);
311       end if;
312    end Linker_Library_Path_Option;
313
314 --  Package elaboration
315
316 begin
317    --  Copy_Attributes always fails on VMS
318
319    if Hostparm.OpenVMS then
320       Preserve := None;
321    end if;
322 end MLib;