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

@ -13,9 +13,9 @@ _scu_folders = set()
_max_includes_per_scu = 1024
def clear_out_existing_files(output_folder, extension):
def clear_out_stale_files(output_folder, extension, fresh_files):
output_folder = os.path.abspath(output_folder)
# print("clear_out_existing_files from folder: " + output_folder)
# print("clear_out_stale_files from folder: " + output_folder)
if not os.path.isdir(output_folder):
# folder does not exist or has not been created yet,
@ -23,8 +23,9 @@ def clear_out_existing_files(output_folder, extension):
return
for file in glob.glob(output_folder + "/*." + extension):
# print("removed pre-existing file: " + file)
os.remove(file)
if not file in fresh_files:
# print("removed stale file: " + file)
os.remove(file)
def folder_not_found(folder):
@ -87,11 +88,16 @@ def write_output_file(file_count, include_list, start_line, end_line, output_fol
short_filename = output_filename_prefix + num_string + ".gen." + extension
output_filename = output_folder + "/" + short_filename
if _verbose:
print("SCU: Generating: %s" % short_filename)
output_path = Path(output_filename)
output_path.write_text(file_text, encoding="utf8")
if not output_path.exists() or output_path.read_text() != file_text:
if _verbose:
print("SCU: Generating: %s" % short_filename)
output_path.write_text(file_text, encoding="utf8")
elif _verbose:
print("SCU: Generation not needed for: " + short_filename)
return output_filename
def write_exception_output_file(file_count, exception_string, output_folder, output_filename_prefix, extension):
@ -109,11 +115,16 @@ def write_exception_output_file(file_count, exception_string, output_folder, out
short_filename = output_filename_prefix + "_exception" + num_string + ".gen." + extension
output_filename = output_folder + "/" + short_filename
if _verbose:
print("SCU: Generating: " + short_filename)
output_path = Path(output_filename)
output_path.write_text(file_text, encoding="utf8")
if not output_path.exists() or output_path.read_text() != file_text:
if _verbose:
print("SCU: Generating: " + short_filename)
output_path.write_text(file_text, encoding="utf8")
elif _verbose:
print("SCU: Generation not needed for: " + short_filename)
return output_filename
def find_section_name(sub_folder):
@ -214,10 +225,7 @@ def process_folder(folders, sought_exceptions=[], includes_per_scu=0, extension=
output_folder = abs_main_folder + "/scu/"
output_filename_prefix = "scu_" + out_filename
# Clear out any existing files (usually we will be overwriting,
# but we want to remove any that are pre-existing that will not be
# overwritten, so as to not compile anything stale)
clear_out_existing_files(output_folder, extension)
fresh_files = set()
for file_count in range(0, num_output_files):
end_line = start_line + lines_per_file
@ -226,19 +234,28 @@ def process_folder(folders, sought_exceptions=[], includes_per_scu=0, extension=
if file_count == (num_output_files - 1):
end_line = len(found_includes)
write_output_file(
fresh_file = write_output_file(
file_count, found_includes, start_line, end_line, output_folder, output_filename_prefix, extension
)
fresh_files.add(fresh_file)
start_line = end_line
# Write the exceptions each in their own scu gen file,
# so they can effectively compile in "old style / normal build".
for exception_count in range(len(found_exceptions)):
write_exception_output_file(
fresh_file = write_exception_output_file(
exception_count, found_exceptions[exception_count], output_folder, output_filename_prefix, extension
)
fresh_files.add(fresh_file)
# Clear out any stale file (usually we will be overwriting if necessary,
# but we want to remove any that are pre-existing that will not be
# overwritten, so as to not compile anything stale).
clear_out_stale_files(output_folder, extension, fresh_files)
def generate_scu_files(max_includes_per_scu):
print("=============================")