Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
1082 lines (945 loc) · 43.4 KB
/
Copy pathsetup.py
File metadata and controls
1082 lines (945 loc) · 43.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# =============================================================================
#
# Copyright (c) 2023, Qualcomm Innovation Center, Inc. All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#
# =============================================================================
#
# Modern build notes:
# - Prefer: python -m build (PEP 517) instead of python setup.py bdist_wheel
# - Still supported: python setup.py bdist_wheel
#
# Example:
# [windows - WOS ARM64 device]
# set QNN_SDK_ROOT=C:/Qualcomm/AIStack/QAIRT/2.42.0.251225/
# set QAI_TOOLCHAINS=aarch64-windows-msvc (For ARM64 Windows Python) [or] set QAI_TOOLCHAINS=arm64x-windows-msvc (For AMD(X64) Windows Python)
# set QAI_HEXAGONARCH=81
#
# python -m build -w
#
# [windows - x86 PC (non-WOS)]
# set QNN_SDK_ROOT=C:/Qualcomm/AIStack/QAIRT/2.42.0.251225/
# set QAI_TOOLCHAINS=x86_64-windows-msvc
# python -m build -w
#
# [linux]
# export QNN_SDK_ROOT=~/QAIRT/2.38.0.250901/
# python -m build -w
#
# [linux - cross-compile for aarch64]
# export QNN_SDK_ROOT=~/QAIRT/2.38.0.250901/
# export QAI_TOOLCHAINS=aarch64-oe-linux-gcc11.2
# export QAI_CMAKE_TOOLCHAIN_FILE=~/toolchain-aarch64.cmake
# export QAI_DSP_ARCHES=73,75,79 (optional - defaults to a per-OS list)
# python -m build -w
# =============================================================================
import os
import platform
import re
import shutil
import subprocess
import sys
import zipfile
from pathlib import Path
import warnings
from typing import Optional
from setuptools import Extension, setup, find_packages
from setuptools.command.build_ext import build_ext
from setuptools.command.bdist_wheel import bdist_wheel
# ---------------------------
# Project constants
# ---------------------------
VERSION = "2.51.40"
CONFIG = "Release" # Release, RelWithDebInfo
PACKAGE_NAME = "qai_appbuilder"
# -----------------------------------------------------------------------------
# Silence setuptools warning banner when invoking legacy "python setup.py ..."
#
# When running:
# python setup.py bdist_wheel ...
# setuptools emits a noisy SetuptoolsDeprecationWarning:
# "setup.py install is deprecated."
# with a long banner.
#
# We only filter it when this file is executed as a script (__main__),
# so PEP517 builds (e.g. "python -m build -w") are unaffected.
# -----------------------------------------------------------------------------
if __name__ == "__main__":
warnings.filterwarnings(
"ignore",
message=r".*setup\.py install is deprecated\..*",
category=Warning,
)
# ---------------------------
# Helpers
# ---------------------------
def _extract_semver3_from_text(text: str) -> Optional[str]:
"""
Extract first 'X.Y.Z' (three numeric dot-separated components) from a string.
Example: 'C:/.../2.42.0.251225/' -> '2.42.0'
"""
if not text:
return None
m = re.search(r"(\d+)\.(\d+)\.(\d+)", text)
if not m:
return None
return f"{m.group(1)}.{m.group(2)}.{m.group(3)}"
def _extract_semver4_from_text(text: str) -> Optional[str]:
"""
Extract 'X.Y.Z.DATE' (4 numeric dot-separated components) from a string.
Example: 'C:/.../2.42.0.251225/' -> '2.42.0.251225'
"""
if not text:
return None
m = re.search(r"(\d+\.\d+\.\d+\.\d+)", text)
return m.group(1) if m else None
def _get_base_version_from_qnn_sdk_root(default: str) -> str:
"""
Prefer extracting base version from QNN_SDK_ROOT path; fallback to provided default.
"""
qnn_root = os.environ.get("QNN_SDK_ROOT", "")
v = _extract_semver3_from_text(qnn_root)
return v if v else default
def _get_hexagonarch_from_argv() -> Optional[str]:
"""
Parse legacy setup.py options early so wheel metadata version can include DSP suffix.
Supports: --hexagonarch 81 OR --hexagonarch=81
"""
argv = sys.argv[1:]
for i, a in enumerate(argv):
if a == "--hexagonarch" and i + 1 < len(argv):
return argv[i + 1]
if a.startswith("--hexagonarch="):
return a.split("=", 1)[1]
return None
def _project_root() -> Path:
return Path(__file__).resolve().parent
def _is_windows() -> bool:
return sys.platform.startswith("win")
def _require_cmake():
"""Fail fast with a readable error if cmake is not available."""
if shutil.which("cmake") is None:
raise RuntimeError("cmake executable not found in PATH. Please install CMake and ensure it's available.")
def _is_wos_device() -> bool:
"""
Detect whether we are running on a Windows on Snapdragon (WOS) device.
On WOS, the underlying CPU is ARM64 (Qualcomm Snapdragon), but x64 Python
reports platform.machine() == 'AMD64' due to emulation.
We distinguish WOS from a real x86_64 PC by checking the processor string
and PROCESSOR_IDENTIFIER / PROCESSOR_ARCHITECTURE environment variables.
Returns True if running on a WOS (ARM64) device, False on a real x86_64 PC.
"""
# Explicit user request via QAI_TOOLCHAINS overrides host detection.
# QAI_TOOLCHAINS=x86_64-windows-msvc means the user explicitly wants an
# x86_64 build, even on a WOS device - treat it as non-WOS so that
# _detect_arch() returns "x86_64" and CMake uses -A x64.
qai_toolchains = os.environ.get("QAI_TOOLCHAINS", "").lower()
if qai_toolchains == "x86_64-windows-msvc":
return False
# arm64x / aarch64 toolchains explicitly request a WOS/ARM build.
if qai_toolchains in ("arm64x-windows-msvc", "aarch64-windows-msvc"):
return True
# Check PROCESSOR_ARCHITECTURE env var (set by Windows)
proc_arch = os.environ.get("PROCESSOR_ARCHITECTURE", "").upper()
proc_id = os.environ.get("PROCESSOR_IDENTIFIER", "").upper()
processor = platform.processor().upper()
print(f"proc_arch:{proc_arch}")
print(f"proc_id:{proc_id}")
print(f"processor:{processor}")
# ARM indicators in processor info
arm_keywords = ("ARM", "QUALCOMM", "SNAPDRAGON", "ORYON")
for kw in arm_keywords:
if kw in proc_id or kw in processor:
return True
# On WOS, PROCESSOR_ARCHITECTURE is typically "ARM64" even for x64 processes
if proc_arch == "ARM64":
return True
return False
def _detect_arch() -> str:
"""
Detect the target build architecture:
- On Linux: aarch64 or x86_64
- On Windows WOS (ARM64 Snapdragon) device:
- If running x64 Python (AMD64) => "ARM64EC"
- Else => "ARM64"
- On Windows x86_64 PC (non-WOS) => "x86_64"
"""
machine = platform.machine()
sysinfo = sys.version
print(f"machine={machine}")
print(f"sysinfo={sysinfo}")
if not _is_windows():
if machine in {"aarch64", "arm64"}:
return "aarch64"
if machine in {"x86_64", "AMD64"}:
return "x86_64-linux"
# Windows: distinguish WOS (ARM64 CPU) from real x86_64 PC
if _is_wos_device():
# WOS device: ARM64 CPU
if machine == "AMD64" or ("AMD64" in sysinfo):
return "ARM64EC"
return "ARM64"
else:
# Real x86_64 Windows PC (non-WOS)
return "x86_64"
def _default_generator_and_args(arch: str):
"""
Return (generator_args:list[str], is_multi_config:bool)
"""
if not _is_windows():
return ([], False)
# Allow overriding the VS generator (e.g. "Visual Studio 18 2026").
# If unset, omit -G entirely so CMake auto-detects the newest installed
# Visual Studio (works for VS 2022, 2026, ... without hardcoding a version).
vs_gen = os.environ.get("QAI_VS_GENERATOR")
gen_flag = ["-G", vs_gen] if vs_gen else []
if arch == "x86_64":
# x86_64 Windows PC: x64 platform
gen = gen_flag + ["-A", "x64"]
return (gen, True)
# WOS ARM64 / ARM64EC: multi-config VS generator
gen = gen_flag + ["-A", arch]
return (gen, True)
def _cmake_python_hints_args() -> list:
"""
Preserve your ARM64EC + x64 Python workaround:
- Force pybind11 to compat mode (avoid FindPython arch check)
- Provide Python executable via multiple variable names
"""
py = os.environ.get("PYTHON_X64_EXECUTABLE", sys.executable)
py = str(Path(py))
return [
"-DPYBIND11_FINDPYTHON=COMPAT",
f"-DPYBIND11_PYTHON_VERSION={sys.version_info.major}.{sys.version_info.minor}",
f"-DPython_EXECUTABLE={py}",
f"-DPython3_EXECUTABLE={py}",
f"-DPYTHON_EXECUTABLE={py}",
]
def _safe_rmtree(p: Path):
shutil.rmtree(p, ignore_errors=True)
def _ensure_file(path: Path, content: str):
path.parent.mkdir(parents=True, exist_ok=True)
if not path.exists():
path.write_text(content, encoding="utf-8")
def _copy_if_exists(src: Path, dst: Path):
if src.exists():
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(src, dst)
def _zip_dir(dirpath: Path, out_fullname: Path):
out_fullname.parent.mkdir(parents=True, exist_ok=True)
with zipfile.ZipFile(out_fullname, "w", zipfile.ZIP_DEFLATED) as zf:
for p in dirpath.rglob("*"):
if p.is_file():
zf.write(p, p.relative_to(dirpath))
def _get_qnn_sdk_root() -> Path:
v = os.environ.get("QNN_SDK_ROOT")
if not v:
raise RuntimeError('QNN_SDK_ROOT environmental variable not set')
p = Path(v)
if not p.exists() or not p.is_dir():
raise RuntimeError(f'QNN_SDK_ROOT="{v}" does not exist or is not a directory')
return p
def _parse_toolchain_system_processor(toolchain_file: str) -> Optional[tuple]:
"""
Parse CMAKE_SYSTEM_NAME and CMAKE_SYSTEM_PROCESSOR from a CMake toolchain
file. Returns (system, processor) or None if either is absent.
Only looks at set() calls with literal string arguments - does not evaluate CMake.
"""
try:
text = Path(toolchain_file).read_text(encoding="utf-8")
except OSError:
return None
def _cmake_var(name: str) -> Optional[str]:
m = re.search(rf'set\s*\(\s*{name}\s+"?([^"\s\)]+)"?', text)
return m.group(1) if m else None
system = _cmake_var("CMAKE_SYSTEM_NAME")
processor = _cmake_var("CMAKE_SYSTEM_PROCESSOR")
if not system or not processor:
return None
return system, processor
def _host_platform_from_toolchain(toolchain_file: str) -> Optional[str]:
"""
Return a distutils platform string (e.g. 'linux-aarch64') derived from
the toolchain file's CMAKE_SYSTEM_NAME/CMAKE_SYSTEM_PROCESSOR, or None
if either is absent or the combination is unrecognised.
"""
parsed = _parse_toolchain_system_processor(toolchain_file)
if not parsed:
return None
system, processor = parsed
if system.lower() == "linux":
return f"linux-{processor}"
if system.lower() == "android":
return f"linux-{processor}"
if system.lower() == "windows":
return f"win-{processor}"
return None
def _pybind11_cross_ext_args(toolchain_file: str) -> list:
"""
pybind11's FindPythonLibsNew.cmake (PYBIND11_FINDPYTHON=COMPAT) determines
PYTHON_MODULE_EXTENSION by executing PYTHON_EXECUTABLE and reading its
EXT_SUFFIX - but PYTHON_EXECUTABLE is the build-host interpreter, so that
suffix is for the host arch, not the cross-compilation target. Compute the
target suffix from the running interpreter's version plus the toolchain's
target triplet, and force it via cmake args (mirrors the PYBIND11_PYTHONLIBS_OVERWRITE
escape hatch documented in FindPythonLibsNew.cmake).
"""
parsed = _parse_toolchain_system_processor(toolchain_file)
if not parsed:
return []
system, processor = parsed
if system.lower() != "linux":
return []
ext_suffix = f".cpython-{sys.version_info.major}{sys.version_info.minor}-{processor}-linux-gnu.so"
return [
"-DPYBIND11_PYTHONLIBS_OVERWRITE=OFF",
f"-DPYTHON_MODULE_EXTENSION={ext_suffix}",
]
def _get_dsp_arches(toolchain: Optional[str] = None, hexagonarch: Optional[str] = None) -> list[str]:
"""Return a list of Hexagon DSP arch versions to package.
QAI_DSP_ARCHES overrides the default list (comma/whitespace-separated,
e.g. "68,73,75,79"), for packaging a custom set of DSP arches.
"""
env_arches = os.environ.get("QAI_DSP_ARCHES")
if env_arches:
return [a for a in re.split(r"[,\s]+", env_arches.strip()) if a]
if _is_windows():
return ["73", "81"]
else:
return ["68", "73", "75", "79", "81"]
def _compute_version_with_dsp_suffix(default_base: str) -> str:
"""
VERSION = <SDK X.Y.Z from QNN_SDK_ROOT>
When QAI_TOOLCHAINS=x86_64-windows-msvc, append '.1' suffix to distinguish
the x86_64 Windows wheel from the ARM64EC wheel built from the same SDK.
e.g. QNN_SDK_ROOT=.../2.48.40.260702/ + QAI_TOOLCHAINS=x86_64-windows-msvc
=> VERSION = '2.48.40.1'
"""
base = _get_base_version_from_qnn_sdk_root(default_base)
qai_toolchains = os.environ.get("QAI_TOOLCHAINS", "").lower()
if qai_toolchains == "x86_64-windows-msvc":
return f"{base}.1"
return f"{base}"
def _patch_setup_py_version(version3: str) -> None:
"""
Rewrite the literal `VERSION = "X.Y.Z"` constant in this setup.py on disk
so it stays in sync with the SDK version extracted from QNN_SDK_ROOT.
Only the first top-level assignment (the one in the 'Project constants'
block) is changed; the later `VERSION = _compute_version_with_dsp_suffix(...)`
re-assignment is left untouched because it does not match the literal pattern.
version3: 3-part semver (e.g. '2.46.0')
"""
setup_py = Path(__file__).resolve()
text = setup_py.read_text(encoding="utf-8")
new_text, n = re.subn(
r'^VERSION\s*=\s*"[^"]*"',
f'VERSION = "{version3}"',
text,
count=1,
flags=re.MULTILINE,
)
if n and new_text != text:
setup_py.write_text(new_text, encoding="utf-8")
print(f'-- Patched setup.py VERSION = "{version3}"')
def _patch_cpp_version_files(version3: str, version4: str) -> None:
"""
Rewrite BuildId.hpp and common.h with the current SDK version.
version3: 3-part semver (e.g. '2.46.0')
version4: 4-part semver (e.g. '2.46.0.260424')
"""
root = _project_root()
# Patch BuildId.hpp
build_id = root / "src" / "Utils" / "BuildId.hpp"
text = build_id.read_text(encoding="utf-8")
text = re.sub(
r'return std::string\("v[^"]*"\)',
f'return std::string("v{version4}")',
text
)
build_id.write_text(text, encoding="utf-8")
# Patch common.h
common_h = root / "pybind" / "common.h"
text = common_h.read_text(encoding="utf-8")
text = re.sub(
r'#define APPBUILDER_VERSION "[^"]*"',
f'#define APPBUILDER_VERSION "{version3}"',
text
)
common_h.write_text(text, encoding="utf-8")
# Patch setup.py's own VERSION constant
_patch_setup_py_version(version3)
# Re-compute VERSION for wheel metadata (and zip naming) based on environment/args.
VERSION = _compute_version_with_dsp_suffix(VERSION)
def _package_zip_name(arch: str) -> str:
if arch == "ARM64EC":
return f"QAI_AppBuilder-win_arm64ec-QNN{VERSION}-{CONFIG}.zip"
if arch == "aarch64":
return f"QAI_AppBuilder-linux_arm64-QNN{VERSION}-{CONFIG}.zip"
if arch == "x86_64":
return f"QAI_AppBuilder-win_x86_64-QNN{VERSION}-{CONFIG}.zip"
if arch == "x86_64-linux":
return f"QAI_AppBuilder-linux_x86_64-QNN{VERSION}-{CONFIG}.zip"
return f"QAI_AppBuilder-win_arm64-QNN{VERSION}-{CONFIG}.zip"
def _ensure_runtime_pkg_dirs(source_pkg_dir: Path, build_pkg_dir: Path):
"""
Ensure libs directory exists and has __init__.py in BOTH:
- source tree (for editable/dev convenience)
- build_lib tree (for wheel content)
"""
for pkg_dir in (source_pkg_dir, build_pkg_dir):
libs = pkg_dir / "libs"
libs.mkdir(parents=True, exist_ok=True)
_ensure_file(
libs / "__init__.py",
"# This file marks this directory as a Python package.\n",
)
def _copy_runtime_artifacts(
*,
arch: str,
toolchain: Optional[str] = None,
hexagonarch: Optional[str] = None,
source_pkg_dir: Path,
build_pkg_dir: Path,
):
"""
Copy Genie/QNN runtime libraries into:
- <package>/ (Genie.dll/so, app svc, libappbuilder)
- <package>/libs (QNN libs, cat, skel, etc.)
Matching your original behavior.
"""
qnn_root = _get_qnn_sdk_root()
# Determine effective toolchain for DSP arch selection (needed for multi-arch Windows packaging).
effective_toolchain = toolchain
if effective_toolchain is None and _is_windows():
if arch == "ARM64EC":
effective_toolchain = "arm64x-windows-msvc"
elif arch == "x86_64":
effective_toolchain = "x86_64-windows-msvc"
else:
effective_toolchain = "aarch64-windows-msvc"
dsp_arches = _get_dsp_arches(toolchain=effective_toolchain, hexagonarch=hexagonarch)
# Decide LIB_PATH (your original priority/order)
if toolchain is None:
if _is_windows():
if arch == "ARM64EC":
lib_path = qnn_root / "lib" / "arm64x-windows-msvc"
elif arch == "x86_64":
lib_path = qnn_root / "lib" / "x86_64-windows-msvc"
else:
lib_path = qnn_root / "lib" / "aarch64-windows-msvc"
else:
# linux/android probing; prefer QAIRT envsetup defaults
candidates: list[Path] = []
if arch == "aarch64":
candidates = [
qnn_root / "lib" / "aarch64-oe-linux-gcc11.2",
qnn_root / "lib" / "aarch64-android",
]
elif arch == "x86_64-linux":
# QAIRT envsetup.sh uses x86_64-linux-clang for PATH/LD_LIBRARY_PATH
candidates = [
qnn_root / "lib" / "x86_64-linux-clang",
]
# Fallbacks
candidates.append(qnn_root / "lib")
lib_path = None
for cand in candidates:
if (cand / "libGenie.so").exists():
lib_path = cand
break
if lib_path is None:
raise RuntimeError(
'Failed to find "libGenie.so" in QNN SDK lib paths. '
'Set QAI_TOOLCHAINS to the correct <QNN_SDK_ROOT>/lib/<toolchain> subdir.'
)
else:
lib_path = qnn_root / "lib" / toolchain
# Where to put QNN libs
def _do_copy_into(pkg_dir: Path):
libs_dir = pkg_dir / "libs"
# Windows Genie
_copy_if_exists(lib_path / "Genie.dll", pkg_dir / "Genie.dll")
# Keep "lib/Release" staging like your old script
(_project_root() / "lib" / "Release").mkdir(parents=True, exist_ok=True)
_copy_if_exists(lib_path / "Genie.dll", _project_root() / "lib" / "Release" / "Genie.dll")
_copy_if_exists(lib_path / "Genie.lib", _project_root() / "lib" / "Release" / "Genie.lib")
# -------------------------
# DSP-arch-specific files
# -------------------------
# Expect dsp_arches like ["73","81"] (instead of single dsp_arch string)
for _dsp in dsp_arches:
dsp_lib_path = qnn_root / "lib" / f"hexagon-v{_dsp}" / "unsigned"
# cat + skel
_copy_if_exists(
dsp_lib_path / f"libqnnhtpV{_dsp}.cat",
libs_dir / f"libqnnhtpV{_dsp}.cat",
)
_copy_if_exists(
dsp_lib_path / f"libQnnHtpV{_dsp}Skel.so",
libs_dir / f"libQnnHtpV{_dsp}Skel.so",
)
# Per-arch stubs (Windows/Linux)
_copy_if_exists(
lib_path / f"QnnHtpV{_dsp}Stub.dll",
libs_dir / f"QnnHtpV{_dsp}Stub.dll",
)
_copy_if_exists(
lib_path / f"libQnnHtpV{_dsp}Stub.so",
libs_dir / f"libQnnHtpV{_dsp}Stub.so",
)
# -------------------------
# Non-DSP-specific QNN libs
# -------------------------
# Windows QNN dlls
_copy_if_exists(lib_path / "QnnHtp.dll", libs_dir / "QnnHtp.dll")
_copy_if_exists(lib_path / "QnnCpu.dll", libs_dir / "QnnCpu.dll")
_copy_if_exists(lib_path / "QnnGpu.dll", libs_dir / "QnnGpu.dll")
_copy_if_exists(lib_path / "QnnHtpNetRunExtensions.dll", libs_dir / "QnnHtpNetRunExtensions.dll")
_copy_if_exists(lib_path / "QnnHtpPrepare.dll", libs_dir / "QnnHtpPrepare.dll")
_copy_if_exists(lib_path / "QnnSystem.dll", libs_dir / "QnnSystem.dll")
# Linux/Android .so variants
_copy_if_exists(lib_path / "libGenie.so", pkg_dir / "libGenie.so")
_copy_if_exists(lib_path / "libGenie.so", _project_root() / "lib" / "Release" / "libGenie.so")
_copy_if_exists(lib_path / "libQnnHtp.so", libs_dir / "libQnnHtp.so")
_copy_if_exists(lib_path / "libQnnCpu.so", libs_dir / "libQnnCpu.so")
_copy_if_exists(lib_path / "libQnnGpu.so", libs_dir / "libQnnGpu.so")
_copy_if_exists(lib_path / "libQnnHtpNetRunExtensions.so", libs_dir / "libQnnHtpNetRunExtensions.so")
_copy_if_exists(lib_path / "libQnnHtpPrepare.so", libs_dir / "libQnnHtpPrepare.so")
_copy_if_exists(lib_path / "libQnnSystem.so", libs_dir / "libQnnSystem.so")
_do_copy_into(source_pkg_dir)
_do_copy_into(build_pkg_dir)
def _build_root_cmake_project(arch: str, source_pkg_dir: Path, build_pkg_dir: Path, toolchain: Optional[str] = None, hexagonarch: Optional[str] = None,) -> None:
"""
Equivalent to your original build_cmake(), but:
- no chdir side effects leaking outside
- uses list-args subprocess
- copies artifacts into both source package dir and build_lib package dir
"""
root = _project_root()
_require_cmake()
build_dir = root / "build"
lib_dir = root / "lib"
generator_args, is_multi_config = _default_generator_and_args(arch)
build_dir.mkdir(parents=True, exist_ok=True)
cmake_configure = ["cmake", "--no-warn-unused-cli", str(root)] + generator_args + _cmake_python_hints_args()
_cmake_toolchain = os.environ.get("QAI_CMAKE_TOOLCHAIN_FILE")
if _cmake_toolchain:
cmake_configure.append(f"-DCMAKE_TOOLCHAIN_FILE={_cmake_toolchain}")
cmake_configure += _pybind11_cross_ext_args(_cmake_toolchain)
if os.environ.get("APPBUILDER_ENABLE_TFLITE", "").strip().lower() in {"1", "on", "true", "yes"}:
cmake_configure.append("-DAPPBUILDER_ENABLE_TFLITE=ON")
if os.environ.get("APPBUILDER_ENABLE_TFLITE_CPU", "").strip().lower() in {"1", "on", "true", "yes"}:
cmake_configure.append("-DAPPBUILDER_ENABLE_TFLITE_CPU=ON")
if os.environ.get("APPBUILDER_ENABLE_EXECUTORCH", "").strip().lower() in {"1", "on", "true", "yes"}:
cmake_configure.append("-DAPPBUILDER_ENABLE_EXECUTORCH=ON")
if os.environ.get("APPBUILDER_ENABLE_EXECUTORCH_QNN", "").strip().lower() in {"1", "on", "true", "yes"}:
cmake_configure.append("-DAPPBUILDER_ENABLE_EXECUTORCH_QNN=ON")
subprocess.run(cmake_configure, cwd=str(build_dir), check=True)
cmake_build = ["cmake", "--build", str(build_dir)]
if is_multi_config:
cmake_build += ["--config", CONFIG]
subprocess.run(cmake_build, cwd=str(build_dir), check=True)
# Copy produced binaries (your original logic)
# Windows outputs: lib/<CONFIG>/QAIAppSvc.exe etc
if (lib_dir / CONFIG / "QAIAppSvc.exe").exists():
_copy_if_exists(lib_dir / CONFIG / "libappbuilder.dll", source_pkg_dir / "libappbuilder.dll")
_copy_if_exists(lib_dir / CONFIG / "QAIAppSvc.exe", source_pkg_dir / "QAIAppSvc.exe")
_copy_if_exists(lib_dir / CONFIG / "QAIAppSvc.pdb", source_pkg_dir / "QAIAppSvc.pdb")
_copy_if_exists(lib_dir / CONFIG / "libappbuilder.pdb", source_pkg_dir / "libappbuilder.pdb")
_copy_if_exists(lib_dir / CONFIG / "libappbuilder.dll", build_pkg_dir / "libappbuilder.dll")
_copy_if_exists(lib_dir / CONFIG / "QAIAppSvc.exe", build_pkg_dir / "QAIAppSvc.exe")
_copy_if_exists(lib_dir / CONFIG / "QAIAppSvc.pdb", build_pkg_dir / "QAIAppSvc.pdb")
_copy_if_exists(lib_dir / CONFIG / "libappbuilder.pdb", build_pkg_dir / "libappbuilder.pdb")
# Linux output
_copy_if_exists(lib_dir / "libappbuilder.so", source_pkg_dir / "libappbuilder.so")
_copy_if_exists(lib_dir / "libappbuilder.so", build_pkg_dir / "libappbuilder.so")
# Linux QAIAppSvc service executable (cross-process inference). It is built
# alongside libappbuilder.so in lib/. Copy it into the package and make sure
# it keeps the executable bit so posix_spawnp can launch it.
if (lib_dir / "QAIAppSvc").exists():
for pkg_dir in (source_pkg_dir, build_pkg_dir):
dst = pkg_dir / "QAIAppSvc"
_copy_if_exists(lib_dir / "QAIAppSvc", dst)
if dst.exists():
os.chmod(dst, 0o755)
# Ensure libs/__init__.py exists
_ensure_runtime_pkg_dirs(source_pkg_dir, build_pkg_dir)
# Copy QNN/Genie runtime libs
_copy_runtime_artifacts(
arch=arch,
toolchain=toolchain,
hexagonarch=hexagonarch,
source_pkg_dir=source_pkg_dir,
build_pkg_dir=build_pkg_dir,
)
if os.environ.get("APPBUILDER_ENABLE_TFLITE", "").strip().lower() in {"1", "on", "true", "yes"}:
if arch != "aarch64" or _is_windows():
raise RuntimeError("TFLite support requires a Linux ARM64 wheel build")
tflite_root = os.environ.get("TFLITE_ROOT", "")
if not tflite_root:
raise RuntimeError("APPBUILDER_ENABLE_TFLITE requires TFLITE_ROOT")
tflite_root_path = Path(tflite_root)
qnn_root = _get_qnn_sdk_root()
tflite_lib = next((p for p in (
tflite_root_path / "lib" / "libtensorflowlite_c.so",
tflite_root_path / "lib" / "libtflite_c.so",
tflite_root_path / "lib" / "aarch64-linux-gnu" / "libtensorflowlite_c.so",
tflite_root_path / "lib" / "aarch64-linux-gnu" / "libtflite_c.so",
) if p.exists()), None)
if tflite_lib is None:
raise RuntimeError("TFLITE_ROOT must provide lib/libtensorflowlite_c.so or lib/libtflite_c.so")
qnn_delegate = qnn_root / "lib" / (toolchain or "aarch64-oe-linux-gcc11.2") / "libQnnTFLiteDelegate.so"
if not qnn_delegate.exists():
raise RuntimeError(f"Missing QNN TFLite delegate: {qnn_delegate}")
for pkg_dir in (source_pkg_dir, build_pkg_dir):
_copy_if_exists(tflite_lib, pkg_dir / "libs" / tflite_lib.name)
_copy_if_exists(qnn_delegate, pkg_dir / "libs" / qnn_delegate.name)
if os.environ.get("APPBUILDER_ENABLE_TFLITE_CPU", "").strip().lower() in {"1", "on", "true", "yes"}:
if not _is_windows():
raise RuntimeError("APPBUILDER_ENABLE_TFLITE_CPU requires a Windows wheel build")
tflite_root = os.environ.get("TFLITE_ROOT", "")
if not tflite_root:
raise RuntimeError("APPBUILDER_ENABLE_TFLITE_CPU requires TFLITE_ROOT")
tflite_root_path = Path(tflite_root)
tflite_dll = next((p for p in (
tflite_root_path / "bin" / "tensorflowlite_c.dll",
tflite_root_path / "bin" / "tflite_c.dll",
tflite_root_path / "lib" / "tensorflowlite_c.dll",
tflite_root_path / "lib" / "tflite_c.dll",
) if p.exists()), None)
if tflite_dll is None:
raise RuntimeError("TFLITE_ROOT must provide a Windows tensorflowlite_c.dll or tflite_c.dll")
for pkg_dir in (source_pkg_dir, build_pkg_dir):
_copy_if_exists(tflite_dll, pkg_dir / "libs" / tflite_dll.name)
if os.environ.get("APPBUILDER_ENABLE_EXECUTORCH", "").strip().lower() in {"1", "on", "true", "yes"}:
executorch_root = os.environ.get("EXECUTORCH_ROOT", "")
if not executorch_root:
raise RuntimeError("APPBUILDER_ENABLE_EXECUTORCH requires EXECUTORCH_ROOT")
executorch_root_path = Path(executorch_root)
shared_library_basenames = [
"extension_module",
"extension_tensor",
"extension_data_loader",
"extension_flat_tensor",
"extension_named_data_map",
"extension_evalue_util",
"extension_threadpool",
"extension_runner_util",
"executorch_core",
"executorch",
"executorch_shared",
"portable_ops_lib",
"portable_kernels",
"kernels_util_all_deps",
"pthreadpool",
"cpuinfo",
"quantized_ops_lib",
"quantized_kernels",
"executorch_backend_xnnpack",
"xnnpack_backend",
"executorch_backend_qnn",
"qnn_executorch_backend",
]
runtime_patterns = [
*(f"lib{name}.so*" for name in shared_library_basenames),
*(f"{name}.dll*" for name in shared_library_basenames),
]
runtime_paths = []
for library_dir in (
executorch_root_path / "lib",
executorch_root_path / "build" / "lib",
):
for pattern in runtime_patterns:
runtime_paths.extend(sorted(library_dir.glob(pattern)))
found_runtime = {
path.name: path for path in runtime_paths if path.is_file()
}
if os.environ.get("APPBUILDER_ENABLE_EXECUTORCH_QNN", "").strip().lower() in {"1", "on", "true", "yes"} and not any("qnn" in name.lower() for name in found_runtime):
raise RuntimeError("APPBUILDER_ENABLE_EXECUTORCH_QNN requires a packaged ExecuTorch QNN backend shared library")
for pkg_dir in (source_pkg_dir, build_pkg_dir):
for runtime_path in found_runtime.values():
_copy_if_exists(runtime_path, pkg_dir / "libs" / runtime_path.name)
def _build_release_zip(arch: str):
"""
Equivalent to your original build_release().
It packages 'lib/package' and writes to dist/<PACKAGE_ZIP>.
"""
root = _project_root()
tmp_path = root / "lib" / "package"
include_path = tmp_path / "include"
dist_dir = root / "dist"
pkg_zip = dist_dir / _package_zip_name(arch)
tmp_path.mkdir(parents=True, exist_ok=True)
include_path.mkdir(parents=True, exist_ok=True)
lib_dir = root / "lib"
# Windows artifacts
if (lib_dir / CONFIG / "QAIAppSvc.exe").exists():
_copy_if_exists(lib_dir / CONFIG / "libappbuilder.dll", tmp_path / "libappbuilder.dll")
_copy_if_exists(lib_dir / CONFIG / "libappbuilder.lib", tmp_path / "libappbuilder.lib")
_copy_if_exists(lib_dir / CONFIG / "QAIAppSvc.exe", tmp_path / "QAIAppSvc.exe")
_copy_if_exists(lib_dir / CONFIG / "libappbuilder.pdb", tmp_path / "libappbuilder.pdb")
_copy_if_exists(lib_dir / CONFIG / "QAIAppSvc.pdb", tmp_path / "QAIAppSvc.pdb")
# Linux artifact
_copy_if_exists(lib_dir / "libappbuilder.so", tmp_path / "libappbuilder.so")
if (lib_dir / "QAIAppSvc").exists():
_copy_if_exists(lib_dir / "QAIAppSvc", tmp_path / "QAIAppSvc")
if (tmp_path / "QAIAppSvc").exists():
os.chmod(tmp_path / "QAIAppSvc", 0o755)
# Headers
_copy_if_exists(root / "src" / "LibAppBuilder.hpp", include_path / "LibAppBuilder.hpp")
_copy_if_exists(root / "src" / "Lora.hpp", include_path / "Lora.hpp")
_zip_dir(tmp_path, pkg_zip)
def _clean_artifacts():
"""
NOTE: We DO NOT delete dist/ because wheel output is there.
"""
root = _project_root()
source_pkg_dir = root / "script" / PACKAGE_NAME
libs_dir = source_pkg_dir / "libs"
# egg-info/build/lib
_safe_rmtree(root / "build")
_safe_rmtree(root / "lib")
_safe_rmtree(root / "script" / f"{PACKAGE_NAME}.egg-info")
# Remove known binaries under source package dir
for fname in [
"libappbuilder.dll", "QAIAppSvc.exe", "QAIAppSvc.pdb", "libappbuilder.pdb",
"libappbuilder.so", "QAIAppSvc", "Genie.dll", "libGenie.so"
]:
p = source_pkg_dir / fname
if p.exists():
p.unlink()
# Remove runtime QNN libs copied into source libs dir (keep __init__.py)
if libs_dir.exists():
for p in libs_dir.iterdir():
if p.is_file() and p.name != "__init__.py":
p.unlink()
# ---------------------------
# CMake extension & commands
# ---------------------------
class CMakeExtension(Extension):
def __init__(self, name: str, sourcedir: str = "") -> None:
super().__init__(name, sources=[])
self.sourcedir = os.fspath((Path(sourcedir).resolve()))
class QaiCMakeBuild(build_ext):
"""
- Runs root CMake build (your old build_cmake) before building the pybind extension
- Supports --toolchains / --hexagonarch as setuptools command options
- Also reads env vars QAI_TOOLCHAINS / QAI_HEXAGONARCH for PEP517 friendliness
"""
user_options = build_ext.user_options + [
("toolchains=", None, "QNN toolchain subdir name under <QNN_SDK_ROOT>/lib/ (e.g. aarch64-windows-msvc)"),
("hexagonarch=", None, "Hexagon DSP arch version (e.g. 81/73/68)"),
]
def initialize_options(self):
super().initialize_options()
self.toolchains = None
self.hexagonarch = None
def finalize_options(self):
super().finalize_options()
# env var fallback (PEP517-friendly)
if self.toolchains is None:
self.toolchains = os.environ.get("QAI_TOOLCHAINS")
if self.hexagonarch is None:
self.hexagonarch = os.environ.get("QAI_HEXAGONARCH")
def run(self):
root = _project_root()
arch = _detect_arch()
print(f"-- Arch: {arch}")
# Auto-patch C++ version headers from QNN_SDK_ROOT
qnn_root_str = os.environ.get("QNN_SDK_ROOT", "")
v3 = _extract_semver3_from_text(qnn_root_str)
v4 = _extract_semver4_from_text(qnn_root_str)
if not v3 or not v4:
raise RuntimeError(
f'Cannot extract version from QNN_SDK_ROOT="{qnn_root_str}". '
'Expected a path containing a version like "2.46.0.260424".'
)
_patch_cpp_version_files(v3, v4)
print(f"-- Version: {v4}")
# Ensure build_lib package dirs
build_py_cmd = self.get_finalized_command("build_py")
build_lib = Path(build_py_cmd.build_lib)
source_pkg_dir = root / "script" / PACKAGE_NAME
build_pkg_dir = build_lib / PACKAGE_NAME
_ensure_runtime_pkg_dirs(source_pkg_dir, build_pkg_dir)
# Root CMake build + copy runtime libs (your old build_cmake)
_build_root_cmake_project(
arch=arch,
toolchain=self.toolchains,
hexagonarch=self.hexagonarch,
source_pkg_dir=source_pkg_dir,
build_pkg_dir=build_pkg_dir,
)
# Now build the actual extension via cmake (pybind/)
super().run()
def build_extension(self, ext: CMakeExtension) -> None:
"""
Your original CMakeBuild.build_extension logic, modernized:
- uses list args (no shell quoting issues)
- preserves ARM64EC generator behavior and Python hints
"""
arch = _detect_arch()
print(f"build_extension-- Arch: {arch}")
ext_fullpath = Path.cwd() / self.get_ext_fullpath(ext.name)
extdir = ext_fullpath.parent.resolve()
cfg = CONFIG
cmake_generator_env = os.environ.get("CMAKE_GENERATOR", "")
# Base CMake args
cmake_args = [
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}{os.sep}",
f"-DCMAKE_BUILD_TYPE={cfg}",
*_cmake_python_hints_args(),
]
# generator / arch selection
generator_args, is_multi_config = _default_generator_and_args(arch)
# If user explicitly set CMAKE_GENERATOR, don't force ours, but keep arch behavior
# (mimic your original logic about "single-config" and "contains_arch")
single_config = any(x in cmake_generator_env for x in {"NMake", "Ninja"})
contains_arch = any(x in cmake_generator_env for x in {"ARM", "Win64"})
if arch != "aarch64" and not single_config and not contains_arch:
# We already provide -A in generator_args (VS). Keep consistent.
pass
if is_multi_config:
cmake_args.append(f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}")
# Build args
build_args = []
if is_multi_config:
build_args += ["--config", cfg]
if "CMAKE_BUILD_PARALLEL_LEVEL" not in os.environ:
if hasattr(self, "parallel") and self.parallel:
build_args += [f"-j{self.parallel}"]
build_temp = Path(self.build_temp) / ext.name
build_temp.mkdir(parents=True, exist_ok=True)
# Configure & build
cmake_configure = ["cmake", "--no-warn-unused-cli", ext.sourcedir] + generator_args + cmake_args
_cmake_toolchain = os.environ.get("QAI_CMAKE_TOOLCHAIN_FILE")
if _cmake_toolchain:
cmake_configure.append(f"-DCMAKE_TOOLCHAIN_FILE={_cmake_toolchain}")
cmake_configure += _pybind11_cross_ext_args(_cmake_toolchain)
if os.environ.get("APPBUILDER_ENABLE_TFLITE", "").strip().lower() in {"1", "on", "true", "yes"}:
cmake_configure.append("-DAPPBUILDER_ENABLE_TFLITE=ON")
if os.environ.get("APPBUILDER_ENABLE_TFLITE_CPU", "").strip().lower() in {"1", "on", "true", "yes"}:
cmake_configure.append("-DAPPBUILDER_ENABLE_TFLITE_CPU=ON")
if os.environ.get("APPBUILDER_ENABLE_EXECUTORCH", "").strip().lower() in {"1", "on", "true", "yes"}:
cmake_configure.append("-DAPPBUILDER_ENABLE_EXECUTORCH=ON")
if os.environ.get("APPBUILDER_ENABLE_EXECUTORCH_QNN", "").strip().lower() in {"1", "on", "true", "yes"}:
cmake_configure.append("-DAPPBUILDER_ENABLE_EXECUTORCH_QNN=ON")
subprocess.run(cmake_configure, cwd=str(build_temp), check=True)
cmake_build = ["cmake", "--build", "."] + build_args
subprocess.run(cmake_build, cwd=str(build_temp), check=True)
class QaiBdistWheel(bdist_wheel):
"""
Preserve old behavior:
- accept legacy CLI options on bdist_wheel: --hexagonarch / --toolchains
- propagate them to build_ext via env vars (PEP517-friendly)
- build wheel
- build release zip
- clean artifacts
"""
user_options = bdist_wheel.user_options + [
("toolchains=", None, "QNN toolchain subdir name under <QNN_SDK_ROOT>/lib/ (e.g. aarch64-windows-msvc)"),
("hexagonarch=", None, "Hexagon DSP arch version (e.g. 81/73/68)"),
]
def initialize_options(self):
super().initialize_options()
self.toolchains = None
self.hexagonarch = None
def finalize_options(self):
# Derive _PYTHON_HOST_PLATFORM from QAI_CMAKE_TOOLCHAIN_FILE before
# super().finalize_options() calls get_platform() to set plat_name.
if not os.environ.get("_PYTHON_HOST_PLATFORM"):
toolchain_file = os.environ.get("QAI_CMAKE_TOOLCHAIN_FILE", "")
if toolchain_file:
host_plat = _host_platform_from_toolchain(toolchain_file)
if host_plat:
os.environ["_PYTHON_HOST_PLATFORM"] = host_plat
print(f"-- _PYTHON_HOST_PLATFORM={host_plat} (derived from toolchain)")
super().finalize_options()
# env var fallback
if self.toolchains is None:
self.toolchains = os.environ.get("QAI_TOOLCHAINS")
if self.hexagonarch is None:
self.hexagonarch = os.environ.get("QAI_HEXAGONARCH")
def run(self):
# Propagate to env vars so build_ext can see them
if self.toolchains:
os.environ["QAI_TOOLCHAINS"] = str(self.toolchains)
if self.hexagonarch:
os.environ["QAI_HEXAGONARCH"] = str(self.hexagonarch)