|
| 1 | +# Copyright 2024-2025 NVIDIA Corporation. All rights reserved. |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE |
| 4 | + |
| 5 | +import os |
| 6 | + |
| 7 | +from .find_dl_common import FindResult, get_cuda_paths_info, no_such_file_in_sub_dirs |
| 8 | +from .sys_path_find_sub_dirs import sys_path_find_sub_dirs |
| 9 | + |
| 10 | + |
| 11 | +def find_so_using_nvidia_lib_dirs(lib_searched_for: str) -> FindResult: |
| 12 | + """Find a .so file using NVIDIA library directories. |
| 13 | +
|
| 14 | + Args: |
| 15 | + lib_searched_for: The library name to search for |
| 16 | +
|
| 17 | + Returns: |
| 18 | + FindResult containing the search results |
| 19 | + """ |
| 20 | + result = FindResult(lib_searched_for) |
| 21 | + file_wild = f"lib{lib_searched_for}.so*" |
| 22 | + sub_dirs = ("lib", "lib64") |
| 23 | + |
| 24 | + for sub_dir in sys_path_find_sub_dirs(sub_dirs): |
| 25 | + for node in sorted(os.listdir(sub_dir)): |
| 26 | + if node.startswith(f"lib{lib_searched_for}.so"): |
| 27 | + result.abs_path = os.path.join(sub_dir, node) |
| 28 | + return result |
| 29 | + |
| 30 | + no_such_file_in_sub_dirs(sub_dirs, file_wild, result.error_messages, result.attachments) |
| 31 | + return result |
| 32 | + |
| 33 | + |
| 34 | +def find_so_using_cudalib_dir(lib_searched_for: str) -> FindResult: |
| 35 | + """Find a .so file using the CUDA library directory. |
| 36 | +
|
| 37 | + Args: |
| 38 | + lib_searched_for: The library name to search for |
| 39 | +
|
| 40 | + Returns: |
| 41 | + FindResult containing the search results |
| 42 | + """ |
| 43 | + result = FindResult(lib_searched_for) |
| 44 | + cudalib_dir = get_cuda_paths_info("cudalib_dir", result.error_messages) |
| 45 | + if not cudalib_dir: |
| 46 | + return result |
| 47 | + |
| 48 | + file_wild = f"lib{lib_searched_for}.so*" |
| 49 | + for node in sorted(os.listdir(cudalib_dir)): |
| 50 | + if node.startswith(f"lib{lib_searched_for}.so"): |
| 51 | + result.abs_path = os.path.join(cudalib_dir, node) |
| 52 | + return result |
| 53 | + |
| 54 | + result.error_messages.append(f"No such file: {file_wild}") |
| 55 | + result.attachments.append(f' listdir("{cudalib_dir}"):') |
| 56 | + for node in sorted(os.listdir(cudalib_dir)): |
| 57 | + result.attachments.append(f" {node}") |
| 58 | + return result |
| 59 | + |
| 60 | + |
| 61 | +def find_so_using_cuda_path(lib_searched_for: str) -> FindResult: |
| 62 | + """Find a .so file using the CUDA path. |
| 63 | +
|
| 64 | + Args: |
| 65 | + lib_searched_for: The library name to search for |
| 66 | +
|
| 67 | + Returns: |
| 68 | + FindResult containing the search results |
| 69 | + """ |
| 70 | + result = FindResult(lib_searched_for) |
| 71 | + cuda_path = get_cuda_paths_info("cuda_path", result.error_messages) |
| 72 | + if not cuda_path: |
| 73 | + return result |
| 74 | + |
| 75 | + file_wild = f"lib{lib_searched_for}.so*" |
| 76 | + for sub_dir in ("lib", "lib64"): |
| 77 | + path = os.path.join(cuda_path, sub_dir) |
| 78 | + if not os.path.isdir(path): |
| 79 | + continue |
| 80 | + for node in sorted(os.listdir(path)): |
| 81 | + if node.startswith(f"lib{lib_searched_for}.so"): |
| 82 | + result.abs_path = os.path.join(path, node) |
| 83 | + return result |
| 84 | + |
| 85 | + result.error_messages.append(f"No such file: {file_wild}") |
| 86 | + for sub_dir in ("lib", "lib64"): |
| 87 | + path = os.path.join(cuda_path, sub_dir) |
| 88 | + if os.path.isdir(path): |
| 89 | + result.attachments.append(f' listdir("{path}"):') |
| 90 | + for node in sorted(os.listdir(path)): |
| 91 | + result.attachments.append(f" {node}") |
| 92 | + return result |
| 93 | + |
| 94 | + |
| 95 | +def find_nvidia_dynamic_library(lib_searched_for: str) -> FindResult: |
| 96 | + """Find a NVIDIA dynamic library on Linux. |
| 97 | +
|
| 98 | + Args: |
| 99 | + lib_searched_for: The library name to search for |
| 100 | +
|
| 101 | + Returns: |
| 102 | + FindResult containing the search results |
| 103 | + """ |
| 104 | + # Try NVIDIA library directories first |
| 105 | + result = find_so_using_nvidia_lib_dirs(lib_searched_for) |
| 106 | + if result.abs_path: |
| 107 | + return result |
| 108 | + |
| 109 | + # Then try CUDA library directory |
| 110 | + result = find_so_using_cudalib_dir(lib_searched_for) |
| 111 | + if result.abs_path: |
| 112 | + return result |
| 113 | + |
| 114 | + # Finally try CUDA path |
| 115 | + result = find_so_using_cuda_path(lib_searched_for) |
| 116 | + return result |
0 commit comments