Fix and improve the build system

- Fix automatic architecture detection
- Fix compiling with MinGW on Linux
  - MinGW on Windows is still not working though
- Default to Clang on macOS
- Remove redundant `use_custom_api_file` option
- Format SConstruct using Flake8

This closes #245.
pull/261/head
Hugo Locurcio 2019-03-27 00:51:51 +01:00
parent df04c4097f
commit 976a188837
No known key found for this signature in database
GPG Key ID: 39E8F8BE30B0A49C
2 changed files with 275 additions and 210 deletions

View File

@ -1,14 +1,16 @@
#!python #!/usr/bin/env python
import os, subprocess, platform, sys import os
import sys
def add_sources(sources, dir, extension): def add_sources(sources, dir, extension):
for f in os.listdir(dir): for f in os.listdir(dir):
if f.endswith('.' + extension): if f.endswith('.' + extension):
sources.append(dir + '/' + f) sources.append(dir + '/' + f)
# Try to detect the host platform automatically
# Try to detect the host platform automatically.
# This is used if no `platform` argument is passed # This is used if no `platform` argument is passed
if sys.platform.startswith('linux'): if sys.platform.startswith('linux'):
host_platform = 'linux' host_platform = 'linux'
@ -17,50 +19,86 @@ elif sys.platform == 'darwin':
elif sys.platform == 'win32': elif sys.platform == 'win32':
host_platform = 'windows' host_platform = 'windows'
else: else:
raise ValueError('Could not detect platform automatically, please specify with platform=<platform>') raise ValueError(
'Could not detect platform automatically, please specify with '
'platform=<platform>'
)
opts = Variables([], ARGUMENTS) opts = Variables([], ARGUMENTS)
opts.Add(EnumVariable(
opts.Add(EnumVariable('platform', 'Target platform', host_platform, 'platform',
allowed_values=('linux', 'osx', 'windows'), 'Target platform',
ignorecase=2)) host_platform,
opts.Add(EnumVariable('bits', 'Target platform bits', 'default', ('default', '32', '64'))) allowed_values=('linux', 'osx', 'windows'),
opts.Add(BoolVariable('use_llvm', 'Use the LLVM compiler - only effective when targeting Linux', False)) ignorecase=2
opts.Add(BoolVariable('use_mingw', 'Use the MinGW compiler - only effective on Windows', False)) ))
opts.Add(EnumVariable(
'bits',
'Target platform bits',
'default',
('default', '32', '64')
))
opts.Add(BoolVariable(
'use_llvm',
'Use the LLVM compiler - only effective when targeting Linux',
False
))
opts.Add(BoolVariable(
'use_mingw',
'Use the MinGW compiler instead of MSVC - only effective on Windows',
False
))
# Must be the same setting as used for cpp_bindings # Must be the same setting as used for cpp_bindings
opts.Add(EnumVariable('target', 'Compilation target', 'debug', opts.Add(EnumVariable(
allowed_values=('debug', 'release'), 'target',
ignorecase=2)) 'Compilation target',
opts.Add(PathVariable('headers_dir', 'Path to the directory containing Godot headers', 'godot_headers', PathVariable.PathIsDir)) 'debug',
opts.Add(BoolVariable('use_custom_api_file', 'Use a custom JSON API file', False)) allowed_values=('debug', 'release'),
opts.Add(PathVariable('custom_api_file', 'Path to the custom JSON API file', None, PathVariable.PathIsFile)) ignorecase=2
opts.Add(BoolVariable('generate_bindings', 'Generate GDNative API bindings', False)) ))
opts.Add(PathVariable(
unknown = opts.UnknownVariables() 'headers_dir',
if unknown: 'Path to the directory containing Godot headers',
print("Unknown variables:" + unknown.keys()) 'godot_headers',
Exit(1) PathVariable.PathIsDir
))
opts.Add(PathVariable(
'custom_api_file',
'Path to a custom JSON API file',
None,
PathVariable.PathIsFile
))
opts.Add(BoolVariable(
'generate_bindings',
'Generate GDNative API bindings',
False
))
env = Environment() env = Environment()
opts.Update(env) opts.Update(env)
Help(opts.GenerateHelpText(env)) Help(opts.GenerateHelpText(env))
# This makes sure to keep the session environment variables on Windows is64 = sys.maxsize > 2**32
# This way, you can run SCons in a Visual Studio 2017 prompt and it will find all the required tools if (
if env['platform'] == 'windows': env['TARGET_ARCH'] == 'amd64' or
env['TARGET_ARCH'] == 'emt64' or
env['TARGET_ARCH'] == 'x86_64'
):
is64 = True
if env['bits'] == 'default':
env['bits'] = '64' if is64 else '32'
# This makes sure to keep the session environment variables on Windows.
# This way, you can run SCons in a Visual Studio 2017 prompt and it will find
# all the required tools
if host_platform == 'windows':
if env['bits'] == '64': if env['bits'] == '64':
env = Environment(TARGET_ARCH='amd64') env = Environment(TARGET_ARCH='amd64')
elif env['bits'] == '32': elif env['bits'] == '32':
env = Environment(TARGET_ARCH='x86') env = Environment(TARGET_ARCH='x86')
else:
print("Warning: bits argument not specified, target arch is=" + env['TARGET_ARCH'])
opts.Update(env)
is64 = False opts.Update(env)
if (env['platform'] == 'osx' or env['TARGET_ARCH'] == 'amd64' or env['TARGET_ARCH'] == 'emt64' or env['TARGET_ARCH'] == 'x86_64'):
is64 = True
if env['bits'] == 'default':
env['bits'] = '64' if is64 else '32'
if env['platform'] == 'linux': if env['platform'] == 'linux':
if env['use_llvm']: if env['use_llvm']:
@ -82,11 +120,22 @@ if env['platform'] == 'linux':
env.Append(LINKFLAGS=['-m32']) env.Append(LINKFLAGS=['-m32'])
elif env['platform'] == 'osx': elif env['platform'] == 'osx':
# Use Clang on macOS by default
env['CXX'] = 'clang++'
if env['bits'] == '32': if env['bits'] == '32':
raise ValueError('Only 64-bit builds are supported for the macOS target.') raise ValueError(
'Only 64-bit builds are supported for the macOS target.'
)
env.Append(CCFLAGS=['-g', '-std=c++14', '-arch', 'x86_64']) env.Append(CCFLAGS=['-g', '-std=c++14', '-arch', 'x86_64'])
env.Append(LINKFLAGS=['-arch', 'x86_64', '-framework', 'Cocoa', '-Wl,-undefined,dynamic_lookup']) env.Append(LINKFLAGS=[
'-arch',
'x86_64',
'-framework',
'Cocoa',
'-Wl,-undefined,dynamic_lookup',
])
if env['target'] == 'debug': if env['target'] == 'debug':
env.Append(CCFLAGS=['-Og']) env.Append(CCFLAGS=['-Og'])
@ -101,40 +150,56 @@ elif env['platform'] == 'windows':
env.Append(CCFLAGS=['/EHsc', '/D_DEBUG', '/MDd']) env.Append(CCFLAGS=['/EHsc', '/D_DEBUG', '/MDd'])
elif env['target'] == 'release': elif env['target'] == 'release':
env.Append(CCFLAGS=['/O2', '/EHsc', '/DNDEBUG', '/MD']) env.Append(CCFLAGS=['/O2', '/EHsc', '/DNDEBUG', '/MD'])
else:
# MinGW elif host_platform == 'linux':
# Cross-compilation using MinGW
if env['bits'] == '64': if env['bits'] == '64':
env['CXX'] = 'x86_64-w64-mingw32-g++' env['CXX'] = 'x86_64-w64-mingw32-g++'
elif env['bits'] == '32': elif env['bits'] == '32':
env['CXX'] = 'i686-w64-mingw32-g++' env['CXX'] = 'i686-w64-mingw32-g++'
# Native or cross-compilation using MinGW
if host_platform == 'linux' or env['use_mingw']:
env.Append(CCFLAGS=['-g', '-O3', '-std=c++14', '-Wwrite-strings']) env.Append(CCFLAGS=['-g', '-O3', '-std=c++14', '-Wwrite-strings'])
env.Append(LINKFLAGS=['--static', '-Wl,--no-undefined', '-static-libgcc', '-static-libstdc++']) env.Append(LINKFLAGS=[
'--static',
'-Wl,--no-undefined',
'-static-libgcc',
'-static-libstdc++',
])
env.Append(CPPPATH=[
env.Append(CPPPATH=['.', env['headers_dir'], 'include', 'include/gen', 'include/core']) '.',
env['headers_dir'],
'include',
'include/gen',
'include/core',
])
# Generate bindings? # Generate bindings?
json_api_file = '' json_api_file = ''
if env['use_custom_api_file']: if 'custom_api_file' in env:
json_api_file = env['custom_api_file'] json_api_file = env['custom_api_file']
else: else:
json_api_file = os.path.join(os.getcwd(), 'godot_headers', 'api.json') json_api_file = os.path.join(os.getcwd(), 'godot_headers', 'api.json')
if env['generate_bindings']: if env['generate_bindings']:
# Actually create the bindings here # Actually create the bindings here
import binding_generator import binding_generator
binding_generator.generate_bindings(json_api_file) binding_generator.generate_bindings(json_api_file)
# source to compile # Sources to compile
sources = [] sources = []
add_sources(sources, 'src/core', 'cpp') add_sources(sources, 'src/core', 'cpp')
add_sources(sources, 'src/gen', 'cpp') add_sources(sources, 'src/gen', 'cpp')
library = env.StaticLibrary( library = env.StaticLibrary(
target='bin/' + 'libgodot-cpp.{}.{}.{}'.format(env['platform'], env['target'], env['bits']), source=sources target='bin/' + 'libgodot-cpp.{}.{}.{}'.format(
env['platform'],
env['target'],
env['bits'],
), source=sources
) )
Default(library) Default(library)

