Loading files
This file is pinned to the selected release. Download the original to open it in your design tool.
4443d3b3aThis file is pinned to the selected release. Download the original to open it in your design tool.
#
# This file is part of LiteX.
#
# Copyright (c) 2026 Florent Kermarrec <florent@enjoy-digital.fr>
# SPDX-License-Identifier: BSD-2-Clause
import importlib
import json
import os
import sys
import tempfile
import types
import unittest
from unittest import mock
def _import_xilinx_f4pga_with_fakes():
class _FakeFlow:
def __init__(self, target, cfg, f4cache):
self.target = target
self.cfg = cfg
self.f4cache = f4cache
def print_resolved_dependencies(self, level):
self.level = level
def execute(self):
self.executed = True
class _FakeF4Cache:
def __init__(self, path):
self.path = path
def save(self):
self.saved = True
class _FakeProjectFlowConfig:
def __init__(self, path):
self.path = path
self.flow_cfg = None
def _make_flow_config(project_flow_config, partname):
return {
"flow_cfg": project_flow_config.flow_cfg,
"partname": partname,
}
def _set_verbosity_level(level):
pass
fake_modules = {
"f4pga": types.ModuleType("f4pga"),
"f4pga.flows": types.ModuleType("f4pga.flows"),
"f4pga.flows.flow": types.ModuleType("f4pga.flows.flow"),
"f4pga.flows.commands": types.ModuleType("f4pga.flows.commands"),
"f4pga.flows.common": types.ModuleType("f4pga.flows.common"),
"f4pga.flows.cache": types.ModuleType("f4pga.flows.cache"),
"f4pga.flows.flow_config": types.ModuleType("f4pga.flows.flow_config"),
}
fake_modules["f4pga"].__path__ = []
fake_modules["f4pga.flows"].__path__ = []
fake_modules["f4pga.flows.flow"].Flow = _FakeFlow
fake_modules["f4pga.flows.commands"].make_flow_config = _make_flow_config
fake_modules["f4pga.flows.common"].set_verbosity_level = _set_verbosity_level
fake_modules["f4pga.flows.cache"].F4Cache = _FakeF4Cache
fake_modules["f4pga.flows.flow_config"].ProjectFlowConfig = _FakeProjectFlowConfig
sys.modules.pop("litex.build.xilinx.f4pga", None)
with mock.patch.dict(sys.modules, fake_modules):
return importlib.import_module("litex.build.xilinx.f4pga")
class TestXilinxF4PGAToolchain(unittest.TestCase):
def setUp(self):
self.f4pga = _import_xilinx_f4pga_with_fakes()
self.addCleanup(sys.modules.pop, "litex.build.xilinx.f4pga", None)
def test_build_project_writes_rerunnable_flow_json(self):
toolchain = self.f4pga.F4PGAToolchain()
toolchain.platform = types.SimpleNamespace(
sources = [
("top.v", "verilog", None),
("extra.sv", "systemverilog", None),
("copied.v", "verilog", None, True), # copy=True sources are 4-tuples.
("ignore.vhd", "vhdl", None),
],
)
toolchain._build_name = "top"
toolchain._partname = "xc7a35tcsg324-1"
with tempfile.TemporaryDirectory() as tmp_dir:
cwd = os.getcwd()
os.chdir(tmp_dir)
try:
toolchain.build_project()
with open("flow.json") as f:
flow = json.load(f)
finally:
os.chdir(cwd)
self.assertEqual(flow["default_part"], "xc7a35tcsg324-1")
self.assertEqual(flow["xc7a35tcsg324-1"]["default_target"], "bitstream")
self.assertEqual(flow["dependencies"]["sources"], ["top.v", "extra.sv", "copied.v"])
self.assertEqual(flow["dependencies"]["xdc"], "top.xdc")
# No .sdc is generated by the toolchain, so it must not be declared as a dependency.
self.assertNotIn("sdc", flow["dependencies"])
self.assertEqual(flow["values"]["top"], "top")
self.assertEqual(flow["values"]["part_name"], "xc7a35tcsg324-1")
self.assertEqual(toolchain._flow.target, "bitstream")
self.assertEqual(toolchain._flow.cfg["partname"], "xc7a35tcsg324-1")
def test_build_script_writes_f4pga_rerun_wrapper(self):
toolchain = self.f4pga.F4PGAToolchain()
toolchain._build_name = "top"
with tempfile.TemporaryDirectory() as tmp_dir:
cwd = os.getcwd()
os.chdir(tmp_dir)
try:
script = toolchain.build_script()
with open(script) as f:
contents = f.read()
finally:
os.chdir(cwd)
self.assertEqual(script, "build_top.sh")
self.assertIn("set -e\n", contents)
self.assertIn("f4pga -vvv build -f flow.json\n", contents)
if __name__ == "__main__":
unittest.main()