Mercurial > hg > octave-kai > gnulib-hg
annotate lib/csharpcomp.c @ 6761:14eb5491c867
* lib/wait-process.c, lib/wait-process.h, lib/csharpcomp.c,
lib/execute.c, lib/javacomp.c: Back out previous change.
author | Derek R. Price <derek@ximbiot.com> |
---|---|
date | Wed, 26 Apr 2006 15:55:46 +0000 |
parents | 5d118b8eb6a4 |
children | c91111628d88 |
rev | line source |
---|---|
5913 | 1 /* Compile a C# program. |
2 Copyright (C) 2003-2005 Free Software Foundation, Inc. | |
3 Written by Bruno Haible <bruno@clisp.org>, 2003. | |
4 | |
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) | |
8 any later version. | |
9 | |
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. | |
14 | |
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. */ | |
18 | |
19 #ifdef HAVE_CONFIG_H | |
20 # include <config.h> | |
21 #endif | |
22 #include <alloca.h> | |
23 | |
24 /* Specification. */ | |
25 #include "csharpcomp.h" | |
26 | |
27 #include <errno.h> | |
28 #include <stdio.h> | |
29 #include <stdlib.h> | |
30 #include <string.h> | |
31 | |
32 #include "execute.h" | |
33 #include "pipe.h" | |
34 #include "wait-process.h" | |
35 #include "getline.h" | |
36 #include "sh-quote.h" | |
37 #include "safe-read.h" | |
38 #include "xallocsa.h" | |
39 #include "error.h" | |
40 #include "gettext.h" | |
41 | |
42 #define _(str) gettext (str) | |
43 | |
44 | |
45 /* Survey of C# compilers. | |
46 | |
47 Program from | |
48 | |
49 cscc pnet | |
50 mcs mono | |
51 csc sscli | |
52 | |
53 We try the CIL interpreters in the following order: | |
54 1. "cscc", because it is a completely free system. | |
55 2. "mcs", because it is a free system but doesn't integrate so well | |
56 with Unix. (Command line options start with / instead of -. Errors go | |
57 to stdout instead of stderr. Source references are printed as | |
58 "file(lineno)" instead of "file:lineno:".) | |
59 3. "csc", although it is not free, because it is a kind of "reference | |
60 implementation" of C#. | |
61 But the order can be changed through the --enable-csharp configuration | |
62 option. | |
63 */ | |
64 | |
65 static int | |
66 compile_csharp_using_pnet (const char * const *sources, | |
67 unsigned int sources_count, | |
68 const char * const *libdirs, | |
69 unsigned int libdirs_count, | |
70 const char * const *libraries, | |
71 unsigned int libraries_count, | |
72 const char *output_file, bool output_is_library, | |
73 bool optimize, bool debug, | |
74 bool verbose) | |
75 { | |
76 static bool cscc_tested; | |
77 static bool cscc_present; | |
78 | |
79 if (!cscc_tested) | |
80 { | |
81 /* Test for presence of cscc: | |
82 "cscc --version >/dev/null 2>/dev/null" */ | |
83 char *argv[3]; | |
84 int exitstatus; | |
85 | |
86 argv[0] = "cscc"; | |
87 argv[1] = "--version"; | |
88 argv[2] = NULL; | |
89 exitstatus = execute ("cscc", "cscc", argv, false, false, true, true, | |
90 true, false); | |
91 cscc_present = (exitstatus == 0); | |
92 cscc_tested = true; | |
93 } | |
94 | |
95 if (cscc_present) | |
96 { | |
97 unsigned int argc; | |
98 char **argv; | |
99 char **argp; | |
100 int exitstatus; | |
101 unsigned int i; | |
102 | |
103 argc = | |
104 1 + (output_is_library ? 1 : 0) + 2 + 2 * libdirs_count | |
105 + 2 * libraries_count + (optimize ? 1 : 0) + (debug ? 1 : 0) | |
106 + sources_count; | |
107 argv = (char **) xallocsa ((argc + 1) * sizeof (char *)); | |
108 | |
109 argp = argv; | |
110 *argp++ = "cscc"; | |
111 if (output_is_library) | |
112 *argp++ = "-shared"; | |
113 *argp++ = "-o"; | |
114 *argp++ = (char *) output_file; | |
115 for (i = 0; i < libdirs_count; i++) | |
116 { | |
117 *argp++ = "-L"; | |
118 *argp++ = (char *) libdirs[i]; | |
119 } | |
120 for (i = 0; i < libraries_count; i++) | |
121 { | |
122 *argp++ = "-l"; | |
123 *argp++ = (char *) libraries[i]; | |
124 } | |
125 if (optimize) | |
126 *argp++ = "-O"; | |
127 if (debug) | |
128 *argp++ = "-g"; | |
129 for (i = 0; i < sources_count; i++) | |
130 { | |
131 const char *source_file = sources[i]; | |
132 if (strlen (source_file) >= 9 | |
133 && memcmp (source_file + strlen (source_file) - 9, ".resource", | |
134 9) == 0) | |
135 { | |
136 char *option = (char *) xallocsa (12 + strlen (source_file) + 1); | |
137 | |
138 memcpy (option, "-fresources=", 12); | |
139 strcpy (option + 12, source_file); | |
140 *argp++ = option; | |
141 } | |
142 else | |
143 *argp++ = (char *) source_file; | |
144 } | |
145 *argp = NULL; | |
146 /* Ensure argv length was correctly calculated. */ | |
147 if (argp - argv != argc) | |
148 abort (); | |
149 | |
150 if (verbose) | |
151 { | |
152 char *command = shell_quote_argv (argv); | |
153 printf ("%s\n", command); | |
154 free (command); | |
155 } | |
156 | |
157 exitstatus = execute ("cscc", "cscc", argv, false, false, false, false, | |
158 true, true); | |
159 | |
160 for (i = 0; i < sources_count; i++) | |
161 if (argv[argc - sources_count + i] != sources[i]) | |
162 freesa (argv[argc - sources_count + i]); | |
163 freesa (argv); | |
164 | |
165 return (exitstatus != 0); | |
166 } | |
167 else | |
168 return -1; | |
169 } | |
170 | |
171 static int | |
172 compile_csharp_using_mono (const char * const *sources, | |
173 unsigned int sources_count, | |
174 const char * const *libdirs, | |
175 unsigned int libdirs_count, | |
176 const char * const *libraries, | |
177 unsigned int libraries_count, | |
178 const char *output_file, bool output_is_library, | |
179 bool optimize, bool debug, | |
180 bool verbose) | |
181 { | |
182 static bool mcs_tested; | |
183 static bool mcs_present; | |
184 | |
185 if (!mcs_tested) | |
186 { | |
187 /* Test for presence of mcs: | |
188 "mcs --version >/dev/null 2>/dev/null" */ | |
189 char *argv[3]; | |
190 int exitstatus; | |
191 | |
192 argv[0] = "mcs"; | |
193 argv[1] = "--version"; | |
194 argv[2] = NULL; | |
195 exitstatus = execute ("mcs", "mcs", argv, false, false, true, true, true, | |
196 false); | |
197 mcs_present = (exitstatus == 0); | |
198 mcs_tested = true; | |
199 } | |
200 | |
201 if (mcs_present) | |
202 { | |
203 unsigned int argc; | |
204 char **argv; | |
205 char **argp; | |
206 pid_t child; | |
207 int fd[1]; | |
208 FILE *fp; | |
209 char *line[2]; | |
210 size_t linesize[2]; | |
211 size_t linelen[2]; | |
212 unsigned int l; | |
213 int exitstatus; | |
214 unsigned int i; | |
215 | |
216 argc = | |
217 1 + (output_is_library ? 1 : 0) + 2 + 2 * libdirs_count | |
218 + 2 * libraries_count + (debug ? 1 : 0) + sources_count; | |
219 argv = (char **) xallocsa ((argc + 1) * sizeof (char *)); | |
220 | |
221 argp = argv; | |
222 *argp++ = "mcs"; | |
223 if (output_is_library) | |
224 *argp++ = "-target:library"; | |
225 *argp++ = "-o"; | |
226 *argp++ = (char *) output_file; | |
227 for (i = 0; i < libdirs_count; i++) | |
228 { | |
229 *argp++ = "-L"; | |
230 *argp++ = (char *) libdirs[i]; | |
231 } | |
232 for (i = 0; i < libraries_count; i++) | |
233 { | |
234 *argp++ = "-r"; | |
235 *argp++ = (char *) libraries[i]; | |
236 } | |
237 if (debug) | |
238 *argp++ = "-g"; | |
239 for (i = 0; i < sources_count; i++) | |
240 { | |
241 const char *source_file = sources[i]; | |
242 if (strlen (source_file) >= 9 | |
243 && memcmp (source_file + strlen (source_file) - 9, ".resource", | |
244 9) == 0) | |
245 { | |
246 char *option = (char *) xallocsa (10 + strlen (source_file) + 1); | |
247 | |
248 memcpy (option, "-resource:", 10); | |
249 strcpy (option + 10, source_file); | |
250 *argp++ = option; | |
251 } | |
252 else | |
253 *argp++ = (char *) source_file; | |
254 } | |
255 *argp = NULL; | |
256 /* Ensure argv length was correctly calculated. */ | |
257 if (argp - argv != argc) | |
258 abort (); | |
259 | |
260 if (verbose) | |
261 { | |
262 char *command = shell_quote_argv (argv); | |
263 printf ("%s\n", command); | |
264 free (command); | |
265 } | |
266 | |
267 child = create_pipe_in ("mcs", "mcs", argv, NULL, false, true, true, fd); | |
268 | |
269 /* Read the subprocess output, copying it to stderr. Drop the last | |
270 line if it starts with "Compilation succeeded". */ | |
271 fp = fdopen (fd[0], "r"); | |
272 if (fp == NULL) | |
273 error (EXIT_FAILURE, errno, _("fdopen() failed")); | |
274 line[0] = NULL; linesize[0] = 0; | |
275 line[1] = NULL; linesize[1] = 0; | |
276 l = 0; | |
277 for (;;) | |
278 { | |
279 linelen[l] = getline (&line[l], &linesize[l], fp); | |
280 if (linelen[l] == (size_t)(-1)) | |
281 break; | |
282 l = (l + 1) % 2; | |
283 if (line[l] != NULL) | |
284 fwrite (line[l], 1, linelen[l], stderr); | |
285 } | |
286 l = (l + 1) % 2; | |
287 if (line[l] != NULL | |
288 && !(linelen[l] >= 21 | |
289 && memcmp (line[l], "Compilation succeeded", 21) == 0)) | |
290 fwrite (line[l], 1, linelen[l], stderr); | |
291 if (line[0] != NULL) | |
292 free (line[0]); | |
293 if (line[1] != NULL) | |
294 free (line[1]); | |
295 fclose (fp); | |
296 | |
297 /* Remove zombie process from process list, and retrieve exit status. */ | |
6761
14eb5491c867
* lib/wait-process.c, lib/wait-process.h, lib/csharpcomp.c,
Derek R. Price <derek@ximbiot.com>
parents:
6759
diff
changeset
|
298 exitstatus = wait_subprocess (child, "mcs", false, false, true, true); |
5913 | 299 |
300 for (i = 0; i < sources_count; i++) | |
301 if (argv[argc - sources_count + i] != sources[i]) | |
302 freesa (argv[argc - sources_count + i]); | |
303 freesa (argv); | |
304 | |
305 return (exitstatus != 0); | |
306 } | |
307 else | |
308 return -1; | |
309 } | |
310 | |
311 static int | |
312 compile_csharp_using_sscli (const char * const *sources, | |
313 unsigned int sources_count, | |
314 const char * const *libdirs, | |
315 unsigned int libdirs_count, | |
316 const char * const *libraries, | |
317 unsigned int libraries_count, | |
318 const char *output_file, bool output_is_library, | |
319 bool optimize, bool debug, | |
320 bool verbose) | |
321 { | |
322 static bool csc_tested; | |
323 static bool csc_present; | |
324 | |
325 if (!csc_tested) | |
326 { | |
327 /* Test for presence of csc: | |
328 "csc -help >/dev/null 2>/dev/null \ | |
329 && ! { csc -help 2>/dev/null | grep -i chicken > /dev/null; }" */ | |
330 char *argv[3]; | |
331 pid_t child; | |
332 int fd[1]; | |
333 int exitstatus; | |
334 | |
335 argv[0] = "csc"; | |
336 argv[1] = "-help"; | |
337 argv[2] = NULL; | |
338 child = create_pipe_in ("csc", "csc", argv, DEV_NULL, true, true, false, | |
339 fd); | |
340 csc_present = false; | |
341 if (child != -1) | |
342 { | |
343 /* Read the subprocess output, and test whether it contains the | |
344 string "chicken". */ | |
345 char c[7]; | |
346 size_t count = 0; | |
347 | |
348 csc_present = true; | |
349 while (safe_read (fd[0], &c[count], 1) > 0) | |
350 { | |
351 if (c[count] >= 'A' && c[count] <= 'Z') | |
352 c[count] += 'a' - 'A'; | |
353 count++; | |
354 if (count == 7) | |
355 { | |
356 if (memcmp (c, "chicken", 7) == 0) | |
357 csc_present = false; | |
358 c[0] = c[1]; c[1] = c[2]; c[2] = c[3]; | |
359 c[3] = c[4]; c[4] = c[5]; c[5] = c[6]; | |
360 count--; | |
361 } | |
362 } | |
363 | |
364 close (fd[0]); | |
365 | |
366 /* Remove zombie process from process list, and retrieve exit | |
367 status. */ | |
368 exitstatus = | |
6761
14eb5491c867
* lib/wait-process.c, lib/wait-process.h, lib/csharpcomp.c,
Derek R. Price <derek@ximbiot.com>
parents:
6759
diff
changeset
|
369 wait_subprocess (child, "csc", false, true, true, false); |
5913 | 370 if (exitstatus != 0) |
371 csc_present = false; | |
372 } | |
373 csc_tested = true; | |
374 } | |
375 | |
376 if (csc_present) | |
377 { | |
378 unsigned int argc; | |
379 char **argv; | |
380 char **argp; | |
381 int exitstatus; | |
382 unsigned int i; | |
383 | |
384 argc = | |
385 1 + 1 + 1 + libdirs_count + libraries_count | |
386 + (optimize ? 1 : 0) + (debug ? 1 : 0) + sources_count; | |
387 argv = (char **) xallocsa ((argc + 1) * sizeof (char *)); | |
388 | |
389 argp = argv; | |
390 *argp++ = "csc"; | |
391 *argp++ = (output_is_library ? "-target:library" : "-target:exe"); | |
392 { | |
393 char *option = (char *) xallocsa (5 + strlen (output_file) + 1); | |
394 memcpy (option, "-out:", 5); | |
395 strcpy (option + 5, output_file); | |
396 *argp++ = option; | |
397 } | |
398 for (i = 0; i < libdirs_count; i++) | |
399 { | |
400 char *option = (char *) xallocsa (5 + strlen (libdirs[i]) + 1); | |
401 memcpy (option, "-lib:", 5); | |
402 strcpy (option + 5, libdirs[i]); | |
403 *argp++ = option; | |
404 } | |
405 for (i = 0; i < libraries_count; i++) | |
406 { | |
6465
8fc725193330
Fix portability bug w.r.t. Microsoft's csc compiler.
Bruno Haible <bruno@clisp.org>
parents:
5913
diff
changeset
|
407 char *option = (char *) xallocsa (11 + strlen (libraries[i]) + 4 + 1); |
5913 | 408 memcpy (option, "-reference:", 11); |
6465
8fc725193330
Fix portability bug w.r.t. Microsoft's csc compiler.
Bruno Haible <bruno@clisp.org>
parents:
5913
diff
changeset
|
409 memcpy (option + 11, libraries[i], strlen (libraries[i])); |
8fc725193330
Fix portability bug w.r.t. Microsoft's csc compiler.
Bruno Haible <bruno@clisp.org>
parents:
5913
diff
changeset
|
410 strcpy (option + 11 + strlen (libraries[i]), ".dll"); |
5913 | 411 *argp++ = option; |
412 } | |
413 if (optimize) | |
414 *argp++ = "-optimize+"; | |
415 if (debug) | |
416 *argp++ = "-debug+"; | |
417 for (i = 0; i < sources_count; i++) | |
418 { | |
419 const char *source_file = sources[i]; | |
420 if (strlen (source_file) >= 9 | |
421 && memcmp (source_file + strlen (source_file) - 9, ".resource", | |
422 9) == 0) | |
423 { | |
424 char *option = (char *) xallocsa (10 + strlen (source_file) + 1); | |
425 | |
426 memcpy (option, "-resource:", 10); | |
427 strcpy (option + 10, source_file); | |
428 *argp++ = option; | |
429 } | |
430 else | |
431 *argp++ = (char *) source_file; | |
432 } | |
433 *argp = NULL; | |
434 /* Ensure argv length was correctly calculated. */ | |
435 if (argp - argv != argc) | |
436 abort (); | |
437 | |
438 if (verbose) | |
439 { | |
440 char *command = shell_quote_argv (argv); | |
441 printf ("%s\n", command); | |
442 free (command); | |
443 } | |
444 | |
445 exitstatus = execute ("csc", "csc", argv, false, false, false, false, | |
446 true, true); | |
447 | |
448 for (i = 2; i < 3 + libdirs_count + libraries_count; i++) | |
449 freesa (argv[i]); | |
450 for (i = 0; i < sources_count; i++) | |
451 if (argv[argc - sources_count + i] != sources[i]) | |
452 freesa (argv[argc - sources_count + i]); | |
453 freesa (argv); | |
454 | |
455 return (exitstatus != 0); | |
456 } | |
457 else | |
458 return -1; | |
459 } | |
460 | |
461 bool | |
462 compile_csharp_class (const char * const *sources, | |
463 unsigned int sources_count, | |
464 const char * const *libdirs, | |
465 unsigned int libdirs_count, | |
466 const char * const *libraries, | |
467 unsigned int libraries_count, | |
468 const char *output_file, | |
469 bool optimize, bool debug, | |
470 bool verbose) | |
471 { | |
472 bool output_is_library = | |
473 (strlen (output_file) >= 4 | |
474 && memcmp (output_file + strlen (output_file) - 4, ".dll", 4) == 0); | |
475 int result; | |
476 | |
477 /* First try the C# implementation specified through --enable-csharp. */ | |
478 #if CSHARP_CHOICE_PNET | |
479 result = compile_csharp_using_pnet (sources, sources_count, | |
480 libdirs, libdirs_count, | |
481 libraries, libraries_count, | |
482 output_file, output_is_library, | |
483 optimize, debug, verbose); | |
484 if (result >= 0) | |
485 return (bool) result; | |
486 #endif | |
487 | |
488 #if CSHARP_CHOICE_MONO | |
489 result = compile_csharp_using_mono (sources, sources_count, | |
490 libdirs, libdirs_count, | |
491 libraries, libraries_count, | |
492 output_file, output_is_library, | |
493 optimize, debug, verbose); | |
494 if (result >= 0) | |
495 return (bool) result; | |
496 #endif | |
497 | |
498 /* Then try the remaining C# implementations in our standard order. */ | |
499 #if !CSHARP_CHOICE_PNET | |
500 result = compile_csharp_using_pnet (sources, sources_count, | |
501 libdirs, libdirs_count, | |
502 libraries, libraries_count, | |
503 output_file, output_is_library, | |
504 optimize, debug, verbose); | |
505 if (result >= 0) | |
506 return (bool) result; | |
507 #endif | |
508 | |
509 #if !CSHARP_CHOICE_MONO | |
510 result = compile_csharp_using_mono (sources, sources_count, | |
511 libdirs, libdirs_count, | |
512 libraries, libraries_count, | |
513 output_file, output_is_library, | |
514 optimize, debug, verbose); | |
515 if (result >= 0) | |
516 return (bool) result; | |
517 #endif | |
518 | |
519 result = compile_csharp_using_sscli (sources, sources_count, | |
520 libdirs, libdirs_count, | |
521 libraries, libraries_count, | |
522 output_file, output_is_library, | |
523 optimize, debug, verbose); | |
524 if (result >= 0) | |
525 return (bool) result; | |
526 | |
527 error (0, 0, _("C# compiler not found, try installing pnet")); | |
528 return true; | |
529 } |