|
| 1 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +# License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 3 | +# You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | +# |
| 5 | +# Copyright (c) 2015, Lars Asplund lars.anders.asplund@gmail.com |
| 6 | + |
| 7 | +""" |
| 8 | +Create simulator instances |
| 9 | +""" |
| 10 | + |
| 11 | +from vunit.modelsim_interface import ModelSimInterface |
| 12 | +from os.path import join, exists |
| 13 | +import os |
| 14 | + |
| 15 | + |
| 16 | +class SimulatorFactory(object): |
| 17 | + """ |
| 18 | + Create simulator instances |
| 19 | + """ |
| 20 | + |
| 21 | + @staticmethod |
| 22 | + def supported_simulators(): |
| 23 | + """ |
| 24 | + Return a list of supported simulator classes |
| 25 | + """ |
| 26 | + return [ModelSimInterface] |
| 27 | + |
| 28 | + @classmethod |
| 29 | + def available_simulators(cls): |
| 30 | + """ |
| 31 | + Return a list of available simulators |
| 32 | + """ |
| 33 | + return [simulator_class |
| 34 | + for simulator_class in cls.supported_simulators() |
| 35 | + if simulator_class.is_available()] |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def select_simulator(cls): |
| 39 | + """ |
| 40 | + Select simulator class, either from VUNIT_SIMULATOR environment variable |
| 41 | + or the first available |
| 42 | + """ |
| 43 | + environ_name = "VUNIT_SIMULATOR" |
| 44 | + |
| 45 | + available_simulators = cls.available_simulators() |
| 46 | + name_mapping = {simulator_class.name: simulator_class for simulator_class in cls.supported_simulators()} |
| 47 | + if len(available_simulators) == 0: |
| 48 | + raise RuntimeError("No available simulator detected. " |
| 49 | + "Simulator executables must be available in PATH environment variable.") |
| 50 | + |
| 51 | + if environ_name in os.environ: |
| 52 | + simulator_name = os.environ[environ_name] |
| 53 | + if simulator_name not in name_mapping: |
| 54 | + raise RuntimeError( |
| 55 | + ("Simulator from " + environ_name + " environment variable %r is not supported. " |
| 56 | + "Supported simulators are %r") |
| 57 | + % (simulator_name, name_mapping.keys())) |
| 58 | + simulator_class = name_mapping[simulator_name] |
| 59 | + else: |
| 60 | + simulator_class = available_simulators[0] |
| 61 | + |
| 62 | + return simulator_class |
| 63 | + |
| 64 | + @classmethod |
| 65 | + def add_arguments(cls, parser): |
| 66 | + """ |
| 67 | + Add command line arguments to parser |
| 68 | + """ |
| 69 | + cls.select_simulator().add_arguments(parser) |
| 70 | + |
| 71 | + def __init__(self, args): |
| 72 | + self._args = args |
| 73 | + self._output_path = args.output_path |
| 74 | + self._simulator_class = self.select_simulator() |
| 75 | + |
| 76 | + @property |
| 77 | + def simulator_name(self): |
| 78 | + return self._simulator_class.name |
| 79 | + |
| 80 | + @property |
| 81 | + def simulator_output_path(self): |
| 82 | + return join(self._output_path, self.simulator_name) |
| 83 | + |
| 84 | + def create(self): |
| 85 | + """ |
| 86 | + Create new simulator instance |
| 87 | + """ |
| 88 | + if not exists(self.simulator_output_path): |
| 89 | + os.makedirs(self.simulator_output_path) |
| 90 | + |
| 91 | + return self._simulator_class.from_args(self.simulator_output_path, |
| 92 | + self._args) |
0 commit comments