build.run: simplify using build products locally, e.g. for programming.
This commit is contained in:
parent
2763b403f1
commit
316ba10207
|
@ -1,7 +1,9 @@
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
|
from contextlib import contextmanager
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import tempfile
|
||||||
import zipfile
|
import zipfile
|
||||||
|
|
||||||
|
|
||||||
|
@ -60,3 +62,22 @@ class BuildProducts:
|
||||||
assert mode in "bt"
|
assert mode in "bt"
|
||||||
with open(os.path.join(self._root, filename), "r" + mode) as f:
|
with open(os.path.join(self._root, filename), "r" + mode) as f:
|
||||||
return f.read()
|
return f.read()
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def extract(self, *filenames):
|
||||||
|
files = []
|
||||||
|
try:
|
||||||
|
for filename in filenames:
|
||||||
|
file = tempfile.NamedTemporaryFile(prefix="nmigen_", suffix="_" + filename)
|
||||||
|
files.append(file)
|
||||||
|
file.write(self.get(filename))
|
||||||
|
|
||||||
|
if len(files) == 0:
|
||||||
|
return (yield)
|
||||||
|
elif len(files) == 1:
|
||||||
|
return (yield files[0].name)
|
||||||
|
else:
|
||||||
|
return (yield [file.name for file in files])
|
||||||
|
finally:
|
||||||
|
for file in files:
|
||||||
|
file.close()
|
||||||
|
|
27
nmigen/vendor/fpga/lattice_ice40.py
vendored
27
nmigen/vendor/fpga/lattice_ice40.py
vendored
|
@ -96,6 +96,7 @@ class LatticeICE40Platform(TemplatedPlatform):
|
||||||
""",
|
""",
|
||||||
r"""
|
r"""
|
||||||
{{get_tool("icepack")}}
|
{{get_tool("icepack")}}
|
||||||
|
{{verbose("-v")}}
|
||||||
{{name}}.asc
|
{{name}}.asc
|
||||||
{{name}}.bin
|
{{name}}.bin
|
||||||
"""
|
"""
|
||||||
|
@ -298,34 +299,24 @@ class IceStormProgrammerMixin:
|
||||||
"specify it using .build(..., program_opts={\"mode\": \"<mode>\"})"
|
"specify it using .build(..., program_opts={\"mode\": \"<mode>\"})"
|
||||||
.format(mode))
|
.format(mode))
|
||||||
|
|
||||||
iceprog = os.environ.get("ICEPROG", "iceprog")
|
iceprog = os.environ.get("ICEPROG", "iceprog")
|
||||||
bitstream = products.get("{}.bin".format(name))
|
|
||||||
if mode == "sram":
|
if mode == "sram":
|
||||||
options = ["-S"]
|
options = ["-S"]
|
||||||
if mode == "flash":
|
if mode == "flash":
|
||||||
options = []
|
options = []
|
||||||
with tempfile.NamedTemporaryFile(prefix="nmigen_iceprog_",
|
with products.extract("{}.bin".format(name)) as bitstream_filename:
|
||||||
suffix=".bin") as bitstream_file:
|
subprocess.run([iceprog, *options, bitstream_filename], check=True)
|
||||||
bitstream_file.write(bitstream)
|
|
||||||
subprocess.run([iceprog, *options, bitstream_file.name], check=True)
|
|
||||||
|
|
||||||
|
|
||||||
class IceBurnProgrammerMixin:
|
class IceBurnProgrammerMixin:
|
||||||
def toolchain_program(self, products, name):
|
def toolchain_program(self, products, name):
|
||||||
iceburn = os.environ.get("ICEBURN", "iCEburn")
|
iceburn = os.environ.get("ICEBURN", "iCEburn")
|
||||||
bitstream = products.get("{}.bin".format(name))
|
with products.extract("{}.bin".format(name)) as bitstream_filename:
|
||||||
with tempfile.NamedTemporaryFile(prefix="nmigen_iceburn_",
|
subprocess.run([iceburn, "-evw", bitstream_filename], check=True)
|
||||||
suffix=".bin") as bitstream_file:
|
|
||||||
bitstream_file.write(bitstream)
|
|
||||||
subprocess.run([iceburn, "-evw", bitstream_file.name], check=True)
|
|
||||||
|
|
||||||
|
|
||||||
class TinyProgrammerMixin:
|
class TinyProgrammerMixin:
|
||||||
def toolchain_program(self, products, name):
|
def toolchain_program(self, products, name):
|
||||||
tinyprog = os.environ.get("TINYPROG", "tinyprog")
|
tinyprog = os.environ.get("TINYPROG", "tinyprog")
|
||||||
options = ["-p"]
|
with products.extract("{}.bin".format(name)) as bitstream_filename:
|
||||||
bitstream = products.get("{}.bin".format(name))
|
subprocess.run([tinyprog, "-p", bitstream_filename], check=True)
|
||||||
with tempfile.NamedTemporaryFile(prefix="nmigen_tinyprog_",
|
|
||||||
suffix=".bin") as bitstream_file:
|
|
||||||
bitstream_file.write(bitstream)
|
|
||||||
subprocess.run([tinyprog, *options, bitstream_file.name], check=True)
|
|
||||||
|
|
Loading…
Reference in a new issue