junrushao commented on code in PR #306:
URL: https://github.com/apache/tvm-ffi/pull/306#discussion_r2595797624
##########
python/tvm_ffi/libinfo.py:
##########
@@ -22,78 +22,152 @@
from __future__ import annotations
+import ctypes
+import importlib.metadata as im
import os
import sys
from pathlib import Path
+from typing import Callable
-def split_env_var(env_var: str, split: str) -> list[str]:
- """Split an environment variable string.
-
- Parameters
- ----------
- env_var
- Name of environment variable.
-
- split
- String to split env_var on.
+def find_libtvm_ffi() -> str:
+ """Find libtvm_ffi.
Returns
-------
- splits
- If env_var exists, split env_var. Otherwise, empty list.
+ path
+ The full path to the located library.
"""
- if os.environ.get(env_var, None):
- return [p.strip() for p in os.environ[env_var].split(split)]
- return []
+ candidate = _find_library_by_basename("apache-tvm-ffi", "tvm_ffi")
+ if ret := _resolve_and_validate([candidate], cond=lambda _: True):
+ return ret
+ raise RuntimeError("Cannot find libtvm_ffi")
-def get_dll_directories() -> list[str]:
- """Get the possible dll directories."""
- ffi_dir = Path(__file__).expanduser().resolve().parent
- dll_path: list[Path] = [ffi_dir / "lib"]
- dll_path.append(ffi_dir / ".." / ".." / "build" / "lib")
- # in source build from parent if needed
- dll_path.append(ffi_dir / ".." / ".." / ".." / "build" / "lib")
- if sys.platform.startswith("linux") or sys.platform.startswith("freebsd"):
- dll_path.extend(Path(p) for p in split_env_var("LD_LIBRARY_PATH", ":"))
- dll_path.extend(Path(p) for p in split_env_var("PATH", ":"))
- elif sys.platform.startswith("darwin"):
- dll_path.extend(Path(p) for p in split_env_var("DYLD_LIBRARY_PATH",
":"))
- dll_path.extend(Path(p) for p in split_env_var("PATH", ":"))
- elif sys.platform.startswith("win32"):
- dll_path.extend(Path(p) for p in split_env_var("PATH", ";"))
+def find_windows_implib() -> str:
+ """Find and return the Windows import library path for tvm_ffi.lib."""
+ # implib = _find_library_by_basename("apache-tvm-ffi", "tvm_ffi").parent /
"tvm_ffi.lib"
+ # ret = _resolve_to_str(implib)
+ candidate = _find_library_by_basename("apache-tvm-ffi", "tvm_ffi").parent
/ "tvm_ffi.lib"
+ if ret := _resolve_and_validate([candidate], cond=lambda _: True):
+ return ret
+ raise RuntimeError("Cannot find implib tvm_ffi.lib")
- valid_paths = []
- for path in dll_path:
- try:
- if path.is_dir():
- valid_paths.append(str(path.resolve()))
- except OSError:
- # need to ignore as resolve may fail if
- # we don't have permission to access it
- pass
- return valid_paths
+def find_source_path() -> str:
+ """Find packaged source home path."""
+ if ret := _resolve_and_validate(
+ paths=[
+ _rel_top_directory(),
+ _dev_top_directory(),
+ ],
+ cond=lambda p: (p / "cmake").is_dir(),
+ ):
+ return ret
+ raise RuntimeError("Cannot find home path.")
-def find_libtvm_ffi() -> str:
- """Find libtvm_ffi.
- Returns
- -------
- path
- The full path to the located library.
+def find_cmake_path() -> str:
+ """Find the preferred cmake path."""
+ if ret := _resolve_and_validate(
+ paths=[
+ _rel_top_directory() / "share" / "cmake" / "tvm_ffi", # Standard
install
+ _dev_top_directory() / "cmake", # Development mode
+ ],
+ cond=lambda p: p.is_dir(),
+ ):
+ return ret
+ raise RuntimeError("Cannot find cmake path.")
- """
- return find_library_by_basename("tvm_ffi")
+
+def find_include_path() -> str:
+ """Find header files for C compilation."""
+ if ret := _resolve_and_validate(
+ paths=[
+ _rel_top_directory() / "include",
+ _dev_top_directory() / "include",
+ ],
+ cond=lambda p: p.is_dir(),
+ ):
+ return ret
+ raise RuntimeError("Cannot find include path.")
+
+
+def find_dlpack_include_path() -> str:
+ """Find dlpack header files for C compilation."""
+ if ret := _resolve_and_validate(
+ paths=[
+ _rel_top_directory() / "include",
+ _dev_top_directory() / "3rdparty" / "dlpack" / "include",
+ ],
+ cond=lambda p: (p / "dlpack").is_dir(),
+ ):
+ return ret
+ raise RuntimeError("Cannot find dlpack include path.")
+
+
+def find_cython_lib() -> str:
+ """Find the path to tvm cython."""
+ suffixes = "pyd" if sys.platform.startswith("win32") else "so"
+
+ def _cond(p: Path) -> Path | bool:
+ for path in p.glob(f"core*.{suffixes}"):
+ if path.is_file():
+ return path
+ return False
+
+ if ret := _resolve_and_validate(
+ paths=[
+ _rel_top_directory(),
+ _dev_top_directory() / "build",
+ ],
+ cond=_cond,
+ ):
+ return ret
+ raise RuntimeError("Cannot find tvm cython path.")
+
+
+def find_python_helper_include_path() -> str:
+ """Find header files for C compilation."""
+ if ret := _resolve_and_validate(
+ paths=[
+ _rel_top_directory() / "cython",
+ _dev_top_directory() / "python" / "tvm_ffi",
+ ],
+ cond=lambda p: (p / "tvm_ffi_python_helpers.h").is_file(),
+ ):
+ return ret
+ raise RuntimeError("Cannot find python helper include path.")
+
+
+def include_paths() -> list[str]:
+ """Find all include paths needed for FFI related compilation."""
+ return sorted(
Review Comment:
sorted is even better given it's always deterministic across platforms and
python versions
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]