aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/gcc/ada/mdll-utl.adb
blob: 85bc2a3a63b154a37fc099861a331770e3c538a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
------------------------------------------------------------------------------
--                                                                          --
--                         GNAT COMPILER COMPONENTS                         --
--                                                                          --
--                            M D L L . T O O L S                           --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--          Copyright (C) 1992-2008, Free Software Foundation, Inc.         --
--                                                                          --
-- GNAT is free software;  you can  redistribute it  and/or modify it under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
-- for  more details.  You should have  received  a copy of the GNU General --
-- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
-- http://www.gnu.org/licenses for a complete copy of the license.          --
--                                                                          --
-- GNAT was originally developed  by the GNAT team at  New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
--                                                                          --
------------------------------------------------------------------------------

--  Interface to externals tools used to build DLL and import libraries

with Ada.Text_IO;
with Ada.Exceptions;

with GNAT.Directory_Operations;
with Osint;

package body MDLL.Utl is

   use Ada;
   use GNAT;

   Dlltool_Name  : constant String := "dlltool";
   Dlltool_Exec  : OS_Lib.String_Access;

   Gcc_Name      : constant String := "gcc";
   Gcc_Exec      : OS_Lib.String_Access;

   Gnatbind_Name : constant String := "gnatbind";
   Gnatbind_Exec : OS_Lib.String_Access;

   Gnatlink_Name : constant String := "gnatlink";
   Gnatlink_Exec : OS_Lib.String_Access;

   procedure Print_Command
     (Tool_Name : String;
      Arguments : OS_Lib.Argument_List);
   --  display the command run when in Verbose mode

   -------------------
   -- Print_Command --
   -------------------

   procedure Print_Command
     (Tool_Name : String;
      Arguments : OS_Lib.Argument_List)
   is
   begin
      if Verbose then
         Text_IO.Put (Tool_Name);
         for K in Arguments'Range loop
            Text_IO.Put (" " & Arguments (K).all);
         end loop;
         Text_IO.New_Line;
      end if;
   end Print_Command;

   -------------
   -- Dlltool --
   -------------

   procedure Dlltool
     (Def_Filename : String;
      DLL_Name     : String;
      Library      : String;
      Exp_Table    : String := "";
      Base_File    : String := "";
      Build_Import : Boolean)
   is
      Arguments  : OS_Lib.Argument_List (1 .. 11);
      A          : Positive;

      Success    : Boolean;

      Def_Opt    : aliased String := "--def";
      Def_V      : aliased String := Def_Filename;
      Dll_Opt    : aliased String := "--dllname";
      Dll_V      : aliased String := DLL_Name;
      Lib_Opt    : aliased String := "--output-lib";
      Lib_V      : aliased String := Library;
      Exp_Opt    : aliased String := "--output-exp";
      Exp_V      : aliased String := Exp_Table;
      Bas_Opt    : aliased String := "--base-file";
      Bas_V      : aliased String := Base_File;
      No_Suf_Opt : aliased String := "-k";

   begin
      Arguments (1 .. 4) := (1 => Def_Opt'Unchecked_Access,
                             2 => Def_V'Unchecked_Access,
                             3 => Dll_Opt'Unchecked_Access,
                             4 => Dll_V'Unchecked_Access);
      A := 4;

      if Kill_Suffix then
         A := A + 1;
         Arguments (A) := No_Suf_Opt'Unchecked_Access;
      end if;

      if Library /= "" and then Build_Import then
         A := A + 1;
         Arguments (A) := Lib_Opt'Unchecked_Access;
         A := A + 1;
         Arguments (A) := Lib_V'Unchecked_Access;
      end if;

      if Exp_Table /= "" then
         A := A + 1;
         Arguments (A) := Exp_Opt'Unchecked_Access;
         A := A + 1;
         Arguments (A) := Exp_V'Unchecked_Access;
      end if;

      if Base_File /= "" then
         A := A + 1;
         Arguments (A) := Bas_Opt'Unchecked_Access;
         A := A + 1;
         Arguments (A) := Bas_V'Unchecked_Access;
      end if;

      Print_Command ("dlltool", Arguments (1 .. A));

      OS_Lib.Spawn (Dlltool_Exec.all, Arguments (1 .. A), Success);

      if not Success then
         Exceptions.Raise_Exception
           (Tools_Error'Identity, Dlltool_Name & " execution error.");
      end if;
   end Dlltool;

   ---------
   -- Gcc --
   ---------

   procedure Gcc
     (Output_File : String;
      Files       : Argument_List;
      Options     : Argument_List;
      Base_File   : String := "";
      Build_Lib   : Boolean := False)
   is
      use Osint;

      Arguments : OS_Lib.Argument_List
        (1 .. 5 + Files'Length + Options'Length);
      A         : Natural := 0;

      Success   : Boolean;
      C_Opt     : aliased String := "-c";
      Out_Opt   : aliased String := "-o";
      Out_V     : aliased String := Output_File;
      Bas_Opt   : aliased String := "-Wl,--base-file," & Base_File;
      Lib_Opt   : aliased String := "-mdll";
      Lib_Dir   : aliased String := "-L" & Object_Dir_Default_Prefix;

   begin
      A := A + 1;
      if Build_Lib then
         Arguments (A) := Lib_Opt'Unchecked_Access;
      else
         Arguments (A) := C_Opt'Unchecked_Access;
      end if;

      A := A + 1;
      Arguments (A .. A + 2) := (Out_Opt'Unchecked_Access,
                                 Out_V'Unchecked_Access,
                                 Lib_Dir'Unchecked_Access);
      A := A + 2;

      if Base_File /= "" then
         A := A + 1;
         Arguments (A) := Bas_Opt'Unchecked_Access;
      end if;

      A := A + 1;
      Arguments (A .. A + Files'Length - 1) := Files;
      A := A + Files'Length - 1;

      if Build_Lib then
         A := A + 1;
         Arguments (A .. A + Options'Length - 1) := Options;
         A := A + Options'Length - 1;
      else
         declare
            Largs : Argument_List (Options'Range);
            L     : Natural := Largs'First - 1;
         begin
            for K in Options'Range loop
               if Options (K) (1 .. 2) /= "-l" then
                  L := L + 1;
                  Largs (L) := Options (K);
               end if;
            end loop;
            A := A + 1;
            Arguments (A .. A + L - 1) := Largs (1 .. L);
            A := A + L - 1;
         end;
      end if;

      Print_Command ("gcc", Arguments (1 .. A));

      OS_Lib.Spawn (Gcc_Exec.all, Arguments (1 .. A), Success);

      if not Success then
         Exceptions.Raise_Exception
           (Tools_Error'Identity, Gcc_Name & " execution error.");
      end if;
   end Gcc;

   --------------
   -- Gnatbind --
   --------------

   procedure Gnatbind
     (Alis : Argument_List;
      Args : Argument_List := Null_Argument_List)
   is
      Arguments   : OS_Lib.Argument_List (1 .. 1 + Alis'Length + Args'Length);
      Success     : Boolean;

      No_Main_Opt : aliased String := "-n";

   begin
      Arguments (1) := No_Main_Opt'Unchecked_Access;
      Arguments (2 .. 1 + Alis'Length) := Alis;
      Arguments (2 + Alis'Length .. Arguments'Last) := Args;

      Print_Command ("gnatbind", Arguments);

      OS_Lib.Spawn (Gnatbind_Exec.all, Arguments, Success);

      --  Delete binder files on failure

      if not Success then
         declare
            Base_Name : constant String :=
              Directory_Operations.Base_Name (Alis (Alis'First).all, ".ali");
         begin
            OS_Lib.Delete_File ("b~" & Base_Name & ".ads", Success);
            OS_Lib.Delete_File ("b~" & Base_Name & ".adb", Success);
         end;

         Exceptions.Raise_Exception
           (Tools_Error'Identity, Gnatbind_Name & " execution error.");
      end if;
   end Gnatbind;

   --------------
   -- Gnatlink --
   --------------

   procedure Gnatlink
     (Ali  : String;
      Args : Argument_List := Null_Argument_List)
   is
      Arguments : OS_Lib.Argument_List (1 .. 1 + Args'Length);
      Success   : Boolean;

      Ali_Name  : aliased String := Ali;

   begin
      Arguments (1) := Ali_Name'Unchecked_Access;
      Arguments (2 .. Arguments'Last) := Args;

      Print_Command ("gnatlink", Arguments);

      OS_Lib.Spawn (Gnatlink_Exec.all, Arguments, Success);

      if not Success then
         --  Delete binder files
         declare
            Base_Name : constant String :=
                          Directory_Operations.Base_Name (Ali, ".ali");
         begin
            OS_Lib.Delete_File ("b~" & Base_Name & ".ads", Success);
            OS_Lib.Delete_File ("b~" & Base_Name & ".adb", Success);
            OS_Lib.Delete_File ("b~" & Base_Name & ".ali", Success);
            OS_Lib.Delete_File ("b~" & Base_Name & ".o", Success);
         end;

         Exceptions.Raise_Exception
           (Tools_Error'Identity, Gnatlink_Name & " execution error.");
      end if;
   end Gnatlink;

   ------------
   -- Locate --
   ------------

   procedure Locate is
      use type OS_Lib.String_Access;
   begin
      --  dlltool

      if Dlltool_Exec = null then
         Dlltool_Exec := OS_Lib.Locate_Exec_On_Path (Dlltool_Name);

         if Dlltool_Exec = null then
            Exceptions.Raise_Exception
              (Tools_Error'Identity, Dlltool_Name & " not found in path");

         elsif Verbose then
            Text_IO.Put_Line ("using " & Dlltool_Exec.all);
         end if;
      end if;

      --  gcc

      if Gcc_Exec = null then
         Gcc_Exec := OS_Lib.Locate_Exec_On_Path (Gcc_Name);

         if Gcc_Exec = null then
            Exceptions.Raise_Exception
              (Tools_Error'Identity, Gcc_Name & " not found in path");

         elsif Verbose then
            Text_IO.Put_Line ("using " & Gcc_Exec.all);
         end if;
      end if;

      --  gnatbind

      if Gnatbind_Exec = null then
         Gnatbind_Exec := OS_Lib.Locate_Exec_On_Path (Gnatbind_Name);

         if Gnatbind_Exec = null then
            Exceptions.Raise_Exception
              (Tools_Error'Identity, Gnatbind_Name & " not found in path");

         elsif Verbose then
            Text_IO.Put_Line ("using " & Gnatbind_Exec.all);
         end if;
      end if;

      --  gnatlink

      if Gnatlink_Exec = null then
         Gnatlink_Exec := OS_Lib.Locate_Exec_On_Path (Gnatlink_Name);

         if Gnatlink_Exec = null then
            Exceptions.Raise_Exception
              (Tools_Error'Identity, Gnatlink_Name & " not found in path");

         elsif Verbose then
            Text_IO.Put_Line ("using " & Gnatlink_Exec.all);
            Text_IO.New_Line;
         end if;
      end if;
   end Locate;

end MDLL.Utl;