1 /* Execute a C# program.
2 Copyright (C) 2003-2016 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 "csharpexec.h"
33 /* Handling of MONO_PATH is just like Java CLASSPATH. */
34 #define CLASSPATHVAR "MONO_PATH"
35 #define new_classpath new_monopath
36 #define set_classpath set_monopath
37 #define reset_classpath reset_monopath
38 #include "classpath.h"
39 #include "classpath.c"
40 #undef reset_classpath
45 /* Handling of clix' PATH variable is just like Java CLASSPATH. */
46 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__
47 /* Native Windows, Cygwin */
48 #define CLASSPATHVAR "PATH"
49 #elif defined __APPLE__ && defined __MACH__
51 #define CLASSPATHVAR "DYLD_LIBRARY_PATH"
54 #define CLASSPATHVAR "LD_LIBRARY_PATH"
56 #define new_classpath new_clixpath
57 #define set_classpath set_clixpath
58 #define reset_classpath reset_clixpath
59 #include "classpath.h"
60 #include "classpath.c"
61 #undef reset_classpath
66 #define _(str) gettext (str)
69 /* Survey of CIL interpreters.
77 With Mono, the MONO_PATH is a colon separated list of pathnames. (On
78 Windows: semicolon separated list of pathnames.)
80 We try the CIL interpreters in the following order:
81 1. "ilrun", because it is a completely free system.
82 2. "mono", because it is a partially free system but doesn't integrate
84 3. "clix", although it is not free, because it is a kind of "reference
85 implementation" of C#.
86 But the order can be changed through the --enable-csharp configuration
91 execute_csharp_using_pnet (const char *assembly_path,
92 const char * const *libdirs,
93 unsigned int libdirs_count,
94 const char * const *args, unsigned int nargs,
95 bool verbose, bool quiet,
96 execute_fn *executer, void *private_data)
98 static bool ilrun_tested;
99 static bool ilrun_present;
103 /* Test for presence of ilrun:
104 "ilrun --version >/dev/null 2>/dev/null" */
109 argv[1] = "--version";
111 exitstatus = execute ("ilrun", "ilrun", argv, false, false, true, true,
113 ilrun_present = (exitstatus == 0);
125 argc = 1 + 2 * libdirs_count + 1 + nargs;
126 argv = (char **) xmalloca ((argc + 1) * sizeof (char *));
130 for (i = 0; i < libdirs_count; i++)
133 *argp++ = (char *) libdirs[i];
135 *argp++ = (char *) assembly_path;
136 for (i = 0; i < nargs; i++)
137 *argp++ = (char *) args[i];
139 /* Ensure argv length was correctly calculated. */
140 if (argp - argv != argc)
145 char *command = shell_quote_argv (argv);
146 printf ("%s\n", command);
150 err = executer ("ilrun", "ilrun", argv, private_data);
161 execute_csharp_using_mono (const char *assembly_path,
162 const char * const *libdirs,
163 unsigned int libdirs_count,
164 const char * const *args, unsigned int nargs,
165 bool verbose, bool quiet,
166 execute_fn *executer, void *private_data)
168 static bool mono_tested;
169 static bool mono_present;
173 /* Test for presence of mono:
174 "mono --version >/dev/null 2>/dev/null" */
179 argv[1] = "--version";
181 exitstatus = execute ("mono", "mono", argv, false, false, true, true,
183 mono_present = (exitstatus == 0);
190 char **argv = (char **) xmalloca ((2 + nargs + 1) * sizeof (char *));
195 old_monopath = set_monopath (libdirs, libdirs_count, false, verbose);
198 argv[1] = (char *) assembly_path;
199 for (i = 0; i <= nargs; i++)
200 argv[2 + i] = (char *) args[i];
204 char *command = shell_quote_argv (argv);
205 printf ("%s\n", command);
209 err = executer ("mono", "mono", argv, private_data);
211 /* Reset MONO_PATH. */
212 reset_monopath (old_monopath);
223 execute_csharp_using_sscli (const char *assembly_path,
224 const char * const *libdirs,
225 unsigned int libdirs_count,
226 const char * const *args, unsigned int nargs,
227 bool verbose, bool quiet,
228 execute_fn *executer, void *private_data)
230 static bool clix_tested;
231 static bool clix_present;
235 /* Test for presence of clix:
236 "clix >/dev/null 2>/dev/null ; test $? = 1" */
242 exitstatus = execute ("clix", "clix", argv, false, false, true, true,
244 clix_present = (exitstatus == 0 || exitstatus == 1);
251 char **argv = (char **) xmalloca ((2 + nargs + 1) * sizeof (char *));
255 /* Set clix' PATH variable. */
256 old_clixpath = set_clixpath (libdirs, libdirs_count, false, verbose);
259 argv[1] = (char *) assembly_path;
260 for (i = 0; i <= nargs; i++)
261 argv[2 + i] = (char *) args[i];
265 char *command = shell_quote_argv (argv);
266 printf ("%s\n", command);
270 err = executer ("clix", "clix", argv, private_data);
272 /* Reset clix' PATH variable. */
273 reset_clixpath (old_clixpath);
284 execute_csharp_program (const char *assembly_path,
285 const char * const *libdirs,
286 unsigned int libdirs_count,
287 const char * const *args,
288 bool verbose, bool quiet,
289 execute_fn *executer, void *private_data)
296 const char * const *arg;
298 for (nargs = 0, arg = args; *arg != NULL; nargs++, arg++)
302 /* First try the C# implementation specified through --enable-csharp. */
303 #if CSHARP_CHOICE_PNET
304 result = execute_csharp_using_pnet (assembly_path, libdirs, libdirs_count,
305 args, nargs, verbose, quiet,
306 executer, private_data);
308 return (bool) result;
311 #if CSHARP_CHOICE_MONO
312 result = execute_csharp_using_mono (assembly_path, libdirs, libdirs_count,
313 args, nargs, verbose, quiet,
314 executer, private_data);
316 return (bool) result;
319 /* Then try the remaining C# implementations in our standard order. */
320 #if !CSHARP_CHOICE_PNET
321 result = execute_csharp_using_pnet (assembly_path, libdirs, libdirs_count,
322 args, nargs, verbose, quiet,
323 executer, private_data);
325 return (bool) result;
328 #if !CSHARP_CHOICE_MONO
329 result = execute_csharp_using_mono (assembly_path, libdirs, libdirs_count,
330 args, nargs, verbose, quiet,
331 executer, private_data);
333 return (bool) result;
336 result = execute_csharp_using_sscli (assembly_path, libdirs, libdirs_count,
337 args, nargs, verbose, quiet,
338 executer, private_data);
340 return (bool) result;
343 error (0, 0, _("C# virtual machine not found, try installing pnet"));