summaryrefslogtreecommitdiff
path: root/modules/gdnative/gdnative_builders.py
blob: cd356ce5136ee68481c7d6e19cb53bc1b566db7f (plain)
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
"""Functions used to generate source files during build time

All such functions are invoked in a subprocess on Windows to prevent build flakiness.

"""
import json
from platform_methods import subprocess_main


def _spaced(e):
    return e if e[-1] == '*' else e + ' '


def _build_gdnative_api_struct_header(api):
    gdnative_api_init_macro = [
        '\textern const godot_gdnative_core_api_struct *_gdnative_wrapper_api_struct;'
    ]

    for ext in api['extensions']:
        name = ext['name']
        gdnative_api_init_macro.append(
            '\textern const godot_gdnative_ext_{0}_api_struct *_gdnative_wrapper_{0}_api_struct;'.format(name))

    gdnative_api_init_macro.append('\t_gdnative_wrapper_api_struct = options->api_struct;')
    gdnative_api_init_macro.append('\tfor (unsigned int i = 0; i < _gdnative_wrapper_api_struct->num_extensions; i++) { ')
    gdnative_api_init_macro.append('\t\tswitch (_gdnative_wrapper_api_struct->extensions[i]->type) {')

    for ext in api['extensions']:
        name = ext['name']
        gdnative_api_init_macro.append(
            '\t\t\tcase GDNATIVE_EXT_%s:' % ext['type'])
        gdnative_api_init_macro.append(
            '\t\t\t\t_gdnative_wrapper_{0}_api_struct = (godot_gdnative_ext_{0}_api_struct *)'
            ' _gdnative_wrapper_api_struct->extensions[i];'.format(name))
        gdnative_api_init_macro.append('\t\t\t\tbreak;')
    gdnative_api_init_macro.append('\t\t}')
    gdnative_api_init_macro.append('\t}')

    out = [
        '/* THIS FILE IS GENERATED DO NOT EDIT */',
        '#ifndef GODOT_GDNATIVE_API_STRUCT_H',
        '#define GODOT_GDNATIVE_API_STRUCT_H',
        '',
        '#include <gdnative/gdnative.h>',
        '#include <android/godot_android.h>',
        '#include <arvr/godot_arvr.h>',
        '#include <nativescript/godot_nativescript.h>',
        '#include <pluginscript/godot_pluginscript.h>',
        '#include <videodecoder/godot_videodecoder.h>',
        '',
        '#define GDNATIVE_API_INIT(options) do {  \\\n' + '  \\\n'.join(gdnative_api_init_macro) + '  \\\n } while (0)',
        '',
        '#ifdef __cplusplus',
        'extern "C" {',
        '#endif',
        '',
        'enum GDNATIVE_API_TYPES {',
        '\tGDNATIVE_' + api['core']['type'] + ','
    ]

    for ext in api['extensions']:
        out += ['\tGDNATIVE_EXT_' + ext['type'] + ',']

    out += ['};', '']

    def generate_extension_struct(name, ext, include_version=True):
        ret_val = []
        if ext['next']:
            ret_val += generate_extension_struct(name, ext['next'])

        ret_val += [
            'typedef struct godot_gdnative_ext_' + name + ('' if not include_version else ('_{0}_{1}'.format(ext['version']['major'], ext['version']['minor']))) + '_api_struct {',
            '\tunsigned int type;',
            '\tgodot_gdnative_api_version version;',
            '\tconst godot_gdnative_api_struct *next;'
        ]

        for funcdef in ext['api']:
            args = ', '.join(['%s%s' % (_spaced(t), n) for t, n in funcdef['arguments']])
            ret_val.append('\t%s(*%s)(%s);' % (_spaced(funcdef['return_type']), funcdef['name'], args))

        ret_val += ['} godot_gdnative_ext_' + name + ('' if not include_version else ('_{0}_{1}'.format(ext['version']['major'], ext['version']['minor']))) + '_api_struct;', '']

        return ret_val


    def generate_core_extension_struct(core):
        ret_val = []
        if core['next']:
            ret_val += generate_core_extension_struct(core['next'])

        ret_val += [
            'typedef struct godot_gdnative_core_' + ('{0}_{1}'.format(core['version']['major'], core['version']['minor'])) + '_api_struct {',
            '\tunsigned int type;',
            '\tgodot_gdnative_api_version version;',
            '\tconst godot_gdnative_api_struct *next;',
        ]

        for funcdef in core['api']:
            args = ', '.join(['%s%s' % (_spaced(t), n) for t, n in funcdef['arguments']])
            ret_val.append('\t%s(*%s)(%s);' % (_spaced(funcdef['return_type']), funcdef['name'], args))

        ret_val += ['} godot_gdnative_core_' + '{0}_{1}'.format(core['version']['major'], core['version']['minor']) + '_api_struct;', '']

        return ret_val


    for ext in api['extensions']:
        name = ext['name']
        out += generate_extension_struct(name, ext, False)

    if api['core']['next']:
        out += generate_core_extension_struct(api['core']['next'])

    out += [
        'typedef struct godot_gdnative_core_api_struct {',
        '\tunsigned int type;',
        '\tgodot_gdnative_api_version version;',
        '\tconst godot_gdnative_api_struct *next;',
        '\tunsigned int num_extensions;',
        '\tconst godot_gdnative_api_struct **extensions;',
    ]

    for funcdef in api['core']['api']:
        args = ', '.join(['%s%s' % (_spaced(t), n) for t, n in funcdef['arguments']])
        out.append('\t%s(*%s)(%s);' % (_spaced(funcdef['return_type']), funcdef['name'], args))

    out += [
        '} godot_gdnative_core_api_struct;',
        '',
        '#ifdef __cplusplus',
        '}',
        '#endif',
        '',
        '#endif // GODOT_GDNATIVE_API_STRUCT_H',
        ''
    ]
    return '\n'.join(out)


