1 /* Compile a C# program.
2 Copyright (C) 2003-2006 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 **) xallocsa ((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 *) xallocsa (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 freesa (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 **) xallocsa ((argc + 1) * sizeof (char *));
221 if (output_is_library)
222 *argp++ = "-target:library";
224 char *option = (char *) xallocsa (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 *) xallocsa (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 *) xallocsa (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 *) xallocsa (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 freesa (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 **) xallocsa ((argc + 1) * sizeof (char *));
403 *argp++ = (output_is_library ? "-target:library" : "-target:exe");
405 char *option = (char *) xallocsa (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 *) xallocsa (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 *) xallocsa (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 *) xallocsa (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 freesa (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"));