"""Static Makefile checks UT-B001 through UT-B010 and UT-B012 through UT-B015.""" from __future__ import annotations import re from pathlib import Path from typing import Any from .contracts import EmptyParams, EvaluationContext, EvaluatorResult from .tool_paths import csh_path, modules_cshrc from .ut_common import check, measurement, unavailable from .ut_mechanical import _manifest, _path_from_manifest, _task_dir _COV = "stmt,cond,tgl,fsm,assert,group" def _assign(text: str, name: str) -> str | None: match = re.search(rf"^{name}\s*\??=\s*(.*)$", text, re.MULTILINE) return match.group(1).strip() if match else None def _names(inventory: Path) -> list[str]: if not inventory.is_file(): return [] return [ line.split("logical_name:", 1)[1].strip().strip("'\"") for line in inventory.read_text(encoding="utf-8", errors="replace").splitlines() if "logical_name:" in line ] def _env(context: EvaluationContext) -> dict[str, Any] | None: task = _task_dir(context) manifest = _manifest(task) if task is not None else None if task is None or manifest is None: return None env = _path_from_manifest(task, manifest, "env_output_dir") if env is None: return None identity = manifest.get("identity") if isinstance(manifest.get("identity"), dict) else {} dut = identity.get("dut_name") if isinstance(identity.get("dut_name"), str) else "" sim = env / "sim" makefile = sim / "Makefile" text = makefile.read_text(encoding="utf-8", errors="replace") if makefile.is_file() else "" return {"task": task, "env": env, "sim": sim, "dut": dut, "make": makefile, "text": text} def ut_b001_makefile_path(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params env = _env(context) if env is None: return unavailable("UT-B001", "环境目录不存在") return check("UT-B001", {"makefile": env["make"].is_file()}, "sim/Makefile") def ut_b002_filelist(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params env = _env(context) if env is None or not env["text"]: return unavailable("UT-B002", "Makefile 不存在") filelist = env["sim"] / "filelist.f" flist = _assign(env["text"], "FLIST") missing = [] if filelist.is_file(): for line in filelist.read_text(encoding="utf-8", errors="replace").splitlines(): token = line.strip() if not token or token.startswith("//") or token.startswith("#"): continue if token.startswith("-f "): token = token[3:].strip() path = Path(token) if not path.is_absolute(): path = env["sim"] / token if not path.exists(): missing.append(token) return check("UT-B002", { "filelist": filelist.is_file(), "flist": flist == "filelist.f", "paths": filelist.is_file() and not missing, }, "filelist 与 FLIST") def ut_b003_simulator(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params env = _env(context) if env is None or not env["text"]: return unavailable("UT-B003", "Makefile 不存在") return check("UT-B003", {"sim": _assign(env["text"], "SIM") == "uvs"}, "SIM ?= uvs") def ut_b004_top(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params env = _env(context) if env is None or not env["text"] or not env["dut"]: return unavailable("UT-B004", "Makefile 或 DUT 名不存在") expected = f"tb_{env['dut']}" tb = env["env"] / "tb" / f"{expected}.sv" body = tb.read_text(encoding="utf-8", errors="replace") if tb.is_file() else "" return check("UT-B004", { "top": _assign(env["text"], "TOP") == expected, "module": f"module {expected}" in body, }, "TOP 与 TB module") def ut_b005_default_tc(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params env = _env(context) if env is None or not env["text"]: return unavailable("UT-B005", "Makefile 不存在") names = _names(env["sim"] / "testcase_inventory.yaml") sanity = next((name for name in names if name.endswith("_sanity")), names[0] if names else "") tc = _assign(env["text"], "TC") or "" if not sanity: return unavailable("UT-B005", "用例清单没有 logical_name") return check("UT-B005", {"tc": tc == sanity and not tc.startswith("tc_")}, "默认 TC") def ut_b006_seed(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params env = _env(context) if env is None or not env["text"]: return unavailable("UT-B006", "Makefile 不存在") return check("UT-B006", {"seed": _assign(env["text"], "SEED") == "$(shell date +%s)"}, "SEED 默认值") def ut_b007_timeout(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params env = _env(context) if env is None or not env["text"]: return unavailable("UT-B007", "Makefile 不存在") return check("UT-B007", {"timeout": _assign(env["text"], "TIMEOUT") == "1800"}, "TIMEOUT") def ut_b008_dump(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params env = _env(context) if env is None or not env["text"]: return unavailable("UT-B008", "Makefile 不存在") text = env["text"] return check("UT-B008", { "default_off": _assign(text, "DUMP") == "0", "gated": "ifeq ($(DUMP),1)" in text and "+DUMP" in text, }, "DUMP 开关") def ut_b009_tc_list(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params env = _env(context) if env is None or not env["text"]: return unavailable("UT-B009", "Makefile 不存在") names = _names(env["sim"] / "testcase_inventory.yaml") listed = (_assign(env["text"], "TC_LIST") or "").split() if not names: return unavailable("UT-B009", "用例清单没有 logical_name") return check("UT-B009", {"names": listed == names}, "TC_LIST") def ut_b010_preflight_paths(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params env = _env(context) if env is None or not env["text"]: return unavailable("UT-B010", "Makefile 不存在") text = env["text"] task_env = _assign(text, "TASK_ENV") or "" modules = str(modules_cshrc()) csh = str(csh_path()) return check("UT-B010", { "task_env": bool(task_env) and Path(task_env).is_file(), "modules": modules in text and Path(modules).is_file(), "csh": csh in text and Path(csh).is_file(), }, "TASK_ENV、模块初始化和 csh") def ut_b012_phony(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params env = _env(context) if env is None or not env["text"]: return unavailable("UT-B012", "Makefile 不存在") match = re.search(r"^\.PHONY:\s*(.*)$", env["text"], re.MULTILINE) targets = set((match.group(1) if match else "").split()) required = {"comp", "run", "run_all", "wave", "cov", "clean"} return check("UT-B012", {"phony": required <= targets}, ".PHONY") def ut_b013_comp_flags(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params env = _env(context) if env is None or not env["text"]: return unavailable("UT-B013", "Makefile 不存在") flags = _assign(env["text"], "COMP_FLAGS") or "" tokens = ("-sv", "-timescale", "-top", "-covdir", _COV, "-usdbon", "-debug", "-ddb") return check("UT-B013", {"flags": all(token in flags for token in tokens)}, "COMP_FLAGS") def ut_b014_run_flags(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params env = _env(context) if env is None or not env["text"]: return unavailable("UT-B014", "Makefile 不存在") flags = _assign(env["text"], "RUN_FLAGS") or "" tokens = ("+TC_NAME=$(TC)", "-fixed_seed=$(SEED)", "-usdb=all", _COV, "-covtest") return check("UT-B014", {"flags": all(token in flags for token in tokens)}, "RUN_FLAGS") def ut_b015_run_log(context: EvaluationContext, params: EmptyParams) -> EvaluatorResult: del params env = _env(context) if env is None or not env["text"]: return unavailable("UT-B015", "Makefile 不存在") return check("UT-B015", {"run_log": _assign(env["text"], "RUN_LOG") == "run_$(TC)_$(SEED).log"}, "RUN_LOG") UT_MAKEFILE_STATIC_EVALUATORS = { item[0]: measurement(item[0], item[1], item[2]) for item in ( ("ut_b001_makefile_path", ut_b001_makefile_path, "UT-B001"), ("ut_b002_filelist", ut_b002_filelist, "UT-B002"), ("ut_b003_simulator", ut_b003_simulator, "UT-B003"), ("ut_b004_top", ut_b004_top, "UT-B004"), ("ut_b005_default_tc", ut_b005_default_tc, "UT-B005"), ("ut_b006_seed", ut_b006_seed, "UT-B006"), ("ut_b007_timeout", ut_b007_timeout, "UT-B007"), ("ut_b008_dump", ut_b008_dump, "UT-B008"), ("ut_b009_tc_list", ut_b009_tc_list, "UT-B009"), ("ut_b010_preflight_paths", ut_b010_preflight_paths, "UT-B010"), ("ut_b012_phony", ut_b012_phony, "UT-B012"), ("ut_b013_comp_flags", ut_b013_comp_flags, "UT-B013"), ("ut_b014_run_flags", ut_b014_run_flags, "UT-B014"), ("ut_b015_run_log", ut_b015_run_log, "UT-B015"), ) }