Skip to content

Commit 92dfdc5

Browse files
committed
Added support for adding external libraries by file name.
Also included libraries when calling Riviera-PRO vsim (using the -L option)
1 parent 126f145 commit 92dfdc5

File tree

6 files changed

+42
-21
lines changed

6 files changed

+42
-21
lines changed

docs/news.d/1073.bugfix.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Added support for adding external Riviera-PRO libraries by library file rather than library directory.
2+
This is needed when the name of the library directory differs from that of the library file.
3+
4+
Included all libraries in the ``vsim`` call using the ``-L`` option.

tests/unit/test_project.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,16 +1915,7 @@ def test_add_external_library_must_exist(self):
19151915
try:
19161916
self.project.add_library("lib2", "lib_path2", is_external=True)
19171917
except ValueError as err:
1918-
self.assertEqual(str(err), "External library 'lib_path2' does not exist")
1919-
else:
1920-
assert False, "ValueError not raised"
1921-
1922-
def test_add_external_library_must_be_a_directory(self):
1923-
write_file("lib_path3", "")
1924-
try:
1925-
self.project.add_library("lib3", "lib_path3", is_external=True)
1926-
except ValueError as err:
1927-
self.assertEqual(str(err), "External library must be a directory. Got 'lib_path3'")
1918+
self.assertEqual(str(err), "External library lib_path2 does not exist")
19281919
else:
19291920
assert False, "ValueError not raised"
19301921

vunit/library.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"""
1212

1313
import logging
14+
from typing import Optional
1415
from vunit.vhdl_standard import VHDLStandard
1516

1617
LOGGER = logging.getLogger(__name__)
@@ -21,9 +22,18 @@ class Library(object): # pylint: disable=too-many-instance-attributes
2122
Represents a VHDL library
2223
"""
2324

24-
def __init__(self, name: str, directory: str, vhdl_standard: VHDLStandard, is_external=False):
25+
def __init__(
26+
self,
27+
name: str,
28+
directory: str,
29+
vhdl_standard: VHDLStandard,
30+
is_external=False,
31+
*,
32+
file_name: Optional[str] = None,
33+
):
2534
self.name = name
2635
self.directory = directory
36+
self.file_name = file_name
2737

2838
# Default VHDL standard for files added unless explicitly set per file
2939
self.vhdl_standard = vhdl_standard

vunit/project.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ def add_library(
8686
directory: Union[str, Path],
8787
vhdl_standard: VHDLStandard = VHDL.STD_2008,
8888
is_external=False,
89+
*,
90+
file_name: Optional[str] = None,
8991
):
9092
"""
9193
Add library to project with logical_name located or to be located in directory
@@ -96,15 +98,14 @@ def add_library(
9698
dpath = Path(directory)
9799
dstr = str(directory)
98100

99-
if is_external:
100-
if not dpath.exists():
101-
raise ValueError(f"External library {dstr!r} does not exist")
101+
full_path = dpath / file_name if file_name is not None else dpath
102102

103-
if not dpath.is_dir():
104-
raise ValueError(f"External library must be a directory. Got {dstr!r}")
103+
if is_external:
104+
if not full_path.exists():
105+
raise ValueError(f"External library {full_path} does not exist")
105106

106-
library = Library(logical_name, dstr, vhdl_standard, is_external=is_external)
107-
LOGGER.debug("Adding library %s with path %s", logical_name, dstr)
107+
library = Library(logical_name, dstr, vhdl_standard, is_external=is_external, file_name=file_name)
108+
LOGGER.debug("Adding library %s with path %s", logical_name, full_path)
108109

109110
self._libraries[logical_name] = library
110111
self._lower_library_names_dict[logical_name.lower()] = library.name

vunit/sim_if/rivierapro.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ def setup_library_mapping(self, project):
145145
mapped_libraries = self._get_mapped_libraries(self._sim_cfg_file_name)
146146
for library in project.get_libraries():
147147
self._libraries.append(library)
148-
self.create_library(library.name, library.directory, mapped_libraries)
148+
path = (
149+
str(Path(library.directory) / library.file_name) if library.file_name is not None else library.directory
150+
)
151+
self.create_library(library.name, path, mapped_libraries)
149152

150153
def compile_source_file_command(self, source_file):
151154
"""
@@ -310,6 +313,9 @@ def _create_load_function(
310313

311314
vsim_flags += ["-lib", config.library_name]
312315

316+
for library in self._libraries:
317+
vsim_flags += ["-L", library.name]
318+
313319
if config.vhdl_configuration_name is None:
314320
# Add the the testbench top-level unit last as coverage is
315321
# only collected for the top-level unit specified last

vunit/ui/__init__.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def add_external_library(self, library_name, path: Union[str, Path], vhdl_standa
232232
Add an externally compiled library as a black-box
233233
234234
:param library_name: The name of the external library
235-
:param path: The path to the external library directory
235+
:param path: The path to the external library file or directory
236236
:param vhdl_standard: The VHDL standard used to compile files,
237237
if None the VUNIT_VHDL_STANDARD environment variable is used
238238
:returns: The created :class:`.Library` object
@@ -245,11 +245,20 @@ def add_external_library(self, library_name, path: Union[str, Path], vhdl_standa
245245
246246
"""
247247

248+
resolved_path = Path(path).resolve()
249+
if resolved_path.is_file():
250+
directory = resolved_path.parent
251+
file_name = resolved_path.name
252+
else:
253+
directory = resolved_path
254+
file_name = None
255+
248256
self._project.add_library(
249257
library_name,
250-
Path(path).resolve(),
258+
directory,
251259
self._which_vhdl_standard(vhdl_standard),
252260
is_external=True,
261+
file_name=file_name
253262
)
254263
return self.library(library_name)
255264

0 commit comments

Comments
 (0)