blob: 85ba45759eb47ebc74fe8ffedd452064228266c5 [file] [log] [blame]
From 4e7de9e20892973b46be2fb9761a670cf0509003 Mon Sep 17 00:00:00 2001
From: Alex Van Damme <atv@google.com>
Date: Thu, 10 Jul 2025 16:03:33 -0700
Subject: [PATCH 6/6] Separate build from test for Verilator
---
cocotb/BUILD.bazel | 2 +-
cocotb/cocotb.bzl | 9 ++++++++-
cocotb/cocotb_wrapper.py | 17 ++++++++++++++++-
3 files changed, 25 insertions(+), 3 deletions(-)
diff --git a/cocotb/BUILD.bazel b/cocotb/BUILD.bazel
index ed8a3a0..72b19fb 100644
--- a/cocotb/BUILD.bazel
+++ b/cocotb/BUILD.bazel
@@ -25,5 +25,5 @@ py_binary(
python_version = "PY3",
srcs_version = "PY3",
visibility = ["//visibility:public"],
- deps = [],
+ deps = ["@bazel_tools//tools/python/runfiles"],
)
diff --git a/cocotb/cocotb.bzl b/cocotb/cocotb.bzl
index 2b0a3c2..90c70bc 100644
--- a/cocotb/cocotb.bzl
+++ b/cocotb/cocotb.bzl
@@ -161,6 +161,7 @@ def _get_test_command(ctx, verilog_files, vhdl_files):
waves_args +
seed_args +
test_module_args +
+ (" --model {}".format(ctx.executable.model.short_path) if ctx.attr.sim_name == "verilator" else "" ) +
("&& cp -fr `pwd`/sim_build/simv.vdb $TEST_UNDECLARED_OUTPUTS_DIR" if ctx.attr.sim_name == "vcs" else "")
)
@@ -185,7 +186,8 @@ def _cocotb_test_impl(ctx):
files = ctx.files.cocotb_wrapper +
verilog_files +
vhdl_files +
- ctx.files.test_module,
+ ctx.files.test_module +
+ ctx.files.model,
transitive_files = _collect_transitive_files(ctx),
).merge(
_collect_transitive_runfiles(ctx),
@@ -253,6 +255,11 @@ _cocotb_test_attrs = {
doc = "Verilog include directories",
default = [],
),
+ "model": attr.label(
+ executable = True,
+ doc = "Verilated model binary",
+ cfg = "exec",
+ ),
"parameters": attr.string_dict(
doc = "Verilog parameters or VHDL generics",
default = {},
diff --git a/cocotb/cocotb_wrapper.py b/cocotb/cocotb_wrapper.py
index ce392a9..1703e4f 100644
--- a/cocotb/cocotb_wrapper.py
+++ b/cocotb/cocotb_wrapper.py
@@ -165,6 +165,11 @@ def cocotb_argument_parser():
default="results.xml",
help="Name of xUnit XML file to store test results in",
)
+ parser.add_argument(
+ "--model",
+ default=None,
+ help="Verilated model binary",
+ )
return parser
@@ -188,7 +193,17 @@ if __name__ == "__main__":
cocotb_tools.runner.MAX_PARALLEL_BUILD_JOBS = 32
runner = get_runner(args.sim)
- runner.build(**build_flags)
+
+ if args.sim == "verilator":
+ import os
+ from bazel_tools.tools.python.runfiles import runfiles
+ r = runfiles.Create()
+ sim_build = os.path.dirname(r.Rlocation(f"kelvin_hw/{args.model}"))
+ test_flags['build_dir'] = sim_build
+ test_flags['extra_env']['LD_LIBRARY_PATH'] = "{}:{}".format(r.Rlocation("kelvin_hw/external/kelvin_pip_deps_cocotb/cocotb/libs"), os.environ.get("LD_LIBRARY_PATH", ""))
+ os.environ.pop('LD_LIBRARY_PATH', None)
+ else:
+ runner.build(**build_flags)
results_xml = runner.test(**test_flags)
(num_tests, num_failed) = get_results(results_xml)
sys.exit(num_failed)
--
2.50.0.727.gbf7dc18ff4-goog