1
0
Fork 0
mirror of https://git.sr.ht/~kivikakk/niar synced 2024-12-22 23:52:25 +00:00

support zig builds.

This commit is contained in:
Asherah Connor 2024-06-16 11:44:18 +03:00
parent 071f1cef41
commit a4b04502b6
2 changed files with 46 additions and 10 deletions

View file

@ -1,5 +1,7 @@
import json
import logging
import os
import shutil
import subprocess
from enum import Enum
from functools import partial
@ -31,13 +33,19 @@ CXXFLAGS = [
class _Optimize(Enum):
none = "none"
rtl = "rtl"
app = "app"
both = "both"
def __str__(self):
return self.value
@property
def opt_rtl(self) -> bool:
return self in (self.rtl,)
return self in (self.rtl, self.both)
@property
def opt_app(self) -> bool:
return self in (self.app, self.both)
def add_arguments(np: Project, parser):
@ -86,6 +94,8 @@ def add_arguments(np: Project, parser):
def main(np: Project, args):
yosys = find_yosys(lambda ver: ver >= (0, 10))
os.makedirs(np.path.build(), exist_ok=True)
platform = np.cxxrtl_target_by_name(args.target)
design = construct_top(np, platform)
@ -113,6 +123,11 @@ def main(np: Project, args):
*(["-O3"] if args.optimize.opt_rtl else ["-O0"]),
*(["-g"] if args.debug else []),
]
if platform.uses_zig:
cxxflags += [
"-DCXXRTL_INCLUDE_CAPI_IMPL",
"-DCXXRTL_INCLUDE_VCD_CAPI_IMPL",
]
procs = []
compile_commands = {}
@ -128,6 +143,8 @@ def main(np: Project, args):
"-o",
str(o_path),
]
if platform.uses_zig:
cmd = ["zig"] + cmd
compile_commands[o_path] = cmd
logger.debug(" ".join(str(e) for e in cmd))
procs.append((cc_path, subprocess.Popen(cmd)))
@ -157,6 +174,24 @@ def main(np: Project, args):
raise RuntimeError("failed compile step")
exe_o_path = np.path.build("cxxrtl")
if platform.uses_zig:
# Zig really wants relative paths.
joined_o_paths = ",".join(
f"../{p.relative_to(np.path())}" for p in cc_o_paths.values()
)
cmd = [
"zig",
"build",
f"-Dclock_hz={int(platform.default_clk_frequency)}",
f"-Dyosys_data_dir={yosys.data_dir()}",
f"-Dcxxrtl_o_paths={joined_o_paths}",
]
if args.optimize.opt_app:
cmd += ["-Doptimize=ReleaseFast"]
logger.debug(" ".join(str(e) for e in cmd))
subprocess.run(cmd, cwd="cxxrtl", check=True)
shutil.copy("cxxrtl/zig-out/bin/cxxrtl", exe_o_path)
else:
cmd = [
"c++",
*cxxflags,

View file

@ -5,3 +5,4 @@ __all__ = ["CxxrtlPlatform"]
class CxxrtlPlatform(metaclass=ABCMeta):
default_clk_frequency = property(abstractmethod(lambda _: None))
uses_zig = False