-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.py
More file actions
96 lines (79 loc) · 3.45 KB
/
setup.py
File metadata and controls
96 lines (79 loc) · 3.45 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
"""
setup.py — responsible only for the optional sdm_cpp C++ extension.
All project metadata lives in pyproject.toml.
The sdm_cpp extension is built only when pybind11 is available. If it is
absent the package still installs and works correctly using the pure-Python
SDM implementation. To enable the C++ acceleration:
pip install pybind11
pip install -e . --no-build-isolation
OpenMP (optional, for multi-threaded acceleration)
----------------------------------------------------
macOS : brew install libomp
Linux : installed with GCC by default
Windows: included with MSVC
"""
from __future__ import annotations
import subprocess
import sys
from setuptools import setup
try:
from setuptools import Extension
import pybind11
pybind11_include = pybind11.get_include()
# ── OpenMP detection ──────────────────────────────────────────────────────
def _find_openmp() -> tuple[list[str], list[str]]:
"""Return (extra_compile_args, extra_link_args) for OpenMP, or ([], [])."""
if sys.platform == "darwin":
import os
archflags = os.environ.get("ARCHFLAGS", "")
if "x86_64" in archflags:
print("[sdm_cpp] Cross-compilation detected — skipping OpenMP.")
return [], []
try:
result = subprocess.run(
["brew", "--prefix", "libomp"],
capture_output=True, text=True, timeout=15,
)
if result.returncode == 0:
prefix = result.stdout.strip()
omp_h = f"{prefix}/include/omp.h"
omp_lib = f"{prefix}/lib/libomp.dylib"
import os.path
if os.path.isfile(omp_h) and os.path.isfile(omp_lib):
print(f"[sdm_cpp] OpenMP found via Homebrew libomp: {prefix}")
return (
["-Xpreprocessor", "-fopenmp", f"-I{prefix}/include"],
[f"-L{prefix}/lib", "-lomp"],
)
print(f"[sdm_cpp] Homebrew libomp found but headers/lib missing — skipping.")
except (FileNotFoundError, subprocess.TimeoutExpired):
pass
print("[sdm_cpp] libomp not found — building without OpenMP (single-threaded).")
return [], []
if sys.platform.startswith("linux"):
return ["-fopenmp"], ["-fopenmp"]
if sys.platform == "win32":
return ["/openmp"], []
return [], []
omp_compile, omp_link = _find_openmp()
if sys.platform == "win32":
base_compile = ["/O2", "/std:c++17"] + omp_compile
else:
base_compile = ["-O3", "-std=c++17"] + omp_compile
sdm_cpp_ext = Extension(
name="sdm_cpp",
sources=["shelxfile/sdm_cpp/sdm_cpp.cpp"],
include_dirs=[pybind11_include],
extra_compile_args=base_compile,
extra_link_args=omp_link,
language="c++",
)
setup(ext_modules=[sdm_cpp_ext])
print("[sdm_cpp] C++ extension will be compiled.")
except ImportError:
print(
"[sdm_cpp] pybind11 not found — skipping C++ extension build.\n"
" Install pybind11 and re-run to enable the C++ acceleration:\n"
" pip install pybind11 && pip install -e . --no-build-isolation"
)
setup()