Skip to content

Commit 7901cdc

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

2 files changed

Lines changed: 37 additions & 49 deletions

File tree

api/steps/verilator/02_verilog_event_step.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@ async def handler(data, context):
7070
if os.path.exists(topname_file):
7171
os.remove(topname_file)
7272

73+
# Patch fesvr includes out of mm.h and mm.cc (copied from testchipip resources).
74+
# They reference fesvr/memif.h which we don't have — our SimDRAM_bb.cc doesn't use it.
75+
for patch_file in [f"{build_dir}/mm.h", f"{build_dir}/mm.cc"]:
76+
if os.path.exists(patch_file):
77+
with open(patch_file, "r") as f:
78+
content = f.read()
79+
patched = "\n".join(
80+
line for line in content.splitlines()
81+
if "fesvr/memif.h" not in line and "fesvr/elfloader.h" not in line
82+
)
83+
if patched != content:
84+
with open(patch_file, "w") as f:
85+
f.write(patched)
86+
context.logger.info(f"Patched fesvr includes from {patch_file}")
87+
7388
# ==================================================================================
7489
# Return result to API
7590
# ==================================================================================

api/steps/verilator/03_build_event_step.py

Lines changed: 22 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -46,68 +46,41 @@ 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 sources — they depend on fesvr which we don't have
50+
_tsi_htif = {"testchip_tsi.cc", "testchip_htif.cc", "SimTSI.cc"}
51+
csrcs = [f for f in csrcs if os.path.basename(f) not in _tsi_htif]
6652

6753
topname = "BBSimHarness"
6854

6955
# ==================================================================================
7056
# Build flags
7157
# ==================================================================================
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])
58+
result_dir = f"{bbdir}/result"
59+
60+
def pkg_config(flag, pkg):
61+
r = subprocess.run(["pkg-config", flag, pkg], capture_output=True, text=True)
62+
return r.stdout.strip() if r.returncode == 0 else ""
63+
64+
readline_inc = pkg_config("--variable=includedir", "readline")
65+
readline_lib = pkg_config("--variable=libdir", "readline")
66+
zlib_lib = pkg_config("--variable=libdir", "zlib")
67+
68+
inc_flags = " ".join([
69+
f"-I{result_dir}/include",
70+
f"-I{build_dir}",
71+
f"-I{arch_dir}/src/csrc/include",
72+
f"-I{readline_inc}",
73+
])
9874

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

10278
ldflags = (
10379
f"-lreadline -ldramsim -lstdc++ -lz "
104-
f"-L{bbdir}/result/lib "
105-
f"-L{dramsim2_dir} "
80+
f"-L{result_dir}/lib "
81+
f"-L{readline_lib} -Wl,-rpath,{readline_lib} "
82+
f"-L{zlib_lib} -Wl,-rpath,{zlib_lib} "
10683
)
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} "
11184

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

0 commit comments

Comments
 (0)