1 /* Execute a C# program.
2 Copyright (C) 2003-2006 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 2, or (at your option)
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, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include "csharpexec.h"
34 /* Handling of MONO_PATH is just like Java CLASSPATH. */
35 #define CLASSPATHVAR "MONO_PATH"
36 #define new_classpath new_monopath
37 #define set_classpath set_monopath
38 #define reset_classpath reset_monopath
39 #include "classpath.h"
40 #include "classpath.c"
41 #undef reset_classpath
46 /* Handling of clix' PATH variable is just like Java CLASSPATH. */
47 #if defined _WIN32 || defined __WIN32__ || defined __CYGWIN__
49 #define CLASSPATHVAR "PATH"
50 #elif defined __APPLE__ && defined __MACH__
52 #define CLASSPATHVAR "DYLD_LIBRARY_PATH"
55 #define CLASSPATHVAR "LD_LIBRARY_PATH"
57 #define new_classpath new_clixpath
58 #define set_classpath set_clixpath
59 #define reset_classpath reset_clixpath
60 #include "classpath.h"
61 #include "classpath.c"
62 #undef reset_classpath
67 #define _(str) gettext (str)
70 /* Survey of CIL interpreters.
78 With Mono, the MONO_PATH is a colon separated list of pathnames. (On
79 Windows: semicolon separated list of pathnames.)
81 We try the CIL interpreters in the following order:
82 1. "ilrun", because it is a completely free system.
83 2. "mono", because it is a partially free system but doesn't integrate
85 3. "clix", although it is not free, because it is a kind of "reference
86 implementation" of C#.
87 But the order can be changed through the --enable-csharp configuration
92 execute_csharp_using_pnet (const char *assembly_path,
93 const char * const *libdirs,
94 unsigned int libdirs_count,
95 const char * const *args, unsigned int nargs,
96 bool verbose, bool quiet,
97 execute_fn *executer, void *private_data)
99 static bool ilrun_tested;
100 static bool ilrun_present;
104 /* Test for presence of ilrun:
105 "ilrun --version >/dev/null 2>/dev/null" */
110 argv[1] = "--version";
112 exitstatus = execute ("ilrun", "ilrun", argv, false, false, true, true,
114 ilrun_present = (exitstatus == 0);
126 argc = 1 + 2 * libdirs_count + 1 + nargs;
127 argv = (char **) xallocsa ((argc + 1) * sizeof (char *));
131 for (i = 0; i < libdirs_count; i++)
134 *argp++ = (char *) libdirs[i];
136 *argp++ = (char *) assembly_path;
137 for (i = 0; i < nargs; i++)
138 *argp++ = (char *) args[i];
140 /* Ensure argv length was correctly calculated. */
141 if (argp - argv != argc)
146 char *command = shell_quote_argv (argv);
147 printf ("%s\n", command);
151 err = executer ("ilrun", "ilrun", argv, private_data);
162 execute_csharp_using_mono (const char *assembly_path,
163 const char * const *libdirs,
164 unsigned int libdirs_count,
165 const char * const *args, unsigned int nargs,
166 bool verbose, bool quiet,
167 execute_fn *executer, void *private_data)
169 static bool mono_tested;
170 static bool mono_present;
174 /* Test for presence of mono:
175 "mono --version >/dev/null 2>/dev/null" */
180 argv[1] = "--version";
182 exitstatus = execute ("mono", "mono", argv, false, false, true, true,
184 mono_present = (exitstatus == 0);
191 char **argv = (char **) xallocsa ((2 + nargs + 1) * sizeof (char *));
196 old_monopath = set_monopath (libdirs, libdirs_count, false, verbose);
199 argv[1] = (char *) assembly_path;
200 for (i = 0; i <= nargs; i++)
201 argv[2 + i] = (char *) args[i];
205 char *command = shell_quote_argv (argv);
206 printf ("%s\n", command);
210 err = executer ("mono", "mono", argv, private_data);
212 /* Reset MONO_PATH. */
213 reset_monopath (old_monopath);
224 execute_csharp_using_sscli (const char *assembly_path,
225 const char * const *libdirs,
226 unsigned int libdirs_count,
227 const char * const *args, unsigned int nargs,
228 bool verbose, bool quiet,
229 execute_fn *executer, void *private_data)
231 static bool clix_tested;
232 static bool clix_present;
236 /* Test for presence of clix:
237 "clix >/dev/null 2>/dev/null ; test $? = 1" */
243 exitstatus = execute ("clix", "clix", argv, false, false, true, true,
245 clix_present = (exitstatus == 0 || exitstatus == 1);
252 char **argv = (char **) xallocsa ((2 + nargs + 1) * sizeof (char *));
256 /* Set clix' PATH variable. */
257 old_clixpath = set_clixpath (libdirs, libdirs_count, false, verbose);
260 argv[1] = (char *) assembly_path;
261 for (i = 0; i <= nargs; i++)
262 argv[2 + i] = (char *) args[i];
266 char *command = shell_quote_argv (argv);
267 printf ("%s\n", command);
271 err = executer ("clix", "clix", argv, private_data);
273 /* Reset clix' PATH variable. */
274 reset_clixpath (old_clixpath);
285 execute_csharp_program (const char *assembly_path,
286 const char * const *libdirs,
287 unsigned int libdirs_count,
288 const char * const *args,
289 bool verbose, bool quiet,
290 execute_fn *executer, void *private_data)
297 const char * const *arg;
299 for (nargs = 0, arg = args; *arg != NULL; nargs++, arg++)
303 /* First try the C# implementation specified through --enable-csharp. */
304 #if CSHARP_CHOICE_PNET
305 result = execute_csharp_using_pnet (assembly_path, libdirs, libdirs_count,
306 args, nargs, verbose, quiet,
307 executer, private_data);
309 return (bool) result;
312 #if CSHARP_CHOICE_MONO
313 result = execute_csharp_using_mono (assembly_path, libdirs, libdirs_count,
314 args, nargs, verbose, quiet,
315 executer, private_data);
317 return (bool) result;
320 /* Then try the remaining C# implementations in our standard order. */
321 #if !CSHARP_CHOICE_PNET
322 result = execute_csharp_using_pnet (assembly_path, libdirs, libdirs_count,
323 args, nargs, verbose, quiet,
324 executer, private_data);
326 return (bool) result;
329 #if !CSHARP_CHOICE_MONO
330 result = execute_csharp_using_mono (assembly_path, libdirs, libdirs_count,
331 args, nargs, verbose, quiet,
332 executer, private_data);
334 return (bool) result;
337 result = execute_csharp_using_sscli (assembly_path, libdirs, libdirs_count,
338 args, nargs, verbose, quiet,
339 executer, private_data);
341 return (bool) result;
344 error (0, 0, _("C# virtual machine not found, try installing pnet"));