1 /* Execute 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/>. */
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 __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.
76 With Mono, the MONO_PATH is a colon separated list of pathnames. (On
77 Windows: semicolon separated list of pathnames.)
79 We try the CIL interpreters in the following order:
80 1. "mono", because it is a partially free system but doesn't integrate
82 2. "clix", although it is not free, because it is a kind of "reference
83 implementation" of C#.
84 But the order can be changed through the --enable-csharp configuration
89 execute_csharp_using_mono (const char *assembly_path
,
90 const char * const *libdirs
,
91 unsigned int libdirs_count
,
92 const char * const *args
, unsigned int nargs
,
93 bool verbose
, bool quiet
,
94 execute_fn
*executer
, void *private_data
)
96 static bool mono_tested
;
97 static bool mono_present
;
101 /* Test for presence of mono:
102 "mono --version >/dev/null 2>/dev/null" */
107 argv
[1] = "--version";
109 exitstatus
= execute ("mono", "mono", argv
, NULL
,
110 false, false, true, true,
112 mono_present
= (exitstatus
== 0);
120 (const char **) xmalloca ((2 + nargs
+ 1) * sizeof (const char *));
125 old_monopath
= set_monopath (libdirs
, libdirs_count
, false, verbose
);
128 argv
[1] = assembly_path
;
129 for (i
= 0; i
<= nargs
; i
++)
130 argv
[2 + i
] = args
[i
];
134 char *command
= shell_quote_argv (argv
);
135 printf ("%s\n", command
);
139 err
= executer ("mono", "mono", argv
, private_data
);
141 /* Reset MONO_PATH. */
142 reset_monopath (old_monopath
);
153 execute_csharp_using_sscli (const char *assembly_path
,
154 const char * const *libdirs
,
155 unsigned int libdirs_count
,
156 const char * const *args
, unsigned int nargs
,
157 bool verbose
, bool quiet
,
158 execute_fn
*executer
, void *private_data
)
160 static bool clix_tested
;
161 static bool clix_present
;
165 /* Test for presence of clix:
166 "clix >/dev/null 2>/dev/null ; test $? = 1" */
172 exitstatus
= execute ("clix", "clix", argv
, NULL
,
173 false, false, true, true,
175 clix_present
= (exitstatus
== 0 || exitstatus
== 1);
183 (const char **) xmalloca ((2 + nargs
+ 1) * sizeof (const char *));
187 /* Set clix' PATH variable. */
188 old_clixpath
= set_clixpath (libdirs
, libdirs_count
, false, verbose
);
191 argv
[1] = assembly_path
;
192 for (i
= 0; i
<= nargs
; i
++)
193 argv
[2 + i
] = args
[i
];
197 char *command
= shell_quote_argv (argv
);
198 printf ("%s\n", command
);
202 err
= executer ("clix", "clix", argv
, private_data
);
204 /* Reset clix' PATH variable. */
205 reset_clixpath (old_clixpath
);
216 execute_csharp_program (const char *assembly_path
,
217 const char * const *libdirs
,
218 unsigned int libdirs_count
,
219 const char * const *args
,
220 bool verbose
, bool quiet
,
221 execute_fn
*executer
, void *private_data
)
228 const char * const *arg
;
230 for (nargs
= 0, arg
= args
; *arg
!= NULL
; nargs
++, arg
++)
234 /* First try the C# implementation specified through --enable-csharp. */
235 #if CSHARP_CHOICE_MONO
236 result
= execute_csharp_using_mono (assembly_path
, libdirs
, libdirs_count
,
237 args
, nargs
, verbose
, quiet
,
238 executer
, private_data
);
240 return (bool) result
;
243 /* Then try the remaining C# implementations in our standard order. */
244 #if !CSHARP_CHOICE_MONO
245 result
= execute_csharp_using_mono (assembly_path
, libdirs
, libdirs_count
,
246 args
, nargs
, verbose
, quiet
,
247 executer
, private_data
);
249 return (bool) result
;
252 result
= execute_csharp_using_sscli (assembly_path
, libdirs
, libdirs_count
,
253 args
, nargs
, verbose
, quiet
,
254 executer
, private_data
);
256 return (bool) result
;
259 error (0, 0, _("C# virtual machine not found, try installing mono"));