-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmuidl.c
836 lines (706 loc) · 20.8 KB
/
muidl.c
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
/*
* muidl.c -- IDL compiler for µiX
* Copyright 2009, 2010, 2011 Kalle A. Sandström <ksandstr@iki.fi>
*
* This file is part of µiX.
*
* µiX is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* µiX is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with µiX. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include <setjmp.h>
#include <assert.h>
#include <alloca.h>
#include <locale.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <glib.h>
#include <libIDL/IDL.h>
#include <llvm-c/Analysis.h>
#include <llvm-c/BitWriter.h>
#include <ccan/talloc/talloc.h>
#include <ccan/darray/darray.h>
#include <ccan/htable/htable.h>
#include "defs.h"
#include "llvmutil.h"
#define T_DEFS (1 << 0)
#define T_COMMON (1 << 1)
#define T_CLIENT (1 << 2)
#define T_SERVICE (1 << 3)
/* command-line arguments */
gboolean arg_verbose = FALSE;
static gboolean arg_version = FALSE, arg_verbose_idl = FALSE,
arg_make_defs = FALSE, arg_make_client = FALSE, arg_make_service = FALSE,
arg_make_common = FALSE, arg_dump_llvm = FALSE;
static gchar **arg_defines = NULL, **arg_idl_files = NULL,
**arg_include_paths = NULL, *arg_dest_path = NULL;
static unsigned int target_mask = ~0u;
static int strvlen(char **strv) __attribute__((pure));
static int msg_callback(
int level,
int num,
int line,
const char *filename,
const char *message)
{
fprintf(stderr, "%s:%d: %s\n", filename, line, message);
return 0;
}
static int compare_str_ptr(const void *a, const void *b)
{
const char *aa = *(char **)a, *bb = *(char **)b;
return strcmp(aa, bb);
}
static int strvlen(char **strv) {
return strv == NULL ? 0 : g_strv_length(strv);
}
bool is_reserved_word(const char *str)
{
/* TODO: this list is not confirmed complete. some of these are reserved by
* stdlib, rather than C99 the language.
*/
static const char *reserved[] = {
"asm", "auto",
"bool", "break",
"case", "char", "const", "continue",
"default", "do", "double",
"else", "enum", "errno", "extern",
"false", "float", "for",
"goto",
"if", "inline", "int",
"long",
"register", "return",
"short", "signed", "sizeof", "static", "struct", "switch",
"true", "typedef",
"union", "unsigned",
"void", "volatile",
"while",
};
if(str == NULL || str[0] == '\0') return false;
void *ptr = bsearch(&str, reserved, G_N_ELEMENTS(reserved),
sizeof(const char *), compare_str_ptr);
if(ptr != NULL) return true;
for(int i=1; i<G_N_ELEMENTS(reserved); i++) {
assert(strcmp(reserved[i-1], reserved[i]) < 0);
}
return false;
}
char *decapsify(const char *name)
{
const int len = strlen(name);
int ncaps = 0;
for(int i=0; i < len; i++) if(isupper(name[i])) ncaps++;
char *out = g_malloc(sizeof(char) * (len + ncaps + 1));
int o = 0;
for(int i=0; i < len; i++) {
if(isupper(name[i])) {
if(i > 0 && islower(name[i-1])) out[o++] = '_';
out[o++] = tolower(name[i]);
} else {
out[o++] = name[i];
}
}
out[o] = '\0';
return out;
}
bool is_real_nre_return_type(IDL_tree typ)
{
if(typ == NULL) return false; /* "void" is not considered "real". */
return IS_USHORT_TYPE(typ)
|| IDL_NODE_TYPE(typ) == IDLN_TYPE_OCTET;
}
IDL_tree get_type_spec(IDL_tree node)
{
if(node == NULL) return NULL;
switch(IDL_NODE_TYPE(node)) {
case IDLN_TYPE_DCL:
return get_type_spec(IDL_TYPE_DCL(node).type_spec);
case IDLN_PARAM_DCL:
return get_type_spec(IDL_PARAM_DCL(node).param_type_spec);
case IDLN_MEMBER:
return get_type_spec(IDL_MEMBER(node).type_spec);
case IDLN_LIST:
case IDLN_IDENT:
return get_type_spec(IDL_get_parent_node(node, IDLN_ANY, NULL));
default:
return node;
}
}
/* returns "true" for types that are passed by value.
*
* the type bestiary is divided into three categories:
* - value types (shorts, longs, words, etc), which are passed and returned
* by value, and are simple members of structs and unions;
* - rigid types (unions, structs, arrays), which are passed and returned by
* reference, and are simple members of structs and unions;
* - long types (sequences, "any", fixed, etc), which have special handling
* for each, and are separately allocated members of structs and unions.
*/
bool is_value_type(IDL_tree type)
{
if(type == NULL) return false; /* void is not a value type. */
switch(IDL_NODE_TYPE(type)) {
/* rigid & long types */
case IDLN_TYPE_ARRAY:
case IDLN_TYPE_SEQUENCE:
case IDLN_TYPE_STRUCT:
case IDLN_TYPE_UNION:
case IDLN_TYPE_ANY:
case IDLN_TYPE_FIXED:
case IDLN_TYPE_STRING:
case IDLN_TYPE_WIDE_STRING:
case IDLN_INTERFACE:
return false;
case IDLN_NATIVE:
return IS_WORD_TYPE(type) || IS_FPAGE_TYPE(type)
|| IS_TIME_TYPE(type);
/* value types. */
case IDLN_TYPE_INTEGER:
case IDLN_TYPE_FLOAT:
case IDLN_TYPE_CHAR:
case IDLN_TYPE_WIDE_CHAR:
case IDLN_TYPE_BOOLEAN:
case IDLN_TYPE_OCTET:
case IDLN_TYPE_ENUM:
return true;
default:
fprintf(stderr, "%s: unsupported type <%s>\n",
__FUNCTION__, IDL_NODE_TYPE_NAME(type));
abort();
}
}
/* returns the unabbreviated C name of a struct, union, interfacedcl, opdcl (in
* which case the stub's name is returned), enum, or const.
*
* the return value must be g_free()'d.
*/
char *long_name(IDL_ns ns, IDL_tree node)
{
IDL_tree ident;
switch(IDL_NODE_TYPE(node)) {
case IDLN_TYPE_STRUCT: ident = IDL_TYPE_STRUCT(node).ident; break;
case IDLN_TYPE_UNION: ident = IDL_TYPE_UNION(node).ident; break;
case IDLN_INTERFACE: ident = IDL_INTERFACE(node).ident; break;
case IDLN_TYPE_ENUM: ident = IDL_TYPE_ENUM(node).ident; break;
case IDLN_EXCEPT_DCL: ident = IDL_EXCEPT_DCL(node).ident; break;
case IDLN_CONST_DCL: ident = IDL_CONST_DCL(node).ident; break;
case IDLN_IDENT: ident = node; break; /* as for struct parameters */
case IDLN_OP_DCL:
/* FIXME: handle interface StubPrefix property! */
ident = IDL_OP_DCL(node).ident;
break;
default:
NOTDEFINED(node);
}
char *prefix;
IDL_tree mod = IDL_get_parent_node(node, IDLN_MODULE, NULL),
iface = IDL_get_parent_node(node, IDLN_INTERFACE, NULL);
if(mod == node) mod = NULL;
if(iface == node) iface = NULL;
char *modname = NULL, *ifacename = NULL;
if(mod != NULL) {
modname = decapsify(IDL_IDENT(IDL_MODULE(mod).ident).str);
}
if(iface != NULL) {
ifacename = decapsify(IDL_IDENT(IDL_INTERFACE(iface).ident).str);
}
if(mod != NULL && iface != NULL) {
prefix = g_strdup_printf("%s_%s_", modname, ifacename);
} else if(mod != NULL) prefix = g_strdup_printf("%s_", modname);
else if(iface != NULL) prefix = g_strdup_printf("%s_", ifacename);
else prefix = g_strdup("");
g_free(modname);
g_free(ifacename);
char *name = decapsify(IDL_IDENT(ident).str),
*ret = g_strconcat(prefix, name, NULL);
g_free(name);
g_free(prefix);
if(is_reserved_word(ret)) {
char *r2 = g_strconcat("_", ret, NULL);
g_free(ret);
ret = r2;
}
return ret;
}
static bool is_struct_rigid(IDL_tree type)
{
for(IDL_tree cur = IDL_TYPE_STRUCT(type).member_list;
cur != NULL;
cur = IDL_LIST(cur).next)
{
IDL_tree member = IDL_LIST(cur).data,
type = get_type_spec(IDL_MEMBER(member).type_spec);
if(!is_rigid_type(type)) return false;
}
return true;
}
IDL_tree get_array_type(IDL_tree type)
{
assert(IDL_NODE_TYPE(type) == IDLN_TYPE_ARRAY);
IDL_tree parent = type->up;
while(parent != NULL && IDL_NODE_TYPE(parent) == IDLN_LIST) {
parent = parent->up;
}
if(parent == NULL) {
fprintf(stderr, "%s: array without parent? what.\n", __func__);
abort();
}
switch(IDL_NODE_TYPE(parent)) {
case IDLN_TYPE_DCL:
return get_type_spec(IDL_TYPE_DCL(parent).type_spec);
case IDLN_MEMBER:
return get_type_spec(IDL_MEMBER(parent).type_spec);
default:
/* FIXME where the node type is something that declares typed
* members.
*/
NOTDEFINED(parent);
}
}
bool is_rigid_type(IDL_tree type)
{
if(type == NULL) return false; /* void is not a rigid type either. */
switch(IDL_NODE_TYPE(type)) {
case IDLN_TYPE_STRUCT:
return is_struct_rigid(type);
case IDLN_TYPE_ARRAY:
/* is rigid if the element type is. */
return is_rigid_type(get_array_type(type));
case IDLN_TYPE_UNION:
NOTDEFINED(type);
case IDLN_NATIVE:
if(IS_MAPGRANT_TYPE(type)) return true;
/* FALL THRU */
default:
return is_value_type(type);
}
}
const char *type_space(const char *ctype)
{
int len = strlen(ctype);
return ctype[len-1] == '*' ? "" : " ";
}
char *vtable_prefix(IDL_ns ns, IDL_tree iface)
{
char *l = long_name(ns, iface), *ret = decapsify(l);
g_free(l);
return ret;
}
static gboolean pick_ifaces(IDL_tree_func_data *tf, gpointer userdata)
{
GList **ifaces_p = userdata;
switch(IDL_NODE_TYPE(tf->tree)) {
default: return FALSE;
case IDLN_SRCFILE:
case IDLN_MODULE:
case IDLN_LIST:
return TRUE;
case IDLN_INTERFACE:
*ifaces_p = g_list_prepend(*ifaces_p, tf->tree);
/* TODO: are there interfaces within interfaces? who knows? µIDL
* will not handle these until v2.
*/
return FALSE;
}
}
static GList *collect_ifaces(IDL_tree tree, IDL_ns ns)
{
GList *ifaces = NULL;
IDL_tree_walk_in_order(tree, &pick_ifaces, &ifaces);
return g_list_reverse(ifaces);
}
void indent(struct print_ctx *pr, int change)
{
if(pr->indent_level + change < 0) {
fprintf(stderr, "warning: %s: changing %d by %d results in %d; clamped\n",
__FUNCTION__, pr->indent_level, change, pr->indent_level + change);
pr->indent_level = 0;
} else {
pr->indent_level += change;
}
}
static char *skipwhites(const char *s)
{
while(isblank(*s)) s++;
return (char *)s;
}
int code_vf(struct print_ctx *pr, const char *fmt, va_list args)
{
char *text = g_strdup_vprintf(fmt, args),
**lines = g_strsplit(skipwhites(text), "\n", 0),
prefix[pr->indent_level + 1];
for(int i=0; i<pr->indent_level; i++) prefix[i] = '\t';
prefix[pr->indent_level] = '\0';
int total = 0;
if(lines[0] == NULL) total = fprintf(pr->of, "\n");
for(int i=0; lines[i] != NULL; i++) {
total += fprintf(pr->of, "%s%s\n", lines[i][0] != '\0' ? prefix : "",
lines[i]);
}
g_strfreev(lines);
g_free(text);
return total;
}
int code_f(struct print_ctx *pr, const char *fmt, ...)
{
va_list al;
va_start(al, fmt);
int n = code_vf(pr, fmt, al);
va_end(al);
return n;
}
/* TODO: unused, remove? */
void close_brace(struct print_ctx *pr)
{
indent(pr, -1);
code_f(pr, "}");
}
char *dispatcher_name(IDL_ns ns, IDL_tree iface, char **vtprefix_p)
{
char *vtprefix = vtable_prefix(ns, iface),
*ret = g_strdup_printf("_muidl_%s_dispatch", vtprefix);
if(vtprefix_p != NULL) *vtprefix_p = vtprefix; else g_free(vtprefix);
return ret;
}
/* FIXME: return errors somehow */
static void print_into(
const char *filename,
void (*prtfn)(struct print_ctx *),
volatile struct print_ctx *pr)
{
char *path;
if(arg_dest_path == NULL) path = g_strdup(filename);
else path = g_build_path("/", arg_dest_path, filename, NULL);
FILE *f = fopen(path, "wb");
if(f == NULL) {
fprintf(stderr, "can't open `%s' for writing: %s\n", path,
strerror(errno));
exit(EXIT_FAILURE);
}
g_free(path);
FILE *oldof = pr->of;
pr->of = f;
if(setjmp(((struct print_ctx *)pr)->fail_to)) {
fprintf(stderr, "%s: fail handler invoked\n", __FUNCTION__);
fclose(f);
exit(EXIT_FAILURE);
}
(*prtfn)((struct print_ctx *)pr);
pr->of = oldof;
fclose(f);
g_string_chunk_clear(pr->tmpstrchunk);
}
typedef LLVMValueRef (*per_iface_fn)(
struct llvm_ctx *,
const struct iface_info *);
static bool free_sdf_key(const char *key, LLVMValueRef fn, void *priv) {
talloc_free(key);
return true;
}
static LLVMModuleRef make_llvm_module(
struct llvm_ctx *ctx,
LLVMModuleRef mod,
const char *basename,
per_iface_fn iface_fn,
IDL_tree_func treefn)
{
if(mod == NULL) {
mod = LLVMModuleCreateWithNameInContext(basename, ctx->ctx);
LLVMSetTarget(mod, "i486-linux-gnu");
}
ctx->module = mod;
/* reset per-module context things. */
strmap_iterate(&ctx->struct_decoder_fns, &free_sdf_key, NULL);
strmap_clear(&ctx->struct_decoder_fns);
assert(ctx->malloc_ptrs == NULL);
if(iface_fn != NULL) {
GLIST_FOREACH(cur, ctx->pr->ifaces) {
struct iface_info *inf = cur->data;
(*iface_fn)(ctx, inf);
}
}
if(treefn != NULL) {
IDL_tree_walk_in_order(ctx->pr->tree, treefn, ctx);
}
char *outmsg = NULL;
if(LLVMVerifyModule(mod, LLVMPrintMessageAction, &outmsg)) {
fprintf(stderr, "LLVM module verifier complained!\n");
LLVMDumpModule(mod);
abort();
LLVMDisposeMessage(outmsg);
}
if(arg_dump_llvm) {
LLVMDumpModule(mod);
}
ctx->module = NULL;
return mod;
}
static void compile_module_to_asm(LLVMModuleRef mod, const char *filename)
{
char *path;
if(arg_dest_path == NULL) path = g_strdup(filename);
else path = g_build_path("/", arg_dest_path, filename, NULL);
char *cmd = g_strdup_printf("llc-11 -O2 -filetype=asm -function-sections -o %s", path);
g_free(path);
FILE *p = popen(cmd, "w");
g_free(cmd);
if(p == NULL) {
fprintf(stderr, "can't open pipe to llc: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
int n = LLVMWriteBitcodeToFD(mod, fileno(p), 0, 0);
if(n != 0) {
fprintf(stderr, "bitcode writing failed; removing the file\n");
unlink(filename);
exit(EXIT_FAILURE);
}
pclose(p);
}
bool do_idl_file(const char *cppopts, const char *filename)
{
IDL_tree tree = NULL;
IDL_ns ns = NULL;
reset_warn_once();
int n = IDL_parse_filename(filename, cppopts, &msg_callback,
&tree, &ns, IDLF_PROPERTIES | IDLF_XPIDL | IDLF_SHOW_CPP_ERRORS
| IDLF_COMBINE_REOPENED_MODULES
| (arg_verbose_idl ? IDLF_VERBOSE : 0)
| IDLF_INHIBIT_INCLUDES,
IDL_WARNING1);
if(n == IDL_ERROR) {
fprintf(stderr, "IDL_parse_filename() failed.\n");
return false;
} else if(n < 0) {
perror(filename);
return false;
}
if(!verify_idl_input(ns, tree)) {
fprintf(stderr, "verification of `%s' failed.\n", filename);
IDL_ns_free(ns);
IDL_tree_free(tree);
return false;
}
GList *ifaces = collect_ifaces(tree, ns);
GLIST_FOREACH(cur, ifaces) {
IDL_tree node = cur->data;
struct iface_info *inf = analyse_interface(ns, node);
cur->data = inf;
}
const char *filepart = strrchr(filename, '/');
if(filepart == NULL) filepart = filename; else filepart++;
char basename[strlen(filepart) + 1];
memcpy(basename, filepart, strlen(filepart) + 1);
char *dot = strrchr(basename, '.');
if(dot != NULL) *dot = '\0';
char *commonname = g_strdup_printf("%s-defs.h", basename);
struct print_ctx print_ctx = {
.of = stdout,
.ns = ns, .tree = tree, .ifaces = ifaces,
.idlfilename = filename,
.common_header_name = commonname,
.tmpstrchunk = g_string_chunk_new(1024),
};
struct llvm_ctx *lc = create_llvm_ctx(&print_ctx);
dispose_llvm_ctx(replace_llvm_ctx(lc));
if(target_mask & T_DEFS) {
print_into(commonname, &print_common_header, &print_ctx);
}
if(target_mask & T_COMMON) {
LLVMModuleRef mod = make_llvm_module(lc, NULL, basename,
NULL, &iter_build_common_module);
compile_module_to_asm(mod, tmp_f(&print_ctx, "%s-common.s",
basename));
LLVMDisposeModule(mod);
}
if(target_mask & T_SERVICE) {
LLVMModuleRef mod = make_llvm_module(lc, NULL, basename,
&build_dispatcher_function, NULL);
make_llvm_module(lc, mod, basename,
&build_exception_raise_fns_for_iface, NULL);
compile_module_to_asm(mod, tmp_f(&print_ctx, "%s-service.s",
basename));
LLVMDisposeModule(mod);
}
if(target_mask & T_CLIENT) {
LLVMModuleRef mod = make_llvm_module(lc, NULL, basename,
&build_stubs_for_iface, NULL);
compile_module_to_asm(mod, tmp_f(&print_ctx, "%s-client.s",
basename));
LLVMDisposeModule(mod);
}
g_string_chunk_free(print_ctx.tmpstrchunk);
g_list_foreach(ifaces, (GFunc)&free_iface_info, NULL);
g_list_free(ifaces);
g_free(commonname);
dispose_llvm_ctx(replace_llvm_ctx(NULL));
IDL_ns_free(ns);
IDL_tree_free(tree);
return true;
}
static void parse_cmdline(int argc, char *argv[])
{
static const GOptionEntry entries[] = {
{ "cpp-define", 'D', 0, G_OPTION_ARG_STRING_ARRAY, &arg_defines,
"command-line definitions for cpp(1)", "NAME[=VALUE]" },
{ "include-path", 'I', 0, G_OPTION_ARG_STRING_ARRAY,
&arg_include_paths, "add PATH to preprocessor search list", "PATH" },
{ "destination-path", 'd', 0, G_OPTION_ARG_STRING,
&arg_dest_path, "produce files in PATH", "PATH" },
{ "dump-llvm", 0, 0, G_OPTION_ARG_NONE, &arg_dump_llvm,
"dump LLVM assembly to stderr (for debugging)", NULL },
{ "defs", 0, 0, G_OPTION_ARG_NONE, &arg_make_defs,
"produce `-defs.h' (default: produce all outputs)", NULL },
{ "client", 0, 0, G_OPTION_ARG_NONE, &arg_make_client,
"produce `-client.s'", NULL },
{ "service", 0, 0, G_OPTION_ARG_NONE, &arg_make_service,
"produce `-service.s'", NULL },
{ "common", 0, 0, G_OPTION_ARG_NONE, &arg_make_common,
"produce `-common.s'", NULL },
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &arg_verbose,
"enable verbose output", NULL },
{ "verbose-idl", 0, 0, G_OPTION_ARG_NONE, &arg_verbose_idl,
"enable verbose output from libIDL", NULL },
{ "version", 'V', 0, G_OPTION_ARG_NONE, &arg_version,
"print muidl version and exit", NULL },
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &arg_idl_files,
"IDL files", "[IDLFILE...]" },
{ NULL }
};
GError *error = NULL;
GOptionContext *oc = g_option_context_new("- µiX IDL compiler");
g_option_context_add_main_entries(oc, entries, NULL);
if(!g_option_context_parse(oc, &argc, &argv, &error)) {
fprintf(stderr, "option parsing error: %s\n", error->message);
g_error_free(error);
exit(EXIT_FAILURE);
}
g_option_context_free(oc);
unsigned int active = 0;
if(arg_make_defs) active |= T_DEFS;
if(arg_make_common) active |= T_COMMON;
if(arg_make_client) active |= T_CLIENT;
if(arg_make_service) active |= T_SERVICE;
if(active != 0) target_mask = active;
if(arg_dest_path != NULL
&& !g_file_test(arg_dest_path, G_FILE_TEST_IS_DIR))
{
fprintf(stderr, "destination `%s' doesn't look like a directory\n",
arg_dest_path);
exit(EXIT_FAILURE);
}
}
static char *join_cpp_opts(const char *parm, char **strv)
{
int num = strvlen(strv);
if(num == 0) {
/* fuck you then. */
return g_strdup("");
}
GString *buf = g_string_sized_new(num * (16 + strlen(parm)));
for(int i=0; i<num; i++) {
g_string_append_printf(buf, "%s%s '%s'", i > 0 ? " " : "",
parm, strv[i]);
}
return g_string_free(buf, FALSE);
}
/* glib's option parser doesn't easily handle -DVARIABLE=123 form, so let's
* help it along a tad by breaking them up. returns new argv.
*/
static char **separate_short_defines(int *argc, char *argv[])
{
assert(*argc > 0);
darray(char *) output = darray_new();
darray_push(output, argv[0]);
int hits = 0;
for(int i=1; i < *argc; i++) {
if(argv[i][0] == '-' && argv[i][1] == 'D' && argv[i][2] != '\0') {
darray_appends(output, strdup("-D"), argv[i] + 2);
hits++;
} else {
darray_push(output, argv[i]);
}
}
if(hits > 0) {
*argc += hits;
assert(output.size == *argc);
return output.item;
} else {
darray_free(output);
return argv;
}
}
static void *aborting_htable_alloc(struct htable *ht, size_t sz)
{
void *ptr = calloc(1, sz);
if(ptr == NULL) {
fprintf(stderr, "calloc of sz=%zu failed for ht=%p\n", sz, ht);
abort();
}
return ptr;
}
static void std_htable_free(struct htable *ht, void *ptr) {
free(ptr);
}
int main(int argc, char *argv[])
{
setlocale(LC_CTYPE, "");
htable_set_allocator(&aborting_htable_alloc, &std_htable_free);
argv = separate_short_defines(&argc, argv);
parse_cmdline(argc, argv);
if(arg_version) {
printf("muidl (µidl) version 0.1\n"
"(compiled for libIDL version %d.%d.%d)\n",
LIBIDL_MAJOR_VERSION, LIBIDL_MINOR_VERSION,
LIBIDL_MICRO_VERSION);
return EXIT_SUCCESS;
}
/* combined module disinhibition first appears in libIDL 0.8.14;
* if it gets into Debian testing before µidl is released, remove this
* part.
*/
if(LIBIDL_VERSION_CODE < LIBIDL_GEN_VERSION(0, 8, 14)) {
fprintf(stderr, "warning: libIDL version is earlier than 0.8.14. "
"spurious module inhibits may occur.\n");
}
if(strvlen(arg_idl_files) == 0) {
printf("no input files\n");
return EXIT_SUCCESS;
}
IDL_check_cast_enable(TRUE);
/* prepare options for cpp. */
char *defs = join_cpp_opts("-D", arg_defines),
*incs = join_cpp_opts("-I", arg_include_paths),
*opts = g_strdup_printf("%s %s", defs, incs);
g_free(defs);
g_free(incs);
bool ok = true;
for(int i=0; i < strvlen(arg_idl_files); i++) {
bool status = do_idl_file(opts, arg_idl_files[i]);
ok = ok && status;
}
#ifndef NDEBUG
g_free(opts);
g_strfreev(arg_defines);
g_strfreev(arg_idl_files);
g_strfreev(arg_include_paths);
#endif
return ok ? EXIT_SUCCESS : EXIT_FAILURE;
}