Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config option for callee_file_dir #96

Merged
merged 2 commits into from
May 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions fault/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
__TEST_DIR = 'normal'


def set_test_dir(target):
"""
Set to 'callee_file_dir' to have the `directory` parameter to
`compile_and_run` relative to the calling file (default is relative to
where Python is invoked)
"""
global __TEST_DIR
assert target in ['normal', 'callee_file_dir']
__TEST_DIR = target


def get_test_dir():
return __TEST_DIR
37 changes: 35 additions & 2 deletions fault/tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from fault.wrapper import CircuitWrapper, PortWrapper, InstanceWrapper
from fault.file import File
import copy
import os
import inspect
from fault.config import get_test_dir


class Tester:
Expand Down Expand Up @@ -138,7 +141,25 @@ def serialize(self):
builder.process(action)
return builder.vectors

def compile(self, target="verilator", **kwargs):
def _make_directory(self, directory):
"""
Handles support for `set_test_dir('callee_file_dir')`

When configured in this mode, fault will generate the test
collateral treating `directory` as relative to the file calling
`compile_and_run` or `compile`.

The default behvaior is to generate the collateral relative to where
Python is invoked.
"""
if get_test_dir() == 'callee_file_dir':
(_, filename, _, _, _, _) = inspect.getouterframes(
inspect.currentframe())[2]
file_path = os.path.abspath(os.path.dirname(filename))
directory = os.path.join(file_path, directory)
return directory

def _compile(self, target="verilator", **kwargs):
"""
Create an instance of the target backend.

Expand All @@ -150,6 +171,16 @@ def compile(self, target="verilator", **kwargs):
"""
self.targets[target] = self.make_target(target, **kwargs)

def compile(self, target="verilator", **kwargs):
"""
Logic deferred to `_compile` method, one level of indirection in order
to avoid calling `_make_directory` twice in `compile` and
`compile_and_run`
"""
if "directory" in kwargs:
kwargs["directory"] = self._make_directory(kwargs["directory"])
self._compile(target, directory, **kwargs)

def run(self, target="verilator"):
"""
Run the current action sequence using the specified `target`. The user
Expand All @@ -168,7 +199,9 @@ def compile_and_run(self, target="verilator", **kwargs):
"""
Compile and run the current action sequence using `target`
"""
self.compile(target, **kwargs)
if "directory" in kwargs:
kwargs["directory"] = self._make_directory(kwargs["directory"])
self._compile(target, **kwargs)
self.run(target)

def retarget(self, new_circuit, clock=None):
Expand Down
2 changes: 2 additions & 0 deletions tests/build/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
25 changes: 25 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import fault
import common
import os
import fault.config


def test_config_test_dir():
file_dir = os.path.dirname(__file__)
harness_file = os.path.join(file_dir, "build/TupleCircuit_driver.cpp")
# Remove harness if it exists to ensure that it's recreated properly
if os.path.isfile(harness_file):
os.remove(harness_file)
fault.config.set_test_dir('callee_file_dir')
circ = common.TestTupleCircuit
tester = fault.Tester(circ)
tester.circuit.I.a = 5
tester.circuit.I.b = 11
tester.eval()
tester.circuit.O.a.expect(5)
tester.circuit.O.b.expect(11)
tester.compile_and_run("verilator", directory="build",
flags=['-Wno-fatal'])
fault.config.set_test_dir('normal')
assert os.path.isfile(harness_file), \
"Verilator harness not created relative to current file"