# Copyright (C) 2026 Hector van der Aa # Copyright (C) 2026 Association Exergie # SPDX-License-Identifier: GPL-3.0-or-later from pathlib import Path import tomllib CORE_CONFIG = { "CM7": { "dir_key": "m7_dir", "type_key": "m7_type", "supported_type": "ST", }, "CM4": { "dir_key": "m4_dir", "type_key": "m4_type", "supported_type": "Zephyr", }, } def find_project_root( start: Path | None = None, filename: str = "neoecu.toml", max_depth: int = 10 ) -> Path | None: current = start or Path.cwd() for _ in range(max_depth + 1): if (current / filename).exists(): return current # return the directory (project root) if current.parent == current: break current = current.parent return None def load_config(root: Path) -> dict: config_path = root / "neoecu.toml" with open(config_path, "rb") as f: return tomllib.load(f) def configured_cores(config: dict) -> list[str]: build_config = config.get("build", {}) cores: list[str] = [] for core, core_config in CORE_CONFIG.items(): if all(build_config.get(key) for key in (core_config["dir_key"], core_config["type_key"])): cores.append(core) return cores def core_root(root: Path, config: dict, core: str) -> Path: return root / config["build"][CORE_CONFIG[core]["dir_key"]] def select_cores(config: dict, args) -> list[str]: selected = [] if getattr(args, "CM7", False): selected.append("CM7") if getattr(args, "CM4", False): selected.append("CM4") if not selected: selected = configured_cores(config) configured = configured_cores(config) missing = [core for core in selected if core not in configured] if missing: missing_cores = ", ".join(missing) raise ValueError(f"{missing_cores} is not fully configured in neoecu.toml") return selected def check_pair(config: dict) -> bool: build_config = config.get("build", {}) cores = configured_cores(config) if not cores: return False for core in cores: core_config = CORE_CONFIG[core] if build_config[core_config["type_key"]] != core_config["supported_type"]: return False return True