-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
66 lines (50 loc) · 1.57 KB
/
Copy pathsetup.py
File metadata and controls
66 lines (50 loc) · 1.57 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
from __future__ import annotations
import os
import shutil
import subprocess
import sys
from pathlib import Path
from setuptools import setup
from setuptools.command.build_py import build_py as _build_py
ROOT = Path(__file__).resolve().parent
NATIVE_SOURCE = ROOT / "csrc" / "librangemap_core.c"
NATIVE_HEADER = ROOT / "csrc" / "librangemap_core.h"
NATIVE_OUTPUT = ROOT / "librangemap" / "native" / "librangemap_core.dll"
def _find_zig() -> str | None:
candidate = shutil.which("zig")
if candidate:
return candidate
local_appdata = os.environ.get("LOCALAPPDATA")
if local_appdata:
packages_root = Path(local_appdata) / "Microsoft" / "WinGet" / "Packages"
if packages_root.exists():
matches = sorted(packages_root.rglob("zig.exe"))
if matches:
return str(matches[0])
return None
def _build_native_library() -> None:
if not NATIVE_SOURCE.exists() or not NATIVE_HEADER.exists():
return
zig = _find_zig()
if zig is None:
raise RuntimeError("Zig is required to build the libRangeMap native C core.")
NATIVE_OUTPUT.parent.mkdir(parents=True, exist_ok=True)
command = [
zig,
"cc",
"-shared",
"-O3",
"-DLRM_BUILD_DLL",
"-o",
str(NATIVE_OUTPUT),
str(NATIVE_SOURCE),
]
subprocess.run(command, cwd=ROOT, check=True)
class build_py(_build_py):
def run(self):
_build_native_library()
super().run()
setup(
cmdclass={"build_py": build_py},
package_data={"librangemap": ["native/*.dll"]},
)