From 79e840fc4061b8483c1e580b98e18ab31869b063 Mon Sep 17 00:00:00 2001 From: Tristan Gingold Date: Thu, 20 Jun 2019 19:01:30 +0200 Subject: libghdl/__init__.py: rework prefix search. --- python/libghdl/__init__.py | 78 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 23 deletions(-) diff --git a/python/libghdl/__init__.py b/python/libghdl/__init__.py index 97b7acc82..6f55d666f 100644 --- a/python/libghdl/__init__.py +++ b/python/libghdl/__init__.py @@ -1,6 +1,6 @@ import ctypes from os import environ -from os.path import dirname, join +from os.path import dirname, join, exists from shutil import which from libghdl.config import __libghdl__ @@ -9,29 +9,60 @@ def _to_char_p(arg): return ctypes.c_char_p(arg), len(arg) -def get_ghdl_path(): - _dir = None - for envvar in [environ.get(item) for item in ['GHDL_BIN_PATH', 'VUNIT_GHDL_PATH']]: - if envvar: - _dir = envvar - break - if not _dir: - _dir = which(environ.get('GHDL', 'ghdl')) - if _dir: - _dir = join(dirname(_dir), '..', 'lib') - return _dir - - -_basedir = get_ghdl_path() or dirname(__file__) - -libghdl = ctypes.CDLL(join(_basedir, __libghdl__)) +def check_libghdl_libdir(libdir): + """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, __libghdl__) + if exists(res): + return res + return None + +def check_libghdl_bindir(bindir): + """Return libghdl path in :param bindir" if found or None""" + if bindir is None: + return None + return check_libghdl_libdir(join(bindir, '..', 'lib')) + +def get_libghdl_path(): + """Locate the directory where the shared library is""" + # Try VUNIT_GHDL_PATH (path of the ghdl binary when using VUnit). + r = check_libghdl_bindir (environ.get('VUNIT_GHDL_PATH')) + if r is not None: + return r + # Try GHDL (name/path of the ghdl binary) + r = environ.get('GHDL', 'ghdl') + r = which(r) + if r is not None: + r = check_libghdl_bindir(dirname(r)) + if r is not None: + return r + # Try within libghdl/ python installation + r = __file__ + r = check_libghdl_bindir(dirname(r)) + if r is not None: + return r + # Try when running from the build directory + r = join(dirname(__file__), '..', '..', 'lib') + r = check_libghdl_libdir(r) + if r is not None: + return r + # Failed. + return None + +libghdl_path = get_libghdl_path() + +if libghdl_path is None: + raise Exception('Cannot find libghdl') + +libghdl = ctypes.CDLL(libghdl_path) libghdl.libghdl_init() def set_option(opt): - arg = _to_char_p(opt) - return libghdl.libghdl__set_option(arg[0], arg[1]) + return libghdl.libghdl__set_option(*_to_char_p(opt)) def analyze_init(): @@ -39,9 +70,10 @@ def analyze_init(): def analyze_file(fname): - arg = _to_char_p(fname) - return libghdl.libghdl__analyze_file(arg[0], arg[1]) + return libghdl.libghdl__analyze_file(*_to_char_p(fname)) -_prefix = environ.get("LIBGHDL_PREFIX") or '--PREFIX=%s' % join(_basedir, 'ghdl') -set_option(_prefix.encode('utf-8')) +if False: + _prefix = environ.get("LIBGHDL_PREFIX") or '--PREFIX=%s' % join(dirname(libghdl_path), 'ghdl') + print('ghdl prefix: {}'.format(_prefix)) + set_option(_prefix.encode('utf-8')) -- cgit v1.2.3