forked from randovania/py-dolphin-memory-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.py
170 lines (142 loc) · 4.93 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
import os
import platform
import re
import subprocess
import sys
from pathlib import Path
from Cython.Build import cythonize
from Cython.Build.Dependencies import default_create_extension
from setuptools import setup
from setuptools.command.build_ext import build_ext
from setuptools.extension import Extension
is_windows = platform.system() == "Windows"
file_dir = Path(__file__).parent.relative_to(Path().absolute())
class CMakeExtension(Extension):
def __init__(self, name, sources, cmake_options, *args, **kw):
super().__init__(name, sources, *args, **kw)
cmake_options["dir"] = os.fspath(Path(cmake_options["dir"]).resolve())
self.cmake_options = cmake_options
class CMakeBuild(build_ext):
def run(self):
try:
out = subprocess.run(
["cmake", "--version"], stdout=subprocess.PIPE, check=True, text=True
)
except (FileNotFoundError, subprocess.CalledProcessError):
raise RuntimeError(
"CMake must be installed to build the following extensions: "
+ ", ".join(e.name for e in self.extensions)
)
if is_windows:
cmake_version = tuple(
int(d)
for d in re.search(r"version\s*([\d.]+)", out.stdout)
.group(1)
.split(".")
)
if cmake_version < (3, 26, 2):
raise RuntimeError("CMake >= 3.1.0 is required on Windows")
super().run()
def build_extension(self, ext):
cmake_options = ext.cmake_options
extdir = Path(self.get_ext_fullpath(ext.name)).parent.resolve()
cmake_args = [
f"-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY={extdir}",
f"-DPYTHON_EXECUTABLE={sys.executable}",
"-DCMAKE_POSITION_INDEPENDENT_CODE=YES",
]
library_output_dir = extdir
cfg = "Debug" if self.debug else "Release"
build_args = ["--config", cfg]
if self.verbose:
build_args.append("--verbose")
if is_windows:
cmake_args += [f"-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}"]
if sys.maxsize > 2**32:
cmake_args += ["-A", "x64"]
build_args += ["--", "/m", "/verbosity:minimal"]
library_name_format = "{}.lib"
else:
cmake_args += ["-DCMAKE_BUILD_TYPE=" + cfg]
build_args += ["--", "-j2"]
library_name_format = "lib{}.a"
env = os.environ.copy()
env["CXXFLAGS"] = '{} -DVERSION_INFO=\\"{}\\"'.format(
env.get("CXXFLAGS", ""), self.distribution.get_version()
)
Path(self.build_temp).mkdir(parents=True, exist_ok=True)
subprocess.run(
["cmake", cmake_options["dir"]] + cmake_args,
cwd=self.build_temp,
env=env,
check=True,
)
if self.force:
subprocess.run(
["cmake", "--build", ".", "--target", "clean"] + build_args,
cwd=self.build_temp,
check=True,
)
for target, target_output in cmake_options["targets"].items():
if self.verbose:
print(["cmake", "--build", ".", "--target", target] + build_args)
subprocess.run(
["cmake", "--build", ".", "--target", target] + build_args,
cwd=self.build_temp,
check=True,
)
ext.extra_objects.append(
os.fspath(
library_output_dir.joinpath(library_name_format.format(target))
)
)
super().build_extension(ext)
cpp_code_dir = os.fspath(file_dir.joinpath("Source"))
custom_include_paths = [
cpp_code_dir,
]
extra_compile_args = []
if is_windows:
extra_compile_args.append("-DUNICODE")
extra_compile_args.append("/std:c++17")
extra_compile_args.append("/MD")
else:
extra_compile_args.append("-std=c++17")
ext_modules = [
CMakeExtension(
"dolphin_memory_engine._dolphin_memory_engine",
[
os.fspath(file_dir.joinpath("_dolphin_memory_engine.pyx")),
],
cmake_options={
"dir": cpp_code_dir,
"targets": {
"dolphin-memory-engine": "lib",
},
},
language="c++",
extra_compile_args=extra_compile_args,
extra_objects=[],
)
]
def create_extension(template, kwds):
""""""
kwds["cmake_options"] = template.cmake_options
return default_create_extension(template, kwds)
cythonized_ext_modules = cythonize(
ext_modules,
include_path=custom_include_paths,
compiler_directives={
"embedsignature": True,
"language_level": "3",
},
create_extension=create_extension,
)
for ext_module in cythonized_ext_modules:
ext_module.include_dirs = custom_include_paths
setup(
cmdclass={
"build_ext": CMakeBuild,
},
ext_modules=cythonized_ext_modules,
)