1 /* Compile a C# program.
2 Copyright (C) 2003-2008 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 3 of the License, or
8 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>. */
22 #include "csharpcomp.h"
31 #include "wait-process.h"
33 #include "safe-read.h"
38 #define _(str) gettext (str)
41 /* Survey of C# compilers.
49 We try the CIL interpreters in the following order:
50 1. "cscc", because it is a completely free system.
51 2. "mcs", because it is a free system but doesn't integrate so well
52 with Unix. (Command line options start with / instead of -. Errors go
53 to stdout instead of stderr. Source references are printed as
54 "file(lineno)" instead of "file:lineno:".)
55 3. "csc", although it is not free, because it is a kind of "reference
56 implementation" of C#.
57 But the order can be changed through the --enable-csharp configuration
62 compile_csharp_using_pnet (const char * const *sources,
63 unsigned int sources_count,
64 const char * const *libdirs,
65 unsigned int libdirs_count,
66 const char * const *libraries,
67 unsigned int libraries_count,
68 const char *output_file, bool output_is_library,
69 bool optimize, bool debug,
72 static bool cscc_tested;
73 static bool cscc_present;
77 /* Test for presence of cscc:
78 "cscc --version >/dev/null 2>/dev/null" */
83 argv[1] = "--version";
85 exitstatus = execute ("cscc", "cscc", argv, false, false, true, true,
87 cscc_present = (exitstatus == 0);
100 1 + (output_is_library ? 1 : 0) + 2 + 2 * libdirs_count
101 + 2 * libraries_count + (optimize ? 1 : 0) + (debug ? 1 : 0)
103 argv = (char **) xmalloca ((argc + 1) * sizeof (char *));
107 if (output_is_library)
110 *argp++ = (char *) output_file;
111 for (i = 0; i < libdirs_count; i++)
114 *argp++ = (char *) libdirs[i];
116 for (i = 0; i < libraries_count; i++)
119 *argp++ = (char *) libraries[i];
125 for (i = 0; i < sources_count; i++)
127 const char *source_file = sources[i];
128 if (strlen (source_file) >= 10
129 && memcmp (source_file + strlen (source_file) - 10, ".resources",
132 char *option = (char *) xmalloca (12 + strlen (source_file) + 1);
134 memcpy (option, "-fresources=", 12);
135 strcpy (option + 12, source_file);
139 *argp++ = (char *) source_file;
142 /* Ensure argv length was correctly calculated. */
143 if (argp - argv != argc)
148 char *command = shell_quote_argv (argv);
149 printf ("%s\n", command);
153 exitstatus = execute ("cscc", "cscc", argv, false, false, false, false,
156 for (i = 0; i < sources_count; i++)
157 if (argv[argc - sources_count + i] != sources[i])
158 freea (argv[argc - sources_count + i]);
161 return (exitstatus != 0);
168 compile_csharp_using_mono (const char * const *sources,
169 unsigned int sources_count,
170 const char * const *libdirs,
171 unsigned int libdirs_count,
172 const char * const *libraries,
173 unsigned int libraries_count,
174 const char *output_file, bool output_is_library,
175 bool optimize, bool debug,
178 static bool mcs_tested;
179 static bool mcs_present;
183 /* Test for presence of mcs:
184 "mcs --version >/dev/null 2>/dev/null"
185 and (to exclude an unrelated 'mcs' program on QNX 6)
186 "mcs --version 2>/dev/null | grep Mono >/dev/null" */
193 argv[1] = "--version";
195 child = create_pipe_in ("mcs", "mcs", argv, DEV_NULL, true, true, false,
200 /* Read the subprocess output, and test whether it contains the
205 while (safe_read (fd[0], &c[count], 1) > 0)
210 if (memcmp (c, "Mono", 4) == 0)
212 c[0] = c[1]; c[1] = c[2]; c[2] = c[3];
219 /* Remove zombie process from process list, and retrieve exit
222 wait_subprocess (child, "mcs", false, true, true, false, NULL);
245 1 + (output_is_library ? 1 : 0) + 1 + libdirs_count + libraries_count
246 + (debug ? 1 : 0) + sources_count;
247 argv = (char **) xmalloca ((argc + 1) * sizeof (char *));
251 if (output_is_library)
252 *argp++ = "-target:library";
254 char *option = (char *) xmalloca (5 + strlen (output_file) + 1);
255 memcpy (option, "-out:", 5);
256 strcpy (option + 5, output_file);
259 for (i = 0; i < libdirs_count; i++)
261 char *option = (char *) xmalloca (5 + strlen (libdirs[i]) + 1);
262 memcpy (option, "-lib:", 5);
263 strcpy (option + 5, libdirs[i]);
266 for (i = 0; i < libraries_count; i++)
268 char *option = (char *) xmalloca (11 + strlen (libraries[i]) + 4 + 1);
269 memcpy (option, "-reference:", 11);
270 memcpy (option + 11, libraries[i], strlen (libraries[i]));
271 strcpy (option + 11 + strlen (libraries[i]), ".dll");
276 for (i = 0; i < sources_count; i++)
278 const char *source_file = sources[i];
279 if (strlen (source_file) >= 10
280 && memcmp (source_file + strlen (source_file) - 10, ".resources",
283 char *option = (char *) xmalloca (10 + strlen (source_file) + 1);
285 memcpy (option, "-resource:", 10);
286 strcpy (option + 10, source_file);
290 *argp++ = (char *) source_file;
293 /* Ensure argv length was correctly calculated. */
294 if (argp - argv != argc)
299 char *command = shell_quote_argv (argv);
300 printf ("%s\n", command);
304 child = create_pipe_in ("mcs", "mcs", argv, NULL, false, true, true, fd);
306 /* Read the subprocess output, copying it to stderr. Drop the last
307 line if it starts with "Compilation succeeded". */
308 fp = fdopen (fd[0], "r");
310 error (EXIT_FAILURE, errno, _("fdopen() failed"));
311 line[0] = NULL; linesize[0] = 0;
312 line[1] = NULL; linesize[1] = 0;
316 linelen[l] = getline (&line[l], &linesize[l], fp);
317 if (linelen[l] == (size_t)(-1))
321 fwrite (line[l], 1, linelen[l], stderr);
325 && !(linelen[l] >= 21
326 && memcmp (line[l], "Compilation succeeded", 21) == 0))
327 fwrite (line[l], 1, linelen[l], stderr);
334 /* Remove zombie process from process list, and retrieve exit status. */
336 wait_subprocess (child, "mcs", false, false, true, true, NULL);
338 for (i = 1 + (output_is_library ? 1 : 0);
339 i < 1 + (output_is_library ? 1 : 0)
340 + 1 + libdirs_count + libraries_count;
343 for (i = 0; i < sources_count; i++)
344 if (argv[argc - sources_count + i] != sources[i])
345 freea (argv[argc - sources_count + i]);
348 return (exitstatus != 0);
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,
365 static bool csc_tested;
366 static bool csc_present;
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; }" */
381 child = create_pipe_in ("csc", "csc", argv, DEV_NULL, true, true, false,
386 /* Read the subprocess output, and test whether it contains the
392 while (safe_read (fd[0], &c[count], 1) > 0)
394 if (c[count] >= 'A' && c[count] <= 'Z')
395 c[count] += 'a' - 'A';
399 if (memcmp (c, "chicken", 7) == 0)
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];
409 /* Remove zombie process from process list, and retrieve exit
412 wait_subprocess (child, "csc", false, true, true, false, NULL);
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 *));
435 (char *) (output_is_library ? "-target:library" : "-target:exe");
437 char *option = (char *) xmalloca (5 + strlen (output_file) + 1);
438 memcpy (option, "-out:", 5);
439 strcpy (option + 5, output_file);
442 for (i = 0; i < libdirs_count; i++)
444 char *option = (char *) xmalloca (5 + strlen (libdirs[i]) + 1);
445 memcpy (option, "-lib:", 5);
446 strcpy (option + 5, libdirs[i]);
449 for (i = 0; i < libraries_count; i++)
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");
458 *argp++ = "-optimize+";
461 for (i = 0; i < sources_count; i++)
463 const char *source_file = sources[i];
464 if (strlen (source_file) >= 10
465 && memcmp (source_file + strlen (source_file) - 10, ".resources",
468 char *option = (char *) xmalloca (10 + strlen (source_file) + 1);
470 memcpy (option, "-resource:", 10);
471 strcpy (option + 10, source_file);
475 *argp++ = (char *) source_file;
478 /* Ensure argv length was correctly calculated. */
479 if (argp - argv != argc)
484 char *command = shell_quote_argv (argv);
485 printf ("%s\n", command);
489 exitstatus = execute ("csc", "csc", argv, false, false, false, false,
492 for (i = 2; i < 3 + libdirs_count + libraries_count; 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]);
499 return (exitstatus != 0);
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,
516 bool output_is_library =
517 (strlen (output_file) >= 4
518 && memcmp (output_file + strlen (output_file) - 4, ".dll", 4) == 0);
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);
529 return (bool) result;
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);
539 return (bool) result;
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);
550 return (bool) result;
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);
560 return (bool) result;
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);
569 return (bool) result;
571 error (0, 0, _("C# compiler not found, try installing pnet"));