From bf74a0983c2d534e217e7312ab559ca8929ff8a2 Mon Sep 17 00:00:00 2001 From: umarcor Date: Sun, 27 Dec 2020 22:10:38 +0100 Subject: rework 'python', rename to 'pyGHDL' * Rename 'python' to 'pyGHDL'. * Let 'thin' be 'libghdl'. * Move move 'pyutils.py' from 'python/libghdl/vhdl' to a separate package ('pyGHDL/libghdl/utils/'). * Update 'vhdl_langserver' accordingly. * Rename 'vhdl_langserver' to 'lsp'. * Move 'ghdl-ls' to 'pyGHDL/cli'. --- pyGHDL/libghdl/__init__.py | 106 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 pyGHDL/libghdl/__init__.py (limited to 'pyGHDL/libghdl/__init__.py') diff --git a/pyGHDL/libghdl/__init__.py b/pyGHDL/libghdl/__init__.py new file mode 100644 index 000000000..4ba7b6b26 --- /dev/null +++ b/pyGHDL/libghdl/__init__.py @@ -0,0 +1,106 @@ +import ctypes +import os +import sys +from os.path import dirname, join, exists, normpath +from shutil import which +from libghdl.version import __version__ + + +def _to_char_p(arg): + return ctypes.c_char_p(arg), len(arg) + + +def _get_libghdl_name(): + """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") + return "libghdl-" + ver + "." + ext + + +def _check_libghdl_libdir(libdir, basename): + """Return libghdl path in :param libdir" if found or None""" + if libdir is None: + return None + # print('libghdl: check in {}'.format(libdir)) + res = join(libdir, basename) + if exists(res): + return res + return None + + +def _check_libghdl_bindir(bindir, basename): + if bindir is None: + return None + return _check_libghdl_libdir(normpath(join(bindir, "..", "lib")), basename) + + +def _get_libghdl_path(): + """Locate the directory where the shared library is""" + basename = _get_libghdl_name() + # Try GHDL_PREFIX + r = os.environ.get("GHDL_PREFIX") + if r is not None: + # GHDL_PREFIX is the prefix of the vhdl libraries, so remove the + # last path component. + r = _check_libghdl_libdir(dirname(r), basename) + if r is not None: + return r + # Try VUNIT_GHDL_PATH (path of the ghdl binary when using VUnit). + r = _check_libghdl_bindir(os.environ.get("VUNIT_GHDL_PATH"), basename) + if r is not None: + return r + # Try GHDL (name/path of the ghdl binary) + r = os.environ.get("GHDL", "ghdl") + r = which(r) + if r is not None: + r = _check_libghdl_bindir(dirname(r), basename) + if r is not None: + return r + # Try within libghdl/ python installation + r = __file__ + r = _check_libghdl_bindir(dirname(r), basename) + if r is not None: + return r + # Try when running from the build directory + r = normpath(join(dirname(__file__), "..", "..", "lib")) + r = _check_libghdl_libdir(r, basename) + if r is not None: + return r + # Failed. + raise Exception("Cannot find libghdl {}".format(basename)) + + +# Load the shared library +_libghdl_path = _get_libghdl_path() +# print("Load {}".format(_libghdl_path)) +libghdl = ctypes.CDLL(_libghdl_path) + +# Initialize it. +libghdl.libghdl_init() +libghdl.libghdl__set_hooks_for_analysis() + +# Set the prefix in order to locate the vhdl libraries. +libghdl.libghdl__set_exec_prefix( + *_to_char_p(dirname(dirname(_libghdl_path)).encode("utf-8")) +) + + +def set_option(opt): + "Set option OPT. Return true iff the option is known and handled" + return libghdl.libghdl__set_option(*_to_char_p(opt)) == 0 + + +def analyze_init(): + # Deprecated as it may raise an exception. Use analyze_init_status + libghdl.libghdl__analyze_init() + +def analyze_init_status(): + # Return 0 in case of success + return libghdl.libghdl__analyze_init_status() + +def analyze_file(fname): + return libghdl.libghdl__analyze_file(*_to_char_p(fname)) + + +def disp_config(): + return libghdl.ghdllocal__disp_config_prefixes() -- cgit v1.2.3 From 1c38bc3933ba4e469811c5898b9aeae896e23afc Mon Sep 17 00:00:00 2001 From: umarcor Date: Tue, 29 Dec 2020 02:40:13 +0100 Subject: pyGHDL/libghdl: fix absolute imports --- pyGHDL/libghdl/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pyGHDL/libghdl/__init__.py') diff --git a/pyGHDL/libghdl/__init__.py b/pyGHDL/libghdl/__init__.py index 4ba7b6b26..da39a4475 100644 --- a/pyGHDL/libghdl/__init__.py +++ b/pyGHDL/libghdl/__init__.py @@ -3,7 +3,7 @@ import os import sys from os.path import dirname, join, exists, normpath from shutil import which -from libghdl.version import __version__ +from pyGHDL.libghdl.version import __version__ def _to_char_p(arg): -- cgit v1.2.3