Debian Codegraph @

src/cocotb_tools/config.py

raw symbols SCIP
1 #!/usr/bin/env python
2 # Copyright cocotb contributors
3 # Copyright (c) 2013 Potential Ventures Ltd
4 # Copyright (c) 2013 SolarFlare Communications Inc
5 # Licensed under the Revised BSD License, see LICENSE for details.
6 # SPDX-License-Identifier: BSD-3-Clause
7
8 """
9 Module for querying the cocotb configuration
10
11 This module provides information in module global variables and through a
12 ``main()`` function that is used in the cocotb-config script.
13
14 Global variables:
15 share_dir: str, path where the cocotb data is stored
16 makefiles_dir: str, path where the cocotb makefiles are installed
17 libs_dir: str, path where the cocotb interface libraries are located
18 """
19
20 import argparse
21 import os
22 import sys
23 import textwrap
24 from pathlib import Path
25
26 import find_libpython
27
28 import cocotb_tools
29
30 base_tools_dir = Path(cocotb_tools.__file__).parent.resolve()
31 base_cocotb_dir = (base_tools_dir.parent / "cocotb").resolve()
32 if not (base_cocotb_dir.exists() and (base_cocotb_dir / "libs").exists()):
33 import cocotb
34
35 base_cocotb_dir = Path(cocotb.__file__).parent.resolve()
36
37 share_dir = base_cocotb_dir.joinpath("share")
38 libs_dir = base_cocotb_dir.joinpath("libs")
39 makefiles_dir = base_tools_dir.joinpath("makefiles")
40
41
42 def _get_version() -> str:
43 import cocotb # noqa: PLC0415
44
45 return cocotb.__version__
46
47
48 def _help_vars_text() -> str:
49 if "dev" in _get_version():
50 doclink = "https://docs.cocotb.org/en/development/library_reference.html"
51 else:
52 doclink = f"https://docs.cocotb.org/en/v{_get_version()}/library_reference.html"
53
54 # NOTE: make sure to keep "helpmsg" aligned with docs/source/library_reference.rst
55 helpmsg = textwrap.dedent(
56 """\
57 The following variables are environment variables:
58
59 cocotb
60 ------
61 COCOTB_TOPLEVEL Instance in the hierarchy to use as the DUT
62 COCOTB_RANDOM_SEED Random seed, to recreate a previous test stimulus
63 COCOTB_ANSI_OUTPUT Force cocotb to print or not print in color
64 COCOTB_REDUCED_LOG_FMT Display log lines shorter
65 COCOTB_ATTACH Pause time value in seconds before the simulator start
66 COCOTB_ENABLE_PROFILING Performance analysis of the Python portion of cocotb
67 COCOTB_LOG_LEVEL Default logging level (default INFO)
68 COCOTB_RESOLVE_X How to resolve X, Z, U, W, - on integer conversion
69 LIBPYTHON_LOC Absolute path to libpython
70
71 Regression Manager
72 ------------------
73 COCOTB_PDB_ON_EXCEPTION Drop into the Python debugger (pdb) on exception
74 COCOTB_TEST_MODULES Module(s) to search for test functions (comma-separated)
75 COCOTB_TESTCASE Test function(s) to run (comma-separated list)
76 COCOTB_RESULTS_FILE File name for xUnit XML tests results
77 COCOTB_USER_COVERAGE Collect Python user coverage (HDL for some simulators)
78 COVERAGE_RCFILE Configuration for user code coverage
79
80 GPI
81 ---
82 GPI_EXTRA Extra libraries to load at runtime (comma-separated)
83
84 Scheduler
85 ---------
86 COCOTB_SCHEDULER_DEBUG Enable additional output of coroutine scheduler
87 COCOTB_TRUST_INERTIAL_WRITES Trust inertial writes rather than mock them using scheduler
88
89 For details, see {}"""
90 ).format(doclink)
91 return helpmsg
92
93
94 def lib_name(interface: str, simulator: str) -> str:
95 """
96 Return the name of interface library for given interface (VPI/VHPI/FLI) and simulator.
97 """
98
99 interface_name = interface.lower()
100 supported_interfaces = ["vpi", "vhpi", "fli"]
101 if interface_name not in supported_interfaces:
102 raise ValueError(
103 "Wrong interface used. Supported: " + ", ".join(supported_interfaces)
104 )
105
106 simulator_name = simulator.lower()
107 supported_sims = [
108 "icarus",
109 "verilator",
110 "questa",
111 "modelsim",
112 "ius",
113 "xcelium",
114 "vcs",
115 "ghdl",
116 "riviera",
117 "activehdl",
118 "cvc",
119 "nvc",
120 "dsim",
121 ]
122 if simulator not in supported_sims:
123 raise ValueError(
124 "Wrong simulator name. Supported: " + ", ".join(supported_sims)
125 )
126
127 if simulator_name in ["questa", "cvc"]:
128 library_name = "modelsim"
129 elif simulator_name == "xcelium":
130 library_name = "ius"
131 elif simulator_name in ["riviera", "activehdl"]:
132 library_name = "aldec"
133 else:
134 library_name = simulator_name
135
136 if library_name == "icarus":
137 lib_ext = ""
138 elif os.name == "nt":
139 lib_ext = ".dll"
140 else:
141 lib_ext = ".so"
142
143 # check if compiled with msvc
144 if (libs_dir / "cocotb.dll").is_file():
145 lib_prefix = ""
146 else:
147 lib_prefix = "lib"
148
149 return lib_prefix + "cocotb" + interface_name + "_" + library_name + lib_ext
150
151
152 def lib_name_path(interface: str, simulator: str) -> Path:
153 """
154 Return the absolute path of interface library for given interface (VPI/VHPI/FLI) and simulator
155 """
156 return libs_dir / lib_name(interface, simulator)
157
158
159 def _get_parser() -> argparse.ArgumentParser:
160 parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
161
162 group = parser.add_mutually_exclusive_group()
163 group.add_argument(
164 "--share",
165 action="store_true",
166 help="Print the path to cocotb's share directory",
167 )
168 group.add_argument(
169 "--makefiles",
170 action="store_true",
171 help="Print the path to cocotb's makefile directory",
172 )
173 group.add_argument(
174 "--python-bin",
175 action="store_true",
176 help="Print the path to the Python executable associated with the environment that cocotb is installed in.",
177 )
178 group.add_argument(
179 "--help-vars",
180 action="store_true",
181 help="Print help about supported Makefile variables",
182 )
183 group.add_argument(
184 "--libpython",
185 action="store_true",
186 help="Print the absolute path to the libpython associated with the current Python installation",
187 )
188 group.add_argument(
189 "--lib-dir",
190 action="store_true",
191 help="Print the absolute path to the interface libraries location",
192 )
193 group.add_argument(
194 "--lib-name",
195 help="Print the name of interface library for given interface (VPI/VHPI/FLI) and simulator",
196 nargs=2,
197 metavar=("INTERFACE", "SIMULATOR"),
198 )
199 group.add_argument(
200 "--lib-name-path",
201 help="Print the absolute path of interface library for given interface (VPI/VHPI/FLI) and simulator",
202 nargs=2,
203 metavar=("INTERFACE", "SIMULATOR"),
204 )
205 group.add_argument(
206 "--version",
207 action="store_true",
208 help="Print the version of cocotb",
209 )
210
211 return parser
212
213
214 def main() -> None:
215 parser = _get_parser()
216 args = parser.parse_args()
217
218 if args.share:
219 print(share_dir.as_posix())
220 elif args.makefiles:
221 print(makefiles_dir.as_posix())
222 elif args.python_bin:
223 print(Path(sys.executable).as_posix())
224 elif args.help_vars:
225 print(_help_vars_text())
226 elif args.libpython:
227 libpython_path = find_libpython.find_libpython()
228 if libpython_path is None:
229 sys.exit(1)
230 print(Path(libpython_path).as_posix())
231 elif args.lib_dir:
232 print(libs_dir.as_posix())
233 elif args.lib_name:
234 print(lib_name(*args.lib_name))
235 elif args.lib_name_path:
236 print(lib_name_path(*args.lib_name_path).as_posix())
237 elif args.version:
238 print(_get_version())
239
240
241 if __name__ == "__main__":
242 main()