bjwanneng/veriflow-cc / src/claude_skills/vf-rtl
vf-rtl
Verifiedcore/init.py
View on GitHub#!/usr/bin/env python3
"""VeriFlow Pipeline Initialization - zero external dependencies.
Usage: python init.py <project_dir>
Creates workspace directories, discovers tools, writes eda_env.sh,
initializes stage journal, and outputs structured JSON result.
"""
import json
import os
import shlex
import shutil
import subprocess
import sys
from pathlib import Path
from state import STAGE_ORDER
def discover_python() -> str:
"""Find a working Python executable."""
candidates = []
# PATH-based candidates
for name in ("python3", "python"):
p = shutil.which(name)
if p:
candidates.append(p)
# Common Windows locations
if sys.platform == "win32":
for p in Path(os.path.expanduser("~")).glob(
"AppData/Local/Programs/Python/*/python.exe"
):
candidates.append(str(p))
# Unix locations
for d in ("/usr/bin", "/usr/local/bin"):
p = Path(d) / "python3"
if p.exists():
candidates.append(str(p))
for p in candidates:
# Skip Windows Store stub
if "WindowsApps" in p:
continue
try:
result = subprocess.run(
[p, "--version"], capture_output=True, timeout=5
)
if result.returncode == 0:
return p
except (OSError, subprocess.TimeoutExpired):
continue
return ""
def discover_eda() -> tuple[str, str]:
"""Find EDA tools (iverilog, yosys). Returns (eda_bin, eda_lib)."""
# Search both Unix-style and Windows-style paths.
# On Windows, Python uses native filesystem APIs so /c/oss-cad-suite
# resolves to C:\c\oss-cad-suite (wrong). We need C:\oss-cad-suite.
# Platform-aware search — mixing Windows and MSYS paths on native Windows
# Python causes wrong resolution (e.g. Path("/c/oss-cad-suite") →
# C:\c\oss-cad-suite). Filter dirs by platform.
if sys.platform == "win32":
search_dirs = [
r"C:\oss-cad-suite",
r"C:\Program Files\iverilog",
r"C:\Program Files (x86)\iverilog",
]
else:
search_dirs = [
"/opt/oss-cad-suite",
"/usr/local",
"/usr",
os.path.expanduser("~/.local"),
]
for base in search_dirs:
base_path = Path(base)
bin_dir = base_path / "bin"
# Check for iverilog or iverilog.exe in bin
iverilog_path = bin_dir / "iverilog"
iverilog_exe_path = bin_dir / "iverilog.exe"
if not ((bin_dir.is_dir() and iverilog_path.exists())
or iverilog_exe_path.exists()):
continue
eda_bin = str(bin_dir)
eda_lib = ""
lib_dir = base_path / "lib"
if lib_dir.is_dir():
eda_lib = str(lib_dir)
ivl_dir = base_path / "lib" / "ivl"
if ivl_dir.is_dir():
eda_lib = f"{eda_lib}{os.pathsep}{ivl_dir}" if eda_lib else str(ivl_dir)
return eda_bin, eda_lib
# Fallback: check PATH
iverilog = shutil.which("iverilog")
if iverilog:
eda_bin = str(Path(iverilog).parent)
# Also look for lib relative to bin
eda_lib = ""
lib_dir = Path(eda_bin).parent / "lib"
if lib_dir.is_dir():
eda_lib = str(lib_dir)
return eda_bin, eda_lib
return "", ""
def verify_iverilog(iverilog_path: str) -> tuple[bool, str]:
"""Actually run iverilog -V to verify it works (catches DLL issues on Windows).
Returns (success, version_or_error).
"""
if not iverilog_path:
return False, "iverilog path is empty"
try:
result = subprocess.run(
[iverilog_path, "-V"],
capture_output=True, text=True, timeout=10,
)
if result.returncode == 0:
version = (result.stdout + result.stderr).split("\n")[0]
return True, version
return False, f"iverilog -V returned code {result.returncode}: {(result.stderr or '')[:200]}"
except FileNotFoundError:
return False, f"iverilog not found at {iverilog_path}"
except OSError as e:
# On Windows, this catches DLL loading failures (e.g., 0xC0000139)
return False, f"Cannot execute iverilog (DLL/library error?): {e}"
except subprocess.TimeoutExpired:
return False, "iverilog -V timed out"
def check_cocotb(python_exe: str) -> bool:
"""Check if cocotb is available."""
if not python_exe:
return False
try:
result = subprocess.run(
[python_exe, "-c", "import cocotb; import cocotb_tools.runner"],
capture_output=True,
timeout=10,
)
return result.returncode == 0
except (OSError, subprocess.TimeoutExpired):
return False
def init_journal(project_dir: Path, is_resume: bool) -> None:
"""Initialize or append to stage journal."""
journal_path = project_dir / "logs" / "stage_journal.md"
journal_path.parent.mkdir(parents=True, exist_ok=True)
from datetime import datetime
ts = datetime.now().isoformat()
if is_resume:
with open(journal_path, "a", encoding="utf-8") as f:
f.write(f"\n---\n\n**Session resumed** at {ts}\n\n")
elif not journal_path.exists():
with open(journal_path, "w", encoding="utf-8") as f:
f.write(
"# VeriFlow Pipeline Stage Journal\n\n"
"This file records the progress, outputs, and key decisions for each pipeline stage.\n"
)
f.write(f"\n## Pipeline Start\n**Timestamp**: {ts}\n")
def write_eda_env(path: Path, python_exe: str, eda_bin: str, eda_lib: str,
ivl_home: str, cocotb_available: bool, skill_paths: list) -> None:
"""Write a `source`-able eda_env.sh with every value shell-quoted.
Paths are shlex.quote'd (single-quoted) so spaces, `$`, backticks, and `"`
in a path round-trip verbatim instead of breaking the export or injecting
shell. `$PATH` and `${PYTHONPATH:-}` are deliberately left unquoted so they
expand when the file is sourced.
`skill_paths` is the list of directories put on PYTHONPATH: the skill root
first, then every subdir holding .py modules (core/, runners/, ...). Bare
imports (`from rtl_utils import ...`) resolve across the grouped source tree.
"""
q = shlex.quote
# Each PATH/PYTHONPATH entry is quoted individually; adjacent quoted tokens
# concatenate in shell, so `'/a b':'/c d':$PATH` is valid.
path_entries = [q(p) for p in [eda_bin, eda_lib] if p] + ["$PATH"]
path_str = os.pathsep.join(path_entries)
pythonpath_str = os.pathsep.join([q(p) for p in skill_paths] + ["${PYTHONPATH:-}"])
with open(path, "w", encoding="utf-8") as f:
f.write(f"export PYTHON_EXE={q(python_exe)}\n")
f.write(f"export EDA_BIN={q(eda_bin)}\n")
f.write(f"export EDA_LIB={q(eda_lib)}\n")
f.write(f"export IVL_HOME={q(ivl_home)}\n")
f.write(
f'export COCOTB_AVAILABLE="{"true" if cocotb_available else "false"}"\n'
)
f.write(f"export PATH={path_str}\n")
f.write(f"export PYTHONPATH={pythonpath_str}\n")
def main():
if len(sys.argv) < 2:
print("Usage: python init.py <project_dir>", file=sys.stderr)
sys.exit(1)
project_dir = Path(sys.argv[1]).resolve()
# Validate requirement.md
req_file = project_dir / "requirement.md"
if not req_file.exists():
print(f"[FATAL] requirement.md not found in {project_dir}", file=sys.stderr)
sys.exit(1)
# Create directories
for d in [
"workspace/docs",
"workspace/rtl",
"workspace/tb",
"workspace/sim",
"workspace/synth",
".veriflow",
"logs",
]:
(project_dir / d).mkdir(parents=True, exist_ok=True)
# Report input files
inputs = {}
for name in ("requirement.md", "constraints.md", "design_intent.md"):
exists = (project_dir / name).exists()
inputs[name] = exists
print(f"[INPUT] {name}: {'YES' if exists else 'NO'}")
context_dir = project_dir / "context"
if context_dir.exists():
ctx_files = list(context_dir.glob("*.md"))
print(f"[INPUT] context/: {len(ctx_files)} file(s)")
for f in ctx_files:
print(f" - {f.name}")
inputs["context_files"] = [f.name for f in ctx_files]
else:
print("[INPUT] context/: 0 file(s)")
inputs["context_files"] = []
# Discover tools
python_exe = discover_python()
print(f"[ENV] Python: {python_exe or 'NOT FOUND'}")
cocotb_available = check_cocotb(python_exe)
print(f"[ENV] cocotb: {'AVAILABLE' if cocotb_available else 'NOT AVAILABLE'}")
eda_bin, eda_lib = discover_eda()
print(f"[ENV] EDA_BIN={eda_bin} EDA_LIB={eda_lib}")
# Detect IVL_HOME (iverilog needs this for its internal preprocessor)
ivl_home = ""
if eda_bin:
eda_base = str(Path(eda_bin).parent)
for candidate in [
Path(eda_base) / "lib" / "ivl",
Path(eda_bin) / ".." / "lib" / "ivl",
]:
if candidate.is_dir():
ivl_home = str(candidate.resolve())
break
# Write eda_env.sh
veriflow_dir = project_dir / ".veriflow"
eda_env_path = veriflow_dir / "eda_env.sh"
# Skill directory holds state.py + helper scripts. Exporting it (and every
# subdir holding .py) on PYTHONPATH lets every shell that sources eda_env.sh
# run things like `python -c "from state import PipelineState"` without
# per-call PYTHONPATH wrangling. Forward-compatible: when flat (no subdirs),
# skill_paths is just [skill_root].
skill_root = Path(__file__).resolve().parent
# init.py lives in core/; walk up to the dir holding SKILL.md (the skill
# root) before enumerating subdirs. Same in source and deployed layout.
while not (skill_root / "SKILL.md").exists() and skill_root.parent != skill_root:
skill_root = skill_root.parent
_DATA_DIRS = {"templates", "references", "docs", "__pycache__"}
skill_paths = [str(skill_root)]
for _sub in sorted(skill_root.iterdir()):
if _sub.is_dir() and _sub.name not in _DATA_DIRS:
skill_paths.append(str(_sub))
write_eda_env(
eda_env_path,
python_exe=python_exe,
eda_bin=eda_bin,
eda_lib=eda_lib,
ivl_home=ivl_home,
cocotb_available=cocotb_available,
skill_paths=skill_paths,
)
print(f"[ENV] Wrote {eda_env_path}")
if ivl_home:
print(f"[ENV] IVL_HOME={ivl_home}")
# Verify iverilog actually runs (catches DLL issues on Windows)
if eda_bin:
iverilog_path = ""
for name in ("iverilog", "iverilog.exe"):
p = Path(eda_bin) / name
if p.exists():
iverilog_path = str(p)
break
if iverilog_path:
ok, msg = verify_iverilog(iverilog_path)
if ok:
print(f"[ENV] iverilog: {msg}")
else:
print(f"[WARN] iverilog found but verification failed: {msg}")
print("[WARN] Simulation may fail. Check DLL/library dependencies.")
if sys.platform == "win32":
print("[HINT] On Windows, ensure oss-cad-suite bin/ and lib/ are both on PATH")
else:
print("[WARN] iverilog binary not found in EDA_BIN")
# Windows DLL check
if sys.platform == "win32" and eda_bin:
eda_bin_path = Path(eda_bin)
dll_files = list(eda_bin_path.glob("*.dll"))
if dll_files:
print(f"[ENV] DLLs found in EDA_BIN: {len(dll_files)}")
else:
lib_dlls = []
for lib_dir_str in (eda_lib.split(os.pathsep) if eda_lib else []):
lib_dir = Path(lib_dir_str)
if lib_dir.is_dir():
lib_dlls.extend(lib_dir.glob("*.dll"))
if lib_dlls:
print(f"[WARN] DLLs found in EDA_LIB ({len(lib_dlls)}) but not in EDA_BIN")
print("[HINT] Ensure EDA_LIB directories are on PATH for Windows DLL resolution")
else:
print("[WARN] No DLLs found near iverilog — may fail on Windows")
# Check existing state
state_path = veriflow_dir / "pipeline_state.json"
state = {"is_resume": False, "stages_completed": [], "next_stage": "spec_golden"}
if state_path.exists():
try:
data = json.loads(state_path.read_text(encoding="utf-8"))
state["is_resume"] = True
state["stages_completed"] = data.get("stages_completed", [])
completed = state["stages_completed"]
stages = STAGE_ORDER
for s in stages:
if s not in completed:
state["next_stage"] = s
break
else:
state["next_stage"] = "complete"
print(f"[STATUS] Resuming — next stage: {state['next_stage']}")
print(f"[STATUS] Completed: {completed}")
except (json.JSONDecodeError, OSError) as e:
print(f"[STATUS] Corrupted state, starting fresh: {e}")
else:
print("[STATUS] New project, starting from Stage 1.")
# Init journal
init_journal(project_dir, state["is_resume"])
# Output structured JSON for main session
result = {
"project_dir": str(project_dir),
"python_exe": python_exe,
"eda_bin": eda_bin,
"eda_lib": eda_lib,
"cocotb_available": cocotb_available,
"is_resume": state["is_resume"],
"stages_completed": state["stages_completed"],
"next_stage": state["next_stage"],
"inputs": inputs,
}
result_path = veriflow_dir / "init_result.json"
try:
result_path.write_text(json.dumps(result, indent=2), encoding="utf-8")
except OSError as e:
print(f"[WARN] Failed to write init_result.json: {e}", file=sys.stderr)
print(f"\n[INIT] Complete. Result: {result_path}")
if __name__ == "__main__":
main()
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 194k | Updated | Claude CodeCodex | Verified | Install |