1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 2002-2013, 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 MLib
.Fil
; use MLib
.Fil
;
27 with MLib
.Tgt
; use MLib
.Tgt
;
30 with Output
; use Output
;
32 with Interfaces
.C
.Strings
; use Interfaces
.C
.Strings
;
34 package body MLib
.Utl
is
36 Adalib_Path
: String_Access
:= null;
37 -- Path of the GNAT adalib directory, specified in procedure
38 -- Specify_Adalib_Dir. Used in function Lib_Directory.
40 Gcc_Name
: String_Access
;
41 -- Default value of the "gcc" executable used in procedure Gcc
43 Gcc_Exec
: String_Access
;
44 -- The full path name of the "gcc" executable
46 Ar_Name
: String_Access
;
47 -- The name of the archive builder for the platform, set when procedure Ar
48 -- is called for the first time.
50 Ar_Exec
: String_Access
;
51 -- The full path name of the archive builder
53 Ar_Options
: String_List_Access
;
54 -- The minimum options used when invoking the archive builder
56 Ar_Append_Options
: String_List_Access
;
57 -- The options to be used when invoking the archive builder to add chunks
58 -- of object files, when building the archive in chunks.
60 Opt_Length
: Natural := 0;
61 -- The max number of options for the Archive_Builder
63 Initial_Size
: Natural := 0;
64 -- The minimum number of bytes for the invocation of the Archive Builder
65 -- (without name of the archive or object files).
67 Ranlib_Name
: String_Access
;
68 -- The name of the archive indexer for the platform, if there is one
70 Ranlib_Exec
: String_Access
:= null;
71 -- The full path name of the archive indexer
73 Ranlib_Options
: String_List_Access
:= null;
74 -- The options to be used when invoking the archive indexer, if any
80 procedure Ar
(Output_File
: String; Objects
: Argument_List
) is
81 Full_Output_File
: constant String :=
82 Ext_To
(Output_File
, Archive_Ext
);
84 Arguments
: Argument_List_Access
;
85 Last_Arg
: Natural := 0;
87 Line_Length
: Natural := 0;
89 Maximum_Size
: Integer;
90 pragma Import
(C
, Maximum_Size
, "__gnat_link_max");
91 -- Maximum number of bytes to put in an invocation of the
95 -- The number of bytes for the invocation of the archive builder
97 Current_Object
: Natural;
100 -- Display an invocation of the Archive Builder
108 if not Opt
.Quiet_Output
then
109 Write_Str
(Ar_Name
.all);
110 Line_Length
:= Ar_Name
'Length;
112 for J
in 1 .. Last_Arg
loop
114 -- Make sure the Output buffer does not overflow
116 if Line_Length
+ 1 + Arguments
(J
)'Length > Buffer_Max
then
123 -- Only output the first object files when not in verbose mode
125 if (not Opt
.Verbose_Mode
) and then J
= Opt_Length
+ 3 then
130 Write_Str
(Arguments
(J
).all);
131 Line_Length
:= Line_Length
+ 1 + Arguments
(J
)'Length;
140 if Ar_Exec
= null then
141 Ar_Name
:= Osint
.Program_Name
(Archive_Builder
, "gnatmake");
142 Ar_Exec
:= Locate_Exec_On_Path
(Ar_Name
.all);
144 if Ar_Exec
= null then
146 Ar_Name
:= new String'(Archive_Builder);
147 Ar_Exec := Locate_Exec_On_Path (Ar_Name.all);
150 if Ar_Exec = null then
151 Fail (Ar_Name.all & " not found in path");
153 elsif Opt.Verbose_Mode then
154 Write_Str ("found ");
155 Write_Line (Ar_Exec.all);
158 Ar_Options := Archive_Builder_Options;
161 for J in Ar_Options'Range loop
162 Initial_Size := Initial_Size + Ar_Options (J)'Length + 1;
165 Ar_Append_Options := Archive_Builder_Append_Options;
167 Opt_Length := Ar_Options'Length;
169 if Ar_Append_Options /= null then
170 Opt_Length := Natural'Max (Ar_Append_Options'Length, Opt_Length);
173 for J in Ar_Append_Options'Range loop
174 Size := Size + Ar_Append_Options (J)'Length + 1;
177 Initial_Size := Integer'Max (Initial_Size, Size);
182 Ranlib_Name := Osint.Program_Name (Archive_Indexer, "gnatmake");
184 if Ranlib_Name'Length > 0 then
185 Ranlib_Exec := Locate_Exec_On_Path (Ranlib_Name.all);
187 if Ranlib_Exec = null then
189 Ranlib_Name := new String'(Archive_Indexer
);
190 Ranlib_Exec
:= Locate_Exec_On_Path
(Ranlib_Name
.all);
193 if Ranlib_Exec
/= null and then Opt
.Verbose_Mode
then
194 Write_Str
("found ");
195 Write_Line
(Ranlib_Exec
.all);
199 Ranlib_Options
:= Archive_Indexer_Options
;
203 new String_List
(1 .. 1 + Opt_Length
+ Objects
'Length);
204 Arguments
(1 .. Ar_Options
'Length) := Ar_Options
.all; -- "ar cr ..."
205 Arguments
(Ar_Options
'Length + 1) := new String'(Full_Output_File);
207 Delete_File (Full_Output_File);
209 Size := Initial_Size + Full_Output_File'Length + 1;
211 -- Check the full size of a call of the archive builder with all the
214 for J in Objects'Range loop
215 Size := Size + Objects (J)'Length + 1;
218 -- If the size is not too large or if it is not possible to build the
219 -- archive in chunks, build the archive in a single invocation.
221 if Size <= Maximum_Size or else Ar_Append_Options = null then
222 Last_Arg := Ar_Options'Length + 1 + Objects'Length;
223 Arguments (Ar_Options'Length + 2 .. Last_Arg) := Objects;
227 Spawn (Ar_Exec.all, Arguments (1 .. Last_Arg), Success);
230 -- Build the archive in several invocation, making sure to not
231 -- go over the maximum size for each invocation.
233 Last_Arg := Ar_Options'Length + 1;
234 Current_Object := Objects'First;
235 Size := Initial_Size + Full_Output_File'Length + 1;
239 while Current_Object <= Objects'Last loop
240 Size := Size + Objects (Current_Object)'Length + 1;
241 exit when Size > Maximum_Size;
242 Last_Arg := Last_Arg + 1;
243 Arguments (Last_Arg) := Objects (Current_Object);
244 Current_Object := Current_Object + 1;
249 Spawn (Ar_Exec.all, Arguments (1 .. Last_Arg), Success);
251 Arguments (1 .. Ar_Append_Options'Length) := Ar_Append_Options.all;
253 (Ar_Append_Options'Length + 1) := new String'(Full_Output_File
);
255 -- Appending invocation(s)
257 Big_Loop
: while Success
and then Current_Object
<= Objects
'Last loop
258 Last_Arg
:= Ar_Append_Options
'Length + 1;
259 Size
:= Initial_Size
+ Full_Output_File
'Length + 1;
261 Inner_Loop
: while Current_Object
<= Objects
'Last loop
262 Size
:= Size
+ Objects
(Current_Object
)'Length + 1;
263 exit Inner_Loop
when Size
> Maximum_Size
;
264 Last_Arg
:= Last_Arg
+ 1;
265 Arguments
(Last_Arg
) := Objects
(Current_Object
);
266 Current_Object
:= Current_Object
+ 1;
271 Spawn
(Ar_Exec
.all, Arguments
(1 .. Last_Arg
), Success
);
276 Fail
(Ar_Name
.all & " execution error.");
279 -- If we have found ranlib, run it over the library
281 if Ranlib_Exec
/= null then
282 if not Opt
.Quiet_Output
then
283 Write_Str
(Ranlib_Name
.all);
285 for J
in Ranlib_Options
'Range loop
286 Write_Str
(Ranlib_Options
(J
).all);
289 Write_Line
(Arguments
(Ar_Options
'Length + 1).all);
294 Ranlib_Options
.all & (Arguments
(Ar_Options
'Length + 1)),
298 Fail
(Ranlib_Name
.all & " execution error.");
307 procedure Delete_File
(Filename
: String) is
308 File
: constant String := Filename
& ASCII
.NUL
;
312 Delete_File
(File
'Address, Success
);
314 if Opt
.Verbose_Mode
then
316 Write_Str
("deleted ");
319 Write_Str
("could not delete ");
322 Write_Line
(Filename
);
331 (Output_File
: String;
332 Objects
: Argument_List
;
333 Options
: Argument_List
;
334 Options_2
: Argument_List
;
335 Driver_Name
: Name_Id
:= No_Name
)
337 Link_Bytes
: Integer := 0;
338 -- Projected number of bytes for the linker command line
341 pragma Import
(C
, Link_Max
, "__gnat_link_max");
342 -- Maximum number of bytes on the command line supported by the OS
343 -- linker. Passed this limit the response file mechanism must be used
346 Object_List_File_Supported
: Boolean;
347 for Object_List_File_Supported
'Size use Character'Size;
349 (C
, Object_List_File_Supported
, "__gnat_objlist_file_supported");
350 -- Predicate indicating whether the linker has an option whereby the
351 -- names of object files can be passed to the linker in a file.
353 Object_File_Option_Ptr
: Interfaces
.C
.Strings
.chars_ptr
;
354 pragma Import
(C
, Object_File_Option_Ptr
, "__gnat_object_file_option");
355 -- Pointer to a string representing the linker option which specifies
356 -- the response file.
358 Object_File_Option
: constant String := Value
(Object_File_Option_Ptr
);
359 -- The linker option which specifies the response file as a string
361 Using_GNU_response_file
: constant Boolean :=
362 Object_File_Option
'Length > 0
365 (Object_File_Option
'Last) = '@';
366 -- Whether a GNU response file is used
368 Tname
: String_Access
;
369 Tname_FD
: File_Descriptor
:= Invalid_FD
;
370 -- Temporary file used by linker to pass list of object files on
371 -- certain systems with limitations on size of arguments.
373 Closing_Status
: Boolean;
378 (1 .. 7 + Objects
'Length + Options
'Length + Options_2
'Length);
383 Out_Opt
: constant String_Access
:= new String'("-o");
384 Out_V : constant String_Access := new String'(Output_File
);
385 Lib_Dir
: constant String_Access
:= new String'("-L" & Lib_Directory);
386 Lib_Opt : constant String_Access := new String'(Dynamic_Option
);
388 Driver
: String_Access
;
390 type Object_Position
is (First
, Second
, Last
);
392 Position
: Object_Position
;
394 procedure Write_RF
(S
: String);
395 -- Write a string to the response file and check if it was successful.
396 -- Fail the program if it was not successful (disk full).
402 procedure Write_RF
(S
: String) is
403 Success
: Boolean := True;
404 Back_Slash
: constant Character := '\';
407 -- If a GNU response file is used, space and backslash need to be
408 -- escaped because they are interpreted as a string separator and
409 -- an escape character respectively by the underlying mechanism.
410 -- On the other hand, quote and double-quote are not escaped since
411 -- they are interpreted as string delimiters on both sides.
413 if Using_GNU_response_file
then
414 for J
in S
'Range loop
415 if S
(J
) = ' ' or else S
(J
) = '\' then
416 if Write
(Tname_FD
, Back_Slash
'Address, 1) /= 1 then
421 if Write
(Tname_FD
, S
(J
)'Address, 1) /= 1 then
427 if Write
(Tname_FD
, S
'Address, S
'Length) /= S
'Length then
432 if Write
(Tname_FD
, ASCII
.LF
'Address, 1) /= 1 then
437 Fail
("cannot generate response file to link library: disk full");
441 -- Start of processing for Gcc
444 if Driver_Name
= No_Name
then
445 if Gcc_Exec
= null then
446 if Gcc_Name
= null then
447 Gcc_Name
:= Osint
.Program_Name
("gcc", "gnatmake");
450 Gcc_Exec
:= Locate_Exec_On_Path
(Gcc_Name
.all);
452 if Gcc_Exec
= null then
453 Fail
(Gcc_Name
.all & " not found in path");
460 Driver
:= Locate_Exec_On_Path
(Get_Name_String
(Driver_Name
));
462 if Driver
= null then
463 Fail
(Get_Name_String
(Driver_Name
) & " not found in path");
469 if Lib_Opt
'Length /= 0 then
471 Arguments
(A
) := Lib_Opt
;
472 Link_Bytes
:= Link_Bytes
+ Lib_Opt
'Length + 1;
476 Arguments
(A
) := Out_Opt
;
477 Link_Bytes
:= Link_Bytes
+ Out_Opt
'Length + 1;
480 Arguments
(A
) := Out_V
;
481 Link_Bytes
:= Link_Bytes
+ Out_V
'Length + 1;
484 Arguments
(A
) := Lib_Dir
;
485 Link_Bytes
:= Link_Bytes
+ Lib_Dir
'Length + 1;
487 A
:= A
+ Options
'Length;
488 Arguments
(A
- Options
'Length + 1 .. A
) := Options
;
490 for J
in Options
'Range loop
491 Link_Bytes
:= Link_Bytes
+ Options
(J
)'Length + 1;
494 if not Opt
.Quiet_Output
then
495 if Opt
.Verbose_Mode
then
496 Write_Str
(Driver
.all);
498 elsif Driver_Name
/= No_Name
then
499 Write_Str
(Get_Name_String
(Driver_Name
));
502 Write_Str
(Gcc_Name
.all);
506 if Opt
.Verbose_Mode
or else J
< 4 then
508 Write_Str
(Arguments
(J
).all);
516 -- Do not display all the object files if not in verbose mode, only
520 for J
in Objects
'Range loop
521 if Opt
.Verbose_Mode
or else Position
= First
then
523 Write_Str
(Objects
(J
).all);
526 elsif Position
= Second
then
533 for J
in Options_2
'Range loop
534 if not Opt
.Verbose_Mode
then
535 if Position
= Second
then
543 Write_Str
(Options_2
(J
).all);
549 for J
in Objects
'Range loop
550 Link_Bytes
:= Link_Bytes
+ Objects
(J
)'Length + 1;
553 for J
in Options_2
'Range loop
554 Link_Bytes
:= Link_Bytes
+ Options_2
(J
)'Length + 1;
557 if Object_List_File_Supported
and then Link_Bytes
> Link_Max
then
559 -- Create a temporary file containing the object files, one object
560 -- file per line for maximal compatibility with linkers supporting
563 Create_Temp_File
(Tname_FD
, Tname
);
565 for J
in Objects
'Range loop
566 Write_RF
(Objects
(J
).all);
569 Close
(Tname_FD
, Closing_Status
);
571 if not Closing_Status
then
572 Fail
("cannot generate response file to link library: disk full");
576 Arguments
(A
) := new String'(Object_File_Option & Tname.all);
579 A := A + Objects'Length;
580 Arguments (A - Objects'Length + 1 .. A) := Objects;
583 A := A + Options_2'Length;
584 Arguments (A - Options_2'Length + 1 .. A) := Options_2;
586 Spawn (Driver.all, Arguments (1 .. A), Success);
589 -- Delete the temporary file used in conjunction with linking
590 -- if one was created.
592 if Tname_FD /= Invalid_FD then
593 Delete_File (Tname.all);
597 if Driver_Name = No_Name then
598 Fail (Gcc_Name.all & " execution error");
600 Fail (Get_Name_String (Driver_Name) & " execution error");
609 function Lib_Directory return String is
610 Libgnat : constant String := Tgt.Libgnat;
613 -- If procedure Specify_Adalib_Dir has been called, used the specified
616 if Adalib_Path /= null then
617 return Adalib_Path.all;
620 Name_Len := Libgnat'Length;
621 Name_Buffer (1 .. Name_Len) := Libgnat;
622 Get_Name_String (Osint.Find_File (Name_Enter, Osint.Library));
626 return Name_Buffer (1 .. Name_Len - Libgnat'Length);
629 ------------------------
630 -- Specify_Adalib_Dir --
631 ------------------------
633 procedure Specify_Adalib_Dir (Path : String) is
635 if Path'Length = 0 then
638 Adalib_Path := new String'(Path
);
640 end Specify_Adalib_Dir
;