aboutsummaryrefslogtreecommitdiffstats
path: root/python/libghdl/__init__.py
blob: 6f55d666f5918ac22114b688d8f20c4b6674cffa (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
import ctypes
from os import environ
from os.path import dirname, join, exists
from shutil import which
from libghdl.config import __libghdl__


def _to_char_p(arg):
    return ctypes.c_char_p(arg), len(arg)


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):
    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 = environ.get("LIBGHDL_PREFIX") or '--PREFIX=%s' % join(dirname(libghdl_path), 'ghdl')
    print('ghdl prefix: {}'.format(_prefix))
    set_option(_prefix.encode('utf-8'))