Move getline and getdelim into stdio.h, per POSIX 200x.
[pspp] / 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       char *argv[3];
187       int exitstatus;
188
189       argv[0] = "mcs";
190       argv[1] = "--version";
191       argv[2] = NULL;
192       exitstatus = execute ("mcs", "mcs", argv, false, false, true, true, true,
193                             false);
194       mcs_present = (exitstatus == 0);
195       mcs_tested = true;
196     }
197
198   if (mcs_present)
199     {
200       unsigned int argc;
201       char **argv;
202       char **argp;
203       pid_t child;
204       int fd[1];
205       FILE *fp;
206       char *line[2];
207       size_t linesize[2];
208       size_t linelen[2];
209       unsigned int l;
210       int exitstatus;
211       unsigned int i;
212
213       argc =
214         1 + (output_is_library ? 1 : 0) + 1 + libdirs_count + libraries_count
215         + (debug ? 1 : 0) + sources_count;
216       argv = (char **) xmalloca ((argc + 1) * sizeof (char *));
217
218       argp = argv;
219       *argp++ = "mcs";
220       if (output_is_library)
221         *argp++ = "-target:library";
222       {
223         char *option = (char *) xmalloca (5 + strlen (output_file) + 1);
224         memcpy (option, "-out:", 5);
225         strcpy (option + 5, output_file);
226         *argp++ = option;
227       }
228       for (i = 0; i < libdirs_count; i++)
229         {
230           char *option = (char *) xmalloca (5 + strlen (libdirs[i]) + 1);
231           memcpy (option, "-lib:", 5);
232           strcpy (option + 5, libdirs[i]);
233           *argp++ = option;
234         }
235       for (i = 0; i < libraries_count; i++)
236         {
237           char *option = (char *) xmalloca (11 + strlen (libraries[i]) + 4 + 1);
238           memcpy (option, "-reference:", 11);
239           memcpy (option + 11, libraries[i], strlen (libraries[i]));
240           strcpy (option + 11 + strlen (libraries[i]), ".dll");
241           *argp++ = option;
242         }
243       if (debug)
244         *argp++ = "-debug";
245       for (i = 0; i < sources_count; i++)
246         {
247           const char *source_file = sources[i];
248           if (strlen (source_file) >= 10
249               && memcmp (source_file + strlen (source_file) - 10, ".resources",
250                          10) == 0)
251             {
252               char *option = (char *) xmalloca (10 + strlen (source_file) + 1);
253
254               memcpy (option, "-resource:", 10);
255               strcpy (option + 10, source_file);
256               *argp++ = option;
257             }
258           else
259             *argp++ = (char *) source_file;
260         }
261       *argp = NULL;
262       /* Ensure argv length was correctly calculated.  */
263       if (argp - argv != argc)
264         abort ();
265
266       if (verbose)
267         {
268           char *command = shell_quote_argv (argv);
269           printf ("%s\n", command);
270           free (command);
271         }
272
273       child = create_pipe_in ("mcs", "mcs", argv, NULL, false, true, true, fd);
274
275       /* Read the subprocess output, copying it to stderr.  Drop the last
276          line if it starts with "Compilation succeeded".  */
277       fp = fdopen (fd[0], "r");
278       if (fp == NULL)
279         error (EXIT_FAILURE, errno, _("fdopen() failed"));
280       line[0] = NULL; linesize[0] = 0;
281       line[1] = NULL; linesize[1] = 0;
282       l = 0;
283       for (;;)
284         {
285           linelen[l] = getline (&line[l], &linesize[l], fp);
286           if (linelen[l] == (size_t)(-1))
287             break;
288           l = (l + 1) % 2;
289           if (line[l] != NULL)
290             fwrite (line[l], 1, linelen[l], stderr);
291         }
292       l = (l + 1) % 2;
293       if (line[l] != NULL
294           && !(linelen[l] >= 21
295                && memcmp (line[l], "Compilation succeeded", 21) == 0))
296         fwrite (line[l], 1, linelen[l], stderr);
297       if (line[0] != NULL)
298         free (line[0]);
299       if (line[1] != NULL)
300         free (line[1]);
301       fclose (fp);
302
303       /* Remove zombie process from process list, and retrieve exit status.  */
304       exitstatus = wait_subprocess (child, "mcs", false, false, true, true);
305
306       for (i = 1 + (output_is_library ? 1 : 0);
307            i < 1 + (output_is_library ? 1 : 0)
308                + 1 + libdirs_count + libraries_count;
309            i++)
310         freea (argv[i]);
311       for (i = 0; i < sources_count; i++)
312         if (argv[argc - sources_count + i] != sources[i])
313           freea (argv[argc - sources_count + i]);
314       freea (argv);
315
316       return (exitstatus != 0);
317     }
318   else
319     return -1;
320 }
321
322 static int
323 compile_csharp_using_sscli (const char * const *sources,
324                             unsigned int sources_count,
325                             const char * const *libdirs,
326                             unsigned int libdirs_count,
327                             const char * const *libraries,
328                             unsigned int libraries_count,
329                             const char *output_file, bool output_is_library,
330                             bool optimize, bool debug,
331                             bool verbose)
332 {
333   static bool csc_tested;
334   static bool csc_present;
335
336   if (!csc_tested)
337     {
338       /* Test for presence of csc:
339          "csc -help >/dev/null 2>/dev/null \
340           && ! { csc -help 2>/dev/null | grep -i chicken > /dev/null; }"  */
341       char *argv[3];
342       pid_t child;
343       int fd[1];
344       int exitstatus;
345
346       argv[0] = "csc";
347       argv[1] = "-help";
348       argv[2] = NULL;
349       child = create_pipe_in ("csc", "csc", argv, DEV_NULL, true, true, false,
350                               fd);
351       csc_present = false;
352       if (child != -1)
353         {
354           /* Read the subprocess output, and test whether it contains the
355              string "chicken".  */
356           char c[7];
357           size_t count = 0;
358
359           csc_present = true;
360           while (safe_read (fd[0], &c[count], 1) > 0)
361             {
362               if (c[count] >= 'A' && c[count] <= 'Z')
363                 c[count] += 'a' - 'A';
364               count++;
365               if (count == 7)
366                 {
367                   if (memcmp (c, "chicken", 7) == 0)
368                     csc_present = false;
369                   c[0] = c[1]; c[1] = c[2]; c[2] = c[3];
370                   c[3] = c[4]; c[4] = c[5]; c[5] = c[6];
371                   count--;
372                 }
373             }
374
375           close (fd[0]);
376
377           /* Remove zombie process from process list, and retrieve exit
378              status.  */
379           exitstatus =
380             wait_subprocess (child, "csc", false, true, true, false);
381           if (exitstatus != 0)
382             csc_present = false;
383         }
384       csc_tested = true;
385     }
386
387   if (csc_present)
388     {
389       unsigned int argc;
390       char **argv;
391       char **argp;
392       int exitstatus;
393       unsigned int i;
394
395       argc =
396         1 + 1 + 1 + libdirs_count + libraries_count
397         + (optimize ? 1 : 0) + (debug ? 1 : 0) + sources_count;
398       argv = (char **) xmalloca ((argc + 1) * sizeof (char *));
399
400       argp = argv;
401       *argp++ = "csc";
402       *argp++ =
403         (char *) (output_is_library ? "-target:library" : "-target:exe");
404       {
405         char *option = (char *) xmalloca (5 + strlen (output_file) + 1);
406         memcpy (option, "-out:", 5);
407         strcpy (option + 5, output_file);
408         *argp++ = option;
409       }
410       for (i = 0; i < libdirs_count; i++)
411         {
412           char *option = (char *) xmalloca (5 + strlen (libdirs[i]) + 1);
413           memcpy (option, "-lib:", 5);
414           strcpy (option + 5, libdirs[i]);
415           *argp++ = option;
416         }
417       for (i = 0; i < libraries_count; i++)
418         {
419           char *option = (char *) xmalloca (11 + strlen (libraries[i]) + 4 + 1);
420           memcpy (option, "-reference:", 11);
421           memcpy (option + 11, libraries[i], strlen (libraries[i]));
422           strcpy (option + 11 + strlen (libraries[i]), ".dll");
423           *argp++ = option;
424         }
425       if (optimize)
426         *argp++ = "-optimize+";
427       if (debug)
428         *argp++ = "-debug+";
429       for (i = 0; i < sources_count; i++)
430         {
431           const char *source_file = sources[i];
432           if (strlen (source_file) >= 10
433               && memcmp (source_file + strlen (source_file) - 10, ".resources",
434                          10) == 0)
435             {
436               char *option = (char *) xmalloca (10 + strlen (source_file) + 1);
437
438               memcpy (option, "-resource:", 10);
439               strcpy (option + 10, source_file);
440               *argp++ = option;
441             }
442           else
443             *argp++ = (char *) source_file;
444         }
445       *argp = NULL;
446       /* Ensure argv length was correctly calculated.  */
447       if (argp - argv != argc)
448         abort ();
449
450       if (verbose)
451         {
452           char *command = shell_quote_argv (argv);
453           printf ("%s\n", command);
454           free (command);
455         }
456
457       exitstatus = execute ("csc", "csc", argv, false, false, false, false,
458                             true, true);
459
460       for (i = 2; i < 3 + libdirs_count + libraries_count; i++)
461         freea (argv[i]);
462       for (i = 0; i < sources_count; i++)
463         if (argv[argc - sources_count + i] != sources[i])
464           freea (argv[argc - sources_count + i]);
465       freea (argv);
466
467       return (exitstatus != 0);
468     }
469   else
470     return -1;
471 }
472
473 bool
474 compile_csharp_class (const char * const *sources,
475                       unsigned int sources_count,
476                       const char * const *libdirs,
477                       unsigned int libdirs_count,
478                       const char * const *libraries,
479                       unsigned int libraries_count,
480                       const char *output_file,
481                       bool optimize, bool debug,
482                       bool verbose)
483 {
484   bool output_is_library =
485     (strlen (output_file) >= 4
486      && memcmp (output_file + strlen (output_file) - 4, ".dll", 4) == 0);
487   int result;
488
489   /* First try the C# implementation specified through --enable-csharp.  */
490 #if CSHARP_CHOICE_PNET
491   result = compile_csharp_using_pnet (sources, sources_count,
492                                       libdirs, libdirs_count,
493                                       libraries, libraries_count,
494                                       output_file, output_is_library,
495                                       optimize, debug, verbose);
496   if (result >= 0)
497     return (bool) result;
498 #endif
499
500 #if CSHARP_CHOICE_MONO
501   result = compile_csharp_using_mono (sources, sources_count,
502                                       libdirs, libdirs_count,
503                                       libraries, libraries_count,
504                                       output_file, output_is_library,
505                                       optimize, debug, verbose);
506   if (result >= 0)
507     return (bool) result;
508 #endif
509
510   /* Then try the remaining C# implementations in our standard order.  */
511 #if !CSHARP_CHOICE_PNET
512   result = compile_csharp_using_pnet (sources, sources_count,
513                                       libdirs, libdirs_count,
514                                       libraries, libraries_count,
515                                       output_file, output_is_library,
516                                       optimize, debug, verbose);
517   if (result >= 0)
518     return (bool) result;
519 #endif
520
521 #if !CSHARP_CHOICE_MONO
522   result = compile_csharp_using_mono (sources, sources_count,
523                                       libdirs, libdirs_count,
524                                       libraries, libraries_count,
525                                       output_file, output_is_library,
526                                       optimize, debug, verbose);
527   if (result >= 0)
528     return (bool) result;
529 #endif
530
531   result = compile_csharp_using_sscli (sources, sources_count,
532                                        libdirs, libdirs_count,
533                                        libraries, libraries_count,
534                                        output_file, output_is_library,
535                                        optimize, debug, verbose);
536   if (result >= 0)
537     return (bool) result;
538
539   error (0, 0, _("C# compiler not found, try installing pnet"));
540   return true;
541 }