Debian Codegraph @

cocotb_build_libs.py

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 # Copyright cocotb contributors
2 # Licensed under the Revised BSD License, see LICENSE for details.
3 # SPDX-License-Identifier: BSD-3-Clause
4
5 import distutils
6 import logging
7 import os
8 import subprocess
9 import sys
10 import sysconfig
11 import textwrap
12 from distutils.ccompiler import get_default_compiler
13 from distutils.file_util import copy_file
14 from typing import List
15
16 from setuptools import Extension
17 from setuptools.command.build_ext import build_ext as _build_ext
18
19 logger = logging.getLogger(__name__)
20 cocotb_share_dir = os.path.abspath(
21 os.path.join(os.path.dirname(__file__), "src", "cocotb", "share")
22 )
23 _base_warns = [
24 "-Wall",
25 "-Wextra",
26 "-Wcast-qual",
27 "-Wwrite-strings",
28 "-Wconversion",
29 # -Wno-missing-field-initializers is required on GCC 4.x to prevent a
30 # spurious warning `error: missing initializer for member ...` when
31 # compiling `PyTypeObject type = {};` in `simulatormodule.cpp`.
32 # (See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36750.) This flag can be
33 # removed once we require later GCC versions.
34 "-Wno-missing-field-initializers",
35 "-Werror=shadow",
36 ]
37 _ccx_warns = [*_base_warns, "-Wnon-virtual-dtor", "-Woverloaded-virtual"]
38 _extra_cxx_compile_args = [
39 "-std=c++11",
40 "-fvisibility=hidden",
41 "-fvisibility-inlines-hidden",
42 *_ccx_warns,
43 ]
44 if os.name != "nt":
45 _extra_cxx_compile_args += ["-flto"]
46
47 _extra_cxx_compile_args_msvc = ["/permissive-"]
48
49 # Make PRI* format macros available with C++11 compiler but older libc, e.g. on RHEL6.
50 _extra_defines = [("__STDC_FORMAT_MACROS", "")]
51
52
53 def create_sxs_assembly_manifest(
54 name: str, filename: str, libraries: List[str], dependency_only=False
55 ) -> str:
56 """
57 Create side-by-side (sxs) assembly manifest
58
59 It contains dependencies to other assemblies (in our case the assemblies are equal to the other libraries).
60 For more details see:
61 - https://docs.microsoft.com/en-us/windows/win32/sbscs/assembly-manifests
62 - https://docs.microsoft.com/en-us/windows/win32/sbscs/using-side-by-side-assemblies
63
64 Args:
65 name: The name of the assembly for which the manifest is generated, e.g. ``libcocotbutils``.
66 filename: The filename of the library, e.g. ``libcocotbutils.dll``.
67 libraries: A list of names of dependent manifests, e.g. ``["libgpilog"]``.
68 """
69
70 architecture = "amd64" if sys.maxsize > 2**32 else "x86"
71 dependencies = []
72
73 for lib in libraries:
74 dependencies.append(
75 textwrap.dedent(
76 """\
77 <dependency>
78 <dependentAssembly>
79 <assemblyIdentity name="%s" version="1.0.0.0" type="win32" processorArchitecture="%s" />
80 </dependentAssembly>
81 </dependency>
82 """
83 )
84 % (lib, architecture)
85 )
86
87 if not dependency_only:
88 manifest_body = textwrap.dedent(
89 """\
90 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
91 <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
92 <assemblyIdentity name="%s" version="1.0.0.0" type="win32" processorArchitecture="%s" />
93 <file name="%s" />
94 %s
95 </assembly>
96 """
97 ) % (
98 name,
99 architecture,
100 filename,
101 textwrap.indent("".join(dependencies), " ").strip(),
102 )
103 else:
104 manifest_body = textwrap.dedent(
105 """\
106 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
107 <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
108 %s
109 </assembly>
110 """
111 ) % (textwrap.indent("".join(dependencies), " ").strip())
112
113 return manifest_body
114
115
116 def create_sxs_appconfig(filename):
117 """
118 Create side-by-side (sxs) application configuration file.
119
120 The application configuration specifies additional search paths for manifests.
121 For more details see: https://docs.microsoft.com/en-us/windows/win32/sbscs/application-configuration-files
122 """
123
124 config_body = textwrap.dedent(
125 """\
126 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
127 <configuration>
128 <windows>
129 <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
130 <probing privatePath="libs" />
131 </assemblyBinding>
132 </windows>
133 </configuration>
134 """
135 )
136
137 dirpath = os.path.dirname(filename)
138 os.makedirs(dirpath, exist_ok=True)
139
140 with open(filename + ".2.config", "w", encoding="utf-8") as f:
141 f.write(config_body)
142
143
144 def create_rc_file(rc_filename, name, filename, libraries, runtime_libraries):
145 """
146 Creates windows resource definition script to embed the side-by-side assembly manifest into the libraries.
147
148 For more details see: https://docs.microsoft.com/en-us/windows/win32/menurc/about-resource-files
149 """
150
151 manifest = create_sxs_assembly_manifest(name, filename, libraries)
152
153 # Escape double quotes and put every line between double quotes for embedding into rc file
154 manifest = manifest.replace('"', '""')
155 manifest = "\n".join([f'"{x}\\r\\n"' for x in manifest.splitlines()])
156
157 rc_body = (
158 textwrap.dedent(
159 """\
160 #pragma code_page(65001) // UTF-8
161 #include <Windows.h>
162
163 LANGUAGE 0x00, 0x00
164
165 ISOLATIONAWARE_MANIFEST_RESOURCE_ID RT_MANIFEST
166 BEGIN
167 %s
168 END
169 """
170 )
171 % manifest
172 )
173
174 if runtime_libraries is not None:
175 manifest = create_sxs_assembly_manifest(
176 name, filename, runtime_libraries, dependency_only=True
177 )
178
179 # Escape double quotes and put every line between double quotes for embedding into rc file
180 manifest = manifest.replace('"', '""')
181 manifest = "\n".join([f'"{x}\\r\\n"' for x in manifest.splitlines()])
182
183 rc_body += (
184 textwrap.dedent(
185 """\
186 1000 RT_MANIFEST
187 BEGIN
188 %s
189 END
190 """
191 )
192 % manifest
193 )
194
195 with open(rc_filename, "w", encoding="utf-8") as f:
196 f.write(rc_body)
197
198
199 def _get_lib_ext_name():
200 """Get name of default library file extension on given OS."""
201
202 if os.name == "nt":
203 ext_name = "dll"
204 else:
205 ext_name = "so"
206
207 return ext_name
208
209
210 class build_ext(_build_ext):
211 def _uses_msvc(self):
212 if self.compiler == "msvc":
213 return True
214 if self.compiler is None:
215 return get_default_compiler() == "msvc"
216 else:
217 return getattr(self.compiler, "compiler_type", None) == "msvc"
218
219 def run(self):
220 if os.name == "nt":
221 create_sxs_appconfig(
222 self.get_ext_fullpath(os.path.join("cocotb", "simulator"))
223 )
224
225 super().run()
226
227 def build_extensions(self):
228 if os.name == "nt":
229 if self._uses_msvc():
230 # Initialize the compiler now so that compiler/linker flags are populated
231 if not self.compiler.initialized:
232 self.compiler.initialize()
233
234 # Setuptools defaults to activate automatic manifest generation for msvc,
235 # disable it here as we manually generate it to also support mingw on windows
236 for k, ldflags in self.compiler._ldflags.items():
237 self.compiler._ldflags[k] = [
238 x for x in ldflags if not x.startswith("/MANIFEST")
239 ] + ["/MANIFEST:NO"]
240
241 self.compiler.compile_options = [
242 x for x in self.compiler.compile_options if not x.startswith("/W")
243 ] + ["/W4"]
244
245 ext_names = {os.path.split(ext.name)[-1] for ext in self.extensions}
246 for ext in self.extensions:
247 fullname = self.get_ext_fullname(ext.name)
248 filename = self.get_ext_filename(fullname)
249 name = os.path.split(fullname)[-1]
250 filename = os.path.split(filename)[-1]
251 libraries = {"lib" + lib for lib in ext.libraries}.intersection(
252 ext_names
253 )
254 rc_filename = name + ".rc"
255 runtime_libraries = None
256
257 # Add the runtime dependency on libcocotb to libembed
258 if name == "libembed":
259 runtime_libraries = ["libcocotb"]
260
261 # Strip lib prefix for msvc
262 if self._uses_msvc():
263 name = name[3:] if name.startswith("lib") else name
264 libraries = {
265 (lib[3:] if lib.startswith("lib") else lib) for lib in libraries
266 }
267 if runtime_libraries is not None:
268 runtime_libraries = {
269 (lib[3:] if lib.startswith("lib") else lib)
270 for lib in runtime_libraries
271 }
272 create_rc_file(
273 rc_filename, name, filename, libraries, runtime_libraries
274 )
275
276 def_dir = os.path.join(cocotb_share_dir, "def")
277 self._gen_import_libs(def_dir)
278
279 for e in self.extensions:
280 e.library_dirs += [def_dir]
281
282 super().build_extensions()
283
284 def build_extension(self, ext):
285 """Build each extension in its own temp directory to make gcov happy.
286
287 A normal PEP 517 install still works as the temp directories are discarded anyway.
288 """
289 lib_name = os.path.split(ext.name)[-1]
290
291 if self._uses_msvc():
292 ext.extra_compile_args += _extra_cxx_compile_args_msvc
293 else:
294 ext.extra_compile_args += _extra_cxx_compile_args
295
296 if os.name == "nt":
297 # Align behavior of gcc with msvc and export only symbols marked with __declspec(dllexport)
298 ext.extra_link_args += ["-Wl,--exclude-all-symbols"]
299 else:
300 ext.extra_link_args += ["-flto"]
301
302 rpaths = []
303 if lib_name == "simulator":
304 rpaths += ["$ORIGIN/libs"]
305 install_name = None
306 else:
307 rpaths += ["$ORIGIN"]
308 install_name = lib_name
309
310 if sys.platform == "darwin":
311 rpaths = [
312 rpath.replace("$ORIGIN", "@loader_path") for rpath in rpaths
313 ]
314 if install_name is not None:
315 ext.extra_link_args += [
316 f"-Wl,-install_name,@rpath/{install_name}.so"
317 ]
318
319 if sys.platform == "linux":
320 # Avoid a runtime dependency on libstdc++. Some simulators
321 # ship a version of libstdc++6.so which is older than the
322 # one cocotb has been compiled with, which will then lead to
323 # load-time errors like "libstdc++.so.6: version
324 # `GLIBCXX_3.4.29' not found (required by
325 # /path/to/libcocotbvhpi_modelsim.so)."
326 ext.extra_link_args += ["-static-libstdc++"]
327
328 ext.extra_link_args += [f"-Wl,-rpath,{rpath}" for rpath in rpaths]
329
330 # vpi_user.h and vhpi_user.h require that WIN32 is defined
331 if os.name == "nt":
332 ext.define_macros += [("WIN32", "")]
333
334 if lib_name == "libembed":
335 if self._uses_msvc():
336 embed_lib_name = "cocotb"
337 else:
338 embed_lib_name = "libcocotb"
339 ext.define_macros += [
340 ("EMBED_IMPL_LIB", embed_lib_name + "." + _get_lib_ext_name())
341 ]
342
343 old_build_temp = self.build_temp
344 self.build_temp = os.path.join(self.build_temp, ext.name)
345 super().build_extension(ext)
346 self.build_temp = old_build_temp
347
348 # Needed for Windows to not assume python module (generate interface in def file)
349 def get_export_symbols(self, ext):
350 return None
351
352 # For proper cocotb library naming, based on https://github.com/cython/cython/issues/1740
353 def get_ext_filename(self, ext_name):
354 """
355 Like the base class method, but for libraries that are not python extension:
356 - removes the ``.cpython-36m-x86_64-linux-gnu.`` or ``-cpython-36m.`` part before the extension
357 - replaces ``.pyd`` with ``.dll`` on windows.
358 """
359
360 filename = _build_ext.get_ext_filename(self, ext_name)
361
362 # for the simulator python extension library, leaving suffix in place
363 if os.path.split(ext_name)[-1] == "simulator":
364 return filename
365
366 head, tail = os.path.split(filename)
367 tail_split = tail.split(".")
368
369 # mingw on msys2 uses `-` as separator
370 tail_split = tail_split[0].split("-")
371
372 # strip lib prefix if msvc is used
373 if self._uses_msvc() and tail_split[0].startswith("lib"):
374 tail_split[0] = tail_split[0][3:]
375
376 filename_short = os.path.join(head, tail_split[0] + "." + _get_lib_ext_name())
377
378 # icarus requires vpl extension
379 filename_short = filename_short.replace(
380 "libcocotbvpi_icarus.so", "libcocotbvpi_icarus.vpl"
381 )
382 filename_short = filename_short.replace(
383 "libcocotbvpi_icarus.dll", "libcocotbvpi_icarus.vpl"
384 )
385 filename_short = filename_short.replace(
386 "cocotbvpi_icarus.dll", "cocotbvpi_icarus.vpl"
387 )
388
389 return filename_short
390
391 def finalize_options(self):
392 """Like the base class method,but add extra library_dirs path."""
393
394 super().finalize_options()
395
396 for ext in self.extensions:
397 ext.library_dirs.append(os.path.join(self.build_lib, "cocotb", "libs"))
398
399 def copy_extensions_to_source(self):
400 """Like the base class method, but copy libs into proper directory in develop."""
401
402 build_py = self.get_finalized_command("build_py")
403 for ext in self.extensions:
404 fullname = self.get_ext_fullname(ext.name)
405 filename = self.get_ext_filename(fullname)
406 modpath = fullname.split(".")
407 package = ".".join(modpath[:-1])
408 package_dir = build_py.get_package_dir(package)
409 # unlike the method from `setuptools`, we do not call `os.path.basename` here
410 dest_filename = os.path.join(package_dir, filename)
411 src_filename = os.path.join(self.build_lib, filename)
412
413 os.makedirs(os.path.dirname(dest_filename), exist_ok=True)
414
415 copy_file(
416 src_filename, dest_filename, verbose=self.verbose, dry_run=self.dry_run
417 )
418 if ext._needs_stub:
419 self.write_stub(package_dir or os.curdir, ext, True)
420
421 def _gen_import_libs(self, def_dir):
422 """
423 On Windows generate import libraries that contains the code required to
424 load the DLL (.a) based on module definition files (.def)
425 """
426
427 for sim in ["icarus", "modelsim", "aldec", "ghdl", "nvcvhpi"]:
428 if self._uses_msvc():
429 subprocess.run(
430 [
431 self.compiler.lib,
432 "/def:" + os.path.join(def_dir, sim + ".def"),
433 "/out:" + os.path.join(def_dir, sim + ".lib"),
434 "/machine:" + ("X64" if sys.maxsize > 2**32 else "X86"),
435 ],
436 check=True,
437 )
438 else:
439 subprocess.run(
440 [
441 "dlltool",
442 "-d",
443 os.path.join(def_dir, sim + ".def"),
444 "-l",
445 os.path.join(def_dir, "lib" + sim + ".a"),
446 ],
447 check=True,
448 )
449
450
451 def _get_python_lib_link():
452 """Get name of python library used for linking"""
453
454 if sys.platform == "darwin":
455 ld_library = sysconfig.get_config_var("LIBRARY")
456 else:
457 ld_library = sysconfig.get_config_var("LDLIBRARY")
458
459 if ld_library is not None:
460 python_lib_link = os.path.splitext(ld_library)[0][3:]
461 else:
462 python_version = sysconfig.get_python_version().replace(".", "")
463 python_lib_link = "python" + python_version
464
465 return python_lib_link
466
467
468 def _get_python_lib():
469 """Get the library for embedded the python interpreter"""
470
471 if os.name == "nt":
472 python_lib = _get_python_lib_link() + "." + _get_lib_ext_name()
473 elif sys.platform == "darwin":
474 python_lib = os.path.join(
475 sysconfig.get_config_var("LIBDIR"), "lib" + _get_python_lib_link() + "."
476 )
477 if os.path.exists(python_lib + "dylib"):
478 python_lib += "dylib"
479 else:
480 python_lib += "so"
481 else:
482 python_lib = "lib" + _get_python_lib_link() + "." + _get_lib_ext_name()
483
484 return python_lib
485
486
487 def _get_common_lib_ext(include_dirs, share_lib_dir):
488 """
489 Defines common libraries.
490
491 All libraries go into the same directory to enable loading without modifying the library path (e.g. LD_LIBRARY_PATH).
492 """
493
494 #
495 # libcocotbutils
496 #
497 libcocotbutils_sources = [os.path.join(share_lib_dir, "utils", "cocotb_utils.cpp")]
498 if os.name == "nt":
499 libcocotbutils_sources += ["libcocotbutils.rc"]
500 libcocotbutils_libraries = ["gpilog"]
501 if sys.platform.startswith(("linux", "darwin", "cygwin", "msys")):
502 libcocotbutils_libraries.append("dl") # dlopen, dlerror, dlsym
503 libcocotbutils = Extension(
504 os.path.join("cocotb", "libs", "libcocotbutils"),
505 define_macros=[("COCOTBUTILS_EXPORTS", ""), *_extra_defines],
506 include_dirs=include_dirs,
507 libraries=libcocotbutils_libraries,
508 sources=libcocotbutils_sources,
509 )
510
511 #
512 # libgpilog
513 #
514 python_lib_dirs = []
515 if sys.platform == "darwin":
516 python_lib_dirs = [sysconfig.get_config_var("LIBDIR")]
517
518 libgpilog_sources = [os.path.join(share_lib_dir, "gpi_log", "gpi_logging.cpp")]
519 if os.name == "nt":
520 libgpilog_sources += ["libgpilog.rc"]
521 libgpilog = Extension(
522 os.path.join("cocotb", "libs", "libgpilog"),
523 define_macros=[("GPILOG_EXPORTS", ""), *_extra_defines],
524 include_dirs=include_dirs,
525 sources=libgpilog_sources,
526 )
527
528 #
529 # libpygpilog
530 #
531 libpygpilog_sources = [
532 os.path.join(share_lib_dir, "py_gpi_log", "py_gpi_logging.cpp")
533 ]
534 if os.name == "nt":
535 libpygpilog_sources += ["libpygpilog.rc"]
536 libpygpilog = Extension(
537 os.path.join("cocotb", "libs", "libpygpilog"),
538 define_macros=[("PYGPILOG_EXPORTS", ""), *_extra_defines],
539 include_dirs=include_dirs,
540 libraries=["gpilog"],
541 sources=libpygpilog_sources,
542 )
543
544 #
545 # libembed
546 #
547 libembed_sources = [os.path.join(share_lib_dir, "embed", "embed.cpp")]
548 if os.name == "nt":
549 libembed_sources += ["libembed.rc"]
550 libembed = Extension(
551 os.path.join("cocotb", "libs", "libembed"),
552 define_macros=[
553 ("COCOTB_EMBED_EXPORTS", ""),
554 ("PYTHON_LIB", _get_python_lib()),
555 *_extra_defines,
556 ],
557 include_dirs=include_dirs,
558 libraries=["gpilog", "cocotbutils"],
559 sources=libembed_sources,
560 )
561
562 #
563 # libcocotb
564 #
565 libcocotb_sources = [os.path.join(share_lib_dir, "embed", "gpi_embed.cpp")]
566 if os.name == "nt":
567 libcocotb_sources += ["libcocotb.rc"]
568 libcocotb = Extension(
569 os.path.join("cocotb", "libs", "libcocotb"),
570 define_macros=_extra_defines,
571 include_dirs=include_dirs,
572 libraries=["gpilog", "cocotbutils", "pygpilog", "gpi"],
573 sources=libcocotb_sources,
574 )
575
576 #
577 # libgpi
578 #
579 libgpi_sources = [
580 os.path.join(share_lib_dir, "gpi", "GpiCbHdl.cpp"),
581 os.path.join(share_lib_dir, "gpi", "GpiCommon.cpp"),
582 ]
583 if os.name == "nt":
584 libgpi_sources += ["libgpi.rc"]
585 libgpi = Extension(
586 os.path.join("cocotb", "libs", "libgpi"),
587 define_macros=[
588 ("GPI_EXPORTS", ""),
589 ("LIB_EXT", _get_lib_ext_name()),
590 ("SINGLETON_HANDLES", ""),
591 *_extra_defines,
592 ],
593 include_dirs=include_dirs,
594 libraries=["cocotbutils", "gpilog", "embed"],
595 sources=libgpi_sources,
596 )
597
598 #
599 # simulator
600 #
601 simulator_sources = [
602 os.path.join(share_lib_dir, "simulator", "simulatormodule.cpp"),
603 ]
604 if os.name == "nt":
605 simulator_sources += ["simulator.rc"]
606 libsim = Extension(
607 os.path.join("cocotb", "simulator"),
608 define_macros=_extra_defines,
609 include_dirs=include_dirs,
610 libraries=["cocotbutils", "gpilog", "gpi", "pygpilog"],
611 library_dirs=python_lib_dirs,
612 sources=simulator_sources,
613 )
614
615 # The libraries in this list are compiled in order of their appearance.
616 # If there is a linking dependency on one library to another,
617 # the linked library must be built first.
618 return [libgpilog, libpygpilog, libcocotbutils, libembed, libgpi, libcocotb, libsim]
619
620
621 def _get_vpi_lib_ext(
622 include_dirs, share_lib_dir, sim_define, extra_lib=[], extra_lib_dir=[]
623 ):
624 lib_name = "libcocotbvpi_" + sim_define.lower()
625 libcocotbvpi_sources = [
626 os.path.join(share_lib_dir, "vpi", "VpiImpl.cpp"),
627 os.path.join(share_lib_dir, "vpi", "VpiCbHdl.cpp"),
628 os.path.join(share_lib_dir, "vpi", "VpiObj.cpp"),
629 os.path.join(share_lib_dir, "vpi", "VpiIterator.cpp"),
630 os.path.join(share_lib_dir, "vpi", "VpiSignal.cpp"),
631 ]
632 if os.name == "nt":
633 libcocotbvpi_sources += [lib_name + ".rc"]
634 libcocotbvpi = Extension(
635 os.path.join("cocotb", "libs", lib_name),
636 define_macros=[("COCOTBVPI_EXPORTS", ""), (sim_define, ""), *_extra_defines],
637 include_dirs=include_dirs,
638 libraries=["gpi", "gpilog", *extra_lib],
639 library_dirs=extra_lib_dir,
640 sources=libcocotbvpi_sources,
641 )
642
643 return libcocotbvpi
644
645
646 def _get_vhpi_lib_ext(
647 include_dirs, share_lib_dir, sim_define, extra_lib=[], extra_lib_dir=[]
648 ):
649 lib_name = "libcocotbvhpi_" + sim_define.lower()
650 libcocotbvhpi_sources = [
651 os.path.join(share_lib_dir, "vhpi", "VhpiImpl.cpp"),
652 os.path.join(share_lib_dir, "vhpi", "VhpiCbHdl.cpp"),
653 ]
654 if os.name == "nt":
655 libcocotbvhpi_sources += [lib_name + ".rc"]
656 libcocotbvhpi = Extension(
657 os.path.join("cocotb", "libs", lib_name),
658 include_dirs=include_dirs,
659 define_macros=[("COCOTBVHPI_EXPORTS", ""), (sim_define, ""), *_extra_defines],
660 libraries=["gpi", "gpilog", *extra_lib],
661 library_dirs=extra_lib_dir,
662 sources=libcocotbvhpi_sources,
663 )
664
665 return libcocotbvhpi
666
667
668 def get_ext():
669 cfg_vars = distutils.sysconfig.get_config_vars()
670
671 if sys.platform == "darwin":
672 cfg_vars["LDSHARED"] = cfg_vars["LDSHARED"].replace("-bundle", "-dynamiclib")
673 cfg_vars["LDCXXSHARED"] = cfg_vars["LDSHARED"].replace("-bundle", "-dynamiclib")
674
675 share_lib_dir = os.path.relpath(os.path.join(cocotb_share_dir, "lib"))
676 include_dirs = [
677 os.path.relpath(os.path.join(cocotb_share_dir, "include")),
678 os.path.relpath(os.path.join(os.path.dirname(__file__), "src", "cocotb")),
679 ]
680
681 ext = []
682
683 logger.info("Compiling interface libraries for cocotb ...")
684
685 ext += _get_common_lib_ext(include_dirs, share_lib_dir)
686
687 #
688 # Icarus Verilog
689 #
690 icarus_extra_lib = []
691 logger.info("Compiling libraries for Icarus Verilog")
692 if os.name == "nt":
693 icarus_extra_lib = ["icarus"]
694
695 icarus_vpi_ext = _get_vpi_lib_ext(
696 include_dirs=include_dirs,
697 share_lib_dir=share_lib_dir,
698 sim_define="ICARUS",
699 extra_lib=icarus_extra_lib,
700 )
701 ext.append(icarus_vpi_ext)
702
703 #
704 # Modelsim/Questa
705 #
706 modelsim_extra_lib = []
707 logger.info("Compiling libraries for Modelsim/Questa")
708 if os.name == "nt":
709 modelsim_extra_lib = ["modelsim"]
710
711 modelsim_vpi_ext = _get_vpi_lib_ext(
712 include_dirs=include_dirs,
713 share_lib_dir=share_lib_dir,
714 sim_define="MODELSIM",
715 extra_lib=modelsim_extra_lib,
716 )
717 ext.append(modelsim_vpi_ext)
718
719 modelsim_vhpi_ext = _get_vhpi_lib_ext(
720 include_dirs=include_dirs,
721 share_lib_dir=share_lib_dir,
722 sim_define="MODELSIM",
723 extra_lib=modelsim_extra_lib,
724 )
725 ext.append(modelsim_vhpi_ext)
726
727 lib_name = "libcocotbfli_modelsim"
728 fli_sources = [
729 os.path.join(share_lib_dir, "fli", "FliImpl.cpp"),
730 os.path.join(share_lib_dir, "fli", "FliCbHdl.cpp"),
731 os.path.join(share_lib_dir, "fli", "FliObjHdl.cpp"),
732 ]
733 if os.name == "nt":
734 fli_sources += [lib_name + ".rc"]
735 fli_ext = Extension(
736 os.path.join("cocotb", "libs", lib_name),
737 define_macros=[("COCOTBFLI_EXPORTS", ""), *_extra_defines],
738 include_dirs=include_dirs,
739 libraries=["gpi", "gpilog", *modelsim_extra_lib],
740 sources=fli_sources,
741 )
742
743 ext.append(fli_ext)
744
745 #
746 # GHDL
747 #
748 ghdl_extra_lib = []
749 logger.info("Compiling libraries for GHDL")
750 if os.name == "nt":
751 ghdl_extra_lib = ["ghdl"]
752
753 ghdl_vpi_ext = _get_vpi_lib_ext(
754 include_dirs=include_dirs,
755 share_lib_dir=share_lib_dir,
756 sim_define="GHDL",
757 extra_lib=ghdl_extra_lib,
758 )
759 ext.append(ghdl_vpi_ext)
760
761 #
762 # IUS
763 #
764 if os.name == "posix":
765 logger.info("Compiling libraries for Incisive/Xcelium")
766 ius_vpi_ext = _get_vpi_lib_ext(
767 include_dirs=include_dirs, share_lib_dir=share_lib_dir, sim_define="IUS"
768 )
769 ext.append(ius_vpi_ext)
770
771 ius_vhpi_ext = _get_vhpi_lib_ext(
772 include_dirs=include_dirs, share_lib_dir=share_lib_dir, sim_define="IUS"
773 )
774 ext.append(ius_vhpi_ext)
775
776 #
777 # VCS
778 #
779 if os.name == "posix":
780 logger.info("Compiling libraries for VCS")
781 vcs_vpi_ext = _get_vpi_lib_ext(
782 include_dirs=include_dirs, share_lib_dir=share_lib_dir, sim_define="VCS"
783 )
784 ext.append(vcs_vpi_ext)
785
786 #
787 # Aldec Riviera Pro
788 #
789 aldec_extra_lib = []
790 logger.info("Compiling libraries for Riviera")
791 if os.name == "nt":
792 aldec_extra_lib = ["aldec"]
793
794 aldec_vpi_ext = _get_vpi_lib_ext(
795 include_dirs=include_dirs,
796 share_lib_dir=share_lib_dir,
797 sim_define="ALDEC",
798 extra_lib=aldec_extra_lib,
799 )
800 ext.append(aldec_vpi_ext)
801
802 aldec_vhpi_ext = _get_vhpi_lib_ext(
803 include_dirs=include_dirs,
804 share_lib_dir=share_lib_dir,
805 sim_define="ALDEC",
806 extra_lib=aldec_extra_lib,
807 )
808 ext.append(aldec_vhpi_ext)
809
810 #
811 # Verilator
812 #
813 if os.name == "posix":
814 logger.info("Compiling libraries for Verilator")
815 verilator_vpi_ext = _get_vpi_lib_ext(
816 include_dirs=include_dirs,
817 share_lib_dir=share_lib_dir,
818 sim_define="VERILATOR",
819 )
820 ext.append(verilator_vpi_ext)
821
822 #
823 # NVC
824 #
825 nvc_extra_lib = []
826 if os.name == "nt":
827 nvc_extra_lib = ["nvcvhpi"]
828 logger.info("Compiling libraries for NVC")
829 nvc_vhpi_ext = _get_vhpi_lib_ext(
830 include_dirs=include_dirs,
831 share_lib_dir=share_lib_dir,
832 sim_define="NVC",
833 extra_lib=nvc_extra_lib,
834 )
835 ext.append(nvc_vhpi_ext)
836
837 #
838 # DSim
839 #
840 if os.name == "posix":
841 logger.info("Compiling libraries for DSim")
842 dsim_vpi_ext = _get_vpi_lib_ext(
843 include_dirs=include_dirs, share_lib_dir=share_lib_dir, sim_define="DSim"
844 )
845 ext.append(dsim_vpi_ext)
846
847 return ext