mibuild.xilinx_vivado: support settingsXX.sh
authorRobert Jordens <jordens@gmail.com>
Mon, 28 Jul 2014 01:30:08 +0000 (19:30 -0600)
committerSebastien Bourdeauducq <sb@m-labs.hk>
Mon, 28 Jul 2014 01:50:15 +0000 (19:50 -0600)
* in the process refactor the version search, the architecture bit width
 detection, the settings search and all also for xilinx_ise
* use distutils.version.StrictVersion

mibuild/tools.py
mibuild/xilinx_ise.py
mibuild/xilinx_tools.py [new file with mode: 0644]
mibuild/xilinx_vivado.py

index 35efca74b8275f4fddf1a94011dae3d74eb50e56..eb63fb4d2e05ef4a4e1d03e15a6bbf7048238462 100644 (file)
@@ -1,4 +1,5 @@
-import os
+import os, struct
+from distutils.version import StrictVersion
 
 def mkdir_noerror(d):
        try:
@@ -21,3 +22,16 @@ def write_to_file(filename, contents, force_unix=False):
        f = open(filename, "w", newline=newline)
        f.write(contents)
        f.close()
+
+def arch_bits():
+       return struct.calcsize("P")*8
+
+def versions(path):
+       for n in os.listdir(path):
+               full = os.path.join(path, n)
+               if not os.path.isdir(full):
+                       continue
+               try:
+                       yield StrictVersion(n)
+               except ValueError:
+                       continue
index 56bd414ae821ad9a5cca5ead731104719e742ab7..12a5862dba48989df97bbcaa6707996d8148c7e9 100644 (file)
@@ -1,5 +1,4 @@
-import os, struct, subprocess, sys
-from decimal import Decimal
+import os, subprocess, sys
 
 from migen.fhdl.std import *
 from migen.fhdl.specials import SynthesisDirective
@@ -7,7 +6,7 @@ from migen.genlib.cdc import *
 from migen.fhdl.structure import _Fragment
 
 from mibuild.generic_platform import *
-from mibuild import tools
+from mibuild import tools, xilinx_tools
 
 def _format_constraint(c):
        if isinstance(c, Pins):
@@ -89,34 +88,21 @@ synth_xilinx -arch {arch} -top top -edif {build_name}.edif""".format(arch=arch,
        if r != 0:
                raise OSError("Subprocess failed")
 
-def _is_valid_version(path, v):
-       try: 
-               Decimal(v)
-               return os.path.isdir(os.path.join(path, v))
-       except:
-               return False
-
 def _run_ise(build_name, ise_path, source, mode, ngdbuild_opt,
-               bitgen_opt, ise_commands, map_opt, par_opt):
+               bitgen_opt, ise_commands, map_opt, par_opt, ver=None):
        if sys.platform == "win32" or sys.platform == "cygwin":
                source = False
        build_script_contents = "# Autogenerated by mibuild\nset -e\n"
        if source:
-               vers = [ver for ver in os.listdir(ise_path) if _is_valid_version(ise_path, ver)]
-               tools_version = max(vers)
-               bits = struct.calcsize("P")*8
-               
-               xilinx_settings_file = os.path.join(ise_path, tools_version, "ISE_DS", "settings{0}.sh".format(bits))
-               if not os.path.exists(xilinx_settings_file) and bits == 64:
-                       # if we are on 64-bit system but the toolchain isn't, try the 32-bit env.
-                       xilinx_settings_file = os.path.join(ise_path, tools_version, "ISE_DS", "settings32.sh")
-               build_script_contents += "source " + xilinx_settings_file + "\n"
+               settings = xilinx_tools.settings(ise_path, ver, "ISE_DS")
+               build_script_contents += "source " + settings + "\n"
        if mode == "edif":
                ext = "edif"
        else:
                ext = "ngc"
                build_script_contents += """
-xst -ifn {build_name}.xst"""
+xst -ifn {build_name}.xst
+"""
 
        build_script_contents += """
 ngdbuild {ngdbuild_opt} -uc {build_name}.ucf {build_name}.{ext} {build_name}.ngd
diff --git a/mibuild/xilinx_tools.py b/mibuild/xilinx_tools.py
new file mode 100644 (file)
index 0000000..98c2e21
--- /dev/null
@@ -0,0 +1,27 @@
+import os
+from distutils.version import StrictVersion
+
+from mibuild import tools
+
+def settings(path, ver=None, sub=None):
+       vers = list(tools.versions(path))
+       if ver is None:
+               ver = max(vers)
+       else:
+               ver = StrictVersion(ver)
+               assert ver in vers
+
+       full = os.path.join(path, str(ver))
+       if sub:
+               full = os.path.join(full, sub)
+
+       search = [64, 32]
+       if tools.arch_bits() == 32:
+               search.reverse()
+
+       for b in search:
+               settings = os.path.join(full, "settings{0}.sh".format(b))
+               if os.path.exists(settings):
+                       return settings
+
+       raise ValueError("no settings file found")
index 7149da5edbe87eeb7e6affe20f5de5330a42ba63..c229ba17b799eb420c86e51eb6c884e8a571298f 100644 (file)
@@ -7,7 +7,7 @@ from migen.fhdl.std import *
 from migen.fhdl.structure import _Fragment
 
 from mibuild.generic_platform import *
-from mibuild import tools
+from mibuild import tools, xilinx_tools
 
 def _format_constraint(c):
        if isinstance(c, Pins):
@@ -56,15 +56,14 @@ def _build_files(device, sources, vincpaths, build_name):
        tcl_contents += "quit\n"        
        tools.write_to_file(build_name + ".tcl", tcl_contents)
 
-def _run_vivado(build_name, vivado_path, source):
+def _run_vivado(build_name, vivado_path, source, ver=None):
        if sys.platform == "win32" or sys.platform == "cygwin":
                source = False
        build_script_contents = "# Autogenerated by mibuild\nset -e\n"
        if source:
-               raise NotImplementedError
-       build_script_contents += """
-vivado -mode tcl -source {build_name}.tcl
-""".format(build_name=build_name)
+               settings = xilinx_tools.settings(vivado_path, ver)
+               build_script_contents += "source " + settings + "\n"
+       build_script_contents += "vivado -mode tcl -source " + build_name + ".tcl\n"
        build_script_file = "build_" + build_name + ".sh"
        tools.write_to_file(build_script_file, build_script_contents, force_unix=True)
 
@@ -80,7 +79,7 @@ class XilinxVivadoPlatform(GenericPlatform):
                return GenericPlatform.get_verilog(self, *args, special_overrides=so, **kwargs)
 
        def build(self, fragment, build_dir="build", build_name="top",
-                       vivado_path="/opt/Xilinx", source=True, run=True):
+                       vivado_path="/opt/Xilinx/Vivado", source=True, run=True):
                tools.mkdir_noerror(build_dir)
                os.chdir(build_dir)