execute, spawn-pipe: Make multithread-safe on native Windows.
[gnulib.git] / lib / csharpcomp.c
blobfac9a895d9de41ea4811e25174d773fe0d13e62c
1 /* Compile a C# program.
2 Copyright (C) 2003-2020 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 <https://www.gnu.org/licenses/>. */
18 #include <config.h>
19 #include <alloca.h>
21 /* Specification. */
22 #include "csharpcomp.h"
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
29 #include "execute.h"
30 #include "spawn-pipe.h"
31 #include "wait-process.h"
32 #include "sh-quote.h"
33 #include "safe-read.h"
34 #include "xmalloca.h"
35 #include "error.h"
36 #include "gettext.h"
38 #define _(str) gettext (str)
41 /* Survey of C# compilers.
43 Program from
45 mcs mono
46 csc sscli
48 We try the CIL interpreters in the following order:
49 1. "mcs", because it is a free system but doesn't integrate so well
50 with Unix. (Command line options start with / instead of -. Errors go
51 to stdout instead of stderr. Source references are printed as
52 "file(lineno)" instead of "file:lineno:".)
53 2. "csc", although it is not free, because it is a kind of "reference
54 implementation" of C#.
55 But the order can be changed through the --enable-csharp configuration
56 option.
59 static int
60 compile_csharp_using_mono (const char * const *sources,
61 unsigned int sources_count,
62 const char * const *libdirs,
63 unsigned int libdirs_count,
64 const char * const *libraries,
65 unsigned int libraries_count,
66 const char *output_file, bool output_is_library,
67 bool optimize, bool debug,
68 bool verbose)
70 static bool mcs_tested;
71 static bool mcs_present;
73 if (!mcs_tested)
75 /* Test for presence of mcs:
76 "mcs --version >/dev/null 2>/dev/null"
77 and (to exclude an unrelated 'mcs' program on QNX 6)
78 "mcs --version 2>/dev/null | grep Mono >/dev/null" */
79 char *argv[3];
80 pid_t child;
81 int fd[1];
82 int exitstatus;
84 argv[0] = "mcs";
85 argv[1] = "--version";
86 argv[2] = NULL;
87 child = create_pipe_in ("mcs", "mcs", argv, DEV_NULL, true, true, false,
88 fd);
89 mcs_present = false;
90 if (child != -1)
92 /* Read the subprocess output, and test whether it contains the
93 string "Mono". */
94 char c[4];
95 size_t count = 0;
97 while (safe_read (fd[0], &c[count], 1) > 0)
99 count++;
100 if (count == 4)
102 if (memcmp (c, "Mono", 4) == 0)
103 mcs_present = true;
104 c[0] = c[1]; c[1] = c[2]; c[2] = c[3];
105 count--;
109 close (fd[0]);
111 /* Remove zombie process from process list, and retrieve exit
112 status. */
113 exitstatus =
114 wait_subprocess (child, "mcs", false, true, true, false, NULL);
115 if (exitstatus != 0)
116 mcs_present = false;
118 mcs_tested = true;
121 if (mcs_present)
123 unsigned int argc;
124 char **argv;
125 char **argp;
126 pid_t child;
127 int fd[1];
128 FILE *fp;
129 char *line[2];
130 size_t linesize[2];
131 size_t linelen[2];
132 unsigned int l;
133 int exitstatus;
134 unsigned int i;
136 argc =
137 1 + (output_is_library ? 1 : 0) + 1 + libdirs_count + libraries_count
138 + (debug ? 1 : 0) + sources_count;
139 argv = (char **) xmalloca ((argc + 1) * sizeof (char *));
141 argp = argv;
142 *argp++ = "mcs";
143 if (output_is_library)
144 *argp++ = "-target:library";
146 char *option = (char *) xmalloca (5 + strlen (output_file) + 1);
147 memcpy (option, "-out:", 5);
148 strcpy (option + 5, output_file);
149 *argp++ = option;
151 for (i = 0; i < libdirs_count; i++)
153 char *option = (char *) xmalloca (5 + strlen (libdirs[i]) + 1);
154 memcpy (option, "-lib:", 5);
155 strcpy (option + 5, libdirs[i]);
156 *argp++ = option;
158 for (i = 0; i < libraries_count; i++)
160 char *option = (char *) xmalloca (11 + strlen (libraries[i]) + 4 + 1);
161 memcpy (option, "-reference:", 11);
162 memcpy (option + 11, libraries[i], strlen (libraries[i]));
163 strcpy (option + 11 + strlen (libraries[i]), ".dll");
164 *argp++ = option;
166 if (debug)
167 *argp++ = "-debug";
168 for (i = 0; i < sources_count; i++)
170 const char *source_file = sources[i];
171 if (strlen (source_file) >= 10
172 && memcmp (source_file + strlen (source_file) - 10, ".resources",
173 10) == 0)
175 char *option = (char *) xmalloca (10 + strlen (source_file) + 1);
177 memcpy (option, "-resource:", 10);
178 strcpy (option + 10, source_file);
179 *argp++ = option;
181 else
182 *argp++ = (char *) source_file;
184 *argp = NULL;
185 /* Ensure argv length was correctly calculated. */
186 if (argp - argv != argc)
187 abort ();
189 if (verbose)
191 char *command = shell_quote_argv (argv);
192 printf ("%s\n", command);
193 free (command);
196 child = create_pipe_in ("mcs", "mcs", argv, NULL, false, true, true, fd);
198 /* Read the subprocess output, copying it to stderr. Drop the last
199 line if it starts with "Compilation succeeded". */
200 fp = fdopen (fd[0], "r");
201 if (fp == NULL)
202 error (EXIT_FAILURE, errno, _("fdopen() failed"));
203 line[0] = NULL; linesize[0] = 0;
204 line[1] = NULL; linesize[1] = 0;
205 l = 0;
206 for (;;)
208 linelen[l] = getline (&line[l], &linesize[l], fp);
209 if (linelen[l] == (size_t)(-1))
210 break;
211 l = (l + 1) % 2;
212 if (line[l] != NULL)
213 fwrite (line[l], 1, linelen[l], stderr);
215 l = (l + 1) % 2;
216 if (line[l] != NULL
217 && !(linelen[l] >= 21
218 && memcmp (line[l], "Compilation succeeded", 21) == 0))
219 fwrite (line[l], 1, linelen[l], stderr);
220 if (line[0] != NULL)
221 free (line[0]);
222 if (line[1] != NULL)
223 free (line[1]);
224 fclose (fp);
226 /* Remove zombie process from process list, and retrieve exit status. */
227 exitstatus =
228 wait_subprocess (child, "mcs", false, false, true, true, NULL);
230 for (i = 1 + (output_is_library ? 1 : 0);
231 i < 1 + (output_is_library ? 1 : 0)
232 + 1 + libdirs_count + libraries_count;
233 i++)
234 freea (argv[i]);
235 for (i = 0; i < sources_count; i++)
236 if (argv[argc - sources_count + i] != sources[i])
237 freea (argv[argc - sources_count + i]);
238 freea (argv);
240 return (exitstatus != 0);
242 else
243 return -1;
246 static int
247 compile_csharp_using_sscli (const char * const *sources,
248 unsigned int sources_count,
249 const char * const *libdirs,
250 unsigned int libdirs_count,
251 const char * const *libraries,
252 unsigned int libraries_count,
253 const char *output_file, bool output_is_library,
254 bool optimize, bool debug,
255 bool verbose)
257 static bool csc_tested;
258 static bool csc_present;
260 if (!csc_tested)
262 /* Test for presence of csc:
263 "csc -help >/dev/null 2>/dev/null \
264 && ! { csc -help 2>/dev/null | grep -i chicken > /dev/null; }" */
265 char *argv[3];
266 pid_t child;
267 int fd[1];
268 int exitstatus;
270 argv[0] = "csc";
271 argv[1] = "-help";
272 argv[2] = NULL;
273 child = create_pipe_in ("csc", "csc", argv, DEV_NULL, true, true, false,
274 fd);
275 csc_present = false;
276 if (child != -1)
278 /* Read the subprocess output, and test whether it contains the
279 string "chicken". */
280 char c[7];
281 size_t count = 0;
283 csc_present = true;
284 while (safe_read (fd[0], &c[count], 1) > 0)
286 if (c[count] >= 'A' && c[count] <= 'Z')
287 c[count] += 'a' - 'A';
288 count++;
289 if (count == 7)
291 if (memcmp (c, "chicken", 7) == 0)
292 csc_present = false;
293 c[0] = c[1]; c[1] = c[2]; c[2] = c[3];
294 c[3] = c[4]; c[4] = c[5]; c[5] = c[6];
295 count--;
299 close (fd[0]);
301 /* Remove zombie process from process list, and retrieve exit
302 status. */
303 exitstatus =
304 wait_subprocess (child, "csc", false, true, true, false, NULL);
305 if (exitstatus != 0)
306 csc_present = false;
308 csc_tested = true;
311 if (csc_present)
313 unsigned int argc;
314 char **argv;
315 char **argp;
316 int exitstatus;
317 unsigned int i;
319 argc =
320 1 + 1 + 1 + libdirs_count + libraries_count
321 + (optimize ? 1 : 0) + (debug ? 1 : 0) + sources_count;
322 argv = (char **) xmalloca ((argc + 1) * sizeof (char *));
324 argp = argv;
325 *argp++ = "csc";
326 *argp++ =
327 (char *) (output_is_library ? "-target:library" : "-target:exe");
329 char *option = (char *) xmalloca (5 + strlen (output_file) + 1);
330 memcpy (option, "-out:", 5);
331 strcpy (option + 5, output_file);
332 *argp++ = option;
334 for (i = 0; i < libdirs_count; i++)
336 char *option = (char *) xmalloca (5 + strlen (libdirs[i]) + 1);
337 memcpy (option, "-lib:", 5);
338 strcpy (option + 5, libdirs[i]);
339 *argp++ = option;
341 for (i = 0; i < libraries_count; i++)
343 char *option = (char *) xmalloca (11 + strlen (libraries[i]) + 4 + 1);
344 memcpy (option, "-reference:", 11);
345 memcpy (option + 11, libraries[i], strlen (libraries[i]));
346 strcpy (option + 11 + strlen (libraries[i]), ".dll");
347 *argp++ = option;
349 if (optimize)
350 *argp++ = "-optimize+";
351 if (debug)
352 *argp++ = "-debug+";
353 for (i = 0; i < sources_count; i++)
355 const char *source_file = sources[i];
356 if (strlen (source_file) >= 10
357 && memcmp (source_file + strlen (source_file) - 10, ".resources",
358 10) == 0)
360 char *option = (char *) xmalloca (10 + strlen (source_file) + 1);
362 memcpy (option, "-resource:", 10);
363 strcpy (option + 10, source_file);
364 *argp++ = option;
366 else
367 *argp++ = (char *) source_file;
369 *argp = NULL;
370 /* Ensure argv length was correctly calculated. */
371 if (argp - argv != argc)
372 abort ();
374 if (verbose)
376 char *command = shell_quote_argv (argv);
377 printf ("%s\n", command);
378 free (command);
381 exitstatus = execute ("csc", "csc", argv, false, false, false, false,
382 true, true, NULL);
384 for (i = 2; i < 3 + libdirs_count + libraries_count; i++)
385 freea (argv[i]);
386 for (i = 0; i < sources_count; i++)
387 if (argv[argc - sources_count + i] != sources[i])
388 freea (argv[argc - sources_count + i]);
389 freea (argv);
391 return (exitstatus != 0);
393 else
394 return -1;
397 bool
398 compile_csharp_class (const char * const *sources,
399 unsigned int sources_count,
400 const char * const *libdirs,
401 unsigned int libdirs_count,
402 const char * const *libraries,
403 unsigned int libraries_count,
404 const char *output_file,
405 bool optimize, bool debug,
406 bool verbose)
408 bool output_is_library =
409 (strlen (output_file) >= 4
410 && memcmp (output_file + strlen (output_file) - 4, ".dll", 4) == 0);
411 int result;
413 /* First try the C# implementation specified through --enable-csharp. */
414 #if CSHARP_CHOICE_MONO
415 result = compile_csharp_using_mono (sources, sources_count,
416 libdirs, libdirs_count,
417 libraries, libraries_count,
418 output_file, output_is_library,
419 optimize, debug, verbose);
420 if (result >= 0)
421 return (bool) result;
422 #endif
424 /* Then try the remaining C# implementations in our standard order. */
425 #if !CSHARP_CHOICE_MONO
426 result = compile_csharp_using_mono (sources, sources_count,
427 libdirs, libdirs_count,
428 libraries, libraries_count,
429 output_file, output_is_library,
430 optimize, debug, verbose);
431 if (result >= 0)
432 return (bool) result;
433 #endif
435 result = compile_csharp_using_sscli (sources, sources_count,
436 libdirs, libdirs_count,
437 libraries, libraries_count,
438 output_file, output_is_library,
439 optimize, debug, verbose);
440 if (result >= 0)
441 return (bool) result;
443 error (0, 0, _("C# compiler not found, try installing mono"));
444 return true;