|
|
@@ -19,12 +19,21 @@ ROOT = Path(__file__).resolve().parents[2]
|
|
|
TEST_DIR = Path(__file__).resolve().parent
|
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
|
|
-DEFAULT_VERIFY_PYTHON = Path(
|
|
|
- os.environ.get(
|
|
|
- "PROD2U_P4_COCOTB_PYTHON",
|
|
|
- "/root/.cache/prod2u/venvs/verify/bin/python",
|
|
|
- )
|
|
|
-).expanduser()
|
|
|
+def _default_verify_python() -> Path:
|
|
|
+ for key in ("PROD2U_P4_COCOTB_PYTHON", "PRODU2_VERIFY_PYTHON"):
|
|
|
+ value = os.environ.get(key, "").strip()
|
|
|
+ if value:
|
|
|
+ return Path(value).expanduser()
|
|
|
+ for candidate in (
|
|
|
+ Path("/data/prod2u/venvs/verify/bin/python"),
|
|
|
+ Path.home() / ".cache" / "prod2u" / "venvs" / "verify" / "bin" / "python",
|
|
|
+ ):
|
|
|
+ if candidate.is_file():
|
|
|
+ return candidate
|
|
|
+ return Path("/data/prod2u/venvs/verify/bin/python")
|
|
|
+
|
|
|
+
|
|
|
+DEFAULT_VERIFY_PYTHON = _default_verify_python()
|
|
|
|
|
|
# Poisonous env vars exported by OSS CAD tabbypy3 / environment scripts.
|
|
|
_OSS_CAD_POISON = (
|
|
|
@@ -107,9 +116,41 @@ def _is_oss_cad_bin(part: str) -> bool:
|
|
|
)
|
|
|
|
|
|
|
|
|
-def _tool_bin_dirs() -> list[str]:
|
|
|
- """Absolute directories for iverilog/vvp so OSS CAD bin is not required on PATH."""
|
|
|
+def _tool_bin_dirs(shim_dir: Path | None = None) -> list[str]:
|
|
|
+ """Dirs with iverilog/vvp without exposing OSS CAD python shims on PATH.
|
|
|
+
|
|
|
+ Host often only has Icarus inside oss-cad-suite/bin. Putting that whole bin
|
|
|
+ on PATH resurrects tabbypy3 for VPI; instead symlink iverilog/vvp into a
|
|
|
+ private shim dir when no system install exists.
|
|
|
+ """
|
|
|
dirs: list[str] = []
|
|
|
+ if shim_dir is not None:
|
|
|
+ shim_dir.mkdir(parents=True, exist_ok=True)
|
|
|
+ for name in ("iverilog", "vvp"):
|
|
|
+ resolved = shutil.which(name)
|
|
|
+ if not resolved:
|
|
|
+ for candidate in (
|
|
|
+ Path("/data/prod2u/oss-cad-suite/latest/bin") / name,
|
|
|
+ Path("/usr/local/bin") / name,
|
|
|
+ Path("/usr/bin") / name,
|
|
|
+ ):
|
|
|
+ if candidate.is_file() and os.access(candidate, os.X_OK):
|
|
|
+ resolved = str(candidate)
|
|
|
+ break
|
|
|
+ if not resolved:
|
|
|
+ continue
|
|
|
+ # OSS CAD iverilog/vvp are shell wrappers using $0-relative paths;
|
|
|
+ # symlinks break them. Emit absolute exec wrappers instead.
|
|
|
+ wrapper = shim_dir / name
|
|
|
+ if wrapper.exists() or wrapper.is_symlink():
|
|
|
+ wrapper.unlink()
|
|
|
+ wrapper.write_text(
|
|
|
+ "#!/bin/sh\n"
|
|
|
+ f'exec "{resolved}" "$@"\n',
|
|
|
+ encoding="ascii",
|
|
|
+ )
|
|
|
+ wrapper.chmod(0o755)
|
|
|
+ dirs.append(str(shim_dir))
|
|
|
for name in ("iverilog", "vvp"):
|
|
|
resolved = shutil.which(name)
|
|
|
if resolved:
|
|
|
@@ -139,7 +180,7 @@ def _sanitize_env(python_exe: Path, build_dir: Path) -> dict[str, str]:
|
|
|
shim_path.unlink()
|
|
|
shim_path.symlink_to(python_exe)
|
|
|
|
|
|
- path_parts: list[str] = [python_bin, str(shim_dir), *_tool_bin_dirs()]
|
|
|
+ path_parts: list[str] = [python_bin, str(shim_dir), *_tool_bin_dirs(shim_dir)]
|
|
|
for part in env.get("PATH", "").split(os.pathsep):
|
|
|
if not part or _is_oss_cad_bin(part):
|
|
|
continue
|
|
|
@@ -160,6 +201,33 @@ def _sanitize_env(python_exe: Path, build_dir: Path) -> dict[str, str]:
|
|
|
return env
|
|
|
|
|
|
|
|
|
+def _already_under_selected_python(python_exe: Path) -> bool:
|
|
|
+ """True when this process was launched via the LD-safe wrapper for python_exe.
|
|
|
+
|
|
|
+ OSS CAD wrappers report sys.executable as bare libexec/python3.11 even when
|
|
|
+ argv0 was venv/bin/python or py3bin/python3.11. Re-execing on that mismatch
|
|
|
+ loops forever.
|
|
|
+ """
|
|
|
+ if os.environ.get("PROD2U_COCOTB_REEXEC_DONE") == "1":
|
|
|
+ return True
|
|
|
+ current = Path(sys.executable).expanduser()
|
|
|
+ selected = python_exe.expanduser()
|
|
|
+ if str(current) == str(selected):
|
|
|
+ return True
|
|
|
+ if current.parent == selected.parent and current.name.startswith("python"):
|
|
|
+ return True
|
|
|
+ # Same wrapper family: venv/bin/python -> py3bin -> libexec
|
|
|
+ try:
|
|
|
+ if current.resolve() == selected.resolve():
|
|
|
+ return True
|
|
|
+ except OSError:
|
|
|
+ pass
|
|
|
+ marker = os.environ.get("PROD2U_P4_COCOTB_PYTHON", "").strip()
|
|
|
+ if marker and Path(marker).expanduser() == selected and "libexec" in current.as_posix():
|
|
|
+ return True
|
|
|
+ return False
|
|
|
+
|
|
|
+
|
|
|
def _ensure_running_under(python_exe: Path, build_dir: Path) -> None:
|
|
|
"""Re-exec under the selected interpreter with a sanitized environment.
|
|
|
|
|
|
@@ -171,17 +239,16 @@ def _ensure_running_under(python_exe: Path, build_dir: Path) -> None:
|
|
|
need_reexec = True
|
|
|
if os.environ.get("PYTHONEXECUTABLE"):
|
|
|
need_reexec = True
|
|
|
- current = Path(sys.executable).expanduser()
|
|
|
- if str(current) != str(python_exe) and not (
|
|
|
- current.parent == python_exe.parent and current.name.startswith("python")
|
|
|
- ):
|
|
|
+ if not _already_under_selected_python(python_exe):
|
|
|
need_reexec = True
|
|
|
if not need_reexec:
|
|
|
return
|
|
|
+ env = _sanitize_env(python_exe, build_dir)
|
|
|
+ env["PROD2U_COCOTB_REEXEC_DONE"] = "1"
|
|
|
os.execve(
|
|
|
str(python_exe),
|
|
|
[str(python_exe), "-B", str(Path(__file__).resolve()), *sys.argv[1:]],
|
|
|
- _sanitize_env(python_exe, build_dir),
|
|
|
+ env,
|
|
|
)
|
|
|
|
|
|
|
|
|
@@ -225,20 +292,22 @@ def main() -> int:
|
|
|
|
|
|
runner = get_runner("icarus")
|
|
|
|
|
|
- _orig_set_env = runner._set_env
|
|
|
+ _orig_set_env_common = runner._set_env_common
|
|
|
|
|
|
- def _set_env_without_oss_cad_poison() -> None:
|
|
|
- _orig_set_env()
|
|
|
+ def _set_env_common_without_oss_cad_poison() -> None:
|
|
|
+ _orig_set_env_common()
|
|
|
for key in _OSS_CAD_POISON:
|
|
|
runner.env.pop(key, None)
|
|
|
- # Re-assert PATH without OSS CAD (cocotb may append libs_dir only).
|
|
|
+ # Re-assert PATH without OSS CAD python shims (cocotb may append libs_dir only).
|
|
|
path_parts = [
|
|
|
part
|
|
|
for part in runner.env.get("PATH", "").split(os.pathsep)
|
|
|
if part and not _is_oss_cad_bin(part)
|
|
|
]
|
|
|
runner.env["PATH"] = os.pathsep.join(path_parts)
|
|
|
+ # Prefer LD-safe wrapper; bare libexec sys.executable breaks VPI children.
|
|
|
runner.env["PYGPI_PYTHON_BIN"] = str(python_exe)
|
|
|
+ runner.env.pop("PYTHONEXECUTABLE", None)
|
|
|
audit = build_dir / "vpi-env-audit.txt"
|
|
|
audit.write_text(
|
|
|
"\n".join(
|
|
|
@@ -261,7 +330,7 @@ def main() -> int:
|
|
|
f"{runner.env['PYTHONEXECUTABLE']}"
|
|
|
)
|
|
|
|
|
|
- runner._set_env = _set_env_without_oss_cad_poison # type: ignore[method-assign]
|
|
|
+ runner._set_env_common = _set_env_common_without_oss_cad_poison # type: ignore[method-assign]
|
|
|
|
|
|
runner.build(
|
|
|
sources=[*rtl_sources(), TEST_DIR / "prod2u_door_temp_uart_top.v"],
|