Skip to content

Commit a3133fb

Browse files
committed
load libc
1 parent 5785f37 commit a3133fb

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

backends/qualcomm/scripts/download_qnn_sdk.py

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,22 @@ def is_linux_x86() -> bool:
3838
)
3939

4040

41-
REQUIRED_QNN_LIBS = ["libc.so.6"]
42-
4341
import subprocess
4442

43+
REQUIRED_LIBC_LIBS = [
44+
"/lib/x86_64-linux-gnu/libc.so.6",
45+
"/lib64/libc.so.6",
46+
"/lib/libc.so.6",
47+
]
48+
4549

4650
def check_glibc_exist() -> bool:
4751
"""
4852
Check if users have glibc installed.
4953
"""
5054
print("[QNN] Checking glibc exist running on Linux x86")
51-
paths = ["/lib/x86_64-linux-gnu/libc.so.6", "/lib64/libc.so.6", "/lib/libc.so.6"]
5255

53-
for path in paths:
56+
for path in REQUIRED_LIBC_LIBS:
5457
try:
5558
output = subprocess.check_output(
5659
[path, "--version"], stderr=subprocess.STDOUT
@@ -59,7 +62,7 @@ def check_glibc_exist() -> bool:
5962
print(output.decode().split("\n")[0])
6063
except Exception:
6164
continue
62-
exists = any(os.path.isfile(p) for p in paths)
65+
exists = any(os.path.isfile(p) for p in REQUIRED_LIBC_LIBS)
6366
if not exists:
6467
logger.error(
6568
r""""
@@ -81,6 +84,19 @@ def check_glibc_exist() -> bool:
8184
return exists
8285

8386

87+
def _load_libc_libs() -> bool:
88+
logger.debug("[QNN] running _load_libc_libs")
89+
logger.debug("[QNN] REQUIRED_LIBC_LIBS: : %s", REQUIRED_LIBC_LIBS)
90+
for sofile in REQUIRED_LIBC_LIBS:
91+
try:
92+
ctypes.CDLL(str(sofile), mode=ctypes.RTLD_GLOBAL)
93+
logger.info("Loaded %s", sofile)
94+
return True
95+
except OSError as e:
96+
logger.warning("[WARN] Failed to load %s: %s", sofile, e)
97+
return False
98+
99+
84100
def _download_archive(url: str, archive_path: pathlib.Path) -> bool:
85101
"""Download archive from URL with progress reporting."""
86102
logger.debug("Archive will be saved to: %s", archive_path)
@@ -159,7 +175,7 @@ def _download_qnn_sdk(dst_folder=SDK_DIR) -> Optional[pathlib.Path]:
159175
)
160176
QAIRT_CONTENT_DIR = f"qairt/{QNN_VERSION}"
161177

162-
if not is_linux_x86() or not check_glibc_exist():
178+
if not is_linux_x86() or not check_glibc_exist(REQUIRED_LIBC_LIBS):
163179
logger.info("Skipping Qualcomm SDK (only supported on Linux x86).")
164180
return None
165181
else:
@@ -400,6 +416,23 @@ def _ensure_libcxx_stack() -> bool:
400416
return lib_loaded
401417

402418

419+
# ---------------------
420+
# Ensure libc family
421+
# ---------------------
422+
def _ensure_libc_stack() -> bool:
423+
"""
424+
Ensure libc stack is available.
425+
"""
426+
exist = check_glibc_exist()
427+
lib_loaded = False
428+
if exist:
429+
logger.info("[libc] glibc exists; start loading.")
430+
lib_loaded = _load_libc_libs()
431+
else:
432+
logger.error("[libc] glibc does not exist; skipping loading.")
433+
return lib_loaded
434+
435+
403436
# ---------------
404437
# Public entrypoint
405438
# ---------------
@@ -420,4 +453,5 @@ def install_qnn_sdk() -> bool:
420453
"""
421454
ok_libcxx = _ensure_libcxx_stack()
422455
ok_qnn = _ensure_qnn_sdk_lib()
423-
return bool(ok_qnn and ok_libcxx)
456+
ok_libc = _ensure_libc_stack()
457+
return bool(ok_qnn and ok_libcxx and ok_libc)

0 commit comments

Comments
 (0)