def _build_gdnative_api_struct_source(api):
    out = [
        '/* THIS FILE IS GENERATED DO NOT EDIT */',
        '',
        '#include <gdnative_api_struct.gen.h>',
        ''
    ]

    def get_extension_struct_name(name, ext, include_version=True):
        return 'godot_gdnative_ext_' + name + ('' if not include_version else ('_{0}_{1}'.format(ext['version']['major'], ext['version']['minor']))) + '_api_struct'

    def get_extension_struct_instance_name(name, ext, include_version=True):
        return 'api_extension_' + name + ('' if not include_version else ('_{0}_{1}'.format(ext['version']['major'], ext['version']['minor']))) + '_struct'

    def get_extension_struct_definition(name, ext, include_version=True):

        ret_val = []

        if ext['next']:
            ret_val += get_extension_struct_definition(name, ext['next'])

        ret_val += [
            'extern const ' + get_extension_struct_name(name, ext, include_version) + ' ' + get_extension_struct_instance_name(name, ext, include_version) + ' = {',
            '\tGDNATIVE_EXT_' + ext['type'] + ',',
            '\t{' + str(ext['version']['major']) + ', ' + str(ext['version']['minor']) + '},',
            '\t' + ('NULL' if not ext['next'] else ('(const godot_gdnative_api_struct *)&' + get_extension_struct_instance_name(name, ext['next']))) + ','
        ]

        for funcdef in ext['api']:
            ret_val.append('\t%s,' % funcdef['name'])

        ret_val += ['};\n']

        return ret_val


    def get_core_struct_definition(core):
        ret_val = []

        if core['next']:
            ret_val += get_core_struct_definition(core['next'])

        ret_val += [
            'extern const godot_gdnative_core_' + ('{0}_{1}_api_struct api_{0}_{1}'.format(core['version']['major'], core['version']['minor'])) + ' = {',
            '\tGDNATIVE_' + core['type'] + ',',
            '\t{' + str(core['version']['major']) + ', ' + str(core['version']['minor']) + '},',
            '\t' + ('NULL' if not core['next'] else ('(const godot_gdnative_api_struct *)& api_{0}_{1}'.format(core['version']['major'], core['version']['minor']))) + ','
        ]

        for funcdef in core['api']:
            ret_val.append('\t%s,' % funcdef['name'])

        ret_val += ['};\n']

        return ret_val

    for ext in api['extensions']:
        name = ext['name']
        out += get_extension_struct_definition(name, ext, False)

    out += ['', 'const godot_gdnative_api_struct *gdnative_extensions_pointers[] = {']

    for ext in api['extensions']:
        name = ext['name']
        out += ['\t(godot_gdnative_api_struct *)&api_extension_' + name + '_struct,']

    out += ['};\n']

    if api['core']['next']:
        out += get_core_struct_definition(api['core']['next'])

    out += [
        'extern const godot_gdnative_core_api_struct api_struct = {',
        '\tGDNATIVE_' + api['core']['type'] + ',',
        '\t{' + str(api['core']['version']['major']) + ', ' + str(api['core']['version']['minor']) + '},',
        '\tNULL,',
        '\t' + str(len(api['extensions'])) + ',',
        '\tgdnative_extensions_pointers,',
    ]

    for funcdef in api['core']['api']:
        out.append('\t%s,' % funcdef['name'])
    out.append('};\n')

    return '\n'.join(out)


