OSDN Git Service

2004-10-04 Vincent Celier <celier@gnat.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / mlib-tgt-vms-ia64.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             M L I B . T G T                              --
6 --                         (Integrity VMS Version)                          --
7 --                                                                          --
8 --                                 B o d y                                  --
9 --                                                                          --
10 --              Copyright (C) 2004, Free Software Foundation, Inc.          --
11 --                                                                          --
12 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
13 -- terms of the  GNU General Public License as published  by the Free Soft- --
14 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
15 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
16 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
17 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
18 -- for  more details.  You should have  received  a copy of the GNU General --
19 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
20 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
21 -- MA 02111-1307, USA.                                                      --
22 --                                                                          --
23 -- GNAT was originally developed  by the GNAT team at  New York University. --
24 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
25 --                                                                          --
26 ------------------------------------------------------------------------------
27
28 --  This is the Integrity VMS version of the body
29
30 with Ada.Characters.Handling; use Ada.Characters.Handling;
31
32 with GNAT.Directory_Operations;  use GNAT.Directory_Operations;
33 with GNAT.OS_Lib;                use GNAT.OS_Lib;
34
35 with MLib.Fil;
36 with MLib.Utl;
37 with Namet;             use Namet;
38 with Opt;               use Opt;
39 with Output;            use Output;
40 with Prj.Com;
41 with System;            use System;
42 with System.Case_Util;  use System.Case_Util;
43
44 package body MLib.Tgt is
45
46    use GNAT;
47
48    Empty_Argument_List : aliased Argument_List := (1 .. 0 => null);
49    Additional_Objects  : Argument_List_Access := Empty_Argument_List'Access;
50    --  Used to add the generated auto-init object files for auto-initializing
51    --  stand-alone libraries.
52
53    Macro_Name   : constant String := "mcr gnu:[bin]gcc -c -x assembler";
54    --  The name of the command to invoke the macro-assembler
55
56    VMS_Options : Argument_List := (1 .. 1 => null);
57
58    Gnatsym_Name : constant String := "gnatsym";
59
60    Gnatsym_Path : String_Access;
61
62    Arguments : Argument_List_Access := null;
63    Last_Argument : Natural := 0;
64
65    Success : Boolean := False;
66
67    Shared_Libgcc : aliased String := "-shared-libgcc";
68
69    No_Shared_Libgcc_Switch : aliased Argument_List := (1 .. 0 => null);
70    Shared_Libgcc_Switch    : aliased Argument_List :=
71                                (1 => Shared_Libgcc'Access);
72    Link_With_Shared_Libgcc : Argument_List_Access :=
73                                No_Shared_Libgcc_Switch'Access;
74
75    ------------------------------
76    -- Target dependent section --
77    ------------------------------
78
79    function Popen (Command, Mode : System.Address) return System.Address;
80    pragma Import (C, Popen);
81
82    function Pclose (File : System.Address) return Integer;
83    pragma Import (C, Pclose);
84
85    ---------------------
86    -- Archive_Builder --
87    ---------------------
88
89    function Archive_Builder return String is
90    begin
91       return "ar";
92    end Archive_Builder;
93
94    -----------------------------
95    -- Archive_Builder_Options --
96    -----------------------------
97
98    function Archive_Builder_Options return String_List_Access is
99    begin
100       return new String_List'(1 => new String'("cr"));
101    end Archive_Builder_Options;
102
103    -----------------
104    -- Archive_Ext --
105    -----------------
106
107    function Archive_Ext return String is
108    begin
109       return "olb";
110    end Archive_Ext;
111
112    ---------------------
113    -- Archive_Indexer --
114    ---------------------
115
116    function Archive_Indexer return String is
117    begin
118       return "ranlib";
119    end Archive_Indexer;
120
121    ---------------------------
122    -- Build_Dynamic_Library --
123    ---------------------------
124
125    procedure Build_Dynamic_Library
126      (Ofiles       : Argument_List;
127       Foreign      : Argument_List;
128       Afiles       : Argument_List;
129       Options      : Argument_List;
130       Options_2    : Argument_List;
131       Interfaces   : Argument_List;
132       Lib_Filename : String;
133       Lib_Dir      : String;
134       Symbol_Data  : Symbol_Record;
135       Driver_Name  : Name_Id := No_Name;
136       Lib_Version  : String  := "";
137       Auto_Init    : Boolean := False)
138    is
139       pragma Unreferenced (Foreign);
140       pragma Unreferenced (Afiles);
141
142       Lib_File : constant String :=
143                    Lib_Dir & Directory_Separator & "lib" &
144                    Fil.Ext_To (Lib_Filename, DLL_Ext);
145
146       Opts      : Argument_List := Options;
147       Last_Opt  : Natural       := Opts'Last;
148       Opts2     : Argument_List (Options'Range);
149       Last_Opt2 : Natural       := Opts2'First - 1;
150
151       Inter : constant Argument_List := Interfaces;
152
153       function Is_Interface (Obj_File : String) return Boolean;
154       --  For a Stand-Alone Library, returns True if Obj_File is the object
155       --  file name of an interface of the SAL. For other libraries, always
156       --  return True.
157
158       function Option_File_Name return String;
159       --  Returns Symbol_File, if not empty. Otherwise, returns "symvec.opt"
160
161       function Version_String return String;
162       --  Returns Lib_Version if not empty, otherwise returns "1".
163       --  Fails gnatmake if Lib_Version is not the image of a positive number.
164
165       ------------------
166       -- Is_Interface --
167       ------------------
168
169       function Is_Interface (Obj_File : String) return Boolean is
170          ALI : constant String :=
171                  Fil.Ext_To
172                   (Filename => To_Lower (Base_Name (Obj_File)),
173                    New_Ext  => "ali");
174
175       begin
176          if Inter'Length = 0 then
177             return True;
178
179          elsif ALI'Length > 2 and then
180                ALI (ALI'First .. ALI'First + 1) = "b$"
181          then
182             return True;
183
184          else
185             for J in Inter'Range loop
186                if Inter (J).all = ALI then
187                   return True;
188                end if;
189             end loop;
190
191             return False;
192          end if;
193       end Is_Interface;
194
195       ----------------------
196       -- Option_File_Name --
197       ----------------------
198
199       function Option_File_Name return String is
200       begin
201          if Symbol_Data.Symbol_File = No_Name then
202             return "symvec.opt";
203          else
204             Get_Name_String (Symbol_Data.Symbol_File);
205             To_Lower (Name_Buffer (1 .. Name_Len));
206             return Name_Buffer (1 .. Name_Len);
207          end if;
208       end Option_File_Name;
209
210       --------------------
211       -- Version_String --
212       --------------------
213
214       function Version_String return String is
215          Version : Integer := 0;
216       begin
217          if Lib_Version = "" then
218             return "1";
219
220          else
221             begin
222                Version := Integer'Value (Lib_Version);
223
224                if Version <= 0 then
225                   raise Constraint_Error;
226                end if;
227
228                return Lib_Version;
229
230             exception
231                when Constraint_Error =>
232                   Fail ("illegal version """, Lib_Version,
233                         """ (on VMS version must be a positive number)");
234                   return "";
235             end;
236          end if;
237       end Version_String;
238
239       Opt_File_Name  : constant String := Option_File_Name;
240       Version        : constant String := Version_String;
241       For_Linker_Opt : String_Access;
242
243    --  Start of processing for Build_Dynamic_Library
244
245    begin
246       --  Invoke gcc with -shared-libgcc, but only for GCC 3 or higher
247
248       if GCC_Version >= 3 then
249          Link_With_Shared_Libgcc := Shared_Libgcc_Switch'Access;
250       else
251          Link_With_Shared_Libgcc := No_Shared_Libgcc_Switch'Access;
252       end if;
253
254       --  Option file must end with ".opt"
255
256       if Opt_File_Name'Length > 4
257         and then
258           Opt_File_Name (Opt_File_Name'Last - 3 .. Opt_File_Name'Last) = ".opt"
259       then
260          For_Linker_Opt := new String'("--for-linker=" & Opt_File_Name);
261       else
262          Fail ("Options File """, Opt_File_Name, """ must end with .opt");
263       end if;
264
265       VMS_Options (VMS_Options'First) := For_Linker_Opt;
266
267       for J in Inter'Range loop
268          To_Lower (Inter (J).all);
269       end loop;
270
271       --  "gnatsym" is necessary for building the option file
272
273       if Gnatsym_Path = null then
274          Gnatsym_Path := OS_Lib.Locate_Exec_On_Path (Gnatsym_Name);
275
276          if Gnatsym_Path = null then
277             Fail (Gnatsym_Name, " not found in path");
278          end if;
279       end if;
280
281       --  For auto-initialization of a stand-alone library, we create
282       --  a macro-assembly file and we invoke the macro-assembler.
283
284       if Auto_Init then
285          declare
286             Macro_File_Name : constant String := Lib_Filename & "$init.asm";
287             Macro_File      : File_Descriptor;
288             Init_Proc       : String := Lib_Filename & "INIT";
289             Popen_Result    : System.Address;
290             Pclose_Result   : Integer;
291             Len             : Natural;
292             OK              : Boolean := True;
293
294             Command  : constant String :=
295                          Macro_Name & " " & Macro_File_Name & ASCII.NUL;
296             --  The command to invoke the assembler on the generated auto-init
297             --  assembly file.
298
299             Mode : constant String := "r" & ASCII.NUL;
300             --  The mode for the invocation of Popen
301
302          begin
303             To_Upper (Init_Proc);
304
305             if Verbose_Mode then
306                Write_Str ("Creating auto-init assembly file """);
307                Write_Str (Macro_File_Name);
308                Write_Line ("""");
309             end if;
310
311             --  Create and write the auto-init assembly file
312
313             declare
314                First_Line : constant String :=
315                  ASCII.HT &
316                  ".type " & Init_Proc & "#, @function" &
317                  ASCII.LF;
318                Second_Line : constant String :=
319                  ASCII.HT &
320                  ".global " & Init_Proc & "#" &
321                  ASCII.LF;
322                Third_Line : constant String :=
323                  ASCII.HT &
324                  ".global LIB$INITIALIZE#" &
325                  ASCII.LF;
326                Fourth_Line : constant String :=
327                  ASCII.HT &
328                  ".section LIB$INITIALIZE#,""a"",@progbits" &
329                  ASCII.LF;
330                Fifth_Line : constant String :=
331                  ASCII.HT &
332                  "data4 @fptr(" & Init_Proc & "#)" &
333                   ASCII.LF;
334
335             begin
336                Macro_File := Create_File (Macro_File_Name, Text);
337                OK := Macro_File /= Invalid_FD;
338
339                if OK then
340                   Len := Write
341                     (Macro_File, First_Line (First_Line'First)'Address,
342                      First_Line'Length);
343                   OK := Len = First_Line'Length;
344                end if;
345
346                if OK then
347                   Len := Write
348                     (Macro_File, Second_Line (Second_Line'First)'Address,
349                      Second_Line'Length);
350                   OK := Len = Second_Line'Length;
351                end if;
352
353                if OK then
354                   Len := Write
355                     (Macro_File, Third_Line (Third_Line'First)'Address,
356                      Third_Line'Length);
357                   OK := Len = Third_Line'Length;
358                end if;
359
360                if OK then
361                   Len := Write
362                     (Macro_File, Fourth_Line (Fourth_Line'First)'Address,
363                      Fourth_Line'Length);
364                   OK := Len = Fourth_Line'Length;
365                end if;
366
367                if OK then
368                   Len := Write
369                     (Macro_File, Fifth_Line (Fifth_Line'First)'Address,
370                      Fifth_Line'Length);
371                   OK := Len = Fifth_Line'Length;
372                end if;
373
374                if OK then
375                   Close (Macro_File, OK);
376                end if;
377
378                if not OK then
379                   Fail ("creation of auto-init assembly file """,
380                         Macro_File_Name, """ failed");
381                end if;
382             end;
383
384             --  Invoke the macro-assembler
385
386             if Verbose_Mode then
387                Write_Str ("Assembling auto-init assembly file """);
388                Write_Str (Macro_File_Name);
389                Write_Line ("""");
390             end if;
391
392             Popen_Result := Popen (Command (Command'First)'Address,
393                                    Mode (Mode'First)'Address);
394
395             if Popen_Result = Null_Address then
396                Fail ("assembly of auto-init assembly file """,
397                      Macro_File_Name, """ failed");
398             end if;
399
400             --  Wait for the end of execution of the macro-assembler
401
402             Pclose_Result := Pclose (Popen_Result);
403
404             if Pclose_Result < 0 then
405                Fail ("assembly of auto init assembly file """,
406                      Macro_File_Name, """ failed");
407             end if;
408
409             --  Add the generated object file to the list of objects to be
410             --  included in the library.
411
412             Additional_Objects :=
413               new Argument_List'
414                 (1 => new String'(Lib_Filename & "$init.obj"));
415          end;
416       end if;
417
418       --  Allocate the argument list and put the symbol file name, the
419       --  reference (if any) and the policy (if not autonomous).
420
421       Arguments := new Argument_List (1 .. Ofiles'Length + 8);
422
423       Last_Argument := 0;
424
425       --  Verbosity
426
427       if Verbose_Mode then
428          Last_Argument := Last_Argument + 1;
429          Arguments (Last_Argument) := new String'("-v");
430       end if;
431
432       --  Version number (major ID)
433
434       if Lib_Version /= "" then
435          Last_Argument := Last_Argument + 1;
436          Arguments (Last_Argument) := new String'("-V");
437          Last_Argument := Last_Argument + 1;
438          Arguments (Last_Argument) := new String'(Version);
439       end if;
440
441       --  Symbol file
442
443       Last_Argument := Last_Argument + 1;
444       Arguments (Last_Argument) := new String'("-s");
445       Last_Argument := Last_Argument + 1;
446       Arguments (Last_Argument) := new String'(Opt_File_Name);
447
448       --  Reference Symbol File
449
450       if Symbol_Data.Reference /= No_Name then
451          Last_Argument := Last_Argument + 1;
452          Arguments (Last_Argument) := new String'("-r");
453          Last_Argument := Last_Argument + 1;
454          Arguments (Last_Argument) :=
455            new String'(Get_Name_String (Symbol_Data.Reference));
456       end if;
457
458       --  Policy
459
460       case Symbol_Data.Symbol_Policy is
461          when Autonomous =>
462             null;
463
464          when Compliant =>
465             Last_Argument := Last_Argument + 1;
466             Arguments (Last_Argument) := new String'("-c");
467
468          when Controlled =>
469             Last_Argument := Last_Argument + 1;
470             Arguments (Last_Argument) := new String'("-C");
471
472          when Restricted =>
473             Last_Argument := Last_Argument + 1;
474             Arguments (Last_Argument) := new String'("-R");
475       end case;
476
477       --  Add each relevant object file
478
479       for Index in Ofiles'Range loop
480          if Is_Interface (Ofiles (Index).all) then
481             Last_Argument := Last_Argument + 1;
482             Arguments (Last_Argument) := new String'(Ofiles (Index).all);
483          end if;
484       end loop;
485
486       --  Spawn gnatsym
487
488       Spawn (Program_Name => Gnatsym_Path.all,
489              Args         => Arguments (1 .. Last_Argument),
490              Success      => Success);
491
492       if not Success then
493          Fail ("unable to create symbol file for library """,
494                Lib_Filename, """");
495       end if;
496
497       Free (Arguments);
498
499       --  Move all the -l switches from Opts to Opts2
500
501       declare
502          Index : Natural := Opts'First;
503          Opt   : String_Access;
504
505       begin
506          while Index <= Last_Opt loop
507             Opt := Opts (Index);
508
509             if Opt'Length > 2 and then
510               Opt (Opt'First .. Opt'First + 1) = "-l"
511             then
512                if Index < Last_Opt then
513                   Opts (Index .. Last_Opt - 1) :=
514                     Opts (Index + 1 .. Last_Opt);
515                end if;
516
517                Last_Opt := Last_Opt - 1;
518
519                Last_Opt2 := Last_Opt2 + 1;
520                Opts2 (Last_Opt2) := Opt;
521
522             else
523                Index := Index + 1;
524             end if;
525          end loop;
526       end;
527
528       --  Invoke gcc to build the library
529
530       Utl.Gcc
531         (Output_File => Lib_File,
532          Objects     => Ofiles & Additional_Objects.all,
533          Options     => VMS_Options,
534          Options_2   => Link_With_Shared_Libgcc.all &
535                         Opts (Opts'First .. Last_Opt) &
536                         Opts2 (Opts2'First .. Last_Opt2) & Options_2,
537          Driver_Name => Driver_Name);
538
539       --  The auto-init object file need to be deleted, so that it will not
540       --  be included in the library as a regular object file, otherwise
541       --  it will be included twice when the library will be built next
542       --  time, which may lead to errors.
543
544       if Auto_Init then
545          declare
546             Auto_Init_Object_File_Name : constant String :=
547                                            Lib_Filename & "$init.obj";
548             Disregard : Boolean;
549
550          begin
551             if Verbose_Mode then
552                Write_Str ("deleting auto-init object file """);
553                Write_Str (Auto_Init_Object_File_Name);
554                Write_Line ("""");
555             end if;
556
557             Delete_File (Auto_Init_Object_File_Name, Success => Disregard);
558          end;
559       end if;
560    end Build_Dynamic_Library;
561
562    -------------
563    -- DLL_Ext --
564    -------------
565
566    function DLL_Ext return String is
567    begin
568       return "exe";
569    end DLL_Ext;
570
571    --------------------
572    -- Dynamic_Option --
573    --------------------
574
575    function Dynamic_Option return String is
576    begin
577       return "-shared";
578    end Dynamic_Option;
579
580    -------------------
581    -- Is_Object_Ext --
582    -------------------
583
584    function Is_Object_Ext (Ext : String) return Boolean is
585    begin
586       return Ext = ".obj";
587    end Is_Object_Ext;
588
589    --------------
590    -- Is_C_Ext --
591    --------------
592
593    function Is_C_Ext (Ext : String) return Boolean is
594    begin
595       return Ext = ".c";
596    end Is_C_Ext;
597
598    --------------------
599    -- Is_Archive_Ext --
600    --------------------
601
602    function Is_Archive_Ext (Ext : String) return Boolean is
603    begin
604       return Ext = ".olb" or else Ext = ".exe";
605    end Is_Archive_Ext;
606
607    -------------
608    -- Libgnat --
609    -------------
610
611    function Libgnat return String is
612       Libgnat_A : constant String := "libgnat.a";
613       Libgnat_Olb : constant String := "libgnat.olb";
614
615    begin
616       Name_Len := Libgnat_A'Length;
617       Name_Buffer (1 .. Name_Len) := Libgnat_A;
618
619       if Osint.Find_File (Name_Enter, Osint.Library) /= No_File then
620          return Libgnat_A;
621
622       else
623          return Libgnat_Olb;
624       end if;
625    end Libgnat;
626
627    ------------------------
628    -- Library_Exists_For --
629    ------------------------
630
631    function Library_Exists_For (Project : Project_Id) return Boolean is
632    begin
633       if not Projects.Table (Project).Library then
634          Fail ("INTERNAL ERROR: Library_Exists_For called " &
635                "for non library project");
636          return False;
637
638       else
639          declare
640             Lib_Dir : constant String :=
641               Get_Name_String (Projects.Table (Project).Library_Dir);
642             Lib_Name : constant String :=
643               Get_Name_String (Projects.Table (Project).Library_Name);
644
645          begin
646             if Projects.Table (Project).Library_Kind = Static then
647                return Is_Regular_File
648                  (Lib_Dir & Directory_Separator & "lib" &
649                   Fil.Ext_To (Lib_Name, Archive_Ext));
650
651             else
652                return Is_Regular_File
653                  (Lib_Dir & Directory_Separator & "lib" &
654                   Fil.Ext_To (Lib_Name, DLL_Ext));
655             end if;
656          end;
657       end if;
658    end Library_Exists_For;
659
660    ---------------------------
661    -- Library_File_Name_For --
662    ---------------------------
663
664    function Library_File_Name_For (Project : Project_Id) return Name_Id is
665    begin
666       if not Projects.Table (Project).Library then
667          Prj.Com.Fail ("INTERNAL ERROR: Library_File_Name_For called " &
668                        "for non library project");
669          return No_Name;
670
671       else
672          declare
673             Lib_Name : constant String :=
674               Get_Name_String (Projects.Table (Project).Library_Name);
675
676          begin
677             Name_Len := 3;
678             Name_Buffer (1 .. Name_Len) := "lib";
679
680             if Projects.Table (Project).Library_Kind = Static then
681                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, Archive_Ext));
682
683             else
684                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, DLL_Ext));
685             end if;
686
687             return Name_Find;
688          end;
689       end if;
690    end Library_File_Name_For;
691
692    ----------------
693    -- Object_Ext --
694    ----------------
695
696    function Object_Ext return String is
697    begin
698       return "obj";
699    end Object_Ext;
700
701    ----------------
702    -- PIC_Option --
703    ----------------
704
705    function PIC_Option return String is
706    begin
707       return "";
708    end PIC_Option;
709
710    -----------------------------------------------
711    -- Standalone_Library_Auto_Init_Is_Supported --
712    -----------------------------------------------
713
714    function Standalone_Library_Auto_Init_Is_Supported return Boolean is
715    begin
716       return True;
717    end Standalone_Library_Auto_Init_Is_Supported;
718
719    ---------------------------
720    -- Support_For_Libraries --
721    ---------------------------
722
723    function Support_For_Libraries return Library_Support is
724    begin
725       return Full;
726    end Support_For_Libraries;
727
728 end MLib.Tgt;