SCons: unify code generations routine and minimize timestamp changes

Previously, all of the code generation routines would just needlessly
write the same files over and over, even when not needed.

This became a problem with the advent of the experimental ninja backend
for SCons, which can be trivially enabled with a few lines of code and
relies on timestamp changes, making it thus impractical.
This commit is contained in:
Riteo 2024-03-13 17:54:14 +01:00
parent 89f70e98d2
commit a5cf92664d
6 changed files with 96 additions and 68 deletions

View file

@ -228,14 +228,22 @@ def get_version_info(module_version_string="", silent=False):
return version_info
def write_file_if_needed(path, string):
try:
with open(path, "r", encoding="utf-8", newline="\n") as f:
if f.read() == string:
return
except FileNotFoundError:
pass
with open(path, "w", encoding="utf-8", newline="\n") as f:
f.write(string)
def generate_version_header(module_version_string=""):
version_info = get_version_info(module_version_string)
# NOTE: It is safe to generate these files here, since this is still executed serially.
with open("core/version_generated.gen.h", "w", encoding="utf-8", newline="\n") as f:
f.write(
"""\
version_info_header = """\
/* THIS FILE IS GENERATED DO NOT EDIT */
#ifndef VERSION_GENERATED_GEN_H
#define VERSION_GENERATED_GEN_H
@ -252,21 +260,20 @@ def generate_version_header(module_version_string=""):
#define VERSION_DOCS_URL "https://docs.godotengine.org/en/" VERSION_DOCS_BRANCH
#endif // VERSION_GENERATED_GEN_H
""".format(
**version_info
)
)
**version_info
)
with open("core/version_hash.gen.cpp", "w", encoding="utf-8", newline="\n") as fhash:
fhash.write(
"""\
version_hash_data = """\
/* THIS FILE IS GENERATED DO NOT EDIT */
#include "core/version.h"
const char *const VERSION_HASH = "{git_hash}";
const uint64_t VERSION_TIMESTAMP = {git_timestamp};
""".format(
**version_info
)
)
**version_info
)
write_file_if_needed("core/version_generated.gen.h", version_info_header)
write_file_if_needed("core/version_hash.gen.cpp", version_hash_data)
def parse_cg_file(fname, uniforms, sizes, conditionals):
@ -385,15 +392,18 @@ def is_module(path):
def write_disabled_classes(class_list):
with open("core/disabled_classes.gen.h", "w", encoding="utf-8", newline="\n") as f:
f.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
f.write("#ifndef DISABLED_CLASSES_GEN_H\n")
f.write("#define DISABLED_CLASSES_GEN_H\n\n")
for c in class_list:
cs = c.strip()
if cs != "":
f.write("#define ClassDB_Disable_" + cs + " 1\n")
f.write("\n#endif\n")
file_contents = ""
file_contents += "/* THIS FILE IS GENERATED DO NOT EDIT */\n"
file_contents += "#ifndef DISABLED_CLASSES_GEN_H\n"
file_contents += "#define DISABLED_CLASSES_GEN_H\n\n"
for c in class_list:
cs = c.strip()
if cs != "":
file_contents += "#define ClassDB_Disable_" + cs + " 1\n"
file_contents += "\n#endif\n"
write_file_if_needed("core/disabled_classes.gen.h", file_contents)
def write_modules(modules):
@ -435,9 +445,7 @@ void uninitialize_modules(ModuleInitializationLevel p_level) {
uninitialize_cpp,
)
# NOTE: It is safe to generate this file here, since this is still executed serially
with open("modules/register_module_types.gen.cpp", "w", encoding="utf-8", newline="\n") as f:
f.write(modules_cpp)
write_file_if_needed("modules/register_module_types.gen.cpp", modules_cpp)
def convert_custom_modules_path(path):