forked from pytgcalls/ntgcalls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
199 lines (174 loc) · 6.85 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
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
import base64
import platform
import shutil
import multiprocessing
import os
import re
import subprocess
import sys
from pathlib import Path
from urllib.request import urlopen
from setuptools import Extension, setup, Command
from setuptools.command.build_ext import build_ext
base_path = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(base_path, 'CMakeLists.txt'), 'r', encoding='utf-8') as f:
regex = re.compile(r'VERSION ([A-Za-z0-9.]+)', re.MULTILINE)
version = re.findall(regex, f.read())[1]
if version.count('.') == 3:
major, minor, path_, tweak = version.split('.')
version = f'{major}.{minor}.{path_}.dev{tweak}'
class CMakeExtension(Extension):
def __init__(self, name: str, sourcedir: str = "") -> None:
super().__init__(name, sources=[])
self.sourcedir = os.fspath(Path(sourcedir).resolve())
def install_clang(path: Path):
url = 'https://chromium.googlesource.com/chromium/src/tools/+/refs/heads/main/clang/scripts/update.py?format=text'
os.mkdir(path)
download_py = Path(path, 'download.py')
with urlopen(url) as response:
with open(download_py, 'w') as file:
file.write(base64.b64decode(response.read()).decode('utf-8'))
subprocess.run(
[sys.executable, download_py, '--output-dir', path],
check=True
)
def get_os():
return subprocess.run(["uname", "-o"], stdout=subprocess.PIPE, text=True).stdout.strip()
def get_os_cmake_args():
if sys.platform.startswith("win32"):
pass
elif sys.platform.startswith("darwin"):
return [
"-DCMAKE_OSX_ARCHITECTURES=arm64",
"-G",
"Xcode",
]
elif get_os() == "Android":
raise NotImplementedError("Android is not supported yet")
elif sys.platform.startswith("linux"):
clang_c, clang_cxx = "clang-17", "clang++-17"
if platform.processor() != 'aarch64':
clang_path = Path(Path.cwd(), 'clang')
if not Path(clang_path, 'bin').exists():
install_clang(clang_path)
clang_c = Path(clang_path, 'bin', 'clang')
clang_cxx = Path(clang_path, 'bin', 'clang++')
return [
f"-DCMAKE_C_COMPILER={clang_c}",
f"-DCMAKE_CXX_COMPILER={clang_cxx}",
]
return []
class CMakeBuild(build_ext):
def build_extension(self, ext: CMakeExtension) -> None:
ext_fullpath = Path.cwd() / self.get_ext_fullpath(ext.name)
extdir = ext_fullpath.parent.resolve()
cfg = "RelWithDebInfo" if "dev" in version else "Release"
cmake_args = [
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY={extdir}{os.sep}",
f"-DPYTHON_EXECUTABLE={sys.executable}",
f"-DCMAKE_BUILD_TYPE={cfg}",
f"-DPY_VERSION_INFO={version}",
f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}",
]
build_args = [
"--config", cfg,
f"-j{multiprocessing.cpu_count()}",
]
cmake_args += get_os_cmake_args()
build_temp = Path(self.build_temp) / ext.name
if not build_temp.exists():
build_temp.mkdir(parents=True)
subprocess.run(
["cmake", ext.sourcedir, *cmake_args], cwd=build_temp, check=True
)
subprocess.run(
["cmake", "--build", ".", *build_args], cwd=build_temp, check=True
)
class SharedCommand(Command):
description = 'Generate shared-libs files'
user_options = [
('no-preserve-cache', None, "Do not preserve cache"),
]
# noinspection PyAttributeOutsideInit
def initialize_options(self):
self.no_preserve_cache = False
def finalize_options(self):
pass
# noinspection PyMethodMayBeStatic
def run(self):
cfg = "RelWithDebInfo" if "dev" in version else "Release"
cmake_args = [
f'-DCMAKE_BUILD_TYPE={cfg}',
]
cmake_args += get_os_cmake_args()
build_args = [
'--config', cfg,
f'-j{multiprocessing.cpu_count()}',
]
build_temp = Path('shared-build')
if not build_temp.exists():
build_temp.mkdir(parents=True)
source_dir = os.path.dirname(os.path.abspath(__file__))
subprocess.run(
['cmake', source_dir, *cmake_args], cwd=build_temp, check=True
)
subprocess.run(
["cmake", "--build", ".", *build_args], cwd=build_temp, check=True
)
release_path = Path(build_temp, 'ntgcalls')
tmp_release_path = Path(release_path, cfg)
build_output = Path("shared-output")
if build_output.exists():
shutil.rmtree(build_output)
build_output.mkdir(parents=True)
include_output = Path(build_output, 'include')
include_output.mkdir(parents=True)
if tmp_release_path.exists():
release_path = tmp_release_path
for file in os.listdir(release_path):
if file.endswith('.dll') or file.endswith('.so') or file.endswith('.dylib'):
lib_output = Path(build_output, file)
shutil.move(Path(release_path, file), lib_output)
shutil.copy(Path(source_dir, 'include', 'ntgcalls.h'), include_output)
if self.no_preserve_cache:
shutil.rmtree(build_temp)
boost_dir = Path(source_dir, 'deps', 'boost')
for boost_build in os.listdir(boost_dir):
if boost_build.startswith('boost_'):
shutil.rmtree(Path(boost_dir, boost_build))
print("Cleanup successfully")
return
raise FileNotFoundError("No library files found")
with open(os.path.join(base_path, 'README.md'), encoding='utf-8') as f:
readme = f.read()
setup(
name='ntgcalls',
version=version,
long_description=readme,
long_description_content_type='text/markdown',
url='https://github.com/pytgcalls/ntgcalls',
author='Laky-64',
author_email='[email protected]',
classifiers=[
'License :: OSI Approved :: '
'GNU Lesser General Public License v3 (LGPLv3)',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3 :: Only',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
'Programming Language :: Python :: Implementation :: CPython',
'Programming Language :: Python :: Implementation :: PyPy',
],
ext_modules=[CMakeExtension("ntgcalls")],
cmdclass={
'build_ext': CMakeBuild,
'build_shared': SharedCommand
},
zip_safe=False,
extras_require={"test": ["pytest>=6.0"]},
python_requires=">=3.7",
)