OSDN Git Service

2004-06-28 Robert Dewar <dewar@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       Interfaces   : Argument_List;
131       Lib_Filename : String;
132       Lib_Dir      : String;
133       Symbol_Data  : Symbol_Record;
134       Driver_Name  : Name_Id := No_Name;
135       Lib_Version  : String  := "";
136       Auto_Init    : Boolean := False)
137    is
138       pragma Unreferenced (Foreign);
139       pragma Unreferenced (Afiles);
140
141       Lib_File : constant String :=
142                    Lib_Dir & Directory_Separator & "lib" &
143                    Fil.Ext_To (Lib_Filename, DLL_Ext);
144
145       Opts      : Argument_List := Options;
146       Last_Opt  : Natural       := Opts'Last;
147       Opts2     : Argument_List (Options'Range);
148       Last_Opt2 : Natural       := Opts2'First - 1;
149
150       Inter : constant Argument_List := Interfaces;
151
152       function Is_Interface (Obj_File : String) return Boolean;
153       --  For a Stand-Alone Library, returns True if Obj_File is the object
154       --  file name of an interface of the SAL. For other libraries, always
155       --  return True.
156
157       function Option_File_Name return String;
158       --  Returns Symbol_File, if not empty. Otherwise, returns "symvec.opt"
159
160       function Version_String return String;
161       --  Returns Lib_Version if not empty, otherwise returns "1".
162       --  Fails gnatmake if Lib_Version is not the image of a positive number.
163
164       ------------------
165       -- Is_Interface --
166       ------------------
167
168       function Is_Interface (Obj_File : String) return Boolean is
169          ALI : constant String :=
170                  Fil.Ext_To
171                   (Filename => To_Lower (Base_Name (Obj_File)),
172                    New_Ext  => "ali");
173
174       begin
175          if Inter'Length = 0 then
176             return True;
177
178          elsif ALI'Length > 2 and then
179                ALI (ALI'First .. ALI'First + 1) = "b$"
180          then
181             return True;
182
183          else
184             for J in Inter'Range loop
185                if Inter (J).all = ALI then
186                   return True;
187                end if;
188             end loop;
189
190             return False;
191          end if;
192       end Is_Interface;
193
194       ----------------------
195       -- Option_File_Name --
196       ----------------------
197
198       function Option_File_Name return String is
199       begin
200          if Symbol_Data.Symbol_File = No_Name then
201             return "symvec.opt";
202          else
203             Get_Name_String (Symbol_Data.Symbol_File);
204             To_Lower (Name_Buffer (1 .. Name_Len));
205             return Name_Buffer (1 .. Name_Len);
206          end if;
207       end Option_File_Name;
208
209       --------------------
210       -- Version_String --
211       --------------------
212
213       function Version_String return String is
214          Version : Integer := 0;
215       begin
216          if Lib_Version = "" then
217             return "1";
218
219          else
220             begin
221                Version := Integer'Value (Lib_Version);
222
223                if Version <= 0 then
224                   raise Constraint_Error;
225                end if;
226
227                return Lib_Version;
228
229             exception
230                when Constraint_Error =>
231                   Fail ("illegal version """, Lib_Version,
232                         """ (on VMS version must be a positive number)");
233                   return "";
234             end;
235          end if;
236       end Version_String;
237
238       Opt_File_Name  : constant String := Option_File_Name;
239       Version        : constant String := Version_String;
240       For_Linker_Opt : String_Access;
241
242    --  Start of processing for Build_Dynamic_Library
243
244    begin
245       --  Invoke gcc with -shared-libgcc, but only for GCC 3 or higher
246
247       if GCC_Version >= 3 then
248          Link_With_Shared_Libgcc := Shared_Libgcc_Switch'Access;
249       else
250          Link_With_Shared_Libgcc := No_Shared_Libgcc_Switch'Access;
251       end if;
252
253       --  Option file must end with ".opt"
254
255       if Opt_File_Name'Length > 4
256         and then
257           Opt_File_Name (Opt_File_Name'Last - 3 .. Opt_File_Name'Last) = ".opt"
258       then
259          For_Linker_Opt := new String'("--for-linker=" & Opt_File_Name);
260       else
261          Fail ("Options File """, Opt_File_Name, """ must end with .opt");
262       end if;
263
264       VMS_Options (VMS_Options'First) := For_Linker_Opt;
265
266       for J in Inter'Range loop
267          To_Lower (Inter (J).all);
268       end loop;
269
270       --  "gnatsym" is necessary for building the option file
271
272       if Gnatsym_Path = null then
273          Gnatsym_Path := OS_Lib.Locate_Exec_On_Path (Gnatsym_Name);
274
275          if Gnatsym_Path = null then
276             Fail (Gnatsym_Name, " not found in path");
277          end if;
278       end if;
279
280       --  For auto-initialization of a stand-alone library, we create
281       --  a macro-assembly file and we invoke the macro-assembler.
282
283       if Auto_Init then
284          declare
285             Macro_File_Name : constant String := Lib_Filename & "$init.asm";
286             Macro_File      : File_Descriptor;
287             Init_Proc       : String := Lib_Filename & "INIT";
288             Popen_Result    : System.Address;
289             Pclose_Result   : Integer;
290             Len             : Natural;
291             OK              : Boolean := True;
292
293             Command  : constant String :=
294                          Macro_Name & " " & Macro_File_Name & ASCII.NUL;
295             --  The command to invoke the assembler on the generated auto-init
296             --  assembly file.
297
298             Mode : constant String := "r" & ASCII.NUL;
299             --  The mode for the invocation of Popen
300
301          begin
302             To_Upper (Init_Proc);
303
304             if Verbose_Mode then
305                Write_Str ("Creating auto-init assembly file """);
306                Write_Str (Macro_File_Name);
307                Write_Line ("""");
308             end if;
309
310             --  Create and write the auto-init assembly file
311
312             declare
313                First_Line : constant String :=
314                  ASCII.HT &
315                  ".type " & Init_Proc & "#, @function" &
316                  ASCII.LF;
317                Second_Line : constant String :=
318                  ASCII.HT &
319                  ".global " & Init_Proc & "#" &
320                  ASCII.LF;
321                Third_Line : constant String :=
322                  ASCII.HT &
323                  ".global LIB$INITIALIZE#" &
324                  ASCII.LF;
325                Fourth_Line : constant String :=
326                  ASCII.HT &
327                  ".section LIB$INITIALIZE#,""a"",@progbits" &
328                  ASCII.LF;
329                Fifth_Line : constant String :=
330                  ASCII.HT &
331                  "data4 @fptr(" & Init_Proc & "#)" &
332                   ASCII.LF;
333
334             begin
335                Macro_File := Create_File (Macro_File_Name, Text);
336                OK := Macro_File /= Invalid_FD;
337
338                if OK then
339                   Len := Write
340                     (Macro_File, First_Line (First_Line'First)'Address,
341                      First_Line'Length);
342                   OK := Len = First_Line'Length;
343                end if;
344
345                if OK then
346                   Len := Write
347                     (Macro_File, Second_Line (Second_Line'First)'Address,
348                      Second_Line'Length);
349                   OK := Len = Second_Line'Length;
350                end if;
351
352                if OK then
353                   Len := Write
354                     (Macro_File, Third_Line (Third_Line'First)'Address,
355                      Third_Line'Length);
356                   OK := Len = Third_Line'Length;
357                end if;
358
359                if OK then
360                   Len := Write
361                     (Macro_File, Fourth_Line (Fourth_Line'First)'Address,
362                      Fourth_Line'Length);
363                   OK := Len = Fourth_Line'Length;
364                end if;
365
366                if OK then
367                   Len := Write
368                     (Macro_File, Fifth_Line (Fifth_Line'First)'Address,
369                      Fifth_Line'Length);
370                   OK := Len = Fifth_Line'Length;
371                end if;
372
373                if OK then
374                   Close (Macro_File, OK);
375                end if;
376
377                if not OK then
378                   Fail ("creation of auto-init assembly file """,
379                         Macro_File_Name, """ failed");
380                end if;
381             end;
382
383             --  Invoke the macro-assembler
384
385             if Verbose_Mode then
386                Write_Str ("Assembling auto-init assembly file """);
387                Write_Str (Macro_File_Name);
388                Write_Line ("""");
389             end if;
390
391             Popen_Result := Popen (Command (Command'First)'Address,
392                                    Mode (Mode'First)'Address);
393
394             if Popen_Result = Null_Address then
395                Fail ("assembly of auto-init assembly file """,
396                      Macro_File_Name, """ failed");
397             end if;
398
399             --  Wait for the end of execution of the macro-assembler
400
401             Pclose_Result := Pclose (Popen_Result);
402
403             if Pclose_Result < 0 then
404                Fail ("assembly of auto init assembly file """,
405                      Macro_File_Name, """ failed");
406             end if;
407
408             --  Add the generated object file to the list of objects to be
409             --  included in the library.
410
411             Additional_Objects :=
412               new Argument_List'
413                 (1 => new String'(Lib_Filename & "$init.obj"));
414          end;
415       end if;
416
417       --  Allocate the argument list and put the symbol file name, the
418       --  reference (if any) and the policy (if not autonomous).
419
420       Arguments := new Argument_List (1 .. Ofiles'Length + 8);
421
422       Last_Argument := 0;
423
424       --  Verbosity
425
426       if Verbose_Mode then
427          Last_Argument := Last_Argument + 1;
428          Arguments (Last_Argument) := new String'("-v");
429       end if;
430
431       --  Version number (major ID)
432
433       if Lib_Version /= "" then
434          Last_Argument := Last_Argument + 1;
435          Arguments (Last_Argument) := new String'("-V");
436          Last_Argument := Last_Argument + 1;
437          Arguments (Last_Argument) := new String'(Version);
438       end if;
439
440       --  Symbol file
441
442       Last_Argument := Last_Argument + 1;
443       Arguments (Last_Argument) := new String'("-s");
444       Last_Argument := Last_Argument + 1;
445       Arguments (Last_Argument) := new String'(Opt_File_Name);
446
447       --  Reference Symbol File
448
449       if Symbol_Data.Reference /= No_Name then
450          Last_Argument := Last_Argument + 1;
451          Arguments (Last_Argument) := new String'("-r");
452          Last_Argument := Last_Argument + 1;
453          Arguments (Last_Argument) :=
454            new String'(Get_Name_String (Symbol_Data.Reference));
455       end if;
456
457       --  Policy
458
459       case Symbol_Data.Symbol_Policy is
460          when Autonomous =>
461             null;
462
463          when Compliant =>
464             Last_Argument := Last_Argument + 1;
465             Arguments (Last_Argument) := new String'("-c");
466
467          when Controlled =>
468             Last_Argument := Last_Argument + 1;
469             Arguments (Last_Argument) := new String'("-C");
470
471          when Restricted =>
472             Last_Argument := Last_Argument + 1;
473             Arguments (Last_Argument) := new String'("-R");
474       end case;
475
476       --  Add each relevant object file
477
478       for Index in Ofiles'Range loop
479          if Is_Interface (Ofiles (Index).all) then
480             Last_Argument := Last_Argument + 1;
481             Arguments (Last_Argument) := new String'(Ofiles (Index).all);
482          end if;
483       end loop;
484
485       --  Spawn gnatsym
486
487       Spawn (Program_Name => Gnatsym_Path.all,
488              Args         => Arguments (1 .. Last_Argument),
489              Success      => Success);
490
491       if not Success then
492          Fail ("unable to create symbol file for library """,
493                Lib_Filename, """");
494       end if;
495
496       Free (Arguments);
497
498       --  Move all the -l switches from Opts to Opts2
499
500       declare
501          Index : Natural := Opts'First;
502          Opt   : String_Access;
503
504       begin
505          while Index <= Last_Opt loop
506             Opt := Opts (Index);
507
508             if Opt'Length > 2 and then
509               Opt (Opt'First .. Opt'First + 1) = "-l"
510             then
511                if Index < Last_Opt then
512                   Opts (Index .. Last_Opt - 1) :=
513                     Opts (Index + 1 .. Last_Opt);
514                end if;
515
516                Last_Opt := Last_Opt - 1;
517
518                Last_Opt2 := Last_Opt2 + 1;
519                Opts2 (Last_Opt2) := Opt;
520
521             else
522                Index := Index + 1;
523             end if;
524          end loop;
525       end;
526
527       --  Invoke gcc to build the library
528
529       Utl.Gcc
530         (Output_File => Lib_File,
531          Objects     => Ofiles & Additional_Objects.all,
532          Options     => VMS_Options,
533          Options_2   => Link_With_Shared_Libgcc.all &
534                         Opts (Opts'First .. Last_Opt) &
535                         Opts2 (Opts2'First .. Last_Opt2),
536          Driver_Name => Driver_Name);
537
538       --  The auto-init object file need to be deleted, so that it will not
539       --  be included in the library as a regular object file, otherwise
540       --  it will be included twice when the library will be built next
541       --  time, which may lead to errors.
542
543       if Auto_Init then
544          declare
545             Auto_Init_Object_File_Name : constant String :=
546                                            Lib_Filename & "$init.obj";
547             Disregard : Boolean;
548
549          begin
550             if Verbose_Mode then
551                Write_Str ("deleting auto-init object file """);
552                Write_Str (Auto_Init_Object_File_Name);
553                Write_Line ("""");
554             end if;
555
556             Delete_File (Auto_Init_Object_File_Name, Success => Disregard);
557          end;
558       end if;
559    end Build_Dynamic_Library;
560
561    -------------
562    -- DLL_Ext --
563    -------------
564
565    function DLL_Ext return String is
566    begin
567       return "exe";
568    end DLL_Ext;
569
570    --------------------
571    -- Dynamic_Option --
572    --------------------
573
574    function Dynamic_Option return String is
575    begin
576       return "-shared";
577    end Dynamic_Option;
578
579    -------------------
580    -- Is_Object_Ext --
581    -------------------
582
583    function Is_Object_Ext (Ext : String) return Boolean is
584    begin
585       return Ext = ".obj";
586    end Is_Object_Ext;
587
588    --------------
589    -- Is_C_Ext --
590    --------------
591
592    function Is_C_Ext (Ext : String) return Boolean is
593    begin
594       return Ext = ".c";
595    end Is_C_Ext;
596
597    --------------------
598    -- Is_Archive_Ext --
599    --------------------
600
601    function Is_Archive_Ext (Ext : String) return Boolean is
602    begin
603       return Ext = ".olb" or else Ext = ".exe";
604    end Is_Archive_Ext;
605
606    -------------
607    -- Libgnat --
608    -------------
609
610    function Libgnat return String is
611       Libgnat_A : constant String := "libgnat.a";
612       Libgnat_Olb : constant String := "libgnat.olb";
613
614    begin
615       Name_Len := Libgnat_A'Length;
616       Name_Buffer (1 .. Name_Len) := Libgnat_A;
617
618       if Osint.Find_File (Name_Enter, Osint.Library) /= No_File then
619          return Libgnat_A;
620
621       else
622          return Libgnat_Olb;
623       end if;
624    end Libgnat;
625
626    ------------------------
627    -- Library_Exists_For --
628    ------------------------
629
630    function Library_Exists_For (Project : Project_Id) return Boolean is
631    begin
632       if not Projects.Table (Project).Library then
633          Fail ("INTERNAL ERROR: Library_Exists_For called " &
634                "for non library project");
635          return False;
636
637       else
638          declare
639             Lib_Dir : constant String :=
640               Get_Name_String (Projects.Table (Project).Library_Dir);
641             Lib_Name : constant String :=
642               Get_Name_String (Projects.Table (Project).Library_Name);
643
644          begin
645             if Projects.Table (Project).Library_Kind = Static then
646                return Is_Regular_File
647                  (Lib_Dir & Directory_Separator & "lib" &
648                   Fil.Ext_To (Lib_Name, Archive_Ext));
649
650             else
651                return Is_Regular_File
652                  (Lib_Dir & Directory_Separator & "lib" &
653                   Fil.Ext_To (Lib_Name, DLL_Ext));
654             end if;
655          end;
656       end if;
657    end Library_Exists_For;
658
659    ---------------------------
660    -- Library_File_Name_For --
661    ---------------------------
662
663    function Library_File_Name_For (Project : Project_Id) return Name_Id is
664    begin
665       if not Projects.Table (Project).Library then
666          Prj.Com.Fail ("INTERNAL ERROR: Library_File_Name_For called " &
667                        "for non library project");
668          return No_Name;
669
670       else
671          declare
672             Lib_Name : constant String :=
673               Get_Name_String (Projects.Table (Project).Library_Name);
674
675          begin
676             Name_Len := 3;
677             Name_Buffer (1 .. Name_Len) := "lib";
678
679             if Projects.Table (Project).Library_Kind = Static then
680                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, Archive_Ext));
681
682             else
683                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, DLL_Ext));
684             end if;
685
686             return Name_Find;
687          end;
688       end if;
689    end Library_File_Name_For;
690
691    ----------------
692    -- Object_Ext --
693    ----------------
694
695    function Object_Ext return String is
696    begin
697       return "obj";
698    end Object_Ext;
699
700    ----------------
701    -- PIC_Option --
702    ----------------
703
704    function PIC_Option return String is
705    begin
706       return "";
707    end PIC_Option;
708
709    -----------------------------------------------
710    -- Standalone_Library_Auto_Init_Is_Supported --
711    -----------------------------------------------
712
713    function Standalone_Library_Auto_Init_Is_Supported return Boolean is
714    begin
715       return True;
716    end Standalone_Library_Auto_Init_Is_Supported;
717
718    ---------------------------
719    -- Support_For_Libraries --
720    ---------------------------
721
722    function Support_For_Libraries return Library_Support is
723    begin
724       return Full;
725    end Support_For_Libraries;
726
727 end MLib.Tgt;