From 1ce3271efe6474f2c122322cd4d89e64730bd037 Mon Sep 17 00:00:00 2001 From: Florent Kermarrec Date: Mon, 7 Nov 2022 13:16:24 +0100 Subject: [PATCH] build/parser: Add LiteXSoCArgumentParser compatibility and switch to it in integration/soc. Move things a bit to add target_group only when platform is set and avoid recursive imports. --- litex/build/parser.py | 64 ++++++++++++++++++++---------------- litex/soc/integration/soc.py | 28 ++-------------- 2 files changed, 38 insertions(+), 54 deletions(-) diff --git a/litex/build/parser.py b/litex/build/parser.py index 676462c10..1c00c798b 100644 --- a/litex/build/parser.py +++ b/litex/build/parser.py @@ -8,8 +8,9 @@ import argparse import importlib import sys -from litex.soc.integration.soc_core import * -from litex.soc.integration.builder import * +from litex.soc.cores import cpu +from litex.soc.integration import soc_core +from litex.soc.integration import builder # Litex Argument Parser ---------------------------------------------------------------------------- @@ -44,30 +45,15 @@ class LiteXArgumentParser(argparse.ArgumentParser): all arguments passed to argparse.ArgumentParser CTOR """ argparse.ArgumentParser.__init__(self, kwargs) - self._platform = platform - if platform is not None: - self._device = platform.device_family - toolchains = platform.toolchains(self._device) - self._default_toolchain = toolchains[0] - else: - self._device = None - toolchains = None - self._default_toolchain = None + self._platform = None + self._device = None + self.toolchains = None + self._default_toolchain = None self._args = None self._toolchain = None - - self._target_group = self.add_argument_group(title="Target options") - if toolchains is not None: - self.add_target_argument("--toolchain", - default = self._default_toolchain, - choices = toolchains, - help = "FPGA toolchain ({}).".format(" or ".join(toolchains))) - else: - self.add_target_argument("-toolchain", help="FPGA toolchain") - self.add_target_argument("--build", action="store_true", help="Build design.") - self.add_target_argument("--load", action="store_true", help="Load bitstream.") - builder_args(self) - soc_core_args(self) + if platform is not None: + self.set_platform(platform) + self.add_target_group() def set_platform(self, platform): """ set platform. Check first if not already set @@ -80,8 +66,9 @@ class LiteXArgumentParser(argparse.ArgumentParser): assert self._platform is None self._platform = platform self._device = platform.device_family - toolchains = platform.toolchains(self._device) - self._default_toolchain = toolchains[0] + self.toolchains = platform.toolchains(self._device) + self._default_toolchain = self.toolchains[0] + # add a setter (LitexArgumentParserInstance.platform = myPlatform) platform = property(None, set_platform) @@ -91,6 +78,20 @@ class LiteXArgumentParser(argparse.ArgumentParser): """ return self._target_group + def add_target_group(self): + """ create target group and add --toolchain/build/load args. + """ + self._target_group = self.add_argument_group(title="Target options") + if self.toolchains is not None: + self.add_target_argument("--toolchain", + default = self._default_toolchain, + choices = self.toolchains, + help = "FPGA toolchain ({}).".format(" or ".join(self.toolchains))) + else: + self.add_target_argument("-toolchain", help="FPGA toolchain") + self.add_target_argument("--build", action="store_true", help="Build design.") + self.add_target_argument("--load", action="store_true", help="Load bitstream.") + def add_target_argument(self, *args, **kwargs): """ wrapper to add argument to "Target options group" from outer of this class @@ -106,7 +107,7 @@ class LiteXArgumentParser(argparse.ArgumentParser): ====== builder arguments dict """ - return builder_argdict(self._args) + return builder.builder_argdict(self._args) @property def soc_argdict(self): @@ -117,7 +118,7 @@ class LiteXArgumentParser(argparse.ArgumentParser): ====== soc_core arguments dict """ - return soc_core_argdict(self._args) # FIXME: Rename to soc_argdict in the future. + return soc_core.soc_core_argdict(self._args) # FIXME: Rename to soc_argdict in the future. @property def toolchain_argdict(self): @@ -140,11 +141,18 @@ class LiteXArgumentParser(argparse.ArgumentParser): Checks first is platform is set: when platform is none: try to search for a platform argument """ + # When platform is None try to search for a user input if self._platform is None: platform = self.get_value_from_key("--platform", None) if platform is not None: self.set_platform(importlib.import_module(platform).Platform) + self.add_target_group() + + # When platform provided/set, set builder/soc_core args. + if self._platform is not None: + builder.builder_args(self) + soc_core.soc_core_args(self) # Intercept selected toolchain to fill arguments. if self._platform is not None: diff --git a/litex/soc/integration/soc.py b/litex/soc/integration/soc.py index e01fba996..ddbd85927 100755 --- a/litex/soc/integration/soc.py +++ b/litex/soc/integration/soc.py @@ -2096,30 +2096,6 @@ class LiteXSoC(SoC): # LiteXSoCArgumentParser --------------------------------------------------------------------------- -class LiteXSoCArgumentParser(argparse.ArgumentParser): - def parse_args(self): +from litex.build.parser import LiteXArgumentParser - # FIXME: Use 2 stages parser? - - def get_selected_cpu_name(): - for name, cpu_cls in cpu.CPUS.items(): - if f"--cpu-type={name}" in sys.argv: - return cpu_cls - if f"--cpu-type" in sys.argv: - if name in sys.argv: - return cpu_cls - return None - - # Intercept selected CPU to fill arguments. - cpu_cls = get_selected_cpu_name() - if cpu_cls is not None and hasattr(cpu_cls, "args_fill"): - cpu_cls.args_fill(self) - - # Get Command-line arguments. - args = argparse.ArgumentParser.parse_args(self) - - # Re-inject CPU read arguments. - if cpu_cls is not None and hasattr(cpu_cls, "args_read"): - cpu_cls.args_read(args) - - return args +class LiteXSoCArgumentParser(LiteXArgumentParser): pass # FIXME: Add compat and remove.