1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

SCons: Make builders prettier, utilize constexpr

This commit is contained in:
Thaddeus Crews
2024-10-29 14:23:08 -05:00
parent 7893202fba
commit be429eb404
21 changed files with 510 additions and 723 deletions

View File

@@ -1,7 +1,8 @@
"""Functions used to generate source files during build time"""
import os
from io import StringIO
import methods
def parse_template(inherits, source, delimiter):
@@ -36,54 +37,36 @@ def parse_template(inherits, source, delimiter):
script_template["script"].replace('"', '\\"').lstrip().replace("\n", "\\n").replace("\t", "_TS_")
)
return (
'{ String("'
+ script_template["inherits"]
+ '"), String("'
+ script_template["name"]
+ '"), String("'
+ script_template["description"]
+ '"), String("'
+ script_template["script"]
+ '")'
+ " },\n"
f'{{ String("{script_template["inherits"]}"), '
+ f'String("{script_template["name"]}"), '
+ f'String("{script_template["description"]}"), '
+ f'String("{script_template["script"]}") }},'
)
def make_templates(target, source, env):
dst = str(target[0])
with StringIO() as s:
s.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n\n")
s.write("#ifndef _CODE_TEMPLATES_H\n")
s.write("#define _CODE_TEMPLATES_H\n\n")
s.write('#include "core/object/object.h"\n')
s.write('#include "core/object/script_language.h"\n')
delimiter = "#" # GDScript single line comment delimiter by default.
if source:
ext = os.path.splitext(str(source[0]))[1]
if ext == ".cs":
delimiter = "//"
delimiter = "#" # GDScript single line comment delimiter by default.
if source:
ext = os.path.splitext(str(source[0]))[1]
if ext == ".cs":
delimiter = "//"
parsed_templates = []
parsed_template_string = ""
number_of_templates = 0
for filepath in source:
filepath = str(filepath)
node_name = os.path.basename(os.path.dirname(filepath))
parsed_templates.append(parse_template(node_name, filepath, delimiter))
for filepath in source:
filepath = str(filepath)
node_name = os.path.basename(os.path.dirname(filepath))
parsed_template = parse_template(node_name, filepath, delimiter)
parsed_template_string += "\t" + parsed_template
number_of_templates += 1
parsed_template_string = "\n\t".join(parsed_templates)
s.write("\nstatic const int TEMPLATES_ARRAY_SIZE = " + str(number_of_templates) + ";\n")
s.write(
"\nstatic const struct ScriptLanguage::ScriptTemplate TEMPLATES[" + str(number_of_templates) + "] = {\n"
)
with methods.generated_wrapper(str(target[0])) as file:
file.write(f"""\
#include "core/object/object.h"
#include "core/object/script_language.h"
s.write(parsed_template_string)
s.write("};\n")
s.write("\n#endif\n")
with open(dst, "w", encoding="utf-8", newline="\n") as f:
f.write(s.getvalue())
inline constexpr int TEMPLATES_ARRAY_SIZE = {len(parsed_templates)};
static const struct ScriptLanguage::ScriptTemplate TEMPLATES[TEMPLATES_ARRAY_SIZE] = {{
{parsed_template_string}
}};
""")