Fix mis-recognition of 'mcs' on QNX 6.
[gnulib.git] / lib / csharpcomp.c
1 /* Compile a C# program.
2    Copyright (C) 2003-2007 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2003.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2, or (at your option)
8    any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #include <config.h>
20 #include <alloca.h>
21
22 /* Specification.  */
23 #include "csharpcomp.h"
24
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "execute.h"
31 #include "pipe.h"
32 #include "wait-process.h"
33 #include "sh-quote.h"
34 #include "safe-read.h"
35 #include "xmalloca.h"
36 #include "error.h"
37 #include "gettext.h"
38
39 #define _(str) gettext (str)
40
41
42 /* Survey of C# compilers.
43
44    Program    from
45
46    cscc       pnet
47    mcs        mono
48    csc        sscli
49
50    We try the CIL interpreters in the following order:
51      1. "cscc", because it is a completely free system.
52      2. "mcs", because it is a free system but doesn't integrate so well
53         with Unix. (Command line options start with / instead of -. Errors go
54         to stdout instead of stderr. Source references are printed as
55         "file(lineno)" instead of "file:lineno:".)
56      3. "csc", although it is not free, because it is a kind of "reference
57         implementation" of C#.
58    But the order can be changed through the --enable-csharp configuration
59    option.
60  */
61
62 static int
63 compile_csharp_using_pnet (const char * const *sources,
64                            unsigned int sources_count,
65                            const char * const *libdirs,
66                            unsigned int libdirs_count,
67                            const char * const *libraries,
68                            unsigned int libraries_count,
69                            const char *output_file, bool output_is_library,
70                            bool optimize, bool debug,
71                            bool verbose)
72 {
73   static bool cscc_tested;
74   static bool cscc_present;
75
76   if (!cscc_tested)
77     {
78       /* Test for presence of cscc:
79          "cscc --version >/dev/null 2>/dev/null"  */
80       char *argv[3];
81       int exitstatus;
82
83       argv[0] = "cscc";
84       argv[1] = "--version";
85       argv[2] = NULL;
86       exitstatus = execute ("cscc", "cscc", argv, false, false, true, true,
87                             true, false);
88       cscc_present = (exitstatus == 0);
89       cscc_tested = true;
90     }
91
92   if (cscc_present)
93     {
94       unsigned int argc;
95       char **argv;
96       char **argp;
97       int exitstatus;
98       unsigned int i;
99
100       argc =
101         1 + (output_is_library ? 1 : 0) + 2 + 2 * libdirs_count
102         + 2 * libraries_count + (optimize ? 1 : 0) + (debug ? 1 : 0)
103         + sources_count;
104       argv = (char **) xmalloca ((argc + 1) * sizeof (char *));
105
106       argp = argv;
107       *argp++ = "cscc";
108       if (output_is_library)
109         *argp++ = "-shared";
110       *argp++ = "-o";
111       *argp++ = (char *) output_file;
112       for (i = 0; i < libdirs_count; i++)
113         {
114           *argp++ = "-L";
115           *argp++ = (char *) libdirs[i];
116         }
117       for (i = 0; i < libraries_count; i++)
118         {
119           *argp++ = "-l";
120           *argp++ = (char *) libraries[i];
121         }
122       if (optimize)
123         *argp++ = "-O";
124       if (debug)
125         *argp++ = "-g";
126       for (i = 0; i < sources_count; i++)
127         {
128           const char *source_file = sources[i];
129           if (strlen (source_file) >= 10
130               && memcmp (source_file + strlen (source_file) - 10, ".resources",
131                          10) == 0)
132             {
133               char *option = (char *) xmalloca (12 + strlen (source_file) + 1);
134
135               memcpy (option, "-fresources=", 12);
136               strcpy (option + 12, source_file);
137               *argp++ = option;
138             }
139           else
140             *argp++ = (char *) source_file;
141         }
142       *argp = NULL;
143       /* Ensure argv length was correctly calculated.  */
144       if (argp - argv != argc)
145         abort ();
146
147       if (verbose)
148         {
149           char *command = shell_quote_argv (argv);
150           printf ("%s\n", command);
151           free (command);
152         }
153
154       exitstatus = execute ("cscc", "cscc", argv, false, false, false, false,
155                             true, true);
156
157       for (i = 0; i < sources_count; i++)
158         if (argv[argc - sources_count + i] != sources[i])
159           freea (argv[argc - sources_count + i]);
160       freea (argv);
161
162       return (exitstatus != 0);
163     }
164   else
165     return -1;
166 }
167
168 static int
169 compile_csharp_using_mono (const char * const *sources,
170                            unsigned int sources_count,
171                            const char * const *libdirs,
172                            unsigned int libdirs_count,
173                            const char * const *libraries,
174                            unsigned int libraries_count,
175                            const char *output_file, bool output_is_library,
176                            bool optimize, bool debug,
177                            bool verbose)
178 {
179   static bool mcs_tested;
180   static bool mcs_present;
181
182   if (!mcs_tested)
183     {
184       /* Test for presence of mcs:
185          "mcs --version >/dev/null 2>/dev/null"
186          and (to exclude an unrelated 'mcs' program on QNX 6)
187          "mcs --version 2>/dev/null | grep Mono >/dev/null"  */
188       char *argv[3];
189       pid_t child;
190       int fd[1];
191       int exitstatus;
192
193       argv[0] = "mcs";
194       argv[1] = "--version";
195       argv[2] = NULL;
196       child = create_pipe_in ("mcs", "mcs", argv, DEV_NULL, true, true, false,
197                               fd);
198       mcs_present = false;
199       if (child != -1)
200         {
201           /* Read the subprocess output, and test whether it contains the
202              string "Mono".  */
203           char c[4];
204           size_t count = 0;
205
206           while (safe_read (fd[0], &c[count], 1) > 0)
207             {
208               count++;
209               if (count == 4)
210                 {
211                   if (memcmp (c, "Mono", 4) == 0)
212                     mcs_present = true;
213                   c[0] = c[1]; c[1] = c[2]; c[2] = c[3];
214                   count--;
215                 }
216             }
217
218           close (fd[0]);
219
220           /* Remove zombie process from process list, and retrieve exit
221              status.  */
222           exitstatus =
223             wait_subprocess (child, "mcs", false, true, true, false);
224           if (exitstatus != 0)
225             mcs_present = false;
226         }
227       mcs_tested = true;
228     }
229
230   if (mcs_present)
231     {
232       unsigned int argc;
233       char **argv;
234       char **argp;
235       pid_t child;
236       int fd[1];
237       FILE *fp;
238       char *line[2];
239       size_t linesize[2];
240       size_t linelen[2];
241       unsigned int l;
242       int exitstatus;
243       unsigned int i;
244
245       argc =
246         1 + (output_is_library ? 1 : 0) + 1 + libdirs_count + libraries_count
247         + (debug ? 1 : 0) + sources_count;
248       argv = (char **) xmalloca ((argc + 1) * sizeof (char *));
249
250       argp = argv;
251       *argp++ = "mcs";
252       if (output_is_library)
253         *argp++ = "-target:library";
254       {
255         char *option = (char *) xmalloca (5 + strlen (output_file) + 1);
256         memcpy (option, "-out:", 5);
257         strcpy (option + 5, output_file);
258         *argp++ = option;
259       }
260       for (i = 0; i < libdirs_count; i++)
261         {
262           char *option = (char *) xmalloca (5 + strlen (libdirs[i]) + 1);
263           memcpy (option, "-lib:", 5);
264           strcpy (option + 5, libdirs[i]);
265           *argp++ = option;
266         }
267       for (i = 0; i < libraries_count; i++)
268         {
269           char *option = (char *) xmalloca (11 + strlen (libraries[i]) + 4 + 1);
270           memcpy (option, "-reference:", 11);
271           memcpy (option + 11, libraries[i], strlen (libraries[i]));
272           strcpy (option + 11 + strlen (libraries[i]), ".dll");
273           *argp++ = option;
274         }
275       if (debug)
276         *argp++ = "-debug";
277       for (i = 0; i < sources_count; i++)
278         {
279           const char *source_file = sources[i];
280           if (strlen (source_file) >= 10
281               && memcmp (source_file + strlen (source_file) - 10, ".resources",
282                          10) == 0)
283             {
284               char *option = (char *) xmalloca (10 + strlen (source_file) + 1);
285
286               memcpy (option, "-resource:", 10);
287               strcpy (option + 10, source_file);
288               *argp++ = option;
289             }
290           else
291             *argp++ = (char *) source_file;
292         }
293       *argp = NULL;
294       /* Ensure argv length was correctly calculated.  */
295       if (argp - argv != argc)
296         abort ();
297
298       if (verbose)
299         {
300           char *command = shell_quote_argv (argv);
301           printf ("%s\n", command);
302           free (command);
303         }
304
305       child = create_pipe_in ("mcs", "mcs", argv, NULL, false, true, true, fd);
306
307       /* Read the subprocess output, copying it to stderr.  Drop the last
308          line if it starts with "Compilation succeeded".  */
309       fp = fdopen (fd[0], "r");
310       if (fp == NULL)
311         error (EXIT_FAILURE, errno, _("fdopen() failed"));
312       line[0] = NULL; linesize[0] = 0;
313       line[1] = NULL; linesize[1] = 0;
314       l = 0;
315       for (;;)
316         {
317           linelen[l] = getline (&line[l], &linesize[l], fp);
318           if (linelen[l] == (size_t)(-1))
319             break;
320           l = (l + 1) % 2;
321           if (line[l] != NULL)
322             fwrite (line[l], 1, linelen[l], stderr);
323         }
324       l = (l + 1) % 2;
325       if (line[l] != NULL
326           && !(linelen[l] >= 21
327                && memcmp (line[l], "Compilation succeeded", 21) == 0))
328         fwrite (line[l], 1, linelen[l], stderr);
329       if (line[0] != NULL)
330         free (line[0]);
331       if (line[1] != NULL)
332         free (line[1]);
333       fclose (fp);
334
335       /* Remove zombie process from process list, and retrieve exit status.  */
336       exitstatus = wait_subprocess (child, "mcs", false, false, true, true);
337
338       for (i = 1 + (output_is_library ? 1 : 0);
339            i < 1 + (output_is_library ? 1 : 0)
340                + 1 + libdirs_count + libraries_count;
341            i++)
342         freea (argv[i]);
343       for (i = 0; i < sources_count; i++)
344         if (argv[argc - sources_count + i] != sources[i])
345           freea (argv[argc - sources_count + i]);
346       freea (argv);
347
348       return (exitstatus != 0);
349     }
350   else
351     return -1;
352 }
353
354 static int
355 compile_csharp_using_sscli (const char * const *sources,
356                             unsigned int sources_count,
357                             const char * const *libdirs,
358                             unsigned int libdirs_count,
359                             const char * const *libraries,
360                             unsigned int libraries_count,
361                             const char *output_file, bool output_is_library,
362                             bool optimize, bool debug,
363                             bool verbose)
364 {
365   static bool csc_tested;
366   static bool csc_present;
367
368   if (!csc_tested)
369     {
370       /* Test for presence of csc:
371          "csc -help >/dev/null 2>/dev/null \
372           && ! { csc -help 2>/dev/null | grep -i chicken > /dev/null; }"  */
373       char *argv[3];
374       pid_t child;
375       int fd[1];
376       int exitstatus;
377
378       argv[0] = "csc";
379       argv[1] = "-help";
380       argv[2] = NULL;
381       child = create_pipe_in ("csc", "csc", argv, DEV_NULL, true, true, false,
382                               fd);
383       csc_present = false;
384       if (child != -1)
385         {
386           /* Read the subprocess output, and test whether it contains the
387              string "chicken".  */
388           char c[7];
389           size_t count = 0;
390
391           csc_present = true;
392           while (safe_read (fd[0], &c[count], 1) > 0)
393             {
394               if (c[count] >= 'A' && c[count] <= 'Z')
395                 c[count] += 'a' - 'A';
396               count++;
397               if (count == 7)
398                 {
399                   if (memcmp (c, "chicken", 7) == 0)
400                     csc_present = false;
401                   c[0] = c[1]; c[1] = c[2]; c[2] = c[3];
402                   c[3] = c[4]; c[4] = c[5]; c[5] = c[6];
403                   count--;
404                 }
405             }
406
407           close (fd[0]);
408
409           /* Remove zombie process from process list, and retrieve exit
410              status.  */
411           exitstatus =
412             wait_subprocess (child, "csc", false, true, true, false);
413           if (exitstatus != 0)
414             csc_present = false;
415         }
416       csc_tested = true;
417     }
418
419   if (csc_present)
420     {
421       unsigned int argc;
422       char **argv;
423       char **argp;
424       int exitstatus;
425       unsigned int i;
426
427       argc =
428         1 + 1 + 1 + libdirs_count + libraries_count
429         + (optimize ? 1 : 0) + (debug ? 1 : 0) + sources_count;
430       argv = (char **) xmalloca ((argc + 1) * sizeof (char *));
431
432       argp = argv;
433       *argp++ = "csc";
434       *argp++ =
435         (char *) (output_is_library ? "-target:library" : "-target:exe");
436       {
437         char *option = (char *) xmalloca (5 + strlen (output_file) + 1);
438         memcpy (option, "-out:", 5);
439         strcpy (option + 5, output_file);
440         *argp++ = option;
441       }
442       for (i = 0; i < libdirs_count; i++)
443         {
444           char *option = (char *) xmalloca (5 + strlen (libdirs[i]) + 1);
445           memcpy (option, "-lib:", 5);
446           strcpy (option + 5, libdirs[i]);
447           *argp++ = option;
448         }
449       for (i = 0; i < libraries_count; i++)
450         {
451           char *option = (char *) xmalloca (11 + strlen (libraries[i]) + 4 + 1);
452           memcpy (option, "-reference:", 11);
453           memcpy (option + 11, libraries[i], strlen (libraries[i]));
454           strcpy (option + 11 + strlen (libraries[i]), ".dll");
455           *argp++ = option;
456         }
457       if (optimize)
458         *argp++ = "-optimize+";
459       if (debug)
460         *argp++ = "-debug+";
461       for (i = 0; i < sources_count; i++)
462         {
463           const char *source_file = sources[i];
464           if (strlen (source_file) >= 10
465               && memcmp (source_file + strlen (source_file) - 10, ".resources",
466                          10) == 0)
467             {
468               char *option = (char *) xmalloca (10 + strlen (source_file) + 1);
469
470               memcpy (option, "-resource:", 10);
471               strcpy (option + 10, source_file);
472               *argp++ = option;
473             }
474           else
475             *argp++ = (char *) source_file;
476         }
477       *argp = NULL;
478       /* Ensure argv length was correctly calculated.  */
479       if (argp - argv != argc)
480         abort ();
481
482       if (verbose)
483         {
484           char *command = shell_quote_argv (argv);
485           printf ("%s\n", command);
486           free (command);
487         }
488
489       exitstatus = execute ("csc", "csc", argv, false, false, false, false,
490                             true, true);
491
492       for (i = 2; i < 3 + libdirs_count + libraries_count; i++)
493         freea (argv[i]);
494       for (i = 0; i < sources_count; i++)
495         if (argv[argc - sources_count + i] != sources[i])
496           freea (argv[argc - sources_count + i]);
497       freea (argv);
498
499       return (exitstatus != 0);
500     }
501   else
502     return -1;
503 }
504
505 bool
506 compile_csharp_class (const char * const *sources,
507                       unsigned int sources_count,
508                       const char * const *libdirs,
509                       unsigned int libdirs_count,
510                       const char * const *libraries,
511                       unsigned int libraries_count,
512                       const char *output_file,
513                       bool optimize, bool debug,
514                       bool verbose)
515 {
516   bool output_is_library =
517     (strlen (output_file) >= 4
518      && memcmp (output_file + strlen (output_file) - 4, ".dll", 4) == 0);
519   int result;
520
521   /* First try the C# implementation specified through --enable-csharp.  */
522 #if CSHARP_CHOICE_PNET
523   result = compile_csharp_using_pnet (sources, sources_count,
524                                       libdirs, libdirs_count,
525                                       libraries, libraries_count,
526                                       output_file, output_is_library,
527                                       optimize, debug, verbose);
528   if (result >= 0)
529     return (bool) result;
530 #endif
531
532 #if CSHARP_CHOICE_MONO
533   result = compile_csharp_using_mono (sources, sources_count,
534                                       libdirs, libdirs_count,
535                                       libraries, libraries_count,
536                                       output_file, output_is_library,
537                                       optimize, debug, verbose);
538   if (result >= 0)
539     return (bool) result;
540 #endif
541
542   /* Then try the remaining C# implementations in our standard order.  */
543 #if !CSHARP_CHOICE_PNET
544   result = compile_csharp_using_pnet (sources, sources_count,
545                                       libdirs, libdirs_count,
546                                       libraries, libraries_count,
547                                       output_file, output_is_library,
548                                       optimize, debug, verbose);
549   if (result >= 0)
550     return (bool) result;
551 #endif
552
553 #if !CSHARP_CHOICE_MONO
554   result = compile_csharp_using_mono (sources, sources_count,
555                                       libdirs, libdirs_count,
556                                       libraries, libraries_count,
557                                       output_file, output_is_library,
558                                       optimize, debug, verbose);
559   if (result >= 0)
560     return (bool) result;
561 #endif
562
563   result = compile_csharp_using_sscli (sources, sources_count,
564                                        libdirs, libdirs_count,
565                                        libraries, libraries_count,
566                                        output_file, output_is_library,
567                                        optimize, debug, verbose);
568   if (result >= 0)
569     return (bool) result;
570
571   error (0, 0, _("C# compiler not found, try installing pnet"));
572   return true;
573 }