2018-03-17 23:23:55 +01:00
|
|
|
"""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):
|
2020-03-30 08:28:32 +02:00
|
|
|
return e if e[-1] == "*" else e + " "
|
2018-03-17 23:23:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _build_gdnative_api_struct_header(api):
|
|
|
|
out = [
|
2020-03-30 08:28:32 +02:00
|
|
|
"/* 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>",
|
2020-04-08 16:47:36 +02:00
|
|
|
"#include <xr/godot_xr.h>",
|
2020-03-30 08:28:32 +02:00
|
|
|
"#include <nativescript/godot_nativescript.h>",
|
|
|
|
"#include <net/godot_net.h>",
|
|
|
|
"#include <pluginscript/godot_pluginscript.h>",
|
|
|
|
"#include <videodecoder/godot_videodecoder.h>",
|
|
|
|
"",
|
|
|
|
"#ifdef __cplusplus",
|
2018-03-17 23:23:55 +01:00
|
|
|
'extern "C" {',
|
2020-03-30 08:28:32 +02:00
|
|
|
"#endif",
|
|
|
|
"",
|
|
|
|
"enum GDNATIVE_API_TYPES {",
|
|
|
|
"\tGDNATIVE_" + api["core"]["type"] + ",",
|
2018-03-17 23:23:55 +01:00
|
|
|
]
|
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
for ext in api["extensions"]:
|
|
|
|
out += ["\tGDNATIVE_EXT_" + ext["type"] + ","]
|
2018-03-17 23:23:55 +01:00
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
out += ["};", ""]
|
2018-03-17 23:23:55 +01:00
|
|
|
|
|
|
|
def generate_extension_struct(name, ext, include_version=True):
|
|
|
|
ret_val = []
|
2020-03-30 08:28:32 +02:00
|
|
|
if ext["next"]:
|
|
|
|
ret_val += generate_extension_struct(name, ext["next"])
|
2018-03-17 23:23:55 +01:00
|
|
|
|
|
|
|
ret_val += [
|
2020-03-30 08:28:32 +02:00
|
|
|
"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;",
|
2018-03-17 23:23:55 +01:00
|
|
|
]
|
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
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))
|
2018-03-17 23:23:55 +01:00
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
ret_val += [
|
|
|
|
"} godot_gdnative_ext_"
|
|
|
|
+ name
|
|
|
|
+ ("" if not include_version else ("_{0}_{1}".format(ext["version"]["major"], ext["version"]["minor"])))
|
|
|
|
+ "_api_struct;",
|
|
|
|
"",
|
|
|
|
]
|
2018-03-17 23:23:55 +01:00
|
|
|
|
|
|
|
return ret_val
|
|
|
|
|
2018-08-30 19:18:55 +02:00
|
|
|
def generate_core_extension_struct(core):
|
|
|
|
ret_val = []
|
2020-03-30 08:28:32 +02:00
|
|
|
if core["next"]:
|
|
|
|
ret_val += generate_core_extension_struct(core["next"])
|
2018-11-20 11:14:07 +01:00
|
|
|
|
2018-08-30 19:18:55 +02:00
|
|
|
ret_val += [
|
2020-03-30 08:28:32 +02:00
|
|
|
"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;",
|
2018-08-30 19:18:55 +02:00
|
|
|
]
|
2018-11-20 11:14:07 +01:00
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
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))
|
2018-11-20 11:14:07 +01:00
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
ret_val += [
|
|
|
|
"} godot_gdnative_core_"
|
|
|
|
+ "{0}_{1}".format(core["version"]["major"], core["version"]["minor"])
|
|
|
|
+ "_api_struct;",
|
|
|
|
"",
|
|
|
|
]
|
2018-11-20 11:14:07 +01:00
|
|
|
|
2018-08-30 19:18:55 +02:00
|
|
|
return ret_val
|
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
for ext in api["extensions"]:
|
|
|
|
name = ext["name"]
|
2018-03-17 23:23:55 +01:00
|
|
|
out += generate_extension_struct(name, ext, False)
|
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
if api["core"]["next"]:
|
|
|
|
out += generate_core_extension_struct(api["core"]["next"])
|
2018-08-30 19:18:55 +02:00
|
|
|
|
2018-03-17 23:23:55 +01:00
|
|
|
out += [
|
2020-03-30 08:28:32 +02:00
|
|
|
"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;",
|
2018-03-17 23:23:55 +01:00
|
|
|
]
|
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
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))
|
2018-03-17 23:23:55 +01:00
|
|
|
|
|
|
|
out += [
|
2020-03-30 08:28:32 +02:00
|
|
|
"} godot_gdnative_core_api_struct;",
|
|
|
|
"",
|
|
|
|
"#ifdef __cplusplus",
|
|
|
|
"}",
|
|
|
|
"#endif",
|
|
|
|
"",
|
|
|
|
"#endif // GODOT_GDNATIVE_API_STRUCT_H",
|
|
|
|
"",
|
2018-03-17 23:23:55 +01:00
|
|
|
]
|
2020-03-30 08:28:32 +02:00
|
|
|
return "\n".join(out)
|
2018-03-17 23:23:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _build_gdnative_api_struct_source(api):
|
2020-03-30 08:28:32 +02:00
|
|
|
out = ["/* THIS FILE IS GENERATED DO NOT EDIT */", "", "#include <gdnative_api_struct.gen.h>", ""]
|
2018-03-17 23:23:55 +01:00
|
|
|
|
|
|
|
def get_extension_struct_name(name, ext, include_version=True):
|
2020-03-30 08:28:32 +02:00
|
|
|
return (
|
|
|
|
"godot_gdnative_ext_"
|
|
|
|
+ name
|
|
|
|
+ ("" if not include_version else ("_{0}_{1}".format(ext["version"]["major"], ext["version"]["minor"])))
|
|
|
|
+ "_api_struct"
|
|
|
|
)
|
2018-03-17 23:23:55 +01:00
|
|
|
|
|
|
|
def get_extension_struct_instance_name(name, ext, include_version=True):
|
2020-03-30 08:28:32 +02:00
|
|
|
return (
|
|
|
|
"api_extension_"
|
|
|
|
+ name
|
|
|
|
+ ("" if not include_version else ("_{0}_{1}".format(ext["version"]["major"], ext["version"]["minor"])))
|
|
|
|
+ "_struct"
|
|
|
|
)
|
2018-03-17 23:23:55 +01:00
|
|
|
|
|
|
|
def get_extension_struct_definition(name, ext, include_version=True):
|
|
|
|
|
|
|
|
ret_val = []
|
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
if ext["next"]:
|
|
|
|
ret_val += get_extension_struct_definition(name, ext["next"])
|
2018-03-17 23:23:55 +01:00
|
|
|
|
|
|
|
ret_val += [
|
2020-03-30 08:28:32 +02:00
|
|
|
"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"
|
|
|
|
+ (
|
2020-04-02 14:41:58 +02:00
|
|
|
"nullptr"
|
2020-03-30 08:28:32 +02:00
|
|
|
if not ext["next"]
|
|
|
|
else ("(const godot_gdnative_api_struct *)&" + get_extension_struct_instance_name(name, ext["next"]))
|
|
|
|
)
|
|
|
|
+ ",",
|
2018-03-17 23:23:55 +01:00
|
|
|
]
|
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
for funcdef in ext["api"]:
|
|
|
|
ret_val.append("\t%s," % funcdef["name"])
|
2018-03-17 23:23:55 +01:00
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
ret_val += ["};\n"]
|
2018-03-17 23:23:55 +01:00
|
|
|
|
|
|
|
return ret_val
|
2018-11-20 11:14:07 +01:00
|
|
|
|
2018-08-30 19:18:55 +02:00
|
|
|
def get_core_struct_definition(core):
|
|
|
|
ret_val = []
|
2018-11-20 11:14:07 +01:00
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
if core["next"]:
|
|
|
|
ret_val += get_core_struct_definition(core["next"])
|
2018-11-20 11:14:07 +01:00
|
|
|
|
2018-08-30 19:18:55 +02:00
|
|
|
ret_val += [
|
2020-03-30 08:28:32 +02:00
|
|
|
"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"
|
|
|
|
+ (
|
2020-04-02 14:41:58 +02:00
|
|
|
"nullptr"
|
2020-03-30 08:28:32 +02:00
|
|
|
if not core["next"]
|
|
|
|
else (
|
|
|
|
"(const godot_gdnative_api_struct *)& api_{0}_{1}".format(
|
|
|
|
core["next"]["version"]["major"], core["next"]["version"]["minor"]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
+ ",",
|
2018-08-30 19:18:55 +02:00
|
|
|
]
|
2018-11-20 11:14:07 +01:00
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
for funcdef in core["api"]:
|
|
|
|
ret_val.append("\t%s," % funcdef["name"])
|
2018-11-20 11:14:07 +01:00
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
ret_val += ["};\n"]
|
2018-11-20 11:14:07 +01:00
|
|
|
|
2018-08-30 19:18:55 +02:00
|
|
|
return ret_val
|
2018-03-17 23:23:55 +01:00
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
for ext in api["extensions"]:
|
|
|
|
name = ext["name"]
|
2018-03-17 23:23:55 +01:00
|
|
|
out += get_extension_struct_definition(name, ext, False)
|
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
out += ["", "const godot_gdnative_api_struct *gdnative_extensions_pointers[] = {"]
|
2018-03-17 23:23:55 +01:00
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
for ext in api["extensions"]:
|
|
|
|
name = ext["name"]
|
|
|
|
out += ["\t(godot_gdnative_api_struct *)&api_extension_" + name + "_struct,"]
|
2018-03-17 23:23:55 +01:00
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
out += ["};\n"]
|
2018-11-20 11:14:07 +01:00
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
if api["core"]["next"]:
|
|
|
|
out += get_core_struct_definition(api["core"]["next"])
|
2018-03-17 23:23:55 +01:00
|
|
|
|
|
|
|
out += [
|
2020-03-30 08:28:32 +02:00
|
|
|
"extern const godot_gdnative_core_api_struct api_struct = {",
|
|
|
|
"\tGDNATIVE_" + api["core"]["type"] + ",",
|
|
|
|
"\t{" + str(api["core"]["version"]["major"]) + ", " + str(api["core"]["version"]["minor"]) + "},",
|
|
|
|
"\t(const godot_gdnative_api_struct *)&api_1_1,",
|
|
|
|
"\t" + str(len(api["extensions"])) + ",",
|
|
|
|
"\tgdnative_extensions_pointers,",
|
2018-03-17 23:23:55 +01:00
|
|
|
]
|
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
for funcdef in api["core"]["api"]:
|
|
|
|
out.append("\t%s," % funcdef["name"])
|
|
|
|
out.append("};\n")
|
2018-03-17 23:23:55 +01:00
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
return "\n".join(out)
|
2018-03-17 23:23:55 +01:00
|
|
|
|
|
|
|
|
|
|
|
def build_gdnative_api_struct(target, source, env):
|
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
with open(source[0], "r") as fd:
|
2018-03-17 23:23:55 +01:00
|
|
|
api = json.load(fd)
|
|
|
|
|
|
|
|
header, source = target
|
2020-03-30 08:28:32 +02:00
|
|
|
with open(header, "w") as fd:
|
2018-03-17 23:23:55 +01:00
|
|
|
fd.write(_build_gdnative_api_struct_header(api))
|
2020-03-30 08:28:32 +02:00
|
|
|
with open(source, "w") as fd:
|
2018-03-17 23:23:55 +01:00
|
|
|
fd.write(_build_gdnative_api_struct_source(api))
|
|
|
|
|
|
|
|
|
2020-03-30 08:28:32 +02:00
|
|
|
if __name__ == "__main__":
|
2018-03-17 23:23:55 +01:00
|
|
|
subprocess_main(globals())
|