1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1999-2014, 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
;
31 with Output
; use Output
;
33 with MLib
.Utl
; use MLib
.Utl
;
37 with GNAT
.Directory_Operations
; use GNAT
.Directory_Operations
;
45 procedure Build_Library
46 (Ofiles
: Argument_List
;
51 if Opt
.Verbose_Mode
and not Opt
.Quiet_Output
then
52 Write_Line
("building a library...");
54 Write_Line
(Output_File
);
58 "lib" & Output_File
& ".a", Objects
=> Ofiles
);
61 ------------------------
62 -- Check_Library_Name --
63 ------------------------
65 procedure Check_Library_Name
(Name
: String) is
67 if Name
'Length = 0 then
68 Prj
.Com
.Fail
("library name cannot be empty");
71 if Name
'Length > Max_Characters_In_Library_Name
then
72 Prj
.Com
.Fail
("illegal library name """
77 if not Is_Letter
(Name
(Name
'First)) then
78 Prj
.Com
.Fail
("illegal library name """
80 & """: should start with a letter");
83 for Index
in Name
'Range loop
84 if not Is_Alphanumeric
(Name
(Index
)) then
85 Prj
.Com
.Fail
("illegal library name """
87 & """: should include only letters and digits");
90 end Check_Library_Name
;
96 procedure Copy_ALI_Files
97 (Files
: Argument_List
;
99 Interfaces
: String_List
)
101 Success
: Boolean := False;
102 To_Dir
: constant String := Get_Name_String
(To
);
103 Is_Interface
: Boolean := False;
105 procedure Verbose_Copy
(Index
: Positive);
106 -- In verbose mode, output a message that the indexed file is copied
107 -- to the destination directory.
113 procedure Verbose_Copy
(Index
: Positive) is
115 if Opt
.Verbose_Mode
then
116 Write_Str
("Copying """);
117 Write_Str
(Files
(Index
).all);
118 Write_Str
(""" to """);
124 -- Start of processing for Copy_ALI_Files
127 if Interfaces
'Length = 0 then
129 -- If there are no Interfaces, copy all the ALI files as is
131 for Index
in Files
'Range loop
132 Verbose_Copy
(Index
);
135 Directory_Separator
&
136 Base_Name
(Files
(Index
).all));
142 Preserve
=> Preserve
);
144 exit when not Success
;
148 -- Copy only the interface ALI file, and put the special indicator
149 -- "SL" on the P line.
151 for Index
in Files
'Range loop
154 File_Name
: String := Base_Name
(Files
(Index
).all);
157 Canonical_Case_File_Name
(File_Name
);
159 -- Check if this is one of the interface ALIs
161 Is_Interface
:= False;
163 for Index
in Interfaces
'Range loop
164 if File_Name
= Interfaces
(Index
).all then
165 Is_Interface
:= True;
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.
176 Verbose_Copy
(Index
);
179 Directory_Separator
&
180 Base_Name
(Files
(Index
).all));
183 FD
: File_Descriptor
;
185 Actual_Len
: Integer;
188 P_Line_Found
: Boolean;
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
;
199 FD
:= Open_Read
(Name_Buffer
'Address, Binary
);
201 if FD
/= Invalid_FD
then
202 Len
:= Integer (File_Length
(FD
));
206 S
:= new String (1 .. Len
+ 3);
208 -- Read the file. This loop is probably not necessary
209 -- since on most (all?) targets, the whole file is
210 -- read in at once, but we have encountered systems
211 -- in the past where this was not true, and we retain
212 -- this loop in case we encounter that in the future.
215 while Curr
<= Len
loop
216 Actual_Len
:= Read
(FD
, S
(Curr
)'Address, Len
);
218 -- Exit if we could not read for some reason
220 exit when Actual_Len
= 0;
222 Curr
:= Curr
+ Actual_Len
;
225 -- We are done with the input file, so we close it
226 -- ignoring any bad status.
230 P_Line_Found
:= False;
232 -- Look for the P line. When found, add marker SL
233 -- at the beginning of the P line.
235 for Index
in 1 .. Len
- 3 loop
236 if (S
(Index
) = ASCII
.LF
238 S
(Index
) = ASCII
.CR
)
239 and then S
(Index
+ 1) = 'P'
241 S
(Index
+ 5 .. Len
+ 3) := S
(Index
+ 2 .. Len
);
242 S
(Index
+ 2 .. Index
+ 4) := " SL";
243 P_Line_Found
:= True;
250 -- Create new modified ALI file
252 Name_Len
:= To_Dir
'Length;
253 Name_Buffer
(1 .. Name_Len
) := To_Dir
;
254 Name_Len
:= Name_Len
+ 1;
255 Name_Buffer
(Name_Len
) := Directory_Separator
;
257 (Name_Len
+ 1 .. Name_Len
+ File_Name
'Length) :=
259 Name_Len
:= Name_Len
+ File_Name
'Length + 1;
260 Name_Buffer
(Name_Len
) := ASCII
.NUL
;
262 FD
:= Create_File
(Name_Buffer
'Address, Binary
);
264 -- Write the modified text and close the newly
267 if FD
/= Invalid_FD
then
268 Actual_Len
:= Write
(FD
, S
(1)'Address, Len
+ 3);
272 -- Set Success to True only if the newly
273 -- created file has been correctly written.
275 Success
:= Status
and then Actual_Len
= Len
+ 3;
279 -- Set_Read_Only is used here, rather than
280 -- Set_Non_Writable, so that gprbuild can
281 -- he compiled with older compilers.
284 (Name_Buffer
(1 .. Name_Len
- 1));
291 -- This is not an interface ALI
299 Prj
.Com
.Fail
("could not copy ALI files to library dir");
305 ----------------------
306 -- Create_Sym_Links --
307 ----------------------
309 procedure Create_Sym_Links
311 Lib_Version
: String;
313 Maj_Version
: String)
316 (Oldpath
: System
.Address
;
317 Newpath
: System
.Address
) return Integer;
318 pragma Import
(C
, Symlink
, "__gnat_symlink");
320 Version_Path
: String_Access
;
324 pragma Unreferenced
(Success
, Result
);
327 Version_Path
:= new String (1 .. Lib_Version
'Length + 1);
328 Version_Path
(1 .. Lib_Version
'Length) := Lib_Version
;
329 Version_Path
(Version_Path
'Last) := ASCII
.NUL
;
331 if Maj_Version
'Length = 0 then
333 Newpath
: String (1 .. Lib_Path
'Length + 1);
335 Newpath
(1 .. Lib_Path
'Length) := Lib_Path
;
336 Newpath
(Newpath
'Last) := ASCII
.NUL
;
337 Delete_File
(Lib_Path
, Success
);
338 Result
:= Symlink
(Version_Path
(1)'Address, Newpath
'Address);
343 Newpath1
: String (1 .. Lib_Path
'Length + 1);
344 Maj_Path
: constant String :=
345 Lib_Dir
& Directory_Separator
& Maj_Version
;
346 Newpath2
: String (1 .. Maj_Path
'Length + 1);
347 Maj_Ver
: String (1 .. Maj_Version
'Length + 1);
350 Newpath1
(1 .. Lib_Path
'Length) := Lib_Path
;
351 Newpath1
(Newpath1
'Last) := ASCII
.NUL
;
353 Newpath2
(1 .. Maj_Path
'Length) := Maj_Path
;
354 Newpath2
(Newpath2
'Last) := ASCII
.NUL
;
356 Maj_Ver
(1 .. Maj_Version
'Length) := Maj_Version
;
357 Maj_Ver
(Maj_Ver
'Last) := ASCII
.NUL
;
359 Delete_File
(Maj_Path
, Success
);
361 Result
:= Symlink
(Version_Path
(1)'Address, Newpath2
'Address);
363 Delete_File
(Lib_Path
, Success
);
365 Result
:= Symlink
(Maj_Ver
'Address, Newpath1
'Address);
368 end Create_Sym_Links
;
370 --------------------------------
371 -- Linker_Library_Path_Option --
372 --------------------------------
374 function Linker_Library_Path_Option
return String_Access
is
376 Run_Path_Option_Ptr
: Interfaces
.C
.Strings
.chars_ptr
;
377 pragma Import
(C
, Run_Path_Option_Ptr
, "__gnat_run_path_option");
378 -- Pointer to string representing the native linker option which
379 -- specifies the path where the dynamic loader should find shared
380 -- libraries. Equal to null string if this system doesn't support it.
382 S
: constant String := Interfaces
.C
.Strings
.Value
(Run_Path_Option_Ptr
);
388 return new String'(S);
390 end Linker_Library_Path_Option;
396 function Major_Id_Name
397 (Lib_Filename : String;
398 Lib_Version : String)
401 Maj_Version : constant String := Lib_Version;
404 Ok_Maj : Boolean := False;
407 Last_Maj := Maj_Version'Last;
408 while Last_Maj > Maj_Version'First loop
409 if Maj_Version (Last_Maj) in '0' .. '9' then
410 Last_Maj := Last_Maj - 1;
413 Ok_Maj := Last_Maj /= Maj_Version'Last and then
414 Maj_Version (Last_Maj) = '.';
417 Last_Maj := Last_Maj - 1;
426 while Last > Maj_Version'First loop
427 if Maj_Version (Last) in '0' .. '9' then
431 Ok_Maj := Last /= Last_Maj and then
432 Maj_Version (Last) = '.';
437 Maj_Version (Maj_Version'First .. Last) = Lib_Filename;
446 return Maj_Version (Maj_Version'First .. Last_Maj);
452 -------------------------------
453 -- Separate_Run_Path_Options --
454 -------------------------------
456 function Separate_Run_Path_Options return Boolean is
457 Separate_Paths : Boolean;
458 for Separate_Paths'Size use Character'Size;
459 pragma Import (C, Separate_Paths, "__gnat_separate_run_path_options");
461 return Separate_Paths;
462 end Separate_Run_Path_Options;