Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: m-labs/nmigen
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: c89c2ce941de
Choose a base ref
...
head repository: m-labs/nmigen
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 3194b5c90ba8
Choose a head ref
  • 1 commit
  • 2 files changed
  • 1 contributor

Commits on Jun 4, 2019

  1. build.run: extract from build.plat.

    whitequark committed Jun 4, 2019
    Copy the full SHA
    3194b5c View commit details
Showing with 64 additions and 58 deletions.
  1. +2 −58 nmigen/build/plat.py
  2. +62 −0 nmigen/build/run.py
60 changes: 2 additions & 58 deletions nmigen/build/plat.py
Original file line number Diff line number Diff line change
@@ -1,78 +1,22 @@
from collections import OrderedDict
from abc import ABCMeta, abstractmethod, abstractproperty
import os
import sys
import subprocess
import textwrap
import re
import zipfile
import jinja2

from .. import __version__
from ..hdl.ast import *
from ..hdl.dsl import *
from ..hdl.ir import *
from ..back import rtlil, verilog
from .res import ConstraintManager
from .res import *
from .run import *


__all__ = ["Platform", "TemplatedPlatform"]


class BuildPlan:
def __init__(self, script):
self.script = script
self.files = OrderedDict()

def add_file(self, filename, content):
assert isinstance(filename, str) and filename not in self.files
# Just to make sure we don't accidentally overwrite anything.
assert not os.path.normpath(filename).startswith("..")
self.files[filename] = content

def execute(self, root="build", run_script=True):
os.makedirs(root, exist_ok=True)
cwd = os.getcwd()
try:
os.chdir(root)

for filename, content in self.files.items():
dirname = os.path.dirname(filename)
if dirname:
os.makedirs(dirname, exist_ok=True)

mode = "wt" if isinstance(content, str) else "wb"
with open(filename, mode) as f:
f.write(content)

if run_script:
if sys.platform.startswith("win32"):
subprocess.run(["cmd", "/c", "{}.bat".format(self.script)], check=True)
else:
subprocess.run(["sh", "{}.sh".format(self.script)], check=True)

return BuildProducts(os.getcwd())

finally:
os.chdir(cwd)

def archive(self, file):
with zipfile.ZipFile(file, "w") as archive:
# Write archive members in deterministic order and with deterministic timestamp.
for filename in sorted(self.files):
archive.writestr(zipfile.ZipInfo(filename), self.files[filename])


class BuildProducts:
def __init__(self, root):
self._root = root

def get(self, filename, mode="b"):
assert mode in "bt"
with open(os.path.join(self._root, filename), "r" + mode) as f:
return f.read()


class Platform(ConstraintManager, metaclass=ABCMeta):
resources = abstractproperty()
connectors = abstractproperty()
62 changes: 62 additions & 0 deletions nmigen/build/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from collections import OrderedDict
import os
import sys
import subprocess
import zipfile


__all__ = ["BuildPlan", "BuildProducts"]


class BuildPlan:
def __init__(self, script):
self.script = script
self.files = OrderedDict()

def add_file(self, filename, content):
assert isinstance(filename, str) and filename not in self.files
# Just to make sure we don't accidentally overwrite anything.
assert not os.path.normpath(filename).startswith("..")
self.files[filename] = content

def execute(self, root="build", run_script=True):
os.makedirs(root, exist_ok=True)
cwd = os.getcwd()
try:
os.chdir(root)

for filename, content in self.files.items():
dirname = os.path.dirname(filename)
if dirname:
os.makedirs(dirname, exist_ok=True)

mode = "wt" if isinstance(content, str) else "wb"
with open(filename, mode) as f:
f.write(content)

if run_script:
if sys.platform.startswith("win32"):
subprocess.run(["cmd", "/c", "{}.bat".format(self.script)], check=True)
else:
subprocess.run(["sh", "{}.sh".format(self.script)], check=True)

return BuildProducts(os.getcwd())

finally:
os.chdir(cwd)

def archive(self, file):
with zipfile.ZipFile(file, "w") as archive:
# Write archive members in deterministic order and with deterministic timestamp.
for filename in sorted(self.files):
archive.writestr(zipfile.ZipInfo(filename), self.files[filename])


class BuildProducts:
def __init__(self, root):
self._root = root

def get(self, filename, mode="b"):
assert mode in "bt"
with open(os.path.join(self._root, filename), "r" + mode) as f:
return f.read()