virtualx-engine/modules/gdnative/SCsub

298 lines
10 KiB
Text
Raw Normal View History

#!/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, "android/*.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("net/SCsub")
SConscript("arvr/SCsub")
2017-10-08 23:47:38 +02:00
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;'
]
2017-11-16 22:05:47 +01:00
2018-05-10 21:36:26 +02:00
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))
2017-11-16 22:05:47 +01:00
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) {')
2017-11-16 22:05:47 +01:00
2018-05-10 21:36:26 +02:00
for ext in api['extensions']:
name = ext['name']
gdnative_api_init_macro.append(
2018-05-10 21:36:26 +02:00
'\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}')
2017-11-16 22:05:47 +01:00
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>',
2017-10-08 23:47:38 +02:00
'#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'] + ','
]
2018-05-10 21:36:26 +02:00
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
2018-05-10 21:36:26 +02:00
for ext in api['extensions']:
name = ext['name']
out += generate_extension_struct(name, ext, False)
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
2018-05-10 21:36:26 +02:00
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[] = {']
2018-05-10 21:36:26 +02:00
for ext in api['extensions']:
name = ext['name']
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))
2018-06-21 06:33:25 +02:00
_, 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>',
2017-10-08 23:47:38 +02:00
'#include <pluginscript/godot_pluginscript.h>',
2017-11-16 22:05:47 +01:00
'#include <arvr/godot_arvr.h>',
'',
'#include <gdnative_api_struct.gen.h>',
'',
'#ifdef __cplusplus',
'extern "C" {',
'#endif',
2017-11-16 22:05:47 +01:00
'',
'godot_gdnative_core_api_struct *_gdnative_wrapper_api_struct = 0;',
]
2018-05-10 21:36:26 +02:00
for ext in api['extensions']:
name = ext['name']
out.append('godot_gdnative_ext_' + name + '_api_struct *_gdnative_wrapper_' + name + '_api_struct = 0;')
2017-11-16 22:05:47 +01:00
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('')
2018-05-10 21:36:26 +02:00
for ext in api['extensions']:
name = ext['name']
for funcdef in ext['api']:
2017-11-16 22:05:47 +01:00
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):
2018-05-10 21:36:26 +02:00
#build wrapper code
2018-06-21 06:33:25 +02:00
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/'])
2018-01-04 19:42:02 +01:00
if gd_wrapper_env['use_lto']:
if not env.msvc:
gd_wrapper_env.Append(CCFLAGS=['-fno-lto'])
gd_wrapper_env.Append(LINKFLAGS=['-fno-lto'])
2018-01-04 19:42:02 +01:00
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])