libinfo.py 3.97 KB
Newer Older
1
"""Library information."""
tqchen committed
2
from __future__ import absolute_import
3
import sys
tqchen committed
4
import os
5

tqchen committed
6

7
def find_lib_path(name=None, search_path=None, optional=False):
tqchen committed
8 9
    """Find dynamic library files.

10 11 12 13 14
    Parameters
    ----------
    name : list of str
        List of names to be found.

tqchen committed
15 16 17 18 19
    Returns
    -------
    lib_path : list(string)
        List of all found path to the libraries
    """
20
    use_runtime = os.environ.get("TVM_USE_RUNTIME_LIB", False)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

    # See https://github.com/dmlc/tvm/issues/281 for some background.

    # NB: This will either be the source directory (if TVM is run
    # inplace) or the install directory (if TVM is installed).
    # An installed TVM's curr_path will look something like:
    #   $PREFIX/lib/python3.6/site-packages/tvm/_ffi
    ffi_dir = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
    source_dir = os.path.join(ffi_dir, "..", "..", "..")
    install_lib_dir = os.path.join(ffi_dir, "..", "..", "..", "..")

    dll_path = []

    if os.environ.get('TVM_LIBRARY_PATH', None):
        dll_path.append(os.environ['TVM_LIBRARY_PATH'])

    if sys.platform.startswith('linux') and os.environ.get('LD_LIBRARY_PATH', None):
tqchen committed
38
        dll_path.extend([p.strip() for p in os.environ['LD_LIBRARY_PATH'].split(":")])
39 40 41
    elif sys.platform.startswith('darwin') and os.environ.get('DYLD_LIBRARY_PATH', None):
        dll_path.extend([p.strip() for p in os.environ['DYLD_LIBRARY_PATH'].split(":")])

42 43
    # Pip lib directory
    dll_path.append(os.path.join(ffi_dir, "../"))
44 45
    # Default cmake build directory
    dll_path.append(os.path.join(source_dir, "build"))
46
    dll_path.append(os.path.join(source_dir, "build", "Release"))
47 48 49 50 51
    # Default mkae build directory
    dll_path.append(os.path.join(source_dir, "lib"))

    dll_path.append(install_lib_dir)

52
    dll_path = [os.path.abspath(x) for x in dll_path]
53 54 55 56 57
    if search_path is not None:
        if search_path is list:
            dll_path = dll_path + search_path
        else:
            dll_path.append(search_path)
58
    if name is not None:
59 60 61 62 63 64
        if isinstance(name, list):
            lib_dll_path = []
            for n in name:
                lib_dll_path += [os.path.join(p, n) for p in dll_path]
        else:
            lib_dll_path = [os.path.join(p, name) for p in dll_path]
65
        runtime_dll_path = []
tqchen committed
66
    else:
67
        if sys.platform.startswith('win32'):
68 69 70 71
            lib_dll_path = [os.path.join(p, 'libtvm.dll') for p in dll_path] +\
                           [os.path.join(p, 'tvm.dll') for p in dll_path]
            runtime_dll_path = [os.path.join(p, 'libtvm_runtime.dll') for p in dll_path] +\
                               [os.path.join(p, 'tvm_runtime.dll') for p in dll_path]
72 73 74
        elif sys.platform.startswith('darwin'):
            lib_dll_path = [os.path.join(p, 'libtvm.dylib') for p in dll_path]
            runtime_dll_path = [os.path.join(p, 'libtvm_runtime.dylib') for p in dll_path]
75 76 77
        else:
            lib_dll_path = [os.path.join(p, 'libtvm.so') for p in dll_path]
            runtime_dll_path = [os.path.join(p, 'libtvm_runtime.so') for p in dll_path]
78

79 80 81
    if not use_runtime:
        # try to find lib_dll_path
        lib_found = [p for p in lib_dll_path if os.path.exists(p) and os.path.isfile(p)]
82 83
        lib_found += [p for p in runtime_dll_path if os.path.exists(p) and os.path.isfile(p)]
    else:
84 85 86
        # try to find runtime_dll_path
        use_runtime = True
        lib_found = [p for p in runtime_dll_path if os.path.exists(p) and os.path.isfile(p)]
87

88
    if not lib_found:
89 90 91 92 93
        message = ('Cannot find the files.\n' +
                   'List of candidates:\n' +
                   str('\n'.join(lib_dll_path + runtime_dll_path)))
        if not optional:
            raise RuntimeError(message)
94
        return None
95

96
    if use_runtime:
97
        sys.stderr.write("Loading runtime library %s... exec only\n" % lib_found[0])
98 99
        sys.stderr.flush()
    return lib_found
tqchen committed
100 101 102


# current version
103 104
# We use the version of the incoming release for code that is under development
__version__ = "0.4.0"