1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
//gcc -m32 monostub.m -o monostub -framework AppKit
#include <mach-o/dyld.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>
#include <dlfcn.h>
#include <errno.h>
#include <ctype.h>
#include <time.h>
#include <limits.h>
#include <libgen.h>
#include "monostub-utils.h"
typedef int (* mono_main) (int argc, char **argv);
typedef char * (* mono_get_runtime_build_info) (void);
typedef char * (* mono_parse_options_from) (const char *, int *, char **[]);
typedef void (* monoeg_g_free) (void *ptr);
typedef void (* gobject_tracker_init) (void *libmono);
static NSString *appName;
#if STATIC_REGISTRAR
// We have full static registrar enabled.
# define XM_FULL_STATIC_REGISTRAR 1
# define XAMARIN_CREATE_CLASSES xamarin_create_classes
#else
// This means we only have xammac's framework registrar
# define XAMARIN_CREATE_CLASSES xamarin_create_classes_Xamarin_Mac
#endif
extern "C" void XAMARIN_CREATE_CLASSES ();
#if NOGUI
static void
show_alert (NSString *msg, NSString *mono_download_url)
{
NSLog (@"Could not launch: %@\n%@\n%@", appName, msg, mono_download_url);
}
#else
static void
show_alert (NSString *msg, NSString *mono_download_url)
{
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:[NSString stringWithFormat:@"Could not launch %@", appName]];
[alert setInformativeText:msg];
[alert addButtonWithTitle:@"Download Mono Framework"];
[alert addButtonWithTitle:@"Cancel"];
NSInteger answer = [alert runModal];
[alert release];
if (answer == NSAlertFirstButtonReturn) {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:mono_download_url]];
}
}
#endif
static void
exit_with_message (NSString *reason)
{
NSDictionary *plist = [[NSBundle mainBundle] infoDictionary];
if (plist) {
appName = (NSString *) [plist objectForKey:(NSString *)kCFBundleNameKey];
}
NSString *fmt = @"%@\n\nPlease download and install the latest version of Mono.";
NSString *msg = [NSString stringWithFormat:fmt, reason];
NSString *mono_download_url = @"https://go.microsoft.com/fwlink/?linkid=835346";
show_alert(msg, mono_download_url);
exit (1);
}
static void *
load_symbol(const char *symbol_type, void *lib, const char *framework_name)
{
void *symbol = dlsym (lib, symbol_type);
if (!symbol) {
NSLog (@"Could not load %s(): %s", symbol_type, dlerror ());
NSString *msg = [NSString stringWithFormat:@"Failed to load the %s framework.", framework_name];
exit_with_message (msg);
}
return symbol;
}
#define LOAD_MONO_SYMBOL(symbol_type, libmono) \
(symbol_type)load_symbol(#symbol_type, libmono, "Mono");
static void
get_mono_env_options (int *ref_argc, char **ref_argv [], void *libmono)
{
const char *env = getenv ("MONO_ENV_OPTIONS");
mono_parse_options_from _mono_parse_options_from = LOAD_MONO_SYMBOL(mono_parse_options_from, libmono);
char *ret = _mono_parse_options_from (env, ref_argc, ref_argv);
if (ret) {
exit_with_message ([NSString stringWithUTF8String:ret]);
}
}
static void
run_md_bundle (NSString *bundleId, NSArray<NSString *> *arguments)
{
NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
int myPID = [[NSProcessInfo processInfo] processIdentifier];
NSRunningApplication *mdApp = nil;
NSArray<NSRunningApplication *> *runningApplications = [NSRunningApplication runningApplicationsWithBundleIdentifier:bundleId];
for (NSRunningApplication *app in runningApplications) {
if ([[[app bundleURL] path] isEqual:[bundleURL path]] && myPID != [app processIdentifier])
{
mdApp = app;
break;
}
}
if (mdApp != nil) {
for (int i = 0; i < 10; i++) {
if ([mdApp isTerminated])
break;
[NSThread sleepForTimeInterval:0.5f];
}
}
NSError *error = nil;
NSDictionary<NSWorkspaceLaunchConfigurationKey, id> *configuration = [NSDictionary dictionaryWithObject:arguments forKey:NSWorkspaceLaunchConfigurationArguments];
mdApp = [[NSWorkspace sharedWorkspace] launchApplicationAtURL:bundleURL options:NSWorkspaceLaunchAsync configuration: configuration error:&error];
if (mdApp == nil)
{
NSLog(@"Failed to start bundle %@: %@", [[NSBundle mainBundle] bundlePath], error);
exit (1);
}
exit (0);
}
static void
correct_locale(void)
{
NSString *preferredLanguage = [[[NSBundle mainBundle] preferredLocalizations] firstObject];
// Apply fixups such as zh_HANS/HANT -> zh_CN/TW
// Strip other languages of remainder so we choose a generic culture.
if ([preferredLanguage hasPrefix:@"zh-Hans"])
preferredLanguage = @"zh_CN";
else if ([preferredLanguage hasPrefix:@"zh-Hant"])
preferredLanguage = @"zh_TW";
preferredLanguage = [preferredLanguage stringByReplacingOccurrencesOfString:@"-" withString:@"_"];
const char *value = [preferredLanguage UTF8String];
setenv("MONODEVELOP_STUB_LANGUAGE", value, 1);
setenv("LANGUAGE", value, 1);
setenv("LC_CTYPE", value, 1);
}
static void
try_load_gobject_tracker (void *libmono)
{
void *gobject_tracker = dlopen("libgobject-tracker.dylib", RTLD_LAZY);
if (!gobject_tracker)
return;
gobject_tracker_init _gobject_tracker_init = (gobject_tracker_init) dlsym (gobject_tracker, "gobject_tracker_init");
if (!_gobject_tracker_init)
return;
_gobject_tracker_init (libmono);
NSLog (@"Loaded gobject tracker\n");
}
static void
run_md_bundle_if_needed(int argc, char **argv)
{
NSString *bundleId = [[NSBundle mainBundle] bundleIdentifier];
// if we are running inside an app bundle and --start-app-bundle has been passed
// run the actual bundle and exit.
if (bundleId && argc > 1 && !strcmp(argv[1], "--start-app-bundle")) {
NSArray<NSString *> *arguments = [NSArray array];
if (argc > 2) {
int new_argc = argc - 2;
NSMutableArray<NSString *> *array = [NSMutableArray arrayWithCapacity:new_argc];
for (int i = 2; i < new_argc; i++)
[array addObject:[NSString stringWithUTF8String:argv[i]]];
arguments = array;
}
run_md_bundle (bundleId, arguments);
}
}
static bool
should_load_xammac_registrar()
{
#if XM_FULL_STATIC_REGISTRAR
char *registrar_toggle = getenv("MD_DISABLE_STATIC_REGISTRAR");
if (registrar_toggle)
return false;
return dlopen("libvsmregistrar.dylib", RTLD_LAZY);
#else
return true;
#endif
}
static void
load_xammac()
{
void *libxammac = dlopen("libxammac.dylib", RTLD_LAZY);
if (!libxammac) {
NSLog (@"Failed to load libxammac.dylib %s", dlerror ());
exit_with_message (@"This application requires Xamarin.Mac native library side-by-side.");
}
if (should_load_xammac_registrar()) {
XAMARIN_CREATE_CLASSES ();
}
}
int
main (int argc, char **argv)
{
int new_argc;
char **new_argv;
mono_main _mono_main;
@autoreleasepool {
//clock_t start = clock();
// Check if we are running inside an actual app bundle. If we are not, then assume we're being run
// as part of `make run` and then binDir should be '.'
appName = [[NSProcessInfo processInfo] processName];
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *binDirFullPath = [[mainBundle resourcePath] stringByAppendingPathComponent:@"lib/monodevelop/bin"];
BOOL isDir = NO;
if (![[NSFileManager defaultManager] fileExistsAtPath: binDirFullPath isDirectory: &isDir] || !isDir)
binDirFullPath = [[mainBundle executablePath] stringByDeletingLastPathComponent];
run_md_bundle_if_needed(argc, argv);
if (update_environment (binDirFullPath)) {
//printf ("Updated the environment.\n");
return execv (argv[0], argv);
}
correct_locale();
//printf ("Running main app.\n");
struct rlimit limit;
if (getrlimit (RLIMIT_NOFILE, &limit) == 0 && limit.rlim_cur < 1024) {
limit.rlim_cur = MIN (limit.rlim_max, 1024);
setrlimit (RLIMIT_NOFILE, &limit);
}
// can be overridden with plist string MonoMinVersion
NSString *req_mono_version = @"5.18.1.24";
NSDictionary *plist = [mainBundle infoDictionary];
if (plist) {
NSString *version_obj = [plist objectForKey:@"MonoMinVersion"];
if (version_obj && [version_obj length] > 0)
req_mono_version = version_obj;
}
// Xamarin.Mac sets MONO_REGISTRY_PATH to ~/Library/Application Support/...,
// so in the case of the Xamarin Installer, this gets set to a non
// existing directory, and when VSmac is started from the installer and
// code that uses the registry (like Publish to Azure) is ran, it tries
// to access the non existing directory, resulting in an exception that
// prevents the code from running correctly, so unset it here.
// See https://devdiv.visualstudio.com/DevDiv/_workitems/edit/896438
unsetenv ("MONO_REGISTRY_PATH");
#if HYBRID_SUSPEND_ABORT_DEBUG
setenv ("MONO_SLEEP_ABORT_LIMIT", "5000", 0);
#endif
setenv ("MONO_GC_PARAMS", "major=marksweep-conc,nursery-size=8m", 0);
void *libmono = dlopen ("libmonosgen-2.0.dylib", RTLD_LAZY);
if (libmono == NULL) {
NSLog (@"Failed to load libmonosgen-2.0.dylib: %s", dlerror ());
NSString *msg = [NSString stringWithFormat:@"This application requires Mono %@ or newer.", req_mono_version];
exit_with_message (msg);
}
load_xammac();
try_load_gobject_tracker (libmono);
monoeg_g_free _g_free = LOAD_MONO_SYMBOL(monoeg_g_free, libmono);
_mono_main = LOAD_MONO_SYMBOL(mono_main, libmono);
mono_get_runtime_build_info _mono_get_runtime_build_info = LOAD_MONO_SYMBOL(mono_get_runtime_build_info, libmono);
char *mono_version = _mono_get_runtime_build_info ();
if (!check_mono_version (mono_version, [req_mono_version UTF8String])) {
NSString *msg = [NSString stringWithFormat:@"This application requires a newer version (%@+) of the Mono framework.", req_mono_version];
exit_with_message (msg);
}
_g_free (mono_version);
// enable --debug so that we can get useful stack traces and add mono env options
int mono_argc = 1;
char **mono_argv = (char **) malloc (sizeof (char *) * mono_argc);
mono_argv[0] = (char *) "--debug";
get_mono_env_options (&mono_argc, &mono_argv, libmono);
// append original arguments
new_argc = mono_argc + argc + 1;
new_argv = (char **) malloc (sizeof (char *) * new_argc);
int n = 0;
new_argv[n++] = argv[0];
for (int i = 0; i < mono_argc; i++) {
new_argv[n++] = strdup (mono_argv[i]);
if (i > 0)
_g_free (mono_argv [i]);
}
_g_free(mono_argv);
// append old arguments
NSString *exePath = [NSString stringWithFormat:@"%@/%@.exe", binDirFullPath, appName];
new_argv[n++] = strdup ([exePath UTF8String]);
for (int i = 1; i < argc; i++)
new_argv[n++] = argv[i];
//clock_t end = clock();
//printf("%f seconds to start\n", (float)(end - start) / CLOCKS_PER_SEC);
}
return _mono_main (new_argc, new_argv);
}
|