Skip to content

Commit 8fe177e

Browse files
committed
temp for sdk and fix libcxx
1 parent bd0ae6a commit 8fe177e

File tree

3 files changed

+73
-97
lines changed

3 files changed

+73
-97
lines changed

backends/qualcomm/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
# If users have preinstalled QNN_SDK_ROOT, we will use it.
77
qnn_sdk_root_flag = os.getenv("QNN_SDK_ROOT", None)
88

9+
print("evn_flag: ", env_flag, "qnn_sdk_root_flag: ", qnn_sdk_root_flag, "qnn_sdk_root")
910
if not env_flag in ("1", "true", "yes") and not qnn_sdk_root_flag:
11+
print("############ Installing QNN SDK ############")
1012
ok = install_qnn_sdk()
1113
if not ok:
1214
raise RuntimeError("Failed to install QNN SDK. Please check the logs above.")

backends/qualcomm/scripts/download_qnn_sdk.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ def get_qnn_version() -> str:
3838
return QNN_VERSION
3939

4040

41-
def _download_qnn_sdk() -> Optional[pathlib.Path]:
41+
def _download_qnn_sdk(dst_folder=SDK_DIR) -> Optional[pathlib.Path]:
4242
"""
43-
Download and extract the Qualcomm SDK into SDK_DIR.
43+
Download and extract the Qualcomm SDK into dst_folder.
4444
4545
Notes:
4646
- Only runs on Linux x86 platforms. Skips otherwise.
@@ -53,8 +53,8 @@ def _download_qnn_sdk() -> Optional[pathlib.Path]:
5353
print("Skipping Qualcomm SDK (only supported on Linux x86).")
5454
return None
5555

56-
SDK_DIR.mkdir(parents=True, exist_ok=True)
57-
print(f"SDK_DIR is {SDK_DIR}, exists: {SDK_DIR.exists()}")
56+
dst_folder.mkdir(parents=True, exist_ok=True)
57+
print(f"dst_folder is {dst_folder}, exists: {dst_folder.exists()}")
5858
print(f"Current working directory: {os.getcwd()}")
5959

6060
with tempfile.TemporaryDirectory() as tmpdir:
@@ -95,24 +95,24 @@ def report_progress(block_num, block_size, total_size):
9595

9696
if QAIRT_URL.endswith(".zip"):
9797
print("Extracting ZIP archive...")
98-
_extract_zip(archive_path, QAIRT_CONTENT_DIR, SDK_DIR)
98+
_extract_zip(archive_path, QAIRT_CONTENT_DIR, dst_folder)
9999
elif QAIRT_URL.endswith((".tar.gz", ".tgz")):
100100
print("Extracting TAR archive...")
101-
_extract_tar(archive_path, QAIRT_CONTENT_DIR, SDK_DIR)
101+
_extract_tar(archive_path, QAIRT_CONTENT_DIR, dst_folder)
102102
else:
103103
raise ValueError(f"Unsupported archive format: {QAIRT_URL}")
104104

105-
print(f"Verifying extraction to {SDK_DIR}")
106-
if SDK_DIR.exists():
105+
print(f"Verifying extraction to {dst_folder}")
106+
if dst_folder.exists():
107107
print(f"SDK directory exists. Contents:")
108-
for item in SDK_DIR.iterdir():
108+
for item in dst_folder.iterdir():
109109
print(f" {item.name}")
110110
else:
111111
print("ERROR: SDK directory was not created!")
112112

113-
print(f"Qualcomm SDK extracted to {SDK_DIR}")
113+
print(f"Qualcomm SDK extracted to {dst_folder}")
114114

115-
return SDK_DIR
115+
return dst_folder
116116

117117

118118
def _extract_zip(archive_path, content_dir, target_dir):
@@ -166,8 +166,8 @@ def _extract_tar(archive_path: pathlib.Path, prefix: str, target_dir: pathlib.Pa
166166
"libc++.so.1.0",
167167
"libc++abi.so.1.0",
168168
"libunwind.so.1",
169-
"libm.so.6",
170-
"libpython3.10.so.1.0",
169+
# "libm.so.6",
170+
# "libpython3.10.so.1.0",
171171
]
172172

173173

@@ -189,23 +189,16 @@ def _stage_libcxx(target_dir: pathlib.Path):
189189
with tarfile.open(temp_tar, "r:xz") as tar:
190190
tar.extractall(temp_extract.parent)
191191

192-
lib_src = temp_extract / "lib"
192+
lib_src = temp_extract / "lib" / "x86_64-unknown-linux-gnu"
193193
for fname in REQUIRED_LIBCXX_LIBS:
194194
src_path = lib_src / fname
195195
if not src_path.exists():
196-
print(f"[libcxx] Warning: {fname} not found in extracted LLVM")
196+
print(
197+
f"[libcxx] Warning: {fname} not found in extracted LLVM src_path {src_path}"
198+
)
197199
continue
198200
shutil.copy(src_path, target_dir / fname)
199201

200-
libcxx = target_dir / "libc++.so.1.0"
201-
libcxx_abi = target_dir / "libc++abi.so.1.0"
202-
if libcxx.exists():
203-
os.symlink("libc++.so.1.0", target_dir / "libc++.so.1")
204-
os.symlink("libc++.so.1", target_dir / "libc++.so")
205-
if libcxx_abi.exists():
206-
os.symlink("libc++abi.so.1.0", target_dir / "libc++abi.so.1")
207-
os.symlink("libc++abi.so.1", target_dir / "libc++abi.so")
208-
209202
print(f"[libcxx] Staged libc++ to {target_dir}")
210203

211204

@@ -294,13 +287,14 @@ def _ensure_qnn_sdk_lib() -> bool:
294287

295288

296289
def _load_libcxx_libs(lib_path):
290+
print("running _load_libcxx_libs")
297291
candidates = list(lib_path.glob("*.so*"))
298292
priority = ["libc++abi", "libc++"]
299293
sorted_candidates = [
300294
f for name in priority for f in candidates if f.name.startswith(name)
301295
]
302296
sorted_candidates += [f for f in candidates if f not in sorted_candidates]
303-
297+
print("sorted_candidates: ", sorted_candidates)
304298
for sofile in sorted_candidates:
305299
try:
306300
ctypes.CDLL(str(sofile), mode=ctypes.RTLD_GLOBAL)
@@ -357,6 +351,6 @@ def install_qnn_sdk() -> bool:
357351
Returns:
358352
True if both steps succeeded (or were already satisfied), else False.
359353
"""
360-
ok_qnn = _ensure_qnn_sdk_lib()
361354
ok_libcxx = _ensure_libcxx_stack()
355+
ok_qnn = _ensure_qnn_sdk_lib()
362356
return bool(ok_qnn and ok_libcxx)

