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"
35 #include "safe-read.h"
40 #define _(str) gettext (str)
43 /* Survey of C# compilers.
51 We try the CIL interpreters in the following order:
52 1. "cscc", because it is a completely free system.
53 2. "mcs", because it is a free system but doesn't integrate so well
54 with Unix. (Command line options start with / instead of -. Errors go
55 to stdout instead of stderr. Source references are printed as
56 "file(lineno)" instead of "file:lineno:".)
57 3. "csc", although it is not free, because it is a kind of "reference
58 implementation" of C#.
59 But the order can be changed through the --enable-csharp configuration
64 compile_csharp_using_pnet (const char * const *sources,
65 unsigned int sources_count,
66 const char * const *libdirs,
67 unsigned int libdirs_count,
68 const char * const *libraries,
69 unsigned int libraries_count,
70 const char *output_file, bool output_is_library,
71 bool optimize, bool debug,
74 static bool cscc_tested;
75 static bool cscc_present;
79 /* Test for presence of cscc:
80 "cscc --version >/dev/null 2>/dev/null" */
85 argv[1] = "--version";
87 exitstatus = execute ("cscc", "cscc", argv, false, false, true, true,
89 cscc_present = (exitstatus == 0);
102 1 + (output_is_library ? 1 : 0) + 2 + 2 * libdirs_count
103 + 2 * libraries_count + (optimize ? 1 : 0) + (debug ? 1 : 0)
105 argv = (char **) xmalloca ((argc + 1) * sizeof (char *));
109 if (output_is_library)
112 *argp++ = (char *) output_file;
113 for (i = 0; i < libdirs_count; i++)
116 *argp++ = (char *) libdirs[i];
118 for (i = 0; i < libraries_count; i++)
121 *argp++ = (char *) libraries[i];
127 for (i = 0; i < sources_count; i++)
129 const char *source_file = sources[i];
130 if (strlen (source_file) >= 10
131 && memcmp (source_file + strlen (source_file) - 10, ".resources",
134 char *option = (char *) xmalloca (12 + strlen (source_file) + 1);
136 memcpy (option, "-fresources=", 12);
137 strcpy (option + 12, source_file);
141 *argp++ = (char *) source_file;
144 /* Ensure argv length was correctly calculated. */
145 if (argp - argv != argc)
150 char *command = shell_quote_argv (argv);
151 printf ("%s\n", command);
155 exitstatus = execute ("cscc", "cscc", argv, false, false, false, false,
158 for (i = 0; i < sources_count; i++)
159 if (argv[argc - sources_count + i] != sources[i])
160 freea (argv[argc - sources_count + i]);
163 return (exitstatus != 0);
170 compile_csharp_using_mono (const char * const *sources,
171 unsigned int sources_count,
172 const char * const *libdirs,
173 unsigned int libdirs_count,
174 const char * const *libraries,
175 unsigned int libraries_count,
176 const char *output_file, bool output_is_library,
177 bool optimize, bool debug,
180 static bool mcs_tested;
181 static bool mcs_present;
185 /* Test for presence of mcs:
186 "mcs --version >/dev/null 2>/dev/null" */
191 argv[1] = "--version";
193 exitstatus = execute ("mcs", "mcs", argv, false, false, true, true, true,
195 mcs_present = (exitstatus == 0);
215 1 + (output_is_library ? 1 : 0) + 1 + libdirs_count + libraries_count
216 + (debug ? 1 : 0) + sources_count;
217 argv = (char **) xmalloca ((argc + 1) * sizeof (char *));
221 if (output_is_library)
222 *argp++ = "-target:library";
224 char *option = (char *) xmalloca (5 + strlen (output_file) + 1);
225 memcpy (option, "-out:", 5);
226 strcpy (option + 5, output_file);
229 for (i = 0; i < libdirs_count; i++)
231 char *option = (char *) xmalloca (5 + strlen (libdirs[i]) + 1);
232 memcpy (option, "-lib:", 5);
233 strcpy (option + 5, libdirs[i]);
236 for (i = 0; i < libraries_count; i++)
238 char *option = (char *) xmalloca (11 + strlen (libraries[i]) + 4 + 1);
239 memcpy (option, "-reference:", 11);
240 memcpy (option + 11, libraries[i], strlen (libraries[i]));
241 strcpy (option + 11 + strlen (libraries[i]), ".dll");
246 for (i = 0; i < sources_count; i++)
248 const char *source_file = sources[i];
249 if (strlen (source_file) >= 10
250 && memcmp (source_file + strlen (source_file) - 10, ".resources",
253 char *option = (char *) xmalloca (10 + strlen (source_file) + 1);
255 memcpy (option, "-resource:", 10);
256 strcpy (option + 10, source_file);
260 *argp++ = (char *) source_file;
263 /* Ensure argv length was correctly calculated. */
264 if (argp - argv != argc)
269 char *command = shell_quote_argv (argv);
270 printf ("%s\n", command);
274 child = create_pipe_in ("mcs", "mcs", argv, NULL, false, true, true, fd);
276 /* Read the subprocess output, copying it to stderr. Drop the last
277 line if it starts with "Compilation succeeded". */
278 fp = fdopen (fd[0], "r");
280 error (EXIT_FAILURE, errno, _("fdopen() failed"));
281 line[0] = NULL; linesize[0] = 0;
282 line[1] = NULL; linesize[1] = 0;
286 linelen[l] = getline (&line[l], &linesize[l], fp);
287 if (linelen[l] == (size_t)(-1))
291 fwrite (line[l], 1, linelen[l], stderr);
295 && !(linelen[l] >= 21
296 && memcmp (line[l], "Compilation succeeded", 21) == 0))
297 fwrite (line[l], 1, linelen[l], stderr);
304 /* Remove zombie process from process list, and retrieve exit status. */
305 exitstatus = wait_subprocess (child, "mcs", false, false, true, true);
307 for (i = 1 + (output_is_library ? 1 : 0);
308 i < 1 + (output_is_library ? 1 : 0)
309 + 1 + libdirs_count + libraries_count;
312 for (i = 0; i < sources_count; i++)
313 if (argv[argc - sources_count + i] != sources[i])
314 freea (argv[argc - sources_count + i]);
317 return (exitstatus != 0);
324 compile_csharp_using_sscli (const char * const *sources,
325 unsigned int sources_count,
326 const char * const *libdirs,
327 unsigned int libdirs_count,
328 const char * const *libraries,
329 unsigned int libraries_count,
330 const char *output_file, bool output_is_library,
331 bool optimize, bool debug,
334 static bool csc_tested;
335 static bool csc_present;
339 /* Test for presence of csc:
340 "csc -help >/dev/null 2>/dev/null \
341 && ! { csc -help 2>/dev/null | grep -i chicken > /dev/null; }" */
350 child = create_pipe_in ("csc", "csc", argv, DEV_NULL, true, true, false,
355 /* Read the subprocess output, and test whether it contains the
361 while (safe_read (fd[0], &c[count], 1) > 0)
363 if (c[count] >= 'A' && c[count] <= 'Z')
364 c[count] += 'a' - 'A';
368 if (memcmp (c, "chicken", 7) == 0)
370 c[0] = c[1]; c[1] = c[2]; c[2] = c[3];
371 c[3] = c[4]; c[4] = c[5]; c[5] = c[6];
378 /* Remove zombie process from process list, and retrieve exit
381 wait_subprocess (child, "csc", false, true, true, false);
397 1 + 1 + 1 + libdirs_count + libraries_count
398 + (optimize ? 1 : 0) + (debug ? 1 : 0) + sources_count;
399 argv = (char **) xmalloca ((argc + 1) * sizeof (char *));
404 (char *) (output_is_library ? "-target:library" : "-target:exe");
406 char *option = (char *) xmalloca (5 + strlen (output_file) + 1);
407 memcpy (option, "-out:", 5);
408 strcpy (option + 5, output_file);
411 for (i = 0; i < libdirs_count; i++)
413 char *option = (char *) xmalloca (5 + strlen (libdirs[i]) + 1);
414 memcpy (option, "-lib:", 5);
415 strcpy (option + 5, libdirs[i]);
418 for (i = 0; i < libraries_count; i++)
420 char *option = (char *) xmalloca (11 + strlen (libraries[i]) + 4 + 1);
421 memcpy (option, "-reference:", 11);
422 memcpy (option + 11, libraries[i], strlen (libraries[i]));
423 strcpy (option + 11 + strlen (libraries[i]), ".dll");
427 *argp++ = "-optimize+";
430 for (i = 0; i < sources_count; i++)
432 const char *source_file = sources[i];
433 if (strlen (source_file) >= 10
434 && memcmp (source_file + strlen (source_file) - 10, ".resources",
437 char *option = (char *) xmalloca (10 + strlen (source_file) + 1);
439 memcpy (option, "-resource:", 10);
440 strcpy (option + 10, source_file);
444 *argp++ = (char *) source_file;
447 /* Ensure argv length was correctly calculated. */
448 if (argp - argv != argc)
453 char *command = shell_quote_argv (argv);
454 printf ("%s\n", command);
458 exitstatus = execute ("csc", "csc", argv, false, false, false, false,
461 for (i = 2; i < 3 + libdirs_count + libraries_count; i++)
463 for (i = 0; i < sources_count; i++)
464 if (argv[argc - sources_count + i] != sources[i])
465 freea (argv[argc - sources_count + i]);
468 return (exitstatus != 0);
475 compile_csharp_class (const char * const *sources,
476 unsigned int sources_count,
477 const char * const *libdirs,
478 unsigned int libdirs_count,
479 const char * const *libraries,
480 unsigned int libraries_count,
481 const char *output_file,
482 bool optimize, bool debug,
485 bool output_is_library =
486 (strlen (output_file) >= 4
487 && memcmp (output_file + strlen (output_file) - 4, ".dll", 4) == 0);
490 /* First try the C# implementation specified through --enable-csharp. */
491 #if CSHARP_CHOICE_PNET
492 result = compile_csharp_using_pnet (sources, sources_count,
493 libdirs, libdirs_count,
494 libraries, libraries_count,
495 output_file, output_is_library,
496 optimize, debug, verbose);
498 return (bool) result;
501 #if CSHARP_CHOICE_MONO
502 result = compile_csharp_using_mono (sources, sources_count,
503 libdirs, libdirs_count,
504 libraries, libraries_count,
505 output_file, output_is_library,
506 optimize, debug, verbose);
508 return (bool) result;
511 /* Then try the remaining C# implementations in our standard order. */
512 #if !CSHARP_CHOICE_PNET
513 result = compile_csharp_using_pnet (sources, sources_count,
514 libdirs, libdirs_count,
515 libraries, libraries_count,
516 output_file, output_is_library,
517 optimize, debug, verbose);
519 return (bool) result;
522 #if !CSHARP_CHOICE_MONO
523 result = compile_csharp_using_mono (sources, sources_count,
524 libdirs, libdirs_count,
525 libraries, libraries_count,
526 output_file, output_is_library,
527 optimize, debug, verbose);
529 return (bool) result;
532 result = compile_csharp_using_sscli (sources, sources_count,
533 libdirs, libdirs_count,
534 libraries, libraries_count,
535 output_file, output_is_library,
536 optimize, debug, verbose);
538 return (bool) result;
540 error (0, 0, _("C# compiler not found, try installing pnet"));