aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
authorTristan Gingold <tgingold@free.fr>2019-06-20 19:02:50 +0200
committerTristan Gingold <tgingold@free.fr>2019-06-20 19:02:50 +0200
commitb5dcc2fcfdab9cb0a80dbf598ecdef6c71333715 (patch)
tree1228ba774b42784ca976ade814ac03f0abeb4672 /python
parentf7aaec6245aea5a59e33b8e4f3aeb11375ef23b0 (diff)
downloadghdl-b5dcc2fcfdab9cb0a80dbf598ecdef6c71333715.tar.gz
ghdl-b5dcc2fcfdab9cb0a80dbf598ecdef6c71333715.tar.bz2
ghdl-b5dcc2fcfdab9cb0a80dbf598ecdef6c71333715.zip
libghdl: just put the version in config.py
Diffstat (limited to 'python')
-rw-r--r--python/libghdl/__init__.py56
1 files changed, 33 insertions, 23 deletions
diff --git a/python/libghdl/__init__.py b/python/libghdl/__init__.py
index 6f55d666f..8735bf666 100644
--- a/python/libghdl/__init__.py
+++ b/python/libghdl/__init__.py
@@ -1,66 +1,76 @@
import ctypes
-from os import environ
+import os
from os.path import dirname, join, exists
from shutil import which
-from libghdl.config import __libghdl__
+from libghdl.config 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(os.name, 'so')
+ return 'libghdl-' + ver + '.' + ext
-def check_libghdl_libdir(libdir):
+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, __libghdl__)
+ res = join(libdir, basename)
if exists(res):
return res
return None
-def check_libghdl_bindir(bindir):
- """Return libghdl path in :param bindir" if found or None"""
+
+def _check_libghdl_bindir(bindir, basename):
if bindir is None:
return None
- return check_libghdl_libdir(join(bindir, '..', 'lib'))
+ return _check_libghdl_libdir(join(bindir, '..', 'lib'), basename)
-def get_libghdl_path():
+
+def _get_libghdl_path():
"""Locate the directory where the shared library is"""
+ basename = get_libghdl_name()
# Try VUNIT_GHDL_PATH (path of the ghdl binary when using VUnit).
- r = check_libghdl_bindir (environ.get('VUNIT_GHDL_PATH'))
+ 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 = environ.get('GHDL', 'ghdl')
+ r = os.environ.get('GHDL', 'ghdl')
r = which(r)
if r is not None:
- r = check_libghdl_bindir(dirname(r))
+ 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))
+ r = _check_libghdl_bindir(dirname(r), basename)
if r is not None:
return r
# Try when running from the build directory
r = join(dirname(__file__), '..', '..', 'lib')
- r = check_libghdl_libdir(r)
+ r = _check_libghdl_libdir(r, basename)
if r is not None:
return r
# Failed.
- return None
+ raise Exception('Cannot find libghdl {}'.format(basename))
-libghdl_path = get_libghdl_path()
-if libghdl_path is None:
- raise Exception('Cannot find libghdl')
-
-libghdl = ctypes.CDLL(libghdl_path)
+# Load the shared library
+_libghdl_path = _get_libghdl_path()
+libghdl = ctypes.CDLL(_libghdl_path)
+# Initialize it.
libghdl.libghdl_init()
+def _to_char_p(arg):
+ return ctypes.c_char_p(arg), len(arg)
+
+
def set_option(opt):
return libghdl.libghdl__set_option(*_to_char_p(opt))
@@ -74,6 +84,6 @@ def analyze_file(fname):
if False:
- _prefix = environ.get("LIBGHDL_PREFIX") or '--PREFIX=%s' % join(dirname(libghdl_path), 'ghdl')
- print('ghdl prefix: {}'.format(_prefix))
+ _prefix = os.environ.get("LIBGHDL_PREFIX") or '--PREFIX=%s' % join(dirname(_libghdl_path), 'ghdl')
+ # print('ghdl prefix: {}'.format(_prefix))
set_option(_prefix.encode('utf-8'))