setup.py

Lines changed: 51 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -466,82 +466,62 @@ def run(self):
466466

467467
try:
468468
# Following code is for building the Qualcomm backend.
469-
from backends.qualcomm.scripts.download_qnn_sdk import (
470-
_download_qnn_sdk,
471-
SDK_DIR,
472-
)
469+
from backends.qualcomm.scripts.download_qnn_sdk import _download_qnn_sdk
473470

474471
os.environ["EXECUTORCH_BUILDING_WHEEL"] = "1"
475472

476-
# qnn sdk setup
477-
_download_qnn_sdk()
478-
479-
sdk_path = Path(SDK_DIR).resolve() # full absolute path
480-
print("sdk_path: ", sdk_path)
481-
if not sdk_path:
482-
raise RuntimeError("Qualcomm SDK not found, cannot build backend")
483-
484-
# Determine paths
485-
prj_root = Path(__file__).parent.resolve()
486-
print("prj_root: ", prj_root)
487-
# TODO: stop using script but add cmake configuration properly
488-
build_sh = prj_root / "backends/qualcomm/scripts/build.sh"
489-
build_root = prj_root / "build-x86"
490-
491-
if not build_sh.exists():
492-
raise FileNotFoundError(f"{build_sh} not found")
493-
494-
# Run build.sh with SDK path exported
495-
env = dict(**os.environ)
496-
print("str(sdk_path): ", str(sdk_path))
497-
env["QNN_SDK_ROOT"] = str(sdk_path)
498-
subprocess.check_call([str(build_sh), "--skip_aarch64"], env=env)
499-
500-
# Copy the main .so into the wheel package
501-
so_src = build_root / "backends/qualcomm/libqnn_executorch_backend.so"
502-
so_dst = Path(
503-
self.get_ext_fullpath("executorch.backends.qualcomm.qnn_backend")
504-
)
505-
self.mkpath(so_dst.parent) # ensure destination exists
506-
self.copy_file(str(so_src), str(so_dst))
507-
print(f"Copied Qualcomm backend: {so_src} -> {so_dst}")
508-
509-
# --- CLEANUP SECTION ---
510-
# 1. Remove Qualcomm SDK staging folder so it doesn’t get packaged
511-
if os.path.exists(SDK_DIR):
512-
try:
513-
shutil.rmtree(SDK_DIR)
514-
print(f"Removed Qualcomm SDK folder: {SDK_DIR}")
515-
except Exception as e:
516-
print(f"Failed to remove SDK_DIR {SDK_DIR}: {e}")
517-
518-
# 2. Also remove the entire packaged SDK tree
519-
pkg_sdk_dir = prj_root / "backends/qualcomm/sdk"
520-
if pkg_sdk_dir.exists():
521-
try:
522-
shutil.rmtree(pkg_sdk_dir)
523-
print(f"Removed packaged Qualcomm SDK: {pkg_sdk_dir}")
524-
except Exception as e:
525-
print(f"Failed to remove packaged SDK dir {pkg_sdk_dir}: {e}")
526-
527-
so_files = [
528-
(
529-
"executorch.backends.qualcomm.python.PyQnnManagerAdaptor",
530-
prj_root
531-
/ "backends/qualcomm/python/PyQnnManagerAdaptor.cpython-310-x86_64-linux-gnu.so",
532-
),
533-
(
534-
"executorch.backends.qualcomm.python.PyQnnWrapperAdaptor",
535-
prj_root
536-
/ "backends/qualcomm/python/PyQnnWrapperAdaptor.cpython-310-x86_64-linux-gnu.so",
537-
),
538-
]
539-
540-
for module_name, so_src in so_files:
541-
so_dst = Path(self.get_ext_fullpath(module_name))
542-
self.mkpath(str(so_dst.parent))
473+
with tempfile.TemporaryDirectory() as tmpdir:
474+
tmp_path = Path(tmpdir)
475+
sdk_path = _download_qnn_sdk(dst_folder=tmp_path)
476+
477+
print("sdk_path: ", sdk_path)
478+
if not sdk_path:
479+
raise RuntimeError("Qualcomm SDK not found, cannot build backend")
480+
481+
# Determine paths
482+
prj_root = Path(__file__).parent.resolve()
483+
print("prj_root: ", prj_root)
484+
build_sh = prj_root / "backends/qualcomm/scripts/build.sh"
485+
build_root = prj_root / "build-x86"
486+
487+
if not build_sh.exists():
488+
raise FileNotFoundError(f"{build_sh} not found")
489+
490+
# Run build.sh with SDK path exported
491+
env = dict(**os.environ)
492+
print("str(sdk_path): ", str(sdk_path))
493+
env["QNN_SDK_ROOT"] = str(sdk_path)
494+
subprocess.check_call([str(build_sh), "--skip_aarch64"], env=env)
495+
496+
# Copy the main .so into the wheel package
497+
so_src = build_root / "backends/qualcomm/libqnn_executorch_backend.so"
498+
so_dst = Path(
499+
self.get_ext_fullpath("executorch.backends.qualcomm.qnn_backend")
500+
)
501+
self.mkpath(so_dst.parent) # ensure destination exists
543502
self.copy_file(str(so_src), str(so_dst))
544503
print(f"Copied Qualcomm backend: {so_src} -> {so_dst}")
504+
505+
# Copy Python adaptor .so files
506+
so_files = [
507+
(
508+
"executorch.backends.qualcomm.python.PyQnnManagerAdaptor",
509+
prj_root
510+
/ "backends/qualcomm/python/PyQnnManagerAdaptor.cpython-310-x86_64-linux-gnu.so",
511+
),
512+
(
513+
"executorch.backends.qualcomm.python.PyQnnWrapperAdaptor",
514+
prj_root
515+
/ "backends/qualcomm/python/PyQnnWrapperAdaptor.cpython-310-x86_64-linux-gnu.so",
516+
),
517+
]
518+
519+
for module_name, so_src in so_files:
520+
so_dst = Path(self.get_ext_fullpath(module_name))
521+
self.mkpath(str(so_dst.parent))
522+
self.copy_file(str(so_src), str(so_dst))
523+
print(f"Copied Qualcomm backend: {so_src} -> {so_dst}")
524+
545525
except ImportError:
546526
print("Fail to build Qualcomm backend")
547527
print("Import error: ", sys.exc_info()[0])

0 commit comments

Comments
 (0)