View File

@ -1,4 +1,4 @@
#!python #!/usr/bin/env python
import json import json
@ -7,30 +7,30 @@ import json
classes = [] classes = []
def generate_bindings(path): def generate_bindings(path):
global classes global classes
classes = json.load(open(path)) classes = json.load(open(path))
icalls = set() icalls = set()
for c in classes: for c in classes:
# print c['name'] # print c['name']
used_classes = get_used_classes(c) used_classes = get_used_classes(c)
header = generate_class_header(used_classes, c) header = generate_class_header(used_classes, c)
impl = generate_class_implementation(icalls, used_classes, c) impl = generate_class_implementation(icalls, used_classes, c)
header_file = open("include/gen/" + strip_name(c["name"]) + ".hpp", "w+") header_file = open("include/gen/" + strip_name(c["name"]) + ".hpp", "w+")
header_file.write(header) header_file.write(header)
source_file = open("src/gen/" + strip_name(c["name"]) + ".cpp", "w+") source_file = open("src/gen/" + strip_name(c["name"]) + ".cpp", "w+")
source_file.write(impl) source_file.write(impl)
icall_header_file = open("src/gen/__icalls.hpp", "w+") icall_header_file = open("src/gen/__icalls.hpp", "w+")
icall_header_file.write(generate_icall_header(icalls)) icall_header_file.write(generate_icall_header(icalls))
icall_source_file = open("src/gen/__icalls.cpp", "w+") icall_source_file = open("src/gen/__icalls.cpp", "w+")
icall_source_file.write(generate_icall_implementation(icalls)) icall_source_file.write(generate_icall_implementation(icalls))
@ -68,12 +68,12 @@ def generate_class_header(used_classes, c):
source.append("#define GODOT_CPP_" + strip_name(c["name"]).upper() + "_HPP") source.append("#define GODOT_CPP_" + strip_name(c["name"]).upper() + "_HPP")
source.append("") source.append("")
source.append("") source.append("")
source.append("#include <gdnative_api_struct.gen.h>") source.append("#include <gdnative_api_struct.gen.h>")
source.append("#include <stdint.h>") source.append("#include <stdint.h>")
source.append("") source.append("")
source.append("#include <core/CoreTypes.hpp>") source.append("#include <core/CoreTypes.hpp>")
class_name = strip_name(c["name"]) class_name = strip_name(c["name"])
@ -101,11 +101,11 @@ def generate_class_header(used_classes, c):
source.append("#include \"" + used_class_name + ".hpp\"") source.append("#include \"" + used_class_name + ".hpp\"")
source.append("") source.append("")
if c["base_class"] != "": if c["base_class"] != "":
source.append("#include \"" + strip_name(c["base_class"]) + ".hpp\"") source.append("#include \"" + strip_name(c["base_class"]) + ".hpp\"")
source.append("namespace godot {") source.append("namespace godot {")
source.append("") source.append("")
@ -115,12 +115,12 @@ def generate_class_header(used_classes, c):
continue continue
else: else:
source.append("class " + strip_name(used_type) + ";") source.append("class " + strip_name(used_type) + ";")
source.append("") source.append("")
vararg_templates = "" vararg_templates = ""
# generate the class definition here # generate the class definition here
source.append("class " + class_name + (" : public _Wrapped" if c["base_class"] == "" else (" : public " + strip_name(c["base_class"])) ) + " {") source.append("class " + class_name + (" : public _Wrapped" if c["base_class"] == "" else (" : public " + strip_name(c["base_class"])) ) + " {")
@ -168,17 +168,17 @@ def generate_class_header(used_classes, c):
source.append("\t};") source.append("\t};")
source.append("\n\t// constants") source.append("\n\t// constants")
for name in c["constants"]: for name in c["constants"]:
if name not in enum_values: if name not in enum_values:
source.append("\tconst static int " + name + " = " + str(c["constants"][name]) + ";") source.append("\tconst static int " + name + " = " + str(c["constants"][name]) + ";")
if c["instanciable"]: if c["instanciable"]:
source.append("") source.append("")
source.append("") source.append("")
source.append("\tstatic " + class_name + " *_new();") source.append("\tstatic " + class_name + " *_new();")
source.append("\n\t// methods") source.append("\n\t// methods")
@ -188,28 +188,28 @@ def generate_class_header(used_classes, c):
source.append("\tstatic T *cast_to(const Object *obj);") source.append("\tstatic T *cast_to(const Object *obj);")
source.append("#endif") source.append("#endif")
source.append("") source.append("")
for method in c["methods"]: for method in c["methods"]:
method_signature = "" method_signature = ""
# TODO decide what to do about virtual methods # TODO decide what to do about virtual methods
# method_signature += "virtual " if method["is_virtual"] else "" # method_signature += "virtual " if method["is_virtual"] else ""
method_signature += make_gdnative_type(method["return_type"]) method_signature += make_gdnative_type(method["return_type"])
method_name = escape_cpp(method["name"]) method_name = escape_cpp(method["name"])
method_signature += method_name + "(" method_signature += method_name + "("
has_default_argument = False has_default_argument = False
method_arguments = "" method_arguments = ""
for i, argument in enumerate(method["arguments"]): for i, argument in enumerate(method["arguments"]):
method_signature += "const " + make_gdnative_type(argument["type"]) method_signature += "const " + make_gdnative_type(argument["type"])
argument_name = escape_cpp(argument["name"]) argument_name = escape_cpp(argument["name"])
method_signature += argument_name method_signature += argument_name
method_arguments += argument_name method_arguments += argument_name
# default arguments # default arguments
def escape_default_arg(_type, default_value): def escape_default_arg(_type, default_value):
if _type == "Color": if _type == "Color":
@ -236,51 +236,51 @@ def generate_class_header(used_classes, c):
return "\"" + default_value + "\"" return "\"" + default_value + "\""
if _type == "RID": if _type == "RID":
return "RID()" return "RID()"
if default_value == "Null" or default_value == "[Object:null]": if default_value == "Null" or default_value == "[Object:null]":
return "nullptr" return "nullptr"
return default_value return default_value
if argument["has_default_value"] or has_default_argument: if argument["has_default_value"] or has_default_argument:
method_signature += " = " + escape_default_arg(argument["type"], argument["default_value"]) method_signature += " = " + escape_default_arg(argument["type"], argument["default_value"])
has_default_argument = True has_default_argument = True
if i != len(method["arguments"]) - 1: if i != len(method["arguments"]) - 1:
method_signature += ", " method_signature += ", "
method_arguments += "," method_arguments += ","
if method["has_varargs"]: if method["has_varargs"]:
if len(method["arguments"]) > 0: if len(method["arguments"]) > 0:
method_signature += ", " method_signature += ", "
method_arguments += ", " method_arguments += ", "
vararg_templates += "\ttemplate <class... Args> " + method_signature + "Args... args){\n\t\treturn " + method_name + "(" + method_arguments + "Array::make(args...));\n\t}\n""" vararg_templates += "\ttemplate <class... Args> " + method_signature + "Args... args){\n\t\treturn " + method_name + "(" + method_arguments + "Array::make(args...));\n\t}\n"""
method_signature += "const Array& __var_args = Array()" method_signature += "const Array& __var_args = Array()"
method_signature += ")" + (" const" if method["is_const"] else "") method_signature += ")" + (" const" if method["is_const"] else "")
source.append("\t" + method_signature + ";") source.append("\t" + method_signature + ";")
source.append(vararg_templates) source.append(vararg_templates)
source.append("};") source.append("};")
source.append("") source.append("")
source.append("}") source.append("}")
source.append("") source.append("")
source.append("#endif") source.append("#endif")
return "\n".join(source) return "\n".join(source)
@ -291,34 +291,34 @@ def generate_class_implementation(icalls, used_classes, c):
source.append("#include \"" + class_name + ".hpp\"") source.append("#include \"" + class_name + ".hpp\"")
source.append("") source.append("")
source.append("") source.append("")
source.append("#include <core/GodotGlobal.hpp>") source.append("#include <core/GodotGlobal.hpp>")
source.append("#include <core/CoreTypes.hpp>") source.append("#include <core/CoreTypes.hpp>")
source.append("#include <core/Ref.hpp>") source.append("#include <core/Ref.hpp>")
source.append("#include <core/Godot.hpp>") source.append("#include <core/Godot.hpp>")
source.append("") source.append("")
source.append("#include \"__icalls.hpp\"") source.append("#include \"__icalls.hpp\"")
source.append("") source.append("")
source.append("") source.append("")
for used_class in used_classes: for used_class in used_classes:
if is_enum(used_class): if is_enum(used_class):
continue continue
else: else:
source.append("#include \"" + strip_name(used_class) + ".hpp\"") source.append("#include \"" + strip_name(used_class) + ".hpp\"")
source.append("") source.append("")
source.append("") source.append("")
source.append("namespace godot {") source.append("namespace godot {")
core_object_name = "this" core_object_name = "this"
source.append("") source.append("")
source.append("") source.append("")
@ -326,44 +326,44 @@ def generate_class_implementation(icalls, used_classes, c):
source.append("" + class_name + " *" + class_name + "::_singleton = NULL;") source.append("" + class_name + " *" + class_name + "::_singleton = NULL;")
source.append("") source.append("")
source.append("") source.append("")
# FIXME Test if inlining has a huge impact on binary size # FIXME Test if inlining has a huge impact on binary size
source.append(class_name + "::" + class_name + "() {") source.append(class_name + "::" + class_name + "() {")
source.append("\t_owner = godot::api->godot_global_get_singleton((char *) \"" + strip_name(c["name"]) + "\");") source.append("\t_owner = godot::api->godot_global_get_singleton((char *) \"" + strip_name(c["name"]) + "\");")
source.append("}") source.append("}")
source.append("") source.append("")
source.append("") source.append("")
if c["instanciable"]: if c["instanciable"]:
source.append(class_name + " *" + strip_name(c["name"]) + "::_new()") source.append(class_name + " *" + strip_name(c["name"]) + "::_new()")
source.append("{") source.append("{")
source.append("\treturn (" + class_name + " *) godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, godot::api->godot_get_class_constructor((char *)\"" + c["name"] + "\")());") source.append("\treturn (" + class_name + " *) godot::nativescript_1_1_api->godot_nativescript_get_instance_binding_data(godot::_RegisterState::language_index, godot::api->godot_get_class_constructor((char *)\"" + c["name"] + "\")());")
source.append("}") source.append("}")
for method in c["methods"]: for method in c["methods"]:
method_signature = "" method_signature = ""
method_signature += make_gdnative_type(method["return_type"]) method_signature += make_gdnative_type(method["return_type"])
method_signature += strip_name(c["name"]) + "::" + escape_cpp(method["name"]) + "(" method_signature += strip_name(c["name"]) + "::" + escape_cpp(method["name"]) + "("
for i, argument in enumerate(method["arguments"]): for i, argument in enumerate(method["arguments"]):
method_signature += "const " + make_gdnative_type(argument["type"]) method_signature += "const " + make_gdnative_type(argument["type"])
method_signature += escape_cpp(argument["name"]) method_signature += escape_cpp(argument["name"])
if i != len(method["arguments"]) - 1: if i != len(method["arguments"]) - 1:
method_signature += ", " method_signature += ", "
if method["has_varargs"]: if method["has_varargs"]:
if len(method["arguments"]) > 0: if len(method["arguments"]) > 0:
method_signature += ", " method_signature += ", "
method_signature += "const Array& __var_args" method_signature += "const Array& __var_args"
method_signature += ")" + (" const" if method["is_const"] else "") method_signature += ")" + (" const" if method["is_const"] else "")
source.append(method_signature + " {") source.append(method_signature + " {")
@ -374,14 +374,14 @@ def generate_class_implementation(icalls, used_classes, c):
source.append("") source.append("")
continue continue
else: else:
source.append("\tstatic godot_method_bind *mb = nullptr;") source.append("\tstatic godot_method_bind *mb = nullptr;")
source.append("\tif (mb == nullptr) {") source.append("\tif (mb == nullptr) {")
source.append("\t\tmb = godot::api->godot_method_bind_get_method(\"" + c["name"] +"\", \"" + method["name"] + "\");") source.append("\t\tmb = godot::api->godot_method_bind_get_method(\"" + c["name"] +"\", \"" + method["name"] + "\");")
source.append("\t}") source.append("\t}")
return_statement = "" return_statement = ""
if method["return_type"] != "void": if method["return_type"] != "void":
if is_class_type(method["return_type"]): if is_class_type(method["return_type"]):
if is_enum(method["return_type"]): if is_enum(method["return_type"]):
@ -392,72 +392,72 @@ def generate_class_implementation(icalls, used_classes, c):
return_statement += "return " + ("(" + strip_name(method["return_type"]) + " *) " if is_class_type(method["return_type"]) else "") return_statement += "return " + ("(" + strip_name(method["return_type"]) + " *) " if is_class_type(method["return_type"]) else "")
else: else:
return_statement += "return " return_statement += "return "
def get_icall_type_name(name): def get_icall_type_name(name):
if is_enum(name): if is_enum(name):
return "int" return "int"
if is_class_type(name): if is_class_type(name):
return "Object" return "Object"
return name return name
if method["has_varargs"]: if method["has_varargs"]:
if len(method["arguments"]) != 0: if len(method["arguments"]) != 0:
source.append("\tVariant __given_args[" + str(len(method["arguments"])) + "];") source.append("\tVariant __given_args[" + str(len(method["arguments"])) + "];")
for i, argument in enumerate(method["arguments"]): for i, argument in enumerate(method["arguments"]):
source.append("\tgodot::api->godot_variant_new_nil((godot_variant *) &__given_args[" + str(i) + "]);") source.append("\tgodot::api->godot_variant_new_nil((godot_variant *) &__given_args[" + str(i) + "]);")
source.append("") source.append("")
for i, argument in enumerate(method["arguments"]): for i, argument in enumerate(method["arguments"]):
source.append("\t__given_args[" + str(i) + "] = " + escape_cpp(argument["name"]) + ";") source.append("\t__given_args[" + str(i) + "] = " + escape_cpp(argument["name"]) + ";")
source.append("") source.append("")
size = "" size = ""
if method["has_varargs"]: if method["has_varargs"]:
size = "(__var_args.size() + " + str(len(method["arguments"])) + ")" size = "(__var_args.size() + " + str(len(method["arguments"])) + ")"
else: else:
size = "(" + str(len(method["arguments"])) + ")" size = "(" + str(len(method["arguments"])) + ")"
source.append("\tgodot_variant **__args = (godot_variant **) alloca(sizeof(godot_variant *) * " + size + ");") source.append("\tgodot_variant **__args = (godot_variant **) alloca(sizeof(godot_variant *) * " + size + ");")
source.append("") source.append("")
for i, argument in enumerate(method["arguments"]): for i, argument in enumerate(method["arguments"]):
source.append("\t__args[" + str(i) + "] = (godot_variant *) &__given_args[" + str(i) + "];") source.append("\t__args[" + str(i) + "] = (godot_variant *) &__given_args[" + str(i) + "];")
source.append("") source.append("")
if method["has_varargs"]: if method["has_varargs"]:
source.append("\tfor (int i = 0; i < __var_args.size(); i++) {") source.append("\tfor (int i = 0; i < __var_args.size(); i++) {")
source.append("\t\t__args[i + " + str(len(method["arguments"])) + "] = (godot_variant *) &((Array &) __var_args)[i];") source.append("\t\t__args[i + " + str(len(method["arguments"])) + "] = (godot_variant *) &((Array &) __var_args)[i];")
source.append("\t}") source.append("\t}")
source.append("") source.append("")
source.append("\tVariant __result;") source.append("\tVariant __result;")
source.append("\t*(godot_variant *) &__result = godot::api->godot_method_bind_call(mb, ((const Object *) " + core_object_name + ")->_owner, (const godot_variant **) __args, " + size + ", nullptr);") source.append("\t*(godot_variant *) &__result = godot::api->godot_method_bind_call(mb, ((const Object *) " + core_object_name + ")->_owner, (const godot_variant **) __args, " + size + ", nullptr);")
source.append("") source.append("")
if is_class_type(method["return_type"]): if is_class_type(method["return_type"]):
source.append("\tObject *obj = Object::___get_from_variant(__result);") source.append("\tObject *obj = Object::___get_from_variant(__result);")
source.append("\tif (obj->has_method(\"reference\"))") source.append("\tif (obj->has_method(\"reference\"))")
source.append("\t\tobj->callv(\"reference\", Array());") source.append("\t\tobj->callv(\"reference\", Array());")
source.append("") source.append("")
for i, argument in enumerate(method["arguments"]): for i, argument in enumerate(method["arguments"]):
source.append("\tgodot::api->godot_variant_destroy((godot_variant *) &__given_args[" + str(i) + "]);") source.append("\tgodot::api->godot_variant_destroy((godot_variant *) &__given_args[" + str(i) + "]);")
source.append("") source.append("")
if method["return_type"] != "void": if method["return_type"] != "void":
cast = "" cast = ""
if is_class_type(method["return_type"]): if is_class_type(method["return_type"]):
@ -468,40 +468,40 @@ def generate_class_implementation(icalls, used_classes, c):
else: else:
cast += "__result;" cast += "__result;"
source.append("\treturn " + cast) source.append("\treturn " + cast)
else: else:
args = [] args = []
for arg in method["arguments"]: for arg in method["arguments"]:
args.append(get_icall_type_name(arg["type"])) args.append(get_icall_type_name(arg["type"]))
icall_ret_type = get_icall_type_name(method["return_type"]) icall_ret_type = get_icall_type_name(method["return_type"])
icall_sig = tuple((icall_ret_type, tuple(args))) icall_sig = tuple((icall_ret_type, tuple(args)))
icalls.add(icall_sig) icalls.add(icall_sig)
icall_name = get_icall_name(icall_sig) icall_name = get_icall_name(icall_sig)
return_statement += icall_name + "(mb, (const Object *) " + core_object_name return_statement += icall_name + "(mb, (const Object *) " + core_object_name
for arg in method["arguments"]: for arg in method["arguments"]:
return_statement += ", " + escape_cpp(arg["name"]) + (".ptr()" if is_reference_type(arg["type"]) else "") return_statement += ", " + escape_cpp(arg["name"]) + (".ptr()" if is_reference_type(arg["type"]) else "")
return_statement += ")" return_statement += ")"
source.append("\t" + return_statement + (")" if is_reference_type(method["return_type"]) else "") + ";") source.append("\t" + return_statement + (")" if is_reference_type(method["return_type"]) else "") + ";")
source.append("}") source.append("}")
source.append("") source.append("")
source.append("}") source.append("}")
return "\n".join(source) return "\n".join(source)
@ -509,36 +509,36 @@ def generate_class_implementation(icalls, used_classes, c):
def generate_icall_header(icalls): def generate_icall_header(icalls):
source = [] source = []
source.append("#ifndef GODOT_CPP__ICALLS_HPP") source.append("#ifndef GODOT_CPP__ICALLS_HPP")
source.append("#define GODOT_CPP__ICALLS_HPP") source.append("#define GODOT_CPP__ICALLS_HPP")
source.append("") source.append("")
source.append("#include <gdnative_api_struct.gen.h>") source.append("#include <gdnative_api_struct.gen.h>")
source.append("#include <stdint.h>") source.append("#include <stdint.h>")
source.append("") source.append("")
source.append("#include <core/CoreTypes.hpp>") source.append("#include <core/CoreTypes.hpp>")
source.append("#include \"Object.hpp\"") source.append("#include \"Object.hpp\"")
source.append("") source.append("")
source.append("") source.append("")
source.append("namespace godot {") source.append("namespace godot {")
source.append("") source.append("")
for icall in icalls: for icall in icalls:
ret_type = icall[0] ret_type = icall[0]
args = icall[1] args = icall[1]
method_signature = "" method_signature = ""
method_signature += return_type(ret_type) + get_icall_name(icall) + "(godot_method_bind *mb, const Object *inst" method_signature += return_type(ret_type) + get_icall_name(icall) + "(godot_method_bind *mb, const Object *inst"
for arg in args: for arg in args:
method_signature += ", const " method_signature += ", const "
if is_core_type(arg): if is_core_type(arg):
method_signature += arg + "&" method_signature += arg + "&"
elif arg == "int": elif arg == "int":
@ -549,51 +549,51 @@ def generate_icall_header(icalls):
method_signature += arg method_signature += arg
else: else:
method_signature += "Object *" method_signature += "Object *"
method_signature += ");" method_signature += ");"
source.append(method_signature) source.append(method_signature)
source.append("") source.append("")
source.append("}") source.append("}")
source.append("") source.append("")
source.append("#endif") source.append("#endif")
return "\n".join(source) return "\n".join(source)
def generate_icall_implementation(icalls): def generate_icall_implementation(icalls):
source = [] source = []
source.append("#include \"__icalls.hpp\"") source.append("#include \"__icalls.hpp\"")
source.append("") source.append("")
source.append("#include <gdnative_api_struct.gen.h>") source.append("#include <gdnative_api_struct.gen.h>")
source.append("#include <stdint.h>") source.append("#include <stdint.h>")
source.append("") source.append("")
source.append("#include <core/GodotGlobal.hpp>") source.append("#include <core/GodotGlobal.hpp>")
source.append("#include <core/CoreTypes.hpp>") source.append("#include <core/CoreTypes.hpp>")
source.append("#include \"Object.hpp\"") source.append("#include \"Object.hpp\"")
source.append("") source.append("")
source.append("") source.append("")
source.append("namespace godot {") source.append("namespace godot {")
source.append("") source.append("")
for icall in icalls: for icall in icalls:
ret_type = icall[0] ret_type = icall[0]
args = icall[1] args = icall[1]
method_signature = "" method_signature = ""
method_signature += return_type(ret_type) + get_icall_name(icall) + "(godot_method_bind *mb, const Object *inst" method_signature += return_type(ret_type) + get_icall_name(icall) + "(godot_method_bind *mb, const Object *inst"
for i, arg in enumerate(args): for i, arg in enumerate(args):
method_signature += ", const " method_signature += ", const "
if is_core_type(arg): if is_core_type(arg):
method_signature += arg + "& " method_signature += arg + "& "
elif arg == "int": elif arg == "int":
@ -604,37 +604,37 @@ def generate_icall_implementation(icalls):
method_signature += arg + " " method_signature += arg + " "
else: else:
method_signature += "Object *" method_signature += "Object *"
method_signature += "arg" + str(i) method_signature += "arg" + str(i)
method_signature += ")" method_signature += ")"
source.append(method_signature + " {") source.append(method_signature + " {")
if ret_type != "void": if ret_type != "void":
source.append("\t" + ("godot_object *" if is_class_type(ret_type) else return_type(ret_type)) + "ret;") source.append("\t" + ("godot_object *" if is_class_type(ret_type) else return_type(ret_type)) + "ret;")
if is_class_type(ret_type): if is_class_type(ret_type):
source.append("\tret = nullptr;") source.append("\tret = nullptr;")
source.append("\tconst void *args[" + ("1" if len(args) == 0 else "") + "] = {") source.append("\tconst void *args[" + ("1" if len(args) == 0 else "") + "] = {")
for i, arg in enumerate(args): for i, arg in enumerate(args):
wrapped_argument = "\t\t" wrapped_argument = "\t\t"
if is_primitive(arg) or is_core_type(arg): if is_primitive(arg) or is_core_type(arg):
wrapped_argument += "(void *) &arg" + str(i) wrapped_argument += "(void *) &arg" + str(i)
else: else:
wrapped_argument += "(void *) (arg" + str(i) + ") ? arg" + str(i) + "->_owner : nullptr" wrapped_argument += "(void *) (arg" + str(i) + ") ? arg" + str(i) + "->_owner : nullptr"
wrapped_argument += "," wrapped_argument += ","
source.append(wrapped_argument) source.append(wrapped_argument)
source.append("\t};") source.append("\t};")
source.append("") source.append("")
source.append("\tgodot::api->godot_method_bind_ptrcall(mb, inst->_owner, args, " + ("nullptr" if ret_type == "void" else "&ret") + ");") source.append("\tgodot::api->godot_method_bind_ptrcall(mb, inst->_owner, args, " + ("nullptr" if ret_type == "void" else "&ret") + ");")
if ret_type != "void": if ret_type != "void":
if is_class_type(ret_type): if is_class_type(ret_type):
source.append("\tif (ret) {") source.append("\tif (ret) {")
@ -644,12 +644,12 @@ def generate_icall_implementation(icalls):
source.append("\treturn (Object *) ret;") source.append("\treturn (Object *) ret;")
else: else:
source.append("\treturn ret;") source.append("\treturn ret;")
source.append("}") source.append("}")
source.append("}") source.append("}")
source.append("") source.append("")
return "\n".join(source) return "\n".join(source)
@ -657,7 +657,7 @@ def generate_icall_implementation(icalls):
def generate_type_registry(classes): def generate_type_registry(classes):
source = [] source = []
source.append("#include \"TagDB.hpp\"") source.append("#include \"TagDB.hpp\"")
source.append("#include <typeinfo>") source.append("#include <typeinfo>")
source.append("\n") source.append("\n")
@ -719,12 +719,12 @@ def return_type(t):
def get_icall_name(sig): def get_icall_name(sig):
ret_type = sig[0] ret_type = sig[0]
args = sig[1] args = sig[1]
name = "___godot_icall_" name = "___godot_icall_"
name += strip_name(ret_type) name += strip_name(ret_type)
for arg in args: for arg in args:
name += "_" + strip_name(arg) name += "_" + strip_name(arg)
return name return name
@ -737,7 +737,7 @@ def get_used_classes(c):
for method in c["methods"]: for method in c["methods"]:
if is_class_type(method["return_type"]) and not (method["return_type"] in classes): if is_class_type(method["return_type"]) and not (method["return_type"] in classes):
classes.append(method["return_type"]) classes.append(method["return_type"])
for arg in method["arguments"]: for arg in method["arguments"]:
if is_class_type(arg["type"]) and not (arg["type"] in classes): if is_class_type(arg["type"]) and not (arg["type"] in classes):
classes.append(arg["type"]) classes.append(arg["type"])