OSDN Git Service

* ada-tree.h (TYPE_RM_SIZE_INT): Use TYPE_LANG_SLOT_1.
[pf3gnuchains/gcc-fork.git] / gcc / ada / mlib-tgt-vms-alpha.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                             M L I B . T G T                              --
6 --                           (Alpha VMS Version)                            --
7 --                                                                          --
8 --                                 B o d y                                  --
9 --                                                                          --
10 --          Copyright (C) 2003-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 Alpha 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       --  If option file name does not ends with ".opt", append "/OPTIONS"
254       --  to its specification for the VMS linker.
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          For_Linker_Opt :=
263            new String'("--for-linker=" & Opt_File_Name & "/OPTIONS");
264       end if;
265
266       VMS_Options (VMS_Options'First) := For_Linker_Opt;
267
268       for J in Inter'Range loop
269          To_Lower (Inter (J).all);
270       end loop;
271
272       --  "gnatsym" is necessary for building the option file
273
274       if Gnatsym_Path = null then
275          Gnatsym_Path := OS_Lib.Locate_Exec_On_Path (Gnatsym_Name);
276
277          if Gnatsym_Path = null then
278             Fail (Gnatsym_Name, " not found in path");
279          end if;
280       end if;
281
282       --  For auto-initialization of a stand-alone library, we create
283       --  a macro-assembly file and we invoke the macro-assembler.
284
285       if Auto_Init then
286          declare
287             Macro_File_Name : constant String := Lib_Filename & "$init.asm";
288             Macro_File      : File_Descriptor;
289             Init_Proc       : String := Lib_Filename & "INIT";
290             Popen_Result    : System.Address;
291             Pclose_Result   : Integer;
292             Len             : Natural;
293             OK              : Boolean := True;
294
295             Command  : constant String :=
296                          Macro_Name & " " & Macro_File_Name & ASCII.NUL;
297             --  The command to invoke the assembler on the generated auto-init
298             --  assembly file.
299
300             Mode : constant String := "r" & ASCII.NUL;
301             --  The mode for the invocation of Popen
302
303          begin
304             To_Upper (Init_Proc);
305
306             if Verbose_Mode then
307                Write_Str ("Creating auto-init assembly file """);
308                Write_Str (Macro_File_Name);
309                Write_Line ("""");
310             end if;
311
312             --  Create and write the auto-init assembly file
313
314             declare
315                First_Line : constant String :=
316                               ASCII.HT & ".section LIB$INITIALIZE,GBL,NOWRT" &
317                ASCII.LF;
318                Second_Line : constant String :=
319                                ASCII.HT & ".long " & Init_Proc & ASCII.LF;
320                --  First and second lines of the auto-init assembly file
321
322             begin
323                Macro_File := Create_File (Macro_File_Name, Text);
324                OK := Macro_File /= Invalid_FD;
325
326                if OK then
327                   Len := Write
328                     (Macro_File, First_Line (First_Line'First)'Address,
329                      First_Line'Length);
330                   OK := Len = First_Line'Length;
331                end if;
332
333                if OK then
334                   Len := Write
335                     (Macro_File, Second_Line (Second_Line'First)'Address,
336                      Second_Line'Length);
337                   OK := Len = Second_Line'Length;
338                end if;
339
340                if OK then
341                   Close (Macro_File, OK);
342                end if;
343
344                if not OK then
345                   Fail ("creation of auto-init assembly file """,
346                         Macro_File_Name, """ failed");
347                end if;
348             end;
349
350             --  Invoke the macro-assembler
351
352             if Verbose_Mode then
353                Write_Str ("Assembling auto-init assembly file """);
354                Write_Str (Macro_File_Name);
355                Write_Line ("""");
356             end if;
357
358             Popen_Result := Popen (Command (Command'First)'Address,
359                                    Mode (Mode'First)'Address);
360
361             if Popen_Result = Null_Address then
362                Fail ("assembly of auto-init assembly file """,
363                      Macro_File_Name, """ failed");
364             end if;
365
366             --  Wait for the end of execution of the macro-assembler
367
368             Pclose_Result := Pclose (Popen_Result);
369
370             if Pclose_Result < 0 then
371                Fail ("assembly of auto init assembly file """,
372                      Macro_File_Name, """ failed");
373             end if;
374
375             --  Add the generated object file to the list of objects to be
376             --  included in the library.
377
378             Additional_Objects :=
379               new Argument_List'
380                 (1 => new String'(Lib_Filename & "$init.obj"));
381          end;
382       end if;
383
384       --  Allocate the argument list and put the symbol file name, the
385       --  reference (if any) and the policy (if not autonomous).
386
387       Arguments := new Argument_List (1 .. Ofiles'Length + 8);
388
389       Last_Argument := 0;
390
391       --  Verbosity
392
393       if Verbose_Mode then
394          Last_Argument := Last_Argument + 1;
395          Arguments (Last_Argument) := new String'("-v");
396       end if;
397
398       --  Version number (major ID)
399
400       if Lib_Version /= "" then
401          Last_Argument := Last_Argument + 1;
402          Arguments (Last_Argument) := new String'("-V");
403          Last_Argument := Last_Argument + 1;
404          Arguments (Last_Argument) := new String'(Version);
405       end if;
406
407       --  Symbol file
408
409       Last_Argument := Last_Argument + 1;
410       Arguments (Last_Argument) := new String'("-s");
411       Last_Argument := Last_Argument + 1;
412       Arguments (Last_Argument) := new String'(Opt_File_Name);
413
414       --  Reference Symbol File
415
416       if Symbol_Data.Reference /= No_Name then
417          Last_Argument := Last_Argument + 1;
418          Arguments (Last_Argument) := new String'("-r");
419          Last_Argument := Last_Argument + 1;
420          Arguments (Last_Argument) :=
421            new String'(Get_Name_String (Symbol_Data.Reference));
422       end if;
423
424       --  Policy
425
426       case Symbol_Data.Symbol_Policy is
427          when Autonomous =>
428             null;
429
430          when Compliant =>
431             Last_Argument := Last_Argument + 1;
432             Arguments (Last_Argument) := new String'("-c");
433
434          when Controlled =>
435             Last_Argument := Last_Argument + 1;
436             Arguments (Last_Argument) := new String'("-C");
437
438          when Restricted =>
439             Last_Argument := Last_Argument + 1;
440             Arguments (Last_Argument) := new String'("-R");
441       end case;
442
443       --  Add each relevant object file
444
445       for Index in Ofiles'Range loop
446          if Is_Interface (Ofiles (Index).all) then
447             Last_Argument := Last_Argument + 1;
448             Arguments (Last_Argument) := new String'(Ofiles (Index).all);
449          end if;
450       end loop;
451
452       --  Spawn gnatsym
453
454       Spawn (Program_Name => Gnatsym_Path.all,
455              Args         => Arguments (1 .. Last_Argument),
456              Success      => Success);
457
458       if not Success then
459          Fail ("unable to create symbol file for library """,
460                Lib_Filename, """");
461       end if;
462
463       Free (Arguments);
464
465       --  Move all the -l switches from Opts to Opts2
466
467       declare
468          Index : Natural := Opts'First;
469          Opt   : String_Access;
470
471       begin
472          while Index <= Last_Opt loop
473             Opt := Opts (Index);
474
475             if Opt'Length > 2 and then
476               Opt (Opt'First .. Opt'First + 1) = "-l"
477             then
478                if Index < Last_Opt then
479                   Opts (Index .. Last_Opt - 1) :=
480                     Opts (Index + 1 .. Last_Opt);
481                end if;
482
483                Last_Opt := Last_Opt - 1;
484
485                Last_Opt2 := Last_Opt2 + 1;
486                Opts2 (Last_Opt2) := Opt;
487
488             else
489                Index := Index + 1;
490             end if;
491          end loop;
492       end;
493
494       --  Invoke gcc to build the library
495
496       Utl.Gcc
497         (Output_File => Lib_File,
498          Objects     => Ofiles & Additional_Objects.all,
499          Options     => VMS_Options,
500          Options_2   => Link_With_Shared_Libgcc.all &
501                         Opts (Opts'First .. Last_Opt) &
502                         Opts2 (Opts2'First .. Last_Opt2),
503          Driver_Name => Driver_Name);
504
505       --  The auto-init object file need to be deleted, so that it will not
506       --  be included in the library as a regular object file, otherwise
507       --  it will be included twice when the library will be built next
508       --  time, which may lead to errors.
509
510       if Auto_Init then
511          declare
512             Auto_Init_Object_File_Name : constant String :=
513                                            Lib_Filename & "$init.obj";
514             Disregard : Boolean;
515
516          begin
517             if Verbose_Mode then
518                Write_Str ("deleting auto-init object file """);
519                Write_Str (Auto_Init_Object_File_Name);
520                Write_Line ("""");
521             end if;
522
523             Delete_File (Auto_Init_Object_File_Name, Success => Disregard);
524          end;
525       end if;
526    end Build_Dynamic_Library;
527
528    -------------
529    -- DLL_Ext --
530    -------------
531
532    function DLL_Ext return String is
533    begin
534       return "exe";
535    end DLL_Ext;
536
537    --------------------
538    -- Dynamic_Option --
539    --------------------
540
541    function Dynamic_Option return String is
542    begin
543       return "-shared";
544    end Dynamic_Option;
545
546    -------------------
547    -- Is_Object_Ext --
548    -------------------
549
550    function Is_Object_Ext (Ext : String) return Boolean is
551    begin
552       return Ext = ".obj";
553    end Is_Object_Ext;
554
555    --------------
556    -- Is_C_Ext --
557    --------------
558
559    function Is_C_Ext (Ext : String) return Boolean is
560    begin
561       return Ext = ".c";
562    end Is_C_Ext;
563
564    --------------------
565    -- Is_Archive_Ext --
566    --------------------
567
568    function Is_Archive_Ext (Ext : String) return Boolean is
569    begin
570       return Ext = ".olb" or else Ext = ".exe";
571    end Is_Archive_Ext;
572
573    -------------
574    -- Libgnat --
575    -------------
576
577    function Libgnat return String is
578       Libgnat_A : constant String := "libgnat.a";
579       Libgnat_Olb : constant String := "libgnat.olb";
580
581    begin
582       Name_Len := Libgnat_A'Length;
583       Name_Buffer (1 .. Name_Len) := Libgnat_A;
584
585       if Osint.Find_File (Name_Enter, Osint.Library) /= No_File then
586          return Libgnat_A;
587
588       else
589          return Libgnat_Olb;
590       end if;
591    end Libgnat;
592
593    ------------------------
594    -- Library_Exists_For --
595    ------------------------
596
597    function Library_Exists_For (Project : Project_Id) return Boolean is
598    begin
599       if not Projects.Table (Project).Library then
600          Fail ("INTERNAL ERROR: Library_Exists_For called " &
601                "for non library project");
602          return False;
603
604       else
605          declare
606             Lib_Dir : constant String :=
607               Get_Name_String (Projects.Table (Project).Library_Dir);
608             Lib_Name : constant String :=
609               Get_Name_String (Projects.Table (Project).Library_Name);
610
611          begin
612             if Projects.Table (Project).Library_Kind = Static then
613                return Is_Regular_File
614                  (Lib_Dir & Directory_Separator & "lib" &
615                   Fil.Ext_To (Lib_Name, Archive_Ext));
616
617             else
618                return Is_Regular_File
619                  (Lib_Dir & Directory_Separator & "lib" &
620                   Fil.Ext_To (Lib_Name, DLL_Ext));
621             end if;
622          end;
623       end if;
624    end Library_Exists_For;
625
626    ---------------------------
627    -- Library_File_Name_For --
628    ---------------------------
629
630    function Library_File_Name_For (Project : Project_Id) return Name_Id is
631    begin
632       if not Projects.Table (Project).Library then
633          Prj.Com.Fail ("INTERNAL ERROR: Library_File_Name_For called " &
634                        "for non library project");
635          return No_Name;
636
637       else
638          declare
639             Lib_Name : constant String :=
640               Get_Name_String (Projects.Table (Project).Library_Name);
641
642          begin
643             Name_Len := 3;
644             Name_Buffer (1 .. Name_Len) := "lib";
645
646             if Projects.Table (Project).Library_Kind = Static then
647                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, Archive_Ext));
648
649             else
650                Add_Str_To_Name_Buffer (Fil.Ext_To (Lib_Name, DLL_Ext));
651             end if;
652
653             return Name_Find;
654          end;
655       end if;
656    end Library_File_Name_For;
657
658    ----------------
659    -- Object_Ext --
660    ----------------
661
662    function Object_Ext return String is
663    begin
664       return "obj";
665    end Object_Ext;
666
667    ----------------
668    -- PIC_Option --
669    ----------------
670
671    function PIC_Option return String is
672    begin
673       return "";
674    end PIC_Option;
675
676    -----------------------------------------------
677    -- Standalone_Library_Auto_Init_Is_Supported --
678    -----------------------------------------------
679
680    function Standalone_Library_Auto_Init_Is_Supported return Boolean is
681    begin
682       return True;
683    end Standalone_Library_Auto_Init_Is_Supported;
684
685    ---------------------------
686    -- Support_For_Libraries --
687    ---------------------------
688
689    function Support_For_Libraries return Library_Support is
690    begin
691       return Full;
692    end Support_For_Libraries;
693
694 end MLib.Tgt;