1 /* Compile a C# program.
2 Copyright (C) 2003-2007 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2003.
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)
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.
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. */
23 #include "csharpcomp.h"
32 #include "wait-process.h"
34 #include "safe-read.h"
39 #define _(str) gettext (str)
42 /* Survey of C# compilers.
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
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,
73 static bool cscc_tested;
74 static bool cscc_present;
78 /* Test for presence of cscc:
79 "cscc --version >/dev/null 2>/dev/null" */
84 argv[1] = "--version";
86 exitstatus = execute ("cscc", "cscc", argv, false, false, true, true,
88 cscc_present = (exitstatus == 0);
101 1 + (output_is_library ? 1 : 0) + 2 + 2 * libdirs_count
102 + 2 * libraries_count + (optimize ? 1 : 0) + (debug ? 1 : 0)
104 argv = (char **) xmalloca ((argc + 1) * sizeof (char *));
108 if (output_is_library)
111 *argp++ = (char *) output_file;
112 for (i = 0; i < libdirs_count; i++)
115 *argp++ = (char *) libdirs[i];
117 for (i = 0; i < libraries_count; i++)
120 *argp++ = (char *) libraries[i];
126 for (i = 0; i < sources_count; i++)
128 const char *source_file = sources[i];
129 if (strlen (source_file) >= 10
130 && memcmp (source_file + strlen (source_file) - 10, ".resources",
133 char *option = (char *) xmalloca (12 + strlen (source_file) + 1);
135 memcpy (option, "-fresources=", 12);
136 strcpy (option + 12, source_file);
140 *argp++ = (char *) source_file;
143 /* Ensure argv length was correctly calculated. */
144 if (argp - argv != argc)
149 char *command = shell_quote_argv (argv);
150 printf ("%s\n", command);
154 exitstatus = execute ("cscc", "cscc", argv, false, false, false, false,
157 for (i = 0; i < sources_count; i++)
158 if (argv[argc - sources_count + i] != sources[i])
159 freea (argv[argc - sources_count + i]);
162 return (exitstatus != 0);
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,
179 static bool mcs_tested;
180 static bool mcs_present;
184 /* Test for presence of mcs:
185 "mcs --version >/dev/null 2>/dev/null" */
190 argv[1] = "--version";
192 exitstatus = execute ("mcs", "mcs", argv, false, false, true, true, true,
194 mcs_present = (exitstatus == 0);
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 *));
220 if (output_is_library)
221 *argp++ = "-target:library";
223 char *option = (char *) xmalloca (5 + strlen (output_file) + 1);
224 memcpy (option, "-out:", 5);
225 strcpy (option + 5, output_file);
228 for (i = 0; i < libdirs_count; i++)
230 char *option = (char *) xmalloca (5 + strlen (libdirs[i]) + 1);
231 memcpy (option, "-lib:", 5);
232 strcpy (option + 5, libdirs[i]);
235 for (i = 0; i < libraries_count; i++)
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");
245 for (i = 0; i < sources_count; i++)
247 const char *source_file = sources[i];
248 if (strlen (source_file) >= 10
249 && memcmp (source_file + strlen (source_file) - 10, ".resources",
252 char *option = (char *) xmalloca (10 + strlen (source_file) + 1);
254 memcpy (option, "-resource:", 10);
255 strcpy (option + 10, source_file);
259 *argp++ = (char *) source_file;
262 /* Ensure argv length was correctly calculated. */
263 if (argp - argv != argc)
268 char *command = shell_quote_argv (argv);
269 printf ("%s\n", command);
273 child = create_pipe_in ("mcs", "mcs", argv, NULL, false, true, true, fd);
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");
279 error (EXIT_FAILURE, errno, _("fdopen() failed"));
280 line[0] = NULL; linesize[0] = 0;
281 line[1] = NULL; linesize[1] = 0;
285 linelen[l] = getline (&line[l], &linesize[l], fp);
286 if (linelen[l] == (size_t)(-1))
290 fwrite (line[l], 1, linelen[l], stderr);
294 && !(linelen[l] >= 21
295 && memcmp (line[l], "Compilation succeeded", 21) == 0))
296 fwrite (line[l], 1, linelen[l], stderr);
303 /* Remove zombie process from process list, and retrieve exit status. */
304 exitstatus = wait_subprocess (child, "mcs", false, false, true, true);
306 for (i = 1 + (output_is_library ? 1 : 0);
307 i < 1 + (output_is_library ? 1 : 0)
308 + 1 + libdirs_count + libraries_count;
311 for (i = 0; i < sources_count; i++)
312 if (argv[argc - sources_count + i] != sources[i])
313 freea (argv[argc - sources_count + i]);
316 return (exitstatus != 0);
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,
333 static bool csc_tested;
334 static bool csc_present;
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; }" */
349 child = create_pipe_in ("csc", "csc", argv, DEV_NULL, true, true, false,
354 /* Read the subprocess output, and test whether it contains the
360 while (safe_read (fd[0], &c[count], 1) > 0)
362 if (c[count] >= 'A' && c[count] <= 'Z')
363 c[count] += 'a' - 'A';
367 if (memcmp (c, "chicken", 7) == 0)
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];
377 /* Remove zombie process from process list, and retrieve exit
380 wait_subprocess (child, "csc", false, true, true, false);
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 *));
403 (char *) (output_is_library ? "-target:library" : "-target:exe");
405 char *option = (char *) xmalloca (5 + strlen (output_file) + 1);
406 memcpy (option, "-out:", 5);
407 strcpy (option + 5, output_file);
410 for (i = 0; i < libdirs_count; i++)
412 char *option = (char *) xmalloca (5 + strlen (libdirs[i]) + 1);
413 memcpy (option, "-lib:", 5);
414 strcpy (option + 5, libdirs[i]);
417 for (i = 0; i < libraries_count; i++)
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");
426 *argp++ = "-optimize+";
429 for (i = 0; i < sources_count; i++)
431 const char *source_file = sources[i];
432 if (strlen (source_file) >= 10
433 && memcmp (source_file + strlen (source_file) - 10, ".resources",
436 char *option = (char *) xmalloca (10 + strlen (source_file) + 1);
438 memcpy (option, "-resource:", 10);
439 strcpy (option + 10, source_file);
443 *argp++ = (char *) source_file;
446 /* Ensure argv length was correctly calculated. */
447 if (argp - argv != argc)
452 char *command = shell_quote_argv (argv);
453 printf ("%s\n", command);
457 exitstatus = execute ("csc", "csc", argv, false, false, false, false,
460 for (i = 2; i < 3 + libdirs_count + libraries_count; 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]);
467 return (exitstatus != 0);
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,
484 bool output_is_library =
485 (strlen (output_file) >= 4
486 && memcmp (output_file + strlen (output_file) - 4, ".dll", 4) == 0);
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);
497 return (bool) result;
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);
507 return (bool) result;
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);
518 return (bool) result;
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);
528 return (bool) result;
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);
537 return (bool) result;
539 error (0, 0, _("C# compiler not found, try installing pnet"));