def build_gdnative_api_struct(target, source, env):

    with open(source[0], 'r') as fd:
        api = json.load(fd)

    header, source = target
    with open(header, 'w') as fd:
        fd.write(_build_gdnative_api_struct_header(api))
    with open(source, 'w') as fd:
        fd.write(_build_gdnative_api_struct_source(api))


def _build_gdnative_wrapper_code(api):
    out = [
        '/* THIS FILE IS GENERATED DO NOT EDIT */',
        '',
        '#include <gdnative/gdnative.h>',
        '#include <nativescript/godot_nativescript.h>',
        '#include <pluginscript/godot_pluginscript.h>',
        '#include <arvr/godot_arvr.h>',
        '#include <videodecoder/godot_videodecoder.h>',
        '',
        '#include <gdnative_api_struct.gen.h>',
        '',
        '#ifdef __cplusplus',
        'extern "C" {',
        '#endif',
        '',
        'godot_gdnative_core_api_struct *_gdnative_wrapper_api_struct = 0;',
    ]

    for ext in api['extensions']:
        name = ext['name']
        out.append('godot_gdnative_ext_' + name + '_api_struct *_gdnative_wrapper_' + name + '_api_struct = 0;')

    out += ['']

    for funcdef in api['core']['api']:
        args = ', '.join(['%s%s' % (_spaced(t), n) for t, n in funcdef['arguments']])
        out.append('%s%s(%s) {' % (_spaced(funcdef['return_type']), funcdef['name'], args))

        args = ', '.join(['%s' % n for t, n in funcdef['arguments']])

        return_line = '\treturn ' if funcdef['return_type'] != 'void' else '\t'
        return_line += '_gdnative_wrapper_api_struct->' + funcdef['name'] + '(' + args + ');'

        out.append(return_line)
        out.append('}')
        out.append('')

    for ext in api['extensions']:
        name = ext['name']
        for funcdef in ext['api']:
            args = ', '.join(['%s%s' % (_spaced(t), n) for t, n in funcdef['arguments']])
            out.append('%s%s(%s) {' % (_spaced(funcdef['return_type']), funcdef['name'], args))

            args = ', '.join(['%s' % n for t, n in funcdef['arguments']])

            return_line = '\treturn ' if funcdef['return_type'] != 'void' else '\t'
            return_line += '_gdnative_wrapper_' + name + '_api_struct->' + funcdef['name'] + '(' + args + ');'

            out.append(return_line)
            out.append('}')
            out.append('')

    out += [
        '#ifdef __cplusplus',
        '}',
        '#endif'
    ]

    return '\n'.join(out)


def build_gdnative_wrapper_code(target, source, env):
    with open(source[0], 'r') as fd:
        api = json.load(fd)

    wrapper_file = target[0]
    with open(wrapper_file, 'w') as fd:
        fd.write(_build_gdnative_wrapper_code(api))


if __name__ == '__main__':
    subprocess_main(globals())