Support for command line arguments
authorSebastien Bourdeauducq <sebastien@milkymist.org>
Fri, 8 Feb 2013 21:23:58 +0000 (22:23 +0100)
committerSebastien Bourdeauducq <sebastien@milkymist.org>
Fri, 8 Feb 2013 21:23:58 +0000 (22:23 +0100)
mibuild/generic_platform.py
mibuild/xilinx_ise.py

index b78f2a4cda9cefee5d9f3d71e2c0d89d7c1dfcdc..2f8fd4aea331d838c00a06d875be596f3bf084a6 100644 (file)
@@ -1,5 +1,5 @@
 from copy import copy
-import os
+import os, argparse
 
 from migen.fhdl.structure import *
 from migen.corelogic.record import Record
@@ -228,3 +228,15 @@ class GenericPlatform:
                
        def build(self, fragment, clock_domains=None):
                raise NotImplementedError("GenericPlatform.build must be overloaded")
+
+       def add_arguments(self, parser):
+               pass # default: no arguments
+
+       def build_arg_ns(self, ns, *args, **kwargs):
+               self.build(*args, **kwargs)
+
+       def build_cmdline(self, *args, **kwargs):
+               parser = argparse.ArgumentParser(description="FPGA bitstream build system")
+               self.add_arguments(parser)
+               ns = parser.parse_args()
+               self.build_arg_ns(ns, *args, **kwargs)
index 0d887beca4e86f6ca0118e3d5ac1412eb9ef926a..4012ca40e58d99ecaa0fbabecdbcf8d88c76d8d9 100644 (file)
@@ -78,17 +78,17 @@ def _build_files(device, sources, named_sc, named_pc, build_name):
 -p %s""" % (build_name, build_name, device)
        tools.write_to_file(build_name + ".xst", xst_contents)
 
-def _run_ise(build_name, xilinx_install_path):
+def _run_ise(build_name, ise_path):
        def is_valid_version(v):
                try: 
                        Decimal(v)
-                       return os.path.isdir(os.path.join(xilinx_install_path, v))
+                       return os.path.isdir(os.path.join(ise_path, v))
                except:
                        return False
-       vers = [ver for ver in os.listdir(xilinx_install_path) if is_valid_version(ver)]
+       vers = [ver for ver in os.listdir(ise_path) if is_valid_version(ver)]
        tools_version = max(vers)
        bits = struct.calcsize("P")*8
-       xilinx_settings_file = '%s/%s/ISE_DS/settings%d.sh' % (xilinx_install_path, tools_version, bits) 
+       xilinx_settings_file = '%s/%s/ISE_DS/settings%d.sh' % (ise_path, tools_version, bits) 
 
        build_script_contents = """# Autogenerated by mibuild
 
@@ -110,7 +110,7 @@ bitgen -g Binary:Yes -w {build_name}-routed.ncd {build_name}.bit
 
 class XilinxISEPlatform(GenericPlatform):
        def build(self, fragment, clock_domains=None, build_dir="build", build_name="top",
-                       xilinx_install_path="/opt/Xilinx", run=True):
+                       ise_path="/opt/Xilinx", run=True):
                tools.mkdir_noerror(build_dir)
                os.chdir(build_dir)
 
@@ -120,6 +120,18 @@ class XilinxISEPlatform(GenericPlatform):
                sources = self.sources + [(v_file, "verilog")]
                _build_files(self.device, sources, named_sc, named_pc, build_name)
                if run:
-                       _run_ise(build_name, xilinx_install_path)
+                       _run_ise(build_name, ise_path)
                
                os.chdir("..")
+
+       def build_arg_ns(self, ns, *args, **kwargs):
+               for n in ["build_dir", "build_name", "ise_path"]:
+                       kwargs[n] = getattr(ns, n)
+               kwargs["run"] = not ns.no_run
+               self.build(*args, **kwargs)
+
+       def add_arguments(self, parser):
+               parser.add_argument("--build-dir", default="build", help="Set the directory in which to generate files and run ISE")
+               parser.add_argument("--build-name", default="top", help="Base name for the generated files")
+               parser.add_argument("--ise-path", default="/opt/Xilinx", help="ISE installation path (without version directory)")
+               parser.add_argument("--no-run", action="store_true", help="Only generate files, do not run ISE")