summaryrefslogtreecommitdiff
path: root/modules/gdnative/SCsub
blob: 31178be97357051b49ff75ec8cb6fffbd1a1c2da (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
#!/usr/bin/env python

Import('env')

gdn_env = env.Clone()

gdn_env.add_source_files(env.modules_sources, "gd_native_library_editor.cpp")
gdn_env.add_source_files(env.modules_sources, "gdnative.cpp")
gdn_env.add_source_files(env.modules_sources, "register_types.cpp")
gdn_env.add_source_files(env.modules_sources, "gdnative/*.cpp")
gdn_env.add_source_files(env.modules_sources, "nativescript/*.cpp")

gdn_env.Append(CPPFLAGS=['-DGDAPI_BUILT_IN'])
gdn_env.Append(CPPPATH=['#modules/gdnative/include/'])

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

def _build_gdnative_api_struct_header(api):
    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 <nativescript/godot_nativescript.h>',
        '',
        '#ifdef __cplusplus',
        'extern "C" {',
        '#endif',
        '',
        'typedef struct godot_gdnative_api_struct {'
    ]

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

    out += [
        '} godot_gdnative_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>',
        '',
        'extern const godot_gdnative_api_struct api_struct = {'
    ]

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

    return '\n'.join(out)

def build_gdnative_api_struct(target, source, env):
    import json
    from collections import OrderedDict

    with open(source[0].path, 'r') as fd:
        # Keep the json ordered
        api = json.load(fd, object_pairs_hook=OrderedDict)

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

_, gensource = gdn_env.Command(['include/gdnative_api_struct.gen.h', 'gdnative_api_struct.gen.cpp'],
                               'gdnative_api.json', build_gdnative_api_struct)
gdn_env.add_source_files(env.modules_sources, [gensource])

if "platform" in env and env["platform"] in ["x11", "iphone"]:
    env.Append(LINKFLAGS=["-rdynamic"])

env.use_ptrcall = True