-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsetup.py
More file actions
115 lines (87 loc) · 2.67 KB
/
setup.py
File metadata and controls
115 lines (87 loc) · 2.67 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
"""setup for piqtree."""
import platform
import subprocess
from pathlib import Path
from pybind11.setup_helpers import Pybind11Extension, build_ext
from setuptools import setup
LIBRARY_DIR = "src/piqtree/_libiqtree/"
IQTREE_LIB_NAME = "iqtree"
def get_brew_prefix(package: str) -> Path:
"""Get the prefix path for a specific Homebrew package."""
return Path(
subprocess.check_output(["brew", "--prefix", package]).strip().decode("utf-8"),
)
extra_libs = []
extra_compile_args = []
include_dirs = []
library_dirs = []
def include_dlls() -> None:
import shutil
from delvewheel._dll_utils import get_all_needed
needed_dll_paths, _, _, _ = get_all_needed(
LIBRARY_DIR + f"{IQTREE_LIB_NAME}.dll",
set(),
None,
"raise",
False, # noqa: FBT003
False, # noqa: FBT003
)
for dll_path in needed_dll_paths:
shutil.copy(dll_path, LIBRARY_DIR)
def setup_windows() -> None:
include_dlls()
def setup_macos() -> None:
brew_prefix_llvm = get_brew_prefix("llvm")
brew_prefix_libomp = get_brew_prefix("libomp")
# Define OpenMP flags and libraries for macOS
extra_compile_args.extend(["-Xpreprocessor", "-fopenmp"])
extra_libs.extend(["z", "omp"])
# Use the paths from Homebrew for libomp
include_dirs.extend([str(brew_prefix_libomp / "include")])
library_dirs.extend(
[
str(brew_prefix_libomp / "lib"),
str(brew_prefix_llvm / "lib"),
],
)
def setup_linux() -> None:
extra_compile_args.extend(["-fopenmp"])
extra_libs.extend(["z", "gomp"])
match system := platform.system():
case "Windows":
setup_windows()
case "Darwin":
setup_macos()
case "Linux":
setup_linux()
case _:
msg = f"Unsupported platform: {system}"
raise ValueError(msg)
ext_modules = [
Pybind11Extension(
"_piqtree",
["src/piqtree/_libiqtree/_piqtree.cpp"],
library_dirs=[
*library_dirs,
LIBRARY_DIR,
],
libraries=[IQTREE_LIB_NAME, *extra_libs],
extra_compile_args=extra_compile_args,
include_dirs=include_dirs,
),
]
class StripBuildExt(build_ext):
def run(self) -> None:
super().run()
system = platform.system()
flag = "-x" if system == "Darwin" else "--strip-unneeded"
for ext in self.extensions:
ext_path = self.get_ext_fullpath(ext.name)
subprocess.run(["strip", flag, ext_path], check=True)
setup(
name="piqtree",
ext_modules=ext_modules,
cmdclass={"build_ext": StripBuildExt},
zip_safe=False,
package_data={"piqtree": ["_libiqtree/*.dll"]},
)