aboutsummaryrefslogtreecommitdiffstats
path: root/python/libghdl/__init__.py
blob: 3050a82da34813990db0cfe0964cb5649bb9c16b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import ctypes
import os
from os.path import dirname, join, exists
from shutil import which
from libghdl.version import __version__


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, 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(join(bindir, '..', 'lib'), basename)


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 (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 = 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()
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))


def analyze_init():
    return libghdl.libghdl__analyze_init()


def analyze_file(fname):
    return libghdl.libghdl__analyze_file(*_to_char_p(fname))


if False:
    _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'))