Skip to content

Commit 50de478

Browse files
committed
stubgen: add LIB_PATH option to locate dependent shared libraries
1 parent 77980ce commit 50de478

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

cmake/nanobind-config.cmake

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ endfunction()
590590
# ---------------------------------------------------------------------------
591591

592592
function (nanobind_add_stub name)
593-
cmake_parse_arguments(PARSE_ARGV 1 ARG "VERBOSE;INCLUDE_PRIVATE;EXCLUDE_DOCSTRINGS;INSTALL_TIME;RECURSIVE;EXCLUDE_FROM_ALL" "MODULE;COMPONENT;PATTERN_FILE;OUTPUT_PATH" "PYTHON_PATH;DEPENDS;MARKER_FILE;OUTPUT")
593+
cmake_parse_arguments(PARSE_ARGV 1 ARG "VERBOSE;INCLUDE_PRIVATE;EXCLUDE_DOCSTRINGS;INSTALL_TIME;RECURSIVE;EXCLUDE_FROM_ALL" "MODULE;COMPONENT;PATTERN_FILE;OUTPUT_PATH" "PYTHON_PATH;LIB_PATH;DEPENDS;MARKER_FILE;OUTPUT")
594594

595595
if (EXISTS ${NB_DIR}/src/stubgen.py)
596596
set(NB_STUBGEN "${NB_DIR}/src/stubgen.py")
@@ -622,6 +622,10 @@ function (nanobind_add_stub name)
622622
list(APPEND NB_STUBGEN_ARGS -i "${PYTHON_PATH}")
623623
endforeach()
624624

625+
foreach (LIB_PATH IN LISTS ARG_LIB_PATH)
626+
list(APPEND NB_STUBGEN_ARGS -L "${LIB_PATH}")
627+
endforeach()
628+
625629
if (ARG_PATTERN_FILE)
626630
list(APPEND NB_STUBGEN_ARGS -p "${ARG_PATTERN_FILE}")
627631
endif()

src/stubgen.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,16 @@ def parse_options(args: List[str]) -> argparse.Namespace:
12631263
help="add the directory to the Python import path (can specify multiple times)",
12641264
)
12651265

1266+
parser.add_argument(
1267+
"-L",
1268+
"--lib-path",
1269+
action="append",
1270+
metavar="PATH",
1271+
dest="lib_paths",
1272+
default=[],
1273+
help="add directory to shared library search path (can specify multiple times)"
1274+
)
1275+
12661276
parser.add_argument(
12671277
"-m",
12681278
"--module",
@@ -1396,6 +1406,7 @@ def add_pattern(query: str, lines: List[str]):
13961406

13971407
def main(args: Optional[List[str]] = None) -> None:
13981408
import sys
1409+
import os
13991410

14001411
# Ensure that the current directory is on the path
14011412
if "" not in sys.path and "." not in sys.path:
@@ -1416,6 +1427,16 @@ def main(args: Optional[List[str]] = None) -> None:
14161427
for i in opt.imports:
14171428
sys.path.insert(0, i)
14181429

1430+
if os.name == 'nt':
1431+
for lib_path in opt.lib_paths:
1432+
os.add_dll_directory(lib_path)
1433+
else:
1434+
lib_env = "DYLD_LIBRARY_PATH" if sys.platform == "darwin" else "LD_LIBRARY_PATH"
1435+
old_value = os.environ.get(lib_env, "")
1436+
paths_str = ":".join(opt.lib_paths)
1437+
if paths_str:
1438+
os.environ[lib_env] = f"{paths_str}:{old_value}" if old_value else paths_str
1439+
14191440
for i, mod in enumerate(opt.modules):
14201441
if not opt.quiet:
14211442
if i > 0:

0 commit comments

Comments
 (0)