Skip to content

Commit 0b44d76

Browse files
committed
refactor: patch fesvr includes in Verilator build steps and streamline include paths
1 parent 3ae5a82 commit 0b44d76

File tree

2 files changed

+50
-53
lines changed

2 files changed

+50
-53
lines changed

api/steps/verilator/02_verilog_event_step.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,33 @@ async def handler(data, context):
6565
stderr_prefix="verilator verilog",
6666
)
6767

68-
# Remove unwanted file
69-
topname_file = f"{arch_dir}/BBSimHarness.sv"
70-
if os.path.exists(topname_file):
71-
os.remove(topname_file)
68+
# Remove testchipip C++ sources that depend on fesvr (which we don't have).
69+
# SimTSI.v is kept so verilator can resolve the SimTSI module reference in BBSimHarness.sv;
70+
# tsi_tick DPI symbol is satisfied by arch/src/csrc/src/monitor/ioe/tsi_stub.cc instead.
71+
for unwanted in [
72+
f"{build_dir}/testchip_htif.cc",
73+
f"{build_dir}/testchip_htif.h",
74+
f"{build_dir}/testchip_tsi.cc",
75+
f"{build_dir}/testchip_tsi.h",
76+
f"{build_dir}/SimTSI.cc",
77+
]:
78+
if os.path.exists(unwanted):
79+
os.remove(unwanted)
80+
81+
# Patch fesvr includes out of mm.h and mm.cc (copied from testchipip resources).
82+
# They reference fesvr/memif.h which we don't have — our SimDRAM_bb.cc doesn't use it.
83+
for patch_file in [f"{build_dir}/mm.h", f"{build_dir}/mm.cc"]:
84+
if os.path.exists(patch_file):
85+
with open(patch_file, "r") as f:
86+
content = f.read()
87+
patched = "\n".join(
88+
line for line in content.splitlines()
89+
if "fesvr/memif.h" not in line and "fesvr/elfloader.h" not in line
90+
)
91+
if patched != content:
92+
with open(patch_file, "w") as f:
93+
f.write(patched)
94+
context.logger.info(f"Patched fesvr includes from {patch_file}")
7295

7396
# ==================================================================================
7497
# Return result to API

api/steps/verilator/03_build_event_step.py

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -46,68 +46,42 @@ async def handler(data, context):
4646
# Exclude testchipip's SimDRAM.cc — our SimDRAM_bb.cc overrides memory_init
4747
csrcs = [f for f in csrcs if not f.endswith("SimDRAM.cc") or "src/csrc" in f]
4848

49-
# Patch fesvr includes out of build/mm.h and build/mm.cc.
50-
# These files are auto-copied from testchipip by Verilator as SimDRAM.v
51-
# companion sources. They reference fesvr/memif.h which we don't have
52-
# (fesvr is removed). The memif_t dependency was only used by SimDRAM.cc's
53-
# load_elf — our SimDRAM_bb.cc doesn't use it.
54-
for patch_file in [f"{build_dir}/mm.h", f"{build_dir}/mm.cc"]:
55-
if os.path.exists(patch_file):
56-
with open(patch_file, "r") as f:
57-
content = f.read()
58-
patched = "\n".join(
59-
line for line in content.splitlines()
60-
if "fesvr/memif.h" not in line and "fesvr/elfloader.h" not in line
61-
)
62-
if patched != content:
63-
with open(patch_file, "w") as f:
64-
f.write(patched)
65-
context.logger.info(f"Patched fesvr includes from {patch_file}")
49+
# Exclude testchipip's TSI/HTIF C++ sources (deleted in verilog step, but guard anyway).
50+
# tsi_tick DPI symbol is provided by tsi_stub.cc in src/csrc instead.
51+
_tsi_htif = {"testchip_tsi.cc", "testchip_htif.cc", "SimTSI.cc"}
52+
csrcs = [f for f in csrcs if os.path.basename(f) not in _tsi_htif]
6653

6754
topname = "BBSimHarness"
6855

6956
# ==================================================================================
7057
# Build flags
7158
# ==================================================================================
72-
dramsim2_dir = f"{arch_dir}/thirdparty/chipyard/tools/DRAMSim2"
73-
74-
# Find readline headers/libs in nix store (not in standard paths under nix)
75-
rl_headers = glob.glob("/nix/store/*readline*-dev/include/readline/readline.h")
76-
readline_inc = os.path.dirname(os.path.dirname(rl_headers[0])) if rl_headers else ""
77-
rl_libs = glob.glob("/nix/store/*readline*/lib/libreadline.so")
78-
readline_lib = os.path.dirname(rl_libs[0]) if rl_libs else ""
79-
80-
# Find zlib headers/libs in nix store
81-
zlib_headers = glob.glob("/nix/store/*zlib*-dev/include/zlib.h")
82-
if not zlib_headers:
83-
zlib_headers = glob.glob("/nix/store/*zlib*/include/zlib.h")
84-
zlib_inc = os.path.dirname(zlib_headers[0]) if zlib_headers else ""
85-
zlib_libs = glob.glob("/nix/store/*zlib*/lib/libz.so")
86-
zlib_lib = os.path.dirname(zlib_libs[0]) if zlib_libs else ""
87-
88-
inc_paths = [
89-
dramsim2_dir,
90-
build_dir,
91-
f"{arch_dir}/src/csrc/include",
92-
]
93-
if readline_inc:
94-
inc_paths.append(readline_inc)
95-
if zlib_inc:
96-
inc_paths.append(zlib_inc)
97-
inc_flags = " ".join([f"-I{p}" for p in inc_paths if p])
59+
result_dir = f"{bbdir}/result"
60+
61+
def pkg_config(flag, pkg):
62+
r = subprocess.run(["pkg-config", flag, pkg], capture_output=True, text=True)
63+
return r.stdout.strip() if r.returncode == 0 else ""
64+
65+
readline_inc = pkg_config("--variable=includedir", "readline")
66+
readline_lib = pkg_config("--variable=libdir", "readline")
67+
zlib_lib = pkg_config("--variable=libdir", "zlib")
68+
69+
inc_flags = " ".join([
70+
f"-I{result_dir}/include",
71+
f"-I{build_dir}",
72+
f"-I{arch_dir}/src/csrc/include",
73+
f"-I{readline_inc}",
74+
])
9875

9976
# -DBBSIM: selects VBBSimHarness in bdb.h / main.cc
10077
cflags = f"{inc_flags} -DBBSIM -DTOP_NAME='\"V{topname}\"' -std=c++17"
10178

10279
ldflags = (
10380
f"-lreadline -ldramsim -lstdc++ -lz "
104-
f"-L{bbdir}/result/lib "
105-
f"-L{dramsim2_dir} "
81+
f"-L{result_dir}/lib "
82+
f"-L{readline_lib} -Wl,-rpath,{readline_lib} "
83+
f"-L{zlib_lib} -Wl,-rpath,{zlib_lib} "
10684
)
107-
if readline_lib:
108-
ldflags += f"-L{readline_lib} -Wl,-rpath,{readline_lib} "
109-
if zlib_lib:
110-
ldflags += f"-L{zlib_lib} -Wl,-rpath,{zlib_lib} "
11185

11286
obj_dir = f"{build_dir}/obj_dir"
11387
subprocess.run(f"rm -rf {obj_dir}", shell=True)

0 commit comments

Comments
 (0)