test_driver.py: Refactor support for list-vars-for-cmake

Signed-off-by: Ronald Cron <[email protected]>
This commit is contained in:
Ronald Cron
2025-12-09 01:04:25 +01:00
parent 3b39e68363
commit 08b152ffcf
+19 -52
View File
@@ -33,50 +33,6 @@ def get_parsearg_base() -> argparse.ArgumentParser:
"name defaults to '<DRIVER>-list-vars.cmake'.")
return parser
def iter_code_files(root: Path) -> Iterable[Path]:
"""
Iterate over all "*.c" and "*.h" files found recursively under the `include`
and `src` subdirectories of `root`.
"""
for directory in ("include", "src"):
directory_path = root / directory
for ext in (".c", ".h"):
yield from directory_path.rglob(f"*{ext}")
def get_src_relpaths(builtin: Path, exclude_files: Set[str]) -> List[Path]:
"""
Return the relative paths of all *.c and *.h files under `builtin`,
excluding those whose names match any of the patterns in `exclude_files`.
The returned paths are relative to `builtin`.
"""
out = []
for file in iter_code_files(builtin):
if not any(fnmatch(file.name, pattern) for pattern in exclude_files):
out.append(file.relative_to(builtin))
out.sort()
return out
def get_dst_relpaths(src_relpaths: List[Path], driver: str) -> List[Path]:
"""
Return the relative paths of the *.c and *.h files generated by the script.
These paths are the same as in `src_relpaths`, except that occurrences of
`mbedtls` in `include/mbedtls/...` paths are replaced with `driver`.
The returned paths are relative to `dst_dir`.
"""
out = []
for path in src_relpaths:
parts = list(path.parts)
if parts[0] == "include" and parts[1] == "mbedtls":
parts[1] = driver
out.append(Path(*parts))
return out
def run_ctags(file: Path) -> Set[str]:
"""
Extract the C identifiers in `file` using ctags.
@@ -153,15 +109,15 @@ class TestDriverGenerator:
raise RuntimeError(f'"src" directory in {src_dir} not found')
def write_list_vars_for_cmake(self, fname: str) -> None:
src_relpaths = get_src_relpaths(self.src_dir, self.exclude_files)
src_relpaths = self.__iter_src_code_files()
with open(self.dst_dir / fname, "w") as f:
f.write(f"set({self.driver}_input_files " + \
" ".join(str(path) for path in src_relpaths) + ")\n\n")
f.write(f"set({self.driver}_files " + \
" ".join(str(path) \
for path in get_dst_relpaths(src_relpaths, self.driver)) + ")\n\n")
" ".join(str(self.__get_dst_relpath(path.relative_to(self.src_dir))) \
for path in src_relpaths) + ")\n\n")
f.write(f"set({self.driver}_src_files " + \
" ".join(str(path) \
" ".join(str(path.relative_to(self.src_dir)) \
for path in src_relpaths if path.suffix == ".c") + ")")
def build_tree(self) -> None:
@@ -203,7 +159,7 @@ class TestDriverGenerator:
f.relative_to(test_driver_include_dir).as_posix() \
for f in test_driver_include_dir.rglob("*.h")
}
for f in iter_code_files(self.dst_dir):
for f in self.__iter_code_files(self.dst_dir):
self.__rewrite_inclusions_in_file(f, headers, \
src_include_dir_name, self.driver)
@@ -217,7 +173,7 @@ class TestDriverGenerator:
need to be prefixed.
"""
identifiers = set()
for file in iter_code_files(self.dst_dir):
for file in self.__iter_code_files(self.dst_dir):
identifiers.update(run_ctags(file))
identifiers_with_prefixes = set()
@@ -232,9 +188,20 @@ class TestDriverGenerator:
with the test driver prefix: <DRIVER>_ for uppercase identifiers,
and <driver>_ for lowercase ones.
"""
for f in iter_code_files(self.dst_dir):
for f in self.__iter_code_files(self.dst_dir):
self.__prefix_identifiers_in_file(f, identifiers_to_prefix, self.driver)
@staticmethod
def __iter_code_files(root: Path) -> Iterable[Path]:
"""
Iterate over all "*.c" and "*.h" files found recursively under the
`include` and `src` subdirectories of `root`.
"""
for directory in ("include", "src"):
directory_path = root / directory
for ext in (".c", ".h"):
yield from directory_path.rglob(f"*{ext}")
def __iter_src_code_files(self) -> List[Path]:
"""
Iterate over all "*.c" and "*.h" files found recursively under the
@@ -243,7 +210,7 @@ class TestDriverGenerator:
`self.exclude_files`.
"""
out = []
for file in iter_code_files(self.src_dir):
for file in self.__iter_code_files(self.src_dir):
if not any(fnmatch(file.name, pattern) for pattern in self.exclude_files):
out.append(file)
return out