-
Notifications
You must be signed in to change notification settings - Fork 40
/
setup.py
145 lines (119 loc) · 4.46 KB
/
setup.py
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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""Setup dot py."""
import os
import platform
import subprocess
import sys
import urllib.request
from pathlib import Path
from setuptools import Extension, setup
from setuptools.command.build_ext import build_ext
CNS_BINARIES = {
"x86_64-linux": "https://surfdrive.surf.nl/files/index.php/s/BWa5OimzbNliTi6/download",
"x86_64-darwin": "https://surfdrive.surf.nl/files/index.php/s/3Fzzte0Zx0L8GTY/download",
"arm64-darwin": "https://surfdrive.surf.nl/files/index.php/s/bYB3xPWf7iwo07X/download",
"aarch64-linux": "https://surfdrive.surf.nl/files/index.php/s/3rHpxcufHGrntHn/download",
}
cpp_extensions = [
Extension(
"haddock.bin.contact_fcc",
sources=["src/haddock/deps/contact_fcc.cpp"],
extra_compile_args=["-O2"],
),
Extension(
"haddock.bin.fast_rmsdmatrix",
sources=["src/haddock/deps/fast-rmsdmatrix.c"],
extra_compile_args=["-Wall", "-O3", "-march=native", "-std=c99"],
extra_link_args=["-lm"],
),
]
class CustomBuild(build_ext):
"""Custom build handles the C/C++ dependencies"""
def run(self):
"""Run the custom build"""
print("Building HADDOCK3 C/C++ binary dependencies...")
self.build_executable(
name="contact_fcc",
cmd=["g++", "-O2", "-o", "contact_fcc", "src/haddock/deps/contact_fcc.cpp"],
)
self.build_executable(
name="fast-rmsdmatrix",
cmd=[
"gcc",
"-Wall",
"-O3",
"-march=native",
"-std=c99",
"-o",
"fast-rmsdmatrix",
"src/haddock/deps/fast-rmsdmatrix.c",
"-lm",
],
)
print("Downloading the CNS binary...")
self.download_cns()
# Run the standard build
build_ext.run(self)
def build_executable(self, name, cmd):
"""Helper function to execute the build command"""
try:
subprocess.check_call(cmd)
# Ensure the source bin directory exists
src_bin_dir = Path("src", "haddock", "bin")
src_bin_dir.mkdir(exist_ok=True, parents=True)
# Move the built executable to the source bin directory
src_bin_exec = Path(src_bin_dir, name)
if src_bin_exec.exists():
src_bin_exec.unlink()
self.move_file(name, src_bin_exec)
# If build_lib exists, also copy to there
if hasattr(self, "build_lib"):
build_bin_dir = Path(self.build_lib, "haddock", "bin")
build_bin_dir.mkdir(exist_ok=True, parents=True)
self.copy_file(Path(src_bin_dir, name), Path(build_bin_dir, name))
print(f"Successfully built and moved {name}")
except subprocess.CalledProcessError as e:
print(f"Error building {name}: {e}")
raise
def download_cns(self):
"""Helper function to download the CNS binary"""
arch = self.get_arch()
if arch not in CNS_BINARIES:
print(f"Unknown architecture: {arch}")
sys.exit(1)
cns_binary_url = CNS_BINARIES[arch]
src_bin_dir = Path("src", "haddock", "bin")
src_bin_dir.mkdir(exist_ok=True, parents=True)
cns_exec = Path(src_bin_dir, "cns")
status, msg = self.download_file(cns_binary_url, cns_exec)
if not status:
print(msg)
sys.exit(1)
# Make it executable
os.chmod(cns_exec, 0o755)
# If build_lib exists, also copy to there
if hasattr(self, "build_lib"):
install_bin_dir = Path(self.build_lib, "haddock", "bin")
install_bin_dir.mkdir(exist_ok=True, parents=True)
self.copy_file(cns_exec, Path(install_bin_dir, "cns"))
@staticmethod
def download_file(url, dest) -> tuple[bool, str]:
"""Download a file from a URL"""
try:
urllib.request.urlretrieve(url, dest)
return True, "Download successful"
except Exception as e: # pylint: disable=broad-except
return False, f"Download failed: {e}"
@staticmethod
def get_arch():
"""Helper function to figure out the architetchure"""
system = platform.system().lower()
machine = platform.machine().lower()
return f"{machine}-{system}"
setup(
cmdclass={
"build_ext": CustomBuild,
},
ext_modules=cpp_extensions,
)