OSDN Git Service

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