forked from livecode/livecode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweak_stub_maker.pl
More file actions
executable file
·560 lines (485 loc) · 13.4 KB
/
weak_stub_maker.pl
File metadata and controls
executable file
·560 lines (485 loc) · 13.4 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
#!/usr/bin/env perl
use warnings;
use Text::ParseWords;
use Getopt::Long;
my $foundation = 0;
GetOptions ('foundation|f' => \$foundation);
my $inputFile = $ARGV[0];
my $outputFile = $ARGV[1];
# Read all of the input data
open INPUT, "<$inputFile"
or die "Could not open input file \"$inputFile\": $!";
my @spec = <INPUT>;
close INPUT;
# Open the output file
open OUTPUT, ">$outputFile"
or die "Could not open output file \"$outputFile\": $!";
# Modules
my %moduleSymbols = ();
my %moduleDetails = ();
my $currentModule = undef;
# Predeclarations
sub output;
sub generateModule;
sub symbolIsReference;
sub typeListToProto;
sub symbolName;
sub symbolInputs;
sub symbolOutputs;
sub symbolIsOptional;
sub trim
{
my $trim = $_[0];
$trim =~ s/^\s+|\s+$//g ;
return $trim;
}
# Repeat for each line in the input
foreach my $line (@spec)
{
# Ignore empty lines
if ($line =~ /^\s*$/)
{
next;
}
# Ignore comment lines
if ($line =~ /^\s*#/)
{
next;
}
# Lines not beginning with tabs define modules
if (substr($line, 0, 1) ne "\t")
{
# Divide the string into the first word and the rest
$line =~ /^([^\s]*)(.*$)/ ;
my $module = trim($1);
my $details = trim($2);
$currentModule = $module;
$moduleDetails{$module} = $details;
#print STDOUT "Module: $module ($details)\n";
}
else
{
# Line defines a symbol for the current module
$line =~ s/^\s+|\s+$// ;
$moduleSymbols{$currentModule} .= $line;
#print STDOUT "\tSymbol: $line";
}
}
output "#include <stdlib.h>";
output "#include <stdio.h>";
output "#include <cstring>";
output ;
output "#define SYMBOL_PREFIX";
output ;
output "typedef void *module_t;";
output "typedef void *handler_t;";
output ;
output "#ifdef _DEBUG";
output "#include <stdint.h>";
output "extern void __MCLog(const char *file, uint32_t line, const char *format, ...);";
output "#define MCLog(...) __MCLog(__FILE__, __LINE__, __VA_ARGS__)";
output "#else";
output "#define MCLog(...) (void) (__VA_ARGS__)";
output "#endif";
# MCSupportLibraryLoad and friends have engine dependency - for example
# when weak linking to the crypto library we rely on revsecurity living
# next to the executable, which is resolved using MCcmd. We also rely on
# mappings from dynamic library names to paths which can be custom-
# defined as deploy parameters. Hence we need to use special
# implementations for access to the libfoundation system library API
# if we require no engine dependency.
if ($foundation == 1)
{
output "#define kMCStringEncodingUTF8 4";
output "extern \"C\" bool MCStringCreateWithBytes(const char *, unsigned int, int, bool, void*&);";
output "extern \"C\" void MCValueRelease(void *);";
output "extern \"C\" bool MCSLibraryCreateWithPath(void *, void*&);";
output "extern \"C\" void *MCSLibraryLookupSymbol(void *, void *);";
output ;
output "static void *MCSupportLibraryLoad(const char *p_name_cstr)";
output "{";
output " void *t_name;";
output " if (!MCStringCreateWithBytes(p_name_cstr, strlen(p_name_cstr), kMCStringEncodingUTF8, false, t_name))";
output " return NULL;";
output " void *t_handle;";
output " if (!MCSLibraryCreateWithPath(t_name, t_handle))";
output " {";
output " t_handle = NULL;";
output " }";
output " MCValueRelease(t_name);";
output " return t_handle;";
output "}";
output ;
output "static void MCSupportLibraryUnload(void *p_handle)";
output "{";
output " if (p_handle != NULL)";
output " MCValueRelease(p_handle);";
output "}";
output ;
output "static void *MCSupportLibraryLookupSymbol(void *p_handle, const char *p_name_cstr)";
output "{";
output " void *t_symbol_name;";
output " if (!MCStringCreateWithBytes(p_name_cstr, strlen(p_name_cstr), kMCStringEncodingUTF8, false, t_symbol_name))";
output " return NULL;";
output " void *t_symbol;";
output " t_symbol = MCSLibraryLookupSymbol(p_handle, t_symbol_name);";
output " MCValueRelease(t_symbol_name);";
output " return t_symbol;";
output "}";
}
else
{
output "extern \"C\" void *MCSupportLibraryLoad(const char *);";
output "extern \"C\" void MCSupportLibraryUnload(void *);";
output "extern \"C\" void *MCSupportLibraryLookupSymbol(void *, const char *);";
}
output ;
output "extern \"C\"";
output "{";
output ;
output "static int module_load(const char *p_source, module_t *r_module)";
output "{";
output " module_t t_module;";
output " t_module = (module_t)MCSupportLibraryLoad(p_source);";
output " if (t_module == NULL)";
output " return 0;";
output " *r_module = t_module;";
output " return 1;";
output "}";
output ;
# MM-2014-02-14: [[ LibOpenSSL 1.0.1e ]] Implemented module_unload for Android.
output "static int module_unload(module_t p_module)";
output "{";
output " MCSupportLibraryUnload(p_module);";
output " return 1;";
output "}";
output ;
output "static int module_resolve(module_t p_module, const char *p_name, handler_t *r_handler)";
output "{";
output " handler_t t_handler;";
output " t_handler = MCSupportLibraryLookupSymbol(p_module, p_name);";
output " if (t_handler == NULL)";
output " return 0;";
output " *r_handler = t_handler;";
output " return 1;";
output "}";
output ;
# Support code for 'could not find library' message boxes
# The order of tools for displaying messages is:
# zenity
# wish (part of Tk)
# xmessage
#
# The engine is about to die anyway so this doesn't do any error checking. It
# also doesn't wait for command completion to prevent hanging waiting for the
# user to click.
#
output ;
output "#if defined(_LINUX)";
output "static void failed_required_link(const char* libname, const char* liblist)";
output "{";
output " const char* dialog =";
output " \"zenity --error --title \\\"\$TITLE\\\" --text \\\"\$TEXT\\\" 2>/dev/null 1>/dev/null && \"";
output " \"echo \\\"wm state . withdrawn ; tk_messageBox -icon error -message \\\\\\\"\$TEXT\\\\\\\" -title \\\\\\\"\$TITLE\\\\\\\" -type ok ; exit \\\" | wish && \"";
output " \"xmessage -center -buttons OK -default OK \\\"\$TITLE:\\\" \\\"\$TEXT\\\"\" ";
output " ;";
output ;
output " char* error = new char[65536];";
output " char* command = new char[65536];";
output ;
output " snprintf(error, 65536, \"Failed to load library \\\'%s\\\' (tried %s)\", libname, liblist);";
output " snprintf(command, 65536, \"TITLE=\\\"LiveCode startup error\\\" TEXT=\\\"%s\\\" /bin/sh -c \\\'%s\\\' &\", error, dialog);";
output " MCLog( \"Fatal: failed to load library \\\'%s\\\' (tried %s)\\n\", libname, liblist);";
output " int ignored = system(command); (void)ignored;";
output " exit(-1);";
output "}";
output "#endif";
output ;
# Process each module
foreach my $module (keys %moduleDetails)
{
#print STDOUT "generateModule $module\n";
generateModule $module;
}
output "}";
close OUTPUT;
sub generateModule
{
my $module = $_[0];
my @libraries = shellwords($moduleDetails{$module});
my $unixLibrary = $libraries[0];
my $darwinLibrary = $libraries[1];
my $windowsLibrary = $libraries[2];
if (@libraries < 1)
{
$unixLibrary = "";
}
if (@libraries < 2)
{
$darwinLibrary = "";
}
if (@libraries < 3)
{
$windowsLibrary = "";
}
my @symbols = split("\n", $moduleSymbols{$module});
foreach my $symbol (@symbols)
{
if (!symbolIsReference($symbol))
{
output "typedef " . typeListToProto(symbolOutputs($symbol), 0) . "(*" . symbolName($symbol) . "_t)(" . typeListToProto(symbolInputs($symbol), 1) . ");";
output symbolName($symbol) . "_t " . symbolName($symbol) . "_ptr = NULL;";
}
else
{
output "void *" . symbolName($symbol) . "_ptr = NULL;"
}
}
my $moduleUpper = uc $module;
output ;
output "#if defined(_MACOSX) || defined(TARGET_SUBPLATFORM_IPHONE)";
output "#define MODULE_${moduleUpper}_NAME \"$darwinLibrary\"";
output "#elif defined(_LINUX) || defined(__EMSCRIPTEN__)";
output "#define MODULE_${moduleUpper}_NAME \"$unixLibrary\"";
# MM-2014-02-10: [[ LibOpenSSL 1.0.1e ]] Prefix android modules with lib.
output "#elif defined(TARGET_SUBPLATFORM_ANDROID)";
output "#define MODULE_${moduleUpper}_NAME \"$unixLibrary\"";
output "#elif defined(_WINDOWS)";
output "#define MODULE_${moduleUpper}_NAME \"$windowsLibrary\"";
output "#endif";
output ;
output "static module_t module_$module = NULL;";
output ;
# MM-2014-02-14: [[ LibOpenSSL 1.0.1e ]] Added finalise function and made sure staid variables are initialised to NULL.
output "void finalise_weak_link_$module(void)";
output "{";
output " module_unload(module_$module);";
output " module_$module = NULL;";
output "}";
output ;
output "int initialise_weak_link_${module}_with_path(const char *p_path)";
output "{";
output " module_$module = NULL;";
output " if (!module_load(p_path, &module_$module))";
output " {";
output " #ifdef _DEBUG";
output " MCLog( \"Unable to load library: %s\\n\", p_path);";
output " #endif";
output " goto err;";
output " }";
foreach my $symbol (@symbols)
{
if (symbolIsOptional($symbol))
{
output "module_resolve(module_$module, SYMBOL_PREFIX \"" . symbolName($symbol) . "\", (handler_t *)&" . symbolName($symbol) . "_ptr);";
}
else
{
output " if (!module_resolve(module_$module, SYMBOL_PREFIX \"" . symbolName($symbol) . "\", (handler_t *)&" . symbolName($symbol) . "_ptr))";
output "{";
output "#ifdef _DEBUG";
output "MCLog( \"Unable to load: " . symbolName($symbol) . "\\n\");";
output "#endif";
output "goto err; ";
output "}";
}
}
output ;
output " return 1;";
output ;
output "err:";
output " if (module_$module != NULL)";
output " module_unload(module_$module);";
output ;
output " return 0;";
output "}";
output ;
output "int initialise_weak_link_${module}(void)";
output "{";
# The list of libraries for Linux may be comma-separated
output "#if defined(_LINUX)";
foreach my $item (split(',', $unixLibrary))
{
output " if(!initialise_weak_link_${module}_with_path(\"$item\"))";
}
output "#else";
output " if (!initialise_weak_link_${module}_with_path(MODULE_${moduleUpper}_NAME))";
output "#endif";
output "{";
output "#ifdef _DEBUG";
output " MCLog( \"Warning: could not load library: $unixLibrary\\n\");";
output "#endif";
output "return 0;";
output "}";
output "return -1;";
output "}";
output ;
output "#if defined(_LINUX)";
output "void initialise_required_weak_link_${module}(void)";
output "{";
output " if (!initialise_weak_link_${module}())";
output " {";
output " failed_required_link(\"${module}\", \"${unixLibrary}\");";
output " }";
output "}";
output "#endif";
output ;
foreach my $symbol (@symbols)
{
if (symbolIsReference($symbol))
{
next;
}
my $outputs = typeListToProto(symbolOutputs($symbol), 0);
my $inputs = typeListToProto(symbolInputs($symbol), 1);
my $args = "";
if ($inputs ne "void")
{
my @inputList = split(',', $inputs);
for (my $index = 0; $index < scalar @inputList; $index++)
{
$args .= "pArg${index}, ";
}
substr($args, -2) = "";
}
output "$outputs" . symbolName($symbol) . "($inputs)";
output "{";
if (symbolName($symbol) =~ /gdk_(?!pixbuf)/)
{
#output " fprintf(stderr, \"" . symbolName($symbol) . "\\n\");";
}
if ($outputs ne "void ")
{
output " return " . symbolName($symbol) . "_ptr($args);";
}
else
{
output " " . symbolName($symbol) . "_ptr($args);";
}
output "}";
output ;
}
}
sub symbolName
{
my $symbol = $_[0];
my @items = split(':', $symbol);
my @words = split('\s+', $items[0]);
if ($words[0] eq '?' or $words[0] eq '@')
{
return $words[1];
}
return $items[0];
}
sub symbolIsOptional
{
my $symbol = $_[0];
return substr($symbol, 0, 1) eq '?';
}
sub symbolIsReference
{
my $symbol = $_[0];
return substr($symbol, 0, 1) eq '@';
}
sub symbolInputs
{
my $symbol = $_[0];
my @items = split(':', $symbol);
my $spec = $items[1];
if (scalar @items < 2)
{
$spec = "() -> ()";
}
my $inputs = substr($spec, 0, index($spec, "->"));
# Remove leading/trailing whitespace and parentheses
$inputs = trim($inputs);
$inputs = substr($inputs, 1, -1);
@items = split(',', $inputs);
my $result = "";
foreach my $item (@items)
{
$result .= trim($item) . "\n";
}
return $result;
}
sub symbolOutputs
{
my $symbol = $_[0];
my @items = split(':', $symbol);
my $spec = $items[1];
if (@items < 2)
{
$spec = "() -> ()";
}
my $outputs = substr($spec, index($spec, "->") + 3);
# Remove leading/trailing whitespace and parentheses
$outputs = trim($outputs);
$outputs = substr($outputs, 1, -1);
@items = split(',', $outputs);
my $result = "";
foreach my $item (@items)
{
$result .= trim($item) . "\n";
}
return $result;
}
sub typeListToProto
{
my $list = $_[0];
my $withArgs = $_[1];
my $proto = "";
my $index = 0;
my @lines = split("\n", $list);
foreach my $line (@lines)
{
if ($line eq "pointer")
{
$proto .= "void *";
}
elsif ($line eq "integer")
{
$proto .= "int ";
}
elsif ($line eq "double")
{
$proto .= "double ";
}
elsif ($line eq "integer64")
{
$proto .= "long long int ";
}
elsif ($line eq "intsize")
{
$proto .= "size_t ";
}
else
{
die "Unknown type specified: $line";
}
if ($withArgs)
{
$proto .= "pArg$index";
}
$proto .= ", ";
$index++;
}
substr($proto, -2) = "";
if ($proto eq "")
{
$proto = "void";
if (!$withArgs)
{
$proto .= " ";
}
}
return $proto;
}
sub output
{
my $line = $_[0];
if (@_ != 1)
{
$line = "";
}
print OUTPUT "$line\n";
}