123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- #!/usr/bin/env python
- Import('env')
- gdn_env = env.Clone()
- 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.add_source_files(env.modules_sources, "gdnative_library_singleton_editor.cpp")
- gdn_env.add_source_files(env.modules_sources, "gdnative_library_editor_plugin.cpp")
- gdn_env.Append(CPPPATH=['#modules/gdnative/include/'])
- SConscript("arvr/SCsub")
- SConscript("pluginscript/SCsub")
- 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 name in api['extensions']:
- 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 (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 name in api['extensions']:
- gdnative_api_init_macro.append(
- '\t\t\tcase GDNATIVE_EXT_%s:' % api['extensions'][name]['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 <arvr/godot_arvr.h>',
- '#include <nativescript/godot_nativescript.h>',
- '#include <pluginscript/godot_pluginscript.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 name in api['extensions']:
- out += ['\tGDNATIVE_EXT_' + api['extensions'][name]['type'] + ',']
- out += ['};', '']
- for name in api['extensions']:
- out += [
- 'typedef struct godot_gdnative_ext_' + name + '_api_struct {',
- '\tunsigned int type;',
- '\tgodot_gdnative_api_version version;',
- '\tconst godot_gdnative_api_struct *next;'
- ]
- for funcdef in api['extensions'][name]['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_ext_' + name + '_api_struct;', '']
- 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>',
- ''
- ]
- for name in api['extensions']:
- out += [
- 'extern const godot_gdnative_ext_' + name + '_api_struct api_extension_' + name + '_struct = {',
- '\tGDNATIVE_EXT_' + api['extensions'][name]['type'] + ',',
- '\t{' + str(api['extensions'][name]['version']['major']) + ', ' + str(api['extensions'][name]['version']['minor']) + '},',
- '\tNULL,'
- ]
- for funcdef in api['extensions'][name]['api']:
- out.append('\t%s,' % funcdef['name'])
- out += ['};\n']
- out += ['', 'const godot_gdnative_api_struct *gdnative_extensions_pointers[] = {']
- for name in api['extensions']:
- out += ['\t(godot_gdnative_api_struct *)&api_extension_' + name + '_struct,']
- out += ['};\n']
- 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):
- import json
- from collections import OrderedDict
- with open(source[0].path, 'r') as fd:
- api = json.load(fd)
- 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.CommandNoCache(['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])
- env.use_ptrcall = True
- 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 <gdnative_api_struct.gen.h>',
- '',
- '#ifdef __cplusplus',
- 'extern "C" {',
- '#endif',
- '',
- 'godot_gdnative_core_api_struct *_gdnative_wrapper_api_struct = 0;',
- ]
- for name in api['extensions']:
- 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 name in api['extensions']:
- for funcdef in api['extensions'][name]['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):
- import json
- with open(source[0].path, 'r') as fd:
- api = json.load(fd)
- wrapper_file = target[0]
- with open(wrapper_file.path, 'w') as fd:
- fd.write(_build_gdnative_wrapper_code(api))
- if ARGUMENTS.get('gdnative_wrapper', False):
- #build wrapper code
- gensource, = gdn_env.CommandNoCache('gdnative_wrapper_code.gen.cpp', 'gdnative_api.json', build_gdnative_wrapper_code)
- gd_wrapper_env = env.Clone()
- gd_wrapper_env.Append(CPPPATH=['#modules/gdnative/include/'])
- if gd_wrapper_env['use_lto']:
- if not env.msvc:
- gd_wrapper_env.Append(CCFLAGS=['-fno-lto'])
- gd_wrapper_env.Append(LINKFLAGS=['-fno-lto'])
- else:
- gd_wrapper_env.Append(CCFLAGS=['/GL-'])
- gd_wrapper_env.Append(LINKFLAGS=['/LTCG:OFF'])
- if not env.msvc:
- gd_wrapper_env.Append(CCFLAGS=['-fPIC'])
- lib = gd_wrapper_env.add_library("#bin/gdnative_wrapper_code", [gensource])
|