forked from radare/valabind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.vala
170 lines (158 loc) · 5.33 KB
/
main.vala
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
/* Copyright GPL3 - 2009-2013 -- pancake */
private static string[] files;
private static string vapidir;
private static string library;
private static bool show_version;
private static bool glibmode;
private static bool camelgetters;
private static bool cxxmode;
private static bool dlangoutput;
private static bool cxxoutput;
private static bool vlangoutput;
private static bool nodeoutput;
private static bool swigoutput;
private static bool ctypesoutput;
private static bool giroutput;
private static bool gooutput;
private static string modulename;
private static string? output;
[CCode (array_length = false, array_null_terminated = true)]
private static string[] packages;
[CCode (array_length = false, array_null_terminated = true)]
private static string[] include_dirs;
[CCode (array_length = false, array_null_terminated = true)]
private static string[] namespaces;
[CCode (array_length = false, array_null_terminated = true)]
private static string[] defines;
private const OptionEntry[] options = {
{ "define", 'D', 0, OptionArg.STRING_ARRAY,
ref defines, "define SYMBOL", "SYMBOL" },
{ "pkg", 0, 0, OptionArg.STRING_ARRAY,
ref packages, "include binding for PACKAGE", "PACKAGE..." },
{ "vapidir", 'V', 0, OptionArg.STRING,
ref vapidir, "define alternative vapi directory", "VAPIDIR" },
{ "include-dir", 'I', 0, OptionArg.STRING_ARRAY,
ref include_dirs, "add include path", "INCLUDEDIR" },
{ "version", 'v', 0, OptionArg.NONE,
ref show_version, "show version information", null },
{ "output", 'o', 0, OptionArg.STRING,
ref output, "specify output file name", "OUTPUT" },
{ "module", 'm', 0, OptionArg.STRING,
ref modulename, "specify module name", "NAME" },
{ "namespace", 'N', 0, OptionArg.STRING_ARRAY,
ref namespaces, "include namespace in the output", "NSPACE" },
{ "cxx-swig", 'x', 0, OptionArg.NONE,
ref cxxmode, "generate C++ code for SWIG", null },
{ "glib", 0, 0, OptionArg.NONE,
ref glibmode, "call g_type_init before any constructor", null },
{ "swig", 0, 0, OptionArg.NONE,
ref swigoutput, "generate swig interface code", null },
{ "camel-getters", 0, 0, OptionArg.NONE,
ref camelgetters, "translate {get,set}_foo into {get,set}Foo", null },
{ "node-ffi", 0, 0, OptionArg.NONE,
ref nodeoutput, "generate node-ffi interface", null },
{ "library", 'l', 0, OptionArg.STRING,
ref library, "library to link", null },
{ "ctypes", 0, 0, OptionArg.NONE,
ref ctypesoutput, "generate python ctypes interface", null },
{ "gir", 0, 0, OptionArg.NONE,
ref giroutput, "generate GIR (GObject-Introspection-Runtime)", null },
{ "cxx", 0, 0, OptionArg.NONE,
ref cxxoutput, "generate C++ interface code", null },
{ "dlang", 0, 0, OptionArg.NONE,
ref dlangoutput, "generate D bindings", null },
{ "go", 0, 0, OptionArg.NONE,
ref gooutput, "generate Go bindings", null },
{ "vlang", 0, 0, OptionArg.NONE,
ref vlangoutput, "generate bindings for Vlang", null },
{ "", 0, 0, OptionArg.FILENAME_ARRAY,
ref files, "vala/vapi input files", "FILE FILE .." },
{ null }
};
int main (string[] args) {
output = null;
vapidir = ".";
files = { "" };
try {
var opt_context = new OptionContext ("- valabind");
opt_context.set_help_enabled (true);
opt_context.add_main_entries (options, null);
opt_context.parse (ref args);
} catch (OptionError e) {
stderr.printf ("%s\nTry --help\n", e.message);
return 1;
}
if (show_version) {
print ("%s\n", version_string);
return 0;
}
if (modulename == null)
error ("No modulename specified. Use --module or --help");
if (files.length == 0)
error ("No files given");
ValabindWriter writer = null;
int count = 0;
if (swigoutput && count++ == 0) {
writer = new SwigWriter (cxxmode);
writer.add_define ("VALABIND_SWIG");
}
if (nodeoutput && count++ == 0) {
writer = new NodeFFIWriter ();
writer.add_define ("VALABIND_NODEJS");
}
if (ctypesoutput && count++ == 0) {
writer = new CtypesWriter ();
writer.add_define ("VALABIND_CTYPES");
}
if (giroutput && count++ == 0) {
writer = new GirWriter ();
writer.add_define ("VALABIND_GIR");
}
if (dlangoutput && count++ == 0) {
writer = new DlangWriter ();
writer.add_define ("VALABIND_DLANG");
}
if (cxxoutput && count++ == 0) {
writer = new CxxWriter ();
writer.add_define ("VALABIND_CXX");
}
if (vlangoutput && count++ == 0) {
writer = new VlangWriter ();
writer.add_define ("VALABIND_VLANG");
}
if (gooutput && count++ == 0) {
writer = new GoWriter ();
writer.add_define ("VALABIND_GO");
}
if (count == 0)
error ("No output mode specified. Try --help\n");
else if (count > 1)
error ("Cannot specify more than one output mode\n");
writer.modulename = modulename;
writer.library = (library != null)? library: modulename;
writer.include_dirs = include_dirs;
writer.namespaces = namespaces;
writer.camelgetters = camelgetters;
writer.init (vapidir, glibmode);
if (packages != null)
foreach (var pkg in packages)
writer.add_external_package (pkg);
if (defines != null) {
foreach (string define in defines) {
writer.add_define (define);
}
}
// TODO: passing more than one source doesnt seems to work :/
foreach (var file in files) {
if (file.index_of (".vapi") == -1) {
writer.pkgmode = true;
writer.pkgname = file;
}
writer.add_source_file (file);
}
writer.parse ();
if (output == null)
output = writer.get_filename (modulename);
writer.write (output);
return 0;
}