libpath.py 1.47 KB
Newer Older
wxchan's avatar
wxchan committed
1
# coding: utf-8
2
"""Find the path to LightGBM dynamic library files."""
3

4
5
import ctypes
from os import environ
6
from pathlib import Path
7
from platform import system
Deddy Jobson's avatar
Deddy Jobson committed
8
from typing import List
9

10
__all__: List[str] = []
11

wxchan's avatar
wxchan committed
12

13
def _find_lib_path() -> List[str]:
wxchan's avatar
wxchan committed
14
    """Find the path to LightGBM library files.
15

wxchan's avatar
wxchan committed
16
17
    Returns
    -------
18
    lib_path: list of str
19
       List of all found library paths to LightGBM.
wxchan's avatar
wxchan committed
20
    """
21
    curr_path = Path(__file__).resolve()
22
23
24
25
26
27
28
29
30
    dll_path = [
        curr_path.parents[1],
        curr_path.parents[0] / "bin",
        curr_path.parents[0] / "lib",
    ]
    if system() in ("Windows", "Microsoft"):
        dll_path.append(curr_path.parents[1] / "Release")
        dll_path.append(curr_path.parents[1] / "windows" / "x64" / "DLL")
        dll_path = [p / "lib_lightgbm.dll" for p in dll_path]
31
32
    elif system() == "Darwin":
        dll_path = [p / "lib_lightgbm.dylib" for p in dll_path]
wxchan's avatar
wxchan committed
33
    else:
34
        dll_path = [p / "lib_lightgbm.so" for p in dll_path]
35
    lib_path = [str(p) for p in dll_path if p.is_file()]
wxchan's avatar
wxchan committed
36
    if not lib_path:
37
38
        dll_path_joined = "\n".join(map(str, dll_path))
        raise Exception(f"Cannot find lightgbm library file in following paths:\n{dll_path_joined}")
wxchan's avatar
wxchan committed
39
    return lib_path
40
41
42
43
44
45
46
47
48
49


# we don't need lib_lightgbm while building docs
_LIB: ctypes.CDLL
if environ.get("LIGHTGBM_BUILD_DOC", False):
    from unittest.mock import Mock  # isort: skip

    _LIB = Mock(ctypes.CDLL)  # type: ignore
else:
    _LIB = ctypes.cdll.LoadLibrary(_find_lib_path()[0])