X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fcsharpcomp.c;h=42d34fa402199cf78ced6ecbcad68d7072aded3d;hb=7ef6c64e210ac0979d7e8ac69bc5b5208c2405ab;hp=cbc6520fbb96dc3f42ee25b48c1f663903904821;hpb=b7b797042fa5f79590c77cad90fb073dccd960b2;p=gnulib.git diff --git a/lib/csharpcomp.c b/lib/csharpcomp.c index cbc6520fb..42d34fa40 100644 --- a/lib/csharpcomp.c +++ b/lib/csharpcomp.c @@ -1,11 +1,11 @@ /* Compile a C# program. - Copyright (C) 2003-2006 Free Software Foundation, Inc. + Copyright (C) 2003-2014 Free Software Foundation, Inc. Written by Bruno Haible , 2003. - This program is free software; you can redistribute it and/or modify + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2, or (at your option) - any later version. + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -13,12 +13,9 @@ GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + along with this program. If not, see . */ -#ifdef HAVE_CONFIG_H -# include -#endif +#include #include /* Specification. */ @@ -30,12 +27,11 @@ #include #include "execute.h" -#include "pipe.h" +#include "spawn-pipe.h" #include "wait-process.h" -#include "getline.h" #include "sh-quote.h" #include "safe-read.h" -#include "xallocsa.h" +#include "xmalloca.h" #include "error.h" #include "gettext.h" @@ -64,14 +60,14 @@ static int compile_csharp_using_pnet (const char * const *sources, - unsigned int sources_count, - const char * const *libdirs, - unsigned int libdirs_count, - const char * const *libraries, - unsigned int libraries_count, - const char *output_file, bool output_is_library, - bool optimize, bool debug, - bool verbose) + unsigned int sources_count, + const char * const *libdirs, + unsigned int libdirs_count, + const char * const *libraries, + unsigned int libraries_count, + const char *output_file, bool output_is_library, + bool optimize, bool debug, + bool verbose) { static bool cscc_tested; static bool cscc_present; @@ -79,7 +75,7 @@ compile_csharp_using_pnet (const char * const *sources, if (!cscc_tested) { /* Test for presence of cscc: - "cscc --version >/dev/null 2>/dev/null" */ + "cscc --version >/dev/null 2>/dev/null" */ char *argv[3]; int exitstatus; @@ -87,7 +83,7 @@ compile_csharp_using_pnet (const char * const *sources, argv[1] = "--version"; argv[2] = NULL; exitstatus = execute ("cscc", "cscc", argv, false, false, true, true, - true, false); + true, false, NULL); cscc_present = (exitstatus == 0); cscc_tested = true; } @@ -101,66 +97,66 @@ compile_csharp_using_pnet (const char * const *sources, unsigned int i; argc = - 1 + (output_is_library ? 1 : 0) + 2 + 2 * libdirs_count - + 2 * libraries_count + (optimize ? 1 : 0) + (debug ? 1 : 0) - + sources_count; - argv = (char **) xallocsa ((argc + 1) * sizeof (char *)); + 1 + (output_is_library ? 1 : 0) + 2 + 2 * libdirs_count + + 2 * libraries_count + (optimize ? 1 : 0) + (debug ? 1 : 0) + + sources_count; + argv = (char **) xmalloca ((argc + 1) * sizeof (char *)); argp = argv; *argp++ = "cscc"; if (output_is_library) - *argp++ = "-shared"; + *argp++ = "-shared"; *argp++ = "-o"; *argp++ = (char *) output_file; for (i = 0; i < libdirs_count; i++) - { - *argp++ = "-L"; - *argp++ = (char *) libdirs[i]; - } + { + *argp++ = "-L"; + *argp++ = (char *) libdirs[i]; + } for (i = 0; i < libraries_count; i++) - { - *argp++ = "-l"; - *argp++ = (char *) libraries[i]; - } + { + *argp++ = "-l"; + *argp++ = (char *) libraries[i]; + } if (optimize) - *argp++ = "-O"; + *argp++ = "-O"; if (debug) - *argp++ = "-g"; + *argp++ = "-g"; for (i = 0; i < sources_count; i++) - { - const char *source_file = sources[i]; - if (strlen (source_file) >= 10 - && memcmp (source_file + strlen (source_file) - 10, ".resources", - 10) == 0) - { - char *option = (char *) xallocsa (12 + strlen (source_file) + 1); - - memcpy (option, "-fresources=", 12); - strcpy (option + 12, source_file); - *argp++ = option; - } - else - *argp++ = (char *) source_file; - } + { + const char *source_file = sources[i]; + if (strlen (source_file) >= 10 + && memcmp (source_file + strlen (source_file) - 10, ".resources", + 10) == 0) + { + char *option = (char *) xmalloca (12 + strlen (source_file) + 1); + + memcpy (option, "-fresources=", 12); + strcpy (option + 12, source_file); + *argp++ = option; + } + else + *argp++ = (char *) source_file; + } *argp = NULL; /* Ensure argv length was correctly calculated. */ if (argp - argv != argc) - abort (); + abort (); if (verbose) - { - char *command = shell_quote_argv (argv); - printf ("%s\n", command); - free (command); - } + { + char *command = shell_quote_argv (argv); + printf ("%s\n", command); + free (command); + } exitstatus = execute ("cscc", "cscc", argv, false, false, false, false, - true, true); + true, true, NULL); for (i = 0; i < sources_count; i++) - if (argv[argc - sources_count + i] != sources[i]) - freesa (argv[argc - sources_count + i]); - freesa (argv); + if (argv[argc - sources_count + i] != sources[i]) + freea (argv[argc - sources_count + i]); + freea (argv); return (exitstatus != 0); } @@ -170,14 +166,14 @@ compile_csharp_using_pnet (const char * const *sources, static int compile_csharp_using_mono (const char * const *sources, - unsigned int sources_count, - const char * const *libdirs, - unsigned int libdirs_count, - const char * const *libraries, - unsigned int libraries_count, - const char *output_file, bool output_is_library, - bool optimize, bool debug, - bool verbose) + unsigned int sources_count, + const char * const *libdirs, + unsigned int libdirs_count, + const char * const *libraries, + unsigned int libraries_count, + const char *output_file, bool output_is_library, + bool optimize, bool debug, + bool verbose) { static bool mcs_tested; static bool mcs_present; @@ -185,16 +181,48 @@ compile_csharp_using_mono (const char * const *sources, if (!mcs_tested) { /* Test for presence of mcs: - "mcs --version >/dev/null 2>/dev/null" */ + "mcs --version >/dev/null 2>/dev/null" + and (to exclude an unrelated 'mcs' program on QNX 6) + "mcs --version 2>/dev/null | grep Mono >/dev/null" */ char *argv[3]; + pid_t child; + int fd[1]; int exitstatus; argv[0] = "mcs"; argv[1] = "--version"; argv[2] = NULL; - exitstatus = execute ("mcs", "mcs", argv, false, false, true, true, true, - false); - mcs_present = (exitstatus == 0); + child = create_pipe_in ("mcs", "mcs", argv, DEV_NULL, true, true, false, + fd); + mcs_present = false; + if (child != -1) + { + /* Read the subprocess output, and test whether it contains the + string "Mono". */ + char c[4]; + size_t count = 0; + + while (safe_read (fd[0], &c[count], 1) > 0) + { + count++; + if (count == 4) + { + if (memcmp (c, "Mono", 4) == 0) + mcs_present = true; + c[0] = c[1]; c[1] = c[2]; c[2] = c[3]; + count--; + } + } + + close (fd[0]); + + /* Remove zombie process from process list, and retrieve exit + status. */ + exitstatus = + wait_subprocess (child, "mcs", false, true, true, false, NULL); + if (exitstatus != 0) + mcs_present = false; + } mcs_tested = true; } @@ -214,107 +242,108 @@ compile_csharp_using_mono (const char * const *sources, unsigned int i; argc = - 1 + (output_is_library ? 1 : 0) + 1 + libdirs_count + libraries_count - + (debug ? 1 : 0) + sources_count; - argv = (char **) xallocsa ((argc + 1) * sizeof (char *)); + 1 + (output_is_library ? 1 : 0) + 1 + libdirs_count + libraries_count + + (debug ? 1 : 0) + sources_count; + argv = (char **) xmalloca ((argc + 1) * sizeof (char *)); argp = argv; *argp++ = "mcs"; if (output_is_library) - *argp++ = "-target:library"; + *argp++ = "-target:library"; { - char *option = (char *) xallocsa (5 + strlen (output_file) + 1); - memcpy (option, "-out:", 5); - strcpy (option + 5, output_file); - *argp++ = option; + char *option = (char *) xmalloca (5 + strlen (output_file) + 1); + memcpy (option, "-out:", 5); + strcpy (option + 5, output_file); + *argp++ = option; } for (i = 0; i < libdirs_count; i++) - { - char *option = (char *) xallocsa (5 + strlen (libdirs[i]) + 1); - memcpy (option, "-lib:", 5); - strcpy (option + 5, libdirs[i]); - *argp++ = option; - } + { + char *option = (char *) xmalloca (5 + strlen (libdirs[i]) + 1); + memcpy (option, "-lib:", 5); + strcpy (option + 5, libdirs[i]); + *argp++ = option; + } for (i = 0; i < libraries_count; i++) - { - char *option = (char *) xallocsa (11 + strlen (libraries[i]) + 4 + 1); - memcpy (option, "-reference:", 11); - memcpy (option + 11, libraries[i], strlen (libraries[i])); - strcpy (option + 11 + strlen (libraries[i]), ".dll"); - *argp++ = option; - } + { + char *option = (char *) xmalloca (11 + strlen (libraries[i]) + 4 + 1); + memcpy (option, "-reference:", 11); + memcpy (option + 11, libraries[i], strlen (libraries[i])); + strcpy (option + 11 + strlen (libraries[i]), ".dll"); + *argp++ = option; + } if (debug) - *argp++ = "-debug"; + *argp++ = "-debug"; for (i = 0; i < sources_count; i++) - { - const char *source_file = sources[i]; - if (strlen (source_file) >= 10 - && memcmp (source_file + strlen (source_file) - 10, ".resources", - 10) == 0) - { - char *option = (char *) xallocsa (10 + strlen (source_file) + 1); - - memcpy (option, "-resource:", 10); - strcpy (option + 10, source_file); - *argp++ = option; - } - else - *argp++ = (char *) source_file; - } + { + const char *source_file = sources[i]; + if (strlen (source_file) >= 10 + && memcmp (source_file + strlen (source_file) - 10, ".resources", + 10) == 0) + { + char *option = (char *) xmalloca (10 + strlen (source_file) + 1); + + memcpy (option, "-resource:", 10); + strcpy (option + 10, source_file); + *argp++ = option; + } + else + *argp++ = (char *) source_file; + } *argp = NULL; /* Ensure argv length was correctly calculated. */ if (argp - argv != argc) - abort (); + abort (); if (verbose) - { - char *command = shell_quote_argv (argv); - printf ("%s\n", command); - free (command); - } + { + char *command = shell_quote_argv (argv); + printf ("%s\n", command); + free (command); + } child = create_pipe_in ("mcs", "mcs", argv, NULL, false, true, true, fd); /* Read the subprocess output, copying it to stderr. Drop the last - line if it starts with "Compilation succeeded". */ + line if it starts with "Compilation succeeded". */ fp = fdopen (fd[0], "r"); if (fp == NULL) - error (EXIT_FAILURE, errno, _("fdopen() failed")); + error (EXIT_FAILURE, errno, _("fdopen() failed")); line[0] = NULL; linesize[0] = 0; line[1] = NULL; linesize[1] = 0; l = 0; for (;;) - { - linelen[l] = getline (&line[l], &linesize[l], fp); - if (linelen[l] == (size_t)(-1)) - break; - l = (l + 1) % 2; - if (line[l] != NULL) - fwrite (line[l], 1, linelen[l], stderr); - } + { + linelen[l] = getline (&line[l], &linesize[l], fp); + if (linelen[l] == (size_t)(-1)) + break; + l = (l + 1) % 2; + if (line[l] != NULL) + fwrite (line[l], 1, linelen[l], stderr); + } l = (l + 1) % 2; if (line[l] != NULL - && !(linelen[l] >= 21 - && memcmp (line[l], "Compilation succeeded", 21) == 0)) - fwrite (line[l], 1, linelen[l], stderr); + && !(linelen[l] >= 21 + && memcmp (line[l], "Compilation succeeded", 21) == 0)) + fwrite (line[l], 1, linelen[l], stderr); if (line[0] != NULL) - free (line[0]); + free (line[0]); if (line[1] != NULL) - free (line[1]); + free (line[1]); fclose (fp); /* Remove zombie process from process list, and retrieve exit status. */ - exitstatus = wait_subprocess (child, "mcs", false, false, true, true); + exitstatus = + wait_subprocess (child, "mcs", false, false, true, true, NULL); for (i = 1 + (output_is_library ? 1 : 0); - i < 1 + (output_is_library ? 1 : 0) - + 1 + libdirs_count + libraries_count; - i++) - freesa (argv[i]); + i < 1 + (output_is_library ? 1 : 0) + + 1 + libdirs_count + libraries_count; + i++) + freea (argv[i]); for (i = 0; i < sources_count; i++) - if (argv[argc - sources_count + i] != sources[i]) - freesa (argv[argc - sources_count + i]); - freesa (argv); + if (argv[argc - sources_count + i] != sources[i]) + freea (argv[argc - sources_count + i]); + freea (argv); return (exitstatus != 0); } @@ -324,14 +353,14 @@ compile_csharp_using_mono (const char * const *sources, static int compile_csharp_using_sscli (const char * const *sources, - unsigned int sources_count, - const char * const *libdirs, - unsigned int libdirs_count, - const char * const *libraries, - unsigned int libraries_count, - const char *output_file, bool output_is_library, - bool optimize, bool debug, - bool verbose) + unsigned int sources_count, + const char * const *libdirs, + unsigned int libdirs_count, + const char * const *libraries, + unsigned int libraries_count, + const char *output_file, bool output_is_library, + bool optimize, bool debug, + bool verbose) { static bool csc_tested; static bool csc_present; @@ -339,8 +368,8 @@ compile_csharp_using_sscli (const char * const *sources, if (!csc_tested) { /* Test for presence of csc: - "csc -help >/dev/null 2>/dev/null \ - && ! { csc -help 2>/dev/null | grep -i chicken > /dev/null; }" */ + "csc -help >/dev/null 2>/dev/null \ + && ! { csc -help 2>/dev/null | grep -i chicken > /dev/null; }" */ char *argv[3]; pid_t child; int fd[1]; @@ -350,40 +379,40 @@ compile_csharp_using_sscli (const char * const *sources, argv[1] = "-help"; argv[2] = NULL; child = create_pipe_in ("csc", "csc", argv, DEV_NULL, true, true, false, - fd); + fd); csc_present = false; if (child != -1) - { - /* Read the subprocess output, and test whether it contains the - string "chicken". */ - char c[7]; - size_t count = 0; - - csc_present = true; - while (safe_read (fd[0], &c[count], 1) > 0) - { - if (c[count] >= 'A' && c[count] <= 'Z') - c[count] += 'a' - 'A'; - count++; - if (count == 7) - { - if (memcmp (c, "chicken", 7) == 0) - csc_present = false; - c[0] = c[1]; c[1] = c[2]; c[2] = c[3]; - c[3] = c[4]; c[4] = c[5]; c[5] = c[6]; - count--; - } - } - - close (fd[0]); - - /* Remove zombie process from process list, and retrieve exit - status. */ - exitstatus = - wait_subprocess (child, "csc", false, true, true, false); - if (exitstatus != 0) - csc_present = false; - } + { + /* Read the subprocess output, and test whether it contains the + string "chicken". */ + char c[7]; + size_t count = 0; + + csc_present = true; + while (safe_read (fd[0], &c[count], 1) > 0) + { + if (c[count] >= 'A' && c[count] <= 'Z') + c[count] += 'a' - 'A'; + count++; + if (count == 7) + { + if (memcmp (c, "chicken", 7) == 0) + csc_present = false; + c[0] = c[1]; c[1] = c[2]; c[2] = c[3]; + c[3] = c[4]; c[4] = c[5]; c[5] = c[6]; + count--; + } + } + + close (fd[0]); + + /* Remove zombie process from process list, and retrieve exit + status. */ + exitstatus = + wait_subprocess (child, "csc", false, true, true, false, NULL); + if (exitstatus != 0) + csc_present = false; + } csc_tested = true; } @@ -396,75 +425,76 @@ compile_csharp_using_sscli (const char * const *sources, unsigned int i; argc = - 1 + 1 + 1 + libdirs_count + libraries_count - + (optimize ? 1 : 0) + (debug ? 1 : 0) + sources_count; - argv = (char **) xallocsa ((argc + 1) * sizeof (char *)); + 1 + 1 + 1 + libdirs_count + libraries_count + + (optimize ? 1 : 0) + (debug ? 1 : 0) + sources_count; + argv = (char **) xmalloca ((argc + 1) * sizeof (char *)); argp = argv; *argp++ = "csc"; - *argp++ = (output_is_library ? "-target:library" : "-target:exe"); + *argp++ = + (char *) (output_is_library ? "-target:library" : "-target:exe"); { - char *option = (char *) xallocsa (5 + strlen (output_file) + 1); - memcpy (option, "-out:", 5); - strcpy (option + 5, output_file); - *argp++ = option; + char *option = (char *) xmalloca (5 + strlen (output_file) + 1); + memcpy (option, "-out:", 5); + strcpy (option + 5, output_file); + *argp++ = option; } for (i = 0; i < libdirs_count; i++) - { - char *option = (char *) xallocsa (5 + strlen (libdirs[i]) + 1); - memcpy (option, "-lib:", 5); - strcpy (option + 5, libdirs[i]); - *argp++ = option; - } + { + char *option = (char *) xmalloca (5 + strlen (libdirs[i]) + 1); + memcpy (option, "-lib:", 5); + strcpy (option + 5, libdirs[i]); + *argp++ = option; + } for (i = 0; i < libraries_count; i++) - { - char *option = (char *) xallocsa (11 + strlen (libraries[i]) + 4 + 1); - memcpy (option, "-reference:", 11); - memcpy (option + 11, libraries[i], strlen (libraries[i])); - strcpy (option + 11 + strlen (libraries[i]), ".dll"); - *argp++ = option; - } + { + char *option = (char *) xmalloca (11 + strlen (libraries[i]) + 4 + 1); + memcpy (option, "-reference:", 11); + memcpy (option + 11, libraries[i], strlen (libraries[i])); + strcpy (option + 11 + strlen (libraries[i]), ".dll"); + *argp++ = option; + } if (optimize) - *argp++ = "-optimize+"; + *argp++ = "-optimize+"; if (debug) - *argp++ = "-debug+"; + *argp++ = "-debug+"; for (i = 0; i < sources_count; i++) - { - const char *source_file = sources[i]; - if (strlen (source_file) >= 10 - && memcmp (source_file + strlen (source_file) - 10, ".resources", - 10) == 0) - { - char *option = (char *) xallocsa (10 + strlen (source_file) + 1); - - memcpy (option, "-resource:", 10); - strcpy (option + 10, source_file); - *argp++ = option; - } - else - *argp++ = (char *) source_file; - } + { + const char *source_file = sources[i]; + if (strlen (source_file) >= 10 + && memcmp (source_file + strlen (source_file) - 10, ".resources", + 10) == 0) + { + char *option = (char *) xmalloca (10 + strlen (source_file) + 1); + + memcpy (option, "-resource:", 10); + strcpy (option + 10, source_file); + *argp++ = option; + } + else + *argp++ = (char *) source_file; + } *argp = NULL; /* Ensure argv length was correctly calculated. */ if (argp - argv != argc) - abort (); + abort (); if (verbose) - { - char *command = shell_quote_argv (argv); - printf ("%s\n", command); - free (command); - } + { + char *command = shell_quote_argv (argv); + printf ("%s\n", command); + free (command); + } exitstatus = execute ("csc", "csc", argv, false, false, false, false, - true, true); + true, true, NULL); for (i = 2; i < 3 + libdirs_count + libraries_count; i++) - freesa (argv[i]); + freea (argv[i]); for (i = 0; i < sources_count; i++) - if (argv[argc - sources_count + i] != sources[i]) - freesa (argv[argc - sources_count + i]); - freesa (argv); + if (argv[argc - sources_count + i] != sources[i]) + freea (argv[argc - sources_count + i]); + freea (argv); return (exitstatus != 0); } @@ -474,14 +504,14 @@ compile_csharp_using_sscli (const char * const *sources, bool compile_csharp_class (const char * const *sources, - unsigned int sources_count, - const char * const *libdirs, - unsigned int libdirs_count, - const char * const *libraries, - unsigned int libraries_count, - const char *output_file, - bool optimize, bool debug, - bool verbose) + unsigned int sources_count, + const char * const *libdirs, + unsigned int libdirs_count, + const char * const *libraries, + unsigned int libraries_count, + const char *output_file, + bool optimize, bool debug, + bool verbose) { bool output_is_library = (strlen (output_file) >= 4 @@ -491,20 +521,20 @@ compile_csharp_class (const char * const *sources, /* First try the C# implementation specified through --enable-csharp. */ #if CSHARP_CHOICE_PNET result = compile_csharp_using_pnet (sources, sources_count, - libdirs, libdirs_count, - libraries, libraries_count, - output_file, output_is_library, - optimize, debug, verbose); + libdirs, libdirs_count, + libraries, libraries_count, + output_file, output_is_library, + optimize, debug, verbose); if (result >= 0) return (bool) result; #endif #if CSHARP_CHOICE_MONO result = compile_csharp_using_mono (sources, sources_count, - libdirs, libdirs_count, - libraries, libraries_count, - output_file, output_is_library, - optimize, debug, verbose); + libdirs, libdirs_count, + libraries, libraries_count, + output_file, output_is_library, + optimize, debug, verbose); if (result >= 0) return (bool) result; #endif @@ -512,29 +542,29 @@ compile_csharp_class (const char * const *sources, /* Then try the remaining C# implementations in our standard order. */ #if !CSHARP_CHOICE_PNET result = compile_csharp_using_pnet (sources, sources_count, - libdirs, libdirs_count, - libraries, libraries_count, - output_file, output_is_library, - optimize, debug, verbose); + libdirs, libdirs_count, + libraries, libraries_count, + output_file, output_is_library, + optimize, debug, verbose); if (result >= 0) return (bool) result; #endif #if !CSHARP_CHOICE_MONO result = compile_csharp_using_mono (sources, sources_count, - libdirs, libdirs_count, - libraries, libraries_count, - output_file, output_is_library, - optimize, debug, verbose); + libdirs, libdirs_count, + libraries, libraries_count, + output_file, output_is_library, + optimize, debug, verbose); if (result >= 0) return (bool) result; #endif result = compile_csharp_using_sscli (sources, sources_count, - libdirs, libdirs_count, - libraries, libraries_count, - output_file, output_is_library, - optimize, debug, verbose); + libdirs, libdirs_count, + libraries, libraries_count, + output_file, output_is_library, + optimize, debug, verbose); if (result >= 0) return (bool) result;