From 24125deab871ecf8601d62c24afb86849bdafaa8 Mon Sep 17 00:00:00 2001 From: Lenny Truong Date: Tue, 14 May 2019 10:51:07 -0700 Subject: [PATCH 1/2] Add config option for callee_file_dir --- fault/config.py | 16 ++++++++++++++++ fault/tester.py | 38 ++++++++++++++++++++++++++++++++++++-- tests/build/.gitignore | 2 ++ tests/test_config.py | 25 +++++++++++++++++++++++++ 4 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 fault/config.py create mode 100644 tests/build/.gitignore create mode 100644 tests/test_config.py diff --git a/fault/config.py b/fault/config.py new file mode 100644 index 00000000..b828480d --- /dev/null +++ b/fault/config.py @@ -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 diff --git a/fault/tester.py b/fault/tester.py index 328223f8..0403d14f 100644 --- a/fault/tester.py +++ b/fault/tester.py @@ -11,6 +11,10 @@ from fault.wrapper import CircuitWrapper, PortWrapper, InstanceWrapper from fault.file import File import copy +import os +import inspect +from pathlib import Path +from fault.config import get_test_dir class Tester: @@ -138,7 +142,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. @@ -150,6 +172,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 @@ -168,7 +200,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): diff --git a/tests/build/.gitignore b/tests/build/.gitignore new file mode 100644 index 00000000..d6b7ef32 --- /dev/null +++ b/tests/build/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/tests/test_config.py b/tests/test_config.py new file mode 100644 index 00000000..b9ea3a65 --- /dev/null +++ b/tests/test_config.py @@ -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" From cffca8318f9054d10cc16b1381875a26843260dc Mon Sep 17 00:00:00 2001 From: Lenny Truong Date: Tue, 14 May 2019 10:53:21 -0700 Subject: [PATCH 2/2] Remove unused import --- fault/tester.py | 1 - 1 file changed, 1 deletion(-) diff --git a/fault/tester.py b/fault/tester.py index 0403d14f..d0bdb5fa 100644 --- a/fault/tester.py +++ b/fault/tester.py @@ -13,7 +13,6 @@ import copy import os import inspect -from pathlib import Path from fault.config import get_test_dir