OSDN Git Service

* reload1.c (reload_cse_simplify): Fix typo in rtx code check.
[pf3gnuchains/gcc-fork.git] / gcc / ada / gnatbl.c
1 /****************************************************************************
2  *                                                                          *
3  *                           GNAT COMPILER TOOLS                            *
4  *                                                                          *
5  *                               G N A T B L                                *
6  *                                                                          *
7  *                          C Implementation File                           *
8  *                                                                          *
9  *                                                                          *
10  *          Copyright (C) 1992-2001 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  * It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). *
25  *                                                                          *
26  ****************************************************************************/
27
28 #include "config.h"
29 #include "system.h"
30
31 #if defined (__EMX__) || defined (MSDOS)
32 #include <process.h>
33 #endif
34 #include "adaint.h"
35
36 #ifdef VMS
37 #ifdef exit
38 #undef exit
39 #endif
40 #define exit __posix_exit
41 #endif
42
43 /* These can be set by command line arguments */
44 char *binder_path = 0;
45 char *linker_path = 0;
46 char *exec_file_name = 0;
47 char *ali_file_name = 0;
48 #define BIND_ARG_MAX 512
49 char *bind_args[BIND_ARG_MAX];
50 int  bind_arg_index = -1;
51 #ifdef MSDOS
52 char *coff2exe_path = 0;
53 char *coff2exe_args[] = {(char *) 0, (char *) 0};
54 char *del_command = 0;
55 #endif
56 int  verbose      = 0;
57 int  o_present    = 0;
58 int  g_present    = 0;
59
60 int  link_arg_max = -1;
61 char **link_args = (char **) 0;
62 int  link_arg_index = -1;
63
64 char *gcc_B_arg = 0;
65
66 #ifndef DIR_SEPARATOR
67 #if defined (__EMX__)
68 #define DIR_SEPARATOR '\\'
69 #else
70 #define DIR_SEPARATOR '/'
71 #endif
72 #endif
73
74 static int linkonly = 0;
75
76 static void addarg              PARAMS ((char *));
77 static void process_args        PARAMS ((int *, char *[]));
78 \f
79 static void
80 addarg (str)
81      char *str;
82 {
83   int i;
84
85   if (++link_arg_index >= link_arg_max)
86     {
87       char **new_link_args
88         = (char **) xcalloc (link_arg_max + 1000, sizeof (char *));
89
90       for (i = 0; i <= link_arg_max; i++)
91         new_link_args[i] = link_args[i];
92
93       if (link_args)
94         free (link_args);
95
96       link_arg_max += 1000;
97       link_args = new_link_args;
98     }
99
100   link_args[link_arg_index] = str;
101 }
102
103 static void
104 process_args (p_argc, argv)
105      int *p_argc;
106      char *argv[];
107 {
108   int i, j;
109
110   for (i = 1; i < *p_argc; i++)
111     {
112       /* -I is passed on to gnatbind */
113       if (! strncmp( argv[i], "-I", 2))
114         {
115           bind_arg_index += 1;
116           if (bind_arg_index >= BIND_ARG_MAX)
117             {
118               fprintf (stderr, "Too many arguments to gnatbind\n");
119               exit (-1);
120       }
121
122           bind_args[bind_arg_index] = argv[i];
123         }
124
125       /* -B is passed on to gcc */
126       if (! strncmp (argv[i], "-B", 2))
127         gcc_B_arg = argv[i];
128
129       /* -v turns on verbose option here and is passed on to gcc */
130
131       if (! strcmp (argv[i], "-v"))
132         verbose = 1;
133
134       if (! strcmp (argv[i], "-o"))
135         {
136           o_present = 1;
137           exec_file_name = argv[i + 1];
138         }
139
140       if (! strcmp (argv[i], "-g"))
141         g_present = 1;
142
143       if (! strcmp (argv[i], "-gnatbind"))
144         {
145           /* Explicit naming of binder.  Grab the value then remove the
146              two arguments from the argument list. */
147           if ( i + 1 >= *p_argc )
148             {
149               fprintf (stderr, "Missing argument for -gnatbind\n");
150               exit (1);
151             }
152
153           binder_path = __gnat_locate_exec (argv[i + 1], (char *) ".");
154           if (!binder_path)
155             {
156               fprintf (stderr, "Could not locate binder: %s\n", argv[i + 1]);
157               exit (1);
158             }
159
160           for (j = i + 2; j < *p_argc; j++)
161             argv[j - 2] = argv[j];
162
163           (*p_argc) -= 2;
164           i--;
165         }
166
167     else if (! strcmp (argv[i], "-linkonly"))
168       {
169         /* Don't call the binder. Set the flag and then remove the
170            argument from the argument list. */
171         linkonly = 1;
172         for (j = i + 1; j < *p_argc; j++)
173           argv[j - 1] = argv[j];
174
175         *p_argc -= 1;
176         i--;
177       }
178
179     else if (! strcmp (argv[i], "-gnatlink"))
180       {
181         /* Explicit naming of binder.  Grab the value then remove the
182            two arguments from the argument list. */
183         if (i + 1 >= *p_argc)
184           {
185             fprintf (stderr, "Missing argument for -gnatlink\n");
186             exit (1);
187           }
188
189         linker_path = __gnat_locate_exec (argv[i + 1], (char *) ".");
190         if (!linker_path)
191           {
192             fprintf (stderr, "Could not locate linker: %s\n", argv[i + 1]);
193             exit (1);
194           }
195
196         for (j = i + 2; j < *p_argc; j++)
197           argv[j - 2] = argv[j];
198         *p_argc -= 2;
199         i--;
200       }
201     }
202 }
203 extern int main PARAMS ((int, char **));
204
205 int
206 main (argc, argv)
207      int argc;
208      char **argv;
209 {
210   int i, j;
211   int done_an_ali = 0;
212   int retcode;
213 #ifdef VMS
214   /* Warning: getenv only retrieves the first directory in VAXC$PATH */
215   char *pathval =
216     xstrdup (__gnat_to_canonical_dir_spec (getenv ("VAXC$PATH"), 0));
217 #else
218   char *pathval = getenv ("PATH");
219 #endif
220   char *spawn_args[5];
221   int  spawn_index = 0;
222
223 #if defined (__EMX__) || defined(MSDOS)
224   char *tmppathval = malloc (strlen (pathval) + 3);
225   strcpy (tmppathval, ".;");
226   pathval = strcat (tmppathval, pathval);
227 #endif
228
229   process_args (&argc , argv);
230
231   if (argc == 1)
232     {
233       fprintf
234         (stdout,
235          "Usage: %s 'name'.ali\n", argv[0]);
236       fprintf
237         (stdout,
238          "             [-o exec_name]        -- by default it is 'name'\n");
239       fprintf
240         (stdout,
241          "             [-v]                  -- verbose mode\n");
242       fprintf
243         (stdout,
244          "             [-linkonly]           -- doesn't call binder\n");
245       fprintf
246         (stdout,
247          "             [-gnatbind name]      -- full name for gnatbind\n");
248       fprintf
249         (stdout,
250          "             [-gnatlink name]      -- full name for linker (gcc)\n");
251       fprintf
252         (stdout,
253          "             [list of objects]     -- non Ada binaries\n");
254       fprintf
255         (stdout,
256          "             [linker options]      -- other options for linker\n");
257       exit (1);
258     }
259
260   if (!binder_path && !linkonly)
261     binder_path = __gnat_locate_exec ((char *) "gnatbind", pathval);
262
263   if (!binder_path && !linkonly)
264     {
265       fprintf (stderr, "Couldn't locate gnatbind\n");
266       exit (1);
267     }
268
269   if (!linker_path)
270     linker_path = __gnat_locate_exec ((char *) "gnatlink", pathval);
271     if (!linker_path)
272       {
273         fprintf (stderr, "Couldn't locate gnatlink\n");
274         exit (1);
275       }
276
277 #ifdef MSDOS
278   coff2exe_path = __gnat_locate_regular_file ("coff2exe.bat", pathval);
279   if (!coff2exe_path)
280     {
281       fprintf (stderr, "Couldn't locate %s\n", "coff2exe.bat");
282       exit (1);
283     }
284   else
285     coff2exe_args[0] = coff2exe_path;
286 #endif
287
288   addarg (linker_path);
289
290   for (i = 1; i < argc; i++)
291     {
292       int arg_len = strlen (argv[i]);
293
294       if (arg_len > 4 && ! strcmp (&argv[i][arg_len - 4], ".ali"))
295         {
296           if (done_an_ali)
297             {
298               fprintf (stderr, 
299                        "Sorry - cannot handle more than one ALI file\n");
300               exit (1);
301             }
302
303           done_an_ali = 1;
304
305           if (__gnat_is_regular_file (argv[i]))
306             {
307               ali_file_name = argv[i];
308               if (!linkonly)
309                 {
310                   /* Run gnatbind */
311                   spawn_index = 0;
312                   spawn_args[spawn_index++] = binder_path;
313                   spawn_args[spawn_index++] = ali_file_name;
314                   for (j = 0 ; j <= bind_arg_index ; j++ )
315                     spawn_args[spawn_index++] = bind_args[j];
316                   spawn_args[spawn_index] = 0;
317
318                   if (verbose)
319                     {
320                       int i;
321                       for (i = 0; i < 2; i++)
322                         printf ("%s ", spawn_args[i]);
323
324                       putchar ('\n');
325                     }
326
327                   retcode = __gnat_portable_spawn (spawn_args);
328                   if (retcode != 0)
329                     exit (retcode);
330                 }
331             }
332           else 
333             addarg (argv[i]);
334         }
335 #ifdef MSDOS
336       else if (!strcmp (argv[i], "-o"))
337         {
338           addarg (argv[i]);
339           if (i < argc)
340             i++;
341
342           {
343             char *ptr = strstr (argv[i], ".exe");
344
345             arg_len = strlen (argv[i]);
346             coff2exe_args[1] = malloc (arg_len + 1);
347             strcpy (coff2exe_args[1], argv[i]);
348             if (ptr != NULL && strlen (ptr) == 4)
349               coff2exe_args[1][arg_len-4] = 0;
350
351             addarg (coff2exe_args[1]);
352           }
353         }
354 #endif
355       else
356         addarg (argv[i]);
357     }
358
359   if (! done_an_ali)
360     {
361       fprintf (stderr, "No \".ali\" file specified\n");
362       exit (1);
363     }
364
365   addarg (ali_file_name);
366   addarg (NULL);
367
368   if (verbose)
369     {
370       int i;
371
372       for (i = 0; i < link_arg_index; i++)
373         printf ("%s ", link_args[i]);
374
375       putchar ('\n');
376     }
377
378   retcode = __gnat_portable_spawn (link_args);
379   if (retcode != 0)
380     exit (retcode);
381
382 #ifdef MSDOS
383   retcode = __gnat_portable_spawn (coff2exe_args);
384   if (retcode != 0)
385     exit (retcode);
386
387   if (!g_present)
388     {
389       del_command = malloc (strlen (coff2exe_args[1]) + 5);
390       sprintf (del_command, "del %s", coff2exe_args[1]);
391       retcode = system (del_command);
392     }
393 #endif
394
395   exit(0);
396 }