From da30afa0c3980103d8855272d155f85a225e5061 Mon Sep 17 00:00:00 2001 From: Dan Klishch Date: Tue, 29 Aug 2023 08:07:25 -0400 Subject: [PATCH] Meta: Allow overriding root for local includes Previously, we always assumed that local includes are relative to the parent directory of a file. This effectively banned style-wise multi-directory subprojects which are not libraries, since there was no way to cleanly reference local file from a different directory. This commit relaxes the restrictions by introducing LOCAL_INCLUDE_ROOT_OVERRIDES. Entry in the set changes root of the local includes to itself for all files in its subtree. --- Meta/check-style.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/Meta/check-style.py b/Meta/check-style.py index f520874791..ff3a9f88a5 100755 --- a/Meta/check-style.py +++ b/Meta/check-style.py @@ -58,6 +58,10 @@ INCLUDE_CHECK_EXCLUDES = { "Userland/Libraries/LibCpp/Tests/preprocessor/", } +LOCAL_INCLUDE_ROOT_OVERRIDES = { + "Meta/Lagom/Tools/CodeGenerators/JSSpecCompiler/", +} + LOCAL_INCLUDE_SUFFIX_EXCLUDES = [ # Some Qt files are required to include their .moc files, which will be located in a deep # subdirectory that we won't find from here. @@ -91,6 +95,12 @@ def is_in_prefix_list(filename, prefix_list): ) +def find_matching_prefix(filename, prefix_list): + matching_prefixes = [prefix for prefix in prefix_list if filename.startswith(prefix)] + assert len(matching_prefixes) <= 1 + return matching_prefixes[0] if matching_prefixes else None + + def run(): errors_license = [] errors_pragma_once_bad = [] @@ -124,7 +134,10 @@ def run(): if BAD_INCLUDE_COMPLEX.search(file_content): errors_include_bad_complex.append(filename) if not is_in_prefix_list(filename, INCLUDE_CHECK_EXCLUDES): - file_directory = pathlib.Path(filename).parent + if include_root := find_matching_prefix(filename, LOCAL_INCLUDE_ROOT_OVERRIDES): + local_include_root = pathlib.Path(include_root) + else: + local_include_root = pathlib.Path(filename).parent for include_line in ANY_INCLUDE_PATTERN.findall(file_content): if SYSTEM_INCLUDE_PATTERN.match(include_line): # Don't try to resolve system-style includes, as these might depend on generators. @@ -137,7 +150,7 @@ def run(): continue relative_filename = local_match.group(1) - referenced_file = file_directory.joinpath(relative_filename) + referenced_file = local_include_root.joinpath(relative_filename) if referenced_file.suffix in LOCAL_INCLUDE_SUFFIX_EXCLUDES: continue