Skip to content

Commit

Permalink
pyGHDL: Added DLL search path for Python ≥3.8. (#1811)
Browse files Browse the repository at this point in the history
* Added DLL search path for Python ≥3.8.

Let Windows CPython 64-bit execute GHDL in msys64/mingw64.

* Fix executable name of Python based on the current environment.

(cherry picked from commit 618c814)

* Removed debug code.
  • Loading branch information
Paebbels committed Jun 30, 2021
1 parent afae1a4 commit 6f96377
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
27 changes: 19 additions & 8 deletions pyGHDL/libghdl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,9 @@
#
# SPDX-License-Identifier: GPL-2.0-or-later
# ============================================================================

from ctypes import c_char_p, CDLL
import os
import sys
from sys import platform as sys_platform, version_info as sys_version_info
from os import environ as os_environ
from pathlib import Path
from shutil import which
from typing import List
Expand Down Expand Up @@ -63,7 +62,7 @@ def InternalErrors(self):
def _get_libghdl_name() -> Path:
"""Get the name of the libghdl library (with version and extension)."""
ver = __version__.replace("-", "_").replace(".", "_")
ext = {"win32": "dll", "cygwin": "dll", "darwin": "dylib"}.get(sys.platform, "so")
ext = {"win32": "dll", "cygwin": "dll", "darwin": "dylib"}.get(sys_platform, "so")
return Path("libghdl-{version}.{ext}".format(version=ver, ext=ext))


Expand Down Expand Up @@ -103,21 +102,21 @@ def _get_libghdl_path():
# Try GHDL_PREFIX
# GHDL_PREFIX is the prefix of the vhdl libraries, so remove the
# last path component.
r = os.environ.get("GHDL_PREFIX")
r = os_environ.get("GHDL_PREFIX")
try:
return _check_libghdl_libdir(Path(r).parent, basename)
except (TypeError, FileNotFoundError):
pass

# Try VUNIT_GHDL_PATH (path of the ghdl binary when using VUnit).
r = os.environ.get("VUNIT_GHDL_PATH")
r = os_environ.get("VUNIT_GHDL_PATH")
try:
return _check_libghdl_bindir(Path(r), basename)
except (TypeError, FileNotFoundError):
pass

# Try GHDL (name/path of the ghdl binary)
r = os.environ.get("GHDL", "ghdl")
r = os_environ.get("GHDL", "ghdl")
r = which(r)
try:
return _check_libghdl_bindir(Path(r).parent, basename)
Expand Down Expand Up @@ -145,7 +144,19 @@ def _get_libghdl_path():
def _initialize():
# Load the shared library
_libghdl_path = _get_libghdl_path()
# print("Load {}".format(_libghdl_path))

# Add DLL search path(s)
if (
sys_platform == "win32"
and sys_version_info.major == 3
and sys_version_info.minor >= 8
):
from os import add_dll_directory as os_add_dll_directory

p1 = _libghdl_path.parent.parent / "bin"
os_add_dll_directory(str(p1))

# Load libghdl shared object
libghdl = CDLL(str(_libghdl_path))

# Initialize it.
Expand Down
5 changes: 3 additions & 2 deletions testsuite/pyunit/dom/Sanity.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@
# ============================================================================
from pathlib import Path
from subprocess import check_call, STDOUT
from sys import executable as sys_executable

from pytest import mark

from pyGHDL.dom.NonStandard import Design, Document
from pyGHDL.dom.NonStandard import Design

if __name__ == "__main__":
print("ERROR: you called a testcase declaration file as an executable module.")
Expand All @@ -51,7 +52,7 @@
@mark.xfail
@mark.parametrize("file", [str(f.relative_to(_TESTSUITE_ROOT)) for f in _TESTSUITE_ROOT.glob("sanity/**/*.vhdl")])
def test_AllVHDLSources(file):
check_call(["python", _GHDL_ROOT / "pyGHDL/cli/DOM.py", file], stderr=STDOUT)
check_call([sys_executable, _GHDL_ROOT / "pyGHDL/cli/DOM.py", file], stderr=STDOUT)

# document = Document(Path(file))
# design.Documents.append(document)

0 comments on commit 6f96377

Please sign in to comment.