libinfo.py 7.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
17
"""Library information."""
tqchen committed
18
from __future__ import absolute_import
19
import sys
tqchen committed
20
import os
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
def split_env_var(env_var, split):
    """Splits environment variable string.

    Parameters
    ----------
    env_var : str
        Name of environment variable.

    split : str
        String to split env_var on.

    Returns
    -------
    splits : list(string)
        If env_var exists, split env_var. Otherwise, empty list.
    """
    if os.environ.get(env_var, None):
        return [p.strip() for p in os.environ[env_var].split(split)]
    return []
tqchen committed
41

42
def find_lib_path(name=None, search_path=None, optional=False):
tqchen committed
43 44
    """Find dynamic library files.

45 46 47 48 49
    Parameters
    ----------
    name : list of str
        List of names to be found.

tqchen committed
50 51 52 53 54
    Returns
    -------
    lib_path : list(string)
        List of all found path to the libraries
    """
55
    use_runtime = os.environ.get("TVM_USE_RUNTIME_LIB", False)
56 57 58 59 60 61 62

    # 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
63
    ffi_dir = os.path.dirname(os.path.realpath(os.path.expanduser(__file__)))
64 65 66 67 68 69 70 71
    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'])

72 73 74 75 76 77 78 79
    if sys.platform.startswith('linux'):
        dll_path.extend(split_env_var('LD_LIBRARY_PATH', ':'))
        dll_path.extend(split_env_var('PATH', ':'))
    elif sys.platform.startswith('darwin'):
        dll_path.extend(split_env_var('DYLD_LIBRARY_PATH', ':'))
        dll_path.extend(split_env_var('PATH', ':'))
    elif sys.platform.startswith('win32'):
        dll_path.extend(split_env_var('PATH', ';'))
80

81
    # Pip lib directory
82
    dll_path.append(os.path.join(ffi_dir, ".."))
83 84
    # Default cmake build directory
    dll_path.append(os.path.join(source_dir, "build"))
85
    dll_path.append(os.path.join(source_dir, "build", "Release"))
86
    # Default make build directory
87 88 89 90
    dll_path.append(os.path.join(source_dir, "lib"))

    dll_path.append(install_lib_dir)

91
    dll_path = [os.path.realpath(x) for x in dll_path]
92 93 94 95 96
    if search_path is not None:
        if search_path is list:
            dll_path = dll_path + search_path
        else:
            dll_path.append(search_path)
97
    if name is not None:
98 99 100 101 102 103
        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]
104
        runtime_dll_path = []
tqchen committed
105
    else:
106
        if sys.platform.startswith('win32'):
107 108 109 110
            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]
111 112 113
        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]
114 115 116
        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]
117

118 119 120
    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)]
121 122
        lib_found += [p for p in runtime_dll_path if os.path.exists(p) and os.path.isfile(p)]
    else:
123 124 125
        # 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)]
126

127
    if not lib_found:
128 129 130 131 132
        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)
133
        return None
134

135
    if use_runtime:
136
        sys.stderr.write("Loading runtime library %s... exec only\n" % lib_found[0])
137 138
        sys.stderr.flush()
    return lib_found
tqchen committed
139 140


141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
def find_include_path(name=None, search_path=None, optional=False):
    """Find header files for C compilation.

    Parameters
    ----------
    name : list of str
        List of directory names to be searched.

    Returns
    -------
    include_path : list(string)
        List of all found paths to header files.
    """
    ffi_dir = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
    source_dir = os.path.join(ffi_dir, "..", "..", "..")
    install_include_dir = os.path.join(ffi_dir, "..", "..", "..", "..")
    third_party_dir = os.path.join(source_dir, "3rdparty")

    header_path = []

    if os.environ.get('TVM_INCLUDE_PATH', None):
        header_path.append(os.environ['TVM_INCLUDE_PATH'])

    header_path.append(install_include_dir)
    header_path.append(source_dir)
    header_path.append(third_party_dir)

    header_path = [os.path.abspath(x) for x in header_path]
    if search_path is not None:
        if search_path is list:
            header_path = header_path + search_path
        else:
            header_path.append(search_path)
    if name is not None:
        if isinstance(name, list):
            tvm_include_path = []
            for n in name:
                tvm_include_path += [os.path.join(p, n) for p in header_path]
        else:
            tvm_include_path = [os.path.join(p, name) for p in header_path]
        dlpack_include_path = []
    else:
        tvm_include_path = [os.path.join(p, 'include') for p in header_path]
        dlpack_include_path = [os.path.join(p, 'dlpack/include') for p in header_path]

        # try to find include path
        include_found = [p for p in tvm_include_path if os.path.exists(p) and os.path.isdir(p)]
        include_found += [p for p in dlpack_include_path if os.path.exists(p) and os.path.isdir(p)]

    if not include_found:
        message = ('Cannot find the files.\n' +
                   'List of candidates:\n' +
                   str('\n'.join(tvm_include_path + dlpack_include_path)))
        if not optional:
            raise RuntimeError(message)
        return None

    return include_found


tqchen committed
201
# current version
202 203 204
# We use the version of the incoming release for code
# that is under development.
# The following line is set by tvm/python/update_version.py
205
__version__ = "0.6.dev"