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.
This commit is contained in:
Dan Klishch 2023-08-29 08:07:25 -04:00 committed by Andrew Kaster
parent da060eedb1
commit da30afa0c3
Notes: sideshowbarker 2024-07-16 20:44:03 +09:00

View file

@ -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